replace special array lookups with value returned in CH structure
[nvi.git] / ex / ex_subst.c
blobe063f1f52e53a4a993e9363519e6373bf5369adb
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.19 1993/11/29 14:15:19 bostic Exp $ (Berkeley) $Date: 1993/11/29 14:15:19 $";
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));
31 * ex_substitute --
32 * [line [,line]] s[ubstitute] [[/;]pat[/;]/repl[/;] [cgr] [count] [#lp]]
34 * Substitute on lines matching a pattern.
36 int
37 ex_substitute(sp, ep, cmdp)
38 SCR *sp;
39 EXF *ep;
40 EXCMDARG *cmdp;
42 regex_t *re, lre;
43 size_t blen, len;
44 u_int flags;
45 int delim, eval, reflags, replaced;
46 char *bp, *ptrn, *rep, *p, *t;
49 * Skip leading white space. Historic vi allowed any non-
50 * alphanumeric to serve as the substitution command delimiter.
52 for (p = cmdp->argv[0]; isblank(*p); ++p);
53 delim = *p++;
54 if (isalnum(delim))
55 return (substitute(sp, ep,
56 cmdp, p, &sp->subre, SUB_MUSTSETR));
59 * Get the pattern string, toss escaped characters.
61 * !!!
62 * Historic vi accepted any of the following forms:
64 * :s/abc/def/ change "abc" to "def"
65 * :s/abc/def change "abc" to "def"
66 * :s/abc/ delete "abc"
67 * :s/abc delete "abc"
69 * QUOTING NOTE:
71 * Only toss an escape character if it escapes a delimiter.
72 * This means that "s/A/\\\\f" replaces "A" with "\\f". It
73 * would be nice to be more regular, i.e. for each layer of
74 * escaping a single escape character is removed, but that's
75 * not how the historic vi worked.
77 for (ptrn = t = p;;) {
78 if (p[0] == '\0' || p[0] == delim) {
79 if (p[0] == delim)
80 ++p;
82 * !!!
83 * Nul terminate the pattern string -- it's passed
84 * to regcomp which doesn't understand anything else.
86 *t = '\0';
87 break;
89 if (p[0] == '\\' && p[1] == delim)
90 ++p;
91 *t++ = *p++;
94 /* If the pattern string is empty, use the last one. */
95 if (*ptrn == NULL) {
96 if (!F_ISSET(sp, S_SUBRE_SET)) {
97 msgq(sp, M_ERR,
98 "No previous regular expression.");
99 return (1);
101 re = &sp->subre;
102 flags = 0;
103 } else {
104 /* Set RE flags. */
105 reflags = 0;
106 if (O_ISSET(sp, O_EXTENDED))
107 reflags |= REG_EXTENDED;
108 if (O_ISSET(sp, O_IGNORECASE))
109 reflags |= REG_ICASE;
111 /* Convert vi-style RE's to POSIX 1003.2 RE's. */
112 if (re_conv(sp, &ptrn, &replaced))
113 return (1);
115 /* Compile the RE. */
116 eval = regcomp(&lre, (char *)ptrn, reflags);
118 /* Free up any allocated memory. */
119 if (replaced)
120 FREE_SPACE(sp, ptrn, 0);
122 if (eval) {
123 re_error(sp, eval, &lre);
124 return (1);
128 * Set saved RE. Historic practice is that
129 * substitutes set direction as well as the RE.
131 sp->subre = lre;
132 sp->searchdir = FORWARD;
133 F_SET(sp, S_SUBRE_SET);
135 re = &lre;
136 flags = SUB_FIRST;
140 * Get the replacement string.
142 * The special character ~ (\~ if O_MAGIC not set) inserts the
143 * previous replacement string into this replacement string.
145 * The special character & (\& if O_MAGIC not set) matches the
146 * entire RE. No handling of & is required here, it's done by
147 * regsub().
149 * QUOTING NOTE:
151 * Only toss an escape character if it escapes a delimiter or
152 * if O_MAGIC is set and it escapes a tilde.
154 if (*p == '\0') {
155 if (sp->repl != NULL)
156 FREE(sp->repl, sp->repl_len);
157 sp->repl = NULL;
158 sp->repl_len = 0;
159 } else {
161 * Count ~'s to figure out how much space we need. We could
162 * special case nonexistent last patterns or whether or not
163 * O_MAGIC is set, but it's probably not worth the effort.
165 for (rep = p, len = 0;
166 p[0] != '\0' && p[0] != delim; ++p, ++len)
167 if (p[0] == '~')
168 len += sp->repl_len;
169 GET_SPACE(sp, bp, blen, len);
170 for (t = bp, len = 0, p = rep;;) {
171 if (p[0] == '\0' || p[0] == delim) {
172 if (p[0] == delim)
173 ++p;
174 break;
176 if (p[0] == '\\') {
177 if (p[1] == delim)
178 ++p;
179 else if (p[1] == '~') {
180 ++p;
181 if (!O_ISSET(sp, O_MAGIC))
182 goto tilde;
184 } else if (p[0] == '~' && O_ISSET(sp, O_MAGIC)) {
185 tilde: ++p;
186 memmove(t, sp->repl, sp->repl_len);
187 t += sp->repl_len;
188 len += sp->repl_len;
189 continue;
191 *t++ = *p++;
192 ++len;
194 if (sp->repl != NULL)
195 FREE(sp->repl, sp->repl_len);
196 if ((sp->repl = malloc(len)) == NULL) {
197 msgq(sp, M_SYSERR, NULL);
198 FREE_SPACE(sp, bp, blen);
199 return (1);
201 memmove(sp->repl, bp, len);
202 sp->repl_len = len;
203 FREE_SPACE(sp, bp, blen);
206 if (checkmatchsize(sp, &sp->subre))
207 return (1);
208 return (substitute(sp, ep, cmdp, p, re, flags));
212 * ex_subagain --
213 * [line [,line]] & [cgr] [count] [#lp]]
215 * Substitute using the last substitute RE and replacement pattern.
218 ex_subagain(sp, ep, cmdp)
219 SCR *sp;
220 EXF *ep;
221 EXCMDARG *cmdp;
223 if (!F_ISSET(sp, S_SUBRE_SET)) {
224 msgq(sp, M_ERR, "No previous regular expression.");
225 return (1);
227 return (substitute(sp, ep, cmdp, cmdp->argv[0], &sp->subre, 0));
231 * ex_subtilde --
232 * [line [,line]] ~ [cgr] [count] [#lp]]
234 * Substitute using the last RE and last substitute replacement pattern.
237 ex_subtilde(sp, ep, cmdp)
238 SCR *sp;
239 EXF *ep;
240 EXCMDARG *cmdp;
242 if (!F_ISSET(sp, S_SRE_SET)) {
243 msgq(sp, M_ERR, "No previous regular expression.");
244 return (1);
246 return (substitute(sp, ep, cmdp, cmdp->argv[0], &sp->sre, 0));
250 * The nasty part of the substitution is what happens when the replacement
251 * string contains newlines. It's a bit tricky -- consider the information
252 * that has to be retained for "s/f\(o\)o/^M\1^M\1/". The solution here is
253 * to build a set of newline offets which we use to break the line up later,
254 * when the replacement is done. Don't change it unless you're pretty damned
255 * confident.
257 #define NEEDNEWLINE(sp) { \
258 if (sp->newl_len == sp->newl_cnt) { \
259 sp->newl_len += 25; \
260 if ((sp->newl = realloc(sp->newl, \
261 sp->newl_len * sizeof(size_t))) == NULL) { \
262 msgq(sp, M_SYSERR, NULL); \
263 sp->newl_len = 0; \
264 return (1); \
269 #define BUILD(sp, l, len) { \
270 if (lbclen + (len) > lblen) { \
271 lblen += MAX(lbclen + (len), 256); \
272 if ((lb = realloc(lb, lblen)) == NULL) { \
273 msgq(sp, M_SYSERR, NULL); \
274 lbclen = 0; \
275 return (1); \
278 memmove(lb + lbclen, l, len); \
279 lbclen += len; \
282 #define NEEDSP(sp, len, pnt) { \
283 if (lbclen + (len) > lblen) { \
284 lblen += MAX(lbclen + (len), 256); \
285 if ((lb = realloc(lb, lblen)) == NULL) { \
286 msgq(sp, M_SYSERR, NULL); \
287 lbclen = 0; \
288 return (1); \
290 pnt = lb + lbclen; \
295 * substitute --
296 * Do the substitution. This stuff is *really* tricky. There are
297 * lots of special cases, and general nastiness. Don't mess with it
298 * unless you're pretty confident.
300 static int
301 substitute(sp, ep, cmdp, s, re, flags)
302 SCR *sp;
303 EXF *ep;
304 EXCMDARG *cmdp;
305 char *s;
306 regex_t *re;
307 u_int flags;
309 MARK from, to;
310 recno_t elno, lno, lastline;
311 size_t blen, cnt, last, lbclen, lblen, len, offset;
312 int do_eol_match, eflags, eval, linechanged, quit;
313 int cflag, gflag, lflag, nflag, pflag, rflag;
314 char *bp, *lb;
317 * Historic vi permitted the '#', 'l' and 'p' options in vi mode, but
318 * it only displayed the last change. I'd disallow them, but they are
319 * useful in combination with the [v]global commands. In the current
320 * model the problem is combining them with the 'c' flag -- the screen
321 * would have to flip back and forth between the confirm screen and the
322 * ex print screen, which would be pretty awful. We do display all
323 * changes, though, for what that's worth.
325 * !!!
326 * Historic vi was fairly strict about the order of "options", the
327 * count, and "flags". I'm somewhat fuzzy on the difference between
328 * options and flags, anyway, so this is a simpler approach, and we
329 * just take it them in whatever order the user gives them. (The ex
330 * usage statement doesn't reflect this.)
332 cflag = gflag = lflag = nflag = pflag = rflag = 0;
333 for (lno = OOBLNO; *s != '\0'; ++s)
334 switch (*s) {
335 case ' ':
336 case '\t':
337 break;
338 case '0': case '1': case '2': case '3': case '4':
339 case '5': case '6': case '7': case '8': case '9':
340 if (lno != OOBLNO)
341 goto usage;
342 errno = 0;
343 lno = strtoul(s, &s, 10);
344 if (errno == ERANGE) {
345 if (lno == LONG_MAX)
346 msgq(sp, M_ERR, "Count overflow.");
347 else if (lno == LONG_MIN)
348 msgq(sp, M_ERR, "Count underflow.");
349 else
350 msgq(sp, M_SYSERR, NULL);
351 return (1);
354 * In historic vi, the count was inclusive from the
355 * second address.
357 cmdp->addr1.lno = cmdp->addr2.lno;
358 cmdp->addr2.lno += lno - 1;
359 break;
360 case '#':
361 nflag = 1;
362 break;
363 case 'c':
364 cflag = 1;
365 break;
366 case 'g':
367 gflag = 1;
368 break;
369 case 'l':
370 lflag = 1;
371 break;
372 case 'p':
373 pflag = 1;
374 break;
375 case 'r':
376 if (LF_ISSET(SUB_FIRST)) {
377 msgq(sp, M_ERR,
378 "Regular expression specified; r flag meaningless.");
379 return (1);
381 if (!F_ISSET(sp, S_SUBRE_SET)) {
382 msgq(sp, M_ERR,
383 "No previous regular expression.");
384 return (1);
386 rflag = 1;
387 break;
388 default:
389 goto usage;
392 if (*s != '\0' || !rflag && LF_ISSET(SUB_MUSTSETR)) {
393 usage: msgq(sp, M_ERR, "Usage: %s", cmdp->cmd->usage);
394 return (1);
397 if (IN_VI_MODE(sp) && cflag && (lflag || nflag || pflag)) {
398 msgq(sp, M_ERR,
399 "The #, l and p flags may not be combined with the c flag in vi mode.");
400 return (1);
403 /* Get some space. */
404 GET_SPACE(sp, bp, blen, 512);
407 * lb: build buffer pointer.
408 * lbclen: current length of built buffer.
409 * lblen; length of build buffer.
411 lb = NULL;
412 lbclen = lblen = 0;
415 * Since multiple changes can happen in a line, we only increment
416 * the change count on the first change to a line.
418 lastline = OOBLNO;
420 /* For each line... */
421 for (quit = 0, lno = cmdp->addr1.lno,
422 elno = cmdp->addr2.lno; !quit && lno <= elno; ++lno) {
424 /* Get the line. */
425 if ((s = file_gline(sp, ep, lno, &len)) == NULL) {
426 GETLINE_ERR(sp, lno);
427 return (1);
431 * Make a local copy if doing confirmation -- when calling
432 * the confirm routine we're likely to lose our cached copy.
434 if (cflag) {
435 ADD_SPACE(sp, bp, blen, len)
436 memmove(bp, s, len);
437 s = bp;
440 /* Reset the buffer pointer. */
441 lbclen = 0;
444 * We don't want to have to do a setline if the line didn't
445 * change -- keep track of whether or not this line changed.
447 linechanged = 0;
449 /* New line, do EOL match. */
450 do_eol_match = 1;
452 /* It's not nul terminated, but we pretend it is. */
453 eflags = REG_STARTEND;
455 /* The search area is from 's' to the end of the line. */
456 nextmatch: sp->match[0].rm_so = 0;
457 sp->match[0].rm_eo = len;
459 /* Get the next match. */
460 skipmatch: eval = regexec(re,
461 (char *)s, re->re_nsub + 1, sp->match, eflags);
464 * There wasn't a match -- if there was an error, deal with
465 * it. If there was a previous match in this line, resolve
466 * the changes into the database. Otherwise, just move on.
468 if (eval == REG_NOMATCH) {
469 if (linechanged)
470 goto endmatch;
471 continue;
473 if (eval != 0) {
474 re_error(sp, eval, re);
475 goto ret1;
478 /* Confirm change. */
479 if (cflag) {
481 * Set the cursor position for confirmation. Note,
482 * if we matched on a '$', the cursor may be past
483 * the end of line.
485 * XXX
486 * May want to "fix" this in the confirm routine;
487 * the confirm routine may be able to display a
488 * cursor past EOL.
490 from.lno = lno;
491 from.cno = sp->match[0].rm_so;
492 to.lno = lno;
493 to.cno = sp->match[0].rm_eo;
494 if (len != 0) {
495 if (to.cno >= len)
496 to.cno = len - 1;
497 if (from.cno >= len)
498 from.cno = len - 1;
501 switch (sp->s_confirm(sp, ep, &from, &to)) {
502 case CONF_YES:
503 break;
504 case CONF_NO:
506 * Copy the bytes before the match and the
507 * bytes in the match into the build buffer.
509 BUILD(sp, s, sp->match[0].rm_eo);
510 goto skip;
511 case CONF_QUIT:
512 /* Set the quit flag. */
513 quit = 1;
515 /* If interruptible, pass the info back. */
516 if (F_ISSET(sp, S_INTERRUPTIBLE))
517 F_SET(sp, S_INTERRUPTED);
520 * If any changes, resolve them, otherwise
521 * return to the main loop.
523 if (linechanged)
524 goto endmatch;
525 continue;
529 /* Copy the bytes before the match into the build buffer. */
530 BUILD(sp, s, sp->match[0].rm_so);
533 * Update the cursor to the start of the change.
535 * !!!
536 * Historic vi just put the cursor on the first non-blank
537 * of the last line changed. This might be better.
539 if (!cflag)
540 sp->cno = sp->match[0].rm_so;
542 /* Substitute the matching bytes. */
543 if (regsub(sp, s, &lb, &lbclen, &lblen))
544 goto ret1;
546 /* Set the change flag so we know this line was modified. */
547 linechanged = 1;
550 * Move the pointers past the matched bytes. One very special
551 * case is that it's possible to match strings of 0 length.
552 * A common example is trying to use " *" to replace groups of
553 * spaces with a single space. Guarantee that we move forward,
554 * but not if we're matching the 0 length string after the last
555 * character in the line.
557 skip: s += sp->match[0].rm_eo;
558 len -= sp->match[0].rm_eo;
559 if (len && sp->match[0].rm_so == sp->match[0].rm_eo) {
560 BUILD(sp, s, 1)
561 ++s;
562 --len;
565 /* Only the first search matches anchored expression. */
566 eflags |= REG_NOTBOL;
569 * If doing a global change with confirmation, we have to
570 * update the screen. The basic idea is to store the line
571 * so the screen update routines can find it, but start at
572 * the old offset.
574 if (linechanged && cflag && gflag) {
575 /* Save offset. */
576 offset = lbclen;
578 /* Copy the suffix. */
579 if (len)
580 BUILD(sp, s, len)
582 /* Store inserted lines, adjusting the build buffer. */
583 last = 0;
584 if (sp->newl_cnt) {
585 for (cnt = 0; cnt < sp->newl_cnt;
586 ++cnt, ++lno, ++elno, ++lastline) {
587 if (file_iline(sp, ep, lno,
588 lb + last, sp->newl[cnt] - last))
589 goto ret1;
590 last = sp->newl[cnt] + 1;
591 ++sp->rptlines[L_ADDED];
593 lbclen -= last;
594 offset -= last;
596 sp->newl_cnt = 0;
599 /* Store and retrieve the line. */
600 if (file_sline(sp, ep, lno, lb + last, lbclen))
601 goto ret1;
602 if ((s = file_gline(sp, ep, lno, &len)) == NULL) {
603 GETLINE_ERR(sp, lno);
604 goto ret1;
606 ADD_SPACE(sp, bp, blen, len)
607 memmove(bp, s, len);
608 s = bp;
610 /* Restart the build. */
611 lbclen = 0;
613 /* Update changed line counter. */
614 if (lastline != lno) {
615 ++sp->rptlines[L_CHANGED];
616 lastline = lno;
619 /* Start in the middle of the line. */
620 sp->match[0].rm_so = offset;
621 sp->match[0].rm_eo = len;
624 * If it's global, continue.
626 * NB: It's possible to match 0-length strings, and we
627 * behave as if such a string matches the space before
628 * the first character in the string and after the last
629 * character in the string. (This is how the historic
630 * vi did it.) So, do one more check after reaching
631 * the end of the string. Set REG_NOTEOL so the '$'
632 * pattern only matches once. One possible bug is that
633 * the '$' pattern will match BEFORE the empty match
634 * after the last character matches. (The '^' matching
635 * doesn't share the problem because the first match
636 * will force you past the matching location.) I don't
637 * see any reasonable way to fix this now.
639 if (do_eol_match) {
640 if (offset == len) {
641 do_eol_match = 0;
642 eflags |= REG_NOTEOL;
644 goto skipmatch;
648 /* If it's global ... (see comment immediately above). */
649 if (gflag && do_eol_match) {
650 if (!len) {
651 do_eol_match = 0;
652 eflags |= REG_NOTEOL;
654 goto nextmatch;
658 /* Copy any remaining bytes into the build buffer. */
659 endmatch: if (len)
660 BUILD(sp, s, len)
662 /* Store inserted lines, adjusting the build buffer. */
663 last = 0;
664 if (sp->newl_cnt) {
665 for (cnt = 0; cnt < sp->newl_cnt;
666 ++cnt, ++lno, ++elno, ++lastline) {
667 if (file_iline(sp, ep,
668 lno, lb + last, sp->newl[cnt] - last))
669 goto ret1;
670 last = sp->newl[cnt] + 1;
671 ++sp->rptlines[L_ADDED];
673 lbclen -= last;
675 sp->newl_cnt = 0;
676 linechanged = 1;
679 /* Store the changed line. */
680 if (linechanged)
681 if (file_sline(sp, ep, lno, lb + last, lbclen))
682 goto ret1;
684 /* Update changed line counter. */
685 if (lastline != lno) {
686 ++sp->rptlines[L_CHANGED];
687 lastline = lno;
690 /* Display as necessary. */
691 if (lflag || nflag || pflag) {
692 from.lno = to.lno = lno;
693 from.cno = to.cno = 0;
694 if (lflag)
695 ex_print(sp, ep, &from, &to, E_F_LIST);
696 if (nflag)
697 ex_print(sp, ep, &from, &to, E_F_HASH);
698 if (pflag)
699 ex_print(sp, ep, &from, &to, E_F_PRINT);
704 * Cursor moves to last line changed, unless doing confirm,
705 * in which case don't move it.
707 if (!cflag && lastline != OOBLNO)
708 sp->lno = lastline;
711 * Note if nothing found. Else, if nothing displayed to the
712 * screen, put something up.
714 if (sp->rptlines[L_CHANGED] == 0 && !F_ISSET(sp, S_GLOBAL))
715 msgq(sp, M_INFO, "No match found.");
716 else if (!lflag && !nflag && !pflag)
717 F_SET(sp, S_AUTOPRINT);
719 FREE_SPACE(sp, bp, blen);
720 return (0);
722 ret1: FREE_SPACE(sp, bp, blen);
723 return (1);
727 * regsub --
728 * Do the substitution for a regular expression.
730 static inline int
731 regsub(sp, ip, lbp, lbclenp, lblenp)
732 SCR *sp;
733 char *ip; /* Input line. */
734 char **lbp;
735 size_t *lbclenp, *lblenp;
737 enum { C_NOTSET, C_LOWER, C_ONELOWER, C_ONEUPPER, C_UPPER } conv;
738 size_t lbclen, lblen; /* Local copies. */
739 size_t mlen; /* Match length. */
740 size_t rpl; /* Remaining replacement length. */
741 char *rp; /* Replacement pointer. */
742 int ch;
743 int no; /* Match replacement offset. */
744 char *p, *t; /* Buffer pointers. */
745 char *lb; /* Local copies. */
747 lb = *lbp; /* Get local copies. */
748 lbclen = *lbclenp;
749 lblen = *lblenp;
752 * QUOTING NOTE:
754 * There are some special sequences that vi provides in the
755 * replacement patterns.
756 * & string the RE matched (\& if nomagic set)
757 * \# n-th regular subexpression
758 * \E end \U, \L conversion
759 * \e end \U, \L conversion
760 * \l convert the next character to lower-case
761 * \L convert to lower-case, until \E, \e, or end of replacement
762 * \u convert the next character to upper-case
763 * \U convert to upper-case, until \E, \e, or end of replacement
765 * Otherwise, since this is the lowest level of replacement, discard
766 * all escape characters. This (hopefully) follows historic practice.
768 #define ADDCH(ch) { \
769 u_int __value; \
770 __value = term_key_val(sp, ch); \
771 if (__value == K_CR || __value == K_NL) { \
772 NEEDNEWLINE(sp); \
773 sp->newl[sp->newl_cnt++] = lbclen; \
774 } else if (conv != C_NOTSET) { \
775 switch (conv) { \
776 case C_ONELOWER: \
777 conv = C_NOTSET; \
778 /* FALLTHROUGH */ \
779 case C_LOWER: \
780 if (isupper(ch)) \
781 ch = tolower(ch); \
782 break; \
783 case C_ONEUPPER: \
784 conv = C_NOTSET; \
785 /* FALLTHROUGH */ \
786 case C_UPPER: \
787 if (islower(ch)) \
788 ch = toupper(ch); \
789 break; \
790 default: \
791 abort(); \
794 NEEDSP(sp, 1, p); \
795 *p++ = ch; \
796 ++lbclen; \
798 conv = C_NOTSET;
799 for (rp = sp->repl, rpl = sp->repl_len, p = lb + lbclen; rpl--;) {
800 switch (ch = *rp++) {
801 case '&':
802 if (O_ISSET(sp, O_MAGIC)) {
803 no = 0;
804 goto subzero;
806 break;
807 case '\\':
808 if (rpl == 0)
809 break;
810 --rpl;
811 switch (*rp) {
812 case '&':
813 if (!O_ISSET(sp, O_MAGIC)) {
814 ++rp;
815 no = 0;
816 goto subzero;
818 break;
819 case '0': case '1': case '2': case '3': case '4':
820 case '5': case '6': case '7': case '8': case '9':
821 no = *rp++ - '0';
822 subzero: if (sp->match[no].rm_so == -1 ||
823 sp->match[no].rm_eo == -1)
824 continue;
825 mlen =
826 sp->match[no].rm_eo - sp->match[no].rm_so;
827 for (t = ip + sp->match[no].rm_so; mlen--; ++t)
828 ADDCH(*t);
829 continue;
830 case 'e':
831 case 'E':
832 ++rp;
833 conv = C_NOTSET;
834 continue;
835 case 'l':
836 ++rp;
837 conv = C_ONELOWER;
838 continue;
839 case 'L':
840 ++rp;
841 conv = C_LOWER;
842 continue;
843 case 'u':
844 ++rp;
845 conv = C_ONEUPPER;
846 continue;
847 case 'U':
848 ++rp;
849 conv = C_UPPER;
850 continue;
851 default:
852 ++rp;
853 break;
856 ADDCH(ch);
859 *lbp = lb; /* Update caller's information. */
860 *lbclenp = lbclen;
861 *lblenp = lblen;
862 return (0);
865 static int
866 checkmatchsize(sp, re)
867 SCR *sp;
868 regex_t *re;
870 /* Build nsub array as necessary. */
871 if (sp->matchsize < re->re_nsub + 1) {
872 sp->matchsize = re->re_nsub + 1;
873 if ((sp->match = realloc(sp->match,
874 sp->matchsize * sizeof(regmatch_t))) == NULL) {
875 msgq(sp, M_SYSERR, NULL);
876 sp->matchsize = 0;
877 return (1);
880 return (0);