home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / perl / part04 < prev    next >
Encoding:
Internet Message Format  |  1988-01-30  |  48.6 KB

  1. Subject:  v13i004:  Perl, a "replacement" for awk and sed, Part04/10
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
  7. Posting-number: Volume 13, Issue 4
  8. Archive-name: perl/part04
  9.  
  10.  
  11.  
  12. #! /bin/sh
  13.  
  14. # Make a new directory for the perl sources, cd to it, and run kits 1
  15. # thru 10 through sh.  When all 10 kits have been run, read README.
  16.  
  17. echo "This is perl 1.0 kit 4 (of 10).  If kit 4 is complete, the line"
  18. echo '"'"End of kit 4 (of 10)"'" will echo at the end.'
  19. echo ""
  20. export PATH || (echo "You didn't use sh, you clunch." ; kill $$)
  21. echo Extracting perl.man.2
  22. sed >perl.man.2 <<'!STUFFY!FUNK!' -e 's/X//'
  23. X''' Beginning of part 2
  24. X''' $Header: perl.man.2,v 1.0 87/12/18 16:18:41 root Exp $
  25. X'''
  26. X''' $Log:    perl.man.2,v $
  27. X''' Revision 1.0  87/12/18  16:18:41  root
  28. X''' Initial revision
  29. X''' 
  30. X'''
  31. X.Ip "goto LABEL" 8 6
  32. XFinds the statement labeled with LABEL and resumes execution there.
  33. XCurrently you may only go to statements in the main body of the program
  34. Xthat are not nested inside a do {} construct.
  35. XThis statement is not implemented very efficiently, and is here only to make
  36. Xthe sed-to-perl translator easier.
  37. XUse at your own risk.
  38. X.Ip "hex(EXPR)" 8 2
  39. XReturns the decimal value of EXPR interpreted as an hex string.
  40. X(To interpret strings that might start with 0 or 0x see oct().)
  41. X.Ip "index(STR,SUBSTR)" 8 4
  42. XReturns the position of SUBSTR in STR, based at 0, or whatever you've
  43. Xset the $[ variable to.
  44. XIf the substring is not found, returns one less than the base, ordinarily -1.
  45. X.Ip "int(EXPR)" 8 3
  46. XReturns the integer portion of EXPR.
  47. X.Ip "join(EXPR,LIST)" 8 8
  48. X.Ip "join(EXPR,ARRAY)" 8
  49. XJoins the separate strings of LIST or ARRAY into a single string with fields
  50. Xseparated by the value of EXPR, and returns the string.
  51. XExample:
  52. X.nf
  53. X    
  54. X    $_ = join(\|':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
  55. X
  56. X.fi
  57. XSee
  58. X.IR split .
  59. X.Ip "keys(ASSOC_ARRAY)" 8 6
  60. XReturns a normal array consisting of all the keys of the named associative
  61. Xarray.
  62. XThe keys are returned in an apparently random order, but it is the same order
  63. Xas either the values() or each() function produces (given that the associative array
  64. Xhas not been modified).
  65. XHere is yet another way to print your environment:
  66. X.nf
  67. X
  68. X.ne 5
  69. X    @keys = keys(ENV);
  70. X    @values = values(ENV);
  71. X    while ($#keys >= 0) {
  72. X        print pop(keys),'=',pop(values),"\n";
  73. X    }
  74. X
  75. X.fi
  76. X.Ip "kill LIST" 8 2
  77. XSends a signal to a list of processes.
  78. XThe first element of the list must be the (numerical) signal to send.
  79. XLIST may be an array, in which case you may wish to use the unshift
  80. Xcommand to put the signal on the front of the array.
  81. XReturns the number of processes successfully signaled.
  82. XNote: in order to use the value you must put the whole thing in parentheses:
  83. X.nf
  84. X
  85. X    $cnt = (kill 9,$child1,$child2);
  86. X
  87. X.fi
  88. X.Ip "last LABEL" 8 8
  89. X.Ip "last" 8
  90. XThe
  91. X.I last
  92. Xcommand is like the
  93. X.I break
  94. Xstatement in C (as used in loops); it immediately exits the loop in question.
  95. XIf the LABEL is omitted, the command refers to the innermost enclosing loop.
  96. XThe
  97. X.I continue
  98. Xblock, if any, is not executed:
  99. X.nf
  100. X
  101. X.ne 4
  102. X    line: while (<stdin>) {
  103. X        last line if /\|^$/;    # exit when done with header
  104. X        .\|.\|.
  105. X    }
  106. X
  107. X.fi
  108. X.Ip "localtime(EXPR)" 8 4
  109. XConverts a time as returned by the time function to a 9-element array with
  110. Xthe time analyzed for the local timezone.
  111. XTypically used as follows:
  112. X.nf
  113. X
  114. X.ne 3
  115. X    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
  116. X       = localtime(time);
  117. X
  118. X.fi
  119. XAll array elements are numeric.
  120. X.Ip "log(EXPR)" 8 3
  121. XReturns logarithm (base e) of EXPR.
  122. X.Ip "next LABEL" 8 8
  123. X.Ip "next" 8
  124. XThe
  125. X.I next
  126. Xcommand is like the
  127. X.I continue
  128. Xstatement in C; it starts the next iteration of the loop:
  129. X.nf
  130. X
  131. X.ne 4
  132. X    line: while (<stdin>) {
  133. X        next line if /\|^#/;    # discard comments
  134. X        .\|.\|.
  135. X    }
  136. X
  137. X.fi
  138. XNote that if there were a
  139. X.I continue
  140. Xblock on the above, it would get executed even on discarded lines.
  141. XIf the LABEL is omitted, the command refers to the innermost enclosing loop.
  142. X.Ip "length(EXPR)" 8 2
  143. XReturns the length in characters of the value of EXPR.
  144. X.Ip "link(OLDFILE,NEWFILE)" 8 2
  145. XCreates a new filename linked to the old filename.
  146. XReturns 1 for success, 0 otherwise.
  147. X.Ip "oct(EXPR)" 8 2
  148. XReturns the decimal value of EXPR interpreted as an octal string.
  149. X(If EXPR happens to start off with 0x, interprets it as a hex string instead.)
  150. XThe following will handle decimal, octal and hex in the standard notation:
  151. X.nf
  152. X
  153. X    $val = oct($val) if $val =~ /^0/;
  154. X
  155. X.fi
  156. X.Ip "open(FILEHANDLE,EXPR)" 8 8
  157. X.Ip "open(FILEHANDLE)" 8
  158. X.Ip "open FILEHANDLE" 8
  159. XOpens the file whose filename is given by EXPR, and associates it with
  160. XFILEHANDLE.
  161. XIf EXPR is omitted, the string variable of the same name as the FILEHANDLE
  162. Xcontains the filename.
  163. XIf the filename begins with \*(L">\*(R", the file is opened for output.
  164. XIf the filename begins with \*(L">>\*(R", the file is opened for appending.
  165. XIf the filename begins with \*(L"|\*(R", the filename is interpreted
  166. Xas a command to which output is to be piped, and if the filename ends
  167. Xwith a \*(L"|\*(R", the filename is interpreted as command which pipes
  168. Xinput to us.
  169. X(You may not have a command that pipes both in and out.)
  170. XOn non-pipe opens, the filename '\-' represents either stdin or stdout, as
  171. Xappropriate.
  172. XOpen returns 1 upon success, '' otherwise.
  173. XExamples:
  174. X.nf
  175. X    
  176. X.ne 3
  177. X    $article = 100;
  178. X    open article || die "Can't find article $article";
  179. X    while (<article>) {\|.\|.\|.
  180. X
  181. X    open(log, '>>/usr/spool/news/twitlog'\|);
  182. X
  183. X    open(article, "caeser <$article |"\|);        # decrypt article
  184. X
  185. X    open(extract, "|sort >/tmp/Tmp$$"\|);        # $$ is our process#
  186. X
  187. X.fi
  188. X.Ip "ord(EXPR)" 8 3
  189. XReturns the ascii value of the first character of EXPR.
  190. X.Ip "pop ARRAY" 8 6
  191. X.Ip "pop(ARRAY)" 8
  192. XPops and returns the last value of the array, shortening the array by 1.
  193. X''' $tmp = $ARRAY[$#ARRAY--]
  194. X.Ip "print FILEHANDLE LIST" 8 9
  195. X.Ip "print LIST" 8
  196. X.Ip "print" 8
  197. XPrints a string or comma-separated list of strings.
  198. XIf FILEHANDLE is omitted, prints by default to standard output (or to the
  199. Xlast selected output channel\*(--see select()).
  200. XIf LIST is also omitted, prints $_ to stdout.
  201. XLIST may also be an array value.
  202. XTo set the default output channel to something other than stdout use the select operation.
  203. X.Ip "printf FILEHANDLE LIST" 8 9
  204. X.Ip "printf LIST" 8
  205. XEquivalent to a "print FILEHANDLE sprintf(LIST)".
  206. X.Ip "push(ARRAY,EXPR)" 8 7
  207. XTreats ARRAY (@ is optional) as a stack, and pushes the value of EXPR
  208. Xonto the end of ARRAY.
  209. XThe length of ARRAY increases by 1.
  210. XHas the same effect as
  211. X.nf
  212. X
  213. X    $ARRAY[$#ARRAY+1] = EXPR;
  214. X
  215. X.fi
  216. Xbut is more efficient.
  217. X.Ip "redo LABEL" 8 8
  218. X.Ip "redo" 8
  219. XThe
  220. X.I redo
  221. Xcommand restarts the loop block without evaluating the conditional again.
  222. XThe
  223. X.I continue
  224. Xblock, if any, is not executed.
  225. XIf the LABEL is omitted, the command refers to the innermost enclosing loop.
  226. XThis command is normally used by programs that want to lie to themselves
  227. Xabout what was just input:
  228. X.nf
  229. X
  230. X.ne 16
  231. X    # a simpleminded Pascal comment stripper
  232. X    # (warning: assumes no { or } in strings)
  233. X    line: while (<stdin>) {
  234. X        while (s|\|({.*}.*\|){.*}|$1 \||) {}
  235. X        s|{.*}| \||;
  236. X        if (s|{.*| \||) {
  237. X            $front = $_;
  238. X            while (<stdin>) {
  239. X                if (\|/\|}/\|) {    # end of comment?
  240. X                    s|^|$front{|;
  241. X                    redo line;
  242. X                }
  243. X            }
  244. X        }
  245. X        print;
  246. X    }
  247. X
  248. X.fi
  249. X.Ip "rename(OLDNAME,NEWNAME)" 8 2
  250. XChanges the name of a file.
  251. XReturns 1 for success, 0 otherwise.
  252. X.Ip "reset EXPR" 8 3
  253. XGenerally used in a
  254. X.I continue
  255. Xblock at the end of a loop to clear variables and reset ?? searches
  256. Xso that they work again.
  257. XThe expression is interpreted as a list of single characters (hyphens allowed
  258. Xfor ranges).
  259. XAll string variables beginning with one of those letters are set to the null
  260. Xstring.
  261. XIf the expression is omitted, one-match searches (?pattern?) are reset to
  262. Xmatch again.
  263. XAlways returns 1.
  264. XExamples:
  265. X.nf
  266. X
  267. X.ne 3
  268. X    reset 'X';    \h'|2i'# reset all X variables
  269. X    reset 'a-z';\h'|2i'# reset lower case variables
  270. X    reset;    \h'|2i'# just reset ?? searches
  271. X
  272. X.fi
  273. X.Ip "s/PATTERN/REPLACEMENT/g" 8 3
  274. XSearches a string for a pattern, and if found, replaces that pattern with the
  275. Xreplacement text and returns the number of substitutions made.
  276. XOtherwise it returns false (0).
  277. XThe \*(L"g\*(R" is optional, and if present, indicates that all occurences
  278. Xof the pattern are to be replaced.
  279. XAny delimiter may replace the slashes; if single quotes are used, no
  280. Xinterpretation is done on the replacement string.
  281. XIf no string is specified via the =~ or !~ operator,
  282. Xthe $_ string is searched and modified.
  283. X(The string specified with =~ must be a string variable or array element,
  284. Xi.e. an lvalue.)
  285. XIf the pattern contains a $ that looks like a variable rather than an
  286. Xend-of-string test, the variable will be interpolated into the pattern at
  287. Xrun-time.
  288. XSee also the section on regular expressions.
  289. XExamples:
  290. X.nf
  291. X
  292. X    s/\|\e\|bgreen\e\|b/mauve/g;        # don't change wintergreen
  293. X
  294. X    $path \|=~ \|s|\|/usr/bin|\|/usr/local/bin|;
  295. X
  296. X    s/Login: $foo/Login: $bar/; # run-time pattern
  297. X
  298. X    s/\|([^ \|]*\|) *\|([^ \|]*\|)\|/\|$2 $1/;    # reverse 1st two fields
  299. X
  300. X.fi
  301. X(Note the use of $ instead of \|\e\| in the last example.  See section
  302. Xon regular expressions.)
  303. X.Ip "seek(FILEHANDLE,POSITION,WHENCE)" 8 3
  304. XRandomly positions the file pointer for FILEHANDLE, just like the fseek()
  305. Xcall of stdio.
  306. XReturns 1 upon success, 0 otherwise.
  307. X.Ip "select(FILEHANDLE)" 8 3
  308. XSets the current default filehandle for output.
  309. XThis has two effects: first, a
  310. X.I write
  311. Xor a
  312. X.I print
  313. Xwithout a filehandle will default to this FILEHANDLE.
  314. XSecond, references to variables related to output will refer to this output
  315. Xchannel.
  316. XFor example, if you have to set the top of form format for more than
  317. Xone output channel, you might do the following:
  318. X.nf
  319. X
  320. X.ne 4
  321. X    select(report1);
  322. X    $^ = 'report1_top';
  323. X    select(report2);
  324. X    $^ = 'report2_top';
  325. X
  326. X.fi
  327. XSelect happens to return TRUE if the file is currently open and FALSE otherwise,
  328. Xbut this has no effect on its operation.
  329. X.Ip "shift(ARRAY)" 8 6
  330. X.Ip "shift ARRAY" 8
  331. X.Ip "shift" 8
  332. XShifts the first value of the array off, shortening the array by 1 and
  333. Xmoving everything down.
  334. XIf ARRAY is omitted, shifts the ARGV array.
  335. XSee also unshift().
  336. X.Ip "sleep EXPR" 8 6
  337. X.Ip "sleep" 8
  338. XCauses the script to sleep for EXPR seconds, or forever if no EXPR.
  339. XMay be interrupted by sending the process a SIGALARM.
  340. XReturns the number of seconds actually slept.
  341. X.Ip "split(/PATTERN/,EXPR)" 8 8
  342. X.Ip "split(/PATTERN/)" 8
  343. X.Ip "split" 8
  344. XSplits a string into an array of strings, and returns it.
  345. XIf EXPR is omitted, splits the $_ string.
  346. XIf PATTERN is also omitted, splits on whitespace (/[\ \et\en]+/).
  347. XAnything matching PATTERN is taken to be a delimiter separating the fields.
  348. X(Note that the delimiter may be longer than one character.)
  349. XTrailing null fields are stripped, which potential users of pop() would
  350. Xdo well to remember.
  351. XA pattern matching the null string will split into separate characters.
  352. X.sp
  353. XExample:
  354. X.nf
  355. X
  356. X.ne 5
  357. X    open(passwd, '/etc/passwd');
  358. X    while (<passwd>) {
  359. X.ie t \{\
  360. X        ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(\|/\|:\|/\|);
  361. X'br\}
  362. X.el \{\
  363. X        ($login, $passwd, $uid, $gid, $gcos, $home, $shell)
  364. X            = split(\|/\|:\|/\|);
  365. X'br\}
  366. X        .\|.\|.
  367. X    }
  368. X
  369. X.fi
  370. X(Note that $shell above will still have a newline on it.  See chop().)
  371. XSee also
  372. X.IR join .
  373. X.Ip "sprintf(FORMAT,LIST)" 8 4
  374. XReturns a string formatted by the usual printf conventions.
  375. XThe * character is not supported.
  376. X.Ip "sqrt(EXPR)" 8 3
  377. XReturn the square root of EXPR.
  378. X.Ip "stat(FILEHANDLE)" 8 6
  379. X.Ip "stat(EXPR)" 8
  380. XReturns a 13-element array giving the statistics for a file, either the file
  381. Xopened via FILEHANDLE, or named by EXPR.
  382. XTypically used as follows:
  383. X.nf
  384. X
  385. X.ne 3
  386. X    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  387. X       $atime,$mtime,$ctime,$blksize,$blocks)
  388. X           = stat($filename);
  389. X
  390. X.fi
  391. X.Ip "substr(EXPR,OFFSET,LEN)" 8 2
  392. XExtracts a substring out of EXPR and returns it.
  393. XFirst character is at offset 0, or whatever you've set $[ to.
  394. X.Ip "system LIST" 8 6
  395. XDoes exactly the same thing as \*(L"exec LIST\*(R" except that a fork
  396. Xis done first, and the parent process waits for the child process to complete.
  397. XNote that argument processing varies depending on the number of arguments.
  398. XSee exec.
  399. X.Ip "tell(FILEHANDLE)" 8 6
  400. X.Ip "tell" 8
  401. XReturns the current file position for FILEHANDLE.
  402. XIf FILEHANDLE is omitted, assumes the file last read.
  403. X.Ip "time" 8 4
  404. XReturns the number of seconds since January 1, 1970.
  405. XSuitable for feeding to gmtime() and localtime().
  406. X.Ip "times" 8 4
  407. XReturns a four-element array giving the user and system times, in seconds, for this
  408. Xprocess and the children of this process.
  409. X.sp
  410. X    ($user,$system,$cuser,$csystem) = times;
  411. X.sp
  412. X.Ip "tr/SEARCHLIST/REPLACEMENTLIST/" 8 5
  413. X.Ip "y/SEARCHLIST/REPLACEMENTLIST/" 8
  414. XTranslates all occurences of the characters found in the search list with
  415. Xthe corresponding character in the replacement list.
  416. XIt returns the number of characters replaced.
  417. XIf no string is specified via the =~ or !~ operator,
  418. Xthe $_ string is translated.
  419. X(The string specified with =~ must be a string variable or array element,
  420. Xi.e. an lvalue.)
  421. XFor
  422. X.I sed
  423. Xdevotees,
  424. X.I y
  425. Xis provided as a synonym for
  426. X.IR tr .
  427. XExamples:
  428. X.nf
  429. X
  430. X    $ARGV[1] \|=~ \|y/A-Z/a-z/;    \h'|3i'# canonicalize to lower case
  431. X
  432. X    $cnt = tr/*/*/;        \h'|3i'# count the stars in $_
  433. X
  434. X.fi
  435. X.Ip "umask(EXPR)" 8 3
  436. XSets the umask for the process and returns the old one.
  437. X.Ip "unlink LIST" 8 2
  438. XDeletes a list of files.
  439. XLIST may be an array.
  440. XReturns the number of files successfully deleted.
  441. XNote: in order to use the value you must put the whole thing in parentheses:
  442. X.nf
  443. X
  444. X    $cnt = (unlink 'a','b','c');
  445. X
  446. X.fi
  447. X.Ip "unshift(ARRAY,LIST)" 8 4
  448. XDoes the opposite of a shift.
  449. XPrepends list to the front of the array, and returns the number of elements
  450. Xin the new array.
  451. X.nf
  452. X
  453. X    unshift(ARGV,'-e') unless $ARGV[0] =~ /^-/;
  454. X
  455. X.fi
  456. X.Ip "values(ASSOC_ARRAY)" 8 6
  457. XReturns a normal array consisting of all the values of the named associative
  458. Xarray.
  459. XThe values are returned in an apparently random order, but it is the same order
  460. Xas either the keys() or each() function produces (given that the associative array
  461. Xhas not been modified).
  462. XSee also keys() and each().
  463. X.Ip "write(FILEHANDLE)" 8 6
  464. X.Ip "write(EXPR)" 8
  465. X.Ip "write(\|)" 8
  466. XWrites a formatted record (possibly multi-line) to the specified file,
  467. Xusing the format associated with that file.
  468. XBy default the format for a file is the one having the same name is the
  469. Xfilehandle, but the format for the current output channel (see
  470. X.IR select )
  471. Xmay be set explicitly
  472. Xby assigning the name of the format to the $~ variable.
  473. X.sp
  474. XTop of form processing is handled automatically:
  475. Xif there is insufficient room on the current page for the formatted 
  476. Xrecord, the page is advanced, a special top-of-page format is used
  477. Xto format the new page header, and then the record is written.
  478. XBy default the top-of-page format is \*(L"top\*(R", but it
  479. Xmay be set to the
  480. Xformat of your choice by assigning the name to the $^ variable.
  481. X.sp
  482. XIf FILEHANDLE is unspecified, output goes to the current default output channel,
  483. Xwhich starts out as stdout but may be changed by the
  484. X.I select
  485. Xoperator.
  486. XIf the FILEHANDLE is an EXPR, then the expression is evaluated and the
  487. Xresulting string is used to look up the name of the FILEHANDLE at run time.
  488. XFor more on formats, see the section on formats later on.
  489. X.Sh "Subroutines"
  490. XA subroutine may be declared as follows:
  491. X.nf
  492. X
  493. X    sub NAME BLOCK
  494. X
  495. X.fi
  496. X.PP
  497. XAny arguments passed to the routine come in as array @_,
  498. Xthat is ($_[0], $_[1], .\|.\|.).
  499. XThe return value of the subroutine is the value of the last expression
  500. Xevaluated.
  501. XThere are no local variables\*(--everything is a global variable.
  502. X.PP
  503. XA subroutine is called using the
  504. X.I do
  505. Xoperator.
  506. X(CAVEAT: For efficiency reasons recursive subroutine calls are not currently
  507. Xsupported.
  508. XThis restriction may go away in the future.  Then again, it may not.)
  509. X.nf
  510. X
  511. X.ne 12
  512. XExample:
  513. X
  514. X    sub MAX {
  515. X        $max = pop(@_);
  516. X        while ($foo = pop(@_)) {
  517. X            $max = $foo \|if \|$max < $foo;
  518. X        }
  519. X        $max;
  520. X    }
  521. X
  522. X    .\|.\|.
  523. X    $bestday = do MAX($mon,$tue,$wed,$thu,$fri);
  524. X
  525. X.ne 21
  526. XExample:
  527. X
  528. X    # get a line, combining continuation lines
  529. X    #  that start with whitespace
  530. X    sub get_line {
  531. X        $thisline = $lookahead;
  532. X        line: while ($lookahead = <stdin>) {
  533. X            if ($lookahead \|=~ \|/\|^[ \^\e\|t]\|/\|) {
  534. X                $thisline \|.= \|$lookahead;
  535. X            }
  536. X            else {
  537. X                last line;
  538. X            }
  539. X        }
  540. X        $thisline;
  541. X    }
  542. X
  543. X    $lookahead = <stdin>;    # get first line
  544. X    while ($_ = get_line(\|)) {
  545. X        .\|.\|.
  546. X    }
  547. X
  548. X.fi
  549. X.nf
  550. X.ne 6
  551. XUse array assignment to name your formal arguments:
  552. X
  553. X    sub maybeset {
  554. X        ($key,$value) = @_;
  555. X        $foo{$key} = $value unless $foo{$key};
  556. X    }
  557. X
  558. X.fi
  559. X.Sh "Regular Expressions"
  560. XThe patterns used in pattern matching are regular expressions such as
  561. Xthose used by
  562. X.IR egrep (1).
  563. XIn addition, \ew matches an alphanumeric character and \eW a nonalphanumeric.
  564. XWord boundaries may be matched by \eb, and non-boundaries by \eB.
  565. XThe bracketing construct \|(\ .\|.\|.\ \|) may also be used, $<digit>
  566. Xmatches the digit'th substring, where digit can range from 1 to 9.
  567. X(You can also use the old standby \e<digit> in search patterns,
  568. Xbut $<digit> also works in replacement patterns and in the block controlled
  569. Xby the current conditional.)
  570. X$+ returns whatever the last bracket match matched.
  571. X$& returns the entire matched string.
  572. XUp to 10 alternatives may given in a pattern, separated by |, with the
  573. Xcaveat that \|(\ .\|.\|.\ |\ .\|.\|.\ \|) is illegal.
  574. XExamples:
  575. X.nf
  576. X    
  577. X    s/\|^\|([^ \|]*\|) \|*([^ \|]*\|)\|/\|$2 $1\|/;    # swap first two words
  578. X
  579. X.ne 5
  580. X    if (/\|Time: \|(.\|.\|):\|(.\|.\|):\|(.\|.\|)\|/\|) {
  581. X        $hours = $1;
  582. X        $minutes = $2;
  583. X        $seconds = $3;
  584. X    }
  585. X
  586. X.fi
  587. XBy default, the ^ character matches only the beginning of the string, and
  588. X.I perl
  589. Xdoes certain optimizations with the assumption that the string contains
  590. Xonly one line.
  591. XYou may, however, wish to treat a string as a multi-line buffer, such that
  592. Xthe ^ will match after any newline within the string.
  593. XAt the cost of a little more overhead, you can do this by setting the variable
  594. X$* to 1.
  595. XSetting it back to 0 makes
  596. X.I perl
  597. Xrevert to its old behavior.
  598. X.Sh "Formats"
  599. XOutput record formats for use with the
  600. X.I write
  601. Xoperator may declared as follows:
  602. X.nf
  603. X
  604. X.ne 3
  605. X    format NAME =
  606. X    FORMLIST
  607. X    .
  608. X
  609. X.fi
  610. XIf name is omitted, format \*(L"stdout\*(R" is defined.
  611. XFORMLIST consists of a sequence of lines, each of which may be of one of three
  612. Xtypes:
  613. X.Ip 1. 4
  614. XA comment.
  615. X.Ip 2. 4
  616. XA \*(L"picture\*(R" line giving the format for one output line.
  617. X.Ip 3. 4
  618. XAn argument line supplying values to plug into a picture line.
  619. X.PP
  620. XPicture lines are printed exactly as they look, except for certain fields
  621. Xthat substitute values into the line.
  622. XEach picture field starts with either @ or ^.
  623. XThe @ field (not to be confused with the array marker @) is the normal
  624. Xcase; ^ fields are used
  625. Xto do rudimentary multi-line text block filling.
  626. XThe length of the field is supplied by padding out the field
  627. Xwith multiple <, >, or | characters to specify, respectively, left justfication,
  628. Xright justification, or centering.
  629. XIf any of the values supplied for these fields contains a newline, only
  630. Xthe text up to the newline is printed.
  631. XThe special field @* can be used for printing multi-line values.
  632. XIt should appear by itself on a line.
  633. X.PP
  634. XThe values are specified on the following line, in the same order as
  635. Xthe picture fields.
  636. XThey must currently be either string variable names or string literals (or
  637. Xpseudo-literals).
  638. XCurrently you can separate values with spaces, but commas may be placed
  639. Xbetween values to prepare for possible future versions in which full expressions
  640. Xare allowed as values.
  641. X.PP
  642. XPicture fields that begin with ^ rather than @ are treated specially.
  643. XThe value supplied must be a string variable name which contains a text
  644. Xstring.
  645. X.I Perl
  646. Xputs as much text as it can into the field, and then chops off the front
  647. Xof the string so that the next time the string variable is referenced,
  648. Xmore of the text can be printed.
  649. XNormally you would use a sequence of fields in a vertical stack to print
  650. Xout a block of text.
  651. XIf you like, you can end the final field with .\|.\|., which will appear in the
  652. Xoutput if the text was too long to appear in its entirety.
  653. X.PP
  654. XSince use of ^ fields can produce variable length records if the text to be
  655. Xformatted is short, you can suppress blank lines by putting the tilde (~)
  656. Xcharacter anywhere in the line.
  657. X(Normally you should put it in the front if possible.)
  658. XThe tilde will be translated to a space upon output.
  659. X.PP
  660. XExamples:
  661. X.nf
  662. X.lg 0
  663. X.cs R 25
  664. X
  665. X.ne 10
  666. X# a report on the /etc/passwd file
  667. Xformat top =
  668. X\&                        Passwd File
  669. XName                Login    Office   Uid   Gid Home
  670. X------------------------------------------------------------------
  671. X\&.
  672. Xformat stdout =
  673. X@<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
  674. X$name               $login   $office $uid $gid  $home
  675. X\&.
  676. X
  677. X.ne 29
  678. X# a report from a bug report form
  679. Xformat top =
  680. X\&                        Bug Reports
  681. X@<<<<<<<<<<<<<<<<<<<<<<<     @|||         @>>>>>>>>>>>>>>>>>>>>>>>
  682. X$system;                      $%;         $date
  683. X------------------------------------------------------------------
  684. X\&.
  685. Xformat stdout =
  686. XSubject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  687. X\&         $subject
  688. XIndex: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  689. X\&       $index                        $description
  690. XPriority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  691. X\&          $priority         $date    $description
  692. XFrom: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  693. X\&      $from                          $description
  694. XAssigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  695. X\&             $programmer             $description
  696. X\&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  697. X\&                                     $description
  698. X\&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  699. X\&                                     $description
  700. X\&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  701. X\&                                     $description
  702. X\&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  703. X\&                                     $description
  704. X\&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<...
  705. X\&                                     $description
  706. X\&.
  707. X
  708. X.cs R
  709. X.lg
  710. XIt is possible to intermix prints with writes on the same output channel,
  711. Xbut you'll have to handle $\- (lines left on the page) yourself.
  712. X.fi
  713. X.PP
  714. XIf you are printing lots of fields that are usually blank, you should consider
  715. Xusing the reset operator between records.
  716. XNot only is it more efficient, but it can prevent the bug of adding another
  717. Xfield and forgetting to zero it.
  718. X.Sh "Predefined Names"
  719. XThe following names have special meaning to
  720. X.IR perl .
  721. XI could have used alphabetic symbols for some of these, but I didn't want
  722. Xto take the chance that someone would say reset "a-zA-Z" and wipe them all
  723. Xout.
  724. XYou'll just have to suffer along with these silly symbols.
  725. XMost of them have reasonable mnemonics, or analogues in one of the shells.
  726. X.Ip $_ 8
  727. XThe default input and pattern-searching space.
  728. XThe following pairs are equivalent:
  729. X.nf
  730. X
  731. X.ne 2
  732. X    while (<>) {\|.\|.\|.    # only equivalent in while!
  733. X    while ($_ = <>) {\|.\|.\|.
  734. X
  735. X.ne 2
  736. X    /\|^Subject:/
  737. X    $_ \|=~ \|/\|^Subject:/
  738. X
  739. X.ne 2
  740. X    y/a-z/A-Z/
  741. X    $_ =~ y/a-z/A-Z/
  742. X
  743. X.ne 2
  744. X    chop
  745. X    chop($_)
  746. X
  747. X.fi 
  748. X(Mnemonic: underline is understood in certain operations.)
  749. X.Ip $. 8
  750. XThe current input line number of the last file that was read.
  751. XReadonly.
  752. X(Mnemonic: many programs use . to mean the current line number.)
  753. X.Ip $/ 8
  754. XThe input record separator, newline by default.
  755. XWorks like awk's RS variable, including treating blank lines as delimiters
  756. Xif set to the null string.
  757. XIf set to a value longer than one character, only the first character is used.
  758. X(Mnemonic: / is used to delimit line boundaries when quoting poetry.)
  759. X.Ip $, 8
  760. XThe output field separator for the print operator.
  761. XOrdinarily the print operator simply prints out the comma separated fields
  762. Xyou specify.
  763. XIn order to get behavior more like awk, set this variable as you would set
  764. Xawk's OFS variable to specify what is printed between fields.
  765. X(Mnemonic: what is printed when there is a , in your print statement.)
  766. X.Ip $\e 8
  767. XThe output record separator for the print operator.
  768. XOrdinarily the print operator simply prints out the comma separated fields
  769. Xyou specify, with no trailing newline or record separator assumed.
  770. XIn order to get behavior more like awk, set this variable as you would set
  771. Xawk's ORS variable to specify what is printed at the end of the print.
  772. X(Mnemonic: you set $\e instead of adding \en at the end of the print.
  773. XAlso, it's just like /, but it's what you get \*(L"back\*(R" from perl.)
  774. X.Ip $# 8
  775. XThe output format for printed numbers.
  776. XThis variable is a half-hearted attempt to emulate awk's OFMT variable.
  777. XThere are times, however, when awk and perl have differing notions of what
  778. Xis in fact numeric.
  779. XAlso, the initial value is %.20g rather than %.6g, so you need to set $#
  780. Xexplicitly to get awk's value.
  781. X(Mnemonic: # is the number sign.)
  782. X.Ip $% 8
  783. XThe current page number of the currently selected output channel.
  784. X(Mnemonic: % is page number in nroff.)
  785. X.Ip $= 8
  786. XThe current page length (printable lines) of the currently selected output
  787. Xchannel.
  788. XDefault is 60.
  789. X(Mnemonic: = has horizontal lines.)
  790. X.Ip $\- 8
  791. XThe number of lines left on the page of the currently selected output channel.
  792. X(Mnemonic: lines_on_page - lines_printed.)
  793. X.Ip $~ 8
  794. XThe name of the current report format for the currently selected output
  795. Xchannel.
  796. X(Mnemonic: brother to $^.)
  797. X.Ip $^ 8
  798. XThe name of the current top-of-page format for the currently selected output
  799. Xchannel.
  800. X(Mnemonic: points to top of page.)
  801. X.Ip $| 8
  802. XIf set to nonzero, forces a flush after every write or print on the currently
  803. Xselected output channel.
  804. XDefault is 0.
  805. XNote that stdout will typically be line buffered if output is to the
  806. Xterminal and block buffered otherwise.
  807. XSetting this variable is useful primarily when you are outputting to a pipe,
  808. Xsuch as when you are running a perl script under rsh and want to see the
  809. Xoutput as it's happening.
  810. X(Mnemonic: when you want your pipes to be piping hot.)
  811. X.Ip $$ 8
  812. XThe process number of the
  813. X.I perl
  814. Xrunning this script.
  815. X(Mnemonic: same as shells.)
  816. X.Ip $? 8
  817. XThe status returned by the last backtick (``) command.
  818. X(Mnemonic: same as sh and ksh.)
  819. X.Ip $+ 8 4
  820. XThe last bracket matched by the last search pattern.
  821. XThis is useful if you don't know which of a set of alternative patterns
  822. Xmatched.
  823. XFor example:
  824. X.nf
  825. X
  826. X    /Version: \|(.*\|)|Revision: \|(.*\|)\|/ \|&& \|($rev = $+);
  827. X
  828. X.fi
  829. X(Mnemonic: be positive and forward looking.)
  830. X.Ip $* 8 2
  831. XSet to 1 to do multiline matching within a string, 0 to assume strings contain
  832. Xa single line.
  833. XDefault is 0.
  834. X(Mnemonic: * matches multiple things.)
  835. X.Ip $0 8
  836. XContains the name of the file containing the
  837. X.I perl
  838. Xscript being executed.
  839. XThe value should be copied elsewhere before any pattern matching happens, which
  840. Xclobbers $0.
  841. X(Mnemonic: same as sh and ksh.)
  842. X.Ip $[ 8 2
  843. XThe index of the first element in an array, and of the first character in
  844. Xa substring.
  845. XDefault is 0, but you could set it to 1 to make
  846. X.I perl
  847. Xbehave more like
  848. X.I awk
  849. X(or Fortran)
  850. Xwhen subscripting and when evaluating the index() and substr() functions.
  851. X(Mnemonic: [ begins subscripts.)
  852. X.Ip $! 8 2
  853. XThe current value of errno, with all the usual caveats.
  854. X(Mnemonic: What just went bang?)
  855. X.Ip @ARGV 8 3
  856. XThe array ARGV contains the command line arguments intended for the script.
  857. XNote that $#ARGV is the generally number of arguments minus one, since
  858. X$ARGV[0] is the first argument, NOT the command name.
  859. XSee $0 for the command name.
  860. X.Ip $ENV{expr} 8 2
  861. XThe associative array ENV contains your current environment.
  862. XSetting a value in ENV changes the environment for child processes.
  863. X.Ip $SIG{expr} 8 2
  864. XThe associative array SIG is used to set signal handlers for various signals.
  865. XExample:
  866. X.nf
  867. X
  868. X.ne 12
  869. X    sub handler {    # 1st argument is signal name
  870. X        ($sig) = @_;
  871. X        print "Caught a SIG$sig--shutting down\n";
  872. X        close(log);
  873. X        exit(0);
  874. X    }
  875. X
  876. X    $SIG{'INT'} = 'handler';
  877. X    $SIG{'QUIT'} = 'handler';
  878. X    ...
  879. X    $SIG{'INT'} = 'DEFAULT';    # restore default action
  880. X    $SIG{'QUIT'} = 'IGNORE';    # ignore SIGQUIT
  881. X
  882. X.fi
  883. X.SH ENVIRONMENT
  884. X.I Perl
  885. Xcurrently uses no environment variables, except to make them available
  886. Xto the script being executed, and to child processes.
  887. XHowever, scripts running setuid would do well to execute the following lines
  888. Xbefore doing anything else, just to keep people honest:
  889. X.nf
  890. X
  891. X.ne 3
  892. X    $ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
  893. X    $ENV{'SHELL'} = '/bin/sh' if $ENV{'SHELL'};
  894. X    $ENV{'IFS'} = '' if $ENV{'IFS'};
  895. X
  896. X.fi
  897. X.SH AUTHOR
  898. XLarry Wall <lwall@jpl-devvax.Jpl.Nasa.Gov>
  899. X.SH FILES
  900. X/tmp/perl\-eXXXXXX    temporary file for
  901. X.B \-e
  902. Xcommands.
  903. X.SH SEE ALSO
  904. Xa2p    awk to perl translator
  905. X.br
  906. Xs2p    sed to perl translator
  907. X.SH DIAGNOSTICS
  908. XCompilation errors will tell you the line number of the error, with an
  909. Xindication of the next token or token type that was to be examined.
  910. X(In the case of a script passed to
  911. X.I perl
  912. Xvia
  913. X.B \-e
  914. Xswitches, each
  915. X.B \-e
  916. Xis counted as one line.)
  917. X.SH TRAPS
  918. XAccustomed awk users should take special note of the following:
  919. X.Ip * 4 2
  920. XSemicolons are required after all simple statements in perl.  Newline
  921. Xis not a statement delimiter.
  922. X.Ip * 4 2
  923. XCurly brackets are required on ifs and whiles.
  924. X.Ip * 4 2
  925. XVariables begin with $ or @ in perl.
  926. X.Ip * 4 2
  927. XArrays index from 0 unless you set $[.
  928. XLikewise string positions in substr() and index().
  929. X.Ip * 4 2
  930. XYou have to decide whether your array has numeric or string indices.
  931. X.Ip * 4 2
  932. XYou have to decide whether you want to use string or numeric comparisons.
  933. X.Ip * 4 2
  934. XReading an input line does not split it for you.  You get to split it yourself
  935. Xto an array.
  936. XAnd split has different arguments.
  937. X.Ip * 4 2
  938. XThe current input line is normally in $_, not $0.
  939. XIt generally does not have the newline stripped.
  940. X($0 is initially the name of the program executed, then the last matched
  941. Xstring.)
  942. X.Ip * 4 2
  943. XThe current filename is $ARGV, not $FILENAME.
  944. XNR, RS, ORS, OFS, and OFMT have equivalents with other symbols.
  945. XFS doesn't have an equivalent, since you have to be explicit about
  946. Xsplit statements.
  947. X.Ip * 4 2
  948. X$<digit> does not refer to fields--it refers to substrings matched by the last
  949. Xmatch pattern.
  950. X.Ip * 4 2
  951. XThe print statement does not add field and record separators unless you set
  952. X$, and $\e.
  953. X.Ip * 4 2
  954. XYou must open your files before you print to them.
  955. X.Ip * 4 2
  956. XThe range operator is \*(L"..\*(R", not comma.
  957. X(The comma operator works as in C.)
  958. X.Ip * 4 2
  959. XThe match operator is \*(L"=~\*(R", not \*(L"~\*(R".
  960. X(\*(L"~\*(R" is the one's complement operator.)
  961. X.Ip * 4 2
  962. XThe concatenation operator is \*(L".\*(R", not the null string.
  963. X(Using the null string would render \*(L"/pat/ /pat/\*(R" unparseable,
  964. Xsince the third slash would be interpreted as a division operator\*(--the
  965. Xtokener is in fact slightly context sensitive for operators like /, ?, and <.
  966. XAnd in fact, . itself can be the beginning of a number.)
  967. X.Ip * 4 2
  968. XThe \ennn construct in patterns must be given as [\ennn] to avoid interpretation
  969. Xas a backreference.
  970. X.Ip * 4 2
  971. XNext, exit, and continue work differently.
  972. X.Ip * 4 2
  973. XWhen in doubt, run the awk construct through a2p and see what it gives you.
  974. X.PP
  975. XCerebral C programmers should take note of the following:
  976. X.Ip * 4 2
  977. XCurly brackets are required on ifs and whiles.
  978. X.Ip * 4 2
  979. XYou should use \*(L"elsif\*(R" rather than \*(L"else if\*(R"
  980. X.Ip * 4 2
  981. XBreak and continue become last and next, respectively.
  982. X.Ip * 4 2
  983. XThere's no switch statement.
  984. X.Ip * 4 2
  985. XVariables begin with $ or @ in perl.
  986. X.Ip * 4 2
  987. XPrintf does not implement *.
  988. X.Ip * 4 2
  989. XComments begin with #, not /*.
  990. X.Ip * 4 2
  991. XYou can't take the address of anything.
  992. X.Ip * 4 2
  993. XSubroutines are not reentrant.
  994. X.Ip * 4 2
  995. XARGV must be capitalized.
  996. X.Ip * 4 2
  997. XThe \*(L"system\*(R" calls link, unlink, rename, etc. return 1 for success, not 0.
  998. X.Ip * 4 2
  999. XSignal handlers deal with signal names, not numbers.
  1000. X.PP
  1001. XSeasoned sed programmers should take note of the following:
  1002. X.Ip * 4 2
  1003. XBackreferences in substitutions use $ rather than \e.
  1004. X.Ip * 4 2
  1005. XThe pattern matching metacharacters (, ), and | do not have backslashes in front.
  1006. X.SH BUGS
  1007. X.PP
  1008. XYou can't currently dereference array elements inside a double-quoted string.
  1009. XYou must assign them to a temporary and interpolate that.
  1010. X.PP
  1011. XAssociative arrays really ought to be first class objects.
  1012. X.PP
  1013. XRecursive subroutines are not currently supported, due to the way temporary
  1014. Xvalues are stored in the syntax tree.
  1015. X.PP
  1016. XArrays ought to be passable to subroutines just as strings are.
  1017. X.PP
  1018. XThe array literal consisting of one element is currently misinterpreted, i.e.
  1019. X.nf
  1020. X
  1021. X    @array = (123);
  1022. X
  1023. X.fi
  1024. Xdoesn't work right.
  1025. X.PP
  1026. X.I Perl
  1027. Xactually stands for Pathologically Eclectic Rubbish Lister, but don't tell
  1028. Xanyone I said that.
  1029. X.rn }` ''
  1030. !STUFFY!FUNK!
  1031. echo Extracting str.c
  1032. sed >str.c <<'!STUFFY!FUNK!' -e 's/X//'
  1033. X/* $Header: str.c,v 1.0 87/12/18 13:06:22 root Exp $
  1034. X *
  1035. X * $Log:    str.c,v $
  1036. X * Revision 1.0  87/12/18  13:06:22  root
  1037. X * Initial revision
  1038. X * 
  1039. X */
  1040. X
  1041. X#include "handy.h"
  1042. X#include "EXTERN.h"
  1043. X#include "search.h"
  1044. X#include "util.h"
  1045. X#include "perl.h"
  1046. X
  1047. Xstr_reset(s)
  1048. Xregister char *s;
  1049. X{
  1050. X    register STAB *stab;
  1051. X    register STR *str;
  1052. X    register int i;
  1053. X    register int max;
  1054. X    register SPAT *spat;
  1055. X
  1056. X    if (!*s) {        /* reset ?? searches */
  1057. X    for (spat = spat_root; spat != Nullspat; spat = spat->spat_next) {
  1058. X        spat->spat_flags &= ~SPAT_USED;
  1059. X    }
  1060. X    return;
  1061. X    }
  1062. X
  1063. X    /* reset variables */
  1064. X
  1065. X    while (*s) {
  1066. X    i = *s;
  1067. X    if (s[1] == '-') {
  1068. X        s += 2;
  1069. X    }
  1070. X    max = *s++;
  1071. X    for ( ; i <= max; i++) {
  1072. X        for (stab = stab_index[i]; stab; stab = stab->stab_next) {
  1073. X        str = stab->stab_val;
  1074. X        str->str_cur = 0;
  1075. X        if (str->str_ptr != Nullch)
  1076. X            str->str_ptr[0] = '\0';
  1077. X        }
  1078. X    }
  1079. X    }
  1080. X}
  1081. X
  1082. Xstr_numset(str,num)
  1083. Xregister STR *str;
  1084. Xdouble num;
  1085. X{
  1086. X    str->str_nval = num;
  1087. X    str->str_pok = 0;        /* invalidate pointer */
  1088. X    str->str_nok = 1;        /* validate number */
  1089. X}
  1090. X
  1091. Xchar *
  1092. Xstr_2ptr(str)
  1093. Xregister STR *str;
  1094. X{
  1095. X    register char *s;
  1096. X
  1097. X    if (!str)
  1098. X    return "";
  1099. X    GROWSTR(&(str->str_ptr), &(str->str_len), 24);
  1100. X    s = str->str_ptr;
  1101. X    if (str->str_nok) {
  1102. X    sprintf(s,"%.20g",str->str_nval);
  1103. X    while (*s) s++;
  1104. X    }
  1105. X    *s = '\0';
  1106. X    str->str_cur = s - str->str_ptr;
  1107. X    str->str_pok = 1;
  1108. X#ifdef DEBUGGING
  1109. X    if (debug & 32)
  1110. X    fprintf(stderr,"0x%lx ptr(%s)\n",str,str->str_ptr);
  1111. X#endif
  1112. X    return str->str_ptr;
  1113. X}
  1114. X
  1115. Xdouble
  1116. Xstr_2num(str)
  1117. Xregister STR *str;
  1118. X{
  1119. X    if (!str)
  1120. X    return 0.0;
  1121. X    if (str->str_len && str->str_pok)
  1122. X    str->str_nval = atof(str->str_ptr);
  1123. X    else
  1124. X    str->str_nval = 0.0;
  1125. X    str->str_nok = 1;
  1126. X#ifdef DEBUGGING
  1127. X    if (debug & 32)
  1128. X    fprintf(stderr,"0x%lx num(%g)\n",str,str->str_nval);
  1129. X#endif
  1130. X    return str->str_nval;
  1131. X}
  1132. X
  1133. Xstr_sset(dstr,sstr)
  1134. XSTR *dstr;
  1135. Xregister STR *sstr;
  1136. X{
  1137. X    if (!sstr)
  1138. X    str_nset(dstr,No,0);
  1139. X    else if (sstr->str_nok)
  1140. X    str_numset(dstr,sstr->str_nval);
  1141. X    else if (sstr->str_pok)
  1142. X    str_nset(dstr,sstr->str_ptr,sstr->str_cur);
  1143. X    else
  1144. X    str_nset(dstr,"",0);
  1145. X}
  1146. X
  1147. Xstr_nset(str,ptr,len)
  1148. Xregister STR *str;
  1149. Xregister char *ptr;
  1150. Xregister int len;
  1151. X{
  1152. X    GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
  1153. X    bcopy(ptr,str->str_ptr,len);
  1154. X    str->str_cur = len;
  1155. X    *(str->str_ptr+str->str_cur) = '\0';
  1156. X    str->str_nok = 0;        /* invalidate number */
  1157. X    str->str_pok = 1;        /* validate pointer */
  1158. X}
  1159. X
  1160. Xstr_set(str,ptr)
  1161. Xregister STR *str;
  1162. Xregister char *ptr;
  1163. X{
  1164. X    register int len;
  1165. X
  1166. X    if (!ptr)
  1167. X    ptr = "";
  1168. X    len = strlen(ptr);
  1169. X    GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
  1170. X    bcopy(ptr,str->str_ptr,len+1);
  1171. X    str->str_cur = len;
  1172. X    str->str_nok = 0;        /* invalidate number */
  1173. X    str->str_pok = 1;        /* validate pointer */
  1174. X}
  1175. X
  1176. Xstr_chop(str,ptr)    /* like set but assuming ptr is in str */
  1177. Xregister STR *str;
  1178. Xregister char *ptr;
  1179. X{
  1180. X    if (!(str->str_pok))
  1181. X    str_2ptr(str);
  1182. X    str->str_cur -= (ptr - str->str_ptr);
  1183. X    bcopy(ptr,str->str_ptr, str->str_cur + 1);
  1184. X    str->str_nok = 0;        /* invalidate number */
  1185. X    str->str_pok = 1;        /* validate pointer */
  1186. X}
  1187. X
  1188. Xstr_ncat(str,ptr,len)
  1189. Xregister STR *str;
  1190. Xregister char *ptr;
  1191. Xregister int len;
  1192. X{
  1193. X    if (!(str->str_pok))
  1194. X    str_2ptr(str);
  1195. X    GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
  1196. X    bcopy(ptr,str->str_ptr+str->str_cur,len);
  1197. X    str->str_cur += len;
  1198. X    *(str->str_ptr+str->str_cur) = '\0';
  1199. X    str->str_nok = 0;        /* invalidate number */
  1200. X    str->str_pok = 1;        /* validate pointer */
  1201. X}
  1202. X
  1203. Xstr_scat(dstr,sstr)
  1204. XSTR *dstr;
  1205. Xregister STR *sstr;
  1206. X{
  1207. X    if (!(sstr->str_pok))
  1208. X    str_2ptr(sstr);
  1209. X    if (sstr)
  1210. X    str_ncat(dstr,sstr->str_ptr,sstr->str_cur);
  1211. X}
  1212. X
  1213. Xstr_cat(str,ptr)
  1214. Xregister STR *str;
  1215. Xregister char *ptr;
  1216. X{
  1217. X    register int len;
  1218. X
  1219. X    if (!ptr)
  1220. X    return;
  1221. X    if (!(str->str_pok))
  1222. X    str_2ptr(str);
  1223. X    len = strlen(ptr);
  1224. X    GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
  1225. X    bcopy(ptr,str->str_ptr+str->str_cur,len+1);
  1226. X    str->str_cur += len;
  1227. X    str->str_nok = 0;        /* invalidate number */
  1228. X    str->str_pok = 1;        /* validate pointer */
  1229. X}
  1230. X
  1231. Xchar *
  1232. Xstr_append_till(str,from,delim,keeplist)
  1233. Xregister STR *str;
  1234. Xregister char *from;
  1235. Xregister int delim;
  1236. Xchar *keeplist;
  1237. X{
  1238. X    register char *to;
  1239. X    register int len;
  1240. X
  1241. X    if (!from)
  1242. X    return Nullch;
  1243. X    len = strlen(from);
  1244. X    GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
  1245. X    str->str_nok = 0;        /* invalidate number */
  1246. X    str->str_pok = 1;        /* validate pointer */
  1247. X    to = str->str_ptr+str->str_cur;
  1248. X    for (; *from; from++,to++) {
  1249. X    if (*from == '\\' && from[1] && delim != '\\') {
  1250. X        if (!keeplist) {
  1251. X        if (from[1] == delim || from[1] == '\\')
  1252. X            from++;
  1253. X        else
  1254. X            *to++ = *from++;
  1255. X        }
  1256. X        else if (index(keeplist,from[1]))
  1257. X        *to++ = *from++;
  1258. X        else
  1259. X        from++;
  1260. X    }
  1261. X    else if (*from == delim)
  1262. X        break;
  1263. X    *to = *from;
  1264. X    }
  1265. X    *to = '\0';
  1266. X    str->str_cur = to - str->str_ptr;
  1267. X    return from;
  1268. X}
  1269. X
  1270. XSTR *
  1271. Xstr_new(len)
  1272. Xint len;
  1273. X{
  1274. X    register STR *str;
  1275. X    
  1276. X    if (freestrroot) {
  1277. X    str = freestrroot;
  1278. X    freestrroot = str->str_link.str_next;
  1279. X    str->str_link.str_magic = Nullstab;
  1280. X    }
  1281. X    else {
  1282. X    str = (STR *) safemalloc(sizeof(STR));
  1283. X    bzero((char*)str,sizeof(STR));
  1284. X    }
  1285. X    if (len)
  1286. X    GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
  1287. X    return str;
  1288. X}
  1289. X
  1290. Xvoid
  1291. Xstr_grow(str,len)
  1292. Xregister STR *str;
  1293. Xint len;
  1294. X{
  1295. X    if (len && str)
  1296. X    GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
  1297. X}
  1298. X
  1299. X/* make str point to what nstr did */
  1300. X
  1301. Xvoid
  1302. Xstr_replace(str,nstr)
  1303. Xregister STR *str;
  1304. Xregister STR *nstr;
  1305. X{
  1306. X    safefree(str->str_ptr);
  1307. X    str->str_ptr = nstr->str_ptr;
  1308. X    str->str_len = nstr->str_len;
  1309. X    str->str_cur = nstr->str_cur;
  1310. X    str->str_pok = nstr->str_pok;
  1311. X    if (str->str_nok = nstr->str_nok)
  1312. X    str->str_nval = nstr->str_nval;
  1313. X    safefree((char*)nstr);
  1314. X}
  1315. X
  1316. Xvoid
  1317. Xstr_free(str)
  1318. Xregister STR *str;
  1319. X{
  1320. X    if (!str)
  1321. X    return;
  1322. X    if (str->str_len)
  1323. X    str->str_ptr[0] = '\0';
  1324. X    str->str_cur = 0;
  1325. X    str->str_nok = 0;
  1326. X    str->str_pok = 0;
  1327. X    str->str_link.str_next = freestrroot;
  1328. X    freestrroot = str;
  1329. X}
  1330. X
  1331. Xstr_len(str)
  1332. Xregister STR *str;
  1333. X{
  1334. X    if (!str)
  1335. X    return 0;
  1336. X    if (!(str->str_pok))
  1337. X    str_2ptr(str);
  1338. X    if (str->str_len)
  1339. X    return str->str_cur;
  1340. X    else
  1341. X    return 0;
  1342. X}
  1343. X
  1344. Xchar *
  1345. Xstr_gets(str,fp)
  1346. Xregister STR *str;
  1347. Xregister FILE *fp;
  1348. X{
  1349. X#ifdef STDSTDIO        /* Here is some breathtakingly efficient cheating */
  1350. X
  1351. X    register char *bp;        /* we're going to steal some values */
  1352. X    register int cnt;        /*  from the stdio struct and put EVERYTHING */
  1353. X    register char *ptr;        /*   in the innermost loop into registers */
  1354. X    register char newline = record_separator;    /* (assuming >= 6 registers) */
  1355. X    int i;
  1356. X    int bpx;
  1357. X    int obpx;
  1358. X    register int get_paragraph;
  1359. X    register char *oldbp;
  1360. X
  1361. X    if (get_paragraph = !newline) {    /* yes, that's an assignment */
  1362. X    newline = '\n';
  1363. X    oldbp = Nullch;            /* remember last \n position (none) */
  1364. X    }
  1365. X    cnt = fp->_cnt;            /* get count into register */
  1366. X    str->str_nok = 0;            /* invalidate number */
  1367. X    str->str_pok = 1;            /* validate pointer */
  1368. X    if (str->str_len <= cnt)        /* make sure we have the room */
  1369. X    GROWSTR(&(str->str_ptr), &(str->str_len), cnt+1);
  1370. X    bp = str->str_ptr;            /* move these two too to registers */
  1371. X    ptr = fp->_ptr;
  1372. X    for (;;) {
  1373. X      screamer:
  1374. X    while (--cnt >= 0) {            /* this */    /* eat */
  1375. X        if ((*bp++ = *ptr++) == newline)    /* really */    /* dust */
  1376. X        goto thats_all_folks;        /* screams */    /* sed :-) */ 
  1377. X    }
  1378. X    
  1379. X    fp->_cnt = cnt;            /* deregisterize cnt and ptr */
  1380. X    fp->_ptr = ptr;
  1381. X    i = _filbuf(fp);        /* get more characters */
  1382. X    cnt = fp->_cnt;
  1383. X    ptr = fp->_ptr;            /* reregisterize cnt and ptr */
  1384. X
  1385. X    bpx = bp - str->str_ptr;    /* prepare for possible relocation */
  1386. X    if (get_paragraph && oldbp)
  1387. X        obpx = oldbp - str->str_ptr;
  1388. X    GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + cnt + 1);
  1389. X    bp = str->str_ptr + bpx;    /* reconstitute our pointer */
  1390. X    if (get_paragraph && oldbp)
  1391. X        oldbp = str->str_ptr + obpx;
  1392. X
  1393. X    if (i == newline) {        /* all done for now? */
  1394. X        *bp++ = i;
  1395. X        goto thats_all_folks;
  1396. X    }
  1397. X    else if (i == EOF)        /* all done for ever? */
  1398. X        goto thats_really_all_folks;
  1399. X    *bp++ = i;            /* now go back to screaming loop */
  1400. X    }
  1401. X
  1402. Xthats_all_folks:
  1403. X    if (get_paragraph && bp - 1 != oldbp) {
  1404. X    oldbp = bp;    /* remember where this newline was */
  1405. X    goto screamer;    /* and go back to the fray */
  1406. X    }
  1407. Xthats_really_all_folks:
  1408. X    fp->_cnt = cnt;            /* put these back or we're in trouble */
  1409. X    fp->_ptr = ptr;
  1410. X    *bp = '\0';
  1411. X    str->str_cur = bp - str->str_ptr;    /* set length */
  1412. X
  1413. X#else /* !STDSTDIO */    /* The big, slow, and stupid way */
  1414. X
  1415. X    static char buf[4192];
  1416. X
  1417. X    if (fgets(buf, sizeof buf, fp) != Nullch)
  1418. X    str_set(str, buf);
  1419. X    else
  1420. X    str_set(str, No);
  1421. X
  1422. X#endif /* STDSTDIO */
  1423. X
  1424. X    return str->str_cur ? str->str_ptr : Nullch;
  1425. X}
  1426. X
  1427. X
  1428. XSTR *
  1429. Xinterp(str,s)
  1430. Xregister STR *str;
  1431. Xregister char *s;
  1432. X{
  1433. X    register char *t = s;
  1434. X    char *envsave = envname;
  1435. X    envname = Nullch;
  1436. X
  1437. X    str_set(str,"");
  1438. X    while (*s) {
  1439. X    if (*s == '\\' && s[1] == '$') {
  1440. X        str_ncat(str, t, s++ - t);
  1441. X        t = s++;
  1442. X    }
  1443. X    else if (*s == '$' && s[1] && s[1] != '|') {
  1444. X        str_ncat(str,t,s-t);
  1445. X        s = scanreg(s,tokenbuf);
  1446. X        str_cat(str,reg_get(tokenbuf));
  1447. X        t = s;
  1448. X    }
  1449. X    else
  1450. X        s++;
  1451. X    }
  1452. X    envname = envsave;
  1453. X    str_ncat(str,t,s-t);
  1454. X    return str;
  1455. X}
  1456. X
  1457. Xvoid
  1458. Xstr_inc(str)
  1459. Xregister STR *str;
  1460. X{
  1461. X    register char *d;
  1462. X
  1463. X    if (!str)
  1464. X    return;
  1465. X    if (str->str_nok) {
  1466. X    str->str_nval += 1.0;
  1467. X    str->str_pok = 0;
  1468. X    return;
  1469. X    }
  1470. X    if (!str->str_pok) {
  1471. X    str->str_nval = 1.0;
  1472. X    str->str_nok = 1;
  1473. X    return;
  1474. X    }
  1475. X    for (d = str->str_ptr; *d && *d != '.'; d++) ;
  1476. X    d--;
  1477. X    if (!isdigit(*str->str_ptr) || !isdigit(*d) ) {
  1478. X        str_numset(str,atof(str->str_ptr) + 1.0);  /* punt */
  1479. X    return;
  1480. X    }
  1481. X    while (d >= str->str_ptr) {
  1482. X    if (++*d <= '9')
  1483. X        return;
  1484. X    *(d--) = '0';
  1485. X    }
  1486. X    /* oh,oh, the number grew */
  1487. X    GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + 2);
  1488. X    str->str_cur++;
  1489. X    for (d = str->str_ptr + str->str_cur; d > str->str_ptr; d--)
  1490. X    *d = d[-1];
  1491. X    *d = '1';
  1492. X}
  1493. X
  1494. Xvoid
  1495. Xstr_dec(str)
  1496. Xregister STR *str;
  1497. X{
  1498. X    register char *d;
  1499. X
  1500. X    if (!str)
  1501. X    return;
  1502. X    if (str->str_nok) {
  1503. X    str->str_nval -= 1.0;
  1504. X    str->str_pok = 0;
  1505. X    return;
  1506. X    }
  1507. X    if (!str->str_pok) {
  1508. X    str->str_nval = -1.0;
  1509. X    str->str_nok = 1;
  1510. X    return;
  1511. X    }
  1512. X    for (d = str->str_ptr; *d && *d != '.'; d++) ;
  1513. X    d--;
  1514. X    if (!isdigit(*str->str_ptr) || !isdigit(*d) || (*d == '0' && d == str->str_ptr)) {
  1515. X        str_numset(str,atof(str->str_ptr) - 1.0);  /* punt */
  1516. X    return;
  1517. X    }
  1518. X    while (d >= str->str_ptr) {
  1519. X    if (--*d >= '0')
  1520. X        return;
  1521. X    *(d--) = '9';
  1522. X    }
  1523. X}
  1524. X
  1525. X/* make a string that will exist for the duration of the expression eval */
  1526. X
  1527. XSTR *
  1528. Xstr_static(oldstr)
  1529. XSTR *oldstr;
  1530. X{
  1531. X    register STR *str = str_new(0);
  1532. X    static long tmps_size = -1;
  1533. X
  1534. X    str_sset(str,oldstr);
  1535. X    if (++tmps_max > tmps_size) {
  1536. X    tmps_size = tmps_max;
  1537. X    if (!(tmps_size & 127)) {
  1538. X        if (tmps_size)
  1539. X        tmps_list = (STR**)saferealloc((char*)tmps_list,
  1540. X            (tmps_size + 128) * sizeof(STR*) );
  1541. X        else
  1542. X        tmps_list = (STR**)safemalloc(128 * sizeof(char*));
  1543. X    }
  1544. X    }
  1545. X    tmps_list[tmps_max] = str;
  1546. X    return str;
  1547. X}
  1548. X
  1549. XSTR *
  1550. Xstr_make(s)
  1551. Xchar *s;
  1552. X{
  1553. X    register STR *str = str_new(0);
  1554. X
  1555. X    str_set(str,s);
  1556. X    return str;
  1557. X}
  1558. X
  1559. XSTR *
  1560. Xstr_nmake(n)
  1561. Xdouble n;
  1562. X{
  1563. X    register STR *str = str_new(0);
  1564. X
  1565. X    str_numset(str,n);
  1566. X    return str;
  1567. X}
  1568. !STUFFY!FUNK!
  1569. echo Extracting Makefile.SH
  1570. sed >Makefile.SH <<'!STUFFY!FUNK!' -e 's/X//'
  1571. Xcase $CONFIG in
  1572. X'')
  1573. X    if test ! -f config.sh; then
  1574. X    ln ../config.sh . || \
  1575. X    ln ../../config.sh . || \
  1576. X    ln ../../../config.sh . || \
  1577. X    (echo "Can't find config.sh."; exit 1)
  1578. X    fi
  1579. X    . config.sh
  1580. X    ;;
  1581. Xesac
  1582. Xcase "$0" in
  1583. X*/*) cd `expr X$0 : 'X\(.*\)/'` ;;
  1584. Xesac
  1585. Xecho "Extracting Makefile (with variable substitutions)"
  1586. Xcat >Makefile <<!GROK!THIS!
  1587. X# $Header: Makefile.SH,v 1.0 87/12/18 16:11:50 root Exp $
  1588. X#
  1589. X# $Log:    Makefile.SH,v $
  1590. X# Revision 1.0  87/12/18  16:11:50  root
  1591. X# Initial revision
  1592. X# 
  1593. X# Revision 1.0  87/12/18  16:01:07  root
  1594. X# Initial revision
  1595. X# 
  1596. X# 
  1597. X
  1598. XCC = $cc
  1599. Xbin = $bin
  1600. Xlib = $lib
  1601. Xmansrc = $mansrc
  1602. Xmanext = $manext
  1603. XCFLAGS = $ccflags -O
  1604. XLDFLAGS = $ldflags
  1605. XSMALL = $small
  1606. XLARGE = $large $split
  1607. X
  1608. Xlibs = $libnm -lm
  1609. X!GROK!THIS!
  1610. X
  1611. Xcat >>Makefile <<'!NO!SUBS!'
  1612. X
  1613. Xpublic = perl
  1614. X
  1615. Xprivate = 
  1616. X
  1617. Xmanpages = perl.man
  1618. X
  1619. Xutil =
  1620. X
  1621. Xsh = Makefile.SH makedepend.SH
  1622. X
  1623. Xh1 = EXTERN.h INTERN.h arg.h array.h cmd.h config.h form.h handy.h
  1624. Xh2 = hash.h perl.h search.h spat.h stab.h str.h util.h
  1625. X
  1626. Xh = $(h1) $(h2)
  1627. X
  1628. Xc1 = arg.c array.c cmd.c dump.c form.c hash.c malloc.c
  1629. Xc2 = search.c stab.c str.c util.c version.c
  1630. X
  1631. Xc = $(c1) $(c2)
  1632. X
  1633. Xobj1 = arg.o array.o cmd.o dump.o form.o hash.o malloc.o
  1634. Xobj2 = search.o stab.o str.o util.o version.o
  1635. X
  1636. Xobj = $(obj1) $(obj2)
  1637. X
  1638. Xlintflags = -phbvxac
  1639. X
  1640. Xaddedbyconf = Makefile.old bsd eunice filexp loc pdp11 usg v7
  1641. X
  1642. X# grrr
  1643. XSHELL = /bin/sh
  1644. X
  1645. X.c.o:
  1646. X    $(CC) -c $(CFLAGS) $(LARGE) $*.c
  1647. X
  1648. Xall: $(public) $(private) $(util)
  1649. X    touch all
  1650. X
  1651. Xperl: $(obj) perl.o
  1652. X    $(CC) $(LDFLAGS) $(LARGE) $(obj) perl.o $(libs) -o perl
  1653. X
  1654. Xperl.c: perl.y
  1655. X    @ echo Expect 2 shift/reduce errors...
  1656. X    yacc perl.y
  1657. X    mv y.tab.c perl.c
  1658. X
  1659. Xperl.o: perl.c perly.c perl.h EXTERN.h search.h util.h INTERN.h handy.h
  1660. X    $(CC) -c $(CFLAGS) $(LARGE) perl.c
  1661. X
  1662. X# if a .h file depends on another .h file...
  1663. X$(h):
  1664. X    touch $@
  1665. X
  1666. Xperl.man: perl.man.1 perl.man.2
  1667. X    cat perl.man.1 perl.man.2 >perl.man
  1668. X
  1669. Xinstall: perl perl.man
  1670. X# won't work with csh
  1671. X    export PATH || exit 1
  1672. X    - mv $(bin)/perl $(bin)/perl.old
  1673. X    - if test `pwd` != $(bin); then cp $(public) $(bin); fi
  1674. X    cd $(bin); \
  1675. Xfor pub in $(public); do \
  1676. Xchmod 755 `basename $$pub`; \
  1677. Xdone
  1678. X    - test $(bin) = /bin || rm -f /bin/perl
  1679. X    - test $(bin) = /bin || ln -s $(bin)/perl /bin || cp $(bin)/perl /bin
  1680. X#    chmod 755 makedir
  1681. X#    - makedir `filexp $(lib)`
  1682. X#    - \
  1683. X#if test `pwd` != `filexp $(lib)`; then \
  1684. X#cp $(private) `filexp $(lib)`; \
  1685. X#fi
  1686. X#    cd `filexp $(lib)`; \
  1687. X#for priv in $(private); do \
  1688. X#chmod 755 `basename $$priv`; \
  1689. X#done
  1690. X    - if test `pwd` != $(mansrc); then \
  1691. Xfor page in $(manpages); do \
  1692. Xcp $$page $(mansrc)/`basename $$page .man`.$(manext); \
  1693. Xdone; \
  1694. Xfi
  1695. X
  1696. Xclean:
  1697. X    rm -f *.o
  1698. X
  1699. Xrealclean:
  1700. X    rm -f perl *.orig */*.orig *.o core $(addedbyconf)
  1701. X
  1702. X# The following lint has practically everything turned on.  Unfortunately,
  1703. X# you have to wade through a lot of mumbo jumbo that can't be suppressed.
  1704. X# If the source file has a /*NOSTRICT*/ somewhere, ignore the lint message
  1705. X# for that spot.
  1706. X
  1707. Xlint:
  1708. X    lint $(lintflags) $(defs) $(c) > perl.fuzz
  1709. X
  1710. Xdepend: makedepend
  1711. X    makedepend
  1712. X
  1713. Xtest: perl
  1714. X    chmod 755 t/TEST t/base.* t/comp.* t/cmd.* t/io.* t/op.*
  1715. X    cd t && (rm -f perl; ln -s ../perl . || ln ../perl .) && TEST
  1716. X
  1717. Xclist:
  1718. X    echo $(c) | tr ' ' '\012' >.clist
  1719. X
  1720. Xhlist:
  1721. X    echo $(h) | tr ' ' '\012' >.hlist
  1722. X
  1723. Xshlist:
  1724. X    echo $(sh) | tr ' ' '\012' >.shlist
  1725. X
  1726. X# AUTOMATICALLY GENERATED MAKE DEPENDENCIES--PUT NOTHING BELOW THIS LINE
  1727. X$(obj):
  1728. X    @ echo "You haven't done a "'"make depend" yet!'; exit 1
  1729. Xmakedepend: makedepend.SH
  1730. X    /bin/sh makedepend.SH
  1731. X!NO!SUBS!
  1732. X$eunicefix Makefile
  1733. Xcase `pwd` in
  1734. X*SH)
  1735. X    $rm -f ../Makefile
  1736. X    ln Makefile ../Makefile
  1737. X    ;;
  1738. Xesac
  1739. !STUFFY!FUNK!
  1740. echo ""
  1741. echo "End of kit 4 (of 10)"
  1742. cat /dev/null >kit4isdone
  1743. config=true
  1744. for iskit in 1 2 3 4 5 6 7 8 9 10; do
  1745.     if test -f kit${iskit}isdone; then
  1746.     echo "You have run kit ${iskit}."
  1747.     else
  1748.     echo "You still need to run kit ${iskit}."
  1749.     config=false
  1750.     fi
  1751. done
  1752. case $config in
  1753.     true)
  1754.     echo "You have run all your kits.  Please read README and then type Configure."
  1755.     chmod 755 Configure
  1756.     ;;
  1757. esac
  1758. : Someone might mail this, so...
  1759. exit
  1760.