xref: /netbsd-src/bin/sh/expand.c (revision 1c9b56c830954ccf3b57004ac65562e3d6afacf6)
1 /*	$NetBSD: expand.c,v 1.68 2005/02/14 20:46:26 dsl Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kenneth Almquist.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)expand.c	8.5 (Berkeley) 5/15/95";
39 #else
40 __RCSID("$NetBSD: expand.c,v 1.68 2005/02/14 20:46:26 dsl Exp $");
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <sys/stat.h>
47 #include <errno.h>
48 #include <dirent.h>
49 #include <unistd.h>
50 #include <pwd.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 
54 /*
55  * Routines to expand arguments to commands.  We have to deal with
56  * backquotes, shell variables, and file metacharacters.
57  */
58 
59 #include "shell.h"
60 #include "main.h"
61 #include "nodes.h"
62 #include "eval.h"
63 #include "expand.h"
64 #include "syntax.h"
65 #include "parser.h"
66 #include "jobs.h"
67 #include "options.h"
68 #include "var.h"
69 #include "input.h"
70 #include "output.h"
71 #include "memalloc.h"
72 #include "error.h"
73 #include "mystring.h"
74 #include "show.h"
75 
76 /*
77  * Structure specifying which parts of the string should be searched
78  * for IFS characters.
79  */
80 
81 struct ifsregion {
82 	struct ifsregion *next;	/* next region in list */
83 	int begoff;		/* offset of start of region */
84 	int endoff;		/* offset of end of region */
85 	int inquotes;		/* search for nul bytes only */
86 };
87 
88 
89 char *expdest;			/* output of current string */
90 struct nodelist *argbackq;	/* list of back quote expressions */
91 struct ifsregion ifsfirst;	/* first struct in list of ifs regions */
92 struct ifsregion *ifslastp;	/* last struct in list */
93 struct arglist exparg;		/* holds expanded arg list */
94 
95 STATIC void argstr(char *, int);
96 STATIC char *exptilde(char *, int);
97 STATIC void expbackq(union node *, int, int);
98 STATIC int subevalvar(char *, char *, int, int, int, int);
99 STATIC char *evalvar(char *, int);
100 STATIC int varisset(char *, int);
101 STATIC void varvalue(char *, int, int, int);
102 STATIC void recordregion(int, int, int);
103 STATIC void removerecordregions(int);
104 STATIC void ifsbreakup(char *, struct arglist *);
105 STATIC void ifsfree(void);
106 STATIC void expandmeta(struct strlist *, int);
107 STATIC void expmeta(char *, char *);
108 STATIC void addfname(char *);
109 STATIC struct strlist *expsort(struct strlist *);
110 STATIC struct strlist *msort(struct strlist *, int);
111 STATIC int pmatch(char *, char *, int);
112 STATIC char *cvtnum(int, char *);
113 
114 /*
115  * Expand shell variables and backquotes inside a here document.
116  */
117 
118 void
119 expandhere(union node *arg, int fd)
120 {
121 	herefd = fd;
122 	expandarg(arg, (struct arglist *)NULL, 0);
123 	xwrite(fd, stackblock(), expdest - stackblock());
124 }
125 
126 
127 /*
128  * Perform variable substitution and command substitution on an argument,
129  * placing the resulting list of arguments in arglist.  If EXP_FULL is true,
130  * perform splitting and file name expansion.  When arglist is NULL, perform
131  * here document expansion.
132  */
133 
134 void
135 expandarg(union node *arg, struct arglist *arglist, int flag)
136 {
137 	struct strlist *sp;
138 	char *p;
139 
140 	argbackq = arg->narg.backquote;
141 	STARTSTACKSTR(expdest);
142 	ifsfirst.next = NULL;
143 	ifslastp = NULL;
144 	argstr(arg->narg.text, flag);
145 	if (arglist == NULL) {
146 		return;			/* here document expanded */
147 	}
148 	STPUTC('\0', expdest);
149 	p = grabstackstr(expdest);
150 	exparg.lastp = &exparg.list;
151 	/*
152 	 * TODO - EXP_REDIR
153 	 */
154 	if (flag & EXP_FULL) {
155 		ifsbreakup(p, &exparg);
156 		*exparg.lastp = NULL;
157 		exparg.lastp = &exparg.list;
158 		expandmeta(exparg.list, flag);
159 	} else {
160 		if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
161 			rmescapes(p);
162 		sp = (struct strlist *)stalloc(sizeof (struct strlist));
163 		sp->text = p;
164 		*exparg.lastp = sp;
165 		exparg.lastp = &sp->next;
166 	}
167 	ifsfree();
168 	*exparg.lastp = NULL;
169 	if (exparg.list) {
170 		*arglist->lastp = exparg.list;
171 		arglist->lastp = exparg.lastp;
172 	}
173 }
174 
175 
176 
177 /*
178  * Perform variable and command substitution.
179  * If EXP_FULL is set, output CTLESC characters to allow for further processing.
180  * Otherwise treat $@ like $* since no splitting will be performed.
181  */
182 
183 STATIC void
184 argstr(char *p, int flag)
185 {
186 	char c;
187 	int quotes = flag & (EXP_FULL | EXP_CASE);	/* do CTLESC */
188 	int firsteq = 1;
189 	const char *ifs;
190 	int ifs_split = EXP_IFS_SPLIT;
191 
192 	if (flag & EXP_IFS_SPLIT)
193 		ifs = ifsset() ? ifsval() : " \t\n";
194 
195 	if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
196 		p = exptilde(p, flag);
197 	for (;;) {
198 		switch (c = *p++) {
199 		case '\0':
200 		case CTLENDVAR: /* end of expanding yyy in ${xxx-yyy} */
201 			return;
202 		case CTLQUOTEMARK:
203 			/* "$@" syntax adherence hack */
204 			if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
205 				break;
206 			if ((flag & EXP_FULL) != 0)
207 				STPUTC(c, expdest);
208 			ifs_split = 0;
209 			break;
210 		case CTLQUOTEEND:
211 			ifs_split = EXP_IFS_SPLIT;
212 			break;
213 		case CTLESC:
214 			if (quotes)
215 				STPUTC(c, expdest);
216 			c = *p++;
217 			STPUTC(c, expdest);
218 			break;
219 		case CTLVAR:
220 			p = evalvar(p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split));
221 			break;
222 		case CTLBACKQ:
223 		case CTLBACKQ|CTLQUOTE:
224 			expbackq(argbackq->n, c & CTLQUOTE, flag);
225 			argbackq = argbackq->next;
226 			break;
227 		case CTLENDARI:
228 			expari(flag);
229 			break;
230 		case ':':
231 		case '=':
232 			/*
233 			 * sort of a hack - expand tildes in variable
234 			 * assignments (after the first '=' and after ':'s).
235 			 */
236 			STPUTC(c, expdest);
237 			if (flag & EXP_VARTILDE && *p == '~') {
238 				if (c == '=') {
239 					if (firsteq)
240 						firsteq = 0;
241 					else
242 						break;
243 				}
244 				p = exptilde(p, flag);
245 			}
246 			break;
247 		default:
248 			STPUTC(c, expdest);
249 			if (flag & EXP_IFS_SPLIT & ifs_split && strchr(ifs, c) != NULL) {
250 				/* We need to get the output split here... */
251 				recordregion(expdest - stackblock() - 1,
252 						expdest - stackblock(), 0);
253 			}
254 			break;
255 		}
256 	}
257 }
258 
259 STATIC char *
260 exptilde(char *p, int flag)
261 {
262 	char c, *startp = p;
263 	struct passwd *pw;
264 	const char *home;
265 	int quotes = flag & (EXP_FULL | EXP_CASE);
266 
267 	while ((c = *p) != '\0') {
268 		switch(c) {
269 		case CTLESC:
270 			return (startp);
271 		case CTLQUOTEMARK:
272 			return (startp);
273 		case ':':
274 			if (flag & EXP_VARTILDE)
275 				goto done;
276 			break;
277 		case '/':
278 			goto done;
279 		}
280 		p++;
281 	}
282 done:
283 	*p = '\0';
284 	if (*(startp+1) == '\0') {
285 		if ((home = lookupvar("HOME")) == NULL)
286 			goto lose;
287 	} else {
288 		if ((pw = getpwnam(startp+1)) == NULL)
289 			goto lose;
290 		home = pw->pw_dir;
291 	}
292 	if (*home == '\0')
293 		goto lose;
294 	*p = c;
295 	while ((c = *home++) != '\0') {
296 		if (quotes && SQSYNTAX[(int)c] == CCTL)
297 			STPUTC(CTLESC, expdest);
298 		STPUTC(c, expdest);
299 	}
300 	return (p);
301 lose:
302 	*p = c;
303 	return (startp);
304 }
305 
306 
307 STATIC void
308 removerecordregions(int endoff)
309 {
310 	if (ifslastp == NULL)
311 		return;
312 
313 	if (ifsfirst.endoff > endoff) {
314 		while (ifsfirst.next != NULL) {
315 			struct ifsregion *ifsp;
316 			INTOFF;
317 			ifsp = ifsfirst.next->next;
318 			ckfree(ifsfirst.next);
319 			ifsfirst.next = ifsp;
320 			INTON;
321 		}
322 		if (ifsfirst.begoff > endoff)
323 			ifslastp = NULL;
324 		else {
325 			ifslastp = &ifsfirst;
326 			ifsfirst.endoff = endoff;
327 		}
328 		return;
329 	}
330 
331 	ifslastp = &ifsfirst;
332 	while (ifslastp->next && ifslastp->next->begoff < endoff)
333 		ifslastp=ifslastp->next;
334 	while (ifslastp->next != NULL) {
335 		struct ifsregion *ifsp;
336 		INTOFF;
337 		ifsp = ifslastp->next->next;
338 		ckfree(ifslastp->next);
339 		ifslastp->next = ifsp;
340 		INTON;
341 	}
342 	if (ifslastp->endoff > endoff)
343 		ifslastp->endoff = endoff;
344 }
345 
346 
347 /*
348  * Expand arithmetic expression.  Backup to start of expression,
349  * evaluate, place result in (backed up) result, adjust string position.
350  */
351 void
352 expari(int flag)
353 {
354 	char *p, *start;
355 	int result;
356 	int begoff;
357 	int quotes = flag & (EXP_FULL | EXP_CASE);
358 	int quoted;
359 
360 	/*	ifsfree(); */
361 
362 	/*
363 	 * This routine is slightly over-complicated for
364 	 * efficiency.  First we make sure there is
365 	 * enough space for the result, which may be bigger
366 	 * than the expression if we add exponentation.  Next we
367 	 * scan backwards looking for the start of arithmetic.  If the
368 	 * next previous character is a CTLESC character, then we
369 	 * have to rescan starting from the beginning since CTLESC
370 	 * characters have to be processed left to right.
371 	 */
372 #if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10
373 #error "integers with more than 10 digits are not supported"
374 #endif
375 	CHECKSTRSPACE(12 - 2, expdest);
376 	USTPUTC('\0', expdest);
377 	start = stackblock();
378 	p = expdest - 1;
379 	while (*p != CTLARI && p >= start)
380 		--p;
381 	if (*p != CTLARI)
382 		error("missing CTLARI (shouldn't happen)");
383 	if (p > start && *(p-1) == CTLESC)
384 		for (p = start; *p != CTLARI; p++)
385 			if (*p == CTLESC)
386 				p++;
387 
388 	if (p[1] == '"')
389 		quoted=1;
390 	else
391 		quoted=0;
392 	begoff = p - start;
393 	removerecordregions(begoff);
394 	if (quotes)
395 		rmescapes(p+2);
396 	result = arith(p+2);
397 	fmtstr(p, 12, "%d", result);
398 
399 	while (*p++)
400 		;
401 
402 	if (quoted == 0)
403 		recordregion(begoff, p - 1 - start, 0);
404 	result = expdest - p + 1;
405 	STADJUST(-result, expdest);
406 }
407 
408 
409 /*
410  * Expand stuff in backwards quotes.
411  */
412 
413 STATIC void
414 expbackq(union node *cmd, int quoted, int flag)
415 {
416 	struct backcmd in;
417 	int i;
418 	char buf[128];
419 	char *p;
420 	char *dest = expdest;
421 	struct ifsregion saveifs, *savelastp;
422 	struct nodelist *saveargbackq;
423 	char lastc;
424 	int startloc = dest - stackblock();
425 	char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
426 	int saveherefd;
427 	int quotes = flag & (EXP_FULL | EXP_CASE);
428 
429 	INTOFF;
430 	saveifs = ifsfirst;
431 	savelastp = ifslastp;
432 	saveargbackq = argbackq;
433 	saveherefd = herefd;
434 	herefd = -1;
435 	p = grabstackstr(dest);
436 	evalbackcmd(cmd, &in);
437 	ungrabstackstr(p, dest);
438 	ifsfirst = saveifs;
439 	ifslastp = savelastp;
440 	argbackq = saveargbackq;
441 	herefd = saveherefd;
442 
443 	p = in.buf;
444 	lastc = '\0';
445 	for (;;) {
446 		if (--in.nleft < 0) {
447 			if (in.fd < 0)
448 				break;
449 			while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
450 			TRACE(("expbackq: read returns %d\n", i));
451 			if (i <= 0)
452 				break;
453 			p = buf;
454 			in.nleft = i - 1;
455 		}
456 		lastc = *p++;
457 		if (lastc != '\0') {
458 			if (quotes && syntax[(int)lastc] == CCTL)
459 				STPUTC(CTLESC, dest);
460 			STPUTC(lastc, dest);
461 		}
462 	}
463 
464 	/* Eat all trailing newlines */
465 	p = stackblock() + startloc;
466 	while (dest > p && dest[-1] == '\n')
467 		STUNPUTC(dest);
468 
469 	if (in.fd >= 0)
470 		close(in.fd);
471 	if (in.buf)
472 		ckfree(in.buf);
473 	if (in.jp)
474 		back_exitstatus = waitforjob(in.jp);
475 	if (quoted == 0)
476 		recordregion(startloc, dest - stackblock(), 0);
477 	TRACE(("evalbackq: size=%d: \"%.*s\"\n",
478 		(dest - stackblock()) - startloc,
479 		(dest - stackblock()) - startloc,
480 		stackblock() + startloc));
481 	expdest = dest;
482 	INTON;
483 }
484 
485 
486 
487 STATIC int
488 subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags)
489 {
490 	char *startp;
491 	char *loc = NULL;
492 	char *q;
493 	int c = 0;
494 	int saveherefd = herefd;
495 	struct nodelist *saveargbackq = argbackq;
496 	int amount;
497 
498 	herefd = -1;
499 	argstr(p, 0);
500 	STACKSTRNUL(expdest);
501 	herefd = saveherefd;
502 	argbackq = saveargbackq;
503 	startp = stackblock() + startloc;
504 	if (str == NULL)
505 	    str = stackblock() + strloc;
506 
507 	switch (subtype) {
508 	case VSASSIGN:
509 		setvar(str, startp, 0);
510 		amount = startp - expdest;
511 		STADJUST(amount, expdest);
512 		varflags &= ~VSNUL;
513 		if (c != 0)
514 			*loc = c;
515 		return 1;
516 
517 	case VSQUESTION:
518 		if (*p != CTLENDVAR) {
519 			outfmt(&errout, "%s\n", startp);
520 			error((char *)NULL);
521 		}
522 		error("%.*s: parameter %snot set", p - str - 1,
523 		      str, (varflags & VSNUL) ? "null or "
524 					      : nullstr);
525 		/* NOTREACHED */
526 
527 	case VSTRIMLEFT:
528 		for (loc = startp; loc < str; loc++) {
529 			c = *loc;
530 			*loc = '\0';
531 			if (patmatch(str, startp, varflags & VSQUOTE))
532 				goto recordleft;
533 			*loc = c;
534 			if ((varflags & VSQUOTE) && *loc == CTLESC)
535 			        loc++;
536 		}
537 		return 0;
538 
539 	case VSTRIMLEFTMAX:
540 		for (loc = str - 1; loc >= startp;) {
541 			c = *loc;
542 			*loc = '\0';
543 			if (patmatch(str, startp, varflags & VSQUOTE))
544 				goto recordleft;
545 			*loc = c;
546 			loc--;
547 			if ((varflags & VSQUOTE) && loc > startp &&
548 			    *(loc - 1) == CTLESC) {
549 				for (q = startp; q < loc; q++)
550 					if (*q == CTLESC)
551 						q++;
552 				if (q > loc)
553 					loc--;
554 			}
555 		}
556 		return 0;
557 
558 	case VSTRIMRIGHT:
559 	        for (loc = str - 1; loc >= startp;) {
560 			if (patmatch(str, loc, varflags & VSQUOTE))
561 				goto recordright;
562 			loc--;
563 			if ((varflags & VSQUOTE) && loc > startp &&
564 			    *(loc - 1) == CTLESC) {
565 				for (q = startp; q < loc; q++)
566 					if (*q == CTLESC)
567 						q++;
568 				if (q > loc)
569 					loc--;
570 			}
571 		}
572 		return 0;
573 
574 	case VSTRIMRIGHTMAX:
575 		for (loc = startp; loc < str - 1; loc++) {
576 			if (patmatch(str, loc, varflags & VSQUOTE))
577 				goto recordright;
578 			if ((varflags & VSQUOTE) && *loc == CTLESC)
579 			        loc++;
580 		}
581 		return 0;
582 
583 	default:
584 		abort();
585 	}
586 
587 recordleft:
588 	*loc = c;
589 	amount = ((str - 1) - (loc - startp)) - expdest;
590 	STADJUST(amount, expdest);
591 	while (loc != str - 1)
592 		*startp++ = *loc++;
593 	return 1;
594 
595 recordright:
596 	amount = loc - expdest;
597 	STADJUST(amount, expdest);
598 	STPUTC('\0', expdest);
599 	STADJUST(-1, expdest);
600 	return 1;
601 }
602 
603 
604 /*
605  * Expand a variable, and return a pointer to the next character in the
606  * input string.
607  */
608 
609 STATIC char *
610 evalvar(char *p, int flag)
611 {
612 	int subtype;
613 	int varflags;
614 	char *var;
615 	char *val;
616 	int patloc;
617 	int c;
618 	int set;
619 	int special;
620 	int startloc;
621 	int varlen;
622 	int apply_ifs;
623 	int quotes = flag & (EXP_FULL | EXP_CASE);
624 
625 	varflags = (unsigned char)*p++;
626 	subtype = varflags & VSTYPE;
627 	var = p;
628 	special = !is_name(*p);
629 	p = strchr(p, '=') + 1;
630 
631 again: /* jump here after setting a variable with ${var=text} */
632 	if (special) {
633 		set = varisset(var, varflags & VSNUL);
634 		val = NULL;
635 	} else {
636 		val = lookupvar(var);
637 		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
638 			val = NULL;
639 			set = 0;
640 		} else
641 			set = 1;
642 	}
643 
644 	varlen = 0;
645 	startloc = expdest - stackblock();
646 
647 	if (!set && uflag) {
648 		switch (subtype) {
649 		case VSNORMAL:
650 		case VSTRIMLEFT:
651 		case VSTRIMLEFTMAX:
652 		case VSTRIMRIGHT:
653 		case VSTRIMRIGHTMAX:
654 		case VSLENGTH:
655 			error("%.*s: parameter not set", p - var - 1, var);
656 			/* NOTREACHED */
657 		}
658 	}
659 
660 	if (set && subtype != VSPLUS) {
661 		/* insert the value of the variable */
662 		if (special) {
663 			varvalue(var, varflags & VSQUOTE, subtype, flag);
664 			if (subtype == VSLENGTH) {
665 				varlen = expdest - stackblock() - startloc;
666 				STADJUST(-varlen, expdest);
667 			}
668 		} else {
669 			char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
670 								  : BASESYNTAX;
671 
672 			if (subtype == VSLENGTH) {
673 				for (;*val; val++)
674 					varlen++;
675 			} else {
676 				while (*val) {
677 					if (quotes && syntax[(int)*val] == CCTL)
678 						STPUTC(CTLESC, expdest);
679 					STPUTC(*val++, expdest);
680 				}
681 
682 			}
683 		}
684 	}
685 
686 
687 	apply_ifs = ((varflags & VSQUOTE) == 0 ||
688 		(*var == '@' && shellparam.nparam != 1));
689 
690 	switch (subtype) {
691 	case VSLENGTH:
692 		expdest = cvtnum(varlen, expdest);
693 		break;
694 
695 	case VSNORMAL:
696 		break;
697 
698 	case VSPLUS:
699 		set = !set;
700 		/* FALLTHROUGH */
701 	case VSMINUS:
702 		if (!set) {
703 		        argstr(p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0));
704 			/*
705 			 * ${x-a b c} doesn't get split, but removing the
706 			 * 'apply_ifs = 0' apparantly breaks ${1+"$@"}..
707 			 * ${x-'a b' c} should generate 2 args.
708 			 */
709 			/* We should have marked stuff already */
710 			apply_ifs = 0;
711 		}
712 		break;
713 
714 	case VSTRIMLEFT:
715 	case VSTRIMLEFTMAX:
716 	case VSTRIMRIGHT:
717 	case VSTRIMRIGHTMAX:
718 		if (!set)
719 			break;
720 		/*
721 		 * Terminate the string and start recording the pattern
722 		 * right after it
723 		 */
724 		STPUTC('\0', expdest);
725 		patloc = expdest - stackblock();
726 		if (subevalvar(p, NULL, patloc, subtype,
727 			       startloc, varflags) == 0) {
728 			int amount = (expdest - stackblock() - patloc) + 1;
729 			STADJUST(-amount, expdest);
730 		}
731 		/* Remove any recorded regions beyond start of variable */
732 		removerecordregions(startloc);
733 		apply_ifs = 1;
734 		break;
735 
736 	case VSASSIGN:
737 	case VSQUESTION:
738 		if (set)
739 			break;
740 		if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
741 			varflags &= ~VSNUL;
742 			/*
743 			 * Remove any recorded regions beyond
744 			 * start of variable
745 			 */
746 			removerecordregions(startloc);
747 			goto again;
748 		}
749 		apply_ifs = 0;
750 		break;
751 
752 	default:
753 		abort();
754 	}
755 
756 	if (apply_ifs)
757 		recordregion(startloc, expdest - stackblock(),
758 			     varflags & VSQUOTE);
759 
760 	if (subtype != VSNORMAL) {	/* skip to end of alternative */
761 		int nesting = 1;
762 		for (;;) {
763 			if ((c = *p++) == CTLESC)
764 				p++;
765 			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
766 				if (set)
767 					argbackq = argbackq->next;
768 			} else if (c == CTLVAR) {
769 				if ((*p++ & VSTYPE) != VSNORMAL)
770 					nesting++;
771 			} else if (c == CTLENDVAR) {
772 				if (--nesting == 0)
773 					break;
774 			}
775 		}
776 	}
777 	return p;
778 }
779 
780 
781 
782 /*
783  * Test whether a specialized variable is set.
784  */
785 
786 STATIC int
787 varisset(char *name, int nulok)
788 {
789 	if (*name == '!')
790 		return backgndpid != -1;
791 	else if (*name == '@' || *name == '*') {
792 		if (*shellparam.p == NULL)
793 			return 0;
794 
795 		if (nulok) {
796 			char **av;
797 
798 			for (av = shellparam.p; *av; av++)
799 				if (**av != '\0')
800 					return 1;
801 			return 0;
802 		}
803 	} else if (is_digit(*name)) {
804 		char *ap;
805 		int num = atoi(name);
806 
807 		if (num > shellparam.nparam)
808 			return 0;
809 
810 		if (num == 0)
811 			ap = arg0;
812 		else
813 			ap = shellparam.p[num - 1];
814 
815 		if (nulok && (ap == NULL || *ap == '\0'))
816 			return 0;
817 	}
818 	return 1;
819 }
820 
821 
822 
823 /*
824  * Add the value of a specialized variable to the stack string.
825  */
826 
827 STATIC void
828 varvalue(char *name, int quoted, int subtype, int flag)
829 {
830 	int num;
831 	char *p;
832 	int i;
833 	char sep;
834 	char **ap;
835 	char const *syntax;
836 
837 #define STRTODEST(p) \
838 	do {\
839 	if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
840 		syntax = quoted? DQSYNTAX : BASESYNTAX; \
841 		while (*p) { \
842 			if (syntax[(int)*p] == CCTL) \
843 				STPUTC(CTLESC, expdest); \
844 			STPUTC(*p++, expdest); \
845 		} \
846 	} else \
847 		while (*p) \
848 			STPUTC(*p++, expdest); \
849 	} while (0)
850 
851 
852 	switch (*name) {
853 	case '$':
854 		num = rootpid;
855 		goto numvar;
856 	case '?':
857 		num = exitstatus;
858 		goto numvar;
859 	case '#':
860 		num = shellparam.nparam;
861 		goto numvar;
862 	case '!':
863 		num = backgndpid;
864 numvar:
865 		expdest = cvtnum(num, expdest);
866 		break;
867 	case '-':
868 		for (i = 0; optlist[i].name; i++) {
869 			if (optlist[i].val)
870 				STPUTC(optlist[i].letter, expdest);
871 		}
872 		break;
873 	case '@':
874 		if (flag & EXP_FULL && quoted) {
875 			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
876 				STRTODEST(p);
877 				if (*ap)
878 					STPUTC('\0', expdest);
879 			}
880 			break;
881 		}
882 		/* fall through */
883 	case '*':
884 		if (ifsset() != 0)
885 			sep = ifsval()[0];
886 		else
887 			sep = ' ';
888 		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
889 			STRTODEST(p);
890 			if (*ap && sep)
891 				STPUTC(sep, expdest);
892 		}
893 		break;
894 	case '0':
895 		p = arg0;
896 		STRTODEST(p);
897 		break;
898 	default:
899 		if (is_digit(*name)) {
900 			num = atoi(name);
901 			if (num > 0 && num <= shellparam.nparam) {
902 				p = shellparam.p[num - 1];
903 				STRTODEST(p);
904 			}
905 		}
906 		break;
907 	}
908 }
909 
910 
911 
912 /*
913  * Record the fact that we have to scan this region of the
914  * string for IFS characters.
915  */
916 
917 STATIC void
918 recordregion(int start, int end, int inquotes)
919 {
920 	struct ifsregion *ifsp;
921 
922 	if (ifslastp == NULL) {
923 		ifsp = &ifsfirst;
924 	} else {
925 		if (ifslastp->endoff == start) {
926 			/* extend previous area */
927 			ifslastp->endoff = end;
928 			return;
929 		}
930 		ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
931 		ifslastp->next = ifsp;
932 	}
933 	ifslastp = ifsp;
934 	ifslastp->next = NULL;
935 	ifslastp->begoff = start;
936 	ifslastp->endoff = end;
937 	ifslastp->inquotes = inquotes;
938 }
939 
940 
941 
942 /*
943  * Break the argument string into pieces based upon IFS and add the
944  * strings to the argument list.  The regions of the string to be
945  * searched for IFS characters have been stored by recordregion.
946  */
947 STATIC void
948 ifsbreakup(char *string, struct arglist *arglist)
949 {
950 	struct ifsregion *ifsp;
951 	struct strlist *sp;
952 	char *start;
953 	char *p;
954 	char *q;
955 	const char *ifs;
956 	const char *ifsspc;
957 	int inquotes;
958 
959 	start = string;
960 	ifsspc = NULL;
961 	inquotes = 0;
962 
963 	if (ifslastp == NULL) {
964 		/* Return entire argument, IFS doesn't apply to any of it */
965 		sp = (struct strlist *)stalloc(sizeof *sp);
966 		sp->text = start;
967 		*arglist->lastp = sp;
968 		arglist->lastp = &sp->next;
969 		return;
970 	}
971 
972 	ifs = ifsset() ? ifsval() : " \t\n";
973 
974 	for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
975 		p = string + ifsp->begoff;
976 		inquotes = ifsp->inquotes;
977 		ifsspc = NULL;
978 		while (p < string + ifsp->endoff) {
979 			q = p;
980 			if (*p == CTLESC)
981 				p++;
982 			if (inquotes) {
983 				/* Only NULs (probably from "$@") end args */
984 				if (*p != 0) {
985 					p++;
986 					continue;
987 				}
988 			} else {
989 				if (!strchr(ifs, *p)) {
990 					p++;
991 					continue;
992 				}
993 				ifsspc = strchr(" \t\n", *p);
994 
995 				/* Ignore IFS whitespace at start */
996 				if (q == start && ifsspc != NULL) {
997 					p++;
998 					start = p;
999 					continue;
1000 				}
1001 			}
1002 
1003 			/* Save this argument... */
1004 			*q = '\0';
1005 			sp = (struct strlist *)stalloc(sizeof *sp);
1006 			sp->text = start;
1007 			*arglist->lastp = sp;
1008 			arglist->lastp = &sp->next;
1009 			p++;
1010 
1011 			if (!inquotes) {
1012 				/* Ignore trailing IFS writespace */
1013 				for (; p < string + ifsp->endoff; p++) {
1014 					q = p;
1015 					if (*p == CTLESC)
1016 						p++;
1017 					if (strchr(ifs, *p) == NULL) {
1018 						p = q;
1019 						break;
1020 					}
1021 					if (strchr(" \t\n", *p) == NULL) {
1022 						if (ifsspc == NULL) {
1023 							p = q;
1024 							break;
1025 						}
1026 						ifsspc = NULL;
1027 					}
1028 				}
1029 			}
1030 			start = p;
1031 		}
1032 	}
1033 
1034 	/* Save anything left as an argument */
1035 	if (*start || (!ifsspc && start > string)) {
1036 		sp = (struct strlist *)stalloc(sizeof *sp);
1037 		sp->text = start;
1038 		*arglist->lastp = sp;
1039 		arglist->lastp = &sp->next;
1040 	}
1041 }
1042 
1043 STATIC void
1044 ifsfree(void)
1045 {
1046 	while (ifsfirst.next != NULL) {
1047 		struct ifsregion *ifsp;
1048 		INTOFF;
1049 		ifsp = ifsfirst.next->next;
1050 		ckfree(ifsfirst.next);
1051 		ifsfirst.next = ifsp;
1052 		INTON;
1053 	}
1054 	ifslastp = NULL;
1055 	ifsfirst.next = NULL;
1056 }
1057 
1058 
1059 
1060 /*
1061  * Expand shell metacharacters.  At this point, the only control characters
1062  * should be escapes.  The results are stored in the list exparg.
1063  */
1064 
1065 char *expdir;
1066 
1067 
1068 STATIC void
1069 expandmeta(struct strlist *str, int flag)
1070 {
1071 	char *p;
1072 	struct strlist **savelastp;
1073 	struct strlist *sp;
1074 	char c;
1075 	/* TODO - EXP_REDIR */
1076 
1077 	while (str) {
1078 		if (fflag)
1079 			goto nometa;
1080 		p = str->text;
1081 		for (;;) {			/* fast check for meta chars */
1082 			if ((c = *p++) == '\0')
1083 				goto nometa;
1084 			if (c == '*' || c == '?' || c == '[' || c == '!')
1085 				break;
1086 		}
1087 		savelastp = exparg.lastp;
1088 		INTOFF;
1089 		if (expdir == NULL) {
1090 			int i = strlen(str->text);
1091 			expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
1092 		}
1093 
1094 		expmeta(expdir, str->text);
1095 		ckfree(expdir);
1096 		expdir = NULL;
1097 		INTON;
1098 		if (exparg.lastp == savelastp) {
1099 			/*
1100 			 * no matches
1101 			 */
1102 nometa:
1103 			*exparg.lastp = str;
1104 			rmescapes(str->text);
1105 			exparg.lastp = &str->next;
1106 		} else {
1107 			*exparg.lastp = NULL;
1108 			*savelastp = sp = expsort(*savelastp);
1109 			while (sp->next != NULL)
1110 				sp = sp->next;
1111 			exparg.lastp = &sp->next;
1112 		}
1113 		str = str->next;
1114 	}
1115 }
1116 
1117 
1118 /*
1119  * Do metacharacter (i.e. *, ?, [...]) expansion.
1120  */
1121 
1122 STATIC void
1123 expmeta(char *enddir, char *name)
1124 {
1125 	char *p;
1126 	const char *cp;
1127 	char *q;
1128 	char *start;
1129 	char *endname;
1130 	int metaflag;
1131 	struct stat statb;
1132 	DIR *dirp;
1133 	struct dirent *dp;
1134 	int atend;
1135 	int matchdot;
1136 
1137 	metaflag = 0;
1138 	start = name;
1139 	for (p = name ; ; p++) {
1140 		if (*p == '*' || *p == '?')
1141 			metaflag = 1;
1142 		else if (*p == '[') {
1143 			q = p + 1;
1144 			if (*q == '!')
1145 				q++;
1146 			for (;;) {
1147 				while (*q == CTLQUOTEMARK)
1148 					q++;
1149 				if (*q == CTLESC)
1150 					q++;
1151 				if (*q == '/' || *q == '\0')
1152 					break;
1153 				if (*++q == ']') {
1154 					metaflag = 1;
1155 					break;
1156 				}
1157 			}
1158 		} else if (*p == '!' && p[1] == '!'	&& (p == name || p[-1] == '/')) {
1159 			metaflag = 1;
1160 		} else if (*p == '\0')
1161 			break;
1162 		else if (*p == CTLQUOTEMARK)
1163 			continue;
1164 		else if (*p == CTLESC)
1165 			p++;
1166 		if (*p == '/') {
1167 			if (metaflag)
1168 				break;
1169 			start = p + 1;
1170 		}
1171 	}
1172 	if (metaflag == 0) {	/* we've reached the end of the file name */
1173 		if (enddir != expdir)
1174 			metaflag++;
1175 		for (p = name ; ; p++) {
1176 			if (*p == CTLQUOTEMARK)
1177 				continue;
1178 			if (*p == CTLESC)
1179 				p++;
1180 			*enddir++ = *p;
1181 			if (*p == '\0')
1182 				break;
1183 		}
1184 		if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1185 			addfname(expdir);
1186 		return;
1187 	}
1188 	endname = p;
1189 	if (start != name) {
1190 		p = name;
1191 		while (p < start) {
1192 			while (*p == CTLQUOTEMARK)
1193 				p++;
1194 			if (*p == CTLESC)
1195 				p++;
1196 			*enddir++ = *p++;
1197 		}
1198 	}
1199 	if (enddir == expdir) {
1200 		cp = ".";
1201 	} else if (enddir == expdir + 1 && *expdir == '/') {
1202 		cp = "/";
1203 	} else {
1204 		cp = expdir;
1205 		enddir[-1] = '\0';
1206 	}
1207 	if ((dirp = opendir(cp)) == NULL)
1208 		return;
1209 	if (enddir != expdir)
1210 		enddir[-1] = '/';
1211 	if (*endname == 0) {
1212 		atend = 1;
1213 	} else {
1214 		atend = 0;
1215 		*endname++ = '\0';
1216 	}
1217 	matchdot = 0;
1218 	p = start;
1219 	while (*p == CTLQUOTEMARK)
1220 		p++;
1221 	if (*p == CTLESC)
1222 		p++;
1223 	if (*p == '.')
1224 		matchdot++;
1225 	while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1226 		if (dp->d_name[0] == '.' && ! matchdot)
1227 			continue;
1228 		if (patmatch(start, dp->d_name, 0)) {
1229 			if (atend) {
1230 				scopy(dp->d_name, enddir);
1231 				addfname(expdir);
1232 			} else {
1233 				for (p = enddir, cp = dp->d_name;
1234 				     (*p++ = *cp++) != '\0';)
1235 					continue;
1236 				p[-1] = '/';
1237 				expmeta(p, endname);
1238 			}
1239 		}
1240 	}
1241 	closedir(dirp);
1242 	if (! atend)
1243 		endname[-1] = '/';
1244 }
1245 
1246 
1247 /*
1248  * Add a file name to the list.
1249  */
1250 
1251 STATIC void
1252 addfname(char *name)
1253 {
1254 	char *p;
1255 	struct strlist *sp;
1256 
1257 	p = stalloc(strlen(name) + 1);
1258 	scopy(name, p);
1259 	sp = (struct strlist *)stalloc(sizeof *sp);
1260 	sp->text = p;
1261 	*exparg.lastp = sp;
1262 	exparg.lastp = &sp->next;
1263 }
1264 
1265 
1266 /*
1267  * Sort the results of file name expansion.  It calculates the number of
1268  * strings to sort and then calls msort (short for merge sort) to do the
1269  * work.
1270  */
1271 
1272 STATIC struct strlist *
1273 expsort(struct strlist *str)
1274 {
1275 	int len;
1276 	struct strlist *sp;
1277 
1278 	len = 0;
1279 	for (sp = str ; sp ; sp = sp->next)
1280 		len++;
1281 	return msort(str, len);
1282 }
1283 
1284 
1285 STATIC struct strlist *
1286 msort(struct strlist *list, int len)
1287 {
1288 	struct strlist *p, *q = NULL;
1289 	struct strlist **lpp;
1290 	int half;
1291 	int n;
1292 
1293 	if (len <= 1)
1294 		return list;
1295 	half = len >> 1;
1296 	p = list;
1297 	for (n = half ; --n >= 0 ; ) {
1298 		q = p;
1299 		p = p->next;
1300 	}
1301 	q->next = NULL;			/* terminate first half of list */
1302 	q = msort(list, half);		/* sort first half of list */
1303 	p = msort(p, len - half);		/* sort second half */
1304 	lpp = &list;
1305 	for (;;) {
1306 		if (strcmp(p->text, q->text) < 0) {
1307 			*lpp = p;
1308 			lpp = &p->next;
1309 			if ((p = *lpp) == NULL) {
1310 				*lpp = q;
1311 				break;
1312 			}
1313 		} else {
1314 			*lpp = q;
1315 			lpp = &q->next;
1316 			if ((q = *lpp) == NULL) {
1317 				*lpp = p;
1318 				break;
1319 			}
1320 		}
1321 	}
1322 	return list;
1323 }
1324 
1325 
1326 
1327 /*
1328  * Returns true if the pattern matches the string.
1329  */
1330 
1331 int
1332 patmatch(char *pattern, char *string, int squoted)
1333 {
1334 #ifdef notdef
1335 	if (pattern[0] == '!' && pattern[1] == '!')
1336 		return 1 - pmatch(pattern + 2, string);
1337 	else
1338 #endif
1339 		return pmatch(pattern, string, squoted);
1340 }
1341 
1342 
1343 STATIC int
1344 pmatch(char *pattern, char *string, int squoted)
1345 {
1346 	char *p, *q;
1347 	char c;
1348 
1349 	p = pattern;
1350 	q = string;
1351 	for (;;) {
1352 		switch (c = *p++) {
1353 		case '\0':
1354 			goto breakloop;
1355 		case CTLESC:
1356 			if (squoted && *q == CTLESC)
1357 				q++;
1358 			if (*q++ != *p++)
1359 				return 0;
1360 			break;
1361 		case CTLQUOTEMARK:
1362 			continue;
1363 		case '?':
1364 			if (squoted && *q == CTLESC)
1365 				q++;
1366 			if (*q++ == '\0')
1367 				return 0;
1368 			break;
1369 		case '*':
1370 			c = *p;
1371 			while (c == CTLQUOTEMARK || c == '*')
1372 				c = *++p;
1373 			if (c != CTLESC &&  c != CTLQUOTEMARK &&
1374 			    c != '?' && c != '*' && c != '[') {
1375 				while (*q != c) {
1376 					if (squoted && *q == CTLESC &&
1377 					    q[1] == c)
1378 						break;
1379 					if (*q == '\0')
1380 						return 0;
1381 					if (squoted && *q == CTLESC)
1382 						q++;
1383 					q++;
1384 				}
1385 			}
1386 			do {
1387 				if (pmatch(p, q, squoted))
1388 					return 1;
1389 				if (squoted && *q == CTLESC)
1390 					q++;
1391 			} while (*q++ != '\0');
1392 			return 0;
1393 		case '[': {
1394 			char *endp;
1395 			int invert, found;
1396 			char chr;
1397 
1398 			endp = p;
1399 			if (*endp == '!')
1400 				endp++;
1401 			for (;;) {
1402 				while (*endp == CTLQUOTEMARK)
1403 					endp++;
1404 				if (*endp == '\0')
1405 					goto dft;		/* no matching ] */
1406 				if (*endp == CTLESC)
1407 					endp++;
1408 				if (*++endp == ']')
1409 					break;
1410 			}
1411 			invert = 0;
1412 			if (*p == '!') {
1413 				invert++;
1414 				p++;
1415 			}
1416 			found = 0;
1417 			chr = *q++;
1418 			if (squoted && chr == CTLESC)
1419 				chr = *q++;
1420 			if (chr == '\0')
1421 				return 0;
1422 			c = *p++;
1423 			do {
1424 				if (c == CTLQUOTEMARK)
1425 					continue;
1426 				if (c == CTLESC)
1427 					c = *p++;
1428 				if (*p == '-' && p[1] != ']') {
1429 					p++;
1430 					while (*p == CTLQUOTEMARK)
1431 						p++;
1432 					if (*p == CTLESC)
1433 						p++;
1434 					if (chr >= c && chr <= *p)
1435 						found = 1;
1436 					p++;
1437 				} else {
1438 					if (chr == c)
1439 						found = 1;
1440 				}
1441 			} while ((c = *p++) != ']');
1442 			if (found == invert)
1443 				return 0;
1444 			break;
1445 		}
1446 dft:	        default:
1447 			if (squoted && *q == CTLESC)
1448 				q++;
1449 			if (*q++ != c)
1450 				return 0;
1451 			break;
1452 		}
1453 	}
1454 breakloop:
1455 	if (*q != '\0')
1456 		return 0;
1457 	return 1;
1458 }
1459 
1460 
1461 
1462 /*
1463  * Remove any CTLESC characters from a string.
1464  */
1465 
1466 void
1467 rmescapes(char *str)
1468 {
1469 	char *p, *q;
1470 
1471 	p = str;
1472 	while (*p != CTLESC && *p != CTLQUOTEMARK) {
1473 		if (*p++ == '\0')
1474 			return;
1475 	}
1476 	q = p;
1477 	while (*p) {
1478 		if (*p == CTLQUOTEMARK) {
1479 			p++;
1480 			continue;
1481 		}
1482 		if (*p == CTLESC)
1483 			p++;
1484 		*q++ = *p++;
1485 	}
1486 	*q = '\0';
1487 }
1488 
1489 
1490 
1491 /*
1492  * See if a pattern matches in a case statement.
1493  */
1494 
1495 int
1496 casematch(union node *pattern, char *val)
1497 {
1498 	struct stackmark smark;
1499 	int result;
1500 	char *p;
1501 
1502 	setstackmark(&smark);
1503 	argbackq = pattern->narg.backquote;
1504 	STARTSTACKSTR(expdest);
1505 	ifslastp = NULL;
1506 	argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1507 	STPUTC('\0', expdest);
1508 	p = grabstackstr(expdest);
1509 	result = patmatch(p, val, 0);
1510 	popstackmark(&smark);
1511 	return result;
1512 }
1513 
1514 /*
1515  * Our own itoa().
1516  */
1517 
1518 STATIC char *
1519 cvtnum(int num, char *buf)
1520 {
1521 	char temp[32];
1522 	int neg = num < 0;
1523 	char *p = temp + 31;
1524 
1525 	temp[31] = '\0';
1526 
1527 	do {
1528 		*--p = num % 10 + '0';
1529 	} while ((num /= 10) != 0);
1530 
1531 	if (neg)
1532 		*--p = '-';
1533 
1534 	while (*p)
1535 		STPUTC(*p++, buf);
1536 	return buf;
1537 }
1538 
1539 /*
1540  * Do most of the work for wordexp(3).
1541  */
1542 
1543 int
1544 wordexpcmd(int argc, char **argv)
1545 {
1546 	size_t len;
1547 	int i;
1548 
1549 	out1fmt("%d", argc - 1);
1550 	out1c('\0');
1551 	for (i = 1, len = 0; i < argc; i++)
1552 		len += strlen(argv[i]);
1553 	out1fmt("%zd", len);
1554 	out1c('\0');
1555 	for (i = 1; i < argc; i++) {
1556 		out1str(argv[i]);
1557 		out1c('\0');
1558 	}
1559 	return (0);
1560 }
1561