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