Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 1 | /* $OpenBSD: eval.c,v 1.40 2013/09/14 20:09:30 millert Exp $ */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 2 | |
| 3 | /*- |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 5 | * 2011, 2012, 2013, 2014, 2015, 2016, 2017 |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 6 | * mirabilos <m@mirbsd.org> |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 7 | * |
| 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. |
| 13 | * |
| 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. |
| 22 | */ |
| 23 | |
| 24 | #include "sh.h" |
| 25 | |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 26 | __RCSID("$MirOS: src/bin/mksh/eval.c,v 1.201 2017/04/06 01:59:54 tg Exp $"); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * string expansion |
| 30 | * |
| 31 | * first pass: quoting, IFS separation, ~, ${}, $() and $(()) substitution. |
| 32 | * second pass: alternation ({,}), filename expansion (*?[]). |
| 33 | */ |
| 34 | |
| 35 | /* expansion generator state */ |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 36 | typedef struct { |
| 37 | /* not including an "int type;" member, see expand() */ |
| 38 | /* string */ |
| 39 | const char *str; |
| 40 | /* source */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 41 | union { |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 42 | /* string[] */ |
| 43 | const char **strv; |
| 44 | /* file */ |
| 45 | struct shf *shf; |
| 46 | } u; |
| 47 | /* variable in ${var...} */ |
| 48 | struct tbl *var; |
| 49 | /* split "$@" / call waitlast in $() */ |
| 50 | bool split; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 51 | } Expand; |
| 52 | |
| 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 ${} */ |
| 60 | |
| 61 | /* States used for field splitting */ |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 62 | #define IFS_WORD 0 /* word has chars (or quotes except "$@") */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 63 | #define IFS_WS 1 /* have seen IFS white-space */ |
| 64 | #define IFS_NWS 2 /* have seen IFS non-white-space */ |
Elliott Hughes | 966dd55 | 2016-12-08 15:56:04 -0800 | [diff] [blame] | 65 | #define IFS_IWS 3 /* beginning of word, ignore IFS WS */ |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 66 | #define IFS_QUOTE 4 /* beg.w/quote, become IFS_WORD unless "$@" */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 67 | |
| 68 | static int varsub(Expand *, const char *, const char *, int *, int *); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 69 | static int comsub(Expand *, const char *, int); |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 70 | static char *valsub(struct op *, Area *); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 71 | static char *trimsub(char *, char *, int); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 72 | static void glob(char *, XPtrV *, bool); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 73 | static void globit(XString *, char **, char *, XPtrV *, int); |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 74 | static const char *maybe_expand_tilde(const char *, XString *, char **, bool); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 75 | #ifndef MKSH_NOPWNAM |
| 76 | static char *homedir(char *); |
| 77 | #endif |
| 78 | static void alt_expand(XPtrV *, char *, char *, char *, int); |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 79 | static int utflen(const char *) MKSH_A_PURE; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 80 | static void utfincptr(const char *, mksh_ari_t *); |
| 81 | |
| 82 | /* UTFMODE functions */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 83 | static int |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 84 | utflen(const char *s) |
| 85 | { |
| 86 | size_t n; |
| 87 | |
| 88 | if (UTFMODE) { |
| 89 | n = 0; |
| 90 | while (*s) { |
| 91 | s += utf_ptradj(s); |
| 92 | ++n; |
| 93 | } |
| 94 | } else |
| 95 | n = strlen(s); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 96 | |
| 97 | if (n > 2147483647) |
| 98 | n = 2147483647; |
| 99 | return ((int)n); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void |
| 103 | utfincptr(const char *s, mksh_ari_t *lp) |
| 104 | { |
| 105 | const char *cp = s; |
| 106 | |
| 107 | while ((*lp)--) |
| 108 | cp += utf_ptradj(cp); |
| 109 | *lp = cp - s; |
| 110 | } |
| 111 | |
| 112 | /* compile and expand word */ |
| 113 | char * |
| 114 | substitute(const char *cp, int f) |
| 115 | { |
| 116 | struct source *s, *sold; |
| 117 | |
| 118 | sold = source; |
| 119 | s = pushs(SWSTR, ATEMP); |
| 120 | s->start = s->str = cp; |
| 121 | source = s; |
| 122 | if (yylex(ONEWORD) != LWORD) |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 123 | internal_errorf(Tbadsubst); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 124 | source = sold; |
| 125 | afree(s, ATEMP); |
| 126 | return (evalstr(yylval.cp, f)); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * expand arg-list |
| 131 | */ |
| 132 | char ** |
| 133 | eval(const char **ap, int f) |
| 134 | { |
| 135 | XPtrV w; |
| 136 | |
| 137 | if (*ap == NULL) { |
| 138 | union mksh_ccphack vap; |
| 139 | |
| 140 | vap.ro = ap; |
| 141 | return (vap.rw); |
| 142 | } |
| 143 | XPinit(w, 32); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 144 | /* space for shell name */ |
| 145 | XPput(w, NULL); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 146 | while (*ap != NULL) |
| 147 | expand(*ap++, &w, f); |
| 148 | XPput(w, NULL); |
| 149 | return ((char **)XPclose(w) + 1); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * expand string |
| 154 | */ |
| 155 | char * |
| 156 | evalstr(const char *cp, int f) |
| 157 | { |
| 158 | XPtrV w; |
| 159 | char *dp = null; |
| 160 | |
| 161 | XPinit(w, 1); |
| 162 | expand(cp, &w, f); |
| 163 | if (XPsize(w)) |
| 164 | dp = *XPptrv(w); |
| 165 | XPfree(w); |
| 166 | return (dp); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * expand string - return only one component |
| 171 | * used from iosetup to expand redirection files |
| 172 | */ |
| 173 | char * |
| 174 | evalonestr(const char *cp, int f) |
| 175 | { |
| 176 | XPtrV w; |
| 177 | char *rv; |
| 178 | |
| 179 | XPinit(w, 1); |
| 180 | expand(cp, &w, f); |
| 181 | switch (XPsize(w)) { |
| 182 | case 0: |
| 183 | rv = null; |
| 184 | break; |
| 185 | case 1: |
| 186 | rv = (char *) *XPptrv(w); |
| 187 | break; |
| 188 | default: |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 189 | rv = evalstr(cp, f & ~DOGLOB); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | XPfree(w); |
| 193 | return (rv); |
| 194 | } |
| 195 | |
| 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) */ |
Elliott Hughes | 966dd55 | 2016-12-08 15:56:04 -0800 | [diff] [blame] | 201 | size_t base; /* start position of expanded word */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 202 | short stype; /* [=+-?%#] action after expanded word */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 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 ${..[+-=]..}) */ |
| 206 | } SubType; |
| 207 | |
| 208 | void |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 209 | expand( |
| 210 | /* input word */ |
| 211 | const char *ccp, |
| 212 | /* output words */ |
| 213 | XPtrV *wp, |
| 214 | /* DO* flags */ |
| 215 | int f) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 216 | { |
| 217 | int c = 0; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 218 | /* expansion type */ |
| 219 | int type; |
| 220 | /* quoted */ |
| 221 | int quote = 0; |
| 222 | /* destination string and live pointer */ |
| 223 | XString ds; |
| 224 | char *dp; |
| 225 | /* source */ |
| 226 | const char *sp; |
| 227 | /* second pass flags */ |
| 228 | int fdo; |
| 229 | /* have word */ |
| 230 | int word; |
| 231 | /* field splitting of parameter/command substitution */ |
| 232 | int doblank; |
| 233 | /* expansion variables */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 234 | Expand x = { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 235 | NULL, { NULL }, NULL, 0 |
| 236 | }; |
| 237 | SubType st_head, *st; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 238 | /* record number of trailing newlines in COMSUB */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 239 | int newlines = 0; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 240 | bool saw_eq, make_magic; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 241 | unsigned int tilde_ok; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 242 | size_t len; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 243 | char *cp; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 244 | |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 245 | if (ccp == NULL) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 246 | internal_errorf("expand(NULL)"); |
| 247 | /* for alias, readonly, set, typeset commands */ |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 248 | if ((f & DOVACHECK) && is_wdvarassign(ccp)) { |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 249 | f &= ~(DOVACHECK | DOBLANK | DOGLOB | DOTILDE); |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 250 | f |= DOASNTILDE | DOSCALAR; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 251 | } |
| 252 | if (Flag(FNOGLOB)) |
| 253 | f &= ~DOGLOB; |
| 254 | if (Flag(FMARKDIRS)) |
| 255 | f |= DOMARKDIRS; |
| 256 | if (Flag(FBRACEEXPAND) && (f & DOGLOB)) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 257 | f |= DOBRACE; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 258 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 259 | /* init destination string */ |
| 260 | Xinit(ds, dp, 128, ATEMP); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 261 | type = XBASE; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 262 | sp = ccp; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 263 | fdo = 0; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 264 | saw_eq = false; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 265 | /* must be 1/0 */ |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 266 | tilde_ok = (f & (DOTILDE | DOASNTILDE)) ? 1 : 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 267 | doblank = 0; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 268 | make_magic = false; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 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)); |
| 272 | st = &st_head; |
| 273 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 274 | while (/* CONSTCOND */ 1) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 275 | Xcheck(ds, dp); |
| 276 | |
| 277 | switch (type) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 278 | case XBASE: |
| 279 | /* original prefixed string */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 280 | c = *sp++; |
| 281 | switch (c) { |
| 282 | case EOS: |
| 283 | c = 0; |
| 284 | break; |
| 285 | case CHAR: |
| 286 | c = *sp++; |
| 287 | break; |
| 288 | case QCHAR: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 289 | /* temporary quote */ |
| 290 | quote |= 2; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 291 | c = *sp++; |
| 292 | break; |
| 293 | case OQUOTE: |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 294 | if (word != IFS_WORD) |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 295 | word = IFS_QUOTE; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 296 | tilde_ok = 0; |
| 297 | quote = 1; |
| 298 | continue; |
| 299 | case CQUOTE: |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 300 | if (word == IFS_QUOTE) |
| 301 | word = IFS_WORD; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 302 | quote = st->quotew; |
| 303 | continue; |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 304 | case COMASUB: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 305 | case COMSUB: |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 306 | case FUNASUB: |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 307 | case FUNSUB: |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 308 | case VALSUB: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 309 | tilde_ok = 0; |
| 310 | if (f & DONTRUNCOMMAND) { |
| 311 | word = IFS_WORD; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 312 | *dp++ = '$'; |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 313 | switch (c) { |
| 314 | case COMASUB: |
| 315 | case COMSUB: |
| 316 | *dp++ = '('; |
| 317 | c = ')'; |
| 318 | break; |
| 319 | case FUNASUB: |
| 320 | case FUNSUB: |
| 321 | case VALSUB: |
| 322 | *dp++ = '{'; |
| 323 | *dp++ = c == VALSUB ? '|' : ' '; |
| 324 | c = '}'; |
| 325 | break; |
| 326 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 327 | while (*sp != '\0') { |
| 328 | Xcheck(ds, dp); |
| 329 | *dp++ = *sp++; |
| 330 | } |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 331 | if (c == '}') |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 332 | *dp++ = ';'; |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 333 | *dp++ = c; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 334 | } else { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 335 | type = comsub(&x, sp, c); |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 336 | if (type != XBASE && (f & DOBLANK)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 337 | doblank++; |
| 338 | sp = strnul(sp) + 1; |
| 339 | newlines = 0; |
| 340 | } |
| 341 | continue; |
| 342 | case EXPRSUB: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 343 | tilde_ok = 0; |
| 344 | if (f & DONTRUNCOMMAND) { |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 345 | word = IFS_WORD; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 346 | *dp++ = '$'; *dp++ = '('; *dp++ = '('; |
| 347 | while (*sp != '\0') { |
| 348 | Xcheck(ds, dp); |
| 349 | *dp++ = *sp++; |
| 350 | } |
| 351 | *dp++ = ')'; *dp++ = ')'; |
| 352 | } else { |
| 353 | struct tbl v; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 354 | |
| 355 | v.flag = DEFINED|ISSET|INTEGER; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 356 | /* not default */ |
| 357 | v.type = 10; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 358 | v.name[0] = '\0'; |
| 359 | v_evaluate(&v, substitute(sp, 0), |
| 360 | KSH_UNWIND_ERROR, true); |
| 361 | sp = strnul(sp) + 1; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 362 | x.str = str_val(&v); |
| 363 | type = XSUB; |
| 364 | if (f & DOBLANK) |
| 365 | doblank++; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 366 | } |
| 367 | continue; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 368 | case OSUBST: { |
| 369 | /* ${{#}var{:}[=+-?#%]word} */ |
| 370 | /*- |
| 371 | * format is: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 372 | * OSUBST [{x] plain-variable-part \0 |
| 373 | * compiled-word-part CSUBST [}x] |
| 374 | * This is where all syntax checking gets done... |
| 375 | */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 376 | /* skip the { or x (}) */ |
| 377 | const char *varname = ++sp; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 378 | int stype; |
| 379 | int slen = 0; |
| 380 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 381 | /* skip variable */ |
| 382 | sp = cstrchr(sp, '\0') + 1; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 383 | type = varsub(&x, varname, sp, &stype, &slen); |
| 384 | if (type < 0) { |
| 385 | char *beg, *end, *str; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 386 | unwind_substsyn: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 387 | /* restore sp */ |
| 388 | sp = varname - 2; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 389 | beg = wdcopy(sp, ATEMP); |
| 390 | end = (wdscan(cstrchr(sp, '\0') + 1, |
| 391 | CSUBST) - sp) + beg; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 392 | /* ({) the } or x is already skipped */ |
| 393 | if (end < wdscan(beg, EOS)) |
| 394 | *end = EOS; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 395 | str = snptreef(NULL, 64, Tf_S, beg); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 396 | afree(beg, ATEMP); |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 397 | errorf(Tf_sD_s, str, Tbadsubst); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 398 | } |
| 399 | if (f & DOBLANK) |
| 400 | doblank++; |
| 401 | tilde_ok = 0; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 402 | if (word == IFS_QUOTE && type != XNULLSUB) |
| 403 | word = IFS_WORD; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 404 | if (type == XBASE) { |
| 405 | /* expand? */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 406 | if (!st->next) { |
| 407 | SubType *newst; |
| 408 | |
| 409 | newst = alloc(sizeof(SubType), ATEMP); |
| 410 | newst->next = NULL; |
| 411 | newst->prev = st; |
| 412 | st->next = newst; |
| 413 | } |
| 414 | st = st->next; |
| 415 | st->stype = stype; |
| 416 | st->base = Xsavepos(ds, dp); |
| 417 | st->f = f; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 418 | if (x.var == vtemp) { |
| 419 | st->var = tempvar(vtemp->name); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 420 | st->var->flag &= ~INTEGER; |
| 421 | /* can't fail here */ |
| 422 | setstr(st->var, |
| 423 | str_val(x.var), |
| 424 | KSH_RETURN_ERROR | 0x4); |
| 425 | } else |
| 426 | st->var = x.var; |
| 427 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 428 | st->quotew = st->quotep = quote; |
| 429 | /* skip qualifier(s) */ |
| 430 | if (stype) |
| 431 | sp += slen; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 432 | switch (stype & 0x17F) { |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 433 | case 0x100 | '#': |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 434 | x.str = shf_smprintf("%08X", |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 435 | (unsigned int)hash(str_val(st->var))); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 436 | break; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 437 | case 0x100 | 'Q': { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 438 | struct shf shf; |
| 439 | |
| 440 | shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf); |
| 441 | print_value_quoted(&shf, str_val(st->var)); |
| 442 | x.str = shf_sclose(&shf); |
| 443 | break; |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 444 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 445 | case '0': { |
| 446 | char *beg, *mid, *end, *stg; |
| 447 | mksh_ari_t from = 0, num = -1, flen, finc = 0; |
| 448 | |
| 449 | beg = wdcopy(sp, ATEMP); |
| 450 | mid = beg + (wdscan(sp, ADELIM) - sp); |
| 451 | stg = beg + (wdscan(sp, CSUBST) - sp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 452 | mid[-2] = EOS; |
| 453 | if (mid[-1] == /*{*/'}') { |
| 454 | sp += mid - beg - 1; |
| 455 | end = NULL; |
| 456 | } else { |
| 457 | end = mid + |
| 458 | (wdscan(mid, ADELIM) - mid); |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 459 | if (end[-1] != /*{*/ '}') |
| 460 | /* more than max delimiters */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 461 | goto unwind_substsyn; |
| 462 | end[-2] = EOS; |
| 463 | sp += end - beg - 1; |
| 464 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 465 | evaluate(substitute(stg = wdstrip(beg, 0), 0), |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 466 | &from, KSH_UNWIND_ERROR, true); |
| 467 | afree(stg, ATEMP); |
| 468 | if (end) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 469 | evaluate(substitute(stg = wdstrip(mid, 0), 0), |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 470 | &num, KSH_UNWIND_ERROR, true); |
| 471 | afree(stg, ATEMP); |
| 472 | } |
| 473 | afree(beg, ATEMP); |
| 474 | beg = str_val(st->var); |
| 475 | flen = utflen(beg); |
| 476 | if (from < 0) { |
| 477 | if (-from < flen) |
| 478 | finc = flen + from; |
| 479 | } else |
| 480 | finc = from < flen ? from : flen; |
| 481 | if (UTFMODE) |
| 482 | utfincptr(beg, &finc); |
| 483 | beg += finc; |
| 484 | flen = utflen(beg); |
| 485 | if (num < 0 || num > flen) |
| 486 | num = flen; |
| 487 | if (UTFMODE) |
| 488 | utfincptr(beg, &num); |
| 489 | strndupx(x.str, beg, num, ATEMP); |
| 490 | goto do_CSUBST; |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 491 | } |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 492 | case 0x100 | '/': |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 493 | case '/': { |
| 494 | char *s, *p, *d, *sbeg, *end; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 495 | char *pat = NULL, *rrep = null; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 496 | char fpat = 0, *tpat1, *tpat2; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 497 | char *ws, *wpat, *wrep; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 498 | |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 499 | s = ws = wdcopy(sp, ATEMP); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 500 | p = s + (wdscan(sp, ADELIM) - sp); |
| 501 | d = s + (wdscan(sp, CSUBST) - sp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 502 | p[-2] = EOS; |
| 503 | if (p[-1] == /*{*/'}') |
| 504 | d = NULL; |
| 505 | else |
| 506 | d[-2] = EOS; |
| 507 | sp += (d ? d : p) - s - 1; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 508 | if (!(stype & 0x180) && |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 509 | s[0] == CHAR && |
| 510 | (s[1] == '#' || s[1] == '%')) |
| 511 | fpat = s[1]; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 512 | wpat = s + (fpat ? 2 : 0); |
| 513 | wrep = d ? p : NULL; |
| 514 | if (!(stype & 0x100)) { |
| 515 | rrep = wrep ? evalstr(wrep, |
| 516 | DOTILDE | DOSCALAR) : |
| 517 | null; |
| 518 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 519 | |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 520 | /* prepare string on which to work */ |
| 521 | strdupx(s, str_val(st->var), ATEMP); |
| 522 | sbeg = s; |
| 523 | again_search: |
| 524 | pat = evalstr(wpat, |
| 525 | DOTILDE | DOSCALAR | DOPAT); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 526 | /* check for special cases */ |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 527 | if (!*pat && !fpat) { |
| 528 | /* |
| 529 | * empty unanchored |
| 530 | * pattern => reject |
| 531 | */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 532 | goto no_repl; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 533 | } |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 534 | if ((stype & 0x180) && |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 535 | gmatchx(null, pat, false)) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 536 | /* |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 537 | * pattern matches empty |
| 538 | * string => don't loop |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 539 | */ |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 540 | stype &= ~0x180; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 541 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 542 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 543 | /* first see if we have any match at all */ |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 544 | if (fpat == '#') { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 545 | /* anchor at the beginning */ |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 546 | tpat1 = shf_smprintf("%s%c*", pat, MAGIC); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 547 | tpat2 = tpat1; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 548 | } else if (fpat == '%') { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 549 | /* anchor at the end */ |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 550 | tpat1 = shf_smprintf("%c*%s", MAGIC, pat); |
| 551 | tpat2 = pat; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 552 | } else { |
| 553 | /* float */ |
| 554 | tpat1 = shf_smprintf("%c*%s%c*", MAGIC, pat, MAGIC); |
| 555 | tpat2 = tpat1 + 2; |
| 556 | } |
| 557 | again_repl: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 558 | /* |
| 559 | * this would not be necessary if gmatchx would return |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 560 | * the start and end values of a match found, like re* |
| 561 | */ |
| 562 | if (!gmatchx(sbeg, tpat1, false)) |
| 563 | goto end_repl; |
| 564 | end = strnul(s); |
| 565 | /* now anchor the beginning of the match */ |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 566 | if (fpat != '#') |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 567 | while (sbeg <= end) { |
| 568 | if (gmatchx(sbeg, tpat2, false)) |
| 569 | break; |
| 570 | else |
| 571 | sbeg++; |
| 572 | } |
| 573 | /* now anchor the end of the match */ |
| 574 | p = end; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 575 | if (fpat != '%') |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 576 | while (p >= sbeg) { |
| 577 | bool gotmatch; |
| 578 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 579 | c = *p; |
| 580 | *p = '\0'; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 581 | gotmatch = tobool(gmatchx(sbeg, pat, false)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 582 | *p = c; |
| 583 | if (gotmatch) |
| 584 | break; |
| 585 | p--; |
| 586 | } |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 587 | strndupx(end, sbeg, p - sbeg, ATEMP); |
| 588 | record_match(end); |
| 589 | afree(end, ATEMP); |
| 590 | if (stype & 0x100) { |
| 591 | if (rrep != null) |
| 592 | afree(rrep, ATEMP); |
| 593 | rrep = wrep ? evalstr(wrep, |
| 594 | DOTILDE | DOSCALAR) : |
| 595 | null; |
| 596 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 597 | strndupx(end, s, sbeg - s, ATEMP); |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 598 | d = shf_smprintf(Tf_sss, end, rrep, p); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 599 | afree(end, ATEMP); |
| 600 | sbeg = d + (sbeg - s) + strlen(rrep); |
| 601 | afree(s, ATEMP); |
| 602 | s = d; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 603 | if (stype & 0x100) { |
| 604 | afree(tpat1, ATEMP); |
| 605 | afree(pat, ATEMP); |
| 606 | goto again_search; |
| 607 | } else if (stype & 0x80) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 608 | goto again_repl; |
| 609 | end_repl: |
| 610 | afree(tpat1, ATEMP); |
| 611 | x.str = s; |
| 612 | no_repl: |
| 613 | afree(pat, ATEMP); |
| 614 | if (rrep != null) |
| 615 | afree(rrep, ATEMP); |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 616 | afree(ws, ATEMP); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 617 | goto do_CSUBST; |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 618 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 619 | case '#': |
| 620 | case '%': |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 621 | /* ! DOBLANK,DOBRACE */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 622 | f = (f & DONTRUNCOMMAND) | |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 623 | DOPAT | DOTILDE | |
| 624 | DOTEMP | DOSCALAR; |
| 625 | tilde_ok = 1; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 626 | st->quotew = quote = 0; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 627 | /* |
| 628 | * Prepend open pattern (so | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 629 | * in a trim will work as |
| 630 | * expected) |
| 631 | */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 632 | if (!Flag(FSH)) { |
| 633 | *dp++ = MAGIC; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 634 | *dp++ = 0x80 | '@'; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 635 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 636 | break; |
| 637 | case '=': |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 638 | /* |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 639 | * Tilde expansion for string |
| 640 | * variables in POSIX mode is |
| 641 | * governed by Austinbug 351. |
| 642 | * In non-POSIX mode historic |
| 643 | * ksh behaviour (enable it!) |
| 644 | * us followed. |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 645 | * Not doing tilde expansion |
| 646 | * for integer variables is a |
| 647 | * non-POSIX thing - makes |
| 648 | * sense though, since ~ is |
| 649 | * a arithmetic operator. |
| 650 | */ |
| 651 | if (!(x.var->flag & INTEGER)) |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 652 | f |= DOASNTILDE | DOTILDE; |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 653 | f |= DOTEMP | DOSCALAR; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 654 | /* |
| 655 | * These will be done after the |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 656 | * value has been assigned. |
| 657 | */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 658 | f &= ~(DOBLANK|DOGLOB|DOBRACE); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 659 | tilde_ok = 1; |
| 660 | break; |
| 661 | case '?': |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 662 | if (*sp == CSUBST) |
| 663 | errorf("%s: parameter null or not set", |
| 664 | st->var->name); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 665 | f &= ~DOBLANK; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 666 | f |= DOTEMP; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 667 | /* FALLTHROUGH */ |
| 668 | default: |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 669 | /* '-' '+' '?' */ |
| 670 | if (quote) |
| 671 | word = IFS_WORD; |
| 672 | else if (dp == Xstring(ds, dp)) |
| 673 | word = IFS_IWS; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 674 | /* Enable tilde expansion */ |
| 675 | tilde_ok = 1; |
| 676 | f |= DOTILDE; |
| 677 | } |
| 678 | } else |
| 679 | /* skip word */ |
| 680 | sp += wdscan(sp, CSUBST) - sp; |
| 681 | continue; |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 682 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 683 | case CSUBST: |
| 684 | /* only get here if expanding word */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 685 | do_CSUBST: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 686 | /* ({) skip the } or x */ |
| 687 | sp++; |
| 688 | /* in case of ${unset:-} */ |
| 689 | tilde_ok = 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 690 | *dp = '\0'; |
| 691 | quote = st->quotep; |
| 692 | f = st->f; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 693 | if (f & DOBLANK) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 694 | doblank--; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 695 | switch (st->stype & 0x17F) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 696 | case '#': |
| 697 | case '%': |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 698 | if (!Flag(FSH)) { |
| 699 | /* Append end-pattern */ |
| 700 | *dp++ = MAGIC; |
| 701 | *dp++ = ')'; |
| 702 | } |
| 703 | *dp = '\0'; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 704 | dp = Xrestpos(ds, dp, st->base); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 705 | /* |
| 706 | * Must use st->var since calling |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 707 | * global would break things |
| 708 | * like x[i+=1]. |
| 709 | */ |
| 710 | x.str = trimsub(str_val(st->var), |
| 711 | dp, st->stype); |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 712 | if (x.str[0] != '\0') { |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 713 | word = IFS_IWS; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 714 | type = XSUB; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 715 | } else if (quote) { |
| 716 | word = IFS_WORD; |
| 717 | type = XSUB; |
| 718 | } else { |
| 719 | if (dp == Xstring(ds, dp)) |
| 720 | word = IFS_IWS; |
| 721 | type = XNULLSUB; |
| 722 | } |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 723 | if (f & DOBLANK) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 724 | doblank++; |
| 725 | st = st->prev; |
| 726 | continue; |
| 727 | case '=': |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 728 | /* |
| 729 | * Restore our position and substitute |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 730 | * the value of st->var (may not be |
| 731 | * the assigned value in the presence |
| 732 | * of integer/right-adj/etc attributes). |
| 733 | */ |
| 734 | dp = Xrestpos(ds, dp, st->base); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 735 | /* |
| 736 | * Must use st->var since calling |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 737 | * global would cause with things |
| 738 | * like x[i+=1] to be evaluated twice. |
| 739 | */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 740 | /* |
| 741 | * Note: not exported by FEXPORT |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 742 | * in AT&T ksh. |
| 743 | */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 744 | /* |
| 745 | * XXX POSIX says readonly is only |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 746 | * fatal for special builtins (setstr |
| 747 | * does readonly check). |
| 748 | */ |
| 749 | len = strlen(dp) + 1; |
| 750 | setstr(st->var, |
| 751 | debunk(alloc(len, ATEMP), |
| 752 | dp, len), KSH_UNWIND_ERROR); |
| 753 | x.str = str_val(st->var); |
| 754 | type = XSUB; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 755 | if (f & DOBLANK) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 756 | doblank++; |
| 757 | st = st->prev; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 758 | word = quote || (!*x.str && (f & DOSCALAR)) ? IFS_WORD : IFS_IWS; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 759 | continue; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 760 | case '?': |
| 761 | dp = Xrestpos(ds, dp, st->base); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 762 | |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 763 | errorf(Tf_sD_s, st->var->name, |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 764 | debunk(dp, dp, strlen(dp) + 1)); |
| 765 | break; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 766 | case '0': |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 767 | case 0x100 | '/': |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 768 | case '/': |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 769 | case 0x100 | '#': |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 770 | case 0x100 | 'Q': |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 771 | dp = Xrestpos(ds, dp, st->base); |
| 772 | type = XSUB; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 773 | word = quote || (!*x.str && (f & DOSCALAR)) ? IFS_WORD : IFS_IWS; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 774 | if (f & DOBLANK) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 775 | doblank++; |
| 776 | st = st->prev; |
| 777 | continue; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 778 | /* default: '-' '+' */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 779 | } |
| 780 | st = st->prev; |
| 781 | type = XBASE; |
| 782 | continue; |
| 783 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 784 | case OPAT: |
| 785 | /* open pattern: *(foo|bar) */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 786 | /* Next char is the type of pattern */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 787 | make_magic = true; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 788 | c = *sp++ | 0x80; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 789 | break; |
| 790 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 791 | case SPAT: |
| 792 | /* pattern separator (|) */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 793 | make_magic = true; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 794 | c = '|'; |
| 795 | break; |
| 796 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 797 | case CPAT: |
| 798 | /* close pattern */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 799 | make_magic = true; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 800 | c = /*(*/ ')'; |
| 801 | break; |
| 802 | } |
| 803 | break; |
| 804 | |
| 805 | case XNULLSUB: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 806 | /* |
| 807 | * Special case for "$@" (and "${foo[@]}") - no |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 808 | * word is generated if $# is 0 (unless there is |
| 809 | * other stuff inside the quotes). |
| 810 | */ |
| 811 | type = XBASE; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 812 | if (f & DOBLANK) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 813 | doblank--; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 814 | if (dp == Xstring(ds, dp) && word != IFS_WORD) |
| 815 | word = IFS_IWS; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 816 | } |
| 817 | continue; |
| 818 | |
| 819 | case XSUB: |
| 820 | case XSUBMID: |
| 821 | if ((c = *x.str++) == 0) { |
| 822 | type = XBASE; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 823 | if (f & DOBLANK) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 824 | doblank--; |
| 825 | continue; |
| 826 | } |
| 827 | break; |
| 828 | |
| 829 | case XARGSEP: |
| 830 | type = XARG; |
| 831 | quote = 1; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 832 | /* FALLTHROUGH */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 833 | case XARG: |
| 834 | if ((c = *x.str++) == '\0') { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 835 | /* |
| 836 | * force null words to be created so |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 837 | * set -- "" 2 ""; echo "$@" will do |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 838 | * the right thing |
| 839 | */ |
| 840 | if (quote && x.split) |
| 841 | word = IFS_WORD; |
| 842 | if ((x.str = *x.u.strv++) == NULL) { |
| 843 | type = XBASE; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 844 | if (f & DOBLANK) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 845 | doblank--; |
| 846 | continue; |
| 847 | } |
| 848 | c = ifs0; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 849 | if ((f & DOHEREDOC)) { |
| 850 | /* pseudo-field-split reliably */ |
| 851 | if (c == 0) |
| 852 | c = ' '; |
| 853 | break; |
| 854 | } |
| 855 | if ((f & DOSCALAR)) { |
| 856 | /* do not field-split */ |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 857 | if (x.split) { |
| 858 | c = ' '; |
| 859 | break; |
| 860 | } |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 861 | if (c == 0) |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 862 | continue; |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 863 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 864 | if (c == 0) { |
| 865 | if (quote && !x.split) |
| 866 | continue; |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 867 | if (!quote && word == IFS_WS) |
| 868 | continue; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 869 | /* this is so we don't terminate */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 870 | c = ' '; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 871 | /* now force-emit a word */ |
| 872 | goto emit_word; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 873 | } |
| 874 | if (quote && x.split) { |
| 875 | /* terminate word for "$@" */ |
| 876 | type = XARGSEP; |
| 877 | quote = 0; |
| 878 | } |
| 879 | } |
| 880 | break; |
| 881 | |
| 882 | case XCOM: |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 883 | if (x.u.shf == NULL) { |
| 884 | /* $(<...) failed */ |
| 885 | subst_exstat = 1; |
| 886 | /* fake EOF */ |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 887 | c = -1; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 888 | } else if (newlines) { |
| 889 | /* spit out saved NLs */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 890 | c = '\n'; |
| 891 | --newlines; |
| 892 | } else { |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 893 | while ((c = shf_getc(x.u.shf)) == 0 || |
| 894 | #ifdef MKSH_WITH_TEXTMODE |
| 895 | c == '\r' || |
| 896 | #endif |
| 897 | c == '\n') { |
| 898 | #ifdef MKSH_WITH_TEXTMODE |
| 899 | if (c == '\r') { |
| 900 | c = shf_getc(x.u.shf); |
| 901 | switch (c) { |
| 902 | case '\n': |
| 903 | break; |
| 904 | default: |
| 905 | shf_ungetc(c, x.u.shf); |
| 906 | /* FALLTHROUGH */ |
| 907 | case -1: |
| 908 | c = '\r'; |
| 909 | break; |
| 910 | } |
| 911 | } |
| 912 | #endif |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 913 | if (c == '\n') |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 914 | /* save newlines */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 915 | newlines++; |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 916 | } |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 917 | if (newlines && c != -1) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 918 | shf_ungetc(c, x.u.shf); |
| 919 | c = '\n'; |
| 920 | --newlines; |
| 921 | } |
| 922 | } |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 923 | if (c == -1) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 924 | newlines = 0; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 925 | if (x.u.shf) |
| 926 | shf_close(x.u.shf); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 927 | if (x.split) |
| 928 | subst_exstat = waitlast(); |
| 929 | type = XBASE; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 930 | if (f & DOBLANK) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 931 | doblank--; |
| 932 | continue; |
| 933 | } |
| 934 | break; |
| 935 | } |
| 936 | |
| 937 | /* check for end of word or IFS separation */ |
| 938 | if (c == 0 || (!quote && (f & DOBLANK) && doblank && |
| 939 | !make_magic && ctype(c, C_IFS))) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 940 | /*- |
| 941 | * How words are broken up: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 942 | * | value of c |
| 943 | * word | ws nws 0 |
| 944 | * ----------------------------------- |
| 945 | * IFS_WORD w/WS w/NWS w |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 946 | * IFS_WS -/WS -/NWS - |
| 947 | * IFS_NWS -/NWS w/NWS - |
Elliott Hughes | f7f7956 | 2014-10-07 15:04:14 -0700 | [diff] [blame] | 948 | * IFS_IWS -/WS w/NWS - |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 949 | * (w means generate a word) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 950 | */ |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 951 | if ((word == IFS_WORD) || (word == IFS_QUOTE) || (c && |
Elliott Hughes | f7f7956 | 2014-10-07 15:04:14 -0700 | [diff] [blame] | 952 | (word == IFS_IWS || word == IFS_NWS) && |
| 953 | !ctype(c, C_IFSWS))) { |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 954 | emit_word: |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 955 | if (f & DOHERESTR) |
| 956 | *dp++ = '\n'; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 957 | *dp++ = '\0'; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 958 | cp = Xclose(ds, dp); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 959 | if (fdo & DOBRACE) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 960 | /* also does globbing */ |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 961 | alt_expand(wp, cp, cp, |
| 962 | cp + Xlength(ds, (dp - 1)), |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 963 | fdo | (f & DOMARKDIRS)); |
| 964 | else if (fdo & DOGLOB) |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 965 | glob(cp, wp, tobool(f & DOMARKDIRS)); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 966 | else if ((f & DOPAT) || !(fdo & DOMAGIC)) |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 967 | XPput(*wp, cp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 968 | else |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 969 | XPput(*wp, debunk(cp, cp, |
| 970 | strlen(cp) + 1)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 971 | fdo = 0; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 972 | saw_eq = false; |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 973 | /* must be 1/0 */ |
| 974 | tilde_ok = (f & (DOTILDE | DOASNTILDE)) ? 1 : 0; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 975 | if (c == 0) |
| 976 | return; |
| 977 | Xinit(ds, dp, 128, ATEMP); |
| 978 | } else if (c == 0) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 979 | return; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 980 | } else if (type == XSUB && ctype(c, C_IFS) && |
| 981 | !ctype(c, C_IFSWS) && Xlength(ds, dp) == 0) { |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 982 | *(cp = alloc(1, ATEMP)) = '\0'; |
| 983 | XPput(*wp, cp); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 984 | type = XSUBMID; |
| 985 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 986 | if (word != IFS_NWS) |
| 987 | word = ctype(c, C_IFSWS) ? IFS_WS : IFS_NWS; |
| 988 | } else { |
| 989 | if (type == XSUB) { |
| 990 | if (word == IFS_NWS && |
| 991 | Xlength(ds, dp) == 0) { |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 992 | *(cp = alloc(1, ATEMP)) = '\0'; |
| 993 | XPput(*wp, cp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 994 | } |
| 995 | type = XSUBMID; |
| 996 | } |
| 997 | |
| 998 | /* age tilde_ok info - ~ code tests second bit */ |
| 999 | tilde_ok <<= 1; |
| 1000 | /* mark any special second pass chars */ |
| 1001 | if (!quote) |
| 1002 | switch (c) { |
| 1003 | case '[': |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1004 | case '!': |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1005 | case '-': |
| 1006 | case ']': |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1007 | /* |
| 1008 | * For character classes - doesn't hurt |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1009 | * to have magic !,-,]s outside of |
| 1010 | * [...] expressions. |
| 1011 | */ |
| 1012 | if (f & (DOPAT | DOGLOB)) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1013 | fdo |= DOMAGIC; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1014 | if (c == '[') |
| 1015 | fdo |= f & DOGLOB; |
| 1016 | *dp++ = MAGIC; |
| 1017 | } |
| 1018 | break; |
| 1019 | case '*': |
| 1020 | case '?': |
| 1021 | if (f & (DOPAT | DOGLOB)) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1022 | fdo |= DOMAGIC | (f & DOGLOB); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1023 | *dp++ = MAGIC; |
| 1024 | } |
| 1025 | break; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1026 | case '{': |
| 1027 | case '}': |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1028 | case ',': |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1029 | if ((f & DOBRACE) && (c == '{' /*}*/ || |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1030 | (fdo & DOBRACE))) { |
| 1031 | fdo |= DOBRACE|DOMAGIC; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1032 | *dp++ = MAGIC; |
| 1033 | } |
| 1034 | break; |
| 1035 | case '=': |
| 1036 | /* Note first unquoted = for ~ */ |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 1037 | if (!(f & DOTEMP) && (!Flag(FPOSIX) || |
| 1038 | (f & DOASNTILDE)) && !saw_eq) { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1039 | saw_eq = true; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1040 | tilde_ok = 1; |
| 1041 | } |
| 1042 | break; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1043 | case ':': |
| 1044 | /* : */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1045 | /* Note unquoted : for ~ */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1046 | if (!(f & DOTEMP) && (f & DOASNTILDE)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1047 | tilde_ok = 1; |
| 1048 | break; |
| 1049 | case '~': |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1050 | /* |
| 1051 | * tilde_ok is reset whenever |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1052 | * any of ' " $( $(( ${ } are seen. |
| 1053 | * Note that tilde_ok must be preserved |
| 1054 | * through the sequence ${A=a=}~ |
| 1055 | */ |
| 1056 | if (type == XBASE && |
Elliott Hughes | 56b517d | 2014-10-06 11:30:44 -0700 | [diff] [blame] | 1057 | (f & (DOTILDE | DOASNTILDE)) && |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1058 | (tilde_ok & 2)) { |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1059 | const char *tcp; |
| 1060 | char *tdp = dp; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1061 | |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1062 | tcp = maybe_expand_tilde(sp, |
| 1063 | &ds, &tdp, |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 1064 | tobool(f & DOASNTILDE)); |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1065 | if (tcp) { |
| 1066 | if (dp != tdp) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1067 | word = IFS_WORD; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1068 | dp = tdp; |
| 1069 | sp = tcp; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1070 | continue; |
| 1071 | } |
| 1072 | } |
| 1073 | break; |
| 1074 | } |
| 1075 | else |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1076 | /* undo temporary */ |
| 1077 | quote &= ~2; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1078 | |
| 1079 | if (make_magic) { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1080 | make_magic = false; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1081 | fdo |= DOMAGIC | (f & DOGLOB); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1082 | *dp++ = MAGIC; |
| 1083 | } else if (ISMAGIC(c)) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1084 | fdo |= DOMAGIC; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1085 | *dp++ = MAGIC; |
| 1086 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1087 | /* save output char */ |
| 1088 | *dp++ = c; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1089 | word = IFS_WORD; |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 1094 | static bool |
| 1095 | hasnonempty(const char **strv) |
| 1096 | { |
| 1097 | size_t i = 0; |
| 1098 | |
| 1099 | while (strv[i]) |
| 1100 | if (*strv[i++]) |
| 1101 | return (true); |
| 1102 | return (false); |
| 1103 | } |
| 1104 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1105 | /* |
| 1106 | * Prepare to generate the string returned by ${} substitution. |
| 1107 | */ |
| 1108 | static int |
| 1109 | varsub(Expand *xp, const char *sp, const char *word, |
| 1110 | int *stypep, /* becomes qualifier type */ |
| 1111 | int *slenp) /* " " len (=, :=, etc.) valid iff *stypep != 0 */ |
| 1112 | { |
| 1113 | int c; |
| 1114 | int state; /* next state: XBASE, XARG, XSUB, XNULLSUB */ |
| 1115 | int stype; /* substitution type */ |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1116 | int slen = 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1117 | const char *p; |
| 1118 | struct tbl *vp; |
| 1119 | bool zero_ok = false; |
| 1120 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1121 | if ((stype = sp[0]) == '\0') |
| 1122 | /* Bad variable name */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1123 | return (-1); |
| 1124 | |
| 1125 | xp->var = NULL; |
| 1126 | |
| 1127 | /*- |
| 1128 | * ${#var}, string length (-U: characters, +U: octets) or array size |
| 1129 | * ${%var}, string width (-U: screen columns, +U: octets) |
| 1130 | */ |
| 1131 | c = sp[1]; |
| 1132 | if (stype == '%' && c == '\0') |
| 1133 | return (-1); |
| 1134 | if ((stype == '#' || stype == '%') && c != '\0') { |
| 1135 | /* Can't have any modifiers for ${#...} or ${%...} */ |
| 1136 | if (*word != CSUBST) |
| 1137 | return (-1); |
| 1138 | sp++; |
| 1139 | /* Check for size of array */ |
| 1140 | if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') && |
| 1141 | p[2] == ']') { |
| 1142 | int n = 0; |
| 1143 | |
| 1144 | if (stype != '#') |
| 1145 | return (-1); |
| 1146 | vp = global(arrayname(sp)); |
| 1147 | if (vp->flag & (ISSET|ARRAY)) |
| 1148 | zero_ok = true; |
| 1149 | for (; vp; vp = vp->u.array) |
| 1150 | if (vp->flag & ISSET) |
| 1151 | n++; |
| 1152 | c = n; |
| 1153 | } else if (c == '*' || c == '@') { |
| 1154 | if (stype != '#') |
| 1155 | return (-1); |
| 1156 | c = e->loc->argc; |
| 1157 | } else { |
| 1158 | p = str_val(global(sp)); |
| 1159 | zero_ok = p != null; |
| 1160 | if (stype == '#') |
| 1161 | c = utflen(p); |
| 1162 | else { |
| 1163 | /* partial utf_mbswidth reimplementation */ |
| 1164 | const char *s = p; |
| 1165 | unsigned int wc; |
| 1166 | size_t len; |
| 1167 | int cw; |
| 1168 | |
| 1169 | c = 0; |
| 1170 | while (*s) { |
| 1171 | if (!UTFMODE || (len = utf_mbtowc(&wc, |
| 1172 | s)) == (size_t)-1) |
| 1173 | /* not UTFMODE or not UTF-8 */ |
| 1174 | wc = (unsigned char)(*s++); |
| 1175 | else |
| 1176 | /* UTFMODE and UTF-8 */ |
| 1177 | s += len; |
| 1178 | /* wc == char or wchar at s++ */ |
| 1179 | if ((cw = utf_wcwidth(wc)) == -1) { |
| 1180 | /* 646, 8859-1, 10646 C0/C1 */ |
| 1181 | c = -1; |
| 1182 | break; |
| 1183 | } |
| 1184 | c += cw; |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | if (Flag(FNOUNSET) && c == 0 && !zero_ok) |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1189 | errorf(Tf_parm, sp); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1190 | /* unqualified variable/string substitution */ |
| 1191 | *stypep = 0; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1192 | xp->str = shf_smprintf(Tf_d, c); |
| 1193 | return (XSUB); |
| 1194 | } |
| 1195 | if (stype == '!' && c != '\0' && *word == CSUBST) { |
| 1196 | sp++; |
| 1197 | if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') && |
| 1198 | p[2] == ']') { |
| 1199 | c = '!'; |
| 1200 | stype = 0; |
| 1201 | goto arraynames; |
| 1202 | } |
| 1203 | xp->var = global(sp); |
| 1204 | xp->str = p ? shf_smprintf("%s[%lu]", |
| 1205 | xp->var->name, arrayindex(xp->var)) : xp->var->name; |
| 1206 | *stypep = 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1207 | return (XSUB); |
| 1208 | } |
| 1209 | |
| 1210 | /* Check for qualifiers in word part */ |
| 1211 | stype = 0; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1212 | c = word[slen + 0] == CHAR ? word[slen + 1] : 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1213 | if (c == ':') { |
| 1214 | slen += 2; |
| 1215 | stype = 0x80; |
| 1216 | c = word[slen + 0] == CHAR ? word[slen + 1] : 0; |
| 1217 | } |
| 1218 | if (!stype && c == '/') { |
| 1219 | slen += 2; |
| 1220 | stype = c; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1221 | if (word[slen] == ADELIM && word[slen + 1] == c) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1222 | slen += 2; |
| 1223 | stype |= 0x80; |
| 1224 | } |
| 1225 | } else if (stype == 0x80 && (c == ' ' || c == '0')) { |
| 1226 | stype |= '0'; |
| 1227 | } else if (ctype(c, C_SUBOP1)) { |
| 1228 | slen += 2; |
| 1229 | stype |= c; |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1230 | } else if (ksh_issubop2(c)) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1231 | /* Note: ksh88 allows :%, :%%, etc */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1232 | slen += 2; |
| 1233 | stype = c; |
| 1234 | if (word[slen + 0] == CHAR && c == word[slen + 1]) { |
| 1235 | stype |= 0x80; |
| 1236 | slen += 2; |
| 1237 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1238 | } else if (c == '@') { |
| 1239 | /* @x where x is command char */ |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1240 | switch (c = word[slen + 2] == CHAR ? word[slen + 3] : 0) { |
| 1241 | case '#': |
| 1242 | case '/': |
| 1243 | case 'Q': |
| 1244 | break; |
| 1245 | default: |
| 1246 | return (-1); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1247 | } |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1248 | stype |= 0x100 | c; |
| 1249 | slen += 4; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1250 | } else if (stype) |
| 1251 | /* : is not ok */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1252 | return (-1); |
| 1253 | if (!stype && *word != CSUBST) |
| 1254 | return (-1); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1255 | |
| 1256 | c = sp[0]; |
| 1257 | if (c == '*' || c == '@') { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1258 | switch (stype & 0x17F) { |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1259 | /* can't assign to a vector */ |
| 1260 | case '=': |
| 1261 | /* can't trim a vector (yet) */ |
| 1262 | case '%': |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1263 | case '#': |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1264 | case '?': |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1265 | case '0': |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1266 | case 0x100 | '/': |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1267 | case '/': |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1268 | case 0x100 | '#': |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1269 | case 0x100 | 'Q': |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1270 | return (-1); |
| 1271 | } |
| 1272 | if (e->loc->argc == 0) { |
| 1273 | xp->str = null; |
| 1274 | xp->var = global(sp); |
| 1275 | state = c == '@' ? XNULLSUB : XSUB; |
| 1276 | } else { |
| 1277 | xp->u.strv = (const char **)e->loc->argv + 1; |
| 1278 | xp->str = *xp->u.strv++; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1279 | /* $@ */ |
| 1280 | xp->split = tobool(c == '@'); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1281 | state = XARG; |
| 1282 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1283 | /* POSIX 2009? */ |
| 1284 | zero_ok = true; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1285 | } else if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') && |
| 1286 | p[2] == ']') { |
| 1287 | XPtrV wv; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1288 | |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1289 | switch (stype & 0x17F) { |
| 1290 | /* can't assign to a vector */ |
| 1291 | case '=': |
| 1292 | /* can't trim a vector (yet) */ |
| 1293 | case '%': |
| 1294 | case '#': |
| 1295 | case '?': |
| 1296 | case '0': |
| 1297 | case 0x100 | '/': |
| 1298 | case '/': |
| 1299 | case 0x100 | '#': |
| 1300 | case 0x100 | 'Q': |
| 1301 | return (-1); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1302 | } |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1303 | c = 0; |
| 1304 | arraynames: |
| 1305 | XPinit(wv, 32); |
| 1306 | vp = global(arrayname(sp)); |
| 1307 | for (; vp; vp = vp->u.array) { |
| 1308 | if (!(vp->flag&ISSET)) |
| 1309 | continue; |
| 1310 | XPput(wv, c == '!' ? shf_smprintf(Tf_lu, |
| 1311 | arrayindex(vp)) : |
| 1312 | str_val(vp)); |
| 1313 | } |
| 1314 | if (XPsize(wv) == 0) { |
| 1315 | xp->str = null; |
| 1316 | state = p[1] == '@' ? XNULLSUB : XSUB; |
| 1317 | XPfree(wv); |
| 1318 | } else { |
| 1319 | XPput(wv, 0); |
| 1320 | xp->u.strv = (const char **)XPptrv(wv); |
| 1321 | xp->str = *xp->u.strv++; |
| 1322 | /* ${foo[@]} */ |
| 1323 | xp->split = tobool(p[1] == '@'); |
| 1324 | state = XARG; |
| 1325 | } |
| 1326 | } else { |
| 1327 | xp->var = global(sp); |
| 1328 | xp->str = str_val(xp->var); |
| 1329 | /* can't assign things like $! or $1 */ |
| 1330 | if ((stype & 0x17F) == '=' && !*xp->str && |
| 1331 | ctype(*sp, C_VAR1 | C_DIGIT)) |
| 1332 | return (-1); |
| 1333 | state = XSUB; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1336 | c = stype & 0x7F; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1337 | /* test the compiler's code generator */ |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1338 | if (((stype < 0x100) && (ksh_issubop2(c) || |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 1339 | (((stype & 0x80) ? *xp->str == '\0' : xp->str == null) && |
| 1340 | (state != XARG || (ifs0 || xp->split ? |
| 1341 | (xp->u.strv[0] == NULL) : !hasnonempty(xp->u.strv))) ? |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1342 | c == '=' || c == '-' || c == '?' : c == '+'))) || |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1343 | stype == (0x80 | '0') || stype == (0x100 | '#') || |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1344 | stype == (0x100 | 'Q') || (stype & 0x7F) == '/') |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1345 | /* expand word instead of variable value */ |
| 1346 | state = XBASE; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1347 | if (Flag(FNOUNSET) && xp->str == null && !zero_ok && |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1348 | (ksh_issubop2(c) || (state != XBASE && c != '+'))) |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1349 | errorf(Tf_parm, sp); |
| 1350 | *stypep = stype; |
| 1351 | *slenp = slen; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1352 | return (state); |
| 1353 | } |
| 1354 | |
| 1355 | /* |
| 1356 | * Run the command in $(...) and read its output. |
| 1357 | */ |
| 1358 | static int |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1359 | comsub(Expand *xp, const char *cp, int fn) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1360 | { |
| 1361 | Source *s, *sold; |
| 1362 | struct op *t; |
| 1363 | struct shf *shf; |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1364 | bool doalias = false; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1365 | uint8_t old_utfmode = UTFMODE; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1366 | |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1367 | switch (fn) { |
| 1368 | case COMASUB: |
| 1369 | fn = COMSUB; |
| 1370 | if (0) |
| 1371 | /* FALLTHROUGH */ |
| 1372 | case FUNASUB: |
| 1373 | fn = FUNSUB; |
| 1374 | doalias = true; |
| 1375 | } |
| 1376 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1377 | s = pushs(SSTRING, ATEMP); |
| 1378 | s->start = s->str = cp; |
| 1379 | sold = source; |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1380 | t = compile(s, true, doalias); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1381 | afree(s, ATEMP); |
| 1382 | source = sold; |
| 1383 | |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1384 | UTFMODE = old_utfmode; |
| 1385 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1386 | if (t == NULL) |
| 1387 | return (XBASE); |
| 1388 | |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1389 | /* no waitlast() unless specifically enabled later */ |
| 1390 | xp->split = false; |
| 1391 | |
| 1392 | if (t->type == TCOM && |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1393 | *t->args == NULL && *t->vars == NULL && t->ioact != NULL) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1394 | /* $(<file) */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1395 | struct ioword *io = *t->ioact; |
| 1396 | char *name; |
| 1397 | |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1398 | switch (io->ioflag & IOTYPE) { |
| 1399 | case IOREAD: |
| 1400 | shf = shf_open(name = evalstr(io->ioname, DOTILDE), |
| 1401 | O_RDONLY, 0, SHF_MAPHI | SHF_CLEXEC); |
| 1402 | if (shf == NULL) |
| 1403 | warningf(!Flag(FTALKING), Tf_sD_s_sD_s, |
| 1404 | name, Tcant_open, "$(<...) input", |
| 1405 | cstrerror(errno)); |
| 1406 | break; |
| 1407 | case IOHERE: |
| 1408 | if (!herein(io, &name)) { |
| 1409 | xp->str = name; |
| 1410 | /* as $(…) requires, trim trailing newlines */ |
| 1411 | name += strlen(name); |
| 1412 | while (name > xp->str && name[-1] == '\n') |
| 1413 | --name; |
| 1414 | *name = '\0'; |
| 1415 | return (XSUB); |
| 1416 | } |
| 1417 | shf = NULL; |
| 1418 | break; |
| 1419 | default: |
| 1420 | errorf(Tf_sD_s, T_funny_command, |
| 1421 | snptreef(NULL, 32, Tft_R, io)); |
| 1422 | } |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1423 | } else if (fn == FUNSUB) { |
| 1424 | int ofd1; |
| 1425 | struct temp *tf = NULL; |
| 1426 | |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1427 | /* |
| 1428 | * create a temporary file, open for reading and writing, |
| 1429 | * with an shf open for reading (buffered) but yet unused |
| 1430 | */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1431 | maketemp(ATEMP, TT_FUNSUB, &tf); |
| 1432 | if (!tf->shf) { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1433 | errorf(Tf_temp, |
| 1434 | Tcreate, tf->tffn, cstrerror(errno)); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1435 | } |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1436 | /* extract shf from temporary file, unlink and free it */ |
| 1437 | shf = tf->shf; |
| 1438 | unlink(tf->tffn); |
| 1439 | afree(tf, ATEMP); |
| 1440 | /* save stdout and let it point to the tempfile */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1441 | ofd1 = savefd(1); |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1442 | ksh_dup2(shf_fileno(shf), 1, false); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1443 | /* |
| 1444 | * run tree, with output thrown into the tempfile, |
| 1445 | * in a new function block |
| 1446 | */ |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1447 | valsub(t, NULL); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1448 | subst_exstat = exstat & 0xFF; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1449 | /* rewind the tempfile and restore regular stdout */ |
| 1450 | lseek(shf_fileno(shf), (off_t)0, SEEK_SET); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1451 | restfd(1, ofd1); |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1452 | } else if (fn == VALSUB) { |
| 1453 | xp->str = valsub(t, ATEMP); |
| 1454 | subst_exstat = exstat & 0xFF; |
| 1455 | return (XSUB); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1456 | } else { |
| 1457 | int ofd1, pv[2]; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1458 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1459 | openpipe(pv); |
| 1460 | shf = shf_fdopen(pv[0], SHF_RD, NULL); |
| 1461 | ofd1 = savefd(1); |
| 1462 | if (pv[1] != 1) { |
| 1463 | ksh_dup2(pv[1], 1, false); |
| 1464 | close(pv[1]); |
| 1465 | } |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1466 | execute(t, XXCOM | XPIPEO | XFORK, NULL); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1467 | restfd(1, ofd1); |
| 1468 | startlast(); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1469 | /* waitlast() */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1470 | xp->split = true; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | xp->u.shf = shf; |
| 1474 | return (XCOM); |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * perform #pattern and %pattern substitution in ${} |
| 1479 | */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1480 | static char * |
| 1481 | trimsub(char *str, char *pat, int how) |
| 1482 | { |
| 1483 | char *end = strnul(str); |
| 1484 | char *p, c; |
| 1485 | |
| 1486 | switch (how & 0xFF) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1487 | case '#': |
| 1488 | /* shortest match at beginning */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1489 | for (p = str; p <= end; p += utf_ptradj(p)) { |
| 1490 | c = *p; *p = '\0'; |
| 1491 | if (gmatchx(str, pat, false)) { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1492 | record_match(str); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1493 | *p = c; |
| 1494 | return (p); |
| 1495 | } |
| 1496 | *p = c; |
| 1497 | } |
| 1498 | break; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1499 | case '#'|0x80: |
| 1500 | /* longest match at beginning */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1501 | for (p = end; p >= str; p--) { |
| 1502 | c = *p; *p = '\0'; |
| 1503 | if (gmatchx(str, pat, false)) { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1504 | record_match(str); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1505 | *p = c; |
| 1506 | return (p); |
| 1507 | } |
| 1508 | *p = c; |
| 1509 | } |
| 1510 | break; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1511 | case '%': |
| 1512 | /* shortest match at end */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1513 | p = end; |
| 1514 | while (p >= str) { |
| 1515 | if (gmatchx(p, pat, false)) |
| 1516 | goto trimsub_match; |
| 1517 | if (UTFMODE) { |
| 1518 | char *op = p; |
| 1519 | while ((p-- > str) && ((*p & 0xC0) == 0x80)) |
| 1520 | ; |
| 1521 | if ((p < str) || (p + utf_ptradj(p) != op)) |
| 1522 | p = op - 1; |
| 1523 | } else |
| 1524 | --p; |
| 1525 | } |
| 1526 | break; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1527 | case '%'|0x80: |
| 1528 | /* longest match at end */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1529 | for (p = str; p <= end; p++) |
| 1530 | if (gmatchx(p, pat, false)) { |
| 1531 | trimsub_match: |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1532 | record_match(p); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1533 | strndupx(end, str, p - str, ATEMP); |
| 1534 | return (end); |
| 1535 | } |
| 1536 | break; |
| 1537 | } |
| 1538 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1539 | /* no match, return string */ |
| 1540 | return (str); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | /* |
| 1544 | * glob |
| 1545 | * Name derived from V6's /etc/glob, the program that expanded filenames. |
| 1546 | */ |
| 1547 | |
| 1548 | /* XXX cp not const 'cause slashes are temporarily replaced with NULs... */ |
| 1549 | static void |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1550 | glob(char *cp, XPtrV *wp, bool markdirs) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1551 | { |
| 1552 | int oldsize = XPsize(*wp); |
| 1553 | |
| 1554 | if (glob_str(cp, wp, markdirs) == 0) |
| 1555 | XPput(*wp, debunk(cp, cp, strlen(cp) + 1)); |
| 1556 | else |
| 1557 | qsort(XPptrv(*wp) + oldsize, XPsize(*wp) - oldsize, |
| 1558 | sizeof(void *), xstrcmp); |
| 1559 | } |
| 1560 | |
| 1561 | #define GF_NONE 0 |
| 1562 | #define GF_EXCHECK BIT(0) /* do existence check on file */ |
| 1563 | #define GF_GLOBBED BIT(1) /* some globbing has been done */ |
| 1564 | #define GF_MARKDIR BIT(2) /* add trailing / to directories */ |
| 1565 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1566 | /* |
| 1567 | * Apply file globbing to cp and store the matching files in wp. Returns |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1568 | * the number of matches found. |
| 1569 | */ |
| 1570 | int |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1571 | glob_str(char *cp, XPtrV *wp, bool markdirs) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1572 | { |
| 1573 | int oldsize = XPsize(*wp); |
| 1574 | XString xs; |
| 1575 | char *xp; |
| 1576 | |
| 1577 | Xinit(xs, xp, 256, ATEMP); |
| 1578 | globit(&xs, &xp, cp, wp, markdirs ? GF_MARKDIR : GF_NONE); |
| 1579 | Xfree(xs, xp); |
| 1580 | |
| 1581 | return (XPsize(*wp) - oldsize); |
| 1582 | } |
| 1583 | |
| 1584 | static void |
| 1585 | globit(XString *xs, /* dest string */ |
| 1586 | char **xpp, /* ptr to dest end */ |
| 1587 | char *sp, /* source path */ |
| 1588 | XPtrV *wp, /* output list */ |
| 1589 | int check) /* GF_* flags */ |
| 1590 | { |
| 1591 | char *np; /* next source component */ |
| 1592 | char *xp = *xpp; |
| 1593 | char *se; |
| 1594 | char odirsep; |
| 1595 | |
| 1596 | /* This to allow long expansions to be interrupted */ |
| 1597 | intrcheck(); |
| 1598 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1599 | if (sp == NULL) { |
| 1600 | /* end of source path */ |
| 1601 | /* |
| 1602 | * We only need to check if the file exists if a pattern |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1603 | * is followed by a non-pattern (eg, foo*x/bar; no check |
| 1604 | * is needed for foo* since the match must exist) or if |
| 1605 | * any patterns were expanded and the markdirs option is set. |
| 1606 | * Symlinks make things a bit tricky... |
| 1607 | */ |
| 1608 | if ((check & GF_EXCHECK) || |
| 1609 | ((check & GF_MARKDIR) && (check & GF_GLOBBED))) { |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1610 | #define stat_check() (stat_done ? stat_done : (stat_done = \ |
| 1611 | stat(Xstring(*xs, xp), &statb) < 0 ? -1 : 1)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1612 | struct stat lstatb, statb; |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1613 | /* -1: failed, 1 ok, 0 not yet done */ |
| 1614 | int stat_done = 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1615 | |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1616 | if (mksh_lstat(Xstring(*xs, xp), &lstatb) < 0) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1617 | return; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1618 | /* |
| 1619 | * special case for systems which strip trailing |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1620 | * slashes from regular files (eg, /etc/passwd/). |
| 1621 | * SunOS 4.1.3 does this... |
| 1622 | */ |
| 1623 | if ((check & GF_EXCHECK) && xp > Xstring(*xs, xp) && |
Elliott Hughes | 966dd55 | 2016-12-08 15:56:04 -0800 | [diff] [blame] | 1624 | mksh_cdirsep(xp[-1]) && !S_ISDIR(lstatb.st_mode) && |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1625 | (!S_ISLNK(lstatb.st_mode) || |
| 1626 | stat_check() < 0 || !S_ISDIR(statb.st_mode))) |
| 1627 | return; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1628 | /* |
| 1629 | * Possibly tack on a trailing / if there isn't already |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1630 | * one and if the file is a directory or a symlink to a |
| 1631 | * directory |
| 1632 | */ |
| 1633 | if (((check & GF_MARKDIR) && (check & GF_GLOBBED)) && |
Elliott Hughes | 966dd55 | 2016-12-08 15:56:04 -0800 | [diff] [blame] | 1634 | xp > Xstring(*xs, xp) && !mksh_cdirsep(xp[-1]) && |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1635 | (S_ISDIR(lstatb.st_mode) || |
| 1636 | (S_ISLNK(lstatb.st_mode) && stat_check() > 0 && |
| 1637 | S_ISDIR(statb.st_mode)))) { |
| 1638 | *xp++ = '/'; |
| 1639 | *xp = '\0'; |
| 1640 | } |
| 1641 | } |
| 1642 | strndupx(np, Xstring(*xs, xp), Xlength(*xs, xp), ATEMP); |
| 1643 | XPput(*wp, np); |
| 1644 | return; |
| 1645 | } |
| 1646 | |
| 1647 | if (xp > Xstring(*xs, xp)) |
| 1648 | *xp++ = '/'; |
Elliott Hughes | 966dd55 | 2016-12-08 15:56:04 -0800 | [diff] [blame] | 1649 | while (mksh_cdirsep(*sp)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1650 | Xcheck(*xs, xp); |
| 1651 | *xp++ = *sp++; |
| 1652 | } |
Elliott Hughes | 966dd55 | 2016-12-08 15:56:04 -0800 | [diff] [blame] | 1653 | np = mksh_sdirsep(sp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1654 | if (np != NULL) { |
| 1655 | se = np; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1656 | /* don't assume '/', can be multiple kinds */ |
| 1657 | odirsep = *np; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1658 | *np++ = '\0'; |
| 1659 | } else { |
| 1660 | odirsep = '\0'; /* keep gcc quiet */ |
| 1661 | se = sp + strlen(sp); |
| 1662 | } |
| 1663 | |
| 1664 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1665 | /* |
| 1666 | * Check if sp needs globbing - done to avoid pattern checks for strings |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1667 | * containing MAGIC characters, open [s without the matching close ], |
| 1668 | * etc. (otherwise opendir() will be called which may fail because the |
| 1669 | * directory isn't readable - if no globbing is needed, only execute |
| 1670 | * permission should be required (as per POSIX)). |
| 1671 | */ |
| 1672 | if (!has_globbing(sp, se)) { |
| 1673 | XcheckN(*xs, xp, se - sp + 1); |
| 1674 | debunk(xp, sp, Xnleft(*xs, xp)); |
| 1675 | xp += strlen(xp); |
| 1676 | *xpp = xp; |
| 1677 | globit(xs, xpp, np, wp, check); |
| 1678 | } else { |
| 1679 | DIR *dirp; |
| 1680 | struct dirent *d; |
| 1681 | char *name; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1682 | size_t len, prefix_len; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1683 | |
| 1684 | /* xp = *xpp; copy_non_glob() may have re-alloc'd xs */ |
| 1685 | *xp = '\0'; |
| 1686 | prefix_len = Xlength(*xs, xp); |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1687 | dirp = opendir(prefix_len ? Xstring(*xs, xp) : Tdot); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1688 | if (dirp == NULL) |
| 1689 | goto Nodir; |
| 1690 | while ((d = readdir(dirp)) != NULL) { |
| 1691 | name = d->d_name; |
| 1692 | if (name[0] == '.' && |
| 1693 | (name[1] == 0 || (name[1] == '.' && name[2] == 0))) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1694 | /* always ignore . and .. */ |
| 1695 | continue; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1696 | if ((*name == '.' && *sp != '.') || |
| 1697 | !gmatchx(name, sp, true)) |
| 1698 | continue; |
| 1699 | |
| 1700 | len = strlen(d->d_name) + 1; |
| 1701 | XcheckN(*xs, xp, len); |
| 1702 | memcpy(xp, name, len); |
| 1703 | *xpp = xp + len - 1; |
| 1704 | globit(xs, xpp, np, wp, |
| 1705 | (check & GF_MARKDIR) | GF_GLOBBED |
| 1706 | | (np ? GF_EXCHECK : GF_NONE)); |
| 1707 | xp = Xstring(*xs, xp) + prefix_len; |
| 1708 | } |
| 1709 | closedir(dirp); |
| 1710 | Nodir: |
| 1711 | ; |
| 1712 | } |
| 1713 | |
| 1714 | if (np != NULL) |
| 1715 | *--np = odirsep; |
| 1716 | } |
| 1717 | |
| 1718 | /* remove MAGIC from string */ |
| 1719 | char * |
| 1720 | debunk(char *dp, const char *sp, size_t dlen) |
| 1721 | { |
| 1722 | char *d; |
| 1723 | const char *s; |
| 1724 | |
| 1725 | if ((s = cstrchr(sp, MAGIC))) { |
| 1726 | if (s - sp >= (ssize_t)dlen) |
| 1727 | return (dp); |
| 1728 | memmove(dp, sp, s - sp); |
| 1729 | for (d = dp + (s - sp); *s && (d - dp < (ssize_t)dlen); s++) |
| 1730 | if (!ISMAGIC(*s) || !(*++s & 0x80) || |
| 1731 | !vstrchr("*+?@! ", *s & 0x7f)) |
| 1732 | *d++ = *s; |
| 1733 | else { |
| 1734 | /* extended pattern operators: *+?@! */ |
| 1735 | if ((*s & 0x7f) != ' ') |
| 1736 | *d++ = *s & 0x7f; |
| 1737 | if (d - dp < (ssize_t)dlen) |
| 1738 | *d++ = '('; |
| 1739 | } |
| 1740 | *d = '\0'; |
| 1741 | } else if (dp != sp) |
| 1742 | strlcpy(dp, sp, dlen); |
| 1743 | return (dp); |
| 1744 | } |
| 1745 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1746 | /* |
| 1747 | * Check if p is an unquoted name, possibly followed by a / or :. If so |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1748 | * puts the expanded version in *dcp,dp and returns a pointer in p just |
| 1749 | * past the name, otherwise returns 0. |
| 1750 | */ |
| 1751 | static const char * |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 1752 | maybe_expand_tilde(const char *p, XString *dsp, char **dpp, bool isassign) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1753 | { |
| 1754 | XString ts; |
| 1755 | char *dp = *dpp; |
| 1756 | char *tp; |
| 1757 | const char *r; |
| 1758 | |
| 1759 | Xinit(ts, tp, 16, ATEMP); |
| 1760 | /* : only for DOASNTILDE form */ |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1761 | while (p[0] == CHAR && /* not cdirsep */ p[1] != '/' && |
Elliott Hughes | 966dd55 | 2016-12-08 15:56:04 -0800 | [diff] [blame] | 1762 | (!isassign || p[1] != ':')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1763 | Xcheck(ts, tp); |
| 1764 | *tp++ = p[1]; |
| 1765 | p += 2; |
| 1766 | } |
| 1767 | *tp = '\0'; |
| 1768 | r = (p[0] == EOS || p[0] == CHAR || p[0] == CSUBST) ? |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 1769 | do_tilde(Xstring(ts, tp)) : NULL; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1770 | Xfree(ts, tp); |
| 1771 | if (r) { |
| 1772 | while (*r) { |
| 1773 | Xcheck(*dsp, dp); |
| 1774 | if (ISMAGIC(*r)) |
| 1775 | *dp++ = MAGIC; |
| 1776 | *dp++ = *r++; |
| 1777 | } |
| 1778 | *dpp = dp; |
| 1779 | r = p; |
| 1780 | } |
| 1781 | return (r); |
| 1782 | } |
| 1783 | |
| 1784 | /* |
| 1785 | * tilde expansion |
| 1786 | * |
| 1787 | * based on a version by Arnold Robbins |
| 1788 | */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1789 | char * |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 1790 | do_tilde(char *cp) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1791 | { |
| 1792 | char *dp = null; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1793 | #ifndef MKSH_NOPWNAM |
| 1794 | bool do_simplify = true; |
| 1795 | #endif |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1796 | |
| 1797 | if (cp[0] == '\0') |
| 1798 | dp = str_val(global("HOME")); |
| 1799 | else if (cp[0] == '+' && cp[1] == '\0') |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1800 | dp = str_val(global(TPWD)); |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 1801 | else if (ksh_isdash(cp)) |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1802 | dp = str_val(global(TOLDPWD)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1803 | #ifndef MKSH_NOPWNAM |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1804 | else { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1805 | dp = homedir(cp); |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1806 | do_simplify = false; |
| 1807 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1808 | #endif |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1809 | |
| 1810 | /* if parameters aren't set, don't expand ~ */ |
| 1811 | if (dp == NULL || dp == null) |
| 1812 | return (NULL); |
| 1813 | |
| 1814 | /* simplify parameters as if cwd upon entry */ |
| 1815 | #ifndef MKSH_NOPWNAM |
| 1816 | if (do_simplify) |
| 1817 | #endif |
| 1818 | { |
| 1819 | strdupx(dp, dp, ATEMP); |
| 1820 | simplify_path(dp); |
| 1821 | } |
| 1822 | return (dp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | #ifndef MKSH_NOPWNAM |
| 1826 | /* |
| 1827 | * map userid to user's home directory. |
| 1828 | * note that 4.3's getpw adds more than 6K to the shell, |
| 1829 | * and the YP version probably adds much more. |
| 1830 | * we might consider our own version of getpwnam() to keep the size down. |
| 1831 | */ |
| 1832 | static char * |
| 1833 | homedir(char *name) |
| 1834 | { |
| 1835 | struct tbl *ap; |
| 1836 | |
| 1837 | ap = ktenter(&homedirs, name, hash(name)); |
| 1838 | if (!(ap->flag & ISSET)) { |
| 1839 | struct passwd *pw; |
| 1840 | |
| 1841 | pw = getpwnam(name); |
| 1842 | if (pw == NULL) |
| 1843 | return (NULL); |
| 1844 | strdupx(ap->val.s, pw->pw_dir, APERM); |
| 1845 | ap->flag |= DEFINED|ISSET|ALLOC; |
| 1846 | } |
| 1847 | return (ap->val.s); |
| 1848 | } |
| 1849 | #endif |
| 1850 | |
| 1851 | static void |
| 1852 | alt_expand(XPtrV *wp, char *start, char *exp_start, char *end, int fdo) |
| 1853 | { |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 1854 | unsigned int count = 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1855 | char *brace_start, *brace_end, *comma = NULL; |
| 1856 | char *field_start; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 1857 | char *p = exp_start; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1858 | |
| 1859 | /* search for open brace */ |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 1860 | while ((p = strchr(p, MAGIC)) && p[1] != '{' /*}*/) |
| 1861 | p += 2; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1862 | brace_start = p; |
| 1863 | |
| 1864 | /* find matching close brace, if any */ |
| 1865 | if (p) { |
| 1866 | comma = NULL; |
| 1867 | count = 1; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 1868 | p += 2; |
| 1869 | while (*p && count) { |
| 1870 | if (ISMAGIC(*p++)) { |
| 1871 | if (*p == '{' /*}*/) |
| 1872 | ++count; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1873 | else if (*p == /*{*/ '}') |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1874 | --count; |
| 1875 | else if (*p == ',' && count == 1) |
| 1876 | comma = p; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 1877 | ++p; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | /* no valid expansions... */ |
| 1882 | if (!p || count != 0) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1883 | /* |
| 1884 | * Note that given a{{b,c} we do not expand anything (this is |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1885 | * what AT&T ksh does. This may be changed to do the {b,c} |
| 1886 | * expansion. } |
| 1887 | */ |
| 1888 | if (fdo & DOGLOB) |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1889 | glob(start, wp, tobool(fdo & DOMARKDIRS)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1890 | else |
| 1891 | XPput(*wp, debunk(start, start, end - start)); |
| 1892 | return; |
| 1893 | } |
| 1894 | brace_end = p; |
| 1895 | if (!comma) { |
| 1896 | alt_expand(wp, start, brace_end, end, fdo); |
| 1897 | return; |
| 1898 | } |
| 1899 | |
| 1900 | /* expand expression */ |
| 1901 | field_start = brace_start + 2; |
| 1902 | count = 1; |
| 1903 | for (p = brace_start + 2; p != brace_end; p++) { |
| 1904 | if (ISMAGIC(*p)) { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1905 | if (*++p == '{' /*}*/) |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 1906 | ++count; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1907 | else if ((*p == /*{*/ '}' && --count == 0) || |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1908 | (*p == ',' && count == 1)) { |
| 1909 | char *news; |
| 1910 | int l1, l2, l3; |
| 1911 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1912 | /* |
| 1913 | * addition safe since these operate on |
| 1914 | * one string (separate substrings) |
| 1915 | */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1916 | l1 = brace_start - start; |
| 1917 | l2 = (p - 1) - field_start; |
| 1918 | l3 = end - brace_end; |
| 1919 | news = alloc(l1 + l2 + l3 + 1, ATEMP); |
| 1920 | memcpy(news, start, l1); |
| 1921 | memcpy(news + l1, field_start, l2); |
| 1922 | memcpy(news + l1 + l2, brace_end, l3); |
| 1923 | news[l1 + l2 + l3] = '\0'; |
| 1924 | alt_expand(wp, news, news + l1, |
| 1925 | news + l1 + l2 + l3, fdo); |
| 1926 | field_start = p + 1; |
| 1927 | } |
| 1928 | } |
| 1929 | } |
| 1930 | return; |
| 1931 | } |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1932 | |
| 1933 | /* helper function due to setjmp/longjmp woes */ |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1934 | static char * |
| 1935 | valsub(struct op *t, Area *ap) |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1936 | { |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1937 | char * volatile cp = NULL; |
| 1938 | struct tbl * volatile vp = NULL; |
| 1939 | |
| 1940 | newenv(E_FUNC); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1941 | newblock(); |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1942 | if (ap) |
| 1943 | vp = local("REPLY", false); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1944 | if (!kshsetjmp(e->jbuf)) |
| 1945 | execute(t, XXCOM | XERROK, NULL); |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1946 | if (vp) |
| 1947 | strdupx(cp, str_val(vp), ap); |
| 1948 | quitenv(NULL); |
| 1949 | |
| 1950 | return (cp); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1951 | } |