]>
Commit | Line | Data |
---|---|---|
8f2ece69 | 1 | /* POSIX.2 wordexp implementation. |
581c785b | 2 | Copyright (C) 1997-2022 Free Software Foundation, Inc. |
c84142e8 | 3 | This file is part of the GNU C Library. |
28f540f4 | 4 | |
c84142e8 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 9 | |
c84142e8 UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
28f540f4 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
28f540f4 | 18 | |
51028f34 UD |
19 | #include <ctype.h> |
20 | #include <errno.h> | |
21 | #include <fcntl.h> | |
22 | #include <fnmatch.h> | |
23 | #include <glob.h> | |
24 | #include <libintl.h> | |
25 | #include <paths.h> | |
26 | #include <pwd.h> | |
bea9b193 | 27 | #include <stdbool.h> |
51028f34 | 28 | #include <stdio.h> |
8f2ece69 | 29 | #include <string.h> |
51028f34 | 30 | #include <sys/param.h> |
8f2ece69 UD |
31 | #include <sys/wait.h> |
32 | #include <unistd.h> | |
51028f34 | 33 | #include <wordexp.h> |
db8cbc6a | 34 | #include <spawn.h> |
4272059d | 35 | #include <scratch_buffer.h> |
eb96ffb0 | 36 | #include <_itoa.h> |
8f2ece69 | 37 | #include <assert.h> |
28f540f4 | 38 | |
8f2ece69 UD |
39 | /* |
40 | * This is a recursive-descent-style word expansion routine. | |
41 | */ | |
42 | ||
14c44e2e | 43 | /* These variables are defined and initialized in the startup code. */ |
100351c3 UD |
44 | extern int __libc_argc attribute_hidden; |
45 | extern char **__libc_argv attribute_hidden; | |
2bcf29ba | 46 | |
8f2ece69 | 47 | /* Some forward declarations */ |
40a55d20 UD |
48 | static int parse_dollars (char **word, size_t *word_length, size_t *max_length, |
49 | const char *words, size_t *offset, int flags, | |
af69217f | 50 | wordexp_t *pwordexp, const char *ifs, |
b41bd5bc | 51 | const char *ifs_white, int quoted); |
40a55d20 UD |
52 | static int parse_backtick (char **word, size_t *word_length, |
53 | size_t *max_length, const char *words, | |
af69217f | 54 | size_t *offset, int flags, wordexp_t *pwordexp, |
b41bd5bc | 55 | const char *ifs, const char *ifs_white); |
22bc7978 UD |
56 | static int parse_dquote (char **word, size_t *word_length, size_t *max_length, |
57 | const char *words, size_t *offset, int flags, | |
58 | wordexp_t *pwordexp, const char *ifs, | |
b41bd5bc FW |
59 | const char *ifs_white); |
60 | static int eval_expr (char *expr, long int *result); | |
8f2ece69 | 61 | |
55c14926 | 62 | /* The w_*() functions manipulate word lists. */ |
8f2ece69 | 63 | |
40a55d20 UD |
64 | #define W_CHUNK (100) |
65 | ||
bac660f8 | 66 | /* Result of w_newword will be ignored if it's the last word. */ |
e9fc7bbb UD |
67 | static inline char * |
68 | w_newword (size_t *actlen, size_t *maxlen) | |
69 | { | |
70 | *actlen = *maxlen = 0; | |
71 | return NULL; | |
72 | } | |
73 | ||
9c7ff11a | 74 | static char * |
40a55d20 UD |
75 | w_addchar (char *buffer, size_t *actlen, size_t *maxlen, char ch) |
76 | /* (lengths exclude trailing zero) */ | |
8f2ece69 | 77 | { |
9c7ff11a | 78 | /* Add a character to the buffer, allocating room for it if needed. */ |
28f540f4 | 79 | |
40a55d20 | 80 | if (*actlen == *maxlen) |
8f2ece69 | 81 | { |
40a55d20 UD |
82 | char *old_buffer = buffer; |
83 | assert (buffer == NULL || *maxlen != 0); | |
84 | *maxlen += W_CHUNK; | |
9c7ff11a | 85 | buffer = (char *) realloc (buffer, 1 + *maxlen); |
40a55d20 UD |
86 | |
87 | if (buffer == NULL) | |
88 | free (old_buffer); | |
89 | } | |
28f540f4 | 90 | |
40a55d20 UD |
91 | if (buffer != NULL) |
92 | { | |
93 | buffer[*actlen] = ch; | |
94 | buffer[++(*actlen)] = '\0'; | |
8f2ece69 | 95 | } |
40a55d20 UD |
96 | |
97 | return buffer; | |
8f2ece69 UD |
98 | } |
99 | ||
40a55d20 | 100 | static char * |
3db52d94 UD |
101 | w_addmem (char *buffer, size_t *actlen, size_t *maxlen, const char *str, |
102 | size_t len) | |
8f2ece69 | 103 | { |
40a55d20 | 104 | /* Add a string to the buffer, allocating room for it if needed. |
8f2ece69 | 105 | */ |
40a55d20 | 106 | if (*actlen + len > *maxlen) |
8f2ece69 | 107 | { |
40a55d20 UD |
108 | char *old_buffer = buffer; |
109 | assert (buffer == NULL || *maxlen != 0); | |
110 | *maxlen += MAX (2 * len, W_CHUNK); | |
111 | buffer = realloc (old_buffer, 1 + *maxlen); | |
112 | ||
113 | if (buffer == NULL) | |
114 | free (old_buffer); | |
8f2ece69 | 115 | } |
8f2ece69 | 116 | |
40a55d20 UD |
117 | if (buffer != NULL) |
118 | { | |
86187531 | 119 | *((char *) __mempcpy (&buffer[*actlen], str, len)) = '\0'; |
40a55d20 | 120 | *actlen += len; |
40a55d20 | 121 | } |
8f2ece69 | 122 | |
40a55d20 | 123 | return buffer; |
8f2ece69 UD |
124 | } |
125 | ||
3db52d94 | 126 | static char * |
3db52d94 UD |
127 | w_addstr (char *buffer, size_t *actlen, size_t *maxlen, const char *str) |
128 | /* (lengths exclude trailing zero) */ | |
129 | { | |
130 | /* Add a string to the buffer, allocating room for it if needed. | |
131 | */ | |
132 | size_t len; | |
133 | ||
134 | assert (str != NULL); /* w_addstr only called from this file */ | |
135 | len = strlen (str); | |
136 | ||
137 | return w_addmem (buffer, actlen, maxlen, str, len); | |
138 | } | |
139 | ||
8f2ece69 UD |
140 | static int |
141 | w_addword (wordexp_t *pwordexp, char *word) | |
142 | { | |
143 | /* Add a word to the wordlist */ | |
144 | size_t num_p; | |
22bc7978 | 145 | char **new_wordv; |
e7c8359e | 146 | bool allocated = false; |
8f2ece69 | 147 | |
c06cc21c UD |
148 | /* Internally, NULL acts like "". Convert NULLs to "" before |
149 | * the caller sees them. | |
150 | */ | |
151 | if (word == NULL) | |
152 | { | |
153 | word = __strdup (""); | |
154 | if (word == NULL) | |
155 | goto no_space; | |
e7c8359e | 156 | allocated = true; |
c06cc21c UD |
157 | } |
158 | ||
8f2ece69 | 159 | num_p = 2 + pwordexp->we_wordc + pwordexp->we_offs; |
22bc7978 UD |
160 | new_wordv = realloc (pwordexp->we_wordv, sizeof (char *) * num_p); |
161 | if (new_wordv != NULL) | |
8f2ece69 | 162 | { |
22bc7978 | 163 | pwordexp->we_wordv = new_wordv; |
05d5dedc UD |
164 | pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc++] = word; |
165 | pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc] = NULL; | |
8f2ece69 UD |
166 | return 0; |
167 | } | |
168 | ||
e7c8359e UD |
169 | if (allocated) |
170 | free (word); | |
171 | ||
c06cc21c | 172 | no_space: |
8f2ece69 UD |
173 | return WRDE_NOSPACE; |
174 | } | |
175 | ||
176 | /* The parse_*() functions should leave *offset being the offset in 'words' | |
177 | * to the last character processed. | |
178 | */ | |
179 | ||
180 | static int | |
40a55d20 UD |
181 | parse_backslash (char **word, size_t *word_length, size_t *max_length, |
182 | const char *words, size_t *offset) | |
8f2ece69 UD |
183 | { |
184 | /* We are poised _at_ a backslash, not in quotes */ | |
185 | ||
186 | switch (words[1 + *offset]) | |
187 | { | |
188 | case 0: | |
189 | /* Backslash is last character of input words */ | |
190 | return WRDE_SYNTAX; | |
191 | ||
192 | case '\n': | |
86187531 | 193 | ++(*offset); |
8f2ece69 UD |
194 | break; |
195 | ||
196 | default: | |
40a55d20 | 197 | *word = w_addchar (*word, word_length, max_length, words[1 + *offset]); |
8f2ece69 UD |
198 | if (*word == NULL) |
199 | return WRDE_NOSPACE; | |
200 | ||
86187531 | 201 | ++(*offset); |
8f2ece69 UD |
202 | break; |
203 | } | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
208 | static int | |
40a55d20 UD |
209 | parse_qtd_backslash (char **word, size_t *word_length, size_t *max_length, |
210 | const char *words, size_t *offset) | |
8f2ece69 UD |
211 | { |
212 | /* We are poised _at_ a backslash, inside quotes */ | |
213 | ||
214 | switch (words[1 + *offset]) | |
215 | { | |
216 | case 0: | |
217 | /* Backslash is last character of input words */ | |
218 | return WRDE_SYNTAX; | |
219 | ||
220 | case '\n': | |
221 | ++(*offset); | |
222 | break; | |
223 | ||
224 | case '$': | |
225 | case '`': | |
226 | case '"': | |
227 | case '\\': | |
40a55d20 | 228 | *word = w_addchar (*word, word_length, max_length, words[1 + *offset]); |
8f2ece69 UD |
229 | if (*word == NULL) |
230 | return WRDE_NOSPACE; | |
231 | ||
40a55d20 | 232 | ++(*offset); |
8f2ece69 UD |
233 | break; |
234 | ||
235 | default: | |
40a55d20 UD |
236 | *word = w_addchar (*word, word_length, max_length, words[*offset]); |
237 | if (*word != NULL) | |
238 | *word = w_addchar (*word, word_length, max_length, words[1 + *offset]); | |
239 | ||
8f2ece69 UD |
240 | if (*word == NULL) |
241 | return WRDE_NOSPACE; | |
242 | ||
40a55d20 | 243 | ++(*offset); |
8f2ece69 UD |
244 | break; |
245 | } | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
250 | static int | |
40a55d20 UD |
251 | parse_tilde (char **word, size_t *word_length, size_t *max_length, |
252 | const char *words, size_t *offset, size_t wordc) | |
8f2ece69 UD |
253 | { |
254 | /* We are poised _at_ a tilde */ | |
55c14926 | 255 | size_t i; |
8f2ece69 UD |
256 | |
257 | if (*word_length != 0) | |
258 | { | |
259 | if (!((*word)[*word_length - 1] == '=' && wordc == 0)) | |
260 | { | |
3db52d94 UD |
261 | if (!((*word)[*word_length - 1] == ':' |
262 | && strchr (*word, '=') && wordc == 0)) | |
8f2ece69 | 263 | { |
40a55d20 | 264 | *word = w_addchar (*word, word_length, max_length, '~'); |
8f2ece69 UD |
265 | return *word ? 0 : WRDE_NOSPACE; |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | for (i = 1 + *offset; words[i]; i++) | |
271 | { | |
34a5a146 JM |
272 | if (words[i] == ':' || words[i] == '/' || words[i] == ' ' |
273 | || words[i] == '\t' || words[i] == 0 ) | |
8f2ece69 UD |
274 | break; |
275 | ||
276 | if (words[i] == '\\') | |
277 | { | |
40a55d20 | 278 | *word = w_addchar (*word, word_length, max_length, '~'); |
8f2ece69 UD |
279 | return *word ? 0 : WRDE_NOSPACE; |
280 | } | |
281 | } | |
282 | ||
283 | if (i == 1 + *offset) | |
284 | { | |
285 | /* Tilde appears on its own */ | |
05d5dedc | 286 | char* home; |
8f2ece69 | 287 | |
05d5dedc UD |
288 | /* POSIX.2 says ~ expands to $HOME and if HOME is unset the |
289 | results are unspecified. We do a lookup on the uid if | |
290 | HOME is unset. */ | |
8f2ece69 | 291 | |
05d5dedc UD |
292 | home = getenv ("HOME"); |
293 | if (home != NULL) | |
8f2ece69 | 294 | { |
05d5dedc | 295 | *word = w_addstr (*word, word_length, max_length, home); |
8f2ece69 UD |
296 | if (*word == NULL) |
297 | return WRDE_NOSPACE; | |
298 | } | |
299 | else | |
300 | { | |
4272059d FW |
301 | struct passwd pwd, *tpwd; |
302 | uid_t uid = __getuid (); | |
303 | int result; | |
304 | struct scratch_buffer tmpbuf; | |
305 | scratch_buffer_init (&tmpbuf); | |
306 | ||
307 | while ((result = __getpwuid_r (uid, &pwd, | |
308 | tmpbuf.data, tmpbuf.length, | |
309 | &tpwd)) != 0 | |
05d5dedc | 310 | && errno == ERANGE) |
4272059d FW |
311 | if (!scratch_buffer_grow (&tmpbuf)) |
312 | return WRDE_NOSPACE; | |
05d5dedc UD |
313 | |
314 | if (result == 0 && tpwd != NULL && pwd.pw_dir != NULL) | |
315 | { | |
316 | *word = w_addstr (*word, word_length, max_length, pwd.pw_dir); | |
317 | if (*word == NULL) | |
4272059d FW |
318 | { |
319 | scratch_buffer_free (&tmpbuf); | |
320 | return WRDE_NOSPACE; | |
321 | } | |
05d5dedc UD |
322 | } |
323 | else | |
324 | { | |
325 | *word = w_addchar (*word, word_length, max_length, '~'); | |
326 | if (*word == NULL) | |
4272059d FW |
327 | { |
328 | scratch_buffer_free (&tmpbuf); | |
329 | return WRDE_NOSPACE; | |
330 | } | |
05d5dedc | 331 | } |
4272059d | 332 | scratch_buffer_free (&tmpbuf); |
8f2ece69 UD |
333 | } |
334 | } | |
335 | else | |
336 | { | |
337 | /* Look up user name in database to get home directory */ | |
344af000 | 338 | char *user = strndupa (&words[1 + *offset], i - (1 + *offset)); |
8f2ece69 | 339 | struct passwd pwd, *tpwd; |
8f2ece69 | 340 | int result; |
4272059d FW |
341 | struct scratch_buffer tmpbuf; |
342 | scratch_buffer_init (&tmpbuf); | |
8f2ece69 | 343 | |
4272059d FW |
344 | while ((result = __getpwnam_r (user, &pwd, tmpbuf.data, tmpbuf.length, |
345 | &tpwd)) != 0 | |
8f2ece69 | 346 | && errno == ERANGE) |
4272059d FW |
347 | if (!scratch_buffer_grow (&tmpbuf)) |
348 | return WRDE_NOSPACE; | |
8f2ece69 | 349 | |
c5f57c58 | 350 | if (result == 0 && tpwd != NULL && pwd.pw_dir) |
40a55d20 | 351 | *word = w_addstr (*word, word_length, max_length, pwd.pw_dir); |
8f2ece69 UD |
352 | else |
353 | { | |
354 | /* (invalid login name) */ | |
40a55d20 | 355 | *word = w_addchar (*word, word_length, max_length, '~'); |
8f2ece69 | 356 | if (*word != NULL) |
40a55d20 | 357 | *word = w_addstr (*word, word_length, max_length, user); |
8f2ece69 UD |
358 | } |
359 | ||
4272059d FW |
360 | scratch_buffer_free (&tmpbuf); |
361 | ||
8f2ece69 UD |
362 | *offset = i - 1; |
363 | } | |
364 | return *word ? 0 : WRDE_NOSPACE; | |
365 | } | |
366 | ||
e9fc7bbb UD |
367 | |
368 | static int | |
e9fc7bbb UD |
369 | do_parse_glob (const char *glob_word, char **word, size_t *word_length, |
370 | size_t *max_length, wordexp_t *pwordexp, const char *ifs, | |
371 | const char *ifs_white) | |
372 | { | |
373 | int error; | |
2e47aff5 | 374 | unsigned int match; |
e9fc7bbb UD |
375 | glob_t globbuf; |
376 | ||
377 | error = glob (glob_word, GLOB_NOCHECK, NULL, &globbuf); | |
378 | ||
379 | if (error != 0) | |
380 | { | |
381 | /* We can only run into memory problems. */ | |
382 | assert (error == GLOB_NOSPACE); | |
383 | return WRDE_NOSPACE; | |
384 | } | |
385 | ||
386 | if (ifs && !*ifs) | |
387 | { | |
388 | /* No field splitting allowed. */ | |
389 | assert (globbuf.gl_pathv[0] != NULL); | |
390 | *word = w_addstr (*word, word_length, max_length, globbuf.gl_pathv[0]); | |
391 | for (match = 1; match < globbuf.gl_pathc && *word != NULL; ++match) | |
392 | { | |
393 | *word = w_addchar (*word, word_length, max_length, ' '); | |
394 | if (*word != NULL) | |
395 | *word = w_addstr (*word, word_length, max_length, | |
396 | globbuf.gl_pathv[match]); | |
397 | } | |
398 | ||
399 | globfree (&globbuf); | |
400 | return *word ? 0 : WRDE_NOSPACE; | |
401 | } | |
402 | ||
403 | assert (ifs == NULL || *ifs != '\0'); | |
404 | if (*word != NULL) | |
405 | { | |
406 | free (*word); | |
407 | *word = w_newword (word_length, max_length); | |
408 | } | |
409 | ||
410 | for (match = 0; match < globbuf.gl_pathc; ++match) | |
411 | { | |
412 | char *matching_word = __strdup (globbuf.gl_pathv[match]); | |
413 | if (matching_word == NULL || w_addword (pwordexp, matching_word)) | |
414 | { | |
415 | globfree (&globbuf); | |
416 | return WRDE_NOSPACE; | |
417 | } | |
418 | } | |
419 | ||
420 | globfree (&globbuf); | |
421 | return 0; | |
422 | } | |
423 | ||
8f2ece69 | 424 | static int |
40a55d20 UD |
425 | parse_glob (char **word, size_t *word_length, size_t *max_length, |
426 | const char *words, size_t *offset, int flags, | |
af69217f | 427 | wordexp_t *pwordexp, const char *ifs, const char *ifs_white) |
8f2ece69 | 428 | { |
2bcf29ba | 429 | /* We are poised just after a '*', a '[' or a '?'. */ |
e9fc7bbb | 430 | int error = WRDE_NOSPACE; |
2bcf29ba | 431 | int quoted = 0; /* 1 if singly-quoted, 2 if doubly */ |
57b36a0a | 432 | size_t i; |
e9fc7bbb | 433 | wordexp_t glob_list; /* List of words to glob */ |
8f2ece69 | 434 | |
e9fc7bbb UD |
435 | glob_list.we_wordc = 0; |
436 | glob_list.we_wordv = NULL; | |
437 | glob_list.we_offs = 0; | |
438 | for (; words[*offset] != '\0'; ++*offset) | |
2bcf29ba | 439 | { |
16d620db | 440 | if (strchr (ifs, words[*offset]) != NULL) |
9b3c7c3c | 441 | /* Reached IFS */ |
8f2ece69 UD |
442 | break; |
443 | ||
2bcf29ba UD |
444 | /* Sort out quoting */ |
445 | if (words[*offset] == '\'') | |
6e4c40ba | 446 | { |
2bcf29ba UD |
447 | if (quoted == 0) |
448 | { | |
449 | quoted = 1; | |
450 | continue; | |
451 | } | |
452 | else if (quoted == 1) | |
453 | { | |
454 | quoted = 0; | |
455 | continue; | |
456 | } | |
6e4c40ba | 457 | } |
2bcf29ba | 458 | else if (words[*offset] == '"') |
6e4c40ba UD |
459 | { |
460 | if (quoted == 0) | |
461 | { | |
462 | quoted = 2; | |
463 | continue; | |
464 | } | |
465 | else if (quoted == 2) | |
466 | { | |
467 | quoted = 0; | |
468 | continue; | |
469 | } | |
470 | } | |
8f2ece69 | 471 | |
2bcf29ba UD |
472 | /* Sort out other special characters */ |
473 | if (quoted != 1 && words[*offset] == '$') | |
474 | { | |
e9fc7bbb UD |
475 | error = parse_dollars (word, word_length, max_length, words, |
476 | offset, flags, &glob_list, ifs, ifs_white, | |
9b3c7c3c | 477 | quoted == 2); |
2bcf29ba | 478 | if (error) |
e9fc7bbb | 479 | goto tidy_up; |
2bcf29ba UD |
480 | |
481 | continue; | |
482 | } | |
483 | else if (words[*offset] == '\\') | |
484 | { | |
485 | if (quoted) | |
e9fc7bbb UD |
486 | error = parse_qtd_backslash (word, word_length, max_length, |
487 | words, offset); | |
2bcf29ba | 488 | else |
e9fc7bbb UD |
489 | error = parse_backslash (word, word_length, max_length, |
490 | words, offset); | |
2bcf29ba UD |
491 | |
492 | if (error) | |
e9fc7bbb | 493 | goto tidy_up; |
2bcf29ba UD |
494 | |
495 | continue; | |
496 | } | |
497 | ||
498 | *word = w_addchar (*word, word_length, max_length, words[*offset]); | |
499 | if (*word == NULL) | |
e9fc7bbb | 500 | goto tidy_up; |
2bcf29ba | 501 | } |
8f2ece69 | 502 | |
e9fc7bbb UD |
503 | /* Don't forget to re-parse the character we stopped at. */ |
504 | --*offset; | |
8f2ece69 | 505 | |
e9fc7bbb | 506 | /* Glob the words */ |
9b3c7c3c | 507 | error = w_addword (&glob_list, *word); |
e9fc7bbb UD |
508 | *word = w_newword (word_length, max_length); |
509 | for (i = 0; error == 0 && i < glob_list.we_wordc; i++) | |
510 | error = do_parse_glob (glob_list.we_wordv[i], word, word_length, | |
511 | max_length, pwordexp, ifs, ifs_white); | |
8f2ece69 | 512 | |
e9fc7bbb UD |
513 | /* Now tidy up */ |
514 | tidy_up: | |
515 | wordfree (&glob_list); | |
516 | return error; | |
8f2ece69 UD |
517 | } |
518 | ||
519 | static int | |
40a55d20 UD |
520 | parse_squote (char **word, size_t *word_length, size_t *max_length, |
521 | const char *words, size_t *offset) | |
8f2ece69 UD |
522 | { |
523 | /* We are poised just after a single quote */ | |
524 | for (; words[*offset]; ++(*offset)) | |
525 | { | |
526 | if (words[*offset] != '\'') | |
527 | { | |
40a55d20 | 528 | *word = w_addchar (*word, word_length, max_length, words[*offset]); |
8f2ece69 UD |
529 | if (*word == NULL) |
530 | return WRDE_NOSPACE; | |
531 | } | |
532 | else return 0; | |
533 | } | |
534 | ||
535 | /* Unterminated string */ | |
536 | return WRDE_SYNTAX; | |
537 | } | |
538 | ||
539 | /* Functions to evaluate an arithmetic expression */ | |
540 | static int | |
3db52d94 | 541 | eval_expr_val (char **expr, long int *result) |
28f540f4 | 542 | { |
8f2ece69 UD |
543 | char *digit; |
544 | ||
545 | /* Skip white space */ | |
546 | for (digit = *expr; digit && *digit && isspace (*digit); ++digit); | |
547 | ||
bcc86889 | 548 | if (*digit == '(') |
8f2ece69 | 549 | { |
8f2ece69 UD |
550 | /* Scan for closing paren */ |
551 | for (++digit; **expr && **expr != ')'; ++(*expr)); | |
552 | ||
553 | /* Is there one? */ | |
554 | if (!**expr) | |
555 | return WRDE_SYNTAX; | |
556 | ||
557 | *(*expr)++ = 0; | |
558 | ||
40a55d20 | 559 | if (eval_expr (digit, result)) |
8f2ece69 UD |
560 | return WRDE_SYNTAX; |
561 | ||
562 | return 0; | |
8f2ece69 UD |
563 | } |
564 | ||
bcc86889 UD |
565 | /* POSIX requires that decimal, octal, and hexadecimal constants are |
566 | recognized. Therefore we pass 0 as the third parameter to strtol. */ | |
567 | *result = strtol (digit, expr, 0); | |
568 | if (digit == *expr) | |
569 | return WRDE_SYNTAX; | |
8f2ece69 | 570 | |
8f2ece69 UD |
571 | return 0; |
572 | } | |
573 | ||
574 | static int | |
3db52d94 | 575 | eval_expr_multdiv (char **expr, long int *result) |
8f2ece69 | 576 | { |
3db52d94 | 577 | long int arg; |
8f2ece69 UD |
578 | |
579 | /* Read a Value */ | |
3db52d94 | 580 | if (eval_expr_val (expr, result) != 0) |
8f2ece69 UD |
581 | return WRDE_SYNTAX; |
582 | ||
583 | while (**expr) | |
584 | { | |
585 | /* Skip white space */ | |
586 | for (; *expr && **expr && isspace (**expr); ++(*expr)); | |
587 | ||
588 | if (**expr == '*') | |
589 | { | |
3db52d94 UD |
590 | ++(*expr); |
591 | if (eval_expr_val (expr, &arg) != 0) | |
8f2ece69 UD |
592 | return WRDE_SYNTAX; |
593 | ||
594 | *result *= arg; | |
595 | } | |
596 | else if (**expr == '/') | |
597 | { | |
3db52d94 UD |
598 | ++(*expr); |
599 | if (eval_expr_val (expr, &arg) != 0) | |
8f2ece69 UD |
600 | return WRDE_SYNTAX; |
601 | ||
2b028564 FW |
602 | /* Division by zero or integer overflow. */ |
603 | if (arg == 0 || (arg == -1 && *result == LONG_MIN)) | |
604 | return WRDE_SYNTAX; | |
605 | ||
8f2ece69 UD |
606 | *result /= arg; |
607 | } | |
608 | else break; | |
609 | } | |
610 | ||
611 | return 0; | |
612 | } | |
613 | ||
614 | static int | |
3db52d94 | 615 | eval_expr (char *expr, long int *result) |
8f2ece69 | 616 | { |
3db52d94 | 617 | long int arg; |
8f2ece69 UD |
618 | |
619 | /* Read a Multdiv */ | |
3db52d94 | 620 | if (eval_expr_multdiv (&expr, result) != 0) |
8f2ece69 UD |
621 | return WRDE_SYNTAX; |
622 | ||
623 | while (*expr) | |
624 | { | |
625 | /* Skip white space */ | |
626 | for (; expr && *expr && isspace (*expr); ++expr); | |
627 | ||
628 | if (*expr == '+') | |
629 | { | |
3db52d94 UD |
630 | ++expr; |
631 | if (eval_expr_multdiv (&expr, &arg) != 0) | |
8f2ece69 UD |
632 | return WRDE_SYNTAX; |
633 | ||
634 | *result += arg; | |
635 | } | |
636 | else if (*expr == '-') | |
637 | { | |
3db52d94 UD |
638 | ++expr; |
639 | if (eval_expr_multdiv (&expr, &arg) != 0) | |
8f2ece69 UD |
640 | return WRDE_SYNTAX; |
641 | ||
642 | *result -= arg; | |
643 | } | |
644 | else break; | |
645 | } | |
646 | ||
647 | return 0; | |
648 | } | |
649 | ||
650 | static int | |
40a55d20 UD |
651 | parse_arith (char **word, size_t *word_length, size_t *max_length, |
652 | const char *words, size_t *offset, int flags, int bracket) | |
8f2ece69 UD |
653 | { |
654 | /* We are poised just after "$((" or "$[" */ | |
28f540f4 | 655 | int error; |
8f2ece69 | 656 | int paren_depth = 1; |
e9fc7bbb UD |
657 | size_t expr_length; |
658 | size_t expr_maxlen; | |
659 | char *expr; | |
8f2ece69 | 660 | |
e9fc7bbb | 661 | expr = w_newword (&expr_length, &expr_maxlen); |
8f2ece69 UD |
662 | for (; words[*offset]; ++(*offset)) |
663 | { | |
664 | switch (words[*offset]) | |
665 | { | |
666 | case '$': | |
40a55d20 | 667 | error = parse_dollars (&expr, &expr_length, &expr_maxlen, |
9b3c7c3c | 668 | words, offset, flags, NULL, NULL, NULL, 1); |
2bcf29ba | 669 | /* The ``1'' here is to tell parse_dollars not to |
8f2ece69 UD |
670 | * split the fields. |
671 | */ | |
672 | if (error) | |
673 | { | |
674 | free (expr); | |
675 | return error; | |
676 | } | |
677 | break; | |
678 | ||
679 | case '`': | |
680 | (*offset)++; | |
40a55d20 | 681 | error = parse_backtick (&expr, &expr_length, &expr_maxlen, |
af69217f UD |
682 | words, offset, flags, NULL, NULL, NULL); |
683 | /* The first NULL here is to tell parse_backtick not to | |
8f2ece69 UD |
684 | * split the fields. |
685 | */ | |
686 | if (error) | |
687 | { | |
688 | free (expr); | |
689 | return error; | |
690 | } | |
691 | break; | |
692 | ||
693 | case '\\': | |
40a55d20 UD |
694 | error = parse_qtd_backslash (&expr, &expr_length, &expr_maxlen, |
695 | words, offset); | |
8f2ece69 UD |
696 | if (error) |
697 | { | |
698 | free (expr); | |
699 | return error; | |
700 | } | |
701 | /* I think that a backslash within an | |
702 | * arithmetic expansion is bound to | |
703 | * cause an error sooner or later anyway though. | |
704 | */ | |
705 | break; | |
706 | ||
707 | case ')': | |
708 | if (--paren_depth == 0) | |
709 | { | |
86187531 | 710 | char result[21]; /* 21 = ceil(log10(2^64)) + 1 */ |
3db52d94 UD |
711 | long int numresult = 0; |
712 | long long int convertme; | |
8f2ece69 UD |
713 | |
714 | if (bracket || words[1 + *offset] != ')') | |
33c3e20e UD |
715 | { |
716 | free (expr); | |
717 | return WRDE_SYNTAX; | |
718 | } | |
8f2ece69 UD |
719 | |
720 | ++(*offset); | |
721 | ||
722 | /* Go - evaluate. */ | |
3db52d94 | 723 | if (*expr && eval_expr (expr, &numresult) != 0) |
33c3e20e UD |
724 | { |
725 | free (expr); | |
726 | return WRDE_SYNTAX; | |
727 | } | |
8f2ece69 | 728 | |
3db52d94 UD |
729 | if (numresult < 0) |
730 | { | |
731 | convertme = -numresult; | |
732 | *word = w_addchar (*word, word_length, max_length, '-'); | |
733 | if (!*word) | |
734 | { | |
735 | free (expr); | |
736 | return WRDE_NOSPACE; | |
737 | } | |
738 | } | |
739 | else | |
740 | convertme = numresult; | |
741 | ||
86187531 | 742 | result[20] = '\0'; |
7fbc8784 UD |
743 | *word = w_addstr (*word, word_length, max_length, |
744 | _itoa (convertme, &result[20], 10, 0)); | |
8f2ece69 UD |
745 | free (expr); |
746 | return *word ? 0 : WRDE_NOSPACE; | |
747 | } | |
40a55d20 | 748 | expr = w_addchar (expr, &expr_length, &expr_maxlen, words[*offset]); |
8f2ece69 UD |
749 | if (expr == NULL) |
750 | return WRDE_NOSPACE; | |
751 | ||
752 | break; | |
753 | ||
754 | case ']': | |
755 | if (bracket && paren_depth == 1) | |
756 | { | |
86187531 | 757 | char result[21]; /* 21 = ceil(log10(2^64)) + 1 */ |
3db52d94 | 758 | long int numresult = 0; |
8f2ece69 UD |
759 | |
760 | /* Go - evaluate. */ | |
761 | if (*expr && eval_expr (expr, &numresult) != 0) | |
33c3e20e UD |
762 | { |
763 | free (expr); | |
764 | return WRDE_SYNTAX; | |
765 | } | |
8f2ece69 | 766 | |
86187531 UD |
767 | result[20] = '\0'; |
768 | *word = w_addstr (*word, word_length, max_length, | |
769 | _itoa_word (numresult, &result[20], 10, 0)); | |
8f2ece69 UD |
770 | free (expr); |
771 | return *word ? 0 : WRDE_NOSPACE; | |
772 | } | |
773 | ||
774 | free (expr); | |
775 | return WRDE_SYNTAX; | |
776 | ||
777 | case '\n': | |
778 | case ';': | |
779 | case '{': | |
780 | case '}': | |
781 | free (expr); | |
782 | return WRDE_BADCHAR; | |
783 | ||
784 | case '(': | |
785 | ++paren_depth; | |
32db86d5 | 786 | /* Fall through. */ |
8f2ece69 | 787 | default: |
40a55d20 | 788 | expr = w_addchar (expr, &expr_length, &expr_maxlen, words[*offset]); |
8f2ece69 UD |
789 | if (expr == NULL) |
790 | return WRDE_NOSPACE; | |
791 | } | |
792 | } | |
793 | ||
794 | /* Premature end */ | |
795 | free (expr); | |
796 | return WRDE_SYNTAX; | |
797 | } | |
798 | ||
db8cbc6a AZ |
799 | #define DYNARRAY_STRUCT strlist |
800 | #define DYNARRAY_ELEMENT char * | |
801 | #define DYNARRAY_PREFIX strlist_ | |
802 | /* Allocates about 512/1024 (32/64 bit) on stack. */ | |
803 | #define DYNARRAY_INITIAL_SIZE 128 | |
804 | #include <malloc/dynarray-skeleton.c> | |
805 | ||
05d5dedc | 806 | /* Function called by child process in exec_comm() */ |
db8cbc6a AZ |
807 | static pid_t |
808 | exec_comm_child (char *comm, int *fildes, bool showerr, bool noexec) | |
05d5dedc | 809 | { |
db8cbc6a | 810 | pid_t pid = -1; |
05d5dedc | 811 | |
db8cbc6a AZ |
812 | /* Execute the command, or just check syntax? */ |
813 | const char *args[] = { _PATH_BSHELL, noexec ? "-nc" : "-c", comm, NULL }; | |
814 | ||
815 | posix_spawn_file_actions_t fa; | |
816 | /* posix_spawn_file_actions_init does not fail. */ | |
817 | __posix_spawn_file_actions_init (&fa); | |
05d5dedc | 818 | |
db8cbc6a AZ |
819 | /* Redirect output. For check syntax only (noexec being true), exec_comm |
820 | explicits sets fildes[1] to -1, so check its value to avoid a failure in | |
821 | __posix_spawn_file_actions_adddup2. */ | |
822 | if (fildes[1] != -1) | |
e038616f | 823 | { |
db8cbc6a AZ |
824 | if (__glibc_likely (fildes[1] != STDOUT_FILENO)) |
825 | { | |
826 | if (__posix_spawn_file_actions_adddup2 (&fa, fildes[1], | |
827 | STDOUT_FILENO) != 0 | |
828 | || __posix_spawn_file_actions_addclose (&fa, fildes[1]) != 0) | |
829 | goto out; | |
830 | } | |
831 | else | |
832 | /* Reset the close-on-exec flag (if necessary). */ | |
833 | if (__posix_spawn_file_actions_adddup2 (&fa, fildes[1], fildes[1]) | |
834 | != 0) | |
835 | goto out; | |
e038616f | 836 | } |
05d5dedc UD |
837 | |
838 | /* Redirect stderr to /dev/null if we have to. */ | |
db8cbc6a AZ |
839 | if (!showerr) |
840 | if (__posix_spawn_file_actions_addopen (&fa, STDERR_FILENO, _PATH_DEVNULL, | |
841 | O_WRONLY, 0) != 0) | |
842 | goto out; | |
843 | ||
844 | struct strlist newenv; | |
845 | strlist_init (&newenv); | |
846 | ||
847 | bool recreate_env = getenv ("IFS") != NULL; | |
848 | if (recreate_env) | |
05d5dedc | 849 | { |
db8cbc6a AZ |
850 | for (char **ep = __environ; *ep != NULL; ep++) |
851 | if (strncmp (*ep, "IFS=", strlen ("IFS=")) != 0) | |
852 | strlist_add (&newenv, *ep); | |
853 | strlist_add (&newenv, NULL); | |
854 | if (strlist_has_failed (&newenv)) | |
855 | goto out; | |
05d5dedc UD |
856 | } |
857 | ||
db8cbc6a AZ |
858 | /* pid is not set if posix_spawn fails, so it keep the original value |
859 | of -1. */ | |
860 | __posix_spawn (&pid, _PATH_BSHELL, &fa, NULL, (char *const *) args, | |
861 | recreate_env ? strlist_begin (&newenv) : __environ); | |
05d5dedc | 862 | |
db8cbc6a AZ |
863 | strlist_free (&newenv); |
864 | ||
865 | out: | |
866 | __posix_spawn_file_actions_destroy (&fa); | |
05d5dedc | 867 | |
db8cbc6a | 868 | return pid; |
05d5dedc UD |
869 | } |
870 | ||
8f2ece69 UD |
871 | /* Function to execute a command and retrieve the results */ |
872 | /* pwordexp contains NULL if field-splitting is forbidden */ | |
873 | static int | |
40a55d20 | 874 | exec_comm (char *comm, char **word, size_t *word_length, size_t *max_length, |
af69217f UD |
875 | int flags, wordexp_t *pwordexp, const char *ifs, |
876 | const char *ifs_white) | |
8f2ece69 | 877 | { |
40a55d20 | 878 | int fildes[2]; |
f89c41c1 | 879 | #define bufsize 128 |
8f2ece69 | 880 | int buflen; |
9b3c7c3c | 881 | int i; |
05d5dedc UD |
882 | int status = 0; |
883 | size_t maxnewlines = 0; | |
f89c41c1 | 884 | char buffer[bufsize]; |
28f540f4 | 885 | pid_t pid; |
db8cbc6a | 886 | bool noexec = false; |
28f540f4 | 887 | |
a39208bd CD |
888 | /* Do nothing if command substitution should not succeed. */ |
889 | if (flags & WRDE_NOCMD) | |
890 | return WRDE_CMDSUB; | |
891 | ||
db8cbc6a | 892 | /* Don't posix_spawn unless necessary */ |
8f2ece69 UD |
893 | if (!comm || !*comm) |
894 | return 0; | |
28f540f4 | 895 | |
46d8874d FW |
896 | if (__pipe2 (fildes, O_CLOEXEC) < 0) |
897 | return WRDE_NOSPACE; | |
28f540f4 | 898 | |
f89c41c1 | 899 | again: |
db8cbc6a AZ |
900 | pid = exec_comm_child (comm, fildes, noexec ? false : flags & WRDE_SHOWERR, |
901 | noexec); | |
902 | if (pid < 0) | |
8f2ece69 | 903 | { |
e038616f UD |
904 | __close (fildes[0]); |
905 | __close (fildes[1]); | |
8f2ece69 UD |
906 | return WRDE_NOSPACE; |
907 | } | |
28f540f4 | 908 | |
f89c41c1 UD |
909 | /* If we are just testing the syntax, only wait. */ |
910 | if (noexec) | |
911 | return (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) == pid | |
912 | && status != 0) ? WRDE_SYNTAX : 0; | |
913 | ||
50304ef0 | 914 | __close (fildes[1]); |
f89c41c1 | 915 | fildes[1] = -1; |
28f540f4 | 916 | |
9b3c7c3c | 917 | if (!pwordexp) |
05d5dedc UD |
918 | /* Quoted - no field splitting */ |
919 | { | |
9b3c7c3c | 920 | while (1) |
8f2ece69 | 921 | { |
f89c41c1 UD |
922 | if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer, |
923 | bufsize))) < 1) | |
9b3c7c3c | 924 | { |
ab087e0a AS |
925 | /* If read returned 0 then the process has closed its |
926 | stdout. Don't use WNOHANG in that case to avoid busy | |
927 | looping until the process eventually exits. */ | |
928 | if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, | |
929 | buflen == 0 ? 0 : WNOHANG)) | |
930 | == 0) | |
9b3c7c3c | 931 | continue; |
f89c41c1 UD |
932 | if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer, |
933 | bufsize))) < 1) | |
9b3c7c3c UD |
934 | break; |
935 | } | |
936 | ||
05d5dedc UD |
937 | maxnewlines += buflen; |
938 | ||
9b3c7c3c UD |
939 | *word = w_addmem (*word, word_length, max_length, buffer, buflen); |
940 | if (*word == NULL) | |
941 | goto no_space; | |
8f2ece69 | 942 | } |
9b3c7c3c UD |
943 | } |
944 | else | |
945 | /* Not quoted - split fields */ | |
946 | { | |
947 | int copying = 0; | |
948 | /* 'copying' is: | |
949 | * 0 when searching for first character in a field not IFS white space | |
950 | * 1 when copying the text of a field | |
951 | * 2 when searching for possible non-whitespace IFS | |
05d5dedc | 952 | * 3 when searching for non-newline after copying field |
9b3c7c3c UD |
953 | */ |
954 | ||
955 | while (1) | |
956 | { | |
f89c41c1 UD |
957 | if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer, |
958 | bufsize))) < 1) | |
9b3c7c3c | 959 | { |
ab087e0a AS |
960 | /* If read returned 0 then the process has closed its |
961 | stdout. Don't use WNOHANG in that case to avoid busy | |
962 | looping until the process eventually exits. */ | |
963 | if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, | |
964 | buflen == 0 ? 0 : WNOHANG)) | |
965 | == 0) | |
9b3c7c3c | 966 | continue; |
f89c41c1 UD |
967 | if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer, |
968 | bufsize))) < 1) | |
9b3c7c3c UD |
969 | break; |
970 | } | |
a8125d85 | 971 | |
9b3c7c3c UD |
972 | for (i = 0; i < buflen; ++i) |
973 | { | |
974 | if (strchr (ifs, buffer[i]) != NULL) | |
975 | { | |
976 | /* Current character is IFS */ | |
977 | if (strchr (ifs_white, buffer[i]) == NULL) | |
978 | { | |
979 | /* Current character is IFS but not whitespace */ | |
980 | if (copying == 2) | |
981 | { | |
982 | /* current character | |
983 | * | | |
984 | * V | |
985 | * eg: text<space><comma><space>moretext | |
986 | * | |
987 | * So, strip whitespace IFS (like at the start) | |
988 | */ | |
989 | copying = 0; | |
990 | continue; | |
991 | } | |
992 | ||
993 | copying = 0; | |
994 | /* fall through and delimit field.. */ | |
995 | } | |
996 | else | |
997 | { | |
05d5dedc UD |
998 | if (buffer[i] == '\n') |
999 | { | |
1000 | /* Current character is (IFS) newline */ | |
9b3c7c3c | 1001 | |
05d5dedc UD |
1002 | /* If copying a field, this is the end of it, |
1003 | but maybe all that's left is trailing newlines. | |
1004 | So start searching for a non-newline. */ | |
1005 | if (copying == 1) | |
1006 | copying = 3; | |
9b3c7c3c | 1007 | |
05d5dedc UD |
1008 | continue; |
1009 | } | |
1010 | else | |
1011 | { | |
1012 | /* Current character is IFS white space, but | |
1013 | not a newline */ | |
1014 | ||
1015 | /* If not either copying a field or searching | |
1016 | for non-newline after a field, ignore it */ | |
1017 | if (copying != 1 && copying != 3) | |
1018 | continue; | |
1019 | ||
1020 | /* End of field (search for non-ws IFS afterwards) */ | |
1021 | copying = 2; | |
1022 | } | |
9b3c7c3c UD |
1023 | } |
1024 | ||
05d5dedc | 1025 | /* First IFS white space (non-newline), or IFS non-whitespace. |
9b3c7c3c UD |
1026 | * Delimit the field. Nulls are converted by w_addword. */ |
1027 | if (w_addword (pwordexp, *word) == WRDE_NOSPACE) | |
1028 | goto no_space; | |
1029 | ||
1030 | *word = w_newword (word_length, max_length); | |
05d5dedc UD |
1031 | |
1032 | maxnewlines = 0; | |
9b3c7c3c UD |
1033 | /* fall back round the loop.. */ |
1034 | } | |
1035 | else | |
1036 | { | |
1037 | /* Not IFS character */ | |
05d5dedc UD |
1038 | |
1039 | if (copying == 3) | |
1040 | { | |
1041 | /* Nothing but (IFS) newlines since the last field, | |
3ce1f295 | 1042 | so delimit it here before starting new word */ |
05d5dedc UD |
1043 | if (w_addword (pwordexp, *word) == WRDE_NOSPACE) |
1044 | goto no_space; | |
1045 | ||
1046 | *word = w_newword (word_length, max_length); | |
1047 | } | |
1048 | ||
9b3c7c3c UD |
1049 | copying = 1; |
1050 | ||
05d5dedc UD |
1051 | if (buffer[i] == '\n') /* happens if newline not in IFS */ |
1052 | maxnewlines++; | |
1053 | else | |
1054 | maxnewlines = 0; | |
1055 | ||
9b3c7c3c UD |
1056 | *word = w_addchar (*word, word_length, max_length, |
1057 | buffer[i]); | |
1058 | if (*word == NULL) | |
1059 | goto no_space; | |
1060 | } | |
1061 | } | |
1062 | } | |
8f2ece69 | 1063 | } |
28f540f4 | 1064 | |
05d5dedc UD |
1065 | /* Chop off trailing newlines (required by POSIX.2) */ |
1066 | /* Ensure we don't go back further than the beginning of the | |
1067 | substitution (i.e. remove maxnewlines bytes at most) */ | |
34a5a146 JM |
1068 | while (maxnewlines-- != 0 |
1069 | && *word_length > 0 && (*word)[*word_length - 1] == '\n') | |
1720f4d0 UD |
1070 | { |
1071 | (*word)[--*word_length] = '\0'; | |
1072 | ||
1ffaaca2 | 1073 | /* If the last word was entirely newlines, turn it into a new word |
1720f4d0 | 1074 | * which can be ignored if there's nothing following it. */ |
1ffaaca2 | 1075 | if (*word_length == 0) |
1720f4d0 UD |
1076 | { |
1077 | free (*word); | |
1078 | *word = w_newword (word_length, max_length); | |
1ffaaca2 | 1079 | break; |
1720f4d0 UD |
1080 | } |
1081 | } | |
22bc7978 | 1082 | |
50304ef0 | 1083 | __close (fildes[0]); |
f89c41c1 | 1084 | fildes[0] = -1; |
05d5dedc UD |
1085 | |
1086 | /* Check for syntax error (re-execute but with "-n" flag) */ | |
1087 | if (buflen < 1 && status != 0) | |
1088 | { | |
db8cbc6a | 1089 | noexec = true; |
f89c41c1 | 1090 | goto again; |
05d5dedc UD |
1091 | } |
1092 | ||
8f2ece69 | 1093 | return 0; |
33c3e20e UD |
1094 | |
1095 | no_space: | |
1096 | __kill (pid, SIGKILL); | |
f89c41c1 | 1097 | TEMP_FAILURE_RETRY (__waitpid (pid, NULL, 0)); |
33c3e20e UD |
1098 | __close (fildes[0]); |
1099 | return WRDE_NOSPACE; | |
8f2ece69 UD |
1100 | } |
1101 | ||
1102 | static int | |
40a55d20 | 1103 | parse_comm (char **word, size_t *word_length, size_t *max_length, |
af69217f UD |
1104 | const char *words, size_t *offset, int flags, wordexp_t *pwordexp, |
1105 | const char *ifs, const char *ifs_white) | |
8f2ece69 UD |
1106 | { |
1107 | /* We are poised just after "$(" */ | |
1108 | int paren_depth = 1; | |
3c20b9b6 | 1109 | int error = 0; |
3c20b9b6 | 1110 | int quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */ |
e9fc7bbb UD |
1111 | size_t comm_length; |
1112 | size_t comm_maxlen; | |
1113 | char *comm = w_newword (&comm_length, &comm_maxlen); | |
8f2ece69 UD |
1114 | |
1115 | for (; words[*offset]; ++(*offset)) | |
28f540f4 | 1116 | { |
8f2ece69 | 1117 | switch (words[*offset]) |
28f540f4 | 1118 | { |
3c20b9b6 UD |
1119 | case '\'': |
1120 | if (quoted == 0) | |
1121 | quoted = 1; | |
1122 | else if (quoted == 1) | |
1123 | quoted = 0; | |
1124 | ||
1125 | break; | |
1126 | ||
1127 | case '"': | |
1128 | if (quoted == 0) | |
1129 | quoted = 2; | |
1130 | else if (quoted == 2) | |
1131 | quoted = 0; | |
1132 | ||
1133 | break; | |
1134 | ||
8f2ece69 | 1135 | case ')': |
3c20b9b6 | 1136 | if (!quoted && --paren_depth == 0) |
8f2ece69 UD |
1137 | { |
1138 | /* Go -- give script to the shell */ | |
3c20b9b6 UD |
1139 | if (comm) |
1140 | { | |
db8cbc6a | 1141 | /* posix_spawn already handles thread cancellation. */ |
3c20b9b6 UD |
1142 | error = exec_comm (comm, word, word_length, max_length, |
1143 | flags, pwordexp, ifs, ifs_white); | |
1144 | free (comm); | |
1145 | } | |
1146 | ||
8f2ece69 UD |
1147 | return error; |
1148 | } | |
1149 | ||
1150 | /* This is just part of the script */ | |
8f2ece69 UD |
1151 | break; |
1152 | ||
1153 | case '(': | |
3c20b9b6 UD |
1154 | if (!quoted) |
1155 | ++paren_depth; | |
28f540f4 | 1156 | } |
3c20b9b6 UD |
1157 | |
1158 | comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]); | |
1159 | if (comm == NULL) | |
1160 | return WRDE_NOSPACE; | |
28f540f4 | 1161 | } |
28f540f4 | 1162 | |
ecf359c6 UD |
1163 | /* Premature end. */ |
1164 | free (comm); | |
3c20b9b6 | 1165 | |
8f2ece69 UD |
1166 | return WRDE_SYNTAX; |
1167 | } | |
28f540f4 | 1168 | |
01d032e0 PP |
1169 | #define CHAR_IN_SET(ch, char_set) \ |
1170 | (memchr (char_set "", ch, sizeof (char_set) - 1) != NULL) | |
1171 | ||
8f2ece69 | 1172 | static int |
40a55d20 | 1173 | parse_param (char **word, size_t *word_length, size_t *max_length, |
af69217f | 1174 | const char *words, size_t *offset, int flags, wordexp_t *pwordexp, |
2bcf29ba | 1175 | const char *ifs, const char *ifs_white, int quoted) |
8f2ece69 UD |
1176 | { |
1177 | /* We are poised just after "$" */ | |
3c20b9b6 | 1178 | enum action |
af6f3906 | 1179 | { |
3c20b9b6 UD |
1180 | ACT_NONE, |
1181 | ACT_RP_SHORT_LEFT = '#', | |
1182 | ACT_RP_LONG_LEFT = 'L', | |
1183 | ACT_RP_SHORT_RIGHT = '%', | |
1184 | ACT_RP_LONG_RIGHT = 'R', | |
1185 | ACT_NULL_ERROR = '?', | |
1186 | ACT_NULL_SUBST = '-', | |
1187 | ACT_NONNULL_SUBST = '+', | |
1188 | ACT_NULL_ASSIGN = '=' | |
af6f3906 | 1189 | }; |
e9fc7bbb UD |
1190 | size_t env_length; |
1191 | size_t env_maxlen; | |
1192 | size_t pat_length; | |
1193 | size_t pat_maxlen; | |
3c20b9b6 | 1194 | size_t start = *offset; |
e9fc7bbb UD |
1195 | char *env; |
1196 | char *pattern; | |
2bcf29ba | 1197 | char *value = NULL; |
3c20b9b6 | 1198 | enum action action = ACT_NONE; |
8f2ece69 | 1199 | int depth = 0; |
3c20b9b6 | 1200 | int colon_seen = 0; |
14c44e2e | 1201 | int seen_hash = 0; |
76fbcfdd | 1202 | int free_value = 0; |
3c20b9b6 | 1203 | int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */ |
8f2ece69 | 1204 | int error; |
1f4a9ebd UD |
1205 | int special = 0; |
1206 | char buffer[21]; | |
3c20b9b6 UD |
1207 | int brace = words[*offset] == '{'; |
1208 | ||
e9fc7bbb UD |
1209 | env = w_newword (&env_length, &env_maxlen); |
1210 | pattern = w_newword (&pat_length, &pat_maxlen); | |
1211 | ||
3c20b9b6 UD |
1212 | if (brace) |
1213 | ++*offset; | |
8f2ece69 | 1214 | |
1f4a9ebd UD |
1215 | /* First collect the parameter name. */ |
1216 | ||
1217 | if (words[*offset] == '#') | |
8f2ece69 | 1218 | { |
1f4a9ebd UD |
1219 | seen_hash = 1; |
1220 | if (!brace) | |
1221 | goto envsubst; | |
1222 | ++*offset; | |
1223 | } | |
af6f3906 | 1224 | |
1f4a9ebd UD |
1225 | if (isalpha (words[*offset]) || words[*offset] == '_') |
1226 | { | |
1227 | /* Normal parameter name. */ | |
1228 | do | |
1229 | { | |
1230 | env = w_addchar (env, &env_length, &env_maxlen, | |
1231 | words[*offset]); | |
1232 | if (env == NULL) | |
1233 | goto no_space; | |
1234 | } | |
1235 | while (isalnum (words[++*offset]) || words[*offset] == '_'); | |
1236 | } | |
1237 | else if (isdigit (words[*offset])) | |
1238 | { | |
1239 | /* Numeric parameter name. */ | |
1240 | special = 1; | |
1241 | do | |
1242 | { | |
1243 | env = w_addchar (env, &env_length, &env_maxlen, | |
1244 | words[*offset]); | |
1245 | if (env == NULL) | |
1246 | goto no_space; | |
1247 | if (!brace) | |
1248 | goto envsubst; | |
1249 | } | |
1250 | while (isdigit(words[++*offset])); | |
1251 | } | |
01d032e0 | 1252 | else if (CHAR_IN_SET (words[*offset], "*@$")) |
1f4a9ebd UD |
1253 | { |
1254 | /* Special parameter. */ | |
1255 | special = 1; | |
1256 | env = w_addchar (env, &env_length, &env_maxlen, | |
1257 | words[*offset]); | |
1258 | if (env == NULL) | |
1259 | goto no_space; | |
1260 | ++*offset; | |
1261 | } | |
1262 | else | |
1263 | { | |
1264 | if (brace) | |
1265 | goto syntax; | |
1266 | } | |
1267 | ||
1268 | if (brace) | |
1269 | { | |
1270 | /* Check for special action to be applied to the value. */ | |
1271 | switch (words[*offset]) | |
1272 | { | |
1273 | case '}': | |
68b50604 | 1274 | /* Evaluate. */ |
1f4a9ebd UD |
1275 | goto envsubst; |
1276 | ||
1277 | case '#': | |
1278 | action = ACT_RP_SHORT_LEFT; | |
1279 | if (words[1 + *offset] == '#') | |
1280 | { | |
1281 | ++*offset; | |
1282 | action = ACT_RP_LONG_LEFT; | |
1283 | } | |
1284 | break; | |
1285 | ||
1286 | case '%': | |
1287 | action = ACT_RP_SHORT_RIGHT; | |
1288 | if (words[1 + *offset] == '%') | |
1289 | { | |
1290 | ++*offset; | |
1291 | action = ACT_RP_LONG_RIGHT; | |
1292 | } | |
1293 | break; | |
1294 | ||
1295 | case ':': | |
01d032e0 | 1296 | if (!CHAR_IN_SET (words[1 + *offset], "-=?+")) |
1f4a9ebd UD |
1297 | goto syntax; |
1298 | ||
1299 | colon_seen = 1; | |
1300 | action = words[++*offset]; | |
1301 | break; | |
1302 | ||
1303 | case '-': | |
1304 | case '=': | |
1305 | case '?': | |
1306 | case '+': | |
1307 | action = words[*offset]; | |
1308 | break; | |
1309 | ||
1310 | default: | |
1311 | goto syntax; | |
1312 | } | |
1313 | ||
bac660f8 | 1314 | /* Now collect the pattern, but don't expand it yet. */ |
1f4a9ebd UD |
1315 | ++*offset; |
1316 | for (; words[*offset]; ++(*offset)) | |
3c20b9b6 UD |
1317 | { |
1318 | switch (words[*offset]) | |
8f2ece69 | 1319 | { |
3c20b9b6 UD |
1320 | case '{': |
1321 | if (!pattern_is_quoted) | |
1322 | ++depth; | |
1323 | break; | |
8f2ece69 | 1324 | |
3c20b9b6 UD |
1325 | case '}': |
1326 | if (!pattern_is_quoted) | |
1327 | { | |
1328 | if (depth == 0) | |
1329 | goto envsubst; | |
1330 | --depth; | |
1331 | } | |
8f2ece69 | 1332 | break; |
8f2ece69 | 1333 | |
3c20b9b6 | 1334 | case '\\': |
bac660f8 UD |
1335 | if (pattern_is_quoted) |
1336 | /* Quoted; treat as normal character. */ | |
1337 | break; | |
1338 | ||
1339 | /* Otherwise, it's an escape: next character is literal. */ | |
1340 | if (words[++*offset] == '\0') | |
3c20b9b6 | 1341 | goto syntax; |
bac660f8 UD |
1342 | |
1343 | pattern = w_addchar (pattern, &pat_length, &pat_maxlen, '\\'); | |
1344 | if (pattern == NULL) | |
1345 | goto no_space; | |
1346 | ||
3c20b9b6 | 1347 | break; |
af6f3906 | 1348 | |
3c20b9b6 UD |
1349 | case '\'': |
1350 | if (pattern_is_quoted == 0) | |
1351 | pattern_is_quoted = 1; | |
1352 | else if (pattern_is_quoted == 1) | |
1353 | pattern_is_quoted = 0; | |
8f2ece69 | 1354 | |
3c20b9b6 | 1355 | break; |
8f2ece69 | 1356 | |
3c20b9b6 UD |
1357 | case '"': |
1358 | if (pattern_is_quoted == 0) | |
1359 | pattern_is_quoted = 2; | |
1360 | else if (pattern_is_quoted == 2) | |
1361 | pattern_is_quoted = 0; | |
8f2ece69 | 1362 | |
3c20b9b6 | 1363 | break; |
8f2ece69 UD |
1364 | } |
1365 | ||
3c20b9b6 UD |
1366 | pattern = w_addchar (pattern, &pat_length, &pat_maxlen, |
1367 | words[*offset]); | |
1368 | if (pattern == NULL) | |
1369 | goto no_space; | |
8f2ece69 UD |
1370 | } |
1371 | } | |
1372 | ||
1f4a9ebd UD |
1373 | /* End of input string -- remember to reparse the character that we |
1374 | * stopped at. */ | |
8f2ece69 UD |
1375 | --(*offset); |
1376 | ||
1377 | envsubst: | |
1378 | if (words[start] == '{' && words[*offset] != '}') | |
af6f3906 | 1379 | goto syntax; |
8f2ece69 | 1380 | |
1f4a9ebd | 1381 | if (env == NULL) |
8f2ece69 | 1382 | { |
14c44e2e UD |
1383 | if (seen_hash) |
1384 | { | |
1385 | /* $# expands to the number of positional parameters */ | |
14c44e2e | 1386 | buffer[20] = '\0'; |
1f4a9ebd UD |
1387 | value = _itoa_word (__libc_argc - 1, &buffer[20], 10, 0); |
1388 | seen_hash = 0; | |
14c44e2e UD |
1389 | } |
1390 | else | |
1391 | { | |
1392 | /* Just $ on its own */ | |
1393 | *offset = start - 1; | |
1394 | *word = w_addchar (*word, word_length, max_length, '$'); | |
1f4a9ebd | 1395 | return *word ? 0 : WRDE_NOSPACE; |
14c44e2e | 1396 | } |
8f2ece69 | 1397 | } |
e9fc7bbb | 1398 | /* Is it a numeric parameter? */ |
1f4a9ebd UD |
1399 | else if (isdigit (env[0])) |
1400 | { | |
5adda61f | 1401 | unsigned long n = strtoul (env, NULL, 10); |
8f2ece69 | 1402 | |
1f4a9ebd UD |
1403 | if (n >= __libc_argc) |
1404 | /* Substitute NULL. */ | |
1405 | value = NULL; | |
1406 | else | |
1407 | /* Replace with appropriate positional parameter. */ | |
1408 | value = __libc_argv[n]; | |
1409 | } | |
14c44e2e | 1410 | /* Is it a special parameter? */ |
1f4a9ebd | 1411 | else if (special) |
2bcf29ba | 1412 | { |
1f4a9ebd UD |
1413 | /* Is it `$$'? */ |
1414 | if (*env == '$') | |
14c44e2e | 1415 | { |
1f4a9ebd | 1416 | buffer[20] = '\0'; |
50304ef0 | 1417 | value = _itoa_word (__getpid (), &buffer[20], 10, 0); |
14c44e2e | 1418 | } |
1f4a9ebd UD |
1419 | /* Is it `${#*}' or `${#@}'? */ |
1420 | else if ((*env == '*' || *env == '@') && seen_hash) | |
2bcf29ba | 1421 | { |
1f4a9ebd UD |
1422 | buffer[20] = '\0'; |
1423 | value = _itoa_word (__libc_argc > 0 ? __libc_argc - 1 : 0, | |
1424 | &buffer[20], 10, 0); | |
1425 | *word = w_addstr (*word, word_length, max_length, value); | |
14c44e2e | 1426 | free (env); |
ecf359c6 | 1427 | free (pattern); |
14c44e2e UD |
1428 | return *word ? 0 : WRDE_NOSPACE; |
1429 | } | |
1430 | /* Is it `$*' or `$@' (unquoted) ? */ | |
1431 | else if (*env == '*' || (*env == '@' && !quoted)) | |
1432 | { | |
68b50604 | 1433 | size_t plist_len = 0; |
14c44e2e | 1434 | int p; |
68b50604 | 1435 | char *end; |
14c44e2e | 1436 | |
2bcf29ba | 1437 | /* Build up value parameter by parameter (copy them) */ |
68b50604 UD |
1438 | for (p = 1; __libc_argv[p]; ++p) |
1439 | plist_len += strlen (__libc_argv[p]) + 1; /* for space */ | |
1440 | value = malloc (plist_len); | |
1441 | if (value == NULL) | |
1442 | goto no_space; | |
1443 | end = value; | |
1444 | *end = 0; | |
8d9618b7 | 1445 | for (p = 1; __libc_argv[p]; ++p) |
2bcf29ba | 1446 | { |
68b50604 UD |
1447 | if (p > 1) |
1448 | *end++ = ' '; | |
1449 | end = __stpcpy (end, __libc_argv[p]); | |
2bcf29ba UD |
1450 | } |
1451 | ||
76fbcfdd | 1452 | free_value = 1; |
2bcf29ba | 1453 | } |
1f4a9ebd UD |
1454 | else |
1455 | { | |
1456 | /* Must be a quoted `$@' */ | |
1457 | assert (*env == '@' && quoted); | |
2bcf29ba | 1458 | |
1f4a9ebd UD |
1459 | /* Each parameter is a separate word ("$@") */ |
1460 | if (__libc_argc == 2) | |
68b50604 | 1461 | value = __libc_argv[1]; |
1f4a9ebd UD |
1462 | else if (__libc_argc > 2) |
1463 | { | |
1464 | int p; | |
14c44e2e | 1465 | |
1f4a9ebd UD |
1466 | /* Append first parameter to current word. */ |
1467 | value = w_addstr (*word, word_length, max_length, | |
1468 | __libc_argv[1]); | |
1469 | if (value == NULL || w_addword (pwordexp, value)) | |
1470 | goto no_space; | |
14c44e2e | 1471 | |
1f4a9ebd UD |
1472 | for (p = 2; __libc_argv[p + 1]; p++) |
1473 | { | |
1474 | char *newword = __strdup (__libc_argv[p]); | |
1475 | if (newword == NULL || w_addword (pwordexp, newword)) | |
1476 | goto no_space; | |
1477 | } | |
2bcf29ba | 1478 | |
1f4a9ebd | 1479 | /* Start a new word with the last parameter. */ |
e9fc7bbb | 1480 | *word = w_newword (word_length, max_length); |
fdacb17d | 1481 | value = __libc_argv[p]; |
1f4a9ebd UD |
1482 | } |
1483 | else | |
2bcf29ba | 1484 | { |
1f4a9ebd UD |
1485 | free (env); |
1486 | free (pattern); | |
1487 | return 0; | |
2bcf29ba UD |
1488 | } |
1489 | } | |
2bcf29ba | 1490 | } |
1f4a9ebd | 1491 | else |
68b50604 UD |
1492 | value = getenv (env); |
1493 | ||
1494 | if (value == NULL && (flags & WRDE_UNDEF)) | |
1f4a9ebd | 1495 | { |
68b50604 | 1496 | /* Variable not defined. */ |
33c3e20e UD |
1497 | error = WRDE_BADVAL; |
1498 | goto do_error; | |
1f4a9ebd | 1499 | } |
8f2ece69 | 1500 | |
3c20b9b6 | 1501 | if (action != ACT_NONE) |
8f2ece69 | 1502 | { |
bac660f8 UD |
1503 | int expand_pattern = 0; |
1504 | ||
1505 | /* First, find out if we need to expand pattern (i.e. if we will | |
1506 | * use it). */ | |
1507 | switch (action) | |
1508 | { | |
1509 | case ACT_RP_SHORT_LEFT: | |
1510 | case ACT_RP_LONG_LEFT: | |
1511 | case ACT_RP_SHORT_RIGHT: | |
1512 | case ACT_RP_LONG_RIGHT: | |
1513 | /* Always expand for these. */ | |
1514 | expand_pattern = 1; | |
1515 | break; | |
1516 | ||
1517 | case ACT_NULL_ERROR: | |
1518 | case ACT_NULL_SUBST: | |
1519 | case ACT_NULL_ASSIGN: | |
1520 | if (!value || (!*value && colon_seen)) | |
1521 | /* If param is unset, or set but null and a colon has been seen, | |
1522 | the expansion of the pattern will be needed. */ | |
1523 | expand_pattern = 1; | |
1524 | ||
1525 | break; | |
1526 | ||
1527 | case ACT_NONNULL_SUBST: | |
1528 | /* Expansion of word will be needed if parameter is set and not null, | |
1529 | or set null but no colon has been seen. */ | |
1530 | if (value && (*value || !colon_seen)) | |
1531 | expand_pattern = 1; | |
1532 | ||
1533 | break; | |
1534 | ||
1535 | default: | |
1536 | assert (! "Unrecognised action!"); | |
1537 | } | |
1538 | ||
1539 | if (expand_pattern) | |
1540 | { | |
1541 | /* We need to perform tilde expansion, parameter expansion, | |
3ce1f295 | 1542 | command substitution, and arithmetic expansion. We also |
bac660f8 UD |
1543 | have to be a bit careful with wildcard characters, as |
1544 | pattern might be given to fnmatch soon. To do this, we | |
1545 | convert quotes to escapes. */ | |
1546 | ||
1547 | char *expanded; | |
1548 | size_t exp_len; | |
1549 | size_t exp_maxl; | |
1550 | char *p; | |
1551 | int quoted = 0; /* 1: single quotes; 2: double */ | |
1552 | ||
1553 | expanded = w_newword (&exp_len, &exp_maxl); | |
1554 | for (p = pattern; p && *p; p++) | |
1555 | { | |
4ad1f026 | 1556 | size_t offset; |
bac660f8 UD |
1557 | |
1558 | switch (*p) | |
1559 | { | |
1560 | case '"': | |
1561 | if (quoted == 2) | |
1562 | quoted = 0; | |
1563 | else if (quoted == 0) | |
1564 | quoted = 2; | |
1565 | else break; | |
1566 | ||
1567 | continue; | |
1568 | ||
1569 | case '\'': | |
1570 | if (quoted == 1) | |
1571 | quoted = 0; | |
1572 | else if (quoted == 0) | |
1573 | quoted = 1; | |
1574 | else break; | |
1575 | ||
1576 | continue; | |
1577 | ||
1578 | case '*': | |
1579 | case '?': | |
1580 | if (quoted) | |
1581 | { | |
1582 | /* Convert quoted wildchar to escaped wildchar. */ | |
1583 | expanded = w_addchar (expanded, &exp_len, | |
1584 | &exp_maxl, '\\'); | |
1585 | ||
1586 | if (expanded == NULL) | |
1587 | goto no_space; | |
1588 | } | |
1589 | break; | |
1590 | ||
1591 | case '$': | |
1592 | offset = 0; | |
1593 | error = parse_dollars (&expanded, &exp_len, &exp_maxl, p, | |
1594 | &offset, flags, NULL, NULL, NULL, 1); | |
1595 | if (error) | |
1596 | { | |
1597 | if (free_value) | |
1598 | free (value); | |
1599 | ||
ecf359c6 | 1600 | free (expanded); |
bac660f8 UD |
1601 | |
1602 | goto do_error; | |
1603 | } | |
1604 | ||
1605 | p += offset; | |
1606 | continue; | |
1607 | ||
1608 | case '~': | |
1609 | if (quoted || exp_len) | |
1610 | break; | |
1611 | ||
1612 | offset = 0; | |
1613 | error = parse_tilde (&expanded, &exp_len, &exp_maxl, p, | |
1614 | &offset, 0); | |
1615 | if (error) | |
1616 | { | |
1617 | if (free_value) | |
1618 | free (value); | |
1619 | ||
ecf359c6 | 1620 | free (expanded); |
bac660f8 UD |
1621 | |
1622 | goto do_error; | |
1623 | } | |
1624 | ||
1625 | p += offset; | |
1626 | continue; | |
1627 | ||
1628 | case '\\': | |
1629 | expanded = w_addchar (expanded, &exp_len, &exp_maxl, '\\'); | |
1630 | ++p; | |
1631 | assert (*p); /* checked when extracted initially */ | |
1632 | if (expanded == NULL) | |
1633 | goto no_space; | |
1634 | } | |
1635 | ||
1636 | expanded = w_addchar (expanded, &exp_len, &exp_maxl, *p); | |
1637 | ||
1638 | if (expanded == NULL) | |
1639 | goto no_space; | |
1640 | } | |
1641 | ||
ecf359c6 | 1642 | free (pattern); |
bac660f8 UD |
1643 | |
1644 | pattern = expanded; | |
1645 | } | |
1646 | ||
8f2ece69 UD |
1647 | switch (action) |
1648 | { | |
3c20b9b6 UD |
1649 | case ACT_RP_SHORT_LEFT: |
1650 | case ACT_RP_LONG_LEFT: | |
1651 | case ACT_RP_SHORT_RIGHT: | |
1652 | case ACT_RP_LONG_RIGHT: | |
af6f3906 UD |
1653 | { |
1654 | char *p; | |
1655 | char c; | |
1656 | char *end; | |
1657 | ||
1f4a9ebd | 1658 | if (value == NULL || pattern == NULL || *pattern == '\0') |
af6f3906 UD |
1659 | break; |
1660 | ||
1661 | end = value + strlen (value); | |
1662 | ||
3c20b9b6 | 1663 | switch (action) |
af6f3906 | 1664 | { |
3c20b9b6 | 1665 | case ACT_RP_SHORT_LEFT: |
3db52d94 | 1666 | for (p = value; p <= end; ++p) |
af6f3906 UD |
1667 | { |
1668 | c = *p; | |
1669 | *p = '\0'; | |
1670 | if (fnmatch (pattern, value, 0) != FNM_NOMATCH) | |
1671 | { | |
1672 | *p = c; | |
68b50604 UD |
1673 | if (free_value) |
1674 | { | |
1675 | char *newval = __strdup (p); | |
1676 | if (newval == NULL) | |
1677 | { | |
1678 | free (value); | |
1679 | goto no_space; | |
1680 | } | |
1681 | free (value); | |
1682 | value = newval; | |
1683 | } | |
1684 | else | |
1685 | value = p; | |
af6f3906 UD |
1686 | break; |
1687 | } | |
1688 | *p = c; | |
1689 | } | |
1690 | ||
1691 | break; | |
1692 | ||
3c20b9b6 | 1693 | case ACT_RP_LONG_LEFT: |
3db52d94 | 1694 | for (p = end; p >= value; --p) |
af6f3906 UD |
1695 | { |
1696 | c = *p; | |
1697 | *p = '\0'; | |
1698 | if (fnmatch (pattern, value, 0) != FNM_NOMATCH) | |
1699 | { | |
1700 | *p = c; | |
68b50604 UD |
1701 | if (free_value) |
1702 | { | |
1703 | char *newval = __strdup (p); | |
1704 | if (newval == NULL) | |
1705 | { | |
1706 | free (value); | |
1707 | goto no_space; | |
1708 | } | |
1709 | free (value); | |
1710 | value = newval; | |
1711 | } | |
1712 | else | |
1713 | value = p; | |
af6f3906 UD |
1714 | break; |
1715 | } | |
1716 | *p = c; | |
1717 | } | |
1718 | ||
1719 | break; | |
1720 | ||
3c20b9b6 | 1721 | case ACT_RP_SHORT_RIGHT: |
3db52d94 | 1722 | for (p = end; p >= value; --p) |
af6f3906 UD |
1723 | { |
1724 | if (fnmatch (pattern, p, 0) != FNM_NOMATCH) | |
1725 | { | |
1f4a9ebd UD |
1726 | char *newval; |
1727 | newval = malloc (p - value + 1); | |
33c3e20e | 1728 | |
1f4a9ebd | 1729 | if (newval == NULL) |
33c3e20e UD |
1730 | { |
1731 | if (free_value) | |
1732 | free (value); | |
1733 | goto no_space; | |
1734 | } | |
1735 | ||
1f4a9ebd UD |
1736 | *(char *) __mempcpy (newval, value, p - value) = '\0'; |
1737 | if (free_value) | |
1738 | free (value); | |
1739 | value = newval; | |
1740 | free_value = 1; | |
af6f3906 UD |
1741 | break; |
1742 | } | |
1743 | } | |
1744 | ||
1745 | break; | |
1746 | ||
3c20b9b6 | 1747 | case ACT_RP_LONG_RIGHT: |
3db52d94 | 1748 | for (p = value; p <= end; ++p) |
af6f3906 UD |
1749 | { |
1750 | if (fnmatch (pattern, p, 0) != FNM_NOMATCH) | |
1751 | { | |
1f4a9ebd UD |
1752 | char *newval; |
1753 | newval = malloc (p - value + 1); | |
33c3e20e | 1754 | |
1f4a9ebd | 1755 | if (newval == NULL) |
33c3e20e UD |
1756 | { |
1757 | if (free_value) | |
1758 | free (value); | |
1759 | goto no_space; | |
1760 | } | |
1761 | ||
1f4a9ebd UD |
1762 | *(char *) __mempcpy (newval, value, p - value) = '\0'; |
1763 | if (free_value) | |
1764 | free (value); | |
1765 | value = newval; | |
1766 | free_value = 1; | |
af6f3906 UD |
1767 | break; |
1768 | } | |
1769 | } | |
1770 | ||
1771 | break; | |
1772 | ||
1773 | default: | |
3c20b9b6 | 1774 | break; |
af6f3906 UD |
1775 | } |
1776 | ||
1777 | break; | |
1778 | } | |
8f2ece69 | 1779 | |
3c20b9b6 | 1780 | case ACT_NULL_ERROR: |
8f2ece69 | 1781 | if (value && *value) |
3db52d94 | 1782 | /* Substitute parameter */ |
8f2ece69 UD |
1783 | break; |
1784 | ||
bac660f8 | 1785 | error = 0; |
8f2ece69 | 1786 | if (!colon_seen && value) |
1f4a9ebd | 1787 | /* Substitute NULL */ |
bac660f8 | 1788 | ; |
1f4a9ebd UD |
1789 | else |
1790 | { | |
51028f34 UD |
1791 | const char *str = pattern; |
1792 | ||
1793 | if (str[0] == '\0') | |
6293b803 | 1794 | str = _("parameter null or not set"); |
51028f34 | 1795 | |
8a259a23 | 1796 | __fxprintf (NULL, "%s: %s\n", env, str); |
8f2ece69 UD |
1797 | } |
1798 | ||
68b50604 UD |
1799 | if (free_value) |
1800 | free (value); | |
33c3e20e | 1801 | goto do_error; |
8f2ece69 | 1802 | |
3c20b9b6 | 1803 | case ACT_NULL_SUBST: |
3db52d94 UD |
1804 | if (value && *value) |
1805 | /* Substitute parameter */ | |
1806 | break; | |
1807 | ||
11bf311e | 1808 | if (free_value) |
bac660f8 | 1809 | free (value); |
3db52d94 | 1810 | |
bac660f8 UD |
1811 | if (!colon_seen && value) |
1812 | /* Substitute NULL */ | |
1813 | goto success; | |
1f4a9ebd | 1814 | |
bac660f8 UD |
1815 | value = pattern ? __strdup (pattern) : pattern; |
1816 | free_value = 1; | |
3db52d94 | 1817 | |
bac660f8 UD |
1818 | if (pattern && !value) |
1819 | goto no_space; | |
3db52d94 | 1820 | |
bac660f8 | 1821 | break; |
8d9618b7 | 1822 | |
bac660f8 UD |
1823 | case ACT_NONNULL_SUBST: |
1824 | if (value && (*value || !colon_seen)) | |
1825 | { | |
11bf311e | 1826 | if (free_value) |
bac660f8 | 1827 | free (value); |
8d9618b7 | 1828 | |
bac660f8 UD |
1829 | value = pattern ? __strdup (pattern) : pattern; |
1830 | free_value = 1; | |
3db52d94 | 1831 | |
bac660f8 UD |
1832 | if (pattern && !value) |
1833 | goto no_space; | |
3db52d94 | 1834 | |
bac660f8 UD |
1835 | break; |
1836 | } | |
3db52d94 UD |
1837 | |
1838 | /* Substitute NULL */ | |
68b50604 UD |
1839 | if (free_value) |
1840 | free (value); | |
33c3e20e | 1841 | goto success; |
3db52d94 | 1842 | |
3c20b9b6 | 1843 | case ACT_NULL_ASSIGN: |
3db52d94 UD |
1844 | if (value && *value) |
1845 | /* Substitute parameter */ | |
1846 | break; | |
1847 | ||
1848 | if (!colon_seen && value) | |
1849 | { | |
1850 | /* Substitute NULL */ | |
68b50604 UD |
1851 | if (free_value) |
1852 | free (value); | |
33c3e20e | 1853 | goto success; |
3db52d94 UD |
1854 | } |
1855 | ||
ecf359c6 | 1856 | if (free_value) |
bac660f8 UD |
1857 | free (value); |
1858 | ||
1859 | value = pattern ? __strdup (pattern) : pattern; | |
1860 | free_value = 1; | |
1861 | ||
1862 | if (pattern && !value) | |
1863 | goto no_space; | |
1864 | ||
a2d4cf72 | 1865 | __setenv (env, value ?: "", 1); |
bac660f8 | 1866 | break; |
3db52d94 | 1867 | |
8f2ece69 | 1868 | default: |
3db52d94 | 1869 | assert (! "Unrecognised action!"); |
8f2ece69 UD |
1870 | } |
1871 | } | |
1872 | ||
ecf359c6 UD |
1873 | free (env); |
1874 | env = NULL; | |
1875 | free (pattern); | |
1876 | pattern = NULL; | |
8f2ece69 | 1877 | |
14c44e2e | 1878 | if (seen_hash) |
3db52d94 UD |
1879 | { |
1880 | char param_length[21]; | |
1881 | param_length[20] = '\0'; | |
1882 | *word = w_addstr (*word, word_length, max_length, | |
1f4a9ebd UD |
1883 | _itoa_word (value ? strlen (value) : 0, |
1884 | ¶m_length[20], 10, 0)); | |
1885 | if (free_value) | |
1886 | { | |
1887 | assert (value != NULL); | |
1888 | free (value); | |
1889 | } | |
1890 | ||
3db52d94 UD |
1891 | return *word ? 0 : WRDE_NOSPACE; |
1892 | } | |
1893 | ||
1f4a9ebd UD |
1894 | if (value == NULL) |
1895 | return 0; | |
1896 | ||
9b3c7c3c UD |
1897 | if (quoted || !pwordexp) |
1898 | { | |
1899 | /* Quoted - no field split */ | |
1900 | *word = w_addstr (*word, word_length, max_length, value); | |
1901 | if (free_value) | |
1902 | free (value); | |
af69217f | 1903 | |
9b3c7c3c UD |
1904 | return *word ? 0 : WRDE_NOSPACE; |
1905 | } | |
1906 | else | |
1907 | { | |
1908 | /* Need to field-split */ | |
1909 | char *value_copy = __strdup (value); /* Don't modify value */ | |
1910 | char *field_begin = value_copy; | |
1911 | int seen_nonws_ifs = 0; | |
1912 | ||
1913 | if (free_value) | |
1914 | free (value); | |
1915 | ||
1916 | if (value_copy == NULL) | |
1917 | goto no_space; | |
1918 | ||
1919 | do | |
1920 | { | |
1921 | char *field_end = field_begin; | |
1922 | char *next_field; | |
1923 | ||
1924 | /* If this isn't the first field, start a new word */ | |
1925 | if (field_begin != value_copy) | |
1926 | { | |
1927 | if (w_addword (pwordexp, *word) == WRDE_NOSPACE) | |
1928 | { | |
1929 | free (value_copy); | |
1930 | goto no_space; | |
1931 | } | |
1932 | ||
1933 | *word = w_newword (word_length, max_length); | |
1934 | } | |
1935 | ||
1936 | /* Skip IFS whitespace before the field */ | |
1937 | field_begin += strspn (field_begin, ifs_white); | |
1938 | ||
1939 | if (!seen_nonws_ifs && *field_begin == 0) | |
1940 | /* Nothing but whitespace */ | |
1941 | break; | |
1942 | ||
1943 | /* Search for the end of the field */ | |
1944 | field_end = field_begin + strcspn (field_begin, ifs); | |
1945 | ||
1946 | /* Set up pointer to the character after end of field and | |
3ce1f295 | 1947 | skip whitespace IFS after it. */ |
9b3c7c3c UD |
1948 | next_field = field_end + strspn (field_end, ifs_white); |
1949 | ||
1950 | /* Skip at most one non-whitespace IFS character after the field */ | |
1951 | seen_nonws_ifs = 0; | |
1952 | if (*next_field && strchr (ifs, *next_field)) | |
1953 | { | |
1954 | seen_nonws_ifs = 1; | |
1955 | next_field++; | |
1956 | } | |
1957 | ||
1958 | /* Null-terminate it */ | |
1959 | *field_end = 0; | |
1960 | ||
1961 | /* Tag a copy onto the current word */ | |
1962 | *word = w_addstr (*word, word_length, max_length, field_begin); | |
1963 | ||
1964 | if (*word == NULL && *field_begin != '\0') | |
1965 | { | |
1966 | free (value_copy); | |
1967 | goto no_space; | |
1968 | } | |
1969 | ||
1970 | field_begin = next_field; | |
1971 | } | |
1972 | while (seen_nonws_ifs || *field_begin); | |
1973 | ||
1974 | free (value_copy); | |
1975 | } | |
1976 | ||
1977 | return 0; | |
8f2ece69 | 1978 | |
33c3e20e UD |
1979 | success: |
1980 | error = 0; | |
1981 | goto do_error; | |
8f2ece69 | 1982 | |
33c3e20e UD |
1983 | no_space: |
1984 | error = WRDE_NOSPACE; | |
1985 | goto do_error; | |
af6f3906 UD |
1986 | |
1987 | syntax: | |
33c3e20e UD |
1988 | error = WRDE_SYNTAX; |
1989 | ||
1990 | do_error: | |
ecf359c6 | 1991 | free (env); |
af6f3906 | 1992 | |
ecf359c6 | 1993 | free (pattern); |
af6f3906 | 1994 | |
33c3e20e | 1995 | return error; |
8f2ece69 UD |
1996 | } |
1997 | ||
01d032e0 PP |
1998 | #undef CHAR_IN_SET |
1999 | ||
8f2ece69 | 2000 | static int |
40a55d20 UD |
2001 | parse_dollars (char **word, size_t *word_length, size_t *max_length, |
2002 | const char *words, size_t *offset, int flags, | |
2bcf29ba | 2003 | wordexp_t *pwordexp, const char *ifs, const char *ifs_white, |
9b3c7c3c | 2004 | int quoted) |
8f2ece69 UD |
2005 | { |
2006 | /* We are poised _at_ "$" */ | |
2007 | switch (words[1 + *offset]) | |
28f540f4 | 2008 | { |
8f2ece69 UD |
2009 | case '"': |
2010 | case '\'': | |
2011 | case 0: | |
40a55d20 | 2012 | *word = w_addchar (*word, word_length, max_length, '$'); |
8f2ece69 UD |
2013 | return *word ? 0 : WRDE_NOSPACE; |
2014 | ||
2015 | case '(': | |
2016 | if (words[2 + *offset] == '(') | |
2017 | { | |
2bcf29ba UD |
2018 | /* Differentiate between $((1+3)) and $((echo);(ls)) */ |
2019 | int i = 3 + *offset; | |
3c20b9b6 UD |
2020 | int depth = 0; |
2021 | while (words[i] && !(depth == 0 && words[i] == ')')) | |
2022 | { | |
2023 | if (words[i] == '(') | |
2024 | ++depth; | |
2025 | else if (words[i] == ')') | |
2026 | --depth; | |
2027 | ||
2028 | ++i; | |
2029 | } | |
2030 | ||
2bcf29ba UD |
2031 | if (words[i] == ')' && words[i + 1] == ')') |
2032 | { | |
2033 | (*offset) += 3; | |
2034 | /* Call parse_arith -- 0 is for "no brackets" */ | |
2035 | return parse_arith (word, word_length, max_length, words, offset, | |
2036 | flags, 0); | |
2037 | } | |
8f2ece69 UD |
2038 | } |
2039 | ||
8f2ece69 | 2040 | (*offset) += 2; |
40a55d20 | 2041 | return parse_comm (word, word_length, max_length, words, offset, flags, |
8d9618b7 | 2042 | quoted? NULL : pwordexp, ifs, ifs_white); |
8f2ece69 UD |
2043 | |
2044 | case '[': | |
2045 | (*offset) += 2; | |
2046 | /* Call parse_arith -- 1 is for "brackets" */ | |
40a55d20 UD |
2047 | return parse_arith (word, word_length, max_length, words, offset, flags, |
2048 | 1); | |
8f2ece69 UD |
2049 | |
2050 | case '{': | |
2051 | default: | |
2052 | ++(*offset); /* parse_param needs to know if "{" is there */ | |
40a55d20 | 2053 | return parse_param (word, word_length, max_length, words, offset, flags, |
2bcf29ba | 2054 | pwordexp, ifs, ifs_white, quoted); |
28f540f4 | 2055 | } |
8f2ece69 | 2056 | } |
28f540f4 | 2057 | |
8f2ece69 | 2058 | static int |
40a55d20 UD |
2059 | parse_backtick (char **word, size_t *word_length, size_t *max_length, |
2060 | const char *words, size_t *offset, int flags, | |
af69217f | 2061 | wordexp_t *pwordexp, const char *ifs, const char *ifs_white) |
8f2ece69 UD |
2062 | { |
2063 | /* We are poised just after "`" */ | |
2064 | int error; | |
8f2ece69 | 2065 | int squoting = 0; |
e9fc7bbb UD |
2066 | size_t comm_length; |
2067 | size_t comm_maxlen; | |
2068 | char *comm = w_newword (&comm_length, &comm_maxlen); | |
8f2ece69 UD |
2069 | |
2070 | for (; words[*offset]; ++(*offset)) | |
28f540f4 | 2071 | { |
8f2ece69 UD |
2072 | switch (words[*offset]) |
2073 | { | |
2074 | case '`': | |
2075 | /* Go -- give the script to the shell */ | |
40a55d20 | 2076 | error = exec_comm (comm, word, word_length, max_length, flags, |
af69217f | 2077 | pwordexp, ifs, ifs_white); |
8f2ece69 UD |
2078 | free (comm); |
2079 | return error; | |
2080 | ||
2081 | case '\\': | |
2082 | if (squoting) | |
2083 | { | |
40a55d20 UD |
2084 | error = parse_qtd_backslash (&comm, &comm_length, &comm_maxlen, |
2085 | words, offset); | |
8f2ece69 UD |
2086 | |
2087 | if (error) | |
2088 | { | |
2089 | free (comm); | |
2090 | return error; | |
2091 | } | |
2092 | ||
2093 | break; | |
2094 | } | |
2095 | ||
40a55d20 UD |
2096 | error = parse_backslash (&comm, &comm_length, &comm_maxlen, words, |
2097 | offset); | |
8f2ece69 UD |
2098 | |
2099 | if (error) | |
2100 | { | |
2101 | free (comm); | |
2102 | return error; | |
2103 | } | |
2104 | ||
2105 | break; | |
2106 | ||
2107 | case '\'': | |
2108 | squoting = 1 - squoting; | |
32db86d5 | 2109 | /* Fall through. */ |
8f2ece69 | 2110 | default: |
40a55d20 | 2111 | comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]); |
8f2ece69 UD |
2112 | if (comm == NULL) |
2113 | return WRDE_NOSPACE; | |
2114 | } | |
28f540f4 RM |
2115 | } |
2116 | ||
8f2ece69 UD |
2117 | /* Premature end */ |
2118 | free (comm); | |
2119 | return WRDE_SYNTAX; | |
2120 | } | |
28f540f4 | 2121 | |
8f2ece69 | 2122 | static int |
40a55d20 | 2123 | parse_dquote (char **word, size_t *word_length, size_t *max_length, |
2bcf29ba UD |
2124 | const char *words, size_t *offset, int flags, |
2125 | wordexp_t *pwordexp, const char * ifs, const char * ifs_white) | |
8f2ece69 UD |
2126 | { |
2127 | /* We are poised just after a double-quote */ | |
2128 | int error; | |
2129 | ||
2130 | for (; words[*offset]; ++(*offset)) | |
2131 | { | |
2132 | switch (words[*offset]) | |
2133 | { | |
2134 | case '"': | |
2135 | return 0; | |
2136 | ||
2137 | case '$': | |
40a55d20 | 2138 | error = parse_dollars (word, word_length, max_length, words, offset, |
9b3c7c3c | 2139 | flags, pwordexp, ifs, ifs_white, 1); |
2bcf29ba UD |
2140 | /* The ``1'' here is to tell parse_dollars not to |
2141 | * split the fields. It may need to, however ("$@"). | |
8f2ece69 UD |
2142 | */ |
2143 | if (error) | |
2144 | return error; | |
28f540f4 | 2145 | |
8f2ece69 UD |
2146 | break; |
2147 | ||
2148 | case '`': | |
8f2ece69 | 2149 | ++(*offset); |
40a55d20 | 2150 | error = parse_backtick (word, word_length, max_length, words, |
af69217f UD |
2151 | offset, flags, NULL, NULL, NULL); |
2152 | /* The first NULL here is to tell parse_backtick not to | |
8f2ece69 UD |
2153 | * split the fields. |
2154 | */ | |
2155 | if (error) | |
2156 | return error; | |
2157 | ||
2158 | break; | |
2159 | ||
2160 | case '\\': | |
40a55d20 UD |
2161 | error = parse_qtd_backslash (word, word_length, max_length, words, |
2162 | offset); | |
8f2ece69 UD |
2163 | |
2164 | if (error) | |
2165 | return error; | |
2166 | ||
2167 | break; | |
2168 | ||
2169 | default: | |
40a55d20 | 2170 | *word = w_addchar (*word, word_length, max_length, words[*offset]); |
8f2ece69 UD |
2171 | if (*word == NULL) |
2172 | return WRDE_NOSPACE; | |
2173 | } | |
2174 | } | |
2175 | ||
2176 | /* Unterminated string */ | |
2177 | return WRDE_SYNTAX; | |
28f540f4 RM |
2178 | } |
2179 | ||
8f2ece69 UD |
2180 | /* |
2181 | * wordfree() is to be called after pwordexp is finished with. | |
2182 | */ | |
28f540f4 RM |
2183 | |
2184 | void | |
8f2ece69 | 2185 | wordfree (wordexp_t *pwordexp) |
28f540f4 | 2186 | { |
8f2ece69 UD |
2187 | |
2188 | /* wordexp can set pwordexp to NULL */ | |
2189 | if (pwordexp && pwordexp->we_wordv) | |
2190 | { | |
2191 | char **wordv = pwordexp->we_wordv; | |
2192 | ||
2193 | for (wordv += pwordexp->we_offs; *wordv; ++wordv) | |
2194 | free (*wordv); | |
2195 | ||
2196 | free (pwordexp->we_wordv); | |
2197 | pwordexp->we_wordv = NULL; | |
2198 | } | |
2199 | } | |
c41f555e | 2200 | libc_hidden_def (wordfree) |
8f2ece69 UD |
2201 | |
2202 | /* | |
2203 | * wordexp() | |
2204 | */ | |
2205 | ||
2206 | int | |
2207 | wordexp (const char *words, wordexp_t *pwordexp, int flags) | |
2208 | { | |
8f2ece69 | 2209 | size_t words_offset; |
e9fc7bbb UD |
2210 | size_t word_length; |
2211 | size_t max_length; | |
2212 | char *word = w_newword (&word_length, &max_length); | |
40a55d20 | 2213 | int error; |
8f2ece69 UD |
2214 | char *ifs; |
2215 | char ifs_white[4]; | |
05d5dedc | 2216 | wordexp_t old_word = *pwordexp; |
8f2ece69 UD |
2217 | |
2218 | if (flags & WRDE_REUSE) | |
22bc7978 UD |
2219 | { |
2220 | /* Minimal implementation of WRDE_REUSE for now */ | |
2221 | wordfree (pwordexp); | |
05d5dedc | 2222 | old_word.we_wordv = NULL; |
22bc7978 | 2223 | } |
8f2ece69 | 2224 | |
05d5dedc | 2225 | if ((flags & WRDE_APPEND) == 0) |
8f2ece69 | 2226 | { |
05d5dedc UD |
2227 | pwordexp->we_wordc = 0; |
2228 | ||
2229 | if (flags & WRDE_DOOFFS) | |
33c3e20e | 2230 | { |
05d5dedc UD |
2231 | pwordexp->we_wordv = calloc (1 + pwordexp->we_offs, sizeof (char *)); |
2232 | if (pwordexp->we_wordv == NULL) | |
2233 | { | |
2234 | error = WRDE_NOSPACE; | |
2235 | goto do_error; | |
2236 | } | |
33c3e20e | 2237 | } |
05d5dedc | 2238 | else |
33c3e20e | 2239 | { |
05d5dedc UD |
2240 | pwordexp->we_wordv = calloc (1, sizeof (char *)); |
2241 | if (pwordexp->we_wordv == NULL) | |
2242 | { | |
2243 | error = WRDE_NOSPACE; | |
2244 | goto do_error; | |
2245 | } | |
8f2ece69 | 2246 | |
05d5dedc UD |
2247 | pwordexp->we_offs = 0; |
2248 | } | |
8f2ece69 UD |
2249 | } |
2250 | ||
8f2ece69 UD |
2251 | /* Find out what the field separators are. |
2252 | * There are two types: whitespace and non-whitespace. | |
2253 | */ | |
2254 | ifs = getenv ("IFS"); | |
2255 | ||
16d620db | 2256 | if (ifs == NULL) |
6796bc80 UD |
2257 | /* IFS unset - use <space><tab><newline>. */ |
2258 | ifs = strcpy (ifs_white, " \t\n"); | |
8f2ece69 UD |
2259 | else |
2260 | { | |
2261 | char *ifsch = ifs; | |
2262 | char *whch = ifs_white; | |
2263 | ||
2264 | while (*ifsch != '\0') | |
af69217f | 2265 | { |
16d620db | 2266 | if (*ifsch == ' ' || *ifsch == '\t' || *ifsch == '\n') |
af69217f UD |
2267 | { |
2268 | /* Whitespace IFS. See first whether it is already in our | |
2269 | collection. */ | |
2270 | char *runp = ifs_white; | |
8f2ece69 | 2271 | |
16d620db | 2272 | while (runp < whch && *runp != *ifsch) |
af69217f | 2273 | ++runp; |
8f2ece69 | 2274 | |
af69217f UD |
2275 | if (runp == whch) |
2276 | *whch++ = *ifsch; | |
2277 | } | |
2278 | ||
3db52d94 | 2279 | ++ifsch; |
af69217f | 2280 | } |
8f2ece69 UD |
2281 | *whch = '\0'; |
2282 | } | |
2283 | ||
9b3c7c3c | 2284 | for (words_offset = 0 ; words[words_offset] ; ++words_offset) |
8f2ece69 UD |
2285 | switch (words[words_offset]) |
2286 | { | |
8f2ece69 | 2287 | case '\\': |
40a55d20 UD |
2288 | error = parse_backslash (&word, &word_length, &max_length, words, |
2289 | &words_offset); | |
8f2ece69 UD |
2290 | |
2291 | if (error) | |
2292 | goto do_error; | |
2293 | ||
2294 | break; | |
2295 | ||
2296 | case '$': | |
40a55d20 | 2297 | error = parse_dollars (&word, &word_length, &max_length, words, |
2bcf29ba | 2298 | &words_offset, flags, pwordexp, ifs, ifs_white, |
9b3c7c3c | 2299 | 0); |
8f2ece69 UD |
2300 | |
2301 | if (error) | |
2302 | goto do_error; | |
2303 | ||
2304 | break; | |
2305 | ||
2306 | case '`': | |
8f2ece69 | 2307 | ++words_offset; |
40a55d20 | 2308 | error = parse_backtick (&word, &word_length, &max_length, words, |
af69217f UD |
2309 | &words_offset, flags, pwordexp, ifs, |
2310 | ifs_white); | |
8f2ece69 UD |
2311 | |
2312 | if (error) | |
2313 | goto do_error; | |
2314 | ||
2315 | break; | |
2316 | ||
2317 | case '"': | |
2318 | ++words_offset; | |
40a55d20 | 2319 | error = parse_dquote (&word, &word_length, &max_length, words, |
2bcf29ba | 2320 | &words_offset, flags, pwordexp, ifs, ifs_white); |
8f2ece69 UD |
2321 | |
2322 | if (error) | |
2323 | goto do_error; | |
2324 | ||
a9c27b3e UD |
2325 | if (!word_length) |
2326 | { | |
2327 | error = w_addword (pwordexp, NULL); | |
2328 | ||
2329 | if (error) | |
2330 | return error; | |
2331 | } | |
2332 | ||
8f2ece69 UD |
2333 | break; |
2334 | ||
2335 | case '\'': | |
2336 | ++words_offset; | |
40a55d20 UD |
2337 | error = parse_squote (&word, &word_length, &max_length, words, |
2338 | &words_offset); | |
8f2ece69 UD |
2339 | |
2340 | if (error) | |
2341 | goto do_error; | |
2342 | ||
a9c27b3e UD |
2343 | if (!word_length) |
2344 | { | |
2345 | error = w_addword (pwordexp, NULL); | |
2346 | ||
2347 | if (error) | |
2348 | return error; | |
2349 | } | |
2350 | ||
8f2ece69 UD |
2351 | break; |
2352 | ||
2353 | case '~': | |
40a55d20 UD |
2354 | error = parse_tilde (&word, &word_length, &max_length, words, |
2355 | &words_offset, pwordexp->we_wordc); | |
8f2ece69 UD |
2356 | |
2357 | if (error) | |
2358 | goto do_error; | |
2359 | ||
2360 | break; | |
2361 | ||
2362 | case '*': | |
2bcf29ba UD |
2363 | case '[': |
2364 | case '?': | |
40a55d20 | 2365 | error = parse_glob (&word, &word_length, &max_length, words, |
af69217f | 2366 | &words_offset, flags, pwordexp, ifs, ifs_white); |
8f2ece69 UD |
2367 | |
2368 | if (error) | |
2369 | goto do_error; | |
2370 | ||
2371 | break; | |
2372 | ||
2373 | default: | |
c06cc21c | 2374 | /* Is it a word separator? */ |
9b3c7c3c | 2375 | if (strchr (" \t", words[words_offset]) == NULL) |
8f2ece69 | 2376 | { |
c06cc21c UD |
2377 | char ch = words[words_offset]; |
2378 | ||
2379 | /* Not a word separator -- but is it a valid word char? */ | |
2380 | if (strchr ("\n|&;<>(){}", ch)) | |
6796bc80 UD |
2381 | { |
2382 | /* Fail */ | |
bac660f8 UD |
2383 | error = WRDE_BADCHAR; |
2384 | goto do_error; | |
6796bc80 UD |
2385 | } |
2386 | ||
8f2ece69 | 2387 | /* "Ordinary" character -- add it to word */ |
40a55d20 | 2388 | word = w_addchar (word, &word_length, &max_length, |
c06cc21c | 2389 | ch); |
8f2ece69 UD |
2390 | if (word == NULL) |
2391 | { | |
2392 | error = WRDE_NOSPACE; | |
2393 | goto do_error; | |
2394 | } | |
2395 | ||
2396 | break; | |
2397 | } | |
2398 | ||
c06cc21c | 2399 | /* If a word has been delimited, add it to the list. */ |
40a55d20 | 2400 | if (word != NULL) |
8f2ece69 | 2401 | { |
9b3c7c3c | 2402 | error = w_addword (pwordexp, word); |
c06cc21c UD |
2403 | if (error) |
2404 | goto do_error; | |
8f2ece69 UD |
2405 | } |
2406 | ||
a8125d85 UD |
2407 | word = w_newword (&word_length, &max_length); |
2408 | } | |
8f2ece69 | 2409 | |
9b3c7c3c UD |
2410 | /* End of string */ |
2411 | ||
2412 | /* There was a word separator at the end */ | |
2413 | if (word == NULL) /* i.e. w_newword */ | |
2414 | return 0; | |
2415 | ||
2416 | /* There was no field separator at the end */ | |
2417 | return w_addword (pwordexp, word); | |
8f2ece69 UD |
2418 | |
2419 | do_error: | |
2420 | /* Error: | |
2bcf29ba | 2421 | * free memory used (unless error is WRDE_NOSPACE), and |
05d5dedc | 2422 | * set pwordexp members back to what they were. |
8f2ece69 | 2423 | */ |
2bcf29ba | 2424 | |
ecf359c6 | 2425 | free (word); |
8f2ece69 | 2426 | |
c06cc21c UD |
2427 | if (error == WRDE_NOSPACE) |
2428 | return WRDE_NOSPACE; | |
2429 | ||
05d5dedc UD |
2430 | if ((flags & WRDE_APPEND) == 0) |
2431 | wordfree (pwordexp); | |
2432 | ||
2433 | *pwordexp = old_word; | |
8f2ece69 | 2434 | return error; |
28f540f4 | 2435 | } |