home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume18 / perl / part21 < prev    next >
Encoding:
Internet Message Format  |  1991-04-16  |  49.2 KB

  1. From: lwall@netlabs.com (Larry Wall)
  2. Newsgroups: comp.sources.misc
  3. Subject: v18i039:  perl - The perl programming language, Part21/36
  4. Message-ID: <1991Apr16.185542.1179@sparky.IMD.Sterling.COM>
  5. Date: 16 Apr 91 18:55:42 GMT
  6. Approved: kent@sparky.imd.sterling.com
  7. X-Checksum-Snefru: 9d4a2d27 855efbbe d8382499 572f16a8
  8.  
  9. Submitted-by: Larry Wall <lwall@netlabs.com>
  10. Posting-number: Volume 18, Issue 39
  11. Archive-name: perl/part21
  12.  
  13. [There are 36 kits for perl version 4.0.]
  14.  
  15. #! /bin/sh
  16.  
  17. # Make a new directory for the perl sources, cd to it, and run kits 1
  18. # thru 36 through sh.  When all 36 kits have been run, read README.
  19.  
  20. echo "This is perl 4.0 kit 21 (of 36).  If kit 21 is complete, the line"
  21. echo '"'"End of kit 21 (of 36)"'" will echo at the end.'
  22. echo ""
  23. export PATH || (echo "You didn't use sh, you clunch." ; kill $$)
  24. mkdir emacs t t/op 2>/dev/null
  25. echo Extracting emacs/perl-mode.el
  26. sed >emacs/perl-mode.el <<'!STUFFY!FUNK!' -e 's/X//'
  27. X;; Perl code editing commands for GNU Emacs
  28. X;;   Copyright (C) 1990  William F. Mann
  29. X;; Adapted from C code editing commands 'c-mode.el', Copyright 1987 by the
  30. X;; Free Software Foundation, under terms of its General Public License.
  31. X
  32. X;; This file may be made part of GNU Emacs at the option of the FSF, or
  33. X;; of the perl distribution at the option of Larry Wall.
  34. X
  35. X;; This code is distributed in the hope that it will be useful,
  36. X;; but WITHOUT ANY WARRANTY.  No author or distributor
  37. X;; accepts responsibility to anyone for the consequences of using it
  38. X;; or for whether it serves any particular purpose or works at all,
  39. X;; unless he says so in writing.  Refer to the GNU Emacs General Public
  40. X;; License for full details.
  41. X
  42. X;; Everyone is granted permission to copy, modify and redistribute
  43. X;; this code, but only under the conditions described in the
  44. X;; GNU Emacs General Public License.   A copy of this license is
  45. X;; supposed to have been given to you along with GNU Emacs so you
  46. X;; can know your rights and responsibilities.  It should be in a
  47. X;; file named COPYING.  Among other things, the copyright notice
  48. X;; and this notice must be preserved on all copies.
  49. X
  50. X;; To enter perl-mode automatically, add (autoload 'perl-mode "perl-mode")
  51. X;; to your .emacs file and change the first line of your perl script to:
  52. X;; #!/usr/bin/perl --     # -*-Perl-*-
  53. X;; With argments to perl:
  54. X;; #!/usr/bin/perl -P-     # -*-Perl-*-
  55. X;; To handle files included with do 'filename.pl';, add something like
  56. X;; (setq auto-mode-alist (append (list (cons "\\.pl$" 'perl-mode))
  57. X;;                               auto-mode-alist))
  58. X;; to your .emacs file; otherwise the .pl suffix defaults to prolog-mode.
  59. X
  60. X;; This code is based on the 18.53 version c-mode.el, with extensive
  61. X;; rewriting.  Most of the features of c-mode survived intact.
  62. X
  63. X;; I added a new feature which adds functionality to TAB; it is controlled
  64. X;; by the variable perl-tab-to-comment.  With it enabled, TAB does the
  65. X;; first thing it can from the following list:  change the indentation;
  66. X;; move past leading white space; delete an empty comment; reindent a
  67. X;; comment; move to end of line; create an empty comment; tell you that
  68. X;; the line ends in a quoted string, or has a # which should be a \#.
  69. X
  70. X;; If your machine is slow, you may want to remove some of the bindings
  71. X;; to electric-perl-terminator.  I changed the indenting defaults to be
  72. X;; what Larry Wall uses in perl/lib, but left in all the options.
  73. X
  74. X;; I also tuned a few things:  comments and labels starting in column
  75. X;; zero are left there by indent-perl-exp; perl-beginning-of-function
  76. X;; goes back to the first open brace/paren in column zero, the open brace
  77. X;; in 'sub ... {', or the equal sign in 'format ... ='; indent-perl-exp
  78. X;; (meta-^q) indents from the current line through the close of the next
  79. X;; brace/paren, so you don't need to start exactly at a brace or paren.
  80. X
  81. X;; It may be good style to put a set of redundant braces around your
  82. X;; main program.  This will let you reindent it with meta-^q.
  83. X
  84. X;; Known problems (these are all caused by limitations in the elisp
  85. X;; parsing routine (parse-partial-sexp), which was not designed for such
  86. X;; a rich language; writing a more suitable parser would be a big job):
  87. X;; 1)  Regular expression delimitors do not act as quotes, so special
  88. X;;       characters such as `'"#:;[](){} may need to be backslashed
  89. X;;       in regular expressions and in both parts of s/// and tr///.
  90. X;; 2)  The globbing syntax <pattern> is not recognized, so special
  91. X;;       characters in the pattern string must be backslashed.
  92. X;; 3)  The q, qq, and << quoting operators are not recognized; see below.
  93. X;; 4)  \ (backslash) always quotes the next character, so '\' is
  94. X;;       treated as the start of a string.  Use "\\" as a work-around.
  95. X;; 5)  To make variables such a $' and $#array work, perl-mode treats
  96. X;;       $ just like backslash, so '$' is the same as problem 5.
  97. X;; 6)  Unfortunately, treating $ like \ makes ${var} be treated as an
  98. X;;       unmatched }.  See below.
  99. X;; 7)  When ' (quote) is used as a package name separator, perl-mode
  100. X;;       doesn't understand, and thinks it is seeing a quoted string.
  101. X
  102. X;; Here are some ugly tricks to bypass some of these problems:  the perl
  103. X;; expression /`/ (that's a back-tick) usually evaluates harmlessly,
  104. X;; but will trick perl-mode into starting a quoted string, which
  105. X;; can be ended with another /`/.  Assuming you have no embedded
  106. X;; back-ticks, this can used to help solve problem 3:
  107. X;;
  108. X;;     /`/; $ugly = q?"'$?; /`/;
  109. X;;
  110. X;; To solve problem 6, add a /{/; before each use of ${var}:
  111. X;;     /{/; while (<${glob_me}>) ...
  112. X;;
  113. X;; Problem 7 is even worse, but this 'fix' does work :-(
  114. X;;     $DB'stop#'
  115. X;;         [$DB'line#'
  116. X;;          ] =~ s/;9$//;
  117. X
  118. X
  119. X(defvar perl-mode-abbrev-table nil
  120. X  "Abbrev table in use in perl-mode buffers.")
  121. X(define-abbrev-table 'perl-mode-abbrev-table ())
  122. X
  123. X(defvar perl-mode-map ()
  124. X  "Keymap used in Perl mode.")
  125. X(if perl-mode-map
  126. X    ()
  127. X  (setq perl-mode-map (make-sparse-keymap))
  128. X  (define-key perl-mode-map "{" 'electric-perl-terminator)
  129. X  (define-key perl-mode-map "}" 'electric-perl-terminator)
  130. X  (define-key perl-mode-map ";" 'electric-perl-terminator)
  131. X  (define-key perl-mode-map ":" 'electric-perl-terminator)
  132. X  (define-key perl-mode-map "\e\C-a" 'perl-beginning-of-function)
  133. X  (define-key perl-mode-map "\e\C-e" 'perl-end-of-function)
  134. X  (define-key perl-mode-map "\e\C-h" 'mark-perl-function)
  135. X  (define-key perl-mode-map "\e\C-q" 'indent-perl-exp)
  136. X  (define-key perl-mode-map "\177" 'backward-delete-char-untabify)
  137. X  (define-key perl-mode-map "\t" 'perl-indent-command))
  138. X
  139. X(autoload 'c-macro-expand "cmacexp"
  140. X  "Display the result of expanding all C macros occurring in the region.
  141. XThe expansion is entirely correct because it uses the C preprocessor."
  142. X  t)
  143. X
  144. X(defvar perl-mode-syntax-table nil
  145. X  "Syntax table in use in perl-mode buffers.")
  146. X
  147. X(if perl-mode-syntax-table
  148. X    ()
  149. X  (setq perl-mode-syntax-table (make-syntax-table (standard-syntax-table)))
  150. X  (modify-syntax-entry ?\n ">" perl-mode-syntax-table)
  151. X  (modify-syntax-entry ?# "<" perl-mode-syntax-table)
  152. X  (modify-syntax-entry ?$ "/" perl-mode-syntax-table)
  153. X  (modify-syntax-entry ?% "." perl-mode-syntax-table)
  154. X  (modify-syntax-entry ?& "." perl-mode-syntax-table)
  155. X  (modify-syntax-entry ?\' "\"" perl-mode-syntax-table)
  156. X  (modify-syntax-entry ?* "." perl-mode-syntax-table)
  157. X  (modify-syntax-entry ?+ "." perl-mode-syntax-table)
  158. X  (modify-syntax-entry ?- "." perl-mode-syntax-table)
  159. X  (modify-syntax-entry ?/ "." perl-mode-syntax-table)
  160. X  (modify-syntax-entry ?< "." perl-mode-syntax-table)
  161. X  (modify-syntax-entry ?= "." perl-mode-syntax-table)
  162. X  (modify-syntax-entry ?> "." perl-mode-syntax-table)
  163. X  (modify-syntax-entry ?\\ "\\" perl-mode-syntax-table)
  164. X  (modify-syntax-entry ?` "\"" perl-mode-syntax-table)
  165. X  (modify-syntax-entry ?| "." perl-mode-syntax-table)
  166. X)
  167. X
  168. X(defconst perl-indent-level 4
  169. X  "*Indentation of Perl statements with respect to containing block.")
  170. X(defconst perl-continued-statement-offset 4
  171. X  "*Extra indent for lines not starting new statements.")
  172. X(defconst perl-continued-brace-offset -4
  173. X  "*Extra indent for substatements that start with open-braces.
  174. XThis is in addition to perl-continued-statement-offset.")
  175. X(defconst perl-brace-offset 0
  176. X  "*Extra indentation for braces, compared with other text in same context.")
  177. X(defconst perl-brace-imaginary-offset 0
  178. X  "*Imagined indentation of an open brace that actually follows a statement.")
  179. X(defconst perl-label-offset -2
  180. X  "*Offset of Perl label lines relative to usual indentation.")
  181. X
  182. X(defconst perl-tab-always-indent t
  183. X  "*Non-nil means TAB in Perl mode should always indent the current line,
  184. Xregardless of where in the line point is when the TAB command is used.")
  185. X
  186. X(defconst perl-tab-to-comment t
  187. X  "*Non-nil means that for lines which don't need indenting, TAB will
  188. Xeither indent an existing comment, move to end-of-line, or if at end-of-line
  189. Xalready, create a new comment.")
  190. X
  191. X(defconst perl-nochange ";?#\\|\f\\|\\s(\\|\\(\\w\\|\\s_\\)+:"
  192. X  "*Lines starting with this regular expression will not be auto-indented.")
  193. X
  194. X(defun perl-mode ()
  195. X  "Major mode for editing Perl code.
  196. XExpression and list commands understand all Perl brackets.
  197. XTab indents for Perl code.
  198. XComments are delimited with # ... \\n.
  199. XParagraphs are separated by blank lines only.
  200. XDelete converts tabs to spaces as it moves back.
  201. X\\{perl-mode-map}
  202. XVariables controlling indentation style:
  203. X perl-tab-always-indent
  204. X    Non-nil means TAB in Perl mode should always indent the current line,
  205. X    regardless of where in the line point is when the TAB command is used.
  206. X perl-tab-to-comment
  207. X    Non-nil means that for lines which don't need indenting, TAB will
  208. X    either delete an empty comment, indent an existing comment, move 
  209. X    to end-of-line, or if at end-of-line already, create a new comment.
  210. X perl-nochange
  211. X    Lines starting with this regular expression will not be auto-indented.
  212. X perl-indent-level
  213. X    Indentation of Perl statements within surrounding block.
  214. X    The surrounding block's indentation is the indentation
  215. X    of the line on which the open-brace appears.
  216. X perl-continued-statement-offset
  217. X    Extra indentation given to a substatement, such as the
  218. X    then-clause of an if or body of a while.
  219. X perl-continued-brace-offset
  220. X    Extra indentation given to a brace that starts a substatement.
  221. X    This is in addition to perl-continued-statement-offset.
  222. X perl-brace-offset
  223. X    Extra indentation for line if it starts with an open brace.
  224. X perl-brace-imaginary-offset
  225. X    An open brace following other text is treated as if it were
  226. X    this far to the right of the start of its line.
  227. X perl-label-offset
  228. X    Extra indentation for line that is a label.
  229. X
  230. XVarious indentation styles:       K&R  BSD  BLK  GNU  LW
  231. X  perl-indent-level                5    8    0    2    4
  232. X  perl-continued-statement-offset  5    8    4    2    4
  233. X  perl-continued-brace-offset      0    0    0    0   -4
  234. X  perl-brace-offset               -5   -8    0    0    0
  235. X  perl-brace-imaginary-offset      0    0    4    0    0
  236. X  perl-label-offset               -5   -8   -2   -2   -2
  237. X
  238. XTurning on Perl mode calls the value of the variable perl-mode-hook with no 
  239. Xargs, if that value is non-nil."
  240. X  (interactive)
  241. X  (kill-all-local-variables)
  242. X  (use-local-map perl-mode-map)
  243. X  (setq major-mode 'perl-mode)
  244. X  (setq mode-name "Perl")
  245. X  (setq local-abbrev-table perl-mode-abbrev-table)
  246. X  (set-syntax-table perl-mode-syntax-table)
  247. X  (make-local-variable 'paragraph-start)
  248. X  (setq paragraph-start (concat "^$\\|" page-delimiter))
  249. X  (make-local-variable 'paragraph-separate)
  250. X  (setq paragraph-separate paragraph-start)
  251. X  (make-local-variable 'paragraph-ignore-fill-prefix)
  252. X  (setq paragraph-ignore-fill-prefix t)
  253. X  (make-local-variable 'indent-line-function)
  254. X  (setq indent-line-function 'perl-indent-line)
  255. X  (make-local-variable 'require-final-newline)
  256. X  (setq require-final-newline t)
  257. X  (make-local-variable 'comment-start)
  258. X  (setq comment-start "# ")
  259. X  (make-local-variable 'comment-end)
  260. X  (setq comment-end "")
  261. X  (make-local-variable 'comment-column)
  262. X  (setq comment-column 32)
  263. X  (make-local-variable 'comment-start-skip)
  264. X  (setq comment-start-skip "\\(^\\|\\s-\\);?#+ *")
  265. X  (make-local-variable 'comment-indent-hook)
  266. X  (setq comment-indent-hook 'perl-comment-indent)
  267. X  (make-local-variable 'parse-sexp-ignore-comments)
  268. X  (setq parse-sexp-ignore-comments nil)
  269. X  (run-hooks 'perl-mode-hook))
  270. X
  271. X;; This is used by indent-for-comment
  272. X;; to decide how much to indent a comment in Perl code
  273. X;; based on its context.
  274. X(defun perl-comment-indent ()
  275. X  (if (and (bolp) (not (eolp)))
  276. X      0                    ;Existing comment at bol stays there.
  277. X    (save-excursion
  278. X      (skip-chars-backward " \t")
  279. X      (max (1+ (current-column))    ;Else indent at comment column
  280. X       comment-column))))        ; except leave at least one space.
  281. X
  282. X(defun electric-perl-terminator (arg)
  283. X  "Insert character.  If at end-of-line, and not in a comment or a quote,
  284. Xcorrect the line's indentation."
  285. X  (interactive "P")
  286. X  (let ((insertpos (point)))
  287. X    (and (not arg)            ; decide whether to indent
  288. X     (eolp)
  289. X     (save-excursion
  290. X       (beginning-of-line)
  291. X       (and (not            ; eliminate comments quickly
  292. X         (re-search-forward comment-start-skip insertpos t)) 
  293. X        (or (/= last-command-char ?:)
  294. X            ;; Colon is special only after a label ....
  295. X            (looking-at "\\s-*\\(\\w\\|\\s_\\)+$"))
  296. X        (let ((pps (parse-partial-sexp 
  297. X                (perl-beginning-of-function) insertpos)))
  298. X          (not (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))
  299. X     (progn                ; must insert, indent, delete
  300. X       (insert-char last-command-char 1)
  301. X       (perl-indent-line)
  302. X       (delete-char -1))))
  303. X  (self-insert-command (prefix-numeric-value arg)))
  304. X
  305. X;; not used anymore, but may be useful someday:
  306. X;;(defun perl-inside-parens-p ()
  307. X;;  (condition-case ()
  308. X;;      (save-excursion
  309. X;;    (save-restriction
  310. X;;      (narrow-to-region (point)
  311. X;;                (perl-beginning-of-function))
  312. X;;      (goto-char (point-max))
  313. X;;      (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
  314. X;;    (error nil)))
  315. X
  316. X(defun perl-indent-command (&optional arg)
  317. X  "Indent current line as Perl code, or optionally, insert a tab character.
  318. X
  319. XWith an argument, indent the current line, regardless of other options.
  320. X
  321. XIf perl-tab-always-indent is nil and point is not in the indentation
  322. Xarea at the beginning of the line, simply insert a tab.
  323. X
  324. XOtherwise, indent the current line.  If point was within the indentation
  325. Xarea it is moved to the end of the indentation area.  If the line was
  326. Xalready indented properly and point was not within the indentation area,
  327. Xand if perl-tab-to-comment is non-nil (the default), then do the first
  328. Xpossible action from the following list:
  329. X
  330. X  1) delete an empty comment
  331. X  2) move forward to start of comment, indenting if necessary
  332. X  3) move forward to end of line
  333. X  4) create an empty comment
  334. X  5) move backward to start of comment, indenting if necessary."
  335. X  (interactive "P")
  336. X  (if arg                ; If arg, just indent this line
  337. X      (perl-indent-line "\f")
  338. X    (if (and (not perl-tab-always-indent)
  339. X         (<= (current-column) (current-indentation)))
  340. X    (insert-tab)
  341. X      (let (bof lsexp delta (oldpnt (point)))
  342. X    (beginning-of-line) 
  343. X    (setq lsexp (point))
  344. X    (setq bof (perl-beginning-of-function))
  345. X    (goto-char oldpnt)
  346. X    (setq delta (perl-indent-line "\f\\|;?#" bof))
  347. X    (and perl-tab-to-comment
  348. X         (= oldpnt (point))        ; done if point moved
  349. X         (if (listp delta)        ; if line starts in a quoted string
  350. X         (setq lsexp (or (nth 2 delta) bof))
  351. X           (= delta 0))        ; done if indenting occurred
  352. X         (let (eol state)
  353. X           (end-of-line) 
  354. X           (setq eol (point))
  355. X           (if (= (char-after bof) ?=)
  356. X           (if (= oldpnt eol)
  357. X               (message "In a format statement"))     
  358. X         (setq state (parse-partial-sexp lsexp eol))
  359. X         (if (nth 3 state)
  360. X             (if (= oldpnt eol)    ; already at eol in a string
  361. X             (message "In a string which starts with a %c."
  362. X                  (nth 3 state)))
  363. X           (if (not (nth 4 state))
  364. X               (if (= oldpnt eol) ; no comment, create one?
  365. X               (indent-for-comment))
  366. X             (beginning-of-line)
  367. X             (if (re-search-forward comment-start-skip eol 'move)
  368. X             (if (eolp)
  369. X                 (progn    ; kill existing comment
  370. X                   (goto-char (match-beginning 0))
  371. X                   (skip-chars-backward " \t")
  372. X                   (kill-region (point) eol))
  373. X               (if (or (< oldpnt (point)) (= oldpnt eol))
  374. X                   (indent-for-comment) ; indent existing comment
  375. X                 (end-of-line)))
  376. X               (if (/= oldpnt eol)
  377. X               (end-of-line)
  378. X             (message "Use backslash to quote # characters.")
  379. X             (ding t))))))))))))
  380. X
  381. X(defun perl-indent-line (&optional nochange parse-start)
  382. X  "Indent current line as Perl code.  Return the amount the indentation 
  383. Xchanged by, or (parse-state) if line starts in a quoted string."
  384. X  (let ((case-fold-search nil)
  385. X    (pos (- (point-max) (point)))
  386. X    (bof (or parse-start (save-excursion (perl-beginning-of-function))))
  387. X    beg indent shift-amt)
  388. X    (beginning-of-line)
  389. X    (setq beg (point))
  390. X    (setq shift-amt
  391. X      (cond ((= (char-after bof) ?=) 0)
  392. X        ((listp (setq indent (calculate-perl-indent bof))) indent)
  393. X        ((looking-at (or nochange perl-nochange)) 0)
  394. X        (t
  395. X         (skip-chars-forward " \t\f")
  396. X         (cond ((looking-at "\\(\\w\\|\\s_\\)+:")
  397. X            (setq indent (max 1 (+ indent perl-label-offset))))
  398. X               ((= (following-char) ?})
  399. X            (setq indent (- indent perl-indent-level)))
  400. X               ((= (following-char) ?{)
  401. X            (setq indent (+ indent perl-brace-offset))))
  402. X         (- indent (current-column)))))
  403. X    (skip-chars-forward " \t\f")
  404. X    (if (and (numberp shift-amt) (/= 0 shift-amt))
  405. X    (progn (delete-region beg (point))
  406. X           (indent-to indent)))
  407. X    ;; If initial point was within line's indentation,
  408. X    ;; position after the indentation.  Else stay at same point in text.
  409. X    (if (> (- (point-max) pos) (point))
  410. X    (goto-char (- (point-max) pos)))
  411. X    shift-amt))
  412. X
  413. X(defun calculate-perl-indent (&optional parse-start)
  414. X  "Return appropriate indentation for current line as Perl code.
  415. XIn usual case returns an integer: the column to indent to.
  416. XReturns (parse-state) if line starts inside a string."
  417. X  (save-excursion
  418. X    (beginning-of-line)
  419. X    (let ((indent-point (point))
  420. X      (case-fold-search nil)
  421. X      (colon-line-end 0)
  422. X      state containing-sexp)
  423. X      (if parse-start            ;used to avoid searching
  424. X      (goto-char parse-start)
  425. X    (perl-beginning-of-function))
  426. X      (while (< (point) indent-point)    ;repeat until right sexp
  427. X    (setq parse-start (point))
  428. X    (setq state (parse-partial-sexp (point) indent-point 0))
  429. X; state = (depth_in_parens innermost_containing_list last_complete_sexp
  430. X;          string_terminator_or_nil inside_commentp following_quotep
  431. X;          minimum_paren-depth_this_scan)
  432. X; Parsing stops if depth in parentheses becomes equal to third arg.
  433. X    (setq containing-sexp (nth 1 state)))
  434. X      (cond ((nth 3 state) state)    ; In a quoted string?
  435. X        ((null containing-sexp)    ; Line is at top level.
  436. X         (skip-chars-forward " \t\f")
  437. X         (if (= (following-char) ?{)
  438. X         0   ; move to beginning of line if it starts a function body
  439. X           ;; indent a little if this is a continuation line
  440. X           (perl-backward-to-noncomment)
  441. X           (if (or (bobp)
  442. X               (memq (preceding-char) '(?\; ?\})))
  443. X           0 perl-continued-statement-offset)))
  444. X        ((/= (char-after containing-sexp) ?{)
  445. X         ;; line is expression, not statement:
  446. X         ;; indent to just after the surrounding open.
  447. X         (goto-char (1+ containing-sexp))
  448. X         (current-column))
  449. X        (t
  450. X         ;; Statement level.  Is it a continuation or a new statement?
  451. X         ;; Find previous non-comment character.
  452. X         (perl-backward-to-noncomment)
  453. X         ;; Back up over label lines, since they don't
  454. X         ;; affect whether our line is a continuation.
  455. X         (while (or (eq (preceding-char) ?\,)
  456. X            (and (eq (preceding-char) ?:)
  457. X                 (memq (char-syntax (char-after (- (point) 2)))
  458. X                   '(?w ?_))))
  459. X           (if (eq (preceding-char) ?\,)
  460. X           (perl-backward-to-start-of-continued-exp containing-sexp))
  461. X           (beginning-of-line)
  462. X           (perl-backward-to-noncomment))
  463. X         ;; Now we get the answer.
  464. X         (if (not (memq (preceding-char) '(?\; ?\} ?\{)))
  465. X         ;; This line is continuation of preceding line's statement;
  466. X         ;; indent  perl-continued-statement-offset  more than the
  467. X         ;; previous line of the statement.
  468. X         (progn
  469. X           (perl-backward-to-start-of-continued-exp containing-sexp)
  470. X           (+ perl-continued-statement-offset (current-column)
  471. X              (if (save-excursion (goto-char indent-point)
  472. X                      (looking-at "[ \t]*{"))
  473. X              perl-continued-brace-offset 0)))
  474. X           ;; This line starts a new statement.
  475. X           ;; Position at last unclosed open.
  476. X           (goto-char containing-sexp)
  477. X           (or
  478. X         ;; If open paren is in col 0, close brace is special
  479. X         (and (bolp)
  480. X              (save-excursion (goto-char indent-point)
  481. X                      (looking-at "[ \t]*}"))
  482. X              perl-indent-level)
  483. X         ;; Is line first statement after an open-brace?
  484. X         ;; If no, find that first statement and indent like it.
  485. X         (save-excursion
  486. X           (forward-char 1)
  487. X           ;; Skip over comments and labels following openbrace.
  488. X           (while (progn
  489. X                (skip-chars-forward " \t\f\n")
  490. X                (cond ((looking-at ";?#")
  491. X                   (forward-line 1) t)
  492. X                  ((looking-at "\\(\\w\\|\\s_\\)+:")
  493. X                   (save-excursion 
  494. X                     (end-of-line) 
  495. X                     (setq colon-line-end (point)))
  496. X                   (search-forward ":")))))
  497. X           ;; The first following code counts
  498. X           ;; if it is before the line we want to indent.
  499. X           (and (< (point) indent-point)
  500. X            (if (> colon-line-end (point))
  501. X                (- (current-indentation) perl-label-offset)
  502. X              (current-column))))
  503. X         ;; If no previous statement,
  504. X         ;; indent it relative to line brace is on.
  505. X         ;; For open paren in column zero, don't let statement
  506. X         ;; start there too.  If perl-indent-level is zero,
  507. X         ;; use perl-brace-offset + perl-continued-statement-offset
  508. X         ;; For open-braces not the first thing in a line,
  509. X         ;; add in perl-brace-imaginary-offset.
  510. X         (+ (if (and (bolp) (zerop perl-indent-level))
  511. X            (+ perl-brace-offset perl-continued-statement-offset)
  512. X              perl-indent-level)
  513. X            ;; Move back over whitespace before the openbrace.
  514. X            ;; If openbrace is not first nonwhite thing on the line,
  515. X            ;; add the perl-brace-imaginary-offset.
  516. X            (progn (skip-chars-backward " \t")
  517. X               (if (bolp) 0 perl-brace-imaginary-offset))
  518. X            ;; If the openbrace is preceded by a parenthesized exp,
  519. X            ;; move to the beginning of that;
  520. X            ;; possibly a different line
  521. X            (progn
  522. X              (if (eq (preceding-char) ?\))
  523. X              (forward-sexp -1))
  524. X              ;; Get initial indentation of the line we are on.
  525. X              (current-indentation))))))))))
  526. X
  527. X(defun perl-backward-to-noncomment ()
  528. X  "Move point backward to after the first non-white-space, skipping comments."
  529. X  (interactive)
  530. X  (let (opoint stop)
  531. X    (while (not stop)
  532. X      (setq opoint (point))
  533. X      (beginning-of-line)
  534. X      (if (re-search-forward comment-start-skip opoint 'move 1)
  535. X      (progn (goto-char (match-end 1))
  536. X         (skip-chars-forward ";")))
  537. X      (skip-chars-backward " \t\f")
  538. X      (setq stop (or (bobp)
  539. X             (not (bolp))
  540. X             (forward-char -1))))))
  541. X
  542. X(defun perl-backward-to-start-of-continued-exp (lim)
  543. X  (if (= (preceding-char) ?\))
  544. X      (forward-sexp -1))
  545. X  (beginning-of-line)
  546. X  (if (<= (point) lim)
  547. X      (goto-char (1+ lim)))
  548. X  (skip-chars-forward " \t\f"))
  549. X
  550. X;; note: this may be slower than the c-mode version, but I can understand it.
  551. X(defun indent-perl-exp ()
  552. X  "Indent each line of the Perl grouping following point."
  553. X  (interactive)
  554. X  (let* ((case-fold-search nil)
  555. X     (oldpnt (point-marker))
  556. X     (bof-mark (save-excursion
  557. X             (end-of-line 2)
  558. X             (perl-beginning-of-function)
  559. X             (point-marker)))
  560. X     eol last-mark lsexp-mark delta)
  561. X    (if (= (char-after (marker-position bof-mark)) ?=)
  562. X    (message "Can't indent a format statement")
  563. X      (message "Indenting Perl expression...")
  564. X      (save-excursion (end-of-line) (setq eol (point)))
  565. X      (save-excursion            ; locate matching close paren
  566. X    (while (and (not (eobp)) (<= (point) eol))
  567. X      (parse-partial-sexp (point) (point-max) 0))
  568. X    (setq last-mark (point-marker)))
  569. X      (setq lsexp-mark bof-mark)
  570. X      (beginning-of-line)
  571. X      (while (< (point) (marker-position last-mark))
  572. X    (setq delta (perl-indent-line nil (marker-position bof-mark)))
  573. X    (if (numberp delta)        ; unquoted start-of-line?
  574. X        (progn 
  575. X          (if (eolp)
  576. X          (delete-horizontal-space))
  577. X          (setq lsexp-mark (point-marker))))
  578. X    (end-of-line)
  579. X    (setq eol (point))
  580. X    (if (nth 4 (parse-partial-sexp (marker-position lsexp-mark) eol))
  581. X        (progn            ; line ends in a comment
  582. X          (beginning-of-line)
  583. X          (if (or (not (looking-at "\\s-*;?#"))
  584. X              (listp delta)
  585. X              (and (/= 0 delta)
  586. X               (= (- (current-indentation) delta) comment-column)))
  587. X          (if (re-search-forward comment-start-skip eol t)
  588. X              (indent-for-comment))))) ; indent existing comment
  589. X    (forward-line 1))
  590. X      (goto-char (marker-position oldpnt))
  591. X      (message "Indenting Perl expression...done"))))
  592. X
  593. X(defun perl-beginning-of-function (&optional arg)
  594. X  "Move backward to next beginning-of-function, or as far as possible.
  595. XWith argument, repeat that many times; negative args move forward.
  596. XReturns new value of point in all cases."
  597. X  (interactive "p")
  598. X  (or arg (setq arg 1))
  599. X  (if (< arg 0) (forward-char 1))
  600. X  (and (/= arg 0)
  601. X       (re-search-backward "^\\s(\\|^\\s-*sub\\b[^{]+{\\|^\\s-*format\\b[^=]*="
  602. X               nil 'move arg)
  603. X       (goto-char (1- (match-end 0))))
  604. X  (point))
  605. X
  606. X;; note: this routine is adapted directly from emacs lisp.el, end-of-defun;
  607. X;; no bugs have been removed :-)
  608. X(defun perl-end-of-function (&optional arg)
  609. X  "Move forward to next end-of-function.
  610. XThe end of a function is found by moving forward from the beginning of one.
  611. XWith argument, repeat that many times; negative args move backward."
  612. X  (interactive "p")
  613. X  (or arg (setq arg 1))
  614. X  (let ((first t))
  615. X    (while (and (> arg 0) (< (point) (point-max)))
  616. X      (let ((pos (point)) npos)
  617. X    (while (progn
  618. X        (if (and first
  619. X             (progn
  620. X              (forward-char 1)
  621. X              (perl-beginning-of-function 1)
  622. X              (not (bobp))))
  623. X            nil
  624. X          (or (bobp) (forward-char -1))
  625. X          (perl-beginning-of-function -1))
  626. X        (setq first nil)
  627. X        (forward-list 1)
  628. X        (skip-chars-forward " \t")
  629. X        (if (looking-at "[#\n]")
  630. X            (forward-line 1))
  631. X        (<= (point) pos))))
  632. X      (setq arg (1- arg)))
  633. X    (while (< arg 0)
  634. X      (let ((pos (point)))
  635. X    (perl-beginning-of-function 1)
  636. X    (forward-sexp 1)
  637. X    (forward-line 1)
  638. X    (if (>= (point) pos)
  639. X        (if (progn (perl-beginning-of-function 2) (not (bobp)))
  640. X        (progn
  641. X          (forward-list 1)
  642. X          (skip-chars-forward " \t")
  643. X          (if (looking-at "[#\n]")
  644. X              (forward-line 1)))
  645. X          (goto-char (point-min)))))
  646. X      (setq arg (1+ arg)))))
  647. X
  648. X(defun mark-perl-function ()
  649. X  "Put mark at end of Perl function, point at beginning."
  650. X  (interactive)
  651. X  (push-mark (point))
  652. X  (perl-end-of-function)
  653. X  (push-mark (point))
  654. X  (perl-beginning-of-function)
  655. X  (backward-paragraph))
  656. X
  657. X;;;;;;;; That's all, folks! ;;;;;;;;;
  658. !STUFFY!FUNK!
  659. echo Extracting regexec.c
  660. sed >regexec.c <<'!STUFFY!FUNK!' -e 's/X//'
  661. X/* NOTE: this is derived from Henry Spencer's regexp code, and should not
  662. X * confused with the original package (see point 3 below).  Thanks, Henry!
  663. X */
  664. X
  665. X/* Additional note: this code is very heavily munged from Henry's version
  666. X * in places.  In some spots I've traded clarity for efficiency, so don't
  667. X * blame Henry for some of the lack of readability.
  668. X */
  669. X
  670. X/* $RCSfile: regexec.c,v $$Revision: 4.0.1.1 $$Date: 91/04/12 09:07:39 $
  671. X *
  672. X * $Log:    regexec.c,v $
  673. X * Revision 4.0.1.1  91/04/12  09:07:39  lwall
  674. X * patch1: regexec only allocated space for 9 subexpresssions
  675. X * 
  676. X * Revision 4.0  91/03/20  01:39:16  lwall
  677. X * 4.0 baseline.
  678. X * 
  679. X */
  680. X
  681. X/*
  682. X * regcomp and regexec -- regsub and regerror are not used in perl
  683. X *
  684. X *    Copyright (c) 1986 by University of Toronto.
  685. X *    Written by Henry Spencer.  Not derived from licensed software.
  686. X *
  687. X *    Permission is granted to anyone to use this software for any
  688. X *    purpose on any computer system, and to redistribute it freely,
  689. X *    subject to the following restrictions:
  690. X *
  691. X *    1. The author is not responsible for the consequences of use of
  692. X *        this software, no matter how awful, even if they arise
  693. X *        from defects in it.
  694. X *
  695. X *    2. The origin of this software must not be misrepresented, either
  696. X *        by explicit claim or by omission.
  697. X *
  698. X *    3. Altered versions must be plainly marked as such, and must not
  699. X *        be misrepresented as being the original software.
  700. X *
  701. X ****    Alterations to Henry's code are...
  702. X ****
  703. X ****    Copyright (c) 1989, Larry Wall
  704. X ****
  705. X ****    You may distribute under the terms of the GNU General Public License
  706. X ****    as specified in the README file that comes with the perl 3.0 kit.
  707. X *
  708. X * Beware that some of this code is subtly aware of the way operator
  709. X * precedence is structured in regular expressions.  Serious changes in
  710. X * regular-expression syntax might require a total rethink.
  711. X */
  712. X#include "EXTERN.h"
  713. X#include "perl.h"
  714. X#include "regcomp.h"
  715. X
  716. X#ifndef STATIC
  717. X#define    STATIC    static
  718. X#endif
  719. X
  720. X#ifdef DEBUGGING
  721. Xint regnarrate = 0;
  722. X#endif
  723. X
  724. X#define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
  725. X#define isSPACE(c) (isascii(c) && isspace(c))
  726. X#define isDIGIT(c) (isascii(c) && isdigit(c))
  727. X#define isUPPER(c) (isascii(c) && isupper(c))
  728. X
  729. X/*
  730. X * regexec and friends
  731. X */
  732. X
  733. X/*
  734. X * Global work variables for regexec().
  735. X */
  736. Xstatic char *regprecomp;
  737. Xstatic char *reginput;        /* String-input pointer. */
  738. Xstatic char regprev;        /* char before regbol, \n if none */
  739. Xstatic char *regbol;        /* Beginning of input, for ^ check. */
  740. Xstatic char *regeol;        /* End of input, for $ check. */
  741. Xstatic char **regstartp;    /* Pointer to startp array. */
  742. Xstatic char **regendp;        /* Ditto for endp. */
  743. Xstatic char *reglastparen;    /* Similarly for lastparen. */
  744. Xstatic char *regtill;
  745. X
  746. Xstatic int regmyp_size = 0;
  747. Xstatic char **regmystartp = Null(char**);
  748. Xstatic char **regmyendp   = Null(char**);
  749. X
  750. X/*
  751. X * Forwards.
  752. X */
  753. XSTATIC int regtry();
  754. XSTATIC int regmatch();
  755. XSTATIC int regrepeat();
  756. X
  757. Xextern int multiline;
  758. X
  759. X/*
  760. X - regexec - match a regexp against a string
  761. X */
  762. Xint
  763. Xregexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
  764. Xregister regexp *prog;
  765. Xchar *stringarg;
  766. Xregister char *strend;    /* pointer to null at end of string */
  767. Xchar *strbeg;    /* real beginning of string */
  768. Xint minend;    /* end of match must be at least minend after stringarg */
  769. XSTR *screamer;
  770. Xint safebase;    /* no need to remember string in subbase */
  771. X{
  772. X    register char *s;
  773. X    register int i;
  774. X    register char *c;
  775. X    register char *string = stringarg;
  776. X    register int tmp;
  777. X    int minlen = 0;        /* must match at least this many chars */
  778. X    int dontbother = 0;    /* how many characters not to try at end */
  779. X
  780. X    /* Be paranoid... */
  781. X    if (prog == NULL || string == NULL) {
  782. X        fatal("NULL regexp parameter");
  783. X        return(0);
  784. X    }
  785. X
  786. X    if (string == strbeg)    /* is ^ valid at stringarg? */
  787. X        regprev = '\n';
  788. X    else {
  789. X        regprev = stringarg[-1];
  790. X        if (!multiline && regprev == '\n')
  791. X        regprev = '\0';        /* force ^ to NOT match */
  792. X    }
  793. X    regprecomp = prog->precomp;
  794. X    /* Check validity of program. */
  795. X    if (UCHARAT(prog->program) != MAGIC) {
  796. X        FAIL("corrupted regexp program");
  797. X    }
  798. X
  799. X    if (prog->do_folding) {
  800. X        safebase = FALSE;
  801. X        i = strend - string;
  802. X        New(1101,c,i+1,char);
  803. X        (void)bcopy(string, c, i+1);
  804. X        string = c;
  805. X        strend = string + i;
  806. X        for (s = string; s < strend; s++)
  807. X            if (isUPPER(*s))
  808. X                *s = tolower(*s);
  809. X    }
  810. X
  811. X    /* If there is a "must appear" string, look for it. */
  812. X    s = string;
  813. X    if (prog->regmust != Nullstr &&
  814. X        (!(prog->reganch & 1) || (multiline && prog->regback >= 0)) ) {
  815. X        if (stringarg == strbeg && screamer) {
  816. X            if (screamfirst[prog->regmust->str_rare] >= 0)
  817. X                s = screaminstr(screamer,prog->regmust);
  818. X            else
  819. X                s = Nullch;
  820. X        }
  821. X#ifndef lint
  822. X        else
  823. X            s = fbminstr((unsigned char*)s, (unsigned char*)strend,
  824. X                prog->regmust);
  825. X#endif
  826. X        if (!s) {
  827. X            ++prog->regmust->str_u.str_useful;    /* hooray */
  828. X            goto phooey;    /* not present */
  829. X        }
  830. X        else if (prog->regback >= 0) {
  831. X            s -= prog->regback;
  832. X            if (s < string)
  833. X                s = string;
  834. X            minlen = prog->regback + prog->regmust->str_cur;
  835. X        }
  836. X        else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
  837. X            str_free(prog->regmust);
  838. X            prog->regmust = Nullstr;    /* disable regmust */
  839. X            s = string;
  840. X        }
  841. X        else {
  842. X            s = string;
  843. X            minlen = prog->regmust->str_cur;
  844. X        }
  845. X    }
  846. X
  847. X    /* Mark beginning of line for ^ . */
  848. X    regbol = string;
  849. X
  850. X    /* Mark end of line for $ (and such) */
  851. X    regeol = strend;
  852. X
  853. X    /* see how far we have to get to not match where we matched before */
  854. X    regtill = string+minend;
  855. X
  856. X    /* Allocate our backreference arrays */
  857. X    if ( regmyp_size < prog->nparens + 1 ) {
  858. X        /* Allocate or enlarge the arrays */
  859. X        regmyp_size = prog->nparens + 1;
  860. X        if ( regmyp_size < 10 ) regmyp_size = 10;    /* minimum */
  861. X        if ( regmystartp ) {
  862. X        /* reallocate larger */
  863. X        Renew(regmystartp,regmyp_size,char*);
  864. X        Renew(regmyendp,  regmyp_size,char*);
  865. X        }
  866. X        else {
  867. X        /* Initial allocation */
  868. X        New(1102,regmystartp,regmyp_size,char*);
  869. X        New(1102,regmyendp,  regmyp_size,char*);
  870. X        }
  871. X    
  872. X    }
  873. X
  874. X    /* Simplest case:  anchored match need be tried only once. */
  875. X    /*  [unless multiline is set] */
  876. X    if (prog->reganch & 1) {
  877. X        if (regtry(prog, string))
  878. X            goto got_it;
  879. X        else if (multiline) {
  880. X            if (minlen)
  881. X                dontbother = minlen - 1;
  882. X            strend -= dontbother;
  883. X            /* for multiline we only have to try after newlines */
  884. X            if (s > string)
  885. X                s--;
  886. X            while (s < strend) {
  887. X                if (*s++ == '\n') {
  888. X                if (s < strend && regtry(prog, s))
  889. X                    goto got_it;
  890. X                }
  891. X            }
  892. X        }
  893. X        goto phooey;
  894. X    }
  895. X
  896. X    /* Messy cases:  unanchored match. */
  897. X    if (prog->regstart) {
  898. X        if (prog->reganch & 2) {    /* we have /x+whatever/ */
  899. X            /* it must be a one character string */
  900. X            i = prog->regstart->str_ptr[0];
  901. X            while (s < strend) {
  902. X                if (*s == i) {
  903. X                    if (regtry(prog, s))
  904. X                        goto got_it;
  905. X                    s++;
  906. X                    while (s < strend && *s == i)
  907. X                    s++;
  908. X                }
  909. X                s++;
  910. X            }
  911. X        }
  912. X        else if (prog->regstart->str_pok == 3) {
  913. X            /* We know what string it must start with. */
  914. X#ifndef lint
  915. X            while ((s = fbminstr((unsigned char*)s,
  916. X              (unsigned char*)strend, prog->regstart)) != NULL)
  917. X#else
  918. X            while (s = Nullch)
  919. X#endif
  920. X            {
  921. X                if (regtry(prog, s))
  922. X                    goto got_it;
  923. X                s++;
  924. X            }
  925. X        }
  926. X        else {
  927. X            c = prog->regstart->str_ptr;
  928. X            while ((s = ninstr(s, strend,
  929. X              c, c + prog->regstart->str_cur )) != NULL) {
  930. X                if (regtry(prog, s))
  931. X                    goto got_it;
  932. X                s++;
  933. X            }
  934. X        }
  935. X        goto phooey;
  936. X    }
  937. X    if (c = prog->regstclass) {
  938. X        int doevery = (prog->reganch & 2) == 0;
  939. X
  940. X        if (minlen)
  941. X            dontbother = minlen - 1;
  942. X        strend -= dontbother;    /* don't bother with what can't match */
  943. X        tmp = 1;
  944. X        /* We know what class it must start with. */
  945. X        switch (OP(c)) {
  946. X        case ANYOF:
  947. X            c = OPERAND(c);
  948. X            while (s < strend) {
  949. X                i = UCHARAT(s);
  950. X                if (!(c[i >> 3] & (1 << (i&7)))) {
  951. X                    if (tmp && regtry(prog, s))
  952. X                        goto got_it;
  953. X                    else
  954. X                        tmp = doevery;
  955. X                }
  956. X                else
  957. X                    tmp = 1;
  958. X                s++;
  959. X            }
  960. X            break;
  961. X        case BOUND:
  962. X            if (minlen)
  963. X            dontbother++,strend--;
  964. X            if (s != string) {
  965. X            i = s[-1];
  966. X            tmp = isALNUM(i);
  967. X            }
  968. X            else
  969. X            tmp = isALNUM(regprev);    /* assume not alphanumeric */
  970. X            while (s < strend) {
  971. X                i = *s;
  972. X                if (tmp != isALNUM(i)) {
  973. X                    tmp = !tmp;
  974. X                    if (regtry(prog, s))
  975. X                        goto got_it;
  976. X                }
  977. X                s++;
  978. X            }
  979. X            if ((minlen || tmp) && regtry(prog,s))
  980. X                goto got_it;
  981. X            break;
  982. X        case NBOUND:
  983. X            if (minlen)
  984. X            dontbother++,strend--;
  985. X            if (s != string) {
  986. X            i = s[-1];
  987. X            tmp = isALNUM(i);
  988. X            }
  989. X            else
  990. X            tmp = isALNUM(regprev);    /* assume not alphanumeric */
  991. X            while (s < strend) {
  992. X                i = *s;
  993. X                if (tmp != isALNUM(i))
  994. X                    tmp = !tmp;
  995. X                else if (regtry(prog, s))
  996. X                    goto got_it;
  997. X                s++;
  998. X            }
  999. X            if ((minlen || !tmp) && regtry(prog,s))
  1000. X                goto got_it;
  1001. X            break;
  1002. X        case ALNUM:
  1003. X            while (s < strend) {
  1004. X                i = *s;
  1005. X                if (isALNUM(i)) {
  1006. X                    if (tmp && regtry(prog, s))
  1007. X                        goto got_it;
  1008. X                    else
  1009. X                        tmp = doevery;
  1010. X                }
  1011. X                else
  1012. X                    tmp = 1;
  1013. X                s++;
  1014. X            }
  1015. X            break;
  1016. X        case NALNUM:
  1017. X            while (s < strend) {
  1018. X                i = *s;
  1019. X                if (!isALNUM(i)) {
  1020. X                    if (tmp && regtry(prog, s))
  1021. X                        goto got_it;
  1022. X                    else
  1023. X                        tmp = doevery;
  1024. X                }
  1025. X                else
  1026. X                    tmp = 1;
  1027. X                s++;
  1028. X            }
  1029. X            break;
  1030. X        case SPACE:
  1031. X            while (s < strend) {
  1032. X                if (isSPACE(*s)) {
  1033. X                    if (tmp && regtry(prog, s))
  1034. X                        goto got_it;
  1035. X                    else
  1036. X                        tmp = doevery;
  1037. X                }
  1038. X                else
  1039. X                    tmp = 1;
  1040. X                s++;
  1041. X            }
  1042. X            break;
  1043. X        case NSPACE:
  1044. X            while (s < strend) {
  1045. X                if (!isSPACE(*s)) {
  1046. X                    if (tmp && regtry(prog, s))
  1047. X                        goto got_it;
  1048. X                    else
  1049. X                        tmp = doevery;
  1050. X                }
  1051. X                else
  1052. X                    tmp = 1;
  1053. X                s++;
  1054. X            }
  1055. X            break;
  1056. X        case DIGIT:
  1057. X            while (s < strend) {
  1058. X                if (isDIGIT(*s)) {
  1059. X                    if (tmp && regtry(prog, s))
  1060. X                        goto got_it;
  1061. X                    else
  1062. X                        tmp = doevery;
  1063. X                }
  1064. X                else
  1065. X                    tmp = 1;
  1066. X                s++;
  1067. X            }
  1068. X            break;
  1069. X        case NDIGIT:
  1070. X            while (s < strend) {
  1071. X                if (!isDIGIT(*s)) {
  1072. X                    if (tmp && regtry(prog, s))
  1073. X                        goto got_it;
  1074. X                    else
  1075. X                        tmp = doevery;
  1076. X                }
  1077. X                else
  1078. X                    tmp = 1;
  1079. X                s++;
  1080. X            }
  1081. X            break;
  1082. X        }
  1083. X    }
  1084. X    else {
  1085. X        if (minlen)
  1086. X            dontbother = minlen - 1;
  1087. X        strend -= dontbother;
  1088. X        /* We don't know much -- general case. */
  1089. X        do {
  1090. X            if (regtry(prog, s))
  1091. X                goto got_it;
  1092. X        } while (s++ < strend);
  1093. X    }
  1094. X
  1095. X    /* Failure. */
  1096. X    goto phooey;
  1097. X
  1098. X    got_it:
  1099. X    if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
  1100. X        strend += dontbother;    /* uncheat */
  1101. X        if (safebase)            /* no need for $digit later */
  1102. X            s = strbeg;
  1103. X        else if (strbeg != prog->subbase) {
  1104. X            i = strend - string + (stringarg - strbeg);
  1105. X            s = nsavestr(strbeg,i);    /* so $digit will work later */
  1106. X            if (prog->subbase)
  1107. X                Safefree(prog->subbase);
  1108. X            prog->subbase = s;
  1109. X            prog->subend = s+i;
  1110. X        }
  1111. X        else
  1112. X            s = prog->subbase;
  1113. X        s += (stringarg - strbeg);
  1114. X        for (i = 0; i <= prog->nparens; i++) {
  1115. X            if (prog->endp[i]) {
  1116. X                prog->startp[i] = s + (prog->startp[i] - string);
  1117. X                prog->endp[i] = s + (prog->endp[i] - string);
  1118. X            }
  1119. X        }
  1120. X        if (prog->do_folding)
  1121. X            Safefree(string);
  1122. X    }
  1123. X    return(1);
  1124. X
  1125. X    phooey:
  1126. X    if (prog->do_folding)
  1127. X        Safefree(string);
  1128. X    return(0);
  1129. X}
  1130. X
  1131. X/*
  1132. X - regtry - try match at specific point
  1133. X */
  1134. Xstatic int            /* 0 failure, 1 success */
  1135. Xregtry(prog, string)
  1136. Xregexp *prog;
  1137. Xchar *string;
  1138. X{
  1139. X    register int i;
  1140. X    register char **sp;
  1141. X    register char **ep;
  1142. X
  1143. X    reginput = string;
  1144. X    regstartp = prog->startp;
  1145. X    regendp = prog->endp;
  1146. X    reglastparen = &prog->lastparen;
  1147. X    prog->lastparen = 0;
  1148. X
  1149. X    sp = prog->startp;
  1150. X    ep = prog->endp;
  1151. X    if (prog->nparens) {
  1152. X        for (i = prog->nparens; i >= 0; i--) {
  1153. X            *sp++ = NULL;
  1154. X            *ep++ = NULL;
  1155. X        }
  1156. X    }
  1157. X    if (regmatch(prog->program + 1) && reginput >= regtill) {
  1158. X        prog->startp[0] = string;
  1159. X        prog->endp[0] = reginput;
  1160. X        return(1);
  1161. X    } else
  1162. X        return(0);
  1163. X}
  1164. X
  1165. X/*
  1166. X - regmatch - main matching routine
  1167. X *
  1168. X * Conceptually the strategy is simple:  check to see whether the current
  1169. X * node matches, call self recursively to see whether the rest matches,
  1170. X * and then act accordingly.  In practice we make some effort to avoid
  1171. X * recursion, in particular by going through "ordinary" nodes (that don't
  1172. X * need to know whether the rest of the match failed) by a loop instead of
  1173. X * by recursion.
  1174. X */
  1175. X/* [lwall] I've hoisted the register declarations to the outer block in order to
  1176. X * maybe save a little bit of pushing and popping on the stack.  It also takes
  1177. X * advantage of machines that use a register save mask on subroutine entry.
  1178. X */
  1179. Xstatic int            /* 0 failure, 1 success */
  1180. Xregmatch(prog)
  1181. Xchar *prog;
  1182. X{
  1183. X    register char *scan;    /* Current node. */
  1184. X    char *next;        /* Next node. */
  1185. X    register int nextchar;
  1186. X    register int n;        /* no or next */
  1187. X    register int ln;        /* len or last */
  1188. X    register char *s;    /* operand or save */
  1189. X    register char *locinput = reginput;
  1190. X
  1191. X    nextchar = *locinput;
  1192. X    scan = prog;
  1193. X#ifdef DEBUGGING
  1194. X    if (scan != NULL && regnarrate)
  1195. X        fprintf(stderr, "%s(\n", regprop(scan));
  1196. X#endif
  1197. X    while (scan != NULL) {
  1198. X#ifdef DEBUGGING
  1199. X        if (regnarrate)
  1200. X            fprintf(stderr, "%s...\n", regprop(scan));
  1201. X#endif
  1202. X
  1203. X#ifdef REGALIGN
  1204. X        next = scan + NEXT(scan);
  1205. X        if (next == scan)
  1206. X            next = NULL;
  1207. X#else
  1208. X        next = regnext(scan);
  1209. X#endif
  1210. X
  1211. X        switch (OP(scan)) {
  1212. X        case BOL:
  1213. X            if (locinput == regbol ? regprev == '\n' :
  1214. X                ((nextchar || locinput < regeol) &&
  1215. X                  locinput[-1] == '\n') )
  1216. X            {
  1217. X                /* regtill = regbol; */
  1218. X                break;
  1219. X            }
  1220. X            return(0);
  1221. X        case EOL:
  1222. X            if ((nextchar || locinput < regeol) && nextchar != '\n')
  1223. X                return(0);
  1224. X            if (!multiline && regeol - locinput > 1)
  1225. X                return 0;
  1226. X            /* regtill = regbol; */
  1227. X            break;
  1228. X        case ANY:
  1229. X            if ((nextchar == '\0' && locinput >= regeol) ||
  1230. X              nextchar == '\n')
  1231. X                return(0);
  1232. X            nextchar = *++locinput;
  1233. X            break;
  1234. X        case EXACTLY:
  1235. X            s = OPERAND(scan);
  1236. X            ln = *s++;
  1237. X            /* Inline the first character, for speed. */
  1238. X            if (*s != nextchar)
  1239. X                return(0);
  1240. X            if (regeol - locinput < ln)
  1241. X                return 0;
  1242. X            if (ln > 1 && bcmp(s, locinput, ln) != 0)
  1243. X                return(0);
  1244. X            locinput += ln;
  1245. X            nextchar = *locinput;
  1246. X            break;
  1247. X        case ANYOF:
  1248. X            s = OPERAND(scan);
  1249. X            if (nextchar < 0)
  1250. X                nextchar = UCHARAT(locinput);
  1251. X            if (s[nextchar >> 3] & (1 << (nextchar&7)))
  1252. X                return(0);
  1253. X            if (!nextchar && locinput >= regeol)
  1254. X                return 0;
  1255. X            nextchar = *++locinput;
  1256. X            break;
  1257. X        case ALNUM:
  1258. X            if (!nextchar)
  1259. X                return(0);
  1260. X            if (!isALNUM(nextchar))
  1261. X                return(0);
  1262. X            nextchar = *++locinput;
  1263. X            break;
  1264. X        case NALNUM:
  1265. X            if (!nextchar && locinput >= regeol)
  1266. X                return(0);
  1267. X            if (isALNUM(nextchar))
  1268. X                return(0);
  1269. X            nextchar = *++locinput;
  1270. X            break;
  1271. X        case NBOUND:
  1272. X        case BOUND:
  1273. X            if (locinput == regbol)    /* was last char in word? */
  1274. X                ln = isALNUM(regprev);
  1275. X            else 
  1276. X                ln = isALNUM(locinput[-1]);
  1277. X            n = isALNUM(nextchar); /* is next char in word? */
  1278. X            if ((ln == n) == (OP(scan) == BOUND))
  1279. X                return(0);
  1280. X            break;
  1281. X        case SPACE:
  1282. X            if (!nextchar && locinput >= regeol)
  1283. X                return(0);
  1284. X            if (!isSPACE(nextchar))
  1285. X                return(0);
  1286. X            nextchar = *++locinput;
  1287. X            break;
  1288. X        case NSPACE:
  1289. X            if (!nextchar)
  1290. X                return(0);
  1291. X            if (isSPACE(nextchar))
  1292. X                return(0);
  1293. X            nextchar = *++locinput;
  1294. X            break;
  1295. X        case DIGIT:
  1296. X            if (!isDIGIT(nextchar))
  1297. X                return(0);
  1298. X            nextchar = *++locinput;
  1299. X            break;
  1300. X        case NDIGIT:
  1301. X            if (!nextchar && locinput >= regeol)
  1302. X                return(0);
  1303. X            if (isDIGIT(nextchar))
  1304. X                return(0);
  1305. X            nextchar = *++locinput;
  1306. X            break;
  1307. X        case REF:
  1308. X            n = ARG1(scan);  /* which paren pair */
  1309. X            s = regmystartp[n];
  1310. X            if (!s)
  1311. X                return(0);
  1312. X            if (!regmyendp[n])
  1313. X                return(0);
  1314. X            if (s == regmyendp[n])
  1315. X                break;
  1316. X            /* Inline the first character, for speed. */
  1317. X            if (*s != nextchar)
  1318. X                return(0);
  1319. X            ln = regmyendp[n] - s;
  1320. X            if (locinput + ln > regeol)
  1321. X                return 0;
  1322. X            if (ln > 1 && bcmp(s, locinput, ln) != 0)
  1323. X                return(0);
  1324. X            locinput += ln;
  1325. X            nextchar = *locinput;
  1326. X            break;
  1327. X
  1328. X        case NOTHING:
  1329. X            break;
  1330. X        case BACK:
  1331. X            break;
  1332. X        case OPEN:
  1333. X            n = ARG1(scan);  /* which paren pair */
  1334. X            reginput = locinput;
  1335. X
  1336. X            regmystartp[n] = locinput;    /* for REF */
  1337. X            if (regmatch(next)) {
  1338. X                /*
  1339. X                 * Don't set startp if some later
  1340. X                 * invocation of the same parentheses
  1341. X                 * already has.
  1342. X                 */
  1343. X                if (regstartp[n] == NULL)
  1344. X                    regstartp[n] = locinput;
  1345. X                return(1);
  1346. X            } else
  1347. X                return(0);
  1348. X            /* NOTREACHED */
  1349. X        case CLOSE: {
  1350. X                n = ARG1(scan);  /* which paren pair */
  1351. X                reginput = locinput;
  1352. X
  1353. X                regmyendp[n] = locinput;    /* for REF */
  1354. X                if (regmatch(next)) {
  1355. X                    /*
  1356. X                     * Don't set endp if some later
  1357. X                     * invocation of the same parentheses
  1358. X                     * already has.
  1359. X                     */
  1360. X                    if (regendp[n] == NULL) {
  1361. X                        regendp[n] = locinput;
  1362. X                        if (n > *reglastparen)
  1363. X                            *reglastparen = n;
  1364. X                    }
  1365. X                    return(1);
  1366. X                } else
  1367. X                    return(0);
  1368. X            }
  1369. X            /*NOTREACHED*/
  1370. X        case BRANCH: {
  1371. X                if (OP(next) != BRANCH)        /* No choice. */
  1372. X                    next = NEXTOPER(scan);    /* Avoid recursion. */
  1373. X                else {
  1374. X                    do {
  1375. X                        reginput = locinput;
  1376. X                        if (regmatch(NEXTOPER(scan)))
  1377. X                            return(1);
  1378. X#ifdef REGALIGN
  1379. X                        if (n = NEXT(scan))
  1380. X                            scan += n;
  1381. X                        else
  1382. X                            scan = NULL;
  1383. X#else
  1384. X                        scan = regnext(scan);
  1385. X#endif
  1386. X                    } while (scan != NULL && OP(scan) == BRANCH);
  1387. X                    return(0);
  1388. X                    /* NOTREACHED */
  1389. X                }
  1390. X            }
  1391. X            break;
  1392. X        case CURLY:
  1393. X            ln = ARG1(scan);  /* min to match */
  1394. X            n  = ARG2(scan);  /* max to match */
  1395. X            scan = NEXTOPER(scan) + 4;
  1396. X            goto repeat;
  1397. X        case STAR:
  1398. X            ln = 0;
  1399. X            n = 0;
  1400. X            scan = NEXTOPER(scan);
  1401. X            goto repeat;
  1402. X        case PLUS:
  1403. X            /*
  1404. X             * Lookahead to avoid useless match attempts
  1405. X             * when we know what character comes next.
  1406. X             */
  1407. X            ln = 1;
  1408. X            n = 0;
  1409. X            scan = NEXTOPER(scan);
  1410. X            repeat:
  1411. X            if (OP(next) == EXACTLY)
  1412. X                nextchar = *(OPERAND(next)+1);
  1413. X            else
  1414. X                nextchar = -1000;
  1415. X            reginput = locinput;
  1416. X            n = regrepeat(scan, n);
  1417. X            if (!multiline && OP(next) == EOL && ln < n)
  1418. X                ln = n;            /* why back off? */
  1419. X            while (n >= ln) {
  1420. X                /* If it could work, try it. */
  1421. X                if (nextchar == -1000 || *reginput == nextchar)
  1422. X                    if (regmatch(next))
  1423. X                        return(1);
  1424. X                /* Couldn't or didn't -- back up. */
  1425. X                n--;
  1426. X                reginput = locinput + n;
  1427. X            }
  1428. X            return(0);
  1429. X        case END:
  1430. X            reginput = locinput; /* put where regtry can find it */
  1431. X            return(1);    /* Success! */
  1432. X        default:
  1433. X            printf("%x %d\n",scan,scan[1]);
  1434. X            FAIL("regexp memory corruption");
  1435. X        }
  1436. X
  1437. X        scan = next;
  1438. X    }
  1439. X
  1440. X    /*
  1441. X     * We get here only if there's trouble -- normally "case END" is
  1442. X     * the terminating point.
  1443. X     */
  1444. X    FAIL("corrupted regexp pointers");
  1445. X    /*NOTREACHED*/
  1446. X#ifdef lint
  1447. X    return 0;
  1448. X#endif
  1449. X}
  1450. X
  1451. X/*
  1452. X - regrepeat - repeatedly match something simple, report how many
  1453. X */
  1454. X/*
  1455. X * [This routine now assumes that it will only match on things of length 1.
  1456. X * That was true before, but now we assume scan - reginput is the count,
  1457. X * rather than incrementing count on every character.]
  1458. X */
  1459. Xstatic int
  1460. Xregrepeat(p, max)
  1461. Xchar *p;
  1462. Xint max;
  1463. X{
  1464. X    register char *scan;
  1465. X    register char *opnd;
  1466. X    register int c;
  1467. X    register char *loceol = regeol;
  1468. X
  1469. X    scan = reginput;
  1470. X    if (max && max < loceol - scan)
  1471. X        loceol = scan + max;
  1472. X    opnd = OPERAND(p);
  1473. X    switch (OP(p)) {
  1474. X    case ANY:
  1475. X        while (scan < loceol && *scan != '\n')
  1476. X            scan++;
  1477. X        break;
  1478. X    case EXACTLY:        /* length of string is 1 */
  1479. X        opnd++;
  1480. X        while (scan < loceol && *opnd == *scan)
  1481. X            scan++;
  1482. X        break;
  1483. X    case ANYOF:
  1484. X        c = UCHARAT(scan);
  1485. X        while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
  1486. X            scan++;
  1487. X            c = UCHARAT(scan);
  1488. X        }
  1489. X        break;
  1490. X    case ALNUM:
  1491. X        while (scan < loceol && isALNUM(*scan))
  1492. X            scan++;
  1493. X        break;
  1494. X    case NALNUM:
  1495. X        while (scan < loceol && !isALNUM(*scan))
  1496. X            scan++;
  1497. X        break;
  1498. X    case SPACE:
  1499. X        while (scan < loceol && isSPACE(*scan))
  1500. X            scan++;
  1501. X        break;
  1502. X    case NSPACE:
  1503. X        while (scan < loceol && !isSPACE(*scan))
  1504. X            scan++;
  1505. X        break;
  1506. X    case DIGIT:
  1507. X        while (scan < loceol && isDIGIT(*scan))
  1508. X            scan++;
  1509. X        break;
  1510. X    case NDIGIT:
  1511. X        while (scan < loceol && !isDIGIT(*scan))
  1512. X            scan++;
  1513. X        break;
  1514. X    default:        /* Oh dear.  Called inappropriately. */
  1515. X        FAIL("internal regexp foulup");
  1516. X        /* NOTREACHED */
  1517. X    }
  1518. X
  1519. X    c = scan - reginput;
  1520. X    reginput = scan;
  1521. X
  1522. X    return(c);
  1523. X}
  1524. X
  1525. X/*
  1526. X - regnext - dig the "next" pointer out of a node
  1527. X *
  1528. X * [Note, when REGALIGN is defined there are two places in regmatch()
  1529. X * that bypass this code for speed.]
  1530. X */
  1531. Xchar *
  1532. Xregnext(p)
  1533. Xregister char *p;
  1534. X{
  1535. X    register int offset;
  1536. X
  1537. X    if (p == ®dummy)
  1538. X        return(NULL);
  1539. X
  1540. X    offset = NEXT(p);
  1541. X    if (offset == 0)
  1542. X        return(NULL);
  1543. X
  1544. X#ifdef REGALIGN
  1545. X    return(p+offset);
  1546. X#else
  1547. X    if (OP(p) == BACK)
  1548. X        return(p-offset);
  1549. X    else
  1550. X        return(p+offset);
  1551. X#endif
  1552. X}
  1553. !STUFFY!FUNK!
  1554. echo Extracting t/op/read.t
  1555. sed >t/op/read.t <<'!STUFFY!FUNK!' -e 's/X//'
  1556. X#!./perl
  1557. X
  1558. X# $Header: read.t,v 4.0 91/03/20 01:54:16 lwall Locked $
  1559. X
  1560. Xprint "1..4\n";
  1561. X
  1562. X
  1563. Xopen(FOO,'op/read.t') || open(FOO,'t/op/read.t') || die "Can't open op.read";
  1564. Xseek(FOO,4,0);
  1565. X$got = read(FOO,$buf,4);
  1566. X
  1567. Xprint ($got == 4 ? "ok 1\n" : "not ok 1\n");
  1568. Xprint ($buf eq "perl" ? "ok 2\n" : "not ok 2 :$buf:\n");
  1569. X
  1570. Xseek(FOO,20000,0);
  1571. X$got = read(FOO,$buf,4);
  1572. X
  1573. Xprint ($got == 0 ? "ok 3\n" : "not ok 3\n");
  1574. Xprint ($buf eq "" ? "ok 4\n" : "not ok 4\n");
  1575. !STUFFY!FUNK!
  1576. echo " "
  1577. echo "End of kit 21 (of 36)"
  1578. cat /dev/null >kit21isdone
  1579. run=''
  1580. config=''
  1581. for iskit in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36; do
  1582.     if test -f kit${iskit}isdone; then
  1583.     run="$run $iskit"
  1584.     else
  1585.     todo="$todo $iskit"
  1586.     fi
  1587. done
  1588. case $todo in
  1589.     '')
  1590.     echo "You have run all your kits.  Please read README and then type Configure."
  1591.     for combo in *:AA; do
  1592.         if test -f "$combo"; then
  1593.         realfile=`basename $combo :AA`
  1594.         cat $realfile:[A-Z][A-Z] >$realfile
  1595.         rm -rf $realfile:[A-Z][A-Z]
  1596.         fi
  1597.     done
  1598.     rm -rf kit*isdone
  1599.     chmod 755 Configure
  1600.     ;;
  1601.     *)  echo "You have run$run."
  1602.     echo "You still need to run$todo."
  1603.     ;;
  1604. esac
  1605. : Someone might mail this, so...
  1606. exit
  1607.  
  1608. exit 0 # Just in case...
  1609. -- 
  1610. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1611. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1612. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1613. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1614.