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