1 /* POSIX.2 wordexp implementation.
2 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Tim Waugh <tim@cyberelk.demon.co.uk>.
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.
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.
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. */
25 #include <sys/types.h>
30 #include <sys/types.h>
37 #include <sys/param.h>
41 #include <stdio-common/_itoa.h>
43 /* Undefine the following line for the production version. */
44 /* #define NDEBUG 1 */
48 * This is a recursive-descent-style word expansion routine.
51 /* These variables are defined and initialized in the startup code. */
52 extern int __libc_argc
;
53 extern char **__libc_argv
;
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
)
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
)
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
)
71 static int eval_expr (char *expr
, long int *result
) internal_function
;
73 /* The w_*() functions manipulate word lists. */
77 /* Result of w_newword will be ignored if it's the last word. */
79 w_newword (size_t *actlen
, size_t *maxlen
)
81 *actlen
= *maxlen
= 0;
86 w_addchar (char *buffer
, size_t *actlen
, size_t *maxlen
, char ch
)
87 /* (lengths exclude trailing zero) */
89 /* Add a character to the buffer, allocating room for it if needed.
92 if (*actlen
== *maxlen
)
94 char *old_buffer
= buffer
;
95 assert (buffer
== NULL
|| *maxlen
!= 0);
97 buffer
= realloc (buffer
, 1 + *maxlen
);
105 buffer
[*actlen
] = ch
;
106 buffer
[++(*actlen
)] = '\0';
114 w_addmem (char *buffer
, size_t *actlen
, size_t *maxlen
, const char *str
,
117 /* Add a string to the buffer, allocating room for it if needed.
119 if (*actlen
+ len
> *maxlen
)
121 char *old_buffer
= buffer
;
122 assert (buffer
== NULL
|| *maxlen
!= 0);
123 *maxlen
+= MAX (2 * len
, W_CHUNK
);
124 buffer
= realloc (old_buffer
, 1 + *maxlen
);
132 *((char *) __mempcpy (&buffer
[*actlen
], str
, len
)) = '\0';
141 w_addstr (char *buffer
, size_t *actlen
, size_t *maxlen
, const char *str
)
142 /* (lengths exclude trailing zero) */
144 /* Add a string to the buffer, allocating room for it if needed.
148 assert (str
!= NULL
); /* w_addstr only called from this file */
151 return w_addmem (buffer
, actlen
, maxlen
, str
, len
);
156 w_addword (wordexp_t
*pwordexp
, char *word
)
158 /* Add a word to the wordlist */
162 /* Internally, NULL acts like "". Convert NULLs to "" before
163 * the caller sees them.
167 word
= __strdup ("");
172 num_p
= 2 + pwordexp
->we_wordc
+ pwordexp
->we_offs
;
173 new_wordv
= realloc (pwordexp
->we_wordv
, sizeof (char *) * num_p
);
174 if (new_wordv
!= NULL
)
176 pwordexp
->we_wordv
= new_wordv
;
177 pwordexp
->we_wordv
[pwordexp
->we_offs
+ pwordexp
->we_wordc
++] = word
;
178 pwordexp
->we_wordv
[pwordexp
->we_offs
+ pwordexp
->we_wordc
] = NULL
;
186 /* The parse_*() functions should leave *offset being the offset in 'words'
187 * to the last character processed.
192 parse_backslash (char **word
, size_t *word_length
, size_t *max_length
,
193 const char *words
, size_t *offset
)
195 /* We are poised _at_ a backslash, not in quotes */
197 switch (words
[1 + *offset
])
200 /* Backslash is last character of input words */
208 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
221 parse_qtd_backslash (char **word
, size_t *word_length
, size_t *max_length
,
222 const char *words
, size_t *offset
)
224 /* We are poised _at_ a backslash, inside quotes */
226 switch (words
[1 + *offset
])
229 /* Backslash is last character of input words */
240 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
248 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
250 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
264 parse_tilde (char **word
, size_t *word_length
, size_t *max_length
,
265 const char *words
, size_t *offset
, size_t wordc
)
267 /* We are poised _at_ a tilde */
270 if (*word_length
!= 0)
272 if (!((*word
)[*word_length
- 1] == '=' && wordc
== 0))
274 if (!((*word
)[*word_length
- 1] == ':'
275 && strchr (*word
, '=') && wordc
== 0))
277 *word
= w_addchar (*word
, word_length
, max_length
, '~');
278 return *word
? 0 : WRDE_NOSPACE
;
283 for (i
= 1 + *offset
; words
[i
]; i
++)
285 if (words
[i
] == ':' || words
[i
] == '/' || words
[i
] == ' ' ||
286 words
[i
] == '\t' || words
[i
] == 0 )
289 if (words
[i
] == '\\')
291 *word
= w_addchar (*word
, word_length
, max_length
, '~');
292 return *word
? 0 : WRDE_NOSPACE
;
296 if (i
== 1 + *offset
)
298 /* Tilde appears on its own */
300 struct passwd pwd
, *tpwd
;
306 /* POSIX.2 says ~ expands to $HOME and if HOME is unset the
307 results are unspecified. We do a lookup on the uid if
310 home
= getenv ("HOME");
313 *word
= w_addstr (*word
, word_length
, max_length
, home
);
320 buffer
= __alloca (buflen
);
322 while ((result
= __getpwuid_r (uid
, &pwd
, buffer
, buflen
, &tpwd
)) != 0
326 buffer
= __alloca (buflen
);
329 if (result
== 0 && tpwd
!= NULL
&& pwd
.pw_dir
!= NULL
)
331 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
337 *word
= w_addchar (*word
, word_length
, max_length
, '~');
345 /* Look up user name in database to get home directory */
346 char *user
= __strndup (&words
[1 + *offset
], i
- (1 + *offset
));
347 struct passwd pwd
, *tpwd
;
349 char* buffer
= __alloca (buflen
);
352 while ((result
= __getpwnam_r (user
, &pwd
, buffer
, buflen
, &tpwd
)) != 0
356 buffer
= __alloca (buflen
);
359 if (result
== 0 && tpwd
!= NULL
&& pwd
.pw_dir
)
360 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
363 /* (invalid login name) */
364 *word
= w_addchar (*word
, word_length
, max_length
, '~');
366 *word
= w_addstr (*word
, word_length
, max_length
, user
);
371 return *word
? 0 : WRDE_NOSPACE
;
377 do_parse_glob (const char *glob_word
, char **word
, size_t *word_length
,
378 size_t *max_length
, wordexp_t
*pwordexp
, const char *ifs
,
379 const char *ifs_white
)
385 error
= glob (glob_word
, GLOB_NOCHECK
, NULL
, &globbuf
);
389 /* We can only run into memory problems. */
390 assert (error
== GLOB_NOSPACE
);
396 /* No field splitting allowed. */
397 assert (globbuf
.gl_pathv
[0] != NULL
);
398 *word
= w_addstr (*word
, word_length
, max_length
, globbuf
.gl_pathv
[0]);
399 for (match
= 1; match
< globbuf
.gl_pathc
&& *word
!= NULL
; ++match
)
401 *word
= w_addchar (*word
, word_length
, max_length
, ' ');
403 *word
= w_addstr (*word
, word_length
, max_length
,
404 globbuf
.gl_pathv
[match
]);
408 return *word
? 0 : WRDE_NOSPACE
;
411 assert (ifs
== NULL
|| *ifs
!= '\0');
415 *word
= w_newword (word_length
, max_length
);
418 for (match
= 0; match
< globbuf
.gl_pathc
; ++match
)
420 char *matching_word
= __strdup (globbuf
.gl_pathv
[match
]);
421 if (matching_word
== NULL
|| w_addword (pwordexp
, matching_word
))
434 parse_glob (char **word
, size_t *word_length
, size_t *max_length
,
435 const char *words
, size_t *offset
, int flags
,
436 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
438 /* We are poised just after a '*', a '[' or a '?'. */
439 int error
= WRDE_NOSPACE
;
440 int quoted
= 0; /* 1 if singly-quoted, 2 if doubly */
442 wordexp_t glob_list
; /* List of words to glob */
444 glob_list
.we_wordc
= 0;
445 glob_list
.we_wordv
= NULL
;
446 glob_list
.we_offs
= 0;
447 for (; words
[*offset
] != '\0'; ++*offset
)
449 if ((ifs
&& strchr (ifs
, words
[*offset
])) ||
450 (!ifs
&& strchr (" \t\n", words
[*offset
])))
454 /* Sort out quoting */
455 if (words
[*offset
] == '\'')
462 else if (quoted
== 1)
468 else if (words
[*offset
] == '"')
475 else if (quoted
== 2)
482 /* Sort out other special characters */
483 if (quoted
!= 1 && words
[*offset
] == '$')
485 error
= parse_dollars (word
, word_length
, max_length
, words
,
486 offset
, flags
, &glob_list
, ifs
, ifs_white
,
493 else if (words
[*offset
] == '\\')
496 error
= parse_qtd_backslash (word
, word_length
, max_length
,
499 error
= parse_backslash (word
, word_length
, max_length
,
508 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
513 /* Don't forget to re-parse the character we stopped at. */
517 error
= w_addword (&glob_list
, *word
);
518 *word
= w_newword (word_length
, max_length
);
519 for (i
= 0; error
== 0 && i
< glob_list
.we_wordc
; i
++)
520 error
= do_parse_glob (glob_list
.we_wordv
[i
], word
, word_length
,
521 max_length
, pwordexp
, ifs
, ifs_white
);
525 wordfree (&glob_list
);
531 parse_squote (char **word
, size_t *word_length
, size_t *max_length
,
532 const char *words
, size_t *offset
)
534 /* We are poised just after a single quote */
535 for (; words
[*offset
]; ++(*offset
))
537 if (words
[*offset
] != '\'')
539 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
546 /* Unterminated string */
550 /* Functions to evaluate an arithmetic expression */
553 eval_expr_val (char **expr
, long int *result
)
558 /* Skip white space */
559 for (digit
= *expr
; digit
&& *digit
&& isspace (*digit
); ++digit
);
565 /* Scan for closing paren */
566 for (++digit
; **expr
&& **expr
!= ')'; ++(*expr
));
574 if (eval_expr (digit
, result
))
579 case '+': /* Positive value */
583 case '-': /* Negative value */
589 if (!isdigit (*digit
))
594 for (; *digit
&& isdigit (*digit
); ++digit
)
595 *result
= (*result
* 10) + (*digit
- '0');
604 eval_expr_multdiv (char **expr
, long int *result
)
609 if (eval_expr_val (expr
, result
) != 0)
614 /* Skip white space */
615 for (; *expr
&& **expr
&& isspace (**expr
); ++(*expr
));
620 if (eval_expr_val (expr
, &arg
) != 0)
625 else if (**expr
== '/')
628 if (eval_expr_val (expr
, &arg
) != 0)
641 eval_expr (char *expr
, long int *result
)
646 if (eval_expr_multdiv (&expr
, result
) != 0)
651 /* Skip white space */
652 for (; expr
&& *expr
&& isspace (*expr
); ++expr
);
657 if (eval_expr_multdiv (&expr
, &arg
) != 0)
662 else if (*expr
== '-')
665 if (eval_expr_multdiv (&expr
, &arg
) != 0)
678 parse_arith (char **word
, size_t *word_length
, size_t *max_length
,
679 const char *words
, size_t *offset
, int flags
, int bracket
)
681 /* We are poised just after "$((" or "$[" */
688 expr
= w_newword (&expr_length
, &expr_maxlen
);
689 for (; words
[*offset
]; ++(*offset
))
691 switch (words
[*offset
])
694 error
= parse_dollars (&expr
, &expr_length
, &expr_maxlen
,
695 words
, offset
, flags
, NULL
, NULL
, NULL
, 1);
696 /* The ``1'' here is to tell parse_dollars not to
708 error
= parse_backtick (&expr
, &expr_length
, &expr_maxlen
,
709 words
, offset
, flags
, NULL
, NULL
, NULL
);
710 /* The first NULL here is to tell parse_backtick not to
721 error
= parse_qtd_backslash (&expr
, &expr_length
, &expr_maxlen
,
728 /* I think that a backslash within an
729 * arithmetic expansion is bound to
730 * cause an error sooner or later anyway though.
735 if (--paren_depth
== 0)
737 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
738 long int numresult
= 0;
739 long long int convertme
;
741 if (bracket
|| words
[1 + *offset
] != ')')
750 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
758 convertme
= -numresult
;
759 *word
= w_addchar (*word
, word_length
, max_length
, '-');
767 convertme
= numresult
;
770 *word
= w_addstr (*word
, word_length
, max_length
,
771 _itoa (convertme
, &result
[20], 10, 0));
773 return *word
? 0 : WRDE_NOSPACE
;
775 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
782 if (bracket
&& paren_depth
== 1)
784 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
785 long int numresult
= 0;
788 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
795 *word
= w_addstr (*word
, word_length
, max_length
,
796 _itoa_word (numresult
, &result
[20], 10, 0));
798 return *word
? 0 : WRDE_NOSPACE
;
814 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
825 /* Function called by child process in exec_comm() */
828 exec_comm_child (char *comm
, int *fildes
, int showerr
, int noexec
)
830 const char *args
[4] = { _PATH_BSHELL
, "-c", comm
, NULL
};
832 /* Execute the command, or just check syntax? */
836 /* Redirect output. */
837 __dup2 (fildes
[1], 1);
840 /* Redirect stderr to /dev/null if we have to. */
845 fd
= __open (_PATH_DEVNULL
, O_WRONLY
);
846 if (fd
>= 0 && fd
!= 2)
853 /* Make sure the subshell doesn't field-split on our behalf. */
857 __execve (_PATH_BSHELL
, (char *const *) args
, __environ
);
863 /* Function to execute a command and retrieve the results */
864 /* pwordexp contains NULL if field-splitting is forbidden */
867 exec_comm (char *comm
, char **word
, size_t *word_length
, size_t *max_length
,
868 int flags
, wordexp_t
*pwordexp
, const char *ifs
,
869 const char *ifs_white
)
876 size_t maxnewlines
= 0;
880 /* Don't fork() unless necessary */
888 if ((pid
= __fork ()) < 0)
897 exec_comm_child(comm
, fildes
, (flags
& WRDE_SHOWERR
), 0);
902 buffer
= __alloca (bufsize
);
905 /* Quoted - no field splitting */
909 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
911 if (__waitpid (pid
, &status
, WNOHANG
) == 0)
913 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
917 maxnewlines
+= buflen
;
919 *word
= w_addmem (*word
, word_length
, max_length
, buffer
, buflen
);
925 /* Not quoted - split fields */
929 * 0 when searching for first character in a field not IFS white space
930 * 1 when copying the text of a field
931 * 2 when searching for possible non-whitespace IFS
932 * 3 when searching for non-newline after copying field
937 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
939 if (__waitpid (pid
, &status
, WNOHANG
) == 0)
941 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
945 for (i
= 0; i
< buflen
; ++i
)
947 if (strchr (ifs
, buffer
[i
]) != NULL
)
949 /* Current character is IFS */
950 if (strchr (ifs_white
, buffer
[i
]) == NULL
)
952 /* Current character is IFS but not whitespace */
958 * eg: text<space><comma><space>moretext
960 * So, strip whitespace IFS (like at the start)
967 /* fall through and delimit field.. */
971 if (buffer
[i
] == '\n')
973 /* Current character is (IFS) newline */
975 /* If copying a field, this is the end of it,
976 but maybe all that's left is trailing newlines.
977 So start searching for a non-newline. */
985 /* Current character is IFS white space, but
988 /* If not either copying a field or searching
989 for non-newline after a field, ignore it */
990 if (copying
!= 1 && copying
!= 3)
993 /* End of field (search for non-ws IFS afterwards) */
998 /* First IFS white space (non-newline), or IFS non-whitespace.
999 * Delimit the field. Nulls are converted by w_addword. */
1000 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1003 *word
= w_newword (word_length
, max_length
);
1006 /* fall back round the loop.. */
1010 /* Not IFS character */
1014 /* Nothing but (IFS) newlines since the last field,
1015 so delimit it here before starting new word */
1016 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1019 *word
= w_newword (word_length
, max_length
);
1024 if (buffer
[i
] == '\n') /* happens if newline not in IFS */
1029 *word
= w_addchar (*word
, word_length
, max_length
,
1038 /* Chop off trailing newlines (required by POSIX.2) */
1039 /* Ensure we don't go back further than the beginning of the
1040 substitution (i.e. remove maxnewlines bytes at most) */
1041 while (maxnewlines
-- != 0 &&
1042 *word_length
> 0 && (*word
)[*word_length
- 1] == '\n')
1044 (*word
)[--*word_length
] = '\0';
1046 /* If the last word was entirely newlines, turn it into a new word
1047 * which can be ignored if there's nothing following it. */
1048 if (*word_length
== 0)
1051 *word
= w_newword (word_length
, max_length
);
1056 __close (fildes
[0]);
1058 /* Check for syntax error (re-execute but with "-n" flag) */
1059 if (buflen
< 1 && status
!= 0)
1061 if ((pid
= __fork ()) < 0)
1064 return WRDE_NOSPACE
;
1069 fildes
[0] = fildes
[1] = -1;
1070 exec_comm_child(comm
, fildes
, 0, 1);
1073 if (__waitpid (pid
, &status
, 0) == pid
&& status
!= 0)
1080 __kill (pid
, SIGKILL
);
1081 __waitpid (pid
, NULL
, 0);
1082 __close (fildes
[0]);
1083 return WRDE_NOSPACE
;
1088 parse_comm (char **word
, size_t *word_length
, size_t *max_length
,
1089 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1090 const char *ifs
, const char *ifs_white
)
1092 /* We are poised just after "$(" */
1093 int paren_depth
= 1;
1095 int quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1098 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
1100 for (; words
[*offset
]; ++(*offset
))
1102 switch (words
[*offset
])
1107 else if (quoted
== 1)
1115 else if (quoted
== 2)
1121 if (!quoted
&& --paren_depth
== 0)
1123 /* Go -- give script to the shell */
1126 error
= exec_comm (comm
, word
, word_length
, max_length
,
1127 flags
, pwordexp
, ifs
, ifs_white
);
1134 /* This is just part of the script */
1142 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
1144 return WRDE_NOSPACE
;
1156 parse_param (char **word
, size_t *word_length
, size_t *max_length
,
1157 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1158 const char *ifs
, const char *ifs_white
, int quoted
)
1160 /* We are poised just after "$" */
1164 ACT_RP_SHORT_LEFT
= '#',
1165 ACT_RP_LONG_LEFT
= 'L',
1166 ACT_RP_SHORT_RIGHT
= '%',
1167 ACT_RP_LONG_RIGHT
= 'R',
1168 ACT_NULL_ERROR
= '?',
1169 ACT_NULL_SUBST
= '-',
1170 ACT_NONNULL_SUBST
= '+',
1171 ACT_NULL_ASSIGN
= '='
1177 size_t start
= *offset
;
1181 enum action action
= ACT_NONE
;
1186 int pattern_is_quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1190 int brace
= words
[*offset
] == '{';
1192 env
= w_newword (&env_length
, &env_maxlen
);
1193 pattern
= w_newword (&pat_length
, &pat_maxlen
);
1198 /* First collect the parameter name. */
1200 if (words
[*offset
] == '#')
1208 if (isalpha (words
[*offset
]) || words
[*offset
] == '_')
1210 /* Normal parameter name. */
1213 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1218 while (isalnum (words
[++*offset
]) || words
[*offset
] == '_');
1220 else if (isdigit (words
[*offset
]))
1222 /* Numeric parameter name. */
1226 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1233 while (isdigit(words
[++*offset
]));
1235 else if (strchr ("*@$", words
[*offset
]) != NULL
)
1237 /* Special parameter. */
1239 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1253 /* Check for special action to be applied to the value. */
1254 switch (words
[*offset
])
1261 action
= ACT_RP_SHORT_LEFT
;
1262 if (words
[1 + *offset
] == '#')
1265 action
= ACT_RP_LONG_LEFT
;
1270 action
= ACT_RP_SHORT_RIGHT
;
1271 if (words
[1 + *offset
] == '%')
1274 action
= ACT_RP_LONG_RIGHT
;
1279 if (strchr ("-=?+", words
[1 + *offset
]) == NULL
)
1283 action
= words
[++*offset
];
1290 action
= words
[*offset
];
1297 /* Now collect the pattern, but don't expand it yet. */
1299 for (; words
[*offset
]; ++(*offset
))
1301 switch (words
[*offset
])
1304 if (!pattern_is_quoted
)
1309 if (!pattern_is_quoted
)
1318 if (pattern_is_quoted
)
1319 /* Quoted; treat as normal character. */
1322 /* Otherwise, it's an escape: next character is literal. */
1323 if (words
[++*offset
] == '\0')
1326 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
, '\\');
1327 if (pattern
== NULL
)
1333 if (pattern_is_quoted
== 0)
1334 pattern_is_quoted
= 1;
1335 else if (pattern_is_quoted
== 1)
1336 pattern_is_quoted
= 0;
1341 if (pattern_is_quoted
== 0)
1342 pattern_is_quoted
= 2;
1343 else if (pattern_is_quoted
== 2)
1344 pattern_is_quoted
= 0;
1349 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
,
1351 if (pattern
== NULL
)
1356 /* End of input string -- remember to reparse the character that we
1361 if (words
[start
] == '{' && words
[*offset
] != '}')
1368 /* $# expands to the number of positional parameters */
1370 value
= _itoa_word (__libc_argc
- 1, &buffer
[20], 10, 0);
1375 /* Just $ on its own */
1376 *offset
= start
- 1;
1377 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1378 return *word
? 0 : WRDE_NOSPACE
;
1381 /* Is it a numeric parameter? */
1382 else if (isdigit (env
[0]))
1386 if (n
>= __libc_argc
)
1387 /* Substitute NULL. */
1390 /* Replace with appropriate positional parameter. */
1391 value
= __libc_argv
[n
];
1393 /* Is it a special parameter? */
1400 value
= _itoa_word (__getpid (), &buffer
[20], 10, 0);
1402 /* Is it `${#*}' or `${#@}'? */
1403 else if ((*env
== '*' || *env
== '@') && seen_hash
)
1406 value
= _itoa_word (__libc_argc
> 0 ? __libc_argc
- 1 : 0,
1407 &buffer
[20], 10, 0);
1408 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1412 return *word
? 0 : WRDE_NOSPACE
;
1414 /* Is it `$*' or `$@' (unquoted) ? */
1415 else if (*env
== '*' || (*env
== '@' && !quoted
))
1417 size_t plist_len
= 0;
1421 /* Build up value parameter by parameter (copy them) */
1422 for (p
= 1; __libc_argv
[p
]; ++p
)
1423 plist_len
+= strlen (__libc_argv
[p
]) + 1; /* for space */
1424 value
= malloc (plist_len
);
1429 for (p
= 1; __libc_argv
[p
]; ++p
)
1433 end
= __stpcpy (end
, __libc_argv
[p
]);
1440 /* Must be a quoted `$@' */
1441 assert (*env
== '@' && quoted
);
1443 /* Each parameter is a separate word ("$@") */
1444 if (__libc_argc
== 2)
1445 value
= __libc_argv
[1];
1446 else if (__libc_argc
> 2)
1450 /* Append first parameter to current word. */
1451 value
= w_addstr (*word
, word_length
, max_length
,
1453 if (value
== NULL
|| w_addword (pwordexp
, value
))
1456 for (p
= 2; __libc_argv
[p
+ 1]; p
++)
1458 char *newword
= __strdup (__libc_argv
[p
]);
1459 if (newword
== NULL
|| w_addword (pwordexp
, newword
))
1463 /* Start a new word with the last parameter. */
1464 *word
= w_newword (word_length
, max_length
);
1465 value
= __libc_argv
[p
];
1476 value
= getenv (env
);
1478 if (value
== NULL
&& (flags
& WRDE_UNDEF
))
1480 /* Variable not defined. */
1481 error
= WRDE_BADVAL
;
1485 if (action
!= ACT_NONE
)
1487 int expand_pattern
= 0;
1489 /* First, find out if we need to expand pattern (i.e. if we will
1493 case ACT_RP_SHORT_LEFT
:
1494 case ACT_RP_LONG_LEFT
:
1495 case ACT_RP_SHORT_RIGHT
:
1496 case ACT_RP_LONG_RIGHT
:
1497 /* Always expand for these. */
1501 case ACT_NULL_ERROR
:
1502 case ACT_NULL_SUBST
:
1503 case ACT_NULL_ASSIGN
:
1504 if (!value
|| (!*value
&& colon_seen
))
1505 /* If param is unset, or set but null and a colon has been seen,
1506 the expansion of the pattern will be needed. */
1511 case ACT_NONNULL_SUBST
:
1512 /* Expansion of word will be needed if parameter is set and not null,
1513 or set null but no colon has been seen. */
1514 if (value
&& (*value
|| !colon_seen
))
1520 assert (! "Unrecognised action!");
1525 /* We need to perform tilde expansion, parameter expansion,
1526 command substitution, and arithmetic expansion. We also
1527 have to be a bit careful with wildcard characters, as
1528 pattern might be given to fnmatch soon. To do this, we
1529 convert quotes to escapes. */
1535 int quoted
= 0; /* 1: single quotes; 2: double */
1537 expanded
= w_newword (&exp_len
, &exp_maxl
);
1538 for (p
= pattern
; p
&& *p
; p
++)
1547 else if (quoted
== 0)
1556 else if (quoted
== 0)
1566 /* Convert quoted wildchar to escaped wildchar. */
1567 expanded
= w_addchar (expanded
, &exp_len
,
1570 if (expanded
== NULL
)
1577 error
= parse_dollars (&expanded
, &exp_len
, &exp_maxl
, p
,
1578 &offset
, flags
, NULL
, NULL
, NULL
, 1);
1594 if (quoted
|| exp_len
)
1598 error
= parse_tilde (&expanded
, &exp_len
, &exp_maxl
, p
,
1615 expanded
= w_addchar (expanded
, &exp_len
, &exp_maxl
, '\\');
1617 assert (*p
); /* checked when extracted initially */
1618 if (expanded
== NULL
)
1622 expanded
= w_addchar (expanded
, &exp_len
, &exp_maxl
, *p
);
1624 if (expanded
== NULL
)
1636 case ACT_RP_SHORT_LEFT
:
1637 case ACT_RP_LONG_LEFT
:
1638 case ACT_RP_SHORT_RIGHT
:
1639 case ACT_RP_LONG_RIGHT
:
1645 if (value
== NULL
|| pattern
== NULL
|| *pattern
== '\0')
1648 end
= value
+ strlen (value
);
1652 case ACT_RP_SHORT_LEFT
:
1653 for (p
= value
; p
<= end
; ++p
)
1657 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1662 char *newval
= __strdup (p
);
1680 case ACT_RP_LONG_LEFT
:
1681 for (p
= end
; p
>= value
; --p
)
1685 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1690 char *newval
= __strdup (p
);
1708 case ACT_RP_SHORT_RIGHT
:
1709 for (p
= end
; p
>= value
; --p
)
1711 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1714 newval
= malloc (p
- value
+ 1);
1723 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1734 case ACT_RP_LONG_RIGHT
:
1735 for (p
= value
; p
<= end
; ++p
)
1737 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1740 newval
= malloc (p
- value
+ 1);
1749 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1767 case ACT_NULL_ERROR
:
1768 if (value
&& *value
)
1769 /* Substitute parameter */
1773 if (!colon_seen
&& value
)
1774 /* Substitute NULL */
1777 fprintf (stderr
, "%s: %s\n", env
, pattern
);
1780 fprintf (stderr
, "%s: parameter null or not set\n", env
);
1781 error
= WRDE_BADVAL
;
1788 case ACT_NULL_SUBST
:
1789 if (value
&& *value
)
1790 /* Substitute parameter */
1793 if (free_value
&& value
)
1796 if (!colon_seen
&& value
)
1797 /* Substitute NULL */
1800 value
= pattern
? __strdup (pattern
) : pattern
;
1803 if (pattern
&& !value
)
1808 case ACT_NONNULL_SUBST
:
1809 if (value
&& (*value
|| !colon_seen
))
1811 if (free_value
&& value
)
1814 value
= pattern
? __strdup (pattern
) : pattern
;
1817 if (pattern
&& !value
)
1823 /* Substitute NULL */
1828 case ACT_NULL_ASSIGN
:
1829 if (value
&& *value
)
1830 /* Substitute parameter */
1833 if (!colon_seen
&& value
)
1835 /* Substitute NULL */
1841 if (free_value
&& value
)
1844 value
= pattern
? __strdup (pattern
) : pattern
;
1847 if (pattern
&& !value
)
1850 __setenv (env
, value
, 1);
1854 assert (! "Unrecognised action!");
1858 free (env
); env
= NULL
;
1859 free (pattern
); pattern
= NULL
;
1863 char param_length
[21];
1864 param_length
[20] = '\0';
1865 *word
= w_addstr (*word
, word_length
, max_length
,
1866 _itoa_word (value
? strlen (value
) : 0,
1867 ¶m_length
[20], 10, 0));
1870 assert (value
!= NULL
);
1874 return *word
? 0 : WRDE_NOSPACE
;
1880 if (quoted
|| !pwordexp
)
1882 /* Quoted - no field split */
1883 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1887 return *word
? 0 : WRDE_NOSPACE
;
1891 /* Need to field-split */
1892 char *value_copy
= __strdup (value
); /* Don't modify value */
1893 char *field_begin
= value_copy
;
1894 int seen_nonws_ifs
= 0;
1899 if (value_copy
== NULL
)
1904 char *field_end
= field_begin
;
1907 /* If this isn't the first field, start a new word */
1908 if (field_begin
!= value_copy
)
1910 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1916 *word
= w_newword (word_length
, max_length
);
1919 /* Skip IFS whitespace before the field */
1920 field_begin
+= strspn (field_begin
, ifs_white
);
1922 if (!seen_nonws_ifs
&& *field_begin
== 0)
1923 /* Nothing but whitespace */
1926 /* Search for the end of the field */
1927 field_end
= field_begin
+ strcspn (field_begin
, ifs
);
1929 /* Set up pointer to the character after end of field and
1930 skip whitespace IFS after it. */
1931 next_field
= field_end
+ strspn (field_end
, ifs_white
);
1933 /* Skip at most one non-whitespace IFS character after the field */
1935 if (*next_field
&& strchr (ifs
, *next_field
))
1941 /* Null-terminate it */
1944 /* Tag a copy onto the current word */
1945 *word
= w_addstr (*word
, word_length
, max_length
, field_begin
);
1947 if (*word
== NULL
&& *field_begin
!= '\0')
1953 field_begin
= next_field
;
1955 while (seen_nonws_ifs
|| *field_begin
);
1967 error
= WRDE_NOSPACE
;
1971 error
= WRDE_SYNTAX
;
1985 parse_dollars (char **word
, size_t *word_length
, size_t *max_length
,
1986 const char *words
, size_t *offset
, int flags
,
1987 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
,
1990 /* We are poised _at_ "$" */
1991 switch (words
[1 + *offset
])
1996 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1997 return *word
? 0 : WRDE_NOSPACE
;
2000 if (words
[2 + *offset
] == '(')
2002 /* Differentiate between $((1+3)) and $((echo);(ls)) */
2003 int i
= 3 + *offset
;
2005 while (words
[i
] && !(depth
== 0 && words
[i
] == ')'))
2007 if (words
[i
] == '(')
2009 else if (words
[i
] == ')')
2015 if (words
[i
] == ')' && words
[i
+ 1] == ')')
2018 /* Call parse_arith -- 0 is for "no brackets" */
2019 return parse_arith (word
, word_length
, max_length
, words
, offset
,
2024 if (flags
& WRDE_NOCMD
)
2028 return parse_comm (word
, word_length
, max_length
, words
, offset
, flags
,
2029 quoted
? NULL
: pwordexp
, ifs
, ifs_white
);
2033 /* Call parse_arith -- 1 is for "brackets" */
2034 return parse_arith (word
, word_length
, max_length
, words
, offset
, flags
,
2039 ++(*offset
); /* parse_param needs to know if "{" is there */
2040 return parse_param (word
, word_length
, max_length
, words
, offset
, flags
,
2041 pwordexp
, ifs
, ifs_white
, quoted
);
2047 parse_backtick (char **word
, size_t *word_length
, size_t *max_length
,
2048 const char *words
, size_t *offset
, int flags
,
2049 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
2051 /* We are poised just after "`" */
2056 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
2058 for (; words
[*offset
]; ++(*offset
))
2060 switch (words
[*offset
])
2063 /* Go -- give the script to the shell */
2064 error
= exec_comm (comm
, word
, word_length
, max_length
, flags
,
2065 pwordexp
, ifs
, ifs_white
);
2072 error
= parse_qtd_backslash (&comm
, &comm_length
, &comm_maxlen
,
2085 error
= parse_backslash (&comm
, &comm_length
, &comm_maxlen
, words
,
2097 squoting
= 1 - squoting
;
2099 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
2101 return WRDE_NOSPACE
;
2112 parse_dquote (char **word
, size_t *word_length
, size_t *max_length
,
2113 const char *words
, size_t *offset
, int flags
,
2114 wordexp_t
*pwordexp
, const char * ifs
, const char * ifs_white
)
2116 /* We are poised just after a double-quote */
2119 for (; words
[*offset
]; ++(*offset
))
2121 switch (words
[*offset
])
2127 error
= parse_dollars (word
, word_length
, max_length
, words
, offset
,
2128 flags
, pwordexp
, ifs
, ifs_white
, 1);
2129 /* The ``1'' here is to tell parse_dollars not to
2130 * split the fields. It may need to, however ("$@").
2138 if (flags
& WRDE_NOCMD
)
2142 error
= parse_backtick (word
, word_length
, max_length
, words
,
2143 offset
, flags
, NULL
, NULL
, NULL
);
2144 /* The first NULL here is to tell parse_backtick not to
2153 error
= parse_qtd_backslash (word
, word_length
, max_length
, words
,
2162 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
2164 return WRDE_NOSPACE
;
2168 /* Unterminated string */
2173 * wordfree() is to be called after pwordexp is finished with.
2177 wordfree (wordexp_t
*pwordexp
)
2180 /* wordexp can set pwordexp to NULL */
2181 if (pwordexp
&& pwordexp
->we_wordv
)
2183 char **wordv
= pwordexp
->we_wordv
;
2185 for (wordv
+= pwordexp
->we_offs
; *wordv
; ++wordv
)
2188 free (pwordexp
->we_wordv
);
2189 pwordexp
->we_wordv
= NULL
;
2198 wordexp (const char *words
, wordexp_t
*pwordexp
, int flags
)
2200 size_t words_offset
;
2203 char *word
= w_newword (&word_length
, &max_length
);
2207 wordexp_t old_word
= *pwordexp
;
2209 if (flags
& WRDE_REUSE
)
2211 /* Minimal implementation of WRDE_REUSE for now */
2212 wordfree (pwordexp
);
2213 old_word
.we_wordv
= NULL
;
2216 if ((flags
& WRDE_APPEND
) == 0)
2218 pwordexp
->we_wordc
= 0;
2220 if (flags
& WRDE_DOOFFS
)
2222 pwordexp
->we_wordv
= calloc (1 + pwordexp
->we_offs
, sizeof (char *));
2223 if (pwordexp
->we_wordv
== NULL
)
2225 error
= WRDE_NOSPACE
;
2231 pwordexp
->we_wordv
= calloc (1, sizeof (char *));
2232 if (pwordexp
->we_wordv
== NULL
)
2234 error
= WRDE_NOSPACE
;
2238 pwordexp
->we_offs
= 0;
2242 /* Find out what the field separators are.
2243 * There are two types: whitespace and non-whitespace.
2245 ifs
= getenv ("IFS");
2248 /* IFS unset - use <space><tab><newline>. */
2249 ifs
= strcpy (ifs_white
, " \t\n");
2253 char *whch
= ifs_white
;
2255 /* Start off with no whitespace IFS characters */
2256 ifs_white
[0] = '\0';
2258 while (*ifsch
!= '\0')
2260 if ((*ifsch
== ' ') || (*ifsch
== '\t') || (*ifsch
== '\n'))
2262 /* Whitespace IFS. See first whether it is already in our
2264 char *runp
= ifs_white
;
2266 while (runp
< whch
&& *runp
!= '\0' && *runp
!= *ifsch
)
2278 for (words_offset
= 0 ; words
[words_offset
] ; ++words_offset
)
2279 switch (words
[words_offset
])
2282 error
= parse_backslash (&word
, &word_length
, &max_length
, words
,
2291 error
= parse_dollars (&word
, &word_length
, &max_length
, words
,
2292 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
,
2301 if (flags
& WRDE_NOCMD
)
2303 error
= WRDE_CMDSUB
;
2308 error
= parse_backtick (&word
, &word_length
, &max_length
, words
,
2309 &words_offset
, flags
, pwordexp
, ifs
,
2319 error
= parse_dquote (&word
, &word_length
, &max_length
, words
,
2320 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2327 error
= w_addword (pwordexp
, NULL
);
2337 error
= parse_squote (&word
, &word_length
, &max_length
, words
,
2345 error
= w_addword (pwordexp
, NULL
);
2354 error
= parse_tilde (&word
, &word_length
, &max_length
, words
,
2355 &words_offset
, pwordexp
->we_wordc
);
2365 error
= parse_glob (&word
, &word_length
, &max_length
, words
,
2366 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2374 /* Is it a word separator? */
2375 if (strchr (" \t", words
[words_offset
]) == NULL
)
2377 char ch
= words
[words_offset
];
2379 /* Not a word separator -- but is it a valid word char? */
2380 if (strchr ("\n|&;<>(){}", ch
))
2383 error
= WRDE_BADCHAR
;
2387 /* "Ordinary" character -- add it to word */
2388 word
= w_addchar (word
, &word_length
, &max_length
,
2392 error
= WRDE_NOSPACE
;
2399 /* If a word has been delimited, add it to the list. */
2402 error
= w_addword (pwordexp
, word
);
2407 word
= w_newword (&word_length
, &max_length
);
2412 /* There was a word separator at the end */
2413 if (word
== NULL
) /* i.e. w_newword */
2416 /* There was no field separator at the end */
2417 return w_addword (pwordexp
, word
);
2421 * free memory used (unless error is WRDE_NOSPACE), and
2422 * set pwordexp members back to what they were.
2428 if (error
== WRDE_NOSPACE
)
2429 return WRDE_NOSPACE
;
2431 if ((flags
& WRDE_APPEND
) == 0)
2432 wordfree (pwordexp
);
2434 *pwordexp
= old_word
;