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