19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1992 Diomidis Spinellis. 39b50d902SRodney W. Grimes * Copyright (c) 1992, 1993 49b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 59b50d902SRodney W. Grimes * 69b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 79b50d902SRodney W. Grimes * Diomidis Spinellis of Imperial College, University of London. 89b50d902SRodney W. Grimes * 99b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 109b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 119b50d902SRodney W. Grimes * are met: 129b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 149b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 159b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 169b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 34e74bf75fSMark Murray #include <sys/cdefs.h> 35e74bf75fSMark Murray __FBSDID("$FreeBSD$"); 36e74bf75fSMark Murray 379b50d902SRodney W. Grimes #ifndef lint 38e74bf75fSMark Murray static const char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93"; 3973a08bb2SPhilippe Charnier #endif 409b50d902SRodney W. Grimes 419b50d902SRodney W. Grimes #include <sys/types.h> 429b50d902SRodney W. Grimes #include <sys/stat.h> 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #include <ctype.h> 4573a08bb2SPhilippe Charnier #include <err.h> 4681a8648aSTim J. Robbins #include <errno.h> 479b50d902SRodney W. Grimes #include <fcntl.h> 489b50d902SRodney W. Grimes #include <limits.h> 499b50d902SRodney W. Grimes #include <regex.h> 509b50d902SRodney W. Grimes #include <stdio.h> 519b50d902SRodney W. Grimes #include <stdlib.h> 529b50d902SRodney W. Grimes #include <string.h> 5381a8648aSTim J. Robbins #include <wchar.h> 549b50d902SRodney W. Grimes 559b50d902SRodney W. Grimes #include "defs.h" 569b50d902SRodney W. Grimes #include "extern.h" 579b50d902SRodney W. Grimes 589b50d902SRodney W. Grimes #define LHSZ 128 599b50d902SRodney W. Grimes #define LHMASK (LHSZ - 1) 609b50d902SRodney W. Grimes static struct labhash { 619b50d902SRodney W. Grimes struct labhash *lh_next; 629b50d902SRodney W. Grimes u_int lh_hash; 639b50d902SRodney W. Grimes struct s_command *lh_cmd; 649b50d902SRodney W. Grimes int lh_ref; 659b50d902SRodney W. Grimes } *labels[LHSZ]; 669b50d902SRodney W. Grimes 673f330d7dSWarner Losh static char *compile_addr(char *, struct s_addr *); 683f330d7dSWarner Losh static char *compile_ccl(char **, char *); 6976570d0aSDiomidis Spinellis static char *compile_delimited(char *, char *, int); 703f330d7dSWarner Losh static char *compile_flags(char *, struct s_subst *); 71bdd72b70SSuleiman Souhlal static regex_t *compile_re(char *, int); 723f330d7dSWarner Losh static char *compile_subst(char *, struct s_subst *); 733f330d7dSWarner Losh static char *compile_text(void); 7481a8648aSTim J. Robbins static char *compile_tr(char *, struct s_tr **); 759b50d902SRodney W. Grimes static struct s_command 763f330d7dSWarner Losh **compile_stream(struct s_command **); 773f330d7dSWarner Losh static char *duptoeol(char *, const char *); 783f330d7dSWarner Losh static void enterlabel(struct s_command *); 799b50d902SRodney W. Grimes static struct s_command 803f330d7dSWarner Losh *findlabel(char *); 813f330d7dSWarner Losh static void fixuplabel(struct s_command *, struct s_command *); 823f330d7dSWarner Losh static void uselabel(void); 839b50d902SRodney W. Grimes 849b50d902SRodney W. Grimes /* 859b50d902SRodney W. Grimes * Command specification. This is used to drive the command parser. 869b50d902SRodney W. Grimes */ 879b50d902SRodney W. Grimes struct s_format { 889b50d902SRodney W. Grimes char code; /* Command code */ 899b50d902SRodney W. Grimes int naddr; /* Number of address args */ 909b50d902SRodney W. Grimes enum e_args args; /* Argument type */ 919b50d902SRodney W. Grimes }; 929b50d902SRodney W. Grimes 939b50d902SRodney W. Grimes static struct s_format cmd_fmts[] = { 949b50d902SRodney W. Grimes {'{', 2, GROUP}, 95ce19262dSJordan K. Hubbard {'}', 0, ENDGROUP}, 969b50d902SRodney W. Grimes {'a', 1, TEXT}, 979b50d902SRodney W. Grimes {'b', 2, BRANCH}, 989b50d902SRodney W. Grimes {'c', 2, TEXT}, 999b50d902SRodney W. Grimes {'d', 2, EMPTY}, 1009b50d902SRodney W. Grimes {'D', 2, EMPTY}, 1019b50d902SRodney W. Grimes {'g', 2, EMPTY}, 1029b50d902SRodney W. Grimes {'G', 2, EMPTY}, 1039b50d902SRodney W. Grimes {'h', 2, EMPTY}, 1049b50d902SRodney W. Grimes {'H', 2, EMPTY}, 1059b50d902SRodney W. Grimes {'i', 1, TEXT}, 1069b50d902SRodney W. Grimes {'l', 2, EMPTY}, 1079b50d902SRodney W. Grimes {'n', 2, EMPTY}, 1089b50d902SRodney W. Grimes {'N', 2, EMPTY}, 1099b50d902SRodney W. Grimes {'p', 2, EMPTY}, 1109b50d902SRodney W. Grimes {'P', 2, EMPTY}, 1119b50d902SRodney W. Grimes {'q', 1, EMPTY}, 1129b50d902SRodney W. Grimes {'r', 1, RFILE}, 1139b50d902SRodney W. Grimes {'s', 2, SUBST}, 1149b50d902SRodney W. Grimes {'t', 2, BRANCH}, 1159b50d902SRodney W. Grimes {'w', 2, WFILE}, 1169b50d902SRodney W. Grimes {'x', 2, EMPTY}, 1179b50d902SRodney W. Grimes {'y', 2, TR}, 1189b50d902SRodney W. Grimes {'!', 2, NONSEL}, 1199b50d902SRodney W. Grimes {':', 0, LABEL}, 1209b50d902SRodney W. Grimes {'#', 0, COMMENT}, 1219b50d902SRodney W. Grimes {'=', 1, EMPTY}, 1229b50d902SRodney W. Grimes {'\0', 0, COMMENT}, 1239b50d902SRodney W. Grimes }; 1249b50d902SRodney W. Grimes 1259b50d902SRodney W. Grimes /* The compiled program. */ 1269b50d902SRodney W. Grimes struct s_command *prog; 1279b50d902SRodney W. Grimes 1289b50d902SRodney W. Grimes /* 1299b50d902SRodney W. Grimes * Compile the program into prog. 1309b50d902SRodney W. Grimes * Initialise appends. 1319b50d902SRodney W. Grimes */ 1329b50d902SRodney W. Grimes void 133e6478125SDag-Erling Smørgrav compile(void) 1349b50d902SRodney W. Grimes { 135ce19262dSJordan K. Hubbard *compile_stream(&prog) = NULL; 1369b50d902SRodney W. Grimes fixuplabel(prog, NULL); 1379b50d902SRodney W. Grimes uselabel(); 1380ea56610SMike Heffner if (appendnum == 0) 1390ea56610SMike Heffner appends = NULL; 1400ea56610SMike Heffner else if ((appends = malloc(sizeof(struct s_appends) * appendnum)) == 1410ea56610SMike Heffner NULL) 1428e33c0a0SDavid E. O'Brien err(1, "malloc"); 1438e33c0a0SDavid E. O'Brien if ((match = malloc((maxnsub + 1) * sizeof(regmatch_t))) == NULL) 1448e33c0a0SDavid E. O'Brien err(1, "malloc"); 1459b50d902SRodney W. Grimes } 1469b50d902SRodney W. Grimes 1479b50d902SRodney W. Grimes #define EATSPACE() do { \ 1489b50d902SRodney W. Grimes if (p) \ 149726aebe5SAndrey A. Chernov while (*p && isspace((unsigned char)*p)) \ 1509b50d902SRodney W. Grimes p++; \ 1519b50d902SRodney W. Grimes } while (0) 1529b50d902SRodney W. Grimes 1539b50d902SRodney W. Grimes static struct s_command ** 154e6478125SDag-Erling Smørgrav compile_stream(struct s_command **link) 1559b50d902SRodney W. Grimes { 156e74bf75fSMark Murray char *p; 1579b50d902SRodney W. Grimes static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ 158ce19262dSJordan K. Hubbard struct s_command *cmd, *cmd2, *stack; 1599b50d902SRodney W. Grimes struct s_format *fp; 160bdd72b70SSuleiman Souhlal char re[_POSIX2_LINE_MAX + 1]; 1619b50d902SRodney W. Grimes int naddr; /* Number of addresses */ 1629b50d902SRodney W. Grimes 163ce19262dSJordan K. Hubbard stack = 0; 1649b50d902SRodney W. Grimes for (;;) { 165c56690efSArchie Cobbs if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) { 166ce19262dSJordan K. Hubbard if (stack != 0) 16773a08bb2SPhilippe Charnier errx(1, "%lu: %s: unexpected EOF (pending }'s)", 16873a08bb2SPhilippe Charnier linenum, fname); 1699b50d902SRodney W. Grimes return (link); 1709b50d902SRodney W. Grimes } 1719b50d902SRodney W. Grimes 1729b50d902SRodney W. Grimes semicolon: EATSPACE(); 1730467aed3STim J. Robbins if (p) { 1740467aed3STim J. Robbins if (*p == '#' || *p == '\0') 1759b50d902SRodney W. Grimes continue; 1760467aed3STim J. Robbins else if (*p == ';') { 1770467aed3STim J. Robbins p++; 1780467aed3STim J. Robbins goto semicolon; 1790467aed3STim J. Robbins } 1800467aed3STim J. Robbins } 1818e33c0a0SDavid E. O'Brien if ((*link = cmd = malloc(sizeof(struct s_command))) == NULL) 1828e33c0a0SDavid E. O'Brien err(1, "malloc"); 1839b50d902SRodney W. Grimes link = &cmd->next; 184f879e8d9SBrian Somers cmd->startline = cmd->nonsel = 0; 1859b50d902SRodney W. Grimes /* First parse the addresses */ 1869b50d902SRodney W. Grimes naddr = 0; 1879b50d902SRodney W. Grimes 1889b50d902SRodney W. Grimes /* Valid characters to start an address */ 1899b50d902SRodney W. Grimes #define addrchar(c) (strchr("0123456789/\\$", (c))) 1909b50d902SRodney W. Grimes if (addrchar(*p)) { 1919b50d902SRodney W. Grimes naddr++; 1928e33c0a0SDavid E. O'Brien if ((cmd->a1 = malloc(sizeof(struct s_addr))) == NULL) 1938e33c0a0SDavid E. O'Brien err(1, "malloc"); 1949b50d902SRodney W. Grimes p = compile_addr(p, cmd->a1); 1959b50d902SRodney W. Grimes EATSPACE(); /* EXTENSION */ 1969b50d902SRodney W. Grimes if (*p == ',') { 1979b50d902SRodney W. Grimes p++; 1989b50d902SRodney W. Grimes EATSPACE(); /* EXTENSION */ 199ce19262dSJordan K. Hubbard naddr++; 2008e33c0a0SDavid E. O'Brien if ((cmd->a2 = malloc(sizeof(struct s_addr))) 2018e33c0a0SDavid E. O'Brien == NULL) 2028e33c0a0SDavid E. O'Brien err(1, "malloc"); 2039b50d902SRodney W. Grimes p = compile_addr(p, cmd->a2); 204ce19262dSJordan K. Hubbard EATSPACE(); 205ce19262dSJordan K. Hubbard } else 206ce19262dSJordan K. Hubbard cmd->a2 = 0; 207ce19262dSJordan K. Hubbard } else 208ce19262dSJordan K. Hubbard cmd->a1 = cmd->a2 = 0; 2099b50d902SRodney W. Grimes 2109b50d902SRodney W. Grimes nonsel: /* Now parse the command */ 2119b50d902SRodney W. Grimes if (!*p) 21273a08bb2SPhilippe Charnier errx(1, "%lu: %s: command expected", linenum, fname); 2139b50d902SRodney W. Grimes cmd->code = *p; 2149b50d902SRodney W. Grimes for (fp = cmd_fmts; fp->code; fp++) 2159b50d902SRodney W. Grimes if (fp->code == *p) 2169b50d902SRodney W. Grimes break; 2179b50d902SRodney W. Grimes if (!fp->code) 21873a08bb2SPhilippe Charnier errx(1, "%lu: %s: invalid command code %c", linenum, fname, *p); 2199b50d902SRodney W. Grimes if (naddr > fp->naddr) 22073a08bb2SPhilippe Charnier errx(1, 22173a08bb2SPhilippe Charnier "%lu: %s: command %c expects up to %d address(es), found %d", 22273a08bb2SPhilippe Charnier linenum, fname, *p, fp->naddr, naddr); 2239b50d902SRodney W. Grimes switch (fp->args) { 2249b50d902SRodney W. Grimes case NONSEL: /* ! */ 2259b50d902SRodney W. Grimes p++; 226ce19262dSJordan K. Hubbard EATSPACE(); 22746da6c48SDiomidis Spinellis cmd->nonsel = 1; 2289b50d902SRodney W. Grimes goto nonsel; 2299b50d902SRodney W. Grimes case GROUP: /* { */ 2309b50d902SRodney W. Grimes p++; 2319b50d902SRodney W. Grimes EATSPACE(); 232ce19262dSJordan K. Hubbard cmd->next = stack; 233ce19262dSJordan K. Hubbard stack = cmd; 234ce19262dSJordan K. Hubbard link = &cmd->u.c; 235ce19262dSJordan K. Hubbard if (*p) 236ce19262dSJordan K. Hubbard goto semicolon; 2379b50d902SRodney W. Grimes break; 238ce19262dSJordan K. Hubbard case ENDGROUP: 239ce19262dSJordan K. Hubbard /* 240ce19262dSJordan K. Hubbard * Short-circuit command processing, since end of 241ce19262dSJordan K. Hubbard * group is really just a noop. 242ce19262dSJordan K. Hubbard */ 243ce19262dSJordan K. Hubbard cmd->nonsel = 1; 244ce19262dSJordan K. Hubbard if (stack == 0) 24573a08bb2SPhilippe Charnier errx(1, "%lu: %s: unexpected }", linenum, fname); 246ce19262dSJordan K. Hubbard cmd2 = stack; 247ce19262dSJordan K. Hubbard stack = cmd2->next; 248ce19262dSJordan K. Hubbard cmd2->next = cmd; 249ce19262dSJordan K. Hubbard /*FALLTHROUGH*/ 2509b50d902SRodney W. Grimes case EMPTY: /* d D g G h H l n N p P q x = \0 */ 2519b50d902SRodney W. Grimes p++; 2529b50d902SRodney W. Grimes EATSPACE(); 2539b50d902SRodney W. Grimes if (*p == ';') { 2549b50d902SRodney W. Grimes p++; 2559b50d902SRodney W. Grimes link = &cmd->next; 2569b50d902SRodney W. Grimes goto semicolon; 2579b50d902SRodney W. Grimes } 2589b50d902SRodney W. Grimes if (*p) 25973a08bb2SPhilippe Charnier errx(1, "%lu: %s: extra characters at the end of %c command", 26073a08bb2SPhilippe Charnier linenum, fname, cmd->code); 2619b50d902SRodney W. Grimes break; 2629b50d902SRodney W. Grimes case TEXT: /* a c i */ 2639b50d902SRodney W. Grimes p++; 2649b50d902SRodney W. Grimes EATSPACE(); 2659b50d902SRodney W. Grimes if (*p != '\\') 26673a08bb2SPhilippe Charnier errx(1, 26773a08bb2SPhilippe Charnier "%lu: %s: command %c expects \\ followed by text", linenum, fname, cmd->code); 2689b50d902SRodney W. Grimes p++; 2699b50d902SRodney W. Grimes EATSPACE(); 2709b50d902SRodney W. Grimes if (*p) 27173a08bb2SPhilippe Charnier errx(1, 27273a08bb2SPhilippe Charnier "%lu: %s: extra characters after \\ at the end of %c command", 27373a08bb2SPhilippe Charnier linenum, fname, cmd->code); 2749b50d902SRodney W. Grimes cmd->t = compile_text(); 2759b50d902SRodney W. Grimes break; 2769b50d902SRodney W. Grimes case COMMENT: /* \0 # */ 2779b50d902SRodney W. Grimes break; 2789b50d902SRodney W. Grimes case WFILE: /* w */ 2799b50d902SRodney W. Grimes p++; 2809b50d902SRodney W. Grimes EATSPACE(); 2819b50d902SRodney W. Grimes if (*p == '\0') 28273a08bb2SPhilippe Charnier errx(1, "%lu: %s: filename expected", linenum, fname); 2839b50d902SRodney W. Grimes cmd->t = duptoeol(p, "w command"); 2849b50d902SRodney W. Grimes if (aflag) 2859b50d902SRodney W. Grimes cmd->u.fd = -1; 2869b50d902SRodney W. Grimes else if ((cmd->u.fd = open(p, 2879b50d902SRodney W. Grimes O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 2889b50d902SRodney W. Grimes DEFFILEMODE)) == -1) 28973a08bb2SPhilippe Charnier err(1, "%s", p); 2909b50d902SRodney W. Grimes break; 2919b50d902SRodney W. Grimes case RFILE: /* r */ 2929b50d902SRodney W. Grimes p++; 2939b50d902SRodney W. Grimes EATSPACE(); 2949b50d902SRodney W. Grimes if (*p == '\0') 29573a08bb2SPhilippe Charnier errx(1, "%lu: %s: filename expected", linenum, fname); 2969b50d902SRodney W. Grimes else 2979b50d902SRodney W. Grimes cmd->t = duptoeol(p, "read command"); 2989b50d902SRodney W. Grimes break; 2999b50d902SRodney W. Grimes case BRANCH: /* b t */ 3009b50d902SRodney W. Grimes p++; 3019b50d902SRodney W. Grimes EATSPACE(); 3029b50d902SRodney W. Grimes if (*p == '\0') 3039b50d902SRodney W. Grimes cmd->t = NULL; 3049b50d902SRodney W. Grimes else 3059b50d902SRodney W. Grimes cmd->t = duptoeol(p, "branch"); 3069b50d902SRodney W. Grimes break; 3079b50d902SRodney W. Grimes case LABEL: /* : */ 3089b50d902SRodney W. Grimes p++; 3099b50d902SRodney W. Grimes EATSPACE(); 3109b50d902SRodney W. Grimes cmd->t = duptoeol(p, "label"); 3119b50d902SRodney W. Grimes if (strlen(p) == 0) 31273a08bb2SPhilippe Charnier errx(1, "%lu: %s: empty label", linenum, fname); 3139b50d902SRodney W. Grimes enterlabel(cmd); 3149b50d902SRodney W. Grimes break; 3159b50d902SRodney W. Grimes case SUBST: /* s */ 3169b50d902SRodney W. Grimes p++; 3179b50d902SRodney W. Grimes if (*p == '\0' || *p == '\\') 31873a08bb2SPhilippe Charnier errx(1, 31973a08bb2SPhilippe Charnier "%lu: %s: substitute pattern can not be delimited by newline or backslash", 32073a08bb2SPhilippe Charnier linenum, fname); 321870945d8SXin LI if ((cmd->u.s = calloc(1, sizeof(struct s_subst))) == NULL) 3228e33c0a0SDavid E. O'Brien err(1, "malloc"); 32376570d0aSDiomidis Spinellis p = compile_delimited(p, re, 0); 3249b50d902SRodney W. Grimes if (p == NULL) 32573a08bb2SPhilippe Charnier errx(1, 32673a08bb2SPhilippe Charnier "%lu: %s: unterminated substitute pattern", linenum, fname); 327d3d0d3a3SHiroki Sato 328d3d0d3a3SHiroki Sato /* Compile RE with no case sensitivity temporarily */ 329d3d0d3a3SHiroki Sato if (*re == '\0') 330d3d0d3a3SHiroki Sato cmd->u.s->re = NULL; 331d3d0d3a3SHiroki Sato else 332d3d0d3a3SHiroki Sato cmd->u.s->re = compile_re(re, 0); 333870945d8SXin LI --p; 334870945d8SXin LI p = compile_subst(p, cmd->u.s); 335870945d8SXin LI p = compile_flags(p, cmd->u.s); 336d3d0d3a3SHiroki Sato 337d3d0d3a3SHiroki Sato /* Recompile RE with case sensitivity from "I" flag if any */ 338bdd72b70SSuleiman Souhlal if (*re == '\0') 339bdd72b70SSuleiman Souhlal cmd->u.s->re = NULL; 340bdd72b70SSuleiman Souhlal else 341bdd72b70SSuleiman Souhlal cmd->u.s->re = compile_re(re, cmd->u.s->icase); 3429b50d902SRodney W. Grimes EATSPACE(); 3439b50d902SRodney W. Grimes if (*p == ';') { 3449b50d902SRodney W. Grimes p++; 3459b50d902SRodney W. Grimes link = &cmd->next; 3469b50d902SRodney W. Grimes goto semicolon; 3479b50d902SRodney W. Grimes } 3489b50d902SRodney W. Grimes break; 3499b50d902SRodney W. Grimes case TR: /* y */ 3509b50d902SRodney W. Grimes p++; 35181a8648aSTim J. Robbins p = compile_tr(p, &cmd->u.y); 3529b50d902SRodney W. Grimes EATSPACE(); 3539b50d902SRodney W. Grimes if (*p == ';') { 3549b50d902SRodney W. Grimes p++; 3559b50d902SRodney W. Grimes link = &cmd->next; 3569b50d902SRodney W. Grimes goto semicolon; 3579b50d902SRodney W. Grimes } 3589b50d902SRodney W. Grimes if (*p) 35973a08bb2SPhilippe Charnier errx(1, 36073a08bb2SPhilippe Charnier "%lu: %s: extra text at the end of a transform command", linenum, fname); 3619b50d902SRodney W. Grimes break; 3629b50d902SRodney W. Grimes } 3639b50d902SRodney W. Grimes } 3649b50d902SRodney W. Grimes } 3659b50d902SRodney W. Grimes 3669b50d902SRodney W. Grimes /* 367463a577bSEitan Adler * Get a delimited string. P points to the delimiter of the string; d points 3689b50d902SRodney W. Grimes * to a buffer area. Newline and delimiter escapes are processed; other 3699b50d902SRodney W. Grimes * escapes are ignored. 3709b50d902SRodney W. Grimes * 3719b50d902SRodney W. Grimes * Returns a pointer to the first character after the final delimiter or NULL 3729b50d902SRodney W. Grimes * in the case of a non-terminated string. The character array d is filled 3739b50d902SRodney W. Grimes * with the processed string. 3749b50d902SRodney W. Grimes */ 3759b50d902SRodney W. Grimes static char * 37676570d0aSDiomidis Spinellis compile_delimited(char *p, char *d, int is_tr) 3779b50d902SRodney W. Grimes { 3789b50d902SRodney W. Grimes char c; 3799b50d902SRodney W. Grimes 3809b50d902SRodney W. Grimes c = *p++; 3819b50d902SRodney W. Grimes if (c == '\0') 3829b50d902SRodney W. Grimes return (NULL); 3839b50d902SRodney W. Grimes else if (c == '\\') 38473a08bb2SPhilippe Charnier errx(1, "%lu: %s: \\ can not be used as a string delimiter", 38573a08bb2SPhilippe Charnier linenum, fname); 3869b50d902SRodney W. Grimes else if (c == '\n') 38773a08bb2SPhilippe Charnier errx(1, "%lu: %s: newline can not be used as a string delimiter", 38873a08bb2SPhilippe Charnier linenum, fname); 3899b50d902SRodney W. Grimes while (*p) { 390128e6a12SDiomidis Spinellis if (*p == '[' && *p != c) { 391ce19262dSJordan K. Hubbard if ((d = compile_ccl(&p, d)) == NULL) 39273a08bb2SPhilippe Charnier errx(1, "%lu: %s: unbalanced brackets ([])", linenum, fname); 393ce19262dSJordan K. Hubbard continue; 394ce19262dSJordan K. Hubbard } else if (*p == '\\' && p[1] == '[') { 395ce19262dSJordan K. Hubbard *d++ = *p++; 396ce19262dSJordan K. Hubbard } else if (*p == '\\' && p[1] == c) 3979b50d902SRodney W. Grimes p++; 3989b50d902SRodney W. Grimes else if (*p == '\\' && p[1] == 'n') { 3999b50d902SRodney W. Grimes *d++ = '\n'; 4009b50d902SRodney W. Grimes p += 2; 4019b50d902SRodney W. Grimes continue; 40276570d0aSDiomidis Spinellis } else if (*p == '\\' && p[1] == '\\') { 40376570d0aSDiomidis Spinellis if (is_tr) 40476570d0aSDiomidis Spinellis p++; 40576570d0aSDiomidis Spinellis else 4069b50d902SRodney W. Grimes *d++ = *p++; 40776570d0aSDiomidis Spinellis } else if (*p == c) { 4089b50d902SRodney W. Grimes *d = '\0'; 4099b50d902SRodney W. Grimes return (p + 1); 4109b50d902SRodney W. Grimes } 4119b50d902SRodney W. Grimes *d++ = *p++; 4129b50d902SRodney W. Grimes } 4139b50d902SRodney W. Grimes return (NULL); 4149b50d902SRodney W. Grimes } 4159b50d902SRodney W. Grimes 416ce19262dSJordan K. Hubbard 417ce19262dSJordan K. Hubbard /* compile_ccl: expand a POSIX character class */ 418ce19262dSJordan K. Hubbard static char * 419e6478125SDag-Erling Smørgrav compile_ccl(char **sp, char *t) 420ce19262dSJordan K. Hubbard { 421ce19262dSJordan K. Hubbard int c, d; 422ce19262dSJordan K. Hubbard char *s = *sp; 423ce19262dSJordan K. Hubbard 424ce19262dSJordan K. Hubbard *t++ = *s++; 425ce19262dSJordan K. Hubbard if (*s == '^') 426ce19262dSJordan K. Hubbard *t++ = *s++; 427ce19262dSJordan K. Hubbard if (*s == ']') 428ce19262dSJordan K. Hubbard *t++ = *s++; 429ce19262dSJordan K. Hubbard for (; *s && (*t = *s) != ']'; s++, t++) 430ce19262dSJordan K. Hubbard if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) { 431ce19262dSJordan K. Hubbard *++t = *++s, t++, s++; 432ce19262dSJordan K. Hubbard for (c = *s; (*t = *s) != ']' || c != d; s++, t++) 433ce19262dSJordan K. Hubbard if ((c = *s) == '\0') 434ce19262dSJordan K. Hubbard return NULL; 435ac8f32ceSDiomidis Spinellis } 436ce19262dSJordan K. Hubbard return (*s == ']') ? *sp = ++s, ++t : NULL; 437ce19262dSJordan K. Hubbard } 438ce19262dSJordan K. Hubbard 4399b50d902SRodney W. Grimes /* 440bdd72b70SSuleiman Souhlal * Compiles the regular expression in RE and returns a pointer to the compiled 441bdd72b70SSuleiman Souhlal * regular expression. 4429b50d902SRodney W. Grimes * Cflags are passed to regcomp. 4439b50d902SRodney W. Grimes */ 444bdd72b70SSuleiman Souhlal static regex_t * 445bdd72b70SSuleiman Souhlal compile_re(char *re, int case_insensitive) 4469b50d902SRodney W. Grimes { 447bdd72b70SSuleiman Souhlal regex_t *rep; 448bdd72b70SSuleiman Souhlal int eval, flags; 4499b50d902SRodney W. Grimes 450bdd72b70SSuleiman Souhlal 451bdd72b70SSuleiman Souhlal flags = rflags; 452bdd72b70SSuleiman Souhlal if (case_insensitive) 453bdd72b70SSuleiman Souhlal flags |= REG_ICASE; 454bdd72b70SSuleiman Souhlal if ((rep = malloc(sizeof(regex_t))) == NULL) 4558e33c0a0SDavid E. O'Brien err(1, "malloc"); 456d3e5e11cSDavid Malone if ((eval = regcomp(rep, re, flags)) != 0) 45773a08bb2SPhilippe Charnier errx(1, "%lu: %s: RE error: %s", 458bdd72b70SSuleiman Souhlal linenum, fname, strregerror(eval, rep)); 459bdd72b70SSuleiman Souhlal if (maxnsub < rep->re_nsub) 460bdd72b70SSuleiman Souhlal maxnsub = rep->re_nsub; 461bdd72b70SSuleiman Souhlal return (rep); 4629b50d902SRodney W. Grimes } 4639b50d902SRodney W. Grimes 4649b50d902SRodney W. Grimes /* 4659b50d902SRodney W. Grimes * Compile the substitution string of a regular expression and set res to 4669b50d902SRodney W. Grimes * point to a saved copy of it. Nsub is the number of parenthesized regular 4679b50d902SRodney W. Grimes * expressions. 4689b50d902SRodney W. Grimes */ 4699b50d902SRodney W. Grimes static char * 470e6478125SDag-Erling Smørgrav compile_subst(char *p, struct s_subst *s) 4719b50d902SRodney W. Grimes { 4729b50d902SRodney W. Grimes static char lbuf[_POSIX2_LINE_MAX + 1]; 473e74bf75fSMark Murray int asize, size; 474e74bf75fSMark Murray u_char ref; 4759b50d902SRodney W. Grimes char c, *text, *op, *sp; 476f020c7faSBrian Feldman int more = 1, sawesc = 0; 4779b50d902SRodney W. Grimes 4789b50d902SRodney W. Grimes c = *p++; /* Terminator character */ 4799b50d902SRodney W. Grimes if (c == '\0') 4809b50d902SRodney W. Grimes return (NULL); 4819b50d902SRodney W. Grimes 4829b50d902SRodney W. Grimes s->maxbref = 0; 4839b50d902SRodney W. Grimes s->linenum = linenum; 4849b50d902SRodney W. Grimes asize = 2 * _POSIX2_LINE_MAX + 1; 4858e33c0a0SDavid E. O'Brien if ((text = malloc(asize)) == NULL) 4868e33c0a0SDavid E. O'Brien err(1, "malloc"); 4879b50d902SRodney W. Grimes size = 0; 4889b50d902SRodney W. Grimes do { 4899b50d902SRodney W. Grimes op = sp = text + size; 4909b50d902SRodney W. Grimes for (; *p; p++) { 491f020c7faSBrian Feldman if (*p == '\\' || sawesc) { 492f020c7faSBrian Feldman /* 493f020c7faSBrian Feldman * If this is a continuation from the last 494f020c7faSBrian Feldman * buffer, we won't have a character to 495f020c7faSBrian Feldman * skip over. 496f020c7faSBrian Feldman */ 497f020c7faSBrian Feldman if (sawesc) 498f020c7faSBrian Feldman sawesc = 0; 499f020c7faSBrian Feldman else 5009b50d902SRodney W. Grimes p++; 501f020c7faSBrian Feldman 502f020c7faSBrian Feldman if (*p == '\0') { 503f020c7faSBrian Feldman /* 504f020c7faSBrian Feldman * This escaped character is continued 505f020c7faSBrian Feldman * in the next part of the line. Note 506f020c7faSBrian Feldman * this fact, then cause the loop to 507f020c7faSBrian Feldman * exit w/ normal EOL case and reenter 508f020c7faSBrian Feldman * above with the new buffer. 509f020c7faSBrian Feldman */ 510f020c7faSBrian Feldman sawesc = 1; 511f020c7faSBrian Feldman p--; 512f020c7faSBrian Feldman continue; 513f020c7faSBrian Feldman } else if (strchr("123456789", *p) != NULL) { 5149b50d902SRodney W. Grimes *sp++ = '\\'; 5159b50d902SRodney W. Grimes ref = *p - '0'; 5169b50d902SRodney W. Grimes if (s->re != NULL && 5179b50d902SRodney W. Grimes ref > s->re->re_nsub) 51873a08bb2SPhilippe Charnier errx(1, "%lu: %s: \\%c not defined in the RE", 51973a08bb2SPhilippe Charnier linenum, fname, *p); 5209b50d902SRodney W. Grimes if (s->maxbref < ref) 5219b50d902SRodney W. Grimes s->maxbref = ref; 5229b50d902SRodney W. Grimes } else if (*p == '&' || *p == '\\') 5239b50d902SRodney W. Grimes *sp++ = '\\'; 5249b50d902SRodney W. Grimes } else if (*p == c) { 525c56690efSArchie Cobbs if (*++p == '\0' && more) { 526c56690efSArchie Cobbs if (cu_fgets(lbuf, sizeof(lbuf), &more)) 5271a6583daSArchie Cobbs p = lbuf; 5281a6583daSArchie Cobbs } 5299b50d902SRodney W. Grimes *sp++ = '\0'; 5309b50d902SRodney W. Grimes size += sp - op; 5318e33c0a0SDavid E. O'Brien if ((s->new = realloc(text, size)) == NULL) 5328e33c0a0SDavid E. O'Brien err(1, "realloc"); 5339b50d902SRodney W. Grimes return (p); 5349b50d902SRodney W. Grimes } else if (*p == '\n') { 53573a08bb2SPhilippe Charnier errx(1, 53673a08bb2SPhilippe Charnier "%lu: %s: unescaped newline inside substitute pattern", linenum, fname); 5379b50d902SRodney W. Grimes /* NOTREACHED */ 5389b50d902SRodney W. Grimes } 5399b50d902SRodney W. Grimes *sp++ = *p; 5409b50d902SRodney W. Grimes } 5419b50d902SRodney W. Grimes size += sp - op; 5429b50d902SRodney W. Grimes if (asize - size < _POSIX2_LINE_MAX + 1) { 5439b50d902SRodney W. Grimes asize *= 2; 5448e33c0a0SDavid E. O'Brien if ((text = realloc(text, asize)) == NULL) 5458e33c0a0SDavid E. O'Brien err(1, "realloc"); 5469b50d902SRodney W. Grimes } 547c56690efSArchie Cobbs } while (cu_fgets(p = lbuf, sizeof(lbuf), &more)); 54873a08bb2SPhilippe Charnier errx(1, "%lu: %s: unterminated substitute in regular expression", 54973a08bb2SPhilippe Charnier linenum, fname); 5509b50d902SRodney W. Grimes /* NOTREACHED */ 5519b50d902SRodney W. Grimes } 5529b50d902SRodney W. Grimes 5539b50d902SRodney W. Grimes /* 5549b50d902SRodney W. Grimes * Compile the flags of the s command 5559b50d902SRodney W. Grimes */ 5569b50d902SRodney W. Grimes static char * 557e6478125SDag-Erling Smørgrav compile_flags(char *p, struct s_subst *s) 5589b50d902SRodney W. Grimes { 5599b50d902SRodney W. Grimes int gn; /* True if we have seen g or n */ 560d432588eSDiomidis Spinellis unsigned long nval; 561a34b42e4SPedro F. Giffuni char wfile[_POSIX2_LINE_MAX + 1], *q, *eq; 5629b50d902SRodney W. Grimes 5639b50d902SRodney W. Grimes s->n = 1; /* Default */ 5649b50d902SRodney W. Grimes s->p = 0; 5659b50d902SRodney W. Grimes s->wfile = NULL; 5669b50d902SRodney W. Grimes s->wfd = -1; 567bdd72b70SSuleiman Souhlal s->icase = 0; 5689b50d902SRodney W. Grimes for (gn = 0;;) { 5699b50d902SRodney W. Grimes EATSPACE(); /* EXTENSION */ 5709b50d902SRodney W. Grimes switch (*p) { 5719b50d902SRodney W. Grimes case 'g': 5729b50d902SRodney W. Grimes if (gn) 57373a08bb2SPhilippe Charnier errx(1, 57473a08bb2SPhilippe Charnier "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); 5759b50d902SRodney W. Grimes gn = 1; 5769b50d902SRodney W. Grimes s->n = 0; 5779b50d902SRodney W. Grimes break; 5789b50d902SRodney W. Grimes case '\0': 5799b50d902SRodney W. Grimes case '\n': 5809b50d902SRodney W. Grimes case ';': 5819b50d902SRodney W. Grimes return (p); 5829b50d902SRodney W. Grimes case 'p': 5839b50d902SRodney W. Grimes s->p = 1; 5849b50d902SRodney W. Grimes break; 58549e89014SEitan Adler case 'i': 586bdd72b70SSuleiman Souhlal case 'I': 587bdd72b70SSuleiman Souhlal s->icase = 1; 588bdd72b70SSuleiman Souhlal break; 5899b50d902SRodney W. Grimes case '1': case '2': case '3': 5909b50d902SRodney W. Grimes case '4': case '5': case '6': 5919b50d902SRodney W. Grimes case '7': case '8': case '9': 5929b50d902SRodney W. Grimes if (gn) 59373a08bb2SPhilippe Charnier errx(1, 59473a08bb2SPhilippe Charnier "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); 5959b50d902SRodney W. Grimes gn = 1; 596d432588eSDiomidis Spinellis errno = 0; 597d432588eSDiomidis Spinellis nval = strtol(p, &p, 10); 598d432588eSDiomidis Spinellis if (errno == ERANGE || nval > INT_MAX) 599d432588eSDiomidis Spinellis errx(1, 600d432588eSDiomidis Spinellis "%lu: %s: overflow in the 'N' substitute flag", linenum, fname); 601d432588eSDiomidis Spinellis s->n = nval; 602d432588eSDiomidis Spinellis p--; 6039b50d902SRodney W. Grimes break; 6049b50d902SRodney W. Grimes case 'w': 6059b50d902SRodney W. Grimes p++; 6069b50d902SRodney W. Grimes #ifdef HISTORIC_PRACTICE 6079b50d902SRodney W. Grimes if (*p != ' ') { 60873a08bb2SPhilippe Charnier warnx("%lu: %s: space missing before w wfile", linenum, fname); 6099b50d902SRodney W. Grimes return (p); 6109b50d902SRodney W. Grimes } 6119b50d902SRodney W. Grimes #endif 6129b50d902SRodney W. Grimes EATSPACE(); 6139b50d902SRodney W. Grimes q = wfile; 614a34b42e4SPedro F. Giffuni eq = wfile + sizeof(wfile) - 1; 6159b50d902SRodney W. Grimes while (*p) { 6169b50d902SRodney W. Grimes if (*p == '\n') 6179b50d902SRodney W. Grimes break; 618a34b42e4SPedro F. Giffuni if (q >= eq) 619a34b42e4SPedro F. Giffuni err(1, "wfile too long"); 6209b50d902SRodney W. Grimes *q++ = *p++; 6219b50d902SRodney W. Grimes } 6229b50d902SRodney W. Grimes *q = '\0'; 6239b50d902SRodney W. Grimes if (q == wfile) 62473a08bb2SPhilippe Charnier errx(1, "%lu: %s: no wfile specified", linenum, fname); 6259b50d902SRodney W. Grimes s->wfile = strdup(wfile); 6269b50d902SRodney W. Grimes if (!aflag && (s->wfd = open(wfile, 6279b50d902SRodney W. Grimes O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 6289b50d902SRodney W. Grimes DEFFILEMODE)) == -1) 62973a08bb2SPhilippe Charnier err(1, "%s", wfile); 6309b50d902SRodney W. Grimes return (p); 6319b50d902SRodney W. Grimes default: 63273a08bb2SPhilippe Charnier errx(1, "%lu: %s: bad flag in substitute command: '%c'", 63373a08bb2SPhilippe Charnier linenum, fname, *p); 6349b50d902SRodney W. Grimes break; 6359b50d902SRodney W. Grimes } 6369b50d902SRodney W. Grimes p++; 6379b50d902SRodney W. Grimes } 6389b50d902SRodney W. Grimes } 6399b50d902SRodney W. Grimes 6409b50d902SRodney W. Grimes /* 6419b50d902SRodney W. Grimes * Compile a translation set of strings into a lookup table. 6429b50d902SRodney W. Grimes */ 6439b50d902SRodney W. Grimes static char * 64481a8648aSTim J. Robbins compile_tr(char *p, struct s_tr **py) 6459b50d902SRodney W. Grimes { 64681a8648aSTim J. Robbins struct s_tr *y; 6479b50d902SRodney W. Grimes int i; 64881a8648aSTim J. Robbins const char *op, *np; 6499b50d902SRodney W. Grimes char old[_POSIX2_LINE_MAX + 1]; 6509b50d902SRodney W. Grimes char new[_POSIX2_LINE_MAX + 1]; 65181a8648aSTim J. Robbins size_t oclen, oldlen, nclen, newlen; 65281a8648aSTim J. Robbins mbstate_t mbs1, mbs2; 65381a8648aSTim J. Robbins 65481a8648aSTim J. Robbins if ((*py = y = malloc(sizeof(*y))) == NULL) 65581a8648aSTim J. Robbins err(1, NULL); 65681a8648aSTim J. Robbins y->multis = NULL; 65781a8648aSTim J. Robbins y->nmultis = 0; 6589b50d902SRodney W. Grimes 6599b50d902SRodney W. Grimes if (*p == '\0' || *p == '\\') 66073a08bb2SPhilippe Charnier errx(1, 66173a08bb2SPhilippe Charnier "%lu: %s: transform pattern can not be delimited by newline or backslash", 66273a08bb2SPhilippe Charnier linenum, fname); 66376570d0aSDiomidis Spinellis p = compile_delimited(p, old, 1); 66473a08bb2SPhilippe Charnier if (p == NULL) 66573a08bb2SPhilippe Charnier errx(1, "%lu: %s: unterminated transform source string", 66673a08bb2SPhilippe Charnier linenum, fname); 66776570d0aSDiomidis Spinellis p = compile_delimited(p - 1, new, 1); 66873a08bb2SPhilippe Charnier if (p == NULL) 66973a08bb2SPhilippe Charnier errx(1, "%lu: %s: unterminated transform target string", 67073a08bb2SPhilippe Charnier linenum, fname); 6719b50d902SRodney W. Grimes EATSPACE(); 67281a8648aSTim J. Robbins op = old; 67381a8648aSTim J. Robbins oldlen = mbsrtowcs(NULL, &op, 0, NULL); 67481a8648aSTim J. Robbins if (oldlen == (size_t)-1) 67581a8648aSTim J. Robbins err(1, NULL); 67681a8648aSTim J. Robbins np = new; 67781a8648aSTim J. Robbins newlen = mbsrtowcs(NULL, &np, 0, NULL); 67881a8648aSTim J. Robbins if (newlen == (size_t)-1) 67981a8648aSTim J. Robbins err(1, NULL); 68081a8648aSTim J. Robbins if (newlen != oldlen) 68173a08bb2SPhilippe Charnier errx(1, "%lu: %s: transform strings are not the same length", 68273a08bb2SPhilippe Charnier linenum, fname); 68381a8648aSTim J. Robbins if (MB_CUR_MAX == 1) { 68481a8648aSTim J. Robbins /* 68581a8648aSTim J. Robbins * The single-byte encoding case is easy: generate a 68681a8648aSTim J. Robbins * lookup table. 68781a8648aSTim J. Robbins */ 6889b50d902SRodney W. Grimes for (i = 0; i <= UCHAR_MAX; i++) 68981a8648aSTim J. Robbins y->bytetab[i] = (char)i; 69081a8648aSTim J. Robbins for (; *op; op++, np++) 69181a8648aSTim J. Robbins y->bytetab[(u_char)*op] = *np; 69281a8648aSTim J. Robbins } else { 69381a8648aSTim J. Robbins /* 69481a8648aSTim J. Robbins * Multi-byte encoding case: generate a lookup table as 69581a8648aSTim J. Robbins * above, but only for single-byte characters. The first 69681a8648aSTim J. Robbins * bytes of multi-byte characters have their lookup table 69781a8648aSTim J. Robbins * entries set to 0, which causes do_tr() to search through 69881a8648aSTim J. Robbins * an auxiliary vector of multi-byte mappings. 69981a8648aSTim J. Robbins */ 70081a8648aSTim J. Robbins memset(&mbs1, 0, sizeof(mbs1)); 70181a8648aSTim J. Robbins memset(&mbs2, 0, sizeof(mbs2)); 70281a8648aSTim J. Robbins for (i = 0; i <= UCHAR_MAX; i++) 70381a8648aSTim J. Robbins y->bytetab[i] = (btowc(i) != WEOF) ? i : 0; 70481a8648aSTim J. Robbins while (*op != '\0') { 70581a8648aSTim J. Robbins oclen = mbrlen(op, MB_LEN_MAX, &mbs1); 70681a8648aSTim J. Robbins if (oclen == (size_t)-1 || oclen == (size_t)-2) 70781a8648aSTim J. Robbins errc(1, EILSEQ, NULL); 70881a8648aSTim J. Robbins nclen = mbrlen(np, MB_LEN_MAX, &mbs2); 70981a8648aSTim J. Robbins if (nclen == (size_t)-1 || nclen == (size_t)-2) 71081a8648aSTim J. Robbins errc(1, EILSEQ, NULL); 71181a8648aSTim J. Robbins if (oclen == 1 && nclen == 1) 71281a8648aSTim J. Robbins y->bytetab[(u_char)*op] = *np; 71381a8648aSTim J. Robbins else { 71481a8648aSTim J. Robbins y->bytetab[(u_char)*op] = 0; 71581a8648aSTim J. Robbins y->multis = realloc(y->multis, 71681a8648aSTim J. Robbins (y->nmultis + 1) * sizeof(*y->multis)); 71781a8648aSTim J. Robbins if (y->multis == NULL) 71881a8648aSTim J. Robbins err(1, NULL); 71981a8648aSTim J. Robbins i = y->nmultis++; 72081a8648aSTim J. Robbins y->multis[i].fromlen = oclen; 72181a8648aSTim J. Robbins memcpy(y->multis[i].from, op, oclen); 72281a8648aSTim J. Robbins y->multis[i].tolen = nclen; 72381a8648aSTim J. Robbins memcpy(y->multis[i].to, np, nclen); 72481a8648aSTim J. Robbins } 72581a8648aSTim J. Robbins op += oclen; 72681a8648aSTim J. Robbins np += nclen; 72781a8648aSTim J. Robbins } 72881a8648aSTim J. Robbins } 7299b50d902SRodney W. Grimes return (p); 7309b50d902SRodney W. Grimes } 7319b50d902SRodney W. Grimes 7329b50d902SRodney W. Grimes /* 733*678fec50SPedro F. Giffuni * Compile the text following an a, c, or i command. 7349b50d902SRodney W. Grimes */ 7359b50d902SRodney W. Grimes static char * 736e6478125SDag-Erling Smørgrav compile_text(void) 7379b50d902SRodney W. Grimes { 73849e65599SBruce Evans int asize, esc_nl, size; 7399b50d902SRodney W. Grimes char *text, *p, *op, *s; 7409b50d902SRodney W. Grimes char lbuf[_POSIX2_LINE_MAX + 1]; 7419b50d902SRodney W. Grimes 7429b50d902SRodney W. Grimes asize = 2 * _POSIX2_LINE_MAX + 1; 7438e33c0a0SDavid E. O'Brien if ((text = malloc(asize)) == NULL) 7448e33c0a0SDavid E. O'Brien err(1, "malloc"); 7459b50d902SRodney W. Grimes size = 0; 746c56690efSArchie Cobbs while (cu_fgets(lbuf, sizeof(lbuf), NULL)) { 7479b50d902SRodney W. Grimes op = s = text + size; 7489b50d902SRodney W. Grimes p = lbuf; 74949e65599SBruce Evans for (esc_nl = 0; *p != '\0'; p++) { 75049e65599SBruce Evans if (*p == '\\' && p[1] != '\0' && *++p == '\n') 75149e65599SBruce Evans esc_nl = 1; 7529b50d902SRodney W. Grimes *s++ = *p; 7539b50d902SRodney W. Grimes } 7549b50d902SRodney W. Grimes size += s - op; 75549e65599SBruce Evans if (!esc_nl) { 7569b50d902SRodney W. Grimes *s = '\0'; 7579b50d902SRodney W. Grimes break; 7589b50d902SRodney W. Grimes } 7599b50d902SRodney W. Grimes if (asize - size < _POSIX2_LINE_MAX + 1) { 7609b50d902SRodney W. Grimes asize *= 2; 7618e33c0a0SDavid E. O'Brien if ((text = realloc(text, asize)) == NULL) 7628e33c0a0SDavid E. O'Brien err(1, "realloc"); 7639b50d902SRodney W. Grimes } 7649b50d902SRodney W. Grimes } 76513ede3c0SBrian Somers text[size] = '\0'; 7668e33c0a0SDavid E. O'Brien if ((p = realloc(text, size + 1)) == NULL) 7678e33c0a0SDavid E. O'Brien err(1, "realloc"); 7688e33c0a0SDavid E. O'Brien return (p); 7699b50d902SRodney W. Grimes } 7709b50d902SRodney W. Grimes 7719b50d902SRodney W. Grimes /* 7729b50d902SRodney W. Grimes * Get an address and return a pointer to the first character after 7739b50d902SRodney W. Grimes * it. Fill the structure pointed to according to the address. 7749b50d902SRodney W. Grimes */ 7759b50d902SRodney W. Grimes static char * 776e6478125SDag-Erling Smørgrav compile_addr(char *p, struct s_addr *a) 7779b50d902SRodney W. Grimes { 778bdd72b70SSuleiman Souhlal char *end, re[_POSIX2_LINE_MAX + 1]; 779bdd72b70SSuleiman Souhlal int icase; 780bdd72b70SSuleiman Souhlal 781bdd72b70SSuleiman Souhlal icase = 0; 7829b50d902SRodney W. Grimes 783f879e8d9SBrian Somers a->type = 0; 7849b50d902SRodney W. Grimes switch (*p) { 7859b50d902SRodney W. Grimes case '\\': /* Context address */ 7869b50d902SRodney W. Grimes ++p; 7879b50d902SRodney W. Grimes /* FALLTHROUGH */ 7889b50d902SRodney W. Grimes case '/': /* Context address */ 78976570d0aSDiomidis Spinellis p = compile_delimited(p, re, 0); 7909b50d902SRodney W. Grimes if (p == NULL) 79173a08bb2SPhilippe Charnier errx(1, "%lu: %s: unterminated regular expression", linenum, fname); 792bdd72b70SSuleiman Souhlal /* Check for case insensitive regexp flag */ 793bdd72b70SSuleiman Souhlal if (*p == 'I') { 794bdd72b70SSuleiman Souhlal icase = 1; 795bdd72b70SSuleiman Souhlal p++; 796bdd72b70SSuleiman Souhlal } 797bdd72b70SSuleiman Souhlal if (*re == '\0') 798bdd72b70SSuleiman Souhlal a->u.r = NULL; 799bdd72b70SSuleiman Souhlal else 800bdd72b70SSuleiman Souhlal a->u.r = compile_re(re, icase); 8019b50d902SRodney W. Grimes a->type = AT_RE; 8029b50d902SRodney W. Grimes return (p); 8039b50d902SRodney W. Grimes 8049b50d902SRodney W. Grimes case '$': /* Last line */ 8059b50d902SRodney W. Grimes a->type = AT_LAST; 8069b50d902SRodney W. Grimes return (p + 1); 807f879e8d9SBrian Somers 808f879e8d9SBrian Somers case '+': /* Relative line number */ 809f879e8d9SBrian Somers a->type = AT_RELLINE; 810f879e8d9SBrian Somers p++; 811f879e8d9SBrian Somers /* FALLTHROUGH */ 8129b50d902SRodney W. Grimes /* Line number */ 8139b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': case '4': 8149b50d902SRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 815f879e8d9SBrian Somers if (a->type == 0) 8169b50d902SRodney W. Grimes a->type = AT_LINE; 8179b50d902SRodney W. Grimes a->u.l = strtol(p, &end, 10); 8189b50d902SRodney W. Grimes return (end); 8199b50d902SRodney W. Grimes default: 82073a08bb2SPhilippe Charnier errx(1, "%lu: %s: expected context address", linenum, fname); 8219b50d902SRodney W. Grimes return (NULL); 8229b50d902SRodney W. Grimes } 8239b50d902SRodney W. Grimes } 8249b50d902SRodney W. Grimes 8259b50d902SRodney W. Grimes /* 8269b50d902SRodney W. Grimes * duptoeol -- 8279b50d902SRodney W. Grimes * Return a copy of all the characters up to \n or \0. 8289b50d902SRodney W. Grimes */ 8299b50d902SRodney W. Grimes static char * 830e6478125SDag-Erling Smørgrav duptoeol(char *s, const char *ctype) 8319b50d902SRodney W. Grimes { 8329b50d902SRodney W. Grimes size_t len; 8339b50d902SRodney W. Grimes int ws; 8348e33c0a0SDavid E. O'Brien char *p, *start; 8359b50d902SRodney W. Grimes 8369b50d902SRodney W. Grimes ws = 0; 8379b50d902SRodney W. Grimes for (start = s; *s != '\0' && *s != '\n'; ++s) 838726aebe5SAndrey A. Chernov ws = isspace((unsigned char)*s); 8399b50d902SRodney W. Grimes *s = '\0'; 8409b50d902SRodney W. Grimes if (ws) 84173a08bb2SPhilippe Charnier warnx("%lu: %s: whitespace after %s", linenum, fname, ctype); 8429b50d902SRodney W. Grimes len = s - start + 1; 8438e33c0a0SDavid E. O'Brien if ((p = malloc(len)) == NULL) 8448e33c0a0SDavid E. O'Brien err(1, "malloc"); 8458e33c0a0SDavid E. O'Brien return (memmove(p, start, len)); 8469b50d902SRodney W. Grimes } 8479b50d902SRodney W. Grimes 8489b50d902SRodney W. Grimes /* 8499b50d902SRodney W. Grimes * Convert goto label names to addresses, and count a and r commands, in 8509b50d902SRodney W. Grimes * the given subset of the script. Free the memory used by labels in b 8519b50d902SRodney W. Grimes * and t commands (but not by :). 8529b50d902SRodney W. Grimes * 8539b50d902SRodney W. Grimes * TODO: Remove } nodes 8549b50d902SRodney W. Grimes */ 8559b50d902SRodney W. Grimes static void 856e6478125SDag-Erling Smørgrav fixuplabel(struct s_command *cp, struct s_command *end) 8579b50d902SRodney W. Grimes { 8589b50d902SRodney W. Grimes 8599b50d902SRodney W. Grimes for (; cp != end; cp = cp->next) 8609b50d902SRodney W. Grimes switch (cp->code) { 8619b50d902SRodney W. Grimes case 'a': 8629b50d902SRodney W. Grimes case 'r': 8639b50d902SRodney W. Grimes appendnum++; 8649b50d902SRodney W. Grimes break; 8659b50d902SRodney W. Grimes case 'b': 8669b50d902SRodney W. Grimes case 't': 8679b50d902SRodney W. Grimes /* Resolve branch target. */ 8689b50d902SRodney W. Grimes if (cp->t == NULL) { 8699b50d902SRodney W. Grimes cp->u.c = NULL; 8709b50d902SRodney W. Grimes break; 8719b50d902SRodney W. Grimes } 8729b50d902SRodney W. Grimes if ((cp->u.c = findlabel(cp->t)) == NULL) 87373a08bb2SPhilippe Charnier errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t); 8749b50d902SRodney W. Grimes free(cp->t); 8759b50d902SRodney W. Grimes break; 8769b50d902SRodney W. Grimes case '{': 8779b50d902SRodney W. Grimes /* Do interior commands. */ 8789b50d902SRodney W. Grimes fixuplabel(cp->u.c, cp->next); 8799b50d902SRodney W. Grimes break; 8809b50d902SRodney W. Grimes } 8819b50d902SRodney W. Grimes } 8829b50d902SRodney W. Grimes 8839b50d902SRodney W. Grimes /* 8849b50d902SRodney W. Grimes * Associate the given command label for later lookup. 8859b50d902SRodney W. Grimes */ 8869b50d902SRodney W. Grimes static void 887e6478125SDag-Erling Smørgrav enterlabel(struct s_command *cp) 8889b50d902SRodney W. Grimes { 889e74bf75fSMark Murray struct labhash **lhp, *lh; 890e74bf75fSMark Murray u_char *p; 891e74bf75fSMark Murray u_int h, c; 8929b50d902SRodney W. Grimes 8939b50d902SRodney W. Grimes for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++) 8949b50d902SRodney W. Grimes h = (h << 5) + h + c; 8959b50d902SRodney W. Grimes lhp = &labels[h & LHMASK]; 8969b50d902SRodney W. Grimes for (lh = *lhp; lh != NULL; lh = lh->lh_next) 8979b50d902SRodney W. Grimes if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0) 89873a08bb2SPhilippe Charnier errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t); 8998e33c0a0SDavid E. O'Brien if ((lh = malloc(sizeof *lh)) == NULL) 9008e33c0a0SDavid E. O'Brien err(1, "malloc"); 9019b50d902SRodney W. Grimes lh->lh_next = *lhp; 9029b50d902SRodney W. Grimes lh->lh_hash = h; 9039b50d902SRodney W. Grimes lh->lh_cmd = cp; 9049b50d902SRodney W. Grimes lh->lh_ref = 0; 9059b50d902SRodney W. Grimes *lhp = lh; 9069b50d902SRodney W. Grimes } 9079b50d902SRodney W. Grimes 9089b50d902SRodney W. Grimes /* 9099b50d902SRodney W. Grimes * Find the label contained in the command l in the command linked 9109b50d902SRodney W. Grimes * list cp. L is excluded from the search. Return NULL if not found. 9119b50d902SRodney W. Grimes */ 9129b50d902SRodney W. Grimes static struct s_command * 913e6478125SDag-Erling Smørgrav findlabel(char *name) 9149b50d902SRodney W. Grimes { 915e74bf75fSMark Murray struct labhash *lh; 916e74bf75fSMark Murray u_char *p; 917e74bf75fSMark Murray u_int h, c; 9189b50d902SRodney W. Grimes 9199b50d902SRodney W. Grimes for (h = 0, p = (u_char *)name; (c = *p) != 0; p++) 9209b50d902SRodney W. Grimes h = (h << 5) + h + c; 9219b50d902SRodney W. Grimes for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) { 9229b50d902SRodney W. Grimes if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) { 9239b50d902SRodney W. Grimes lh->lh_ref = 1; 9249b50d902SRodney W. Grimes return (lh->lh_cmd); 9259b50d902SRodney W. Grimes } 9269b50d902SRodney W. Grimes } 9279b50d902SRodney W. Grimes return (NULL); 9289b50d902SRodney W. Grimes } 9299b50d902SRodney W. Grimes 9309b50d902SRodney W. Grimes /* 9319b50d902SRodney W. Grimes * Warn about any unused labels. As a side effect, release the label hash 9329b50d902SRodney W. Grimes * table space. 9339b50d902SRodney W. Grimes */ 9349b50d902SRodney W. Grimes static void 935e6478125SDag-Erling Smørgrav uselabel(void) 9369b50d902SRodney W. Grimes { 937e74bf75fSMark Murray struct labhash *lh, *next; 938e74bf75fSMark Murray int i; 9399b50d902SRodney W. Grimes 9409b50d902SRodney W. Grimes for (i = 0; i < LHSZ; i++) { 9419b50d902SRodney W. Grimes for (lh = labels[i]; lh != NULL; lh = next) { 9429b50d902SRodney W. Grimes next = lh->lh_next; 9439b50d902SRodney W. Grimes if (!lh->lh_ref) 94473a08bb2SPhilippe Charnier warnx("%lu: %s: unused label '%s'", 94573a08bb2SPhilippe Charnier linenum, fname, lh->lh_cmd->t); 9469b50d902SRodney W. Grimes free(lh); 9479b50d902SRodney W. Grimes } 9489b50d902SRodney W. Grimes } 9499b50d902SRodney W. Grimes } 950