1 /* variables.c -- Functions for hacking shell variables. */
3 /* Copyright (C) 1987-2023 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #include "bashtypes.h"
24 #include "posixstat.h"
25 #include "posixtime.h"
28 # if defined (__QNXNTO__)
29 # include <sys/netmgr.h>
32 # endif /* !__QNXNTO__ */
35 #if defined (HAVE_UNISTD_H)
40 #include "chartypes.h"
41 #if defined (HAVE_PWD_H)
48 #define NEED_XTRACE_SET_DECL
53 #include "execute_cmd.h"
55 #include "mailcheck.h"
64 #include "builtins/getopt.h"
65 #include "builtins/common.h"
66 #include "builtins/builtext.h"
68 #if defined (READLINE)
69 # include "bashline.h"
70 # include <readline/readline.h>
72 # include <tilde/tilde.h>
76 # include "bashhist.h"
77 # include <readline/history.h>
80 #if defined (PROGRAMMABLE_COMPLETION)
81 # include "pcomplete.h"
84 #define VARIABLES_HASH_BUCKETS 1024 /* must be power of two */
85 #define FUNCTIONS_HASH_BUCKETS 512
86 #define TEMPENV_HASH_BUCKETS 4 /* must be power of two */
88 #define BASHFUNC_PREFIX "BASH_FUNC_"
89 #define BASHFUNC_PREFLEN 10 /* == strlen(BASHFUNC_PREFIX */
90 #define BASHFUNC_SUFFIX "%%"
91 #define BASHFUNC_SUFFLEN 2 /* == strlen(BASHFUNC_SUFFIX) */
94 #define BASHARRAY_PREFIX "BASH_ARRAY_"
95 #define BASHARRAY_PREFLEN 11
96 #define BASHARRAY_SUFFIX "%%"
97 #define BASHARRAY_SUFFLEN 2
99 #define BASHASSOC_PREFIX "BASH_ASSOC_"
100 #define BASHASSOC_PREFLEN 11
101 #define BASHASSOC_SUFFIX "%%" /* needs to be the same as BASHARRAY_SUFFIX */
102 #define BASHASSOC_SUFFLEN 2
105 /* flags for find_variable_internal */
107 #define FV_FORCETEMPENV 0x01
108 #define FV_SKIPINVISIBLE 0x02
109 #define FV_NODYNAMIC 0x04
111 extern char **environ
;
113 /* Variables used here and defined in other files. */
114 extern time_t shell_start_time
;
115 extern struct timeval shellstart
;
117 /* The list of shell variables that the user has created at the global
118 scope, or that came from the environment. */
119 VAR_CONTEXT
*global_variables
= (VAR_CONTEXT
*)NULL
;
121 /* The current list of shell variables, including function scopes */
122 VAR_CONTEXT
*shell_variables
= (VAR_CONTEXT
*)NULL
;
124 /* The list of shell functions that the user has created, or that came from
126 HASH_TABLE
*shell_functions
= (HASH_TABLE
*)NULL
;
128 HASH_TABLE
*invalid_env
= (HASH_TABLE
*)NULL
;
130 #if defined (DEBUGGER)
131 /* The table of shell function definitions that the user defined or that
132 came from the environment. */
133 HASH_TABLE
*shell_function_defs
= (HASH_TABLE
*)NULL
;
136 /* The current variable context. This is really a count of how deep into
137 executing functions we are. */
138 int variable_context
= 0;
140 /* If non-zero, local variables inherit values and attributes from a variable
141 with the same name at a previous scope. */
142 int localvar_inherit
= 0;
144 /* If non-zero, calling `unset' on local variables in previous scopes marks
145 them as invisible so lookups find them unset. This is the same behavior
146 as local variables in the current local scope. */
147 int localvar_unset
= 0;
149 /* The set of shell assignments which are made only in the environment
150 for a single command. */
151 HASH_TABLE
*temporary_env
= (HASH_TABLE
*)NULL
;
153 /* Set to non-zero if an assignment error occurs while putting variables
154 into the temporary environment. */
155 int tempenv_assign_error
;
157 /* Some funky variables which are known about specially. Here is where
158 "$*", "$1", and all the cruft is kept. */
159 char *dollar_vars
[10];
160 WORD_LIST
*rest_of_args
= (WORD_LIST
*)NULL
;
161 int posparam_count
= 0;
163 /* The value of $$. */
164 pid_t dollar_dollar_pid
;
166 /* Non-zero means that we have to remake EXPORT_ENV. */
167 int array_needs_making
= 1;
169 /* The number of times BASH has been executed. This is set
170 by initialize_variables (). */
173 /* An array which is passed to commands as their environment. It is
174 manufactured from the union of the initial environment and the
175 shell variables that are marked for export. */
176 char **export_env
= (char **)NULL
;
177 static int export_env_index
;
178 static size_t export_env_size
;
180 #if defined (READLINE)
181 static int winsize_assignment
; /* currently assigning to LINES or COLUMNS */
184 SHELL_VAR nameref_invalid_value
;
185 static SHELL_VAR nameref_maxloop_value
;
187 static HASH_TABLE
*last_table_searched
; /* hash_lookup sets this */
188 static VAR_CONTEXT
*last_context_searched
;
190 /* Some forward declarations. */
191 static void create_variable_tables (void);
193 static void set_machine_vars (void);
194 static void set_home_var (void);
195 static void set_shell_var (void);
196 static char *get_bash_name (void);
197 static void initialize_shell_level (void);
198 static void uidset (void);
199 #if defined (ARRAY_VARS)
200 static void make_vers_array (void);
203 static SHELL_VAR
*null_assign (SHELL_VAR
*, char *, arrayind_t
, char *);
204 #if defined (ARRAY_VARS)
205 static SHELL_VAR
*null_array_assign (SHELL_VAR
*, char *, arrayind_t
, char *);
207 static SHELL_VAR
*get_self (SHELL_VAR
*);
209 #if defined (ARRAY_VARS)
210 static SHELL_VAR
*init_dynamic_array_var (char *, sh_var_value_func_t
*, sh_var_assign_func_t
*, int);
211 static SHELL_VAR
*init_dynamic_assoc_var (char *, sh_var_value_func_t
*, sh_var_assign_func_t
*, int);
214 static inline SHELL_VAR
*set_int_value (SHELL_VAR
*, intmax_t, int);
215 static inline SHELL_VAR
*set_string_value (SHELL_VAR
*, const char *, int);
217 static SHELL_VAR
*assign_seconds (SHELL_VAR
*, char *, arrayind_t
, char *);
218 static SHELL_VAR
*get_seconds (SHELL_VAR
*);
219 static SHELL_VAR
*init_seconds_var (void);
221 static SHELL_VAR
*get_monoseconds (SHELL_VAR
*);
223 static SHELL_VAR
*assign_random (SHELL_VAR
*, char *, arrayind_t
, char *);
224 static SHELL_VAR
*get_random (SHELL_VAR
*);
226 static SHELL_VAR
*get_urandom (SHELL_VAR
*);
228 static SHELL_VAR
*assign_lineno (SHELL_VAR
*, char *, arrayind_t
, char *);
229 static SHELL_VAR
*get_lineno (SHELL_VAR
*);
231 static SHELL_VAR
*assign_subshell (SHELL_VAR
*, char *, arrayind_t
, char *);
232 static SHELL_VAR
*get_subshell (SHELL_VAR
*);
234 static SHELL_VAR
*get_epochseconds (SHELL_VAR
*);
235 static SHELL_VAR
*get_epochrealtime (SHELL_VAR
*);
237 static SHELL_VAR
*get_bashpid (SHELL_VAR
*);
239 static SHELL_VAR
*get_bash_argv0 (SHELL_VAR
*);
240 static SHELL_VAR
*assign_bash_argv0 (SHELL_VAR
*, char *, arrayind_t
, char *);
241 static void set_argv0 (void);
243 #if defined (HISTORY)
244 static SHELL_VAR
*get_histcmd (SHELL_VAR
*);
247 #if defined (READLINE)
248 static SHELL_VAR
*get_comp_wordbreaks (SHELL_VAR
*);
249 static SHELL_VAR
*assign_comp_wordbreaks (SHELL_VAR
*, char *, arrayind_t
, char *);
252 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
253 static SHELL_VAR
*assign_dirstack (SHELL_VAR
*, char *, arrayind_t
, char *);
254 static SHELL_VAR
*get_dirstack (SHELL_VAR
*);
257 #if defined (ARRAY_VARS)
258 static SHELL_VAR
*get_groupset (SHELL_VAR
*);
259 # if defined (DEBUGGER)
260 static SHELL_VAR
*get_bashargcv (SHELL_VAR
*);
262 static SHELL_VAR
*build_hashcmd (SHELL_VAR
*);
263 static SHELL_VAR
*get_hashcmd (SHELL_VAR
*);
264 static SHELL_VAR
*assign_hashcmd (SHELL_VAR
*, char *, arrayind_t
, char *);
266 static SHELL_VAR
*build_aliasvar (SHELL_VAR
*);
267 static SHELL_VAR
*get_aliasvar (SHELL_VAR
*);
268 static SHELL_VAR
*assign_aliasvar (SHELL_VAR
*, char *, arrayind_t
, char *);
272 static SHELL_VAR
*get_funcname (SHELL_VAR
*);
273 static SHELL_VAR
*init_funcname_var (void);
275 static void initialize_dynamic_variables (void);
277 static SHELL_VAR
*bind_invalid_envvar (const char *, const char *, int);
279 static int var_sametype (SHELL_VAR
*, SHELL_VAR
*);
281 static SHELL_VAR
*hash_lookup (const char *, HASH_TABLE
*);
282 static SHELL_VAR
*new_shell_variable (const char *);
283 static SHELL_VAR
*make_new_variable (const char *, HASH_TABLE
*);
284 static SHELL_VAR
*bind_variable_internal (const char *, const char *, HASH_TABLE
*, int, int);
286 static void init_shell_variable (SHELL_VAR
*);
288 static void dispose_variable_value (SHELL_VAR
*);
289 static void free_variable_hash_data (PTR_T
);
291 static VARLIST
*vlist_alloc (size_t);
292 static VARLIST
*vlist_realloc (VARLIST
*, size_t);
293 static void vlist_add (VARLIST
*, SHELL_VAR
*, int);
295 static void flatten (HASH_TABLE
*, sh_var_map_func_t
*, VARLIST
*, int);
297 static int qsort_var_comp (SHELL_VAR
**, SHELL_VAR
**);
299 static SHELL_VAR
**vapply (sh_var_map_func_t
*);
300 static SHELL_VAR
**fapply (sh_var_map_func_t
*);
302 static int visible_var (SHELL_VAR
*);
303 static int visible_and_exported (SHELL_VAR
*);
304 static int export_environment_candidate (SHELL_VAR
*);
305 static int local_and_exported (SHELL_VAR
*);
306 static int visible_variable_in_context (SHELL_VAR
*);
307 static int variable_in_context (SHELL_VAR
*);
308 #if defined (ARRAY_VARS)
309 static int visible_array_vars (SHELL_VAR
*);
312 static SHELL_VAR
*find_variable_internal (const char *, int);
314 static SHELL_VAR
*find_nameref_at_context (SHELL_VAR
*, VAR_CONTEXT
*);
315 static SHELL_VAR
*find_variable_nameref_context (SHELL_VAR
*, VAR_CONTEXT
*, VAR_CONTEXT
**);
316 static SHELL_VAR
*find_variable_last_nameref_context (SHELL_VAR
*, VAR_CONTEXT
*, VAR_CONTEXT
**);
318 static SHELL_VAR
*bind_tempenv_variable (const char *, const char *);
319 static void push_posix_temp_var (PTR_T
);
320 static void push_temp_var (PTR_T
);
321 static void propagate_temp_var (PTR_T
);
322 static void dispose_temporary_env (sh_free_func_t
*);
324 static inline char *mk_env_string (const char *, const char *, int);
325 static char **make_env_array_from_var_list (SHELL_VAR
**);
326 static char **make_var_export_array (VAR_CONTEXT
*);
327 static char **make_func_export_array (void);
328 static void add_temp_array_to_env (char **, int, int);
330 static int n_shell_variables (void);
331 static int set_context (SHELL_VAR
*);
333 static void push_func_var (PTR_T
);
334 static void push_builtin_var (PTR_T
);
335 static void push_exported_var (PTR_T
);
337 static void delete_local_contexts (VAR_CONTEXT
*);
339 /* This needs to be looked at again. */
340 static inline void push_posix_tempvar_internal (SHELL_VAR
*, int);
342 static inline int find_special_var (const char *);
345 create_variable_tables (void)
347 if (shell_variables
== 0)
349 shell_variables
= global_variables
= new_var_context ((char *)NULL
, 0);
350 shell_variables
->scope
= 0;
351 shell_variables
->table
= hash_create (VARIABLES_HASH_BUCKETS
);
354 if (shell_functions
== 0)
355 shell_functions
= hash_create (FUNCTIONS_HASH_BUCKETS
);
357 #if defined (DEBUGGER)
358 if (shell_function_defs
== 0)
359 shell_function_defs
= hash_create (FUNCTIONS_HASH_BUCKETS
);
363 /* Initialize the shell variables from the current environment.
364 If PRIVMODE is nonzero, don't import functions from ENV or
367 initialize_shell_variables (char **env
, int privmode
)
369 char *name
, *string
, *temp_string
;
370 int c
, char_index
, string_index
, string_length
, ro
;
373 create_variable_tables ();
375 for (string_index
= 0; env
&& (string
= env
[string_index
++]); )
379 while ((c
= *string
++) && c
!= '=')
381 if (string
[-1] == '=')
382 char_index
= string
- name
- 1;
384 /* If there are weird things in the environment, like `=xxx' or a
385 string without an `=', just skip them. */
389 /* ASSERT(name[char_index] == '=') */
390 name
[char_index
] = '\0';
391 /* Now, name = env variable name, string = env variable value, and
392 char_index == strlen (name) */
394 temp_var
= (SHELL_VAR
*)NULL
;
396 #if defined (FUNCTION_IMPORT)
397 /* If exported function, define it now. Don't import functions from
398 the environment in privileged mode. */
399 if (privmode
== 0 && read_but_dont_execute
== 0 &&
400 STREQN (BASHFUNC_PREFIX
, name
, BASHFUNC_PREFLEN
) &&
401 STREQ (BASHFUNC_SUFFIX
, name
+ char_index
- BASHFUNC_SUFFLEN
) &&
402 STREQN ("() {", string
, 4))
405 char *tname
; /* desired imported function name */
407 namelen
= char_index
- BASHFUNC_PREFLEN
- BASHFUNC_SUFFLEN
;
409 tname
= name
+ BASHFUNC_PREFLEN
; /* start of func name */
410 tname
[namelen
] = '\0'; /* now tname == func name */
412 string_length
= strlen (string
);
413 temp_string
= (char *)xmalloc (namelen
+ string_length
+ 2);
415 memcpy (temp_string
, tname
, namelen
);
416 temp_string
[namelen
] = ' ';
417 memcpy (temp_string
+ namelen
+ 1, string
, string_length
+ 1);
419 /* Don't import function names that are invalid identifiers from the
420 environment in posix mode, though we still allow them to be defined as
422 if (absolute_program (tname
) == 0 && (posixly_correct
== 0 || legal_identifier (tname
)))
423 parse_and_execute (temp_string
, tname
, SEVAL_NONINT
|SEVAL_NOHIST
|SEVAL_FUNCDEF
|SEVAL_ONECMD
);
425 free (temp_string
); /* parse_and_execute does this */
427 if (temp_var
= find_function (tname
))
429 VSETATTR (temp_var
, (att_exported
|att_imported
));
430 array_needs_making
= 1;
434 if (temp_var
= bind_invalid_envvar (name
, string
, 0))
436 VSETATTR (temp_var
, (att_exported
| att_imported
| att_invisible
));
437 array_needs_making
= 1;
439 last_command_exit_value
= EXECUTION_FAILURE
;
440 report_error (_("error importing function definition for `%s'"), tname
);
443 /* Restore original suffix */
444 tname
[namelen
] = BASHFUNC_SUFFIX
[0];
447 #endif /* FUNCTION_IMPORT */
448 #if defined (ARRAY_VARS)
450 /* Array variables may not yet be exported. */
451 if (STREQN (BASHARRAY_PREFIX
, name
, BASHARRAY_PREFLEN
) &&
452 STREQN (BASHARRAY_SUFFIX
, name
+ char_index
- BASHARRAY_SUFFLEN
, BASHARRAY_SUFFLEN
) &&
453 *string
== '(' && string
[1] == '[' && string
[strlen (string
) - 1] == ')')
456 char *tname
; /* desired imported array variable name */
458 namelen
= char_index
- BASHARRAY_PREFLEN
- BASHARRAY_SUFFLEN
;
460 tname
= name
+ BASHARRAY_PREFLEN
; /* start of variable name */
461 tname
[namelen
] = '\0'; /* now tname == varname */
464 temp_string
= extract_array_assignment_list (string
, &string_length
);
465 temp_var
= assign_array_from_string (tname
, temp_string
, 0);
467 if (temp_var
&& noassign_p (temp_var
) == 0)
469 VSETATTR (temp_var
, (att_exported
| att_imported
));
470 array_needs_making
= 1;
473 else if (STREQN (BASHASSOC_PREFIX
, name
, BASHASSOC_PREFLEN
) &&
474 STREQN (BASHASSOC_SUFFIX
, name
+ char_index
- BASHASSOC_SUFFLEN
, BASHASSOC_SUFFLEN
) &&
475 *string
== '(' && string
[1] == '[' && string
[strlen (string
) - 1] == ')')
478 char *tname
; /* desired imported assoc variable name */
480 namelen
= char_index
- BASHASSOC_PREFLEN
- BASHASSOC_SUFFLEN
;
482 tname
= name
+ BASHASSOC_PREFLEN
; /* start of variable name */
483 tname
[namelen
] = '\0'; /* now tname == varname */
485 /* need to make sure it exists as an associative array first */
486 temp_var
= find_or_make_array_variable (tname
, 2);
487 if (temp_var
&& noassign_p (temp_var
))
492 temp_string
= extract_array_assignment_list (string
, &string_length
);
493 temp_var
= assign_array_var_from_string (temp_var
, temp_string
, 0);
498 VSETATTR (temp_var
, (att_exported
| att_imported
));
499 array_needs_making
= 1;
503 # endif /* ARRAY_EXPORT */
507 /* If we processed a command-line option that caused SHELLOPTS to be
508 set, it may already be set (and read-only) by the time we process
509 the shell's environment. */
510 if (/* posixly_correct &&*/ STREQ (name
, "SHELLOPTS"))
512 temp_var
= find_variable ("SHELLOPTS");
513 ro
= temp_var
&& readonly_p (temp_var
);
515 VUNSETATTR (temp_var
, att_readonly
);
517 if (legal_identifier (name
))
519 temp_var
= bind_variable (name
, string
, 0);
522 VSETATTR (temp_var
, (att_exported
| att_imported
));
524 VSETATTR (temp_var
, att_readonly
);
529 temp_var
= bind_invalid_envvar (name
, string
, 0);
531 VSETATTR (temp_var
, (att_exported
| att_imported
| att_invisible
));
534 array_needs_making
= 1;
537 name
[char_index
] = '=';
538 /* temp_var can be NULL if it was an exported function with a syntax
539 error (a different bug, but it still shouldn't dump core). */
540 if (temp_var
&& function_p (temp_var
) == 0) /* XXX not yet */
542 CACHE_IMPORTSTR (temp_var
, name
);
548 /* Set up initial value of $_ */
549 temp_var
= set_if_not ("_", dollar_vars
[0]);
551 /* Remember this pid. */
552 dollar_dollar_pid
= getpid ();
554 /* Now make our own defaults in case the vars that we think are
555 important are missing. */
556 temp_var
= set_if_not ("PATH", DEFAULT_PATH_VALUE
);
557 temp_var
= set_if_not ("TERM", "dumb");
559 #if defined (__QNX__)
560 /* set node id -- don't import it from the environment */
563 # if defined (__QNXNTO__)
564 netmgr_ndtostr(ND2S_LOCAL_STR
, ND_LOCAL_NODE
, node_name
, sizeof(node_name
));
566 qnx_nidtostr (getnid (), node_name
, sizeof (node_name
));
568 temp_var
= bind_variable ("NODE", node_name
, 0);
570 set_auto_export (temp_var
);
574 /* set up the prompts. */
575 if (interactive_shell
)
577 #if defined (PROMPT_STRING_DECODE)
578 set_if_not ("PS1", primary_prompt
);
580 if (current_user
.uid
== -1)
581 get_current_user_info ();
582 set_if_not ("PS1", current_user
.euid
== 0 ? "# " : primary_prompt
);
584 set_if_not ("PS2", secondary_prompt
);
587 if (current_user
.euid
== 0)
588 bind_variable ("PS4", "+ ", 0);
590 set_if_not ("PS4", "+ ");
592 /* Don't allow IFS to be imported from the environment. */
593 temp_var
= bind_variable ("IFS", " \t\n", 0);
596 /* Magic machine types. Pretty convenient. */
599 /* Default MAILCHECK for interactive shells. Defer the creation of a
600 default MAILPATH until the startup files are read, because MAIL
601 names a mail file if MAILPATH is not set, and we should provide a
602 default only if neither is set. */
603 if (interactive_shell
)
605 temp_var
= set_if_not ("MAILCHECK", posixly_correct
? "600" : "60");
606 VSETATTR (temp_var
, att_integer
);
609 /* Do some things with shell level. */
610 initialize_shell_level ();
616 /* Initialize the `getopts' stuff. */
617 temp_var
= bind_variable ("OPTIND", "1", 0);
618 VSETATTR (temp_var
, att_integer
);
620 bind_variable ("OPTERR", "1", 0);
623 if (login_shell
== 1 && posixly_correct
== 0)
626 /* Get the full pathname to THIS shell, and set the BASH variable
628 name
= get_bash_name ();
629 temp_var
= bind_variable ("BASH", name
, 0);
632 /* Make the exported environment variable SHELL be the user's login
633 shell. Note that the `tset' command looks at this variable
634 to determine what style of commands to output; if it ends in "csh",
635 then C-shell commands are output, else Bourne shell commands. */
638 /* Make a variable called BASH_VERSION which contains the version info. */
639 bind_variable ("BASH_VERSION", shell_version_string (), 0);
640 #if defined (ARRAY_VARS)
644 if (command_execution_string
)
645 bind_variable ("BASH_EXECUTION_STRING", command_execution_string
, 0);
647 /* Find out if we're supposed to be in Posix.2 mode via an
648 environment variable. */
649 temp_var
= find_variable ("POSIXLY_CORRECT");
651 temp_var
= find_variable ("POSIX_PEDANTIC");
652 if (temp_var
&& imported_p (temp_var
))
653 sv_strict_posix (temp_var
->name
);
655 #if defined (HISTORY)
656 /* Set history variables to defaults, and then do whatever we would
657 do if the variable had just been set. Do this only in the case
658 that we are remembering commands on the history list. */
659 if (remember_on_history
)
661 name
= bash_tilde_expand (posixly_correct
? "~/.sh_history" : "~/.bash_history", 0);
663 set_if_not ("HISTFILE", name
);
668 /* Seed the random number generators. */
672 /* Handle some "special" variables that we may have inherited from a
674 if (interactive_shell
)
676 temp_var
= find_variable ("IGNOREEOF");
678 temp_var
= find_variable ("ignoreeof");
679 if (temp_var
&& imported_p (temp_var
))
680 sv_ignoreeof (temp_var
->name
);
683 #if defined (HISTORY)
684 if (interactive_shell
&& remember_on_history
)
686 sv_history_control ("HISTCONTROL");
687 sv_histignore ("HISTIGNORE");
688 sv_histtimefmt ("HISTTIMEFORMAT");
692 #if defined (READLINE) && defined (STRICT_POSIX)
693 /* POSIXLY_CORRECT will be 1 here if the shell was compiled
694 -DSTRICT_POSIX or if POSIXLY_CORRECT was supplied in the shell's
696 if (interactive_shell
&& posixly_correct
&& no_line_editing
== 0)
697 rl_prefer_env_winsize
= 1;
698 #endif /* READLINE && STRICT_POSIX */
700 /* Get the user's real and effective user ids. */
703 temp_var
= set_if_not ("BASH_LOADABLES_PATH", DEFAULT_LOADABLE_BUILTINS_PATH
);
705 temp_var
= find_variable ("BASH_XTRACEFD");
706 if (temp_var
&& imported_p (temp_var
))
707 sv_xtracefd (temp_var
->name
);
709 sv_shcompat ("BASH_COMPAT");
711 /* Allow FUNCNEST to be inherited from the environment. */
712 sv_funcnest ("FUNCNEST");
714 /* Initialize the dynamic variables, and seed their values. */
715 initialize_dynamic_variables ();
718 /* **************************************************************** */
720 /* Setting values for special shell variables */
722 /* **************************************************************** */
725 set_machine_vars (void)
729 temp_var
= set_if_not ("HOSTTYPE", HOSTTYPE
);
730 temp_var
= set_if_not ("OSTYPE", OSTYPE
);
731 temp_var
= set_if_not ("MACHTYPE", MACHTYPE
);
733 temp_var
= set_if_not ("HOSTNAME", current_host_name
);
736 /* Set $HOME to the information in the password file if we didn't get
737 it from the environment. */
739 /* This function is not static so the tilde and readline libraries can
742 sh_get_home_dir (void)
744 if (current_user
.home_dir
== 0)
745 get_current_user_info ();
746 return current_user
.home_dir
;
754 temp_var
= find_variable ("HOME");
756 temp_var
= bind_variable ("HOME", sh_get_home_dir (), 0);
758 VSETATTR (temp_var
, att_exported
);
762 /* Set $SHELL to the user's login shell if it is not already set. Call
763 get_current_user_info if we haven't already fetched the shell. */
769 temp_var
= find_variable ("SHELL");
772 if (current_user
.shell
== 0)
773 get_current_user_info ();
774 temp_var
= bind_variable ("SHELL", current_user
.shell
, 0);
777 VSETATTR (temp_var
, att_exported
);
786 if ((login_shell
== 1) && RELPATH(shell_name
))
788 if (current_user
.shell
== 0)
789 get_current_user_info ();
790 name
= savestring (current_user
.shell
);
792 else if (ABSPATH(shell_name
))
793 name
= savestring (shell_name
);
794 else if (shell_name
[0] == '.' && shell_name
[1] == '/')
796 /* Fast path for common case. */
800 cdir
= get_string_value ("PWD");
804 name
= (char *)xmalloc (len
+ strlen (shell_name
) + 1);
806 strcpy (name
+ len
, shell_name
+ 1);
809 name
= savestring (shell_name
);
816 tname
= find_user_command (shell_name
);
820 /* Try the current directory. If there is not an executable
821 there, just punt and use the login shell. */
822 s
= file_status (shell_name
);
825 tname
= make_absolute (shell_name
, get_string_value ("PWD"));
826 if (*shell_name
== '.')
828 name
= sh_canonpath (tname
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
839 if (current_user
.shell
== 0)
840 get_current_user_info ();
841 name
= savestring (current_user
.shell
);
846 name
= full_pathname (tname
);
855 adjust_shell_level (int change
)
857 char new_level
[5], *old_SHLVL
;
861 old_SHLVL
= get_string_value ("SHLVL");
862 if (old_SHLVL
== 0 || *old_SHLVL
== '\0' || legal_number (old_SHLVL
, &old_level
) == 0)
865 shell_level
= old_level
+ change
;
868 else if (shell_level
>= 1000)
870 internal_warning (_("shell level (%d) too high, resetting to 1"), shell_level
);
874 /* We don't need the full generality of itos here. */
875 if (shell_level
< 10)
877 new_level
[0] = shell_level
+ '0';
880 else if (shell_level
< 100)
882 new_level
[0] = (shell_level
/ 10) + '0';
883 new_level
[1] = (shell_level
% 10) + '0';
886 else if (shell_level
< 1000)
888 new_level
[0] = (shell_level
/ 100) + '0';
889 old_level
= shell_level
% 100;
890 new_level
[1] = (old_level
/ 10) + '0';
891 new_level
[2] = (old_level
% 10) + '0';
895 temp_var
= bind_variable ("SHLVL", new_level
, 0);
896 set_auto_export (temp_var
);
900 initialize_shell_level (void)
902 adjust_shell_level (1);
905 /* If we got PWD from the environment, update our idea of the current
906 working directory. In any case, make sure that PWD exists before
907 checking it. It is possible for getcwd () to fail on shell startup,
908 and in that case, PWD would be undefined. If this is an interactive
909 login shell, see if $HOME is the current working directory, and if
910 that's not the same string as $PWD, set PWD=$HOME. */
915 SHELL_VAR
*temp_var
, *home_var
;
916 char *temp_string
, *home_string
, *current_dir
;
918 home_var
= find_variable ("HOME");
919 home_string
= home_var
? value_cell (home_var
) : (char *)NULL
;
921 temp_var
= find_variable ("PWD");
922 /* Follow posix rules for importing PWD */
923 if (temp_var
&& imported_p (temp_var
) &&
924 (temp_string
= value_cell (temp_var
)) &&
925 temp_string
[0] == '/' &&
926 same_file (temp_string
, ".", (struct stat
*)NULL
, (struct stat
*)NULL
))
928 current_dir
= sh_canonpath (temp_string
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
929 if (current_dir
== 0)
930 current_dir
= get_working_directory ("shell_init");
932 set_working_directory (current_dir
);
933 if (posixly_correct
&& current_dir
)
935 temp_var
= bind_variable ("PWD", current_dir
, 0);
936 set_auto_export (temp_var
);
940 else if (home_string
&& interactive_shell
&& login_shell
&&
941 same_file (home_string
, ".", (struct stat
*)NULL
, (struct stat
*)NULL
))
943 set_working_directory (home_string
);
944 temp_var
= bind_variable ("PWD", home_string
, 0);
945 set_auto_export (temp_var
);
949 temp_string
= get_working_directory ("shell-init");
952 temp_var
= bind_variable ("PWD", temp_string
, 0);
953 set_auto_export (temp_var
);
958 /* According to the Single Unix Specification, v2, $OLDPWD is an
959 `environment variable' and therefore should be auto-exported. If we
960 don't find OLDPWD in the environment, or it doesn't name a directory,
961 make a dummy invisible variable for OLDPWD, and mark it as exported. */
962 temp_var
= find_variable ("OLDPWD");
963 #if defined (OLDPWD_CHECK_DIRECTORY)
964 if (temp_var
== 0 || value_cell (temp_var
) == 0 || file_isdir (value_cell (temp_var
)) == 0)
966 if (temp_var
== 0 || value_cell (temp_var
) == 0)
969 temp_var
= bind_variable ("OLDPWD", (char *)NULL
, 0);
970 VSETATTR (temp_var
, (att_exported
| att_invisible
));
974 /* Make a variable $PPID, which holds the pid of the shell's parent. */
978 char namebuf
[INT_STRLEN_BOUND(pid_t
) + 1], *name
;
981 name
= inttostr (getppid (), namebuf
, sizeof(namebuf
));
982 temp_var
= find_variable ("PPID");
984 VUNSETATTR (temp_var
, (att_readonly
| att_exported
));
985 temp_var
= bind_variable ("PPID", name
, 0);
986 VSETATTR (temp_var
, (att_readonly
| att_integer
));
992 char buff
[INT_STRLEN_BOUND(uid_t
) + 1], *b
;
993 register SHELL_VAR
*v
;
995 b
= inttostr (current_user
.uid
, buff
, sizeof (buff
));
996 v
= find_variable ("UID");
999 v
= bind_variable ("UID", b
, 0);
1000 VSETATTR (v
, (att_readonly
| att_integer
));
1003 if (current_user
.euid
!= current_user
.uid
)
1004 b
= inttostr (current_user
.euid
, buff
, sizeof (buff
));
1006 v
= find_variable ("EUID");
1009 v
= bind_variable ("EUID", b
, 0);
1010 VSETATTR (v
, (att_readonly
| att_integer
));
1014 #if defined (ARRAY_VARS)
1016 make_vers_array (void)
1020 char *s
, d
[32], b
[INT_STRLEN_BOUND(int) + 1];
1022 unbind_variable_noref ("BASH_VERSINFO");
1024 vv
= make_new_array_variable ("BASH_VERSINFO");
1025 av
= array_cell (vv
);
1026 strcpy (d
, dist_version
);
1027 s
= strchr (d
, '.');
1030 array_insert (av
, 0, d
);
1031 array_insert (av
, 1, s
);
1032 s
= inttostr (patch_level
, b
, sizeof (b
));
1033 array_insert (av
, 2, s
);
1034 s
= inttostr (build_version
, b
, sizeof (b
));
1035 array_insert (av
, 3, s
);
1036 array_insert (av
, 4, release_status
);
1037 array_insert (av
, 5, MACHTYPE
);
1039 VSETATTR (vv
, att_readonly
);
1041 #endif /* ARRAY_VARS */
1043 /* Set the environment variables $LINES and $COLUMNS in response to
1044 a window size change. */
1046 sh_set_lines_and_columns (int lines
, int cols
)
1048 char val
[INT_STRLEN_BOUND(int) + 1], *v
;
1050 #if defined (READLINE)
1051 /* If we are currently assigning to LINES or COLUMNS, don't do anything. */
1052 if (winsize_assignment
)
1056 v
= inttostr (lines
, val
, sizeof (val
));
1057 bind_variable ("LINES", v
, 0);
1059 v
= inttostr (cols
, val
, sizeof (val
));
1060 bind_variable ("COLUMNS", v
, 0);
1063 /* **************************************************************** */
1065 /* Printing variables and values */
1067 /* **************************************************************** */
1069 /* Print LIST (a list of shell variables) to stdout in such a way that
1070 they can be read back in. */
1072 print_var_list (SHELL_VAR
**list
)
1075 register SHELL_VAR
*var
;
1077 for (i
= 0; list
&& (var
= list
[i
]); i
++)
1078 if (invisible_p (var
) == 0)
1079 print_assignment (var
);
1082 /* Print LIST (a list of shell functions) to stdout in such a way that
1083 they can be read back in. */
1085 print_func_list (SHELL_VAR
**list
)
1088 register SHELL_VAR
*var
;
1090 for (i
= 0; list
&& (var
= list
[i
]); i
++)
1092 printf ("%s ", var
->name
);
1093 print_var_function (var
);
1098 /* Print the value of a single SHELL_VAR. No newline is
1099 output, but the variable is printed in such a way that
1100 it can be read back in. */
1102 print_assignment (SHELL_VAR
*var
)
1104 if (var_isset (var
) == 0)
1107 if (function_p (var
))
1109 printf ("%s", var
->name
);
1110 print_var_function (var
);
1113 #if defined (ARRAY_VARS)
1114 else if (array_p (var
))
1115 print_array_assignment (var
, 0);
1116 else if (assoc_p (var
))
1117 print_assoc_assignment (var
, 0);
1118 #endif /* ARRAY_VARS */
1121 printf ("%s=", var
->name
);
1122 print_var_value (var
, 1);
1127 /* Print the value cell of VAR, a shell variable. Do not print
1128 the name, nor leading/trailing newline. If QUOTE is non-zero,
1129 and the value contains shell metacharacters, quote the value
1130 in such a way that it can be read back in. */
1132 print_var_value (SHELL_VAR
*var
, int quote
)
1136 if (var_isset (var
) == 0)
1139 if (quote
&& posixly_correct
== 0 && ansic_shouldquote (value_cell (var
)))
1141 t
= ansic_quote (value_cell (var
), 0, (int *)0);
1145 else if (quote
&& sh_contains_shell_metas (value_cell (var
)))
1147 t
= sh_single_quote (value_cell (var
));
1152 printf ("%s", value_cell (var
));
1155 /* Print the function cell of VAR, a shell variable. Do not
1156 print the name, nor leading/trailing newline. */
1158 print_var_function (SHELL_VAR
*var
)
1162 if (function_p (var
) && var_isset (var
))
1164 x
= named_function_string ((char *)NULL
, function_cell(var
), FUNC_MULTILINE
|FUNC_EXTERNAL
);
1169 /* **************************************************************** */
1171 /* Dynamic Variables */
1173 /* **************************************************************** */
1175 /* DYNAMIC VARIABLES
1177 These are variables whose values are generated anew each time they are
1178 referenced. These are implemented using a pair of function pointers
1179 in the struct variable: assign_func, which is called from bind_variable
1180 and, if arrays are compiled into the shell, some of the functions in
1181 arrayfunc.c, and dynamic_value, which is called from find_variable.
1183 assign_func is called from bind_variable_internal, if
1184 bind_variable_internal discovers that the variable being assigned to
1185 has such a function. The function is called as
1186 SHELL_VAR *temp = (*(entry->assign_func)) (entry, value, ind)
1187 and the (SHELL_VAR *)temp is returned as the value of bind_variable. It
1188 is usually ENTRY (self). IND is an index for an array variable, and
1191 dynamic_value is called from find_variable_internal to return a `new'
1192 value for the specified dynamic variable. If this function is NULL,
1193 the variable is treated as a `normal' shell variable. If it is not,
1194 however, then this function is called like this:
1195 tempvar = (*(var->dynamic_value)) (var);
1197 Sometimes `tempvar' will replace the value of `var'. Other times, the
1198 shell will simply use the string value. Pretty object-oriented, huh?
1200 Be warned, though: if you `unset' a special variable, it loses its
1201 special meaning, even if you subsequently set it.
1203 The special assignment code would probably have been better put in
1204 subst.c: do_assignment_internal, in the same style as
1205 stupidly_hack_special_variables, but I wanted the changes as
1206 localized as possible. */
1208 #define INIT_DYNAMIC_VAR(var, val, gfunc, afunc) \
1211 v = bind_variable (var, (val), 0); \
1212 v->dynamic_value = gfunc; \
1213 v->assign_func = afunc; \
1217 #define INIT_DYNAMIC_ARRAY_VAR(var, gfunc, afunc) \
1220 v = make_new_array_variable (var); \
1221 v->dynamic_value = gfunc; \
1222 v->assign_func = afunc; \
1226 #define INIT_DYNAMIC_ASSOC_VAR(var, gfunc, afunc) \
1229 v = make_new_assoc_variable (var); \
1230 v->dynamic_value = gfunc; \
1231 v->assign_func = afunc; \
1236 null_assign (SHELL_VAR
*self
, char *value
, arrayind_t unused
, char *key
)
1241 #if defined (ARRAY_VARS)
1243 null_array_assign (SHELL_VAR
*self
, char *value
, arrayind_t ind
, char *key
)
1249 /* Degenerate `dynamic_value' function; just returns what's passed without
1252 get_self (SHELL_VAR
*self
)
1257 #if defined (ARRAY_VARS)
1258 /* A generic dynamic array variable initializer. Initialize array variable
1259 NAME with dynamic value function GETFUNC and assignment function SETFUNC. */
1261 init_dynamic_array_var (char *name
, sh_var_value_func_t
*getfunc
, sh_var_assign_func_t
*setfunc
, int attrs
)
1265 v
= find_variable (name
);
1268 INIT_DYNAMIC_ARRAY_VAR (name
, getfunc
, setfunc
);
1270 VSETATTR (v
, attrs
);
1275 init_dynamic_assoc_var (char *name
, sh_var_value_func_t
*getfunc
, sh_var_assign_func_t
*setfunc
, int attrs
)
1279 v
= find_variable (name
);
1282 INIT_DYNAMIC_ASSOC_VAR (name
, getfunc
, setfunc
);
1284 VSETATTR (v
, attrs
);
1289 /* Set the string value of VAR to the string representation of VALUE.
1290 Right now this takes an INTMAX_T because that's what itos needs. If
1291 FLAGS&1, we force the integer attribute on. */
1292 static inline SHELL_VAR
*
1293 set_int_value (SHELL_VAR
*var
, intmax_t value
, int flags
)
1298 FREE (value_cell (var
));
1299 var_setvalue (var
, p
);
1301 VSETATTR (var
, att_integer
);
1305 static inline SHELL_VAR
*
1306 set_string_value (SHELL_VAR
*var
, const char *value
, int flags
)
1310 if (value
&& *value
)
1311 p
= savestring (value
);
1314 p
= (char *)xmalloc (1);
1317 FREE (value_cell (var
));
1318 var_setvalue (var
, p
);
1322 /* The value of $SECONDS. This is the number of seconds since shell
1323 invocation, or, the number of seconds since the last assignment + the
1324 value of the last assignment. */
1325 static intmax_t seconds_value_assigned
;
1328 assign_seconds (SHELL_VAR
*self
, char *value
, arrayind_t unused
, char *key
)
1333 if (integer_p (self
))
1334 nval
= evalexp (value
, 0, &expok
);
1336 expok
= legal_number (value
, &nval
);
1337 seconds_value_assigned
= expok
? nval
: 0;
1338 gettimeofday (&shellstart
, NULL
);
1339 shell_start_time
= shellstart
.tv_sec
;
1340 return (set_int_value (self
, nval
, integer_p (self
) != 0));
1344 get_seconds (SHELL_VAR
*var
)
1346 time_t time_since_start
;
1349 gettimeofday(&tv
, NULL
);
1350 time_since_start
= tv
.tv_sec
- shell_start_time
;
1351 return (set_int_value (var
, seconds_value_assigned
+ time_since_start
, 1));
1355 init_seconds_var (void)
1359 v
= find_variable ("SECONDS");
1362 if (legal_number (value_cell(v
), &seconds_value_assigned
) == 0)
1363 seconds_value_assigned
= 0;
1365 INIT_DYNAMIC_VAR ("SECONDS", (v
? value_cell (v
) : (char *)NULL
), get_seconds
, assign_seconds
);
1369 /* Functions for BASH_MONOSECONDS */
1371 get_monoseconds (SHELL_VAR
*self
)
1377 #if defined (HAVE_CLOCK_GETTIME) && defined (CLOCK_MONOTONIC)
1380 ret
= clock_gettime (CLOCK_MONOTONIC
, &ts
);
1384 return (set_int_value (self
, nval
, integer_p (self
) != 0));
1388 /* Fall back to gettimeofday if clock_gettime not available or fails */
1389 ret
= gettimeofday (&tv
, NULL
);
1391 return (set_int_value (self
, nval
, integer_p (self
) != 0));
1394 /* Functions for $RANDOM and $SRANDOM */
1396 int last_random_value
;
1397 static int seeded_subshell
= 0;
1400 assign_random (SHELL_VAR
*self
, char *value
, arrayind_t unused
, char *key
)
1405 if (integer_p (self
))
1406 seedval
= evalexp (value
, 0, &expok
);
1408 expok
= legal_number (value
, &seedval
);
1412 if (subshell_environment
)
1413 seeded_subshell
= getpid ();
1414 return (set_int_value (self
, seedval
, integer_p (self
) != 0));
1418 get_random_number (void)
1422 /* Reset for command and process substitution. */
1424 if (subshell_environment
&& seeded_subshell
!= pid
)
1427 seeded_subshell
= pid
;
1432 while (rv
== last_random_value
);
1434 return (last_random_value
= rv
);
1438 get_random (SHELL_VAR
*var
)
1442 rv
= get_random_number ();
1443 return (set_int_value (var
, rv
, 1));
1447 get_urandom (SHELL_VAR
*var
)
1451 rv
= get_urandom32 ();
1452 return (set_int_value (var
, rv
, 1));
1456 assign_lineno (SHELL_VAR
*var
, char *value
, arrayind_t unused
, char *key
)
1460 if (value
== 0 || *value
== '\0' || legal_number (value
, &new_value
) == 0)
1462 line_number
= line_number_base
= new_value
;
1463 return (set_int_value (var
, line_number
, integer_p (var
) != 0));
1466 /* Function which returns the current line number. */
1468 get_lineno (SHELL_VAR
*var
)
1472 ln
= executing_line_number ();
1473 return (set_int_value (var
, ln
, 0));
1477 assign_subshell (SHELL_VAR
*var
, char *value
, arrayind_t unused
, char *key
)
1481 if (value
== 0 || *value
== '\0' || legal_number (value
, &new_value
) == 0)
1483 subshell_level
= new_value
;
1488 get_subshell (SHELL_VAR
*var
)
1490 return (set_int_value (var
, subshell_level
, 0));
1494 get_epochseconds (SHELL_VAR
*var
)
1499 return (set_int_value (var
, now
, 0));
1503 get_epochrealtime (SHELL_VAR
*var
)
1508 gettimeofday (&tv
, NULL
);
1509 snprintf (buf
, sizeof (buf
), "%u%c%06u", (unsigned)tv
.tv_sec
,
1511 (unsigned)tv
.tv_usec
);
1513 return (set_string_value (var
, buf
, 0));
1517 get_bashpid (SHELL_VAR
*var
)
1522 return (set_int_value (var
, pid
, 1));
1526 get_bash_argv0 (SHELL_VAR
*var
)
1528 return (set_string_value (var
, dollar_vars
[0], 0));
1531 static char *static_shell_name
= 0;
1534 assign_bash_argv0 (SHELL_VAR
*var
, char *value
, arrayind_t unused
, char *key
)
1541 FREE (dollar_vars
[0]);
1542 dollar_vars
[0] = savestring (value
);
1544 /* Need these gyrations because shell_name isn't dynamically allocated */
1545 vlen
= STRLEN (value
);
1546 static_shell_name
= xrealloc (static_shell_name
, vlen
+ 1);
1547 strcpy (static_shell_name
, value
);
1549 shell_name
= static_shell_name
;
1558 v
= find_variable ("BASH_ARGV0");
1559 if (v
&& imported_p (v
))
1560 assign_bash_argv0 (v
, value_cell (v
), 0, 0);
1564 get_bash_command (SHELL_VAR
*var
)
1568 p
= the_printed_command_except_trap
? the_printed_command_except_trap
: "";
1569 return (set_string_value (var
, p
, 0));
1572 #if defined (HISTORY)
1574 get_histcmd (SHELL_VAR
*var
)
1578 /* Do the same adjustment here we do in parse.y:prompt_history_number,
1579 assuming that we are in one of two states: decoding this as part of
1580 the prompt string, in which case we do not want to assume that the
1581 command has been saved to the history and the history number incremented,
1582 or the expansion is part of the current command being executed and has
1583 already been saved to history and the history number incremented.
1584 Right now we use EXECUTING as the determinant. */
1585 n
= history_number () - executing
;
1586 return (set_int_value (var
, n
, 0));
1590 #if defined (READLINE)
1591 /* When this function returns, VAR->value points to malloced memory. */
1593 get_comp_wordbreaks (SHELL_VAR
*var
)
1595 /* If we don't have anything yet, assign a default value. */
1596 if (rl_completer_word_break_characters
== 0 && bash_readline_initialized
== 0)
1597 enable_hostname_completion (perform_hostname_completion
);
1599 return (set_string_value (var
, rl_completer_word_break_characters
, 0));
1602 /* When this function returns, rl_completer_word_break_characters points to
1605 assign_comp_wordbreaks (SHELL_VAR
*self
, char *value
, arrayind_t unused
, char *key
)
1607 if (rl_completer_word_break_characters
&&
1608 rl_completer_word_break_characters
!= rl_basic_word_break_characters
)
1609 free ((void *)rl_completer_word_break_characters
);
1611 rl_completer_word_break_characters
= savestring (value
);
1614 #endif /* READLINE */
1616 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
1618 assign_dirstack (SHELL_VAR
*self
, char *value
, arrayind_t ind
, char *key
)
1620 set_dirstack_element (ind
, 1, value
);
1625 get_dirstack (SHELL_VAR
*self
)
1630 l
= get_directory_stack (0);
1631 a
= array_from_word_list (l
);
1632 array_dispose (array_cell (self
));
1634 var_setarray (self
, a
);
1637 #endif /* PUSHD AND POPD && ARRAY_VARS */
1639 #if defined (ARRAY_VARS)
1640 /* We don't want to initialize the group set with a call to getgroups()
1641 unless we're asked to, but we only want to do it once. */
1643 get_groupset (SHELL_VAR
*self
)
1648 static char **group_set
= (char **)NULL
;
1652 group_set
= get_group_list (&ng
);
1653 a
= array_cell (self
);
1654 for (i
= 0; i
< ng
; i
++)
1655 array_insert (a
, i
, group_set
[i
]);
1660 # if defined (DEBUGGER)
1662 get_bashargcv (SHELL_VAR
*self
)
1664 static int self_semaphore
= 0;
1666 /* Backwards compatibility: if we refer to BASH_ARGV or BASH_ARGC at the
1667 top level without enabling debug mode, and we don't have an instance
1668 of the variable set, initialize the arg arrays.
1669 This will already have been done if debugging_mode != 0. */
1670 if (self_semaphore
== 0 && variable_context
== 0 && debugging_mode
== 0) /* don't do it for shell functions */
1681 build_hashcmd (SHELL_VAR
*self
)
1686 BUCKET_CONTENTS
*item
;
1688 h
= assoc_cell (self
);
1692 if (hashed_filenames
== 0 || HASH_ENTRIES (hashed_filenames
) == 0)
1694 var_setvalue (self
, (char *)NULL
);
1698 h
= assoc_create (hashed_filenames
->nbuckets
);
1699 for (i
= 0; i
< hashed_filenames
->nbuckets
; i
++)
1701 for (item
= hash_items (i
, hashed_filenames
); item
; item
= item
->next
)
1703 k
= savestring (item
->key
);
1704 v
= pathdata(item
)->path
;
1705 assoc_insert (h
, k
, v
);
1709 var_setvalue (self
, (char *)h
);
1714 get_hashcmd (SHELL_VAR
*self
)
1716 build_hashcmd (self
);
1721 assign_hashcmd (SHELL_VAR
*self
, char *value
, arrayind_t ind
, char *key
)
1723 #if defined (RESTRICTED_SHELL)
1728 if (strchr (value
, '/'))
1730 sh_restricted (value
);
1731 return (SHELL_VAR
*)NULL
;
1733 /* If we are changing the hash table in a restricted shell, make sure the
1734 target pathname can be found using a $PATH search. */
1735 full_path
= find_user_command (value
);
1736 if (full_path
== 0 || *full_path
== 0 || executable_file (full_path
) == 0)
1738 sh_notfound (value
);
1740 return ((SHELL_VAR
*)NULL
);
1745 phash_insert (key
, value
, 0, 0);
1746 return (build_hashcmd (self
));
1751 build_aliasvar (SHELL_VAR
*self
)
1756 BUCKET_CONTENTS
*item
;
1758 h
= assoc_cell (self
);
1762 if (aliases
== 0 || HASH_ENTRIES (aliases
) == 0)
1764 var_setvalue (self
, (char *)NULL
);
1768 h
= assoc_create (aliases
->nbuckets
);
1769 for (i
= 0; i
< aliases
->nbuckets
; i
++)
1771 for (item
= hash_items (i
, aliases
); item
; item
= item
->next
)
1773 k
= savestring (item
->key
);
1774 v
= ((alias_t
*)(item
->data
))->value
;
1775 assoc_insert (h
, k
, v
);
1779 var_setvalue (self
, (char *)h
);
1784 get_aliasvar (SHELL_VAR
*self
)
1786 build_aliasvar (self
);
1791 assign_aliasvar (SHELL_VAR
*self
, char *value
, arrayind_t ind
, char *key
)
1793 if (legal_alias_name (key
, 0) == 0)
1795 report_error (_("`%s': invalid alias name"), key
);
1798 add_alias (key
, value
);
1799 return (build_aliasvar (self
));
1803 #endif /* ARRAY_VARS */
1805 /* If ARRAY_VARS is not defined, this just returns the name of any
1806 currently-executing function. If we have arrays, it's a call stack. */
1808 get_funcname (SHELL_VAR
*self
)
1810 #if ! defined (ARRAY_VARS)
1811 if (variable_context
&& this_shell_function
)
1812 return (set_string_value (self
, this_shell_function
->name
, 0));
1818 make_funcname_visible (int on_or_off
)
1822 v
= find_variable ("FUNCNAME");
1823 if (v
== 0 || v
->dynamic_value
== 0)
1827 VUNSETATTR (v
, att_invisible
);
1829 VSETATTR (v
, att_invisible
);
1833 init_funcname_var (void)
1837 v
= find_variable ("FUNCNAME");
1840 #if defined (ARRAY_VARS)
1841 INIT_DYNAMIC_ARRAY_VAR ("FUNCNAME", get_funcname
, null_array_assign
);
1843 INIT_DYNAMIC_VAR ("FUNCNAME", (char *)NULL
, get_funcname
, null_assign
);
1845 VSETATTR (v
, att_invisible
|att_noassign
);
1850 initialize_dynamic_variables (void)
1854 v
= init_seconds_var ();
1856 INIT_DYNAMIC_VAR ("BASH_ARGV0", (char *)NULL
, get_bash_argv0
, assign_bash_argv0
);
1858 INIT_DYNAMIC_VAR ("BASH_COMMAND", (char *)NULL
, get_bash_command
, (sh_var_assign_func_t
*)NULL
);
1859 INIT_DYNAMIC_VAR ("BASH_SUBSHELL", (char *)NULL
, get_subshell
, assign_subshell
);
1861 INIT_DYNAMIC_VAR ("BASH_MONOSECONDS", (char *)NULL
, get_monoseconds
, (sh_var_assign_func_t
*)NULL
);
1863 INIT_DYNAMIC_VAR ("RANDOM", (char *)NULL
, get_random
, assign_random
);
1864 VSETATTR (v
, att_integer
);
1865 INIT_DYNAMIC_VAR ("SRANDOM", (char *)NULL
, get_urandom
, (sh_var_assign_func_t
*)NULL
);
1866 VSETATTR (v
, att_integer
);
1867 INIT_DYNAMIC_VAR ("LINENO", (char *)NULL
, get_lineno
, assign_lineno
);
1868 VSETATTR (v
, att_regenerate
);
1870 INIT_DYNAMIC_VAR ("BASHPID", (char *)NULL
, get_bashpid
, null_assign
);
1871 VSETATTR (v
, att_integer
);
1873 INIT_DYNAMIC_VAR ("EPOCHSECONDS", (char *)NULL
, get_epochseconds
, null_assign
);
1874 VSETATTR (v
, att_regenerate
);
1875 INIT_DYNAMIC_VAR ("EPOCHREALTIME", (char *)NULL
, get_epochrealtime
, null_assign
);
1876 VSETATTR (v
, att_regenerate
);
1878 #if defined (HISTORY)
1879 INIT_DYNAMIC_VAR ("HISTCMD", (char *)NULL
, get_histcmd
, (sh_var_assign_func_t
*)NULL
);
1880 VSETATTR (v
, att_integer
);
1883 #if defined (READLINE)
1884 INIT_DYNAMIC_VAR ("COMP_WORDBREAKS", (char *)NULL
, get_comp_wordbreaks
, assign_comp_wordbreaks
);
1887 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
1888 v
= init_dynamic_array_var ("DIRSTACK", get_dirstack
, assign_dirstack
, 0);
1889 #endif /* PUSHD_AND_POPD && ARRAY_VARS */
1891 #if defined (ARRAY_VARS)
1892 v
= init_dynamic_array_var ("GROUPS", get_groupset
, null_array_assign
, att_noassign
);
1894 # if defined (DEBUGGER)
1895 v
= init_dynamic_array_var ("BASH_ARGC", get_bashargcv
, null_array_assign
, att_noassign
|att_nounset
);
1896 v
= init_dynamic_array_var ("BASH_ARGV", get_bashargcv
, null_array_assign
, att_noassign
|att_nounset
);
1897 # endif /* DEBUGGER */
1898 v
= init_dynamic_array_var ("BASH_SOURCE", get_self
, null_array_assign
, att_noassign
|att_nounset
);
1899 v
= init_dynamic_array_var ("BASH_LINENO", get_self
, null_array_assign
, att_noassign
|att_nounset
);
1901 v
= init_dynamic_assoc_var ("BASH_CMDS", get_hashcmd
, assign_hashcmd
, att_nofree
);
1902 # if defined (ALIAS)
1903 v
= init_dynamic_assoc_var ("BASH_ALIASES", get_aliasvar
, assign_aliasvar
, att_nofree
);
1907 v
= init_funcname_var ();
1910 /* **************************************************************** */
1912 /* Retrieving variables and values */
1914 /* **************************************************************** */
1918 var_isset (SHELL_VAR
*var
)
1920 return (var
->value
!= 0);
1924 var_isunset (SHELL_VAR
*var
)
1926 return (var
->value
== 0);
1930 /* How to get a pointer to the shell variable or function named NAME.
1931 HASHED_VARS is a pointer to the hash table containing the list
1932 of interest (either variables or functions). */
1935 hash_lookup (const char *name
, HASH_TABLE
*hashed_vars
)
1937 BUCKET_CONTENTS
*bucket
;
1939 bucket
= hash_search (name
, hashed_vars
, 0);
1940 /* If we find the name in HASHED_VARS, set LAST_TABLE_SEARCHED to that
1943 last_table_searched
= hashed_vars
;
1944 return (bucket
? (SHELL_VAR
*)bucket
->data
: (SHELL_VAR
*)NULL
);
1948 var_lookup (const char *name
, VAR_CONTEXT
*vcontext
)
1953 v
= (SHELL_VAR
*)NULL
;
1954 for (vc
= vcontext
; vc
; vc
= vc
->down
)
1955 if (v
= hash_lookup (name
, vc
->table
))
1961 /* Look up the variable entry named NAME. If SEARCH_TEMPENV is non-zero,
1962 then also search the temporarily built list of exported variables.
1963 The lookup order is:
1965 shell_variables list
1969 find_variable_internal (const char *name
, int flags
)
1972 int search_tempenv
, force_tempenv
;
1975 var
= (SHELL_VAR
*)NULL
;
1977 force_tempenv
= (flags
& FV_FORCETEMPENV
);
1979 /* If explicitly requested, first look in the temporary environment for
1980 the variable. This allows constructs such as "foo=x eval 'echo $foo'"
1981 to get the `exported' value of $foo. This happens if we are executing
1982 a function or builtin, or if we are looking up a variable in a
1983 "subshell environment". */
1984 search_tempenv
= force_tempenv
|| (expanding_redir
== 0 && subshell_environment
);
1986 if (search_tempenv
&& temporary_env
)
1987 var
= hash_lookup (name
, temporary_env
);
1991 if ((flags
& FV_SKIPINVISIBLE
) == 0)
1992 var
= var_lookup (name
, shell_variables
);
1995 /* essentially var_lookup expanded inline so we can check for
1997 for (vc
= shell_variables
; vc
; vc
= vc
->down
)
1999 var
= hash_lookup (name
, vc
->table
);
2000 if (var
&& invisible_p (var
))
2009 return ((SHELL_VAR
*)NULL
);
2011 return (var
->dynamic_value
? (*(var
->dynamic_value
)) (var
) : var
);
2014 /* Look up and resolve the chain of nameref variables starting at V all the
2015 way to NULL or non-nameref. */
2017 find_variable_nameref (SHELL_VAR
*v
)
2021 SHELL_VAR
*orig
, *oldv
;
2025 while (v
&& nameref_p (v
))
2028 if (level
> NAMEREF_MAX
)
2029 return (&nameref_maxloop_value
);
2030 newname
= nameref_cell (v
);
2031 if (newname
== 0 || *newname
== '\0')
2032 return ((SHELL_VAR
*)0);
2035 if (expanding_redir
== 0 && (assigning_in_environment
|| executing_builtin
))
2036 flags
|= FV_FORCETEMPENV
;
2037 /* We don't handle array subscripts here. */
2038 v
= find_variable_internal (newname
, flags
);
2039 if (v
== orig
|| v
== oldv
)
2041 internal_warning (_("%s: circular name reference"), orig
->name
);
2043 /* XXX - provisional change - circular refs go to
2044 global scope for resolution, without namerefs. */
2045 if (variable_context
&& v
->context
)
2046 return (find_global_variable_noref (v
->name
));
2049 return ((SHELL_VAR
*)0);
2055 /* Resolve the chain of nameref variables for NAME. XXX - could change later */
2057 find_variable_last_nameref (const char *name
, int vflags
)
2063 nv
= v
= find_variable_noref (name
);
2065 while (v
&& nameref_p (v
))
2068 if (level
> NAMEREF_MAX
)
2069 return ((SHELL_VAR
*)0); /* error message here? */
2070 newname
= nameref_cell (v
);
2071 if (newname
== 0 || *newname
== '\0')
2072 return ((vflags
&& invisible_p (v
)) ? v
: (SHELL_VAR
*)0);
2075 if (expanding_redir
== 0 && (assigning_in_environment
|| executing_builtin
))
2076 flags
|= FV_FORCETEMPENV
;
2077 /* We don't accommodate array subscripts here. */
2078 v
= find_variable_internal (newname
, flags
);
2083 /* Resolve the chain of nameref variables for NAME. XXX - could change later */
2085 find_global_variable_last_nameref (const char *name
, int vflags
)
2091 nv
= v
= find_global_variable_noref (name
);
2093 while (v
&& nameref_p (v
))
2096 if (level
> NAMEREF_MAX
)
2097 return ((SHELL_VAR
*)0); /* error message here? */
2098 newname
= nameref_cell (v
);
2099 if (newname
== 0 || *newname
== '\0')
2100 return ((vflags
&& invisible_p (v
)) ? v
: (SHELL_VAR
*)0);
2102 /* We don't accommodate array subscripts here. */
2103 v
= find_global_variable_noref (newname
);
2109 find_nameref_at_context (SHELL_VAR
*v
, VAR_CONTEXT
*vc
)
2111 SHELL_VAR
*nv
, *nv2
;
2117 while (nv
&& nameref_p (nv
))
2120 if (level
> NAMEREF_MAX
)
2121 return (&nameref_maxloop_value
);
2122 newname
= nameref_cell (nv
);
2123 if (newname
== 0 || *newname
== '\0')
2124 return ((SHELL_VAR
*)NULL
);
2125 nv2
= hash_lookup (newname
, vc
->table
);
2133 /* Do nameref resolution from the VC, which is the local context for some
2134 function or builtin, `up' the chain to the global variables context. If
2135 NVCP is not NULL, return the variable context where we finally ended the
2136 nameref resolution (so the bind_variable_internal can use the correct
2137 variable context and hash table). */
2139 find_variable_nameref_context (SHELL_VAR
*v
, VAR_CONTEXT
*vc
, VAR_CONTEXT
**nvcp
)
2141 SHELL_VAR
*nv
, *nv2
;
2144 /* Look starting at the current context all the way `up' */
2145 for (nv
= v
, nvc
= vc
; nvc
; nvc
= nvc
->down
)
2147 nv2
= find_nameref_at_context (nv
, nvc
);
2148 if (nv2
== &nameref_maxloop_value
)
2149 return (nv2
); /* XXX */
2155 if (nameref_p (nv
) == 0)
2158 return (nameref_p (nv
) ? (SHELL_VAR
*)NULL
: nv
);
2161 /* Do nameref resolution from the VC, which is the local context for some
2162 function or builtin, `up' the chain to the global variables context. If
2163 NVCP is not NULL, return the variable context where we finally ended the
2164 nameref resolution (so the bind_variable_internal can use the correct
2165 variable context and hash table). */
2167 find_variable_last_nameref_context (SHELL_VAR
*v
, VAR_CONTEXT
*vc
, VAR_CONTEXT
**nvcp
)
2169 SHELL_VAR
*nv
, *nv2
;
2172 /* Look starting at the current context all the way `up' */
2173 for (nv
= v
, nvc
= vc
; nvc
; nvc
= nvc
->down
)
2175 nv2
= find_nameref_at_context (nv
, nvc
);
2176 if (nv2
== &nameref_maxloop_value
)
2177 return (nv2
); /* XXX */
2184 return (nameref_p (nv
) ? nv
: (SHELL_VAR
*)NULL
);
2188 find_variable_nameref_for_create (const char *name
, int flags
)
2192 /* See if we have a nameref pointing to a variable that hasn't been
2194 var
= find_variable_last_nameref (name
, 1);
2195 if (var
== &nameref_maxloop_value
)
2197 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
2198 return ((SHELL_VAR
*)INVALID_NAMEREF_VALUE
);
2200 if ((flags
&1) && var
&& nameref_p (var
) && invisible_p (var
))
2202 internal_warning (_("%s: removing nameref attribute"), name
);
2203 VUNSETATTR (var
, att_nameref
);
2205 if (var
&& nameref_p (var
))
2207 if (legal_identifier (nameref_cell (var
)) == 0)
2209 sh_invalidid (nameref_cell (var
) ? nameref_cell (var
) : "");
2210 return ((SHELL_VAR
*)INVALID_NAMEREF_VALUE
);
2217 find_variable_nameref_for_assignment (const char *name
, int flags
)
2221 /* See if we have a nameref pointing to a variable that hasn't been
2223 var
= find_variable_last_nameref (name
, 1);
2224 if (var
== &nameref_maxloop_value
)
2226 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
2227 return ((SHELL_VAR
*)INVALID_NAMEREF_VALUE
);
2229 if (var
&& nameref_p (var
) && invisible_p (var
)) /* XXX - flags */
2231 internal_warning (_("%s: removing nameref attribute"), name
);
2232 VUNSETATTR (var
, att_nameref
);
2234 if (var
&& nameref_p (var
))
2236 if (valid_nameref_value (nameref_cell (var
), 1) == 0)
2238 sh_invalidid (nameref_cell (var
) ? nameref_cell (var
) : "");
2239 return ((SHELL_VAR
*)INVALID_NAMEREF_VALUE
);
2245 /* If find_variable (name) returns NULL, check that it's not a nameref
2246 referencing a variable that doesn't exist. If it is, return the new
2247 name. If not, return the original name. Kind of like the previous
2248 function, but dealing strictly with names. This takes assignment flags
2249 so it can deal with the various assignment modes used by `declare'. */
2251 nameref_transform_name (const char *name
, int flags
)
2256 if (flags
& ASS_MKLOCAL
)
2258 v
= find_variable_last_nameref (name
, 1);
2259 /* If we're making local variables, only follow namerefs that point to
2260 non-existent variables at the same variable context. */
2261 if (v
&& v
->context
!= variable_context
)
2264 else if (flags
& ASS_MKGLOBAL
)
2265 v
= (flags
& ASS_CHKLOCAL
) ? find_variable_last_nameref (name
, 1)
2266 : find_global_variable_last_nameref (name
, 1);
2267 if (v
&& nameref_p (v
) && valid_nameref_value (nameref_cell (v
), 1))
2268 return nameref_cell (v
);
2269 return (char *)name
;
2272 /* Find a variable, forcing a search of the temporary environment first */
2274 find_variable_tempenv (const char *name
)
2278 var
= find_variable_internal (name
, FV_FORCETEMPENV
);
2279 if (var
&& nameref_p (var
))
2281 var
= find_variable_nameref (var
);
2282 if (var
== &nameref_maxloop_value
)
2284 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
2291 /* Find a variable, not forcing a search of the temporary environment first */
2293 find_variable_notempenv (const char *name
)
2297 var
= find_variable_internal (name
, 0);
2298 if (var
&& nameref_p (var
))
2300 var
= find_variable_nameref (var
);
2301 if (var
== &nameref_maxloop_value
)
2303 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
2311 find_global_variable (const char *name
)
2315 var
= var_lookup (name
, global_variables
);
2316 if (var
&& nameref_p (var
))
2318 var
= find_variable_nameref (var
); /* XXX - find_global_variable_noref? */
2319 if (var
== &nameref_maxloop_value
)
2321 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
2322 return ((SHELL_VAR
*)NULL
);
2327 return ((SHELL_VAR
*)NULL
);
2329 return (var
->dynamic_value
? (*(var
->dynamic_value
)) (var
) : var
);
2333 find_global_variable_noref (const char *name
)
2337 var
= var_lookup (name
, global_variables
);
2340 return ((SHELL_VAR
*)NULL
);
2342 return (var
->dynamic_value
? (*(var
->dynamic_value
)) (var
) : var
);
2346 find_shell_variable (const char *name
)
2350 var
= var_lookup (name
, shell_variables
);
2351 if (var
&& nameref_p (var
))
2353 var
= find_variable_nameref (var
);
2354 if (var
== &nameref_maxloop_value
)
2356 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
2357 return ((SHELL_VAR
*)NULL
);
2362 return ((SHELL_VAR
*)NULL
);
2364 return (var
->dynamic_value
? (*(var
->dynamic_value
)) (var
) : var
);
2367 /* Look up the variable entry named NAME. Returns the entry or NULL. */
2369 find_variable (const char *name
)
2374 last_table_searched
= 0;
2376 if (expanding_redir
== 0 && (assigning_in_environment
|| executing_builtin
))
2377 flags
|= FV_FORCETEMPENV
;
2378 v
= find_variable_internal (name
, flags
);
2379 if (v
&& nameref_p (v
))
2381 v
= find_variable_nameref (v
);
2382 if (v
== &nameref_maxloop_value
)
2384 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
2392 /* Find the first instance of NAME in the variable context chain; return first
2393 one found without att_invisible set; return 0 if no non-invisible instances
2396 find_variable_no_invisible (const char *name
)
2401 last_table_searched
= 0;
2402 flags
= FV_SKIPINVISIBLE
;
2403 if (expanding_redir
== 0 && (assigning_in_environment
|| executing_builtin
))
2404 flags
|= FV_FORCETEMPENV
;
2405 v
= find_variable_internal (name
, flags
);
2406 if (v
&& nameref_p (v
))
2408 v
= find_variable_nameref (v
);
2409 if (v
== &nameref_maxloop_value
)
2411 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
2419 /* Find the first instance of NAME in the variable context chain; return first
2420 one found even if att_invisible set. */
2422 find_variable_for_assignment (const char *name
)
2427 last_table_searched
= 0;
2429 if (expanding_redir
== 0 && (assigning_in_environment
|| executing_builtin
))
2430 flags
|= FV_FORCETEMPENV
;
2431 v
= find_variable_internal (name
, flags
);
2432 if (v
&& nameref_p (v
))
2434 v
= find_variable_nameref (v
);
2435 if (v
== &nameref_maxloop_value
)
2437 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
2446 find_variable_noref (const char *name
)
2452 if (expanding_redir
== 0 && (assigning_in_environment
|| executing_builtin
))
2453 flags
|= FV_FORCETEMPENV
;
2454 v
= find_variable_internal (name
, flags
);
2458 /* Look up the function entry whose name matches STRING.
2459 Returns the entry or NULL. */
2461 find_function (const char *name
)
2463 return (hash_lookup (name
, shell_functions
));
2466 /* Find the function definition for the shell function named NAME. Returns
2467 the entry or NULL. */
2469 find_function_def (const char *name
)
2471 #if defined (DEBUGGER)
2472 return ((FUNCTION_DEF
*)hash_lookup (name
, shell_function_defs
));
2474 return ((FUNCTION_DEF
*)0);
2478 /* Return the value of VAR. VAR is assumed to have been the result of a
2479 lookup without any subscript, if arrays are compiled into the shell. */
2481 get_variable_value (SHELL_VAR
*var
)
2484 return ((char *)NULL
);
2485 #if defined (ARRAY_VARS)
2486 else if (array_p (var
))
2487 return (array_reference (array_cell (var
), 0));
2488 else if (assoc_p (var
))
2489 return (assoc_reference (assoc_cell (var
), "0"));
2492 return (value_cell (var
));
2495 /* Return the string value of a variable. Return NULL if the variable
2496 doesn't exist. Don't cons a new string. This is a potential memory
2497 leak if the variable is found in the temporary environment, but doesn't
2498 leak in practice. Since functions and variables have separate name
2499 spaces, returns NULL if var_name is a shell function only. */
2501 get_string_value (const char *name
)
2505 var
= find_variable (name
);
2506 return ((var
) ? get_variable_value (var
) : (char *)NULL
);
2509 /* This is present for use by the tilde and readline libraries. */
2511 sh_get_env_value (const char *v
)
2513 return get_string_value (v
);
2516 /* **************************************************************** */
2518 /* Creating and setting variables */
2520 /* **************************************************************** */
2523 var_sametype (SHELL_VAR
*v1
, SHELL_VAR
*v2
)
2525 if (v1
== 0 || v2
== 0)
2527 #if defined (ARRAY_VARS)
2528 else if (assoc_p (v1
) && assoc_p (v2
))
2530 else if (array_p (v1
) && array_p (v2
))
2532 else if (array_p (v1
) || array_p (v2
))
2534 else if (assoc_p (v1
) || assoc_p (v2
))
2542 validate_inherited_value (SHELL_VAR
*var
, int type
)
2544 #if defined (ARRAY_VARS)
2545 if (type
== att_array
&& assoc_p (var
))
2547 else if (type
== att_assoc
&& array_p (var
))
2551 return 1; /* should we run convert_var_to_array here or let the caller? */
2554 /* Set NAME to VALUE if NAME has no value. */
2556 set_if_not (const char *name
, const char *value
)
2560 if (shell_variables
== 0)
2561 create_variable_tables ();
2563 v
= find_variable (name
);
2565 v
= bind_variable_internal (name
, value
, global_variables
->table
, HASH_NOSRCH
, 0);
2569 /* Create a local variable referenced by NAME. */
2571 make_local_variable (const char *name
, int flags
)
2573 SHELL_VAR
*new_var
, *old_var
, *old_ref
;
2578 /* We don't want to follow the nameref chain when making local variables; we
2579 just want to create them. */
2580 old_ref
= find_variable_noref (name
);
2581 if (old_ref
&& nameref_p (old_ref
) == 0)
2583 /* local foo; local foo; is a no-op. */
2584 old_var
= find_variable (name
);
2585 if (old_ref
== 0 && old_var
&& local_p (old_var
) && old_var
->context
== variable_context
)
2588 /* local -n foo; local -n foo; is a no-op. */
2589 if (old_ref
&& local_p (old_ref
) && old_ref
->context
== variable_context
)
2592 /* From here on, we want to use the refvar, not the variable it references */
2596 was_tmpvar
= old_var
&& tempvar_p (old_var
);
2597 /* If we're making a local variable in a shell function, the temporary env
2598 has already been merged into the function's variable context stack. We
2599 can assume that a temporary var in the same context appears in the same
2600 VAR_CONTEXT and can safely be returned without creating a new variable
2601 (which results in duplicate names in the same VAR_CONTEXT->table */
2602 /* We can't just test tmpvar_p because variables in the temporary env given
2603 to a shell function appear in the function's local variable VAR_CONTEXT
2604 but retain their tempvar attribute. We want temporary variables that are
2605 found in temporary_env, hence the test for last_table_searched, which is
2606 set in hash_lookup and only (so far) checked here. */
2607 if (was_tmpvar
&& old_var
->context
== variable_context
&& last_table_searched
!= temporary_env
)
2609 VUNSETATTR (old_var
, att_invisible
); /* XXX */
2610 /* We still want to flag this variable as local, though, and set things
2611 up so that it gets treated as a local variable. */
2613 /* Since we found the variable in a temporary environment, this will
2615 for (vc
= shell_variables
; vc
; vc
= vc
->down
)
2616 if (vc_isfuncenv (vc
) && vc
->scope
== variable_context
)
2618 goto set_local_var_flags
;
2623 /* If we want to change to "inherit the old variable's value" semantics,
2624 here is where to save the old value. */
2625 old_value
= was_tmpvar
? value_cell (old_var
) : (char *)NULL
;
2627 for (vc
= shell_variables
; vc
; vc
= vc
->down
)
2628 if (vc_isfuncenv (vc
) && vc
->scope
== variable_context
)
2633 internal_error (_("make_local_variable: no function context at current scope"));
2634 return ((SHELL_VAR
*)NULL
);
2636 else if (vc
->table
== 0)
2637 vc
->table
= hash_create (TEMPENV_HASH_BUCKETS
);
2639 /* Since this is called only from the local/declare/typeset code, we can
2640 call builtin_error here without worry (of course, it will also work
2641 for anything that sets this_command_name). Variables with the `noassign'
2642 attribute may not be made local. The test against old_var's context
2643 level is to disallow local copies of readonly global variables (since I
2644 believe that this could be a security hole). Readonly copies of calling
2645 function local variables are OK. */
2646 if (old_var
&& (noassign_p (old_var
) ||
2647 (readonly_p (old_var
) && old_var
->context
== 0)))
2649 if (readonly_p (old_var
))
2651 else if (noassign_p (old_var
))
2652 builtin_error (_("%s: variable may not be assigned value"), name
);
2654 /* Let noassign variables through with a warning */
2655 if (readonly_p (old_var
))
2657 return ((SHELL_VAR
*)NULL
);
2661 new_var
= make_new_variable (name
, vc
->table
);
2665 /* This handles the case where a variable is found in both the temporary
2666 environment *and* declared as a local variable. If we want to avoid
2667 multiple entries with the same name in VC->table (that might mess up
2668 unset), we need to use the existing variable entry and destroy the
2669 current value. Currently disabled because it doesn't matter -- the
2670 right things happen. */
2672 if (was_tmpvar
&& (new_var
= hash_lookup (name
, vc
->table
)))
2674 dispose_variable_value (new_var
);
2675 init_variable (new_var
);
2679 new_var
= make_new_variable (name
, vc
->table
);
2681 /* If we found this variable in one of the temporary environments,
2682 inherit its value. Watch to see if this causes problems with
2683 things like `x=4 local x'. XXX - see above for temporary env
2684 variables with the same context level as variable_context */
2685 /* XXX - we should only do this if the variable is not an array. */
2686 /* If we want to change the local variable semantics to "inherit
2687 the old variable's value" here is where to set it. And we would
2688 need to use copy_variable (currently unused) to do it for all
2689 possible variable values. */
2691 var_setvalue (new_var
, savestring (old_value
));
2692 else if (localvar_inherit
|| (flags
& MKLOC_INHERIT
))
2694 /* This may not make sense for nameref variables that are shadowing
2695 variables with the same name, but we don't know that yet. */
2696 #if defined (ARRAY_VARS)
2697 if (assoc_p (old_var
))
2698 var_setassoc (new_var
, assoc_copy (assoc_cell (old_var
)));
2699 else if (array_p (old_var
))
2700 var_setarray (new_var
, array_copy (array_cell (old_var
)));
2701 else if (value_cell (old_var
))
2703 if (value_cell (old_var
))
2705 var_setvalue (new_var
, savestring (value_cell (old_var
)));
2707 var_setvalue (new_var
, (char *)NULL
);
2710 if (localvar_inherit
|| (flags
& MKLOC_INHERIT
))
2712 /* It doesn't make sense to inherit the nameref attribute */
2713 new_var
->attributes
= old_var
->attributes
& ~att_nameref
;
2714 new_var
->dynamic_value
= old_var
->dynamic_value
;
2715 new_var
->assign_func
= old_var
->assign_func
;
2718 /* We inherit the export attribute, but no others. */
2719 new_var
->attributes
= exported_p (old_var
) ? att_exported
: 0;
2722 set_local_var_flags
:
2723 vc
->flags
|= VC_HASLOCAL
;
2725 new_var
->context
= variable_context
;
2726 VSETATTR (new_var
, att_local
);
2731 /* value_cell will be 0 if localvar_inherit == 0 or there was no old variable
2732 with the same name or the old variable was invisible */
2733 if (was_tmpvar
== 0 && value_cell (new_var
) == 0)
2734 VSETATTR (new_var
, att_invisible
); /* XXX */
2739 init_variable (SHELL_VAR
*entry
)
2741 var_setvalue (entry
, (char *)NULL
);
2742 CLEAR_EXPORTSTR (entry
);
2744 entry
->dynamic_value
= (sh_var_value_func_t
*)NULL
;
2745 entry
->assign_func
= (sh_var_assign_func_t
*)NULL
;
2747 entry
->attributes
= 0;
2749 /* Always assume variables are to be made at toplevel!
2750 make_local_variable has the responsibility of changing the
2751 variable context. */
2755 /* Create a new shell variable with name NAME. */
2757 new_shell_variable (const char *name
)
2761 entry
= (SHELL_VAR
*)xmalloc (sizeof (SHELL_VAR
));
2763 entry
->name
= savestring (name
);
2764 init_variable (entry
);
2768 /* Create a new shell variable with name NAME and add it to the hash table
2771 make_new_variable (const char *name
, HASH_TABLE
*table
)
2774 BUCKET_CONTENTS
*elt
;
2776 entry
= new_shell_variable (name
);
2778 /* Make sure we have a shell_variables hash table to add to. */
2779 if (shell_variables
== 0)
2780 create_variable_tables ();
2782 elt
= hash_insert (savestring (name
), table
, HASH_NOSRCH
);
2783 elt
->data
= (PTR_T
)entry
;
2788 #if defined (ARRAY_VARS)
2790 make_new_array_variable (const char *name
)
2795 entry
= make_new_variable (name
, global_variables
->table
);
2796 array
= array_create ();
2798 var_setarray (entry
, array
);
2799 VSETATTR (entry
, att_array
);
2804 make_local_array_variable (const char *name
, int flags
)
2810 assoc_ok
= flags
& MKLOC_ASSOCOK
;
2812 var
= make_local_variable (name
, flags
& MKLOC_INHERIT
); /* XXX for now */
2813 /* If ASSOC_OK is non-zero, assume that we are ok with letting an assoc
2814 variable return to the caller without converting it. The caller will
2815 either flag an error or do the conversion itself. */
2816 if (var
== 0 || array_p (var
) || (assoc_ok
&& assoc_p (var
)))
2819 /* Validate any value we inherited from a variable instance at a previous
2820 scope and discard anything that's invalid. */
2821 if (localvar_inherit
&& assoc_p (var
))
2823 internal_warning (_("%s: cannot inherit value from incompatible type"), name
);
2824 VUNSETATTR (var
, att_assoc
);
2825 dispose_variable_value (var
);
2826 array
= array_create ();
2827 var_setarray (var
, array
);
2829 else if (localvar_inherit
)
2830 var
= convert_var_to_array (var
); /* XXX */
2833 dispose_variable_value (var
);
2834 array
= array_create ();
2835 var_setarray (var
, array
);
2838 VSETATTR (var
, att_array
);
2843 make_new_assoc_variable (const char *name
)
2848 entry
= make_new_variable (name
, global_variables
->table
);
2849 hash
= assoc_create (ASSOC_HASH_BUCKETS
);
2851 var_setassoc (entry
, hash
);
2852 VSETATTR (entry
, att_assoc
);
2857 make_local_assoc_variable (const char *name
, int flags
)
2863 array_ok
= flags
& MKLOC_ARRAYOK
;
2865 var
= make_local_variable (name
, flags
& MKLOC_INHERIT
); /* XXX for now */
2866 /* If ARRAY_OK is non-zero, assume that we are ok with letting an array
2867 variable return to the caller without converting it. The caller will
2868 either flag an error or do the conversion itself. */
2869 if (var
== 0 || assoc_p (var
) || (array_ok
&& array_p (var
)))
2872 /* Validate any value we inherited from a variable instance at a previous
2873 scope and discard anything that's invalid. */
2874 if (localvar_inherit
&& array_p (var
))
2876 internal_warning (_("%s: cannot inherit value from incompatible type"), name
);
2877 VUNSETATTR (var
, att_array
);
2878 dispose_variable_value (var
);
2879 hash
= assoc_create (ASSOC_HASH_BUCKETS
);
2880 var_setassoc (var
, hash
);
2882 else if (localvar_inherit
)
2883 var
= convert_var_to_assoc (var
); /* XXX */
2886 dispose_variable_value (var
);
2887 hash
= assoc_create (ASSOC_HASH_BUCKETS
);
2888 var_setassoc (var
, hash
);
2891 VSETATTR (var
, att_assoc
);
2897 make_variable_value (SHELL_VAR
*var
, const char *value
, int flags
)
2899 char *retval
, *oval
;
2900 intmax_t lval
, rval
;
2904 /* If this variable has had its type set to integer (via `declare -i'),
2905 then do expression evaluation on it and store the result. The
2906 functions in expr.c (evalexp()) and bind_int_variable() are responsible
2907 for turning off the integer flag if they don't want further
2908 evaluation done. Callers that find it inconvenient to do this can set
2909 the ASS_NOEVAL flag. For the special case of arithmetic expression
2910 evaluation, the caller can set ASS_NOLONGJMP to avoid jumping out to
2912 if ((flags
& ASS_NOEVAL
) == 0 && integer_p (var
))
2914 if (flags
& ASS_APPEND
)
2916 oval
= value_cell (var
);
2917 lval
= evalexp (oval
, 0, &expok
); /* ksh93 seems to do this */
2920 if (flags
& ASS_NOLONGJMP
)
2924 top_level_cleanup ();
2925 jump_to_top_level (DISCARD
);
2929 rval
= evalexp (value
, 0, &expok
);
2932 if (flags
& ASS_NOLONGJMP
)
2936 top_level_cleanup ();
2937 jump_to_top_level (DISCARD
);
2940 /* This can be fooled if the variable's value changes while evaluating
2941 `rval'. We can change it if we move the evaluation of lval to here. */
2942 if (flags
& ASS_APPEND
)
2944 retval
= itos (rval
);
2946 #if defined (CASEMOD_ATTRS)
2947 else if ((flags
& ASS_NOEVAL
) == 0 && (capcase_p (var
) || uppercase_p (var
) || lowercase_p (var
)))
2949 if (flags
& ASS_APPEND
)
2951 oval
= get_variable_value (var
);
2952 if (oval
== 0) /* paranoia */
2954 olen
= STRLEN (oval
);
2955 retval
= (char *)xmalloc (olen
+ (value
? STRLEN (value
) : 0) + 1);
2956 strcpy (retval
, oval
);
2958 strcpy (retval
+olen
, value
);
2961 retval
= savestring (value
);
2964 retval
= (char *)xmalloc (1);
2967 op
= capcase_p (var
) ? CASE_CAPITALIZE
2968 : (uppercase_p (var
) ? CASE_UPPER
: CASE_LOWER
);
2969 oval
= sh_modcase (retval
, (char *)0, op
);
2973 #endif /* CASEMOD_ATTRS */
2977 if (flags
& ASS_APPEND
)
2979 oval
= get_variable_value (var
);
2980 if (oval
== 0) /* paranoia */
2982 olen
= STRLEN (oval
);
2983 retval
= (char *)xmalloc (olen
+ (value
? STRLEN (value
) : 0) + 1);
2984 strcpy (retval
, oval
);
2986 strcpy (retval
+olen
, value
);
2989 retval
= savestring (value
);
2992 retval
= (char *)xmalloc (1);
2997 retval
= (char *)NULL
;
3002 /* If we can optimize appending to string variables, say so */
3004 can_optimize_assignment (SHELL_VAR
*entry
, const char *value
, int aflags
)
3006 if ((aflags
& ASS_APPEND
) == 0)
3008 #if defined (ARRAY_VARS)
3009 if (array_p (entry
) || assoc_p (entry
))
3012 if (integer_p (entry
) || uppercase_p (entry
) || lowercase_p (entry
) || capcase_p (entry
))
3014 if (readonly_p (entry
) || noassign_p (entry
))
3019 /* right now we optimize appends to string variables */
3021 optimized_assignment (SHELL_VAR
*entry
, const char *value
, int aflags
)
3026 v
= value_cell (entry
);
3028 vlen
= STRLEN (value
);
3030 new = (char *)xrealloc (v
, len
+ vlen
+ 8); /* for now */
3037 strcpy (new + len
, value
);
3038 var_setvalue (entry
, new);
3042 /* Bind a variable NAME to VALUE in the HASH_TABLE TABLE, which may be the
3043 temporary environment (but usually is not). HFLAGS controls how NAME
3044 is looked up in TABLE; AFLAGS controls how VALUE is assigned */
3046 bind_variable_internal (const char *name
, const char *value
, HASH_TABLE
*table
, int hflags
, int aflags
)
3048 char *newval
, *tname
;
3049 SHELL_VAR
*entry
, *tentry
;
3051 entry
= (hflags
& HASH_NOSRCH
) ? (SHELL_VAR
*)NULL
: hash_lookup (name
, table
);
3052 /* Follow the nameref chain here if this is the global variables table */
3053 if (entry
&& nameref_p (entry
) && (invisible_p (entry
) == 0) && table
== global_variables
->table
)
3055 entry
= find_global_variable (entry
->name
);
3056 /* Let's see if we have a nameref referencing a variable that hasn't yet
3059 entry
= find_variable_last_nameref (name
, 0); /* XXX */
3060 if (entry
== 0) /* just in case */
3064 /* The first clause handles `declare -n ref; ref=x;' or `declare -n ref;
3066 if (entry
&& invisible_p (entry
) && nameref_p (entry
))
3068 if ((aflags
& ASS_FORCE
) == 0 && value
&& valid_nameref_value (value
, 0) == 0)
3070 sh_invalidid (value
);
3071 return ((SHELL_VAR
*)NULL
);
3075 else if (entry
&& nameref_p (entry
))
3077 newval
= nameref_cell (entry
); /* XXX - newval can't be NULL here */
3078 if (valid_nameref_value (newval
, 0) == 0)
3080 sh_invalidid (newval
);
3081 return ((SHELL_VAR
*)NULL
);
3083 #if defined (ARRAY_VARS)
3084 /* declare -n foo=x[2] ; foo=bar */
3085 if (valid_array_reference (newval
, 0))
3087 tname
= array_variable_name (newval
, 0, (char **)0, (int *)0);
3088 if (tname
&& (tentry
= find_variable_noref (tname
)) && nameref_p (tentry
))
3090 /* nameref variables can't be arrays */
3091 internal_warning (_("%s: removing nameref attribute"), name_cell (tentry
));
3092 FREE (value_cell (tentry
)); /* XXX - bash-4.3 compat */
3093 var_setvalue (tentry
, (char *)NULL
);
3094 VUNSETATTR (tentry
, att_nameref
);
3098 /* entry == nameref variable; tentry == array variable;
3099 newval == x[2]; value = bar
3100 We don't need to call make_variable_value here, since
3101 assign_array_element will eventually do it itself based on
3102 newval and aflags. */
3104 entry
= assign_array_element (newval
, value
, aflags
|ASS_NAMEREF
, (array_eltstate_t
*)0);
3111 entry
= make_new_variable (newval
, table
);
3112 var_setvalue (entry
, make_variable_value (entry
, value
, aflags
));
3115 else if (entry
== 0)
3117 entry
= make_new_variable (name
, table
);
3118 var_setvalue (entry
, make_variable_value (entry
, value
, aflags
)); /* XXX */
3120 else if (entry
->assign_func
) /* array vars have assign functions now */
3122 if ((readonly_p (entry
) && (aflags
& ASS_FORCE
) == 0) || noassign_p (entry
))
3124 if (readonly_p (entry
))
3125 err_readonly (name_cell (entry
));
3129 INVALIDATE_EXPORTSTR (entry
);
3130 newval
= (aflags
& ASS_APPEND
) ? make_variable_value (entry
, value
, aflags
) : (char *)value
;
3131 if (assoc_p (entry
))
3132 entry
= (*(entry
->assign_func
)) (entry
, newval
, -1, savestring ("0"));
3133 else if (array_p (entry
))
3134 entry
= (*(entry
->assign_func
)) (entry
, newval
, 0, 0);
3136 entry
= (*(entry
->assign_func
)) (entry
, newval
, -1, 0);
3137 if (newval
!= value
)
3144 if ((readonly_p (entry
) && (aflags
& ASS_FORCE
) == 0) || noassign_p (entry
))
3146 if (readonly_p (entry
))
3147 err_readonly (name_cell (entry
));
3151 /* Variables which are bound are visible. */
3152 VUNSETATTR (entry
, att_invisible
);
3154 /* If we can optimize the assignment, do so and return. Right now, we
3155 optimize appends to string variables. */
3156 if (can_optimize_assignment (entry
, value
, aflags
))
3158 INVALIDATE_EXPORTSTR (entry
);
3159 optimized_assignment (entry
, value
, aflags
);
3161 if (mark_modified_vars
)
3162 VSETATTR (entry
, att_exported
);
3164 if (exported_p (entry
))
3165 array_needs_making
= 1;
3170 #if defined (ARRAY_VARS)
3171 if (assoc_p (entry
) || array_p (entry
))
3172 newval
= make_array_variable_value (entry
, 0, "0", value
, aflags
);
3175 newval
= make_variable_value (entry
, value
, aflags
); /* XXX */
3177 /* Invalidate any cached export string */
3178 INVALIDATE_EXPORTSTR (entry
);
3180 #if defined (ARRAY_VARS)
3181 /* XXX -- this bears looking at again -- XXX */
3182 /* If an existing array variable x is being assigned to with x=b or
3183 `read x' or something of that nature, silently convert it to
3184 x[0]=b or `read x[0]'. */
3185 if (assoc_p (entry
))
3187 assoc_insert (assoc_cell (entry
), savestring ("0"), newval
);
3190 else if (array_p (entry
))
3192 array_insert (array_cell (entry
), 0, newval
);
3198 FREE (value_cell (entry
));
3199 var_setvalue (entry
, newval
);
3203 if (mark_modified_vars
)
3204 VSETATTR (entry
, att_exported
);
3206 if (exported_p (entry
))
3207 array_needs_making
= 1;
3212 /* Bind a variable NAME to VALUE. This conses up the name
3213 and value strings. If we have a temporary environment, we bind there
3214 first, then we bind into shell_variables. */
3217 bind_variable (const char *name
, const char *value
, int flags
)
3220 VAR_CONTEXT
*vc
, *nvc
;
3222 if (shell_variables
== 0)
3223 create_variable_tables ();
3225 /* If we have a temporary environment, look there first for the variable,
3226 and, if found, modify the value there before modifying it in the
3227 shell_variables table. This allows sourced scripts to modify values
3228 given to them in a temporary environment while modifying the variable
3229 value that the caller sees. The caller can inhibit this by setting
3230 ASS_NOTEMPENV in FLAGS. */
3231 if (temporary_env
&& value
&& (flags
& ASS_NOTEMPENV
) == 0) /* XXX - can value be null here? */
3232 bind_tempenv_variable (name
, value
);
3234 /* XXX -- handle local variables here. */
3235 for (vc
= shell_variables
; vc
; vc
= vc
->down
)
3237 if (vc_isfuncenv (vc
) || vc_isbltnenv (vc
))
3239 v
= hash_lookup (name
, vc
->table
);
3241 if (v
&& nameref_p (v
))
3243 /* This starts at the context where we found the nameref. If we
3244 want to start the name resolution over again at the original
3245 context, this is where we need to change it */
3246 nv
= find_variable_nameref_context (v
, vc
, &nvc
);
3249 nv
= find_variable_last_nameref_context (v
, vc
, &nvc
);
3250 if (nv
&& nv
!= &nameref_maxloop_value
&& nameref_p (nv
))
3252 /* If this nameref variable doesn't have a value yet,
3253 set the value. Otherwise, assign using the value as
3255 if (nameref_cell (nv
) == 0)
3256 return (bind_variable_internal (nv
->name
, value
, nvc
->table
, 0, flags
));
3257 #if defined (ARRAY_VARS)
3258 else if (valid_array_reference (nameref_cell (nv
), 0))
3259 return (assign_array_element (nameref_cell (nv
), value
, flags
, (array_eltstate_t
*)0));
3262 return (bind_variable_internal (nameref_cell (nv
), value
, nvc
->table
, 0, flags
));
3264 else if (nv
== &nameref_maxloop_value
)
3266 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), v
->name
, NAMEREF_MAX
);
3267 return (bind_global_variable (v
->name
, value
, flags
));
3272 else if (nv
== &nameref_maxloop_value
)
3274 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), v
->name
, NAMEREF_MAX
);
3275 return (bind_global_variable (v
->name
, value
, flags
));
3281 return (bind_variable_internal (v
->name
, value
, nvc
->table
, 0, flags
));
3284 /* bind_variable_internal will handle nameref resolution in this case */
3285 return (bind_variable_internal (name
, value
, global_variables
->table
, 0, flags
));
3289 bind_global_variable (const char *name
, const char *value
, int flags
)
3291 if (shell_variables
== 0)
3292 create_variable_tables ();
3294 /* bind_variable_internal will handle nameref resolution in this case */
3295 return (bind_variable_internal (name
, value
, global_variables
->table
, 0, flags
));
3299 bind_invalid_envvar (const char *name
, const char *value
, int aflags
)
3301 if (invalid_env
== 0)
3302 invalid_env
= hash_create (64); /* XXX */
3303 return (bind_variable_internal (name
, value
, invalid_env
, HASH_NOSRCH
, aflags
));
3306 /* Make VAR, a simple shell variable, have value VALUE. Once assigned a
3307 value, variables are no longer invisible. This is a duplicate of part
3308 of the internals of bind_variable. If the variable is exported, or
3309 all modified variables should be exported, mark the variable for export
3310 and note that the export environment needs to be recreated. */
3312 bind_variable_value (SHELL_VAR
*var
, char *value
, int aflags
)
3317 invis
= invisible_p (var
);
3318 VUNSETATTR (var
, att_invisible
);
3320 if (var
->assign_func
)
3322 /* If we're appending, we need the old value, so use
3323 make_variable_value */
3324 t
= (aflags
& ASS_APPEND
) ? make_variable_value (var
, value
, aflags
) : value
;
3325 (*(var
->assign_func
)) (var
, t
, -1, 0);
3326 if (t
!= value
&& t
)
3331 t
= make_variable_value (var
, value
, aflags
);
3332 if ((aflags
& (ASS_NAMEREF
|ASS_FORCE
)) == ASS_NAMEREF
&& check_selfref (name_cell (var
), t
, 0))
3334 if (variable_context
)
3335 internal_warning (_("%s: circular name reference"), name_cell (var
));
3338 internal_error (_("%s: nameref variable self references not allowed"), name_cell (var
));
3341 VSETATTR (var
, att_invisible
); /* XXX */
3342 return ((SHELL_VAR
*)NULL
);
3345 if ((aflags
& ASS_NAMEREF
) && (valid_nameref_value (t
, 0) == 0))
3349 VSETATTR (var
, att_invisible
); /* XXX */
3350 return ((SHELL_VAR
*)NULL
);
3352 FREE (value_cell (var
));
3353 var_setvalue (var
, t
);
3356 INVALIDATE_EXPORTSTR (var
);
3358 if (mark_modified_vars
)
3359 VSETATTR (var
, att_exported
);
3361 if (exported_p (var
))
3362 array_needs_making
= 1;
3367 /* Bind/create a shell variable with the name LHS to the RHS.
3368 This creates or modifies a variable such that it is an integer.
3370 This used to be in expr.c, but it is here so that all of the
3371 variable binding stuff is localized. Since we don't want any
3372 recursive evaluation from bind_variable() (possible without this code,
3373 since bind_variable() calls the evaluator for variables with the integer
3374 attribute set), we temporarily turn off the integer attribute for each
3375 variable we set here, then turn it back on after binding as necessary. */
3378 bind_int_variable (const char *lhs
, const char *rhs
, int flags
)
3380 register SHELL_VAR
*v
;
3381 int isint
, isarr
, implicitarray
, vflags
, avflags
;
3383 isint
= isarr
= implicitarray
= 0;
3384 #if defined (ARRAY_VARS)
3385 /* Don't rely on VA_NOEXPAND being 1, set it explicitly */
3386 vflags
= convert_assign_flags_to_validarray_flags (flags
);
3387 if (valid_array_reference (lhs
, vflags
))
3390 avflags
= convert_assign_flags_to_arrayval_flags (flags
);
3391 v
= array_variable_part (lhs
, avflags
, (char **)0, (int *)0);
3393 else if (legal_identifier (lhs
) == 0)
3396 return ((SHELL_VAR
*)NULL
);
3400 v
= find_variable (lhs
);
3404 isint
= integer_p (v
);
3405 VUNSETATTR (v
, att_integer
);
3406 #if defined (ARRAY_VARS)
3407 if (array_p (v
) && isarr
== 0)
3412 #if defined (ARRAY_VARS)
3414 v
= assign_array_element (lhs
, rhs
, flags
, (array_eltstate_t
*)0);
3415 else if (implicitarray
)
3416 v
= bind_array_variable (lhs
, 0, rhs
, 0); /* XXX - check on flags */
3419 v
= bind_variable (lhs
, rhs
, 0); /* why not use bind_variable_value? */
3424 VSETATTR (v
, att_integer
);
3425 VUNSETATTR (v
, att_invisible
);
3428 if (v
&& nameref_p (v
))
3429 internal_warning (_("%s: assigning integer to name reference"), lhs
);
3435 bind_var_to_int (const char *var
, intmax_t val
, int flags
)
3437 char ibuf
[INT_STRLEN_BOUND (intmax_t) + 1], *p
;
3439 p
= fmtulong (val
, 10, ibuf
, sizeof (ibuf
), 0);
3440 return (bind_int_variable (var
, p
, flags
));
3443 /* Do a function binding to a variable. You pass the name and
3444 the command to bind to. This conses the name and command. */
3446 bind_function (const char *name
, COMMAND
*value
)
3450 entry
= find_function (name
);
3453 BUCKET_CONTENTS
*elt
;
3455 elt
= hash_insert (savestring (name
), shell_functions
, HASH_NOSRCH
);
3456 entry
= new_shell_variable (name
);
3457 elt
->data
= (PTR_T
)entry
;
3460 INVALIDATE_EXPORTSTR (entry
);
3462 if (var_isset (entry
))
3463 dispose_command (function_cell (entry
));
3466 var_setfunc (entry
, copy_command (value
));
3468 var_setfunc (entry
, 0);
3470 VSETATTR (entry
, att_function
);
3472 if (mark_modified_vars
)
3473 VSETATTR (entry
, att_exported
);
3475 VUNSETATTR (entry
, att_invisible
); /* Just to be sure */
3477 if (exported_p (entry
))
3478 array_needs_making
= 1;
3480 #if defined (PROGRAMMABLE_COMPLETION)
3481 set_itemlist_dirty (&it_functions
);
3487 #if defined (DEBUGGER)
3488 /* Bind a function definition, which includes source file and line number
3489 information in addition to the command, into the FUNCTION_DEF hash table.
3490 If (FLAGS & 1), overwrite any existing definition. If FLAGS == 0, leave
3491 any existing definition alone. */
3493 bind_function_def (const char *name
, FUNCTION_DEF
*value
, int flags
)
3495 FUNCTION_DEF
*entry
;
3496 BUCKET_CONTENTS
*elt
;
3499 entry
= find_function_def (name
);
3500 if (entry
&& (flags
& 1))
3502 dispose_function_def_contents (entry
);
3503 entry
= copy_function_def_contents (value
, entry
);
3509 cmd
= value
->command
;
3511 entry
= copy_function_def (value
);
3512 value
->command
= cmd
;
3514 elt
= hash_insert (savestring (name
), shell_function_defs
, HASH_NOSRCH
);
3515 elt
->data
= (PTR_T
*)entry
;
3518 #endif /* DEBUGGER */
3520 /* Add STRING, which is of the form foo=bar, to the temporary environment
3521 HASH_TABLE (temporary_env). The functions in execute_cmd.c are
3522 responsible for moving the main temporary env to one of the other
3523 temporary environments. The expansion code in subst.c calls this. */
3525 assign_in_env (const WORD_DESC
*word
, int flags
)
3528 char *name
, *temp
, *value
, *newname
;
3532 string
= word
->word
;
3535 offset
= assignment (string
, 0);
3536 newname
= name
= savestring (string
);
3537 value
= (char *)NULL
;
3539 if (name
[offset
] == '=')
3543 /* don't ignore the `+' when assigning temporary environment */
3544 if (name
[offset
- 1] == '+')
3546 name
[offset
- 1] = '\0';
3547 aflags
|= ASS_APPEND
;
3550 if (legal_identifier (name
) == 0)
3552 sh_invalidid (name
);
3557 var
= find_variable (name
);
3560 var
= find_variable_last_nameref (name
, 1);
3561 /* If we're assigning a value to a nameref variable in the temp
3562 environment, and the value of the nameref is valid for assignment,
3563 but the variable does not already exist, assign to the nameref
3564 target and add the target to the temporary environment. This is
3566 /* We use 2 in the call to valid_nameref_value because we don't want
3567 to allow array references here at all (newname will be used to
3568 create a variable directly below) */
3569 if (var
&& nameref_p (var
) && valid_nameref_value (nameref_cell (var
), 2))
3571 newname
= nameref_cell (var
);
3572 var
= 0; /* don't use it for append */
3576 newname
= name_cell (var
); /* no-op if not nameref */
3578 if (var
&& (readonly_p (var
) || noassign_p (var
)))
3580 if (readonly_p (var
))
3581 err_readonly (name
);
3585 temp
= name
+ offset
+ 1;
3587 value
= expand_assignment_string_to_string (temp
, 0);
3589 if (var
&& (aflags
& ASS_APPEND
))
3593 value
= (char *)xmalloc (1); /* like do_assignment_internal */
3596 temp
= make_variable_value (var
, value
, aflags
);
3602 if (temporary_env
== 0)
3603 temporary_env
= hash_create (TEMPENV_HASH_BUCKETS
);
3605 var
= hash_lookup (newname
, temporary_env
);
3607 var
= make_new_variable (newname
, temporary_env
);
3609 FREE (value_cell (var
));
3613 value
= (char *)xmalloc (1); /* see above */
3617 var_setvalue (var
, value
);
3618 var
->attributes
|= (att_exported
|att_tempvar
);
3619 var
->context
= variable_context
; /* XXX */
3621 INVALIDATE_EXPORTSTR (var
);
3622 var
->exportstr
= mk_env_string (newname
, value
, 0);
3624 array_needs_making
= 1;
3628 if (STREQ (newname
, "POSIXLY_CORRECT") || STREQ (newname
, "POSIX_PEDANDTIC"))
3629 save_posix_options (); /* XXX one level of saving right now */
3630 stupidly_hack_special_variables (newname
);
3633 if (echo_command_at_execute
)
3634 /* The Korn shell prints the `+ ' in front of assignment statements,
3636 xtrace_print_assignment (name
, value
, 0, 1);
3642 /* **************************************************************** */
3644 /* Copying variables */
3646 /* **************************************************************** */
3648 #ifdef INCLUDE_UNUSED
3649 /* Copy VAR to a new data structure and return that structure. */
3651 copy_variable (SHELL_VAR
*var
)
3653 SHELL_VAR
*copy
= (SHELL_VAR
*)NULL
;
3657 copy
= (SHELL_VAR
*)xmalloc (sizeof (SHELL_VAR
));
3659 copy
->attributes
= var
->attributes
;
3660 copy
->name
= savestring (var
->name
);
3662 if (function_p (var
))
3663 var_setfunc (copy
, copy_command (function_cell (var
)));
3664 #if defined (ARRAY_VARS)
3665 else if (array_p (var
))
3666 var_setarray (copy
, array_copy (array_cell (var
)));
3667 else if (assoc_p (var
))
3668 var_setassoc (copy
, assoc_copy (assoc_cell (var
)));
3670 else if (nameref_cell (var
)) /* XXX - nameref */
3671 var_setref (copy
, savestring (nameref_cell (var
)));
3672 else if (value_cell (var
)) /* XXX - nameref */
3673 var_setvalue (copy
, savestring (value_cell (var
)));
3675 var_setvalue (copy
, (char *)NULL
);
3677 copy
->dynamic_value
= var
->dynamic_value
;
3678 copy
->assign_func
= var
->assign_func
;
3680 copy
->exportstr
= COPY_EXPORTSTR (var
);
3682 copy
->context
= var
->context
;
3688 /* **************************************************************** */
3690 /* Deleting and unsetting variables */
3692 /* **************************************************************** */
3694 /* Dispose of the information attached to VAR. */
3696 dispose_variable_value (SHELL_VAR
*var
)
3698 if (function_p (var
))
3699 dispose_command (function_cell (var
));
3700 #if defined (ARRAY_VARS)
3701 else if (array_p (var
))
3702 array_dispose (array_cell (var
));
3703 else if (assoc_p (var
))
3704 assoc_dispose (assoc_cell (var
));
3706 else if (nameref_p (var
))
3707 FREE (nameref_cell (var
));
3709 FREE (value_cell (var
));
3713 dispose_variable (SHELL_VAR
*var
)
3718 if (nofree_p (var
) == 0)
3719 dispose_variable_value (var
);
3721 FREE_EXPORTSTR (var
);
3725 if (exported_p (var
))
3726 array_needs_making
= 1;
3731 /* Unset the shell variable referenced by NAME. Unsetting a nameref variable
3732 unsets the variable it resolves to but leaves the nameref alone. */
3734 unbind_variable (const char *name
)
3739 v
= var_lookup (name
, shell_variables
);
3740 nv
= (v
&& nameref_p (v
)) ? find_variable_nameref (v
) : (SHELL_VAR
*)NULL
;
3741 if (nv
== &nameref_maxloop_value
)
3743 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
3747 r
= nv
? makunbound (nv
->name
, shell_variables
) : makunbound (name
, shell_variables
);
3751 /* Unbind NAME, where NAME is assumed to be a nameref variable */
3753 unbind_nameref (const char *name
)
3757 v
= var_lookup (name
, shell_variables
);
3758 if (v
&& nameref_p (v
))
3759 return makunbound (name
, shell_variables
);
3763 /* Unbind the first instance of NAME, whether it's a nameref or not */
3765 unbind_variable_noref (const char *name
)
3769 v
= var_lookup (name
, shell_variables
);
3771 return makunbound (name
, shell_variables
);
3776 unbind_global_variable (const char *name
)
3781 v
= var_lookup (name
, global_variables
);
3782 /* This starts at the current scope, just like find_global_variable; should we
3783 use find_global_variable_nameref here? */
3784 nv
= (v
&& nameref_p (v
)) ? find_variable_nameref (v
) : (SHELL_VAR
*)NULL
;
3785 if (nv
== &nameref_maxloop_value
)
3787 internal_warning (_("%s: maximum nameref depth (%d) exceeded"), name
, NAMEREF_MAX
);
3791 r
= nv
? makunbound (nv
->name
, shell_variables
) : makunbound (name
, global_variables
);
3796 unbind_global_variable_noref (const char *name
)
3800 v
= var_lookup (name
, global_variables
);
3802 return makunbound (name
, global_variables
);
3807 check_unbind_variable (const char *name
)
3811 v
= find_variable (name
);
3812 if (v
&& readonly_p (v
))
3814 internal_error (_("%s: cannot unset: readonly %s"), name
, "variable");
3817 else if (v
&& non_unsettable_p (v
))
3819 internal_error (_("%s: cannot unset"), name
);
3822 return (unbind_variable (name
));
3825 /* Unset the shell function named NAME. */
3827 unbind_func (const char *name
)
3829 BUCKET_CONTENTS
*elt
;
3832 elt
= hash_remove (name
, shell_functions
, 0);
3837 #if defined (PROGRAMMABLE_COMPLETION)
3838 set_itemlist_dirty (&it_functions
);
3841 func
= (SHELL_VAR
*)elt
->data
;
3844 if (exported_p (func
))
3845 array_needs_making
++;
3846 dispose_variable (func
);
3855 #if defined (DEBUGGER)
3857 unbind_function_def (const char *name
)
3859 BUCKET_CONTENTS
*elt
;
3860 FUNCTION_DEF
*funcdef
;
3862 elt
= hash_remove (name
, shell_function_defs
, 0);
3867 funcdef
= (FUNCTION_DEF
*)elt
->data
;
3869 dispose_function_def (funcdef
);
3876 #endif /* DEBUGGER */
3879 delete_var (const char *name
, VAR_CONTEXT
*vc
)
3881 BUCKET_CONTENTS
*elt
;
3885 for (elt
= (BUCKET_CONTENTS
*)NULL
, v
= vc
; v
; v
= v
->down
)
3886 if (elt
= hash_remove (name
, v
->table
, 0))
3892 old_var
= (SHELL_VAR
*)elt
->data
;
3896 dispose_variable (old_var
);
3900 /* Make the variable associated with NAME go away. HASH_LIST is the
3901 hash table from which this variable should be deleted (either
3902 shell_variables or shell_functions).
3903 Returns non-zero if the variable couldn't be found. */
3905 makunbound (const char *name
, VAR_CONTEXT
*vc
)
3907 BUCKET_CONTENTS
*elt
, *new_elt
;
3912 for (elt
= (BUCKET_CONTENTS
*)NULL
, v
= vc
; v
; v
= v
->down
)
3913 if (elt
= hash_remove (name
, v
->table
, 0))
3919 old_var
= (SHELL_VAR
*)elt
->data
;
3921 if (old_var
&& exported_p (old_var
))
3922 array_needs_making
++;
3924 /* If we're unsetting a local variable and we're still executing inside
3925 the function, just mark the variable as invisible. The function
3926 eventually called by pop_var_context() will clean it up later. This
3927 must be done so that if the variable is subsequently assigned a new
3928 value inside the function, the `local' attribute is still present.
3929 We also need to add it back into the correct hash table. */
3930 if (old_var
&& local_p (old_var
) &&
3931 (old_var
->context
== variable_context
|| (localvar_unset
&& old_var
->context
< variable_context
)))
3933 if (nofree_p (old_var
))
3934 var_setvalue (old_var
, (char *)NULL
);
3935 #if defined (ARRAY_VARS)
3936 else if (array_p (old_var
))
3937 array_dispose (array_cell (old_var
));
3938 else if (assoc_p (old_var
))
3939 assoc_dispose (assoc_cell (old_var
));
3941 else if (nameref_p (old_var
))
3942 FREE (nameref_cell (old_var
));
3944 FREE (value_cell (old_var
));
3946 /* Reset the attributes. Preserve the export attribute if the variable
3947 came from a temporary environment. Make sure it stays local, and
3948 make it invisible. */
3949 old_var
->attributes
= (exported_p (old_var
) && tempvar_p (old_var
)) ? att_exported
: 0;
3950 #else /* TAG:bash-5.3 look at this again */
3951 /* Reset the attributes, but preserve the export attribute.
3952 Make sure it stays local, and make it invisible. */
3953 old_var
->attributes
= exported_p (old_var
) ? att_exported
: 0;
3955 VSETATTR (old_var
, att_local
);
3956 VSETATTR (old_var
, att_invisible
);
3957 var_setvalue (old_var
, (char *)NULL
);
3958 INVALIDATE_EXPORTSTR (old_var
);
3960 new_elt
= hash_insert (savestring (old_var
->name
), v
->table
, 0);
3961 new_elt
->data
= (PTR_T
)old_var
;
3962 stupidly_hack_special_variables (old_var
->name
);
3969 /* Have to save a copy of name here, because it might refer to
3970 old_var->name. If so, stupidly_hack_special_variables will
3971 reference freed memory. */
3972 t
= savestring (name
);
3977 dispose_variable (old_var
);
3978 stupidly_hack_special_variables (t
);
3984 /* Get rid of all of the variables in the current context. */
3986 kill_all_local_variables (void)
3990 for (vc
= shell_variables
; vc
; vc
= vc
->down
)
3991 if (vc_isfuncenv (vc
) && vc
->scope
== variable_context
)
3996 if (vc
->table
&& vc_haslocals (vc
))
3998 delete_all_variables (vc
->table
);
3999 hash_dispose (vc
->table
);
4001 vc
->table
= (HASH_TABLE
*)NULL
;
4005 free_variable_hash_data (PTR_T data
)
4009 var
= (SHELL_VAR
*)data
;
4010 dispose_variable (var
);
4013 /* Delete the entire contents of the hash table. */
4015 delete_all_variables (HASH_TABLE
*hashed_vars
)
4017 hash_flush (hashed_vars
, free_variable_hash_data
);
4020 /* **************************************************************** */
4022 /* Setting variable attributes */
4024 /* **************************************************************** */
4026 #define FIND_OR_MAKE_VARIABLE(name, entry) \
4029 entry = find_variable (name); \
4032 entry = bind_variable (name, "", 0); \
4033 if (entry) entry->attributes |= att_invisible; \
4038 /* Make the variable associated with NAME be readonly.
4039 If NAME does not exist yet, create it. */
4041 set_var_read_only (char *name
)
4045 FIND_OR_MAKE_VARIABLE (name
, entry
);
4046 VSETATTR (entry
, att_readonly
);
4049 #ifdef INCLUDE_UNUSED
4050 /* Make the function associated with NAME be readonly.
4051 If NAME does not exist, we just punt, like auto_export code below. */
4053 set_func_read_only (const char *name
)
4057 entry
= find_function (name
);
4059 VSETATTR (entry
, att_readonly
);
4062 /* Make the variable associated with NAME be auto-exported.
4063 If NAME does not exist yet, create it. */
4065 set_var_auto_export (char *name
)
4069 FIND_OR_MAKE_VARIABLE (name
, entry
);
4070 set_auto_export (entry
);
4073 /* Make the function associated with NAME be auto-exported. */
4075 set_func_auto_export (const char *name
)
4079 entry
= find_function (name
);
4081 set_auto_export (entry
);
4085 /* **************************************************************** */
4087 /* Creating lists of variables */
4089 /* **************************************************************** */
4092 vlist_alloc (size_t nentries
)
4096 vlist
= (VARLIST
*)xmalloc (sizeof (VARLIST
));
4097 vlist
->list
= (SHELL_VAR
**)xmalloc ((nentries
+ 1) * sizeof (SHELL_VAR
*));
4098 vlist
->list_size
= nentries
;
4099 vlist
->list_len
= 0;
4100 vlist
->list
[0] = (SHELL_VAR
*)NULL
;
4106 vlist_realloc (VARLIST
*vlist
, size_t n
)
4109 return (vlist
= vlist_alloc (n
));
4110 if (n
> vlist
->list_size
)
4112 vlist
->list_size
= n
;
4113 vlist
->list
= (SHELL_VAR
**)xrealloc (vlist
->list
, (vlist
->list_size
+ 1) * sizeof (SHELL_VAR
*));
4119 vlist_add (VARLIST
*vlist
, SHELL_VAR
*var
, int flags
)
4123 for (i
= 0; i
< vlist
->list_len
; i
++)
4124 if (STREQ (var
->name
, vlist
->list
[i
]->name
))
4126 if (i
< vlist
->list_len
)
4129 if (i
>= vlist
->list_size
)
4130 vlist
= vlist_realloc (vlist
, vlist
->list_size
+ 16);
4132 vlist
->list
[vlist
->list_len
++] = var
;
4133 vlist
->list
[vlist
->list_len
] = (SHELL_VAR
*)NULL
;
4136 /* Map FUNCTION over the variables in VAR_HASH_TABLE. Return an array of the
4137 variables for which FUNCTION returns a non-zero value. A NULL value
4138 for FUNCTION means to use all variables. */
4140 map_over (sh_var_map_func_t
*function
, VAR_CONTEXT
*vc
)
4147 for (nentries
= 0, v
= vc
; v
; v
= v
->down
)
4148 nentries
+= HASH_ENTRIES (v
->table
);
4151 return (SHELL_VAR
**)NULL
;
4153 vlist
= vlist_alloc (nentries
);
4155 for (v
= vc
; v
; v
= v
->down
)
4156 flatten (v
->table
, function
, vlist
, 0);
4164 map_over_funcs (sh_var_map_func_t
*function
)
4169 if (shell_functions
== 0 || HASH_ENTRIES (shell_functions
) == 0)
4170 return ((SHELL_VAR
**)NULL
);
4172 vlist
= vlist_alloc (HASH_ENTRIES (shell_functions
));
4174 flatten (shell_functions
, function
, vlist
, 0);
4181 /* Flatten VAR_HASH_TABLE, applying FUNC to each member and adding those
4182 elements for which FUNC succeeds to VLIST->list. FLAGS is reserved
4183 for future use. Only unique names are added to VLIST. If FUNC is
4184 NULL, each variable in VAR_HASH_TABLE is added to VLIST. If VLIST is
4185 NULL, FUNC is applied to each SHELL_VAR in VAR_HASH_TABLE. If VLIST
4186 and FUNC are both NULL, nothing happens. */
4188 flatten (HASH_TABLE
*var_hash_table
, sh_var_map_func_t
*func
, VARLIST
*vlist
, int flags
)
4191 register BUCKET_CONTENTS
*tlist
;
4195 if (var_hash_table
== 0 || (HASH_ENTRIES (var_hash_table
) == 0) || (vlist
== 0 && func
== 0))
4198 for (i
= 0; i
< var_hash_table
->nbuckets
; i
++)
4200 for (tlist
= hash_items (i
, var_hash_table
); tlist
; tlist
= tlist
->next
)
4202 var
= (SHELL_VAR
*)tlist
->data
;
4204 r
= func
? (*func
) (var
) : 1;
4206 vlist_add (vlist
, var
, flags
);
4212 sort_variables (SHELL_VAR
**array
)
4214 qsort (array
, strvec_len ((char **)array
), sizeof (SHELL_VAR
*), (QSFUNC
*)qsort_var_comp
);
4218 qsort_var_comp (SHELL_VAR
**var1
, SHELL_VAR
**var2
)
4222 if ((result
= (*var1
)->name
[0] - (*var2
)->name
[0]) == 0)
4223 result
= strcmp ((*var1
)->name
, (*var2
)->name
);
4228 /* Apply FUNC to each variable in SHELL_VARIABLES, adding each one for
4229 which FUNC succeeds to an array of SHELL_VAR *s. Returns the array. */
4231 vapply (sh_var_map_func_t
*func
)
4235 list
= map_over (func
, shell_variables
);
4236 if (list
/* && posixly_correct */)
4237 sort_variables (list
);
4241 /* Apply FUNC to each variable in SHELL_FUNCTIONS, adding each one for
4242 which FUNC succeeds to an array of SHELL_VAR *s. Returns the array. */
4244 fapply (sh_var_map_func_t
*func
)
4248 list
= map_over_funcs (func
);
4249 if (list
/* && posixly_correct */)
4250 sort_variables (list
);
4254 /* Create a NULL terminated array of all the shell variables. */
4256 all_shell_variables (void)
4258 return (vapply ((sh_var_map_func_t
*)NULL
));
4261 /* Create a NULL terminated array of all the shell functions. */
4263 all_shell_functions (void)
4265 return (fapply ((sh_var_map_func_t
*)NULL
));
4269 visible_var (SHELL_VAR
*var
)
4271 return (invisible_p (var
) == 0);
4275 all_visible_functions (void)
4277 return (fapply (visible_var
));
4281 all_visible_variables (void)
4283 return (vapply (visible_var
));
4286 /* Return non-zero if the variable VAR is visible and exported. Array
4287 variables cannot be exported. */
4289 visible_and_exported (SHELL_VAR
*var
)
4291 return (invisible_p (var
) == 0 && exported_p (var
));
4294 /* Candidate variables for the export environment are either valid variables
4295 with the export attribute or invalid variables inherited from the initial
4296 environment and simply passed through. */
4298 export_environment_candidate (SHELL_VAR
*var
)
4300 return (exported_p (var
) && (invisible_p (var
) == 0 || imported_p (var
)));
4303 /* Return non-zero if VAR is a local variable in the current context and
4306 local_and_exported (SHELL_VAR
*var
)
4308 return (invisible_p (var
) == 0 && local_p (var
) && var
->context
== variable_context
&& exported_p (var
));
4312 all_exported_variables (void)
4314 return (vapply (visible_and_exported
));
4318 local_exported_variables (void)
4320 return (vapply (local_and_exported
));
4324 variable_in_context (SHELL_VAR
*var
)
4326 return (local_p (var
) && var
->context
== variable_context
);
4330 visible_variable_in_context (SHELL_VAR
*var
)
4332 return (invisible_p (var
) == 0 && local_p (var
) && var
->context
== variable_context
);
4336 all_local_variables (int visible_only
)
4342 vc
= shell_variables
;
4343 for (vc
= shell_variables
; vc
; vc
= vc
->down
)
4344 if (vc_isfuncenv (vc
) && vc
->scope
== variable_context
)
4349 internal_error (_("all_local_variables: no function context at current scope"));
4350 return (SHELL_VAR
**)NULL
;
4352 if (vc
->table
== 0 || HASH_ENTRIES (vc
->table
) == 0 || vc_haslocals (vc
) == 0)
4353 return (SHELL_VAR
**)NULL
;
4355 vlist
= vlist_alloc (HASH_ENTRIES (vc
->table
));
4358 flatten (vc
->table
, visible_variable_in_context
, vlist
, 0);
4360 flatten (vc
->table
, variable_in_context
, vlist
, 0);
4365 sort_variables (ret
);
4369 #if defined (ARRAY_VARS)
4370 /* Return non-zero if the variable VAR is visible and an array. */
4372 visible_array_vars (SHELL_VAR
*var
)
4374 return (invisible_p (var
) == 0 && (array_p (var
) || assoc_p (var
)));
4378 all_array_variables (void)
4380 return (vapply (visible_array_vars
));
4382 #endif /* ARRAY_VARS */
4385 all_variables_matching_prefix (const char *prefix
)
4387 SHELL_VAR
**varlist
;
4392 plen
= STRLEN (prefix
);
4393 varlist
= all_visible_variables ();
4394 for (vind
= 0; varlist
&& varlist
[vind
]; vind
++)
4396 if (varlist
== 0 || vind
== 0)
4397 return ((char **)NULL
);
4398 rlist
= strvec_create (vind
+ 1);
4399 for (vind
= rind
= 0; varlist
[vind
]; vind
++)
4401 if (plen
== 0 || STREQN (prefix
, varlist
[vind
]->name
, plen
))
4402 rlist
[rind
++] = savestring (varlist
[vind
]->name
);
4404 rlist
[rind
] = (char *)0;
4410 /* **************************************************************** */
4412 /* Managing temporary variable scopes */
4414 /* **************************************************************** */
4416 /* Make variable NAME have VALUE in the temporary environment. */
4418 bind_tempenv_variable (const char *name
, const char *value
)
4422 var
= temporary_env
? hash_lookup (name
, temporary_env
) : (SHELL_VAR
*)NULL
;
4426 FREE (value_cell (var
));
4427 var_setvalue (var
, savestring (value
));
4428 INVALIDATE_EXPORTSTR (var
);
4434 /* Find a variable in the temporary environment that is named NAME.
4435 Return the SHELL_VAR *, or NULL if not found. */
4437 find_tempenv_variable (const char *name
)
4439 return (temporary_env
? hash_lookup (name
, temporary_env
) : (SHELL_VAR
*)NULL
);
4442 char **tempvar_list
;
4445 /* Take a variable from an assignment statement preceding a posix special
4446 builtin (including `return') and create a global variable from it. This
4447 is called from merge_temporary_env, which is only called when in posix
4450 push_posix_temp_var (PTR_T data
)
4454 HASH_TABLE
*binding_table
;
4457 var
= (SHELL_VAR
*)data
;
4459 /* Just like do_assignment_internal(). This makes assignments preceding
4460 special builtins act like standalone assignment statements when in
4461 posix mode, satisfying the posix requirement that this affect the
4462 "current execution environment." */
4463 v
= bind_variable (var
->name
, value_cell (var
), ASS_FORCE
|ASS_NOLONGJMP
);
4465 /* XXX - do we need to worry about array variables here? */
4468 /* If this modifies an existing local variable, v->context will be non-zero.
4469 If it comes back with v->context == 0, we bound at the global context.
4470 Set binding_table appropriately. It doesn't matter whether it's correct
4471 if the variable is local, only that it's not global_variables->table */
4472 binding_table
= v
->context
? shell_variables
->table
: global_variables
->table
;
4475 /* global variables are no longer temporary and don't need propagating. */
4476 if (v
->context
== 0)
4477 var
->attributes
&= ~(att_tempvar
|att_propagate
);
4481 v
->attributes
|= var
->attributes
; /* preserve tempvar attribute if appropriate */
4482 /* If we don't bind a local variable, propagate the value. If we bind a
4483 local variable (the "current execution environment"), keep it as local
4484 and don't propagate it to the calling environment. */
4485 if (v
->context
> 0 && local_p (v
) == 0)
4486 v
->attributes
|= att_propagate
;
4488 v
->attributes
&= ~att_propagate
;
4491 if (find_special_var (var
->name
) >= 0)
4492 tempvar_list
[tvlist_ind
++] = savestring (var
->name
);
4494 dispose_variable (var
);
4497 /* Push the variable described by (SHELL_VAR *)DATA down to the next
4498 variable context from the temporary environment. This can be called
4500 1. propagate_temp_var: which is called to propagate variables in
4501 assignments like `var=value declare -x var' to the surrounding
4504 In this case, the variable should have the att_propagate flag set and
4505 we can create variables in the current scope.
4508 push_temp_var (PTR_T data
)
4511 HASH_TABLE
*binding_table
;
4513 var
= (SHELL_VAR
*)data
;
4515 binding_table
= shell_variables
->table
;
4516 if (binding_table
== 0)
4518 if (shell_variables
== global_variables
)
4519 /* shouldn't happen */
4520 binding_table
= shell_variables
->table
= global_variables
->table
= hash_create (VARIABLES_HASH_BUCKETS
);
4522 binding_table
= shell_variables
->table
= hash_create (TEMPENV_HASH_BUCKETS
);
4525 v
= bind_variable_internal (var
->name
, value_cell (var
), binding_table
, 0, ASS_FORCE
|ASS_NOLONGJMP
);
4527 #if defined (ARRAY_VARS)
4528 if (v
&& (array_p (var
) || assoc_p (var
)))
4529 arrayvar_copyval (var
, v
);
4532 /* XXX - should we set the context here? It shouldn't matter because of how
4533 assign_in_env works, but we do it anyway. */
4535 v
->context
= shell_variables
->scope
;
4537 if (binding_table
== global_variables
->table
) /* XXX */
4538 var
->attributes
&= ~(att_tempvar
|att_propagate
);
4541 var
->attributes
|= att_propagate
; /* XXX - propagate more than once? */
4542 if (binding_table
== shell_variables
->table
)
4543 shell_variables
->flags
|= VC_HASTMPVAR
;
4546 v
->attributes
|= var
->attributes
;
4548 if (find_special_var (var
->name
) >= 0)
4549 tempvar_list
[tvlist_ind
++] = savestring (var
->name
);
4551 dispose_variable (var
);
4554 /* Take a variable described by DATA and push it to the surrounding scope if
4555 the PROPAGATE attribute is set. That gets set by push_temp_var if we are
4556 taking a variable like `var=value declare -x var' and propagating it to
4557 the enclosing scope. */
4559 propagate_temp_var (PTR_T data
)
4563 var
= (SHELL_VAR
*)data
;
4564 if (tempvar_p (var
) && propagate_p (var
))
4565 push_temp_var (data
);
4568 if (find_special_var (var
->name
) >= 0)
4569 tempvar_list
[tvlist_ind
++] = savestring (var
->name
);
4570 dispose_variable (var
);
4574 /* Free the storage used in the hash table for temporary
4575 environment variables. PUSHF is a function to be called
4576 to free each hash table entry. It takes care of pushing variables
4577 to previous scopes if appropriate. PUSHF stores names of variables
4578 that require special handling (e.g., IFS) on tempvar_list, so this
4579 function can call stupidly_hack_special_variables on all the
4580 variables in the list when the temporary hash table is destroyed. */
4582 dispose_temporary_env (sh_free_func_t
*pushf
)
4585 HASH_TABLE
*disposer
;
4587 tempvar_list
= strvec_create (HASH_ENTRIES (temporary_env
) + 1);
4588 tempvar_list
[tvlist_ind
= 0] = 0;
4590 disposer
= temporary_env
;
4591 temporary_env
= (HASH_TABLE
*)NULL
;
4593 hash_flush (disposer
, pushf
);
4594 hash_dispose (disposer
);
4596 tempvar_list
[tvlist_ind
] = 0;
4598 array_needs_making
= 1;
4600 for (i
= 0; i
< tvlist_ind
; i
++)
4601 stupidly_hack_special_variables (tempvar_list
[i
]);
4603 strvec_dispose (tempvar_list
);
4609 dispose_used_env_vars (void)
4613 dispose_temporary_env (propagate_temp_var
);
4614 maybe_make_export_env ();
4618 /* Take all of the shell variables in the temporary environment HASH_TABLE
4619 and make shell variables from them at the current variable context.
4620 Right now, this is only called in Posix mode to implement the historical
4621 accident of creating global variables from assignment statements preceding
4622 special builtins, but we check in case this acquires another caller later. */
4624 merge_temporary_env (void)
4627 dispose_temporary_env (posixly_correct
? push_posix_temp_var
: push_temp_var
);
4630 /* Temporary function to use if we want to separate function and special
4631 builtin behavior. */
4633 merge_function_temporary_env (void)
4636 dispose_temporary_env (push_temp_var
);
4640 flush_temporary_env (void)
4644 hash_flush (temporary_env
, free_variable_hash_data
);
4645 hash_dispose (temporary_env
);
4646 temporary_env
= (HASH_TABLE
*)NULL
;
4650 /* **************************************************************** */
4652 /* Creating and manipulating the environment */
4654 /* **************************************************************** */
4656 static inline char *
4657 mk_env_string (const char *name
, const char *value
, int attributes
)
4659 size_t name_len
, value_len
;
4661 int isfunc
, isarray
;
4663 name_len
= strlen (name
);
4664 value_len
= STRLEN (value
);
4666 isfunc
= attributes
& att_function
;
4667 #if defined (ARRAY_VARS) && defined (ARRAY_EXPORT)
4668 isarray
= attributes
& (att_array
|att_assoc
);
4671 /* If we are exporting a shell function, construct the encoded function
4673 if (isfunc
&& value
)
4675 p
= (char *)xmalloc (BASHFUNC_PREFLEN
+ name_len
+ BASHFUNC_SUFFLEN
+ value_len
+ 2);
4677 memcpy (q
, BASHFUNC_PREFIX
, BASHFUNC_PREFLEN
);
4678 q
+= BASHFUNC_PREFLEN
;
4679 memcpy (q
, name
, name_len
);
4681 memcpy (q
, BASHFUNC_SUFFIX
, BASHFUNC_SUFFLEN
);
4682 q
+= BASHFUNC_SUFFLEN
;
4684 #if defined (ARRAY_VARS) && defined (ARRAY_EXPORT)
4685 else if (isarray
&& value
)
4687 if (attributes
& att_assoc
)
4688 p
= (char *)xmalloc (BASHASSOC_PREFLEN
+ name_len
+ BASHASSOC_SUFFLEN
+ value_len
+ 2);
4690 p
= (char *)xmalloc (BASHARRAY_PREFLEN
+ name_len
+ BASHARRAY_SUFFLEN
+ value_len
+ 2);
4692 if (attributes
& att_assoc
)
4694 memcpy (q
, BASHASSOC_PREFIX
, BASHASSOC_PREFLEN
);
4695 q
+= BASHASSOC_PREFLEN
;
4699 memcpy (q
, BASHARRAY_PREFIX
, BASHARRAY_PREFLEN
);
4700 q
+= BASHARRAY_PREFLEN
;
4702 memcpy (q
, name
, name_len
);
4704 /* These are actually the same currently */
4705 if (attributes
& att_assoc
)
4707 memcpy (q
, BASHASSOC_SUFFIX
, BASHASSOC_SUFFLEN
);
4708 q
+= BASHARRAY_SUFFLEN
;
4712 memcpy (q
, BASHARRAY_SUFFIX
, BASHARRAY_SUFFLEN
);
4713 q
+= BASHARRAY_SUFFLEN
;
4719 p
= (char *)xmalloc (2 + name_len
+ value_len
);
4720 memcpy (p
, name
, name_len
);
4725 if (value
&& *value
)
4729 t
= dequote_escapes (value
);
4730 value_len
= STRLEN (t
);
4731 memcpy (q
+ 1, t
, value_len
+ 1);
4735 memcpy (q
+ 1, value
, value_len
+ 1);
4746 valid_exportstr (SHELL_VAR
*v
)
4753 internal_error (_("%s has null exportstr"), v
->name
);
4756 if (legal_variable_starter ((unsigned char)*s
) == 0)
4758 internal_error (_("invalid character %d in exportstr for %s"), *s
, v
->name
);
4761 for (s
= v
->exportstr
+ 1; s
&& *s
; s
++)
4765 if (legal_variable_char ((unsigned char)*s
) == 0)
4767 internal_error (_("invalid character %d in exportstr for %s"), *s
, v
->name
);
4773 internal_error (_("no `=' in exportstr for %s"), v
->name
);
4780 #if defined (ARRAY_VARS)
4781 # define USE_EXPORTSTR (value == var->exportstr && array_p (var) == 0 && assoc_p (var) == 0)
4783 # define USE_EXPORTSTR (value == var->exportstr)
4787 make_env_array_from_var_list (SHELL_VAR
**vars
)
4789 register int i
, list_index
;
4790 register SHELL_VAR
*var
;
4791 char **list
, *value
;
4793 list
= strvec_create ((1 + strvec_len ((char **)vars
)));
4795 for (i
= 0, list_index
= 0; var
= vars
[i
]; i
++)
4797 #if defined (__CYGWIN__)
4798 /* We don't use the exportstr stuff on Cygwin at all. */
4799 INVALIDATE_EXPORTSTR (var
);
4802 /* If the value is generated dynamically, generate it here. */
4803 if (regen_p (var
) && var
->dynamic_value
)
4805 var
= (*(var
->dynamic_value
)) (var
);
4806 INVALIDATE_EXPORTSTR (var
);
4810 value
= var
->exportstr
;
4811 else if (function_p (var
))
4812 value
= named_function_string ((char *)NULL
, function_cell (var
), 0);
4813 #if defined (ARRAY_VARS)
4814 else if (array_p (var
))
4816 value
= array_to_assign (array_cell (var
), 0);
4818 continue; /* XXX array vars cannot yet be exported */
4819 # endif /* ARRAY_EXPORT */
4820 else if (assoc_p (var
))
4822 value
= assoc_to_assign (assoc_cell (var
), 0);
4824 continue; /* XXX associative array vars cannot yet be exported */
4825 # endif /* ARRAY_EXPORT */
4828 value
= value_cell (var
);
4832 /* Gee, I'd like to get away with not using savestring() if we're
4833 using the cached exportstr... */
4834 list
[list_index
] = USE_EXPORTSTR
? savestring (value
)
4835 : mk_env_string (var
->name
, value
, var
->attributes
);
4837 if (USE_EXPORTSTR
== 0)
4838 SAVE_EXPORTSTR (var
, list
[list_index
]);
4841 #undef USE_EXPORTSTR
4843 #if defined (ARRAY_VARS) && defined (ARRAY_EXPORT)
4844 if (array_p (var
) || assoc_p (var
))
4850 list
[list_index
] = (char *)NULL
;
4854 /* Make an array of assignment statements from the hash table
4855 HASHED_VARS which contains SHELL_VARs. Only visible, exported
4856 variables are eligible. */
4858 make_var_export_array (VAR_CONTEXT
*vcxt
)
4864 vars
= map_over (visible_and_exported
, vcxt
);
4866 vars
= map_over (export_environment_candidate
, vcxt
);
4870 return (char **)NULL
;
4872 list
= make_env_array_from_var_list (vars
);
4879 make_func_export_array (void)
4884 vars
= map_over_funcs (visible_and_exported
);
4886 return (char **)NULL
;
4888 list
= make_env_array_from_var_list (vars
);
4894 /* Add ENVSTR to the end of the exported environment, EXPORT_ENV. */
4895 #define add_to_export_env(envstr,do_alloc) \
4898 if (export_env_index >= (export_env_size - 1)) \
4900 export_env_size += 16; \
4901 export_env = strvec_resize (export_env, export_env_size); \
4902 environ = export_env; \
4904 export_env[export_env_index++] = (do_alloc) ? savestring (envstr) : envstr; \
4905 export_env[export_env_index] = (char *)NULL; \
4908 /* Add ASSIGN to EXPORT_ENV, or supersede a previous assignment in the
4909 array with the same left-hand side. Return the new EXPORT_ENV. */
4911 add_or_supercede_exported_var (char *assign
, int do_alloc
)
4916 equal_offset
= assignment (assign
, 0);
4917 if (equal_offset
== 0)
4918 return (export_env
);
4920 /* If this is a function, then only supersede the function definition.
4921 We do this by including the `=() {' in the comparison, like
4922 initialize_shell_variables does. */
4923 if (assign
[equal_offset
+ 1] == '(' &&
4924 strncmp (assign
+ equal_offset
+ 2, ") {", 3) == 0) /* } */
4927 for (i
= 0; i
< export_env_index
; i
++)
4929 if (STREQN (assign
, export_env
[i
], equal_offset
+ 1))
4931 free (export_env
[i
]);
4932 export_env
[i
] = do_alloc
? savestring (assign
) : assign
;
4933 return (export_env
);
4936 add_to_export_env (assign
, do_alloc
);
4937 return (export_env
);
4941 add_temp_array_to_env (char **temp_array
, int do_alloc
, int do_supercede
)
4945 if (temp_array
== 0)
4948 for (i
= 0; temp_array
[i
]; i
++)
4951 export_env
= add_or_supercede_exported_var (temp_array
[i
], do_alloc
);
4953 add_to_export_env (temp_array
[i
], do_alloc
);
4959 /* Make the environment array for the command about to be executed, if the
4960 array needs making. Otherwise, do nothing. If a shell action could
4961 change the array that commands receive for their environment, then the
4962 code should `array_needs_making++'.
4964 The order to add to the array is:
4966 list of var contexts whose head is shell_variables
4969 This is the shell variable lookup order. We add only new variable
4970 names at each step, which allows local variables and variables in
4971 the temporary environments to shadow variables in the global (or
4972 any previous) scope.
4976 n_shell_variables (void)
4981 for (n
= 0, vc
= shell_variables
; vc
; vc
= vc
->down
)
4982 n
+= HASH_ENTRIES (vc
->table
);
4987 chkexport (char *name
)
4991 v
= find_variable (name
);
4992 if (v
&& exported_p (v
))
4994 array_needs_making
= 1;
4995 maybe_make_export_env ();
5002 maybe_make_export_env (void)
5004 register char **temp_array
;
5006 VAR_CONTEXT
*tcxt
, *icxt
;
5008 if (array_needs_making
)
5011 strvec_flush (export_env
);
5013 /* Make a guess based on how many shell variables and functions we
5014 have. Since there will always be array variables, and array
5015 variables are not (yet) exported, this will always be big enough
5016 for the exported variables and functions. */
5017 new_size
= n_shell_variables () + HASH_ENTRIES (shell_functions
) + 1 +
5018 HASH_ENTRIES (temporary_env
) + HASH_ENTRIES (invalid_env
);
5019 if (new_size
> export_env_size
)
5021 export_env_size
= new_size
;
5022 export_env
= strvec_resize (export_env
, export_env_size
);
5023 environ
= export_env
;
5025 export_env
[export_env_index
= 0] = (char *)NULL
;
5027 /* Make a dummy variable context from the temporary_env, stick it on
5028 the front of shell_variables, call make_var_export_array on the
5029 whole thing to flatten it, and convert the list of SHELL_VAR *s
5030 to the form needed by the environment. */
5033 tcxt
= new_var_context ((char *)NULL
, 0);
5034 tcxt
->table
= temporary_env
;
5035 tcxt
->down
= shell_variables
;
5038 tcxt
= shell_variables
;
5042 icxt
= new_var_context ((char *)NULL
, 0);
5043 icxt
->table
= invalid_env
;
5049 temp_array
= make_var_export_array (icxt
);
5051 add_temp_array_to_env (temp_array
, 0, 0);
5056 if (tcxt
!= shell_variables
)
5059 #if defined (RESTRICTED_SHELL)
5060 /* Restricted shells may not export shell functions. */
5061 temp_array
= restricted
? (char **)0 : make_func_export_array ();
5063 temp_array
= make_func_export_array ();
5066 add_temp_array_to_env (temp_array
, 0, 0);
5068 array_needs_making
= 0;
5072 /* This is an efficiency hack. PWD and OLDPWD are auto-exported, so
5073 we will need to remake the exported environment every time we
5074 change directories. `_' is always put into the environment for
5075 every external command, so without special treatment it will always
5076 cause the environment to be remade.
5078 If there is no other reason to make the exported environment, we can
5079 just update the variables in place and mark the exported environment
5080 as no longer needing a remake. */
5082 update_export_env_inplace (char *env_prefix
, int preflen
, char *value
)
5086 evar
= (char *)xmalloc (STRLEN (value
) + preflen
+ 1);
5087 strcpy (evar
, env_prefix
);
5089 strcpy (evar
+ preflen
, value
);
5090 export_env
= add_or_supercede_exported_var (evar
, 0);
5093 /* We always put _ in the environment as the name of this command. */
5095 put_command_name_into_env (char *command_name
)
5097 update_export_env_inplace ("_=", 2, command_name
);
5100 /* **************************************************************** */
5102 /* Managing variable contexts */
5104 /* **************************************************************** */
5106 /* Allocate and return a new variable context with NAME and FLAGS.
5107 NAME can be NULL. */
5110 new_var_context (char *name
, int flags
)
5114 vc
= (VAR_CONTEXT
*)xmalloc (sizeof (VAR_CONTEXT
));
5115 vc
->name
= name
? savestring (name
) : (char *)NULL
;
5116 vc
->scope
= variable_context
;
5119 vc
->up
= vc
->down
= (VAR_CONTEXT
*)NULL
;
5120 vc
->table
= (HASH_TABLE
*)NULL
;
5125 /* Free a variable context and its data, including the hash table. Dispose
5126 all of the variables. */
5128 dispose_var_context (VAR_CONTEXT
*vc
)
5134 delete_all_variables (vc
->table
);
5135 hash_dispose (vc
->table
);
5141 /* Set VAR's scope level to the current variable context. */
5143 set_context (SHELL_VAR
*var
)
5145 return (var
->context
= variable_context
);
5148 /* Make a new variable context with NAME and FLAGS and a HASH_TABLE of
5149 temporary variables, and push it onto shell_variables. This is
5150 for shell functions. */
5152 push_var_context (char *name
, int flags
, HASH_TABLE
*tempvars
)
5155 int posix_func_behavior
;
5157 /* As of IEEE Std 1003.1-2017, assignment statements preceding shell
5158 functions no longer behave like assignment statements preceding
5159 special builtins, and do not persist in the current shell environment.
5160 This is austin group interp #654, though nobody implements it yet. */
5161 posix_func_behavior
= 0;
5163 vc
= new_var_context (name
, flags
);
5164 /* Posix interp 1009, temporary assignments preceding function calls modify
5165 the current environment *before* the command is executed. */
5166 if (posix_func_behavior
&& (flags
& VC_FUNCENV
) && tempvars
== temporary_env
)
5167 merge_temporary_env ();
5170 vc
->table
= tempvars
;
5171 /* Have to do this because the temp environment was created before
5172 variable_context was incremented. */
5173 /* XXX - only need to do it if flags&VC_FUNCENV */
5174 flatten (tempvars
, set_context
, (VARLIST
*)NULL
, 0);
5175 vc
->flags
|= VC_HASTMPVAR
;
5177 vc
->down
= shell_variables
;
5178 shell_variables
->up
= vc
;
5180 return (shell_variables
= vc
);
5183 /* This can be called from one of two code paths:
5184 1. pop_scope, which implements the posix rules for propagating variable
5185 assignments preceding special builtins to the surrounding scope
5186 (push_builtin_var -- isbltin == 1);
5187 2. pop_var_context, which is called from pop_context and implements the
5188 posix rules for propagating variable assignments preceding function
5189 calls to the surrounding scope (push_func_var -- isbltin == 0)
5191 It takes variables out of a temporary environment hash table. We take the
5196 push_posix_tempvar_internal (SHELL_VAR
*var
, int isbltin
)
5199 int posix_var_behavior
;
5201 /* As of IEEE Std 1003.1-2017, assignment statements preceding shell
5202 functions no longer behave like assignment statements preceding
5203 special builtins, and do not persist in the current shell environment.
5204 This is austin group interp #654, though nobody implements it yet. */
5205 posix_var_behavior
= posixly_correct
&& isbltin
;
5208 if (local_p (var
) && STREQ (var
->name
, "-"))
5210 set_current_options (value_cell (var
));
5214 /* This takes variable assignments preceding special builtins that can execute
5215 multiple commands (source, eval, etc.) and performs the equivalent of
5216 an assignment statement to modify the closest enclosing variable (the
5217 posix "current execution environment"). This makes the behavior the same
5218 as push_posix_temp_var; but the circumstances of calling are slightly
5220 else if (tempvar_p (var
) && posix_var_behavior
)
5222 /* similar to push_posix_temp_var */
5223 v
= bind_variable (var
->name
, value_cell (var
), ASS_FORCE
|ASS_NOLONGJMP
);
5226 v
->attributes
|= var
->attributes
;
5227 if (v
->context
== 0)
5228 v
->attributes
&= ~(att_tempvar
|att_propagate
);
5229 /* XXX - set att_propagate here if v->context > 0? */
5232 else if (tempvar_p (var
) && propagate_p (var
))
5234 /* Make sure we have a hash table to store the variable in while it is
5235 being propagated down to the global variables table. Create one if
5237 if ((vc_isfuncenv (shell_variables
) || vc_istempenv (shell_variables
)) && shell_variables
->table
== 0)
5238 shell_variables
->table
= hash_create (VARIABLES_HASH_BUCKETS
);
5239 v
= bind_variable_internal (var
->name
, value_cell (var
), shell_variables
->table
, 0, 0);
5240 /* XXX - should we set v->context here? */
5242 v
->context
= shell_variables
->scope
;
5243 if (shell_variables
== global_variables
)
5244 var
->attributes
&= ~(att_tempvar
|att_propagate
);
5246 shell_variables
->flags
|= VC_HASTMPVAR
;
5248 v
->attributes
|= var
->attributes
;
5251 stupidly_hack_special_variables (var
->name
); /* XXX */
5253 #if defined (ARRAY_VARS)
5254 if (v
&& (array_p (var
) || assoc_p (var
)))
5255 arrayvar_copyval (var
, v
);
5258 dispose_variable (var
);
5262 push_func_var (PTR_T data
)
5266 var
= (SHELL_VAR
*)data
;
5267 push_posix_tempvar_internal (var
, 0);
5271 push_builtin_var (PTR_T data
)
5275 var
= (SHELL_VAR
*)data
;
5276 push_posix_tempvar_internal (var
, 1);
5279 /* Pop the top context off of VCXT and dispose of it, returning the rest of
5282 pop_var_context (void)
5284 VAR_CONTEXT
*ret
, *vcxt
;
5286 vcxt
= shell_variables
;
5287 if (vc_isfuncenv (vcxt
) == 0)
5289 /* If we haven't flushed all of the local contexts already, flag an error */
5290 if (shell_variables
!= global_variables
|| variable_context
> 0)
5291 internal_error (_("pop_var_context: head of shell_variables not a function context"));
5295 if (ret
= vcxt
->down
)
5297 ret
->up
= (VAR_CONTEXT
*)NULL
;
5298 shell_variables
= ret
;
5300 hash_flush (vcxt
->table
, push_func_var
);
5301 dispose_var_context (vcxt
);
5304 internal_error (_("pop_var_context: no global_variables context"));
5308 delete_local_contexts (VAR_CONTEXT
*vcxt
)
5312 for (v
= vcxt
; v
!= global_variables
; v
= t
)
5315 dispose_var_context (v
);
5319 /* Delete the HASH_TABLEs for all variable contexts beginning at VCXT, and
5320 all of the VAR_CONTEXTs except GLOBAL_VARIABLES. */
5322 delete_all_contexts (VAR_CONTEXT
*vcxt
)
5324 delete_local_contexts (vcxt
);
5325 delete_all_variables (global_variables
->table
);
5326 shell_variables
= global_variables
;
5329 /* Reset the context so we are not executing in a shell function. Only call
5330 this if you are getting ready to exit the shell. */
5332 reset_local_contexts (void)
5334 delete_local_contexts (shell_variables
);
5335 shell_variables
= global_variables
;
5336 variable_context
= 0;
5339 /* **************************************************************** */
5341 /* Pushing and Popping temporary variable scopes */
5343 /* **************************************************************** */
5346 push_scope (int flags
, HASH_TABLE
*tmpvars
)
5348 return (push_var_context ((char *)NULL
, flags
, tmpvars
));
5352 push_exported_var (PTR_T data
)
5356 var
= (SHELL_VAR
*)data
;
5358 /* If a temp var had its export attribute set, or it's marked to be
5359 propagated, bind it in the previous scope before disposing it. */
5360 /* XXX - This isn't exactly right, because all tempenv variables have the
5361 export attribute set. */
5362 if (tempvar_p (var
) && exported_p (var
) && propagate_p (var
))
5364 var
->attributes
&= ~att_tempvar
; /* XXX */
5365 v
= bind_variable_internal (var
->name
, value_cell (var
), shell_variables
->table
, 0, 0);
5366 if (shell_variables
== global_variables
)
5367 var
->attributes
&= ~att_propagate
;
5370 v
->attributes
|= var
->attributes
;
5371 v
->context
= shell_variables
->scope
;
5375 stupidly_hack_special_variables (var
->name
); /* XXX */
5377 dispose_variable (var
);
5380 /* This is called to propagate variables in the temporary environment of a
5381 special builtin (if IS_SPECIAL != 0) or exported variables that are the
5382 result of a builtin like `source' or `command' that can operate on the
5383 variables in its temporary environment. In the first case, we call
5384 push_builtin_var, which does the right thing. */
5386 pop_scope (void *is_special
)
5388 VAR_CONTEXT
*vcxt
, *ret
;
5391 vcxt
= shell_variables
;
5392 if (vc_istempscope (vcxt
) == 0)
5394 internal_error (_("pop_scope: head of shell_variables not a temporary environment scope"));
5397 is_bltinenv
= vc_isbltnenv (vcxt
); /* XXX - for later */
5401 ret
->up
= (VAR_CONTEXT
*)NULL
;
5403 shell_variables
= ret
;
5405 /* Now we can take care of merging variables in VCXT into set of scopes
5406 whose head is RET (shell_variables). */
5410 if ((intptr_t) is_special
)
5411 hash_flush (vcxt
->table
, push_builtin_var
);
5413 hash_flush (vcxt
->table
, push_exported_var
);
5414 hash_dispose (vcxt
->table
);
5418 sv_ifs ("IFS"); /* XXX here for now */
5421 /* **************************************************************** */
5423 /* Pushing and Popping function contexts */
5425 /* **************************************************************** */
5427 struct saved_dollar_vars
{
5433 static struct saved_dollar_vars
*dollar_arg_stack
= (struct saved_dollar_vars
*)NULL
;
5434 static size_t dollar_arg_stack_slots
;
5435 static int dollar_arg_stack_index
;
5437 /* Functions to manipulate dollar_vars array. Need to keep these in sync with
5438 whatever remember_args() does. */
5440 save_dollar_vars (void)
5445 ret
= strvec_create (10);
5446 for (i
= 1; i
< 10; i
++)
5448 ret
[i
] = dollar_vars
[i
];
5449 dollar_vars
[i
] = (char *)NULL
;
5455 restore_dollar_vars (char **args
)
5459 for (i
= 1; i
< 10; i
++)
5460 dollar_vars
[i
] = args
[i
];
5464 free_dollar_vars (void)
5468 for (i
= 1; i
< 10; i
++)
5470 FREE (dollar_vars
[i
]);
5471 dollar_vars
[i
] = (char *)NULL
;
5476 free_saved_dollar_vars (char **args
)
5480 for (i
= 1; i
< 10; i
++)
5484 /* Do what remember_args (xxx, 1) would have done. */
5486 clear_dollar_vars (void)
5488 free_dollar_vars ();
5489 dispose_words (rest_of_args
);
5491 rest_of_args
= (WORD_LIST
*)NULL
;
5495 /* XXX - should always be followed by remember_args () */
5496 /* NAME is the function name */
5498 push_context (char *name
, int is_subshell
, HASH_TABLE
*tempvars
)
5500 if (is_subshell
== 0)
5501 push_dollar_vars ();
5503 push_var_context (name
, VC_FUNCENV
, tempvars
);
5506 /* Only called when subshell == 0, so we don't need to check, and can
5507 unconditionally pop the dollar vars off the stack. */
5509 pop_context (void *ignore
)
5515 sv_ifs ("IFS"); /* XXX here for now */
5518 /* Save the existing positional parameters on a stack. */
5520 push_dollar_vars (void)
5522 if (dollar_arg_stack_index
+ 2 > dollar_arg_stack_slots
)
5524 dollar_arg_stack
= (struct saved_dollar_vars
*)
5525 xrealloc (dollar_arg_stack
, (dollar_arg_stack_slots
+= 10)
5526 * sizeof (struct saved_dollar_vars
));
5529 dollar_arg_stack
[dollar_arg_stack_index
].count
= posparam_count
;
5530 dollar_arg_stack
[dollar_arg_stack_index
].first_ten
= save_dollar_vars ();
5531 dollar_arg_stack
[dollar_arg_stack_index
++].rest
= rest_of_args
;
5532 rest_of_args
= (WORD_LIST
*)NULL
;
5535 dollar_arg_stack
[dollar_arg_stack_index
].first_ten
= (char **)NULL
;
5536 dollar_arg_stack
[dollar_arg_stack_index
].rest
= (WORD_LIST
*)NULL
;
5539 /* Restore the positional parameters from our stack. */
5541 pop_dollar_vars (void)
5543 if (dollar_arg_stack
== 0 || dollar_arg_stack_index
== 0)
5546 /* Wipe out current values */
5547 clear_dollar_vars ();
5549 rest_of_args
= dollar_arg_stack
[--dollar_arg_stack_index
].rest
;
5550 restore_dollar_vars (dollar_arg_stack
[dollar_arg_stack_index
].first_ten
);
5551 free (dollar_arg_stack
[dollar_arg_stack_index
].first_ten
);
5552 posparam_count
= dollar_arg_stack
[dollar_arg_stack_index
].count
;
5554 dollar_arg_stack
[dollar_arg_stack_index
].first_ten
= (char **)NULL
;
5555 dollar_arg_stack
[dollar_arg_stack_index
].rest
= (WORD_LIST
*)NULL
;
5556 dollar_arg_stack
[dollar_arg_stack_index
].count
= 0;
5558 set_dollar_vars_unchanged ();
5559 invalidate_cached_quoted_dollar_at ();
5563 dispose_saved_dollar_vars (void)
5565 if (dollar_arg_stack
== 0 || dollar_arg_stack_index
== 0)
5568 dispose_words (dollar_arg_stack
[--dollar_arg_stack_index
].rest
);
5569 free_saved_dollar_vars (dollar_arg_stack
[dollar_arg_stack_index
].first_ten
);
5570 free (dollar_arg_stack
[dollar_arg_stack_index
].first_ten
);
5572 dollar_arg_stack
[dollar_arg_stack_index
].first_ten
= (char **)NULL
;
5573 dollar_arg_stack
[dollar_arg_stack_index
].rest
= (WORD_LIST
*)NULL
;
5574 dollar_arg_stack
[dollar_arg_stack_index
].count
= 0;
5577 /* Initialize BASH_ARGV and BASH_ARGC after turning on extdebug after the
5578 shell is initialized */
5580 init_bash_argv (void)
5582 if (bash_argv_initialized
== 0)
5585 bash_argv_initialized
= 1;
5590 save_bash_argv (void)
5594 list
= list_rest_of_args ();
5596 dispose_words (list
);
5599 /* Manipulate the special BASH_ARGV and BASH_ARGC variables. */
5602 push_args (WORD_LIST
*list
)
5604 #if defined (ARRAY_VARS) && defined (DEBUGGER)
5605 SHELL_VAR
*bash_argv_v
, *bash_argc_v
;
5606 ARRAY
*bash_argv_a
, *bash_argc_a
;
5611 GET_ARRAY_FROM_VAR ("BASH_ARGV", bash_argv_v
, bash_argv_a
);
5612 GET_ARRAY_FROM_VAR ("BASH_ARGC", bash_argc_v
, bash_argc_a
);
5614 for (l
= list
, i
= 0; l
; l
= l
->next
, i
++)
5615 array_push (bash_argv_a
, l
->word
->word
);
5618 array_push (bash_argc_a
, t
);
5620 #endif /* ARRAY_VARS && DEBUGGER */
5623 /* Remove arguments from BASH_ARGV array. Pop top element off BASH_ARGC
5624 array and use that value as the count of elements to remove from
5629 #if defined (ARRAY_VARS) && defined (DEBUGGER)
5630 SHELL_VAR
*bash_argv_v
, *bash_argc_v
;
5631 ARRAY
*bash_argv_a
, *bash_argc_a
;
5635 GET_ARRAY_FROM_VAR ("BASH_ARGV", bash_argv_v
, bash_argv_a
);
5636 GET_ARRAY_FROM_VAR ("BASH_ARGC", bash_argc_v
, bash_argc_a
);
5638 ce
= array_unshift_element (bash_argc_a
);
5639 if (ce
== 0 || legal_number (element_value (ce
), &i
) == 0)
5643 array_pop (bash_argv_a
);
5644 array_dispose_element (ce
);
5645 #endif /* ARRAY_VARS && DEBUGGER */
5649 uw_pop_args (void *ignore
)
5654 /*************************************************
5656 * Functions to manage special variables *
5658 *************************************************/
5660 /* Extern declarations for variables this code has to manage. */
5662 /* An alist of name.function for each special variable. Most of the
5663 functions don't do much, and in fact, this would be faster with a
5664 switch statement, but by the end of this file, I am sick of switch
5667 #define SET_INT_VAR(name, intvar) intvar = find_variable (name) != 0
5669 /* This table will be sorted with qsort() the first time it's accessed. */
5670 struct name_and_function
{
5672 sh_sv_func_t
*function
;
5675 static struct name_and_function special_vars
[] = {
5676 { "BASH_COMPAT", sv_shcompat
},
5677 { "BASH_XTRACEFD", sv_xtracefd
},
5679 #if defined (JOB_CONTROL)
5680 { "CHILD_MAX", sv_childmax
},
5683 #if defined (READLINE)
5684 # if defined (STRICT_POSIX)
5685 { "COLUMNS", sv_winsize
},
5687 { "COMP_WORDBREAKS", sv_comp_wordbreaks
},
5690 { "EXECIGNORE", sv_execignore
},
5692 { "FUNCNEST", sv_funcnest
},
5694 { "GLOBIGNORE", sv_globignore
},
5695 { "GLOBSORT", sv_globsort
},
5697 #if defined (HISTORY)
5698 { "HISTCONTROL", sv_history_control
},
5699 { "HISTFILESIZE", sv_histsize
},
5700 { "HISTIGNORE", sv_histignore
},
5701 { "HISTSIZE", sv_histsize
},
5702 { "HISTTIMEFORMAT", sv_histtimefmt
},
5705 #if defined (__CYGWIN__)
5706 { "HOME", sv_home
},
5709 #if defined (READLINE)
5710 { "HOSTFILE", sv_hostfile
},
5714 { "IGNOREEOF", sv_ignoreeof
},
5716 { "LANG", sv_locale
},
5717 { "LC_ALL", sv_locale
},
5718 { "LC_COLLATE", sv_locale
},
5719 { "LC_CTYPE", sv_locale
},
5720 { "LC_MESSAGES", sv_locale
},
5721 { "LC_NUMERIC", sv_locale
},
5722 { "LC_TIME", sv_locale
},
5724 #if defined (READLINE) && defined (STRICT_POSIX)
5725 { "LINES", sv_winsize
},
5728 { "MAIL", sv_mail
},
5729 { "MAILCHECK", sv_mail
},
5730 { "MAILPATH", sv_mail
},
5732 { "OPTERR", sv_opterr
},
5733 { "OPTIND", sv_optind
},
5735 { "PATH", sv_path
},
5736 { "POSIXLY_CORRECT", sv_strict_posix
},
5738 #if defined (READLINE)
5739 { "TERM", sv_terminal
},
5740 { "TERMCAP", sv_terminal
},
5741 { "TERMINFO", sv_terminal
},
5742 #endif /* READLINE */
5744 { "TEXTDOMAIN", sv_locale
},
5745 { "TEXTDOMAINDIR", sv_locale
},
5747 #if defined (HAVE_TZSET)
5751 #if defined (HISTORY) && defined (BANG_HISTORY)
5752 { "histchars", sv_histchars
},
5753 #endif /* HISTORY && BANG_HISTORY */
5755 { "ignoreeof", sv_ignoreeof
},
5757 { (char *)0, (sh_sv_func_t
*)0 }
5760 #define N_SPECIAL_VARS (sizeof (special_vars) / sizeof (special_vars[0]) - 1)
5763 sv_compare (struct name_and_function
*sv1
, struct name_and_function
*sv2
)
5767 if ((r
= sv1
->name
[0] - sv2
->name
[0]) == 0)
5768 r
= strcmp (sv1
->name
, sv2
->name
);
5773 find_special_var (const char *name
)
5777 for (i
= 0; special_vars
[i
].name
; i
++)
5779 r
= special_vars
[i
].name
[0] - name
[0];
5781 r
= strcmp (special_vars
[i
].name
, name
);
5785 /* Can't match any of rest of elements in sorted list. Take this out
5786 if it causes problems in certain environments. */
5792 /* The variable in NAME has just had its state changed. Check to see if it
5793 is one of the special ones where something special happens. */
5795 stupidly_hack_special_variables (const char *name
)
5797 static int sv_sorted
= 0;
5800 if (sv_sorted
== 0) /* shouldn't need, but it's fairly cheap. */
5802 qsort (special_vars
, N_SPECIAL_VARS
, sizeof (special_vars
[0]),
5803 (QSFUNC
*)sv_compare
);
5807 i
= find_special_var (name
);
5809 (*(special_vars
[i
].function
)) ((char *)name
);
5812 /* Special variables that need hooks to be run when they are unset as part
5813 of shell reinitialization should have their sv_ functions run here. */
5815 reinit_special_variables (void)
5817 #if defined (READLINE)
5818 sv_comp_wordbreaks ("COMP_WORDBREAKS");
5820 sv_globignore ("GLOBIGNORE");
5821 sv_opterr ("OPTERR");
5825 sv_ifs (const char *name
)
5829 v
= find_variable ("IFS");
5833 /* What to do just after the PATH variable has changed. */
5835 sv_path (const char *name
)
5841 /* What to do just after one of the MAILxxxx variables has changed. NAME
5842 is the name of the variable. This is called with NAME set to one of
5843 MAIL, MAILCHECK, or MAILPATH. */
5845 sv_mail (const char *name
)
5847 /* If the time interval for checking the files has changed, then
5848 reset the mail timer. Otherwise, one of the pathname vars
5849 to the users mailbox has changed, so rebuild the array of
5851 if (name
[4] == 'C') /* if (strcmp (name, "MAILCHECK") == 0) */
5852 reset_mail_timer ();
5856 remember_mail_dates ();
5861 sv_funcnest (const char *name
)
5866 v
= find_variable (name
);
5869 else if (legal_number (value_cell (v
), &num
) == 0)
5875 /* What to do when EXECIGNORE changes. */
5877 sv_execignore (const char *name
)
5879 setup_exec_ignore (name
);
5882 /* What to do when GLOBIGNORE changes. */
5884 sv_globignore (const char *name
)
5886 if (privileged_mode
== 0)
5887 setup_glob_ignore (name
);
5890 /* What to do when GLOBSORT changes. */
5892 sv_globsort (const char *name
)
5894 setup_globsort (name
);
5897 #if defined (READLINE)
5899 sv_comp_wordbreaks (const char *name
)
5903 sv
= find_variable (name
);
5905 reset_completer_word_break_chars ();
5908 /* What to do just after one of the TERMxxx variables has changed.
5909 If we are an interactive shell, then try to reset the terminal
5910 information in readline. */
5912 sv_terminal (const char *name
)
5914 if (interactive_shell
&& no_line_editing
== 0)
5915 rl_reset_terminal (get_string_value ("TERM"));
5919 sv_hostfile (const char *name
)
5923 v
= find_variable (name
);
5925 clear_hostname_list ();
5927 hostname_list_initialized
= 0;
5930 #if defined (STRICT_POSIX)
5931 /* In strict posix mode, we allow assignments to LINES and COLUMNS (and values
5932 found in the initial environment) to override the terminal size reported by
5935 sv_winsize (const char *name
)
5941 if (posixly_correct
== 0 || interactive_shell
== 0 || no_line_editing
)
5944 v
= find_variable (name
);
5945 if (v
== 0 || var_isset (v
) == 0)
5946 rl_reset_screen_size ();
5949 if (legal_number (value_cell (v
), &xd
) == 0)
5951 winsize_assignment
= 1;
5952 d
= xd
; /* truncate */
5953 if (name
[0] == 'L') /* LINES */
5954 rl_set_screen_size (d
, -1);
5956 rl_set_screen_size (-1, d
);
5957 winsize_assignment
= 0;
5960 #endif /* STRICT_POSIX */
5961 #endif /* READLINE */
5963 /* Update the value of HOME in the export environment so tilde expansion will
5965 #if defined (__CYGWIN__)
5966 sv_home (const char *name
)
5968 array_needs_making
= 1;
5969 maybe_make_export_env ();
5973 #if defined (HISTORY)
5974 /* What to do after the HISTSIZE or HISTFILESIZE variables change.
5975 If there is a value for this HISTSIZE (and it is numeric), then stifle
5976 the history. Otherwise, if there is NO value for this variable,
5977 unstifle the history. If name is HISTFILESIZE, and its value is
5978 numeric, truncate the history file to hold no more than that many
5981 sv_histsize (const char *name
)
5987 temp
= get_string_value (name
);
5991 if (legal_number (temp
, &num
))
5994 if (hmax
< 0 && name
[4] == 'S')
5995 unstifle_history (); /* unstifle history if HISTSIZE < 0 */
5996 else if (name
[4] == 'S')
5998 stifle_history (hmax
);
5999 hmax
= where_history ();
6000 if (history_lines_this_session
> hmax
)
6001 history_lines_this_session
= hmax
;
6003 else if (hmax
>= 0) /* truncate HISTFILE if HISTFILESIZE >= 0 */
6005 history_truncate_file (get_string_value ("HISTFILE"), hmax
);
6006 /* If we just shrank the history file to fewer lines than we've
6007 already read, make sure we adjust our idea of how many lines
6008 we have read from the file. */
6009 if (hmax
< history_lines_in_file
)
6010 history_lines_in_file
= hmax
;
6014 else if (name
[4] == 'S')
6015 unstifle_history ();
6018 /* What to do after the HISTIGNORE variable changes. */
6020 sv_histignore (const char *name
)
6022 setup_history_ignore (name
);
6025 /* What to do after the HISTCONTROL variable changes. */
6027 sv_history_control (const char *name
)
6033 history_control
= 0;
6034 temp
= get_string_value (name
);
6036 if (temp
== 0 || *temp
== 0)
6040 while (val
= extract_colon_unit (temp
, &tptr
))
6042 if (STREQ (val
, "ignorespace"))
6043 history_control
|= HC_IGNSPACE
;
6044 else if (STREQ (val
, "ignoredups"))
6045 history_control
|= HC_IGNDUPS
;
6046 else if (STREQ (val
, "ignoreboth"))
6047 history_control
|= HC_IGNBOTH
;
6048 else if (STREQ (val
, "erasedups"))
6049 history_control
|= HC_ERASEDUPS
;
6055 #if defined (BANG_HISTORY)
6056 /* Setting/unsetting of the history expansion character. */
6058 sv_histchars (const char *name
)
6062 temp
= get_string_value (name
);
6065 history_expansion_char
= *temp
;
6066 if (temp
[0] && temp
[1])
6068 history_subst_char
= temp
[1];
6070 history_comment_char
= temp
[2];
6075 history_expansion_char
= '!';
6076 history_subst_char
= '^';
6077 history_comment_char
= '#';
6080 #endif /* BANG_HISTORY */
6083 sv_histtimefmt (const char *name
)
6087 if (v
= find_variable (name
))
6089 if (history_comment_char
== 0)
6090 history_comment_char
= '#';
6092 history_write_timestamps
= (v
!= 0);
6094 #endif /* HISTORY */
6096 #if defined (HAVE_TZSET)
6098 sv_tz (const char *name
)
6102 v
= find_variable (name
);
6103 if (v
&& exported_p (v
))
6104 array_needs_making
= 1;
6106 array_needs_making
= 1;
6108 if (array_needs_making
)
6110 maybe_make_export_env ();
6116 /* If the variable exists, then the value of it can be the number
6117 of times we actually ignore the EOF. The default is small,
6118 (smaller than csh, anyway). */
6120 sv_ignoreeof (const char *name
)
6125 eof_encountered
= 0;
6127 tmp_var
= find_variable (name
);
6128 ignoreeof
= tmp_var
&& var_isset (tmp_var
);
6129 temp
= tmp_var
? value_cell (tmp_var
) : (char *)NULL
;
6131 eof_encountered_limit
= (*temp
&& all_digits (temp
)) ? atoi (temp
) : 10;
6132 set_shellopts (); /* make sure `ignoreeof' is/is not in $SHELLOPTS */
6136 sv_optind (const char *name
)
6142 var
= find_variable ("OPTIND");
6143 tt
= var
? get_variable_value (var
) : (char *)NULL
;
6145 /* Assume that if var->context < variable_context and variable_context > 0
6146 then we are restoring the variables's previous state while returning
6152 /* According to POSIX, setting OPTIND=1 resets the internal state
6154 if (s
< 0 || s
== 1)
6163 sv_opterr (const char *name
)
6167 tt
= get_string_value ("OPTERR");
6168 sh_opterr
= (tt
&& *tt
) ? atoi (tt
) : 1;
6172 sv_strict_posix (const char *name
)
6176 var
= find_variable (name
);
6177 posixly_correct
= var
&& var_isset (var
);
6178 posix_initialize (posixly_correct
);
6179 #if defined (READLINE)
6180 if (interactive_shell
)
6181 posix_readline_initialize (posixly_correct
);
6182 #endif /* READLINE */
6183 set_shellopts (); /* make sure `posix' is/is not in $SHELLOPTS */
6187 sv_locale (const char *name
)
6192 v
= get_string_value (name
);
6193 if (name
[0] == 'L' && name
[1] == 'A') /* LANG */
6194 r
= set_lang (name
, v
);
6196 r
= set_locale_var (name
, v
); /* LC_*, TEXTDOMAIN* */
6199 if (r
== 0 && posixly_correct
)
6200 set_exit_status (EXECUTION_FAILURE
);
6204 #if defined (ARRAY_VARS)
6206 set_pipestatus_array (int *ps
, int nproc
)
6212 char *t
, tbuf
[INT_STRLEN_BOUND(int) + 1];
6214 v
= find_variable ("PIPESTATUS");
6216 v
= make_new_array_variable ("PIPESTATUS");
6217 if (array_p (v
) == 0)
6218 return; /* Do nothing if not an array variable. */
6221 if (a
== 0 || array_num_elements (a
) == 0)
6223 for (i
= 0; i
< nproc
; i
++) /* was ps[i] != -1, not i < nproc */
6225 t
= inttostr (ps
[i
], tbuf
, sizeof (tbuf
));
6226 array_insert (a
, i
, t
);
6232 if (array_num_elements (a
) == nproc
&& nproc
== 1)
6234 #ifndef ALT_ARRAY_IMPLEMENTATION
6235 ae
= element_forw (a
->head
);
6237 ae
= a
->elements
[0];
6239 ARRAY_ELEMENT_REPLACE (ae
, itos (ps
[0]));
6241 else if (array_num_elements (a
) <= nproc
)
6243 /* modify in array_num_elements members in place, then add */
6244 #ifndef ALT_ARRAY_IMPLEMENTATION
6247 for (i
= 0; i
< array_num_elements (a
); i
++)
6249 #ifndef ALT_ARRAY_IMPLEMENTATION
6250 ae
= element_forw (ae
);
6252 ae
= a
->elements
[i
];
6254 ARRAY_ELEMENT_REPLACE (ae
, itos (ps
[i
]));
6257 for ( ; i
< nproc
; i
++)
6259 t
= inttostr (ps
[i
], tbuf
, sizeof (tbuf
));
6260 array_insert (a
, i
, t
);
6265 #ifndef ALT_ARRAY_IMPLEMENTATION
6266 /* deleting elements. it's faster to rebuild the array. */
6268 for (i
= 0; i
< nproc
; i
++)
6270 t
= inttostr (ps
[i
], tbuf
, sizeof (tbuf
));
6271 array_insert (a
, i
, t
);
6274 /* deleting elements. replace the first NPROC, free the rest */
6275 for (i
= 0; i
< nproc
; i
++)
6277 ae
= a
->elements
[i
];
6278 ARRAY_ELEMENT_REPLACE (ae
, itos (ps
[i
]));
6280 for ( ; i
<= array_max_index (a
); i
++)
6282 array_dispose_element (a
->elements
[i
]);
6283 a
->elements
[i
] = (ARRAY_ELEMENT
*)NULL
;
6286 /* bookkeeping usually taken care of by array_insert */
6287 set_max_index (a
, nproc
- 1);
6288 set_first_index (a
, 0);
6289 set_num_elements (a
, nproc
);
6290 #endif /* ALT_ARRAY_IMPLEMENTATION */
6295 save_pipestatus_array (void)
6300 v
= find_variable ("PIPESTATUS");
6301 if (v
== 0 || array_p (v
) == 0 || array_cell (v
) == 0)
6302 return ((ARRAY
*)NULL
);
6304 a
= array_copy (array_cell (v
));
6310 restore_pipestatus_array (ARRAY
*a
)
6315 v
= find_variable ("PIPESTATUS");
6316 /* XXX - should we still assign even if existing value is NULL? */
6317 if (v
== 0 || array_p (v
) == 0 || array_cell (v
) == 0)
6320 a2
= array_cell (v
);
6321 var_setarray (v
, a
);
6328 set_pipestatus_from_exit (int s
)
6330 #if defined (ARRAY_VARS)
6331 static int v
[2] = { 0, -1 };
6334 set_pipestatus_array (v
, 1);
6339 sv_xtracefd (const char *name
)
6346 v
= find_variable (name
);
6354 if (t
== 0 || *t
== 0)
6358 fd
= (int)strtol (t
, &e
, 10);
6359 if (e
!= t
&& *e
== '\0' && sh_validfd (fd
))
6361 fp
= fdopen (fd
, "w");
6363 internal_error (_("%s: %s: cannot open as FILE"), name
, value_cell (v
));
6365 xtrace_set (fd
, fp
);
6368 internal_error (_("%s: %s: invalid value for trace file descriptor"), name
, value_cell (v
));
6372 #define MIN_COMPAT_LEVEL 31
6375 sv_shcompat (const char *name
)
6379 int tens
, ones
, compatval
;
6381 v
= find_variable (name
);
6384 shell_compatibility_level
= DEFAULT_COMPAT_LEVEL
;
6385 set_compatibility_opts ();
6388 val
= value_cell (v
);
6389 if (val
== 0 || *val
== '\0')
6391 shell_compatibility_level
= DEFAULT_COMPAT_LEVEL
;
6392 set_compatibility_opts ();
6395 /* Handle decimal-like compatibility version specifications: 4.2 */
6396 if (ISDIGIT (val
[0]) && val
[1] == '.' && ISDIGIT (val
[2]) && val
[3] == 0)
6398 tens
= val
[0] - '0';
6399 ones
= val
[2] - '0';
6400 compatval
= tens
*10 + ones
;
6402 /* Handle integer-like compatibility version specifications: 42 */
6403 else if (ISDIGIT (val
[0]) && ISDIGIT (val
[1]) && val
[2] == 0)
6405 tens
= val
[0] - '0';
6406 ones
= val
[1] - '0';
6407 compatval
= tens
*10 + ones
;
6412 internal_error (_("%s: %s: compatibility value out of range"), name
, val
);
6413 shell_compatibility_level
= DEFAULT_COMPAT_LEVEL
;
6414 set_compatibility_opts ();
6418 if (compatval
< MIN_COMPAT_LEVEL
|| compatval
> DEFAULT_COMPAT_LEVEL
)
6421 shell_compatibility_level
= compatval
;
6422 set_compatibility_opts ();
6425 #if defined (JOB_CONTROL)
6427 sv_childmax (const char *name
)
6432 tt
= get_string_value (name
);
6433 s
= (tt
&& *tt
) ? atoi (tt
) : 0;