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