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. */
77 /* Result of w_newword will be ignored if it 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';
140 /* Result of w_emptyword will not be ignored even if it is the last. */
142 w_emptyword (size_t *actlen
, size_t *maxlen
)
144 char *word
= malloc (1 + W_CHUNK
);
156 w_addstr (char *buffer
, size_t *actlen
, size_t *maxlen
, const char *str
)
157 /* (lengths exclude trailing zero) */
159 /* Add a string to the buffer, allocating room for it if needed.
163 assert (str
!= NULL
); /* w_addstr only called from this file */
166 return w_addmem (buffer
, actlen
, maxlen
, str
, len
);
171 w_addword (wordexp_t
*pwordexp
, char *word
)
173 /* Add a word to the wordlist */
177 /* Internally, NULL acts like "". Convert NULLs to "" before
178 * the caller sees them.
182 word
= __strdup ("");
187 num_p
= 2 + pwordexp
->we_wordc
+ pwordexp
->we_offs
;
188 new_wordv
= realloc (pwordexp
->we_wordv
, sizeof (char *) * num_p
);
189 if (new_wordv
!= NULL
)
191 pwordexp
->we_wordv
= new_wordv
;
192 pwordexp
->we_wordv
[pwordexp
->we_wordc
++] = word
;
193 pwordexp
->we_wordv
[pwordexp
->we_wordc
] = NULL
;
201 /* The parse_*() functions should leave *offset being the offset in 'words'
202 * to the last character processed.
207 parse_backslash (char **word
, size_t *word_length
, size_t *max_length
,
208 const char *words
, size_t *offset
)
210 /* We are poised _at_ a backslash, not in quotes */
212 switch (words
[1 + *offset
])
215 /* Backslash is last character of input words */
223 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
236 parse_qtd_backslash (char **word
, size_t *word_length
, size_t *max_length
,
237 const char *words
, size_t *offset
)
239 /* We are poised _at_ a backslash, inside quotes */
241 switch (words
[1 + *offset
])
244 /* Backslash is last character of input words */
255 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
263 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
265 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
279 parse_tilde (char **word
, size_t *word_length
, size_t *max_length
,
280 const char *words
, size_t *offset
, size_t wordc
)
282 /* We are poised _at_ a tilde */
285 if (*word_length
!= 0)
287 if (!((*word
)[*word_length
- 1] == '=' && wordc
== 0))
289 if (!((*word
)[*word_length
- 1] == ':'
290 && strchr (*word
, '=') && wordc
== 0))
292 *word
= w_addchar (*word
, word_length
, max_length
, '~');
293 return *word
? 0 : WRDE_NOSPACE
;
298 for (i
= 1 + *offset
; words
[i
]; i
++)
300 if (words
[i
] == ':' || words
[i
] == '/' || words
[i
] == ' ' ||
301 words
[i
] == '\t' || words
[i
] == 0 )
304 if (words
[i
] == '\\')
306 *word
= w_addchar (*word
, word_length
, max_length
, '~');
307 return *word
? 0 : WRDE_NOSPACE
;
311 if (i
== 1 + *offset
)
313 /* Tilde appears on its own */
315 struct passwd pwd
, *tpwd
;
317 char* buffer
= __alloca (buflen
);
322 while ((result
= __getpwuid_r (uid
, &pwd
, buffer
, buflen
, &tpwd
)) != 0
326 buffer
= __alloca (buflen
);
329 if (result
== 0 && 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
, '~');
344 /* Look up user name in database to get home directory */
345 char *user
= __strndup (&words
[1 + *offset
], i
- *offset
);
346 struct passwd pwd
, *tpwd
;
348 char* buffer
= __alloca (buflen
);
351 while ((result
= __getpwnam_r (user
, &pwd
, buffer
, buflen
, &tpwd
)) != 0
355 buffer
= __alloca (buflen
);
358 if (result
== 0 && pwd
.pw_dir
)
359 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
362 /* (invalid login name) */
363 *word
= w_addchar (*word
, word_length
, max_length
, '~');
365 *word
= w_addstr (*word
, word_length
, max_length
, user
);
370 return *word
? 0 : WRDE_NOSPACE
;
376 do_parse_glob (const char *glob_word
, char **word
, size_t *word_length
,
377 size_t *max_length
, wordexp_t
*pwordexp
, const char *ifs
,
378 const char *ifs_white
)
384 error
= glob (glob_word
, GLOB_NOCHECK
, NULL
, &globbuf
);
388 /* We can only run into memory problems. */
389 assert (error
== GLOB_NOSPACE
);
395 /* No field splitting allowed. */
396 assert (globbuf
.gl_pathv
[0] != NULL
);
397 *word
= w_addstr (*word
, word_length
, max_length
, globbuf
.gl_pathv
[0]);
398 for (match
= 1; match
< globbuf
.gl_pathc
&& *word
!= NULL
; ++match
)
400 *word
= w_addchar (*word
, word_length
, max_length
, ' ');
402 *word
= w_addstr (*word
, word_length
, max_length
,
403 globbuf
.gl_pathv
[match
]);
407 return *word
? 0 : WRDE_NOSPACE
;
410 assert (ifs
== NULL
|| *ifs
!= '\0');
414 *word
= w_newword (word_length
, max_length
);
417 for (match
= 0; match
< globbuf
.gl_pathc
; ++match
)
419 char *matching_word
= __strdup (globbuf
.gl_pathv
[match
]);
420 if (matching_word
== NULL
|| w_addword (pwordexp
, matching_word
))
433 parse_glob (char **word
, size_t *word_length
, size_t *max_length
,
434 const char *words
, size_t *offset
, int flags
,
435 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
437 /* We are poised just after a '*', a '[' or a '?'. */
438 int error
= WRDE_NOSPACE
;
439 int quoted
= 0; /* 1 if singly-quoted, 2 if doubly */
441 wordexp_t glob_list
; /* List of words to glob */
443 glob_list
.we_wordc
= 0;
444 glob_list
.we_wordv
= NULL
;
445 glob_list
.we_offs
= 0;
446 for (; words
[*offset
] != '\0'; ++*offset
)
448 if ((ifs
&& strchr (ifs
, words
[*offset
])) ||
449 (!ifs
&& strchr (" \t\n", words
[*offset
])))
453 /* Sort out quoting */
454 if (words
[*offset
] == '\'')
461 else if (quoted
== 1)
467 else if (words
[*offset
] == '"')
474 else if (quoted
== 2)
481 /* Sort out other special characters */
482 if (quoted
!= 1 && words
[*offset
] == '$')
484 error
= parse_dollars (word
, word_length
, max_length
, words
,
485 offset
, flags
, &glob_list
, ifs
, ifs_white
,
492 else if (words
[*offset
] == '\\')
495 error
= parse_qtd_backslash (word
, word_length
, max_length
,
498 error
= parse_backslash (word
, word_length
, max_length
,
507 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
512 /* Don't forget to re-parse the character we stopped at. */
516 error
= w_addword (&glob_list
, *word
);
517 *word
= w_newword (word_length
, max_length
);
518 for (i
= 0; error
== 0 && i
< glob_list
.we_wordc
; i
++)
519 error
= do_parse_glob (glob_list
.we_wordv
[i
], word
, word_length
,
520 max_length
, pwordexp
, ifs
, ifs_white
);
524 wordfree (&glob_list
);
530 parse_squote (char **word
, size_t *word_length
, size_t *max_length
,
531 const char *words
, size_t *offset
)
533 /* We are poised just after a single quote */
534 for (; words
[*offset
]; ++(*offset
))
536 if (words
[*offset
] != '\'')
538 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
545 /* Unterminated string */
549 /* Functions to evaluate an arithmetic expression */
552 eval_expr_val (char **expr
, long int *result
)
557 /* Skip white space */
558 for (digit
= *expr
; digit
&& *digit
&& isspace (*digit
); ++digit
);
564 /* Scan for closing paren */
565 for (++digit
; **expr
&& **expr
!= ')'; ++(*expr
));
573 if (eval_expr (digit
, result
))
578 case '+': /* Positive value */
582 case '-': /* Negative value */
588 if (!isdigit (*digit
))
593 for (; *digit
&& isdigit (*digit
); ++digit
)
594 *result
= (*result
* 10) + (*digit
- '0');
603 eval_expr_multdiv (char **expr
, long int *result
)
608 if (eval_expr_val (expr
, result
) != 0)
613 /* Skip white space */
614 for (; *expr
&& **expr
&& isspace (**expr
); ++(*expr
));
619 if (eval_expr_val (expr
, &arg
) != 0)
624 else if (**expr
== '/')
627 if (eval_expr_val (expr
, &arg
) != 0)
640 eval_expr (char *expr
, long int *result
)
645 if (eval_expr_multdiv (&expr
, result
) != 0)
650 /* Skip white space */
651 for (; expr
&& *expr
&& isspace (*expr
); ++expr
);
656 if (eval_expr_multdiv (&expr
, &arg
) != 0)
661 else if (*expr
== '-')
664 if (eval_expr_multdiv (&expr
, &arg
) != 0)
677 parse_arith (char **word
, size_t *word_length
, size_t *max_length
,
678 const char *words
, size_t *offset
, int flags
, int bracket
)
680 /* We are poised just after "$((" or "$[" */
687 expr
= w_newword (&expr_length
, &expr_maxlen
);
688 for (; words
[*offset
]; ++(*offset
))
690 switch (words
[*offset
])
693 error
= parse_dollars (&expr
, &expr_length
, &expr_maxlen
,
694 words
, offset
, flags
, NULL
, NULL
, NULL
, 1);
695 /* The ``1'' here is to tell parse_dollars not to
707 error
= parse_backtick (&expr
, &expr_length
, &expr_maxlen
,
708 words
, offset
, flags
, NULL
, NULL
, NULL
);
709 /* The first NULL here is to tell parse_backtick not to
720 error
= parse_qtd_backslash (&expr
, &expr_length
, &expr_maxlen
,
727 /* I think that a backslash within an
728 * arithmetic expansion is bound to
729 * cause an error sooner or later anyway though.
734 if (--paren_depth
== 0)
736 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
737 long int numresult
= 0;
738 long long int convertme
;
740 if (bracket
|| words
[1 + *offset
] != ')')
746 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
751 convertme
= -numresult
;
752 *word
= w_addchar (*word
, word_length
, max_length
, '-');
760 convertme
= numresult
;
763 *word
= w_addstr (*word
, word_length
, max_length
,
764 _itoa (convertme
, &result
[20], 10, 0));
766 return *word
? 0 : WRDE_NOSPACE
;
768 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
775 if (bracket
&& paren_depth
== 1)
777 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
778 long int numresult
= 0;
781 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
785 *word
= w_addstr (*word
, word_length
, max_length
,
786 _itoa_word (numresult
, &result
[20], 10, 0));
788 return *word
? 0 : WRDE_NOSPACE
;
804 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
815 /* Function to execute a command and retrieve the results */
816 /* pwordexp contains NULL if field-splitting is forbidden */
819 exec_comm (char *comm
, char **word
, size_t *word_length
, size_t *max_length
,
820 int flags
, wordexp_t
*pwordexp
, const char *ifs
,
821 const char *ifs_white
)
829 int keep_empty_word
= 0;
831 /* Don't fork() unless necessary */
839 if ((pid
= __fork ()) < 0)
848 const char *args
[4] = { _PATH_BSHELL
, "-c", comm
, NULL
};
850 /* Redirect output. */
851 __dup2 (fildes
[1], 1);
854 /* Redirect stderr to /dev/null if we have to. */
855 if ((flags
& WRDE_SHOWERR
) == 0)
859 fd
= __open (_PATH_DEVNULL
, O_WRONLY
);
860 if (fd
>= 0 && fd
!= 2)
868 __execve (_PATH_BSHELL
, (char *const *) args
, __environ
);
877 buffer
= __alloca (bufsize
);
880 { /* Quoted - no field splitting */
884 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
886 if (__waitpid (pid
, NULL
, WNOHANG
) == 0)
888 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
892 *word
= w_addmem (*word
, word_length
, max_length
, buffer
, buflen
);
895 __kill (pid
, SIGKILL
);
896 __waitpid (pid
, NULL
, 0);
903 /* Not quoted - split fields */
907 * 0 when searching for first character in a field not IFS white space
908 * 1 when copying the text of a field
909 * 2 when searching for possible non-whitespace IFS
914 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
916 if (__waitpid (pid
, NULL
, WNOHANG
) == 0)
918 if ((__read (fildes
[0], buffer
, bufsize
)) < 1)
922 for (i
= 0; i
< buflen
; ++i
)
924 if (strchr (ifs
, buffer
[i
]) != NULL
)
926 /* Current character is IFS */
927 if (strchr (ifs_white
, buffer
[i
]) == NULL
)
929 /* Current character is IFS but not whitespace */
931 /* After this delimiter, another field must result.
940 * eg: text<space><comma><space>moretext
942 * So, strip whitespace IFS (like at the start)
949 /* fall through and delimit field.. */
953 /* Current character is IFS white space */
955 /* If not copying a field, ignore it */
959 /* End of field (search for non-ws IFS afterwards) */
963 /* First IFS white space, or IFS non-whitespace.
964 * Delimit the field. Nulls are converted by w_addword. */
965 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
967 __kill (pid
, SIGKILL
);
968 __waitpid (pid
, NULL
, 0);
974 *word
= w_emptyword (word_length
, max_length
);
976 *word
= w_newword (word_length
, max_length
);
977 /* fall back round the loop.. */
981 /* Not IFS character */
984 if (buffer
[i
] != '\n')
987 *word
= w_addchar (*word
, word_length
, max_length
,
991 __kill (pid
, SIGKILL
);
992 __waitpid (pid
, NULL
, 0);
1001 /* Bash chops off trailing newlines, which seems sensible. */
1002 while (*word_length
> 0 && (*word
)[*word_length
- 1] == '\n')
1004 (*word
)[--*word_length
] = '\0';
1006 /* If the last word was entirely newlines, and the previous word
1007 * wasn't delimited with IFS non-whitespace, turn it into a new word
1008 * which can be ignored if there's nothing following it. */
1009 if (!keep_empty_word
&& *word_length
== 0)
1012 *word
= w_newword (word_length
, max_length
);
1016 __close (fildes
[0]);
1022 parse_comm (char **word
, size_t *word_length
, size_t *max_length
,
1023 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1024 const char *ifs
, const char *ifs_white
)
1026 /* We are poised just after "$(" */
1027 int paren_depth
= 1;
1029 int quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1032 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
1034 for (; words
[*offset
]; ++(*offset
))
1036 switch (words
[*offset
])
1041 else if (quoted
== 1)
1049 else if (quoted
== 2)
1055 if (!quoted
&& --paren_depth
== 0)
1057 /* Go -- give script to the shell */
1060 error
= exec_comm (comm
, word
, word_length
, max_length
,
1061 flags
, pwordexp
, ifs
, ifs_white
);
1068 /* This is just part of the script */
1076 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
1078 return WRDE_NOSPACE
;
1090 parse_param (char **word
, size_t *word_length
, size_t *max_length
,
1091 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1092 const char *ifs
, const char *ifs_white
, int quoted
)
1094 /* We are poised just after "$" */
1098 ACT_RP_SHORT_LEFT
= '#',
1099 ACT_RP_LONG_LEFT
= 'L',
1100 ACT_RP_SHORT_RIGHT
= '%',
1101 ACT_RP_LONG_RIGHT
= 'R',
1102 ACT_NULL_ERROR
= '?',
1103 ACT_NULL_SUBST
= '-',
1104 ACT_NONNULL_SUBST
= '+',
1105 ACT_NULL_ASSIGN
= '='
1111 size_t start
= *offset
;
1115 enum action action
= ACT_NONE
;
1120 int pattern_is_quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1124 int brace
= words
[*offset
] == '{';
1126 env
= w_newword (&env_length
, &env_maxlen
);
1127 pattern
= w_newword (&pat_length
, &pat_maxlen
);
1132 /* First collect the parameter name. */
1134 if (words
[*offset
] == '#')
1142 if (isalpha (words
[*offset
]) || words
[*offset
] == '_')
1144 /* Normal parameter name. */
1147 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1152 while (isalnum (words
[++*offset
]) || words
[*offset
] == '_');
1154 else if (isdigit (words
[*offset
]))
1156 /* Numeric parameter name. */
1160 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1167 while (isdigit(words
[++*offset
]));
1169 else if (strchr ("*@$", words
[*offset
]) != NULL
)
1171 /* Special parameter. */
1173 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1187 /* Check for special action to be applied to the value. */
1188 switch (words
[*offset
])
1195 action
= ACT_RP_SHORT_LEFT
;
1196 if (words
[1 + *offset
] == '#')
1199 action
= ACT_RP_LONG_LEFT
;
1204 action
= ACT_RP_SHORT_RIGHT
;
1205 if (words
[1 + *offset
] == '%')
1208 action
= ACT_RP_LONG_RIGHT
;
1213 if (strchr ("-=?+", words
[1 + *offset
]) == NULL
)
1217 action
= words
[++*offset
];
1224 action
= words
[*offset
];
1231 /* Now collect the pattern. */
1233 for (; words
[*offset
]; ++(*offset
))
1235 switch (words
[*offset
])
1238 if (!pattern_is_quoted
)
1243 if (!pattern_is_quoted
)
1252 if (!pattern_is_quoted
&& words
[++*offset
] == '\0')
1257 if (pattern_is_quoted
== 0)
1258 pattern_is_quoted
= 1;
1259 else if (pattern_is_quoted
== 1)
1260 pattern_is_quoted
= 0;
1265 if (pattern_is_quoted
== 0)
1266 pattern_is_quoted
= 2;
1267 else if (pattern_is_quoted
== 2)
1268 pattern_is_quoted
= 0;
1273 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
,
1275 if (pattern
== NULL
)
1280 /* End of input string -- remember to reparse the character that we
1285 if (words
[start
] == '{' && words
[*offset
] != '}')
1292 /* $# expands to the number of positional parameters */
1294 value
= _itoa_word (__libc_argc
- 1, &buffer
[20], 10, 0);
1299 /* Just $ on its own */
1300 *offset
= start
- 1;
1301 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1302 return *word
? 0 : WRDE_NOSPACE
;
1305 /* Is it a numeric parameter? */
1306 else if (isdigit (env
[0]))
1310 if (n
>= __libc_argc
)
1311 /* Substitute NULL. */
1314 /* Replace with appropriate positional parameter. */
1315 value
= __libc_argv
[n
];
1317 /* Is it a special parameter? */
1324 value
= _itoa_word (__getpid (), &buffer
[20], 10, 0);
1326 /* Is it `${#*}' or `${#@}'? */
1327 else if ((*env
== '*' || *env
== '@') && seen_hash
)
1330 value
= _itoa_word (__libc_argc
> 0 ? __libc_argc
- 1 : 0,
1331 &buffer
[20], 10, 0);
1332 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1334 return *word
? 0 : WRDE_NOSPACE
;
1336 /* Is it `$*' or `$@' (unquoted) ? */
1337 else if (*env
== '*' || (*env
== '@' && !quoted
))
1339 size_t plist_len
= 0;
1343 /* Build up value parameter by parameter (copy them) */
1344 for (p
= 1; __libc_argv
[p
]; ++p
)
1345 plist_len
+= strlen (__libc_argv
[p
]) + 1; /* for space */
1346 value
= malloc (plist_len
);
1351 for (p
= 1; __libc_argv
[p
]; ++p
)
1355 end
= __stpcpy (end
, __libc_argv
[p
]);
1362 /* Must be a quoted `$@' */
1363 assert (*env
== '@' && quoted
);
1365 /* Each parameter is a separate word ("$@") */
1366 if (__libc_argc
== 2)
1367 value
= __libc_argv
[1];
1368 else if (__libc_argc
> 2)
1372 /* Append first parameter to current word. */
1373 value
= w_addstr (*word
, word_length
, max_length
,
1375 if (value
== NULL
|| w_addword (pwordexp
, value
))
1378 for (p
= 2; __libc_argv
[p
+ 1]; p
++)
1380 char *newword
= __strdup (__libc_argv
[p
]);
1381 if (newword
== NULL
|| w_addword (pwordexp
, newword
))
1385 /* Start a new word with the last parameter. */
1386 *word
= w_newword (word_length
, max_length
);
1387 value
= __libc_argv
[p
];
1398 value
= getenv (env
);
1400 if (value
== NULL
&& (flags
& WRDE_UNDEF
))
1402 /* Variable not defined. */
1410 if (action
!= ACT_NONE
)
1414 case ACT_RP_SHORT_LEFT
:
1415 case ACT_RP_LONG_LEFT
:
1416 case ACT_RP_SHORT_RIGHT
:
1417 case ACT_RP_LONG_RIGHT
:
1423 if (value
== NULL
|| pattern
== NULL
|| *pattern
== '\0')
1426 end
= value
+ strlen (value
);
1430 case ACT_RP_SHORT_LEFT
:
1431 for (p
= value
; p
<= end
; ++p
)
1435 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1440 char *newval
= __strdup (p
);
1458 case ACT_RP_LONG_LEFT
:
1459 for (p
= end
; p
>= value
; --p
)
1463 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1468 char *newval
= __strdup (p
);
1486 case ACT_RP_SHORT_RIGHT
:
1487 for (p
= end
; p
>= value
; --p
)
1489 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1492 newval
= malloc (p
- value
+ 1);
1495 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1506 case ACT_RP_LONG_RIGHT
:
1507 for (p
= value
; p
<= end
; ++p
)
1509 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1512 newval
= malloc (p
- value
+ 1);
1515 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1533 case ACT_NULL_ERROR
:
1534 if (value
&& *value
)
1535 /* Substitute parameter */
1538 if (!colon_seen
&& value
)
1539 /* Substitute NULL */
1543 /* Expand 'pattern' and write it to stderr */
1546 error
= wordexp (pattern
, &we
, flags
);
1552 fprintf (stderr
, "%s:", env
);
1554 for (i
= 0; i
< we
.we_wordc
; ++i
)
1556 fprintf (stderr
, " %s", we
.we_wordv
[i
]);
1559 fprintf (stderr
, "\n");
1560 error
= WRDE_BADVAL
;
1567 fprintf (stderr
, "%s: parameter null or not set\n", env
);
1568 error
= WRDE_BADVAL
;
1577 case ACT_NULL_SUBST
:
1578 if (value
&& *value
)
1579 /* Substitute parameter */
1582 if (!colon_seen
&& value
)
1584 /* Substitute NULL */
1594 /* Substitute word */
1603 /* No field-splitting is allowed, so imagine
1604 quotes around the word. */
1605 char *qtd_pattern
= malloc (3 + strlen (pattern
));
1607 sprintf (qtd_pattern
, "\"%s\"", pattern
);
1609 pattern
= qtd_pattern
;
1612 if (pattern
== NULL
&& (pattern
= __strdup ("")) == NULL
)
1615 error
= wordexp (pattern
, &we
, flags
);
1623 /* Fingers crossed that the quotes worked.. */
1624 assert (!quoted
|| we
.we_wordc
== 1);
1627 for (i
= 0; i
< we
.we_wordc
; ++i
)
1628 if (w_addword (pwordexp
, __strdup (we
.we_wordv
[i
]))
1632 if (i
< we
.we_wordc
)
1634 /* Ran out of space */
1639 if (action
== ACT_NULL_ASSIGN
)
1643 size_t words_size
= 0;
1646 /* Cannot assign special parameters. */
1649 for (i
= 0; i
< we
.we_wordc
; i
++)
1650 words_size
+= strlen (we
.we_wordv
[i
]) + 1; /* for <space> */
1653 cp
= words
= __alloca (words_size
);
1655 for (i
= 0; i
< we
.we_wordc
- 1; i
++)
1657 cp
= __stpcpy (cp
, we
.we_wordv
[i
]);
1661 strcpy (cp
, we
.we_wordv
[i
]);
1664 setenv (env
, words
, 1);
1673 case ACT_NONNULL_SUBST
:
1674 if (value
&& *value
)
1677 if (!colon_seen
&& value
)
1680 /* Substitute NULL */
1687 case ACT_NULL_ASSIGN
:
1688 if (value
&& *value
)
1689 /* Substitute parameter */
1692 if (!colon_seen
&& value
)
1694 /* Substitute NULL */
1702 /* This checks for '=' so it knows to assign */
1706 assert (! "Unrecognised action!");
1715 char param_length
[21];
1716 param_length
[20] = '\0';
1717 *word
= w_addstr (*word
, word_length
, max_length
,
1718 _itoa_word (value
? strlen (value
) : 0,
1719 ¶m_length
[20], 10, 0));
1722 assert (value
!= NULL
);
1726 return *word
? 0 : WRDE_NOSPACE
;
1732 if (quoted
|| !pwordexp
)
1734 /* Quoted - no field split */
1735 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1739 return *word
? 0 : WRDE_NOSPACE
;
1743 /* Need to field-split */
1744 char *value_copy
= __strdup (value
); /* Don't modify value */
1745 char *field_begin
= value_copy
;
1746 int seen_nonws_ifs
= 0;
1751 if (value_copy
== NULL
)
1756 char *field_end
= field_begin
;
1759 /* If this isn't the first field, start a new word */
1760 if (field_begin
!= value_copy
)
1762 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1768 *word
= w_emptyword (word_length
, max_length
);
1771 /* Skip IFS whitespace before the field */
1772 field_begin
+= strspn (field_begin
, ifs_white
);
1774 if (!seen_nonws_ifs
&& *field_begin
== 0)
1775 /* Nothing but whitespace */
1778 /* Search for the end of the field */
1779 field_end
= field_begin
+ strcspn (field_begin
, ifs
);
1781 /* Set up pointer to the character after end of field and
1782 skip whitespace IFS after it. */
1783 next_field
= field_end
+ strspn (field_end
, ifs_white
);
1785 /* Skip at most one non-whitespace IFS character after the field */
1787 if (*next_field
&& strchr (ifs
, *next_field
))
1793 /* Null-terminate it */
1796 /* Tag a copy onto the current word */
1797 *word
= w_addstr (*word
, word_length
, max_length
, field_begin
);
1799 if (*word
== NULL
&& *field_begin
!= '\0')
1805 field_begin
= next_field
;
1807 while (seen_nonws_ifs
|| *field_begin
);
1821 return WRDE_NOSPACE
;
1835 parse_dollars (char **word
, size_t *word_length
, size_t *max_length
,
1836 const char *words
, size_t *offset
, int flags
,
1837 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
,
1840 /* We are poised _at_ "$" */
1841 switch (words
[1 + *offset
])
1846 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1847 return *word
? 0 : WRDE_NOSPACE
;
1850 if (words
[2 + *offset
] == '(')
1852 /* Differentiate between $((1+3)) and $((echo);(ls)) */
1853 int i
= 3 + *offset
;
1855 while (words
[i
] && !(depth
== 0 && words
[i
] == ')'))
1857 if (words
[i
] == '(')
1859 else if (words
[i
] == ')')
1865 if (words
[i
] == ')' && words
[i
+ 1] == ')')
1868 /* Call parse_arith -- 0 is for "no brackets" */
1869 return parse_arith (word
, word_length
, max_length
, words
, offset
,
1874 if (flags
& WRDE_NOCMD
)
1878 return parse_comm (word
, word_length
, max_length
, words
, offset
, flags
,
1879 quoted
? NULL
: pwordexp
, ifs
, ifs_white
);
1883 /* Call parse_arith -- 1 is for "brackets" */
1884 return parse_arith (word
, word_length
, max_length
, words
, offset
, flags
,
1889 ++(*offset
); /* parse_param needs to know if "{" is there */
1890 return parse_param (word
, word_length
, max_length
, words
, offset
, flags
,
1891 pwordexp
, ifs
, ifs_white
, quoted
);
1896 parse_backtick (char **word
, size_t *word_length
, size_t *max_length
,
1897 const char *words
, size_t *offset
, int flags
,
1898 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
1900 /* We are poised just after "`" */
1905 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
1907 for (; words
[*offset
]; ++(*offset
))
1909 switch (words
[*offset
])
1912 /* Go -- give the script to the shell */
1913 error
= exec_comm (comm
, word
, word_length
, max_length
, flags
,
1914 pwordexp
, ifs
, ifs_white
);
1921 error
= parse_qtd_backslash (&comm
, &comm_length
, &comm_maxlen
,
1934 error
= parse_backslash (&comm
, &comm_length
, &comm_maxlen
, words
,
1946 squoting
= 1 - squoting
;
1948 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
1950 return WRDE_NOSPACE
;
1961 parse_dquote (char **word
, size_t *word_length
, size_t *max_length
,
1962 const char *words
, size_t *offset
, int flags
,
1963 wordexp_t
*pwordexp
, const char * ifs
, const char * ifs_white
)
1965 /* We are poised just after a double-quote */
1968 for (; words
[*offset
]; ++(*offset
))
1970 switch (words
[*offset
])
1976 error
= parse_dollars (word
, word_length
, max_length
, words
, offset
,
1977 flags
, pwordexp
, ifs
, ifs_white
, 1);
1978 /* The ``1'' here is to tell parse_dollars not to
1979 * split the fields. It may need to, however ("$@").
1987 if (flags
& WRDE_NOCMD
)
1991 error
= parse_backtick (word
, word_length
, max_length
, words
,
1992 offset
, flags
, NULL
, NULL
, NULL
);
1993 /* The first NULL here is to tell parse_backtick not to
2002 error
= parse_qtd_backslash (word
, word_length
, max_length
, words
,
2011 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
2013 return WRDE_NOSPACE
;
2017 /* Unterminated string */
2022 * wordfree() is to be called after pwordexp is finished with.
2026 wordfree (wordexp_t
*pwordexp
)
2029 /* wordexp can set pwordexp to NULL */
2030 if (pwordexp
&& pwordexp
->we_wordv
)
2032 char **wordv
= pwordexp
->we_wordv
;
2034 for (wordv
+= pwordexp
->we_offs
; *wordv
; ++wordv
)
2037 free (pwordexp
->we_wordv
);
2038 pwordexp
->we_wordv
= NULL
;
2047 wordexp (const char *words
, wordexp_t
*pwordexp
, int flags
)
2049 size_t wordv_offset
;
2050 size_t words_offset
;
2053 char *word
= w_newword (&word_length
, &max_length
);
2057 char **old_wordv
= pwordexp
->we_wordv
;
2058 size_t old_wordc
= (flags
& WRDE_REUSE
) ? pwordexp
->we_wordc
: 0;
2060 if (flags
& WRDE_REUSE
)
2062 /* Minimal implementation of WRDE_REUSE for now */
2063 wordfree (pwordexp
);
2067 if (flags
& WRDE_DOOFFS
)
2069 pwordexp
->we_wordv
= calloc (1 + pwordexp
->we_offs
, sizeof (char *));
2070 if (pwordexp
->we_wordv
== NULL
)
2071 return WRDE_NOSPACE
;
2075 pwordexp
->we_wordv
= calloc (1, sizeof (char *));
2076 if (pwordexp
->we_wordv
== NULL
)
2077 return WRDE_NOSPACE
;
2079 pwordexp
->we_offs
= 0;
2082 if ((flags
& WRDE_APPEND
) == 0)
2083 pwordexp
->we_wordc
= 0;
2085 wordv_offset
= pwordexp
->we_offs
+ pwordexp
->we_wordc
;
2087 /* Find out what the field separators are.
2088 * There are two types: whitespace and non-whitespace.
2090 ifs
= getenv ("IFS");
2093 /* IFS unset - use <space><tab><newline>. */
2094 ifs
= strcpy (ifs_white
, " \t\n");
2098 char *whch
= ifs_white
;
2100 /* Start off with no whitespace IFS characters */
2101 ifs_white
[0] = '\0';
2103 while (*ifsch
!= '\0')
2105 if ((*ifsch
== ' ') || (*ifsch
== '\t') || (*ifsch
== '\n'))
2107 /* Whitespace IFS. See first whether it is already in our
2109 char *runp
= ifs_white
;
2111 while (runp
< whch
&& *runp
!= '\0' && *runp
!= *ifsch
)
2123 for (words_offset
= 0 ; words
[words_offset
] ; ++words_offset
)
2124 switch (words
[words_offset
])
2127 error
= parse_backslash (&word
, &word_length
, &max_length
, words
,
2136 error
= parse_dollars (&word
, &word_length
, &max_length
, words
,
2137 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
,
2146 if (flags
& WRDE_NOCMD
)
2150 error
= parse_backtick (&word
, &word_length
, &max_length
, words
,
2151 &words_offset
, flags
, pwordexp
, ifs
,
2161 error
= parse_dquote (&word
, &word_length
, &max_length
, words
,
2162 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2171 error
= parse_squote (&word
, &word_length
, &max_length
, words
,
2180 error
= parse_tilde (&word
, &word_length
, &max_length
, words
,
2181 &words_offset
, pwordexp
->we_wordc
);
2191 error
= parse_glob (&word
, &word_length
, &max_length
, words
,
2192 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2200 /* Is it a word separator? */
2201 if (strchr (" \t", words
[words_offset
]) == NULL
)
2203 char ch
= words
[words_offset
];
2205 /* Not a word separator -- but is it a valid word char? */
2206 if (strchr ("\n|&;<>(){}", ch
))
2209 wordfree (pwordexp
);
2210 pwordexp
->we_wordc
= 0;
2211 pwordexp
->we_wordv
= old_wordv
;
2212 return WRDE_BADCHAR
;
2215 /* "Ordinary" character -- add it to word */
2217 /* Convert IFS chars to blanks -- bash does this */
2218 if (strchr (ifs
, ch
))
2221 word
= w_addchar (word
, &word_length
, &max_length
,
2225 error
= WRDE_NOSPACE
;
2232 /* If a word has been delimited, add it to the list. */
2235 error
= w_addword (pwordexp
, word
);
2240 word
= w_newword (&word_length
, &max_length
);
2245 /* There was a word separator at the end */
2246 if (word
== NULL
) /* i.e. w_newword */
2249 /* There was no field separator at the end */
2250 return w_addword (pwordexp
, word
);
2254 * free memory used (unless error is WRDE_NOSPACE), and
2255 * set we_wordc and wd_wordv back to what they were.
2261 if (error
== WRDE_NOSPACE
)
2262 return WRDE_NOSPACE
;
2264 wordfree (pwordexp
);
2265 pwordexp
->we_wordv
= old_wordv
;
2266 pwordexp
->we_wordc
= old_wordc
;