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