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