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>.
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. */
78 w_newword (size_t *actlen
, size_t *maxlen
)
80 *actlen
= *maxlen
= 0;
85 w_addchar (char *buffer
, size_t *actlen
, size_t *maxlen
, char ch
)
86 /* (lengths exclude trailing zero) */
88 /* Add a character to the buffer, allocating room for it if needed.
91 if (*actlen
== *maxlen
)
93 char *old_buffer
= buffer
;
94 assert (buffer
== NULL
|| *maxlen
!= 0);
96 buffer
= realloc (buffer
, 1 + *maxlen
);
104 buffer
[*actlen
] = ch
;
105 buffer
[++(*actlen
)] = '\0';
113 w_addmem (char *buffer
, size_t *actlen
, size_t *maxlen
, const char *str
,
116 /* Add a string to the buffer, allocating room for it if needed.
118 if (*actlen
+ len
> *maxlen
)
120 char *old_buffer
= buffer
;
121 assert (buffer
== NULL
|| *maxlen
!= 0);
122 *maxlen
+= MAX (2 * len
, W_CHUNK
);
123 buffer
= realloc (old_buffer
, 1 + *maxlen
);
131 *((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 num_p
= 2 + pwordexp
->we_wordc
+ pwordexp
->we_offs
;
163 new_wordv
= realloc (pwordexp
->we_wordv
, sizeof (char *) * num_p
);
164 if (new_wordv
!= NULL
)
166 pwordexp
->we_wordv
= new_wordv
;
167 pwordexp
->we_wordv
[pwordexp
->we_wordc
++] = word
;
168 pwordexp
->we_wordv
[pwordexp
->we_wordc
] = NULL
;
175 /* The parse_*() functions should leave *offset being the offset in 'words'
176 * to the last character processed.
181 parse_backslash (char **word
, size_t *word_length
, size_t *max_length
,
182 const char *words
, size_t *offset
)
184 /* We are poised _at_ a backslash, not in quotes */
186 switch (words
[1 + *offset
])
189 /* Backslash is last character of input words */
197 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
210 parse_qtd_backslash (char **word
, size_t *word_length
, size_t *max_length
,
211 const char *words
, size_t *offset
)
213 /* We are poised _at_ a backslash, inside quotes */
215 switch (words
[1 + *offset
])
218 /* Backslash is last character of input words */
229 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
237 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
239 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
253 parse_tilde (char **word
, size_t *word_length
, size_t *max_length
,
254 const char *words
, size_t *offset
, size_t wordc
)
256 /* We are poised _at_ a tilde */
259 if (*word_length
!= 0)
261 if (!((*word
)[*word_length
- 1] == '=' && wordc
== 0))
263 if (!((*word
)[*word_length
- 1] == ':'
264 && strchr (*word
, '=') && wordc
== 0))
266 *word
= w_addchar (*word
, word_length
, max_length
, '~');
267 return *word
? 0 : WRDE_NOSPACE
;
272 for (i
= 1 + *offset
; words
[i
]; i
++)
274 if (words
[i
] == ':' || words
[i
] == '/' || words
[i
] == ' ' ||
275 words
[i
] == '\t' || words
[i
] == 0 )
278 if (words
[i
] == '\\')
280 *word
= w_addchar (*word
, word_length
, max_length
, '~');
281 return *word
? 0 : WRDE_NOSPACE
;
285 if (i
== 1 + *offset
)
287 /* Tilde appears on its own */
289 struct passwd pwd
, *tpwd
;
291 char* buffer
= __alloca (buflen
);
296 while ((result
= __getpwuid_r (uid
, &pwd
, buffer
, buflen
, &tpwd
)) != 0
300 buffer
= __alloca (buflen
);
303 if (result
== 0 && pwd
.pw_dir
!= NULL
)
305 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
311 *word
= w_addchar (*word
, word_length
, max_length
, '~');
318 /* Look up user name in database to get home directory */
319 char *user
= __strndup (&words
[1 + *offset
], i
- *offset
);
320 struct passwd pwd
, *tpwd
;
322 char* buffer
= __alloca (buflen
);
325 while ((result
= __getpwnam_r (user
, &pwd
, buffer
, buflen
, &tpwd
)) != 0
329 buffer
= __alloca (buflen
);
332 if (result
== 0 && pwd
.pw_dir
)
333 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
336 /* (invalid login name) */
337 *word
= w_addchar (*word
, word_length
, max_length
, '~');
339 *word
= w_addstr (*word
, word_length
, max_length
, user
);
344 return *word
? 0 : WRDE_NOSPACE
;
350 do_parse_glob (const char *glob_word
, char **word
, size_t *word_length
,
351 size_t *max_length
, wordexp_t
*pwordexp
, const char *ifs
,
352 const char *ifs_white
)
358 error
= glob (glob_word
, GLOB_NOCHECK
, NULL
, &globbuf
);
362 /* We can only run into memory problems. */
363 assert (error
== GLOB_NOSPACE
);
369 /* No field splitting allowed. */
370 assert (globbuf
.gl_pathv
[0] != NULL
);
371 *word
= w_addstr (*word
, word_length
, max_length
, globbuf
.gl_pathv
[0]);
372 for (match
= 1; match
< globbuf
.gl_pathc
&& *word
!= NULL
; ++match
)
374 *word
= w_addchar (*word
, word_length
, max_length
, ' ');
376 *word
= w_addstr (*word
, word_length
, max_length
,
377 globbuf
.gl_pathv
[match
]);
381 return *word
? 0 : WRDE_NOSPACE
;
384 assert (ifs
== NULL
|| *ifs
!= '\0');
388 *word
= w_newword (word_length
, max_length
);
391 for (match
= 0; match
< globbuf
.gl_pathc
; ++match
)
393 char *matching_word
= __strdup (globbuf
.gl_pathv
[match
]);
394 if (matching_word
== NULL
|| w_addword (pwordexp
, matching_word
))
407 parse_glob (char **word
, size_t *word_length
, size_t *max_length
,
408 const char *words
, size_t *offset
, int flags
,
409 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
411 /* We are poised just after a '*', a '[' or a '?'. */
412 int error
= WRDE_NOSPACE
;
413 int quoted
= 0; /* 1 if singly-quoted, 2 if doubly */
415 wordexp_t glob_list
; /* List of words to glob */
417 glob_list
.we_wordc
= 0;
418 glob_list
.we_wordv
= NULL
;
419 glob_list
.we_offs
= 0;
420 for (; words
[*offset
] != '\0'; ++*offset
)
422 if ((ifs
&& strchr (ifs
, words
[*offset
])) ||
423 (!ifs
&& strchr (" \t\n", words
[*offset
])))
427 /* Sort out quoting */
428 if (words
[*offset
] == '\'')
434 else if (quoted
== 1)
439 else if (words
[*offset
] == '"')
445 else if (quoted
== 2)
451 /* Sort out other special characters */
452 if (quoted
!= 1 && words
[*offset
] == '$')
454 error
= parse_dollars (word
, word_length
, max_length
, words
,
455 offset
, flags
, &glob_list
, ifs
, ifs_white
,
462 else if (words
[*offset
] == '\\')
465 error
= parse_qtd_backslash (word
, word_length
, max_length
,
468 error
= parse_backslash (word
, word_length
, max_length
,
477 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
482 /* Don't forget to re-parse the character we stopped at. */
486 error
= w_addword (&glob_list
, *word
);
487 *word
= w_newword (word_length
, max_length
);
488 for (i
= 0; error
== 0 && i
< glob_list
.we_wordc
; i
++)
489 error
= do_parse_glob (glob_list
.we_wordv
[i
], word
, word_length
,
490 max_length
, pwordexp
, ifs
, ifs_white
);
494 wordfree (&glob_list
);
500 parse_squote (char **word
, size_t *word_length
, size_t *max_length
,
501 const char *words
, size_t *offset
)
503 /* We are poised just after a single quote */
504 for (; words
[*offset
]; ++(*offset
))
506 if (words
[*offset
] != '\'')
508 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
515 /* Unterminated string */
519 /* Functions to evaluate an arithmetic expression */
522 eval_expr_val (char **expr
, long int *result
)
527 /* Skip white space */
528 for (digit
= *expr
; digit
&& *digit
&& isspace (*digit
); ++digit
);
534 /* Scan for closing paren */
535 for (++digit
; **expr
&& **expr
!= ')'; ++(*expr
));
543 if (eval_expr (digit
, result
))
548 case '+': /* Positive value */
552 case '-': /* Negative value */
558 if (!isdigit (*digit
))
563 for (; *digit
&& isdigit (*digit
); ++digit
)
564 *result
= (*result
* 10) + (*digit
- '0');
573 eval_expr_multdiv (char **expr
, long int *result
)
578 if (eval_expr_val (expr
, result
) != 0)
583 /* Skip white space */
584 for (; *expr
&& **expr
&& isspace (**expr
); ++(*expr
));
589 if (eval_expr_val (expr
, &arg
) != 0)
594 else if (**expr
== '/')
597 if (eval_expr_val (expr
, &arg
) != 0)
610 eval_expr (char *expr
, long int *result
)
615 if (eval_expr_multdiv (&expr
, result
) != 0)
620 /* Skip white space */
621 for (; expr
&& *expr
&& isspace (*expr
); ++expr
);
626 if (eval_expr_multdiv (&expr
, &arg
) != 0)
631 else if (*expr
== '-')
634 if (eval_expr_multdiv (&expr
, &arg
) != 0)
647 parse_arith (char **word
, size_t *word_length
, size_t *max_length
,
648 const char *words
, size_t *offset
, int flags
, int bracket
)
650 /* We are poised just after "$((" or "$[" */
657 expr
= w_newword (&expr_length
, &expr_maxlen
);
658 for (; words
[*offset
]; ++(*offset
))
660 switch (words
[*offset
])
663 error
= parse_dollars (&expr
, &expr_length
, &expr_maxlen
,
664 words
, offset
, flags
, NULL
, NULL
, NULL
, 1);
665 /* The ``1'' here is to tell parse_dollars not to
677 error
= parse_backtick (&expr
, &expr_length
, &expr_maxlen
,
678 words
, offset
, flags
, NULL
, NULL
, NULL
);
679 /* The first NULL here is to tell parse_backtick not to
690 error
= parse_qtd_backslash (&expr
, &expr_length
, &expr_maxlen
,
697 /* I think that a backslash within an
698 * arithmetic expansion is bound to
699 * cause an error sooner or later anyway though.
704 if (--paren_depth
== 0)
706 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
707 long int numresult
= 0;
708 long long int convertme
;
710 if (bracket
|| words
[1 + *offset
] != ')')
716 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
721 convertme
= -numresult
;
722 *word
= w_addchar (*word
, word_length
, max_length
, '-');
730 convertme
= numresult
;
733 *word
= w_addstr (*word
, word_length
, max_length
,
734 _itoa (convertme
, &result
[20], 10, 0));
736 return *word
? 0 : WRDE_NOSPACE
;
738 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
745 if (bracket
&& paren_depth
== 1)
747 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
748 long int numresult
= 0;
751 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
755 *word
= w_addstr (*word
, word_length
, max_length
,
756 _itoa_word (numresult
, &result
[20], 10, 0));
758 return *word
? 0 : WRDE_NOSPACE
;
774 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
785 /* Function to execute a command and retrieve the results */
786 /* pwordexp contains NULL if field-splitting is forbidden */
789 exec_comm (char *comm
, char **word
, size_t *word_length
, size_t *max_length
,
790 int flags
, wordexp_t
*pwordexp
, const char *ifs
,
791 const char *ifs_white
)
800 /* Don't fork() unless necessary */
808 if ((pid
= __fork ()) < 0)
817 const char *args
[4] = { _PATH_BSHELL
, "-c", comm
, NULL
};
819 /* Redirect output. */
820 __dup2 (fildes
[1], 1);
823 /* Redirect stderr to /dev/null if we have to. */
824 if ((flags
& WRDE_SHOWERR
) == 0)
828 fd
= __open (_PATH_DEVNULL
, O_WRONLY
);
829 if (fd
>= 0 && fd
!= 2)
837 __execve (_PATH_BSHELL
, (char *const *) args
, __environ
);
846 buffer
= __alloca (bufsize
);
849 { /* Quoted - no field splitting */
853 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
855 if (__waitpid (pid
, NULL
, WNOHANG
) == 0)
857 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
861 *word
= w_addmem (*word
, word_length
, max_length
, buffer
, buflen
);
864 __kill (pid
, SIGKILL
);
865 __waitpid (pid
, NULL
, 0);
872 /* Not quoted - split fields */
876 * 0 when searching for first character in a field not IFS white space
877 * 1 when copying the text of a field
878 * 2 when searching for possible non-whitespace IFS
883 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
885 if (__waitpid (pid
, NULL
, WNOHANG
) == 0)
887 if ((__read (fildes
[0], buffer
, bufsize
)) < 1)
891 for (i
= 0; i
< buflen
; ++i
)
893 if (strchr (ifs
, buffer
[i
]) != NULL
)
895 /* Current character is IFS */
896 if (strchr (ifs_white
, buffer
[i
]) == NULL
)
898 /* Current character is IFS but not whitespace */
904 * eg: text<space><comma><space>moretext
906 * So, strip whitespace IFS (like at the start)
913 /* fall through and delimit field.. */
917 /* Current character is IFS white space */
919 /* If not copying a field, ignore it */
923 /* End of field (search for non-IFS afterwards) */
927 /* First IFS white space, or IFS non-whitespace.
928 * Delimit the field. */
931 /* This field is null, so make it an empty string */
932 *word
= w_addchar (*word
, word_length
, max_length
, 0);
935 __kill (pid
, SIGKILL
);
936 __waitpid (pid
, NULL
, 0);
942 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
944 __kill (pid
, SIGKILL
);
945 __waitpid (pid
, NULL
, 0);
950 *word
= w_newword (word_length
, max_length
);
951 /* fall back round the loop.. */
955 /* Not IFS character */
957 *word
= w_addchar (*word
, word_length
, max_length
,
961 __kill (pid
, SIGKILL
);
962 __waitpid (pid
, NULL
, 0);
971 /* Bash chops off trailing newlines, which seems sensible. */
972 while (*word_length
> 0 && (*word
)[*word_length
- 1] == '\n')
973 (*word
)[--*word_length
] = '\0';
981 parse_comm (char **word
, size_t *word_length
, size_t *max_length
,
982 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
983 const char *ifs
, const char *ifs_white
)
985 /* We are poised just after "$(" */
988 int quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
991 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
993 for (; words
[*offset
]; ++(*offset
))
995 switch (words
[*offset
])
1000 else if (quoted
== 1)
1008 else if (quoted
== 2)
1014 if (!quoted
&& --paren_depth
== 0)
1016 /* Go -- give script to the shell */
1019 error
= exec_comm (comm
, word
, word_length
, max_length
,
1020 flags
, pwordexp
, ifs
, ifs_white
);
1027 /* This is just part of the script */
1035 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
1037 return WRDE_NOSPACE
;
1049 parse_param (char **word
, size_t *word_length
, size_t *max_length
,
1050 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1051 const char *ifs
, const char *ifs_white
, int quoted
)
1053 /* We are poised just after "$" */
1057 ACT_RP_SHORT_LEFT
= '#',
1058 ACT_RP_LONG_LEFT
= 'L',
1059 ACT_RP_SHORT_RIGHT
= '%',
1060 ACT_RP_LONG_RIGHT
= 'R',
1061 ACT_NULL_ERROR
= '?',
1062 ACT_NULL_SUBST
= '-',
1063 ACT_NONNULL_SUBST
= '+',
1064 ACT_NULL_ASSIGN
= '='
1070 size_t start
= *offset
;
1074 enum action action
= ACT_NONE
;
1079 int pattern_is_quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1083 int brace
= words
[*offset
] == '{';
1085 env
= w_newword (&env_length
, &env_maxlen
);
1086 pattern
= w_newword (&pat_length
, &pat_maxlen
);
1091 /* First collect the parameter name. */
1093 if (words
[*offset
] == '#')
1101 if (isalpha (words
[*offset
]) || words
[*offset
] == '_')
1103 /* Normal parameter name. */
1106 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1111 while (isalnum (words
[++*offset
]) || words
[*offset
] == '_');
1113 else if (isdigit (words
[*offset
]))
1115 /* Numeric parameter name. */
1119 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1126 while (isdigit(words
[++*offset
]));
1128 else if (strchr ("*@$", words
[*offset
]) != NULL
)
1130 /* Special parameter. */
1132 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1146 /* Check for special action to be applied to the value. */
1147 switch (words
[*offset
])
1154 action
= ACT_RP_SHORT_LEFT
;
1155 if (words
[1 + *offset
] == '#')
1158 action
= ACT_RP_LONG_LEFT
;
1163 action
= ACT_RP_SHORT_RIGHT
;
1164 if (words
[1 + *offset
] == '%')
1167 action
= ACT_RP_LONG_RIGHT
;
1172 if (strchr ("-=?+", words
[1 + *offset
]) == NULL
)
1176 action
= words
[++*offset
];
1183 action
= words
[*offset
];
1190 /* Now collect the pattern. */
1192 for (; words
[*offset
]; ++(*offset
))
1194 switch (words
[*offset
])
1197 if (!pattern_is_quoted
)
1202 if (!pattern_is_quoted
)
1211 if (!pattern_is_quoted
&& words
[++*offset
] == '\0')
1216 if (pattern_is_quoted
== 0)
1217 pattern_is_quoted
= 1;
1218 else if (pattern_is_quoted
== 1)
1219 pattern_is_quoted
= 0;
1224 if (pattern_is_quoted
== 0)
1225 pattern_is_quoted
= 2;
1226 else if (pattern_is_quoted
== 2)
1227 pattern_is_quoted
= 0;
1232 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
,
1234 if (pattern
== NULL
)
1239 /* End of input string -- remember to reparse the character that we
1244 if (words
[start
] == '{' && words
[*offset
] != '}')
1251 /* $# expands to the number of positional parameters */
1253 value
= _itoa_word (__libc_argc
- 1, &buffer
[20], 10, 0);
1258 /* Just $ on its own */
1259 *offset
= start
- 1;
1260 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1261 return *word
? 0 : WRDE_NOSPACE
;
1264 /* Is it a numeric parameter? */
1265 else if (isdigit (env
[0]))
1269 if (n
>= __libc_argc
)
1270 /* Substitute NULL. */
1273 /* Replace with appropriate positional parameter. */
1274 value
= __libc_argv
[n
];
1276 /* Is it a special parameter? */
1283 value
= _itoa_word (__getpid (), &buffer
[20], 10, 0);
1285 /* Is it `${#*}' or `${#@}'? */
1286 else if ((*env
== '*' || *env
== '@') && seen_hash
)
1289 value
= _itoa_word (__libc_argc
> 0 ? __libc_argc
- 1 : 0,
1290 &buffer
[20], 10, 0);
1291 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1293 return *word
? 0 : WRDE_NOSPACE
;
1295 /* Is it `$*' or `$@' (unquoted) ? */
1296 else if (*env
== '*' || (*env
== '@' && !quoted
))
1298 size_t plist_len
= 0;
1302 /* Build up value parameter by parameter (copy them) */
1303 for (p
= 1; __libc_argv
[p
]; ++p
)
1304 plist_len
+= strlen (__libc_argv
[p
]) + 1; /* for space */
1305 value
= malloc (plist_len
);
1310 for (p
= 1; __libc_argv
[p
]; ++p
)
1314 end
= __stpcpy (end
, __libc_argv
[p
]);
1321 /* Must be a quoted `$@' */
1322 assert (*env
== '@' && quoted
);
1324 /* Each parameter is a separate word ("$@") */
1325 if (__libc_argc
== 2)
1326 value
= __libc_argv
[1];
1327 else if (__libc_argc
> 2)
1331 /* Append first parameter to current word. */
1332 value
= w_addstr (*word
, word_length
, max_length
,
1334 if (value
== NULL
|| w_addword (pwordexp
, value
))
1337 for (p
= 2; __libc_argv
[p
+ 1]; p
++)
1339 char *newword
= __strdup (__libc_argv
[p
]);
1340 if (newword
== NULL
|| w_addword (pwordexp
, newword
))
1344 /* Start a new word with the last parameter. */
1345 *word
= w_newword (word_length
, max_length
);
1346 value
= __libc_argv
[p
];
1357 value
= getenv (env
);
1359 if (value
== NULL
&& (flags
& WRDE_UNDEF
))
1361 /* Variable not defined. */
1369 if (action
!= ACT_NONE
)
1373 case ACT_RP_SHORT_LEFT
:
1374 case ACT_RP_LONG_LEFT
:
1375 case ACT_RP_SHORT_RIGHT
:
1376 case ACT_RP_LONG_RIGHT
:
1382 if (value
== NULL
|| pattern
== NULL
|| *pattern
== '\0')
1385 end
= value
+ strlen (value
);
1389 case ACT_RP_SHORT_LEFT
:
1390 for (p
= value
; p
<= end
; ++p
)
1394 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1399 char *newval
= __strdup (p
);
1417 case ACT_RP_LONG_LEFT
:
1418 for (p
= end
; p
>= value
; --p
)
1422 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1427 char *newval
= __strdup (p
);
1445 case ACT_RP_SHORT_RIGHT
:
1446 for (p
= end
; p
>= value
; --p
)
1448 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1451 newval
= malloc (p
- value
+ 1);
1454 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1465 case ACT_RP_LONG_RIGHT
:
1466 for (p
= value
; p
<= end
; ++p
)
1468 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1471 newval
= malloc (p
- value
+ 1);
1474 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1492 case ACT_NULL_ERROR
:
1493 if (value
&& *value
)
1494 /* Substitute parameter */
1497 if (!colon_seen
&& value
)
1498 /* Substitute NULL */
1502 /* Expand 'pattern' and write it to stderr */
1505 error
= wordexp (pattern
, &we
, flags
);
1511 fprintf (stderr
, "%s:", env
);
1513 for (i
= 0; i
< we
.we_wordc
; ++i
)
1515 fprintf (stderr
, " %s", we
.we_wordv
[i
]);
1518 fprintf (stderr
, "\n");
1519 error
= WRDE_BADVAL
;
1526 fprintf (stderr
, "%s: parameter null or not set\n", env
);
1527 error
= WRDE_BADVAL
;
1536 case ACT_NULL_SUBST
:
1537 if (value
&& *value
)
1538 /* Substitute parameter */
1541 if (!colon_seen
&& value
)
1543 /* Substitute NULL */
1553 /* Substitute word */
1562 /* No field-splitting is allowed, so imagine
1563 quotes around the word. */
1564 char *qtd_pattern
= malloc (3 + strlen (pattern
));
1566 sprintf (qtd_pattern
, "\"%s\"", pattern
);
1568 pattern
= qtd_pattern
;
1571 if (pattern
== NULL
&& (pattern
= __strdup ("")) == NULL
)
1574 error
= wordexp (pattern
, &we
, flags
);
1582 /* Fingers crossed that the quotes worked.. */
1583 assert (!quoted
|| we
.we_wordc
== 1);
1586 for (i
= 0; i
< we
.we_wordc
; ++i
)
1587 if (w_addword (pwordexp
, __strdup (we
.we_wordv
[i
]))
1591 if (i
< we
.we_wordc
)
1593 /* Ran out of space */
1598 if (action
== ACT_NULL_ASSIGN
)
1602 size_t words_size
= 0;
1605 /* Cannot assign special parameters. */
1608 for (i
= 0; i
< we
.we_wordc
; i
++)
1609 words_size
+= strlen (we
.we_wordv
[i
]) + 1; /* for <space> */
1612 cp
= words
= __alloca (words_size
);
1614 for (i
= 0; i
< we
.we_wordc
- 1; i
++)
1616 cp
= __stpcpy (cp
, we
.we_wordv
[i
]);
1620 strcpy (cp
, we
.we_wordv
[i
]);
1623 setenv (env
, words
, 1);
1632 case ACT_NONNULL_SUBST
:
1633 if (value
&& *value
)
1636 if (!colon_seen
&& value
)
1639 /* Substitute NULL */
1646 case ACT_NULL_ASSIGN
:
1647 if (value
&& *value
)
1648 /* Substitute parameter */
1651 if (!colon_seen
&& value
)
1653 /* Substitute NULL */
1661 /* This checks for '=' so it knows to assign */
1665 assert (! "Unrecognised action!");
1674 char param_length
[21];
1675 param_length
[20] = '\0';
1676 *word
= w_addstr (*word
, word_length
, max_length
,
1677 _itoa_word (value
? strlen (value
) : 0,
1678 ¶m_length
[20], 10, 0));
1681 assert (value
!= NULL
);
1685 return *word
? 0 : WRDE_NOSPACE
;
1691 if (quoted
|| !pwordexp
)
1693 /* Quoted - no field split */
1694 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1698 return *word
? 0 : WRDE_NOSPACE
;
1702 /* Need to field-split */
1703 char *value_copy
= __strdup (value
); /* Don't modify value */
1704 char *field_begin
= value_copy
;
1705 int seen_nonws_ifs
= 0;
1710 if (value_copy
== NULL
)
1711 return WRDE_NOSPACE
;
1715 char *field_end
= field_begin
;
1718 /* If this isn't the first field, start a new word */
1719 if (field_begin
!= value_copy
)
1721 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1724 return WRDE_NOSPACE
;
1727 *word
= w_newword (word_length
, max_length
);
1730 /* Skip IFS whitespace before the field */
1731 field_begin
+= strspn (field_begin
, ifs_white
);
1733 if (!seen_nonws_ifs
&& *field_begin
== 0)
1734 /* Nothing but whitespace */
1737 /* Search for the end of the field */
1738 field_end
= field_begin
+ strcspn (field_begin
, ifs
);
1740 /* Set up pointer to the character after end of field and
1741 skip whitespace IFS after it. */
1742 next_field
= field_end
+ strspn (field_end
, ifs_white
);
1744 /* Skip at most one non-whitespace IFS character after the field */
1746 if (*next_field
&& strchr (ifs
, *next_field
))
1752 /* Null-terminate it */
1755 /* Tag a copy onto the current word */
1756 *word
= w_addstr (*word
, word_length
, max_length
, field_begin
);
1761 return WRDE_NOSPACE
;
1764 field_begin
= next_field
;
1766 while (seen_nonws_ifs
|| *field_begin
);
1780 return WRDE_NOSPACE
;
1794 parse_dollars (char **word
, size_t *word_length
, size_t *max_length
,
1795 const char *words
, size_t *offset
, int flags
,
1796 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
,
1799 /* We are poised _at_ "$" */
1800 switch (words
[1 + *offset
])
1805 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1806 return *word
? 0 : WRDE_NOSPACE
;
1809 if (words
[2 + *offset
] == '(')
1811 /* Differentiate between $((1+3)) and $((echo);(ls)) */
1812 int i
= 3 + *offset
;
1814 while (words
[i
] && !(depth
== 0 && words
[i
] == ')'))
1816 if (words
[i
] == '(')
1818 else if (words
[i
] == ')')
1824 if (words
[i
] == ')' && words
[i
+ 1] == ')')
1827 /* Call parse_arith -- 0 is for "no brackets" */
1828 return parse_arith (word
, word_length
, max_length
, words
, offset
,
1833 if (flags
& WRDE_NOCMD
)
1837 return parse_comm (word
, word_length
, max_length
, words
, offset
, flags
,
1838 quoted
? NULL
: pwordexp
, ifs
, ifs_white
);
1842 /* Call parse_arith -- 1 is for "brackets" */
1843 return parse_arith (word
, word_length
, max_length
, words
, offset
, flags
,
1848 ++(*offset
); /* parse_param needs to know if "{" is there */
1849 return parse_param (word
, word_length
, max_length
, words
, offset
, flags
,
1850 pwordexp
, ifs
, ifs_white
, quoted
);
1855 parse_backtick (char **word
, size_t *word_length
, size_t *max_length
,
1856 const char *words
, size_t *offset
, int flags
,
1857 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
1859 /* We are poised just after "`" */
1864 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
1866 for (; words
[*offset
]; ++(*offset
))
1868 switch (words
[*offset
])
1871 /* Go -- give the script to the shell */
1872 error
= exec_comm (comm
, word
, word_length
, max_length
, flags
,
1873 pwordexp
, ifs
, ifs_white
);
1880 error
= parse_qtd_backslash (&comm
, &comm_length
, &comm_maxlen
,
1893 error
= parse_backslash (&comm
, &comm_length
, &comm_maxlen
, words
,
1905 squoting
= 1 - squoting
;
1907 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
1909 return WRDE_NOSPACE
;
1920 parse_dquote (char **word
, size_t *word_length
, size_t *max_length
,
1921 const char *words
, size_t *offset
, int flags
,
1922 wordexp_t
*pwordexp
, const char * ifs
, const char * ifs_white
)
1924 /* We are poised just after a double-quote */
1927 for (; words
[*offset
]; ++(*offset
))
1929 switch (words
[*offset
])
1935 error
= parse_dollars (word
, word_length
, max_length
, words
, offset
,
1936 flags
, pwordexp
, ifs
, ifs_white
, 1);
1937 /* The ``1'' here is to tell parse_dollars not to
1938 * split the fields. It may need to, however ("$@").
1946 if (flags
& WRDE_NOCMD
)
1950 error
= parse_backtick (word
, word_length
, max_length
, words
,
1951 offset
, flags
, NULL
, NULL
, NULL
);
1952 /* The first NULL here is to tell parse_backtick not to
1961 error
= parse_qtd_backslash (word
, word_length
, max_length
, words
,
1970 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
1972 return WRDE_NOSPACE
;
1976 /* Unterminated string */
1981 * wordfree() is to be called after pwordexp is finished with.
1985 wordfree (wordexp_t
*pwordexp
)
1988 /* wordexp can set pwordexp to NULL */
1989 if (pwordexp
&& pwordexp
->we_wordv
)
1991 char **wordv
= pwordexp
->we_wordv
;
1993 for (wordv
+= pwordexp
->we_offs
; *wordv
; ++wordv
)
1996 free (pwordexp
->we_wordv
);
1997 pwordexp
->we_wordv
= NULL
;
2006 wordexp (const char *words
, wordexp_t
*pwordexp
, int flags
)
2008 size_t wordv_offset
;
2009 size_t words_offset
;
2012 char *word
= w_newword (&word_length
, &max_length
);
2016 char **old_wordv
= pwordexp
->we_wordv
;
2017 size_t old_wordc
= (flags
& WRDE_REUSE
) ? pwordexp
->we_wordc
: 0;
2019 if (flags
& WRDE_REUSE
)
2021 /* Minimal implementation of WRDE_REUSE for now */
2022 wordfree (pwordexp
);
2026 if (flags
& WRDE_DOOFFS
)
2028 pwordexp
->we_wordv
= calloc (1 + pwordexp
->we_offs
, sizeof (char *));
2029 if (pwordexp
->we_wordv
== NULL
)
2030 return WRDE_NOSPACE
;
2034 pwordexp
->we_wordv
= calloc (1, sizeof (char *));
2035 if (pwordexp
->we_wordv
== NULL
)
2036 return WRDE_NOSPACE
;
2038 pwordexp
->we_offs
= 0;
2041 if ((flags
& WRDE_APPEND
) == 0)
2042 pwordexp
->we_wordc
= 0;
2044 wordv_offset
= pwordexp
->we_offs
+ pwordexp
->we_wordc
;
2046 /* Find out what the field separators are.
2047 * There are two types: whitespace and non-whitespace.
2049 ifs
= getenv ("IFS");
2052 /* NULL IFS means no field-splitting is to be performed */
2053 ifs
= strcpy (ifs_white
, "");
2057 char *whch
= ifs_white
;
2059 /* Start off with no whitespace IFS characters */
2060 ifs_white
[0] = '\0';
2062 while (*ifsch
!= '\0')
2064 if ((*ifsch
== ' ') || (*ifsch
== '\t') || (*ifsch
== '\n'))
2066 /* Whitespace IFS. See first whether it is already in our
2068 char *runp
= ifs_white
;
2070 while (runp
< whch
&& *runp
!= '\0' && *runp
!= *ifsch
)
2082 for (words_offset
= 0 ; words
[words_offset
] ; ++words_offset
)
2083 switch (words
[words_offset
])
2096 wordfree (pwordexp
);
2097 pwordexp
->we_wordc
= 0;
2098 pwordexp
->we_wordv
= old_wordv
;
2099 return WRDE_BADCHAR
;
2102 error
= parse_backslash (&word
, &word_length
, &max_length
, words
,
2111 error
= parse_dollars (&word
, &word_length
, &max_length
, words
,
2112 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
,
2121 if (flags
& WRDE_NOCMD
)
2125 error
= parse_backtick (&word
, &word_length
, &max_length
, words
,
2126 &words_offset
, flags
, pwordexp
, ifs
,
2136 error
= parse_dquote (&word
, &word_length
, &max_length
, words
,
2137 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2146 error
= parse_squote (&word
, &word_length
, &max_length
, words
,
2155 error
= parse_tilde (&word
, &word_length
, &max_length
, words
,
2156 &words_offset
, pwordexp
->we_wordc
);
2166 error
= parse_glob (&word
, &word_length
, &max_length
, words
,
2167 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2175 /* Is it a field separator? */
2176 if (strchr (ifs
, words
[words_offset
]) == NULL
)
2178 /* "Ordinary" character -- add it to word */
2180 word
= w_addchar (word
, &word_length
, &max_length
,
2181 words
[words_offset
]);
2184 error
= WRDE_NOSPACE
;
2191 /* Field separator */
2192 if (strchr (ifs_white
, words
[words_offset
]))
2194 /* It's a whitespace IFS char. Ignore it at the beginning
2195 of a line and ignore multiple instances. */
2196 if (!word
|| !*word
)
2199 if (w_addword (pwordexp
, word
) == WRDE_NOSPACE
)
2201 error
= WRDE_NOSPACE
;
2205 word
= w_newword (&word_length
, &max_length
);
2209 /* It's a non-whitespace IFS char */
2211 /* Multiple non-whitespace IFS chars are treated as one. */
2214 if (w_addword (pwordexp
, word
) == WRDE_NOSPACE
)
2216 error
= WRDE_NOSPACE
;
2221 word
= w_newword (&word_length
, &max_length
);
2226 /* There was a field separator at the end */
2230 /* There was no field separator at the end */
2231 return w_addword (pwordexp
, word
);
2235 * free memory used (unless error is WRDE_NOSPACE), and
2236 * set we_wordc and wd_wordv back to what they were.
2239 if (error
== WRDE_NOSPACE
)
2240 return WRDE_NOSPACE
;
2245 wordfree (pwordexp
);
2246 pwordexp
->we_wordv
= old_wordv
;
2247 pwordexp
->we_wordc
= old_wordc
;