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