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