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