1 /* 2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 3 * Copyright (c) 1992 Diomidis Spinellis. 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Diomidis Spinellis of Imperial College, University of London. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/types.h> 36 #include <sys/stat.h> 37 38 #include <ctype.h> 39 #include <err.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 #include <wchar.h> 48 #include <libintl.h> 49 #include <note.h> 50 51 #include "defs.h" 52 #include "extern.h" 53 54 #define LHSZ 128 55 #define LHMASK (LHSZ - 1) 56 static struct labhash { 57 struct labhash *lh_next; 58 uint_t lh_hash; 59 struct s_command *lh_cmd; 60 int lh_ref; 61 } *labels[LHSZ]; 62 63 static char *compile_addr(char *, struct s_addr *); 64 static char *compile_ccl(char **, char *); 65 static char *compile_delimited(char *, char *, int); 66 static char *compile_flags(char *, struct s_subst *); 67 static regex_t *compile_re(char *, int); 68 static char *compile_subst(char *, struct s_subst *); 69 static char *compile_text(void); 70 static char *compile_tr(char *, struct s_tr **); 71 static struct s_command 72 **compile_stream(struct s_command **); 73 static char *duptoeol(char *, const char *); 74 static void enterlabel(struct s_command *); 75 static struct s_command 76 *findlabel(char *); 77 static void fixuplabel(struct s_command *, struct s_command *); 78 static void uselabel(void); 79 80 /* 81 * Command specification. This is used to drive the command parser. 82 */ 83 struct s_format { 84 char code; /* Command code */ 85 int naddr; /* Number of address args */ 86 enum e_args args; /* Argument type */ 87 }; 88 89 static struct s_format cmd_fmts[] = { 90 {'{', 2, GROUP}, 91 {'}', 0, ENDGROUP}, 92 {'a', 1, TEXT}, 93 {'b', 2, BRANCH}, 94 {'c', 2, TEXT}, 95 {'d', 2, EMPTY}, 96 {'D', 2, EMPTY}, 97 {'g', 2, EMPTY}, 98 {'G', 2, EMPTY}, 99 {'h', 2, EMPTY}, 100 {'H', 2, EMPTY}, 101 {'i', 1, TEXT}, 102 {'l', 2, EMPTY}, 103 {'n', 2, EMPTY}, 104 {'N', 2, EMPTY}, 105 {'p', 2, EMPTY}, 106 {'P', 2, EMPTY}, 107 {'q', 1, EMPTY}, 108 {'r', 1, RFILE}, 109 {'s', 2, SUBST}, 110 {'t', 2, BRANCH}, 111 {'w', 2, WFILE}, 112 {'x', 2, EMPTY}, 113 {'y', 2, TR}, 114 {'!', 2, NONSEL}, 115 {':', 0, LABEL}, 116 {'#', 0, COMMENT}, 117 {'=', 1, EMPTY}, 118 {'\0', 0, COMMENT}, 119 }; 120 121 /* The compiled program. */ 122 struct s_command *prog; 123 124 /* 125 * Compile the program into prog. 126 * Initialise appends. 127 */ 128 void 129 compile(void) 130 { 131 *compile_stream(&prog) = NULL; 132 fixuplabel(prog, NULL); 133 uselabel(); 134 if (appendnum == 0) 135 appends = NULL; 136 else if ((appends = malloc(sizeof (struct s_appends) * appendnum)) == 137 NULL) 138 err(1, "malloc"); 139 if ((match = malloc((maxnsub + 1) * sizeof (regmatch_t))) == NULL) 140 err(1, "malloc"); 141 } 142 143 #define EATSPACE() do { \ 144 if (p) \ 145 while (*p && isspace((unsigned char)*p)) \ 146 p++; \ 147 _NOTE(CONSTCOND) \ 148 } while (0) 149 150 static struct s_command ** 151 compile_stream(struct s_command **link) 152 { 153 char *p; 154 static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ 155 struct s_command *cmd, *cmd2, *stack; 156 struct s_format *fp; 157 char re[_POSIX2_LINE_MAX + 1]; 158 int naddr; /* Number of addresses */ 159 160 stack = 0; 161 for (;;) { 162 if ((p = cu_fgets(lbuf, sizeof (lbuf), NULL)) == NULL) { 163 if (stack != 0) 164 fatal(_("unexpected EOF (pending }'s)")); 165 return (link); 166 } 167 168 semicolon: EATSPACE(); 169 if (p) { 170 if (*p == '#' || *p == '\0') 171 continue; 172 else if (*p == ';') { 173 p++; 174 goto semicolon; 175 } 176 } 177 if ((*link = cmd = malloc(sizeof (struct s_command))) == NULL) 178 err(1, "malloc"); 179 link = &cmd->next; 180 cmd->startline = cmd->nonsel = 0; 181 /* First parse the addresses */ 182 naddr = 0; 183 184 /* Valid characters to start an address */ 185 #define addrchar(c) (strchr("0123456789/\\$", (c))) 186 if (addrchar(*p)) { 187 naddr++; 188 if ((cmd->a1 = malloc(sizeof (struct s_addr))) == NULL) 189 err(1, "malloc"); 190 p = compile_addr(p, cmd->a1); 191 EATSPACE(); /* EXTENSION */ 192 if (*p == ',') { 193 p++; 194 EATSPACE(); /* EXTENSION */ 195 naddr++; 196 if ((cmd->a2 = malloc(sizeof (struct s_addr))) 197 == NULL) 198 err(1, "malloc"); 199 p = compile_addr(p, cmd->a2); 200 EATSPACE(); 201 } else 202 cmd->a2 = 0; 203 } else 204 cmd->a1 = cmd->a2 = 0; 205 206 nonsel: /* Now parse the command */ 207 if (!*p) 208 fatal(_("command expected")); 209 cmd->code = *p; 210 for (fp = cmd_fmts; fp->code; fp++) 211 if (fp->code == *p) 212 break; 213 if (!fp->code) 214 fatal(_("invalid command code %c"), *p); 215 if (naddr > fp->naddr) 216 fatal(_("command %c expects up to %d address(es), " 217 "found %d"), *p, fp->naddr, naddr); 218 switch (fp->args) { 219 case NONSEL: /* ! */ 220 p++; 221 EATSPACE(); 222 cmd->nonsel = 1; 223 goto nonsel; 224 case GROUP: /* { */ 225 p++; 226 EATSPACE(); 227 cmd->next = stack; 228 stack = cmd; 229 link = &cmd->u.c; 230 if (*p) 231 goto semicolon; 232 break; 233 case ENDGROUP: 234 /* 235 * Short-circuit command processing, since end of 236 * group is really just a noop. 237 */ 238 cmd->nonsel = 1; 239 if (stack == 0) 240 fatal(_("unexpected }")); 241 cmd2 = stack; 242 stack = cmd2->next; 243 cmd2->next = cmd; 244 /*FALLTHROUGH*/ 245 case EMPTY: /* d D g G h H l n N p P q x = \0 */ 246 p++; 247 EATSPACE(); 248 if (*p == ';') { 249 p++; 250 link = &cmd->next; 251 goto semicolon; 252 } 253 if (*p) 254 fatal(_("extra characters at the end of %c " 255 "command"), cmd->code); 256 break; 257 case TEXT: /* a c i */ 258 p++; 259 EATSPACE(); 260 if (*p != '\\') 261 fatal(_("command %c expects \\ " 262 "followed by text"), cmd->code); 263 p++; 264 EATSPACE(); 265 if (*p) 266 fatal(_("extra characters after \\ " 267 "at the end of %c command"), 268 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 fatal(_("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, 0666)) == -1) 283 err(1, "%s", p); 284 break; 285 case RFILE: /* r */ 286 p++; 287 EATSPACE(); 288 if (*p == '\0') 289 fatal(_("filename expected")); 290 else 291 cmd->t = duptoeol(p, "read command"); 292 break; 293 case BRANCH: /* b t */ 294 p++; 295 EATSPACE(); 296 if (*p == '\0') 297 cmd->t = NULL; 298 else 299 cmd->t = duptoeol(p, "branch"); 300 break; 301 case LABEL: /* : */ 302 p++; 303 EATSPACE(); 304 cmd->t = duptoeol(p, "label"); 305 if (strlen(p) == 0) 306 fatal(_("empty label")); 307 enterlabel(cmd); 308 break; 309 case SUBST: /* s */ 310 p++; 311 if (*p == '\0' || *p == '\\') 312 fatal(_("substitute pattern can not " 313 "be delimited by newline or backslash")); 314 if ((cmd->u.s = calloc(1, sizeof (struct s_subst))) == 315 NULL) 316 err(1, "malloc"); 317 p = compile_delimited(p, re, 0); 318 if (p == NULL) 319 fatal(_("unterminated substitute pattern")); 320 321 /* Compile RE with no case sensitivity temporarily */ 322 if (*re == '\0') 323 cmd->u.s->re = NULL; 324 else 325 cmd->u.s->re = compile_re(re, 0); 326 --p; 327 p = compile_subst(p, cmd->u.s); 328 p = compile_flags(p, cmd->u.s); 329 330 /* Recompile RE with case sens. from "I" flag if any */ 331 if (*re == '\0') 332 cmd->u.s->re = NULL; 333 else 334 cmd->u.s->re = compile_re(re, cmd->u.s->icase); 335 EATSPACE(); 336 if (*p == ';') { 337 p++; 338 link = &cmd->next; 339 goto semicolon; 340 } 341 break; 342 case TR: /* y */ 343 p++; 344 p = compile_tr(p, &cmd->u.y); 345 EATSPACE(); 346 if (*p == ';') { 347 p++; 348 link = &cmd->next; 349 goto semicolon; 350 } 351 if (*p) 352 fatal(_("extra text at the end of a " 353 "transform command")); 354 break; 355 } 356 } 357 } 358 359 /* 360 * Get a delimited string. P points to the delimeter of the string; d points 361 * to a buffer area. Newline and delimiter escapes are processed; other 362 * escapes are ignored. 363 * 364 * Returns a pointer to the first character after the final delimiter or NULL 365 * in the case of a non-terminated string. The character array d is filled 366 * with the processed string. 367 */ 368 static char * 369 compile_delimited(char *p, char *d, int is_tr) 370 { 371 char c; 372 373 c = *p++; 374 if (c == '\0') 375 return (NULL); 376 else if (c == '\\') 377 fatal(_("\\ can not be used as a string delimiter")); 378 else if (c == '\n') 379 fatal(_("newline can not be used as a string delimiter")); 380 while (*p) { 381 if (*p == '[' && *p != c) { 382 if ((d = compile_ccl(&p, d)) == NULL) 383 fatal(_("unbalanced brackets ([])")); 384 continue; 385 } else if (*p == '\\' && p[1] == '[') { 386 *d++ = *p++; 387 } else if (*p == '\\' && p[1] == c) { 388 p++; 389 } else if (*p == '\\' && 390 (p[1] == 'n' || p[1] == 'r' || p[1] == 't')) { 391 switch (p[1]) { 392 case 'n': 393 *d++ = '\n'; 394 break; 395 case 'r': 396 *d++ = '\r'; 397 break; 398 case 't': 399 *d++ = '\t'; 400 break; 401 } 402 p += 2; 403 continue; 404 } else if (*p == '\\' && p[1] == '\\') { 405 if (is_tr) 406 p++; 407 else 408 *d++ = *p++; 409 } else if (*p == c) { 410 *d = '\0'; 411 return (p + 1); 412 } 413 *d++ = *p++; 414 } 415 return (NULL); 416 } 417 418 419 /* compile_ccl: expand a POSIX character class */ 420 static char * 421 compile_ccl(char **sp, char *t) 422 { 423 int c, d; 424 char *s = *sp; 425 426 *t++ = *s++; 427 if (*s == '^') 428 *t++ = *s++; 429 if (*s == ']') 430 *t++ = *s++; 431 for (; *s && (*t = *s) != ']'; s++, t++) { 432 if (*s == '[' && 433 ((d = *(s+1)) == '.' || d == ':' || d == '=')) { 434 *++t = *++s, t++, s++; 435 for (c = *s; (*t = *s) != ']' || c != d; s++, t++) 436 if ((c = *s) == '\0') 437 return (NULL); 438 } else if (*s == '\\') { 439 switch (s[1]) { 440 case 'n': 441 *t = '\n'; 442 s++; 443 break; 444 case 'r': 445 *t = '\r'; 446 s++; 447 break; 448 case 't': 449 *t = '\t'; 450 s++; 451 break; 452 } 453 } 454 } 455 return ((*s == ']') ? *sp = ++s, ++t : NULL); 456 } 457 458 /* 459 * Compiles the regular expression in RE and returns a pointer to the compiled 460 * regular expression. 461 * Cflags are passed to regcomp. 462 */ 463 static regex_t * 464 compile_re(char *re, int case_insensitive) 465 { 466 regex_t *rep; 467 int eval, flags; 468 469 470 flags = rflags; 471 if (case_insensitive) 472 flags |= REG_ICASE; 473 if ((rep = malloc(sizeof (regex_t))) == NULL) 474 err(1, "malloc"); 475 if ((eval = regcomp(rep, re, flags)) != 0) 476 fatal(_("RE error: %s"), strregerror(eval, rep)); 477 if (maxnsub < rep->re_nsub) 478 maxnsub = rep->re_nsub; 479 return (rep); 480 } 481 482 /* 483 * Compile the substitution string of a regular expression and set res to 484 * point to a saved copy of it. Nsub is the number of parenthesized regular 485 * expressions. 486 */ 487 static char * 488 compile_subst(char *p, struct s_subst *s) 489 { 490 static char lbuf[_POSIX2_LINE_MAX + 1]; 491 int asize; 492 uintptr_t size; 493 uchar_t ref; 494 char c, *text, *op, *sp; 495 int more = 1, sawesc = 0; 496 497 c = *p++; /* Terminator character */ 498 if (c == '\0') 499 return (NULL); 500 501 s->maxbref = 0; 502 s->linenum = linenum; 503 asize = 2 * _POSIX2_LINE_MAX + 1; 504 if ((text = malloc(asize)) == NULL) 505 err(1, "malloc"); 506 size = 0; 507 do { 508 op = sp = text + size; 509 for (; *p; p++) { 510 if (*p == '\\' || sawesc) { 511 /* 512 * If this is a continuation from the last 513 * buffer, we won't have a character to 514 * skip over. 515 */ 516 if (sawesc) 517 sawesc = 0; 518 else 519 p++; 520 521 if (*p == '\0') { 522 /* 523 * This escaped character is continued 524 * in the next part of the line. Note 525 * this fact, then cause the loop to 526 * exit w/ normal EOL case and reenter 527 * above with the new buffer. 528 */ 529 sawesc = 1; 530 p--; 531 continue; 532 } else if (strchr("123456789", *p) != NULL) { 533 *sp++ = '\\'; 534 ref = *p - '0'; 535 if (s->re != NULL && 536 ref > s->re->re_nsub) 537 fatal(_("not defined in " 538 "the RE: \\%c"), *p); 539 if (s->maxbref < ref) 540 s->maxbref = ref; 541 } else { 542 switch (*p) { 543 case '&': 544 case '\\': 545 *sp++ = '\\'; 546 break; 547 case 'n': 548 *p = '\n'; 549 break; 550 case 'r': 551 *p = '\r'; 552 break; 553 case 't': 554 *p = '\t'; 555 break; 556 } 557 } 558 } else if (*p == c) { 559 if (*++p == '\0' && more) { 560 if (cu_fgets(lbuf, sizeof (lbuf), 561 &more)) 562 p = lbuf; 563 } 564 *sp++ = '\0'; 565 size += (uintptr_t)sp - (uintptr_t)op; 566 if ((s->new = realloc(text, size)) == NULL) 567 err(1, "realloc"); 568 return (p); 569 } else if (*p == '\n') { 570 fatal(_("unescaped newline inside " 571 "substitute pattern")); 572 /* NOTREACHED */ 573 } 574 *sp++ = *p; 575 } 576 size += (uintptr_t)sp - (uintptr_t)op; 577 if (asize - size < _POSIX2_LINE_MAX + 1) { 578 asize *= 2; 579 if ((text = realloc(text, asize)) == NULL) 580 err(1, "realloc"); 581 } 582 } while (cu_fgets(p = lbuf, sizeof (lbuf), &more)); 583 fatal(_("unterminated substitute in regular expression")); 584 return (NULL); 585 } 586 587 /* 588 * Compile the flags of the s command 589 */ 590 static char * 591 compile_flags(char *p, struct s_subst *s) 592 { 593 int gn; /* True if we have seen g or n */ 594 unsigned long nval; 595 char wfile[_POSIX2_LINE_MAX + 1], *q; 596 597 s->n = 1; /* Default */ 598 s->p = 0; 599 s->wfile = NULL; 600 s->wfd = -1; 601 s->icase = 0; 602 gn = 0; 603 for (;;) { 604 EATSPACE(); /* EXTENSION */ 605 switch (*p) { 606 case 'g': 607 if (gn) 608 fatal(_("more than one number or " 609 "'g' in substitute flags")); 610 gn = 1; 611 s->n = 0; 612 break; 613 case '\0': 614 case '\n': 615 case ';': 616 return (p); 617 case 'p': 618 s->p = 1; 619 break; 620 case 'I': 621 s->icase = 1; 622 break; 623 case '1': case '2': case '3': 624 case '4': case '5': case '6': 625 case '7': case '8': case '9': 626 if (gn) 627 fatal(_("more than one number or " 628 "'g' in substitute flags")); 629 gn = 1; 630 errno = 0; 631 nval = strtol(p, &p, 10); 632 if (errno == ERANGE || nval > INT_MAX) 633 fatal(_("overflow in the 'N' substitute flag")); 634 s->n = nval; 635 p--; 636 break; 637 case 'w': 638 p++; 639 #ifdef HISTORIC_PRACTICE 640 if (*p != ' ') { 641 fatal(_("space missing before w wfile")); 642 return (p); 643 } 644 #endif 645 EATSPACE(); 646 q = wfile; 647 while (*p) { 648 if (*p == '\n') 649 break; 650 *q++ = *p++; 651 } 652 *q = '\0'; 653 if (q == wfile) 654 fatal(_("no wfile specified")); 655 s->wfile = strdup(wfile); 656 if (!aflag && (s->wfd = open(wfile, 657 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 0666)) == -1) 658 err(1, "%s", wfile); 659 return (p); 660 default: 661 fatal(_("bad flag in substitute command: '%c'"), *p); 662 break; 663 } 664 p++; 665 } 666 } 667 668 /* 669 * Compile a translation set of strings into a lookup table. 670 */ 671 static char * 672 compile_tr(char *p, struct s_tr **py) 673 { 674 struct s_tr *y; 675 int i; 676 const char *op, *np; 677 char old[_POSIX2_LINE_MAX + 1]; 678 char new[_POSIX2_LINE_MAX + 1]; 679 size_t oclen, oldlen, nclen, newlen; 680 mbstate_t mbs1, mbs2; 681 682 if ((*py = y = malloc(sizeof (*y))) == NULL) 683 err(1, NULL); 684 y->multis = NULL; 685 y->nmultis = 0; 686 687 if (*p == '\0' || *p == '\\') 688 fatal(_("transform pattern can not be delimited by " 689 "newline or backslash")); 690 p = compile_delimited(p, old, 1); 691 if (p == NULL) 692 fatal(_("unterminated transform source string")); 693 p = compile_delimited(p - 1, new, 1); 694 if (p == NULL) 695 fatal(_("unterminated transform target string")); 696 EATSPACE(); 697 op = old; 698 oldlen = mbsrtowcs(NULL, &op, 0, NULL); 699 if (oldlen == (size_t)-1) 700 err(1, NULL); 701 np = new; 702 newlen = mbsrtowcs(NULL, &np, 0, NULL); 703 if (newlen == (size_t)-1) 704 err(1, NULL); 705 if (newlen != oldlen) 706 fatal(_("transform strings are not the same length")); 707 if (MB_CUR_MAX == 1) { 708 /* 709 * The single-byte encoding case is easy: generate a 710 * lookup table. 711 */ 712 for (i = 0; i <= UCHAR_MAX; i++) 713 y->bytetab[i] = (char)i; 714 for (; *op; op++, np++) 715 y->bytetab[(uchar_t)*op] = *np; 716 } else { 717 /* 718 * Multi-byte encoding case: generate a lookup table as 719 * above, but only for single-byte characters. The first 720 * bytes of multi-byte characters have their lookup table 721 * entries set to 0, which causes do_tr() to search through 722 * an auxiliary vector of multi-byte mappings. 723 */ 724 (void) memset(&mbs1, 0, sizeof (mbs1)); 725 (void) memset(&mbs2, 0, sizeof (mbs2)); 726 for (i = 0; i <= UCHAR_MAX; i++) 727 y->bytetab[i] = (btowc(i) != WEOF) ? (uchar_t)i : 0; 728 while (*op != '\0') { 729 oclen = mbrlen(op, MB_LEN_MAX, &mbs1); 730 if (oclen == (size_t)-1 || oclen == (size_t)-2) 731 errx(1, "%s", strerror(EILSEQ)); 732 nclen = mbrlen(np, MB_LEN_MAX, &mbs2); 733 if (nclen == (size_t)-1 || nclen == (size_t)-2) 734 errx(1, "%s", strerror(EILSEQ)); 735 if (oclen == 1 && nclen == 1) 736 y->bytetab[(uchar_t)*op] = *np; 737 else { 738 y->bytetab[(uchar_t)*op] = 0; 739 y->multis = realloc(y->multis, 740 (y->nmultis + 1) * sizeof (*y->multis)); 741 if (y->multis == NULL) 742 err(1, NULL); 743 i = y->nmultis++; 744 y->multis[i].fromlen = oclen; 745 (void) memcpy(y->multis[i].from, op, oclen); 746 y->multis[i].tolen = nclen; 747 (void) memcpy(y->multis[i].to, np, nclen); 748 } 749 op += oclen; 750 np += nclen; 751 } 752 } 753 return (p); 754 } 755 756 /* 757 * Compile the text following an a or i command. 758 */ 759 static char * 760 compile_text(void) 761 { 762 int esc_nl; 763 uintptr_t size, asize; 764 char *text, *p, *op, *s; 765 char lbuf[_POSIX2_LINE_MAX + 1]; 766 767 asize = 2 * _POSIX2_LINE_MAX + 1; 768 if ((text = malloc(asize)) == NULL) 769 err(1, "malloc"); 770 size = 0; 771 while (cu_fgets(lbuf, sizeof (lbuf), NULL)) { 772 op = s = text + size; 773 p = lbuf; 774 EATSPACE(); 775 for (esc_nl = 0; *p != '\0'; p++) { 776 if (*p == '\\' && p[1] != '\0' && *++p == '\n') 777 esc_nl = 1; 778 *s++ = *p; 779 } 780 size += (uintptr_t)s - (uintptr_t)op; 781 if (!esc_nl) { 782 *s = '\0'; 783 break; 784 } 785 if (asize - size < _POSIX2_LINE_MAX + 1) { 786 asize *= 2; 787 if ((text = realloc(text, asize)) == NULL) 788 err(1, "realloc"); 789 } 790 } 791 text[size] = '\0'; 792 if ((p = realloc(text, size + 1)) == NULL) 793 err(1, "realloc"); 794 return (p); 795 } 796 797 /* 798 * Get an address and return a pointer to the first character after 799 * it. Fill the structure pointed to according to the address. 800 */ 801 static char * 802 compile_addr(char *p, struct s_addr *a) 803 { 804 char *end, re[_POSIX2_LINE_MAX + 1]; 805 int icase; 806 807 icase = 0; 808 809 a->type = 0; 810 switch (*p) { 811 case '\\': /* Context address */ 812 ++p; 813 /* FALLTHROUGH */ 814 case '/': /* Context address */ 815 p = compile_delimited(p, re, 0); 816 if (p == NULL) 817 fatal(_("unterminated regular expression")); 818 819 /* Check for case insensitive regexp flag */ 820 if (*p == 'I') { 821 icase = 1; 822 p++; 823 } 824 if (*re == '\0') 825 a->u.r = NULL; 826 else 827 a->u.r = compile_re(re, icase); 828 a->type = AT_RE; 829 return (p); 830 831 case '$': /* Last line */ 832 a->type = AT_LAST; 833 return (p + 1); 834 835 case '+': /* Relative line number */ 836 a->type = AT_RELLINE; 837 p++; 838 /* FALLTHROUGH */ 839 /* Line number */ 840 case '0': case '1': case '2': case '3': case '4': 841 case '5': case '6': case '7': case '8': case '9': 842 if (a->type == 0) 843 a->type = AT_LINE; 844 a->u.l = strtol(p, &end, 10); 845 return (end); 846 default: 847 fatal(_("expected context address")); 848 return (NULL); 849 } 850 } 851 852 /* 853 * duptoeol -- 854 * Return a copy of all the characters up to \n or \0. 855 */ 856 static char * 857 duptoeol(char *s, const char *ctype) 858 { 859 size_t len; 860 int ws; 861 char *p, *start; 862 863 ws = 0; 864 for (start = s; *s != '\0' && *s != '\n'; ++s) 865 ws = isspace((unsigned char)*s); 866 *s = '\0'; 867 if (ws) 868 warnx(_("%lu: %s: whitespace after %s"), linenum, fname, ctype); 869 len = (uintptr_t)s - (uintptr_t)start + 1; 870 if ((p = malloc(len)) == NULL) 871 err(1, "malloc"); 872 return (memmove(p, start, len)); 873 } 874 875 /* 876 * Convert goto label names to addresses, and count a and r commands, in 877 * the given subset of the script. Free the memory used by labels in b 878 * and t commands (but not by :). 879 * 880 * TODO: Remove } nodes 881 */ 882 static void 883 fixuplabel(struct s_command *cp, struct s_command *end) 884 { 885 886 for (; cp != end; cp = cp->next) 887 switch (cp->code) { 888 case 'a': 889 case 'r': 890 appendnum++; 891 break; 892 case 'b': 893 case 't': 894 /* Resolve branch target. */ 895 if (cp->t == NULL) { 896 cp->u.c = NULL; 897 break; 898 } 899 if ((cp->u.c = findlabel(cp->t)) == NULL) 900 fatal(_("undefined label '%s'"), cp->t); 901 free(cp->t); 902 break; 903 case '{': 904 /* Do interior commands. */ 905 fixuplabel(cp->u.c, cp->next); 906 break; 907 } 908 } 909 910 /* 911 * Associate the given command label for later lookup. 912 */ 913 static void 914 enterlabel(struct s_command *cp) 915 { 916 struct labhash **lhp, *lh; 917 uchar_t *p; 918 uint_t h, c; 919 920 for (h = 0, p = (uchar_t *)cp->t; (c = *p) != 0; p++) 921 h = (h << 5) + h + c; 922 lhp = &labels[h & LHMASK]; 923 for (lh = *lhp; lh != NULL; lh = lh->lh_next) 924 if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0) 925 fatal(_("duplicate label '%s'"), cp->t); 926 if ((lh = malloc(sizeof (*lh))) == NULL) 927 err(1, "malloc"); 928 lh->lh_next = *lhp; 929 lh->lh_hash = h; 930 lh->lh_cmd = cp; 931 lh->lh_ref = 0; 932 *lhp = lh; 933 } 934 935 /* 936 * Find the label contained in the command l in the command linked 937 * list cp. L is excluded from the search. Return NULL if not found. 938 */ 939 static struct s_command * 940 findlabel(char *name) 941 { 942 struct labhash *lh; 943 uchar_t *p; 944 uint_t h, c; 945 946 for (h = 0, p = (uchar_t *)name; (c = *p) != 0; p++) 947 h = (h << 5) + h + c; 948 for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) { 949 if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) { 950 lh->lh_ref = 1; 951 return (lh->lh_cmd); 952 } 953 } 954 return (NULL); 955 } 956 957 /* 958 * Warn about any unused labels. As a side effect, release the label hash 959 * table space. 960 */ 961 static void 962 uselabel(void) 963 { 964 struct labhash *lh, *next; 965 int i; 966 967 for (i = 0; i < LHSZ; i++) { 968 for (lh = labels[i]; lh != NULL; lh = next) { 969 next = lh->lh_next; 970 if (!lh->lh_ref) 971 warnx(_("%lu: %s: unused label '%s'"), 972 linenum, fname, lh->lh_cmd->t); 973 free(lh); 974 } 975 } 976 } 977