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