Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 1 | /* $OpenBSD: lex.c,v 1.51 2015/09/10 22:48:58 nicm 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 | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 26 | __RCSID("$MirOS: src/bin/mksh/lex.c,v 1.250 2018/10/20 18:34:14 tg Exp $"); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * states while lexing word |
| 30 | */ |
| 31 | #define SBASE 0 /* outside any lexical constructs */ |
| 32 | #define SWORD 1 /* implicit quoting for substitute() */ |
| 33 | #define SLETPAREN 2 /* inside (( )), implicit quoting */ |
| 34 | #define SSQUOTE 3 /* inside '' */ |
| 35 | #define SDQUOTE 4 /* inside "" */ |
| 36 | #define SEQUOTE 5 /* inside $'' */ |
| 37 | #define SBRACE 6 /* inside ${} */ |
| 38 | #define SQBRACE 7 /* inside "${}" */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 39 | #define SBQUOTE 8 /* inside `` */ |
| 40 | #define SASPAREN 9 /* inside $(( )) */ |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 41 | #define SHEREDELIM 10 /* parsing << or <<- delimiter */ |
| 42 | #define SHEREDQUOTE 11 /* parsing " in << or <<- delimiter */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 43 | #define SPATTERN 12 /* parsing *(...|...) pattern (*+?@!) */ |
| 44 | #define SADELIM 13 /* like SBASE, looking for delimiter */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 45 | #define STBRACEKORN 14 /* parsing ${...[#%]...} !FSH */ |
| 46 | #define STBRACEBOURNE 15 /* parsing ${...[#%]...} FSH */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 47 | #define SINVALID 255 /* invalid state */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 48 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 49 | struct sretrace_info { |
| 50 | struct sretrace_info *next; |
| 51 | XString xs; |
| 52 | char *xp; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 55 | /* |
| 56 | * Structure to keep track of the lexing state and the various pieces of info |
| 57 | * needed for each particular state. |
| 58 | */ |
| 59 | typedef struct lex_state { |
| 60 | union { |
| 61 | /* point to the next state block */ |
| 62 | struct lex_state *base; |
| 63 | /* marks start of state output in output string */ |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 64 | size_t start; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 65 | /* SBQUOTE: true if in double quotes: "`...`" */ |
| 66 | /* SEQUOTE: got NUL, ignore rest of string */ |
| 67 | bool abool; |
| 68 | /* SADELIM information */ |
| 69 | struct { |
| 70 | /* character to search for */ |
| 71 | unsigned char delimiter; |
| 72 | /* max. number of delimiters */ |
| 73 | unsigned char num; |
| 74 | } adelim; |
| 75 | } u; |
| 76 | /* count open parentheses */ |
| 77 | short nparen; |
| 78 | /* type of this state */ |
| 79 | uint8_t type; |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 80 | /* extra flags */ |
| 81 | uint8_t ls_flags; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 82 | } Lex_state; |
| 83 | #define ls_base u.base |
| 84 | #define ls_start u.start |
| 85 | #define ls_bool u.abool |
| 86 | #define ls_adelim u.adelim |
| 87 | |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 88 | /* ls_flags */ |
| 89 | #define LS_HEREDOC BIT(0) |
| 90 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 91 | typedef struct { |
| 92 | Lex_state *base; |
| 93 | Lex_state *end; |
| 94 | } State_info; |
| 95 | |
| 96 | static void readhere(struct ioword *); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 97 | static void ungetsc(int); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 98 | static void ungetsc_i(int); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 99 | static int getsc_uu(void); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 100 | static void getsc_line(Source *); |
| 101 | static int getsc_bn(void); |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 102 | static int getsc_i(void); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 103 | static char *get_brace_var(XString *, char *); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 104 | static bool arraysub(char **); |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 105 | static void gethere(void); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 106 | static Lex_state *push_state_i(State_info *, Lex_state *); |
| 107 | static Lex_state *pop_state_i(State_info *, Lex_state *); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 108 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 109 | static int backslash_skip; |
| 110 | static int ignore_backslash_newline; |
| 111 | |
| 112 | /* optimised getsc_bn() */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 113 | #define o_getsc() (*source->str != '\0' && *source->str != '\\' && \ |
| 114 | !backslash_skip ? *source->str++ : getsc_bn()) |
| 115 | /* optimised getsc_uu() */ |
| 116 | #define o_getsc_u() ((*source->str != '\0') ? *source->str++ : getsc_uu()) |
| 117 | |
| 118 | /* retrace helper */ |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 119 | #define o_getsc_r(carg) \ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 120 | int cev = (carg); \ |
| 121 | struct sretrace_info *rp = retrace_info; \ |
| 122 | \ |
| 123 | while (rp) { \ |
| 124 | Xcheck(rp->xs, rp->xp); \ |
| 125 | *rp->xp++ = cev; \ |
| 126 | rp = rp->next; \ |
| 127 | } \ |
| 128 | \ |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 129 | return (cev); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 130 | |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 131 | /* callback */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 132 | static int |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 133 | getsc_i(void) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 134 | { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 135 | o_getsc_r((unsigned int)(unsigned char)o_getsc()); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 136 | } |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 137 | |
| 138 | #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST) |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 139 | #define getsc() getsc_i() |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 140 | #else |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 141 | static int getsc_r(int); |
| 142 | |
| 143 | static int |
| 144 | getsc_r(int c) |
| 145 | { |
| 146 | o_getsc_r(c); |
| 147 | } |
| 148 | |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 149 | #define getsc() getsc_r((unsigned int)(unsigned char)o_getsc()) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 150 | #endif |
| 151 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 152 | #define STATE_BSIZE 8 |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 153 | |
| 154 | #define PUSH_STATE(s) do { \ |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 155 | uint8_t state_flags = statep->ls_flags; \ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 156 | if (++statep == state_info.end) \ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 157 | statep = push_state_i(&state_info, statep); \ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 158 | state = statep->type = (s); \ |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 159 | statep->ls_flags = state_flags; \ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 160 | } while (/* CONSTCOND */ 0) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 161 | |
| 162 | #define POP_STATE() do { \ |
| 163 | if (--statep == state_info.base) \ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 164 | statep = pop_state_i(&state_info, statep); \ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 165 | state = statep->type; \ |
| 166 | } while (/* CONSTCOND */ 0) |
| 167 | |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 168 | #define PUSH_SRETRACE(s) do { \ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 169 | struct sretrace_info *ri; \ |
| 170 | \ |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 171 | PUSH_STATE(s); \ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 172 | statep->ls_start = Xsavepos(ws, wp); \ |
| 173 | ri = alloc(sizeof(struct sretrace_info), ATEMP); \ |
| 174 | Xinit(ri->xs, ri->xp, 64, ATEMP); \ |
| 175 | ri->next = retrace_info; \ |
| 176 | retrace_info = ri; \ |
| 177 | } while (/* CONSTCOND */ 0) |
| 178 | |
| 179 | #define POP_SRETRACE() do { \ |
| 180 | wp = Xrestpos(ws, wp, statep->ls_start); \ |
| 181 | *retrace_info->xp = '\0'; \ |
| 182 | sp = Xstring(retrace_info->xs, retrace_info->xp); \ |
| 183 | dp = (void *)retrace_info; \ |
| 184 | retrace_info = retrace_info->next; \ |
| 185 | afree(dp, ATEMP); \ |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 186 | POP_STATE(); \ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 187 | } while (/* CONSTCOND */ 0) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Lexical analyser |
| 191 | * |
| 192 | * tokens are not regular expressions, they are LL(1). |
| 193 | * for example, "${var:-${PWD}}", and "$(size $(whence ksh))". |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 194 | * hence the state stack. Note "$(...)" are now parsed recursively. |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 195 | */ |
| 196 | |
| 197 | int |
| 198 | yylex(int cf) |
| 199 | { |
| 200 | Lex_state states[STATE_BSIZE], *statep, *s2, *base; |
| 201 | State_info state_info; |
| 202 | int c, c2, state; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 203 | size_t cz; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 204 | XString ws; /* expandable output word */ |
| 205 | char *wp; /* output word pointer */ |
| 206 | char *sp, *dp; |
| 207 | |
| 208 | Again: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 209 | states[0].type = SINVALID; |
| 210 | states[0].ls_base = NULL; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 211 | statep = &states[1]; |
| 212 | state_info.base = states; |
| 213 | state_info.end = &state_info.base[STATE_BSIZE]; |
| 214 | |
| 215 | Xinit(ws, wp, 64, ATEMP); |
| 216 | |
| 217 | backslash_skip = 0; |
| 218 | ignore_backslash_newline = 0; |
| 219 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 220 | if (cf & ONEWORD) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 221 | state = SWORD; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 222 | else if (cf & LETEXPR) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 223 | /* enclose arguments in (double) quotes */ |
| 224 | *wp++ = OQUOTE; |
| 225 | state = SLETPAREN; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 226 | statep->nparen = 0; |
| 227 | } else { |
| 228 | /* normal lexing */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 229 | state = (cf & HEREDELIM) ? SHEREDELIM : SBASE; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 230 | do { |
| 231 | c = getsc(); |
| 232 | } while (ctype(c, C_BLANK)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 233 | if (c == '#') { |
| 234 | ignore_backslash_newline++; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 235 | do { |
| 236 | c = getsc(); |
| 237 | } while (!ctype(c, C_NUL | C_LF)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 238 | ignore_backslash_newline--; |
| 239 | } |
| 240 | ungetsc(c); |
| 241 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 242 | if (source->flags & SF_ALIAS) { |
| 243 | /* trailing ' ' in alias definition */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 244 | source->flags &= ~SF_ALIAS; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 245 | /* POSIX: trailing space only counts if parsing simple cmd */ |
| 246 | if (!Flag(FPOSIX) || (cf & CMDWORD)) |
| 247 | cf |= ALIAS; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 250 | /* Initial state: one of SWORD SLETPAREN SHEREDELIM SBASE */ |
| 251 | statep->type = state; |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 252 | statep->ls_flags = (cf & HEREDOC) ? LS_HEREDOC : 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 253 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 254 | /* collect non-special or quoted characters to form word */ |
| 255 | while (!((c = getsc()) == 0 || |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 256 | ((state == SBASE || state == SHEREDELIM) && ctype(c, C_LEX1)))) { |
| 257 | if (state == SBASE && |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 258 | subshell_nesting_type == ORD(/*{*/ '}') && |
| 259 | (unsigned int)c == ORD(/*{*/ '}')) |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 260 | /* possibly end ${ :;} */ |
| 261 | break; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 262 | Xcheck(ws, wp); |
| 263 | switch (state) { |
| 264 | case SADELIM: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 265 | if ((unsigned int)c == ORD('(')) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 266 | statep->nparen++; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 267 | else if ((unsigned int)c == ORD(')')) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 268 | statep->nparen--; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 269 | else if (statep->nparen == 0 && |
| 270 | ((unsigned int)c == ORD(/*{*/ '}') || |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 271 | c == (int)statep->ls_adelim.delimiter)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 272 | *wp++ = ADELIM; |
| 273 | *wp++ = c; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 274 | if ((unsigned int)c == ORD(/*{*/ '}') || |
| 275 | --statep->ls_adelim.num == 0) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 276 | POP_STATE(); |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 277 | if ((unsigned int)c == ORD(/*{*/ '}')) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 278 | POP_STATE(); |
| 279 | break; |
| 280 | } |
| 281 | /* FALLTHROUGH */ |
| 282 | case SBASE: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 283 | if ((unsigned int)c == ORD('[') && (cf & CMDASN)) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 284 | /* temporary */ |
| 285 | *wp = EOS; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 286 | if (is_wdvarname(Xstring(ws, wp), false)) { |
| 287 | char *p, *tmp; |
| 288 | |
| 289 | if (arraysub(&tmp)) { |
| 290 | *wp++ = CHAR; |
| 291 | *wp++ = c; |
| 292 | for (p = tmp; *p; ) { |
| 293 | Xcheck(ws, wp); |
| 294 | *wp++ = CHAR; |
| 295 | *wp++ = *p++; |
| 296 | } |
| 297 | afree(tmp, ATEMP); |
| 298 | break; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | *wp++ = CHAR; |
| 302 | *wp++ = c; |
| 303 | break; |
| 304 | } |
| 305 | /* FALLTHROUGH */ |
| 306 | Sbase1: /* includes *(...|...) pattern (*+?@!) */ |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 307 | if (ctype(c, C_PATMO)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 308 | c2 = getsc(); |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 309 | if ((unsigned int)c2 == ORD('(' /*)*/)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 310 | *wp++ = OPAT; |
| 311 | *wp++ = c; |
| 312 | PUSH_STATE(SPATTERN); |
| 313 | break; |
| 314 | } |
| 315 | ungetsc(c2); |
| 316 | } |
| 317 | /* FALLTHROUGH */ |
| 318 | Sbase2: /* doesn't include *(...|...) pattern (*+?@!) */ |
| 319 | switch (c) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 320 | case ORD('\\'): |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 321 | getsc_qchar: |
| 322 | if ((c = getsc())) { |
| 323 | /* trailing \ is lost */ |
| 324 | *wp++ = QCHAR; |
| 325 | *wp++ = c; |
| 326 | } |
| 327 | break; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 328 | case ORD('\''): |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 329 | open_ssquote_unless_heredoc: |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 330 | if ((statep->ls_flags & LS_HEREDOC)) |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 331 | goto store_char; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 332 | *wp++ = OQUOTE; |
| 333 | ignore_backslash_newline++; |
| 334 | PUSH_STATE(SSQUOTE); |
| 335 | break; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 336 | case ORD('"'): |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 337 | open_sdquote: |
| 338 | *wp++ = OQUOTE; |
| 339 | PUSH_STATE(SDQUOTE); |
| 340 | break; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 341 | case ORD('$'): |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 342 | /* |
| 343 | * processing of dollar sign belongs into |
| 344 | * Subst, except for those which can open |
| 345 | * a string: $'…' and $"…" |
| 346 | */ |
| 347 | subst_dollar_ex: |
| 348 | c = getsc(); |
| 349 | switch (c) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 350 | case ORD('"'): |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 351 | goto open_sdquote; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 352 | case ORD('\''): |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 353 | goto open_sequote; |
| 354 | default: |
| 355 | goto SubstS; |
| 356 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 357 | default: |
| 358 | goto Subst; |
| 359 | } |
| 360 | break; |
| 361 | |
| 362 | Subst: |
| 363 | switch (c) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 364 | case ORD('\\'): |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 365 | c = getsc(); |
| 366 | switch (c) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 367 | case ORD('"'): |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 368 | if ((statep->ls_flags & LS_HEREDOC)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 369 | goto heredocquote; |
| 370 | /* FALLTHROUGH */ |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 371 | case ORD('\\'): |
| 372 | case ORD('$'): |
| 373 | case ORD('`'): |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 374 | store_qchar: |
| 375 | *wp++ = QCHAR; |
| 376 | *wp++ = c; |
| 377 | break; |
| 378 | default: |
| 379 | heredocquote: |
| 380 | Xcheck(ws, wp); |
| 381 | if (c) { |
| 382 | /* trailing \ is lost */ |
| 383 | *wp++ = CHAR; |
| 384 | *wp++ = '\\'; |
| 385 | *wp++ = CHAR; |
| 386 | *wp++ = c; |
| 387 | } |
| 388 | break; |
| 389 | } |
| 390 | break; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 391 | case ORD('$'): |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 392 | c = getsc(); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 393 | SubstS: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 394 | if ((unsigned int)c == ORD('(' /*)*/)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 395 | c = getsc(); |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 396 | if ((unsigned int)c == ORD('(' /*)*/)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 397 | *wp++ = EXPRSUB; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 398 | PUSH_SRETRACE(SASPAREN); |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 399 | /* unneeded? */ |
| 400 | /*statep->ls_flags &= ~LS_HEREDOC;*/ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 401 | statep->nparen = 2; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 402 | *retrace_info->xp++ = '('; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 403 | } else { |
| 404 | ungetsc(c); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 405 | subst_command: |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 406 | c = COMSUB; |
| 407 | subst_command2: |
| 408 | sp = yyrecursive(c); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 409 | cz = strlen(sp) + 1; |
| 410 | XcheckN(ws, wp, cz); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 411 | *wp++ = c; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 412 | memcpy(wp, sp, cz); |
| 413 | wp += cz; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 414 | } |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 415 | } else if ((unsigned int)c == ORD('{' /*}*/)) { |
| 416 | if ((unsigned int)(c = getsc()) == ORD('|')) { |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 417 | /* |
| 418 | * non-subenvironment |
| 419 | * value substitution |
| 420 | */ |
| 421 | c = VALSUB; |
| 422 | goto subst_command2; |
| 423 | } else if (ctype(c, C_IFSWS)) { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 424 | /* |
| 425 | * non-subenvironment |
| 426 | * "command" substitution |
| 427 | */ |
| 428 | c = FUNSUB; |
| 429 | goto subst_command2; |
| 430 | } |
| 431 | ungetsc(c); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 432 | *wp++ = OSUBST; |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 433 | *wp++ = '{' /*}*/; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 434 | wp = get_brace_var(&ws, wp); |
| 435 | c = getsc(); |
| 436 | /* allow :# and :% (ksh88 compat) */ |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 437 | if ((unsigned int)c == ORD(':')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 438 | *wp++ = CHAR; |
| 439 | *wp++ = c; |
| 440 | c = getsc(); |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 441 | if ((unsigned int)c == ORD(':')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 442 | *wp++ = CHAR; |
| 443 | *wp++ = '0'; |
| 444 | *wp++ = ADELIM; |
| 445 | *wp++ = ':'; |
| 446 | PUSH_STATE(SBRACE); |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 447 | /* perhaps unneeded? */ |
| 448 | statep->ls_flags &= ~LS_HEREDOC; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 449 | PUSH_STATE(SADELIM); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 450 | statep->ls_adelim.delimiter = ':'; |
| 451 | statep->ls_adelim.num = 1; |
| 452 | statep->nparen = 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 453 | break; |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 454 | } else if (ctype(c, C_DIGIT | C_DOLAR | C_SPC) || |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 455 | /*XXX what else? */ |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 456 | c == '(' /*)*/) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 457 | /* substring subst. */ |
| 458 | if (c != ' ') { |
| 459 | *wp++ = CHAR; |
| 460 | *wp++ = ' '; |
| 461 | } |
| 462 | ungetsc(c); |
| 463 | PUSH_STATE(SBRACE); |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 464 | /* perhaps unneeded? */ |
| 465 | statep->ls_flags &= ~LS_HEREDOC; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 466 | PUSH_STATE(SADELIM); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 467 | statep->ls_adelim.delimiter = ':'; |
| 468 | statep->ls_adelim.num = 2; |
| 469 | statep->nparen = 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 470 | break; |
| 471 | } |
| 472 | } else if (c == '/') { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 473 | c2 = ADELIM; |
| 474 | parse_adelim_slash: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 475 | *wp++ = CHAR; |
| 476 | *wp++ = c; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 477 | if ((unsigned int)(c = getsc()) == ORD('/')) { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 478 | *wp++ = c2; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 479 | *wp++ = c; |
| 480 | } else |
| 481 | ungetsc(c); |
| 482 | PUSH_STATE(SBRACE); |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 483 | /* perhaps unneeded? */ |
| 484 | statep->ls_flags &= ~LS_HEREDOC; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 485 | PUSH_STATE(SADELIM); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 486 | statep->ls_adelim.delimiter = '/'; |
| 487 | statep->ls_adelim.num = 1; |
| 488 | statep->nparen = 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 489 | break; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 490 | } else if (c == '@') { |
| 491 | c2 = getsc(); |
| 492 | ungetsc(c2); |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 493 | if ((unsigned int)c2 == ORD('/')) { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 494 | c2 = CHAR; |
| 495 | goto parse_adelim_slash; |
| 496 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 497 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 498 | /* |
| 499 | * If this is a trim operation, |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 500 | * treat (,|,) specially in STBRACE. |
| 501 | */ |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 502 | if (ctype(c, C_SUB2)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 503 | ungetsc(c); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 504 | if (Flag(FSH)) |
| 505 | PUSH_STATE(STBRACEBOURNE); |
| 506 | else |
| 507 | PUSH_STATE(STBRACEKORN); |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 508 | /* single-quotes-in-heredoc-trim */ |
| 509 | statep->ls_flags &= ~LS_HEREDOC; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 510 | } else { |
| 511 | ungetsc(c); |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 512 | if (state == SDQUOTE || |
| 513 | state == SQBRACE) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 514 | PUSH_STATE(SQBRACE); |
| 515 | else |
| 516 | PUSH_STATE(SBRACE); |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 517 | /* here no LS_HEREDOC removal */ |
| 518 | /* single-quotes-in-heredoc-braces */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 519 | } |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 520 | } else if (ctype(c, C_ALPHX)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 521 | *wp++ = OSUBST; |
| 522 | *wp++ = 'X'; |
| 523 | do { |
| 524 | Xcheck(ws, wp); |
| 525 | *wp++ = c; |
| 526 | c = getsc(); |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 527 | } while (ctype(c, C_ALNUX)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 528 | *wp++ = '\0'; |
| 529 | *wp++ = CSUBST; |
| 530 | *wp++ = 'X'; |
| 531 | ungetsc(c); |
| 532 | } else if (ctype(c, C_VAR1 | C_DIGIT)) { |
| 533 | Xcheck(ws, wp); |
| 534 | *wp++ = OSUBST; |
| 535 | *wp++ = 'X'; |
| 536 | *wp++ = c; |
| 537 | *wp++ = '\0'; |
| 538 | *wp++ = CSUBST; |
| 539 | *wp++ = 'X'; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 540 | } else { |
| 541 | *wp++ = CHAR; |
| 542 | *wp++ = '$'; |
| 543 | ungetsc(c); |
| 544 | } |
| 545 | break; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 546 | case ORD('`'): |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 547 | subst_gravis: |
| 548 | PUSH_STATE(SBQUOTE); |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 549 | *wp++ = COMASUB; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 550 | /* |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 551 | * We need to know whether we are within double |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 552 | * quotes in order to translate \" to " within |
| 553 | * "…`…\"…`…" because, unlike for COMSUBs, the |
| 554 | * outer double quoteing changes the backslash |
| 555 | * meaning for the inside. For more details: |
| 556 | * http://austingroupbugs.net/view.php?id=1015 |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 557 | */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 558 | statep->ls_bool = false; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 559 | s2 = statep; |
| 560 | base = state_info.base; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 561 | while (/* CONSTCOND */ 1) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 562 | for (; s2 != base; s2--) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 563 | if (s2->type == SDQUOTE) { |
| 564 | statep->ls_bool = true; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 565 | break; |
| 566 | } |
| 567 | } |
| 568 | if (s2 != base) |
| 569 | break; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 570 | if (!(s2 = s2->ls_base)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 571 | break; |
| 572 | base = s2-- - STATE_BSIZE; |
| 573 | } |
| 574 | break; |
| 575 | case QCHAR: |
| 576 | if (cf & LQCHAR) { |
| 577 | *wp++ = QCHAR; |
| 578 | *wp++ = getsc(); |
| 579 | break; |
| 580 | } |
| 581 | /* FALLTHROUGH */ |
| 582 | default: |
| 583 | store_char: |
| 584 | *wp++ = CHAR; |
| 585 | *wp++ = c; |
| 586 | } |
| 587 | break; |
| 588 | |
| 589 | case SEQUOTE: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 590 | if ((unsigned int)c == ORD('\'')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 591 | POP_STATE(); |
| 592 | *wp++ = CQUOTE; |
| 593 | ignore_backslash_newline--; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 594 | } else if ((unsigned int)c == ORD('\\')) { |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 595 | if ((c2 = unbksl(true, getsc_i, ungetsc)) == -1) |
| 596 | c2 = getsc(); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 597 | if (c2 == 0) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 598 | statep->ls_bool = true; |
| 599 | if (!statep->ls_bool) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 600 | char ts[4]; |
| 601 | |
| 602 | if ((unsigned int)c2 < 0x100) { |
| 603 | *wp++ = QCHAR; |
| 604 | *wp++ = c2; |
| 605 | } else { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 606 | cz = utf_wctomb(ts, c2 - 0x100); |
| 607 | ts[cz] = 0; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 608 | cz = 0; |
| 609 | do { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 610 | *wp++ = QCHAR; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 611 | *wp++ = ts[cz]; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 612 | } while (ts[++cz]); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 615 | } else if (!statep->ls_bool) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 616 | *wp++ = QCHAR; |
| 617 | *wp++ = c; |
| 618 | } |
| 619 | break; |
| 620 | |
| 621 | case SSQUOTE: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 622 | if ((unsigned int)c == ORD('\'')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 623 | POP_STATE(); |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 624 | if ((statep->ls_flags & LS_HEREDOC) || |
| 625 | state == SQBRACE) |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 626 | goto store_char; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 627 | *wp++ = CQUOTE; |
| 628 | ignore_backslash_newline--; |
| 629 | } else { |
| 630 | *wp++ = QCHAR; |
| 631 | *wp++ = c; |
| 632 | } |
| 633 | break; |
| 634 | |
| 635 | case SDQUOTE: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 636 | if ((unsigned int)c == ORD('"')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 637 | POP_STATE(); |
| 638 | *wp++ = CQUOTE; |
| 639 | } else |
| 640 | goto Subst; |
| 641 | break; |
| 642 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 643 | /* $(( ... )) */ |
| 644 | case SASPAREN: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 645 | if ((unsigned int)c == ORD('(')) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 646 | statep->nparen++; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 647 | else if ((unsigned int)c == ORD(')')) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 648 | statep->nparen--; |
| 649 | if (statep->nparen == 1) { |
| 650 | /* end of EXPRSUB */ |
| 651 | POP_SRETRACE(); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 652 | |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 653 | if ((unsigned int)(c2 = getsc()) == ORD(/*(*/ ')')) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 654 | cz = strlen(sp) - 2; |
| 655 | XcheckN(ws, wp, cz); |
| 656 | memcpy(wp, sp + 1, cz); |
| 657 | wp += cz; |
| 658 | afree(sp, ATEMP); |
| 659 | *wp++ = '\0'; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 660 | break; |
| 661 | } else { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 662 | Source *s; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 663 | |
| 664 | ungetsc(c2); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 665 | /* |
| 666 | * mismatched parenthesis - |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 667 | * assume we were really |
| 668 | * parsing a $(...) expression |
| 669 | */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 670 | --wp; |
| 671 | s = pushs(SREREAD, |
| 672 | source->areap); |
| 673 | s->start = s->str = |
| 674 | s->u.freeme = sp; |
| 675 | s->next = source; |
| 676 | source = s; |
| 677 | goto subst_command; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 681 | /* reuse existing state machine */ |
| 682 | goto Sbase2; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 683 | |
| 684 | case SQBRACE: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 685 | if ((unsigned int)c == ORD('\\')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 686 | /* |
| 687 | * perform POSIX "quote removal" if the back- |
| 688 | * slash is "special", i.e. same cases as the |
| 689 | * {case '\\':} in Subst: plus closing brace; |
| 690 | * in mksh code "quote removal" on '\c' means |
| 691 | * write QCHAR+c, otherwise CHAR+\+CHAR+c are |
| 692 | * emitted (in heredocquote:) |
| 693 | */ |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 694 | if ((unsigned int)(c = getsc()) == ORD('"') || |
| 695 | (unsigned int)c == ORD('\\') || |
| 696 | ctype(c, C_DOLAR | C_GRAVE) || |
| 697 | (unsigned int)c == ORD(/*{*/ '}')) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 698 | goto store_qchar; |
| 699 | goto heredocquote; |
| 700 | } |
| 701 | goto common_SQBRACE; |
| 702 | |
| 703 | case SBRACE: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 704 | if ((unsigned int)c == ORD('\'')) |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 705 | goto open_ssquote_unless_heredoc; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 706 | else if ((unsigned int)c == ORD('\\')) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 707 | goto getsc_qchar; |
| 708 | common_SQBRACE: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 709 | if ((unsigned int)c == ORD('"')) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 710 | goto open_sdquote; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 711 | else if ((unsigned int)c == ORD('$')) |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 712 | goto subst_dollar_ex; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 713 | else if ((unsigned int)c == ORD('`')) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 714 | goto subst_gravis; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 715 | else if ((unsigned int)c != ORD(/*{*/ '}')) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 716 | goto store_char; |
| 717 | POP_STATE(); |
| 718 | *wp++ = CSUBST; |
| 719 | *wp++ = /*{*/ '}'; |
| 720 | break; |
| 721 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 722 | /* Same as SBASE, except (,|,) treated specially */ |
| 723 | case STBRACEKORN: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 724 | if ((unsigned int)c == ORD('|')) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 725 | *wp++ = SPAT; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 726 | else if ((unsigned int)c == ORD('(')) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 727 | *wp++ = OPAT; |
| 728 | /* simile for @ */ |
| 729 | *wp++ = ' '; |
| 730 | PUSH_STATE(SPATTERN); |
| 731 | } else /* FALLTHROUGH */ |
| 732 | case STBRACEBOURNE: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 733 | if ((unsigned int)c == ORD(/*{*/ '}')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 734 | POP_STATE(); |
| 735 | *wp++ = CSUBST; |
| 736 | *wp++ = /*{*/ '}'; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 737 | } else |
| 738 | goto Sbase1; |
| 739 | break; |
| 740 | |
| 741 | case SBQUOTE: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 742 | if ((unsigned int)c == ORD('`')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 743 | *wp++ = 0; |
| 744 | POP_STATE(); |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 745 | } else if ((unsigned int)c == ORD('\\')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 746 | switch (c = getsc()) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 747 | case 0: |
| 748 | /* trailing \ is lost */ |
| 749 | break; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 750 | case ORD('$'): |
| 751 | case ORD('`'): |
| 752 | case ORD('\\'): |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 753 | *wp++ = c; |
| 754 | break; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 755 | case ORD('"'): |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 756 | if (statep->ls_bool) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 757 | *wp++ = c; |
| 758 | break; |
| 759 | } |
| 760 | /* FALLTHROUGH */ |
| 761 | default: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 762 | *wp++ = '\\'; |
| 763 | *wp++ = c; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 764 | break; |
| 765 | } |
| 766 | } else |
| 767 | *wp++ = c; |
| 768 | break; |
| 769 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 770 | /* ONEWORD */ |
| 771 | case SWORD: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 772 | goto Subst; |
| 773 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 774 | /* LETEXPR: (( ... )) */ |
| 775 | case SLETPAREN: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 776 | if ((unsigned int)c == ORD(/*(*/ ')')) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 777 | if (statep->nparen > 0) |
| 778 | --statep->nparen; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 779 | else if ((unsigned int)(c2 = getsc()) == ORD(/*(*/ ')')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 780 | c = 0; |
| 781 | *wp++ = CQUOTE; |
| 782 | goto Done; |
| 783 | } else { |
| 784 | Source *s; |
| 785 | |
| 786 | ungetsc(c2); |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 787 | ungetsc(c); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 788 | /* |
| 789 | * mismatched parenthesis - |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 790 | * assume we were really |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 791 | * parsing a (...) expression |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 792 | */ |
| 793 | *wp = EOS; |
| 794 | sp = Xstring(ws, wp); |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 795 | dp = wdstrip(sp + 1, WDS_TPUTS); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 796 | s = pushs(SREREAD, source->areap); |
| 797 | s->start = s->str = s->u.freeme = dp; |
| 798 | s->next = source; |
| 799 | source = s; |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 800 | ungetsc('(' /*)*/); |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 801 | return (ORD('(' /*)*/)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 802 | } |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 803 | } else if ((unsigned int)c == ORD('(')) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 804 | /* |
| 805 | * parentheses inside quotes and |
| 806 | * backslashes are lost, but AT&T ksh |
| 807 | * doesn't count them either |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 808 | */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 809 | ++statep->nparen; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 810 | goto Sbase2; |
| 811 | |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 812 | /* << or <<- delimiter */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 813 | case SHEREDELIM: |
| 814 | /* |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 815 | * here delimiters need a special case since |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 816 | * $ and `...` are not to be treated specially |
| 817 | */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 818 | switch (c) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 819 | case ORD('\\'): |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 820 | if ((c = getsc())) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 821 | /* trailing \ is lost */ |
| 822 | *wp++ = QCHAR; |
| 823 | *wp++ = c; |
| 824 | } |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 825 | break; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 826 | case ORD('\''): |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 827 | goto open_ssquote_unless_heredoc; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 828 | case ORD('$'): |
| 829 | if ((unsigned int)(c2 = getsc()) == ORD('\'')) { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 830 | open_sequote: |
| 831 | *wp++ = OQUOTE; |
| 832 | ignore_backslash_newline++; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 833 | PUSH_STATE(SEQUOTE); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 834 | statep->ls_bool = false; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 835 | break; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 836 | } else if ((unsigned int)c2 == ORD('"')) { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 837 | /* FALLTHROUGH */ |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 838 | case ORD('"'): |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 839 | PUSH_SRETRACE(SHEREDQUOTE); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 840 | break; |
| 841 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 842 | ungetsc(c2); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 843 | /* FALLTHROUGH */ |
| 844 | default: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 845 | *wp++ = CHAR; |
| 846 | *wp++ = c; |
| 847 | } |
| 848 | break; |
| 849 | |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 850 | /* " in << or <<- delimiter */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 851 | case SHEREDQUOTE: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 852 | if ((unsigned int)c != ORD('"')) |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 853 | goto Subst; |
| 854 | POP_SRETRACE(); |
| 855 | dp = strnul(sp) - 1; |
| 856 | /* remove the trailing double quote */ |
| 857 | *dp = '\0'; |
| 858 | /* store the quoted string */ |
| 859 | *wp++ = OQUOTE; |
Elliott Hughes | 5001206 | 2015-03-10 22:22:24 -0700 | [diff] [blame] | 860 | XcheckN(ws, wp, (dp - sp) * 2); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 861 | dp = sp; |
| 862 | while ((c = *dp++)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 863 | if (c == '\\') { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 864 | switch ((c = *dp++)) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 865 | case ORD('\\'): |
| 866 | case ORD('"'): |
| 867 | case ORD('$'): |
| 868 | case ORD('`'): |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 869 | break; |
| 870 | default: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 871 | *wp++ = CHAR; |
| 872 | *wp++ = '\\'; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 873 | break; |
| 874 | } |
| 875 | } |
| 876 | *wp++ = CHAR; |
| 877 | *wp++ = c; |
| 878 | } |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 879 | afree(sp, ATEMP); |
| 880 | *wp++ = CQUOTE; |
| 881 | state = statep->type = SHEREDELIM; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 882 | break; |
| 883 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 884 | /* in *(...|...) pattern (*+?@!) */ |
| 885 | case SPATTERN: |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 886 | if ((unsigned int)c == ORD(/*(*/ ')')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 887 | *wp++ = CPAT; |
| 888 | POP_STATE(); |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 889 | } else if ((unsigned int)c == ORD('|')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 890 | *wp++ = SPAT; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 891 | } else if ((unsigned int)c == ORD('(')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 892 | *wp++ = OPAT; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 893 | /* simile for @ */ |
| 894 | *wp++ = ' '; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 895 | PUSH_STATE(SPATTERN); |
| 896 | } else |
| 897 | goto Sbase1; |
| 898 | break; |
| 899 | } |
| 900 | } |
| 901 | Done: |
| 902 | Xcheck(ws, wp); |
| 903 | if (statep != &states[1]) |
| 904 | /* XXX figure out what is missing */ |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 905 | yyerror("no closing quote"); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 906 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 907 | /* This done to avoid tests for SHEREDELIM wherever SBASE tested */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 908 | if (state == SHEREDELIM) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 909 | state = SBASE; |
| 910 | |
| 911 | dp = Xstring(ws, wp); |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 912 | if (state == SBASE && ( |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 913 | (c == '&' && !Flag(FSH) && !Flag(FPOSIX)) || |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 914 | ctype(c, C_ANGLE)) && ((c2 = Xlength(ws, wp)) == 0 || |
| 915 | (c2 == 2 && dp[0] == CHAR && ctype(dp[1], C_DIGIT)))) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 916 | struct ioword *iop = alloc(sizeof(struct ioword), ATEMP); |
| 917 | |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 918 | iop->unit = c2 == 2 ? ksh_numdig(dp[1]) : c == '<' ? 0 : 1; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 919 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 920 | if (c == '&') { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 921 | if ((unsigned int)(c2 = getsc()) != ORD('>')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 922 | ungetsc(c2); |
| 923 | goto no_iop; |
| 924 | } |
| 925 | c = c2; |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 926 | iop->ioflag = IOBASH; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 927 | } else |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 928 | iop->ioflag = 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 929 | |
| 930 | c2 = getsc(); |
| 931 | /* <<, >>, <> are ok, >< is not */ |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 932 | if (c == c2 || ((unsigned int)c == ORD('<') && |
| 933 | (unsigned int)c2 == ORD('>'))) { |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 934 | iop->ioflag |= c == c2 ? |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 935 | ((unsigned int)c == ORD('>') ? IOCAT : IOHERE) : IORDWR; |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 936 | if (iop->ioflag == IOHERE) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 937 | if ((unsigned int)(c2 = getsc()) == ORD('-')) |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 938 | iop->ioflag |= IOSKIP; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 939 | else if ((unsigned int)c2 == ORD('<')) |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 940 | iop->ioflag |= IOHERESTR; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 941 | else |
| 942 | ungetsc(c2); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 943 | } |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 944 | } else if ((unsigned int)c2 == ORD('&')) |
| 945 | iop->ioflag |= IODUP | ((unsigned int)c == ORD('<') ? IORDUP : 0); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 946 | else { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 947 | iop->ioflag |= (unsigned int)c == ORD('>') ? IOWRITE : IOREAD; |
| 948 | if ((unsigned int)c == ORD('>') && (unsigned int)c2 == ORD('|')) |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 949 | iop->ioflag |= IOCLOB; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 950 | else |
| 951 | ungetsc(c2); |
| 952 | } |
| 953 | |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 954 | iop->ioname = NULL; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 955 | iop->delim = NULL; |
| 956 | iop->heredoc = NULL; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 957 | /* free word */ |
| 958 | Xfree(ws, wp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 959 | yylval.iop = iop; |
| 960 | return (REDIR); |
| 961 | no_iop: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 962 | afree(iop, ATEMP); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | if (wp == dp && state == SBASE) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 966 | /* free word */ |
| 967 | Xfree(ws, wp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 968 | /* no word, process LEX1 character */ |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 969 | if (((unsigned int)c == ORD('|')) || |
| 970 | ((unsigned int)c == ORD('&')) || |
| 971 | ((unsigned int)c == ORD(';')) || |
| 972 | ((unsigned int)c == ORD('(' /*)*/))) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 973 | if ((c2 = getsc()) == c) |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 974 | c = ((unsigned int)c == ORD(';')) ? BREAK : |
| 975 | ((unsigned int)c == ORD('|')) ? LOGOR : |
| 976 | ((unsigned int)c == ORD('&')) ? LOGAND : |
| 977 | /* (unsigned int)c == ORD('(' )) */ MDPAREN; |
| 978 | else if ((unsigned int)c == ORD('|') && (unsigned int)c2 == ORD('&')) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 979 | c = COPROC; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 980 | else if ((unsigned int)c == ORD(';') && (unsigned int)c2 == ORD('|')) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 981 | c = BRKEV; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 982 | else if ((unsigned int)c == ORD(';') && (unsigned int)c2 == ORD('&')) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 983 | c = BRKFT; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 984 | else |
| 985 | ungetsc(c2); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 986 | #ifndef MKSH_SMALL |
| 987 | if (c == BREAK) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 988 | if ((unsigned int)(c2 = getsc()) == ORD('&')) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 989 | c = BRKEV; |
| 990 | else |
| 991 | ungetsc(c2); |
| 992 | } |
| 993 | #endif |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 994 | } else if ((unsigned int)c == ORD('\n')) { |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 995 | if (cf & HEREDELIM) |
| 996 | ungetsc(c); |
| 997 | else { |
| 998 | gethere(); |
| 999 | if (cf & CONTIN) |
| 1000 | goto Again; |
| 1001 | } |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1002 | } else if (c == '\0' && !(cf & HEREDELIM)) { |
| 1003 | struct ioword **p = heres; |
| 1004 | |
| 1005 | while (p < herep) |
| 1006 | if ((*p)->ioflag & IOHERESTR) |
| 1007 | ++p; |
| 1008 | else |
| 1009 | /* ksh -c 'cat <<EOF' can cause this */ |
| 1010 | yyerror(Tf_heredoc, |
| 1011 | evalstr((*p)->delim, 0)); |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 1012 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1013 | return (c); |
| 1014 | } |
| 1015 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1016 | /* terminate word */ |
| 1017 | *wp++ = EOS; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1018 | yylval.cp = Xclose(ws, wp); |
| 1019 | if (state == SWORD || state == SLETPAREN |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1020 | /* XXX ONEWORD? */) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1021 | return (LWORD); |
| 1022 | |
| 1023 | /* unget terminator */ |
| 1024 | ungetsc(c); |
| 1025 | |
| 1026 | /* |
| 1027 | * note: the alias-vs-function code below depends on several |
| 1028 | * interna: starting from here, source->str is not modified; |
| 1029 | * the way getsc() and ungetsc() operate; etc. |
| 1030 | */ |
| 1031 | |
| 1032 | /* copy word to unprefixed string ident */ |
| 1033 | sp = yylval.cp; |
| 1034 | dp = ident; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 1035 | while ((dp - ident) < IDENT && (c = *sp++) == CHAR) |
| 1036 | *dp++ = *sp++; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1037 | if (c != EOS) |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1038 | /* word is not unquoted, or space ran out */ |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 1039 | dp = ident; |
| 1040 | /* make sure the ident array stays NUL padded */ |
| 1041 | memset(dp, 0, (ident + IDENT) - dp + 1); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1042 | |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1043 | if (*ident != '\0' && (cf & (KEYWORD | ALIAS))) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1044 | struct tbl *p; |
| 1045 | uint32_t h = hash(ident); |
| 1046 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1047 | if ((cf & KEYWORD) && (p = ktsearch(&keywords, ident, h)) && |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1048 | (!(cf & ESACONLY) || p->val.i == ESAC || |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1049 | (unsigned int)p->val.i == ORD(/*{*/ '}'))) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1050 | afree(yylval.cp, ATEMP); |
| 1051 | return (p->val.i); |
| 1052 | } |
| 1053 | if ((cf & ALIAS) && (p = ktsearch(&aliases, ident, h)) && |
| 1054 | (p->flag & ISSET)) { |
| 1055 | /* |
| 1056 | * this still points to the same character as the |
| 1057 | * ungetsc'd terminator from above |
| 1058 | */ |
| 1059 | const char *cp = source->str; |
| 1060 | |
| 1061 | /* prefer POSIX but not Korn functions over aliases */ |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1062 | while (ctype(*cp, C_BLANK)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1063 | /* |
| 1064 | * this is like getsc() without skipping |
| 1065 | * over Source boundaries (including not |
| 1066 | * parsing ungetsc'd characters that got |
| 1067 | * pushed into an SREREAD) which is what |
| 1068 | * we want here anyway: find out whether |
| 1069 | * the alias name is followed by a POSIX |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 1070 | * function definition |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1071 | */ |
| 1072 | ++cp; |
| 1073 | /* prefer functions over aliases */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1074 | if (cp[0] != '(' || cp[1] != ')') { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1075 | Source *s = source; |
| 1076 | |
| 1077 | while (s && (s->flags & SF_HASALIAS)) |
| 1078 | if (s->u.tblp == p) |
| 1079 | return (LWORD); |
| 1080 | else |
| 1081 | s = s->next; |
| 1082 | /* push alias expansion */ |
| 1083 | s = pushs(SALIAS, source->areap); |
| 1084 | s->start = s->str = p->val.s; |
| 1085 | s->u.tblp = p; |
| 1086 | s->flags |= SF_HASALIAS; |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1087 | s->line = source->line; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1088 | s->next = source; |
| 1089 | if (source->type == SEOF) { |
| 1090 | /* prevent infinite recursion at EOS */ |
| 1091 | source->u.tblp = p; |
| 1092 | source->flags |= SF_HASALIAS; |
| 1093 | } |
| 1094 | source = s; |
| 1095 | afree(yylval.cp, ATEMP); |
| 1096 | goto Again; |
| 1097 | } |
| 1098 | } |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1099 | } else if (*ident == '\0') { |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 1100 | /* retain typeset et al. even when quoted */ |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1101 | struct tbl *tt = get_builtin((dp = wdstrip(yylval.cp, 0))); |
| 1102 | uint32_t flag = tt ? tt->flag : 0; |
| 1103 | |
| 1104 | if (flag & (DECL_UTIL | DECL_FWDR)) |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 1105 | strlcpy(ident, dp, sizeof(ident)); |
| 1106 | afree(dp, ATEMP); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | return (LWORD); |
| 1110 | } |
| 1111 | |
| 1112 | static void |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 1113 | gethere(void) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1114 | { |
| 1115 | struct ioword **p; |
| 1116 | |
| 1117 | for (p = heres; p < herep; p++) |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 1118 | if (!((*p)->ioflag & IOHERESTR)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1119 | readhere(*p); |
| 1120 | herep = heres; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * read "<<word" text into temp file |
| 1125 | */ |
| 1126 | |
| 1127 | static void |
| 1128 | readhere(struct ioword *iop) |
| 1129 | { |
| 1130 | int c; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1131 | const char *eof, *eofp; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1132 | XString xs; |
| 1133 | char *xp; |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 1134 | size_t xpos; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1135 | |
Elliott Hughes | fc0307d | 2016-02-02 15:26:47 -0800 | [diff] [blame] | 1136 | eof = evalstr(iop->delim, 0); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1137 | |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 1138 | if (!(iop->ioflag & IOEVAL)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1139 | ignore_backslash_newline++; |
| 1140 | |
| 1141 | Xinit(xs, xp, 256, ATEMP); |
| 1142 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1143 | heredoc_read_line: |
| 1144 | /* beginning of line */ |
| 1145 | eofp = eof; |
| 1146 | xpos = Xsavepos(xs, xp); |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 1147 | if (iop->ioflag & IOSKIP) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1148 | /* skip over leading tabs */ |
| 1149 | while ((c = getsc()) == '\t') |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 1150 | ; /* nothing */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1151 | goto heredoc_parse_char; |
| 1152 | } |
| 1153 | heredoc_read_char: |
| 1154 | c = getsc(); |
| 1155 | heredoc_parse_char: |
| 1156 | /* compare with here document marker */ |
| 1157 | if (!*eofp) { |
| 1158 | /* end of here document marker, what to do? */ |
| 1159 | switch (c) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1160 | case ORD(/*(*/ ')'): |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1161 | if (!subshell_nesting_type) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1162 | /*- |
| 1163 | * not allowed outside $(...) or (...) |
| 1164 | * => mismatch |
| 1165 | */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1166 | break; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1167 | /* allow $(...) or (...) to close here */ |
| 1168 | ungetsc(/*(*/ ')'); |
| 1169 | /* FALLTHROUGH */ |
| 1170 | case 0: |
| 1171 | /* |
| 1172 | * Allow EOF here to commands without trailing |
| 1173 | * newlines (mksh -c '...') will work as well. |
| 1174 | */ |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1175 | case ORD('\n'): |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1176 | /* Newline terminates here document marker */ |
| 1177 | goto heredoc_found_terminator; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1178 | } |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 1179 | } else if ((unsigned int)c == ord(*eofp++)) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1180 | /* store; then read and compare next character */ |
| 1181 | goto heredoc_store_and_loop; |
| 1182 | /* nope, mismatch; read until end of line */ |
| 1183 | while (c != '\n') { |
| 1184 | if (!c) |
| 1185 | /* oops, reached EOF */ |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1186 | yyerror(Tf_heredoc, eof); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1187 | /* store character */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1188 | Xcheck(xs, xp); |
| 1189 | Xput(xs, xp, c); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1190 | /* read next character */ |
| 1191 | c = getsc(); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1192 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1193 | /* we read a newline as last character */ |
| 1194 | heredoc_store_and_loop: |
| 1195 | /* store character */ |
| 1196 | Xcheck(xs, xp); |
| 1197 | Xput(xs, xp, c); |
| 1198 | if (c == '\n') |
| 1199 | goto heredoc_read_line; |
| 1200 | goto heredoc_read_char; |
| 1201 | |
| 1202 | heredoc_found_terminator: |
| 1203 | /* jump back to saved beginning of line */ |
| 1204 | xp = Xrestpos(xs, xp, xpos); |
| 1205 | /* terminate, close and store */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1206 | Xput(xs, xp, '\0'); |
| 1207 | iop->heredoc = Xclose(xs, xp); |
| 1208 | |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 1209 | if (!(iop->ioflag & IOEVAL)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1210 | ignore_backslash_newline--; |
| 1211 | } |
| 1212 | |
| 1213 | void |
| 1214 | yyerror(const char *fmt, ...) |
| 1215 | { |
| 1216 | va_list va; |
| 1217 | |
| 1218 | /* pop aliases and re-reads */ |
| 1219 | while (source->type == SALIAS || source->type == SREREAD) |
| 1220 | source = source->next; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1221 | /* zap pending input */ |
| 1222 | source->str = null; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1223 | |
| 1224 | error_prefix(true); |
| 1225 | va_start(va, fmt); |
| 1226 | shf_vfprintf(shl_out, fmt, va); |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1227 | shf_putc('\n', shl_out); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1228 | va_end(va); |
| 1229 | errorfz(); |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * input for yylex with alias expansion |
| 1234 | */ |
| 1235 | |
| 1236 | Source * |
| 1237 | pushs(int type, Area *areap) |
| 1238 | { |
| 1239 | Source *s; |
| 1240 | |
| 1241 | s = alloc(sizeof(Source), areap); |
| 1242 | memset(s, 0, sizeof(Source)); |
| 1243 | s->type = type; |
| 1244 | s->str = null; |
| 1245 | s->areap = areap; |
| 1246 | if (type == SFILE || type == SSTDIN) |
| 1247 | XinitN(s->xs, 256, s->areap); |
| 1248 | return (s); |
| 1249 | } |
| 1250 | |
| 1251 | static int |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1252 | getsc_uu(void) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1253 | { |
| 1254 | Source *s = source; |
| 1255 | int c; |
| 1256 | |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1257 | while ((c = ord(*s->str++)) == 0) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1258 | /* return 0 for EOF by default */ |
| 1259 | s->str = NULL; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1260 | switch (s->type) { |
| 1261 | case SEOF: |
| 1262 | s->str = null; |
| 1263 | return (0); |
| 1264 | |
| 1265 | case SSTDIN: |
| 1266 | case SFILE: |
| 1267 | getsc_line(s); |
| 1268 | break; |
| 1269 | |
| 1270 | case SWSTR: |
| 1271 | break; |
| 1272 | |
| 1273 | case SSTRING: |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1274 | case SSTRINGCMDLINE: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1275 | break; |
| 1276 | |
| 1277 | case SWORDS: |
| 1278 | s->start = s->str = *s->u.strv++; |
| 1279 | s->type = SWORDSEP; |
| 1280 | break; |
| 1281 | |
| 1282 | case SWORDSEP: |
| 1283 | if (*s->u.strv == NULL) { |
| 1284 | s->start = s->str = "\n"; |
| 1285 | s->type = SEOF; |
| 1286 | } else { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1287 | s->start = s->str = T1space; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1288 | s->type = SWORDS; |
| 1289 | } |
| 1290 | break; |
| 1291 | |
| 1292 | case SALIAS: |
| 1293 | if (s->flags & SF_ALIASEND) { |
| 1294 | /* pass on an unused SF_ALIAS flag */ |
| 1295 | source = s->next; |
| 1296 | source->flags |= s->flags & SF_ALIAS; |
| 1297 | s = source; |
| 1298 | } else if (*s->u.tblp->val.s && |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1299 | ctype((c = strnul(s->u.tblp->val.s)[-1]), C_SPACE)) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1300 | /* pop source stack */ |
| 1301 | source = s = s->next; |
| 1302 | /* |
| 1303 | * Note that this alias ended with a |
| 1304 | * space, enabling alias expansion on |
| 1305 | * the following word. |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1306 | */ |
| 1307 | s->flags |= SF_ALIAS; |
| 1308 | } else { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1309 | /* |
| 1310 | * At this point, we need to keep the current |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1311 | * alias in the source list so recursive |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1312 | * aliases can be detected and we also need to |
| 1313 | * return the next character. Do this by |
| 1314 | * temporarily popping the alias to get the |
| 1315 | * next character and then put it back in the |
| 1316 | * source list with the SF_ALIASEND flag set. |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1317 | */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1318 | /* pop source stack */ |
| 1319 | source = s->next; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1320 | source->flags |= s->flags & SF_ALIAS; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1321 | c = getsc_uu(); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1322 | if (c) { |
| 1323 | s->flags |= SF_ALIASEND; |
| 1324 | s->ugbuf[0] = c; s->ugbuf[1] = '\0'; |
| 1325 | s->start = s->str = s->ugbuf; |
| 1326 | s->next = source; |
| 1327 | source = s; |
| 1328 | } else { |
| 1329 | s = source; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1330 | /* avoid reading EOF twice */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1331 | s->str = NULL; |
| 1332 | break; |
| 1333 | } |
| 1334 | } |
| 1335 | continue; |
| 1336 | |
| 1337 | case SREREAD: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1338 | if (s->start != s->ugbuf) |
| 1339 | /* yuck */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1340 | afree(s->u.freeme, ATEMP); |
| 1341 | source = s = s->next; |
| 1342 | continue; |
| 1343 | } |
| 1344 | if (s->str == NULL) { |
| 1345 | s->type = SEOF; |
| 1346 | s->start = s->str = null; |
| 1347 | return ('\0'); |
| 1348 | } |
| 1349 | if (s->flags & SF_ECHO) { |
| 1350 | shf_puts(s->str, shl_out); |
| 1351 | shf_flush(shl_out); |
| 1352 | } |
| 1353 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1354 | return (c); |
| 1355 | } |
| 1356 | |
| 1357 | static void |
| 1358 | getsc_line(Source *s) |
| 1359 | { |
| 1360 | char *xp = Xstring(s->xs, xp), *cp; |
| 1361 | bool interactive = Flag(FTALKING) && s->type == SSTDIN; |
Elliott Hughes | 4708626 | 2019-03-26 12:34:31 -0700 | [diff] [blame] | 1362 | bool have_tty = interactive && (s->flags & SF_TTY) && tty_hasstate; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1363 | |
| 1364 | /* Done here to ensure nothing odd happens when a timeout occurs */ |
| 1365 | XcheckN(s->xs, xp, LINE); |
| 1366 | *xp = '\0'; |
| 1367 | s->start = s->str = xp; |
| 1368 | |
| 1369 | if (have_tty && ksh_tmout) { |
| 1370 | ksh_tmout_state = TMOUT_READING; |
| 1371 | alarm(ksh_tmout); |
| 1372 | } |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1373 | if (interactive) { |
| 1374 | if (cur_prompt == PS1) |
| 1375 | histsave(&s->line, NULL, HIST_FLUSH, true); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1376 | change_winsz(); |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1377 | } |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1378 | #ifndef MKSH_NO_CMDLINE_EDITING |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1379 | if (have_tty && ( |
| 1380 | #if !MKSH_S_NOVI |
| 1381 | Flag(FVI) || |
| 1382 | #endif |
| 1383 | Flag(FEMACS) || Flag(FGMACS))) { |
| 1384 | int nread; |
| 1385 | |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1386 | nread = x_read(xp); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1387 | if (nread < 0) |
| 1388 | /* read error */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1389 | nread = 0; |
| 1390 | xp[nread] = '\0'; |
| 1391 | xp += nread; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1392 | } else |
| 1393 | #endif |
| 1394 | { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1395 | if (interactive) |
| 1396 | pprompt(prompt, 0); |
| 1397 | else |
| 1398 | s->line++; |
| 1399 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1400 | while (/* CONSTCOND */ 1) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1401 | char *p = shf_getse(xp, Xnleft(s->xs, xp), s->u.shf); |
| 1402 | |
| 1403 | if (!p && shf_error(s->u.shf) && |
| 1404 | shf_errno(s->u.shf) == EINTR) { |
| 1405 | shf_clearerr(s->u.shf); |
| 1406 | if (trap) |
| 1407 | runtraps(0); |
| 1408 | continue; |
| 1409 | } |
| 1410 | if (!p || (xp = p, xp[-1] == '\n')) |
| 1411 | break; |
| 1412 | /* double buffer size */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1413 | /* move past NUL so doubling works... */ |
| 1414 | xp++; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1415 | XcheckN(s->xs, xp, Xlength(s->xs, xp)); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1416 | /* ...and move back again */ |
| 1417 | xp--; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1418 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1419 | /* |
| 1420 | * flush any unwanted input so other programs/builtins |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1421 | * can read it. Not very optimal, but less error prone |
| 1422 | * than flushing else where, dealing with redirections, |
| 1423 | * etc. |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1424 | * TODO: reduce size of shf buffer (~128?) if SSTDIN |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1425 | */ |
| 1426 | if (s->type == SSTDIN) |
| 1427 | shf_flush(s->u.shf); |
| 1428 | } |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1429 | /* |
| 1430 | * XXX: temporary kludge to restore source after a |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1431 | * trap may have been executed. |
| 1432 | */ |
| 1433 | source = s; |
| 1434 | if (have_tty && ksh_tmout) { |
| 1435 | ksh_tmout_state = TMOUT_EXECUTING; |
| 1436 | alarm(0); |
| 1437 | } |
| 1438 | cp = Xstring(s->xs, xp); |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 1439 | rndpush(cp); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1440 | s->start = s->str = cp; |
| 1441 | strip_nuls(Xstring(s->xs, xp), Xlength(s->xs, xp)); |
| 1442 | /* Note: if input is all nulls, this is not eof */ |
| 1443 | if (Xlength(s->xs, xp) == 0) { |
| 1444 | /* EOF */ |
| 1445 | if (s->type == SFILE) |
| 1446 | shf_fdclose(s->u.shf); |
| 1447 | s->str = NULL; |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 1448 | } else if (interactive && *s->str) { |
| 1449 | if (cur_prompt != PS1) |
| 1450 | histsave(&s->line, s->str, HIST_APPEND, true); |
| 1451 | else if (!ctype(*s->str, C_IFS | C_IFSWS)) |
| 1452 | histsave(&s->line, s->str, HIST_QUEUE, true); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1453 | #if !defined(MKSH_SMALL) && HAVE_PERSISTENT_HISTORY |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 1454 | else |
| 1455 | goto check_for_sole_return; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1456 | } else if (interactive && cur_prompt == PS1) { |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 1457 | check_for_sole_return: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1458 | cp = Xstring(s->xs, xp); |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1459 | while (ctype(*cp, C_IFSWS)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1460 | ++cp; |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 1461 | if (!*cp) { |
| 1462 | histsave(&s->line, NULL, HIST_FLUSH, true); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1463 | histsync(); |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 1464 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1465 | #endif |
| 1466 | } |
| 1467 | if (interactive) |
| 1468 | set_prompt(PS2, NULL); |
| 1469 | } |
| 1470 | |
| 1471 | void |
| 1472 | set_prompt(int to, Source *s) |
| 1473 | { |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 1474 | cur_prompt = (uint8_t)to; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1475 | |
| 1476 | switch (to) { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1477 | /* command */ |
| 1478 | case PS1: |
| 1479 | /* |
| 1480 | * Substitute ! and !! here, before substitutions are done |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1481 | * so ! in expanded variables are not expanded. |
| 1482 | * NOTE: this is not what AT&T ksh does (it does it after |
| 1483 | * substitutions, POSIX doesn't say which is to be done. |
| 1484 | */ |
| 1485 | { |
| 1486 | struct shf *shf; |
| 1487 | char * volatile ps1; |
| 1488 | Area *saved_atemp; |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 1489 | int saved_lineno; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1490 | |
| 1491 | ps1 = str_val(global("PS1")); |
| 1492 | shf = shf_sopen(NULL, strlen(ps1) * 2, |
| 1493 | SHF_WR | SHF_DYNAMIC, NULL); |
| 1494 | while (*ps1) |
| 1495 | if (*ps1 != '!' || *++ps1 == '!') |
| 1496 | shf_putchar(*ps1++, shf); |
| 1497 | else |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1498 | shf_fprintf(shf, Tf_lu, s ? |
Elliott Hughes | b27ce95 | 2015-04-21 13:39:18 -0700 | [diff] [blame] | 1499 | (unsigned long)s->line + 1 : 0UL); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1500 | ps1 = shf_sclose(shf); |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 1501 | saved_lineno = current_lineno; |
| 1502 | if (s) |
| 1503 | current_lineno = s->line + 1; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1504 | saved_atemp = ATEMP; |
| 1505 | newenv(E_ERRH); |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1506 | if (kshsetjmp(e->jbuf)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1507 | prompt = safe_prompt; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1508 | /* |
| 1509 | * Don't print an error - assume it has already |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1510 | * been printed. Reason is we may have forked |
| 1511 | * to run a command and the child may be |
| 1512 | * unwinding its stack through this code as it |
| 1513 | * exits. |
| 1514 | */ |
| 1515 | } else { |
| 1516 | char *cp = substitute(ps1, 0); |
| 1517 | strdupx(prompt, cp, saved_atemp); |
| 1518 | } |
Elliott Hughes | 96b4363 | 2015-07-17 11:39:41 -0700 | [diff] [blame] | 1519 | current_lineno = saved_lineno; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1520 | quitenv(NULL); |
| 1521 | } |
| 1522 | break; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1523 | /* command continuation */ |
| 1524 | case PS2: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1525 | prompt = str_val(global("PS2")); |
| 1526 | break; |
| 1527 | } |
| 1528 | } |
| 1529 | |
Thorsten Glaser | 811a575 | 2013-07-25 14:24:45 +0000 | [diff] [blame] | 1530 | int |
| 1531 | pprompt(const char *cp, int ntruncate) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1532 | { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1533 | char delimiter = 0; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 1534 | bool doprint = (ntruncate != -1); |
| 1535 | bool indelimit = false; |
| 1536 | int columns = 0, lines = 0; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1537 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1538 | /* |
| 1539 | * Undocumented AT&T ksh feature: |
| 1540 | * If the second char in the prompt string is \r then the first |
| 1541 | * char is taken to be a non-printing delimiter and any chars |
| 1542 | * between two instances of the delimiter are not considered to |
| 1543 | * be part of the prompt length |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1544 | */ |
| 1545 | if (*cp && cp[1] == '\r') { |
| 1546 | delimiter = *cp; |
| 1547 | cp += 2; |
| 1548 | } |
| 1549 | for (; *cp; cp++) { |
| 1550 | if (indelimit && *cp != delimiter) |
| 1551 | ; |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1552 | else if (ctype(*cp, C_CR | C_LF)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1553 | lines += columns / x_cols + ((*cp == '\n') ? 1 : 0); |
| 1554 | columns = 0; |
| 1555 | } else if (*cp == '\t') { |
| 1556 | columns = (columns | 7) + 1; |
| 1557 | } else if (*cp == '\b') { |
| 1558 | if (columns > 0) |
| 1559 | columns--; |
| 1560 | } else if (*cp == delimiter) |
| 1561 | indelimit = !indelimit; |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1562 | else if (UTFMODE && (rtt2asc(*cp) > 0x7F)) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1563 | const char *cp2; |
| 1564 | columns += utf_widthadj(cp, &cp2); |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 1565 | if (doprint && (indelimit || |
| 1566 | (ntruncate < (x_cols * lines + columns)))) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1567 | shf_write(cp, cp2 - cp, shl_out); |
| 1568 | cp = cp2 - /* loop increment */ 1; |
| 1569 | continue; |
| 1570 | } else |
| 1571 | columns++; |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 1572 | if (doprint && (*cp != delimiter) && |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1573 | (indelimit || (ntruncate < (x_cols * lines + columns)))) |
| 1574 | shf_putc(*cp, shl_out); |
| 1575 | } |
Elliott Hughes | 737fdce | 2014-08-07 12:59:26 -0700 | [diff] [blame] | 1576 | if (doprint) |
| 1577 | shf_flush(shl_out); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1578 | return (x_cols * lines + columns); |
| 1579 | } |
| 1580 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1581 | /* |
| 1582 | * Read the variable part of a ${...} expression (i.e. up to but not |
| 1583 | * including the :[-+?=#%] or close-brace). |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1584 | */ |
| 1585 | static char * |
| 1586 | get_brace_var(XString *wsp, char *wp) |
| 1587 | { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1588 | char c; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1589 | enum parse_state { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1590 | PS_INITIAL, PS_SAW_PERCENT, PS_SAW_HASH, PS_SAW_BANG, |
| 1591 | PS_IDENT, PS_NUMBER, PS_VAR1 |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1592 | } state = PS_INITIAL; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1593 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1594 | while (/* CONSTCOND */ 1) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1595 | c = getsc(); |
| 1596 | /* State machine to figure out where the variable part ends. */ |
| 1597 | switch (state) { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1598 | case PS_SAW_HASH: |
| 1599 | if (ctype(c, C_VAR1)) { |
| 1600 | char c2; |
| 1601 | |
| 1602 | c2 = getsc(); |
| 1603 | ungetsc(c2); |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1604 | if (ord(c2) != ORD(/*{*/ '}')) { |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1605 | ungetsc(c); |
| 1606 | goto out; |
| 1607 | } |
| 1608 | } |
| 1609 | goto ps_common; |
| 1610 | case PS_SAW_BANG: |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1611 | switch (ord(c)) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1612 | case ORD('@'): |
| 1613 | case ORD('#'): |
| 1614 | case ORD('-'): |
| 1615 | case ORD('?'): |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1616 | goto out; |
| 1617 | } |
| 1618 | goto ps_common; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1619 | case PS_INITIAL: |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1620 | switch (ord(c)) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1621 | case ORD('%'): |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1622 | state = PS_SAW_PERCENT; |
| 1623 | goto next; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1624 | case ORD('#'): |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1625 | state = PS_SAW_HASH; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1626 | goto next; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1627 | case ORD('!'): |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1628 | state = PS_SAW_BANG; |
| 1629 | goto next; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1630 | } |
| 1631 | /* FALLTHROUGH */ |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1632 | case PS_SAW_PERCENT: |
| 1633 | ps_common: |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1634 | if (ctype(c, C_ALPHX)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1635 | state = PS_IDENT; |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1636 | else if (ctype(c, C_DIGIT)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1637 | state = PS_NUMBER; |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1638 | else if (ctype(c, C_VAR1)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1639 | state = PS_VAR1; |
| 1640 | else |
| 1641 | goto out; |
| 1642 | break; |
| 1643 | case PS_IDENT: |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1644 | if (!ctype(c, C_ALNUX)) { |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1645 | if (ord(c) == ORD('[')) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1646 | char *tmp, *p; |
| 1647 | |
| 1648 | if (!arraysub(&tmp)) |
Elliott Hughes | a3c3f96 | 2017-04-12 16:52:30 -0700 | [diff] [blame] | 1649 | yyerror("missing ]"); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1650 | *wp++ = c; |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1651 | p = tmp; |
| 1652 | while (*p) { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1653 | Xcheck(*wsp, wp); |
| 1654 | *wp++ = *p++; |
| 1655 | } |
| 1656 | afree(tmp, ATEMP); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1657 | /* the ] */ |
| 1658 | c = getsc(); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1659 | } |
| 1660 | goto out; |
| 1661 | } |
Elliott Hughes | 77740fc | 2016-08-12 15:06:53 -0700 | [diff] [blame] | 1662 | next: |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1663 | break; |
| 1664 | case PS_NUMBER: |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1665 | if (!ctype(c, C_DIGIT)) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1666 | goto out; |
| 1667 | break; |
| 1668 | case PS_VAR1: |
| 1669 | goto out; |
| 1670 | } |
| 1671 | Xcheck(*wsp, wp); |
| 1672 | *wp++ = c; |
| 1673 | } |
| 1674 | out: |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1675 | /* end of variable part */ |
| 1676 | *wp++ = '\0'; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1677 | ungetsc(c); |
| 1678 | return (wp); |
| 1679 | } |
| 1680 | |
| 1681 | /* |
| 1682 | * Save an array subscript - returns true if matching bracket found, false |
| 1683 | * if eof or newline was found. |
| 1684 | * (Returned string double null terminated) |
| 1685 | */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1686 | static bool |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1687 | arraysub(char **strp) |
| 1688 | { |
| 1689 | XString ws; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1690 | char *wp, c; |
| 1691 | /* we are just past the initial [ */ |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1692 | unsigned int depth = 1; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1693 | |
| 1694 | Xinit(ws, wp, 32, ATEMP); |
| 1695 | |
| 1696 | do { |
| 1697 | c = getsc(); |
| 1698 | Xcheck(ws, wp); |
| 1699 | *wp++ = c; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1700 | if (ord(c) == ORD('[')) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1701 | depth++; |
Elliott Hughes | dd4abe0 | 2018-02-05 15:55:19 -0800 | [diff] [blame] | 1702 | else if (ord(c) == ORD(']')) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1703 | depth--; |
| 1704 | } while (depth > 0 && c && c != '\n'); |
| 1705 | |
| 1706 | *wp++ = '\0'; |
| 1707 | *strp = Xclose(ws, wp); |
| 1708 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1709 | return (tobool(depth == 0)); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | /* Unget a char: handles case when we are already at the start of the buffer */ |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1713 | static void |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1714 | ungetsc(int c) |
| 1715 | { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1716 | struct sretrace_info *rp = retrace_info; |
| 1717 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1718 | if (backslash_skip) |
| 1719 | backslash_skip--; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1720 | /* Don't unget EOF... */ |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1721 | if (source->str == null && c == '\0') |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1722 | return; |
| 1723 | while (rp) { |
| 1724 | if (Xlength(rp->xs, rp->xp)) |
| 1725 | rp->xp--; |
| 1726 | rp = rp->next; |
| 1727 | } |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1728 | ungetsc_i(c); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1729 | } |
| 1730 | static void |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1731 | ungetsc_i(int c) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1732 | { |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1733 | if (source->str > source->start) |
| 1734 | source->str--; |
| 1735 | else { |
| 1736 | Source *s; |
| 1737 | |
| 1738 | s = pushs(SREREAD, source->areap); |
| 1739 | s->ugbuf[0] = c; s->ugbuf[1] = '\0'; |
| 1740 | s->start = s->str = s->ugbuf; |
| 1741 | s->next = source; |
| 1742 | source = s; |
| 1743 | } |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | /* Called to get a char that isn't a \newline sequence. */ |
| 1748 | static int |
| 1749 | getsc_bn(void) |
| 1750 | { |
| 1751 | int c, c2; |
| 1752 | |
| 1753 | if (ignore_backslash_newline) |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1754 | return (o_getsc_u()); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1755 | |
| 1756 | if (backslash_skip == 1) { |
| 1757 | backslash_skip = 2; |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1758 | return (o_getsc_u()); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | backslash_skip = 0; |
| 1762 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1763 | while (/* CONSTCOND */ 1) { |
| 1764 | c = o_getsc_u(); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1765 | if (c == '\\') { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1766 | if ((c2 = o_getsc_u()) == '\n') |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1767 | /* ignore the \newline; get the next char... */ |
| 1768 | continue; |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1769 | ungetsc_i(c2); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1770 | backslash_skip = 1; |
| 1771 | } |
| 1772 | return (c); |
| 1773 | } |
| 1774 | } |
| 1775 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1776 | void |
| 1777 | yyskiputf8bom(void) |
| 1778 | { |
| 1779 | int c; |
| 1780 | |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1781 | if (rtt2asc((c = o_getsc_u())) != 0xEF) { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1782 | ungetsc_i(c); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1783 | return; |
| 1784 | } |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1785 | if (rtt2asc((c = o_getsc_u())) != 0xBB) { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1786 | ungetsc_i(c); |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1787 | ungetsc_i(asc2rtt(0xEF)); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1788 | return; |
| 1789 | } |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1790 | if (rtt2asc((c = o_getsc_u())) != 0xBF) { |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1791 | ungetsc_i(c); |
Elliott Hughes | 23925bb | 2017-09-22 16:04:20 -0700 | [diff] [blame] | 1792 | ungetsc_i(asc2rtt(0xBB)); |
| 1793 | ungetsc_i(asc2rtt(0xEF)); |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1794 | return; |
| 1795 | } |
| 1796 | UTFMODE |= 8; |
| 1797 | } |
| 1798 | |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1799 | static Lex_state * |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1800 | push_state_i(State_info *si, Lex_state *old_end) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1801 | { |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1802 | Lex_state *news = alloc2(STATE_BSIZE, sizeof(Lex_state), ATEMP); |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1803 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1804 | news[0].ls_base = old_end; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1805 | si->base = &news[0]; |
| 1806 | si->end = &news[STATE_BSIZE]; |
| 1807 | return (&news[1]); |
| 1808 | } |
| 1809 | |
| 1810 | static Lex_state * |
Thorsten Glaser | c2dc5de | 2013-02-18 23:02:51 +0000 | [diff] [blame] | 1811 | pop_state_i(State_info *si, Lex_state *old_end) |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1812 | { |
| 1813 | Lex_state *old_base = si->base; |
| 1814 | |
Geremy Condra | 03ebf06 | 2011-10-12 18:17:24 -0700 | [diff] [blame] | 1815 | si->base = old_end->ls_base - STATE_BSIZE; |
| 1816 | si->end = old_end->ls_base; |
Jean-Baptiste Queru | 5155f1c | 2011-06-16 10:05:28 -0700 | [diff] [blame] | 1817 | |
| 1818 | afree(old_base, ATEMP); |
| 1819 | |
| 1820 | return (si->base + STATE_BSIZE - 1); |
| 1821 | } |