xref: /netbsd-src/bin/sh/expand.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: expand.c,v 1.74 2006/05/20 13:57:27 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.74 2006/05/20 13:57:27 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 = NULL;
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 		return 1;
514 
515 	case VSQUESTION:
516 		if (*p != CTLENDVAR) {
517 			outfmt(&errout, "%s\n", startp);
518 			error((char *)NULL);
519 		}
520 		error("%.*s: parameter %snot set", p - str - 1,
521 		      str, (varflags & VSNUL) ? "null or "
522 					      : nullstr);
523 		/* NOTREACHED */
524 
525 	case VSTRIMLEFT:
526 		for (loc = startp; loc < str; loc++) {
527 			c = *loc;
528 			*loc = '\0';
529 			if (patmatch(str, startp, varflags & VSQUOTE))
530 				goto recordleft;
531 			*loc = c;
532 			if ((varflags & VSQUOTE) && *loc == CTLESC)
533 			        loc++;
534 		}
535 		return 0;
536 
537 	case VSTRIMLEFTMAX:
538 		for (loc = str - 1; loc >= startp;) {
539 			c = *loc;
540 			*loc = '\0';
541 			if (patmatch(str, startp, varflags & VSQUOTE))
542 				goto recordleft;
543 			*loc = c;
544 			loc--;
545 			if ((varflags & VSQUOTE) && loc > startp &&
546 			    *(loc - 1) == CTLESC) {
547 				for (q = startp; q < loc; q++)
548 					if (*q == CTLESC)
549 						q++;
550 				if (q > loc)
551 					loc--;
552 			}
553 		}
554 		return 0;
555 
556 	case VSTRIMRIGHT:
557 	        for (loc = str - 1; loc >= startp;) {
558 			if (patmatch(str, loc, varflags & VSQUOTE))
559 				goto recordright;
560 			loc--;
561 			if ((varflags & VSQUOTE) && loc > startp &&
562 			    *(loc - 1) == CTLESC) {
563 				for (q = startp; q < loc; q++)
564 					if (*q == CTLESC)
565 						q++;
566 				if (q > loc)
567 					loc--;
568 			}
569 		}
570 		return 0;
571 
572 	case VSTRIMRIGHTMAX:
573 		for (loc = startp; loc < str - 1; loc++) {
574 			if (patmatch(str, loc, varflags & VSQUOTE))
575 				goto recordright;
576 			if ((varflags & VSQUOTE) && *loc == CTLESC)
577 			        loc++;
578 		}
579 		return 0;
580 
581 	default:
582 		abort();
583 	}
584 
585 recordleft:
586 	*loc = c;
587 	amount = ((str - 1) - (loc - startp)) - expdest;
588 	STADJUST(amount, expdest);
589 	while (loc != str - 1)
590 		*startp++ = *loc++;
591 	return 1;
592 
593 recordright:
594 	amount = loc - expdest;
595 	STADJUST(amount, expdest);
596 	STPUTC('\0', expdest);
597 	STADJUST(-1, expdest);
598 	return 1;
599 }
600 
601 
602 /*
603  * Expand a variable, and return a pointer to the next character in the
604  * input string.
605  */
606 
607 STATIC char *
608 evalvar(char *p, int flag)
609 {
610 	int subtype;
611 	int varflags;
612 	char *var;
613 	char *val;
614 	int patloc;
615 	int c;
616 	int set;
617 	int special;
618 	int startloc;
619 	int varlen;
620 	int apply_ifs;
621 	int quotes = flag & (EXP_FULL | EXP_CASE);
622 
623 	varflags = (unsigned char)*p++;
624 	subtype = varflags & VSTYPE;
625 	var = p;
626 	special = !is_name(*p);
627 	p = strchr(p, '=') + 1;
628 
629 again: /* jump here after setting a variable with ${var=text} */
630 	if (special) {
631 		set = varisset(var, varflags & VSNUL);
632 		val = NULL;
633 	} else {
634 		val = lookupvar(var);
635 		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
636 			val = NULL;
637 			set = 0;
638 		} else
639 			set = 1;
640 	}
641 
642 	varlen = 0;
643 	startloc = expdest - stackblock();
644 
645 	if (!set && uflag) {
646 		switch (subtype) {
647 		case VSNORMAL:
648 		case VSTRIMLEFT:
649 		case VSTRIMLEFTMAX:
650 		case VSTRIMRIGHT:
651 		case VSTRIMRIGHTMAX:
652 		case VSLENGTH:
653 			error("%.*s: parameter not set", p - var - 1, var);
654 			/* NOTREACHED */
655 		}
656 	}
657 
658 	if (set && subtype != VSPLUS) {
659 		/* insert the value of the variable */
660 		if (special) {
661 			varvalue(var, varflags & VSQUOTE, subtype, flag);
662 			if (subtype == VSLENGTH) {
663 				varlen = expdest - stackblock() - startloc;
664 				STADJUST(-varlen, expdest);
665 			}
666 		} else {
667 			char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
668 								  : BASESYNTAX;
669 
670 			if (subtype == VSLENGTH) {
671 				for (;*val; val++)
672 					varlen++;
673 			} else {
674 				while (*val) {
675 					if (quotes && syntax[(int)*val] == CCTL)
676 						STPUTC(CTLESC, expdest);
677 					STPUTC(*val++, expdest);
678 				}
679 
680 			}
681 		}
682 	}
683 
684 
685 	apply_ifs = ((varflags & VSQUOTE) == 0 ||
686 		(*var == '@' && shellparam.nparam != 1));
687 
688 	switch (subtype) {
689 	case VSLENGTH:
690 		expdest = cvtnum(varlen, expdest);
691 		break;
692 
693 	case VSNORMAL:
694 		break;
695 
696 	case VSPLUS:
697 		set = !set;
698 		/* FALLTHROUGH */
699 	case VSMINUS:
700 		if (!set) {
701 		        argstr(p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0));
702 			/*
703 			 * ${x-a b c} doesn't get split, but removing the
704 			 * 'apply_ifs = 0' apparantly breaks ${1+"$@"}..
705 			 * ${x-'a b' c} should generate 2 args.
706 			 */
707 			/* We should have marked stuff already */
708 			apply_ifs = 0;
709 		}
710 		break;
711 
712 	case VSTRIMLEFT:
713 	case VSTRIMLEFTMAX:
714 	case VSTRIMRIGHT:
715 	case VSTRIMRIGHTMAX:
716 		if (!set)
717 			break;
718 		/*
719 		 * Terminate the string and start recording the pattern
720 		 * right after it
721 		 */
722 		STPUTC('\0', expdest);
723 		patloc = expdest - stackblock();
724 		if (subevalvar(p, NULL, patloc, subtype,
725 			       startloc, varflags) == 0) {
726 			int amount = (expdest - stackblock() - patloc) + 1;
727 			STADJUST(-amount, expdest);
728 		}
729 		/* Remove any recorded regions beyond start of variable */
730 		removerecordregions(startloc);
731 		apply_ifs = 1;
732 		break;
733 
734 	case VSASSIGN:
735 	case VSQUESTION:
736 		if (set)
737 			break;
738 		if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
739 			varflags &= ~VSNUL;
740 			/*
741 			 * Remove any recorded regions beyond
742 			 * start of variable
743 			 */
744 			removerecordregions(startloc);
745 			goto again;
746 		}
747 		apply_ifs = 0;
748 		break;
749 
750 	default:
751 		abort();
752 	}
753 
754 	if (apply_ifs)
755 		recordregion(startloc, expdest - stackblock(),
756 			     varflags & VSQUOTE);
757 
758 	if (subtype != VSNORMAL) {	/* skip to end of alternative */
759 		int nesting = 1;
760 		for (;;) {
761 			if ((c = *p++) == CTLESC)
762 				p++;
763 			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
764 				if (set)
765 					argbackq = argbackq->next;
766 			} else if (c == CTLVAR) {
767 				if ((*p++ & VSTYPE) != VSNORMAL)
768 					nesting++;
769 			} else if (c == CTLENDVAR) {
770 				if (--nesting == 0)
771 					break;
772 			}
773 		}
774 	}
775 	return p;
776 }
777 
778 
779 
780 /*
781  * Test whether a specialized variable is set.
782  */
783 
784 STATIC int
785 varisset(char *name, int nulok)
786 {
787 	if (*name == '!')
788 		return backgndpid != -1;
789 	else if (*name == '@' || *name == '*') {
790 		if (*shellparam.p == NULL)
791 			return 0;
792 
793 		if (nulok) {
794 			char **av;
795 
796 			for (av = shellparam.p; *av; av++)
797 				if (**av != '\0')
798 					return 1;
799 			return 0;
800 		}
801 	} else if (is_digit(*name)) {
802 		char *ap;
803 		int num = atoi(name);
804 
805 		if (num > shellparam.nparam)
806 			return 0;
807 
808 		if (num == 0)
809 			ap = arg0;
810 		else
811 			ap = shellparam.p[num - 1];
812 
813 		if (nulok && (ap == NULL || *ap == '\0'))
814 			return 0;
815 	}
816 	return 1;
817 }
818 
819 
820 
821 /*
822  * Add the value of a specialized variable to the stack string.
823  */
824 
825 STATIC void
826 varvalue(char *name, int quoted, int subtype, int flag)
827 {
828 	int num;
829 	char *p;
830 	int i;
831 	char sep;
832 	char **ap;
833 	char const *syntax;
834 
835 #define STRTODEST(p) \
836 	do {\
837 	if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
838 		syntax = quoted? DQSYNTAX : BASESYNTAX; \
839 		while (*p) { \
840 			if (syntax[(int)*p] == CCTL) \
841 				STPUTC(CTLESC, expdest); \
842 			STPUTC(*p++, expdest); \
843 		} \
844 	} else \
845 		while (*p) \
846 			STPUTC(*p++, expdest); \
847 	} while (0)
848 
849 
850 	switch (*name) {
851 	case '$':
852 		num = rootpid;
853 		goto numvar;
854 	case '?':
855 		num = exitstatus;
856 		goto numvar;
857 	case '#':
858 		num = shellparam.nparam;
859 		goto numvar;
860 	case '!':
861 		num = backgndpid;
862 numvar:
863 		expdest = cvtnum(num, expdest);
864 		break;
865 	case '-':
866 		for (i = 0; optlist[i].name; i++) {
867 			if (optlist[i].val && optlist[i].letter)
868 				STPUTC(optlist[i].letter, expdest);
869 		}
870 		break;
871 	case '@':
872 		if (flag & EXP_FULL && quoted) {
873 			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
874 				STRTODEST(p);
875 				/* Nul forces a parameter split inside "" */
876 				STPUTC('\0', expdest);
877 			}
878 			break;
879 		}
880 		/* fall through */
881 	case '*':
882 		if (ifsset() != 0)
883 			sep = ifsval()[0];
884 		else
885 			sep = ' ';
886 		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
887 			STRTODEST(p);
888 			if (*ap && sep)
889 				STPUTC(sep, expdest);
890 		}
891 		break;
892 	case '0':
893 		p = arg0;
894 		STRTODEST(p);
895 		break;
896 	default:
897 		if (is_digit(*name)) {
898 			num = atoi(name);
899 			if (num > 0 && num <= shellparam.nparam) {
900 				p = shellparam.p[num - 1];
901 				STRTODEST(p);
902 			}
903 		}
904 		break;
905 	}
906 }
907 
908 
909 
910 /*
911  * Record the fact that we have to scan this region of the
912  * string for IFS characters.
913  */
914 
915 STATIC void
916 recordregion(int start, int end, int inquotes)
917 {
918 	struct ifsregion *ifsp;
919 
920 	if (ifslastp == NULL) {
921 		ifsp = &ifsfirst;
922 	} else {
923 		if (ifslastp->endoff == start
924 		    && ifslastp->inquotes == inquotes) {
925 			/* extend previous area */
926 			ifslastp->endoff = end;
927 			return;
928 		}
929 		ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
930 		ifslastp->next = ifsp;
931 	}
932 	ifslastp = ifsp;
933 	ifslastp->next = NULL;
934 	ifslastp->begoff = start;
935 	ifslastp->endoff = end;
936 	ifslastp->inquotes = inquotes;
937 }
938 
939 
940 
941 /*
942  * Break the argument string into pieces based upon IFS and add the
943  * strings to the argument list.  The regions of the string to be
944  * searched for IFS characters have been stored by recordregion.
945  */
946 STATIC void
947 ifsbreakup(char *string, struct arglist *arglist)
948 {
949 	struct ifsregion *ifsp;
950 	struct strlist *sp;
951 	char *start;
952 	char *p;
953 	char *q;
954 	const char *ifs;
955 	const char *ifsspc;
956 	int inquotes;
957 
958 	start = string;
959 	ifsspc = NULL;
960 	inquotes = 0;
961 
962 	if (ifslastp == NULL) {
963 		/* Return entire argument, IFS doesn't apply to any of it */
964 		sp = (struct strlist *)stalloc(sizeof *sp);
965 		sp->text = start;
966 		*arglist->lastp = sp;
967 		arglist->lastp = &sp->next;
968 		return;
969 	}
970 
971 	ifs = ifsset() ? ifsval() : " \t\n";
972 
973 	for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
974 		p = string + ifsp->begoff;
975 		inquotes = ifsp->inquotes;
976 		ifsspc = NULL;
977 		while (p < string + ifsp->endoff) {
978 			q = p;
979 			if (*p == CTLESC)
980 				p++;
981 			if (inquotes) {
982 				/* Only NULs (probably from "$@") end args */
983 				if (*p != 0) {
984 					p++;
985 					continue;
986 				}
987 			} else {
988 				if (!strchr(ifs, *p)) {
989 					p++;
990 					continue;
991 				}
992 				ifsspc = strchr(" \t\n", *p);
993 
994 				/* Ignore IFS whitespace at start */
995 				if (q == start && ifsspc != NULL) {
996 					p++;
997 					start = p;
998 					continue;
999 				}
1000 			}
1001 
1002 			/* Save this argument... */
1003 			*q = '\0';
1004 			sp = (struct strlist *)stalloc(sizeof *sp);
1005 			sp->text = start;
1006 			*arglist->lastp = sp;
1007 			arglist->lastp = &sp->next;
1008 			p++;
1009 
1010 			if (ifsspc != NULL) {
1011 				/* Ignore further trailing IFS whitespace */
1012 				for (; p < string + ifsp->endoff; p++) {
1013 					q = p;
1014 					if (*p == CTLESC)
1015 						p++;
1016 					if (strchr(ifs, *p) == NULL) {
1017 						p = q;
1018 						break;
1019 					}
1020 					if (strchr(" \t\n", *p) == NULL) {
1021 						p++;
1022 						break;
1023 					}
1024 				}
1025 			}
1026 			start = p;
1027 		}
1028 	}
1029 
1030 	/*
1031 	 * Save anything left as an argument.
1032 	 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1033 	 * generating 2 arguments, the second of which is empty.
1034 	 * Some recent clarification of the Posix spec say that it
1035 	 * should only generate one....
1036 	 */
1037 	if (*start) {
1038 		sp = (struct strlist *)stalloc(sizeof *sp);
1039 		sp->text = start;
1040 		*arglist->lastp = sp;
1041 		arglist->lastp = &sp->next;
1042 	}
1043 }
1044 
1045 STATIC void
1046 ifsfree(void)
1047 {
1048 	while (ifsfirst.next != NULL) {
1049 		struct ifsregion *ifsp;
1050 		INTOFF;
1051 		ifsp = ifsfirst.next->next;
1052 		ckfree(ifsfirst.next);
1053 		ifsfirst.next = ifsp;
1054 		INTON;
1055 	}
1056 	ifslastp = NULL;
1057 	ifsfirst.next = NULL;
1058 }
1059 
1060 
1061 
1062 /*
1063  * Expand shell metacharacters.  At this point, the only control characters
1064  * should be escapes.  The results are stored in the list exparg.
1065  */
1066 
1067 char *expdir;
1068 
1069 
1070 STATIC void
1071 expandmeta(struct strlist *str, int flag)
1072 {
1073 	char *p;
1074 	struct strlist **savelastp;
1075 	struct strlist *sp;
1076 	char c;
1077 	/* TODO - EXP_REDIR */
1078 
1079 	while (str) {
1080 		if (fflag)
1081 			goto nometa;
1082 		p = str->text;
1083 		for (;;) {			/* fast check for meta chars */
1084 			if ((c = *p++) == '\0')
1085 				goto nometa;
1086 			if (c == '*' || c == '?' || c == '[' || c == '!')
1087 				break;
1088 		}
1089 		savelastp = exparg.lastp;
1090 		INTOFF;
1091 		if (expdir == NULL) {
1092 			int i = strlen(str->text);
1093 			expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
1094 		}
1095 
1096 		expmeta(expdir, str->text);
1097 		ckfree(expdir);
1098 		expdir = NULL;
1099 		INTON;
1100 		if (exparg.lastp == savelastp) {
1101 			/*
1102 			 * no matches
1103 			 */
1104 nometa:
1105 			*exparg.lastp = str;
1106 			rmescapes(str->text);
1107 			exparg.lastp = &str->next;
1108 		} else {
1109 			*exparg.lastp = NULL;
1110 			*savelastp = sp = expsort(*savelastp);
1111 			while (sp->next != NULL)
1112 				sp = sp->next;
1113 			exparg.lastp = &sp->next;
1114 		}
1115 		str = str->next;
1116 	}
1117 }
1118 
1119 
1120 /*
1121  * Do metacharacter (i.e. *, ?, [...]) expansion.
1122  */
1123 
1124 STATIC void
1125 expmeta(char *enddir, char *name)
1126 {
1127 	char *p;
1128 	const char *cp;
1129 	char *q;
1130 	char *start;
1131 	char *endname;
1132 	int metaflag;
1133 	struct stat statb;
1134 	DIR *dirp;
1135 	struct dirent *dp;
1136 	int atend;
1137 	int matchdot;
1138 
1139 	metaflag = 0;
1140 	start = name;
1141 	for (p = name ; ; p++) {
1142 		if (*p == '*' || *p == '?')
1143 			metaflag = 1;
1144 		else if (*p == '[') {
1145 			q = p + 1;
1146 			if (*q == '!')
1147 				q++;
1148 			for (;;) {
1149 				while (*q == CTLQUOTEMARK)
1150 					q++;
1151 				if (*q == CTLESC)
1152 					q++;
1153 				if (*q == '/' || *q == '\0')
1154 					break;
1155 				if (*++q == ']') {
1156 					metaflag = 1;
1157 					break;
1158 				}
1159 			}
1160 		} else if (*p == '!' && p[1] == '!'	&& (p == name || p[-1] == '/')) {
1161 			metaflag = 1;
1162 		} else if (*p == '\0')
1163 			break;
1164 		else if (*p == CTLQUOTEMARK)
1165 			continue;
1166 		else if (*p == CTLESC)
1167 			p++;
1168 		if (*p == '/') {
1169 			if (metaflag)
1170 				break;
1171 			start = p + 1;
1172 		}
1173 	}
1174 	if (metaflag == 0) {	/* we've reached the end of the file name */
1175 		if (enddir != expdir)
1176 			metaflag++;
1177 		for (p = name ; ; p++) {
1178 			if (*p == CTLQUOTEMARK)
1179 				continue;
1180 			if (*p == CTLESC)
1181 				p++;
1182 			*enddir++ = *p;
1183 			if (*p == '\0')
1184 				break;
1185 		}
1186 		if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1187 			addfname(expdir);
1188 		return;
1189 	}
1190 	endname = p;
1191 	if (start != name) {
1192 		p = name;
1193 		while (p < start) {
1194 			while (*p == CTLQUOTEMARK)
1195 				p++;
1196 			if (*p == CTLESC)
1197 				p++;
1198 			*enddir++ = *p++;
1199 		}
1200 	}
1201 	if (enddir == expdir) {
1202 		cp = ".";
1203 	} else if (enddir == expdir + 1 && *expdir == '/') {
1204 		cp = "/";
1205 	} else {
1206 		cp = expdir;
1207 		enddir[-1] = '\0';
1208 	}
1209 	if ((dirp = opendir(cp)) == NULL)
1210 		return;
1211 	if (enddir != expdir)
1212 		enddir[-1] = '/';
1213 	if (*endname == 0) {
1214 		atend = 1;
1215 	} else {
1216 		atend = 0;
1217 		*endname++ = '\0';
1218 	}
1219 	matchdot = 0;
1220 	p = start;
1221 	while (*p == CTLQUOTEMARK)
1222 		p++;
1223 	if (*p == CTLESC)
1224 		p++;
1225 	if (*p == '.')
1226 		matchdot++;
1227 	while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1228 		if (dp->d_name[0] == '.' && ! matchdot)
1229 			continue;
1230 		if (patmatch(start, dp->d_name, 0)) {
1231 			if (atend) {
1232 				scopy(dp->d_name, enddir);
1233 				addfname(expdir);
1234 			} else {
1235 				for (p = enddir, cp = dp->d_name;
1236 				     (*p++ = *cp++) != '\0';)
1237 					continue;
1238 				p[-1] = '/';
1239 				expmeta(p, endname);
1240 			}
1241 		}
1242 	}
1243 	closedir(dirp);
1244 	if (! atend)
1245 		endname[-1] = '/';
1246 }
1247 
1248 
1249 /*
1250  * Add a file name to the list.
1251  */
1252 
1253 STATIC void
1254 addfname(char *name)
1255 {
1256 	char *p;
1257 	struct strlist *sp;
1258 
1259 	p = stalloc(strlen(name) + 1);
1260 	scopy(name, p);
1261 	sp = (struct strlist *)stalloc(sizeof *sp);
1262 	sp->text = p;
1263 	*exparg.lastp = sp;
1264 	exparg.lastp = &sp->next;
1265 }
1266 
1267 
1268 /*
1269  * Sort the results of file name expansion.  It calculates the number of
1270  * strings to sort and then calls msort (short for merge sort) to do the
1271  * work.
1272  */
1273 
1274 STATIC struct strlist *
1275 expsort(struct strlist *str)
1276 {
1277 	int len;
1278 	struct strlist *sp;
1279 
1280 	len = 0;
1281 	for (sp = str ; sp ; sp = sp->next)
1282 		len++;
1283 	return msort(str, len);
1284 }
1285 
1286 
1287 STATIC struct strlist *
1288 msort(struct strlist *list, int len)
1289 {
1290 	struct strlist *p, *q = NULL;
1291 	struct strlist **lpp;
1292 	int half;
1293 	int n;
1294 
1295 	if (len <= 1)
1296 		return list;
1297 	half = len >> 1;
1298 	p = list;
1299 	for (n = half ; --n >= 0 ; ) {
1300 		q = p;
1301 		p = p->next;
1302 	}
1303 	q->next = NULL;			/* terminate first half of list */
1304 	q = msort(list, half);		/* sort first half of list */
1305 	p = msort(p, len - half);		/* sort second half */
1306 	lpp = &list;
1307 	for (;;) {
1308 		if (strcmp(p->text, q->text) < 0) {
1309 			*lpp = p;
1310 			lpp = &p->next;
1311 			if ((p = *lpp) == NULL) {
1312 				*lpp = q;
1313 				break;
1314 			}
1315 		} else {
1316 			*lpp = q;
1317 			lpp = &q->next;
1318 			if ((q = *lpp) == NULL) {
1319 				*lpp = p;
1320 				break;
1321 			}
1322 		}
1323 	}
1324 	return list;
1325 }
1326 
1327 
1328 
1329 /*
1330  * Returns true if the pattern matches the string.
1331  */
1332 
1333 int
1334 patmatch(char *pattern, char *string, int squoted)
1335 {
1336 #ifdef notdef
1337 	if (pattern[0] == '!' && pattern[1] == '!')
1338 		return 1 - pmatch(pattern + 2, string);
1339 	else
1340 #endif
1341 		return pmatch(pattern, string, squoted);
1342 }
1343 
1344 
1345 STATIC int
1346 pmatch(char *pattern, char *string, int squoted)
1347 {
1348 	char *p, *q;
1349 	char c;
1350 
1351 	p = pattern;
1352 	q = string;
1353 	for (;;) {
1354 		switch (c = *p++) {
1355 		case '\0':
1356 			goto breakloop;
1357 		case CTLESC:
1358 			if (squoted && *q == CTLESC)
1359 				q++;
1360 			if (*q++ != *p++)
1361 				return 0;
1362 			break;
1363 		case CTLQUOTEMARK:
1364 			continue;
1365 		case '?':
1366 			if (squoted && *q == CTLESC)
1367 				q++;
1368 			if (*q++ == '\0')
1369 				return 0;
1370 			break;
1371 		case '*':
1372 			c = *p;
1373 			while (c == CTLQUOTEMARK || c == '*')
1374 				c = *++p;
1375 			if (c != CTLESC &&  c != CTLQUOTEMARK &&
1376 			    c != '?' && c != '*' && c != '[') {
1377 				while (*q != c) {
1378 					if (squoted && *q == CTLESC &&
1379 					    q[1] == c)
1380 						break;
1381 					if (*q == '\0')
1382 						return 0;
1383 					if (squoted && *q == CTLESC)
1384 						q++;
1385 					q++;
1386 				}
1387 			}
1388 			do {
1389 				if (pmatch(p, q, squoted))
1390 					return 1;
1391 				if (squoted && *q == CTLESC)
1392 					q++;
1393 			} while (*q++ != '\0');
1394 			return 0;
1395 		case '[': {
1396 			char *endp;
1397 			int invert, found;
1398 			char chr;
1399 
1400 			endp = p;
1401 			if (*endp == '!')
1402 				endp++;
1403 			for (;;) {
1404 				while (*endp == CTLQUOTEMARK)
1405 					endp++;
1406 				if (*endp == '\0')
1407 					goto dft;		/* no matching ] */
1408 				if (*endp == CTLESC)
1409 					endp++;
1410 				if (*++endp == ']')
1411 					break;
1412 			}
1413 			invert = 0;
1414 			if (*p == '!') {
1415 				invert++;
1416 				p++;
1417 			}
1418 			found = 0;
1419 			chr = *q++;
1420 			if (squoted && chr == CTLESC)
1421 				chr = *q++;
1422 			if (chr == '\0')
1423 				return 0;
1424 			c = *p++;
1425 			do {
1426 				if (c == CTLQUOTEMARK)
1427 					continue;
1428 				if (c == CTLESC)
1429 					c = *p++;
1430 				if (*p == '-' && p[1] != ']') {
1431 					p++;
1432 					while (*p == CTLQUOTEMARK)
1433 						p++;
1434 					if (*p == CTLESC)
1435 						p++;
1436 					if (chr >= c && chr <= *p)
1437 						found = 1;
1438 					p++;
1439 				} else {
1440 					if (chr == c)
1441 						found = 1;
1442 				}
1443 			} while ((c = *p++) != ']');
1444 			if (found == invert)
1445 				return 0;
1446 			break;
1447 		}
1448 dft:	        default:
1449 			if (squoted && *q == CTLESC)
1450 				q++;
1451 			if (*q++ != c)
1452 				return 0;
1453 			break;
1454 		}
1455 	}
1456 breakloop:
1457 	if (*q != '\0')
1458 		return 0;
1459 	return 1;
1460 }
1461 
1462 
1463 
1464 /*
1465  * Remove any CTLESC characters from a string.
1466  */
1467 
1468 void
1469 rmescapes(char *str)
1470 {
1471 	char *p, *q;
1472 
1473 	p = str;
1474 	while (*p != CTLESC && *p != CTLQUOTEMARK) {
1475 		if (*p++ == '\0')
1476 			return;
1477 	}
1478 	q = p;
1479 	while (*p) {
1480 		if (*p == CTLQUOTEMARK) {
1481 			p++;
1482 			continue;
1483 		}
1484 		if (*p == CTLESC)
1485 			p++;
1486 		*q++ = *p++;
1487 	}
1488 	*q = '\0';
1489 }
1490 
1491 
1492 
1493 /*
1494  * See if a pattern matches in a case statement.
1495  */
1496 
1497 int
1498 casematch(union node *pattern, char *val)
1499 {
1500 	struct stackmark smark;
1501 	int result;
1502 	char *p;
1503 
1504 	setstackmark(&smark);
1505 	argbackq = pattern->narg.backquote;
1506 	STARTSTACKSTR(expdest);
1507 	ifslastp = NULL;
1508 	argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1509 	STPUTC('\0', expdest);
1510 	p = grabstackstr(expdest);
1511 	result = patmatch(p, val, 0);
1512 	popstackmark(&smark);
1513 	return result;
1514 }
1515 
1516 /*
1517  * Our own itoa().
1518  */
1519 
1520 STATIC char *
1521 cvtnum(int num, char *buf)
1522 {
1523 	char temp[32];
1524 	int neg = num < 0;
1525 	char *p = temp + 31;
1526 
1527 	temp[31] = '\0';
1528 
1529 	do {
1530 		*--p = num % 10 + '0';
1531 	} while ((num /= 10) != 0);
1532 
1533 	if (neg)
1534 		*--p = '-';
1535 
1536 	while (*p)
1537 		STPUTC(*p++, buf);
1538 	return buf;
1539 }
1540 
1541 /*
1542  * Do most of the work for wordexp(3).
1543  */
1544 
1545 int
1546 wordexpcmd(int argc, char **argv)
1547 {
1548 	size_t len;
1549 	int i;
1550 
1551 	out1fmt("%d", argc - 1);
1552 	out1c('\0');
1553 	for (i = 1, len = 0; i < argc; i++)
1554 		len += strlen(argv[i]);
1555 	out1fmt("%zd", len);
1556 	out1c('\0');
1557 	for (i = 1; i < argc; i++) {
1558 		out1str(argv[i]);
1559 		out1c('\0');
1560 	}
1561 	return (0);
1562 }
1563