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 389b50d902SRodney W. Grimes #ifndef lint 399b50d902SRodney W. Grimes static char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93"; 409b50d902SRodney W. Grimes #endif /* not lint */ 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #include <sys/types.h> 439b50d902SRodney W. Grimes #include <sys/stat.h> 449b50d902SRodney W. Grimes 459b50d902SRodney W. Grimes #include <ctype.h> 469b50d902SRodney W. Grimes #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> 539b50d902SRodney W. Grimes 549b50d902SRodney W. Grimes #include "defs.h" 559b50d902SRodney W. Grimes #include "extern.h" 569b50d902SRodney W. Grimes 579b50d902SRodney W. Grimes #define LHSZ 128 589b50d902SRodney W. Grimes #define LHMASK (LHSZ - 1) 599b50d902SRodney W. Grimes static struct labhash { 609b50d902SRodney W. Grimes struct labhash *lh_next; 619b50d902SRodney W. Grimes u_int lh_hash; 629b50d902SRodney W. Grimes struct s_command *lh_cmd; 639b50d902SRodney W. Grimes int lh_ref; 649b50d902SRodney W. Grimes } *labels[LHSZ]; 659b50d902SRodney W. Grimes 669b50d902SRodney W. Grimes static char *compile_addr __P((char *, struct s_addr *)); 67ce19262dSJordan K. Hubbard static char *compile_ccl __P((char **, char *)); 689b50d902SRodney W. Grimes static char *compile_delimited __P((char *, char *)); 699b50d902SRodney W. Grimes static char *compile_flags __P((char *, struct s_subst *)); 709b50d902SRodney W. Grimes static char *compile_re __P((char *, regex_t **)); 719b50d902SRodney W. Grimes static char *compile_subst __P((char *, struct s_subst *)); 729b50d902SRodney W. Grimes static char *compile_text __P((void)); 739b50d902SRodney W. Grimes static char *compile_tr __P((char *, char **)); 749b50d902SRodney W. Grimes static struct s_command 75ce19262dSJordan K. Hubbard **compile_stream __P((struct s_command **)); 769b50d902SRodney W. Grimes static char *duptoeol __P((char *, char *)); 779b50d902SRodney W. Grimes static void enterlabel __P((struct s_command *)); 789b50d902SRodney W. Grimes static struct s_command 799b50d902SRodney W. Grimes *findlabel __P((char *)); 809b50d902SRodney W. Grimes static void fixuplabel __P((struct s_command *, struct s_command *)); 819b50d902SRodney W. Grimes static void uselabel __P((void)); 829b50d902SRodney W. Grimes 839b50d902SRodney W. Grimes /* 849b50d902SRodney W. Grimes * Command specification. This is used to drive the command parser. 859b50d902SRodney W. Grimes */ 869b50d902SRodney W. Grimes struct s_format { 879b50d902SRodney W. Grimes char code; /* Command code */ 889b50d902SRodney W. Grimes int naddr; /* Number of address args */ 899b50d902SRodney W. Grimes enum e_args args; /* Argument type */ 909b50d902SRodney W. Grimes }; 919b50d902SRodney W. Grimes 929b50d902SRodney W. Grimes static struct s_format cmd_fmts[] = { 939b50d902SRodney W. Grimes {'{', 2, GROUP}, 94ce19262dSJordan K. Hubbard {'}', 0, ENDGROUP}, 959b50d902SRodney W. Grimes {'a', 1, TEXT}, 969b50d902SRodney W. Grimes {'b', 2, BRANCH}, 979b50d902SRodney W. Grimes {'c', 2, TEXT}, 989b50d902SRodney W. Grimes {'d', 2, EMPTY}, 999b50d902SRodney W. Grimes {'D', 2, EMPTY}, 1009b50d902SRodney W. Grimes {'g', 2, EMPTY}, 1019b50d902SRodney W. Grimes {'G', 2, EMPTY}, 1029b50d902SRodney W. Grimes {'h', 2, EMPTY}, 1039b50d902SRodney W. Grimes {'H', 2, EMPTY}, 1049b50d902SRodney W. Grimes {'i', 1, TEXT}, 1059b50d902SRodney W. Grimes {'l', 2, EMPTY}, 1069b50d902SRodney W. Grimes {'n', 2, EMPTY}, 1079b50d902SRodney W. Grimes {'N', 2, EMPTY}, 1089b50d902SRodney W. Grimes {'p', 2, EMPTY}, 1099b50d902SRodney W. Grimes {'P', 2, EMPTY}, 1109b50d902SRodney W. Grimes {'q', 1, EMPTY}, 1119b50d902SRodney W. Grimes {'r', 1, RFILE}, 1129b50d902SRodney W. Grimes {'s', 2, SUBST}, 1139b50d902SRodney W. Grimes {'t', 2, BRANCH}, 1149b50d902SRodney W. Grimes {'w', 2, WFILE}, 1159b50d902SRodney W. Grimes {'x', 2, EMPTY}, 1169b50d902SRodney W. Grimes {'y', 2, TR}, 1179b50d902SRodney W. Grimes {'!', 2, NONSEL}, 1189b50d902SRodney W. Grimes {':', 0, LABEL}, 1199b50d902SRodney W. Grimes {'#', 0, COMMENT}, 1209b50d902SRodney W. Grimes {'=', 1, EMPTY}, 1219b50d902SRodney W. Grimes {'\0', 0, COMMENT}, 1229b50d902SRodney W. Grimes }; 1239b50d902SRodney W. Grimes 1249b50d902SRodney W. Grimes /* The compiled program. */ 1259b50d902SRodney W. Grimes struct s_command *prog; 1269b50d902SRodney W. Grimes 1279b50d902SRodney W. Grimes /* 1289b50d902SRodney W. Grimes * Compile the program into prog. 1299b50d902SRodney W. Grimes * Initialise appends. 1309b50d902SRodney W. Grimes */ 1319b50d902SRodney W. Grimes void 1329b50d902SRodney W. Grimes compile() 1339b50d902SRodney W. Grimes { 134ce19262dSJordan K. Hubbard *compile_stream(&prog) = NULL; 1359b50d902SRodney W. Grimes fixuplabel(prog, NULL); 1369b50d902SRodney W. Grimes uselabel(); 1379b50d902SRodney W. Grimes appends = xmalloc(sizeof(struct s_appends) * appendnum); 1389b50d902SRodney W. Grimes match = xmalloc((maxnsub + 1) * sizeof(regmatch_t)); 1399b50d902SRodney W. Grimes } 1409b50d902SRodney W. Grimes 1419b50d902SRodney W. Grimes #define EATSPACE() do { \ 1429b50d902SRodney W. Grimes if (p) \ 143726aebe5SAndrey A. Chernov while (*p && isspace((unsigned char)*p)) \ 1449b50d902SRodney W. Grimes p++; \ 1459b50d902SRodney W. Grimes } while (0) 1469b50d902SRodney W. Grimes 1479b50d902SRodney W. Grimes static struct s_command ** 148ce19262dSJordan K. Hubbard compile_stream(link) 1499b50d902SRodney W. Grimes struct s_command **link; 1509b50d902SRodney W. Grimes { 151ce19262dSJordan K. Hubbard register char *p; 1529b50d902SRodney W. Grimes static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ 153ce19262dSJordan K. Hubbard struct s_command *cmd, *cmd2, *stack; 1549b50d902SRodney W. Grimes struct s_format *fp; 1559b50d902SRodney W. Grimes int naddr; /* Number of addresses */ 1569b50d902SRodney W. Grimes 157ce19262dSJordan K. Hubbard stack = 0; 1589b50d902SRodney W. Grimes for (;;) { 1599b50d902SRodney W. Grimes if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) { 160ce19262dSJordan K. Hubbard if (stack != 0) 1619b50d902SRodney W. Grimes err(COMPILE, "unexpected EOF (pending }'s)"); 1629b50d902SRodney W. Grimes return (link); 1639b50d902SRodney W. Grimes } 1649b50d902SRodney W. Grimes 1659b50d902SRodney W. Grimes semicolon: EATSPACE(); 1669b50d902SRodney W. Grimes if (p && (*p == '#' || *p == '\0')) 1679b50d902SRodney W. Grimes continue; 1689b50d902SRodney W. Grimes *link = cmd = xmalloc(sizeof(struct s_command)); 1699b50d902SRodney W. Grimes link = &cmd->next; 1709b50d902SRodney W. Grimes cmd->nonsel = cmd->inrange = 0; 1719b50d902SRodney W. Grimes /* First parse the addresses */ 1729b50d902SRodney W. Grimes naddr = 0; 1739b50d902SRodney W. Grimes 1749b50d902SRodney W. Grimes /* Valid characters to start an address */ 1759b50d902SRodney W. Grimes #define addrchar(c) (strchr("0123456789/\\$", (c))) 1769b50d902SRodney W. Grimes if (addrchar(*p)) { 1779b50d902SRodney W. Grimes naddr++; 1789b50d902SRodney W. Grimes cmd->a1 = xmalloc(sizeof(struct s_addr)); 1799b50d902SRodney W. Grimes p = compile_addr(p, cmd->a1); 1809b50d902SRodney W. Grimes EATSPACE(); /* EXTENSION */ 1819b50d902SRodney W. Grimes if (*p == ',') { 1829b50d902SRodney W. Grimes p++; 1839b50d902SRodney W. Grimes EATSPACE(); /* EXTENSION */ 184ce19262dSJordan K. Hubbard naddr++; 1859b50d902SRodney W. Grimes cmd->a2 = xmalloc(sizeof(struct s_addr)); 1869b50d902SRodney W. Grimes p = compile_addr(p, cmd->a2); 187ce19262dSJordan K. Hubbard EATSPACE(); 188ce19262dSJordan K. Hubbard } else 189ce19262dSJordan K. Hubbard cmd->a2 = 0; 190ce19262dSJordan K. Hubbard } else 191ce19262dSJordan K. Hubbard cmd->a1 = cmd->a2 = 0; 1929b50d902SRodney W. Grimes 1939b50d902SRodney W. Grimes nonsel: /* Now parse the command */ 1949b50d902SRodney W. Grimes if (!*p) 1959b50d902SRodney W. Grimes err(COMPILE, "command expected"); 1969b50d902SRodney W. Grimes cmd->code = *p; 1979b50d902SRodney W. Grimes for (fp = cmd_fmts; fp->code; fp++) 1989b50d902SRodney W. Grimes if (fp->code == *p) 1999b50d902SRodney W. Grimes break; 2009b50d902SRodney W. Grimes if (!fp->code) 2019b50d902SRodney W. Grimes err(COMPILE, "invalid command code %c", *p); 2029b50d902SRodney W. Grimes if (naddr > fp->naddr) 2039b50d902SRodney W. Grimes err(COMPILE, 2049b50d902SRodney W. Grimes "command %c expects up to %d address(es), found %d", *p, fp->naddr, naddr); 2059b50d902SRodney W. Grimes switch (fp->args) { 2069b50d902SRodney W. Grimes case NONSEL: /* ! */ 2079b50d902SRodney W. Grimes p++; 208ce19262dSJordan K. Hubbard EATSPACE(); 209ce19262dSJordan K. Hubbard cmd->nonsel = ! cmd->nonsel; 2109b50d902SRodney W. Grimes goto nonsel; 2119b50d902SRodney W. Grimes case GROUP: /* { */ 2129b50d902SRodney W. Grimes p++; 2139b50d902SRodney W. Grimes EATSPACE(); 214ce19262dSJordan K. Hubbard cmd->next = stack; 215ce19262dSJordan K. Hubbard stack = cmd; 216ce19262dSJordan K. Hubbard link = &cmd->u.c; 217ce19262dSJordan K. Hubbard if (*p) 218ce19262dSJordan K. Hubbard goto semicolon; 2199b50d902SRodney W. Grimes break; 220ce19262dSJordan K. Hubbard case ENDGROUP: 221ce19262dSJordan K. Hubbard /* 222ce19262dSJordan K. Hubbard * Short-circuit command processing, since end of 223ce19262dSJordan K. Hubbard * group is really just a noop. 224ce19262dSJordan K. Hubbard */ 225ce19262dSJordan K. Hubbard cmd->nonsel = 1; 226ce19262dSJordan K. Hubbard if (stack == 0) 227ce19262dSJordan K. Hubbard err(COMPILE, "unexpected }"); 228ce19262dSJordan K. Hubbard cmd2 = stack; 229ce19262dSJordan K. Hubbard stack = cmd2->next; 230ce19262dSJordan K. Hubbard cmd2->next = cmd; 231ce19262dSJordan K. Hubbard /*FALLTHROUGH*/ 2329b50d902SRodney W. Grimes case EMPTY: /* d D g G h H l n N p P q x = \0 */ 2339b50d902SRodney W. Grimes p++; 2349b50d902SRodney W. Grimes EATSPACE(); 2359b50d902SRodney W. Grimes if (*p == ';') { 2369b50d902SRodney W. Grimes p++; 2379b50d902SRodney W. Grimes link = &cmd->next; 2389b50d902SRodney W. Grimes goto semicolon; 2399b50d902SRodney W. Grimes } 2409b50d902SRodney W. Grimes if (*p) 2419b50d902SRodney W. Grimes err(COMPILE, 2429b50d902SRodney W. Grimes "extra characters at the end of %c command", cmd->code); 2439b50d902SRodney W. Grimes break; 2449b50d902SRodney W. Grimes case TEXT: /* a c i */ 2459b50d902SRodney W. Grimes p++; 2469b50d902SRodney W. Grimes EATSPACE(); 2479b50d902SRodney W. Grimes if (*p != '\\') 2489b50d902SRodney W. Grimes err(COMPILE, 2499b50d902SRodney W. Grimes "command %c expects \\ followed by text", cmd->code); 2509b50d902SRodney W. Grimes p++; 2519b50d902SRodney W. Grimes EATSPACE(); 2529b50d902SRodney W. Grimes if (*p) 2539b50d902SRodney W. Grimes err(COMPILE, 2549b50d902SRodney W. Grimes "extra characters after \\ at the end of %c command", cmd->code); 2559b50d902SRodney W. Grimes cmd->t = compile_text(); 2569b50d902SRodney W. Grimes break; 2579b50d902SRodney W. Grimes case COMMENT: /* \0 # */ 2589b50d902SRodney W. Grimes break; 2599b50d902SRodney W. Grimes case WFILE: /* w */ 2609b50d902SRodney W. Grimes p++; 2619b50d902SRodney W. Grimes EATSPACE(); 2629b50d902SRodney W. Grimes if (*p == '\0') 2639b50d902SRodney W. Grimes err(COMPILE, "filename expected"); 2649b50d902SRodney W. Grimes cmd->t = duptoeol(p, "w command"); 2659b50d902SRodney W. Grimes if (aflag) 2669b50d902SRodney W. Grimes cmd->u.fd = -1; 2679b50d902SRodney W. Grimes else if ((cmd->u.fd = open(p, 2689b50d902SRodney W. Grimes O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 2699b50d902SRodney W. Grimes DEFFILEMODE)) == -1) 2709b50d902SRodney W. Grimes err(FATAL, "%s: %s\n", p, strerror(errno)); 2719b50d902SRodney W. Grimes break; 2729b50d902SRodney W. Grimes case RFILE: /* r */ 2739b50d902SRodney W. Grimes p++; 2749b50d902SRodney W. Grimes EATSPACE(); 2759b50d902SRodney W. Grimes if (*p == '\0') 2769b50d902SRodney W. Grimes err(COMPILE, "filename expected"); 2779b50d902SRodney W. Grimes else 2789b50d902SRodney W. Grimes cmd->t = duptoeol(p, "read command"); 2799b50d902SRodney W. Grimes break; 2809b50d902SRodney W. Grimes case BRANCH: /* b t */ 2819b50d902SRodney W. Grimes p++; 2829b50d902SRodney W. Grimes EATSPACE(); 2839b50d902SRodney W. Grimes if (*p == '\0') 2849b50d902SRodney W. Grimes cmd->t = NULL; 2859b50d902SRodney W. Grimes else 2869b50d902SRodney W. Grimes cmd->t = duptoeol(p, "branch"); 2879b50d902SRodney W. Grimes break; 2889b50d902SRodney W. Grimes case LABEL: /* : */ 2899b50d902SRodney W. Grimes p++; 2909b50d902SRodney W. Grimes EATSPACE(); 2919b50d902SRodney W. Grimes cmd->t = duptoeol(p, "label"); 2929b50d902SRodney W. Grimes if (strlen(p) == 0) 2939b50d902SRodney W. Grimes err(COMPILE, "empty label"); 2949b50d902SRodney W. Grimes enterlabel(cmd); 2959b50d902SRodney W. Grimes break; 2969b50d902SRodney W. Grimes case SUBST: /* s */ 2979b50d902SRodney W. Grimes p++; 2989b50d902SRodney W. Grimes if (*p == '\0' || *p == '\\') 2999b50d902SRodney W. Grimes err(COMPILE, 3009b50d902SRodney W. Grimes "substitute pattern can not be delimited by newline or backslash"); 3019b50d902SRodney W. Grimes cmd->u.s = xmalloc(sizeof(struct s_subst)); 3029b50d902SRodney W. Grimes p = compile_re(p, &cmd->u.s->re); 3039b50d902SRodney W. Grimes if (p == NULL) 3049b50d902SRodney W. Grimes err(COMPILE, "unterminated substitute pattern"); 3059b50d902SRodney W. Grimes --p; 3069b50d902SRodney W. Grimes p = compile_subst(p, cmd->u.s); 3079b50d902SRodney W. Grimes p = compile_flags(p, cmd->u.s); 3089b50d902SRodney W. Grimes EATSPACE(); 3099b50d902SRodney W. Grimes if (*p == ';') { 3109b50d902SRodney W. Grimes p++; 3119b50d902SRodney W. Grimes link = &cmd->next; 3129b50d902SRodney W. Grimes goto semicolon; 3139b50d902SRodney W. Grimes } 3149b50d902SRodney W. Grimes break; 3159b50d902SRodney W. Grimes case TR: /* y */ 3169b50d902SRodney W. Grimes p++; 3179b50d902SRodney W. Grimes p = compile_tr(p, (char **)&cmd->u.y); 3189b50d902SRodney W. Grimes EATSPACE(); 3199b50d902SRodney W. Grimes if (*p == ';') { 3209b50d902SRodney W. Grimes p++; 3219b50d902SRodney W. Grimes link = &cmd->next; 3229b50d902SRodney W. Grimes goto semicolon; 3239b50d902SRodney W. Grimes } 3249b50d902SRodney W. Grimes if (*p) 3259b50d902SRodney W. Grimes err(COMPILE, 3269b50d902SRodney W. Grimes "extra text at the end of a transform command"); 3279b50d902SRodney W. Grimes break; 3289b50d902SRodney W. Grimes } 3299b50d902SRodney W. Grimes } 3309b50d902SRodney W. Grimes } 3319b50d902SRodney W. Grimes 3329b50d902SRodney W. Grimes /* 3339b50d902SRodney W. Grimes * Get a delimited string. P points to the delimeter of the string; d points 3349b50d902SRodney W. Grimes * to a buffer area. Newline and delimiter escapes are processed; other 3359b50d902SRodney W. Grimes * escapes are ignored. 3369b50d902SRodney W. Grimes * 3379b50d902SRodney W. Grimes * Returns a pointer to the first character after the final delimiter or NULL 3389b50d902SRodney W. Grimes * in the case of a non-terminated string. The character array d is filled 3399b50d902SRodney W. Grimes * with the processed string. 3409b50d902SRodney W. Grimes */ 3419b50d902SRodney W. Grimes static char * 3429b50d902SRodney W. Grimes compile_delimited(p, d) 3439b50d902SRodney W. Grimes char *p, *d; 3449b50d902SRodney W. Grimes { 3459b50d902SRodney W. Grimes char c; 3469b50d902SRodney W. Grimes 3479b50d902SRodney W. Grimes c = *p++; 3489b50d902SRodney W. Grimes if (c == '\0') 3499b50d902SRodney W. Grimes return (NULL); 3509b50d902SRodney W. Grimes else if (c == '\\') 3519b50d902SRodney W. Grimes err(COMPILE, "\\ can not be used as a string delimiter"); 3529b50d902SRodney W. Grimes else if (c == '\n') 3539b50d902SRodney W. Grimes err(COMPILE, "newline can not be used as a string delimiter"); 3549b50d902SRodney W. Grimes while (*p) { 355ce19262dSJordan K. Hubbard if (*p == '[') { 356ce19262dSJordan K. Hubbard if ((d = compile_ccl(&p, d)) == NULL) 357ce19262dSJordan K. Hubbard err(COMPILE, "unbalanced brackets ([])"); 358ce19262dSJordan K. Hubbard continue; 359ce19262dSJordan K. Hubbard } else if (*p == '\\' && p[1] == '[') { 360ce19262dSJordan K. Hubbard *d++ = *p++; 361ce19262dSJordan K. Hubbard } else if (*p == '\\' && p[1] == c) 3629b50d902SRodney W. Grimes p++; 3639b50d902SRodney W. Grimes else if (*p == '\\' && p[1] == 'n') { 3649b50d902SRodney W. Grimes *d++ = '\n'; 3659b50d902SRodney W. Grimes p += 2; 3669b50d902SRodney W. Grimes continue; 3679b50d902SRodney W. Grimes } else if (*p == '\\' && p[1] == '\\') 3689b50d902SRodney W. Grimes *d++ = *p++; 3699b50d902SRodney W. Grimes else if (*p == c) { 3709b50d902SRodney W. Grimes *d = '\0'; 3719b50d902SRodney W. Grimes return (p + 1); 3729b50d902SRodney W. Grimes } 3739b50d902SRodney W. Grimes *d++ = *p++; 3749b50d902SRodney W. Grimes } 3759b50d902SRodney W. Grimes return (NULL); 3769b50d902SRodney W. Grimes } 3779b50d902SRodney W. Grimes 378ce19262dSJordan K. Hubbard 379ce19262dSJordan K. Hubbard /* compile_ccl: expand a POSIX character class */ 380ce19262dSJordan K. Hubbard static char * 381ce19262dSJordan K. Hubbard compile_ccl(sp, t) 382ce19262dSJordan K. Hubbard char **sp; 383ce19262dSJordan K. Hubbard char *t; 384ce19262dSJordan K. Hubbard { 385ce19262dSJordan K. Hubbard int c, d; 386ce19262dSJordan K. Hubbard char *s = *sp; 387ce19262dSJordan K. Hubbard 388ce19262dSJordan K. Hubbard *t++ = *s++; 389ce19262dSJordan K. Hubbard if (*s == '^') 390ce19262dSJordan K. Hubbard *t++ = *s++; 391ce19262dSJordan K. Hubbard if (*s == ']') 392ce19262dSJordan K. Hubbard *t++ = *s++; 393ce19262dSJordan K. Hubbard for (; *s && (*t = *s) != ']'; s++, t++) 394ce19262dSJordan K. Hubbard if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) { 395ce19262dSJordan K. Hubbard *++t = *++s, t++, s++; 396ce19262dSJordan K. Hubbard for (c = *s; (*t = *s) != ']' || c != d; s++, t++) 397ce19262dSJordan K. Hubbard if ((c = *s) == '\0') 398ce19262dSJordan K. Hubbard return NULL; 399ce19262dSJordan K. Hubbard } else if (*s == '\\' && s[1] == 'n') 400ce19262dSJordan K. Hubbard *t = '\n', s++; 401ce19262dSJordan K. Hubbard return (*s == ']') ? *sp = ++s, ++t : NULL; 402ce19262dSJordan K. Hubbard } 403ce19262dSJordan K. Hubbard 4049b50d902SRodney W. Grimes /* 4059b50d902SRodney W. Grimes * Get a regular expression. P points to the delimiter of the regular 4069b50d902SRodney W. Grimes * expression; repp points to the address of a regexp pointer. Newline 4079b50d902SRodney W. Grimes * and delimiter escapes are processed; other escapes are ignored. 4089b50d902SRodney W. Grimes * Returns a pointer to the first character after the final delimiter 4099b50d902SRodney W. Grimes * or NULL in the case of a non terminated regular expression. The regexp 4109b50d902SRodney W. Grimes * pointer is set to the compiled regular expression. 4119b50d902SRodney W. Grimes * Cflags are passed to regcomp. 4129b50d902SRodney W. Grimes */ 4139b50d902SRodney W. Grimes static char * 4149b50d902SRodney W. Grimes compile_re(p, repp) 4159b50d902SRodney W. Grimes char *p; 4169b50d902SRodney W. Grimes regex_t **repp; 4179b50d902SRodney W. Grimes { 4189b50d902SRodney W. Grimes int eval; 4199b50d902SRodney W. Grimes char re[_POSIX2_LINE_MAX + 1]; 4209b50d902SRodney W. Grimes 4219b50d902SRodney W. Grimes p = compile_delimited(p, re); 4229b50d902SRodney W. Grimes if (p && strlen(re) == 0) { 4239b50d902SRodney W. Grimes *repp = NULL; 4249b50d902SRodney W. Grimes return (p); 4259b50d902SRodney W. Grimes } 4269b50d902SRodney W. Grimes *repp = xmalloc(sizeof(regex_t)); 4279b50d902SRodney W. Grimes if (p && (eval = regcomp(*repp, re, 0)) != 0) 4289b50d902SRodney W. Grimes err(COMPILE, "RE error: %s", strregerror(eval, *repp)); 4299b50d902SRodney W. Grimes if (maxnsub < (*repp)->re_nsub) 4309b50d902SRodney W. Grimes maxnsub = (*repp)->re_nsub; 4319b50d902SRodney W. Grimes return (p); 4329b50d902SRodney W. Grimes } 4339b50d902SRodney W. Grimes 4349b50d902SRodney W. Grimes /* 4359b50d902SRodney W. Grimes * Compile the substitution string of a regular expression and set res to 4369b50d902SRodney W. Grimes * point to a saved copy of it. Nsub is the number of parenthesized regular 4379b50d902SRodney W. Grimes * expressions. 4389b50d902SRodney W. Grimes */ 4399b50d902SRodney W. Grimes static char * 4409b50d902SRodney W. Grimes compile_subst(p, s) 4419b50d902SRodney W. Grimes char *p; 4429b50d902SRodney W. Grimes struct s_subst *s; 4439b50d902SRodney W. Grimes { 4449b50d902SRodney W. Grimes static char lbuf[_POSIX2_LINE_MAX + 1]; 4459b50d902SRodney W. Grimes int asize, ref, size; 4469b50d902SRodney W. Grimes char c, *text, *op, *sp; 4479b50d902SRodney W. Grimes 4489b50d902SRodney W. Grimes c = *p++; /* Terminator character */ 4499b50d902SRodney W. Grimes if (c == '\0') 4509b50d902SRodney W. Grimes return (NULL); 4519b50d902SRodney W. Grimes 4529b50d902SRodney W. Grimes s->maxbref = 0; 4539b50d902SRodney W. Grimes s->linenum = linenum; 4549b50d902SRodney W. Grimes asize = 2 * _POSIX2_LINE_MAX + 1; 4559b50d902SRodney W. Grimes text = xmalloc(asize); 4569b50d902SRodney W. Grimes size = 0; 4579b50d902SRodney W. Grimes do { 4589b50d902SRodney W. Grimes op = sp = text + size; 4599b50d902SRodney W. Grimes for (; *p; p++) { 4609b50d902SRodney W. Grimes if (*p == '\\') { 4619b50d902SRodney W. Grimes p++; 4629b50d902SRodney W. Grimes if (strchr("123456789", *p) != NULL) { 4639b50d902SRodney W. Grimes *sp++ = '\\'; 4649b50d902SRodney W. Grimes ref = *p - '0'; 4659b50d902SRodney W. Grimes if (s->re != NULL && 4669b50d902SRodney W. Grimes ref > s->re->re_nsub) 4679b50d902SRodney W. Grimes err(COMPILE, 4689b50d902SRodney W. Grimes "\\%c not defined in the RE", *p); 4699b50d902SRodney W. Grimes if (s->maxbref < ref) 4709b50d902SRodney W. Grimes s->maxbref = ref; 4719b50d902SRodney W. Grimes } else if (*p == '&' || *p == '\\') 4729b50d902SRodney W. Grimes *sp++ = '\\'; 4739b50d902SRodney W. Grimes } else if (*p == c) { 4749b50d902SRodney W. Grimes p++; 4759b50d902SRodney W. Grimes *sp++ = '\0'; 4769b50d902SRodney W. Grimes size += sp - op; 4779b50d902SRodney W. Grimes s->new = xrealloc(text, size); 4789b50d902SRodney W. Grimes return (p); 4799b50d902SRodney W. Grimes } else if (*p == '\n') { 4809b50d902SRodney W. Grimes err(COMPILE, 4819b50d902SRodney W. Grimes "unescaped newline inside substitute pattern"); 4829b50d902SRodney W. Grimes /* NOTREACHED */ 4839b50d902SRodney W. Grimes } 4849b50d902SRodney W. Grimes *sp++ = *p; 4859b50d902SRodney W. Grimes } 4869b50d902SRodney W. Grimes size += sp - op; 4879b50d902SRodney W. Grimes if (asize - size < _POSIX2_LINE_MAX + 1) { 4889b50d902SRodney W. Grimes asize *= 2; 4899b50d902SRodney W. Grimes text = xmalloc(asize); 4909b50d902SRodney W. Grimes } 4919b50d902SRodney W. Grimes } while (cu_fgets(p = lbuf, sizeof(lbuf))); 4929b50d902SRodney W. Grimes err(COMPILE, "unterminated substitute in regular expression"); 4939b50d902SRodney W. Grimes /* NOTREACHED */ 4949b50d902SRodney W. Grimes } 4959b50d902SRodney W. Grimes 4969b50d902SRodney W. Grimes /* 4979b50d902SRodney W. Grimes * Compile the flags of the s command 4989b50d902SRodney W. Grimes */ 4999b50d902SRodney W. Grimes static char * 5009b50d902SRodney W. Grimes compile_flags(p, s) 5019b50d902SRodney W. Grimes char *p; 5029b50d902SRodney W. Grimes struct s_subst *s; 5039b50d902SRodney W. Grimes { 5049b50d902SRodney W. Grimes int gn; /* True if we have seen g or n */ 5059b50d902SRodney W. Grimes char wfile[_POSIX2_LINE_MAX + 1], *q; 5069b50d902SRodney W. Grimes 5079b50d902SRodney W. Grimes s->n = 1; /* Default */ 5089b50d902SRodney W. Grimes s->p = 0; 5099b50d902SRodney W. Grimes s->wfile = NULL; 5109b50d902SRodney W. Grimes s->wfd = -1; 5119b50d902SRodney W. Grimes for (gn = 0;;) { 5129b50d902SRodney W. Grimes EATSPACE(); /* EXTENSION */ 5139b50d902SRodney W. Grimes switch (*p) { 5149b50d902SRodney W. Grimes case 'g': 5159b50d902SRodney W. Grimes if (gn) 5169b50d902SRodney W. Grimes err(COMPILE, 5179b50d902SRodney W. Grimes "more than one number or 'g' in substitute flags"); 5189b50d902SRodney W. Grimes gn = 1; 5199b50d902SRodney W. Grimes s->n = 0; 5209b50d902SRodney W. Grimes break; 5219b50d902SRodney W. Grimes case '\0': 5229b50d902SRodney W. Grimes case '\n': 5239b50d902SRodney W. Grimes case ';': 5249b50d902SRodney W. Grimes return (p); 5259b50d902SRodney W. Grimes case 'p': 5269b50d902SRodney W. Grimes s->p = 1; 5279b50d902SRodney W. Grimes break; 5289b50d902SRodney W. Grimes case '1': case '2': case '3': 5299b50d902SRodney W. Grimes case '4': case '5': case '6': 5309b50d902SRodney W. Grimes case '7': case '8': case '9': 5319b50d902SRodney W. Grimes if (gn) 5329b50d902SRodney W. Grimes err(COMPILE, 5339b50d902SRodney W. Grimes "more than one number or 'g' in substitute flags"); 5349b50d902SRodney W. Grimes gn = 1; 5359b50d902SRodney W. Grimes /* XXX Check for overflow */ 5369b50d902SRodney W. Grimes s->n = (int)strtol(p, &p, 10); 5379b50d902SRodney W. Grimes break; 5389b50d902SRodney W. Grimes case 'w': 5399b50d902SRodney W. Grimes p++; 5409b50d902SRodney W. Grimes #ifdef HISTORIC_PRACTICE 5419b50d902SRodney W. Grimes if (*p != ' ') { 5429b50d902SRodney W. Grimes err(WARNING, "space missing before w wfile"); 5439b50d902SRodney W. Grimes return (p); 5449b50d902SRodney W. Grimes } 5459b50d902SRodney W. Grimes #endif 5469b50d902SRodney W. Grimes EATSPACE(); 5479b50d902SRodney W. Grimes q = wfile; 5489b50d902SRodney W. Grimes while (*p) { 5499b50d902SRodney W. Grimes if (*p == '\n') 5509b50d902SRodney W. Grimes break; 5519b50d902SRodney W. Grimes *q++ = *p++; 5529b50d902SRodney W. Grimes } 5539b50d902SRodney W. Grimes *q = '\0'; 5549b50d902SRodney W. Grimes if (q == wfile) 5559b50d902SRodney W. Grimes err(COMPILE, "no wfile specified"); 5569b50d902SRodney W. Grimes s->wfile = strdup(wfile); 5579b50d902SRodney W. Grimes if (!aflag && (s->wfd = open(wfile, 5589b50d902SRodney W. Grimes O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 5599b50d902SRodney W. Grimes DEFFILEMODE)) == -1) 5609b50d902SRodney W. Grimes err(FATAL, "%s: %s\n", wfile, strerror(errno)); 5619b50d902SRodney W. Grimes return (p); 5629b50d902SRodney W. Grimes default: 5639b50d902SRodney W. Grimes err(COMPILE, 5649b50d902SRodney W. Grimes "bad flag in substitute command: '%c'", *p); 5659b50d902SRodney W. Grimes break; 5669b50d902SRodney W. Grimes } 5679b50d902SRodney W. Grimes p++; 5689b50d902SRodney W. Grimes } 5699b50d902SRodney W. Grimes } 5709b50d902SRodney W. Grimes 5719b50d902SRodney W. Grimes /* 5729b50d902SRodney W. Grimes * Compile a translation set of strings into a lookup table. 5739b50d902SRodney W. Grimes */ 5749b50d902SRodney W. Grimes static char * 5759b50d902SRodney W. Grimes compile_tr(p, transtab) 5769b50d902SRodney W. Grimes char *p; 5779b50d902SRodney W. Grimes char **transtab; 5789b50d902SRodney W. Grimes { 5799b50d902SRodney W. Grimes int i; 5809b50d902SRodney W. Grimes char *lt, *op, *np; 5819b50d902SRodney W. Grimes char old[_POSIX2_LINE_MAX + 1]; 5829b50d902SRodney W. Grimes char new[_POSIX2_LINE_MAX + 1]; 5839b50d902SRodney W. Grimes 5849b50d902SRodney W. Grimes if (*p == '\0' || *p == '\\') 5859b50d902SRodney W. Grimes err(COMPILE, 5869b50d902SRodney W. Grimes "transform pattern can not be delimited by newline or backslash"); 5879b50d902SRodney W. Grimes p = compile_delimited(p, old); 5889b50d902SRodney W. Grimes if (p == NULL) { 5899b50d902SRodney W. Grimes err(COMPILE, "unterminated transform source string"); 5909b50d902SRodney W. Grimes return (NULL); 5919b50d902SRodney W. Grimes } 5929b50d902SRodney W. Grimes p = compile_delimited(--p, new); 5939b50d902SRodney W. Grimes if (p == NULL) { 5949b50d902SRodney W. Grimes err(COMPILE, "unterminated transform target string"); 5959b50d902SRodney W. Grimes return (NULL); 5969b50d902SRodney W. Grimes } 5979b50d902SRodney W. Grimes EATSPACE(); 5989b50d902SRodney W. Grimes if (strlen(new) != strlen(old)) { 5999b50d902SRodney W. Grimes err(COMPILE, "transform strings are not the same length"); 6009b50d902SRodney W. Grimes return (NULL); 6019b50d902SRodney W. Grimes } 6029b50d902SRodney W. Grimes /* We assume characters are 8 bits */ 6039b50d902SRodney W. Grimes lt = xmalloc(UCHAR_MAX); 6049b50d902SRodney W. Grimes for (i = 0; i <= UCHAR_MAX; i++) 6059b50d902SRodney W. Grimes lt[i] = (char)i; 6069b50d902SRodney W. Grimes for (op = old, np = new; *op; op++, np++) 6079b50d902SRodney W. Grimes lt[(u_char)*op] = *np; 6089b50d902SRodney W. Grimes *transtab = lt; 6099b50d902SRodney W. Grimes return (p); 6109b50d902SRodney W. Grimes } 6119b50d902SRodney W. Grimes 6129b50d902SRodney W. Grimes /* 6139b50d902SRodney W. Grimes * Compile the text following an a or i command. 6149b50d902SRodney W. Grimes */ 6159b50d902SRodney W. Grimes static char * 6169b50d902SRodney W. Grimes compile_text() 6179b50d902SRodney W. Grimes { 61849e65599SBruce Evans int asize, esc_nl, size; 6199b50d902SRodney W. Grimes char *text, *p, *op, *s; 6209b50d902SRodney W. Grimes char lbuf[_POSIX2_LINE_MAX + 1]; 6219b50d902SRodney W. Grimes 6229b50d902SRodney W. Grimes asize = 2 * _POSIX2_LINE_MAX + 1; 6239b50d902SRodney W. Grimes text = xmalloc(asize); 6249b50d902SRodney W. Grimes size = 0; 6259b50d902SRodney W. Grimes while (cu_fgets(lbuf, sizeof(lbuf))) { 6269b50d902SRodney W. Grimes op = s = text + size; 6279b50d902SRodney W. Grimes p = lbuf; 6289b50d902SRodney W. Grimes EATSPACE(); 62949e65599SBruce Evans for (esc_nl = 0; *p != '\0'; p++) { 63049e65599SBruce Evans if (*p == '\\' && p[1] != '\0' && *++p == '\n') 63149e65599SBruce Evans esc_nl = 1; 6329b50d902SRodney W. Grimes *s++ = *p; 6339b50d902SRodney W. Grimes } 6349b50d902SRodney W. Grimes size += s - op; 63549e65599SBruce Evans if (!esc_nl) { 6369b50d902SRodney W. Grimes *s = '\0'; 6379b50d902SRodney W. Grimes break; 6389b50d902SRodney W. Grimes } 6399b50d902SRodney W. Grimes if (asize - size < _POSIX2_LINE_MAX + 1) { 6409b50d902SRodney W. Grimes asize *= 2; 6419b50d902SRodney W. Grimes text = xmalloc(asize); 6429b50d902SRodney W. Grimes } 6439b50d902SRodney W. Grimes } 6449b50d902SRodney W. Grimes return (xrealloc(text, size + 1)); 6459b50d902SRodney W. Grimes } 6469b50d902SRodney W. Grimes 6479b50d902SRodney W. Grimes /* 6489b50d902SRodney W. Grimes * Get an address and return a pointer to the first character after 6499b50d902SRodney W. Grimes * it. Fill the structure pointed to according to the address. 6509b50d902SRodney W. Grimes */ 6519b50d902SRodney W. Grimes static char * 6529b50d902SRodney W. Grimes compile_addr(p, a) 6539b50d902SRodney W. Grimes char *p; 6549b50d902SRodney W. Grimes struct s_addr *a; 6559b50d902SRodney W. Grimes { 6569b50d902SRodney W. Grimes char *end; 6579b50d902SRodney W. Grimes 6589b50d902SRodney W. Grimes switch (*p) { 6599b50d902SRodney W. Grimes case '\\': /* Context address */ 6609b50d902SRodney W. Grimes ++p; 6619b50d902SRodney W. Grimes /* FALLTHROUGH */ 6629b50d902SRodney W. Grimes case '/': /* Context address */ 6639b50d902SRodney W. Grimes p = compile_re(p, &a->u.r); 6649b50d902SRodney W. Grimes if (p == NULL) 6659b50d902SRodney W. Grimes err(COMPILE, "unterminated regular expression"); 6669b50d902SRodney W. Grimes a->type = AT_RE; 6679b50d902SRodney W. Grimes return (p); 6689b50d902SRodney W. Grimes 6699b50d902SRodney W. Grimes case '$': /* Last line */ 6709b50d902SRodney W. Grimes a->type = AT_LAST; 6719b50d902SRodney W. Grimes return (p + 1); 6729b50d902SRodney W. Grimes /* Line number */ 6739b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': case '4': 6749b50d902SRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 6759b50d902SRodney W. Grimes a->type = AT_LINE; 6769b50d902SRodney W. Grimes a->u.l = strtol(p, &end, 10); 6779b50d902SRodney W. Grimes return (end); 6789b50d902SRodney W. Grimes default: 6799b50d902SRodney W. Grimes err(COMPILE, "expected context address"); 6809b50d902SRodney W. Grimes return (NULL); 6819b50d902SRodney W. Grimes } 6829b50d902SRodney W. Grimes } 6839b50d902SRodney W. Grimes 6849b50d902SRodney W. Grimes /* 6859b50d902SRodney W. Grimes * duptoeol -- 6869b50d902SRodney W. Grimes * Return a copy of all the characters up to \n or \0. 6879b50d902SRodney W. Grimes */ 6889b50d902SRodney W. Grimes static char * 6899b50d902SRodney W. Grimes duptoeol(s, ctype) 6909b50d902SRodney W. Grimes register char *s; 6919b50d902SRodney W. Grimes char *ctype; 6929b50d902SRodney W. Grimes { 6939b50d902SRodney W. Grimes size_t len; 6949b50d902SRodney W. Grimes int ws; 6959b50d902SRodney W. Grimes char *start; 6969b50d902SRodney W. Grimes 6979b50d902SRodney W. Grimes ws = 0; 6989b50d902SRodney W. Grimes for (start = s; *s != '\0' && *s != '\n'; ++s) 699726aebe5SAndrey A. Chernov ws = isspace((unsigned char)*s); 7009b50d902SRodney W. Grimes *s = '\0'; 7019b50d902SRodney W. Grimes if (ws) 7029b50d902SRodney W. Grimes err(WARNING, "whitespace after %s", ctype); 7039b50d902SRodney W. Grimes len = s - start + 1; 7049b50d902SRodney W. Grimes return (memmove(xmalloc(len), start, len)); 7059b50d902SRodney W. Grimes } 7069b50d902SRodney W. Grimes 7079b50d902SRodney W. Grimes /* 7089b50d902SRodney W. Grimes * Convert goto label names to addresses, and count a and r commands, in 7099b50d902SRodney W. Grimes * the given subset of the script. Free the memory used by labels in b 7109b50d902SRodney W. Grimes * and t commands (but not by :). 7119b50d902SRodney W. Grimes * 7129b50d902SRodney W. Grimes * TODO: Remove } nodes 7139b50d902SRodney W. Grimes */ 7149b50d902SRodney W. Grimes static void 7159b50d902SRodney W. Grimes fixuplabel(cp, end) 7169b50d902SRodney W. Grimes struct s_command *cp, *end; 7179b50d902SRodney W. Grimes { 7189b50d902SRodney W. Grimes 7199b50d902SRodney W. Grimes for (; cp != end; cp = cp->next) 7209b50d902SRodney W. Grimes switch (cp->code) { 7219b50d902SRodney W. Grimes case 'a': 7229b50d902SRodney W. Grimes case 'r': 7239b50d902SRodney W. Grimes appendnum++; 7249b50d902SRodney W. Grimes break; 7259b50d902SRodney W. Grimes case 'b': 7269b50d902SRodney W. Grimes case 't': 7279b50d902SRodney W. Grimes /* Resolve branch target. */ 7289b50d902SRodney W. Grimes if (cp->t == NULL) { 7299b50d902SRodney W. Grimes cp->u.c = NULL; 7309b50d902SRodney W. Grimes break; 7319b50d902SRodney W. Grimes } 7329b50d902SRodney W. Grimes if ((cp->u.c = findlabel(cp->t)) == NULL) 7339b50d902SRodney W. Grimes err(COMPILE2, "undefined label '%s'", cp->t); 7349b50d902SRodney W. Grimes free(cp->t); 7359b50d902SRodney W. Grimes break; 7369b50d902SRodney W. Grimes case '{': 7379b50d902SRodney W. Grimes /* Do interior commands. */ 7389b50d902SRodney W. Grimes fixuplabel(cp->u.c, cp->next); 7399b50d902SRodney W. Grimes break; 7409b50d902SRodney W. Grimes } 7419b50d902SRodney W. Grimes } 7429b50d902SRodney W. Grimes 7439b50d902SRodney W. Grimes /* 7449b50d902SRodney W. Grimes * Associate the given command label for later lookup. 7459b50d902SRodney W. Grimes */ 7469b50d902SRodney W. Grimes static void 7479b50d902SRodney W. Grimes enterlabel(cp) 7489b50d902SRodney W. Grimes struct s_command *cp; 7499b50d902SRodney W. Grimes { 7509b50d902SRodney W. Grimes register struct labhash **lhp, *lh; 7519b50d902SRodney W. Grimes register u_char *p; 7529b50d902SRodney W. Grimes register u_int h, c; 7539b50d902SRodney W. Grimes 7549b50d902SRodney W. Grimes for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++) 7559b50d902SRodney W. Grimes h = (h << 5) + h + c; 7569b50d902SRodney W. Grimes lhp = &labels[h & LHMASK]; 7579b50d902SRodney W. Grimes for (lh = *lhp; lh != NULL; lh = lh->lh_next) 7589b50d902SRodney W. Grimes if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0) 7599b50d902SRodney W. Grimes err(COMPILE2, "duplicate label '%s'", cp->t); 7609b50d902SRodney W. Grimes lh = xmalloc(sizeof *lh); 7619b50d902SRodney W. Grimes lh->lh_next = *lhp; 7629b50d902SRodney W. Grimes lh->lh_hash = h; 7639b50d902SRodney W. Grimes lh->lh_cmd = cp; 7649b50d902SRodney W. Grimes lh->lh_ref = 0; 7659b50d902SRodney W. Grimes *lhp = lh; 7669b50d902SRodney W. Grimes } 7679b50d902SRodney W. Grimes 7689b50d902SRodney W. Grimes /* 7699b50d902SRodney W. Grimes * Find the label contained in the command l in the command linked 7709b50d902SRodney W. Grimes * list cp. L is excluded from the search. Return NULL if not found. 7719b50d902SRodney W. Grimes */ 7729b50d902SRodney W. Grimes static struct s_command * 7739b50d902SRodney W. Grimes findlabel(name) 7749b50d902SRodney W. Grimes char *name; 7759b50d902SRodney W. Grimes { 7769b50d902SRodney W. Grimes register struct labhash *lh; 7779b50d902SRodney W. Grimes register u_char *p; 7789b50d902SRodney W. Grimes register u_int h, c; 7799b50d902SRodney W. Grimes 7809b50d902SRodney W. Grimes for (h = 0, p = (u_char *)name; (c = *p) != 0; p++) 7819b50d902SRodney W. Grimes h = (h << 5) + h + c; 7829b50d902SRodney W. Grimes for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) { 7839b50d902SRodney W. Grimes if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) { 7849b50d902SRodney W. Grimes lh->lh_ref = 1; 7859b50d902SRodney W. Grimes return (lh->lh_cmd); 7869b50d902SRodney W. Grimes } 7879b50d902SRodney W. Grimes } 7889b50d902SRodney W. Grimes return (NULL); 7899b50d902SRodney W. Grimes } 7909b50d902SRodney W. Grimes 7919b50d902SRodney W. Grimes /* 7929b50d902SRodney W. Grimes * Warn about any unused labels. As a side effect, release the label hash 7939b50d902SRodney W. Grimes * table space. 7949b50d902SRodney W. Grimes */ 7959b50d902SRodney W. Grimes static void 7969b50d902SRodney W. Grimes uselabel() 7979b50d902SRodney W. Grimes { 7989b50d902SRodney W. Grimes register struct labhash *lh, *next; 7999b50d902SRodney W. Grimes register int i; 8009b50d902SRodney W. Grimes 8019b50d902SRodney W. Grimes for (i = 0; i < LHSZ; i++) { 8029b50d902SRodney W. Grimes for (lh = labels[i]; lh != NULL; lh = next) { 8039b50d902SRodney W. Grimes next = lh->lh_next; 8049b50d902SRodney W. Grimes if (!lh->lh_ref) 8059b50d902SRodney W. Grimes err(WARNING, "unused label '%s'", 8069b50d902SRodney W. Grimes lh->lh_cmd->t); 8079b50d902SRodney W. Grimes free(lh); 8089b50d902SRodney W. Grimes } 8099b50d902SRodney W. Grimes } 8109b50d902SRodney W. Grimes } 811