xref: /freebsd/bin/sh/expand.c (revision c39d3320ab681401a3644401928e8be40f13afd9)
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.
20fbbd9655SWarner 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 */
924b88c807SRodney W. Grimes 
93439948cdSJilles Tjoelker static const char *argstr(const char *, struct nodelist **restrict, int,
94439948cdSJilles Tjoelker     struct worddest *);
9552d5897dSJilles Tjoelker static const char *exptilde(const char *, int);
96439948cdSJilles Tjoelker static const char *expari(const char *, struct nodelist **restrict, int,
97439948cdSJilles Tjoelker     struct worddest *);
9847d8814cSJilles Tjoelker static void expbackq(union node *, int, int, struct worddest *);
99e2708b16SJilles Tjoelker static const char *subevalvar_trim(const char *, struct nodelist **restrict,
100e2708b16SJilles Tjoelker     int, int, int);
101e59833ccSJilles Tjoelker static const char *subevalvar_misc(const char *, struct nodelist **restrict,
102e59833ccSJilles Tjoelker     const char *, int, int, int);
103439948cdSJilles Tjoelker static const char *evalvar(const char *, struct nodelist **restrict, int,
104439948cdSJilles Tjoelker     struct worddest *);
10561346cbdSJilles Tjoelker static int varisset(const char *, int);
10647d8814cSJilles Tjoelker static void strtodest(const char *, int, int, int, struct worddest *);
10747d8814cSJilles Tjoelker static void reprocess(int, int, int, int, struct worddest *);
10847d8814cSJilles Tjoelker static void varvalue(const char *, int, int, int, struct worddest *);
1090e39c931SJilles Tjoelker static void expandmeta(char *, struct arglist *);
1108ef0ae8aSJilles Tjoelker static void expmeta(char *, char *, struct arglist *);
1118ef0ae8aSJilles Tjoelker static int expsortcmp(const void *, const void *);
11247d8814cSJilles Tjoelker static int patmatch(const char *, const char *);
11347d8814cSJilles Tjoelker static void cvtnum(int, char *);
114fa93fc65SAndrey A. Chernov static int collate_range_cmp(wchar_t, wchar_t);
1153cd859a7SAndrey A. Chernov 
1168ef0ae8aSJilles Tjoelker void
1178ef0ae8aSJilles Tjoelker emptyarglist(struct arglist *list)
1188ef0ae8aSJilles Tjoelker {
1198ef0ae8aSJilles Tjoelker 
1208ef0ae8aSJilles Tjoelker 	list->args = list->smallarg;
1218ef0ae8aSJilles Tjoelker 	list->count = 0;
1228ef0ae8aSJilles Tjoelker 	list->capacity = sizeof(list->smallarg) / sizeof(list->smallarg[0]);
1238ef0ae8aSJilles Tjoelker }
1248ef0ae8aSJilles Tjoelker 
125046bfe52SJilles Tjoelker void
1268ef0ae8aSJilles Tjoelker appendarglist(struct arglist *list, char *str)
1278ef0ae8aSJilles Tjoelker {
1288ef0ae8aSJilles Tjoelker 	char **newargs;
1298ef0ae8aSJilles Tjoelker 	int newcapacity;
1308ef0ae8aSJilles Tjoelker 
1318ef0ae8aSJilles Tjoelker 	if (list->count >= list->capacity) {
1328ef0ae8aSJilles Tjoelker 		newcapacity = list->capacity * 2;
1338ef0ae8aSJilles Tjoelker 		if (newcapacity < 16)
1348ef0ae8aSJilles Tjoelker 			newcapacity = 16;
1358ef0ae8aSJilles Tjoelker 		if (newcapacity > INT_MAX / (int)sizeof(newargs[0]))
1368ef0ae8aSJilles Tjoelker 			error("Too many entries in arglist");
1378ef0ae8aSJilles Tjoelker 		newargs = stalloc(newcapacity * sizeof(newargs[0]));
1388ef0ae8aSJilles Tjoelker 		memcpy(newargs, list->args, list->count * sizeof(newargs[0]));
1398ef0ae8aSJilles Tjoelker 		list->args = newargs;
1408ef0ae8aSJilles Tjoelker 		list->capacity = newcapacity;
1418ef0ae8aSJilles Tjoelker 	}
1428ef0ae8aSJilles Tjoelker 	list->args[list->count++] = str;
1438ef0ae8aSJilles Tjoelker }
1448ef0ae8aSJilles Tjoelker 
145fa93fc65SAndrey A. Chernov static int
146fa93fc65SAndrey A. Chernov collate_range_cmp(wchar_t c1, wchar_t c2)
147fa93fc65SAndrey A. Chernov {
148*c39d3320SJilles Tjoelker 	wchar_t s1[2], s2[2];
149fa93fc65SAndrey A. Chernov 
150fa93fc65SAndrey A. Chernov 	s1[0] = c1;
151*c39d3320SJilles Tjoelker 	s1[1] = L'\0';
152fa93fc65SAndrey A. Chernov 	s2[0] = c2;
153*c39d3320SJilles Tjoelker 	s2[1] = L'\0';
154fa93fc65SAndrey A. Chernov 	return (wcscoll(s1, s2));
155fa93fc65SAndrey A. Chernov }
156fa93fc65SAndrey A. Chernov 
157f7dea851SJilles Tjoelker static char *
158f7dea851SJilles Tjoelker stputs_quotes(const char *data, const char *syntax, char *p)
159f7dea851SJilles Tjoelker {
160f7dea851SJilles Tjoelker 	while (*data) {
161f7dea851SJilles Tjoelker 		CHECKSTRSPACE(2, p);
162f7dea851SJilles Tjoelker 		if (syntax[(int)*data] == CCTL)
163f7dea851SJilles Tjoelker 			USTPUTC(CTLESC, p);
164f7dea851SJilles Tjoelker 		USTPUTC(*data++, p);
165f7dea851SJilles Tjoelker 	}
166f7dea851SJilles Tjoelker 	return (p);
167f7dea851SJilles Tjoelker }
168f7dea851SJilles Tjoelker #define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p)
1694b88c807SRodney W. Grimes 
17047d8814cSJilles Tjoelker static char *
1710e39c931SJilles Tjoelker nextword(char c, int flag, char *p, struct worddest *dst)
17247d8814cSJilles Tjoelker {
17347d8814cSJilles Tjoelker 	int is_ws;
17447d8814cSJilles Tjoelker 
17547d8814cSJilles Tjoelker 	is_ws = c == '\t' || c == '\n' || c == ' ';
17647d8814cSJilles Tjoelker 	if (p != stackblock() || (is_ws ? dst->state == WORD_QUOTEMARK :
17747d8814cSJilles Tjoelker 	    dst->state != WORD_WS_DELIMITED) || c == '\0') {
17847d8814cSJilles Tjoelker 		STPUTC('\0', p);
1790e39c931SJilles Tjoelker 		if (flag & EXP_GLOB)
1800e39c931SJilles Tjoelker 			expandmeta(grabstackstr(p), dst->list);
1810e39c931SJilles Tjoelker 		else
1820e39c931SJilles Tjoelker 			appendarglist(dst->list, grabstackstr(p));
18347d8814cSJilles Tjoelker 		dst->state = is_ws ? WORD_WS_DELIMITED : WORD_IDLE;
18447d8814cSJilles Tjoelker 	} else if (!is_ws && dst->state == WORD_WS_DELIMITED)
18547d8814cSJilles Tjoelker 		dst->state = WORD_IDLE;
18647d8814cSJilles Tjoelker 	/* Reserve space while the stack string is empty. */
1870e39c931SJilles Tjoelker 	appendarglist(dst->list, NULL);
1880e39c931SJilles Tjoelker 	dst->list->count--;
18947d8814cSJilles Tjoelker 	STARTSTACKSTR(p);
19047d8814cSJilles Tjoelker 	return p;
19147d8814cSJilles Tjoelker }
1920e39c931SJilles Tjoelker #define NEXTWORD(c, flag, p, dstlist) p = nextword(c, flag, p, dstlist)
19347d8814cSJilles Tjoelker 
19447d8814cSJilles Tjoelker static char *
1950e39c931SJilles Tjoelker stputs_split(const char *data, const char *syntax, int flag, char *p,
19647d8814cSJilles Tjoelker     struct worddest *dst)
19747d8814cSJilles Tjoelker {
19847d8814cSJilles Tjoelker 	const char *ifs;
19947d8814cSJilles Tjoelker 	char c;
20047d8814cSJilles Tjoelker 
20147d8814cSJilles Tjoelker 	ifs = ifsset() ? ifsval() : " \t\n";
20247d8814cSJilles Tjoelker 	while (*data) {
20347d8814cSJilles Tjoelker 		CHECKSTRSPACE(2, p);
20447d8814cSJilles Tjoelker 		c = *data++;
20547d8814cSJilles Tjoelker 		if (strchr(ifs, c) != NULL) {
2060e39c931SJilles Tjoelker 			NEXTWORD(c, flag, p, dst);
20747d8814cSJilles Tjoelker 			continue;
20847d8814cSJilles Tjoelker 		}
2090e39c931SJilles Tjoelker 		if (flag & EXP_GLOB && syntax[(int)c] == CCTL)
21047d8814cSJilles Tjoelker 			USTPUTC(CTLESC, p);
21147d8814cSJilles Tjoelker 		USTPUTC(c, p);
21247d8814cSJilles Tjoelker 	}
21347d8814cSJilles Tjoelker 	return (p);
21447d8814cSJilles Tjoelker }
2150e39c931SJilles Tjoelker #define STPUTS_SPLIT(data, syntax, flag, p, dst) p = stputs_split((data), syntax, flag, p, dst)
21647d8814cSJilles Tjoelker 
2174b88c807SRodney W. Grimes /*
2182ca3d70fSJilles Tjoelker  * Perform expansions on an argument, placing the resulting list of arguments
2192ca3d70fSJilles Tjoelker  * in arglist.  Parameter expansion, command substitution and arithmetic
2202ca3d70fSJilles Tjoelker  * expansion are always performed; additional expansions can be requested
2212ca3d70fSJilles Tjoelker  * via flag (EXP_*).
2222ca3d70fSJilles Tjoelker  * The result is left in the stack string.
2233e0b768cSJilles Tjoelker  * When arglist is NULL, perform here document expansion.
2242ca3d70fSJilles Tjoelker  *
2252ca3d70fSJilles Tjoelker  * Caution: this function uses global state and is not reentrant.
2262ca3d70fSJilles Tjoelker  * However, a new invocation after an interrupted invocation is safe
2272ca3d70fSJilles Tjoelker  * and will reset the global state for the new call.
2284b88c807SRodney W. Grimes  */
2294b88c807SRodney W. Grimes void
2305134c3f7SWarner Losh expandarg(union node *arg, struct arglist *arglist, int flag)
2314b88c807SRodney W. Grimes {
23247d8814cSJilles Tjoelker 	struct worddest exparg;
233439948cdSJilles Tjoelker 	struct nodelist *argbackq;
2344b88c807SRodney W. Grimes 
2350e39c931SJilles Tjoelker 	if (fflag)
2360e39c931SJilles Tjoelker 		flag &= ~EXP_GLOB;
2374b88c807SRodney W. Grimes 	argbackq = arg->narg.backquote;
2380e39c931SJilles Tjoelker 	exparg.list = arglist;
23947d8814cSJilles Tjoelker 	exparg.state = WORD_IDLE;
2404b88c807SRodney W. Grimes 	STARTSTACKSTR(expdest);
241439948cdSJilles Tjoelker 	argstr(arg->narg.text, &argbackq, flag, &exparg);
2424b88c807SRodney W. Grimes 	if (arglist == NULL) {
243292e6676SJilles Tjoelker 		STACKSTRNUL(expdest);
2444b88c807SRodney W. Grimes 		return;			/* here document expanded */
2454b88c807SRodney W. Grimes 	}
2460e39c931SJilles Tjoelker 	if ((flag & EXP_SPLIT) == 0 || expdest != stackblock() ||
24747d8814cSJilles Tjoelker 	    exparg.state == WORD_QUOTEMARK) {
2484b88c807SRodney W. Grimes 		STPUTC('\0', expdest);
2490e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT) {
2500e39c931SJilles Tjoelker 			if (flag & EXP_GLOB)
2510e39c931SJilles Tjoelker 				expandmeta(grabstackstr(expdest), exparg.list);
25247d8814cSJilles Tjoelker 			else
2530e39c931SJilles Tjoelker 				appendarglist(exparg.list, grabstackstr(expdest));
2540e39c931SJilles Tjoelker 		}
2550e39c931SJilles Tjoelker 	}
2560e39c931SJilles Tjoelker 	if ((flag & EXP_SPLIT) == 0)
25747d8814cSJilles Tjoelker 		appendarglist(arglist, grabstackstr(expdest));
2584b88c807SRodney W. Grimes }
2594b88c807SRodney W. Grimes 
2604b88c807SRodney W. Grimes 
2614b88c807SRodney W. Grimes 
2624b88c807SRodney W. Grimes /*
2632ca3d70fSJilles Tjoelker  * Perform parameter expansion, command substitution and arithmetic
2642ca3d70fSJilles Tjoelker  * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
265ce16da82SJilles Tjoelker  * Processing ends at a CTLENDVAR or CTLENDARI character as well as '\0'.
2662ca3d70fSJilles Tjoelker  * This is used to expand word in ${var+word} etc.
2670e39c931SJilles Tjoelker  * If EXP_GLOB or EXP_CASE are set, keep and/or generate CTLESC
2682ca3d70fSJilles Tjoelker  * characters to allow for further processing.
26947d8814cSJilles Tjoelker  *
2700e39c931SJilles Tjoelker  * If EXP_SPLIT is set, dst receives any complete words produced.
2714b88c807SRodney W. Grimes  */
27252d5897dSJilles Tjoelker static const char *
273439948cdSJilles Tjoelker argstr(const char *p, struct nodelist **restrict argbackq, int flag,
274439948cdSJilles Tjoelker     struct worddest *dst)
2754b88c807SRodney W. Grimes {
27696522b88SSteve Price 	char c;
2770e39c931SJilles Tjoelker 	int quotes = flag & (EXP_GLOB | EXP_CASE);	/* do CTLESC */
2784b88c807SRodney W. Grimes 	int firsteq = 1;
279048f2667SJilles Tjoelker 	int split_lit;
280048f2667SJilles Tjoelker 	int lit_quoted;
2814b88c807SRodney W. Grimes 
282048f2667SJilles Tjoelker 	split_lit = flag & EXP_SPLIT_LIT;
283048f2667SJilles Tjoelker 	lit_quoted = flag & EXP_LIT_QUOTED;
284048f2667SJilles Tjoelker 	flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED);
2854b88c807SRodney W. Grimes 	if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
2864b88c807SRodney W. Grimes 		p = exptilde(p, flag);
2874b88c807SRodney W. Grimes 	for (;;) {
2889d37e157SJilles Tjoelker 		CHECKSTRSPACE(2, expdest);
2894b88c807SRodney W. Grimes 		switch (c = *p++) {
2904b88c807SRodney W. Grimes 		case '\0':
291a2cba42fSJilles Tjoelker 			return (p - 1);
2922ca3d70fSJilles Tjoelker 		case CTLENDVAR:
293ce16da82SJilles Tjoelker 		case CTLENDARI:
294a2cba42fSJilles Tjoelker 			return (p);
2956f47734fSTor Egge 		case CTLQUOTEMARK:
296048f2667SJilles Tjoelker 			lit_quoted = 1;
2976f47734fSTor Egge 			/* "$@" syntax adherence hack */
298a5cb58abSJilles Tjoelker 			if (p[0] == CTLVAR && (p[1] & VSQUOTE) != 0 &&
299a5cb58abSJilles Tjoelker 			    p[2] == '@' && p[3] == '=')
3006f47734fSTor Egge 				break;
3010e39c931SJilles Tjoelker 			if ((flag & EXP_SPLIT) != 0 && expdest == stackblock())
30247d8814cSJilles Tjoelker 				dst->state = WORD_QUOTEMARK;
3036f47734fSTor Egge 			break;
304048f2667SJilles Tjoelker 		case CTLQUOTEEND:
305048f2667SJilles Tjoelker 			lit_quoted = 0;
306048f2667SJilles Tjoelker 			break;
3074b88c807SRodney W. Grimes 		case CTLESC:
3084b88c807SRodney W. Grimes 			c = *p++;
30947d8814cSJilles Tjoelker 			if (split_lit && !lit_quoted &&
31047d8814cSJilles Tjoelker 			    strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
3110e39c931SJilles Tjoelker 				NEXTWORD(c, flag, expdest, dst);
31247d8814cSJilles Tjoelker 				break;
31347d8814cSJilles Tjoelker 			}
31447d8814cSJilles Tjoelker 			if (quotes)
31547d8814cSJilles Tjoelker 				USTPUTC(CTLESC, expdest);
3169d37e157SJilles Tjoelker 			USTPUTC(c, expdest);
3174b88c807SRodney W. Grimes 			break;
3184b88c807SRodney W. Grimes 		case CTLVAR:
319439948cdSJilles Tjoelker 			p = evalvar(p, argbackq, flag, dst);
3204b88c807SRodney W. Grimes 			break;
3214b88c807SRodney W. Grimes 		case CTLBACKQ:
3224b88c807SRodney W. Grimes 		case CTLBACKQ|CTLQUOTE:
323439948cdSJilles Tjoelker 			expbackq((*argbackq)->n, c & CTLQUOTE, flag, dst);
324439948cdSJilles Tjoelker 			*argbackq = (*argbackq)->next;
3254b88c807SRodney W. Grimes 			break;
326ce16da82SJilles Tjoelker 		case CTLARI:
327439948cdSJilles Tjoelker 			p = expari(p, argbackq, flag, dst);
3284b88c807SRodney W. Grimes 			break;
3294b88c807SRodney W. Grimes 		case ':':
3304b88c807SRodney W. Grimes 		case '=':
3314b88c807SRodney W. Grimes 			/*
3324b88c807SRodney W. Grimes 			 * sort of a hack - expand tildes in variable
3334b88c807SRodney W. Grimes 			 * assignments (after the first '=' and after ':'s).
3344b88c807SRodney W. Grimes 			 */
33547d8814cSJilles Tjoelker 			if (split_lit && !lit_quoted &&
33647d8814cSJilles Tjoelker 			    strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
3370e39c931SJilles Tjoelker 				NEXTWORD(c, flag, expdest, dst);
33847d8814cSJilles Tjoelker 				break;
33947d8814cSJilles Tjoelker 			}
3409d37e157SJilles Tjoelker 			USTPUTC(c, expdest);
341048f2667SJilles Tjoelker 			if (flag & EXP_VARTILDE && *p == '~' &&
342048f2667SJilles Tjoelker 			    (c != '=' || firsteq)) {
343048f2667SJilles Tjoelker 				if (c == '=')
3444b88c807SRodney W. Grimes 					firsteq = 0;
3454b88c807SRodney W. Grimes 				p = exptilde(p, flag);
3464b88c807SRodney W. Grimes 			}
3474b88c807SRodney W. Grimes 			break;
3484b88c807SRodney W. Grimes 		default:
34947d8814cSJilles Tjoelker 			if (split_lit && !lit_quoted &&
35047d8814cSJilles Tjoelker 			    strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
3510e39c931SJilles Tjoelker 				NEXTWORD(c, flag, expdest, dst);
35247d8814cSJilles Tjoelker 				break;
35347d8814cSJilles Tjoelker 			}
3549d37e157SJilles Tjoelker 			USTPUTC(c, expdest);
3554b88c807SRodney W. Grimes 		}
3564b88c807SRodney W. Grimes 	}
3574b88c807SRodney W. Grimes }
3584b88c807SRodney W. Grimes 
3592ca3d70fSJilles Tjoelker /*
3602ca3d70fSJilles Tjoelker  * Perform tilde expansion, placing the result in the stack string and
3612ca3d70fSJilles Tjoelker  * returning the next position in the input string to process.
3622ca3d70fSJilles Tjoelker  */
36352d5897dSJilles Tjoelker static const char *
36452d5897dSJilles Tjoelker exptilde(const char *p, int flag)
3654b88c807SRodney W. Grimes {
36652d5897dSJilles Tjoelker 	char c;
36752d5897dSJilles Tjoelker 	const char *startp = p;
36852d5897dSJilles Tjoelker 	const char *user;
3694b88c807SRodney W. Grimes 	struct passwd *pw;
3704b88c807SRodney W. Grimes 	char *home;
37152d5897dSJilles Tjoelker 	int len;
3724b88c807SRodney W. Grimes 
3737034d8dfSJilles Tjoelker 	for (;;) {
3747034d8dfSJilles Tjoelker 		c = *p;
3754b88c807SRodney W. Grimes 		switch(c) {
37605c10507SJilles Tjoelker 		case CTLESC: /* This means CTL* are always considered quoted. */
37705c10507SJilles Tjoelker 		case CTLVAR:
37805c10507SJilles Tjoelker 		case CTLBACKQ:
37905c10507SJilles Tjoelker 		case CTLBACKQ | CTLQUOTE:
38005c10507SJilles Tjoelker 		case CTLARI:
38105c10507SJilles Tjoelker 		case CTLENDARI:
3825557a02aSTor Egge 		case CTLQUOTEMARK:
3835557a02aSTor Egge 			return (startp);
3844b88c807SRodney W. Grimes 		case ':':
3857034d8dfSJilles Tjoelker 			if ((flag & EXP_VARTILDE) == 0)
3864b88c807SRodney W. Grimes 				break;
3877034d8dfSJilles Tjoelker 			/* FALLTHROUGH */
3887034d8dfSJilles Tjoelker 		case '\0':
3894b88c807SRodney W. Grimes 		case '/':
390634e9188SJilles Tjoelker 		case CTLENDVAR:
39152d5897dSJilles Tjoelker 			len = p - startp - 1;
39252d5897dSJilles Tjoelker 			STPUTBIN(startp + 1, len, expdest);
39352d5897dSJilles Tjoelker 			STACKSTRNUL(expdest);
39452d5897dSJilles Tjoelker 			user = expdest - len;
39552d5897dSJilles Tjoelker 			if (*user == '\0') {
39633c5acf0SJilles Tjoelker 				home = lookupvar("HOME");
3974b88c807SRodney W. Grimes 			} else {
39852d5897dSJilles Tjoelker 				pw = getpwnam(user);
39933c5acf0SJilles Tjoelker 				home = pw != NULL ? pw->pw_dir : NULL;
4004b88c807SRodney W. Grimes 			}
40152d5897dSJilles Tjoelker 			STADJUST(-len, expdest);
40233c5acf0SJilles Tjoelker 			if (home == NULL || *home == '\0')
40333c5acf0SJilles Tjoelker 				return (startp);
40447d8814cSJilles Tjoelker 			strtodest(home, flag, VSNORMAL, 1, NULL);
4054b88c807SRodney W. Grimes 			return (p);
4064b88c807SRodney W. Grimes 		}
4077034d8dfSJilles Tjoelker 		p++;
4087034d8dfSJilles Tjoelker 	}
4097034d8dfSJilles Tjoelker }
4104b88c807SRodney W. Grimes 
4114b88c807SRodney W. Grimes 
4124b88c807SRodney W. Grimes /*
413ce16da82SJilles Tjoelker  * Expand arithmetic expression.
4144b88c807SRodney W. Grimes  */
41552d5897dSJilles Tjoelker static const char *
416439948cdSJilles Tjoelker expari(const char *p, struct nodelist **restrict argbackq, int flag,
417439948cdSJilles Tjoelker     struct worddest *dst)
4184b88c807SRodney W. Grimes {
419ce16da82SJilles Tjoelker 	char *q, *start;
420d9d588d4SStefan Farfeleder 	arith_t result;
4216f47734fSTor Egge 	int begoff;
4226f47734fSTor Egge 	int quoted;
423ce16da82SJilles Tjoelker 	int adj;
4244b88c807SRodney W. Grimes 
425ce16da82SJilles Tjoelker 	quoted = *p++ == '"';
426ce16da82SJilles Tjoelker 	begoff = expdest - stackblock();
427439948cdSJilles Tjoelker 	p = argstr(p, argbackq, 0, NULL);
428ce16da82SJilles Tjoelker 	STPUTC('\0', expdest);
429ce16da82SJilles Tjoelker 	start = stackblock() + begoff;
430ce16da82SJilles Tjoelker 
431593e925aSJilles Tjoelker 	q = grabstackstr(expdest);
432ce16da82SJilles Tjoelker 	result = arith(start);
433593e925aSJilles Tjoelker 	ungrabstackstr(q, expdest);
434ce16da82SJilles Tjoelker 
435ce16da82SJilles Tjoelker 	start = stackblock() + begoff;
436ce16da82SJilles Tjoelker 	adj = start - expdest;
437ce16da82SJilles Tjoelker 	STADJUST(adj, expdest);
438ce16da82SJilles Tjoelker 
439ce16da82SJilles Tjoelker 	CHECKSTRSPACE((int)(DIGITS(result) + 1), expdest);
440ce16da82SJilles Tjoelker 	fmtstr(expdest, DIGITS(result), ARITH_FORMAT_STR, result);
441ce16da82SJilles Tjoelker 	adj = strlen(expdest);
442ce16da82SJilles Tjoelker 	STADJUST(adj, expdest);
443ce16da82SJilles Tjoelker 	if (!quoted)
44447d8814cSJilles Tjoelker 		reprocess(expdest - adj - stackblock(), flag, VSNORMAL, 0, dst);
445ce16da82SJilles Tjoelker 	return p;
4464b88c807SRodney W. Grimes }
4474b88c807SRodney W. Grimes 
4484b88c807SRodney W. Grimes 
4494b88c807SRodney W. Grimes /*
4502ca3d70fSJilles Tjoelker  * Perform command substitution.
4514b88c807SRodney W. Grimes  */
45288328642SDavid E. O'Brien static void
45347d8814cSJilles Tjoelker expbackq(union node *cmd, int quoted, int flag, struct worddest *dst)
4544b88c807SRodney W. Grimes {
4554b88c807SRodney W. Grimes 	struct backcmd in;
4564b88c807SRodney W. Grimes 	int i;
4574b88c807SRodney W. Grimes 	char buf[128];
4584b88c807SRodney W. Grimes 	char *p;
4594b88c807SRodney W. Grimes 	char *dest = expdest;
4604b88c807SRodney W. Grimes 	char lastc;
4614b88c807SRodney W. Grimes 	char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
4620e39c931SJilles Tjoelker 	int quotes = flag & (EXP_GLOB | EXP_CASE);
46346c6b52dSJilles Tjoelker 	size_t nnl;
46447d8814cSJilles Tjoelker 	const char *ifs;
4654b88c807SRodney W. Grimes 
4664b88c807SRodney W. Grimes 	INTOFF;
4674b88c807SRodney W. Grimes 	p = grabstackstr(dest);
4684b88c807SRodney W. Grimes 	evalbackcmd(cmd, &in);
4694b88c807SRodney W. Grimes 	ungrabstackstr(p, dest);
4704b88c807SRodney W. Grimes 
4714b88c807SRodney W. Grimes 	p = in.buf;
47299907703SBill Fenner 	nnl = 0;
4730e39c931SJilles Tjoelker 	if (!quoted && flag & EXP_SPLIT)
47447d8814cSJilles Tjoelker 		ifs = ifsset() ? ifsval() : " \t\n";
47547d8814cSJilles Tjoelker 	else
47647d8814cSJilles Tjoelker 		ifs = "";
47799907703SBill Fenner 	/* Don't copy trailing newlines */
4784b88c807SRodney W. Grimes 	for (;;) {
4794b88c807SRodney W. Grimes 		if (--in.nleft < 0) {
4804b88c807SRodney W. Grimes 			if (in.fd < 0)
4814b88c807SRodney W. Grimes 				break;
4823bb6ada2SJilles Tjoelker 			while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR)
4833bb6ada2SJilles Tjoelker 				;
4844b88c807SRodney W. Grimes 			TRACE(("expbackq: read returns %d\n", i));
4854b88c807SRodney W. Grimes 			if (i <= 0)
4864b88c807SRodney W. Grimes 				break;
4874b88c807SRodney W. Grimes 			p = buf;
4884b88c807SRodney W. Grimes 			in.nleft = i - 1;
4894b88c807SRodney W. Grimes 		}
4904b88c807SRodney W. Grimes 		lastc = *p++;
49147d8814cSJilles Tjoelker 		if (lastc == '\0')
49247d8814cSJilles Tjoelker 			continue;
49399907703SBill Fenner 		if (lastc == '\n') {
49499907703SBill Fenner 			nnl++;
49599907703SBill Fenner 		} else {
49647d8814cSJilles Tjoelker 			if (nnl > 0) {
49747d8814cSJilles Tjoelker 				if (strchr(ifs, '\n') != NULL) {
4980e39c931SJilles Tjoelker 					NEXTWORD('\n', flag, dest, dst);
49947d8814cSJilles Tjoelker 					nnl = 0;
50047d8814cSJilles Tjoelker 				} else {
501d8f32e72SJilles Tjoelker 					CHECKSTRSPACE(nnl + 2, dest);
50299907703SBill Fenner 					while (nnl > 0) {
50399907703SBill Fenner 						nnl--;
504d8f32e72SJilles Tjoelker 						USTPUTC('\n', dest);
50599907703SBill Fenner 					}
50647d8814cSJilles Tjoelker 				}
50747d8814cSJilles Tjoelker 			}
50847d8814cSJilles Tjoelker 			if (strchr(ifs, lastc) != NULL)
5090e39c931SJilles Tjoelker 				NEXTWORD(lastc, flag, dest, dst);
51047d8814cSJilles Tjoelker 			else {
51147d8814cSJilles Tjoelker 				CHECKSTRSPACE(2, dest);
512fa0951d6SJilles Tjoelker 				if (quotes && syntax[(int)lastc] == CCTL)
513d8f32e72SJilles Tjoelker 					USTPUTC(CTLESC, dest);
514d8f32e72SJilles Tjoelker 				USTPUTC(lastc, dest);
5154b88c807SRodney W. Grimes 			}
5164b88c807SRodney W. Grimes 		}
51799907703SBill Fenner 	}
518aa9caaf6SPeter Wemm 
5194b88c807SRodney W. Grimes 	if (in.fd >= 0)
5204b88c807SRodney W. Grimes 		close(in.fd);
5214b88c807SRodney W. Grimes 	if (in.buf)
5224b88c807SRodney W. Grimes 		ckfree(in.buf);
523586fd248SJilles Tjoelker 	if (in.jp) {
524586fd248SJilles Tjoelker 		p = grabstackstr(dest);
52557b2932aSMartin Cracauer 		exitstatus = waitforjob(in.jp, (int *)NULL);
526586fd248SJilles Tjoelker 		ungrabstackstr(p, dest);
527586fd248SJilles Tjoelker 	}
528a54caffdSJilles Tjoelker 	TRACE(("expbackq: done\n"));
5294b88c807SRodney W. Grimes 	expdest = dest;
5304b88c807SRodney W. Grimes 	INTON;
5314b88c807SRodney W. Grimes }
5324b88c807SRodney W. Grimes 
5334b88c807SRodney W. Grimes 
5344b88c807SRodney W. Grimes 
5357034d8dfSJilles Tjoelker static void
5367034d8dfSJilles Tjoelker recordleft(const char *str, const char *loc, char *startp)
5377034d8dfSJilles Tjoelker {
5387034d8dfSJilles Tjoelker 	int amount;
5397034d8dfSJilles Tjoelker 
5407034d8dfSJilles Tjoelker 	amount = ((str - 1) - (loc - startp)) - expdest;
5417034d8dfSJilles Tjoelker 	STADJUST(amount, expdest);
5427034d8dfSJilles Tjoelker 	while (loc != str - 1)
5437034d8dfSJilles Tjoelker 		*startp++ = *loc++;
5447034d8dfSJilles Tjoelker }
5457034d8dfSJilles Tjoelker 
546e2708b16SJilles Tjoelker static const char *
547e2708b16SJilles Tjoelker subevalvar_trim(const char *p, struct nodelist **restrict argbackq, int strloc,
548439948cdSJilles Tjoelker     int subtype, int startloc)
549aa9caaf6SPeter Wemm {
550aa9caaf6SPeter Wemm 	char *startp;
551aa9caaf6SPeter Wemm 	char *loc = NULL;
5524f2f5faaSJilles Tjoelker 	char *str;
553aa9caaf6SPeter Wemm 	int c = 0;
554ab0a2172SSteve Price 	int amount;
555ab0a2172SSteve Price 
556e2708b16SJilles Tjoelker 	p = argstr(p, argbackq, EXP_CASE | EXP_TILDE, NULL);
557aa9caaf6SPeter Wemm 	STACKSTRNUL(expdest);
558aa9caaf6SPeter Wemm 	startp = stackblock() + startloc;
559ab0a2172SSteve Price 	str = stackblock() + strloc;
560aa9caaf6SPeter Wemm 
561aa9caaf6SPeter Wemm 	switch (subtype) {
562aa9caaf6SPeter Wemm 	case VSTRIMLEFT:
56396522b88SSteve Price 		for (loc = startp; loc < str; loc++) {
564aa9caaf6SPeter Wemm 			c = *loc;
565aa9caaf6SPeter Wemm 			*loc = '\0';
56647d8814cSJilles Tjoelker 			if (patmatch(str, startp)) {
567aa9caaf6SPeter Wemm 				*loc = c;
5687034d8dfSJilles Tjoelker 				recordleft(str, loc, startp);
569e2708b16SJilles Tjoelker 				return p;
570aa9caaf6SPeter Wemm 			}
571aa9caaf6SPeter Wemm 			*loc = c;
572aa9caaf6SPeter Wemm 		}
57347d8814cSJilles Tjoelker 		break;
574aa9caaf6SPeter Wemm 
575aa9caaf6SPeter Wemm 	case VSTRIMLEFTMAX:
5768b220a61STor Egge 		for (loc = str - 1; loc >= startp;) {
577aa9caaf6SPeter Wemm 			c = *loc;
578aa9caaf6SPeter Wemm 			*loc = '\0';
57947d8814cSJilles Tjoelker 			if (patmatch(str, startp)) {
580aa9caaf6SPeter Wemm 				*loc = c;
5817034d8dfSJilles Tjoelker 				recordleft(str, loc, startp);
582e2708b16SJilles Tjoelker 				return p;
583aa9caaf6SPeter Wemm 			}
584aa9caaf6SPeter Wemm 			*loc = c;
5858b220a61STor Egge 			loc--;
5868b220a61STor Egge 		}
58747d8814cSJilles Tjoelker 		break;
588aa9caaf6SPeter Wemm 
589aa9caaf6SPeter Wemm 	case VSTRIMRIGHT:
5908b220a61STor Egge 		for (loc = str - 1; loc >= startp;) {
59147d8814cSJilles Tjoelker 			if (patmatch(str, loc)) {
592ab0a2172SSteve Price 				amount = loc - expdest;
593ab0a2172SSteve Price 				STADJUST(amount, expdest);
594e2708b16SJilles Tjoelker 				return p;
595aa9caaf6SPeter Wemm 			}
5968b220a61STor Egge 			loc--;
5978b220a61STor Egge 		}
59847d8814cSJilles Tjoelker 		break;
599aa9caaf6SPeter Wemm 
600aa9caaf6SPeter Wemm 	case VSTRIMRIGHTMAX:
601aa9caaf6SPeter Wemm 		for (loc = startp; loc < str - 1; loc++) {
60247d8814cSJilles Tjoelker 			if (patmatch(str, loc)) {
603ab0a2172SSteve Price 				amount = loc - expdest;
604ab0a2172SSteve Price 				STADJUST(amount, expdest);
605e2708b16SJilles Tjoelker 				return p;
606aa9caaf6SPeter Wemm 			}
607aa9caaf6SPeter Wemm 		}
60847d8814cSJilles Tjoelker 		break;
609aa9caaf6SPeter Wemm 
610aa9caaf6SPeter Wemm 
611aa9caaf6SPeter Wemm 	default:
612aa9caaf6SPeter Wemm 		abort();
613aa9caaf6SPeter Wemm 	}
61447d8814cSJilles Tjoelker 	amount = (expdest - stackblock() - strloc) + 1;
61547d8814cSJilles Tjoelker 	STADJUST(-amount, expdest);
616e2708b16SJilles Tjoelker 	return p;
617aa9caaf6SPeter Wemm }
618aa9caaf6SPeter Wemm 
619aa9caaf6SPeter Wemm 
620e59833ccSJilles Tjoelker static const char *
621e59833ccSJilles Tjoelker subevalvar_misc(const char *p, struct nodelist **restrict argbackq,
622e59833ccSJilles Tjoelker     const char *var, int subtype, int startloc, int varflags)
6234f2f5faaSJilles Tjoelker {
6244f2f5faaSJilles Tjoelker 	char *startp;
6254f2f5faaSJilles Tjoelker 	int amount;
6264f2f5faaSJilles Tjoelker 
627e59833ccSJilles Tjoelker 	p = argstr(p, argbackq, EXP_TILDE, NULL);
6284f2f5faaSJilles Tjoelker 	STACKSTRNUL(expdest);
6294f2f5faaSJilles Tjoelker 	startp = stackblock() + startloc;
6304f2f5faaSJilles Tjoelker 
6314f2f5faaSJilles Tjoelker 	switch (subtype) {
6324f2f5faaSJilles Tjoelker 	case VSASSIGN:
6334f2f5faaSJilles Tjoelker 		setvar(var, startp, 0);
6344f2f5faaSJilles Tjoelker 		amount = startp - expdest;
6354f2f5faaSJilles Tjoelker 		STADJUST(amount, expdest);
636e59833ccSJilles Tjoelker 		return p;
6374f2f5faaSJilles Tjoelker 
6384f2f5faaSJilles Tjoelker 	case VSQUESTION:
6394f2f5faaSJilles Tjoelker 		if (*p != CTLENDVAR) {
6404f2f5faaSJilles Tjoelker 			outfmt(out2, "%s\n", startp);
6414f2f5faaSJilles Tjoelker 			error((char *)NULL);
6424f2f5faaSJilles Tjoelker 		}
6434f2f5faaSJilles Tjoelker 		error("%.*s: parameter %snot set", (int)(p - var - 1),
6444f2f5faaSJilles Tjoelker 		      var, (varflags & VSNUL) ? "null or " : "");
6454f2f5faaSJilles Tjoelker 
6464f2f5faaSJilles Tjoelker 	default:
6474f2f5faaSJilles Tjoelker 		abort();
6484f2f5faaSJilles Tjoelker 	}
6494f2f5faaSJilles Tjoelker }
6504f2f5faaSJilles Tjoelker 
6514f2f5faaSJilles Tjoelker 
6524b88c807SRodney W. Grimes /*
6534b88c807SRodney W. Grimes  * Expand a variable, and return a pointer to the next character in the
6544b88c807SRodney W. Grimes  * input string.
6554b88c807SRodney W. Grimes  */
6564b88c807SRodney W. Grimes 
65752d5897dSJilles Tjoelker static const char *
658439948cdSJilles Tjoelker evalvar(const char *p, struct nodelist **restrict argbackq, int flag,
659439948cdSJilles Tjoelker     struct worddest *dst)
6604b88c807SRodney W. Grimes {
6614b88c807SRodney W. Grimes 	int subtype;
6624b88c807SRodney W. Grimes 	int varflags;
66352d5897dSJilles Tjoelker 	const char *var;
66461346cbdSJilles Tjoelker 	const char *val;
665c4e5a8a8STor Egge 	int patloc;
6664b88c807SRodney W. Grimes 	int c;
6674b88c807SRodney W. Grimes 	int set;
6684b88c807SRodney W. Grimes 	int special;
6694b88c807SRodney W. Grimes 	int startloc;
670aa9caaf6SPeter Wemm 	int varlen;
6714c244ed2SJilles Tjoelker 	int varlenb;
67247d8814cSJilles Tjoelker 	char buf[21];
6734b88c807SRodney W. Grimes 
674bb4f73caSStefan Farfeleder 	varflags = (unsigned char)*p++;
6754b88c807SRodney W. Grimes 	subtype = varflags & VSTYPE;
6764b88c807SRodney W. Grimes 	var = p;
6774b88c807SRodney W. Grimes 	special = 0;
6784b88c807SRodney W. Grimes 	if (! is_name(*p))
6794b88c807SRodney W. Grimes 		special = 1;
6804b88c807SRodney W. Grimes 	p = strchr(p, '=') + 1;
681b71085aaSStefan Farfeleder 	if (varflags & VSLINENO) {
682b71085aaSStefan Farfeleder 		set = 1;
68354396489SJilles Tjoelker 		special = 1;
68454396489SJilles Tjoelker 		val = NULL;
685b71085aaSStefan Farfeleder 	} else if (special) {
68696522b88SSteve Price 		set = varisset(var, varflags & VSNUL);
6874b88c807SRodney W. Grimes 		val = NULL;
6884b88c807SRodney W. Grimes 	} else {
689b2acf887SMartin Cracauer 		val = bltinlookup(var, 1);
690aa9caaf6SPeter Wemm 		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
6914b88c807SRodney W. Grimes 			val = NULL;
6924b88c807SRodney W. Grimes 			set = 0;
6934b88c807SRodney W. Grimes 		} else
6944b88c807SRodney W. Grimes 			set = 1;
6954b88c807SRodney W. Grimes 	}
696aa9caaf6SPeter Wemm 	varlen = 0;
6974b88c807SRodney W. Grimes 	startloc = expdest - stackblock();
69864254a66SJilles Tjoelker 	if (!set && uflag && *var != '@' && *var != '*') {
6991b5a48ffSTim J. Robbins 		switch (subtype) {
7001b5a48ffSTim J. Robbins 		case VSNORMAL:
7011b5a48ffSTim J. Robbins 		case VSTRIMLEFT:
7021b5a48ffSTim J. Robbins 		case VSTRIMLEFTMAX:
7031b5a48ffSTim J. Robbins 		case VSTRIMRIGHT:
7041b5a48ffSTim J. Robbins 		case VSTRIMRIGHTMAX:
7051b5a48ffSTim J. Robbins 		case VSLENGTH:
706024ae004SRuslan Ermilov 			error("%.*s: parameter not set", (int)(p - var - 1),
707024ae004SRuslan Ermilov 			    var);
7081b5a48ffSTim J. Robbins 		}
7091b5a48ffSTim J. Robbins 	}
7104b88c807SRodney W. Grimes 	if (set && subtype != VSPLUS) {
7114b88c807SRodney W. Grimes 		/* insert the value of the variable */
7124b88c807SRodney W. Grimes 		if (special) {
71347d8814cSJilles Tjoelker 			if (varflags & VSLINENO) {
71447d8814cSJilles Tjoelker 				if (p - var > (ptrdiff_t)sizeof(buf))
71547d8814cSJilles Tjoelker 					abort();
71647d8814cSJilles Tjoelker 				memcpy(buf, var, p - var - 1);
71747d8814cSJilles Tjoelker 				buf[p - var - 1] = '\0';
71847d8814cSJilles Tjoelker 				strtodest(buf, flag, subtype,
71947d8814cSJilles Tjoelker 				    varflags & VSQUOTE, dst);
72047d8814cSJilles Tjoelker 			} else
72147d8814cSJilles Tjoelker 				varvalue(var, varflags & VSQUOTE, subtype, flag,
72247d8814cSJilles Tjoelker 				    dst);
723aa9caaf6SPeter Wemm 			if (subtype == VSLENGTH) {
7244c244ed2SJilles Tjoelker 				varlenb = expdest - stackblock() - startloc;
7254c244ed2SJilles Tjoelker 				varlen = varlenb;
7264c244ed2SJilles Tjoelker 				if (localeisutf8) {
7274c244ed2SJilles Tjoelker 					val = stackblock() + startloc;
7284c244ed2SJilles Tjoelker 					for (;val != expdest; val++)
7294c244ed2SJilles Tjoelker 						if ((*val & 0xC0) == 0x80)
7304c244ed2SJilles Tjoelker 							varlen--;
7314c244ed2SJilles Tjoelker 				}
7324c244ed2SJilles Tjoelker 				STADJUST(-varlenb, expdest);
733aa9caaf6SPeter Wemm 			}
7344b88c807SRodney W. Grimes 		} else {
735aa9caaf6SPeter Wemm 			if (subtype == VSLENGTH) {
736aa9caaf6SPeter Wemm 				for (;*val; val++)
7374c244ed2SJilles Tjoelker 					if (!localeisutf8 ||
7384c244ed2SJilles Tjoelker 					    (*val & 0xC0) != 0x80)
739aa9caaf6SPeter Wemm 						varlen++;
740aa9caaf6SPeter Wemm 			}
741f7dea851SJilles Tjoelker 			else
7427034d8dfSJilles Tjoelker 				strtodest(val, flag, subtype,
74347d8814cSJilles Tjoelker 				    varflags & VSQUOTE, dst);
7444b88c807SRodney W. Grimes 		}
745aa9caaf6SPeter Wemm 	}
746aa9caaf6SPeter Wemm 
7474b88c807SRodney W. Grimes 	if (subtype == VSPLUS)
7484b88c807SRodney W. Grimes 		set = ! set;
749aa9caaf6SPeter Wemm 
750aa9caaf6SPeter Wemm 	switch (subtype) {
751aa9caaf6SPeter Wemm 	case VSLENGTH:
75247d8814cSJilles Tjoelker 		cvtnum(varlen, buf);
75347d8814cSJilles Tjoelker 		strtodest(buf, flag, VSNORMAL, varflags & VSQUOTE, dst);
7547034d8dfSJilles Tjoelker 		break;
755aa9caaf6SPeter Wemm 
756aa9caaf6SPeter Wemm 	case VSNORMAL:
7579e1bb30eSJilles Tjoelker 		return p;
758aa9caaf6SPeter Wemm 
759aa9caaf6SPeter Wemm 	case VSPLUS:
760aa9caaf6SPeter Wemm 	case VSMINUS:
761aa9caaf6SPeter Wemm 		if (!set) {
7629e1bb30eSJilles Tjoelker 			return argstr(p, argbackq,
763439948cdSJilles Tjoelker 			    flag | (flag & EXP_SPLIT ? EXP_SPLIT_LIT : 0) |
76447d8814cSJilles Tjoelker 			    (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0), dst);
765aa9caaf6SPeter Wemm 		}
766aa9caaf6SPeter Wemm 		break;
767aa9caaf6SPeter Wemm 
768aa9caaf6SPeter Wemm 	case VSTRIMLEFT:
769aa9caaf6SPeter Wemm 	case VSTRIMLEFTMAX:
770aa9caaf6SPeter Wemm 	case VSTRIMRIGHT:
771aa9caaf6SPeter Wemm 	case VSTRIMRIGHTMAX:
7729e1bb30eSJilles Tjoelker 		if (!set)
773aa9caaf6SPeter Wemm 			break;
774aa9caaf6SPeter Wemm 		/*
775aa9caaf6SPeter Wemm 		 * Terminate the string and start recording the pattern
776aa9caaf6SPeter Wemm 		 * right after it
777aa9caaf6SPeter Wemm 		 */
778aa9caaf6SPeter Wemm 		STPUTC('\0', expdest);
779c4e5a8a8STor Egge 		patloc = expdest - stackblock();
780e2708b16SJilles Tjoelker 		p = subevalvar_trim(p, argbackq, patloc, subtype, startloc);
78147d8814cSJilles Tjoelker 		reprocess(startloc, flag, VSNORMAL, varflags & VSQUOTE, dst);
7820e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT && *var == '@' && varflags & VSQUOTE)
78347d8814cSJilles Tjoelker 			dst->state = WORD_QUOTEMARK;
784e2708b16SJilles Tjoelker 		return p;
785aa9caaf6SPeter Wemm 
786aa9caaf6SPeter Wemm 	case VSASSIGN:
787aa9caaf6SPeter Wemm 	case VSQUESTION:
788aa9caaf6SPeter Wemm 		if (!set) {
789e59833ccSJilles Tjoelker 			p = subevalvar_misc(p, argbackq, var, subtype,
79056bf1d61SJilles Tjoelker 			    startloc, varflags);
79156bf1d61SJilles Tjoelker 			/* assert(subtype == VSASSIGN); */
792e59833ccSJilles Tjoelker 			val = lookupvar(var);
793e59833ccSJilles Tjoelker 			strtodest(val, flag, subtype, varflags & VSQUOTE, dst);
794e59833ccSJilles Tjoelker 			return p;
795ab0a2172SSteve Price 		}
796aa9caaf6SPeter Wemm 		break;
797aa9caaf6SPeter Wemm 
79862addaefSStefan Farfeleder 	case VSERROR:
79962addaefSStefan Farfeleder 		c = p - var - 1;
80062addaefSStefan Farfeleder 		error("${%.*s%s}: Bad substitution", c, var,
80162addaefSStefan Farfeleder 		    (c > 0 && *p != CTLENDVAR) ? "..." : "");
80262addaefSStefan Farfeleder 
803aa9caaf6SPeter Wemm 	default:
804aa9caaf6SPeter Wemm 		abort();
8054b88c807SRodney W. Grimes 	}
806aa9caaf6SPeter Wemm 
8079e1bb30eSJilles Tjoelker 	{	/* skip to end of alternative */
8084b88c807SRodney W. Grimes 		int nesting = 1;
8094b88c807SRodney W. Grimes 		for (;;) {
8104b88c807SRodney W. Grimes 			if ((c = *p++) == CTLESC)
8114b88c807SRodney W. Grimes 				p++;
8129e1bb30eSJilles Tjoelker 			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE))
813439948cdSJilles Tjoelker 				*argbackq = (*argbackq)->next;
8149e1bb30eSJilles Tjoelker 			else if (c == CTLVAR) {
8154b88c807SRodney W. Grimes 				if ((*p++ & VSTYPE) != VSNORMAL)
8164b88c807SRodney W. Grimes 					nesting++;
8174b88c807SRodney W. Grimes 			} else if (c == CTLENDVAR) {
8184b88c807SRodney W. Grimes 				if (--nesting == 0)
8194b88c807SRodney W. Grimes 					break;
8204b88c807SRodney W. Grimes 			}
8214b88c807SRodney W. Grimes 		}
8224b88c807SRodney W. Grimes 	}
8234b88c807SRodney W. Grimes 	return p;
8244b88c807SRodney W. Grimes }
8254b88c807SRodney W. Grimes 
8264b88c807SRodney W. Grimes 
8274b88c807SRodney W. Grimes 
8284b88c807SRodney W. Grimes /*
8294b88c807SRodney W. Grimes  * Test whether a specialized variable is set.
8304b88c807SRodney W. Grimes  */
8314b88c807SRodney W. Grimes 
83288328642SDavid E. O'Brien static int
83361346cbdSJilles Tjoelker varisset(const char *name, int nulok)
8344b88c807SRodney W. Grimes {
8354b88c807SRodney W. Grimes 
83696522b88SSteve Price 	if (*name == '!')
837ed4c3b5fSJilles Tjoelker 		return backgndpidset();
83896522b88SSteve Price 	else if (*name == '@' || *name == '*') {
8394b88c807SRodney W. Grimes 		if (*shellparam.p == NULL)
8404b88c807SRodney W. Grimes 			return 0;
84196522b88SSteve Price 
84296522b88SSteve Price 		if (nulok) {
84396522b88SSteve Price 			char **av;
84496522b88SSteve Price 
84596522b88SSteve Price 			for (av = shellparam.p; *av; av++)
84696522b88SSteve Price 				if (**av != '\0')
84796522b88SSteve Price 					return 1;
84896522b88SSteve Price 			return 0;
84996522b88SSteve Price 		}
8505c817731SPeter Wemm 	} else if (is_digit(*name)) {
85196522b88SSteve Price 		char *ap;
8527b9104c0SJilles Tjoelker 		long num;
85396522b88SSteve Price 
8547b9104c0SJilles Tjoelker 		errno = 0;
8557b9104c0SJilles Tjoelker 		num = strtol(name, NULL, 10);
8567b9104c0SJilles Tjoelker 		if (errno != 0 || num > shellparam.nparam)
85796522b88SSteve Price 			return 0;
85896522b88SSteve Price 
85996522b88SSteve Price 		if (num == 0)
86096522b88SSteve Price 			ap = arg0;
86196522b88SSteve Price 		else
86296522b88SSteve Price 			ap = shellparam.p[num - 1];
86396522b88SSteve Price 
86496522b88SSteve Price 		if (nulok && (ap == NULL || *ap == '\0'))
8654b88c807SRodney W. Grimes 			return 0;
8664b88c807SRodney W. Grimes 	}
8674b88c807SRodney W. Grimes 	return 1;
8684b88c807SRodney W. Grimes }
8694b88c807SRodney W. Grimes 
870f7dea851SJilles Tjoelker static void
87147d8814cSJilles Tjoelker strtodest(const char *p, int flag, int subtype, int quoted,
87247d8814cSJilles Tjoelker     struct worddest *dst)
873f7dea851SJilles Tjoelker {
87447d8814cSJilles Tjoelker 	if (subtype == VSLENGTH || subtype == VSTRIMLEFT ||
87547d8814cSJilles Tjoelker 	    subtype == VSTRIMLEFTMAX || subtype == VSTRIMRIGHT ||
87647d8814cSJilles Tjoelker 	    subtype == VSTRIMRIGHTMAX)
87747d8814cSJilles Tjoelker 		STPUTS(p, expdest);
8780e39c931SJilles Tjoelker 	else if (flag & EXP_SPLIT && !quoted && dst != NULL)
8790e39c931SJilles Tjoelker 		STPUTS_SPLIT(p, BASESYNTAX, flag, expdest, dst);
8800e39c931SJilles Tjoelker 	else if (flag & (EXP_GLOB | EXP_CASE))
881f7dea851SJilles Tjoelker 		STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
882f7dea851SJilles Tjoelker 	else
883f7dea851SJilles Tjoelker 		STPUTS(p, expdest);
884f7dea851SJilles Tjoelker }
8854b88c807SRodney W. Grimes 
88647d8814cSJilles Tjoelker static void
88747d8814cSJilles Tjoelker reprocess(int startloc, int flag, int subtype, int quoted,
88847d8814cSJilles Tjoelker     struct worddest *dst)
88947d8814cSJilles Tjoelker {
89047d8814cSJilles Tjoelker 	static char *buf = NULL;
89147d8814cSJilles Tjoelker 	static size_t buflen = 0;
89247d8814cSJilles Tjoelker 	char *startp;
89347d8814cSJilles Tjoelker 	size_t len, zpos, zlen;
89447d8814cSJilles Tjoelker 
89547d8814cSJilles Tjoelker 	startp = stackblock() + startloc;
89647d8814cSJilles Tjoelker 	len = expdest - startp;
89747d8814cSJilles Tjoelker 	if (len >= SIZE_MAX / 2)
89847d8814cSJilles Tjoelker 		abort();
89947d8814cSJilles Tjoelker 	INTOFF;
90047d8814cSJilles Tjoelker 	if (len >= buflen) {
90147d8814cSJilles Tjoelker 		ckfree(buf);
90247d8814cSJilles Tjoelker 		buf = NULL;
90347d8814cSJilles Tjoelker 	}
90447d8814cSJilles Tjoelker 	if (buflen < 128)
90547d8814cSJilles Tjoelker 		buflen = 128;
90647d8814cSJilles Tjoelker 	while (len >= buflen)
90747d8814cSJilles Tjoelker 		buflen <<= 1;
90847d8814cSJilles Tjoelker 	if (buf == NULL)
90947d8814cSJilles Tjoelker 		buf = ckmalloc(buflen);
91047d8814cSJilles Tjoelker 	INTON;
91147d8814cSJilles Tjoelker 	memcpy(buf, startp, len);
91247d8814cSJilles Tjoelker 	buf[len] = '\0';
91347d8814cSJilles Tjoelker 	STADJUST(-len, expdest);
91447d8814cSJilles Tjoelker 	for (zpos = 0;;) {
91547d8814cSJilles Tjoelker 		zlen = strlen(buf + zpos);
91647d8814cSJilles Tjoelker 		strtodest(buf + zpos, flag, subtype, quoted, dst);
91747d8814cSJilles Tjoelker 		zpos += zlen + 1;
91847d8814cSJilles Tjoelker 		if (zpos == len + 1)
91947d8814cSJilles Tjoelker 			break;
9200e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT && (quoted || (zlen > 0 && zpos < len)))
9210e39c931SJilles Tjoelker 			NEXTWORD('\0', flag, expdest, dst);
92247d8814cSJilles Tjoelker 	}
92347d8814cSJilles Tjoelker }
92447d8814cSJilles Tjoelker 
9254b88c807SRodney W. Grimes /*
9264b88c807SRodney W. Grimes  * Add the value of a specialized variable to the stack string.
9274b88c807SRodney W. Grimes  */
9284b88c807SRodney W. Grimes 
92988328642SDavid E. O'Brien static void
93047d8814cSJilles Tjoelker varvalue(const char *name, int quoted, int subtype, int flag,
93147d8814cSJilles Tjoelker     struct worddest *dst)
9324b88c807SRodney W. Grimes {
9334b88c807SRodney W. Grimes 	int num;
9344b88c807SRodney W. Grimes 	char *p;
9354b88c807SRodney W. Grimes 	int i;
93647d8814cSJilles Tjoelker 	int splitlater;
9373fb51b3aSJilles Tjoelker 	char sep[2];
9384b88c807SRodney W. Grimes 	char **ap;
93947d8814cSJilles Tjoelker 	char buf[(NSHORTOPTS > 10 ? NSHORTOPTS : 10) + 1];
94047d8814cSJilles Tjoelker 
94147d8814cSJilles Tjoelker 	if (subtype == VSLENGTH)
94247d8814cSJilles Tjoelker 		flag &= ~EXP_FULL;
94347d8814cSJilles Tjoelker 	splitlater = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
94447d8814cSJilles Tjoelker 		subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX;
9454b88c807SRodney W. Grimes 
9465c817731SPeter Wemm 	switch (*name) {
9474b88c807SRodney W. Grimes 	case '$':
9484b88c807SRodney W. Grimes 		num = rootpid;
949622fdf32SJilles Tjoelker 		break;
9504b88c807SRodney W. Grimes 	case '?':
951aa9caaf6SPeter Wemm 		num = oexitstatus;
952622fdf32SJilles Tjoelker 		break;
9534b88c807SRodney W. Grimes 	case '#':
9544b88c807SRodney W. Grimes 		num = shellparam.nparam;
955622fdf32SJilles Tjoelker 		break;
9564b88c807SRodney W. Grimes 	case '!':
957ed4c3b5fSJilles Tjoelker 		num = backgndpidval();
9584b88c807SRodney W. Grimes 		break;
9594b88c807SRodney W. Grimes 	case '-':
96047d8814cSJilles Tjoelker 		p = buf;
96162c37116SJilles Tjoelker 		for (i = 0 ; i < NSHORTOPTS ; i++) {
9623da40d4aSJilles Tjoelker 			if (optval[i])
9633da40d4aSJilles Tjoelker 				*p++ = optletter[i];
9644b88c807SRodney W. Grimes 		}
96547d8814cSJilles Tjoelker 		*p = '\0';
96647d8814cSJilles Tjoelker 		strtodest(buf, flag, subtype, quoted, dst);
967622fdf32SJilles Tjoelker 		return;
9684b88c807SRodney W. Grimes 	case '@':
9690e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT && quoted) {
9704b88c807SRodney W. Grimes 			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
97147d8814cSJilles Tjoelker 				strtodest(p, flag, subtype, quoted, dst);
97247d8814cSJilles Tjoelker 				if (*ap) {
97347d8814cSJilles Tjoelker 					if (splitlater)
9746f47734fSTor Egge 						STPUTC('\0', expdest);
97547d8814cSJilles Tjoelker 					else
9760e39c931SJilles Tjoelker 						NEXTWORD('\0', flag, expdest,
9770e39c931SJilles Tjoelker 						    dst);
9786f47734fSTor Egge 				}
97947d8814cSJilles Tjoelker 			}
98047d8814cSJilles Tjoelker 			if (shellparam.nparam > 0)
98147d8814cSJilles Tjoelker 				dst->state = WORD_QUOTEMARK;
982622fdf32SJilles Tjoelker 			return;
9836f47734fSTor Egge 		}
9840d9f1a69SPhilippe Charnier 		/* FALLTHROUGH */
9856f47734fSTor Egge 	case '*':
986f7d95a07SRalf S. Engelschall 		if (ifsset())
9873fb51b3aSJilles Tjoelker 			sep[0] = ifsval()[0];
9886f47734fSTor Egge 		else
9893fb51b3aSJilles Tjoelker 			sep[0] = ' ';
9903fb51b3aSJilles Tjoelker 		sep[1] = '\0';
9916f47734fSTor Egge 		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
99247d8814cSJilles Tjoelker 			strtodest(p, flag, subtype, quoted, dst);
993715a0dd5SJilles Tjoelker 			if (!*ap)
994715a0dd5SJilles Tjoelker 				break;
9953fb51b3aSJilles Tjoelker 			if (sep[0])
99647d8814cSJilles Tjoelker 				strtodest(sep, flag, subtype, quoted, dst);
9970e39c931SJilles Tjoelker 			else if (flag & EXP_SPLIT && !quoted && **ap != '\0') {
99847d8814cSJilles Tjoelker 				if (splitlater)
9993fb51b3aSJilles Tjoelker 					STPUTC('\0', expdest);
100047d8814cSJilles Tjoelker 				else
10010e39c931SJilles Tjoelker 					NEXTWORD('\0', flag, expdest, dst);
100247d8814cSJilles Tjoelker 			}
10034b88c807SRodney W. Grimes 		}
1004622fdf32SJilles Tjoelker 		return;
10054b88c807SRodney W. Grimes 	default:
10065c817731SPeter Wemm 		if (is_digit(*name)) {
10075c817731SPeter Wemm 			num = atoi(name);
10085ddabb83SJilles Tjoelker 			if (num == 0)
10095ddabb83SJilles Tjoelker 				p = arg0;
10105ddabb83SJilles Tjoelker 			else if (num > 0 && num <= shellparam.nparam)
10115c817731SPeter Wemm 				p = shellparam.p[num - 1];
10125ddabb83SJilles Tjoelker 			else
1013622fdf32SJilles Tjoelker 				return;
101447d8814cSJilles Tjoelker 			strtodest(p, flag, subtype, quoted, dst);
10154b88c807SRodney W. Grimes 		}
1016622fdf32SJilles Tjoelker 		return;
10174b88c807SRodney W. Grimes 	}
101847d8814cSJilles Tjoelker 	cvtnum(num, buf);
101947d8814cSJilles Tjoelker 	strtodest(buf, flag, subtype, quoted, dst);
10204b88c807SRodney W. Grimes }
10214b88c807SRodney W. Grimes 
10224b88c807SRodney W. Grimes 
10234b88c807SRodney W. Grimes 
1024aa7b6f82SDavid E. O'Brien static char expdir[PATH_MAX];
1025c8a3d81fSJilles Tjoelker #define expdir_end (expdir + sizeof(expdir))
10264b88c807SRodney W. Grimes 
10272ca3d70fSJilles Tjoelker /*
10282ca3d70fSJilles Tjoelker  * Perform pathname generation and remove control characters.
1029bc57b4d4SJilles Tjoelker  * At this point, the only control characters should be CTLESC.
10308ef0ae8aSJilles Tjoelker  * The results are stored in the list dstlist.
10312ca3d70fSJilles Tjoelker  */
103288328642SDavid E. O'Brien static void
10330e39c931SJilles Tjoelker expandmeta(char *pattern, struct arglist *dstlist)
10344b88c807SRodney W. Grimes {
10354b88c807SRodney W. Grimes 	char *p;
10368ef0ae8aSJilles Tjoelker 	int firstmatch;
10374b88c807SRodney W. Grimes 	char c;
10384b88c807SRodney W. Grimes 
10398ef0ae8aSJilles Tjoelker 	firstmatch = dstlist->count;
10400e39c931SJilles Tjoelker 	p = pattern;
1041622fdf32SJilles Tjoelker 	for (; (c = *p) != '\0'; p++) {
1042622fdf32SJilles Tjoelker 		/* fast check for meta chars */
1043622fdf32SJilles Tjoelker 		if (c == '*' || c == '?' || c == '[') {
10444b88c807SRodney W. Grimes 			INTOFF;
10450e39c931SJilles Tjoelker 			expmeta(expdir, pattern, dstlist);
10464b88c807SRodney W. Grimes 			INTON;
1047622fdf32SJilles Tjoelker 			break;
1048622fdf32SJilles Tjoelker 		}
1049622fdf32SJilles Tjoelker 	}
10508ef0ae8aSJilles Tjoelker 	if (dstlist->count == firstmatch) {
10514b88c807SRodney W. Grimes 		/*
10524b88c807SRodney W. Grimes 		 * no matches
10534b88c807SRodney W. Grimes 		 */
10540e39c931SJilles Tjoelker 		rmescapes(pattern);
10550e39c931SJilles Tjoelker 		appendarglist(dstlist, pattern);
10564b88c807SRodney W. Grimes 	} else {
10578ef0ae8aSJilles Tjoelker 		qsort(&dstlist->args[firstmatch],
10588ef0ae8aSJilles Tjoelker 		    dstlist->count - firstmatch,
10598ef0ae8aSJilles Tjoelker 		    sizeof(dstlist->args[0]), expsortcmp);
10604b88c807SRodney W. Grimes 	}
10614b88c807SRodney W. Grimes }
10624b88c807SRodney W. Grimes 
10634b88c807SRodney W. Grimes 
10644b88c807SRodney W. Grimes /*
10654b88c807SRodney W. Grimes  * Do metacharacter (i.e. *, ?, [...]) expansion.
10664b88c807SRodney W. Grimes  */
10674b88c807SRodney W. Grimes 
106888328642SDavid E. O'Brien static void
10698ef0ae8aSJilles Tjoelker expmeta(char *enddir, char *name, struct arglist *arglist)
10704b88c807SRodney W. Grimes {
107146c6b52dSJilles Tjoelker 	const char *p;
107246c6b52dSJilles Tjoelker 	const char *q;
107346c6b52dSJilles Tjoelker 	const char *start;
10744b88c807SRodney W. Grimes 	char *endname;
10754b88c807SRodney W. Grimes 	int metaflag;
10764b88c807SRodney W. Grimes 	struct stat statb;
10774b88c807SRodney W. Grimes 	DIR *dirp;
10784b88c807SRodney W. Grimes 	struct dirent *dp;
10794b88c807SRodney W. Grimes 	int atend;
10804b88c807SRodney W. Grimes 	int matchdot;
10814710b07eSJilles Tjoelker 	int esc;
10827a2b9d4bSJilles Tjoelker 	int namlen;
10834b88c807SRodney W. Grimes 
10844b88c807SRodney W. Grimes 	metaflag = 0;
10854b88c807SRodney W. Grimes 	start = name;
10864710b07eSJilles Tjoelker 	for (p = name; esc = 0, *p; p += esc + 1) {
10874b88c807SRodney W. Grimes 		if (*p == '*' || *p == '?')
10884b88c807SRodney W. Grimes 			metaflag = 1;
10894b88c807SRodney W. Grimes 		else if (*p == '[') {
10904b88c807SRodney W. Grimes 			q = p + 1;
1091ea1376dfSAndrey A. Chernov 			if (*q == '!' || *q == '^')
10924b88c807SRodney W. Grimes 				q++;
10934b88c807SRodney W. Grimes 			for (;;) {
10944b88c807SRodney W. Grimes 				if (*q == CTLESC)
10954b88c807SRodney W. Grimes 					q++;
10964b88c807SRodney W. Grimes 				if (*q == '/' || *q == '\0')
10974b88c807SRodney W. Grimes 					break;
10984b88c807SRodney W. Grimes 				if (*++q == ']') {
10994b88c807SRodney W. Grimes 					metaflag = 1;
11004b88c807SRodney W. Grimes 					break;
11014b88c807SRodney W. Grimes 				}
11024b88c807SRodney W. Grimes 			}
11034b88c807SRodney W. Grimes 		} else if (*p == '\0')
11044b88c807SRodney W. Grimes 			break;
11054710b07eSJilles Tjoelker 		else {
11064710b07eSJilles Tjoelker 			if (*p == CTLESC)
11074710b07eSJilles Tjoelker 				esc++;
11084710b07eSJilles Tjoelker 			if (p[esc] == '/') {
11094b88c807SRodney W. Grimes 				if (metaflag)
11104b88c807SRodney W. Grimes 					break;
11114710b07eSJilles Tjoelker 				start = p + esc + 1;
11124710b07eSJilles Tjoelker 			}
11134b88c807SRodney W. Grimes 		}
11144b88c807SRodney W. Grimes 	}
11154b88c807SRodney W. Grimes 	if (metaflag == 0) {	/* we've reached the end of the file name */
11164b88c807SRodney W. Grimes 		if (enddir != expdir)
11174b88c807SRodney W. Grimes 			metaflag++;
11184b88c807SRodney W. Grimes 		for (p = name ; ; p++) {
11194b88c807SRodney W. Grimes 			if (*p == CTLESC)
11204b88c807SRodney W. Grimes 				p++;
11214b88c807SRodney W. Grimes 			*enddir++ = *p;
11224b88c807SRodney W. Grimes 			if (*p == '\0')
11234b88c807SRodney W. Grimes 				break;
1124c8a3d81fSJilles Tjoelker 			if (enddir == expdir_end)
1125c8a3d81fSJilles Tjoelker 				return;
11264b88c807SRodney W. Grimes 		}
11270e3e87bdSXin LI 		if (metaflag == 0 || lstat(expdir, &statb) >= 0)
11288ef0ae8aSJilles Tjoelker 			appendarglist(arglist, stsavestr(expdir));
11294b88c807SRodney W. Grimes 		return;
11304b88c807SRodney W. Grimes 	}
113146c6b52dSJilles Tjoelker 	endname = name + (p - name);
11324b88c807SRodney W. Grimes 	if (start != name) {
11334b88c807SRodney W. Grimes 		p = name;
11344b88c807SRodney W. Grimes 		while (p < start) {
11354b88c807SRodney W. Grimes 			if (*p == CTLESC)
11364b88c807SRodney W. Grimes 				p++;
11374b88c807SRodney W. Grimes 			*enddir++ = *p++;
1138c8a3d81fSJilles Tjoelker 			if (enddir == expdir_end)
1139c8a3d81fSJilles Tjoelker 				return;
11404b88c807SRodney W. Grimes 		}
11414b88c807SRodney W. Grimes 	}
11424b88c807SRodney W. Grimes 	if (enddir == expdir) {
11434b88c807SRodney W. Grimes 		p = ".";
11444b88c807SRodney W. Grimes 	} else if (enddir == expdir + 1 && *expdir == '/') {
11454b88c807SRodney W. Grimes 		p = "/";
11464b88c807SRodney W. Grimes 	} else {
11474b88c807SRodney W. Grimes 		p = expdir;
11484b88c807SRodney W. Grimes 		enddir[-1] = '\0';
11494b88c807SRodney W. Grimes 	}
11504b88c807SRodney W. Grimes 	if ((dirp = opendir(p)) == NULL)
11514b88c807SRodney W. Grimes 		return;
11524b88c807SRodney W. Grimes 	if (enddir != expdir)
11534b88c807SRodney W. Grimes 		enddir[-1] = '/';
11544b88c807SRodney W. Grimes 	if (*endname == 0) {
11554b88c807SRodney W. Grimes 		atend = 1;
11564b88c807SRodney W. Grimes 	} else {
11574b88c807SRodney W. Grimes 		atend = 0;
11584710b07eSJilles Tjoelker 		*endname = '\0';
11594710b07eSJilles Tjoelker 		endname += esc + 1;
11604b88c807SRodney W. Grimes 	}
11614b88c807SRodney W. Grimes 	matchdot = 0;
11626f47734fSTor Egge 	p = start;
11636f47734fSTor Egge 	if (*p == CTLESC)
11646f47734fSTor Egge 		p++;
11656f47734fSTor Egge 	if (*p == '.')
11664b88c807SRodney W. Grimes 		matchdot++;
11674b88c807SRodney W. Grimes 	while (! int_pending() && (dp = readdir(dirp)) != NULL) {
11684b88c807SRodney W. Grimes 		if (dp->d_name[0] == '.' && ! matchdot)
11694b88c807SRodney W. Grimes 			continue;
117047d8814cSJilles Tjoelker 		if (patmatch(start, dp->d_name)) {
11717a2b9d4bSJilles Tjoelker 			namlen = dp->d_namlen;
11727a2b9d4bSJilles Tjoelker 			if (enddir + namlen + 1 > expdir_end)
1173aa9caaf6SPeter Wemm 				continue;
11747a2b9d4bSJilles Tjoelker 			memcpy(enddir, dp->d_name, namlen + 1);
1175c8a3d81fSJilles Tjoelker 			if (atend)
11768ef0ae8aSJilles Tjoelker 				appendarglist(arglist, stsavestr(expdir));
1177c8a3d81fSJilles Tjoelker 			else {
11786e8db49aSJilles Tjoelker 				if (dp->d_type != DT_UNKNOWN &&
11796e8db49aSJilles Tjoelker 				    dp->d_type != DT_DIR &&
11806e8db49aSJilles Tjoelker 				    dp->d_type != DT_LNK)
11816e8db49aSJilles Tjoelker 					continue;
11827a2b9d4bSJilles Tjoelker 				if (enddir + namlen + 2 > expdir_end)
1183c8a3d81fSJilles Tjoelker 					continue;
11847a2b9d4bSJilles Tjoelker 				enddir[namlen] = '/';
11857a2b9d4bSJilles Tjoelker 				enddir[namlen + 1] = '\0';
11868ef0ae8aSJilles Tjoelker 				expmeta(enddir + namlen + 1, endname, arglist);
11874b88c807SRodney W. Grimes 			}
11884b88c807SRodney W. Grimes 		}
11894b88c807SRodney W. Grimes 	}
11904b88c807SRodney W. Grimes 	closedir(dirp);
11914b88c807SRodney W. Grimes 	if (! atend)
11924710b07eSJilles Tjoelker 		endname[-esc - 1] = esc ? CTLESC : '/';
11934b88c807SRodney W. Grimes }
11944b88c807SRodney W. Grimes 
11954b88c807SRodney W. Grimes 
11968ef0ae8aSJilles Tjoelker static int
11978ef0ae8aSJilles Tjoelker expsortcmp(const void *p1, const void *p2)
11984b88c807SRodney W. Grimes {
11998ef0ae8aSJilles Tjoelker 	const char *s1 = *(const char * const *)p1;
12008ef0ae8aSJilles Tjoelker 	const char *s2 = *(const char * const *)p2;
12014b88c807SRodney W. Grimes 
1202143d321aSAndrey A. Chernov 	return (strcoll(s1, s2));
12034b88c807SRodney W. Grimes }
12044b88c807SRodney W. Grimes 
12054b88c807SRodney W. Grimes 
12064b88c807SRodney W. Grimes 
12077cc6b3dfSJilles Tjoelker static wchar_t
12087cc6b3dfSJilles Tjoelker get_wc(const char **p)
12097cc6b3dfSJilles Tjoelker {
12107cc6b3dfSJilles Tjoelker 	wchar_t c;
12117cc6b3dfSJilles Tjoelker 	int chrlen;
12127cc6b3dfSJilles Tjoelker 
12137cc6b3dfSJilles Tjoelker 	chrlen = mbtowc(&c, *p, 4);
12147cc6b3dfSJilles Tjoelker 	if (chrlen == 0)
12157cc6b3dfSJilles Tjoelker 		return 0;
12167cc6b3dfSJilles Tjoelker 	else if (chrlen == -1)
12177cc6b3dfSJilles Tjoelker 		c = 0;
12187cc6b3dfSJilles Tjoelker 	else
12197cc6b3dfSJilles Tjoelker 		*p += chrlen;
12207cc6b3dfSJilles Tjoelker 	return c;
12217cc6b3dfSJilles Tjoelker }
12227cc6b3dfSJilles Tjoelker 
12237cc6b3dfSJilles Tjoelker 
12244b88c807SRodney W. Grimes /*
1225ff4dc672SJilles Tjoelker  * See if a character matches a character class, starting at the first colon
1226ff4dc672SJilles Tjoelker  * of "[:class:]".
1227ff4dc672SJilles Tjoelker  * If a valid character class is recognized, a pointer to the next character
1228ff4dc672SJilles Tjoelker  * after the final closing bracket is stored into *end, otherwise a null
1229ff4dc672SJilles Tjoelker  * pointer is stored into *end.
1230ff4dc672SJilles Tjoelker  */
1231ff4dc672SJilles Tjoelker static int
1232ff4dc672SJilles Tjoelker match_charclass(const char *p, wchar_t chr, const char **end)
1233ff4dc672SJilles Tjoelker {
1234ff4dc672SJilles Tjoelker 	char name[20];
1235ff4dc672SJilles Tjoelker 	const char *nameend;
1236ff4dc672SJilles Tjoelker 	wctype_t cclass;
1237ff4dc672SJilles Tjoelker 
1238ff4dc672SJilles Tjoelker 	*end = NULL;
1239ff4dc672SJilles Tjoelker 	p++;
1240ff4dc672SJilles Tjoelker 	nameend = strstr(p, ":]");
124146c6b52dSJilles Tjoelker 	if (nameend == NULL || (size_t)(nameend - p) >= sizeof(name) ||
124246c6b52dSJilles Tjoelker 	    nameend == p)
1243ff4dc672SJilles Tjoelker 		return 0;
1244ff4dc672SJilles Tjoelker 	memcpy(name, p, nameend - p);
1245ff4dc672SJilles Tjoelker 	name[nameend - p] = '\0';
1246ff4dc672SJilles Tjoelker 	*end = nameend + 2;
1247ff4dc672SJilles Tjoelker 	cclass = wctype(name);
1248ff4dc672SJilles Tjoelker 	/* An unknown class matches nothing but is valid nevertheless. */
1249ff4dc672SJilles Tjoelker 	if (cclass == 0)
1250ff4dc672SJilles Tjoelker 		return 0;
1251ff4dc672SJilles Tjoelker 	return iswctype(chr, cclass);
1252ff4dc672SJilles Tjoelker }
1253ff4dc672SJilles Tjoelker 
1254ff4dc672SJilles Tjoelker 
1255ff4dc672SJilles Tjoelker /*
12564b88c807SRodney W. Grimes  * Returns true if the pattern matches the string.
12574b88c807SRodney W. Grimes  */
12584b88c807SRodney W. Grimes 
1259260fc3f4SJilles Tjoelker static int
126047d8814cSJilles Tjoelker patmatch(const char *pattern, const char *string)
12614b88c807SRodney W. Grimes {
1262ff4dc672SJilles Tjoelker 	const char *p, *q, *end;
1263820491f8SJilles Tjoelker 	const char *bt_p, *bt_q;
126496522b88SSteve Price 	char c;
12657cc6b3dfSJilles Tjoelker 	wchar_t wc, wc2;
12664b88c807SRodney W. Grimes 
12674b88c807SRodney W. Grimes 	p = pattern;
12684b88c807SRodney W. Grimes 	q = string;
1269820491f8SJilles Tjoelker 	bt_p = NULL;
1270820491f8SJilles Tjoelker 	bt_q = NULL;
12714b88c807SRodney W. Grimes 	for (;;) {
12724b88c807SRodney W. Grimes 		switch (c = *p++) {
12734b88c807SRodney W. Grimes 		case '\0':
1274820491f8SJilles Tjoelker 			if (*q != '\0')
1275820491f8SJilles Tjoelker 				goto backtrack;
1276820491f8SJilles Tjoelker 			return 1;
12774b88c807SRodney W. Grimes 		case CTLESC:
12784b88c807SRodney W. Grimes 			if (*q++ != *p++)
1279820491f8SJilles Tjoelker 				goto backtrack;
12804b88c807SRodney W. Grimes 			break;
12814b88c807SRodney W. Grimes 		case '?':
1282820491f8SJilles Tjoelker 			if (*q == '\0')
12834b88c807SRodney W. Grimes 				return 0;
1284820491f8SJilles Tjoelker 			if (localeisutf8) {
1285820491f8SJilles Tjoelker 				wc = get_wc(&q);
1286820491f8SJilles Tjoelker 				/*
1287820491f8SJilles Tjoelker 				 * A '?' does not match invalid UTF-8 but a
1288820491f8SJilles Tjoelker 				 * '*' does, so backtrack.
1289820491f8SJilles Tjoelker 				 */
1290820491f8SJilles Tjoelker 				if (wc == 0)
1291820491f8SJilles Tjoelker 					goto backtrack;
1292820491f8SJilles Tjoelker 			} else
1293acb4eadaSJilles Tjoelker 				q++;
12944b88c807SRodney W. Grimes 			break;
12954b88c807SRodney W. Grimes 		case '*':
12964b88c807SRodney W. Grimes 			c = *p;
1297bc57b4d4SJilles Tjoelker 			while (c == '*')
12986f47734fSTor Egge 				c = *++p;
1299820491f8SJilles Tjoelker 			/*
1300820491f8SJilles Tjoelker 			 * If the pattern ends here, we know the string
1301820491f8SJilles Tjoelker 			 * matches without needing to look at the rest of it.
1302820491f8SJilles Tjoelker 			 */
1303820491f8SJilles Tjoelker 			if (c == '\0')
13044b88c807SRodney W. Grimes 				return 1;
1305820491f8SJilles Tjoelker 			/*
1306820491f8SJilles Tjoelker 			 * First try the shortest match for the '*' that
1307820491f8SJilles Tjoelker 			 * could work. We can forget any earlier '*' since
1308820491f8SJilles Tjoelker 			 * there is no way having it match more characters
1309820491f8SJilles Tjoelker 			 * can help us, given that we are already here.
1310820491f8SJilles Tjoelker 			 */
1311820491f8SJilles Tjoelker 			bt_p = p;
1312820491f8SJilles Tjoelker 			bt_q = q;
1313820491f8SJilles Tjoelker 			break;
13144b88c807SRodney W. Grimes 		case '[': {
13154a4867d6SJilles Tjoelker 			const char *savep, *saveq;
13164b88c807SRodney W. Grimes 			int invert, found;
13177cc6b3dfSJilles Tjoelker 			wchar_t chr;
13184b88c807SRodney W. Grimes 
13194a4867d6SJilles Tjoelker 			savep = p, saveq = q;
13204b88c807SRodney W. Grimes 			invert = 0;
1321ea1376dfSAndrey A. Chernov 			if (*p == '!' || *p == '^') {
13224b88c807SRodney W. Grimes 				invert++;
13234b88c807SRodney W. Grimes 				p++;
13244b88c807SRodney W. Grimes 			}
13254b88c807SRodney W. Grimes 			found = 0;
1326820491f8SJilles Tjoelker 			if (*q == '\0')
1327aa9caaf6SPeter Wemm 				return 0;
1328820491f8SJilles Tjoelker 			if (localeisutf8) {
1329820491f8SJilles Tjoelker 				chr = get_wc(&q);
1330820491f8SJilles Tjoelker 				if (chr == 0)
1331820491f8SJilles Tjoelker 					goto backtrack;
1332820491f8SJilles Tjoelker 			} else
1333820491f8SJilles Tjoelker 				chr = (unsigned char)*q++;
13344b88c807SRodney W. Grimes 			c = *p++;
13354b88c807SRodney W. Grimes 			do {
13364a4867d6SJilles Tjoelker 				if (c == '\0') {
13374a4867d6SJilles Tjoelker 					p = savep, q = saveq;
13384a4867d6SJilles Tjoelker 					c = '[';
13394a4867d6SJilles Tjoelker 					goto dft;
13404a4867d6SJilles Tjoelker 				}
1341ff4dc672SJilles Tjoelker 				if (c == '[' && *p == ':') {
1342ff4dc672SJilles Tjoelker 					found |= match_charclass(p, chr, &end);
1343ff4dc672SJilles Tjoelker 					if (end != NULL)
1344ff4dc672SJilles Tjoelker 						p = end;
1345ff4dc672SJilles Tjoelker 				}
13464b88c807SRodney W. Grimes 				if (c == CTLESC)
13474b88c807SRodney W. Grimes 					c = *p++;
13487cc6b3dfSJilles Tjoelker 				if (localeisutf8 && c & 0x80) {
13497cc6b3dfSJilles Tjoelker 					p--;
13507cc6b3dfSJilles Tjoelker 					wc = get_wc(&p);
13517cc6b3dfSJilles Tjoelker 					if (wc == 0) /* bad utf-8 */
13527cc6b3dfSJilles Tjoelker 						return 0;
13537cc6b3dfSJilles Tjoelker 				} else
1354f5ac5937SJilles Tjoelker 					wc = (unsigned char)c;
13554b88c807SRodney W. Grimes 				if (*p == '-' && p[1] != ']') {
13564b88c807SRodney W. Grimes 					p++;
13574b88c807SRodney W. Grimes 					if (*p == CTLESC)
13584b88c807SRodney W. Grimes 						p++;
13597cc6b3dfSJilles Tjoelker 					if (localeisutf8) {
13607cc6b3dfSJilles Tjoelker 						wc2 = get_wc(&p);
13617cc6b3dfSJilles Tjoelker 						if (wc2 == 0) /* bad utf-8 */
13627cc6b3dfSJilles Tjoelker 							return 0;
13637cc6b3dfSJilles Tjoelker 					} else
1364f5ac5937SJilles Tjoelker 						wc2 = (unsigned char)*p++;
1365fa93fc65SAndrey A. Chernov 					if (   collate_range_cmp(chr, wc) >= 0
1366fa93fc65SAndrey A. Chernov 					    && collate_range_cmp(chr, wc2) <= 0
1367fa93fc65SAndrey A. Chernov 					   )
13684b88c807SRodney W. Grimes 						found = 1;
13694b88c807SRodney W. Grimes 				} else {
13707cc6b3dfSJilles Tjoelker 					if (chr == wc)
13714b88c807SRodney W. Grimes 						found = 1;
13724b88c807SRodney W. Grimes 				}
13734b88c807SRodney W. Grimes 			} while ((c = *p++) != ']');
13744b88c807SRodney W. Grimes 			if (found == invert)
1375820491f8SJilles Tjoelker 				goto backtrack;
13764b88c807SRodney W. Grimes 			break;
13774b88c807SRodney W. Grimes 		}
13784b88c807SRodney W. Grimes dft:	        default:
1379820491f8SJilles Tjoelker 			if (*q == '\0')
13804b88c807SRodney W. Grimes 				return 0;
1381820491f8SJilles Tjoelker 			if (*q++ == c)
1382820491f8SJilles Tjoelker 				break;
1383820491f8SJilles Tjoelker backtrack:
1384820491f8SJilles Tjoelker 			/*
1385820491f8SJilles Tjoelker 			 * If we have a mismatch (other than hitting the end
1386820491f8SJilles Tjoelker 			 * of the string), go back to the last '*' seen and
1387820491f8SJilles Tjoelker 			 * have it match one additional character.
1388820491f8SJilles Tjoelker 			 */
1389820491f8SJilles Tjoelker 			if (bt_p == NULL)
1390820491f8SJilles Tjoelker 				return 0;
1391820491f8SJilles Tjoelker 			if (*bt_q == '\0')
1392820491f8SJilles Tjoelker 				return 0;
1393820491f8SJilles Tjoelker 			bt_q++;
1394820491f8SJilles Tjoelker 			p = bt_p;
1395820491f8SJilles Tjoelker 			q = bt_q;
13964b88c807SRodney W. Grimes 			break;
13974b88c807SRodney W. Grimes 		}
13984b88c807SRodney W. Grimes 	}
13994b88c807SRodney W. Grimes }
14004b88c807SRodney W. Grimes 
14014b88c807SRodney W. Grimes 
14024b88c807SRodney W. Grimes 
14034b88c807SRodney W. Grimes /*
14042ca3d70fSJilles Tjoelker  * Remove any CTLESC and CTLQUOTEMARK characters from a string.
14054b88c807SRodney W. Grimes  */
14064b88c807SRodney W. Grimes 
14074b88c807SRodney W. Grimes void
14085134c3f7SWarner Losh rmescapes(char *str)
14094b88c807SRodney W. Grimes {
141096522b88SSteve Price 	char *p, *q;
14114b88c807SRodney W. Grimes 
14124b88c807SRodney W. Grimes 	p = str;
1413048f2667SJilles Tjoelker 	while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) {
14144b88c807SRodney W. Grimes 		if (*p++ == '\0')
14154b88c807SRodney W. Grimes 			return;
14164b88c807SRodney W. Grimes 	}
14174b88c807SRodney W. Grimes 	q = p;
14184b88c807SRodney W. Grimes 	while (*p) {
1419048f2667SJilles Tjoelker 		if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
14206f47734fSTor Egge 			p++;
14216f47734fSTor Egge 			continue;
14226f47734fSTor Egge 		}
14234b88c807SRodney W. Grimes 		if (*p == CTLESC)
14244b88c807SRodney W. Grimes 			p++;
14254b88c807SRodney W. Grimes 		*q++ = *p++;
14264b88c807SRodney W. Grimes 	}
14274b88c807SRodney W. Grimes 	*q = '\0';
14284b88c807SRodney W. Grimes }
14294b88c807SRodney W. Grimes 
14304b88c807SRodney W. Grimes 
14314b88c807SRodney W. Grimes 
14324b88c807SRodney W. Grimes /*
14334b88c807SRodney W. Grimes  * See if a pattern matches in a case statement.
14344b88c807SRodney W. Grimes  */
14354b88c807SRodney W. Grimes 
14364b88c807SRodney W. Grimes int
14372cac6e36SJilles Tjoelker casematch(union node *pattern, const char *val)
14384b88c807SRodney W. Grimes {
14394b88c807SRodney W. Grimes 	struct stackmark smark;
1440439948cdSJilles Tjoelker 	struct nodelist *argbackq;
14414b88c807SRodney W. Grimes 	int result;
14424b88c807SRodney W. Grimes 	char *p;
14434b88c807SRodney W. Grimes 
14444b88c807SRodney W. Grimes 	setstackmark(&smark);
14454b88c807SRodney W. Grimes 	argbackq = pattern->narg.backquote;
14464b88c807SRodney W. Grimes 	STARTSTACKSTR(expdest);
1447439948cdSJilles Tjoelker 	argstr(pattern->narg.text, &argbackq, EXP_TILDE | EXP_CASE, NULL);
14484b88c807SRodney W. Grimes 	STPUTC('\0', expdest);
14494b88c807SRodney W. Grimes 	p = grabstackstr(expdest);
145047d8814cSJilles Tjoelker 	result = patmatch(p, val);
14514b88c807SRodney W. Grimes 	popstackmark(&smark);
14524b88c807SRodney W. Grimes 	return result;
14534b88c807SRodney W. Grimes }
1454aa9caaf6SPeter Wemm 
1455aa9caaf6SPeter Wemm /*
1456aa9caaf6SPeter Wemm  * Our own itoa().
1457aa9caaf6SPeter Wemm  */
1458aa9caaf6SPeter Wemm 
145947d8814cSJilles Tjoelker static void
14605134c3f7SWarner Losh cvtnum(int num, char *buf)
1461aa9caaf6SPeter Wemm {
1462aa9caaf6SPeter Wemm 	char temp[32];
1463aa9caaf6SPeter Wemm 	int neg = num < 0;
1464aa9caaf6SPeter Wemm 	char *p = temp + 31;
1465aa9caaf6SPeter Wemm 
1466aa9caaf6SPeter Wemm 	temp[31] = '\0';
1467aa9caaf6SPeter Wemm 
1468aa9caaf6SPeter Wemm 	do {
1469aa9caaf6SPeter Wemm 		*--p = num % 10 + '0';
1470aa9caaf6SPeter Wemm 	} while ((num /= 10) != 0);
1471aa9caaf6SPeter Wemm 
1472aa9caaf6SPeter Wemm 	if (neg)
1473aa9caaf6SPeter Wemm 		*--p = '-';
1474aa9caaf6SPeter Wemm 
147547d8814cSJilles Tjoelker 	memcpy(buf, p, temp + 32 - p);
1476aa9caaf6SPeter Wemm }
14772c25061fSTim J. Robbins 
14782c25061fSTim J. Robbins /*
14792c25061fSTim J. Robbins  * Do most of the work for wordexp(3).
14802c25061fSTim J. Robbins  */
14812c25061fSTim J. Robbins 
14822c25061fSTim J. Robbins int
14832c25061fSTim J. Robbins wordexpcmd(int argc, char **argv)
14842c25061fSTim J. Robbins {
14852c25061fSTim J. Robbins 	size_t len;
14862c25061fSTim J. Robbins 	int i;
14872c25061fSTim J. Robbins 
14882c25061fSTim J. Robbins 	out1fmt("%08x", argc - 1);
14892c25061fSTim J. Robbins 	for (i = 1, len = 0; i < argc; i++)
14902c25061fSTim J. Robbins 		len += strlen(argv[i]);
14912c25061fSTim J. Robbins 	out1fmt("%08x", (int)len);
1492aeb5d065SJilles Tjoelker 	for (i = 1; i < argc; i++)
1493aeb5d065SJilles Tjoelker 		outbin(argv[i], strlen(argv[i]) + 1, out1);
14942c25061fSTim J. Robbins         return (0);
14952c25061fSTim J. Robbins }
1496d358fa78SJilles Tjoelker 
1497d358fa78SJilles Tjoelker /*
1498d358fa78SJilles Tjoelker  * Do most of the work for wordexp(3), new version.
1499d358fa78SJilles Tjoelker  */
1500d358fa78SJilles Tjoelker 
1501d358fa78SJilles Tjoelker int
1502d358fa78SJilles Tjoelker freebsd_wordexpcmd(int argc __unused, char **argv __unused)
1503d358fa78SJilles Tjoelker {
1504d358fa78SJilles Tjoelker 	struct arglist arglist;
1505d358fa78SJilles Tjoelker 	union node *args, *n;
15068ef0ae8aSJilles Tjoelker 	size_t len;
1507d358fa78SJilles Tjoelker 	int ch;
1508d358fa78SJilles Tjoelker 	int protected = 0;
1509d358fa78SJilles Tjoelker 	int fd = -1;
15108ef0ae8aSJilles Tjoelker 	int i;
1511d358fa78SJilles Tjoelker 
1512d358fa78SJilles Tjoelker 	while ((ch = nextopt("f:p")) != '\0') {
1513d358fa78SJilles Tjoelker 		switch (ch) {
1514d358fa78SJilles Tjoelker 		case 'f':
1515d358fa78SJilles Tjoelker 			fd = number(shoptarg);
1516d358fa78SJilles Tjoelker 			break;
1517d358fa78SJilles Tjoelker 		case 'p':
1518d358fa78SJilles Tjoelker 			protected = 1;
1519d358fa78SJilles Tjoelker 			break;
1520d358fa78SJilles Tjoelker 		}
1521d358fa78SJilles Tjoelker 	}
1522d358fa78SJilles Tjoelker 	if (*argptr != NULL)
1523d358fa78SJilles Tjoelker 		error("wrong number of arguments");
1524d358fa78SJilles Tjoelker 	if (fd < 0)
1525d358fa78SJilles Tjoelker 		error("missing fd");
1526d358fa78SJilles Tjoelker 	INTOFF;
1527d358fa78SJilles Tjoelker 	setinputfd(fd, 1);
1528d358fa78SJilles Tjoelker 	INTON;
1529d358fa78SJilles Tjoelker 	args = parsewordexp();
1530d358fa78SJilles Tjoelker 	popfile(); /* will also close fd */
1531d358fa78SJilles Tjoelker 	if (protected)
1532d358fa78SJilles Tjoelker 		for (n = args; n != NULL; n = n->narg.next) {
1533d358fa78SJilles Tjoelker 			if (n->narg.backquote != NULL) {
1534d358fa78SJilles Tjoelker 				outcslow('C', out1);
1535d358fa78SJilles Tjoelker 				error("command substitution disabled");
1536d358fa78SJilles Tjoelker 			}
1537d358fa78SJilles Tjoelker 		}
1538d358fa78SJilles Tjoelker 	outcslow(' ', out1);
15398ef0ae8aSJilles Tjoelker 	emptyarglist(&arglist);
1540d358fa78SJilles Tjoelker 	for (n = args; n != NULL; n = n->narg.next)
1541d358fa78SJilles Tjoelker 		expandarg(n, &arglist, EXP_FULL | EXP_TILDE);
15428ef0ae8aSJilles Tjoelker 	for (i = 0, len = 0; i < arglist.count; i++)
15438ef0ae8aSJilles Tjoelker 		len += strlen(arglist.args[i]);
15448ef0ae8aSJilles Tjoelker 	out1fmt("%016x %016zx", arglist.count, len);
15458ef0ae8aSJilles Tjoelker 	for (i = 0; i < arglist.count; i++)
15468ef0ae8aSJilles Tjoelker 		outbin(arglist.args[i], strlen(arglist.args[i]) + 1, out1);
1547d358fa78SJilles Tjoelker 	return (0);
1548d358fa78SJilles Tjoelker }
1549