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