1 /* $OpenBSD: eval.c,v 1.37 2011/10/11 14:32:43 otto 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.136 2013/02/10 23:43:59 tg Exp $");
31 * first pass: quoting, IFS separation, ~, ${}, $() and $(()) substitution.
32 * second pass: alternation ({,}), filename expansion (*?[]).
35 /* expansion generator state */
36 typedef struct Expand {
37 /* int type; */ /* see expand() */
38 const char *str; /* string */
40 const char **strv; /* string[] */
41 struct shf *shf; /* file */
43 struct tbl *var; /* variable in ${var..} */
44 bool split; /* split "$@" / call waitlast $() */
47 #define XBASE 0 /* scanning original */
48 #define XSUB 1 /* expanding ${} string */
49 #define XARGSEP 2 /* ifs0 between "$*" */
50 #define XARG 3 /* expanding $*, $@ */
51 #define XCOM 4 /* expanding $() */
52 #define XNULLSUB 5 /* "$@" when $# is 0 (don't generate word) */
53 #define XSUBMID 6 /* middle of expanding ${} */
55 /* States used for field splitting */
56 #define IFS_WORD 0 /* word has chars (or quotes) */
57 #define IFS_WS 1 /* have seen IFS white-space */
58 #define IFS_NWS 2 /* have seen IFS non-white-space */
60 static int varsub(Expand *, const char *, const char *, int *, int *);
61 static int comsub(Expand *, const char *, int);
62 static void funsub(struct op *);
63 static char *trimsub(char *, char *, int);
64 static void glob(char *, XPtrV *, bool);
65 static void globit(XString *, char **, char *, XPtrV *, int);
66 static const char *maybe_expand_tilde(const char *, XString *, char **, int);
68 static char *homedir(char *);
70 static void alt_expand(XPtrV *, char *, char *, char *, int);
71 static int utflen(const char *);
72 static void utfincptr(const char *, mksh_ari_t *);
74 /* UTFMODE functions */
95 utfincptr(const char *s, mksh_ari_t *lp)
100 cp += utf_ptradj(cp);
104 /* compile and expand word */
106 substitute(const char *cp, int f)
108 struct source *s, *sold;
111 s = pushs(SWSTR, ATEMP);
112 s->start = s->str = cp;
114 if (yylex(ONEWORD) != LWORD)
115 internal_errorf("bad substitution");
118 return (evalstr(yylval.cp, f));
125 eval(const char **ap, int f)
130 union mksh_ccphack vap;
136 /* space for shell name */
139 expand(*ap++, &w, f);
141 return ((char **)XPclose(w) + 1);
148 evalstr(const char *cp, int f)
162 * expand string - return only one component
163 * used from iosetup to expand redirection files
166 evalonestr(const char *cp, int f)
178 rv = (char *) *XPptrv(w);
181 rv = evalstr(cp, f&~DOGLOB);
188 /* for nested substitution: ${var:=$var2} */
189 typedef struct SubType {
190 struct tbl *var; /* variable for ${var..} */
191 struct SubType *prev; /* old type */
192 struct SubType *next; /* poped type (to avoid re-allocating) */
193 size_t base; /* begin position of expanded word */
194 short stype; /* [=+-?%#] action after expanded word */
195 short f; /* saved value of f (DOPAT, etc) */
196 uint8_t quotep; /* saved value of quote (for ${..[%#]..}) */
197 uint8_t quotew; /* saved value of quote (for ${..[+-=]..}) */
201 expand(const char *cp, /* input word */
202 XPtrV *wp, /* output words */
203 int f) /* DO* flags */
206 int type; /* expansion type */
207 int quote = 0; /* quoted */
208 XString ds; /* destination string */
209 char *dp; /* destination */
210 const char *sp; /* source */
211 int fdo, word; /* second pass flags; have word */
212 int doblank; /* field splitting of parameter/command subst */
214 /* expansion variables */
215 NULL, { NULL }, NULL, 0
217 SubType st_head, *st;
218 /* For trailing newlines in COMSUB */
220 bool saw_eq, make_magic;
225 internal_errorf("expand(NULL)");
226 /* for alias, readonly, set, typeset commands */
227 if ((f & DOVACHECK) && is_wdvarassign(cp)) {
228 f &= ~(DOVACHECK|DOBLANK|DOGLOB|DOTILDE);
235 if (Flag(FBRACEEXPAND) && (f & DOGLOB))
238 /* init destination string */
239 Xinit(ds, dp, 128, ATEMP);
245 tilde_ok = (f & (DOTILDE|DOASNTILDE)) ? 1 : 0;
248 word = (f&DOBLANK) ? IFS_WS : IFS_WORD;
249 /* clang doesn't know OSUBST comes before CSUBST */
250 memset(&st_head, 0, sizeof(st_head));
253 while (/* CONSTCOND */ 1) {
258 /* original prefixed string */
268 /* temporary quote */
283 if (f & DONTRUNCOMMAND) {
291 while (*sp != '\0') {
301 type = comsub(&x, sp, c);
302 if (type == XCOM && (f&DOBLANK))
311 if (f & DONTRUNCOMMAND) {
312 *dp++ = '$'; *dp++ = '('; *dp++ = '(';
313 while (*sp != '\0') {
317 *dp++ = ')'; *dp++ = ')';
322 v.flag = DEFINED|ISSET|INTEGER;
326 v_evaluate(&v, substitute(sp, 0),
327 KSH_UNWIND_ERROR, true);
329 for (p = str_val(&v); *p; ) {
336 /* ${{#}var{:}[=+-?#%]word} */
339 * OSUBST [{x] plain-variable-part \0
340 * compiled-word-part CSUBST [}x]
341 * This is where all syntax checking gets done...
343 /* skip the { or x (}) */
344 const char *varname = ++sp;
349 sp = cstrchr(sp, '\0') + 1;
350 type = varsub(&x, varname, sp, &stype, &slen);
352 char *beg, *end, *str;
356 end = (beg = wdcopy(sp, ATEMP)) +
357 (wdscan(sp, CSUBST) - sp);
358 /* ({) the } or x is already skipped */
359 if (end < wdscan(beg, EOS))
361 str = snptreef(NULL, 64, "%S", beg);
363 errorf("%s: %s", str, "bad substitution");
373 newst = alloc(sizeof(SubType), ATEMP);
380 st->base = Xsavepos(ds, dp);
382 if (x.var == &vtemp) {
384 st->var->flag &= ~INTEGER;
385 /* can't fail here */
388 KSH_RETURN_ERROR | 0x4);
392 st->quotew = st->quotep = quote;
393 /* skip qualifier(s) */
396 switch (stype & 0x17F) {
403 beg = wdcopy(sp, ATEMP);
404 end = beg + (wdscan(sp, CSUBST) - sp);
406 end = wdstrip(beg, 0);
408 evaluate(substitute(end, 0),
409 &seed, KSH_UNWIND_ERROR, true);
410 /* hash with seed, for now */
415 x.str = shf_smprintf("%08X",
423 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
424 print_value_quoted(&shf, str_val(st->var));
425 x.str = shf_sclose(&shf);
429 char *beg, *mid, *end, *stg;
430 mksh_ari_t from = 0, num = -1, flen, finc = 0;
432 beg = wdcopy(sp, ATEMP);
433 mid = beg + (wdscan(sp, ADELIM) - sp);
434 stg = beg + (wdscan(sp, CSUBST) - sp);
436 goto unwind_substsyn;
438 if (mid[-1] == /*{*/'}') {
443 (wdscan(mid, ADELIM) - mid);
445 /* more than max delimiters */
446 end[-1] != /*{*/ '}')
447 goto unwind_substsyn;
451 evaluate(substitute(stg = wdstrip(beg, 0), 0),
452 &from, KSH_UNWIND_ERROR, true);
455 evaluate(substitute(stg = wdstrip(mid, 0), 0),
456 &num, KSH_UNWIND_ERROR, true);
460 beg = str_val(st->var);
466 finc = from < flen ? from : flen;
468 utfincptr(beg, &finc);
471 if (num < 0 || num > flen)
474 utfincptr(beg, &num);
475 strndupx(x.str, beg, num, ATEMP);
479 char *s, *p, *d, *sbeg, *end;
481 char *tpat0, *tpat1, *tpat2;
483 s = wdcopy(sp, ATEMP);
484 p = s + (wdscan(sp, ADELIM) - sp);
485 d = s + (wdscan(sp, CSUBST) - sp);
487 goto unwind_substsyn;
489 if (p[-1] == /*{*/'}')
493 sp += (d ? d : p) - s - 1;
495 WDS_KEEPQ | WDS_MAGIC);
496 pat = substitute(tpat0, 0);
498 d = wdstrip(p, WDS_KEEPQ);
499 rrep = substitute(d, 0);
510 /* XXX really? */ s[1] == '\\' ||
518 /* check for special cases */
519 d = str_val(st->var);
523 /* anchor at begin */
540 tpat1 = tpat2 = NULL;
542 if (gmatchx(null, tpat0, false)) {
549 /* but is anchored */
550 s = shf_smprintf("%s%s",
555 /* prepare string on which to work */
556 strdupx(s, d, ATEMP);
559 /* first see if we have any match at all */
562 /* anchor at the beginning */
563 tpat1 = shf_smprintf("%s%c*", ++tpat0, MAGIC);
565 } else if (*pat == '%') {
566 /* anchor at the end */
567 tpat1 = shf_smprintf("%c*%s", MAGIC, ++tpat0);
571 tpat1 = shf_smprintf("%c*%s%c*", MAGIC, pat, MAGIC);
576 * this would not be necessary if gmatchx would return
577 * the start and end values of a match found, like re*
579 if (!gmatchx(sbeg, tpat1, false))
582 /* now anchor the beginning of the match */
584 while (sbeg <= end) {
585 if (gmatchx(sbeg, tpat2, false))
590 /* now anchor the end of the match */
598 gotmatch = tobool(gmatchx(sbeg, tpat0, false));
604 strndupx(end, s, sbeg - s, ATEMP);
605 d = shf_smprintf("%s%s%s", end, rrep, p);
607 sbeg = d + (sbeg - s) + strlen(rrep);
624 /* ! DOBLANK,DOBRACE,DOTILDE */
625 f = (f & DONTRUNCOMMAND) |
627 st->quotew = quote = 0;
629 * Prepend open pattern (so |
630 * in a trim will work as
640 * Enabling tilde expansion
642 * non-standard ksh, but is
643 * consistent with rules for
644 * other assignments. Not
645 * sure what POSIX thinks of
647 * Not doing tilde expansion
648 * for integer variables is a
649 * non-POSIX thing - makes
650 * sense though, since ~ is
651 * a arithmetic operator.
653 if (!(x.var->flag & INTEGER))
654 f |= DOASNTILDE|DOTILDE;
657 * These will be done after the
658 * value has been assigned.
660 f &= ~(DOBLANK|DOGLOB|DOBRACE);
668 /* Enable tilde expansion */
674 sp += wdscan(sp, CSUBST) - sp;
678 /* only get here if expanding word */
680 /* ({) skip the } or x */
682 /* in case of ${unset:-} */
689 switch (st->stype & 0x17F) {
693 /* Append end-pattern */
698 dp = Xrestpos(ds, dp, st->base);
700 * Must use st->var since calling
701 * global would break things
704 x.str = trimsub(str_val(st->var),
706 if (x.str[0] != '\0' || st->quotep)
716 * Restore our position and substitute
717 * the value of st->var (may not be
718 * the assigned value in the presence
719 * of integer/right-adj/etc attributes).
721 dp = Xrestpos(ds, dp, st->base);
723 * Must use st->var since calling
724 * global would cause with things
725 * like x[i+=1] to be evaluated twice.
728 * Note: not exported by FEXPORT
732 * XXX POSIX says readonly is only
733 * fatal for special builtins (setstr
734 * does readonly check).
736 len = strlen(dp) + 1;
738 debunk(alloc(len, ATEMP),
739 dp, len), KSH_UNWIND_ERROR);
740 x.str = str_val(st->var);
747 char *s = Xrestpos(ds, dp, st->base);
749 errorf("%s: %s", st->var->name,
751 "parameter null or not set" :
752 (debunk(s, s, strlen(s) + 1), s));
758 dp = Xrestpos(ds, dp, st->base);
770 /* open pattern: *(foo|bar) */
771 /* Next char is the type of pattern */
777 /* pattern separator (|) */
792 * Special case for "$@" (and "${foo[@]}") - no
793 * word is generated if $# is 0 (unless there is
794 * other stuff inside the quotes).
800 * not really correct: x=; "$x$@" should
801 * generate a null argument and
802 * set A; "${@:+}" shouldn't.
804 if (dp == Xstring(ds, dp))
811 if ((c = *x.str++) == 0) {
824 if ((c = *x.str++) == '\0') {
826 * force null words to be created so
827 * set -- '' 2 ''; foo "$@" will do
830 if (quote && x.split)
832 if ((x.str = *x.u.strv++) == NULL) {
840 if (quote && !x.split)
844 if (quote && x.split) {
845 /* terminate word for "$@" */
854 /* Spit out saved NLs */
858 while ((c = shf_getc(x.u.shf)) == 0 || c == '\n')
862 if (newlines && c != EOF) {
863 shf_ungetc(c, x.u.shf);
872 subst_exstat = waitlast();
881 /* check for end of word or IFS separation */
882 if (c == 0 || (!quote && (f & DOBLANK) && doblank &&
883 !make_magic && ctype(c, C_IFS))) {
885 * How words are broken up:
888 * -----------------------------------
889 * IFS_WORD w/WS w/NWS w
890 * IFS_WS -/WS w/NWS -
891 * IFS_NWS -/NWS w/NWS w
892 * (w means generate a word)
893 * Note that IFS_NWS/0 generates a word (AT&T ksh
894 * doesn't do this, but POSIX does).
896 if (word == IFS_WORD ||
897 (!ctype(c, C_IFSWS) && c && word == IFS_NWS)) {
903 /* also does globbing */
905 p + Xlength(ds, (dp - 1)),
906 fdo | (f & DOMARKDIRS));
907 else if (fdo & DOGLOB)
908 glob(p, wp, tobool(f & DOMARKDIRS));
909 else if ((f & DOPAT) || !(fdo & DOMAGIC))
912 XPput(*wp, debunk(p, p, strlen(p) + 1));
915 tilde_ok = (f & (DOTILDE|DOASNTILDE)) ? 1 : 0;
918 Xinit(ds, dp, 128, ATEMP);
921 } else if (type == XSUB && ctype(c, C_IFS) &&
922 !ctype(c, C_IFSWS) && Xlength(ds, dp) == 0) {
925 *(p = alloc(1, ATEMP)) = '\0';
930 word = ctype(c, C_IFSWS) ? IFS_WS : IFS_NWS;
933 if (word == IFS_NWS &&
934 Xlength(ds, dp) == 0) {
937 *(p = alloc(1, ATEMP)) = '\0';
943 /* age tilde_ok info - ~ code tests second bit */
945 /* mark any special second pass chars */
953 * For character classes - doesn't hurt
954 * to have magic !,-,]s outside of
957 if (f & (DOPAT | DOGLOB)) {
966 if (f & (DOPAT | DOGLOB)) {
967 fdo |= DOMAGIC | (f & DOGLOB);
974 if ((f & DOBRACE) && (c == '{' /*}*/ ||
976 fdo |= DOBRACE|DOMAGIC;
981 /* Note first unquoted = for ~ */
982 if (!(f & DOTEMP) && !saw_eq &&
983 (Flag(FBRACEEXPAND) ||
991 /* Note unquoted : for ~ */
992 if (!(f & DOTEMP) && (f & DOASNTILDE))
997 * tilde_ok is reset whenever
998 * any of ' " $( $(( ${ } are seen.
999 * Note that tilde_ok must be preserved
1000 * through the sequence ${A=a=}~
1002 if (type == XBASE &&
1003 (f & (DOTILDE|DOASNTILDE)) &&
1009 p = maybe_expand_tilde(sp,
1023 /* undo temporary */
1028 fdo |= DOMAGIC | (f & DOGLOB);
1030 } else if (ISMAGIC(c)) {
1034 /* save output char */
1042 * Prepare to generate the string returned by ${} substitution.
1045 varsub(Expand *xp, const char *sp, const char *word,
1046 int *stypep, /* becomes qualifier type */
1047 int *slenp) /* " " len (=, :=, etc.) valid iff *stypep != 0 */
1050 int state; /* next state: XBASE, XARG, XSUB, XNULLSUB */
1051 int stype; /* substitution type */
1055 bool zero_ok = false;
1057 if ((stype = sp[0]) == '\0')
1058 /* Bad variable name */
1064 * ${#var}, string length (-U: characters, +U: octets) or array size
1065 * ${%var}, string width (-U: screen columns, +U: octets)
1068 if (stype == '%' && c == '\0')
1070 if ((stype == '#' || stype == '%') && c != '\0') {
1071 /* Can't have any modifiers for ${#...} or ${%...} */
1072 if (*word != CSUBST)
1075 /* Check for size of array */
1076 if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') &&
1082 vp = global(arrayname(sp));
1083 if (vp->flag & (ISSET|ARRAY))
1085 for (; vp; vp = vp->u.array)
1086 if (vp->flag & ISSET)
1089 } else if (c == '*' || c == '@') {
1094 p = str_val(global(sp));
1095 zero_ok = p != null;
1099 /* partial utf_mbswidth reimplementation */
1107 if (!UTFMODE || (len = utf_mbtowc(&wc,
1109 /* not UTFMODE or not UTF-8 */
1110 wc = (unsigned char)(*s++);
1112 /* UTFMODE and UTF-8 */
1114 /* wc == char or wchar at s++ */
1115 if ((cw = utf_wcwidth(wc)) == -1) {
1116 /* 646, 8859-1, 10646 C0/C1 */
1124 if (Flag(FNOUNSET) && c == 0 && !zero_ok)
1125 errorf("%s: %s", sp, "parameter not set");
1126 /* unqualified variable/string substitution */
1128 xp->str = shf_smprintf("%d", c);
1132 /* Check for qualifiers in word part */
1134 c = word[slen = 0] == CHAR ? word[1] : 0;
1138 c = word[slen + 0] == CHAR ? word[slen + 1] : 0;
1140 if (!stype && c == '/') {
1143 if (word[slen] == ADELIM) {
1147 } else if (stype == 0x80 && (c == ' ' || c == '0')) {
1149 } else if (ctype(c, C_SUBOP1)) {
1152 } else if (ctype(c, C_SUBOP2)) {
1153 /* Note: ksh88 allows :%, :%%, etc */
1156 if (word[slen + 0] == CHAR && c == word[slen + 1]) {
1160 } else if (c == '@') {
1161 /* @x where x is command char */
1164 if (word[slen] == CHAR) {
1165 stype |= word[slen + 1];
1171 if (!stype && *word != CSUBST)
1177 if (c == '*' || c == '@') {
1178 switch (stype & 0x17F) {
1179 case '=': /* can't assign to a vector */
1180 case '%': /* can't trim a vector (yet) */
1188 if (e->loc->argc == 0) {
1190 xp->var = global(sp);
1191 state = c == '@' ? XNULLSUB : XSUB;
1193 xp->u.strv = (const char **)e->loc->argv + 1;
1194 xp->str = *xp->u.strv++;
1196 xp->split = tobool(c == '@');
1202 if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') &&
1206 switch (stype & 0x17F) {
1207 case '=': /* can't assign to a vector */
1208 case '%': /* can't trim a vector (yet) */
1218 if ((c = sp[0]) == '!')
1220 vp = global(arrayname(sp));
1221 for (; vp; vp = vp->u.array) {
1222 if (!(vp->flag&ISSET))
1224 XPput(wv, c == '!' ? shf_smprintf("%lu",
1228 if (XPsize(wv) == 0) {
1230 state = p[1] == '@' ? XNULLSUB : XSUB;
1234 xp->u.strv = (const char **)XPptrv(wv);
1235 xp->str = *xp->u.strv++;
1237 xp->split = tobool(p[1] == '@');
1241 /* Can't assign things like $! or $1 */
1242 if ((stype & 0x17F) == '=' &&
1243 ctype(*sp, C_VAR1 | C_DIGIT))
1245 if (*sp == '!' && sp[1]) {
1247 xp->var = global(sp);
1248 if (vstrchr(sp, '[')) {
1249 if (xp->var->flag & ISSET)
1250 xp->str = shf_smprintf("%lu",
1251 arrayindex(xp->var));
1254 } else if (xp->var->flag & ISSET)
1255 xp->str = xp->var->name;
1260 xp->var = global(sp);
1261 xp->str = str_val(xp->var);
1268 /* test the compiler's code generator */
1269 if (((stype < 0x100) && (ctype(c, C_SUBOP2) || c == '/' ||
1270 (((stype&0x80) ? *xp->str=='\0' : xp->str==null) ? /* undef? */
1271 c == '=' || c == '-' || c == '?' : c == '+'))) ||
1272 stype == (0x80 | '0') || stype == (0x100 | '#') ||
1273 stype == (0x100 | 'Q'))
1274 /* expand word instead of variable value */
1276 if (Flag(FNOUNSET) && xp->str == null && !zero_ok &&
1277 (ctype(c, C_SUBOP2) || (state != XBASE && c != '+')))
1278 errorf("%s: %s", sp, "parameter not set");
1283 * Run the command in $(...) and read its output.
1286 comsub(Expand *xp, const char *cp, int fn MKSH_A_UNUSED)
1291 uint8_t old_utfmode = UTFMODE;
1293 s = pushs(SSTRING, ATEMP);
1294 s->start = s->str = cp;
1296 t = compile(s, true);
1300 UTFMODE = old_utfmode;
1305 /* no waitlast() unless specifically enabled later */
1308 if (t->type == TCOM &&
1309 *t->args == NULL && *t->vars == NULL && t->ioact != NULL) {
1311 struct ioword *io = *t->ioact;
1314 if ((io->flag & IOTYPE) != IOREAD)
1315 errorf("%s: %s", "funny $() command",
1316 snptreef(NULL, 32, "%R", io));
1317 shf = shf_open(name = evalstr(io->name, DOTILDE), O_RDONLY, 0,
1318 SHF_MAPHI|SHF_CLEXEC);
1320 errorf("%s: %s %s", name, "can't open", "$() input");
1321 } else if (fn == FUNSUB) {
1323 struct temp *tf = NULL;
1325 /* create a temporary file, open for writing */
1326 maketemp(ATEMP, TT_FUNSUB, &tf);
1328 errorf("can't %s temporary file %s: %s",
1329 "create", tf->tffn, cstrerror(errno));
1331 /* save stdout and make the temporary file it */
1333 ksh_dup2(shf_fileno(tf->shf), 1, false);
1335 * run tree, with output thrown into the tempfile,
1336 * in a new function block
1339 subst_exstat = exstat & 0xFF;
1340 /* close the tempfile and restore regular stdout */
1343 /* now open, unlink and free the tempfile for reading */
1344 shf = shf_open(tf->tffn, O_RDONLY, 0, SHF_MAPHI | SHF_CLEXEC);
1351 shf = shf_fdopen(pv[0], SHF_RD, NULL);
1354 ksh_dup2(pv[1], 1, false);
1357 execute(t, XXCOM | XPIPEO | XFORK, NULL);
1369 * perform #pattern and %pattern substitution in ${}
1372 trimsub(char *str, char *pat, int how)
1374 char *end = strnul(str);
1377 switch (how & 0xFF) {
1379 /* shortest match at beginning */
1380 for (p = str; p <= end; p += utf_ptradj(p)) {
1382 if (gmatchx(str, pat, false)) {
1390 /* longest match at beginning */
1391 for (p = end; p >= str; p--) {
1393 if (gmatchx(str, pat, false)) {
1401 /* shortest match at end */
1404 if (gmatchx(p, pat, false))
1408 while ((p-- > str) && ((*p & 0xC0) == 0x80))
1410 if ((p < str) || (p + utf_ptradj(p) != op))
1417 /* longest match at end */
1418 for (p = str; p <= end; p++)
1419 if (gmatchx(p, pat, false)) {
1421 strndupx(end, str, p - str, ATEMP);
1427 /* no match, return string */
1433 * Name derived from V6's /etc/glob, the program that expanded filenames.
1436 /* XXX cp not const 'cause slashes are temporarily replaced with NULs... */
1438 glob(char *cp, XPtrV *wp, bool markdirs)
1440 int oldsize = XPsize(*wp);
1442 if (glob_str(cp, wp, markdirs) == 0)
1443 XPput(*wp, debunk(cp, cp, strlen(cp) + 1));
1445 qsort(XPptrv(*wp) + oldsize, XPsize(*wp) - oldsize,
1446 sizeof(void *), xstrcmp);
1450 #define GF_EXCHECK BIT(0) /* do existence check on file */
1451 #define GF_GLOBBED BIT(1) /* some globbing has been done */
1452 #define GF_MARKDIR BIT(2) /* add trailing / to directories */
1455 * Apply file globbing to cp and store the matching files in wp. Returns
1456 * the number of matches found.
1459 glob_str(char *cp, XPtrV *wp, bool markdirs)
1461 int oldsize = XPsize(*wp);
1465 Xinit(xs, xp, 256, ATEMP);
1466 globit(&xs, &xp, cp, wp, markdirs ? GF_MARKDIR : GF_NONE);
1469 return (XPsize(*wp) - oldsize);
1473 globit(XString *xs, /* dest string */
1474 char **xpp, /* ptr to dest end */
1475 char *sp, /* source path */
1476 XPtrV *wp, /* output list */
1477 int check) /* GF_* flags */
1479 char *np; /* next source component */
1484 /* This to allow long expansions to be interrupted */
1488 /* end of source path */
1490 * We only need to check if the file exists if a pattern
1491 * is followed by a non-pattern (eg, foo*x/bar; no check
1492 * is needed for foo* since the match must exist) or if
1493 * any patterns were expanded and the markdirs option is set.
1494 * Symlinks make things a bit tricky...
1496 if ((check & GF_EXCHECK) ||
1497 ((check & GF_MARKDIR) && (check & GF_GLOBBED))) {
1498 #define stat_check() (stat_done ? stat_done : \
1499 (stat_done = stat(Xstring(*xs, xp), &statb) < 0 \
1501 struct stat lstatb, statb;
1502 int stat_done = 0; /* -1: failed, 1 ok */
1504 if (mksh_lstat(Xstring(*xs, xp), &lstatb) < 0)
1507 * special case for systems which strip trailing
1508 * slashes from regular files (eg, /etc/passwd/).
1509 * SunOS 4.1.3 does this...
1511 if ((check & GF_EXCHECK) && xp > Xstring(*xs, xp) &&
1512 xp[-1] == '/' && !S_ISDIR(lstatb.st_mode) &&
1513 (!S_ISLNK(lstatb.st_mode) ||
1514 stat_check() < 0 || !S_ISDIR(statb.st_mode)))
1517 * Possibly tack on a trailing / if there isn't already
1518 * one and if the file is a directory or a symlink to a
1521 if (((check & GF_MARKDIR) && (check & GF_GLOBBED)) &&
1522 xp > Xstring(*xs, xp) && xp[-1] != '/' &&
1523 (S_ISDIR(lstatb.st_mode) ||
1524 (S_ISLNK(lstatb.st_mode) && stat_check() > 0 &&
1525 S_ISDIR(statb.st_mode)))) {
1530 strndupx(np, Xstring(*xs, xp), Xlength(*xs, xp), ATEMP);
1535 if (xp > Xstring(*xs, xp))
1537 while (*sp == '/') {
1541 np = strchr(sp, '/');
1544 /* don't assume '/', can be multiple kinds */
1548 odirsep = '\0'; /* keep gcc quiet */
1549 se = sp + strlen(sp);
1554 * Check if sp needs globbing - done to avoid pattern checks for strings
1555 * containing MAGIC characters, open [s without the matching close ],
1556 * etc. (otherwise opendir() will be called which may fail because the
1557 * directory isn't readable - if no globbing is needed, only execute
1558 * permission should be required (as per POSIX)).
1560 if (!has_globbing(sp, se)) {
1561 XcheckN(*xs, xp, se - sp + 1);
1562 debunk(xp, sp, Xnleft(*xs, xp));
1565 globit(xs, xpp, np, wp, check);
1570 size_t len, prefix_len;
1572 /* xp = *xpp; copy_non_glob() may have re-alloc'd xs */
1574 prefix_len = Xlength(*xs, xp);
1575 dirp = opendir(prefix_len ? Xstring(*xs, xp) : ".");
1578 while ((d = readdir(dirp)) != NULL) {
1580 if (name[0] == '.' &&
1581 (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1582 /* always ignore . and .. */
1584 if ((*name == '.' && *sp != '.') ||
1585 !gmatchx(name, sp, true))
1588 len = strlen(d->d_name) + 1;
1589 XcheckN(*xs, xp, len);
1590 memcpy(xp, name, len);
1591 *xpp = xp + len - 1;
1592 globit(xs, xpp, np, wp,
1593 (check & GF_MARKDIR) | GF_GLOBBED
1594 | (np ? GF_EXCHECK : GF_NONE));
1595 xp = Xstring(*xs, xp) + prefix_len;
1606 /* remove MAGIC from string */
1608 debunk(char *dp, const char *sp, size_t dlen)
1613 if ((s = cstrchr(sp, MAGIC))) {
1614 if (s - sp >= (ssize_t)dlen)
1616 memmove(dp, sp, s - sp);
1617 for (d = dp + (s - sp); *s && (d - dp < (ssize_t)dlen); s++)
1618 if (!ISMAGIC(*s) || !(*++s & 0x80) ||
1619 !vstrchr("*+?@! ", *s & 0x7f))
1622 /* extended pattern operators: *+?@! */
1623 if ((*s & 0x7f) != ' ')
1625 if (d - dp < (ssize_t)dlen)
1629 } else if (dp != sp)
1630 strlcpy(dp, sp, dlen);
1635 * Check if p is an unquoted name, possibly followed by a / or :. If so
1636 * puts the expanded version in *dcp,dp and returns a pointer in p just
1637 * past the name, otherwise returns 0.
1640 maybe_expand_tilde(const char *p, XString *dsp, char **dpp, int isassign)
1647 Xinit(ts, tp, 16, ATEMP);
1648 /* : only for DOASNTILDE form */
1649 while (p[0] == CHAR && p[1] != '/' && (!isassign || p[1] != ':'))
1656 r = (p[0] == EOS || p[0] == CHAR || p[0] == CSUBST) ?
1657 tilde(Xstring(ts, tp)) : NULL;
1675 * based on a version by Arnold Robbins
1684 dp = str_val(global("HOME"));
1685 else if (cp[0] == '+' && cp[1] == '\0')
1686 dp = str_val(global("PWD"));
1687 else if (cp[0] == '-' && cp[1] == '\0')
1688 dp = str_val(global("OLDPWD"));
1689 #ifndef MKSH_NOPWNAM
1693 /* If HOME, PWD or OLDPWD are not set, don't expand ~ */
1694 return (dp == null ? NULL : dp);
1697 #ifndef MKSH_NOPWNAM
1699 * map userid to user's home directory.
1700 * note that 4.3's getpw adds more than 6K to the shell,
1701 * and the YP version probably adds much more.
1702 * we might consider our own version of getpwnam() to keep the size down.
1709 ap = ktenter(&homedirs, name, hash(name));
1710 if (!(ap->flag & ISSET)) {
1713 pw = getpwnam(name);
1716 strdupx(ap->val.s, pw->pw_dir, APERM);
1717 ap->flag |= DEFINED|ISSET|ALLOC;
1724 alt_expand(XPtrV *wp, char *start, char *exp_start, char *end, int fdo)
1727 char *brace_start, *brace_end, *comma = NULL;
1731 /* search for open brace */
1732 for (p = exp_start; (p = strchr(p, MAGIC)) && p[1] != '{' /*}*/; p += 2)
1736 /* find matching close brace, if any */
1740 for (p += 2; *p && count; p++) {
1742 if (*++p == '{' /*}*/)
1744 else if (*p == /*{*/ '}')
1746 else if (*p == ',' && count == 1)
1751 /* no valid expansions... */
1752 if (!p || count != 0) {
1754 * Note that given a{{b,c} we do not expand anything (this is
1755 * what AT&T ksh does. This may be changed to do the {b,c}
1759 glob(start, wp, tobool(fdo & DOMARKDIRS));
1761 XPput(*wp, debunk(start, start, end - start));
1766 alt_expand(wp, start, brace_end, end, fdo);
1770 /* expand expression */
1771 field_start = brace_start + 2;
1773 for (p = brace_start + 2; p != brace_end; p++) {
1775 if (*++p == '{' /*}*/)
1777 else if ((*p == /*{*/ '}' && --count == 0) ||
1778 (*p == ',' && count == 1)) {
1783 * addition safe since these operate on
1784 * one string (separate substrings)
1786 l1 = brace_start - start;
1787 l2 = (p - 1) - field_start;
1788 l3 = end - brace_end;
1789 news = alloc(l1 + l2 + l3 + 1, ATEMP);
1790 memcpy(news, start, l1);
1791 memcpy(news + l1, field_start, l2);
1792 memcpy(news + l1 + l2, brace_end, l3);
1793 news[l1 + l2 + l3] = '\0';
1794 alt_expand(wp, news, news + l1,
1795 news + l1 + l2 + l3, fdo);
1796 field_start = p + 1;
1803 /* helper function due to setjmp/longjmp woes */
1805 funsub(struct op *t)
1809 if (!kshsetjmp(e->jbuf))
1810 execute(t, XXCOM | XERROK, NULL);