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