1 /* $OpenBSD: eval.c,v 1.40 2013/09/14 20:09:30 millert Exp $ */
4 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
5 * 2011, 2012, 2013, 2014, 2015, 2016
6 * mirabilos <m@mirbsd.org>
8 * Provided that these terms and disclaimer and all copyright notices
9 * are retained or reproduced in an accompanying document, permission
10 * is granted to deal in this work without restriction, including un-
11 * limited rights to use, publicly perform, distribute, sell, modify,
12 * merge, give away, or sublicence.
14 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 * the utmost extent permitted by applicable law, neither express nor
16 * implied; without malicious intent or gross negligence. In no event
17 * may a licensor, author or contributor be held liable for indirect,
18 * direct, other damage, loss, or other issues arising in any way out
19 * of dealing in the work, even if advised of the possibility of such
20 * damage or existence of a defect, except proven that it results out
21 * of said person's immediate fault when using the work as intended.
26 __RCSID("$MirOS: src/bin/mksh/eval.c,v 1.180 2016/01/19 23:12:12 tg Exp $");
31 * first pass: quoting, IFS separation, ~, ${}, $() and $(()) substitution.
32 * second pass: alternation ({,}), filename expansion (*?[]).
35 /* expansion generator state */
37 /* not including an "int type;" member, see expand() */
47 /* variable in ${var...} */
49 /* split "$@" / call waitlast in $() */
53 #define XBASE 0 /* scanning original */
54 #define XSUB 1 /* expanding ${} string */
55 #define XARGSEP 2 /* ifs0 between "$*" */
56 #define XARG 3 /* expanding $*, $@ */
57 #define XCOM 4 /* expanding $() */
58 #define XNULLSUB 5 /* "$@" when $# is 0 (don't generate word) */
59 #define XSUBMID 6 /* middle of expanding ${} */
61 /* States used for field splitting */
62 #define IFS_WORD 0 /* word has chars (or quotes except "$@") */
63 #define IFS_WS 1 /* have seen IFS white-space */
64 #define IFS_NWS 2 /* have seen IFS non-white-space */
65 #define IFS_IWS 3 /* begin of word, ignore IFS WS */
66 #define IFS_QUOTE 4 /* beg.w/quote, become IFS_WORD unless "$@" */
68 static int varsub(Expand *, const char *, const char *, int *, int *);
69 static int comsub(Expand *, const char *, int);
70 static char *valsub(struct op *, Area *);
71 static char *trimsub(char *, char *, int);
72 static void glob(char *, XPtrV *, bool);
73 static void globit(XString *, char **, char *, XPtrV *, int);
74 static const char *maybe_expand_tilde(const char *, XString *, char **, bool);
76 static char *homedir(char *);
78 static void alt_expand(XPtrV *, char *, char *, char *, int);
79 static int utflen(const char *) MKSH_A_PURE;
80 static void utfincptr(const char *, mksh_ari_t *);
82 /* UTFMODE functions */
103 utfincptr(const char *s, mksh_ari_t *lp)
108 cp += utf_ptradj(cp);
112 /* compile and expand word */
114 substitute(const char *cp, int f)
116 struct source *s, *sold;
119 s = pushs(SWSTR, ATEMP);
120 s->start = s->str = cp;
122 if (yylex(ONEWORD) != LWORD)
123 internal_errorf("bad substitution");
126 return (evalstr(yylval.cp, f));
133 eval(const char **ap, int f)
138 union mksh_ccphack vap;
144 /* space for shell name */
147 expand(*ap++, &w, f);
149 return ((char **)XPclose(w) + 1);
156 evalstr(const char *cp, int f)
170 * expand string - return only one component
171 * used from iosetup to expand redirection files
174 evalonestr(const char *cp, int f)
186 rv = (char *) *XPptrv(w);
189 rv = evalstr(cp, f&~DOGLOB);
196 /* for nested substitution: ${var:=$var2} */
197 typedef struct SubType {
198 struct tbl *var; /* variable for ${var..} */
199 struct SubType *prev; /* old type */
200 struct SubType *next; /* poped type (to avoid re-allocating) */
201 size_t base; /* begin position of expanded word */
202 short stype; /* [=+-?%#] action after expanded word */
203 short f; /* saved value of f (DOPAT, etc) */
204 uint8_t quotep; /* saved value of quote (for ${..[%#]..}) */
205 uint8_t quotew; /* saved value of quote (for ${..[+-=]..}) */
222 /* destination string and live pointer */
227 /* second pass flags */
231 /* field splitting of parameter/command substitution */
233 /* expansion variables */
235 NULL, { NULL }, NULL, 0
237 SubType st_head, *st;
238 /* record number of trailing newlines in COMSUB */
240 bool saw_eq, make_magic;
241 unsigned int tilde_ok;
246 internal_errorf("expand(NULL)");
247 /* for alias, readonly, set, typeset commands */
248 if ((f & DOVACHECK) && is_wdvarassign(ccp)) {
249 f &= ~(DOVACHECK | DOBLANK | DOGLOB | DOTILDE);
250 f |= DOASNTILDE | DOSCALAR;
256 if (Flag(FBRACEEXPAND) && (f & DOGLOB))
259 /* init destination string */
260 Xinit(ds, dp, 128, ATEMP);
266 tilde_ok = (f & (DOTILDE | DOASNTILDE)) ? 1 : 0;
269 word = (f&DOBLANK) ? IFS_WS : IFS_WORD;
270 /* clang doesn't know OSUBST comes before CSUBST */
271 memset(&st_head, 0, sizeof(st_head));
274 while (/* CONSTCOND */ 1) {
279 /* original prefixed string */
289 /* temporary quote */
294 if (word != IFS_WORD)
300 if (word == IFS_QUOTE)
308 if (f & DONTRUNCOMMAND) {
311 *dp++ = c == COMSUB ? '(' : '{';
313 *dp++ = c == FUNSUB ? ' ' : '|';
314 while (*sp != '\0') {
324 type = comsub(&x, sp, c);
325 if (type != XBASE && (f & DOBLANK))
333 if (f & DONTRUNCOMMAND) {
335 *dp++ = '$'; *dp++ = '('; *dp++ = '(';
336 while (*sp != '\0') {
340 *dp++ = ')'; *dp++ = ')';
344 v.flag = DEFINED|ISSET|INTEGER;
348 v_evaluate(&v, substitute(sp, 0),
349 KSH_UNWIND_ERROR, true);
358 /* ${{#}var{:}[=+-?#%]word} */
361 * OSUBST [{x] plain-variable-part \0
362 * compiled-word-part CSUBST [}x]
363 * This is where all syntax checking gets done...
365 /* skip the { or x (}) */
366 const char *varname = ++sp;
371 sp = cstrchr(sp, '\0') + 1;
372 type = varsub(&x, varname, sp, &stype, &slen);
374 char *beg, *end, *str;
378 end = (beg = wdcopy(sp, ATEMP)) +
379 (wdscan(sp, CSUBST) - sp);
380 /* ({) the } or x is already skipped */
381 if (end < wdscan(beg, EOS))
383 str = snptreef(NULL, 64, "%S", beg);
385 errorf("%s: %s", str, "bad substitution");
390 if (word == IFS_QUOTE && type != XNULLSUB)
397 newst = alloc(sizeof(SubType), ATEMP);
404 st->base = Xsavepos(ds, dp);
406 if (x.var == &vtemp) {
408 st->var->flag &= ~INTEGER;
409 /* can't fail here */
412 KSH_RETURN_ERROR | 0x4);
416 st->quotew = st->quotep = quote;
417 /* skip qualifier(s) */
420 switch (stype & 0x17F) {
422 x.str = shf_smprintf("%08X",
423 (unsigned int)hash(str_val(st->var)));
428 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
429 print_value_quoted(&shf, str_val(st->var));
430 x.str = shf_sclose(&shf);
434 char *beg, *mid, *end, *stg;
435 mksh_ari_t from = 0, num = -1, flen, finc = 0;
437 beg = wdcopy(sp, ATEMP);
438 mid = beg + (wdscan(sp, ADELIM) - sp);
439 stg = beg + (wdscan(sp, CSUBST) - sp);
441 if (mid[-1] == /*{*/'}') {
446 (wdscan(mid, ADELIM) - mid);
447 if (end[-1] != /*{*/ '}')
448 /* more than max delimiters */
449 goto unwind_substsyn;
453 evaluate(substitute(stg = wdstrip(beg, 0), 0),
454 &from, KSH_UNWIND_ERROR, true);
457 evaluate(substitute(stg = wdstrip(mid, 0), 0),
458 &num, KSH_UNWIND_ERROR, true);
462 beg = str_val(st->var);
468 finc = from < flen ? from : flen;
470 utfincptr(beg, &finc);
473 if (num < 0 || num > flen)
476 utfincptr(beg, &num);
477 strndupx(x.str, beg, num, ATEMP);
481 char *s, *p, *d, *sbeg, *end;
483 char fpat = 0, *tpat1, *tpat2;
485 s = wdcopy(sp, ATEMP);
486 p = s + (wdscan(sp, ADELIM) - sp);
487 d = s + (wdscan(sp, CSUBST) - sp);
489 if (p[-1] == /*{*/'}')
493 sp += (d ? d : p) - s - 1;
494 if (!(stype & 0x80) &&
496 (s[1] == '#' || s[1] == '%'))
498 pat = evalstr(s + (fpat ? 2 : 0),
499 DOTILDE | DOSCALAR | DOPAT);
500 rrep = d ? evalstr(p,
501 DOTILDE | DOSCALAR) : null;
504 /* check for special cases */
505 if (!*pat && !fpat) {
512 if ((stype & 0x80) &&
513 gmatchx(null, pat, false)) {
515 * pattern matches empty
516 * string => don't loop
521 /* prepare string on which to work */
522 strdupx(s, str_val(st->var), ATEMP);
525 /* first see if we have any match at all */
527 /* anchor at the beginning */
528 tpat1 = shf_smprintf("%s%c*", pat, MAGIC);
530 } else if (fpat == '%') {
531 /* anchor at the end */
532 tpat1 = shf_smprintf("%c*%s", MAGIC, pat);
536 tpat1 = shf_smprintf("%c*%s%c*", MAGIC, pat, MAGIC);
541 * this would not be necessary if gmatchx would return
542 * the start and end values of a match found, like re*
544 if (!gmatchx(sbeg, tpat1, false))
547 /* now anchor the beginning of the match */
549 while (sbeg <= end) {
550 if (gmatchx(sbeg, tpat2, false))
555 /* now anchor the end of the match */
563 gotmatch = tobool(gmatchx(sbeg, pat, false));
569 strndupx(end, s, sbeg - s, ATEMP);
570 d = shf_smprintf("%s%s%s", end, rrep, p);
572 sbeg = d + (sbeg - s) + strlen(rrep);
588 /* ! DOBLANK,DOBRACE */
589 f = (f & DONTRUNCOMMAND) |
593 st->quotew = quote = 0;
595 * Prepend open pattern (so |
596 * in a trim will work as
606 * Enabling tilde expansion
608 * non-standard ksh, but is
609 * consistent with rules for
610 * other assignments. Not
611 * sure what POSIX thinks of
613 * Not doing tilde expansion
614 * for integer variables is a
615 * non-POSIX thing - makes
616 * sense though, since ~ is
617 * a arithmetic operator.
619 if (!(x.var->flag & INTEGER))
620 f |= DOASNTILDE | DOTILDE;
623 * These will be done after the
624 * value has been assigned.
626 f &= ~(DOBLANK|DOGLOB|DOBRACE);
631 errorf("%s: parameter null or not set",
640 else if (dp == Xstring(ds, dp))
642 /* Enable tilde expansion */
648 sp += wdscan(sp, CSUBST) - sp;
652 /* only get here if expanding word */
654 /* ({) skip the } or x */
656 /* in case of ${unset:-} */
663 switch (st->stype & 0x17F) {
667 /* Append end-pattern */
672 dp = Xrestpos(ds, dp, st->base);
674 * Must use st->var since calling
675 * global would break things
678 x.str = trimsub(str_val(st->var),
680 if (x.str[0] != '\0') {
687 if (dp == Xstring(ds, dp))
697 * Restore our position and substitute
698 * the value of st->var (may not be
699 * the assigned value in the presence
700 * of integer/right-adj/etc attributes).
702 dp = Xrestpos(ds, dp, st->base);
704 * Must use st->var since calling
705 * global would cause with things
706 * like x[i+=1] to be evaluated twice.
709 * Note: not exported by FEXPORT
713 * XXX POSIX says readonly is only
714 * fatal for special builtins (setstr
715 * does readonly check).
717 len = strlen(dp) + 1;
719 debunk(alloc(len, ATEMP),
720 dp, len), KSH_UNWIND_ERROR);
721 x.str = str_val(st->var);
726 word = quote || (!*x.str && (f & DOSCALAR)) ? IFS_WORD : IFS_IWS;
729 dp = Xrestpos(ds, dp, st->base);
731 errorf("%s: %s", st->var->name,
732 debunk(dp, dp, strlen(dp) + 1));
738 dp = Xrestpos(ds, dp, st->base);
740 word = quote || (!*x.str && (f & DOSCALAR)) ? IFS_WORD : IFS_IWS;
745 /* default: '-' '+' */
752 /* open pattern: *(foo|bar) */
753 /* Next char is the type of pattern */
759 /* pattern separator (|) */
774 * Special case for "$@" (and "${foo[@]}") - no
775 * word is generated if $# is 0 (unless there is
776 * other stuff inside the quotes).
781 if (dp == Xstring(ds, dp) && word != IFS_WORD)
788 if ((c = *x.str++) == 0) {
801 if ((c = *x.str++) == '\0') {
803 * force null words to be created so
804 * set -- "" 2 ""; echo "$@" will do
807 if (quote && x.split)
809 if ((x.str = *x.u.strv++) == NULL) {
816 if ((f & DOHEREDOC)) {
817 /* pseudo-field-split reliably */
822 if ((f & DOSCALAR)) {
823 /* do not field-split */
832 if (quote && !x.split)
834 if (!quote && word == IFS_WS)
836 /* this is so we don't terminate */
838 /* now force-emit a word */
841 if (quote && x.split) {
842 /* terminate word for "$@" */
850 if (x.u.shf == NULL) {
855 } else if (newlines) {
856 /* spit out saved NLs */
860 while ((c = shf_getc(x.u.shf)) == 0 || c == '\n')
864 if (newlines && c != -1) {
865 shf_ungetc(c, x.u.shf);
875 subst_exstat = waitlast();
884 /* check for end of word or IFS separation */
885 if (c == 0 || (!quote && (f & DOBLANK) && doblank &&
886 !make_magic && ctype(c, C_IFS))) {
888 * How words are broken up:
891 * -----------------------------------
892 * IFS_WORD w/WS w/NWS w
893 * IFS_WS -/WS -/NWS -
894 * IFS_NWS -/NWS w/NWS -
895 * IFS_IWS -/WS w/NWS -
896 * (w means generate a word)
898 if ((word == IFS_WORD) || (word == IFS_QUOTE) || (c &&
899 (word == IFS_IWS || word == IFS_NWS) &&
900 !ctype(c, C_IFSWS))) {
907 /* also does globbing */
908 alt_expand(wp, cp, cp,
909 cp + Xlength(ds, (dp - 1)),
910 fdo | (f & DOMARKDIRS));
911 else if (fdo & DOGLOB)
912 glob(cp, wp, tobool(f & DOMARKDIRS));
913 else if ((f & DOPAT) || !(fdo & DOMAGIC))
916 XPput(*wp, debunk(cp, cp,
921 tilde_ok = (f & (DOTILDE | DOASNTILDE)) ? 1 : 0;
924 Xinit(ds, dp, 128, ATEMP);
927 } else if (type == XSUB && ctype(c, C_IFS) &&
928 !ctype(c, C_IFSWS) && Xlength(ds, dp) == 0) {
929 *(cp = alloc(1, ATEMP)) = '\0';
934 word = ctype(c, C_IFSWS) ? IFS_WS : IFS_NWS;
937 if (word == IFS_NWS &&
938 Xlength(ds, dp) == 0) {
939 *(cp = alloc(1, ATEMP)) = '\0';
945 /* age tilde_ok info - ~ code tests second bit */
947 /* mark any special second pass chars */
955 * For character classes - doesn't hurt
956 * to have magic !,-,]s outside of
959 if (f & (DOPAT | DOGLOB)) {
968 if (f & (DOPAT | DOGLOB)) {
969 fdo |= DOMAGIC | (f & DOGLOB);
976 if ((f & DOBRACE) && (c == '{' /*}*/ ||
978 fdo |= DOBRACE|DOMAGIC;
983 /* Note first unquoted = for ~ */
984 if (!(f & DOTEMP) && (!Flag(FPOSIX) ||
985 (f & DOASNTILDE)) && !saw_eq) {
992 /* Note unquoted : for ~ */
993 if (!(f & DOTEMP) && (f & DOASNTILDE))
998 * tilde_ok is reset whenever
999 * any of ' " $( $(( ${ } are seen.
1000 * Note that tilde_ok must be preserved
1001 * through the sequence ${A=a=}~
1003 if (type == XBASE &&
1004 (f & (DOTILDE | DOASNTILDE)) &&
1009 tcp = maybe_expand_tilde(sp,
1011 tobool(f & DOASNTILDE));
1023 /* undo temporary */
1028 fdo |= DOMAGIC | (f & DOGLOB);
1030 } else if (ISMAGIC(c)) {
1034 /* save output char */
1042 hasnonempty(const char **strv)
1053 * Prepare to generate the string returned by ${} substitution.
1056 varsub(Expand *xp, const char *sp, const char *word,
1057 int *stypep, /* becomes qualifier type */
1058 int *slenp) /* " " len (=, :=, etc.) valid iff *stypep != 0 */
1061 int state; /* next state: XBASE, XARG, XSUB, XNULLSUB */
1062 int stype; /* substitution type */
1066 bool zero_ok = false;
1068 if ((stype = sp[0]) == '\0')
1069 /* Bad variable name */
1075 * ${#var}, string length (-U: characters, +U: octets) or array size
1076 * ${%var}, string width (-U: screen columns, +U: octets)
1079 if (stype == '%' && c == '\0')
1081 if ((stype == '#' || stype == '%') && c != '\0') {
1082 /* Can't have any modifiers for ${#...} or ${%...} */
1083 if (*word != CSUBST)
1086 /* Check for size of array */
1087 if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') &&
1093 vp = global(arrayname(sp));
1094 if (vp->flag & (ISSET|ARRAY))
1096 for (; vp; vp = vp->u.array)
1097 if (vp->flag & ISSET)
1100 } else if (c == '*' || c == '@') {
1105 p = str_val(global(sp));
1106 zero_ok = p != null;
1110 /* partial utf_mbswidth reimplementation */
1118 if (!UTFMODE || (len = utf_mbtowc(&wc,
1120 /* not UTFMODE or not UTF-8 */
1121 wc = (unsigned char)(*s++);
1123 /* UTFMODE and UTF-8 */
1125 /* wc == char or wchar at s++ */
1126 if ((cw = utf_wcwidth(wc)) == -1) {
1127 /* 646, 8859-1, 10646 C0/C1 */
1135 if (Flag(FNOUNSET) && c == 0 && !zero_ok)
1136 errorf("%s: %s", sp, "parameter not set");
1137 /* unqualified variable/string substitution */
1139 xp->str = shf_smprintf("%d", c);
1143 /* Check for qualifiers in word part */
1145 c = word[slen = 0] == CHAR ? word[1] : 0;
1149 c = word[slen + 0] == CHAR ? word[slen + 1] : 0;
1151 if (!stype && c == '/') {
1154 if (word[slen] == ADELIM) {
1158 } else if (stype == 0x80 && (c == ' ' || c == '0')) {
1160 } else if (ctype(c, C_SUBOP1)) {
1163 } else if (ctype(c, C_SUBOP2)) {
1164 /* Note: ksh88 allows :%, :%%, etc */
1167 if (word[slen + 0] == CHAR && c == word[slen + 1]) {
1171 } else if (c == '@') {
1172 /* @x where x is command char */
1175 if (word[slen] == CHAR) {
1176 stype |= word[slen + 1];
1182 if (!stype && *word != CSUBST)
1188 if (c == '*' || c == '@') {
1189 switch (stype & 0x17F) {
1190 /* can't assign to a vector */
1192 /* can't trim a vector (yet) */
1201 if (e->loc->argc == 0) {
1203 xp->var = global(sp);
1204 state = c == '@' ? XNULLSUB : XSUB;
1206 xp->u.strv = (const char **)e->loc->argv + 1;
1207 xp->str = *xp->u.strv++;
1209 xp->split = tobool(c == '@');
1215 if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') &&
1219 switch (stype & 0x17F) {
1220 /* can't assign to a vector */
1222 /* can't trim a vector (yet) */
1233 if ((c = sp[0]) == '!')
1235 vp = global(arrayname(sp));
1236 for (; vp; vp = vp->u.array) {
1237 if (!(vp->flag&ISSET))
1239 XPput(wv, c == '!' ? shf_smprintf("%lu",
1243 if (XPsize(wv) == 0) {
1245 state = p[1] == '@' ? XNULLSUB : XSUB;
1249 xp->u.strv = (const char **)XPptrv(wv);
1250 xp->str = *xp->u.strv++;
1252 xp->split = tobool(p[1] == '@');
1256 /* Can't assign things like $! or $1 */
1257 if ((stype & 0x17F) == '=' &&
1258 ctype(*sp, C_VAR1 | C_DIGIT))
1260 if (*sp == '!' && sp[1]) {
1262 xp->var = global(sp);
1263 if (vstrchr(sp, '['))
1264 xp->str = shf_smprintf("%s[%lu]",
1266 arrayindex(xp->var));
1268 xp->str = xp->var->name;
1270 xp->var = global(sp);
1271 xp->str = str_val(xp->var);
1278 /* test the compiler's code generator */
1279 if (((stype < 0x100) && (ctype(c, C_SUBOP2) || c == '/' ||
1280 (((stype & 0x80) ? *xp->str == '\0' : xp->str == null) &&
1281 (state != XARG || (ifs0 || xp->split ?
1282 (xp->u.strv[0] == NULL) : !hasnonempty(xp->u.strv))) ?
1283 c == '=' || c == '-' || c == '?' : c == '+'))) ||
1284 stype == (0x80 | '0') || stype == (0x100 | '#') ||
1285 stype == (0x100 | 'Q'))
1286 /* expand word instead of variable value */
1288 if (Flag(FNOUNSET) && xp->str == null && !zero_ok &&
1289 (ctype(c, C_SUBOP2) || (state != XBASE && c != '+')))
1290 errorf("%s: %s", sp, "parameter not set");
1295 * Run the command in $(...) and read its output.
1298 comsub(Expand *xp, const char *cp, int fn MKSH_A_UNUSED)
1303 uint8_t old_utfmode = UTFMODE;
1305 s = pushs(SSTRING, ATEMP);
1306 s->start = s->str = cp;
1308 t = compile(s, true);
1312 UTFMODE = old_utfmode;
1317 /* no waitlast() unless specifically enabled later */
1320 if (t->type == TCOM &&
1321 *t->args == NULL && *t->vars == NULL && t->ioact != NULL) {
1323 struct ioword *io = *t->ioact;
1326 if ((io->ioflag & IOTYPE) != IOREAD)
1327 errorf("%s: %s", T_funny_command,
1328 snptreef(NULL, 32, "%R", io));
1329 shf = shf_open(name = evalstr(io->ioname, DOTILDE), O_RDONLY,
1330 0, SHF_MAPHI | SHF_CLEXEC);
1332 warningf(!Flag(FTALKING), "%s: %s %s: %s", name,
1333 "can't open", "$(<...) input", cstrerror(errno));
1334 } else if (fn == FUNSUB) {
1336 struct temp *tf = NULL;
1339 * create a temporary file, open for reading and writing,
1340 * with an shf open for reading (buffered) but yet unused
1342 maketemp(ATEMP, TT_FUNSUB, &tf);
1344 errorf("can't %s temporary file %s: %s",
1345 "create", tf->tffn, cstrerror(errno));
1347 /* extract shf from temporary file, unlink and free it */
1351 /* save stdout and let it point to the tempfile */
1353 ksh_dup2(shf_fileno(shf), 1, false);
1355 * run tree, with output thrown into the tempfile,
1356 * in a new function block
1359 subst_exstat = exstat & 0xFF;
1360 /* rewind the tempfile and restore regular stdout */
1361 lseek(shf_fileno(shf), (off_t)0, SEEK_SET);
1363 } else if (fn == VALSUB) {
1364 xp->str = valsub(t, ATEMP);
1365 subst_exstat = exstat & 0xFF;
1371 shf = shf_fdopen(pv[0], SHF_RD, NULL);
1374 ksh_dup2(pv[1], 1, false);
1377 execute(t, XXCOM | XPIPEO | XFORK, NULL);
1389 * perform #pattern and %pattern substitution in ${}
1392 trimsub(char *str, char *pat, int how)
1394 char *end = strnul(str);
1397 switch (how & 0xFF) {
1399 /* shortest match at beginning */
1400 for (p = str; p <= end; p += utf_ptradj(p)) {
1402 if (gmatchx(str, pat, false)) {
1410 /* longest match at beginning */
1411 for (p = end; p >= str; p--) {
1413 if (gmatchx(str, pat, false)) {
1421 /* shortest match at end */
1424 if (gmatchx(p, pat, false))
1428 while ((p-- > str) && ((*p & 0xC0) == 0x80))
1430 if ((p < str) || (p + utf_ptradj(p) != op))
1437 /* longest match at end */
1438 for (p = str; p <= end; p++)
1439 if (gmatchx(p, pat, false)) {
1441 strndupx(end, str, p - str, ATEMP);
1447 /* no match, return string */
1453 * Name derived from V6's /etc/glob, the program that expanded filenames.
1456 /* XXX cp not const 'cause slashes are temporarily replaced with NULs... */
1458 glob(char *cp, XPtrV *wp, bool markdirs)
1460 int oldsize = XPsize(*wp);
1462 if (glob_str(cp, wp, markdirs) == 0)
1463 XPput(*wp, debunk(cp, cp, strlen(cp) + 1));
1465 qsort(XPptrv(*wp) + oldsize, XPsize(*wp) - oldsize,
1466 sizeof(void *), xstrcmp);
1470 #define GF_EXCHECK BIT(0) /* do existence check on file */
1471 #define GF_GLOBBED BIT(1) /* some globbing has been done */
1472 #define GF_MARKDIR BIT(2) /* add trailing / to directories */
1475 * Apply file globbing to cp and store the matching files in wp. Returns
1476 * the number of matches found.
1479 glob_str(char *cp, XPtrV *wp, bool markdirs)
1481 int oldsize = XPsize(*wp);
1485 Xinit(xs, xp, 256, ATEMP);
1486 globit(&xs, &xp, cp, wp, markdirs ? GF_MARKDIR : GF_NONE);
1489 return (XPsize(*wp) - oldsize);
1493 globit(XString *xs, /* dest string */
1494 char **xpp, /* ptr to dest end */
1495 char *sp, /* source path */
1496 XPtrV *wp, /* output list */
1497 int check) /* GF_* flags */
1499 char *np; /* next source component */
1504 /* This to allow long expansions to be interrupted */
1508 /* end of source path */
1510 * We only need to check if the file exists if a pattern
1511 * is followed by a non-pattern (eg, foo*x/bar; no check
1512 * is needed for foo* since the match must exist) or if
1513 * any patterns were expanded and the markdirs option is set.
1514 * Symlinks make things a bit tricky...
1516 if ((check & GF_EXCHECK) ||
1517 ((check & GF_MARKDIR) && (check & GF_GLOBBED))) {
1518 #define stat_check() (stat_done ? stat_done : (stat_done = \
1519 stat(Xstring(*xs, xp), &statb) < 0 ? -1 : 1))
1520 struct stat lstatb, statb;
1521 /* -1: failed, 1 ok, 0 not yet done */
1524 if (mksh_lstat(Xstring(*xs, xp), &lstatb) < 0)
1527 * special case for systems which strip trailing
1528 * slashes from regular files (eg, /etc/passwd/).
1529 * SunOS 4.1.3 does this...
1531 if ((check & GF_EXCHECK) && xp > Xstring(*xs, xp) &&
1532 xp[-1] == '/' && !S_ISDIR(lstatb.st_mode) &&
1533 (!S_ISLNK(lstatb.st_mode) ||
1534 stat_check() < 0 || !S_ISDIR(statb.st_mode)))
1537 * Possibly tack on a trailing / if there isn't already
1538 * one and if the file is a directory or a symlink to a
1541 if (((check & GF_MARKDIR) && (check & GF_GLOBBED)) &&
1542 xp > Xstring(*xs, xp) && xp[-1] != '/' &&
1543 (S_ISDIR(lstatb.st_mode) ||
1544 (S_ISLNK(lstatb.st_mode) && stat_check() > 0 &&
1545 S_ISDIR(statb.st_mode)))) {
1550 strndupx(np, Xstring(*xs, xp), Xlength(*xs, xp), ATEMP);
1555 if (xp > Xstring(*xs, xp))
1557 while (*sp == '/') {
1561 np = strchr(sp, '/');
1564 /* don't assume '/', can be multiple kinds */
1568 odirsep = '\0'; /* keep gcc quiet */
1569 se = sp + strlen(sp);
1574 * Check if sp needs globbing - done to avoid pattern checks for strings
1575 * containing MAGIC characters, open [s without the matching close ],
1576 * etc. (otherwise opendir() will be called which may fail because the
1577 * directory isn't readable - if no globbing is needed, only execute
1578 * permission should be required (as per POSIX)).
1580 if (!has_globbing(sp, se)) {
1581 XcheckN(*xs, xp, se - sp + 1);
1582 debunk(xp, sp, Xnleft(*xs, xp));
1585 globit(xs, xpp, np, wp, check);
1590 size_t len, prefix_len;
1592 /* xp = *xpp; copy_non_glob() may have re-alloc'd xs */
1594 prefix_len = Xlength(*xs, xp);
1595 dirp = opendir(prefix_len ? Xstring(*xs, xp) : ".");
1598 while ((d = readdir(dirp)) != NULL) {
1600 if (name[0] == '.' &&
1601 (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1602 /* always ignore . and .. */
1604 if ((*name == '.' && *sp != '.') ||
1605 !gmatchx(name, sp, true))
1608 len = strlen(d->d_name) + 1;
1609 XcheckN(*xs, xp, len);
1610 memcpy(xp, name, len);
1611 *xpp = xp + len - 1;
1612 globit(xs, xpp, np, wp,
1613 (check & GF_MARKDIR) | GF_GLOBBED
1614 | (np ? GF_EXCHECK : GF_NONE));
1615 xp = Xstring(*xs, xp) + prefix_len;
1626 /* remove MAGIC from string */
1628 debunk(char *dp, const char *sp, size_t dlen)
1633 if ((s = cstrchr(sp, MAGIC))) {
1634 if (s - sp >= (ssize_t)dlen)
1636 memmove(dp, sp, s - sp);
1637 for (d = dp + (s - sp); *s && (d - dp < (ssize_t)dlen); s++)
1638 if (!ISMAGIC(*s) || !(*++s & 0x80) ||
1639 !vstrchr("*+?@! ", *s & 0x7f))
1642 /* extended pattern operators: *+?@! */
1643 if ((*s & 0x7f) != ' ')
1645 if (d - dp < (ssize_t)dlen)
1649 } else if (dp != sp)
1650 strlcpy(dp, sp, dlen);
1655 * Check if p is an unquoted name, possibly followed by a / or :. If so
1656 * puts the expanded version in *dcp,dp and returns a pointer in p just
1657 * past the name, otherwise returns 0.
1660 maybe_expand_tilde(const char *p, XString *dsp, char **dpp, bool isassign)
1667 Xinit(ts, tp, 16, ATEMP);
1668 /* : only for DOASNTILDE form */
1669 while (p[0] == CHAR && p[1] != '/' && (!isassign || p[1] != ':'))
1676 r = (p[0] == EOS || p[0] == CHAR || p[0] == CSUBST) ?
1677 do_tilde(Xstring(ts, tp)) : NULL;
1695 * based on a version by Arnold Robbins
1704 dp = str_val(global("HOME"));
1705 else if (cp[0] == '+' && cp[1] == '\0')
1706 dp = str_val(global("PWD"));
1707 else if (ksh_isdash(cp))
1708 dp = str_val(global("OLDPWD"));
1709 #ifndef MKSH_NOPWNAM
1713 /* If HOME, PWD or OLDPWD are not set, don't expand ~ */
1714 return (dp == null ? NULL : dp);
1717 #ifndef MKSH_NOPWNAM
1719 * map userid to user's home directory.
1720 * note that 4.3's getpw adds more than 6K to the shell,
1721 * and the YP version probably adds much more.
1722 * we might consider our own version of getpwnam() to keep the size down.
1729 ap = ktenter(&homedirs, name, hash(name));
1730 if (!(ap->flag & ISSET)) {
1733 pw = getpwnam(name);
1736 strdupx(ap->val.s, pw->pw_dir, APERM);
1737 ap->flag |= DEFINED|ISSET|ALLOC;
1744 alt_expand(XPtrV *wp, char *start, char *exp_start, char *end, int fdo)
1746 unsigned int count = 0;
1747 char *brace_start, *brace_end, *comma = NULL;
1749 char *p = exp_start;
1751 /* search for open brace */
1752 while ((p = strchr(p, MAGIC)) && p[1] != '{' /*}*/)
1756 /* find matching close brace, if any */
1761 while (*p && count) {
1762 if (ISMAGIC(*p++)) {
1763 if (*p == '{' /*}*/)
1765 else if (*p == /*{*/ '}')
1767 else if (*p == ',' && count == 1)
1773 /* no valid expansions... */
1774 if (!p || count != 0) {
1776 * Note that given a{{b,c} we do not expand anything (this is
1777 * what AT&T ksh does. This may be changed to do the {b,c}
1781 glob(start, wp, tobool(fdo & DOMARKDIRS));
1783 XPput(*wp, debunk(start, start, end - start));
1788 alt_expand(wp, start, brace_end, end, fdo);
1792 /* expand expression */
1793 field_start = brace_start + 2;
1795 for (p = brace_start + 2; p != brace_end; p++) {
1797 if (*++p == '{' /*}*/)
1799 else if ((*p == /*{*/ '}' && --count == 0) ||
1800 (*p == ',' && count == 1)) {
1805 * addition safe since these operate on
1806 * one string (separate substrings)
1808 l1 = brace_start - start;
1809 l2 = (p - 1) - field_start;
1810 l3 = end - brace_end;
1811 news = alloc(l1 + l2 + l3 + 1, ATEMP);
1812 memcpy(news, start, l1);
1813 memcpy(news + l1, field_start, l2);
1814 memcpy(news + l1 + l2, brace_end, l3);
1815 news[l1 + l2 + l3] = '\0';
1816 alt_expand(wp, news, news + l1,
1817 news + l1 + l2 + l3, fdo);
1818 field_start = p + 1;
1825 /* helper function due to setjmp/longjmp woes */
1827 valsub(struct op *t, Area *ap)
1829 char * volatile cp = NULL;
1830 struct tbl * volatile vp = NULL;
1835 vp = local("REPLY", false);
1836 if (!kshsetjmp(e->jbuf))
1837 execute(t, XXCOM | XERROK, NULL);
1839 strdupx(cp, str_val(vp), ap);