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