xref: /freebsd/bin/sh/expand.c (revision 3da40d4a6b80386c9fe890bb2f6577bca834c17e)
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.
204b88c807SRodney W. Grimes  * 4. 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 
9447d8814cSJilles Tjoelker static char *argstr(char *, int, struct worddest *);
9588328642SDavid E. O'Brien static char *exptilde(char *, int);
9647d8814cSJilles Tjoelker static char *expari(char *, int, struct worddest *);
9747d8814cSJilles Tjoelker static void expbackq(union node *, int, int, struct worddest *);
9847d8814cSJilles Tjoelker static void subevalvar_trim(char *, int, int, int);
994f2f5faaSJilles Tjoelker static int subevalvar_misc(char *, const char *, int, int, int);
10047d8814cSJilles Tjoelker static char *evalvar(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 *);
1107cc6b3dfSJilles Tjoelker 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 
14188328642SDavid E. O'Brien static int
1427cc6b3dfSJilles Tjoelker collate_range_cmp(wchar_t c1, wchar_t c2)
1433cd859a7SAndrey A. Chernov {
1447cc6b3dfSJilles Tjoelker 	static wchar_t s1[2], s2[2];
1453cd859a7SAndrey A. Chernov 
1463cd859a7SAndrey A. Chernov 	s1[0] = c1;
1473cd859a7SAndrey A. Chernov 	s2[0] = c2;
1487cc6b3dfSJilles Tjoelker 	return (wcscoll(s1, s2));
1493cd859a7SAndrey A. Chernov }
1504b88c807SRodney W. Grimes 
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  */
265a2cba42fSJilles Tjoelker static char *
26647d8814cSJilles Tjoelker argstr(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  */
35588328642SDavid E. O'Brien static char *
3565134c3f7SWarner Losh exptilde(char *p, int flag)
3574b88c807SRodney W. Grimes {
3584b88c807SRodney W. Grimes 	char c, *startp = p;
3594b88c807SRodney W. Grimes 	struct passwd *pw;
3604b88c807SRodney W. Grimes 	char *home;
3614b88c807SRodney W. Grimes 
3627034d8dfSJilles Tjoelker 	for (;;) {
3637034d8dfSJilles Tjoelker 		c = *p;
3644b88c807SRodney W. Grimes 		switch(c) {
36505c10507SJilles Tjoelker 		case CTLESC: /* This means CTL* are always considered quoted. */
36605c10507SJilles Tjoelker 		case CTLVAR:
36705c10507SJilles Tjoelker 		case CTLBACKQ:
36805c10507SJilles Tjoelker 		case CTLBACKQ | CTLQUOTE:
36905c10507SJilles Tjoelker 		case CTLARI:
37005c10507SJilles Tjoelker 		case CTLENDARI:
3715557a02aSTor Egge 		case CTLQUOTEMARK:
3725557a02aSTor Egge 			return (startp);
3734b88c807SRodney W. Grimes 		case ':':
3747034d8dfSJilles Tjoelker 			if ((flag & EXP_VARTILDE) == 0)
3754b88c807SRodney W. Grimes 				break;
3767034d8dfSJilles Tjoelker 			/* FALLTHROUGH */
3777034d8dfSJilles Tjoelker 		case '\0':
3784b88c807SRodney W. Grimes 		case '/':
379634e9188SJilles Tjoelker 		case CTLENDVAR:
3804b88c807SRodney W. Grimes 			*p = '\0';
3814b88c807SRodney W. Grimes 			if (*(startp+1) == '\0') {
38233c5acf0SJilles Tjoelker 				home = lookupvar("HOME");
3834b88c807SRodney W. Grimes 			} else {
38433c5acf0SJilles Tjoelker 				pw = getpwnam(startp+1);
38533c5acf0SJilles Tjoelker 				home = pw != NULL ? pw->pw_dir : NULL;
3864b88c807SRodney W. Grimes 			}
3874b88c807SRodney W. Grimes 			*p = c;
38833c5acf0SJilles Tjoelker 			if (home == NULL || *home == '\0')
38933c5acf0SJilles Tjoelker 				return (startp);
39047d8814cSJilles Tjoelker 			strtodest(home, flag, VSNORMAL, 1, NULL);
3914b88c807SRodney W. Grimes 			return (p);
3924b88c807SRodney W. Grimes 		}
3937034d8dfSJilles Tjoelker 		p++;
3947034d8dfSJilles Tjoelker 	}
3957034d8dfSJilles Tjoelker }
3964b88c807SRodney W. Grimes 
3974b88c807SRodney W. Grimes 
3984b88c807SRodney W. Grimes /*
399ce16da82SJilles Tjoelker  * Expand arithmetic expression.
4004b88c807SRodney W. Grimes  */
401ce16da82SJilles Tjoelker static char *
40247d8814cSJilles Tjoelker expari(char *p, int flag, struct worddest *dst)
4034b88c807SRodney W. Grimes {
404ce16da82SJilles Tjoelker 	char *q, *start;
405d9d588d4SStefan Farfeleder 	arith_t result;
4066f47734fSTor Egge 	int begoff;
4076f47734fSTor Egge 	int quoted;
408ce16da82SJilles Tjoelker 	int adj;
4094b88c807SRodney W. Grimes 
410ce16da82SJilles Tjoelker 	quoted = *p++ == '"';
411ce16da82SJilles Tjoelker 	begoff = expdest - stackblock();
41247d8814cSJilles Tjoelker 	p = argstr(p, 0, NULL);
413ce16da82SJilles Tjoelker 	STPUTC('\0', expdest);
414ce16da82SJilles Tjoelker 	start = stackblock() + begoff;
415ce16da82SJilles Tjoelker 
416593e925aSJilles Tjoelker 	q = grabstackstr(expdest);
417ce16da82SJilles Tjoelker 	result = arith(start);
418593e925aSJilles Tjoelker 	ungrabstackstr(q, expdest);
419ce16da82SJilles Tjoelker 
420ce16da82SJilles Tjoelker 	start = stackblock() + begoff;
421ce16da82SJilles Tjoelker 	adj = start - expdest;
422ce16da82SJilles Tjoelker 	STADJUST(adj, expdest);
423ce16da82SJilles Tjoelker 
424ce16da82SJilles Tjoelker 	CHECKSTRSPACE((int)(DIGITS(result) + 1), expdest);
425ce16da82SJilles Tjoelker 	fmtstr(expdest, DIGITS(result), ARITH_FORMAT_STR, result);
426ce16da82SJilles Tjoelker 	adj = strlen(expdest);
427ce16da82SJilles Tjoelker 	STADJUST(adj, expdest);
428ce16da82SJilles Tjoelker 	if (!quoted)
42947d8814cSJilles Tjoelker 		reprocess(expdest - adj - stackblock(), flag, VSNORMAL, 0, dst);
430ce16da82SJilles Tjoelker 	return p;
4314b88c807SRodney W. Grimes }
4324b88c807SRodney W. Grimes 
4334b88c807SRodney W. Grimes 
4344b88c807SRodney W. Grimes /*
4352ca3d70fSJilles Tjoelker  * Perform command substitution.
4364b88c807SRodney W. Grimes  */
43788328642SDavid E. O'Brien static void
43847d8814cSJilles Tjoelker expbackq(union node *cmd, int quoted, int flag, struct worddest *dst)
4394b88c807SRodney W. Grimes {
4404b88c807SRodney W. Grimes 	struct backcmd in;
4414b88c807SRodney W. Grimes 	int i;
4424b88c807SRodney W. Grimes 	char buf[128];
4434b88c807SRodney W. Grimes 	char *p;
4444b88c807SRodney W. Grimes 	char *dest = expdest;
4454b88c807SRodney W. Grimes 	struct nodelist *saveargbackq;
4464b88c807SRodney W. Grimes 	char lastc;
4474b88c807SRodney W. Grimes 	char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
4480e39c931SJilles Tjoelker 	int quotes = flag & (EXP_GLOB | EXP_CASE);
44946c6b52dSJilles Tjoelker 	size_t nnl;
45047d8814cSJilles Tjoelker 	const char *ifs;
4514b88c807SRodney W. Grimes 
4524b88c807SRodney W. Grimes 	INTOFF;
4534b88c807SRodney W. Grimes 	saveargbackq = argbackq;
4544b88c807SRodney W. Grimes 	p = grabstackstr(dest);
4554b88c807SRodney W. Grimes 	evalbackcmd(cmd, &in);
4564b88c807SRodney W. Grimes 	ungrabstackstr(p, dest);
4574b88c807SRodney W. Grimes 	argbackq = saveargbackq;
4584b88c807SRodney W. Grimes 
4594b88c807SRodney W. Grimes 	p = in.buf;
4604b88c807SRodney W. Grimes 	lastc = '\0';
46199907703SBill Fenner 	nnl = 0;
4620e39c931SJilles Tjoelker 	if (!quoted && flag & EXP_SPLIT)
46347d8814cSJilles Tjoelker 		ifs = ifsset() ? ifsval() : " \t\n";
46447d8814cSJilles Tjoelker 	else
46547d8814cSJilles Tjoelker 		ifs = "";
46699907703SBill Fenner 	/* Don't copy trailing newlines */
4674b88c807SRodney W. Grimes 	for (;;) {
4684b88c807SRodney W. Grimes 		if (--in.nleft < 0) {
4694b88c807SRodney W. Grimes 			if (in.fd < 0)
4704b88c807SRodney W. Grimes 				break;
4714b88c807SRodney W. Grimes 			while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
4724b88c807SRodney W. Grimes 			TRACE(("expbackq: read returns %d\n", i));
4734b88c807SRodney W. Grimes 			if (i <= 0)
4744b88c807SRodney W. Grimes 				break;
4754b88c807SRodney W. Grimes 			p = buf;
4764b88c807SRodney W. Grimes 			in.nleft = i - 1;
4774b88c807SRodney W. Grimes 		}
4784b88c807SRodney W. Grimes 		lastc = *p++;
47947d8814cSJilles Tjoelker 		if (lastc == '\0')
48047d8814cSJilles Tjoelker 			continue;
48199907703SBill Fenner 		if (lastc == '\n') {
48299907703SBill Fenner 			nnl++;
48399907703SBill Fenner 		} else {
48447d8814cSJilles Tjoelker 			if (nnl > 0) {
48547d8814cSJilles Tjoelker 				if (strchr(ifs, '\n') != NULL) {
4860e39c931SJilles Tjoelker 					NEXTWORD('\n', flag, dest, dst);
48747d8814cSJilles Tjoelker 					nnl = 0;
48847d8814cSJilles Tjoelker 				} else {
489d8f32e72SJilles Tjoelker 					CHECKSTRSPACE(nnl + 2, dest);
49099907703SBill Fenner 					while (nnl > 0) {
49199907703SBill Fenner 						nnl--;
492d8f32e72SJilles Tjoelker 						USTPUTC('\n', dest);
49399907703SBill Fenner 					}
49447d8814cSJilles Tjoelker 				}
49547d8814cSJilles Tjoelker 			}
49647d8814cSJilles Tjoelker 			if (strchr(ifs, lastc) != NULL)
4970e39c931SJilles Tjoelker 				NEXTWORD(lastc, flag, dest, dst);
49847d8814cSJilles Tjoelker 			else {
49947d8814cSJilles Tjoelker 				CHECKSTRSPACE(2, dest);
500fa0951d6SJilles Tjoelker 				if (quotes && syntax[(int)lastc] == CCTL)
501d8f32e72SJilles Tjoelker 					USTPUTC(CTLESC, dest);
502d8f32e72SJilles Tjoelker 				USTPUTC(lastc, dest);
5034b88c807SRodney W. Grimes 			}
5044b88c807SRodney W. Grimes 		}
50599907703SBill Fenner 	}
506aa9caaf6SPeter Wemm 
5074b88c807SRodney W. Grimes 	if (in.fd >= 0)
5084b88c807SRodney W. Grimes 		close(in.fd);
5094b88c807SRodney W. Grimes 	if (in.buf)
5104b88c807SRodney W. Grimes 		ckfree(in.buf);
5114b88c807SRodney W. Grimes 	if (in.jp)
51257b2932aSMartin Cracauer 		exitstatus = waitforjob(in.jp, (int *)NULL);
5138ab2e970SJohn Baldwin 	TRACE(("expbackq: size=%td: \"%.*s\"\n",
5148ab2e970SJohn Baldwin 		((dest - stackblock()) - startloc),
5158ab2e970SJohn Baldwin 		(int)((dest - stackblock()) - startloc),
5164b88c807SRodney W. Grimes 		stackblock() + startloc));
5174b88c807SRodney W. Grimes 	expdest = dest;
5184b88c807SRodney W. Grimes 	INTON;
5194b88c807SRodney W. Grimes }
5204b88c807SRodney W. Grimes 
5214b88c807SRodney W. Grimes 
5224b88c807SRodney W. Grimes 
5237034d8dfSJilles Tjoelker static void
5247034d8dfSJilles Tjoelker recordleft(const char *str, const char *loc, char *startp)
5257034d8dfSJilles Tjoelker {
5267034d8dfSJilles Tjoelker 	int amount;
5277034d8dfSJilles Tjoelker 
5287034d8dfSJilles Tjoelker 	amount = ((str - 1) - (loc - startp)) - expdest;
5297034d8dfSJilles Tjoelker 	STADJUST(amount, expdest);
5307034d8dfSJilles Tjoelker 	while (loc != str - 1)
5317034d8dfSJilles Tjoelker 		*startp++ = *loc++;
5327034d8dfSJilles Tjoelker }
5337034d8dfSJilles Tjoelker 
53447d8814cSJilles Tjoelker static void
53547d8814cSJilles Tjoelker subevalvar_trim(char *p, int strloc, int subtype, int startloc)
536aa9caaf6SPeter Wemm {
537aa9caaf6SPeter Wemm 	char *startp;
538aa9caaf6SPeter Wemm 	char *loc = NULL;
5394f2f5faaSJilles Tjoelker 	char *str;
540aa9caaf6SPeter Wemm 	int c = 0;
541aa9caaf6SPeter Wemm 	struct nodelist *saveargbackq = argbackq;
542ab0a2172SSteve Price 	int amount;
543ab0a2172SSteve Price 
54447d8814cSJilles Tjoelker 	argstr(p, EXP_CASE | EXP_TILDE, NULL);
545aa9caaf6SPeter Wemm 	STACKSTRNUL(expdest);
546aa9caaf6SPeter Wemm 	argbackq = saveargbackq;
547aa9caaf6SPeter Wemm 	startp = stackblock() + startloc;
548ab0a2172SSteve Price 	str = stackblock() + strloc;
549aa9caaf6SPeter Wemm 
550aa9caaf6SPeter Wemm 	switch (subtype) {
551aa9caaf6SPeter Wemm 	case VSTRIMLEFT:
55296522b88SSteve Price 		for (loc = startp; loc < str; loc++) {
553aa9caaf6SPeter Wemm 			c = *loc;
554aa9caaf6SPeter Wemm 			*loc = '\0';
55547d8814cSJilles Tjoelker 			if (patmatch(str, startp)) {
556aa9caaf6SPeter Wemm 				*loc = c;
5577034d8dfSJilles Tjoelker 				recordleft(str, loc, startp);
55847d8814cSJilles Tjoelker 				return;
559aa9caaf6SPeter Wemm 			}
560aa9caaf6SPeter Wemm 			*loc = c;
561aa9caaf6SPeter Wemm 		}
56247d8814cSJilles Tjoelker 		break;
563aa9caaf6SPeter Wemm 
564aa9caaf6SPeter Wemm 	case VSTRIMLEFTMAX:
5658b220a61STor Egge 		for (loc = str - 1; loc >= startp;) {
566aa9caaf6SPeter Wemm 			c = *loc;
567aa9caaf6SPeter Wemm 			*loc = '\0';
56847d8814cSJilles Tjoelker 			if (patmatch(str, startp)) {
569aa9caaf6SPeter Wemm 				*loc = c;
5707034d8dfSJilles Tjoelker 				recordleft(str, loc, startp);
57147d8814cSJilles Tjoelker 				return;
572aa9caaf6SPeter Wemm 			}
573aa9caaf6SPeter Wemm 			*loc = c;
5748b220a61STor Egge 			loc--;
5758b220a61STor Egge 		}
57647d8814cSJilles Tjoelker 		break;
577aa9caaf6SPeter Wemm 
578aa9caaf6SPeter Wemm 	case VSTRIMRIGHT:
5798b220a61STor Egge 		for (loc = str - 1; loc >= startp;) {
58047d8814cSJilles Tjoelker 			if (patmatch(str, loc)) {
581ab0a2172SSteve Price 				amount = loc - expdest;
582ab0a2172SSteve Price 				STADJUST(amount, expdest);
58347d8814cSJilles Tjoelker 				return;
584aa9caaf6SPeter Wemm 			}
5858b220a61STor Egge 			loc--;
5868b220a61STor Egge 		}
58747d8814cSJilles Tjoelker 		break;
588aa9caaf6SPeter Wemm 
589aa9caaf6SPeter Wemm 	case VSTRIMRIGHTMAX:
590aa9caaf6SPeter Wemm 		for (loc = startp; loc < str - 1; loc++) {
59147d8814cSJilles Tjoelker 			if (patmatch(str, loc)) {
592ab0a2172SSteve Price 				amount = loc - expdest;
593ab0a2172SSteve Price 				STADJUST(amount, expdest);
59447d8814cSJilles Tjoelker 				return;
595aa9caaf6SPeter Wemm 			}
596aa9caaf6SPeter Wemm 		}
59747d8814cSJilles Tjoelker 		break;
598aa9caaf6SPeter Wemm 
599aa9caaf6SPeter Wemm 
600aa9caaf6SPeter Wemm 	default:
601aa9caaf6SPeter Wemm 		abort();
602aa9caaf6SPeter Wemm 	}
60347d8814cSJilles Tjoelker 	amount = (expdest - stackblock() - strloc) + 1;
60447d8814cSJilles Tjoelker 	STADJUST(-amount, expdest);
605aa9caaf6SPeter Wemm }
606aa9caaf6SPeter Wemm 
607aa9caaf6SPeter Wemm 
6084f2f5faaSJilles Tjoelker static int
6094f2f5faaSJilles Tjoelker subevalvar_misc(char *p, const char *var, int subtype, int startloc,
6104f2f5faaSJilles Tjoelker   int varflags)
6114f2f5faaSJilles Tjoelker {
6124f2f5faaSJilles Tjoelker 	char *startp;
6134f2f5faaSJilles Tjoelker 	struct nodelist *saveargbackq = argbackq;
6144f2f5faaSJilles Tjoelker 	int amount;
6154f2f5faaSJilles Tjoelker 
61647d8814cSJilles Tjoelker 	argstr(p, EXP_TILDE, NULL);
6174f2f5faaSJilles Tjoelker 	STACKSTRNUL(expdest);
6184f2f5faaSJilles Tjoelker 	argbackq = saveargbackq;
6194f2f5faaSJilles Tjoelker 	startp = stackblock() + startloc;
6204f2f5faaSJilles Tjoelker 
6214f2f5faaSJilles Tjoelker 	switch (subtype) {
6224f2f5faaSJilles Tjoelker 	case VSASSIGN:
6234f2f5faaSJilles Tjoelker 		setvar(var, startp, 0);
6244f2f5faaSJilles Tjoelker 		amount = startp - expdest;
6254f2f5faaSJilles Tjoelker 		STADJUST(amount, expdest);
6264f2f5faaSJilles Tjoelker 		return 1;
6274f2f5faaSJilles Tjoelker 
6284f2f5faaSJilles Tjoelker 	case VSQUESTION:
6294f2f5faaSJilles Tjoelker 		if (*p != CTLENDVAR) {
6304f2f5faaSJilles Tjoelker 			outfmt(out2, "%s\n", startp);
6314f2f5faaSJilles Tjoelker 			error((char *)NULL);
6324f2f5faaSJilles Tjoelker 		}
6334f2f5faaSJilles Tjoelker 		error("%.*s: parameter %snot set", (int)(p - var - 1),
6344f2f5faaSJilles Tjoelker 		      var, (varflags & VSNUL) ? "null or " : "");
6354f2f5faaSJilles Tjoelker 		return 0;
6364f2f5faaSJilles Tjoelker 
6374f2f5faaSJilles Tjoelker 	default:
6384f2f5faaSJilles Tjoelker 		abort();
6394f2f5faaSJilles Tjoelker 	}
6404f2f5faaSJilles Tjoelker }
6414f2f5faaSJilles Tjoelker 
6424f2f5faaSJilles Tjoelker 
6434b88c807SRodney W. Grimes /*
6444b88c807SRodney W. Grimes  * Expand a variable, and return a pointer to the next character in the
6454b88c807SRodney W. Grimes  * input string.
6464b88c807SRodney W. Grimes  */
6474b88c807SRodney W. Grimes 
64888328642SDavid E. O'Brien static char *
64947d8814cSJilles Tjoelker evalvar(char *p, int flag, struct worddest *dst)
6504b88c807SRodney W. Grimes {
6514b88c807SRodney W. Grimes 	int subtype;
6524b88c807SRodney W. Grimes 	int varflags;
6534b88c807SRodney W. Grimes 	char *var;
65461346cbdSJilles Tjoelker 	const char *val;
655c4e5a8a8STor Egge 	int patloc;
6564b88c807SRodney W. Grimes 	int c;
6574b88c807SRodney W. Grimes 	int set;
6584b88c807SRodney W. Grimes 	int special;
6594b88c807SRodney W. Grimes 	int startloc;
660aa9caaf6SPeter Wemm 	int varlen;
6614c244ed2SJilles Tjoelker 	int varlenb;
66247d8814cSJilles Tjoelker 	char buf[21];
6634b88c807SRodney W. Grimes 
664bb4f73caSStefan Farfeleder 	varflags = (unsigned char)*p++;
6654b88c807SRodney W. Grimes 	subtype = varflags & VSTYPE;
6664b88c807SRodney W. Grimes 	var = p;
6674b88c807SRodney W. Grimes 	special = 0;
6684b88c807SRodney W. Grimes 	if (! is_name(*p))
6694b88c807SRodney W. Grimes 		special = 1;
6704b88c807SRodney W. Grimes 	p = strchr(p, '=') + 1;
6714b88c807SRodney W. Grimes again: /* jump here after setting a variable with ${var=text} */
672b71085aaSStefan Farfeleder 	if (varflags & VSLINENO) {
673b71085aaSStefan Farfeleder 		set = 1;
67454396489SJilles Tjoelker 		special = 1;
67554396489SJilles Tjoelker 		val = NULL;
676b71085aaSStefan Farfeleder 	} else if (special) {
67796522b88SSteve Price 		set = varisset(var, varflags & VSNUL);
6784b88c807SRodney W. Grimes 		val = NULL;
6794b88c807SRodney W. Grimes 	} else {
680b2acf887SMartin Cracauer 		val = bltinlookup(var, 1);
681aa9caaf6SPeter Wemm 		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
6824b88c807SRodney W. Grimes 			val = NULL;
6834b88c807SRodney W. Grimes 			set = 0;
6844b88c807SRodney W. Grimes 		} else
6854b88c807SRodney W. Grimes 			set = 1;
6864b88c807SRodney W. Grimes 	}
687aa9caaf6SPeter Wemm 	varlen = 0;
6884b88c807SRodney W. Grimes 	startloc = expdest - stackblock();
68964254a66SJilles Tjoelker 	if (!set && uflag && *var != '@' && *var != '*') {
6901b5a48ffSTim J. Robbins 		switch (subtype) {
6911b5a48ffSTim J. Robbins 		case VSNORMAL:
6921b5a48ffSTim J. Robbins 		case VSTRIMLEFT:
6931b5a48ffSTim J. Robbins 		case VSTRIMLEFTMAX:
6941b5a48ffSTim J. Robbins 		case VSTRIMRIGHT:
6951b5a48ffSTim J. Robbins 		case VSTRIMRIGHTMAX:
6961b5a48ffSTim J. Robbins 		case VSLENGTH:
697024ae004SRuslan Ermilov 			error("%.*s: parameter not set", (int)(p - var - 1),
698024ae004SRuslan Ermilov 			    var);
6991b5a48ffSTim J. Robbins 		}
7001b5a48ffSTim J. Robbins 	}
7014b88c807SRodney W. Grimes 	if (set && subtype != VSPLUS) {
7024b88c807SRodney W. Grimes 		/* insert the value of the variable */
7034b88c807SRodney W. Grimes 		if (special) {
70447d8814cSJilles Tjoelker 			if (varflags & VSLINENO) {
70547d8814cSJilles Tjoelker 				if (p - var > (ptrdiff_t)sizeof(buf))
70647d8814cSJilles Tjoelker 					abort();
70747d8814cSJilles Tjoelker 				memcpy(buf, var, p - var - 1);
70847d8814cSJilles Tjoelker 				buf[p - var - 1] = '\0';
70947d8814cSJilles Tjoelker 				strtodest(buf, flag, subtype,
71047d8814cSJilles Tjoelker 				    varflags & VSQUOTE, dst);
71147d8814cSJilles Tjoelker 			} else
71247d8814cSJilles Tjoelker 				varvalue(var, varflags & VSQUOTE, subtype, flag,
71347d8814cSJilles Tjoelker 				    dst);
714aa9caaf6SPeter Wemm 			if (subtype == VSLENGTH) {
7154c244ed2SJilles Tjoelker 				varlenb = expdest - stackblock() - startloc;
7164c244ed2SJilles Tjoelker 				varlen = varlenb;
7174c244ed2SJilles Tjoelker 				if (localeisutf8) {
7184c244ed2SJilles Tjoelker 					val = stackblock() + startloc;
7194c244ed2SJilles Tjoelker 					for (;val != expdest; val++)
7204c244ed2SJilles Tjoelker 						if ((*val & 0xC0) == 0x80)
7214c244ed2SJilles Tjoelker 							varlen--;
7224c244ed2SJilles Tjoelker 				}
7234c244ed2SJilles Tjoelker 				STADJUST(-varlenb, expdest);
724aa9caaf6SPeter Wemm 			}
7254b88c807SRodney W. Grimes 		} else {
726aa9caaf6SPeter Wemm 			if (subtype == VSLENGTH) {
727aa9caaf6SPeter Wemm 				for (;*val; val++)
7284c244ed2SJilles Tjoelker 					if (!localeisutf8 ||
7294c244ed2SJilles Tjoelker 					    (*val & 0xC0) != 0x80)
730aa9caaf6SPeter Wemm 						varlen++;
731aa9caaf6SPeter Wemm 			}
732f7dea851SJilles Tjoelker 			else
7337034d8dfSJilles Tjoelker 				strtodest(val, flag, subtype,
73447d8814cSJilles Tjoelker 				    varflags & VSQUOTE, dst);
7354b88c807SRodney W. Grimes 		}
736aa9caaf6SPeter Wemm 	}
737aa9caaf6SPeter Wemm 
7384b88c807SRodney W. Grimes 	if (subtype == VSPLUS)
7394b88c807SRodney W. Grimes 		set = ! set;
740aa9caaf6SPeter Wemm 
741aa9caaf6SPeter Wemm 	switch (subtype) {
742aa9caaf6SPeter Wemm 	case VSLENGTH:
74347d8814cSJilles Tjoelker 		cvtnum(varlen, buf);
74447d8814cSJilles Tjoelker 		strtodest(buf, flag, VSNORMAL, varflags & VSQUOTE, dst);
7457034d8dfSJilles Tjoelker 		break;
746aa9caaf6SPeter Wemm 
747aa9caaf6SPeter Wemm 	case VSNORMAL:
748aa9caaf6SPeter Wemm 		break;
749aa9caaf6SPeter Wemm 
750aa9caaf6SPeter Wemm 	case VSPLUS:
751aa9caaf6SPeter Wemm 	case VSMINUS:
752aa9caaf6SPeter Wemm 		if (!set) {
7530e39c931SJilles Tjoelker 			argstr(p, flag | (flag & EXP_SPLIT ? EXP_SPLIT_LIT : 0) |
75447d8814cSJilles Tjoelker 			    (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0), dst);
755aa9caaf6SPeter Wemm 			break;
756aa9caaf6SPeter Wemm 		}
757aa9caaf6SPeter Wemm 		break;
758aa9caaf6SPeter Wemm 
759aa9caaf6SPeter Wemm 	case VSTRIMLEFT:
760aa9caaf6SPeter Wemm 	case VSTRIMLEFTMAX:
761aa9caaf6SPeter Wemm 	case VSTRIMRIGHT:
762aa9caaf6SPeter Wemm 	case VSTRIMRIGHTMAX:
763aa9caaf6SPeter Wemm 		if (!set)
764aa9caaf6SPeter Wemm 			break;
765aa9caaf6SPeter Wemm 		/*
766aa9caaf6SPeter Wemm 		 * Terminate the string and start recording the pattern
767aa9caaf6SPeter Wemm 		 * right after it
768aa9caaf6SPeter Wemm 		 */
769aa9caaf6SPeter Wemm 		STPUTC('\0', expdest);
770c4e5a8a8STor Egge 		patloc = expdest - stackblock();
77147d8814cSJilles Tjoelker 		subevalvar_trim(p, patloc, subtype, startloc);
77247d8814cSJilles Tjoelker 		reprocess(startloc, flag, VSNORMAL, varflags & VSQUOTE, dst);
7730e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT && *var == '@' && varflags & VSQUOTE)
77447d8814cSJilles Tjoelker 			dst->state = WORD_QUOTEMARK;
7757034d8dfSJilles Tjoelker 		break;
776aa9caaf6SPeter Wemm 
777aa9caaf6SPeter Wemm 	case VSASSIGN:
778aa9caaf6SPeter Wemm 	case VSQUESTION:
779aa9caaf6SPeter Wemm 		if (!set) {
7804f2f5faaSJilles Tjoelker 			if (subevalvar_misc(p, var, subtype, startloc,
7814f2f5faaSJilles Tjoelker 			    varflags)) {
782ab0a2172SSteve Price 				varflags &= ~VSNUL;
7834b88c807SRodney W. Grimes 				goto again;
784ab0a2172SSteve Price 			}
785aa9caaf6SPeter Wemm 			break;
7864b88c807SRodney W. Grimes 		}
787aa9caaf6SPeter Wemm 		break;
788aa9caaf6SPeter Wemm 
78962addaefSStefan Farfeleder 	case VSERROR:
79062addaefSStefan Farfeleder 		c = p - var - 1;
79162addaefSStefan Farfeleder 		error("${%.*s%s}: Bad substitution", c, var,
79262addaefSStefan Farfeleder 		    (c > 0 && *p != CTLENDVAR) ? "..." : "");
79362addaefSStefan Farfeleder 
794aa9caaf6SPeter Wemm 	default:
795aa9caaf6SPeter Wemm 		abort();
7964b88c807SRodney W. Grimes 	}
797aa9caaf6SPeter Wemm 
7984b88c807SRodney W. Grimes 	if (subtype != VSNORMAL) {	/* skip to end of alternative */
7994b88c807SRodney W. Grimes 		int nesting = 1;
8004b88c807SRodney W. Grimes 		for (;;) {
8014b88c807SRodney W. Grimes 			if ((c = *p++) == CTLESC)
8024b88c807SRodney W. Grimes 				p++;
8034b88c807SRodney W. Grimes 			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
8044b88c807SRodney W. Grimes 				if (set)
8054b88c807SRodney W. Grimes 					argbackq = argbackq->next;
8064b88c807SRodney W. Grimes 			} else if (c == CTLVAR) {
8074b88c807SRodney W. Grimes 				if ((*p++ & VSTYPE) != VSNORMAL)
8084b88c807SRodney W. Grimes 					nesting++;
8094b88c807SRodney W. Grimes 			} else if (c == CTLENDVAR) {
8104b88c807SRodney W. Grimes 				if (--nesting == 0)
8114b88c807SRodney W. Grimes 					break;
8124b88c807SRodney W. Grimes 			}
8134b88c807SRodney W. Grimes 		}
8144b88c807SRodney W. Grimes 	}
8154b88c807SRodney W. Grimes 	return p;
8164b88c807SRodney W. Grimes }
8174b88c807SRodney W. Grimes 
8184b88c807SRodney W. Grimes 
8194b88c807SRodney W. Grimes 
8204b88c807SRodney W. Grimes /*
8214b88c807SRodney W. Grimes  * Test whether a specialized variable is set.
8224b88c807SRodney W. Grimes  */
8234b88c807SRodney W. Grimes 
82488328642SDavid E. O'Brien static int
82561346cbdSJilles Tjoelker varisset(const char *name, int nulok)
8264b88c807SRodney W. Grimes {
8274b88c807SRodney W. Grimes 
82896522b88SSteve Price 	if (*name == '!')
829ed4c3b5fSJilles Tjoelker 		return backgndpidset();
83096522b88SSteve Price 	else if (*name == '@' || *name == '*') {
8314b88c807SRodney W. Grimes 		if (*shellparam.p == NULL)
8324b88c807SRodney W. Grimes 			return 0;
83396522b88SSteve Price 
83496522b88SSteve Price 		if (nulok) {
83596522b88SSteve Price 			char **av;
83696522b88SSteve Price 
83796522b88SSteve Price 			for (av = shellparam.p; *av; av++)
83896522b88SSteve Price 				if (**av != '\0')
83996522b88SSteve Price 					return 1;
84096522b88SSteve Price 			return 0;
84196522b88SSteve Price 		}
8425c817731SPeter Wemm 	} else if (is_digit(*name)) {
84396522b88SSteve Price 		char *ap;
8447b9104c0SJilles Tjoelker 		long num;
84596522b88SSteve Price 
8467b9104c0SJilles Tjoelker 		errno = 0;
8477b9104c0SJilles Tjoelker 		num = strtol(name, NULL, 10);
8487b9104c0SJilles Tjoelker 		if (errno != 0 || num > shellparam.nparam)
84996522b88SSteve Price 			return 0;
85096522b88SSteve Price 
85196522b88SSteve Price 		if (num == 0)
85296522b88SSteve Price 			ap = arg0;
85396522b88SSteve Price 		else
85496522b88SSteve Price 			ap = shellparam.p[num - 1];
85596522b88SSteve Price 
85696522b88SSteve Price 		if (nulok && (ap == NULL || *ap == '\0'))
8574b88c807SRodney W. Grimes 			return 0;
8584b88c807SRodney W. Grimes 	}
8594b88c807SRodney W. Grimes 	return 1;
8604b88c807SRodney W. Grimes }
8614b88c807SRodney W. Grimes 
862f7dea851SJilles Tjoelker static void
86347d8814cSJilles Tjoelker strtodest(const char *p, int flag, int subtype, int quoted,
86447d8814cSJilles Tjoelker     struct worddest *dst)
865f7dea851SJilles Tjoelker {
86647d8814cSJilles Tjoelker 	if (subtype == VSLENGTH || subtype == VSTRIMLEFT ||
86747d8814cSJilles Tjoelker 	    subtype == VSTRIMLEFTMAX || subtype == VSTRIMRIGHT ||
86847d8814cSJilles Tjoelker 	    subtype == VSTRIMRIGHTMAX)
86947d8814cSJilles Tjoelker 		STPUTS(p, expdest);
8700e39c931SJilles Tjoelker 	else if (flag & EXP_SPLIT && !quoted && dst != NULL)
8710e39c931SJilles Tjoelker 		STPUTS_SPLIT(p, BASESYNTAX, flag, expdest, dst);
8720e39c931SJilles Tjoelker 	else if (flag & (EXP_GLOB | EXP_CASE))
873f7dea851SJilles Tjoelker 		STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
874f7dea851SJilles Tjoelker 	else
875f7dea851SJilles Tjoelker 		STPUTS(p, expdest);
876f7dea851SJilles Tjoelker }
8774b88c807SRodney W. Grimes 
87847d8814cSJilles Tjoelker static void
87947d8814cSJilles Tjoelker reprocess(int startloc, int flag, int subtype, int quoted,
88047d8814cSJilles Tjoelker     struct worddest *dst)
88147d8814cSJilles Tjoelker {
88247d8814cSJilles Tjoelker 	static char *buf = NULL;
88347d8814cSJilles Tjoelker 	static size_t buflen = 0;
88447d8814cSJilles Tjoelker 	char *startp;
88547d8814cSJilles Tjoelker 	size_t len, zpos, zlen;
88647d8814cSJilles Tjoelker 
88747d8814cSJilles Tjoelker 	startp = stackblock() + startloc;
88847d8814cSJilles Tjoelker 	len = expdest - startp;
88947d8814cSJilles Tjoelker 	if (len >= SIZE_MAX / 2)
89047d8814cSJilles Tjoelker 		abort();
89147d8814cSJilles Tjoelker 	INTOFF;
89247d8814cSJilles Tjoelker 	if (len >= buflen) {
89347d8814cSJilles Tjoelker 		ckfree(buf);
89447d8814cSJilles Tjoelker 		buf = NULL;
89547d8814cSJilles Tjoelker 	}
89647d8814cSJilles Tjoelker 	if (buflen < 128)
89747d8814cSJilles Tjoelker 		buflen = 128;
89847d8814cSJilles Tjoelker 	while (len >= buflen)
89947d8814cSJilles Tjoelker 		buflen <<= 1;
90047d8814cSJilles Tjoelker 	if (buf == NULL)
90147d8814cSJilles Tjoelker 		buf = ckmalloc(buflen);
90247d8814cSJilles Tjoelker 	INTON;
90347d8814cSJilles Tjoelker 	memcpy(buf, startp, len);
90447d8814cSJilles Tjoelker 	buf[len] = '\0';
90547d8814cSJilles Tjoelker 	STADJUST(-len, expdest);
90647d8814cSJilles Tjoelker 	for (zpos = 0;;) {
90747d8814cSJilles Tjoelker 		zlen = strlen(buf + zpos);
90847d8814cSJilles Tjoelker 		strtodest(buf + zpos, flag, subtype, quoted, dst);
90947d8814cSJilles Tjoelker 		zpos += zlen + 1;
91047d8814cSJilles Tjoelker 		if (zpos == len + 1)
91147d8814cSJilles Tjoelker 			break;
9120e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT && (quoted || (zlen > 0 && zpos < len)))
9130e39c931SJilles Tjoelker 			NEXTWORD('\0', flag, expdest, dst);
91447d8814cSJilles Tjoelker 	}
91547d8814cSJilles Tjoelker }
91647d8814cSJilles Tjoelker 
9174b88c807SRodney W. Grimes /*
9184b88c807SRodney W. Grimes  * Add the value of a specialized variable to the stack string.
9194b88c807SRodney W. Grimes  */
9204b88c807SRodney W. Grimes 
92188328642SDavid E. O'Brien static void
92247d8814cSJilles Tjoelker varvalue(const char *name, int quoted, int subtype, int flag,
92347d8814cSJilles Tjoelker     struct worddest *dst)
9244b88c807SRodney W. Grimes {
9254b88c807SRodney W. Grimes 	int num;
9264b88c807SRodney W. Grimes 	char *p;
9274b88c807SRodney W. Grimes 	int i;
92847d8814cSJilles Tjoelker 	int splitlater;
9293fb51b3aSJilles Tjoelker 	char sep[2];
9304b88c807SRodney W. Grimes 	char **ap;
93147d8814cSJilles Tjoelker 	char buf[(NSHORTOPTS > 10 ? NSHORTOPTS : 10) + 1];
93247d8814cSJilles Tjoelker 
93347d8814cSJilles Tjoelker 	if (subtype == VSLENGTH)
93447d8814cSJilles Tjoelker 		flag &= ~EXP_FULL;
93547d8814cSJilles Tjoelker 	splitlater = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
93647d8814cSJilles Tjoelker 		subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX;
9374b88c807SRodney W. Grimes 
9385c817731SPeter Wemm 	switch (*name) {
9394b88c807SRodney W. Grimes 	case '$':
9404b88c807SRodney W. Grimes 		num = rootpid;
941622fdf32SJilles Tjoelker 		break;
9424b88c807SRodney W. Grimes 	case '?':
943aa9caaf6SPeter Wemm 		num = oexitstatus;
944622fdf32SJilles Tjoelker 		break;
9454b88c807SRodney W. Grimes 	case '#':
9464b88c807SRodney W. Grimes 		num = shellparam.nparam;
947622fdf32SJilles Tjoelker 		break;
9484b88c807SRodney W. Grimes 	case '!':
949ed4c3b5fSJilles Tjoelker 		num = backgndpidval();
9504b88c807SRodney W. Grimes 		break;
9514b88c807SRodney W. Grimes 	case '-':
95247d8814cSJilles Tjoelker 		p = buf;
95362c37116SJilles Tjoelker 		for (i = 0 ; i < NSHORTOPTS ; i++) {
954*3da40d4aSJilles Tjoelker 			if (optval[i])
955*3da40d4aSJilles Tjoelker 				*p++ = optletter[i];
9564b88c807SRodney W. Grimes 		}
95747d8814cSJilles Tjoelker 		*p = '\0';
95847d8814cSJilles Tjoelker 		strtodest(buf, flag, subtype, quoted, dst);
959622fdf32SJilles Tjoelker 		return;
9604b88c807SRodney W. Grimes 	case '@':
9610e39c931SJilles Tjoelker 		if (flag & EXP_SPLIT && quoted) {
9624b88c807SRodney W. Grimes 			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
96347d8814cSJilles Tjoelker 				strtodest(p, flag, subtype, quoted, dst);
96447d8814cSJilles Tjoelker 				if (*ap) {
96547d8814cSJilles Tjoelker 					if (splitlater)
9666f47734fSTor Egge 						STPUTC('\0', expdest);
96747d8814cSJilles Tjoelker 					else
9680e39c931SJilles Tjoelker 						NEXTWORD('\0', flag, expdest,
9690e39c931SJilles Tjoelker 						    dst);
9706f47734fSTor Egge 				}
97147d8814cSJilles Tjoelker 			}
97247d8814cSJilles Tjoelker 			if (shellparam.nparam > 0)
97347d8814cSJilles Tjoelker 				dst->state = WORD_QUOTEMARK;
974622fdf32SJilles Tjoelker 			return;
9756f47734fSTor Egge 		}
9760d9f1a69SPhilippe Charnier 		/* FALLTHROUGH */
9776f47734fSTor Egge 	case '*':
978f7d95a07SRalf S. Engelschall 		if (ifsset())
9793fb51b3aSJilles Tjoelker 			sep[0] = ifsval()[0];
9806f47734fSTor Egge 		else
9813fb51b3aSJilles Tjoelker 			sep[0] = ' ';
9823fb51b3aSJilles Tjoelker 		sep[1] = '\0';
9836f47734fSTor Egge 		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
98447d8814cSJilles Tjoelker 			strtodest(p, flag, subtype, quoted, dst);
985715a0dd5SJilles Tjoelker 			if (!*ap)
986715a0dd5SJilles Tjoelker 				break;
9873fb51b3aSJilles Tjoelker 			if (sep[0])
98847d8814cSJilles Tjoelker 				strtodest(sep, flag, subtype, quoted, dst);
9890e39c931SJilles Tjoelker 			else if (flag & EXP_SPLIT && !quoted && **ap != '\0') {
99047d8814cSJilles Tjoelker 				if (splitlater)
9913fb51b3aSJilles Tjoelker 					STPUTC('\0', expdest);
99247d8814cSJilles Tjoelker 				else
9930e39c931SJilles Tjoelker 					NEXTWORD('\0', flag, expdest, dst);
99447d8814cSJilles Tjoelker 			}
9954b88c807SRodney W. Grimes 		}
996622fdf32SJilles Tjoelker 		return;
9974b88c807SRodney W. Grimes 	default:
9985c817731SPeter Wemm 		if (is_digit(*name)) {
9995c817731SPeter Wemm 			num = atoi(name);
10005ddabb83SJilles Tjoelker 			if (num == 0)
10015ddabb83SJilles Tjoelker 				p = arg0;
10025ddabb83SJilles Tjoelker 			else if (num > 0 && num <= shellparam.nparam)
10035c817731SPeter Wemm 				p = shellparam.p[num - 1];
10045ddabb83SJilles Tjoelker 			else
1005622fdf32SJilles Tjoelker 				return;
100647d8814cSJilles Tjoelker 			strtodest(p, flag, subtype, quoted, dst);
10074b88c807SRodney W. Grimes 		}
1008622fdf32SJilles Tjoelker 		return;
10094b88c807SRodney W. Grimes 	}
101047d8814cSJilles Tjoelker 	cvtnum(num, buf);
101147d8814cSJilles Tjoelker 	strtodest(buf, flag, subtype, quoted, dst);
10124b88c807SRodney W. Grimes }
10134b88c807SRodney W. Grimes 
10144b88c807SRodney W. Grimes 
10154b88c807SRodney W. Grimes 
1016aa7b6f82SDavid E. O'Brien static char expdir[PATH_MAX];
1017c8a3d81fSJilles Tjoelker #define expdir_end (expdir + sizeof(expdir))
10184b88c807SRodney W. Grimes 
10192ca3d70fSJilles Tjoelker /*
10202ca3d70fSJilles Tjoelker  * Perform pathname generation and remove control characters.
1021bc57b4d4SJilles Tjoelker  * At this point, the only control characters should be CTLESC.
10228ef0ae8aSJilles Tjoelker  * The results are stored in the list dstlist.
10232ca3d70fSJilles Tjoelker  */
102488328642SDavid E. O'Brien static void
10250e39c931SJilles Tjoelker expandmeta(char *pattern, struct arglist *dstlist)
10264b88c807SRodney W. Grimes {
10274b88c807SRodney W. Grimes 	char *p;
10288ef0ae8aSJilles Tjoelker 	int firstmatch;
10294b88c807SRodney W. Grimes 	char c;
10304b88c807SRodney W. Grimes 
10318ef0ae8aSJilles Tjoelker 	firstmatch = dstlist->count;
10320e39c931SJilles Tjoelker 	p = pattern;
1033622fdf32SJilles Tjoelker 	for (; (c = *p) != '\0'; p++) {
1034622fdf32SJilles Tjoelker 		/* fast check for meta chars */
1035622fdf32SJilles Tjoelker 		if (c == '*' || c == '?' || c == '[') {
10364b88c807SRodney W. Grimes 			INTOFF;
10370e39c931SJilles Tjoelker 			expmeta(expdir, pattern, dstlist);
10384b88c807SRodney W. Grimes 			INTON;
1039622fdf32SJilles Tjoelker 			break;
1040622fdf32SJilles Tjoelker 		}
1041622fdf32SJilles Tjoelker 	}
10428ef0ae8aSJilles Tjoelker 	if (dstlist->count == firstmatch) {
10434b88c807SRodney W. Grimes 		/*
10444b88c807SRodney W. Grimes 		 * no matches
10454b88c807SRodney W. Grimes 		 */
10460e39c931SJilles Tjoelker 		rmescapes(pattern);
10470e39c931SJilles Tjoelker 		appendarglist(dstlist, pattern);
10484b88c807SRodney W. Grimes 	} else {
10498ef0ae8aSJilles Tjoelker 		qsort(&dstlist->args[firstmatch],
10508ef0ae8aSJilles Tjoelker 		    dstlist->count - firstmatch,
10518ef0ae8aSJilles Tjoelker 		    sizeof(dstlist->args[0]), expsortcmp);
10524b88c807SRodney W. Grimes 	}
10534b88c807SRodney W. Grimes }
10544b88c807SRodney W. Grimes 
10554b88c807SRodney W. Grimes 
10564b88c807SRodney W. Grimes /*
10574b88c807SRodney W. Grimes  * Do metacharacter (i.e. *, ?, [...]) expansion.
10584b88c807SRodney W. Grimes  */
10594b88c807SRodney W. Grimes 
106088328642SDavid E. O'Brien static void
10618ef0ae8aSJilles Tjoelker expmeta(char *enddir, char *name, struct arglist *arglist)
10624b88c807SRodney W. Grimes {
106346c6b52dSJilles Tjoelker 	const char *p;
106446c6b52dSJilles Tjoelker 	const char *q;
106546c6b52dSJilles Tjoelker 	const char *start;
10664b88c807SRodney W. Grimes 	char *endname;
10674b88c807SRodney W. Grimes 	int metaflag;
10684b88c807SRodney W. Grimes 	struct stat statb;
10694b88c807SRodney W. Grimes 	DIR *dirp;
10704b88c807SRodney W. Grimes 	struct dirent *dp;
10714b88c807SRodney W. Grimes 	int atend;
10724b88c807SRodney W. Grimes 	int matchdot;
10734710b07eSJilles Tjoelker 	int esc;
10747a2b9d4bSJilles Tjoelker 	int namlen;
10754b88c807SRodney W. Grimes 
10764b88c807SRodney W. Grimes 	metaflag = 0;
10774b88c807SRodney W. Grimes 	start = name;
10784710b07eSJilles Tjoelker 	for (p = name; esc = 0, *p; p += esc + 1) {
10794b88c807SRodney W. Grimes 		if (*p == '*' || *p == '?')
10804b88c807SRodney W. Grimes 			metaflag = 1;
10814b88c807SRodney W. Grimes 		else if (*p == '[') {
10824b88c807SRodney W. Grimes 			q = p + 1;
1083ea1376dfSAndrey A. Chernov 			if (*q == '!' || *q == '^')
10844b88c807SRodney W. Grimes 				q++;
10854b88c807SRodney W. Grimes 			for (;;) {
10864b88c807SRodney W. Grimes 				if (*q == CTLESC)
10874b88c807SRodney W. Grimes 					q++;
10884b88c807SRodney W. Grimes 				if (*q == '/' || *q == '\0')
10894b88c807SRodney W. Grimes 					break;
10904b88c807SRodney W. Grimes 				if (*++q == ']') {
10914b88c807SRodney W. Grimes 					metaflag = 1;
10924b88c807SRodney W. Grimes 					break;
10934b88c807SRodney W. Grimes 				}
10944b88c807SRodney W. Grimes 			}
10954b88c807SRodney W. Grimes 		} else if (*p == '\0')
10964b88c807SRodney W. Grimes 			break;
10974710b07eSJilles Tjoelker 		else {
10984710b07eSJilles Tjoelker 			if (*p == CTLESC)
10994710b07eSJilles Tjoelker 				esc++;
11004710b07eSJilles Tjoelker 			if (p[esc] == '/') {
11014b88c807SRodney W. Grimes 				if (metaflag)
11024b88c807SRodney W. Grimes 					break;
11034710b07eSJilles Tjoelker 				start = p + esc + 1;
11044710b07eSJilles Tjoelker 			}
11054b88c807SRodney W. Grimes 		}
11064b88c807SRodney W. Grimes 	}
11074b88c807SRodney W. Grimes 	if (metaflag == 0) {	/* we've reached the end of the file name */
11084b88c807SRodney W. Grimes 		if (enddir != expdir)
11094b88c807SRodney W. Grimes 			metaflag++;
11104b88c807SRodney W. Grimes 		for (p = name ; ; p++) {
11114b88c807SRodney W. Grimes 			if (*p == CTLESC)
11124b88c807SRodney W. Grimes 				p++;
11134b88c807SRodney W. Grimes 			*enddir++ = *p;
11144b88c807SRodney W. Grimes 			if (*p == '\0')
11154b88c807SRodney W. Grimes 				break;
1116c8a3d81fSJilles Tjoelker 			if (enddir == expdir_end)
1117c8a3d81fSJilles Tjoelker 				return;
11184b88c807SRodney W. Grimes 		}
11190e3e87bdSXin LI 		if (metaflag == 0 || lstat(expdir, &statb) >= 0)
11208ef0ae8aSJilles Tjoelker 			appendarglist(arglist, stsavestr(expdir));
11214b88c807SRodney W. Grimes 		return;
11224b88c807SRodney W. Grimes 	}
112346c6b52dSJilles Tjoelker 	endname = name + (p - name);
11244b88c807SRodney W. Grimes 	if (start != name) {
11254b88c807SRodney W. Grimes 		p = name;
11264b88c807SRodney W. Grimes 		while (p < start) {
11274b88c807SRodney W. Grimes 			if (*p == CTLESC)
11284b88c807SRodney W. Grimes 				p++;
11294b88c807SRodney W. Grimes 			*enddir++ = *p++;
1130c8a3d81fSJilles Tjoelker 			if (enddir == expdir_end)
1131c8a3d81fSJilles Tjoelker 				return;
11324b88c807SRodney W. Grimes 		}
11334b88c807SRodney W. Grimes 	}
11344b88c807SRodney W. Grimes 	if (enddir == expdir) {
11354b88c807SRodney W. Grimes 		p = ".";
11364b88c807SRodney W. Grimes 	} else if (enddir == expdir + 1 && *expdir == '/') {
11374b88c807SRodney W. Grimes 		p = "/";
11384b88c807SRodney W. Grimes 	} else {
11394b88c807SRodney W. Grimes 		p = expdir;
11404b88c807SRodney W. Grimes 		enddir[-1] = '\0';
11414b88c807SRodney W. Grimes 	}
11424b88c807SRodney W. Grimes 	if ((dirp = opendir(p)) == NULL)
11434b88c807SRodney W. Grimes 		return;
11444b88c807SRodney W. Grimes 	if (enddir != expdir)
11454b88c807SRodney W. Grimes 		enddir[-1] = '/';
11464b88c807SRodney W. Grimes 	if (*endname == 0) {
11474b88c807SRodney W. Grimes 		atend = 1;
11484b88c807SRodney W. Grimes 	} else {
11494b88c807SRodney W. Grimes 		atend = 0;
11504710b07eSJilles Tjoelker 		*endname = '\0';
11514710b07eSJilles Tjoelker 		endname += esc + 1;
11524b88c807SRodney W. Grimes 	}
11534b88c807SRodney W. Grimes 	matchdot = 0;
11546f47734fSTor Egge 	p = start;
11556f47734fSTor Egge 	if (*p == CTLESC)
11566f47734fSTor Egge 		p++;
11576f47734fSTor Egge 	if (*p == '.')
11584b88c807SRodney W. Grimes 		matchdot++;
11594b88c807SRodney W. Grimes 	while (! int_pending() && (dp = readdir(dirp)) != NULL) {
11604b88c807SRodney W. Grimes 		if (dp->d_name[0] == '.' && ! matchdot)
11614b88c807SRodney W. Grimes 			continue;
116247d8814cSJilles Tjoelker 		if (patmatch(start, dp->d_name)) {
11637a2b9d4bSJilles Tjoelker 			namlen = dp->d_namlen;
11647a2b9d4bSJilles Tjoelker 			if (enddir + namlen + 1 > expdir_end)
1165aa9caaf6SPeter Wemm 				continue;
11667a2b9d4bSJilles Tjoelker 			memcpy(enddir, dp->d_name, namlen + 1);
1167c8a3d81fSJilles Tjoelker 			if (atend)
11688ef0ae8aSJilles Tjoelker 				appendarglist(arglist, stsavestr(expdir));
1169c8a3d81fSJilles Tjoelker 			else {
11706e8db49aSJilles Tjoelker 				if (dp->d_type != DT_UNKNOWN &&
11716e8db49aSJilles Tjoelker 				    dp->d_type != DT_DIR &&
11726e8db49aSJilles Tjoelker 				    dp->d_type != DT_LNK)
11736e8db49aSJilles Tjoelker 					continue;
11747a2b9d4bSJilles Tjoelker 				if (enddir + namlen + 2 > expdir_end)
1175c8a3d81fSJilles Tjoelker 					continue;
11767a2b9d4bSJilles Tjoelker 				enddir[namlen] = '/';
11777a2b9d4bSJilles Tjoelker 				enddir[namlen + 1] = '\0';
11788ef0ae8aSJilles Tjoelker 				expmeta(enddir + namlen + 1, endname, arglist);
11794b88c807SRodney W. Grimes 			}
11804b88c807SRodney W. Grimes 		}
11814b88c807SRodney W. Grimes 	}
11824b88c807SRodney W. Grimes 	closedir(dirp);
11834b88c807SRodney W. Grimes 	if (! atend)
11844710b07eSJilles Tjoelker 		endname[-esc - 1] = esc ? CTLESC : '/';
11854b88c807SRodney W. Grimes }
11864b88c807SRodney W. Grimes 
11874b88c807SRodney W. Grimes 
11888ef0ae8aSJilles Tjoelker static int
11898ef0ae8aSJilles Tjoelker expsortcmp(const void *p1, const void *p2)
11904b88c807SRodney W. Grimes {
11918ef0ae8aSJilles Tjoelker 	const char *s1 = *(const char * const *)p1;
11928ef0ae8aSJilles Tjoelker 	const char *s2 = *(const char * const *)p2;
11934b88c807SRodney W. Grimes 
11948ef0ae8aSJilles Tjoelker 	return (strcmp(s1, s2));
11954b88c807SRodney W. Grimes }
11964b88c807SRodney W. Grimes 
11974b88c807SRodney W. Grimes 
11984b88c807SRodney W. Grimes 
11997cc6b3dfSJilles Tjoelker static wchar_t
12007cc6b3dfSJilles Tjoelker get_wc(const char **p)
12017cc6b3dfSJilles Tjoelker {
12027cc6b3dfSJilles Tjoelker 	wchar_t c;
12037cc6b3dfSJilles Tjoelker 	int chrlen;
12047cc6b3dfSJilles Tjoelker 
12057cc6b3dfSJilles Tjoelker 	chrlen = mbtowc(&c, *p, 4);
12067cc6b3dfSJilles Tjoelker 	if (chrlen == 0)
12077cc6b3dfSJilles Tjoelker 		return 0;
12087cc6b3dfSJilles Tjoelker 	else if (chrlen == -1)
12097cc6b3dfSJilles Tjoelker 		c = 0;
12107cc6b3dfSJilles Tjoelker 	else
12117cc6b3dfSJilles Tjoelker 		*p += chrlen;
12127cc6b3dfSJilles Tjoelker 	return c;
12137cc6b3dfSJilles Tjoelker }
12147cc6b3dfSJilles Tjoelker 
12157cc6b3dfSJilles Tjoelker 
12164b88c807SRodney W. Grimes /*
1217ff4dc672SJilles Tjoelker  * See if a character matches a character class, starting at the first colon
1218ff4dc672SJilles Tjoelker  * of "[:class:]".
1219ff4dc672SJilles Tjoelker  * If a valid character class is recognized, a pointer to the next character
1220ff4dc672SJilles Tjoelker  * after the final closing bracket is stored into *end, otherwise a null
1221ff4dc672SJilles Tjoelker  * pointer is stored into *end.
1222ff4dc672SJilles Tjoelker  */
1223ff4dc672SJilles Tjoelker static int
1224ff4dc672SJilles Tjoelker match_charclass(const char *p, wchar_t chr, const char **end)
1225ff4dc672SJilles Tjoelker {
1226ff4dc672SJilles Tjoelker 	char name[20];
1227ff4dc672SJilles Tjoelker 	const char *nameend;
1228ff4dc672SJilles Tjoelker 	wctype_t cclass;
1229ff4dc672SJilles Tjoelker 
1230ff4dc672SJilles Tjoelker 	*end = NULL;
1231ff4dc672SJilles Tjoelker 	p++;
1232ff4dc672SJilles Tjoelker 	nameend = strstr(p, ":]");
123346c6b52dSJilles Tjoelker 	if (nameend == NULL || (size_t)(nameend - p) >= sizeof(name) ||
123446c6b52dSJilles Tjoelker 	    nameend == p)
1235ff4dc672SJilles Tjoelker 		return 0;
1236ff4dc672SJilles Tjoelker 	memcpy(name, p, nameend - p);
1237ff4dc672SJilles Tjoelker 	name[nameend - p] = '\0';
1238ff4dc672SJilles Tjoelker 	*end = nameend + 2;
1239ff4dc672SJilles Tjoelker 	cclass = wctype(name);
1240ff4dc672SJilles Tjoelker 	/* An unknown class matches nothing but is valid nevertheless. */
1241ff4dc672SJilles Tjoelker 	if (cclass == 0)
1242ff4dc672SJilles Tjoelker 		return 0;
1243ff4dc672SJilles Tjoelker 	return iswctype(chr, cclass);
1244ff4dc672SJilles Tjoelker }
1245ff4dc672SJilles Tjoelker 
1246ff4dc672SJilles Tjoelker 
1247ff4dc672SJilles Tjoelker /*
12484b88c807SRodney W. Grimes  * Returns true if the pattern matches the string.
12494b88c807SRodney W. Grimes  */
12504b88c807SRodney W. Grimes 
1251260fc3f4SJilles Tjoelker static int
125247d8814cSJilles Tjoelker patmatch(const char *pattern, const char *string)
12534b88c807SRodney W. Grimes {
1254ff4dc672SJilles Tjoelker 	const char *p, *q, *end;
1255820491f8SJilles Tjoelker 	const char *bt_p, *bt_q;
125696522b88SSteve Price 	char c;
12577cc6b3dfSJilles Tjoelker 	wchar_t wc, wc2;
12584b88c807SRodney W. Grimes 
12594b88c807SRodney W. Grimes 	p = pattern;
12604b88c807SRodney W. Grimes 	q = string;
1261820491f8SJilles Tjoelker 	bt_p = NULL;
1262820491f8SJilles Tjoelker 	bt_q = NULL;
12634b88c807SRodney W. Grimes 	for (;;) {
12644b88c807SRodney W. Grimes 		switch (c = *p++) {
12654b88c807SRodney W. Grimes 		case '\0':
1266820491f8SJilles Tjoelker 			if (*q != '\0')
1267820491f8SJilles Tjoelker 				goto backtrack;
1268820491f8SJilles Tjoelker 			return 1;
12694b88c807SRodney W. Grimes 		case CTLESC:
12704b88c807SRodney W. Grimes 			if (*q++ != *p++)
1271820491f8SJilles Tjoelker 				goto backtrack;
12724b88c807SRodney W. Grimes 			break;
12734b88c807SRodney W. Grimes 		case '?':
1274820491f8SJilles Tjoelker 			if (*q == '\0')
12754b88c807SRodney W. Grimes 				return 0;
1276820491f8SJilles Tjoelker 			if (localeisutf8) {
1277820491f8SJilles Tjoelker 				wc = get_wc(&q);
1278820491f8SJilles Tjoelker 				/*
1279820491f8SJilles Tjoelker 				 * A '?' does not match invalid UTF-8 but a
1280820491f8SJilles Tjoelker 				 * '*' does, so backtrack.
1281820491f8SJilles Tjoelker 				 */
1282820491f8SJilles Tjoelker 				if (wc == 0)
1283820491f8SJilles Tjoelker 					goto backtrack;
1284820491f8SJilles Tjoelker 			} else
1285820491f8SJilles Tjoelker 				wc = (unsigned char)*q++;
12864b88c807SRodney W. Grimes 			break;
12874b88c807SRodney W. Grimes 		case '*':
12884b88c807SRodney W. Grimes 			c = *p;
1289bc57b4d4SJilles Tjoelker 			while (c == '*')
12906f47734fSTor Egge 				c = *++p;
1291820491f8SJilles Tjoelker 			/*
1292820491f8SJilles Tjoelker 			 * If the pattern ends here, we know the string
1293820491f8SJilles Tjoelker 			 * matches without needing to look at the rest of it.
1294820491f8SJilles Tjoelker 			 */
1295820491f8SJilles Tjoelker 			if (c == '\0')
12964b88c807SRodney W. Grimes 				return 1;
1297820491f8SJilles Tjoelker 			/*
1298820491f8SJilles Tjoelker 			 * First try the shortest match for the '*' that
1299820491f8SJilles Tjoelker 			 * could work. We can forget any earlier '*' since
1300820491f8SJilles Tjoelker 			 * there is no way having it match more characters
1301820491f8SJilles Tjoelker 			 * can help us, given that we are already here.
1302820491f8SJilles Tjoelker 			 */
1303820491f8SJilles Tjoelker 			bt_p = p;
1304820491f8SJilles Tjoelker 			bt_q = q;
1305820491f8SJilles Tjoelker 			break;
13064b88c807SRodney W. Grimes 		case '[': {
13074a4867d6SJilles Tjoelker 			const char *savep, *saveq;
13084b88c807SRodney W. Grimes 			int invert, found;
13097cc6b3dfSJilles Tjoelker 			wchar_t chr;
13104b88c807SRodney W. Grimes 
13114a4867d6SJilles Tjoelker 			savep = p, saveq = q;
13124b88c807SRodney W. Grimes 			invert = 0;
1313ea1376dfSAndrey A. Chernov 			if (*p == '!' || *p == '^') {
13144b88c807SRodney W. Grimes 				invert++;
13154b88c807SRodney W. Grimes 				p++;
13164b88c807SRodney W. Grimes 			}
13174b88c807SRodney W. Grimes 			found = 0;
1318820491f8SJilles Tjoelker 			if (*q == '\0')
1319aa9caaf6SPeter Wemm 				return 0;
1320820491f8SJilles Tjoelker 			if (localeisutf8) {
1321820491f8SJilles Tjoelker 				chr = get_wc(&q);
1322820491f8SJilles Tjoelker 				if (chr == 0)
1323820491f8SJilles Tjoelker 					goto backtrack;
1324820491f8SJilles Tjoelker 			} else
1325820491f8SJilles Tjoelker 				chr = (unsigned char)*q++;
13264b88c807SRodney W. Grimes 			c = *p++;
13274b88c807SRodney W. Grimes 			do {
13284a4867d6SJilles Tjoelker 				if (c == '\0') {
13294a4867d6SJilles Tjoelker 					p = savep, q = saveq;
13304a4867d6SJilles Tjoelker 					c = '[';
13314a4867d6SJilles Tjoelker 					goto dft;
13324a4867d6SJilles Tjoelker 				}
1333ff4dc672SJilles Tjoelker 				if (c == '[' && *p == ':') {
1334ff4dc672SJilles Tjoelker 					found |= match_charclass(p, chr, &end);
1335ff4dc672SJilles Tjoelker 					if (end != NULL)
1336ff4dc672SJilles Tjoelker 						p = end;
1337ff4dc672SJilles Tjoelker 				}
13384b88c807SRodney W. Grimes 				if (c == CTLESC)
13394b88c807SRodney W. Grimes 					c = *p++;
13407cc6b3dfSJilles Tjoelker 				if (localeisutf8 && c & 0x80) {
13417cc6b3dfSJilles Tjoelker 					p--;
13427cc6b3dfSJilles Tjoelker 					wc = get_wc(&p);
13437cc6b3dfSJilles Tjoelker 					if (wc == 0) /* bad utf-8 */
13447cc6b3dfSJilles Tjoelker 						return 0;
13457cc6b3dfSJilles Tjoelker 				} else
1346f5ac5937SJilles Tjoelker 					wc = (unsigned char)c;
13474b88c807SRodney W. Grimes 				if (*p == '-' && p[1] != ']') {
13484b88c807SRodney W. Grimes 					p++;
13494b88c807SRodney W. Grimes 					if (*p == CTLESC)
13504b88c807SRodney W. Grimes 						p++;
13517cc6b3dfSJilles Tjoelker 					if (localeisutf8) {
13527cc6b3dfSJilles Tjoelker 						wc2 = get_wc(&p);
13537cc6b3dfSJilles Tjoelker 						if (wc2 == 0) /* bad utf-8 */
13547cc6b3dfSJilles Tjoelker 							return 0;
13557cc6b3dfSJilles Tjoelker 					} else
1356f5ac5937SJilles Tjoelker 						wc2 = (unsigned char)*p++;
13577cc6b3dfSJilles Tjoelker 					if (   collate_range_cmp(chr, wc) >= 0
13587cc6b3dfSJilles Tjoelker 					    && collate_range_cmp(chr, wc2) <= 0
1359ba726b8aSAndrey A. Chernov 					   )
13604b88c807SRodney W. Grimes 						found = 1;
13614b88c807SRodney W. Grimes 				} else {
13627cc6b3dfSJilles Tjoelker 					if (chr == wc)
13634b88c807SRodney W. Grimes 						found = 1;
13644b88c807SRodney W. Grimes 				}
13654b88c807SRodney W. Grimes 			} while ((c = *p++) != ']');
13664b88c807SRodney W. Grimes 			if (found == invert)
1367820491f8SJilles Tjoelker 				goto backtrack;
13684b88c807SRodney W. Grimes 			break;
13694b88c807SRodney W. Grimes 		}
13704b88c807SRodney W. Grimes dft:	        default:
1371820491f8SJilles Tjoelker 			if (*q == '\0')
13724b88c807SRodney W. Grimes 				return 0;
1373820491f8SJilles Tjoelker 			if (*q++ == c)
1374820491f8SJilles Tjoelker 				break;
1375820491f8SJilles Tjoelker backtrack:
1376820491f8SJilles Tjoelker 			/*
1377820491f8SJilles Tjoelker 			 * If we have a mismatch (other than hitting the end
1378820491f8SJilles Tjoelker 			 * of the string), go back to the last '*' seen and
1379820491f8SJilles Tjoelker 			 * have it match one additional character.
1380820491f8SJilles Tjoelker 			 */
1381820491f8SJilles Tjoelker 			if (bt_p == NULL)
1382820491f8SJilles Tjoelker 				return 0;
1383820491f8SJilles Tjoelker 			if (*bt_q == '\0')
1384820491f8SJilles Tjoelker 				return 0;
1385820491f8SJilles Tjoelker 			bt_q++;
1386820491f8SJilles Tjoelker 			p = bt_p;
1387820491f8SJilles Tjoelker 			q = bt_q;
13884b88c807SRodney W. Grimes 			break;
13894b88c807SRodney W. Grimes 		}
13904b88c807SRodney W. Grimes 	}
13914b88c807SRodney W. Grimes }
13924b88c807SRodney W. Grimes 
13934b88c807SRodney W. Grimes 
13944b88c807SRodney W. Grimes 
13954b88c807SRodney W. Grimes /*
13962ca3d70fSJilles Tjoelker  * Remove any CTLESC and CTLQUOTEMARK characters from a string.
13974b88c807SRodney W. Grimes  */
13984b88c807SRodney W. Grimes 
13994b88c807SRodney W. Grimes void
14005134c3f7SWarner Losh rmescapes(char *str)
14014b88c807SRodney W. Grimes {
140296522b88SSteve Price 	char *p, *q;
14034b88c807SRodney W. Grimes 
14044b88c807SRodney W. Grimes 	p = str;
1405048f2667SJilles Tjoelker 	while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) {
14064b88c807SRodney W. Grimes 		if (*p++ == '\0')
14074b88c807SRodney W. Grimes 			return;
14084b88c807SRodney W. Grimes 	}
14094b88c807SRodney W. Grimes 	q = p;
14104b88c807SRodney W. Grimes 	while (*p) {
1411048f2667SJilles Tjoelker 		if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
14126f47734fSTor Egge 			p++;
14136f47734fSTor Egge 			continue;
14146f47734fSTor Egge 		}
14154b88c807SRodney W. Grimes 		if (*p == CTLESC)
14164b88c807SRodney W. Grimes 			p++;
14174b88c807SRodney W. Grimes 		*q++ = *p++;
14184b88c807SRodney W. Grimes 	}
14194b88c807SRodney W. Grimes 	*q = '\0';
14204b88c807SRodney W. Grimes }
14214b88c807SRodney W. Grimes 
14224b88c807SRodney W. Grimes 
14234b88c807SRodney W. Grimes 
14244b88c807SRodney W. Grimes /*
14254b88c807SRodney W. Grimes  * See if a pattern matches in a case statement.
14264b88c807SRodney W. Grimes  */
14274b88c807SRodney W. Grimes 
14284b88c807SRodney W. Grimes int
14292cac6e36SJilles Tjoelker casematch(union node *pattern, const char *val)
14304b88c807SRodney W. Grimes {
14314b88c807SRodney W. Grimes 	struct stackmark smark;
14324b88c807SRodney W. Grimes 	int result;
14334b88c807SRodney W. Grimes 	char *p;
14344b88c807SRodney W. Grimes 
14354b88c807SRodney W. Grimes 	setstackmark(&smark);
14364b88c807SRodney W. Grimes 	argbackq = pattern->narg.backquote;
14374b88c807SRodney W. Grimes 	STARTSTACKSTR(expdest);
143847d8814cSJilles Tjoelker 	argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, NULL);
14394b88c807SRodney W. Grimes 	STPUTC('\0', expdest);
14404b88c807SRodney W. Grimes 	p = grabstackstr(expdest);
144147d8814cSJilles Tjoelker 	result = patmatch(p, val);
14424b88c807SRodney W. Grimes 	popstackmark(&smark);
14434b88c807SRodney W. Grimes 	return result;
14444b88c807SRodney W. Grimes }
1445aa9caaf6SPeter Wemm 
1446aa9caaf6SPeter Wemm /*
1447aa9caaf6SPeter Wemm  * Our own itoa().
1448aa9caaf6SPeter Wemm  */
1449aa9caaf6SPeter Wemm 
145047d8814cSJilles Tjoelker static void
14515134c3f7SWarner Losh cvtnum(int num, char *buf)
1452aa9caaf6SPeter Wemm {
1453aa9caaf6SPeter Wemm 	char temp[32];
1454aa9caaf6SPeter Wemm 	int neg = num < 0;
1455aa9caaf6SPeter Wemm 	char *p = temp + 31;
1456aa9caaf6SPeter Wemm 
1457aa9caaf6SPeter Wemm 	temp[31] = '\0';
1458aa9caaf6SPeter Wemm 
1459aa9caaf6SPeter Wemm 	do {
1460aa9caaf6SPeter Wemm 		*--p = num % 10 + '0';
1461aa9caaf6SPeter Wemm 	} while ((num /= 10) != 0);
1462aa9caaf6SPeter Wemm 
1463aa9caaf6SPeter Wemm 	if (neg)
1464aa9caaf6SPeter Wemm 		*--p = '-';
1465aa9caaf6SPeter Wemm 
146647d8814cSJilles Tjoelker 	memcpy(buf, p, temp + 32 - p);
1467aa9caaf6SPeter Wemm }
14682c25061fSTim J. Robbins 
14692c25061fSTim J. Robbins /*
14702c25061fSTim J. Robbins  * Do most of the work for wordexp(3).
14712c25061fSTim J. Robbins  */
14722c25061fSTim J. Robbins 
14732c25061fSTim J. Robbins int
14742c25061fSTim J. Robbins wordexpcmd(int argc, char **argv)
14752c25061fSTim J. Robbins {
14762c25061fSTim J. Robbins 	size_t len;
14772c25061fSTim J. Robbins 	int i;
14782c25061fSTim J. Robbins 
14792c25061fSTim J. Robbins 	out1fmt("%08x", argc - 1);
14802c25061fSTim J. Robbins 	for (i = 1, len = 0; i < argc; i++)
14812c25061fSTim J. Robbins 		len += strlen(argv[i]);
14822c25061fSTim J. Robbins 	out1fmt("%08x", (int)len);
1483aeb5d065SJilles Tjoelker 	for (i = 1; i < argc; i++)
1484aeb5d065SJilles Tjoelker 		outbin(argv[i], strlen(argv[i]) + 1, out1);
14852c25061fSTim J. Robbins         return (0);
14862c25061fSTim J. Robbins }
1487d358fa78SJilles Tjoelker 
1488d358fa78SJilles Tjoelker /*
1489d358fa78SJilles Tjoelker  * Do most of the work for wordexp(3), new version.
1490d358fa78SJilles Tjoelker  */
1491d358fa78SJilles Tjoelker 
1492d358fa78SJilles Tjoelker int
1493d358fa78SJilles Tjoelker freebsd_wordexpcmd(int argc __unused, char **argv __unused)
1494d358fa78SJilles Tjoelker {
1495d358fa78SJilles Tjoelker 	struct arglist arglist;
1496d358fa78SJilles Tjoelker 	union node *args, *n;
14978ef0ae8aSJilles Tjoelker 	size_t len;
1498d358fa78SJilles Tjoelker 	int ch;
1499d358fa78SJilles Tjoelker 	int protected = 0;
1500d358fa78SJilles Tjoelker 	int fd = -1;
15018ef0ae8aSJilles Tjoelker 	int i;
1502d358fa78SJilles Tjoelker 
1503d358fa78SJilles Tjoelker 	while ((ch = nextopt("f:p")) != '\0') {
1504d358fa78SJilles Tjoelker 		switch (ch) {
1505d358fa78SJilles Tjoelker 		case 'f':
1506d358fa78SJilles Tjoelker 			fd = number(shoptarg);
1507d358fa78SJilles Tjoelker 			break;
1508d358fa78SJilles Tjoelker 		case 'p':
1509d358fa78SJilles Tjoelker 			protected = 1;
1510d358fa78SJilles Tjoelker 			break;
1511d358fa78SJilles Tjoelker 		}
1512d358fa78SJilles Tjoelker 	}
1513d358fa78SJilles Tjoelker 	if (*argptr != NULL)
1514d358fa78SJilles Tjoelker 		error("wrong number of arguments");
1515d358fa78SJilles Tjoelker 	if (fd < 0)
1516d358fa78SJilles Tjoelker 		error("missing fd");
1517d358fa78SJilles Tjoelker 	INTOFF;
1518d358fa78SJilles Tjoelker 	setinputfd(fd, 1);
1519d358fa78SJilles Tjoelker 	INTON;
1520d358fa78SJilles Tjoelker 	args = parsewordexp();
1521d358fa78SJilles Tjoelker 	popfile(); /* will also close fd */
1522d358fa78SJilles Tjoelker 	if (protected)
1523d358fa78SJilles Tjoelker 		for (n = args; n != NULL; n = n->narg.next) {
1524d358fa78SJilles Tjoelker 			if (n->narg.backquote != NULL) {
1525d358fa78SJilles Tjoelker 				outcslow('C', out1);
1526d358fa78SJilles Tjoelker 				error("command substitution disabled");
1527d358fa78SJilles Tjoelker 			}
1528d358fa78SJilles Tjoelker 		}
1529d358fa78SJilles Tjoelker 	outcslow(' ', out1);
15308ef0ae8aSJilles Tjoelker 	emptyarglist(&arglist);
1531d358fa78SJilles Tjoelker 	for (n = args; n != NULL; n = n->narg.next)
1532d358fa78SJilles Tjoelker 		expandarg(n, &arglist, EXP_FULL | EXP_TILDE);
15338ef0ae8aSJilles Tjoelker 	for (i = 0, len = 0; i < arglist.count; i++)
15348ef0ae8aSJilles Tjoelker 		len += strlen(arglist.args[i]);
15358ef0ae8aSJilles Tjoelker 	out1fmt("%016x %016zx", arglist.count, len);
15368ef0ae8aSJilles Tjoelker 	for (i = 0; i < arglist.count; i++)
15378ef0ae8aSJilles Tjoelker 		outbin(arglist.args[i], strlen(arglist.args[i]) + 1, out1);
1538d358fa78SJilles Tjoelker 	return (0);
1539d358fa78SJilles Tjoelker }
1540