1 /*- 2 * Copyright (c) 1980, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 /*static char sccsid[] = "from: @(#)dol.c 8.1 (Berkeley) 5/31/93";*/ 36 static char *rcsid = "$Id: dol.c,v 1.6 1994/09/23 11:16:33 mycroft Exp $"; 37 #endif /* not lint */ 38 39 #include <sys/types.h> 40 #include <fcntl.h> 41 #include <errno.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #if __STDC__ 46 # include <stdarg.h> 47 #else 48 # include <varargs.h> 49 #endif 50 51 #include "csh.h" 52 #include "extern.h" 53 54 /* 55 * These routines perform variable substitution and quoting via ' and ". 56 * To this point these constructs have been preserved in the divided 57 * input words. Here we expand variables and turn quoting via ' and " into 58 * QUOTE bits on characters (which prevent further interpretation). 59 * If the `:q' modifier was applied during history expansion, then 60 * some QUOTEing may have occurred already, so we dont "trim()" here. 61 */ 62 63 static int Dpeekc, Dpeekrd; /* Peeks for DgetC and Dreadc */ 64 static Char *Dcp, **Dvp; /* Input vector for Dreadc */ 65 66 #define DEOF -1 67 68 #define unDgetC(c) Dpeekc = c 69 70 #define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */ 71 72 /* 73 * The following variables give the information about the current 74 * $ expansion, recording the current word position, the remaining 75 * words within this expansion, the count of remaining words, and the 76 * information about any : modifier which is being applied. 77 */ 78 #define MAXWLEN (BUFSIZ - 4) 79 #define MAXMOD MAXWLEN /* This cannot overflow */ 80 static Char *dolp; /* Remaining chars from this word */ 81 static Char **dolnxt; /* Further words */ 82 static int dolcnt; /* Count of further words */ 83 static Char dolmod[MAXMOD]; /* : modifier character */ 84 static int dolnmod; /* Number of modifiers */ 85 static int dolmcnt; /* :gx -> 10000, else 1 */ 86 static int dolwcnt; /* :wx -> 10000, else 1 */ 87 88 static void Dfix2 __P((Char **)); 89 static Char *Dpack __P((Char *, Char *)); 90 static int Dword __P((void)); 91 static void dolerror __P((Char *)); 92 static int DgetC __P((int)); 93 static void Dgetdol __P((void)); 94 static void fixDolMod __P((void)); 95 static void setDolp __P((Char *)); 96 static void unDredc __P((int)); 97 static int Dredc __P((void)); 98 static void Dtestq __P((int)); 99 100 101 /* 102 * Fix up the $ expansions and quotations in the 103 * argument list to command t. 104 */ 105 void 106 Dfix(t) 107 register struct command *t; 108 { 109 register Char **pp; 110 register Char *p; 111 112 if (noexec) 113 return; 114 /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */ 115 for (pp = t->t_dcom; (p = *pp++) != NULL;) 116 for (; *p; p++) { 117 if (cmap(*p, _DOL | QUOTES)) { /* $, \, ', ", ` */ 118 Dfix2(t->t_dcom); /* found one */ 119 blkfree(t->t_dcom); 120 t->t_dcom = gargv; 121 gargv = 0; 122 return; 123 } 124 } 125 } 126 127 /* 128 * $ substitute one word, for i/o redirection 129 */ 130 Char * 131 Dfix1(cp) 132 register Char *cp; 133 { 134 Char *Dv[2]; 135 136 if (noexec) 137 return (0); 138 Dv[0] = cp; 139 Dv[1] = NULL; 140 Dfix2(Dv); 141 if (gargc != 1) { 142 setname(vis_str(cp)); 143 stderror(ERR_NAME | ERR_AMBIG); 144 } 145 cp = Strsave(gargv[0]); 146 blkfree(gargv), gargv = 0; 147 return (cp); 148 } 149 150 /* 151 * Subroutine to do actual fixing after state initialization. 152 */ 153 static void 154 Dfix2(v) 155 Char **v; 156 { 157 ginit(); /* Initialize glob's area pointers */ 158 Dvp = v; 159 Dcp = STRNULL; /* Setup input vector for Dreadc */ 160 unDgetC(0); 161 unDredc(0); /* Clear out any old peeks (at error) */ 162 dolp = 0; 163 dolcnt = 0; /* Clear out residual $ expands (...) */ 164 while (Dword()) 165 continue; 166 } 167 168 /* 169 * Pack up more characters in this word 170 */ 171 static Char * 172 Dpack(wbuf, wp) 173 Char *wbuf, *wp; 174 { 175 register int c; 176 register int i = MAXWLEN - (wp - wbuf); 177 178 for (;;) { 179 c = DgetC(DODOL); 180 if (c == '\\') { 181 c = DgetC(0); 182 if (c == DEOF) { 183 unDredc(c); 184 *wp = 0; 185 Gcat(STRNULL, wbuf); 186 return (NULL); 187 } 188 if (c == '\n') 189 c = ' '; 190 else 191 c |= QUOTE; 192 } 193 if (c == DEOF) { 194 unDredc(c); 195 *wp = 0; 196 Gcat(STRNULL, wbuf); 197 return (NULL); 198 } 199 if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */ 200 unDgetC(c); 201 if (cmap(c, QUOTES)) 202 return (wp); 203 *wp++ = 0; 204 Gcat(STRNULL, wbuf); 205 return (NULL); 206 } 207 if (--i <= 0) 208 stderror(ERR_WTOOLONG); 209 *wp++ = c; 210 } 211 } 212 213 /* 214 * Get a word. This routine is analogous to the routine 215 * word() in sh.lex.c for the main lexical input. One difference 216 * here is that we don't get a newline to terminate our expansion. 217 * Rather, DgetC will return a DEOF when we hit the end-of-input. 218 */ 219 static int 220 Dword() 221 { 222 register int c, c1; 223 Char wbuf[BUFSIZ]; 224 register Char *wp = wbuf; 225 register int i = MAXWLEN; 226 register bool dolflg; 227 bool sofar = 0, done = 0; 228 229 while (!done) { 230 done = 1; 231 c = DgetC(DODOL); 232 switch (c) { 233 234 case DEOF: 235 if (sofar == 0) 236 return (0); 237 /* finish this word and catch the code above the next time */ 238 unDredc(c); 239 /* fall into ... */ 240 241 case '\n': 242 *wp = 0; 243 Gcat(STRNULL, wbuf); 244 return (1); 245 246 case ' ': 247 case '\t': 248 done = 0; 249 break; 250 251 case '`': 252 /* We preserve ` quotations which are done yet later */ 253 *wp++ = c, --i; 254 case '\'': 255 case '"': 256 /* 257 * Note that DgetC never returns a QUOTES character from an 258 * expansion, so only true input quotes will get us here or out. 259 */ 260 c1 = c; 261 dolflg = c1 == '"' ? DODOL : 0; 262 for (;;) { 263 c = DgetC(dolflg); 264 if (c == c1) 265 break; 266 if (c == '\n' || c == DEOF) 267 stderror(ERR_UNMATCHED, c1); 268 if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE)) 269 --wp, ++i; 270 if (--i <= 0) 271 stderror(ERR_WTOOLONG); 272 switch (c1) { 273 274 case '"': 275 /* 276 * Leave any `s alone for later. Other chars are all 277 * quoted, thus `...` can tell it was within "...". 278 */ 279 *wp++ = c == '`' ? '`' : c | QUOTE; 280 break; 281 282 case '\'': 283 /* Prevent all further interpretation */ 284 *wp++ = c | QUOTE; 285 break; 286 287 case '`': 288 /* Leave all text alone for later */ 289 *wp++ = c; 290 break; 291 292 default: 293 break; 294 } 295 } 296 if (c1 == '`') 297 *wp++ = '`' /* i--; eliminated */; 298 sofar = 1; 299 if ((wp = Dpack(wbuf, wp)) == NULL) 300 return (1); 301 else { 302 i = MAXWLEN - (wp - wbuf); 303 done = 0; 304 } 305 break; 306 307 case '\\': 308 c = DgetC(0); /* No $ subst! */ 309 if (c == '\n' || c == DEOF) { 310 done = 0; 311 break; 312 } 313 c |= QUOTE; 314 break; 315 316 default: 317 break; 318 } 319 if (done) { 320 unDgetC(c); 321 sofar = 1; 322 if ((wp = Dpack(wbuf, wp)) == NULL) 323 return (1); 324 else { 325 i = MAXWLEN - (wp - wbuf); 326 done = 0; 327 } 328 } 329 } 330 /* Really NOTREACHED */ 331 return (0); 332 } 333 334 335 /* 336 * Get a character, performing $ substitution unless flag is 0. 337 * Any QUOTES character which is returned from a $ expansion is 338 * QUOTEd so that it will not be recognized above. 339 */ 340 static int 341 DgetC(flag) 342 register int flag; 343 { 344 register int c; 345 346 top: 347 if ((c = Dpeekc) != '\0') { 348 Dpeekc = 0; 349 return (c); 350 } 351 if (lap) { 352 c = *lap++ & (QUOTE | TRIM); 353 if (c == 0) { 354 lap = 0; 355 goto top; 356 } 357 quotspec: 358 if (cmap(c, QUOTES)) 359 return (c | QUOTE); 360 return (c); 361 } 362 if (dolp) { 363 if ((c = *dolp++ & (QUOTE | TRIM)) != '\0') 364 goto quotspec; 365 if (dolcnt > 0) { 366 setDolp(*dolnxt++); 367 --dolcnt; 368 return (' '); 369 } 370 dolp = 0; 371 } 372 if (dolcnt > 0) { 373 setDolp(*dolnxt++); 374 --dolcnt; 375 goto top; 376 } 377 c = Dredc(); 378 if (c == '$' && flag) { 379 Dgetdol(); 380 goto top; 381 } 382 return (c); 383 } 384 385 static Char *nulvec[] = {0}; 386 static struct varent nulargv = {nulvec, STRargv, { NULL, NULL, NULL }, 0}; 387 388 static void 389 dolerror(s) 390 Char *s; 391 { 392 setname(vis_str(s)); 393 stderror(ERR_NAME | ERR_RANGE); 394 } 395 396 /* 397 * Handle the multitudinous $ expansion forms. 398 * Ugh. 399 */ 400 static void 401 Dgetdol() 402 { 403 register Char *np; 404 register struct varent *vp = NULL; 405 Char name[4 * MAXVARLEN + 1]; 406 int c, sc; 407 int subscr = 0, lwb = 1, upb = 0; 408 bool dimen = 0, bitset = 0; 409 char tnp; 410 Char wbuf[BUFSIZ]; 411 static Char *dolbang = NULL; 412 413 dolnmod = dolmcnt = dolwcnt = 0; 414 c = sc = DgetC(0); 415 if (c == '{') 416 c = DgetC(0); /* sc is { to take } later */ 417 if ((c & TRIM) == '#') 418 dimen++, c = DgetC(0); /* $# takes dimension */ 419 else if (c == '?') 420 bitset++, c = DgetC(0); /* $? tests existence */ 421 switch (c) { 422 423 case '!': 424 if (dimen || bitset) 425 stderror(ERR_SYNTAX); 426 if (backpid != 0) { 427 if (dolbang) 428 xfree((ptr_t) dolbang); 429 setDolp(dolbang = putn(backpid)); 430 } 431 goto eatbrac; 432 433 case '$': 434 if (dimen || bitset) 435 stderror(ERR_SYNTAX); 436 setDolp(doldol); 437 goto eatbrac; 438 439 case '<' | QUOTE: 440 if (bitset) 441 stderror(ERR_NOTALLOWED, "$?<"); 442 if (dimen) 443 stderror(ERR_NOTALLOWED, "$?#"); 444 for (np = wbuf; read(OLDSTD, &tnp, 1) == 1; np++) { 445 *np = (unsigned char) tnp; 446 if (np >= &wbuf[BUFSIZ - 1]) 447 stderror(ERR_LTOOLONG); 448 if (tnp == '\n') 449 break; 450 } 451 *np = 0; 452 /* 453 * KLUDGE: dolmod is set here because it will cause setDolp to call 454 * domod and thus to copy wbuf. Otherwise setDolp would use it 455 * directly. If we saved it ourselves, no one would know when to free 456 * it. The actual function of the 'q' causes filename expansion not to 457 * be done on the interpolated value. 458 */ 459 dolmod[dolnmod++] = 'q'; 460 dolmcnt = 10000; 461 setDolp(wbuf); 462 goto eatbrac; 463 464 case DEOF: 465 case '\n': 466 stderror(ERR_SYNTAX); 467 /* NOTREACHED */ 468 break; 469 470 case '*': 471 (void) Strcpy(name, STRargv); 472 vp = adrof(STRargv); 473 subscr = -1; /* Prevent eating [...] */ 474 break; 475 476 default: 477 np = name; 478 if (Isdigit(c)) { 479 if (dimen) 480 stderror(ERR_NOTALLOWED, "$#<num>"); 481 subscr = 0; 482 do { 483 subscr = subscr * 10 + c - '0'; 484 c = DgetC(0); 485 } while (Isdigit(c)); 486 unDredc(c); 487 if (subscr < 0) { 488 dolerror(vp->v_name); 489 return; 490 } 491 if (subscr == 0) { 492 if (bitset) { 493 dolp = ffile ? STR1 : STR0; 494 goto eatbrac; 495 } 496 if (ffile == 0) 497 stderror(ERR_DOLZERO); 498 fixDolMod(); 499 setDolp(ffile); 500 goto eatbrac; 501 } 502 if (bitset) 503 stderror(ERR_DOLQUEST); 504 vp = adrof(STRargv); 505 if (vp == 0) { 506 vp = &nulargv; 507 goto eatmod; 508 } 509 break; 510 } 511 if (!alnum(c)) 512 stderror(ERR_VARALNUM); 513 for (;;) { 514 *np++ = c; 515 c = DgetC(0); 516 if (!alnum(c)) 517 break; 518 if (np >= &name[MAXVARLEN]) 519 stderror(ERR_VARTOOLONG); 520 } 521 *np++ = 0; 522 unDredc(c); 523 vp = adrof(name); 524 } 525 if (bitset) { 526 dolp = (vp || getenv(short2str(name))) ? STR1 : STR0; 527 goto eatbrac; 528 } 529 if (vp == 0) { 530 np = str2short(getenv(short2str(name))); 531 if (np) { 532 fixDolMod(); 533 setDolp(np); 534 goto eatbrac; 535 } 536 udvar(name); 537 /* NOTREACHED */ 538 } 539 c = DgetC(0); 540 upb = blklen(vp->vec); 541 if (dimen == 0 && subscr == 0 && c == '[') { 542 np = name; 543 for (;;) { 544 c = DgetC(DODOL); /* Allow $ expand within [ ] */ 545 if (c == ']') 546 break; 547 if (c == '\n' || c == DEOF) 548 stderror(ERR_INCBR); 549 if (np >= &name[sizeof(name) / sizeof(Char) - 2]) 550 stderror(ERR_VARTOOLONG); 551 *np++ = c; 552 } 553 *np = 0, np = name; 554 if (dolp || dolcnt) /* $ exp must end before ] */ 555 stderror(ERR_EXPORD); 556 if (!*np) 557 stderror(ERR_SYNTAX); 558 if (Isdigit(*np)) { 559 int i; 560 561 for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0') 562 continue; 563 if ((i < 0 || i > upb) && !any("-*", *np)) { 564 dolerror(vp->v_name); 565 return; 566 } 567 lwb = i; 568 if (!*np) 569 upb = lwb, np = STRstar; 570 } 571 if (*np == '*') 572 np++; 573 else if (*np != '-') 574 stderror(ERR_MISSING, '-'); 575 else { 576 register int i = upb; 577 578 np++; 579 if (Isdigit(*np)) { 580 i = 0; 581 while (Isdigit(*np)) 582 i = i * 10 + *np++ - '0'; 583 if (i < 0 || i > upb) { 584 dolerror(vp->v_name); 585 return; 586 } 587 } 588 if (i < lwb) 589 upb = lwb - 1; 590 else 591 upb = i; 592 } 593 if (lwb == 0) { 594 if (upb != 0) { 595 dolerror(vp->v_name); 596 return; 597 } 598 upb = -1; 599 } 600 if (*np) 601 stderror(ERR_SYNTAX); 602 } 603 else { 604 if (subscr > 0) 605 if (subscr > upb) 606 lwb = 1, upb = 0; 607 else 608 lwb = upb = subscr; 609 unDredc(c); 610 } 611 if (dimen) { 612 Char *cp = putn(upb - lwb + 1); 613 614 addla(cp); 615 xfree((ptr_t) cp); 616 } 617 else { 618 eatmod: 619 fixDolMod(); 620 dolnxt = &vp->vec[lwb - 1]; 621 dolcnt = upb - lwb + 1; 622 } 623 eatbrac: 624 if (sc == '{') { 625 c = Dredc(); 626 if (c != '}') 627 stderror(ERR_MISSING, '}'); 628 } 629 } 630 631 static void 632 fixDolMod() 633 { 634 register int c; 635 636 c = DgetC(0); 637 if (c == ':') { 638 do { 639 c = DgetC(0), dolmcnt = 1, dolwcnt = 1; 640 if (c == 'g' || c == 'a') { 641 if (c == 'g') 642 dolmcnt = 10000; 643 else 644 dolwcnt = 10000; 645 c = DgetC(0); 646 } 647 if ((c == 'g' && dolmcnt != 10000) || 648 (c == 'a' && dolwcnt != 10000)) { 649 if (c == 'g') 650 dolmcnt = 10000; 651 else 652 dolwcnt = 10000; 653 c = DgetC(0); 654 } 655 656 if (c == 's') { /* [eichin:19910926.0755EST] */ 657 int delimcnt = 2; 658 int delim = DgetC(0); 659 dolmod[dolnmod++] = c; 660 dolmod[dolnmod++] = delim; 661 662 if (!delim || letter(delim) 663 || Isdigit(delim) || any(" \t\n", delim)) { 664 seterror(ERR_BADSUBST); 665 break; 666 } 667 while ((c = DgetC(0)) != (-1)) { 668 dolmod[dolnmod++] = c; 669 if(c == delim) delimcnt--; 670 if(!delimcnt) break; 671 } 672 if(delimcnt) { 673 seterror(ERR_BADSUBST); 674 break; 675 } 676 continue; 677 } 678 if (!any("htrqxes", c)) 679 stderror(ERR_BADMOD, c); 680 dolmod[dolnmod++] = c; 681 if (c == 'q') 682 dolmcnt = 10000; 683 } 684 while ((c = DgetC(0)) == ':'); 685 unDredc(c); 686 } 687 else 688 unDredc(c); 689 } 690 691 static void 692 setDolp(cp) 693 register Char *cp; 694 { 695 register Char *dp; 696 int i; 697 698 if (dolnmod == 0 || dolmcnt == 0) { 699 dolp = cp; 700 return; 701 } 702 dp = cp = Strsave(cp); 703 for (i = 0; i < dolnmod; i++) { 704 /* handle s// [eichin:19910926.0510EST] */ 705 if(dolmod[i] == 's') { 706 int delim; 707 Char *lhsub, *rhsub, *np; 708 size_t lhlen = 0, rhlen = 0; 709 int didmod = 0; 710 711 delim = dolmod[++i]; 712 if (!delim || letter(delim) 713 || Isdigit(delim) || any(" \t\n", delim)) { 714 seterror(ERR_BADSUBST); 715 break; 716 } 717 lhsub = &dolmod[++i]; 718 while(dolmod[i] != delim && dolmod[++i]) { 719 lhlen++; 720 } 721 dolmod[i] = 0; 722 rhsub = &dolmod[++i]; 723 while(dolmod[i] != delim && dolmod[++i]) { 724 rhlen++; 725 } 726 dolmod[i] = 0; 727 728 do { 729 dp = Strstr(cp, lhsub); 730 if (dp) { 731 np = (Char *) xmalloc((size_t) 732 ((Strlen(cp) + 1 - lhlen + rhlen) * 733 sizeof(Char))); 734 (void) Strncpy(np, cp, dp - cp); 735 (void) Strcpy(np + (dp - cp), rhsub); 736 (void) Strcpy(np + (dp - cp) + rhlen, dp + lhlen); 737 738 xfree((ptr_t) cp); 739 dp = cp = np; 740 didmod = 1; 741 } else { 742 /* should this do a seterror? */ 743 break; 744 } 745 } 746 while (dolwcnt == 10000); 747 /* 748 * restore dolmod for additional words 749 */ 750 dolmod[i] = rhsub[-1] = delim; 751 if (didmod) 752 dolmcnt--; 753 else 754 break; 755 } else { 756 int didmod = 0; 757 758 do { 759 if ((dp = domod(cp, dolmod[i]))) { 760 didmod = 1; 761 if (Strcmp(cp, dp) == 0) { 762 xfree((ptr_t) cp); 763 cp = dp; 764 break; 765 } 766 else { 767 xfree((ptr_t) cp); 768 cp = dp; 769 } 770 } 771 else 772 break; 773 } 774 while (dolwcnt == 10000); 775 dp = cp; 776 if (didmod) 777 dolmcnt--; 778 else 779 break; 780 } 781 } 782 783 if (dp) { 784 addla(dp); 785 xfree((ptr_t) dp); 786 } 787 else 788 addla(cp); 789 790 dolp = STRNULL; 791 if (seterr) 792 stderror(ERR_OLD); 793 } 794 795 static void 796 unDredc(c) 797 int c; 798 { 799 800 Dpeekrd = c; 801 } 802 803 static int 804 Dredc() 805 { 806 register int c; 807 808 if ((c = Dpeekrd) != '\0') { 809 Dpeekrd = 0; 810 return (c); 811 } 812 if (Dcp && (c = *Dcp++)) 813 return (c & (QUOTE | TRIM)); 814 if (*Dvp == 0) { 815 Dcp = 0; 816 return (DEOF); 817 } 818 Dcp = *Dvp++; 819 return (' '); 820 } 821 822 static void 823 Dtestq(c) 824 register int c; 825 { 826 827 if (cmap(c, QUOTES)) 828 gflag = 1; 829 } 830 831 /* 832 * Form a shell temporary file (in unit 0) from the words 833 * of the shell input up to EOF or a line the same as "term". 834 * Unit 0 should have been closed before this call. 835 */ 836 void 837 /*ARGSUSED*/ 838 heredoc(term) 839 Char *term; 840 { 841 register int c; 842 Char *Dv[2]; 843 Char obuf[BUFSIZ], lbuf[BUFSIZ], mbuf[BUFSIZ]; 844 int ocnt, lcnt, mcnt; 845 register Char *lbp, *obp, *mbp; 846 Char **vp; 847 bool quoted; 848 char *tmp; 849 850 tmp = short2str(shtemp); 851 if (open(tmp, O_RDWR | O_CREAT | O_TRUNC, 0600) < 0) 852 stderror(ERR_SYSTEM, tmp, strerror(errno)); 853 (void) unlink(tmp); /* 0 0 inode! */ 854 Dv[0] = term; 855 Dv[1] = NULL; 856 gflag = 0; 857 trim(Dv); 858 rscan(Dv, Dtestq); 859 quoted = gflag; 860 ocnt = BUFSIZ; 861 obp = obuf; 862 for (;;) { 863 /* 864 * Read up a line 865 */ 866 lbp = lbuf; 867 lcnt = BUFSIZ - 4; 868 for (;;) { 869 c = readc(1); /* 1 -> Want EOF returns */ 870 if (c < 0 || c == '\n') 871 break; 872 if ((c &= TRIM) != '\0') { 873 *lbp++ = c; 874 if (--lcnt < 0) { 875 setname("<<"); 876 stderror(ERR_NAME | ERR_OVERFLOW); 877 } 878 } 879 } 880 *lbp = 0; 881 882 /* 883 * Check for EOF or compare to terminator -- before expansion 884 */ 885 if (c < 0 || eq(lbuf, term)) { 886 (void) write(0, short2str(obuf), (size_t) (BUFSIZ - ocnt)); 887 (void) lseek(0, 0l, L_SET); 888 return; 889 } 890 891 /* 892 * If term was quoted or -n just pass it on 893 */ 894 if (quoted || noexec) { 895 *lbp++ = '\n'; 896 *lbp = 0; 897 for (lbp = lbuf; (c = *lbp++) != '\0';) { 898 *obp++ = c; 899 if (--ocnt == 0) { 900 (void) write(0, short2str(obuf), BUFSIZ); 901 obp = obuf; 902 ocnt = BUFSIZ; 903 } 904 } 905 continue; 906 } 907 908 /* 909 * Term wasn't quoted so variable and then command expand the input 910 * line 911 */ 912 Dcp = lbuf; 913 Dvp = Dv + 1; 914 mbp = mbuf; 915 mcnt = BUFSIZ - 4; 916 for (;;) { 917 c = DgetC(DODOL); 918 if (c == DEOF) 919 break; 920 if ((c &= TRIM) == 0) 921 continue; 922 /* \ quotes \ $ ` here */ 923 if (c == '\\') { 924 c = DgetC(0); 925 if (!any("$\\`", c)) 926 unDgetC(c | QUOTE), c = '\\'; 927 else 928 c |= QUOTE; 929 } 930 *mbp++ = c; 931 if (--mcnt == 0) { 932 setname("<<"); 933 stderror(ERR_NAME | ERR_OVERFLOW); 934 } 935 } 936 *mbp++ = 0; 937 938 /* 939 * If any ` in line do command substitution 940 */ 941 mbp = mbuf; 942 if (any(short2str(mbp), '`')) { 943 /* 944 * 1 arg to dobackp causes substitution to be literal. Words are 945 * broken only at newlines so that all blanks and tabs are 946 * preserved. Blank lines (null words) are not discarded. 947 */ 948 vp = dobackp(mbuf, 1); 949 } 950 else 951 /* Setup trivial vector similar to return of dobackp */ 952 Dv[0] = mbp, Dv[1] = NULL, vp = Dv; 953 954 /* 955 * Resurrect the words from the command substitution each separated by 956 * a newline. Note that the last newline of a command substitution 957 * will have been discarded, but we put a newline after the last word 958 * because this represents the newline after the last input line! 959 */ 960 for (; *vp; vp++) { 961 for (mbp = *vp; *mbp; mbp++) { 962 *obp++ = *mbp & TRIM; 963 if (--ocnt == 0) { 964 (void) write(0, short2str(obuf), BUFSIZ); 965 obp = obuf; 966 ocnt = BUFSIZ; 967 } 968 } 969 *obp++ = '\n'; 970 if (--ocnt == 0) { 971 (void) write(0, short2str(obuf), BUFSIZ); 972 obp = obuf; 973 ocnt = BUFSIZ; 974 } 975 } 976 if (pargv) 977 blkfree(pargv), pargv = 0; 978 } 979 } 980