Home | History | Annotate | Line # | Download | only in ksh
eval.c revision 1.5
      1 /*	$NetBSD: eval.c,v 1.5 2004/07/07 19:20:09 mycroft Exp $	*/
      2 
      3 /*
      4  * Expansion - quoting, separation, substitution, globbing
      5  */
      6 #include <sys/cdefs.h>
      7 
      8 #ifndef lint
      9 __RCSID("$NetBSD: eval.c,v 1.5 2004/07/07 19:20:09 mycroft Exp $");
     10 #endif
     11 
     12 
     13 #include "sh.h"
     14 #include <pwd.h>
     15 #include "ksh_dir.h"
     16 #include "ksh_stat.h"
     17 
     18 /*
     19  * string expansion
     20  *
     21  * first pass: quoting, IFS separation, ~, ${}, $() and $(()) substitution.
     22  * second pass: alternation ({,}), filename expansion (*?[]).
     23  */
     24 
     25 /* expansion generator state */
     26 typedef struct Expand {
     27 	/* int  type; */	/* see expand() */
     28 	const char *str;	/* string */
     29 	union {
     30 		const char **strv;/* string[] */
     31 		struct shf *shf;/* file */
     32 	} u;			/* source */
     33 	struct tbl *var;	/* variable in ${var..} */
     34 	short	split;		/* split "$@" / call waitlast $() */
     35 } Expand;
     36 
     37 #define	XBASE		0	/* scanning original */
     38 #define	XSUB		1	/* expanding ${} string */
     39 #define	XARGSEP		2	/* ifs0 between "$*" */
     40 #define	XARG		3	/* expanding $*, $@ */
     41 #define	XCOM		4	/* expanding $() */
     42 #define XNULLSUB	5	/* "$@" when $# is 0 (don't generate word) */
     43 
     44 /* States used for field splitting */
     45 #define IFS_WORD	0	/* word has chars (or quotes) */
     46 #define IFS_WS		1	/* have seen IFS white-space */
     47 #define IFS_NWS		2	/* have seen IFS non-white-space */
     48 
     49 static	int	varsub ARGS((Expand *xp, char *sp, char *word, int *stypep, int *slenp));
     50 static	int	comsub ARGS((Expand *xp, char *cp));
     51 static	char   *trimsub ARGS((char *str, char *pat, int how));
     52 static	void	glob ARGS((char *cp, XPtrV *wp, int markdirs));
     53 static	void	globit ARGS((XString *xs, char **xpp, char *sp, XPtrV *wp,
     54 			     int check));
     55 static char	*maybe_expand_tilde ARGS((char *p, XString *dsp, char **dpp,
     56 					  int isassign));
     57 static	char   *tilde ARGS((char *acp));
     58 static	char   *homedir ARGS((char *name));
     59 #ifdef BRACE_EXPAND
     60 static void	alt_expand ARGS((XPtrV *wp, char *start, char *exp_start,
     61 				 char *end, int fdo));
     62 #endif
     63 
     64 /* compile and expand word */
     65 char *
     66 substitute(cp, f)
     67 	const char *cp;
     68 	int f;
     69 {
     70 	struct source *s, *sold;
     71 
     72 	sold = source;
     73 	s = pushs(SWSTR, ATEMP);
     74 	s->start = s->str = cp;
     75 	source = s;
     76 	if (yylex(ONEWORD) != LWORD)
     77 		internal_errorf(1, "substitute");
     78 	source = sold;
     79 	afree(s, ATEMP);
     80 	return evalstr(yylval.cp, f);
     81 }
     82 
     83 /*
     84  * expand arg-list
     85  */
     86 char **
     87 eval(ap, f)
     88 	register char **ap;
     89 	int f;
     90 {
     91 	XPtrV w;
     92 
     93 	if (*ap == NULL)
     94 		return ap;
     95 	XPinit(w, 32);
     96 	XPput(w, NULL);		/* space for shell name */
     97 #ifdef	SHARPBANG
     98 	XPput(w, NULL);		/* and space for one arg */
     99 #endif
    100 	while (*ap != NULL)
    101 		expand(*ap++, &w, f);
    102 	XPput(w, NULL);
    103 #ifdef	SHARPBANG
    104 	return (char **) XPclose(w) + 2;
    105 #else
    106 	return (char **) XPclose(w) + 1;
    107 #endif
    108 }
    109 
    110 /*
    111  * expand string
    112  */
    113 char *
    114 evalstr(cp, f)
    115 	char *cp;
    116 	int f;
    117 {
    118 	XPtrV w;
    119 
    120 	XPinit(w, 1);
    121 	expand(cp, &w, f);
    122 	cp = (XPsize(w) == 0) ? null : (char*) *XPptrv(w);
    123 	XPfree(w);
    124 	return cp;
    125 }
    126 
    127 /*
    128  * expand string - return only one component
    129  * used from iosetup to expand redirection files
    130  */
    131 char *
    132 evalonestr(cp, f)
    133 	register char *cp;
    134 	int f;
    135 {
    136 	XPtrV w;
    137 
    138 	XPinit(w, 1);
    139 	expand(cp, &w, f);
    140 	switch (XPsize(w)) {
    141 	case 0:
    142 		cp = null;
    143 		break;
    144 	case 1:
    145 		cp = (char*) *XPptrv(w);
    146 		break;
    147 	default:
    148 		cp = evalstr(cp, f&~DOGLOB);
    149 		break;
    150 	}
    151 	XPfree(w);
    152 	return cp;
    153 }
    154 
    155 /* for nested substitution: ${var:=$var2} */
    156 typedef struct SubType {
    157 	short	stype;		/* [=+-?%#] action after expanded word */
    158 	short	base;		/* begin position of expanded word */
    159 	short	f;		/* saved value of f (DOPAT, etc) */
    160 	struct tbl *var;	/* variable for ${var..} */
    161 	short	quote;		/* saved value of quote (for ${..[%#]..}) */
    162 	struct SubType *prev;	/* old type */
    163 	struct SubType *next;	/* poped type (to avoid re-allocating) */
    164 } SubType;
    165 
    166 void
    167 expand(cp, wp, f)
    168 	char *cp;		/* input word */
    169 	register XPtrV *wp;	/* output words */
    170 	int f;			/* DO* flags */
    171 {
    172 	register int UNINITIALIZED(c);
    173 	register int type;	/* expansion type */
    174 	register int quote = 0;	/* quoted */
    175 	XString ds;		/* destination string */
    176 	register char *dp, *sp;	/* dest., source */
    177 	int fdo, word;		/* second pass flags; have word */
    178 	int doblank;		/* field splitting of parameter/command subst */
    179 	Expand x;		/* expansion variables */
    180 	SubType st_head, *st;
    181 	int UNINITIALIZED(newlines); /* For trailing newlines in COMSUB */
    182 	int saw_eq, tilde_ok;
    183 	int make_magic;
    184 	size_t len;
    185 
    186 	if (cp == NULL)
    187 		internal_errorf(1, "expand(NULL)");
    188 	/* for alias, readonly, set, typeset commands */
    189 	if ((f & DOVACHECK) && is_wdvarassign(cp)) {
    190 		f &= ~(DOVACHECK|DOBLANK|DOGLOB|DOTILDE);
    191 		f |= DOASNTILDE;
    192 	}
    193 	if (Flag(FNOGLOB))
    194 		f &= ~DOGLOB;
    195 	if (Flag(FMARKDIRS))
    196 		f |= DOMARKDIRS;
    197 #ifdef BRACE_EXPAND
    198 	if (Flag(FBRACEEXPAND) && (f & DOGLOB))
    199 		f |= DOBRACE_;
    200 #endif /* BRACE_EXPAND */
    201 
    202 	Xinit(ds, dp, 128, ATEMP);	/* init dest. string */
    203 	type = XBASE;
    204 	sp = cp;
    205 	fdo = 0;
    206 	saw_eq = 0;
    207 	tilde_ok = (f & (DOTILDE|DOASNTILDE)) ? 1 : 0; /* must be 1/0 */
    208 	doblank = 0;
    209 	make_magic = 0;
    210 	word = (f&DOBLANK) ? IFS_WS : IFS_WORD;
    211 	st_head.next = (SubType *) 0;
    212 	st = &st_head;
    213 
    214 	while (1) {
    215 		Xcheck(ds, dp);
    216 
    217 		switch (type) {
    218 		  case XBASE:	/* original prefixed string */
    219 			c = *sp++;
    220 			switch (c) {
    221 			  case EOS:
    222 				c = 0;
    223 				break;
    224 			  case CHAR:
    225 				c = *sp++;
    226 				break;
    227 			  case QCHAR:
    228 				quote |= 2; /* temporary quote */
    229 				c = *sp++;
    230 				break;
    231 			  case OQUOTE:
    232 				word = IFS_WORD;
    233 				tilde_ok = 0;
    234 				quote = 1;
    235 				continue;
    236 			  case CQUOTE:
    237 				quote = 0;
    238 				continue;
    239 			  case COMSUB:
    240 				tilde_ok = 0;
    241 				if (f & DONTRUNCOMMAND) {
    242 					word = IFS_WORD;
    243 					*dp++ = '$'; *dp++ = '(';
    244 					while (*sp != '\0') {
    245 						Xcheck(ds, dp);
    246 						*dp++ = *sp++;
    247 					}
    248 					*dp++ = ')';
    249 				} else {
    250 					type = comsub(&x, sp);
    251 					if (type == XCOM && (f&DOBLANK))
    252 						doblank++;
    253 					sp = strchr(sp, 0) + 1;
    254 					newlines = 0;
    255 				}
    256 				continue;
    257 			  case EXPRSUB:
    258 				word = IFS_WORD;
    259 				tilde_ok = 0;
    260 				if (f & DONTRUNCOMMAND) {
    261 					*dp++ = '$'; *dp++ = '('; *dp++ = '(';
    262 					while (*sp != '\0') {
    263 						Xcheck(ds, dp);
    264 						*dp++ = *sp++;
    265 					}
    266 					*dp++ = ')'; *dp++ = ')';
    267 				} else {
    268 					struct tbl v;
    269 					char *p;
    270 
    271 					v.flag = DEFINED|ISSET|INTEGER;
    272 					v.type = 10; /* not default */
    273 					v.name[0] = '\0';
    274 					v_evaluate(&v, substitute(sp, 0),
    275 						KSH_UNWIND_ERROR);
    276 					sp = strchr(sp, 0) + 1;
    277 					for (p = str_val(&v); *p; ) {
    278 						Xcheck(ds, dp);
    279 						*dp++ = *p++;
    280 					}
    281 				}
    282 				continue;
    283 			  case OSUBST: /* ${{#}var{:}[=+-?#%]word} */
    284 			  /* format is:
    285 			   *   OSUBST [{x] plain-variable-part \0
    286 			   *     compiled-word-part CSUBST [}x]
    287 			   * This is were all syntax checking gets done...
    288 			   */
    289 			  {
    290 				char *varname = ++sp; /* skip the { or x (}) */
    291 				int stype;
    292 				int slen;
    293 
    294 				sp = strchr(sp, '\0') + 1; /* skip variable */
    295 				type = varsub(&x, varname, sp, &stype, &slen);
    296 				if (type < 0) {
    297 					char endc;
    298 					char *str, *end;
    299 
    300 					end = (char *) wdscan(sp, CSUBST);
    301 					/* ({) the } or x is already skipped */
    302 					endc = *end;
    303 					*end = EOS;
    304 					str = snptreef((char *) 0, 64, "%S",
    305 							varname - 1);
    306 					*end = endc;
    307 					errorf("%s: bad substitution", str);
    308 				}
    309 				if (f&DOBLANK)
    310 					doblank++;
    311 				tilde_ok = 0;
    312 				if (type == XBASE) {	/* expand? */
    313 					if (!st->next) {
    314 						SubType *newst;
    315 
    316 						newst = (SubType *) alloc(
    317 							sizeof(SubType), ATEMP);
    318 						newst->next = (SubType *) 0;
    319 						newst->prev = st;
    320 						st->next = newst;
    321 					}
    322 					st = st->next;
    323 					st->stype = stype;
    324 					st->base = Xsavepos(ds, dp);
    325 					st->f = f;
    326 					st->var = x.var;
    327 					st->quote = quote;
    328 					/* skip qualifier(s) */
    329 					if (stype)
    330 						sp += slen;
    331 					switch (stype & 0x7f) {
    332 					  case '#':
    333 					  case '%':
    334 						/* ! DOBLANK,DOBRACE_,DOTILDE */
    335 						f = DOPAT | (f&DONTRUNCOMMAND)
    336 						    | DOTEMP_;
    337 						quote = 0;
    338 						/* Prepend open pattern (so |
    339 						 * in a trim will work as
    340 						 * expected)
    341 						 */
    342 						*dp++ = MAGIC;
    343 						*dp++ = '@' + 0x80;
    344 						break;
    345 					  case '=':
    346 						/* Enabling tilde expansion
    347 						 * after :'s here is
    348 						 * non-standard ksh, but is
    349 						 * consistent with rules for
    350 						 * other assignments.  Not
    351 						 * sure what POSIX thinks of
    352 						 * this.
    353 						 * Not doing tilde expansion
    354 						 * for integer variables is a
    355 						 * non-POSIX thing - makes
    356 						 * sense though, since ~ is
    357 						 * a arithmetic operator.
    358 						 */
    359 						if (!(x.var->flag & INTEGER))
    360 							f |= DOASNTILDE|DOTILDE;
    361 						f |= DOTEMP_;
    362 						/* These will be done after the
    363 						 * value has been assigned.
    364 						 */
    365 						f &= ~(DOBLANK|DOGLOB|DOBRACE_);
    366 						tilde_ok = 1;
    367 						break;
    368 					  case '?':
    369 						f &= ~DOBLANK;
    370 						f |= DOTEMP_;
    371 						/* fall through */
    372 					  default:
    373 						/* Enable tilde expansion */
    374 						tilde_ok = 1;
    375 						f |= DOTILDE;
    376 					}
    377 				} else
    378 					/* skip word */
    379 					sp = (char *) wdscan(sp, CSUBST);
    380 				continue;
    381 			  }
    382 			  case CSUBST: /* only get here if expanding word */
    383 				sp++; /* ({) skip the } or x */
    384 				tilde_ok = 0;	/* in case of ${unset:-} */
    385 				*dp = '\0';
    386 				quote = st->quote;
    387 				f = st->f;
    388 				if (f&DOBLANK)
    389 					doblank--;
    390 				switch (st->stype&0x7f) {
    391 				  case '#':
    392 				  case '%':
    393 					/* Append end-pattern */
    394 					*dp++ = MAGIC; *dp++ = ')'; *dp = '\0';
    395 					dp = Xrestpos(ds, dp, st->base);
    396 					/* Must use st->var since calling
    397 					 * global would break things
    398 					 * like x[i+=1].
    399 					 */
    400 					x.str = trimsub(str_val(st->var),
    401 						dp, st->stype);
    402 					type = XSUB;
    403 					if (f&DOBLANK)
    404 						doblank++;
    405 					st = st->prev;
    406 					continue;
    407 				  case '=':
    408 					/* Restore our position and substitute
    409 					 * the value of st->var (may not be
    410 					 * the assigned value in the presence
    411 					 * of integer/right-adj/etc attributes).
    412 					 */
    413 					dp = Xrestpos(ds, dp, st->base);
    414 					/* Must use st->var since calling
    415 					 * global would cause with things
    416 					 * like x[i+=1] to be evaluated twice.
    417 					 */
    418 					/* Note: not exported by FEXPORT
    419 					 * in at&t ksh.
    420 					 */
    421 					/* XXX POSIX says readonly is only
    422 					 * fatal for special builtins (setstr
    423 					 * does readonly check).
    424 					 */
    425 					len = strlen(dp) + 1;
    426 					setstr(st->var,
    427 					    debunk((char *) alloc(len, ATEMP),
    428 						dp, len),
    429 					    KSH_UNWIND_ERROR);
    430 					x.str = str_val(st->var);
    431 					type = XSUB;
    432 					if (f&DOBLANK)
    433 						doblank++;
    434 					st = st->prev;
    435 					continue;
    436 				  case '?':
    437 				    {
    438 					char *s = Xrestpos(ds, dp, st->base);
    439 
    440 					errorf("%s: %s", st->var->name,
    441 					    dp == s ?
    442 					      "parameter null or not set"
    443 					    : (debunk(s, s, strlen(s) + 1), s));
    444 				    }
    445 				}
    446 				st = st->prev;
    447 				type = XBASE;
    448 				continue;
    449 
    450 			  case OPAT: /* open pattern: *(foo|bar) */
    451 				/* Next char is the type of pattern */
    452 				make_magic = 1;
    453 				c = *sp++ + 0x80;
    454 				break;
    455 
    456 			  case SPAT: /* pattern separator (|) */
    457 				make_magic = 1;
    458 				c = '|';
    459 				break;
    460 
    461 			  case CPAT: /* close pattern */
    462 				make_magic = 1;
    463 				c = /*(*/ ')';
    464 				break;
    465 			}
    466 			break;
    467 
    468 		  case XNULLSUB:
    469 			/* Special case for "$@" (and "${foo[@]}") - no
    470 			 * word is generated if $# is 0 (unless there is
    471 			 * other stuff inside the quotes).
    472 			 */
    473 			type = XBASE;
    474 			if (f&DOBLANK) {
    475 				doblank--;
    476 				/* not really correct: x=; "$x$@" should
    477 				 * generate a null argument and
    478 				 * set A; "${@:+}" shouldn't.
    479 				 */
    480 				if (dp == Xstring(ds, dp))
    481 					word = IFS_WS;
    482 			}
    483 			continue;
    484 
    485 		  case XSUB:
    486 			if ((c = *x.str++) == 0) {
    487 				type = XBASE;
    488 				if (f&DOBLANK)
    489 					doblank--;
    490 				continue;
    491 			}
    492 			break;
    493 
    494 		  case XARGSEP:
    495 			type = XARG;
    496 			quote = 1;
    497 		  case XARG:
    498 			if ((c = *x.str++) == '\0') {
    499 				/* force null words to be created so
    500 				 * set -- '' 2 ''; foo "$@" will do
    501 				 * the right thing
    502 				 */
    503 				if (quote && x.split)
    504 					word = IFS_WORD;
    505 				if ((x.str = *x.u.strv++) == NULL) {
    506 					type = XBASE;
    507 					if (f&DOBLANK)
    508 						doblank--;
    509 					continue;
    510 				}
    511 				c = ifs0;
    512 				if (c == 0) {
    513 					if (quote && !x.split)
    514 						continue;
    515 					c = ' ';
    516 				}
    517 				if (quote && x.split) {
    518 					/* terminate word for "$@" */
    519 					type = XARGSEP;
    520 					quote = 0;
    521 				}
    522 			}
    523 			break;
    524 
    525 		  case XCOM:
    526 			if (newlines) {		/* Spit out saved nl's */
    527 				c = '\n';
    528 				--newlines;
    529 			} else {
    530 				while ((c = shf_getc(x.u.shf)) == 0 || c == '\n')
    531 				    if (c == '\n')
    532 					    newlines++;	/* Save newlines */
    533 				if (newlines && c != EOF) {
    534 					shf_ungetc(c, x.u.shf);
    535 					c = '\n';
    536 					--newlines;
    537 				}
    538 			}
    539 			if (c == EOF) {
    540 				newlines = 0;
    541 				shf_close(x.u.shf);
    542 				if (x.split)
    543 					subst_exstat = waitlast();
    544 				type = XBASE;
    545 				if (f&DOBLANK)
    546 					doblank--;
    547 				continue;
    548 			}
    549 			break;
    550 		}
    551 
    552 		/* check for end of word or IFS separation */
    553 		if (c == 0 || (!quote && (f & DOBLANK) && doblank && !make_magic
    554 			       && ctype(c, C_IFS)))
    555 		{
    556 			/* How words are broken up:
    557 			 *		   |       value of c
    558 			 *	  word	   |	ws	nws	0
    559 			 *	-----------------------------------
    560 			 *	IFS_WORD	w/WS	w/NWS	w
    561 			 *	IFS_WS		-/WS	w/NWS	-
    562 			 *	IFS_NWS		-/NWS	w/NWS	w
    563 			 *   (w means generate a word)
    564 			 * Note that IFS_NWS/0 generates a word (at&t ksh
    565 			 * doesn't do this, but POSIX does).
    566 			 */
    567 			if (word == IFS_WORD
    568 			    || (!ctype(c, C_IFSWS) && (c || word == IFS_NWS)))
    569 			{
    570 				char *p;
    571 
    572 				*dp++ = '\0';
    573 				p = Xclose(ds, dp);
    574 #ifdef BRACE_EXPAND
    575 				if (fdo & DOBRACE_)
    576 					/* also does globbing */
    577 					alt_expand(wp, p, p,
    578 						   p + Xlength(ds, (dp - 1)),
    579 						   fdo | (f & DOMARKDIRS));
    580 				else
    581 #endif /* BRACE_EXPAND */
    582 				if (fdo & DOGLOB)
    583 					glob(p, wp, f & DOMARKDIRS);
    584 				else if ((f & DOPAT) || !(fdo & DOMAGIC_))
    585 					XPput(*wp, p);
    586 				else
    587 					XPput(*wp, debunk(p, p, strlen(p) + 1));
    588 				fdo = 0;
    589 				saw_eq = 0;
    590 				tilde_ok = (f & (DOTILDE|DOASNTILDE)) ? 1 : 0;
    591 				if (c != 0)
    592 					Xinit(ds, dp, 128, ATEMP);
    593 			}
    594 			if (c == 0)
    595 				return;
    596 			if (word != IFS_NWS)
    597 				word = ctype(c, C_IFSWS) ? IFS_WS : IFS_NWS;
    598 		} else {
    599 			/* age tilde_ok info - ~ code tests second bit */
    600 			tilde_ok <<= 1;
    601 			/* mark any special second pass chars */
    602 			if (!quote)
    603 				switch (c) {
    604 				  case '[':
    605 				  case NOT:
    606 				  case '-':
    607 				  case ']':
    608 					/* For character classes - doesn't hurt
    609 					 * to have magic !,-,]'s outside of
    610 					 * [...] expressions.
    611 					 */
    612 					if (f & (DOPAT | DOGLOB)) {
    613 						fdo |= DOMAGIC_;
    614 						if (c == '[')
    615 							fdo |= f & DOGLOB;
    616 						*dp++ = MAGIC;
    617 					}
    618 					break;
    619 				  case '*':
    620 				  case '?':
    621 					if (f & (DOPAT | DOGLOB)) {
    622 						fdo |= DOMAGIC_ | (f & DOGLOB);
    623 						*dp++ = MAGIC;
    624 					}
    625 					break;
    626 #ifdef BRACE_EXPAND
    627 				  case OBRACE:
    628 				  case ',':
    629 				  case CBRACE:
    630 					if ((f & DOBRACE_) && (c == OBRACE
    631 						|| (fdo & DOBRACE_)))
    632 					{
    633 						fdo |= DOBRACE_|DOMAGIC_;
    634 						*dp++ = MAGIC;
    635 					}
    636 					break;
    637 #endif /* BRACE_EXPAND */
    638 				  case '=':
    639 					/* Note first unquoted = for ~ */
    640 					if (!(f & DOTEMP_) && !saw_eq) {
    641 						saw_eq = 1;
    642 						tilde_ok = 1;
    643 					}
    644 					break;
    645 				  case PATHSEP: /* : */
    646 					/* Note unquoted : for ~ */
    647 					if (!(f & DOTEMP_) && (f & DOASNTILDE))
    648 						tilde_ok = 1;
    649 					break;
    650 				  case '~':
    651 					/* tilde_ok is reset whenever
    652 					 * any of ' " $( $(( ${ } are seen.
    653 					 * Note that tilde_ok must be preserved
    654 					 * through the sequence ${A=a=}~
    655 					 */
    656 					if (type == XBASE
    657 					    && (f & (DOTILDE|DOASNTILDE))
    658 					    && (tilde_ok & 2))
    659 					{
    660 						char *p, *dp_x;
    661 
    662 						dp_x = dp;
    663 						p = maybe_expand_tilde(sp,
    664 							&ds, &dp_x,
    665 							f & DOASNTILDE);
    666 						if (p) {
    667 							if (dp != dp_x)
    668 								word = IFS_WORD;
    669 							dp = dp_x;
    670 							sp = p;
    671 							continue;
    672 						}
    673 					}
    674 					break;
    675 				}
    676 			else
    677 				quote &= ~2; /* undo temporary */
    678 
    679 			if (make_magic) {
    680 				make_magic = 0;
    681 				fdo |= DOMAGIC_ | (f & DOGLOB);
    682 				*dp++ = MAGIC;
    683 			} else if (ISMAGIC(c)) {
    684 				fdo |= DOMAGIC_;
    685 				*dp++ = MAGIC;
    686 			}
    687 			*dp++ = c; /* save output char */
    688 			word = IFS_WORD;
    689 		}
    690 	}
    691 }
    692 
    693 /*
    694  * Prepare to generate the string returned by ${} substitution.
    695  */
    696 static int
    697 varsub(xp, sp, word, stypep, slenp)
    698 	Expand *xp;
    699 	char *sp;
    700 	char *word;
    701 	int *stypep;	/* becomes qualifier type */
    702 	int *slenp;	/* " " len (=, :=, etc.) valid iff *stypep != 0 */
    703 {
    704 	int c;
    705 	int state;	/* next state: XBASE, XARG, XSUB, XNULLSUB */
    706 	int stype;	/* substitution type */
    707 	int slen;
    708 	char *p;
    709 	struct tbl *vp;
    710 
    711 	if (sp[0] == '\0')	/* Bad variable name */
    712 		return -1;
    713 
    714 	xp->var = (struct tbl *) 0;
    715 
    716 	/* ${#var}, string length or array size */
    717 	if (sp[0] == '#' && (c = sp[1]) != '\0') {
    718 		int zero_ok = 0;
    719 
    720 		/* Can't have any modifiers for ${#...} */
    721 		if (*word != CSUBST)
    722 			return -1;
    723 		sp++;
    724 		/* Check for size of array */
    725 		if ((p=strchr(sp,'[')) && (p[1]=='*'||p[1]=='@') && p[2]==']') {
    726 			int n = 0;
    727 			int max = 0;
    728 			vp = global(arrayname(sp));
    729 			if (vp->flag & (ISSET|ARRAY))
    730 				zero_ok = 1;
    731 			for (; vp; vp = vp->u.array)
    732 				if (vp->flag & ISSET) {
    733 					max = vp->index + 1;
    734 					n++;
    735 				}
    736 			c = n; /* ksh88/ksh93 go for number, not max index */
    737 		} else if (c == '*' || c == '@')
    738 			c = e->loc->argc;
    739 		else {
    740 			p = str_val(global(sp));
    741 			zero_ok = p != null;
    742 			c = strlen(p);
    743 		}
    744 		if (Flag(FNOUNSET) && c == 0 && !zero_ok)
    745 			errorf("%s: parameter not set", sp);
    746 		*stypep = 0; /* unqualified variable/string substitution */
    747 		xp->str = str_save(ulton((unsigned long)c, 10), ATEMP);
    748 		return XSUB;
    749 	}
    750 
    751 	/* Check for qualifiers in word part */
    752 	stype = 0;
    753 	c = word[slen = 0] == CHAR ? word[1] : 0;
    754 	if (c == ':') {
    755 		slen += 2;
    756 		stype = 0x80;
    757 		c = word[slen + 0] == CHAR ? word[slen + 1] : 0;
    758 	}
    759 	if (ctype(c, C_SUBOP1)) {
    760 		slen += 2;
    761 		stype |= c;
    762 	} else if (ctype(c, C_SUBOP2)) { /* Note: ksh88 allows :%, :%%, etc */
    763 		slen += 2;
    764 		stype = c;
    765 		if (word[slen + 0] == CHAR && c == word[slen + 1]) {
    766 			stype |= 0x80;
    767 			slen += 2;
    768 		}
    769 	} else if (stype)	/* : is not ok */
    770 		return -1;
    771 	if (!stype && *word != CSUBST)
    772 		return -1;
    773 	*stypep = stype;
    774 	*slenp = slen;
    775 
    776 	c = sp[0];
    777 	if (c == '*' || c == '@') {
    778 		switch (stype & 0x7f) {
    779 		  case '=':	/* can't assign to a vector */
    780 		  case '%':	/* can't trim a vector (yet) */
    781 		  case '#':
    782 			return -1;
    783 		}
    784 		if (e->loc->argc == 0) {
    785 			xp->str = null;
    786 			state = c == '@' ? XNULLSUB : XSUB;
    787 		} else {
    788 			xp->u.strv = (const char **) e->loc->argv + 1;
    789 			xp->str = *xp->u.strv++;
    790 			xp->split = c == '@'; /* $@ */
    791 			state = XARG;
    792 		}
    793 	} else {
    794 		if ((p=strchr(sp,'[')) && (p[1]=='*'||p[1]=='@') && p[2]==']') {
    795 			XPtrV wv;
    796 
    797 			switch (stype & 0x7f) {
    798 			  case '=':	/* can't assign to a vector */
    799 			  case '%':	/* can't trim a vector (yet) */
    800 			  case '#':
    801 				return -1;
    802 			}
    803 			XPinit(wv, 32);
    804 			vp = global(arrayname(sp));
    805 			for (; vp; vp = vp->u.array) {
    806 				if (!(vp->flag&ISSET))
    807 					continue;
    808 				XPput(wv, str_val(vp));
    809 			}
    810 			if (XPsize(wv) == 0) {
    811 				xp->str = null;
    812 				state = p[1] == '@' ? XNULLSUB : XSUB;
    813 				XPfree(wv);
    814 			} else {
    815 				XPput(wv, 0);
    816 				xp->u.strv = (const char **) XPptrv(wv);
    817 				xp->str = *xp->u.strv++;
    818 				xp->split = p[1] == '@'; /* ${foo[@]} */
    819 				state = XARG;
    820 			}
    821 		} else {
    822 			/* Can't assign things like $! or $1 */
    823 			if ((stype & 0x7f) == '='
    824 			    && (ctype(*sp, C_VAR1) || digit(*sp)))
    825 				return -1;
    826 			xp->var = global(sp);
    827 			xp->str = str_val(xp->var);
    828 			state = XSUB;
    829 		}
    830 	}
    831 
    832 	c = stype&0x7f;
    833 	/* test the compiler's code generator */
    834 	if (ctype(c, C_SUBOP2) ||
    835 	    (((stype&0x80) ? *xp->str=='\0' : xp->str==null) ? /* undef? */
    836 	     c == '=' || c == '-' || c == '?' : c == '+'))
    837 		state = XBASE;	/* expand word instead of variable value */
    838 	if (Flag(FNOUNSET) && xp->str == null
    839 	    && (ctype(c, C_SUBOP2) || (state != XBASE && c != '+')))
    840 		errorf("%s: parameter not set", sp);
    841 	return state;
    842 }
    843 
    844 /*
    845  * Run the command in $(...) and read its output.
    846  */
    847 static int
    848 comsub(xp, cp)
    849 	register Expand *xp;
    850 	char *cp;
    851 {
    852 	Source *s, *sold;
    853 	register struct op *t;
    854 	struct shf *shf;
    855 
    856 	s = pushs(SSTRING, ATEMP);
    857 	s->start = s->str = cp;
    858 	sold = source;
    859 	t = compile(s);
    860 	source = sold;
    861 
    862 	if (t == NULL)
    863 		return XBASE;
    864 
    865 	if (t != NULL && t->type == TCOM && /* $(<file) */
    866 	    *t->args == NULL && *t->vars == NULL && t->ioact != NULL) {
    867 		register struct ioword *io = *t->ioact;
    868 		char *name;
    869 
    870 		if ((io->flag&IOTYPE) != IOREAD)
    871 			errorf("funny $() command: %s",
    872 				snptreef((char *) 0, 32, "%R", io));
    873 		shf = shf_open(name = evalstr(io->name, DOTILDE), O_RDONLY, 0,
    874 			SHF_MAPHI|SHF_CLEXEC);
    875 		if (shf == NULL)
    876 			errorf("%s: cannot open $() input", name);
    877 		xp->split = 0;	/* no waitlast() */
    878 	} else {
    879 		int ofd1, pv[2];
    880 		openpipe(pv);
    881 		shf = shf_fdopen(pv[0], SHF_RD, (struct shf *) 0);
    882 		ofd1 = savefd(1, 0);	/* fd 1 may be closed... */
    883 		if (pv[1] != 1) {
    884 			ksh_dup2(pv[1], 1, FALSE);
    885 			close(pv[1]);
    886 		}
    887 		execute(t, XFORK|XXCOM|XPIPEO);
    888 		restfd(1, ofd1);
    889 		startlast();
    890 		xp->split = 1;	/* waitlast() */
    891 	}
    892 
    893 	xp->u.shf = shf;
    894 	return XCOM;
    895 }
    896 
    897 /*
    898  * perform #pattern and %pattern substitution in ${}
    899  */
    900 
    901 static char *
    902 trimsub(str, pat, how)
    903 	register char *str;
    904 	char *pat;
    905 	int how;
    906 {
    907 	register char *end = strchr(str, 0);
    908 	register char *p, c;
    909 
    910 	switch (how&0xff) {	/* UCHAR_MAX maybe? */
    911 	  case '#':		/* shortest at beginning */
    912 		for (p = str; p <= end; p++) {
    913 			c = *p; *p = '\0';
    914 			if (gmatch(str, pat, FALSE)) {
    915 				*p = c;
    916 				return p;
    917 			}
    918 			*p = c;
    919 		}
    920 		break;
    921 	  case '#'|0x80:	/* longest match at beginning */
    922 		for (p = end; p >= str; p--) {
    923 			c = *p; *p = '\0';
    924 			if (gmatch(str, pat, FALSE)) {
    925 				*p = c;
    926 				return p;
    927 			}
    928 			*p = c;
    929 		}
    930 		break;
    931 	  case '%':		/* shortest match at end */
    932 		for (p = end; p >= str; p--) {
    933 			if (gmatch(p, pat, FALSE))
    934 				return str_nsave(str, p - str, ATEMP);
    935 		}
    936 		break;
    937 	  case '%'|0x80:	/* longest match at end */
    938 		for (p = str; p <= end; p++) {
    939 			if (gmatch(p, pat, FALSE))
    940 				return str_nsave(str, p - str, ATEMP);
    941 		}
    942 		break;
    943 	}
    944 
    945 	return str;		/* no match, return string */
    946 }
    947 
    948 /*
    949  * glob
    950  * Name derived from V6's /etc/glob, the program that expanded filenames.
    951  */
    952 
    953 /* XXX cp not const 'cause slashes are temporarily replaced with nulls... */
    954 static void
    955 glob(cp, wp, markdirs)
    956 	char *cp;
    957 	register XPtrV *wp;
    958 	int markdirs;
    959 {
    960 	int oldsize = XPsize(*wp);
    961 
    962 	if (glob_str(cp, wp, markdirs) == 0)
    963 		XPput(*wp, debunk(cp, cp, strlen(cp) + 1));
    964 	else
    965 		qsortp(XPptrv(*wp) + oldsize, (size_t)(XPsize(*wp) - oldsize),
    966 			xstrcmp);
    967 }
    968 
    969 #define GF_NONE		0
    970 #define GF_EXCHECK	BIT(0)		/* do existence check on file */
    971 #define GF_GLOBBED	BIT(1)		/* some globbing has been done */
    972 #define GF_MARKDIR	BIT(2)		/* add trailing / to directories */
    973 
    974 /* Apply file globbing to cp and store the matching files in wp.  Returns
    975  * the number of matches found.
    976  */
    977 int
    978 glob_str(cp, wp, markdirs)
    979 	char *cp;
    980 	XPtrV *wp;
    981 	int markdirs;
    982 {
    983 	int oldsize = XPsize(*wp);
    984 	XString xs;
    985 	char *xp;
    986 
    987 	Xinit(xs, xp, 256, ATEMP);
    988 	globit(&xs, &xp, cp, wp, markdirs ? GF_MARKDIR : GF_NONE);
    989 	Xfree(xs, xp);
    990 
    991 	return XPsize(*wp) - oldsize;
    992 }
    993 
    994 static void
    995 globit(xs, xpp, sp, wp, check)
    996 	XString *xs;		/* dest string */
    997 	char **xpp;		/* ptr to dest end */
    998 	char *sp;		/* source path */
    999 	register XPtrV *wp;	/* output list */
   1000 	int check;		/* GF_* flags */
   1001 {
   1002 	register char *np;	/* next source component */
   1003 	char *xp = *xpp;
   1004 	char *se;
   1005 	char odirsep;
   1006 
   1007 	/* This to allow long expansions to be interrupted */
   1008 	intrcheck();
   1009 
   1010 	if (sp == NULL) {	/* end of source path */
   1011 		/* We only need to check if the file exists if a pattern
   1012 		 * is followed by a non-pattern (eg, foo*x/bar; no check
   1013 		 * is needed for foo* since the match must exist) or if
   1014 		 * any patterns were expanded and the markdirs option is set.
   1015 		 * Symlinks make things a bit tricky...
   1016 		 */
   1017 		if ((check & GF_EXCHECK)
   1018 		    || ((check & GF_MARKDIR) && (check & GF_GLOBBED)))
   1019 		{
   1020 #define stat_check()	(stat_done ? stat_done : \
   1021 			    (stat_done = stat(Xstring(*xs, xp), &statb) < 0 \
   1022 				? -1 : 1))
   1023 			struct stat lstatb, statb;
   1024 			int stat_done = 0;	 /* -1: failed, 1 ok */
   1025 
   1026 			if (lstat(Xstring(*xs, xp), &lstatb) < 0)
   1027 				return;
   1028 			/* special case for systems which strip trailing
   1029 			 * slashes from regular files (eg, /etc/passwd/).
   1030 			 * SunOS 4.1.3 does this...
   1031 			 */
   1032 			if ((check & GF_EXCHECK) && xp > Xstring(*xs, xp)
   1033 			    && ISDIRSEP(xp[-1]) && !S_ISDIR(lstatb.st_mode)
   1034 #ifdef S_ISLNK
   1035 			    && (!S_ISLNK(lstatb.st_mode)
   1036 				|| stat_check() < 0
   1037 				|| !S_ISDIR(statb.st_mode))
   1038 #endif /* S_ISLNK */
   1039 				)
   1040 				return;
   1041 			/* Possibly tack on a trailing / if there isn't already
   1042 			 * one and if the file is a directory or a symlink to a
   1043 			 * directory
   1044 			 */
   1045 			if (((check & GF_MARKDIR) && (check & GF_GLOBBED))
   1046 			    && xp > Xstring(*xs, xp) && !ISDIRSEP(xp[-1])
   1047 			    && (S_ISDIR(lstatb.st_mode)
   1048 #ifdef S_ISLNK
   1049 				|| (S_ISLNK(lstatb.st_mode)
   1050 				    && stat_check() > 0
   1051 				    && S_ISDIR(statb.st_mode))
   1052 #endif /* S_ISLNK */
   1053 				    ))
   1054 			{
   1055 				*xp++ = DIRSEP;
   1056 				*xp = '\0';
   1057 			}
   1058 		}
   1059 #ifdef OS2 /* Done this way to avoid bug in gcc 2.7.2... */
   1060     /* Ugly kludge required for command
   1061      * completion - see how search_access()
   1062      * is implemented for OS/2...
   1063      */
   1064 # define KLUDGE_VAL	4
   1065 #else /* OS2 */
   1066 # define KLUDGE_VAL	0
   1067 #endif /* OS2 */
   1068 		XPput(*wp, str_nsave(Xstring(*xs, xp), Xlength(*xs, xp)
   1069 			+ KLUDGE_VAL, ATEMP));
   1070 		return;
   1071 	}
   1072 
   1073 	if (xp > Xstring(*xs, xp))
   1074 		*xp++ = DIRSEP;
   1075 	while (ISDIRSEP(*sp)) {
   1076 		Xcheck(*xs, xp);
   1077 		*xp++ = *sp++;
   1078 	}
   1079 	np = ksh_strchr_dirsep(sp);
   1080 	if (np != NULL) {
   1081 		se = np;
   1082 		odirsep = *np;	/* don't assume DIRSEP, can be multiple kinds */
   1083 		*np++ = '\0';
   1084 	} else {
   1085 		odirsep = '\0'; /* keep gcc quiet */
   1086 		se = sp + strlen(sp);
   1087 	}
   1088 
   1089 
   1090 	/* Check if sp needs globbing - done to avoid pattern checks for strings
   1091 	 * containing MAGIC characters, open ['s without the matching close ],
   1092 	 * etc. (otherwise opendir() will be called which may fail because the
   1093 	 * directory isn't readable - if no globbing is needed, only execute
   1094 	 * permission should be required (as per POSIX)).
   1095 	 */
   1096 	if (!has_globbing(sp, se)) {
   1097 		XcheckN(*xs, xp, se - sp + 1);
   1098 		debunk(xp, sp, Xnleft(*xs, xp));
   1099 		xp += strlen(xp);
   1100 		*xpp = xp;
   1101 		globit(xs, xpp, np, wp, check);
   1102 	} else {
   1103 		DIR *dirp;
   1104 		struct dirent *d;
   1105 		char *name;
   1106 		int len;
   1107 		int prefix_len;
   1108 
   1109 		/* xp = *xpp;	   copy_non_glob() may have re-alloc'd xs */
   1110 		*xp = '\0';
   1111 		prefix_len = Xlength(*xs, xp);
   1112 		dirp = ksh_opendir(prefix_len ? Xstring(*xs, xp) : ".");
   1113 		if (dirp == NULL)
   1114 			goto Nodir;
   1115 		while ((d = readdir(dirp)) != NULL) {
   1116 			name = d->d_name;
   1117 			if (name[0] == '.' &&
   1118 			    (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
   1119 				continue; /* always ignore . and .. */
   1120 			if ((*name == '.' && *sp != '.')
   1121 			    || !gmatch(name, sp, TRUE))
   1122 				continue;
   1123 
   1124 			len = NLENGTH(d) + 1;
   1125 			XcheckN(*xs, xp, len);
   1126 			memcpy(xp, name, len);
   1127 			*xpp = xp + len - 1;
   1128 			globit(xs, xpp, np, wp,
   1129 				(check & GF_MARKDIR) | GF_GLOBBED
   1130 				| (np ? GF_EXCHECK : GF_NONE));
   1131 			xp = Xstring(*xs, xp) + prefix_len;
   1132 		}
   1133 		closedir(dirp);
   1134 	  Nodir:;
   1135 	}
   1136 
   1137 	if (np != NULL)
   1138 		*--np = odirsep;
   1139 }
   1140 
   1141 #if 0
   1142 /* Check if p contains something that needs globbing; if it does, 0 is
   1143  * returned; if not, p is copied into xs/xp after stripping any MAGICs
   1144  */
   1145 static int	copy_non_glob ARGS((XString *xs, char **xpp, char *p));
   1146 static int
   1147 copy_non_glob(xs, xpp, p)
   1148 	XString *xs;
   1149 	char **xpp;
   1150 	char *p;
   1151 {
   1152 	char *xp;
   1153 	int len = strlen(p);
   1154 
   1155 	XcheckN(*xs, *xpp, len);
   1156 	xp = *xpp;
   1157 	for (; *p; p++) {
   1158 		if (ISMAGIC(*p)) {
   1159 			int c = *++p;
   1160 
   1161 			if (c == '*' || c == '?')
   1162 				return 0;
   1163 			if (*p == '[') {
   1164 				char *q = p + 1;
   1165 
   1166 				if (ISMAGIC(*q) && q[1] == NOT)
   1167 					q += 2;
   1168 				if (ISMAGIC(*q) && q[1] == ']')
   1169 					q += 2;
   1170 				for (; *q; q++)
   1171 					if (ISMAGIC(*q) && *++q == ']')
   1172 						return 0;
   1173 				/* pass a literal [ through */
   1174 			}
   1175 			/* must be a MAGIC-MAGIC, or MAGIC-!, MAGIC--, etc. */
   1176 		}
   1177 		*xp++ = *p;
   1178 	}
   1179 	*xp = '\0';
   1180 	*xpp = xp;
   1181 	return 1;
   1182 }
   1183 #endif /* 0 */
   1184 
   1185 /* remove MAGIC from string */
   1186 char *
   1187 debunk(dp, sp, dlen)
   1188 	char *dp;
   1189 	const char *sp;
   1190 	size_t dlen;
   1191 {
   1192 	char *d, *s;
   1193 
   1194 	if ((s = strchr(sp, MAGIC))) {
   1195 		if (s - sp >= dlen)
   1196 			return dp;
   1197 		memcpy(dp, sp, s - sp);
   1198 		for (d = dp + (s - sp); *s && (d - dp < dlen); s++)
   1199 			if (!ISMAGIC(*s) || !(*++s & 0x80)
   1200 			    || !strchr("*+?@! ", *s & 0x7f))
   1201 				*d++ = *s;
   1202 			else {
   1203 				/* extended pattern operators: *+?@! */
   1204 				if ((*s & 0x7f) != ' ')
   1205 					*d++ = *s & 0x7f;
   1206 				if (d - dp < dlen)
   1207 					*d++ = '(';
   1208 			}
   1209 		*d = '\0';
   1210 	} else if (dp != sp)
   1211 		strlcpy(dp, sp, dlen);
   1212 	return dp;
   1213 }
   1214 
   1215 /* Check if p is an unquoted name, possibly followed by a / or :.  If so
   1216  * puts the expanded version in *dcp,dp and returns a pointer in p just
   1217  * past the name, otherwise returns 0.
   1218  */
   1219 static char *
   1220 maybe_expand_tilde(p, dsp, dpp, isassign)
   1221 	char *p;
   1222 	XString *dsp;
   1223 	char **dpp;
   1224 	int isassign;
   1225 {
   1226 	XString ts;
   1227 	char *dp = *dpp;
   1228 	char *tp, *r;
   1229 
   1230 	Xinit(ts, tp, 16, ATEMP);
   1231 	/* : only for DOASNTILDE form */
   1232 	while (p[0] == CHAR && !ISDIRSEP(p[1])
   1233 	       && (!isassign || p[1] != PATHSEP))
   1234 	{
   1235 		Xcheck(ts, tp);
   1236 		*tp++ = p[1];
   1237 		p += 2;
   1238 	}
   1239 	*tp = '\0';
   1240 	r = (p[0] == EOS || p[0] == CHAR || p[0] == CSUBST) ? tilde(Xstring(ts, tp)) : (char *) 0;
   1241 	Xfree(ts, tp);
   1242 	if (r) {
   1243 		while (*r) {
   1244 			Xcheck(*dsp, dp);
   1245 			if (ISMAGIC(*r))
   1246 				*dp++ = MAGIC;
   1247 			*dp++ = *r++;
   1248 		}
   1249 		*dpp = dp;
   1250 		r = p;
   1251 	}
   1252 	return r;
   1253 }
   1254 
   1255 /*
   1256  * tilde expansion
   1257  *
   1258  * based on a version by Arnold Robbins
   1259  */
   1260 
   1261 static char *
   1262 tilde(cp)
   1263 	char *cp;
   1264 {
   1265 	char *dp;
   1266 
   1267 	if (cp[0] == '\0')
   1268 		dp = str_val(global("HOME"));
   1269 	else if (cp[0] == '+' && cp[1] == '\0')
   1270 		dp = str_val(global("PWD"));
   1271 	else if (cp[0] == '-' && cp[1] == '\0')
   1272 		dp = str_val(global("OLDPWD"));
   1273 	else
   1274 		dp = homedir(cp);
   1275 	/* If HOME, PWD or OLDPWD are not set, don't expand ~ */
   1276 	if (dp == null)
   1277 		dp = (char *) 0;
   1278 	return dp;
   1279 }
   1280 
   1281 /*
   1282  * map userid to user's home directory.
   1283  * note that 4.3's getpw adds more than 6K to the shell,
   1284  * and the YP version probably adds much more.
   1285  * we might consider our own version of getpwnam() to keep the size down.
   1286  */
   1287 
   1288 static char *
   1289 homedir(name)
   1290 	char *name;
   1291 {
   1292 	register struct tbl *ap;
   1293 
   1294 	ap = tenter(&homedirs, name, hash(name));
   1295 	if (!(ap->flag & ISSET)) {
   1296 #ifdef OS2
   1297 		/* No usernames in OS2 - punt */
   1298 		return NULL;
   1299 #else /* OS2 */
   1300 		struct passwd *pw;
   1301 
   1302 		pw = getpwnam(name);
   1303 		if (pw == NULL)
   1304 			return NULL;
   1305 		ap->val.s = str_save(pw->pw_dir, APERM);
   1306 		ap->flag |= DEFINED|ISSET|ALLOC;
   1307 #endif /* OS2 */
   1308 	}
   1309 	return ap->val.s;
   1310 }
   1311 
   1312 #ifdef BRACE_EXPAND
   1313 static void
   1314 alt_expand(wp, start, exp_start, end, fdo)
   1315 	XPtrV *wp;
   1316 	char *start, *exp_start;
   1317 	char *end;
   1318 	int fdo;
   1319 {
   1320 	int UNINITIALIZED(count);
   1321 	char *brace_start, *brace_end, *UNINITIALIZED(comma);
   1322 	char *field_start;
   1323 	char *p;
   1324 
   1325 	/* search for open brace */
   1326 	for (p = exp_start; (p = strchr(p, MAGIC)) && p[1] != OBRACE; p += 2)
   1327 		;
   1328 	brace_start = p;
   1329 
   1330 	/* find matching close brace, if any */
   1331 	if (p) {
   1332 		comma = (char *) 0;
   1333 		count = 1;
   1334 		for (p += 2; *p && count; p++) {
   1335 			if (ISMAGIC(*p)) {
   1336 				if (*++p == OBRACE)
   1337 					count++;
   1338 				else if (*p == CBRACE)
   1339 					--count;
   1340 				else if (*p == ',' && count == 1)
   1341 					comma = p;
   1342 			}
   1343 		}
   1344 	}
   1345 	/* no valid expansions... */
   1346 	if (!p || count != 0) {
   1347 		/* Note that given a{{b,c} we do not expand anything (this is
   1348 		 * what at&t ksh does.  This may be changed to do the {b,c}
   1349 		 * expansion. }
   1350 		 */
   1351 		if (fdo & DOGLOB)
   1352 			glob(start, wp, fdo & DOMARKDIRS);
   1353 		else
   1354 			XPput(*wp, debunk(start, start, end - start));
   1355 		return;
   1356 	}
   1357 	brace_end = p;
   1358 	if (!comma) {
   1359 		alt_expand(wp, start, brace_end, end, fdo);
   1360 		return;
   1361 	}
   1362 
   1363 	/* expand expression */
   1364 	field_start = brace_start + 2;
   1365 	count = 1;
   1366 	for (p = brace_start + 2; p != brace_end; p++) {
   1367 		if (ISMAGIC(*p)) {
   1368 			if (*++p == OBRACE)
   1369 				count++;
   1370 			else if ((*p == CBRACE && --count == 0)
   1371 				 || (*p == ',' && count == 1))
   1372 			{
   1373 				char *new;
   1374 				int l1, l2, l3;
   1375 
   1376 				l1 = brace_start - start;
   1377 				l2 = (p - 1) - field_start;
   1378 				l3 = end - brace_end;
   1379 				new = (char *) alloc(l1 + l2 + l3 + 1, ATEMP);
   1380 				memcpy(new, start, l1);
   1381 				memcpy(new + l1, field_start, l2);
   1382 				memcpy(new + l1 + l2, brace_end, l3);
   1383 				new[l1 + l2 + l3] = '\0';
   1384 				alt_expand(wp, new, new + l1,
   1385 					   new + l1 + l2 + l3, fdo);
   1386 				field_start = p + 1;
   1387 			}
   1388 		}
   1389 	}
   1390 	return;
   1391 }
   1392 #endif /* BRACE_EXPAND */
   1393