diff options
| author | Chet Ramey <chet.ramey@case.edu> | 2013-01-03 10:49:10 -0500 |
|---|---|---|
| committer | Chet Ramey <chet.ramey@case.edu> | 2013-01-03 10:49:10 -0500 |
| commit | 3fe251cb5e2168d5f29b9137ae5619ef63336c06 (patch) | |
| tree | 837f64b18363f33d4ceb50d8053b889c3a3ea1f6 /builtins | |
| parent | 7175a77f3a2805595fab2841bdf491d7be417467 (diff) | |
| download | bash-3fe251cb5e2168d5f29b9137ae5619ef63336c06.tar.gz | |
bash-20121228 remove leftover and stray files
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/common.h~ | 175 | ||||
| -rw-r--r-- | builtins/declare.def~ | 694 | ||||
| -rw-r--r-- | builtins/evalfile.c~ | 348 | ||||
| -rw-r--r-- | builtins/evalstring.c~ | 604 | ||||
| -rw-r--r-- | builtins/read.def~ | 1074 | ||||
| -rw-r--r-- | builtins/shopt.def~ | 783 |
6 files changed, 0 insertions, 3678 deletions
diff --git a/builtins/common.h~ b/builtins/common.h~ deleted file mode 100644 index aaaa0c75..00000000 --- a/builtins/common.h~ +++ /dev/null @@ -1,175 +0,0 @@ -/* common.h -- extern declarations for functions defined in common.c. */ - -/* Copyright (C) 1993-2010 Free Software Foundation, Inc. - - This file is part of GNU Bash, the Bourne Again SHell. - - Bash is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Bash is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Bash. If not, see <http://www.gnu.org/licenses/>. -*/ - -#if !defined (__COMMON_H) -# define __COMMON_H - -#include "stdc.h" - -#define ISOPTION(s, c) (s[0] == '-' && !s[2] && s[1] == c) - -/* Flag values for parse_and_execute () */ -#define SEVAL_NONINT 0x001 -#define SEVAL_INTERACT 0x002 -#define SEVAL_NOHIST 0x004 -#define SEVAL_NOFREE 0x008 -#define SEVAL_RESETLINE 0x010 -#define SEVAL_PARSEONLY 0x020 -#define SEVAL_NOLONGJMP 0x040 - -/* Flags for describe_command, shared between type.def and command.def */ -#define CDESC_ALL 0x001 /* type -a */ -#define CDESC_SHORTDESC 0x002 /* command -V */ -#define CDESC_REUSABLE 0x004 /* command -v */ -#define CDESC_TYPE 0x008 /* type -t */ -#define CDESC_PATH_ONLY 0x010 /* type -p */ -#define CDESC_FORCE_PATH 0x020 /* type -ap or type -P */ -#define CDESC_NOFUNCS 0x040 /* type -f */ -#define CDESC_ABSPATH 0x080 /* convert to absolute path, no ./ */ - -/* Flags for get_job_by_name */ -#define JM_PREFIX 0x01 /* prefix of job name */ -#define JM_SUBSTRING 0x02 /* substring of job name */ -#define JM_EXACT 0x04 /* match job name exactly */ -#define JM_STOPPED 0x08 /* match stopped jobs only */ -#define JM_FIRSTMATCH 0x10 /* return first matching job */ - -/* Flags for remember_args and value of changed_dollar_vars */ -#define ARGS_NONE 0x0 -#define ARGS_INVOC 0x01 -#define ARGS_FUNC 0x02 -#define ARGS_SETBLTIN 0x04 - -/* Functions from common.c */ -extern void builtin_error __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2))); -extern void builtin_warning __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2))); -extern void builtin_usage __P((void)); -extern void no_args __P((WORD_LIST *)); -extern int no_options __P((WORD_LIST *)); - -/* common error message functions */ -extern void sh_needarg __P((char *)); -extern void sh_neednumarg __P((char *)); -extern void sh_notfound __P((char *)); -extern void sh_invalidopt __P((char *)); -extern void sh_invalidoptname __P((char *)); -extern void sh_invalidid __P((char *)); -extern void sh_invalidnum __P((char *)); -extern void sh_invalidsig __P((char *)); -extern void sh_erange __P((char *, char *)); -extern void sh_badpid __P((char *)); -extern void sh_badjob __P((char *)); -extern void sh_readonly __P((const char *)); -extern void sh_nojobs __P((char *)); -extern void sh_restricted __P((char *)); -extern void sh_notbuiltin __P((char *)); -extern void sh_wrerror __P((void)); -extern void sh_ttyerror __P((int)); -extern int sh_chkwrite __P((int)); - -extern char **make_builtin_argv __P((WORD_LIST *, int *)); -extern void remember_args __P((WORD_LIST *, int)); - -extern int dollar_vars_changed __P((void)); -extern void set_dollar_vars_unchanged __P((void)); -extern void set_dollar_vars_changed __P((void)); - -extern int get_numeric_arg __P((WORD_LIST *, int, intmax_t *)); -extern int get_exitstat __P((WORD_LIST *)); -extern int read_octal __P((char *)); - -/* Keeps track of the current working directory. */ -extern char *the_current_working_directory; -extern char *get_working_directory __P((char *)); -extern void set_working_directory __P((char *)); - -#if defined (JOB_CONTROL) -extern int get_job_by_name __P((const char *, int)); -extern int get_job_spec __P((WORD_LIST *)); -#endif -extern int display_signal_list __P((WORD_LIST *, int)); - -/* It's OK to declare a function as returning a Function * without - providing a definition of what a `Function' is. */ -extern struct builtin *builtin_address_internal __P((char *, int)); -extern sh_builtin_func_t *find_shell_builtin __P((char *)); -extern sh_builtin_func_t *builtin_address __P((char *)); -extern sh_builtin_func_t *find_special_builtin __P((char *)); -extern void initialize_shell_builtins __P((void)); - -/* Functions from exit.def */ -extern void bash_logout __P((void)); - -/* Functions from getopts.def */ -extern void getopts_reset __P((int)); - -/* Functions from set.def */ -extern int minus_o_option_value __P((char *)); -extern void list_minus_o_opts __P((int, int)); -extern char **get_minus_o_opts __P((void)); -extern int set_minus_o_option __P((int, char *)); - -extern void set_shellopts __P((void)); -extern void parse_shellopts __P((char *)); -extern void initialize_shell_options __P((int)); - -extern void reset_shell_options __P((void)); - -/* Functions from shopt.def */ -extern void reset_shopt_options __P((void)); -extern char **get_shopt_options __P((void)); - -extern int shopt_setopt __P((char *, int)); -extern int shopt_listopt __P((char *, int)); - -extern int set_login_shell __P((char *, int)); - -extern void set_bashopts __P((void)); -extern void parse_bashopts __P((char *)); -extern void initialize_bashopts __P((int)); - -/* Functions from type.def */ -extern int describe_command __P((char *, int)); - -/* Functions from setattr.def */ -extern int set_or_show_attributes __P((WORD_LIST *, int, int)); -extern int show_all_var_attributes __P((int, int)); -extern int show_var_attributes __P((SHELL_VAR *, int, int)); -extern int show_name_attributes __P((char *, int)); -extern void set_var_attribute __P((char *, int, int)); - -/* Functions from pushd.def */ -extern char *get_dirstack_from_string __P((char *)); -extern char *get_dirstack_element __P((intmax_t, int)); -extern void set_dirstack_element __P((intmax_t, int, char *)); -extern WORD_LIST *get_directory_stack __P((int)); - -/* Functions from evalstring.c */ -extern int parse_and_execute __P((char *, const char *, int)); -extern int evalstring __P((char *, const char *, int)); -extern void parse_and_execute_cleanup __P((void)); -extern int parse_string __P((char *, const char *, int, char **)); - -/* Functions from evalfile.c */ -extern int maybe_execute_file __P((const char *, int)); -extern int source_file __P((const char *, int)); -extern int fc_execute_file __P((const char *)); - -#endif /* !__COMMON_H */ diff --git a/builtins/declare.def~ b/builtins/declare.def~ deleted file mode 100644 index 345bd15f..00000000 --- a/builtins/declare.def~ +++ /dev/null @@ -1,694 +0,0 @@ -This file is declare.def, from which is created declare.c. -It implements the builtins "declare" and "local" in Bash. - -Copyright (C) 1987-2012 Free Software Foundation, Inc. - -This file is part of GNU Bash, the Bourne Again SHell. - -Bash is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -Bash is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with Bash. If not, see <http://www.gnu.org/licenses/>. - -$PRODUCES declare.c - -$BUILTIN declare -$FUNCTION declare_builtin -$SHORT_DOC declare [-aAfFgilnrtux] [-p] [name[=value] ...] -Set variable values and attributes. - -Declare variables and give them attributes. If no NAMEs are given, -display the attributes and values of all variables. - -Options: - -f restrict action or display to function names and definitions - -F restrict display to function names only (plus line number and - source file when debugging) - -g create global variables when used in a shell function; otherwise - ignored - -p display the attributes and value of each NAME - -Options which set attributes: - -a to make NAMEs indexed arrays (if supported) - -A to make NAMEs associative arrays (if supported) - -i to make NAMEs have the `integer' attribute - -l to convert NAMEs to lower case on assignment - -n make NAME a reference to the variable named by its value - -r to make NAMEs readonly - -t to make NAMEs have the `trace' attribute - -u to convert NAMEs to upper case on assignment - -x to make NAMEs export - -Using `+' instead of `-' turns off the given attribute. - -Variables with the integer attribute have arithmetic evaluation (see -the `let' command) performed when the variable is assigned a value. - -When used in a function, `declare' makes NAMEs local, as with the `local' -command. The `-g' option suppresses this behavior. - -Exit Status: -Returns success unless an invalid option is supplied or a variable -assignment error occurs. -$END - -$BUILTIN typeset -$FUNCTION declare_builtin -$SHORT_DOC typeset [-aAfFgilrtux] [-p] name[=value] ... -Set variable values and attributes. - -Obsolete. See `help declare'. -$END - -#include <config.h> - -#if defined (HAVE_UNISTD_H) -# ifdef _MINIX -# include <sys/types.h> -# endif -# include <unistd.h> -#endif - -#include <stdio.h> - -#include "../bashansi.h" -#include "../bashintl.h" - -#include "../shell.h" -#include "common.h" -#include "builtext.h" -#include "bashgetopt.h" - -extern int array_needs_making; -extern int posixly_correct; - -static int declare_internal __P((register WORD_LIST *, int)); - -/* Declare or change variable attributes. */ -int -declare_builtin (list) - register WORD_LIST *list; -{ - return (declare_internal (list, 0)); -} - -$BUILTIN local -$FUNCTION local_builtin -$SHORT_DOC local [option] name[=value] ... -Define local variables. - -Create a local variable called NAME, and give it VALUE. OPTION can -be any option accepted by `declare'. - -Local variables can only be used within a function; they are visible -only to the function where they are defined and its children. - -Exit Status: -Returns success unless an invalid option is supplied, a variable -assignment error occurs, or the shell is not executing a function. -$END -int -local_builtin (list) - register WORD_LIST *list; -{ - if (variable_context) - return (declare_internal (list, 1)); - else - { - builtin_error (_("can only be used in a function")); - return (EXECUTION_FAILURE); - } -} - -#if defined (ARRAY_VARS) -# define DECLARE_OPTS "+acfgilnprtuxAF" -#else -# define DECLARE_OPTS "+cfgilnprtuxF" -#endif - -/* The workhorse function. */ -static int -declare_internal (list, local_var) - register WORD_LIST *list; - int local_var; -{ - int flags_on, flags_off, *flags; - int any_failed, assign_error, pflag, nodefs, opt, mkglobal, onref, offref; - char *t, *subscript_start; - SHELL_VAR *var, *refvar; - FUNCTION_DEF *shell_fn; - - flags_on = flags_off = any_failed = assign_error = pflag = nodefs = mkglobal = 0; - refvar = (SHELL_VAR *)NULL; - reset_internal_getopt (); - while ((opt = internal_getopt (list, DECLARE_OPTS)) != EOF) - { - flags = list_opttype == '+' ? &flags_off : &flags_on; - - switch (opt) - { - case 'a': -#if defined (ARRAY_VARS) - *flags |= att_array; - break; -#else - builtin_usage (); - return (EX_USAGE); -#endif - case 'A': -#if defined (ARRAY_VARS) - *flags |= att_assoc; - break; -#else - builtin_usage (); - return (EX_USAGE); -#endif - case 'p': - if (local_var == 0) - pflag++; - break; - case 'F': - nodefs++; - *flags |= att_function; - break; - case 'f': - *flags |= att_function; - break; - case 'g': - if (flags == &flags_on) - mkglobal = 1; - break; - case 'i': - *flags |= att_integer; - break; - case 'n': - *flags |= att_nameref; - break; - case 'r': - *flags |= att_readonly; - break; - case 't': - *flags |= att_trace; - break; - case 'x': - *flags |= att_exported; - array_needs_making = 1; - break; -#if defined (CASEMOD_ATTRS) -# if defined (CASEMOD_CAPCASE) - case 'c': - *flags |= att_capcase; - if (flags == &flags_on) - flags_off |= att_uppercase|att_lowercase; - break; -# endif - case 'l': - *flags |= att_lowercase; - if (flags == &flags_on) - flags_off |= att_capcase|att_uppercase; - break; - case 'u': - *flags |= att_uppercase; - if (flags == &flags_on) - flags_off |= att_capcase|att_lowercase; - break; -#endif /* CASEMOD_ATTRS */ - default: - builtin_usage (); - return (EX_USAGE); - } - } - - list = loptend; - - /* If there are no more arguments left, then we just want to show - some variables. */ - if (list == 0) /* declare -[aAfFirtx] */ - { - /* Show local variables defined at this context level if this is - the `local' builtin. */ - if (local_var) - { - register SHELL_VAR **vlist; - register int i; - - vlist = all_local_variables (); - - if (vlist) - { - for (i = 0; vlist[i]; i++) - print_assignment (vlist[i]); - - free (vlist); - } - } - else if (pflag && (flags_on == 0 || flags_on == att_function)) - show_all_var_attributes (flags_on == 0, nodefs); - else if (flags_on == 0) - return (set_builtin ((WORD_LIST *)NULL)); - else - set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs); - - return (sh_chkwrite (EXECUTION_SUCCESS)); - } - - if (pflag) /* declare -p [-aAfFirtx] name [name...] */ - { - for (any_failed = 0; list; list = list->next) - { - pflag = show_name_attributes (list->word->word, nodefs); - if (pflag) - { - sh_notfound (list->word->word); - any_failed++; - } - } - return (sh_chkwrite (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS)); - } - -#define NEXT_VARIABLE() free (name); list = list->next; continue - - /* There are arguments left, so we are making variables. */ - while (list) /* declare [-aAfFirx] name [name ...] */ - { - char *value, *name; - int offset, aflags; -#if defined (ARRAY_VARS) - int making_array_special, compound_array_assign, simple_array_assign; -#endif - - name = savestring (list->word->word); - offset = assignment (name, 0); - aflags = 0; - - if (offset) /* declare [-aAfFirx] name=value */ - { - name[offset] = '\0'; - value = name + offset + 1; - if (name[offset - 1] == '+') - { - aflags |= ASS_APPEND; - name[offset - 1] = '\0'; - } - } - else - value = ""; - - /* Do some lexical error checking on the LHS and RHS of the assignment - that is specific to nameref variables. */ - if (flags_on & att_nameref) - { - if (valid_array_reference (name)) - { - builtin_error (_("%s: reference variable cannot be an array"), name); - assign_error++; - NEXT_VARIABLE (); - } - else if (STREQ (name, value)) - { - builtin_error (_("%s: nameref variable self references not allowed"), name); - assign_error++; - NEXT_VARIABLE (); - } - } - -#if defined (ARRAY_VARS) - compound_array_assign = simple_array_assign = 0; - subscript_start = (char *)NULL; - if (t = strchr (name, '[')) /* ] */ - { - /* If offset != 0 we have already validated any array reference */ - if (offset == 0 && valid_array_reference (name) == 0) - { - sh_invalidid (name); - assign_error++; - NEXT_VARIABLE (); - } - subscript_start = t; - *t = '\0'; - making_array_special = 1; - } - else - making_array_special = 0; -#endif - - /* If we're in posix mode or not looking for a shell function (since - shell function names don't have to be valid identifiers when the - shell's not in posix mode), check whether or not the argument is a - valid, well-formed shell identifier. */ - if ((posixly_correct || (flags_on & att_function) == 0) && legal_identifier (name) == 0) - { - sh_invalidid (name); - assign_error++; - NEXT_VARIABLE (); - } - - /* If VARIABLE_CONTEXT has a non-zero value, then we are executing - inside of a function. This means we should make local variables, - not global ones. */ - - /* XXX - this has consequences when we're making a local copy of a - variable that was in the temporary environment. Watch out - for this. */ - refvar = (SHELL_VAR *)NULL; - if (variable_context && mkglobal == 0 && ((flags_on & att_function) == 0)) - { -#if defined (ARRAY_VARS) - if (flags_on & att_assoc) - var = make_local_assoc_variable (name); - else if ((flags_on & att_array) || making_array_special) - var = make_local_array_variable (name, making_array_special); - else -#endif -#if 0 - /* XXX - this doesn't work right yet. */ - /* See below for rationale for doing this. */ - if (flags_on & att_nameref) - { - /* See if we are trying to modify an existing nameref variable */ - var = find_variable_last_nameref (name); - if (var && nameref_p (var) == 0) - var = make_local_variable (name); - } - else if (flags_off & att_nameref) - { - var = (SHELL_VAR *)NULL; - /* See if we are trying to modify an existing nameref variable */ - refvar = find_variable_last_nameref (name); - if (refvar && nameref_p (refvar) == 0) - refvar = 0; - if (refvar) - var = make_local_variable (nameref_cell (refvar)); - if (var == 0) - var = make_local_variable (name); - } - else -#endif - var = make_local_variable (name); - if (var == 0) - { - any_failed++; - NEXT_VARIABLE (); - } - } - else - var = (SHELL_VAR *)NULL; - - /* If we are declaring a function, then complain about it in some way. - We don't let people make functions by saying `typeset -f foo=bar'. */ - - /* There should be a way, however, to let people look at a particular - function definition by saying `typeset -f foo'. */ - - if (flags_on & att_function) - { - if (offset) /* declare -f [-rix] foo=bar */ - { - builtin_error (_("cannot use `-f' to make functions")); - free (name); - return (EXECUTION_FAILURE); - } - else /* declare -f [-rx] name [name...] */ - { - var = find_function (name); - - if (var) - { - if (readonly_p (var) && (flags_off & att_readonly)) - { - builtin_error (_("%s: readonly function"), name); - any_failed++; - NEXT_VARIABLE (); - } - - /* declare -[Ff] name [name...] */ - if (flags_on == att_function && flags_off == 0) - { -#if defined (DEBUGGER) - if (nodefs && debugging_mode) - { - shell_fn = find_function_def (var->name); - if (shell_fn) - printf ("%s %d %s\n", var->name, shell_fn->line, shell_fn->source_file); - else - printf ("%s\n", var->name); - } - else -#endif /* DEBUGGER */ - { - t = nodefs ? var->name - : named_function_string (name, function_cell (var), FUNC_MULTILINE|FUNC_EXTERNAL); - printf ("%s\n", t); - any_failed = sh_chkwrite (any_failed); - } - } - else /* declare -[fF] -[rx] name [name...] */ - { - VSETATTR (var, flags_on); - VUNSETATTR (var, flags_off); - } - } - else - any_failed++; - NEXT_VARIABLE (); - } - } - else /* declare -[aAirx] name [name...] */ - { - /* Non-null if we just created or fetched a local variable. */ - /* Here's what ksh93 seems to do. If we are modifying an existing - nameref variable, we don't follow the nameref chain past the last - nameref, and we set the nameref variable's value so future - references to that variable will return the value of the variable - we're assigning right now. */ - if (var == 0 && (flags_on & att_nameref)) - { - /* See if we are trying to modify an existing nameref variable */ - var = mkglobal ? find_global_variable_last_nameref (name) : find_variable_last_nameref (name); - if (var && nameref_p (var) == 0) - var = 0; - } - /* However, if we're turning off the nameref attribute on an existing - nameref variable, we first follow the nameref chain to the end, - modify the value of the variable this nameref variable references, - *CHANGING ITS VALUE AS A SIDE EFFECT* then turn off the nameref - flag *LEAVING THE NAMEREF VARIABLE'S VALUE UNCHANGED* */ - else if (var == 0 && (flags_off & att_nameref)) - { - /* See if we are trying to modify an existing nameref variable */ - refvar = mkglobal ? find_global_variable_last_nameref (name) : find_variable_last_nameref (name); - if (refvar && nameref_p (refvar) == 0) - refvar = 0; - if (refvar) - var = mkglobal ? find_global_variable (nameref_cell (refvar)) : find_variable (nameref_cell (refvar)); - } - - if (var == 0) - var = mkglobal ? find_global_variable (name) : find_variable (name); - - if (var == 0) - { -#if defined (ARRAY_VARS) - if (flags_on & att_assoc) - { - var = make_new_assoc_variable (name); - if (offset == 0) - VSETATTR (var, att_invisible); - } - else if ((flags_on & att_array) || making_array_special) - { - var = make_new_array_variable (name); - if (offset == 0) - VSETATTR (var, att_invisible); - } - else -#endif - - if (offset) - var = bind_variable (name, "", 0); - else - { - var = bind_variable (name, (char *)NULL, 0); - VSETATTR (var, att_invisible); - } - } - /* Can't take an existing array variable and make it a nameref */ - else if ((array_p (var) || assoc_p (var)) && (flags_on & att_nameref)) - { - builtin_error (_("%s: reference variable cannot be an array"), name); - assign_error++; - NEXT_VARIABLE (); - } - - /* Cannot use declare +r to turn off readonly attribute. */ - if (readonly_p (var) && (flags_off & att_readonly)) - { - sh_readonly (name); - any_failed++; - NEXT_VARIABLE (); - } - - /* Cannot use declare to assign value to readonly or noassign - variable. */ - if ((readonly_p (var) || noassign_p (var)) && offset) - { - if (readonly_p (var)) - sh_readonly (name); - assign_error++; - NEXT_VARIABLE (); - } - -#if defined (ARRAY_VARS) - if ((making_array_special || (flags_on & (att_array|att_assoc)) || array_p (var) || assoc_p (var)) && offset) - { - int vlen; - vlen = STRLEN (value); - - if (value[0] == '(' && value[vlen-1] == ')') - compound_array_assign = 1; - else - simple_array_assign = 1; - } - - /* Cannot use declare +a name or declare +A name to remove an - array variable. */ - if (((flags_off & att_array) && array_p (var)) || ((flags_off & att_assoc) && assoc_p (var))) - { - builtin_error (_("%s: cannot destroy array variables in this way"), name); - any_failed++; - NEXT_VARIABLE (); - } - - if ((flags_on & att_array) && assoc_p (var)) - { - builtin_error (_("%s: cannot convert associative to indexed array"), name); - any_failed++; - NEXT_VARIABLE (); - } - if ((flags_on & att_assoc) && array_p (var)) - { - builtin_error (_("%s: cannot convert indexed to associative array"), name); - any_failed++; - NEXT_VARIABLE (); - } - - /* declare -A name[[n]] makes name an associative array variable. */ - if (flags_on & att_assoc) - { - if (assoc_p (var) == 0) - var = convert_var_to_assoc (var); - } - /* declare -a name[[n]] or declare name[n] makes name an indexed - array variable. */ - else if ((making_array_special || (flags_on & att_array)) && array_p (var) == 0 && assoc_p (var) == 0) - var = convert_var_to_array (var); -#endif /* ARRAY_VARS */ - - /* XXX - we note that we are turning on nameref attribute and defer - setting it until the assignment has been made so we don't do an - inadvertent nameref lookup. Might have to do the same thing for - flags_off&att_nameref. */ - /* XXX - ksh93 makes it an error to set a readonly nameref variable - using a single typeset command. */ - onref = (flags_on & att_nameref); - flags_on &= ~att_nameref; - if (array_p (var) || assoc_p (var) - || (offset && compound_array_assign) - || simple_array_assign) - onref = 0; /* array variables may not be namerefs */ - - /* ksh93 seems to do this */ - offref = (flags_off & att_nameref); - flags_off &= ~att_nameref; - - VSETATTR (var, flags_on); - VUNSETATTR (var, flags_off); - -#if defined (ARRAY_VARS) - if (offset && compound_array_assign) - assign_array_var_from_string (var, value, aflags); - else if (simple_array_assign && subscript_start) - { - /* declare [-aA] name[N]=value */ - *subscript_start = '['; /* ] */ - var = assign_array_element (name, value, 0); /* XXX - not aflags */ - *subscript_start = '\0'; - if (var == 0) /* some kind of assignment error */ - { - assign_error++; - flags_on |= onref; - flags_off |= offref; - NEXT_VARIABLE (); - } - } - else if (simple_array_assign) - { - /* let bind_{array,assoc}_variable take care of this. */ - if (assoc_p (var)) - bind_assoc_variable (var, name, savestring ("0"), value, aflags); - else - bind_array_variable (name, 0, value, aflags); - } - else -#endif - /* bind_variable_value duplicates the essential internals of - bind_variable() */ - if (offset) - bind_variable_value (var, value, aflags); - - /* If we found this variable in the temporary environment, as with - `var=value declare -x var', make sure it is treated identically - to `var=value export var'. Do the same for `declare -r' and - `readonly'. Preserve the attributes, except for att_tempvar. */ - /* XXX -- should this create a variable in the global scope, or - modify the local variable flags? ksh93 has it modify the - global scope. - Need to handle case like in set_var_attribute where a temporary - variable is in the same table as the function local vars. */ - if ((flags_on & (att_exported|att_readonly)) && tempvar_p (var)) - { - SHELL_VAR *tv; - char *tvalue; - - tv = find_tempenv_variable (var->name); - if (tv) - { - tvalue = var_isset (var) ? savestring (value_cell (var)) : savestring (""); - tv = bind_variable (var->name, tvalue, 0); - tv->attributes |= var->attributes & ~att_tempvar; - if (tv->context > 0) - VSETATTR (tv, att_propagate); - free (tvalue); - } - VSETATTR (var, att_propagate); - } - } - - /* Turn on nameref attribute we deferred above. */ - /* XXX - should we turn on the noassign attribute for consistency with - ksh93 when we turn on the nameref attribute? */ - VSETATTR (var, onref); - flags_on |= onref; - VUNSETATTR (var, offref); - flags_off |= offref; - /* Yuck. ksh93 compatibility */ - if (refvar) - VUNSETATTR (refvar, flags_off); - - stupidly_hack_special_variables (name); - - NEXT_VARIABLE (); - } - - return (assign_error ? EX_BADASSIGN - : ((any_failed == 0) ? EXECUTION_SUCCESS - : EXECUTION_FAILURE)); -} diff --git a/builtins/evalfile.c~ b/builtins/evalfile.c~ deleted file mode 100644 index 4d69acb1..00000000 --- a/builtins/evalfile.c~ +++ /dev/null @@ -1,348 +0,0 @@ -/* evalfile.c - read and evaluate commands from a file or file descriptor */ - -/* Copyright (C) 1996-2009 Free Software Foundation, Inc. - - This file is part of GNU Bash, the Bourne Again SHell. - - Bash is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Bash is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Bash. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include <config.h> - -#if defined (HAVE_UNISTD_H) -# include <unistd.h> -#endif - -#include "../bashtypes.h" -#include "posixstat.h" -#include "filecntl.h" - -#include <stdio.h> -#include <signal.h> -#include <errno.h> - -#include "../bashansi.h" -#include "../bashintl.h" - -#include "../shell.h" -#include "../jobs.h" -#include "../builtins.h" -#include "../flags.h" -#include "../input.h" -#include "../execute_cmd.h" -#include "../trap.h" - -#if defined (HISTORY) -# include "../bashhist.h" -#endif - -#include <typemax.h> - -#include "common.h" - -#if !defined (errno) -extern int errno; -#endif - -/* Flags for _evalfile() */ -#define FEVAL_ENOENTOK 0x001 -#define FEVAL_BUILTIN 0x002 -#define FEVAL_UNWINDPROT 0x004 -#define FEVAL_NONINT 0x008 -#define FEVAL_LONGJMP 0x010 -#define FEVAL_HISTORY 0x020 -#define FEVAL_CHECKBINARY 0x040 -#define FEVAL_REGFILE 0x080 -#define FEVAL_NOPUSHARGS 0x100 - -extern int posixly_correct; -extern int indirection_level, subshell_environment; -extern int return_catch_flag, return_catch_value; -extern int last_command_exit_value; -extern int executing_command_builtin; - -/* How many `levels' of sourced files we have. */ -int sourcelevel = 0; - -static int -_evalfile (filename, flags) - const char *filename; - int flags; -{ - volatile int old_interactive; - procenv_t old_return_catch; - int return_val, fd, result, pflags, i, nnull; - ssize_t nr; /* return value from read(2) */ - char *string; - struct stat finfo; - size_t file_size; - sh_vmsg_func_t *errfunc; -#if defined (ARRAY_VARS) - SHELL_VAR *funcname_v, *nfv, *bash_source_v, *bash_lineno_v; - ARRAY *funcname_a, *bash_source_a, *bash_lineno_a; -# if defined (DEBUGGER) - SHELL_VAR *bash_argv_v, *bash_argc_v; - ARRAY *bash_argv_a, *bash_argc_a; -# endif - char *t, tt[2]; -#endif - - USE_VAR(pflags); - -#if defined (ARRAY_VARS) - GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a); - GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a); - GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a); -# if defined (DEBUGGER) - GET_ARRAY_FROM_VAR ("BASH_ARGV", bash_argv_v, bash_argv_a); - GET_ARRAY_FROM_VAR ("BASH_ARGC", bash_argc_v, bash_argc_a); -# endif -#endif - - fd = open (filename, O_RDONLY); - - if (fd < 0 || (fstat (fd, &finfo) == -1)) - { -file_error_and_exit: - if (((flags & FEVAL_ENOENTOK) == 0) || errno != ENOENT) - file_error (filename); - - if (flags & FEVAL_LONGJMP) - { - last_command_exit_value = 1; - jump_to_top_level (EXITPROG); - } - - return ((flags & FEVAL_BUILTIN) ? EXECUTION_FAILURE - : ((errno == ENOENT) ? 0 : -1)); - } - - errfunc = ((flags & FEVAL_BUILTIN) ? builtin_error : internal_error); - - if (S_ISDIR (finfo.st_mode)) - { - (*errfunc) (_("%s: is a directory"), filename); - return ((flags & FEVAL_BUILTIN) ? EXECUTION_FAILURE : -1); - } - else if ((flags & FEVAL_REGFILE) && S_ISREG (finfo.st_mode) == 0) - { - (*errfunc) (_("%s: not a regular file"), filename); - return ((flags & FEVAL_BUILTIN) ? EXECUTION_FAILURE : -1); - } - - file_size = (size_t)finfo.st_size; - /* Check for overflow with large files. */ - if (file_size != finfo.st_size || file_size + 1 < file_size) - { - (*errfunc) (_("%s: file is too large"), filename); - return ((flags & FEVAL_BUILTIN) ? EXECUTION_FAILURE : -1); - } - - if (S_ISREG (finfo.st_mode) && file_size <= SSIZE_MAX) - { - string = (char *)xmalloc (1 + file_size); - nr = read (fd, string, file_size); - if (nr >= 0) - string[nr] = '\0'; - } - else - nr = zmapfd (fd, &string, 0); - - return_val = errno; - close (fd); - errno = return_val; - - if (nr < 0) /* XXX was != file_size, not < 0 */ - { - free (string); - goto file_error_and_exit; - } - - if (nr == 0) - { - free (string); - return ((flags & FEVAL_BUILTIN) ? EXECUTION_SUCCESS : 1); - } - - if ((flags & FEVAL_CHECKBINARY) && - check_binary_file (string, (nr > 80) ? 80 : nr)) - { - free (string); - (*errfunc) (_("%s: cannot execute binary file"), filename); - return ((flags & FEVAL_BUILTIN) ? EX_BINARY_FILE : -1); - } - - i = strlen (string); - if (i < nr) - { - for (nnull = i = 0; i < nr; i++) - if (string[i] == '\0') - { - memmove (string+i, string+i+1, nr - i); - nr--; - /* Even if the `check binary' flag is not set, we want to avoid - sourcing files with more than 256 null characters -- that - probably indicates a binary file. */ - if ((flags & FEVAL_BUILTIN) && ++nnull > 256) - { - free (string); - (*errfunc) (_("%s: cannot execute binary file"), filename); - return ((flags & FEVAL_BUILTIN) ? EX_BINARY_FILE : -1); - } - } - } - - if (flags & FEVAL_UNWINDPROT) - { - begin_unwind_frame ("_evalfile"); - - unwind_protect_int (return_catch_flag); - unwind_protect_jmp_buf (return_catch); - if (flags & FEVAL_NONINT) - unwind_protect_int (interactive); - unwind_protect_int (sourcelevel); - } - else - { - COPY_PROCENV (return_catch, old_return_catch); - if (flags & FEVAL_NONINT) - old_interactive = interactive; - } - - if (flags & FEVAL_NONINT) - interactive = 0; - - return_catch_flag++; - sourcelevel++; - -#if defined (ARRAY_VARS) - array_push (bash_source_a, (char *)filename); - t = itos (executing_line_number ()); - array_push (bash_lineno_a, t); - free (t); - array_push (funcname_a, "source"); /* not exactly right */ -# if defined (DEBUGGER) - /* Have to figure out a better way to do this when `source' is supplied - arguments */ - if ((flags & FEVAL_NOPUSHARGS) == 0) - { - array_push (bash_argv_a, (char *)filename); - tt[0] = '1'; tt[1] = '\0'; - array_push (bash_argc_a, tt); - } -# endif -#endif - - /* set the flags to be passed to parse_and_execute */ - pflags = SEVAL_RESETLINE; - pflags |= (flags & FEVAL_HISTORY) ? 0 : SEVAL_NOHIST; - - if (flags & FEVAL_BUILTIN) - result = EXECUTION_SUCCESS; - - return_val = setjmp (return_catch); - - /* If `return' was seen outside of a function, but in the script, then - force parse_and_execute () to clean up. */ - if (return_val) - { - parse_and_execute_cleanup (); - result = return_catch_value; - } - else - result = parse_and_execute (string, filename, pflags); - - if (flags & FEVAL_UNWINDPROT) - run_unwind_frame ("_evalfile"); - else - { - if (flags & FEVAL_NONINT) - interactive = old_interactive; - return_catch_flag--; - sourcelevel--; - COPY_PROCENV (old_return_catch, return_catch); - } - -#if defined (ARRAY_VARS) - /* These two variables cannot be unset, and cannot be affected by the - sourced file. */ - array_pop (bash_source_a); - array_pop (bash_lineno_a); - - /* FUNCNAME can be unset, and so can potentially be changed by the - sourced file. */ - GET_ARRAY_FROM_VAR ("FUNCNAME", nfv, funcname_a); - if (nfv == funcname_v) - array_pop (funcname_a); -# if defined (DEBUGGER) - if ((flags & FEVAL_NOPUSHARGS) == 0) - { - array_pop (bash_argc_a); - array_pop (bash_argv_a); - } -# endif -#endif - - return ((flags & FEVAL_BUILTIN) ? result : 1); -} - -int -maybe_execute_file (fname, force_noninteractive) - const char *fname; - int force_noninteractive; -{ - char *filename; - int result, flags; - - filename = bash_tilde_expand (fname, 0); - flags = FEVAL_ENOENTOK; - if (force_noninteractive) - flags |= FEVAL_NONINT; - result = _evalfile (filename, flags); - free (filename); - return result; -} - -#if defined (HISTORY) -int -fc_execute_file (filename) - const char *filename; -{ - int flags; - - /* We want these commands to show up in the history list if - remember_on_history is set. */ - flags = FEVAL_ENOENTOK|FEVAL_HISTORY|FEVAL_REGFILE; - return (_evalfile (filename, flags)); -} -#endif /* HISTORY */ - -int -source_file (filename, sflags) - const char *filename; - int sflags; -{ - int flags, rval; - - flags = FEVAL_BUILTIN|FEVAL_UNWINDPROT|FEVAL_NONINT; - if (sflags) - flags |= FEVAL_NOPUSHARGS; - /* POSIX shells exit if non-interactive and file error. */ - if (posixly_correct && interactive_shell == 0 && executing_command_builtin == 0) - flags |= FEVAL_LONGJMP; - rval = _evalfile (filename, flags); - - run_return_trap (); - return rval; -} diff --git a/builtins/evalstring.c~ b/builtins/evalstring.c~ deleted file mode 100644 index a78688cc..00000000 --- a/builtins/evalstring.c~ +++ /dev/null @@ -1,604 +0,0 @@ -/* evalstring.c - evaluate a string as one or more shell commands. */ - -/* Copyright (C) 1996-2012 Free Software Foundation, Inc. - - This file is part of GNU Bash, the Bourne Again SHell. - - Bash is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Bash is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Bash. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include <config.h> - -#if defined (HAVE_UNISTD_H) -# ifdef _MINIX -# include <sys/types.h> -# endif -# include <unistd.h> -#endif - -#include <stdio.h> -#include <signal.h> - -#include <errno.h> - -#include "filecntl.h" -#include "../bashansi.h" - -#include "../shell.h" -#include "../jobs.h" -#include "../builtins.h" -#include "../flags.h" -#include "../input.h" -#include "../execute_cmd.h" -#include "../redir.h" -#include "../trap.h" -#include "../bashintl.h" - -#include <y.tab.h> - -#if defined (HISTORY) -# include "../bashhist.h" -#endif - -#include "common.h" -#include "builtext.h" - -#if !defined (errno) -extern int errno; -#endif - -#define IS_BUILTIN(s) (builtin_address_internal(s, 0) != (struct builtin *)NULL) - -extern int indirection_level, subshell_environment; -extern int line_number, line_number_for_err_trap; -extern int current_token, shell_eof_token; -extern int last_command_exit_value; -extern int running_trap; -extern int loop_level; -extern int executing_list; -extern int comsub_ignore_return; -extern int posixly_correct; -extern int return_catch_flag, return_catch_value; -extern sh_builtin_func_t *this_shell_builtin; -extern char *the_printed_command_except_trap; - -int parse_and_execute_level = 0; - -static int cat_file __P((REDIRECT *)); - -#define PE_TAG "parse_and_execute top" -#define PS_TAG "parse_string top" - -#if defined (HISTORY) -static void -set_history_remembering () -{ - remember_on_history = enable_history_list; -} -#endif - -static void -restore_lastcom (x) - char *x; -{ - FREE (the_printed_command_except_trap); - the_printed_command_except_trap = x; -} - -/* How to force parse_and_execute () to clean up after itself. */ -void -parse_and_execute_cleanup () -{ - if (running_trap) - { - run_trap_cleanup (running_trap - 1); - unfreeze_jobs_list (); - } - - if (have_unwind_protects ()) - run_unwind_frame (PE_TAG); - else - parse_and_execute_level = 0; /* XXX */ -} - -static void -parse_prologue (string, flags, tag) - char *string; - int flags; - char *tag; -{ - char *orig_string, *lastcom; - int x; - - orig_string = string; - /* Unwind protect this invocation of parse_and_execute (). */ - begin_unwind_frame (tag); - unwind_protect_int (parse_and_execute_level); - unwind_protect_jmp_buf (top_level); - unwind_protect_int (indirection_level); - unwind_protect_int (line_number); - unwind_protect_int (line_number_for_err_trap); - unwind_protect_int (loop_level); - unwind_protect_int (executing_list); - unwind_protect_int (comsub_ignore_return); - if (flags & (SEVAL_NONINT|SEVAL_INTERACT)) - unwind_protect_int (interactive); - -#if defined (HISTORY) - if (parse_and_execute_level == 0) - add_unwind_protect (set_history_remembering, (char *)NULL); - else - unwind_protect_int (remember_on_history); /* can be used in scripts */ -# if defined (BANG_HISTORY) - if (interactive_shell) - unwind_protect_int (history_expansion_inhibited); -# endif /* BANG_HISTORY */ -#endif /* HISTORY */ - - if (interactive_shell) - { - x = get_current_prompt_level (); - add_unwind_protect (set_current_prompt_level, x); - } - - if (the_printed_command_except_trap) - { - lastcom = savestring (the_printed_command_except_trap); - add_unwind_protect (restore_lastcom, lastcom); - } - - add_unwind_protect (pop_stream, (char *)NULL); - if (orig_string && ((flags & SEVAL_NOFREE) == 0)) - add_unwind_protect (xfree, orig_string); - end_unwind_frame (); - - if (flags & (SEVAL_NONINT|SEVAL_INTERACT)) - interactive = (flags & SEVAL_NONINT) ? 0 : 1; - -#if defined (HISTORY) - if (flags & SEVAL_NOHIST) - bash_history_disable (); -#endif /* HISTORY */ -} - -/* Parse and execute the commands in STRING. Returns whatever - execute_command () returns. This frees STRING. FLAGS is a - flags word; look in common.h for the possible values. Actions - are: - (flags & SEVAL_NONINT) -> interactive = 0; - (flags & SEVAL_INTERACT) -> interactive = 1; - (flags & SEVAL_NOHIST) -> call bash_history_disable () - (flags & SEVAL_NOFREE) -> don't free STRING when finished - (flags & SEVAL_RESETLINE) -> reset line_number to 1 -*/ - -int -parse_and_execute (string, from_file, flags) - char *string; - const char *from_file; - int flags; -{ - int code, lreset; - volatile int should_jump_to_top_level, last_result; - COMMAND *volatile command; - volatile sigset_t pe_sigmask; - - parse_prologue (string, flags, PE_TAG); - - parse_and_execute_level++; - - lreset = flags & SEVAL_RESETLINE; - -#if defined (HAVE_POSIX_SIGNALS) - /* If we longjmp and are going to go on, use this to restore signal mask */ - sigemptyset (&pe_sigmask); - sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &pe_sigmask); -#endif - - /* Reset the line number if the caller wants us to. If we don't reset the - line number, we have to subtract one, because we will add one just - before executing the next command (resetting the line number sets it to - 0; the first line number is 1). */ - push_stream (lreset); - if (lreset == 0) - line_number--; - - indirection_level++; - - code = should_jump_to_top_level = 0; - last_result = EXECUTION_SUCCESS; - - with_input_from_string (string, from_file); - while (*(bash_input.location.string)) - { - command = (COMMAND *)NULL; - - if (interrupt_state) - { - last_result = EXECUTION_FAILURE; - break; - } - - /* Provide a location for functions which `longjmp (top_level)' to - jump to. This prevents errors in substitution from restarting - the reader loop directly, for example. */ - code = setjmp_nosigs (top_level); - - if (code) - { - should_jump_to_top_level = 0; - switch (code) - { - case ERREXIT: - /* variable_context -> 0 is what eval.c:reader_loop() does in - these circumstances. Don't bother with cleanup here because - we don't want to run the function execution cleanup stuff - that will cause pop_context and other functions to run. - XXX - change that if we want the function context to be - unwound. */ - if (exit_immediately_on_error && variable_context) - { - discard_unwind_frame ("pe_dispose"); - variable_context = 0; /* not in a function */ - } - should_jump_to_top_level = 1; - goto out; - case FORCE_EOF: - case EXITPROG: - if (command) - run_unwind_frame ("pe_dispose"); - /* Remember to call longjmp (top_level) after the old - value for it is restored. */ - should_jump_to_top_level = 1; - goto out; - - case DISCARD: - if (command) - run_unwind_frame ("pe_dispose"); - last_result = last_command_exit_value = EXECUTION_FAILURE; /* XXX */ - if (subshell_environment) - { - should_jump_to_top_level = 1; - goto out; - } - else - { -#if 0 - dispose_command (command); /* pe_dispose does this */ -#endif -#if defined (HAVE_POSIX_SIGNALS) - sigprocmask (SIG_SETMASK, &pe_sigmask, (sigset_t *)NULL); -#endif - continue; - } - - default: - command_error ("parse_and_execute", CMDERR_BADJUMP, code, 0); - break; - } - } - - if (parse_command () == 0) - { - if ((flags & SEVAL_PARSEONLY) || (interactive_shell == 0 && read_but_dont_execute)) - { - last_result = EXECUTION_SUCCESS; - dispose_command (global_command); - global_command = (COMMAND *)NULL; - } - else if (command = global_command) - { - struct fd_bitmap *bitmap; - - bitmap = new_fd_bitmap (FD_BITMAP_SIZE); - begin_unwind_frame ("pe_dispose"); - add_unwind_protect (dispose_fd_bitmap, bitmap); - add_unwind_protect (dispose_command, command); /* XXX */ - - global_command = (COMMAND *)NULL; - - if ((subshell_environment & SUBSHELL_COMSUB) && comsub_ignore_return) - command->flags |= CMD_IGNORE_RETURN; - -#if defined (ONESHOT) - /* - * IF - * we were invoked as `bash -c' (startup_state == 2) AND - * parse_and_execute has not been called recursively AND - * we're not running a trap AND - * we have parsed the full command (string == '\0') AND - * we're not going to run the exit trap AND - * we have a simple command without redirections AND - * the command is not being timed AND - * the command's return status is not being inverted - * THEN - * tell the execution code that we don't need to fork - */ - if (startup_state == 2 && parse_and_execute_level == 1 && - running_trap == 0 && - *bash_input.location.string == '\0' && - command->type == cm_simple && - signal_is_trapped (EXIT_TRAP) == 0 && - command->redirects == 0 && command->value.Simple->redirects == 0 && - ((command->flags & CMD_TIME_PIPELINE) == 0) && - ((command->flags & CMD_INVERT_RETURN) == 0)) - { - command->flags |= CMD_NO_FORK; - command->value.Simple->flags |= CMD_NO_FORK; - } -#endif /* ONESHOT */ - - /* See if this is a candidate for $( <file ). */ - if (startup_state == 2 && - (subshell_environment & SUBSHELL_COMSUB) && - *bash_input.location.string == '\0' && - command->type == cm_simple && !command->redirects && - (command->flags & CMD_TIME_PIPELINE) == 0 && - command->value.Simple->words == 0 && - command->value.Simple->redirects && - command->value.Simple->redirects->next == 0 && - command->value.Simple->redirects->instruction == r_input_direction && - command->value.Simple->redirects->redirector.dest == 0) - { - int r; - r = cat_file (command->value.Simple->redirects); - last_result = (r < 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS; - } - else - last_result = execute_command_internal - (command, 0, NO_PIPE, NO_PIPE, bitmap); - dispose_command (command); - dispose_fd_bitmap (bitmap); - discard_unwind_frame ("pe_dispose"); - } - } - else - { - last_result = EXECUTION_FAILURE; - - if (interactive_shell == 0 && this_shell_builtin && - (this_shell_builtin == source_builtin || this_shell_builtin == eval_builtin) && - last_command_exit_value == EX_BADSYNTAX && posixly_correct) - { - should_jump_to_top_level = 1; - code = ERREXIT; - last_command_exit_value = EX_BADUSAGE; - } - - /* Since we are shell compatible, syntax errors in a script - abort the execution of the script. Right? */ - break; - } - } - - out: - - run_unwind_frame (PE_TAG); - - if (interrupt_state && parse_and_execute_level == 0) - { - /* An interrupt during non-interactive execution in an - interactive shell (e.g. via $PROMPT_COMMAND) should - not cause the shell to exit. */ - interactive = interactive_shell; - throw_to_top_level (); - } - - if (should_jump_to_top_level) - jump_to_top_level (code); - - return (last_result); -} - -/* Parse a command contained in STRING according to FLAGS and return the - number of characters consumed from the string. If non-NULL, set *ENDP - to the position in the string where the parse ended. Used to validate - command substitutions during parsing to obey Posix rules about finding - the end of the command and balancing parens. */ -int -parse_string (string, from_file, flags, endp) - char *string; - const char *from_file; - int flags; - char **endp; -{ - int code, nc; - volatile int should_jump_to_top_level; - COMMAND *volatile command, *oglobal; - char *ostring; - volatile sigset_t ps_sigmask; - - parse_prologue (string, flags, PS_TAG); - -#if defined (HAVE_POSIX_SIGNALS) - /* If we longjmp and are going to go on, use this to restore signal mask */ - sigemptyset (&ps_sigmask); - sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &ps_sigmask); -#endif - - /* Reset the line number if the caller wants us to. If we don't reset the - line number, we have to subtract one, because we will add one just - before executing the next command (resetting the line number sets it to - 0; the first line number is 1). */ - push_stream (0); - - code = should_jump_to_top_level = 0; - oglobal = global_command; - ostring = string; - - with_input_from_string (string, from_file); - while (*(bash_input.location.string)) - { - command = (COMMAND *)NULL; - -#if 0 - if (interrupt_state) - break; -#endif - - /* Provide a location for functions which `longjmp (top_level)' to - jump to. */ - code = setjmp_nosigs (top_level); - - if (code) - { -#if defined (DEBUG) -itrace("parse_string: longjmp executed: code = %d", code); -#endif - should_jump_to_top_level = 0; - switch (code) - { - case FORCE_EOF: - case ERREXIT: - case EXITPROG: - case DISCARD: /* XXX */ - if (command) - dispose_command (command); - /* Remember to call longjmp (top_level) after the old - value for it is restored. */ - should_jump_to_top_level = 1; - goto out; - - default: -#if defined (HAVE_POSIX_SIGNALS) - sigprocmask (SIG_SETMASK, &ps_sigmask, (sigset_t *)NULL); -#endif - command_error ("parse_string", CMDERR_BADJUMP, code, 0); - break; - } - } - - if (parse_command () == 0) - { - dispose_command (global_command); - global_command = (COMMAND *)NULL; - } - else - { - if ((flags & SEVAL_NOLONGJMP) == 0) - { - should_jump_to_top_level = 1; - code = DISCARD; - } - else - reset_parser (); /* XXX - sets token_to_read */ - break; - } - - if (current_token == yacc_EOF || current_token == shell_eof_token) - break; - } - - out: - - global_command = oglobal; - nc = bash_input.location.string - ostring; - if (endp) - *endp = bash_input.location.string; - - run_unwind_frame (PS_TAG); - - if (should_jump_to_top_level) - jump_to_top_level (code); - - return (nc); -} - -/* Handle a $( < file ) command substitution. This expands the filename, - returning errors as appropriate, then just cats the file to the standard - output. */ -static int -cat_file (r) - REDIRECT *r; -{ - char *fn; - int fd, rval; - - if (r->instruction != r_input_direction) - return -1; - - /* Get the filename. */ - if (posixly_correct && !interactive_shell) - disallow_filename_globbing++; - fn = redirection_expand (r->redirectee.filename); - if (posixly_correct && !interactive_shell) - disallow_filename_globbing--; - - if (fn == 0) - { - redirection_error (r, AMBIGUOUS_REDIRECT); - return -1; - } - - fd = open(fn, O_RDONLY); - if (fd < 0) - { - file_error (fn); - free (fn); - return -1; - } - - rval = zcatfd (fd, 1, fn); - - free (fn); - close (fd); - - return (rval); -} - -int -evalstring (string, from_file, flags) - char *string; - const char *from_file; - int flags; -{ - volatile int r, rflag, rcatch; - - rcatch = 0; - rflag = return_catch_flag; - /* If we are in a place where `return' is valid, we have to catch - `eval "... return"' and make sure parse_and_execute cleans up. Then - we can trampoline to the previous saved return_catch location. */ - if (rflag) - { - begin_unwind_frame ("evalstring"); - - unwind_protect_int (return_catch_flag); - unwind_protect_jmp_buf (return_catch); - - return_catch_flag++; /* increment so we have a counter */ - rcatch = setjmp_nosigs (return_catch); - } - - if (rcatch) - { - parse_and_execute_cleanup (); - r = return_catch_value; - } - else - /* Note that parse_and_execute () frees the string it is passed. */ - r = parse_and_execute (string, from_file, flags); - - if (rflag) - { - run_unwind_frame ("evalstring"); - if (rcatch && return_catch_flag) - { - return_catch_value = r; - longjmp (return_catch, 1); - } - } - - return (r); -} diff --git a/builtins/read.def~ b/builtins/read.def~ deleted file mode 100644 index f8164fcd..00000000 --- a/builtins/read.def~ +++ /dev/null @@ -1,1074 +0,0 @@ -This file is read.def, from which is created read.c. -It implements the builtin "read" in Bash. - -Copyright (C) 1987-2012 Free Software Foundation, Inc. - -This file is part of GNU Bash, the Bourne Again SHell. - -Bash is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -Bash is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with Bash. If not, see <http://www.gnu.org/licenses/>. - -$PRODUCES read.c - -$BUILTIN read -$FUNCTION read_builtin -$SHORT_DOC read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] -Read a line from the standard input and split it into fields. - -Reads a single line from the standard input, or from file descriptor FD -if the -u option is supplied. The line is split into fields as with word -splitting, and the first word is assigned to the first NAME, the second -word to the second NAME, and so on, with any leftover words assigned to -the last NAME. Only the characters found in $IFS are recognized as word -delimiters. - -If no NAMEs are supplied, the line read is stored in the REPLY variable. - -Options: - -a array assign the words read to sequential indices of the array - variable ARRAY, starting at zero - -d delim continue until the first character of DELIM is read, rather - than newline - -e use Readline to obtain the line in an interactive shell - -i text Use TEXT as the initial text for Readline - -n nchars return after reading NCHARS characters rather than waiting - for a newline, but honor a delimiter if fewer than NCHARS - characters are read before the delimiter - -N nchars return only after reading exactly NCHARS characters, unless - EOF is encountered or read times out, ignoring any delimiter - -p prompt output the string PROMPT without a trailing newline before - attempting to read - -r do not allow backslashes to escape any characters - -s do not echo input coming from a terminal - -t timeout time out and return failure if a complete line of input is - not read within TIMEOUT seconds. The value of the TMOUT - variable is the default timeout. TIMEOUT may be a - fractional number. If TIMEOUT is 0, read returns immediately, - without trying to read any data, returning success only if - input is available on the specified file descriptor. The - exit status is greater than 128 if the timeout is exceeded - -u fd read from file descriptor FD instead of the standard input - -Exit Status: -The return code is zero, unless end-of-file is encountered, read times out -(in which case it's greater than 128), a variable assignment error occurs, -or an invalid file descriptor is supplied as the argument to -u. -$END - -#include <config.h> - -#include "bashtypes.h" -#include "posixstat.h" - -#include <stdio.h> - -#include "bashansi.h" - -#if defined (HAVE_UNISTD_H) -# include <unistd.h> -#endif - -#include <signal.h> -#include <errno.h> - -#ifdef __CYGWIN__ -# include <fcntl.h> -# include <io.h> -#endif - -#include "../bashintl.h" - -#include "../shell.h" -#include "common.h" -#include "bashgetopt.h" - -#include <shtty.h> - -#if defined (READLINE) -#include "../bashline.h" -#include <readline/readline.h> -#endif - -#if defined (BUFFERED_INPUT) -# include "input.h" -#endif - -#include "shmbutil.h" - -#if !defined(errno) -extern int errno; -#endif - -extern void run_pending_traps __P((void)); - -extern int posixly_correct; -extern int trapped_signal_received; - -struct ttsave -{ - int fd; - TTYSTRUCT *attrs; -}; - -#if defined (READLINE) -static void reset_attempted_completion_function __P((char *)); -static int set_itext __P((void)); -static char *edit_line __P((char *, char *)); -static void set_eol_delim __P((int)); -static void reset_eol_delim __P((char *)); -#endif -static SHELL_VAR *bind_read_variable __P((char *, char *)); -#if defined (HANDLE_MULTIBYTE) -static int read_mbchar __P((int, char *, int, int, int)); -#endif -static void ttyrestore __P((struct ttsave *)); - -static sighandler sigalrm __P((int)); -static void reset_alarm __P((void)); - -static procenv_t alrmbuf; -static int sigalrm_seen, reading; -static SigHandler *old_alrm; -static unsigned char delim; - -/* In most cases, SIGALRM just sets a flag that we check periodically. This - avoids problems with the semi-tricky stuff we do with the xfree of - input_string at the top of the unwind-protect list (see below). */ -#define CHECK_ALRM \ - do { \ - if (sigalrm_seen) \ - longjmp (alrmbuf, 1); \ - } while (0) - -static sighandler -sigalrm (s) - int s; -{ - sigalrm_seen = 1; - if (signal_is_trapped (SIGALRM)) - trap_handler (SIGALRM); - if (reading) /* do the longjmp if we get SIGALRM while in read() */ - longjmp (alrmbuf, 1); -} - -static void -reset_alarm () -{ - set_signal_handler (SIGALRM, old_alrm); - falarm (0, 0); -} - -/* Read the value of the shell variables whose names follow. - The reading is done from the current input stream, whatever - that may be. Successive words of the input line are assigned - to the variables mentioned in LIST. The last variable in LIST - gets the remainder of the words on the line. If no variables - are mentioned in LIST, then the default variable is $REPLY. */ -int -read_builtin (list) - WORD_LIST *list; -{ - register char *varname; - int size, i, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2; - int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul; - int raw, edit, nchars, silent, have_timeout, ignore_delim, fd, lastsig, t_errno; - unsigned int tmsec, tmusec; - long ival, uval; - intmax_t intval; - char c; - char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname; - char *e, *t, *t1, *ps2, *tofree; - struct stat tsb; - SHELL_VAR *var; - TTYSTRUCT ttattrs, ttset; - struct ttsave termsave; -#if defined (ARRAY_VARS) - WORD_LIST *alist; -#endif -#if defined (READLINE) - char *rlbuf, *itext; - int rlind; -#endif - - USE_VAR(size); - USE_VAR(i); - USE_VAR(pass_next); - USE_VAR(print_ps2); - USE_VAR(saw_escape); - USE_VAR(input_is_pipe); -/* USE_VAR(raw); */ - USE_VAR(edit); - USE_VAR(tmsec); - USE_VAR(tmusec); - USE_VAR(nchars); - USE_VAR(silent); - USE_VAR(ifs_chars); - USE_VAR(prompt); - USE_VAR(arrayname); -#if defined (READLINE) - USE_VAR(rlbuf); - USE_VAR(rlind); - USE_VAR(itext); -#endif - USE_VAR(list); - USE_VAR(ps2); - USE_VAR(lastsig); - - sigalrm_seen = reading = 0; - - i = 0; /* Index into the string that we are reading. */ - raw = edit = 0; /* Not reading raw input by default. */ - silent = 0; - arrayname = prompt = (char *)NULL; - fd = 0; /* file descriptor to read from */ - -#if defined (READLINE) - rlbuf = itext = (char *)0; - rlind = 0; -#endif - - tmsec = tmusec = 0; /* no timeout */ - nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0; - delim = '\n'; /* read until newline */ - ignore_delim = 0; - - reset_internal_getopt (); - while ((opt = internal_getopt (list, "ersa:d:i:n:p:t:u:N:")) != -1) - { - switch (opt) - { - case 'r': - raw = 1; - break; - case 'p': - prompt = list_optarg; - break; - case 's': - silent = 1; - break; - case 'e': -#if defined (READLINE) - edit = 1; -#endif - break; - case 'i': -#if defined (READLINE) - itext = list_optarg; -#endif - break; -#if defined (ARRAY_VARS) - case 'a': - arrayname = list_optarg; - break; -#endif - case 't': - code = uconvert (list_optarg, &ival, &uval); - if (code == 0 || ival < 0 || uval < 0) - { - builtin_error (_("%s: invalid timeout specification"), list_optarg); - return (EXECUTION_FAILURE); - } - else - { - have_timeout = 1; - tmsec = ival; - tmusec = uval; - } - break; - case 'N': - ignore_delim = 1; - delim = -1; - case 'n': - code = legal_number (list_optarg, &intval); - if (code == 0 || intval < 0 || intval != (int)intval) - { - sh_invalidnum (list_optarg); - return (EXECUTION_FAILURE); - } - else - nchars = intval; - break; - case 'u': - code = legal_number (list_optarg, &intval); - if (code == 0 || intval < 0 || intval != (int)intval) - { - builtin_error (_("%s: invalid file descriptor specification"), list_optarg); - return (EXECUTION_FAILURE); - } - else - fd = intval; - if (sh_validfd (fd) == 0) - { - builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno)); - return (EXECUTION_FAILURE); - } - break; - case 'd': - delim = *list_optarg; - break; - default: - builtin_usage (); - return (EX_USAGE); - } - } - list = loptend; - - /* `read -t 0 var' tests whether input is available with select/FIONREAD, - and fails if those are unavailable */ - if (have_timeout && tmsec == 0 && tmusec == 0) -#if 0 - return (EXECUTION_FAILURE); -#else - return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE); -#endif - - /* If we're asked to ignore the delimiter, make sure we do. */ - if (ignore_delim) - delim = -1; - - /* IF IFS is unset, we use the default of " \t\n". */ - ifs_chars = getifs (); - if (ifs_chars == 0) /* XXX - shouldn't happen */ - ifs_chars = ""; - /* If we want to read exactly NCHARS chars, don't split on IFS */ - if (ignore_delim) - ifs_chars = ""; - for (skip_ctlesc = skip_ctlnul = 0, e = ifs_chars; *e; e++) - skip_ctlesc |= *e == CTLESC, skip_ctlnul |= *e == CTLNUL; - - input_string = (char *)xmalloc (size = 112); /* XXX was 128 */ - input_string[0] = '\0'; - - /* $TMOUT, if set, is the default timeout for read. */ - if (have_timeout == 0 && (e = get_string_value ("TMOUT"))) - { - code = uconvert (e, &ival, &uval); - if (code == 0 || ival < 0 || uval < 0) - tmsec = tmusec = 0; - else - { - tmsec = ival; - tmusec = uval; - } - } - - begin_unwind_frame ("read_builtin"); - -#if defined (BUFFERED_INPUT) - if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd)) - sync_buffered_stream (default_buffered_input); -#endif - - input_is_tty = isatty (fd); - if (input_is_tty == 0) -#ifndef __CYGWIN__ - input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE); -#else - input_is_pipe = 1; -#endif - - /* If the -p, -e or -s flags were given, but input is not coming from the - terminal, turn them off. */ - if ((prompt || edit || silent) && input_is_tty == 0) - { - prompt = (char *)NULL; -#if defined (READLINE) - itext = (char *)NULL; -#endif - edit = silent = 0; - } - -#if defined (READLINE) - if (edit) - add_unwind_protect (xfree, rlbuf); -#endif - - pass_next = 0; /* Non-zero signifies last char was backslash. */ - saw_escape = 0; /* Non-zero signifies that we saw an escape char */ - - if (tmsec > 0 || tmusec > 0) - { - /* Turn off the timeout if stdin is a regular file (e.g. from - input redirection). */ - if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode)) - tmsec = tmusec = 0; - } - - if (tmsec > 0 || tmusec > 0) - { - code = setjmp_nosigs (alrmbuf); - if (code) - { - sigalrm_seen = 0; - /* Tricky. The top of the unwind-protect stack is the free of - input_string. We want to run all the rest and use input_string, - so we have to remove it from the stack. */ - orig_input_string = 0; - - remove_unwind_protect (); - run_unwind_frame ("read_builtin"); - input_string[i] = '\0'; /* make sure it's terminated */ - retval = 128+SIGALRM; - goto assign_vars; - } - old_alrm = set_signal_handler (SIGALRM, sigalrm); - add_unwind_protect (reset_alarm, (char *)NULL); -#if defined (READLINE) - if (edit) - add_unwind_protect (reset_attempted_completion_function, (char *)NULL); -#endif - falarm (tmsec, tmusec); - } - - /* If we've been asked to read only NCHARS chars, or we're using some - character other than newline to terminate the line, do the right - thing to readline or the tty. */ - if (nchars > 0 || delim != '\n') - { -#if defined (READLINE) - if (edit) - { - if (nchars > 0) - { - unwind_protect_int (rl_num_chars_to_read); - rl_num_chars_to_read = nchars; - } - if (delim != '\n') - { - set_eol_delim (delim); - add_unwind_protect (reset_eol_delim, (char *)NULL); - } - } - else -#endif - if (input_is_tty) - { - /* ttsave() */ - termsave.fd = fd; - ttgetattr (fd, &ttattrs); - termsave.attrs = &ttattrs; - - ttset = ttattrs; - i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset); - if (i < 0) - sh_ttyerror (1); - add_unwind_protect ((Function *)ttyrestore, (char *)&termsave); - } - } - else if (silent) /* turn off echo but leave term in canonical mode */ - { - /* ttsave (); */ - termsave.fd = fd; - ttgetattr (fd, &ttattrs); - termsave.attrs = &ttattrs; - - ttset = ttattrs; - i = ttfd_noecho (fd, &ttset); /* ttnoecho (); */ - if (i < 0) - sh_ttyerror (1); - - add_unwind_protect ((Function *)ttyrestore, (char *)&termsave); - } - - /* This *must* be the top unwind-protect on the stack, so the manipulation - of the unwind-protect stack after the realloc() works right. */ - add_unwind_protect (xfree, input_string); - - CHECK_ALRM; - if ((nchars > 0) && (input_is_tty == 0) && ignore_delim) /* read -N */ - unbuffered_read = 2; - else if ((nchars > 0) || (delim != '\n') || input_is_pipe) - unbuffered_read = 1; - - if (prompt && edit == 0) - { - fprintf (stderr, "%s", prompt); - fflush (stderr); - } - -#if defined (__CYGWIN__) && defined (O_TEXT) - setmode (0, O_TEXT); -#endif - - ps2 = 0; - for (print_ps2 = eof = retval = 0;;) - { - CHECK_ALRM; - -#if defined (READLINE) - if (edit) - { - if (rlbuf && rlbuf[rlind] == '\0') - { - xfree (rlbuf); - rlbuf = (char *)0; - } - if (rlbuf == 0) - { - reading = 1; - rlbuf = edit_line (prompt ? prompt : "", itext); - reading = 0; - rlind = 0; - } - if (rlbuf == 0) - { - eof = 1; - break; - } - c = rlbuf[rlind++]; - } - else - { -#endif - - if (print_ps2) - { - if (ps2 == 0) - ps2 = get_string_value ("PS2"); - fprintf (stderr, "%s", ps2 ? ps2 : ""); - fflush (stderr); - print_ps2 = 0; - } - -#if 0 - if (posixly_correct == 0) - interrupt_immediately++; -#endif - reading = 1; - if (unbuffered_read == 2) - retval = posixly_correct ? zreadintr (fd, &c, 1) : zreadn (fd, &c, nchars - nr); - else if (unbuffered_read) - retval = posixly_correct ? zreadintr (fd, &c, 1) : zread (fd, &c, 1); - else - retval = posixly_correct ? zreadcintr (fd, &c) : zreadc (fd, &c); - reading = 0; -#if 0 - if (posixly_correct == 0) - interrupt_immediately--; -#endif - - if (retval <= 0) - { - if (retval < 0 && errno == EINTR) - { - lastsig = LASTSIG(); - if (lastsig == 0) - lastsig = trapped_signal_received; - run_pending_traps (); /* because interrupt_immediately is not set */ - } - else - lastsig = 0; - CHECK_TERMSIG; - eof = 1; - break; - } - - CHECK_ALRM; - -#if defined (READLINE) - } -#endif - - CHECK_ALRM; - if (i + 4 >= size) /* XXX was i + 2; use i + 4 for multibyte/read_mbchar */ - { - char *t; - t = (char *)xrealloc (input_string, size += 128); - - /* Only need to change unwind-protect if input_string changes */ - if (t != input_string) - { - input_string = t; - remove_unwind_protect (); - add_unwind_protect (xfree, input_string); - } - } - - /* If the next character is to be accepted verbatim, a backslash - newline pair still disappears from the input. */ - if (pass_next) - { - pass_next = 0; - if (c == '\n') - { - i--; /* back up over the CTLESC */ - if (interactive && input_is_tty && raw == 0) - print_ps2 = 1; - } - else - goto add_char; - continue; - } - - /* This may cause problems if IFS contains CTLESC */ - if (c == '\\' && raw == 0) - { - pass_next++; - if (skip_ctlesc == 0) - { - saw_escape++; - input_string[i++] = CTLESC; - } - continue; - } - - if ((unsigned char)c == delim) - break; - - if (c == '\0' && delim != '\0') - continue; /* skip NUL bytes in input */ - - if ((skip_ctlesc == 0 && c == CTLESC) || (skip_ctlnul == 0 && c == CTLNUL)) - { - saw_escape++; - input_string[i++] = CTLESC; - } - -add_char: - input_string[i++] = c; - CHECK_ALRM; - -#if defined (HANDLE_MULTIBYTE) - if (nchars > 0 && MB_CUR_MAX > 1 && is_basic (c) == 0) - { - input_string[i] = '\0'; /* for simplicity and debugging */ - i += read_mbchar (fd, input_string, i, c, unbuffered_read); - } -#endif - - nr++; - - if (nchars > 0 && nr >= nchars) - break; - } - input_string[i] = '\0'; - CHECK_ALRM; - - if (retval < 0) - { - t_errno = errno; - if (errno != EINTR) - builtin_error (_("read error: %d: %s"), fd, strerror (errno)); - run_unwind_frame ("read_builtin"); - return ((t_errno != EINTR) ? EXECUTION_FAILURE : 128+lastsig); - } - - if (tmsec > 0 || tmusec > 0) - reset_alarm (); - - if (nchars > 0 || delim != '\n') - { -#if defined (READLINE) - if (edit) - { - if (nchars > 0) - rl_num_chars_to_read = 0; - if (delim != '\n') - reset_eol_delim ((char *)NULL); - } - else -#endif - if (input_is_tty) - ttyrestore (&termsave); - } - else if (silent) - ttyrestore (&termsave); - - if (unbuffered_read == 0) - zsyncfd (fd); - - discard_unwind_frame ("read_builtin"); - - retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS; - -assign_vars: - -#if defined (ARRAY_VARS) - /* If -a was given, take the string read, break it into a list of words, - an assign them to `arrayname' in turn. */ - if (arrayname) - { - if (legal_identifier (arrayname) == 0) - { - sh_invalidid (arrayname); - xfree (input_string); - return (EXECUTION_FAILURE); - } - - var = find_or_make_array_variable (arrayname, 1); - if (var == 0) - { - xfree (input_string); - return EXECUTION_FAILURE; /* readonly or noassign */ - } - if (assoc_p (var)) - { - builtin_error (_("%s: cannot convert associative to indexed array"), arrayname); - xfree (input_string); - return EXECUTION_FAILURE; /* existing associative array */ - } - else if (invisible_p (var)) - VUNSETATTR (var, att_invisible); - array_flush (array_cell (var)); - - alist = list_string (input_string, ifs_chars, 0); - if (alist) - { - if (saw_escape) - dequote_list (alist); - else - word_list_remove_quoted_nulls (alist); - assign_array_var_from_word_list (var, alist, 0); - dispose_words (alist); - } - xfree (input_string); - return (retval); - } -#endif /* ARRAY_VARS */ - - /* If there are no variables, save the text of the line read to the - variable $REPLY. ksh93 strips leading and trailing IFS whitespace, - so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the - same way, but I believe that the difference in behaviors is useful - enough to not do it. Without the bash behavior, there is no way - to read a line completely without interpretation or modification - unless you mess with $IFS (e.g., setting it to the empty string). - If you disagree, change the occurrences of `#if 0' to `#if 1' below. */ - if (list == 0) - { -#if 0 - orig_input_string = input_string; - for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++) - ; - input_string = t; - input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape); -#endif - - if (saw_escape) - { - t = dequote_string (input_string); - var = bind_variable ("REPLY", t, 0); - free (t); - } - else - var = bind_variable ("REPLY", input_string, 0); - VUNSETATTR (var, att_invisible); - - xfree (input_string); - return (retval); - } - - /* This code implements the Posix.2 spec for splitting the words - read and assigning them to variables. */ - orig_input_string = input_string; - - /* Remove IFS white space at the beginning of the input string. If - $IFS is null, no field splitting is performed. */ - for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++) - ; - input_string = t; - for (; list->next; list = list->next) - { - varname = list->word->word; -#if defined (ARRAY_VARS) - if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0) -#else - if (legal_identifier (varname) == 0) -#endif - { - sh_invalidid (varname); - xfree (orig_input_string); - return (EXECUTION_FAILURE); - } - - /* If there are more variables than words read from the input, - the remaining variables are set to the empty string. */ - if (*input_string) - { - /* This call updates INPUT_STRING. */ - t = get_word_from_string (&input_string, ifs_chars, &e); - if (t) - *e = '\0'; - /* Don't bother to remove the CTLESC unless we added one - somewhere while reading the string. */ - if (t && saw_escape) - { - t1 = dequote_string (t); - var = bind_read_variable (varname, t1); - xfree (t1); - } - else - var = bind_read_variable (varname, t ? t : ""); - } - else - { - t = (char *)0; - var = bind_read_variable (varname, ""); - } - - FREE (t); - if (var == 0) - { - xfree (orig_input_string); - return (EXECUTION_FAILURE); - } - - stupidly_hack_special_variables (varname); - VUNSETATTR (var, att_invisible); - } - - /* Now assign the rest of the line to the last variable argument. */ -#if defined (ARRAY_VARS) - if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0) -#else - if (legal_identifier (list->word->word) == 0) -#endif - { - sh_invalidid (list->word->word); - xfree (orig_input_string); - return (EXECUTION_FAILURE); - } - -#if 0 - /* This has to be done this way rather than using string_list - and list_string because Posix.2 says that the last variable gets the - remaining words and their intervening separators. */ - input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape); -#else - /* Check whether or not the number of fields is exactly the same as the - number of variables. */ - tofree = NULL; - if (*input_string) - { - t1 = input_string; - t = get_word_from_string (&input_string, ifs_chars, &e); - if (*input_string == 0) - tofree = input_string = t; - else - { - input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape); - tofree = t; - } - } -#endif - - if (saw_escape && input_string && *input_string) - { - t = dequote_string (input_string); - var = bind_read_variable (list->word->word, t); - xfree (t); - } - else - var = bind_read_variable (list->word->word, input_string ? input_string : ""); - - if (var) - { - stupidly_hack_special_variables (list->word->word); - VUNSETATTR (var, att_invisible); - } - else - retval = EXECUTION_FAILURE; - - FREE (tofree); - xfree (orig_input_string); - - return (retval); -} - -static SHELL_VAR * -bind_read_variable (name, value) - char *name, *value; -{ - SHELL_VAR *v; - -#if defined (ARRAY_VARS) - if (valid_array_reference (name) == 0) - v = bind_variable (name, value, 0); - else - v = assign_array_element (name, value, 0); -#else /* !ARRAY_VARS */ - v = bind_variable (name, value, 0); -#endif /* !ARRAY_VARS */ - return (v == 0 ? v - : ((readonly_p (v) || noassign_p (v)) ? (SHELL_VAR *)NULL : v)); -} - -#if defined (HANDLE_MULTIBYTE) -static int -read_mbchar (fd, string, ind, ch, unbuffered) - int fd; - char *string; - int ind, ch, unbuffered; -{ - char mbchar[MB_LEN_MAX + 1]; - int i, n, r; - char c; - size_t ret; - mbstate_t ps, ps_back; - wchar_t wc; - - memset (&ps, '\0', sizeof (mbstate_t)); - memset (&ps_back, '\0', sizeof (mbstate_t)); - - mbchar[0] = ch; - i = 1; - for (n = 0; n <= MB_LEN_MAX; n++) - { - ps_back = ps; - ret = mbrtowc (&wc, mbchar, i, &ps); - if (ret == (size_t)-2) - { - ps = ps_back; - /* We don't want to be interrupted during a multibyte char read */ - if (unbuffered) - r = zread (fd, &c, 1); - else - r = zreadc (fd, &c); - if (r < 0) - goto mbchar_return; - mbchar[i++] = c; - continue; - } - else if (ret == (size_t)-1 || ret == (size_t)0 || ret > (size_t)0) - break; - } - -mbchar_return: - if (i > 1) /* read a multibyte char */ - /* mbchar[0] is already string[ind-1] */ - for (r = 1; r < i; r++) - string[ind+r-1] = mbchar[r]; - return i - 1; -} -#endif - - -static void -ttyrestore (ttp) - struct ttsave *ttp; -{ - ttsetattr (ttp->fd, ttp->attrs); -} - -#if defined (READLINE) -static rl_completion_func_t *old_attempted_completion_function = 0; -static rl_hook_func_t *old_startup_hook; -static char *deftext; - -static void -reset_attempted_completion_function (cp) - char *cp; -{ - if (rl_attempted_completion_function == 0 && old_attempted_completion_function) - rl_attempted_completion_function = old_attempted_completion_function; -} - -static int -set_itext () -{ - int r1, r2; - - r1 = r2 = 0; - if (old_startup_hook) - r1 = (*old_startup_hook) (); - if (deftext) - { - r2 = rl_insert_text (deftext); - deftext = (char *)NULL; - rl_startup_hook = old_startup_hook; - old_startup_hook = (rl_hook_func_t *)NULL; - } - return (r1 || r2); -} - -static char * -edit_line (p, itext) - char *p; - char *itext; -{ - char *ret; - int len; - - if (bash_readline_initialized == 0) - initialize_readline (); - - old_attempted_completion_function = rl_attempted_completion_function; - rl_attempted_completion_function = (rl_completion_func_t *)NULL; - if (itext) - { - old_startup_hook = rl_startup_hook; - rl_startup_hook = set_itext; - deftext = itext; - } - - ret = readline (p); - - rl_attempted_completion_function = old_attempted_completion_function; - old_attempted_completion_function = (rl_completion_func_t *)NULL; - - if (ret == 0) - return ret; - len = strlen (ret); - ret = (char *)xrealloc (ret, len + 2); - ret[len++] = delim; - ret[len] = '\0'; - return ret; -} - -static int old_delim_ctype; -static rl_command_func_t *old_delim_func; -static int old_newline_ctype; -static rl_command_func_t *old_newline_func; - -static unsigned char delim_char; - -static void -set_eol_delim (c) - int c; -{ - Keymap cmap; - - if (bash_readline_initialized == 0) - initialize_readline (); - cmap = rl_get_keymap (); - - /* Change newline to self-insert */ - old_newline_ctype = cmap[RETURN].type; - old_newline_func = cmap[RETURN].function; - cmap[RETURN].type = ISFUNC; - cmap[RETURN].function = rl_insert; - - /* Bind the delimiter character to accept-line. */ - old_delim_ctype = cmap[c].type; - old_delim_func = cmap[c].function; - cmap[c].type = ISFUNC; - cmap[c].function = rl_newline; - - delim_char = c; -} - -static void -reset_eol_delim (cp) - char *cp; -{ - Keymap cmap; - - cmap = rl_get_keymap (); - - cmap[RETURN].type = old_newline_ctype; - cmap[RETURN].function = old_newline_func; - - cmap[delim_char].type = old_delim_ctype; - cmap[delim_char].function = old_delim_func; -} -#endif diff --git a/builtins/shopt.def~ b/builtins/shopt.def~ deleted file mode 100644 index ffec201c..00000000 --- a/builtins/shopt.def~ +++ /dev/null @@ -1,783 +0,0 @@ -This file is shopt.def, from which is created shopt.c. -It implements the Bash `shopt' builtin. - -Copyright (C) 1994-2012 Free Software Foundation, Inc. - -This file is part of GNU Bash, the Bourne Again SHell. - -Bash is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -Bash is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with Bash. If not, see <http://www.gnu.org/licenses/>. - -$PRODUCES shopt.c - -$BUILTIN shopt -$FUNCTION shopt_builtin -$SHORT_DOC shopt [-pqsu] [-o] [optname ...] -Set and unset shell options. - -Change the setting of each shell option OPTNAME. Without any option -arguments, list all shell options with an indication of whether or not each -is set. - -Options: - -o restrict OPTNAMEs to those defined for use with `set -o' - -p print each shell option with an indication of its status - -q suppress output - -s enable (set) each OPTNAME - -u disable (unset) each OPTNAME - -Exit Status: -Returns success if OPTNAME is enabled; fails if an invalid option is -given or OPTNAME is disabled. -$END - -#include <config.h> - -#if defined (HAVE_UNISTD_H) -# ifdef _MINIX -# include <sys/types.h> -# endif -# include <unistd.h> -#endif - -#include <stdio.h> - -#include "version.h" - -#include "../bashintl.h" - -#include "../shell.h" -#include "../flags.h" -#include "common.h" -#include "bashgetopt.h" - -#if defined (READLINE) -# include "../bashline.h" -#endif - -#if defined (HISTORY) -# include "../bashhist.h" -#endif - -#define UNSETOPT 0 -#define SETOPT 1 - -#define OPTFMT "%-15s\t%s\n" - -extern int allow_null_glob_expansion, fail_glob_expansion, glob_dot_filenames; -extern int cdable_vars, mail_warning, source_uses_path; -extern int no_exit_on_failed_exec, print_shift_error; -extern int check_hashed_filenames, promptvars; -extern int cdspelling, expand_aliases; -extern int extended_quote; -extern int check_window_size; -extern int glob_ignore_case, match_ignore_case; -extern int hup_on_exit; -extern int xpg_echo; -extern int gnu_error_format; -extern int check_jobs_at_exit; -extern int autocd; -extern int glob_star; -extern int glob_asciirange; -extern int lastpipe_opt; - -#if defined (EXTENDED_GLOB) -extern int extended_glob; -#endif - -#if defined (READLINE) -extern int hist_verify, history_reediting, perform_hostname_completion; -extern int no_empty_command_completion; -extern int force_fignore; -extern int dircomplete_spelling, dircomplete_expand; -extern int complete_fullquote; - -extern int enable_hostname_completion __P((int)); -#endif - -#if defined (PROGRAMMABLE_COMPLETION) -extern int prog_completion_enabled; -#endif - -#if defined (RESTRICTED_SHELL) -extern char *shell_name; -#endif - -#if defined (DEBUGGER) -extern int debugging_mode; -#endif - -static void shopt_error __P((char *)); - -static int set_shellopts_after_change __P((char *, int)); -static int shopt_enable_hostname_completion __P((char *, int)); -static int set_compatibility_level __P((char *, int)); - -#if defined (RESTRICTED_SHELL) -static int set_restricted_shell __P((char *, int)); -#endif - -#if defined (READLINE) -static int shopt_set_complete_direxpand __P((char *, int)); -#endif - -static int shopt_login_shell; -static int shopt_compat31; -static int shopt_compat32; -static int shopt_compat40; -static int shopt_compat41; -static int shopt_compat42; - -typedef int shopt_set_func_t __P((char *, int)); - -static struct { - char *name; - int *value; - shopt_set_func_t *set_func; -} shopt_vars[] = { - { "autocd", &autocd, (shopt_set_func_t *)NULL }, - { "cdable_vars", &cdable_vars, (shopt_set_func_t *)NULL }, - { "cdspell", &cdspelling, (shopt_set_func_t *)NULL }, - { "checkhash", &check_hashed_filenames, (shopt_set_func_t *)NULL }, -#if defined (JOB_CONTROL) - { "checkjobs", &check_jobs_at_exit, (shopt_set_func_t *)NULL }, -#endif - { "checkwinsize", &check_window_size, (shopt_set_func_t *)NULL }, -#if defined (HISTORY) - { "cmdhist", &command_oriented_history, (shopt_set_func_t *)NULL }, -#endif - { "compat31", &shopt_compat31, set_compatibility_level }, - { "compat32", &shopt_compat32, set_compatibility_level }, - { "compat40", &shopt_compat40, set_compatibility_level }, - { "compat41", &shopt_compat41, set_compatibility_level }, - { "compat42", &shopt_compat41, set_compatibility_level }, -#if defined (READLINE) - { "complete_fullquote", &complete_fullquote, (shopt_set_func_t *)NULL}, - { "direxpand", &dircomplete_expand, shopt_set_complete_direxpand }, - { "dirspell", &dircomplete_spelling, (shopt_set_func_t *)NULL }, -#endif - { "dotglob", &glob_dot_filenames, (shopt_set_func_t *)NULL }, - { "execfail", &no_exit_on_failed_exec, (shopt_set_func_t *)NULL }, - { "expand_aliases", &expand_aliases, (shopt_set_func_t *)NULL }, -#if defined (DEBUGGER) - { "extdebug", &debugging_mode, (shopt_set_func_t *)NULL }, -#endif -#if defined (EXTENDED_GLOB) - { "extglob", &extended_glob, (shopt_set_func_t *)NULL }, -#endif - { "extquote", &extended_quote, (shopt_set_func_t *)NULL }, - { "failglob", &fail_glob_expansion, (shopt_set_func_t *)NULL }, -#if defined (READLINE) - { "force_fignore", &force_fignore, (shopt_set_func_t *)NULL }, -#endif - { "globstar", &glob_star, (shopt_set_func_t *)NULL }, - { "globasciiranges", &glob_asciirange, (shopt_set_func_t *)NULL }, - { "gnu_errfmt", &gnu_error_format, (shopt_set_func_t *)NULL }, -#if defined (HISTORY) - { "histappend", &force_append_history, (shopt_set_func_t *)NULL }, -#endif -#if defined (READLINE) - { "histreedit", &history_reediting, (shopt_set_func_t *)NULL }, - { "histverify", &hist_verify, (shopt_set_func_t *)NULL }, - { "hostcomplete", &perform_hostname_completion, shopt_enable_hostname_completion }, -#endif - { "huponexit", &hup_on_exit, (shopt_set_func_t *)NULL }, - { "interactive_comments", &interactive_comments, set_shellopts_after_change }, - { "lastpipe", &lastpipe_opt, (shopt_set_func_t *)NULL }, -#if defined (HISTORY) - { "lithist", &literal_history, (shopt_set_func_t *)NULL }, -#endif - { "login_shell", &shopt_login_shell, set_login_shell }, - { "mailwarn", &mail_warning, (shopt_set_func_t *)NULL }, -#if defined (READLINE) - { "no_empty_cmd_completion", &no_empty_command_completion, (shopt_set_func_t *)NULL }, -#endif - { "nocaseglob", &glob_ignore_case, (shopt_set_func_t *)NULL }, - { "nocasematch", &match_ignore_case, (shopt_set_func_t *)NULL }, - { "nullglob", &allow_null_glob_expansion, (shopt_set_func_t *)NULL }, -#if defined (PROGRAMMABLE_COMPLETION) - { "progcomp", &prog_completion_enabled, (shopt_set_func_t *)NULL }, -#endif - { "promptvars", &promptvars, (shopt_set_func_t *)NULL }, -#if defined (RESTRICTED_SHELL) - { "restricted_shell", &restricted_shell, set_restricted_shell }, -#endif - { "shift_verbose", &print_shift_error, (shopt_set_func_t *)NULL }, - { "sourcepath", &source_uses_path, (shopt_set_func_t *)NULL }, - { "xpg_echo", &xpg_echo, (shopt_set_func_t *)NULL }, - { (char *)0, (int *)0, (shopt_set_func_t *)NULL } -}; - -#define N_SHOPT_OPTIONS (sizeof (shopt_vars) / sizeof (shopt_vars[0])) - -#define GET_SHOPT_OPTION_VALUE(i) (*shopt_vars[i].value) - -static const char * const on = "on"; -static const char * const off = "off"; - -static int find_shopt __P((char *)); -static int toggle_shopts __P((int, WORD_LIST *, int)); -static void print_shopt __P((char *, int, int)); -static int list_shopts __P((WORD_LIST *, int)); -static int list_some_shopts __P((int, int)); -static int list_shopt_o_options __P((WORD_LIST *, int)); -static int list_some_o_options __P((int, int)); -static int set_shopt_o_options __P((int, WORD_LIST *, int)); - -#define SFLAG 0x01 -#define UFLAG 0x02 -#define QFLAG 0x04 -#define OFLAG 0x08 -#define PFLAG 0x10 - -int -shopt_builtin (list) - WORD_LIST *list; -{ - int opt, flags, rval; - - flags = 0; - reset_internal_getopt (); - while ((opt = internal_getopt (list, "psuoq")) != -1) - { - switch (opt) - { - case 's': - flags |= SFLAG; - break; - case 'u': - flags |= UFLAG; - break; - case 'q': - flags |= QFLAG; - break; - case 'o': - flags |= OFLAG; - break; - case 'p': - flags |= PFLAG; - break; - default: - builtin_usage (); - return (EX_USAGE); - } - } - list = loptend; - - if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG)) - { - builtin_error (_("cannot set and unset shell options simultaneously")); - return (EXECUTION_FAILURE); - } - - rval = EXECUTION_SUCCESS; - if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */ - rval = list_shopt_o_options (list, flags); - else if (list && (flags & OFLAG)) /* shopt -so args */ - rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG); - else if (flags & OFLAG) /* shopt -so */ - rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags); - else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */ - rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG); - else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */ - rval = list_shopts (list, flags); - else /* shopt -su */ - rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags); - return (rval); -} - -/* Reset the options managed by `shopt' to the values they would have at - shell startup. */ -void -reset_shopt_options () -{ - allow_null_glob_expansion = glob_dot_filenames = 0; - cdable_vars = mail_warning = 0; - no_exit_on_failed_exec = print_shift_error = 0; - check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0; - - source_uses_path = promptvars = 1; - -#if defined (EXTENDED_GLOB) - extended_glob = 0; -#endif - -#if defined (HISTORY) - literal_history = force_append_history = 0; - command_oriented_history = 1; -#endif - -#if defined (READLINE) - hist_verify = history_reediting = 0; - perform_hostname_completion = 1; -#endif - - shopt_login_shell = login_shell; -} - -static int -find_shopt (name) - char *name; -{ - int i; - - for (i = 0; shopt_vars[i].name; i++) - if (STREQ (name, shopt_vars[i].name)) - return i; - return -1; -} - -static void -shopt_error (s) - char *s; -{ - builtin_error (_("%s: invalid shell option name"), s); -} - -static int -toggle_shopts (mode, list, quiet) - int mode; - WORD_LIST *list; - int quiet; -{ - WORD_LIST *l; - int ind, rval; - - for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next) - { - ind = find_shopt (l->word->word); - if (ind < 0) - { - shopt_error (l->word->word); - rval = EXECUTION_FAILURE; - } - else - { - *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */ - if (shopt_vars[ind].set_func) - (*shopt_vars[ind].set_func) (shopt_vars[ind].name, mode); - } - } - - set_bashopts (); - return (rval); -} - -static void -print_shopt (name, val, flags) - char *name; - int val, flags; -{ - if (flags & PFLAG) - printf ("shopt %s %s\n", val ? "-s" : "-u", name); - else - printf (OPTFMT, name, val ? on : off); -} - -/* List the values of all or any of the `shopt' options. Returns 0 if - all were listed or all variables queried were on; 1 otherwise. */ -static int -list_shopts (list, flags) - WORD_LIST *list; - int flags; -{ - WORD_LIST *l; - int i, val, rval; - - if (list == 0) - { - for (i = 0; shopt_vars[i].name; i++) - { - val = *shopt_vars[i].value; - if ((flags & QFLAG) == 0) - print_shopt (shopt_vars[i].name, val, flags); - } - return (sh_chkwrite (EXECUTION_SUCCESS)); - } - - for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next) - { - i = find_shopt (l->word->word); - if (i < 0) - { - shopt_error (l->word->word); - rval = EXECUTION_FAILURE; - continue; - } - val = *shopt_vars[i].value; - if (val == 0) - rval = EXECUTION_FAILURE; - if ((flags & QFLAG) == 0) - print_shopt (l->word->word, val, flags); - } - - return (sh_chkwrite (rval)); -} - -static int -list_some_shopts (mode, flags) - int mode, flags; -{ - int val, i; - - for (i = 0; shopt_vars[i].name; i++) - { - val = *shopt_vars[i].value; - if (((flags & QFLAG) == 0) && mode == val) - print_shopt (shopt_vars[i].name, val, flags); - } - return (sh_chkwrite (EXECUTION_SUCCESS)); -} - -static int -list_shopt_o_options (list, flags) - WORD_LIST *list; - int flags; -{ - WORD_LIST *l; - int val, rval; - - if (list == 0) - { - if ((flags & QFLAG) == 0) - list_minus_o_opts (-1, (flags & PFLAG)); - return (sh_chkwrite (EXECUTION_SUCCESS)); - } - - for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next) - { - val = minus_o_option_value (l->word->word); - if (val == -1) - { - sh_invalidoptname (l->word->word); - rval = EXECUTION_FAILURE; - continue; - } - if (val == 0) - rval = EXECUTION_FAILURE; - if ((flags & QFLAG) == 0) - { - if (flags & PFLAG) - printf ("set %co %s\n", val ? '-' : '+', l->word->word); - else - printf (OPTFMT, l->word->word, val ? on : off); - } - } - return (sh_chkwrite (rval)); -} - -static int -list_some_o_options (mode, flags) - int mode, flags; -{ - if ((flags & QFLAG) == 0) - list_minus_o_opts (mode, (flags & PFLAG)); - return (sh_chkwrite (EXECUTION_SUCCESS)); -} - -static int -set_shopt_o_options (mode, list, quiet) - int mode; - WORD_LIST *list; - int quiet; -{ - WORD_LIST *l; - int rval; - - for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next) - { - if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE) - rval = EXECUTION_FAILURE; - } - set_shellopts (); - return rval; -} - -/* If we set or unset interactive_comments with shopt, make sure the - change is reflected in $SHELLOPTS. */ -static int -set_shellopts_after_change (option_name, mode) - char *option_name; - int mode; -{ - set_shellopts (); - return (0); -} - -static int -shopt_enable_hostname_completion (option_name, mode) - char *option_name; - int mode; -{ - return (enable_hostname_completion (mode)); -} - -static int -set_compatibility_level (option_name, mode) - char *option_name; - int mode; -{ - /* Need to change logic here as we add more compatibility levels or do - this with a shell variable. */ - - /* First, check option_name so we can turn off other compat options when - one is set. */ - if (mode && option_name[6] == '3') - shopt_compat40 = shopt_compat41 = shopt_compat42 = 0; - else if (mode && option_name[6] == '4') - shopt_compat31 = shopt_compat32 = 0; - - if (mode && option_name[6] == '3' && option_name[7] == '1') - shopt_compat32 = 0; - else if (mode && option_name[6] == '3' && option_name[7] == '2') - shopt_compat31 = 0; - else if (mode && option_name[6] == '4' && option_name[7] == '0') - shopt_compat41 = shopt_compat42 = 0; - else if (mode && option_name[6] == '4' && option_name[7] == '1') - shopt_compat40 = shopt_compat42 = 0; - else if (mode && option_name[6] == '4' && option_name[7] == '2') - shopt_compat40 = shopt_compat41 = 0; - - /* Then set shell_compatibility_level based on what remains */ - if (shopt_compat31) - shell_compatibility_level = 31; - else if (shopt_compat32) - shell_compatibility_level = 32; - else if (shopt_compat40) - shell_compatibility_level = 40; - else if (shopt_compat41) - shell_compatibility_level = 41; - else if (shopt_compat42) - shell_compatibility_level = 42; - else - shell_compatibility_level = DEFAULT_COMPAT_LEVEL; - - return 0; -} - -/* Set and unset the various compatibility options from the value of - shell_compatibility_level; used by sv_shcompat */ -void -set_compatibility_opts () -{ - shopt_compat31 = shopt_compat32 = shopt_compat40 = shopt_compat41 = shopt_compat42 = 0; - switch (shell_compatibility_level) - { - case DEFAULT_COMPAT_LEVEL: - break; - case 42: - shopt_compat42 = 1; break; - case 41: - shopt_compat41 = 1; break; - case 40: - shopt_compat40 = 1; break; - case 32: - shopt_compat32 = 1; break; - case 31: - shopt_compat31 = 1; break; - } -} - -#if defined (READLINE) -static int -shopt_set_complete_direxpand (option_name, mode) - char *option_name; - int mode; -{ - set_directory_hook (); - return 0; -} -#endif - -#if defined (RESTRICTED_SHELL) -/* Don't allow the value of restricted_shell to be modified. */ - -static int -set_restricted_shell (option_name, mode) - char *option_name; - int mode; -{ - static int save_restricted = -1; - - if (save_restricted == -1) - save_restricted = shell_is_restricted (shell_name); - - restricted_shell = save_restricted; - return (0); -} -#endif /* RESTRICTED_SHELL */ - -/* Not static so shell.c can call it to initialize shopt_login_shell */ -int -set_login_shell (option_name, mode) - char *option_name; - int mode; -{ - shopt_login_shell = login_shell != 0; - return (0); -} - -char ** -get_shopt_options () -{ - char **ret; - int n, i; - - n = sizeof (shopt_vars) / sizeof (shopt_vars[0]); - ret = strvec_create (n + 1); - for (i = 0; shopt_vars[i].name; i++) - ret[i] = savestring (shopt_vars[i].name); - ret[i] = (char *)NULL; - return ret; -} - -/* - * External interface for other parts of the shell. NAME is a string option; - * MODE is 0 if we want to unset an option; 1 if we want to set an option. - * REUSABLE is 1 if we want to print output in a form that may be reused. - */ -int -shopt_setopt (name, mode) - char *name; - int mode; -{ - WORD_LIST *wl; - int r; - - wl = add_string_to_list (name, (WORD_LIST *)NULL); - r = toggle_shopts (mode, wl, 0); - dispose_words (wl); - return r; -} - -int -shopt_listopt (name, reusable) - char *name; - int reusable; -{ - int i; - - if (name == 0) - return (list_shopts ((WORD_LIST *)NULL, reusable ? PFLAG : 0)); - - i = find_shopt (name); - if (i < 0) - { - shopt_error (name); - return (EXECUTION_FAILURE); - } - - print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0); - return (sh_chkwrite (EXECUTION_SUCCESS)); -} - -void -set_bashopts () -{ - char *value; - char tflag[N_SHOPT_OPTIONS]; - int vsize, i, vptr, *ip, exported; - SHELL_VAR *v; - - for (vsize = i = 0; shopt_vars[i].name; i++) - { - tflag[i] = 0; - if (GET_SHOPT_OPTION_VALUE (i)) - { - vsize += strlen (shopt_vars[i].name) + 1; - tflag[i] = 1; - } - } - - value = (char *)xmalloc (vsize + 1); - - for (i = vptr = 0; shopt_vars[i].name; i++) - { - if (tflag[i]) - { - strcpy (value + vptr, shopt_vars[i].name); - vptr += strlen (shopt_vars[i].name); - value[vptr++] = ':'; - } - } - - if (vptr) - vptr--; /* cut off trailing colon */ - value[vptr] = '\0'; - - v = find_variable ("BASHOPTS"); - - /* Turn off the read-only attribute so we can bind the new value, and - note whether or not the variable was exported. */ - if (v) - { - VUNSETATTR (v, att_readonly); - exported = exported_p (v); - } - else - exported = 0; - - v = bind_variable ("BASHOPTS", value, 0); - - /* Turn the read-only attribute back on, and turn off the export attribute - if it was set implicitly by mark_modified_vars and SHELLOPTS was not - exported before we bound the new value. */ - VSETATTR (v, att_readonly); - if (mark_modified_vars && exported == 0 && exported_p (v)) - VUNSETATTR (v, att_exported); - - free (value); -} - -void -parse_bashopts (value) - char *value; -{ - char *vname; - int vptr, ind; - - vptr = 0; - while (vname = extract_colon_unit (value, &vptr)) - { - ind = find_shopt (vname); - if (ind >= 0) - *shopt_vars[ind].value = 1; - free (vname); - } -} - -void -initialize_bashopts (no_bashopts) - int no_bashopts; -{ - char *temp; - SHELL_VAR *var; - - if (no_bashopts == 0) - { - var = find_variable ("BASHOPTS"); - /* set up any shell options we may have inherited. */ - if (var && imported_p (var)) - { - temp = (array_p (var) || assoc_p (var)) ? (char *)NULL : savestring (value_cell (var)); - if (temp) - { - parse_bashopts (temp); - free (temp); - } - } - } - - /* Set up the $BASHOPTS variable. */ - set_bashopts (); -} |
