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