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