compile.c revision 276099
150397Sobrien/*- 2169689Skan * Copyright (c) 1992 Diomidis Spinellis. 390075Sobrien * Copyright (c) 1992, 1993 4122180Skan * The Regents of the University of California. All rights reserved. 550397Sobrien * 6132718Skan * This code is derived from software contributed to Berkeley by 750397Sobrien * Diomidis Spinellis of Imperial College, University of London. 8132718Skan * 950397Sobrien * Redistribution and use in source and binary forms, with or without 1050397Sobrien * modification, are permitted provided that the following conditions 1150397Sobrien * are met: 1250397Sobrien * 1. Redistributions of source code must retain the above copyright 13132718Skan * notice, this list of conditions and the following disclaimer. 1450397Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1550397Sobrien * notice, this list of conditions and the following disclaimer in the 1650397Sobrien * documentation and/or other materials provided with the distribution. 1750397Sobrien * 4. Neither the name of the University nor the names of its contributors 1850397Sobrien * may be used to endorse or promote products derived from this software 19132718Skan * without specific prior written permission. 20169689Skan * 21169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2250397Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23122180Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2450397Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2550397Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2690075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2750397Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2850397Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2990075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3050397Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3150397Sobrien * SUCH DAMAGE. 32122180Skan */ 33122180Skan 34122180Skan#include <sys/cdefs.h> 35122180Skan__FBSDID("$FreeBSD: stable/10/usr.bin/sed/compile.c 276099 2014-12-23 02:46:00Z pfg $"); 36122180Skan 37122180Skan#ifndef lint 38122180Skanstatic const char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93"; 39122180Skan#endif 40122180Skan 41122180Skan#include <sys/types.h> 42122180Skan#include <sys/stat.h> 43122180Skan 44122180Skan#include <ctype.h> 45122180Skan#include <err.h> 4650397Sobrien#include <errno.h> 47122180Skan#include <fcntl.h> 48122180Skan#include <limits.h> 49122180Skan#include <regex.h> 5050397Sobrien#include <stdio.h> 5150397Sobrien#include <stdlib.h> 5250397Sobrien#include <string.h> 5350397Sobrien#include <wchar.h> 5450397Sobrien 5550397Sobrien#include "defs.h" 5650397Sobrien#include "extern.h" 57122180Skan 5850397Sobrien#define LHSZ 128 5950397Sobrien#define LHMASK (LHSZ - 1) 60122180Skanstatic struct labhash { 6150397Sobrien struct labhash *lh_next; 62122180Skan u_int lh_hash; 63122180Skan struct s_command *lh_cmd; 64117395Skan int lh_ref; 65117395Skan} *labels[LHSZ]; 66122180Skan 6750397Sobrienstatic char *compile_addr(char *, struct s_addr *); 68122180Skanstatic char *compile_ccl(char **, char *); 69122180Skanstatic char *compile_delimited(char *, char *, int); 7050397Sobrienstatic char *compile_flags(char *, struct s_subst *); 7150397Sobrienstatic regex_t *compile_re(char *, int); 72122180Skanstatic char *compile_subst(char *, struct s_subst *); 7350397Sobrienstatic char *compile_text(void); 7450397Sobrienstatic char *compile_tr(char *, struct s_tr **); 7550397Sobrienstatic struct s_command 7650397Sobrien **compile_stream(struct s_command **); 7750397Sobrienstatic char *duptoeol(char *, const char *); 7850397Sobrienstatic void enterlabel(struct s_command *); 7950397Sobrienstatic struct s_command 8050397Sobrien *findlabel(char *); 8150397Sobrienstatic void fixuplabel(struct s_command *, struct s_command *); 8250397Sobrienstatic void uselabel(void); 8350397Sobrien 8450397Sobrien/* 8550397Sobrien * Command specification. This is used to drive the command parser. 8650397Sobrien */ 8750397Sobrienstruct s_format { 8890075Sobrien char code; /* Command code */ 8990075Sobrien int naddr; /* Number of address args */ 9050397Sobrien enum e_args args; /* Argument type */ 9150397Sobrien}; 9250397Sobrien 9350397Sobrienstatic struct s_format cmd_fmts[] = { 9450397Sobrien {'{', 2, GROUP}, 9550397Sobrien {'}', 0, ENDGROUP}, 9650397Sobrien {'a', 1, TEXT}, 9750397Sobrien {'b', 2, BRANCH}, 98122180Skan {'c', 2, TEXT}, 9950397Sobrien {'d', 2, EMPTY}, 10050397Sobrien {'D', 2, EMPTY}, 10150397Sobrien {'g', 2, EMPTY}, 10250397Sobrien {'G', 2, EMPTY}, 103122180Skan {'h', 2, EMPTY}, 10450397Sobrien {'H', 2, EMPTY}, 10550397Sobrien {'i', 1, TEXT}, 10650397Sobrien {'l', 2, EMPTY}, 10750397Sobrien {'n', 2, EMPTY}, 10850397Sobrien {'N', 2, EMPTY}, 10950397Sobrien {'p', 2, EMPTY}, 11050397Sobrien {'P', 2, EMPTY}, 11150397Sobrien {'q', 1, EMPTY}, 11250397Sobrien {'r', 1, RFILE}, 11350397Sobrien {'s', 2, SUBST}, 11450397Sobrien {'t', 2, BRANCH}, 11550397Sobrien {'w', 2, WFILE}, 11650397Sobrien {'x', 2, EMPTY}, 117122180Skan {'y', 2, TR}, 11850397Sobrien {'!', 2, NONSEL}, 11950397Sobrien {':', 0, LABEL}, 12050397Sobrien {'#', 0, COMMENT}, 12150397Sobrien {'=', 1, EMPTY}, 12250397Sobrien {'\0', 0, COMMENT}, 12350397Sobrien}; 12450397Sobrien 12550397Sobrien/* The compiled program. */ 12650397Sobrienstruct s_command *prog; 12750397Sobrien 12850397Sobrien/* 12950397Sobrien * Compile the program into prog. 13050397Sobrien * Initialise appends. 13150397Sobrien */ 13250397Sobrienvoid 133122180Skancompile(void) 134122180Skan{ 135122180Skan *compile_stream(&prog) = NULL; 136122180Skan fixuplabel(prog, NULL); 137122180Skan uselabel(); 138122180Skan if (appendnum == 0) 139122180Skan appends = NULL; 140122180Skan else if ((appends = malloc(sizeof(struct s_appends) * appendnum)) == 141122180Skan NULL) 142122180Skan err(1, "malloc"); 143122180Skan if ((match = malloc((maxnsub + 1) * sizeof(regmatch_t))) == NULL) 144122180Skan err(1, "malloc"); 145122180Skan} 146132718Skan 147122180Skan#define EATSPACE() do { \ 148122180Skan if (p) \ 149122180Skan while (*p && isspace((unsigned char)*p)) \ 150122180Skan p++; \ 151122180Skan } while (0) 152122180Skan 153122180Skanstatic struct s_command ** 15450397Sobriencompile_stream(struct s_command **link) 15550397Sobrien{ 156122180Skan char *p; 157122180Skan static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ 158122180Skan struct s_command *cmd, *cmd2, *stack; 159122180Skan struct s_format *fp; 160122180Skan char re[_POSIX2_LINE_MAX + 1]; 161122180Skan int naddr; /* Number of addresses */ 162122180Skan 163122180Skan stack = 0; 164122180Skan for (;;) { 165122180Skan if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) { 166122180Skan if (stack != 0) 167122180Skan errx(1, "%lu: %s: unexpected EOF (pending }'s)", 168122180Skan linenum, fname); 169122180Skan return (link); 17050397Sobrien } 17150397Sobrien 17250397Sobriensemicolon: EATSPACE(); 17350397Sobrien if (p) { 174122180Skan if (*p == '#' || *p == '\0') 17550397Sobrien continue; 17650397Sobrien else if (*p == ';') { 177122180Skan p++; 178122180Skan goto semicolon; 17950397Sobrien } 18050397Sobrien } 181122180Skan if ((*link = cmd = malloc(sizeof(struct s_command))) == NULL) 182122180Skan err(1, "malloc"); 183122180Skan link = &cmd->next; 184122180Skan cmd->startline = cmd->nonsel = 0; 185122180Skan /* First parse the addresses */ 18650397Sobrien naddr = 0; 18750397Sobrien 18850397Sobrien/* Valid characters to start an address */ 189122180Skan#define addrchar(c) (strchr("0123456789/\\$", (c))) 190122180Skan if (addrchar(*p)) { 191122180Skan naddr++; 19250397Sobrien if ((cmd->a1 = malloc(sizeof(struct s_addr))) == NULL) 193122180Skan err(1, "malloc"); 194122180Skan p = compile_addr(p, cmd->a1); 195122180Skan EATSPACE(); /* EXTENSION */ 19650397Sobrien if (*p == ',') { 19750397Sobrien p++; 198122180Skan EATSPACE(); /* EXTENSION */ 199122180Skan naddr++; 200122180Skan if ((cmd->a2 = malloc(sizeof(struct s_addr))) 201122180Skan == NULL) 202122180Skan err(1, "malloc"); 203122180Skan p = compile_addr(p, cmd->a2); 204122180Skan EATSPACE(); 205122180Skan } else 20650397Sobrien cmd->a2 = 0; 20750397Sobrien } else 20850397Sobrien cmd->a1 = cmd->a2 = 0; 209122180Skan 21050397Sobriennonsel: /* Now parse the command */ 211122180Skan if (!*p) 212122180Skan errx(1, "%lu: %s: command expected", linenum, fname); 213122180Skan cmd->code = *p; 214122180Skan for (fp = cmd_fmts; fp->code; fp++) 215122180Skan if (fp->code == *p) 216122180Skan break; 217122180Skan if (!fp->code) 218122180Skan errx(1, "%lu: %s: invalid command code %c", linenum, fname, *p); 219122180Skan if (naddr > fp->naddr) 220122180Skan errx(1, 221122180Skan "%lu: %s: command %c expects up to %d address(es), found %d", 222122180Skan linenum, fname, *p, fp->naddr, naddr); 223122180Skan switch (fp->args) { 224122180Skan case NONSEL: /* ! */ 225117395Skan p++; 22650397Sobrien EATSPACE(); 22750397Sobrien cmd->nonsel = 1; 228117395Skan goto nonsel; 229122180Skan case GROUP: /* { */ 23050397Sobrien p++; 23150397Sobrien EATSPACE(); 232117395Skan cmd->next = stack; 23350397Sobrien stack = cmd; 23450397Sobrien link = &cmd->u.c; 23550397Sobrien if (*p) 23650397Sobrien goto semicolon; 23750397Sobrien break; 23850397Sobrien case ENDGROUP: 23950397Sobrien /* 24050397Sobrien * Short-circuit command processing, since end of 24150397Sobrien * group is really just a noop. 24250397Sobrien */ 24350397Sobrien cmd->nonsel = 1; 24450397Sobrien if (stack == 0) 24550397Sobrien errx(1, "%lu: %s: unexpected }", linenum, fname); 24650397Sobrien cmd2 = stack; 24750397Sobrien stack = cmd2->next; 24850397Sobrien cmd2->next = cmd; 24950397Sobrien /*FALLTHROUGH*/ 25050397Sobrien case EMPTY: /* d D g G h H l n N p P q x = \0 */ 251117395Skan p++; 25250397Sobrien EATSPACE(); 25350397Sobrien if (*p == ';') { 25450397Sobrien p++; 255122180Skan link = &cmd->next; 256169689Skan goto semicolon; 25750397Sobrien } 25850397Sobrien if (*p) 259122180Skan errx(1, "%lu: %s: extra characters at the end of %c command", 260122180Skan linenum, fname, cmd->code); 261122180Skan break; 262122180Skan case TEXT: /* a c i */ 26350397Sobrien p++; 26450397Sobrien EATSPACE(); 26550397Sobrien if (*p != '\\') 26650397Sobrien errx(1, 26750397Sobrien"%lu: %s: command %c expects \\ followed by text", linenum, fname, cmd->code); 26850397Sobrien p++; 269122180Skan EATSPACE(); 270122180Skan if (*p) 271122180Skan errx(1, 272122180Skan "%lu: %s: extra characters after \\ at the end of %c command", 273132718Skan linenum, fname, cmd->code); 27450397Sobrien cmd->t = compile_text(); 27550397Sobrien break; 27650397Sobrien case COMMENT: /* \0 # */ 277122180Skan break; 27850397Sobrien case WFILE: /* w */ 27990075Sobrien p++; 28090075Sobrien EATSPACE(); 28190075Sobrien if (*p == '\0') 28290075Sobrien errx(1, "%lu: %s: filename expected", linenum, fname); 28390075Sobrien cmd->t = duptoeol(p, "w command"); 28490075Sobrien if (aflag) 28590075Sobrien cmd->u.fd = -1; 28690075Sobrien else if ((cmd->u.fd = open(p, 28790075Sobrien O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 28890075Sobrien DEFFILEMODE)) == -1) 28990075Sobrien err(1, "%s", p); 29090075Sobrien break; 29190075Sobrien case RFILE: /* r */ 29290075Sobrien p++; 29390075Sobrien EATSPACE(); 29490075Sobrien if (*p == '\0') 29590075Sobrien errx(1, "%lu: %s: filename expected", linenum, fname); 29690075Sobrien else 29790075Sobrien cmd->t = duptoeol(p, "read command"); 29890075Sobrien break; 29990075Sobrien case BRANCH: /* b t */ 30090075Sobrien p++; 30190075Sobrien EATSPACE(); 30290075Sobrien if (*p == '\0') 30390075Sobrien cmd->t = NULL; 30490075Sobrien else 30590075Sobrien cmd->t = duptoeol(p, "branch"); 30690075Sobrien break; 30790075Sobrien case LABEL: /* : */ 30890075Sobrien p++; 30990075Sobrien EATSPACE(); 31090075Sobrien cmd->t = duptoeol(p, "label"); 31190075Sobrien if (strlen(p) == 0) 312 errx(1, "%lu: %s: empty label", linenum, fname); 313 enterlabel(cmd); 314 break; 315 case SUBST: /* s */ 316 p++; 317 if (*p == '\0' || *p == '\\') 318 errx(1, 319"%lu: %s: substitute pattern can not be delimited by newline or backslash", 320 linenum, fname); 321 if ((cmd->u.s = calloc(1, sizeof(struct s_subst))) == NULL) 322 err(1, "malloc"); 323 p = compile_delimited(p, re, 0); 324 if (p == NULL) 325 errx(1, 326 "%lu: %s: unterminated substitute pattern", linenum, fname); 327 328 /* Compile RE with no case sensitivity temporarily */ 329 if (*re == '\0') 330 cmd->u.s->re = NULL; 331 else 332 cmd->u.s->re = compile_re(re, 0); 333 --p; 334 p = compile_subst(p, cmd->u.s); 335 p = compile_flags(p, cmd->u.s); 336 337 /* Recompile RE with case sensitivity from "I" flag if any */ 338 if (*re == '\0') 339 cmd->u.s->re = NULL; 340 else 341 cmd->u.s->re = compile_re(re, cmd->u.s->icase); 342 EATSPACE(); 343 if (*p == ';') { 344 p++; 345 link = &cmd->next; 346 goto semicolon; 347 } 348 break; 349 case TR: /* y */ 350 p++; 351 p = compile_tr(p, &cmd->u.y); 352 EATSPACE(); 353 if (*p == ';') { 354 p++; 355 link = &cmd->next; 356 goto semicolon; 357 } 358 if (*p) 359 errx(1, 360"%lu: %s: extra text at the end of a transform command", linenum, fname); 361 break; 362 } 363 } 364} 365 366/* 367 * Get a delimited string. P points to the delimeter of the string; d points 368 * to a buffer area. Newline and delimiter escapes are processed; other 369 * escapes are ignored. 370 * 371 * Returns a pointer to the first character after the final delimiter or NULL 372 * in the case of a non-terminated string. The character array d is filled 373 * with the processed string. 374 */ 375static char * 376compile_delimited(char *p, char *d, int is_tr) 377{ 378 char c; 379 380 c = *p++; 381 if (c == '\0') 382 return (NULL); 383 else if (c == '\\') 384 errx(1, "%lu: %s: \\ can not be used as a string delimiter", 385 linenum, fname); 386 else if (c == '\n') 387 errx(1, "%lu: %s: newline can not be used as a string delimiter", 388 linenum, fname); 389 while (*p) { 390 if (*p == '[' && *p != c) { 391 if ((d = compile_ccl(&p, d)) == NULL) 392 errx(1, "%lu: %s: unbalanced brackets ([])", linenum, fname); 393 continue; 394 } else if (*p == '\\' && p[1] == '[') { 395 *d++ = *p++; 396 } else if (*p == '\\' && p[1] == c) 397 p++; 398 else if (*p == '\\' && p[1] == 'n') { 399 *d++ = '\n'; 400 p += 2; 401 continue; 402 } else if (*p == '\\' && p[1] == '\\') { 403 if (is_tr) 404 p++; 405 else 406 *d++ = *p++; 407 } else if (*p == c) { 408 *d = '\0'; 409 return (p + 1); 410 } 411 *d++ = *p++; 412 } 413 return (NULL); 414} 415 416 417/* compile_ccl: expand a POSIX character class */ 418static char * 419compile_ccl(char **sp, char *t) 420{ 421 int c, d; 422 char *s = *sp; 423 424 *t++ = *s++; 425 if (*s == '^') 426 *t++ = *s++; 427 if (*s == ']') 428 *t++ = *s++; 429 for (; *s && (*t = *s) != ']'; s++, t++) 430 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) { 431 *++t = *++s, t++, s++; 432 for (c = *s; (*t = *s) != ']' || c != d; s++, t++) 433 if ((c = *s) == '\0') 434 return NULL; 435 } 436 return (*s == ']') ? *sp = ++s, ++t : NULL; 437} 438 439/* 440 * Compiles the regular expression in RE and returns a pointer to the compiled 441 * regular expression. 442 * Cflags are passed to regcomp. 443 */ 444static regex_t * 445compile_re(char *re, int case_insensitive) 446{ 447 regex_t *rep; 448 int eval, flags; 449 450 451 flags = rflags; 452 if (case_insensitive) 453 flags |= REG_ICASE; 454 if ((rep = malloc(sizeof(regex_t))) == NULL) 455 err(1, "malloc"); 456 if ((eval = regcomp(rep, re, flags)) != 0) 457 errx(1, "%lu: %s: RE error: %s", 458 linenum, fname, strregerror(eval, rep)); 459 if (maxnsub < rep->re_nsub) 460 maxnsub = rep->re_nsub; 461 return (rep); 462} 463 464/* 465 * Compile the substitution string of a regular expression and set res to 466 * point to a saved copy of it. Nsub is the number of parenthesized regular 467 * expressions. 468 */ 469static char * 470compile_subst(char *p, struct s_subst *s) 471{ 472 static char lbuf[_POSIX2_LINE_MAX + 1]; 473 int asize, size; 474 u_char ref; 475 char c, *text, *op, *sp; 476 int more = 1, sawesc = 0; 477 478 c = *p++; /* Terminator character */ 479 if (c == '\0') 480 return (NULL); 481 482 s->maxbref = 0; 483 s->linenum = linenum; 484 asize = 2 * _POSIX2_LINE_MAX + 1; 485 if ((text = malloc(asize)) == NULL) 486 err(1, "malloc"); 487 size = 0; 488 do { 489 op = sp = text + size; 490 for (; *p; p++) { 491 if (*p == '\\' || sawesc) { 492 /* 493 * If this is a continuation from the last 494 * buffer, we won't have a character to 495 * skip over. 496 */ 497 if (sawesc) 498 sawesc = 0; 499 else 500 p++; 501 502 if (*p == '\0') { 503 /* 504 * This escaped character is continued 505 * in the next part of the line. Note 506 * this fact, then cause the loop to 507 * exit w/ normal EOL case and reenter 508 * above with the new buffer. 509 */ 510 sawesc = 1; 511 p--; 512 continue; 513 } else if (strchr("123456789", *p) != NULL) { 514 *sp++ = '\\'; 515 ref = *p - '0'; 516 if (s->re != NULL && 517 ref > s->re->re_nsub) 518 errx(1, "%lu: %s: \\%c not defined in the RE", 519 linenum, fname, *p); 520 if (s->maxbref < ref) 521 s->maxbref = ref; 522 } else if (*p == '&' || *p == '\\') 523 *sp++ = '\\'; 524 } else if (*p == c) { 525 if (*++p == '\0' && more) { 526 if (cu_fgets(lbuf, sizeof(lbuf), &more)) 527 p = lbuf; 528 } 529 *sp++ = '\0'; 530 size += sp - op; 531 if ((s->new = realloc(text, size)) == NULL) 532 err(1, "realloc"); 533 return (p); 534 } else if (*p == '\n') { 535 errx(1, 536"%lu: %s: unescaped newline inside substitute pattern", linenum, fname); 537 /* NOTREACHED */ 538 } 539 *sp++ = *p; 540 } 541 size += sp - op; 542 if (asize - size < _POSIX2_LINE_MAX + 1) { 543 asize *= 2; 544 if ((text = realloc(text, asize)) == NULL) 545 err(1, "realloc"); 546 } 547 } while (cu_fgets(p = lbuf, sizeof(lbuf), &more)); 548 errx(1, "%lu: %s: unterminated substitute in regular expression", 549 linenum, fname); 550 /* NOTREACHED */ 551} 552 553/* 554 * Compile the flags of the s command 555 */ 556static char * 557compile_flags(char *p, struct s_subst *s) 558{ 559 int gn; /* True if we have seen g or n */ 560 unsigned long nval; 561 char wfile[_POSIX2_LINE_MAX + 1], *q, *eq; 562 563 s->n = 1; /* Default */ 564 s->p = 0; 565 s->wfile = NULL; 566 s->wfd = -1; 567 s->icase = 0; 568 for (gn = 0;;) { 569 EATSPACE(); /* EXTENSION */ 570 switch (*p) { 571 case 'g': 572 if (gn) 573 errx(1, 574"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); 575 gn = 1; 576 s->n = 0; 577 break; 578 case '\0': 579 case '\n': 580 case ';': 581 return (p); 582 case 'p': 583 s->p = 1; 584 break; 585 case 'i': 586 case 'I': 587 s->icase = 1; 588 break; 589 case '1': case '2': case '3': 590 case '4': case '5': case '6': 591 case '7': case '8': case '9': 592 if (gn) 593 errx(1, 594"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); 595 gn = 1; 596 errno = 0; 597 nval = strtol(p, &p, 10); 598 if (errno == ERANGE || nval > INT_MAX) 599 errx(1, 600"%lu: %s: overflow in the 'N' substitute flag", linenum, fname); 601 s->n = nval; 602 p--; 603 break; 604 case 'w': 605 p++; 606#ifdef HISTORIC_PRACTICE 607 if (*p != ' ') { 608 warnx("%lu: %s: space missing before w wfile", linenum, fname); 609 return (p); 610 } 611#endif 612 EATSPACE(); 613 q = wfile; 614 eq = wfile + sizeof(wfile) - 1; 615 while (*p) { 616 if (*p == '\n') 617 break; 618 if (q >= eq) 619 err(1, "wfile too long"); 620 *q++ = *p++; 621 } 622 *q = '\0'; 623 if (q == wfile) 624 errx(1, "%lu: %s: no wfile specified", linenum, fname); 625 s->wfile = strdup(wfile); 626 if (!aflag && (s->wfd = open(wfile, 627 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 628 DEFFILEMODE)) == -1) 629 err(1, "%s", wfile); 630 return (p); 631 default: 632 errx(1, "%lu: %s: bad flag in substitute command: '%c'", 633 linenum, fname, *p); 634 break; 635 } 636 p++; 637 } 638} 639 640/* 641 * Compile a translation set of strings into a lookup table. 642 */ 643static char * 644compile_tr(char *p, struct s_tr **py) 645{ 646 struct s_tr *y; 647 int i; 648 const char *op, *np; 649 char old[_POSIX2_LINE_MAX + 1]; 650 char new[_POSIX2_LINE_MAX + 1]; 651 size_t oclen, oldlen, nclen, newlen; 652 mbstate_t mbs1, mbs2; 653 654 if ((*py = y = malloc(sizeof(*y))) == NULL) 655 err(1, NULL); 656 y->multis = NULL; 657 y->nmultis = 0; 658 659 if (*p == '\0' || *p == '\\') 660 errx(1, 661 "%lu: %s: transform pattern can not be delimited by newline or backslash", 662 linenum, fname); 663 p = compile_delimited(p, old, 1); 664 if (p == NULL) 665 errx(1, "%lu: %s: unterminated transform source string", 666 linenum, fname); 667 p = compile_delimited(p - 1, new, 1); 668 if (p == NULL) 669 errx(1, "%lu: %s: unterminated transform target string", 670 linenum, fname); 671 EATSPACE(); 672 op = old; 673 oldlen = mbsrtowcs(NULL, &op, 0, NULL); 674 if (oldlen == (size_t)-1) 675 err(1, NULL); 676 np = new; 677 newlen = mbsrtowcs(NULL, &np, 0, NULL); 678 if (newlen == (size_t)-1) 679 err(1, NULL); 680 if (newlen != oldlen) 681 errx(1, "%lu: %s: transform strings are not the same length", 682 linenum, fname); 683 if (MB_CUR_MAX == 1) { 684 /* 685 * The single-byte encoding case is easy: generate a 686 * lookup table. 687 */ 688 for (i = 0; i <= UCHAR_MAX; i++) 689 y->bytetab[i] = (char)i; 690 for (; *op; op++, np++) 691 y->bytetab[(u_char)*op] = *np; 692 } else { 693 /* 694 * Multi-byte encoding case: generate a lookup table as 695 * above, but only for single-byte characters. The first 696 * bytes of multi-byte characters have their lookup table 697 * entries set to 0, which causes do_tr() to search through 698 * an auxiliary vector of multi-byte mappings. 699 */ 700 memset(&mbs1, 0, sizeof(mbs1)); 701 memset(&mbs2, 0, sizeof(mbs2)); 702 for (i = 0; i <= UCHAR_MAX; i++) 703 y->bytetab[i] = (btowc(i) != WEOF) ? i : 0; 704 while (*op != '\0') { 705 oclen = mbrlen(op, MB_LEN_MAX, &mbs1); 706 if (oclen == (size_t)-1 || oclen == (size_t)-2) 707 errc(1, EILSEQ, NULL); 708 nclen = mbrlen(np, MB_LEN_MAX, &mbs2); 709 if (nclen == (size_t)-1 || nclen == (size_t)-2) 710 errc(1, EILSEQ, NULL); 711 if (oclen == 1 && nclen == 1) 712 y->bytetab[(u_char)*op] = *np; 713 else { 714 y->bytetab[(u_char)*op] = 0; 715 y->multis = realloc(y->multis, 716 (y->nmultis + 1) * sizeof(*y->multis)); 717 if (y->multis == NULL) 718 err(1, NULL); 719 i = y->nmultis++; 720 y->multis[i].fromlen = oclen; 721 memcpy(y->multis[i].from, op, oclen); 722 y->multis[i].tolen = nclen; 723 memcpy(y->multis[i].to, np, nclen); 724 } 725 op += oclen; 726 np += nclen; 727 } 728 } 729 return (p); 730} 731 732/* 733 * Compile the text following an a or i command. 734 */ 735static char * 736compile_text(void) 737{ 738 int asize, esc_nl, size; 739 char *text, *p, *op, *s; 740 char lbuf[_POSIX2_LINE_MAX + 1]; 741 742 asize = 2 * _POSIX2_LINE_MAX + 1; 743 if ((text = malloc(asize)) == NULL) 744 err(1, "malloc"); 745 size = 0; 746 while (cu_fgets(lbuf, sizeof(lbuf), NULL)) { 747 op = s = text + size; 748 p = lbuf; 749 EATSPACE(); 750 for (esc_nl = 0; *p != '\0'; p++) { 751 if (*p == '\\' && p[1] != '\0' && *++p == '\n') 752 esc_nl = 1; 753 *s++ = *p; 754 } 755 size += s - op; 756 if (!esc_nl) { 757 *s = '\0'; 758 break; 759 } 760 if (asize - size < _POSIX2_LINE_MAX + 1) { 761 asize *= 2; 762 if ((text = realloc(text, asize)) == NULL) 763 err(1, "realloc"); 764 } 765 } 766 text[size] = '\0'; 767 if ((p = realloc(text, size + 1)) == NULL) 768 err(1, "realloc"); 769 return (p); 770} 771 772/* 773 * Get an address and return a pointer to the first character after 774 * it. Fill the structure pointed to according to the address. 775 */ 776static char * 777compile_addr(char *p, struct s_addr *a) 778{ 779 char *end, re[_POSIX2_LINE_MAX + 1]; 780 int icase; 781 782 icase = 0; 783 784 a->type = 0; 785 switch (*p) { 786 case '\\': /* Context address */ 787 ++p; 788 /* FALLTHROUGH */ 789 case '/': /* Context address */ 790 p = compile_delimited(p, re, 0); 791 if (p == NULL) 792 errx(1, "%lu: %s: unterminated regular expression", linenum, fname); 793 /* Check for case insensitive regexp flag */ 794 if (*p == 'I') { 795 icase = 1; 796 p++; 797 } 798 if (*re == '\0') 799 a->u.r = NULL; 800 else 801 a->u.r = compile_re(re, icase); 802 a->type = AT_RE; 803 return (p); 804 805 case '$': /* Last line */ 806 a->type = AT_LAST; 807 return (p + 1); 808 809 case '+': /* Relative line number */ 810 a->type = AT_RELLINE; 811 p++; 812 /* FALLTHROUGH */ 813 /* Line number */ 814 case '0': case '1': case '2': case '3': case '4': 815 case '5': case '6': case '7': case '8': case '9': 816 if (a->type == 0) 817 a->type = AT_LINE; 818 a->u.l = strtol(p, &end, 10); 819 return (end); 820 default: 821 errx(1, "%lu: %s: expected context address", linenum, fname); 822 return (NULL); 823 } 824} 825 826/* 827 * duptoeol -- 828 * Return a copy of all the characters up to \n or \0. 829 */ 830static char * 831duptoeol(char *s, const char *ctype) 832{ 833 size_t len; 834 int ws; 835 char *p, *start; 836 837 ws = 0; 838 for (start = s; *s != '\0' && *s != '\n'; ++s) 839 ws = isspace((unsigned char)*s); 840 *s = '\0'; 841 if (ws) 842 warnx("%lu: %s: whitespace after %s", linenum, fname, ctype); 843 len = s - start + 1; 844 if ((p = malloc(len)) == NULL) 845 err(1, "malloc"); 846 return (memmove(p, start, len)); 847} 848 849/* 850 * Convert goto label names to addresses, and count a and r commands, in 851 * the given subset of the script. Free the memory used by labels in b 852 * and t commands (but not by :). 853 * 854 * TODO: Remove } nodes 855 */ 856static void 857fixuplabel(struct s_command *cp, struct s_command *end) 858{ 859 860 for (; cp != end; cp = cp->next) 861 switch (cp->code) { 862 case 'a': 863 case 'r': 864 appendnum++; 865 break; 866 case 'b': 867 case 't': 868 /* Resolve branch target. */ 869 if (cp->t == NULL) { 870 cp->u.c = NULL; 871 break; 872 } 873 if ((cp->u.c = findlabel(cp->t)) == NULL) 874 errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t); 875 free(cp->t); 876 break; 877 case '{': 878 /* Do interior commands. */ 879 fixuplabel(cp->u.c, cp->next); 880 break; 881 } 882} 883 884/* 885 * Associate the given command label for later lookup. 886 */ 887static void 888enterlabel(struct s_command *cp) 889{ 890 struct labhash **lhp, *lh; 891 u_char *p; 892 u_int h, c; 893 894 for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++) 895 h = (h << 5) + h + c; 896 lhp = &labels[h & LHMASK]; 897 for (lh = *lhp; lh != NULL; lh = lh->lh_next) 898 if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0) 899 errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t); 900 if ((lh = malloc(sizeof *lh)) == NULL) 901 err(1, "malloc"); 902 lh->lh_next = *lhp; 903 lh->lh_hash = h; 904 lh->lh_cmd = cp; 905 lh->lh_ref = 0; 906 *lhp = lh; 907} 908 909/* 910 * Find the label contained in the command l in the command linked 911 * list cp. L is excluded from the search. Return NULL if not found. 912 */ 913static struct s_command * 914findlabel(char *name) 915{ 916 struct labhash *lh; 917 u_char *p; 918 u_int h, c; 919 920 for (h = 0, p = (u_char *)name; (c = *p) != 0; p++) 921 h = (h << 5) + h + c; 922 for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) { 923 if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) { 924 lh->lh_ref = 1; 925 return (lh->lh_cmd); 926 } 927 } 928 return (NULL); 929} 930 931/* 932 * Warn about any unused labels. As a side effect, release the label hash 933 * table space. 934 */ 935static void 936uselabel(void) 937{ 938 struct labhash *lh, *next; 939 int i; 940 941 for (i = 0; i < LHSZ; i++) { 942 for (lh = labels[i]; lh != NULL; lh = next) { 943 next = lh->lh_next; 944 if (!lh->lh_ref) 945 warnx("%lu: %s: unused label '%s'", 946 linenum, fname, lh->lh_cmd->t); 947 free(lh); 948 } 949 } 950} 951