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