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