1: /*	@(#)sh.dol.c	2.1	SCCS id keyword	*/
   2: /* Copyright (c) 1980 Regents of the University of California */
   3: #include "sh.h"
   4: 
   5: /*
   6:  * C shell
   7:  */
   8: 
   9: /*
  10:  * These routines perform variable substitution and quoting via ' and ".
  11:  * To this point these constructs have been preserved in the divided
  12:  * input words.  Here we expand variables and turn quoting via ' and " into
  13:  * QUOTE bits on characters (which prevent further interpretation).
  14:  * If the `:q' modifier was applied during history expansion, then
  15:  * some QUOTEing may have occurred already, so we dont "scan(,&trim)" here.
  16:  */
  17: 
  18: int Dpeekc, Dpeekrd;        /* Peeks for DgetC and Dreadc */
  19: char    *Dcp, **Dvp;            /* Input vector for Dreadc */
  20: 
  21: #define DEOF    -1
  22: 
  23: #define unDgetC(c)  Dpeekc = c
  24: 
  25: char    *QUOTES = "\\'`\"";
  26: 
  27: /*
  28:  * The following variables give the information about the current
  29:  * $ expansion, recording the current word position, the remaining
  30:  * words within this expansion, the count of remaining words, and the
  31:  * information about any : modifier which is being applied.
  32:  */
  33: char    *dolp;          /* Remaining chars from this word */
  34: char    **dolnxt;       /* Further words */
  35: int dolcnt;         /* Count of further words */
  36: char    dolmod;         /* : modifier character */
  37: int dolmcnt;        /* :gx -> 10000, else 1 */
  38: 
  39: int Dtest();        /* Test for \ " ` or ' */
  40: 
  41: /*
  42:  * Fix up the $ expansions and quotations in the
  43:  * argument list to command t.
  44:  */
  45: Dfix(t)
  46:     register struct command *t;
  47: {
  48: 
  49:     if (noexec)
  50:         return;
  51:     gflag = 0, rscan(t->t_dcom, Dtest);
  52:     if (gflag == 0)
  53:         return;
  54:     Dfix2(t->t_dcom);
  55:     blkfree(t->t_dcom), t->t_dcom = gargv, gargv = 0;
  56: }
  57: 
  58: /*
  59:  * $ substitute one word, for i/o redirection
  60:  */
  61: char *
  62: Dfix1(cp)
  63:     register char *cp;
  64: {
  65:     char *Dv[2];
  66: 
  67:     if (noexec)
  68:         return (0);
  69:     Dv[0] = cp; Dv[1] = NOSTR;
  70:     Dfix2(Dv);
  71:     if (gargc != 1) {
  72:         setname(cp);
  73:         bferr("Ambiguous");
  74:     }
  75:     cp = savestr(gargv[0]);
  76:     blkfree(gargv), gargv = 0;
  77:     return (cp);
  78: }
  79: 
  80: /*
  81:  * Subroutine to do actual fixing after state initialization.
  82:  */
  83: Dfix2(v)
  84:     char **v;
  85: {
  86:     char *agargv[GAVSIZ];
  87: 
  88:     ginit(agargv);          /* Initialize glob's area pointers */
  89:     Dvp = v; Dcp = "";      /* Setup input vector for Dreadc */
  90:     unDgetC(0); unDredc(0);     /* Clear out any old peeks (at error) */
  91:     dolp = 0; dolcnt = 0;       /* Clear out residual $ expands (...) */
  92:     while (Dword())
  93:         continue;
  94:     gargv = copyblk(gargv);
  95: }
  96: 
  97: /*
  98:  * Get a word.  This routine is analogous to the routine
  99:  * word() in sh.lex.c for the main lexical input.  One difference
 100:  * here is that we don't get a newline to terminate our expansion.
 101:  * Rather, DgetC will return a DEOF when we hit the end-of-input.
 102:  */
 103: Dword()
 104: {
 105:     register int c, c1;
 106:     char wbuf[BUFSIZ];
 107:     register char *wp = wbuf;
 108:     register int i = BUFSIZ - 4;
 109:     register bool dolflg;
 110:     bool sofar = 0;
 111: 
 112: loop:
 113:     c = DgetC(DODOL);
 114:     switch (c) {
 115: 
 116:     case DEOF:
 117: deof:
 118:         if (sofar == 0)
 119:             return (0);
 120:         /* finish this word and catch the code above the next time */
 121:         unDredc(c);
 122:         /* fall into ... */
 123: 
 124:     case '\n':
 125:         *wp = 0;
 126:         goto ret;
 127: 
 128:     case ' ':
 129:     case '\t':
 130:         goto loop;
 131: 
 132:     case '`':
 133:         /* We preserve ` quotations which are done yet later */
 134:         *wp++ = c, --i;
 135:     case '\'':
 136:     case '"':
 137:         /*
 138: 		 * Note that DgetC never returns a QUOTES character
 139: 		 * from an expansion, so only true input quotes will
 140: 		 * get us here or out.
 141: 		 */
 142:         c1 = c;
 143:         dolflg = c1 == '"' ? DODOL : 0;
 144:         for (;;) {
 145:             c = DgetC(dolflg);
 146:             if (c == c1)
 147:                 break;
 148:             if (c == '\n' || c == DEOF)
 149:                 error("Unmatched %c", c1);
 150:             if ((c & (QUOTE|TRIM)) == ('\n' | QUOTE))
 151:                 --wp, ++i;
 152:             if (--i <= 0)
 153:                 goto toochars;
 154:             switch (c1) {
 155: 
 156:             case '"':
 157:                 /*
 158: 				 * Leave any `s alone for later.
 159: 				 * Other chars are all quoted, thus `...`
 160: 				 * can tell it was within "...".
 161: 				 */
 162:                 *wp++ = c == '`' ? '`' : c | QUOTE;
 163:                 break;
 164: 
 165:             case '\'':
 166:                 /* Prevent all further interpretation */
 167:                 *wp++ = c | QUOTE;
 168:                 break;
 169: 
 170:             case '`':
 171:                 /* Leave all text alone for later */
 172:                 *wp++ = c;
 173:                 break;
 174:             }
 175:         }
 176:         if (c1 == '`')
 177:             *wp++ = '`', --i;
 178:         goto pack;      /* continue the word */
 179: 
 180:     case '\\':
 181:         c = DgetC(0);       /* No $ subst! */
 182:         if (c == '\n' || c == DEOF)
 183:             goto loop;
 184:         c |= QUOTE;
 185:         break;
 186:     }
 187:     unDgetC(c);
 188: pack:
 189:     sofar = 1;
 190:     /* pack up more characters in this word */
 191:     for (;;) {
 192:         c = DgetC(DODOL);
 193:         if (c == '\\') {
 194:             c = DgetC(0);
 195:             if (c == DEOF)
 196:                 goto deof;
 197:             if (c == '\n')
 198:                 c = ' ';
 199:             else
 200:                 c |= QUOTE;
 201:         }
 202:         if (c == DEOF)
 203:             goto deof;
 204:         if (any(c, " '`\"\t\n")) {
 205:             unDgetC(c);
 206:             if (any(c, QUOTES))
 207:                 goto loop;
 208:             *wp++ = 0;
 209:             goto ret;
 210:         }
 211:         if (--i <= 0)
 212: toochars:
 213:             error("Word too long");
 214:         *wp++ = c;
 215:     }
 216: ret:
 217:     Gcat("", wbuf);
 218:     return (1);
 219: }
 220: 
 221: /*
 222:  * Get a character, performing $ substitution unless flag is 0.
 223:  * Any QUOTES character which is returned from a $ expansion is
 224:  * QUOTEd so that it will not be recognized above.
 225:  */
 226: DgetC(flag)
 227:     register int flag;
 228: {
 229:     register int c;
 230: 
 231: top:
 232:     if (c = Dpeekc) {
 233:         Dpeekc = 0;
 234:         return (c);
 235:     }
 236:     if (lap) {
 237:         c = *lap++;
 238:         if (c == 0) {
 239:             lap = 0;
 240:             goto top;
 241:         }
 242: quotspec:
 243:         if (any(c, QUOTES))
 244:             return (c | QUOTE);
 245:         return (c);
 246:     }
 247:     if (dolp) {
 248:         if (c = *dolp++)
 249:             goto quotspec;
 250:         if (dolcnt > 0) {
 251:             setDolp(*dolnxt++);
 252:             --dolcnt;
 253:             return (' ');
 254:         }
 255:         dolp = 0;
 256:     }
 257:     if (dolcnt > 0) {
 258:         setDolp(*dolnxt++);
 259:         --dolcnt;
 260:         goto top;
 261:     }
 262:     c = Dredc();
 263:     if (c == '$' && flag) {
 264:         Dgetdol();
 265:         goto top;
 266:     }
 267:     return (c);
 268: }
 269: 
 270: char    *nulvec[] = { 0 };
 271: struct  varent nulargv = { nulvec, "argv", 0 };
 272: 
 273: /*
 274:  * Handle the multitudinous $ expansion forms.
 275:  * Ugh.
 276:  */
 277: Dgetdol()
 278: {
 279:     register char *np;
 280:     register struct varent *vp;
 281:     char name[20];
 282:     int c, sc;
 283:     int subscr = 0, lwb = 1, upb = 0;
 284:     bool dimen = 0, isset = 0;
 285: 
 286:     dolmod = dolmcnt = 0;
 287:     c = sc = DgetC(0);
 288:     if (c == '{')
 289:         c = DgetC(0);       /* sc is { to take } later */
 290:     if ((c & TRIM) == '#')
 291:         dimen++, c = DgetC(0);      /* $# takes dimension */
 292:     else if (c == '?')
 293:         isset++, c = DgetC(0);      /* $? tests existence */
 294:     switch (c) {
 295: 
 296:     case '$':
 297:         if (dimen || isset)
 298:             goto syntax;        /* No $?$, $#$ */
 299:         setDolp(doldol);
 300:         goto eatbrac;
 301: 
 302:     case DEOF:
 303:     case '\n':
 304:         goto syntax;
 305: 
 306:     case '*':
 307:         strcpy(name, "argv");
 308:         vp = adrof("argv");
 309:         subscr = -1;            /* Prevent eating [...] */
 310:         break;
 311: 
 312:     default:
 313:         np = name;
 314:         if (digit(c)) {
 315:             if (dimen)
 316:                 goto syntax;    /* No $#1, e.g. */
 317:             subscr = 0;
 318:             do {
 319:                 subscr = subscr * 10 + c - '0';
 320:                 c = DgetC(0);
 321:             } while (digit(c));
 322:             unDredc(c);
 323:             if (subscr < 0)
 324:                 goto oob;
 325:             if (subscr == 0) {
 326:                 if (isset) {
 327:                     dolp = file ? "1" : "0";
 328:                     goto eatbrac;
 329:                 }
 330:                 if (file == 0)
 331:                     error("No file for $0");
 332:                 setDolp(file);
 333:                 goto eatbrac;
 334:             }
 335:             if (isset)
 336:                 goto syntax;
 337:             vp = adrof("argv");
 338:             if (vp == 0) {
 339:                 vp = &nulargv;
 340:                 goto eatmod;
 341:             }
 342:             break;
 343:         }
 344:         if (!alnum(c))
 345:             goto syntax;
 346:         for (;;) {
 347:             *np++ = c;
 348:             c = DgetC(0);
 349:             if (!alnum(c))
 350:                 break;
 351:             if (np >= &name[sizeof name - 2])
 352: syntax:
 353:                 error("Variable syntax");
 354:         }
 355:         *np++ = 0;
 356:         unDredc(c);
 357:         vp = adrof(name);
 358:     }
 359:     if (isset) {
 360:         dolp = (vp || getenv(name)) ? "1" : "0";
 361:         goto eatbrac;
 362:     }
 363:     if (vp == 0) {
 364:         char *cp = getenv(name);
 365: 
 366:         if (cp) {
 367:             addla(cp);
 368:             return;
 369:         }
 370:         udvar(name);
 371:     }
 372:     c = DgetC(0);
 373:     upb = blklen(vp->vec);
 374:     if (dimen == 0 && subscr == 0 && c == '[') {
 375:         np = name;
 376:         for (;;) {
 377:             c = DgetC(DODOL);   /* Allow $ expand within [ ] */
 378:             if (c == ']')
 379:                 break;
 380:             if (c == '\n' || c == DEOF)
 381:                 goto syntax;
 382:             if (np >= &name[sizeof name - 2])
 383:                 goto syntax;
 384:             *np++ = c;
 385:         }
 386:         *np = 0, np = name;
 387:         if (dolp || dolcnt)     /* $ exp must end before ] */
 388:             goto syntax;
 389:         if (!*np)
 390:             goto syntax;
 391:         if (digit(*np)) {
 392:             register int i = 0;
 393: 
 394:             while (digit(*np))
 395:                 i = i * 10 + *np++ - '0';
 396:             if ((i < 0 || i > upb) && !any(*np, "-*")) {
 397: oob:
 398:                 setname(vp->name);
 399:                 error("Subscript out of range");
 400:             }
 401:             lwb = i;
 402:             if (!*np)
 403:                 upb = lwb, np = "*";
 404:         }
 405:         if (*np == '*')
 406:             np++;
 407:         else if (*np != '-')
 408:             goto syntax;
 409:         else {
 410:             register int i = upb;
 411: 
 412:             np++;
 413:             if (digit(*np)) {
 414:                 i = 0;
 415:                 while (digit(*np))
 416:                     i = i * 10 + *np++ - '0';
 417:                 if (i < 0 || i > upb)
 418:                     goto oob;
 419:             }
 420:             if (i < lwb)
 421:                 upb = lwb - 1;
 422:             else
 423:                 upb = i;
 424:         }
 425:         if (lwb == 0) {
 426:             if (upb != 0)
 427:                 goto oob;
 428:             upb = -1;
 429:         }
 430:         if (*np)
 431:             goto syntax;
 432:     } else {
 433:         if (subscr > 0)
 434:             if (subscr > upb)
 435:                 lwb = 1, upb = 0;
 436:             else
 437:                 lwb = upb = subscr;
 438:         unDredc(c);
 439:     }
 440:     if (dimen) {
 441:         char *cp = putn(upb - lwb + 1);
 442: 
 443:         addla(cp);
 444:         xfree(cp);
 445:     } else {
 446: eatmod:
 447:         c = DgetC(0);
 448:         if (c == ':') {
 449:             c = DgetC(0), dolmcnt = 1;
 450:             if (c == 'g')
 451:                 c = DgetC(0), dolmcnt = 10000;
 452:             if (!any(c, "htrqx"))
 453:                 error("Bad : mod in $");
 454:             dolmod = c;
 455:             if (c == 'q')
 456:                 dolmcnt = 10000;
 457:         } else
 458:             unDredc(c);
 459:         dolnxt = &vp->vec[lwb - 1];
 460:         dolcnt = upb - lwb + 1;
 461:     }
 462: eatbrac:
 463:     if (sc == '{') {
 464:         c = Dredc();
 465:         if (c != '}')
 466:             goto syntax;
 467:     }
 468: }
 469: 
 470: setDolp(cp)
 471:     register char *cp;
 472: {
 473:     register char *dp;
 474: 
 475:     if (dolmod == 0 || dolmcnt == 0) {
 476:         dolp = cp;
 477:         return;
 478:     }
 479:     dp = domod(cp, dolmod);
 480:     if (dp) {
 481:         dolmcnt--;
 482:         addla(dp);
 483:         xfree(dp);
 484:     } else
 485:         addla(cp);
 486:     dolp = "";
 487: }
 488: 
 489: unDredc(c)
 490:     int c;
 491: {
 492: 
 493:     Dpeekrd = c;
 494: }
 495: 
 496: Dredc()
 497: {
 498:     register int c;
 499: 
 500:     if (c = Dpeekrd) {
 501:         Dpeekrd = 0;
 502:         return (c);
 503:     }
 504:     if (Dcp && (c = *Dcp++))
 505:         return (c);
 506:     if (*Dvp == 0) {
 507:         Dcp = 0;
 508:         return (DEOF);
 509:     }
 510:     Dcp = *Dvp++;
 511:     return (' ');
 512: }
 513: 
 514: Dtest(c)
 515:     register int c;
 516: {
 517: 
 518:     /* Note that c isn't trimmed thus !...:q's aren't lost */
 519:     if (any(c, "$\\'`\""))
 520:         gflag = 1;
 521: }
 522: 
 523: Dtestq(c)
 524:     register int c;
 525: {
 526: 
 527:     if (any(c, "\\'`\""))
 528:         gflag = 1;
 529: }
 530: 
 531: /*
 532:  * Form a shell temporary file (in unit 0) from the words
 533:  * of the shell input up to a line the same as "term".
 534:  * Unit 0 should have been closed before this call.
 535:  */
 536: heredoc(term)
 537:     char *term;
 538: {
 539:     register int c;
 540:     char *Dv[2];
 541:     char obuf[BUFSIZ], lbuf[BUFSIZ], mbuf[BUFSIZ];
 542:     int ocnt, lcnt, mcnt;
 543:     register char *lbp, *obp, *mbp;
 544:     char **vp;
 545:     bool quoted;
 546: 
 547:     if (creat(shtemp, 0600) < 0)
 548:         Perror(shtemp);
 549:     close(0);
 550:     if (open(shtemp, 2) < 0) {
 551:         int oerrno = errno;
 552: 
 553:         unlink(shtemp);
 554:         errno = oerrno;
 555:         Perror(shtemp);
 556:     }
 557:     unlink(shtemp);         /* 0 0 inode! */
 558:     Dv[0] = term; Dv[1] = NOSTR; gflag = 0;
 559:     scan(Dv, trim); rscan(Dv, Dtestq); quoted = gflag;
 560:     ocnt = BUFSIZ; obp = obuf;
 561:     for (;;) {
 562:         /*
 563: 		 * Read up a line
 564: 		 */
 565:         lbp = lbuf; lcnt = BUFSIZ - 4;
 566:         for (;;) {
 567:             c = readc(1);       /* 1 -> Want EOF returns */
 568:             if (c < 0) {
 569:                 setname(term);
 570:                 bferr("<< terminator not found");
 571:             }
 572:             if (c == '\n')
 573:                 break;
 574:             if (c &= TRIM) {
 575:                 *lbp++ = c;
 576:                 if (--lcnt < 0) {
 577:                     setname("<<");
 578:                     error("Line overflow");
 579:                 }
 580:             }
 581:         }
 582:         *lbp = 0;
 583: 
 584:         /*
 585: 		 * Compare to terminator -- before expansion
 586: 		 */
 587:         if (eq(lbuf, term)) {
 588:             write(0, obuf, BUFSIZ - ocnt);
 589:             lseek(0, 0l, 0);
 590:             return;
 591:         }
 592: 
 593:         /*
 594: 		 * If term was quoted or -n just pass it on
 595: 		 */
 596:         if (quoted || noexec) {
 597:             *lbp++ = '\n'; *lbp = 0;
 598:             for (lbp = lbuf; c = *lbp++;) {
 599:                 *obp++ = c;
 600:                 if (--ocnt == 0) {
 601:                     write(0, obuf, BUFSIZ);
 602:                     obp = obuf; ocnt = BUFSIZ;
 603:                 }
 604:             }
 605:             continue;
 606:         }
 607: 
 608:         /*
 609: 		 * Term wasn't quoted so variable and then command
 610: 		 * expand the input line
 611: 		 */
 612:         Dcp = lbuf; Dvp = Dv + 1; mbp = mbuf; mcnt = BUFSIZ - 4;
 613:         for (;;) {
 614:             c = DgetC(DODOL);
 615:             if (c == DEOF)
 616:                 break;
 617:             if ((c &= TRIM) == 0)
 618:                 continue;
 619:             /* \ quotes \ $ ` here */
 620:             if (c =='\\') {
 621:                 c = DgetC(0);
 622:                 if (!any(c, "$\\`"))
 623:                     unDgetC(c | QUOTE), c = '\\';
 624:                 else
 625:                     c |= QUOTE;
 626:             }
 627:             *mbp++ = c;
 628:             if (--mcnt == 0) {
 629:                 setname("<<");
 630:                 bferr("Line overflow");
 631:             }
 632:         }
 633:         *mbp++ = 0;
 634: 
 635:         /*
 636: 		 * If any ` in line do command substitution
 637: 		 */
 638:         mbp = mbuf;
 639:         if (any('`', mbp)) {
 640:             /*
 641: 			 * 1 arg to dobackp causes substitution to be literal.
 642: 			 * Words are broken only at newlines so that all blanks
 643: 			 * and tabs are preserved.  Blank lines (null words)
 644: 			 * are not discarded.
 645: 			 */
 646:             vp = dobackp(mbuf, 1);
 647:         } else
 648:             /* Setup trivial vector similar to return of dobackp */
 649:             Dv[0] = mbp, Dv[1] = NOSTR, vp = Dv;
 650: 
 651:         /*
 652: 		 * Resurrect the words from the command substitution
 653: 		 * each separated by a newline.  Note that the last
 654: 		 * newline of a command substitution will have been
 655: 		 * discarded, but we put a newline after the last word
 656: 		 * because this represents the newline after the last
 657: 		 * input line!
 658: 		 */
 659:         for (; *vp; vp++) {
 660:             for (mbp = *vp; *mbp; mbp++) {
 661:                 *obp++ = *mbp & TRIM;
 662:                 if (--ocnt == 0) {
 663:                     write(0, obuf, BUFSIZ);
 664:                     obp = obuf; ocnt = BUFSIZ;
 665:                 }
 666:             }
 667:             *obp++ = '\n';
 668:             if (--ocnt == 0) {
 669:                 write(0, obuf, BUFSIZ);
 670:                 obp = obuf; ocnt = BUFSIZ;
 671:             }
 672:         }
 673:         if (pargv)
 674:             blkfree(pargv), pargv = 0;
 675:     }
 676: }

Defined functions

Dfix defined in line 45; used 1 times
Dfix2 defined in line 83; used 2 times
DgetC defined in line 226; used 18 times
Dgetdol defined in line 277; used 1 times
Dredc defined in line 496; used 2 times
Dtest defined in line 514; used 2 times
Dtestq defined in line 523; used 1 times
Dword defined in line 103; used 1 times
  • in line 92
heredoc defined in line 536; used 1 times
setDolp defined in line 470; used 4 times
unDredc defined in line 489; used 6 times

Defined variables

Dcp defined in line 19; used 6 times
Dpeekc defined in line 18; used 3 times
Dpeekrd defined in line 18; used 3 times
Dvp defined in line 19; used 4 times
QUOTES defined in line 25; used 2 times
dolcnt defined in line 35; used 7 times
dolmcnt defined in line 37; used 6 times
dolmod defined in line 36; used 4 times
dolnxt defined in line 34; used 3 times
dolp defined in line 33; used 9 times
nulargv defined in line 271; used 1 times
nulvec defined in line 270; used 1 times

Defined macros

DEOF defined in line 21; used 7 times
unDgetC defined in line 23; used 4 times
Last modified: 1980-09-12
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1584
Valid CSS Valid XHTML 1.0 Strict