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