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
37aa9caaf6SPeter Wemm #include <sys/types.h>
38aa9caaf6SPeter Wemm #include <sys/time.h>
39aa9caaf6SPeter Wemm #include <sys/stat.h>
40aa9caaf6SPeter Wemm #include <dirent.h>
418ab2e970SJohn Baldwin #include <errno.h>
428ab2e970SJohn Baldwin #include <inttypes.h>
433cd859a7SAndrey A. Chernov #include <limits.h>
448ab2e970SJohn Baldwin #include <pwd.h>
456f47734fSTor Egge #include <stdio.h>
468ab2e970SJohn Baldwin #include <stdlib.h>
472c25061fSTim J. Robbins #include <string.h>
488ab2e970SJohn Baldwin #include <unistd.h>
497cc6b3dfSJilles Tjoelker #include <wchar.h>
50ff4dc672SJilles Tjoelker #include <wctype.h>
51aa9caaf6SPeter Wemm
524b88c807SRodney W. Grimes /*
534b88c807SRodney W. Grimes * Routines to expand arguments to commands. We have to deal with
544b88c807SRodney W. Grimes * backquotes, shell variables, and file metacharacters.
554b88c807SRodney W. Grimes */
564b88c807SRodney W. Grimes
574b88c807SRodney W. Grimes #include "shell.h"
584b88c807SRodney W. Grimes #include "main.h"
594b88c807SRodney W. Grimes #include "nodes.h"
604b88c807SRodney W. Grimes #include "eval.h"
614b88c807SRodney W. Grimes #include "expand.h"
624b88c807SRodney W. Grimes #include "syntax.h"
634b88c807SRodney W. Grimes #include "parser.h"
644b88c807SRodney W. Grimes #include "jobs.h"
654b88c807SRodney W. Grimes #include "options.h"
664b88c807SRodney W. Grimes #include "var.h"
674b88c807SRodney W. Grimes #include "input.h"
684b88c807SRodney W. Grimes #include "output.h"
694b88c807SRodney W. Grimes #include "memalloc.h"
704b88c807SRodney W. Grimes #include "error.h"
714b88c807SRodney W. Grimes #include "mystring.h"
72aa9caaf6SPeter Wemm #include "arith.h"
73aa9caaf6SPeter Wemm #include "show.h"
74454a02b3SJilles Tjoelker #include "builtins.h"
754b88c807SRodney W. Grimes
7647d8814cSJilles Tjoelker enum wordstate { WORD_IDLE, WORD_WS_DELIMITED, WORD_QUOTEMARK };
774b88c807SRodney W. Grimes
7847d8814cSJilles Tjoelker struct worddest {
790e39c931SJilles Tjoelker struct arglist *list;
8047d8814cSJilles Tjoelker enum wordstate state;
814b88c807SRodney W. Grimes };
824b88c807SRodney W. Grimes
83aa7b6f82SDavid E. O'Brien static char *expdest; /* output of current string */
844b88c807SRodney W. Grimes
85439948cdSJilles Tjoelker static const char *argstr(const char *, struct nodelist **restrict, int,
86439948cdSJilles Tjoelker struct worddest *);
8752d5897dSJilles Tjoelker static const char *exptilde(const char *, int);
88439948cdSJilles Tjoelker static const char *expari(const char *, struct nodelist **restrict, int,
89439948cdSJilles Tjoelker struct worddest *);
9047d8814cSJilles Tjoelker static void expbackq(union node *, int, int, struct worddest *);
91e2708b16SJilles Tjoelker static const char *subevalvar_trim(const char *, struct nodelist **restrict,
92e2708b16SJilles Tjoelker int, int, int);
93e59833ccSJilles Tjoelker static const char *subevalvar_misc(const char *, struct nodelist **restrict,
94e59833ccSJilles Tjoelker const char *, int, int, int);
95439948cdSJilles Tjoelker static const char *evalvar(const char *, struct nodelist **restrict, int,
96439948cdSJilles Tjoelker struct worddest *);
9761346cbdSJilles Tjoelker static int varisset(const char *, int);
9847d8814cSJilles Tjoelker static void strtodest(const char *, int, int, int, struct worddest *);
9947d8814cSJilles Tjoelker static void reprocess(int, int, int, int, struct worddest *);
10047d8814cSJilles Tjoelker static void varvalue(const char *, int, int, int, struct worddest *);
1010e39c931SJilles Tjoelker static void expandmeta(char *, struct arglist *);
1028ef0ae8aSJilles Tjoelker static void expmeta(char *, char *, struct arglist *);
1038ef0ae8aSJilles Tjoelker static int expsortcmp(const void *, const void *);
10447d8814cSJilles Tjoelker static int patmatch(const char *, const char *);
10547d8814cSJilles Tjoelker static void cvtnum(int, char *);
106fa93fc65SAndrey A. Chernov static int collate_range_cmp(wchar_t, wchar_t);
1073cd859a7SAndrey A. Chernov
1088ef0ae8aSJilles Tjoelker void
emptyarglist(struct arglist * list)1098ef0ae8aSJilles Tjoelker emptyarglist(struct arglist *list)
1108ef0ae8aSJilles Tjoelker {
1118ef0ae8aSJilles Tjoelker
1128ef0ae8aSJilles Tjoelker list->args = list->smallarg;
1138ef0ae8aSJilles Tjoelker list->count = 0;
1148ef0ae8aSJilles Tjoelker list->capacity = sizeof(list->smallarg) / sizeof(list->smallarg[0]);
1158ef0ae8aSJilles Tjoelker }
1168ef0ae8aSJilles Tjoelker
117046bfe52SJilles Tjoelker void
appendarglist(struct arglist * list,char * str)1188ef0ae8aSJilles Tjoelker appendarglist(struct arglist *list, char *str)
1198ef0ae8aSJilles Tjoelker {
1208ef0ae8aSJilles Tjoelker char **newargs;
1218ef0ae8aSJilles Tjoelker int newcapacity;
1228ef0ae8aSJilles Tjoelker
1238ef0ae8aSJilles Tjoelker if (list->count >= list->capacity) {
1248ef0ae8aSJilles Tjoelker newcapacity = list->capacity * 2;
1258ef0ae8aSJilles Tjoelker if (newcapacity < 16)
1268ef0ae8aSJilles Tjoelker newcapacity = 16;
1278ef0ae8aSJilles Tjoelker if (newcapacity > INT_MAX / (int)sizeof(newargs[0]))
1288ef0ae8aSJilles Tjoelker error("Too many entries in arglist");
1298ef0ae8aSJilles Tjoelker newargs = stalloc(newcapacity * sizeof(newargs[0]));
1308ef0ae8aSJilles Tjoelker memcpy(newargs, list->args, list->count * sizeof(newargs[0]));
1318ef0ae8aSJilles Tjoelker list->args = newargs;
1328ef0ae8aSJilles Tjoelker list->capacity = newcapacity;
1338ef0ae8aSJilles Tjoelker }
1348ef0ae8aSJilles Tjoelker list->args[list->count++] = str;
1358ef0ae8aSJilles Tjoelker }
1368ef0ae8aSJilles Tjoelker
137fa93fc65SAndrey A. Chernov static int
collate_range_cmp(wchar_t c1,wchar_t c2)138fa93fc65SAndrey A. Chernov collate_range_cmp(wchar_t c1, wchar_t c2)
139fa93fc65SAndrey A. Chernov {
140c39d3320SJilles Tjoelker wchar_t s1[2], s2[2];
141fa93fc65SAndrey A. Chernov
142fa93fc65SAndrey A. Chernov s1[0] = c1;
143c39d3320SJilles Tjoelker s1[1] = L'\0';
144fa93fc65SAndrey A. Chernov s2[0] = c2;
145c39d3320SJilles Tjoelker s2[1] = L'\0';
146fa93fc65SAndrey A. Chernov return (wcscoll(s1, s2));
147fa93fc65SAndrey A. Chernov }
148fa93fc65SAndrey A. Chernov
149f7dea851SJilles Tjoelker static char *
stputs_quotes(const char * data,const char * syntax,char * p)150f7dea851SJilles Tjoelker stputs_quotes(const char *data, const char *syntax, char *p)
151f7dea851SJilles Tjoelker {
152f7dea851SJilles Tjoelker while (*data) {
153f7dea851SJilles Tjoelker CHECKSTRSPACE(2, p);
154f7dea851SJilles Tjoelker if (syntax[(int)*data] == CCTL)
155f7dea851SJilles Tjoelker USTPUTC(CTLESC, p);
156f7dea851SJilles Tjoelker USTPUTC(*data++, p);
157f7dea851SJilles Tjoelker }
158f7dea851SJilles Tjoelker return (p);
159f7dea851SJilles Tjoelker }
160f7dea851SJilles Tjoelker #define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p)
1614b88c807SRodney W. Grimes
16247d8814cSJilles Tjoelker static char *
nextword(char c,int flag,char * p,struct worddest * dst)1630e39c931SJilles Tjoelker nextword(char c, int flag, char *p, struct worddest *dst)
16447d8814cSJilles Tjoelker {
16547d8814cSJilles Tjoelker int is_ws;
16647d8814cSJilles Tjoelker
16747d8814cSJilles Tjoelker is_ws = c == '\t' || c == '\n' || c == ' ';
16847d8814cSJilles Tjoelker if (p != stackblock() || (is_ws ? dst->state == WORD_QUOTEMARK :
16947d8814cSJilles Tjoelker dst->state != WORD_WS_DELIMITED) || c == '\0') {
17047d8814cSJilles Tjoelker STPUTC('\0', p);
1710e39c931SJilles Tjoelker if (flag & EXP_GLOB)
1720e39c931SJilles Tjoelker expandmeta(grabstackstr(p), dst->list);
1730e39c931SJilles Tjoelker else
1740e39c931SJilles Tjoelker appendarglist(dst->list, grabstackstr(p));
17547d8814cSJilles Tjoelker dst->state = is_ws ? WORD_WS_DELIMITED : WORD_IDLE;
17647d8814cSJilles Tjoelker } else if (!is_ws && dst->state == WORD_WS_DELIMITED)
17747d8814cSJilles Tjoelker dst->state = WORD_IDLE;
17847d8814cSJilles Tjoelker /* Reserve space while the stack string is empty. */
1790e39c931SJilles Tjoelker appendarglist(dst->list, NULL);
1800e39c931SJilles Tjoelker dst->list->count--;
18147d8814cSJilles Tjoelker STARTSTACKSTR(p);
18247d8814cSJilles Tjoelker return p;
18347d8814cSJilles Tjoelker }
1840e39c931SJilles Tjoelker #define NEXTWORD(c, flag, p, dstlist) p = nextword(c, flag, p, dstlist)
18547d8814cSJilles Tjoelker
18647d8814cSJilles Tjoelker static char *
stputs_split(const char * data,const char * syntax,int flag,char * p,struct worddest * dst)1870e39c931SJilles Tjoelker stputs_split(const char *data, const char *syntax, int flag, char *p,
18847d8814cSJilles Tjoelker struct worddest *dst)
18947d8814cSJilles Tjoelker {
19047d8814cSJilles Tjoelker const char *ifs;
19147d8814cSJilles Tjoelker char c;
19247d8814cSJilles Tjoelker
19347d8814cSJilles Tjoelker ifs = ifsset() ? ifsval() : " \t\n";
19447d8814cSJilles Tjoelker while (*data) {
19547d8814cSJilles Tjoelker CHECKSTRSPACE(2, p);
19647d8814cSJilles Tjoelker c = *data++;
19747d8814cSJilles Tjoelker if (strchr(ifs, c) != NULL) {
1980e39c931SJilles Tjoelker NEXTWORD(c, flag, p, dst);
19947d8814cSJilles Tjoelker continue;
20047d8814cSJilles Tjoelker }
2010e39c931SJilles Tjoelker if (flag & EXP_GLOB && syntax[(int)c] == CCTL)
20247d8814cSJilles Tjoelker USTPUTC(CTLESC, p);
20347d8814cSJilles Tjoelker USTPUTC(c, p);
20447d8814cSJilles Tjoelker }
20547d8814cSJilles Tjoelker return (p);
20647d8814cSJilles Tjoelker }
2070e39c931SJilles Tjoelker #define STPUTS_SPLIT(data, syntax, flag, p, dst) p = stputs_split((data), syntax, flag, p, dst)
20847d8814cSJilles Tjoelker
2094b88c807SRodney W. Grimes /*
2102ca3d70fSJilles Tjoelker * Perform expansions on an argument, placing the resulting list of arguments
2112ca3d70fSJilles Tjoelker * in arglist. Parameter expansion, command substitution and arithmetic
2122ca3d70fSJilles Tjoelker * expansion are always performed; additional expansions can be requested
2132ca3d70fSJilles Tjoelker * via flag (EXP_*).
2142ca3d70fSJilles Tjoelker * The result is left in the stack string.
2153e0b768cSJilles Tjoelker * When arglist is NULL, perform here document expansion.
2162ca3d70fSJilles Tjoelker *
217b9807277SJilles Tjoelker * When doing something that may cause this to be re-entered, make sure
218b9807277SJilles Tjoelker * the stack string is empty via grabstackstr() and do not assume expdest
219b9807277SJilles Tjoelker * remains valid.
2204b88c807SRodney W. Grimes */
2214b88c807SRodney W. Grimes void
expandarg(union node * arg,struct arglist * arglist,int flag)2225134c3f7SWarner Losh expandarg(union node *arg, struct arglist *arglist, int flag)
2234b88c807SRodney W. Grimes {
22447d8814cSJilles Tjoelker struct worddest exparg;
225439948cdSJilles Tjoelker struct nodelist *argbackq;
2264b88c807SRodney W. Grimes
2270e39c931SJilles Tjoelker if (fflag)
2280e39c931SJilles Tjoelker flag &= ~EXP_GLOB;
2294b88c807SRodney W. Grimes argbackq = arg->narg.backquote;
2300e39c931SJilles Tjoelker exparg.list = arglist;
23147d8814cSJilles Tjoelker exparg.state = WORD_IDLE;
2324b88c807SRodney W. Grimes STARTSTACKSTR(expdest);
233439948cdSJilles Tjoelker argstr(arg->narg.text, &argbackq, flag, &exparg);
2344b88c807SRodney W. Grimes if (arglist == NULL) {
235292e6676SJilles Tjoelker STACKSTRNUL(expdest);
2364b88c807SRodney W. Grimes return; /* here document expanded */
2374b88c807SRodney W. Grimes }
2380e39c931SJilles Tjoelker if ((flag & EXP_SPLIT) == 0 || expdest != stackblock() ||
23947d8814cSJilles Tjoelker exparg.state == WORD_QUOTEMARK) {
2404b88c807SRodney W. Grimes STPUTC('\0', expdest);
2410e39c931SJilles Tjoelker if (flag & EXP_SPLIT) {
2420e39c931SJilles Tjoelker if (flag & EXP_GLOB)
2430e39c931SJilles Tjoelker expandmeta(grabstackstr(expdest), exparg.list);
24447d8814cSJilles Tjoelker else
2450e39c931SJilles Tjoelker appendarglist(exparg.list, grabstackstr(expdest));
2460e39c931SJilles Tjoelker }
2470e39c931SJilles Tjoelker }
2480e39c931SJilles Tjoelker if ((flag & EXP_SPLIT) == 0)
24947d8814cSJilles Tjoelker appendarglist(arglist, grabstackstr(expdest));
2504b88c807SRodney W. Grimes }
2514b88c807SRodney W. Grimes
2524b88c807SRodney W. Grimes
2534b88c807SRodney W. Grimes
2544b88c807SRodney W. Grimes /*
2552ca3d70fSJilles Tjoelker * Perform parameter expansion, command substitution and arithmetic
2562ca3d70fSJilles Tjoelker * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
257ce16da82SJilles Tjoelker * Processing ends at a CTLENDVAR or CTLENDARI character as well as '\0'.
2582ca3d70fSJilles Tjoelker * This is used to expand word in ${var+word} etc.
2590e39c931SJilles Tjoelker * If EXP_GLOB or EXP_CASE are set, keep and/or generate CTLESC
2602ca3d70fSJilles Tjoelker * characters to allow for further processing.
26147d8814cSJilles Tjoelker *
2620e39c931SJilles Tjoelker * If EXP_SPLIT is set, dst receives any complete words produced.
2634b88c807SRodney W. Grimes */
26452d5897dSJilles Tjoelker static const char *
argstr(const char * p,struct nodelist ** restrict argbackq,int flag,struct worddest * dst)265439948cdSJilles Tjoelker argstr(const char *p, struct nodelist **restrict argbackq, int flag,
266439948cdSJilles Tjoelker 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:
311439948cdSJilles Tjoelker p = evalvar(p, argbackq, flag, dst);
3124b88c807SRodney W. Grimes break;
3134b88c807SRodney W. Grimes case CTLBACKQ:
3144b88c807SRodney W. Grimes case CTLBACKQ|CTLQUOTE:
315439948cdSJilles Tjoelker expbackq((*argbackq)->n, c & CTLQUOTE, flag, dst);
316439948cdSJilles Tjoelker *argbackq = (*argbackq)->next;
3174b88c807SRodney W. Grimes break;
318ce16da82SJilles Tjoelker case CTLARI:
319439948cdSJilles Tjoelker p = expari(p, argbackq, flag, dst);
3204b88c807SRodney W. Grimes break;
3214b88c807SRodney W. Grimes case ':':
3224b88c807SRodney W. Grimes case '=':
3234b88c807SRodney W. Grimes /*
3244b88c807SRodney W. Grimes * sort of a hack - expand tildes in variable
3254b88c807SRodney W. Grimes * assignments (after the first '=' and after ':'s).
3264b88c807SRodney W. Grimes */
32747d8814cSJilles Tjoelker if (split_lit && !lit_quoted &&
32847d8814cSJilles Tjoelker strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
3290e39c931SJilles Tjoelker NEXTWORD(c, flag, expdest, dst);
33047d8814cSJilles Tjoelker break;
33147d8814cSJilles Tjoelker }
3329d37e157SJilles Tjoelker USTPUTC(c, expdest);
333048f2667SJilles Tjoelker if (flag & EXP_VARTILDE && *p == '~' &&
334048f2667SJilles Tjoelker (c != '=' || firsteq)) {
335048f2667SJilles Tjoelker if (c == '=')
3364b88c807SRodney W. Grimes firsteq = 0;
3374b88c807SRodney W. Grimes p = exptilde(p, flag);
3384b88c807SRodney W. Grimes }
3394b88c807SRodney W. Grimes break;
3404b88c807SRodney W. Grimes default:
34147d8814cSJilles Tjoelker if (split_lit && !lit_quoted &&
34247d8814cSJilles Tjoelker strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
3430e39c931SJilles Tjoelker NEXTWORD(c, flag, expdest, dst);
34447d8814cSJilles Tjoelker break;
34547d8814cSJilles Tjoelker }
3469d37e157SJilles Tjoelker USTPUTC(c, expdest);
3474b88c807SRodney W. Grimes }
3484b88c807SRodney W. Grimes }
3494b88c807SRodney W. Grimes }
3504b88c807SRodney W. Grimes
3512ca3d70fSJilles Tjoelker /*
3522ca3d70fSJilles Tjoelker * Perform tilde expansion, placing the result in the stack string and
3532ca3d70fSJilles Tjoelker * returning the next position in the input string to process.
3542ca3d70fSJilles Tjoelker */
35552d5897dSJilles Tjoelker static const char *
exptilde(const char * p,int flag)35652d5897dSJilles Tjoelker exptilde(const char *p, int flag)
3574b88c807SRodney W. Grimes {
35852d5897dSJilles Tjoelker char c;
35952d5897dSJilles Tjoelker const char *startp = p;
36052d5897dSJilles Tjoelker const char *user;
3614b88c807SRodney W. Grimes struct passwd *pw;
3624b88c807SRodney W. Grimes char *home;
36352d5897dSJilles Tjoelker int len;
3644b88c807SRodney W. Grimes
3657034d8dfSJilles Tjoelker for (;;) {
3667034d8dfSJilles Tjoelker c = *p;
3674b88c807SRodney W. Grimes switch(c) {
36805c10507SJilles Tjoelker case CTLESC: /* This means CTL* are always considered quoted. */
36905c10507SJilles Tjoelker case CTLVAR:
37005c10507SJilles Tjoelker case CTLBACKQ:
37105c10507SJilles Tjoelker case CTLBACKQ | CTLQUOTE:
37205c10507SJilles Tjoelker case CTLARI:
37305c10507SJilles Tjoelker case CTLENDARI:
3745557a02aSTor Egge case CTLQUOTEMARK:
3755557a02aSTor Egge return (startp);
3764b88c807SRodney W. Grimes case ':':
3777034d8dfSJilles Tjoelker if ((flag & EXP_VARTILDE) == 0)
3784b88c807SRodney W. Grimes break;
3797034d8dfSJilles Tjoelker /* FALLTHROUGH */
3807034d8dfSJilles Tjoelker case '\0':
3814b88c807SRodney W. Grimes case '/':
382634e9188SJilles Tjoelker case CTLENDVAR:
38352d5897dSJilles Tjoelker len = p - startp - 1;
38452d5897dSJilles Tjoelker STPUTBIN(startp + 1, len, expdest);
38552d5897dSJilles Tjoelker STACKSTRNUL(expdest);
38652d5897dSJilles Tjoelker user = expdest - len;
38752d5897dSJilles Tjoelker if (*user == '\0') {
38833c5acf0SJilles Tjoelker home = lookupvar("HOME");
3894b88c807SRodney W. Grimes } else {
39052d5897dSJilles Tjoelker pw = getpwnam(user);
39133c5acf0SJilles Tjoelker home = pw != NULL ? pw->pw_dir : NULL;
3924b88c807SRodney W. Grimes }
39352d5897dSJilles Tjoelker STADJUST(-len, expdest);
39433c5acf0SJilles Tjoelker if (home == NULL || *home == '\0')
39533c5acf0SJilles Tjoelker return (startp);
39647d8814cSJilles Tjoelker strtodest(home, flag, VSNORMAL, 1, NULL);
3974b88c807SRodney W. Grimes return (p);
3984b88c807SRodney W. Grimes }
3997034d8dfSJilles Tjoelker p++;
4007034d8dfSJilles Tjoelker }
4017034d8dfSJilles Tjoelker }
4024b88c807SRodney W. Grimes
4034b88c807SRodney W. Grimes
4044b88c807SRodney W. Grimes /*
405ce16da82SJilles Tjoelker * Expand arithmetic expression.
4064b88c807SRodney W. Grimes */
40752d5897dSJilles Tjoelker static const char *
expari(const char * p,struct nodelist ** restrict argbackq,int flag,struct worddest * dst)408439948cdSJilles Tjoelker expari(const char *p, struct nodelist **restrict argbackq, int flag,
409439948cdSJilles Tjoelker struct worddest *dst)
4104b88c807SRodney W. Grimes {
411ce16da82SJilles Tjoelker char *q, *start;
412d9d588d4SStefan Farfeleder arith_t result;
4136f47734fSTor Egge int begoff;
4146f47734fSTor Egge int quoted;
415ce16da82SJilles Tjoelker int adj;
4164b88c807SRodney W. Grimes
417ce16da82SJilles Tjoelker quoted = *p++ == '"';
418ce16da82SJilles Tjoelker begoff = expdest - stackblock();
419439948cdSJilles Tjoelker p = argstr(p, argbackq, 0, NULL);
420ce16da82SJilles Tjoelker STPUTC('\0', expdest);
421ce16da82SJilles Tjoelker start = stackblock() + begoff;
422ce16da82SJilles Tjoelker
423593e925aSJilles Tjoelker q = grabstackstr(expdest);
424ce16da82SJilles Tjoelker result = arith(start);
425593e925aSJilles Tjoelker ungrabstackstr(q, expdest);
426ce16da82SJilles Tjoelker
427ce16da82SJilles Tjoelker start = stackblock() + begoff;
428ce16da82SJilles Tjoelker adj = start - expdest;
429ce16da82SJilles Tjoelker STADJUST(adj, expdest);
430ce16da82SJilles Tjoelker
431ce16da82SJilles Tjoelker CHECKSTRSPACE((int)(DIGITS(result) + 1), expdest);
432ce16da82SJilles Tjoelker fmtstr(expdest, DIGITS(result), ARITH_FORMAT_STR, result);
433ce16da82SJilles Tjoelker adj = strlen(expdest);
434ce16da82SJilles Tjoelker STADJUST(adj, expdest);
4351b21b7faSJilles Tjoelker /*
4361b21b7faSJilles Tjoelker * If this is quoted, a '-' must not indicate a range in [...].
4371b21b7faSJilles Tjoelker * If this is not quoted, splitting may occur.
4381b21b7faSJilles Tjoelker */
4391b21b7faSJilles Tjoelker if (quoted ?
4401b21b7faSJilles Tjoelker result < 0 && begoff > 1 && flag & (EXP_GLOB | EXP_CASE) :
4411b21b7faSJilles Tjoelker flag & EXP_SPLIT)
4421b21b7faSJilles Tjoelker reprocess(expdest - adj - stackblock(), flag, VSNORMAL, quoted,
4431b21b7faSJilles Tjoelker dst);
444ce16da82SJilles Tjoelker return p;
4454b88c807SRodney W. Grimes }
4464b88c807SRodney W. Grimes
4474b88c807SRodney W. Grimes
4484b88c807SRodney W. Grimes /*
4492ca3d70fSJilles Tjoelker * Perform command substitution.
4504b88c807SRodney W. Grimes */
45188328642SDavid E. O'Brien static void
expbackq(union node * cmd,int quoted,int flag,struct worddest * dst)45247d8814cSJilles Tjoelker expbackq(union node *cmd, int quoted, int flag, struct worddest *dst)
4534b88c807SRodney W. Grimes {
4544b88c807SRodney W. Grimes struct backcmd in;
4554b88c807SRodney W. Grimes int i;
4564b88c807SRodney W. Grimes char buf[128];
4574b88c807SRodney W. Grimes char *p;
4584b88c807SRodney W. Grimes char *dest = expdest;
4594b88c807SRodney W. Grimes char lastc;
4604b88c807SRodney W. Grimes char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
4610e39c931SJilles Tjoelker int quotes = flag & (EXP_GLOB | EXP_CASE);
46246c6b52dSJilles Tjoelker size_t nnl;
46347d8814cSJilles Tjoelker const char *ifs;
464d4993b6dSJilles Tjoelker int startloc;
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;
472d4993b6dSJilles Tjoelker startloc = dest - stackblock();
47399907703SBill Fenner nnl = 0;
4740e39c931SJilles Tjoelker if (!quoted && flag & EXP_SPLIT)
47547d8814cSJilles Tjoelker ifs = ifsset() ? ifsval() : " \t\n";
47647d8814cSJilles Tjoelker else
47747d8814cSJilles Tjoelker ifs = "";
478b9807277SJilles Tjoelker /* Remove trailing newlines */
4794b88c807SRodney W. Grimes for (;;) {
4804b88c807SRodney W. Grimes if (--in.nleft < 0) {
4814b88c807SRodney W. Grimes if (in.fd < 0)
4824b88c807SRodney W. Grimes break;
4833bb6ada2SJilles Tjoelker while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR)
4843bb6ada2SJilles Tjoelker ;
4854b88c807SRodney W. Grimes TRACE(("expbackq: read returns %d\n", i));
4864b88c807SRodney W. Grimes if (i <= 0)
4874b88c807SRodney W. Grimes break;
4884b88c807SRodney W. Grimes p = buf;
4894b88c807SRodney W. Grimes in.nleft = i - 1;
4904b88c807SRodney W. Grimes }
4914b88c807SRodney W. Grimes lastc = *p++;
49247d8814cSJilles Tjoelker if (lastc == '\0')
49347d8814cSJilles Tjoelker continue;
494d4993b6dSJilles Tjoelker if (nnl > 0 && lastc != '\n') {
4950e39c931SJilles Tjoelker NEXTWORD('\n', flag, dest, dst);
49647d8814cSJilles Tjoelker nnl = 0;
49799907703SBill Fenner }
498d4993b6dSJilles Tjoelker if (strchr(ifs, lastc) != NULL) {
499d4993b6dSJilles Tjoelker if (lastc == '\n')
500d4993b6dSJilles Tjoelker nnl++;
501d4993b6dSJilles Tjoelker else
5020e39c931SJilles Tjoelker NEXTWORD(lastc, flag, dest, dst);
503d4993b6dSJilles Tjoelker } else {
50447d8814cSJilles Tjoelker CHECKSTRSPACE(2, dest);
505fa0951d6SJilles Tjoelker if (quotes && syntax[(int)lastc] == CCTL)
506d8f32e72SJilles Tjoelker USTPUTC(CTLESC, dest);
507d8f32e72SJilles Tjoelker USTPUTC(lastc, dest);
5084b88c807SRodney W. Grimes }
5094b88c807SRodney W. Grimes }
510d4993b6dSJilles Tjoelker while (dest > stackblock() + startloc && STTOPC(dest) == '\n')
511d4993b6dSJilles Tjoelker STUNPUTC(dest);
512aa9caaf6SPeter Wemm
5134b88c807SRodney W. Grimes if (in.fd >= 0)
5144b88c807SRodney W. Grimes close(in.fd);
5154b88c807SRodney W. Grimes if (in.buf)
5164b88c807SRodney W. Grimes ckfree(in.buf);
517586fd248SJilles Tjoelker if (in.jp) {
518586fd248SJilles Tjoelker p = grabstackstr(dest);
51957b2932aSMartin Cracauer exitstatus = waitforjob(in.jp, (int *)NULL);
520586fd248SJilles Tjoelker ungrabstackstr(p, dest);
521586fd248SJilles Tjoelker }
522a54caffdSJilles Tjoelker TRACE(("expbackq: done\n"));
5234b88c807SRodney W. Grimes expdest = dest;
5244b88c807SRodney W. Grimes INTON;
5254b88c807SRodney W. Grimes }
5264b88c807SRodney W. Grimes
5274b88c807SRodney W. Grimes
5284b88c807SRodney W. Grimes
5297034d8dfSJilles Tjoelker static void
recordleft(const char * str,const char * loc,char * startp)5307034d8dfSJilles Tjoelker recordleft(const char *str, const char *loc, char *startp)
5317034d8dfSJilles Tjoelker {
5327034d8dfSJilles Tjoelker int amount;
5337034d8dfSJilles Tjoelker
5347034d8dfSJilles Tjoelker amount = ((str - 1) - (loc - startp)) - expdest;
5357034d8dfSJilles Tjoelker STADJUST(amount, expdest);
5367034d8dfSJilles Tjoelker while (loc != str - 1)
5377034d8dfSJilles Tjoelker *startp++ = *loc++;
5387034d8dfSJilles Tjoelker }
5397034d8dfSJilles Tjoelker
540e2708b16SJilles Tjoelker static const char *
subevalvar_trim(const char * p,struct nodelist ** restrict argbackq,int strloc,int subtype,int startloc)541e2708b16SJilles Tjoelker subevalvar_trim(const char *p, struct nodelist **restrict argbackq, int strloc,
542439948cdSJilles Tjoelker int subtype, int startloc)
543aa9caaf6SPeter Wemm {
544aa9caaf6SPeter Wemm char *startp;
545aa9caaf6SPeter Wemm char *loc = NULL;
5464f2f5faaSJilles Tjoelker char *str;
547aa9caaf6SPeter Wemm int c = 0;
548ab0a2172SSteve Price int amount;
549ab0a2172SSteve Price
550e2708b16SJilles Tjoelker p = argstr(p, argbackq, EXP_CASE | EXP_TILDE, NULL);
551aa9caaf6SPeter Wemm STACKSTRNUL(expdest);
552aa9caaf6SPeter Wemm startp = stackblock() + startloc;
553ab0a2172SSteve Price str = stackblock() + strloc;
554aa9caaf6SPeter Wemm
555aa9caaf6SPeter Wemm switch (subtype) {
556aa9caaf6SPeter Wemm case VSTRIMLEFT:
55796522b88SSteve Price for (loc = startp; loc < str; loc++) {
558aa9caaf6SPeter Wemm c = *loc;
559aa9caaf6SPeter Wemm *loc = '\0';
56047d8814cSJilles Tjoelker if (patmatch(str, startp)) {
561aa9caaf6SPeter Wemm *loc = c;
5627034d8dfSJilles Tjoelker recordleft(str, loc, startp);
563e2708b16SJilles Tjoelker return p;
564aa9caaf6SPeter Wemm }
565aa9caaf6SPeter Wemm *loc = c;
566aa9caaf6SPeter Wemm }
56747d8814cSJilles Tjoelker break;
568aa9caaf6SPeter Wemm
569aa9caaf6SPeter Wemm case VSTRIMLEFTMAX:
5708b220a61STor Egge for (loc = str - 1; loc >= startp;) {
571aa9caaf6SPeter Wemm c = *loc;
572aa9caaf6SPeter Wemm *loc = '\0';
57347d8814cSJilles Tjoelker if (patmatch(str, startp)) {
574aa9caaf6SPeter Wemm *loc = c;
5757034d8dfSJilles Tjoelker recordleft(str, loc, startp);
576e2708b16SJilles Tjoelker return p;
577aa9caaf6SPeter Wemm }
578aa9caaf6SPeter Wemm *loc = c;
5798b220a61STor Egge loc--;
5808b220a61STor Egge }
58147d8814cSJilles Tjoelker break;
582aa9caaf6SPeter Wemm
583aa9caaf6SPeter Wemm case VSTRIMRIGHT:
5848b220a61STor Egge for (loc = str - 1; loc >= startp;) {
58547d8814cSJilles Tjoelker if (patmatch(str, loc)) {
586ab0a2172SSteve Price amount = loc - expdest;
587ab0a2172SSteve Price STADJUST(amount, expdest);
588e2708b16SJilles Tjoelker return p;
589aa9caaf6SPeter Wemm }
5908b220a61STor Egge loc--;
5918b220a61STor Egge }
59247d8814cSJilles Tjoelker break;
593aa9caaf6SPeter Wemm
594aa9caaf6SPeter Wemm case VSTRIMRIGHTMAX:
595aa9caaf6SPeter Wemm for (loc = startp; loc < str - 1; loc++) {
59647d8814cSJilles Tjoelker if (patmatch(str, loc)) {
597ab0a2172SSteve Price amount = loc - expdest;
598ab0a2172SSteve Price STADJUST(amount, expdest);
599e2708b16SJilles Tjoelker return p;
600aa9caaf6SPeter Wemm }
601aa9caaf6SPeter Wemm }
60247d8814cSJilles Tjoelker break;
603aa9caaf6SPeter Wemm
604aa9caaf6SPeter Wemm
605aa9caaf6SPeter Wemm default:
606aa9caaf6SPeter Wemm abort();
607aa9caaf6SPeter Wemm }
60847d8814cSJilles Tjoelker amount = (expdest - stackblock() - strloc) + 1;
60947d8814cSJilles Tjoelker STADJUST(-amount, expdest);
610e2708b16SJilles Tjoelker return p;
611aa9caaf6SPeter Wemm }
612aa9caaf6SPeter Wemm
613aa9caaf6SPeter Wemm
614e59833ccSJilles Tjoelker static const char *
subevalvar_misc(const char * p,struct nodelist ** restrict argbackq,const char * var,int subtype,int startloc,int varflags)615e59833ccSJilles Tjoelker subevalvar_misc(const char *p, struct nodelist **restrict argbackq,
616e59833ccSJilles Tjoelker const char *var, int subtype, int startloc, int varflags)
6174f2f5faaSJilles Tjoelker {
618*468ed396SJilles Tjoelker const char *end;
6194f2f5faaSJilles Tjoelker char *startp;
6204f2f5faaSJilles Tjoelker int amount;
6214f2f5faaSJilles Tjoelker
622*468ed396SJilles Tjoelker end = argstr(p, argbackq, EXP_TILDE, NULL);
6234f2f5faaSJilles Tjoelker STACKSTRNUL(expdest);
6244f2f5faaSJilles Tjoelker startp = stackblock() + startloc;
6254f2f5faaSJilles Tjoelker
6264f2f5faaSJilles Tjoelker switch (subtype) {
6274f2f5faaSJilles Tjoelker case VSASSIGN:
6284f2f5faaSJilles Tjoelker setvar(var, startp, 0);
6294f2f5faaSJilles Tjoelker amount = startp - expdest;
6304f2f5faaSJilles Tjoelker STADJUST(amount, expdest);
631*468ed396SJilles Tjoelker return end;
6324f2f5faaSJilles Tjoelker
6334f2f5faaSJilles Tjoelker case VSQUESTION:
6344f2f5faaSJilles Tjoelker if (*p != CTLENDVAR) {
6354f2f5faaSJilles Tjoelker outfmt(out2, "%s\n", startp);
6364f2f5faaSJilles Tjoelker error((char *)NULL);
6374f2f5faaSJilles Tjoelker }
6384f2f5faaSJilles Tjoelker error("%.*s: parameter %snot set", (int)(p - var - 1),
6394f2f5faaSJilles Tjoelker var, (varflags & VSNUL) ? "null or " : "");
6404f2f5faaSJilles Tjoelker
6414f2f5faaSJilles Tjoelker default:
6424f2f5faaSJilles Tjoelker abort();
6434f2f5faaSJilles Tjoelker }
6444f2f5faaSJilles Tjoelker }
6454f2f5faaSJilles Tjoelker
6464f2f5faaSJilles Tjoelker
6474b88c807SRodney W. Grimes /*
6484b88c807SRodney W. Grimes * Expand a variable, and return a pointer to the next character in the
6494b88c807SRodney W. Grimes * input string.
6504b88c807SRodney W. Grimes */
6514b88c807SRodney W. Grimes
65252d5897dSJilles Tjoelker static const char *
evalvar(const char * p,struct nodelist ** restrict argbackq,int flag,struct worddest * dst)653439948cdSJilles Tjoelker evalvar(const char *p, struct nodelist **restrict argbackq, int flag,
654439948cdSJilles Tjoelker struct worddest *dst)
6554b88c807SRodney W. Grimes {
6564b88c807SRodney W. Grimes int subtype;
6574b88c807SRodney W. Grimes int varflags;
65852d5897dSJilles Tjoelker const char *var;
65961346cbdSJilles Tjoelker const char *val;
660c4e5a8a8STor Egge int patloc;
6614b88c807SRodney W. Grimes int c;
6624b88c807SRodney W. Grimes int set;
6634b88c807SRodney W. Grimes int special;
6644b88c807SRodney W. Grimes int startloc;
665aa9caaf6SPeter Wemm int varlen;
6664c244ed2SJilles Tjoelker int varlenb;
66747d8814cSJilles Tjoelker char buf[21];
6684b88c807SRodney W. Grimes
669bb4f73caSStefan Farfeleder varflags = (unsigned char)*p++;
6704b88c807SRodney W. Grimes subtype = varflags & VSTYPE;
6714b88c807SRodney W. Grimes var = p;
6724b88c807SRodney W. Grimes special = 0;
6734b88c807SRodney W. Grimes if (! is_name(*p))
6744b88c807SRodney W. Grimes special = 1;
6754b88c807SRodney W. Grimes p = strchr(p, '=') + 1;
676b71085aaSStefan Farfeleder if (varflags & VSLINENO) {
677b71085aaSStefan Farfeleder set = 1;
67854396489SJilles Tjoelker special = 1;
67954396489SJilles Tjoelker val = NULL;
680b71085aaSStefan Farfeleder } else if (special) {
68196522b88SSteve Price set = varisset(var, varflags & VSNUL);
6824b88c807SRodney W. Grimes val = NULL;
6834b88c807SRodney W. Grimes } else {
684b2acf887SMartin Cracauer val = bltinlookup(var, 1);
685aa9caaf6SPeter Wemm if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
6864b88c807SRodney W. Grimes val = NULL;
6874b88c807SRodney W. Grimes set = 0;
6884b88c807SRodney W. Grimes } else
6894b88c807SRodney W. Grimes set = 1;
6904b88c807SRodney W. Grimes }
691aa9caaf6SPeter Wemm varlen = 0;
6924b88c807SRodney W. Grimes startloc = expdest - stackblock();
69364254a66SJilles Tjoelker if (!set && uflag && *var != '@' && *var != '*') {
6941b5a48ffSTim J. Robbins switch (subtype) {
6951b5a48ffSTim J. Robbins case VSNORMAL:
6961b5a48ffSTim J. Robbins case VSTRIMLEFT:
6971b5a48ffSTim J. Robbins case VSTRIMLEFTMAX:
6981b5a48ffSTim J. Robbins case VSTRIMRIGHT:
6991b5a48ffSTim J. Robbins case VSTRIMRIGHTMAX:
7001b5a48ffSTim J. Robbins case VSLENGTH:
701024ae004SRuslan Ermilov error("%.*s: parameter not set", (int)(p - var - 1),
702024ae004SRuslan Ermilov var);
7031b5a48ffSTim J. Robbins }
7041b5a48ffSTim J. Robbins }
7054b88c807SRodney W. Grimes if (set && subtype != VSPLUS) {
7064b88c807SRodney W. Grimes /* insert the value of the variable */
7074b88c807SRodney W. Grimes if (special) {
70847d8814cSJilles Tjoelker if (varflags & VSLINENO) {
70947d8814cSJilles Tjoelker if (p - var > (ptrdiff_t)sizeof(buf))
71047d8814cSJilles Tjoelker abort();
71147d8814cSJilles Tjoelker memcpy(buf, var, p - var - 1);
71247d8814cSJilles Tjoelker buf[p - var - 1] = '\0';
71347d8814cSJilles Tjoelker strtodest(buf, flag, subtype,
71447d8814cSJilles Tjoelker varflags & VSQUOTE, dst);
71547d8814cSJilles Tjoelker } else
71647d8814cSJilles Tjoelker varvalue(var, varflags & VSQUOTE, subtype, flag,
71747d8814cSJilles Tjoelker dst);
718aa9caaf6SPeter Wemm if (subtype == VSLENGTH) {
7194c244ed2SJilles Tjoelker varlenb = expdest - stackblock() - startloc;
7204c244ed2SJilles Tjoelker varlen = varlenb;
7214c244ed2SJilles Tjoelker if (localeisutf8) {
7224c244ed2SJilles Tjoelker val = stackblock() + startloc;
7234c244ed2SJilles Tjoelker for (;val != expdest; val++)
7244c244ed2SJilles Tjoelker if ((*val & 0xC0) == 0x80)
7254c244ed2SJilles Tjoelker varlen--;
7264c244ed2SJilles Tjoelker }
7274c244ed2SJilles Tjoelker STADJUST(-varlenb, expdest);
728aa9caaf6SPeter Wemm }
7294b88c807SRodney W. Grimes } else {
730aa9caaf6SPeter Wemm if (subtype == VSLENGTH) {
731aa9caaf6SPeter Wemm for (;*val; val++)
7324c244ed2SJilles Tjoelker if (!localeisutf8 ||
7334c244ed2SJilles Tjoelker (*val & 0xC0) != 0x80)
734aa9caaf6SPeter Wemm varlen++;
735aa9caaf6SPeter Wemm }
736f7dea851SJilles Tjoelker else
7377034d8dfSJilles Tjoelker strtodest(val, flag, subtype,
73847d8814cSJilles Tjoelker varflags & VSQUOTE, dst);
7394b88c807SRodney W. Grimes }
740aa9caaf6SPeter Wemm }
741aa9caaf6SPeter Wemm
7424b88c807SRodney W. Grimes if (subtype == VSPLUS)
7434b88c807SRodney W. Grimes set = ! set;
744aa9caaf6SPeter Wemm
745aa9caaf6SPeter Wemm switch (subtype) {
746aa9caaf6SPeter Wemm case VSLENGTH:
74747d8814cSJilles Tjoelker cvtnum(varlen, buf);
74847d8814cSJilles Tjoelker strtodest(buf, flag, VSNORMAL, varflags & VSQUOTE, dst);
7497034d8dfSJilles Tjoelker break;
750aa9caaf6SPeter Wemm
751aa9caaf6SPeter Wemm case VSNORMAL:
7529e1bb30eSJilles Tjoelker return p;
753aa9caaf6SPeter Wemm
754aa9caaf6SPeter Wemm case VSPLUS:
755aa9caaf6SPeter Wemm case VSMINUS:
756aa9caaf6SPeter Wemm if (!set) {
7579e1bb30eSJilles Tjoelker return argstr(p, argbackq,
758439948cdSJilles Tjoelker flag | (flag & EXP_SPLIT ? EXP_SPLIT_LIT : 0) |
75947d8814cSJilles Tjoelker (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0), dst);
760aa9caaf6SPeter Wemm }
761aa9caaf6SPeter Wemm break;
762aa9caaf6SPeter Wemm
763aa9caaf6SPeter Wemm case VSTRIMLEFT:
764aa9caaf6SPeter Wemm case VSTRIMLEFTMAX:
765aa9caaf6SPeter Wemm case VSTRIMRIGHT:
766aa9caaf6SPeter Wemm case VSTRIMRIGHTMAX:
7679e1bb30eSJilles Tjoelker if (!set)
768aa9caaf6SPeter Wemm break;
769aa9caaf6SPeter Wemm /*
770aa9caaf6SPeter Wemm * Terminate the string and start recording the pattern
771aa9caaf6SPeter Wemm * right after it
772aa9caaf6SPeter Wemm */
773aa9caaf6SPeter Wemm STPUTC('\0', expdest);
774c4e5a8a8STor Egge patloc = expdest - stackblock();
775e2708b16SJilles Tjoelker p = subevalvar_trim(p, argbackq, patloc, subtype, startloc);
77647d8814cSJilles Tjoelker reprocess(startloc, flag, VSNORMAL, varflags & VSQUOTE, dst);
7770e39c931SJilles Tjoelker if (flag & EXP_SPLIT && *var == '@' && varflags & VSQUOTE)
77847d8814cSJilles Tjoelker dst->state = WORD_QUOTEMARK;
779e2708b16SJilles Tjoelker return p;
780aa9caaf6SPeter Wemm
781aa9caaf6SPeter Wemm case VSASSIGN:
782aa9caaf6SPeter Wemm case VSQUESTION:
783aa9caaf6SPeter Wemm if (!set) {
784e59833ccSJilles Tjoelker p = subevalvar_misc(p, argbackq, var, subtype,
78556bf1d61SJilles Tjoelker startloc, varflags);
78656bf1d61SJilles Tjoelker /* assert(subtype == VSASSIGN); */
787e59833ccSJilles Tjoelker val = lookupvar(var);
788e59833ccSJilles Tjoelker strtodest(val, flag, subtype, varflags & VSQUOTE, dst);
789e59833ccSJilles Tjoelker return p;
790ab0a2172SSteve Price }
791aa9caaf6SPeter Wemm break;
792aa9caaf6SPeter Wemm
79362addaefSStefan Farfeleder case VSERROR:
79462addaefSStefan Farfeleder c = p - var - 1;
79562addaefSStefan Farfeleder error("${%.*s%s}: Bad substitution", c, var,
79662addaefSStefan Farfeleder (c > 0 && *p != CTLENDVAR) ? "..." : "");
79762addaefSStefan Farfeleder
798aa9caaf6SPeter Wemm default:
799aa9caaf6SPeter Wemm abort();
8004b88c807SRodney W. Grimes }
801aa9caaf6SPeter Wemm
8029e1bb30eSJilles Tjoelker { /* skip to end of alternative */
8034b88c807SRodney W. Grimes int nesting = 1;
8044b88c807SRodney W. Grimes for (;;) {
8054b88c807SRodney W. Grimes if ((c = *p++) == CTLESC)
8064b88c807SRodney W. Grimes p++;
8079e1bb30eSJilles Tjoelker else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE))
808439948cdSJilles Tjoelker *argbackq = (*argbackq)->next;
8099e1bb30eSJilles Tjoelker else if (c == CTLVAR) {
8104b88c807SRodney W. Grimes if ((*p++ & VSTYPE) != VSNORMAL)
8114b88c807SRodney W. Grimes nesting++;
8124b88c807SRodney W. Grimes } else if (c == CTLENDVAR) {
8134b88c807SRodney W. Grimes if (--nesting == 0)
8144b88c807SRodney W. Grimes break;
8154b88c807SRodney W. Grimes }
8164b88c807SRodney W. Grimes }
8174b88c807SRodney W. Grimes }
8184b88c807SRodney W. Grimes return p;
8194b88c807SRodney W. Grimes }
8204b88c807SRodney W. Grimes
8214b88c807SRodney W. Grimes
8224b88c807SRodney W. Grimes
8234b88c807SRodney W. Grimes /*
824b9807277SJilles Tjoelker * Test whether a special or positional parameter is set.
8254b88c807SRodney W. Grimes */
8264b88c807SRodney W. Grimes
82788328642SDavid E. O'Brien static int
varisset(const char * name,int nulok)82861346cbdSJilles Tjoelker varisset(const char *name, int nulok)
8294b88c807SRodney W. Grimes {
8304b88c807SRodney W. Grimes
83196522b88SSteve Price if (*name == '!')
832ed4c3b5fSJilles Tjoelker return backgndpidset();
83396522b88SSteve Price else if (*name == '@' || *name == '*') {
8344b88c807SRodney W. Grimes if (*shellparam.p == NULL)
8354b88c807SRodney W. Grimes return 0;
83696522b88SSteve Price
83796522b88SSteve Price if (nulok) {
83896522b88SSteve Price char **av;
83996522b88SSteve Price
84096522b88SSteve Price for (av = shellparam.p; *av; av++)
84196522b88SSteve Price if (**av != '\0')
84296522b88SSteve Price return 1;
84396522b88SSteve Price return 0;
84496522b88SSteve Price }
8455c817731SPeter Wemm } else if (is_digit(*name)) {
84696522b88SSteve Price char *ap;
8477b9104c0SJilles Tjoelker long num;
84896522b88SSteve Price
8497b9104c0SJilles Tjoelker errno = 0;
8507b9104c0SJilles Tjoelker num = strtol(name, NULL, 10);
8517b9104c0SJilles Tjoelker if (errno != 0 || num > shellparam.nparam)
85296522b88SSteve Price return 0;
85396522b88SSteve Price
85496522b88SSteve Price if (num == 0)
85596522b88SSteve Price ap = arg0;
85696522b88SSteve Price else
85796522b88SSteve Price ap = shellparam.p[num - 1];
85896522b88SSteve Price
85996522b88SSteve Price if (nulok && (ap == NULL || *ap == '\0'))
8604b88c807SRodney W. Grimes return 0;
8614b88c807SRodney W. Grimes }
8624b88c807SRodney W. Grimes return 1;
8634b88c807SRodney W. Grimes }
8644b88c807SRodney W. Grimes
865f7dea851SJilles Tjoelker static void
strtodest(const char * p,int flag,int subtype,int quoted,struct worddest * dst)86647d8814cSJilles Tjoelker strtodest(const char *p, int flag, int subtype, int quoted,
86747d8814cSJilles Tjoelker struct worddest *dst)
868f7dea851SJilles Tjoelker {
86947d8814cSJilles Tjoelker if (subtype == VSLENGTH || subtype == VSTRIMLEFT ||
87047d8814cSJilles Tjoelker subtype == VSTRIMLEFTMAX || subtype == VSTRIMRIGHT ||
87147d8814cSJilles Tjoelker subtype == VSTRIMRIGHTMAX)
87247d8814cSJilles Tjoelker STPUTS(p, expdest);
8730e39c931SJilles Tjoelker else if (flag & EXP_SPLIT && !quoted && dst != NULL)
8740e39c931SJilles Tjoelker STPUTS_SPLIT(p, BASESYNTAX, flag, expdest, dst);
8750e39c931SJilles Tjoelker else if (flag & (EXP_GLOB | EXP_CASE))
876f7dea851SJilles Tjoelker STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
877f7dea851SJilles Tjoelker else
878f7dea851SJilles Tjoelker STPUTS(p, expdest);
879f7dea851SJilles Tjoelker }
8804b88c807SRodney W. Grimes
88147d8814cSJilles Tjoelker static void
reprocess(int startloc,int flag,int subtype,int quoted,struct worddest * dst)88247d8814cSJilles Tjoelker reprocess(int startloc, int flag, int subtype, int quoted,
88347d8814cSJilles Tjoelker struct worddest *dst)
88447d8814cSJilles Tjoelker {
88547d8814cSJilles Tjoelker static char *buf = NULL;
88647d8814cSJilles Tjoelker static size_t buflen = 0;
88747d8814cSJilles Tjoelker char *startp;
88847d8814cSJilles Tjoelker size_t len, zpos, zlen;
88947d8814cSJilles Tjoelker
89047d8814cSJilles Tjoelker startp = stackblock() + startloc;
89147d8814cSJilles Tjoelker len = expdest - startp;
8924269bba2SJilles Tjoelker if (len >= SIZE_MAX / 2 || len > PTRDIFF_MAX)
89347d8814cSJilles Tjoelker abort();
89447d8814cSJilles Tjoelker INTOFF;
89547d8814cSJilles Tjoelker if (len >= buflen) {
89647d8814cSJilles Tjoelker ckfree(buf);
89747d8814cSJilles Tjoelker buf = NULL;
89847d8814cSJilles Tjoelker }
89947d8814cSJilles Tjoelker if (buflen < 128)
90047d8814cSJilles Tjoelker buflen = 128;
90147d8814cSJilles Tjoelker while (len >= buflen)
90247d8814cSJilles Tjoelker buflen <<= 1;
90347d8814cSJilles Tjoelker if (buf == NULL)
90447d8814cSJilles Tjoelker buf = ckmalloc(buflen);
90547d8814cSJilles Tjoelker INTON;
90647d8814cSJilles Tjoelker memcpy(buf, startp, len);
90747d8814cSJilles Tjoelker buf[len] = '\0';
9084269bba2SJilles Tjoelker STADJUST(-(ptrdiff_t)len, expdest);
90947d8814cSJilles Tjoelker for (zpos = 0;;) {
91047d8814cSJilles Tjoelker zlen = strlen(buf + zpos);
91147d8814cSJilles Tjoelker strtodest(buf + zpos, flag, subtype, quoted, dst);
91247d8814cSJilles Tjoelker zpos += zlen + 1;
91347d8814cSJilles Tjoelker if (zpos == len + 1)
91447d8814cSJilles Tjoelker break;
9150e39c931SJilles Tjoelker if (flag & EXP_SPLIT && (quoted || (zlen > 0 && zpos < len)))
9160e39c931SJilles Tjoelker NEXTWORD('\0', flag, expdest, dst);
91747d8814cSJilles Tjoelker }
91847d8814cSJilles Tjoelker }
91947d8814cSJilles Tjoelker
9204b88c807SRodney W. Grimes /*
921b9807277SJilles Tjoelker * Add the value of a special or positional parameter to the stack string.
9224b88c807SRodney W. Grimes */
9234b88c807SRodney W. Grimes
92488328642SDavid E. O'Brien static void
varvalue(const char * name,int quoted,int subtype,int flag,struct worddest * dst)92547d8814cSJilles Tjoelker varvalue(const char *name, int quoted, int subtype, int flag,
92647d8814cSJilles Tjoelker struct worddest *dst)
9274b88c807SRodney W. Grimes {
9284b88c807SRodney W. Grimes int num;
9294b88c807SRodney W. Grimes char *p;
9304b88c807SRodney W. Grimes int i;
93147d8814cSJilles Tjoelker int splitlater;
9323fb51b3aSJilles Tjoelker char sep[2];
9334b88c807SRodney W. Grimes char **ap;
93447d8814cSJilles Tjoelker char buf[(NSHORTOPTS > 10 ? NSHORTOPTS : 10) + 1];
93547d8814cSJilles Tjoelker
93647d8814cSJilles Tjoelker if (subtype == VSLENGTH)
93747d8814cSJilles Tjoelker flag &= ~EXP_FULL;
93847d8814cSJilles Tjoelker splitlater = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
93947d8814cSJilles Tjoelker subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX;
9404b88c807SRodney W. Grimes
9415c817731SPeter Wemm switch (*name) {
9424b88c807SRodney W. Grimes case '$':
9434b88c807SRodney W. Grimes num = rootpid;
944622fdf32SJilles Tjoelker break;
9454b88c807SRodney W. Grimes case '?':
946aa9caaf6SPeter Wemm num = oexitstatus;
947622fdf32SJilles Tjoelker break;
9484b88c807SRodney W. Grimes case '#':
9494b88c807SRodney W. Grimes num = shellparam.nparam;
950622fdf32SJilles Tjoelker break;
9514b88c807SRodney W. Grimes case '!':
952ed4c3b5fSJilles Tjoelker num = backgndpidval();
9534b88c807SRodney W. Grimes break;
9544b88c807SRodney W. Grimes case '-':
95547d8814cSJilles Tjoelker p = buf;
95662c37116SJilles Tjoelker for (i = 0 ; i < NSHORTOPTS ; i++) {
9573da40d4aSJilles Tjoelker if (optval[i])
9583da40d4aSJilles Tjoelker *p++ = optletter[i];
9594b88c807SRodney W. Grimes }
96047d8814cSJilles Tjoelker *p = '\0';
96147d8814cSJilles Tjoelker strtodest(buf, flag, subtype, quoted, dst);
962622fdf32SJilles Tjoelker return;
9634b88c807SRodney W. Grimes case '@':
9640e39c931SJilles Tjoelker if (flag & EXP_SPLIT && quoted) {
9654b88c807SRodney W. Grimes for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
96647d8814cSJilles Tjoelker strtodest(p, flag, subtype, quoted, dst);
96747d8814cSJilles Tjoelker if (*ap) {
96847d8814cSJilles Tjoelker if (splitlater)
9696f47734fSTor Egge STPUTC('\0', expdest);
97047d8814cSJilles Tjoelker else
9710e39c931SJilles Tjoelker NEXTWORD('\0', flag, expdest,
9720e39c931SJilles Tjoelker dst);
9736f47734fSTor Egge }
97447d8814cSJilles Tjoelker }
97547d8814cSJilles Tjoelker if (shellparam.nparam > 0)
97647d8814cSJilles Tjoelker dst->state = WORD_QUOTEMARK;
977622fdf32SJilles Tjoelker return;
9786f47734fSTor Egge }
9790d9f1a69SPhilippe Charnier /* FALLTHROUGH */
9806f47734fSTor Egge case '*':
981f7d95a07SRalf S. Engelschall if (ifsset())
9823fb51b3aSJilles Tjoelker sep[0] = ifsval()[0];
9836f47734fSTor Egge else
9843fb51b3aSJilles Tjoelker sep[0] = ' ';
9853fb51b3aSJilles Tjoelker sep[1] = '\0';
9866f47734fSTor Egge for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
98747d8814cSJilles Tjoelker strtodest(p, flag, subtype, quoted, dst);
988715a0dd5SJilles Tjoelker if (!*ap)
989715a0dd5SJilles Tjoelker break;
9903fb51b3aSJilles Tjoelker if (sep[0])
99147d8814cSJilles Tjoelker strtodest(sep, flag, subtype, quoted, dst);
9920e39c931SJilles Tjoelker else if (flag & EXP_SPLIT && !quoted && **ap != '\0') {
99347d8814cSJilles Tjoelker if (splitlater)
9943fb51b3aSJilles Tjoelker STPUTC('\0', expdest);
99547d8814cSJilles Tjoelker else
9960e39c931SJilles Tjoelker NEXTWORD('\0', flag, expdest, dst);
99747d8814cSJilles Tjoelker }
9984b88c807SRodney W. Grimes }
999622fdf32SJilles Tjoelker return;
10004b88c807SRodney W. Grimes default:
10015c817731SPeter Wemm if (is_digit(*name)) {
10025c817731SPeter Wemm num = atoi(name);
10035ddabb83SJilles Tjoelker if (num == 0)
10045ddabb83SJilles Tjoelker p = arg0;
10055ddabb83SJilles Tjoelker else if (num > 0 && num <= shellparam.nparam)
10065c817731SPeter Wemm p = shellparam.p[num - 1];
10075ddabb83SJilles Tjoelker else
1008622fdf32SJilles Tjoelker return;
100947d8814cSJilles Tjoelker strtodest(p, flag, subtype, quoted, dst);
10104b88c807SRodney W. Grimes }
1011622fdf32SJilles Tjoelker return;
10124b88c807SRodney W. Grimes }
101347d8814cSJilles Tjoelker cvtnum(num, buf);
101447d8814cSJilles Tjoelker strtodest(buf, flag, subtype, quoted, dst);
10154b88c807SRodney W. Grimes }
10164b88c807SRodney W. Grimes
10174b88c807SRodney W. Grimes
10184b88c807SRodney W. Grimes
1019aa7b6f82SDavid E. O'Brien static char expdir[PATH_MAX];
1020c8a3d81fSJilles Tjoelker #define expdir_end (expdir + sizeof(expdir))
10214b88c807SRodney W. Grimes
10222ca3d70fSJilles Tjoelker /*
10232ca3d70fSJilles Tjoelker * Perform pathname generation and remove control characters.
1024bc57b4d4SJilles Tjoelker * At this point, the only control characters should be CTLESC.
10258ef0ae8aSJilles Tjoelker * The results are stored in the list dstlist.
10262ca3d70fSJilles Tjoelker */
102788328642SDavid E. O'Brien static void
expandmeta(char * pattern,struct arglist * dstlist)10280e39c931SJilles Tjoelker expandmeta(char *pattern, struct arglist *dstlist)
10294b88c807SRodney W. Grimes {
10304b88c807SRodney W. Grimes char *p;
10318ef0ae8aSJilles Tjoelker int firstmatch;
10324b88c807SRodney W. Grimes char c;
10334b88c807SRodney W. Grimes
10348ef0ae8aSJilles Tjoelker firstmatch = dstlist->count;
10350e39c931SJilles Tjoelker p = pattern;
1036622fdf32SJilles Tjoelker for (; (c = *p) != '\0'; p++) {
1037622fdf32SJilles Tjoelker /* fast check for meta chars */
1038622fdf32SJilles Tjoelker if (c == '*' || c == '?' || c == '[') {
10394b88c807SRodney W. Grimes INTOFF;
10400e39c931SJilles Tjoelker expmeta(expdir, pattern, dstlist);
10414b88c807SRodney W. Grimes INTON;
1042622fdf32SJilles Tjoelker break;
1043622fdf32SJilles Tjoelker }
1044622fdf32SJilles Tjoelker }
10458ef0ae8aSJilles Tjoelker if (dstlist->count == firstmatch) {
10464b88c807SRodney W. Grimes /*
10474b88c807SRodney W. Grimes * no matches
10484b88c807SRodney W. Grimes */
10490e39c931SJilles Tjoelker rmescapes(pattern);
10500e39c931SJilles Tjoelker appendarglist(dstlist, pattern);
10514b88c807SRodney W. Grimes } else {
10528ef0ae8aSJilles Tjoelker qsort(&dstlist->args[firstmatch],
10538ef0ae8aSJilles Tjoelker dstlist->count - firstmatch,
10548ef0ae8aSJilles Tjoelker sizeof(dstlist->args[0]), expsortcmp);
10554b88c807SRodney W. Grimes }
10564b88c807SRodney W. Grimes }
10574b88c807SRodney W. Grimes
10584b88c807SRodney W. Grimes
10594b88c807SRodney W. Grimes /*
10604b88c807SRodney W. Grimes * Do metacharacter (i.e. *, ?, [...]) expansion.
10614b88c807SRodney W. Grimes */
10624b88c807SRodney W. Grimes
106388328642SDavid E. O'Brien static void
expmeta(char * enddir,char * name,struct arglist * arglist)10648ef0ae8aSJilles Tjoelker expmeta(char *enddir, char *name, struct arglist *arglist)
10654b88c807SRodney W. Grimes {
106646c6b52dSJilles Tjoelker const char *p;
106746c6b52dSJilles Tjoelker const char *q;
106846c6b52dSJilles Tjoelker const char *start;
10694b88c807SRodney W. Grimes char *endname;
10704b88c807SRodney W. Grimes int metaflag;
10714b88c807SRodney W. Grimes struct stat statb;
10724b88c807SRodney W. Grimes DIR *dirp;
10734b88c807SRodney W. Grimes struct dirent *dp;
10744b88c807SRodney W. Grimes int atend;
10754b88c807SRodney W. Grimes int matchdot;
10764710b07eSJilles Tjoelker int esc;
10777a2b9d4bSJilles Tjoelker int namlen;
10784b88c807SRodney W. Grimes
10794b88c807SRodney W. Grimes metaflag = 0;
10804b88c807SRodney W. Grimes start = name;
10814710b07eSJilles Tjoelker for (p = name; esc = 0, *p; p += esc + 1) {
10824b88c807SRodney W. Grimes if (*p == '*' || *p == '?')
10834b88c807SRodney W. Grimes metaflag = 1;
10844b88c807SRodney W. Grimes else if (*p == '[') {
10854b88c807SRodney W. Grimes q = p + 1;
1086ea1376dfSAndrey A. Chernov if (*q == '!' || *q == '^')
10874b88c807SRodney W. Grimes q++;
10884b88c807SRodney W. Grimes for (;;) {
10894b88c807SRodney W. Grimes if (*q == CTLESC)
10904b88c807SRodney W. Grimes q++;
10914b88c807SRodney W. Grimes if (*q == '/' || *q == '\0')
10924b88c807SRodney W. Grimes break;
10934b88c807SRodney W. Grimes if (*++q == ']') {
10944b88c807SRodney W. Grimes metaflag = 1;
10954b88c807SRodney W. Grimes break;
10964b88c807SRodney W. Grimes }
10974b88c807SRodney W. Grimes }
10984b88c807SRodney W. Grimes } else if (*p == '\0')
10994b88c807SRodney W. Grimes break;
11004710b07eSJilles Tjoelker else {
11014710b07eSJilles Tjoelker if (*p == CTLESC)
11024710b07eSJilles Tjoelker esc++;
11034710b07eSJilles Tjoelker if (p[esc] == '/') {
11044b88c807SRodney W. Grimes if (metaflag)
11054b88c807SRodney W. Grimes break;
11064710b07eSJilles Tjoelker start = p + esc + 1;
11074710b07eSJilles Tjoelker }
11084b88c807SRodney W. Grimes }
11094b88c807SRodney W. Grimes }
11104b88c807SRodney W. Grimes if (metaflag == 0) { /* we've reached the end of the file name */
11114b88c807SRodney W. Grimes if (enddir != expdir)
11124b88c807SRodney W. Grimes metaflag++;
11134b88c807SRodney W. Grimes for (p = name ; ; p++) {
11144b88c807SRodney W. Grimes if (*p == CTLESC)
11154b88c807SRodney W. Grimes p++;
11164b88c807SRodney W. Grimes *enddir++ = *p;
11174b88c807SRodney W. Grimes if (*p == '\0')
11184b88c807SRodney W. Grimes break;
1119c8a3d81fSJilles Tjoelker if (enddir == expdir_end)
1120c8a3d81fSJilles Tjoelker return;
11214b88c807SRodney W. Grimes }
11220e3e87bdSXin LI if (metaflag == 0 || lstat(expdir, &statb) >= 0)
11238ef0ae8aSJilles Tjoelker appendarglist(arglist, stsavestr(expdir));
11244b88c807SRodney W. Grimes return;
11254b88c807SRodney W. Grimes }
112646c6b52dSJilles Tjoelker endname = name + (p - name);
11274b88c807SRodney W. Grimes if (start != name) {
11284b88c807SRodney W. Grimes p = name;
11294b88c807SRodney W. Grimes while (p < start) {
11304b88c807SRodney W. Grimes if (*p == CTLESC)
11314b88c807SRodney W. Grimes p++;
11324b88c807SRodney W. Grimes *enddir++ = *p++;
1133c8a3d81fSJilles Tjoelker if (enddir == expdir_end)
1134c8a3d81fSJilles Tjoelker return;
11354b88c807SRodney W. Grimes }
11364b88c807SRodney W. Grimes }
11374b88c807SRodney W. Grimes if (enddir == expdir) {
11384b88c807SRodney W. Grimes p = ".";
11394b88c807SRodney W. Grimes } else if (enddir == expdir + 1 && *expdir == '/') {
11404b88c807SRodney W. Grimes p = "/";
11414b88c807SRodney W. Grimes } else {
11424b88c807SRodney W. Grimes p = expdir;
11434b88c807SRodney W. Grimes enddir[-1] = '\0';
11444b88c807SRodney W. Grimes }
11454b88c807SRodney W. Grimes if ((dirp = opendir(p)) == NULL)
11464b88c807SRodney W. Grimes return;
11474b88c807SRodney W. Grimes if (enddir != expdir)
11484b88c807SRodney W. Grimes enddir[-1] = '/';
11494b88c807SRodney W. Grimes if (*endname == 0) {
11504b88c807SRodney W. Grimes atend = 1;
11514b88c807SRodney W. Grimes } else {
11524b88c807SRodney W. Grimes atend = 0;
11534710b07eSJilles Tjoelker *endname = '\0';
11544710b07eSJilles Tjoelker endname += esc + 1;
11554b88c807SRodney W. Grimes }
11564b88c807SRodney W. Grimes matchdot = 0;
11576f47734fSTor Egge p = start;
11586f47734fSTor Egge if (*p == CTLESC)
11596f47734fSTor Egge p++;
11606f47734fSTor Egge if (*p == '.')
11614b88c807SRodney W. Grimes matchdot++;
11624b88c807SRodney W. Grimes while (! int_pending() && (dp = readdir(dirp)) != NULL) {
11634b88c807SRodney W. Grimes if (dp->d_name[0] == '.' && ! matchdot)
11644b88c807SRodney W. Grimes continue;
116547d8814cSJilles Tjoelker if (patmatch(start, dp->d_name)) {
11667a2b9d4bSJilles Tjoelker namlen = dp->d_namlen;
11677a2b9d4bSJilles Tjoelker if (enddir + namlen + 1 > expdir_end)
1168aa9caaf6SPeter Wemm continue;
11697a2b9d4bSJilles Tjoelker memcpy(enddir, dp->d_name, namlen + 1);
1170c8a3d81fSJilles Tjoelker if (atend)
11718ef0ae8aSJilles Tjoelker appendarglist(arglist, stsavestr(expdir));
1172c8a3d81fSJilles Tjoelker else {
11736e8db49aSJilles Tjoelker if (dp->d_type != DT_UNKNOWN &&
11746e8db49aSJilles Tjoelker dp->d_type != DT_DIR &&
11756e8db49aSJilles Tjoelker dp->d_type != DT_LNK)
11766e8db49aSJilles Tjoelker continue;
11777a2b9d4bSJilles Tjoelker if (enddir + namlen + 2 > expdir_end)
1178c8a3d81fSJilles Tjoelker continue;
11797a2b9d4bSJilles Tjoelker enddir[namlen] = '/';
11807a2b9d4bSJilles Tjoelker enddir[namlen + 1] = '\0';
11818ef0ae8aSJilles Tjoelker expmeta(enddir + namlen + 1, endname, arglist);
11824b88c807SRodney W. Grimes }
11834b88c807SRodney W. Grimes }
11844b88c807SRodney W. Grimes }
11854b88c807SRodney W. Grimes closedir(dirp);
11864b88c807SRodney W. Grimes if (! atend)
11874710b07eSJilles Tjoelker endname[-esc - 1] = esc ? CTLESC : '/';
11884b88c807SRodney W. Grimes }
11894b88c807SRodney W. Grimes
11904b88c807SRodney W. Grimes
11918ef0ae8aSJilles Tjoelker static int
expsortcmp(const void * p1,const void * p2)11928ef0ae8aSJilles Tjoelker expsortcmp(const void *p1, const void *p2)
11934b88c807SRodney W. Grimes {
11948ef0ae8aSJilles Tjoelker const char *s1 = *(const char * const *)p1;
11958ef0ae8aSJilles Tjoelker const char *s2 = *(const char * const *)p2;
11964b88c807SRodney W. Grimes
1197143d321aSAndrey A. Chernov return (strcoll(s1, s2));
11984b88c807SRodney W. Grimes }
11994b88c807SRodney W. Grimes
12004b88c807SRodney W. Grimes
12014b88c807SRodney W. Grimes
12027cc6b3dfSJilles Tjoelker static wchar_t
get_wc(const char ** p)12037cc6b3dfSJilles Tjoelker get_wc(const char **p)
12047cc6b3dfSJilles Tjoelker {
12057cc6b3dfSJilles Tjoelker wchar_t c;
12067cc6b3dfSJilles Tjoelker int chrlen;
12077cc6b3dfSJilles Tjoelker
12087cc6b3dfSJilles Tjoelker chrlen = mbtowc(&c, *p, 4);
12097cc6b3dfSJilles Tjoelker if (chrlen == 0)
12107cc6b3dfSJilles Tjoelker return 0;
12117cc6b3dfSJilles Tjoelker else if (chrlen == -1)
12127cc6b3dfSJilles Tjoelker c = 0;
12137cc6b3dfSJilles Tjoelker else
12147cc6b3dfSJilles Tjoelker *p += chrlen;
12157cc6b3dfSJilles Tjoelker return c;
12167cc6b3dfSJilles Tjoelker }
12177cc6b3dfSJilles Tjoelker
12187cc6b3dfSJilles Tjoelker
12194b88c807SRodney W. Grimes /*
1220ff4dc672SJilles Tjoelker * See if a character matches a character class, starting at the first colon
1221ff4dc672SJilles Tjoelker * of "[:class:]".
1222ff4dc672SJilles Tjoelker * If a valid character class is recognized, a pointer to the next character
1223ff4dc672SJilles Tjoelker * after the final closing bracket is stored into *end, otherwise a null
1224ff4dc672SJilles Tjoelker * pointer is stored into *end.
1225ff4dc672SJilles Tjoelker */
1226ff4dc672SJilles Tjoelker static int
match_charclass(const char * p,wchar_t chr,const char ** end)1227ff4dc672SJilles Tjoelker match_charclass(const char *p, wchar_t chr, const char **end)
1228ff4dc672SJilles Tjoelker {
1229ff4dc672SJilles Tjoelker char name[20];
1230ff4dc672SJilles Tjoelker const char *nameend;
1231ff4dc672SJilles Tjoelker wctype_t cclass;
1232ff4dc672SJilles Tjoelker
1233ff4dc672SJilles Tjoelker *end = NULL;
1234ff4dc672SJilles Tjoelker p++;
1235ff4dc672SJilles Tjoelker nameend = strstr(p, ":]");
123646c6b52dSJilles Tjoelker if (nameend == NULL || (size_t)(nameend - p) >= sizeof(name) ||
123746c6b52dSJilles Tjoelker nameend == p)
1238ff4dc672SJilles Tjoelker return 0;
1239ff4dc672SJilles Tjoelker memcpy(name, p, nameend - p);
1240ff4dc672SJilles Tjoelker name[nameend - p] = '\0';
1241ff4dc672SJilles Tjoelker *end = nameend + 2;
1242ff4dc672SJilles Tjoelker cclass = wctype(name);
1243ff4dc672SJilles Tjoelker /* An unknown class matches nothing but is valid nevertheless. */
1244ff4dc672SJilles Tjoelker if (cclass == 0)
1245ff4dc672SJilles Tjoelker return 0;
1246ff4dc672SJilles Tjoelker return iswctype(chr, cclass);
1247ff4dc672SJilles Tjoelker }
1248ff4dc672SJilles Tjoelker
1249ff4dc672SJilles Tjoelker
1250ff4dc672SJilles Tjoelker /*
12514b88c807SRodney W. Grimes * Returns true if the pattern matches the string.
12524b88c807SRodney W. Grimes */
12534b88c807SRodney W. Grimes
1254260fc3f4SJilles Tjoelker static int
patmatch(const char * pattern,const char * string)125547d8814cSJilles Tjoelker patmatch(const char *pattern, const char *string)
12564b88c807SRodney W. Grimes {
1257ff4dc672SJilles Tjoelker const char *p, *q, *end;
1258820491f8SJilles Tjoelker const char *bt_p, *bt_q;
125996522b88SSteve Price char c;
12607cc6b3dfSJilles Tjoelker wchar_t wc, wc2;
12614b88c807SRodney W. Grimes
12624b88c807SRodney W. Grimes p = pattern;
12634b88c807SRodney W. Grimes q = string;
1264820491f8SJilles Tjoelker bt_p = NULL;
1265820491f8SJilles Tjoelker bt_q = NULL;
12664b88c807SRodney W. Grimes for (;;) {
12674b88c807SRodney W. Grimes switch (c = *p++) {
12684b88c807SRodney W. Grimes case '\0':
1269820491f8SJilles Tjoelker if (*q != '\0')
1270820491f8SJilles Tjoelker goto backtrack;
1271820491f8SJilles Tjoelker return 1;
12724b88c807SRodney W. Grimes case CTLESC:
12734b88c807SRodney W. Grimes if (*q++ != *p++)
1274820491f8SJilles Tjoelker goto backtrack;
12754b88c807SRodney W. Grimes break;
12764b88c807SRodney W. Grimes case '?':
1277820491f8SJilles Tjoelker if (*q == '\0')
12784b88c807SRodney W. Grimes return 0;
1279820491f8SJilles Tjoelker if (localeisutf8) {
1280820491f8SJilles Tjoelker wc = get_wc(&q);
1281820491f8SJilles Tjoelker /*
1282820491f8SJilles Tjoelker * A '?' does not match invalid UTF-8 but a
1283820491f8SJilles Tjoelker * '*' does, so backtrack.
1284820491f8SJilles Tjoelker */
1285820491f8SJilles Tjoelker if (wc == 0)
1286820491f8SJilles Tjoelker goto backtrack;
1287820491f8SJilles Tjoelker } else
1288acb4eadaSJilles Tjoelker q++;
12894b88c807SRodney W. Grimes break;
12904b88c807SRodney W. Grimes case '*':
12914b88c807SRodney W. Grimes c = *p;
1292bc57b4d4SJilles Tjoelker while (c == '*')
12936f47734fSTor Egge c = *++p;
1294820491f8SJilles Tjoelker /*
1295820491f8SJilles Tjoelker * If the pattern ends here, we know the string
1296820491f8SJilles Tjoelker * matches without needing to look at the rest of it.
1297820491f8SJilles Tjoelker */
1298820491f8SJilles Tjoelker if (c == '\0')
12994b88c807SRodney W. Grimes return 1;
1300820491f8SJilles Tjoelker /*
1301820491f8SJilles Tjoelker * First try the shortest match for the '*' that
1302820491f8SJilles Tjoelker * could work. We can forget any earlier '*' since
1303820491f8SJilles Tjoelker * there is no way having it match more characters
1304820491f8SJilles Tjoelker * can help us, given that we are already here.
1305820491f8SJilles Tjoelker */
1306820491f8SJilles Tjoelker bt_p = p;
1307820491f8SJilles Tjoelker bt_q = q;
1308820491f8SJilles Tjoelker break;
13094b88c807SRodney W. Grimes case '[': {
13104a4867d6SJilles Tjoelker const char *savep, *saveq;
13114b88c807SRodney W. Grimes int invert, found;
13127cc6b3dfSJilles Tjoelker wchar_t chr;
13134b88c807SRodney W. Grimes
13144a4867d6SJilles Tjoelker savep = p, saveq = q;
13154b88c807SRodney W. Grimes invert = 0;
1316ea1376dfSAndrey A. Chernov if (*p == '!' || *p == '^') {
13174b88c807SRodney W. Grimes invert++;
13184b88c807SRodney W. Grimes p++;
13194b88c807SRodney W. Grimes }
13204b88c807SRodney W. Grimes found = 0;
1321820491f8SJilles Tjoelker if (*q == '\0')
1322aa9caaf6SPeter Wemm return 0;
1323820491f8SJilles Tjoelker if (localeisutf8) {
1324820491f8SJilles Tjoelker chr = get_wc(&q);
1325820491f8SJilles Tjoelker if (chr == 0)
1326820491f8SJilles Tjoelker goto backtrack;
1327820491f8SJilles Tjoelker } else
1328820491f8SJilles Tjoelker chr = (unsigned char)*q++;
13294b88c807SRodney W. Grimes c = *p++;
13304b88c807SRodney W. Grimes do {
13314a4867d6SJilles Tjoelker if (c == '\0') {
13324a4867d6SJilles Tjoelker p = savep, q = saveq;
13334a4867d6SJilles Tjoelker c = '[';
13344a4867d6SJilles Tjoelker goto dft;
13354a4867d6SJilles Tjoelker }
1336ff4dc672SJilles Tjoelker if (c == '[' && *p == ':') {
1337ff4dc672SJilles Tjoelker found |= match_charclass(p, chr, &end);
13384d7f36eeSJilles Tjoelker if (end != NULL) {
1339ff4dc672SJilles Tjoelker p = end;
13404d7f36eeSJilles Tjoelker continue;
13414d7f36eeSJilles Tjoelker }
1342ff4dc672SJilles Tjoelker }
13434b88c807SRodney W. Grimes if (c == CTLESC)
13444b88c807SRodney W. Grimes c = *p++;
13457cc6b3dfSJilles Tjoelker if (localeisutf8 && c & 0x80) {
13467cc6b3dfSJilles Tjoelker p--;
13477cc6b3dfSJilles Tjoelker wc = get_wc(&p);
13487cc6b3dfSJilles Tjoelker if (wc == 0) /* bad utf-8 */
13497cc6b3dfSJilles Tjoelker return 0;
13507cc6b3dfSJilles Tjoelker } else
1351f5ac5937SJilles Tjoelker wc = (unsigned char)c;
13524b88c807SRodney W. Grimes if (*p == '-' && p[1] != ']') {
13534b88c807SRodney W. Grimes p++;
13544b88c807SRodney W. Grimes if (*p == CTLESC)
13554b88c807SRodney W. Grimes p++;
13567cc6b3dfSJilles Tjoelker if (localeisutf8) {
13577cc6b3dfSJilles Tjoelker wc2 = get_wc(&p);
13587cc6b3dfSJilles Tjoelker if (wc2 == 0) /* bad utf-8 */
13597cc6b3dfSJilles Tjoelker return 0;
13607cc6b3dfSJilles Tjoelker } else
1361f5ac5937SJilles Tjoelker wc2 = (unsigned char)*p++;
1362fa93fc65SAndrey A. Chernov if ( collate_range_cmp(chr, wc) >= 0
1363fa93fc65SAndrey A. Chernov && collate_range_cmp(chr, wc2) <= 0
1364fa93fc65SAndrey A. Chernov )
13654b88c807SRodney W. Grimes found = 1;
13664b88c807SRodney W. Grimes } else {
13677cc6b3dfSJilles Tjoelker if (chr == wc)
13684b88c807SRodney W. Grimes found = 1;
13694b88c807SRodney W. Grimes }
13704b88c807SRodney W. Grimes } while ((c = *p++) != ']');
13714b88c807SRodney W. Grimes if (found == invert)
1372820491f8SJilles Tjoelker goto backtrack;
13734b88c807SRodney W. Grimes break;
13744b88c807SRodney W. Grimes }
13754b88c807SRodney W. Grimes dft: default:
1376820491f8SJilles Tjoelker if (*q == '\0')
13774b88c807SRodney W. Grimes return 0;
1378820491f8SJilles Tjoelker if (*q++ == c)
1379820491f8SJilles Tjoelker break;
1380820491f8SJilles Tjoelker backtrack:
1381820491f8SJilles Tjoelker /*
1382820491f8SJilles Tjoelker * If we have a mismatch (other than hitting the end
1383820491f8SJilles Tjoelker * of the string), go back to the last '*' seen and
1384820491f8SJilles Tjoelker * have it match one additional character.
1385820491f8SJilles Tjoelker */
1386820491f8SJilles Tjoelker if (bt_p == NULL)
1387820491f8SJilles Tjoelker return 0;
1388820491f8SJilles Tjoelker if (*bt_q == '\0')
1389820491f8SJilles Tjoelker return 0;
1390820491f8SJilles Tjoelker bt_q++;
1391820491f8SJilles Tjoelker p = bt_p;
1392820491f8SJilles Tjoelker q = bt_q;
13934b88c807SRodney W. Grimes break;
13944b88c807SRodney W. Grimes }
13954b88c807SRodney W. Grimes }
13964b88c807SRodney W. Grimes }
13974b88c807SRodney W. Grimes
13984b88c807SRodney W. Grimes
13994b88c807SRodney W. Grimes
14004b88c807SRodney W. Grimes /*
14012ca3d70fSJilles Tjoelker * Remove any CTLESC and CTLQUOTEMARK characters from a string.
14024b88c807SRodney W. Grimes */
14034b88c807SRodney W. Grimes
14044b88c807SRodney W. Grimes void
rmescapes(char * str)14055134c3f7SWarner Losh rmescapes(char *str)
14064b88c807SRodney W. Grimes {
140796522b88SSteve Price char *p, *q;
14084b88c807SRodney W. Grimes
14094b88c807SRodney W. Grimes p = str;
1410048f2667SJilles Tjoelker while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) {
14114b88c807SRodney W. Grimes if (*p++ == '\0')
14124b88c807SRodney W. Grimes return;
14134b88c807SRodney W. Grimes }
14144b88c807SRodney W. Grimes q = p;
14154b88c807SRodney W. Grimes while (*p) {
1416048f2667SJilles Tjoelker if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
14176f47734fSTor Egge p++;
14186f47734fSTor Egge continue;
14196f47734fSTor Egge }
14204b88c807SRodney W. Grimes if (*p == CTLESC)
14214b88c807SRodney W. Grimes p++;
14224b88c807SRodney W. Grimes *q++ = *p++;
14234b88c807SRodney W. Grimes }
14244b88c807SRodney W. Grimes *q = '\0';
14254b88c807SRodney W. Grimes }
14264b88c807SRodney W. Grimes
14274b88c807SRodney W. Grimes
14284b88c807SRodney W. Grimes
14294b88c807SRodney W. Grimes /*
14304b88c807SRodney W. Grimes * See if a pattern matches in a case statement.
14314b88c807SRodney W. Grimes */
14324b88c807SRodney W. Grimes
14334b88c807SRodney W. Grimes int
casematch(union node * pattern,const char * val)14342cac6e36SJilles Tjoelker casematch(union node *pattern, const char *val)
14354b88c807SRodney W. Grimes {
14364b88c807SRodney W. Grimes struct stackmark smark;
1437439948cdSJilles Tjoelker struct nodelist *argbackq;
14384b88c807SRodney W. Grimes int result;
14394b88c807SRodney W. Grimes char *p;
14404b88c807SRodney W. Grimes
14414b88c807SRodney W. Grimes setstackmark(&smark);
14424b88c807SRodney W. Grimes argbackq = pattern->narg.backquote;
14434b88c807SRodney W. Grimes STARTSTACKSTR(expdest);
1444439948cdSJilles Tjoelker argstr(pattern->narg.text, &argbackq, EXP_TILDE | EXP_CASE, NULL);
14454b88c807SRodney W. Grimes STPUTC('\0', expdest);
14464b88c807SRodney W. Grimes p = grabstackstr(expdest);
144747d8814cSJilles Tjoelker result = patmatch(p, val);
14484b88c807SRodney W. Grimes popstackmark(&smark);
14494b88c807SRodney W. Grimes return result;
14504b88c807SRodney W. Grimes }
1451aa9caaf6SPeter Wemm
1452aa9caaf6SPeter Wemm /*
1453aa9caaf6SPeter Wemm * Our own itoa().
1454aa9caaf6SPeter Wemm */
1455aa9caaf6SPeter Wemm
145647d8814cSJilles Tjoelker static void
cvtnum(int num,char * buf)14575134c3f7SWarner Losh cvtnum(int num, char *buf)
1458aa9caaf6SPeter Wemm {
1459aa9caaf6SPeter Wemm char temp[32];
1460aa9caaf6SPeter Wemm int neg = num < 0;
1461aa9caaf6SPeter Wemm char *p = temp + 31;
1462aa9caaf6SPeter Wemm
1463aa9caaf6SPeter Wemm temp[31] = '\0';
1464aa9caaf6SPeter Wemm
1465aa9caaf6SPeter Wemm do {
1466aa9caaf6SPeter Wemm *--p = num % 10 + '0';
1467aa9caaf6SPeter Wemm } while ((num /= 10) != 0);
1468aa9caaf6SPeter Wemm
1469aa9caaf6SPeter Wemm if (neg)
1470aa9caaf6SPeter Wemm *--p = '-';
1471aa9caaf6SPeter Wemm
147247d8814cSJilles Tjoelker memcpy(buf, p, temp + 32 - p);
1473aa9caaf6SPeter Wemm }
14742c25061fSTim J. Robbins
14752c25061fSTim J. Robbins /*
14762c25061fSTim J. Robbins * Do most of the work for wordexp(3).
14772c25061fSTim J. Robbins */
14782c25061fSTim J. Robbins
14792c25061fSTim J. Robbins int
wordexpcmd(int argc,char ** argv)14802c25061fSTim J. Robbins wordexpcmd(int argc, char **argv)
14812c25061fSTim J. Robbins {
14822c25061fSTim J. Robbins size_t len;
14832c25061fSTim J. Robbins int i;
14842c25061fSTim J. Robbins
14852c25061fSTim J. Robbins out1fmt("%08x", argc - 1);
14862c25061fSTim J. Robbins for (i = 1, len = 0; i < argc; i++)
14872c25061fSTim J. Robbins len += strlen(argv[i]);
14882c25061fSTim J. Robbins out1fmt("%08x", (int)len);
1489aeb5d065SJilles Tjoelker for (i = 1; i < argc; i++)
1490aeb5d065SJilles Tjoelker outbin(argv[i], strlen(argv[i]) + 1, out1);
14912c25061fSTim J. Robbins return (0);
14922c25061fSTim J. Robbins }
1493d358fa78SJilles Tjoelker
1494d358fa78SJilles Tjoelker /*
1495d358fa78SJilles Tjoelker * Do most of the work for wordexp(3), new version.
1496d358fa78SJilles Tjoelker */
1497d358fa78SJilles Tjoelker
1498d358fa78SJilles Tjoelker int
freebsd_wordexpcmd(int argc __unused,char ** argv __unused)1499d358fa78SJilles Tjoelker freebsd_wordexpcmd(int argc __unused, char **argv __unused)
1500d358fa78SJilles Tjoelker {
1501d358fa78SJilles Tjoelker struct arglist arglist;
1502d358fa78SJilles Tjoelker union node *args, *n;
15038ef0ae8aSJilles Tjoelker size_t len;
1504d358fa78SJilles Tjoelker int ch;
1505d358fa78SJilles Tjoelker int protected = 0;
1506d358fa78SJilles Tjoelker int fd = -1;
15078ef0ae8aSJilles Tjoelker int i;
1508d358fa78SJilles Tjoelker
1509d358fa78SJilles Tjoelker while ((ch = nextopt("f:p")) != '\0') {
1510d358fa78SJilles Tjoelker switch (ch) {
1511d358fa78SJilles Tjoelker case 'f':
1512d358fa78SJilles Tjoelker fd = number(shoptarg);
1513d358fa78SJilles Tjoelker break;
1514d358fa78SJilles Tjoelker case 'p':
1515d358fa78SJilles Tjoelker protected = 1;
1516d358fa78SJilles Tjoelker break;
1517d358fa78SJilles Tjoelker }
1518d358fa78SJilles Tjoelker }
1519d358fa78SJilles Tjoelker if (*argptr != NULL)
1520d358fa78SJilles Tjoelker error("wrong number of arguments");
1521d358fa78SJilles Tjoelker if (fd < 0)
1522d358fa78SJilles Tjoelker error("missing fd");
1523d358fa78SJilles Tjoelker INTOFF;
1524d358fa78SJilles Tjoelker setinputfd(fd, 1);
1525d358fa78SJilles Tjoelker INTON;
1526d358fa78SJilles Tjoelker args = parsewordexp();
1527d358fa78SJilles Tjoelker popfile(); /* will also close fd */
1528d358fa78SJilles Tjoelker if (protected)
1529d358fa78SJilles Tjoelker for (n = args; n != NULL; n = n->narg.next) {
1530d358fa78SJilles Tjoelker if (n->narg.backquote != NULL) {
1531d358fa78SJilles Tjoelker outcslow('C', out1);
1532d358fa78SJilles Tjoelker error("command substitution disabled");
1533d358fa78SJilles Tjoelker }
1534d358fa78SJilles Tjoelker }
1535d358fa78SJilles Tjoelker outcslow(' ', out1);
15368ef0ae8aSJilles Tjoelker emptyarglist(&arglist);
1537d358fa78SJilles Tjoelker for (n = args; n != NULL; n = n->narg.next)
1538d358fa78SJilles Tjoelker expandarg(n, &arglist, EXP_FULL | EXP_TILDE);
15398ef0ae8aSJilles Tjoelker for (i = 0, len = 0; i < arglist.count; i++)
15408ef0ae8aSJilles Tjoelker len += strlen(arglist.args[i]);
15418ef0ae8aSJilles Tjoelker out1fmt("%016x %016zx", arglist.count, len);
15428ef0ae8aSJilles Tjoelker for (i = 0; i < arglist.count; i++)
15438ef0ae8aSJilles Tjoelker outbin(arglist.args[i], strlen(arglist.args[i]) + 1, out1);
1544d358fa78SJilles Tjoelker return (0);
1545d358fa78SJilles Tjoelker }
1546