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