rework initial error handling -- since we check for any screens on
[nvi.git] / ex / ex_subst.c
blobed18ec597bb58db46b4a439f0659d40878a9b6a5
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: ex_subst.c,v 8.33 1994/01/09 17:56:14 bostic Exp $ (Berkeley) $Date: 1994/01/09 17:56:14 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
20 #include "vi.h"
21 #include "excmd.h"
22 #include "interrupt.h"
24 #define SUB_FIRST 0x01 /* The 'r' flag isn't reasonable. */
25 #define SUB_MUSTSETR 0x02 /* The 'r' flag is required. */
27 static int checkmatchsize __P((SCR *, regex_t *));
28 static inline int regsub __P((SCR *,
29 char *, char **, size_t *, size_t *));
30 static void subst_intr __P((int));
31 static int substitute __P((SCR *, EXF *,
32 EXCMDARG *, char *, regex_t *, u_int));
35 * ex_substitute --
36 * [line [,line]] s[ubstitute] [[/;]pat[/;]/repl[/;] [cgr] [count] [#lp]]
38 * Substitute on lines matching a pattern.
40 int
41 ex_substitute(sp, ep, cmdp)
42 SCR *sp;
43 EXF *ep;
44 EXCMDARG *cmdp;
46 regex_t *re, lre;
47 size_t blen, len;
48 u_int flags;
49 int delim, eval, reflags, replaced;
50 char *bp, *ptrn, *rep, *p, *t;
53 * Skip leading white space.
55 * !!!
56 * Historic vi allowed any non-alphanumeric to serve as the
57 * substitution command delimiter.
59 * !!!
60 * If the arguments are empty, it's the same as &, i.e. we
61 * repeat the last substitution.
63 for (p = cmdp->argv[0]->bp,
64 len = cmdp->argv[0]->len; len > 0; --len, ++p) {
65 if (!isblank(*p))
66 break;
68 if (len == 0)
69 return (ex_subagain(sp, ep, cmdp));
70 delim = *p++;
71 if (isalnum(delim))
72 return (substitute(sp, ep,
73 cmdp, p, &sp->subre, SUB_MUSTSETR));
76 * Get the pattern string, toss escaped characters.
78 * !!!
79 * Historic vi accepted any of the following forms:
81 * :s/abc/def/ change "abc" to "def"
82 * :s/abc/def change "abc" to "def"
83 * :s/abc/ delete "abc"
84 * :s/abc delete "abc"
86 * QUOTING NOTE:
88 * Only toss an escape character if it escapes a delimiter.
89 * This means that "s/A/\\\\f" replaces "A" with "\\f". It
90 * would be nice to be more regular, i.e. for each layer of
91 * escaping a single escape character is removed, but that's
92 * not how the historic vi worked.
94 for (ptrn = t = p;;) {
95 if (p[0] == '\0' || p[0] == delim) {
96 if (p[0] == delim)
97 ++p;
99 * !!!
100 * Nul terminate the pattern string -- it's passed
101 * to regcomp which doesn't understand anything else.
103 *t = '\0';
104 break;
106 if (p[0] == '\\' && p[1] == delim)
107 ++p;
108 *t++ = *p++;
111 /* If the pattern string is empty, use the last one. */
112 if (*ptrn == NULL) {
113 if (!F_ISSET(sp, S_SUBRE_SET)) {
114 msgq(sp, M_ERR,
115 "No previous regular expression.");
116 return (1);
118 re = &sp->subre;
119 flags = 0;
120 } else {
121 /* Set RE flags. */
122 reflags = 0;
123 if (O_ISSET(sp, O_EXTENDED))
124 reflags |= REG_EXTENDED;
125 if (O_ISSET(sp, O_IGNORECASE))
126 reflags |= REG_ICASE;
128 /* Convert vi-style RE's to POSIX 1003.2 RE's. */
129 if (re_conv(sp, &ptrn, &replaced))
130 return (1);
132 /* Compile the RE. */
133 eval = regcomp(&lre, (char *)ptrn, reflags);
135 /* Free up any allocated memory. */
136 if (replaced)
137 FREE_SPACE(sp, ptrn, 0);
139 if (eval) {
140 re_error(sp, eval, &lre);
141 return (1);
145 * Set saved RE.
147 * !!!
148 * Historic practice is that substitutes set the search
149 * direction as well as both substitute and search RE's.
151 sp->searchdir = FORWARD;
152 sp->sre = lre;
153 F_SET(sp, S_SRE_SET);
154 sp->subre = lre;
155 F_SET(sp, S_SUBRE_SET);
157 re = &lre;
158 flags = SUB_FIRST;
162 * Get the replacement string.
164 * The special character ~ (\~ if O_MAGIC not set) inserts the
165 * previous replacement string into this replacement string.
167 * The special character & (\& if O_MAGIC not set) matches the
168 * entire RE. No handling of & is required here, it's done by
169 * regsub().
171 * QUOTING NOTE:
173 * Only toss an escape character if it escapes a delimiter or
174 * an escape character, or if O_MAGIC is set and it escapes a
175 * tilde.
177 if (*p == '\0') {
178 if (sp->repl != NULL)
179 FREE(sp->repl, sp->repl_len);
180 sp->repl = NULL;
181 sp->repl_len = 0;
182 } else {
184 * Count ~'s to figure out how much space we need. We could
185 * special case nonexistent last patterns or whether or not
186 * O_MAGIC is set, but it's probably not worth the effort.
188 for (rep = p, len = 0;
189 p[0] != '\0' && p[0] != delim; ++p, ++len)
190 if (p[0] == '~')
191 len += sp->repl_len;
192 GET_SPACE_RET(sp, bp, blen, len);
193 for (t = bp, len = 0, p = rep;;) {
194 if (p[0] == '\0' || p[0] == delim) {
195 if (p[0] == delim)
196 ++p;
197 break;
199 if (p[0] == '\\') {
200 if (p[1] == '\\' || p[1] == delim)
201 ++p;
202 else if (p[1] == '~') {
203 ++p;
204 if (!O_ISSET(sp, O_MAGIC))
205 goto tilde;
207 } else if (p[0] == '~' && O_ISSET(sp, O_MAGIC)) {
208 tilde: ++p;
209 memmove(t, sp->repl, sp->repl_len);
210 t += sp->repl_len;
211 len += sp->repl_len;
212 continue;
214 *t++ = *p++;
215 ++len;
217 if (sp->repl != NULL)
218 FREE(sp->repl, sp->repl_len);
219 if ((sp->repl = malloc(len)) == NULL) {
220 msgq(sp, M_SYSERR, NULL);
221 FREE_SPACE(sp, bp, blen);
222 return (1);
224 memmove(sp->repl, bp, len);
225 sp->repl_len = len;
226 FREE_SPACE(sp, bp, blen);
229 if (checkmatchsize(sp, &sp->subre))
230 return (1);
231 return (substitute(sp, ep, cmdp, p, re, flags));
235 * ex_subagain --
236 * [line [,line]] & [cgr] [count] [#lp]]
238 * Substitute using the last substitute RE and replacement pattern.
241 ex_subagain(sp, ep, cmdp)
242 SCR *sp;
243 EXF *ep;
244 EXCMDARG *cmdp;
246 if (!F_ISSET(sp, S_SUBRE_SET)) {
247 msgq(sp, M_ERR, "No previous regular expression.");
248 return (1);
250 return (substitute(sp, ep, cmdp, cmdp->argv[0]->bp, &sp->subre, 0));
254 * ex_subtilde --
255 * [line [,line]] ~ [cgr] [count] [#lp]]
257 * Substitute using the last RE and last substitute replacement pattern.
260 ex_subtilde(sp, ep, cmdp)
261 SCR *sp;
262 EXF *ep;
263 EXCMDARG *cmdp;
265 if (!F_ISSET(sp, S_SRE_SET)) {
266 msgq(sp, M_ERR, "No previous regular expression.");
267 return (1);
269 return (substitute(sp, ep, cmdp, cmdp->argv[0]->bp, &sp->sre, 0));
273 * The nasty part of the substitution is what happens when the replacement
274 * string contains newlines. It's a bit tricky -- consider the information
275 * that has to be retained for "s/f\(o\)o/^M\1^M\1/". The solution here is
276 * to build a set of newline offsets which we use to break the line up later,
277 * when the replacement is done. Don't change it unless you're pretty damned
278 * confident.
280 #define NEEDNEWLINE(sp) { \
281 if (sp->newl_len == sp->newl_cnt) { \
282 sp->newl_len += 25; \
283 REALLOC(sp, sp->newl, size_t *, \
284 sp->newl_len * sizeof(size_t)); \
285 if (sp->newl == NULL) { \
286 sp->newl_len = 0; \
287 return (1); \
292 #define BUILD(sp, l, len) { \
293 if (lbclen + (len) > lblen) { \
294 lblen += MAX(lbclen + (len), 256); \
295 REALLOC(sp, lb, char *, lblen); \
296 if (lb == NULL) { \
297 lbclen = 0; \
298 return (1); \
301 memmove(lb + lbclen, l, len); \
302 lbclen += len; \
305 #define NEEDSP(sp, len, pnt) { \
306 if (lbclen + (len) > lblen) { \
307 lblen += MAX(lbclen + (len), 256); \
308 REALLOC(sp, lb, char *, lblen); \
309 if (lb == NULL) { \
310 lbclen = 0; \
311 return (1); \
313 pnt = lb + lbclen; \
318 * substitute --
319 * Do the substitution. This stuff is *really* tricky. There are
320 * lots of special cases, and general nastiness. Don't mess with it
321 * unless you're pretty confident.
323 static int
324 substitute(sp, ep, cmdp, s, re, flags)
325 SCR *sp;
326 EXF *ep;
327 EXCMDARG *cmdp;
328 char *s;
329 regex_t *re;
330 u_int flags;
332 DECLARE_INTERRUPTS;
333 MARK from, to;
334 recno_t elno, lno;
335 size_t blen, cnt, last, lbclen, lblen, len, llen, offset, saved_offset;
336 int didsub, do_eol_match, eflags, empty_ok, eval;
337 int linechanged, matched, quit, rval;
338 int cflag, gflag, lflag, nflag, pflag, rflag;
339 char *bp, *lb;
342 * Historic vi permitted the '#', 'l' and 'p' options in vi mode, but
343 * it only displayed the last change. I'd disallow them, but they are
344 * useful in combination with the [v]global commands. In the current
345 * model the problem is combining them with the 'c' flag -- the screen
346 * would have to flip back and forth between the confirm screen and the
347 * ex print screen, which would be pretty awful. We do display all
348 * changes, though, for what that's worth.
350 * !!!
351 * Historic vi was fairly strict about the order of "options", the
352 * count, and "flags". I'm somewhat fuzzy on the difference between
353 * options and flags, anyway, so this is a simpler approach, and we
354 * just take it them in whatever order the user gives them. (The ex
355 * usage statement doesn't reflect this.)
357 cflag = gflag = lflag = nflag = pflag = rflag = 0;
358 for (lno = OOBLNO; *s != '\0'; ++s)
359 switch (*s) {
360 case ' ':
361 case '\t':
362 break;
363 case '0': case '1': case '2': case '3': case '4':
364 case '5': case '6': case '7': case '8': case '9':
365 if (lno != OOBLNO)
366 goto usage;
367 errno = 0;
368 lno = strtoul(s, &s, 10);
369 if (*s == '\0') /* Loop increment correction. */
370 --s;
371 if (errno == ERANGE) {
372 if (lno == LONG_MAX)
373 msgq(sp, M_ERR, "Count overflow.");
374 else if (lno == LONG_MIN)
375 msgq(sp, M_ERR, "Count underflow.");
376 else
377 msgq(sp, M_SYSERR, NULL);
378 return (1);
381 * In historic vi, the count was inclusive from the
382 * second address.
384 cmdp->addr1.lno = cmdp->addr2.lno;
385 cmdp->addr2.lno += lno - 1;
386 break;
387 case '#':
388 nflag = 1;
389 break;
390 case 'c':
391 cflag = 1;
392 break;
393 case 'g':
394 gflag = 1;
395 break;
396 case 'l':
397 lflag = 1;
398 break;
399 case 'p':
400 pflag = 1;
401 break;
402 case 'r':
403 if (LF_ISSET(SUB_FIRST)) {
404 msgq(sp, M_ERR,
405 "Regular expression specified; r flag meaningless.");
406 return (1);
408 if (!F_ISSET(sp, S_SUBRE_SET)) {
409 msgq(sp, M_ERR,
410 "No previous regular expression.");
411 return (1);
413 rflag = 1;
414 break;
415 default:
416 goto usage;
419 if (*s != '\0' || !rflag && LF_ISSET(SUB_MUSTSETR)) {
420 usage: msgq(sp, M_ERR, "Usage: %s", cmdp->cmd->usage);
421 return (1);
424 if (IN_VI_MODE(sp) && cflag && (lflag || nflag || pflag)) {
425 msgq(sp, M_ERR,
426 "The #, l and p flags may not be combined with the c flag in vi mode.");
427 return (1);
430 if (!F_ISSET(sp, S_GLOBAL))
431 SET_UP_INTERRUPTS(subst_intr);
434 * bp: if interactive, line cache
435 * blen: if interactive, line cache length
436 * lb: build buffer pointer.
437 * lbclen: current length of built buffer.
438 * lblen; length of build buffer.
440 bp = lb = NULL;
441 blen = lbclen = lblen = 0;
443 /* For each line... */
444 for (matched = quit = 0, lno = cmdp->addr1.lno,
445 elno = cmdp->addr2.lno; !quit && lno <= elno; ++lno) {
447 /* Someone's unhappy, time to stop. */
448 if (F_ISSET(sp, S_INTERRUPTED)) {
449 if (!F_ISSET(sp, S_GLOBAL))
450 msgq(sp, M_INFO, "Interrupted.");
451 break;
454 /* Get the line. */
455 if ((s = file_gline(sp, ep, lno, &llen)) == NULL) {
456 GETLINE_ERR(sp, lno);
457 return (1);
461 * Make a local copy if doing confirmation -- when calling
462 * the confirm routine we're likely to lose the cached copy.
464 if (cflag) {
465 if (bp == NULL) {
466 GET_SPACE_RET(sp, bp, blen, llen);
467 } else
468 ADD_SPACE_RET(sp, bp, blen, llen);
469 memmove(bp, s, llen);
470 s = bp;
473 /* Start searching from the beginning. */
474 offset = 0;
475 len = llen;
477 /* Reset the build buffer offset. */
478 lbclen = 0;
480 /* Reset empty match flag. */
481 empty_ok = 1;
484 * We don't want to have to do a setline if the line didn't
485 * change -- keep track of whether or not this line changed.
486 * If doing confirmations, don't want to keep setting the
487 * line if change is refused -- keep track of substitutions.
489 didsub = linechanged = 0;
491 /* New line, do an EOL match. */
492 do_eol_match = 1;
494 /* It's not nul terminated, but we pretend it is. */
495 eflags = REG_STARTEND;
498 * The search area is from s + offset to the EOL.
500 * Generally, sp->match[0].rm_so is the offset of the start
501 * of the match from the start of the search, and offset is
502 * the offset of the start of the last search.
504 nextmatch: sp->match[0].rm_so = 0;
505 sp->match[0].rm_eo = len;
507 /* Get the next match. */
508 eval = regexec(re,
509 (char *)s + offset, re->re_nsub + 1, sp->match, eflags);
512 * There wasn't a match or if there was an error, deal with
513 * it. If there was a previous match in this line, resolve
514 * the changes into the database. Otherwise, just move on.
516 if (eval == REG_NOMATCH)
517 goto endmatch;
518 if (eval != 0) {
519 re_error(sp, eval, re);
520 goto ret1;
522 matched = 1;
524 /* Only the first search can match an anchored expression. */
525 eflags |= REG_NOTBOL;
528 * !!!
529 * It's possible to match 0-length strings -- for example, the
530 * command s;a*;X;, when matched against the string "aabb" will
531 * result in "XbXbX", i.e. the matches are "aa", the space
532 * between the b's and the space between the b's and the end of
533 * the string. There is a similar space between the beginning
534 * of the string and the a's. The rule that we use (because vi
535 * historically used it) is that any 0-length match, occurring
536 * immediately after a match, is ignored. Otherwise, the above
537 * example would have resulted in "XXbXbX". Another example is
538 * incorrectly using " *" to replace groups of spaces with one
539 * space.
541 * The way we do this is that if we just had a successful match,
542 * the starting offset does not skip characters, and the match
543 * is empty, ignore the match and move forward. If there's no
544 * more characters in the string, we were attempting to match
545 * after the last character, so quit.
547 if (!empty_ok &&
548 sp->match[0].rm_so == 0 && sp->match[0].rm_eo == 0) {
549 empty_ok = 1;
550 if (len == 0)
551 goto endmatch;
552 BUILD(sp, s + offset, 1)
553 ++offset;
554 --len;
555 goto nextmatch;
558 /* Confirm change. */
559 if (cflag) {
561 * Set the cursor position for confirmation. Note,
562 * if we matched on a '$', the cursor may be past
563 * the end of line.
565 * XXX
566 * We may want to "fix" this in the confirm routine,
567 * if the confirm routine should be able to display
568 * a cursor past EOL.
570 from.lno = to.lno = lno;
571 from.cno = sp->match[0].rm_so + offset;
572 to.cno = sp->match[0].rm_eo;
573 if (llen == 0)
574 from.cno = to.cno = 0;
575 else {
576 if (to.cno >= llen)
577 to.cno = llen - 1;
578 if (from.cno >= llen)
579 from.cno = llen - 1;
581 switch (sp->s_confirm(sp, ep, &from, &to)) {
582 case CONF_YES:
583 break;
584 case CONF_NO:
585 didsub = 0;
586 BUILD(sp, s +offset, sp->match[0].rm_eo);
587 goto skip;
588 case CONF_QUIT:
589 /* Set the quit flag. */
590 quit = 1;
592 /* If interruptible, pass the info back. */
593 if (F_ISSET(sp, S_INTERRUPTIBLE))
594 F_SET(sp, S_INTERRUPTED);
597 * If any changes, resolve them, otherwise
598 * return to the main loop.
600 goto endmatch;
604 /* Copy the bytes before the match into the build buffer. */
605 BUILD(sp, s + offset, sp->match[0].rm_so);
608 * Cursor moves to last line changed, unless doing confirm,
609 * in which case don't move it.
611 * !!!
612 * Historic vi just put the cursor on the first non-blank
613 * of the last line changed. This might be better.
615 if (!cflag) {
616 sp->lno = lno;
617 sp->cno = sp->match[0].rm_so + offset;
620 /* Substitute the matching bytes. */
621 didsub = 1;
622 if (regsub(sp, s + offset, &lb, &lbclen, &lblen))
623 goto ret1;
625 /* Set the change flag so we know this line was modified. */
626 linechanged = 1;
628 /* Move past the matched bytes. */
629 skip: offset += sp->match[0].rm_eo;
630 len -= sp->match[0].rm_eo;
632 /* A match cannot be followed by an empty pattern. */
633 empty_ok = 0;
636 * If doing a global change with confirmation, we have to
637 * update the screen. The basic idea is to store the line
638 * so the screen update routines can find it, and restart.
640 if (didsub && cflag && gflag) {
642 * The new search offset will be the end of the
643 * modified line.
645 saved_offset = lbclen;
647 /* Copy the rest of the line. */
648 if (len)
649 BUILD(sp, s + offset, len)
651 /* Set the new offset. */
652 offset = saved_offset;
654 /* Store inserted lines, adjusting the build buffer. */
655 last = 0;
656 if (sp->newl_cnt) {
657 for (cnt = 0;
658 cnt < sp->newl_cnt; ++cnt, ++lno, ++elno) {
659 if (file_iline(sp, ep, lno,
660 lb + last, sp->newl[cnt] - last))
661 goto ret1;
662 last = sp->newl[cnt] + 1;
663 ++sp->rptlines[L_ADDED];
665 lbclen -= last;
666 offset -= last;
667 sp->newl_cnt = 0;
670 /* Store and retrieve the line. */
671 if (file_sline(sp, ep, lno, lb + last, lbclen))
672 goto ret1;
673 if ((s = file_gline(sp, ep, lno, &llen)) == NULL) {
674 GETLINE_ERR(sp, lno);
675 goto ret1;
677 ADD_SPACE_RET(sp, bp, blen, llen)
678 memmove(bp, s, llen);
679 s = bp;
680 len = llen - offset;
682 /* Restart the build. */
683 lbclen = 0;
684 BUILD(sp, s, offset);
687 * If we haven't already done the after-the-string
688 * match, do one. Set REG_NOTEOL so the '$' pattern
689 * only matches once.
691 if (!do_eol_match)
692 goto endmatch;
693 if (offset == len) {
694 do_eol_match = 0;
695 eflags |= REG_NOTEOL;
697 goto nextmatch;
701 * If it's a global:
703 * If at the end of the string, do a test for the after
704 * the string match. Set REG_NOTEOL so the '$' pattern
705 * only matches once.
707 if (gflag && do_eol_match) {
708 if (len == 0) {
709 do_eol_match = 0;
710 eflags |= REG_NOTEOL;
712 goto nextmatch;
715 endmatch: if (!linechanged)
716 continue;
718 /* Copy any remaining bytes into the build buffer. */
719 if (len)
720 BUILD(sp, s + offset, len)
722 /* Store inserted lines, adjusting the build buffer. */
723 last = 0;
724 if (sp->newl_cnt) {
725 for (cnt = 0;
726 cnt < sp->newl_cnt; ++cnt, ++lno, ++elno) {
727 if (file_iline(sp, ep,
728 lno, lb + last, sp->newl[cnt] - last))
729 goto ret1;
730 last = sp->newl[cnt] + 1;
731 ++sp->rptlines[L_ADDED];
733 lbclen -= last;
734 sp->newl_cnt = 0;
737 /* Store the changed line. */
738 if (file_sline(sp, ep, lno, lb + last, lbclen))
739 goto ret1;
741 /* Update changed line counter. */
742 ++sp->rptlines[L_CHANGED];
744 /* Display as necessary. */
745 if (lflag || nflag || pflag) {
746 from.lno = to.lno = lno;
747 from.cno = to.cno = 0;
748 if (lflag)
749 ex_print(sp, ep, &from, &to, E_F_LIST);
750 if (nflag)
751 ex_print(sp, ep, &from, &to, E_F_HASH);
752 if (pflag)
753 ex_print(sp, ep, &from, &to, E_F_PRINT);
758 * If not in a global command, and nothing matched, say so.
759 * Else, if none of the lines displayed, put something up.
761 if (!matched) {
762 if (!F_ISSET(sp, S_GLOBAL))
763 msgq(sp, M_INFO, "No match found.");
764 } else if (!lflag && !nflag && !pflag)
765 F_SET(EXP(sp), EX_AUTOPRINT);
767 rval = 0;
768 if (0) {
769 ret1: rval = 1;
772 interrupt_err:
773 if (!F_ISSET(sp, S_GLOBAL))
774 TEAR_DOWN_INTERRUPTS;
776 if (bp != NULL)
777 FREE_SPACE(sp, bp, blen);
778 return (rval);
782 * regsub --
783 * Do the substitution for a regular expression.
785 static inline int
786 regsub(sp, ip, lbp, lbclenp, lblenp)
787 SCR *sp;
788 char *ip; /* Input line. */
789 char **lbp;
790 size_t *lbclenp, *lblenp;
792 enum { C_NOTSET, C_LOWER, C_ONELOWER, C_ONEUPPER, C_UPPER } conv;
793 size_t lbclen, lblen; /* Local copies. */
794 size_t mlen; /* Match length. */
795 size_t rpl; /* Remaining replacement length. */
796 char *rp; /* Replacement pointer. */
797 int ch;
798 int no; /* Match replacement offset. */
799 char *p, *t; /* Buffer pointers. */
800 char *lb; /* Local copies. */
802 lb = *lbp; /* Get local copies. */
803 lbclen = *lbclenp;
804 lblen = *lblenp;
807 * QUOTING NOTE:
809 * There are some special sequences that vi provides in the
810 * replacement patterns.
811 * & string the RE matched (\& if nomagic set)
812 * \# n-th regular subexpression
813 * \E end \U, \L conversion
814 * \e end \U, \L conversion
815 * \l convert the next character to lower-case
816 * \L convert to lower-case, until \E, \e, or end of replacement
817 * \u convert the next character to upper-case
818 * \U convert to upper-case, until \E, \e, or end of replacement
820 * Otherwise, since this is the lowest level of replacement, discard
821 * all escape characters. This (hopefully) follows historic practice.
823 #define ADDCH(ch) { \
824 CHAR_T __ch = (ch); \
825 u_int __value = term_key_val(sp, __ch); \
826 if (__value == K_CR || __value == K_NL) { \
827 NEEDNEWLINE(sp); \
828 sp->newl[sp->newl_cnt++] = lbclen; \
829 } else if (conv != C_NOTSET) { \
830 switch (conv) { \
831 case C_ONELOWER: \
832 conv = C_NOTSET; \
833 /* FALLTHROUGH */ \
834 case C_LOWER: \
835 if (isupper(__ch)) \
836 __ch = tolower(__ch); \
837 break; \
838 case C_ONEUPPER: \
839 conv = C_NOTSET; \
840 /* FALLTHROUGH */ \
841 case C_UPPER: \
842 if (islower(__ch)) \
843 __ch = toupper(__ch); \
844 break; \
845 default: \
846 abort(); \
849 NEEDSP(sp, 1, p); \
850 *p++ = __ch; \
851 ++lbclen; \
853 conv = C_NOTSET;
854 for (rp = sp->repl, rpl = sp->repl_len, p = lb + lbclen; rpl--;) {
855 switch (ch = *rp++) {
856 case '&':
857 if (O_ISSET(sp, O_MAGIC)) {
858 no = 0;
859 goto subzero;
861 break;
862 case '\\':
863 if (rpl == 0)
864 break;
865 --rpl;
866 switch (ch = *rp) {
867 case '&':
868 if (!O_ISSET(sp, O_MAGIC)) {
869 ++rp;
870 no = 0;
871 goto subzero;
873 break;
874 case '0': case '1': case '2': case '3': case '4':
875 case '5': case '6': case '7': case '8': case '9':
876 no = *rp++ - '0';
877 subzero: if (sp->match[no].rm_so == -1 ||
878 sp->match[no].rm_eo == -1)
879 continue;
880 mlen =
881 sp->match[no].rm_eo - sp->match[no].rm_so;
882 for (t = ip + sp->match[no].rm_so; mlen--; ++t)
883 ADDCH(*t);
884 continue;
885 case 'e':
886 case 'E':
887 ++rp;
888 conv = C_NOTSET;
889 continue;
890 case 'l':
891 ++rp;
892 conv = C_ONELOWER;
893 continue;
894 case 'L':
895 ++rp;
896 conv = C_LOWER;
897 continue;
898 case 'u':
899 ++rp;
900 conv = C_ONEUPPER;
901 continue;
902 case 'U':
903 ++rp;
904 conv = C_UPPER;
905 continue;
906 default:
907 ++rp;
908 break;
911 ADDCH(ch);
914 *lbp = lb; /* Update caller's information. */
915 *lbclenp = lbclen;
916 *lblenp = lblen;
917 return (0);
920 static int
921 checkmatchsize(sp, re)
922 SCR *sp;
923 regex_t *re;
925 /* Build nsub array as necessary. */
926 if (sp->matchsize < re->re_nsub + 1) {
927 sp->matchsize = re->re_nsub + 1;
928 REALLOC(sp, sp->match,
929 regmatch_t *, sp->matchsize * sizeof(regmatch_t));
930 if (sp->match == NULL) {
931 sp->matchsize = 0;
932 return (1);
935 return (0);
939 * subst_intr --
940 * Set the interrupt bit in any screen that is interruptible.
942 * XXX
943 * In the future this may be a problem. The user should be able to move to
944 * another screen and keep typing while this runs. If so, and the user has
945 * more than one substitute running, it will be hard to decide which one to
946 * stop.
948 static void
949 subst_intr(signo)
950 int signo;
952 SCR *sp;
954 for (sp = __global_list->dq.cqh_first;
955 sp != (void *)&__global_list->dq; sp = sp->q.cqe_next)
956 if (F_ISSET(sp, S_INTERRUPTIBLE))
957 F_SET(sp, S_INTERRUPTED);