1 /* $NetBSD: compile.c,v 1.24 2002/06/14 00:41:42 wiz 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.24 2002/06/14 00:41:42 wiz 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(char *, struct s_addr *); 74 static char *compile_ccl(char **, char *); 75 static char *compile_delimited(char *, char *); 76 static char *compile_flags(char *, struct s_subst *); 77 static char *compile_re(char *, regex_t **); 78 static char *compile_subst(char *, struct s_subst *); 79 static char *compile_text(void); 80 static char *compile_tr(char *, char **); 81 static struct s_command 82 **compile_stream(struct s_command **); 83 static char *duptoeol(char *, char *); 84 static void enterlabel(struct s_command *); 85 static struct s_command 86 *findlabel(char *); 87 static void fixuplabel(struct s_command *, struct s_command *); 88 static void uselabel(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(void) 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(struct s_command **link) 158 { 159 char *p; 160 static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ 161 struct s_command *cmd, *cmd2, *stack; 162 struct s_format *fp; 163 int naddr; /* Number of addresses */ 164 165 stack = 0; 166 for (;;) { 167 if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) { 168 if (stack != 0) 169 err(COMPILE, "unexpected EOF (pending }'s)"); 170 return (link); 171 } 172 173 semicolon: EATSPACE(); 174 if (p) { 175 if (*p == '#' || *p == '\0') 176 continue; 177 else if (*p == ';') { 178 p++; 179 goto semicolon; 180 } 181 } 182 *link = cmd = xmalloc(sizeof(struct s_command)); 183 link = &cmd->next; 184 cmd->nonsel = cmd->inrange = 0; 185 /* First parse the addresses */ 186 naddr = 0; 187 188 /* Valid characters to start an address */ 189 #define addrchar(c) (strchr("0123456789/\\$", (c))) 190 if (addrchar(*p)) { 191 naddr++; 192 cmd->a1 = xmalloc(sizeof(struct s_addr)); 193 p = compile_addr(p, cmd->a1); 194 EATSPACE(); /* EXTENSION */ 195 if (*p == ',') { 196 p++; 197 EATSPACE(); /* EXTENSION */ 198 naddr++; 199 cmd->a2 = xmalloc(sizeof(struct s_addr)); 200 p = compile_addr(p, cmd->a2); 201 EATSPACE(); 202 } else 203 cmd->a2 = 0; 204 } else 205 cmd->a1 = cmd->a2 = 0; 206 207 nonsel: /* Now parse the command */ 208 if (!*p) 209 err(COMPILE, "command expected"); 210 cmd->code = *p; 211 for (fp = cmd_fmts; fp->code; fp++) 212 if (fp->code == *p) 213 break; 214 if (!fp->code) 215 err(COMPILE, "invalid command code %c", *p); 216 if (naddr > fp->naddr) 217 err(COMPILE, 218 "command %c expects up to %d address(es), found %d", *p, fp->naddr, naddr); 219 switch (fp->args) { 220 case NONSEL: /* ! */ 221 p++; 222 EATSPACE(); 223 cmd->nonsel = ! cmd->nonsel; 224 goto nonsel; 225 case GROUP: /* { */ 226 p++; 227 EATSPACE(); 228 cmd->next = stack; 229 stack = cmd; 230 link = &cmd->u.c; 231 if (*p) 232 goto semicolon; 233 break; 234 case ENDGROUP: 235 /* 236 * Short-circuit command processing, since end of 237 * group is really just a noop. 238 */ 239 cmd->nonsel = 1; 240 if (stack == 0) 241 err(COMPILE, "unexpected }"); 242 cmd2 = stack; 243 stack = cmd2->next; 244 cmd2->next = cmd; 245 /*FALLTHROUGH*/ 246 case EMPTY: /* d D g G h H l n N p P q x = \0 */ 247 p++; 248 EATSPACE(); 249 if (*p == ';') { 250 p++; 251 link = &cmd->next; 252 goto semicolon; 253 } 254 if (*p) 255 err(COMPILE, 256 "extra characters at the end of %c command", cmd->code); 257 break; 258 case TEXT: /* a c i */ 259 p++; 260 EATSPACE(); 261 if (*p != '\\') 262 err(COMPILE, 263 "command %c expects \\ followed by text", cmd->code); 264 p++; 265 EATSPACE(); 266 if (*p) 267 err(COMPILE, 268 "extra characters after \\ at the end of %c command", cmd->code); 269 cmd->t = compile_text(); 270 break; 271 case COMMENT: /* \0 # */ 272 break; 273 case WFILE: /* w */ 274 p++; 275 EATSPACE(); 276 if (*p == '\0') 277 err(COMPILE, "filename expected"); 278 cmd->t = duptoeol(p, "w command"); 279 if (aflag) 280 cmd->u.fd = -1; 281 else if ((cmd->u.fd = open(p, 282 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 283 DEFFILEMODE)) == -1) 284 err(FATAL, "%s: %s", p, strerror(errno)); 285 break; 286 case RFILE: /* r */ 287 p++; 288 EATSPACE(); 289 if (*p == '\0') 290 err(COMPILE, "filename expected"); 291 else 292 cmd->t = duptoeol(p, "read command"); 293 break; 294 case BRANCH: /* b t */ 295 p++; 296 EATSPACE(); 297 if (*p == '\0') 298 cmd->t = NULL; 299 else 300 cmd->t = duptoeol(p, "branch"); 301 break; 302 case LABEL: /* : */ 303 p++; 304 EATSPACE(); 305 cmd->t = duptoeol(p, "label"); 306 if (strlen(p) == 0) 307 err(COMPILE, "empty label"); 308 enterlabel(cmd); 309 break; 310 case SUBST: /* s */ 311 p++; 312 if (*p == '\0' || *p == '\\') 313 err(COMPILE, 314 "substitute pattern can not be 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 err(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; 327 } 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; 337 } 338 if (*p) 339 err(COMPILE, 340 "extra text at the end of a transform command"); 341 break; 342 } 343 } 344 } 345 346 /* 347 * Get a delimited string. P points to the delimiter of the string; d points 348 * to a buffer area. Newline and delimiter escapes are processed; other 349 * escapes are ignored. 350 * 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. 354 */ 355 static char * 356 compile_delimited(char *p, char *d) 357 { 358 char c; 359 360 c = *p++; 361 if (c == '\0') 362 return (NULL); 363 else if (c == '\\') 364 err(COMPILE, "\\ can not be used as a string delimiter"); 365 else if (c == '\n') 366 err(COMPILE, "newline can not be used as a string delimiter"); 367 while (*p) { 368 if (*p == '[') { 369 if ((d = compile_ccl(&p, d)) == NULL) 370 err(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 *d++ = *p++; 382 else if (*p == c) { 383 *d = '\0'; 384 return (p + 1); 385 } 386 *d++ = *p++; 387 } 388 return (NULL); 389 } 390 391 392 /* compile_ccl: expand a POSIX character class */ 393 static char * 394 compile_ccl(char **sp, char *t) 395 { 396 int c, d; 397 char *s = *sp; 398 399 *t++ = *s++; 400 if (*s == '^') 401 *t++ = *s++; 402 if (*s == ']') 403 *t++ = *s++; 404 for (; *s && (*t = *s) != ']'; s++, t++) 405 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) { 406 *++t = *++s, t++, s++; 407 for (c = *s; (*t = *s) != ']' || c != d; s++, t++) 408 if ((c = *s) == '\0') 409 return NULL; 410 } else if (*s == '\\' && s[1] == 'n') 411 *t = '\n', s++; 412 return (*s == ']') ? *sp = ++s, ++t : NULL; 413 } 414 415 /* 416 * Get a regular expression. P points to the delimiter of the regular 417 * expression; repp points to the address of a regexp pointer. Newline 418 * and delimiter escapes are processed; other escapes are ignored. 419 * Returns a pointer to the first character after the final delimiter 420 * or NULL in the case of a non terminated regular expression. The regexp 421 * pointer is set to the compiled regular expression. 422 * Cflags are passed to regcomp. 423 */ 424 static char * 425 compile_re(char *p, 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, ere)) != 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(char *p, struct s_subst *s) 450 { 451 static char lbuf[_POSIX2_LINE_MAX + 1]; 452 int asize, ref, size; 453 char c, *text, *op, *sp; 454 455 c = *p++; /* Terminator character */ 456 if (c == '\0') 457 return (NULL); 458 459 s->maxbref = 0; 460 s->linenum = linenum; 461 asize = 2 * _POSIX2_LINE_MAX + 1; 462 text = xmalloc(asize); 463 size = 0; 464 do { 465 op = sp = text + size; 466 for (; *p; p++) { 467 if (*p == '\\') { 468 p++; 469 if (strchr("123456789", *p) != NULL) { 470 *sp++ = '\\'; 471 ref = *p - '0'; 472 if (s->re != NULL && 473 ref > s->re->re_nsub) 474 err(COMPILE, 475 "\\%c not defined in the RE", *p); 476 if (s->maxbref < ref) 477 s->maxbref = ref; 478 } else if (*p == '&' || *p == '\\') 479 *sp++ = '\\'; 480 } else if (*p == c) { 481 p++; 482 *sp++ = '\0'; 483 size += sp - op; 484 s->new = xrealloc(text, size); 485 return (p); 486 } else if (*p == '\n') { 487 err(COMPILE, 488 "unescaped newline inside substitute pattern"); 489 /* NOTREACHED */ 490 } 491 *sp++ = *p; 492 } 493 size += sp - op; 494 if (asize - size < _POSIX2_LINE_MAX + 1) { 495 asize *= 2; 496 text = xmalloc(asize); 497 } 498 } while (cu_fgets(p = lbuf, sizeof(lbuf))); 499 err(COMPILE, "unterminated substitute in regular expression"); 500 /* NOTREACHED */ 501 return (NULL); 502 } 503 504 /* 505 * Compile the flags of the s command 506 */ 507 static char * 508 compile_flags(char *p, struct s_subst *s) 509 { 510 int gn; /* True if we have seen g or n */ 511 char wfile[_POSIX2_LINE_MAX + 1], *q; 512 513 s->n = 1; /* Default */ 514 s->p = 0; 515 s->wfile = NULL; 516 s->wfd = -1; 517 for (gn = 0;;) { 518 EATSPACE(); /* EXTENSION */ 519 switch (*p) { 520 case 'g': 521 if (gn) 522 err(COMPILE, 523 "more than one number or 'g' in substitute flags"); 524 gn = 1; 525 s->n = 0; 526 break; 527 case '\0': 528 case '\n': 529 case ';': 530 return (p); 531 case 'p': 532 s->p = 1; 533 break; 534 case '1': case '2': case '3': 535 case '4': case '5': case '6': 536 case '7': case '8': case '9': 537 if (gn) 538 err(COMPILE, 539 "more than one number or 'g' in substitute flags"); 540 gn = 1; 541 /* XXX Check for overflow */ 542 s->n = (int)strtol(p, &p, 10); 543 break; 544 case 'w': 545 p++; 546 #ifdef HISTORIC_PRACTICE 547 if (*p != ' ') { 548 err(WARNING, "space missing before w wfile"); 549 return (p); 550 } 551 #endif 552 EATSPACE(); 553 q = wfile; 554 while (*p) { 555 if (*p == '\n') 556 break; 557 *q++ = *p++; 558 } 559 *q = '\0'; 560 if (q == wfile) 561 err(COMPILE, "no wfile specified"); 562 s->wfile = strdup(wfile); 563 if (!aflag && (s->wfd = open(wfile, 564 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 565 DEFFILEMODE)) == -1) 566 err(FATAL, "%s: %s", wfile, strerror(errno)); 567 return (p); 568 default: 569 err(COMPILE, 570 "bad flag in substitute command: '%c'", *p); 571 break; 572 } 573 p++; 574 } 575 } 576 577 /* 578 * Compile a translation set of strings into a lookup table. 579 */ 580 static char * 581 compile_tr(char *p, char **transtab) 582 { 583 int i; 584 char *lt, *op, *np; 585 char old[_POSIX2_LINE_MAX + 1]; 586 char new[_POSIX2_LINE_MAX + 1]; 587 588 if (*p == '\0' || *p == '\\') 589 err(COMPILE, 590 "transform pattern can not be delimited by newline or backslash"); 591 p = compile_delimited(p, old); 592 if (p == NULL) { 593 err(COMPILE, "unterminated transform source string"); 594 return (NULL); 595 } 596 p = compile_delimited(--p, new); 597 if (p == NULL) { 598 err(COMPILE, "unterminated transform target string"); 599 return (NULL); 600 } 601 EATSPACE(); 602 if (strlen(new) != strlen(old)) { 603 err(COMPILE, "transform strings are not the same length"); 604 return (NULL); 605 } 606 /* We assume characters are 8 bits */ 607 lt = xmalloc(UCHAR_MAX); 608 for (i = 0; i <= UCHAR_MAX; i++) 609 lt[i] = (char)i; 610 for (op = old, np = new; *op; op++, np++) 611 lt[(u_char)*op] = *np; 612 *transtab = lt; 613 return (p); 614 } 615 616 /* 617 * Compile the text following an a or i command. 618 */ 619 static char * 620 compile_text(void) 621 { 622 int asize, size; 623 char *text, *p, *op, *s; 624 char lbuf[_POSIX2_LINE_MAX + 1]; 625 626 asize = 2 * _POSIX2_LINE_MAX + 1; 627 text = xmalloc(asize); 628 size = 0; 629 while (cu_fgets(lbuf, sizeof(lbuf))) { 630 op = s = text + size; 631 p = lbuf; 632 EATSPACE(); 633 for (; *p; p++) { 634 if (*p == '\\') 635 p++; 636 *s++ = *p; 637 } 638 size += s - op; 639 if (p[-2] != '\\') { 640 *s = '\0'; 641 break; 642 } 643 if (asize - size < _POSIX2_LINE_MAX + 1) { 644 asize *= 2; 645 text = xmalloc(asize); 646 } 647 } 648 return (xrealloc(text, size + 1)); 649 } 650 651 /* 652 * Get an address and return a pointer to the first character after 653 * it. Fill the structure pointed to according to the address. 654 */ 655 static char * 656 compile_addr(char *p, struct s_addr *a) 657 { 658 char *end; 659 660 switch (*p) { 661 case '\\': /* Context address */ 662 ++p; 663 /* FALLTHROUGH */ 664 case '/': /* Context address */ 665 p = compile_re(p, &a->u.r); 666 if (p == NULL) 667 err(COMPILE, "unterminated regular expression"); 668 a->type = AT_RE; 669 return (p); 670 671 case '$': /* Last line */ 672 a->type = AT_LAST; 673 return (p + 1); 674 /* Line number */ 675 case '0': case '1': case '2': case '3': case '4': 676 case '5': case '6': case '7': case '8': case '9': 677 a->type = AT_LINE; 678 a->u.l = strtol(p, &end, 10); 679 return (end); 680 default: 681 err(COMPILE, "expected context address"); 682 return (NULL); 683 } 684 } 685 686 /* 687 * duptoeol -- 688 * Return a copy of all the characters up to \n or \0. 689 */ 690 static char * 691 duptoeol(char *s, char *ctype) 692 { 693 size_t len; 694 int ws; 695 char *start; 696 697 ws = 0; 698 for (start = s; *s != '\0' && *s != '\n'; ++s) 699 ws = isspace((unsigned char)*s); 700 *s = '\0'; 701 if (ws) 702 err(WARNING, "whitespace after %s", ctype); 703 len = s - start + 1; 704 return (memmove(xmalloc(len), start, len)); 705 } 706 707 /* 708 * Convert goto label names to addresses, and count a and r commands, in 709 * the given subset of the script. Free the memory used by labels in b 710 * and t commands (but not by :). 711 * 712 * TODO: Remove } nodes 713 */ 714 static void 715 fixuplabel(struct s_command *cp, struct s_command *end) 716 { 717 718 for (; cp != end; cp = cp->next) 719 switch (cp->code) { 720 case 'a': 721 case 'r': 722 appendnum++; 723 break; 724 case 'b': 725 case 't': 726 /* Resolve branch target. */ 727 if (cp->t == NULL) { 728 cp->u.c = NULL; 729 break; 730 } 731 if ((cp->u.c = findlabel(cp->t)) == NULL) 732 err(COMPILE2, "undefined label '%s'", cp->t); 733 free(cp->t); 734 break; 735 case '{': 736 /* Do interior commands. */ 737 fixuplabel(cp->u.c, cp->next); 738 break; 739 } 740 } 741 742 /* 743 * Associate the given command label for later lookup. 744 */ 745 static void 746 enterlabel(struct s_command *cp) 747 { 748 struct labhash **lhp, *lh; 749 u_char *p; 750 u_int h, c; 751 752 for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++) 753 h = (h << 5) + h + c; 754 lhp = &labels[h & LHMASK]; 755 for (lh = *lhp; lh != NULL; lh = lh->lh_next) 756 if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0) 757 err(COMPILE2, "duplicate label '%s'", cp->t); 758 lh = xmalloc(sizeof *lh); 759 lh->lh_next = *lhp; 760 lh->lh_hash = h; 761 lh->lh_cmd = cp; 762 lh->lh_ref = 0; 763 *lhp = lh; 764 } 765 766 /* 767 * Find the label contained in the command l in the command linked 768 * list cp. L is excluded from the search. Return NULL if not found. 769 */ 770 static struct s_command * 771 findlabel(char *name) 772 { 773 struct labhash *lh; 774 u_char *p; 775 u_int h, c; 776 777 for (h = 0, p = (u_char *)name; (c = *p) != 0; p++) 778 h = (h << 5) + h + c; 779 for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) { 780 if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) { 781 lh->lh_ref = 1; 782 return (lh->lh_cmd); 783 } 784 } 785 return (NULL); 786 } 787 788 /* 789 * Warn about any unused labels. As a side effect, release the label hash 790 * table space. 791 */ 792 static void 793 uselabel(void) 794 { 795 struct labhash *lh, *next; 796 int i; 797 798 for (i = 0; i < LHSZ; i++) { 799 for (lh = labels[i]; lh != NULL; lh = next) { 800 next = lh->lh_next; 801 if (!lh->lh_ref) 802 err(WARNING, "unused label '%s'", 803 lh->lh_cmd->t); 804 free(lh); 805 } 806 } 807 } 808