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