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