xref: /netbsd-src/bin/sh/expand.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: expand.c,v 1.141 2021/11/22 05:17:43 kre Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kenneth Almquist.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)expand.c	8.5 (Berkeley) 5/15/95";
39 #else
40 __RCSID("$NetBSD: expand.c,v 1.141 2021/11/22 05:17:43 kre Exp $");
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <sys/stat.h>
47 #include <errno.h>
48 #include <dirent.h>
49 #include <unistd.h>
50 #include <pwd.h>
51 #include <limits.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <wctype.h>
55 #include <wchar.h>
56 
57 /*
58  * Routines to expand arguments to commands.  We have to deal with
59  * backquotes, shell variables, and file metacharacters.
60  */
61 
62 #include "shell.h"
63 #include "main.h"
64 #include "nodes.h"
65 #include "eval.h"
66 #include "expand.h"
67 #include "syntax.h"
68 #include "arithmetic.h"
69 #include "parser.h"
70 #include "jobs.h"
71 #include "options.h"
72 #include "builtins.h"
73 #include "var.h"
74 #include "input.h"
75 #include "output.h"
76 #include "memalloc.h"
77 #include "error.h"
78 #include "mystring.h"
79 #include "show.h"
80 
81 /*
82  * Structure specifying which parts of the string should be searched
83  * for IFS characters.
84  */
85 
86 struct ifsregion {
87 	struct ifsregion *next;	/* next region in list */
88 	int begoff;		/* offset of start of region */
89 	int endoff;		/* offset of end of region */
90 	int inquotes;		/* search for nul bytes only */
91 };
92 
93 
94 char *expdest;			/* output of current string */
95 struct nodelist *argbackq;	/* list of back quote expressions */
96 struct ifsregion ifsfirst;	/* first struct in list of ifs regions */
97 struct ifsregion *ifslastp;	/* last struct in list */
98 struct arglist exparg;		/* holds expanded arg list */
99 
100 static int empty_dollar_at;	/* have expanded "$@" to nothing */
101 
102 STATIC const char *argstr(const char *, int);
103 STATIC const char *exptilde(const char *, int);
104 STATIC void expbackq(union node *, int, int);
105 STATIC const char *expari(const char *);
106 STATIC int subevalvar(const char *, const char *, int, int, int);
107 STATIC int subevalvar_trim(const char *, int, int, int, int, int);
108 STATIC const char *evalvar(const char *, int);
109 STATIC int varisset(const char *, int);
110 STATIC void varvalue(const char *, int, int, int);
111 STATIC void recordregion(int, int, int);
112 STATIC void removerecordregions(int);
113 STATIC void ifsbreakup(char *, struct arglist *);
114 STATIC void ifsfree(void);
115 STATIC void expandmeta(struct strlist *, int);
116 STATIC void expmeta(char *, char *);
117 STATIC void addfname(char *);
118 STATIC struct strlist *expsort(struct strlist *);
119 STATIC struct strlist *msort(struct strlist *, int);
120 STATIC int patmatch(const char *, const char *, int);
121 STATIC char *cvtnum(int, char *);
122 static int collate_range_cmp(wchar_t, wchar_t);
123 STATIC void add_args(struct strlist *);
124 STATIC void rmescapes_nl(char *);
125 
126 #ifdef	DEBUG
127 #define	NULLTERM_4_TRACE(p)	STACKSTRNUL(p)
128 #else
129 #define	NULLTERM_4_TRACE(p)	do { /* nothing */ } while (0)
130 #endif
131 
132 #define	IS_BORING(_ch)						\
133 	((_ch) == CTLQUOTEMARK || (_ch) == CTLQUOTEEND || (_ch) == CTLNONL)
134 #define	SKIP_BORING(p)						\
135 	do {							\
136 		char _ch;					\
137 								\
138 		while ((_ch = *(p)), IS_BORING(_ch))		\
139 			(p)++;					\
140 	} while (0)
141 
142 /*
143  * Expand shell variables and backquotes inside a here document.
144  */
145 
146 char *
147 expandhere(union node *arg)
148 {
149 	int len;
150 
151 	VTRACE(DBG_EXPAND|DBG_REDIR, ("expandhere(%p)\n", arg));
152 	expandarg(arg, NULL, 0);
153 	len = rmescapes(stackblock());
154 	VTRACE(DBG_EXPAND|DBG_REDIR, ("expandhere() -> %d\n", len));
155 	return stalloc(len + 1);	/* include the \0 */
156 }
157 
158 
159 static int
160 collate_range_cmp(wchar_t c1, wchar_t c2)
161 {
162 	wchar_t s1[2], s2[2];
163 
164 	s1[0] = c1;
165 	s1[1] = L'\0';
166 	s2[0] = c2;
167 	s2[1] = L'\0';
168 	return (wcscoll(s1, s2));
169 }
170 
171 /*
172  * Perform variable substitution and command substitution on an argument,
173  * placing the resulting list of arguments in arglist.  If EXP_FULL is true,
174  * perform splitting and file name expansion.  When arglist is NULL, perform
175  * here document expansion.
176  */
177 
178 void
179 expandarg(union node *arg, struct arglist *arglist, int flag)
180 {
181 	struct strlist *sp;
182 	char *p;
183 
184 	CTRACE(DBG_EXPAND, ("expandarg(fl=%#x)\n", flag));
185 	if (fflag)		/* no filename expandsion */
186 		flag &= ~EXP_GLOB;
187 
188 	empty_dollar_at = 0;
189 	argbackq = arg->narg.backquote;
190 	STARTSTACKSTR(expdest);
191 	ifsfirst.next = NULL;
192 	ifslastp = NULL;
193 	line_number = arg->narg.lineno;
194 	argstr(arg->narg.text, flag);
195 	if (arglist == NULL) {
196 		STACKSTRNUL(expdest);
197 		CTRACE(DBG_EXPAND,
198 		    ("expandarg: no arglist, done[%d] (len %d) \"%s\"\n",
199 		    back_exitstatus, expdest - stackblock(), stackblock()));
200 		return;			/* here document expanded */
201 	}
202 	STPUTC('\0', expdest);
203 	CTRACE(DBG_EXPAND, ("expandarg: arglist got (%d) \"%s\"\n",
204 		    expdest - stackblock() - 1, stackblock()));
205 	p = grabstackstr(expdest);
206 	exparg.lastp = &exparg.list;
207 	/*
208 	 * TODO - EXP_REDIR
209 	 */
210 	if (flag & EXP_SPLIT) {
211 		ifsbreakup(p, &exparg);
212 		*exparg.lastp = NULL;
213 		exparg.lastp = &exparg.list;
214 		if (flag & EXP_GLOB)
215 			expandmeta(exparg.list, flag);
216 		else
217 			add_args(exparg.list);
218 #if 0
219 	} else if (flag & EXP_REDIR) {
220 		/* if EXP_REDIR ever happens, it happens here */
221 		/* for now just (below) remove escapes, and leave it alone */
222 #endif
223 	} else {
224 		rmescapes(p);	/* we might have escaped CTL bytes to remove */
225 		sp = stalloc(sizeof(*sp));
226 		sp->text = p;
227 		*exparg.lastp = sp;
228 		exparg.lastp = &sp->next;
229 	}
230 	ifsfree();
231 	*exparg.lastp = NULL;
232 	if (exparg.list) {
233 		*arglist->lastp = exparg.list;
234 		arglist->lastp = exparg.lastp;
235 	}
236 }
237 
238 
239 
240 /*
241  * Perform variable and command substitution.
242  * If EXP_GLOB is set, output CTLESC characters to allow for further processing.
243  * If EXP_SPLIT is set, remember location of result for later,
244  * Otherwise treat $@ like $* since no splitting will be performed.
245  */
246 
247 STATIC const char *
248 argstr(const char *p, int flag)
249 {
250 	char c;
251 	const int quotes = flag & EXP_QNEEDED;		/* do CTLESC */
252 	int firsteq = 1;
253 	int had_dol_at = 0;
254 	int startoff;
255 	const char *ifs = NULL;
256 	int ifs_split = EXP_IFS_SPLIT;
257 
258 	if (flag & EXP_IFS_SPLIT)
259 		ifs = ifsval();
260 
261 	CTRACE(DBG_EXPAND, ("argstr(\"%s\", %#x) quotes=%#x\n", p,flag,quotes));
262 
263 	startoff = expdest - stackblock();
264 	if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
265 		p = exptilde(p, flag);
266 	for (;;) {
267 		switch (c = *p++) {
268 		case '\0':
269 			NULLTERM_4_TRACE(expdest);
270 			VTRACE(DBG_EXPAND, ("argstr returning at \"\" "
271 			   "added \"%s\" to expdest\n", stackblock()));
272 			return p - 1;
273 		case CTLENDVAR: /* end of expanding yyy in ${xxx-yyy} */
274 		case CTLENDARI: /* end of a $(( )) string */
275 			if (had_dol_at && *p == CTLQUOTEEND)
276 				p++;
277 			NULLTERM_4_TRACE(expdest);
278 			VTRACE(DBG_EXPAND, ("argstr returning at \"%.6s\"..."
279 			    " after %2.2X; added \"%s\" to expdest\n",
280 			    p, (c & 0xff), stackblock()));
281 			return p;
282 		case CTLQUOTEMARK:
283 			/* "$@" syntax adherence hack */
284 			if (p[0] == CTLVAR && p[1] & VSQUOTE &&
285 			    p[2] == '@' && p[3] == '=') {
286 				had_dol_at = 1;
287 				break;
288 			}
289 			had_dol_at = 0;
290 			empty_dollar_at = 0;
291 			if ((flag & EXP_SPLIT) != 0)
292 				STPUTC(c, expdest);
293 			ifs_split = 0;
294 			break;
295 		case CTLNONL:
296 			if (flag & EXP_NL)
297 				STPUTC(c, expdest);
298 			line_number++;
299 			break;
300 		case CTLCNL:
301 			STPUTC('\n', expdest);	/* no line_number++ */
302 			break;
303 		case CTLQUOTEEND:
304 			if (empty_dollar_at &&
305 			    expdest - stackblock() > startoff &&
306 			    expdest[-1] == CTLQUOTEMARK)
307 				expdest--;
308 			else if (!had_dol_at && (flag & EXP_SPLIT) != 0)
309 				STPUTC(c, expdest);
310 			ifs_split = EXP_IFS_SPLIT;
311 			had_dol_at = 0;
312 			break;
313 		case CTLESC:
314 			if (quotes || ISCTL(*p))
315 				STPUTC(c, expdest);
316 			c = *p++;
317 			STPUTC(c, expdest);
318 			if (c == '\n')		/* should not happen, but ... */
319 				line_number++;
320 			break;
321 		case CTLVAR: {
322 #ifdef DEBUG
323 			unsigned int pos = expdest - stackblock();
324 			NULLTERM_4_TRACE(expdest);
325 #endif
326 			p = evalvar(p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split));
327 			NULLTERM_4_TRACE(expdest);
328 			VTRACE(DBG_EXPAND, ("argstr evalvar "
329 			   "added %zd \"%s\" to expdest\n",
330 			   (size_t)(expdest - (stackblock() + pos)),
331 			   stackblock() + pos));
332 			break;
333 		}
334 		case CTLBACKQ:
335 		case CTLBACKQ|CTLQUOTE: {
336 #ifdef DEBUG
337 			unsigned int pos = expdest - stackblock();
338 #endif
339 			expbackq(argbackq->n, c & CTLQUOTE, flag);
340 			argbackq = argbackq->next;
341 			NULLTERM_4_TRACE(expdest);
342 			VTRACE(DBG_EXPAND, ("argstr expbackq added \"%s\" "
343 			   "to expdest\n", stackblock() + pos));
344 			break;
345 		}
346 		case CTLARI: {
347 #ifdef DEBUG
348 			unsigned int pos = expdest - stackblock();
349 #endif
350 			p = expari(p);
351 			NULLTERM_4_TRACE(expdest);
352 			VTRACE(DBG_EXPAND, ("argstr expari "
353 			   "+ \"%s\" to expdest p=\"%.5s...\"\n",
354 			   stackblock() + pos, p));
355 			break;
356 		}
357 		case ':':
358 		case '=':
359 			/*
360 			 * sort of a hack - expand tildes in variable
361 			 * assignments (after the first '=' and after ':'s).
362 			 */
363 			STPUTC(c, expdest);
364 			if (flag & EXP_VARTILDE && *p == '~') {
365 				if (c == '=') {
366 					if (firsteq)
367 						firsteq = 0;
368 					else
369 						break;
370 				}
371 				p = exptilde(p, flag);
372 			}
373 			break;
374 		default:
375 			if (c == '\n')
376 				line_number++;
377 			STPUTC(c, expdest);
378 			if (flag & ifs_split && strchr(ifs, c) != NULL) {
379 				/* We need to get the output split here... */
380 				recordregion(expdest - stackblock() - 1,
381 						expdest - stackblock(), 0);
382 			}
383 			break;
384 		}
385 	}
386 }
387 
388 STATIC const char *
389 exptilde(const char *p, int flag)
390 {
391 	char c;
392 	const char *startp = p;
393 	struct passwd *pw;
394 	const char *home;
395 	const int quotes = flag & EXP_QNEEDED;
396 	char *user;
397 	struct stackmark smark;
398 #ifdef DEBUG
399 	unsigned int offs = expdest - stackblock();
400 #endif
401 
402 	setstackmark(&smark);
403 	(void) grabstackstr(expdest);
404 	user = stackblock();		/* we will just borrow top of stack */
405 
406 	while ((c = *++p) != '\0') {
407 		switch(c) {
408 		case CTLESC:		/* any of these occurring */
409 		case CTLVAR:		/* means ~ expansion */
410 		case CTLBACKQ:		/* does not happen at all */
411 		case CTLBACKQ | CTLQUOTE:
412 		case CTLARI:		/* just leave original unchanged */
413 		case CTLENDARI:
414 		case CTLQUOTEMARK:
415 		case '\n':
416 			popstackmark(&smark);
417 			return (startp);
418 		case CTLNONL:
419 			continue;
420 		case ':':
421 			if (!posix || flag & EXP_VARTILDE)
422 				goto done;
423 			break;
424 		case CTLENDVAR:
425 		case '/':
426 			goto done;
427 		}
428 		STPUTC(c, user);
429 	}
430  done:
431 	STACKSTRNUL(user);
432 	user = stackblock();		/* to start of collected username */
433 
434 	CTRACE(DBG_EXPAND, ("exptilde, found \"~%s\"", user));
435 	if (*user == '\0') {
436 		home = lookupvar("HOME");
437 		/*
438 		 * if HOME is unset, results are unspecified...
439 		 * we used to just leave the ~ unchanged, but
440 		 * (some) other shells do ... and this seems more useful.
441 		 */
442 		if (home == NULL && (pw = getpwuid(getuid())) != NULL)
443 			home = pw->pw_dir;
444 	} else if ((pw = getpwnam(user)) == NULL) {
445 		/*
446 		 * If user does not exist, results are undefined.
447 		 * so we can abort() here if we want, but let's not!
448 		 */
449 		home = NULL;
450 	} else
451 		home = pw->pw_dir;
452 
453 	VTRACE(DBG_EXPAND, (" ->\"%s\"", home ? home : "<<NULL>>"));
454 	popstackmark(&smark);	/* now expdest is valid again */
455 
456 	/*
457 	 * Posix XCU 2.6.1: The value of $HOME (for ~) or the initial
458 	 *		working directory from getpwnam() for ~user
459 	 * Nothing there about "except if a null string".  So do what it wants.
460 	 */
461 	if (home == NULL /* || *home == '\0' */) {
462 		CTRACE(DBG_EXPAND, (": returning unused \"%s\"\n", startp));
463 		return startp;
464 	} while ((c = *home++) != '\0') {
465 		if ((quotes && NEEDESC(c)) || ISCTL(c))
466 			STPUTC(CTLESC, expdest);
467 		STPUTC(c, expdest);
468 	}
469 	CTRACE(DBG_EXPAND, (": added %d \"%.*s\" returning \"%s\"\n",
470 	      expdest - stackblock() - offs, expdest - stackblock() - offs,
471 	      stackblock() + offs, p));
472 
473 	return (p);
474 }
475 
476 
477 STATIC void
478 removerecordregions(int endoff)
479 {
480 
481 	VTRACE(DBG_EXPAND, ("removerecordregions(%d):", endoff));
482 	if (ifslastp == NULL) {
483 		VTRACE(DBG_EXPAND, (" none\n", endoff));
484 		return;
485 	}
486 
487 	if (ifsfirst.endoff > endoff) {
488 		VTRACE(DBG_EXPAND, (" first(%d)", ifsfirst.endoff));
489 		while (ifsfirst.next != NULL) {
490 			struct ifsregion *ifsp;
491 			INTOFF;
492 			ifsp = ifsfirst.next->next;
493 			ckfree(ifsfirst.next);
494 			ifsfirst.next = ifsp;
495 			INTON;
496 		}
497 		if (ifsfirst.begoff > endoff)
498 			ifslastp = NULL;
499 		else {
500 			VTRACE(DBG_EXPAND,("->(%d,%d)",ifsfirst.begoff,endoff));
501 			ifslastp = &ifsfirst;
502 			ifsfirst.endoff = endoff;
503 		}
504 		VTRACE(DBG_EXPAND, ("\n"));
505 		return;
506 	}
507 
508 	ifslastp = &ifsfirst;
509 	while (ifslastp->next && ifslastp->next->begoff < endoff)
510 		ifslastp=ifslastp->next;
511 	VTRACE(DBG_EXPAND, (" found(%d,%d)", ifslastp->begoff,ifslastp->endoff));
512 	while (ifslastp->next != NULL) {
513 		struct ifsregion *ifsp;
514 		INTOFF;
515 		ifsp = ifslastp->next->next;
516 		ckfree(ifslastp->next);
517 		ifslastp->next = ifsp;
518 		INTON;
519 	}
520 	if (ifslastp->endoff > endoff)
521 		ifslastp->endoff = endoff;
522 	VTRACE(DBG_EXPAND, ("->(%d,%d)", ifslastp->begoff,ifslastp->endoff));
523 }
524 
525 
526 /*
527  * Expand arithmetic expression.
528  *
529  * In this incarnation, we start at the beginning (yes, "Let's start at the
530  * very beginning.  A very good place to start.") and collect the expression
531  * until the end - which means expanding anything contained within.
532  *
533  * Fortunately, argstr() just happens to do that for us...
534  */
535 STATIC const char *
536 expari(const char *p)
537 {
538 	char *q, *start;
539 	intmax_t result;
540 	int adjustment;
541 	int begoff;
542 	int quoted;
543 	struct stackmark smark;
544 
545 	/*	ifsfree(); */
546 
547 	/*
548 	 * SPACE_NEEDED is enough for all possible digits (rounded up)
549 	 * plus possible "-", and the terminating '\0', hence, plus 2
550 	 *
551 	 * The calculation produces the number of bytes needed to
552 	 * represent the biggest possible value, in octal.  We only
553 	 * generate decimal, which takes (often) less digits (never more)
554 	 * so this is safe, if occasionally slightly wasteful.
555 	 */
556 #define SPACE_NEEDED ((int)((sizeof(intmax_t) * CHAR_BIT + 2) / 3 + 2))
557 
558 	quoted = *p++ == '"';
559 	begoff = expdest - stackblock();
560 	VTRACE(DBG_EXPAND, ("expari%s: \"%s\" begoff %d\n",
561 	    quoted ? "(quoted)" : "", p, begoff));
562 
563 	p = argstr(p, EXP_NL);			/* expand $(( )) string */
564 	STPUTC('\0', expdest);
565 	start = stackblock() + begoff;
566 
567 	removerecordregions(begoff);		/* nothing there is kept */
568 	rmescapes_nl(start);		/* convert CRTNONL back into \n's */
569 
570 	setstackmark(&smark);
571 	q = grabstackstr(expdest);	/* keep the expression while eval'ing */
572 	result = arith(start, line_number);
573 	popstackmark(&smark);		/* return the stack to before grab */
574 
575 	start = stackblock() + begoff;		/* block may have moved */
576 	adjustment = expdest - start;
577 	STADJUST(-adjustment, expdest);		/* remove the argstr() result */
578 
579 	CHECKSTRSPACE(SPACE_NEEDED, expdest);	/* nb: stack block might move */
580 	fmtstr(expdest, SPACE_NEEDED, "%"PRIdMAX, result);
581 
582 	for (q = expdest; *q++ != '\0'; )	/* find end of what we added */
583 		;
584 
585 	if (quoted == 0)			/* allow weird splitting */
586 		recordregion(begoff, begoff + q - 1 - expdest, 0);
587 	adjustment = q - expdest - 1;
588 	STADJUST(adjustment, expdest);		/* move expdest to end */
589 	VTRACE(DBG_EXPAND, ("expari: adding %d \"%s\", returning \"%.5s...\"\n",
590 	    adjustment, stackblock() + begoff, p));
591 
592 	return p;
593 }
594 
595 
596 /*
597  * Expand stuff in backwards quotes (these days, any command substitution).
598  */
599 
600 STATIC void
601 expbackq(union node *cmd, int quoted, int flag)
602 {
603 	struct backcmd in;
604 	int i;
605 	char buf[128];
606 	char *p;
607 	char *dest = expdest;	/* expdest may be reused by eval, use an alt */
608 	struct ifsregion saveifs, *savelastp;
609 	struct nodelist *saveargbackq;
610 	char lastc;
611 	int startloc = dest - stackblock();
612 	int saveherefd;
613 	const int quotes = flag & EXP_QNEEDED;
614 	int nnl;
615 	struct stackmark smark;
616 
617 	VTRACE(DBG_EXPAND, ("expbackq( ..., q=%d flag=%#x) have %d\n",
618 	    quoted, flag, startloc));
619 	INTOFF;
620 	saveifs = ifsfirst;
621 	savelastp = ifslastp;
622 	saveargbackq = argbackq;
623 	saveherefd = herefd;
624 	herefd = -1;
625 
626 	setstackmark(&smark);	/* preserve the stack */
627 	p = grabstackstr(dest);	/* save what we have there currently */
628 	evalbackcmd(cmd, &in);	/* evaluate the $( ) tree (using stack) */
629 	popstackmark(&smark);	/* and return stack to when we entered */
630 
631 	ifsfirst = saveifs;
632 	ifslastp = savelastp;
633 	argbackq = saveargbackq;
634 	herefd = saveherefd;
635 
636 	p = in.buf;		/* now extract the results */
637 	nnl = 0;		/* dropping trailing \n's */
638 	for (;;) {
639 		if (--in.nleft < 0) {
640 			if (in.fd < 0)
641 				break;
642 			INTON;
643 			while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR)
644 				continue;
645 			INTOFF;
646 			VTRACE(DBG_EXPAND, ("expbackq: read returns %d\n", i));
647 			if (i <= 0)
648 				break;
649 			p = buf;
650 			in.nleft = i - 1;
651 		}
652 		lastc = *p++;
653 		if (lastc != '\0') {
654 			if (lastc == '\n')	/* don't save \n yet */
655 				nnl++;		/* it might be trailing */
656 			else {
657 				/*
658 				 * We have something other than \n
659 				 *
660 				 * Before saving it, we need to insert
661 				 * any \n's that we have just skipped.
662 				 */
663 
664 				/* XXX
665 				 * this hack is just because our
666 				 * CHECKSTRSPACE() is lazy, and only
667 				 * ever grows the stack once, even
668 				 * if that does not allocate the space
669 				 * we requested.  ie: safe for small
670 				 * requests, but not large ones.
671 				 * FIXME someday...
672 				 */
673 				if (nnl < 20) {
674 					CHECKSTRSPACE(nnl + 2, dest);
675 					while (nnl > 0) {
676 						nnl--;
677 						USTPUTC('\n', dest);
678 					}
679 				} else {
680 					/* The slower, safer, way */
681 					while (nnl > 0) {
682 						nnl--;
683 						STPUTC('\n', dest);
684 					}
685 					CHECKSTRSPACE(2, dest);
686 				}
687 				if ((quotes && quoted && NEEDESC(lastc)) ||
688 				    ISCTL(lastc))
689 					USTPUTC(CTLESC, dest);
690 				USTPUTC(lastc, dest);
691 			}
692 		}
693 	}
694 
695 	if (in.fd >= 0)
696 		close(in.fd);
697 	if (in.buf)
698 		ckfree(in.buf);
699 	if (in.jp)
700 		back_exitstatus = waitforjob(in.jp);
701 	if (quoted == 0)
702 		recordregion(startloc, dest - stackblock(), 0);
703 	CTRACE(DBG_EXPAND, ("evalbackq: [%d] size=%d: \"%.*s\"\n",
704 		back_exitstatus,
705 		(int)((dest - stackblock()) - startloc),
706 		(int)((dest - stackblock()) - startloc),
707 		stackblock() + startloc));
708 
709 	expdest = dest;		/* all done, expdest is all ours again */
710 	INTON;
711 }
712 
713 
714 STATIC int
715 subevalvar(const char *p, const char *str, int subtype, int startloc,
716     int varflags)
717 {
718 	char *startp;
719 	int saveherefd = herefd;
720 	struct nodelist *saveargbackq = argbackq;
721 	int amount;
722 
723 	herefd = -1;
724 	VTRACE(DBG_EXPAND, ("subevalvar(%d) \"%.20s\" ${%.*s} sloc=%d vf=%x\n",
725 	    subtype, p, p-str, str, startloc, varflags));
726 	argstr(p, subtype == VSASSIGN ? EXP_VARTILDE : EXP_TILDE);
727 	STACKSTRNUL(expdest);
728 	herefd = saveherefd;
729 	argbackq = saveargbackq;
730 	startp = stackblock() + startloc;
731 
732 	switch (subtype) {
733 	case VSASSIGN:
734 		setvar(str, startp, 0);
735 		amount = startp - expdest;	/* remove what argstr added */
736 		STADJUST(amount, expdest);
737 		varflags &= ~VSNUL;	/*XXX Huh?   What's that achieve? */
738 		return 1;			/* go back and eval var again */
739 
740 	case VSQUESTION:
741 		if (*p != CTLENDVAR) {
742 			outfmt(&errout, "%s\n", startp);
743 			error(NULL);
744 		}
745 		error("%.*s: parameter %snot set",
746 		      (int)(p - str - 1),
747 		      str, (varflags & VSNUL) ? "null or "
748 					      : nullstr);
749 		/* NOTREACHED */
750 
751 	default:
752 		abort();
753 	}
754 }
755 
756 STATIC int
757 subevalvar_trim(const char *p, int strloc, int subtype, int startloc,
758     int varflags, int quotes)
759 {
760 	char *startp;
761 	char *str;
762 	char *loc = NULL;
763 	char *q;
764 	int c = 0;
765 	int saveherefd = herefd;
766 	struct nodelist *saveargbackq = argbackq;
767 	int amount;
768 
769 	herefd = -1;
770 	switch (subtype) {
771 	case VSTRIMLEFT:
772 	case VSTRIMLEFTMAX:
773 	case VSTRIMRIGHT:
774 	case VSTRIMRIGHTMAX:
775 		break;
776 	default:
777 		abort();
778 		break;
779 	}
780 
781 	VTRACE(DBG_EXPAND,
782 	("subevalvar_trim(\"%.9s\", STR@%d, SUBT=%d, start@%d, vf=%x, q=%x)\n",
783 		p, strloc, subtype, startloc, varflags, quotes));
784 
785 	argstr(p, (varflags & (VSQUOTE|VSPATQ)) == VSQUOTE ? 0 : EXP_CASE);
786 	STACKSTRNUL(expdest);
787 	herefd = saveherefd;
788 	argbackq = saveargbackq;
789 	startp = stackblock() + startloc;
790 	str = stackblock() + strloc;
791 
792 	switch (subtype) {
793 
794 	case VSTRIMLEFT:
795 		for (loc = startp; loc < str; loc++) {
796 			c = *loc;
797 			*loc = '\0';
798 			if (patmatch(str, startp, quotes))
799 				goto recordleft;
800 			*loc = c;
801 			if (quotes && *loc == CTLESC)
802 				loc++;
803 		}
804 		return 0;
805 
806 	case VSTRIMLEFTMAX:
807 		for (loc = str - 1; loc >= startp;) {
808 			c = *loc;
809 			*loc = '\0';
810 			if (patmatch(str, startp, quotes))
811 				goto recordleft;
812 			*loc = c;
813 			loc--;
814 			if (quotes && loc > startp &&
815 			    *(loc - 1) == CTLESC) {
816 				for (q = startp; q < loc; q++)
817 					if (*q == CTLESC)
818 						q++;
819 				if (q > loc)
820 					loc--;
821 			}
822 		}
823 		return 0;
824 
825 	case VSTRIMRIGHT:
826 		for (loc = str - 1; loc >= startp;) {
827 			if (patmatch(str, loc, quotes))
828 				goto recordright;
829 			loc--;
830 			if (quotes && loc > startp &&
831 			    *(loc - 1) == CTLESC) {
832 				for (q = startp; q < loc; q++)
833 					if (*q == CTLESC)
834 						q++;
835 				if (q > loc)
836 					loc--;
837 			}
838 		}
839 		return 0;
840 
841 	case VSTRIMRIGHTMAX:
842 		for (loc = startp; loc < str - 1; loc++) {
843 			if (patmatch(str, loc, quotes))
844 				goto recordright;
845 			if (quotes && *loc == CTLESC)
846 				loc++;
847 		}
848 		return 0;
849 
850 	default:
851 		abort();
852 	}
853 
854  recordleft:
855 	*loc = c;
856 	amount = ((str - 1) - (loc - startp)) - expdest;
857 	STADJUST(amount, expdest);
858 	while (loc != str - 1)
859 		*startp++ = *loc++;
860 	return 1;
861 
862  recordright:
863 	amount = loc - expdest;
864 	STADJUST(amount, expdest);
865 	STPUTC('\0', expdest);
866 	STADJUST(-1, expdest);
867 	return 1;
868 }
869 
870 
871 /*
872  * Expand a variable, and return a pointer to the next character in the
873  * input string.
874  */
875 
876 STATIC const char *
877 evalvar(const char *p, int flag)
878 {
879 	int subtype;
880 	int varflags;
881 	const char *var;
882 	char *val;
883 	int patloc;
884 	int c;
885 	int set;
886 	int special;
887 	int startloc;
888 	int varlen;
889 	int apply_ifs;
890 	const int quotes = flag & EXP_QNEEDED;
891 
892 	varflags = (unsigned char)*p++;
893 	subtype = varflags & VSTYPE;
894 	var = p;
895 	special = !is_name(*p);
896 	p = strchr(p, '=') + 1;
897 
898 	CTRACE(DBG_EXPAND,
899 	    ("evalvar \"%.*s\", flag=%#X quotes=%#X vf=%#X subtype=%X\n",
900 	    p - var - 1, var, flag, quotes, varflags, subtype));
901 
902  again: /* jump here after setting a variable with ${var=text} */
903 	if (varflags & VSLINENO) {
904 		if (line_num.flags & VUNSET) {
905 			set = 0;
906 			val = NULL;
907 		} else {
908 			set = 1;
909 			special = p - var;
910 			val = NULL;
911 		}
912 	} else if (special) {
913 		set = varisset(var, varflags & VSNUL);
914 		val = NULL;
915 		if (!set && *var == '@')
916 			empty_dollar_at = 1;
917 	} else {
918 		val = lookupvar(var);
919 		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
920 			val = NULL;
921 			set = 0;
922 		} else
923 			set = 1;
924 	}
925 
926 	varlen = 0;
927 	startloc = expdest - stackblock();
928 
929 	if (!set && uflag && *var != '@' && *var != '*') {
930 		switch (subtype) {
931 		case VSNORMAL:
932 		case VSTRIMLEFT:
933 		case VSTRIMLEFTMAX:
934 		case VSTRIMRIGHT:
935 		case VSTRIMRIGHTMAX:
936 		case VSLENGTH:
937 			error("%.*s: parameter not set",
938 			    (int)(p - var - 1), var);
939 			/* NOTREACHED */
940 		}
941 	}
942 
943 #if 0		/* no longer need this $@ evil ... */
944 	if (!set && subtype != VSPLUS && special && *var == '@')
945 		if (startloc > 0 && expdest[-1] == CTLQUOTEMARK)
946 			expdest--, startloc--;
947 #endif
948 
949 	if (set && subtype != VSPLUS) {
950 		/* insert the value of the variable */
951 		if (special) {
952 			if (varflags & VSLINENO) {
953 				/*
954 				 * The LINENO hack (expansion part)
955 				 */
956 				while (--special > 0) {
957 /*						not needed, it is a number...
958 					if (quotes && NEEDESC(*var))
959 						STPUTC(CTLESC, expdest);
960 */
961 					STPUTC(*var++, expdest);
962 				}
963 			} else
964 				varvalue(var, varflags&VSQUOTE, subtype, flag);
965 			if (subtype == VSLENGTH) {
966 				varlen = expdest - stackblock() - startloc;
967 				STADJUST(-varlen, expdest);
968 			}
969 		} else {
970 
971 			if (subtype == VSLENGTH) {
972 				for (; *val; val++)
973 					varlen++;
974 			} else if (quotes && varflags & VSQUOTE) {
975 				/*
976 				 * If we are going to look for magic in the
977 				 * value (quotes is set) and the expansion
978 				 * occurs inside "" (VSQUOTE) then any char
979 				 * that has any potential special meaning
980 				 * needs to have that meaning suppressed,
981 				 * so supply a CTLESC prefix for it.
982 				 */
983 				for (; (c = *val) != '\0'; val++) {
984 					if (NEEDESC(c))
985 						STPUTC(CTLESC, expdest);
986 					STPUTC(c, expdest);
987 				}
988 			} else {
989 				/*
990 				 * We are going to rmescapes() later,
991 				 * so make sure that any data char that
992 				 * might be mistaken for one of our CTLxxx
993 				 * magic chars is protected ... always.
994 				 */
995 				for (; (c = *val) != '\0'; val++) {
996 					if (ISCTL(c))
997 						STPUTC(CTLESC, expdest);
998 					STPUTC(c, expdest);
999 				}
1000 			}
1001 		}
1002 	}
1003 
1004 
1005 	if (varflags & VSQUOTE) {
1006 		if  (*var == '@' && shellparam.nparam != 1)
1007 		    apply_ifs = 1;
1008 		else {
1009 		    /*
1010 		     * Mark so that we don't apply IFS if we recurse through
1011 		     * here expanding $bar from "${foo-$bar}".
1012 		     */
1013 		    flag |= EXP_IN_QUOTES;
1014 		    apply_ifs = 0;
1015 		}
1016 	} else if (flag & EXP_IN_QUOTES) {
1017 		apply_ifs = 0;
1018 	} else
1019 		apply_ifs = 1;
1020 
1021 	switch (subtype) {
1022 	case VSLENGTH:
1023 		expdest = cvtnum(varlen, expdest);
1024 		break;
1025 
1026 	case VSNORMAL:
1027 		break;
1028 
1029 	case VSPLUS:
1030 		set = !set;
1031 		/* FALLTHROUGH */
1032 	case VSMINUS:
1033 		if (!set) {
1034 			argstr(p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0));
1035 			/*
1036 			 * ${x-a b c} doesn't get split, but removing the
1037 			 * 'apply_ifs = 0' apparently breaks ${1+"$@"}..
1038 			 * ${x-'a b' c} should generate 2 args.
1039 			 */
1040 			if (*p != CTLENDVAR)
1041 			/* We should have marked stuff already */
1042 				apply_ifs = 0;
1043 		}
1044 		break;
1045 
1046 	case VSTRIMLEFT:
1047 	case VSTRIMLEFTMAX:
1048 	case VSTRIMRIGHT:
1049 	case VSTRIMRIGHTMAX:
1050 		if (!set) {
1051 			set = 1;  /* allow argbackq to be advanced if needed */
1052 			break;
1053 		}
1054 		/*
1055 		 * Terminate the string and start recording the pattern
1056 		 * right after it
1057 		 */
1058 		STPUTC('\0', expdest);
1059 		patloc = expdest - stackblock();
1060 		if (subevalvar_trim(p, patloc, subtype, startloc, varflags,
1061 		    quotes) == 0) {
1062 			int amount = (expdest - stackblock() - patloc) + 1;
1063 			STADJUST(-amount, expdest);
1064 		}
1065 		/* Remove any recorded regions beyond start of variable */
1066 		removerecordregions(startloc);
1067 		apply_ifs = 1;
1068 		break;
1069 
1070 	case VSASSIGN:
1071 	case VSQUESTION:
1072 		if (set)
1073 			break;
1074 		if (subevalvar(p, var, subtype, startloc, varflags)) {
1075 			/* if subevalvar() returns, it always returns 1 */
1076 
1077 			varflags &= ~VSNUL;
1078 			/*
1079 			 * Remove any recorded regions beyond
1080 			 * start of variable
1081 			 */
1082 			removerecordregions(startloc);
1083 			goto again;
1084 		}
1085 		apply_ifs = 0;		/* never executed */
1086 		break;
1087 
1088 	default:
1089 		abort();
1090 	}
1091 
1092 	if (apply_ifs)
1093 		recordregion(startloc, expdest - stackblock(),
1094 			     varflags & VSQUOTE);
1095 
1096 	if (subtype != VSNORMAL) {	/* skip to end of alternative */
1097 		int nesting = 1;
1098 		for (;;) {
1099 			if ((c = *p++) == CTLESC)
1100 				p++;
1101 			else if (c == CTLNONL)
1102 				;
1103 			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
1104 				if (set)
1105 					argbackq = argbackq->next;
1106 			} else if (c == CTLVAR) {
1107 				if ((*p++ & VSTYPE) != VSNORMAL)
1108 					nesting++;
1109 			} else if (c == CTLENDVAR) {
1110 				if (--nesting == 0)
1111 					break;
1112 			}
1113 		}
1114 	}
1115 	return p;
1116 }
1117 
1118 
1119 
1120 /*
1121  * Test whether a special parameter is set.
1122  */
1123 
1124 STATIC int
1125 varisset(const char *name, int nulok)
1126 {
1127 	if (*name == '!')
1128 		return backgndpid != -1;
1129 	else if (*name == '@' || *name == '*') {
1130 		if (*shellparam.p == NULL)
1131 			return 0;
1132 
1133 		if (nulok) {
1134 			char **av;
1135 
1136 			for (av = shellparam.p; *av; av++)
1137 				if (**av != '\0')
1138 					return 1;
1139 			return 0;
1140 		}
1141 	} else if (is_digit(*name)) {
1142 		char *ap;
1143 		long num;
1144 
1145 		/*
1146 		 * handle overflow sensibly (the *ap tests should never fail)
1147 		 */
1148 		errno = 0;
1149 		num = strtol(name, &ap, 10);
1150 		if (errno != 0 || (*ap != '\0' && *ap != '='))
1151 			return 0;
1152 
1153 		if (num == 0)
1154 			ap = arg0;
1155 		else if (num > shellparam.nparam)
1156 			return 0;
1157 		else
1158 			ap = shellparam.p[num - 1];
1159 
1160 		if (nulok && (ap == NULL || *ap == '\0'))
1161 			return 0;
1162 	}
1163 	return 1;
1164 }
1165 
1166 
1167 
1168 /*
1169  * Add the value of a specialized variable to the stack string.
1170  */
1171 
1172 STATIC void
1173 varvalue(const char *name, int quoted, int subtype, int flag)
1174 {
1175 	int num;
1176 	char *p;
1177 	int i;
1178 	int sep;
1179 	char **ap;
1180 #ifdef DEBUG
1181 	char *start = expdest;
1182 #endif
1183 
1184 	VTRACE(DBG_EXPAND, ("varvalue(%c%s, sub=%d, fl=%#x)", *name,
1185 	    quoted ? ", quoted" : "", subtype, flag));
1186 
1187 	if (subtype == VSLENGTH)	/* no magic required ... */
1188 		flag &= ~EXP_FULL;
1189 
1190 #define STRTODEST(p) \
1191 	do {\
1192 		if ((flag & EXP_QNEEDED) && quoted) { \
1193 			while (*p) { \
1194 				if (NEEDESC(*p)) \
1195 					STPUTC(CTLESC, expdest); \
1196 				STPUTC(*p++, expdest); \
1197 			} \
1198 		} else \
1199 			while (*p) { \
1200 				if (ISCTL(*p)) \
1201 					STPUTC(CTLESC, expdest); \
1202 				STPUTC(*p++, expdest); \
1203 			} \
1204 	} while (0)
1205 
1206 
1207 	switch (*name) {
1208 	case '$':
1209 		num = rootpid;
1210 		break;
1211 	case '?':
1212 		num = exitstatus;
1213 		break;
1214 	case '#':
1215 		num = shellparam.nparam;
1216 		break;
1217 	case '!':
1218 		num = backgndpid;
1219 		break;
1220 	case '-':
1221 		for (i = 0; i < option_flags; i++) {
1222 			if (optlist[optorder[i]].val)
1223 				STPUTC(optlist[optorder[i]].letter, expdest);
1224 		}
1225 		VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start));
1226 		return;
1227 	case '@':
1228 		if (flag & EXP_SPLIT && quoted) {
1229 			VTRACE(DBG_EXPAND, (": $@ split (%d)\n",
1230 			    shellparam.nparam));
1231 #if 0
1232 		/* GROSS HACK */
1233 			if (shellparam.nparam == 0 &&
1234 				expdest[-1] == CTLQUOTEMARK)
1235 					expdest--;
1236 		/* KCAH SSORG */
1237 #endif
1238 			if (shellparam.nparam == 0)
1239 				empty_dollar_at = 1;
1240 
1241 			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
1242 				if (*p == '\0') {
1243 					/* retain an explicit null string */
1244 					STPUTC(CTLQUOTEMARK, expdest);
1245 					STPUTC(CTLQUOTEEND, expdest);
1246 				} else
1247 					STRTODEST(p);
1248 				if (*ap)
1249 					/* A NUL separates args inside "" */
1250 					STPUTC('\0', expdest);
1251 			}
1252 			return;
1253 		}
1254 		/* fall through */
1255 	case '*':
1256 		sep = ifsval()[0];
1257 		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
1258 			STRTODEST(p);
1259 			if (!*ap)
1260 				break;
1261 			if (sep) {
1262 				if (quoted && (flag & EXP_QNEEDED) &&
1263 				    NEEDESC(sep))
1264 					STPUTC(CTLESC, expdest);
1265 				STPUTC(sep, expdest);
1266 			} else
1267 			    if ((flag & (EXP_SPLIT|EXP_IN_QUOTES)) == EXP_SPLIT
1268 			      && !quoted && **ap != '\0')
1269 				STPUTC('\0', expdest);
1270 		}
1271 		VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start));
1272 		return;
1273 	default:
1274 		if (is_digit(*name)) {
1275 			long lnum;
1276 
1277 			errno = 0;
1278 			lnum = strtol(name, &p, 10);
1279 			if (errno != 0 || (*p != '\0' && *p != '='))
1280 				return;
1281 
1282 			if (lnum == 0)
1283 				p = arg0;
1284 			else if (lnum > 0 && lnum <= shellparam.nparam)
1285 				p = shellparam.p[lnum - 1];
1286 			else
1287 				return;
1288 			STRTODEST(p);
1289 		}
1290 		VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start));
1291 		return;
1292 	}
1293 	/*
1294 	 * only the specials with an int value arrive here
1295 	 */
1296 	VTRACE(DBG_EXPAND, ("(%d)", num));
1297 	expdest = cvtnum(num, expdest);
1298 	VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start));
1299 }
1300 
1301 
1302 
1303 /*
1304  * Record the fact that we have to scan this region of the
1305  * string for IFS characters.
1306  */
1307 
1308 STATIC void
1309 recordregion(int start, int end, int inquotes)
1310 {
1311 	struct ifsregion *ifsp;
1312 
1313 	VTRACE(DBG_EXPAND, ("recordregion(%d,%d,%d)\n", start, end, inquotes));
1314 	if (ifslastp == NULL) {
1315 		ifsp = &ifsfirst;
1316 	} else {
1317 		if (ifslastp->endoff == start
1318 		    && ifslastp->inquotes == inquotes) {
1319 			/* extend previous area */
1320 			ifslastp->endoff = end;
1321 			return;
1322 		}
1323 		ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
1324 		ifslastp->next = ifsp;
1325 	}
1326 	ifslastp = ifsp;
1327 	ifslastp->next = NULL;
1328 	ifslastp->begoff = start;
1329 	ifslastp->endoff = end;
1330 	ifslastp->inquotes = inquotes;
1331 }
1332 
1333 
1334 
1335 /*
1336  * Break the argument string into pieces based upon IFS and add the
1337  * strings to the argument list.  The regions of the string to be
1338  * searched for IFS characters have been stored by recordregion.
1339  */
1340 STATIC void
1341 ifsbreakup(char *string, struct arglist *arglist)
1342 {
1343 	struct ifsregion *ifsp;
1344 	struct strlist *sp;
1345 	char *start;
1346 	char *p;
1347 	char *q;
1348 	const char *ifs;
1349 	const char *ifsspc;
1350 	int had_param_ch = 0;
1351 
1352 	start = string;
1353 
1354 	VTRACE(DBG_EXPAND, ("ifsbreakup(\"%s\")", string)); /* misses \0's */
1355 	if (ifslastp == NULL) {
1356 		/* Return entire argument, IFS doesn't apply to any of it */
1357 		VTRACE(DBG_EXPAND, ("no regions\n", string));
1358 		sp = stalloc(sizeof(*sp));
1359 		sp->text = start;
1360 		*arglist->lastp = sp;
1361 		arglist->lastp = &sp->next;
1362 		return;
1363 	}
1364 
1365 	ifs = ifsval();
1366 
1367 	for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
1368 		p = string + ifsp->begoff;
1369 		VTRACE(DBG_EXPAND, (" !%.*s!(%d)", ifsp->endoff-ifsp->begoff,
1370 		    p, ifsp->endoff-ifsp->begoff));
1371 		while (p < string + ifsp->endoff) {
1372 			had_param_ch = 1;
1373 			q = p;
1374 			if (IS_BORING(*p)) {
1375 				p++;
1376 				continue;
1377 			}
1378 			if (*p == CTLESC)
1379 				p++;
1380 			if (ifsp->inquotes) {
1381 				/* Only NULs (should be from "$@") end args */
1382 				if (*p != 0) {
1383 					p++;
1384 					continue;
1385 				}
1386 				ifsspc = NULL;
1387 				VTRACE(DBG_EXPAND, (" \\0 nxt:\"%s\" ", p));
1388 			} else {
1389 				if (!strchr(ifs, *p)) {
1390 					p++;
1391 					continue;
1392 				}
1393 				had_param_ch = 0;
1394 				ifsspc = strchr(" \t\n", *p);
1395 
1396 				/* Ignore IFS whitespace at start */
1397 				if (q == start && ifsspc != NULL) {
1398 					p++;
1399 					start = p;
1400 					continue;
1401 				}
1402 			}
1403 
1404 			/* Save this argument... */
1405 			*q = '\0';
1406 			VTRACE(DBG_EXPAND, ("<%s>", start));
1407 			sp = stalloc(sizeof(*sp));
1408 			sp->text = start;
1409 			*arglist->lastp = sp;
1410 			arglist->lastp = &sp->next;
1411 			p++;
1412 
1413 			if (ifsspc != NULL) {
1414 				/* Ignore further trailing IFS whitespace */
1415 				for (; p < string + ifsp->endoff; p++) {
1416 					q = p;
1417 					if (*p == CTLNONL)
1418 						continue;
1419 					if (*p == CTLESC)
1420 						p++;
1421 					if (strchr(ifs, *p) == NULL) {
1422 						p = q;
1423 						break;
1424 					}
1425 					if (strchr(" \t\n", *p) == NULL) {
1426 						p++;
1427 						break;
1428 					}
1429 				}
1430 			}
1431 			start = p;
1432 		}
1433 	}
1434 
1435 /*
1436 	while (*start == CTLQUOTEEND)
1437 		start++;
1438 */
1439 
1440 	/*
1441 	 * Save anything left as an argument.
1442 	 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1443 	 * generating 2 arguments, the second of which is empty.
1444 	 * Some recent clarification of the Posix spec say that it
1445 	 * should only generate one....
1446 	 */
1447 	if (had_param_ch || *start != 0) {
1448 		VTRACE(DBG_EXPAND, (" T<%s>", start));
1449 		sp = stalloc(sizeof(*sp));
1450 		sp->text = start;
1451 		*arglist->lastp = sp;
1452 		arglist->lastp = &sp->next;
1453 	}
1454 	VTRACE(DBG_EXPAND, ("\n"));
1455 }
1456 
1457 STATIC void
1458 ifsfree(void)
1459 {
1460 	while (ifsfirst.next != NULL) {
1461 		struct ifsregion *ifsp;
1462 		INTOFF;
1463 		ifsp = ifsfirst.next->next;
1464 		ckfree(ifsfirst.next);
1465 		ifsfirst.next = ifsp;
1466 		INTON;
1467 	}
1468 	ifslastp = NULL;
1469 	ifsfirst.next = NULL;
1470 }
1471 
1472 
1473 
1474 /*
1475  * Expand shell metacharacters.  At this point, the only control characters
1476  * should be escapes.  The results are stored in the list exparg.
1477  */
1478 
1479 char *expdir;
1480 
1481 
1482 STATIC void
1483 expandmeta(struct strlist *str, int flag)
1484 {
1485 	char *p;
1486 	struct strlist **savelastp;
1487 	struct strlist *sp;
1488 	char c;
1489 	/* TODO - EXP_REDIR */
1490 
1491 	while (str) {
1492 		p = str->text;
1493 		for (;;) {			/* fast check for meta chars */
1494 			if ((c = *p++) == '\0')
1495 				goto nometa;
1496 			if (c == '*' || c == '?' || c == '[' /* || c == '!' */)
1497 				break;
1498 		}
1499 		savelastp = exparg.lastp;
1500 		INTOFF;
1501 		if (expdir == NULL) {
1502 			int i = strlen(str->text);
1503 			expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
1504 		}
1505 
1506 		expmeta(expdir, str->text);
1507 		ckfree(expdir);
1508 		expdir = NULL;
1509 		INTON;
1510 		if (exparg.lastp == savelastp) {
1511 			/*
1512 			 * no matches
1513 			 */
1514  nometa:
1515 			*exparg.lastp = str;
1516 			rmescapes(str->text);
1517 			exparg.lastp = &str->next;
1518 		} else {
1519 			*exparg.lastp = NULL;
1520 			*savelastp = sp = expsort(*savelastp);
1521 			while (sp->next != NULL)
1522 				sp = sp->next;
1523 			exparg.lastp = &sp->next;
1524 		}
1525 		str = str->next;
1526 	}
1527 }
1528 
1529 STATIC void
1530 add_args(struct strlist *str)
1531 {
1532 	while (str) {
1533 		*exparg.lastp = str;
1534 		rmescapes(str->text);
1535 		exparg.lastp = &str->next;
1536 		str = str->next;
1537 	}
1538 }
1539 
1540 
1541 /*
1542  * Do metacharacter (i.e. *, ?, [...]) expansion.
1543  */
1544 
1545 STATIC void
1546 expmeta(char *enddir, char *name)
1547 {
1548 	char *p;
1549 	const char *cp;
1550 	char *q;
1551 	char *start;
1552 	char *endname;
1553 	int metaflag;
1554 	struct stat statb;
1555 	DIR *dirp;
1556 	struct dirent *dp;
1557 	int atend;
1558 	int matchdot;
1559 
1560 	CTRACE(DBG_EXPAND|DBG_MATCH, ("expmeta(\"%s\")\n", name));
1561 	metaflag = 0;
1562 	start = name;
1563 	for (p = name ; ; p++) {
1564 		if (*p == '*' || *p == '?')
1565 			metaflag = 1;
1566 		else if (*p == '[') {
1567 			q = p + 1;
1568 			if (*q == '!' || *q == '^')
1569 				q++;
1570 			for (;;) {
1571 				while (IS_BORING(*q))
1572 					q++;
1573 				if (*q == ']') {
1574 					q++;
1575 					metaflag = 1;
1576 					break;
1577 				}
1578 				if (*q == '[' && q[1] == ':') {
1579 					/*
1580 					 * character class, look for :] ending
1581 					 * also stop on ']' (end bracket expr)
1582 					 * or '\0' or '/' (end pattern)
1583 					 */
1584 					while (*++q != '\0' && *q != ']' &&
1585 					    *q != '/') {
1586 						if (*q == CTLESC) {
1587 							if (*++q == '\0')
1588 								break;
1589 							if (*q == '/')
1590 								break;
1591 						} else if (*q == ':' &&
1592 						    q[1] == ']')
1593 							break;
1594 					}
1595 					if (*q == ':') {
1596 						/*
1597 						 * stopped at ':]'
1598 						 * still in [...]
1599 						 * skip ":]" and continue;
1600 						 */
1601 						q += 2;
1602 						continue;
1603 					}
1604 
1605 					/* done at end of pattern, not [...] */
1606 					if (*q == '\0' || *q == '/')
1607 						break;
1608 
1609 					/* found the ']', we have a [...] */
1610 					metaflag = 1;
1611 					q++;	/* skip ']' */
1612 					break;
1613 				}
1614 				if (*q == CTLESC)
1615 					q++;
1616 				/* end of pattern cannot be escaped */
1617 				if (*q == '/' || *q == '\0')
1618 					break;
1619 				q++;
1620 			}
1621 		} else if (*p == '\0')
1622 			break;
1623 		else if (IS_BORING(*p))
1624 			continue;
1625 		else if (*p == CTLESC)
1626 			p++;
1627 		if (*p == '/') {
1628 			if (metaflag)
1629 				break;
1630 			start = p + 1;
1631 		}
1632 	}
1633 	if (metaflag == 0) {	/* we've reached the end of the file name */
1634 		if (enddir != expdir)
1635 			metaflag++;
1636 		for (p = name ; ; p++) {
1637 			if (IS_BORING(*p))
1638 				continue;
1639 			if (*p == CTLESC)
1640 				p++;
1641 			*enddir++ = *p;
1642 			if (*p == '\0')
1643 				break;
1644 		}
1645 		if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1646 			addfname(expdir);
1647 		return;
1648 	}
1649 	endname = p;
1650 	if (start != name) {
1651 		p = name;
1652 		while (p < start) {
1653 			while (IS_BORING(*p))
1654 				p++;
1655 			if (*p == CTLESC)
1656 				p++;
1657 			*enddir++ = *p++;
1658 		}
1659 	}
1660 	if (enddir == expdir) {
1661 		cp = ".";
1662 	} else if (enddir == expdir + 1 && *expdir == '/') {
1663 		cp = "/";
1664 	} else {
1665 		cp = expdir;
1666 		enddir[-1] = '\0';
1667 	}
1668 	if ((dirp = opendir(cp)) == NULL)
1669 		return;
1670 	if (enddir != expdir)
1671 		enddir[-1] = '/';
1672 	if (*endname == 0) {
1673 		atend = 1;
1674 	} else {
1675 		atend = 0;
1676 		*endname++ = '\0';
1677 	}
1678 	matchdot = 0;
1679 	p = start;
1680 	while (IS_BORING(*p))
1681 		p++;
1682 	if (*p == CTLESC)
1683 		p++;
1684 	if (*p == '.')
1685 		matchdot++;
1686 	while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1687 		if (dp->d_name[0] == '.' && ! matchdot)
1688 			continue;
1689 		if (patmatch(start, dp->d_name, 0)) {
1690 			if (atend) {
1691 				scopy(dp->d_name, enddir);
1692 				addfname(expdir);
1693 			} else {
1694 				for (p = enddir, cp = dp->d_name;
1695 				     (*p++ = *cp++) != '\0';)
1696 					continue;
1697 				p[-1] = '/';
1698 				expmeta(p, endname);
1699 			}
1700 		}
1701 	}
1702 	closedir(dirp);
1703 	if (! atend)
1704 		endname[-1] = '/';
1705 }
1706 
1707 
1708 /*
1709  * Add a file name to the list.
1710  */
1711 
1712 STATIC void
1713 addfname(char *name)
1714 {
1715 	char *p;
1716 	struct strlist *sp;
1717 
1718 	p = stalloc(strlen(name) + 1);
1719 	scopy(name, p);
1720 	sp = stalloc(sizeof(*sp));
1721 	sp->text = p;
1722 	*exparg.lastp = sp;
1723 	exparg.lastp = &sp->next;
1724 }
1725 
1726 
1727 /*
1728  * Sort the results of file name expansion.  It calculates the number of
1729  * strings to sort and then calls msort (short for merge sort) to do the
1730  * work.
1731  */
1732 
1733 STATIC struct strlist *
1734 expsort(struct strlist *str)
1735 {
1736 	int len;
1737 	struct strlist *sp;
1738 
1739 	len = 0;
1740 	for (sp = str ; sp ; sp = sp->next)
1741 		len++;
1742 	return msort(str, len);
1743 }
1744 
1745 
1746 STATIC struct strlist *
1747 msort(struct strlist *list, int len)
1748 {
1749 	struct strlist *p, *q = NULL;
1750 	struct strlist **lpp;
1751 	int half;
1752 	int n;
1753 
1754 	if (len <= 1)
1755 		return list;
1756 	half = len >> 1;
1757 	p = list;
1758 	for (n = half ; --n >= 0 ; ) {
1759 		q = p;
1760 		p = p->next;
1761 	}
1762 	q->next = NULL;			/* terminate first half of list */
1763 	q = msort(list, half);		/* sort first half of list */
1764 	p = msort(p, len - half);		/* sort second half */
1765 	lpp = &list;
1766 	for (;;) {
1767 		if (strcmp(p->text, q->text) < 0) {
1768 			*lpp = p;
1769 			lpp = &p->next;
1770 			if ((p = *lpp) == NULL) {
1771 				*lpp = q;
1772 				break;
1773 			}
1774 		} else {
1775 			*lpp = q;
1776 			lpp = &q->next;
1777 			if ((q = *lpp) == NULL) {
1778 				*lpp = p;
1779 				break;
1780 			}
1781 		}
1782 	}
1783 	return list;
1784 }
1785 
1786 
1787 /*
1788  * See if a character matches a character class, starting at the first colon
1789  * of "[:class:]".
1790  * If a valid character class is recognized, a pointer to the next character
1791  * after the final closing bracket is stored into *end, otherwise a null
1792  * pointer is stored into *end.
1793  */
1794 static int
1795 match_charclass(const char *p, wchar_t chr, const char **end)
1796 {
1797 	char name[20];
1798 	char *nameend;
1799 	wctype_t cclass;
1800 	char *q;
1801 
1802 	*end = NULL;
1803 	p++;
1804 	q = &name[0];
1805 	nameend = strstr(p, ":]");
1806 	if (nameend == NULL || nameend == p)	/* not a valid class */
1807 		return 0;
1808 
1809 	if (*p == CTLESC) {
1810 		if (*++p == CTLESC)
1811 			return 0;
1812 		if (p == nameend)
1813 			return 0;
1814 	}
1815 	if (!is_alpha(*p))
1816 		return 0;
1817 	while (p < nameend) {
1818 		if (*p == CTLESC) {
1819 			p++;
1820 			if (p == nameend)
1821 				return 0;
1822 		}
1823 		if (!is_in_name(*p))	/* '_' is a local extension */
1824 			return 0;
1825 		if (q < &name[sizeof name])
1826 			*q++ = *p++;
1827 		else
1828 			p++;
1829 	}
1830 
1831 	*end = nameend + 2;		/* committed to it being a char class */
1832 
1833 	if (q < &name[sizeof name])	/* a usable name found */
1834 		*q++ = '\0';
1835 	else				/* too long, valid, but no match */
1836 		return 0;
1837 
1838 	cclass = wctype(name);
1839 	/* An unknown class matches nothing but is valid nevertheless. */
1840 	if (cclass == 0)
1841 		return 0;
1842 	return iswctype(chr, cclass);
1843 }
1844 
1845 
1846 /*
1847  * Returns true if the pattern matches the string.
1848  */
1849 
1850 STATIC int
1851 patmatch(const char *pattern, const char *string, int squoted)
1852 {
1853 	const char *p, *q, *end;
1854 	const char *bt_p, *bt_q;
1855 	char c;
1856 	wchar_t wc, wc2;
1857 
1858 	VTRACE(DBG_MATCH, ("patmatch(P=\"%s\", W=\"%s\"%s): ",
1859 	    pattern, string, squoted ? ", SQ" : ""));
1860 	p = pattern;
1861 	q = string;
1862 	bt_p = NULL;
1863 	bt_q = NULL;
1864 	for (;;) {
1865 		switch (c = *p++) {
1866 		case '\0':
1867 			if (squoted && *q == CTLESC) {
1868 				if (q[1] == '\0')
1869 					q++;
1870 			}
1871 			if (*q != '\0')
1872 				goto backtrack;
1873 			VTRACE(DBG_MATCH, ("match\n"));
1874 			return 1;
1875 		case CTLESC:
1876 			if (squoted && *q == CTLESC)
1877 				q++;
1878 			if (*p == '\0' && *q == '\0') {
1879 				VTRACE(DBG_MATCH, ("match-\\\n"));
1880 				return 1;
1881 			}
1882 			if (*q++ != *p++)
1883 				goto backtrack;
1884 			break;
1885 		case '\\':
1886 			if (squoted && *q == CTLESC)
1887 				q++;
1888 			if (*q++ != *p++)
1889 				goto backtrack;
1890 			break;
1891 		case CTLQUOTEMARK:
1892 		case CTLQUOTEEND:
1893 		case CTLNONL:
1894 			continue;
1895 		case '?':
1896 			if (squoted && *q == CTLESC)
1897 				q++;
1898 			if (*q++ == '\0') {
1899 				VTRACE(DBG_MATCH, ("?fail\n"));
1900 				return 0;
1901 			}
1902 			break;
1903 		case '*':
1904 			c = *p;
1905 			while (c == CTLQUOTEMARK || c == '*')
1906 				c = *++p;
1907 			if (c != CTLESC && !IS_BORING(c) &&
1908 			    c != '?' && c != '*' && c != '[') {
1909 				while (*q != c) {
1910 					if (squoted && *q == CTLESC &&
1911 					    q[1] == c)
1912 						break;
1913 					if (*q == '\0') {
1914 						VTRACE(DBG_MATCH, ("*fail\n"));
1915 						return 0;
1916 					}
1917 					if (squoted && *q == CTLESC)
1918 						q++;
1919 					q++;
1920 				}
1921 			}
1922 			if (c == CTLESC && p[1] == '\0') {
1923 				VTRACE(DBG_MATCH, ("match+\\\n"));
1924 				return 1;
1925 			}
1926 			/*
1927 			 * First try the shortest match for the '*' that
1928 			 * could work. We can forget any earlier '*' since
1929 			 * there is no way having it match more characters
1930 			 * can help us, given that we are already here.
1931 			 */
1932 			bt_p = p;
1933 			bt_q = q;
1934 			break;
1935 		case '[': {
1936 			const char *savep, *saveq, *endp;
1937 			int invert, found;
1938 			unsigned char chr;
1939 
1940 			/*
1941 			 * First quick check to see if there is a
1942 			 * possible matching ']' - if not, then this
1943 			 * is not a char class, and the '[' is just
1944 			 * a literal '['.
1945 			 *
1946 			 * This check will not detect all non classes, but
1947 			 * that's OK - It just means that we execute the
1948 			 * harder code sometimes when it it cannot succeed.
1949 			 */
1950 			endp = p;
1951 			if (*endp == '!' || *endp == '^')
1952 				endp++;
1953 			for (;;) {
1954 				while (IS_BORING(*endp))
1955 					endp++;
1956 				if (*endp == '\0')
1957 					goto dft;	/* no matching ] */
1958 				if (*endp++ == ']')
1959 					break;
1960 			}
1961 			/* end shortcut */
1962 
1963 			savep = p, saveq = q;
1964 			invert = 0;
1965 			if (*p == '!' || *p == '^') {
1966 				invert++;
1967 				p++;
1968 			}
1969 			found = 0;
1970 			if (*q == '\0') {
1971 				VTRACE(DBG_MATCH, ("[]fail\n"));
1972 				return 0;
1973 			}
1974 			if (squoted && *q == CTLESC)
1975 				q++;
1976 			chr = (unsigned char)*q++;
1977 			c = *p++;
1978 			do {
1979 				if (IS_BORING(c))
1980 					continue;
1981 				if (c == '\0') {
1982 					p = savep, q = saveq;
1983 					c = '[';
1984 					goto dft;
1985 				}
1986 				if (c == '[' && *p == ':') {
1987 					found |= match_charclass(p, chr, &end);
1988 					if (end != NULL) {
1989 						p = end;
1990 						continue;
1991 					}
1992 				}
1993 				if (c == CTLESC || c == '\\')
1994 					c = *p++;
1995 				wc = (unsigned char)c;
1996 				if (*p == '-' && p[1] != ']') {
1997 					p++;
1998 					if (*p == CTLESC || *p == '\\')
1999 						p++;
2000 					wc2 = (unsigned char)*p++;
2001 					if (   collate_range_cmp(chr, wc) >= 0
2002 					    && collate_range_cmp(chr, wc2) <= 0
2003 					   )
2004 						found = 1;
2005 				} else {
2006 					if (chr == wc)
2007 						found = 1;
2008 				}
2009 			} while ((c = *p++) != ']');
2010 			if (found == invert)
2011 				goto backtrack;
2012 			break;
2013 		}
2014   dft:		default:
2015 			if (squoted && *q == CTLESC)
2016 				q++;
2017 			if (*q++ == c)
2018 				break;
2019   backtrack:
2020 			/*
2021 			 * If we have a mismatch (other than hitting the end
2022 			 * of the string), go back to the last '*' seen and
2023 			 * have it match one additional character.
2024 			 */
2025 			if (bt_p == NULL) {
2026 				VTRACE(DBG_MATCH, ("BTP fail\n"));
2027 				return 0;
2028 			}
2029 			if (*bt_q == '\0') {
2030 				VTRACE(DBG_MATCH, ("BTQ fail\n"));
2031 				return 0;
2032 			}
2033 			bt_q++;
2034 			p = bt_p;
2035 			q = bt_q;
2036 			break;
2037 		}
2038 	}
2039 }
2040 
2041 
2042 
2043 /*
2044  * Remove any CTLESC or CTLNONL characters from a string.
2045  *
2046  * String is modified in place, and we return the length of the result
2047  */
2048 
2049 int
2050 rmescapes(char *str)
2051 {
2052 	char *p, *q;
2053 
2054 	p = str;
2055 	while (!ISCTL(*p)) {
2056 		if (*p++ == '\0')
2057 			return ((int)(p - str) - 1);
2058 	}
2059 	q = p;
2060 	while (*p) {
2061 		if (IS_BORING(*p)) {
2062 			p++;
2063 			continue;
2064 		}
2065 		if (*p == CTLCNL) {
2066 			p++;
2067 			*q++ = '\n';
2068 			continue;
2069 		}
2070 		if (*p == CTLESC)
2071 			p++;
2072 #ifdef DEBUG
2073 		else if (ISCTL(*p))
2074 			abort();
2075 #endif
2076 		*q++ = *p++;
2077 	}
2078 	*q = '\0';
2079 
2080 	return ((int)(q - str));
2081 }
2082 
2083 /*
2084  * and a special version for dealing with expressions to be parsed
2085  * by the arithmetic evaluator.   That needs to be able to count \n's
2086  * even ones that were \newline elided \n's, so we have to put the
2087  * latter back into the string - just being careful to put them only
2088  * at a place where white space can reasonably occur in the string
2089  * -- then the \n we insert will just be white space, and ignored
2090  * for all purposes except line counting.
2091  */
2092 
2093 void
2094 rmescapes_nl(char *str)
2095 {
2096 	char *p, *q;
2097 	int nls = 0, holdnl = 0, holdlast;
2098 
2099 	p = str;
2100 	while (!ISCTL(*p)) {
2101 		if (*p++ == '\0')
2102 			return;
2103 	}
2104 	if (p > str)	/* must reprocess char before stopper (if any) */
2105 		--p;	/* so we do not place a \n badly */
2106 	q = p;
2107 	while (*p) {
2108 		if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
2109 			p++;
2110 			continue;
2111 		}
2112 		if (*p == CTLNONL) {
2113 			p++;
2114 			nls++;
2115 			continue;
2116 		}
2117 		if (*p == CTLCNL) {
2118 			p++;
2119 			*q++ = '\n';
2120 			continue;
2121 		}
2122 		if (*p == CTLESC)
2123 			p++;
2124 #ifdef DEBUG
2125 		else if (ISCTL(*p))
2126 			abort();
2127 #endif
2128 
2129 		holdlast = holdnl;
2130 		holdnl = is_in_name(*p);	/* letters, digits, _ */
2131 		if (q == str || is_space(q[-1]) || (*p != '=' && q[-1] != *p)) {
2132 			if (nls > 0 && holdnl != holdlast) {
2133 				while (nls > 0)
2134 					*q++ = '\n', nls--;
2135 			}
2136 		}
2137 		*q++ = *p++;
2138 	}
2139 	while (--nls >= 0)
2140 		*q++ = '\n';
2141 	*q = '\0';
2142 }
2143 
2144 
2145 
2146 /*
2147  * See if a pattern matches in a case statement.
2148  */
2149 
2150 int
2151 casematch(union node *pattern, char *val)
2152 {
2153 	struct stackmark smark;
2154 	int result;
2155 	char *p;
2156 
2157 	CTRACE(DBG_MATCH, ("casematch(P=\"%s\", W=\"%s\")\n",
2158 	    pattern->narg.text, val));
2159 	setstackmark(&smark);
2160 	argbackq = pattern->narg.backquote;
2161 	STARTSTACKSTR(expdest);
2162 	ifslastp = NULL;
2163 	argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
2164 	STPUTC('\0', expdest);
2165 	p = grabstackstr(expdest);
2166 	result = patmatch(p, val, 0);
2167 	popstackmark(&smark);
2168 	return result;
2169 }
2170 
2171 /*
2172  * Our own itoa().   Assumes result buffer is on the stack
2173  */
2174 
2175 STATIC char *
2176 cvtnum(int num, char *buf)
2177 {
2178 	char temp[32];
2179 	int neg = num < 0;
2180 	char *p = temp + sizeof temp - 1;
2181 
2182 	if (neg)
2183 		num = -num;
2184 
2185 	*p = '\0';
2186 	do {
2187 		*--p = num % 10 + '0';
2188 	} while ((num /= 10) != 0 && p > temp + 1);
2189 
2190 	if (neg)
2191 		*--p = '-';
2192 
2193 	while (*p)
2194 		STPUTC(*p++, buf);
2195 	return buf;
2196 }
2197 
2198 /*
2199  * Do most of the work for wordexp(3).
2200  */
2201 
2202 int
2203 wordexpcmd(int argc, char **argv)
2204 {
2205 	size_t len;
2206 	int i;
2207 
2208 	out1fmt("%d", argc - 1);
2209 	out1c('\0');
2210 	for (i = 1, len = 0; i < argc; i++)
2211 		len += strlen(argv[i]);
2212 	out1fmt("%zu", len);
2213 	out1c('\0');
2214 	for (i = 1; i < argc; i++) {
2215 		out1str(argv[i]);
2216 		out1c('\0');
2217 	}
2218 	return (0);
2219 }
2220