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