Annotation of src/usr.bin/sed/compile.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: compile.c,v 1.13 2002/07/09 21:31:03 deraadt Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1992 Diomidis Spinellis.
5: * Copyright (c) 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Diomidis Spinellis of Imperial College, University of London.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.14 ! millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
1.9 millert 37: /* from: static char sccsid[] = "@(#)compile.c 8.2 (Berkeley) 4/28/95"; */
1.14 ! millert 38: static char *rcsid = "$OpenBSD: compile.c,v 1.13 2002/07/09 21:31:03 deraadt Exp $";
1.1 deraadt 39: #endif /* not lint */
40:
41: #include <sys/types.h>
42: #include <sys/stat.h>
43:
44: #include <ctype.h>
45: #include <errno.h>
46: #include <fcntl.h>
47: #include <limits.h>
48: #include <regex.h>
49: #include <stdio.h>
50: #include <stdlib.h>
51: #include <string.h>
52:
53: #include "defs.h"
54: #include "extern.h"
55:
56: #define LHSZ 128
57: #define LHMASK (LHSZ - 1)
58: static struct labhash {
59: struct labhash *lh_next;
60: u_int lh_hash;
61: struct s_command *lh_cmd;
62: int lh_ref;
63: } *labels[LHSZ];
64:
1.11 millert 65: static char *compile_addr(char *, struct s_addr *);
66: static char *compile_ccl(char **, char *);
67: static char *compile_delimited(char *, char *);
68: static char *compile_flags(char *, struct s_subst *);
69: static char *compile_re(char *, regex_t **);
70: static char *compile_subst(char *, struct s_subst *);
71: static char *compile_text(void);
72: static char *compile_tr(char *, char **);
1.1 deraadt 73: static struct s_command
1.11 millert 74: **compile_stream(struct s_command **);
75: static char *duptoeol(char *, char *);
76: static void enterlabel(struct s_command *);
1.1 deraadt 77: static struct s_command
1.11 millert 78: *findlabel(char *);
79: static void fixuplabel(struct s_command *, struct s_command *);
80: static void uselabel(void);
1.1 deraadt 81:
82: /*
83: * Command specification. This is used to drive the command parser.
84: */
85: struct s_format {
86: char code; /* Command code */
87: int naddr; /* Number of address args */
88: enum e_args args; /* Argument type */
89: };
90:
91: static struct s_format cmd_fmts[] = {
92: {'{', 2, GROUP},
93: {'}', 0, ENDGROUP},
94: {'a', 1, TEXT},
95: {'b', 2, BRANCH},
96: {'c', 2, TEXT},
97: {'d', 2, EMPTY},
98: {'D', 2, EMPTY},
99: {'g', 2, EMPTY},
100: {'G', 2, EMPTY},
101: {'h', 2, EMPTY},
102: {'H', 2, EMPTY},
103: {'i', 1, TEXT},
104: {'l', 2, EMPTY},
105: {'n', 2, EMPTY},
106: {'N', 2, EMPTY},
107: {'p', 2, EMPTY},
108: {'P', 2, EMPTY},
109: {'q', 1, EMPTY},
110: {'r', 1, RFILE},
111: {'s', 2, SUBST},
112: {'t', 2, BRANCH},
113: {'w', 2, WFILE},
114: {'x', 2, EMPTY},
115: {'y', 2, TR},
116: {'!', 2, NONSEL},
117: {':', 0, LABEL},
118: {'#', 0, COMMENT},
119: {'=', 1, EMPTY},
120: {'\0', 0, COMMENT},
121: };
122:
123: /* The compiled program. */
124: struct s_command *prog;
125:
126: /*
127: * Compile the program into prog.
128: * Initialise appends.
129: */
130: void
131: compile()
132: {
133: *compile_stream(&prog) = NULL;
134: fixuplabel(prog, NULL);
135: uselabel();
136: appends = xmalloc(sizeof(struct s_appends) * appendnum);
137: match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
138: }
139:
140: #define EATSPACE() do { \
141: if (p) \
142: while (*p && isascii(*p) && isspace(*p)) \
143: p++; \
144: } while (0)
145:
146: static struct s_command **
147: compile_stream(link)
148: struct s_command **link;
149: {
1.10 mpech 150: char *p;
1.1 deraadt 151: static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */
152: struct s_command *cmd, *cmd2, *stack;
153: struct s_format *fp;
154: int naddr; /* Number of addresses */
155:
156: stack = 0;
157: for (;;) {
158: if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) {
159: if (stack != 0)
160: err(COMPILE, "unexpected EOF (pending }'s)");
161: return (link);
162: }
163:
164: semicolon: EATSPACE();
165: if (p && (*p == '#' || *p == '\0'))
166: continue;
167: *link = cmd = xmalloc(sizeof(struct s_command));
168: link = &cmd->next;
169: cmd->nonsel = cmd->inrange = 0;
170: /* First parse the addresses */
171: naddr = 0;
172:
173: /* Valid characters to start an address */
174: #define addrchar(c) (strchr("0123456789/\\$", (c)))
175: if (addrchar(*p)) {
176: naddr++;
177: cmd->a1 = xmalloc(sizeof(struct s_addr));
178: p = compile_addr(p, cmd->a1);
179: EATSPACE(); /* EXTENSION */
180: if (*p == ',') {
181: p++;
182: EATSPACE(); /* EXTENSION */
183: naddr++;
184: cmd->a2 = xmalloc(sizeof(struct s_addr));
185: p = compile_addr(p, cmd->a2);
186: EATSPACE();
187: } else
188: cmd->a2 = 0;
189: } else
190: cmd->a1 = cmd->a2 = 0;
191:
192: nonsel: /* Now parse the command */
193: if (!*p)
194: err(COMPILE, "command expected");
195: cmd->code = *p;
196: for (fp = cmd_fmts; fp->code; fp++)
197: if (fp->code == *p)
198: break;
199: if (!fp->code)
200: err(COMPILE, "invalid command code %c", *p);
201: if (naddr > fp->naddr)
202: err(COMPILE,
203: "command %c expects up to %d address(es), found %d", *p, fp->naddr, naddr);
204: switch (fp->args) {
205: case NONSEL: /* ! */
206: p++;
207: EATSPACE();
208: cmd->nonsel = ! cmd->nonsel;
209: goto nonsel;
210: case GROUP: /* { */
211: p++;
212: EATSPACE();
213: cmd->next = stack;
214: stack = cmd;
215: link = &cmd->u.c;
216: if (*p)
217: goto semicolon;
218: break;
219: case ENDGROUP:
220: /*
221: * Short-circuit command processing, since end of
222: * group is really just a noop.
223: */
224: cmd->nonsel = 1;
225: if (stack == 0)
226: err(COMPILE, "unexpected }");
227: cmd2 = stack;
228: stack = cmd2->next;
229: cmd2->next = cmd;
230: /*FALLTHROUGH*/
231: case EMPTY: /* d D g G h H l n N p P q x = \0 */
232: p++;
233: EATSPACE();
234: if (*p == ';') {
235: p++;
236: link = &cmd->next;
237: goto semicolon;
238: }
239: if (*p)
240: err(COMPILE,
241: "extra characters at the end of %c command", cmd->code);
242: break;
243: case TEXT: /* a c i */
244: p++;
245: EATSPACE();
246: if (*p != '\\')
247: err(COMPILE,
248: "command %c expects \\ followed by text", cmd->code);
249: p++;
250: EATSPACE();
251: if (*p)
252: err(COMPILE,
253: "extra characters after \\ at the end of %c command", cmd->code);
254: cmd->t = compile_text();
255: break;
256: case COMMENT: /* \0 # */
257: break;
258: case WFILE: /* w */
259: p++;
260: EATSPACE();
261: if (*p == '\0')
262: err(COMPILE, "filename expected");
263: cmd->t = duptoeol(p, "w command");
264: if (aflag)
265: cmd->u.fd = -1;
266: else if ((cmd->u.fd = open(p,
267: O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
268: DEFFILEMODE)) == -1)
1.12 jsyn 269: err(FATAL, "%s: %s", p, strerror(errno));
1.1 deraadt 270: break;
271: case RFILE: /* r */
272: p++;
273: EATSPACE();
1.13 deraadt 274: cmd->t = duptoeol(p, "read command");
1.1 deraadt 275: break;
276: case BRANCH: /* b t */
277: p++;
278: EATSPACE();
279: if (*p == '\0')
280: cmd->t = NULL;
281: else
282: cmd->t = duptoeol(p, "branch");
283: break;
284: case LABEL: /* : */
285: p++;
286: EATSPACE();
287: cmd->t = duptoeol(p, "label");
288: if (strlen(p) == 0)
289: err(COMPILE, "empty label");
290: enterlabel(cmd);
291: break;
292: case SUBST: /* s */
293: p++;
294: if (*p == '\0' || *p == '\\')
295: err(COMPILE,
296: "substitute pattern can not be delimited by newline or backslash");
297: cmd->u.s = xmalloc(sizeof(struct s_subst));
298: p = compile_re(p, &cmd->u.s->re);
299: if (p == NULL)
300: err(COMPILE, "unterminated substitute pattern");
301: --p;
302: p = compile_subst(p, cmd->u.s);
303: p = compile_flags(p, cmd->u.s);
304: EATSPACE();
305: if (*p == ';') {
306: p++;
307: link = &cmd->next;
308: goto semicolon;
309: }
310: break;
311: case TR: /* y */
312: p++;
313: p = compile_tr(p, (char **)&cmd->u.y);
314: EATSPACE();
315: if (*p == ';') {
316: p++;
317: link = &cmd->next;
318: goto semicolon;
319: }
320: if (*p)
321: err(COMPILE,
322: "extra text at the end of a transform command");
323: break;
324: }
325: }
326: }
327:
328: /*
329: * Get a delimited string. P points to the delimeter of the string; d points
330: * to a buffer area. Newline and delimiter escapes are processed; other
331: * escapes are ignored.
332: *
333: * Returns a pointer to the first character after the final delimiter or NULL
334: * in the case of a non-terminated string. The character array d is filled
335: * with the processed string.
336: */
337: static char *
338: compile_delimited(p, d)
339: char *p, *d;
340: {
341: char c;
342:
343: c = *p++;
344: if (c == '\0')
345: return (NULL);
346: else if (c == '\\')
347: err(COMPILE, "\\ can not be used as a string delimiter");
348: else if (c == '\n')
349: err(COMPILE, "newline can not be used as a string delimiter");
350: while (*p) {
351: if (*p == '[') {
352: if ((d = compile_ccl(&p, d)) == NULL)
353: err(COMPILE, "unbalanced brackets ([])");
354: continue;
355: } else if (*p == '\\' && p[1] == '[') {
356: *d++ = *p++;
357: } else if (*p == '\\' && p[1] == c)
358: p++;
359: else if (*p == '\\' && p[1] == 'n') {
360: *d++ = '\n';
361: p += 2;
362: continue;
363: } else if (*p == '\\' && p[1] == '\\')
364: *d++ = *p++;
365: else if (*p == c) {
366: *d = '\0';
367: return (p + 1);
368: }
369: *d++ = *p++;
370: }
371: return (NULL);
372: }
373:
374:
375: /* compile_ccl: expand a POSIX character class */
376: static char *
377: compile_ccl(sp, t)
378: char **sp;
379: char *t;
380: {
381: int c, d;
382: char *s = *sp;
383:
384: *t++ = *s++;
385: if (*s == '^')
386: *t++ = *s++;
387: if (*s == ']')
388: *t++ = *s++;
389: for (; *s && (*t = *s) != ']'; s++, t++)
390: if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
391: *++t = *++s, t++, s++;
392: for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
393: if ((c = *s) == '\0')
394: return NULL;
395: } else if (*s == '\\' && s[1] == 'n')
396: *t = '\n', s++;
397: return (*s == ']') ? *sp = ++s, ++t : NULL;
398: }
399:
400: /*
401: * Get a regular expression. P points to the delimiter of the regular
402: * expression; repp points to the address of a regexp pointer. Newline
403: * and delimiter escapes are processed; other escapes are ignored.
404: * Returns a pointer to the first character after the final delimiter
405: * or NULL in the case of a non terminated regular expression. The regexp
406: * pointer is set to the compiled regular expression.
407: * Cflags are passed to regcomp.
408: */
409: static char *
410: compile_re(p, repp)
411: char *p;
412: regex_t **repp;
413: {
414: int eval;
415: char re[_POSIX2_LINE_MAX + 1];
416:
417: p = compile_delimited(p, re);
418: if (p && strlen(re) == 0) {
419: *repp = NULL;
420: return (p);
421: }
422: *repp = xmalloc(sizeof(regex_t));
1.6 millert 423: if (p && (eval = regcomp(*repp, re, 0)) != 0)
1.1 deraadt 424: err(COMPILE, "RE error: %s", strregerror(eval, *repp));
425: if (maxnsub < (*repp)->re_nsub)
426: maxnsub = (*repp)->re_nsub;
427: return (p);
428: }
429:
430: /*
431: * Compile the substitution string of a regular expression and set res to
432: * point to a saved copy of it. Nsub is the number of parenthesized regular
433: * expressions.
434: */
435: static char *
436: compile_subst(p, s)
437: char *p;
438: struct s_subst *s;
439: {
440: static char lbuf[_POSIX2_LINE_MAX + 1];
441: int asize, ref, size;
442: char c, *text, *op, *sp;
443:
444: c = *p++; /* Terminator character */
445: if (c == '\0')
446: return (NULL);
447:
448: s->maxbref = 0;
449: s->linenum = linenum;
450: asize = 2 * _POSIX2_LINE_MAX + 1;
451: text = xmalloc(asize);
452: size = 0;
453: do {
454: op = sp = text + size;
455: for (; *p; p++) {
456: if (*p == '\\') {
457: p++;
458: if (strchr("123456789", *p) != NULL) {
459: *sp++ = '\\';
460: ref = *p - '0';
461: if (s->re != NULL &&
462: ref > s->re->re_nsub)
463: err(COMPILE,
464: "\\%c not defined in the RE", *p);
465: if (s->maxbref < ref)
466: s->maxbref = ref;
467: } else if (*p == '&' || *p == '\\')
468: *sp++ = '\\';
469: } else if (*p == c) {
470: p++;
471: *sp++ = '\0';
472: size += sp - op;
473: s->new = xrealloc(text, size);
474: return (p);
475: } else if (*p == '\n') {
476: err(COMPILE,
477: "unescaped newline inside substitute pattern");
478: /* NOTREACHED */
479: }
480: *sp++ = *p;
481: }
482: size += sp - op;
483: if (asize - size < _POSIX2_LINE_MAX + 1) {
484: asize *= 2;
485: text = xmalloc(asize);
486: }
487: } while (cu_fgets(p = lbuf, sizeof(lbuf)));
488: err(COMPILE, "unterminated substitute in regular expression");
489: /* NOTREACHED */
490: }
491:
492: /*
493: * Compile the flags of the s command
494: */
495: static char *
496: compile_flags(p, s)
497: char *p;
498: struct s_subst *s;
499: {
500: int gn; /* True if we have seen g or n */
501: char wfile[_POSIX2_LINE_MAX + 1], *q;
502:
503: s->n = 1; /* Default */
504: s->p = 0;
505: s->wfile = NULL;
506: s->wfd = -1;
507: for (gn = 0;;) {
508: EATSPACE(); /* EXTENSION */
509: switch (*p) {
510: case 'g':
511: if (gn)
512: err(COMPILE,
513: "more than one number or 'g' in substitute flags");
514: gn = 1;
515: s->n = 0;
516: break;
517: case '\0':
518: case '\n':
519: case ';':
520: return (p);
521: case 'p':
522: s->p = 1;
523: break;
524: case '1': case '2': case '3':
525: case '4': case '5': case '6':
526: case '7': case '8': case '9':
527: if (gn)
528: err(COMPILE,
529: "more than one number or 'g' in substitute flags");
530: gn = 1;
531: /* XXX Check for overflow */
532: s->n = (int)strtol(p, &p, 10);
533: break;
534: case 'w':
535: p++;
536: #ifdef HISTORIC_PRACTICE
537: if (*p != ' ') {
538: err(WARNING, "space missing before w wfile");
539: return (p);
540: }
541: #endif
542: EATSPACE();
543: q = wfile;
544: while (*p) {
545: if (*p == '\n')
546: break;
547: *q++ = *p++;
548: }
549: *q = '\0';
550: if (q == wfile)
551: err(COMPILE, "no wfile specified");
552: s->wfile = strdup(wfile);
553: if (!aflag && (s->wfd = open(wfile,
554: O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
555: DEFFILEMODE)) == -1)
1.12 jsyn 556: err(FATAL, "%s: %s", wfile, strerror(errno));
1.1 deraadt 557: return (p);
558: default:
559: err(COMPILE,
560: "bad flag in substitute command: '%c'", *p);
561: break;
562: }
563: p++;
564: }
565: }
566:
567: /*
568: * Compile a translation set of strings into a lookup table.
569: */
570: static char *
571: compile_tr(p, transtab)
572: char *p;
573: char **transtab;
574: {
575: int i;
576: char *lt, *op, *np;
577: char old[_POSIX2_LINE_MAX + 1];
578: char new[_POSIX2_LINE_MAX + 1];
579:
580: if (*p == '\0' || *p == '\\')
581: err(COMPILE,
582: "transform pattern can not be delimited by newline or backslash");
583: p = compile_delimited(p, old);
584: if (p == NULL) {
585: err(COMPILE, "unterminated transform source string");
586: return (NULL);
587: }
588: p = compile_delimited(--p, new);
589: if (p == NULL) {
590: err(COMPILE, "unterminated transform target string");
591: return (NULL);
592: }
593: EATSPACE();
594: if (strlen(new) != strlen(old)) {
595: err(COMPILE, "transform strings are not the same length");
596: return (NULL);
597: }
598: /* We assume characters are 8 bits */
599: lt = xmalloc(UCHAR_MAX);
600: for (i = 0; i <= UCHAR_MAX; i++)
601: lt[i] = (char)i;
602: for (op = old, np = new; *op; op++, np++)
603: lt[(u_char)*op] = *np;
604: *transtab = lt;
605: return (p);
606: }
607:
608: /*
1.7 deraadt 609: * Compile the text following an a, c, or i command.
1.1 deraadt 610: */
611: static char *
612: compile_text()
613: {
1.4 deraadt 614: int asize, esc_nl, size;
1.1 deraadt 615: char *text, *p, *op, *s;
616: char lbuf[_POSIX2_LINE_MAX + 1];
617:
618: asize = 2 * _POSIX2_LINE_MAX + 1;
619: text = xmalloc(asize);
620: size = 0;
621: while (cu_fgets(lbuf, sizeof(lbuf))) {
622: op = s = text + size;
623: p = lbuf;
624: EATSPACE();
1.4 deraadt 625: for (esc_nl = 0; *p != '\0'; p++) {
626: if (*p == '\\' && p[1] != '\0' && *++p == '\n')
627: esc_nl = 1;
1.1 deraadt 628: *s++ = *p;
629: }
630: size += s - op;
1.4 deraadt 631: if (!esc_nl) {
1.1 deraadt 632: *s = '\0';
633: break;
634: }
635: if (asize - size < _POSIX2_LINE_MAX + 1) {
636: asize *= 2;
637: text = xmalloc(asize);
638: }
639: }
1.8 brian 640: text[size] = '\0';
1.1 deraadt 641: return (xrealloc(text, size + 1));
642: }
643:
644: /*
645: * Get an address and return a pointer to the first character after
646: * it. Fill the structure pointed to according to the address.
647: */
648: static char *
649: compile_addr(p, a)
650: char *p;
651: struct s_addr *a;
652: {
653: char *end;
654:
655: switch (*p) {
656: case '\\': /* Context address */
657: ++p;
658: /* FALLTHROUGH */
659: case '/': /* Context address */
660: p = compile_re(p, &a->u.r);
661: if (p == NULL)
662: err(COMPILE, "unterminated regular expression");
663: a->type = AT_RE;
664: return (p);
665:
666: case '$': /* Last line */
667: a->type = AT_LAST;
668: return (p + 1);
669: /* Line number */
670: case '0': case '1': case '2': case '3': case '4':
671: case '5': case '6': case '7': case '8': case '9':
672: a->type = AT_LINE;
1.3 millert 673: a->u.l = strtoul(p, &end, 10);
1.1 deraadt 674: return (end);
675: default:
676: err(COMPILE, "expected context address");
677: return (NULL);
678: }
679: }
680:
681: /*
682: * duptoeol --
683: * Return a copy of all the characters up to \n or \0.
684: */
685: static char *
686: duptoeol(s, ctype)
1.10 mpech 687: char *s;
1.1 deraadt 688: char *ctype;
689: {
690: size_t len;
691: int ws;
692: char *start;
693:
694: ws = 0;
695: for (start = s; *s != '\0' && *s != '\n'; ++s)
696: ws = isspace(*s);
697: *s = '\0';
698: if (ws)
699: err(WARNING, "whitespace after %s", ctype);
700: len = s - start + 1;
701: return (memmove(xmalloc(len), start, len));
702: }
703:
704: /*
705: * Convert goto label names to addresses, and count a and r commands, in
706: * the given subset of the script. Free the memory used by labels in b
707: * and t commands (but not by :).
708: *
709: * TODO: Remove } nodes
710: */
711: static void
712: fixuplabel(cp, end)
713: struct s_command *cp, *end;
714: {
715:
716: for (; cp != end; cp = cp->next)
717: switch (cp->code) {
718: case 'a':
719: case 'r':
720: appendnum++;
721: break;
722: case 'b':
723: case 't':
724: /* Resolve branch target. */
725: if (cp->t == NULL) {
726: cp->u.c = NULL;
727: break;
728: }
729: if ((cp->u.c = findlabel(cp->t)) == NULL)
730: err(COMPILE2, "undefined label '%s'", cp->t);
731: free(cp->t);
732: break;
733: case '{':
734: /* Do interior commands. */
735: fixuplabel(cp->u.c, cp->next);
736: break;
737: }
738: }
739:
740: /*
741: * Associate the given command label for later lookup.
742: */
743: static void
744: enterlabel(cp)
745: struct s_command *cp;
746: {
1.10 mpech 747: struct labhash **lhp, *lh;
748: u_char *p;
749: u_int h, c;
1.1 deraadt 750:
751: for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
752: h = (h << 5) + h + c;
753: lhp = &labels[h & LHMASK];
754: for (lh = *lhp; lh != NULL; lh = lh->lh_next)
755: if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
756: err(COMPILE2, "duplicate label '%s'", cp->t);
757: lh = xmalloc(sizeof *lh);
758: lh->lh_next = *lhp;
759: lh->lh_hash = h;
760: lh->lh_cmd = cp;
761: lh->lh_ref = 0;
762: *lhp = lh;
763: }
764:
765: /*
766: * Find the label contained in the command l in the command linked
767: * list cp. L is excluded from the search. Return NULL if not found.
768: */
769: static struct s_command *
770: findlabel(name)
771: char *name;
772: {
1.10 mpech 773: struct labhash *lh;
774: u_char *p;
775: u_int h, c;
1.1 deraadt 776:
777: for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
778: h = (h << 5) + h + c;
779: for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
780: if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
781: lh->lh_ref = 1;
782: return (lh->lh_cmd);
783: }
784: }
785: return (NULL);
786: }
787:
788: /*
789: * Warn about any unused labels. As a side effect, release the label hash
790: * table space.
791: */
792: static void
793: uselabel()
794: {
1.10 mpech 795: struct labhash *lh, *next;
796: int i;
1.1 deraadt 797:
798: for (i = 0; i < LHSZ; i++) {
799: for (lh = labels[i]; lh != NULL; lh = next) {
800: next = lh->lh_next;
801: if (!lh->lh_ref)
802: err(WARNING, "unused label '%s'",
803: lh->lh_cmd->t);
804: free(lh);
805: }
806: }
807: }