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