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