1*0a6a1f1dSLionel Sambuc /* $NetBSD: expand.c,v 1.93 2015/08/27 07:46:47 christos Exp $ */
2d90bee97SLionel Sambuc
3d90bee97SLionel Sambuc /*-
4d90bee97SLionel Sambuc * Copyright (c) 1991, 1993
5d90bee97SLionel Sambuc * The Regents of the University of California. All rights reserved.
6d90bee97SLionel Sambuc *
7d90bee97SLionel Sambuc * This code is derived from software contributed to Berkeley by
8d90bee97SLionel Sambuc * Kenneth Almquist.
9d90bee97SLionel Sambuc *
10d90bee97SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11d90bee97SLionel Sambuc * modification, are permitted provided that the following conditions
12d90bee97SLionel Sambuc * are met:
13d90bee97SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14d90bee97SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15d90bee97SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16d90bee97SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17d90bee97SLionel Sambuc * documentation and/or other materials provided with the distribution.
18d90bee97SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
19d90bee97SLionel Sambuc * may be used to endorse or promote products derived from this software
20d90bee97SLionel Sambuc * without specific prior written permission.
21d90bee97SLionel Sambuc *
22d90bee97SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23d90bee97SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24d90bee97SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25d90bee97SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26d90bee97SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27d90bee97SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28d90bee97SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29d90bee97SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30d90bee97SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31d90bee97SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32d90bee97SLionel Sambuc * SUCH DAMAGE.
33d90bee97SLionel Sambuc */
34d90bee97SLionel Sambuc
35d90bee97SLionel Sambuc #include <sys/cdefs.h>
36d90bee97SLionel Sambuc #ifndef lint
37d90bee97SLionel Sambuc #if 0
38d90bee97SLionel Sambuc static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95";
39d90bee97SLionel Sambuc #else
40*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: expand.c,v 1.93 2015/08/27 07:46:47 christos Exp $");
41d90bee97SLionel Sambuc #endif
42d90bee97SLionel Sambuc #endif /* not lint */
43d90bee97SLionel Sambuc
44d90bee97SLionel Sambuc #include <sys/types.h>
45d90bee97SLionel Sambuc #include <sys/time.h>
46d90bee97SLionel Sambuc #include <sys/stat.h>
47d90bee97SLionel Sambuc #include <errno.h>
48d90bee97SLionel Sambuc #include <dirent.h>
49d90bee97SLionel Sambuc #include <unistd.h>
50d90bee97SLionel Sambuc #include <pwd.h>
51d90bee97SLionel Sambuc #include <limits.h>
52d90bee97SLionel Sambuc #include <stdlib.h>
53d90bee97SLionel Sambuc #include <stdio.h>
54*0a6a1f1dSLionel Sambuc #include <wctype.h>
55d90bee97SLionel Sambuc
56d90bee97SLionel Sambuc /*
57d90bee97SLionel Sambuc * Routines to expand arguments to commands. We have to deal with
58d90bee97SLionel Sambuc * backquotes, shell variables, and file metacharacters.
59d90bee97SLionel Sambuc */
60d90bee97SLionel Sambuc
61d90bee97SLionel Sambuc #include "shell.h"
62d90bee97SLionel Sambuc #include "main.h"
63d90bee97SLionel Sambuc #include "nodes.h"
64d90bee97SLionel Sambuc #include "eval.h"
65d90bee97SLionel Sambuc #include "expand.h"
66d90bee97SLionel Sambuc #include "syntax.h"
67d90bee97SLionel Sambuc #include "parser.h"
68d90bee97SLionel Sambuc #include "jobs.h"
69d90bee97SLionel Sambuc #include "options.h"
70d90bee97SLionel Sambuc #include "builtins.h"
71d90bee97SLionel Sambuc #include "var.h"
72d90bee97SLionel Sambuc #include "input.h"
73d90bee97SLionel Sambuc #include "output.h"
74d90bee97SLionel Sambuc #include "memalloc.h"
75d90bee97SLionel Sambuc #include "error.h"
76d90bee97SLionel Sambuc #include "mystring.h"
77d90bee97SLionel Sambuc #include "show.h"
78d90bee97SLionel Sambuc
79d90bee97SLionel Sambuc /*
80d90bee97SLionel Sambuc * Structure specifying which parts of the string should be searched
81d90bee97SLionel Sambuc * for IFS characters.
82d90bee97SLionel Sambuc */
83d90bee97SLionel Sambuc
84d90bee97SLionel Sambuc struct ifsregion {
85d90bee97SLionel Sambuc struct ifsregion *next; /* next region in list */
86d90bee97SLionel Sambuc int begoff; /* offset of start of region */
87d90bee97SLionel Sambuc int endoff; /* offset of end of region */
88d90bee97SLionel Sambuc int inquotes; /* search for nul bytes only */
89d90bee97SLionel Sambuc };
90d90bee97SLionel Sambuc
91d90bee97SLionel Sambuc
92d90bee97SLionel Sambuc char *expdest; /* output of current string */
93d90bee97SLionel Sambuc struct nodelist *argbackq; /* list of back quote expressions */
94d90bee97SLionel Sambuc struct ifsregion ifsfirst; /* first struct in list of ifs regions */
95d90bee97SLionel Sambuc struct ifsregion *ifslastp; /* last struct in list */
96d90bee97SLionel Sambuc struct arglist exparg; /* holds expanded arg list */
97d90bee97SLionel Sambuc
98d90bee97SLionel Sambuc STATIC void argstr(char *, int);
99d90bee97SLionel Sambuc STATIC char *exptilde(char *, int);
100d90bee97SLionel Sambuc STATIC void expbackq(union node *, int, int);
101*0a6a1f1dSLionel Sambuc STATIC int subevalvar(char *, char *, int, int, int, int, int);
102d90bee97SLionel Sambuc STATIC char *evalvar(char *, int);
103d90bee97SLionel Sambuc STATIC int varisset(char *, int);
104d90bee97SLionel Sambuc STATIC void varvalue(char *, int, int, int);
105d90bee97SLionel Sambuc STATIC void recordregion(int, int, int);
106d90bee97SLionel Sambuc STATIC void removerecordregions(int);
107d90bee97SLionel Sambuc STATIC void ifsbreakup(char *, struct arglist *);
108d90bee97SLionel Sambuc STATIC void ifsfree(void);
109d90bee97SLionel Sambuc STATIC void expandmeta(struct strlist *, int);
110d90bee97SLionel Sambuc STATIC void expmeta(char *, char *);
111d90bee97SLionel Sambuc STATIC void addfname(char *);
112d90bee97SLionel Sambuc STATIC struct strlist *expsort(struct strlist *);
113d90bee97SLionel Sambuc STATIC struct strlist *msort(struct strlist *, int);
114d90bee97SLionel Sambuc STATIC int pmatch(char *, char *, int);
115d90bee97SLionel Sambuc STATIC char *cvtnum(int, char *);
116d90bee97SLionel Sambuc
117d90bee97SLionel Sambuc /*
118d90bee97SLionel Sambuc * Expand shell variables and backquotes inside a here document.
119d90bee97SLionel Sambuc */
120d90bee97SLionel Sambuc
121d90bee97SLionel Sambuc void
expandhere(union node * arg,int fd)122d90bee97SLionel Sambuc expandhere(union node *arg, int fd)
123d90bee97SLionel Sambuc {
124d90bee97SLionel Sambuc herefd = fd;
125d90bee97SLionel Sambuc expandarg(arg, NULL, 0);
126d90bee97SLionel Sambuc xwrite(fd, stackblock(), expdest - stackblock());
127d90bee97SLionel Sambuc }
128d90bee97SLionel Sambuc
129d90bee97SLionel Sambuc
130d90bee97SLionel Sambuc /*
131d90bee97SLionel Sambuc * Perform variable substitution and command substitution on an argument,
132d90bee97SLionel Sambuc * placing the resulting list of arguments in arglist. If EXP_FULL is true,
133d90bee97SLionel Sambuc * perform splitting and file name expansion. When arglist is NULL, perform
134d90bee97SLionel Sambuc * here document expansion.
135d90bee97SLionel Sambuc */
136d90bee97SLionel Sambuc
137d90bee97SLionel Sambuc void
expandarg(union node * arg,struct arglist * arglist,int flag)138d90bee97SLionel Sambuc expandarg(union node *arg, struct arglist *arglist, int flag)
139d90bee97SLionel Sambuc {
140d90bee97SLionel Sambuc struct strlist *sp;
141d90bee97SLionel Sambuc char *p;
142d90bee97SLionel Sambuc
143d90bee97SLionel Sambuc argbackq = arg->narg.backquote;
144d90bee97SLionel Sambuc STARTSTACKSTR(expdest);
145d90bee97SLionel Sambuc ifsfirst.next = NULL;
146d90bee97SLionel Sambuc ifslastp = NULL;
147d90bee97SLionel Sambuc argstr(arg->narg.text, flag);
148d90bee97SLionel Sambuc if (arglist == NULL) {
149d90bee97SLionel Sambuc return; /* here document expanded */
150d90bee97SLionel Sambuc }
151d90bee97SLionel Sambuc STPUTC('\0', expdest);
152d90bee97SLionel Sambuc p = grabstackstr(expdest);
153d90bee97SLionel Sambuc exparg.lastp = &exparg.list;
154d90bee97SLionel Sambuc /*
155d90bee97SLionel Sambuc * TODO - EXP_REDIR
156d90bee97SLionel Sambuc */
157d90bee97SLionel Sambuc if (flag & EXP_FULL) {
158d90bee97SLionel Sambuc ifsbreakup(p, &exparg);
159d90bee97SLionel Sambuc *exparg.lastp = NULL;
160d90bee97SLionel Sambuc exparg.lastp = &exparg.list;
161d90bee97SLionel Sambuc expandmeta(exparg.list, flag);
162d90bee97SLionel Sambuc } else {
163d90bee97SLionel Sambuc if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
164d90bee97SLionel Sambuc rmescapes(p);
165d90bee97SLionel Sambuc sp = (struct strlist *)stalloc(sizeof (struct strlist));
166d90bee97SLionel Sambuc sp->text = p;
167d90bee97SLionel Sambuc *exparg.lastp = sp;
168d90bee97SLionel Sambuc exparg.lastp = &sp->next;
169d90bee97SLionel Sambuc }
170d90bee97SLionel Sambuc ifsfree();
171d90bee97SLionel Sambuc *exparg.lastp = NULL;
172d90bee97SLionel Sambuc if (exparg.list) {
173d90bee97SLionel Sambuc *arglist->lastp = exparg.list;
174d90bee97SLionel Sambuc arglist->lastp = exparg.lastp;
175d90bee97SLionel Sambuc }
176d90bee97SLionel Sambuc }
177d90bee97SLionel Sambuc
178d90bee97SLionel Sambuc
179d90bee97SLionel Sambuc
180d90bee97SLionel Sambuc /*
181d90bee97SLionel Sambuc * Perform variable and command substitution.
182d90bee97SLionel Sambuc * If EXP_FULL is set, output CTLESC characters to allow for further processing.
183d90bee97SLionel Sambuc * Otherwise treat $@ like $* since no splitting will be performed.
184d90bee97SLionel Sambuc */
185d90bee97SLionel Sambuc
186d90bee97SLionel Sambuc STATIC void
argstr(char * p,int flag)187d90bee97SLionel Sambuc argstr(char *p, int flag)
188d90bee97SLionel Sambuc {
189d90bee97SLionel Sambuc char c;
190d90bee97SLionel Sambuc int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */
191d90bee97SLionel Sambuc int firsteq = 1;
192d90bee97SLionel Sambuc const char *ifs = NULL;
193d90bee97SLionel Sambuc int ifs_split = EXP_IFS_SPLIT;
194d90bee97SLionel Sambuc
195d90bee97SLionel Sambuc if (flag & EXP_IFS_SPLIT)
196d90bee97SLionel Sambuc ifs = ifsset() ? ifsval() : " \t\n";
197d90bee97SLionel Sambuc
198d90bee97SLionel Sambuc if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
199d90bee97SLionel Sambuc p = exptilde(p, flag);
200d90bee97SLionel Sambuc for (;;) {
201d90bee97SLionel Sambuc switch (c = *p++) {
202d90bee97SLionel Sambuc case '\0':
203d90bee97SLionel Sambuc case CTLENDVAR: /* end of expanding yyy in ${xxx-yyy} */
204d90bee97SLionel Sambuc return;
205d90bee97SLionel Sambuc case CTLQUOTEMARK:
206d90bee97SLionel Sambuc /* "$@" syntax adherence hack */
207d90bee97SLionel Sambuc if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
208d90bee97SLionel Sambuc break;
209d90bee97SLionel Sambuc if ((flag & EXP_FULL) != 0)
210d90bee97SLionel Sambuc STPUTC(c, expdest);
211d90bee97SLionel Sambuc ifs_split = 0;
212d90bee97SLionel Sambuc break;
213d90bee97SLionel Sambuc case CTLQUOTEEND:
214d90bee97SLionel Sambuc ifs_split = EXP_IFS_SPLIT;
215d90bee97SLionel Sambuc break;
216d90bee97SLionel Sambuc case CTLESC:
217d90bee97SLionel Sambuc if (quotes)
218d90bee97SLionel Sambuc STPUTC(c, expdest);
219d90bee97SLionel Sambuc c = *p++;
220d90bee97SLionel Sambuc STPUTC(c, expdest);
221d90bee97SLionel Sambuc break;
222d90bee97SLionel Sambuc case CTLVAR:
223d90bee97SLionel Sambuc p = evalvar(p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split));
224d90bee97SLionel Sambuc break;
225d90bee97SLionel Sambuc case CTLBACKQ:
226d90bee97SLionel Sambuc case CTLBACKQ|CTLQUOTE:
227d90bee97SLionel Sambuc expbackq(argbackq->n, c & CTLQUOTE, flag);
228d90bee97SLionel Sambuc argbackq = argbackq->next;
229d90bee97SLionel Sambuc break;
230d90bee97SLionel Sambuc case CTLENDARI:
231d90bee97SLionel Sambuc expari(flag);
232d90bee97SLionel Sambuc break;
233d90bee97SLionel Sambuc case ':':
234d90bee97SLionel Sambuc case '=':
235d90bee97SLionel Sambuc /*
236d90bee97SLionel Sambuc * sort of a hack - expand tildes in variable
237d90bee97SLionel Sambuc * assignments (after the first '=' and after ':'s).
238d90bee97SLionel Sambuc */
239d90bee97SLionel Sambuc STPUTC(c, expdest);
240d90bee97SLionel Sambuc if (flag & EXP_VARTILDE && *p == '~') {
241d90bee97SLionel Sambuc if (c == '=') {
242d90bee97SLionel Sambuc if (firsteq)
243d90bee97SLionel Sambuc firsteq = 0;
244d90bee97SLionel Sambuc else
245d90bee97SLionel Sambuc break;
246d90bee97SLionel Sambuc }
247d90bee97SLionel Sambuc p = exptilde(p, flag);
248d90bee97SLionel Sambuc }
249d90bee97SLionel Sambuc break;
250d90bee97SLionel Sambuc default:
251d90bee97SLionel Sambuc STPUTC(c, expdest);
252d90bee97SLionel Sambuc if (flag & ifs_split && strchr(ifs, c) != NULL) {
253d90bee97SLionel Sambuc /* We need to get the output split here... */
254d90bee97SLionel Sambuc recordregion(expdest - stackblock() - 1,
255d90bee97SLionel Sambuc expdest - stackblock(), 0);
256d90bee97SLionel Sambuc }
257d90bee97SLionel Sambuc break;
258d90bee97SLionel Sambuc }
259d90bee97SLionel Sambuc }
260d90bee97SLionel Sambuc }
261d90bee97SLionel Sambuc
262d90bee97SLionel Sambuc STATIC char *
exptilde(char * p,int flag)263d90bee97SLionel Sambuc exptilde(char *p, int flag)
264d90bee97SLionel Sambuc {
265d90bee97SLionel Sambuc char c, *startp = p;
266d90bee97SLionel Sambuc struct passwd *pw;
267d90bee97SLionel Sambuc const char *home;
268d90bee97SLionel Sambuc int quotes = flag & (EXP_FULL | EXP_CASE);
269d90bee97SLionel Sambuc
270d90bee97SLionel Sambuc while ((c = *p) != '\0') {
271d90bee97SLionel Sambuc switch(c) {
272d90bee97SLionel Sambuc case CTLESC:
273d90bee97SLionel Sambuc return (startp);
274d90bee97SLionel Sambuc case CTLQUOTEMARK:
275d90bee97SLionel Sambuc return (startp);
276d90bee97SLionel Sambuc case ':':
277d90bee97SLionel Sambuc if (flag & EXP_VARTILDE)
278d90bee97SLionel Sambuc goto done;
279d90bee97SLionel Sambuc break;
280d90bee97SLionel Sambuc case '/':
281d90bee97SLionel Sambuc goto done;
282d90bee97SLionel Sambuc }
283d90bee97SLionel Sambuc p++;
284d90bee97SLionel Sambuc }
285d90bee97SLionel Sambuc done:
286d90bee97SLionel Sambuc *p = '\0';
287d90bee97SLionel Sambuc if (*(startp+1) == '\0') {
288d90bee97SLionel Sambuc if ((home = lookupvar("HOME")) == NULL)
289d90bee97SLionel Sambuc goto lose;
290d90bee97SLionel Sambuc } else {
291d90bee97SLionel Sambuc if ((pw = getpwnam(startp+1)) == NULL)
292d90bee97SLionel Sambuc goto lose;
293d90bee97SLionel Sambuc home = pw->pw_dir;
294d90bee97SLionel Sambuc }
295d90bee97SLionel Sambuc if (*home == '\0')
296d90bee97SLionel Sambuc goto lose;
297d90bee97SLionel Sambuc *p = c;
298d90bee97SLionel Sambuc while ((c = *home++) != '\0') {
299d90bee97SLionel Sambuc if (quotes && SQSYNTAX[(int)c] == CCTL)
300d90bee97SLionel Sambuc STPUTC(CTLESC, expdest);
301d90bee97SLionel Sambuc STPUTC(c, expdest);
302d90bee97SLionel Sambuc }
303d90bee97SLionel Sambuc return (p);
304d90bee97SLionel Sambuc lose:
305d90bee97SLionel Sambuc *p = c;
306d90bee97SLionel Sambuc return (startp);
307d90bee97SLionel Sambuc }
308d90bee97SLionel Sambuc
309d90bee97SLionel Sambuc
310d90bee97SLionel Sambuc STATIC void
removerecordregions(int endoff)311d90bee97SLionel Sambuc removerecordregions(int endoff)
312d90bee97SLionel Sambuc {
313d90bee97SLionel Sambuc if (ifslastp == NULL)
314d90bee97SLionel Sambuc return;
315d90bee97SLionel Sambuc
316d90bee97SLionel Sambuc if (ifsfirst.endoff > endoff) {
317d90bee97SLionel Sambuc while (ifsfirst.next != NULL) {
318d90bee97SLionel Sambuc struct ifsregion *ifsp;
319d90bee97SLionel Sambuc INTOFF;
320d90bee97SLionel Sambuc ifsp = ifsfirst.next->next;
321d90bee97SLionel Sambuc ckfree(ifsfirst.next);
322d90bee97SLionel Sambuc ifsfirst.next = ifsp;
323d90bee97SLionel Sambuc INTON;
324d90bee97SLionel Sambuc }
325d90bee97SLionel Sambuc if (ifsfirst.begoff > endoff)
326d90bee97SLionel Sambuc ifslastp = NULL;
327d90bee97SLionel Sambuc else {
328d90bee97SLionel Sambuc ifslastp = &ifsfirst;
329d90bee97SLionel Sambuc ifsfirst.endoff = endoff;
330d90bee97SLionel Sambuc }
331d90bee97SLionel Sambuc return;
332d90bee97SLionel Sambuc }
333d90bee97SLionel Sambuc
334d90bee97SLionel Sambuc ifslastp = &ifsfirst;
335d90bee97SLionel Sambuc while (ifslastp->next && ifslastp->next->begoff < endoff)
336d90bee97SLionel Sambuc ifslastp=ifslastp->next;
337d90bee97SLionel Sambuc while (ifslastp->next != NULL) {
338d90bee97SLionel Sambuc struct ifsregion *ifsp;
339d90bee97SLionel Sambuc INTOFF;
340d90bee97SLionel Sambuc ifsp = ifslastp->next->next;
341d90bee97SLionel Sambuc ckfree(ifslastp->next);
342d90bee97SLionel Sambuc ifslastp->next = ifsp;
343d90bee97SLionel Sambuc INTON;
344d90bee97SLionel Sambuc }
345d90bee97SLionel Sambuc if (ifslastp->endoff > endoff)
346d90bee97SLionel Sambuc ifslastp->endoff = endoff;
347d90bee97SLionel Sambuc }
348d90bee97SLionel Sambuc
349d90bee97SLionel Sambuc
350d90bee97SLionel Sambuc /*
351d90bee97SLionel Sambuc * Expand arithmetic expression. Backup to start of expression,
352d90bee97SLionel Sambuc * evaluate, place result in (backed up) result, adjust string position.
353d90bee97SLionel Sambuc */
354d90bee97SLionel Sambuc void
expari(int flag)355d90bee97SLionel Sambuc expari(int flag)
356d90bee97SLionel Sambuc {
357d90bee97SLionel Sambuc char *p, *start;
358d90bee97SLionel Sambuc intmax_t result;
359d90bee97SLionel Sambuc int adjustment;
360d90bee97SLionel Sambuc int begoff;
361d90bee97SLionel Sambuc int quotes = flag & (EXP_FULL | EXP_CASE);
362d90bee97SLionel Sambuc int quoted;
363d90bee97SLionel Sambuc
364d90bee97SLionel Sambuc /* ifsfree(); */
365d90bee97SLionel Sambuc
366d90bee97SLionel Sambuc /*
367d90bee97SLionel Sambuc * This routine is slightly over-complicated for
368d90bee97SLionel Sambuc * efficiency. First we make sure there is
369d90bee97SLionel Sambuc * enough space for the result, which may be bigger
370d90bee97SLionel Sambuc * than the expression if we add exponentation. Next we
371d90bee97SLionel Sambuc * scan backwards looking for the start of arithmetic. If the
372d90bee97SLionel Sambuc * next previous character is a CTLESC character, then we
373d90bee97SLionel Sambuc * have to rescan starting from the beginning since CTLESC
374d90bee97SLionel Sambuc * characters have to be processed left to right.
375d90bee97SLionel Sambuc */
376d90bee97SLionel Sambuc /* SPACE_NEEDED is enough for all digits, plus possible "-", plus 2 (why?) */
377d90bee97SLionel Sambuc #define SPACE_NEEDED ((sizeof(intmax_t) * CHAR_BIT + 2) / 3 + 1 + 2)
378d90bee97SLionel Sambuc CHECKSTRSPACE((int)(SPACE_NEEDED - 2), expdest);
379d90bee97SLionel Sambuc USTPUTC('\0', expdest);
380d90bee97SLionel Sambuc start = stackblock();
381d90bee97SLionel Sambuc p = expdest - 1;
382d90bee97SLionel Sambuc while (*p != CTLARI && p >= start)
383d90bee97SLionel Sambuc --p;
384d90bee97SLionel Sambuc if (*p != CTLARI)
385d90bee97SLionel Sambuc error("missing CTLARI (shouldn't happen)");
386d90bee97SLionel Sambuc if (p > start && *(p-1) == CTLESC)
387d90bee97SLionel Sambuc for (p = start; *p != CTLARI; p++)
388d90bee97SLionel Sambuc if (*p == CTLESC)
389d90bee97SLionel Sambuc p++;
390d90bee97SLionel Sambuc
391d90bee97SLionel Sambuc if (p[1] == '"')
392d90bee97SLionel Sambuc quoted=1;
393d90bee97SLionel Sambuc else
394d90bee97SLionel Sambuc quoted=0;
395d90bee97SLionel Sambuc begoff = p - start;
396d90bee97SLionel Sambuc removerecordregions(begoff);
397d90bee97SLionel Sambuc if (quotes)
398d90bee97SLionel Sambuc rmescapes(p+2);
399d90bee97SLionel Sambuc result = arith(p+2);
400d90bee97SLionel Sambuc fmtstr(p, SPACE_NEEDED, "%"PRIdMAX, result);
401d90bee97SLionel Sambuc
402d90bee97SLionel Sambuc while (*p++)
403d90bee97SLionel Sambuc ;
404d90bee97SLionel Sambuc
405d90bee97SLionel Sambuc if (quoted == 0)
406d90bee97SLionel Sambuc recordregion(begoff, p - 1 - start, 0);
407d90bee97SLionel Sambuc adjustment = expdest - p + 1;
408d90bee97SLionel Sambuc STADJUST(-adjustment, expdest);
409d90bee97SLionel Sambuc }
410d90bee97SLionel Sambuc
411d90bee97SLionel Sambuc
412d90bee97SLionel Sambuc /*
413d90bee97SLionel Sambuc * Expand stuff in backwards quotes.
414d90bee97SLionel Sambuc */
415d90bee97SLionel Sambuc
416d90bee97SLionel Sambuc STATIC void
expbackq(union node * cmd,int quoted,int flag)417d90bee97SLionel Sambuc expbackq(union node *cmd, int quoted, int flag)
418d90bee97SLionel Sambuc {
419d90bee97SLionel Sambuc struct backcmd in;
420d90bee97SLionel Sambuc int i;
421d90bee97SLionel Sambuc char buf[128];
422d90bee97SLionel Sambuc char *p;
423d90bee97SLionel Sambuc char *dest = expdest;
424d90bee97SLionel Sambuc struct ifsregion saveifs, *savelastp;
425d90bee97SLionel Sambuc struct nodelist *saveargbackq;
426d90bee97SLionel Sambuc char lastc;
427d90bee97SLionel Sambuc int startloc = dest - stackblock();
428d90bee97SLionel Sambuc char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
429d90bee97SLionel Sambuc int saveherefd;
430d90bee97SLionel Sambuc int quotes = flag & (EXP_FULL | EXP_CASE);
431d90bee97SLionel Sambuc int nnl;
432d90bee97SLionel Sambuc
433d90bee97SLionel Sambuc INTOFF;
434d90bee97SLionel Sambuc saveifs = ifsfirst;
435d90bee97SLionel Sambuc savelastp = ifslastp;
436d90bee97SLionel Sambuc saveargbackq = argbackq;
437d90bee97SLionel Sambuc saveherefd = herefd;
438d90bee97SLionel Sambuc herefd = -1;
439d90bee97SLionel Sambuc p = grabstackstr(dest);
440d90bee97SLionel Sambuc evalbackcmd(cmd, &in);
441d90bee97SLionel Sambuc ungrabstackstr(p, dest);
442d90bee97SLionel Sambuc ifsfirst = saveifs;
443d90bee97SLionel Sambuc ifslastp = savelastp;
444d90bee97SLionel Sambuc argbackq = saveargbackq;
445d90bee97SLionel Sambuc herefd = saveherefd;
446d90bee97SLionel Sambuc
447d90bee97SLionel Sambuc p = in.buf;
448d90bee97SLionel Sambuc lastc = '\0';
449d90bee97SLionel Sambuc nnl = 0;
450d90bee97SLionel Sambuc for (;;) {
451d90bee97SLionel Sambuc if (--in.nleft < 0) {
452d90bee97SLionel Sambuc if (in.fd < 0)
453d90bee97SLionel Sambuc break;
454*0a6a1f1dSLionel Sambuc while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR)
455*0a6a1f1dSLionel Sambuc continue;
456d90bee97SLionel Sambuc TRACE(("expbackq: read returns %d\n", i));
457d90bee97SLionel Sambuc if (i <= 0)
458d90bee97SLionel Sambuc break;
459d90bee97SLionel Sambuc p = buf;
460d90bee97SLionel Sambuc in.nleft = i - 1;
461d90bee97SLionel Sambuc }
462d90bee97SLionel Sambuc lastc = *p++;
463d90bee97SLionel Sambuc if (lastc != '\0') {
464d90bee97SLionel Sambuc if (lastc == '\n')
465d90bee97SLionel Sambuc nnl++;
466d90bee97SLionel Sambuc else {
467d90bee97SLionel Sambuc CHECKSTRSPACE(nnl + 2, dest);
468d90bee97SLionel Sambuc while (nnl > 0) {
469d90bee97SLionel Sambuc nnl--;
470d90bee97SLionel Sambuc USTPUTC('\n', dest);
471d90bee97SLionel Sambuc }
472d90bee97SLionel Sambuc if (quotes && syntax[(int)lastc] == CCTL)
473d90bee97SLionel Sambuc USTPUTC(CTLESC, dest);
474d90bee97SLionel Sambuc USTPUTC(lastc, dest);
475d90bee97SLionel Sambuc }
476d90bee97SLionel Sambuc }
477d90bee97SLionel Sambuc }
478d90bee97SLionel Sambuc
479d90bee97SLionel Sambuc if (in.fd >= 0)
480d90bee97SLionel Sambuc close(in.fd);
481d90bee97SLionel Sambuc if (in.buf)
482d90bee97SLionel Sambuc ckfree(in.buf);
483d90bee97SLionel Sambuc if (in.jp)
484d90bee97SLionel Sambuc back_exitstatus = waitforjob(in.jp);
485d90bee97SLionel Sambuc if (quoted == 0)
486d90bee97SLionel Sambuc recordregion(startloc, dest - stackblock(), 0);
487d90bee97SLionel Sambuc TRACE(("evalbackq: size=%d: \"%.*s\"\n",
488d90bee97SLionel Sambuc (int)((dest - stackblock()) - startloc),
489d90bee97SLionel Sambuc (int)((dest - stackblock()) - startloc),
490d90bee97SLionel Sambuc stackblock() + startloc));
491d90bee97SLionel Sambuc expdest = dest;
492d90bee97SLionel Sambuc INTON;
493d90bee97SLionel Sambuc }
494d90bee97SLionel Sambuc
495d90bee97SLionel Sambuc
496d90bee97SLionel Sambuc
497d90bee97SLionel Sambuc STATIC int
subevalvar(char * p,char * str,int strloc,int subtype,int startloc,int varflags,int quotes)498*0a6a1f1dSLionel Sambuc subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags, int quotes)
499d90bee97SLionel Sambuc {
500d90bee97SLionel Sambuc char *startp;
501d90bee97SLionel Sambuc char *loc = NULL;
502d90bee97SLionel Sambuc char *q;
503d90bee97SLionel Sambuc int c = 0;
504d90bee97SLionel Sambuc int saveherefd = herefd;
505d90bee97SLionel Sambuc struct nodelist *saveargbackq = argbackq;
506d90bee97SLionel Sambuc int amount, how;
507d90bee97SLionel Sambuc
508d90bee97SLionel Sambuc herefd = -1;
509d90bee97SLionel Sambuc switch (subtype) {
510d90bee97SLionel Sambuc case VSTRIMLEFT:
511d90bee97SLionel Sambuc case VSTRIMLEFTMAX:
512d90bee97SLionel Sambuc case VSTRIMRIGHT:
513d90bee97SLionel Sambuc case VSTRIMRIGHTMAX:
514d90bee97SLionel Sambuc how = (varflags & VSQUOTE) ? 0 : EXP_CASE;
515d90bee97SLionel Sambuc break;
516d90bee97SLionel Sambuc default:
517d90bee97SLionel Sambuc how = 0;
518d90bee97SLionel Sambuc break;
519d90bee97SLionel Sambuc }
520d90bee97SLionel Sambuc argstr(p, how);
521d90bee97SLionel Sambuc STACKSTRNUL(expdest);
522d90bee97SLionel Sambuc herefd = saveherefd;
523d90bee97SLionel Sambuc argbackq = saveargbackq;
524d90bee97SLionel Sambuc startp = stackblock() + startloc;
525d90bee97SLionel Sambuc if (str == NULL)
526d90bee97SLionel Sambuc str = stackblock() + strloc;
527d90bee97SLionel Sambuc
528d90bee97SLionel Sambuc switch (subtype) {
529d90bee97SLionel Sambuc case VSASSIGN:
530d90bee97SLionel Sambuc setvar(str, startp, 0);
531d90bee97SLionel Sambuc amount = startp - expdest;
532d90bee97SLionel Sambuc STADJUST(amount, expdest);
533d90bee97SLionel Sambuc varflags &= ~VSNUL;
534d90bee97SLionel Sambuc return 1;
535d90bee97SLionel Sambuc
536d90bee97SLionel Sambuc case VSQUESTION:
537d90bee97SLionel Sambuc if (*p != CTLENDVAR) {
538d90bee97SLionel Sambuc outfmt(&errout, "%s\n", startp);
539d90bee97SLionel Sambuc error(NULL);
540d90bee97SLionel Sambuc }
541d90bee97SLionel Sambuc error("%.*s: parameter %snot set",
542d90bee97SLionel Sambuc (int)(p - str - 1),
543d90bee97SLionel Sambuc str, (varflags & VSNUL) ? "null or "
544d90bee97SLionel Sambuc : nullstr);
545d90bee97SLionel Sambuc /* NOTREACHED */
546d90bee97SLionel Sambuc
547d90bee97SLionel Sambuc case VSTRIMLEFT:
548d90bee97SLionel Sambuc for (loc = startp; loc < str; loc++) {
549d90bee97SLionel Sambuc c = *loc;
550d90bee97SLionel Sambuc *loc = '\0';
551*0a6a1f1dSLionel Sambuc if (patmatch(str, startp, quotes))
552d90bee97SLionel Sambuc goto recordleft;
553d90bee97SLionel Sambuc *loc = c;
554*0a6a1f1dSLionel Sambuc if (quotes && *loc == CTLESC)
555d90bee97SLionel Sambuc loc++;
556d90bee97SLionel Sambuc }
557d90bee97SLionel Sambuc return 0;
558d90bee97SLionel Sambuc
559d90bee97SLionel Sambuc case VSTRIMLEFTMAX:
560d90bee97SLionel Sambuc for (loc = str - 1; loc >= startp;) {
561d90bee97SLionel Sambuc c = *loc;
562d90bee97SLionel Sambuc *loc = '\0';
563*0a6a1f1dSLionel Sambuc if (patmatch(str, startp, quotes))
564d90bee97SLionel Sambuc goto recordleft;
565d90bee97SLionel Sambuc *loc = c;
566d90bee97SLionel Sambuc loc--;
567*0a6a1f1dSLionel Sambuc if (quotes && loc > startp &&
568d90bee97SLionel Sambuc *(loc - 1) == CTLESC) {
569d90bee97SLionel Sambuc for (q = startp; q < loc; q++)
570d90bee97SLionel Sambuc if (*q == CTLESC)
571d90bee97SLionel Sambuc q++;
572d90bee97SLionel Sambuc if (q > loc)
573d90bee97SLionel Sambuc loc--;
574d90bee97SLionel Sambuc }
575d90bee97SLionel Sambuc }
576d90bee97SLionel Sambuc return 0;
577d90bee97SLionel Sambuc
578d90bee97SLionel Sambuc case VSTRIMRIGHT:
579d90bee97SLionel Sambuc for (loc = str - 1; loc >= startp;) {
580*0a6a1f1dSLionel Sambuc if (patmatch(str, loc, quotes))
581d90bee97SLionel Sambuc goto recordright;
582d90bee97SLionel Sambuc loc--;
583*0a6a1f1dSLionel Sambuc if (quotes && loc > startp &&
584d90bee97SLionel Sambuc *(loc - 1) == CTLESC) {
585d90bee97SLionel Sambuc for (q = startp; q < loc; q++)
586d90bee97SLionel Sambuc if (*q == CTLESC)
587d90bee97SLionel Sambuc q++;
588d90bee97SLionel Sambuc if (q > loc)
589d90bee97SLionel Sambuc loc--;
590d90bee97SLionel Sambuc }
591d90bee97SLionel Sambuc }
592d90bee97SLionel Sambuc return 0;
593d90bee97SLionel Sambuc
594d90bee97SLionel Sambuc case VSTRIMRIGHTMAX:
595d90bee97SLionel Sambuc for (loc = startp; loc < str - 1; loc++) {
596*0a6a1f1dSLionel Sambuc if (patmatch(str, loc, quotes))
597d90bee97SLionel Sambuc goto recordright;
598*0a6a1f1dSLionel Sambuc if (quotes && *loc == CTLESC)
599d90bee97SLionel Sambuc loc++;
600d90bee97SLionel Sambuc }
601d90bee97SLionel Sambuc return 0;
602d90bee97SLionel Sambuc
603d90bee97SLionel Sambuc default:
604d90bee97SLionel Sambuc abort();
605d90bee97SLionel Sambuc }
606d90bee97SLionel Sambuc
607d90bee97SLionel Sambuc recordleft:
608d90bee97SLionel Sambuc *loc = c;
609d90bee97SLionel Sambuc amount = ((str - 1) - (loc - startp)) - expdest;
610d90bee97SLionel Sambuc STADJUST(amount, expdest);
611d90bee97SLionel Sambuc while (loc != str - 1)
612d90bee97SLionel Sambuc *startp++ = *loc++;
613d90bee97SLionel Sambuc return 1;
614d90bee97SLionel Sambuc
615d90bee97SLionel Sambuc recordright:
616d90bee97SLionel Sambuc amount = loc - expdest;
617d90bee97SLionel Sambuc STADJUST(amount, expdest);
618d90bee97SLionel Sambuc STPUTC('\0', expdest);
619d90bee97SLionel Sambuc STADJUST(-1, expdest);
620d90bee97SLionel Sambuc return 1;
621d90bee97SLionel Sambuc }
622d90bee97SLionel Sambuc
623d90bee97SLionel Sambuc
624d90bee97SLionel Sambuc /*
625d90bee97SLionel Sambuc * Expand a variable, and return a pointer to the next character in the
626d90bee97SLionel Sambuc * input string.
627d90bee97SLionel Sambuc */
628d90bee97SLionel Sambuc
629d90bee97SLionel Sambuc STATIC char *
evalvar(char * p,int flag)630d90bee97SLionel Sambuc evalvar(char *p, int flag)
631d90bee97SLionel Sambuc {
632d90bee97SLionel Sambuc int subtype;
633d90bee97SLionel Sambuc int varflags;
634d90bee97SLionel Sambuc char *var;
635d90bee97SLionel Sambuc char *val;
636d90bee97SLionel Sambuc int patloc;
637d90bee97SLionel Sambuc int c;
638d90bee97SLionel Sambuc int set;
639d90bee97SLionel Sambuc int special;
640d90bee97SLionel Sambuc int startloc;
641d90bee97SLionel Sambuc int varlen;
642d90bee97SLionel Sambuc int apply_ifs;
643d90bee97SLionel Sambuc int quotes = flag & (EXP_FULL | EXP_CASE);
644d90bee97SLionel Sambuc
645d90bee97SLionel Sambuc varflags = (unsigned char)*p++;
646d90bee97SLionel Sambuc subtype = varflags & VSTYPE;
647d90bee97SLionel Sambuc var = p;
648d90bee97SLionel Sambuc special = !is_name(*p);
649d90bee97SLionel Sambuc p = strchr(p, '=') + 1;
650d90bee97SLionel Sambuc
651d90bee97SLionel Sambuc again: /* jump here after setting a variable with ${var=text} */
652d90bee97SLionel Sambuc if (varflags & VSLINENO) {
653d90bee97SLionel Sambuc set = 1;
654d90bee97SLionel Sambuc special = 0;
655d90bee97SLionel Sambuc val = var;
656d90bee97SLionel Sambuc p[-1] = '\0';
657d90bee97SLionel Sambuc } else if (special) {
658d90bee97SLionel Sambuc set = varisset(var, varflags & VSNUL);
659d90bee97SLionel Sambuc val = NULL;
660d90bee97SLionel Sambuc } else {
661d90bee97SLionel Sambuc val = lookupvar(var);
662d90bee97SLionel Sambuc if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
663d90bee97SLionel Sambuc val = NULL;
664d90bee97SLionel Sambuc set = 0;
665d90bee97SLionel Sambuc } else
666d90bee97SLionel Sambuc set = 1;
667d90bee97SLionel Sambuc }
668d90bee97SLionel Sambuc
669d90bee97SLionel Sambuc varlen = 0;
670d90bee97SLionel Sambuc startloc = expdest - stackblock();
671d90bee97SLionel Sambuc
672d90bee97SLionel Sambuc if (!set && uflag && *var != '@' && *var != '*') {
673d90bee97SLionel Sambuc switch (subtype) {
674d90bee97SLionel Sambuc case VSNORMAL:
675d90bee97SLionel Sambuc case VSTRIMLEFT:
676d90bee97SLionel Sambuc case VSTRIMLEFTMAX:
677d90bee97SLionel Sambuc case VSTRIMRIGHT:
678d90bee97SLionel Sambuc case VSTRIMRIGHTMAX:
679d90bee97SLionel Sambuc case VSLENGTH:
680d90bee97SLionel Sambuc error("%.*s: parameter not set",
681d90bee97SLionel Sambuc (int)(p - var - 1), var);
682d90bee97SLionel Sambuc /* NOTREACHED */
683d90bee97SLionel Sambuc }
684d90bee97SLionel Sambuc }
685d90bee97SLionel Sambuc
686d90bee97SLionel Sambuc if (set && subtype != VSPLUS) {
687d90bee97SLionel Sambuc /* insert the value of the variable */
688d90bee97SLionel Sambuc if (special) {
689d90bee97SLionel Sambuc varvalue(var, varflags & VSQUOTE, subtype, flag);
690d90bee97SLionel Sambuc if (subtype == VSLENGTH) {
691d90bee97SLionel Sambuc varlen = expdest - stackblock() - startloc;
692d90bee97SLionel Sambuc STADJUST(-varlen, expdest);
693d90bee97SLionel Sambuc }
694d90bee97SLionel Sambuc } else {
695d90bee97SLionel Sambuc char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
696d90bee97SLionel Sambuc : BASESYNTAX;
697d90bee97SLionel Sambuc
698d90bee97SLionel Sambuc if (subtype == VSLENGTH) {
699d90bee97SLionel Sambuc for (;*val; val++)
700d90bee97SLionel Sambuc varlen++;
701d90bee97SLionel Sambuc } else {
702d90bee97SLionel Sambuc while (*val) {
703d90bee97SLionel Sambuc if (quotes && syntax[(int)*val] == CCTL)
704d90bee97SLionel Sambuc STPUTC(CTLESC, expdest);
705d90bee97SLionel Sambuc STPUTC(*val++, expdest);
706d90bee97SLionel Sambuc }
707d90bee97SLionel Sambuc
708d90bee97SLionel Sambuc }
709d90bee97SLionel Sambuc }
710d90bee97SLionel Sambuc }
711d90bee97SLionel Sambuc
712d90bee97SLionel Sambuc
713d90bee97SLionel Sambuc if (flag & EXP_IN_QUOTES)
714d90bee97SLionel Sambuc apply_ifs = 0;
715d90bee97SLionel Sambuc else if (varflags & VSQUOTE) {
716d90bee97SLionel Sambuc if (*var == '@' && shellparam.nparam != 1)
717d90bee97SLionel Sambuc apply_ifs = 1;
718d90bee97SLionel Sambuc else {
719d90bee97SLionel Sambuc /*
720d90bee97SLionel Sambuc * Mark so that we don't apply IFS if we recurse through
721d90bee97SLionel Sambuc * here expanding $bar from "${foo-$bar}".
722d90bee97SLionel Sambuc */
723d90bee97SLionel Sambuc flag |= EXP_IN_QUOTES;
724d90bee97SLionel Sambuc apply_ifs = 0;
725d90bee97SLionel Sambuc }
726d90bee97SLionel Sambuc } else
727d90bee97SLionel Sambuc apply_ifs = 1;
728d90bee97SLionel Sambuc
729d90bee97SLionel Sambuc switch (subtype) {
730d90bee97SLionel Sambuc case VSLENGTH:
731d90bee97SLionel Sambuc expdest = cvtnum(varlen, expdest);
732d90bee97SLionel Sambuc break;
733d90bee97SLionel Sambuc
734d90bee97SLionel Sambuc case VSNORMAL:
735d90bee97SLionel Sambuc break;
736d90bee97SLionel Sambuc
737d90bee97SLionel Sambuc case VSPLUS:
738d90bee97SLionel Sambuc set = !set;
739d90bee97SLionel Sambuc /* FALLTHROUGH */
740d90bee97SLionel Sambuc case VSMINUS:
741d90bee97SLionel Sambuc if (!set) {
742d90bee97SLionel Sambuc argstr(p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0));
743d90bee97SLionel Sambuc /*
744d90bee97SLionel Sambuc * ${x-a b c} doesn't get split, but removing the
745d90bee97SLionel Sambuc * 'apply_ifs = 0' apparently breaks ${1+"$@"}..
746d90bee97SLionel Sambuc * ${x-'a b' c} should generate 2 args.
747d90bee97SLionel Sambuc */
748d90bee97SLionel Sambuc /* We should have marked stuff already */
749d90bee97SLionel Sambuc apply_ifs = 0;
750d90bee97SLionel Sambuc }
751d90bee97SLionel Sambuc break;
752d90bee97SLionel Sambuc
753d90bee97SLionel Sambuc case VSTRIMLEFT:
754d90bee97SLionel Sambuc case VSTRIMLEFTMAX:
755d90bee97SLionel Sambuc case VSTRIMRIGHT:
756d90bee97SLionel Sambuc case VSTRIMRIGHTMAX:
757d90bee97SLionel Sambuc if (!set)
758d90bee97SLionel Sambuc break;
759d90bee97SLionel Sambuc /*
760d90bee97SLionel Sambuc * Terminate the string and start recording the pattern
761d90bee97SLionel Sambuc * right after it
762d90bee97SLionel Sambuc */
763d90bee97SLionel Sambuc STPUTC('\0', expdest);
764d90bee97SLionel Sambuc patloc = expdest - stackblock();
765d90bee97SLionel Sambuc if (subevalvar(p, NULL, patloc, subtype,
766*0a6a1f1dSLionel Sambuc startloc, varflags, quotes) == 0) {
767d90bee97SLionel Sambuc int amount = (expdest - stackblock() - patloc) + 1;
768d90bee97SLionel Sambuc STADJUST(-amount, expdest);
769d90bee97SLionel Sambuc }
770d90bee97SLionel Sambuc /* Remove any recorded regions beyond start of variable */
771d90bee97SLionel Sambuc removerecordregions(startloc);
772d90bee97SLionel Sambuc apply_ifs = 1;
773d90bee97SLionel Sambuc break;
774d90bee97SLionel Sambuc
775d90bee97SLionel Sambuc case VSASSIGN:
776d90bee97SLionel Sambuc case VSQUESTION:
777d90bee97SLionel Sambuc if (set)
778d90bee97SLionel Sambuc break;
779*0a6a1f1dSLionel Sambuc if (subevalvar(p, var, 0, subtype, startloc, varflags, quotes)) {
780d90bee97SLionel Sambuc varflags &= ~VSNUL;
781d90bee97SLionel Sambuc /*
782d90bee97SLionel Sambuc * Remove any recorded regions beyond
783d90bee97SLionel Sambuc * start of variable
784d90bee97SLionel Sambuc */
785d90bee97SLionel Sambuc removerecordregions(startloc);
786d90bee97SLionel Sambuc goto again;
787d90bee97SLionel Sambuc }
788d90bee97SLionel Sambuc apply_ifs = 0;
789d90bee97SLionel Sambuc break;
790d90bee97SLionel Sambuc
791d90bee97SLionel Sambuc default:
792d90bee97SLionel Sambuc abort();
793d90bee97SLionel Sambuc }
794d90bee97SLionel Sambuc p[-1] = '='; /* recover overwritten '=' */
795d90bee97SLionel Sambuc
796d90bee97SLionel Sambuc if (apply_ifs)
797d90bee97SLionel Sambuc recordregion(startloc, expdest - stackblock(),
798d90bee97SLionel Sambuc varflags & VSQUOTE);
799d90bee97SLionel Sambuc
800d90bee97SLionel Sambuc if (subtype != VSNORMAL) { /* skip to end of alternative */
801d90bee97SLionel Sambuc int nesting = 1;
802d90bee97SLionel Sambuc for (;;) {
803d90bee97SLionel Sambuc if ((c = *p++) == CTLESC)
804d90bee97SLionel Sambuc p++;
805d90bee97SLionel Sambuc else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
806d90bee97SLionel Sambuc if (set)
807d90bee97SLionel Sambuc argbackq = argbackq->next;
808d90bee97SLionel Sambuc } else if (c == CTLVAR) {
809d90bee97SLionel Sambuc if ((*p++ & VSTYPE) != VSNORMAL)
810d90bee97SLionel Sambuc nesting++;
811d90bee97SLionel Sambuc } else if (c == CTLENDVAR) {
812d90bee97SLionel Sambuc if (--nesting == 0)
813d90bee97SLionel Sambuc break;
814d90bee97SLionel Sambuc }
815d90bee97SLionel Sambuc }
816d90bee97SLionel Sambuc }
817d90bee97SLionel Sambuc return p;
818d90bee97SLionel Sambuc }
819d90bee97SLionel Sambuc
820d90bee97SLionel Sambuc
821d90bee97SLionel Sambuc
822d90bee97SLionel Sambuc /*
823d90bee97SLionel Sambuc * Test whether a specialized variable is set.
824d90bee97SLionel Sambuc */
825d90bee97SLionel Sambuc
826d90bee97SLionel Sambuc STATIC int
varisset(char * name,int nulok)827d90bee97SLionel Sambuc varisset(char *name, int nulok)
828d90bee97SLionel Sambuc {
829d90bee97SLionel Sambuc if (*name == '!')
830d90bee97SLionel Sambuc return backgndpid != -1;
831d90bee97SLionel Sambuc else if (*name == '@' || *name == '*') {
832d90bee97SLionel Sambuc if (*shellparam.p == NULL)
833d90bee97SLionel Sambuc return 0;
834d90bee97SLionel Sambuc
835d90bee97SLionel Sambuc if (nulok) {
836d90bee97SLionel Sambuc char **av;
837d90bee97SLionel Sambuc
838d90bee97SLionel Sambuc for (av = shellparam.p; *av; av++)
839d90bee97SLionel Sambuc if (**av != '\0')
840d90bee97SLionel Sambuc return 1;
841d90bee97SLionel Sambuc return 0;
842d90bee97SLionel Sambuc }
843d90bee97SLionel Sambuc } else if (is_digit(*name)) {
844d90bee97SLionel Sambuc char *ap;
845d90bee97SLionel Sambuc int num = atoi(name);
846d90bee97SLionel Sambuc
847d90bee97SLionel Sambuc if (num > shellparam.nparam)
848d90bee97SLionel Sambuc return 0;
849d90bee97SLionel Sambuc
850d90bee97SLionel Sambuc if (num == 0)
851d90bee97SLionel Sambuc ap = arg0;
852d90bee97SLionel Sambuc else
853d90bee97SLionel Sambuc ap = shellparam.p[num - 1];
854d90bee97SLionel Sambuc
855d90bee97SLionel Sambuc if (nulok && (ap == NULL || *ap == '\0'))
856d90bee97SLionel Sambuc return 0;
857d90bee97SLionel Sambuc }
858d90bee97SLionel Sambuc return 1;
859d90bee97SLionel Sambuc }
860d90bee97SLionel Sambuc
861d90bee97SLionel Sambuc
862d90bee97SLionel Sambuc
863d90bee97SLionel Sambuc /*
864d90bee97SLionel Sambuc * Add the value of a specialized variable to the stack string.
865d90bee97SLionel Sambuc */
866d90bee97SLionel Sambuc
867d90bee97SLionel Sambuc STATIC void
varvalue(char * name,int quoted,int subtype,int flag)868d90bee97SLionel Sambuc varvalue(char *name, int quoted, int subtype, int flag)
869d90bee97SLionel Sambuc {
870d90bee97SLionel Sambuc int num;
871d90bee97SLionel Sambuc char *p;
872d90bee97SLionel Sambuc int i;
873d90bee97SLionel Sambuc char sep;
874d90bee97SLionel Sambuc char **ap;
875d90bee97SLionel Sambuc char const *syntax;
876d90bee97SLionel Sambuc
877d90bee97SLionel Sambuc #define STRTODEST(p) \
878d90bee97SLionel Sambuc do {\
879d90bee97SLionel Sambuc if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
880d90bee97SLionel Sambuc syntax = quoted? DQSYNTAX : BASESYNTAX; \
881d90bee97SLionel Sambuc while (*p) { \
882d90bee97SLionel Sambuc if (syntax[(int)*p] == CCTL) \
883d90bee97SLionel Sambuc STPUTC(CTLESC, expdest); \
884d90bee97SLionel Sambuc STPUTC(*p++, expdest); \
885d90bee97SLionel Sambuc } \
886d90bee97SLionel Sambuc } else \
887d90bee97SLionel Sambuc while (*p) \
888d90bee97SLionel Sambuc STPUTC(*p++, expdest); \
889d90bee97SLionel Sambuc } while (0)
890d90bee97SLionel Sambuc
891d90bee97SLionel Sambuc
892d90bee97SLionel Sambuc switch (*name) {
893d90bee97SLionel Sambuc case '$':
894d90bee97SLionel Sambuc num = rootpid;
895d90bee97SLionel Sambuc goto numvar;
896d90bee97SLionel Sambuc case '?':
897d90bee97SLionel Sambuc num = exitstatus;
898d90bee97SLionel Sambuc goto numvar;
899d90bee97SLionel Sambuc case '#':
900d90bee97SLionel Sambuc num = shellparam.nparam;
901d90bee97SLionel Sambuc goto numvar;
902d90bee97SLionel Sambuc case '!':
903d90bee97SLionel Sambuc num = backgndpid;
904d90bee97SLionel Sambuc numvar:
905d90bee97SLionel Sambuc expdest = cvtnum(num, expdest);
906d90bee97SLionel Sambuc break;
907d90bee97SLionel Sambuc case '-':
908d90bee97SLionel Sambuc for (i = 0; optlist[i].name; i++) {
909d90bee97SLionel Sambuc if (optlist[i].val && optlist[i].letter)
910d90bee97SLionel Sambuc STPUTC(optlist[i].letter, expdest);
911d90bee97SLionel Sambuc }
912d90bee97SLionel Sambuc break;
913d90bee97SLionel Sambuc case '@':
914d90bee97SLionel Sambuc if (flag & EXP_FULL && quoted) {
915d90bee97SLionel Sambuc for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
916d90bee97SLionel Sambuc STRTODEST(p);
917d90bee97SLionel Sambuc if (*ap)
918d90bee97SLionel Sambuc /* A NUL separates args inside "" */
919d90bee97SLionel Sambuc STPUTC('\0', expdest);
920d90bee97SLionel Sambuc }
921d90bee97SLionel Sambuc break;
922d90bee97SLionel Sambuc }
923d90bee97SLionel Sambuc /* fall through */
924d90bee97SLionel Sambuc case '*':
925d90bee97SLionel Sambuc if (ifsset() != 0)
926d90bee97SLionel Sambuc sep = ifsval()[0];
927d90bee97SLionel Sambuc else
928d90bee97SLionel Sambuc sep = ' ';
929d90bee97SLionel Sambuc for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
930d90bee97SLionel Sambuc STRTODEST(p);
931d90bee97SLionel Sambuc if (*ap && sep)
932d90bee97SLionel Sambuc STPUTC(sep, expdest);
933d90bee97SLionel Sambuc }
934d90bee97SLionel Sambuc break;
935d90bee97SLionel Sambuc case '0':
936d90bee97SLionel Sambuc p = arg0;
937d90bee97SLionel Sambuc STRTODEST(p);
938d90bee97SLionel Sambuc break;
939d90bee97SLionel Sambuc default:
940d90bee97SLionel Sambuc if (is_digit(*name)) {
941d90bee97SLionel Sambuc num = atoi(name);
942d90bee97SLionel Sambuc if (num > 0 && num <= shellparam.nparam) {
943d90bee97SLionel Sambuc p = shellparam.p[num - 1];
944d90bee97SLionel Sambuc STRTODEST(p);
945d90bee97SLionel Sambuc }
946d90bee97SLionel Sambuc }
947d90bee97SLionel Sambuc break;
948d90bee97SLionel Sambuc }
949d90bee97SLionel Sambuc }
950d90bee97SLionel Sambuc
951d90bee97SLionel Sambuc
952d90bee97SLionel Sambuc
953d90bee97SLionel Sambuc /*
954d90bee97SLionel Sambuc * Record the fact that we have to scan this region of the
955d90bee97SLionel Sambuc * string for IFS characters.
956d90bee97SLionel Sambuc */
957d90bee97SLionel Sambuc
958d90bee97SLionel Sambuc STATIC void
recordregion(int start,int end,int inquotes)959d90bee97SLionel Sambuc recordregion(int start, int end, int inquotes)
960d90bee97SLionel Sambuc {
961d90bee97SLionel Sambuc struct ifsregion *ifsp;
962d90bee97SLionel Sambuc
963d90bee97SLionel Sambuc if (ifslastp == NULL) {
964d90bee97SLionel Sambuc ifsp = &ifsfirst;
965d90bee97SLionel Sambuc } else {
966d90bee97SLionel Sambuc if (ifslastp->endoff == start
967d90bee97SLionel Sambuc && ifslastp->inquotes == inquotes) {
968d90bee97SLionel Sambuc /* extend previous area */
969d90bee97SLionel Sambuc ifslastp->endoff = end;
970d90bee97SLionel Sambuc return;
971d90bee97SLionel Sambuc }
972d90bee97SLionel Sambuc ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
973d90bee97SLionel Sambuc ifslastp->next = ifsp;
974d90bee97SLionel Sambuc }
975d90bee97SLionel Sambuc ifslastp = ifsp;
976d90bee97SLionel Sambuc ifslastp->next = NULL;
977d90bee97SLionel Sambuc ifslastp->begoff = start;
978d90bee97SLionel Sambuc ifslastp->endoff = end;
979d90bee97SLionel Sambuc ifslastp->inquotes = inquotes;
980d90bee97SLionel Sambuc }
981d90bee97SLionel Sambuc
982d90bee97SLionel Sambuc
983d90bee97SLionel Sambuc
984d90bee97SLionel Sambuc /*
985d90bee97SLionel Sambuc * Break the argument string into pieces based upon IFS and add the
986d90bee97SLionel Sambuc * strings to the argument list. The regions of the string to be
987d90bee97SLionel Sambuc * searched for IFS characters have been stored by recordregion.
988d90bee97SLionel Sambuc */
989d90bee97SLionel Sambuc STATIC void
ifsbreakup(char * string,struct arglist * arglist)990d90bee97SLionel Sambuc ifsbreakup(char *string, struct arglist *arglist)
991d90bee97SLionel Sambuc {
992d90bee97SLionel Sambuc struct ifsregion *ifsp;
993d90bee97SLionel Sambuc struct strlist *sp;
994d90bee97SLionel Sambuc char *start;
995d90bee97SLionel Sambuc char *p;
996d90bee97SLionel Sambuc char *q;
997d90bee97SLionel Sambuc const char *ifs;
998d90bee97SLionel Sambuc const char *ifsspc;
999d90bee97SLionel Sambuc int had_param_ch = 0;
1000d90bee97SLionel Sambuc
1001d90bee97SLionel Sambuc start = string;
1002d90bee97SLionel Sambuc
1003d90bee97SLionel Sambuc if (ifslastp == NULL) {
1004d90bee97SLionel Sambuc /* Return entire argument, IFS doesn't apply to any of it */
1005d90bee97SLionel Sambuc sp = (struct strlist *)stalloc(sizeof *sp);
1006d90bee97SLionel Sambuc sp->text = start;
1007d90bee97SLionel Sambuc *arglist->lastp = sp;
1008d90bee97SLionel Sambuc arglist->lastp = &sp->next;
1009d90bee97SLionel Sambuc return;
1010d90bee97SLionel Sambuc }
1011d90bee97SLionel Sambuc
1012d90bee97SLionel Sambuc ifs = ifsset() ? ifsval() : " \t\n";
1013d90bee97SLionel Sambuc
1014d90bee97SLionel Sambuc for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
1015d90bee97SLionel Sambuc p = string + ifsp->begoff;
1016d90bee97SLionel Sambuc while (p < string + ifsp->endoff) {
1017d90bee97SLionel Sambuc had_param_ch = 1;
1018d90bee97SLionel Sambuc q = p;
1019d90bee97SLionel Sambuc if (*p == CTLESC)
1020d90bee97SLionel Sambuc p++;
1021d90bee97SLionel Sambuc if (ifsp->inquotes) {
1022d90bee97SLionel Sambuc /* Only NULs (should be from "$@") end args */
1023d90bee97SLionel Sambuc if (*p != 0) {
1024d90bee97SLionel Sambuc p++;
1025d90bee97SLionel Sambuc continue;
1026d90bee97SLionel Sambuc }
1027d90bee97SLionel Sambuc ifsspc = NULL;
1028d90bee97SLionel Sambuc } else {
1029d90bee97SLionel Sambuc if (!strchr(ifs, *p)) {
1030d90bee97SLionel Sambuc p++;
1031d90bee97SLionel Sambuc continue;
1032d90bee97SLionel Sambuc }
1033d90bee97SLionel Sambuc had_param_ch = 0;
1034d90bee97SLionel Sambuc ifsspc = strchr(" \t\n", *p);
1035d90bee97SLionel Sambuc
1036d90bee97SLionel Sambuc /* Ignore IFS whitespace at start */
1037d90bee97SLionel Sambuc if (q == start && ifsspc != NULL) {
1038d90bee97SLionel Sambuc p++;
1039d90bee97SLionel Sambuc start = p;
1040d90bee97SLionel Sambuc continue;
1041d90bee97SLionel Sambuc }
1042d90bee97SLionel Sambuc }
1043d90bee97SLionel Sambuc
1044d90bee97SLionel Sambuc /* Save this argument... */
1045d90bee97SLionel Sambuc *q = '\0';
1046d90bee97SLionel Sambuc sp = (struct strlist *)stalloc(sizeof *sp);
1047d90bee97SLionel Sambuc sp->text = start;
1048d90bee97SLionel Sambuc *arglist->lastp = sp;
1049d90bee97SLionel Sambuc arglist->lastp = &sp->next;
1050d90bee97SLionel Sambuc p++;
1051d90bee97SLionel Sambuc
1052d90bee97SLionel Sambuc if (ifsspc != NULL) {
1053d90bee97SLionel Sambuc /* Ignore further trailing IFS whitespace */
1054d90bee97SLionel Sambuc for (; p < string + ifsp->endoff; p++) {
1055d90bee97SLionel Sambuc q = p;
1056d90bee97SLionel Sambuc if (*p == CTLESC)
1057d90bee97SLionel Sambuc p++;
1058d90bee97SLionel Sambuc if (strchr(ifs, *p) == NULL) {
1059d90bee97SLionel Sambuc p = q;
1060d90bee97SLionel Sambuc break;
1061d90bee97SLionel Sambuc }
1062d90bee97SLionel Sambuc if (strchr(" \t\n", *p) == NULL) {
1063d90bee97SLionel Sambuc p++;
1064d90bee97SLionel Sambuc break;
1065d90bee97SLionel Sambuc }
1066d90bee97SLionel Sambuc }
1067d90bee97SLionel Sambuc }
1068d90bee97SLionel Sambuc start = p;
1069d90bee97SLionel Sambuc }
1070d90bee97SLionel Sambuc }
1071d90bee97SLionel Sambuc
1072d90bee97SLionel Sambuc /*
1073d90bee97SLionel Sambuc * Save anything left as an argument.
1074d90bee97SLionel Sambuc * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1075d90bee97SLionel Sambuc * generating 2 arguments, the second of which is empty.
1076d90bee97SLionel Sambuc * Some recent clarification of the Posix spec say that it
1077d90bee97SLionel Sambuc * should only generate one....
1078d90bee97SLionel Sambuc */
1079d90bee97SLionel Sambuc if (had_param_ch || *start != 0) {
1080d90bee97SLionel Sambuc sp = (struct strlist *)stalloc(sizeof *sp);
1081d90bee97SLionel Sambuc sp->text = start;
1082d90bee97SLionel Sambuc *arglist->lastp = sp;
1083d90bee97SLionel Sambuc arglist->lastp = &sp->next;
1084d90bee97SLionel Sambuc }
1085d90bee97SLionel Sambuc }
1086d90bee97SLionel Sambuc
1087d90bee97SLionel Sambuc STATIC void
ifsfree(void)1088d90bee97SLionel Sambuc ifsfree(void)
1089d90bee97SLionel Sambuc {
1090d90bee97SLionel Sambuc while (ifsfirst.next != NULL) {
1091d90bee97SLionel Sambuc struct ifsregion *ifsp;
1092d90bee97SLionel Sambuc INTOFF;
1093d90bee97SLionel Sambuc ifsp = ifsfirst.next->next;
1094d90bee97SLionel Sambuc ckfree(ifsfirst.next);
1095d90bee97SLionel Sambuc ifsfirst.next = ifsp;
1096d90bee97SLionel Sambuc INTON;
1097d90bee97SLionel Sambuc }
1098d90bee97SLionel Sambuc ifslastp = NULL;
1099d90bee97SLionel Sambuc ifsfirst.next = NULL;
1100d90bee97SLionel Sambuc }
1101d90bee97SLionel Sambuc
1102d90bee97SLionel Sambuc
1103d90bee97SLionel Sambuc
1104d90bee97SLionel Sambuc /*
1105d90bee97SLionel Sambuc * Expand shell metacharacters. At this point, the only control characters
1106d90bee97SLionel Sambuc * should be escapes. The results are stored in the list exparg.
1107d90bee97SLionel Sambuc */
1108d90bee97SLionel Sambuc
1109d90bee97SLionel Sambuc char *expdir;
1110d90bee97SLionel Sambuc
1111d90bee97SLionel Sambuc
1112d90bee97SLionel Sambuc STATIC void
expandmeta(struct strlist * str,int flag)1113d90bee97SLionel Sambuc expandmeta(struct strlist *str, int flag)
1114d90bee97SLionel Sambuc {
1115d90bee97SLionel Sambuc char *p;
1116d90bee97SLionel Sambuc struct strlist **savelastp;
1117d90bee97SLionel Sambuc struct strlist *sp;
1118d90bee97SLionel Sambuc char c;
1119d90bee97SLionel Sambuc /* TODO - EXP_REDIR */
1120d90bee97SLionel Sambuc
1121d90bee97SLionel Sambuc while (str) {
1122d90bee97SLionel Sambuc if (fflag)
1123d90bee97SLionel Sambuc goto nometa;
1124d90bee97SLionel Sambuc p = str->text;
1125d90bee97SLionel Sambuc for (;;) { /* fast check for meta chars */
1126d90bee97SLionel Sambuc if ((c = *p++) == '\0')
1127d90bee97SLionel Sambuc goto nometa;
1128d90bee97SLionel Sambuc if (c == '*' || c == '?' || c == '[' || c == '!')
1129d90bee97SLionel Sambuc break;
1130d90bee97SLionel Sambuc }
1131d90bee97SLionel Sambuc savelastp = exparg.lastp;
1132d90bee97SLionel Sambuc INTOFF;
1133d90bee97SLionel Sambuc if (expdir == NULL) {
1134d90bee97SLionel Sambuc int i = strlen(str->text);
1135d90bee97SLionel Sambuc expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
1136d90bee97SLionel Sambuc }
1137d90bee97SLionel Sambuc
1138d90bee97SLionel Sambuc expmeta(expdir, str->text);
1139d90bee97SLionel Sambuc ckfree(expdir);
1140d90bee97SLionel Sambuc expdir = NULL;
1141d90bee97SLionel Sambuc INTON;
1142d90bee97SLionel Sambuc if (exparg.lastp == savelastp) {
1143d90bee97SLionel Sambuc /*
1144d90bee97SLionel Sambuc * no matches
1145d90bee97SLionel Sambuc */
1146d90bee97SLionel Sambuc nometa:
1147d90bee97SLionel Sambuc *exparg.lastp = str;
1148d90bee97SLionel Sambuc rmescapes(str->text);
1149d90bee97SLionel Sambuc exparg.lastp = &str->next;
1150d90bee97SLionel Sambuc } else {
1151d90bee97SLionel Sambuc *exparg.lastp = NULL;
1152d90bee97SLionel Sambuc *savelastp = sp = expsort(*savelastp);
1153d90bee97SLionel Sambuc while (sp->next != NULL)
1154d90bee97SLionel Sambuc sp = sp->next;
1155d90bee97SLionel Sambuc exparg.lastp = &sp->next;
1156d90bee97SLionel Sambuc }
1157d90bee97SLionel Sambuc str = str->next;
1158d90bee97SLionel Sambuc }
1159d90bee97SLionel Sambuc }
1160d90bee97SLionel Sambuc
1161d90bee97SLionel Sambuc
1162d90bee97SLionel Sambuc /*
1163d90bee97SLionel Sambuc * Do metacharacter (i.e. *, ?, [...]) expansion.
1164d90bee97SLionel Sambuc */
1165d90bee97SLionel Sambuc
1166d90bee97SLionel Sambuc STATIC void
expmeta(char * enddir,char * name)1167d90bee97SLionel Sambuc expmeta(char *enddir, char *name)
1168d90bee97SLionel Sambuc {
1169d90bee97SLionel Sambuc char *p;
1170d90bee97SLionel Sambuc const char *cp;
1171d90bee97SLionel Sambuc char *q;
1172d90bee97SLionel Sambuc char *start;
1173d90bee97SLionel Sambuc char *endname;
1174d90bee97SLionel Sambuc int metaflag;
1175d90bee97SLionel Sambuc struct stat statb;
1176d90bee97SLionel Sambuc DIR *dirp;
1177d90bee97SLionel Sambuc struct dirent *dp;
1178d90bee97SLionel Sambuc int atend;
1179d90bee97SLionel Sambuc int matchdot;
1180d90bee97SLionel Sambuc
1181d90bee97SLionel Sambuc metaflag = 0;
1182d90bee97SLionel Sambuc start = name;
1183d90bee97SLionel Sambuc for (p = name ; ; p++) {
1184d90bee97SLionel Sambuc if (*p == '*' || *p == '?')
1185d90bee97SLionel Sambuc metaflag = 1;
1186d90bee97SLionel Sambuc else if (*p == '[') {
1187d90bee97SLionel Sambuc q = p + 1;
1188d90bee97SLionel Sambuc if (*q == '!')
1189d90bee97SLionel Sambuc q++;
1190d90bee97SLionel Sambuc for (;;) {
1191d90bee97SLionel Sambuc while (*q == CTLQUOTEMARK)
1192d90bee97SLionel Sambuc q++;
1193d90bee97SLionel Sambuc if (*q == CTLESC)
1194d90bee97SLionel Sambuc q++;
1195d90bee97SLionel Sambuc if (*q == '/' || *q == '\0')
1196d90bee97SLionel Sambuc break;
1197d90bee97SLionel Sambuc if (*++q == ']') {
1198d90bee97SLionel Sambuc metaflag = 1;
1199d90bee97SLionel Sambuc break;
1200d90bee97SLionel Sambuc }
1201d90bee97SLionel Sambuc }
1202d90bee97SLionel Sambuc } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
1203d90bee97SLionel Sambuc metaflag = 1;
1204d90bee97SLionel Sambuc } else if (*p == '\0')
1205d90bee97SLionel Sambuc break;
1206d90bee97SLionel Sambuc else if (*p == CTLQUOTEMARK)
1207d90bee97SLionel Sambuc continue;
1208d90bee97SLionel Sambuc else if (*p == CTLESC)
1209d90bee97SLionel Sambuc p++;
1210d90bee97SLionel Sambuc if (*p == '/') {
1211d90bee97SLionel Sambuc if (metaflag)
1212d90bee97SLionel Sambuc break;
1213d90bee97SLionel Sambuc start = p + 1;
1214d90bee97SLionel Sambuc }
1215d90bee97SLionel Sambuc }
1216d90bee97SLionel Sambuc if (metaflag == 0) { /* we've reached the end of the file name */
1217d90bee97SLionel Sambuc if (enddir != expdir)
1218d90bee97SLionel Sambuc metaflag++;
1219d90bee97SLionel Sambuc for (p = name ; ; p++) {
1220d90bee97SLionel Sambuc if (*p == CTLQUOTEMARK)
1221d90bee97SLionel Sambuc continue;
1222d90bee97SLionel Sambuc if (*p == CTLESC)
1223d90bee97SLionel Sambuc p++;
1224d90bee97SLionel Sambuc *enddir++ = *p;
1225d90bee97SLionel Sambuc if (*p == '\0')
1226d90bee97SLionel Sambuc break;
1227d90bee97SLionel Sambuc }
1228d90bee97SLionel Sambuc if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1229d90bee97SLionel Sambuc addfname(expdir);
1230d90bee97SLionel Sambuc return;
1231d90bee97SLionel Sambuc }
1232d90bee97SLionel Sambuc endname = p;
1233d90bee97SLionel Sambuc if (start != name) {
1234d90bee97SLionel Sambuc p = name;
1235d90bee97SLionel Sambuc while (p < start) {
1236d90bee97SLionel Sambuc while (*p == CTLQUOTEMARK)
1237d90bee97SLionel Sambuc p++;
1238d90bee97SLionel Sambuc if (*p == CTLESC)
1239d90bee97SLionel Sambuc p++;
1240d90bee97SLionel Sambuc *enddir++ = *p++;
1241d90bee97SLionel Sambuc }
1242d90bee97SLionel Sambuc }
1243d90bee97SLionel Sambuc if (enddir == expdir) {
1244d90bee97SLionel Sambuc cp = ".";
1245d90bee97SLionel Sambuc } else if (enddir == expdir + 1 && *expdir == '/') {
1246d90bee97SLionel Sambuc cp = "/";
1247d90bee97SLionel Sambuc } else {
1248d90bee97SLionel Sambuc cp = expdir;
1249d90bee97SLionel Sambuc enddir[-1] = '\0';
1250d90bee97SLionel Sambuc }
1251d90bee97SLionel Sambuc if ((dirp = opendir(cp)) == NULL)
1252d90bee97SLionel Sambuc return;
1253d90bee97SLionel Sambuc if (enddir != expdir)
1254d90bee97SLionel Sambuc enddir[-1] = '/';
1255d90bee97SLionel Sambuc if (*endname == 0) {
1256d90bee97SLionel Sambuc atend = 1;
1257d90bee97SLionel Sambuc } else {
1258d90bee97SLionel Sambuc atend = 0;
1259d90bee97SLionel Sambuc *endname++ = '\0';
1260d90bee97SLionel Sambuc }
1261d90bee97SLionel Sambuc matchdot = 0;
1262d90bee97SLionel Sambuc p = start;
1263d90bee97SLionel Sambuc while (*p == CTLQUOTEMARK)
1264d90bee97SLionel Sambuc p++;
1265d90bee97SLionel Sambuc if (*p == CTLESC)
1266d90bee97SLionel Sambuc p++;
1267d90bee97SLionel Sambuc if (*p == '.')
1268d90bee97SLionel Sambuc matchdot++;
1269d90bee97SLionel Sambuc while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1270d90bee97SLionel Sambuc if (dp->d_name[0] == '.' && ! matchdot)
1271d90bee97SLionel Sambuc continue;
1272d90bee97SLionel Sambuc if (patmatch(start, dp->d_name, 0)) {
1273d90bee97SLionel Sambuc if (atend) {
1274d90bee97SLionel Sambuc scopy(dp->d_name, enddir);
1275d90bee97SLionel Sambuc addfname(expdir);
1276d90bee97SLionel Sambuc } else {
1277d90bee97SLionel Sambuc for (p = enddir, cp = dp->d_name;
1278d90bee97SLionel Sambuc (*p++ = *cp++) != '\0';)
1279d90bee97SLionel Sambuc continue;
1280d90bee97SLionel Sambuc p[-1] = '/';
1281d90bee97SLionel Sambuc expmeta(p, endname);
1282d90bee97SLionel Sambuc }
1283d90bee97SLionel Sambuc }
1284d90bee97SLionel Sambuc }
1285d90bee97SLionel Sambuc closedir(dirp);
1286d90bee97SLionel Sambuc if (! atend)
1287d90bee97SLionel Sambuc endname[-1] = '/';
1288d90bee97SLionel Sambuc }
1289d90bee97SLionel Sambuc
1290d90bee97SLionel Sambuc
1291d90bee97SLionel Sambuc /*
1292d90bee97SLionel Sambuc * Add a file name to the list.
1293d90bee97SLionel Sambuc */
1294d90bee97SLionel Sambuc
1295d90bee97SLionel Sambuc STATIC void
addfname(char * name)1296d90bee97SLionel Sambuc addfname(char *name)
1297d90bee97SLionel Sambuc {
1298d90bee97SLionel Sambuc char *p;
1299d90bee97SLionel Sambuc struct strlist *sp;
1300d90bee97SLionel Sambuc
1301d90bee97SLionel Sambuc p = stalloc(strlen(name) + 1);
1302d90bee97SLionel Sambuc scopy(name, p);
1303d90bee97SLionel Sambuc sp = (struct strlist *)stalloc(sizeof *sp);
1304d90bee97SLionel Sambuc sp->text = p;
1305d90bee97SLionel Sambuc *exparg.lastp = sp;
1306d90bee97SLionel Sambuc exparg.lastp = &sp->next;
1307d90bee97SLionel Sambuc }
1308d90bee97SLionel Sambuc
1309d90bee97SLionel Sambuc
1310d90bee97SLionel Sambuc /*
1311d90bee97SLionel Sambuc * Sort the results of file name expansion. It calculates the number of
1312d90bee97SLionel Sambuc * strings to sort and then calls msort (short for merge sort) to do the
1313d90bee97SLionel Sambuc * work.
1314d90bee97SLionel Sambuc */
1315d90bee97SLionel Sambuc
1316d90bee97SLionel Sambuc STATIC struct strlist *
expsort(struct strlist * str)1317d90bee97SLionel Sambuc expsort(struct strlist *str)
1318d90bee97SLionel Sambuc {
1319d90bee97SLionel Sambuc int len;
1320d90bee97SLionel Sambuc struct strlist *sp;
1321d90bee97SLionel Sambuc
1322d90bee97SLionel Sambuc len = 0;
1323d90bee97SLionel Sambuc for (sp = str ; sp ; sp = sp->next)
1324d90bee97SLionel Sambuc len++;
1325d90bee97SLionel Sambuc return msort(str, len);
1326d90bee97SLionel Sambuc }
1327d90bee97SLionel Sambuc
1328d90bee97SLionel Sambuc
1329d90bee97SLionel Sambuc STATIC struct strlist *
msort(struct strlist * list,int len)1330d90bee97SLionel Sambuc msort(struct strlist *list, int len)
1331d90bee97SLionel Sambuc {
1332d90bee97SLionel Sambuc struct strlist *p, *q = NULL;
1333d90bee97SLionel Sambuc struct strlist **lpp;
1334d90bee97SLionel Sambuc int half;
1335d90bee97SLionel Sambuc int n;
1336d90bee97SLionel Sambuc
1337d90bee97SLionel Sambuc if (len <= 1)
1338d90bee97SLionel Sambuc return list;
1339d90bee97SLionel Sambuc half = len >> 1;
1340d90bee97SLionel Sambuc p = list;
1341d90bee97SLionel Sambuc for (n = half ; --n >= 0 ; ) {
1342d90bee97SLionel Sambuc q = p;
1343d90bee97SLionel Sambuc p = p->next;
1344d90bee97SLionel Sambuc }
1345d90bee97SLionel Sambuc q->next = NULL; /* terminate first half of list */
1346d90bee97SLionel Sambuc q = msort(list, half); /* sort first half of list */
1347d90bee97SLionel Sambuc p = msort(p, len - half); /* sort second half */
1348d90bee97SLionel Sambuc lpp = &list;
1349d90bee97SLionel Sambuc for (;;) {
1350d90bee97SLionel Sambuc if (strcmp(p->text, q->text) < 0) {
1351d90bee97SLionel Sambuc *lpp = p;
1352d90bee97SLionel Sambuc lpp = &p->next;
1353d90bee97SLionel Sambuc if ((p = *lpp) == NULL) {
1354d90bee97SLionel Sambuc *lpp = q;
1355d90bee97SLionel Sambuc break;
1356d90bee97SLionel Sambuc }
1357d90bee97SLionel Sambuc } else {
1358d90bee97SLionel Sambuc *lpp = q;
1359d90bee97SLionel Sambuc lpp = &q->next;
1360d90bee97SLionel Sambuc if ((q = *lpp) == NULL) {
1361d90bee97SLionel Sambuc *lpp = p;
1362d90bee97SLionel Sambuc break;
1363d90bee97SLionel Sambuc }
1364d90bee97SLionel Sambuc }
1365d90bee97SLionel Sambuc }
1366d90bee97SLionel Sambuc return list;
1367d90bee97SLionel Sambuc }
1368d90bee97SLionel Sambuc
1369d90bee97SLionel Sambuc
1370*0a6a1f1dSLionel Sambuc /*
1371*0a6a1f1dSLionel Sambuc * See if a character matches a character class, starting at the first colon
1372*0a6a1f1dSLionel Sambuc * of "[:class:]".
1373*0a6a1f1dSLionel Sambuc * If a valid character class is recognized, a pointer to the next character
1374*0a6a1f1dSLionel Sambuc * after the final closing bracket is stored into *end, otherwise a null
1375*0a6a1f1dSLionel Sambuc * pointer is stored into *end.
1376*0a6a1f1dSLionel Sambuc */
1377*0a6a1f1dSLionel Sambuc static int
match_charclass(char * p,wchar_t chr,char ** end)1378*0a6a1f1dSLionel Sambuc match_charclass(char *p, wchar_t chr, char **end)
1379*0a6a1f1dSLionel Sambuc {
1380*0a6a1f1dSLionel Sambuc char name[20];
1381*0a6a1f1dSLionel Sambuc char *nameend;
1382*0a6a1f1dSLionel Sambuc wctype_t cclass;
1383*0a6a1f1dSLionel Sambuc
1384*0a6a1f1dSLionel Sambuc *end = NULL;
1385*0a6a1f1dSLionel Sambuc p++;
1386*0a6a1f1dSLionel Sambuc nameend = strstr(p, ":]");
1387*0a6a1f1dSLionel Sambuc if (nameend == NULL || (size_t)(nameend - p) >= sizeof(name) ||
1388*0a6a1f1dSLionel Sambuc nameend == p)
1389*0a6a1f1dSLionel Sambuc return 0;
1390*0a6a1f1dSLionel Sambuc memcpy(name, p, nameend - p);
1391*0a6a1f1dSLionel Sambuc name[nameend - p] = '\0';
1392*0a6a1f1dSLionel Sambuc *end = nameend + 2;
1393*0a6a1f1dSLionel Sambuc cclass = wctype(name);
1394*0a6a1f1dSLionel Sambuc /* An unknown class matches nothing but is valid nevertheless. */
1395*0a6a1f1dSLionel Sambuc if (cclass == 0)
1396*0a6a1f1dSLionel Sambuc return 0;
1397*0a6a1f1dSLionel Sambuc return iswctype(chr, cclass);
1398*0a6a1f1dSLionel Sambuc }
1399*0a6a1f1dSLionel Sambuc
1400*0a6a1f1dSLionel Sambuc
1401d90bee97SLionel Sambuc
1402d90bee97SLionel Sambuc /*
1403d90bee97SLionel Sambuc * Returns true if the pattern matches the string.
1404d90bee97SLionel Sambuc */
1405d90bee97SLionel Sambuc
1406d90bee97SLionel Sambuc int
patmatch(char * pattern,char * string,int squoted)1407d90bee97SLionel Sambuc patmatch(char *pattern, char *string, int squoted)
1408d90bee97SLionel Sambuc {
1409d90bee97SLionel Sambuc #ifdef notdef
1410d90bee97SLionel Sambuc if (pattern[0] == '!' && pattern[1] == '!')
1411d90bee97SLionel Sambuc return 1 - pmatch(pattern + 2, string);
1412d90bee97SLionel Sambuc else
1413d90bee97SLionel Sambuc #endif
1414d90bee97SLionel Sambuc return pmatch(pattern, string, squoted);
1415d90bee97SLionel Sambuc }
1416d90bee97SLionel Sambuc
1417d90bee97SLionel Sambuc
1418d90bee97SLionel Sambuc STATIC int
pmatch(char * pattern,char * string,int squoted)1419d90bee97SLionel Sambuc pmatch(char *pattern, char *string, int squoted)
1420d90bee97SLionel Sambuc {
1421*0a6a1f1dSLionel Sambuc char *p, *q, *end;
1422d90bee97SLionel Sambuc char c;
1423d90bee97SLionel Sambuc
1424d90bee97SLionel Sambuc p = pattern;
1425d90bee97SLionel Sambuc q = string;
1426d90bee97SLionel Sambuc for (;;) {
1427d90bee97SLionel Sambuc switch (c = *p++) {
1428d90bee97SLionel Sambuc case '\0':
1429d90bee97SLionel Sambuc goto breakloop;
1430d90bee97SLionel Sambuc case CTLESC:
1431d90bee97SLionel Sambuc if (squoted && *q == CTLESC)
1432d90bee97SLionel Sambuc q++;
1433d90bee97SLionel Sambuc if (*q++ != *p++)
1434d90bee97SLionel Sambuc return 0;
1435d90bee97SLionel Sambuc break;
1436d90bee97SLionel Sambuc case CTLQUOTEMARK:
1437d90bee97SLionel Sambuc continue;
1438d90bee97SLionel Sambuc case '?':
1439d90bee97SLionel Sambuc if (squoted && *q == CTLESC)
1440d90bee97SLionel Sambuc q++;
1441d90bee97SLionel Sambuc if (*q++ == '\0')
1442d90bee97SLionel Sambuc return 0;
1443d90bee97SLionel Sambuc break;
1444d90bee97SLionel Sambuc case '*':
1445d90bee97SLionel Sambuc c = *p;
1446d90bee97SLionel Sambuc while (c == CTLQUOTEMARK || c == '*')
1447d90bee97SLionel Sambuc c = *++p;
1448d90bee97SLionel Sambuc if (c != CTLESC && c != CTLQUOTEMARK &&
1449d90bee97SLionel Sambuc c != '?' && c != '*' && c != '[') {
1450d90bee97SLionel Sambuc while (*q != c) {
1451d90bee97SLionel Sambuc if (squoted && *q == CTLESC &&
1452d90bee97SLionel Sambuc q[1] == c)
1453d90bee97SLionel Sambuc break;
1454d90bee97SLionel Sambuc if (*q == '\0')
1455d90bee97SLionel Sambuc return 0;
1456d90bee97SLionel Sambuc if (squoted && *q == CTLESC)
1457d90bee97SLionel Sambuc q++;
1458d90bee97SLionel Sambuc q++;
1459d90bee97SLionel Sambuc }
1460d90bee97SLionel Sambuc }
1461d90bee97SLionel Sambuc do {
1462d90bee97SLionel Sambuc if (pmatch(p, q, squoted))
1463d90bee97SLionel Sambuc return 1;
1464d90bee97SLionel Sambuc if (squoted && *q == CTLESC)
1465d90bee97SLionel Sambuc q++;
1466d90bee97SLionel Sambuc } while (*q++ != '\0');
1467d90bee97SLionel Sambuc return 0;
1468d90bee97SLionel Sambuc case '[': {
1469d90bee97SLionel Sambuc char *endp;
1470d90bee97SLionel Sambuc int invert, found;
1471d90bee97SLionel Sambuc char chr;
1472d90bee97SLionel Sambuc
1473d90bee97SLionel Sambuc endp = p;
1474d90bee97SLionel Sambuc if (*endp == '!')
1475d90bee97SLionel Sambuc endp++;
1476d90bee97SLionel Sambuc for (;;) {
1477d90bee97SLionel Sambuc while (*endp == CTLQUOTEMARK)
1478d90bee97SLionel Sambuc endp++;
1479d90bee97SLionel Sambuc if (*endp == '\0')
1480d90bee97SLionel Sambuc goto dft; /* no matching ] */
1481d90bee97SLionel Sambuc if (*endp == CTLESC)
1482d90bee97SLionel Sambuc endp++;
1483d90bee97SLionel Sambuc if (*++endp == ']')
1484d90bee97SLionel Sambuc break;
1485d90bee97SLionel Sambuc }
1486d90bee97SLionel Sambuc invert = 0;
1487d90bee97SLionel Sambuc if (*p == '!') {
1488d90bee97SLionel Sambuc invert++;
1489d90bee97SLionel Sambuc p++;
1490d90bee97SLionel Sambuc }
1491d90bee97SLionel Sambuc found = 0;
1492d90bee97SLionel Sambuc chr = *q++;
1493d90bee97SLionel Sambuc if (squoted && chr == CTLESC)
1494d90bee97SLionel Sambuc chr = *q++;
1495d90bee97SLionel Sambuc if (chr == '\0')
1496d90bee97SLionel Sambuc return 0;
1497d90bee97SLionel Sambuc c = *p++;
1498d90bee97SLionel Sambuc do {
1499d90bee97SLionel Sambuc if (c == CTLQUOTEMARK)
1500d90bee97SLionel Sambuc continue;
1501*0a6a1f1dSLionel Sambuc if (c == '[' && *p == ':') {
1502*0a6a1f1dSLionel Sambuc found |= match_charclass(p, chr, &end);
1503*0a6a1f1dSLionel Sambuc if (end != NULL)
1504*0a6a1f1dSLionel Sambuc p = end;
1505*0a6a1f1dSLionel Sambuc }
1506d90bee97SLionel Sambuc if (c == CTLESC)
1507d90bee97SLionel Sambuc c = *p++;
1508d90bee97SLionel Sambuc if (*p == '-' && p[1] != ']') {
1509d90bee97SLionel Sambuc p++;
1510d90bee97SLionel Sambuc while (*p == CTLQUOTEMARK)
1511d90bee97SLionel Sambuc p++;
1512d90bee97SLionel Sambuc if (*p == CTLESC)
1513d90bee97SLionel Sambuc p++;
1514d90bee97SLionel Sambuc if (chr >= c && chr <= *p)
1515d90bee97SLionel Sambuc found = 1;
1516d90bee97SLionel Sambuc p++;
1517d90bee97SLionel Sambuc } else {
1518d90bee97SLionel Sambuc if (chr == c)
1519d90bee97SLionel Sambuc found = 1;
1520d90bee97SLionel Sambuc }
1521d90bee97SLionel Sambuc } while ((c = *p++) != ']');
1522d90bee97SLionel Sambuc if (found == invert)
1523d90bee97SLionel Sambuc return 0;
1524d90bee97SLionel Sambuc break;
1525d90bee97SLionel Sambuc }
1526d90bee97SLionel Sambuc dft: default:
1527d90bee97SLionel Sambuc if (squoted && *q == CTLESC)
1528d90bee97SLionel Sambuc q++;
1529d90bee97SLionel Sambuc if (*q++ != c)
1530d90bee97SLionel Sambuc return 0;
1531d90bee97SLionel Sambuc break;
1532d90bee97SLionel Sambuc }
1533d90bee97SLionel Sambuc }
1534d90bee97SLionel Sambuc breakloop:
1535d90bee97SLionel Sambuc if (*q != '\0')
1536d90bee97SLionel Sambuc return 0;
1537d90bee97SLionel Sambuc return 1;
1538d90bee97SLionel Sambuc }
1539d90bee97SLionel Sambuc
1540d90bee97SLionel Sambuc
1541d90bee97SLionel Sambuc
1542d90bee97SLionel Sambuc /*
1543d90bee97SLionel Sambuc * Remove any CTLESC characters from a string.
1544d90bee97SLionel Sambuc */
1545d90bee97SLionel Sambuc
1546d90bee97SLionel Sambuc void
rmescapes(char * str)1547d90bee97SLionel Sambuc rmescapes(char *str)
1548d90bee97SLionel Sambuc {
1549d90bee97SLionel Sambuc char *p, *q;
1550d90bee97SLionel Sambuc
1551d90bee97SLionel Sambuc p = str;
1552d90bee97SLionel Sambuc while (*p != CTLESC && *p != CTLQUOTEMARK) {
1553d90bee97SLionel Sambuc if (*p++ == '\0')
1554d90bee97SLionel Sambuc return;
1555d90bee97SLionel Sambuc }
1556d90bee97SLionel Sambuc q = p;
1557d90bee97SLionel Sambuc while (*p) {
1558d90bee97SLionel Sambuc if (*p == CTLQUOTEMARK) {
1559d90bee97SLionel Sambuc p++;
1560d90bee97SLionel Sambuc continue;
1561d90bee97SLionel Sambuc }
1562d90bee97SLionel Sambuc if (*p == CTLESC)
1563d90bee97SLionel Sambuc p++;
1564d90bee97SLionel Sambuc *q++ = *p++;
1565d90bee97SLionel Sambuc }
1566d90bee97SLionel Sambuc *q = '\0';
1567d90bee97SLionel Sambuc }
1568d90bee97SLionel Sambuc
1569d90bee97SLionel Sambuc
1570d90bee97SLionel Sambuc
1571d90bee97SLionel Sambuc /*
1572d90bee97SLionel Sambuc * See if a pattern matches in a case statement.
1573d90bee97SLionel Sambuc */
1574d90bee97SLionel Sambuc
1575d90bee97SLionel Sambuc int
casematch(union node * pattern,char * val)1576d90bee97SLionel Sambuc casematch(union node *pattern, char *val)
1577d90bee97SLionel Sambuc {
1578d90bee97SLionel Sambuc struct stackmark smark;
1579d90bee97SLionel Sambuc int result;
1580d90bee97SLionel Sambuc char *p;
1581d90bee97SLionel Sambuc
1582d90bee97SLionel Sambuc setstackmark(&smark);
1583d90bee97SLionel Sambuc argbackq = pattern->narg.backquote;
1584d90bee97SLionel Sambuc STARTSTACKSTR(expdest);
1585d90bee97SLionel Sambuc ifslastp = NULL;
1586d90bee97SLionel Sambuc argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1587d90bee97SLionel Sambuc STPUTC('\0', expdest);
1588d90bee97SLionel Sambuc p = grabstackstr(expdest);
1589d90bee97SLionel Sambuc result = patmatch(p, val, 0);
1590d90bee97SLionel Sambuc popstackmark(&smark);
1591d90bee97SLionel Sambuc return result;
1592d90bee97SLionel Sambuc }
1593d90bee97SLionel Sambuc
1594d90bee97SLionel Sambuc /*
1595d90bee97SLionel Sambuc * Our own itoa().
1596d90bee97SLionel Sambuc */
1597d90bee97SLionel Sambuc
1598d90bee97SLionel Sambuc STATIC char *
cvtnum(int num,char * buf)1599d90bee97SLionel Sambuc cvtnum(int num, char *buf)
1600d90bee97SLionel Sambuc {
1601d90bee97SLionel Sambuc char temp[32];
1602d90bee97SLionel Sambuc int neg = num < 0;
1603d90bee97SLionel Sambuc char *p = temp + 31;
1604d90bee97SLionel Sambuc
1605d90bee97SLionel Sambuc temp[31] = '\0';
1606d90bee97SLionel Sambuc
1607d90bee97SLionel Sambuc do {
1608d90bee97SLionel Sambuc *--p = num % 10 + '0';
1609d90bee97SLionel Sambuc } while ((num /= 10) != 0);
1610d90bee97SLionel Sambuc
1611d90bee97SLionel Sambuc if (neg)
1612d90bee97SLionel Sambuc *--p = '-';
1613d90bee97SLionel Sambuc
1614d90bee97SLionel Sambuc while (*p)
1615d90bee97SLionel Sambuc STPUTC(*p++, buf);
1616d90bee97SLionel Sambuc return buf;
1617d90bee97SLionel Sambuc }
1618d90bee97SLionel Sambuc
1619d90bee97SLionel Sambuc /*
1620d90bee97SLionel Sambuc * Do most of the work for wordexp(3).
1621d90bee97SLionel Sambuc */
1622d90bee97SLionel Sambuc
1623d90bee97SLionel Sambuc int
wordexpcmd(int argc,char ** argv)1624d90bee97SLionel Sambuc wordexpcmd(int argc, char **argv)
1625d90bee97SLionel Sambuc {
1626d90bee97SLionel Sambuc size_t len;
1627d90bee97SLionel Sambuc int i;
1628d90bee97SLionel Sambuc
1629d90bee97SLionel Sambuc out1fmt("%d", argc - 1);
1630d90bee97SLionel Sambuc out1c('\0');
1631d90bee97SLionel Sambuc for (i = 1, len = 0; i < argc; i++)
1632d90bee97SLionel Sambuc len += strlen(argv[i]);
1633d90bee97SLionel Sambuc out1fmt("%zu", len);
1634d90bee97SLionel Sambuc out1c('\0');
1635d90bee97SLionel Sambuc for (i = 1; i < argc; i++) {
1636d90bee97SLionel Sambuc out1str(argv[i]);
1637d90bee97SLionel Sambuc out1c('\0');
1638d90bee97SLionel Sambuc }
1639d90bee97SLionel Sambuc return (0);
1640d90bee97SLionel Sambuc }
1641