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