expand.c revision 229220
1218885Sdim/*-
2218885Sdim * Copyright (c) 1991, 1993
3218885Sdim *	The Regents of the University of California.  All rights reserved.
4218885Sdim * Copyright (c) 1997-2005
5218885Sdim *	Herbert Xu <herbert@gondor.apana.org.au>.  All rights reserved.
6218885Sdim *
7218885Sdim * This code is derived from software contributed to Berkeley by
8218885Sdim * Kenneth Almquist.
9218885Sdim *
10263508Sdim * Redistribution and use in source and binary forms, with or without
11263508Sdim * modification, are permitted provided that the following conditions
12263508Sdim * are met:
13218885Sdim * 1. Redistributions of source code must retain the above copyright
14218885Sdim *    notice, this list of conditions and the following disclaimer.
15218885Sdim * 2. Redistributions in binary form must reproduce the above copyright
16263508Sdim *    notice, this list of conditions and the following disclaimer in the
17263508Sdim *    documentation and/or other materials provided with the distribution.
18263508Sdim * 4. Neither the name of the University nor the names of its contributors
19263508Sdim *    may be used to endorse or promote products derived from this software
20263508Sdim *    without specific prior written permission.
21263508Sdim *
22263508Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23263508Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24263508Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25263508Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26263508Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27263508Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28263508Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29263508Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30263508Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31263508Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32263508Sdim * SUCH DAMAGE.
33263508Sdim */
34263508Sdim
35263508Sdim#ifndef lint
36263508Sdim#if 0
37263508Sdimstatic char sccsid[] = "@(#)expand.c	8.5 (Berkeley) 5/15/95";
38263508Sdim#endif
39263508Sdim#endif /* not lint */
40263508Sdim#include <sys/cdefs.h>
41263508Sdim__FBSDID("$FreeBSD: head/bin/sh/expand.c 229220 2012-01-01 22:17:12Z jilles $");
42263508Sdim
43263508Sdim#include <sys/types.h>
44263508Sdim#include <sys/time.h>
45263508Sdim#include <sys/stat.h>
46263508Sdim#include <dirent.h>
47263508Sdim#include <errno.h>
48263508Sdim#include <inttypes.h>
49263508Sdim#include <limits.h>
50263508Sdim#include <pwd.h>
51263508Sdim#include <stdio.h>
52263508Sdim#include <stdlib.h>
53263508Sdim#include <string.h>
54263508Sdim#include <unistd.h>
55263508Sdim#include <wchar.h>
56263508Sdim#include <wctype.h>
57263508Sdim
58263508Sdim/*
59263508Sdim * Routines to expand arguments to commands.  We have to deal with
60263508Sdim * backquotes, shell variables, and file metacharacters.
61263508Sdim */
62263508Sdim
63263508Sdim#include "shell.h"
64263508Sdim#include "main.h"
65263508Sdim#include "nodes.h"
66263508Sdim#include "eval.h"
67263508Sdim#include "expand.h"
68263508Sdim#include "syntax.h"
69263508Sdim#include "parser.h"
70263508Sdim#include "jobs.h"
71263508Sdim#include "options.h"
72263508Sdim#include "var.h"
73263508Sdim#include "input.h"
74263508Sdim#include "output.h"
75263508Sdim#include "memalloc.h"
76263508Sdim#include "error.h"
77263508Sdim#include "mystring.h"
78263508Sdim#include "arith.h"
79263508Sdim#include "show.h"
80263508Sdim#include "builtins.h"
81263508Sdim
82263508Sdim/*
83263508Sdim * Structure specifying which parts of the string should be searched
84263508Sdim * for IFS characters.
85263508Sdim */
86263508Sdim
87263508Sdimstruct ifsregion {
88263508Sdim	struct ifsregion *next;	/* next region in list */
89263508Sdim	int begoff;		/* offset of start of region */
90263508Sdim	int endoff;		/* offset of end of region */
91263508Sdim	int inquotes;		/* search for nul bytes only */
92263508Sdim};
93263508Sdim
94263508Sdim
95263508Sdimstatic char *expdest;			/* output of current string */
96263508Sdimstatic struct nodelist *argbackq;	/* list of back quote expressions */
97263508Sdimstatic struct ifsregion ifsfirst;	/* first struct in list of ifs regions */
98263508Sdimstatic struct ifsregion *ifslastp;	/* last struct in list */
99263508Sdimstatic struct arglist exparg;		/* holds expanded arg list */
100263508Sdim
101263508Sdimstatic void argstr(char *, int);
102263508Sdimstatic char *exptilde(char *, int);
103263508Sdimstatic void expbackq(union node *, int, int);
104263508Sdimstatic int subevalvar(char *, char *, int, int, int, int, int);
105263508Sdimstatic char *evalvar(char *, int);
106263508Sdimstatic int varisset(char *, int);
107263508Sdimstatic void varvalue(char *, int, int, int);
108263508Sdimstatic void recordregion(int, int, int);
109263508Sdimstatic void removerecordregions(int);
110263508Sdimstatic void ifsbreakup(char *, struct arglist *);
111263508Sdimstatic void expandmeta(struct strlist *, int);
112263508Sdimstatic void expmeta(char *, char *);
113263508Sdimstatic void addfname(char *);
114263508Sdimstatic struct strlist *expsort(struct strlist *);
115263508Sdimstatic struct strlist *msort(struct strlist *, int);
116263508Sdimstatic int patmatch(const char *, const char *, int);
117263508Sdimstatic char *cvtnum(int, char *);
118263508Sdimstatic int collate_range_cmp(wchar_t, wchar_t);
119263508Sdim
120263508Sdimstatic int
121263508Sdimcollate_range_cmp(wchar_t c1, wchar_t c2)
122263508Sdim{
123263508Sdim	static wchar_t s1[2], s2[2];
124263508Sdim
125263508Sdim	s1[0] = c1;
126263508Sdim	s2[0] = c2;
127263508Sdim	return (wcscoll(s1, s2));
128263508Sdim}
129263508Sdim
130263508Sdim/*
131263508Sdim * Expand shell variables and backquotes inside a here document.
132263508Sdim *	union node *arg		the document
133263508Sdim *	int fd;			where to write the expanded version
134263508Sdim */
135263508Sdim
136263508Sdimvoid
137263508Sdimexpandhere(union node *arg, int fd)
138263508Sdim{
139263508Sdim	expandarg(arg, (struct arglist *)NULL, 0);
140263508Sdim	xwrite(fd, stackblock(), expdest - stackblock());
141263508Sdim}
142263508Sdim
143263508Sdimstatic char *
144263508Sdimstputs_quotes(const char *data, const char *syntax, char *p)
145263508Sdim{
146263508Sdim	while (*data) {
147263508Sdim		CHECKSTRSPACE(2, p);
148263508Sdim		if (syntax[(int)*data] == CCTL)
149263508Sdim			USTPUTC(CTLESC, p);
150263508Sdim		USTPUTC(*data++, p);
151263508Sdim	}
152263508Sdim	return (p);
153263508Sdim}
154263508Sdim#define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p)
155263508Sdim
156263508Sdim/*
157263508Sdim * Perform expansions on an argument, placing the resulting list of arguments
158263508Sdim * in arglist.  Parameter expansion, command substitution and arithmetic
159263508Sdim * expansion are always performed; additional expansions can be requested
160263508Sdim * via flag (EXP_*).
161263508Sdim * The result is left in the stack string.
162263508Sdim * When arglist is NULL, perform here document expansion.
163263508Sdim *
164263508Sdim * Caution: this function uses global state and is not reentrant.
165263508Sdim * However, a new invocation after an interrupted invocation is safe
166263508Sdim * and will reset the global state for the new call.
167263508Sdim */
168263508Sdimvoid
169263508Sdimexpandarg(union node *arg, struct arglist *arglist, int flag)
170263508Sdim{
171263508Sdim	struct strlist *sp;
172263508Sdim	char *p;
173263508Sdim
174263508Sdim	argbackq = arg->narg.backquote;
175263508Sdim	STARTSTACKSTR(expdest);
176263508Sdim	ifsfirst.next = NULL;
177263508Sdim	ifslastp = NULL;
178263508Sdim	argstr(arg->narg.text, flag);
179263508Sdim	if (arglist == NULL) {
180263508Sdim		STACKSTRNUL(expdest);
181263508Sdim		return;			/* here document expanded */
182263508Sdim	}
183263508Sdim	STPUTC('\0', expdest);
184263508Sdim	p = grabstackstr(expdest);
185263508Sdim	exparg.lastp = &exparg.list;
186263508Sdim	/*
187263508Sdim	 * TODO - EXP_REDIR
188263508Sdim	 */
189263508Sdim	if (flag & EXP_FULL) {
190263508Sdim		ifsbreakup(p, &exparg);
191263508Sdim		*exparg.lastp = NULL;
192263508Sdim		exparg.lastp = &exparg.list;
193263508Sdim		expandmeta(exparg.list, flag);
194263508Sdim	} else {
195263508Sdim		if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
196263508Sdim			rmescapes(p);
197263508Sdim		sp = (struct strlist *)stalloc(sizeof (struct strlist));
198263508Sdim		sp->text = p;
199263508Sdim		*exparg.lastp = sp;
200263508Sdim		exparg.lastp = &sp->next;
201263508Sdim	}
202263508Sdim	while (ifsfirst.next != NULL) {
203263508Sdim		struct ifsregion *ifsp;
204263508Sdim		INTOFF;
205263508Sdim		ifsp = ifsfirst.next->next;
206263508Sdim		ckfree(ifsfirst.next);
207263508Sdim		ifsfirst.next = ifsp;
208263508Sdim		INTON;
209263508Sdim	}
210263508Sdim	*exparg.lastp = NULL;
211263508Sdim	if (exparg.list) {
212263508Sdim		*arglist->lastp = exparg.list;
213263508Sdim		arglist->lastp = exparg.lastp;
214263508Sdim	}
215263508Sdim}
216263508Sdim
217263508Sdim
218263508Sdim
219263508Sdim/*
220263508Sdim * Perform parameter expansion, command substitution and arithmetic
221263508Sdim * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
222263508Sdim * Processing ends at a CTLENDVAR character as well as '\0'.
223263508Sdim * This is used to expand word in ${var+word} etc.
224263508Sdim * If EXP_FULL, EXP_CASE or EXP_REDIR are set, keep and/or generate CTLESC
225263508Sdim * characters to allow for further processing.
226263508Sdim * If EXP_FULL is set, also preserve CTLQUOTEMARK characters.
227263508Sdim */
228263508Sdimstatic void
229263508Sdimargstr(char *p, int flag)
230263508Sdim{
231263508Sdim	char c;
232263508Sdim	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);	/* do CTLESC */
233263508Sdim	int firsteq = 1;
234263508Sdim	int split_lit;
235263508Sdim	int lit_quoted;
236263508Sdim
237263508Sdim	split_lit = flag & EXP_SPLIT_LIT;
238263508Sdim	lit_quoted = flag & EXP_LIT_QUOTED;
239263508Sdim	flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED);
240263508Sdim	if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
241263508Sdim		p = exptilde(p, flag);
242263508Sdim	for (;;) {
243263508Sdim		CHECKSTRSPACE(2, expdest);
244263508Sdim		switch (c = *p++) {
245263508Sdim		case '\0':
246263508Sdim		case CTLENDVAR:
247263508Sdim			goto breakloop;
248263508Sdim		case CTLQUOTEMARK:
249263508Sdim			lit_quoted = 1;
250263508Sdim			/* "$@" syntax adherence hack */
251263508Sdim			if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
252263508Sdim				break;
253263508Sdim			if ((flag & EXP_FULL) != 0)
254263508Sdim				USTPUTC(c, expdest);
255263508Sdim			break;
256263508Sdim		case CTLQUOTEEND:
257263508Sdim			lit_quoted = 0;
258263508Sdim			break;
259263508Sdim		case CTLESC:
260263508Sdim			if (quotes)
261263508Sdim				USTPUTC(c, expdest);
262263508Sdim			c = *p++;
263263508Sdim			USTPUTC(c, expdest);
264263508Sdim			if (split_lit && !lit_quoted)
265263508Sdim				recordregion(expdest - stackblock() -
266263508Sdim				    (quotes ? 2 : 1),
267263508Sdim				    expdest - stackblock(), 0);
268263508Sdim			break;
269263508Sdim		case CTLVAR:
270263508Sdim			p = evalvar(p, flag);
271263508Sdim			break;
272263508Sdim		case CTLBACKQ:
273263508Sdim		case CTLBACKQ|CTLQUOTE:
274263508Sdim			expbackq(argbackq->n, c & CTLQUOTE, flag);
275263508Sdim			argbackq = argbackq->next;
276263508Sdim			break;
277263508Sdim		case CTLENDARI:
278263508Sdim			expari(flag);
279263508Sdim			break;
280263508Sdim		case ':':
281263508Sdim		case '=':
282263508Sdim			/*
283263508Sdim			 * sort of a hack - expand tildes in variable
284263508Sdim			 * assignments (after the first '=' and after ':'s).
285263508Sdim			 */
286263508Sdim			USTPUTC(c, expdest);
287263508Sdim			if (split_lit && !lit_quoted)
288263508Sdim				recordregion(expdest - stackblock() - 1,
289263508Sdim				    expdest - stackblock(), 0);
290263508Sdim			if (flag & EXP_VARTILDE && *p == '~' &&
291263508Sdim			    (c != '=' || firsteq)) {
292263508Sdim				if (c == '=')
293263508Sdim					firsteq = 0;
294263508Sdim				p = exptilde(p, flag);
295263508Sdim			}
296263508Sdim			break;
297263508Sdim		default:
298263508Sdim			USTPUTC(c, expdest);
299263508Sdim			if (split_lit && !lit_quoted)
300263508Sdim				recordregion(expdest - stackblock() - 1,
301263508Sdim				    expdest - stackblock(), 0);
302263508Sdim		}
303263508Sdim	}
304263508Sdimbreakloop:;
305263508Sdim}
306263508Sdim
307263508Sdim/*
308263508Sdim * Perform tilde expansion, placing the result in the stack string and
309263508Sdim * returning the next position in the input string to process.
310263508Sdim */
311263508Sdimstatic char *
312263508Sdimexptilde(char *p, int flag)
313263508Sdim{
314263508Sdim	char c, *startp = p;
315263508Sdim	struct passwd *pw;
316263508Sdim	char *home;
317263508Sdim	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
318263508Sdim
319263508Sdim	while ((c = *p) != '\0') {
320263508Sdim		switch(c) {
321263508Sdim		case CTLESC: /* This means CTL* are always considered quoted. */
322263508Sdim		case CTLVAR:
323263508Sdim		case CTLBACKQ:
324263508Sdim		case CTLBACKQ | CTLQUOTE:
325263508Sdim		case CTLARI:
326263508Sdim		case CTLENDARI:
327263508Sdim		case CTLQUOTEMARK:
328263508Sdim			return (startp);
329263508Sdim		case ':':
330263508Sdim			if (flag & EXP_VARTILDE)
331263508Sdim				goto done;
332263508Sdim			break;
333263508Sdim		case '/':
334263508Sdim		case CTLENDVAR:
335263508Sdim			goto done;
336263508Sdim		}
337263508Sdim		p++;
338263508Sdim	}
339263508Sdimdone:
340263508Sdim	*p = '\0';
341263508Sdim	if (*(startp+1) == '\0') {
342263508Sdim		if ((home = lookupvar("HOME")) == NULL)
343263508Sdim			goto lose;
344263508Sdim	} else {
345263508Sdim		if ((pw = getpwnam(startp+1)) == NULL)
346263508Sdim			goto lose;
347263508Sdim		home = pw->pw_dir;
348263508Sdim	}
349263508Sdim	if (*home == '\0')
350263508Sdim		goto lose;
351263508Sdim	*p = c;
352263508Sdim	if (quotes)
353263508Sdim		STPUTS_QUOTES(home, SQSYNTAX, expdest);
354263508Sdim	else
355263508Sdim		STPUTS(home, expdest);
356263508Sdim	return (p);
357263508Sdimlose:
358263508Sdim	*p = c;
359263508Sdim	return (startp);
360263508Sdim}
361263508Sdim
362263508Sdim
363263508Sdimstatic void
364263508Sdimremoverecordregions(int endoff)
365263508Sdim{
366263508Sdim	if (ifslastp == NULL)
367263508Sdim		return;
368263508Sdim
369263508Sdim	if (ifsfirst.endoff > endoff) {
370263508Sdim		while (ifsfirst.next != NULL) {
371263508Sdim			struct ifsregion *ifsp;
372263508Sdim			INTOFF;
373263508Sdim			ifsp = ifsfirst.next->next;
374263508Sdim			ckfree(ifsfirst.next);
375263508Sdim			ifsfirst.next = ifsp;
376263508Sdim			INTON;
377263508Sdim		}
378263508Sdim		if (ifsfirst.begoff > endoff)
379263508Sdim			ifslastp = NULL;
380263508Sdim		else {
381263508Sdim			ifslastp = &ifsfirst;
382263508Sdim			ifsfirst.endoff = endoff;
383263508Sdim		}
384263508Sdim		return;
385263508Sdim	}
386263508Sdim
387263508Sdim	ifslastp = &ifsfirst;
388263508Sdim	while (ifslastp->next && ifslastp->next->begoff < endoff)
389263508Sdim		ifslastp=ifslastp->next;
390	while (ifslastp->next != NULL) {
391		struct ifsregion *ifsp;
392		INTOFF;
393		ifsp = ifslastp->next->next;
394		ckfree(ifslastp->next);
395		ifslastp->next = ifsp;
396		INTON;
397	}
398	if (ifslastp->endoff > endoff)
399		ifslastp->endoff = endoff;
400}
401
402/*
403 * Expand arithmetic expression.  Backup to start of expression,
404 * evaluate, place result in (backed up) result, adjust string position.
405 */
406void
407expari(int flag)
408{
409	char *p, *q, *start;
410	arith_t result;
411	int begoff;
412	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
413	int quoted;
414
415	/*
416	 * This routine is slightly over-complicated for
417	 * efficiency.  First we make sure there is
418	 * enough space for the result, which may be bigger
419	 * than the expression.  Next we
420	 * scan backwards looking for the start of arithmetic.  If the
421	 * next previous character is a CTLESC character, then we
422	 * have to rescan starting from the beginning since CTLESC
423	 * characters have to be processed left to right.
424	 */
425	CHECKSTRSPACE(DIGITS(result) - 2, expdest);
426	USTPUTC('\0', expdest);
427	start = stackblock();
428	p = expdest - 2;
429	while (p >= start && *p != CTLARI)
430		--p;
431	if (p < start || *p != CTLARI)
432		error("missing CTLARI (shouldn't happen)");
433	if (p > start && *(p - 1) == CTLESC)
434		for (p = start; *p != CTLARI; p++)
435			if (*p == CTLESC)
436				p++;
437
438	if (p[1] == '"')
439		quoted=1;
440	else
441		quoted=0;
442	begoff = p - start;
443	removerecordregions(begoff);
444	if (quotes)
445		rmescapes(p+2);
446	q = grabstackstr(expdest);
447	result = arith(p+2);
448	ungrabstackstr(q, expdest);
449	fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result);
450	while (*p++)
451		;
452	if (quoted == 0)
453		recordregion(begoff, p - 1 - start, 0);
454	result = expdest - p + 1;
455	STADJUST(-result, expdest);
456}
457
458
459/*
460 * Perform command substitution.
461 */
462static void
463expbackq(union node *cmd, int quoted, int flag)
464{
465	struct backcmd in;
466	int i;
467	char buf[128];
468	char *p;
469	char *dest = expdest;
470	struct ifsregion saveifs, *savelastp;
471	struct nodelist *saveargbackq;
472	char lastc;
473	int startloc = dest - stackblock();
474	char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
475	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
476	int nnl;
477
478	INTOFF;
479	saveifs = ifsfirst;
480	savelastp = ifslastp;
481	saveargbackq = argbackq;
482	p = grabstackstr(dest);
483	evalbackcmd(cmd, &in);
484	ungrabstackstr(p, dest);
485	ifsfirst = saveifs;
486	ifslastp = savelastp;
487	argbackq = saveargbackq;
488
489	p = in.buf;
490	lastc = '\0';
491	nnl = 0;
492	/* Don't copy trailing newlines */
493	for (;;) {
494		if (--in.nleft < 0) {
495			if (in.fd < 0)
496				break;
497			while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
498			TRACE(("expbackq: read returns %d\n", i));
499			if (i <= 0)
500				break;
501			p = buf;
502			in.nleft = i - 1;
503		}
504		lastc = *p++;
505		if (lastc != '\0') {
506			if (lastc == '\n') {
507				nnl++;
508			} else {
509				CHECKSTRSPACE(nnl + 2, dest);
510				while (nnl > 0) {
511					nnl--;
512					USTPUTC('\n', dest);
513				}
514				if (quotes && syntax[(int)lastc] == CCTL)
515					USTPUTC(CTLESC, dest);
516				USTPUTC(lastc, dest);
517			}
518		}
519	}
520
521	if (in.fd >= 0)
522		close(in.fd);
523	if (in.buf)
524		ckfree(in.buf);
525	if (in.jp)
526		exitstatus = waitforjob(in.jp, (int *)NULL);
527	if (quoted == 0)
528		recordregion(startloc, dest - stackblock(), 0);
529	TRACE(("expbackq: size=%td: \"%.*s\"\n",
530		((dest - stackblock()) - startloc),
531		(int)((dest - stackblock()) - startloc),
532		stackblock() + startloc));
533	expdest = dest;
534	INTON;
535}
536
537
538
539static int
540subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
541  int varflags, int quotes)
542{
543	char *startp;
544	char *loc = NULL;
545	char *q;
546	int c = 0;
547	struct nodelist *saveargbackq = argbackq;
548	int amount;
549
550	argstr(p, (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
551	    subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX ?
552	    EXP_CASE : 0) | EXP_TILDE);
553	STACKSTRNUL(expdest);
554	argbackq = saveargbackq;
555	startp = stackblock() + startloc;
556	if (str == NULL)
557	    str = stackblock() + strloc;
558
559	switch (subtype) {
560	case VSASSIGN:
561		setvar(str, startp, 0);
562		amount = startp - expdest;
563		STADJUST(amount, expdest);
564		varflags &= ~VSNUL;
565		return 1;
566
567	case VSQUESTION:
568		if (*p != CTLENDVAR) {
569			outfmt(out2, "%s\n", startp);
570			error((char *)NULL);
571		}
572		error("%.*s: parameter %snot set", (int)(p - str - 1),
573		      str, (varflags & VSNUL) ? "null or "
574					      : nullstr);
575		return 0;
576
577	case VSTRIMLEFT:
578		for (loc = startp; loc < str; loc++) {
579			c = *loc;
580			*loc = '\0';
581			if (patmatch(str, startp, quotes)) {
582				*loc = c;
583				goto recordleft;
584			}
585			*loc = c;
586			if (quotes && *loc == CTLESC)
587				loc++;
588		}
589		return 0;
590
591	case VSTRIMLEFTMAX:
592		for (loc = str - 1; loc >= startp;) {
593			c = *loc;
594			*loc = '\0';
595			if (patmatch(str, startp, quotes)) {
596				*loc = c;
597				goto recordleft;
598			}
599			*loc = c;
600			loc--;
601			if (quotes && loc > startp && *(loc - 1) == CTLESC) {
602				for (q = startp; q < loc; q++)
603					if (*q == CTLESC)
604						q++;
605				if (q > loc)
606					loc--;
607			}
608		}
609		return 0;
610
611	case VSTRIMRIGHT:
612		for (loc = str - 1; loc >= startp;) {
613			if (patmatch(str, loc, quotes)) {
614				amount = loc - expdest;
615				STADJUST(amount, expdest);
616				return 1;
617			}
618			loc--;
619			if (quotes && loc > startp && *(loc - 1) == CTLESC) {
620				for (q = startp; q < loc; q++)
621					if (*q == CTLESC)
622						q++;
623				if (q > loc)
624					loc--;
625			}
626		}
627		return 0;
628
629	case VSTRIMRIGHTMAX:
630		for (loc = startp; loc < str - 1; loc++) {
631			if (patmatch(str, loc, quotes)) {
632				amount = loc - expdest;
633				STADJUST(amount, expdest);
634				return 1;
635			}
636			if (quotes && *loc == CTLESC)
637				loc++;
638		}
639		return 0;
640
641
642	default:
643		abort();
644	}
645
646recordleft:
647	amount = ((str - 1) - (loc - startp)) - expdest;
648	STADJUST(amount, expdest);
649	while (loc != str - 1)
650		*startp++ = *loc++;
651	return 1;
652}
653
654
655/*
656 * Expand a variable, and return a pointer to the next character in the
657 * input string.
658 */
659
660static char *
661evalvar(char *p, int flag)
662{
663	int subtype;
664	int varflags;
665	char *var;
666	char *val;
667	int patloc;
668	int c;
669	int set;
670	int special;
671	int startloc;
672	int varlen;
673	int varlenb;
674	int easy;
675	int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
676
677	varflags = (unsigned char)*p++;
678	subtype = varflags & VSTYPE;
679	var = p;
680	special = 0;
681	if (! is_name(*p))
682		special = 1;
683	p = strchr(p, '=') + 1;
684again: /* jump here after setting a variable with ${var=text} */
685	if (varflags & VSLINENO) {
686		set = 1;
687		special = 0;
688		val = var;
689		p[-1] = '\0';	/* temporarily overwrite '=' to have \0
690				   terminated string */
691	} else if (special) {
692		set = varisset(var, varflags & VSNUL);
693		val = NULL;
694	} else {
695		val = bltinlookup(var, 1);
696		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
697			val = NULL;
698			set = 0;
699		} else
700			set = 1;
701	}
702	varlen = 0;
703	startloc = expdest - stackblock();
704	if (!set && uflag && *var != '@' && *var != '*') {
705		switch (subtype) {
706		case VSNORMAL:
707		case VSTRIMLEFT:
708		case VSTRIMLEFTMAX:
709		case VSTRIMRIGHT:
710		case VSTRIMRIGHTMAX:
711		case VSLENGTH:
712			error("%.*s: parameter not set", (int)(p - var - 1),
713			    var);
714		}
715	}
716	if (set && subtype != VSPLUS) {
717		/* insert the value of the variable */
718		if (special) {
719			varvalue(var, varflags & VSQUOTE, subtype, flag);
720			if (subtype == VSLENGTH) {
721				varlenb = expdest - stackblock() - startloc;
722				varlen = varlenb;
723				if (localeisutf8) {
724					val = stackblock() + startloc;
725					for (;val != expdest; val++)
726						if ((*val & 0xC0) == 0x80)
727							varlen--;
728				}
729				STADJUST(-varlenb, expdest);
730			}
731		} else {
732			char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
733								  : BASESYNTAX;
734
735			if (subtype == VSLENGTH) {
736				for (;*val; val++)
737					if (!localeisutf8 ||
738					    (*val & 0xC0) != 0x80)
739						varlen++;
740			}
741			else {
742				if (quotes)
743					STPUTS_QUOTES(val, syntax, expdest);
744				else
745					STPUTS(val, expdest);
746
747			}
748		}
749	}
750
751	if (subtype == VSPLUS)
752		set = ! set;
753
754	easy = ((varflags & VSQUOTE) == 0 ||
755		(*var == '@' && shellparam.nparam != 1));
756
757
758	switch (subtype) {
759	case VSLENGTH:
760		expdest = cvtnum(varlen, expdest);
761		goto record;
762
763	case VSNORMAL:
764		if (!easy)
765			break;
766record:
767		recordregion(startloc, expdest - stackblock(),
768		    varflags & VSQUOTE || (ifsset() && ifsval()[0] == '\0' &&
769		    (*var == '@' || *var == '*')));
770		break;
771
772	case VSPLUS:
773	case VSMINUS:
774		if (!set) {
775			argstr(p, flag | (flag & EXP_FULL ? EXP_SPLIT_LIT : 0) |
776			    (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0));
777			break;
778		}
779		if (easy)
780			goto record;
781		break;
782
783	case VSTRIMLEFT:
784	case VSTRIMLEFTMAX:
785	case VSTRIMRIGHT:
786	case VSTRIMRIGHTMAX:
787		if (!set)
788			break;
789		/*
790		 * Terminate the string and start recording the pattern
791		 * right after it
792		 */
793		STPUTC('\0', expdest);
794		patloc = expdest - stackblock();
795		if (subevalvar(p, NULL, patloc, subtype,
796		    startloc, varflags, quotes) == 0) {
797			int amount = (expdest - stackblock() - patloc) + 1;
798			STADJUST(-amount, expdest);
799		}
800		/* Remove any recorded regions beyond start of variable */
801		removerecordregions(startloc);
802		goto record;
803
804	case VSASSIGN:
805	case VSQUESTION:
806		if (!set) {
807			if (subevalvar(p, var, 0, subtype, startloc, varflags,
808			    quotes)) {
809				varflags &= ~VSNUL;
810				/*
811				 * Remove any recorded regions beyond
812				 * start of variable
813				 */
814				removerecordregions(startloc);
815				goto again;
816			}
817			break;
818		}
819		if (easy)
820			goto record;
821		break;
822
823	case VSERROR:
824		c = p - var - 1;
825		error("${%.*s%s}: Bad substitution", c, var,
826		    (c > 0 && *p != CTLENDVAR) ? "..." : "");
827
828	default:
829		abort();
830	}
831	p[-1] = '=';	/* recover overwritten '=' */
832
833	if (subtype != VSNORMAL) {	/* skip to end of alternative */
834		int nesting = 1;
835		for (;;) {
836			if ((c = *p++) == CTLESC)
837				p++;
838			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
839				if (set)
840					argbackq = argbackq->next;
841			} else if (c == CTLVAR) {
842				if ((*p++ & VSTYPE) != VSNORMAL)
843					nesting++;
844			} else if (c == CTLENDVAR) {
845				if (--nesting == 0)
846					break;
847			}
848		}
849	}
850	return p;
851}
852
853
854
855/*
856 * Test whether a specialized variable is set.
857 */
858
859static int
860varisset(char *name, int nulok)
861{
862
863	if (*name == '!')
864		return backgndpidset();
865	else if (*name == '@' || *name == '*') {
866		if (*shellparam.p == NULL)
867			return 0;
868
869		if (nulok) {
870			char **av;
871
872			for (av = shellparam.p; *av; av++)
873				if (**av != '\0')
874					return 1;
875			return 0;
876		}
877	} else if (is_digit(*name)) {
878		char *ap;
879		int num = atoi(name);
880
881		if (num > shellparam.nparam)
882			return 0;
883
884		if (num == 0)
885			ap = arg0;
886		else
887			ap = shellparam.p[num - 1];
888
889		if (nulok && (ap == NULL || *ap == '\0'))
890			return 0;
891	}
892	return 1;
893}
894
895static void
896strtodest(const char *p, int flag, int subtype, int quoted)
897{
898	if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH)
899		STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
900	else
901		STPUTS(p, expdest);
902}
903
904/*
905 * Add the value of a specialized variable to the stack string.
906 */
907
908static void
909varvalue(char *name, int quoted, int subtype, int flag)
910{
911	int num;
912	char *p;
913	int i;
914	char sep;
915	char **ap;
916
917	switch (*name) {
918	case '$':
919		num = rootpid;
920		goto numvar;
921	case '?':
922		num = oexitstatus;
923		goto numvar;
924	case '#':
925		num = shellparam.nparam;
926		goto numvar;
927	case '!':
928		num = backgndpidval();
929numvar:
930		expdest = cvtnum(num, expdest);
931		break;
932	case '-':
933		for (i = 0 ; i < NOPTS ; i++) {
934			if (optlist[i].val)
935				STPUTC(optlist[i].letter, expdest);
936		}
937		break;
938	case '@':
939		if (flag & EXP_FULL && quoted) {
940			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
941				strtodest(p, flag, subtype, quoted);
942				if (*ap)
943					STPUTC('\0', expdest);
944			}
945			break;
946		}
947		/* FALLTHROUGH */
948	case '*':
949		if (ifsset())
950			sep = ifsval()[0];
951		else
952			sep = ' ';
953		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
954			strtodest(p, flag, subtype, quoted);
955			if (!*ap)
956				break;
957			if (sep || (flag & EXP_FULL && !quoted && **ap != '\0'))
958				STPUTC(sep, expdest);
959		}
960		break;
961	case '0':
962		p = arg0;
963		strtodest(p, flag, subtype, quoted);
964		break;
965	default:
966		if (is_digit(*name)) {
967			num = atoi(name);
968			if (num > 0 && num <= shellparam.nparam) {
969				p = shellparam.p[num - 1];
970				strtodest(p, flag, subtype, quoted);
971			}
972		}
973		break;
974	}
975}
976
977
978
979/*
980 * Record the fact that we have to scan this region of the
981 * string for IFS characters.
982 */
983
984static void
985recordregion(int start, int end, int inquotes)
986{
987	struct ifsregion *ifsp;
988
989	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 */
1021static void
1022ifsbreakup(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
1121static 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 */
1129static void
1130expandmeta(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			 */
1156nometa:
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
1176static void
1177expmeta(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
1318static void
1319addfname(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
1339static struct strlist *
1340expsort(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
1352static struct strlist *
1353msort(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
1394static wchar_t
1395get_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 */
1418static int
1419match_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
1445static int
1446patmatch(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		}
1583dft:	        default:
1584			if (squoted && *q == CTLESC)
1585				q++;
1586			if (*q == '\0')
1587				return 0;
1588			if (*q++ == c)
1589				break;
1590backtrack:
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
1616void
1617rmescapes(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
1645int
1646casematch(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
1668static char *
1669cvtnum(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
1692int
1693wordexpcmd(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