binutils: allow static linking
[openadk.git] / package / toolbox / src / sed / compile.c
blob6d86428ca31c9de18f6f78e8e894e9c0e31b46a3
1 /* $OpenBSD: compile.c,v 1.42 2017/08/01 18:05:53 martijn Exp $ */
3 /*-
4 * Copyright (c) 2016
5 * mirabilos <m@mirbsd.org>
6 * Copyright (c) 1992 Diomidis Spinellis.
7 * Copyright (c) 1992, 1993
8 * The Regents of the University of California. All rights reserved.
10 * This code is derived from software contributed to Berkeley by
11 * Diomidis Spinellis of Imperial College, University of London.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. 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.
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.
38 #include <sys/types.h>
39 #include <sys/stat.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <limits.h>
45 #include <regex.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #include "defs.h"
51 #include "extern.h"
53 __RCSID("$MirOS: src/usr.bin/sed/compile.c,v 1.3 2017/11/20 01:23:56 tg Exp $");
55 #define LHSZ 128
56 #define LHMASK (LHSZ - 1)
57 static struct labhash {
58 struct labhash *lh_next;
59 u_int lh_hash;
60 struct s_command *lh_cmd;
61 int lh_ref;
62 } *labels[LHSZ];
64 static char *compile_addr(char *, struct s_addr *);
65 static char *compile_ccl(char **, char *);
66 static char *compile_delimited(char *, char *, int);
67 static char *compile_flags(char *, struct s_subst *);
68 static char *compile_re(char *, regex_t **);
69 static char *compile_subst(char *, struct s_subst *);
70 static char *compile_text(void);
71 static char *compile_tr(char *, char **);
72 static struct s_command
73 **compile_stream(struct s_command **);
74 static char *duptoeol(char *, const char *, char **);
75 static void enterlabel(struct s_command *);
76 static struct s_command
77 *findlabel(char *);
78 static void fixuplabel(struct s_command *, struct s_command *);
79 static void uselabel(void);
82 * Command specification. This is used to drive the command parser.
84 struct s_format {
85 char code; /* Command code */
86 int naddr; /* Number of address args */
87 enum e_args args; /* Argument type */
90 static struct s_format cmd_fmts[] = {
91 {'{', 2, GROUP},
92 {'}', 0, ENDGROUP},
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},
122 /* The compiled program. */
123 struct s_command *prog;
126 * Compile the program into prog.
127 * Initialise appends.
129 void
130 compile(void)
132 *compile_stream(&prog) = NULL;
133 fixuplabel(prog, NULL);
134 uselabel();
135 appends = xreallocarray(NULL, appendnum, sizeof(struct s_appends));
136 match = xreallocarray(NULL, maxnsub + 1, sizeof(regmatch_t));
139 #define EATSPACE() do { \
140 if (p) \
141 while (isascii((unsigned char)*p) && \
142 isspace((unsigned char)*p)) \
143 p++; \
144 } while (0)
146 static struct s_command **
147 compile_stream(struct s_command **link)
149 char *p;
150 static char *lbuf; /* To avoid excessive malloc calls */
151 static size_t bufsize;
152 struct s_command *cmd, *cmd2, *stack;
153 struct s_format *fp;
154 int naddr; /* Number of addresses */
156 stack = 0;
157 for (;;) {
158 if ((p = cu_fgets(&lbuf, &bufsize)) == NULL) {
159 if (stack != 0)
160 error(COMPILE, "unexpected EOF (pending }'s)");
161 return (link);
164 semicolon: EATSPACE();
165 if (*p == '#' || *p == '\0')
166 continue;
167 if (*p == ';') {
168 p++;
169 goto semicolon;
171 *link = cmd = xmalloc(sizeof(struct s_command));
172 link = &cmd->next;
173 cmd->nonsel = cmd->inrange = 0;
174 /* First parse the addresses */
175 naddr = 0;
177 /* Valid characters to start an address */
178 #define addrchar(c) (strchr("0123456789/\\$", (c)))
179 if (addrchar(*p)) {
180 naddr++;
181 cmd->a1 = xmalloc(sizeof(struct s_addr));
182 p = compile_addr(p, cmd->a1);
183 EATSPACE(); /* EXTENSION */
184 if (*p == ',') {
185 p++;
186 EATSPACE(); /* EXTENSION */
187 naddr++;
188 cmd->a2 = xmalloc(sizeof(struct s_addr));
189 p = compile_addr(p, cmd->a2);
190 EATSPACE();
191 } else {
192 cmd->a2 = 0;
194 } else {
195 cmd->a1 = cmd->a2 = 0;
198 nonsel: /* Now parse the command */
199 if (!*p)
200 error(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 error(COMPILE, "invalid command code %c", *p);
207 if (naddr > fp->naddr)
208 error(COMPILE,
209 "command %c expects up to %d address(es), found %d",
210 *p, fp->naddr, naddr);
211 switch (fp->args) {
212 case NONSEL: /* ! */
213 p++;
214 EATSPACE();
215 cmd->nonsel = 1;
216 goto nonsel;
217 case GROUP: /* { */
218 p++;
219 EATSPACE();
220 cmd->next = stack;
221 stack = cmd;
222 link = &cmd->u.c;
223 if (*p)
224 goto semicolon;
225 break;
226 case ENDGROUP:
228 * Short-circuit command processing, since end of
229 * group is really just a noop.
231 cmd->nonsel = 1;
232 if (stack == 0)
233 error(COMPILE, "unexpected }");
234 cmd2 = stack;
235 stack = cmd2->next;
236 cmd2->next = cmd;
237 /*FALLTHROUGH*/
238 case EMPTY: /* d D g G h H l n N p P q x = \0 */
239 p++;
240 EATSPACE();
241 if (*p == ';') {
242 p++;
243 link = &cmd->next;
244 goto semicolon;
246 if (*p)
247 error(COMPILE,
248 "extra characters at the end of %c command", cmd->code);
249 break;
250 case TEXT: /* a c i */
251 p++;
252 EATSPACE();
253 if (*p != '\\')
254 error(COMPILE, "command %c expects \\ followed by"
255 " text", cmd->code);
256 p++;
257 EATSPACE();
258 if (*p)
259 error(COMPILE, "extra characters after \\ at the"
260 " end of %c command", cmd->code);
261 cmd->t = compile_text();
262 break;
263 case COMMENT: /* \0 # */
264 break;
265 case WFILE: /* w */
266 p++;
267 EATSPACE();
268 if (*p == '\0')
269 error(COMPILE, "filename expected");
270 cmd->t = duptoeol(p, "w command", NULL);
271 if (aflag) {
272 cmd->u.fd = -1;
273 pledge_wpath = 1;
275 else if ((cmd->u.fd = open(p,
276 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
277 DEFFILEMODE)) == -1)
278 error(FATAL, "%s: %s", p, strerror(errno));
279 break;
280 case RFILE: /* r */
281 pledge_rpath = 1;
282 p++;
283 EATSPACE();
284 cmd->t = duptoeol(p, "read command", NULL);
285 break;
286 case BRANCH: /* b t */
287 p++;
288 EATSPACE();
289 if (*p == '\0')
290 cmd->t = NULL;
291 else
292 cmd->t = duptoeol(p, "branch", &p);
293 if (*p == ';') {
294 p++;
295 goto semicolon;
297 break;
298 case LABEL: /* : */
299 p++;
300 EATSPACE();
301 cmd->t = duptoeol(p, "label", &p);
302 if (strlen(cmd->t) == 0)
303 error(COMPILE, "empty label");
304 enterlabel(cmd);
305 if (*p == ';') {
306 p++;
307 goto semicolon;
309 break;
310 case SUBST: /* s */
311 p++;
312 if (*p == '\0' || *p == '\\')
313 error(COMPILE, "substitute pattern can not be"
314 " delimited by newline or backslash");
315 cmd->u.s = xmalloc(sizeof(struct s_subst));
316 p = compile_re(p, &cmd->u.s->re);
317 if (p == NULL)
318 error(COMPILE, "unterminated substitute pattern");
319 --p;
320 p = compile_subst(p, cmd->u.s);
321 p = compile_flags(p, cmd->u.s);
322 EATSPACE();
323 if (*p == ';') {
324 p++;
325 link = &cmd->next;
326 goto semicolon;
328 break;
329 case TR: /* y */
330 p++;
331 p = compile_tr(p, (char **)&cmd->u.y);
332 EATSPACE();
333 if (*p == ';') {
334 p++;
335 link = &cmd->next;
336 goto semicolon;
338 if (*p)
339 error(COMPILE, "extra text at the end of a"
340 " transform command");
341 break;
347 * Get a delimited string. P points to the delimeter of the string; d points
348 * to a buffer area. Newline and delimiter escapes are processed; other
349 * escapes are ignored.
351 * Returns a pointer to the first character after the final delimiter or NULL
352 * in the case of a non-terminated string. The character array d is filled
353 * with the processed string.
355 static char *
356 compile_delimited(char *p, char *d, int is_tr)
358 char c;
360 c = *p++;
361 if (c == '\0')
362 return (NULL);
363 else if (c == '\\')
364 error(COMPILE, "\\ can not be used as a string delimiter");
365 else if (c == '\n')
366 error(COMPILE, "newline can not be used as a string delimiter");
367 while (*p) {
368 if (*p == '[' && *p != c) {
369 if ((d = compile_ccl(&p, d)) == NULL)
370 error(COMPILE, "unbalanced brackets ([])");
371 continue;
372 } else if (*p == '\\' && p[1] == '[') {
373 *d++ = *p++;
374 } else if (*p == '\\' && p[1] == c) {
375 p++;
376 } else if (*p == '\\' && p[1] == 'n') {
377 *d++ = '\n';
378 p += 2;
379 continue;
380 } else if (*p == '\\' && p[1] == '\\') {
381 if (is_tr)
382 p++;
383 else
384 *d++ = *p++;
385 } else if (*p == c) {
386 *d = '\0';
387 return (p + 1);
389 *d++ = *p++;
391 return (NULL);
395 /* compile_ccl: expand a POSIX character class */
396 static char *
397 compile_ccl(char **sp, char *t)
399 int c, d;
400 char *s = *sp;
402 *t++ = *s++;
403 if (*s == '^')
404 *t++ = *s++;
405 if (*s == ']')
406 *t++ = *s++;
407 for (; *s && (*t = *s) != ']'; s++, t++)
408 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
409 *++t = *++s, t++, s++;
410 for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
411 if ((c = *s) == '\0')
412 return NULL;
413 } else if (*s == '\\' && s[1] == 'n') {
414 *t = '\n';
415 s++;
417 if (*s == ']') {
418 *sp = ++s;
419 return (++t);
420 } else {
421 return (NULL);
426 * Get a regular expression. P points to the delimiter of the regular
427 * expression; repp points to the address of a regexp pointer. Newline
428 * and delimiter escapes are processed; other escapes are ignored.
429 * Returns a pointer to the first character after the final delimiter
430 * or NULL in the case of a non terminated regular expression. The regexp
431 * pointer is set to the compiled regular expression.
432 * Cflags are passed to regcomp.
434 static char *
435 compile_re(char *p, regex_t **repp)
437 int eval;
438 char *re;
440 re = xmalloc(strlen(p) + 1); /* strlen(re) <= strlen(p) */
441 p = compile_delimited(p, re, 0);
442 if (p && strlen(re) == 0) {
443 *repp = NULL;
444 free(re);
445 return (p);
447 *repp = xmalloc(sizeof(regex_t));
448 if (p && (eval = regcomp(*repp, re, Eflag ? REG_EXTENDED : 0)) != 0)
449 error(COMPILE, "RE error: %s", strregerror(eval, *repp));
450 if (maxnsub < (*repp)->re_nsub)
451 maxnsub = (*repp)->re_nsub;
452 free(re);
453 return (p);
457 * Compile the substitution string of a regular expression and set res to
458 * point to a saved copy of it. Nsub is the number of parenthesized regular
459 * expressions.
461 static char *
462 compile_subst(char *p, struct s_subst *s)
464 static char *lbuf;
465 static size_t bufsize;
466 size_t asize, ref, size;
467 char c, *text, *op, *sp;
468 int sawesc = 0;
470 c = *p++; /* Terminator character */
471 if (c == '\0')
472 return (NULL);
474 s->maxbref = 0;
475 s->linenum = linenum;
476 text = NULL;
477 asize = size = 0;
478 do {
479 size_t len = ROUNDLEN(strlen(p) + 1);
480 if (asize - size < len) {
481 do {
482 asize += len;
483 } while (asize - size < len);
484 text = xrealloc(text, asize);
486 op = sp = text + size;
487 for (; *p; p++) {
488 if (*p == '\\' || sawesc) {
490 * If this is a continuation from the last
491 * buffer, we won't have a character to
492 * skip over.
494 if (sawesc)
495 sawesc = 0;
496 else
497 p++;
499 if (*p == '\0') {
501 * This escaped character is continued
502 * in the next part of the line. Note
503 * this fact, then cause the loop to
504 * exit w/ normal EOL case and reenter
505 * above with the new buffer.
507 sawesc = 1;
508 p--;
509 continue;
510 } else if (strchr("123456789", *p) != NULL) {
511 *sp++ = '\\';
512 ref = *p - '0';
513 if (s->re != NULL &&
514 ref > s->re->re_nsub)
515 error(COMPILE,
516 "\\%c not defined in the RE", *p);
517 if (s->maxbref < ref)
518 s->maxbref = ref;
519 } else if (*p == '&' || *p == '\\')
520 *sp++ = '\\';
521 } else if (*p == c) {
522 p++;
523 *sp++ = '\0';
524 size += sp - op;
525 s->new = xrealloc(text, size);
526 return (p);
527 } else if (*p == '\n') {
528 error(COMPILE,
529 "unescaped newline inside substitute pattern");
531 *sp++ = *p;
533 size += sp - op;
534 } while ((p = cu_fgets(&lbuf, &bufsize)));
535 error(COMPILE, "unterminated substitute in regular expression");
539 * Compile the flags of the s command
541 static char *
542 compile_flags(char *p, struct s_subst *s)
544 int gn; /* True if we have seen g or n */
545 long l;
546 char wfile[PATH_MAX], *q, *eq;
548 s->n = 1; /* Default */
549 s->p = 0;
550 s->wfile = NULL;
551 s->wfd = -1;
552 for (gn = 0;;) {
553 EATSPACE(); /* EXTENSION */
554 switch (*p) {
555 case 'g':
556 if (gn)
557 error(COMPILE, "more than one number or 'g' in"
558 " substitute flags");
559 gn = 1;
560 s->n = 0;
561 break;
562 case '\0':
563 case '\n':
564 case ';':
565 return (p);
566 case 'p':
567 s->p = 1;
568 break;
569 case '1': case '2': case '3':
570 case '4': case '5': case '6':
571 case '7': case '8': case '9':
572 if (gn)
573 error(COMPILE, "more than one number or 'g' in"
574 " substitute flags");
575 gn = 1;
576 l = strtol(p, &p, 10);
577 if (l <= 0 || l >= INT_MAX)
578 error(COMPILE,
579 "number in substitute flags out of range");
580 s->n = (int)l;
581 continue;
582 case 'w':
583 p++;
584 #ifdef HISTORIC_PRACTICE
585 if (*p != ' ') {
586 warning("space missing before w wfile");
587 return (p);
589 #endif
590 EATSPACE();
591 q = wfile;
592 eq = wfile + sizeof(wfile) - 1;
593 while (*p) {
594 if (*p == '\n')
595 break;
596 if (q >= eq)
597 error(COMPILE, "wfile too long");
598 *q++ = *p++;
600 *q = '\0';
601 if (q == wfile)
602 error(COMPILE, "no wfile specified");
603 s->wfile = strdup(wfile);
604 if (aflag)
605 pledge_wpath = 1;
606 else if ((s->wfd = open(wfile,
607 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
608 DEFFILEMODE)) == -1)
609 error(FATAL, "%s: %s", wfile, strerror(errno));
610 return (p);
611 default:
612 error(COMPILE,
613 "bad flag in substitute command: '%c'", *p);
614 break;
616 p++;
621 * Compile a translation set of strings into a lookup table.
623 static char *
624 compile_tr(char *p, char **transtab)
626 int i;
627 char *lt, *op, *np;
628 char *old = NULL, *new = NULL;
630 if (*p == '\0' || *p == '\\')
631 error(COMPILE,
632 "transform pattern can not be delimited by newline or backslash");
633 old = xmalloc(strlen(p) + 1);
634 p = compile_delimited(p, old, 1);
635 if (p == NULL) {
636 error(COMPILE, "unterminated transform source string");
637 goto bad;
639 new = xmalloc(strlen(p) + 1);
640 p = compile_delimited(--p, new, 1);
641 if (p == NULL) {
642 error(COMPILE, "unterminated transform target string");
643 goto bad;
645 EATSPACE();
646 if (strlen(new) != strlen(old)) {
647 error(COMPILE, "transform strings are not the same length");
648 goto bad;
650 /* We assume characters are 8 bits */
651 lt = xmalloc(UCHAR_MAX + 1);
652 for (i = 0; i <= UCHAR_MAX; i++)
653 lt[i] = (char)i;
654 for (op = old, np = new; *op; op++, np++)
655 lt[(u_char)*op] = *np;
656 *transtab = lt;
657 free(old);
658 free(new);
659 return (p);
660 bad:
661 free(old);
662 free(new);
663 return (NULL);
667 * Compile the text following an a, c, or i command.
669 static char *
670 compile_text(void)
672 size_t asize, size;
673 int esc_nl;
674 char *lbuf, *text, *p, *op, *s;
675 size_t bufsize;
677 lbuf = text = NULL;
678 asize = size = 0;
679 while ((p = cu_fgets(&lbuf, &bufsize))) {
680 size_t len = ROUNDLEN(strlen(p) + 1);
681 if (asize - size < len) {
682 do {
683 asize += len;
684 } while (asize - size < len);
685 text = xrealloc(text, asize);
687 op = s = text + size;
688 for (esc_nl = 0; *p != '\0'; p++) {
689 if (*p == '\\' && p[1] != '\0' && *++p == '\n')
690 esc_nl = 1;
691 *s++ = *p;
693 size += s - op;
694 if (!esc_nl) {
695 *s = '\0';
696 break;
699 free(lbuf);
700 text = xrealloc(text, size + 1);
701 text[size] = '\0';
702 return (text);
706 * Get an address and return a pointer to the first character after
707 * it. Fill the structure pointed to according to the address.
709 static char *
710 compile_addr(char *p, struct s_addr *a)
712 char *end;
714 switch (*p) {
715 case '\\': /* Context address */
716 ++p;
717 /* FALLTHROUGH */
718 case '/': /* Context address */
719 p = compile_re(p, &a->u.r);
720 if (p == NULL)
721 error(COMPILE, "unterminated regular expression");
722 a->type = AT_RE;
723 return (p);
725 case '$': /* Last line */
726 a->type = AT_LAST;
727 return (p + 1);
728 /* Line number */
729 case '0': case '1': case '2': case '3': case '4':
730 case '5': case '6': case '7': case '8': case '9':
731 a->type = AT_LINE;
732 a->u.l = strtoul(p, &end, 10);
733 return (end);
734 default:
735 error(COMPILE, "expected context address");
736 return (NULL);
741 * duptoeol --
742 * Return a copy of all the characters up to \n or \0.
744 static char *
745 duptoeol(char *s, const char *ctype, char **semi)
747 size_t len;
748 int ws;
749 char *start;
751 ws = 0;
752 if (semi) {
753 for (start = s; *s != '\0' && *s != '\n' && *s != ';'; ++s)
754 ws = isspace((unsigned char)*s);
755 } else {
756 for (start = s; *s != '\0' && *s != '\n'; ++s)
757 ws = isspace((unsigned char)*s);
758 *s = '\0';
760 if (ws)
761 warning("whitespace after %s", ctype);
762 len = s - start + 1;
763 if (semi)
764 *semi = s;
765 s = xmalloc(len);
766 strlcpy(s, start, len);
767 return (s);
771 * Convert goto label names to addresses, and count a and r commands, in
772 * the given subset of the script. Free the memory used by labels in b
773 * and t commands (but not by :).
775 * TODO: Remove } nodes
777 static void
778 fixuplabel(struct s_command *cp, struct s_command *end)
781 for (; cp != end; cp = cp->next)
782 switch (cp->code) {
783 case 'a':
784 case 'r':
785 appendnum++;
786 break;
787 case 'b':
788 case 't':
789 /* Resolve branch target. */
790 if (cp->t == NULL) {
791 cp->u.c = NULL;
792 break;
794 if ((cp->u.c = findlabel(cp->t)) == NULL)
795 error(COMPILE, "undefined label '%s'", cp->t);
796 free(cp->t);
797 break;
798 case '{':
799 /* Do interior commands. */
800 fixuplabel(cp->u.c, cp->next);
801 break;
806 * Associate the given command label for later lookup.
808 static void
809 enterlabel(struct s_command *cp)
811 struct labhash **lhp, *lh;
812 u_char *p;
813 u_int h, c;
815 for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
816 h = (h << 5) + h + c;
817 lhp = &labels[h & LHMASK];
818 for (lh = *lhp; lh != NULL; lh = lh->lh_next)
819 if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
820 error(COMPILE, "duplicate label '%s'", cp->t);
821 lh = xmalloc(sizeof *lh);
822 lh->lh_next = *lhp;
823 lh->lh_hash = h;
824 lh->lh_cmd = cp;
825 lh->lh_ref = 0;
826 *lhp = lh;
830 * Find the label contained in the command l in the command linked
831 * list cp. L is excluded from the search. Return NULL if not found.
833 static struct s_command *
834 findlabel(char *name)
836 struct labhash *lh;
837 u_char *p;
838 u_int h, c;
840 for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
841 h = (h << 5) + h + c;
842 for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
843 if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
844 lh->lh_ref = 1;
845 return (lh->lh_cmd);
848 return (NULL);
852 * Warn about any unused labels. As a side effect, release the label hash
853 * table space.
855 static void
856 uselabel(void)
858 struct labhash *lh, *next;
859 int i;
861 for (i = 0; i < LHSZ; i++) {
862 for (lh = labels[i]; lh != NULL; lh = next) {
863 next = lh->lh_next;
864 if (!lh->lh_ref)
865 warning("unused label '%s'",
866 lh->lh_cmd->t);
867 free(lh);