forgot to delete a TRACE call
[nvi.git] / ex / ex_subst.c
blob62bcd2ac6036fd6a393a05a8e544a71e5275364a
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.31 1994/01/02 17:54:08 bostic Exp $ (Berkeley) $Date: 1994/01/02 17:54:08 $";
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>
19 #include "vi.h"
20 #include "excmd.h"
22 #define SUB_FIRST 0x01 /* The 'r' flag isn't reasonable. */
23 #define SUB_MUSTSETR 0x02 /* The 'r' flag is required. */
25 static int checkmatchsize __P((SCR *, regex_t *));
26 static inline int regsub __P((SCR *,
27 char *, char **, size_t *, size_t *));
28 static int substitute __P((SCR *, EXF *,
29 EXCMDARG *, char *, regex_t *, u_int));
32 * ex_substitute --
33 * [line [,line]] s[ubstitute] [[/;]pat[/;]/repl[/;] [cgr] [count] [#lp]]
35 * Substitute on lines matching a pattern.
37 int
38 ex_substitute(sp, ep, cmdp)
39 SCR *sp;
40 EXF *ep;
41 EXCMDARG *cmdp;
43 regex_t *re, lre;
44 size_t blen, len;
45 u_int flags;
46 int delim, eval, reflags, replaced;
47 char *bp, *ptrn, *rep, *p, *t;
50 * Skip leading white space.
52 * !!!
53 * Historic vi allowed any non-alphanumeric to serve as the
54 * substitution command delimiter.
56 * !!!
57 * If the arguments are empty, it's the same as &, i.e. we
58 * repeat the last substitution.
60 for (p = cmdp->argv[0]->bp,
61 len = cmdp->argv[0]->len; len > 0; --len, ++p) {
62 if (!isblank(*p))
63 break;
65 if (len == 0)
66 return (ex_subagain(sp, ep, cmdp));
67 delim = *p++;
68 if (isalnum(delim))
69 return (substitute(sp, ep,
70 cmdp, p, &sp->subre, SUB_MUSTSETR));
73 * Get the pattern string, toss escaped characters.
75 * !!!
76 * Historic vi accepted any of the following forms:
78 * :s/abc/def/ change "abc" to "def"
79 * :s/abc/def change "abc" to "def"
80 * :s/abc/ delete "abc"
81 * :s/abc delete "abc"
83 * QUOTING NOTE:
85 * Only toss an escape character if it escapes a delimiter.
86 * This means that "s/A/\\\\f" replaces "A" with "\\f". It
87 * would be nice to be more regular, i.e. for each layer of
88 * escaping a single escape character is removed, but that's
89 * not how the historic vi worked.
91 for (ptrn = t = p;;) {
92 if (p[0] == '\0' || p[0] == delim) {
93 if (p[0] == delim)
94 ++p;
96 * !!!
97 * Nul terminate the pattern string -- it's passed
98 * to regcomp which doesn't understand anything else.
100 *t = '\0';
101 break;
103 if (p[0] == '\\' && p[1] == delim)
104 ++p;
105 *t++ = *p++;
108 /* If the pattern string is empty, use the last one. */
109 if (*ptrn == NULL) {
110 if (!F_ISSET(sp, S_SUBRE_SET)) {
111 msgq(sp, M_ERR,
112 "No previous regular expression.");
113 return (1);
115 re = &sp->subre;
116 flags = 0;
117 } else {
118 /* Set RE flags. */
119 reflags = 0;
120 if (O_ISSET(sp, O_EXTENDED))
121 reflags |= REG_EXTENDED;
122 if (O_ISSET(sp, O_IGNORECASE))
123 reflags |= REG_ICASE;
125 /* Convert vi-style RE's to POSIX 1003.2 RE's. */
126 if (re_conv(sp, &ptrn, &replaced))
127 return (1);
129 /* Compile the RE. */
130 eval = regcomp(&lre, (char *)ptrn, reflags);
132 /* Free up any allocated memory. */
133 if (replaced)
134 FREE_SPACE(sp, ptrn, 0);
136 if (eval) {
137 re_error(sp, eval, &lre);
138 return (1);
142 * Set saved RE.
144 * !!!
145 * Historic practice is that substitutes set the search
146 * direction as well as both substitute and search RE's.
148 sp->searchdir = FORWARD;
149 sp->sre = lre;
150 F_SET(sp, S_SRE_SET);
151 sp->subre = lre;
152 F_SET(sp, S_SUBRE_SET);
154 re = &lre;
155 flags = SUB_FIRST;
159 * Get the replacement string.
161 * The special character ~ (\~ if O_MAGIC not set) inserts the
162 * previous replacement string into this replacement string.
164 * The special character & (\& if O_MAGIC not set) matches the
165 * entire RE. No handling of & is required here, it's done by
166 * regsub().
168 * QUOTING NOTE:
170 * Only toss an escape character if it escapes a delimiter or
171 * an escape character, or if O_MAGIC is set and it escapes a
172 * tilde.
174 if (*p == '\0') {
175 if (sp->repl != NULL)
176 FREE(sp->repl, sp->repl_len);
177 sp->repl = NULL;
178 sp->repl_len = 0;
179 } else {
181 * Count ~'s to figure out how much space we need. We could
182 * special case nonexistent last patterns or whether or not
183 * O_MAGIC is set, but it's probably not worth the effort.
185 for (rep = p, len = 0;
186 p[0] != '\0' && p[0] != delim; ++p, ++len)
187 if (p[0] == '~')
188 len += sp->repl_len;
189 GET_SPACE_RET(sp, bp, blen, len);
190 for (t = bp, len = 0, p = rep;;) {
191 if (p[0] == '\0' || p[0] == delim) {
192 if (p[0] == delim)
193 ++p;
194 break;
196 if (p[0] == '\\') {
197 if (p[1] == '\\' || p[1] == delim)
198 ++p;
199 else if (p[1] == '~') {
200 ++p;
201 if (!O_ISSET(sp, O_MAGIC))
202 goto tilde;
204 } else if (p[0] == '~' && O_ISSET(sp, O_MAGIC)) {
205 tilde: ++p;
206 memmove(t, sp->repl, sp->repl_len);
207 t += sp->repl_len;
208 len += sp->repl_len;
209 continue;
211 *t++ = *p++;
212 ++len;
214 if (sp->repl != NULL)
215 FREE(sp->repl, sp->repl_len);
216 if ((sp->repl = malloc(len)) == NULL) {
217 msgq(sp, M_SYSERR, NULL);
218 FREE_SPACE(sp, bp, blen);
219 return (1);
221 memmove(sp->repl, bp, len);
222 sp->repl_len = len;
223 FREE_SPACE(sp, bp, blen);
226 if (checkmatchsize(sp, &sp->subre))
227 return (1);
228 return (substitute(sp, ep, cmdp, p, re, flags));
232 * ex_subagain --
233 * [line [,line]] & [cgr] [count] [#lp]]
235 * Substitute using the last substitute RE and replacement pattern.
238 ex_subagain(sp, ep, cmdp)
239 SCR *sp;
240 EXF *ep;
241 EXCMDARG *cmdp;
243 if (!F_ISSET(sp, S_SUBRE_SET)) {
244 msgq(sp, M_ERR, "No previous regular expression.");
245 return (1);
247 return (substitute(sp, ep, cmdp, cmdp->argv[0]->bp, &sp->subre, 0));
251 * ex_subtilde --
252 * [line [,line]] ~ [cgr] [count] [#lp]]
254 * Substitute using the last RE and last substitute replacement pattern.
257 ex_subtilde(sp, ep, cmdp)
258 SCR *sp;
259 EXF *ep;
260 EXCMDARG *cmdp;
262 if (!F_ISSET(sp, S_SRE_SET)) {
263 msgq(sp, M_ERR, "No previous regular expression.");
264 return (1);
266 return (substitute(sp, ep, cmdp, cmdp->argv[0]->bp, &sp->sre, 0));
270 * The nasty part of the substitution is what happens when the replacement
271 * string contains newlines. It's a bit tricky -- consider the information
272 * that has to be retained for "s/f\(o\)o/^M\1^M\1/". The solution here is
273 * to build a set of newline offsets which we use to break the line up later,
274 * when the replacement is done. Don't change it unless you're pretty damned
275 * confident.
277 #define NEEDNEWLINE(sp) { \
278 if (sp->newl_len == sp->newl_cnt) { \
279 sp->newl_len += 25; \
280 REALLOC(sp, sp->newl, size_t *, \
281 sp->newl_len * sizeof(size_t)); \
282 if (sp->newl == NULL) { \
283 sp->newl_len = 0; \
284 return (1); \
289 #define BUILD(sp, l, len) { \
290 if (lbclen + (len) > lblen) { \
291 lblen += MAX(lbclen + (len), 256); \
292 REALLOC(sp, lb, char *, lblen); \
293 if (lb == NULL) { \
294 lbclen = 0; \
295 return (1); \
298 memmove(lb + lbclen, l, len); \
299 lbclen += len; \
302 #define NEEDSP(sp, len, pnt) { \
303 if (lbclen + (len) > lblen) { \
304 lblen += MAX(lbclen + (len), 256); \
305 REALLOC(sp, lb, char *, lblen); \
306 if (lb == NULL) { \
307 lbclen = 0; \
308 return (1); \
310 pnt = lb + lbclen; \
315 * substitute --
316 * Do the substitution. This stuff is *really* tricky. There are
317 * lots of special cases, and general nastiness. Don't mess with it
318 * unless you're pretty confident.
320 static int
321 substitute(sp, ep, cmdp, s, re, flags)
322 SCR *sp;
323 EXF *ep;
324 EXCMDARG *cmdp;
325 char *s;
326 regex_t *re;
327 u_int flags;
329 MARK from, to;
330 recno_t elno, lno;
331 size_t blen, cnt, last, lbclen, lblen, len, llen, offset, saved_offset;
332 int didsub, do_eol_match;
333 int eflags, empty_ok, eval, linechanged, matched, quit;
334 int cflag, gflag, lflag, nflag, pflag, rflag;
335 char *bp, *lb;
338 * Historic vi permitted the '#', 'l' and 'p' options in vi mode, but
339 * it only displayed the last change. I'd disallow them, but they are
340 * useful in combination with the [v]global commands. In the current
341 * model the problem is combining them with the 'c' flag -- the screen
342 * would have to flip back and forth between the confirm screen and the
343 * ex print screen, which would be pretty awful. We do display all
344 * changes, though, for what that's worth.
346 * !!!
347 * Historic vi was fairly strict about the order of "options", the
348 * count, and "flags". I'm somewhat fuzzy on the difference between
349 * options and flags, anyway, so this is a simpler approach, and we
350 * just take it them in whatever order the user gives them. (The ex
351 * usage statement doesn't reflect this.)
353 cflag = gflag = lflag = nflag = pflag = rflag = 0;
354 for (lno = OOBLNO; *s != '\0'; ++s)
355 switch (*s) {
356 case ' ':
357 case '\t':
358 break;
359 case '0': case '1': case '2': case '3': case '4':
360 case '5': case '6': case '7': case '8': case '9':
361 if (lno != OOBLNO)
362 goto usage;
363 errno = 0;
364 lno = strtoul(s, &s, 10);
365 if (*s == '\0') /* Loop increment correction. */
366 --s;
367 if (errno == ERANGE) {
368 if (lno == LONG_MAX)
369 msgq(sp, M_ERR, "Count overflow.");
370 else if (lno == LONG_MIN)
371 msgq(sp, M_ERR, "Count underflow.");
372 else
373 msgq(sp, M_SYSERR, NULL);
374 return (1);
377 * In historic vi, the count was inclusive from the
378 * second address.
380 cmdp->addr1.lno = cmdp->addr2.lno;
381 cmdp->addr2.lno += lno - 1;
382 break;
383 case '#':
384 nflag = 1;
385 break;
386 case 'c':
387 cflag = 1;
388 break;
389 case 'g':
390 gflag = 1;
391 break;
392 case 'l':
393 lflag = 1;
394 break;
395 case 'p':
396 pflag = 1;
397 break;
398 case 'r':
399 if (LF_ISSET(SUB_FIRST)) {
400 msgq(sp, M_ERR,
401 "Regular expression specified; r flag meaningless.");
402 return (1);
404 if (!F_ISSET(sp, S_SUBRE_SET)) {
405 msgq(sp, M_ERR,
406 "No previous regular expression.");
407 return (1);
409 rflag = 1;
410 break;
411 default:
412 goto usage;
415 if (*s != '\0' || !rflag && LF_ISSET(SUB_MUSTSETR)) {
416 usage: msgq(sp, M_ERR, "Usage: %s", cmdp->cmd->usage);
417 return (1);
420 if (IN_VI_MODE(sp) && cflag && (lflag || nflag || pflag)) {
421 msgq(sp, M_ERR,
422 "The #, l and p flags may not be combined with the c flag in vi mode.");
423 return (1);
427 * bp: if interactive, line cache
428 * blen: if interactive, line cache length
429 * lb: build buffer pointer.
430 * lbclen: current length of built buffer.
431 * lblen; length of build buffer.
433 bp = lb = NULL;
434 blen = lbclen = lblen = 0;
436 /* For each line... */
437 for (matched = quit = 0, lno = cmdp->addr1.lno,
438 elno = cmdp->addr2.lno; !quit && lno <= elno; ++lno) {
440 /* Get the line. */
441 if ((s = file_gline(sp, ep, lno, &llen)) == NULL) {
442 GETLINE_ERR(sp, lno);
443 return (1);
447 * Make a local copy if doing confirmation -- when calling
448 * the confirm routine we're likely to lose the cached copy.
450 if (cflag) {
451 if (bp == NULL) {
452 GET_SPACE_RET(sp, bp, blen, llen);
453 } else
454 ADD_SPACE_RET(sp, bp, blen, llen);
455 memmove(bp, s, llen);
456 s = bp;
459 /* Start searching from the beginning. */
460 offset = 0;
461 len = llen;
463 /* Reset the build buffer offset. */
464 lbclen = 0;
466 /* Reset empty match flag. */
467 empty_ok = 1;
470 * We don't want to have to do a setline if the line didn't
471 * change -- keep track of whether or not this line changed.
472 * If doing confirmations, don't want to keep setting the
473 * line if change is refused -- keep track of substitutions.
475 didsub = linechanged = 0;
477 /* New line, do an EOL match. */
478 do_eol_match = 1;
480 /* It's not nul terminated, but we pretend it is. */
481 eflags = REG_STARTEND;
484 * The search area is from s + offset to the EOL.
486 * Generally, sp->match[0].rm_so is the offset of the start
487 * of the match from the start of the search, and offset is
488 * the offset of the start of the last search.
490 nextmatch: sp->match[0].rm_so = 0;
491 sp->match[0].rm_eo = len;
493 /* Get the next match. */
494 eval = regexec(re,
495 (char *)s + offset, re->re_nsub + 1, sp->match, eflags);
498 * There wasn't a match or if there was an error, deal with
499 * it. If there was a previous match in this line, resolve
500 * the changes into the database. Otherwise, just move on.
502 if (eval == REG_NOMATCH)
503 goto endmatch;
504 if (eval != 0) {
505 re_error(sp, eval, re);
506 goto ret1;
508 matched = 1;
510 /* Only the first search can match an anchored expression. */
511 eflags |= REG_NOTBOL;
514 * !!!
515 * It's possible to match 0-length strings -- for example, the
516 * command s;a*;X;, when matched against the string "aabb" will
517 * result in "XbXbX", i.e. the matches are "aa", the space
518 * between the b's and the space between the b's and the end of
519 * the string. There is a similar space between the beginning
520 * of the string and the a's. The rule that we use (because vi
521 * historically used it) is that any 0-length match, occurring
522 * immediately after a match, is ignored. Otherwise, the above
523 * example would have resulted in "XXbXbX". Another example is
524 * incorrectly using " *" to replace groups of spaces with one
525 * space.
527 * The way we do this is that if we just had a successful match,
528 * the starting offset does not skip characters, and the match
529 * is empty, ignore the match and move forward. If there's no
530 * more characters in the string, we were attempting to match
531 * after the last character, so quit.
533 if (!empty_ok &&
534 sp->match[0].rm_so == 0 && sp->match[0].rm_eo == 0) {
535 empty_ok = 1;
536 if (len == 0)
537 goto endmatch;
538 BUILD(sp, s + offset, 1)
539 ++offset;
540 --len;
541 goto nextmatch;
544 /* Confirm change. */
545 if (cflag) {
547 * Set the cursor position for confirmation. Note,
548 * if we matched on a '$', the cursor may be past
549 * the end of line.
551 * XXX
552 * We may want to "fix" this in the confirm routine,
553 * if the confirm routine should be able to display
554 * a cursor past EOL.
556 from.lno = to.lno = lno;
557 from.cno = sp->match[0].rm_so + offset;
558 to.cno = sp->match[0].rm_eo;
559 if (llen == 0)
560 from.cno = to.cno = 0;
561 else {
562 if (to.cno >= llen)
563 to.cno = llen - 1;
564 if (from.cno >= llen)
565 from.cno = llen - 1;
567 switch (sp->s_confirm(sp, ep, &from, &to)) {
568 case CONF_YES:
569 break;
570 case CONF_NO:
571 didsub = 0;
572 BUILD(sp, s +offset, sp->match[0].rm_eo);
573 goto skip;
574 case CONF_QUIT:
575 /* Set the quit flag. */
576 quit = 1;
578 /* If interruptible, pass the info back. */
579 if (F_ISSET(sp, S_INTERRUPTIBLE))
580 F_SET(sp, S_INTERRUPTED);
583 * If any changes, resolve them, otherwise
584 * return to the main loop.
586 goto endmatch;
590 /* Copy the bytes before the match into the build buffer. */
591 BUILD(sp, s + offset, sp->match[0].rm_so);
594 * Cursor moves to last line changed, unless doing confirm,
595 * in which case don't move it.
597 * !!!
598 * Historic vi just put the cursor on the first non-blank
599 * of the last line changed. This might be better.
601 if (!cflag) {
602 sp->lno = lno;
603 sp->cno = sp->match[0].rm_so + offset;
606 /* Substitute the matching bytes. */
607 didsub = 1;
608 if (regsub(sp, s + offset, &lb, &lbclen, &lblen))
609 goto ret1;
611 /* Set the change flag so we know this line was modified. */
612 linechanged = 1;
614 /* Move past the matched bytes. */
615 skip: offset += sp->match[0].rm_eo;
616 len -= sp->match[0].rm_eo;
618 /* A match cannot be followed by an empty pattern. */
619 empty_ok = 0;
622 * If doing a global change with confirmation, we have to
623 * update the screen. The basic idea is to store the line
624 * so the screen update routines can find it, and restart.
626 if (didsub && cflag && gflag) {
628 * The new search offset will be the end of the
629 * modified line.
631 saved_offset = lbclen;
633 /* Copy the rest of the line. */
634 if (len)
635 BUILD(sp, s + offset, len)
637 /* Set the new offset. */
638 offset = saved_offset;
640 /* Store inserted lines, adjusting the build buffer. */
641 last = 0;
642 if (sp->newl_cnt) {
643 for (cnt = 0;
644 cnt < sp->newl_cnt; ++cnt, ++lno, ++elno) {
645 if (file_iline(sp, ep, lno,
646 lb + last, sp->newl[cnt] - last))
647 goto ret1;
648 last = sp->newl[cnt] + 1;
649 ++sp->rptlines[L_ADDED];
651 lbclen -= last;
652 offset -= last;
653 sp->newl_cnt = 0;
656 /* Store and retrieve the line. */
657 if (file_sline(sp, ep, lno, lb + last, lbclen))
658 goto ret1;
659 if ((s = file_gline(sp, ep, lno, &llen)) == NULL) {
660 GETLINE_ERR(sp, lno);
661 goto ret1;
663 ADD_SPACE_RET(sp, bp, blen, llen)
664 memmove(bp, s, llen);
665 s = bp;
666 len = llen - offset;
668 /* Restart the build. */
669 lbclen = 0;
670 BUILD(sp, s, offset);
673 * If we haven't already done the after-the-string
674 * match, do one. Set REG_NOTEOL so the '$' pattern
675 * only matches once.
677 if (!do_eol_match)
678 goto endmatch;
679 if (offset == len) {
680 do_eol_match = 0;
681 eflags |= REG_NOTEOL;
683 goto nextmatch;
687 * If it's a global:
689 * If at the end of the string, do a test for the after
690 * the string match. Set REG_NOTEOL so the '$' pattern
691 * only matches once.
693 if (gflag && do_eol_match) {
694 if (len == 0) {
695 do_eol_match = 0;
696 eflags |= REG_NOTEOL;
698 goto nextmatch;
701 endmatch: if (!linechanged)
702 continue;
704 /* Copy any remaining bytes into the build buffer. */
705 if (len)
706 BUILD(sp, s + offset, len)
708 /* Store inserted lines, adjusting the build buffer. */
709 last = 0;
710 if (sp->newl_cnt) {
711 for (cnt = 0;
712 cnt < sp->newl_cnt; ++cnt, ++lno, ++elno) {
713 if (file_iline(sp, ep,
714 lno, lb + last, sp->newl[cnt] - last))
715 goto ret1;
716 last = sp->newl[cnt] + 1;
717 ++sp->rptlines[L_ADDED];
719 lbclen -= last;
720 sp->newl_cnt = 0;
723 /* Store the changed line. */
724 if (file_sline(sp, ep, lno, lb + last, lbclen))
725 goto ret1;
727 /* Update changed line counter. */
728 ++sp->rptlines[L_CHANGED];
730 /* Display as necessary. */
731 if (lflag || nflag || pflag) {
732 from.lno = to.lno = lno;
733 from.cno = to.cno = 0;
734 if (lflag)
735 ex_print(sp, ep, &from, &to, E_F_LIST);
736 if (nflag)
737 ex_print(sp, ep, &from, &to, E_F_HASH);
738 if (pflag)
739 ex_print(sp, ep, &from, &to, E_F_PRINT);
744 * If not in a global command, and nothing matched, say so.
745 * Else, if none of the lines displayed, put something up.
747 if (!matched) {
748 if (!F_ISSET(sp, S_GLOBAL))
749 msgq(sp, M_INFO, "No match found.");
750 } else if (!lflag && !nflag && !pflag)
751 F_SET(EXP(sp), EX_AUTOPRINT);
753 if (bp != NULL)
754 FREE_SPACE(sp, bp, blen);
755 return (0);
757 if (bp != NULL)
758 ret1: FREE_SPACE(sp, bp, blen);
759 return (1);
763 * regsub --
764 * Do the substitution for a regular expression.
766 static inline int
767 regsub(sp, ip, lbp, lbclenp, lblenp)
768 SCR *sp;
769 char *ip; /* Input line. */
770 char **lbp;
771 size_t *lbclenp, *lblenp;
773 enum { C_NOTSET, C_LOWER, C_ONELOWER, C_ONEUPPER, C_UPPER } conv;
774 size_t lbclen, lblen; /* Local copies. */
775 size_t mlen; /* Match length. */
776 size_t rpl; /* Remaining replacement length. */
777 char *rp; /* Replacement pointer. */
778 int ch;
779 int no; /* Match replacement offset. */
780 char *p, *t; /* Buffer pointers. */
781 char *lb; /* Local copies. */
783 lb = *lbp; /* Get local copies. */
784 lbclen = *lbclenp;
785 lblen = *lblenp;
788 * QUOTING NOTE:
790 * There are some special sequences that vi provides in the
791 * replacement patterns.
792 * & string the RE matched (\& if nomagic set)
793 * \# n-th regular subexpression
794 * \E end \U, \L conversion
795 * \e end \U, \L conversion
796 * \l convert the next character to lower-case
797 * \L convert to lower-case, until \E, \e, or end of replacement
798 * \u convert the next character to upper-case
799 * \U convert to upper-case, until \E, \e, or end of replacement
801 * Otherwise, since this is the lowest level of replacement, discard
802 * all escape characters. This (hopefully) follows historic practice.
804 #define ADDCH(ch) { \
805 CHAR_T __ch = (ch); \
806 u_int __value = term_key_val(sp, __ch); \
807 if (__value == K_CR || __value == K_NL) { \
808 NEEDNEWLINE(sp); \
809 sp->newl[sp->newl_cnt++] = lbclen; \
810 } else if (conv != C_NOTSET) { \
811 switch (conv) { \
812 case C_ONELOWER: \
813 conv = C_NOTSET; \
814 /* FALLTHROUGH */ \
815 case C_LOWER: \
816 if (isupper(__ch)) \
817 __ch = tolower(__ch); \
818 break; \
819 case C_ONEUPPER: \
820 conv = C_NOTSET; \
821 /* FALLTHROUGH */ \
822 case C_UPPER: \
823 if (islower(__ch)) \
824 __ch = toupper(__ch); \
825 break; \
826 default: \
827 abort(); \
830 NEEDSP(sp, 1, p); \
831 *p++ = __ch; \
832 ++lbclen; \
834 conv = C_NOTSET;
835 for (rp = sp->repl, rpl = sp->repl_len, p = lb + lbclen; rpl--;) {
836 switch (ch = *rp++) {
837 case '&':
838 if (O_ISSET(sp, O_MAGIC)) {
839 no = 0;
840 goto subzero;
842 break;
843 case '\\':
844 if (rpl == 0)
845 break;
846 --rpl;
847 switch (ch = *rp) {
848 case '&':
849 if (!O_ISSET(sp, O_MAGIC)) {
850 ++rp;
851 no = 0;
852 goto subzero;
854 break;
855 case '0': case '1': case '2': case '3': case '4':
856 case '5': case '6': case '7': case '8': case '9':
857 no = *rp++ - '0';
858 subzero: if (sp->match[no].rm_so == -1 ||
859 sp->match[no].rm_eo == -1)
860 continue;
861 mlen =
862 sp->match[no].rm_eo - sp->match[no].rm_so;
863 for (t = ip + sp->match[no].rm_so; mlen--; ++t)
864 ADDCH(*t);
865 continue;
866 case 'e':
867 case 'E':
868 ++rp;
869 conv = C_NOTSET;
870 continue;
871 case 'l':
872 ++rp;
873 conv = C_ONELOWER;
874 continue;
875 case 'L':
876 ++rp;
877 conv = C_LOWER;
878 continue;
879 case 'u':
880 ++rp;
881 conv = C_ONEUPPER;
882 continue;
883 case 'U':
884 ++rp;
885 conv = C_UPPER;
886 continue;
887 default:
888 ++rp;
889 break;
892 ADDCH(ch);
895 *lbp = lb; /* Update caller's information. */
896 *lbclenp = lbclen;
897 *lblenp = lblen;
898 return (0);
901 static int
902 checkmatchsize(sp, re)
903 SCR *sp;
904 regex_t *re;
906 /* Build nsub array as necessary. */
907 if (sp->matchsize < re->re_nsub + 1) {
908 sp->matchsize = re->re_nsub + 1;
909 REALLOC(sp, sp->match,
910 regmatch_t *, sp->matchsize * sizeof(regmatch_t));
911 if (sp->match == NULL) {
912 sp->matchsize = 0;
913 return (1);
916 return (0);