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