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