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