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