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