#include "config.h"
#include "bashtypes.h"
#include <stdio.h>
#include "chartypes.h"
#include <pwd.h>
#include <signal.h>
#include <errno.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "bashansi.h"
#include "posixstat.h"
#include "bashintl.h"
#include "shell.h"
#include "flags.h"
#include "jobs.h"
#include "execute_cmd.h"
#include "filecntl.h"
#include "trap.h"
#include "pathexp.h"
#include "mailcheck.h"
#include "shmbutil.h"
#include "builtins/getopt.h"
#include "builtins/common.h"
#include <tilde/tilde.h>
#include <glob/strmatch.h>
#if !defined (errno)
extern int errno;
#endif
#define DEFAULT_INITIAL_ARRAY_SIZE 112
#define DEFAULT_ARRAY_SIZE 128
#define VT_VARIABLE 0
#define VT_POSPARMS 1
#define VT_ARRAYVAR 2
#define VT_ARRAYMEMBER 3
#define VT_STARSUB 128
#define ST_BACKSL 0x01
#define ST_CTLESC 0x02
#define ST_SQUOTE 0x04
#define ST_DQUOTE 0x08
#define EX_NOALLOC 0x01
#define EX_VARNAME 0x02
#define EX_REQMATCH 0x04
#define EX_COMMAND 0x08
#define PF_NOCOMSUB 0x01
#define LBRACE '{'
#define RBRACE '}'
#define LPAREN '('
#define RPAREN ')'
#define VALID_SPECIAL_LENGTH_PARAM(c) \
((c) == '-' || (c) == '?' || (c) == '#')
#define VALID_INDIR_PARAM(c) \
((c) == '#' || (c) == '?' || (c) == '@' || (c) == '*')
#define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
#define SPECIAL_VAR(name, wi) \
((DIGIT (*name) && all_digits (name)) || \
(name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
(wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))
typedef WORD_LIST *EXPFUNC __P((char *, int));
pid_t last_command_subst_pid = NO_PID;
pid_t current_command_subst_pid = NO_PID;
SHELL_VAR *ifs_var;
char *ifs_value;
unsigned char ifs_cmap[UCHAR_MAX + 1];
#if defined (HANDLE_MULTIBYTE)
unsigned char ifs_firstc[MB_LEN_MAX];
size_t ifs_firstc_len;
#else
unsigned char ifs_firstc;
#endif
extern int last_command_exit_value, last_command_exit_signal;
extern int subshell_environment;
extern int subshell_level, parse_and_execute_level;
extern int eof_encountered;
extern int return_catch_flag, return_catch_value;
extern pid_t dollar_dollar_pid;
extern int posixly_correct;
extern char *this_command_name;
extern struct fd_bitmap *current_fds_to_close;
extern int wordexp_only;
extern int expanding_redir;
extern int tempenv_assign_error;
#if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
extern wchar_t *wcsdup __P((const wchar_t *));
#endif
int allow_null_glob_expansion;
int fail_glob_expansion;
#if 0
char *glob_argv_flags;
static int glob_argv_flags_size;
#endif
static WORD_LIST expand_word_error, expand_word_fatal;
static WORD_DESC expand_wdesc_error, expand_wdesc_fatal;
static char expand_param_error, expand_param_fatal;
static char extract_string_error, extract_string_fatal;
static int no_longjmp_on_fatal_error = 0;
static int expand_no_split_dollar_star = 0;
WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
static WORD_LIST *garglist = (WORD_LIST *)NULL;
static char *quoted_substring __P((char *, int, int));
static int quoted_strlen __P((char *));
static char *quoted_strchr __P((char *, int, int));
static char *expand_string_if_necessary __P((char *, int, EXPFUNC *));
static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *));
static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
static WORD_LIST *expand_string_internal __P((char *, int));
static WORD_LIST *expand_string_leave_quoted __P((char *, int));
static WORD_LIST *expand_string_for_rhs __P((char *, int, int *, int *));
static WORD_LIST *list_quote_escapes __P((WORD_LIST *));
static char *dequote_escapes __P((char *));
static char *make_quoted_char __P((int));
static WORD_LIST *quote_list __P((WORD_LIST *));
static char *remove_quoted_escapes __P((char *));
static char *remove_quoted_nulls __P((char *));
static int unquoted_substring __P((char *, char *));
static int unquoted_member __P((int, char *));
#if defined (ARRAY_VARS)
static SHELL_VAR *do_compound_assignment __P((char *, char *, int));
#endif
static int do_assignment_internal __P((const WORD_DESC *, int));
static char *string_extract_verbatim __P((char *, size_t, int *, char *));
static char *string_extract __P((char *, int *, char *, int));
static char *string_extract_double_quoted __P((char *, int *, int));
static inline char *string_extract_single_quoted __P((char *, int *));
static inline int skip_single_quoted __P((const char *, size_t, int));
static int skip_double_quoted __P((char *, size_t, int));
static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int));
static char *extract_dollar_brace_string __P((char *, int *, int, int));
static char *pos_params __P((char *, int, int, int));
static unsigned char *mb_getcharlens __P((char *, int));
static char *remove_upattern __P((char *, char *, int));
#if defined (HANDLE_MULTIBYTE)
static wchar_t *remove_wpattern __P((wchar_t *, size_t, wchar_t *, int));
#endif
static char *remove_pattern __P((char *, char *, int));
static int match_pattern_char __P((char *, char *));
static int match_upattern __P((char *, char *, int, char **, char **));
#if defined (HANDLE_MULTIBYTE)
static int match_pattern_wchar __P((wchar_t *, wchar_t *));
static int match_wpattern __P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
#endif
static int match_pattern __P((char *, char *, int, char **, char **));
static int getpatspec __P((int, char *));
static char *getpattern __P((char *, int, int));
static char *variable_remove_pattern __P((char *, char *, int, int));
static char *list_remove_pattern __P((WORD_LIST *, char *, int, int, int));
static char *parameter_list_remove_pattern __P((int, char *, int, int));
#ifdef ARRAY_VARS
static char *array_remove_pattern __P((ARRAY *, char *, int, char *, int));
#endif
static char *parameter_brace_remove_pattern __P((char *, char *, char *, int, int));
static char *process_substitute __P((char *, int));
static char *read_comsub __P((int, int));
#ifdef ARRAY_VARS
static arrayind_t array_length_reference __P((char *));
#endif
static int valid_brace_expansion_word __P((char *, int));
static int chk_atstar __P((char *, int, int *, int *));
static int chk_arithsub __P((const char *, int));
static WORD_DESC *parameter_brace_expand_word __P((char *, int, int));
static WORD_DESC *parameter_brace_expand_indir __P((char *, int, int, int *, int *));
static WORD_DESC *parameter_brace_expand_rhs __P((char *, char *, int, int, int *, int *));
static void parameter_brace_expand_error __P((char *, char *));
static int valid_length_expression __P((char *));
static intmax_t parameter_brace_expand_length __P((char *));
static char *skiparith __P((char *, int));
static int verify_substring_values __P((char *, char *, int, intmax_t *, intmax_t *));
static int get_var_and_type __P((char *, char *, int, SHELL_VAR **, char **));
static char *mb_substring __P((char *, int, int));
static char *parameter_brace_substring __P((char *, char *, char *, int));
static char *pos_params_pat_subst __P((char *, char *, char *, int));
static char *parameter_brace_patsub __P((char *, char *, char *, int));
static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int *, int *));
static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int));
static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
static WORD_LIST *word_list_split __P((WORD_LIST *));
static void exp_jump_to_top_level __P((int));
static WORD_LIST *separate_out_assignments __P((WORD_LIST *));
static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int));
#ifdef BRACE_EXPANSION
static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int));
#endif
static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int));
static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int));
#ifdef INCLUDE_UNUSED
static char *
quoted_substring (string, start, end)
char *string;
int start, end;
{
register int len, l;
register char *result, *s, *r;
len = end - start;
for (s = string, l = 0; *s && l < start; )
{
if (*s == CTLESC)
{
s++;
continue;
}
l++;
if (*s == 0)
break;
}
r = result = (char *)xmalloc (2*len + 1);
s = string + l;
for (l = 0; l < len; s++)
{
if (*s == CTLESC)
*r++ = *s++;
*r++ = *s;
l++;
if (*s == 0)
break;
}
*r = '\0';
return result;
}
#endif
#ifdef INCLUDE_UNUSED
static int
quoted_strlen (s)
char *s;
{
register char *p;
int i;
i = 0;
for (p = s; *p; p++)
{
if (*p == CTLESC)
{
p++;
if (*p == 0)
return (i + 1);
}
i++;
}
return i;
}
#endif
static char *
quoted_strchr (s, c, flags)
char *s;
int c, flags;
{
register char *p;
for (p = s; *p; p++)
{
if (((flags & ST_BACKSL) && *p == '\\')
|| ((flags & ST_CTLESC) && *p == CTLESC))
{
p++;
if (*p == '\0')
return ((char *)NULL);
continue;
}
else if (*p == c)
return p;
}
return ((char *)NULL);
}
static int
unquoted_member (character, string)
int character;
char *string;
{
size_t slen;
int sindex, c;
DECLARE_MBSTATE;
slen = strlen (string);
sindex = 0;
while (c = string[sindex])
{
if (c == character)
return (1);
switch (c)
{
default:
ADVANCE_CHAR (string, slen, sindex);
break;
case '\\':
sindex++;
if (string[sindex])
ADVANCE_CHAR (string, slen, sindex);
break;
case '\'':
sindex = skip_single_quoted (string, slen, ++sindex);
break;
case '"':
sindex = skip_double_quoted (string, slen, ++sindex);
break;
}
}
return (0);
}
static int
unquoted_substring (substr, string)
char *substr, *string;
{
size_t slen;
int sindex, c, sublen;
DECLARE_MBSTATE;
if (substr == 0 || *substr == '\0')
return (0);
slen = strlen (string);
sublen = strlen (substr);
for (sindex = 0; c = string[sindex]; )
{
if (STREQN (string + sindex, substr, sublen))
return (1);
switch (c)
{
case '\\':
sindex++;
if (string[sindex])
ADVANCE_CHAR (string, slen, sindex);
break;
case '\'':
sindex = skip_single_quoted (string, slen, ++sindex);
break;
case '"':
sindex = skip_double_quoted (string, slen, ++sindex);
break;
default:
ADVANCE_CHAR (string, slen, sindex);
break;
}
}
return (0);
}
INLINE char *
sub_append_string (source, target, indx, size)
char *source, *target;
int *indx, *size;
{
if (source)
{
int srclen, n;
srclen = STRLEN (source);
if (srclen >= (int)(*size - *indx))
{
n = srclen + *indx;
n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
target = (char *)xrealloc (target, (*size = n));
}
FASTCOPY (source, target + *indx, srclen);
*indx += srclen;
target[*indx] = '\0';
free (source);
}
return (target);
}
#if 0
char *
sub_append_number (number, target, indx, size)
intmax_t number;
int *indx, *size;
char *target;
{
char *temp;
temp = itos (number);
return (sub_append_string (temp, target, indx, size));
}
#endif
static char *
string_extract (string, sindex, charlist, flags)
char *string;
int *sindex;
char *charlist;
int flags;
{
register int c, i;
int found;
size_t slen;
char *temp;
DECLARE_MBSTATE;
slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
i = *sindex;
found = 0;
while (c = string[i])
{
if (c == '\\')
{
if (string[i + 1])
i++;
else
break;
}
#if defined (ARRAY_VARS)
else if ((flags & EX_VARNAME) && c == '[')
{
int ni;
ni = skipsubscript (string, i);
if (string[ni] == ']')
i = ni;
}
#endif
else if (MEMBER (c, charlist))
{
found = 1;
break;
}
ADVANCE_CHAR (string, slen, i);
}
if ((flags & EX_REQMATCH) && found == 0)
{
*sindex = i;
return (&extract_string_error);
}
temp = (flags & EX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
*sindex = i;
return (temp);
}
static char *
string_extract_double_quoted (string, sindex, stripdq)
char *string;
int *sindex, stripdq;
{
size_t slen;
char *send;
int j, i, t;
unsigned char c;
char *temp, *ret;
int pass_next, backquote, si;
int dquote;
DECLARE_MBSTATE;
slen = strlen (string + *sindex) + *sindex;
send = string + slen;
pass_next = backquote = dquote = 0;
temp = (char *)xmalloc (1 + slen - *sindex);
j = 0;
i = *sindex;
while (c = string[i])
{
if (pass_next)
{
if ((stripdq == 0 && c != '"') ||
(stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
temp[j++] = '\\';
pass_next = 0;
add_one_character:
COPY_CHAR_I (temp, j, string, send, i);
continue;
}
if (c == '\\')
{
pass_next++;
i++;
continue;
}
if (backquote)
{
if (c == '`')
backquote = 0;
temp[j++] = c;
i++;
continue;
}
if (c == '`')
{
temp[j++] = c;
backquote++;
i++;
continue;
}
if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
{
int free_ret = 1;
si = i + 2;
if (string[i + 1] == LPAREN)
ret = extract_delimited_string (string, &si, "$(", "(", ")", EX_COMMAND);
else
ret = extract_dollar_brace_string (string, &si, 1, 0);
temp[j++] = '$';
temp[j++] = string[i + 1];
if (ret == 0 && no_longjmp_on_fatal_error)
{
free_ret = 0;
ret = string + i + 2;
}
for (t = 0; ret[t]; t++, j++)
temp[j] = ret[t];
temp[j] = string[si];
if (string[si])
{
j++;
i = si + 1;
}
else
i = si;
if (free_ret)
free (ret);
continue;
}
if (c != '"')
goto add_one_character;
if (stripdq)
{
dquote ^= 1;
i++;
continue;
}
break;
}
temp[j] = '\0';
if (c)
i++;
*sindex = i;
return (temp);
}
static int
skip_double_quoted (string, slen, sind)
char *string;
size_t slen;
int sind;
{
int c, i;
char *ret;
int pass_next, backquote, si;
DECLARE_MBSTATE;
pass_next = backquote = 0;
i = sind;
while (c = string[i])
{
if (pass_next)
{
pass_next = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '\\')
{
pass_next++;
i++;
continue;
}
else if (backquote)
{
if (c == '`')
backquote = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '`')
{
backquote++;
i++;
continue;
}
else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
{
si = i + 2;
if (string[i + 1] == LPAREN)
ret = extract_delimited_string (string, &si, "$(", "(", ")", EX_NOALLOC|EX_COMMAND);
else
ret = extract_dollar_brace_string (string, &si, 0, EX_NOALLOC);
i = si + 1;
continue;
}
else if (c != '"')
{
ADVANCE_CHAR (string, slen, i);
continue;
}
else
break;
}
if (c)
i++;
return (i);
}
static inline char *
string_extract_single_quoted (string, sindex)
char *string;
int *sindex;
{
register int i;
size_t slen;
char *t;
DECLARE_MBSTATE;
slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
i = *sindex;
while (string[i] && string[i] != '\'')
ADVANCE_CHAR (string, slen, i);
t = substring (string, *sindex, i);
if (string[i])
i++;
*sindex = i;
return (t);
}
static inline int
skip_single_quoted (string, slen, sind)
const char *string;
size_t slen;
int sind;
{
register int c;
DECLARE_MBSTATE;
c = sind;
while (string[c] && string[c] != '\'')
ADVANCE_CHAR (string, slen, c);
if (string[c])
c++;
return c;
}
static char *
string_extract_verbatim (string, slen, sindex, charlist)
char *string;
size_t slen;
int *sindex;
char *charlist;
{
register int i = *sindex;
#if defined (HANDLE_MULTIBYTE)
size_t clen;
wchar_t *wcharlist;
#endif
int c;
char *temp;
DECLARE_MBSTATE;
if (charlist[0] == '\'' && charlist[1] == '\0')
{
temp = string_extract_single_quoted (string, sindex);
--*sindex;
return temp;
}
i = *sindex;
#if 0
slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 1;
#endif
#if defined (HANDLE_MULTIBYTE)
clen = strlen (charlist);
wcharlist = 0;
#endif
while (c = string[i])
{
#if defined (HANDLE_MULTIBYTE)
size_t mblength;
#endif
if (c == CTLESC)
{
i += 2;
continue;
}
#if defined (HANDLE_MULTIBYTE)
mblength = MBLEN (string + i, slen - i);
if (mblength > 1)
{
wchar_t wc;
mblength = mbtowc (&wc, string + i, slen - i);
if (MB_INVALIDCH (mblength))
{
if (MEMBER (c, charlist))
break;
}
else
{
if (wcharlist == 0)
{
size_t len;
len = mbstowcs (wcharlist, charlist, 0);
if (len == -1)
len = 0;
wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1));
mbstowcs (wcharlist, charlist, len + 1);
}
if (wcschr (wcharlist, wc))
break;
}
}
else
#endif
if (MEMBER (c, charlist))
break;
ADVANCE_CHAR (string, slen, i);
}
#if defined (HANDLE_MULTIBYTE)
FREE (wcharlist);
#endif
temp = substring (string, *sindex, i);
*sindex = i;
return (temp);
}
char *
extract_command_subst (string, sindex)
char *string;
int *sindex;
{
return (extract_delimited_string (string, sindex, "$(", "(", ")", EX_COMMAND));
}
char *
extract_arithmetic_subst (string, sindex)
char *string;
int *sindex;
{
return (extract_delimited_string (string, sindex, "$[", "[", "]", 0));
}
#if defined (PROCESS_SUBSTITUTION)
char *
extract_process_subst (string, starter, sindex)
char *string;
char *starter;
int *sindex;
{
return (extract_delimited_string (string, sindex, starter, "(", ")", 0));
}
#endif
#if defined (ARRAY_VARS)
char *
extract_array_assignment_list (string, sindex)
char *string;
int *sindex;
{
int slen;
char *ret;
slen = strlen (string);
if (string[slen - 1] == ')')
{
ret = substring (string, *sindex, slen - 1);
*sindex = slen - 1;
return ret;
}
return 0;
}
#endif
static char *
extract_delimited_string (string, sindex, opener, alt_opener, closer, flags)
char *string;
int *sindex;
char *opener, *alt_opener, *closer;
int flags;
{
int i, c, si;
size_t slen;
char *t, *result;
int pass_character, nesting_level, in_comment;
int len_closer, len_opener, len_alt_opener;
DECLARE_MBSTATE;
slen = strlen (string + *sindex) + *sindex;
len_opener = STRLEN (opener);
len_alt_opener = STRLEN (alt_opener);
len_closer = STRLEN (closer);
pass_character = in_comment = 0;
nesting_level = 1;
i = *sindex;
while (nesting_level)
{
c = string[i];
if (c == 0)
break;
if (in_comment)
{
if (c == '\n')
in_comment = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
if (pass_character)
{
pass_character = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
if ((flags & EX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || whitespace (string[i - 1])))
{
in_comment = 1;
ADVANCE_CHAR (string, slen, i);
continue;
}
if (c == CTLESC || c == '\\')
{
pass_character++;
i++;
continue;
}
if (STREQN (string + i, opener, len_opener))
{
si = i + len_opener;
t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|EX_NOALLOC);
i = si + 1;
continue;
}
if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
{
si = i + len_alt_opener;
t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|EX_NOALLOC);
i = si + 1;
continue;
}
if (STREQN (string + i, closer, len_closer))
{
i += len_closer - 1;
nesting_level--;
if (nesting_level == 0)
break;
}
if (c == '`')
{
si = i + 1;
t = string_extract (string, &si, "`", flags|EX_NOALLOC);
i = si + 1;
continue;
}
if (c == '\'' || c == '"')
{
si = i + 1;
i = (c == '\'') ? skip_single_quoted (string, slen, si)
: skip_double_quoted (string, slen, si);
continue;
}
ADVANCE_CHAR (string, slen, i);
}
if (c == 0 && nesting_level)
{
if (no_longjmp_on_fatal_error == 0)
{
report_error (_("bad substitution: no closing `%s' in %s"), closer, string);
last_command_exit_value = EXECUTION_FAILURE;
exp_jump_to_top_level (DISCARD);
}
else
{
*sindex = i;
return (char *)NULL;
}
}
si = i - *sindex - len_closer + 1;
if (flags & EX_NOALLOC)
result = (char *)NULL;
else
{
result = (char *)xmalloc (1 + si);
strncpy (result, string + *sindex, si);
result[si] = '\0';
}
*sindex = i;
return (result);
}
static char *
extract_dollar_brace_string (string, sindex, quoted, flags)
char *string;
int *sindex, quoted, flags;
{
register int i, c;
size_t slen;
int pass_character, nesting_level, si;
char *result, *t;
DECLARE_MBSTATE;
pass_character = 0;
nesting_level = 1;
slen = strlen (string + *sindex) + *sindex;
i = *sindex;
while (c = string[i])
{
if (pass_character)
{
pass_character = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
if (c == CTLESC || c == '\\')
{
pass_character++;
i++;
continue;
}
if (string[i] == '$' && string[i+1] == LBRACE)
{
nesting_level++;
i += 2;
continue;
}
if (c == RBRACE)
{
nesting_level--;
if (nesting_level == 0)
break;
i++;
continue;
}
if (c == '`')
{
si = i + 1;
t = string_extract (string, &si, "`", flags|EX_NOALLOC);
i = si + 1;
continue;
}
if (string[i] == '$' && string[i+1] == LPAREN)
{
si = i + 2;
t = extract_delimited_string (string, &si, "$(", "(", ")", flags|EX_NOALLOC|EX_COMMAND);
i = si + 1;
continue;
}
if (c == '\'' || c == '"')
{
si = i + 1;
i = (c == '\'') ? skip_single_quoted (string, slen, si)
: skip_double_quoted (string, slen, si);
continue;
}
ADVANCE_CHAR (string, slen, i);
}
if (c == 0 && nesting_level)
{
if (no_longjmp_on_fatal_error == 0)
{
report_error (_("bad substitution: no closing `%s' in %s"), "}", string);
last_command_exit_value = EXECUTION_FAILURE;
exp_jump_to_top_level (DISCARD);
}
else
{
*sindex = i;
return ((char *)NULL);
}
}
result = (flags & EX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
*sindex = i;
return (result);
}
char *
de_backslash (string)
char *string;
{
register size_t slen;
register int i, j, prev_i;
DECLARE_MBSTATE;
slen = strlen (string);
i = j = 0;
while (i < slen)
{
if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
string[i + 1] == '$'))
i++;
prev_i = i;
ADVANCE_CHAR (string, slen, i);
if (j < prev_i)
do string[j++] = string[prev_i++]; while (prev_i < i);
else
j = i;
}
string[j] = '\0';
return (string);
}
#if 0
void
unquote_bang (string)
char *string;
{
register int i, j;
register char *temp;
temp = (char *)xmalloc (1 + strlen (string));
for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
{
if (string[i] == '\\' && string[i + 1] == '!')
{
temp[j] = '!';
i++;
}
}
strcpy (string, temp);
free (temp);
}
#endif
#if defined (READLINE)
#define CQ_RETURN(x) do { no_longjmp_on_fatal_error = 0; return (x); } while (0)
int
char_is_quoted (string, eindex)
char *string;
int eindex;
{
int i, pass_next, c;
size_t slen;
DECLARE_MBSTATE;
slen = strlen (string);
no_longjmp_on_fatal_error = 1;
i = pass_next = 0;
while (i <= eindex)
{
c = string[i];
if (pass_next)
{
pass_next = 0;
if (i >= eindex)
CQ_RETURN(1);
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '\\')
{
pass_next = 1;
i++;
continue;
}
else if (c == '\'' || c == '"')
{
i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
: skip_double_quoted (string, slen, ++i);
if (i > eindex)
CQ_RETURN(1);
}
else
ADVANCE_CHAR (string, slen, i);
}
CQ_RETURN(0);
}
int
unclosed_pair (string, eindex, openstr)
char *string;
int eindex;
char *openstr;
{
int i, pass_next, openc, olen;
size_t slen;
DECLARE_MBSTATE;
slen = strlen (string);
olen = strlen (openstr);
i = pass_next = openc = 0;
while (i <= eindex)
{
if (pass_next)
{
pass_next = 0;
if (i >= eindex)
return 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (string[i] == '\\')
{
pass_next = 1;
i++;
continue;
}
else if (STREQN (string + i, openstr, olen))
{
openc = 1 - openc;
i += olen;
}
else if (string[i] == '\'' || string[i] == '"')
{
i = (string[i] == '\'') ? skip_single_quoted (string, slen, i)
: skip_double_quoted (string, slen, i);
if (i > eindex)
return 0;
}
else
ADVANCE_CHAR (string, slen, i);
}
return (openc);
}
int
skip_to_delim (string, start, delims)
char *string;
int start;
char *delims;
{
int i, pass_next, backq, si, c;
size_t slen;
char *temp;
DECLARE_MBSTATE;
slen = strlen (string + start) + start;
no_longjmp_on_fatal_error = 1;
i = start;
pass_next = backq = 0;
while (c = string[i])
{
if (pass_next)
{
pass_next = 0;
if (c == 0)
CQ_RETURN(i);
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '\\')
{
pass_next = 1;
i++;
continue;
}
else if (backq)
{
if (c == '`')
backq = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '`')
{
backq = 1;
i++;
continue;
}
else if (c == '\'' || c == '"')
{
i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
: skip_double_quoted (string, slen, ++i);
}
else if (c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
{
si = i + 2;
if (string[si] == '\0')
CQ_RETURN(si);
if (string[i+1] == LPAREN)
temp = extract_delimited_string (string, &si, "$(", "(", ")", EX_NOALLOC|EX_COMMAND);
else
temp = extract_dollar_brace_string (string, &si, 0, EX_NOALLOC);
i = si;
if (string[i] == '\0')
break;
i++;
continue;
}
else if (member (c, delims))
break;
else
ADVANCE_CHAR (string, slen, i);
}
CQ_RETURN(i);
}
WORD_LIST *
split_at_delims (string, slen, delims, sentinel, nwp, cwp)
char *string;
int slen;
char *delims;
int sentinel;
int *nwp, *cwp;
{
int ts, te, i, nw, cw, ifs_split;
char *token, *d, *d2;
WORD_LIST *ret, *tl;
if (string == 0 || *string == '\0')
{
if (nwp)
*nwp = 0;
if (cwp)
*cwp = 0;
return ((WORD_LIST *)NULL);
}
d = (delims == 0) ? ifs_value : delims;
ifs_split = delims == 0;
d2 = 0;
if (delims)
{
size_t slength;
#if defined (HANDLE_MULTIBYTE)
size_t mblength = 1;
#endif
DECLARE_MBSTATE;
slength = strlen (delims);
d2 = (char *)xmalloc (slength + 1);
i = ts = 0;
while (delims[i])
{
#if defined (HANDLE_MULTIBYTE)
mbstate_t state_bak;
state_bak = state;
mblength = MBRLEN (delims + i, slength, &state);
if (MB_INVALIDCH (mblength))
state = state_bak;
else if (mblength > 1)
{
memcpy (d2 + ts, delims + i, mblength);
ts += mblength;
i += mblength;
slength -= mblength;
continue;
}
#endif
if (whitespace (delims[i]) == 0)
d2[ts++] = delims[i];
i++;
slength--;
}
d2[ts] = '\0';
}
ret = (WORD_LIST *)NULL;
for (i = 0; member (string[i], d) && spctabnl (string[i]); i++)
;
if (string[i] == '\0')
return (ret);
ts = i;
nw = 0;
cw = -1;
while (1)
{
te = skip_to_delim (string, ts, d);
if (ts == te && d2 && member (string[ts], d2))
{
te = ts + 1;
if (ifs_split)
while (member (string[te], d) && spctabnl (string[te]))
te++;
else
while (member (string[te], d2))
te++;
}
token = substring (string, ts, te);
ret = add_string_to_list (token, ret);
free (token);
nw++;
if (sentinel >= ts && sentinel <= te)
cw = nw;
if (cwp && cw == -1 && sentinel == ts-1)
cw = nw;
if (cwp && cw == -1 && sentinel < ts)
{
tl = make_word_list (make_word (""), ret->next);
ret->next = tl;
cw = nw;
nw++;
}
if (string[te] == 0)
break;
i = te;
while (member (string[i], d) && (ifs_split || spctabnl(string[i])))
i++;
if (string[i])
ts = i;
else
break;
}
if (cwp && cw == -1 && sentinel >= slen)
{
if (whitespace (string[sentinel - 1]))
{
token = "";
ret = add_string_to_list (token, ret);
nw++;
}
cw = nw;
}
if (nwp)
*nwp = nw;
if (cwp)
*cwp = cw;
return (REVERSE_LIST (ret, WORD_LIST *));
}
#endif
#if 0
char *
assignment_name (string)
char *string;
{
int offset;
char *temp;
offset = assignment (string, 0);
if (offset == 0)
return (char *)NULL;
temp = substring (string, 0, offset);
return (temp);
}
#endif
char *
string_list_internal (list, sep)
WORD_LIST *list;
char *sep;
{
register WORD_LIST *t;
char *result, *r;
int word_len, sep_len, result_size;
if (list == 0)
return ((char *)NULL);
if (list->next == 0)
return (savestring (list->word->word));
sep_len = STRLEN (sep);
result_size = 0;
for (t = list; t; t = t->next)
{
if (t != list)
result_size += sep_len;
result_size += strlen (t->word->word);
}
r = result = (char *)xmalloc (result_size + 1);
for (t = list; t; t = t->next)
{
if (t != list && sep_len)
{
if (sep_len > 1)
{
FASTCOPY (sep, r, sep_len);
r += sep_len;
}
else
*r++ = sep[0];
}
word_len = strlen (t->word->word);
FASTCOPY (t->word->word, r, word_len);
r += word_len;
}
*r = '\0';
return (result);
}
char *
string_list (list)
WORD_LIST *list;
{
return (string_list_internal (list, " "));
}
char *
string_list_dollar_star (list)
WORD_LIST *list;
{
char *ret;
#if defined (HANDLE_MULTIBYTE)
# if defined (__GNUC__)
char sep[MB_CUR_MAX + 1];
# else
char *sep = 0;
# endif
#else
char sep[2];
#endif
#if defined (HANDLE_MULTIBYTE)
# if !defined (__GNUC__)
sep = (char *)xmalloc (MB_CUR_MAX + 1);
# endif
if (ifs_firstc_len == 1)
{
sep[0] = ifs_firstc[0];
sep[1] = '\0';
}
else
{
memcpy (sep, ifs_firstc, ifs_firstc_len);
sep[ifs_firstc_len] = '\0';
}
#else
sep[0] = ifs_firstc;
sep[1] = '\0';
#endif
ret = string_list_internal (list, sep);
#if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
free (sep);
#endif
return ret;
}
char *
string_list_dollar_at (list, quoted)
WORD_LIST *list;
int quoted;
{
char *ifs, *ret;
#if defined (HANDLE_MULTIBYTE)
# if defined (__GNUC__)
char sep[MB_CUR_MAX + 1];
# else
char *sep = 0;
# endif
#else
char sep[2];
#endif
WORD_LIST *tlist;
ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
#if defined (HANDLE_MULTIBYTE)
# if !defined (__GNUC__)
sep = (char *)xmalloc (MB_CUR_MAX + 1);
# endif
if (ifs && *ifs)
{
if (ifs_firstc_len == 1)
{
sep[0] = ifs_firstc[0];
sep[1] = '\0';
}
else
{
memcpy (sep, ifs_firstc, ifs_firstc_len);
sep[ifs_firstc_len] = '\0';
}
}
else
{
sep[0] = ' ';
sep[1] = '\0';
}
#else
sep[0] = (ifs == 0 || *ifs == 0) ? ' ' : *ifs;
sep[1] = '\0';
#endif
#if 0
tlist = ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (ifs && *ifs == 0))
#else
tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
#endif
? quote_list (list)
: list_quote_escapes (list);
ret = string_list_internal (tlist, sep);
#if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
free (sep);
#endif
return ret;
}
#define issep(c) \
(((separators)[0]) ? ((separators)[1] ? isifs(c) \
: (c) == (separators)[0]) \
: 0)
WORD_LIST *
list_string (string, separators, quoted)
register char *string, *separators;
int quoted;
{
WORD_LIST *result;
WORD_DESC *t;
char *current_word, *s;
int sindex, sh_style_split, whitesep;
size_t slen;
if (!string || !*string)
return ((WORD_LIST *)NULL);
sh_style_split = separators && separators[0] == ' ' &&
separators[1] == '\t' &&
separators[2] == '\n' &&
separators[3] == '\0';
slen = 0;
if (!quoted || !separators || !*separators)
{
for (s = string; *s && spctabnl (*s) && issep (*s); s++);
if (!*s)
return ((WORD_LIST *)NULL);
string = s;
}
slen = (MB_CUR_MAX > 1) ? strlen (string) : 1;
for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; )
{
current_word = string_extract_verbatim (string, slen, &sindex, separators);
if (current_word == 0)
break;
if (QUOTED_NULL (current_word))
{
t = alloc_word_desc ();
t->word = make_quoted_char ('\0');
t->flags |= W_QUOTED|W_HASQUOTEDNULL;
result = make_word_list (t, result);
}
else if (current_word[0] != '\0')
{
remove_quoted_nulls (current_word);
result = add_string_to_list (current_word, result);
result->word->flags &= ~W_HASQUOTEDNULL;
if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
result->word->flags |= W_QUOTED;
}
else if (!sh_style_split && !spctabnl (string[sindex]))
{
t = alloc_word_desc ();
t->word = make_quoted_char ('\0');
t->flags |= W_QUOTED|W_HASQUOTEDNULL;
result = make_word_list (t, result);
}
free (current_word);
whitesep = string[sindex] && spctabnl (string[sindex]);
if (string[sindex])
{
DECLARE_MBSTATE;
ADVANCE_CHAR (string, slen, sindex);
}
while (string[sindex] && spctabnl (string[sindex]) && issep (string[sindex]))
sindex++;
if (string[sindex] && whitesep && issep (string[sindex]) && !spctabnl (string[sindex]))
{
sindex++;
while (string[sindex] && spctabnl (string[sindex]) && isifs (string[sindex]))
sindex++;
}
}
return (REVERSE_LIST (result, WORD_LIST *));
}
char *
get_word_from_string (stringp, separators, endptr)
char **stringp, *separators, **endptr;
{
register char *s;
char *current_word;
int sindex, sh_style_split, whitesep;
size_t slen;
if (!stringp || !*stringp || !**stringp)
return ((char *)NULL);
s = *stringp;
sh_style_split = separators && separators[0] == ' ' &&
separators[1] == '\t' &&
separators[2] == '\n' &&
separators[3] == '\0';
slen = 0;
if (sh_style_split || !separators || !*separators)
{
for (; *s && spctabnl (*s) && isifs (*s); s++);
if (!*s)
{
*stringp = s;
if (endptr)
*endptr = s;
return ((char *)NULL);
}
}
sindex = 0;
slen = (MB_CUR_MAX > 1) ? strlen (s) : 1;
current_word = string_extract_verbatim (s, slen, &sindex, separators);
if (endptr)
*endptr = s + sindex;
whitesep = s[sindex] && spctabnl (s[sindex]);
if (s[sindex])
{
DECLARE_MBSTATE;
ADVANCE_CHAR (s, slen, sindex);
}
while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
sindex++;
if (s[sindex] && whitesep && isifs (s[sindex]) && !spctabnl (s[sindex]))
{
sindex++;
while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
sindex++;
}
*stringp = s + sindex;
return (current_word);
}
char *
strip_trailing_ifs_whitespace (string, separators, saw_escape)
char *string, *separators;
int saw_escape;
{
char *s;
s = string + STRLEN (string) - 1;
while (s > string && ((spctabnl (*s) && isifs (*s)) ||
(saw_escape && *s == CTLESC && spctabnl (s[1]))))
s--;
*++s = '\0';
return string;
}
#if 0
WORD_LIST *
list_string_with_quotes (string)
char *string;
{
WORD_LIST *list;
char *token, *s;
size_t s_len;
int c, i, tokstart, len;
for (s = string; s && *s && spctabnl (*s); s++)
;
if (s == 0 || *s == 0)
return ((WORD_LIST *)NULL);
s_len = strlen (s);
tokstart = i = 0;
list = (WORD_LIST *)NULL;
while (1)
{
c = s[i];
if (c == '\\')
{
i++;
if (s[i])
i++;
}
else if (c == '\'')
i = skip_single_quoted (s, s_len, ++i);
else if (c == '"')
i = skip_double_quoted (s, s_len, ++i);
else if (c == 0 || spctabnl (c))
{
token = substring (s, tokstart, i);
list = add_string_to_list (token, list);
free (token);
while (spctabnl (s[i]))
i++;
if (s[i])
tokstart = i;
else
break;
}
else
i++;
}
return (REVERSE_LIST (list, WORD_LIST *));
}
#endif
#if defined (ARRAY_VARS)
static SHELL_VAR *
do_compound_assignment (name, value, flags)
char *name, *value;
int flags;
{
SHELL_VAR *v;
int mklocal;
WORD_LIST *list;
mklocal = flags & ASS_MKLOCAL;
if (mklocal && variable_context)
{
list = expand_compound_array_assignment (value, flags);
v = find_variable (name);
if (v == 0 || array_p (v) == 0 || v->context != variable_context)
v = make_local_array_variable (name);
assign_compound_array_list (v, list, flags);
}
else
v = assign_array_from_string (name, value, flags);
return (v);
}
#endif
static int
do_assignment_internal (word, expand)
const WORD_DESC *word;
int expand;
{
int offset, tlen, appendop, assign_list, aflags;
char *name, *value;
SHELL_VAR *entry;
#if defined (ARRAY_VARS)
char *t;
int ni;
#endif
const char *string;
if (word == 0 || word->word == 0)
return 0;
appendop = assign_list = aflags = 0;
string = word->word;
offset = assignment (string, 0);
name = savestring (string);
value = (char *)NULL;
if (name[offset] == '=')
{
char *temp;
if (name[offset - 1] == '+')
{
appendop = 1;
name[offset - 1] = '\0';
}
name[offset] = 0;
temp = name + offset + 1;
tlen = STRLEN (temp);
#if defined (ARRAY_VARS)
if (expand && (word->flags & W_COMPASSIGN))
{
assign_list = ni = 1;
value = extract_array_assignment_list (temp, &ni);
}
else
#endif
if (expand && temp[0])
value = expand_string_if_necessary (temp, 0, expand_string_assignment);
else
value = savestring (temp);
}
if (value == 0)
{
value = (char *)xmalloc (1);
value[0] = '\0';
}
if (echo_command_at_execute)
{
if (appendop)
name[offset - 1] = '+';
xtrace_print_assignment (name, value, assign_list, 1);
if (appendop)
name[offset - 1] = '\0';
}
#define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
if (appendop)
aflags |= ASS_APPEND;
#if defined (ARRAY_VARS)
if (t = xstrchr (name, '['))
{
if (assign_list)
{
report_error (_("%s: cannot assign list to array member"), name);
ASSIGN_RETURN (0);
}
entry = assign_array_element (name, value, aflags);
if (entry == 0)
ASSIGN_RETURN (0);
}
else if (assign_list)
{
if (word->flags & W_ASSIGNARG)
aflags |= ASS_MKLOCAL;
entry = do_compound_assignment (name, value, aflags);
}
else
#endif
entry = bind_variable (name, value, aflags);
stupidly_hack_special_variables (name);
if (entry)
VUNSETATTR (entry, att_invisible);
ASSIGN_RETURN (entry ? ((readonly_p (entry) == 0) && noassign_p (entry) == 0) : 0);
}
int
do_assignment (string)
char *string;
{
WORD_DESC td;
td.flags = W_ASSIGNMENT;
td.word = string;
return do_assignment_internal (&td, 1);
}
int
do_word_assignment (word)
WORD_DESC *word;
{
return do_assignment_internal (word, 1);
}
int
do_assignment_no_expand (string)
char *string;
{
WORD_DESC td;
td.flags = W_ASSIGNMENT;
td.word = string;
return (do_assignment_internal (&td, 0));
}
WORD_LIST *
list_rest_of_args ()
{
register WORD_LIST *list, *args;
int i;
for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++)
list = make_word_list (make_bare_word (dollar_vars[i]), list);
for (args = rest_of_args; args; args = args->next)
list = make_word_list (make_bare_word (args->word->word), list);
return (REVERSE_LIST (list, WORD_LIST *));
}
int
number_of_args ()
{
register WORD_LIST *list;
int n;
for (n = 0; n < 9 && dollar_vars[n+1]; n++)
;
for (list = rest_of_args; list; list = list->next)
n++;
return n;
}
char *
get_dollar_var_value (ind)
intmax_t ind;
{
char *temp;
WORD_LIST *p;
if (ind < 10)
temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL;
else
{
ind -= 10;
for (p = rest_of_args; p && ind--; p = p->next)
;
temp = p ? savestring (p->word->word) : (char *)NULL;
}
return (temp);
}
char *
string_rest_of_args (dollar_star)
int dollar_star;
{
register WORD_LIST *list;
char *string;
list = list_rest_of_args ();
string = dollar_star ? string_list_dollar_star (list) : string_list (list);
dispose_words (list);
return (string);
}
static char *
pos_params (string, start, end, quoted)
char *string;
int start, end, quoted;
{
WORD_LIST *save, *params, *h, *t;
char *ret;
int i;
if (start == end)
return ((char *)NULL);
save = params = list_rest_of_args ();
if (save == 0)
return ((char *)NULL);
for (i = 1; params && i < start; i++)
params = params->next;
if (params == 0)
return ((char *)NULL);
for (h = t = params; params && i < end; i++)
{
t = params;
params = params->next;
}
t->next = (WORD_LIST *)NULL;
if (string[0] == '*')
{
if (quoted & Q_DOUBLE_QUOTES)
ret = string_list_dollar_star (quote_list (h));
else if (quoted & Q_HERE_DOCUMENT)
ret = string_list (quote_list (h));
else
ret = string_list (h);
}
else
ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (h) : h);
if (t != params)
t->next = params;
dispose_words (save);
return (ret);
}
#if defined (PROCESS_SUBSTITUTION)
#define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
#else
#define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
#endif
static char *
expand_string_if_necessary (string, quoted, func)
char *string;
int quoted;
EXPFUNC *func;
{
WORD_LIST *list;
size_t slen;
int i, saw_quote;
char *ret;
DECLARE_MBSTATE;
slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
i = saw_quote = 0;
while (string[i])
{
if (EXP_CHAR (string[i]))
break;
else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
saw_quote = 1;
ADVANCE_CHAR (string, slen, i);
}
if (string[i])
{
list = (*func) (string, quoted);
if (list)
{
ret = string_list (list);
dispose_words (list);
}
else
ret = (char *)NULL;
}
else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
ret = string_quote_removal (string, quoted);
else
ret = savestring (string);
return ret;
}
static inline char *
expand_string_to_string_internal (string, quoted, func)
char *string;
int quoted;
EXPFUNC *func;
{
WORD_LIST *list;
char *ret;
if (string == 0 || *string == '\0')
return ((char *)NULL);
list = (*func) (string, quoted);
if (list)
{
ret = string_list (list);
dispose_words (list);
}
else
ret = (char *)NULL;
return (ret);
}
char *
expand_string_to_string (string, quoted)
char *string;
int quoted;
{
return (expand_string_to_string_internal (string, quoted, expand_string));
}
char *
expand_string_unsplit_to_string (string, quoted)
char *string;
int quoted;
{
return (expand_string_to_string_internal (string, quoted, expand_string_unsplit));
}
char *
expand_assignment_string_to_string (string, quoted)
char *string;
int quoted;
{
return (expand_string_to_string_internal (string, quoted, expand_string_assignment));
}
char *
expand_arith_string (string, quoted)
char *string;
int quoted;
{
return (expand_string_if_necessary (string, quoted, expand_string));
}
#if defined (COND_COMMAND)
char *
remove_backslashes (string)
char *string;
{
char *r, *ret, *s;
r = ret = (char *)xmalloc (strlen (string) + 1);
for (s = string; s && *s; )
{
if (*s == '\\')
s++;
if (*s == 0)
break;
*r++ = *s++;
}
*r = '\0';
return ret;
}
char *
cond_expand_word (w, special)
WORD_DESC *w;
int special;
{
char *r, *p;
WORD_LIST *l;
int qflags;
if (w->word == 0 || w->word[0] == '\0')
return ((char *)NULL);
l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0);
if (l)
{
if (special == 0)
{
dequote_list (l);
r = string_list (l);
}
else
{
qflags = QGLOB_CVTNULL;
if (special == 2)
qflags |= QGLOB_REGEXP;
p = string_list (l);
r = quote_string_for_globbing (p, qflags);
free (p);
}
dispose_words (l);
}
else
r = (char *)NULL;
return r;
}
#endif
static WORD_LIST *
call_expand_word_internal (w, q, i, c, e)
WORD_DESC *w;
int q, i, *c, *e;
{
WORD_LIST *result;
result = expand_word_internal (w, q, i, c, e);
if (result == &expand_word_error || result == &expand_word_fatal)
{
w->word = (char *)NULL;
last_command_exit_value = EXECUTION_FAILURE;
exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF);
}
else
return (result);
}
static WORD_LIST *
expand_string_internal (string, quoted)
char *string;
int quoted;
{
WORD_DESC td;
WORD_LIST *tresult;
if (string == 0 || *string == 0)
return ((WORD_LIST *)NULL);
td.flags = 0;
td.word = savestring (string);
tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
FREE (td.word);
return (tresult);
}
WORD_LIST *
expand_string_unsplit (string, quoted)
char *string;
int quoted;
{
WORD_LIST *value;
if (string == 0 || *string == '\0')
return ((WORD_LIST *)NULL);
expand_no_split_dollar_star = 1;
value = expand_string_internal (string, quoted);
expand_no_split_dollar_star = 0;
if (value)
{
if (value->word)
{
remove_quoted_nulls (value->word->word);
value->word->flags &= ~W_HASQUOTEDNULL;
}
dequote_list (value);
}
return (value);
}
WORD_LIST *
expand_string_assignment (string, quoted)
char *string;
int quoted;
{
WORD_DESC td;
WORD_LIST *value;
if (string == 0 || *string == '\0')
return ((WORD_LIST *)NULL);
expand_no_split_dollar_star = 1;
td.flags = W_ASSIGNRHS;
td.word = savestring (string);
value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
FREE (td.word);
expand_no_split_dollar_star = 0;
if (value)
{
if (value->word)
{
remove_quoted_nulls (value->word->word);
value->word->flags &= ~W_HASQUOTEDNULL;
}
dequote_list (value);
}
return (value);
}
WORD_LIST *
expand_prompt_string (string, quoted, wflags)
char *string;
int quoted;
int wflags;
{
WORD_LIST *value;
WORD_DESC td;
if (string == 0 || *string == 0)
return ((WORD_LIST *)NULL);
td.flags = wflags;
td.word = savestring (string);
no_longjmp_on_fatal_error = 1;
value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
no_longjmp_on_fatal_error = 0;
if (value == &expand_word_error || value == &expand_word_fatal)
{
value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL);
return value;
}
FREE (td.word);
if (value)
{
if (value->word)
{
remove_quoted_nulls (value->word->word);
value->word->flags &= ~W_HASQUOTEDNULL;
}
dequote_list (value);
}
return (value);
}
static WORD_LIST *
expand_string_leave_quoted (string, quoted)
char *string;
int quoted;
{
WORD_LIST *tlist;
WORD_LIST *tresult;
if (string == 0 || *string == '\0')
return ((WORD_LIST *)NULL);
tlist = expand_string_internal (string, quoted);
if (tlist)
{
tresult = word_list_split (tlist);
dispose_words (tlist);
return (tresult);
}
return ((WORD_LIST *)NULL);
}
static WORD_LIST *
expand_string_for_rhs (string, quoted, dollar_at_p, has_dollar_at)
char *string;
int quoted, *dollar_at_p, *has_dollar_at;
{
WORD_DESC td;
WORD_LIST *tresult;
if (string == 0 || *string == '\0')
return (WORD_LIST *)NULL;
td.flags = 0;
td.word = string;
tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, has_dollar_at);
return (tresult);
}
WORD_LIST *
expand_string (string, quoted)
char *string;
int quoted;
{
WORD_LIST *result;
if (string == 0 || *string == '\0')
return ((WORD_LIST *)NULL);
result = expand_string_leave_quoted (string, quoted);
return (result ? dequote_list (result) : result);
}
char *
quote_escapes (string)
char *string;
{
register char *s, *t;
size_t slen;
char *result, *send;
int quote_spaces;
DECLARE_MBSTATE;
slen = strlen (string);
send = string + slen;
quote_spaces = (ifs_value && *ifs_value == 0);
t = result = (char *)xmalloc ((slen * 2) + 1);
s = string;
while (*s)
{
if (*s == CTLESC || *s == CTLNUL || (quote_spaces && *s == ' '))
*t++ = CTLESC;
COPY_CHAR_P (t, s, send);
}
*t = '\0';
return (result);
}
static WORD_LIST *
list_quote_escapes (list)
WORD_LIST *list;
{
register WORD_LIST *w;
char *t;
for (w = list; w; w = w->next)
{
t = w->word->word;
w->word->word = quote_escapes (t);
free (t);
}
return list;
}
static char *
dequote_escapes (string)
char *string;
{
register char *s, *t;
size_t slen;
char *result, *send;
int quote_spaces;
DECLARE_MBSTATE;
if (string == 0)
return string;
slen = strlen (string);
send = string + slen;
t = result = (char *)xmalloc (slen + 1);
s = string;
if (strchr (string, CTLESC) == 0)
return (strcpy (result, s));
quote_spaces = (ifs_value && *ifs_value == 0);
while (*s)
{
if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' ')))
{
s++;
if (*s == '\0')
break;
}
COPY_CHAR_P (t, s, send);
}
*t = '\0';
return result;
}
static char *
make_quoted_char (c)
int c;
{
char *temp;
temp = (char *)xmalloc (3);
if (c == 0)
{
temp[0] = CTLNUL;
temp[1] = '\0';
}
else
{
temp[0] = CTLESC;
temp[1] = c;
temp[2] = '\0';
}
return (temp);
}
char *
quote_string (string)
char *string;
{
register char *t;
size_t slen;
char *result, *send;
if (*string == 0)
{
result = (char *)xmalloc (2);
result[0] = CTLNUL;
result[1] = '\0';
}
else
{
DECLARE_MBSTATE;
slen = strlen (string);
send = string + slen;
result = (char *)xmalloc ((slen * 2) + 1);
for (t = result; string < send; )
{
*t++ = CTLESC;
COPY_CHAR_P (t, string, send);
}
*t = '\0';
}
return (result);
}
char *
dequote_string (string)
char *string;
{
register char *s, *t;
size_t slen;
char *result, *send;
DECLARE_MBSTATE;
slen = strlen (string);
t = result = (char *)xmalloc (slen + 1);
if (QUOTED_NULL (string))
{
result[0] = '\0';
return (result);
}
if (strchr (string, CTLESC) == NULL)
return (strcpy (result, string));
send = string + slen;
s = string;
while (*s)
{
if (*s == CTLESC)
{
s++;
if (*s == '\0')
break;
}
COPY_CHAR_P (t, s, send);
}
*t = '\0';
return (result);
}
static WORD_LIST *
quote_list (list)
WORD_LIST *list;
{
register WORD_LIST *w;
char *t;
for (w = list; w; w = w->next)
{
t = w->word->word;
w->word->word = quote_string (t);
free (t);
w->word->flags |= W_QUOTED;
}
return list;
}
WORD_LIST *
dequote_list (list)
WORD_LIST *list;
{
register char *s;
register WORD_LIST *tlist;
for (tlist = list; tlist; tlist = tlist->next)
{
s = dequote_string (tlist->word->word);
free (tlist->word->word);
tlist->word->word = s;
}
return list;
}
static char *
remove_quoted_escapes (string)
char *string;
{
char *t;
if (string)
{
t = dequote_escapes (string);
strcpy (string, t);
free (t);
}
return (string);
}
static char *
remove_quoted_nulls (string)
char *string;
{
register size_t slen;
register int i, j, prev_i;
DECLARE_MBSTATE;
if (strchr (string, CTLNUL) == 0)
return string;
slen = strlen (string);
i = j = 0;
while (i < slen)
{
if (string[i] == CTLESC)
{
i++; string[j++] = CTLESC;
if (i == slen)
break;
}
else if (string[i] == CTLNUL)
i++;
prev_i = i;
ADVANCE_CHAR (string, slen, i);
if (j < prev_i)
{
do string[j++] = string[prev_i++]; while (prev_i < i);
}
else
j = i;
}
string[j] = '\0';
return (string);
}
void
word_list_remove_quoted_nulls (list)
WORD_LIST *list;
{
register WORD_LIST *t;
for (t = list; t; t = t->next)
{
remove_quoted_nulls (t->word->word);
t->word->flags &= ~W_HASQUOTEDNULL;
}
}
#if defined (HANDLE_MULTIBYTE)
#if 0
static unsigned char *
mb_getcharlens (string, len)
char *string;
int len;
{
int i, offset, last;
unsigned char *ret;
char *p;
DECLARE_MBSTATE;
i = offset = 0;
last = 0;
ret = (unsigned char *)xmalloc (len);
memset (ret, 0, len);
while (string[last])
{
ADVANCE_CHAR (string, len, offset);
ret[last] = offset - last;
last = offset;
}
return ret;
}
#endif
#endif
#define RP_LONG_LEFT 1
#define RP_SHORT_LEFT 2
#define RP_LONG_RIGHT 3
#define RP_SHORT_RIGHT 4
static char *
remove_upattern (param, pattern, op)
char *param, *pattern;
int op;
{
register int len;
register char *end;
register char *p, *ret, c;
len = STRLEN (param);
end = param + len;
switch (op)
{
case RP_LONG_LEFT:
for (p = end; p >= param; p--)
{
c = *p; *p = '\0';
if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
*p = c;
return (savestring (p));
}
*p = c;
}
break;
case RP_SHORT_LEFT:
for (p = param; p <= end; p++)
{
c = *p; *p = '\0';
if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
*p = c;
return (savestring (p));
}
*p = c;
}
break;
case RP_LONG_RIGHT:
for (p = param; p <= end; p++)
{
if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
c = *p; *p = '\0';
ret = savestring (param);
*p = c;
return (ret);
}
}
break;
case RP_SHORT_RIGHT:
for (p = end; p >= param; p--)
{
if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
c = *p; *p = '\0';
ret = savestring (param);
*p = c;
return (ret);
}
}
break;
}
return (savestring (param));
}
#if defined (HANDLE_MULTIBYTE)
static wchar_t *
remove_wpattern (wparam, wstrlen, wpattern, op)
wchar_t *wparam;
size_t wstrlen;
wchar_t *wpattern;
int op;
{
wchar_t wc, *ret;
int n;
switch (op)
{
case RP_LONG_LEFT:
for (n = wstrlen; n >= 0; n--)
{
wc = wparam[n]; wparam[n] = L'\0';
if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
wparam[n] = wc;
return (wcsdup (wparam + n));
}
wparam[n] = wc;
}
break;
case RP_SHORT_LEFT:
for (n = 0; n <= wstrlen; n++)
{
wc = wparam[n]; wparam[n] = L'\0';
if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
wparam[n] = wc;
return (wcsdup (wparam + n));
}
wparam[n] = wc;
}
break;
case RP_LONG_RIGHT:
for (n = 0; n <= wstrlen; n++)
{
if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
wc = wparam[n]; wparam[n] = L'\0';
ret = wcsdup (wparam);
wparam[n] = wc;
return (ret);
}
}
break;
case RP_SHORT_RIGHT:
for (n = wstrlen; n >= 0; n--)
{
if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
wc = wparam[n]; wparam[n] = L'\0';
ret = wcsdup (wparam);
wparam[n] = wc;
return (ret);
}
}
break;
}
return (wcsdup (wparam));
}
#endif
static char *
remove_pattern (param, pattern, op)
char *param, *pattern;
int op;
{
if (param == NULL)
return (param);
if (*param == '\0' || pattern == NULL || *pattern == '\0')
return (savestring (param));
#if defined (HANDLE_MULTIBYTE)
if (MB_CUR_MAX > 1)
{
wchar_t *ret, *oret;
size_t n;
wchar_t *wparam, *wpattern;
mbstate_t ps;
char *xret;
n = xdupmbstowcs (&wpattern, NULL, pattern);
if (n == (size_t)-1)
return (remove_upattern (param, pattern, op));
n = xdupmbstowcs (&wparam, NULL, param);
if (n == (size_t)-1)
{
free (wpattern);
return (remove_upattern (param, pattern, op));
}
oret = ret = remove_wpattern (wparam, n, wpattern, op);
free (wparam);
free (wpattern);
n = strlen (param);
xret = (char *)xmalloc (n + 1);
memset (&ps, '\0', sizeof (mbstate_t));
n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps);
xret[n] = '\0';
free (oret);
return xret;
}
else
#endif
return (remove_upattern (param, pattern, op));
}
static int
match_pattern_char (pat, string)
char *pat, *string;
{
char c;
if (*string == 0)
return (0);
switch (c = *pat++)
{
default:
return (*string == c);
case '\\':
return (*string == *pat);
case '?':
return (*pat == LPAREN ? 1 : (*string != '\0'));
case '*':
return (1);
case '+':
case '!':
case '@':
return (*pat == LPAREN ? 1 : (*string == c));
case '[':
return (*string != '\0');
}
}
static int
match_upattern (string, pat, mtype, sp, ep)
char *string, *pat;
int mtype;
char **sp, **ep;
{
int c, len;
register char *p, *p1, *npat;
char *end;
len = STRLEN (pat);
if (pat[0] != '*' || pat[len - 1] != '*')
{
p = npat = (char *)xmalloc (len + 3);
p1 = pat;
if (*p1 != '*')
*p++ = '*';
while (*p1)
*p++ = *p1++;
if (p1[-1] != '*' || p[-2] == '\\')
*p++ = '*';
*p = '\0';
}
else
npat = pat;
c = strmatch (npat, string, FNMATCH_EXTFLAG);
if (npat != pat)
free (npat);
if (c == FNM_NOMATCH)
return (0);
len = STRLEN (string);
end = string + len;
switch (mtype)
{
case MATCH_ANY:
for (p = string; p <= end; p++)
{
if (match_pattern_char (pat, p))
{
for (p1 = end; p1 >= p; p1--)
{
c = *p1; *p1 = '\0';
if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
{
*p1 = c;
*sp = p;
*ep = p1;
return 1;
}
*p1 = c;
}
}
}
return (0);
case MATCH_BEG:
if (match_pattern_char (pat, string) == 0)
return (0);
for (p = end; p >= string; p--)
{
c = *p; *p = '\0';
if (strmatch (pat, string, FNMATCH_EXTFLAG) == 0)
{
*p = c;
*sp = string;
*ep = p;
return 1;
}
*p = c;
}
return (0);
case MATCH_END:
for (p = string; p <= end; p++)
{
if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
{
*sp = p;
*ep = end;
return 1;
}
}
return (0);
}
return (0);
}
#if defined (HANDLE_MULTIBYTE)
static int
match_pattern_wchar (wpat, wstring)
wchar_t *wpat, *wstring;
{
wchar_t wc;
if (*wstring == 0)
return (0);
switch (wc = *wpat++)
{
default:
return (*wstring == wc);
case L'\\':
return (*wstring == *wpat);
case L'?':
return (*wpat == LPAREN ? 1 : (*wstring != L'\0'));
case L'*':
return (1);
case L'+':
case L'!':
case L'@':
return (*wpat == LPAREN ? 1 : (*wstring == wc));
case L'[':
return (*wstring != L'\0');
}
}
static int
match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep)
wchar_t *wstring;
char **indices;
size_t wstrlen;
wchar_t *wpat;
int mtype;
char **sp, **ep;
{
wchar_t wc, *wp, *nwpat, *wp1;
int len;
#if 0
size_t n, n1;
#else
int n, n1;
#endif
len = wcslen (wpat);
if (wpat[0] != L'*' || wpat[len - 1] != L'*')
{
wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t));
wp1 = wpat;
if (*wp1 != L'*')
*wp++ = L'*';
while (*wp1 != L'\0')
*wp++ = *wp1++;
if (wp1[-1] != L'*' || wp1[-2] == L'\\')
*wp++ = L'*';
*wp = '\0';
}
else
nwpat = wpat;
len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG);
if (nwpat != wpat)
free (nwpat);
if (len == FNM_NOMATCH)
return (0);
switch (mtype)
{
case MATCH_ANY:
for (n = 0; n <= wstrlen; n++)
{
if (match_pattern_wchar (wpat, wstring + n))
{
for (n1 = wstrlen; n1 >= n; n1--)
{
wc = wstring[n1]; wstring[n1] = L'\0';
if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
{
wstring[n1] = wc;
*sp = indices[n];
*ep = indices[n1];
return 1;
}
wstring[n1] = wc;
}
}
}
return (0);
case MATCH_BEG:
if (match_pattern_wchar (wpat, wstring) == 0)
return (0);
for (n = wstrlen; n >= 0; n--)
{
wc = wstring[n]; wstring[n] = L'\0';
if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG) == 0)
{
wstring[n] = wc;
*sp = indices[0];
*ep = indices[n];
return 1;
}
wstring[n] = wc;
}
return (0);
case MATCH_END:
for (n = 0; n <= wstrlen; n++)
{
if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
{
*sp = indices[n];
*ep = indices[wstrlen];
return 1;
}
}
return (0);
}
return (0);
}
#endif
static int
match_pattern (string, pat, mtype, sp, ep)
char *string, *pat;
int mtype;
char **sp, **ep;
{
#if defined (HANDLE_MULTIBYTE)
int ret;
size_t n;
wchar_t *wstring, *wpat;
char **indices;
#endif
if (string == 0 || *string == 0 || pat == 0 || *pat == 0)
return (0);
#if defined (HANDLE_MULTIBYTE)
if (MB_CUR_MAX > 1)
{
n = xdupmbstowcs (&wpat, NULL, pat);
if (n == (size_t)-1)
return (match_upattern (string, pat, mtype, sp, ep));
n = xdupmbstowcs (&wstring, &indices, string);
if (n == (size_t)-1)
{
free (wpat);
return (match_upattern (string, pat, mtype, sp, ep));
}
ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep);
free (wpat);
free (wstring);
free (indices);
return (ret);
}
else
#endif
return (match_upattern (string, pat, mtype, sp, ep));
}
static int
getpatspec (c, value)
int c;
char *value;
{
if (c == '#')
return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT);
else
return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT);
}
static char *
getpattern (value, quoted, expandpat)
char *value;
int quoted, expandpat;
{
char *pat, *tword;
WORD_LIST *l;
#if 0
int i;
#endif
#if 0
if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword)
{
i = 0;
pat = string_extract_double_quoted (tword, &i, 1);
free (tword);
tword = pat;
}
#endif
l = *value ? expand_string_for_rhs (value,
(quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted,
(int *)NULL, (int *)NULL)
: (WORD_LIST *)0;
pat = string_list (l);
dispose_words (l);
if (pat)
{
tword = quote_string_for_globbing (pat, QGLOB_CVTNULL);
free (pat);
pat = tword;
}
return (pat);
}
#if 0
static char *
variable_remove_pattern (value, pattern, patspec, quoted)
char *value, *pattern;
int patspec, quoted;
{
char *tword;
tword = remove_pattern (value, pattern, patspec);
return (tword);
}
#endif
static char *
list_remove_pattern (list, pattern, patspec, itype, quoted)
WORD_LIST *list;
char *pattern;
int patspec, itype, quoted;
{
WORD_LIST *new, *l;
WORD_DESC *w;
char *tword;
for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
{
tword = remove_pattern (l->word->word, pattern, patspec);
w = alloc_word_desc ();
w->word = tword ? tword : savestring ("");
new = make_word_list (w, new);
}
l = REVERSE_LIST (new, WORD_LIST *);
if (itype == '*')
tword = (quoted & Q_DOUBLE_QUOTES) ? string_list_dollar_star (l) : string_list (l);
else
tword = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (l) : l);
dispose_words (l);
return (tword);
}
static char *
parameter_list_remove_pattern (itype, pattern, patspec, quoted)
int itype;
char *pattern;
int patspec, quoted;
{
char *ret;
WORD_LIST *list;
list = list_rest_of_args ();
if (list == 0)
return ((char *)NULL);
ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
dispose_words (list);
return (ret);
}
#if defined (ARRAY_VARS)
static char *
array_remove_pattern (a, pattern, patspec, varname, quoted)
ARRAY *a;
char *pattern;
int patspec;
char *varname;
int quoted;
{
int itype;
char *ret;
WORD_LIST *list;
SHELL_VAR *v;
v = array_variable_part (varname, &ret, 0);
itype = ret[0];
list = array_to_word_list (a);
if (list == 0)
return ((char *)NULL);
ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
dispose_words (list);
return ret;
}
#endif
static char *
parameter_brace_remove_pattern (varname, value, patstr, rtype, quoted)
char *varname, *value, *patstr;
int rtype, quoted;
{
int vtype, patspec, starsub;
char *temp1, *val, *pattern;
SHELL_VAR *v;
if (value == 0)
return ((char *)NULL);
this_command_name = varname;
vtype = get_var_and_type (varname, value, quoted, &v, &val);
if (vtype == -1)
return ((char *)NULL);
starsub = vtype & VT_STARSUB;
vtype &= ~VT_STARSUB;
patspec = getpatspec (rtype, patstr);
if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
patstr++;
temp1 = savestring (patstr);
pattern = getpattern (temp1, quoted, 1);
free (temp1);
temp1 = (char *)NULL;
switch (vtype)
{
case VT_VARIABLE:
case VT_ARRAYMEMBER:
temp1 = remove_pattern (val, pattern, patspec);
if (vtype == VT_VARIABLE)
FREE (val);
if (temp1)
{
val = quote_escapes (temp1);
free (temp1);
temp1 = val;
}
break;
#if defined (ARRAY_VARS)
case VT_ARRAYVAR:
temp1 = array_remove_pattern (array_cell (v), pattern, patspec, varname, quoted);
if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
{
val = quote_escapes (temp1);
free (temp1);
temp1 = val;
}
break;
#endif
case VT_POSPARMS:
temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted);
if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
{
val = quote_escapes (temp1);
free (temp1);
temp1 = val;
}
break;
}
FREE (pattern);
return temp1;
}
WORD_LIST *
expand_word (word, quoted)
WORD_DESC *word;
int quoted;
{
WORD_LIST *result, *tresult;
tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
result = word_list_split (tresult);
dispose_words (tresult);
return (result ? dequote_list (result) : result);
}
WORD_LIST *
expand_word_unsplit (word, quoted)
WORD_DESC *word;
int quoted;
{
WORD_LIST *result;
expand_no_split_dollar_star = 1;
result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
expand_no_split_dollar_star = 0;
return (result ? dequote_list (result) : result);
}
WORD_LIST *
expand_word_leave_quoted (word, quoted)
WORD_DESC *word;
int quoted;
{
return (call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL));
}
#if defined (PROCESS_SUBSTITUTION)
#if !defined (HAVE_DEV_FD)
#define FIFO_INCR 20
struct temp_fifo {
char *file;
pid_t proc;
};
static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL;
static int nfifo;
static int fifo_list_size;
static void
add_fifo_list (pathname)
char *pathname;
{
if (nfifo >= fifo_list_size - 1)
{
fifo_list_size += FIFO_INCR;
fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
fifo_list_size * sizeof (struct temp_fifo));
}
fifo_list[nfifo].file = savestring (pathname);
nfifo++;
}
void
unlink_fifo_list ()
{
int saved, i, j;
if (nfifo == 0)
return;
for (i = saved = 0; i < nfifo; i++)
{
if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
{
unlink (fifo_list[i].file);
free (fifo_list[i].file);
fifo_list[i].file = (char *)NULL;
fifo_list[i].proc = -1;
}
else
saved++;
}
if (saved)
{
for (i = j = 0; i < nfifo; i++)
if (fifo_list[i].file)
{
fifo_list[j].file = fifo_list[i].file;
fifo_list[j].proc = fifo_list[i].proc;
j++;
}
nfifo = j;
}
else
nfifo = 0;
}
int
fifos_pending ()
{
return nfifo;
}
static char *
make_named_pipe ()
{
char *tname;
tname = sh_mktmpname ("sh-np", MT_USERANDOM);
if (mkfifo (tname, 0600) < 0)
{
free (tname);
return ((char *)NULL);
}
add_fifo_list (tname);
return (tname);
}
#else
static char *dev_fd_list = (char *)NULL;
static int nfds;
static int totfds;
static void
add_fifo_list (fd)
int fd;
{
if (!dev_fd_list || fd >= totfds)
{
int ofds;
ofds = totfds;
totfds = getdtablesize ();
if (totfds < 0 || totfds > 256)
totfds = 256;
if (fd > totfds)
totfds = fd + 2;
dev_fd_list = (char *)xrealloc (dev_fd_list, totfds);
memset (dev_fd_list + ofds, '\0', totfds - ofds);
}
dev_fd_list[fd] = 1;
nfds++;
}
int
fifos_pending ()
{
return 0;
}
void
unlink_fifo_list ()
{
register int i;
if (nfds == 0)
return;
for (i = 0; nfds && i < totfds; i++)
if (dev_fd_list[i])
{
close (i);
dev_fd_list[i] = 0;
nfds--;
}
nfds = 0;
}
#if defined (NOTDEF)
print_dev_fd_list ()
{
register int i;
fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ());
fflush (stderr);
for (i = 0; i < totfds; i++)
{
if (dev_fd_list[i])
fprintf (stderr, " %d", i);
}
fprintf (stderr, "\n");
}
#endif
static char *
make_dev_fd_filename (fd)
int fd;
{
char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p;
ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 4);
strcpy (ret, DEV_FD_PREFIX);
p = inttostr (fd, intbuf, sizeof (intbuf));
strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p);
add_fifo_list (fd);
return (ret);
}
#endif
static char *
process_substitute (string, open_for_read_in_child)
char *string;
int open_for_read_in_child;
{
char *pathname;
int fd, result;
pid_t old_pid, pid;
#if defined (HAVE_DEV_FD)
int parent_pipe_fd, child_pipe_fd;
int fildes[2];
#endif
#if defined (JOB_CONTROL)
pid_t old_pipeline_pgrp;
#endif
if (!string || !*string || wordexp_only)
return ((char *)NULL);
#if !defined (HAVE_DEV_FD)
pathname = make_named_pipe ();
#else
if (pipe (fildes) < 0)
{
sys_error (_("cannot make pipe for process substitution"));
return ((char *)NULL);
}
parent_pipe_fd = fildes[open_for_read_in_child];
child_pipe_fd = fildes[1 - open_for_read_in_child];
parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64);
pathname = make_dev_fd_filename (parent_pipe_fd);
#endif
if (!pathname)
{
sys_error (_("cannot make pipe for process substitution"));
return ((char *)NULL);
}
old_pid = last_made_pid;
#if defined (JOB_CONTROL)
old_pipeline_pgrp = pipeline_pgrp;
pipeline_pgrp = shell_pgrp;
save_pipeline (1);
#endif
pid = make_child ((char *)NULL, 1);
if (pid == 0)
{
reset_terminating_signals ();
free_pushed_string_input ();
restore_original_signals ();
setup_async_signals ();
subshell_environment |= SUBSHELL_COMSUB;
}
#if defined (JOB_CONTROL)
set_sigchld_handler ();
stop_making_children ();
pipeline_pgrp = old_pipeline_pgrp;
#endif
if (pid < 0)
{
sys_error (_("cannot make child for process substitution"));
free (pathname);
#if defined (HAVE_DEV_FD)
close (parent_pipe_fd);
close (child_pipe_fd);
#endif
return ((char *)NULL);
}
if (pid > 0)
{
#if defined (JOB_CONTROL)
restore_pipeline (1);
#endif
#if !defined (HAVE_DEV_FD)
fifo_list[nfifo-1].proc = pid;
#endif
last_made_pid = old_pid;
#if defined (JOB_CONTROL) && defined (PGRP_PIPE)
close_pgrp_pipe ();
#endif
#if defined (HAVE_DEV_FD)
close (child_pipe_fd);
#endif
return (pathname);
}
set_sigint_handler ();
#if defined (JOB_CONTROL)
set_job_control (0);
#endif
#if !defined (HAVE_DEV_FD)
fd = open (pathname, open_for_read_in_child ? O_RDONLY|O_NONBLOCK : O_WRONLY);
if (fd < 0)
{
if (open_for_read_in_child)
sys_error (_("cannot open named pipe %s for reading"), pathname);
else
sys_error (_("cannot open named pipe %s for writing"), pathname);
exit (127);
}
if (open_for_read_in_child)
{
if (sh_unset_nodelay_mode (fd) < 0)
{
sys_error (_("cannout reset nodelay mode for fd %d"), fd);
exit (127);
}
}
#else
fd = child_pipe_fd;
#endif
if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
{
sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname,
open_for_read_in_child ? 0 : 1);
exit (127);
}
if (fd != (open_for_read_in_child ? 0 : 1))
close (fd);
if (current_fds_to_close)
{
close_fd_bitmap (current_fds_to_close);
current_fds_to_close = (struct fd_bitmap *)NULL;
}
#if defined (HAVE_DEV_FD)
close (parent_pipe_fd);
dev_fd_list[parent_pipe_fd] = 0;
#endif
result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
#if !defined (HAVE_DEV_FD)
close (open_for_read_in_child ? 0 : 1);
#endif
exit (result);
}
#endif
static char *
read_comsub (fd, quoted)
int fd, quoted;
{
char *istring, buf[128], *bufp;
int istring_index, istring_size, c;
ssize_t bufn;
istring = (char *)NULL;
istring_index = istring_size = bufn = 0;
#ifdef __CYGWIN__
setmode (fd, O_TEXT);
#endif
while (1)
{
if (fd < 0)
break;
if (--bufn <= 0)
{
bufn = zread (fd, buf, sizeof (buf));
if (bufn <= 0)
break;
bufp = buf;
}
c = *bufp++;
if (c == 0)
{
#if 0
internal_warning ("read_comsub: ignored null byte in input");
#endif
continue;
}
RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE);
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) )
istring[istring_index++] = CTLESC;
else if (c == CTLESC)
istring[istring_index++] = CTLESC;
else if (c == CTLNUL || (c == ' ' && (ifs_value && *ifs_value == 0)))
istring[istring_index++] = CTLESC;
istring[istring_index++] = c;
#if 0
#if defined (__CYGWIN__)
if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r')
{
istring_index--;
istring[istring_index - 1] = '\n';
}
#endif
#endif
}
if (istring)
istring[istring_index] = '\0';
if (istring_index == 0)
{
FREE (istring);
return (char *)NULL;
}
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
{
while (istring_index > 0)
{
if (istring[istring_index - 1] == '\n')
{
--istring_index;
if (istring[istring_index - 1] == CTLESC)
--istring_index;
}
else
break;
}
istring[istring_index] = '\0';
}
else
strip_trailing (istring, istring_index - 1, 1);
return istring;
}
char *
command_substitute (string, quoted)
char *string;
int quoted;
{
pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
char *istring;
int result, fildes[2], function_value, pflags, rc;
istring = (char *)NULL;
if (!string || !*string || (string[0] == '\n' && !string[1]))
return ((char *)NULL);
if (wordexp_only && read_but_dont_execute)
{
last_command_exit_value = 125;
jump_to_top_level (EXITPROG);
}
if (subst_assign_varlist == 0 || garglist == 0)
maybe_make_export_env ();
pflags = interactive ? SEVAL_RESETLINE : 0;
if (pipe (fildes) < 0)
{
sys_error (_("cannot make pipe for command substitution"));
goto error_exit;
}
old_pid = last_made_pid;
#if defined (JOB_CONTROL)
old_pipeline_pgrp = pipeline_pgrp;
if ((subshell_environment & SUBSHELL_PIPE) == 0)
pipeline_pgrp = shell_pgrp;
cleanup_the_pipeline ();
#endif
old_async_pid = last_asynchronous_pid;
#if 0
pid = make_child ((char *)NULL, 0);
#else
pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC);
#endif
last_asynchronous_pid = old_async_pid;
if (pid == 0)
reset_signal_handlers ();
#if defined (JOB_CONTROL)
set_sigchld_handler ();
stop_making_children ();
if (pid != 0)
pipeline_pgrp = old_pipeline_pgrp;
#else
stop_making_children ();
#endif
if (pid < 0)
{
sys_error (_("cannot make child for command substitution"));
error_exit:
FREE (istring);
close (fildes[0]);
close (fildes[1]);
return ((char *)NULL);
}
if (pid == 0)
{
set_sigint_handler ();
free_pushed_string_input ();
if (dup2 (fildes[1], 1) < 0)
{
sys_error (_("command_substitute: cannot duplicate pipe as fd 1"));
exit (EXECUTION_FAILURE);
}
if ((fildes[1] != fileno (stdin)) &&
(fildes[1] != fileno (stdout)) &&
(fildes[1] != fileno (stderr)))
close (fildes[1]);
if ((fildes[0] != fileno (stdin)) &&
(fildes[0] != fileno (stdout)) &&
(fildes[0] != fileno (stderr)))
close (fildes[0]);
interactive = 0;
subshell_environment |= SUBSHELL_COMSUB;
if (posixly_correct == 0)
exit_immediately_on_error = 0;
remove_quoted_escapes (string);
startup_state = 2;
result = setjmp (top_level);
if (result == 0 && return_catch_flag)
function_value = setjmp (return_catch);
else
function_value = 0;
if (result == ERREXIT)
rc = last_command_exit_value;
else if (result == EXITPROG)
rc = last_command_exit_value;
else if (result)
rc = EXECUTION_FAILURE;
else if (function_value)
rc = return_catch_value;
else
{
subshell_level++;
rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST);
subshell_level--;
}
last_command_exit_value = rc;
rc = run_exit_trap ();
#if defined (PROCESS_SUBSTITUTION)
unlink_fifo_list ();
#endif
exit (rc);
}
else
{
#if defined (JOB_CONTROL) && defined (PGRP_PIPE)
close_pgrp_pipe ();
#endif
close (fildes[1]);
istring = read_comsub (fildes[0], quoted);
close (fildes[0]);
current_command_subst_pid = pid;
last_command_exit_value = wait_for (pid);
last_command_subst_pid = pid;
last_made_pid = old_pid;
#if defined (JOB_CONTROL)
if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT)
kill (getpid (), SIGINT);
#if 0
if (interactive && pipeline_pgrp != (pid_t)0 && pipeline_pgrp != last_asynchronous_pid)
#else
if (interactive && pipeline_pgrp != (pid_t)0 && (subshell_environment & SUBSHELL_ASYNC) == 0)
#endif
give_terminal_to (pipeline_pgrp, 0);
#endif
return (istring);
}
}
#if defined (ARRAY_VARS)
static arrayind_t
array_length_reference (s)
char *s;
{
int len;
arrayind_t ind;
char *t, c;
ARRAY *array;
SHELL_VAR *var;
var = array_variable_part (s, &t, &len);
if ((var == 0 || array_p (var) == 0) && unbound_vars_is_error)
{
c = *--t;
*t = '\0';
err_unboundvar (s);
*t = c;
return (-1);
}
else if (var == 0)
return 0;
array = array_p (var) ? array_cell (var) : (ARRAY *)NULL;
if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
return (array_p (var) ? array_num_elements (array) : 1);
ind = array_expand_index (t, len);
if (ind < 0)
{
err_badarraysub (t);
return (-1);
}
if (array_p (var))
t = array_reference (array, ind);
else
t = (ind == 0) ? value_cell (var) : (char *)NULL;
len = MB_STRLEN (t);
return (len);
}
#endif
static int
valid_brace_expansion_word (name, var_is_special)
char *name;
int var_is_special;
{
if (DIGIT (*name) && all_digits (name))
return 1;
else if (var_is_special)
return 1;
#if defined (ARRAY_VARS)
else if (valid_array_reference (name))
return 1;
#endif
else if (legal_identifier (name))
return 1;
else
return 0;
}
static int
chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at)
char *name;
int quoted;
int *quoted_dollar_atp, *contains_dollar_at;
{
char *temp1;
if (name == 0)
{
if (quoted_dollar_atp)
*quoted_dollar_atp = 0;
if (contains_dollar_at)
*contains_dollar_at = 0;
return 0;
}
if (name[0] == '@' && name[1] == 0)
{
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
return 1;
}
else if (name[0] == '*' && name[1] == '\0' && quoted == 0)
{
if (contains_dollar_at)
*contains_dollar_at = 1;
return 1;
}
#if defined (ARRAY_VARS)
else if (valid_array_reference (name))
{
temp1 = xstrchr (name, '[');
if (temp1 && temp1[1] == '@' && temp1[2] == ']')
{
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
return 1;
}
if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0)
{
if (contains_dollar_at)
*contains_dollar_at = 1;
return 1;
}
}
#endif
return 0;
}
static WORD_DESC *
parameter_brace_expand_word (name, var_is_special, quoted)
char *name;
int var_is_special, quoted;
{
WORD_DESC *ret;
char *temp, *tt;
intmax_t arg_index;
SHELL_VAR *var;
int atype, rflags;
ret = 0;
temp = 0;
rflags = 0;
if (legal_number (name, &arg_index))
{
tt = get_dollar_var_value (arg_index);
if (tt)
temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
? quote_string (tt)
: quote_escapes (tt);
else
temp = (char *)NULL;
FREE (tt);
}
else if (var_is_special)
{
int sindex;
tt = (char *)xmalloc (2 + strlen (name));
tt[sindex = 0] = '$';
strcpy (tt + 1, name);
ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
(int *)NULL, (int *)NULL, 0);
free (tt);
}
#if defined (ARRAY_VARS)
else if (valid_array_reference (name))
{
temp = array_value (name, quoted, &atype);
if (atype == 0 && temp)
temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
? quote_string (temp)
: quote_escapes (temp);
else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
rflags |= W_HASQUOTEDNULL;
}
#endif
else if (var = find_variable (name))
{
if (var_isset (var) && invisible_p (var) == 0)
{
#if defined (ARRAY_VARS)
temp = array_p (var) ? array_reference (array_cell (var), 0) : value_cell (var);
#else
temp = value_cell (var);
#endif
if (temp)
temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
? quote_string (temp)
: quote_escapes (temp);
}
else
temp = (char *)NULL;
}
else
temp = (char *)NULL;
if (ret == 0)
{
ret = alloc_word_desc ();
ret->word = temp;
ret->flags |= rflags;
}
return ret;
}
static WORD_DESC *
parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at)
char *name;
int var_is_special, quoted;
int *quoted_dollar_atp, *contains_dollar_at;
{
char *temp, *t;
WORD_DESC *w;
w = parameter_brace_expand_word (name, var_is_special, quoted);
t = w->word;
if (t)
{
temp = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
? dequote_string (t)
: dequote_escapes (t);
free (t);
t = temp;
}
dispose_word_desc (w);
chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at);
if (t == 0)
return (WORD_DESC *)NULL;
w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted);
free (t);
return w;
}
static WORD_DESC *
parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat)
char *name, *value;
int c, quoted, *qdollaratp, *hasdollarat;
{
WORD_DESC *w;
WORD_LIST *l;
char *t, *t1, *temp;
int hasdol;
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value)
{
hasdol = 0;
temp = string_extract_double_quoted (value, &hasdol, 1);
}
else
temp = value;
w = alloc_word_desc ();
hasdol = 0;
l = *temp ? expand_string_for_rhs (temp, quoted, &hasdol, (int *)NULL)
: (WORD_LIST *)0;
if (hasdollarat)
*hasdollarat = hasdol || (l && l->next);
if (temp != value)
free (temp);
if (l)
{
temp = (hasdol || l->next) ? string_list_dollar_star (l) : string_list (l);
if (qdollaratp && ((hasdol && quoted) || l->next))
*qdollaratp = 1;
dispose_words (l);
}
else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && hasdol)
{
temp = make_quoted_char ('\0');
w->flags |= W_HASQUOTEDNULL;
}
else
temp = (char *)NULL;
if (c == '-' || c == '+')
{
w->word = temp;
return w;
}
t = temp ? savestring (temp) : savestring ("");
t1 = dequote_string (t);
free (t);
#if defined (ARRAY_VARS)
if (valid_array_reference (name))
assign_array_element (name, t1, 0);
else
#endif
bind_variable (name, t1, 0);
free (t1);
w->word = temp;
return w;
}
static void
parameter_brace_expand_error (name, value)
char *name, *value;
{
WORD_LIST *l;
char *temp;
if (value && *value)
{
l = expand_string (value, 0);
temp = string_list (l);
report_error ("%s: %s", name, temp ? temp : "");
FREE (temp);
dispose_words (l);
}
else
report_error (_("%s: parameter null or not set"), name);
free (name);
FREE (value);
}
static int
valid_length_expression (name)
char *name;
{
return (name[1] == '\0' ||
((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') ||
(DIGIT (name[1]) && all_digits (name + 1)) ||
#if defined (ARRAY_VARS)
valid_array_reference (name + 1) ||
#endif
legal_identifier (name + 1));
}
#if defined (HANDLE_MULTIBYTE)
size_t
mbstrlen (s)
const char *s;
{
size_t clen, nc;
mbstate_t mbs, mbsbak;
nc = 0;
memset (&mbs, 0, sizeof (mbs));
mbsbak = mbs;
while ((clen = mbrlen(s, MB_CUR_MAX, &mbs)) != 0)
{
if (MB_INVALIDCH(clen))
{
clen = 1;
mbs = mbsbak;
}
s += clen;
nc++;
mbsbak = mbs;
}
return nc;
}
#endif
static intmax_t
parameter_brace_expand_length (name)
char *name;
{
char *t, *newname;
intmax_t number, arg_index;
WORD_LIST *list;
#if defined (ARRAY_VARS)
SHELL_VAR *var;
#endif
if (name[1] == '\0')
number = number_of_args ();
else if ((name[1] == '@' || name[1] == '*') && name[2] == '\0')
number = number_of_args ();
else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
{
switch (name[1])
{
case '-':
t = which_set_flags ();
break;
case '?':
t = itos (last_command_exit_value);
break;
case '$':
t = itos (dollar_dollar_pid);
break;
case '!':
if (last_asynchronous_pid == NO_PID)
t = (char *)NULL;
else
t = itos (last_asynchronous_pid);
break;
case '#':
t = itos (number_of_args ());
break;
}
number = STRLEN (t);
FREE (t);
}
#if defined (ARRAY_VARS)
else if (valid_array_reference (name + 1))
number = array_length_reference (name + 1);
#endif
else
{
number = 0;
if (legal_number (name + 1, &arg_index))
{
t = get_dollar_var_value (arg_index);
number = MB_STRLEN (t);
FREE (t);
}
#if defined (ARRAY_VARS)
else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && array_p (var))
{
t = array_reference (array_cell (var), 0);
number = MB_STRLEN (t);
}
#endif
else
{
newname = savestring (name);
newname[0] = '$';
list = expand_string (newname, Q_DOUBLE_QUOTES);
t = list ? string_list (list) : (char *)NULL;
free (newname);
if (list)
dispose_words (list);
number = MB_STRLEN (t);
FREE (t);
}
}
return (number);
}
static char *
skiparith (substr, delim)
char *substr;
int delim;
{
size_t sublen;
int skipcol, pcount, i;
DECLARE_MBSTATE;
sublen = strlen (substr);
i = skipcol = pcount = 0;
while (substr[i])
{
if (substr[i] == LPAREN)
{
pcount++;
i++;
continue;
}
if (substr[i] == RPAREN && pcount)
{
pcount--;
i++;
continue;
}
if (pcount)
{
ADVANCE_CHAR (substr, sublen, i);
continue;
}
if (substr[i] == ':' && skipcol)
{
skipcol--;
i++;
continue;
}
if (substr[i] == delim)
break;
if (substr[i] == '?')
{
skipcol++;
i++;
continue;
}
ADVANCE_CHAR (substr, sublen, i);
}
return (substr + i);
}
static int
verify_substring_values (value, substr, vtype, e1p, e2p)
char *value, *substr;
int vtype;
intmax_t *e1p, *e2p;
{
char *t, *temp1, *temp2;
arrayind_t len;
int expok;
#if defined (ARRAY_VARS)
ARRAY *a;
#endif
t = skiparith (substr, ':');
if (*t && *t == ':')
*t = '\0';
else
t = (char *)0;
temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES);
*e1p = evalexp (temp1, &expok);
free (temp1);
if (expok == 0)
return (0);
len = -1;
switch (vtype)
{
case VT_VARIABLE:
case VT_ARRAYMEMBER:
len = MB_STRLEN (value);
break;
case VT_POSPARMS:
len = number_of_args () + 1;
break;
#if defined (ARRAY_VARS)
case VT_ARRAYVAR:
a = (ARRAY *)value;
len = array_max_index (a) + (*e1p < 0);
break;
#endif
}
if (len == -1)
return -1;
if (*e1p < 0)
*e1p += len;
if (*e1p > len || *e1p < 0)
return (-1);
#if defined (ARRAY_VARS)
if (vtype == VT_ARRAYVAR)
len = array_num_elements (a);
#endif
if (t)
{
t++;
temp2 = savestring (t);
temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
free (temp2);
t[-1] = ':';
*e2p = evalexp (temp1, &expok);
free (temp1);
if (expok == 0)
return (0);
if (*e2p < 0)
{
internal_error (_("%s: substring expression < 0"), t);
return (0);
}
#if defined (ARRAY_VARS)
if (vtype != VT_ARRAYVAR)
#endif
{
*e2p += *e1p;
if (*e2p > len)
*e2p = len;
}
}
else
*e2p = len;
return (1);
}
static int
get_var_and_type (varname, value, quoted, varp, valp)
char *varname, *value;
int quoted;
SHELL_VAR **varp;
char **valp;
{
int vtype;
char *temp;
#if defined (ARRAY_VARS)
SHELL_VAR *v;
#endif
vtype = (varname[0] == '@' || varname[0] == '*') && varname[1] == '\0';
if (vtype == VT_POSPARMS && varname[0] == '*')
vtype |= VT_STARSUB;
*varp = (SHELL_VAR *)NULL;
#if defined (ARRAY_VARS)
if (valid_array_reference (varname))
{
v = array_variable_part (varname, &temp, (int *)0);
if (v && array_p (v))
{
if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')
{
vtype = VT_ARRAYVAR;
if (temp[0] == '*')
vtype |= VT_STARSUB;
*valp = (char *)array_cell (v);
}
else
{
vtype = VT_ARRAYMEMBER;
*valp = array_value (varname, 1, (int *)NULL);
}
*varp = v;
}
else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']'))
{
vtype = VT_VARIABLE;
*varp = v;
if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
*valp = dequote_string (value);
else
*valp = dequote_escapes (value);
}
else
return -1;
}
else if ((v = find_variable (varname)) && (invisible_p (v) == 0) && array_p (v))
{
vtype = VT_ARRAYMEMBER;
*varp = v;
*valp = array_reference (array_cell (v), 0);
}
else
#endif
{
if (value && vtype == VT_VARIABLE)
{
if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
*valp = dequote_string (value);
else
*valp = dequote_escapes (value);
}
else
*valp = value;
}
return vtype;
}
#if defined (HANDLE_MULTIBYTE)
static char *
mb_substring (string, s, e)
char *string;
int s, e;
{
char *tt;
int start, stop, i, slen;
DECLARE_MBSTATE;
start = 0;
slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0;
i = s;
while (string[start] && i--)
ADVANCE_CHAR (string, slen, start);
stop = start;
i = e - s;
while (string[stop] && i--)
ADVANCE_CHAR (string, slen, stop);
tt = substring (string, start, stop);
return tt;
}
#endif
static char *
parameter_brace_substring (varname, value, substr, quoted)
char *varname, *value, *substr;
int quoted;
{
intmax_t e1, e2;
int vtype, r, starsub;
char *temp, *val, *tt, *oname;
SHELL_VAR *v;
if (value == 0)
return ((char *)NULL);
oname = this_command_name;
this_command_name = varname;
vtype = get_var_and_type (varname, value, quoted, &v, &val);
if (vtype == -1)
{
this_command_name = oname;
return ((char *)NULL);
}
starsub = vtype & VT_STARSUB;
vtype &= ~VT_STARSUB;
r = verify_substring_values (val, substr, vtype, &e1, &e2);
this_command_name = oname;
if (r <= 0)
return ((r == 0) ? &expand_param_error : (char *)NULL);
switch (vtype)
{
case VT_VARIABLE:
case VT_ARRAYMEMBER:
#if defined (HANDLE_MULTIBYTE)
if (MB_CUR_MAX > 1)
tt = mb_substring (val, e1, e2);
else
#endif
tt = substring (val, e1, e2);
if (vtype == VT_VARIABLE)
FREE (val);
if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
temp = quote_string (tt);
else
temp = tt ? quote_escapes (tt) : (char *)NULL;
FREE (tt);
break;
case VT_POSPARMS:
tt = pos_params (varname, e1, e2, quoted);
if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
{
temp = tt ? quote_escapes (tt) : (char *)NULL;
FREE (tt);
}
else
temp = tt;
break;
#if defined (ARRAY_VARS)
case VT_ARRAYVAR:
tt = array_subrange (array_cell (v), e1, e2, starsub, quoted);
#if 0
if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
{
temp = tt ? quote_escapes (tt) : (char *)NULL;
FREE (tt);
}
else
#endif
temp = tt;
break;
#endif
default:
temp = (char *)NULL;
}
return temp;
}
char *
pat_subst (string, pat, rep, mflags)
char *string, *pat, *rep;
int mflags;
{
char *ret, *s, *e, *str;
int rsize, rptr, l, replen, mtype;
mtype = mflags & MATCH_TYPEMASK;
if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
{
replen = STRLEN (rep);
l = strlen (string);
ret = (char *)xmalloc (replen + l + 2);
if (replen == 0)
strcpy (ret, string);
else if (mtype == MATCH_BEG)
{
strcpy (ret, rep);
strcpy (ret + replen, string);
}
else
{
strcpy (ret, string);
strcpy (ret + l, rep);
}
return (ret);
}
ret = (char *)xmalloc (rsize = 64);
ret[0] = '\0';
for (replen = STRLEN (rep), rptr = 0, str = string;;)
{
if (match_pattern (str, pat, mtype, &s, &e) == 0)
break;
l = s - str;
RESIZE_MALLOCED_BUFFER (ret, rptr, (l + replen), rsize, 64);
if (l)
{
strncpy (ret + rptr, str, l);
rptr += l;
}
if (replen)
{
strncpy (ret + rptr, rep, replen);
rptr += replen;
}
str = e;
if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
break;
if (s == e)
e++, str++;
}
if (*str)
{
RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
strcpy (ret + rptr, str);
}
else
ret[rptr] = '\0';
return ret;
}
static char *
pos_params_pat_subst (string, pat, rep, mflags)
char *string, *pat, *rep;
int mflags;
{
WORD_LIST *save, *params;
WORD_DESC *w;
char *ret;
save = params = list_rest_of_args ();
if (save == 0)
return ((char *)NULL);
for ( ; params; params = params->next)
{
ret = pat_subst (params->word->word, pat, rep, mflags);
w = alloc_word_desc ();
w->word = ret ? ret : savestring ("");
dispose_word (params->word);
params->word = w;
}
if ((mflags & (MATCH_QUOTED|MATCH_STARSUB)) == (MATCH_QUOTED|MATCH_STARSUB))
ret = string_list_dollar_star (quote_list (save));
else
ret = string_list ((mflags & MATCH_QUOTED) ? quote_list (save) : save);
dispose_words (save);
return (ret);
}
static char *
parameter_brace_patsub (varname, value, patsub, quoted)
char *varname, *value, *patsub;
int quoted;
{
int vtype, mflags, starsub;
char *val, *temp, *pat, *rep, *p, *lpatsub, *tt;
SHELL_VAR *v;
if (value == 0)
return ((char *)NULL);
this_command_name = varname;
vtype = get_var_and_type (varname, value, quoted, &v, &val);
if (vtype == -1)
return ((char *)NULL);
starsub = vtype & VT_STARSUB;
vtype &= ~VT_STARSUB;
mflags = 0;
if (patsub && *patsub == '/')
{
mflags |= MATCH_GLOBREP;
patsub++;
}
lpatsub = savestring (patsub);
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
mflags |= MATCH_QUOTED;
if (starsub)
mflags |= MATCH_STARSUB;
if (rep = quoted_strchr ((*patsub == '/') ? lpatsub+1 : lpatsub, '/', ST_BACKSL))
*rep++ = '\0';
else
rep = (char *)NULL;
if (rep && *rep == '\0')
rep = (char *)NULL;
pat = getpattern (lpatsub, quoted, 1);
if (rep)
{
if ((mflags & MATCH_QUOTED) == 0)
rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
else
rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit);
}
p = pat;
if (mflags & MATCH_GLOBREP)
mflags |= MATCH_ANY;
else if (pat && pat[0] == '#')
{
mflags |= MATCH_BEG;
p++;
}
else if (pat && pat[0] == '%')
{
mflags |= MATCH_END;
p++;
}
else
mflags |= MATCH_ANY;
switch (vtype)
{
case VT_VARIABLE:
case VT_ARRAYMEMBER:
temp = pat_subst (val, p, rep, mflags);
if (vtype == VT_VARIABLE)
FREE (val);
if (temp)
{
tt = quote_escapes (temp);
free (temp);
temp = tt;
}
break;
case VT_POSPARMS:
temp = pos_params_pat_subst (val, p, rep, mflags);
if (temp && (mflags & MATCH_QUOTED) == 0)
{
tt = quote_escapes (temp);
free (temp);
temp = tt;
}
break;
#if defined (ARRAY_VARS)
case VT_ARRAYVAR:
temp = array_patsub (array_cell (v), p, rep, mflags);
#if 0
if (temp && (mflags & MATCH_QUOTED) == 0)
{
tt = quote_escapes (temp);
free (temp);
temp = tt;
}
#endif
break;
#endif
}
FREE (pat);
FREE (rep);
free (lpatsub);
return temp;
}
static int
chk_arithsub (s, len)
const char *s;
int len;
{
int i, count;
DECLARE_MBSTATE;
i = count = 0;
while (i < len)
{
if (s[i] == '(')
count++;
else if (s[i] == ')')
{
count--;
if (count < 0)
return 0;
}
switch (s[i])
{
default:
ADVANCE_CHAR (s, len, i);
break;
case '\\':
i++;
if (s[i])
ADVANCE_CHAR (s, len, i);
break;
case '\'':
i = skip_single_quoted (s, len, ++i);
break;
case '"':
i = skip_double_quoted ((char *)s, len, ++i);
break;
}
}
return (count == 0);
}
static WORD_DESC *
parameter_brace_expand (string, indexp, quoted, quoted_dollar_atp, contains_dollar_at)
char *string;
int *indexp, quoted, *quoted_dollar_atp, *contains_dollar_at;
{
int check_nullness, var_is_set, var_is_null, var_is_special;
int want_substring, want_indir, want_patsub;
char *name, *value, *temp, *temp1;
WORD_DESC *tdesc, *ret;
int t_index, sindex, c, tflag;
intmax_t number;
value = (char *)NULL;
var_is_set = var_is_null = var_is_special = check_nullness = 0;
want_substring = want_indir = want_patsub = 0;
sindex = *indexp;
t_index = ++sindex;
if (string[t_index] == '#' && legal_variable_starter (string[t_index+1]))
name = string_extract (string, &t_index, "}", EX_VARNAME);
else
name = string_extract (string, &t_index, "#%:-=?+/}", EX_VARNAME);
ret = 0;
tflag = 0;
if ((sindex == t_index &&
(string[t_index] == '-' ||
string[t_index] == '?' ||
string[t_index] == '#')) ||
(sindex == t_index - 1 && string[sindex] == '!' &&
(string[t_index] == '#' ||
string[t_index] == '?' ||
string[t_index] == '@' ||
string[t_index] == '*')))
{
t_index++;
free (name);
temp1 = string_extract (string, &t_index, "#%:-=?+/}", 0);
name = (char *)xmalloc (3 + (strlen (temp1)));
*name = string[sindex];
if (string[sindex] == '!')
{
name[1] = string[sindex + 1];
strcpy (name + 2, temp1);
}
else
strcpy (name + 1, temp1);
free (temp1);
}
sindex = t_index;
if (c = string[sindex])
sindex++;
if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
{
check_nullness++;
if (c = string[sindex])
sindex++;
}
else if (c == ':' && string[sindex] != RBRACE)
want_substring = 1;
else if (c == '/' && string[sindex] != RBRACE)
want_patsub = 1;
if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
{
name = (char *)xrealloc (name, 3);
name[1] = c;
name[2] = '\0';
c = string[sindex++];
}
if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
member (c, "%:=+/") && string[sindex] == RBRACE)
{
temp = (char *)NULL;
goto bad_substitution;
}
want_indir = *name == '!' &&
(legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1])
|| VALID_INDIR_PARAM (name[1]));
if (SPECIAL_VAR (name, want_indir))
var_is_special++;
if (*name == '#' && name[1])
{
if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
{
temp = (char *)NULL;
goto bad_substitution;
}
number = parameter_brace_expand_length (name);
free (name);
*indexp = sindex;
if (number < 0)
return (&expand_wdesc_error);
else
{
ret = alloc_word_desc ();
ret->word = itos (number);
return ret;
}
}
if (name[0] == '@' && name[1] == '\0')
{
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
}
if (want_indir && string[sindex - 1] == RBRACE &&
(string[sindex - 2] == '*' || string[sindex - 2] == '@') &&
legal_variable_starter ((unsigned char) name[1]))
{
char **x;
WORD_LIST *xlist;
temp1 = savestring (name + 1);
number = strlen (temp1);
temp1[number - 1] = '\0';
x = all_variables_matching_prefix (temp1);
xlist = strvec_to_word_list (x, 0, 0);
if (string[sindex - 2] == '*')
temp = string_list_dollar_star (xlist);
else
{
temp = string_list_dollar_at (xlist, quoted);
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
}
free (x);
free (xlist);
free (temp1);
*indexp = sindex;
ret = alloc_word_desc ();
ret->word = temp;
return ret;
}
#if defined (ARRAY_VARS)
if (want_indir && string[sindex - 1] == RBRACE &&
string[sindex - 2] == ']' && valid_array_reference (name+1))
{
char *x, *x1;
temp1 = savestring (name + 1);
x = array_variable_name (temp1, &x1, (int *)0);
FREE (x);
if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == ']')
{
temp = array_keys (temp1, quoted);
if (x1[0] == '@')
{
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
}
free (temp1);
*indexp = sindex;
ret = alloc_word_desc ();
ret->word = temp;
return ret;
}
free (temp1);
}
#endif
if (valid_brace_expansion_word (want_indir ? name + 1 : name,
var_is_special) == 0)
{
temp = (char *)NULL;
goto bad_substitution;
}
if (want_indir)
tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at);
else
tdesc = parameter_brace_expand_word (name, var_is_special, quoted);
if (tdesc)
{
temp = tdesc->word;
tflag = tdesc->flags;
dispose_word_desc (tdesc);
}
else
temp = (char *)0;
#if defined (ARRAY_VARS)
if (valid_array_reference (name))
chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at);
#endif
var_is_set = temp != (char *)0;
var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
if (c && c != RBRACE)
{
value = extract_dollar_brace_string (string, &sindex, quoted, 0);
if (string[sindex] == RBRACE)
sindex++;
else
goto bad_substitution;
}
else
value = (char *)NULL;
*indexp = sindex;
if (want_substring)
{
temp1 = parameter_brace_substring (name, temp, value, quoted);
FREE (name);
FREE (value);
FREE (temp);
if (temp1 == &expand_param_error)
return (&expand_wdesc_error);
else if (temp1 == &expand_param_fatal)
return (&expand_wdesc_fatal);
ret = alloc_word_desc ();
ret->word = temp1;
if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
return ret;
}
else if (want_patsub)
{
temp1 = parameter_brace_patsub (name, temp, value, quoted);
FREE (name);
FREE (value);
FREE (temp);
if (temp1 == &expand_param_error)
return (&expand_wdesc_error);
else if (temp1 == &expand_param_fatal)
return (&expand_wdesc_fatal);
ret = alloc_word_desc ();
ret->word = temp1;
return ret;
}
switch (c)
{
default:
case '\0':
bad_substitution:
report_error (_("%s: bad substitution"), string ? string : "??");
FREE (value);
FREE (temp);
free (name);
return &expand_wdesc_error;
case RBRACE:
if (var_is_set == 0 && unbound_vars_is_error)
{
err_unboundvar (name);
FREE (value);
FREE (temp);
free (name);
last_command_exit_value = EXECUTION_FAILURE;
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
break;
case '#':
case '%':
if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
{
FREE (value);
break;
}
temp1 = parameter_brace_remove_pattern (name, temp, value, c, quoted);
free (temp);
free (value);
temp = temp1;
break;
case '-':
case '=':
case '?':
case '+':
if (var_is_set && var_is_null == 0)
{
if (c == '+')
{
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 0;
if (contains_dollar_at)
*contains_dollar_at = 0;
FREE (temp);
if (value)
{
ret = parameter_brace_expand_rhs (name, value, c,
quoted,
quoted_dollar_atp,
contains_dollar_at);
free (value);
}
else
temp = (char *)NULL;
}
else
{
FREE (value);
}
}
else
{
FREE (temp);
temp = (char *)NULL;
if (c == '=' && var_is_special)
{
report_error (_("$%s: cannot assign in this way"), name);
free (name);
free (value);
return &expand_wdesc_error;
}
else if (c == '?')
{
parameter_brace_expand_error (name, value);
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
else if (c != '+')
{
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 0;
if (contains_dollar_at)
*contains_dollar_at = 0;
ret = parameter_brace_expand_rhs (name, value, c, quoted,
quoted_dollar_atp,
contains_dollar_at);
}
free (value);
}
break;
}
free (name);
if (ret == 0)
{
ret = alloc_word_desc ();
ret->flags = tflag;
ret->word = temp;
}
return (ret);
}
static WORD_DESC *
param_expand (string, sindex, quoted, expanded_something,
contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
pflags)
char *string;
int *sindex, quoted, *expanded_something, *contains_dollar_at;
int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
{
char *temp, *temp1, uerror[3];
int zindex, t_index, expok;
unsigned char c;
intmax_t number;
SHELL_VAR *var;
WORD_LIST *list;
WORD_DESC *tdesc, *ret;
int tflag;
zindex = *sindex;
c = string[++zindex];
temp = (char *)NULL;
ret = tdesc = (WORD_DESC *)NULL;
tflag = 0;
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp1 = dollar_vars[TODIGIT (c)];
if (unbound_vars_is_error && temp1 == (char *)NULL)
{
uerror[0] = '$';
uerror[1] = c;
uerror[2] = '\0';
err_unboundvar (uerror);
last_command_exit_value = EXECUTION_FAILURE;
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
if (temp1)
temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
? quote_string (temp1)
: quote_escapes (temp1);
else
temp = (char *)NULL;
break;
case '$':
temp = itos (dollar_dollar_pid);
break;
case '#':
temp = itos (number_of_args ());
break;
case '?':
temp = itos (last_command_exit_value);
break;
case '-':
temp = which_set_flags ();
break;
case '!':
if (last_asynchronous_pid == NO_PID)
{
if (expanded_something)
*expanded_something = 0;
temp = (char *)NULL;
if (unbound_vars_is_error)
{
uerror[0] = '$';
uerror[1] = c;
uerror[2] = '\0';
err_unboundvar (uerror);
last_command_exit_value = EXECUTION_FAILURE;
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
}
else
temp = itos (last_asynchronous_pid);
break;
case '*':
list = list_rest_of_args ();
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0)
temp = (char *)NULL;
else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
{
temp = (quoted & Q_DOUBLE_QUOTES) ? string_list_dollar_star (list) : string_list (list);
temp1 = quote_string (temp);
if (*temp == 0)
tflag |= W_HASQUOTEDNULL;
free (temp);
temp = temp1;
}
else
{
#if 1
# if defined (HANDLE_MULTIBYTE)
if (expand_no_split_dollar_star && ifs_firstc[0] == 0)
# else
if (expand_no_split_dollar_star && ifs_firstc == 0)
# endif
temp = string_list_dollar_star (list);
else
temp = string_list_dollar_at (list, quoted);
#else
temp = string_list_dollar_at (list, quoted);
#endif
if (expand_no_split_dollar_star == 0 && contains_dollar_at)
*contains_dollar_at = 1;
}
dispose_words (list);
break;
case '@':
list = list_rest_of_args ();
if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
*quoted_dollar_at_p = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
temp = string_list_dollar_at (list, quoted);
dispose_words (list);
break;
case LBRACE:
tdesc = parameter_brace_expand (string, &zindex, quoted,
quoted_dollar_at_p,
contains_dollar_at);
if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
return (tdesc);
temp = tdesc ? tdesc->word : (char *)0;
if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp))
{
if (had_quoted_null_p)
*had_quoted_null_p = 1;
if (*quoted_dollar_at_p == 0)
{
free (temp);
tdesc->word = temp = (char *)NULL;
}
}
ret = tdesc;
goto return0;
case LPAREN:
t_index = zindex + 1;
temp = extract_command_subst (string, &t_index);
zindex = t_index;
if (temp && *temp == LPAREN)
{
char *temp2;
temp1 = temp + 1;
temp2 = savestring (temp1);
t_index = strlen (temp2) - 1;
if (temp2[t_index] != RPAREN)
{
free (temp2);
goto comsub;
}
temp2[t_index] = '\0';
if (chk_arithsub (temp2, t_index) == 0)
{
free (temp2);
goto comsub;
}
temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
free (temp2);
arithsub:
this_command_name = (char *)NULL;
number = evalexp (temp1, &expok);
free (temp);
free (temp1);
if (expok == 0)
{
if (interactive_shell == 0 && posixly_correct)
{
last_command_exit_value = EXECUTION_FAILURE;
return (&expand_wdesc_fatal);
}
else
return (&expand_wdesc_error);
}
temp = itos (number);
break;
}
comsub:
if (pflags & PF_NOCOMSUB)
temp1 = substring (string, *sindex, zindex+1);
else
temp1 = command_substitute (temp, quoted);
FREE (temp);
temp = temp1;
break;
case '[':
t_index = zindex + 1;
temp = extract_arithmetic_subst (string, &t_index);
zindex = t_index;
temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES);
goto arithsub;
default:
temp = (char *)NULL;
for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++)
;
temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL;
if (temp1 == 0 || *temp1 == '\0')
{
FREE (temp1);
temp = (char *)xmalloc (2);
temp[0] = '$';
temp[1] = '\0';
if (expanded_something)
*expanded_something = 0;
goto return0;
}
var = find_variable (temp1);
if (var && invisible_p (var) == 0 && var_isset (var))
{
#if defined (ARRAY_VARS)
if (array_p (var))
{
temp = array_reference (array_cell (var), 0);
if (temp)
temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
? quote_string (temp)
: quote_escapes (temp);
else if (unbound_vars_is_error)
goto unbound_variable;
}
else
#endif
{
temp = value_cell (var);
temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
? quote_string (temp)
: quote_escapes (temp);
}
free (temp1);
goto return0;
}
temp = (char *)NULL;
unbound_variable:
if (unbound_vars_is_error)
err_unboundvar (temp1);
else
{
free (temp1);
goto return0;
}
free (temp1);
last_command_exit_value = EXECUTION_FAILURE;
return ((unbound_vars_is_error && interactive_shell == 0)
? &expand_wdesc_fatal
: &expand_wdesc_error);
}
if (string[zindex])
zindex++;
return0:
*sindex = zindex;
if (ret == 0)
{
ret = alloc_word_desc ();
ret->flags = tflag;
ret->word = temp;
}
return ret;
}
#define UNQUOTED 0
#define PARTIALLY_QUOTED 1
#define WHOLLY_QUOTED 2
static WORD_LIST *
expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something)
WORD_DESC *word;
int quoted, isexp;
int *contains_dollar_at;
int *expanded_something;
{
WORD_LIST *list;
WORD_DESC *tword;
char *istring;
int istring_size;
int istring_index;
char *temp, *temp1;
register char *string;
size_t string_size;
int sindex;
int quoted_dollar_at;
int quoted_state;
int had_quoted_null;
int has_dollar_at;
int tflag;
int assignoff;
register unsigned char c;
int t_index;
char twochars[2];
DECLARE_MBSTATE;
istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
istring[istring_index = 0] = '\0';
quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
quoted_state = UNQUOTED;
string = word->word;
if (string == 0)
goto finished_with_string;
string_size = (MB_CUR_MAX > 1) ? strlen (string) : 1;
if (contains_dollar_at)
*contains_dollar_at = 0;
assignoff = -1;
for (sindex = 0; ;)
{
c = string[sindex];
switch (c)
{
case '\0':
goto finished_with_string;
case CTLESC:
sindex++;
#if HANDLE_MULTIBYTE
if (MB_CUR_MAX > 1 && string[sindex])
{
SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
}
else
#endif
{
temp = (char *)xmalloc (3);
temp[0] = CTLESC;
temp[1] = c = string[sindex];
temp[2] = '\0';
}
dollar_add_string:
if (string[sindex])
sindex++;
add_string:
if (temp)
{
istring = sub_append_string (temp, istring, &istring_index, &istring_size);
temp = (char *)0;
}
break;
#if defined (PROCESS_SUBSTITUTION)
case '<':
case '>':
{
if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB)) || posixly_correct)
{
sindex--;
goto add_character;
}
else
t_index = sindex + 1;
temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index);
sindex = t_index;
temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0;
FREE (temp1);
goto dollar_add_string;
}
#endif
case '=':
if (word->flags & (W_ASSIGNRHS|W_NOTILDE))
goto add_character;
if ((word->flags & W_ASSIGNMENT) &&
(posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
assignoff == -1 && sindex > 0)
assignoff = sindex;
if (sindex == assignoff && string[sindex+1] == '~')
word->flags |= W_ITILDE;
#if 0
else if ((word->flags & W_ASSIGNMENT) &&
(posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
string[sindex+1] == '~')
word->flags |= W_ITILDE;
#endif
goto add_character;
case ':':
if (word->flags & W_NOTILDE)
goto add_character;
if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) &&
string[sindex+1] == '~')
word->flags |= W_ITILDE;
goto add_character;
case '~':
if ((word->flags & (W_NOTILDE|W_DQUOTE)) ||
(sindex > 0 && ((word->flags & W_ITILDE) == 0)) ||
(quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
{
word->flags &= ~W_ITILDE;
goto add_character;
}
if (word->flags & W_ASSIGNRHS)
tflag = 2;
else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP))
tflag = 1;
else
tflag = 0;
temp = bash_tilde_find_word (string + sindex, tflag, &t_index);
word->flags &= ~W_ITILDE;
if (temp && *temp && t_index > 0)
{
temp1 = bash_tilde_expand (temp, tflag);
if (temp1 && *temp1 == '~' && STREQ (temp, temp1))
{
FREE (temp);
FREE (temp1);
goto add_character;
}
free (temp);
temp = temp1;
sindex += t_index;
goto add_string;
}
else
{
FREE (temp);
goto add_character;
}
case '$':
if (expanded_something)
*expanded_something = 1;
has_dollar_at = 0;
tword = param_expand (string, &sindex, quoted, expanded_something,
&has_dollar_at, "ed_dollar_at,
&had_quoted_null,
(word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0);
if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal)
{
free (string);
free (istring);
return ((tword == &expand_wdesc_error) ? &expand_word_error
: &expand_word_fatal);
}
if (contains_dollar_at && has_dollar_at)
*contains_dollar_at = 1;
if (tword && (tword->flags & W_HASQUOTEDNULL))
had_quoted_null = 1;
temp = tword->word;
dispose_word_desc (tword);
goto add_string;
break;
case '`':
{
t_index = sindex++;
temp = string_extract (string, &sindex, "`", EX_REQMATCH);
if (temp == &extract_string_error || temp == &extract_string_fatal)
{
if (sindex - 1 == t_index)
{
sindex = t_index;
goto add_character;
}
report_error ("bad substitution: no closing \"`\" in %s", string+t_index);
free (string);
free (istring);
return ((temp == &extract_string_error) ? &expand_word_error
: &expand_word_fatal);
}
if (expanded_something)
*expanded_something = 1;
if (word->flags & W_NOCOMSUB)
temp1 = substring (string, t_index, sindex + 1);
else
{
de_backslash (temp);
temp1 = command_substitute (temp, quoted);
}
FREE (temp);
temp = temp1;
goto dollar_add_string;
}
case '\\':
if (string[sindex + 1] == '\n')
{
sindex += 2;
continue;
}
c = string[++sindex];
if (quoted & Q_HERE_DOCUMENT)
tflag = CBSHDOC;
else if (quoted & Q_DOUBLE_QUOTES)
tflag = CBSDQUOTE;
else
tflag = 0;
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0))
{
SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size);
}
else if (c == 0)
{
c = CTLNUL;
sindex--;
goto add_character;
}
else
{
SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
}
sindex++;
add_twochars:
RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
DEFAULT_ARRAY_SIZE);
istring[istring_index++] = twochars[0];
istring[istring_index++] = twochars[1];
istring[istring_index] = '\0';
break;
case '"':
#if 0
if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
#else
if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
#endif
goto add_character;
t_index = ++sindex;
temp = string_extract_double_quoted (string, &sindex, 0);
quoted_state = (t_index == 1 && string[sindex] == '\0')
? WHOLLY_QUOTED
: PARTIALLY_QUOTED;
if (temp && *temp)
{
tword = alloc_word_desc ();
tword->word = temp;
temp = (char *)NULL;
has_dollar_at = 0;
list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &has_dollar_at, (int *)NULL);
if (list == &expand_word_error || list == &expand_word_fatal)
{
free (istring);
free (string);
tword->word = (char *)NULL;
dispose_word (tword);
return list;
}
dispose_word (tword);
if (list == 0 && has_dollar_at)
{
quoted_dollar_at++;
break;
}
if (list)
dequote_list (list);
if (list && list->word && (list->word->flags & W_HASQUOTEDNULL))
had_quoted_null = 1;
if (has_dollar_at)
{
quoted_dollar_at++;
if (contains_dollar_at)
*contains_dollar_at = 1;
if (expanded_something)
*expanded_something = 1;
}
}
else
{
FREE (temp);
list = (WORD_LIST *)NULL;
}
if (list)
{
if (list->next)
{
temp = quoted_dollar_at
? string_list_dollar_at (list, Q_DOUBLE_QUOTES)
: string_list (quote_list (list));
dispose_words (list);
goto add_string;
}
else
{
temp = savestring (list->word->word);
tflag = list->word->flags;
dispose_words (list);
if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0)
remove_quoted_nulls (temp);
}
}
else
temp = (char *)NULL;
if (temp == 0 && quoted_state == PARTIALLY_QUOTED)
continue;
add_quoted_string:
if (temp)
{
temp1 = temp;
temp = quote_string (temp);
free (temp1);
goto add_string;
}
else
{
c = CTLNUL;
sindex--;
goto add_character;
}
case '\'':
#if 0
if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
#else
if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
#endif
goto add_character;
t_index = ++sindex;
temp = string_extract_single_quoted (string, &sindex);
quoted_state = (t_index == 1 && string[sindex] == '\0')
? WHOLLY_QUOTED
: PARTIALLY_QUOTED;
if (*temp == '\0')
{
free (temp);
temp = (char *)NULL;
}
else
remove_quoted_escapes (temp);
if (temp == 0 && (quoted_state == PARTIALLY_QUOTED))
continue;
if (temp == 0)
{
c = CTLNUL;
sindex--;
goto add_character;
}
else
goto add_quoted_string;
default:
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c)))
{
if (string[sindex])
sindex++;
if (c == 0)
{
c = CTLNUL;
goto add_character;
}
else
{
#if HANDLE_MULTIBYTE
if (MB_CUR_MAX > 1)
sindex--;
if (MB_CUR_MAX > 1)
{
SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
}
else
#endif
{
twochars[0] = CTLESC;
twochars[1] = c;
goto add_twochars;
}
}
}
SADD_MBCHAR (temp, string, sindex, string_size);
add_character:
RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
DEFAULT_ARRAY_SIZE);
istring[istring_index++] = c;
istring[istring_index] = '\0';
sindex++;
}
}
finished_with_string:
if (*istring == '\0')
{
if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED))
{
istring[0] = CTLNUL;
istring[1] = '\0';
tword = make_bare_word (istring);
tword->flags |= W_HASQUOTEDNULL;
list = make_word_list (tword, (WORD_LIST *)NULL);
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
tword->flags |= W_QUOTED;
}
else if (quoted_state == UNQUOTED || quoted_dollar_at)
list = (WORD_LIST *)NULL;
#if 0
else
{
tword = make_bare_word (istring);
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
tword->flags |= W_QUOTED;
list = make_word_list (tword, (WORD_LIST *)NULL);
}
#else
else
list = (WORD_LIST *)NULL;
#endif
}
else if (word->flags & W_NOSPLIT)
{
tword = make_bare_word (istring);
if (word->flags & W_ASSIGNMENT)
tword->flags |= W_ASSIGNMENT;
if (word->flags & W_COMPASSIGN)
tword->flags |= W_COMPASSIGN;
if (word->flags & W_NOGLOB)
tword->flags |= W_NOGLOB;
if (word->flags & W_NOEXPAND)
tword->flags |= W_NOEXPAND;
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
tword->flags |= W_QUOTED;
if (had_quoted_null)
tword->flags |= W_HASQUOTEDNULL;
list = make_word_list (tword, (WORD_LIST *)NULL);
}
else
{
char *ifs_chars;
ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
if (has_dollar_at && ifs_chars)
list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
else
{
tword = make_bare_word (istring);
if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED))
tword->flags |= W_QUOTED;
if (word->flags & W_ASSIGNMENT)
tword->flags |= W_ASSIGNMENT;
if (word->flags & W_COMPASSIGN)
tword->flags |= W_COMPASSIGN;
if (word->flags & W_NOGLOB)
tword->flags |= W_NOGLOB;
if (word->flags & W_NOEXPAND)
tword->flags |= W_NOEXPAND;
if (had_quoted_null)
tword->flags |= W_HASQUOTEDNULL;
list = make_word_list (tword, (WORD_LIST *)NULL);
}
}
free (istring);
return (list);
}
char *
string_quote_removal (string, quoted)
char *string;
int quoted;
{
size_t slen;
char *r, *result_string, *temp, *send;
int sindex, tindex, dquote;
unsigned char c;
DECLARE_MBSTATE;
slen = strlen (string);
send = string + slen;
r = result_string = (char *)xmalloc (slen + 1);
for (dquote = sindex = 0; c = string[sindex];)
{
switch (c)
{
case '\\':
c = string[++sindex];
if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
*r++ = '\\';
default:
SCOPY_CHAR_M (r, string, send, sindex);
break;
case '\'':
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote)
{
*r++ = c;
sindex++;
break;
}
tindex = sindex + 1;
temp = string_extract_single_quoted (string, &tindex);
if (temp)
{
strcpy (r, temp);
r += strlen (r);
free (temp);
}
sindex = tindex;
break;
case '"':
dquote = 1 - dquote;
sindex++;
break;
}
}
*r = '\0';
return (result_string);
}
#if 0
WORD_DESC *
word_quote_removal (word, quoted)
WORD_DESC *word;
int quoted;
{
WORD_DESC *w;
char *t;
t = string_quote_removal (word->word, quoted);
w = alloc_word_desc ();
w->word = t ? t : savestring ("");
return (w);
}
WORD_LIST *
word_list_quote_removal (list, quoted)
WORD_LIST *list;
int quoted;
{
WORD_LIST *result, *t, *tresult, *e;
for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
{
tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL);
#if 0
result = (WORD_LIST *) list_append (result, tresult);
#else
if (result == 0)
result = e = tresult;
else
{
e->next = tresult;
while (e->next)
e = e->next;
}
#endif
}
return (result);
}
#endif
void
setifs (v)
SHELL_VAR *v;
{
char *t;
unsigned char uc;
ifs_var = v;
#if 0
ifs_value = v ? value_cell (v) : " \t\n";
#else
ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n";
#endif
memset (ifs_cmap, '\0', sizeof (ifs_cmap));
for (t = ifs_value ; t && *t; t++)
{
uc = *t;
ifs_cmap[uc] = 1;
}
#if defined (HANDLE_MULTIBYTE)
if (ifs_value == 0)
{
ifs_firstc[0] = '\0';
ifs_firstc_len = 1;
}
else
{
size_t ifs_len;
ifs_len = strnlen (ifs_value, MB_CUR_MAX);
ifs_firstc_len = MBLEN (ifs_value, ifs_len);
if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len))
{
ifs_firstc[0] = ifs_value[0];
ifs_firstc[1] = '\0';
ifs_firstc_len = 1;
}
else
memcpy (ifs_firstc, ifs_value, ifs_firstc_len);
}
#else
ifs_firstc = ifs_value ? *ifs_value : 0;
#endif
}
char *
getifs ()
{
return ifs_value;
}
WORD_LIST *
word_split (w, ifs_chars)
WORD_DESC *w;
char *ifs_chars;
{
WORD_LIST *result;
if (w)
{
char *xifs;
xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars;
result = list_string (w->word, xifs, w->flags & W_QUOTED);
}
else
result = (WORD_LIST *)NULL;
return (result);
}
static WORD_LIST *
word_list_split (list)
WORD_LIST *list;
{
WORD_LIST *result, *t, *tresult, *e;
for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
{
tresult = word_split (t->word, ifs_value);
if (result == 0)
result = e = tresult;
else
{
e->next = tresult;
while (e->next)
e = e->next;
}
}
return (result);
}
static void
exp_jump_to_top_level (v)
int v;
{
expand_no_split_dollar_star = 0;
expanding_redir = 0;
if (parse_and_execute_level == 0)
top_level_cleanup ();
jump_to_top_level (v);
}
#define PREPEND_LIST(nlist, elist) \
do { nlist->next = elist; elist = nlist; } while (0)
static WORD_LIST *
separate_out_assignments (tlist)
WORD_LIST *tlist;
{
register WORD_LIST *vp, *lp;
if (!tlist)
return ((WORD_LIST *)NULL);
if (subst_assign_varlist)
dispose_words (subst_assign_varlist);
subst_assign_varlist = (WORD_LIST *)NULL;
vp = lp = tlist;
while (lp && (lp->word->flags & W_ASSIGNMENT))
{
vp = lp;
lp = lp->next;
}
if (lp != tlist)
{
subst_assign_varlist = tlist;
vp->next = (WORD_LIST *)NULL;
tlist = lp;
}
if (!tlist)
return ((WORD_LIST *)NULL);
if (place_keywords_in_env)
{
WORD_LIST *tp;
tp = tlist;
lp = tlist->next;
while (lp)
{
if (lp->word->flags & W_ASSIGNMENT)
{
if (!subst_assign_varlist)
subst_assign_varlist = vp = lp;
else
{
vp->next = lp;
vp = lp;
}
tp->next = lp->next;
lp->next = (WORD_LIST *)NULL;
lp = tp->next;
}
else
{
tp = lp;
lp = lp->next;
}
}
}
return (tlist);
}
#define WEXP_VARASSIGN 0x001
#define WEXP_BRACEEXP 0x002
#define WEXP_TILDEEXP 0x004
#define WEXP_PARAMEXP 0x008
#define WEXP_PATHEXP 0x010
#define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
#define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
#define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
WORD_LIST *
expand_words (list)
WORD_LIST *list;
{
return (expand_word_list_internal (list, WEXP_ALL));
}
WORD_LIST *
expand_words_no_vars (list)
WORD_LIST *list;
{
return (expand_word_list_internal (list, WEXP_NOVARS));
}
WORD_LIST *
expand_words_shellexp (list)
WORD_LIST *list;
{
return (expand_word_list_internal (list, WEXP_SHELLEXP));
}
static WORD_LIST *
glob_expand_word_list (tlist, eflags)
WORD_LIST *tlist;
int eflags;
{
char **glob_array, *temp_string;
register int glob_index;
WORD_LIST *glob_list, *output_list, *disposables, *next;
WORD_DESC *tword;
output_list = disposables = (WORD_LIST *)NULL;
glob_array = (char **)NULL;
while (tlist)
{
next = tlist->next;
if ((tlist->word->flags & W_NOGLOB) == 0 &&
unquoted_glob_pattern_p (tlist->word->word))
{
glob_array = shell_glob_filename (tlist->word->word);
if (glob_array == 0 || GLOB_FAILED (glob_array))
{
glob_array = (char **)xmalloc (sizeof (char *));
glob_array[0] = (char *)NULL;
}
if (glob_array[0] == NULL)
{
temp_string = dequote_string (tlist->word->word);
free (tlist->word->word);
tlist->word->word = temp_string;
}
glob_list = (WORD_LIST *)NULL;
for (glob_index = 0; glob_array[glob_index]; glob_index++)
{
tword = make_bare_word (glob_array[glob_index]);
tword->flags |= W_GLOBEXP;
glob_list = make_word_list (tword, glob_list);
}
if (glob_list)
{
output_list = (WORD_LIST *)list_append (glob_list, output_list);
PREPEND_LIST (tlist, disposables);
}
else if (fail_glob_expansion != 0)
{
report_error (_("no match: %s"), tlist->word->word);
exp_jump_to_top_level (DISCARD);
}
else if (allow_null_glob_expansion == 0)
{
PREPEND_LIST (tlist, output_list);
}
else
{
PREPEND_LIST (tlist, disposables);
}
}
else
{
temp_string = dequote_string (tlist->word->word);
free (tlist->word->word);
tlist->word->word = temp_string;
PREPEND_LIST (tlist, output_list);
}
strvec_dispose (glob_array);
glob_array = (char **)NULL;
tlist = next;
}
if (disposables)
dispose_words (disposables);
if (output_list)
output_list = REVERSE_LIST (output_list, WORD_LIST *);
return (output_list);
}
#if defined (BRACE_EXPANSION)
static WORD_LIST *
brace_expand_word_list (tlist, eflags)
WORD_LIST *tlist;
int eflags;
{
register char **expansions;
char *temp_string;
WORD_LIST *disposables, *output_list, *next;
WORD_DESC *w;
int eindex;
for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next)
{
next = tlist->next;
if (xstrchr (tlist->word->word, LBRACE))
{
expansions = brace_expand (tlist->word->word);
for (eindex = 0; temp_string = expansions[eindex]; eindex++)
{
w = make_word (temp_string);
if (STREQ (temp_string, tlist->word->word))
w->flags = tlist->word->flags;
output_list = make_word_list (w, output_list);
free (expansions[eindex]);
}
free (expansions);
PREPEND_LIST (tlist, disposables);
}
else
PREPEND_LIST (tlist, output_list);
}
if (disposables)
dispose_words (disposables);
if (output_list)
output_list = REVERSE_LIST (output_list, WORD_LIST *);
return (output_list);
}
#endif
static WORD_LIST *
shell_expand_word_list (tlist, eflags)
WORD_LIST *tlist;
int eflags;
{
WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list;
int expanded_something, has_dollar_at;
char *temp_string;
new_list = (WORD_LIST *)NULL;
for (orig_list = tlist; tlist; tlist = next)
{
temp_string = tlist->word->word;
next = tlist->next;
#if defined (ARRAY_VARS)
if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
{
int t;
t = do_word_assignment (tlist->word);
if (t == 0)
{
last_command_exit_value = EXECUTION_FAILURE;
exp_jump_to_top_level (DISCARD);
}
t = assignment (tlist->word->word, 0);
tlist->word->word[t] = '\0';
tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG);
}
#endif
expanded_something = 0;
expanded = expand_word_internal
(tlist->word, 0, 0, &has_dollar_at, &expanded_something);
if (expanded == &expand_word_error || expanded == &expand_word_fatal)
{
tlist->word->word = (char *)NULL;
dispose_words (orig_list);
dispose_words (new_list);
last_command_exit_value = EXECUTION_FAILURE;
if (expanded == &expand_word_error)
exp_jump_to_top_level (DISCARD);
else
exp_jump_to_top_level (FORCE_EOF);
}
if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0)
{
temp_list = word_list_split (expanded);
dispose_words (expanded);
}
else
{
word_list_remove_quoted_nulls (expanded);
temp_list = expanded;
}
expanded = REVERSE_LIST (temp_list, WORD_LIST *);
new_list = (WORD_LIST *)list_append (expanded, new_list);
}
if (orig_list)
dispose_words (orig_list);
if (new_list)
new_list = REVERSE_LIST (new_list, WORD_LIST *);
return (new_list);
}
static WORD_LIST *
expand_word_list_internal (list, eflags)
WORD_LIST *list;
int eflags;
{
WORD_LIST *new_list, *temp_list;
int tint;
if (list == 0)
return ((WORD_LIST *)NULL);
garglist = new_list = copy_word_list (list);
if (eflags & WEXP_VARASSIGN)
{
garglist = new_list = separate_out_assignments (new_list);
if (new_list == 0)
{
if (subst_assign_varlist)
{
for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
{
this_command_name = (char *)NULL;
tint = do_word_assignment (temp_list->word);
if (tint == 0)
{
last_command_exit_value = EXECUTION_FAILURE;
if (interactive_shell == 0 && posixly_correct)
exp_jump_to_top_level (FORCE_EOF);
else
exp_jump_to_top_level (DISCARD);
}
}
dispose_words (subst_assign_varlist);
subst_assign_varlist = (WORD_LIST *)NULL;
}
return ((WORD_LIST *)NULL);
}
}
#if defined (BRACE_EXPANSION)
if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list)
new_list = brace_expand_word_list (new_list, eflags);
#endif
new_list = shell_expand_word_list (new_list, eflags);
if (new_list)
{
if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0)
new_list = glob_expand_word_list (new_list, eflags);
else
new_list = dequote_list (new_list);
}
if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
{
sh_wassign_func_t *assign_func;
assign_func = new_list ? assign_in_env : do_word_assignment;
tempenv_assign_error = 0;
for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
{
this_command_name = (char *)NULL;
tint = (*assign_func) (temp_list->word);
if (tint == 0)
{
if (assign_func == do_word_assignment)
{
last_command_exit_value = EXECUTION_FAILURE;
if (interactive_shell == 0 && posixly_correct)
exp_jump_to_top_level (FORCE_EOF);
else
exp_jump_to_top_level (DISCARD);
}
else
tempenv_assign_error++;
}
}
dispose_words (subst_assign_varlist);
subst_assign_varlist = (WORD_LIST *)NULL;
}
#if 0
tint = list_length (new_list) + 1;
RESIZE_MALLOCED_BUFFER (glob_argv_flags, 0, tint, glob_argv_flags_size, 16);
for (tint = 0, temp_list = new_list; temp_list; temp_list = temp_list->next)
glob_argv_flags[tint++] = (temp_list->word->flags & W_GLOBEXP) ? '1' : '0';
glob_argv_flags[tint] = '\0';
#endif
return (new_list);
}