Annotation of src/usr.bin/sed/process.c, Revision 1.31
1.31 ! krw 1: /* $OpenBSD: process.c,v 1.30 2017/01/20 10:26:16 krw Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1992 Diomidis Spinellis.
5: * Copyright (c) 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Diomidis Spinellis of Imperial College, University of London.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.10 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #include <sys/types.h>
37: #include <sys/stat.h>
38: #include <sys/uio.h>
39:
40: #include <ctype.h>
41: #include <errno.h>
42: #include <fcntl.h>
43: #include <limits.h>
44: #include <regex.h>
45: #include <stdio.h>
46: #include <stdlib.h>
47: #include <string.h>
48: #include <unistd.h>
49:
50: #include "defs.h"
51: #include "extern.h"
52:
53: static SPACE HS, PS, SS;
54: #define pd PS.deleted
55: #define ps PS.space
56: #define psl PS.len
1.24 jasper 57: #define psanl PS.append_newline
1.1 deraadt 58: #define hs HS.space
59: #define hsl HS.len
60:
1.7 millert 61: static inline int applies(struct s_command *);
62: static void flush_appends(void);
63: static void lputs(char *);
1.28 martijn 64: static inline int regexec_e(regex_t *, const char *, int, int, size_t,
65: size_t);
1.7 millert 66: static void regsub(SPACE *, char *, char *);
67: static int substitute(struct s_command *);
1.1 deraadt 68:
69: struct s_appends *appends; /* Array of pointers to strings to append. */
70: static int appendx; /* Index into appends array. */
1.22 deraadt 71: size_t appendnum; /* Size of appends array. */
1.1 deraadt 72:
73: static int lastaddr; /* Set by applies if last address of a range. */
74: static int sdone; /* If any substitutes since last line input. */
75: /* Iov structure for 'w' commands. */
76: static regex_t *defpreg;
77: size_t maxnsub;
78: regmatch_t *match;
79:
1.24 jasper 80: #define OUT() do {\
81: fwrite(ps, 1, psl, outfile);\
82: if (psanl) fputc('\n', outfile);\
83: } while (0)
1.1 deraadt 84:
85: void
1.11 deraadt 86: process(void)
1.1 deraadt 87: {
88: struct s_command *cp;
89: SPACE tspace;
90: size_t len, oldpsl;
91: char *p;
1.24 jasper 92: int oldpsanl;
1.1 deraadt 93:
94: for (linenum = 0; mf_fgets(&PS, REPLACE);) {
95: pd = 0;
96: top:
97: cp = prog;
98: redirect:
99: while (cp != NULL) {
100: if (!applies(cp)) {
101: cp = cp->next;
102: continue;
103: }
104: switch (cp->code) {
105: case '{':
106: cp = cp->u.c;
107: goto redirect;
108: case 'a':
1.12 tedu 109: if (appendx >= appendnum) {
1.20 deraadt 110: appends = xreallocarray(appends,
1.22 deraadt 111: appendnum,
112: 2 * sizeof(struct s_appends));
1.12 tedu 113: appendnum *= 2;
114: }
1.1 deraadt 115: appends[appendx].type = AP_STRING;
116: appends[appendx].s = cp->t;
117: appends[appendx].len = strlen(cp->t);
118: appendx++;
119: break;
120: case 'b':
121: cp = cp->u.c;
122: goto redirect;
123: case 'c':
124: pd = 1;
125: psl = 0;
1.24 jasper 126: if (cp->a2 == NULL || lastaddr || lastline())
127: (void)fprintf(outfile, "%s", cp->t);
1.1 deraadt 128: break;
129: case 'd':
130: pd = 1;
131: goto new;
132: case 'D':
133: if (pd)
134: goto new;
1.8 millert 135: if (psl == 0 ||
1.24 jasper 136: (p = memchr(ps, '\n', psl)) == NULL) {
1.1 deraadt 137: pd = 1;
138: goto new;
139: } else {
140: psl -= (p + 1) - ps;
141: memmove(ps, p + 1, psl);
142: goto top;
143: }
144: case 'g':
145: cspace(&PS, hs, hsl, REPLACE);
146: break;
147: case 'G':
1.24 jasper 148: cspace(&PS, "\n", 1, 0);
1.1 deraadt 149: cspace(&PS, hs, hsl, 0);
150: break;
151: case 'h':
152: cspace(&HS, ps, psl, REPLACE);
153: break;
154: case 'H':
1.24 jasper 155: cspace(&HS, "\n", 1, 0);
1.1 deraadt 156: cspace(&HS, ps, psl, 0);
157: break;
158: case 'i':
1.24 jasper 159: (void)fprintf(outfile, "%s", cp->t);
1.1 deraadt 160: break;
161: case 'l':
162: lputs(ps);
163: break;
164: case 'n':
165: if (!nflag && !pd)
1.24 jasper 166: OUT();
1.1 deraadt 167: flush_appends();
168: if (!mf_fgets(&PS, REPLACE))
169: exit(0);
170: pd = 0;
171: break;
172: case 'N':
173: flush_appends();
1.24 jasper 174: cspace(&PS, "\n", 1, 0);
175: if (!mf_fgets(&PS, 0))
1.1 deraadt 176: exit(0);
177: break;
178: case 'p':
179: if (pd)
180: break;
1.24 jasper 181: OUT();
1.1 deraadt 182: break;
183: case 'P':
184: if (pd)
185: break;
1.24 jasper 186: if ((p = memchr(ps, '\n', psl)) != NULL) {
1.1 deraadt 187: oldpsl = psl;
1.24 jasper 188: oldpsanl = psanl;
189: psl = p - ps;
190: psanl = 1;
191: OUT();
1.21 jsg 192: psl = oldpsl;
193: } else {
1.24 jasper 194: OUT();
1.1 deraadt 195: }
196: break;
197: case 'q':
198: if (!nflag && !pd)
1.24 jasper 199: OUT();
1.1 deraadt 200: flush_appends();
201: exit(0);
202: case 'r':
1.22 deraadt 203: if (appendx >= appendnum) {
1.20 deraadt 204: appends = xreallocarray(appends,
1.22 deraadt 205: appendnum,
206: 2 * sizeof(struct s_appends));
207: appendnum *= 2;
208: }
1.1 deraadt 209: appends[appendx].type = AP_FILE;
210: appends[appendx].s = cp->t;
211: appends[appendx].len = strlen(cp->t);
212: appendx++;
213: break;
214: case 's':
215: sdone |= substitute(cp);
216: break;
217: case 't':
218: if (sdone) {
219: sdone = 0;
220: cp = cp->u.c;
221: goto redirect;
222: }
223: break;
224: case 'w':
225: if (pd)
226: break;
227: if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
228: O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
229: DEFFILEMODE)) == -1)
1.27 mmcc 230: error(FATAL, "%s: %s",
1.1 deraadt 231: cp->t, strerror(errno));
1.24 jasper 232: if (write(cp->u.fd, ps, psl) != psl ||
233: write(cp->u.fd, "\n", 1) != 1)
1.27 mmcc 234: error(FATAL, "%s: %s",
1.1 deraadt 235: cp->t, strerror(errno));
236: break;
237: case 'x':
238: if (hs == NULL)
1.24 jasper 239: cspace(&HS, "", 0, REPLACE);
1.1 deraadt 240: tspace = PS;
241: PS = HS;
1.24 jasper 242: psanl = tspace.append_newline;
1.1 deraadt 243: HS = tspace;
244: break;
245: case 'y':
1.8 millert 246: if (pd || psl == 0)
1.1 deraadt 247: break;
1.24 jasper 248: for (p = ps, len = psl; len--; ++p)
1.4 deraadt 249: *p = cp->u.y[(unsigned char)*p];
1.1 deraadt 250: break;
251: case ':':
252: case '}':
253: break;
254: case '=':
1.24 jasper 255: (void)fprintf(outfile, "%lu\n", linenum);
1.1 deraadt 256: }
257: cp = cp->next;
258: } /* for all cp */
259:
260: new: if (!nflag && !pd)
1.24 jasper 261: OUT();
1.1 deraadt 262: flush_appends();
263: } /* for all lines */
264: }
265:
266: /*
267: * TRUE if the address passed matches the current program state
268: * (lastline, linenumber, ps).
269: */
270: #define MATCH(a) \
1.28 martijn 271: (a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, 0, psl) : \
1.24 jasper 272: (a)->type == AT_LINE ? linenum == (a)->u.l : lastline()
1.1 deraadt 273:
274: /*
275: * Return TRUE if the command applies to the current line. Sets the inrange
276: * flag to process ranges. Interprets the non-select (``!'') flag.
277: */
278: static inline int
1.11 deraadt 279: applies(struct s_command *cp)
1.1 deraadt 280: {
281: int r;
282:
283: lastaddr = 0;
284: if (cp->a1 == NULL && cp->a2 == NULL)
285: r = 1;
286: else if (cp->a2)
287: if (cp->inrange) {
288: if (MATCH(cp->a2)) {
289: cp->inrange = 0;
290: lastaddr = 1;
291: }
292: r = 1;
293: } else if (MATCH(cp->a1)) {
294: /*
295: * If the second address is a number less than or
296: * equal to the line number first selected, only
297: * one line shall be selected.
298: * -- POSIX 1003.2
299: */
300: if (cp->a2->type == AT_LINE &&
301: linenum >= cp->a2->u.l)
302: lastaddr = 1;
303: else
304: cp->inrange = 1;
305: r = 1;
306: } else
307: r = 0;
308: else
309: r = MATCH(cp->a1);
1.13 tedu 310: return (cp->nonsel ? !r : r);
1.1 deraadt 311: }
312:
313: /*
1.24 jasper 314: * Reset all inrange markers.
315: */
316: void
317: resetranges(void)
318: {
319: struct s_command *cp;
320:
321: for (cp = prog; cp; cp = cp->code == '{' ? cp->u.c : cp->next)
322: if (cp->a2)
323: cp->inrange = 0;
324: }
325:
326: /*
1.1 deraadt 327: * substitute --
328: * Do substitutions in the pattern space. Currently, we build a
329: * copy of the new pattern space in the substitute space structure
330: * and then swap them.
331: */
332: static int
1.11 deraadt 333: substitute(struct s_command *cp)
1.1 deraadt 334: {
335: SPACE tspace;
336: regex_t *re;
1.18 schwarze 337: regoff_t slen;
1.1 deraadt 338: int n, lastempty;
1.28 martijn 339: size_t le = 0;
1.1 deraadt 340: char *s;
341:
342: s = ps;
343: re = cp->u.s->re;
344: if (re == NULL) {
345: if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
346: linenum = cp->u.s->linenum;
1.27 mmcc 347: error(COMPILE, "\\%d not defined in the RE",
1.1 deraadt 348: cp->u.s->maxbref);
349: }
350: }
1.28 martijn 351: if (!regexec_e(re, ps, 0, 0, 0, psl))
1.1 deraadt 352: return (0);
353:
354: SS.len = 0; /* Clean substitute space. */
355: slen = psl;
356: n = cp->u.s->n;
357: lastempty = 1;
358:
1.18 schwarze 359: do {
360: /* Copy the leading retained string. */
1.28 martijn 361: if (n <= 1 && (match[0].rm_so > le))
362: cspace(&SS, s, match[0].rm_so - le, APPEND);
1.18 schwarze 363:
364: /* Skip zero-length matches right after other matches. */
1.28 martijn 365: if (lastempty || (match[0].rm_so - le) ||
1.18 schwarze 366: match[0].rm_so != match[0].rm_eo) {
367: if (n <= 1) {
368: /* Want this match: append replacement. */
1.28 martijn 369: regsub(&SS, ps, cp->u.s->new);
1.18 schwarze 370: if (n == 1)
371: n = -1;
1.1 deraadt 372: } else {
1.18 schwarze 373: /* Want a later match: append original. */
1.28 martijn 374: if (match[0].rm_eo - le)
375: cspace(&SS, s, match[0].rm_eo - le,
376: APPEND);
1.18 schwarze 377: n--;
1.1 deraadt 378: }
379: }
1.18 schwarze 380:
381: /* Move past this match. */
1.28 martijn 382: s = ps + match[0].rm_eo;
383: slen = psl - match[0].rm_eo;
384: le = match[0].rm_eo;
1.18 schwarze 385:
386: /*
387: * After a zero-length match, advance one byte,
388: * and at the end of the line, terminate.
389: */
390: if (match[0].rm_so == match[0].rm_eo) {
391: if (*s == '\0' || *s == '\n')
392: slen = -1;
393: else
394: slen--;
1.28 martijn 395: if (*s != '\0') {
1.31 ! krw 396: cspace(&SS, s++, 1, APPEND);
1.28 martijn 397: le++;
398: }
1.18 schwarze 399: lastempty = 1;
400: } else
401: lastempty = 0;
402:
1.28 martijn 403: } while (n >= 0 && slen >= 0 &&
404: regexec_e(re, ps, REG_NOTBOL, 0, le, psl));
1.18 schwarze 405:
406: /* Did not find the requested number of matches. */
1.29 martijn 407: if (n > 0)
1.18 schwarze 408: return (0);
409:
410: /* Copy the trailing retained string. */
411: if (slen > 0)
1.1 deraadt 412: cspace(&SS, s, slen, APPEND);
413:
414: /*
415: * Swap the substitute space and the pattern space, and make sure
416: * that any leftover pointers into stdio memory get lost.
417: */
418: tspace = PS;
419: PS = SS;
1.24 jasper 420: psanl = tspace.append_newline;
1.1 deraadt 421: SS = tspace;
422: SS.space = SS.back;
423:
424: /* Handle the 'p' flag. */
425: if (cp->u.s->p)
1.24 jasper 426: OUT();
1.1 deraadt 427:
428: /* Handle the 'w' flag. */
429: if (cp->u.s->wfile && !pd) {
430: if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
431: O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
1.27 mmcc 432: error(FATAL, "%s: %s", cp->u.s->wfile, strerror(errno));
1.24 jasper 433: if (write(cp->u.s->wfd, ps, psl) != psl ||
434: write(cp->u.s->wfd, "\n", 1) != 1)
1.27 mmcc 435: error(FATAL, "%s: %s", cp->u.s->wfile, strerror(errno));
1.1 deraadt 436: }
437: return (1);
438: }
439:
440: /*
441: * Flush append requests. Always called before reading a line,
442: * therefore it also resets the substitution done (sdone) flag.
443: */
444: static void
1.11 deraadt 445: flush_appends(void)
1.1 deraadt 446: {
447: FILE *f;
448: int count, i;
449: char buf[8 * 1024];
450:
1.26 jasper 451: for (i = 0; i < appendx; i++)
1.1 deraadt 452: switch (appends[i].type) {
453: case AP_STRING:
1.26 jasper 454: fwrite(appends[i].s, sizeof(char), appends[i].len,
1.24 jasper 455: outfile);
1.1 deraadt 456: break;
457: case AP_FILE:
458: /*
459: * Read files probably shouldn't be cached. Since
460: * it's not an error to read a non-existent file,
461: * it's possible that another program is interacting
462: * with the sed script through the file system. It
463: * would be truly bizarre, but possible. It's probably
464: * not that big a performance win, anyhow.
465: */
466: if ((f = fopen(appends[i].s, "r")) == NULL)
467: break;
1.3 deraadt 468: while ((count = fread(buf, sizeof(char), sizeof(buf), f)))
1.24 jasper 469: (void)fwrite(buf, sizeof(char), count, outfile);
1.1 deraadt 470: (void)fclose(f);
471: break;
472: }
1.24 jasper 473: if (ferror(outfile))
1.27 mmcc 474: error(FATAL, "%s: %s", outfname, strerror(errno ? errno : EIO));
1.1 deraadt 475: appendx = sdone = 0;
476: }
477:
478: static void
1.11 deraadt 479: lputs(char *s)
1.1 deraadt 480: {
1.6 mpech 481: int count;
1.25 deraadt 482: extern int termwidth;
1.24 jasper 483: const char *escapes;
484: char *p;
1.1 deraadt 485:
1.26 jasper 486: for (count = 0; *s; ++s) {
1.1 deraadt 487: if (count >= termwidth) {
1.24 jasper 488: (void)fprintf(outfile, "\\\n");
1.1 deraadt 489: count = 0;
490: }
1.19 deraadt 491: if (isascii((unsigned char)*s) && isprint((unsigned char)*s)
492: && *s != '\\') {
1.24 jasper 493: (void)fputc(*s, outfile);
1.1 deraadt 494: count++;
1.24 jasper 495: } else if (*s == '\n') {
496: (void)fputc('$', outfile);
497: (void)fputc('\n', outfile);
498: count = 0;
499: } else {
1.14 millert 500: escapes = "\\\a\b\f\r\t\v";
1.24 jasper 501: (void)fputc('\\', outfile);
1.3 deraadt 502: if ((p = strchr(escapes, *s))) {
1.24 jasper 503: (void)fputc("\\abfrtv"[p - escapes], outfile);
1.1 deraadt 504: count += 2;
505: } else {
1.24 jasper 506: (void)fprintf(outfile, "%03o", *(u_char *)s);
1.1 deraadt 507: count += 4;
508: }
509: }
510: }
1.24 jasper 511: (void)fputc('$', outfile);
512: (void)fputc('\n', outfile);
513: if (ferror(outfile))
1.27 mmcc 514: error(FATAL, "%s: %s", outfname, strerror(errno ? errno : EIO));
1.1 deraadt 515: }
516:
517: static inline int
1.11 deraadt 518: regexec_e(regex_t *preg, const char *string, int eflags,
1.28 martijn 519: int nomatch, size_t start, size_t stop)
1.1 deraadt 520: {
521: int eval;
1.26 jasper 522:
1.1 deraadt 523: if (preg == NULL) {
524: if (defpreg == NULL)
1.27 mmcc 525: error(FATAL, "first RE may not be empty");
1.1 deraadt 526: } else
527: defpreg = preg;
528:
1.24 jasper 529: /* Set anchors */
1.28 martijn 530: match[0].rm_so = start;
531: match[0].rm_eo = stop;
1.26 jasper 532:
1.1 deraadt 533: eval = regexec(defpreg, string,
534: nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
1.13 tedu 535: switch (eval) {
1.1 deraadt 536: case 0:
537: return (1);
538: case REG_NOMATCH:
539: return (0);
540: }
1.27 mmcc 541: error(FATAL, "RE error: %s", strregerror(eval, defpreg));
1.1 deraadt 542: }
543:
544: /*
545: * regsub - perform substitutions after a regexp match
546: * Based on a routine by Henry Spencer
547: */
548: static void
1.11 deraadt 549: regsub(SPACE *sp, char *string, char *src)
1.1 deraadt 550: {
1.6 mpech 551: int len, no;
552: char c, *dst;
1.1 deraadt 553:
554: #define NEEDSP(reqlen) \
1.12 tedu 555: if (sp->len + (reqlen) + 1 >= sp->blen) { \
556: size_t newlen = sp->blen + (reqlen) + 1024; \
557: sp->space = sp->back = xrealloc(sp->back, newlen); \
558: sp->blen = newlen; \
1.1 deraadt 559: dst = sp->space + sp->len; \
560: }
561:
562: dst = sp->space + sp->len;
563: while ((c = *src++) != '\0') {
564: if (c == '&')
565: no = 0;
1.19 deraadt 566: else if (c == '\\' && isdigit((unsigned char)*src))
1.1 deraadt 567: no = *src++ - '0';
568: else
569: no = -1;
570: if (no < 0) { /* Ordinary character. */
1.31 ! krw 571: if (c == '\\' && (*src == '\\' || *src == '&'))
! 572: c = *src++;
1.1 deraadt 573: NEEDSP(1);
1.31 ! krw 574: *dst++ = c;
1.1 deraadt 575: ++sp->len;
1.31 ! krw 576: } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) {
1.1 deraadt 577: len = match[no].rm_eo - match[no].rm_so;
578: NEEDSP(len);
579: memmove(dst, string + match[no].rm_so, len);
580: dst += len;
581: sp->len += len;
582: }
583: }
584: NEEDSP(1);
585: *dst = '\0';
586: }
587:
588: /*
589: * aspace --
590: * Append the source space to the destination space, allocating new
591: * space as necessary.
592: */
593: void
1.24 jasper 594: cspace(SPACE *sp, const char *p, size_t len, enum e_spflag spflag)
1.1 deraadt 595: {
596: size_t tlen;
597:
598: /* Make sure SPACE has enough memory and ramp up quickly. */
599: tlen = sp->len + len + 1;
600: if (tlen > sp->blen) {
1.12 tedu 601: size_t newlen = tlen + 1024;
602: sp->space = sp->back = xrealloc(sp->back, newlen);
603: sp->blen = newlen;
1.1 deraadt 604: }
605:
606: if (spflag == REPLACE)
607: sp->len = 0;
608:
609: memmove(sp->space + sp->len, p, len);
610:
611: sp->space[sp->len += len] = '\0';
612: }
613:
614: /*
615: * Close all cached opened files and report any errors
616: */
617: void
1.11 deraadt 618: cfclose(struct s_command *cp, struct s_command *end)
1.1 deraadt 619: {
620:
621: for (; cp != end; cp = cp->next)
1.13 tedu 622: switch (cp->code) {
1.1 deraadt 623: case 's':
624: if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
1.27 mmcc 625: error(FATAL,
1.1 deraadt 626: "%s: %s", cp->u.s->wfile, strerror(errno));
627: cp->u.s->wfd = -1;
628: break;
629: case 'w':
630: if (cp->u.fd != -1 && close(cp->u.fd))
1.27 mmcc 631: error(FATAL, "%s: %s", cp->t, strerror(errno));
1.1 deraadt 632: cp->u.fd = -1;
633: break;
634: case '{':
635: cfclose(cp->u.c, cp->next);
636: break;
637: }
638: }