Home | History | Annotate | Line # | Download | only in sh
      1 /*	$NetBSD: expand.c,v 1.147 2025/05/07 14:01:01 kre Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)expand.c	8.5 (Berkeley) 5/15/95";
     39 #else
     40 __RCSID("$NetBSD: expand.c,v 1.147 2025/05/07 14:01:01 kre Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 #include <sys/time.h>
     46 #include <sys/stat.h>
     47 #include <errno.h>
     48 #include <dirent.h>
     49 #include <unistd.h>
     50 #include <pwd.h>
     51 #include <limits.h>
     52 #include <stdlib.h>
     53 #include <stdio.h>
     54 #include <wctype.h>
     55 #include <wchar.h>
     56 
     57 /*
     58  * Routines to expand arguments to commands.  We have to deal with
     59  * backquotes, shell variables, and file metacharacters.
     60  */
     61 
     62 #include "shell.h"
     63 #include "main.h"
     64 #include "nodes.h"
     65 #include "eval.h"
     66 #include "expand.h"
     67 #include "syntax.h"
     68 #include "arithmetic.h"
     69 #include "parser.h"
     70 #include "jobs.h"
     71 #include "options.h"
     72 #include "builtins.h"
     73 #include "var.h"
     74 #include "input.h"
     75 #include "output.h"
     76 #include "memalloc.h"
     77 #include "error.h"
     78 #include "mystring.h"
     79 #include "show.h"
     80 
     81 /*
     82  * Structure specifying which parts of the string should be searched
     83  * for IFS characters.
     84  */
     85 
     86 struct ifsregion {
     87 	struct ifsregion *next;	/* next region in list */
     88 	int begoff;		/* offset of start of region */
     89 	int endoff;		/* offset of end of region */
     90 	int inquotes;		/* search for nul bytes only */
     91 };
     92 
     93 
     94 char *expdest;			/* output of current string */
     95 struct nodelist *argbackq;	/* list of back quote expressions */
     96 struct ifsregion ifsfirst;	/* first struct in list of ifs regions */
     97 struct ifsregion *ifslastp;	/* last struct in list */
     98 struct arglist exparg;		/* holds expanded arg list */
     99 
    100 static int empty_dollar_at;	/* have expanded "$@" to nothing */
    101 
    102 STATIC const char *argstr(const char *, int);
    103 STATIC const char *exptilde(const char *, int);
    104 STATIC void expbackq(union node *, int, int);
    105 STATIC const char *expari(const char *);
    106 STATIC int subevalvar(const char *, const char *, int, int, int);
    107 STATIC int subevalvar_trim(const char *, int, int, int, int, int);
    108 STATIC const char *evalvar(const char *, int);
    109 STATIC int varisset(const char *, int);
    110 STATIC void varvalue(const char *, int, int, int);
    111 STATIC void recordregion(int, int, int);
    112 STATIC void removerecordregions(int);
    113 STATIC void ifsbreakup(char *, struct arglist *);
    114 STATIC void ifsfree(void);
    115 STATIC void expandmeta(struct strlist *, int);
    116 STATIC void expmeta(char *, char *);
    117 STATIC void addfname(char *);
    118 STATIC struct strlist *expsort(struct strlist *);
    119 STATIC struct strlist *msort(struct strlist *, int);
    120 STATIC int patmatch(const char *, const char *, int);
    121 STATIC char *cvtnum(int, char *);
    122 static int collate_range_cmp(wchar_t, wchar_t);
    123 STATIC void add_args(struct strlist *);
    124 STATIC void rmescapes_nl(char *);
    125 
    126 #ifdef	DEBUG
    127 #define	NULLTERM_4_TRACE(p)	STACKSTRNUL(p)
    128 #else
    129 #define	NULLTERM_4_TRACE(p)	do { /* nothing */ } while (0)
    130 #endif
    131 
    132 #define	IS_BORING(_ch)						\
    133 	((_ch) == CTLQUOTEMARK || (_ch) == CTLQUOTEEND || (_ch) == CTLNONL)
    134 #define	SKIP_BORING(p)						\
    135 	do {							\
    136 		char _ch;					\
    137 								\
    138 		while ((_ch = *(p)), IS_BORING(_ch))		\
    139 			(p)++;					\
    140 	} while (0)
    141 
    142 /*
    143  * Expand shell variables and backquotes inside a here document.
    144  */
    145 
    146 char *
    147 expandhere(union node *arg)
    148 {
    149 	int len;
    150 
    151 	VTRACE(DBG_EXPAND|DBG_REDIR, ("expandhere(%p)\n", arg));
    152 	expandarg(arg, NULL, 0);
    153 	len = rmescapes(stackblock());
    154 	VTRACE(DBG_EXPAND|DBG_REDIR, ("expandhere() -> %d\n", len));
    155 	return stalloc(len + 1);	/* include the \0 */
    156 }
    157 
    158 
    159 static int
    160 collate_range_cmp(wchar_t c1, wchar_t c2)
    161 {
    162 	wchar_t s1[2], s2[2];
    163 
    164 	s1[0] = c1;
    165 	s1[1] = L'\0';
    166 	s2[0] = c2;
    167 	s2[1] = L'\0';
    168 	return (wcscoll(s1, s2));
    169 }
    170 
    171 /*
    172  * Perform variable substitution and command substitution on an argument,
    173  * placing the resulting list of arguments in arglist.  If EXP_FULL is true,
    174  * perform splitting and file name expansion.  When arglist is NULL, perform
    175  * here document expansion.
    176  */
    177 
    178 void
    179 expandarg(union node *arg, struct arglist *arglist, int flag)
    180 {
    181 	struct strlist *sp;
    182 	char *p;
    183 
    184 	CTRACE(DBG_EXPAND, ("expandarg(fl=%#x)\n", flag));
    185 	if (fflag)		/* no filename expandsion */
    186 		flag &= ~EXP_GLOB;
    187 
    188 	empty_dollar_at = 0;
    189 	argbackq = arg->narg.backquote;
    190 	STARTSTACKSTR(expdest);
    191 	ifsfirst.next = NULL;
    192 	ifslastp = NULL;
    193 	line_number = arg->narg.lineno;
    194 	argstr(arg->narg.text, flag);
    195 	if (arglist == NULL) {
    196 		STACKSTRNUL(expdest);
    197 		CTRACE(DBG_EXPAND,
    198 		    ("expandarg: no arglist, done[%d] (len %d) \"%s\"\n",
    199 		    back_exitstatus, expdest - stackblock(), stackblock()));
    200 		return;			/* here document expanded */
    201 	}
    202 	STPUTC('\0', expdest);
    203 	CTRACE(DBG_EXPAND, ("expandarg: arglist got (%d) \"%s\"\n",
    204 		    expdest - stackblock() - 1, stackblock()));
    205 	p = grabstackstr(expdest);
    206 	exparg.lastp = &exparg.list;
    207 	/*
    208 	 * TODO - EXP_REDIR
    209 	 */
    210 	if (flag & EXP_SPLIT) {
    211 		ifsbreakup(p, &exparg);
    212 		*exparg.lastp = NULL;
    213 		exparg.lastp = &exparg.list;
    214 		if (flag & EXP_GLOB)
    215 			expandmeta(exparg.list, flag);
    216 		else
    217 			add_args(exparg.list);
    218 #if 0
    219 	} else if (flag & EXP_REDIR) {
    220 		/* if EXP_REDIR ever happens, it happens here */
    221 		/* for now just (below) remove escapes, and leave it alone */
    222 #endif
    223 	} else {
    224 		rmescapes(p);	/* we might have escaped CTL bytes to remove */
    225 		sp = stalloc(sizeof(*sp));
    226 		sp->text = p;
    227 		*exparg.lastp = sp;
    228 		exparg.lastp = &sp->next;
    229 	}
    230 	ifsfree();
    231 	*exparg.lastp = NULL;
    232 	if (exparg.list) {
    233 		*arglist->lastp = exparg.list;
    234 		arglist->lastp = exparg.lastp;
    235 	}
    236 }
    237 
    238 
    239 
    240 /*
    241  * Perform variable and command substitution.
    242  * If EXP_GLOB is set, output CTLESC characters to allow for further processing.
    243  * If EXP_SPLIT is set, remember location of result for later,
    244  * Otherwise treat $@ like $* since no splitting will be performed.
    245  */
    246 
    247 STATIC const char *
    248 argstr(const char *p, int flag)
    249 {
    250 	char c;
    251 	const int quotes = flag & EXP_QNEEDED;		/* do CTLESC */
    252 	int firsteq = 1;
    253 	int had_dol_at = 0;
    254 	int startoff;
    255 	const char *ifs = NULL;
    256 	int ifs_split = EXP_IFS_SPLIT;
    257 
    258 	if (flag & EXP_IFS_SPLIT)
    259 		ifs = ifsval();
    260 
    261 	CTRACE(DBG_EXPAND, ("argstr(\"%s\", %#x) quotes=%#x\n", p,flag,quotes));
    262 
    263 	startoff = expdest - stackblock();
    264 	if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
    265 		p = exptilde(p, flag);
    266 	for (;;) {
    267 		switch (c = *p++) {
    268 		case '\0':
    269 			NULLTERM_4_TRACE(expdest);
    270 			VTRACE(DBG_EXPAND, ("argstr returning at \"\" "
    271 			   "added \"%s\" to expdest\n", stackblock()));
    272 			return p - 1;
    273 		case CTLENDVAR: /* end of expanding yyy in ${xxx-yyy} */
    274 		case CTLENDARI: /* end of a $(( )) string */
    275 			if (had_dol_at && *p == CTLQUOTEEND)
    276 				p++;
    277 			NULLTERM_4_TRACE(expdest);
    278 			VTRACE(DBG_EXPAND, ("argstr returning at \"%.6s\"..."
    279 			    " after %2.2X; added \"%s\" to expdest\n",
    280 			    p, (c & 0xff), stackblock()));
    281 			return p;
    282 		case CTLQUOTEMARK:
    283 			/* "$@" syntax adherence hack */
    284 			if (p[0] == CTLVAR && p[1] & VSQUOTE &&
    285 			    p[2] == '@' && p[3] == '=') {
    286 				had_dol_at = 1;
    287 				break;
    288 			}
    289 			had_dol_at = 0;
    290 			empty_dollar_at = 0;
    291 			if ((flag & EXP_SPLIT) != 0)
    292 				STPUTC(c, expdest);
    293 			ifs_split = 0;
    294 			break;
    295 		case CTLNONL:
    296 			if (flag & EXP_NL)
    297 				STPUTC(c, expdest);
    298 			line_number++;
    299 			break;
    300 		case CTLCNL:
    301 			STPUTC('\n', expdest);	/* no line_number++ */
    302 			break;
    303 		case CTLQUOTEEND:
    304 			if (empty_dollar_at &&
    305 			    expdest - stackblock() > startoff &&
    306 			    expdest[-1] == CTLQUOTEMARK)
    307 				expdest--;
    308 			else if (!had_dol_at && (flag & EXP_SPLIT) != 0)
    309 				STPUTC(c, expdest);
    310 			ifs_split = EXP_IFS_SPLIT;
    311 			had_dol_at = 0;
    312 			break;
    313 		case CTLESC:
    314 			if (quotes || ISCTL(*p))
    315 				STPUTC(c, expdest);
    316 			c = *p++;
    317 			STPUTC(c, expdest);
    318 			if (c == '\n')		/* should not happen, but ... */
    319 				line_number++;
    320 			break;
    321 		case CTLVAR: {
    322 #ifdef DEBUG
    323 			unsigned int pos = expdest - stackblock();
    324 			NULLTERM_4_TRACE(expdest);
    325 #endif
    326 			p = evalvar(p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split));
    327 			NULLTERM_4_TRACE(expdest);
    328 			VTRACE(DBG_EXPAND, ("argstr evalvar "
    329 			   "added %zd \"%s\" to expdest\n",
    330 			   (size_t)(expdest - (stackblock() + pos)),
    331 			   stackblock() + pos));
    332 			break;
    333 		}
    334 		case CTLBACKQ:
    335 		case CTLBACKQ|CTLQUOTE: {
    336 #ifdef DEBUG
    337 			unsigned int pos = expdest - stackblock();
    338 #endif
    339 			expbackq(argbackq->n, c & CTLQUOTE, flag);
    340 			argbackq = argbackq->next;
    341 			NULLTERM_4_TRACE(expdest);
    342 			VTRACE(DBG_EXPAND, ("argstr expbackq added \"%s\" "
    343 			   "to expdest\n", stackblock() + pos));
    344 			break;
    345 		}
    346 		case CTLARI: {
    347 #ifdef DEBUG
    348 			unsigned int pos = expdest - stackblock();
    349 #endif
    350 			p = expari(p);
    351 			NULLTERM_4_TRACE(expdest);
    352 			VTRACE(DBG_EXPAND, ("argstr expari "
    353 			   "+ \"%s\" to expdest p=\"%.5s...\"\n",
    354 			   stackblock() + pos, p));
    355 			break;
    356 		}
    357 		case ':':
    358 		case '=':
    359 			/*
    360 			 * sort of a hack - expand tildes in variable
    361 			 * assignments (after the first '=' and after ':'s).
    362 			 */
    363 			STPUTC(c, expdest);
    364 			if (flag & EXP_VARTILDE && *p == '~') {
    365 				if (c == '=') {
    366 					if (firsteq)
    367 						firsteq = 0;
    368 					else
    369 						break;
    370 				}
    371 				p = exptilde(p, flag);
    372 			}
    373 			break;
    374 		default:
    375 			if (c == '\n')
    376 				line_number++;
    377 			STPUTC(c, expdest);
    378 			if (flag & ifs_split && strchr(ifs, c) != NULL) {
    379 				/* We need to get the output split here... */
    380 				recordregion(expdest - stackblock() - 1,
    381 						expdest - stackblock(), 0);
    382 			}
    383 			break;
    384 		}
    385 	}
    386 }
    387 
    388 STATIC const char *
    389 exptilde(const char *p, int flag)
    390 {
    391 	char c, last;
    392 	const char *startp = p;
    393 	struct passwd *pw;
    394 	const char *home;
    395 	const int quotes = flag & EXP_QNEEDED;
    396 	char *user;
    397 	struct stackmark smark;
    398 #ifdef DEBUG
    399 	unsigned int offs = expdest - stackblock();
    400 #endif
    401 
    402 	setstackmark(&smark);
    403 	(void) grabstackstr(expdest);
    404 	user = stackblock();		/* we will just borrow top of stack */
    405 
    406 	while ((c = *++p) != '\0') {
    407 		switch(c) {
    408 		case CTLESC:		/* any of these occurring */
    409 		case CTLVAR:		/* means ~ expansion */
    410 		case CTLBACKQ:		/* does not happen at all */
    411 		case CTLBACKQ | CTLQUOTE:
    412 		case CTLARI:		/* just leave original unchanged */
    413 		case CTLENDARI:
    414 		case CTLQUOTEMARK:
    415 		case '\n':
    416 			popstackmark(&smark);
    417 			return (startp);
    418 		case CTLNONL:
    419 			continue;
    420 		case ':':
    421 			if (!posix || flag & EXP_VARTILDE)
    422 				goto done;
    423 			break;
    424 		case CTLENDVAR:
    425 		case '/':
    426 			goto done;
    427 		}
    428 		STPUTC(c, user);
    429 	}
    430  done:
    431 	STACKSTRNUL(user);
    432 	user = stackblock();		/* to start of collected username */
    433 
    434 	CTRACE(DBG_EXPAND, ("exptilde, found \"~%s\"", user));
    435 	if (*user == '\0') {
    436 		home = lookupvar("HOME");
    437 		/*
    438 		 * if HOME is unset, results are unspecified...
    439 		 * we used to just leave the ~ unchanged, but
    440 		 * (some) other shells do ... and this seems more useful.
    441 		 */
    442 		if (home == NULL && (pw = getpwuid(getuid())) != NULL)
    443 			home = pw->pw_dir;
    444 	} else if ((pw = getpwnam(user)) == NULL) {
    445 		/*
    446 		 * If user does not exist, results are undefined.
    447 		 * so we can abort() here if we want, but let's not!
    448 		 */
    449 		home = NULL;
    450 	} else
    451 		home = pw->pw_dir;
    452 
    453 	VTRACE(DBG_EXPAND, (" ->\"%s\"", home ? home : "<<NULL>>"));
    454 	popstackmark(&smark);	/* now expdest is valid again */
    455 
    456 	/*
    457 	 * Posix XCU 2.6.1: The value of $HOME (for ~) or the initial
    458 	 *		working directory from getpwnam() for ~user
    459 	 * Nothing there about "except if a null string".  So do what it wants.
    460 	 * In later drafts (to become Issue 8), it is even required that in
    461 	 * this case, (where HOME='') a bare ~ expands to "" (which must not
    462 	 * be reduced to nothing).
    463 	 */
    464 	last = '\0';		/* just in case *home == '\0' (already) */
    465 	if (home == NULL) {
    466 		CTRACE(DBG_EXPAND, (": returning unused \"%s\"\n", startp));
    467 		return startp;
    468 	}
    469 	while ((c = *home++) != '\0') {
    470 		if ((quotes && NEEDESC(c)) || ISCTL(c))
    471 			STPUTC(CTLESC, expdest);
    472 		STPUTC(c, expdest);
    473 		last = c;
    474 	}
    475 
    476 	/*
    477 	 * If HOME (or whatver) ended in a '/' (last == '/'), and
    478 	 * the ~prefix was terminated by a '/', then only keep one
    479 	 * of them - since we already took the one from HOME, just
    480 	 * skip over the one that ended the tilde prefix.
    481 	 *
    482 	 * Current (Issue 8) drafts say this is permitted, and recommend
    483 	 * it - a later version of the standard will probably require it.
    484 	 * This is to prevent ~/foo generating //foo when HOME=/ (and
    485 	 * other cases like it, but that's the important one).
    486 	 */
    487 	if (last == '/' && *p == '/')
    488 		p++;
    489 
    490 	CTRACE(DBG_EXPAND, (": added %d \"%.*s\" returning \"%s\"\n",
    491 	      expdest - stackblock() - offs, expdest - stackblock() - offs,
    492 	      stackblock() + offs, p));
    493 
    494 	return (p);
    495 }
    496 
    497 
    498 STATIC void
    499 removerecordregions(int endoff)
    500 {
    501 
    502 	VTRACE(DBG_EXPAND, ("removerecordregions(%d):", endoff));
    503 	if (ifslastp == NULL) {
    504 		VTRACE(DBG_EXPAND, (" none\n", endoff));
    505 		return;
    506 	}
    507 
    508 	if (ifsfirst.endoff > endoff) {
    509 		VTRACE(DBG_EXPAND, (" first(%d)", ifsfirst.endoff));
    510 		while (ifsfirst.next != NULL) {
    511 			struct ifsregion *ifsp;
    512 			INTOFF;
    513 			ifsp = ifsfirst.next->next;
    514 			ckfree(ifsfirst.next);
    515 			ifsfirst.next = ifsp;
    516 			INTON;
    517 		}
    518 		if (ifsfirst.begoff > endoff)
    519 			ifslastp = NULL;
    520 		else {
    521 			VTRACE(DBG_EXPAND,("->(%d,%d)",ifsfirst.begoff,endoff));
    522 			ifslastp = &ifsfirst;
    523 			ifsfirst.endoff = endoff;
    524 		}
    525 		VTRACE(DBG_EXPAND, ("\n"));
    526 		return;
    527 	}
    528 
    529 	ifslastp = &ifsfirst;
    530 	while (ifslastp->next && ifslastp->next->begoff < endoff)
    531 		ifslastp=ifslastp->next;
    532 	VTRACE(DBG_EXPAND, (" found(%d,%d)", ifslastp->begoff,ifslastp->endoff));
    533 	while (ifslastp->next != NULL) {
    534 		struct ifsregion *ifsp;
    535 		INTOFF;
    536 		ifsp = ifslastp->next->next;
    537 		ckfree(ifslastp->next);
    538 		ifslastp->next = ifsp;
    539 		INTON;
    540 	}
    541 	if (ifslastp->endoff > endoff)
    542 		ifslastp->endoff = endoff;
    543 	VTRACE(DBG_EXPAND, ("->(%d,%d)", ifslastp->begoff,ifslastp->endoff));
    544 }
    545 
    546 
    547 /*
    548  * Expand arithmetic expression.
    549  *
    550  * In this incarnation, we start at the beginning (yes, "Let's start at the
    551  * very beginning.  A very good place to start.") and collect the expression
    552  * until the end - which means expanding anything contained within.
    553  *
    554  * Fortunately, argstr() just happens to do that for us...
    555  */
    556 STATIC const char *
    557 expari(const char *p)
    558 {
    559 	char *q, *start;
    560 	intmax_t result;
    561 	int adjustment;
    562 	int begoff;
    563 	int quoted;
    564 	struct stackmark smark;
    565 
    566 	/*	ifsfree(); */
    567 
    568 	/*
    569 	 * SPACE_NEEDED is enough for all possible digits (rounded up)
    570 	 * plus possible "-", and the terminating '\0', hence, plus 2
    571 	 *
    572 	 * The calculation produces the number of bytes needed to
    573 	 * represent the biggest possible value, in octal.  We only
    574 	 * generate decimal, which takes (often) less digits (never more)
    575 	 * so this is safe, if occasionally slightly wasteful.
    576 	 */
    577 #define SPACE_NEEDED ((int)((sizeof(intmax_t) * CHAR_BIT + 2) / 3 + 2))
    578 
    579 	quoted = *p++ == '"';
    580 	begoff = expdest - stackblock();
    581 	VTRACE(DBG_EXPAND, ("expari%s: \"%s\" begoff %d\n",
    582 	    quoted ? "(quoted)" : "", p, begoff));
    583 
    584 	p = argstr(p, EXP_NL);			/* expand $(( )) string */
    585 	STPUTC('\0', expdest);
    586 	start = stackblock() + begoff;
    587 
    588 	removerecordregions(begoff);		/* nothing there is kept */
    589 	rmescapes_nl(start);		/* convert CRTNONL back into \n's */
    590 
    591 	setstackmark(&smark);
    592 	q = grabstackstr(expdest);	/* keep the expression while eval'ing */
    593 	result = arith(start, line_number);
    594 	popstackmark(&smark);		/* return the stack to before grab */
    595 
    596 	start = stackblock() + begoff;		/* block may have moved */
    597 	adjustment = expdest - start;
    598 	STADJUST(-adjustment, expdest);		/* remove the argstr() result */
    599 
    600 	CHECKSTRSPACE(SPACE_NEEDED, expdest);	/* nb: stack block might move */
    601 	fmtstr(expdest, SPACE_NEEDED, "%"PRIdMAX, result);
    602 
    603 	for (q = expdest; *q++ != '\0'; )	/* find end of what we added */
    604 		;
    605 
    606 	if (quoted == 0)			/* allow weird splitting */
    607 		recordregion(begoff, begoff + q - 1 - expdest, 0);
    608 	adjustment = q - expdest - 1;
    609 	STADJUST(adjustment, expdest);		/* move expdest to end */
    610 	VTRACE(DBG_EXPAND, ("expari: adding %d \"%s\", returning \"%.5s...\"\n",
    611 	    adjustment, stackblock() + begoff, p));
    612 
    613 	return p;
    614 }
    615 
    616 
    617 /*
    618  * Expand stuff in backwards quotes (these days, any command substitution).
    619  */
    620 
    621 STATIC void
    622 expbackq(union node *cmd, int quoted, int flag)
    623 {
    624 	struct backcmd in;
    625 	int i;
    626 	char buf[128];
    627 	char *p;
    628 	char *dest = expdest;	/* expdest may be reused by eval, use an alt */
    629 	struct ifsregion saveifs, *savelastp;
    630 	struct nodelist *saveargbackq;
    631 	char lastc;
    632 	int startloc = dest - stackblock();
    633 	const int quotes = flag & EXP_QNEEDED;
    634 	int nnl;
    635 	struct stackmark smark;
    636 
    637 	VTRACE(DBG_EXPAND, ("expbackq( ..., q=%d flag=%#x) have %d\n",
    638 	    quoted, flag, startloc));
    639 	INTOFF;
    640 	saveifs = ifsfirst;
    641 	savelastp = ifslastp;
    642 	saveargbackq = argbackq;
    643 
    644 	setstackmark(&smark);	/* preserve the stack */
    645 	p = grabstackstr(dest);	/* save what we have there currently */
    646 	evalbackcmd(cmd, &in);	/* evaluate the $( ) tree (using stack) */
    647 	popstackmark(&smark);	/* and return stack to when we entered */
    648 
    649 	ifsfirst = saveifs;
    650 	ifslastp = savelastp;
    651 	argbackq = saveargbackq;
    652 
    653 	p = in.buf;		/* now extract the results */
    654 	nnl = 0;		/* dropping trailing \n's */
    655 	for (;;) {
    656 		if (--in.nleft < 0) {
    657 			if (in.fd < 0)
    658 				break;
    659 			INTON;
    660 			while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR)
    661 				continue;
    662 			INTOFF;
    663 			VTRACE(DBG_EXPAND, ("expbackq: read returns %d\n", i));
    664 			if (i <= 0)
    665 				break;
    666 			p = buf;
    667 			in.nleft = i - 1;
    668 		}
    669 		lastc = *p++;
    670 		if (lastc != '\0') {
    671 			if (lastc == '\n')	/* don't save \n yet */
    672 				nnl++;		/* it might be trailing */
    673 			else {
    674 				/*
    675 				 * We have something other than \n
    676 				 *
    677 				 * Before saving it, we need to insert
    678 				 * any \n's that we have just skipped.
    679 				 */
    680 
    681 				/* XXX
    682 				 * this hack is just because our
    683 				 * CHECKSTRSPACE() is lazy, and only
    684 				 * ever grows the stack once, even
    685 				 * if that does not allocate the space
    686 				 * we requested.  ie: safe for small
    687 				 * requests, but not large ones.
    688 				 * FIXME someday...
    689 				 */
    690 				if (nnl < 20) {
    691 					CHECKSTRSPACE(nnl + 2, dest);
    692 					while (nnl > 0) {
    693 						nnl--;
    694 						USTPUTC('\n', dest);
    695 					}
    696 				} else {
    697 					/* The slower, safer, way */
    698 					while (nnl > 0) {
    699 						nnl--;
    700 						STPUTC('\n', dest);
    701 					}
    702 					CHECKSTRSPACE(2, dest);
    703 				}
    704 				if ((quotes && quoted && NEEDESC(lastc)) ||
    705 				    ISCTL(lastc))
    706 					USTPUTC(CTLESC, dest);
    707 				USTPUTC(lastc, dest);
    708 			}
    709 		}
    710 	}
    711 
    712 	if (in.fd >= 0)
    713 		close(in.fd);
    714 	if (in.buf)
    715 		ckfree(in.buf);
    716 	if (in.jp)
    717 		back_exitstatus = waitforjob(in.jp);
    718 	if (quoted == 0)
    719 		recordregion(startloc, dest - stackblock(), 0);
    720 	CTRACE(DBG_EXPAND, ("evalbackq: [%d] size=%d: \"%.*s\"\n",
    721 		back_exitstatus,
    722 		(int)((dest - stackblock()) - startloc),
    723 		(int)((dest - stackblock()) - startloc),
    724 		stackblock() + startloc));
    725 
    726 	expdest = dest;		/* all done, expdest is all ours again */
    727 	INTON;
    728 }
    729 
    730 
    731 STATIC int
    732 subevalvar(const char *p, const char *str, int subtype, int startloc,
    733     int varflags)
    734 {
    735 	char *startp;
    736 	struct nodelist *saveargbackq = argbackq;
    737 	int amount;
    738 
    739 	VTRACE(DBG_EXPAND, ("subevalvar(%d) \"%.20s\" ${%.*s} sloc=%d vf=%x\n",
    740 	    subtype, p, p-str, str, startloc, varflags));
    741 	argstr(p, subtype == VSASSIGN ? EXP_VARTILDE : EXP_TILDE);
    742 	STACKSTRNUL(expdest);
    743 	argbackq = saveargbackq;
    744 	startp = stackblock() + startloc;
    745 
    746 	switch (subtype) {
    747 	case VSASSIGN:
    748 		setvar(str, startp, 0);
    749 		amount = startp - expdest;	/* remove what argstr added */
    750 		STADJUST(amount, expdest);
    751 		varflags &= ~VSNUL;	/*XXX Huh?   What's that achieve? */
    752 		return 1;			/* go back and eval var again */
    753 
    754 	case VSQUESTION:
    755 		if (*p != CTLENDVAR) {
    756 			outfmt(&errout, "%s\n", startp);
    757 			error(NULL);
    758 		}
    759 		error("%.*s: parameter %snot set",
    760 		      (int)(p - str - 1),
    761 		      str, (varflags & VSNUL) ? "null or "
    762 					      : nullstr);
    763 		/* NOTREACHED */
    764 
    765 	default:
    766 		abort();
    767 	}
    768 }
    769 
    770 STATIC int
    771 subevalvar_trim(const char *p, int strloc, int subtype, int startloc,
    772     int varflags, int quotes)
    773 {
    774 	char *startp;
    775 	char *str;
    776 	char *loc = NULL;
    777 	char *q;
    778 	int c = 0;
    779 	struct nodelist *saveargbackq = argbackq;
    780 	int amount;
    781 
    782 	switch (subtype) {
    783 	case VSTRIMLEFT:
    784 	case VSTRIMLEFTMAX:
    785 	case VSTRIMRIGHT:
    786 	case VSTRIMRIGHTMAX:
    787 		break;
    788 	default:
    789 		abort();
    790 		break;
    791 	}
    792 
    793 	VTRACE(DBG_EXPAND,
    794 	("subevalvar_trim(\"%.9s\", STR@%d, SUBT=%d, start@%d, vf=%x, q=%x)\n",
    795 		p, strloc, subtype, startloc, varflags, quotes));
    796 
    797 	argstr(p, (varflags & (VSQUOTE|VSPATQ)) == VSQUOTE ? 0 : EXP_CASE);
    798 	STACKSTRNUL(expdest);
    799 	argbackq = saveargbackq;
    800 	startp = stackblock() + startloc;
    801 	str = stackblock() + strloc;
    802 
    803 	switch (subtype) {
    804 
    805 	case VSTRIMLEFT:
    806 		for (loc = startp; loc < str; loc++) {
    807 			c = *loc;
    808 			*loc = '\0';
    809 			if (patmatch(str, startp, quotes))
    810 				goto recordleft;
    811 			*loc = c;
    812 			if (quotes && *loc == CTLESC)
    813 				loc++;
    814 		}
    815 		return 0;
    816 
    817 	case VSTRIMLEFTMAX:
    818 		for (loc = str - 1; loc >= startp;) {
    819 			c = *loc;
    820 			*loc = '\0';
    821 			if (patmatch(str, startp, quotes))
    822 				goto recordleft;
    823 			*loc = c;
    824 			loc--;
    825 			if (quotes && loc > startp &&
    826 			    *(loc - 1) == CTLESC) {
    827 				for (q = startp; q < loc; q++)
    828 					if (*q == CTLESC)
    829 						q++;
    830 				if (q > loc)
    831 					loc--;
    832 			}
    833 		}
    834 		return 0;
    835 
    836 	case VSTRIMRIGHT:
    837 		for (loc = str - 1; loc >= startp;) {
    838 			if (patmatch(str, loc, quotes))
    839 				goto recordright;
    840 			loc--;
    841 			if (quotes && loc > startp &&
    842 			    *(loc - 1) == CTLESC) {
    843 				for (q = startp; q < loc; q++)
    844 					if (*q == CTLESC)
    845 						q++;
    846 				if (q > loc)
    847 					loc--;
    848 			}
    849 		}
    850 		return 0;
    851 
    852 	case VSTRIMRIGHTMAX:
    853 		for (loc = startp; loc < str - 1; loc++) {
    854 			if (patmatch(str, loc, quotes))
    855 				goto recordright;
    856 			if (quotes && *loc == CTLESC)
    857 				loc++;
    858 		}
    859 		return 0;
    860 
    861 	default:
    862 		abort();
    863 	}
    864 
    865  recordleft:
    866 	*loc = c;
    867 	amount = ((str - 1) - (loc - startp)) - expdest;
    868 	STADJUST(amount, expdest);
    869 	while (loc != str - 1)
    870 		*startp++ = *loc++;
    871 	return 1;
    872 
    873  recordright:
    874 	amount = loc - expdest;
    875 	STADJUST(amount, expdest);
    876 	STPUTC('\0', expdest);
    877 	STADJUST(-1, expdest);
    878 	return 1;
    879 }
    880 
    881 
    882 /*
    883  * Expand a variable, and return a pointer to the next character in the
    884  * input string.
    885  */
    886 
    887 STATIC const char *
    888 evalvar(const char *p, int flag)
    889 {
    890 	int subtype;
    891 	int varflags;
    892 	const char *var;
    893 	char *val;
    894 	int patloc;
    895 	int c;
    896 	int set;
    897 	int special;
    898 	int startloc;
    899 	int varlen;
    900 	int apply_ifs;
    901 	const int quotes = flag & EXP_QNEEDED;
    902 
    903 	varflags = (unsigned char)*p++;
    904 	subtype = varflags & VSTYPE;
    905 	var = p;
    906 	special = subtype != VSUNKNOWN && !is_name(*p);
    907 	p = strchr(p, '=') + 1;
    908 
    909 	CTRACE(DBG_EXPAND,
    910 	    ("evalvar \"%.*s\", flag=%#X quotes=%#X vf=%#X subtype=%X\n",
    911 	    (int)(p - var - 1), var, flag, quotes, varflags, subtype));
    912 
    913  again: /* jump here after setting a variable with ${var=text} */
    914 	if (varflags & VSLINENO) {
    915 		if (line_num.flags & VUNSET) {
    916 			set = 0;
    917 			val = NULL;
    918 		} else {
    919 			set = 1;
    920 			special = p - var;
    921 			val = NULL;
    922 		}
    923 	} else if (special) {
    924 		set = varisset(var, varflags & VSNUL);
    925 		val = NULL;
    926 		if (!set && *var == '@')
    927 			empty_dollar_at = 1;
    928 	} else {
    929 		val = lookupvar(var);
    930 		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
    931 			val = NULL;
    932 			set = 0;
    933 		} else
    934 			set = 1;
    935 	}
    936 
    937 	varlen = 0;
    938 	startloc = expdest - stackblock();
    939 
    940 	if (!set && uflag && *var != '@' && *var != '*') {
    941 		switch (subtype) {
    942 		case VSNORMAL:
    943 		case VSTRIMLEFT:
    944 		case VSTRIMLEFTMAX:
    945 		case VSTRIMRIGHT:
    946 		case VSTRIMRIGHTMAX:
    947 		case VSLENGTH:
    948 			error("%.*s: parameter not set",
    949 			    (int)(p - var - 1), var);
    950 			/* NOTREACHED */
    951 		}
    952 	}
    953 
    954 #if 0		/* no longer need this $@ evil ... */
    955 	if (!set && subtype != VSPLUS && special && *var == '@')
    956 		if (startloc > 0 && expdest[-1] == CTLQUOTEMARK)
    957 			expdest--, startloc--;
    958 #endif
    959 
    960 	if (set && subtype != VSPLUS) {
    961 		/* insert the value of the variable */
    962 		if (special) {
    963 			if (varflags & VSLINENO) {
    964 				/*
    965 				 * The LINENO hack (expansion part)
    966 				 */
    967 				while (--special > 0) {
    968 /*						not needed, it is a number...
    969 					if (quotes && NEEDESC(*var))
    970 						STPUTC(CTLESC, expdest);
    971 */
    972 					STPUTC(*var++, expdest);
    973 				}
    974 			} else
    975 				varvalue(var, varflags&VSQUOTE, subtype, flag);
    976 			if (subtype == VSLENGTH) {
    977 				varlen = expdest - stackblock() - startloc;
    978 				STADJUST(-varlen, expdest);
    979 			}
    980 		} else {
    981 
    982 			if (subtype == VSLENGTH) {
    983 				for (; *val; val++)
    984 					varlen++;
    985 			} else if (quotes && varflags & VSQUOTE) {
    986 				/*
    987 				 * If we are going to look for magic in the
    988 				 * value (quotes is set) and the expansion
    989 				 * occurs inside "" (VSQUOTE) then any char
    990 				 * that has any potential special meaning
    991 				 * needs to have that meaning suppressed,
    992 				 * so supply a CTLESC prefix for it.
    993 				 */
    994 				for (; (c = *val) != '\0'; val++) {
    995 					if (NEEDESC(c))
    996 						STPUTC(CTLESC, expdest);
    997 					STPUTC(c, expdest);
    998 				}
    999 			} else {
   1000 				/*
   1001 				 * We are going to rmescapes() later,
   1002 				 * so make sure that any data char that
   1003 				 * might be mistaken for one of our CTLxxx
   1004 				 * magic chars is protected ... always.
   1005 				 */
   1006 				for (; (c = *val) != '\0'; val++) {
   1007 					if (ISCTL(c))
   1008 						STPUTC(CTLESC, expdest);
   1009 					STPUTC(c, expdest);
   1010 				}
   1011 			}
   1012 		}
   1013 	}
   1014 
   1015 
   1016 	if (varflags & VSQUOTE) {
   1017 		if  (*var == '@' && shellparam.nparam != 1)
   1018 		    apply_ifs = 1;
   1019 		else {
   1020 		    /*
   1021 		     * Mark so that we don't apply IFS if we recurse through
   1022 		     * here expanding $bar from "${foo-$bar}".
   1023 		     */
   1024 		    flag |= EXP_IN_QUOTES;
   1025 		    apply_ifs = 0;
   1026 		}
   1027 	} else if (flag & EXP_IN_QUOTES) {
   1028 		apply_ifs = 0;
   1029 	} else
   1030 		apply_ifs = 1;
   1031 
   1032 	switch (subtype) {
   1033 	case VSLENGTH:
   1034 		expdest = cvtnum(varlen, expdest);
   1035 		break;
   1036 
   1037 	case VSNORMAL:
   1038 		break;
   1039 
   1040 	case VSPLUS:
   1041 		set = !set;
   1042 		/* FALLTHROUGH */
   1043 	case VSMINUS:
   1044 		if (!set) {
   1045 			argstr(p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0));
   1046 			/*
   1047 			 * ${x-a b c} doesn't get split, but removing the
   1048 			 * 'apply_ifs = 0' apparently breaks ${1+"$@"}..
   1049 			 * ${x-'a b' c} should generate 2 args.
   1050 			 */
   1051 			if (*p != CTLENDVAR)
   1052 			/* We should have marked stuff already */
   1053 				apply_ifs = 0;
   1054 		}
   1055 		break;
   1056 
   1057 	case VSTRIMLEFT:
   1058 	case VSTRIMLEFTMAX:
   1059 	case VSTRIMRIGHT:
   1060 	case VSTRIMRIGHTMAX:
   1061 		if (!set) {
   1062 			set = 1;  /* allow argbackq to be advanced if needed */
   1063 			break;
   1064 		}
   1065 		/*
   1066 		 * Terminate the string and start recording the pattern
   1067 		 * right after it
   1068 		 */
   1069 		STPUTC('\0', expdest);
   1070 		patloc = expdest - stackblock();
   1071 		if (subevalvar_trim(p, patloc, subtype, startloc, varflags,
   1072 		    quotes) == 0) {
   1073 			int amount = (expdest - stackblock() - patloc) + 1;
   1074 			STADJUST(-amount, expdest);
   1075 		}
   1076 		/* Remove any recorded regions beyond start of variable */
   1077 		removerecordregions(startloc);
   1078 		apply_ifs = 1;
   1079 		break;
   1080 
   1081 	case VSASSIGN:
   1082 	case VSQUESTION:
   1083 		if (set)
   1084 			break;
   1085 		if (subevalvar(p, var, subtype, startloc, varflags)) {
   1086 			/* if subevalvar() returns, it always returns 1 */
   1087 
   1088 			varflags &= ~VSNUL;
   1089 			/*
   1090 			 * Remove any recorded regions beyond
   1091 			 * start of variable
   1092 			 */
   1093 			removerecordregions(startloc);
   1094 			goto again;
   1095 		}
   1096 		apply_ifs = 0;		/* never executed */
   1097 		break;
   1098 
   1099 	case VSUNKNOWN:
   1100 		VTRACE(DBG_EXPAND,
   1101 	    	   ("evalvar \"%.*s\", unknown [%p %p] \"%.3s\" (%#2x %#2x)\n",
   1102 		    (int)(p - var - 1), var, var, p, p, p[0] & 0xFF, p[1] & 0xFF));
   1103 
   1104 		if ((p - var) <= 1)
   1105 			error("%d: unknown expansion type", line_number);
   1106 		else {
   1107 			if (*p == '#')	/* only VSUNKNOWN as a ${#var:...} */
   1108 				error("%d: ${#%.*s%c..}: unknown modifier",
   1109 				     line_number, (int)(p - var - 1),
   1110 				     var, p[1]&0xFF);
   1111 
   1112 			if (*p == CTLESC)
   1113 				p++;
   1114 			error("%d: ${%.*s%c..}: unknown modifier",
   1115 			    line_number, (int)(p - var - 1), var, (*p & 0xFF));
   1116 		}
   1117 		/* NOTREACHED */
   1118 
   1119 	default:
   1120 		abort();
   1121 	}
   1122 
   1123 	if (apply_ifs)
   1124 		recordregion(startloc, expdest - stackblock(),
   1125 			     varflags & VSQUOTE);
   1126 
   1127 	if (subtype != VSNORMAL) {	/* skip to end of alternative */
   1128 		int nesting = 1;
   1129 		for (;;) {
   1130 			if ((c = *p++) == CTLESC)
   1131 				p++;
   1132 			else if (c == CTLNONL)
   1133 				;
   1134 			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
   1135 				if (set)
   1136 					argbackq = argbackq->next;
   1137 			} else if (c == CTLVAR) {
   1138 				if ((*p++ & VSTYPE) != VSNORMAL)
   1139 					nesting++;
   1140 			} else if (c == CTLENDVAR) {
   1141 				if (--nesting == 0)
   1142 					break;
   1143 			}
   1144 		}
   1145 	}
   1146 	return p;
   1147 }
   1148 
   1149 
   1150 
   1151 /*
   1152  * Test whether a special parameter is set.
   1153  */
   1154 
   1155 STATIC int
   1156 varisset(const char *name, int nulok)
   1157 {
   1158 	if (*name == '!')
   1159 		return backgndpid != -1;
   1160 	else if (*name == '@' || *name == '*') {
   1161 		if (*shellparam.p == NULL)
   1162 			return 0;
   1163 
   1164 		if (nulok) {
   1165 			char **av;
   1166 
   1167 			for (av = shellparam.p; *av; av++)
   1168 				if (**av != '\0')
   1169 					return 1;
   1170 			return 0;
   1171 		}
   1172 	} else if (is_digit(*name)) {
   1173 		char *ap;
   1174 		long num;
   1175 
   1176 		/*
   1177 		 * handle overflow sensibly (the *ap tests should never fail)
   1178 		 */
   1179 		errno = 0;
   1180 		num = strtol(name, &ap, 10);
   1181 		if (errno != 0 || (*ap != '\0' && *ap != '='))
   1182 			return 0;
   1183 
   1184 		if (num == 0)
   1185 			ap = arg0;
   1186 		else if (num > shellparam.nparam)
   1187 			return 0;
   1188 		else
   1189 			ap = shellparam.p[num - 1];
   1190 
   1191 		if (nulok && (ap == NULL || *ap == '\0'))
   1192 			return 0;
   1193 	}
   1194 	return 1;
   1195 }
   1196 
   1197 
   1198 
   1199 /*
   1200  * Add the value of a specialized variable to the stack string.
   1201  */
   1202 
   1203 STATIC void
   1204 varvalue(const char *name, int quoted, int subtype, int flag)
   1205 {
   1206 	int num;
   1207 	char *p;
   1208 	int i;
   1209 	int sep;
   1210 	char **ap;
   1211 #ifdef DEBUG
   1212 	char *start = expdest;
   1213 #endif
   1214 
   1215 	VTRACE(DBG_EXPAND, ("varvalue(%c%s, sub=%d, fl=%#x)", *name,
   1216 	    quoted ? ", quoted" : "", subtype, flag));
   1217 
   1218 	if (subtype == VSLENGTH)	/* no magic required ... */
   1219 		flag &= ~(EXP_FULL | EXP_QNEEDED);
   1220 
   1221 #define STRTODEST(p) \
   1222 	do {\
   1223 		if ((flag & EXP_QNEEDED) && quoted) { \
   1224 			while (*p) { \
   1225 				if (NEEDESC(*p)) \
   1226 					STPUTC(CTLESC, expdest); \
   1227 				STPUTC(*p++, expdest); \
   1228 			} \
   1229 		} else \
   1230 			while (*p) { \
   1231 				if ((flag & EXP_QNEEDED) && ISCTL(*p)) \
   1232 					STPUTC(CTLESC, expdest); \
   1233 				STPUTC(*p++, expdest); \
   1234 			} \
   1235 	} while (0)
   1236 
   1237 
   1238 	switch (*name) {
   1239 	case '$':
   1240 		num = rootpid;
   1241 		break;
   1242 	case '?':
   1243 		num = exitstatus;
   1244 		break;
   1245 	case '#':
   1246 		num = shellparam.nparam;
   1247 		break;
   1248 	case '!':
   1249 		num = backgndpid;
   1250 		break;
   1251 	case '-':
   1252 		for (i = 0; i < option_flags; i++) {
   1253 			if (optlist[optorder[i]].val)
   1254 				STPUTC(optlist[optorder[i]].letter, expdest);
   1255 		}
   1256 		VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start));
   1257 		return;
   1258 	case '@':
   1259 		if (flag & EXP_SPLIT && quoted) {
   1260 			VTRACE(DBG_EXPAND, (": $@ split (%d)\n",
   1261 			    shellparam.nparam));
   1262 #if 0
   1263 		/* GROSS HACK */
   1264 			if (shellparam.nparam == 0 &&
   1265 				expdest[-1] == CTLQUOTEMARK)
   1266 					expdest--;
   1267 		/* KCAH SSORG */
   1268 #endif
   1269 			if (shellparam.nparam == 0)
   1270 				empty_dollar_at = 1;
   1271 
   1272 			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
   1273 				if (*p == '\0') {
   1274 					/* retain an explicit null string */
   1275 					STPUTC(CTLQUOTEMARK, expdest);
   1276 					STPUTC(CTLQUOTEEND, expdest);
   1277 				} else
   1278 					STRTODEST(p);
   1279 				if (*ap)
   1280 					/* A NUL separates args inside "" */
   1281 					STPUTC('\0', expdest);
   1282 			}
   1283 			return;
   1284 		}
   1285 		/* fall through */
   1286 	case '*':
   1287 		sep = ifsval()[0];
   1288 		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
   1289 			STRTODEST(p);
   1290 			if (!*ap)
   1291 				break;
   1292 			if (sep) {
   1293 				if (quoted && (flag & EXP_QNEEDED) &&
   1294 				    NEEDESC(sep))
   1295 					STPUTC(CTLESC, expdest);
   1296 				STPUTC(sep, expdest);
   1297 			} else
   1298 			    if ((flag & (EXP_SPLIT|EXP_IN_QUOTES)) == EXP_SPLIT
   1299 			      && !quoted && **ap != '\0')
   1300 				STPUTC('\0', expdest);
   1301 		}
   1302 		VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start));
   1303 		return;
   1304 	default:
   1305 		if (is_digit(*name)) {
   1306 			long lnum;
   1307 
   1308 			errno = 0;
   1309 			lnum = strtol(name, &p, 10);
   1310 			if (errno != 0 || (*p != '\0' && *p != '='))
   1311 				return;
   1312 
   1313 			if (lnum == 0)
   1314 				p = arg0;
   1315 			else if (lnum > 0 && lnum <= shellparam.nparam)
   1316 				p = shellparam.p[lnum - 1];
   1317 			else
   1318 				return;
   1319 			STRTODEST(p);
   1320 		}
   1321 		VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start));
   1322 		return;
   1323 	}
   1324 	/*
   1325 	 * only the specials with an int value arrive here
   1326 	 */
   1327 	VTRACE(DBG_EXPAND, ("(%d)", num));
   1328 	expdest = cvtnum(num, expdest);
   1329 	VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start));
   1330 }
   1331 
   1332 
   1333 
   1334 /*
   1335  * Record the fact that we have to scan this region of the
   1336  * string for IFS characters.
   1337  */
   1338 
   1339 STATIC void
   1340 recordregion(int start, int end, int inquotes)
   1341 {
   1342 	struct ifsregion *ifsp;
   1343 
   1344 	VTRACE(DBG_EXPAND, ("recordregion(%d,%d,%d)\n", start, end, inquotes));
   1345 	if (ifslastp == NULL) {
   1346 		ifsp = &ifsfirst;
   1347 	} else {
   1348 		if (ifslastp->endoff == start
   1349 		    && ifslastp->inquotes == inquotes) {
   1350 			/* extend previous area */
   1351 			ifslastp->endoff = end;
   1352 			return;
   1353 		}
   1354 		ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
   1355 		ifslastp->next = ifsp;
   1356 	}
   1357 	ifslastp = ifsp;
   1358 	ifslastp->next = NULL;
   1359 	ifslastp->begoff = start;
   1360 	ifslastp->endoff = end;
   1361 	ifslastp->inquotes = inquotes;
   1362 }
   1363 
   1364 
   1365 
   1366 /*
   1367  * Break the argument string into pieces based upon IFS and add the
   1368  * strings to the argument list.  The regions of the string to be
   1369  * searched for IFS characters have been stored by recordregion.
   1370  */
   1371 STATIC void
   1372 ifsbreakup(char *string, struct arglist *arglist)
   1373 {
   1374 	struct ifsregion *ifsp;
   1375 	struct strlist *sp;
   1376 	char *start;
   1377 	char *p;
   1378 	char *q;
   1379 	const char *ifs;
   1380 	const char *ifsspc;
   1381 	int had_param_ch = 0;
   1382 
   1383 	start = string;
   1384 
   1385 	VTRACE(DBG_EXPAND, ("ifsbreakup(\"%s\")", string)); /* misses \0's */
   1386 	if (ifslastp == NULL) {
   1387 		/* Return entire argument, IFS doesn't apply to any of it */
   1388 		VTRACE(DBG_EXPAND, ("no regions\n", string));
   1389 		sp = stalloc(sizeof(*sp));
   1390 		sp->text = start;
   1391 		*arglist->lastp = sp;
   1392 		arglist->lastp = &sp->next;
   1393 		return;
   1394 	}
   1395 
   1396 	ifs = ifsval();
   1397 
   1398 	for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
   1399 		p = string + ifsp->begoff;
   1400 		VTRACE(DBG_EXPAND, (" !%.*s!(%d)", ifsp->endoff-ifsp->begoff,
   1401 		    p, ifsp->endoff-ifsp->begoff));
   1402 		while (p < string + ifsp->endoff) {
   1403 			had_param_ch = 1;
   1404 			q = p;
   1405 			if (IS_BORING(*p)) {
   1406 				p++;
   1407 				continue;
   1408 			}
   1409 			if (*p == CTLESC)
   1410 				p++;
   1411 			if (ifsp->inquotes) {
   1412 				/* Only NULs (should be from "$@") end args */
   1413 				if (*p != 0) {
   1414 					p++;
   1415 					continue;
   1416 				}
   1417 				ifsspc = NULL;
   1418 				VTRACE(DBG_EXPAND, (" \\0 nxt:\"%s\" ", p));
   1419 			} else {
   1420 				if (!strchr(ifs, *p)) {
   1421 					p++;
   1422 					continue;
   1423 				}
   1424 				had_param_ch = 0;
   1425 				ifsspc = strchr(" \t\n", *p);
   1426 
   1427 				/* Ignore IFS whitespace at start */
   1428 				if (q == start && ifsspc != NULL) {
   1429 					p++;
   1430 					start = p;
   1431 					continue;
   1432 				}
   1433 			}
   1434 
   1435 			/* Save this argument... */
   1436 			*q = '\0';
   1437 			VTRACE(DBG_EXPAND, ("<%s>", start));
   1438 			sp = stalloc(sizeof(*sp));
   1439 			sp->text = start;
   1440 			*arglist->lastp = sp;
   1441 			arglist->lastp = &sp->next;
   1442 			p++;
   1443 
   1444 			if (ifsspc != NULL) {
   1445 				/* Ignore further trailing IFS whitespace */
   1446 				for (; p < string + ifsp->endoff; p++) {
   1447 					q = p;
   1448 					if (*p == CTLNONL)
   1449 						continue;
   1450 					if (*p == CTLESC)
   1451 						p++;
   1452 					if (strchr(ifs, *p) == NULL) {
   1453 						p = q;
   1454 						break;
   1455 					}
   1456 					if (strchr(" \t\n", *p) == NULL) {
   1457 						p++;
   1458 						break;
   1459 					}
   1460 				}
   1461 			}
   1462 			start = p;
   1463 		}
   1464 	}
   1465 
   1466 /*
   1467 	while (*start == CTLQUOTEEND)
   1468 		start++;
   1469 */
   1470 
   1471 	/*
   1472 	 * Save anything left as an argument.
   1473 	 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
   1474 	 * generating 2 arguments, the second of which is empty.
   1475 	 * Some recent clarification of the Posix spec say that it
   1476 	 * should only generate one....
   1477 	 */
   1478 	if (had_param_ch || *start != 0) {
   1479 		VTRACE(DBG_EXPAND, (" T<%s>", start));
   1480 		sp = stalloc(sizeof(*sp));
   1481 		sp->text = start;
   1482 		*arglist->lastp = sp;
   1483 		arglist->lastp = &sp->next;
   1484 	}
   1485 	VTRACE(DBG_EXPAND, ("\n"));
   1486 }
   1487 
   1488 STATIC void
   1489 ifsfree(void)
   1490 {
   1491 	while (ifsfirst.next != NULL) {
   1492 		struct ifsregion *ifsp;
   1493 		INTOFF;
   1494 		ifsp = ifsfirst.next->next;
   1495 		ckfree(ifsfirst.next);
   1496 		ifsfirst.next = ifsp;
   1497 		INTON;
   1498 	}
   1499 	ifslastp = NULL;
   1500 	ifsfirst.next = NULL;
   1501 }
   1502 
   1503 
   1504 
   1505 /*
   1506  * Expand shell metacharacters.  At this point, the only control characters
   1507  * should be escapes.  The results are stored in the list exparg.
   1508  */
   1509 
   1510 char *expdir;
   1511 
   1512 
   1513 STATIC void
   1514 expandmeta(struct strlist *str, int flag)
   1515 {
   1516 	char *p;
   1517 	struct strlist **savelastp;
   1518 	struct strlist *sp;
   1519 	char c;
   1520 	/* TODO - EXP_REDIR */
   1521 
   1522 	while (str) {
   1523 		p = str->text;
   1524 		for (;;) {			/* fast check for meta chars */
   1525 			if ((c = *p++) == '\0')
   1526 				goto nometa;
   1527 			if (c == '*' || c == '?' || c == '[' /* || c == '!' */)
   1528 				break;
   1529 		}
   1530 		savelastp = exparg.lastp;
   1531 		INTOFF;
   1532 		if (expdir == NULL) {
   1533 			int i = strlen(str->text);
   1534 			expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
   1535 		}
   1536 
   1537 		expmeta(expdir, str->text);
   1538 		ckfree(expdir);
   1539 		expdir = NULL;
   1540 		INTON;
   1541 		if (exparg.lastp == savelastp) {
   1542 			/*
   1543 			 * no matches
   1544 			 */
   1545  nometa:
   1546 			*exparg.lastp = str;
   1547 			rmescapes(str->text);
   1548 			exparg.lastp = &str->next;
   1549 		} else {
   1550 			*exparg.lastp = NULL;
   1551 			*savelastp = sp = expsort(*savelastp);
   1552 			while (sp->next != NULL)
   1553 				sp = sp->next;
   1554 			exparg.lastp = &sp->next;
   1555 		}
   1556 		str = str->next;
   1557 	}
   1558 }
   1559 
   1560 STATIC void
   1561 add_args(struct strlist *str)
   1562 {
   1563 	while (str) {
   1564 		*exparg.lastp = str;
   1565 		rmescapes(str->text);
   1566 		exparg.lastp = &str->next;
   1567 		str = str->next;
   1568 	}
   1569 }
   1570 
   1571 
   1572 /*
   1573  * Do metacharacter (i.e. *, ?, [...]) expansion.
   1574  */
   1575 
   1576 STATIC void
   1577 expmeta(char *enddir, char *name)
   1578 {
   1579 	char *p;
   1580 	const char *cp;
   1581 	char *q;
   1582 	char *start;
   1583 	char *endname;
   1584 	int metaflag;
   1585 	struct stat statb;
   1586 	DIR *dirp;
   1587 	struct dirent *dp;
   1588 	int atend;
   1589 	int matchdot;
   1590 
   1591 	CTRACE(DBG_EXPAND|DBG_MATCH, ("expmeta(\"%s\")\n", name));
   1592 	metaflag = 0;
   1593 	start = name;
   1594 	for (p = name ; ; p++) {
   1595 		if (*p == '*' || *p == '?')
   1596 			metaflag = 1;
   1597 		else if (*p == '[') {
   1598 			q = p + 1;
   1599 			if (*q == '!' || *q == '^')
   1600 				q++;
   1601 			for (;;) {
   1602 				while (IS_BORING(*q))
   1603 					q++;
   1604 				if (*q == ']') {
   1605 					q++;
   1606 					metaflag = 1;
   1607 					break;
   1608 				}
   1609 				if (*q == '[' && q[1] == ':') {
   1610 					/*
   1611 					 * character class, look for :] ending
   1612 					 * also stop on ']' (end bracket expr)
   1613 					 * or '\0' or '/' (end pattern)
   1614 					 */
   1615 					while (*++q != '\0' && *q != ']' &&
   1616 					    *q != '/') {
   1617 						if (*q == CTLESC) {
   1618 							if (*++q == '\0')
   1619 								break;
   1620 							if (*q == '/')
   1621 								break;
   1622 						} else if (*q == ':' &&
   1623 						    q[1] == ']')
   1624 							break;
   1625 					}
   1626 					if (*q == ':') {
   1627 						/*
   1628 						 * stopped at ':]'
   1629 						 * still in [...]
   1630 						 * skip ":]" and continue;
   1631 						 */
   1632 						q += 2;
   1633 						continue;
   1634 					}
   1635 
   1636 					/* done at end of pattern, not [...] */
   1637 					if (*q == '\0' || *q == '/')
   1638 						break;
   1639 
   1640 					/* found the ']', we have a [...] */
   1641 					metaflag = 1;
   1642 					q++;	/* skip ']' */
   1643 					break;
   1644 				}
   1645 				if (*q == CTLESC)
   1646 					q++;
   1647 				/* end of pattern cannot be escaped */
   1648 				if (*q == '/' || *q == '\0')
   1649 					break;
   1650 				q++;
   1651 			}
   1652 		} else if (*p == '\0')
   1653 			break;
   1654 		else if (IS_BORING(*p))
   1655 			continue;
   1656 		else if (*p == CTLESC)
   1657 			p++;
   1658 		if (*p == '/') {
   1659 			if (metaflag)
   1660 				break;
   1661 			start = p + 1;
   1662 		}
   1663 	}
   1664 	if (metaflag == 0) {	/* we've reached the end of the file name */
   1665 		if (enddir != expdir)
   1666 			metaflag++;
   1667 		for (p = name ; ; p++) {
   1668 			if (IS_BORING(*p))
   1669 				continue;
   1670 			if (*p == CTLESC)
   1671 				p++;
   1672 			*enddir++ = *p;
   1673 			if (*p == '\0')
   1674 				break;
   1675 		}
   1676 		if (metaflag == 0 || lstat(expdir, &statb) >= 0)
   1677 			addfname(expdir);
   1678 		return;
   1679 	}
   1680 	endname = p;
   1681 	if (start != name) {
   1682 		p = name;
   1683 		while (p < start) {
   1684 			while (IS_BORING(*p))
   1685 				p++;
   1686 			if (*p == CTLESC)
   1687 				p++;
   1688 			*enddir++ = *p++;
   1689 		}
   1690 	}
   1691 	if (enddir == expdir) {
   1692 		cp = ".";
   1693 	} else if (enddir == expdir + 1 && *expdir == '/') {
   1694 		cp = "/";
   1695 	} else {
   1696 		cp = expdir;
   1697 		enddir[-1] = '\0';
   1698 	}
   1699 	if ((dirp = opendir(cp)) == NULL)
   1700 		return;
   1701 	if (enddir != expdir)
   1702 		enddir[-1] = '/';
   1703 	if (*endname == 0) {
   1704 		atend = 1;
   1705 	} else {
   1706 		atend = 0;
   1707 		*endname++ = '\0';
   1708 	}
   1709 	matchdot = 0;
   1710 	p = start;
   1711 	while (IS_BORING(*p))
   1712 		p++;
   1713 	if (*p == CTLESC)
   1714 		p++;
   1715 	if (*p == '.')
   1716 		matchdot++;
   1717 	while (! int_pending() && (dp = readdir(dirp)) != NULL) {
   1718 		if (dp->d_name[0] == '.' && ! matchdot)
   1719 			continue;
   1720 		if (patmatch(start, dp->d_name, 0)) {
   1721 			if (atend) {
   1722 				scopy(dp->d_name, enddir);
   1723 				addfname(expdir);
   1724 			} else {
   1725 				for (p = enddir, cp = dp->d_name;
   1726 				     (*p++ = *cp++) != '\0';)
   1727 					continue;
   1728 				p[-1] = '/';
   1729 				expmeta(p, endname);
   1730 			}
   1731 		}
   1732 	}
   1733 	closedir(dirp);
   1734 	if (! atend)
   1735 		endname[-1] = '/';
   1736 }
   1737 
   1738 
   1739 /*
   1740  * Add a file name to the list.
   1741  */
   1742 
   1743 STATIC void
   1744 addfname(char *name)
   1745 {
   1746 	char *p;
   1747 	struct strlist *sp;
   1748 
   1749 	p = stalloc(strlen(name) + 1);
   1750 	scopy(name, p);
   1751 	sp = stalloc(sizeof(*sp));
   1752 	sp->text = p;
   1753 	*exparg.lastp = sp;
   1754 	exparg.lastp = &sp->next;
   1755 }
   1756 
   1757 
   1758 /*
   1759  * Sort the results of file name expansion.  It calculates the number of
   1760  * strings to sort and then calls msort (short for merge sort) to do the
   1761  * work.
   1762  */
   1763 
   1764 STATIC struct strlist *
   1765 expsort(struct strlist *str)
   1766 {
   1767 	int len;
   1768 	struct strlist *sp;
   1769 
   1770 	len = 0;
   1771 	for (sp = str ; sp ; sp = sp->next)
   1772 		len++;
   1773 	return msort(str, len);
   1774 }
   1775 
   1776 
   1777 STATIC struct strlist *
   1778 msort(struct strlist *list, int len)
   1779 {
   1780 	struct strlist *p, *q = NULL;
   1781 	struct strlist **lpp;
   1782 	int half;
   1783 	int n;
   1784 
   1785 	if (len <= 1)
   1786 		return list;
   1787 	half = len >> 1;
   1788 	p = list;
   1789 	for (n = half ; --n >= 0 ; ) {
   1790 		q = p;
   1791 		p = p->next;
   1792 	}
   1793 	q->next = NULL;			/* terminate first half of list */
   1794 	q = msort(list, half);		/* sort first half of list */
   1795 	p = msort(p, len - half);		/* sort second half */
   1796 	lpp = &list;
   1797 	for (;;) {
   1798 		if (strcmp(p->text, q->text) < 0) {
   1799 			*lpp = p;
   1800 			lpp = &p->next;
   1801 			if ((p = *lpp) == NULL) {
   1802 				*lpp = q;
   1803 				break;
   1804 			}
   1805 		} else {
   1806 			*lpp = q;
   1807 			lpp = &q->next;
   1808 			if ((q = *lpp) == NULL) {
   1809 				*lpp = p;
   1810 				break;
   1811 			}
   1812 		}
   1813 	}
   1814 	return list;
   1815 }
   1816 
   1817 
   1818 /*
   1819  * See if a character matches a character class, starting at the first colon
   1820  * of "[:class:]".
   1821  * If a valid character class is recognized, a pointer to the next character
   1822  * after the final closing bracket is stored into *end, otherwise a null
   1823  * pointer is stored into *end.
   1824  */
   1825 static int
   1826 match_charclass(const char *p, wchar_t chr, const char **end)
   1827 {
   1828 	char name[20];
   1829 	const char *nameend;
   1830 	wctype_t cclass;
   1831 	char *q;
   1832 
   1833 	*end = NULL;
   1834 	p++;
   1835 	q = &name[0];
   1836 	nameend = strstr(p, ":]");
   1837 	if (nameend == NULL || nameend == p)	/* not a valid class */
   1838 		return 0;
   1839 
   1840 	if (*p == CTLESC) {
   1841 		if (*++p == CTLESC)
   1842 			return 0;
   1843 		if (p == nameend)
   1844 			return 0;
   1845 	}
   1846 	if (!is_alpha(*p))
   1847 		return 0;
   1848 	while (p < nameend) {
   1849 		if (*p == CTLESC) {
   1850 			p++;
   1851 			if (p == nameend)
   1852 				return 0;
   1853 		}
   1854 		if (!is_in_name(*p))	/* '_' is a local extension */
   1855 			return 0;
   1856 		if (q < &name[sizeof name])
   1857 			*q++ = *p++;
   1858 		else
   1859 			p++;
   1860 	}
   1861 
   1862 	*end = nameend + 2;		/* committed to it being a char class */
   1863 
   1864 	if (q < &name[sizeof name])	/* a usable name found */
   1865 		*q++ = '\0';
   1866 	else				/* too long, valid, but no match */
   1867 		return 0;
   1868 
   1869 	cclass = wctype(name);
   1870 	/* An unknown class matches nothing but is valid nevertheless. */
   1871 	if (cclass == 0)
   1872 		return 0;
   1873 	return iswctype(chr, cclass);
   1874 }
   1875 
   1876 
   1877 /*
   1878  * Returns true if the pattern matches the string.
   1879  */
   1880 
   1881 STATIC int
   1882 patmatch(const char *pattern, const char *string, int squoted)
   1883 {
   1884 	const char *p, *q, *end;
   1885 	const char *bt_p, *bt_q;
   1886 	char c;
   1887 	wchar_t wc, wc2;
   1888 
   1889 	VTRACE(DBG_MATCH, ("patmatch(P=\"%s\", W=\"%s\"%s): ",
   1890 	    pattern, string, squoted ? ", SQ" : ""));
   1891 	p = pattern;
   1892 	q = string;
   1893 	bt_p = NULL;
   1894 	bt_q = NULL;
   1895 	for (;;) {
   1896 		switch (c = *p++) {
   1897 		case '\0':
   1898 			if (squoted && *q == CTLESC) {
   1899 				if (q[1] == '\0')
   1900 					q++;
   1901 			}
   1902 			if (*q != '\0')
   1903 				goto backtrack;
   1904 			VTRACE(DBG_MATCH, ("match\n"));
   1905 			return 1;
   1906 		case CTLESC:
   1907 			if (squoted && *q == CTLESC)
   1908 				q++;
   1909 			if (*p == '\0' && *q == '\0') {
   1910 				VTRACE(DBG_MATCH, ("match-\\\n"));
   1911 				return 1;
   1912 			}
   1913 			if (*q++ != *p++)
   1914 				goto backtrack;
   1915 			break;
   1916 		case '\\':
   1917 			if (squoted && *q == CTLESC)
   1918 				q++;
   1919 			if (*q++ != *p++)
   1920 				goto backtrack;
   1921 			break;
   1922 		case CTLQUOTEMARK:
   1923 		case CTLQUOTEEND:
   1924 		case CTLNONL:
   1925 			continue;
   1926 		case '?':
   1927 			if (squoted && *q == CTLESC)
   1928 				q++;
   1929 			if (*q++ == '\0') {
   1930 				VTRACE(DBG_MATCH, ("?fail\n"));
   1931 				return 0;
   1932 			}
   1933 			break;
   1934 		case '*':
   1935 			c = *p;
   1936 			while (c == CTLQUOTEMARK || c == '*')
   1937 				c = *++p;
   1938 			if (c != CTLESC && !IS_BORING(c) &&
   1939 			    c != '?' && c != '*' && c != '[') {
   1940 				while (*q != c) {
   1941 					if (squoted && *q == CTLESC &&
   1942 					    q[1] == c)
   1943 						break;
   1944 					if (*q == '\0') {
   1945 						VTRACE(DBG_MATCH, ("*fail\n"));
   1946 						return 0;
   1947 					}
   1948 					if (squoted && *q == CTLESC)
   1949 						q++;
   1950 					q++;
   1951 				}
   1952 			}
   1953 			if (c == CTLESC && p[1] == '\0') {
   1954 				VTRACE(DBG_MATCH, ("match+\\\n"));
   1955 				return 1;
   1956 			}
   1957 			/*
   1958 			 * First try the shortest match for the '*' that
   1959 			 * could work. We can forget any earlier '*' since
   1960 			 * there is no way having it match more characters
   1961 			 * can help us, given that we are already here.
   1962 			 */
   1963 			bt_p = p;
   1964 			bt_q = q;
   1965 			break;
   1966 		case '[': {
   1967 			const char *savep, *saveq, *endp;
   1968 			int invert, found;
   1969 			unsigned char chr;
   1970 
   1971 			/*
   1972 			 * First quick check to see if there is a
   1973 			 * possible matching ']' - if not, then this
   1974 			 * is not a char class, and the '[' is just
   1975 			 * a literal '['.
   1976 			 *
   1977 			 * This check will not detect all non classes, but
   1978 			 * that's OK - It just means that we execute the
   1979 			 * harder code sometimes when it it cannot succeed.
   1980 			 */
   1981 			endp = p;
   1982 			if (*endp == '!' || *endp == '^')
   1983 				endp++;
   1984 			for (;;) {
   1985 				while (IS_BORING(*endp))
   1986 					endp++;
   1987 				if (*endp == '\0')
   1988 					goto dft;	/* no matching ] */
   1989 				if (*endp++ == ']')
   1990 					break;
   1991 			}
   1992 			/* end shortcut */
   1993 
   1994 			savep = p, saveq = q;
   1995 			invert = 0;
   1996 			if (*p == '!' || *p == '^') {
   1997 				invert++;
   1998 				p++;
   1999 			}
   2000 			found = 0;
   2001 			if (*q == '\0') {
   2002 				VTRACE(DBG_MATCH, ("[]fail\n"));
   2003 				return 0;
   2004 			}
   2005 			if (squoted && *q == CTLESC)
   2006 				q++;
   2007 			chr = (unsigned char)*q++;
   2008 			c = *p++;
   2009 			do {
   2010 				if (IS_BORING(c))
   2011 					continue;
   2012 				if (c == '\0') {
   2013 					p = savep, q = saveq;
   2014 					c = '[';
   2015 					goto dft;
   2016 				}
   2017 				if (c == '[' && *p == ':') {
   2018 					found |= match_charclass(p, chr, &end);
   2019 					if (end != NULL) {
   2020 						p = end;
   2021 						continue;
   2022 					}
   2023 				}
   2024 				if (c == CTLESC || c == '\\')
   2025 					c = *p++;
   2026 				wc = (unsigned char)c;
   2027 				if (*p == '-' && p[1] != ']') {
   2028 					p++;
   2029 					if (*p == CTLESC || *p == '\\')
   2030 						p++;
   2031 					wc2 = (unsigned char)*p++;
   2032 					if (   collate_range_cmp(chr, wc) >= 0
   2033 					    && collate_range_cmp(chr, wc2) <= 0
   2034 					   )
   2035 						found = 1;
   2036 				} else {
   2037 					if (chr == wc)
   2038 						found = 1;
   2039 				}
   2040 			} while ((c = *p++) != ']');
   2041 			if (found == invert)
   2042 				goto backtrack;
   2043 			break;
   2044 		}
   2045   dft:		default:
   2046 			if (squoted && *q == CTLESC)
   2047 				q++;
   2048 			if (*q++ == c)
   2049 				break;
   2050   backtrack:
   2051 			/*
   2052 			 * If we have a mismatch (other than hitting the end
   2053 			 * of the string), go back to the last '*' seen and
   2054 			 * have it match one additional character.
   2055 			 */
   2056 			if (bt_p == NULL) {
   2057 				VTRACE(DBG_MATCH, ("BTP fail\n"));
   2058 				return 0;
   2059 			}
   2060 			if (*bt_q == '\0') {
   2061 				VTRACE(DBG_MATCH, ("BTQ fail\n"));
   2062 				return 0;
   2063 			}
   2064 			bt_q++;
   2065 			p = bt_p;
   2066 			q = bt_q;
   2067 			break;
   2068 		}
   2069 	}
   2070 }
   2071 
   2072 
   2073 
   2074 /*
   2075  * Remove any CTLESC or CTLNONL characters from a string.
   2076  *
   2077  * String is modified in place, and we return the length of the result
   2078  */
   2079 
   2080 int
   2081 rmescapes(char *str)
   2082 {
   2083 	char *p, *q;
   2084 
   2085 	p = str;
   2086 	while (!ISCTL(*p)) {
   2087 		if (*p++ == '\0')
   2088 			return ((int)(p - str) - 1);
   2089 	}
   2090 	q = p;
   2091 	while (*p) {
   2092 		if (IS_BORING(*p)) {
   2093 			p++;
   2094 			continue;
   2095 		}
   2096 		if (*p == CTLCNL) {
   2097 			p++;
   2098 			*q++ = '\n';
   2099 			continue;
   2100 		}
   2101 		if (*p == CTLESC)
   2102 			p++;
   2103 #ifdef DEBUG
   2104 		else if (ISCTL(*p))
   2105 			abort();
   2106 #endif
   2107 		*q++ = *p++;
   2108 	}
   2109 	*q = '\0';
   2110 
   2111 	return ((int)(q - str));
   2112 }
   2113 
   2114 /*
   2115  * and a special version for dealing with expressions to be parsed
   2116  * by the arithmetic evaluator.   That needs to be able to count \n's
   2117  * even ones that were \newline elided \n's, so we have to put the
   2118  * latter back into the string - just being careful to put them only
   2119  * at a place where white space can reasonably occur in the string
   2120  * -- then the \n we insert will just be white space, and ignored
   2121  * for all purposes except line counting.
   2122  */
   2123 
   2124 void
   2125 rmescapes_nl(char *str)
   2126 {
   2127 	char *p, *q;
   2128 	int nls = 0, holdnl = 0, holdlast;
   2129 
   2130 	p = str;
   2131 	while (!ISCTL(*p)) {
   2132 		if (*p++ == '\0')
   2133 			return;
   2134 	}
   2135 	if (p > str)	/* must reprocess char before stopper (if any) */
   2136 		--p;	/* so we do not place a \n badly */
   2137 	q = p;
   2138 	while (*p) {
   2139 		if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
   2140 			p++;
   2141 			continue;
   2142 		}
   2143 		if (*p == CTLNONL) {
   2144 			p++;
   2145 			nls++;
   2146 			continue;
   2147 		}
   2148 		if (*p == CTLCNL) {
   2149 			p++;
   2150 			*q++ = '\n';
   2151 			continue;
   2152 		}
   2153 		if (*p == CTLESC)
   2154 			p++;
   2155 #ifdef DEBUG
   2156 		else if (ISCTL(*p))
   2157 			abort();
   2158 #endif
   2159 
   2160 		holdlast = holdnl;
   2161 		holdnl = is_in_name(*p);	/* letters, digits, _ */
   2162 		if (q == str || is_space(q[-1]) || (*p != '=' && q[-1] != *p)) {
   2163 			if (nls > 0 && holdnl != holdlast) {
   2164 				while (nls > 0)
   2165 					*q++ = '\n', nls--;
   2166 			}
   2167 		}
   2168 		*q++ = *p++;
   2169 	}
   2170 	while (--nls >= 0)
   2171 		*q++ = '\n';
   2172 	*q = '\0';
   2173 }
   2174 
   2175 
   2176 
   2177 /*
   2178  * See if a pattern matches in a case statement.
   2179  */
   2180 
   2181 int
   2182 casematch(union node *pattern, char *val)
   2183 {
   2184 	struct stackmark smark;
   2185 	int result;
   2186 	char *p;
   2187 
   2188 	CTRACE(DBG_MATCH, ("casematch(P=\"%s\", W=\"%s\")\n",
   2189 	    pattern->narg.text, val));
   2190 	setstackmark(&smark);
   2191 	argbackq = pattern->narg.backquote;
   2192 	STARTSTACKSTR(expdest);
   2193 	ifslastp = NULL;
   2194 	argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
   2195 	STPUTC('\0', expdest);
   2196 	p = grabstackstr(expdest);
   2197 	result = patmatch(p, val, 0);
   2198 	popstackmark(&smark);
   2199 	return result;
   2200 }
   2201 
   2202 /*
   2203  * Our own itoa().   Assumes result buffer is on the stack
   2204  */
   2205 
   2206 STATIC char *
   2207 cvtnum(int num, char *buf)
   2208 {
   2209 	char temp[32];
   2210 	int neg = num < 0;
   2211 	char *p = temp + sizeof temp - 1;
   2212 
   2213 	if (neg)
   2214 		num = -num;
   2215 
   2216 	*p = '\0';
   2217 	do {
   2218 		*--p = num % 10 + '0';
   2219 	} while ((num /= 10) != 0 && p > temp + 1);
   2220 
   2221 	if (neg)
   2222 		*--p = '-';
   2223 
   2224 	while (*p)
   2225 		STPUTC(*p++, buf);
   2226 	return buf;
   2227 }
   2228 
   2229 /*
   2230  * Do most of the work for wordexp(3).
   2231  */
   2232 
   2233 int
   2234 wordexpcmd(int argc, char **argv)
   2235 {
   2236 	size_t len;
   2237 	int i;
   2238 
   2239 	out1fmt("%d", argc - 1);
   2240 	out1c('\0');
   2241 	for (i = 1, len = 0; i < argc; i++)
   2242 		len += strlen(argv[i]);
   2243 	out1fmt("%zu", len);
   2244 	out1c('\0');
   2245 	for (i = 1; i < argc; i++) {
   2246 		out1str(argv[i]);
   2247 		out1c('\0');
   2248 	}
   2249 	return (0);
   2250 }
   2251