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