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