xref: /freebsd/bin/sh/expand.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44710b07eSJilles Tjoelker  * Copyright (c) 1997-2005
54710b07eSJilles Tjoelker  *	Herbert Xu <herbert@gondor.apana.org.au>.  All rights reserved.
647d8814cSJilles Tjoelker  * Copyright (c) 2010-2015
747d8814cSJilles Tjoelker  *	Jilles Tjoelker <jilles@stack.nl>.  All rights reserved.
84b88c807SRodney W. Grimes  *
94b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
104b88c807SRodney W. Grimes  * Kenneth Almquist.
114b88c807SRodney W. Grimes  *
124b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
134b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
144b88c807SRodney W. Grimes  * are met:
154b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
164b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
174b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
184b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
194b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
383d7b5b93SPhilippe Charnier #if 0
393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)expand.c	8.5 (Berkeley) 5/15/95";
403d7b5b93SPhilippe Charnier #endif
414b88c807SRodney W. Grimes #endif /* not lint */
422749b141SDavid E. O'Brien #include <sys/cdefs.h>
432749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
444b88c807SRodney W. Grimes 
45aa9caaf6SPeter Wemm #include <sys/types.h>
46aa9caaf6SPeter Wemm #include <sys/time.h>
47aa9caaf6SPeter Wemm #include <sys/stat.h>
48aa9caaf6SPeter Wemm #include <dirent.h>
498ab2e970SJohn Baldwin #include <errno.h>
508ab2e970SJohn Baldwin #include <inttypes.h>
513cd859a7SAndrey A. Chernov #include <limits.h>
528ab2e970SJohn Baldwin #include <pwd.h>
536f47734fSTor Egge #include <stdio.h>
548ab2e970SJohn Baldwin #include <stdlib.h>
552c25061fSTim J. Robbins #include <string.h>
568ab2e970SJohn Baldwin #include <unistd.h>
577cc6b3dfSJilles Tjoelker #include <wchar.h>
58ff4dc672SJilles Tjoelker #include <wctype.h>
59aa9caaf6SPeter Wemm 
604b88c807SRodney W. Grimes /*
614b88c807SRodney W. Grimes  * Routines to expand arguments to commands.  We have to deal with
624b88c807SRodney W. Grimes  * backquotes, shell variables, and file metacharacters.
634b88c807SRodney W. Grimes  */
644b88c807SRodney W. Grimes 
654b88c807SRodney W. Grimes #include "shell.h"
664b88c807SRodney W. Grimes #include "main.h"
674b88c807SRodney W. Grimes #include "nodes.h"
684b88c807SRodney W. Grimes #include "eval.h"
694b88c807SRodney W. Grimes #include "expand.h"
704b88c807SRodney W. Grimes #include "syntax.h"
714b88c807SRodney W. Grimes #include "parser.h"
724b88c807SRodney W. Grimes #include "jobs.h"
734b88c807SRodney W. Grimes #include "options.h"
744b88c807SRodney W. Grimes #include "var.h"
754b88c807SRodney W. Grimes #include "input.h"
764b88c807SRodney W. Grimes #include "output.h"
774b88c807SRodney W. Grimes #include "memalloc.h"
784b88c807SRodney W. Grimes #include "error.h"
794b88c807SRodney W. Grimes #include "mystring.h"
80aa9caaf6SPeter Wemm #include "arith.h"
81aa9caaf6SPeter Wemm #include "show.h"
82454a02b3SJilles Tjoelker #include "builtins.h"
834b88c807SRodney W. Grimes 
8447d8814cSJilles Tjoelker enum wordstate { WORD_IDLE, WORD_WS_DELIMITED, WORD_QUOTEMARK };
854b88c807SRodney W. Grimes 
8647d8814cSJilles Tjoelker struct worddest {
870e39c931SJilles Tjoelker 	struct arglist *list;
8847d8814cSJilles Tjoelker 	enum wordstate state;
894b88c807SRodney W. Grimes };
904b88c807SRodney W. Grimes 
91aa7b6f82SDavid E. O'Brien static char *expdest;			/* output of current string */
92aa7b6f82SDavid E. O'Brien static struct nodelist *argbackq;	/* list of back quote expressions */
934b88c807SRodney W. Grimes 
9452d5897dSJilles Tjoelker static const char *argstr(const char *, int, struct worddest *);
9552d5897dSJilles Tjoelker static const char *exptilde(const char *, int);
9652d5897dSJilles Tjoelker static const char *expari(const char *, int, struct worddest *);
9747d8814cSJilles Tjoelker static void expbackq(union node *, int, int, struct worddest *);
9852d5897dSJilles Tjoelker static void subevalvar_trim(const char *, int, int, int);
9952d5897dSJilles Tjoelker static int subevalvar_misc(const char *, const char *, int, int, int);
10052d5897dSJilles Tjoelker static const char *evalvar(const char *, int, struct worddest *);
10161346cbdSJilles Tjoelker static int varisset(const char *, int);
10247d8814cSJilles Tjoelker static void strtodest(const char *, int, int, int, struct worddest *);
10347d8814cSJilles Tjoelker static void reprocess(int, int, int, int, struct worddest *);
10447d8814cSJilles Tjoelker static void varvalue(const char *, int, int, int, struct worddest *);
1050e39c931SJilles Tjoelker static void expandmeta(char *, struct arglist *);
1068ef0ae8aSJilles Tjoelker static void expmeta(char *, char *, struct arglist *);
1078ef0ae8aSJilles Tjoelker static int expsortcmp(const void *, const void *);
10847d8814cSJilles Tjoelker static int patmatch(const char *, const char *);
10947d8814cSJilles Tjoelker static void cvtnum(int, char *);
110fa93fc65SAndrey A. Chernov static int collate_range_cmp(wchar_t, wchar_t);
1113cd859a7SAndrey A. Chernov 
1128ef0ae8aSJilles Tjoelker void
1138ef0ae8aSJilles Tjoelker emptyarglist(struct arglist *list)
1148ef0ae8aSJilles Tjoelker {
1158ef0ae8aSJilles Tjoelker 
1168ef0ae8aSJilles Tjoelker 	list->args = list->smallarg;
1178ef0ae8aSJilles Tjoelker 	list->count = 0;
1188ef0ae8aSJilles Tjoelker 	list->capacity = sizeof(list->smallarg) / sizeof(list->smallarg[0]);
1198ef0ae8aSJilles Tjoelker }
1208ef0ae8aSJilles Tjoelker 
121046bfe52SJilles Tjoelker void
1228ef0ae8aSJilles Tjoelker appendarglist(struct arglist *list, char *str)
1238ef0ae8aSJilles Tjoelker {
1248ef0ae8aSJilles Tjoelker 	char **newargs;
1258ef0ae8aSJilles Tjoelker 	int newcapacity;
1268ef0ae8aSJilles Tjoelker 
1278ef0ae8aSJilles Tjoelker 	if (list->count >= list->capacity) {
1288ef0ae8aSJilles Tjoelker 		newcapacity = list->capacity * 2;
1298ef0ae8aSJilles Tjoelker 		if (newcapacity < 16)
1308ef0ae8aSJilles Tjoelker 			newcapacity = 16;
1318ef0ae8aSJilles Tjoelker 		if (newcapacity > INT_MAX / (int)sizeof(newargs[0]))
1328ef0ae8aSJilles Tjoelker 			error("Too many entries in arglist");
1338ef0ae8aSJilles Tjoelker 		newargs = stalloc(newcapacity * sizeof(newargs[0]));
1348ef0ae8aSJilles Tjoelker 		memcpy(newargs, list->args, list->count * sizeof(newargs[0]));
1358ef0ae8aSJilles Tjoelker 		list->args = newargs;
1368ef0ae8aSJilles Tjoelker 		list->capacity = newcapacity;
1378ef0ae8aSJilles Tjoelker 	}
1388ef0ae8aSJilles Tjoelker 	list->args[list->count++] = str;
1398ef0ae8aSJilles Tjoelker }
1408ef0ae8aSJilles Tjoelker 
141fa93fc65SAndrey A. Chernov static int
142fa93fc65SAndrey A. Chernov collate_range_cmp(wchar_t c1, wchar_t c2)
143fa93fc65SAndrey A. Chernov {
144fa93fc65SAndrey A. Chernov 	static wchar_t s1[2], s2[2];
145fa93fc65SAndrey A. Chernov 
146fa93fc65SAndrey A. Chernov 	s1[0] = c1;
147fa93fc65SAndrey A. Chernov 	s2[0] = c2;
148fa93fc65SAndrey A. Chernov 	return (wcscoll(s1, s2));
149fa93fc65SAndrey A. Chernov }
150fa93fc65SAndrey A. Chernov 
151f7dea851SJilles Tjoelker static char *
152f7dea851SJilles Tjoelker stputs_quotes(const char *data, const char *syntax, char *p)
153f7dea851SJilles Tjoelker {
154f7dea851SJilles Tjoelker 	while (*data) {
155f7dea851SJilles Tjoelker 		CHECKSTRSPACE(2, p);
156f7dea851SJilles Tjoelker 		if (syntax[(int)*data] == CCTL)
157f7dea851SJilles Tjoelker 			USTPUTC(CTLESC, p);
158f7dea851SJilles Tjoelker 		USTPUTC(*data++, p);
159f7dea851SJilles Tjoelker 	}
160f7dea851SJilles Tjoelker 	return (p);
161f7dea851SJilles Tjoelker }
162f7dea851SJilles Tjoelker #define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p)
1634b88c807SRodney W. Grimes 
16447d8814cSJilles Tjoelker static char *
1650e39c931SJilles Tjoelker nextword(char c, int flag, char *p, struct worddest *dst)
16647d8814cSJilles Tjoelker {
16747d8814cSJilles Tjoelker 	int is_ws;
16847d8814cSJilles Tjoelker 
16947d8814cSJilles Tjoelker 	is_ws = c == '\t' || c == '\n' || c == ' ';
17047d8814cSJilles Tjoelker 	if (p != stackblock() || (is_ws ? dst->state == WORD_QUOTEMARK :
17147d8814cSJilles Tjoelker 	    dst->state != WORD_WS_DELIMITED) || c == '\0') {
17247d8814cSJilles Tjoelker 		STPUTC('\0', p);
1730e39c931SJilles Tjoelker 		if (flag & EXP_GLOB)
1740e39c931SJilles Tjoelker 			expandmeta(grabstackstr(p), dst->list);
1750e39c931SJilles Tjoelker 		else
1760e39c931SJilles Tjoelker 			appendarglist(dst->list, grabstackstr(p));
17747d8814cSJilles Tjoelker 		dst->state = is_ws ? WORD_WS_DELIMITED : WORD_IDLE;
17847d8814cSJilles Tjoelker 	} else if (!is_ws && dst->state == WORD_WS_DELIMITED)
17947d8814cSJilles Tjoelker 		dst->state = WORD_IDLE;
18047d8814cSJilles Tjoelker 	/* Reserve space while the stack string is empty. */
1810e39c931SJilles Tjoelker 	appendarglist(dst->list, NULL);
1820e39c931SJilles Tjoelker 	dst->list->count--;
18347d8814cSJilles Tjoelker 	STARTSTACKSTR(p);
18447d8814cSJilles Tjoelker 	return p;
18547d8814cSJilles Tjoelker }
1860e39c931SJilles Tjoelker #define NEXTWORD(c, flag, p, dstlist) p = nextword(c, flag, p, dstlist)
18747d8814cSJilles Tjoelker 
18847d8814cSJilles Tjoelker static char *
1890e39c931SJilles Tjoelker stputs_split(const char *data, const char *syntax, int flag, char *p,
19047d8814cSJilles Tjoelker     struct worddest *dst)
19147d8814cSJilles Tjoelker {
19247d8814cSJilles Tjoelker 	const char *ifs;
19347d8814cSJilles Tjoelker 	char c;
19447d8814cSJilles Tjoelker 
19547d8814cSJilles Tjoelker 	ifs = ifsset() ? ifsval() : " \t\n";
19647d8814cSJilles Tjoelker 	while (*data) {
19747d8814cSJilles Tjoelker 		CHECKSTRSPACE(2, p);
19847d8814cSJilles Tjoelker 		c = *data++;
19947d8814cSJilles Tjoelker 		if (strchr(ifs, c) != NULL) {
2000e39c931SJilles Tjoelker 			NEXTWORD(c, flag, p, dst);
20147d8814cSJilles Tjoelker 			continue;
20247d8814cSJilles Tjoelker 		}
2030e39c931SJilles Tjoelker 		if (flag & EXP_GLOB && syntax[(int)c] == CCTL)
20447d8814cSJilles Tjoelker 			USTPUTC(CTLESC, p);
20547d8814cSJilles Tjoelker 		USTPUTC(c, p);
20647d8814cSJilles Tjoelker 	}
20747d8814cSJilles Tjoelker 	return (p);
20847d8814cSJilles Tjoelker }
2090e39c931SJilles Tjoelker #define STPUTS_SPLIT(data, syntax, flag, p, dst) p = stputs_split((data), syntax, flag, p, dst)
21047d8814cSJilles Tjoelker 
2114b88c807SRodney W. Grimes /*
2122ca3d70fSJilles Tjoelker  * Perform expansions on an argument, placing the resulting list of arguments
2132ca3d70fSJilles Tjoelker  * in arglist.  Parameter expansion, command substitution and arithmetic
2142ca3d70fSJilles Tjoelker  * expansion are always performed; additional expansions can be requested
2152ca3d70fSJilles Tjoelker  * via flag (EXP_*).
2162ca3d70fSJilles Tjoelker  * The result is left in the stack string.
2173e0b768cSJilles Tjoelker  * When arglist is NULL, perform here document expansion.
2182ca3d70fSJilles Tjoelker  *
2192ca3d70fSJilles Tjoelker  * Caution: this function uses global state and is not reentrant.
2202ca3d70fSJilles Tjoelker  * However, a new invocation after an interrupted invocation is safe
2212ca3d70fSJilles Tjoelker  * and will reset the global state for the new call.
2224b88c807SRodney W. Grimes  */
2234b88c807SRodney W. Grimes void
2245134c3f7SWarner Losh expandarg(union node *arg, struct arglist *arglist, int flag)
2254b88c807SRodney W. Grimes {
22647d8814cSJilles Tjoelker 	struct worddest exparg;
2274b88c807SRodney W. Grimes 
2280e39c931SJilles Tjoelker 	if (fflag)
2290e39c931SJilles Tjoelker 		flag &= ~EXP_GLOB;
2304b88c807SRodney W. Grimes 	argbackq = arg->narg.backquote;
2310e39c931SJilles Tjoelker 	exparg.list = arglist;
23247d8814cSJilles Tjoelker 	exparg.state = WORD_IDLE;
2334b88c807SRodney W. Grimes 	STARTSTACKSTR(expdest);
23447d8814cSJilles Tjoelker 	argstr(arg->narg.text, flag, &exparg);
2354b88c807SRodney W. Grimes 	if (arglist == NULL) {
236292e6676SJilles Tjoelker 		STACKSTRNUL(expdest);
2374b88c807SRodney W. Grimes 		return;			/* here document expanded */
2384b88c807SRodney W. Grimes 	}
2390e39c931SJilles Tjoelker 	if ((flag & EXP_SPLIT) == 0 || expdest != stackblock() ||
24047d8814cSJilles Tjoelker 	    exparg.state == WORD_QUOTEMARK) {
2414b88c807SRodney W. Grimes 		STPUTC('\0', expdest);
2420e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT) {
2430e39c931SJilles Tjoelker 			if (flag & EXP_GLOB)
2440e39c931SJilles Tjoelker 				expandmeta(grabstackstr(expdest), exparg.list);
24547d8814cSJilles Tjoelker 			else
2460e39c931SJilles Tjoelker 				appendarglist(exparg.list, grabstackstr(expdest));
2470e39c931SJilles Tjoelker 		}
2480e39c931SJilles Tjoelker 	}
2490e39c931SJilles Tjoelker 	if ((flag & EXP_SPLIT) == 0)
25047d8814cSJilles Tjoelker 		appendarglist(arglist, grabstackstr(expdest));
2514b88c807SRodney W. Grimes }
2524b88c807SRodney W. Grimes 
2534b88c807SRodney W. Grimes 
2544b88c807SRodney W. Grimes 
2554b88c807SRodney W. Grimes /*
2562ca3d70fSJilles Tjoelker  * Perform parameter expansion, command substitution and arithmetic
2572ca3d70fSJilles Tjoelker  * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
258ce16da82SJilles Tjoelker  * Processing ends at a CTLENDVAR or CTLENDARI character as well as '\0'.
2592ca3d70fSJilles Tjoelker  * This is used to expand word in ${var+word} etc.
2600e39c931SJilles Tjoelker  * If EXP_GLOB or EXP_CASE are set, keep and/or generate CTLESC
2612ca3d70fSJilles Tjoelker  * characters to allow for further processing.
26247d8814cSJilles Tjoelker  *
2630e39c931SJilles Tjoelker  * If EXP_SPLIT is set, dst receives any complete words produced.
2644b88c807SRodney W. Grimes  */
26552d5897dSJilles Tjoelker static const char *
26652d5897dSJilles Tjoelker argstr(const char *p, int flag, struct worddest *dst)
2674b88c807SRodney W. Grimes {
26896522b88SSteve Price 	char c;
2690e39c931SJilles Tjoelker 	int quotes = flag & (EXP_GLOB | EXP_CASE);	/* do CTLESC */
2704b88c807SRodney W. Grimes 	int firsteq = 1;
271048f2667SJilles Tjoelker 	int split_lit;
272048f2667SJilles Tjoelker 	int lit_quoted;
2734b88c807SRodney W. Grimes 
274048f2667SJilles Tjoelker 	split_lit = flag & EXP_SPLIT_LIT;
275048f2667SJilles Tjoelker 	lit_quoted = flag & EXP_LIT_QUOTED;
276048f2667SJilles Tjoelker 	flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED);
2774b88c807SRodney W. Grimes 	if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
2784b88c807SRodney W. Grimes 		p = exptilde(p, flag);
2794b88c807SRodney W. Grimes 	for (;;) {
2809d37e157SJilles Tjoelker 		CHECKSTRSPACE(2, expdest);
2814b88c807SRodney W. Grimes 		switch (c = *p++) {
2824b88c807SRodney W. Grimes 		case '\0':
283a2cba42fSJilles Tjoelker 			return (p - 1);
2842ca3d70fSJilles Tjoelker 		case CTLENDVAR:
285ce16da82SJilles Tjoelker 		case CTLENDARI:
286a2cba42fSJilles Tjoelker 			return (p);
2876f47734fSTor Egge 		case CTLQUOTEMARK:
288048f2667SJilles Tjoelker 			lit_quoted = 1;
2896f47734fSTor Egge 			/* "$@" syntax adherence hack */
290a5cb58abSJilles Tjoelker 			if (p[0] == CTLVAR && (p[1] & VSQUOTE) != 0 &&
291a5cb58abSJilles Tjoelker 			    p[2] == '@' && p[3] == '=')
2926f47734fSTor Egge 				break;
2930e39c931SJilles Tjoelker 			if ((flag & EXP_SPLIT) != 0 && expdest == stackblock())
29447d8814cSJilles Tjoelker 				dst->state = WORD_QUOTEMARK;
2956f47734fSTor Egge 			break;
296048f2667SJilles Tjoelker 		case CTLQUOTEEND:
297048f2667SJilles Tjoelker 			lit_quoted = 0;
298048f2667SJilles Tjoelker 			break;
2994b88c807SRodney W. Grimes 		case CTLESC:
3004b88c807SRodney W. Grimes 			c = *p++;
30147d8814cSJilles Tjoelker 			if (split_lit && !lit_quoted &&
30247d8814cSJilles Tjoelker 			    strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
3030e39c931SJilles Tjoelker 				NEXTWORD(c, flag, expdest, dst);
30447d8814cSJilles Tjoelker 				break;
30547d8814cSJilles Tjoelker 			}
30647d8814cSJilles Tjoelker 			if (quotes)
30747d8814cSJilles Tjoelker 				USTPUTC(CTLESC, expdest);
3089d37e157SJilles Tjoelker 			USTPUTC(c, expdest);
3094b88c807SRodney W. Grimes 			break;
3104b88c807SRodney W. Grimes 		case CTLVAR:
31147d8814cSJilles Tjoelker 			p = evalvar(p, flag, dst);
3124b88c807SRodney W. Grimes 			break;
3134b88c807SRodney W. Grimes 		case CTLBACKQ:
3144b88c807SRodney W. Grimes 		case CTLBACKQ|CTLQUOTE:
31547d8814cSJilles Tjoelker 			expbackq(argbackq->n, c & CTLQUOTE, flag, dst);
3164b88c807SRodney W. Grimes 			argbackq = argbackq->next;
3174b88c807SRodney W. Grimes 			break;
318ce16da82SJilles Tjoelker 		case CTLARI:
31947d8814cSJilles Tjoelker 			p = expari(p, flag, dst);
3204b88c807SRodney W. Grimes 			break;
3214b88c807SRodney W. Grimes 		case ':':
3224b88c807SRodney W. Grimes 		case '=':
3234b88c807SRodney W. Grimes 			/*
3244b88c807SRodney W. Grimes 			 * sort of a hack - expand tildes in variable
3254b88c807SRodney W. Grimes 			 * assignments (after the first '=' and after ':'s).
3264b88c807SRodney W. Grimes 			 */
32747d8814cSJilles Tjoelker 			if (split_lit && !lit_quoted &&
32847d8814cSJilles Tjoelker 			    strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
3290e39c931SJilles Tjoelker 				NEXTWORD(c, flag, expdest, dst);
33047d8814cSJilles Tjoelker 				break;
33147d8814cSJilles Tjoelker 			}
3329d37e157SJilles Tjoelker 			USTPUTC(c, expdest);
333048f2667SJilles Tjoelker 			if (flag & EXP_VARTILDE && *p == '~' &&
334048f2667SJilles Tjoelker 			    (c != '=' || firsteq)) {
335048f2667SJilles Tjoelker 				if (c == '=')
3364b88c807SRodney W. Grimes 					firsteq = 0;
3374b88c807SRodney W. Grimes 				p = exptilde(p, flag);
3384b88c807SRodney W. Grimes 			}
3394b88c807SRodney W. Grimes 			break;
3404b88c807SRodney W. Grimes 		default:
34147d8814cSJilles Tjoelker 			if (split_lit && !lit_quoted &&
34247d8814cSJilles Tjoelker 			    strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
3430e39c931SJilles Tjoelker 				NEXTWORD(c, flag, expdest, dst);
34447d8814cSJilles Tjoelker 				break;
34547d8814cSJilles Tjoelker 			}
3469d37e157SJilles Tjoelker 			USTPUTC(c, expdest);
3474b88c807SRodney W. Grimes 		}
3484b88c807SRodney W. Grimes 	}
3494b88c807SRodney W. Grimes }
3504b88c807SRodney W. Grimes 
3512ca3d70fSJilles Tjoelker /*
3522ca3d70fSJilles Tjoelker  * Perform tilde expansion, placing the result in the stack string and
3532ca3d70fSJilles Tjoelker  * returning the next position in the input string to process.
3542ca3d70fSJilles Tjoelker  */
35552d5897dSJilles Tjoelker static const char *
35652d5897dSJilles Tjoelker exptilde(const char *p, int flag)
3574b88c807SRodney W. Grimes {
35852d5897dSJilles Tjoelker 	char c;
35952d5897dSJilles Tjoelker 	const char *startp = p;
36052d5897dSJilles Tjoelker 	const char *user;
3614b88c807SRodney W. Grimes 	struct passwd *pw;
3624b88c807SRodney W. Grimes 	char *home;
36352d5897dSJilles Tjoelker 	int len;
3644b88c807SRodney W. Grimes 
3657034d8dfSJilles Tjoelker 	for (;;) {
3667034d8dfSJilles Tjoelker 		c = *p;
3674b88c807SRodney W. Grimes 		switch(c) {
36805c10507SJilles Tjoelker 		case CTLESC: /* This means CTL* are always considered quoted. */
36905c10507SJilles Tjoelker 		case CTLVAR:
37005c10507SJilles Tjoelker 		case CTLBACKQ:
37105c10507SJilles Tjoelker 		case CTLBACKQ | CTLQUOTE:
37205c10507SJilles Tjoelker 		case CTLARI:
37305c10507SJilles Tjoelker 		case CTLENDARI:
3745557a02aSTor Egge 		case CTLQUOTEMARK:
3755557a02aSTor Egge 			return (startp);
3764b88c807SRodney W. Grimes 		case ':':
3777034d8dfSJilles Tjoelker 			if ((flag & EXP_VARTILDE) == 0)
3784b88c807SRodney W. Grimes 				break;
3797034d8dfSJilles Tjoelker 			/* FALLTHROUGH */
3807034d8dfSJilles Tjoelker 		case '\0':
3814b88c807SRodney W. Grimes 		case '/':
382634e9188SJilles Tjoelker 		case CTLENDVAR:
38352d5897dSJilles Tjoelker 			len = p - startp - 1;
38452d5897dSJilles Tjoelker 			STPUTBIN(startp + 1, len, expdest);
38552d5897dSJilles Tjoelker 			STACKSTRNUL(expdest);
38652d5897dSJilles Tjoelker 			user = expdest - len;
38752d5897dSJilles Tjoelker 			if (*user == '\0') {
38833c5acf0SJilles Tjoelker 				home = lookupvar("HOME");
3894b88c807SRodney W. Grimes 			} else {
39052d5897dSJilles Tjoelker 				pw = getpwnam(user);
39133c5acf0SJilles Tjoelker 				home = pw != NULL ? pw->pw_dir : NULL;
3924b88c807SRodney W. Grimes 			}
39352d5897dSJilles Tjoelker 			STADJUST(-len, expdest);
39433c5acf0SJilles Tjoelker 			if (home == NULL || *home == '\0')
39533c5acf0SJilles Tjoelker 				return (startp);
39647d8814cSJilles Tjoelker 			strtodest(home, flag, VSNORMAL, 1, NULL);
3974b88c807SRodney W. Grimes 			return (p);
3984b88c807SRodney W. Grimes 		}
3997034d8dfSJilles Tjoelker 		p++;
4007034d8dfSJilles Tjoelker 	}
4017034d8dfSJilles Tjoelker }
4024b88c807SRodney W. Grimes 
4034b88c807SRodney W. Grimes 
4044b88c807SRodney W. Grimes /*
405ce16da82SJilles Tjoelker  * Expand arithmetic expression.
4064b88c807SRodney W. Grimes  */
40752d5897dSJilles Tjoelker static const char *
40852d5897dSJilles Tjoelker expari(const char *p, int flag, struct worddest *dst)
4094b88c807SRodney W. Grimes {
410ce16da82SJilles Tjoelker 	char *q, *start;
411d9d588d4SStefan Farfeleder 	arith_t result;
4126f47734fSTor Egge 	int begoff;
4136f47734fSTor Egge 	int quoted;
414ce16da82SJilles Tjoelker 	int adj;
4154b88c807SRodney W. Grimes 
416ce16da82SJilles Tjoelker 	quoted = *p++ == '"';
417ce16da82SJilles Tjoelker 	begoff = expdest - stackblock();
41847d8814cSJilles Tjoelker 	p = argstr(p, 0, NULL);
419ce16da82SJilles Tjoelker 	STPUTC('\0', expdest);
420ce16da82SJilles Tjoelker 	start = stackblock() + begoff;
421ce16da82SJilles Tjoelker 
422593e925aSJilles Tjoelker 	q = grabstackstr(expdest);
423ce16da82SJilles Tjoelker 	result = arith(start);
424593e925aSJilles Tjoelker 	ungrabstackstr(q, expdest);
425ce16da82SJilles Tjoelker 
426ce16da82SJilles Tjoelker 	start = stackblock() + begoff;
427ce16da82SJilles Tjoelker 	adj = start - expdest;
428ce16da82SJilles Tjoelker 	STADJUST(adj, expdest);
429ce16da82SJilles Tjoelker 
430ce16da82SJilles Tjoelker 	CHECKSTRSPACE((int)(DIGITS(result) + 1), expdest);
431ce16da82SJilles Tjoelker 	fmtstr(expdest, DIGITS(result), ARITH_FORMAT_STR, result);
432ce16da82SJilles Tjoelker 	adj = strlen(expdest);
433ce16da82SJilles Tjoelker 	STADJUST(adj, expdest);
434ce16da82SJilles Tjoelker 	if (!quoted)
43547d8814cSJilles Tjoelker 		reprocess(expdest - adj - stackblock(), flag, VSNORMAL, 0, dst);
436ce16da82SJilles Tjoelker 	return p;
4374b88c807SRodney W. Grimes }
4384b88c807SRodney W. Grimes 
4394b88c807SRodney W. Grimes 
4404b88c807SRodney W. Grimes /*
4412ca3d70fSJilles Tjoelker  * Perform command substitution.
4424b88c807SRodney W. Grimes  */
44388328642SDavid E. O'Brien static void
44447d8814cSJilles Tjoelker expbackq(union node *cmd, int quoted, int flag, struct worddest *dst)
4454b88c807SRodney W. Grimes {
4464b88c807SRodney W. Grimes 	struct backcmd in;
4474b88c807SRodney W. Grimes 	int i;
4484b88c807SRodney W. Grimes 	char buf[128];
4494b88c807SRodney W. Grimes 	char *p;
4504b88c807SRodney W. Grimes 	char *dest = expdest;
4514b88c807SRodney W. Grimes 	struct nodelist *saveargbackq;
4524b88c807SRodney W. Grimes 	char lastc;
4534b88c807SRodney W. Grimes 	char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
4540e39c931SJilles Tjoelker 	int quotes = flag & (EXP_GLOB | EXP_CASE);
45546c6b52dSJilles Tjoelker 	size_t nnl;
45647d8814cSJilles Tjoelker 	const char *ifs;
4574b88c807SRodney W. Grimes 
4584b88c807SRodney W. Grimes 	INTOFF;
4594b88c807SRodney W. Grimes 	saveargbackq = argbackq;
4604b88c807SRodney W. Grimes 	p = grabstackstr(dest);
4614b88c807SRodney W. Grimes 	evalbackcmd(cmd, &in);
4624b88c807SRodney W. Grimes 	ungrabstackstr(p, dest);
4634b88c807SRodney W. Grimes 	argbackq = saveargbackq;
4644b88c807SRodney W. Grimes 
4654b88c807SRodney W. Grimes 	p = in.buf;
46699907703SBill Fenner 	nnl = 0;
4670e39c931SJilles Tjoelker 	if (!quoted && flag & EXP_SPLIT)
46847d8814cSJilles Tjoelker 		ifs = ifsset() ? ifsval() : " \t\n";
46947d8814cSJilles Tjoelker 	else
47047d8814cSJilles Tjoelker 		ifs = "";
47199907703SBill Fenner 	/* Don't copy trailing newlines */
4724b88c807SRodney W. Grimes 	for (;;) {
4734b88c807SRodney W. Grimes 		if (--in.nleft < 0) {
4744b88c807SRodney W. Grimes 			if (in.fd < 0)
4754b88c807SRodney W. Grimes 				break;
4763bb6ada2SJilles Tjoelker 			while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR)
4773bb6ada2SJilles Tjoelker 				;
4784b88c807SRodney W. Grimes 			TRACE(("expbackq: read returns %d\n", i));
4794b88c807SRodney W. Grimes 			if (i <= 0)
4804b88c807SRodney W. Grimes 				break;
4814b88c807SRodney W. Grimes 			p = buf;
4824b88c807SRodney W. Grimes 			in.nleft = i - 1;
4834b88c807SRodney W. Grimes 		}
4844b88c807SRodney W. Grimes 		lastc = *p++;
48547d8814cSJilles Tjoelker 		if (lastc == '\0')
48647d8814cSJilles Tjoelker 			continue;
48799907703SBill Fenner 		if (lastc == '\n') {
48899907703SBill Fenner 			nnl++;
48999907703SBill Fenner 		} else {
49047d8814cSJilles Tjoelker 			if (nnl > 0) {
49147d8814cSJilles Tjoelker 				if (strchr(ifs, '\n') != NULL) {
4920e39c931SJilles Tjoelker 					NEXTWORD('\n', flag, dest, dst);
49347d8814cSJilles Tjoelker 					nnl = 0;
49447d8814cSJilles Tjoelker 				} else {
495d8f32e72SJilles Tjoelker 					CHECKSTRSPACE(nnl + 2, dest);
49699907703SBill Fenner 					while (nnl > 0) {
49799907703SBill Fenner 						nnl--;
498d8f32e72SJilles Tjoelker 						USTPUTC('\n', dest);
49999907703SBill Fenner 					}
50047d8814cSJilles Tjoelker 				}
50147d8814cSJilles Tjoelker 			}
50247d8814cSJilles Tjoelker 			if (strchr(ifs, lastc) != NULL)
5030e39c931SJilles Tjoelker 				NEXTWORD(lastc, flag, dest, dst);
50447d8814cSJilles Tjoelker 			else {
50547d8814cSJilles Tjoelker 				CHECKSTRSPACE(2, dest);
506fa0951d6SJilles Tjoelker 				if (quotes && syntax[(int)lastc] == CCTL)
507d8f32e72SJilles Tjoelker 					USTPUTC(CTLESC, dest);
508d8f32e72SJilles Tjoelker 				USTPUTC(lastc, dest);
5094b88c807SRodney W. Grimes 			}
5104b88c807SRodney W. Grimes 		}
51199907703SBill Fenner 	}
512aa9caaf6SPeter Wemm 
5134b88c807SRodney W. Grimes 	if (in.fd >= 0)
5144b88c807SRodney W. Grimes 		close(in.fd);
5154b88c807SRodney W. Grimes 	if (in.buf)
5164b88c807SRodney W. Grimes 		ckfree(in.buf);
5174b88c807SRodney W. Grimes 	if (in.jp)
51857b2932aSMartin Cracauer 		exitstatus = waitforjob(in.jp, (int *)NULL);
5198ab2e970SJohn Baldwin 	TRACE(("expbackq: size=%td: \"%.*s\"\n",
5208ab2e970SJohn Baldwin 		((dest - stackblock()) - startloc),
5218ab2e970SJohn Baldwin 		(int)((dest - stackblock()) - startloc),
5224b88c807SRodney W. Grimes 		stackblock() + startloc));
5234b88c807SRodney W. Grimes 	expdest = dest;
5244b88c807SRodney W. Grimes 	INTON;
5254b88c807SRodney W. Grimes }
5264b88c807SRodney W. Grimes 
5274b88c807SRodney W. Grimes 
5284b88c807SRodney W. Grimes 
5297034d8dfSJilles Tjoelker static void
5307034d8dfSJilles Tjoelker recordleft(const char *str, const char *loc, char *startp)
5317034d8dfSJilles Tjoelker {
5327034d8dfSJilles Tjoelker 	int amount;
5337034d8dfSJilles Tjoelker 
5347034d8dfSJilles Tjoelker 	amount = ((str - 1) - (loc - startp)) - expdest;
5357034d8dfSJilles Tjoelker 	STADJUST(amount, expdest);
5367034d8dfSJilles Tjoelker 	while (loc != str - 1)
5377034d8dfSJilles Tjoelker 		*startp++ = *loc++;
5387034d8dfSJilles Tjoelker }
5397034d8dfSJilles Tjoelker 
54047d8814cSJilles Tjoelker static void
54152d5897dSJilles Tjoelker subevalvar_trim(const char *p, int strloc, int subtype, int startloc)
542aa9caaf6SPeter Wemm {
543aa9caaf6SPeter Wemm 	char *startp;
544aa9caaf6SPeter Wemm 	char *loc = NULL;
5454f2f5faaSJilles Tjoelker 	char *str;
546aa9caaf6SPeter Wemm 	int c = 0;
547aa9caaf6SPeter Wemm 	struct nodelist *saveargbackq = argbackq;
548ab0a2172SSteve Price 	int amount;
549ab0a2172SSteve Price 
55047d8814cSJilles Tjoelker 	argstr(p, EXP_CASE | EXP_TILDE, NULL);
551aa9caaf6SPeter Wemm 	STACKSTRNUL(expdest);
552aa9caaf6SPeter Wemm 	argbackq = saveargbackq;
553aa9caaf6SPeter Wemm 	startp = stackblock() + startloc;
554ab0a2172SSteve Price 	str = stackblock() + strloc;
555aa9caaf6SPeter Wemm 
556aa9caaf6SPeter Wemm 	switch (subtype) {
557aa9caaf6SPeter Wemm 	case VSTRIMLEFT:
55896522b88SSteve Price 		for (loc = startp; loc < str; loc++) {
559aa9caaf6SPeter Wemm 			c = *loc;
560aa9caaf6SPeter Wemm 			*loc = '\0';
56147d8814cSJilles Tjoelker 			if (patmatch(str, startp)) {
562aa9caaf6SPeter Wemm 				*loc = c;
5637034d8dfSJilles Tjoelker 				recordleft(str, loc, startp);
56447d8814cSJilles Tjoelker 				return;
565aa9caaf6SPeter Wemm 			}
566aa9caaf6SPeter Wemm 			*loc = c;
567aa9caaf6SPeter Wemm 		}
56847d8814cSJilles Tjoelker 		break;
569aa9caaf6SPeter Wemm 
570aa9caaf6SPeter Wemm 	case VSTRIMLEFTMAX:
5718b220a61STor Egge 		for (loc = str - 1; loc >= startp;) {
572aa9caaf6SPeter Wemm 			c = *loc;
573aa9caaf6SPeter Wemm 			*loc = '\0';
57447d8814cSJilles Tjoelker 			if (patmatch(str, startp)) {
575aa9caaf6SPeter Wemm 				*loc = c;
5767034d8dfSJilles Tjoelker 				recordleft(str, loc, startp);
57747d8814cSJilles Tjoelker 				return;
578aa9caaf6SPeter Wemm 			}
579aa9caaf6SPeter Wemm 			*loc = c;
5808b220a61STor Egge 			loc--;
5818b220a61STor Egge 		}
58247d8814cSJilles Tjoelker 		break;
583aa9caaf6SPeter Wemm 
584aa9caaf6SPeter Wemm 	case VSTRIMRIGHT:
5858b220a61STor Egge 		for (loc = str - 1; loc >= startp;) {
58647d8814cSJilles Tjoelker 			if (patmatch(str, loc)) {
587ab0a2172SSteve Price 				amount = loc - expdest;
588ab0a2172SSteve Price 				STADJUST(amount, expdest);
58947d8814cSJilles Tjoelker 				return;
590aa9caaf6SPeter Wemm 			}
5918b220a61STor Egge 			loc--;
5928b220a61STor Egge 		}
59347d8814cSJilles Tjoelker 		break;
594aa9caaf6SPeter Wemm 
595aa9caaf6SPeter Wemm 	case VSTRIMRIGHTMAX:
596aa9caaf6SPeter Wemm 		for (loc = startp; loc < str - 1; loc++) {
59747d8814cSJilles Tjoelker 			if (patmatch(str, loc)) {
598ab0a2172SSteve Price 				amount = loc - expdest;
599ab0a2172SSteve Price 				STADJUST(amount, expdest);
60047d8814cSJilles Tjoelker 				return;
601aa9caaf6SPeter Wemm 			}
602aa9caaf6SPeter Wemm 		}
60347d8814cSJilles Tjoelker 		break;
604aa9caaf6SPeter Wemm 
605aa9caaf6SPeter Wemm 
606aa9caaf6SPeter Wemm 	default:
607aa9caaf6SPeter Wemm 		abort();
608aa9caaf6SPeter Wemm 	}
60947d8814cSJilles Tjoelker 	amount = (expdest - stackblock() - strloc) + 1;
61047d8814cSJilles Tjoelker 	STADJUST(-amount, expdest);
611aa9caaf6SPeter Wemm }
612aa9caaf6SPeter Wemm 
613aa9caaf6SPeter Wemm 
6144f2f5faaSJilles Tjoelker static int
61552d5897dSJilles Tjoelker subevalvar_misc(const char *p, const char *var, int subtype, int startloc,
6164f2f5faaSJilles Tjoelker   int varflags)
6174f2f5faaSJilles Tjoelker {
6184f2f5faaSJilles Tjoelker 	char *startp;
6194f2f5faaSJilles Tjoelker 	struct nodelist *saveargbackq = argbackq;
6204f2f5faaSJilles Tjoelker 	int amount;
6214f2f5faaSJilles Tjoelker 
62247d8814cSJilles Tjoelker 	argstr(p, EXP_TILDE, NULL);
6234f2f5faaSJilles Tjoelker 	STACKSTRNUL(expdest);
6244f2f5faaSJilles Tjoelker 	argbackq = saveargbackq;
6254f2f5faaSJilles Tjoelker 	startp = stackblock() + startloc;
6264f2f5faaSJilles Tjoelker 
6274f2f5faaSJilles Tjoelker 	switch (subtype) {
6284f2f5faaSJilles Tjoelker 	case VSASSIGN:
6294f2f5faaSJilles Tjoelker 		setvar(var, startp, 0);
6304f2f5faaSJilles Tjoelker 		amount = startp - expdest;
6314f2f5faaSJilles Tjoelker 		STADJUST(amount, expdest);
6324f2f5faaSJilles Tjoelker 		return 1;
6334f2f5faaSJilles Tjoelker 
6344f2f5faaSJilles Tjoelker 	case VSQUESTION:
6354f2f5faaSJilles Tjoelker 		if (*p != CTLENDVAR) {
6364f2f5faaSJilles Tjoelker 			outfmt(out2, "%s\n", startp);
6374f2f5faaSJilles Tjoelker 			error((char *)NULL);
6384f2f5faaSJilles Tjoelker 		}
6394f2f5faaSJilles Tjoelker 		error("%.*s: parameter %snot set", (int)(p - var - 1),
6404f2f5faaSJilles Tjoelker 		      var, (varflags & VSNUL) ? "null or " : "");
6414f2f5faaSJilles Tjoelker 		return 0;
6424f2f5faaSJilles Tjoelker 
6434f2f5faaSJilles Tjoelker 	default:
6444f2f5faaSJilles Tjoelker 		abort();
6454f2f5faaSJilles Tjoelker 	}
6464f2f5faaSJilles Tjoelker }
6474f2f5faaSJilles Tjoelker 
6484f2f5faaSJilles Tjoelker 
6494b88c807SRodney W. Grimes /*
6504b88c807SRodney W. Grimes  * Expand a variable, and return a pointer to the next character in the
6514b88c807SRodney W. Grimes  * input string.
6524b88c807SRodney W. Grimes  */
6534b88c807SRodney W. Grimes 
65452d5897dSJilles Tjoelker static const char *
65552d5897dSJilles Tjoelker evalvar(const char *p, int flag, struct worddest *dst)
6564b88c807SRodney W. Grimes {
6574b88c807SRodney W. Grimes 	int subtype;
6584b88c807SRodney W. Grimes 	int varflags;
65952d5897dSJilles Tjoelker 	const char *var;
66061346cbdSJilles Tjoelker 	const char *val;
661c4e5a8a8STor Egge 	int patloc;
6624b88c807SRodney W. Grimes 	int c;
6634b88c807SRodney W. Grimes 	int set;
6644b88c807SRodney W. Grimes 	int special;
6654b88c807SRodney W. Grimes 	int startloc;
666aa9caaf6SPeter Wemm 	int varlen;
6674c244ed2SJilles Tjoelker 	int varlenb;
66847d8814cSJilles Tjoelker 	char buf[21];
6694b88c807SRodney W. Grimes 
670bb4f73caSStefan Farfeleder 	varflags = (unsigned char)*p++;
6714b88c807SRodney W. Grimes 	subtype = varflags & VSTYPE;
6724b88c807SRodney W. Grimes 	var = p;
6734b88c807SRodney W. Grimes 	special = 0;
6744b88c807SRodney W. Grimes 	if (! is_name(*p))
6754b88c807SRodney W. Grimes 		special = 1;
6764b88c807SRodney W. Grimes 	p = strchr(p, '=') + 1;
6774b88c807SRodney W. Grimes again: /* jump here after setting a variable with ${var=text} */
678b71085aaSStefan Farfeleder 	if (varflags & VSLINENO) {
679b71085aaSStefan Farfeleder 		set = 1;
68054396489SJilles Tjoelker 		special = 1;
68154396489SJilles Tjoelker 		val = NULL;
682b71085aaSStefan Farfeleder 	} else if (special) {
68396522b88SSteve Price 		set = varisset(var, varflags & VSNUL);
6844b88c807SRodney W. Grimes 		val = NULL;
6854b88c807SRodney W. Grimes 	} else {
686b2acf887SMartin Cracauer 		val = bltinlookup(var, 1);
687aa9caaf6SPeter Wemm 		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
6884b88c807SRodney W. Grimes 			val = NULL;
6894b88c807SRodney W. Grimes 			set = 0;
6904b88c807SRodney W. Grimes 		} else
6914b88c807SRodney W. Grimes 			set = 1;
6924b88c807SRodney W. Grimes 	}
693aa9caaf6SPeter Wemm 	varlen = 0;
6944b88c807SRodney W. Grimes 	startloc = expdest - stackblock();
69564254a66SJilles Tjoelker 	if (!set && uflag && *var != '@' && *var != '*') {
6961b5a48ffSTim J. Robbins 		switch (subtype) {
6971b5a48ffSTim J. Robbins 		case VSNORMAL:
6981b5a48ffSTim J. Robbins 		case VSTRIMLEFT:
6991b5a48ffSTim J. Robbins 		case VSTRIMLEFTMAX:
7001b5a48ffSTim J. Robbins 		case VSTRIMRIGHT:
7011b5a48ffSTim J. Robbins 		case VSTRIMRIGHTMAX:
7021b5a48ffSTim J. Robbins 		case VSLENGTH:
703024ae004SRuslan Ermilov 			error("%.*s: parameter not set", (int)(p - var - 1),
704024ae004SRuslan Ermilov 			    var);
7051b5a48ffSTim J. Robbins 		}
7061b5a48ffSTim J. Robbins 	}
7074b88c807SRodney W. Grimes 	if (set && subtype != VSPLUS) {
7084b88c807SRodney W. Grimes 		/* insert the value of the variable */
7094b88c807SRodney W. Grimes 		if (special) {
71047d8814cSJilles Tjoelker 			if (varflags & VSLINENO) {
71147d8814cSJilles Tjoelker 				if (p - var > (ptrdiff_t)sizeof(buf))
71247d8814cSJilles Tjoelker 					abort();
71347d8814cSJilles Tjoelker 				memcpy(buf, var, p - var - 1);
71447d8814cSJilles Tjoelker 				buf[p - var - 1] = '\0';
71547d8814cSJilles Tjoelker 				strtodest(buf, flag, subtype,
71647d8814cSJilles Tjoelker 				    varflags & VSQUOTE, dst);
71747d8814cSJilles Tjoelker 			} else
71847d8814cSJilles Tjoelker 				varvalue(var, varflags & VSQUOTE, subtype, flag,
71947d8814cSJilles Tjoelker 				    dst);
720aa9caaf6SPeter Wemm 			if (subtype == VSLENGTH) {
7214c244ed2SJilles Tjoelker 				varlenb = expdest - stackblock() - startloc;
7224c244ed2SJilles Tjoelker 				varlen = varlenb;
7234c244ed2SJilles Tjoelker 				if (localeisutf8) {
7244c244ed2SJilles Tjoelker 					val = stackblock() + startloc;
7254c244ed2SJilles Tjoelker 					for (;val != expdest; val++)
7264c244ed2SJilles Tjoelker 						if ((*val & 0xC0) == 0x80)
7274c244ed2SJilles Tjoelker 							varlen--;
7284c244ed2SJilles Tjoelker 				}
7294c244ed2SJilles Tjoelker 				STADJUST(-varlenb, expdest);
730aa9caaf6SPeter Wemm 			}
7314b88c807SRodney W. Grimes 		} else {
732aa9caaf6SPeter Wemm 			if (subtype == VSLENGTH) {
733aa9caaf6SPeter Wemm 				for (;*val; val++)
7344c244ed2SJilles Tjoelker 					if (!localeisutf8 ||
7354c244ed2SJilles Tjoelker 					    (*val & 0xC0) != 0x80)
736aa9caaf6SPeter Wemm 						varlen++;
737aa9caaf6SPeter Wemm 			}
738f7dea851SJilles Tjoelker 			else
7397034d8dfSJilles Tjoelker 				strtodest(val, flag, subtype,
74047d8814cSJilles Tjoelker 				    varflags & VSQUOTE, dst);
7414b88c807SRodney W. Grimes 		}
742aa9caaf6SPeter Wemm 	}
743aa9caaf6SPeter Wemm 
7444b88c807SRodney W. Grimes 	if (subtype == VSPLUS)
7454b88c807SRodney W. Grimes 		set = ! set;
746aa9caaf6SPeter Wemm 
747aa9caaf6SPeter Wemm 	switch (subtype) {
748aa9caaf6SPeter Wemm 	case VSLENGTH:
74947d8814cSJilles Tjoelker 		cvtnum(varlen, buf);
75047d8814cSJilles Tjoelker 		strtodest(buf, flag, VSNORMAL, varflags & VSQUOTE, dst);
7517034d8dfSJilles Tjoelker 		break;
752aa9caaf6SPeter Wemm 
753aa9caaf6SPeter Wemm 	case VSNORMAL:
754aa9caaf6SPeter Wemm 		break;
755aa9caaf6SPeter Wemm 
756aa9caaf6SPeter Wemm 	case VSPLUS:
757aa9caaf6SPeter Wemm 	case VSMINUS:
758aa9caaf6SPeter Wemm 		if (!set) {
7590e39c931SJilles Tjoelker 			argstr(p, flag | (flag & EXP_SPLIT ? EXP_SPLIT_LIT : 0) |
76047d8814cSJilles Tjoelker 			    (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0), dst);
761aa9caaf6SPeter Wemm 			break;
762aa9caaf6SPeter Wemm 		}
763aa9caaf6SPeter Wemm 		break;
764aa9caaf6SPeter Wemm 
765aa9caaf6SPeter Wemm 	case VSTRIMLEFT:
766aa9caaf6SPeter Wemm 	case VSTRIMLEFTMAX:
767aa9caaf6SPeter Wemm 	case VSTRIMRIGHT:
768aa9caaf6SPeter Wemm 	case VSTRIMRIGHTMAX:
769aa9caaf6SPeter Wemm 		if (!set)
770aa9caaf6SPeter Wemm 			break;
771aa9caaf6SPeter Wemm 		/*
772aa9caaf6SPeter Wemm 		 * Terminate the string and start recording the pattern
773aa9caaf6SPeter Wemm 		 * right after it
774aa9caaf6SPeter Wemm 		 */
775aa9caaf6SPeter Wemm 		STPUTC('\0', expdest);
776c4e5a8a8STor Egge 		patloc = expdest - stackblock();
77747d8814cSJilles Tjoelker 		subevalvar_trim(p, patloc, subtype, startloc);
77847d8814cSJilles Tjoelker 		reprocess(startloc, flag, VSNORMAL, varflags & VSQUOTE, dst);
7790e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT && *var == '@' && varflags & VSQUOTE)
78047d8814cSJilles Tjoelker 			dst->state = WORD_QUOTEMARK;
7817034d8dfSJilles Tjoelker 		break;
782aa9caaf6SPeter Wemm 
783aa9caaf6SPeter Wemm 	case VSASSIGN:
784aa9caaf6SPeter Wemm 	case VSQUESTION:
785aa9caaf6SPeter Wemm 		if (!set) {
7864f2f5faaSJilles Tjoelker 			if (subevalvar_misc(p, var, subtype, startloc,
7874f2f5faaSJilles Tjoelker 			    varflags)) {
788ab0a2172SSteve Price 				varflags &= ~VSNUL;
7894b88c807SRodney W. Grimes 				goto again;
790ab0a2172SSteve Price 			}
791aa9caaf6SPeter Wemm 			break;
7924b88c807SRodney W. Grimes 		}
793aa9caaf6SPeter Wemm 		break;
794aa9caaf6SPeter Wemm 
79562addaefSStefan Farfeleder 	case VSERROR:
79662addaefSStefan Farfeleder 		c = p - var - 1;
79762addaefSStefan Farfeleder 		error("${%.*s%s}: Bad substitution", c, var,
79862addaefSStefan Farfeleder 		    (c > 0 && *p != CTLENDVAR) ? "..." : "");
79962addaefSStefan Farfeleder 
800aa9caaf6SPeter Wemm 	default:
801aa9caaf6SPeter Wemm 		abort();
8024b88c807SRodney W. Grimes 	}
803aa9caaf6SPeter Wemm 
8044b88c807SRodney W. Grimes 	if (subtype != VSNORMAL) {	/* skip to end of alternative */
8054b88c807SRodney W. Grimes 		int nesting = 1;
8064b88c807SRodney W. Grimes 		for (;;) {
8074b88c807SRodney W. Grimes 			if ((c = *p++) == CTLESC)
8084b88c807SRodney W. Grimes 				p++;
8094b88c807SRodney W. Grimes 			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
8104b88c807SRodney W. Grimes 				if (set)
8114b88c807SRodney W. Grimes 					argbackq = argbackq->next;
8124b88c807SRodney W. Grimes 			} else if (c == CTLVAR) {
8134b88c807SRodney W. Grimes 				if ((*p++ & VSTYPE) != VSNORMAL)
8144b88c807SRodney W. Grimes 					nesting++;
8154b88c807SRodney W. Grimes 			} else if (c == CTLENDVAR) {
8164b88c807SRodney W. Grimes 				if (--nesting == 0)
8174b88c807SRodney W. Grimes 					break;
8184b88c807SRodney W. Grimes 			}
8194b88c807SRodney W. Grimes 		}
8204b88c807SRodney W. Grimes 	}
8214b88c807SRodney W. Grimes 	return p;
8224b88c807SRodney W. Grimes }
8234b88c807SRodney W. Grimes 
8244b88c807SRodney W. Grimes 
8254b88c807SRodney W. Grimes 
8264b88c807SRodney W. Grimes /*
8274b88c807SRodney W. Grimes  * Test whether a specialized variable is set.
8284b88c807SRodney W. Grimes  */
8294b88c807SRodney W. Grimes 
83088328642SDavid E. O'Brien static int
83161346cbdSJilles Tjoelker varisset(const char *name, int nulok)
8324b88c807SRodney W. Grimes {
8334b88c807SRodney W. Grimes 
83496522b88SSteve Price 	if (*name == '!')
835ed4c3b5fSJilles Tjoelker 		return backgndpidset();
83696522b88SSteve Price 	else if (*name == '@' || *name == '*') {
8374b88c807SRodney W. Grimes 		if (*shellparam.p == NULL)
8384b88c807SRodney W. Grimes 			return 0;
83996522b88SSteve Price 
84096522b88SSteve Price 		if (nulok) {
84196522b88SSteve Price 			char **av;
84296522b88SSteve Price 
84396522b88SSteve Price 			for (av = shellparam.p; *av; av++)
84496522b88SSteve Price 				if (**av != '\0')
84596522b88SSteve Price 					return 1;
84696522b88SSteve Price 			return 0;
84796522b88SSteve Price 		}
8485c817731SPeter Wemm 	} else if (is_digit(*name)) {
84996522b88SSteve Price 		char *ap;
8507b9104c0SJilles Tjoelker 		long num;
85196522b88SSteve Price 
8527b9104c0SJilles Tjoelker 		errno = 0;
8537b9104c0SJilles Tjoelker 		num = strtol(name, NULL, 10);
8547b9104c0SJilles Tjoelker 		if (errno != 0 || num > shellparam.nparam)
85596522b88SSteve Price 			return 0;
85696522b88SSteve Price 
85796522b88SSteve Price 		if (num == 0)
85896522b88SSteve Price 			ap = arg0;
85996522b88SSteve Price 		else
86096522b88SSteve Price 			ap = shellparam.p[num - 1];
86196522b88SSteve Price 
86296522b88SSteve Price 		if (nulok && (ap == NULL || *ap == '\0'))
8634b88c807SRodney W. Grimes 			return 0;
8644b88c807SRodney W. Grimes 	}
8654b88c807SRodney W. Grimes 	return 1;
8664b88c807SRodney W. Grimes }
8674b88c807SRodney W. Grimes 
868f7dea851SJilles Tjoelker static void
86947d8814cSJilles Tjoelker strtodest(const char *p, int flag, int subtype, int quoted,
87047d8814cSJilles Tjoelker     struct worddest *dst)
871f7dea851SJilles Tjoelker {
87247d8814cSJilles Tjoelker 	if (subtype == VSLENGTH || subtype == VSTRIMLEFT ||
87347d8814cSJilles Tjoelker 	    subtype == VSTRIMLEFTMAX || subtype == VSTRIMRIGHT ||
87447d8814cSJilles Tjoelker 	    subtype == VSTRIMRIGHTMAX)
87547d8814cSJilles Tjoelker 		STPUTS(p, expdest);
8760e39c931SJilles Tjoelker 	else if (flag & EXP_SPLIT && !quoted && dst != NULL)
8770e39c931SJilles Tjoelker 		STPUTS_SPLIT(p, BASESYNTAX, flag, expdest, dst);
8780e39c931SJilles Tjoelker 	else if (flag & (EXP_GLOB | EXP_CASE))
879f7dea851SJilles Tjoelker 		STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
880f7dea851SJilles Tjoelker 	else
881f7dea851SJilles Tjoelker 		STPUTS(p, expdest);
882f7dea851SJilles Tjoelker }
8834b88c807SRodney W. Grimes 
88447d8814cSJilles Tjoelker static void
88547d8814cSJilles Tjoelker reprocess(int startloc, int flag, int subtype, int quoted,
88647d8814cSJilles Tjoelker     struct worddest *dst)
88747d8814cSJilles Tjoelker {
88847d8814cSJilles Tjoelker 	static char *buf = NULL;
88947d8814cSJilles Tjoelker 	static size_t buflen = 0;
89047d8814cSJilles Tjoelker 	char *startp;
89147d8814cSJilles Tjoelker 	size_t len, zpos, zlen;
89247d8814cSJilles Tjoelker 
89347d8814cSJilles Tjoelker 	startp = stackblock() + startloc;
89447d8814cSJilles Tjoelker 	len = expdest - startp;
89547d8814cSJilles Tjoelker 	if (len >= SIZE_MAX / 2)
89647d8814cSJilles Tjoelker 		abort();
89747d8814cSJilles Tjoelker 	INTOFF;
89847d8814cSJilles Tjoelker 	if (len >= buflen) {
89947d8814cSJilles Tjoelker 		ckfree(buf);
90047d8814cSJilles Tjoelker 		buf = NULL;
90147d8814cSJilles Tjoelker 	}
90247d8814cSJilles Tjoelker 	if (buflen < 128)
90347d8814cSJilles Tjoelker 		buflen = 128;
90447d8814cSJilles Tjoelker 	while (len >= buflen)
90547d8814cSJilles Tjoelker 		buflen <<= 1;
90647d8814cSJilles Tjoelker 	if (buf == NULL)
90747d8814cSJilles Tjoelker 		buf = ckmalloc(buflen);
90847d8814cSJilles Tjoelker 	INTON;
90947d8814cSJilles Tjoelker 	memcpy(buf, startp, len);
91047d8814cSJilles Tjoelker 	buf[len] = '\0';
91147d8814cSJilles Tjoelker 	STADJUST(-len, expdest);
91247d8814cSJilles Tjoelker 	for (zpos = 0;;) {
91347d8814cSJilles Tjoelker 		zlen = strlen(buf + zpos);
91447d8814cSJilles Tjoelker 		strtodest(buf + zpos, flag, subtype, quoted, dst);
91547d8814cSJilles Tjoelker 		zpos += zlen + 1;
91647d8814cSJilles Tjoelker 		if (zpos == len + 1)
91747d8814cSJilles Tjoelker 			break;
9180e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT && (quoted || (zlen > 0 && zpos < len)))
9190e39c931SJilles Tjoelker 			NEXTWORD('\0', flag, expdest, dst);
92047d8814cSJilles Tjoelker 	}
92147d8814cSJilles Tjoelker }
92247d8814cSJilles Tjoelker 
9234b88c807SRodney W. Grimes /*
9244b88c807SRodney W. Grimes  * Add the value of a specialized variable to the stack string.
9254b88c807SRodney W. Grimes  */
9264b88c807SRodney W. Grimes 
92788328642SDavid E. O'Brien static void
92847d8814cSJilles Tjoelker varvalue(const char *name, int quoted, int subtype, int flag,
92947d8814cSJilles Tjoelker     struct worddest *dst)
9304b88c807SRodney W. Grimes {
9314b88c807SRodney W. Grimes 	int num;
9324b88c807SRodney W. Grimes 	char *p;
9334b88c807SRodney W. Grimes 	int i;
93447d8814cSJilles Tjoelker 	int splitlater;
9353fb51b3aSJilles Tjoelker 	char sep[2];
9364b88c807SRodney W. Grimes 	char **ap;
93747d8814cSJilles Tjoelker 	char buf[(NSHORTOPTS > 10 ? NSHORTOPTS : 10) + 1];
93847d8814cSJilles Tjoelker 
93947d8814cSJilles Tjoelker 	if (subtype == VSLENGTH)
94047d8814cSJilles Tjoelker 		flag &= ~EXP_FULL;
94147d8814cSJilles Tjoelker 	splitlater = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
94247d8814cSJilles Tjoelker 		subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX;
9434b88c807SRodney W. Grimes 
9445c817731SPeter Wemm 	switch (*name) {
9454b88c807SRodney W. Grimes 	case '$':
9464b88c807SRodney W. Grimes 		num = rootpid;
947622fdf32SJilles Tjoelker 		break;
9484b88c807SRodney W. Grimes 	case '?':
949aa9caaf6SPeter Wemm 		num = oexitstatus;
950622fdf32SJilles Tjoelker 		break;
9514b88c807SRodney W. Grimes 	case '#':
9524b88c807SRodney W. Grimes 		num = shellparam.nparam;
953622fdf32SJilles Tjoelker 		break;
9544b88c807SRodney W. Grimes 	case '!':
955ed4c3b5fSJilles Tjoelker 		num = backgndpidval();
9564b88c807SRodney W. Grimes 		break;
9574b88c807SRodney W. Grimes 	case '-':
95847d8814cSJilles Tjoelker 		p = buf;
95962c37116SJilles Tjoelker 		for (i = 0 ; i < NSHORTOPTS ; i++) {
9603da40d4aSJilles Tjoelker 			if (optval[i])
9613da40d4aSJilles Tjoelker 				*p++ = optletter[i];
9624b88c807SRodney W. Grimes 		}
96347d8814cSJilles Tjoelker 		*p = '\0';
96447d8814cSJilles Tjoelker 		strtodest(buf, flag, subtype, quoted, dst);
965622fdf32SJilles Tjoelker 		return;
9664b88c807SRodney W. Grimes 	case '@':
9670e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT && quoted) {
9684b88c807SRodney W. Grimes 			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
96947d8814cSJilles Tjoelker 				strtodest(p, flag, subtype, quoted, dst);
97047d8814cSJilles Tjoelker 				if (*ap) {
97147d8814cSJilles Tjoelker 					if (splitlater)
9726f47734fSTor Egge 						STPUTC('\0', expdest);
97347d8814cSJilles Tjoelker 					else
9740e39c931SJilles Tjoelker 						NEXTWORD('\0', flag, expdest,
9750e39c931SJilles Tjoelker 						    dst);
9766f47734fSTor Egge 				}
97747d8814cSJilles Tjoelker 			}
97847d8814cSJilles Tjoelker 			if (shellparam.nparam > 0)
97947d8814cSJilles Tjoelker 				dst->state = WORD_QUOTEMARK;
980622fdf32SJilles Tjoelker 			return;
9816f47734fSTor Egge 		}
9820d9f1a69SPhilippe Charnier 		/* FALLTHROUGH */
9836f47734fSTor Egge 	case '*':
984f7d95a07SRalf S. Engelschall 		if (ifsset())
9853fb51b3aSJilles Tjoelker 			sep[0] = ifsval()[0];
9866f47734fSTor Egge 		else
9873fb51b3aSJilles Tjoelker 			sep[0] = ' ';
9883fb51b3aSJilles Tjoelker 		sep[1] = '\0';
9896f47734fSTor Egge 		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
99047d8814cSJilles Tjoelker 			strtodest(p, flag, subtype, quoted, dst);
991715a0dd5SJilles Tjoelker 			if (!*ap)
992715a0dd5SJilles Tjoelker 				break;
9933fb51b3aSJilles Tjoelker 			if (sep[0])
99447d8814cSJilles Tjoelker 				strtodest(sep, flag, subtype, quoted, dst);
9950e39c931SJilles Tjoelker 			else if (flag & EXP_SPLIT && !quoted && **ap != '\0') {
99647d8814cSJilles Tjoelker 				if (splitlater)
9973fb51b3aSJilles Tjoelker 					STPUTC('\0', expdest);
99847d8814cSJilles Tjoelker 				else
9990e39c931SJilles Tjoelker 					NEXTWORD('\0', flag, expdest, dst);
100047d8814cSJilles Tjoelker 			}
10014b88c807SRodney W. Grimes 		}
1002622fdf32SJilles Tjoelker 		return;
10034b88c807SRodney W. Grimes 	default:
10045c817731SPeter Wemm 		if (is_digit(*name)) {
10055c817731SPeter Wemm 			num = atoi(name);
10065ddabb83SJilles Tjoelker 			if (num == 0)
10075ddabb83SJilles Tjoelker 				p = arg0;
10085ddabb83SJilles Tjoelker 			else if (num > 0 && num <= shellparam.nparam)
10095c817731SPeter Wemm 				p = shellparam.p[num - 1];
10105ddabb83SJilles Tjoelker 			else
1011622fdf32SJilles Tjoelker 				return;
101247d8814cSJilles Tjoelker 			strtodest(p, flag, subtype, quoted, dst);
10134b88c807SRodney W. Grimes 		}
1014622fdf32SJilles Tjoelker 		return;
10154b88c807SRodney W. Grimes 	}
101647d8814cSJilles Tjoelker 	cvtnum(num, buf);
101747d8814cSJilles Tjoelker 	strtodest(buf, flag, subtype, quoted, dst);
10184b88c807SRodney W. Grimes }
10194b88c807SRodney W. Grimes 
10204b88c807SRodney W. Grimes 
10214b88c807SRodney W. Grimes 
1022aa7b6f82SDavid E. O'Brien static char expdir[PATH_MAX];
1023c8a3d81fSJilles Tjoelker #define expdir_end (expdir + sizeof(expdir))
10244b88c807SRodney W. Grimes 
10252ca3d70fSJilles Tjoelker /*
10262ca3d70fSJilles Tjoelker  * Perform pathname generation and remove control characters.
1027bc57b4d4SJilles Tjoelker  * At this point, the only control characters should be CTLESC.
10288ef0ae8aSJilles Tjoelker  * The results are stored in the list dstlist.
10292ca3d70fSJilles Tjoelker  */
103088328642SDavid E. O'Brien static void
10310e39c931SJilles Tjoelker expandmeta(char *pattern, struct arglist *dstlist)
10324b88c807SRodney W. Grimes {
10334b88c807SRodney W. Grimes 	char *p;
10348ef0ae8aSJilles Tjoelker 	int firstmatch;
10354b88c807SRodney W. Grimes 	char c;
10364b88c807SRodney W. Grimes 
10378ef0ae8aSJilles Tjoelker 	firstmatch = dstlist->count;
10380e39c931SJilles Tjoelker 	p = pattern;
1039622fdf32SJilles Tjoelker 	for (; (c = *p) != '\0'; p++) {
1040622fdf32SJilles Tjoelker 		/* fast check for meta chars */
1041622fdf32SJilles Tjoelker 		if (c == '*' || c == '?' || c == '[') {
10424b88c807SRodney W. Grimes 			INTOFF;
10430e39c931SJilles Tjoelker 			expmeta(expdir, pattern, dstlist);
10444b88c807SRodney W. Grimes 			INTON;
1045622fdf32SJilles Tjoelker 			break;
1046622fdf32SJilles Tjoelker 		}
1047622fdf32SJilles Tjoelker 	}
10488ef0ae8aSJilles Tjoelker 	if (dstlist->count == firstmatch) {
10494b88c807SRodney W. Grimes 		/*
10504b88c807SRodney W. Grimes 		 * no matches
10514b88c807SRodney W. Grimes 		 */
10520e39c931SJilles Tjoelker 		rmescapes(pattern);
10530e39c931SJilles Tjoelker 		appendarglist(dstlist, pattern);
10544b88c807SRodney W. Grimes 	} else {
10558ef0ae8aSJilles Tjoelker 		qsort(&dstlist->args[firstmatch],
10568ef0ae8aSJilles Tjoelker 		    dstlist->count - firstmatch,
10578ef0ae8aSJilles Tjoelker 		    sizeof(dstlist->args[0]), expsortcmp);
10584b88c807SRodney W. Grimes 	}
10594b88c807SRodney W. Grimes }
10604b88c807SRodney W. Grimes 
10614b88c807SRodney W. Grimes 
10624b88c807SRodney W. Grimes /*
10634b88c807SRodney W. Grimes  * Do metacharacter (i.e. *, ?, [...]) expansion.
10644b88c807SRodney W. Grimes  */
10654b88c807SRodney W. Grimes 
106688328642SDavid E. O'Brien static void
10678ef0ae8aSJilles Tjoelker expmeta(char *enddir, char *name, struct arglist *arglist)
10684b88c807SRodney W. Grimes {
106946c6b52dSJilles Tjoelker 	const char *p;
107046c6b52dSJilles Tjoelker 	const char *q;
107146c6b52dSJilles Tjoelker 	const char *start;
10724b88c807SRodney W. Grimes 	char *endname;
10734b88c807SRodney W. Grimes 	int metaflag;
10744b88c807SRodney W. Grimes 	struct stat statb;
10754b88c807SRodney W. Grimes 	DIR *dirp;
10764b88c807SRodney W. Grimes 	struct dirent *dp;
10774b88c807SRodney W. Grimes 	int atend;
10784b88c807SRodney W. Grimes 	int matchdot;
10794710b07eSJilles Tjoelker 	int esc;
10807a2b9d4bSJilles Tjoelker 	int namlen;
10814b88c807SRodney W. Grimes 
10824b88c807SRodney W. Grimes 	metaflag = 0;
10834b88c807SRodney W. Grimes 	start = name;
10844710b07eSJilles Tjoelker 	for (p = name; esc = 0, *p; p += esc + 1) {
10854b88c807SRodney W. Grimes 		if (*p == '*' || *p == '?')
10864b88c807SRodney W. Grimes 			metaflag = 1;
10874b88c807SRodney W. Grimes 		else if (*p == '[') {
10884b88c807SRodney W. Grimes 			q = p + 1;
1089ea1376dfSAndrey A. Chernov 			if (*q == '!' || *q == '^')
10904b88c807SRodney W. Grimes 				q++;
10914b88c807SRodney W. Grimes 			for (;;) {
10924b88c807SRodney W. Grimes 				if (*q == CTLESC)
10934b88c807SRodney W. Grimes 					q++;
10944b88c807SRodney W. Grimes 				if (*q == '/' || *q == '\0')
10954b88c807SRodney W. Grimes 					break;
10964b88c807SRodney W. Grimes 				if (*++q == ']') {
10974b88c807SRodney W. Grimes 					metaflag = 1;
10984b88c807SRodney W. Grimes 					break;
10994b88c807SRodney W. Grimes 				}
11004b88c807SRodney W. Grimes 			}
11014b88c807SRodney W. Grimes 		} else if (*p == '\0')
11024b88c807SRodney W. Grimes 			break;
11034710b07eSJilles Tjoelker 		else {
11044710b07eSJilles Tjoelker 			if (*p == CTLESC)
11054710b07eSJilles Tjoelker 				esc++;
11064710b07eSJilles Tjoelker 			if (p[esc] == '/') {
11074b88c807SRodney W. Grimes 				if (metaflag)
11084b88c807SRodney W. Grimes 					break;
11094710b07eSJilles Tjoelker 				start = p + esc + 1;
11104710b07eSJilles Tjoelker 			}
11114b88c807SRodney W. Grimes 		}
11124b88c807SRodney W. Grimes 	}
11134b88c807SRodney W. Grimes 	if (metaflag == 0) {	/* we've reached the end of the file name */
11144b88c807SRodney W. Grimes 		if (enddir != expdir)
11154b88c807SRodney W. Grimes 			metaflag++;
11164b88c807SRodney W. Grimes 		for (p = name ; ; p++) {
11174b88c807SRodney W. Grimes 			if (*p == CTLESC)
11184b88c807SRodney W. Grimes 				p++;
11194b88c807SRodney W. Grimes 			*enddir++ = *p;
11204b88c807SRodney W. Grimes 			if (*p == '\0')
11214b88c807SRodney W. Grimes 				break;
1122c8a3d81fSJilles Tjoelker 			if (enddir == expdir_end)
1123c8a3d81fSJilles Tjoelker 				return;
11244b88c807SRodney W. Grimes 		}
11250e3e87bdSXin LI 		if (metaflag == 0 || lstat(expdir, &statb) >= 0)
11268ef0ae8aSJilles Tjoelker 			appendarglist(arglist, stsavestr(expdir));
11274b88c807SRodney W. Grimes 		return;
11284b88c807SRodney W. Grimes 	}
112946c6b52dSJilles Tjoelker 	endname = name + (p - name);
11304b88c807SRodney W. Grimes 	if (start != name) {
11314b88c807SRodney W. Grimes 		p = name;
11324b88c807SRodney W. Grimes 		while (p < start) {
11334b88c807SRodney W. Grimes 			if (*p == CTLESC)
11344b88c807SRodney W. Grimes 				p++;
11354b88c807SRodney W. Grimes 			*enddir++ = *p++;
1136c8a3d81fSJilles Tjoelker 			if (enddir == expdir_end)
1137c8a3d81fSJilles Tjoelker 				return;
11384b88c807SRodney W. Grimes 		}
11394b88c807SRodney W. Grimes 	}
11404b88c807SRodney W. Grimes 	if (enddir == expdir) {
11414b88c807SRodney W. Grimes 		p = ".";
11424b88c807SRodney W. Grimes 	} else if (enddir == expdir + 1 && *expdir == '/') {
11434b88c807SRodney W. Grimes 		p = "/";
11444b88c807SRodney W. Grimes 	} else {
11454b88c807SRodney W. Grimes 		p = expdir;
11464b88c807SRodney W. Grimes 		enddir[-1] = '\0';
11474b88c807SRodney W. Grimes 	}
11484b88c807SRodney W. Grimes 	if ((dirp = opendir(p)) == NULL)
11494b88c807SRodney W. Grimes 		return;
11504b88c807SRodney W. Grimes 	if (enddir != expdir)
11514b88c807SRodney W. Grimes 		enddir[-1] = '/';
11524b88c807SRodney W. Grimes 	if (*endname == 0) {
11534b88c807SRodney W. Grimes 		atend = 1;
11544b88c807SRodney W. Grimes 	} else {
11554b88c807SRodney W. Grimes 		atend = 0;
11564710b07eSJilles Tjoelker 		*endname = '\0';
11574710b07eSJilles Tjoelker 		endname += esc + 1;
11584b88c807SRodney W. Grimes 	}
11594b88c807SRodney W. Grimes 	matchdot = 0;
11606f47734fSTor Egge 	p = start;
11616f47734fSTor Egge 	if (*p == CTLESC)
11626f47734fSTor Egge 		p++;
11636f47734fSTor Egge 	if (*p == '.')
11644b88c807SRodney W. Grimes 		matchdot++;
11654b88c807SRodney W. Grimes 	while (! int_pending() && (dp = readdir(dirp)) != NULL) {
11664b88c807SRodney W. Grimes 		if (dp->d_name[0] == '.' && ! matchdot)
11674b88c807SRodney W. Grimes 			continue;
116847d8814cSJilles Tjoelker 		if (patmatch(start, dp->d_name)) {
11697a2b9d4bSJilles Tjoelker 			namlen = dp->d_namlen;
11707a2b9d4bSJilles Tjoelker 			if (enddir + namlen + 1 > expdir_end)
1171aa9caaf6SPeter Wemm 				continue;
11727a2b9d4bSJilles Tjoelker 			memcpy(enddir, dp->d_name, namlen + 1);
1173c8a3d81fSJilles Tjoelker 			if (atend)
11748ef0ae8aSJilles Tjoelker 				appendarglist(arglist, stsavestr(expdir));
1175c8a3d81fSJilles Tjoelker 			else {
11766e8db49aSJilles Tjoelker 				if (dp->d_type != DT_UNKNOWN &&
11776e8db49aSJilles Tjoelker 				    dp->d_type != DT_DIR &&
11786e8db49aSJilles Tjoelker 				    dp->d_type != DT_LNK)
11796e8db49aSJilles Tjoelker 					continue;
11807a2b9d4bSJilles Tjoelker 				if (enddir + namlen + 2 > expdir_end)
1181c8a3d81fSJilles Tjoelker 					continue;
11827a2b9d4bSJilles Tjoelker 				enddir[namlen] = '/';
11837a2b9d4bSJilles Tjoelker 				enddir[namlen + 1] = '\0';
11848ef0ae8aSJilles Tjoelker 				expmeta(enddir + namlen + 1, endname, arglist);
11854b88c807SRodney W. Grimes 			}
11864b88c807SRodney W. Grimes 		}
11874b88c807SRodney W. Grimes 	}
11884b88c807SRodney W. Grimes 	closedir(dirp);
11894b88c807SRodney W. Grimes 	if (! atend)
11904710b07eSJilles Tjoelker 		endname[-esc - 1] = esc ? CTLESC : '/';
11914b88c807SRodney W. Grimes }
11924b88c807SRodney W. Grimes 
11934b88c807SRodney W. Grimes 
11948ef0ae8aSJilles Tjoelker static int
11958ef0ae8aSJilles Tjoelker expsortcmp(const void *p1, const void *p2)
11964b88c807SRodney W. Grimes {
11978ef0ae8aSJilles Tjoelker 	const char *s1 = *(const char * const *)p1;
11988ef0ae8aSJilles Tjoelker 	const char *s2 = *(const char * const *)p2;
11994b88c807SRodney W. Grimes 
1200143d321aSAndrey A. Chernov 	return (strcoll(s1, s2));
12014b88c807SRodney W. Grimes }
12024b88c807SRodney W. Grimes 
12034b88c807SRodney W. Grimes 
12044b88c807SRodney W. Grimes 
12057cc6b3dfSJilles Tjoelker static wchar_t
12067cc6b3dfSJilles Tjoelker get_wc(const char **p)
12077cc6b3dfSJilles Tjoelker {
12087cc6b3dfSJilles Tjoelker 	wchar_t c;
12097cc6b3dfSJilles Tjoelker 	int chrlen;
12107cc6b3dfSJilles Tjoelker 
12117cc6b3dfSJilles Tjoelker 	chrlen = mbtowc(&c, *p, 4);
12127cc6b3dfSJilles Tjoelker 	if (chrlen == 0)
12137cc6b3dfSJilles Tjoelker 		return 0;
12147cc6b3dfSJilles Tjoelker 	else if (chrlen == -1)
12157cc6b3dfSJilles Tjoelker 		c = 0;
12167cc6b3dfSJilles Tjoelker 	else
12177cc6b3dfSJilles Tjoelker 		*p += chrlen;
12187cc6b3dfSJilles Tjoelker 	return c;
12197cc6b3dfSJilles Tjoelker }
12207cc6b3dfSJilles Tjoelker 
12217cc6b3dfSJilles Tjoelker 
12224b88c807SRodney W. Grimes /*
1223ff4dc672SJilles Tjoelker  * See if a character matches a character class, starting at the first colon
1224ff4dc672SJilles Tjoelker  * of "[:class:]".
1225ff4dc672SJilles Tjoelker  * If a valid character class is recognized, a pointer to the next character
1226ff4dc672SJilles Tjoelker  * after the final closing bracket is stored into *end, otherwise a null
1227ff4dc672SJilles Tjoelker  * pointer is stored into *end.
1228ff4dc672SJilles Tjoelker  */
1229ff4dc672SJilles Tjoelker static int
1230ff4dc672SJilles Tjoelker match_charclass(const char *p, wchar_t chr, const char **end)
1231ff4dc672SJilles Tjoelker {
1232ff4dc672SJilles Tjoelker 	char name[20];
1233ff4dc672SJilles Tjoelker 	const char *nameend;
1234ff4dc672SJilles Tjoelker 	wctype_t cclass;
1235ff4dc672SJilles Tjoelker 
1236ff4dc672SJilles Tjoelker 	*end = NULL;
1237ff4dc672SJilles Tjoelker 	p++;
1238ff4dc672SJilles Tjoelker 	nameend = strstr(p, ":]");
123946c6b52dSJilles Tjoelker 	if (nameend == NULL || (size_t)(nameend - p) >= sizeof(name) ||
124046c6b52dSJilles Tjoelker 	    nameend == p)
1241ff4dc672SJilles Tjoelker 		return 0;
1242ff4dc672SJilles Tjoelker 	memcpy(name, p, nameend - p);
1243ff4dc672SJilles Tjoelker 	name[nameend - p] = '\0';
1244ff4dc672SJilles Tjoelker 	*end = nameend + 2;
1245ff4dc672SJilles Tjoelker 	cclass = wctype(name);
1246ff4dc672SJilles Tjoelker 	/* An unknown class matches nothing but is valid nevertheless. */
1247ff4dc672SJilles Tjoelker 	if (cclass == 0)
1248ff4dc672SJilles Tjoelker 		return 0;
1249ff4dc672SJilles Tjoelker 	return iswctype(chr, cclass);
1250ff4dc672SJilles Tjoelker }
1251ff4dc672SJilles Tjoelker 
1252ff4dc672SJilles Tjoelker 
1253ff4dc672SJilles Tjoelker /*
12544b88c807SRodney W. Grimes  * Returns true if the pattern matches the string.
12554b88c807SRodney W. Grimes  */
12564b88c807SRodney W. Grimes 
1257260fc3f4SJilles Tjoelker static int
125847d8814cSJilles Tjoelker patmatch(const char *pattern, const char *string)
12594b88c807SRodney W. Grimes {
1260ff4dc672SJilles Tjoelker 	const char *p, *q, *end;
1261820491f8SJilles Tjoelker 	const char *bt_p, *bt_q;
126296522b88SSteve Price 	char c;
12637cc6b3dfSJilles Tjoelker 	wchar_t wc, wc2;
12644b88c807SRodney W. Grimes 
12654b88c807SRodney W. Grimes 	p = pattern;
12664b88c807SRodney W. Grimes 	q = string;
1267820491f8SJilles Tjoelker 	bt_p = NULL;
1268820491f8SJilles Tjoelker 	bt_q = NULL;
12694b88c807SRodney W. Grimes 	for (;;) {
12704b88c807SRodney W. Grimes 		switch (c = *p++) {
12714b88c807SRodney W. Grimes 		case '\0':
1272820491f8SJilles Tjoelker 			if (*q != '\0')
1273820491f8SJilles Tjoelker 				goto backtrack;
1274820491f8SJilles Tjoelker 			return 1;
12754b88c807SRodney W. Grimes 		case CTLESC:
12764b88c807SRodney W. Grimes 			if (*q++ != *p++)
1277820491f8SJilles Tjoelker 				goto backtrack;
12784b88c807SRodney W. Grimes 			break;
12794b88c807SRodney W. Grimes 		case '?':
1280820491f8SJilles Tjoelker 			if (*q == '\0')
12814b88c807SRodney W. Grimes 				return 0;
1282820491f8SJilles Tjoelker 			if (localeisutf8) {
1283820491f8SJilles Tjoelker 				wc = get_wc(&q);
1284820491f8SJilles Tjoelker 				/*
1285820491f8SJilles Tjoelker 				 * A '?' does not match invalid UTF-8 but a
1286820491f8SJilles Tjoelker 				 * '*' does, so backtrack.
1287820491f8SJilles Tjoelker 				 */
1288820491f8SJilles Tjoelker 				if (wc == 0)
1289820491f8SJilles Tjoelker 					goto backtrack;
1290820491f8SJilles Tjoelker 			} else
1291acb4eadaSJilles Tjoelker 				q++;
12924b88c807SRodney W. Grimes 			break;
12934b88c807SRodney W. Grimes 		case '*':
12944b88c807SRodney W. Grimes 			c = *p;
1295bc57b4d4SJilles Tjoelker 			while (c == '*')
12966f47734fSTor Egge 				c = *++p;
1297820491f8SJilles Tjoelker 			/*
1298820491f8SJilles Tjoelker 			 * If the pattern ends here, we know the string
1299820491f8SJilles Tjoelker 			 * matches without needing to look at the rest of it.
1300820491f8SJilles Tjoelker 			 */
1301820491f8SJilles Tjoelker 			if (c == '\0')
13024b88c807SRodney W. Grimes 				return 1;
1303820491f8SJilles Tjoelker 			/*
1304820491f8SJilles Tjoelker 			 * First try the shortest match for the '*' that
1305820491f8SJilles Tjoelker 			 * could work. We can forget any earlier '*' since
1306820491f8SJilles Tjoelker 			 * there is no way having it match more characters
1307820491f8SJilles Tjoelker 			 * can help us, given that we are already here.
1308820491f8SJilles Tjoelker 			 */
1309820491f8SJilles Tjoelker 			bt_p = p;
1310820491f8SJilles Tjoelker 			bt_q = q;
1311820491f8SJilles Tjoelker 			break;
13124b88c807SRodney W. Grimes 		case '[': {
13134a4867d6SJilles Tjoelker 			const char *savep, *saveq;
13144b88c807SRodney W. Grimes 			int invert, found;
13157cc6b3dfSJilles Tjoelker 			wchar_t chr;
13164b88c807SRodney W. Grimes 
13174a4867d6SJilles Tjoelker 			savep = p, saveq = q;
13184b88c807SRodney W. Grimes 			invert = 0;
1319ea1376dfSAndrey A. Chernov 			if (*p == '!' || *p == '^') {
13204b88c807SRodney W. Grimes 				invert++;
13214b88c807SRodney W. Grimes 				p++;
13224b88c807SRodney W. Grimes 			}
13234b88c807SRodney W. Grimes 			found = 0;
1324820491f8SJilles Tjoelker 			if (*q == '\0')
1325aa9caaf6SPeter Wemm 				return 0;
1326820491f8SJilles Tjoelker 			if (localeisutf8) {
1327820491f8SJilles Tjoelker 				chr = get_wc(&q);
1328820491f8SJilles Tjoelker 				if (chr == 0)
1329820491f8SJilles Tjoelker 					goto backtrack;
1330820491f8SJilles Tjoelker 			} else
1331820491f8SJilles Tjoelker 				chr = (unsigned char)*q++;
13324b88c807SRodney W. Grimes 			c = *p++;
13334b88c807SRodney W. Grimes 			do {
13344a4867d6SJilles Tjoelker 				if (c == '\0') {
13354a4867d6SJilles Tjoelker 					p = savep, q = saveq;
13364a4867d6SJilles Tjoelker 					c = '[';
13374a4867d6SJilles Tjoelker 					goto dft;
13384a4867d6SJilles Tjoelker 				}
1339ff4dc672SJilles Tjoelker 				if (c == '[' && *p == ':') {
1340ff4dc672SJilles Tjoelker 					found |= match_charclass(p, chr, &end);
1341ff4dc672SJilles Tjoelker 					if (end != NULL)
1342ff4dc672SJilles Tjoelker 						p = end;
1343ff4dc672SJilles Tjoelker 				}
13444b88c807SRodney W. Grimes 				if (c == CTLESC)
13454b88c807SRodney W. Grimes 					c = *p++;
13467cc6b3dfSJilles Tjoelker 				if (localeisutf8 && c & 0x80) {
13477cc6b3dfSJilles Tjoelker 					p--;
13487cc6b3dfSJilles Tjoelker 					wc = get_wc(&p);
13497cc6b3dfSJilles Tjoelker 					if (wc == 0) /* bad utf-8 */
13507cc6b3dfSJilles Tjoelker 						return 0;
13517cc6b3dfSJilles Tjoelker 				} else
1352f5ac5937SJilles Tjoelker 					wc = (unsigned char)c;
13534b88c807SRodney W. Grimes 				if (*p == '-' && p[1] != ']') {
13544b88c807SRodney W. Grimes 					p++;
13554b88c807SRodney W. Grimes 					if (*p == CTLESC)
13564b88c807SRodney W. Grimes 						p++;
13577cc6b3dfSJilles Tjoelker 					if (localeisutf8) {
13587cc6b3dfSJilles Tjoelker 						wc2 = get_wc(&p);
13597cc6b3dfSJilles Tjoelker 						if (wc2 == 0) /* bad utf-8 */
13607cc6b3dfSJilles Tjoelker 							return 0;
13617cc6b3dfSJilles Tjoelker 					} else
1362f5ac5937SJilles Tjoelker 						wc2 = (unsigned char)*p++;
1363fa93fc65SAndrey A. Chernov 					if (   collate_range_cmp(chr, wc) >= 0
1364fa93fc65SAndrey A. Chernov 					    && collate_range_cmp(chr, wc2) <= 0
1365fa93fc65SAndrey A. Chernov 					   )
13664b88c807SRodney W. Grimes 						found = 1;
13674b88c807SRodney W. Grimes 				} else {
13687cc6b3dfSJilles Tjoelker 					if (chr == wc)
13694b88c807SRodney W. Grimes 						found = 1;
13704b88c807SRodney W. Grimes 				}
13714b88c807SRodney W. Grimes 			} while ((c = *p++) != ']');
13724b88c807SRodney W. Grimes 			if (found == invert)
1373820491f8SJilles Tjoelker 				goto backtrack;
13744b88c807SRodney W. Grimes 			break;
13754b88c807SRodney W. Grimes 		}
13764b88c807SRodney W. Grimes dft:	        default:
1377820491f8SJilles Tjoelker 			if (*q == '\0')
13784b88c807SRodney W. Grimes 				return 0;
1379820491f8SJilles Tjoelker 			if (*q++ == c)
1380820491f8SJilles Tjoelker 				break;
1381820491f8SJilles Tjoelker backtrack:
1382820491f8SJilles Tjoelker 			/*
1383820491f8SJilles Tjoelker 			 * If we have a mismatch (other than hitting the end
1384820491f8SJilles Tjoelker 			 * of the string), go back to the last '*' seen and
1385820491f8SJilles Tjoelker 			 * have it match one additional character.
1386820491f8SJilles Tjoelker 			 */
1387820491f8SJilles Tjoelker 			if (bt_p == NULL)
1388820491f8SJilles Tjoelker 				return 0;
1389820491f8SJilles Tjoelker 			if (*bt_q == '\0')
1390820491f8SJilles Tjoelker 				return 0;
1391820491f8SJilles Tjoelker 			bt_q++;
1392820491f8SJilles Tjoelker 			p = bt_p;
1393820491f8SJilles Tjoelker 			q = bt_q;
13944b88c807SRodney W. Grimes 			break;
13954b88c807SRodney W. Grimes 		}
13964b88c807SRodney W. Grimes 	}
13974b88c807SRodney W. Grimes }
13984b88c807SRodney W. Grimes 
13994b88c807SRodney W. Grimes 
14004b88c807SRodney W. Grimes 
14014b88c807SRodney W. Grimes /*
14022ca3d70fSJilles Tjoelker  * Remove any CTLESC and CTLQUOTEMARK characters from a string.
14034b88c807SRodney W. Grimes  */
14044b88c807SRodney W. Grimes 
14054b88c807SRodney W. Grimes void
14065134c3f7SWarner Losh rmescapes(char *str)
14074b88c807SRodney W. Grimes {
140896522b88SSteve Price 	char *p, *q;
14094b88c807SRodney W. Grimes 
14104b88c807SRodney W. Grimes 	p = str;
1411048f2667SJilles Tjoelker 	while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) {
14124b88c807SRodney W. Grimes 		if (*p++ == '\0')
14134b88c807SRodney W. Grimes 			return;
14144b88c807SRodney W. Grimes 	}
14154b88c807SRodney W. Grimes 	q = p;
14164b88c807SRodney W. Grimes 	while (*p) {
1417048f2667SJilles Tjoelker 		if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
14186f47734fSTor Egge 			p++;
14196f47734fSTor Egge 			continue;
14206f47734fSTor Egge 		}
14214b88c807SRodney W. Grimes 		if (*p == CTLESC)
14224b88c807SRodney W. Grimes 			p++;
14234b88c807SRodney W. Grimes 		*q++ = *p++;
14244b88c807SRodney W. Grimes 	}
14254b88c807SRodney W. Grimes 	*q = '\0';
14264b88c807SRodney W. Grimes }
14274b88c807SRodney W. Grimes 
14284b88c807SRodney W. Grimes 
14294b88c807SRodney W. Grimes 
14304b88c807SRodney W. Grimes /*
14314b88c807SRodney W. Grimes  * See if a pattern matches in a case statement.
14324b88c807SRodney W. Grimes  */
14334b88c807SRodney W. Grimes 
14344b88c807SRodney W. Grimes int
14352cac6e36SJilles Tjoelker casematch(union node *pattern, const char *val)
14364b88c807SRodney W. Grimes {
14374b88c807SRodney W. Grimes 	struct stackmark smark;
14384b88c807SRodney W. Grimes 	int result;
14394b88c807SRodney W. Grimes 	char *p;
14404b88c807SRodney W. Grimes 
14414b88c807SRodney W. Grimes 	setstackmark(&smark);
14424b88c807SRodney W. Grimes 	argbackq = pattern->narg.backquote;
14434b88c807SRodney W. Grimes 	STARTSTACKSTR(expdest);
144447d8814cSJilles Tjoelker 	argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, NULL);
14454b88c807SRodney W. Grimes 	STPUTC('\0', expdest);
14464b88c807SRodney W. Grimes 	p = grabstackstr(expdest);
144747d8814cSJilles Tjoelker 	result = patmatch(p, val);
14484b88c807SRodney W. Grimes 	popstackmark(&smark);
14494b88c807SRodney W. Grimes 	return result;
14504b88c807SRodney W. Grimes }
1451aa9caaf6SPeter Wemm 
1452aa9caaf6SPeter Wemm /*
1453aa9caaf6SPeter Wemm  * Our own itoa().
1454aa9caaf6SPeter Wemm  */
1455aa9caaf6SPeter Wemm 
145647d8814cSJilles Tjoelker static void
14575134c3f7SWarner Losh cvtnum(int num, char *buf)
1458aa9caaf6SPeter Wemm {
1459aa9caaf6SPeter Wemm 	char temp[32];
1460aa9caaf6SPeter Wemm 	int neg = num < 0;
1461aa9caaf6SPeter Wemm 	char *p = temp + 31;
1462aa9caaf6SPeter Wemm 
1463aa9caaf6SPeter Wemm 	temp[31] = '\0';
1464aa9caaf6SPeter Wemm 
1465aa9caaf6SPeter Wemm 	do {
1466aa9caaf6SPeter Wemm 		*--p = num % 10 + '0';
1467aa9caaf6SPeter Wemm 	} while ((num /= 10) != 0);
1468aa9caaf6SPeter Wemm 
1469aa9caaf6SPeter Wemm 	if (neg)
1470aa9caaf6SPeter Wemm 		*--p = '-';
1471aa9caaf6SPeter Wemm 
147247d8814cSJilles Tjoelker 	memcpy(buf, p, temp + 32 - p);
1473aa9caaf6SPeter Wemm }
14742c25061fSTim J. Robbins 
14752c25061fSTim J. Robbins /*
14762c25061fSTim J. Robbins  * Do most of the work for wordexp(3).
14772c25061fSTim J. Robbins  */
14782c25061fSTim J. Robbins 
14792c25061fSTim J. Robbins int
14802c25061fSTim J. Robbins wordexpcmd(int argc, char **argv)
14812c25061fSTim J. Robbins {
14822c25061fSTim J. Robbins 	size_t len;
14832c25061fSTim J. Robbins 	int i;
14842c25061fSTim J. Robbins 
14852c25061fSTim J. Robbins 	out1fmt("%08x", argc - 1);
14862c25061fSTim J. Robbins 	for (i = 1, len = 0; i < argc; i++)
14872c25061fSTim J. Robbins 		len += strlen(argv[i]);
14882c25061fSTim J. Robbins 	out1fmt("%08x", (int)len);
1489aeb5d065SJilles Tjoelker 	for (i = 1; i < argc; i++)
1490aeb5d065SJilles Tjoelker 		outbin(argv[i], strlen(argv[i]) + 1, out1);
14912c25061fSTim J. Robbins         return (0);
14922c25061fSTim J. Robbins }
1493d358fa78SJilles Tjoelker 
1494d358fa78SJilles Tjoelker /*
1495d358fa78SJilles Tjoelker  * Do most of the work for wordexp(3), new version.
1496d358fa78SJilles Tjoelker  */
1497d358fa78SJilles Tjoelker 
1498d358fa78SJilles Tjoelker int
1499d358fa78SJilles Tjoelker freebsd_wordexpcmd(int argc __unused, char **argv __unused)
1500d358fa78SJilles Tjoelker {
1501d358fa78SJilles Tjoelker 	struct arglist arglist;
1502d358fa78SJilles Tjoelker 	union node *args, *n;
15038ef0ae8aSJilles Tjoelker 	size_t len;
1504d358fa78SJilles Tjoelker 	int ch;
1505d358fa78SJilles Tjoelker 	int protected = 0;
1506d358fa78SJilles Tjoelker 	int fd = -1;
15078ef0ae8aSJilles Tjoelker 	int i;
1508d358fa78SJilles Tjoelker 
1509d358fa78SJilles Tjoelker 	while ((ch = nextopt("f:p")) != '\0') {
1510d358fa78SJilles Tjoelker 		switch (ch) {
1511d358fa78SJilles Tjoelker 		case 'f':
1512d358fa78SJilles Tjoelker 			fd = number(shoptarg);
1513d358fa78SJilles Tjoelker 			break;
1514d358fa78SJilles Tjoelker 		case 'p':
1515d358fa78SJilles Tjoelker 			protected = 1;
1516d358fa78SJilles Tjoelker 			break;
1517d358fa78SJilles Tjoelker 		}
1518d358fa78SJilles Tjoelker 	}
1519d358fa78SJilles Tjoelker 	if (*argptr != NULL)
1520d358fa78SJilles Tjoelker 		error("wrong number of arguments");
1521d358fa78SJilles Tjoelker 	if (fd < 0)
1522d358fa78SJilles Tjoelker 		error("missing fd");
1523d358fa78SJilles Tjoelker 	INTOFF;
1524d358fa78SJilles Tjoelker 	setinputfd(fd, 1);
1525d358fa78SJilles Tjoelker 	INTON;
1526d358fa78SJilles Tjoelker 	args = parsewordexp();
1527d358fa78SJilles Tjoelker 	popfile(); /* will also close fd */
1528d358fa78SJilles Tjoelker 	if (protected)
1529d358fa78SJilles Tjoelker 		for (n = args; n != NULL; n = n->narg.next) {
1530d358fa78SJilles Tjoelker 			if (n->narg.backquote != NULL) {
1531d358fa78SJilles Tjoelker 				outcslow('C', out1);
1532d358fa78SJilles Tjoelker 				error("command substitution disabled");
1533d358fa78SJilles Tjoelker 			}
1534d358fa78SJilles Tjoelker 		}
1535d358fa78SJilles Tjoelker 	outcslow(' ', out1);
15368ef0ae8aSJilles Tjoelker 	emptyarglist(&arglist);
1537d358fa78SJilles Tjoelker 	for (n = args; n != NULL; n = n->narg.next)
1538d358fa78SJilles Tjoelker 		expandarg(n, &arglist, EXP_FULL | EXP_TILDE);
15398ef0ae8aSJilles Tjoelker 	for (i = 0, len = 0; i < arglist.count; i++)
15408ef0ae8aSJilles Tjoelker 		len += strlen(arglist.args[i]);
15418ef0ae8aSJilles Tjoelker 	out1fmt("%016x %016zx", arglist.count, len);
15428ef0ae8aSJilles Tjoelker 	for (i = 0; i < arglist.count; i++)
15438ef0ae8aSJilles Tjoelker 		outbin(arglist.args[i], strlen(arglist.args[i]) + 1, out1);
1544d358fa78SJilles Tjoelker 	return (0);
1545d358fa78SJilles Tjoelker }
1546