1 /* POSIX.2 wordexp implementation.
2 Copyright (C) 1997, 1998, 1999 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_wordc
++] = word
;
178 pwordexp
->we_wordv
[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
;
302 char* buffer
= __alloca (buflen
);
307 while ((result
= __getpwuid_r (uid
, &pwd
, buffer
, buflen
, &tpwd
)) != 0
311 buffer
= __alloca (buflen
);
314 if (result
== 0 && tpwd
!= NULL
&& pwd
.pw_dir
!= NULL
)
316 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
322 *word
= w_addchar (*word
, word_length
, max_length
, '~');
329 /* Look up user name in database to get home directory */
330 char *user
= __strndup (&words
[1 + *offset
], i
- *offset
);
331 struct passwd pwd
, *tpwd
;
333 char* buffer
= __alloca (buflen
);
336 while ((result
= __getpwnam_r (user
, &pwd
, buffer
, buflen
, &tpwd
)) != 0
340 buffer
= __alloca (buflen
);
343 if (result
== 0 && tpwd
!= NULL
&& pwd
.pw_dir
)
344 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
347 /* (invalid login name) */
348 *word
= w_addchar (*word
, word_length
, max_length
, '~');
350 *word
= w_addstr (*word
, word_length
, max_length
, user
);
355 return *word
? 0 : WRDE_NOSPACE
;
361 do_parse_glob (const char *glob_word
, char **word
, size_t *word_length
,
362 size_t *max_length
, wordexp_t
*pwordexp
, const char *ifs
,
363 const char *ifs_white
)
369 error
= glob (glob_word
, GLOB_NOCHECK
, NULL
, &globbuf
);
373 /* We can only run into memory problems. */
374 assert (error
== GLOB_NOSPACE
);
380 /* No field splitting allowed. */
381 assert (globbuf
.gl_pathv
[0] != NULL
);
382 *word
= w_addstr (*word
, word_length
, max_length
, globbuf
.gl_pathv
[0]);
383 for (match
= 1; match
< globbuf
.gl_pathc
&& *word
!= NULL
; ++match
)
385 *word
= w_addchar (*word
, word_length
, max_length
, ' ');
387 *word
= w_addstr (*word
, word_length
, max_length
,
388 globbuf
.gl_pathv
[match
]);
392 return *word
? 0 : WRDE_NOSPACE
;
395 assert (ifs
== NULL
|| *ifs
!= '\0');
399 *word
= w_newword (word_length
, max_length
);
402 for (match
= 0; match
< globbuf
.gl_pathc
; ++match
)
404 char *matching_word
= __strdup (globbuf
.gl_pathv
[match
]);
405 if (matching_word
== NULL
|| w_addword (pwordexp
, matching_word
))
418 parse_glob (char **word
, size_t *word_length
, size_t *max_length
,
419 const char *words
, size_t *offset
, int flags
,
420 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
422 /* We are poised just after a '*', a '[' or a '?'. */
423 int error
= WRDE_NOSPACE
;
424 int quoted
= 0; /* 1 if singly-quoted, 2 if doubly */
426 wordexp_t glob_list
; /* List of words to glob */
428 glob_list
.we_wordc
= 0;
429 glob_list
.we_wordv
= NULL
;
430 glob_list
.we_offs
= 0;
431 for (; words
[*offset
] != '\0'; ++*offset
)
433 if ((ifs
&& strchr (ifs
, words
[*offset
])) ||
434 (!ifs
&& strchr (" \t\n", words
[*offset
])))
438 /* Sort out quoting */
439 if (words
[*offset
] == '\'')
446 else if (quoted
== 1)
452 else if (words
[*offset
] == '"')
459 else if (quoted
== 2)
466 /* Sort out other special characters */
467 if (quoted
!= 1 && words
[*offset
] == '$')
469 error
= parse_dollars (word
, word_length
, max_length
, words
,
470 offset
, flags
, &glob_list
, ifs
, ifs_white
,
477 else if (words
[*offset
] == '\\')
480 error
= parse_qtd_backslash (word
, word_length
, max_length
,
483 error
= parse_backslash (word
, word_length
, max_length
,
492 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
497 /* Don't forget to re-parse the character we stopped at. */
501 error
= w_addword (&glob_list
, *word
);
502 *word
= w_newword (word_length
, max_length
);
503 for (i
= 0; error
== 0 && i
< glob_list
.we_wordc
; i
++)
504 error
= do_parse_glob (glob_list
.we_wordv
[i
], word
, word_length
,
505 max_length
, pwordexp
, ifs
, ifs_white
);
509 wordfree (&glob_list
);
515 parse_squote (char **word
, size_t *word_length
, size_t *max_length
,
516 const char *words
, size_t *offset
)
518 /* We are poised just after a single quote */
519 for (; words
[*offset
]; ++(*offset
))
521 if (words
[*offset
] != '\'')
523 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
530 /* Unterminated string */
534 /* Functions to evaluate an arithmetic expression */
537 eval_expr_val (char **expr
, long int *result
)
542 /* Skip white space */
543 for (digit
= *expr
; digit
&& *digit
&& isspace (*digit
); ++digit
);
549 /* Scan for closing paren */
550 for (++digit
; **expr
&& **expr
!= ')'; ++(*expr
));
558 if (eval_expr (digit
, result
))
563 case '+': /* Positive value */
567 case '-': /* Negative value */
573 if (!isdigit (*digit
))
578 for (; *digit
&& isdigit (*digit
); ++digit
)
579 *result
= (*result
* 10) + (*digit
- '0');
588 eval_expr_multdiv (char **expr
, long int *result
)
593 if (eval_expr_val (expr
, result
) != 0)
598 /* Skip white space */
599 for (; *expr
&& **expr
&& isspace (**expr
); ++(*expr
));
604 if (eval_expr_val (expr
, &arg
) != 0)
609 else if (**expr
== '/')
612 if (eval_expr_val (expr
, &arg
) != 0)
625 eval_expr (char *expr
, long int *result
)
630 if (eval_expr_multdiv (&expr
, result
) != 0)
635 /* Skip white space */
636 for (; expr
&& *expr
&& isspace (*expr
); ++expr
);
641 if (eval_expr_multdiv (&expr
, &arg
) != 0)
646 else if (*expr
== '-')
649 if (eval_expr_multdiv (&expr
, &arg
) != 0)
662 parse_arith (char **word
, size_t *word_length
, size_t *max_length
,
663 const char *words
, size_t *offset
, int flags
, int bracket
)
665 /* We are poised just after "$((" or "$[" */
672 expr
= w_newword (&expr_length
, &expr_maxlen
);
673 for (; words
[*offset
]; ++(*offset
))
675 switch (words
[*offset
])
678 error
= parse_dollars (&expr
, &expr_length
, &expr_maxlen
,
679 words
, offset
, flags
, NULL
, NULL
, NULL
, 1);
680 /* The ``1'' here is to tell parse_dollars not to
692 error
= parse_backtick (&expr
, &expr_length
, &expr_maxlen
,
693 words
, offset
, flags
, NULL
, NULL
, NULL
);
694 /* The first NULL here is to tell parse_backtick not to
705 error
= parse_qtd_backslash (&expr
, &expr_length
, &expr_maxlen
,
712 /* I think that a backslash within an
713 * arithmetic expansion is bound to
714 * cause an error sooner or later anyway though.
719 if (--paren_depth
== 0)
721 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
722 long int numresult
= 0;
723 long long int convertme
;
725 if (bracket
|| words
[1 + *offset
] != ')')
734 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
742 convertme
= -numresult
;
743 *word
= w_addchar (*word
, word_length
, max_length
, '-');
751 convertme
= numresult
;
754 *word
= w_addstr (*word
, word_length
, max_length
,
755 _itoa (convertme
, &result
[20], 10, 0));
757 return *word
? 0 : WRDE_NOSPACE
;
759 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
766 if (bracket
&& paren_depth
== 1)
768 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
769 long int numresult
= 0;
772 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
779 *word
= w_addstr (*word
, word_length
, max_length
,
780 _itoa_word (numresult
, &result
[20], 10, 0));
782 return *word
? 0 : WRDE_NOSPACE
;
798 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
809 /* Function to execute a command and retrieve the results */
810 /* pwordexp contains NULL if field-splitting is forbidden */
813 exec_comm (char *comm
, char **word
, size_t *word_length
, size_t *max_length
,
814 int flags
, wordexp_t
*pwordexp
, const char *ifs
,
815 const char *ifs_white
)
824 /* Don't fork() unless necessary */
832 if ((pid
= __fork ()) < 0)
843 const char *args
[4] = { _PATH_BSHELL
, "-c", comm
, NULL
};
845 /* Redirect output. */
846 __dup2 (fildes
[1], 1);
849 /* Redirect stderr to /dev/null if we have to. */
850 if ((flags
& WRDE_SHOWERR
) == 0)
854 fd
= __open (_PATH_DEVNULL
, O_WRONLY
);
855 if (fd
>= 0 && fd
!= 2)
862 /* Make sure the subshell doesn't field-split on our behalf. */
866 __execve (_PATH_BSHELL
, (char *const *) args
, __environ
);
875 buffer
= __alloca (bufsize
);
878 { /* Quoted - no field splitting */
882 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
884 if (__waitpid (pid
, NULL
, WNOHANG
) == 0)
886 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
890 *word
= w_addmem (*word
, word_length
, max_length
, buffer
, buflen
);
896 /* Not quoted - split fields */
900 * 0 when searching for first character in a field not IFS white space
901 * 1 when copying the text of a field
902 * 2 when searching for possible non-whitespace IFS
907 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
909 if (__waitpid (pid
, NULL
, WNOHANG
) == 0)
911 if ((__read (fildes
[0], buffer
, bufsize
)) < 1)
915 for (i
= 0; i
< buflen
; ++i
)
917 if (strchr (ifs
, buffer
[i
]) != NULL
)
919 /* Current character is IFS */
920 if (strchr (ifs_white
, buffer
[i
]) == NULL
)
922 /* Current character is IFS but not whitespace */
928 * eg: text<space><comma><space>moretext
930 * So, strip whitespace IFS (like at the start)
937 /* fall through and delimit field.. */
941 /* Current character is IFS white space */
943 /* If not copying a field, ignore it */
947 /* End of field (search for non-ws IFS afterwards) */
951 /* First IFS white space, or IFS non-whitespace.
952 * Delimit the field. Nulls are converted by w_addword. */
953 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
956 *word
= w_newword (word_length
, max_length
);
957 /* fall back round the loop.. */
961 /* Not IFS character */
964 *word
= w_addchar (*word
, word_length
, max_length
,
973 /* Bash chops off trailing newlines, which seems sensible. */
974 while (*word_length
> 0 && (*word
)[*word_length
- 1] == '\n')
976 (*word
)[--*word_length
] = '\0';
978 /* If the last word was entirely newlines, turn it into a new word
979 * which can be ignored if there's nothing following it. */
980 if (*word_length
== 0)
983 *word
= w_newword (word_length
, max_length
);
992 __kill (pid
, SIGKILL
);
993 __waitpid (pid
, NULL
, 0);
1000 parse_comm (char **word
, size_t *word_length
, size_t *max_length
,
1001 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1002 const char *ifs
, const char *ifs_white
)
1004 /* We are poised just after "$(" */
1005 int paren_depth
= 1;
1007 int quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1010 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
1012 for (; words
[*offset
]; ++(*offset
))
1014 switch (words
[*offset
])
1019 else if (quoted
== 1)
1027 else if (quoted
== 2)
1033 if (!quoted
&& --paren_depth
== 0)
1035 /* Go -- give script to the shell */
1038 error
= exec_comm (comm
, word
, word_length
, max_length
,
1039 flags
, pwordexp
, ifs
, ifs_white
);
1046 /* This is just part of the script */
1054 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
1056 return WRDE_NOSPACE
;
1068 parse_param (char **word
, size_t *word_length
, size_t *max_length
,
1069 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1070 const char *ifs
, const char *ifs_white
, int quoted
)
1072 /* We are poised just after "$" */
1076 ACT_RP_SHORT_LEFT
= '#',
1077 ACT_RP_LONG_LEFT
= 'L',
1078 ACT_RP_SHORT_RIGHT
= '%',
1079 ACT_RP_LONG_RIGHT
= 'R',
1080 ACT_NULL_ERROR
= '?',
1081 ACT_NULL_SUBST
= '-',
1082 ACT_NONNULL_SUBST
= '+',
1083 ACT_NULL_ASSIGN
= '='
1089 size_t start
= *offset
;
1093 enum action action
= ACT_NONE
;
1098 int pattern_is_quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1102 int brace
= words
[*offset
] == '{';
1104 env
= w_newword (&env_length
, &env_maxlen
);
1105 pattern
= w_newword (&pat_length
, &pat_maxlen
);
1110 /* First collect the parameter name. */
1112 if (words
[*offset
] == '#')
1120 if (isalpha (words
[*offset
]) || words
[*offset
] == '_')
1122 /* Normal parameter name. */
1125 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1130 while (isalnum (words
[++*offset
]) || words
[*offset
] == '_');
1132 else if (isdigit (words
[*offset
]))
1134 /* Numeric parameter name. */
1138 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1145 while (isdigit(words
[++*offset
]));
1147 else if (strchr ("*@$", words
[*offset
]) != NULL
)
1149 /* Special parameter. */
1151 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1165 /* Check for special action to be applied to the value. */
1166 switch (words
[*offset
])
1173 action
= ACT_RP_SHORT_LEFT
;
1174 if (words
[1 + *offset
] == '#')
1177 action
= ACT_RP_LONG_LEFT
;
1182 action
= ACT_RP_SHORT_RIGHT
;
1183 if (words
[1 + *offset
] == '%')
1186 action
= ACT_RP_LONG_RIGHT
;
1191 if (strchr ("-=?+", words
[1 + *offset
]) == NULL
)
1195 action
= words
[++*offset
];
1202 action
= words
[*offset
];
1209 /* Now collect the pattern, but don't expand it yet. */
1211 for (; words
[*offset
]; ++(*offset
))
1213 switch (words
[*offset
])
1216 if (!pattern_is_quoted
)
1221 if (!pattern_is_quoted
)
1230 if (pattern_is_quoted
)
1231 /* Quoted; treat as normal character. */
1234 /* Otherwise, it's an escape: next character is literal. */
1235 if (words
[++*offset
] == '\0')
1238 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
, '\\');
1239 if (pattern
== NULL
)
1245 if (pattern_is_quoted
== 0)
1246 pattern_is_quoted
= 1;
1247 else if (pattern_is_quoted
== 1)
1248 pattern_is_quoted
= 0;
1253 if (pattern_is_quoted
== 0)
1254 pattern_is_quoted
= 2;
1255 else if (pattern_is_quoted
== 2)
1256 pattern_is_quoted
= 0;
1261 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
,
1263 if (pattern
== NULL
)
1268 /* End of input string -- remember to reparse the character that we
1273 if (words
[start
] == '{' && words
[*offset
] != '}')
1280 /* $# expands to the number of positional parameters */
1282 value
= _itoa_word (__libc_argc
- 1, &buffer
[20], 10, 0);
1287 /* Just $ on its own */
1288 *offset
= start
- 1;
1289 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1290 return *word
? 0 : WRDE_NOSPACE
;
1293 /* Is it a numeric parameter? */
1294 else if (isdigit (env
[0]))
1298 if (n
>= __libc_argc
)
1299 /* Substitute NULL. */
1302 /* Replace with appropriate positional parameter. */
1303 value
= __libc_argv
[n
];
1305 /* Is it a special parameter? */
1312 value
= _itoa_word (__getpid (), &buffer
[20], 10, 0);
1314 /* Is it `${#*}' or `${#@}'? */
1315 else if ((*env
== '*' || *env
== '@') && seen_hash
)
1318 value
= _itoa_word (__libc_argc
> 0 ? __libc_argc
- 1 : 0,
1319 &buffer
[20], 10, 0);
1320 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1324 return *word
? 0 : WRDE_NOSPACE
;
1326 /* Is it `$*' or `$@' (unquoted) ? */
1327 else if (*env
== '*' || (*env
== '@' && !quoted
))
1329 size_t plist_len
= 0;
1333 /* Build up value parameter by parameter (copy them) */
1334 for (p
= 1; __libc_argv
[p
]; ++p
)
1335 plist_len
+= strlen (__libc_argv
[p
]) + 1; /* for space */
1336 value
= malloc (plist_len
);
1341 for (p
= 1; __libc_argv
[p
]; ++p
)
1345 end
= __stpcpy (end
, __libc_argv
[p
]);
1352 /* Must be a quoted `$@' */
1353 assert (*env
== '@' && quoted
);
1355 /* Each parameter is a separate word ("$@") */
1356 if (__libc_argc
== 2)
1357 value
= __libc_argv
[1];
1358 else if (__libc_argc
> 2)
1362 /* Append first parameter to current word. */
1363 value
= w_addstr (*word
, word_length
, max_length
,
1365 if (value
== NULL
|| w_addword (pwordexp
, value
))
1368 for (p
= 2; __libc_argv
[p
+ 1]; p
++)
1370 char *newword
= __strdup (__libc_argv
[p
]);
1371 if (newword
== NULL
|| w_addword (pwordexp
, newword
))
1375 /* Start a new word with the last parameter. */
1376 *word
= w_newword (word_length
, max_length
);
1377 value
= __libc_argv
[p
];
1388 value
= getenv (env
);
1390 if (value
== NULL
&& (flags
& WRDE_UNDEF
))
1392 /* Variable not defined. */
1393 error
= WRDE_BADVAL
;
1397 if (action
!= ACT_NONE
)
1399 int expand_pattern
= 0;
1401 /* First, find out if we need to expand pattern (i.e. if we will
1405 case ACT_RP_SHORT_LEFT
:
1406 case ACT_RP_LONG_LEFT
:
1407 case ACT_RP_SHORT_RIGHT
:
1408 case ACT_RP_LONG_RIGHT
:
1409 /* Always expand for these. */
1413 case ACT_NULL_ERROR
:
1414 case ACT_NULL_SUBST
:
1415 case ACT_NULL_ASSIGN
:
1416 if (!value
|| (!*value
&& colon_seen
))
1417 /* If param is unset, or set but null and a colon has been seen,
1418 the expansion of the pattern will be needed. */
1423 case ACT_NONNULL_SUBST
:
1424 /* Expansion of word will be needed if parameter is set and not null,
1425 or set null but no colon has been seen. */
1426 if (value
&& (*value
|| !colon_seen
))
1432 assert (! "Unrecognised action!");
1437 /* We need to perform tilde expansion, parameter expansion,
1438 command substitution, and arithmetic expansion. We also
1439 have to be a bit careful with wildcard characters, as
1440 pattern might be given to fnmatch soon. To do this, we
1441 convert quotes to escapes. */
1447 int quoted
= 0; /* 1: single quotes; 2: double */
1449 expanded
= w_newword (&exp_len
, &exp_maxl
);
1450 for (p
= pattern
; p
&& *p
; p
++)
1459 else if (quoted
== 0)
1468 else if (quoted
== 0)
1478 /* Convert quoted wildchar to escaped wildchar. */
1479 expanded
= w_addchar (expanded
, &exp_len
,
1482 if (expanded
== NULL
)
1489 error
= parse_dollars (&expanded
, &exp_len
, &exp_maxl
, p
,
1490 &offset
, flags
, NULL
, NULL
, NULL
, 1);
1506 if (quoted
|| exp_len
)
1510 error
= parse_tilde (&expanded
, &exp_len
, &exp_maxl
, p
,
1527 expanded
= w_addchar (expanded
, &exp_len
, &exp_maxl
, '\\');
1529 assert (*p
); /* checked when extracted initially */
1530 if (expanded
== NULL
)
1534 expanded
= w_addchar (expanded
, &exp_len
, &exp_maxl
, *p
);
1536 if (expanded
== NULL
)
1548 case ACT_RP_SHORT_LEFT
:
1549 case ACT_RP_LONG_LEFT
:
1550 case ACT_RP_SHORT_RIGHT
:
1551 case ACT_RP_LONG_RIGHT
:
1557 if (value
== NULL
|| pattern
== NULL
|| *pattern
== '\0')
1560 end
= value
+ strlen (value
);
1564 case ACT_RP_SHORT_LEFT
:
1565 for (p
= value
; p
<= end
; ++p
)
1569 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1574 char *newval
= __strdup (p
);
1592 case ACT_RP_LONG_LEFT
:
1593 for (p
= end
; p
>= value
; --p
)
1597 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1602 char *newval
= __strdup (p
);
1620 case ACT_RP_SHORT_RIGHT
:
1621 for (p
= end
; p
>= value
; --p
)
1623 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1626 newval
= malloc (p
- value
+ 1);
1635 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1646 case ACT_RP_LONG_RIGHT
:
1647 for (p
= value
; p
<= end
; ++p
)
1649 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1652 newval
= malloc (p
- value
+ 1);
1661 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1679 case ACT_NULL_ERROR
:
1680 if (value
&& *value
)
1681 /* Substitute parameter */
1685 if (!colon_seen
&& value
)
1686 /* Substitute NULL */
1689 fprintf (stderr
, "%s: %s\n", env
, pattern
);
1692 fprintf (stderr
, "%s: parameter null or not set\n", env
);
1693 error
= WRDE_BADVAL
;
1700 case ACT_NULL_SUBST
:
1701 if (value
&& *value
)
1702 /* Substitute parameter */
1705 if (free_value
&& value
)
1708 if (!colon_seen
&& value
)
1709 /* Substitute NULL */
1712 value
= pattern
? __strdup (pattern
) : pattern
;
1715 if (pattern
&& !value
)
1720 case ACT_NONNULL_SUBST
:
1721 if (value
&& (*value
|| !colon_seen
))
1723 if (free_value
&& value
)
1726 value
= pattern
? __strdup (pattern
) : pattern
;
1729 if (pattern
&& !value
)
1735 /* Substitute NULL */
1740 case ACT_NULL_ASSIGN
:
1741 if (value
&& *value
)
1742 /* Substitute parameter */
1745 if (!colon_seen
&& value
)
1747 /* Substitute NULL */
1753 if (free_value
&& value
)
1756 value
= pattern
? __strdup (pattern
) : pattern
;
1759 if (pattern
&& !value
)
1762 setenv (env
, value
, 1);
1766 assert (! "Unrecognised action!");
1770 free (env
); env
= NULL
;
1771 free (pattern
); pattern
= NULL
;
1775 char param_length
[21];
1776 param_length
[20] = '\0';
1777 *word
= w_addstr (*word
, word_length
, max_length
,
1778 _itoa_word (value
? strlen (value
) : 0,
1779 ¶m_length
[20], 10, 0));
1782 assert (value
!= NULL
);
1786 return *word
? 0 : WRDE_NOSPACE
;
1792 if (quoted
|| !pwordexp
)
1794 /* Quoted - no field split */
1795 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1799 return *word
? 0 : WRDE_NOSPACE
;
1803 /* Need to field-split */
1804 char *value_copy
= __strdup (value
); /* Don't modify value */
1805 char *field_begin
= value_copy
;
1806 int seen_nonws_ifs
= 0;
1811 if (value_copy
== NULL
)
1816 char *field_end
= field_begin
;
1819 /* If this isn't the first field, start a new word */
1820 if (field_begin
!= value_copy
)
1822 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1828 *word
= w_newword (word_length
, max_length
);
1831 /* Skip IFS whitespace before the field */
1832 field_begin
+= strspn (field_begin
, ifs_white
);
1834 if (!seen_nonws_ifs
&& *field_begin
== 0)
1835 /* Nothing but whitespace */
1838 /* Search for the end of the field */
1839 field_end
= field_begin
+ strcspn (field_begin
, ifs
);
1841 /* Set up pointer to the character after end of field and
1842 skip whitespace IFS after it. */
1843 next_field
= field_end
+ strspn (field_end
, ifs_white
);
1845 /* Skip at most one non-whitespace IFS character after the field */
1847 if (*next_field
&& strchr (ifs
, *next_field
))
1853 /* Null-terminate it */
1856 /* Tag a copy onto the current word */
1857 *word
= w_addstr (*word
, word_length
, max_length
, field_begin
);
1859 if (*word
== NULL
&& *field_begin
!= '\0')
1865 field_begin
= next_field
;
1867 while (seen_nonws_ifs
|| *field_begin
);
1879 error
= WRDE_NOSPACE
;
1883 error
= WRDE_SYNTAX
;
1897 parse_dollars (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
,
1902 /* We are poised _at_ "$" */
1903 switch (words
[1 + *offset
])
1908 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1909 return *word
? 0 : WRDE_NOSPACE
;
1912 if (words
[2 + *offset
] == '(')
1914 /* Differentiate between $((1+3)) and $((echo);(ls)) */
1915 int i
= 3 + *offset
;
1917 while (words
[i
] && !(depth
== 0 && words
[i
] == ')'))
1919 if (words
[i
] == '(')
1921 else if (words
[i
] == ')')
1927 if (words
[i
] == ')' && words
[i
+ 1] == ')')
1930 /* Call parse_arith -- 0 is for "no brackets" */
1931 return parse_arith (word
, word_length
, max_length
, words
, offset
,
1936 if (flags
& WRDE_NOCMD
)
1940 return parse_comm (word
, word_length
, max_length
, words
, offset
, flags
,
1941 quoted
? NULL
: pwordexp
, ifs
, ifs_white
);
1945 /* Call parse_arith -- 1 is for "brackets" */
1946 return parse_arith (word
, word_length
, max_length
, words
, offset
, flags
,
1951 ++(*offset
); /* parse_param needs to know if "{" is there */
1952 return parse_param (word
, word_length
, max_length
, words
, offset
, flags
,
1953 pwordexp
, ifs
, ifs_white
, quoted
);
1959 parse_backtick (char **word
, size_t *word_length
, size_t *max_length
,
1960 const char *words
, size_t *offset
, int flags
,
1961 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
1963 /* We are poised just after "`" */
1968 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
1970 for (; words
[*offset
]; ++(*offset
))
1972 switch (words
[*offset
])
1975 /* Go -- give the script to the shell */
1976 error
= exec_comm (comm
, word
, word_length
, max_length
, flags
,
1977 pwordexp
, ifs
, ifs_white
);
1984 error
= parse_qtd_backslash (&comm
, &comm_length
, &comm_maxlen
,
1997 error
= parse_backslash (&comm
, &comm_length
, &comm_maxlen
, words
,
2009 squoting
= 1 - squoting
;
2011 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
2013 return WRDE_NOSPACE
;
2024 parse_dquote (char **word
, size_t *word_length
, size_t *max_length
,
2025 const char *words
, size_t *offset
, int flags
,
2026 wordexp_t
*pwordexp
, const char * ifs
, const char * ifs_white
)
2028 /* We are poised just after a double-quote */
2031 for (; words
[*offset
]; ++(*offset
))
2033 switch (words
[*offset
])
2039 error
= parse_dollars (word
, word_length
, max_length
, words
, offset
,
2040 flags
, pwordexp
, ifs
, ifs_white
, 1);
2041 /* The ``1'' here is to tell parse_dollars not to
2042 * split the fields. It may need to, however ("$@").
2050 if (flags
& WRDE_NOCMD
)
2054 error
= parse_backtick (word
, word_length
, max_length
, words
,
2055 offset
, flags
, NULL
, NULL
, NULL
);
2056 /* The first NULL here is to tell parse_backtick not to
2065 error
= parse_qtd_backslash (word
, word_length
, max_length
, words
,
2074 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
2076 return WRDE_NOSPACE
;
2080 /* Unterminated string */
2085 * wordfree() is to be called after pwordexp is finished with.
2089 wordfree (wordexp_t
*pwordexp
)
2092 /* wordexp can set pwordexp to NULL */
2093 if (pwordexp
&& pwordexp
->we_wordv
)
2095 char **wordv
= pwordexp
->we_wordv
;
2097 for (wordv
+= pwordexp
->we_offs
; *wordv
; ++wordv
)
2100 free (pwordexp
->we_wordv
);
2101 pwordexp
->we_wordv
= NULL
;
2110 wordexp (const char *words
, wordexp_t
*pwordexp
, int flags
)
2112 size_t wordv_offset
;
2113 size_t words_offset
;
2116 char *word
= w_newword (&word_length
, &max_length
);
2120 char **old_wordv
= pwordexp
->we_wordv
;
2121 size_t old_wordc
= (flags
& WRDE_REUSE
) ? pwordexp
->we_wordc
: 0;
2123 if (flags
& WRDE_REUSE
)
2125 /* Minimal implementation of WRDE_REUSE for now */
2126 wordfree (pwordexp
);
2130 if (flags
& WRDE_DOOFFS
)
2132 pwordexp
->we_wordv
= calloc (1 + pwordexp
->we_offs
, sizeof (char *));
2133 if (pwordexp
->we_wordv
== NULL
)
2135 error
= WRDE_NOSPACE
;
2141 pwordexp
->we_wordv
= calloc (1, sizeof (char *));
2142 if (pwordexp
->we_wordv
== NULL
)
2144 error
= WRDE_NOSPACE
;
2148 pwordexp
->we_offs
= 0;
2151 if ((flags
& WRDE_APPEND
) == 0)
2152 pwordexp
->we_wordc
= 0;
2154 wordv_offset
= pwordexp
->we_offs
+ pwordexp
->we_wordc
;
2156 /* Find out what the field separators are.
2157 * There are two types: whitespace and non-whitespace.
2159 ifs
= getenv ("IFS");
2162 /* IFS unset - use <space><tab><newline>. */
2163 ifs
= strcpy (ifs_white
, " \t\n");
2167 char *whch
= ifs_white
;
2169 /* Start off with no whitespace IFS characters */
2170 ifs_white
[0] = '\0';
2172 while (*ifsch
!= '\0')
2174 if ((*ifsch
== ' ') || (*ifsch
== '\t') || (*ifsch
== '\n'))
2176 /* Whitespace IFS. See first whether it is already in our
2178 char *runp
= ifs_white
;
2180 while (runp
< whch
&& *runp
!= '\0' && *runp
!= *ifsch
)
2192 for (words_offset
= 0 ; words
[words_offset
] ; ++words_offset
)
2193 switch (words
[words_offset
])
2196 error
= parse_backslash (&word
, &word_length
, &max_length
, words
,
2205 error
= parse_dollars (&word
, &word_length
, &max_length
, words
,
2206 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
,
2215 if (flags
& WRDE_NOCMD
)
2217 error
= WRDE_CMDSUB
;
2222 error
= parse_backtick (&word
, &word_length
, &max_length
, words
,
2223 &words_offset
, flags
, pwordexp
, ifs
,
2233 error
= parse_dquote (&word
, &word_length
, &max_length
, words
,
2234 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2241 error
= w_addword (pwordexp
, NULL
);
2251 error
= parse_squote (&word
, &word_length
, &max_length
, words
,
2259 error
= w_addword (pwordexp
, NULL
);
2268 error
= parse_tilde (&word
, &word_length
, &max_length
, words
,
2269 &words_offset
, pwordexp
->we_wordc
);
2279 error
= parse_glob (&word
, &word_length
, &max_length
, words
,
2280 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2288 /* Is it a word separator? */
2289 if (strchr (" \t", words
[words_offset
]) == NULL
)
2291 char ch
= words
[words_offset
];
2293 /* Not a word separator -- but is it a valid word char? */
2294 if (strchr ("\n|&;<>(){}", ch
))
2297 error
= WRDE_BADCHAR
;
2301 /* "Ordinary" character -- add it to word */
2302 word
= w_addchar (word
, &word_length
, &max_length
,
2306 error
= WRDE_NOSPACE
;
2313 /* If a word has been delimited, add it to the list. */
2316 error
= w_addword (pwordexp
, word
);
2321 word
= w_newword (&word_length
, &max_length
);
2326 /* There was a word separator at the end */
2327 if (word
== NULL
) /* i.e. w_newword */
2330 /* There was no field separator at the end */
2331 return w_addword (pwordexp
, word
);
2335 * free memory used (unless error is WRDE_NOSPACE), and
2336 * set we_wordc and wd_wordv back to what they were.
2342 if (error
== WRDE_NOSPACE
)
2343 return WRDE_NOSPACE
;
2345 wordfree (pwordexp
);
2346 pwordexp
->we_wordv
= old_wordv
;
2347 pwordexp
->we_wordc
= old_wordc
;