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