process.c (14670B)
1 /* $OpenBSD: process.c,v 1.19 2013/11/28 18:24:55 deraadt 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 #include <sys/ioctl.h> 39 #include <sys/uio.h> 40 41 #include <ctype.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <limits.h> 45 #include <regex.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #include "defs.h" 52 #include "extern.h" 53 #include "util.h" 54 55 static SPACE HS, PS, SS; 56 #define pd PS.deleted 57 #define ps PS.space 58 #define psl PS.len 59 #define hs HS.space 60 #define hsl HS.len 61 62 static inline int applies(struct s_command *); 63 static void flush_appends(void); 64 static void lputs(char *); 65 static inline int regexec_e(regex_t *, char *, int, int, size_t); 66 static void regsub(SPACE *, char *, char *); 67 static int substitute(struct s_command *); 68 69 struct s_appends *appends; /* Array of pointers to strings to append. */ 70 static int appendx; /* Index into appends array. */ 71 int appendnum; /* Size of appends array. */ 72 73 static int lastaddr; /* Set by applies if last address of a range. */ 74 static int sdone; /* If any substitutes since last line input. */ 75 /* Iov structure for 'w' commands. */ 76 static regex_t *defpreg; 77 size_t maxnsub; 78 regmatch_t *match; 79 80 #define OUT(s) do { fwrite(s, sizeof(u_char), psl, stdout); } while (0) 81 82 void 83 process(void) 84 { 85 struct s_command *cp; 86 SPACE tspace; 87 size_t len, oldpsl; 88 char *p; 89 90 for (linenum = 0; mf_fgets(&PS, REPLACE);) { 91 pd = 0; 92 top: 93 cp = prog; 94 redirect: 95 while (cp != NULL) { 96 if (!applies(cp)) { 97 cp = cp->next; 98 continue; 99 } 100 switch (cp->code) { 101 case '{': 102 cp = cp->u.c; 103 goto redirect; 104 case 'a': 105 if (appendx >= appendnum) { 106 appends = xrealloc(appends, 107 sizeof(struct s_appends) * 108 (appendnum * 2)); 109 appendnum *= 2; 110 } 111 appends[appendx].type = AP_STRING; 112 appends[appendx].s = cp->t; 113 appends[appendx].len = strlen(cp->t); 114 appendx++; 115 break; 116 case 'b': 117 cp = cp->u.c; 118 goto redirect; 119 case 'c': 120 pd = 1; 121 psl = 0; 122 if (cp->a2 == NULL || lastaddr) 123 (void)printf("%s", cp->t); 124 break; 125 case 'd': 126 pd = 1; 127 goto new; 128 case 'D': 129 if (pd) 130 goto new; 131 if (psl == 0 || 132 (p = memchr(ps, '\n', psl - 1)) == NULL) { 133 pd = 1; 134 goto new; 135 } else { 136 psl -= (p + 1) - ps; 137 memmove(ps, p + 1, psl); 138 goto top; 139 } 140 case 'g': 141 cspace(&PS, hs, hsl, REPLACE); 142 break; 143 case 'G': 144 if (hs == NULL) 145 cspace(&HS, "\n", 1, REPLACE); 146 cspace(&PS, hs, hsl, 0); 147 break; 148 case 'h': 149 cspace(&HS, ps, psl, REPLACE); 150 break; 151 case 'H': 152 cspace(&HS, ps, psl, 0); 153 break; 154 case 'i': 155 (void)printf("%s", cp->t); 156 break; 157 case 'l': 158 lputs(ps); 159 break; 160 case 'n': 161 if (!nflag && !pd) 162 OUT(ps); 163 flush_appends(); 164 if (!mf_fgets(&PS, REPLACE)) 165 exit(0); 166 pd = 0; 167 break; 168 case 'N': 169 flush_appends(); 170 if (!mf_fgets(&PS, 0)) { 171 if (!nflag && !pd) 172 OUT(ps); 173 exit(0); 174 } 175 break; 176 case 'p': 177 if (pd) 178 break; 179 OUT(ps); 180 break; 181 case 'P': 182 if (pd) 183 break; 184 if (psl != 0 && 185 (p = memchr(ps, '\n', psl - 1)) != NULL) { 186 oldpsl = psl; 187 psl = (p + 1) - ps; 188 } 189 OUT(ps); 190 if (p != NULL) 191 psl = oldpsl; 192 break; 193 case 'q': 194 if (!nflag && !pd) 195 OUT(ps); 196 flush_appends(); 197 exit(0); 198 case 'r': 199 if (appendx >= appendnum) 200 appends = xrealloc(appends, 201 sizeof(struct s_appends) * 202 (appendnum *= 2)); 203 appends[appendx].type = AP_FILE; 204 appends[appendx].s = cp->t; 205 appends[appendx].len = strlen(cp->t); 206 appendx++; 207 break; 208 case 's': 209 sdone |= substitute(cp); 210 break; 211 case 't': 212 if (sdone) { 213 sdone = 0; 214 cp = cp->u.c; 215 goto redirect; 216 } 217 break; 218 case 'w': 219 if (pd) 220 break; 221 if (cp->u.fd == -1 && (cp->u.fd = open(cp->t, 222 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 223 DEFFILEMODE)) == -1) 224 err(FATAL, "%s: %s", 225 cp->t, strerror(errno)); 226 if (write(cp->u.fd, ps, psl) != psl) 227 err(FATAL, "%s: %s", 228 cp->t, strerror(errno)); 229 break; 230 case 'x': 231 if (hs == NULL) 232 cspace(&HS, "\n", 1, REPLACE); 233 tspace = PS; 234 PS = HS; 235 HS = tspace; 236 break; 237 case 'y': 238 if (pd || psl == 0) 239 break; 240 for (p = ps, len = psl; --len; ++p) 241 *p = cp->u.y[(unsigned char)*p]; 242 break; 243 case ':': 244 case '}': 245 break; 246 case '=': 247 (void)printf("%lu\n", linenum); 248 } 249 cp = cp->next; 250 } /* for all cp */ 251 252 new: if (!nflag && !pd) 253 OUT(ps); 254 flush_appends(); 255 } /* for all lines */ 256 } 257 258 /* 259 * TRUE if the address passed matches the current program state 260 * (lastline, linenumber, ps). 261 */ 262 #define MATCH(a) \ 263 (a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, psl) : \ 264 (a)->type == AT_LINE ? linenum == (a)->u.l : lastline 265 266 /* 267 * Return TRUE if the command applies to the current line. Sets the inrange 268 * flag to process ranges. Interprets the non-select (``!'') flag. 269 */ 270 static inline int 271 applies(struct s_command *cp) 272 { 273 int r; 274 275 lastaddr = 0; 276 if (cp->a1 == NULL && cp->a2 == NULL) 277 r = 1; 278 else if (cp->a2) 279 if (cp->inrange) { 280 if (MATCH(cp->a2)) { 281 cp->inrange = 0; 282 lastaddr = 1; 283 } 284 r = 1; 285 } else if (MATCH(cp->a1)) { 286 /* 287 * If the second address is a number less than or 288 * equal to the line number first selected, only 289 * one line shall be selected. 290 * -- POSIX 1003.2 291 */ 292 if (cp->a2->type == AT_LINE && 293 linenum >= cp->a2->u.l) 294 lastaddr = 1; 295 else 296 cp->inrange = 1; 297 r = 1; 298 } else 299 r = 0; 300 else 301 r = MATCH(cp->a1); 302 return (cp->nonsel ? !r : r); 303 } 304 305 /* 306 * substitute -- 307 * Do substitutions in the pattern space. Currently, we build a 308 * copy of the new pattern space in the substitute space structure 309 * and then swap them. 310 */ 311 static int 312 substitute(struct s_command *cp) 313 { 314 SPACE tspace; 315 regex_t *re; 316 regoff_t slen; 317 int n, lastempty; 318 char *s; 319 320 s = ps; 321 re = cp->u.s->re; 322 if (re == NULL) { 323 if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) { 324 linenum = cp->u.s->linenum; 325 err(COMPILE, "\\%d not defined in the RE", 326 cp->u.s->maxbref); 327 } 328 } 329 if (!regexec_e(re, s, 0, 0, psl)) 330 return (0); 331 332 SS.len = 0; /* Clean substitute space. */ 333 slen = psl; 334 n = cp->u.s->n; 335 lastempty = 1; 336 337 do { 338 /* Copy the leading retained string. */ 339 if (n <= 1 && match[0].rm_so) 340 cspace(&SS, s, match[0].rm_so, APPEND); 341 342 /* Skip zero-length matches right after other matches. */ 343 if (lastempty || match[0].rm_so || 344 match[0].rm_so != match[0].rm_eo) { 345 if (n <= 1) { 346 /* Want this match: append replacement. */ 347 regsub(&SS, s, cp->u.s->new); 348 if (n == 1) 349 n = -1; 350 } else { 351 /* Want a later match: append original. */ 352 if (match[0].rm_eo) 353 cspace(&SS, s, match[0].rm_eo, APPEND); 354 n--; 355 } 356 } 357 358 /* Move past this match. */ 359 s += match[0].rm_eo; 360 slen -= match[0].rm_eo; 361 362 /* 363 * After a zero-length match, advance one byte, 364 * and at the end of the line, terminate. 365 */ 366 if (match[0].rm_so == match[0].rm_eo) { 367 if (*s == '\0' || *s == '\n') 368 slen = -1; 369 else 370 slen--; 371 if (*s != '\0') 372 cspace(&SS, s++, 1, APPEND); 373 lastempty = 1; 374 } else 375 lastempty = 0; 376 377 } while (n >= 0 && slen >= 0 && regexec_e(re, s, REG_NOTBOL, 0, slen)); 378 379 /* Did not find the requested number of matches. */ 380 if (n > 1) 381 return (0); 382 383 /* Copy the trailing retained string. */ 384 if (slen > 0) 385 cspace(&SS, s, slen, APPEND); 386 387 /* 388 * Swap the substitute space and the pattern space, and make sure 389 * that any leftover pointers into stdio memory get lost. 390 */ 391 tspace = PS; 392 PS = SS; 393 SS = tspace; 394 SS.space = SS.back; 395 396 /* Handle the 'p' flag. */ 397 if (cp->u.s->p) 398 OUT(ps); 399 400 /* Handle the 'w' flag. */ 401 if (cp->u.s->wfile && !pd) { 402 if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile, 403 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1) 404 err(FATAL, "%s: %s", cp->u.s->wfile, strerror(errno)); 405 if (write(cp->u.s->wfd, ps, psl) != psl) 406 err(FATAL, "%s: %s", cp->u.s->wfile, strerror(errno)); 407 } 408 return (1); 409 } 410 411 /* 412 * Flush append requests. Always called before reading a line, 413 * therefore it also resets the substitution done (sdone) flag. 414 */ 415 static void 416 flush_appends(void) 417 { 418 FILE *f; 419 int count, i; 420 char buf[8 * 1024]; 421 422 for (i = 0; i < appendx; i++) 423 switch (appends[i].type) { 424 case AP_STRING: 425 fwrite(appends[i].s, sizeof(char), appends[i].len, 426 stdout); 427 break; 428 case AP_FILE: 429 /* 430 * Read files probably shouldn't be cached. Since 431 * it's not an error to read a non-existent file, 432 * it's possible that another program is interacting 433 * with the sed script through the file system. It 434 * would be truly bizarre, but possible. It's probably 435 * not that big a performance win, anyhow. 436 */ 437 if ((f = fopen(appends[i].s, "r")) == NULL) 438 break; 439 while ((count = fread(buf, sizeof(char), sizeof(buf), f))) 440 (void)fwrite(buf, sizeof(char), count, stdout); 441 (void)fclose(f); 442 break; 443 } 444 if (ferror(stdout)) 445 err(FATAL, "stdout: %s", strerror(errno ? errno : EIO)); 446 appendx = sdone = 0; 447 } 448 449 static void 450 lputs(char *s) 451 { 452 int count; 453 char *escapes, *p; 454 struct winsize win; 455 static int termwidth = -1; 456 457 if (termwidth == -1) { 458 if ((p = getenv("COLUMNS"))) 459 termwidth = atoi(p); 460 else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && 461 win.ws_col > 0) 462 termwidth = win.ws_col; 463 else 464 termwidth = 60; 465 } 466 467 for (count = 0; *s; ++s) { 468 if (count >= termwidth) { 469 (void)printf("\\\n"); 470 count = 0; 471 } 472 if (isascii((unsigned char)*s) && isprint((unsigned char)*s) 473 && *s != '\\') { 474 (void)putchar(*s); 475 count++; 476 } else if (*s != '\n') { 477 escapes = "\\\a\b\f\r\t\v"; 478 (void)putchar('\\'); 479 if ((p = strchr(escapes, *s))) { 480 (void)putchar("\\abfrtv"[p - escapes]); 481 count += 2; 482 } else { 483 (void)printf("%03o", *(u_char *)s); 484 count += 4; 485 } 486 } 487 } 488 (void)putchar('$'); 489 (void)putchar('\n'); 490 if (ferror(stdout)) 491 err(FATAL, "stdout: %s", strerror(errno ? errno : EIO)); 492 } 493 494 static inline int 495 regexec_e(regex_t *preg, char *string, int eflags, 496 int nomatch, size_t slen) 497 { 498 int eval; 499 int tmp; 500 501 if (preg == NULL) { 502 if (defpreg == NULL) 503 err(FATAL, "first RE may not be empty"); 504 } else 505 defpreg = preg; 506 507 /* Set anchors, discounting trailing newline (if any). */ 508 if (slen > 0 && string[slen - 1] == '\n') 509 slen--; 510 511 /* workaround for systems that don't support REG_STARTEND */ 512 tmp = string[slen]; 513 string[slen] = '\0'; 514 515 eval = regexec(defpreg, string, 516 nomatch ? 0 : maxnsub + 1, match, eflags); 517 518 string[slen] = tmp; /* restore byte */ 519 520 switch (eval) { 521 case 0: 522 return (1); 523 case REG_NOMATCH: 524 return (0); 525 } 526 err(FATAL, "RE error: %s", strregerror(eval, defpreg)); 527 /* NOTREACHED */ 528 return 0; 529 } 530 531 /* 532 * regsub - perform substitutions after a regexp match 533 * Based on a routine by Henry Spencer 534 */ 535 static void 536 regsub(SPACE *sp, char *string, char *src) 537 { 538 int len, no; 539 char c, *dst; 540 541 #define NEEDSP(reqlen) \ 542 if (sp->len + (reqlen) + 1 >= sp->blen) { \ 543 size_t newlen = sp->blen + (reqlen) + 1024; \ 544 sp->space = sp->back = xrealloc(sp->back, newlen); \ 545 sp->blen = newlen; \ 546 dst = sp->space + sp->len; \ 547 } 548 549 dst = sp->space + sp->len; 550 while ((c = *src++) != '\0') { 551 if (c == '&') 552 no = 0; 553 else if (c == '\\' && isdigit((unsigned char)*src)) 554 no = *src++ - '0'; 555 else 556 no = -1; 557 if (no < 0) { /* Ordinary character. */ 558 if (c == '\\' && (*src == '\\' || *src == '&')) 559 c = *src++; 560 NEEDSP(1); 561 *dst++ = c; 562 ++sp->len; 563 } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) { 564 len = match[no].rm_eo - match[no].rm_so; 565 NEEDSP(len); 566 memmove(dst, string + match[no].rm_so, len); 567 dst += len; 568 sp->len += len; 569 } 570 } 571 NEEDSP(1); 572 *dst = '\0'; 573 } 574 575 /* 576 * aspace -- 577 * Append the source space to the destination space, allocating new 578 * space as necessary. 579 */ 580 void 581 cspace(SPACE *sp, char *p, size_t len, enum e_spflag spflag) 582 { 583 size_t tlen; 584 585 /* Make sure SPACE has enough memory and ramp up quickly. */ 586 tlen = sp->len + len + 1; 587 if (tlen > sp->blen) { 588 size_t newlen = tlen + 1024; 589 sp->space = sp->back = xrealloc(sp->back, newlen); 590 sp->blen = newlen; 591 } 592 593 if (spflag == REPLACE) 594 sp->len = 0; 595 596 memmove(sp->space + sp->len, p, len); 597 598 sp->space[sp->len += len] = '\0'; 599 } 600 601 /* 602 * Close all cached opened files and report any errors 603 */ 604 void 605 cfclose(struct s_command *cp, struct s_command *end) 606 { 607 608 for (; cp != end; cp = cp->next) 609 switch (cp->code) { 610 case 's': 611 if (cp->u.s->wfd != -1 && close(cp->u.s->wfd)) 612 err(FATAL, 613 "%s: %s", cp->u.s->wfile, strerror(errno)); 614 cp->u.s->wfd = -1; 615 break; 616 case 'w': 617 if (cp->u.fd != -1 && close(cp->u.fd)) 618 err(FATAL, "%s: %s", cp->t, strerror(errno)); 619 cp->u.fd = -1; 620 break; 621 case '{': 622 cfclose(cp->u.c, cp->next); 623 break; 624 } 625 }