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