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