expand.c revision 45514
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3836150Scharnier#if 0
3936150Scharnierstatic char sccsid[] = "@(#)expand.c	8.5 (Berkeley) 5/15/95";
4036150Scharnier#endif
4136150Scharnierstatic const char rcsid[] =
4245514Stegge	"$Id: expand.c,v 1.24 1998/09/13 19:24:57 tegge Exp $";
431556Srgrimes#endif /* not lint */
441556Srgrimes
4517987Speter#include <sys/types.h>
4617987Speter#include <sys/time.h>
4717987Speter#include <sys/stat.h>
4817987Speter#include <errno.h>
4917987Speter#include <dirent.h>
5017987Speter#include <unistd.h>
5117987Speter#include <pwd.h>
5217987Speter#include <stdlib.h>
5319281Sache#include <limits.h>
5438887Stegge#include <stdio.h>
5517987Speter
561556Srgrimes/*
571556Srgrimes * Routines to expand arguments to commands.  We have to deal with
581556Srgrimes * backquotes, shell variables, and file metacharacters.
591556Srgrimes */
601556Srgrimes
611556Srgrimes#include "shell.h"
621556Srgrimes#include "main.h"
631556Srgrimes#include "nodes.h"
641556Srgrimes#include "eval.h"
651556Srgrimes#include "expand.h"
661556Srgrimes#include "syntax.h"
671556Srgrimes#include "parser.h"
681556Srgrimes#include "jobs.h"
691556Srgrimes#include "options.h"
701556Srgrimes#include "var.h"
711556Srgrimes#include "input.h"
721556Srgrimes#include "output.h"
731556Srgrimes#include "memalloc.h"
741556Srgrimes#include "error.h"
751556Srgrimes#include "mystring.h"
7617987Speter#include "arith.h"
7717987Speter#include "show.h"
781556Srgrimes
791556Srgrimes/*
801556Srgrimes * Structure specifying which parts of the string should be searched
811556Srgrimes * for IFS characters.
821556Srgrimes */
831556Srgrimes
841556Srgrimesstruct ifsregion {
851556Srgrimes	struct ifsregion *next;	/* next region in list */
861556Srgrimes	int begoff;		/* offset of start of region */
871556Srgrimes	int endoff;		/* offset of end of region */
881556Srgrimes	int nulonly;		/* search for nul bytes only */
891556Srgrimes};
901556Srgrimes
911556Srgrimes
921556Srgrimeschar *expdest;			/* output of current string */
931556Srgrimesstruct nodelist *argbackq;	/* list of back quote expressions */
941556Srgrimesstruct ifsregion ifsfirst;	/* first struct in list of ifs regions */
951556Srgrimesstruct ifsregion *ifslastp;	/* last struct in list */
961556Srgrimesstruct arglist exparg;		/* holds expanded arg list */
971556Srgrimes
9817987SpeterSTATIC void argstr __P((char *, int));
9917987SpeterSTATIC char *exptilde __P((char *, int));
10017987SpeterSTATIC void expbackq __P((union node *, int, int));
10120425SsteveSTATIC int subevalvar __P((char *, char *, int, int, int, int));
10217987SpeterSTATIC char *evalvar __P((char *, int));
10325233SsteveSTATIC int varisset __P((char *, int));
10418202SpeterSTATIC void varvalue __P((char *, int, int));
10517987SpeterSTATIC void recordregion __P((int, int, int));
10638887SteggeSTATIC void removerecordregions __P((int));
10717987SpeterSTATIC void ifsbreakup __P((char *, struct arglist *));
10817987SpeterSTATIC void expandmeta __P((struct strlist *, int));
10917987SpeterSTATIC void expmeta __P((char *, char *));
11017987SpeterSTATIC void addfname __P((char *));
11117987SpeterSTATIC struct strlist *expsort __P((struct strlist *));
11217987SpeterSTATIC struct strlist *msort __P((struct strlist *, int));
11345514SteggeSTATIC int pmatch __P((char *, char *, int));
11417987SpeterSTATIC char *cvtnum __P((int, char *));
11519281SacheSTATIC int collate_range_cmp __P((int, int));
1161556Srgrimes
11719281SacheSTATIC int collate_range_cmp (c1, c2)
11819281Sache	int c1, c2;
11919281Sache{
12019281Sache	static char s1[2], s2[2];
12119281Sache	int ret;
12219281Sache
12319281Sache	c1 &= UCHAR_MAX;
12419281Sache	c2 &= UCHAR_MAX;
12519281Sache	if (c1 == c2)
12619281Sache		return (0);
12719281Sache	s1[0] = c1;
12819281Sache	s2[0] = c2;
12919281Sache	if ((ret = strcoll(s1, s2)) != 0)
13019281Sache		return (ret);
13119281Sache	return (c1 - c2);
13219281Sache}
13319281Sache
1341556Srgrimes/*
1351556Srgrimes * Expand shell variables and backquotes inside a here document.
1361556Srgrimes */
1371556Srgrimes
1381556Srgrimesvoid
1391556Srgrimesexpandhere(arg, fd)
1401556Srgrimes	union node *arg;	/* the document */
1411556Srgrimes	int fd;			/* where to write the expanded version */
1421556Srgrimes	{
1431556Srgrimes	herefd = fd;
1441556Srgrimes	expandarg(arg, (struct arglist *)NULL, 0);
14539137Stegge	xwrite(fd, stackblock(), expdest - stackblock());
1461556Srgrimes}
1471556Srgrimes
1481556Srgrimes
1491556Srgrimes/*
1501556Srgrimes * Perform variable substitution and command substitution on an argument,
1511556Srgrimes * placing the resulting list of arguments in arglist.  If EXP_FULL is true,
1521556Srgrimes * perform splitting and file name expansion.  When arglist is NULL, perform
1531556Srgrimes * here document expansion.
1541556Srgrimes */
1551556Srgrimes
1561556Srgrimesvoid
1571556Srgrimesexpandarg(arg, arglist, flag)
1581556Srgrimes	union node *arg;
1591556Srgrimes	struct arglist *arglist;
16017987Speter	int flag;
16117987Speter{
1621556Srgrimes	struct strlist *sp;
1631556Srgrimes	char *p;
1641556Srgrimes
1651556Srgrimes	argbackq = arg->narg.backquote;
1661556Srgrimes	STARTSTACKSTR(expdest);
1671556Srgrimes	ifsfirst.next = NULL;
1681556Srgrimes	ifslastp = NULL;
1691556Srgrimes	argstr(arg->narg.text, flag);
1701556Srgrimes	if (arglist == NULL) {
1711556Srgrimes		return;			/* here document expanded */
1721556Srgrimes	}
1731556Srgrimes	STPUTC('\0', expdest);
1741556Srgrimes	p = grabstackstr(expdest);
1751556Srgrimes	exparg.lastp = &exparg.list;
1761556Srgrimes	/*
1771556Srgrimes	 * TODO - EXP_REDIR
1781556Srgrimes	 */
1791556Srgrimes	if (flag & EXP_FULL) {
1801556Srgrimes		ifsbreakup(p, &exparg);
1811556Srgrimes		*exparg.lastp = NULL;
1821556Srgrimes		exparg.lastp = &exparg.list;
1831556Srgrimes		expandmeta(exparg.list, flag);
1841556Srgrimes	} else {
1851556Srgrimes		if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
1861556Srgrimes			rmescapes(p);
1871556Srgrimes		sp = (struct strlist *)stalloc(sizeof (struct strlist));
1881556Srgrimes		sp->text = p;
1891556Srgrimes		*exparg.lastp = sp;
1901556Srgrimes		exparg.lastp = &sp->next;
1911556Srgrimes	}
1921556Srgrimes	while (ifsfirst.next != NULL) {
1931556Srgrimes		struct ifsregion *ifsp;
1941556Srgrimes		INTOFF;
1951556Srgrimes		ifsp = ifsfirst.next->next;
1961556Srgrimes		ckfree(ifsfirst.next);
1971556Srgrimes		ifsfirst.next = ifsp;
1981556Srgrimes		INTON;
1991556Srgrimes	}
2001556Srgrimes	*exparg.lastp = NULL;
2011556Srgrimes	if (exparg.list) {
2021556Srgrimes		*arglist->lastp = exparg.list;
2031556Srgrimes		arglist->lastp = exparg.lastp;
2041556Srgrimes	}
2051556Srgrimes}
2061556Srgrimes
2071556Srgrimes
2081556Srgrimes
2091556Srgrimes/*
2101556Srgrimes * Perform variable and command substitution.  If EXP_FULL is set, output CTLESC
2111556Srgrimes * characters to allow for further processing.  Otherwise treat
2121556Srgrimes * $@ like $* since no splitting will be performed.
2131556Srgrimes */
2141556Srgrimes
2151556SrgrimesSTATIC void
2161556Srgrimesargstr(p, flag)
21725233Ssteve	char *p;
21817987Speter	int flag;
21917987Speter{
22025233Ssteve	char c;
2211556Srgrimes	int quotes = flag & (EXP_FULL | EXP_CASE);	/* do CTLESC */
2221556Srgrimes	int firsteq = 1;
2231556Srgrimes
2241556Srgrimes	if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
2251556Srgrimes		p = exptilde(p, flag);
2261556Srgrimes	for (;;) {
2271556Srgrimes		switch (c = *p++) {
2281556Srgrimes		case '\0':
2291556Srgrimes		case CTLENDVAR: /* ??? */
2301556Srgrimes			goto breakloop;
23138887Stegge		case CTLQUOTEMARK:
23238887Stegge			/* "$@" syntax adherence hack */
23338887Stegge			if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
23438887Stegge				break;
23539137Stegge			if ((flag & EXP_FULL) != 0)
23639137Stegge				STPUTC(c, expdest);
23738887Stegge			break;
2381556Srgrimes		case CTLESC:
2391556Srgrimes			if (quotes)
2401556Srgrimes				STPUTC(c, expdest);
2411556Srgrimes			c = *p++;
2421556Srgrimes			STPUTC(c, expdest);
2431556Srgrimes			break;
2441556Srgrimes		case CTLVAR:
2451556Srgrimes			p = evalvar(p, flag);
2461556Srgrimes			break;
2471556Srgrimes		case CTLBACKQ:
2481556Srgrimes		case CTLBACKQ|CTLQUOTE:
2491556Srgrimes			expbackq(argbackq->n, c & CTLQUOTE, flag);
2501556Srgrimes			argbackq = argbackq->next;
2511556Srgrimes			break;
2521556Srgrimes		case CTLENDARI:
2531556Srgrimes			expari(flag);
2541556Srgrimes			break;
2551556Srgrimes		case ':':
2561556Srgrimes		case '=':
2571556Srgrimes			/*
2581556Srgrimes			 * sort of a hack - expand tildes in variable
2591556Srgrimes			 * assignments (after the first '=' and after ':'s).
2601556Srgrimes			 */
2611556Srgrimes			STPUTC(c, expdest);
2621556Srgrimes			if (flag & EXP_VARTILDE && *p == '~') {
2631556Srgrimes				if (c == '=') {
2641556Srgrimes					if (firsteq)
2651556Srgrimes						firsteq = 0;
2661556Srgrimes					else
2671556Srgrimes						break;
2681556Srgrimes				}
2691556Srgrimes				p = exptilde(p, flag);
2701556Srgrimes			}
2711556Srgrimes			break;
2721556Srgrimes		default:
2731556Srgrimes			STPUTC(c, expdest);
2741556Srgrimes		}
2751556Srgrimes	}
2761556Srgrimesbreakloop:;
2771556Srgrimes}
2781556Srgrimes
2791556SrgrimesSTATIC char *
2801556Srgrimesexptilde(p, flag)
2811556Srgrimes	char *p;
28217987Speter	int flag;
28317987Speter{
2841556Srgrimes	char c, *startp = p;
2851556Srgrimes	struct passwd *pw;
2861556Srgrimes	char *home;
2871556Srgrimes	int quotes = flag & (EXP_FULL | EXP_CASE);
2881556Srgrimes
28917987Speter	while ((c = *p) != '\0') {
2901556Srgrimes		switch(c) {
2911556Srgrimes		case CTLESC:
2921556Srgrimes			return (startp);
29339137Stegge		case CTLQUOTEMARK:
29439137Stegge			return (startp);
2951556Srgrimes		case ':':
2961556Srgrimes			if (flag & EXP_VARTILDE)
2971556Srgrimes				goto done;
2981556Srgrimes			break;
2991556Srgrimes		case '/':
3001556Srgrimes			goto done;
3011556Srgrimes		}
3021556Srgrimes		p++;
3031556Srgrimes	}
3041556Srgrimesdone:
3051556Srgrimes	*p = '\0';
3061556Srgrimes	if (*(startp+1) == '\0') {
3071556Srgrimes		if ((home = lookupvar("HOME")) == NULL)
3081556Srgrimes			goto lose;
3091556Srgrimes	} else {
3101556Srgrimes		if ((pw = getpwnam(startp+1)) == NULL)
3111556Srgrimes			goto lose;
3121556Srgrimes		home = pw->pw_dir;
3131556Srgrimes	}
3141556Srgrimes	if (*home == '\0')
3151556Srgrimes		goto lose;
3161556Srgrimes	*p = c;
31717987Speter	while ((c = *home++) != '\0') {
3181556Srgrimes		if (quotes && SQSYNTAX[c] == CCTL)
3191556Srgrimes			STPUTC(CTLESC, expdest);
3201556Srgrimes		STPUTC(c, expdest);
3211556Srgrimes	}
3221556Srgrimes	return (p);
3231556Srgrimeslose:
3241556Srgrimes	*p = c;
3251556Srgrimes	return (startp);
3261556Srgrimes}
3271556Srgrimes
3281556Srgrimes
32938887SteggeSTATIC void
33038887Steggeremoverecordregions(endoff)
33138887Stegge	int endoff;
33238887Stegge{
33338887Stegge	if (ifslastp == NULL)
33438887Stegge		return;
33538887Stegge
33638887Stegge	if (ifsfirst.endoff > endoff) {
33738887Stegge		while (ifsfirst.next != NULL) {
33838887Stegge			struct ifsregion *ifsp;
33938887Stegge			INTOFF;
34038887Stegge			ifsp = ifsfirst.next->next;
34138887Stegge			ckfree(ifsfirst.next);
34238887Stegge			ifsfirst.next = ifsp;
34338887Stegge			INTON;
34438887Stegge		}
34538887Stegge		if (ifsfirst.begoff > endoff)
34638887Stegge			ifslastp = NULL;
34738887Stegge		else {
34838887Stegge			ifslastp = &ifsfirst;
34938887Stegge			ifsfirst.endoff = endoff;
35038887Stegge		}
35138887Stegge		return;
35238887Stegge	}
35338887Stegge
35438887Stegge	ifslastp = &ifsfirst;
35538887Stegge	while (ifslastp->next && ifslastp->next->begoff < endoff)
35638887Stegge		ifslastp=ifslastp->next;
35738887Stegge	while (ifslastp->next != NULL) {
35838887Stegge		struct ifsregion *ifsp;
35938887Stegge		INTOFF;
36038887Stegge		ifsp = ifslastp->next->next;
36138887Stegge		ckfree(ifslastp->next);
36238887Stegge		ifslastp->next = ifsp;
36338887Stegge		INTON;
36438887Stegge	}
36538887Stegge	if (ifslastp->endoff > endoff)
36638887Stegge		ifslastp->endoff = endoff;
36738887Stegge}
36838887Stegge
3691556Srgrimes/*
3701556Srgrimes * Expand arithmetic expression.  Backup to start of expression,
3711556Srgrimes * evaluate, place result in (backed up) result, adjust string position.
3721556Srgrimes */
3731556Srgrimesvoid
3741556Srgrimesexpari(flag)
37517987Speter	int flag;
37617987Speter{
3771556Srgrimes	char *p, *start;
3781556Srgrimes	int result;
37938887Stegge	int begoff;
3801556Srgrimes	int quotes = flag & (EXP_FULL | EXP_CASE);
38138887Stegge	int quoted;
3821556Srgrimes
38325233Ssteve
3841556Srgrimes	/*
3851556Srgrimes	 * This routine is slightly over-compilcated for
3861556Srgrimes	 * efficiency.  First we make sure there is
3871556Srgrimes	 * enough space for the result, which may be bigger
3881556Srgrimes	 * than the expression if we add exponentation.  Next we
3891556Srgrimes	 * scan backwards looking for the start of arithmetic.  If the
3901556Srgrimes	 * next previous character is a CTLESC character, then we
3911556Srgrimes	 * have to rescan starting from the beginning since CTLESC
3928855Srgrimes	 * characters have to be processed left to right.
3931556Srgrimes	 */
39422777Ssteve#if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10
39522777Ssteve#error "integers with more than 10 digits are not supported"
39622777Ssteve#endif
39722777Ssteve	CHECKSTRSPACE(12 - 2, expdest);
3988855Srgrimes	USTPUTC('\0', expdest);
3991556Srgrimes	start = stackblock();
4001556Srgrimes	p = expdest;
4011556Srgrimes	while (*p != CTLARI && p >= start)
4021556Srgrimes		--p;
4031556Srgrimes	if (*p != CTLARI)
4041556Srgrimes		error("missing CTLARI (shouldn't happen)");
4051556Srgrimes	if (p > start && *(p-1) == CTLESC)
4061556Srgrimes		for (p = start; *p != CTLARI; p++)
4071556Srgrimes			if (*p == CTLESC)
4081556Srgrimes				p++;
40938887Stegge
41038887Stegge	if (p[1] == '"')
41138887Stegge		quoted=1;
41238887Stegge	else
41338887Stegge		quoted=0;
41438887Stegge	begoff = p - start;
41538887Stegge	removerecordregions(begoff);
4161556Srgrimes	if (quotes)
41738887Stegge		rmescapes(p+2);
41838887Stegge	result = arith(p+2);
41922777Ssteve	fmtstr(p, 12, "%d", result);
4201556Srgrimes	while (*p++)
4211556Srgrimes		;
42238887Stegge	if (quoted == 0)
42338887Stegge		recordregion(begoff, p - 1 - start, 0);
4241556Srgrimes	result = expdest - p + 1;
4251556Srgrimes	STADJUST(-result, expdest);
4261556Srgrimes}
4271556Srgrimes
4281556Srgrimes
4291556Srgrimes/*
4301556Srgrimes * Expand stuff in backwards quotes.
4311556Srgrimes */
4321556Srgrimes
4331556SrgrimesSTATIC void
4341556Srgrimesexpbackq(cmd, quoted, flag)
4351556Srgrimes	union node *cmd;
43617987Speter	int quoted;
43717987Speter	int flag;
43817987Speter{
4391556Srgrimes	struct backcmd in;
4401556Srgrimes	int i;
4411556Srgrimes	char buf[128];
4421556Srgrimes	char *p;
4431556Srgrimes	char *dest = expdest;
4441556Srgrimes	struct ifsregion saveifs, *savelastp;
4451556Srgrimes	struct nodelist *saveargbackq;
4461556Srgrimes	char lastc;
4471556Srgrimes	int startloc = dest - stackblock();
4481556Srgrimes	char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
4491556Srgrimes	int saveherefd;
4501556Srgrimes	int quotes = flag & (EXP_FULL | EXP_CASE);
4511556Srgrimes
4521556Srgrimes	INTOFF;
4531556Srgrimes	saveifs = ifsfirst;
4541556Srgrimes	savelastp = ifslastp;
4551556Srgrimes	saveargbackq = argbackq;
4568855Srgrimes	saveherefd = herefd;
4571556Srgrimes	herefd = -1;
4581556Srgrimes	p = grabstackstr(dest);
4591556Srgrimes	evalbackcmd(cmd, &in);
4601556Srgrimes	ungrabstackstr(p, dest);
4611556Srgrimes	ifsfirst = saveifs;
4621556Srgrimes	ifslastp = savelastp;
4631556Srgrimes	argbackq = saveargbackq;
4641556Srgrimes	herefd = saveherefd;
4651556Srgrimes
4661556Srgrimes	p = in.buf;
4671556Srgrimes	lastc = '\0';
4681556Srgrimes	for (;;) {
4691556Srgrimes		if (--in.nleft < 0) {
4701556Srgrimes			if (in.fd < 0)
4711556Srgrimes				break;
4721556Srgrimes			while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
4731556Srgrimes			TRACE(("expbackq: read returns %d\n", i));
4741556Srgrimes			if (i <= 0)
4751556Srgrimes				break;
4761556Srgrimes			p = buf;
4771556Srgrimes			in.nleft = i - 1;
4781556Srgrimes		}
4791556Srgrimes		lastc = *p++;
4801556Srgrimes		if (lastc != '\0') {
4811556Srgrimes			if (quotes && syntax[lastc] == CCTL)
4821556Srgrimes				STPUTC(CTLESC, dest);
4831556Srgrimes			STPUTC(lastc, dest);
4841556Srgrimes		}
4851556Srgrimes	}
48617987Speter
48717987Speter	/* Eat all trailing newlines */
48817987Speter	for (p--; lastc == '\n'; lastc = *--p)
4891556Srgrimes		STUNPUTC(dest);
49017987Speter
4911556Srgrimes	if (in.fd >= 0)
4921556Srgrimes		close(in.fd);
4931556Srgrimes	if (in.buf)
4941556Srgrimes		ckfree(in.buf);
4951556Srgrimes	if (in.jp)
4961556Srgrimes		exitstatus = waitforjob(in.jp);
4971556Srgrimes	if (quoted == 0)
4981556Srgrimes		recordregion(startloc, dest - stackblock(), 0);
4991556Srgrimes	TRACE(("evalbackq: size=%d: \"%.*s\"\n",
5001556Srgrimes		(dest - stackblock()) - startloc,
5011556Srgrimes		(dest - stackblock()) - startloc,
5021556Srgrimes		stackblock() + startloc));
5031556Srgrimes	expdest = dest;
5041556Srgrimes	INTON;
5051556Srgrimes}
5061556Srgrimes
5071556Srgrimes
5081556Srgrimes
50917987SpeterSTATIC int
51020425Sstevesubevalvar(p, str, strloc, subtype, startloc, varflags)
51117987Speter	char *p;
51217987Speter	char *str;
51320425Ssteve	int strloc;
51417987Speter	int subtype;
51517987Speter	int startloc;
51617987Speter	int varflags;
51717987Speter{
51817987Speter	char *startp;
51917987Speter	char *loc = NULL;
52045514Stegge	char *q;
52117987Speter	int c = 0;
52217987Speter	int saveherefd = herefd;
52317987Speter	struct nodelist *saveargbackq = argbackq;
52420425Ssteve	int amount;
52520425Ssteve
52617987Speter	herefd = -1;
52717987Speter	argstr(p, 0);
52817987Speter	STACKSTRNUL(expdest);
52917987Speter	herefd = saveherefd;
53017987Speter	argbackq = saveargbackq;
53117987Speter	startp = stackblock() + startloc;
53220425Ssteve	if (str == NULL)
53320425Ssteve	    str = stackblock() + strloc;
53417987Speter
53517987Speter	switch (subtype) {
53617987Speter	case VSASSIGN:
53717987Speter		setvar(str, startp, 0);
53820425Ssteve		amount = startp - expdest;
53920425Ssteve		STADJUST(amount, expdest);
54017987Speter		varflags &= ~VSNUL;
54117987Speter		if (c != 0)
54217987Speter			*loc = c;
54317987Speter		return 1;
54417987Speter
54517987Speter	case VSQUESTION:
54617987Speter		if (*p != CTLENDVAR) {
54717987Speter			outfmt(&errout, "%s\n", startp);
54817987Speter			error((char *)NULL);
54917987Speter		}
55017987Speter		error("%.*s: parameter %snot set", p - str - 1,
55117987Speter		      str, (varflags & VSNUL) ? "null or "
55217987Speter					      : nullstr);
55317987Speter		return 0;
55417987Speter
55517987Speter	case VSTRIMLEFT:
55625233Ssteve		for (loc = startp; loc < str; loc++) {
55717987Speter			c = *loc;
55817987Speter			*loc = '\0';
55945514Stegge			if (patmatch(str, startp, varflags & VSQUOTE)) {
56017987Speter				*loc = c;
56117987Speter				goto recordleft;
56217987Speter			}
56317987Speter			*loc = c;
56445514Stegge			if ((varflags & VSQUOTE) && *loc == CTLESC)
56545514Stegge				loc++;
56617987Speter		}
56717987Speter		return 0;
56817987Speter
56917987Speter	case VSTRIMLEFTMAX:
57045514Stegge		for (loc = str - 1; loc >= startp;) {
57117987Speter			c = *loc;
57217987Speter			*loc = '\0';
57345514Stegge			if (patmatch(str, startp, varflags & VSQUOTE)) {
57417987Speter				*loc = c;
57517987Speter				goto recordleft;
57617987Speter			}
57717987Speter			*loc = c;
57845514Stegge			loc--;
57945514Stegge			if ((varflags & VSQUOTE) && loc > startp &&
58045514Stegge			    *(loc - 1) == CTLESC) {
58145514Stegge				for (q = startp; q < loc; q++)
58245514Stegge					if (*q == CTLESC)
58345514Stegge						q++;
58445514Stegge				if (q > loc)
58545514Stegge					loc--;
58645514Stegge			}
58717987Speter		}
58817987Speter		return 0;
58917987Speter
59017987Speter	case VSTRIMRIGHT:
59145514Stegge		for (loc = str - 1; loc >= startp;) {
59245514Stegge			if (patmatch(str, loc, varflags & VSQUOTE)) {
59320425Ssteve				amount = loc - expdest;
59420425Ssteve				STADJUST(amount, expdest);
59517987Speter				return 1;
59617987Speter			}
59745514Stegge			loc--;
59845514Stegge			if ((varflags & VSQUOTE) && loc > startp &&
59945514Stegge			    *(loc - 1) == CTLESC) {
60045514Stegge				for (q = startp; q < loc; q++)
60145514Stegge					if (*q == CTLESC)
60245514Stegge						q++;
60345514Stegge				if (q > loc)
60445514Stegge					loc--;
60545514Stegge			}
60617987Speter		}
60717987Speter		return 0;
60817987Speter
60917987Speter	case VSTRIMRIGHTMAX:
61017987Speter		for (loc = startp; loc < str - 1; loc++) {
61145514Stegge			if (patmatch(str, loc, varflags & VSQUOTE)) {
61220425Ssteve				amount = loc - expdest;
61320425Ssteve				STADJUST(amount, expdest);
61417987Speter				return 1;
61517987Speter			}
61645514Stegge			if ((varflags & VSQUOTE) && *loc == CTLESC)
61745514Stegge				loc++;
61817987Speter		}
61917987Speter		return 0;
62017987Speter
62117987Speter
62217987Speter	default:
62317987Speter		abort();
62417987Speter	}
62517987Speter
62617987Speterrecordleft:
62720425Ssteve	amount = ((str - 1) - (loc - startp)) - expdest;
62820425Ssteve	STADJUST(amount, expdest);
62917987Speter	while (loc != str - 1)
63017987Speter		*startp++ = *loc++;
63117987Speter	return 1;
63217987Speter}
63317987Speter
63417987Speter
6351556Srgrimes/*
6361556Srgrimes * Expand a variable, and return a pointer to the next character in the
6371556Srgrimes * input string.
6381556Srgrimes */
6391556Srgrimes
6401556SrgrimesSTATIC char *
6411556Srgrimesevalvar(p, flag)
6421556Srgrimes	char *p;
64317987Speter	int flag;
64417987Speter{
6451556Srgrimes	int subtype;
6461556Srgrimes	int varflags;
6471556Srgrimes	char *var;
6481556Srgrimes	char *val;
64917987Speter	char *pat;
6501556Srgrimes	int c;
6511556Srgrimes	int set;
6521556Srgrimes	int special;
6531556Srgrimes	int startloc;
65417987Speter	int varlen;
65517987Speter	int easy;
6561556Srgrimes	int quotes = flag & (EXP_FULL | EXP_CASE);
6571556Srgrimes
6581556Srgrimes	varflags = *p++;
6591556Srgrimes	subtype = varflags & VSTYPE;
6601556Srgrimes	var = p;
6611556Srgrimes	special = 0;
6621556Srgrimes	if (! is_name(*p))
6631556Srgrimes		special = 1;
6641556Srgrimes	p = strchr(p, '=') + 1;
6651556Srgrimesagain: /* jump here after setting a variable with ${var=text} */
6661556Srgrimes	if (special) {
66725233Ssteve		set = varisset(var, varflags & VSNUL);
6681556Srgrimes		val = NULL;
6691556Srgrimes	} else {
6701556Srgrimes		val = lookupvar(var);
67117987Speter		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
6721556Srgrimes			val = NULL;
6731556Srgrimes			set = 0;
6741556Srgrimes		} else
6751556Srgrimes			set = 1;
6761556Srgrimes	}
67717987Speter	varlen = 0;
6781556Srgrimes	startloc = expdest - stackblock();
6791556Srgrimes	if (set && subtype != VSPLUS) {
6801556Srgrimes		/* insert the value of the variable */
6811556Srgrimes		if (special) {
68218202Speter			varvalue(var, varflags & VSQUOTE, flag & EXP_FULL);
68317987Speter			if (subtype == VSLENGTH) {
68425233Ssteve				varlen = expdest - stackblock() - startloc;
68525233Ssteve				STADJUST(-varlen, expdest);
68617987Speter			}
6871556Srgrimes		} else {
68820425Ssteve			char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
68917987Speter								  : BASESYNTAX;
6901556Srgrimes
69117987Speter			if (subtype == VSLENGTH) {
69217987Speter				for (;*val; val++)
69317987Speter					varlen++;
6941556Srgrimes			}
69517987Speter			else {
69617987Speter				while (*val) {
69717987Speter					if (quotes && syntax[*val] == CCTL)
69817987Speter						STPUTC(CTLESC, expdest);
69917987Speter					STPUTC(*val++, expdest);
70017987Speter				}
70117987Speter
70217987Speter			}
7031556Srgrimes		}
7041556Srgrimes	}
70520425Ssteve
7061556Srgrimes	if (subtype == VSPLUS)
7071556Srgrimes		set = ! set;
70817987Speter
70920425Ssteve	easy = ((varflags & VSQUOTE) == 0 ||
71017987Speter		(*var == '@' && shellparam.nparam != 1));
71117987Speter
71217987Speter
71317987Speter	switch (subtype) {
71417987Speter	case VSLENGTH:
71517987Speter		expdest = cvtnum(varlen, expdest);
71617987Speter		goto record;
71717987Speter
71817987Speter	case VSNORMAL:
71917987Speter		if (!easy)
72017987Speter			break;
72117987Speterrecord:
72220425Ssteve		recordregion(startloc, expdest - stackblock(),
72317987Speter			     varflags & VSQUOTE);
72417987Speter		break;
72517987Speter
72617987Speter	case VSPLUS:
72717987Speter	case VSMINUS:
72817987Speter		if (!set) {
7291556Srgrimes			argstr(p, flag);
73017987Speter			break;
73117987Speter		}
73217987Speter		if (easy)
73317987Speter			goto record;
73417987Speter		break;
73517987Speter
73617987Speter	case VSTRIMLEFT:
73717987Speter	case VSTRIMLEFTMAX:
73817987Speter	case VSTRIMRIGHT:
73917987Speter	case VSTRIMRIGHTMAX:
74017987Speter		if (!set)
74117987Speter			break;
74217987Speter		/*
74317987Speter		 * Terminate the string and start recording the pattern
74417987Speter		 * right after it
74517987Speter		 */
74617987Speter		STPUTC('\0', expdest);
74717987Speter		pat = expdest;
74820425Ssteve		if (subevalvar(p, NULL, expdest - stackblock(), subtype,
74938887Stegge			       startloc, varflags) == 0) {
75025233Ssteve			int amount = (expdest - pat) + 1;
75125233Ssteve			STADJUST(-amount, expdest);
75225233Ssteve		}
75338887Stegge		/* Remove any recorded regions beyond start of variable */
75438887Stegge		removerecordregions(startloc);
75538887Stegge		goto record;
75617987Speter
75717987Speter	case VSASSIGN:
75817987Speter	case VSQUESTION:
75917987Speter		if (!set) {
76020425Ssteve			if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
76120425Ssteve				varflags &= ~VSNUL;
76238887Stegge				/*
76338887Stegge				 * Remove any recorded regions beyond
76438887Stegge				 * start of variable
76538887Stegge				 */
76638887Stegge				removerecordregions(startloc);
7671556Srgrimes				goto again;
76820425Ssteve			}
76917987Speter			break;
7701556Srgrimes		}
77117987Speter		if (easy)
77217987Speter			goto record;
77317987Speter		break;
77417987Speter
77517987Speter	default:
77617987Speter		abort();
7771556Srgrimes	}
77817987Speter
7791556Srgrimes	if (subtype != VSNORMAL) {	/* skip to end of alternative */
7801556Srgrimes		int nesting = 1;
7811556Srgrimes		for (;;) {
7821556Srgrimes			if ((c = *p++) == CTLESC)
7831556Srgrimes				p++;
7841556Srgrimes			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
7851556Srgrimes				if (set)
7861556Srgrimes					argbackq = argbackq->next;
7871556Srgrimes			} else if (c == CTLVAR) {
7881556Srgrimes				if ((*p++ & VSTYPE) != VSNORMAL)
7891556Srgrimes					nesting++;
7901556Srgrimes			} else if (c == CTLENDVAR) {
7911556Srgrimes				if (--nesting == 0)
7921556Srgrimes					break;
7931556Srgrimes			}
7941556Srgrimes		}
7951556Srgrimes	}
7961556Srgrimes	return p;
7971556Srgrimes}
7981556Srgrimes
7991556Srgrimes
8001556Srgrimes
8011556Srgrimes/*
8021556Srgrimes * Test whether a specialized variable is set.
8031556Srgrimes */
8041556Srgrimes
8051556SrgrimesSTATIC int
80625233Sstevevarisset(name, nulok)
80718202Speter	char *name;
80825233Ssteve	int nulok;
80925233Ssteve{
8101556Srgrimes
81125233Ssteve	if (*name == '!')
81225233Ssteve		return backgndpid != -1;
81325233Ssteve	else if (*name == '@' || *name == '*') {
8141556Srgrimes		if (*shellparam.p == NULL)
8151556Srgrimes			return 0;
81625233Ssteve
81725233Ssteve		if (nulok) {
81825233Ssteve			char **av;
81925233Ssteve
82025233Ssteve			for (av = shellparam.p; *av; av++)
82125233Ssteve				if (**av != '\0')
82225233Ssteve					return 1;
82325233Ssteve			return 0;
82425233Ssteve		}
82518202Speter	} else if (is_digit(*name)) {
82625233Ssteve		char *ap;
82720425Ssteve		int num = atoi(name);
82825233Ssteve
82925233Ssteve		if (num > shellparam.nparam)
83025233Ssteve			return 0;
83125233Ssteve
83225233Ssteve		if (num == 0)
83325233Ssteve			ap = arg0;
83425233Ssteve		else
83525233Ssteve			ap = shellparam.p[num - 1];
83625233Ssteve
83725233Ssteve		if (nulok && (ap == NULL || *ap == '\0'))
83825233Ssteve			return 0;
8391556Srgrimes	}
8401556Srgrimes	return 1;
8411556Srgrimes}
8421556Srgrimes
8431556Srgrimes
8441556Srgrimes
8451556Srgrimes/*
8461556Srgrimes * Add the value of a specialized variable to the stack string.
8471556Srgrimes */
8481556Srgrimes
8491556SrgrimesSTATIC void
8501556Srgrimesvarvalue(name, quoted, allow_split)
85118202Speter	char *name;
85217987Speter	int quoted;
85317987Speter	int allow_split;
85417987Speter{
8551556Srgrimes	int num;
8561556Srgrimes	char *p;
8571556Srgrimes	int i;
85817987Speter	extern int oexitstatus;
8591556Srgrimes	char sep;
8601556Srgrimes	char **ap;
8611556Srgrimes	char const *syntax;
8621556Srgrimes
8631556Srgrimes#define STRTODEST(p) \
8641556Srgrimes	do {\
8651556Srgrimes	if (allow_split) { \
8661556Srgrimes		syntax = quoted? DQSYNTAX : BASESYNTAX; \
8671556Srgrimes		while (*p) { \
8681556Srgrimes			if (syntax[*p] == CCTL) \
8691556Srgrimes				STPUTC(CTLESC, expdest); \
8701556Srgrimes			STPUTC(*p++, expdest); \
8711556Srgrimes		} \
8721556Srgrimes	} else \
8731556Srgrimes		while (*p) \
8741556Srgrimes			STPUTC(*p++, expdest); \
8751556Srgrimes	} while (0)
8761556Srgrimes
8771556Srgrimes
87818202Speter	switch (*name) {
8791556Srgrimes	case '$':
8801556Srgrimes		num = rootpid;
8811556Srgrimes		goto numvar;
8821556Srgrimes	case '?':
88317987Speter		num = oexitstatus;
8841556Srgrimes		goto numvar;
8851556Srgrimes	case '#':
8861556Srgrimes		num = shellparam.nparam;
8871556Srgrimes		goto numvar;
8881556Srgrimes	case '!':
8891556Srgrimes		num = backgndpid;
8901556Srgrimesnumvar:
89117987Speter		expdest = cvtnum(num, expdest);
8921556Srgrimes		break;
8931556Srgrimes	case '-':
8941556Srgrimes		for (i = 0 ; i < NOPTS ; i++) {
8951556Srgrimes			if (optlist[i].val)
8961556Srgrimes				STPUTC(optlist[i].letter, expdest);
8971556Srgrimes		}
8981556Srgrimes		break;
8991556Srgrimes	case '@':
90038887Stegge		if (allow_split && quoted) {
90138887Stegge			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
90238887Stegge				STRTODEST(p);
90338887Stegge				if (*ap)
90438887Stegge					STPUTC('\0', expdest);
90538887Stegge			}
90638887Stegge			break;
9071556Srgrimes		}
9088855Srgrimes		/* fall through */
9091556Srgrimes	case '*':
91038887Stegge		if (ifsset() != 0)
91138887Stegge			sep = ifsval()[0];
91238887Stegge		else
91338887Stegge			sep = ' ';
9141556Srgrimes		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
9151556Srgrimes			STRTODEST(p);
91638887Stegge			if (*ap && sep)
9171556Srgrimes				STPUTC(sep, expdest);
9181556Srgrimes		}
9191556Srgrimes		break;
9201556Srgrimes	case '0':
9211556Srgrimes		p = arg0;
9221556Srgrimes		STRTODEST(p);
9231556Srgrimes		break;
9241556Srgrimes	default:
92518202Speter		if (is_digit(*name)) {
92618202Speter			num = atoi(name);
92718202Speter			if (num > 0 && num <= shellparam.nparam) {
92818202Speter				p = shellparam.p[num - 1];
92918202Speter				STRTODEST(p);
93018202Speter			}
9311556Srgrimes		}
9321556Srgrimes		break;
9331556Srgrimes	}
9341556Srgrimes}
9351556Srgrimes
9361556Srgrimes
9371556Srgrimes
9381556Srgrimes/*
9391556Srgrimes * Record the the fact that we have to scan this region of the
9401556Srgrimes * string for IFS characters.
9411556Srgrimes */
9421556Srgrimes
9431556SrgrimesSTATIC void
94420425Ssteverecordregion(start, end, nulonly)
94517987Speter	int start;
94617987Speter	int end;
94717987Speter	int nulonly;
94817987Speter{
94925233Ssteve	struct ifsregion *ifsp;
9501556Srgrimes
9511556Srgrimes	if (ifslastp == NULL) {
9521556Srgrimes		ifsp = &ifsfirst;
9531556Srgrimes	} else {
9541556Srgrimes		ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
9551556Srgrimes		ifslastp->next = ifsp;
9561556Srgrimes	}
9571556Srgrimes	ifslastp = ifsp;
9581556Srgrimes	ifslastp->next = NULL;
9591556Srgrimes	ifslastp->begoff = start;
9601556Srgrimes	ifslastp->endoff = end;
9611556Srgrimes	ifslastp->nulonly = nulonly;
9621556Srgrimes}
9631556Srgrimes
9641556Srgrimes
9651556Srgrimes
9661556Srgrimes/*
9671556Srgrimes * Break the argument string into pieces based upon IFS and add the
9681556Srgrimes * strings to the argument list.  The regions of the string to be
9691556Srgrimes * searched for IFS characters have been stored by recordregion.
9701556Srgrimes */
9711556SrgrimesSTATIC void
9721556Srgrimesifsbreakup(string, arglist)
9731556Srgrimes	char *string;
9741556Srgrimes	struct arglist *arglist;
9751556Srgrimes	{
9761556Srgrimes	struct ifsregion *ifsp;
9771556Srgrimes	struct strlist *sp;
9781556Srgrimes	char *start;
97925233Ssteve	char *p;
9801556Srgrimes	char *q;
9811556Srgrimes	char *ifs;
98217987Speter	int ifsspc;
98338887Stegge	int nulonly;
9841556Srgrimes
98517987Speter
9861556Srgrimes	start = string;
98738887Stegge	ifsspc = 0;
98838887Stegge	nulonly = 0;
9891556Srgrimes	if (ifslastp != NULL) {
9901556Srgrimes		ifsp = &ifsfirst;
9911556Srgrimes		do {
9921556Srgrimes			p = string + ifsp->begoff;
99338887Stegge			nulonly = ifsp->nulonly;
99438887Stegge			ifs = nulonly ? nullstr :
99538887Stegge				( ifsset() ? ifsval() : " \t\n" );
99638887Stegge			ifsspc = 0;
9971556Srgrimes			while (p < string + ifsp->endoff) {
9981556Srgrimes				q = p;
9991556Srgrimes				if (*p == CTLESC)
10001556Srgrimes					p++;
100138887Stegge				if (strchr(ifs, *p)) {
100238887Stegge					if (!nulonly)
100338887Stegge						ifsspc = (strchr(" \t\n", *p) != NULL);
100438887Stegge					/* Ignore IFS whitespace at start */
100538887Stegge					if (q == start && ifsspc) {
100638887Stegge						p++;
100738887Stegge						start = p;
100838887Stegge						continue;
10091556Srgrimes					}
101038887Stegge					*q = '\0';
101138887Stegge					sp = (struct strlist *)stalloc(sizeof *sp);
101238887Stegge					sp->text = start;
101338887Stegge					*arglist->lastp = sp;
101438887Stegge					arglist->lastp = &sp->next;
101538887Stegge					p++;
101638887Stegge					if (!nulonly) {
10171556Srgrimes						for (;;) {
101838887Stegge							if (p >= string + ifsp->endoff) {
10191556Srgrimes								break;
102038887Stegge							}
10211556Srgrimes							q = p;
10221556Srgrimes							if (*p == CTLESC)
10231556Srgrimes								p++;
102438887Stegge							if (strchr(ifs, *p) == NULL ) {
10251556Srgrimes								p = q;
10261556Srgrimes								break;
102738887Stegge							} else if (strchr(" \t\n",*p) == NULL) {
102838887Stegge								if (ifsspc) {
102938887Stegge									p++;
103038887Stegge									ifsspc = 0;
103138887Stegge								} else {
103238887Stegge									p = q;
103338887Stegge									break;
103438887Stegge								}
103538887Stegge							} else
103638887Stegge								p++;
10371556Srgrimes						}
10381556Srgrimes					}
10391556Srgrimes					start = p;
104038887Stegge				} else
104138887Stegge					p++;
10421556Srgrimes			}
10431556Srgrimes		} while ((ifsp = ifsp->next) != NULL);
104438887Stegge		if (*start || (!ifsspc && start > string &&
104538887Stegge			(nulonly || 1))) {
10461556Srgrimes			sp = (struct strlist *)stalloc(sizeof *sp);
10471556Srgrimes			sp->text = start;
10481556Srgrimes			*arglist->lastp = sp;
10491556Srgrimes			arglist->lastp = &sp->next;
10501556Srgrimes		}
10511556Srgrimes	} else {
10521556Srgrimes		sp = (struct strlist *)stalloc(sizeof *sp);
10531556Srgrimes		sp->text = start;
10541556Srgrimes		*arglist->lastp = sp;
10551556Srgrimes		arglist->lastp = &sp->next;
10561556Srgrimes	}
10571556Srgrimes}
10581556Srgrimes
10591556Srgrimes
10601556Srgrimes
10611556Srgrimes/*
10621556Srgrimes * Expand shell metacharacters.  At this point, the only control characters
10631556Srgrimes * should be escapes.  The results are stored in the list exparg.
10641556Srgrimes */
10651556Srgrimes
10661556Srgrimeschar *expdir;
10671556Srgrimes
10681556Srgrimes
10691556SrgrimesSTATIC void
10701556Srgrimesexpandmeta(str, flag)
10711556Srgrimes	struct strlist *str;
107225905Ssteve	int flag __unused;
107317987Speter{
10741556Srgrimes	char *p;
10751556Srgrimes	struct strlist **savelastp;
10761556Srgrimes	struct strlist *sp;
10771556Srgrimes	char c;
10781556Srgrimes	/* TODO - EXP_REDIR */
10791556Srgrimes
10801556Srgrimes	while (str) {
10811556Srgrimes		if (fflag)
10821556Srgrimes			goto nometa;
10831556Srgrimes		p = str->text;
10841556Srgrimes		for (;;) {			/* fast check for meta chars */
10851556Srgrimes			if ((c = *p++) == '\0')
10861556Srgrimes				goto nometa;
10871556Srgrimes			if (c == '*' || c == '?' || c == '[' || c == '!')
10881556Srgrimes				break;
10891556Srgrimes		}
10901556Srgrimes		savelastp = exparg.lastp;
10911556Srgrimes		INTOFF;
10921556Srgrimes		if (expdir == NULL) {
10931556Srgrimes			int i = strlen(str->text);
10941556Srgrimes			expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
10951556Srgrimes		}
10961556Srgrimes
10971556Srgrimes		expmeta(expdir, str->text);
10981556Srgrimes		ckfree(expdir);
10991556Srgrimes		expdir = NULL;
11001556Srgrimes		INTON;
11011556Srgrimes		if (exparg.lastp == savelastp) {
11028855Srgrimes			/*
11038855Srgrimes			 * no matches
11041556Srgrimes			 */
11051556Srgrimesnometa:
11061556Srgrimes			*exparg.lastp = str;
11071556Srgrimes			rmescapes(str->text);
11081556Srgrimes			exparg.lastp = &str->next;
11091556Srgrimes		} else {
11101556Srgrimes			*exparg.lastp = NULL;
11111556Srgrimes			*savelastp = sp = expsort(*savelastp);
11121556Srgrimes			while (sp->next != NULL)
11131556Srgrimes				sp = sp->next;
11141556Srgrimes			exparg.lastp = &sp->next;
11151556Srgrimes		}
11161556Srgrimes		str = str->next;
11171556Srgrimes	}
11181556Srgrimes}
11191556Srgrimes
11201556Srgrimes
11211556Srgrimes/*
11221556Srgrimes * Do metacharacter (i.e. *, ?, [...]) expansion.
11231556Srgrimes */
11241556Srgrimes
11251556SrgrimesSTATIC void
11261556Srgrimesexpmeta(enddir, name)
11271556Srgrimes	char *enddir;
11281556Srgrimes	char *name;
11291556Srgrimes	{
113025233Ssteve	char *p;
11311556Srgrimes	char *q;
11321556Srgrimes	char *start;
11331556Srgrimes	char *endname;
11341556Srgrimes	int metaflag;
11351556Srgrimes	struct stat statb;
11361556Srgrimes	DIR *dirp;
11371556Srgrimes	struct dirent *dp;
11381556Srgrimes	int atend;
11391556Srgrimes	int matchdot;
11401556Srgrimes
11411556Srgrimes	metaflag = 0;
11421556Srgrimes	start = name;
11431556Srgrimes	for (p = name ; ; p++) {
11441556Srgrimes		if (*p == '*' || *p == '?')
11451556Srgrimes			metaflag = 1;
11461556Srgrimes		else if (*p == '[') {
11471556Srgrimes			q = p + 1;
114826488Sache			if (*q == '!' || *q == '^')
11491556Srgrimes				q++;
11501556Srgrimes			for (;;) {
115138887Stegge				while (*q == CTLQUOTEMARK)
115238887Stegge					q++;
11531556Srgrimes				if (*q == CTLESC)
11541556Srgrimes					q++;
11551556Srgrimes				if (*q == '/' || *q == '\0')
11561556Srgrimes					break;
11571556Srgrimes				if (*++q == ']') {
11581556Srgrimes					metaflag = 1;
11591556Srgrimes					break;
11601556Srgrimes				}
11611556Srgrimes			}
11621556Srgrimes		} else if (*p == '!' && p[1] == '!'	&& (p == name || p[-1] == '/')) {
11631556Srgrimes			metaflag = 1;
11641556Srgrimes		} else if (*p == '\0')
11651556Srgrimes			break;
116638887Stegge		else if (*p == CTLQUOTEMARK)
116738887Stegge			continue;
11681556Srgrimes		else if (*p == CTLESC)
11691556Srgrimes			p++;
11701556Srgrimes		if (*p == '/') {
11711556Srgrimes			if (metaflag)
11721556Srgrimes				break;
11731556Srgrimes			start = p + 1;
11741556Srgrimes		}
11751556Srgrimes	}
11761556Srgrimes	if (metaflag == 0) {	/* we've reached the end of the file name */
11771556Srgrimes		if (enddir != expdir)
11781556Srgrimes			metaflag++;
11791556Srgrimes		for (p = name ; ; p++) {
118038887Stegge			if (*p == CTLQUOTEMARK)
118138887Stegge				continue;
11821556Srgrimes			if (*p == CTLESC)
11831556Srgrimes				p++;
11841556Srgrimes			*enddir++ = *p;
11851556Srgrimes			if (*p == '\0')
11861556Srgrimes				break;
11871556Srgrimes		}
11881556Srgrimes		if (metaflag == 0 || stat(expdir, &statb) >= 0)
11891556Srgrimes			addfname(expdir);
11901556Srgrimes		return;
11911556Srgrimes	}
11921556Srgrimes	endname = p;
11931556Srgrimes	if (start != name) {
11941556Srgrimes		p = name;
11951556Srgrimes		while (p < start) {
119638887Stegge			while (*p == CTLQUOTEMARK)
119738887Stegge				p++;
11981556Srgrimes			if (*p == CTLESC)
11991556Srgrimes				p++;
12001556Srgrimes			*enddir++ = *p++;
12011556Srgrimes		}
12021556Srgrimes	}
12031556Srgrimes	if (enddir == expdir) {
12041556Srgrimes		p = ".";
12051556Srgrimes	} else if (enddir == expdir + 1 && *expdir == '/') {
12061556Srgrimes		p = "/";
12071556Srgrimes	} else {
12081556Srgrimes		p = expdir;
12091556Srgrimes		enddir[-1] = '\0';
12101556Srgrimes	}
12111556Srgrimes	if ((dirp = opendir(p)) == NULL)
12121556Srgrimes		return;
12131556Srgrimes	if (enddir != expdir)
12141556Srgrimes		enddir[-1] = '/';
12151556Srgrimes	if (*endname == 0) {
12161556Srgrimes		atend = 1;
12171556Srgrimes	} else {
12181556Srgrimes		atend = 0;
12191556Srgrimes		*endname++ = '\0';
12201556Srgrimes	}
12211556Srgrimes	matchdot = 0;
122238887Stegge	p = start;
122338887Stegge	while (*p == CTLQUOTEMARK)
122438887Stegge		p++;
122538887Stegge	if (*p == CTLESC)
122638887Stegge		p++;
122738887Stegge	if (*p == '.')
12281556Srgrimes		matchdot++;
12291556Srgrimes	while (! int_pending() && (dp = readdir(dirp)) != NULL) {
12301556Srgrimes		if (dp->d_name[0] == '.' && ! matchdot)
12311556Srgrimes			continue;
123245514Stegge		if (patmatch(start, dp->d_name, 0)) {
12331556Srgrimes			if (atend) {
12341556Srgrimes				scopy(dp->d_name, enddir);
12351556Srgrimes				addfname(expdir);
12361556Srgrimes			} else {
12371556Srgrimes				char *q;
123817987Speter				for (p = enddir, q = dp->d_name;
123917987Speter				     (*p++ = *q++) != '\0';)
124017987Speter					continue;
12411556Srgrimes				p[-1] = '/';
12421556Srgrimes				expmeta(p, endname);
12431556Srgrimes			}
12441556Srgrimes		}
12451556Srgrimes	}
12461556Srgrimes	closedir(dirp);
12471556Srgrimes	if (! atend)
12481556Srgrimes		endname[-1] = '/';
12491556Srgrimes}
12501556Srgrimes
12511556Srgrimes
12521556Srgrimes/*
12531556Srgrimes * Add a file name to the list.
12541556Srgrimes */
12551556Srgrimes
12561556SrgrimesSTATIC void
12571556Srgrimesaddfname(name)
12581556Srgrimes	char *name;
12591556Srgrimes	{
12601556Srgrimes	char *p;
12611556Srgrimes	struct strlist *sp;
12621556Srgrimes
12631556Srgrimes	p = stalloc(strlen(name) + 1);
12641556Srgrimes	scopy(name, p);
12651556Srgrimes	sp = (struct strlist *)stalloc(sizeof *sp);
12661556Srgrimes	sp->text = p;
12671556Srgrimes	*exparg.lastp = sp;
12681556Srgrimes	exparg.lastp = &sp->next;
12691556Srgrimes}
12701556Srgrimes
12711556Srgrimes
12721556Srgrimes/*
12731556Srgrimes * Sort the results of file name expansion.  It calculates the number of
12741556Srgrimes * strings to sort and then calls msort (short for merge sort) to do the
12751556Srgrimes * work.
12761556Srgrimes */
12771556Srgrimes
12781556SrgrimesSTATIC struct strlist *
12791556Srgrimesexpsort(str)
12801556Srgrimes	struct strlist *str;
12811556Srgrimes	{
12821556Srgrimes	int len;
12831556Srgrimes	struct strlist *sp;
12841556Srgrimes
12851556Srgrimes	len = 0;
12861556Srgrimes	for (sp = str ; sp ; sp = sp->next)
12871556Srgrimes		len++;
12881556Srgrimes	return msort(str, len);
12891556Srgrimes}
12901556Srgrimes
12911556Srgrimes
12921556SrgrimesSTATIC struct strlist *
12931556Srgrimesmsort(list, len)
12941556Srgrimes	struct strlist *list;
129517987Speter	int len;
129617987Speter{
129717987Speter	struct strlist *p, *q = NULL;
12981556Srgrimes	struct strlist **lpp;
12991556Srgrimes	int half;
13001556Srgrimes	int n;
13011556Srgrimes
13021556Srgrimes	if (len <= 1)
13031556Srgrimes		return list;
13048855Srgrimes	half = len >> 1;
13051556Srgrimes	p = list;
13061556Srgrimes	for (n = half ; --n >= 0 ; ) {
13071556Srgrimes		q = p;
13081556Srgrimes		p = p->next;
13091556Srgrimes	}
13101556Srgrimes	q->next = NULL;			/* terminate first half of list */
13111556Srgrimes	q = msort(list, half);		/* sort first half of list */
13121556Srgrimes	p = msort(p, len - half);		/* sort second half */
13131556Srgrimes	lpp = &list;
13141556Srgrimes	for (;;) {
13151556Srgrimes		if (strcmp(p->text, q->text) < 0) {
13161556Srgrimes			*lpp = p;
13171556Srgrimes			lpp = &p->next;
13181556Srgrimes			if ((p = *lpp) == NULL) {
13191556Srgrimes				*lpp = q;
13201556Srgrimes				break;
13211556Srgrimes			}
13221556Srgrimes		} else {
13231556Srgrimes			*lpp = q;
13241556Srgrimes			lpp = &q->next;
13251556Srgrimes			if ((q = *lpp) == NULL) {
13261556Srgrimes				*lpp = p;
13271556Srgrimes				break;
13281556Srgrimes			}
13291556Srgrimes		}
13301556Srgrimes	}
13311556Srgrimes	return list;
13321556Srgrimes}
13331556Srgrimes
13341556Srgrimes
13351556Srgrimes
13361556Srgrimes/*
13371556Srgrimes * Returns true if the pattern matches the string.
13381556Srgrimes */
13391556Srgrimes
13401556Srgrimesint
134145514Steggepatmatch(pattern, string, squoted)
13421556Srgrimes	char *pattern;
13431556Srgrimes	char *string;
134445514Stegge	int squoted;	/* string might have quote chars */
13451556Srgrimes	{
13461556Srgrimes#ifdef notdef
13471556Srgrimes	if (pattern[0] == '!' && pattern[1] == '!')
13481556Srgrimes		return 1 - pmatch(pattern + 2, string);
13491556Srgrimes	else
13501556Srgrimes#endif
135145514Stegge		return pmatch(pattern, string, squoted);
13521556Srgrimes}
13531556Srgrimes
135417987Speter
135517525SacheSTATIC int
135645514Steggepmatch(pattern, string, squoted)
13571556Srgrimes	char *pattern;
13581556Srgrimes	char *string;
135945514Stegge	int squoted;
13601556Srgrimes	{
136125233Ssteve	char *p, *q;
136225233Ssteve	char c;
13631556Srgrimes
13641556Srgrimes	p = pattern;
13651556Srgrimes	q = string;
13661556Srgrimes	for (;;) {
13671556Srgrimes		switch (c = *p++) {
13681556Srgrimes		case '\0':
13691556Srgrimes			goto breakloop;
13701556Srgrimes		case CTLESC:
137145514Stegge			if (squoted && *q == CTLESC)
137245514Stegge				q++;
13731556Srgrimes			if (*q++ != *p++)
13741556Srgrimes				return 0;
13751556Srgrimes			break;
137638887Stegge		case CTLQUOTEMARK:
137738887Stegge			continue;
13781556Srgrimes		case '?':
137945514Stegge			if (squoted && *q == CTLESC)
138045514Stegge				q++;
13811556Srgrimes			if (*q++ == '\0')
13821556Srgrimes				return 0;
13831556Srgrimes			break;
13841556Srgrimes		case '*':
13851556Srgrimes			c = *p;
138638887Stegge			while (c == CTLQUOTEMARK || c == '*')
138738887Stegge				c = *++p;
138838887Stegge			if (c != CTLESC &&  c != CTLQUOTEMARK &&
138938887Stegge			    c != '?' && c != '*' && c != '[') {
13901556Srgrimes				while (*q != c) {
139145514Stegge					if (squoted && *q == CTLESC &&
139245514Stegge					    q[1] == c)
139345514Stegge						break;
13941556Srgrimes					if (*q == '\0')
13951556Srgrimes						return 0;
139645514Stegge					if (squoted && *q == CTLESC)
139745514Stegge						q++;
13981556Srgrimes					q++;
13991556Srgrimes				}
14001556Srgrimes			}
14011556Srgrimes			do {
140245514Stegge				if (pmatch(p, q, squoted))
14031556Srgrimes					return 1;
140445514Stegge				if (squoted && *q == CTLESC)
140545514Stegge					q++;
14061556Srgrimes			} while (*q++ != '\0');
14071556Srgrimes			return 0;
14081556Srgrimes		case '[': {
14091556Srgrimes			char *endp;
14101556Srgrimes			int invert, found;
14111556Srgrimes			char chr;
14121556Srgrimes
14131556Srgrimes			endp = p;
141426488Sache			if (*endp == '!' || *endp == '^')
14151556Srgrimes				endp++;
14161556Srgrimes			for (;;) {
141738887Stegge				while (*endp == CTLQUOTEMARK)
141838887Stegge					endp++;
14191556Srgrimes				if (*endp == '\0')
14201556Srgrimes					goto dft;		/* no matching ] */
14211556Srgrimes				if (*endp == CTLESC)
14221556Srgrimes					endp++;
14231556Srgrimes				if (*++endp == ']')
14241556Srgrimes					break;
14251556Srgrimes			}
14261556Srgrimes			invert = 0;
142726488Sache			if (*p == '!' || *p == '^') {
14281556Srgrimes				invert++;
14291556Srgrimes				p++;
14301556Srgrimes			}
14311556Srgrimes			found = 0;
14321556Srgrimes			chr = *q++;
143345514Stegge			if (squoted && chr == CTLESC)
143445514Stegge				chr = *q++;
143517987Speter			if (chr == '\0')
143617987Speter				return 0;
14371556Srgrimes			c = *p++;
14381556Srgrimes			do {
143938887Stegge				if (c == CTLQUOTEMARK)
144038887Stegge					continue;
14411556Srgrimes				if (c == CTLESC)
14421556Srgrimes					c = *p++;
14431556Srgrimes				if (*p == '-' && p[1] != ']') {
14441556Srgrimes					p++;
144538887Stegge					while (*p == CTLQUOTEMARK)
144638887Stegge						p++;
14471556Srgrimes					if (*p == CTLESC)
14481556Srgrimes						p++;
144917557Sache					if (   collate_range_cmp(chr, c) >= 0
145017557Sache					    && collate_range_cmp(chr, *p) <= 0
145117525Sache					   )
14521556Srgrimes						found = 1;
14531556Srgrimes					p++;
14541556Srgrimes				} else {
14551556Srgrimes					if (chr == c)
14561556Srgrimes						found = 1;
14571556Srgrimes				}
14581556Srgrimes			} while ((c = *p++) != ']');
14591556Srgrimes			if (found == invert)
14601556Srgrimes				return 0;
14611556Srgrimes			break;
14621556Srgrimes		}
14631556Srgrimesdft:	        default:
146445514Stegge			if (squoted && *q == CTLESC)
146545514Stegge				q++;
14661556Srgrimes			if (*q++ != c)
14671556Srgrimes				return 0;
14681556Srgrimes			break;
14691556Srgrimes		}
14701556Srgrimes	}
14711556Srgrimesbreakloop:
14721556Srgrimes	if (*q != '\0')
14731556Srgrimes		return 0;
14741556Srgrimes	return 1;
14751556Srgrimes}
14761556Srgrimes
14771556Srgrimes
14781556Srgrimes
14791556Srgrimes/*
14801556Srgrimes * Remove any CTLESC characters from a string.
14811556Srgrimes */
14821556Srgrimes
14831556Srgrimesvoid
14841556Srgrimesrmescapes(str)
14851556Srgrimes	char *str;
148638887Stegge{
148725233Ssteve	char *p, *q;
14881556Srgrimes
14891556Srgrimes	p = str;
149038887Stegge	while (*p != CTLESC && *p != CTLQUOTEMARK) {
14911556Srgrimes		if (*p++ == '\0')
14921556Srgrimes			return;
14931556Srgrimes	}
14941556Srgrimes	q = p;
14951556Srgrimes	while (*p) {
149638887Stegge		if (*p == CTLQUOTEMARK) {
149738887Stegge			p++;
149838887Stegge			continue;
149938887Stegge		}
15001556Srgrimes		if (*p == CTLESC)
15011556Srgrimes			p++;
15021556Srgrimes		*q++ = *p++;
15031556Srgrimes	}
15041556Srgrimes	*q = '\0';
15051556Srgrimes}
15061556Srgrimes
15071556Srgrimes
15081556Srgrimes
15091556Srgrimes/*
15101556Srgrimes * See if a pattern matches in a case statement.
15111556Srgrimes */
15121556Srgrimes
15131556Srgrimesint
15141556Srgrimescasematch(pattern, val)
15151556Srgrimes	union node *pattern;
15161556Srgrimes	char *val;
15171556Srgrimes	{
15181556Srgrimes	struct stackmark smark;
15191556Srgrimes	int result;
15201556Srgrimes	char *p;
15211556Srgrimes
15221556Srgrimes	setstackmark(&smark);
15231556Srgrimes	argbackq = pattern->narg.backquote;
15241556Srgrimes	STARTSTACKSTR(expdest);
15251556Srgrimes	ifslastp = NULL;
15261556Srgrimes	argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
15271556Srgrimes	STPUTC('\0', expdest);
15281556Srgrimes	p = grabstackstr(expdest);
152945514Stegge	result = patmatch(p, val, 0);
15301556Srgrimes	popstackmark(&smark);
15311556Srgrimes	return result;
15321556Srgrimes}
153317987Speter
153417987Speter/*
153517987Speter * Our own itoa().
153617987Speter */
153717987Speter
153817987SpeterSTATIC char *
153917987Spetercvtnum(num, buf)
154017987Speter	int num;
154117987Speter	char *buf;
154217987Speter	{
154317987Speter	char temp[32];
154417987Speter	int neg = num < 0;
154517987Speter	char *p = temp + 31;
154617987Speter
154717987Speter	temp[31] = '\0';
154817987Speter
154917987Speter	do {
155017987Speter		*--p = num % 10 + '0';
155117987Speter	} while ((num /= 10) != 0);
155217987Speter
155317987Speter	if (neg)
155417987Speter		*--p = '-';
155517987Speter
155617987Speter	while (*p)
155717987Speter		STPUTC(*p++, buf);
155817987Speter	return buf;
155917987Speter}
1560