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