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