/* general.c -- Stuff that is used by all files. */
-/* Copyright (C) 1987-2016 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2021 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
#include "findcmd.h"
#include "test.h"
#include "trap.h"
+#include "pathexp.h"
#include "builtins/common.h"
# include <sys/cygwin.h>
#endif
-static char *bash_special_tilde_expansions __P((char *));
-static int unquoted_tilde_word __P((const char *));
-static void initialize_group_array __P((void));
+static char *bash_special_tilde_expansions PARAMS((char *));
+static int unquoted_tilde_word PARAMS((const char *));
+static void initialize_group_array PARAMS((void));
/* A standard error message to use when getcwd() returns NULL. */
const char * const bash_getcwd_errstr = N_("getcwd: cannot access parent directories");
expand_aliases
inherit_errexit
print_shift_error
+ posixglob
and the following variables which cannot be user-modified:
{
&interactive_comments,
&source_uses_path,
- &expand_aliases,
+ &expaliases_flag,
&inherit_errexit,
&print_shift_error,
0
};
+static char *saved_posix_vars = 0;
+
void
posix_initialize (on)
int on;
/* Things that should be turned on when posix mode is enabled. */
if (on != 0)
{
- interactive_comments = source_uses_path = expand_aliases = 1;
+ interactive_comments = source_uses_path = 1;
+ expand_aliases = expaliases_flag = 1;
inherit_errexit = 1;
source_searches_cwd = 0;
print_shift_error = 1;
-
}
/* Things that should be turned on when posix mode is disabled. */
- if (on == 0)
+ else if (saved_posix_vars) /* on == 0, restore saved settings */
+ {
+ set_posix_options (saved_posix_vars);
+ expand_aliases = expaliases_flag;
+ free (saved_posix_vars);
+ saved_posix_vars = 0;
+ }
+ else /* on == 0, restore a default set of settings */
{
source_searches_cwd = 1;
- expand_aliases = interactive_shell;
+ expand_aliases = expaliases_flag = interactive_shell; /* XXX */
print_shift_error = 0;
}
}
return bitmap;
}
+#undef save_posix_options
+void
+save_posix_options ()
+{
+ saved_posix_vars = get_posix_options (saved_posix_vars);
+}
+
void
set_posix_options (bitmap)
const char *bitmap;
}
/* Make sure that WORD is a valid shell identifier, i.e.
- does not contain a dollar sign, nor is quoted in any way. Nor
- does it consist of all digits. If CHECK_WORD is non-zero,
+ does not contain a dollar sign, nor is quoted in any way.
+ If CHECK_WORD is non-zero,
the word is checked to ensure that it consists of only letters,
- digits, and underscores. */
+ digits, and underscores, and does not consist of all digits. */
int
check_identifier (word, check_word)
WORD_DESC *word;
int check_word;
{
- if ((word->flags & (W_HASDOLLAR|W_QUOTED)) || all_digits (word->word))
+ if (word->flags & (W_HASDOLLAR|W_QUOTED)) /* XXX - HASDOLLAR? */
{
internal_error (_("`%s': not a valid identifier"), word->word);
return (0);
}
- else if (check_word && legal_identifier (word->word) == 0)
+ else if (check_word && (all_digits (word->word) || legal_identifier (word->word) == 0))
{
internal_error (_("`%s': not a valid identifier"), word->word);
return (0);
/* Returns non-zero if STRING is an assignment statement. The returned value
is the index of the `=' sign. If FLAGS&1 we are expecting a compound assignment
- and don't want an array subscript before the `='. */
+ and require an array subscript before the `=' to denote an assignment
+ statement. */
int
assignment (string, flags)
const char *string;
c = string[indx = 0];
#if defined (ARRAY_VARS)
- if ((legal_variable_starter (c) == 0) && ((flags&1) == 0 || c != '[')) /* ] */
+ /* If parser_state includes PST_COMPASSIGN, FLAGS will include 1, so we are
+ parsing the contents of a compound assignment. If parser_state includes
+ PST_REPARSE, we are in the middle of an assignment statement and breaking
+ the words between the parens into words and assignment statements, but
+ we don't need to check for that right now. Within a compound assignment,
+ the subscript is required to make the word an assignment statement. If
+ we don't have a subscript, even if the word is a valid assignment
+ statement otherwise, we don't want to treat it as one. */
+ if ((flags & 1) && c != '[') /* ] */
+ return (0);
+ else if ((flags & 1) == 0 && legal_variable_starter (c) == 0)
#else
if (legal_variable_starter (c) == 0)
#endif
int sample_len;
{
register int i;
+ int nline;
unsigned char c;
+ if (sample_len >= 4 && sample[0] == 0x7f && sample[1] == 'E' && sample[2] == 'L' && sample[3] == 'F')
+ return 1;
+
+ /* Generally we check the first line for NULs. If the first line looks like
+ a `#!' interpreter specifier, we look for NULs in the first two lines. */
+ nline = (sample[0] == '#' && sample[1] == '!') ? 2 : 1;
+
for (i = 0; i < sample_len; i++)
{
c = sample[i];
- if (c == '\n')
+ if (c == '\n' && --nline == 0)
return (0);
if (c == '\0')
return (1);
/* **************************************************************** */
#if defined (PUSHD_AND_POPD)
-extern char *get_dirstack_from_string __P((char *));
+extern char *get_dirstack_from_string PARAMS((char *));
#endif
static char **bash_tilde_prefixes;
const char *s;
int assign_p;
{
- int old_immed, old_term, r;
+ int r;
char *ret;
-#if 0
- old_immed = interrupt_immediately;
- old_term = terminate_immediately;
- /* We want to be able to interrupt tilde expansion. Ordinarily, we can just
- jump to top_level, but we don't want to run any trap commands in a signal
- handler context. We might be able to get away with just checking for
- things like SIGINT and SIGQUIT. */
- if (any_signals_trapped () < 0)
- interrupt_immediately = 1;
- terminate_immediately = 1;
-#endif
-
tilde_additional_prefixes = assign_p == 0 ? (char **)0
: (assign_p == 2 ? bash_tilde_prefixes2 : bash_tilde_prefixes);
if (assign_p == 2)
r = (*s == '~') ? unquoted_tilde_word (s) : 1;
ret = r ? tilde_expand (s) : savestring (s);
-#if 0
- interrupt_immediately = old_immed;
- terminate_immediately = old_term;
-#endif
-
QUIT;
return (ret);