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