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 * 3. All advertising materials mentioning features or use of this software 189b50d902SRodney W. Grimes * must display the following acknowledgement: 199b50d902SRodney W. Grimes * This product includes software developed by the University of 209b50d902SRodney W. Grimes * California, Berkeley and its contributors. 219b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 229b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 239b50d902SRodney W. Grimes * without specific prior written permission. 249b50d902SRodney W. Grimes * 259b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 269b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 279b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 289b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 299b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 309b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 319b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 329b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 339b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 349b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 359b50d902SRodney W. Grimes * SUCH DAMAGE. 369b50d902SRodney W. Grimes */ 379b50d902SRodney W. Grimes 38e74bf75fSMark Murray #include <sys/cdefs.h> 39e74bf75fSMark Murray __FBSDID("$FreeBSD$"); 40e74bf75fSMark Murray 419b50d902SRodney W. Grimes #ifndef lint 42e74bf75fSMark Murray static const char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93"; 4373a08bb2SPhilippe Charnier #endif 449b50d902SRodney W. Grimes 459b50d902SRodney W. Grimes #include <sys/types.h> 469b50d902SRodney W. Grimes #include <sys/stat.h> 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include <ctype.h> 4973a08bb2SPhilippe Charnier #include <err.h> 509b50d902SRodney W. Grimes #include <fcntl.h> 519b50d902SRodney W. Grimes #include <limits.h> 529b50d902SRodney W. Grimes #include <regex.h> 539b50d902SRodney W. Grimes #include <stdio.h> 549b50d902SRodney W. Grimes #include <stdlib.h> 559b50d902SRodney W. Grimes #include <string.h> 569b50d902SRodney W. Grimes 579b50d902SRodney W. Grimes #include "defs.h" 589b50d902SRodney W. Grimes #include "extern.h" 599b50d902SRodney W. Grimes 609b50d902SRodney W. Grimes #define LHSZ 128 619b50d902SRodney W. Grimes #define LHMASK (LHSZ - 1) 629b50d902SRodney W. Grimes static struct labhash { 639b50d902SRodney W. Grimes struct labhash *lh_next; 649b50d902SRodney W. Grimes u_int lh_hash; 659b50d902SRodney W. Grimes struct s_command *lh_cmd; 669b50d902SRodney W. Grimes int lh_ref; 679b50d902SRodney W. Grimes } *labels[LHSZ]; 689b50d902SRodney W. Grimes 693f330d7dSWarner Losh static char *compile_addr(char *, struct s_addr *); 703f330d7dSWarner Losh static char *compile_ccl(char **, char *); 713f330d7dSWarner Losh static char *compile_delimited(char *, char *); 723f330d7dSWarner Losh static char *compile_flags(char *, struct s_subst *); 733f330d7dSWarner Losh static char *compile_re(char *, regex_t **); 743f330d7dSWarner Losh static char *compile_subst(char *, struct s_subst *); 753f330d7dSWarner Losh static char *compile_text(void); 763f330d7dSWarner Losh static char *compile_tr(char *, char **); 779b50d902SRodney W. Grimes static struct s_command 783f330d7dSWarner Losh **compile_stream(struct s_command **); 793f330d7dSWarner Losh static char *duptoeol(char *, const char *); 803f330d7dSWarner Losh static void enterlabel(struct s_command *); 819b50d902SRodney W. Grimes static struct s_command 823f330d7dSWarner Losh *findlabel(char *); 833f330d7dSWarner Losh static void fixuplabel(struct s_command *, struct s_command *); 843f330d7dSWarner Losh static void uselabel(void); 859b50d902SRodney W. Grimes 869b50d902SRodney W. Grimes /* 879b50d902SRodney W. Grimes * Command specification. This is used to drive the command parser. 889b50d902SRodney W. Grimes */ 899b50d902SRodney W. Grimes struct s_format { 909b50d902SRodney W. Grimes char code; /* Command code */ 919b50d902SRodney W. Grimes int naddr; /* Number of address args */ 929b50d902SRodney W. Grimes enum e_args args; /* Argument type */ 939b50d902SRodney W. Grimes }; 949b50d902SRodney W. Grimes 959b50d902SRodney W. Grimes static struct s_format cmd_fmts[] = { 969b50d902SRodney W. Grimes {'{', 2, GROUP}, 97ce19262dSJordan K. Hubbard {'}', 0, ENDGROUP}, 989b50d902SRodney W. Grimes {'a', 1, TEXT}, 999b50d902SRodney W. Grimes {'b', 2, BRANCH}, 1009b50d902SRodney W. Grimes {'c', 2, TEXT}, 1019b50d902SRodney W. Grimes {'d', 2, EMPTY}, 1029b50d902SRodney W. Grimes {'D', 2, EMPTY}, 1039b50d902SRodney W. Grimes {'g', 2, EMPTY}, 1049b50d902SRodney W. Grimes {'G', 2, EMPTY}, 1059b50d902SRodney W. Grimes {'h', 2, EMPTY}, 1069b50d902SRodney W. Grimes {'H', 2, EMPTY}, 1079b50d902SRodney W. Grimes {'i', 1, TEXT}, 1089b50d902SRodney W. Grimes {'l', 2, EMPTY}, 1099b50d902SRodney W. Grimes {'n', 2, EMPTY}, 1109b50d902SRodney W. Grimes {'N', 2, EMPTY}, 1119b50d902SRodney W. Grimes {'p', 2, EMPTY}, 1129b50d902SRodney W. Grimes {'P', 2, EMPTY}, 1139b50d902SRodney W. Grimes {'q', 1, EMPTY}, 1149b50d902SRodney W. Grimes {'r', 1, RFILE}, 1159b50d902SRodney W. Grimes {'s', 2, SUBST}, 1169b50d902SRodney W. Grimes {'t', 2, BRANCH}, 1179b50d902SRodney W. Grimes {'w', 2, WFILE}, 1189b50d902SRodney W. Grimes {'x', 2, EMPTY}, 1199b50d902SRodney W. Grimes {'y', 2, TR}, 1209b50d902SRodney W. Grimes {'!', 2, NONSEL}, 1219b50d902SRodney W. Grimes {':', 0, LABEL}, 1229b50d902SRodney W. Grimes {'#', 0, COMMENT}, 1239b50d902SRodney W. Grimes {'=', 1, EMPTY}, 1249b50d902SRodney W. Grimes {'\0', 0, COMMENT}, 1259b50d902SRodney W. Grimes }; 1269b50d902SRodney W. Grimes 1279b50d902SRodney W. Grimes /* The compiled program. */ 1289b50d902SRodney W. Grimes struct s_command *prog; 1299b50d902SRodney W. Grimes 1309b50d902SRodney W. Grimes /* 1319b50d902SRodney W. Grimes * Compile the program into prog. 1329b50d902SRodney W. Grimes * Initialise appends. 1339b50d902SRodney W. Grimes */ 1349b50d902SRodney W. Grimes void 1359b50d902SRodney W. Grimes compile() 1369b50d902SRodney W. Grimes { 137ce19262dSJordan K. Hubbard *compile_stream(&prog) = NULL; 1389b50d902SRodney W. Grimes fixuplabel(prog, NULL); 1399b50d902SRodney W. Grimes uselabel(); 1400ea56610SMike Heffner if (appendnum == 0) 1410ea56610SMike Heffner appends = NULL; 1420ea56610SMike Heffner else if ((appends = malloc(sizeof(struct s_appends) * appendnum)) == 1430ea56610SMike Heffner NULL) 1448e33c0a0SDavid E. O'Brien err(1, "malloc"); 1458e33c0a0SDavid E. O'Brien if ((match = malloc((maxnsub + 1) * sizeof(regmatch_t))) == NULL) 1468e33c0a0SDavid E. O'Brien err(1, "malloc"); 1479b50d902SRodney W. Grimes } 1489b50d902SRodney W. Grimes 1499b50d902SRodney W. Grimes #define EATSPACE() do { \ 1509b50d902SRodney W. Grimes if (p) \ 151726aebe5SAndrey A. Chernov while (*p && isspace((unsigned char)*p)) \ 1529b50d902SRodney W. Grimes p++; \ 1539b50d902SRodney W. Grimes } while (0) 1549b50d902SRodney W. Grimes 1559b50d902SRodney W. Grimes static struct s_command ** 156ce19262dSJordan K. Hubbard compile_stream(link) 1579b50d902SRodney W. Grimes struct s_command **link; 1589b50d902SRodney W. Grimes { 159e74bf75fSMark Murray char *p; 1609b50d902SRodney W. Grimes static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ 161ce19262dSJordan K. Hubbard struct s_command *cmd, *cmd2, *stack; 1629b50d902SRodney W. Grimes struct s_format *fp; 1639b50d902SRodney W. Grimes int naddr; /* Number of addresses */ 1649b50d902SRodney W. Grimes 165ce19262dSJordan K. Hubbard stack = 0; 1669b50d902SRodney W. Grimes for (;;) { 167c56690efSArchie Cobbs if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) { 168ce19262dSJordan K. Hubbard if (stack != 0) 16973a08bb2SPhilippe Charnier errx(1, "%lu: %s: unexpected EOF (pending }'s)", 17073a08bb2SPhilippe Charnier linenum, fname); 1719b50d902SRodney W. Grimes return (link); 1729b50d902SRodney W. Grimes } 1739b50d902SRodney W. Grimes 1749b50d902SRodney W. Grimes semicolon: EATSPACE(); 1750467aed3STim J. Robbins if (p) { 1760467aed3STim J. Robbins if (*p == '#' || *p == '\0') 1779b50d902SRodney W. Grimes continue; 1780467aed3STim J. Robbins else if (*p == ';') { 1790467aed3STim J. Robbins p++; 1800467aed3STim J. Robbins goto semicolon; 1810467aed3STim J. Robbins } 1820467aed3STim J. Robbins } 1838e33c0a0SDavid E. O'Brien if ((*link = cmd = malloc(sizeof(struct s_command))) == NULL) 1848e33c0a0SDavid E. O'Brien err(1, "malloc"); 1859b50d902SRodney W. Grimes link = &cmd->next; 1869b50d902SRodney W. Grimes cmd->nonsel = cmd->inrange = 0; 1879b50d902SRodney W. Grimes /* First parse the addresses */ 1889b50d902SRodney W. Grimes naddr = 0; 1899b50d902SRodney W. Grimes 1909b50d902SRodney W. Grimes /* Valid characters to start an address */ 1919b50d902SRodney W. Grimes #define addrchar(c) (strchr("0123456789/\\$", (c))) 1929b50d902SRodney W. Grimes if (addrchar(*p)) { 1939b50d902SRodney W. Grimes naddr++; 1948e33c0a0SDavid E. O'Brien if ((cmd->a1 = malloc(sizeof(struct s_addr))) == NULL) 1958e33c0a0SDavid E. O'Brien err(1, "malloc"); 1969b50d902SRodney W. Grimes p = compile_addr(p, cmd->a1); 1979b50d902SRodney W. Grimes EATSPACE(); /* EXTENSION */ 1989b50d902SRodney W. Grimes if (*p == ',') { 1999b50d902SRodney W. Grimes p++; 2009b50d902SRodney W. Grimes EATSPACE(); /* EXTENSION */ 201ce19262dSJordan K. Hubbard naddr++; 2028e33c0a0SDavid E. O'Brien if ((cmd->a2 = malloc(sizeof(struct s_addr))) 2038e33c0a0SDavid E. O'Brien == NULL) 2048e33c0a0SDavid E. O'Brien err(1, "malloc"); 2059b50d902SRodney W. Grimes p = compile_addr(p, cmd->a2); 206ce19262dSJordan K. Hubbard EATSPACE(); 207ce19262dSJordan K. Hubbard } else 208ce19262dSJordan K. Hubbard cmd->a2 = 0; 209ce19262dSJordan K. Hubbard } else 210ce19262dSJordan K. Hubbard cmd->a1 = cmd->a2 = 0; 2119b50d902SRodney W. Grimes 2129b50d902SRodney W. Grimes nonsel: /* Now parse the command */ 2139b50d902SRodney W. Grimes if (!*p) 21473a08bb2SPhilippe Charnier errx(1, "%lu: %s: command expected", linenum, fname); 2159b50d902SRodney W. Grimes cmd->code = *p; 2169b50d902SRodney W. Grimes for (fp = cmd_fmts; fp->code; fp++) 2179b50d902SRodney W. Grimes if (fp->code == *p) 2189b50d902SRodney W. Grimes break; 2199b50d902SRodney W. Grimes if (!fp->code) 22073a08bb2SPhilippe Charnier errx(1, "%lu: %s: invalid command code %c", linenum, fname, *p); 2219b50d902SRodney W. Grimes if (naddr > fp->naddr) 22273a08bb2SPhilippe Charnier errx(1, 22373a08bb2SPhilippe Charnier "%lu: %s: command %c expects up to %d address(es), found %d", 22473a08bb2SPhilippe Charnier linenum, fname, *p, fp->naddr, naddr); 2259b50d902SRodney W. Grimes switch (fp->args) { 2269b50d902SRodney W. Grimes case NONSEL: /* ! */ 2279b50d902SRodney W. Grimes p++; 228ce19262dSJordan K. Hubbard EATSPACE(); 229ce19262dSJordan K. Hubbard cmd->nonsel = ! cmd->nonsel; 2309b50d902SRodney W. Grimes goto nonsel; 2319b50d902SRodney W. Grimes case GROUP: /* { */ 2329b50d902SRodney W. Grimes p++; 2339b50d902SRodney W. Grimes EATSPACE(); 234ce19262dSJordan K. Hubbard cmd->next = stack; 235ce19262dSJordan K. Hubbard stack = cmd; 236ce19262dSJordan K. Hubbard link = &cmd->u.c; 237ce19262dSJordan K. Hubbard if (*p) 238ce19262dSJordan K. Hubbard goto semicolon; 2399b50d902SRodney W. Grimes break; 240ce19262dSJordan K. Hubbard case ENDGROUP: 241ce19262dSJordan K. Hubbard /* 242ce19262dSJordan K. Hubbard * Short-circuit command processing, since end of 243ce19262dSJordan K. Hubbard * group is really just a noop. 244ce19262dSJordan K. Hubbard */ 245ce19262dSJordan K. Hubbard cmd->nonsel = 1; 246ce19262dSJordan K. Hubbard if (stack == 0) 24773a08bb2SPhilippe Charnier errx(1, "%lu: %s: unexpected }", linenum, fname); 248ce19262dSJordan K. Hubbard cmd2 = stack; 249ce19262dSJordan K. Hubbard stack = cmd2->next; 250ce19262dSJordan K. Hubbard cmd2->next = cmd; 251ce19262dSJordan K. Hubbard /*FALLTHROUGH*/ 2529b50d902SRodney W. Grimes case EMPTY: /* d D g G h H l n N p P q x = \0 */ 2539b50d902SRodney W. Grimes p++; 2549b50d902SRodney W. Grimes EATSPACE(); 2559b50d902SRodney W. Grimes if (*p == ';') { 2569b50d902SRodney W. Grimes p++; 2579b50d902SRodney W. Grimes link = &cmd->next; 2589b50d902SRodney W. Grimes goto semicolon; 2599b50d902SRodney W. Grimes } 2609b50d902SRodney W. Grimes if (*p) 26173a08bb2SPhilippe Charnier errx(1, "%lu: %s: extra characters at the end of %c command", 26273a08bb2SPhilippe Charnier linenum, fname, cmd->code); 2639b50d902SRodney W. Grimes break; 2649b50d902SRodney W. Grimes case TEXT: /* a c i */ 2659b50d902SRodney W. Grimes p++; 2669b50d902SRodney W. Grimes EATSPACE(); 2679b50d902SRodney W. Grimes if (*p != '\\') 26873a08bb2SPhilippe Charnier errx(1, 26973a08bb2SPhilippe Charnier "%lu: %s: command %c expects \\ followed by text", linenum, fname, cmd->code); 2709b50d902SRodney W. Grimes p++; 2719b50d902SRodney W. Grimes EATSPACE(); 2729b50d902SRodney W. Grimes if (*p) 27373a08bb2SPhilippe Charnier errx(1, 27473a08bb2SPhilippe Charnier "%lu: %s: extra characters after \\ at the end of %c command", 27573a08bb2SPhilippe Charnier linenum, fname, cmd->code); 2769b50d902SRodney W. Grimes cmd->t = compile_text(); 2779b50d902SRodney W. Grimes break; 2789b50d902SRodney W. Grimes case COMMENT: /* \0 # */ 2799b50d902SRodney W. Grimes break; 2809b50d902SRodney W. Grimes case WFILE: /* w */ 2819b50d902SRodney W. Grimes p++; 2829b50d902SRodney W. Grimes EATSPACE(); 2839b50d902SRodney W. Grimes if (*p == '\0') 28473a08bb2SPhilippe Charnier errx(1, "%lu: %s: filename expected", linenum, fname); 2859b50d902SRodney W. Grimes cmd->t = duptoeol(p, "w command"); 2869b50d902SRodney W. Grimes if (aflag) 2879b50d902SRodney W. Grimes cmd->u.fd = -1; 2889b50d902SRodney W. Grimes else if ((cmd->u.fd = open(p, 2899b50d902SRodney W. Grimes O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 2909b50d902SRodney W. Grimes DEFFILEMODE)) == -1) 29173a08bb2SPhilippe Charnier err(1, "%s", p); 2929b50d902SRodney W. Grimes break; 2939b50d902SRodney W. Grimes case RFILE: /* r */ 2949b50d902SRodney W. Grimes p++; 2959b50d902SRodney W. Grimes EATSPACE(); 2969b50d902SRodney W. Grimes if (*p == '\0') 29773a08bb2SPhilippe Charnier errx(1, "%lu: %s: filename expected", linenum, fname); 2989b50d902SRodney W. Grimes else 2999b50d902SRodney W. Grimes cmd->t = duptoeol(p, "read command"); 3009b50d902SRodney W. Grimes break; 3019b50d902SRodney W. Grimes case BRANCH: /* b t */ 3029b50d902SRodney W. Grimes p++; 3039b50d902SRodney W. Grimes EATSPACE(); 3049b50d902SRodney W. Grimes if (*p == '\0') 3059b50d902SRodney W. Grimes cmd->t = NULL; 3069b50d902SRodney W. Grimes else 3079b50d902SRodney W. Grimes cmd->t = duptoeol(p, "branch"); 3089b50d902SRodney W. Grimes break; 3099b50d902SRodney W. Grimes case LABEL: /* : */ 3109b50d902SRodney W. Grimes p++; 3119b50d902SRodney W. Grimes EATSPACE(); 3129b50d902SRodney W. Grimes cmd->t = duptoeol(p, "label"); 3139b50d902SRodney W. Grimes if (strlen(p) == 0) 31473a08bb2SPhilippe Charnier errx(1, "%lu: %s: empty label", linenum, fname); 3159b50d902SRodney W. Grimes enterlabel(cmd); 3169b50d902SRodney W. Grimes break; 3179b50d902SRodney W. Grimes case SUBST: /* s */ 3189b50d902SRodney W. Grimes p++; 3199b50d902SRodney W. Grimes if (*p == '\0' || *p == '\\') 32073a08bb2SPhilippe Charnier errx(1, 32173a08bb2SPhilippe Charnier "%lu: %s: substitute pattern can not be delimited by newline or backslash", 32273a08bb2SPhilippe Charnier linenum, fname); 3238e33c0a0SDavid E. O'Brien if ((cmd->u.s = malloc(sizeof(struct s_subst))) == NULL) 3248e33c0a0SDavid E. O'Brien err(1, "malloc"); 3259b50d902SRodney W. Grimes p = compile_re(p, &cmd->u.s->re); 3269b50d902SRodney W. Grimes if (p == NULL) 32773a08bb2SPhilippe Charnier errx(1, 32873a08bb2SPhilippe Charnier "%lu: %s: unterminated substitute pattern", linenum, fname); 3299b50d902SRodney W. Grimes --p; 3309b50d902SRodney W. Grimes p = compile_subst(p, cmd->u.s); 3319b50d902SRodney W. Grimes p = compile_flags(p, cmd->u.s); 3329b50d902SRodney W. Grimes EATSPACE(); 3339b50d902SRodney W. Grimes if (*p == ';') { 3349b50d902SRodney W. Grimes p++; 3359b50d902SRodney W. Grimes link = &cmd->next; 3369b50d902SRodney W. Grimes goto semicolon; 3379b50d902SRodney W. Grimes } 3389b50d902SRodney W. Grimes break; 3399b50d902SRodney W. Grimes case TR: /* y */ 3409b50d902SRodney W. Grimes p++; 3419b50d902SRodney W. Grimes p = compile_tr(p, (char **)&cmd->u.y); 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 if (*p) 34973a08bb2SPhilippe Charnier errx(1, 35073a08bb2SPhilippe Charnier "%lu: %s: extra text at the end of a transform command", linenum, fname); 3519b50d902SRodney W. Grimes break; 3529b50d902SRodney W. Grimes } 3539b50d902SRodney W. Grimes } 3549b50d902SRodney W. Grimes } 3559b50d902SRodney W. Grimes 3569b50d902SRodney W. Grimes /* 3579b50d902SRodney W. Grimes * Get a delimited string. P points to the delimeter of the string; d points 3589b50d902SRodney W. Grimes * to a buffer area. Newline and delimiter escapes are processed; other 3599b50d902SRodney W. Grimes * escapes are ignored. 3609b50d902SRodney W. Grimes * 3619b50d902SRodney W. Grimes * Returns a pointer to the first character after the final delimiter or NULL 3629b50d902SRodney W. Grimes * in the case of a non-terminated string. The character array d is filled 3639b50d902SRodney W. Grimes * with the processed string. 3649b50d902SRodney W. Grimes */ 3659b50d902SRodney W. Grimes static char * 3669b50d902SRodney W. Grimes compile_delimited(p, d) 3679b50d902SRodney W. Grimes char *p, *d; 3689b50d902SRodney W. Grimes { 3699b50d902SRodney W. Grimes char c; 3709b50d902SRodney W. Grimes 3719b50d902SRodney W. Grimes c = *p++; 3729b50d902SRodney W. Grimes if (c == '\0') 3739b50d902SRodney W. Grimes return (NULL); 3749b50d902SRodney W. Grimes else if (c == '\\') 37573a08bb2SPhilippe Charnier errx(1, "%lu: %s: \\ can not be used as a string delimiter", 37673a08bb2SPhilippe Charnier linenum, fname); 3779b50d902SRodney W. Grimes else if (c == '\n') 37873a08bb2SPhilippe Charnier errx(1, "%lu: %s: newline can not be used as a string delimiter", 37973a08bb2SPhilippe Charnier linenum, fname); 3809b50d902SRodney W. Grimes while (*p) { 381ce19262dSJordan K. Hubbard if (*p == '[') { 382ce19262dSJordan K. Hubbard if ((d = compile_ccl(&p, d)) == NULL) 38373a08bb2SPhilippe Charnier errx(1, "%lu: %s: unbalanced brackets ([])", linenum, fname); 384ce19262dSJordan K. Hubbard continue; 385ce19262dSJordan K. Hubbard } else if (*p == '\\' && p[1] == '[') { 386ce19262dSJordan K. Hubbard *d++ = *p++; 387ce19262dSJordan K. Hubbard } else if (*p == '\\' && p[1] == c) 3889b50d902SRodney W. Grimes p++; 3899b50d902SRodney W. Grimes else if (*p == '\\' && p[1] == 'n') { 3909b50d902SRodney W. Grimes *d++ = '\n'; 3919b50d902SRodney W. Grimes p += 2; 3929b50d902SRodney W. Grimes continue; 3939b50d902SRodney W. Grimes } else if (*p == '\\' && p[1] == '\\') 3949b50d902SRodney W. Grimes *d++ = *p++; 3959b50d902SRodney W. Grimes else if (*p == c) { 3969b50d902SRodney W. Grimes *d = '\0'; 3979b50d902SRodney W. Grimes return (p + 1); 3989b50d902SRodney W. Grimes } 3999b50d902SRodney W. Grimes *d++ = *p++; 4009b50d902SRodney W. Grimes } 4019b50d902SRodney W. Grimes return (NULL); 4029b50d902SRodney W. Grimes } 4039b50d902SRodney W. Grimes 404ce19262dSJordan K. Hubbard 405ce19262dSJordan K. Hubbard /* compile_ccl: expand a POSIX character class */ 406ce19262dSJordan K. Hubbard static char * 407ce19262dSJordan K. Hubbard compile_ccl(sp, t) 408ce19262dSJordan K. Hubbard char **sp; 409ce19262dSJordan K. Hubbard char *t; 410ce19262dSJordan K. Hubbard { 411ce19262dSJordan K. Hubbard int c, d; 412ce19262dSJordan K. Hubbard char *s = *sp; 413ce19262dSJordan K. Hubbard 414ce19262dSJordan K. Hubbard *t++ = *s++; 415ce19262dSJordan K. Hubbard if (*s == '^') 416ce19262dSJordan K. Hubbard *t++ = *s++; 417ce19262dSJordan K. Hubbard if (*s == ']') 418ce19262dSJordan K. Hubbard *t++ = *s++; 419ce19262dSJordan K. Hubbard for (; *s && (*t = *s) != ']'; s++, t++) 420ce19262dSJordan K. Hubbard if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) { 421ce19262dSJordan K. Hubbard *++t = *++s, t++, s++; 422ce19262dSJordan K. Hubbard for (c = *s; (*t = *s) != ']' || c != d; s++, t++) 423ce19262dSJordan K. Hubbard if ((c = *s) == '\0') 424ce19262dSJordan K. Hubbard return NULL; 425ce19262dSJordan K. Hubbard } else if (*s == '\\' && s[1] == 'n') 426ce19262dSJordan K. Hubbard *t = '\n', s++; 427ce19262dSJordan K. Hubbard return (*s == ']') ? *sp = ++s, ++t : NULL; 428ce19262dSJordan K. Hubbard } 429ce19262dSJordan K. Hubbard 4309b50d902SRodney W. Grimes /* 4319b50d902SRodney W. Grimes * Get a regular expression. P points to the delimiter of the regular 4329b50d902SRodney W. Grimes * expression; repp points to the address of a regexp pointer. Newline 4339b50d902SRodney W. Grimes * and delimiter escapes are processed; other escapes are ignored. 4349b50d902SRodney W. Grimes * Returns a pointer to the first character after the final delimiter 4359b50d902SRodney W. Grimes * or NULL in the case of a non terminated regular expression. The regexp 4369b50d902SRodney W. Grimes * pointer is set to the compiled regular expression. 4379b50d902SRodney W. Grimes * Cflags are passed to regcomp. 4389b50d902SRodney W. Grimes */ 4399b50d902SRodney W. Grimes static char * 4409b50d902SRodney W. Grimes compile_re(p, repp) 4419b50d902SRodney W. Grimes char *p; 4429b50d902SRodney W. Grimes regex_t **repp; 4439b50d902SRodney W. Grimes { 4449b50d902SRodney W. Grimes int eval; 4459b50d902SRodney W. Grimes char re[_POSIX2_LINE_MAX + 1]; 4469b50d902SRodney W. Grimes 4479b50d902SRodney W. Grimes p = compile_delimited(p, re); 4489b50d902SRodney W. Grimes if (p && strlen(re) == 0) { 4499b50d902SRodney W. Grimes *repp = NULL; 4509b50d902SRodney W. Grimes return (p); 4519b50d902SRodney W. Grimes } 4528e33c0a0SDavid E. O'Brien if ((*repp = malloc(sizeof(regex_t))) == NULL) 4538e33c0a0SDavid E. O'Brien err(1, "malloc"); 454175de1e6SBrian Feldman if (p && (eval = regcomp(*repp, re, rflags)) != 0) 45573a08bb2SPhilippe Charnier errx(1, "%lu: %s: RE error: %s", 45673a08bb2SPhilippe Charnier linenum, fname, strregerror(eval, *repp)); 4579b50d902SRodney W. Grimes if (maxnsub < (*repp)->re_nsub) 4589b50d902SRodney W. Grimes maxnsub = (*repp)->re_nsub; 4599b50d902SRodney W. Grimes return (p); 4609b50d902SRodney W. Grimes } 4619b50d902SRodney W. Grimes 4629b50d902SRodney W. Grimes /* 4639b50d902SRodney W. Grimes * Compile the substitution string of a regular expression and set res to 4649b50d902SRodney W. Grimes * point to a saved copy of it. Nsub is the number of parenthesized regular 4659b50d902SRodney W. Grimes * expressions. 4669b50d902SRodney W. Grimes */ 4679b50d902SRodney W. Grimes static char * 4689b50d902SRodney W. Grimes compile_subst(p, s) 4699b50d902SRodney W. Grimes char *p; 4709b50d902SRodney W. Grimes 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 * 5579b50d902SRodney W. Grimes compile_flags(p, s) 5589b50d902SRodney W. Grimes char *p; 5599b50d902SRodney W. Grimes struct s_subst *s; 5609b50d902SRodney W. Grimes { 5619b50d902SRodney W. Grimes int gn; /* True if we have seen g or n */ 5629b50d902SRodney W. Grimes char wfile[_POSIX2_LINE_MAX + 1], *q; 5639b50d902SRodney W. Grimes 5649b50d902SRodney W. Grimes s->n = 1; /* Default */ 5659b50d902SRodney W. Grimes s->p = 0; 5669b50d902SRodney W. Grimes s->wfile = NULL; 5679b50d902SRodney W. Grimes s->wfd = -1; 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; 5859b50d902SRodney W. Grimes case '1': case '2': case '3': 5869b50d902SRodney W. Grimes case '4': case '5': case '6': 5879b50d902SRodney W. Grimes case '7': case '8': case '9': 5889b50d902SRodney W. Grimes if (gn) 58973a08bb2SPhilippe Charnier errx(1, 59073a08bb2SPhilippe Charnier "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); 5919b50d902SRodney W. Grimes gn = 1; 5929b50d902SRodney W. Grimes /* XXX Check for overflow */ 5939b50d902SRodney W. Grimes s->n = (int)strtol(p, &p, 10); 5949b50d902SRodney W. Grimes break; 5959b50d902SRodney W. Grimes case 'w': 5969b50d902SRodney W. Grimes p++; 5979b50d902SRodney W. Grimes #ifdef HISTORIC_PRACTICE 5989b50d902SRodney W. Grimes if (*p != ' ') { 59973a08bb2SPhilippe Charnier warnx("%lu: %s: space missing before w wfile", linenum, fname); 6009b50d902SRodney W. Grimes return (p); 6019b50d902SRodney W. Grimes } 6029b50d902SRodney W. Grimes #endif 6039b50d902SRodney W. Grimes EATSPACE(); 6049b50d902SRodney W. Grimes q = wfile; 6059b50d902SRodney W. Grimes while (*p) { 6069b50d902SRodney W. Grimes if (*p == '\n') 6079b50d902SRodney W. Grimes break; 6089b50d902SRodney W. Grimes *q++ = *p++; 6099b50d902SRodney W. Grimes } 6109b50d902SRodney W. Grimes *q = '\0'; 6119b50d902SRodney W. Grimes if (q == wfile) 61273a08bb2SPhilippe Charnier errx(1, "%lu: %s: no wfile specified", linenum, fname); 6139b50d902SRodney W. Grimes s->wfile = strdup(wfile); 6149b50d902SRodney W. Grimes if (!aflag && (s->wfd = open(wfile, 6159b50d902SRodney W. Grimes O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 6169b50d902SRodney W. Grimes DEFFILEMODE)) == -1) 61773a08bb2SPhilippe Charnier err(1, "%s", wfile); 6189b50d902SRodney W. Grimes return (p); 6199b50d902SRodney W. Grimes default: 62073a08bb2SPhilippe Charnier errx(1, "%lu: %s: bad flag in substitute command: '%c'", 62173a08bb2SPhilippe Charnier linenum, fname, *p); 6229b50d902SRodney W. Grimes break; 6239b50d902SRodney W. Grimes } 6249b50d902SRodney W. Grimes p++; 6259b50d902SRodney W. Grimes } 6269b50d902SRodney W. Grimes } 6279b50d902SRodney W. Grimes 6289b50d902SRodney W. Grimes /* 6299b50d902SRodney W. Grimes * Compile a translation set of strings into a lookup table. 6309b50d902SRodney W. Grimes */ 6319b50d902SRodney W. Grimes static char * 6329b50d902SRodney W. Grimes compile_tr(p, transtab) 6339b50d902SRodney W. Grimes char *p; 6349b50d902SRodney W. Grimes char **transtab; 6359b50d902SRodney W. Grimes { 6369b50d902SRodney W. Grimes int i; 6379b50d902SRodney W. Grimes char *lt, *op, *np; 6389b50d902SRodney W. Grimes char old[_POSIX2_LINE_MAX + 1]; 6399b50d902SRodney W. Grimes char new[_POSIX2_LINE_MAX + 1]; 6409b50d902SRodney W. Grimes 6419b50d902SRodney W. Grimes if (*p == '\0' || *p == '\\') 64273a08bb2SPhilippe Charnier errx(1, 64373a08bb2SPhilippe Charnier "%lu: %s: transform pattern can not be delimited by newline or backslash", 64473a08bb2SPhilippe Charnier linenum, fname); 6459b50d902SRodney W. Grimes p = compile_delimited(p, old); 64673a08bb2SPhilippe Charnier if (p == NULL) 64773a08bb2SPhilippe Charnier errx(1, "%lu: %s: unterminated transform source string", 64873a08bb2SPhilippe Charnier linenum, fname); 6499b50d902SRodney W. Grimes p = compile_delimited(--p, new); 65073a08bb2SPhilippe Charnier if (p == NULL) 65173a08bb2SPhilippe Charnier errx(1, "%lu: %s: unterminated transform target string", 65273a08bb2SPhilippe Charnier linenum, fname); 6539b50d902SRodney W. Grimes EATSPACE(); 65473a08bb2SPhilippe Charnier if (strlen(new) != strlen(old)) 65573a08bb2SPhilippe Charnier errx(1, "%lu: %s: transform strings are not the same length", 65673a08bb2SPhilippe Charnier linenum, fname); 6579b50d902SRodney W. Grimes /* We assume characters are 8 bits */ 6588e33c0a0SDavid E. O'Brien if ((lt = malloc(UCHAR_MAX)) == NULL) 6598e33c0a0SDavid E. O'Brien err(1, "malloc"); 6609b50d902SRodney W. Grimes for (i = 0; i <= UCHAR_MAX; i++) 6619b50d902SRodney W. Grimes lt[i] = (char)i; 6629b50d902SRodney W. Grimes for (op = old, np = new; *op; op++, np++) 6639b50d902SRodney W. Grimes lt[(u_char)*op] = *np; 6649b50d902SRodney W. Grimes *transtab = lt; 6659b50d902SRodney W. Grimes return (p); 6669b50d902SRodney W. Grimes } 6679b50d902SRodney W. Grimes 6689b50d902SRodney W. Grimes /* 6699b50d902SRodney W. Grimes * Compile the text following an a or i command. 6709b50d902SRodney W. Grimes */ 6719b50d902SRodney W. Grimes static char * 6729b50d902SRodney W. Grimes compile_text() 6739b50d902SRodney W. Grimes { 67449e65599SBruce Evans int asize, esc_nl, size; 6759b50d902SRodney W. Grimes char *text, *p, *op, *s; 6769b50d902SRodney W. Grimes char lbuf[_POSIX2_LINE_MAX + 1]; 6779b50d902SRodney W. Grimes 6789b50d902SRodney W. Grimes asize = 2 * _POSIX2_LINE_MAX + 1; 6798e33c0a0SDavid E. O'Brien if ((text = malloc(asize)) == NULL) 6808e33c0a0SDavid E. O'Brien err(1, "malloc"); 6819b50d902SRodney W. Grimes size = 0; 682c56690efSArchie Cobbs while (cu_fgets(lbuf, sizeof(lbuf), NULL)) { 6839b50d902SRodney W. Grimes op = s = text + size; 6849b50d902SRodney W. Grimes p = lbuf; 6859b50d902SRodney W. Grimes EATSPACE(); 68649e65599SBruce Evans for (esc_nl = 0; *p != '\0'; p++) { 68749e65599SBruce Evans if (*p == '\\' && p[1] != '\0' && *++p == '\n') 68849e65599SBruce Evans esc_nl = 1; 6899b50d902SRodney W. Grimes *s++ = *p; 6909b50d902SRodney W. Grimes } 6919b50d902SRodney W. Grimes size += s - op; 69249e65599SBruce Evans if (!esc_nl) { 6939b50d902SRodney W. Grimes *s = '\0'; 6949b50d902SRodney W. Grimes break; 6959b50d902SRodney W. Grimes } 6969b50d902SRodney W. Grimes if (asize - size < _POSIX2_LINE_MAX + 1) { 6979b50d902SRodney W. Grimes asize *= 2; 6988e33c0a0SDavid E. O'Brien if ((text = realloc(text, asize)) == NULL) 6998e33c0a0SDavid E. O'Brien err(1, "realloc"); 7009b50d902SRodney W. Grimes } 7019b50d902SRodney W. Grimes } 70213ede3c0SBrian Somers text[size] = '\0'; 7038e33c0a0SDavid E. O'Brien if ((p = realloc(text, size + 1)) == NULL) 7048e33c0a0SDavid E. O'Brien err(1, "realloc"); 7058e33c0a0SDavid E. O'Brien return (p); 7069b50d902SRodney W. Grimes } 7079b50d902SRodney W. Grimes 7089b50d902SRodney W. Grimes /* 7099b50d902SRodney W. Grimes * Get an address and return a pointer to the first character after 7109b50d902SRodney W. Grimes * it. Fill the structure pointed to according to the address. 7119b50d902SRodney W. Grimes */ 7129b50d902SRodney W. Grimes static char * 7139b50d902SRodney W. Grimes compile_addr(p, a) 7149b50d902SRodney W. Grimes char *p; 7159b50d902SRodney W. Grimes struct s_addr *a; 7169b50d902SRodney W. Grimes { 7179b50d902SRodney W. Grimes char *end; 7189b50d902SRodney W. Grimes 7199b50d902SRodney W. Grimes switch (*p) { 7209b50d902SRodney W. Grimes case '\\': /* Context address */ 7219b50d902SRodney W. Grimes ++p; 7229b50d902SRodney W. Grimes /* FALLTHROUGH */ 7239b50d902SRodney W. Grimes case '/': /* Context address */ 7249b50d902SRodney W. Grimes p = compile_re(p, &a->u.r); 7259b50d902SRodney W. Grimes if (p == NULL) 72673a08bb2SPhilippe Charnier errx(1, "%lu: %s: unterminated regular expression", linenum, fname); 7279b50d902SRodney W. Grimes a->type = AT_RE; 7289b50d902SRodney W. Grimes return (p); 7299b50d902SRodney W. Grimes 7309b50d902SRodney W. Grimes case '$': /* Last line */ 7319b50d902SRodney W. Grimes a->type = AT_LAST; 7329b50d902SRodney W. Grimes return (p + 1); 7339b50d902SRodney W. Grimes /* Line number */ 7349b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': case '4': 7359b50d902SRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 7369b50d902SRodney W. Grimes a->type = AT_LINE; 7379b50d902SRodney W. Grimes a->u.l = strtol(p, &end, 10); 7389b50d902SRodney W. Grimes return (end); 7399b50d902SRodney W. Grimes default: 74073a08bb2SPhilippe Charnier errx(1, "%lu: %s: expected context address", linenum, fname); 7419b50d902SRodney W. Grimes return (NULL); 7429b50d902SRodney W. Grimes } 7439b50d902SRodney W. Grimes } 7449b50d902SRodney W. Grimes 7459b50d902SRodney W. Grimes /* 7469b50d902SRodney W. Grimes * duptoeol -- 7479b50d902SRodney W. Grimes * Return a copy of all the characters up to \n or \0. 7489b50d902SRodney W. Grimes */ 7499b50d902SRodney W. Grimes static char * 7509b50d902SRodney W. Grimes duptoeol(s, ctype) 751e74bf75fSMark Murray char *s; 752e74bf75fSMark Murray const char *ctype; 7539b50d902SRodney W. Grimes { 7549b50d902SRodney W. Grimes size_t len; 7559b50d902SRodney W. Grimes int ws; 7568e33c0a0SDavid E. O'Brien char *p, *start; 7579b50d902SRodney W. Grimes 7589b50d902SRodney W. Grimes ws = 0; 7599b50d902SRodney W. Grimes for (start = s; *s != '\0' && *s != '\n'; ++s) 760726aebe5SAndrey A. Chernov ws = isspace((unsigned char)*s); 7619b50d902SRodney W. Grimes *s = '\0'; 7629b50d902SRodney W. Grimes if (ws) 76373a08bb2SPhilippe Charnier warnx("%lu: %s: whitespace after %s", linenum, fname, ctype); 7649b50d902SRodney W. Grimes len = s - start + 1; 7658e33c0a0SDavid E. O'Brien if ((p = malloc(len)) == NULL) 7668e33c0a0SDavid E. O'Brien err(1, "malloc"); 7678e33c0a0SDavid E. O'Brien return (memmove(p, start, len)); 7689b50d902SRodney W. Grimes } 7699b50d902SRodney W. Grimes 7709b50d902SRodney W. Grimes /* 7719b50d902SRodney W. Grimes * Convert goto label names to addresses, and count a and r commands, in 7729b50d902SRodney W. Grimes * the given subset of the script. Free the memory used by labels in b 7739b50d902SRodney W. Grimes * and t commands (but not by :). 7749b50d902SRodney W. Grimes * 7759b50d902SRodney W. Grimes * TODO: Remove } nodes 7769b50d902SRodney W. Grimes */ 7779b50d902SRodney W. Grimes static void 7789b50d902SRodney W. Grimes fixuplabel(cp, end) 7799b50d902SRodney W. Grimes struct s_command *cp, *end; 7809b50d902SRodney W. Grimes { 7819b50d902SRodney W. Grimes 7829b50d902SRodney W. Grimes for (; cp != end; cp = cp->next) 7839b50d902SRodney W. Grimes switch (cp->code) { 7849b50d902SRodney W. Grimes case 'a': 7859b50d902SRodney W. Grimes case 'r': 7869b50d902SRodney W. Grimes appendnum++; 7879b50d902SRodney W. Grimes break; 7889b50d902SRodney W. Grimes case 'b': 7899b50d902SRodney W. Grimes case 't': 7909b50d902SRodney W. Grimes /* Resolve branch target. */ 7919b50d902SRodney W. Grimes if (cp->t == NULL) { 7929b50d902SRodney W. Grimes cp->u.c = NULL; 7939b50d902SRodney W. Grimes break; 7949b50d902SRodney W. Grimes } 7959b50d902SRodney W. Grimes if ((cp->u.c = findlabel(cp->t)) == NULL) 79673a08bb2SPhilippe Charnier errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t); 7979b50d902SRodney W. Grimes free(cp->t); 7989b50d902SRodney W. Grimes break; 7999b50d902SRodney W. Grimes case '{': 8009b50d902SRodney W. Grimes /* Do interior commands. */ 8019b50d902SRodney W. Grimes fixuplabel(cp->u.c, cp->next); 8029b50d902SRodney W. Grimes break; 8039b50d902SRodney W. Grimes } 8049b50d902SRodney W. Grimes } 8059b50d902SRodney W. Grimes 8069b50d902SRodney W. Grimes /* 8079b50d902SRodney W. Grimes * Associate the given command label for later lookup. 8089b50d902SRodney W. Grimes */ 8099b50d902SRodney W. Grimes static void 8109b50d902SRodney W. Grimes enterlabel(cp) 8119b50d902SRodney W. Grimes struct s_command *cp; 8129b50d902SRodney W. Grimes { 813e74bf75fSMark Murray struct labhash **lhp, *lh; 814e74bf75fSMark Murray u_char *p; 815e74bf75fSMark Murray u_int h, c; 8169b50d902SRodney W. Grimes 8179b50d902SRodney W. Grimes for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++) 8189b50d902SRodney W. Grimes h = (h << 5) + h + c; 8199b50d902SRodney W. Grimes lhp = &labels[h & LHMASK]; 8209b50d902SRodney W. Grimes for (lh = *lhp; lh != NULL; lh = lh->lh_next) 8219b50d902SRodney W. Grimes if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0) 82273a08bb2SPhilippe Charnier errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t); 8238e33c0a0SDavid E. O'Brien if ((lh = malloc(sizeof *lh)) == NULL) 8248e33c0a0SDavid E. O'Brien err(1, "malloc"); 8259b50d902SRodney W. Grimes lh->lh_next = *lhp; 8269b50d902SRodney W. Grimes lh->lh_hash = h; 8279b50d902SRodney W. Grimes lh->lh_cmd = cp; 8289b50d902SRodney W. Grimes lh->lh_ref = 0; 8299b50d902SRodney W. Grimes *lhp = lh; 8309b50d902SRodney W. Grimes } 8319b50d902SRodney W. Grimes 8329b50d902SRodney W. Grimes /* 8339b50d902SRodney W. Grimes * Find the label contained in the command l in the command linked 8349b50d902SRodney W. Grimes * list cp. L is excluded from the search. Return NULL if not found. 8359b50d902SRodney W. Grimes */ 8369b50d902SRodney W. Grimes static struct s_command * 8379b50d902SRodney W. Grimes findlabel(name) 8389b50d902SRodney W. Grimes char *name; 8399b50d902SRodney W. Grimes { 840e74bf75fSMark Murray struct labhash *lh; 841e74bf75fSMark Murray u_char *p; 842e74bf75fSMark Murray u_int h, c; 8439b50d902SRodney W. Grimes 8449b50d902SRodney W. Grimes for (h = 0, p = (u_char *)name; (c = *p) != 0; p++) 8459b50d902SRodney W. Grimes h = (h << 5) + h + c; 8469b50d902SRodney W. Grimes for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) { 8479b50d902SRodney W. Grimes if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) { 8489b50d902SRodney W. Grimes lh->lh_ref = 1; 8499b50d902SRodney W. Grimes return (lh->lh_cmd); 8509b50d902SRodney W. Grimes } 8519b50d902SRodney W. Grimes } 8529b50d902SRodney W. Grimes return (NULL); 8539b50d902SRodney W. Grimes } 8549b50d902SRodney W. Grimes 8559b50d902SRodney W. Grimes /* 8569b50d902SRodney W. Grimes * Warn about any unused labels. As a side effect, release the label hash 8579b50d902SRodney W. Grimes * table space. 8589b50d902SRodney W. Grimes */ 8599b50d902SRodney W. Grimes static void 8609b50d902SRodney W. Grimes uselabel() 8619b50d902SRodney W. Grimes { 862e74bf75fSMark Murray struct labhash *lh, *next; 863e74bf75fSMark Murray int i; 8649b50d902SRodney W. Grimes 8659b50d902SRodney W. Grimes for (i = 0; i < LHSZ; i++) { 8669b50d902SRodney W. Grimes for (lh = labels[i]; lh != NULL; lh = next) { 8679b50d902SRodney W. Grimes next = lh->lh_next; 8689b50d902SRodney W. Grimes if (!lh->lh_ref) 86973a08bb2SPhilippe Charnier warnx("%lu: %s: unused label '%s'", 87073a08bb2SPhilippe Charnier linenum, fname, lh->lh_cmd->t); 8719b50d902SRodney W. Grimes free(lh); 8729b50d902SRodney W. Grimes } 8739b50d902SRodney W. Grimes } 8749b50d902SRodney W. Grimes } 875