1 /* $OpenBSD: compile.c,v 1.53 2024/07/17 20:57:15 millert 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. Neither the name of the University nor the names of its contributors
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 #include <sys/types.h>
37 #include <sys/stat.h>
38
39 #include <ctype.h>
40 #include <err.h>
41 #include <fcntl.h>
42 #include <limits.h>
43 #include <regex.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include "defs.h"
49 #include "extern.h"
50
51 #define LHSZ 128
52 #define LHMASK (LHSZ - 1)
53 static struct labhash {
54 struct labhash *lh_next;
55 u_int lh_hash;
56 struct s_command *lh_cmd;
57 int lh_ref;
58 } *labels[LHSZ];
59
60 static char *compile_addr(char *, struct s_addr *);
61 static char *compile_ccl(char **, char *);
62 static char *compile_delimited(char *, char *);
63 static char *compile_flags(char *, struct s_subst *);
64 static char *compile_re(char *, regex_t **);
65 static char *compile_subst(char *, struct s_subst *);
66 static char *compile_text(void);
67 static char *compile_tr(char *, char **);
68 static struct s_command
69 **compile_stream(struct s_command **);
70 static char *duptoeol(char *, char *, char **);
71 static void enterlabel(struct s_command *);
72 static struct s_command
73 *findlabel(char *);
74 static void fixuplabel(struct s_command *, struct s_command *);
75 static void uselabel(void);
76
77 /*
78 * Command specification. This is used to drive the command parser.
79 */
80 struct s_format {
81 char code; /* Command code */
82 int naddr; /* Number of address args */
83 enum e_args args; /* Argument type */
84 };
85
86 static struct s_format cmd_fmts[] = {
87 {'{', 2, GROUP},
88 {'}', 0, ENDGROUP},
89 {'a', 1, TEXT},
90 {'b', 2, BRANCH},
91 {'c', 2, TEXT},
92 {'d', 2, EMPTY},
93 {'D', 2, EMPTY},
94 {'g', 2, EMPTY},
95 {'G', 2, EMPTY},
96 {'h', 2, EMPTY},
97 {'H', 2, EMPTY},
98 {'i', 1, TEXT},
99 {'l', 2, EMPTY},
100 {'n', 2, EMPTY},
101 {'N', 2, EMPTY},
102 {'p', 2, EMPTY},
103 {'P', 2, EMPTY},
104 {'q', 1, EMPTY},
105 {'r', 1, RFILE},
106 {'s', 2, SUBST},
107 {'t', 2, BRANCH},
108 {'w', 2, WFILE},
109 {'x', 2, EMPTY},
110 {'y', 2, TR},
111 {'!', 2, NONSEL},
112 {':', 0, LABEL},
113 {'#', 0, COMMENT},
114 {'=', 1, EMPTY},
115 {'\0', 0, COMMENT},
116 };
117
118 /* The compiled program. */
119 struct s_command *prog;
120
121 /*
122 * Compile the program into prog.
123 * Initialise appends.
124 */
125 void
compile(void)126 compile(void)
127 {
128 *compile_stream(&prog) = NULL;
129 fixuplabel(prog, NULL);
130 uselabel();
131 appends = xreallocarray(NULL, appendnum, sizeof(struct s_appends));
132 match = xreallocarray(NULL, maxnsub + 1, sizeof(regmatch_t));
133 }
134
135 #define EATSPACE() do { \
136 if (p) \
137 while (isascii((unsigned char)*p) && \
138 isspace((unsigned char)*p)) \
139 p++; \
140 } while (0)
141
142 static struct s_command **
compile_stream(struct s_command ** link)143 compile_stream(struct s_command **link)
144 {
145 char *p;
146 static char *lbuf; /* To avoid excessive malloc calls */
147 static size_t bufsize;
148 struct s_command *cmd, *cmd2, *stack;
149 struct s_format *fp;
150 int naddr; /* Number of addresses */
151
152 stack = 0;
153 for (;;) {
154 if ((p = cu_getline(&lbuf, &bufsize)) == NULL) {
155 if (stack != 0)
156 error("unexpected EOF (pending }'s)");
157 return (link);
158 }
159
160 semicolon: EATSPACE();
161 if (*p == '#' || *p == '\0')
162 continue;
163 if (*p == ';') {
164 p++;
165 goto semicolon;
166 }
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 }
190 } else {
191 cmd->a1 = cmd->a2 = 0;
192 }
193
194 nonsel: /* Now parse the command */
195 if (!*p)
196 error("command expected");
197 cmd->code = *p;
198 for (fp = cmd_fmts; fp->code; fp++)
199 if (fp->code == *p)
200 break;
201 if (!fp->code)
202 error("invalid command code %c", *p);
203 if (naddr > fp->naddr)
204 error("command %c expects up to %d address(es),"
205 " found %d", *p, fp->naddr, naddr);
206 switch (fp->args) {
207 case NONSEL: /* ! */
208 p++;
209 EATSPACE();
210 cmd->nonsel = 1;
211 goto nonsel;
212 case GROUP: /* { */
213 p++;
214 EATSPACE();
215 cmd->next = stack;
216 stack = cmd;
217 link = &cmd->u.c;
218 if (*p)
219 goto semicolon;
220 break;
221 case ENDGROUP:
222 /*
223 * Short-circuit command processing, since end of
224 * group is really just a noop.
225 */
226 cmd->nonsel = 1;
227 if (stack == 0)
228 error("unexpected }");
229 cmd2 = stack;
230 stack = cmd2->next;
231 cmd2->next = cmd;
232 /*FALLTHROUGH*/
233 case EMPTY: /* d D g G h H l n N p P q x = \0 */
234 p++;
235 EATSPACE();
236 if (*p == ';') {
237 p++;
238 link = &cmd->next;
239 goto semicolon;
240 }
241 if (*p)
242 error("extra characters at the end of %c"
243 " command", cmd->code);
244 break;
245 case TEXT: /* a c i */
246 p++;
247 EATSPACE();
248 if (*p != '\\')
249 error("command %c expects \\ followed by text",
250 cmd->code);
251 p++;
252 EATSPACE();
253 if (*p)
254 error("extra characters after \\ at the"
255 " end of %c command", cmd->code);
256 cmd->t = compile_text();
257 break;
258 case COMMENT: /* \0 # */
259 break;
260 case WFILE: /* w */
261 p++;
262 EATSPACE();
263 if (*p == '\0')
264 error("filename expected");
265 cmd->t = duptoeol(p, "w command", NULL);
266 if (aflag) {
267 cmd->u.fd = -1;
268 pledge_wpath = 1;
269 }
270 else if ((cmd->u.fd = open(p,
271 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
272 DEFFILEMODE)) == -1)
273 err(1, "%s", p);
274 break;
275 case RFILE: /* r */
276 pledge_rpath = 1;
277 p++;
278 EATSPACE();
279 if (*p == '\0')
280 error("filename expected");
281 cmd->t = duptoeol(p, "read command", NULL);
282 break;
283 case BRANCH: /* b t */
284 p++;
285 EATSPACE();
286 if (*p == '\0' || *p == ';')
287 cmd->t = NULL;
288 else
289 cmd->t = duptoeol(p, "branch", &p);
290 if (*p == ';') {
291 p++;
292 goto semicolon;
293 }
294 break;
295 case LABEL: /* : */
296 p++;
297 EATSPACE();
298 cmd->t = duptoeol(p, "label", &p);
299 if (strlen(cmd->t) == 0)
300 error("empty label");
301 enterlabel(cmd);
302 if (*p == ';') {
303 p++;
304 goto semicolon;
305 }
306 break;
307 case SUBST: /* s */
308 p++;
309 if (*p == '\0' || *p == '\\')
310 error("substitute pattern can not be"
311 " delimited by newline or backslash");
312 cmd->u.s = xmalloc(sizeof(struct s_subst));
313 p = compile_re(p, &cmd->u.s->re);
314 if (p == NULL)
315 error("unterminated substitute pattern");
316 --p;
317 p = compile_subst(p, cmd->u.s);
318 p = compile_flags(p, cmd->u.s);
319 EATSPACE();
320 if (*p == ';') {
321 p++;
322 link = &cmd->next;
323 goto semicolon;
324 }
325 break;
326 case TR: /* y */
327 p++;
328 p = compile_tr(p, (char **)&cmd->u.y);
329 EATSPACE();
330 if (*p == ';') {
331 p++;
332 link = &cmd->next;
333 goto semicolon;
334 }
335 if (*p)
336 error("extra text at the end of a"
337 " transform command");
338 break;
339 }
340 }
341 }
342
343 /*
344 * Get a delimited string. P points to the delimiter of the string; d points
345 * to a buffer area. Newline and delimiter escapes are processed; other
346 * escapes are ignored.
347 *
348 * Returns a pointer to the first character after the final delimiter or NULL
349 * in the case of a non-terminated string. The character array d is filled
350 * with the processed string.
351 */
352 static char *
compile_delimited(char * p,char * d)353 compile_delimited(char *p, char *d)
354 {
355 char c;
356
357 c = *p++;
358 if (c == '\0')
359 return (NULL);
360 else if (c == '\\')
361 error("\\ can not be used as a string delimiter");
362 else if (c == '\n')
363 error("newline can not be used as a string delimiter");
364
365 while (p[0]) {
366 /* Unescaped delimiter: We are done. */
367 if (p[0] == c) {
368 *d = '\0';
369 return p + 1;
370 }
371 if (p[0] == '\\') {
372 /* Escaped delimiter: Skip the backslash. */
373 if (p[1] == c) {
374 p++;
375 } else {
376 /* Backslash-n: Match linefeed. */
377 if (p[1] == 'n') {
378 *d++ = '\n';
379 p += 2;
380 /* Other escapes remain unchanged. */
381 } else {
382 *d++ = *p++;
383 *d++ = *p++;
384 }
385 continue;
386 }
387 }
388 if (p[0] != '[')
389 *d++ = *p++;
390 /*
391 * Bracket expression:
392 * It may contain the delimiter without escaping.
393 */
394 else if ((d = compile_ccl(&p, d)) == NULL)
395 error("unbalanced brackets ([])");
396 }
397 return NULL;
398 }
399
400
401 /* compile_ccl: expand a POSIX character class */
402 static char *
compile_ccl(char ** sp,char * t)403 compile_ccl(char **sp, char *t)
404 {
405 int c, d;
406 char *s = *sp;
407
408 *t++ = *s++;
409 if (*s == '^')
410 *t++ = *s++;
411 if (*s == ']')
412 *t++ = *s++;
413 for (; *s && (*t = *s) != ']'; s++, t++)
414 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
415 *++t = *++s, t++, s++;
416 for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
417 if ((c = *s) == '\0')
418 return NULL;
419 } else if (*s == '\\' && s[1] == 'n') {
420 *t = '\n';
421 s++;
422 }
423 if (*s == ']') {
424 *sp = ++s;
425 return (++t);
426 } else {
427 return (NULL);
428 }
429 }
430
431 /*
432 * Get a regular expression. P points to the delimiter of the regular
433 * expression; repp points to the address of a regexp pointer. Newline
434 * and delimiter escapes are processed; other escapes are ignored.
435 * Returns a pointer to the first character after the final delimiter
436 * or NULL in the case of a non terminated regular expression. The regexp
437 * pointer is set to the compiled regular expression.
438 * Cflags are passed to regcomp.
439 */
440 static char *
compile_re(char * p,regex_t ** repp)441 compile_re(char *p, regex_t **repp)
442 {
443 int eval;
444 char *re;
445
446 re = xmalloc(strlen(p) + 1); /* strlen(re) <= strlen(p) */
447 p = compile_delimited(p, re);
448 if (p && strlen(re) == 0) {
449 *repp = NULL;
450 free(re);
451 return (p);
452 }
453 *repp = xmalloc(sizeof(regex_t));
454 if (p && (eval = regcomp(*repp, re, Eflag ? REG_EXTENDED : 0)) != 0)
455 error("RE error: %s", strregerror(eval, *repp));
456 if (maxnsub < (*repp)->re_nsub)
457 maxnsub = (*repp)->re_nsub;
458 free(re);
459 return (p);
460 }
461
462 /*
463 * Compile the substitution string of a regular expression and set res to
464 * point to a saved copy of it. Nsub is the number of parenthesized regular
465 * expressions.
466 */
467 static char *
compile_subst(char * p,struct s_subst * s)468 compile_subst(char *p, struct s_subst *s)
469 {
470 static char *lbuf;
471 static size_t bufsize;
472 size_t asize, ref, size;
473 char c, *text, *op, *sp;
474 int sawesc = 0;
475
476 c = *p++; /* Terminator character */
477 if (c == '\0')
478 return (NULL);
479
480 s->maxbref = 0;
481 s->linenum = linenum;
482 text = NULL;
483 asize = size = 0;
484 do {
485 size_t len = ROUNDLEN(strlen(p) + 1);
486 if (asize - size < len) {
487 do {
488 asize += len;
489 } while (asize - size < len);
490 text = xrealloc(text, asize);
491 }
492 op = sp = text + size;
493 for (; *p; p++) {
494 if (*p == '\\' || sawesc) {
495 /*
496 * If this is a continuation from the last
497 * buffer, we won't have a character to
498 * skip over.
499 */
500 if (sawesc)
501 sawesc = 0;
502 else
503 p++;
504
505 if (*p == '\0') {
506 /*
507 * This escaped character is continued
508 * in the next part of the line. Note
509 * this fact, then cause the loop to
510 * exit w/ normal EOL case and reenter
511 * above with the new buffer.
512 */
513 sawesc = 1;
514 p--;
515 continue;
516 } else if (strchr("123456789", *p) != NULL) {
517 *sp++ = '\\';
518 ref = *p - '0';
519 if (s->re != NULL &&
520 ref > s->re->re_nsub)
521 error("\\%c not defined in the"
522 " RE", *p);
523 if (s->maxbref < ref)
524 s->maxbref = ref;
525 } else if (*p == '&' || *p == '\\')
526 *sp++ = '\\';
527 } else if (*p == c) {
528 p++;
529 *sp++ = '\0';
530 size += sp - op;
531 s->new = xrealloc(text, size);
532 return (p);
533 } else if (*p == '\n') {
534 error("unescaped newline inside substitute"
535 " pattern");
536 }
537 *sp++ = *p;
538 }
539 size += sp - op;
540 } while ((p = cu_getline(&lbuf, &bufsize)));
541 error("unterminated substitute in regular expression");
542 }
543
544 /*
545 * Compile the flags of the s command
546 */
547 static char *
compile_flags(char * p,struct s_subst * s)548 compile_flags(char *p, struct s_subst *s)
549 {
550 int gn; /* True if we have seen g or n */
551 long l;
552
553 s->n = 1; /* Default */
554 s->p = 0;
555 s->wfile = NULL;
556 s->wfd = -1;
557 for (gn = 0;;) {
558 EATSPACE(); /* EXTENSION */
559 switch (*p) {
560 case 'g':
561 if (gn)
562 error("more than one number or 'g' in"
563 " substitute flags");
564 gn = 1;
565 s->n = 0;
566 break;
567 case '\0':
568 case '\n':
569 case ';':
570 return (p);
571 case 'p':
572 s->p = 1;
573 break;
574 case '1': case '2': case '3':
575 case '4': case '5': case '6':
576 case '7': case '8': case '9':
577 if (gn)
578 error("more than one number or 'g' in"
579 " substitute flags");
580 gn = 1;
581 l = strtol(p, &p, 10);
582 if (l <= 0 || l >= INT_MAX)
583 error("number in substitute flags out of"
584 " range");
585 s->n = (int)l;
586 continue;
587 case 'w':
588 p++;
589 EATSPACE();
590 if (*p == '\0')
591 error("filename expected");
592 s->wfile = duptoeol(p, "s command w flag", NULL);
593 *p = '\0';
594 if (aflag)
595 pledge_wpath = 1;
596 else if ((s->wfd = open(s->wfile,
597 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
598 DEFFILEMODE)) == -1)
599 err(1, "%s", s->wfile);
600 return (p);
601 default:
602 error("bad flag in substitute command: '%c'", *p);
603 break;
604 }
605 p++;
606 }
607 }
608
609 /*
610 * Compile a translation set of strings into a lookup table.
611 */
612 static char *
compile_tr(char * old,char ** transtab)613 compile_tr(char *old, char **transtab)
614 {
615 int i;
616 char delimiter, check[UCHAR_MAX + 1];
617 char *new, *end;
618
619 memset(check, 0, sizeof(check));
620 delimiter = *old;
621 if (delimiter == '\\')
622 error("\\ can not be used as a string delimiter");
623 else if (delimiter == '\n' || delimiter == '\0')
624 error("newline can not be used as a string delimiter");
625
626 new = old++;
627 do {
628 if ((new = strchr(new + 1, delimiter)) == NULL)
629 error("unterminated transform source string");
630 } while (*(new - 1) == '\\' && *(new -2) != '\\');
631 *new = '\0';
632 end = new++;
633 do {
634 if ((end = strchr(end + 1, delimiter)) == NULL)
635 error("unterminated transform target string");
636 } while (*(end -1) == '\\' && *(end -2) != '\\');
637 *end = '\0';
638
639 /* We assume characters are 8 bits */
640 *transtab = xmalloc(UCHAR_MAX + 1);
641 for (i = 0; i <= UCHAR_MAX; i++)
642 (*transtab)[i] = (char)i;
643
644 while (*old != '\0' && *new != '\0') {
645 if (*old == '\\') {
646 old++;
647 if (*old == 'n')
648 *old = '\n';
649 else if (*old != delimiter && *old != '\\')
650 error("Unexpected character after backslash");
651 }
652 if (*new == '\\') {
653 new++;
654 if (*new == 'n')
655 *new = '\n';
656 else if (*new != delimiter && *new != '\\')
657 error("Unexpected character after backslash");
658 }
659 if (check[(u_char) *old] == 1)
660 error("Repeated character in source string");
661 check[(u_char) *old] = 1;
662 (*transtab)[(u_char) *old++] = *new++;
663 }
664 if (*old != '\0' || *new != '\0')
665 error("transform strings are not the same length");
666 return end + 1;
667 }
668
669 /*
670 * Compile the text following an a, c, or i command.
671 */
672 static char *
compile_text(void)673 compile_text(void)
674 {
675 size_t asize, size, bufsize;
676 char *lbuf, *text, *p, *op, *s;
677 int esc_nl;
678
679 lbuf = text = NULL;
680 asize = size = 0;
681 while ((p = cu_getline(&lbuf, &bufsize))) {
682 size_t len = ROUNDLEN(strlen(p) + 1);
683 if (asize - size < len) {
684 do {
685 asize += len;
686 } while (asize - size < len);
687 text = xrealloc(text, asize);
688 }
689 op = s = text + size;
690 for (esc_nl = 0; *p != '\0'; p++) {
691 if (*p == '\\' && p[1] != '\0' && *++p == '\n')
692 esc_nl = 1;
693 *s++ = *p;
694 }
695 size += s - op;
696 if (!esc_nl) {
697 *s = '\0';
698 break;
699 }
700 }
701 free(lbuf);
702 text = xrealloc(text, size + 1);
703 text[size] = '\0';
704 return (text);
705 }
706
707 /*
708 * Get an address and return a pointer to the first character after
709 * it. Fill the structure pointed to according to the address.
710 */
711 static char *
compile_addr(char * p,struct s_addr * a)712 compile_addr(char *p, struct s_addr *a)
713 {
714 char *end;
715
716 switch (*p) {
717 case '\\': /* Context address */
718 ++p;
719 /* FALLTHROUGH */
720 case '/': /* Context address */
721 p = compile_re(p, &a->u.r);
722 if (p == NULL)
723 error("unterminated regular expression");
724 a->type = AT_RE;
725 return (p);
726
727 case '$': /* Last line */
728 a->type = AT_LAST;
729 return (p + 1);
730 /* Line number */
731 case '0': case '1': case '2': case '3': case '4':
732 case '5': case '6': case '7': case '8': case '9':
733 a->type = AT_LINE;
734 a->u.l = strtoul(p, &end, 10);
735 return (end);
736 default:
737 error("expected context address");
738 return (NULL);
739 }
740 }
741
742 /*
743 * duptoeol --
744 * Return a copy of all the characters up to \n or \0.
745 */
746 static char *
duptoeol(char * s,char * ctype,char ** semi)747 duptoeol(char *s, char *ctype, char **semi)
748 {
749 size_t len;
750 int ws;
751 char *start;
752
753 ws = 0;
754 if (semi) {
755 for (start = s; *s != '\0' && *s != '\n' && *s != ';'; ++s)
756 ws = isspace((unsigned char)*s);
757 } else {
758 for (start = s; *s != '\0' && *s != '\n'; ++s)
759 ws = isspace((unsigned char)*s);
760 *s = '\0';
761 }
762 if (ws)
763 warning("whitespace after %s", ctype);
764 len = s - start + 1;
765 if (semi)
766 *semi = s;
767 s = xmalloc(len);
768 strlcpy(s, start, len);
769 return (s);
770 }
771
772 /*
773 * Convert goto label names to addresses, and count a and r commands, in
774 * the given subset of the script. Free the memory used by labels in b
775 * and t commands (but not by :).
776 *
777 * TODO: Remove } nodes
778 */
779 static void
fixuplabel(struct s_command * cp,struct s_command * end)780 fixuplabel(struct s_command *cp, struct s_command *end)
781 {
782
783 for (; cp != end; cp = cp->next)
784 switch (cp->code) {
785 case 'a':
786 case 'r':
787 appendnum++;
788 break;
789 case 'b':
790 case 't':
791 /* Resolve branch target. */
792 if (cp->t == NULL) {
793 cp->u.c = NULL;
794 break;
795 }
796 if ((cp->u.c = findlabel(cp->t)) == NULL)
797 error("undefined label '%s'", cp->t);
798 free(cp->t);
799 break;
800 case '{':
801 /* Do interior commands. */
802 fixuplabel(cp->u.c, cp->next);
803 break;
804 }
805 }
806
807 /*
808 * Associate the given command label for later lookup.
809 */
810 static void
enterlabel(struct s_command * cp)811 enterlabel(struct s_command *cp)
812 {
813 struct labhash **lhp, *lh;
814 u_char *p;
815 u_int h, c;
816
817 for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
818 h = (h << 5) + h + c;
819 lhp = &labels[h & LHMASK];
820 for (lh = *lhp; lh != NULL; lh = lh->lh_next)
821 if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
822 error("duplicate label '%s'", cp->t);
823 lh = xmalloc(sizeof *lh);
824 lh->lh_next = *lhp;
825 lh->lh_hash = h;
826 lh->lh_cmd = cp;
827 lh->lh_ref = 0;
828 *lhp = lh;
829 }
830
831 /*
832 * Find the label contained in the command l in the command linked
833 * list cp. L is excluded from the search. Return NULL if not found.
834 */
835 static struct s_command *
findlabel(char * name)836 findlabel(char *name)
837 {
838 struct labhash *lh;
839 u_char *p;
840 u_int h, c;
841
842 for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
843 h = (h << 5) + h + c;
844 for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
845 if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
846 lh->lh_ref = 1;
847 return (lh->lh_cmd);
848 }
849 }
850 return (NULL);
851 }
852
853 /*
854 * Warn about any unused labels. As a side effect, release the label hash
855 * table space.
856 */
857 static void
uselabel(void)858 uselabel(void)
859 {
860 struct labhash *lh, *next;
861 int i;
862
863 for (i = 0; i < LHSZ; i++) {
864 for (lh = labels[i]; lh != NULL; lh = next) {
865 next = lh->lh_next;
866 if (!lh->lh_ref)
867 warning("unused label '%s'",
868 lh->lh_cmd->t);
869 free(lh);
870 }
871 }
872 }
873