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