1 /* $OpenBSD: eval.c,v 1.39 2013/07/01 17:25:27 jca Exp $ */
4 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
6 * Thorsten Glaser <tg@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.142 2013/07/24 18:03:57 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) */
63 #define IFS_WS 1 /* have seen IFS white-space */
64 #define IFS_NWS 2 /* have seen IFS non-white-space */
66 static int varsub(Expand *, const char *, const char *, int *, int *);
67 static int comsub(Expand *, const char *, int);
68 static char *valsub(struct op *, Area *);
69 static char *trimsub(char *, char *, int);
70 static void glob(char *, XPtrV *, bool);
71 static void globit(XString *, char **, char *, XPtrV *, int);
72 static const char *maybe_expand_tilde(const char *, XString *, char **, int);
74 static char *homedir(char *);
76 static void alt_expand(XPtrV *, char *, char *, char *, int);
77 static int utflen(const char *);
78 static void utfincptr(const char *, mksh_ari_t *);
80 /* UTFMODE functions */
101 utfincptr(const char *s, mksh_ari_t *lp)
106 cp += utf_ptradj(cp);
110 /* compile and expand word */
112 substitute(const char *cp, int f)
114 struct source *s, *sold;
117 s = pushs(SWSTR, ATEMP);
118 s->start = s->str = cp;
120 if (yylex(ONEWORD) != LWORD)
121 internal_errorf("bad substitution");
124 return (evalstr(yylval.cp, f));
131 eval(const char **ap, int f)
136 union mksh_ccphack vap;
142 /* space for shell name */
145 expand(*ap++, &w, f);
147 return ((char **)XPclose(w) + 1);
154 evalstr(const char *cp, int f)
168 * expand string - return only one component
169 * used from iosetup to expand redirection files
172 evalonestr(const char *cp, int f)
184 rv = (char *) *XPptrv(w);
187 rv = evalstr(cp, f&~DOGLOB);
194 /* for nested substitution: ${var:=$var2} */
195 typedef struct SubType {
196 struct tbl *var; /* variable for ${var..} */
197 struct SubType *prev; /* old type */
198 struct SubType *next; /* poped type (to avoid re-allocating) */
199 size_t base; /* begin position of expanded word */
200 short stype; /* [=+-?%#] action after expanded word */
201 short f; /* saved value of f (DOPAT, etc) */
202 uint8_t quotep; /* saved value of quote (for ${..[%#]..}) */
203 uint8_t quotew; /* saved value of quote (for ${..[+-=]..}) */
220 /* destination string and live pointer */
225 /* second pass flags */
229 /* field splitting of parameter/command substitution */
231 /* expansion variables */
233 NULL, { NULL }, NULL, 0
235 SubType st_head, *st;
236 /* record number of trailing newlines in COMSUB */
238 bool saw_eq, make_magic;
244 internal_errorf("expand(NULL)");
245 /* for alias, readonly, set, typeset commands */
246 if ((f & DOVACHECK) && is_wdvarassign(ccp)) {
247 f &= ~(DOVACHECK|DOBLANK|DOGLOB|DOTILDE);
254 if (Flag(FBRACEEXPAND) && (f & DOGLOB))
257 /* init destination string */
258 Xinit(ds, dp, 128, ATEMP);
264 tilde_ok = (f & (DOTILDE|DOASNTILDE)) ? 1 : 0;
267 word = (f&DOBLANK) ? IFS_WS : IFS_WORD;
268 /* clang doesn't know OSUBST comes before CSUBST */
269 memset(&st_head, 0, sizeof(st_head));
272 while (/* CONSTCOND */ 1) {
277 /* original prefixed string */
287 /* temporary quote */
303 if (f & DONTRUNCOMMAND) {
306 *dp++ = c == COMSUB ? '(' : '{';
308 *dp++ = c == FUNSUB ? ' ' : '|';
309 while (*sp != '\0') {
319 type = comsub(&x, sp, c);
320 if (type != XBASE && (f & DOBLANK))
329 if (f & DONTRUNCOMMAND) {
330 *dp++ = '$'; *dp++ = '('; *dp++ = '(';
331 while (*sp != '\0') {
335 *dp++ = ')'; *dp++ = ')';
339 v.flag = DEFINED|ISSET|INTEGER;
343 v_evaluate(&v, substitute(sp, 0),
344 KSH_UNWIND_ERROR, true);
354 /* ${{#}var{:}[=+-?#%]word} */
357 * OSUBST [{x] plain-variable-part \0
358 * compiled-word-part CSUBST [}x]
359 * This is where all syntax checking gets done...
361 /* skip the { or x (}) */
362 const char *varname = ++sp;
367 sp = cstrchr(sp, '\0') + 1;
368 type = varsub(&x, varname, sp, &stype, &slen);
370 char *beg, *end, *str;
374 end = (beg = wdcopy(sp, ATEMP)) +
375 (wdscan(sp, CSUBST) - sp);
376 /* ({) the } or x is already skipped */
377 if (end < wdscan(beg, EOS))
379 str = snptreef(NULL, 64, "%S", beg);
381 errorf("%s: %s", str, "bad substitution");
391 newst = alloc(sizeof(SubType), ATEMP);
398 st->base = Xsavepos(ds, dp);
400 if (x.var == &vtemp) {
402 st->var->flag &= ~INTEGER;
403 /* can't fail here */
406 KSH_RETURN_ERROR | 0x4);
410 st->quotew = st->quotep = quote;
411 /* skip qualifier(s) */
414 switch (stype & 0x17F) {
420 beg = wdcopy(sp, ATEMP);
421 end = beg + (wdscan(sp, CSUBST) - sp);
423 end = wdstrip(beg, 0);
425 evaluate(substitute(end, 0),
426 &seed, KSH_UNWIND_ERROR, true);
427 /* hash with seed, for now */
432 x.str = shf_smprintf("%08X",
439 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
440 print_value_quoted(&shf, str_val(st->var));
441 x.str = shf_sclose(&shf);
445 char *beg, *mid, *end, *stg;
446 mksh_ari_t from = 0, num = -1, flen, finc = 0;
448 beg = wdcopy(sp, ATEMP);
449 mid = beg + (wdscan(sp, ADELIM) - sp);
450 stg = beg + (wdscan(sp, CSUBST) - sp);
452 goto unwind_substsyn;
454 if (mid[-1] == /*{*/'}') {
459 (wdscan(mid, ADELIM) - mid);
461 /* more than max delimiters */
462 end[-1] != /*{*/ '}')
463 goto unwind_substsyn;
467 evaluate(substitute(stg = wdstrip(beg, 0), 0),
468 &from, KSH_UNWIND_ERROR, true);
471 evaluate(substitute(stg = wdstrip(mid, 0), 0),
472 &num, KSH_UNWIND_ERROR, true);
476 beg = str_val(st->var);
482 finc = from < flen ? from : flen;
484 utfincptr(beg, &finc);
487 if (num < 0 || num > flen)
490 utfincptr(beg, &num);
491 strndupx(x.str, beg, num, ATEMP);
495 char *s, *p, *d, *sbeg, *end;
497 char *tpat0, *tpat1, *tpat2;
499 s = wdcopy(sp, ATEMP);
500 p = s + (wdscan(sp, ADELIM) - sp);
501 d = s + (wdscan(sp, CSUBST) - sp);
503 goto unwind_substsyn;
505 if (p[-1] == /*{*/'}')
509 sp += (d ? d : p) - s - 1;
511 WDS_KEEPQ | WDS_MAGIC);
512 pat = substitute(tpat0, 0);
514 d = wdstrip(p, WDS_KEEPQ);
515 rrep = substitute(d, 0);
526 /* XXX really? */ s[1] == '\\' ||
534 /* check for special cases */
535 d = str_val(st->var);
539 /* anchor at begin */
556 tpat1 = tpat2 = NULL;
558 if (gmatchx(null, tpat0, false)) {
565 /* but is anchored */
566 s = shf_smprintf("%s%s",
571 /* prepare string on which to work */
572 strdupx(s, d, ATEMP);
575 /* first see if we have any match at all */
578 /* anchor at the beginning */
579 tpat1 = shf_smprintf("%s%c*", ++tpat0, MAGIC);
581 } else if (*pat == '%') {
582 /* anchor at the end */
583 tpat1 = shf_smprintf("%c*%s", MAGIC, ++tpat0);
587 tpat1 = shf_smprintf("%c*%s%c*", MAGIC, pat, MAGIC);
592 * this would not be necessary if gmatchx would return
593 * the start and end values of a match found, like re*
595 if (!gmatchx(sbeg, tpat1, false))
598 /* now anchor the beginning of the match */
600 while (sbeg <= end) {
601 if (gmatchx(sbeg, tpat2, false))
606 /* now anchor the end of the match */
614 gotmatch = tobool(gmatchx(sbeg, tpat0, false));
620 strndupx(end, s, sbeg - s, ATEMP);
621 d = shf_smprintf("%s%s%s", end, rrep, p);
623 sbeg = d + (sbeg - s) + strlen(rrep);
640 /* ! DOBLANK,DOBRACE,DOTILDE */
641 f = (f & DONTRUNCOMMAND) |
643 st->quotew = quote = 0;
645 * Prepend open pattern (so |
646 * in a trim will work as
656 * Enabling tilde expansion
658 * non-standard ksh, but is
659 * consistent with rules for
660 * other assignments. Not
661 * sure what POSIX thinks of
663 * Not doing tilde expansion
664 * for integer variables is a
665 * non-POSIX thing - makes
666 * sense though, since ~ is
667 * a arithmetic operator.
669 if (!(x.var->flag & INTEGER))
670 f |= DOASNTILDE|DOTILDE;
673 * These will be done after the
674 * value has been assigned.
676 f &= ~(DOBLANK|DOGLOB|DOBRACE);
684 /* Enable tilde expansion */
690 sp += wdscan(sp, CSUBST) - sp;
694 /* only get here if expanding word */
696 /* ({) skip the } or x */
698 /* in case of ${unset:-} */
705 switch (st->stype & 0x17F) {
709 /* Append end-pattern */
714 dp = Xrestpos(ds, dp, st->base);
716 * Must use st->var since calling
717 * global would break things
720 x.str = trimsub(str_val(st->var),
722 if (x.str[0] != '\0' || st->quotep)
732 * Restore our position and substitute
733 * the value of st->var (may not be
734 * the assigned value in the presence
735 * of integer/right-adj/etc attributes).
737 dp = Xrestpos(ds, dp, st->base);
739 * Must use st->var since calling
740 * global would cause with things
741 * like x[i+=1] to be evaluated twice.
744 * Note: not exported by FEXPORT
748 * XXX POSIX says readonly is only
749 * fatal for special builtins (setstr
750 * does readonly check).
752 len = strlen(dp) + 1;
754 debunk(alloc(len, ATEMP),
755 dp, len), KSH_UNWIND_ERROR);
756 x.str = str_val(st->var);
763 char *s = Xrestpos(ds, dp, st->base);
765 errorf("%s: %s", st->var->name,
767 "parameter null or not set" :
768 (debunk(s, s, strlen(s) + 1), s));
774 dp = Xrestpos(ds, dp, st->base);
786 /* open pattern: *(foo|bar) */
787 /* Next char is the type of pattern */
793 /* pattern separator (|) */
808 * Special case for "$@" (and "${foo[@]}") - no
809 * word is generated if $# is 0 (unless there is
810 * other stuff inside the quotes).
816 * not really correct: x=; "$x$@" should
817 * generate a null argument and
818 * set A; "${@:+}" shouldn't.
820 if (dp == Xstring(ds, dp))
827 if ((c = *x.str++) == 0) {
840 if ((c = *x.str++) == '\0') {
842 * force null words to be created so
843 * set -- '' 2 ''; foo "$@" will do
846 if (quote && x.split)
848 if ((x.str = *x.u.strv++) == NULL) {
856 if (quote && !x.split)
858 /* this is so we don't terminate */
860 /* now force-emit a word */
863 if (quote && x.split) {
864 /* terminate word for "$@" */
872 if (x.u.shf == NULL) {
877 } else if (newlines) {
878 /* spit out saved NLs */
882 while ((c = shf_getc(x.u.shf)) == 0 || c == '\n')
886 if (newlines && c != EOF) {
887 shf_ungetc(c, x.u.shf);
897 subst_exstat = waitlast();
906 /* check for end of word or IFS separation */
907 if (c == 0 || (!quote && (f & DOBLANK) && doblank &&
908 !make_magic && ctype(c, C_IFS))) {
910 * How words are broken up:
913 * -----------------------------------
914 * IFS_WORD w/WS w/NWS w
915 * IFS_WS -/WS w/NWS -
916 * IFS_NWS -/NWS w/NWS w
917 * (w means generate a word)
918 * Note that IFS_NWS/0 generates a word (AT&T ksh
919 * doesn't do this, but POSIX does).
921 if (word == IFS_WORD ||
922 (!ctype(c, C_IFSWS) && c && word == IFS_NWS)) {
927 /* also does globbing */
928 alt_expand(wp, cp, cp,
929 cp + Xlength(ds, (dp - 1)),
930 fdo | (f & DOMARKDIRS));
931 else if (fdo & DOGLOB)
932 glob(cp, wp, tobool(f & DOMARKDIRS));
933 else if ((f & DOPAT) || !(fdo & DOMAGIC))
936 XPput(*wp, debunk(cp, cp,
940 tilde_ok = (f & (DOTILDE|DOASNTILDE)) ? 1 : 0;
943 Xinit(ds, dp, 128, ATEMP);
946 } else if (type == XSUB && ctype(c, C_IFS) &&
947 !ctype(c, C_IFSWS) && Xlength(ds, dp) == 0) {
948 *(cp = alloc(1, ATEMP)) = '\0';
953 word = ctype(c, C_IFSWS) ? IFS_WS : IFS_NWS;
956 if (word == IFS_NWS &&
957 Xlength(ds, dp) == 0) {
958 *(cp = alloc(1, ATEMP)) = '\0';
964 /* age tilde_ok info - ~ code tests second bit */
966 /* mark any special second pass chars */
974 * For character classes - doesn't hurt
975 * to have magic !,-,]s outside of
978 if (f & (DOPAT | DOGLOB)) {
987 if (f & (DOPAT | DOGLOB)) {
988 fdo |= DOMAGIC | (f & DOGLOB);
995 if ((f & DOBRACE) && (c == '{' /*}*/ ||
997 fdo |= DOBRACE|DOMAGIC;
1002 /* Note first unquoted = for ~ */
1003 if (!(f & DOTEMP) && !saw_eq &&
1004 (Flag(FBRACEEXPAND) ||
1005 (f & DOASNTILDE))) {
1012 /* Note unquoted : for ~ */
1013 if (!(f & DOTEMP) && (f & DOASNTILDE))
1018 * tilde_ok is reset whenever
1019 * any of ' " $( $(( ${ } are seen.
1020 * Note that tilde_ok must be preserved
1021 * through the sequence ${A=a=}~
1023 if (type == XBASE &&
1024 (f & (DOTILDE|DOASNTILDE)) &&
1029 tcp = maybe_expand_tilde(sp,
1043 /* undo temporary */
1048 fdo |= DOMAGIC | (f & DOGLOB);
1050 } else if (ISMAGIC(c)) {
1054 /* save output char */
1062 * Prepare to generate the string returned by ${} substitution.
1065 varsub(Expand *xp, const char *sp, const char *word,
1066 int *stypep, /* becomes qualifier type */
1067 int *slenp) /* " " len (=, :=, etc.) valid iff *stypep != 0 */
1070 int state; /* next state: XBASE, XARG, XSUB, XNULLSUB */
1071 int stype; /* substitution type */
1075 bool zero_ok = false;
1077 if ((stype = sp[0]) == '\0')
1078 /* Bad variable name */
1084 * ${#var}, string length (-U: characters, +U: octets) or array size
1085 * ${%var}, string width (-U: screen columns, +U: octets)
1088 if (stype == '%' && c == '\0')
1090 if ((stype == '#' || stype == '%') && c != '\0') {
1091 /* Can't have any modifiers for ${#...} or ${%...} */
1092 if (*word != CSUBST)
1095 /* Check for size of array */
1096 if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') &&
1102 vp = global(arrayname(sp));
1103 if (vp->flag & (ISSET|ARRAY))
1105 for (; vp; vp = vp->u.array)
1106 if (vp->flag & ISSET)
1109 } else if (c == '*' || c == '@') {
1114 p = str_val(global(sp));
1115 zero_ok = p != null;
1119 /* partial utf_mbswidth reimplementation */
1127 if (!UTFMODE || (len = utf_mbtowc(&wc,
1129 /* not UTFMODE or not UTF-8 */
1130 wc = (unsigned char)(*s++);
1132 /* UTFMODE and UTF-8 */
1134 /* wc == char or wchar at s++ */
1135 if ((cw = utf_wcwidth(wc)) == -1) {
1136 /* 646, 8859-1, 10646 C0/C1 */
1144 if (Flag(FNOUNSET) && c == 0 && !zero_ok)
1145 errorf("%s: %s", sp, "parameter not set");
1146 /* unqualified variable/string substitution */
1148 xp->str = shf_smprintf("%d", c);
1152 /* Check for qualifiers in word part */
1154 c = word[slen = 0] == CHAR ? word[1] : 0;
1158 c = word[slen + 0] == CHAR ? word[slen + 1] : 0;
1160 if (!stype && c == '/') {
1163 if (word[slen] == ADELIM) {
1167 } else if (stype == 0x80 && (c == ' ' || c == '0')) {
1169 } else if (ctype(c, C_SUBOP1)) {
1172 } else if (ctype(c, C_SUBOP2)) {
1173 /* Note: ksh88 allows :%, :%%, etc */
1176 if (word[slen + 0] == CHAR && c == word[slen + 1]) {
1180 } else if (c == '@') {
1181 /* @x where x is command char */
1184 if (word[slen] == CHAR) {
1185 stype |= word[slen + 1];
1191 if (!stype && *word != CSUBST)
1197 if (c == '*' || c == '@') {
1198 switch (stype & 0x17F) {
1199 /* can't assign to a vector */
1201 /* can't trim a vector (yet) */
1210 if (e->loc->argc == 0) {
1212 xp->var = global(sp);
1213 state = c == '@' ? XNULLSUB : XSUB;
1215 xp->u.strv = (const char **)e->loc->argv + 1;
1216 xp->str = *xp->u.strv++;
1218 xp->split = tobool(c == '@');
1224 if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') &&
1228 switch (stype & 0x17F) {
1229 /* can't assign to a vector */
1231 /* can't trim a vector (yet) */
1242 if ((c = sp[0]) == '!')
1244 vp = global(arrayname(sp));
1245 for (; vp; vp = vp->u.array) {
1246 if (!(vp->flag&ISSET))
1248 XPput(wv, c == '!' ? shf_smprintf("%lu",
1252 if (XPsize(wv) == 0) {
1254 state = p[1] == '@' ? XNULLSUB : XSUB;
1258 xp->u.strv = (const char **)XPptrv(wv);
1259 xp->str = *xp->u.strv++;
1261 xp->split = tobool(p[1] == '@');
1265 /* Can't assign things like $! or $1 */
1266 if ((stype & 0x17F) == '=' &&
1267 ctype(*sp, C_VAR1 | C_DIGIT))
1269 if (*sp == '!' && sp[1]) {
1271 xp->var = global(sp);
1272 if (vstrchr(sp, '[')) {
1273 if (xp->var->flag & ISSET)
1274 xp->str = shf_smprintf("%lu",
1275 arrayindex(xp->var));
1278 } else if (xp->var->flag & ISSET)
1279 xp->str = xp->var->name;
1284 xp->var = global(sp);
1285 xp->str = str_val(xp->var);
1292 /* test the compiler's code generator */
1293 if (((stype < 0x100) && (ctype(c, C_SUBOP2) || c == '/' ||
1294 (((stype&0x80) ? *xp->str=='\0' : xp->str==null) ? /* undef? */
1295 c == '=' || c == '-' || c == '?' : c == '+'))) ||
1296 stype == (0x80 | '0') || stype == (0x100 | '#') ||
1297 stype == (0x100 | 'Q'))
1298 /* expand word instead of variable value */
1300 if (Flag(FNOUNSET) && xp->str == null && !zero_ok &&
1301 (ctype(c, C_SUBOP2) || (state != XBASE && c != '+')))
1302 errorf("%s: %s", sp, "parameter not set");
1307 * Run the command in $(...) and read its output.
1310 comsub(Expand *xp, const char *cp, int fn MKSH_A_UNUSED)
1315 uint8_t old_utfmode = UTFMODE;
1317 s = pushs(SSTRING, ATEMP);
1318 s->start = s->str = cp;
1320 t = compile(s, true);
1324 UTFMODE = old_utfmode;
1329 /* no waitlast() unless specifically enabled later */
1332 if (t->type == TCOM &&
1333 *t->args == NULL && *t->vars == NULL && t->ioact != NULL) {
1335 struct ioword *io = *t->ioact;
1338 if ((io->flag & IOTYPE) != IOREAD)
1339 errorf("%s: %s", "funny $() command",
1340 snptreef(NULL, 32, "%R", io));
1341 shf = shf_open(name = evalstr(io->name, DOTILDE), O_RDONLY, 0,
1342 SHF_MAPHI|SHF_CLEXEC);
1344 warningf(!Flag(FTALKING), "%s: %s %s: %s", name,
1345 "can't open", "$(<...) input", cstrerror(errno));
1346 } else if (fn == FUNSUB) {
1348 struct temp *tf = NULL;
1351 * create a temporary file, open for reading and writing,
1352 * with an shf open for reading (buffered) but yet unused
1354 maketemp(ATEMP, TT_FUNSUB, &tf);
1356 errorf("can't %s temporary file %s: %s",
1357 "create", tf->tffn, cstrerror(errno));
1359 /* extract shf from temporary file, unlink and free it */
1363 /* save stdout and let it point to the tempfile */
1365 ksh_dup2(shf_fileno(shf), 1, false);
1367 * run tree, with output thrown into the tempfile,
1368 * in a new function block
1371 subst_exstat = exstat & 0xFF;
1372 /* rewind the tempfile and restore regular stdout */
1373 lseek(shf_fileno(shf), (off_t)0, SEEK_SET);
1375 } else if (fn == VALSUB) {
1376 xp->str = valsub(t, ATEMP);
1377 subst_exstat = exstat & 0xFF;
1383 shf = shf_fdopen(pv[0], SHF_RD, NULL);
1386 ksh_dup2(pv[1], 1, false);
1389 execute(t, XXCOM | XPIPEO | XFORK, NULL);
1401 * perform #pattern and %pattern substitution in ${}
1404 trimsub(char *str, char *pat, int how)
1406 char *end = strnul(str);
1409 switch (how & 0xFF) {
1411 /* shortest match at beginning */
1412 for (p = str; p <= end; p += utf_ptradj(p)) {
1414 if (gmatchx(str, pat, false)) {
1422 /* longest match at beginning */
1423 for (p = end; p >= str; p--) {
1425 if (gmatchx(str, pat, false)) {
1433 /* shortest match at end */
1436 if (gmatchx(p, pat, false))
1440 while ((p-- > str) && ((*p & 0xC0) == 0x80))
1442 if ((p < str) || (p + utf_ptradj(p) != op))
1449 /* longest match at end */
1450 for (p = str; p <= end; p++)
1451 if (gmatchx(p, pat, false)) {
1453 strndupx(end, str, p - str, ATEMP);
1459 /* no match, return string */
1465 * Name derived from V6's /etc/glob, the program that expanded filenames.
1468 /* XXX cp not const 'cause slashes are temporarily replaced with NULs... */
1470 glob(char *cp, XPtrV *wp, bool markdirs)
1472 int oldsize = XPsize(*wp);
1474 if (glob_str(cp, wp, markdirs) == 0)
1475 XPput(*wp, debunk(cp, cp, strlen(cp) + 1));
1477 qsort(XPptrv(*wp) + oldsize, XPsize(*wp) - oldsize,
1478 sizeof(void *), xstrcmp);
1482 #define GF_EXCHECK BIT(0) /* do existence check on file */
1483 #define GF_GLOBBED BIT(1) /* some globbing has been done */
1484 #define GF_MARKDIR BIT(2) /* add trailing / to directories */
1487 * Apply file globbing to cp and store the matching files in wp. Returns
1488 * the number of matches found.
1491 glob_str(char *cp, XPtrV *wp, bool markdirs)
1493 int oldsize = XPsize(*wp);
1497 Xinit(xs, xp, 256, ATEMP);
1498 globit(&xs, &xp, cp, wp, markdirs ? GF_MARKDIR : GF_NONE);
1501 return (XPsize(*wp) - oldsize);
1505 globit(XString *xs, /* dest string */
1506 char **xpp, /* ptr to dest end */
1507 char *sp, /* source path */
1508 XPtrV *wp, /* output list */
1509 int check) /* GF_* flags */
1511 char *np; /* next source component */
1516 /* This to allow long expansions to be interrupted */
1520 /* end of source path */
1522 * We only need to check if the file exists if a pattern
1523 * is followed by a non-pattern (eg, foo*x/bar; no check
1524 * is needed for foo* since the match must exist) or if
1525 * any patterns were expanded and the markdirs option is set.
1526 * Symlinks make things a bit tricky...
1528 if ((check & GF_EXCHECK) ||
1529 ((check & GF_MARKDIR) && (check & GF_GLOBBED))) {
1530 #define stat_check() (stat_done ? stat_done : (stat_done = \
1531 stat(Xstring(*xs, xp), &statb) < 0 ? -1 : 1))
1532 struct stat lstatb, statb;
1533 /* -1: failed, 1 ok, 0 not yet done */
1536 if (mksh_lstat(Xstring(*xs, xp), &lstatb) < 0)
1539 * special case for systems which strip trailing
1540 * slashes from regular files (eg, /etc/passwd/).
1541 * SunOS 4.1.3 does this...
1543 if ((check & GF_EXCHECK) && xp > Xstring(*xs, xp) &&
1544 xp[-1] == '/' && !S_ISDIR(lstatb.st_mode) &&
1545 (!S_ISLNK(lstatb.st_mode) ||
1546 stat_check() < 0 || !S_ISDIR(statb.st_mode)))
1549 * Possibly tack on a trailing / if there isn't already
1550 * one and if the file is a directory or a symlink to a
1553 if (((check & GF_MARKDIR) && (check & GF_GLOBBED)) &&
1554 xp > Xstring(*xs, xp) && xp[-1] != '/' &&
1555 (S_ISDIR(lstatb.st_mode) ||
1556 (S_ISLNK(lstatb.st_mode) && stat_check() > 0 &&
1557 S_ISDIR(statb.st_mode)))) {
1562 strndupx(np, Xstring(*xs, xp), Xlength(*xs, xp), ATEMP);
1567 if (xp > Xstring(*xs, xp))
1569 while (*sp == '/') {
1573 np = strchr(sp, '/');
1576 /* don't assume '/', can be multiple kinds */
1580 odirsep = '\0'; /* keep gcc quiet */
1581 se = sp + strlen(sp);
1586 * Check if sp needs globbing - done to avoid pattern checks for strings
1587 * containing MAGIC characters, open [s without the matching close ],
1588 * etc. (otherwise opendir() will be called which may fail because the
1589 * directory isn't readable - if no globbing is needed, only execute
1590 * permission should be required (as per POSIX)).
1592 if (!has_globbing(sp, se)) {
1593 XcheckN(*xs, xp, se - sp + 1);
1594 debunk(xp, sp, Xnleft(*xs, xp));
1597 globit(xs, xpp, np, wp, check);
1602 size_t len, prefix_len;
1604 /* xp = *xpp; copy_non_glob() may have re-alloc'd xs */
1606 prefix_len = Xlength(*xs, xp);
1607 dirp = opendir(prefix_len ? Xstring(*xs, xp) : ".");
1610 while ((d = readdir(dirp)) != NULL) {
1612 if (name[0] == '.' &&
1613 (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1614 /* always ignore . and .. */
1616 if ((*name == '.' && *sp != '.') ||
1617 !gmatchx(name, sp, true))
1620 len = strlen(d->d_name) + 1;
1621 XcheckN(*xs, xp, len);
1622 memcpy(xp, name, len);
1623 *xpp = xp + len - 1;
1624 globit(xs, xpp, np, wp,
1625 (check & GF_MARKDIR) | GF_GLOBBED
1626 | (np ? GF_EXCHECK : GF_NONE));
1627 xp = Xstring(*xs, xp) + prefix_len;
1638 /* remove MAGIC from string */
1640 debunk(char *dp, const char *sp, size_t dlen)
1645 if ((s = cstrchr(sp, MAGIC))) {
1646 if (s - sp >= (ssize_t)dlen)
1648 memmove(dp, sp, s - sp);
1649 for (d = dp + (s - sp); *s && (d - dp < (ssize_t)dlen); s++)
1650 if (!ISMAGIC(*s) || !(*++s & 0x80) ||
1651 !vstrchr("*+?@! ", *s & 0x7f))
1654 /* extended pattern operators: *+?@! */
1655 if ((*s & 0x7f) != ' ')
1657 if (d - dp < (ssize_t)dlen)
1661 } else if (dp != sp)
1662 strlcpy(dp, sp, dlen);
1667 * Check if p is an unquoted name, possibly followed by a / or :. If so
1668 * puts the expanded version in *dcp,dp and returns a pointer in p just
1669 * past the name, otherwise returns 0.
1672 maybe_expand_tilde(const char *p, XString *dsp, char **dpp, int isassign)
1679 Xinit(ts, tp, 16, ATEMP);
1680 /* : only for DOASNTILDE form */
1681 while (p[0] == CHAR && p[1] != '/' && (!isassign || p[1] != ':'))
1688 r = (p[0] == EOS || p[0] == CHAR || p[0] == CSUBST) ?
1689 tilde(Xstring(ts, tp)) : NULL;
1707 * based on a version by Arnold Robbins
1716 dp = str_val(global("HOME"));
1717 else if (cp[0] == '+' && cp[1] == '\0')
1718 dp = str_val(global("PWD"));
1719 else if (cp[0] == '-' && cp[1] == '\0')
1720 dp = str_val(global("OLDPWD"));
1721 #ifndef MKSH_NOPWNAM
1725 /* If HOME, PWD or OLDPWD are not set, don't expand ~ */
1726 return (dp == null ? NULL : dp);
1729 #ifndef MKSH_NOPWNAM
1731 * map userid to user's home directory.
1732 * note that 4.3's getpw adds more than 6K to the shell,
1733 * and the YP version probably adds much more.
1734 * we might consider our own version of getpwnam() to keep the size down.
1741 ap = ktenter(&homedirs, name, hash(name));
1742 if (!(ap->flag & ISSET)) {
1745 pw = getpwnam(name);
1748 strdupx(ap->val.s, pw->pw_dir, APERM);
1749 ap->flag |= DEFINED|ISSET|ALLOC;
1756 alt_expand(XPtrV *wp, char *start, char *exp_start, char *end, int fdo)
1759 char *brace_start, *brace_end, *comma = NULL;
1763 /* search for open brace */
1764 for (p = exp_start; (p = strchr(p, MAGIC)) && p[1] != '{' /*}*/; p += 2)
1768 /* find matching close brace, if any */
1772 for (p += 2; *p && count; p++) {
1774 if (*++p == '{' /*}*/)
1776 else if (*p == /*{*/ '}')
1778 else if (*p == ',' && count == 1)
1783 /* no valid expansions... */
1784 if (!p || count != 0) {
1786 * Note that given a{{b,c} we do not expand anything (this is
1787 * what AT&T ksh does. This may be changed to do the {b,c}
1791 glob(start, wp, tobool(fdo & DOMARKDIRS));
1793 XPput(*wp, debunk(start, start, end - start));
1798 alt_expand(wp, start, brace_end, end, fdo);
1802 /* expand expression */
1803 field_start = brace_start + 2;
1805 for (p = brace_start + 2; p != brace_end; p++) {
1807 if (*++p == '{' /*}*/)
1809 else if ((*p == /*{*/ '}' && --count == 0) ||
1810 (*p == ',' && count == 1)) {
1815 * addition safe since these operate on
1816 * one string (separate substrings)
1818 l1 = brace_start - start;
1819 l2 = (p - 1) - field_start;
1820 l3 = end - brace_end;
1821 news = alloc(l1 + l2 + l3 + 1, ATEMP);
1822 memcpy(news, start, l1);
1823 memcpy(news + l1, field_start, l2);
1824 memcpy(news + l1 + l2, brace_end, l3);
1825 news[l1 + l2 + l3] = '\0';
1826 alt_expand(wp, news, news + l1,
1827 news + l1 + l2 + l3, fdo);
1828 field_start = p + 1;
1835 /* helper function due to setjmp/longjmp woes */
1837 valsub(struct op *t, Area *ap)
1839 char * volatile cp = NULL;
1840 struct tbl * volatile vp = NULL;
1845 vp = local("REPLY", false);
1846 if (!kshsetjmp(e->jbuf))
1847 execute(t, XXCOM | XERROK, NULL);
1849 strdupx(cp, str_val(vp), ap);