Index: stable/3/usr.bin/sed/compile.c =================================================================== --- stable/3/usr.bin/sed/compile.c (revision 89997) +++ stable/3/usr.bin/sed/compile.c (revision 89998) @@ -1,826 +1,826 @@ /*- * Copyright (c) 1992 Diomidis Spinellis. * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Diomidis Spinellis of Imperial College, University of London. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint #if 0 static char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include "defs.h" #include "extern.h" #define LHSZ 128 #define LHMASK (LHSZ - 1) static struct labhash { struct labhash *lh_next; u_int lh_hash; struct s_command *lh_cmd; int lh_ref; } *labels[LHSZ]; static char *compile_addr __P((char *, struct s_addr *)); static char *compile_ccl __P((char **, char *)); static char *compile_delimited __P((char *, char *)); static char *compile_flags __P((char *, struct s_subst *)); static char *compile_re __P((char *, regex_t **)); static char *compile_subst __P((char *, struct s_subst *)); static char *compile_text __P((void)); static char *compile_tr __P((char *, char **)); static struct s_command **compile_stream __P((struct s_command **)); static char *duptoeol __P((char *, char *)); static void enterlabel __P((struct s_command *)); static struct s_command *findlabel __P((char *)); static void fixuplabel __P((struct s_command *, struct s_command *)); static void uselabel __P((void)); /* * Command specification. This is used to drive the command parser. */ struct s_format { char code; /* Command code */ int naddr; /* Number of address args */ enum e_args args; /* Argument type */ }; static struct s_format cmd_fmts[] = { {'{', 2, GROUP}, {'}', 0, ENDGROUP}, {'a', 1, TEXT}, {'b', 2, BRANCH}, {'c', 2, TEXT}, {'d', 2, EMPTY}, {'D', 2, EMPTY}, {'g', 2, EMPTY}, {'G', 2, EMPTY}, {'h', 2, EMPTY}, {'H', 2, EMPTY}, {'i', 1, TEXT}, {'l', 2, EMPTY}, {'n', 2, EMPTY}, {'N', 2, EMPTY}, {'p', 2, EMPTY}, {'P', 2, EMPTY}, {'q', 1, EMPTY}, {'r', 1, RFILE}, {'s', 2, SUBST}, {'t', 2, BRANCH}, {'w', 2, WFILE}, {'x', 2, EMPTY}, {'y', 2, TR}, {'!', 2, NONSEL}, {':', 0, LABEL}, {'#', 0, COMMENT}, {'=', 1, EMPTY}, {'\0', 0, COMMENT}, }; /* The compiled program. */ struct s_command *prog; /* * Compile the program into prog. * Initialise appends. */ void compile() { *compile_stream(&prog) = NULL; fixuplabel(prog, NULL); uselabel(); appends = xmalloc(sizeof(struct s_appends) * appendnum); match = xmalloc((maxnsub + 1) * sizeof(regmatch_t)); } #define EATSPACE() do { \ if (p) \ while (*p && isspace((unsigned char)*p)) \ p++; \ } while (0) static struct s_command ** compile_stream(link) struct s_command **link; { register char *p; static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ struct s_command *cmd, *cmd2, *stack; struct s_format *fp; int naddr; /* Number of addresses */ stack = 0; for (;;) { if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) { if (stack != 0) errx(1, "%lu: %s: unexpected EOF (pending }'s)", linenum, fname); return (link); } semicolon: EATSPACE(); if (p && (*p == '#' || *p == '\0')) continue; *link = cmd = xmalloc(sizeof(struct s_command)); link = &cmd->next; cmd->nonsel = cmd->inrange = 0; /* First parse the addresses */ naddr = 0; /* Valid characters to start an address */ #define addrchar(c) (strchr("0123456789/\\$", (c))) if (addrchar(*p)) { naddr++; cmd->a1 = xmalloc(sizeof(struct s_addr)); p = compile_addr(p, cmd->a1); EATSPACE(); /* EXTENSION */ if (*p == ',') { p++; EATSPACE(); /* EXTENSION */ naddr++; cmd->a2 = xmalloc(sizeof(struct s_addr)); p = compile_addr(p, cmd->a2); EATSPACE(); } else cmd->a2 = 0; } else cmd->a1 = cmd->a2 = 0; nonsel: /* Now parse the command */ if (!*p) errx(1, "%lu: %s: command expected", linenum, fname); cmd->code = *p; for (fp = cmd_fmts; fp->code; fp++) if (fp->code == *p) break; if (!fp->code) errx(1, "%lu: %s: invalid command code %c", linenum, fname, *p); if (naddr > fp->naddr) errx(1, "%lu: %s: command %c expects up to %d address(es), found %d", linenum, fname, *p, fp->naddr, naddr); switch (fp->args) { case NONSEL: /* ! */ p++; EATSPACE(); cmd->nonsel = ! cmd->nonsel; goto nonsel; case GROUP: /* { */ p++; EATSPACE(); cmd->next = stack; stack = cmd; link = &cmd->u.c; if (*p) goto semicolon; break; case ENDGROUP: /* * Short-circuit command processing, since end of * group is really just a noop. */ cmd->nonsel = 1; if (stack == 0) errx(1, "%lu: %s: unexpected }", linenum, fname); cmd2 = stack; stack = cmd2->next; cmd2->next = cmd; /*FALLTHROUGH*/ case EMPTY: /* d D g G h H l n N p P q x = \0 */ p++; EATSPACE(); if (*p == ';') { p++; link = &cmd->next; goto semicolon; } if (*p) errx(1, "%lu: %s: extra characters at the end of %c command", linenum, fname, cmd->code); break; case TEXT: /* a c i */ p++; EATSPACE(); if (*p != '\\') errx(1, "%lu: %s: command %c expects \\ followed by text", linenum, fname, cmd->code); p++; EATSPACE(); if (*p) errx(1, "%lu: %s: extra characters after \\ at the end of %c command", linenum, fname, cmd->code); cmd->t = compile_text(); break; case COMMENT: /* \0 # */ break; case WFILE: /* w */ p++; EATSPACE(); if (*p == '\0') errx(1, "%lu: %s: filename expected", linenum, fname); cmd->t = duptoeol(p, "w command"); if (aflag) cmd->u.fd = -1; else if ((cmd->u.fd = open(p, O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1) err(1, "%s", p); break; case RFILE: /* r */ p++; EATSPACE(); if (*p == '\0') errx(1, "%lu: %s: filename expected", linenum, fname); else cmd->t = duptoeol(p, "read command"); break; case BRANCH: /* b t */ p++; EATSPACE(); if (*p == '\0') cmd->t = NULL; else cmd->t = duptoeol(p, "branch"); break; case LABEL: /* : */ p++; EATSPACE(); cmd->t = duptoeol(p, "label"); if (strlen(p) == 0) errx(1, "%lu: %s: empty label", linenum, fname); enterlabel(cmd); break; case SUBST: /* s */ p++; if (*p == '\0' || *p == '\\') errx(1, "%lu: %s: substitute pattern can not be delimited by newline or backslash", linenum, fname); cmd->u.s = xmalloc(sizeof(struct s_subst)); p = compile_re(p, &cmd->u.s->re); if (p == NULL) errx(1, "%lu: %s: unterminated substitute pattern", linenum, fname); --p; p = compile_subst(p, cmd->u.s); p = compile_flags(p, cmd->u.s); EATSPACE(); if (*p == ';') { p++; link = &cmd->next; goto semicolon; } break; case TR: /* y */ p++; p = compile_tr(p, (char **)&cmd->u.y); EATSPACE(); if (*p == ';') { p++; link = &cmd->next; goto semicolon; } if (*p) errx(1, "%lu: %s: extra text at the end of a transform command", linenum, fname); break; } } } /* * Get a delimited string. P points to the delimeter of the string; d points * to a buffer area. Newline and delimiter escapes are processed; other * escapes are ignored. * * Returns a pointer to the first character after the final delimiter or NULL * in the case of a non-terminated string. The character array d is filled * with the processed string. */ static char * compile_delimited(p, d) char *p, *d; { char c; c = *p++; if (c == '\0') return (NULL); else if (c == '\\') errx(1, "%lu: %s: \\ can not be used as a string delimiter", linenum, fname); else if (c == '\n') errx(1, "%lu: %s: newline can not be used as a string delimiter", linenum, fname); while (*p) { if (*p == '[') { if ((d = compile_ccl(&p, d)) == NULL) errx(1, "%lu: %s: unbalanced brackets ([])", linenum, fname); continue; } else if (*p == '\\' && p[1] == '[') { *d++ = *p++; } else if (*p == '\\' && p[1] == c) p++; else if (*p == '\\' && p[1] == 'n') { *d++ = '\n'; p += 2; continue; } else if (*p == '\\' && p[1] == '\\') *d++ = *p++; else if (*p == c) { *d = '\0'; return (p + 1); } *d++ = *p++; } return (NULL); } /* compile_ccl: expand a POSIX character class */ static char * compile_ccl(sp, t) char **sp; char *t; { int c, d; char *s = *sp; *t++ = *s++; if (*s == '^') *t++ = *s++; if (*s == ']') *t++ = *s++; for (; *s && (*t = *s) != ']'; s++, t++) if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) { *++t = *++s, t++, s++; for (c = *s; (*t = *s) != ']' || c != d; s++, t++) if ((c = *s) == '\0') return NULL; } else if (*s == '\\' && s[1] == 'n') *t = '\n', s++; return (*s == ']') ? *sp = ++s, ++t : NULL; } /* * Get a regular expression. P points to the delimiter of the regular * expression; repp points to the address of a regexp pointer. Newline * and delimiter escapes are processed; other escapes are ignored. * Returns a pointer to the first character after the final delimiter * or NULL in the case of a non terminated regular expression. The regexp * pointer is set to the compiled regular expression. * Cflags are passed to regcomp. */ static char * compile_re(p, repp) char *p; regex_t **repp; { int eval; char re[_POSIX2_LINE_MAX + 1]; p = compile_delimited(p, re); if (p && strlen(re) == 0) { *repp = NULL; return (p); } *repp = xmalloc(sizeof(regex_t)); - if (p && (eval = regcomp(*repp, re, 0)) != 0) + if (p && (eval = regcomp(*repp, re, rflags)) != 0) errx(1, "%lu: %s: RE error: %s", linenum, fname, strregerror(eval, *repp)); if (maxnsub < (*repp)->re_nsub) maxnsub = (*repp)->re_nsub; return (p); } /* * Compile the substitution string of a regular expression and set res to * point to a saved copy of it. Nsub is the number of parenthesized regular * expressions. */ static char * compile_subst(p, s) char *p; struct s_subst *s; { static char lbuf[_POSIX2_LINE_MAX + 1]; int asize, ref, size; char c, *text, *op, *sp; int more = 0; c = *p++; /* Terminator character */ if (c == '\0') return (NULL); s->maxbref = 0; s->linenum = linenum; asize = 2 * _POSIX2_LINE_MAX + 1; text = xmalloc(asize); size = 0; do { op = sp = text + size; for (; *p; p++) { if (*p == '\\') { p++; if (strchr("123456789", *p) != NULL) { *sp++ = '\\'; ref = *p - '0'; if (s->re != NULL && ref > s->re->re_nsub) errx(1, "%lu: %s: \\%c not defined in the RE", linenum, fname, *p); if (s->maxbref < ref) s->maxbref = ref; } else if (*p == '&' || *p == '\\') *sp++ = '\\'; } else if (*p == c) { if (*++p == '\0' && more) { if (cu_fgets(lbuf, sizeof(lbuf), &more)) p = lbuf; } *sp++ = '\0'; size += sp - op; s->new = xrealloc(text, size); return (p); } else if (*p == '\n') { errx(1, "%lu: %s: unescaped newline inside substitute pattern", linenum, fname); /* NOTREACHED */ } *sp++ = *p; } size += sp - op; if (asize - size < _POSIX2_LINE_MAX + 1) { asize *= 2; text = xrealloc(text, asize); } } while (cu_fgets(p = lbuf, sizeof(lbuf), &more)); errx(1, "%lu: %s: unterminated substitute in regular expression", linenum, fname); /* NOTREACHED */ } /* * Compile the flags of the s command */ static char * compile_flags(p, s) char *p; struct s_subst *s; { int gn; /* True if we have seen g or n */ char wfile[_POSIX2_LINE_MAX + 1], *q; s->n = 1; /* Default */ s->p = 0; s->wfile = NULL; s->wfd = -1; for (gn = 0;;) { EATSPACE(); /* EXTENSION */ switch (*p) { case 'g': if (gn) errx(1, "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); gn = 1; s->n = 0; break; case '\0': case '\n': case ';': return (p); case 'p': s->p = 1; break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if (gn) errx(1, "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); gn = 1; /* XXX Check for overflow */ s->n = (int)strtol(p, &p, 10); break; case 'w': p++; #ifdef HISTORIC_PRACTICE if (*p != ' ') { warnx("%lu: %s: space missing before w wfile", linenum, fname); return (p); } #endif EATSPACE(); q = wfile; while (*p) { if (*p == '\n') break; *q++ = *p++; } *q = '\0'; if (q == wfile) errx(1, "%lu: %s: no wfile specified", linenum, fname); s->wfile = strdup(wfile); if (!aflag && (s->wfd = open(wfile, O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1) err(1, "%s", wfile); return (p); default: errx(1, "%lu: %s: bad flag in substitute command: '%c'", linenum, fname, *p); break; } p++; } } /* * Compile a translation set of strings into a lookup table. */ static char * compile_tr(p, transtab) char *p; char **transtab; { int i; char *lt, *op, *np; char old[_POSIX2_LINE_MAX + 1]; char new[_POSIX2_LINE_MAX + 1]; if (*p == '\0' || *p == '\\') errx(1, "%lu: %s: transform pattern can not be delimited by newline or backslash", linenum, fname); p = compile_delimited(p, old); if (p == NULL) errx(1, "%lu: %s: unterminated transform source string", linenum, fname); p = compile_delimited(--p, new); if (p == NULL) errx(1, "%lu: %s: unterminated transform target string", linenum, fname); EATSPACE(); if (strlen(new) != strlen(old)) errx(1, "%lu: %s: transform strings are not the same length", linenum, fname); /* We assume characters are 8 bits */ lt = xmalloc(UCHAR_MAX); for (i = 0; i <= UCHAR_MAX; i++) lt[i] = (char)i; for (op = old, np = new; *op; op++, np++) lt[(u_char)*op] = *np; *transtab = lt; return (p); } /* * Compile the text following an a or i command. */ static char * compile_text() { int asize, esc_nl, size; char *text, *p, *op, *s; char lbuf[_POSIX2_LINE_MAX + 1]; asize = 2 * _POSIX2_LINE_MAX + 1; text = xmalloc(asize); size = 0; while (cu_fgets(lbuf, sizeof(lbuf), NULL)) { op = s = text + size; p = lbuf; EATSPACE(); for (esc_nl = 0; *p != '\0'; p++) { if (*p == '\\' && p[1] != '\0' && *++p == '\n') esc_nl = 1; *s++ = *p; } size += s - op; if (!esc_nl) { *s = '\0'; break; } if (asize - size < _POSIX2_LINE_MAX + 1) { asize *= 2; text = xmalloc(asize); } } text[size] = '\0'; return (xrealloc(text, size + 1)); } /* * Get an address and return a pointer to the first character after * it. Fill the structure pointed to according to the address. */ static char * compile_addr(p, a) char *p; struct s_addr *a; { char *end; switch (*p) { case '\\': /* Context address */ ++p; /* FALLTHROUGH */ case '/': /* Context address */ p = compile_re(p, &a->u.r); if (p == NULL) errx(1, "%lu: %s: unterminated regular expression", linenum, fname); a->type = AT_RE; return (p); case '$': /* Last line */ a->type = AT_LAST; return (p + 1); /* Line number */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': a->type = AT_LINE; a->u.l = strtol(p, &end, 10); return (end); default: errx(1, "%lu: %s: expected context address", linenum, fname); return (NULL); } } /* * duptoeol -- * Return a copy of all the characters up to \n or \0. */ static char * duptoeol(s, ctype) register char *s; char *ctype; { size_t len; int ws; char *start; ws = 0; for (start = s; *s != '\0' && *s != '\n'; ++s) ws = isspace((unsigned char)*s); *s = '\0'; if (ws) warnx("%lu: %s: whitespace after %s", linenum, fname, ctype); len = s - start + 1; return (memmove(xmalloc(len), start, len)); } /* * Convert goto label names to addresses, and count a and r commands, in * the given subset of the script. Free the memory used by labels in b * and t commands (but not by :). * * TODO: Remove } nodes */ static void fixuplabel(cp, end) struct s_command *cp, *end; { for (; cp != end; cp = cp->next) switch (cp->code) { case 'a': case 'r': appendnum++; break; case 'b': case 't': /* Resolve branch target. */ if (cp->t == NULL) { cp->u.c = NULL; break; } if ((cp->u.c = findlabel(cp->t)) == NULL) errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t); free(cp->t); break; case '{': /* Do interior commands. */ fixuplabel(cp->u.c, cp->next); break; } } /* * Associate the given command label for later lookup. */ static void enterlabel(cp) struct s_command *cp; { register struct labhash **lhp, *lh; register u_char *p; register u_int h, c; for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++) h = (h << 5) + h + c; lhp = &labels[h & LHMASK]; for (lh = *lhp; lh != NULL; lh = lh->lh_next) if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0) errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t); lh = xmalloc(sizeof *lh); lh->lh_next = *lhp; lh->lh_hash = h; lh->lh_cmd = cp; lh->lh_ref = 0; *lhp = lh; } /* * Find the label contained in the command l in the command linked * list cp. L is excluded from the search. Return NULL if not found. */ static struct s_command * findlabel(name) char *name; { register struct labhash *lh; register u_char *p; register u_int h, c; for (h = 0, p = (u_char *)name; (c = *p) != 0; p++) h = (h << 5) + h + c; for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) { if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) { lh->lh_ref = 1; return (lh->lh_cmd); } } return (NULL); } /* * Warn about any unused labels. As a side effect, release the label hash * table space. */ static void uselabel() { register struct labhash *lh, *next; register int i; for (i = 0; i < LHSZ; i++) { for (lh = labels[i]; lh != NULL; lh = next) { next = lh->lh_next; if (!lh->lh_ref) warnx("%lu: %s: unused label '%s'", linenum, fname, lh->lh_cmd->t); free(lh); } } } Index: stable/3/usr.bin/sed/extern.h =================================================================== --- stable/3/usr.bin/sed/extern.h (revision 89997) +++ stable/3/usr.bin/sed/extern.h (revision 89998) @@ -1,58 +1,60 @@ /*- * Copyright (c) 1992 Diomidis Spinellis. * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Diomidis Spinellis of Imperial College, University of London. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)extern.h 8.1 (Berkeley) 6/6/93 + * From: @(#)extern.h 8.1 (Berkeley) 6/6/93 + * $FreeBSD$ */ extern struct s_command *prog; extern struct s_appends *appends; extern regmatch_t *match; extern size_t maxnsub; extern u_long linenum; extern int appendnum; extern int lastline; extern int aflag, eflag, nflag; extern char *fname; +extern int rflags; /* regex flags to add */ void cfclose __P((struct s_command *, struct s_command *)); void compile __P((void)); void cspace __P((SPACE *, char *, size_t, enum e_spflag)); char *cu_fgets __P((char *, int, int *)); int mf_fgets __P((SPACE *, enum e_spflag)); void process __P((void)); char *strregerror __P((int, regex_t *)); void *xmalloc __P((u_int)); void *xrealloc __P((void *, u_int)); Index: stable/3/usr.bin/sed/main.c =================================================================== --- stable/3/usr.bin/sed/main.c (revision 89997) +++ stable/3/usr.bin/sed/main.c (revision 89998) @@ -1,383 +1,387 @@ /*- * Copyright (c) 1992 Diomidis Spinellis. * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Diomidis Spinellis of Imperial College, University of London. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1992, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include "defs.h" #include "extern.h" /* * Linked list of units (strings and files) to be compiled */ struct s_compunit { struct s_compunit *next; enum e_cut {CU_FILE, CU_STRING} type; char *s; /* Pointer to string or fname */ }; /* * Linked list pointer to compilation units and pointer to current * next pointer. */ static struct s_compunit *script, **cu_nextp = &script; /* * Linked list of files to be processed */ struct s_flist { char *fname; struct s_flist *next; }; /* * Linked list pointer to files and pointer to current * next pointer. */ static struct s_flist *files, **fl_nextp = &files; int aflag, eflag, nflag; +int rflags = 0; /* * Current file and line number; line numbers restart across compilation * units, but span across input files. */ char *fname; /* File name. */ u_long linenum; int lastline; /* TRUE on the last line of the last file */ static void add_compunit __P((enum e_cut, char *)); static void add_file __P((char *)); static void usage __P((void)); int main(argc, argv) int argc; char *argv[]; { int c, fflag; char *temp_arg; (void) setlocale(LC_ALL, ""); fflag = 0; - while ((c = getopt(argc, argv, "ae:f:n")) != -1) + while ((c = getopt(argc, argv, "Eae:f:n")) != -1) switch (c) { + case 'E': + rflags = REG_EXTENDED; + break; case 'a': aflag = 1; break; case 'e': eflag = 1; temp_arg = xmalloc(strlen(optarg) + 2); strcpy(temp_arg, optarg); strcat(temp_arg, "\n"); add_compunit(CU_STRING, temp_arg); break; case 'f': fflag = 1; add_compunit(CU_FILE, optarg); break; case 'n': nflag = 1; break; default: case '?': usage(); } argc -= optind; argv += optind; /* First usage case; script is the first arg */ if (!eflag && !fflag && *argv) { add_compunit(CU_STRING, *argv); argv++; } compile(); /* Continue with first and start second usage */ if (*argv) for (; *argv; argv++) add_file(*argv); else add_file(NULL); process(); cfclose(prog, NULL); if (fclose(stdout)) err(1, "stdout"); exit (0); } static void usage() { (void)fprintf(stderr, "%s\n%s\n", - "usage: sed script [-an] [file ...]", + "usage: sed script [-Ean] [file ...]", " sed [-an] [-e script] ... [-f script_file] ... [file ...]"); exit(1); } /* * Like fgets, but go through the chain of compilation units chaining them * together. Empty strings and files are ignored. */ char * cu_fgets(buf, n, more) char *buf; int n; int *more; { static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; static FILE *f; /* Current open file */ static char *s; /* Current pointer inside string */ static char string_ident[30]; char *p; again: switch (state) { case ST_EOF: if (script == NULL) { if (more != NULL) *more = 0; return (NULL); } linenum = 0; switch (script->type) { case CU_FILE: if ((f = fopen(script->s, "r")) == NULL) err(1, "%s", script->s); fname = script->s; state = ST_FILE; goto again; case CU_STRING: if ((snprintf(string_ident, sizeof(string_ident), "\"%s\"", script->s)) >= sizeof(string_ident) - 1) (void)strcpy(string_ident + sizeof(string_ident) - 6, " ...\""); fname = string_ident; s = script->s; state = ST_STRING; goto again; } case ST_FILE: if ((p = fgets(buf, n, f)) != NULL) { linenum++; if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') nflag = 1; if (more != NULL) *more = !feof(f); return (p); } script = script->next; (void)fclose(f); state = ST_EOF; goto again; case ST_STRING: if (linenum == 0 && s[0] == '#' && s[1] == 'n') nflag = 1; p = buf; for (;;) { if (n-- <= 1) { *p = '\0'; linenum++; if (more != NULL) *more = 1; return (buf); } switch (*s) { case '\0': state = ST_EOF; if (s == script->s) { script = script->next; goto again; } else { script = script->next; *p = '\0'; linenum++; if (more != NULL) *more = 0; return (buf); } case '\n': *p++ = '\n'; *p = '\0'; s++; linenum++; if (more != NULL) *more = 0; return (buf); default: *p++ = *s++; } } } /* NOTREACHED */ return (NULL); } /* * Like fgets, but go through the list of files chaining them together. * Set len to the length of the line. */ int mf_fgets(sp, spflag) SPACE *sp; enum e_spflag spflag; { static FILE *f; /* Current open file */ size_t len; char *p; int c; if (f == NULL) /* Advance to first non-empty file */ for (;;) { if (files == NULL) { lastline = 1; return (0); } if (files->fname == NULL) { f = stdin; fname = "stdin"; } else { fname = files->fname; if ((f = fopen(fname, "r")) == NULL) err(1, "%s", fname); } if ((c = getc(f)) != EOF) { (void)ungetc(c, f); break; } (void)fclose(f); files = files->next; } if (lastline) { sp->len = 0; return (0); } /* * Use fgetln so that we can handle essentially infinite input data. * Can't use the pointer into the stdio buffer as the process space * because the ungetc() can cause it to move. */ p = fgetln(f, &len); if (ferror(f)) errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); cspace(sp, p, len, spflag); linenum++; /* Advance to next non-empty file */ while ((c = getc(f)) == EOF) { (void)fclose(f); files = files->next; if (files == NULL) { lastline = 1; return (1); } if (files->fname == NULL) { f = stdin; fname = "stdin"; } else { fname = files->fname; if ((f = fopen(fname, "r")) == NULL) err(1, "%s", fname); } } (void)ungetc(c, f); return (1); } /* * Add a compilation unit to the linked list */ static void add_compunit(type, s) enum e_cut type; char *s; { struct s_compunit *cu; cu = xmalloc(sizeof(struct s_compunit)); cu->type = type; cu->s = s; cu->next = NULL; *cu_nextp = cu; cu_nextp = &cu->next; } /* * Add a file to the linked list */ static void add_file(s) char *s; { struct s_flist *fp; fp = xmalloc(sizeof(struct s_flist)); fp->next = NULL; *fl_nextp = fp; fp->fname = s; fl_nextp = &fp->next; } Index: stable/3/usr.bin/sed/sed.1 =================================================================== --- stable/3/usr.bin/sed/sed.1 (revision 89997) +++ stable/3/usr.bin/sed/sed.1 (revision 89998) @@ -1,519 +1,527 @@ .\" Copyright (c) 1992, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" the Institute of Electrical and Electronics Engineers, Inc. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)sed.1 8.2 (Berkeley) 12/30/93 .\" $FreeBSD$ .\" .Dd December 30, 1993 .Dt SED 1 .Os .Sh NAME .Nm sed .Nd stream editor .Sh SYNOPSIS .Nm sed -.Op Fl an +.Op Fl Ean .Ar command .Op Ar file ... .Nm sed -.Op Fl an +.Op Fl Ean .Op Fl e Ar command .Op Fl f Ar command_file .Op Ar file ... .Sh DESCRIPTION The .Nm utility reads the specified files, or the standard input if no files are specified, modifying the input as specified by a list of commands. The input is then written to the standard output. .Pp A single command may be specified as the first argument to .Nm sed . Multiple commands may be specified by using the .Fl e or .Fl f options. All commands are applied to the input in the order they are specified regardless of their origin. .Pp The following options are available: .Bl -tag -width indent +.It Fl E +Interpret regular expressions as extended (modern) regular expressions +rather than basic regular expressions (BRE's). The +.Xr re_format 7 +manual page fully describes both formats. .It Fl a The files listed as parameters for the .Dq w functions are created (or truncated) before any processing begins, by default. The .Fl a option causes .Nm to delay opening each file until a command containing the related .Dq w function is applied to a line of input. .It Fl e Ar command Append the editing commands specified by the .Ar command argument to the list of commands. .It Fl f Ar command_file Append the editing commands found in the file .Ar command_file to the list of commands. The editing commands should each be listed on a separate line. .It Fl n By default, each line of input is echoed to the standard output after all of the commands have been applied to it. The .Fl n option suppresses this behavior. .El .Pp The form of a .Nm command is as follows: .sp .Dl [address[,address]]function[arguments] .sp Whitespace may be inserted before the first address and the function portions of the command. .Pp Normally, .Nm cyclically copies a line of input, not including its terminating newline character, into a .Em "pattern space" , (unless there is something left after a .Dq D function), applies all of the commands with addresses that select that pattern space, copies the pattern space to the standard output, appending a newline, and deletes the pattern space. .Pp Some of the functions use a .Em "hold space" to save all or part of the pattern space for subsequent retrieval. .Sh "Sed Addresses" An address is not required, but if specified must be a number (that counts input lines cumulatively across input files), a dollar .Po .Dq $ .Pc character that addresses the last line of input, or a context address (which consists of a regular expression preceded and followed by a delimiter). .Pp A command line with no addresses selects every pattern space. .Pp A command line with one address selects all of the pattern spaces that match the address. .Pp A command line with two addresses selects an inclusive range. This range starts with the first pattern space that matches the first address. The end of the range is the next following pattern space that matches the second address. If the second address is a number less than or equal to the line number first selected, only that line is selected. In the case when the second address is a context address, sed does not re-match the second address against the pattern space that matched the first address. Starting at the first line following the selected range, sed starts looking again for the first address. .Nm starts looking again for the first address. .Pp Editing commands can be applied to non-selected pattern spaces by use of the exclamation character .Po .Dq ! .Pc function. .Sh "Sed Regular Expressions" The .Nm regular expressions are basic regular expressions (BRE's, see .Xr regex 3 -for more information). -In addition, +for more information) by default. .Nm -has the following two additions to BRE's: +can use extended (modern) regular expressions instead if the +.Fl E +flag is given. In addition, +.Nm +has the following two additions to regular expressions: .sp .Bl -enum -compact .It In a context address, any character other than a backslash .Po .Dq \e .Pc or newline character may be used to delimit the regular expression. Also, putting a backslash character before the delimiting character causes the character to be treated literally. For example, in the context address \exabc\exdefx, the RE delimiter is an .Dq x and the second .Dq x stands for itself, so that the regular expression is .Dq abcxdef . .sp .It The escape sequence \en matches a newline character embedded in the pattern space. You can't, however, use a literal newline character in an address or in the substitute command. .El .Pp One special feature of .Nm regular expressions is that they can default to the last regular expression used. If a regular expression is empty, i.e. just the delimiter characters are specified, the last regular expression encountered is used instead. The last regular expression is defined as the last regular expression used as part of an address or substitute command, and at run-time, not compile-time. For example, the command .Dq /abc/s//XXX/ will substitute .Dq XXX for the pattern .Dq abc . .Sh "Sed Functions" In the following list of commands, the maximum number of permissible addresses for each command is indicated by [0addr], [1addr], or [2addr], representing zero, one, or two addresses. .Pp The argument .Em text consists of one or more lines. To embed a newline in the text, precede it with a backslash. Other backslashes in text are deleted and the following character taken literally. .Pp The .Dq r and .Dq w functions take an optional file parameter, which should be separated from the function letter by white space. Each file given as an argument to .Nm is created (or its contents truncated) before any input processing begins. .Pp The .Dq b , .Dq r , .Dq s , .Dq t , .Dq w , .Dq y , .Dq ! , and .Dq \&: functions all accept additional arguments. The following synopses indicate which arguments have to be separated from the function letters by white space characters. .Pp Two of the functions take a function-list. This is a list of .Nm functions separated by newlines, as follows: .Bd -literal -offset indent { function function ... function } .Ed .Pp The .Dq { can be preceded by white space and can be followed by white space. The function can be preceded by white space. The terminating .Dq } must be preceded by a newline or optional white space. .sp .Bl -tag -width "XXXXXX" -compact .It [2addr] function-list Execute function-list only when the pattern space is selected. .sp .It [1addr]a\e .It text .br Write .Em text to standard output immediately before each attempt to read a line of input, whether by executing the .Dq N function or by beginning a new cycle. .sp .It [2addr]b[label] Branch to the .Dq \&: function with the specified label. If the label is not specified, branch to the end of the script. .sp .It [2addr]c\e .It text .br Delete the pattern space. With 0 or 1 address or at the end of a 2-address range, .Em text is written to the standard output. .sp .It [2addr]d Delete the pattern space and start the next cycle. .sp .It [2addr]D Delete the initial segment of the pattern space through the first newline character and start the next cycle. .sp .It [2addr]g Replace the contents of the pattern space with the contents of the hold space. .sp .It [2addr]G Append a newline character followed by the contents of the hold space to the pattern space. .sp .It [2addr]h Replace the contents of the hold space with the contents of the pattern space. .sp .It [2addr]H Append a newline character followed by the contents of the pattern space to the hold space. .sp .It [1addr]i\e .It text .br Write .Em text to the standard output. .sp .It [2addr]l (The letter ell.) Write the pattern space to the standard output in a visually unambiguous form. This form is as follows: .sp .Bl -tag -width "carriage-returnXX" -offset indent -compact .It backslash \e\e .It alert \ea .It form-feed \ef .It newline \en .It carriage-return \er .It tab \et .It vertical tab \ev .El .Pp Nonprintable characters are written as three-digit octal numbers (with a preceding backslash) for each byte in the character (most significant byte first). Long lines are folded, with the point of folding indicated by displaying a backslash followed by a newline. The end of each line is marked with a .Dq $ . .sp .It [2addr]n Write the pattern space to the standard output if the default output has not been suppressed, and replace the pattern space with the next line of input. .sp .It [2addr]N Append the next line of input to the pattern space, using an embedded newline character to separate the appended material from the original contents. Note that the current line number changes. .sp .It [2addr]p Write the pattern space to standard output. .sp .It [2addr]P Write the pattern space, up to the first newline character to the standard output. .sp .It [1addr]q Branch to the end of the script and quit without starting a new cycle. .sp .It [1addr]r file Copy the contents of .Em file to the standard output immediately before the next attempt to read a line of input. If .Em file cannot be read for any reason, it is silently ignored and no error condition is set. .sp .It [2addr]s/regular expression/replacement/flags Substitute the replacement string for the first instance of the regular expression in the pattern space. Any character other than backslash or newline can be used instead of a slash to delimit the RE and the replacement. Within the RE and the replacement, the RE delimiter itself can be used as a literal character if it is preceded by a backslash. .Pp An ampersand .Po .Dq & .Pc appearing in the replacement is replaced by the string matching the RE. The special meaning of .Dq & in this context can be suppressed by preceding it by a backslash. The string .Dq \e# , where .Dq # is a digit, is replaced by the text matched by the corresponding backreference expression (see .Xr re_format 7 ). .Pp A line can be split by substituting a newline character into it. To specify a newline character in the replacement string, precede it with a backslash. .Pp The value of .Em flags in the substitute function is zero or more of the following: .Bl -tag -width "XXXXXX" -offset indent .It "0 ... 9" Make the substitution only for the N'th occurrence of the regular expression in the pattern space. .It g Make the substitution for all non-overlapping matches of the regular expression, not just the first one. .It p Write the pattern space to standard output if a replacement was made. If the replacement string is identical to that which it replaces, it is still considered to have been a replacement. .It w Em file Append the pattern space to .Em file if a replacement was made. If the replacement string is identical to that which it replaces, it is still considered to have been a replacement. .El .sp .It [2addr]t [label] Branch to the .Dq \&: function bearing the label if any substitutions have been made since the most recent reading of an input line or execution of a .Dq t function. If no label is specified, branch to the end of the script. .sp .It [2addr]w Em file Append the pattern space to the .Em file . .sp .It [2addr]x Swap the contents of the pattern and hold spaces. .sp .It [2addr]y/string1/string2/ Replace all occurrences of characters in .Em string1 in the pattern space with the corresponding characters from .Em string2 . Any character other than a backslash or newline can be used instead of a slash to delimit the strings. Within .Em string1 and .Em string2 , a backslash followed by any character other than a newline is that literal character, and a backslash followed by an ``n'' is replaced by a newline character. .sp .It [2addr]!function .It [2addr]!function-list Apply the function or function-list only to the lines that are .Em not selected by the address(es). .sp .It [0addr]:label This function does nothing; it bears a label to which the .Dq b and .Dq t commands may branch. .sp .It [1addr]= Write the line number to the standard output followed by a newline character. .sp .It [0addr] Empty lines are ignored. .sp .It [0addr]# The .Dq # and the remainder of the line are ignored (treated as a comment), with the single exception that if the first two characters in the file are .Dq #n , the default output is suppressed. This is the same as specifying the .Fl n option on the command line. .El .Pp The .Nm utility exits 0 on success and >0 if an error occurs. .Sh SEE ALSO .Xr awk 1 , .Xr ed 1 , .Xr grep 1 , .Xr regex 3 , .Xr re_format 7 .Sh HISTORY A .Nm command appeared in .At v7 . .Sh STANDARDS The .Nm function is expected to be a superset of the .St -p1003.2 specification.