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
] == '\'')
435 else if (quoted
== 1)
441 else if (words
[*offset
] == '"')
448 else if (quoted
== 2)
455 /* Sort out other special characters */
456 if (quoted
!= 1 && words
[*offset
] == '$')
458 error
= parse_dollars (word
, word_length
, max_length
, words
,
459 offset
, flags
, &glob_list
, ifs
, ifs_white
,
466 else if (words
[*offset
] == '\\')
469 error
= parse_qtd_backslash (word
, word_length
, max_length
,
472 error
= parse_backslash (word
, word_length
, max_length
,
481 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
486 /* Don't forget to re-parse the character we stopped at. */
490 error
= w_addword (&glob_list
, *word
);
491 *word
= w_newword (word_length
, max_length
);
492 for (i
= 0; error
== 0 && i
< glob_list
.we_wordc
; i
++)
493 error
= do_parse_glob (glob_list
.we_wordv
[i
], word
, word_length
,
494 max_length
, pwordexp
, ifs
, ifs_white
);
498 wordfree (&glob_list
);
504 parse_squote (char **word
, size_t *word_length
, size_t *max_length
,
505 const char *words
, size_t *offset
)
507 /* We are poised just after a single quote */
508 for (; words
[*offset
]; ++(*offset
))
510 if (words
[*offset
] != '\'')
512 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
519 /* Unterminated string */
523 /* Functions to evaluate an arithmetic expression */
526 eval_expr_val (char **expr
, long int *result
)
531 /* Skip white space */
532 for (digit
= *expr
; digit
&& *digit
&& isspace (*digit
); ++digit
);
538 /* Scan for closing paren */
539 for (++digit
; **expr
&& **expr
!= ')'; ++(*expr
));
547 if (eval_expr (digit
, result
))
552 case '+': /* Positive value */
556 case '-': /* Negative value */
562 if (!isdigit (*digit
))
567 for (; *digit
&& isdigit (*digit
); ++digit
)
568 *result
= (*result
* 10) + (*digit
- '0');
577 eval_expr_multdiv (char **expr
, long int *result
)
582 if (eval_expr_val (expr
, result
) != 0)
587 /* Skip white space */
588 for (; *expr
&& **expr
&& isspace (**expr
); ++(*expr
));
593 if (eval_expr_val (expr
, &arg
) != 0)
598 else if (**expr
== '/')
601 if (eval_expr_val (expr
, &arg
) != 0)
614 eval_expr (char *expr
, long int *result
)
619 if (eval_expr_multdiv (&expr
, result
) != 0)
624 /* Skip white space */
625 for (; expr
&& *expr
&& isspace (*expr
); ++expr
);
630 if (eval_expr_multdiv (&expr
, &arg
) != 0)
635 else if (*expr
== '-')
638 if (eval_expr_multdiv (&expr
, &arg
) != 0)
651 parse_arith (char **word
, size_t *word_length
, size_t *max_length
,
652 const char *words
, size_t *offset
, int flags
, int bracket
)
654 /* We are poised just after "$((" or "$[" */
661 expr
= w_newword (&expr_length
, &expr_maxlen
);
662 for (; words
[*offset
]; ++(*offset
))
664 switch (words
[*offset
])
667 error
= parse_dollars (&expr
, &expr_length
, &expr_maxlen
,
668 words
, offset
, flags
, NULL
, NULL
, NULL
, 1);
669 /* The ``1'' here is to tell parse_dollars not to
681 error
= parse_backtick (&expr
, &expr_length
, &expr_maxlen
,
682 words
, offset
, flags
, NULL
, NULL
, NULL
);
683 /* The first NULL here is to tell parse_backtick not to
694 error
= parse_qtd_backslash (&expr
, &expr_length
, &expr_maxlen
,
701 /* I think that a backslash within an
702 * arithmetic expansion is bound to
703 * cause an error sooner or later anyway though.
708 if (--paren_depth
== 0)
710 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
711 long int numresult
= 0;
712 long long int convertme
;
714 if (bracket
|| words
[1 + *offset
] != ')')
720 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
725 convertme
= -numresult
;
726 *word
= w_addchar (*word
, word_length
, max_length
, '-');
734 convertme
= numresult
;
737 *word
= w_addstr (*word
, word_length
, max_length
,
738 _itoa (convertme
, &result
[20], 10, 0));
740 return *word
? 0 : WRDE_NOSPACE
;
742 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
749 if (bracket
&& paren_depth
== 1)
751 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
752 long int numresult
= 0;
755 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
759 *word
= w_addstr (*word
, word_length
, max_length
,
760 _itoa_word (numresult
, &result
[20], 10, 0));
762 return *word
? 0 : WRDE_NOSPACE
;
778 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
789 /* Function to execute a command and retrieve the results */
790 /* pwordexp contains NULL if field-splitting is forbidden */
793 exec_comm (char *comm
, char **word
, size_t *word_length
, size_t *max_length
,
794 int flags
, wordexp_t
*pwordexp
, const char *ifs
,
795 const char *ifs_white
)
804 /* Don't fork() unless necessary */
812 if ((pid
= __fork ()) < 0)
821 const char *args
[4] = { _PATH_BSHELL
, "-c", comm
, NULL
};
823 /* Redirect output. */
824 __dup2 (fildes
[1], 1);
827 /* Redirect stderr to /dev/null if we have to. */
828 if ((flags
& WRDE_SHOWERR
) == 0)
832 fd
= __open (_PATH_DEVNULL
, O_WRONLY
);
833 if (fd
>= 0 && fd
!= 2)
841 __execve (_PATH_BSHELL
, (char *const *) args
, __environ
);
850 buffer
= __alloca (bufsize
);
853 { /* Quoted - no field splitting */
857 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
859 if (__waitpid (pid
, NULL
, WNOHANG
) == 0)
861 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
865 *word
= w_addmem (*word
, word_length
, max_length
, buffer
, buflen
);
868 __kill (pid
, SIGKILL
);
869 __waitpid (pid
, NULL
, 0);
876 /* Not quoted - split fields */
880 * 0 when searching for first character in a field not IFS white space
881 * 1 when copying the text of a field
882 * 2 when searching for possible non-whitespace IFS
887 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
889 if (__waitpid (pid
, NULL
, WNOHANG
) == 0)
891 if ((__read (fildes
[0], buffer
, bufsize
)) < 1)
895 for (i
= 0; i
< buflen
; ++i
)
897 if (strchr (ifs
, buffer
[i
]) != NULL
)
899 /* Current character is IFS */
900 if (strchr (ifs_white
, buffer
[i
]) == NULL
)
902 /* Current character is IFS but not whitespace */
908 * eg: text<space><comma><space>moretext
910 * So, strip whitespace IFS (like at the start)
917 /* fall through and delimit field.. */
921 /* Current character is IFS white space */
923 /* If not copying a field, ignore it */
927 /* End of field (search for non-IFS afterwards) */
931 /* First IFS white space, or IFS non-whitespace.
932 * Delimit the field. */
935 /* This field is null, so make it an empty string */
936 *word
= w_addchar (*word
, word_length
, max_length
, 0);
939 __kill (pid
, SIGKILL
);
940 __waitpid (pid
, NULL
, 0);
946 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
948 __kill (pid
, SIGKILL
);
949 __waitpid (pid
, NULL
, 0);
954 *word
= w_newword (word_length
, max_length
);
955 /* fall back round the loop.. */
959 /* Not IFS character */
961 *word
= w_addchar (*word
, word_length
, max_length
,
965 __kill (pid
, SIGKILL
);
966 __waitpid (pid
, NULL
, 0);
975 /* Bash chops off trailing newlines, which seems sensible. */
976 while (*word_length
> 0 && (*word
)[*word_length
- 1] == '\n')
977 (*word
)[--*word_length
] = '\0';
985 parse_comm (char **word
, size_t *word_length
, size_t *max_length
,
986 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
987 const char *ifs
, const char *ifs_white
)
989 /* We are poised just after "$(" */
992 int quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
995 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
997 for (; words
[*offset
]; ++(*offset
))
999 switch (words
[*offset
])
1004 else if (quoted
== 1)
1012 else if (quoted
== 2)
1018 if (!quoted
&& --paren_depth
== 0)
1020 /* Go -- give script to the shell */
1023 error
= exec_comm (comm
, word
, word_length
, max_length
,
1024 flags
, pwordexp
, ifs
, ifs_white
);
1031 /* This is just part of the script */
1039 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
1041 return WRDE_NOSPACE
;
1053 parse_param (char **word
, size_t *word_length
, size_t *max_length
,
1054 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1055 const char *ifs
, const char *ifs_white
, int quoted
)
1057 /* We are poised just after "$" */
1061 ACT_RP_SHORT_LEFT
= '#',
1062 ACT_RP_LONG_LEFT
= 'L',
1063 ACT_RP_SHORT_RIGHT
= '%',
1064 ACT_RP_LONG_RIGHT
= 'R',
1065 ACT_NULL_ERROR
= '?',
1066 ACT_NULL_SUBST
= '-',
1067 ACT_NONNULL_SUBST
= '+',
1068 ACT_NULL_ASSIGN
= '='
1074 size_t start
= *offset
;
1078 enum action action
= ACT_NONE
;
1083 int pattern_is_quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1087 int brace
= words
[*offset
] == '{';
1089 env
= w_newword (&env_length
, &env_maxlen
);
1090 pattern
= w_newword (&pat_length
, &pat_maxlen
);
1095 /* First collect the parameter name. */
1097 if (words
[*offset
] == '#')
1105 if (isalpha (words
[*offset
]) || words
[*offset
] == '_')
1107 /* Normal parameter name. */
1110 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1115 while (isalnum (words
[++*offset
]) || words
[*offset
] == '_');
1117 else if (isdigit (words
[*offset
]))
1119 /* Numeric parameter name. */
1123 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1130 while (isdigit(words
[++*offset
]));
1132 else if (strchr ("*@$", words
[*offset
]) != NULL
)
1134 /* Special parameter. */
1136 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1150 /* Check for special action to be applied to the value. */
1151 switch (words
[*offset
])
1158 action
= ACT_RP_SHORT_LEFT
;
1159 if (words
[1 + *offset
] == '#')
1162 action
= ACT_RP_LONG_LEFT
;
1167 action
= ACT_RP_SHORT_RIGHT
;
1168 if (words
[1 + *offset
] == '%')
1171 action
= ACT_RP_LONG_RIGHT
;
1176 if (strchr ("-=?+", words
[1 + *offset
]) == NULL
)
1180 action
= words
[++*offset
];
1187 action
= words
[*offset
];
1194 /* Now collect the pattern. */
1196 for (; words
[*offset
]; ++(*offset
))
1198 switch (words
[*offset
])
1201 if (!pattern_is_quoted
)
1206 if (!pattern_is_quoted
)
1215 if (!pattern_is_quoted
&& words
[++*offset
] == '\0')
1220 if (pattern_is_quoted
== 0)
1221 pattern_is_quoted
= 1;
1222 else if (pattern_is_quoted
== 1)
1223 pattern_is_quoted
= 0;
1228 if (pattern_is_quoted
== 0)
1229 pattern_is_quoted
= 2;
1230 else if (pattern_is_quoted
== 2)
1231 pattern_is_quoted
= 0;
1236 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
,
1238 if (pattern
== NULL
)
1243 /* End of input string -- remember to reparse the character that we
1248 if (words
[start
] == '{' && words
[*offset
] != '}')
1255 /* $# expands to the number of positional parameters */
1257 value
= _itoa_word (__libc_argc
- 1, &buffer
[20], 10, 0);
1262 /* Just $ on its own */
1263 *offset
= start
- 1;
1264 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1265 return *word
? 0 : WRDE_NOSPACE
;
1268 /* Is it a numeric parameter? */
1269 else if (isdigit (env
[0]))
1273 if (n
>= __libc_argc
)
1274 /* Substitute NULL. */
1277 /* Replace with appropriate positional parameter. */
1278 value
= __libc_argv
[n
];
1280 /* Is it a special parameter? */
1287 value
= _itoa_word (__getpid (), &buffer
[20], 10, 0);
1289 /* Is it `${#*}' or `${#@}'? */
1290 else if ((*env
== '*' || *env
== '@') && seen_hash
)
1293 value
= _itoa_word (__libc_argc
> 0 ? __libc_argc
- 1 : 0,
1294 &buffer
[20], 10, 0);
1295 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1297 return *word
? 0 : WRDE_NOSPACE
;
1299 /* Is it `$*' or `$@' (unquoted) ? */
1300 else if (*env
== '*' || (*env
== '@' && !quoted
))
1302 size_t plist_len
= 0;
1306 /* Build up value parameter by parameter (copy them) */
1307 for (p
= 1; __libc_argv
[p
]; ++p
)
1308 plist_len
+= strlen (__libc_argv
[p
]) + 1; /* for space */
1309 value
= malloc (plist_len
);
1314 for (p
= 1; __libc_argv
[p
]; ++p
)
1318 end
= __stpcpy (end
, __libc_argv
[p
]);
1325 /* Must be a quoted `$@' */
1326 assert (*env
== '@' && quoted
);
1328 /* Each parameter is a separate word ("$@") */
1329 if (__libc_argc
== 2)
1330 value
= __libc_argv
[1];
1331 else if (__libc_argc
> 2)
1335 /* Append first parameter to current word. */
1336 value
= w_addstr (*word
, word_length
, max_length
,
1338 if (value
== NULL
|| w_addword (pwordexp
, value
))
1341 for (p
= 2; __libc_argv
[p
+ 1]; p
++)
1343 char *newword
= __strdup (__libc_argv
[p
]);
1344 if (newword
== NULL
|| w_addword (pwordexp
, newword
))
1348 /* Start a new word with the last parameter. */
1349 *word
= w_newword (word_length
, max_length
);
1350 value
= __libc_argv
[p
];
1361 value
= getenv (env
);
1363 if (value
== NULL
&& (flags
& WRDE_UNDEF
))
1365 /* Variable not defined. */
1373 if (action
!= ACT_NONE
)
1377 case ACT_RP_SHORT_LEFT
:
1378 case ACT_RP_LONG_LEFT
:
1379 case ACT_RP_SHORT_RIGHT
:
1380 case ACT_RP_LONG_RIGHT
:
1386 if (value
== NULL
|| pattern
== NULL
|| *pattern
== '\0')
1389 end
= value
+ strlen (value
);
1393 case ACT_RP_SHORT_LEFT
:
1394 for (p
= value
; p
<= end
; ++p
)
1398 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1403 char *newval
= __strdup (p
);
1421 case ACT_RP_LONG_LEFT
:
1422 for (p
= end
; p
>= value
; --p
)
1426 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1431 char *newval
= __strdup (p
);
1449 case ACT_RP_SHORT_RIGHT
:
1450 for (p
= end
; p
>= value
; --p
)
1452 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1455 newval
= malloc (p
- value
+ 1);
1458 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1469 case ACT_RP_LONG_RIGHT
:
1470 for (p
= value
; p
<= end
; ++p
)
1472 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1475 newval
= malloc (p
- value
+ 1);
1478 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1496 case ACT_NULL_ERROR
:
1497 if (value
&& *value
)
1498 /* Substitute parameter */
1501 if (!colon_seen
&& value
)
1502 /* Substitute NULL */
1506 /* Expand 'pattern' and write it to stderr */
1509 error
= wordexp (pattern
, &we
, flags
);
1515 fprintf (stderr
, "%s:", env
);
1517 for (i
= 0; i
< we
.we_wordc
; ++i
)
1519 fprintf (stderr
, " %s", we
.we_wordv
[i
]);
1522 fprintf (stderr
, "\n");
1523 error
= WRDE_BADVAL
;
1530 fprintf (stderr
, "%s: parameter null or not set\n", env
);
1531 error
= WRDE_BADVAL
;
1540 case ACT_NULL_SUBST
:
1541 if (value
&& *value
)
1542 /* Substitute parameter */
1545 if (!colon_seen
&& value
)
1547 /* Substitute NULL */
1557 /* Substitute word */
1566 /* No field-splitting is allowed, so imagine
1567 quotes around the word. */
1568 char *qtd_pattern
= malloc (3 + strlen (pattern
));
1570 sprintf (qtd_pattern
, "\"%s\"", pattern
);
1572 pattern
= qtd_pattern
;
1575 if (pattern
== NULL
&& (pattern
= __strdup ("")) == NULL
)
1578 error
= wordexp (pattern
, &we
, flags
);
1586 /* Fingers crossed that the quotes worked.. */
1587 assert (!quoted
|| we
.we_wordc
== 1);
1590 for (i
= 0; i
< we
.we_wordc
; ++i
)
1591 if (w_addword (pwordexp
, __strdup (we
.we_wordv
[i
]))
1595 if (i
< we
.we_wordc
)
1597 /* Ran out of space */
1602 if (action
== ACT_NULL_ASSIGN
)
1606 size_t words_size
= 0;
1609 /* Cannot assign special parameters. */
1612 for (i
= 0; i
< we
.we_wordc
; i
++)
1613 words_size
+= strlen (we
.we_wordv
[i
]) + 1; /* for <space> */
1616 cp
= words
= __alloca (words_size
);
1618 for (i
= 0; i
< we
.we_wordc
- 1; i
++)
1620 cp
= __stpcpy (cp
, we
.we_wordv
[i
]);
1624 strcpy (cp
, we
.we_wordv
[i
]);
1627 setenv (env
, words
, 1);
1636 case ACT_NONNULL_SUBST
:
1637 if (value
&& *value
)
1640 if (!colon_seen
&& value
)
1643 /* Substitute NULL */
1650 case ACT_NULL_ASSIGN
:
1651 if (value
&& *value
)
1652 /* Substitute parameter */
1655 if (!colon_seen
&& value
)
1657 /* Substitute NULL */
1665 /* This checks for '=' so it knows to assign */
1669 assert (! "Unrecognised action!");
1678 char param_length
[21];
1679 param_length
[20] = '\0';
1680 *word
= w_addstr (*word
, word_length
, max_length
,
1681 _itoa_word (value
? strlen (value
) : 0,
1682 ¶m_length
[20], 10, 0));
1685 assert (value
!= NULL
);
1689 return *word
? 0 : WRDE_NOSPACE
;
1695 if (quoted
|| !pwordexp
)
1697 /* Quoted - no field split */
1698 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1702 return *word
? 0 : WRDE_NOSPACE
;
1706 /* Need to field-split */
1707 char *value_copy
= __strdup (value
); /* Don't modify value */
1708 char *field_begin
= value_copy
;
1709 int seen_nonws_ifs
= 0;
1714 if (value_copy
== NULL
)
1715 return WRDE_NOSPACE
;
1719 char *field_end
= field_begin
;
1722 /* If this isn't the first field, start a new word */
1723 if (field_begin
!= value_copy
)
1725 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1728 return WRDE_NOSPACE
;
1731 *word
= w_newword (word_length
, max_length
);
1734 /* Skip IFS whitespace before the field */
1735 field_begin
+= strspn (field_begin
, ifs_white
);
1737 if (!seen_nonws_ifs
&& *field_begin
== 0)
1738 /* Nothing but whitespace */
1741 /* Search for the end of the field */
1742 field_end
= field_begin
+ strcspn (field_begin
, ifs
);
1744 /* Set up pointer to the character after end of field and
1745 skip whitespace IFS after it. */
1746 next_field
= field_end
+ strspn (field_end
, ifs_white
);
1748 /* Skip at most one non-whitespace IFS character after the field */
1750 if (*next_field
&& strchr (ifs
, *next_field
))
1756 /* Null-terminate it */
1759 /* Tag a copy onto the current word */
1760 *word
= w_addstr (*word
, word_length
, max_length
, field_begin
);
1765 return WRDE_NOSPACE
;
1768 field_begin
= next_field
;
1770 while (seen_nonws_ifs
|| *field_begin
);
1784 return WRDE_NOSPACE
;
1798 parse_dollars (char **word
, size_t *word_length
, size_t *max_length
,
1799 const char *words
, size_t *offset
, int flags
,
1800 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
,
1803 /* We are poised _at_ "$" */
1804 switch (words
[1 + *offset
])
1809 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1810 return *word
? 0 : WRDE_NOSPACE
;
1813 if (words
[2 + *offset
] == '(')
1815 /* Differentiate between $((1+3)) and $((echo);(ls)) */
1816 int i
= 3 + *offset
;
1818 while (words
[i
] && !(depth
== 0 && words
[i
] == ')'))
1820 if (words
[i
] == '(')
1822 else if (words
[i
] == ')')
1828 if (words
[i
] == ')' && words
[i
+ 1] == ')')
1831 /* Call parse_arith -- 0 is for "no brackets" */
1832 return parse_arith (word
, word_length
, max_length
, words
, offset
,
1837 if (flags
& WRDE_NOCMD
)
1841 return parse_comm (word
, word_length
, max_length
, words
, offset
, flags
,
1842 quoted
? NULL
: pwordexp
, ifs
, ifs_white
);
1846 /* Call parse_arith -- 1 is for "brackets" */
1847 return parse_arith (word
, word_length
, max_length
, words
, offset
, flags
,
1852 ++(*offset
); /* parse_param needs to know if "{" is there */
1853 return parse_param (word
, word_length
, max_length
, words
, offset
, flags
,
1854 pwordexp
, ifs
, ifs_white
, quoted
);
1859 parse_backtick (char **word
, size_t *word_length
, size_t *max_length
,
1860 const char *words
, size_t *offset
, int flags
,
1861 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
1863 /* We are poised just after "`" */
1868 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
1870 for (; words
[*offset
]; ++(*offset
))
1872 switch (words
[*offset
])
1875 /* Go -- give the script to the shell */
1876 error
= exec_comm (comm
, word
, word_length
, max_length
, flags
,
1877 pwordexp
, ifs
, ifs_white
);
1884 error
= parse_qtd_backslash (&comm
, &comm_length
, &comm_maxlen
,
1897 error
= parse_backslash (&comm
, &comm_length
, &comm_maxlen
, words
,
1909 squoting
= 1 - squoting
;
1911 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
1913 return WRDE_NOSPACE
;
1924 parse_dquote (char **word
, size_t *word_length
, size_t *max_length
,
1925 const char *words
, size_t *offset
, int flags
,
1926 wordexp_t
*pwordexp
, const char * ifs
, const char * ifs_white
)
1928 /* We are poised just after a double-quote */
1931 for (; words
[*offset
]; ++(*offset
))
1933 switch (words
[*offset
])
1939 error
= parse_dollars (word
, word_length
, max_length
, words
, offset
,
1940 flags
, pwordexp
, ifs
, ifs_white
, 1);
1941 /* The ``1'' here is to tell parse_dollars not to
1942 * split the fields. It may need to, however ("$@").
1950 if (flags
& WRDE_NOCMD
)
1954 error
= parse_backtick (word
, word_length
, max_length
, words
,
1955 offset
, flags
, NULL
, NULL
, NULL
);
1956 /* The first NULL here is to tell parse_backtick not to
1965 error
= parse_qtd_backslash (word
, word_length
, max_length
, words
,
1974 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
1976 return WRDE_NOSPACE
;
1980 /* Unterminated string */
1985 * wordfree() is to be called after pwordexp is finished with.
1989 wordfree (wordexp_t
*pwordexp
)
1992 /* wordexp can set pwordexp to NULL */
1993 if (pwordexp
&& pwordexp
->we_wordv
)
1995 char **wordv
= pwordexp
->we_wordv
;
1997 for (wordv
+= pwordexp
->we_offs
; *wordv
; ++wordv
)
2000 free (pwordexp
->we_wordv
);
2001 pwordexp
->we_wordv
= NULL
;
2010 wordexp (const char *words
, wordexp_t
*pwordexp
, int flags
)
2012 size_t wordv_offset
;
2013 size_t words_offset
;
2016 char *word
= w_newword (&word_length
, &max_length
);
2020 char **old_wordv
= pwordexp
->we_wordv
;
2021 size_t old_wordc
= (flags
& WRDE_REUSE
) ? pwordexp
->we_wordc
: 0;
2023 if (flags
& WRDE_REUSE
)
2025 /* Minimal implementation of WRDE_REUSE for now */
2026 wordfree (pwordexp
);
2030 if (flags
& WRDE_DOOFFS
)
2032 pwordexp
->we_wordv
= calloc (1 + pwordexp
->we_offs
, sizeof (char *));
2033 if (pwordexp
->we_wordv
== NULL
)
2034 return WRDE_NOSPACE
;
2038 pwordexp
->we_wordv
= calloc (1, sizeof (char *));
2039 if (pwordexp
->we_wordv
== NULL
)
2040 return WRDE_NOSPACE
;
2042 pwordexp
->we_offs
= 0;
2045 if ((flags
& WRDE_APPEND
) == 0)
2046 pwordexp
->we_wordc
= 0;
2048 wordv_offset
= pwordexp
->we_offs
+ pwordexp
->we_wordc
;
2050 /* Find out what the field separators are.
2051 * There are two types: whitespace and non-whitespace.
2053 ifs
= getenv ("IFS");
2056 /* IFS unset - use <space><tab><newline>. */
2057 ifs
= strcpy (ifs_white
, " \t\n");
2061 char *whch
= ifs_white
;
2063 /* Start off with no whitespace IFS characters */
2064 ifs_white
[0] = '\0';
2066 while (*ifsch
!= '\0')
2068 if ((*ifsch
== ' ') || (*ifsch
== '\t') || (*ifsch
== '\n'))
2070 /* Whitespace IFS. See first whether it is already in our
2072 char *runp
= ifs_white
;
2074 while (runp
< whch
&& *runp
!= '\0' && *runp
!= *ifsch
)
2086 for (words_offset
= 0 ; words
[words_offset
] ; ++words_offset
)
2087 switch (words
[words_offset
])
2090 error
= parse_backslash (&word
, &word_length
, &max_length
, words
,
2099 error
= parse_dollars (&word
, &word_length
, &max_length
, words
,
2100 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
,
2109 if (flags
& WRDE_NOCMD
)
2113 error
= parse_backtick (&word
, &word_length
, &max_length
, words
,
2114 &words_offset
, flags
, pwordexp
, ifs
,
2124 error
= parse_dquote (&word
, &word_length
, &max_length
, words
,
2125 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2134 error
= parse_squote (&word
, &word_length
, &max_length
, words
,
2143 error
= parse_tilde (&word
, &word_length
, &max_length
, words
,
2144 &words_offset
, pwordexp
->we_wordc
);
2154 error
= parse_glob (&word
, &word_length
, &max_length
, words
,
2155 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2163 /* Is it a field separator? */
2164 if (strchr (ifs
, words
[words_offset
]) == NULL
)
2166 /* Not a field separator -- but is it a valid word char? */
2167 if (strchr ("\n|&;<>(){}", words
[words_offset
]))
2170 wordfree (pwordexp
);
2171 pwordexp
->we_wordc
= 0;
2172 pwordexp
->we_wordv
= old_wordv
;
2173 return WRDE_BADCHAR
;
2176 /* "Ordinary" character -- add it to word */
2178 word
= w_addchar (word
, &word_length
, &max_length
,
2179 words
[words_offset
]);
2182 error
= WRDE_NOSPACE
;
2189 /* Field separator */
2190 if (strchr (ifs_white
, words
[words_offset
]))
2192 /* It's a whitespace IFS char. Ignore it at the beginning
2193 of a line and ignore multiple instances. */
2194 if (!word
|| !*word
)
2197 if (w_addword (pwordexp
, word
) == WRDE_NOSPACE
)
2199 error
= WRDE_NOSPACE
;
2203 word
= w_newword (&word_length
, &max_length
);
2207 /* It's a non-whitespace IFS char */
2209 /* Multiple non-whitespace IFS chars are treated as one. */
2212 if (w_addword (pwordexp
, word
) == WRDE_NOSPACE
)
2214 error
= WRDE_NOSPACE
;
2219 word
= w_newword (&word_length
, &max_length
);
2224 /* There was a field separator at the end */
2228 /* There was no field separator at the end */
2229 return w_addword (pwordexp
, word
);
2233 * free memory used (unless error is WRDE_NOSPACE), and
2234 * set we_wordc and wd_wordv back to what they were.
2237 if (error
== WRDE_NOSPACE
)
2238 return WRDE_NOSPACE
;
2243 wordfree (pwordexp
);
2244 pwordexp
->we_wordv
= old_wordv
;
2245 pwordexp
->we_wordc
= old_wordc
;