155992Sbostic /*- 255992Sbostic * Copyright (c) 1992 Diomidis Spinellis. 355992Sbostic * Copyright (c) 1992 The Regents of the University of California. 455992Sbostic * All rights reserved. 555992Sbostic * 655992Sbostic * This code is derived from software contributed to Berkeley by 755992Sbostic * Diomidis Spinellis of Imperial College, University of London. 855992Sbostic * 955992Sbostic * %sccs.include.redist.c% 1055992Sbostic */ 1155992Sbostic 1255992Sbostic #ifndef lint 13*56077Sbostic static char sccsid[] = "@(#)compile.c 5.4 (Berkeley) 08/28/92"; 1455992Sbostic #endif /* not lint */ 1555992Sbostic 1655992Sbostic #include <sys/types.h> 1756019Sbostic #include <sys/stat.h> 1855992Sbostic 1955992Sbostic #include <ctype.h> 2055992Sbostic #include <errno.h> 2155992Sbostic #include <fcntl.h> 2255992Sbostic #include <limits.h> 2355992Sbostic #include <regex.h> 2455992Sbostic #include <stdio.h> 2555992Sbostic #include <stdlib.h> 2655992Sbostic #include <string.h> 2755992Sbostic 2855992Sbostic #include "defs.h" 2955992Sbostic #include "extern.h" 3055992Sbostic 3155992Sbostic static char *compile_addr __P((char *, struct s_addr *)); 3255992Sbostic static char *compile_delimited __P((char *, char *)); 3355992Sbostic static char *compile_flags __P((char *, struct s_subst *)); 34*56077Sbostic static char *compile_re __P((char *, regex_t **)); 3556019Sbostic static char *compile_subst __P((char *, struct s_subst *)); 3655992Sbostic static char *compile_text __P((void)); 3755992Sbostic static char *compile_tr __P((char *, char **)); 3855992Sbostic static struct s_command 3955992Sbostic **compile_stream __P((char *, struct s_command **, char *)); 4055992Sbostic static char *duptoeol __P((char *)); 4155992Sbostic static struct s_command 4255992Sbostic *findlabel __P((struct s_command *, struct s_command *)); 4355992Sbostic static void fixuplabel __P((struct s_command *, struct s_command *)); 4455992Sbostic 4555992Sbostic /* 4655992Sbostic * Command specification. This is used to drive the command parser. 4755992Sbostic */ 4855992Sbostic struct s_format { 4955992Sbostic char code; /* Command code */ 5055992Sbostic int naddr; /* Number of address args */ 5155992Sbostic enum e_args args; /* Argument type */ 5255992Sbostic }; 5355992Sbostic 5455992Sbostic static struct s_format cmd_fmts[] = { 5555992Sbostic {'{', 2, GROUP}, 5655992Sbostic {'a', 1, TEXT}, 5755992Sbostic {'b', 2, BRANCH}, 5855992Sbostic {'c', 2, TEXT}, 5955992Sbostic {'d', 2, EMPTY}, 6055992Sbostic {'D', 2, EMPTY}, 6155992Sbostic {'g', 2, EMPTY}, 6255992Sbostic {'G', 2, EMPTY}, 6355992Sbostic {'h', 2, EMPTY}, 6455992Sbostic {'H', 2, EMPTY}, 6555992Sbostic {'i', 1, TEXT}, 6655992Sbostic {'l', 2, EMPTY}, 6755992Sbostic {'n', 2, EMPTY}, 6855992Sbostic {'N', 2, EMPTY}, 6955992Sbostic {'p', 2, EMPTY}, 7055992Sbostic {'P', 2, EMPTY}, 7155992Sbostic {'q', 1, EMPTY}, 7255992Sbostic {'r', 1, RFILE}, 7355992Sbostic {'s', 2, SUBST}, 7455992Sbostic {'t', 2, BRANCH}, 7555992Sbostic {'w', 2, WFILE}, 7655992Sbostic {'x', 2, EMPTY}, 7755992Sbostic {'y', 2, TR}, 7855992Sbostic {'!', 2, NONSEL}, 7955992Sbostic {':', 0, LABEL}, 8055992Sbostic {'#', 0, COMMENT}, 8155992Sbostic {'=', 1, EMPTY}, 8255992Sbostic {'\0', 0, COMMENT}, 8355992Sbostic }; 8455992Sbostic 8556019Sbostic /* The compiled program. */ 8655992Sbostic struct s_command *prog; 8755992Sbostic 8855992Sbostic /* 8955992Sbostic * Compile the program into prog. 9056019Sbostic * Initialise appends. 9155992Sbostic */ 9255992Sbostic void 9355992Sbostic compile() 9455992Sbostic { 9555992Sbostic *compile_stream(NULL, &prog, NULL) = NULL; 9655992Sbostic fixuplabel(prog, prog); 9755992Sbostic appends = xmalloc(sizeof(struct s_appends) * appendnum); 98*56077Sbostic match = xmalloc((maxnsub + 1) * sizeof(regmatch_t)); 9955992Sbostic } 10055992Sbostic 10155992Sbostic #define EATSPACE() do { \ 10255992Sbostic if (p) \ 10355992Sbostic while (*p && isascii(*p) && isspace(*p)) \ 10455992Sbostic p++; \ 10555992Sbostic } while (0) 10655992Sbostic 10755992Sbostic static struct s_command ** 10855992Sbostic compile_stream(terminator, link, p) 10955992Sbostic char *terminator; 11055992Sbostic struct s_command **link; 11155992Sbostic register char *p; 11255992Sbostic { 11355992Sbostic static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ 11455992Sbostic struct s_command *cmd, *cmd2; 11555992Sbostic struct s_format *fp; 11655992Sbostic int naddr; /* Number of addresses */ 11755992Sbostic 11855992Sbostic if (p != NULL) 11955992Sbostic goto semicolon; 12055992Sbostic for (;;) { 12155992Sbostic if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) { 12255992Sbostic if (terminator != NULL) 12355992Sbostic err(COMPILE, "unexpected EOF (pending }'s)"); 12455992Sbostic return (link); 12555992Sbostic } 12655992Sbostic 12755992Sbostic semicolon: EATSPACE(); 12855992Sbostic if (p && (*p == '#' || *p == '\0')) 12955992Sbostic continue; 13055992Sbostic if (*p == '}') { 13155992Sbostic if (terminator == NULL) 13255992Sbostic err(COMPILE, "unexpected }"); 13355992Sbostic return (link); 13455992Sbostic } 13555992Sbostic *link = cmd = xmalloc(sizeof(struct s_command)); 13655992Sbostic link = &cmd->next; 13755992Sbostic cmd->nonsel = cmd->inrange = 0; 13855992Sbostic /* First parse the addresses */ 13955992Sbostic naddr = 0; 14055992Sbostic cmd->a1 = cmd->a2 = NULL; 14155992Sbostic 14255992Sbostic /* Valid characters to start an address */ 14355992Sbostic #define addrchar(c) (strchr("0123456789/\\$", (c))) 14455992Sbostic if (addrchar(*p)) { 14555992Sbostic naddr++; 14655992Sbostic cmd->a1 = xmalloc(sizeof(struct s_addr)); 14755992Sbostic p = compile_addr(p, cmd->a1); 14855992Sbostic EATSPACE(); /* EXTENSION */ 14955992Sbostic if (*p == ',') { 15055992Sbostic naddr++; 15155992Sbostic p++; 15255992Sbostic EATSPACE(); /* EXTENSION */ 15355992Sbostic cmd->a2 = xmalloc(sizeof(struct s_addr)); 15455992Sbostic p = compile_addr(p, cmd->a2); 15555992Sbostic } 15655992Sbostic } 15755992Sbostic 15855992Sbostic nonsel: /* Now parse the command */ 15955992Sbostic EATSPACE(); 16055992Sbostic if (!*p) 16155992Sbostic err(COMPILE, "command expected"); 16255992Sbostic cmd->code = *p; 16355992Sbostic for (fp = cmd_fmts; fp->code; fp++) 16455992Sbostic if (fp->code == *p) 16555992Sbostic break; 16655992Sbostic if (!fp->code) 16755992Sbostic err(COMPILE, "invalid command code %c", *p); 16855992Sbostic if (naddr > fp->naddr) 16955992Sbostic err(COMPILE, 17055992Sbostic "command %c expects up to %d address(es), found %d", *p, fp->naddr, naddr); 17155992Sbostic switch (fp->args) { 17255992Sbostic case NONSEL: /* ! */ 17355992Sbostic cmd->nonsel = ! cmd->nonsel; 17455992Sbostic p++; 17555992Sbostic goto nonsel; 17655992Sbostic case GROUP: /* { */ 17755992Sbostic p++; 17855992Sbostic EATSPACE(); 17955992Sbostic if (!*p) 18055992Sbostic p = NULL; 18155992Sbostic cmd2 = xmalloc(sizeof(struct s_command)); 18255992Sbostic cmd2->code = '}'; 18355992Sbostic *compile_stream("}", &cmd->u.c, p) = cmd2; 18455992Sbostic cmd->next = cmd2; 18555992Sbostic link = &cmd2->next; 18655992Sbostic break; 18755992Sbostic case EMPTY: /* d D g G h H l n N p P q x = \0 */ 18855992Sbostic p++; 18955992Sbostic EATSPACE(); 19055992Sbostic if (*p == ';') { 19155992Sbostic p++; 19255992Sbostic link = &cmd->next; 19355992Sbostic goto semicolon; 19455992Sbostic } 19555992Sbostic if (*p) 19655992Sbostic err(COMPILE, 19755992Sbostic "extra characters at the end of %c command", cmd->code); 19855992Sbostic break; 19955992Sbostic case TEXT: /* a c i */ 20055992Sbostic p++; 20155992Sbostic EATSPACE(); 20255992Sbostic if (*p != '\\') 20355992Sbostic err(COMPILE, 20455992Sbostic "command %c expects \\ followed by text", cmd->code); 20555992Sbostic p++; 20655992Sbostic EATSPACE(); 20755992Sbostic if (*p) 20855992Sbostic err(COMPILE, 20955992Sbostic "extra characters after \\ at the end of %c command", cmd->code); 21055992Sbostic cmd->t = compile_text(); 21155992Sbostic break; 21255992Sbostic case COMMENT: /* \0 # */ 21355992Sbostic break; 21455992Sbostic case WFILE: /* w */ 21555992Sbostic p++; 21655992Sbostic EATSPACE(); 21755992Sbostic if (*p == '\0') 21855992Sbostic err(COMPILE, "filename expected"); 21955992Sbostic cmd->t = duptoeol(p); 22055992Sbostic if (aflag) 22155992Sbostic cmd->u.fd = -1; 22255992Sbostic else if ((cmd->u.fd = open(p, 22355992Sbostic O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 22455992Sbostic DEFFILEMODE)) == -1) 22555992Sbostic err(FATAL, "%s: %s\n", p, strerror(errno)); 22655992Sbostic break; 22755992Sbostic case RFILE: /* r */ 22855992Sbostic p++; 22955992Sbostic EATSPACE(); 23055992Sbostic if (*p == '\0') 23155992Sbostic err(COMPILE, "filename expected"); 23255992Sbostic else 23355992Sbostic cmd->t = duptoeol(p); 23455992Sbostic break; 23555992Sbostic case BRANCH: /* b t */ 23655992Sbostic p++; 23755992Sbostic EATSPACE(); 23855992Sbostic if (*p == '\0') 23955992Sbostic cmd->t = NULL; 24055992Sbostic else 24155992Sbostic cmd->t = duptoeol(p); 24255992Sbostic break; 24355992Sbostic case LABEL: /* : */ 24455992Sbostic p++; 24555992Sbostic EATSPACE(); 24655992Sbostic cmd->t = duptoeol(p); 24755992Sbostic if (strlen(p) == 0) 24855992Sbostic err(COMPILE, "empty label"); 24955992Sbostic break; 25055992Sbostic case SUBST: /* s */ 25155992Sbostic p++; 25255992Sbostic if (*p == '\0' || *p == '\\') 25355992Sbostic err(COMPILE, 25455992Sbostic "substitute pattern can not be delimited by newline or backslash"); 25555992Sbostic cmd->u.s = xmalloc(sizeof(struct s_subst)); 256*56077Sbostic p = compile_re(p, &cmd->u.s->re); 25755992Sbostic if (p == NULL) 25856004Sbostic err(COMPILE, "unterminated substitute pattern"); 25956019Sbostic --p; 26056019Sbostic p = compile_subst(p, cmd->u.s); 26155992Sbostic p = compile_flags(p, cmd->u.s); 26255992Sbostic EATSPACE(); 26355992Sbostic if (*p == ';') { 26455992Sbostic p++; 26555992Sbostic link = &cmd->next; 26655992Sbostic goto semicolon; 26755992Sbostic } 26855992Sbostic break; 26955992Sbostic case TR: /* y */ 27055992Sbostic p++; 27155992Sbostic p = compile_tr(p, (char **)&cmd->u.y); 27255992Sbostic EATSPACE(); 27355992Sbostic if (*p == ';') { 27455992Sbostic p++; 27555992Sbostic link = &cmd->next; 27655992Sbostic goto semicolon; 27755992Sbostic } 27855992Sbostic if (*p) 27955992Sbostic err(COMPILE, 28055992Sbostic "extra text at the end of a transform command"); 28155992Sbostic break; 28255992Sbostic } 28355992Sbostic } 28455992Sbostic } 28555992Sbostic 28655992Sbostic /* 28755992Sbostic * Get a delimited string. P points to the delimeter of the string; d points 28855992Sbostic * to a buffer area. Newline and delimiter escapes are processed; other 28955992Sbostic * escapes are ignored. 29055992Sbostic * 29155992Sbostic * Returns a pointer to the first character after the final delimiter or NULL 29255992Sbostic * in the case of a non-terminated string. The character array d is filled 29355992Sbostic * with the processed string. 29455992Sbostic */ 29555992Sbostic static char * 29655992Sbostic compile_delimited(p, d) 29755992Sbostic char *p, *d; 29855992Sbostic { 29955992Sbostic char c; 30055992Sbostic 30155992Sbostic c = *p++; 30255992Sbostic if (c == '\0') 30355992Sbostic return (NULL); 30455992Sbostic else if (c == '\\') 30555992Sbostic err(COMPILE, "\\ can not be used as a string delimiter"); 30655992Sbostic else if (c == '\n') 30755992Sbostic err(COMPILE, "newline can not be used as a string delimiter"); 30855992Sbostic while (*p) { 30955992Sbostic if (*p == '\\' && p[1] == c) 31056019Sbostic p++; 31155992Sbostic else if (*p == '\\' && p[1] == 'n') { 31256019Sbostic *d++ = '\n'; 31356019Sbostic p += 2; 31456019Sbostic continue; 31555992Sbostic } else if (*p == c) { 31655992Sbostic *d = '\0'; 31755992Sbostic return (p + 1); 31855992Sbostic } 31955992Sbostic *d++ = *p++; 32055992Sbostic } 32155992Sbostic return (NULL); 32255992Sbostic } 32355992Sbostic 32455992Sbostic /* 32556019Sbostic * Get a regular expression. P points to the delimiter of the regular 32656019Sbostic * expression; repp points to the address of a regexp pointer. Newline 32756019Sbostic * and delimiter escapes are processed; other escapes are ignored. 32855992Sbostic * Returns a pointer to the first character after the final delimiter 32956019Sbostic * or NULL in the case of a non terminated regular expression. The regexp 33056019Sbostic * pointer is set to the compiled regular expression. 33155992Sbostic * Cflags are passed to regcomp. 33255992Sbostic */ 33355992Sbostic static char * 334*56077Sbostic compile_re(p, repp) 33555992Sbostic char *p; 33656019Sbostic regex_t **repp; 33755992Sbostic { 33855992Sbostic int eval; 33955992Sbostic char re[_POSIX2_LINE_MAX + 1]; 34055992Sbostic 34155992Sbostic p = compile_delimited(p, re); 34256019Sbostic if (p && strlen(re) == 0) { 34356019Sbostic *repp = NULL; 34456019Sbostic return (p); 34556019Sbostic } 34656019Sbostic *repp = xmalloc(sizeof(regex_t)); 347*56077Sbostic if (p && (eval = regcomp(*repp, re, 0)) != 0) 34856019Sbostic err(COMPILE, "RE error: %s", strregerror(eval, *repp)); 349*56077Sbostic if (maxnsub < (*repp)->re_nsub) 350*56077Sbostic maxnsub = (*repp)->re_nsub; 35155992Sbostic return (p); 35255992Sbostic } 35355992Sbostic 35455992Sbostic /* 35555992Sbostic * Compile the substitution string of a regular expression and set res to 35655992Sbostic * point to a saved copy of it. Nsub is the number of parenthesized regular 35755992Sbostic * expressions. 35855992Sbostic */ 35955992Sbostic static char * 36056019Sbostic compile_subst(p, s) 36156019Sbostic char *p; 36256019Sbostic struct s_subst *s; 36355992Sbostic { 36455992Sbostic static char lbuf[_POSIX2_LINE_MAX + 1]; 36556019Sbostic int asize, ref, size; 36656019Sbostic char c, *text, *op, *sp; 36755992Sbostic 36855992Sbostic c = *p++; /* Terminator character */ 36955992Sbostic if (c == '\0') 37055992Sbostic return (NULL); 37155992Sbostic 37256019Sbostic s->maxbref = 0; 37356019Sbostic s->linenum = linenum; 37455992Sbostic asize = 2 * _POSIX2_LINE_MAX + 1; 37555992Sbostic text = xmalloc(asize); 37655992Sbostic size = 0; 37755992Sbostic do { 37856019Sbostic op = sp = text + size; 37955992Sbostic for (; *p; p++) { 38055992Sbostic if (*p == '\\') { 38155992Sbostic p++; 38255992Sbostic if (strchr("123456789", *p) != NULL) { 38356019Sbostic *sp++ = '\\'; 38456019Sbostic ref = *p - '0'; 38556019Sbostic if (s->re != NULL && 38656019Sbostic ref > s->re->re_nsub) 38755992Sbostic err(COMPILE, 38856019Sbostic "\\%c not defined in the RE", *p); 389*56077Sbostic if (s->maxbref < ref) 390*56077Sbostic s->maxbref = ref; 39155992Sbostic } else if (*p == '&') 39256019Sbostic *sp++ = '\\'; 39355992Sbostic } else if (*p == c) { 39455992Sbostic p++; 39556019Sbostic *sp++ = '\0'; 39656019Sbostic size += sp - op; 39756019Sbostic s->new = xrealloc(text, size); 39855992Sbostic return (p); 39955992Sbostic } else if (*p == '\n') { 40055992Sbostic err(COMPILE, 40155992Sbostic "unescaped newline inside substitute pattern"); 40256019Sbostic /* NOTREACHED */ 40355992Sbostic } 40456019Sbostic *sp++ = *p; 40555992Sbostic } 40656019Sbostic size += sp - op; 40755992Sbostic if (asize - size < _POSIX2_LINE_MAX + 1) { 40855992Sbostic asize *= 2; 40955992Sbostic text = xmalloc(asize); 41055992Sbostic } 41155992Sbostic } while (cu_fgets(p = lbuf, sizeof(lbuf))); 41256019Sbostic err(COMPILE, "unterminated substitute in regular expression"); 41356019Sbostic /* NOTREACHED */ 41455992Sbostic } 41555992Sbostic 41655992Sbostic /* 41755992Sbostic * Compile the flags of the s command 41855992Sbostic */ 41955992Sbostic static char * 42055992Sbostic compile_flags(p, s) 42155992Sbostic char *p; 42255992Sbostic struct s_subst *s; 42355992Sbostic { 42455992Sbostic int gn; /* True if we have seen g or n */ 42555992Sbostic char wfile[_POSIX2_LINE_MAX + 1], *q; 42655992Sbostic 42755992Sbostic s->n = 1; /* Default */ 42855992Sbostic s->p = 0; 42955992Sbostic s->wfile = NULL; 43055992Sbostic s->wfd = -1; 43155992Sbostic for (gn = 0;;) { 43255992Sbostic EATSPACE(); /* EXTENSION */ 43355992Sbostic switch (*p) { 43455992Sbostic case 'g': 43555992Sbostic if (gn) 43656004Sbostic err(COMPILE, 43756004Sbostic "more than one number or 'g' in substitute flags"); 43855992Sbostic gn = 1; 43955992Sbostic s->n = 0; 44055992Sbostic break; 44155992Sbostic case '\0': 44255992Sbostic case '\n': 44355992Sbostic case ';': 44455992Sbostic return (p); 44555992Sbostic case 'p': 44655992Sbostic s->p = 1; 44755992Sbostic break; 44855992Sbostic case '1': case '2': case '3': 44955992Sbostic case '4': case '5': case '6': 45055992Sbostic case '7': case '8': case '9': 45155992Sbostic if (gn) 45256004Sbostic err(COMPILE, 45356004Sbostic "more than one number or 'g' in substitute flags"); 45455992Sbostic gn = 1; 45555992Sbostic /* XXX Check for overflow */ 45655992Sbostic s->n = (int)strtol(p, &p, 10); 45755992Sbostic break; 45855992Sbostic case 'w': 45955992Sbostic p++; 46055992Sbostic #ifdef HISTORIC_PRACTICE 46155992Sbostic if (*p != ' ') { 46255992Sbostic err(WARNING, "space missing before w wfile"); 46355992Sbostic return (p); 46455992Sbostic } 46555992Sbostic #endif 46655992Sbostic EATSPACE(); 46755992Sbostic q = wfile; 46855992Sbostic while (*p) { 46955992Sbostic if (*p == '\n') 47055992Sbostic break; 47155992Sbostic *q++ = *p++; 47255992Sbostic } 47355992Sbostic *q = '\0'; 47455992Sbostic if (q == wfile) 47556004Sbostic err(COMPILE, "no wfile specified"); 47655992Sbostic s->wfile = strdup(wfile); 47755992Sbostic if (!aflag && (s->wfd = open(wfile, 47855992Sbostic O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 47955992Sbostic DEFFILEMODE)) == -1) 48055992Sbostic err(FATAL, "%s: %s\n", wfile, strerror(errno)); 48155992Sbostic return (p); 48255992Sbostic default: 48355992Sbostic err(COMPILE, 48456004Sbostic "bad flag in substitute command: '%c'", *p); 48555992Sbostic break; 48655992Sbostic } 48755992Sbostic p++; 48855992Sbostic } 48955992Sbostic } 49055992Sbostic 49155992Sbostic /* 49255992Sbostic * Compile a translation set of strings into a lookup table. 49355992Sbostic */ 49455992Sbostic static char * 49555992Sbostic compile_tr(p, transtab) 49655992Sbostic char *p; 49755992Sbostic char **transtab; 49855992Sbostic { 49955992Sbostic int i; 50055992Sbostic char *lt, *op, *np; 50155992Sbostic char old[_POSIX2_LINE_MAX + 1]; 50255992Sbostic char new[_POSIX2_LINE_MAX + 1]; 50355992Sbostic 50455992Sbostic if (*p == '\0' || *p == '\\') 50555992Sbostic err(COMPILE, 50655992Sbostic "transform pattern can not be delimited by newline or backslash"); 50755992Sbostic p = compile_delimited(p, old); 50855992Sbostic if (p == NULL) { 50955992Sbostic err(COMPILE, "unterminated transform source string"); 51055992Sbostic return (NULL); 51155992Sbostic } 51255992Sbostic p = compile_delimited(--p, new); 51355992Sbostic if (p == NULL) { 51455992Sbostic err(COMPILE, "unterminated transform target string"); 51555992Sbostic return (NULL); 51655992Sbostic } 51755992Sbostic EATSPACE(); 51855992Sbostic if (strlen(new) != strlen(old)) { 51955992Sbostic err(COMPILE, "transform strings are not the same length"); 52055992Sbostic return (NULL); 52155992Sbostic } 52255992Sbostic /* We assume characters are 8 bits */ 52355992Sbostic lt = xmalloc(UCHAR_MAX); 52455992Sbostic for (i = 0; i <= UCHAR_MAX; i++) 52555992Sbostic lt[i] = (char)i; 52655992Sbostic for (op = old, np = new; *op; op++, np++) 52755992Sbostic lt[(u_char)*op] = *np; 52855992Sbostic *transtab = lt; 52955992Sbostic return (p); 53055992Sbostic } 53155992Sbostic 53255992Sbostic /* 53355992Sbostic * Compile the text following an a or i command. 53455992Sbostic */ 53555992Sbostic static char * 53655992Sbostic compile_text() 53755992Sbostic { 53855992Sbostic int asize, size; 53955992Sbostic char *text, *p, *op, *s; 54055992Sbostic char lbuf[_POSIX2_LINE_MAX + 1]; 54155992Sbostic 54255992Sbostic asize = 2 * _POSIX2_LINE_MAX + 1; 54355992Sbostic text = xmalloc(asize); 54455992Sbostic size = 0; 54555992Sbostic while (cu_fgets(lbuf, sizeof(lbuf))) { 54655992Sbostic op = s = text + size; 54755992Sbostic p = lbuf; 54855992Sbostic EATSPACE(); 54955992Sbostic for (; *p; p++) { 55055992Sbostic if (*p == '\\') 55155992Sbostic p++; 55255992Sbostic *s++ = *p; 55355992Sbostic } 55455992Sbostic size += s - op; 55555992Sbostic if (p[-2] != '\\') { 55655992Sbostic *s = '\0'; 55755992Sbostic break; 55855992Sbostic } 55955992Sbostic if (asize - size < _POSIX2_LINE_MAX + 1) { 56055992Sbostic asize *= 2; 56155992Sbostic text = xmalloc(asize); 56255992Sbostic } 56355992Sbostic } 56455992Sbostic return (xrealloc(text, size + 1)); 56555992Sbostic } 56655992Sbostic 56755992Sbostic /* 56855992Sbostic * Get an address and return a pointer to the first character after 56955992Sbostic * it. Fill the structure pointed to according to the address. 57055992Sbostic */ 57155992Sbostic static char * 57255992Sbostic compile_addr(p, a) 57355992Sbostic char *p; 57455992Sbostic struct s_addr *a; 57555992Sbostic { 57655992Sbostic char *end; 57755992Sbostic 57855992Sbostic switch (*p) { 57955992Sbostic case '\\': /* Context address */ 58056019Sbostic ++p; 58156019Sbostic /* FALLTHROUGH */ 58255992Sbostic case '/': /* Context address */ 583*56077Sbostic p = compile_re(p, &a->u.r); 58455992Sbostic if (p == NULL) 58555992Sbostic err(COMPILE, "unterminated regular expression"); 58655992Sbostic a->type = AT_RE; 58755992Sbostic return (p); 58856019Sbostic 58955992Sbostic case '$': /* Last line */ 59055992Sbostic a->type = AT_LAST; 59155992Sbostic return (p + 1); 59255992Sbostic /* Line number */ 59355992Sbostic case '0': case '1': case '2': case '3': case '4': 59455992Sbostic case '5': case '6': case '7': case '8': case '9': 59555992Sbostic a->type = AT_LINE; 59655992Sbostic a->u.l = strtol(p, &end, 10); 59755992Sbostic return (end); 59855992Sbostic default: 59955992Sbostic err(COMPILE, "expected context address"); 60055992Sbostic return (NULL); 60155992Sbostic } 60255992Sbostic } 60355992Sbostic 60455992Sbostic /* 60555992Sbostic * Return a copy of all the characters up to \n or \0 60655992Sbostic */ 60755992Sbostic static char * 60855992Sbostic duptoeol(s) 60955992Sbostic register char *s; 61055992Sbostic { 61155992Sbostic size_t len; 61255992Sbostic char *start; 61355992Sbostic 61455992Sbostic for (start = s; *s != '\0' && *s != '\n'; ++s); 61555992Sbostic *s = '\0'; 61655992Sbostic len = s - start + 1; 61755992Sbostic return (memmove(xmalloc(len), start, len)); 61855992Sbostic } 61955992Sbostic 62055992Sbostic /* 62155992Sbostic * Find the label contained in the command l in the command linked list cp. 62255992Sbostic * L is excluded from the search. Return NULL if not found. 62355992Sbostic */ 62455992Sbostic static struct s_command * 62555992Sbostic findlabel(l, cp) 62655992Sbostic struct s_command *l, *cp; 62755992Sbostic { 62855992Sbostic struct s_command *r; 62955992Sbostic 63055992Sbostic for (; cp; cp = cp->next) 63155992Sbostic if (cp->code == ':' && cp != l && strcmp(l->t, cp->t) == 0) 63255992Sbostic return (cp); 63355992Sbostic else if (cp->code == '{' && (r = findlabel(l, cp->u.c))) 63455992Sbostic return (r); 63555992Sbostic return (NULL); 63655992Sbostic } 63755992Sbostic 63855992Sbostic /* 63955992Sbostic * Convert goto label names to addresses. 64055992Sbostic * Detect duplicate labels. 64155992Sbostic * Set appendnum to the number of a and r commands in the script. 64255992Sbostic * Free the memory used by labels in b and t commands (but not by :) 64355992Sbostic * Root is a pointer to the script linked list; cp points to the 64455992Sbostic * search start. 64555992Sbostic * TODO: Remove } nodes 64655992Sbostic */ 64755992Sbostic static void 64855992Sbostic fixuplabel(root, cp) 64955992Sbostic struct s_command *root, *cp; 65055992Sbostic { 65155992Sbostic struct s_command *cp2; 65255992Sbostic 65355992Sbostic for (; cp; cp = cp->next) 65455992Sbostic switch (cp->code) { 65556019Sbostic case ':': 65656019Sbostic if (findlabel(cp, root)) 65756019Sbostic err(COMPILE2, "duplicate label %s", cp->t); 65856019Sbostic break; 65955992Sbostic case 'a': 66055992Sbostic case 'r': 66155992Sbostic appendnum++; 66255992Sbostic break; 66355992Sbostic case 'b': 66455992Sbostic case 't': 66555992Sbostic if (cp->t == NULL) { 66655992Sbostic cp->u.c = NULL; 66755992Sbostic break; 66855992Sbostic } 66955992Sbostic if ((cp2 = findlabel(cp, root)) == NULL) 67056004Sbostic err(COMPILE2, "undefined label '%s'", cp->t); 67155992Sbostic free(cp->t); 67255992Sbostic cp->u.c = cp2; 67355992Sbostic break; 67455992Sbostic case '{': 67555992Sbostic fixuplabel(root, cp->u.c); 67655992Sbostic break; 67755992Sbostic } 67855992Sbostic } 679