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