1 /* POSIX.2 wordexp implementation.
2 Copyright (C) 1997,1998,1999,2000,2001,2002 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
33 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/types.h>
45 #include <stdio-common/_itoa.h>
47 /* Undefine the following line for the production version. */
48 /* #define NDEBUG 1 */
51 /* Get some device information. */
52 #include <device-nrs.h>
55 * This is a recursive-descent-style word expansion routine.
58 /* These variables are defined and initialized in the startup code. */
59 extern int __libc_argc attribute_hidden
;
60 extern char **__libc_argv attribute_hidden
;
62 /* Some forward declarations */
63 static int parse_dollars (char **word
, size_t *word_length
, size_t *max_length
,
64 const char *words
, size_t *offset
, int flags
,
65 wordexp_t
*pwordexp
, const char *ifs
,
66 const char *ifs_white
, int quoted
)
68 static int parse_backtick (char **word
, size_t *word_length
,
69 size_t *max_length
, const char *words
,
70 size_t *offset
, int flags
, wordexp_t
*pwordexp
,
71 const char *ifs
, const char *ifs_white
)
73 static int parse_dquote (char **word
, size_t *word_length
, size_t *max_length
,
74 const char *words
, size_t *offset
, int flags
,
75 wordexp_t
*pwordexp
, const char *ifs
,
76 const char *ifs_white
)
78 static int eval_expr (char *expr
, long int *result
) internal_function
;
80 /* The w_*() functions manipulate word lists. */
84 /* Result of w_newword will be ignored if it's the last word. */
86 w_newword (size_t *actlen
, size_t *maxlen
)
88 *actlen
= *maxlen
= 0;
93 w_addchar (char *buffer
, size_t *actlen
, size_t *maxlen
, char ch
)
94 /* (lengths exclude trailing zero) */
96 /* Add a character to the buffer, allocating room for it if needed.
99 if (*actlen
== *maxlen
)
101 char *old_buffer
= buffer
;
102 assert (buffer
== NULL
|| *maxlen
!= 0);
104 buffer
= realloc (buffer
, 1 + *maxlen
);
112 buffer
[*actlen
] = ch
;
113 buffer
[++(*actlen
)] = '\0';
121 w_addmem (char *buffer
, size_t *actlen
, size_t *maxlen
, const char *str
,
124 /* Add a string to the buffer, allocating room for it if needed.
126 if (*actlen
+ len
> *maxlen
)
128 char *old_buffer
= buffer
;
129 assert (buffer
== NULL
|| *maxlen
!= 0);
130 *maxlen
+= MAX (2 * len
, W_CHUNK
);
131 buffer
= realloc (old_buffer
, 1 + *maxlen
);
139 *((char *) __mempcpy (&buffer
[*actlen
], str
, len
)) = '\0';
148 w_addstr (char *buffer
, size_t *actlen
, size_t *maxlen
, const char *str
)
149 /* (lengths exclude trailing zero) */
151 /* Add a string to the buffer, allocating room for it if needed.
155 assert (str
!= NULL
); /* w_addstr only called from this file */
158 return w_addmem (buffer
, actlen
, maxlen
, str
, len
);
163 w_addword (wordexp_t
*pwordexp
, char *word
)
165 /* Add a word to the wordlist */
169 /* Internally, NULL acts like "". Convert NULLs to "" before
170 * the caller sees them.
174 word
= __strdup ("");
179 num_p
= 2 + pwordexp
->we_wordc
+ pwordexp
->we_offs
;
180 new_wordv
= realloc (pwordexp
->we_wordv
, sizeof (char *) * num_p
);
181 if (new_wordv
!= NULL
)
183 pwordexp
->we_wordv
= new_wordv
;
184 pwordexp
->we_wordv
[pwordexp
->we_offs
+ pwordexp
->we_wordc
++] = word
;
185 pwordexp
->we_wordv
[pwordexp
->we_offs
+ pwordexp
->we_wordc
] = NULL
;
193 /* The parse_*() functions should leave *offset being the offset in 'words'
194 * to the last character processed.
199 parse_backslash (char **word
, size_t *word_length
, size_t *max_length
,
200 const char *words
, size_t *offset
)
202 /* We are poised _at_ a backslash, not in quotes */
204 switch (words
[1 + *offset
])
207 /* Backslash is last character of input words */
215 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
228 parse_qtd_backslash (char **word
, size_t *word_length
, size_t *max_length
,
229 const char *words
, size_t *offset
)
231 /* We are poised _at_ a backslash, inside quotes */
233 switch (words
[1 + *offset
])
236 /* Backslash is last character of input words */
247 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
255 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
257 *word
= w_addchar (*word
, word_length
, max_length
, words
[1 + *offset
]);
271 parse_tilde (char **word
, size_t *word_length
, size_t *max_length
,
272 const char *words
, size_t *offset
, size_t wordc
)
274 /* We are poised _at_ a tilde */
277 if (*word_length
!= 0)
279 if (!((*word
)[*word_length
- 1] == '=' && wordc
== 0))
281 if (!((*word
)[*word_length
- 1] == ':'
282 && strchr (*word
, '=') && wordc
== 0))
284 *word
= w_addchar (*word
, word_length
, max_length
, '~');
285 return *word
? 0 : WRDE_NOSPACE
;
290 for (i
= 1 + *offset
; words
[i
]; i
++)
292 if (words
[i
] == ':' || words
[i
] == '/' || words
[i
] == ' ' ||
293 words
[i
] == '\t' || words
[i
] == 0 )
296 if (words
[i
] == '\\')
298 *word
= w_addchar (*word
, word_length
, max_length
, '~');
299 return *word
? 0 : WRDE_NOSPACE
;
303 if (i
== 1 + *offset
)
305 /* Tilde appears on its own */
307 struct passwd pwd
, *tpwd
;
313 /* POSIX.2 says ~ expands to $HOME and if HOME is unset the
314 results are unspecified. We do a lookup on the uid if
317 home
= getenv ("HOME");
320 *word
= w_addstr (*word
, word_length
, max_length
, home
);
327 buffer
= __alloca (buflen
);
329 while ((result
= __getpwuid_r (uid
, &pwd
, buffer
, buflen
, &tpwd
)) != 0
333 buffer
= __alloca (buflen
);
336 if (result
== 0 && tpwd
!= NULL
&& pwd
.pw_dir
!= NULL
)
338 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
344 *word
= w_addchar (*word
, word_length
, max_length
, '~');
352 /* Look up user name in database to get home directory */
353 char *user
= strndupa (&words
[1 + *offset
], i
- (1 + *offset
));
354 struct passwd pwd
, *tpwd
;
356 char* buffer
= __alloca (buflen
);
359 while ((result
= __getpwnam_r (user
, &pwd
, buffer
, buflen
, &tpwd
)) != 0
363 buffer
= __alloca (buflen
);
366 if (result
== 0 && tpwd
!= NULL
&& pwd
.pw_dir
)
367 *word
= w_addstr (*word
, word_length
, max_length
, pwd
.pw_dir
);
370 /* (invalid login name) */
371 *word
= w_addchar (*word
, word_length
, max_length
, '~');
373 *word
= w_addstr (*word
, word_length
, max_length
, user
);
378 return *word
? 0 : WRDE_NOSPACE
;
384 do_parse_glob (const char *glob_word
, char **word
, size_t *word_length
,
385 size_t *max_length
, wordexp_t
*pwordexp
, const char *ifs
,
386 const char *ifs_white
)
392 error
= glob (glob_word
, GLOB_NOCHECK
, NULL
, &globbuf
);
396 /* We can only run into memory problems. */
397 assert (error
== GLOB_NOSPACE
);
403 /* No field splitting allowed. */
404 assert (globbuf
.gl_pathv
[0] != NULL
);
405 *word
= w_addstr (*word
, word_length
, max_length
, globbuf
.gl_pathv
[0]);
406 for (match
= 1; match
< globbuf
.gl_pathc
&& *word
!= NULL
; ++match
)
408 *word
= w_addchar (*word
, word_length
, max_length
, ' ');
410 *word
= w_addstr (*word
, word_length
, max_length
,
411 globbuf
.gl_pathv
[match
]);
415 return *word
? 0 : WRDE_NOSPACE
;
418 assert (ifs
== NULL
|| *ifs
!= '\0');
422 *word
= w_newword (word_length
, max_length
);
425 for (match
= 0; match
< globbuf
.gl_pathc
; ++match
)
427 char *matching_word
= __strdup (globbuf
.gl_pathv
[match
]);
428 if (matching_word
== NULL
|| w_addword (pwordexp
, matching_word
))
441 parse_glob (char **word
, size_t *word_length
, size_t *max_length
,
442 const char *words
, size_t *offset
, int flags
,
443 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
445 /* We are poised just after a '*', a '[' or a '?'. */
446 int error
= WRDE_NOSPACE
;
447 int quoted
= 0; /* 1 if singly-quoted, 2 if doubly */
449 wordexp_t glob_list
; /* List of words to glob */
451 glob_list
.we_wordc
= 0;
452 glob_list
.we_wordv
= NULL
;
453 glob_list
.we_offs
= 0;
454 for (; words
[*offset
] != '\0'; ++*offset
)
456 if ((ifs
&& strchr (ifs
, words
[*offset
])) ||
457 (!ifs
&& strchr (" \t\n", words
[*offset
])))
461 /* Sort out quoting */
462 if (words
[*offset
] == '\'')
469 else if (quoted
== 1)
475 else if (words
[*offset
] == '"')
482 else if (quoted
== 2)
489 /* Sort out other special characters */
490 if (quoted
!= 1 && words
[*offset
] == '$')
492 error
= parse_dollars (word
, word_length
, max_length
, words
,
493 offset
, flags
, &glob_list
, ifs
, ifs_white
,
500 else if (words
[*offset
] == '\\')
503 error
= parse_qtd_backslash (word
, word_length
, max_length
,
506 error
= parse_backslash (word
, word_length
, max_length
,
515 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
520 /* Don't forget to re-parse the character we stopped at. */
524 error
= w_addword (&glob_list
, *word
);
525 *word
= w_newword (word_length
, max_length
);
526 for (i
= 0; error
== 0 && i
< glob_list
.we_wordc
; i
++)
527 error
= do_parse_glob (glob_list
.we_wordv
[i
], word
, word_length
,
528 max_length
, pwordexp
, ifs
, ifs_white
);
532 wordfree (&glob_list
);
538 parse_squote (char **word
, size_t *word_length
, size_t *max_length
,
539 const char *words
, size_t *offset
)
541 /* We are poised just after a single quote */
542 for (; words
[*offset
]; ++(*offset
))
544 if (words
[*offset
] != '\'')
546 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
553 /* Unterminated string */
557 /* Functions to evaluate an arithmetic expression */
560 eval_expr_val (char **expr
, long int *result
)
565 /* Skip white space */
566 for (digit
= *expr
; digit
&& *digit
&& isspace (*digit
); ++digit
);
572 /* Scan for closing paren */
573 for (++digit
; **expr
&& **expr
!= ')'; ++(*expr
));
581 if (eval_expr (digit
, result
))
586 case '+': /* Positive value */
590 case '-': /* Negative value */
596 if (!isdigit (*digit
))
601 for (; *digit
&& isdigit (*digit
); ++digit
)
602 *result
= (*result
* 10) + (*digit
- '0');
611 eval_expr_multdiv (char **expr
, long int *result
)
616 if (eval_expr_val (expr
, result
) != 0)
621 /* Skip white space */
622 for (; *expr
&& **expr
&& isspace (**expr
); ++(*expr
));
627 if (eval_expr_val (expr
, &arg
) != 0)
632 else if (**expr
== '/')
635 if (eval_expr_val (expr
, &arg
) != 0)
648 eval_expr (char *expr
, long int *result
)
653 if (eval_expr_multdiv (&expr
, result
) != 0)
658 /* Skip white space */
659 for (; expr
&& *expr
&& isspace (*expr
); ++expr
);
664 if (eval_expr_multdiv (&expr
, &arg
) != 0)
669 else if (*expr
== '-')
672 if (eval_expr_multdiv (&expr
, &arg
) != 0)
685 parse_arith (char **word
, size_t *word_length
, size_t *max_length
,
686 const char *words
, size_t *offset
, int flags
, int bracket
)
688 /* We are poised just after "$((" or "$[" */
695 expr
= w_newword (&expr_length
, &expr_maxlen
);
696 for (; words
[*offset
]; ++(*offset
))
698 switch (words
[*offset
])
701 error
= parse_dollars (&expr
, &expr_length
, &expr_maxlen
,
702 words
, offset
, flags
, NULL
, NULL
, NULL
, 1);
703 /* The ``1'' here is to tell parse_dollars not to
715 error
= parse_backtick (&expr
, &expr_length
, &expr_maxlen
,
716 words
, offset
, flags
, NULL
, NULL
, NULL
);
717 /* The first NULL here is to tell parse_backtick not to
728 error
= parse_qtd_backslash (&expr
, &expr_length
, &expr_maxlen
,
735 /* I think that a backslash within an
736 * arithmetic expansion is bound to
737 * cause an error sooner or later anyway though.
742 if (--paren_depth
== 0)
744 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
745 long int numresult
= 0;
746 long long int convertme
;
748 if (bracket
|| words
[1 + *offset
] != ')')
757 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
765 convertme
= -numresult
;
766 *word
= w_addchar (*word
, word_length
, max_length
, '-');
774 convertme
= numresult
;
777 *word
= w_addstr (*word
, word_length
, max_length
,
778 _itoa (convertme
, &result
[20], 10, 0));
780 return *word
? 0 : WRDE_NOSPACE
;
782 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
789 if (bracket
&& paren_depth
== 1)
791 char result
[21]; /* 21 = ceil(log10(2^64)) + 1 */
792 long int numresult
= 0;
795 if (*expr
&& eval_expr (expr
, &numresult
) != 0)
802 *word
= w_addstr (*word
, word_length
, max_length
,
803 _itoa_word (numresult
, &result
[20], 10, 0));
805 return *word
? 0 : WRDE_NOSPACE
;
821 expr
= w_addchar (expr
, &expr_length
, &expr_maxlen
, words
[*offset
]);
832 /* Function called by child process in exec_comm() */
835 exec_comm_child (char *comm
, int *fildes
, int showerr
, int noexec
)
837 const char *args
[4] = { _PATH_BSHELL
, "-c", comm
, NULL
};
839 /* Execute the command, or just check syntax? */
843 /* Redirect output. */
844 __dup2 (fildes
[1], 1);
847 /* Redirect stderr to /dev/null if we have to. */
853 fd
= __open (_PATH_DEVNULL
, O_WRONLY
);
854 if (fd
>= 0 && fd
!= 2)
859 /* Be paranoid. Check that we actually opened the /dev/null
861 if (__builtin_expect (__fxstat64 (_STAT_VER
, 2, &st
), 0) != 0
862 || __builtin_expect (S_ISCHR (st
.st_mode
), 1) == 0
863 #if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
864 || st
.st_rdev
!= makedev (DEV_NULL_MAJOR
, DEV_NULL_MINOR
)
867 /* It's not the /dev/null device. Stop right here. The
868 problem is: how do we stop? We use _exit() with an
869 hopefully unusual exit code. */
873 /* Make sure the subshell doesn't field-split on our behalf. */
877 __execve (_PATH_BSHELL
, (char *const *) args
, __environ
);
883 /* Function to execute a command and retrieve the results */
884 /* pwordexp contains NULL if field-splitting is forbidden */
887 exec_comm (char *comm
, char **word
, size_t *word_length
, size_t *max_length
,
888 int flags
, wordexp_t
*pwordexp
, const char *ifs
,
889 const char *ifs_white
)
896 size_t maxnewlines
= 0;
900 /* Don't fork() unless necessary */
908 if ((pid
= __fork ()) < 0)
917 exec_comm_child(comm
, fildes
, (flags
& WRDE_SHOWERR
), 0);
922 buffer
= __alloca (bufsize
);
925 /* Quoted - no field splitting */
929 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
931 if (__waitpid (pid
, &status
, WNOHANG
) == 0)
933 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
937 maxnewlines
+= buflen
;
939 *word
= w_addmem (*word
, word_length
, max_length
, buffer
, buflen
);
945 /* Not quoted - split fields */
949 * 0 when searching for first character in a field not IFS white space
950 * 1 when copying the text of a field
951 * 2 when searching for possible non-whitespace IFS
952 * 3 when searching for non-newline after copying field
957 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
959 if (__waitpid (pid
, &status
, WNOHANG
) == 0)
961 if ((buflen
= __read (fildes
[0], buffer
, bufsize
)) < 1)
965 for (i
= 0; i
< buflen
; ++i
)
967 if (strchr (ifs
, buffer
[i
]) != NULL
)
969 /* Current character is IFS */
970 if (strchr (ifs_white
, buffer
[i
]) == NULL
)
972 /* Current character is IFS but not whitespace */
978 * eg: text<space><comma><space>moretext
980 * So, strip whitespace IFS (like at the start)
987 /* fall through and delimit field.. */
991 if (buffer
[i
] == '\n')
993 /* Current character is (IFS) newline */
995 /* If copying a field, this is the end of it,
996 but maybe all that's left is trailing newlines.
997 So start searching for a non-newline. */
1005 /* Current character is IFS white space, but
1008 /* If not either copying a field or searching
1009 for non-newline after a field, ignore it */
1010 if (copying
!= 1 && copying
!= 3)
1013 /* End of field (search for non-ws IFS afterwards) */
1018 /* First IFS white space (non-newline), or IFS non-whitespace.
1019 * Delimit the field. Nulls are converted by w_addword. */
1020 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1023 *word
= w_newword (word_length
, max_length
);
1026 /* fall back round the loop.. */
1030 /* Not IFS character */
1034 /* Nothing but (IFS) newlines since the last field,
1035 so delimit it here before starting new word */
1036 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1039 *word
= w_newword (word_length
, max_length
);
1044 if (buffer
[i
] == '\n') /* happens if newline not in IFS */
1049 *word
= w_addchar (*word
, word_length
, max_length
,
1058 /* Chop off trailing newlines (required by POSIX.2) */
1059 /* Ensure we don't go back further than the beginning of the
1060 substitution (i.e. remove maxnewlines bytes at most) */
1061 while (maxnewlines
-- != 0 &&
1062 *word_length
> 0 && (*word
)[*word_length
- 1] == '\n')
1064 (*word
)[--*word_length
] = '\0';
1066 /* If the last word was entirely newlines, turn it into a new word
1067 * which can be ignored if there's nothing following it. */
1068 if (*word_length
== 0)
1071 *word
= w_newword (word_length
, max_length
);
1076 __close (fildes
[0]);
1078 /* Check for syntax error (re-execute but with "-n" flag) */
1079 if (buflen
< 1 && status
!= 0)
1081 if ((pid
= __fork ()) < 0)
1084 return WRDE_NOSPACE
;
1089 fildes
[0] = fildes
[1] = -1;
1090 exec_comm_child(comm
, fildes
, 0, 1);
1093 if (__waitpid (pid
, &status
, 0) == pid
&& status
!= 0)
1100 __kill (pid
, SIGKILL
);
1101 __waitpid (pid
, NULL
, 0);
1102 __close (fildes
[0]);
1103 return WRDE_NOSPACE
;
1108 parse_comm (char **word
, size_t *word_length
, size_t *max_length
,
1109 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1110 const char *ifs
, const char *ifs_white
)
1112 /* We are poised just after "$(" */
1113 int paren_depth
= 1;
1115 int quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1118 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
1120 for (; words
[*offset
]; ++(*offset
))
1122 switch (words
[*offset
])
1127 else if (quoted
== 1)
1135 else if (quoted
== 2)
1141 if (!quoted
&& --paren_depth
== 0)
1143 /* Go -- give script to the shell */
1146 error
= exec_comm (comm
, word
, word_length
, max_length
,
1147 flags
, pwordexp
, ifs
, ifs_white
);
1154 /* This is just part of the script */
1162 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
1164 return WRDE_NOSPACE
;
1176 parse_param (char **word
, size_t *word_length
, size_t *max_length
,
1177 const char *words
, size_t *offset
, int flags
, wordexp_t
*pwordexp
,
1178 const char *ifs
, const char *ifs_white
, int quoted
)
1180 /* We are poised just after "$" */
1184 ACT_RP_SHORT_LEFT
= '#',
1185 ACT_RP_LONG_LEFT
= 'L',
1186 ACT_RP_SHORT_RIGHT
= '%',
1187 ACT_RP_LONG_RIGHT
= 'R',
1188 ACT_NULL_ERROR
= '?',
1189 ACT_NULL_SUBST
= '-',
1190 ACT_NONNULL_SUBST
= '+',
1191 ACT_NULL_ASSIGN
= '='
1197 size_t start
= *offset
;
1201 enum action action
= ACT_NONE
;
1206 int pattern_is_quoted
= 0; /* 1 for singly-quoted, 2 for doubly-quoted */
1210 int brace
= words
[*offset
] == '{';
1212 env
= w_newword (&env_length
, &env_maxlen
);
1213 pattern
= w_newword (&pat_length
, &pat_maxlen
);
1218 /* First collect the parameter name. */
1220 if (words
[*offset
] == '#')
1228 if (isalpha (words
[*offset
]) || words
[*offset
] == '_')
1230 /* Normal parameter name. */
1233 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1238 while (isalnum (words
[++*offset
]) || words
[*offset
] == '_');
1240 else if (isdigit (words
[*offset
]))
1242 /* Numeric parameter name. */
1246 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1253 while (isdigit(words
[++*offset
]));
1255 else if (strchr ("*@$", words
[*offset
]) != NULL
)
1257 /* Special parameter. */
1259 env
= w_addchar (env
, &env_length
, &env_maxlen
,
1273 /* Check for special action to be applied to the value. */
1274 switch (words
[*offset
])
1281 action
= ACT_RP_SHORT_LEFT
;
1282 if (words
[1 + *offset
] == '#')
1285 action
= ACT_RP_LONG_LEFT
;
1290 action
= ACT_RP_SHORT_RIGHT
;
1291 if (words
[1 + *offset
] == '%')
1294 action
= ACT_RP_LONG_RIGHT
;
1299 if (strchr ("-=?+", words
[1 + *offset
]) == NULL
)
1303 action
= words
[++*offset
];
1310 action
= words
[*offset
];
1317 /* Now collect the pattern, but don't expand it yet. */
1319 for (; words
[*offset
]; ++(*offset
))
1321 switch (words
[*offset
])
1324 if (!pattern_is_quoted
)
1329 if (!pattern_is_quoted
)
1338 if (pattern_is_quoted
)
1339 /* Quoted; treat as normal character. */
1342 /* Otherwise, it's an escape: next character is literal. */
1343 if (words
[++*offset
] == '\0')
1346 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
, '\\');
1347 if (pattern
== NULL
)
1353 if (pattern_is_quoted
== 0)
1354 pattern_is_quoted
= 1;
1355 else if (pattern_is_quoted
== 1)
1356 pattern_is_quoted
= 0;
1361 if (pattern_is_quoted
== 0)
1362 pattern_is_quoted
= 2;
1363 else if (pattern_is_quoted
== 2)
1364 pattern_is_quoted
= 0;
1369 pattern
= w_addchar (pattern
, &pat_length
, &pat_maxlen
,
1371 if (pattern
== NULL
)
1376 /* End of input string -- remember to reparse the character that we
1381 if (words
[start
] == '{' && words
[*offset
] != '}')
1388 /* $# expands to the number of positional parameters */
1390 value
= _itoa_word (__libc_argc
- 1, &buffer
[20], 10, 0);
1395 /* Just $ on its own */
1396 *offset
= start
- 1;
1397 *word
= w_addchar (*word
, word_length
, max_length
, '$');
1398 return *word
? 0 : WRDE_NOSPACE
;
1401 /* Is it a numeric parameter? */
1402 else if (isdigit (env
[0]))
1406 if (n
>= __libc_argc
)
1407 /* Substitute NULL. */
1410 /* Replace with appropriate positional parameter. */
1411 value
= __libc_argv
[n
];
1413 /* Is it a special parameter? */
1420 value
= _itoa_word (__getpid (), &buffer
[20], 10, 0);
1422 /* Is it `${#*}' or `${#@}'? */
1423 else if ((*env
== '*' || *env
== '@') && seen_hash
)
1426 value
= _itoa_word (__libc_argc
> 0 ? __libc_argc
- 1 : 0,
1427 &buffer
[20], 10, 0);
1428 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1432 return *word
? 0 : WRDE_NOSPACE
;
1434 /* Is it `$*' or `$@' (unquoted) ? */
1435 else if (*env
== '*' || (*env
== '@' && !quoted
))
1437 size_t plist_len
= 0;
1441 /* Build up value parameter by parameter (copy them) */
1442 for (p
= 1; __libc_argv
[p
]; ++p
)
1443 plist_len
+= strlen (__libc_argv
[p
]) + 1; /* for space */
1444 value
= malloc (plist_len
);
1449 for (p
= 1; __libc_argv
[p
]; ++p
)
1453 end
= __stpcpy (end
, __libc_argv
[p
]);
1460 /* Must be a quoted `$@' */
1461 assert (*env
== '@' && quoted
);
1463 /* Each parameter is a separate word ("$@") */
1464 if (__libc_argc
== 2)
1465 value
= __libc_argv
[1];
1466 else if (__libc_argc
> 2)
1470 /* Append first parameter to current word. */
1471 value
= w_addstr (*word
, word_length
, max_length
,
1473 if (value
== NULL
|| w_addword (pwordexp
, value
))
1476 for (p
= 2; __libc_argv
[p
+ 1]; p
++)
1478 char *newword
= __strdup (__libc_argv
[p
]);
1479 if (newword
== NULL
|| w_addword (pwordexp
, newword
))
1483 /* Start a new word with the last parameter. */
1484 *word
= w_newword (word_length
, max_length
);
1485 value
= __libc_argv
[p
];
1496 value
= getenv (env
);
1498 if (value
== NULL
&& (flags
& WRDE_UNDEF
))
1500 /* Variable not defined. */
1501 error
= WRDE_BADVAL
;
1505 if (action
!= ACT_NONE
)
1507 int expand_pattern
= 0;
1509 /* First, find out if we need to expand pattern (i.e. if we will
1513 case ACT_RP_SHORT_LEFT
:
1514 case ACT_RP_LONG_LEFT
:
1515 case ACT_RP_SHORT_RIGHT
:
1516 case ACT_RP_LONG_RIGHT
:
1517 /* Always expand for these. */
1521 case ACT_NULL_ERROR
:
1522 case ACT_NULL_SUBST
:
1523 case ACT_NULL_ASSIGN
:
1524 if (!value
|| (!*value
&& colon_seen
))
1525 /* If param is unset, or set but null and a colon has been seen,
1526 the expansion of the pattern will be needed. */
1531 case ACT_NONNULL_SUBST
:
1532 /* Expansion of word will be needed if parameter is set and not null,
1533 or set null but no colon has been seen. */
1534 if (value
&& (*value
|| !colon_seen
))
1540 assert (! "Unrecognised action!");
1545 /* We need to perform tilde expansion, parameter expansion,
1546 command substitution, and arithmetic expansion. We also
1547 have to be a bit careful with wildcard characters, as
1548 pattern might be given to fnmatch soon. To do this, we
1549 convert quotes to escapes. */
1555 int quoted
= 0; /* 1: single quotes; 2: double */
1557 expanded
= w_newword (&exp_len
, &exp_maxl
);
1558 for (p
= pattern
; p
&& *p
; p
++)
1567 else if (quoted
== 0)
1576 else if (quoted
== 0)
1586 /* Convert quoted wildchar to escaped wildchar. */
1587 expanded
= w_addchar (expanded
, &exp_len
,
1590 if (expanded
== NULL
)
1597 error
= parse_dollars (&expanded
, &exp_len
, &exp_maxl
, p
,
1598 &offset
, flags
, NULL
, NULL
, NULL
, 1);
1614 if (quoted
|| exp_len
)
1618 error
= parse_tilde (&expanded
, &exp_len
, &exp_maxl
, p
,
1635 expanded
= w_addchar (expanded
, &exp_len
, &exp_maxl
, '\\');
1637 assert (*p
); /* checked when extracted initially */
1638 if (expanded
== NULL
)
1642 expanded
= w_addchar (expanded
, &exp_len
, &exp_maxl
, *p
);
1644 if (expanded
== NULL
)
1656 case ACT_RP_SHORT_LEFT
:
1657 case ACT_RP_LONG_LEFT
:
1658 case ACT_RP_SHORT_RIGHT
:
1659 case ACT_RP_LONG_RIGHT
:
1665 if (value
== NULL
|| pattern
== NULL
|| *pattern
== '\0')
1668 end
= value
+ strlen (value
);
1672 case ACT_RP_SHORT_LEFT
:
1673 for (p
= value
; p
<= end
; ++p
)
1677 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1682 char *newval
= __strdup (p
);
1700 case ACT_RP_LONG_LEFT
:
1701 for (p
= end
; p
>= value
; --p
)
1705 if (fnmatch (pattern
, value
, 0) != FNM_NOMATCH
)
1710 char *newval
= __strdup (p
);
1728 case ACT_RP_SHORT_RIGHT
:
1729 for (p
= end
; p
>= value
; --p
)
1731 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1734 newval
= malloc (p
- value
+ 1);
1743 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1754 case ACT_RP_LONG_RIGHT
:
1755 for (p
= value
; p
<= end
; ++p
)
1757 if (fnmatch (pattern
, p
, 0) != FNM_NOMATCH
)
1760 newval
= malloc (p
- value
+ 1);
1769 *(char *) __mempcpy (newval
, value
, p
- value
) = '\0';
1787 case ACT_NULL_ERROR
:
1788 if (value
&& *value
)
1789 /* Substitute parameter */
1793 if (!colon_seen
&& value
)
1794 /* Substitute NULL */
1798 const char *str
= pattern
;
1801 str
= _("parameter null or not set");
1804 if (_IO_fwide (stderr
, 0) > 0)
1805 __fwprintf (stderr
, L
"%s: %s\n", env
, str
);
1808 fprintf (stderr
, "%s: %s\n", env
, str
);
1815 case ACT_NULL_SUBST
:
1816 if (value
&& *value
)
1817 /* Substitute parameter */
1820 if (free_value
&& value
)
1823 if (!colon_seen
&& value
)
1824 /* Substitute NULL */
1827 value
= pattern
? __strdup (pattern
) : pattern
;
1830 if (pattern
&& !value
)
1835 case ACT_NONNULL_SUBST
:
1836 if (value
&& (*value
|| !colon_seen
))
1838 if (free_value
&& value
)
1841 value
= pattern
? __strdup (pattern
) : pattern
;
1844 if (pattern
&& !value
)
1850 /* Substitute NULL */
1855 case ACT_NULL_ASSIGN
:
1856 if (value
&& *value
)
1857 /* Substitute parameter */
1860 if (!colon_seen
&& value
)
1862 /* Substitute NULL */
1868 if (free_value
&& value
)
1871 value
= pattern
? __strdup (pattern
) : pattern
;
1874 if (pattern
&& !value
)
1877 __setenv (env
, value
, 1);
1881 assert (! "Unrecognised action!");
1885 free (env
); env
= NULL
;
1886 free (pattern
); pattern
= NULL
;
1890 char param_length
[21];
1891 param_length
[20] = '\0';
1892 *word
= w_addstr (*word
, word_length
, max_length
,
1893 _itoa_word (value
? strlen (value
) : 0,
1894 ¶m_length
[20], 10, 0));
1897 assert (value
!= NULL
);
1901 return *word
? 0 : WRDE_NOSPACE
;
1907 if (quoted
|| !pwordexp
)
1909 /* Quoted - no field split */
1910 *word
= w_addstr (*word
, word_length
, max_length
, value
);
1914 return *word
? 0 : WRDE_NOSPACE
;
1918 /* Need to field-split */
1919 char *value_copy
= __strdup (value
); /* Don't modify value */
1920 char *field_begin
= value_copy
;
1921 int seen_nonws_ifs
= 0;
1926 if (value_copy
== NULL
)
1931 char *field_end
= field_begin
;
1934 /* If this isn't the first field, start a new word */
1935 if (field_begin
!= value_copy
)
1937 if (w_addword (pwordexp
, *word
) == WRDE_NOSPACE
)
1943 *word
= w_newword (word_length
, max_length
);
1946 /* Skip IFS whitespace before the field */
1947 field_begin
+= strspn (field_begin
, ifs_white
);
1949 if (!seen_nonws_ifs
&& *field_begin
== 0)
1950 /* Nothing but whitespace */
1953 /* Search for the end of the field */
1954 field_end
= field_begin
+ strcspn (field_begin
, ifs
);
1956 /* Set up pointer to the character after end of field and
1957 skip whitespace IFS after it. */
1958 next_field
= field_end
+ strspn (field_end
, ifs_white
);
1960 /* Skip at most one non-whitespace IFS character after the field */
1962 if (*next_field
&& strchr (ifs
, *next_field
))
1968 /* Null-terminate it */
1971 /* Tag a copy onto the current word */
1972 *word
= w_addstr (*word
, word_length
, max_length
, field_begin
);
1974 if (*word
== NULL
&& *field_begin
!= '\0')
1980 field_begin
= next_field
;
1982 while (seen_nonws_ifs
|| *field_begin
);
1994 error
= WRDE_NOSPACE
;
1998 error
= WRDE_SYNTAX
;
2012 parse_dollars (char **word
, size_t *word_length
, size_t *max_length
,
2013 const char *words
, size_t *offset
, int flags
,
2014 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
,
2017 /* We are poised _at_ "$" */
2018 switch (words
[1 + *offset
])
2023 *word
= w_addchar (*word
, word_length
, max_length
, '$');
2024 return *word
? 0 : WRDE_NOSPACE
;
2027 if (words
[2 + *offset
] == '(')
2029 /* Differentiate between $((1+3)) and $((echo);(ls)) */
2030 int i
= 3 + *offset
;
2032 while (words
[i
] && !(depth
== 0 && words
[i
] == ')'))
2034 if (words
[i
] == '(')
2036 else if (words
[i
] == ')')
2042 if (words
[i
] == ')' && words
[i
+ 1] == ')')
2045 /* Call parse_arith -- 0 is for "no brackets" */
2046 return parse_arith (word
, word_length
, max_length
, words
, offset
,
2051 if (flags
& WRDE_NOCMD
)
2055 return parse_comm (word
, word_length
, max_length
, words
, offset
, flags
,
2056 quoted
? NULL
: pwordexp
, ifs
, ifs_white
);
2060 /* Call parse_arith -- 1 is for "brackets" */
2061 return parse_arith (word
, word_length
, max_length
, words
, offset
, flags
,
2066 ++(*offset
); /* parse_param needs to know if "{" is there */
2067 return parse_param (word
, word_length
, max_length
, words
, offset
, flags
,
2068 pwordexp
, ifs
, ifs_white
, quoted
);
2074 parse_backtick (char **word
, size_t *word_length
, size_t *max_length
,
2075 const char *words
, size_t *offset
, int flags
,
2076 wordexp_t
*pwordexp
, const char *ifs
, const char *ifs_white
)
2078 /* We are poised just after "`" */
2083 char *comm
= w_newword (&comm_length
, &comm_maxlen
);
2085 for (; words
[*offset
]; ++(*offset
))
2087 switch (words
[*offset
])
2090 /* Go -- give the script to the shell */
2091 error
= exec_comm (comm
, word
, word_length
, max_length
, flags
,
2092 pwordexp
, ifs
, ifs_white
);
2099 error
= parse_qtd_backslash (&comm
, &comm_length
, &comm_maxlen
,
2112 error
= parse_backslash (&comm
, &comm_length
, &comm_maxlen
, words
,
2124 squoting
= 1 - squoting
;
2126 comm
= w_addchar (comm
, &comm_length
, &comm_maxlen
, words
[*offset
]);
2128 return WRDE_NOSPACE
;
2139 parse_dquote (char **word
, size_t *word_length
, size_t *max_length
,
2140 const char *words
, size_t *offset
, int flags
,
2141 wordexp_t
*pwordexp
, const char * ifs
, const char * ifs_white
)
2143 /* We are poised just after a double-quote */
2146 for (; words
[*offset
]; ++(*offset
))
2148 switch (words
[*offset
])
2154 error
= parse_dollars (word
, word_length
, max_length
, words
, offset
,
2155 flags
, pwordexp
, ifs
, ifs_white
, 1);
2156 /* The ``1'' here is to tell parse_dollars not to
2157 * split the fields. It may need to, however ("$@").
2165 if (flags
& WRDE_NOCMD
)
2169 error
= parse_backtick (word
, word_length
, max_length
, words
,
2170 offset
, flags
, NULL
, NULL
, NULL
);
2171 /* The first NULL here is to tell parse_backtick not to
2180 error
= parse_qtd_backslash (word
, word_length
, max_length
, words
,
2189 *word
= w_addchar (*word
, word_length
, max_length
, words
[*offset
]);
2191 return WRDE_NOSPACE
;
2195 /* Unterminated string */
2200 * wordfree() is to be called after pwordexp is finished with.
2204 wordfree (wordexp_t
*pwordexp
)
2207 /* wordexp can set pwordexp to NULL */
2208 if (pwordexp
&& pwordexp
->we_wordv
)
2210 char **wordv
= pwordexp
->we_wordv
;
2212 for (wordv
+= pwordexp
->we_offs
; *wordv
; ++wordv
)
2215 free (pwordexp
->we_wordv
);
2216 pwordexp
->we_wordv
= NULL
;
2219 libc_hidden_def (wordfree
)
2226 wordexp (const char *words
, wordexp_t
*pwordexp
, int flags
)
2228 size_t words_offset
;
2231 char *word
= w_newword (&word_length
, &max_length
);
2235 wordexp_t old_word
= *pwordexp
;
2237 if (flags
& WRDE_REUSE
)
2239 /* Minimal implementation of WRDE_REUSE for now */
2240 wordfree (pwordexp
);
2241 old_word
.we_wordv
= NULL
;
2244 if ((flags
& WRDE_APPEND
) == 0)
2246 pwordexp
->we_wordc
= 0;
2248 if (flags
& WRDE_DOOFFS
)
2250 pwordexp
->we_wordv
= calloc (1 + pwordexp
->we_offs
, sizeof (char *));
2251 if (pwordexp
->we_wordv
== NULL
)
2253 error
= WRDE_NOSPACE
;
2259 pwordexp
->we_wordv
= calloc (1, sizeof (char *));
2260 if (pwordexp
->we_wordv
== NULL
)
2262 error
= WRDE_NOSPACE
;
2266 pwordexp
->we_offs
= 0;
2270 /* Find out what the field separators are.
2271 * There are two types: whitespace and non-whitespace.
2273 ifs
= getenv ("IFS");
2276 /* IFS unset - use <space><tab><newline>. */
2277 ifs
= strcpy (ifs_white
, " \t\n");
2281 char *whch
= ifs_white
;
2283 /* Start off with no whitespace IFS characters */
2284 ifs_white
[0] = '\0';
2286 while (*ifsch
!= '\0')
2288 if ((*ifsch
== ' ') || (*ifsch
== '\t') || (*ifsch
== '\n'))
2290 /* Whitespace IFS. See first whether it is already in our
2292 char *runp
= ifs_white
;
2294 while (runp
< whch
&& *runp
!= '\0' && *runp
!= *ifsch
)
2306 for (words_offset
= 0 ; words
[words_offset
] ; ++words_offset
)
2307 switch (words
[words_offset
])
2310 error
= parse_backslash (&word
, &word_length
, &max_length
, words
,
2319 error
= parse_dollars (&word
, &word_length
, &max_length
, words
,
2320 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
,
2329 if (flags
& WRDE_NOCMD
)
2331 error
= WRDE_CMDSUB
;
2336 error
= parse_backtick (&word
, &word_length
, &max_length
, words
,
2337 &words_offset
, flags
, pwordexp
, ifs
,
2347 error
= parse_dquote (&word
, &word_length
, &max_length
, words
,
2348 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2355 error
= w_addword (pwordexp
, NULL
);
2365 error
= parse_squote (&word
, &word_length
, &max_length
, words
,
2373 error
= w_addword (pwordexp
, NULL
);
2382 error
= parse_tilde (&word
, &word_length
, &max_length
, words
,
2383 &words_offset
, pwordexp
->we_wordc
);
2393 error
= parse_glob (&word
, &word_length
, &max_length
, words
,
2394 &words_offset
, flags
, pwordexp
, ifs
, ifs_white
);
2402 /* Is it a word separator? */
2403 if (strchr (" \t", words
[words_offset
]) == NULL
)
2405 char ch
= words
[words_offset
];
2407 /* Not a word separator -- but is it a valid word char? */
2408 if (strchr ("\n|&;<>(){}", ch
))
2411 error
= WRDE_BADCHAR
;
2415 /* "Ordinary" character -- add it to word */
2416 word
= w_addchar (word
, &word_length
, &max_length
,
2420 error
= WRDE_NOSPACE
;
2427 /* If a word has been delimited, add it to the list. */
2430 error
= w_addword (pwordexp
, word
);
2435 word
= w_newword (&word_length
, &max_length
);
2440 /* There was a word separator at the end */
2441 if (word
== NULL
) /* i.e. w_newword */
2444 /* There was no field separator at the end */
2445 return w_addword (pwordexp
, word
);
2449 * free memory used (unless error is WRDE_NOSPACE), and
2450 * set pwordexp members back to what they were.
2456 if (error
== WRDE_NOSPACE
)
2457 return WRDE_NOSPACE
;
2459 if ((flags
& WRDE_APPEND
) == 0)
2460 wordfree (pwordexp
);
2462 *pwordexp
= old_word
;