expand.c revision 221646
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
4207944Sjilles * Copyright (c) 1997-2005
5207944Sjilles *	Herbert Xu <herbert@gondor.apana.org.au>.  All rights reserved.
61556Srgrimes *
71556Srgrimes * This code is derived from software contributed to Berkeley by
81556Srgrimes * Kenneth Almquist.
91556Srgrimes *
101556Srgrimes * Redistribution and use in source and binary forms, with or without
111556Srgrimes * modification, are permitted provided that the following conditions
121556Srgrimes * are met:
131556Srgrimes * 1. Redistributions of source code must retain the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer.
151556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161556Srgrimes *    notice, this list of conditions and the following disclaimer in the
171556Srgrimes *    documentation and/or other materials provided with the distribution.
181556Srgrimes * 4. Neither the name of the University nor the names of its contributors
191556Srgrimes *    may be used to endorse or promote products derived from this software
201556Srgrimes *    without specific prior written permission.
211556Srgrimes *
221556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321556Srgrimes * SUCH DAMAGE.
331556Srgrimes */
341556Srgrimes
351556Srgrimes#ifndef lint
3636150Scharnier#if 0
3736150Scharnierstatic char sccsid[] = "@(#)expand.c	8.5 (Berkeley) 5/15/95";
3836150Scharnier#endif
391556Srgrimes#endif /* not lint */
4099110Sobrien#include <sys/cdefs.h>
4199110Sobrien__FBSDID("$FreeBSD: head/bin/sh/expand.c 221646 2011-05-08 11:32:20Z jilles $");
421556Srgrimes
4317987Speter#include <sys/types.h>
4417987Speter#include <sys/time.h>
4517987Speter#include <sys/stat.h>
46213775Sjhb#include <dirent.h>
4717987Speter#include <errno.h>
48213775Sjhb#include <inttypes.h>
49213775Sjhb#include <limits.h>
5017987Speter#include <pwd.h>
51213775Sjhb#include <stdio.h>
5217987Speter#include <stdlib.h>
53108286Stjr#include <string.h>
54213775Sjhb#include <unistd.h>
55221646Sjilles#include <wchar.h>
5617987Speter
571556Srgrimes/*
581556Srgrimes * Routines to expand arguments to commands.  We have to deal with
591556Srgrimes * backquotes, shell variables, and file metacharacters.
601556Srgrimes */
611556Srgrimes
621556Srgrimes#include "shell.h"
631556Srgrimes#include "main.h"
641556Srgrimes#include "nodes.h"
651556Srgrimes#include "eval.h"
661556Srgrimes#include "expand.h"
671556Srgrimes#include "syntax.h"
681556Srgrimes#include "parser.h"
691556Srgrimes#include "jobs.h"
701556Srgrimes#include "options.h"
711556Srgrimes#include "var.h"
721556Srgrimes#include "input.h"
731556Srgrimes#include "output.h"
741556Srgrimes#include "memalloc.h"
751556Srgrimes#include "error.h"
761556Srgrimes#include "mystring.h"
7717987Speter#include "arith.h"
7817987Speter#include "show.h"
791556Srgrimes
801556Srgrimes/*
811556Srgrimes * Structure specifying which parts of the string should be searched
821556Srgrimes * for IFS characters.
831556Srgrimes */
841556Srgrimes
851556Srgrimesstruct ifsregion {
861556Srgrimes	struct ifsregion *next;	/* next region in list */
871556Srgrimes	int begoff;		/* offset of start of region */
881556Srgrimes	int endoff;		/* offset of end of region */
89194975Sjilles	int inquotes;		/* search for nul bytes only */
901556Srgrimes};
911556Srgrimes
921556Srgrimes
93213760Sobrienstatic char *expdest;			/* output of current string */
94213760Sobrienstatic struct nodelist *argbackq;	/* list of back quote expressions */
95213760Sobrienstatic struct ifsregion ifsfirst;	/* first struct in list of ifs regions */
96213760Sobrienstatic struct ifsregion *ifslastp;	/* last struct in list */
97213760Sobrienstatic struct arglist exparg;		/* holds expanded arg list */
981556Srgrimes
99213811Sobrienstatic void argstr(char *, int);
100213811Sobrienstatic char *exptilde(char *, int);
101213811Sobrienstatic void expbackq(union node *, int, int);
102214524Sjillesstatic int subevalvar(char *, char *, int, int, int, int, int);
103213811Sobrienstatic char *evalvar(char *, int);
104213811Sobrienstatic int varisset(char *, int);
105213811Sobrienstatic void varvalue(char *, int, int, int);
106213811Sobrienstatic void recordregion(int, int, int);
107213811Sobrienstatic void removerecordregions(int);
108213811Sobrienstatic void ifsbreakup(char *, struct arglist *);
109213811Sobrienstatic void expandmeta(struct strlist *, int);
110213811Sobrienstatic void expmeta(char *, char *);
111213811Sobrienstatic void addfname(char *);
112213811Sobrienstatic struct strlist *expsort(struct strlist *);
113213811Sobrienstatic struct strlist *msort(struct strlist *, int);
114213811Sobrienstatic char *cvtnum(int, char *);
115221646Sjillesstatic int collate_range_cmp(wchar_t, wchar_t);
1161556Srgrimes
117213811Sobrienstatic int
118221646Sjillescollate_range_cmp(wchar_t c1, wchar_t c2)
11919281Sache{
120221646Sjilles	static wchar_t s1[2], s2[2];
12119281Sache
12219281Sache	s1[0] = c1;
12319281Sache	s2[0] = c2;
124221646Sjilles	return (wcscoll(s1, s2));
12519281Sache}
12619281Sache
1271556Srgrimes/*
1281556Srgrimes * Expand shell variables and backquotes inside a here document.
12990111Simp *	union node *arg		the document
13090111Simp *	int fd;			where to write the expanded version
1311556Srgrimes */
1321556Srgrimes
1331556Srgrimesvoid
13490111Simpexpandhere(union node *arg, int fd)
13590111Simp{
1361556Srgrimes	expandarg(arg, (struct arglist *)NULL, 0);
13739137Stegge	xwrite(fd, stackblock(), expdest - stackblock());
1381556Srgrimes}
1391556Srgrimes
140216384Sjillesstatic char *
141216384Sjillesstputs_quotes(const char *data, const char *syntax, char *p)
142216384Sjilles{
143216384Sjilles	while (*data) {
144216384Sjilles		CHECKSTRSPACE(2, p);
145216384Sjilles		if (syntax[(int)*data] == CCTL)
146216384Sjilles			USTPUTC(CTLESC, p);
147216384Sjilles		USTPUTC(*data++, p);
148216384Sjilles	}
149216384Sjilles	return (p);
150216384Sjilles}
151216384Sjilles#define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p)
1521556Srgrimes
1531556Srgrimes/*
154212243Sjilles * Perform expansions on an argument, placing the resulting list of arguments
155212243Sjilles * in arglist.  Parameter expansion, command substitution and arithmetic
156212243Sjilles * expansion are always performed; additional expansions can be requested
157212243Sjilles * via flag (EXP_*).
158212243Sjilles * The result is left in the stack string.
159218203Sjilles * When arglist is NULL, perform here document expansion.
160212243Sjilles *
161212243Sjilles * Caution: this function uses global state and is not reentrant.
162212243Sjilles * However, a new invocation after an interrupted invocation is safe
163212243Sjilles * and will reset the global state for the new call.
1641556Srgrimes */
1651556Srgrimesvoid
16690111Simpexpandarg(union node *arg, struct arglist *arglist, int flag)
16717987Speter{
1681556Srgrimes	struct strlist *sp;
1691556Srgrimes	char *p;
1701556Srgrimes
1711556Srgrimes	argbackq = arg->narg.backquote;
1721556Srgrimes	STARTSTACKSTR(expdest);
1731556Srgrimes	ifsfirst.next = NULL;
1741556Srgrimes	ifslastp = NULL;
1751556Srgrimes	argstr(arg->narg.text, flag);
1761556Srgrimes	if (arglist == NULL) {
1771556Srgrimes		return;			/* here document expanded */
1781556Srgrimes	}
1791556Srgrimes	STPUTC('\0', expdest);
1801556Srgrimes	p = grabstackstr(expdest);
1811556Srgrimes	exparg.lastp = &exparg.list;
1821556Srgrimes	/*
1831556Srgrimes	 * TODO - EXP_REDIR
1841556Srgrimes	 */
1851556Srgrimes	if (flag & EXP_FULL) {
1861556Srgrimes		ifsbreakup(p, &exparg);
1871556Srgrimes		*exparg.lastp = NULL;
1881556Srgrimes		exparg.lastp = &exparg.list;
1891556Srgrimes		expandmeta(exparg.list, flag);
1901556Srgrimes	} else {
1911556Srgrimes		if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
1921556Srgrimes			rmescapes(p);
1931556Srgrimes		sp = (struct strlist *)stalloc(sizeof (struct strlist));
1941556Srgrimes		sp->text = p;
1951556Srgrimes		*exparg.lastp = sp;
1961556Srgrimes		exparg.lastp = &sp->next;
1971556Srgrimes	}
1981556Srgrimes	while (ifsfirst.next != NULL) {
1991556Srgrimes		struct ifsregion *ifsp;
2001556Srgrimes		INTOFF;
2011556Srgrimes		ifsp = ifsfirst.next->next;
2021556Srgrimes		ckfree(ifsfirst.next);
2031556Srgrimes		ifsfirst.next = ifsp;
2041556Srgrimes		INTON;
2051556Srgrimes	}
2061556Srgrimes	*exparg.lastp = NULL;
2071556Srgrimes	if (exparg.list) {
2081556Srgrimes		*arglist->lastp = exparg.list;
2091556Srgrimes		arglist->lastp = exparg.lastp;
2101556Srgrimes	}
2111556Srgrimes}
2121556Srgrimes
2131556Srgrimes
2141556Srgrimes
2151556Srgrimes/*
216212243Sjilles * Perform parameter expansion, command substitution and arithmetic
217212243Sjilles * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
218212243Sjilles * Processing ends at a CTLENDVAR character as well as '\0'.
219212243Sjilles * This is used to expand word in ${var+word} etc.
220212243Sjilles * If EXP_FULL, EXP_CASE or EXP_REDIR are set, keep and/or generate CTLESC
221212243Sjilles * characters to allow for further processing.
222212243Sjilles * If EXP_FULL is set, also preserve CTLQUOTEMARK characters.
2231556Srgrimes */
224213811Sobrienstatic void
22590111Simpargstr(char *p, int flag)
22617987Speter{
22725233Ssteve	char c;
228104672Stjr	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);	/* do CTLESC */
2291556Srgrimes	int firsteq = 1;
230214512Sjilles	int split_lit;
231214512Sjilles	int lit_quoted;
2321556Srgrimes
233214512Sjilles	split_lit = flag & EXP_SPLIT_LIT;
234214512Sjilles	lit_quoted = flag & EXP_LIT_QUOTED;
235214512Sjilles	flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED);
2361556Srgrimes	if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
2371556Srgrimes		p = exptilde(p, flag);
2381556Srgrimes	for (;;) {
239215783Sjilles		CHECKSTRSPACE(2, expdest);
2401556Srgrimes		switch (c = *p++) {
2411556Srgrimes		case '\0':
242212243Sjilles		case CTLENDVAR:
2431556Srgrimes			goto breakloop;
24438887Stegge		case CTLQUOTEMARK:
245214512Sjilles			lit_quoted = 1;
24638887Stegge			/* "$@" syntax adherence hack */
24738887Stegge			if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
24838887Stegge				break;
24939137Stegge			if ((flag & EXP_FULL) != 0)
250215783Sjilles				USTPUTC(c, expdest);
25138887Stegge			break;
252214512Sjilles		case CTLQUOTEEND:
253214512Sjilles			lit_quoted = 0;
254214512Sjilles			break;
2551556Srgrimes		case CTLESC:
2561556Srgrimes			if (quotes)
257215783Sjilles				USTPUTC(c, expdest);
2581556Srgrimes			c = *p++;
259215783Sjilles			USTPUTC(c, expdest);
260214512Sjilles			if (split_lit && !lit_quoted)
261214512Sjilles				recordregion(expdest - stackblock() -
262214512Sjilles				    (quotes ? 2 : 1),
263214512Sjilles				    expdest - stackblock(), 0);
2641556Srgrimes			break;
2651556Srgrimes		case CTLVAR:
2661556Srgrimes			p = evalvar(p, flag);
2671556Srgrimes			break;
2681556Srgrimes		case CTLBACKQ:
2691556Srgrimes		case CTLBACKQ|CTLQUOTE:
2701556Srgrimes			expbackq(argbackq->n, c & CTLQUOTE, flag);
2711556Srgrimes			argbackq = argbackq->next;
2721556Srgrimes			break;
2731556Srgrimes		case CTLENDARI:
2741556Srgrimes			expari(flag);
2751556Srgrimes			break;
2761556Srgrimes		case ':':
2771556Srgrimes		case '=':
2781556Srgrimes			/*
2791556Srgrimes			 * sort of a hack - expand tildes in variable
2801556Srgrimes			 * assignments (after the first '=' and after ':'s).
2811556Srgrimes			 */
282215783Sjilles			USTPUTC(c, expdest);
283214512Sjilles			if (split_lit && !lit_quoted)
284214512Sjilles				recordregion(expdest - stackblock() - 1,
285214512Sjilles				    expdest - stackblock(), 0);
286214512Sjilles			if (flag & EXP_VARTILDE && *p == '~' &&
287214512Sjilles			    (c != '=' || firsteq)) {
288214512Sjilles				if (c == '=')
289214512Sjilles					firsteq = 0;
2901556Srgrimes				p = exptilde(p, flag);
2911556Srgrimes			}
2921556Srgrimes			break;
2931556Srgrimes		default:
294215783Sjilles			USTPUTC(c, expdest);
295214512Sjilles			if (split_lit && !lit_quoted)
296214512Sjilles				recordregion(expdest - stackblock() - 1,
297214512Sjilles				    expdest - stackblock(), 0);
2981556Srgrimes		}
2991556Srgrimes	}
3001556Srgrimesbreakloop:;
3011556Srgrimes}
3021556Srgrimes
303212243Sjilles/*
304212243Sjilles * Perform tilde expansion, placing the result in the stack string and
305212243Sjilles * returning the next position in the input string to process.
306212243Sjilles */
307213811Sobrienstatic char *
30890111Simpexptilde(char *p, int flag)
30917987Speter{
3101556Srgrimes	char c, *startp = p;
3111556Srgrimes	struct passwd *pw;
3121556Srgrimes	char *home;
313108935Stjr	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
3141556Srgrimes
31517987Speter	while ((c = *p) != '\0') {
3161556Srgrimes		switch(c) {
317200988Sjilles		case CTLESC: /* This means CTL* are always considered quoted. */
318200988Sjilles		case CTLVAR:
319200988Sjilles		case CTLBACKQ:
320200988Sjilles		case CTLBACKQ | CTLQUOTE:
321200988Sjilles		case CTLARI:
322200988Sjilles		case CTLENDARI:
32339137Stegge		case CTLQUOTEMARK:
32439137Stegge			return (startp);
3251556Srgrimes		case ':':
3261556Srgrimes			if (flag & EXP_VARTILDE)
3271556Srgrimes				goto done;
3281556Srgrimes			break;
3291556Srgrimes		case '/':
330206150Sjilles		case CTLENDVAR:
3311556Srgrimes			goto done;
3321556Srgrimes		}
3331556Srgrimes		p++;
3341556Srgrimes	}
3351556Srgrimesdone:
3361556Srgrimes	*p = '\0';
3371556Srgrimes	if (*(startp+1) == '\0') {
3381556Srgrimes		if ((home = lookupvar("HOME")) == NULL)
3391556Srgrimes			goto lose;
3401556Srgrimes	} else {
3411556Srgrimes		if ((pw = getpwnam(startp+1)) == NULL)
3421556Srgrimes			goto lose;
3431556Srgrimes		home = pw->pw_dir;
3441556Srgrimes	}
3451556Srgrimes	if (*home == '\0')
3461556Srgrimes		goto lose;
3471556Srgrimes	*p = c;
348216384Sjilles	if (quotes)
349216384Sjilles		STPUTS_QUOTES(home, SQSYNTAX, expdest);
350216384Sjilles	else
351216384Sjilles		STPUTS(home, expdest);
3521556Srgrimes	return (p);
3531556Srgrimeslose:
3541556Srgrimes	*p = c;
3551556Srgrimes	return (startp);
3561556Srgrimes}
3571556Srgrimes
3581556Srgrimes
359213811Sobrienstatic void
36090111Simpremoverecordregions(int endoff)
36138887Stegge{
36238887Stegge	if (ifslastp == NULL)
36338887Stegge		return;
36438887Stegge
36538887Stegge	if (ifsfirst.endoff > endoff) {
36638887Stegge		while (ifsfirst.next != NULL) {
36738887Stegge			struct ifsregion *ifsp;
36838887Stegge			INTOFF;
36938887Stegge			ifsp = ifsfirst.next->next;
37038887Stegge			ckfree(ifsfirst.next);
37138887Stegge			ifsfirst.next = ifsp;
37238887Stegge			INTON;
37338887Stegge		}
37438887Stegge		if (ifsfirst.begoff > endoff)
37538887Stegge			ifslastp = NULL;
37638887Stegge		else {
37738887Stegge			ifslastp = &ifsfirst;
37838887Stegge			ifsfirst.endoff = endoff;
37938887Stegge		}
38038887Stegge		return;
38138887Stegge	}
382155301Sschweikh
38338887Stegge	ifslastp = &ifsfirst;
38438887Stegge	while (ifslastp->next && ifslastp->next->begoff < endoff)
38538887Stegge		ifslastp=ifslastp->next;
38638887Stegge	while (ifslastp->next != NULL) {
38738887Stegge		struct ifsregion *ifsp;
38838887Stegge		INTOFF;
38938887Stegge		ifsp = ifslastp->next->next;
39038887Stegge		ckfree(ifslastp->next);
39138887Stegge		ifslastp->next = ifsp;
39238887Stegge		INTON;
39338887Stegge	}
39438887Stegge	if (ifslastp->endoff > endoff)
39538887Stegge		ifslastp->endoff = endoff;
39638887Stegge}
39738887Stegge
3981556Srgrimes/*
3991556Srgrimes * Expand arithmetic expression.  Backup to start of expression,
4001556Srgrimes * evaluate, place result in (backed up) result, adjust string position.
4011556Srgrimes */
4021556Srgrimesvoid
40390111Simpexpari(int flag)
40417987Speter{
405207206Sjilles	char *p, *q, *start;
406178631Sstefanf	arith_t result;
40738887Stegge	int begoff;
408108935Stjr	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
40938887Stegge	int quoted;
4101556Srgrimes
4111556Srgrimes	/*
41246684Skris	 * This routine is slightly over-complicated for
4131556Srgrimes	 * efficiency.  First we make sure there is
4141556Srgrimes	 * enough space for the result, which may be bigger
415212243Sjilles	 * than the expression.  Next we
4161556Srgrimes	 * scan backwards looking for the start of arithmetic.  If the
4171556Srgrimes	 * next previous character is a CTLESC character, then we
4181556Srgrimes	 * have to rescan starting from the beginning since CTLESC
4198855Srgrimes	 * characters have to be processed left to right.
4201556Srgrimes	 */
421178631Sstefanf	CHECKSTRSPACE(DIGITS(result) - 2, expdest);
4228855Srgrimes	USTPUTC('\0', expdest);
4231556Srgrimes	start = stackblock();
42483676Stegge	p = expdest - 2;
42583676Stegge	while (p >= start && *p != CTLARI)
4261556Srgrimes		--p;
42783676Stegge	if (p < start || *p != CTLARI)
4281556Srgrimes		error("missing CTLARI (shouldn't happen)");
42983676Stegge	if (p > start && *(p - 1) == CTLESC)
4301556Srgrimes		for (p = start; *p != CTLARI; p++)
4311556Srgrimes			if (*p == CTLESC)
4321556Srgrimes				p++;
43338887Stegge
43438887Stegge	if (p[1] == '"')
43538887Stegge		quoted=1;
43638887Stegge	else
43738887Stegge		quoted=0;
43838887Stegge	begoff = p - start;
43938887Stegge	removerecordregions(begoff);
4401556Srgrimes	if (quotes)
44138887Stegge		rmescapes(p+2);
442207206Sjilles	q = grabstackstr(expdest);
44338887Stegge	result = arith(p+2);
444207206Sjilles	ungrabstackstr(q, expdest);
445178631Sstefanf	fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result);
4461556Srgrimes	while (*p++)
4471556Srgrimes		;
44838887Stegge	if (quoted == 0)
44938887Stegge		recordregion(begoff, p - 1 - start, 0);
4501556Srgrimes	result = expdest - p + 1;
4511556Srgrimes	STADJUST(-result, expdest);
4521556Srgrimes}
4531556Srgrimes
4541556Srgrimes
4551556Srgrimes/*
456212243Sjilles * Perform command substitution.
4571556Srgrimes */
458213811Sobrienstatic void
45990111Simpexpbackq(union node *cmd, int quoted, int flag)
46017987Speter{
4611556Srgrimes	struct backcmd in;
4621556Srgrimes	int i;
4631556Srgrimes	char buf[128];
4641556Srgrimes	char *p;
4651556Srgrimes	char *dest = expdest;
4661556Srgrimes	struct ifsregion saveifs, *savelastp;
4671556Srgrimes	struct nodelist *saveargbackq;
4681556Srgrimes	char lastc;
4691556Srgrimes	int startloc = dest - stackblock();
4701556Srgrimes	char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
471108935Stjr	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
472115424Sfenner	int nnl;
4731556Srgrimes
4741556Srgrimes	INTOFF;
4751556Srgrimes	saveifs = ifsfirst;
4761556Srgrimes	savelastp = ifslastp;
4771556Srgrimes	saveargbackq = argbackq;
4781556Srgrimes	p = grabstackstr(dest);
4791556Srgrimes	evalbackcmd(cmd, &in);
4801556Srgrimes	ungrabstackstr(p, dest);
4811556Srgrimes	ifsfirst = saveifs;
4821556Srgrimes	ifslastp = savelastp;
4831556Srgrimes	argbackq = saveargbackq;
4841556Srgrimes
4851556Srgrimes	p = in.buf;
4861556Srgrimes	lastc = '\0';
487115424Sfenner	nnl = 0;
488115424Sfenner	/* Don't copy trailing newlines */
4891556Srgrimes	for (;;) {
4901556Srgrimes		if (--in.nleft < 0) {
4911556Srgrimes			if (in.fd < 0)
4921556Srgrimes				break;
4931556Srgrimes			while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
4941556Srgrimes			TRACE(("expbackq: read returns %d\n", i));
4951556Srgrimes			if (i <= 0)
4961556Srgrimes				break;
4971556Srgrimes			p = buf;
4981556Srgrimes			in.nleft = i - 1;
4991556Srgrimes		}
5001556Srgrimes		lastc = *p++;
5011556Srgrimes		if (lastc != '\0') {
502115424Sfenner			if (lastc == '\n') {
503115424Sfenner				nnl++;
504115424Sfenner			} else {
505216706Sjilles				CHECKSTRSPACE(nnl + 2, dest);
506115424Sfenner				while (nnl > 0) {
507115424Sfenner					nnl--;
508216706Sjilles					USTPUTC('\n', dest);
509115424Sfenner				}
510216496Sjilles				if (quotes && syntax[(int)lastc] == CCTL)
511216706Sjilles					USTPUTC(CTLESC, dest);
512216706Sjilles				USTPUTC(lastc, dest);
513115424Sfenner			}
5141556Srgrimes		}
5151556Srgrimes	}
51617987Speter
5171556Srgrimes	if (in.fd >= 0)
5181556Srgrimes		close(in.fd);
5191556Srgrimes	if (in.buf)
5201556Srgrimes		ckfree(in.buf);
5211556Srgrimes	if (in.jp)
52245916Scracauer		exitstatus = waitforjob(in.jp, (int *)NULL);
5231556Srgrimes	if (quoted == 0)
5241556Srgrimes		recordregion(startloc, dest - stackblock(), 0);
525213775Sjhb	TRACE(("expbackq: size=%td: \"%.*s\"\n",
526213775Sjhb		((dest - stackblock()) - startloc),
527213775Sjhb		(int)((dest - stackblock()) - startloc),
5281556Srgrimes		stackblock() + startloc));
5291556Srgrimes	expdest = dest;
5301556Srgrimes	INTON;
5311556Srgrimes}
5321556Srgrimes
5331556Srgrimes
5341556Srgrimes
535213811Sobrienstatic int
53690111Simpsubevalvar(char *p, char *str, int strloc, int subtype, int startloc,
537214524Sjilles  int varflags, int quotes)
53817987Speter{
53917987Speter	char *startp;
54017987Speter	char *loc = NULL;
54145514Stegge	char *q;
54217987Speter	int c = 0;
54317987Speter	struct nodelist *saveargbackq = argbackq;
54420425Ssteve	int amount;
54520425Ssteve
546206150Sjilles	argstr(p, (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
547206147Sjilles	    subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX ?
548206150Sjilles	    EXP_CASE : 0) | EXP_TILDE);
54917987Speter	STACKSTRNUL(expdest);
55017987Speter	argbackq = saveargbackq;
55117987Speter	startp = stackblock() + startloc;
55220425Ssteve	if (str == NULL)
55320425Ssteve	    str = stackblock() + strloc;
55417987Speter
55517987Speter	switch (subtype) {
55617987Speter	case VSASSIGN:
55717987Speter		setvar(str, startp, 0);
55820425Ssteve		amount = startp - expdest;
55920425Ssteve		STADJUST(amount, expdest);
56017987Speter		varflags &= ~VSNUL;
56117987Speter		return 1;
56217987Speter
56317987Speter	case VSQUESTION:
56417987Speter		if (*p != CTLENDVAR) {
565201366Sjilles			outfmt(out2, "%s\n", startp);
56617987Speter			error((char *)NULL);
56717987Speter		}
568112254Sru		error("%.*s: parameter %snot set", (int)(p - str - 1),
56917987Speter		      str, (varflags & VSNUL) ? "null or "
57017987Speter					      : nullstr);
57117987Speter		return 0;
57217987Speter
57317987Speter	case VSTRIMLEFT:
57425233Ssteve		for (loc = startp; loc < str; loc++) {
57517987Speter			c = *loc;
57617987Speter			*loc = '\0';
577214524Sjilles			if (patmatch(str, startp, quotes)) {
57817987Speter				*loc = c;
57917987Speter				goto recordleft;
58017987Speter			}
58117987Speter			*loc = c;
582214524Sjilles			if (quotes && *loc == CTLESC)
58345514Stegge				loc++;
58417987Speter		}
58517987Speter		return 0;
58617987Speter
58717987Speter	case VSTRIMLEFTMAX:
58845514Stegge		for (loc = str - 1; loc >= startp;) {
58917987Speter			c = *loc;
59017987Speter			*loc = '\0';
591214524Sjilles			if (patmatch(str, startp, quotes)) {
59217987Speter				*loc = c;
59317987Speter				goto recordleft;
59417987Speter			}
59517987Speter			*loc = c;
59645514Stegge			loc--;
597214524Sjilles			if (quotes && loc > startp && *(loc - 1) == CTLESC) {
59845514Stegge				for (q = startp; q < loc; q++)
59945514Stegge					if (*q == CTLESC)
60045514Stegge						q++;
60145514Stegge				if (q > loc)
60245514Stegge					loc--;
60345514Stegge			}
60417987Speter		}
60517987Speter		return 0;
60617987Speter
60717987Speter	case VSTRIMRIGHT:
60845514Stegge		for (loc = str - 1; loc >= startp;) {
609214524Sjilles			if (patmatch(str, loc, quotes)) {
61020425Ssteve				amount = loc - expdest;
61120425Ssteve				STADJUST(amount, expdest);
61217987Speter				return 1;
61317987Speter			}
61445514Stegge			loc--;
615214524Sjilles			if (quotes && loc > startp && *(loc - 1) == CTLESC) {
61645514Stegge				for (q = startp; q < loc; q++)
61745514Stegge					if (*q == CTLESC)
61845514Stegge						q++;
61945514Stegge				if (q > loc)
62045514Stegge					loc--;
62145514Stegge			}
62217987Speter		}
62317987Speter		return 0;
62417987Speter
62517987Speter	case VSTRIMRIGHTMAX:
62617987Speter		for (loc = startp; loc < str - 1; loc++) {
627214524Sjilles			if (patmatch(str, loc, quotes)) {
62820425Ssteve				amount = loc - expdest;
62920425Ssteve				STADJUST(amount, expdest);
63017987Speter				return 1;
63117987Speter			}
632214524Sjilles			if (quotes && *loc == CTLESC)
63345514Stegge				loc++;
63417987Speter		}
63517987Speter		return 0;
63617987Speter
63717987Speter
63817987Speter	default:
63917987Speter		abort();
64017987Speter	}
64117987Speter
64217987Speterrecordleft:
64320425Ssteve	amount = ((str - 1) - (loc - startp)) - expdest;
64420425Ssteve	STADJUST(amount, expdest);
64517987Speter	while (loc != str - 1)
64617987Speter		*startp++ = *loc++;
64717987Speter	return 1;
64817987Speter}
64917987Speter
65017987Speter
6511556Srgrimes/*
6521556Srgrimes * Expand a variable, and return a pointer to the next character in the
6531556Srgrimes * input string.
6541556Srgrimes */
6551556Srgrimes
656213811Sobrienstatic char *
65790111Simpevalvar(char *p, int flag)
65817987Speter{
6591556Srgrimes	int subtype;
6601556Srgrimes	int varflags;
6611556Srgrimes	char *var;
6621556Srgrimes	char *val;
66345644Stegge	int patloc;
6641556Srgrimes	int c;
6651556Srgrimes	int set;
6661556Srgrimes	int special;
6671556Srgrimes	int startloc;
66817987Speter	int varlen;
669221602Sjilles	int varlenb;
67017987Speter	int easy;
671108935Stjr	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
6721556Srgrimes
673164081Sstefanf	varflags = (unsigned char)*p++;
6741556Srgrimes	subtype = varflags & VSTYPE;
6751556Srgrimes	var = p;
6761556Srgrimes	special = 0;
6771556Srgrimes	if (! is_name(*p))
6781556Srgrimes		special = 1;
6791556Srgrimes	p = strchr(p, '=') + 1;
6801556Srgrimesagain: /* jump here after setting a variable with ${var=text} */
681179022Sstefanf	if (varflags & VSLINENO) {
682179022Sstefanf		set = 1;
683179022Sstefanf		special = 0;
684179022Sstefanf		val = var;
685179022Sstefanf		p[-1] = '\0';	/* temporarily overwrite '=' to have \0
686179022Sstefanf				   terminated string */
687179022Sstefanf	} else if (special) {
68825233Ssteve		set = varisset(var, varflags & VSNUL);
6891556Srgrimes		val = NULL;
6901556Srgrimes	} else {
69160592Scracauer		val = bltinlookup(var, 1);
69217987Speter		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
6931556Srgrimes			val = NULL;
6941556Srgrimes			set = 0;
6951556Srgrimes		} else
6961556Srgrimes			set = 1;
6971556Srgrimes	}
69817987Speter	varlen = 0;
6991556Srgrimes	startloc = expdest - stackblock();
700198454Sjilles	if (!set && uflag && *var != '@' && *var != '*') {
70196939Stjr		switch (subtype) {
70296939Stjr		case VSNORMAL:
70396939Stjr		case VSTRIMLEFT:
70496939Stjr		case VSTRIMLEFTMAX:
70596939Stjr		case VSTRIMRIGHT:
70696939Stjr		case VSTRIMRIGHTMAX:
70796939Stjr		case VSLENGTH:
708112254Sru			error("%.*s: parameter not set", (int)(p - var - 1),
709112254Sru			    var);
71096939Stjr		}
71196939Stjr	}
7121556Srgrimes	if (set && subtype != VSPLUS) {
7131556Srgrimes		/* insert the value of the variable */
7141556Srgrimes		if (special) {
715164081Sstefanf			varvalue(var, varflags & VSQUOTE, subtype, flag);
71617987Speter			if (subtype == VSLENGTH) {
717221602Sjilles				varlenb = expdest - stackblock() - startloc;
718221602Sjilles				varlen = varlenb;
719221602Sjilles				if (localeisutf8) {
720221602Sjilles					val = stackblock() + startloc;
721221602Sjilles					for (;val != expdest; val++)
722221602Sjilles						if ((*val & 0xC0) == 0x80)
723221602Sjilles							varlen--;
724221602Sjilles				}
725221602Sjilles				STADJUST(-varlenb, expdest);
72617987Speter			}
7271556Srgrimes		} else {
72820425Ssteve			char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
72917987Speter								  : BASESYNTAX;
7301556Srgrimes
73117987Speter			if (subtype == VSLENGTH) {
73217987Speter				for (;*val; val++)
733221602Sjilles					if (!localeisutf8 ||
734221602Sjilles					    (*val & 0xC0) != 0x80)
735221602Sjilles						varlen++;
7361556Srgrimes			}
73717987Speter			else {
738216384Sjilles				if (quotes)
739216384Sjilles					STPUTS_QUOTES(val, syntax, expdest);
740216384Sjilles				else
741216384Sjilles					STPUTS(val, expdest);
74217987Speter
74317987Speter			}
7441556Srgrimes		}
7451556Srgrimes	}
74620425Ssteve
7471556Srgrimes	if (subtype == VSPLUS)
7481556Srgrimes		set = ! set;
74917987Speter
75020425Ssteve	easy = ((varflags & VSQUOTE) == 0 ||
75117987Speter		(*var == '@' && shellparam.nparam != 1));
75217987Speter
75317987Speter
75417987Speter	switch (subtype) {
75517987Speter	case VSLENGTH:
75617987Speter		expdest = cvtnum(varlen, expdest);
75717987Speter		goto record;
75817987Speter
75917987Speter	case VSNORMAL:
76017987Speter		if (!easy)
76117987Speter			break;
76217987Speterrecord:
76320425Ssteve		recordregion(startloc, expdest - stackblock(),
76417987Speter			     varflags & VSQUOTE);
76517987Speter		break;
76617987Speter
76717987Speter	case VSPLUS:
76817987Speter	case VSMINUS:
76917987Speter		if (!set) {
770214512Sjilles			argstr(p, flag | (flag & EXP_FULL ? EXP_SPLIT_LIT : 0) |
771214512Sjilles			    (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0));
77217987Speter			break;
77317987Speter		}
77417987Speter		if (easy)
77517987Speter			goto record;
77617987Speter		break;
77717987Speter
77817987Speter	case VSTRIMLEFT:
77917987Speter	case VSTRIMLEFTMAX:
78017987Speter	case VSTRIMRIGHT:
78117987Speter	case VSTRIMRIGHTMAX:
78217987Speter		if (!set)
78317987Speter			break;
78417987Speter		/*
78517987Speter		 * Terminate the string and start recording the pattern
78617987Speter		 * right after it
78717987Speter		 */
78817987Speter		STPUTC('\0', expdest);
78945644Stegge		patloc = expdest - stackblock();
79045644Stegge		if (subevalvar(p, NULL, patloc, subtype,
791214524Sjilles		    startloc, varflags, quotes) == 0) {
79245644Stegge			int amount = (expdest - stackblock() - patloc) + 1;
79325233Ssteve			STADJUST(-amount, expdest);
79425233Ssteve		}
79538887Stegge		/* Remove any recorded regions beyond start of variable */
79638887Stegge		removerecordregions(startloc);
79738887Stegge		goto record;
79817987Speter
79917987Speter	case VSASSIGN:
80017987Speter	case VSQUESTION:
80117987Speter		if (!set) {
802214524Sjilles			if (subevalvar(p, var, 0, subtype, startloc, varflags,
803214524Sjilles			    quotes)) {
80420425Ssteve				varflags &= ~VSNUL;
805155301Sschweikh				/*
806155301Sschweikh				 * Remove any recorded regions beyond
807155301Sschweikh				 * start of variable
80838887Stegge				 */
80938887Stegge				removerecordregions(startloc);
8101556Srgrimes				goto again;
81120425Ssteve			}
81217987Speter			break;
8131556Srgrimes		}
81417987Speter		if (easy)
81517987Speter			goto record;
81617987Speter		break;
81717987Speter
818164003Sstefanf	case VSERROR:
819164003Sstefanf		c = p - var - 1;
820164003Sstefanf		error("${%.*s%s}: Bad substitution", c, var,
821164003Sstefanf		    (c > 0 && *p != CTLENDVAR) ? "..." : "");
822164003Sstefanf
82317987Speter	default:
82417987Speter		abort();
8251556Srgrimes	}
826179022Sstefanf	p[-1] = '=';	/* recover overwritten '=' */
82717987Speter
8281556Srgrimes	if (subtype != VSNORMAL) {	/* skip to end of alternative */
8291556Srgrimes		int nesting = 1;
8301556Srgrimes		for (;;) {
8311556Srgrimes			if ((c = *p++) == CTLESC)
8321556Srgrimes				p++;
8331556Srgrimes			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
8341556Srgrimes				if (set)
8351556Srgrimes					argbackq = argbackq->next;
8361556Srgrimes			} else if (c == CTLVAR) {
8371556Srgrimes				if ((*p++ & VSTYPE) != VSNORMAL)
8381556Srgrimes					nesting++;
8391556Srgrimes			} else if (c == CTLENDVAR) {
8401556Srgrimes				if (--nesting == 0)
8411556Srgrimes					break;
8421556Srgrimes			}
8431556Srgrimes		}
8441556Srgrimes	}
8451556Srgrimes	return p;
8461556Srgrimes}
8471556Srgrimes
8481556Srgrimes
8491556Srgrimes
8501556Srgrimes/*
8511556Srgrimes * Test whether a specialized variable is set.
8521556Srgrimes */
8531556Srgrimes
854213811Sobrienstatic int
85590111Simpvarisset(char *name, int nulok)
85625233Ssteve{
8571556Srgrimes
85825233Ssteve	if (*name == '!')
859209600Sjilles		return backgndpidset();
86025233Ssteve	else if (*name == '@' || *name == '*') {
8611556Srgrimes		if (*shellparam.p == NULL)
8621556Srgrimes			return 0;
86325233Ssteve
86425233Ssteve		if (nulok) {
86525233Ssteve			char **av;
86625233Ssteve
86725233Ssteve			for (av = shellparam.p; *av; av++)
86825233Ssteve				if (**av != '\0')
86925233Ssteve					return 1;
87025233Ssteve			return 0;
87125233Ssteve		}
87218202Speter	} else if (is_digit(*name)) {
87325233Ssteve		char *ap;
87420425Ssteve		int num = atoi(name);
87525233Ssteve
87625233Ssteve		if (num > shellparam.nparam)
87725233Ssteve			return 0;
87825233Ssteve
87925233Ssteve		if (num == 0)
88025233Ssteve			ap = arg0;
88125233Ssteve		else
88225233Ssteve			ap = shellparam.p[num - 1];
88325233Ssteve
88425233Ssteve		if (nulok && (ap == NULL || *ap == '\0'))
88525233Ssteve			return 0;
8861556Srgrimes	}
8871556Srgrimes	return 1;
8881556Srgrimes}
8891556Srgrimes
890216384Sjillesstatic void
891216384Sjillesstrtodest(const char *p, int flag, int subtype, int quoted)
892216384Sjilles{
893216384Sjilles	if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH)
894216384Sjilles		STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
895216384Sjilles	else
896216384Sjilles		STPUTS(p, expdest);
897216384Sjilles}
8981556Srgrimes
8991556Srgrimes/*
9001556Srgrimes * Add the value of a specialized variable to the stack string.
9011556Srgrimes */
9021556Srgrimes
903213811Sobrienstatic void
904164081Sstefanfvarvalue(char *name, int quoted, int subtype, int flag)
90517987Speter{
9061556Srgrimes	int num;
9071556Srgrimes	char *p;
9081556Srgrimes	int i;
9091556Srgrimes	char sep;
9101556Srgrimes	char **ap;
9111556Srgrimes
91218202Speter	switch (*name) {
9131556Srgrimes	case '$':
9141556Srgrimes		num = rootpid;
9151556Srgrimes		goto numvar;
9161556Srgrimes	case '?':
91717987Speter		num = oexitstatus;
9181556Srgrimes		goto numvar;
9191556Srgrimes	case '#':
9201556Srgrimes		num = shellparam.nparam;
9211556Srgrimes		goto numvar;
9221556Srgrimes	case '!':
923209600Sjilles		num = backgndpidval();
9241556Srgrimesnumvar:
92517987Speter		expdest = cvtnum(num, expdest);
9261556Srgrimes		break;
9271556Srgrimes	case '-':
9281556Srgrimes		for (i = 0 ; i < NOPTS ; i++) {
9291556Srgrimes			if (optlist[i].val)
9301556Srgrimes				STPUTC(optlist[i].letter, expdest);
9311556Srgrimes		}
9321556Srgrimes		break;
9331556Srgrimes	case '@':
934164081Sstefanf		if (flag & EXP_FULL && quoted) {
93538887Stegge			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
936216384Sjilles				strtodest(p, flag, subtype, quoted);
93738887Stegge				if (*ap)
93838887Stegge					STPUTC('\0', expdest);
93938887Stegge			}
94038887Stegge			break;
9411556Srgrimes		}
942102410Scharnier		/* FALLTHROUGH */
9431556Srgrimes	case '*':
944149825Srse		if (ifsset())
94538887Stegge			sep = ifsval()[0];
94638887Stegge		else
94738887Stegge			sep = ' ';
9481556Srgrimes		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
949216384Sjilles			strtodest(p, flag, subtype, quoted);
95038887Stegge			if (*ap && sep)
9511556Srgrimes				STPUTC(sep, expdest);
9521556Srgrimes		}
9531556Srgrimes		break;
9541556Srgrimes	case '0':
9551556Srgrimes		p = arg0;
956216384Sjilles		strtodest(p, flag, subtype, quoted);
9571556Srgrimes		break;
9581556Srgrimes	default:
95918202Speter		if (is_digit(*name)) {
96018202Speter			num = atoi(name);
96118202Speter			if (num > 0 && num <= shellparam.nparam) {
96218202Speter				p = shellparam.p[num - 1];
963216384Sjilles				strtodest(p, flag, subtype, quoted);
96418202Speter			}
9651556Srgrimes		}
9661556Srgrimes		break;
9671556Srgrimes	}
9681556Srgrimes}
9691556Srgrimes
9701556Srgrimes
9711556Srgrimes
9721556Srgrimes/*
973218909Sbrucec * Record the fact that we have to scan this region of the
9741556Srgrimes * string for IFS characters.
9751556Srgrimes */
9761556Srgrimes
977213811Sobrienstatic void
978194975Sjillesrecordregion(int start, int end, int inquotes)
97917987Speter{
98025233Ssteve	struct ifsregion *ifsp;
9811556Srgrimes
9821556Srgrimes	if (ifslastp == NULL) {
9831556Srgrimes		ifsp = &ifsfirst;
9841556Srgrimes	} else {
985194975Sjilles		if (ifslastp->endoff == start
986194975Sjilles		    && ifslastp->inquotes == inquotes) {
987194975Sjilles			/* extend previous area */
988194975Sjilles			ifslastp->endoff = end;
989194975Sjilles			return;
990194975Sjilles		}
9911556Srgrimes		ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
9921556Srgrimes		ifslastp->next = ifsp;
9931556Srgrimes	}
9941556Srgrimes	ifslastp = ifsp;
9951556Srgrimes	ifslastp->next = NULL;
9961556Srgrimes	ifslastp->begoff = start;
9971556Srgrimes	ifslastp->endoff = end;
998194975Sjilles	ifslastp->inquotes = inquotes;
9991556Srgrimes}
10001556Srgrimes
10011556Srgrimes
10021556Srgrimes
10031556Srgrimes/*
10041556Srgrimes * Break the argument string into pieces based upon IFS and add the
10051556Srgrimes * strings to the argument list.  The regions of the string to be
10061556Srgrimes * searched for IFS characters have been stored by recordregion.
1007212243Sjilles * CTLESC characters are preserved but have little effect in this pass
1008212243Sjilles * other than escaping CTL* characters.  In particular, they do not escape
1009212243Sjilles * IFS characters: that should be done with the ifsregion mechanism.
1010212243Sjilles * CTLQUOTEMARK characters are used to preserve empty quoted strings.
1011212243Sjilles * This pass treats them as a regular character, making the string non-empty.
1012212243Sjilles * Later, they are removed along with the other CTL* characters.
10131556Srgrimes */
1014213811Sobrienstatic void
101590111Simpifsbreakup(char *string, struct arglist *arglist)
101690111Simp{
10171556Srgrimes	struct ifsregion *ifsp;
10181556Srgrimes	struct strlist *sp;
10191556Srgrimes	char *start;
102025233Ssteve	char *p;
10211556Srgrimes	char *q;
1022201053Sjilles	const char *ifs;
1023194975Sjilles	const char *ifsspc;
1024194975Sjilles	int had_param_ch = 0;
10251556Srgrimes
1026194975Sjilles	start = string;
102717987Speter
1028194975Sjilles	if (ifslastp == NULL) {
1029194975Sjilles		/* Return entire argument, IFS doesn't apply to any of it */
1030194975Sjilles		sp = (struct strlist *)stalloc(sizeof *sp);
1031194975Sjilles		sp->text = start;
1032194975Sjilles		*arglist->lastp = sp;
1033194975Sjilles		arglist->lastp = &sp->next;
1034194975Sjilles		return;
1035194975Sjilles	}
1036194975Sjilles
1037194975Sjilles	ifs = ifsset() ? ifsval() : " \t\n";
1038194975Sjilles
1039194975Sjilles	for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
1040194975Sjilles		p = string + ifsp->begoff;
1041194975Sjilles		while (p < string + ifsp->endoff) {
1042194975Sjilles			q = p;
1043194975Sjilles			if (*p == CTLESC)
1044194975Sjilles				p++;
1045194975Sjilles			if (ifsp->inquotes) {
1046194975Sjilles				/* Only NULs (should be from "$@") end args */
1047194977Sjilles				had_param_ch = 1;
1048194975Sjilles				if (*p != 0) {
10491556Srgrimes					p++;
1050194975Sjilles					continue;
1051194975Sjilles				}
1052194975Sjilles				ifsspc = NULL;
1053194975Sjilles			} else {
1054194975Sjilles				if (!strchr(ifs, *p)) {
1055194977Sjilles					had_param_ch = 1;
105638887Stegge					p++;
1057194975Sjilles					continue;
1058194975Sjilles				}
1059194975Sjilles				ifsspc = strchr(" \t\n", *p);
1060194975Sjilles
1061194975Sjilles				/* Ignore IFS whitespace at start */
1062194975Sjilles				if (q == start && ifsspc != NULL) {
1063194975Sjilles					p++;
10641556Srgrimes					start = p;
1065194975Sjilles					continue;
1066194975Sjilles				}
1067194977Sjilles				had_param_ch = 0;
10681556Srgrimes			}
1069194975Sjilles
1070194975Sjilles			/* Save this argument... */
1071194975Sjilles			*q = '\0';
10721556Srgrimes			sp = (struct strlist *)stalloc(sizeof *sp);
10731556Srgrimes			sp->text = start;
10741556Srgrimes			*arglist->lastp = sp;
10751556Srgrimes			arglist->lastp = &sp->next;
1076194975Sjilles			p++;
1077194975Sjilles
1078194975Sjilles			if (ifsspc != NULL) {
1079194975Sjilles				/* Ignore further trailing IFS whitespace */
1080194975Sjilles				for (; p < string + ifsp->endoff; p++) {
1081194975Sjilles					q = p;
1082194975Sjilles					if (*p == CTLESC)
1083194975Sjilles						p++;
1084194975Sjilles					if (strchr(ifs, *p) == NULL) {
1085194975Sjilles						p = q;
1086194975Sjilles						break;
1087194975Sjilles					}
1088194975Sjilles					if (strchr(" \t\n", *p) == NULL) {
1089194975Sjilles						p++;
1090194975Sjilles						break;
1091194975Sjilles					}
1092194975Sjilles				}
1093194975Sjilles			}
1094194975Sjilles			start = p;
10951556Srgrimes		}
1096194975Sjilles	}
1097194975Sjilles
1098194975Sjilles	/*
1099194975Sjilles	 * Save anything left as an argument.
1100194975Sjilles	 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1101194975Sjilles	 * generating 2 arguments, the second of which is empty.
1102194975Sjilles	 * Some recent clarification of the Posix spec say that it
1103194975Sjilles	 * should only generate one....
1104194975Sjilles	 */
1105194975Sjilles	if (had_param_ch || *start != 0) {
11061556Srgrimes		sp = (struct strlist *)stalloc(sizeof *sp);
11071556Srgrimes		sp->text = start;
11081556Srgrimes		*arglist->lastp = sp;
11091556Srgrimes		arglist->lastp = &sp->next;
11101556Srgrimes	}
11111556Srgrimes}
11121556Srgrimes
11131556Srgrimes
1114213760Sobrienstatic char expdir[PATH_MAX];
1115212243Sjilles#define expdir_end (expdir + sizeof(expdir))
11161556Srgrimes
11171556Srgrimes/*
1118212243Sjilles * Perform pathname generation and remove control characters.
1119212243Sjilles * At this point, the only control characters should be CTLESC and CTLQUOTEMARK.
1120212243Sjilles * The results are stored in the list exparg.
11211556Srgrimes */
1122213811Sobrienstatic void
112390111Simpexpandmeta(struct strlist *str, int flag __unused)
112417987Speter{
11251556Srgrimes	char *p;
11261556Srgrimes	struct strlist **savelastp;
11271556Srgrimes	struct strlist *sp;
11281556Srgrimes	char c;
11291556Srgrimes	/* TODO - EXP_REDIR */
11301556Srgrimes
11311556Srgrimes	while (str) {
11321556Srgrimes		if (fflag)
11331556Srgrimes			goto nometa;
11341556Srgrimes		p = str->text;
11351556Srgrimes		for (;;) {			/* fast check for meta chars */
11361556Srgrimes			if ((c = *p++) == '\0')
11371556Srgrimes				goto nometa;
1138211646Sjilles			if (c == '*' || c == '?' || c == '[')
11391556Srgrimes				break;
11401556Srgrimes		}
11411556Srgrimes		savelastp = exparg.lastp;
11421556Srgrimes		INTOFF;
11431556Srgrimes		expmeta(expdir, str->text);
11441556Srgrimes		INTON;
11451556Srgrimes		if (exparg.lastp == savelastp) {
11468855Srgrimes			/*
11478855Srgrimes			 * no matches
11481556Srgrimes			 */
11491556Srgrimesnometa:
11501556Srgrimes			*exparg.lastp = str;
11511556Srgrimes			rmescapes(str->text);
11521556Srgrimes			exparg.lastp = &str->next;
11531556Srgrimes		} else {
11541556Srgrimes			*exparg.lastp = NULL;
11551556Srgrimes			*savelastp = sp = expsort(*savelastp);
11561556Srgrimes			while (sp->next != NULL)
11571556Srgrimes				sp = sp->next;
11581556Srgrimes			exparg.lastp = &sp->next;
11591556Srgrimes		}
11601556Srgrimes		str = str->next;
11611556Srgrimes	}
11621556Srgrimes}
11631556Srgrimes
11641556Srgrimes
11651556Srgrimes/*
11661556Srgrimes * Do metacharacter (i.e. *, ?, [...]) expansion.
11671556Srgrimes */
11681556Srgrimes
1169213811Sobrienstatic void
117090111Simpexpmeta(char *enddir, char *name)
117190111Simp{
117225233Ssteve	char *p;
11731556Srgrimes	char *q;
11741556Srgrimes	char *start;
11751556Srgrimes	char *endname;
11761556Srgrimes	int metaflag;
11771556Srgrimes	struct stat statb;
11781556Srgrimes	DIR *dirp;
11791556Srgrimes	struct dirent *dp;
11801556Srgrimes	int atend;
11811556Srgrimes	int matchdot;
1182207944Sjilles	int esc;
11831556Srgrimes
11841556Srgrimes	metaflag = 0;
11851556Srgrimes	start = name;
1186207944Sjilles	for (p = name; esc = 0, *p; p += esc + 1) {
11871556Srgrimes		if (*p == '*' || *p == '?')
11881556Srgrimes			metaflag = 1;
11891556Srgrimes		else if (*p == '[') {
11901556Srgrimes			q = p + 1;
119126488Sache			if (*q == '!' || *q == '^')
11921556Srgrimes				q++;
11931556Srgrimes			for (;;) {
119438887Stegge				while (*q == CTLQUOTEMARK)
119538887Stegge					q++;
11961556Srgrimes				if (*q == CTLESC)
11971556Srgrimes					q++;
11981556Srgrimes				if (*q == '/' || *q == '\0')
11991556Srgrimes					break;
12001556Srgrimes				if (*++q == ']') {
12011556Srgrimes					metaflag = 1;
12021556Srgrimes					break;
12031556Srgrimes				}
12041556Srgrimes			}
12051556Srgrimes		} else if (*p == '\0')
12061556Srgrimes			break;
120738887Stegge		else if (*p == CTLQUOTEMARK)
120838887Stegge			continue;
1209207944Sjilles		else {
1210207944Sjilles			if (*p == CTLESC)
1211207944Sjilles				esc++;
1212207944Sjilles			if (p[esc] == '/') {
1213207944Sjilles				if (metaflag)
1214207944Sjilles					break;
1215207944Sjilles				start = p + esc + 1;
1216207944Sjilles			}
12171556Srgrimes		}
12181556Srgrimes	}
12191556Srgrimes	if (metaflag == 0) {	/* we've reached the end of the file name */
12201556Srgrimes		if (enddir != expdir)
12211556Srgrimes			metaflag++;
12221556Srgrimes		for (p = name ; ; p++) {
122338887Stegge			if (*p == CTLQUOTEMARK)
122438887Stegge				continue;
12251556Srgrimes			if (*p == CTLESC)
12261556Srgrimes				p++;
12271556Srgrimes			*enddir++ = *p;
12281556Srgrimes			if (*p == '\0')
12291556Srgrimes				break;
1230211155Sjilles			if (enddir == expdir_end)
1231211155Sjilles				return;
12321556Srgrimes		}
1233147812Sdelphij		if (metaflag == 0 || lstat(expdir, &statb) >= 0)
12341556Srgrimes			addfname(expdir);
12351556Srgrimes		return;
12361556Srgrimes	}
12371556Srgrimes	endname = p;
12381556Srgrimes	if (start != name) {
12391556Srgrimes		p = name;
12401556Srgrimes		while (p < start) {
124138887Stegge			while (*p == CTLQUOTEMARK)
124238887Stegge				p++;
12431556Srgrimes			if (*p == CTLESC)
12441556Srgrimes				p++;
12451556Srgrimes			*enddir++ = *p++;
1246211155Sjilles			if (enddir == expdir_end)
1247211155Sjilles				return;
12481556Srgrimes		}
12491556Srgrimes	}
12501556Srgrimes	if (enddir == expdir) {
12511556Srgrimes		p = ".";
12521556Srgrimes	} else if (enddir == expdir + 1 && *expdir == '/') {
12531556Srgrimes		p = "/";
12541556Srgrimes	} else {
12551556Srgrimes		p = expdir;
12561556Srgrimes		enddir[-1] = '\0';
12571556Srgrimes	}
12581556Srgrimes	if ((dirp = opendir(p)) == NULL)
12591556Srgrimes		return;
12601556Srgrimes	if (enddir != expdir)
12611556Srgrimes		enddir[-1] = '/';
12621556Srgrimes	if (*endname == 0) {
12631556Srgrimes		atend = 1;
12641556Srgrimes	} else {
12651556Srgrimes		atend = 0;
1266207944Sjilles		*endname = '\0';
1267207944Sjilles		endname += esc + 1;
12681556Srgrimes	}
12691556Srgrimes	matchdot = 0;
127038887Stegge	p = start;
127138887Stegge	while (*p == CTLQUOTEMARK)
127238887Stegge		p++;
127338887Stegge	if (*p == CTLESC)
127438887Stegge		p++;
127538887Stegge	if (*p == '.')
12761556Srgrimes		matchdot++;
12771556Srgrimes	while (! int_pending() && (dp = readdir(dirp)) != NULL) {
12781556Srgrimes		if (dp->d_name[0] == '.' && ! matchdot)
12791556Srgrimes			continue;
128045514Stegge		if (patmatch(start, dp->d_name, 0)) {
1281211155Sjilles			if (enddir + dp->d_namlen + 1 > expdir_end)
1282211155Sjilles				continue;
1283211155Sjilles			memcpy(enddir, dp->d_name, dp->d_namlen + 1);
1284211155Sjilles			if (atend)
12851556Srgrimes				addfname(expdir);
1286211155Sjilles			else {
1287211155Sjilles				if (enddir + dp->d_namlen + 2 > expdir_end)
128817987Speter					continue;
1289211155Sjilles				enddir[dp->d_namlen] = '/';
1290211155Sjilles				enddir[dp->d_namlen + 1] = '\0';
1291211155Sjilles				expmeta(enddir + dp->d_namlen + 1, endname);
12921556Srgrimes			}
12931556Srgrimes		}
12941556Srgrimes	}
12951556Srgrimes	closedir(dirp);
12961556Srgrimes	if (! atend)
1297207944Sjilles		endname[-esc - 1] = esc ? CTLESC : '/';
12981556Srgrimes}
12991556Srgrimes
13001556Srgrimes
13011556Srgrimes/*
13021556Srgrimes * Add a file name to the list.
13031556Srgrimes */
13041556Srgrimes
1305213811Sobrienstatic void
130690111Simpaddfname(char *name)
130790111Simp{
13081556Srgrimes	char *p;
13091556Srgrimes	struct strlist *sp;
13101556Srgrimes
13111556Srgrimes	p = stalloc(strlen(name) + 1);
13121556Srgrimes	scopy(name, p);
13131556Srgrimes	sp = (struct strlist *)stalloc(sizeof *sp);
13141556Srgrimes	sp->text = p;
13151556Srgrimes	*exparg.lastp = sp;
13161556Srgrimes	exparg.lastp = &sp->next;
13171556Srgrimes}
13181556Srgrimes
13191556Srgrimes
13201556Srgrimes/*
13211556Srgrimes * Sort the results of file name expansion.  It calculates the number of
13221556Srgrimes * strings to sort and then calls msort (short for merge sort) to do the
13231556Srgrimes * work.
13241556Srgrimes */
13251556Srgrimes
1326213811Sobrienstatic struct strlist *
132790111Simpexpsort(struct strlist *str)
132890111Simp{
13291556Srgrimes	int len;
13301556Srgrimes	struct strlist *sp;
13311556Srgrimes
13321556Srgrimes	len = 0;
13331556Srgrimes	for (sp = str ; sp ; sp = sp->next)
13341556Srgrimes		len++;
13351556Srgrimes	return msort(str, len);
13361556Srgrimes}
13371556Srgrimes
13381556Srgrimes
1339213811Sobrienstatic struct strlist *
134090111Simpmsort(struct strlist *list, int len)
134117987Speter{
134217987Speter	struct strlist *p, *q = NULL;
13431556Srgrimes	struct strlist **lpp;
13441556Srgrimes	int half;
13451556Srgrimes	int n;
13461556Srgrimes
13471556Srgrimes	if (len <= 1)
13481556Srgrimes		return list;
13498855Srgrimes	half = len >> 1;
13501556Srgrimes	p = list;
13511556Srgrimes	for (n = half ; --n >= 0 ; ) {
13521556Srgrimes		q = p;
13531556Srgrimes		p = p->next;
13541556Srgrimes	}
13551556Srgrimes	q->next = NULL;			/* terminate first half of list */
13561556Srgrimes	q = msort(list, half);		/* sort first half of list */
13571556Srgrimes	p = msort(p, len - half);		/* sort second half */
13581556Srgrimes	lpp = &list;
13591556Srgrimes	for (;;) {
13601556Srgrimes		if (strcmp(p->text, q->text) < 0) {
13611556Srgrimes			*lpp = p;
13621556Srgrimes			lpp = &p->next;
13631556Srgrimes			if ((p = *lpp) == NULL) {
13641556Srgrimes				*lpp = q;
13651556Srgrimes				break;
13661556Srgrimes			}
13671556Srgrimes		} else {
13681556Srgrimes			*lpp = q;
13691556Srgrimes			lpp = &q->next;
13701556Srgrimes			if ((q = *lpp) == NULL) {
13711556Srgrimes				*lpp = p;
13721556Srgrimes				break;
13731556Srgrimes			}
13741556Srgrimes		}
13751556Srgrimes	}
13761556Srgrimes	return list;
13771556Srgrimes}
13781556Srgrimes
13791556Srgrimes
13801556Srgrimes
1381221646Sjillesstatic wchar_t
1382221646Sjillesget_wc(const char **p)
1383221646Sjilles{
1384221646Sjilles	wchar_t c;
1385221646Sjilles	int chrlen;
1386221646Sjilles
1387221646Sjilles	chrlen = mbtowc(&c, *p, 4);
1388221646Sjilles	if (chrlen == 0)
1389221646Sjilles		return 0;
1390221646Sjilles	else if (chrlen == -1)
1391221646Sjilles		c = 0;
1392221646Sjilles	else
1393221646Sjilles		*p += chrlen;
1394221646Sjilles	return c;
1395221646Sjilles}
1396221646Sjilles
1397221646Sjilles
13981556Srgrimes/*
13991556Srgrimes * Returns true if the pattern matches the string.
14001556Srgrimes */
14011556Srgrimes
14021556Srgrimesint
1403200956Sjillespatmatch(const char *pattern, const char *string, int squoted)
140490111Simp{
1405200956Sjilles	const char *p, *q;
140625233Ssteve	char c;
1407221646Sjilles	wchar_t wc, wc2;
14081556Srgrimes
14091556Srgrimes	p = pattern;
14101556Srgrimes	q = string;
14111556Srgrimes	for (;;) {
14121556Srgrimes		switch (c = *p++) {
14131556Srgrimes		case '\0':
14141556Srgrimes			goto breakloop;
14151556Srgrimes		case CTLESC:
141645514Stegge			if (squoted && *q == CTLESC)
141745514Stegge				q++;
14181556Srgrimes			if (*q++ != *p++)
14191556Srgrimes				return 0;
14201556Srgrimes			break;
142138887Stegge		case CTLQUOTEMARK:
142238887Stegge			continue;
14231556Srgrimes		case '?':
142445514Stegge			if (squoted && *q == CTLESC)
142545514Stegge				q++;
1426221646Sjilles			if (localeisutf8)
1427221646Sjilles				wc = get_wc(&q);
1428221646Sjilles			else
1429221646Sjilles				wc = *q++;
1430221646Sjilles			if (wc == '\0')
14311556Srgrimes				return 0;
14321556Srgrimes			break;
14331556Srgrimes		case '*':
14341556Srgrimes			c = *p;
143538887Stegge			while (c == CTLQUOTEMARK || c == '*')
143638887Stegge				c = *++p;
143738887Stegge			if (c != CTLESC &&  c != CTLQUOTEMARK &&
143838887Stegge			    c != '?' && c != '*' && c != '[') {
14391556Srgrimes				while (*q != c) {
144045514Stegge					if (squoted && *q == CTLESC &&
144145514Stegge					    q[1] == c)
144245514Stegge						break;
14431556Srgrimes					if (*q == '\0')
14441556Srgrimes						return 0;
144545514Stegge					if (squoted && *q == CTLESC)
144645514Stegge						q++;
14471556Srgrimes					q++;
14481556Srgrimes				}
14491556Srgrimes			}
14501556Srgrimes			do {
1451211646Sjilles				if (patmatch(p, q, squoted))
14521556Srgrimes					return 1;
145345514Stegge				if (squoted && *q == CTLESC)
145445514Stegge					q++;
14551556Srgrimes			} while (*q++ != '\0');
14561556Srgrimes			return 0;
14571556Srgrimes		case '[': {
1458200956Sjilles			const char *endp;
14591556Srgrimes			int invert, found;
1460221646Sjilles			wchar_t chr;
14611556Srgrimes
14621556Srgrimes			endp = p;
146326488Sache			if (*endp == '!' || *endp == '^')
14641556Srgrimes				endp++;
14651556Srgrimes			for (;;) {
146638887Stegge				while (*endp == CTLQUOTEMARK)
146738887Stegge					endp++;
14681556Srgrimes				if (*endp == '\0')
14691556Srgrimes					goto dft;		/* no matching ] */
14701556Srgrimes				if (*endp == CTLESC)
14711556Srgrimes					endp++;
14721556Srgrimes				if (*++endp == ']')
14731556Srgrimes					break;
14741556Srgrimes			}
14751556Srgrimes			invert = 0;
147626488Sache			if (*p == '!' || *p == '^') {
14771556Srgrimes				invert++;
14781556Srgrimes				p++;
14791556Srgrimes			}
14801556Srgrimes			found = 0;
1481221646Sjilles			if (squoted && *q == CTLESC)
1482221646Sjilles				q++;
1483221646Sjilles			if (localeisutf8)
1484221646Sjilles				chr = get_wc(&q);
1485221646Sjilles			else
148645514Stegge				chr = *q++;
148717987Speter			if (chr == '\0')
148817987Speter				return 0;
14891556Srgrimes			c = *p++;
14901556Srgrimes			do {
149138887Stegge				if (c == CTLQUOTEMARK)
149238887Stegge					continue;
14931556Srgrimes				if (c == CTLESC)
14941556Srgrimes					c = *p++;
1495221646Sjilles				if (localeisutf8 && c & 0x80) {
1496221646Sjilles					p--;
1497221646Sjilles					wc = get_wc(&p);
1498221646Sjilles					if (wc == 0) /* bad utf-8 */
1499221646Sjilles						return 0;
1500221646Sjilles				} else
1501221646Sjilles					wc = c;
15021556Srgrimes				if (*p == '-' && p[1] != ']') {
15031556Srgrimes					p++;
150438887Stegge					while (*p == CTLQUOTEMARK)
150538887Stegge						p++;
15061556Srgrimes					if (*p == CTLESC)
15071556Srgrimes						p++;
1508221646Sjilles					if (localeisutf8) {
1509221646Sjilles						wc2 = get_wc(&p);
1510221646Sjilles						if (wc2 == 0) /* bad utf-8 */
1511221646Sjilles							return 0;
1512221646Sjilles					} else
1513221646Sjilles						wc2 = *p++;
1514221646Sjilles					if (   collate_range_cmp(chr, wc) >= 0
1515221646Sjilles					    && collate_range_cmp(chr, wc2) <= 0
151617525Sache					   )
15171556Srgrimes						found = 1;
15181556Srgrimes				} else {
1519221646Sjilles					if (chr == wc)
15201556Srgrimes						found = 1;
15211556Srgrimes				}
15221556Srgrimes			} while ((c = *p++) != ']');
15231556Srgrimes			if (found == invert)
15241556Srgrimes				return 0;
15251556Srgrimes			break;
15261556Srgrimes		}
15271556Srgrimesdft:	        default:
152845514Stegge			if (squoted && *q == CTLESC)
152945514Stegge				q++;
15301556Srgrimes			if (*q++ != c)
15311556Srgrimes				return 0;
15321556Srgrimes			break;
15331556Srgrimes		}
15341556Srgrimes	}
15351556Srgrimesbreakloop:
15361556Srgrimes	if (*q != '\0')
15371556Srgrimes		return 0;
15381556Srgrimes	return 1;
15391556Srgrimes}
15401556Srgrimes
15411556Srgrimes
15421556Srgrimes
15431556Srgrimes/*
1544212243Sjilles * Remove any CTLESC and CTLQUOTEMARK characters from a string.
15451556Srgrimes */
15461556Srgrimes
15471556Srgrimesvoid
154890111Simprmescapes(char *str)
154938887Stegge{
155025233Ssteve	char *p, *q;
15511556Srgrimes
15521556Srgrimes	p = str;
1553214512Sjilles	while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) {
15541556Srgrimes		if (*p++ == '\0')
15551556Srgrimes			return;
15561556Srgrimes	}
15571556Srgrimes	q = p;
15581556Srgrimes	while (*p) {
1559214512Sjilles		if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
156038887Stegge			p++;
156138887Stegge			continue;
156238887Stegge		}
15631556Srgrimes		if (*p == CTLESC)
15641556Srgrimes			p++;
15651556Srgrimes		*q++ = *p++;
15661556Srgrimes	}
15671556Srgrimes	*q = '\0';
15681556Srgrimes}
15691556Srgrimes
15701556Srgrimes
15711556Srgrimes
15721556Srgrimes/*
15731556Srgrimes * See if a pattern matches in a case statement.
15741556Srgrimes */
15751556Srgrimes
15761556Srgrimesint
1577200956Sjillescasematch(union node *pattern, const char *val)
157890111Simp{
15791556Srgrimes	struct stackmark smark;
15801556Srgrimes	int result;
15811556Srgrimes	char *p;
15821556Srgrimes
15831556Srgrimes	setstackmark(&smark);
15841556Srgrimes	argbackq = pattern->narg.backquote;
15851556Srgrimes	STARTSTACKSTR(expdest);
15861556Srgrimes	ifslastp = NULL;
15871556Srgrimes	argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
15881556Srgrimes	STPUTC('\0', expdest);
15891556Srgrimes	p = grabstackstr(expdest);
159045514Stegge	result = patmatch(p, val, 0);
15911556Srgrimes	popstackmark(&smark);
15921556Srgrimes	return result;
15931556Srgrimes}
159417987Speter
159517987Speter/*
159617987Speter * Our own itoa().
159717987Speter */
159817987Speter
1599213811Sobrienstatic char *
160090111Simpcvtnum(int num, char *buf)
160190111Simp{
160217987Speter	char temp[32];
160317987Speter	int neg = num < 0;
160417987Speter	char *p = temp + 31;
160517987Speter
160617987Speter	temp[31] = '\0';
160717987Speter
160817987Speter	do {
160917987Speter		*--p = num % 10 + '0';
161017987Speter	} while ((num /= 10) != 0);
161117987Speter
161217987Speter	if (neg)
161317987Speter		*--p = '-';
161417987Speter
1615215783Sjilles	STPUTS(p, buf);
161617987Speter	return buf;
161717987Speter}
1618108286Stjr
1619108286Stjr/*
1620216778Sjilles * Check statically if expanding a string may have side effects.
1621216778Sjilles */
1622216778Sjillesint
1623216778Sjillesexpandhassideeffects(const char *p)
1624216778Sjilles{
1625216778Sjilles	int c;
1626216778Sjilles	int arinest;
1627216778Sjilles
1628216778Sjilles	arinest = 0;
1629216778Sjilles	while ((c = *p++) != '\0') {
1630216778Sjilles		switch (c) {
1631216778Sjilles		case CTLESC:
1632216778Sjilles			p++;
1633216778Sjilles			break;
1634216778Sjilles		case CTLVAR:
1635216778Sjilles			c = *p++;
1636216778Sjilles			/* Expanding $! sets the job to remembered. */
1637216778Sjilles			if (*p == '!')
1638216778Sjilles				return 1;
1639216778Sjilles			if ((c & VSTYPE) == VSASSIGN)
1640216778Sjilles				return 1;
1641216778Sjilles			/*
1642216778Sjilles			 * If we are in arithmetic, the parameter may contain
1643216778Sjilles			 * '=' which may cause side effects. Exceptions are
1644216778Sjilles			 * the length of a parameter and $$, $# and $? which
1645216778Sjilles			 * are always numeric.
1646216778Sjilles			 */
1647216778Sjilles			if ((c & VSTYPE) == VSLENGTH) {
1648216778Sjilles				while (*p != '=')
1649216778Sjilles					p++;
1650216778Sjilles				p++;
1651216778Sjilles				break;
1652216778Sjilles			}
1653216778Sjilles			if ((*p == '$' || *p == '#' || *p == '?') &&
1654216778Sjilles			    p[1] == '=') {
1655216778Sjilles				p += 2;
1656216778Sjilles				break;
1657216778Sjilles			}
1658216778Sjilles			if (arinest > 0)
1659216778Sjilles				return 1;
1660216778Sjilles			break;
1661216778Sjilles		case CTLBACKQ:
1662216778Sjilles		case CTLBACKQ | CTLQUOTE:
1663216778Sjilles			if (arinest > 0)
1664216778Sjilles				return 1;
1665216778Sjilles			break;
1666216778Sjilles		case CTLARI:
1667216778Sjilles			arinest++;
1668216778Sjilles			break;
1669216778Sjilles		case CTLENDARI:
1670216778Sjilles			arinest--;
1671216778Sjilles			break;
1672216778Sjilles		case '=':
1673216778Sjilles			if (*p == '=') {
1674216778Sjilles				/* Allow '==' operator. */
1675216778Sjilles				p++;
1676216778Sjilles				continue;
1677216778Sjilles			}
1678216778Sjilles			if (arinest > 0)
1679216778Sjilles				return 1;
1680216778Sjilles			break;
1681216778Sjilles		case '!': case '<': case '>':
1682216778Sjilles			/* Allow '!=', '<=', '>=' operators. */
1683216778Sjilles			if (*p == '=')
1684216778Sjilles				p++;
1685216778Sjilles			break;
1686216778Sjilles		}
1687216778Sjilles	}
1688216778Sjilles	return 0;
1689216778Sjilles}
1690216778Sjilles
1691216778Sjilles/*
1692108286Stjr * Do most of the work for wordexp(3).
1693108286Stjr */
1694108286Stjr
1695108286Stjrint
1696108286Stjrwordexpcmd(int argc, char **argv)
1697108286Stjr{
1698108286Stjr	size_t len;
1699108286Stjr	int i;
1700108286Stjr
1701108286Stjr	out1fmt("%08x", argc - 1);
1702108286Stjr	for (i = 1, len = 0; i < argc; i++)
1703108286Stjr		len += strlen(argv[i]);
1704108286Stjr	out1fmt("%08x", (int)len);
1705215567Sjilles	for (i = 1; i < argc; i++)
1706215567Sjilles		outbin(argv[i], strlen(argv[i]) + 1, out1);
1707108286Stjr        return (0);
1708108286Stjr}
1709