1 /* variables.c -- Functions for hacking shell variables. */
3 /* Copyright (C) 1987-2010 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)
47 #define NEED_XTRACE_SET_DECL
51 #include "execute_cmd.h"
53 #include "mailcheck.h"
59 #include "builtins/getopt.h"
60 #include "builtins/common.h"
62 #if defined (READLINE)
63 # include "bashline.h"
64 # include <readline/readline.h>
66 # include <tilde/tilde.h>
70 # include "bashhist.h"
71 # include <readline/history.h>
74 #if defined (PROGRAMMABLE_COMPLETION)
75 # include "pcomplete.h"
78 #define TEMPENV_HASH_BUCKETS 4 /* must be power of two */
80 #define ifsname(s) ((s)[0] == 'I' && (s)[1] == 'F' && (s)[2] == 'S' && (s)[3] == '\0')
82 extern char **environ
;
84 /* Variables used here and defined in other files. */
85 extern int posixly_correct
;
86 extern int line_number
, line_number_base
;
87 extern int subshell_environment
, indirection_level
, subshell_level
;
88 extern int build_version
, patch_level
;
89 extern int expanding_redir
;
90 extern char *dist_version
, *release_status
;
91 extern char *shell_name
;
92 extern char *primary_prompt
, *secondary_prompt
;
93 extern char *current_host_name
;
94 extern sh_builtin_func_t
*this_shell_builtin
;
95 extern SHELL_VAR
*this_shell_function
;
96 extern char *the_printed_command_except_trap
;
97 extern char *this_command_name
;
98 extern char *command_execution_string
;
99 extern time_t shell_start_time
;
100 extern int assigning_in_environment
;
101 extern int executing_builtin
;
102 extern int funcnest_max
;
104 #if defined (READLINE)
105 extern int no_line_editing
;
106 extern int perform_hostname_completion
;
109 /* The list of shell variables that the user has created at the global
110 scope, or that came from the environment. */
111 VAR_CONTEXT
*global_variables
= (VAR_CONTEXT
*)NULL
;
113 /* The current list of shell variables, including function scopes */
114 VAR_CONTEXT
*shell_variables
= (VAR_CONTEXT
*)NULL
;
116 /* The list of shell functions that the user has created, or that came from
118 HASH_TABLE
*shell_functions
= (HASH_TABLE
*)NULL
;
120 #if defined (DEBUGGER)
121 /* The table of shell function definitions that the user defined or that
122 came from the environment. */
123 HASH_TABLE
*shell_function_defs
= (HASH_TABLE
*)NULL
;
126 /* The current variable context. This is really a count of how deep into
127 executing functions we are. */
128 int variable_context
= 0;
130 /* The set of shell assignments which are made only in the environment
131 for a single command. */
132 HASH_TABLE
*temporary_env
= (HASH_TABLE
*)NULL
;
134 /* Set to non-zero if an assignment error occurs while putting variables
135 into the temporary environment. */
136 int tempenv_assign_error
;
138 /* Some funky variables which are known about specially. Here is where
139 "$*", "$1", and all the cruft is kept. */
140 char *dollar_vars
[10];
141 WORD_LIST
*rest_of_args
= (WORD_LIST
*)NULL
;
143 /* The value of $$. */
144 pid_t dollar_dollar_pid
;
146 /* Non-zero means that we have to remake EXPORT_ENV. */
147 int array_needs_making
= 1;
149 /* The number of times BASH has been executed. This is set
150 by initialize_variables (). */
153 /* An array which is passed to commands as their environment. It is
154 manufactured from the union of the initial environment and the
155 shell variables that are marked for export. */
156 char **export_env
= (char **)NULL
;
157 static int export_env_index
;
158 static int export_env_size
;
160 #if defined (READLINE)
161 static int winsize_assignment
; /* currently assigning to LINES or COLUMNS */
164 /* Some forward declarations. */
165 static void create_variable_tables
__P((void));
167 static void set_machine_vars
__P((void));
168 static void set_home_var
__P((void));
169 static void set_shell_var
__P((void));
170 static char *get_bash_name
__P((void));
171 static void initialize_shell_level
__P((void));
172 static void uidset
__P((void));
173 #if defined (ARRAY_VARS)
174 static void make_vers_array
__P((void));
177 static SHELL_VAR
*null_assign
__P((SHELL_VAR
*, char *, arrayind_t
, char *));
178 #if defined (ARRAY_VARS)
179 static SHELL_VAR
*null_array_assign
__P((SHELL_VAR
*, char *, arrayind_t
, char *));
181 static SHELL_VAR
*get_self
__P((SHELL_VAR
*));
183 #if defined (ARRAY_VARS)
184 static SHELL_VAR
*init_dynamic_array_var
__P((char *, sh_var_value_func_t
*, sh_var_assign_func_t
*, int));
185 static SHELL_VAR
*init_dynamic_assoc_var
__P((char *, sh_var_value_func_t
*, sh_var_assign_func_t
*, int));
188 static SHELL_VAR
*assign_seconds
__P((SHELL_VAR
*, char *, arrayind_t
, char *));
189 static SHELL_VAR
*get_seconds
__P((SHELL_VAR
*));
190 static SHELL_VAR
*init_seconds_var
__P((void));
192 static int brand
__P((void));
193 static void sbrand
__P((unsigned long)); /* set bash random number generator. */
194 static void seedrand
__P((void)); /* seed generator randomly */
195 static SHELL_VAR
*assign_random
__P((SHELL_VAR
*, char *, arrayind_t
, char *));
196 static SHELL_VAR
*get_random
__P((SHELL_VAR
*));
198 static SHELL_VAR
*assign_lineno
__P((SHELL_VAR
*, char *, arrayind_t
, char *));
199 static SHELL_VAR
*get_lineno
__P((SHELL_VAR
*));
201 static SHELL_VAR
*assign_subshell
__P((SHELL_VAR
*, char *, arrayind_t
, char *));
202 static SHELL_VAR
*get_subshell
__P((SHELL_VAR
*));
204 static SHELL_VAR
*get_bashpid
__P((SHELL_VAR
*));
206 #if defined (HISTORY)
207 static SHELL_VAR
*get_histcmd
__P((SHELL_VAR
*));
210 #if defined (READLINE)
211 static SHELL_VAR
*get_comp_wordbreaks
__P((SHELL_VAR
*));
212 static SHELL_VAR
*assign_comp_wordbreaks
__P((SHELL_VAR
*, char *, arrayind_t
, char *));
215 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
216 static SHELL_VAR
*assign_dirstack
__P((SHELL_VAR
*, char *, arrayind_t
, char *));
217 static SHELL_VAR
*get_dirstack
__P((SHELL_VAR
*));
220 #if defined (ARRAY_VARS)
221 static SHELL_VAR
*get_groupset
__P((SHELL_VAR
*));
223 static SHELL_VAR
*build_hashcmd
__P((SHELL_VAR
*));
224 static SHELL_VAR
*get_hashcmd
__P((SHELL_VAR
*));
225 static SHELL_VAR
*assign_hashcmd
__P((SHELL_VAR
*, char *, arrayind_t
, char *));
227 static SHELL_VAR
*build_aliasvar
__P((SHELL_VAR
*));
228 static SHELL_VAR
*get_aliasvar
__P((SHELL_VAR
*));
229 static SHELL_VAR
*assign_aliasvar
__P((SHELL_VAR
*, char *, arrayind_t
, char *));
233 static SHELL_VAR
*get_funcname
__P((SHELL_VAR
*));
234 static SHELL_VAR
*init_funcname_var
__P((void));
236 static void initialize_dynamic_variables
__P((void));
238 static SHELL_VAR
*hash_lookup
__P((const char *, HASH_TABLE
*));
239 static SHELL_VAR
*new_shell_variable
__P((const char *));
240 static SHELL_VAR
*make_new_variable
__P((const char *, HASH_TABLE
*));
241 static SHELL_VAR
*bind_variable_internal
__P((const char *, char *, HASH_TABLE
*, int, int));
243 static void dispose_variable_value
__P((SHELL_VAR
*));
244 static void free_variable_hash_data
__P((PTR_T
));
246 static VARLIST
*vlist_alloc
__P((int));
247 static VARLIST
*vlist_realloc
__P((VARLIST
*, int));
248 static void vlist_add
__P((VARLIST
*, SHELL_VAR
*, int));
250 static void flatten
__P((HASH_TABLE
*, sh_var_map_func_t
*, VARLIST
*, int));
252 static int qsort_var_comp
__P((SHELL_VAR
**, SHELL_VAR
**));
254 static SHELL_VAR
**vapply
__P((sh_var_map_func_t
*));
255 static SHELL_VAR
**fapply
__P((sh_var_map_func_t
*));
257 static int visible_var
__P((SHELL_VAR
*));
258 static int visible_and_exported
__P((SHELL_VAR
*));
259 static int export_environment_candidate
__P((SHELL_VAR
*));
260 static int local_and_exported
__P((SHELL_VAR
*));
261 static int variable_in_context
__P((SHELL_VAR
*));
262 #if defined (ARRAY_VARS)
263 static int visible_array_vars
__P((SHELL_VAR
*));
266 static SHELL_VAR
*bind_tempenv_variable
__P((const char *, char *));
267 static void push_temp_var
__P((PTR_T
));
268 static void propagate_temp_var
__P((PTR_T
));
269 static void dispose_temporary_env
__P((sh_free_func_t
*));
271 static inline char *mk_env_string
__P((const char *, const char *));
272 static char **make_env_array_from_var_list
__P((SHELL_VAR
**));
273 static char **make_var_export_array
__P((VAR_CONTEXT
*));
274 static char **make_func_export_array
__P((void));
275 static void add_temp_array_to_env
__P((char **, int, int));
277 static int n_shell_variables
__P((void));
278 static int set_context
__P((SHELL_VAR
*));
280 static void push_func_var
__P((PTR_T
));
281 static void push_exported_var
__P((PTR_T
));
283 static inline int find_special_var
__P((const char *));
286 create_variable_tables ()
288 if (shell_variables
== 0)
290 shell_variables
= global_variables
= new_var_context ((char *)NULL
, 0);
291 shell_variables
->scope
= 0;
292 shell_variables
->table
= hash_create (0);
295 if (shell_functions
== 0)
296 shell_functions
= hash_create (0);
298 #if defined (DEBUGGER)
299 if (shell_function_defs
== 0)
300 shell_function_defs
= hash_create (0);
304 /* Initialize the shell variables from the current environment.
305 If PRIVMODE is nonzero, don't import functions from ENV or
308 initialize_shell_variables (env
, privmode
)
312 char *name
, *string
, *temp_string
;
313 int c
, char_index
, string_index
, string_length
;
316 create_variable_tables ();
318 for (string_index
= 0; string
= env
[string_index
++]; )
322 while ((c
= *string
++) && c
!= '=')
324 if (string
[-1] == '=')
325 char_index
= string
- name
- 1;
327 /* If there are weird things in the environment, like `=xxx' or a
328 string without an `=', just skip them. */
332 /* ASSERT(name[char_index] == '=') */
333 name
[char_index
] = '\0';
334 /* Now, name = env variable name, string = env variable value, and
335 char_index == strlen (name) */
337 temp_var
= (SHELL_VAR
*)NULL
;
339 /* If exported function, define it now. Don't import functions from
340 the environment in privileged mode. */
341 if (privmode
== 0 && read_but_dont_execute
== 0 && STREQN ("() {", string
, 4))
343 string_length
= strlen (string
);
344 temp_string
= (char *)xmalloc (3 + string_length
+ char_index
);
346 strcpy (temp_string
, name
);
347 temp_string
[char_index
] = ' ';
348 strcpy (temp_string
+ char_index
+ 1, string
);
350 parse_and_execute (temp_string
, name
, SEVAL_NONINT
|SEVAL_NOHIST
);
352 /* Ancient backwards compatibility. Old versions of bash exported
353 functions like name()=() {...} */
354 if (name
[char_index
- 1] == ')' && name
[char_index
- 2] == '(')
355 name
[char_index
- 2] = '\0';
357 if (temp_var
= find_function (name
))
359 VSETATTR (temp_var
, (att_exported
|att_imported
));
360 array_needs_making
= 1;
363 report_error (_("error importing function definition for `%s'"), name
);
366 if (name
[char_index
- 1] == ')' && name
[char_index
- 2] == '\0')
367 name
[char_index
- 2] = '('; /* ) */
369 #if defined (ARRAY_VARS)
371 /* Array variables may not yet be exported. */
372 else if (*string
== '(' && string
[1] == '[' && string
[strlen (string
) - 1] == ')')
375 temp_string
= extract_array_assignment_list (string
, &string_length
);
376 temp_var
= assign_array_from_string (name
, temp_string
);
378 VSETATTR (temp_var
, (att_exported
| att_imported
));
379 array_needs_making
= 1;
384 else if (legal_identifier (name
))
389 temp_var
= bind_variable (name
, string
, 0);
392 if (legal_identifier (name
))
393 VSETATTR (temp_var
, (att_exported
| att_imported
));
395 VSETATTR (temp_var
, (att_exported
| att_imported
| att_invisible
));
396 array_needs_making
= 1;
400 name
[char_index
] = '=';
401 /* temp_var can be NULL if it was an exported function with a syntax
402 error (a different bug, but it still shouldn't dump core). */
403 if (temp_var
&& function_p (temp_var
) == 0) /* XXX not yet */
405 CACHE_IMPORTSTR (temp_var
, name
);
411 /* Set up initial value of $_ */
412 temp_var
= set_if_not ("_", dollar_vars
[0]);
414 /* Remember this pid. */
415 dollar_dollar_pid
= getpid ();
417 /* Now make our own defaults in case the vars that we think are
418 important are missing. */
419 temp_var
= set_if_not ("PATH", DEFAULT_PATH_VALUE
);
421 set_auto_export (temp_var
); /* XXX */
424 temp_var
= set_if_not ("TERM", "dumb");
426 set_auto_export (temp_var
); /* XXX */
429 #if defined (__QNX__)
430 /* set node id -- don't import it from the environment */
433 # if defined (__QNXNTO__)
434 netmgr_ndtostr(ND2S_LOCAL_STR
, ND_LOCAL_NODE
, node_name
, sizeof(node_name
));
436 qnx_nidtostr (getnid (), node_name
, sizeof (node_name
));
438 temp_var
= bind_variable ("NODE", node_name
, 0);
439 set_auto_export (temp_var
);
443 /* set up the prompts. */
444 if (interactive_shell
)
446 #if defined (PROMPT_STRING_DECODE)
447 set_if_not ("PS1", primary_prompt
);
449 if (current_user
.uid
== -1)
450 get_current_user_info ();
451 set_if_not ("PS1", current_user
.euid
== 0 ? "# " : primary_prompt
);
453 set_if_not ("PS2", secondary_prompt
);
455 set_if_not ("PS4", "+ ");
457 /* Don't allow IFS to be imported from the environment. */
458 temp_var
= bind_variable ("IFS", " \t\n", 0);
461 /* Magic machine types. Pretty convenient. */
464 /* Default MAILCHECK for interactive shells. Defer the creation of a
465 default MAILPATH until the startup files are read, because MAIL
466 names a mail file if MAILPATH is not set, and we should provide a
467 default only if neither is set. */
468 if (interactive_shell
)
470 temp_var
= set_if_not ("MAILCHECK", posixly_correct
? "600" : "60");
471 VSETATTR (temp_var
, att_integer
);
474 /* Do some things with shell level. */
475 initialize_shell_level ();
479 /* Initialize the `getopts' stuff. */
480 temp_var
= bind_variable ("OPTIND", "1", 0);
481 VSETATTR (temp_var
, att_integer
);
483 bind_variable ("OPTERR", "1", 0);
486 if (login_shell
== 1 && posixly_correct
== 0)
489 /* Get the full pathname to THIS shell, and set the BASH variable
491 name
= get_bash_name ();
492 temp_var
= bind_variable ("BASH", name
, 0);
495 /* Make the exported environment variable SHELL be the user's login
496 shell. Note that the `tset' command looks at this variable
497 to determine what style of commands to output; if it ends in "csh",
498 then C-shell commands are output, else Bourne shell commands. */
501 /* Make a variable called BASH_VERSION which contains the version info. */
502 bind_variable ("BASH_VERSION", shell_version_string (), 0);
503 #if defined (ARRAY_VARS)
507 if (command_execution_string
)
508 bind_variable ("BASH_EXECUTION_STRING", command_execution_string
, 0);
510 /* Find out if we're supposed to be in Posix.2 mode via an
511 environment variable. */
512 temp_var
= find_variable ("POSIXLY_CORRECT");
514 temp_var
= find_variable ("POSIX_PEDANTIC");
515 if (temp_var
&& imported_p (temp_var
))
516 sv_strict_posix (temp_var
->name
);
518 #if defined (HISTORY)
519 /* Set history variables to defaults, and then do whatever we would
520 do if the variable had just been set. Do this only in the case
521 that we are remembering commands on the history list. */
522 if (remember_on_history
)
524 name
= bash_tilde_expand (posixly_correct
? "~/.sh_history" : "~/.bash_history", 0);
526 set_if_not ("HISTFILE", name
);
530 set_if_not ("HISTSIZE", "500");
531 sv_histsize ("HISTSIZE");
536 /* Seed the random number generator. */
539 /* Handle some "special" variables that we may have inherited from a
541 if (interactive_shell
)
543 temp_var
= find_variable ("IGNOREEOF");
545 temp_var
= find_variable ("ignoreeof");
546 if (temp_var
&& imported_p (temp_var
))
547 sv_ignoreeof (temp_var
->name
);
550 #if defined (HISTORY)
551 if (interactive_shell
&& remember_on_history
)
553 sv_history_control ("HISTCONTROL");
554 sv_histignore ("HISTIGNORE");
555 sv_histtimefmt ("HISTTIMEFORMAT");
559 #if defined (READLINE) && defined (STRICT_POSIX)
560 /* POSIXLY_CORRECT will only be 1 here if the shell was compiled
562 if (interactive_shell
&& posixly_correct
&& no_line_editing
== 0)
563 rl_prefer_env_winsize
= 1;
564 #endif /* READLINE && STRICT_POSIX */
569 * I'm tired of the arguing and bug reports. Bash now leaves SSH_CLIENT
570 * and SSH2_CLIENT alone. I'm going to rely on the shell_level check in
571 * isnetconn() to avoid running the startup files more often than wanted.
572 * That will, of course, only work if the user's login shell is bash, so
573 * I've made that behavior conditional on SSH_SOURCE_BASHRC being defined
577 temp_var
= find_variable ("SSH_CLIENT");
578 if (temp_var
&& imported_p (temp_var
))
580 VUNSETATTR (temp_var
, att_exported
);
581 array_needs_making
= 1;
583 temp_var
= find_variable ("SSH2_CLIENT");
584 if (temp_var
&& imported_p (temp_var
))
586 VUNSETATTR (temp_var
, att_exported
);
587 array_needs_making
= 1;
591 /* Get the user's real and effective user ids. */
594 temp_var
= find_variable ("BASH_XTRACEFD");
595 if (temp_var
&& imported_p (temp_var
))
596 sv_xtracefd (temp_var
->name
);
598 /* Initialize the dynamic variables, and seed their values. */
599 initialize_dynamic_variables ();
602 /* **************************************************************** */
604 /* Setting values for special shell variables */
606 /* **************************************************************** */
613 temp_var
= set_if_not ("HOSTTYPE", HOSTTYPE
);
614 temp_var
= set_if_not ("OSTYPE", OSTYPE
);
615 temp_var
= set_if_not ("MACHTYPE", MACHTYPE
);
617 temp_var
= set_if_not ("HOSTNAME", current_host_name
);
620 /* Set $HOME to the information in the password file if we didn't get
621 it from the environment. */
623 /* This function is not static so the tilde and readline libraries can
628 if (current_user
.home_dir
== 0)
629 get_current_user_info ();
630 return current_user
.home_dir
;
638 temp_var
= find_variable ("HOME");
640 temp_var
= bind_variable ("HOME", sh_get_home_dir (), 0);
642 VSETATTR (temp_var
, att_exported
);
646 /* Set $SHELL to the user's login shell if it is not already set. Call
647 get_current_user_info if we haven't already fetched the shell. */
653 temp_var
= find_variable ("SHELL");
656 if (current_user
.shell
== 0)
657 get_current_user_info ();
658 temp_var
= bind_variable ("SHELL", current_user
.shell
, 0);
661 VSETATTR (temp_var
, att_exported
);
670 if ((login_shell
== 1) && RELPATH(shell_name
))
672 if (current_user
.shell
== 0)
673 get_current_user_info ();
674 name
= savestring (current_user
.shell
);
676 else if (ABSPATH(shell_name
))
677 name
= savestring (shell_name
);
678 else if (shell_name
[0] == '.' && shell_name
[1] == '/')
680 /* Fast path for common case. */
684 cdir
= get_string_value ("PWD");
688 name
= (char *)xmalloc (len
+ strlen (shell_name
) + 1);
690 strcpy (name
+ len
, shell_name
+ 1);
693 name
= savestring (shell_name
);
700 tname
= find_user_command (shell_name
);
704 /* Try the current directory. If there is not an executable
705 there, just punt and use the login shell. */
706 s
= file_status (shell_name
);
709 tname
= make_absolute (shell_name
, get_string_value ("PWD"));
710 if (*shell_name
== '.')
712 name
= sh_canonpath (tname
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
723 if (current_user
.shell
== 0)
724 get_current_user_info ();
725 name
= savestring (current_user
.shell
);
730 name
= full_pathname (tname
);
739 adjust_shell_level (change
)
742 char new_level
[5], *old_SHLVL
;
746 old_SHLVL
= get_string_value ("SHLVL");
747 if (old_SHLVL
== 0 || *old_SHLVL
== '\0' || legal_number (old_SHLVL
, &old_level
) == 0)
750 shell_level
= old_level
+ change
;
753 else if (shell_level
> 1000)
755 internal_warning (_("shell level (%d) too high, resetting to 1"), shell_level
);
759 /* We don't need the full generality of itos here. */
760 if (shell_level
< 10)
762 new_level
[0] = shell_level
+ '0';
765 else if (shell_level
< 100)
767 new_level
[0] = (shell_level
/ 10) + '0';
768 new_level
[1] = (shell_level
% 10) + '0';
771 else if (shell_level
< 1000)
773 new_level
[0] = (shell_level
/ 100) + '0';
774 old_level
= shell_level
% 100;
775 new_level
[1] = (old_level
/ 10) + '0';
776 new_level
[2] = (old_level
% 10) + '0';
780 temp_var
= bind_variable ("SHLVL", new_level
, 0);
781 set_auto_export (temp_var
);
785 initialize_shell_level ()
787 adjust_shell_level (1);
790 /* If we got PWD from the environment, update our idea of the current
791 working directory. In any case, make sure that PWD exists before
792 checking it. It is possible for getcwd () to fail on shell startup,
793 and in that case, PWD would be undefined. If this is an interactive
794 login shell, see if $HOME is the current working directory, and if
795 that's not the same string as $PWD, set PWD=$HOME. */
800 SHELL_VAR
*temp_var
, *home_var
;
801 char *temp_string
, *home_string
;
803 home_var
= find_variable ("HOME");
804 home_string
= home_var
? value_cell (home_var
) : (char *)NULL
;
806 temp_var
= find_variable ("PWD");
807 if (temp_var
&& imported_p (temp_var
) &&
808 (temp_string
= value_cell (temp_var
)) &&
809 same_file (temp_string
, ".", (struct stat
*)NULL
, (struct stat
*)NULL
))
810 set_working_directory (temp_string
);
811 else if (home_string
&& interactive_shell
&& login_shell
&&
812 same_file (home_string
, ".", (struct stat
*)NULL
, (struct stat
*)NULL
))
814 set_working_directory (home_string
);
815 temp_var
= bind_variable ("PWD", home_string
, 0);
816 set_auto_export (temp_var
);
820 temp_string
= get_working_directory ("shell-init");
823 temp_var
= bind_variable ("PWD", temp_string
, 0);
824 set_auto_export (temp_var
);
829 /* According to the Single Unix Specification, v2, $OLDPWD is an
830 `environment variable' and therefore should be auto-exported.
831 Make a dummy invisible variable for OLDPWD, and mark it as exported. */
832 temp_var
= bind_variable ("OLDPWD", (char *)NULL
, 0);
833 VSETATTR (temp_var
, (att_exported
| att_invisible
));
836 /* Make a variable $PPID, which holds the pid of the shell's parent. */
840 char namebuf
[INT_STRLEN_BOUND(pid_t
) + 1], *name
;
843 name
= inttostr (getppid (), namebuf
, sizeof(namebuf
));
844 temp_var
= find_variable ("PPID");
846 VUNSETATTR (temp_var
, (att_readonly
| att_exported
));
847 temp_var
= bind_variable ("PPID", name
, 0);
848 VSETATTR (temp_var
, (att_readonly
| att_integer
));
854 char buff
[INT_STRLEN_BOUND(uid_t
) + 1], *b
;
855 register SHELL_VAR
*v
;
857 b
= inttostr (current_user
.uid
, buff
, sizeof (buff
));
858 v
= find_variable ("UID");
861 v
= bind_variable ("UID", b
, 0);
862 VSETATTR (v
, (att_readonly
| att_integer
));
865 if (current_user
.euid
!= current_user
.uid
)
866 b
= inttostr (current_user
.euid
, buff
, sizeof (buff
));
868 v
= find_variable ("EUID");
871 v
= bind_variable ("EUID", b
, 0);
872 VSETATTR (v
, (att_readonly
| att_integer
));
876 #if defined (ARRAY_VARS)
882 char *s
, d
[32], b
[INT_STRLEN_BOUND(int) + 1];
884 unbind_variable ("BASH_VERSINFO");
886 vv
= make_new_array_variable ("BASH_VERSINFO");
887 av
= array_cell (vv
);
888 strcpy (d
, dist_version
);
892 array_insert (av
, 0, d
);
893 array_insert (av
, 1, s
);
894 s
= inttostr (patch_level
, b
, sizeof (b
));
895 array_insert (av
, 2, s
);
896 s
= inttostr (build_version
, b
, sizeof (b
));
897 array_insert (av
, 3, s
);
898 array_insert (av
, 4, release_status
);
899 array_insert (av
, 5, MACHTYPE
);
901 VSETATTR (vv
, att_readonly
);
903 #endif /* ARRAY_VARS */
905 /* Set the environment variables $LINES and $COLUMNS in response to
906 a window size change. */
908 sh_set_lines_and_columns (lines
, cols
)
911 char val
[INT_STRLEN_BOUND(int) + 1], *v
;
913 #if defined (READLINE)
914 /* If we are currently assigning to LINES or COLUMNS, don't do anything. */
915 if (winsize_assignment
)
919 v
= inttostr (lines
, val
, sizeof (val
));
920 bind_variable ("LINES", v
, 0);
922 v
= inttostr (cols
, val
, sizeof (val
));
923 bind_variable ("COLUMNS", v
, 0);
926 /* **************************************************************** */
928 /* Printing variables and values */
930 /* **************************************************************** */
932 /* Print LIST (a list of shell variables) to stdout in such a way that
933 they can be read back in. */
935 print_var_list (list
)
936 register SHELL_VAR
**list
;
939 register SHELL_VAR
*var
;
941 for (i
= 0; list
&& (var
= list
[i
]); i
++)
942 if (invisible_p (var
) == 0)
943 print_assignment (var
);
946 /* Print LIST (a list of shell functions) to stdout in such a way that
947 they can be read back in. */
949 print_func_list (list
)
950 register SHELL_VAR
**list
;
953 register SHELL_VAR
*var
;
955 for (i
= 0; list
&& (var
= list
[i
]); i
++)
957 printf ("%s ", var
->name
);
958 print_var_function (var
);
963 /* Print the value of a single SHELL_VAR. No newline is
964 output, but the variable is printed in such a way that
965 it can be read back in. */
967 print_assignment (var
)
970 if (var_isset (var
) == 0)
973 if (function_p (var
))
975 printf ("%s", var
->name
);
976 print_var_function (var
);
979 #if defined (ARRAY_VARS)
980 else if (array_p (var
))
981 print_array_assignment (var
, 0);
982 else if (assoc_p (var
))
983 print_assoc_assignment (var
, 0);
984 #endif /* ARRAY_VARS */
987 printf ("%s=", var
->name
);
988 print_var_value (var
, 1);
993 /* Print the value cell of VAR, a shell variable. Do not print
994 the name, nor leading/trailing newline. If QUOTE is non-zero,
995 and the value contains shell metacharacters, quote the value
996 in such a way that it can be read back in. */
998 print_var_value (var
, quote
)
1004 if (var_isset (var
) == 0)
1007 if (quote
&& posixly_correct
== 0 && ansic_shouldquote (value_cell (var
)))
1009 t
= ansic_quote (value_cell (var
), 0, (int *)0);
1013 else if (quote
&& sh_contains_shell_metas (value_cell (var
)))
1015 t
= sh_single_quote (value_cell (var
));
1020 printf ("%s", value_cell (var
));
1023 /* Print the function cell of VAR, a shell variable. Do not
1024 print the name, nor leading/trailing newline. */
1026 print_var_function (var
)
1031 if (function_p (var
) && var_isset (var
))
1033 x
= named_function_string ((char *)NULL
, function_cell(var
), FUNC_MULTILINE
|FUNC_EXTERNAL
);
1038 /* **************************************************************** */
1040 /* Dynamic Variables */
1042 /* **************************************************************** */
1044 /* DYNAMIC VARIABLES
1046 These are variables whose values are generated anew each time they are
1047 referenced. These are implemented using a pair of function pointers
1048 in the struct variable: assign_func, which is called from bind_variable
1049 and, if arrays are compiled into the shell, some of the functions in
1050 arrayfunc.c, and dynamic_value, which is called from find_variable.
1052 assign_func is called from bind_variable_internal, if
1053 bind_variable_internal discovers that the variable being assigned to
1054 has such a function. The function is called as
1055 SHELL_VAR *temp = (*(entry->assign_func)) (entry, value, ind)
1056 and the (SHELL_VAR *)temp is returned as the value of bind_variable. It
1057 is usually ENTRY (self). IND is an index for an array variable, and
1060 dynamic_value is called from find_variable_internal to return a `new'
1061 value for the specified dynamic varible. If this function is NULL,
1062 the variable is treated as a `normal' shell variable. If it is not,
1063 however, then this function is called like this:
1064 tempvar = (*(var->dynamic_value)) (var);
1066 Sometimes `tempvar' will replace the value of `var'. Other times, the
1067 shell will simply use the string value. Pretty object-oriented, huh?
1069 Be warned, though: if you `unset' a special variable, it loses its
1070 special meaning, even if you subsequently set it.
1072 The special assignment code would probably have been better put in
1073 subst.c: do_assignment_internal, in the same style as
1074 stupidly_hack_special_variables, but I wanted the changes as
1075 localized as possible. */
1077 #define INIT_DYNAMIC_VAR(var, val, gfunc, afunc) \
1080 v = bind_variable (var, (val), 0); \
1081 v->dynamic_value = gfunc; \
1082 v->assign_func = afunc; \
1086 #define INIT_DYNAMIC_ARRAY_VAR(var, gfunc, afunc) \
1089 v = make_new_array_variable (var); \
1090 v->dynamic_value = gfunc; \
1091 v->assign_func = afunc; \
1095 #define INIT_DYNAMIC_ASSOC_VAR(var, gfunc, afunc) \
1098 v = make_new_assoc_variable (var); \
1099 v->dynamic_value = gfunc; \
1100 v->assign_func = afunc; \
1105 null_assign (self
, value
, unused
, key
)
1114 #if defined (ARRAY_VARS)
1116 null_array_assign (self
, value
, ind
, key
)
1126 /* Degenerate `dynamic_value' function; just returns what's passed without
1135 #if defined (ARRAY_VARS)
1136 /* A generic dynamic array variable initializer. Intialize array variable
1137 NAME with dynamic value function GETFUNC and assignment function SETFUNC. */
1139 init_dynamic_array_var (name
, getfunc
, setfunc
, attrs
)
1141 sh_var_value_func_t
*getfunc
;
1142 sh_var_assign_func_t
*setfunc
;
1147 v
= find_variable (name
);
1150 INIT_DYNAMIC_ARRAY_VAR (name
, getfunc
, setfunc
);
1152 VSETATTR (v
, attrs
);
1157 init_dynamic_assoc_var (name
, getfunc
, setfunc
, attrs
)
1159 sh_var_value_func_t
*getfunc
;
1160 sh_var_assign_func_t
*setfunc
;
1165 v
= find_variable (name
);
1168 INIT_DYNAMIC_ASSOC_VAR (name
, getfunc
, setfunc
);
1170 VSETATTR (v
, attrs
);
1175 /* The value of $SECONDS. This is the number of seconds since shell
1176 invocation, or, the number of seconds since the last assignment + the
1177 value of the last assignment. */
1178 static intmax_t seconds_value_assigned
;
1181 assign_seconds (self
, value
, unused
, key
)
1187 if (legal_number (value
, &seconds_value_assigned
) == 0)
1188 seconds_value_assigned
= 0;
1189 shell_start_time
= NOW
;
1197 time_t time_since_start
;
1200 time_since_start
= NOW
- shell_start_time
;
1201 p
= itos(seconds_value_assigned
+ time_since_start
);
1203 FREE (value_cell (var
));
1205 VSETATTR (var
, att_integer
);
1206 var_setvalue (var
, p
);
1215 v
= find_variable ("SECONDS");
1218 if (legal_number (value_cell(v
), &seconds_value_assigned
) == 0)
1219 seconds_value_assigned
= 0;
1221 INIT_DYNAMIC_VAR ("SECONDS", (v
? value_cell (v
) : (char *)NULL
), get_seconds
, assign_seconds
);
1225 /* The random number seed. You can change this by setting RANDOM. */
1226 static unsigned long rseed
= 1;
1227 static int last_random_value
;
1228 static int seeded_subshell
= 0;
1230 /* A linear congruential random number generator based on the example
1231 one in the ANSI C standard. This one isn't very good, but a more
1232 complicated one is overkill. */
1234 /* Returns a pseudo-random number between 0 and 32767. */
1238 /* From "Random number generators: good ones are hard to find",
1239 Park and Miller, Communications of the ACM, vol. 31, no. 10,
1240 October 1988, p. 1195. filtered through FreeBSD */
1243 /* Can't seed with 0. */
1248 rseed
= 16807 * l
- 2836 * h
;
1251 rseed
+= 0x7fffffff;
1253 return ((unsigned int)(rseed
& 32767)); /* was % 32768 */
1256 /* Set the random number generator seed to SEED. */
1262 last_random_value
= 0;
1270 gettimeofday (&tv
, NULL
);
1271 sbrand (tv
.tv_sec
^ tv
.tv_usec
^ getpid ());
1275 assign_random (self
, value
, unused
, key
)
1281 sbrand (strtoul (value
, (char **)NULL
, 10));
1282 if (subshell_environment
)
1283 seeded_subshell
= getpid ();
1288 get_random_number ()
1292 /* Reset for command and process substitution. */
1294 if (subshell_environment
&& seeded_subshell
!= pid
)
1297 seeded_subshell
= pid
;
1302 while (rv
== last_random_value
);
1313 rv
= get_random_number ();
1314 last_random_value
= rv
;
1317 FREE (value_cell (var
));
1319 VSETATTR (var
, att_integer
);
1320 var_setvalue (var
, p
);
1325 assign_lineno (var
, value
, unused
, key
)
1333 if (value
== 0 || *value
== '\0' || legal_number (value
, &new_value
) == 0)
1335 line_number
= line_number_base
= new_value
;
1339 /* Function which returns the current line number. */
1347 ln
= executing_line_number ();
1349 FREE (value_cell (var
));
1350 var_setvalue (var
, p
);
1355 assign_subshell (var
, value
, unused
, key
)
1363 if (value
== 0 || *value
== '\0' || legal_number (value
, &new_value
) == 0)
1365 subshell_level
= new_value
;
1375 p
= itos (subshell_level
);
1376 FREE (value_cell (var
));
1377 var_setvalue (var
, p
);
1391 FREE (value_cell (var
));
1392 VSETATTR (var
, att_integer
|att_readonly
);
1393 var_setvalue (var
, p
);
1398 get_bash_command (var
)
1403 if (the_printed_command_except_trap
)
1404 p
= savestring (the_printed_command_except_trap
);
1407 p
= (char *)xmalloc (1);
1410 FREE (value_cell (var
));
1411 var_setvalue (var
, p
);
1415 #if defined (HISTORY)
1422 p
= itos (history_number ());
1423 FREE (value_cell (var
));
1424 var_setvalue (var
, p
);
1429 #if defined (READLINE)
1430 /* When this function returns, VAR->value points to malloced memory. */
1432 get_comp_wordbreaks (var
)
1435 /* If we don't have anything yet, assign a default value. */
1436 if (rl_completer_word_break_characters
== 0 && bash_readline_initialized
== 0)
1437 enable_hostname_completion (perform_hostname_completion
);
1439 FREE (value_cell (var
));
1440 var_setvalue (var
, savestring (rl_completer_word_break_characters
));
1445 /* When this function returns, rl_completer_word_break_characters points to
1448 assign_comp_wordbreaks (self
, value
, unused
, key
)
1454 if (rl_completer_word_break_characters
&&
1455 rl_completer_word_break_characters
!= rl_basic_word_break_characters
)
1456 free (rl_completer_word_break_characters
);
1458 rl_completer_word_break_characters
= savestring (value
);
1461 #endif /* READLINE */
1463 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
1465 assign_dirstack (self
, value
, ind
, key
)
1471 set_dirstack_element (ind
, 1, value
);
1482 l
= get_directory_stack (0);
1483 a
= array_from_word_list (l
);
1484 array_dispose (array_cell (self
));
1486 var_setarray (self
, a
);
1489 #endif /* PUSHD AND POPD && ARRAY_VARS */
1491 #if defined (ARRAY_VARS)
1492 /* We don't want to initialize the group set with a call to getgroups()
1493 unless we're asked to, but we only want to do it once. */
1501 static char **group_set
= (char **)NULL
;
1505 group_set
= get_group_list (&ng
);
1506 a
= array_cell (self
);
1507 for (i
= 0; i
< ng
; i
++)
1508 array_insert (a
, i
, group_set
[i
]);
1514 build_hashcmd (self
)
1520 BUCKET_CONTENTS
*item
;
1522 h
= assoc_cell (self
);
1526 if (hashed_filenames
== 0 || HASH_ENTRIES (hashed_filenames
) == 0)
1528 var_setvalue (self
, (char *)NULL
);
1532 h
= assoc_create (hashed_filenames
->nbuckets
);
1533 for (i
= 0; i
< hashed_filenames
->nbuckets
; i
++)
1535 for (item
= hash_items (i
, hashed_filenames
); item
; item
= item
->next
)
1537 k
= savestring (item
->key
);
1538 v
= pathdata(item
)->path
;
1539 assoc_insert (h
, k
, v
);
1543 var_setvalue (self
, (char *)h
);
1551 build_hashcmd (self
);
1556 assign_hashcmd (self
, value
, ind
, key
)
1562 phash_insert (key
, value
, 0, 0);
1563 return (build_hashcmd (self
));
1568 build_aliasvar (self
)
1574 BUCKET_CONTENTS
*item
;
1576 h
= assoc_cell (self
);
1580 if (aliases
== 0 || HASH_ENTRIES (aliases
) == 0)
1582 var_setvalue (self
, (char *)NULL
);
1586 h
= assoc_create (aliases
->nbuckets
);
1587 for (i
= 0; i
< aliases
->nbuckets
; i
++)
1589 for (item
= hash_items (i
, aliases
); item
; item
= item
->next
)
1591 k
= savestring (item
->key
);
1592 v
= ((alias_t
*)(item
->data
))->value
;
1593 assoc_insert (h
, k
, v
);
1597 var_setvalue (self
, (char *)h
);
1605 build_aliasvar (self
);
1610 assign_aliasvar (self
, value
, ind
, key
)
1616 add_alias (key
, value
);
1617 return (build_aliasvar (self
));
1621 #endif /* ARRAY_VARS */
1623 /* If ARRAY_VARS is not defined, this just returns the name of any
1624 currently-executing function. If we have arrays, it's a call stack. */
1629 #if ! defined (ARRAY_VARS)
1631 if (variable_context
&& this_shell_function
)
1633 FREE (value_cell (self
));
1634 t
= savestring (this_shell_function
->name
);
1635 var_setvalue (self
, t
);
1642 make_funcname_visible (on_or_off
)
1647 v
= find_variable ("FUNCNAME");
1648 if (v
== 0 || v
->dynamic_value
== 0)
1652 VUNSETATTR (v
, att_invisible
);
1654 VSETATTR (v
, att_invisible
);
1658 init_funcname_var ()
1662 v
= find_variable ("FUNCNAME");
1665 #if defined (ARRAY_VARS)
1666 INIT_DYNAMIC_ARRAY_VAR ("FUNCNAME", get_funcname
, null_array_assign
);
1668 INIT_DYNAMIC_VAR ("FUNCNAME", (char *)NULL
, get_funcname
, null_assign
);
1670 VSETATTR (v
, att_invisible
|att_noassign
);
1675 initialize_dynamic_variables ()
1679 v
= init_seconds_var ();
1681 INIT_DYNAMIC_VAR ("BASH_COMMAND", (char *)NULL
, get_bash_command
, (sh_var_assign_func_t
*)NULL
);
1682 INIT_DYNAMIC_VAR ("BASH_SUBSHELL", (char *)NULL
, get_subshell
, assign_subshell
);
1684 INIT_DYNAMIC_VAR ("RANDOM", (char *)NULL
, get_random
, assign_random
);
1685 VSETATTR (v
, att_integer
);
1686 INIT_DYNAMIC_VAR ("LINENO", (char *)NULL
, get_lineno
, assign_lineno
);
1687 VSETATTR (v
, att_integer
);
1689 INIT_DYNAMIC_VAR ("BASHPID", (char *)NULL
, get_bashpid
, null_assign
);
1690 VSETATTR (v
, att_integer
|att_readonly
);
1692 #if defined (HISTORY)
1693 INIT_DYNAMIC_VAR ("HISTCMD", (char *)NULL
, get_histcmd
, (sh_var_assign_func_t
*)NULL
);
1694 VSETATTR (v
, att_integer
);
1697 #if defined (READLINE)
1698 INIT_DYNAMIC_VAR ("COMP_WORDBREAKS", (char *)NULL
, get_comp_wordbreaks
, assign_comp_wordbreaks
);
1701 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
1702 v
= init_dynamic_array_var ("DIRSTACK", get_dirstack
, assign_dirstack
, 0);
1703 #endif /* PUSHD_AND_POPD && ARRAY_VARS */
1705 #if defined (ARRAY_VARS)
1706 v
= init_dynamic_array_var ("GROUPS", get_groupset
, null_array_assign
, att_noassign
);
1708 # if defined (DEBUGGER)
1709 v
= init_dynamic_array_var ("BASH_ARGC", get_self
, null_array_assign
, att_noassign
|att_nounset
);
1710 v
= init_dynamic_array_var ("BASH_ARGV", get_self
, null_array_assign
, att_noassign
|att_nounset
);
1711 # endif /* DEBUGGER */
1712 v
= init_dynamic_array_var ("BASH_SOURCE", get_self
, null_array_assign
, att_noassign
|att_nounset
);
1713 v
= init_dynamic_array_var ("BASH_LINENO", get_self
, null_array_assign
, att_noassign
|att_nounset
);
1715 v
= init_dynamic_assoc_var ("BASH_CMDS", get_hashcmd
, assign_hashcmd
, att_nofree
);
1716 # if defined (ALIAS)
1717 v
= init_dynamic_assoc_var ("BASH_ALIASES", get_aliasvar
, assign_aliasvar
, att_nofree
);
1721 v
= init_funcname_var ();
1724 /* **************************************************************** */
1726 /* Retrieving variables and values */
1728 /* **************************************************************** */
1730 /* How to get a pointer to the shell variable or function named NAME.
1731 HASHED_VARS is a pointer to the hash table containing the list
1732 of interest (either variables or functions). */
1735 hash_lookup (name
, hashed_vars
)
1737 HASH_TABLE
*hashed_vars
;
1739 BUCKET_CONTENTS
*bucket
;
1741 bucket
= hash_search (name
, hashed_vars
, 0);
1742 return (bucket
? (SHELL_VAR
*)bucket
->data
: (SHELL_VAR
*)NULL
);
1746 var_lookup (name
, vcontext
)
1748 VAR_CONTEXT
*vcontext
;
1753 v
= (SHELL_VAR
*)NULL
;
1754 for (vc
= vcontext
; vc
; vc
= vc
->down
)
1755 if (v
= hash_lookup (name
, vc
->table
))
1761 /* Look up the variable entry named NAME. If SEARCH_TEMPENV is non-zero,
1762 then also search the temporarily built list of exported variables.
1763 The lookup order is:
1765 shell_variables list
1769 find_variable_internal (name
, force_tempenv
)
1776 var
= (SHELL_VAR
*)NULL
;
1778 /* If explicitly requested, first look in the temporary environment for
1779 the variable. This allows constructs such as "foo=x eval 'echo $foo'"
1780 to get the `exported' value of $foo. This happens if we are executing
1781 a function or builtin, or if we are looking up a variable in a
1782 "subshell environment". */
1783 search_tempenv
= force_tempenv
|| (expanding_redir
== 0 && subshell_environment
);
1785 if (search_tempenv
&& temporary_env
)
1786 var
= hash_lookup (name
, temporary_env
);
1789 var
= var_lookup (name
, shell_variables
);
1792 return ((SHELL_VAR
*)NULL
);
1794 return (var
->dynamic_value
? (*(var
->dynamic_value
)) (var
) : var
);
1798 find_global_variable (name
)
1803 var
= var_lookup (name
, global_variables
);
1806 return ((SHELL_VAR
*)NULL
);
1808 return (var
->dynamic_value
? (*(var
->dynamic_value
)) (var
) : var
);
1811 /* Look up the variable entry named NAME. Returns the entry or NULL. */
1813 find_variable (name
)
1816 return (find_variable_internal (name
, (expanding_redir
== 0 && (assigning_in_environment
|| executing_builtin
))));
1819 /* Look up the function entry whose name matches STRING.
1820 Returns the entry or NULL. */
1822 find_function (name
)
1825 return (hash_lookup (name
, shell_functions
));
1828 /* Find the function definition for the shell function named NAME. Returns
1829 the entry or NULL. */
1831 find_function_def (name
)
1834 #if defined (DEBUGGER)
1835 return ((FUNCTION_DEF
*)hash_lookup (name
, shell_function_defs
));
1837 return ((FUNCTION_DEF
*)0);
1841 /* Return the value of VAR. VAR is assumed to have been the result of a
1842 lookup without any subscript, if arrays are compiled into the shell. */
1844 get_variable_value (var
)
1848 return ((char *)NULL
);
1849 #if defined (ARRAY_VARS)
1850 else if (array_p (var
))
1851 return (array_reference (array_cell (var
), 0));
1852 else if (assoc_p (var
))
1853 return (assoc_reference (assoc_cell (var
), "0"));
1856 return (value_cell (var
));
1859 /* Return the string value of a variable. Return NULL if the variable
1860 doesn't exist. Don't cons a new string. This is a potential memory
1861 leak if the variable is found in the temporary environment. Since
1862 functions and variables have separate name spaces, returns NULL if
1863 var_name is a shell function only. */
1865 get_string_value (var_name
)
1866 const char *var_name
;
1870 var
= find_variable (var_name
);
1871 return ((var
) ? get_variable_value (var
) : (char *)NULL
);
1874 /* This is present for use by the tilde and readline libraries. */
1876 sh_get_env_value (v
)
1879 return get_string_value (v
);
1882 /* **************************************************************** */
1884 /* Creating and setting variables */
1886 /* **************************************************************** */
1888 /* Set NAME to VALUE if NAME has no value. */
1890 set_if_not (name
, value
)
1895 if (shell_variables
== 0)
1896 create_variable_tables ();
1898 v
= find_variable (name
);
1900 v
= bind_variable_internal (name
, value
, global_variables
->table
, HASH_NOSRCH
, 0);
1904 /* Create a local variable referenced by NAME. */
1906 make_local_variable (name
)
1909 SHELL_VAR
*new_var
, *old_var
;
1914 /* local foo; local foo; is a no-op. */
1915 old_var
= find_variable (name
);
1916 if (old_var
&& local_p (old_var
) && old_var
->context
== variable_context
)
1918 VUNSETATTR (old_var
, att_invisible
);
1922 was_tmpvar
= old_var
&& tempvar_p (old_var
);
1924 tmp_value
= value_cell (old_var
);
1926 for (vc
= shell_variables
; vc
; vc
= vc
->down
)
1927 if (vc_isfuncenv (vc
) && vc
->scope
== variable_context
)
1932 internal_error (_("make_local_variable: no function context at current scope"));
1933 return ((SHELL_VAR
*)NULL
);
1935 else if (vc
->table
== 0)
1936 vc
->table
= hash_create (TEMPENV_HASH_BUCKETS
);
1938 /* Since this is called only from the local/declare/typeset code, we can
1939 call builtin_error here without worry (of course, it will also work
1940 for anything that sets this_command_name). Variables with the `noassign'
1941 attribute may not be made local. The test against old_var's context
1942 level is to disallow local copies of readonly global variables (since I
1943 believe that this could be a security hole). Readonly copies of calling
1944 function local variables are OK. */
1945 if (old_var
&& (noassign_p (old_var
) ||
1946 (readonly_p (old_var
) && old_var
->context
== 0)))
1948 if (readonly_p (old_var
))
1950 return ((SHELL_VAR
*)NULL
);
1954 new_var
= make_new_variable (name
, vc
->table
);
1957 new_var
= make_new_variable (name
, vc
->table
);
1959 /* If we found this variable in one of the temporary environments,
1960 inherit its value. Watch to see if this causes problems with
1961 things like `x=4 local x'. */
1963 var_setvalue (new_var
, savestring (tmp_value
));
1965 new_var
->attributes
= exported_p (old_var
) ? att_exported
: 0;
1968 vc
->flags
|= VC_HASLOCAL
;
1970 new_var
->context
= variable_context
;
1971 VSETATTR (new_var
, att_local
);
1979 /* Create a new shell variable with name NAME. */
1981 new_shell_variable (name
)
1986 entry
= (SHELL_VAR
*)xmalloc (sizeof (SHELL_VAR
));
1988 entry
->name
= savestring (name
);
1989 var_setvalue (entry
, (char *)NULL
);
1990 CLEAR_EXPORTSTR (entry
);
1992 entry
->dynamic_value
= (sh_var_value_func_t
*)NULL
;
1993 entry
->assign_func
= (sh_var_assign_func_t
*)NULL
;
1995 entry
->attributes
= 0;
1997 /* Always assume variables are to be made at toplevel!
1998 make_local_variable has the responsibilty of changing the
1999 variable context. */
2005 /* Create a new shell variable with name NAME and add it to the hash table
2008 make_new_variable (name
, table
)
2013 BUCKET_CONTENTS
*elt
;
2015 entry
= new_shell_variable (name
);
2017 /* Make sure we have a shell_variables hash table to add to. */
2018 if (shell_variables
== 0)
2019 create_variable_tables ();
2021 elt
= hash_insert (savestring (name
), table
, HASH_NOSRCH
);
2022 elt
->data
= (PTR_T
)entry
;
2027 #if defined (ARRAY_VARS)
2029 make_new_array_variable (name
)
2035 entry
= make_new_variable (name
, global_variables
->table
);
2036 array
= array_create ();
2038 var_setarray (entry
, array
);
2039 VSETATTR (entry
, att_array
);
2044 make_local_array_variable (name
)
2050 var
= make_local_variable (name
);
2051 if (var
== 0 || array_p (var
))
2054 array
= array_create ();
2056 dispose_variable_value (var
);
2057 var_setarray (var
, array
);
2058 VSETATTR (var
, att_array
);
2063 make_new_assoc_variable (name
)
2069 entry
= make_new_variable (name
, global_variables
->table
);
2070 hash
= assoc_create (0);
2072 var_setassoc (entry
, hash
);
2073 VSETATTR (entry
, att_assoc
);
2078 make_local_assoc_variable (name
)
2084 var
= make_local_variable (name
);
2085 if (var
== 0 || assoc_p (var
))
2088 dispose_variable_value (var
);
2089 hash
= assoc_create (0);
2091 var_setassoc (var
, hash
);
2092 VSETATTR (var
, att_assoc
);
2098 make_variable_value (var
, value
, flags
)
2103 char *retval
, *oval
;
2104 intmax_t lval
, rval
;
2105 int expok
, olen
, op
;
2107 /* If this variable has had its type set to integer (via `declare -i'),
2108 then do expression evaluation on it and store the result. The
2109 functions in expr.c (evalexp()) and bind_int_variable() are responsible
2110 for turning off the integer flag if they don't want further
2112 if (integer_p (var
))
2114 if (flags
& ASS_APPEND
)
2116 oval
= value_cell (var
);
2117 lval
= evalexp (oval
, &expok
); /* ksh93 seems to do this */
2120 top_level_cleanup ();
2121 jump_to_top_level (DISCARD
);
2124 rval
= evalexp (value
, &expok
);
2127 top_level_cleanup ();
2128 jump_to_top_level (DISCARD
);
2130 if (flags
& ASS_APPEND
)
2132 retval
= itos (rval
);
2134 #if defined (CASEMOD_ATTRS)
2135 else if (capcase_p (var
) || uppercase_p (var
) || lowercase_p (var
))
2137 if (flags
& ASS_APPEND
)
2139 oval
= get_variable_value (var
);
2140 if (oval
== 0) /* paranoia */
2142 olen
= STRLEN (oval
);
2143 retval
= (char *)xmalloc (olen
+ (value
? STRLEN (value
) : 0) + 1);
2144 strcpy (retval
, oval
);
2146 strcpy (retval
+olen
, value
);
2149 retval
= savestring (value
);
2152 retval
= (char *)xmalloc (1);
2155 op
= capcase_p (var
) ? CASE_CAPITALIZE
2156 : (uppercase_p (var
) ? CASE_UPPER
: CASE_LOWER
);
2157 oval
= sh_modcase (retval
, (char *)0, op
);
2161 #endif /* CASEMOD_ATTRS */
2164 if (flags
& ASS_APPEND
)
2166 oval
= get_variable_value (var
);
2167 if (oval
== 0) /* paranoia */
2169 olen
= STRLEN (oval
);
2170 retval
= (char *)xmalloc (olen
+ (value
? STRLEN (value
) : 0) + 1);
2171 strcpy (retval
, oval
);
2173 strcpy (retval
+olen
, value
);
2176 retval
= savestring (value
);
2179 retval
= (char *)xmalloc (1);
2184 retval
= (char *)NULL
;
2189 /* Bind a variable NAME to VALUE in the HASH_TABLE TABLE, which may be the
2190 temporary environment (but usually is not). */
2192 bind_variable_internal (name
, value
, table
, hflags
, aflags
)
2201 entry
= (hflags
& HASH_NOSRCH
) ? (SHELL_VAR
*)NULL
: hash_lookup (name
, table
);
2205 entry
= make_new_variable (name
, table
);
2206 var_setvalue (entry
, make_variable_value (entry
, value
, 0)); /* XXX */
2208 else if (entry
->assign_func
) /* array vars have assign functions now */
2210 INVALIDATE_EXPORTSTR (entry
);
2211 newval
= (aflags
& ASS_APPEND
) ? make_variable_value (entry
, value
, aflags
) : value
;
2212 if (assoc_p (entry
))
2213 entry
= (*(entry
->assign_func
)) (entry
, newval
, -1, savestring ("0"));
2214 else if (array_p (entry
))
2215 entry
= (*(entry
->assign_func
)) (entry
, newval
, 0, 0);
2217 entry
= (*(entry
->assign_func
)) (entry
, newval
, -1, 0);
2218 if (newval
!= value
)
2224 if (readonly_p (entry
) || noassign_p (entry
))
2226 if (readonly_p (entry
))
2227 err_readonly (name
);
2231 /* Variables which are bound are visible. */
2232 VUNSETATTR (entry
, att_invisible
);
2234 newval
= make_variable_value (entry
, value
, aflags
); /* XXX */
2236 /* Invalidate any cached export string */
2237 INVALIDATE_EXPORTSTR (entry
);
2239 #if defined (ARRAY_VARS)
2240 /* XXX -- this bears looking at again -- XXX */
2241 /* If an existing array variable x is being assigned to with x=b or
2242 `read x' or something of that nature, silently convert it to
2243 x[0]=b or `read x[0]'. */
2244 if (array_p (entry
))
2246 array_insert (array_cell (entry
), 0, newval
);
2249 else if (assoc_p (entry
))
2251 assoc_insert (assoc_cell (entry
), savestring ("0"), newval
);
2257 FREE (value_cell (entry
));
2258 var_setvalue (entry
, newval
);
2262 if (mark_modified_vars
)
2263 VSETATTR (entry
, att_exported
);
2265 if (exported_p (entry
))
2266 array_needs_making
= 1;
2271 /* Bind a variable NAME to VALUE. This conses up the name
2272 and value strings. If we have a temporary environment, we bind there
2273 first, then we bind into shell_variables. */
2276 bind_variable (name
, value
, flags
)
2284 if (shell_variables
== 0)
2285 create_variable_tables ();
2287 /* If we have a temporary environment, look there first for the variable,
2288 and, if found, modify the value there before modifying it in the
2289 shell_variables table. This allows sourced scripts to modify values
2290 given to them in a temporary environment while modifying the variable
2291 value that the caller sees. */
2293 bind_tempenv_variable (name
, value
);
2295 /* XXX -- handle local variables here. */
2296 for (vc
= shell_variables
; vc
; vc
= vc
->down
)
2298 if (vc_isfuncenv (vc
) || vc_isbltnenv (vc
))
2300 v
= hash_lookup (name
, vc
->table
);
2302 return (bind_variable_internal (name
, value
, vc
->table
, 0, flags
));
2305 return (bind_variable_internal (name
, value
, global_variables
->table
, 0, flags
));
2308 /* Make VAR, a simple shell variable, have value VALUE. Once assigned a
2309 value, variables are no longer invisible. This is a duplicate of part
2310 of the internals of bind_variable. If the variable is exported, or
2311 all modified variables should be exported, mark the variable for export
2312 and note that the export environment needs to be recreated. */
2314 bind_variable_value (var
, value
, aflags
)
2321 VUNSETATTR (var
, att_invisible
);
2323 if (var
->assign_func
)
2325 /* If we're appending, we need the old value, so use
2326 make_variable_value */
2327 t
= (aflags
& ASS_APPEND
) ? make_variable_value (var
, value
, aflags
) : value
;
2328 (*(var
->assign_func
)) (var
, t
, -1, 0);
2329 if (t
!= value
&& t
)
2334 t
= make_variable_value (var
, value
, aflags
);
2335 FREE (value_cell (var
));
2336 var_setvalue (var
, t
);
2339 INVALIDATE_EXPORTSTR (var
);
2341 if (mark_modified_vars
)
2342 VSETATTR (var
, att_exported
);
2344 if (exported_p (var
))
2345 array_needs_making
= 1;
2350 /* Bind/create a shell variable with the name LHS to the RHS.
2351 This creates or modifies a variable such that it is an integer.
2353 This used to be in expr.c, but it is here so that all of the
2354 variable binding stuff is localized. Since we don't want any
2355 recursive evaluation from bind_variable() (possible without this code,
2356 since bind_variable() calls the evaluator for variables with the integer
2357 attribute set), we temporarily turn off the integer attribute for each
2358 variable we set here, then turn it back on after binding as necessary. */
2361 bind_int_variable (lhs
, rhs
)
2364 register SHELL_VAR
*v
;
2368 #if defined (ARRAY_VARS)
2369 if (valid_array_reference (lhs
))
2372 v
= array_variable_part (lhs
, (char **)0, (int *)0);
2376 v
= find_variable (lhs
);
2380 isint
= integer_p (v
);
2381 VUNSETATTR (v
, att_integer
);
2384 #if defined (ARRAY_VARS)
2386 v
= assign_array_element (lhs
, rhs
, 0);
2389 v
= bind_variable (lhs
, rhs
, 0);
2392 VSETATTR (v
, att_integer
);
2398 bind_var_to_int (var
, val
)
2402 char ibuf
[INT_STRLEN_BOUND (intmax_t) + 1], *p
;
2404 p
= fmtulong (val
, 10, ibuf
, sizeof (ibuf
), 0);
2405 return (bind_int_variable (var
, p
));
2408 /* Do a function binding to a variable. You pass the name and
2409 the command to bind to. This conses the name and command. */
2411 bind_function (name
, value
)
2417 entry
= find_function (name
);
2420 BUCKET_CONTENTS
*elt
;
2422 elt
= hash_insert (savestring (name
), shell_functions
, HASH_NOSRCH
);
2423 entry
= new_shell_variable (name
);
2424 elt
->data
= (PTR_T
)entry
;
2427 INVALIDATE_EXPORTSTR (entry
);
2429 if (var_isset (entry
))
2430 dispose_command (function_cell (entry
));
2433 var_setfunc (entry
, copy_command (value
));
2435 var_setfunc (entry
, 0);
2437 VSETATTR (entry
, att_function
);
2439 if (mark_modified_vars
)
2440 VSETATTR (entry
, att_exported
);
2442 VUNSETATTR (entry
, att_invisible
); /* Just to be sure */
2444 if (exported_p (entry
))
2445 array_needs_making
= 1;
2447 #if defined (PROGRAMMABLE_COMPLETION)
2448 set_itemlist_dirty (&it_functions
);
2454 #if defined (DEBUGGER)
2455 /* Bind a function definition, which includes source file and line number
2456 information in addition to the command, into the FUNCTION_DEF hash table.*/
2458 bind_function_def (name
, value
)
2460 FUNCTION_DEF
*value
;
2462 FUNCTION_DEF
*entry
;
2463 BUCKET_CONTENTS
*elt
;
2466 entry
= find_function_def (name
);
2469 dispose_function_def_contents (entry
);
2470 entry
= copy_function_def_contents (value
, entry
);
2474 cmd
= value
->command
;
2476 entry
= copy_function_def (value
);
2477 value
->command
= cmd
;
2479 elt
= hash_insert (savestring (name
), shell_function_defs
, HASH_NOSRCH
);
2480 elt
->data
= (PTR_T
*)entry
;
2483 #endif /* DEBUGGER */
2485 /* Add STRING, which is of the form foo=bar, to the temporary environment
2486 HASH_TABLE (temporary_env). The functions in execute_cmd.c are
2487 responsible for moving the main temporary env to one of the other
2488 temporary environments. The expansion code in subst.c calls this. */
2490 assign_in_env (word
, flags
)
2495 char *name
, *temp
, *value
;
2499 string
= word
->word
;
2501 offset
= assignment (string
, 0);
2502 name
= savestring (string
);
2503 value
= (char *)NULL
;
2505 if (name
[offset
] == '=')
2509 /* ignore the `+' when assigning temporary environment */
2510 if (name
[offset
- 1] == '+')
2511 name
[offset
- 1] = '\0';
2513 var
= find_variable (name
);
2514 if (var
&& (readonly_p (var
) || noassign_p (var
)))
2516 if (readonly_p (var
))
2517 err_readonly (name
);
2522 temp
= name
+ offset
+ 1;
2523 value
= expand_assignment_string_to_string (temp
, 0);
2526 if (temporary_env
== 0)
2527 temporary_env
= hash_create (TEMPENV_HASH_BUCKETS
);
2529 var
= hash_lookup (name
, temporary_env
);
2531 var
= make_new_variable (name
, temporary_env
);
2533 FREE (value_cell (var
));
2537 value
= (char *)xmalloc (1); /* like do_assignment_internal */
2541 var_setvalue (var
, value
);
2542 var
->attributes
|= (att_exported
|att_tempvar
);
2543 var
->context
= variable_context
; /* XXX */
2545 INVALIDATE_EXPORTSTR (var
);
2546 var
->exportstr
= mk_env_string (name
, value
);
2548 array_needs_making
= 1;
2556 stupidly_hack_special_variables (name
);
2558 if (echo_command_at_execute
)
2559 /* The Korn shell prints the `+ ' in front of assignment statements,
2561 xtrace_print_assignment (name
, value
, 0, 1);
2567 /* **************************************************************** */
2569 /* Copying variables */
2571 /* **************************************************************** */
2573 #ifdef INCLUDE_UNUSED
2574 /* Copy VAR to a new data structure and return that structure. */
2579 SHELL_VAR
*copy
= (SHELL_VAR
*)NULL
;
2583 copy
= (SHELL_VAR
*)xmalloc (sizeof (SHELL_VAR
));
2585 copy
->attributes
= var
->attributes
;
2586 copy
->name
= savestring (var
->name
);
2588 if (function_p (var
))
2589 var_setfunc (copy
, copy_command (function_cell (var
)));
2590 #if defined (ARRAY_VARS)
2591 else if (array_p (var
))
2592 var_setarray (copy
, array_copy (array_cell (var
)));
2593 else if (assoc_p (var
))
2594 var_setassoc (copy
, assoc_copy (assoc_cell (var
)));
2596 else if (value_cell (var
))
2597 var_setvalue (copy
, savestring (value_cell (var
)));
2599 var_setvalue (copy
, (char *)NULL
);
2601 copy
->dynamic_value
= var
->dynamic_value
;
2602 copy
->assign_func
= var
->assign_func
;
2604 copy
->exportstr
= COPY_EXPORTSTR (var
);
2606 copy
->context
= var
->context
;
2612 /* **************************************************************** */
2614 /* Deleting and unsetting variables */
2616 /* **************************************************************** */
2618 /* Dispose of the information attached to VAR. */
2620 dispose_variable_value (var
)
2623 if (function_p (var
))
2624 dispose_command (function_cell (var
));
2625 #if defined (ARRAY_VARS)
2626 else if (array_p (var
))
2627 array_dispose (array_cell (var
));
2628 else if (assoc_p (var
))
2629 assoc_dispose (assoc_cell (var
));
2632 FREE (value_cell (var
));
2636 dispose_variable (var
)
2642 if (nofree_p (var
) == 0)
2643 dispose_variable_value (var
);
2645 FREE_EXPORTSTR (var
);
2649 if (exported_p (var
))
2650 array_needs_making
= 1;
2655 /* Unset the shell variable referenced by NAME. */
2657 unbind_variable (name
)
2660 return makunbound (name
, shell_variables
);
2663 /* Unset the shell function named NAME. */
2668 BUCKET_CONTENTS
*elt
;
2671 elt
= hash_remove (name
, shell_functions
, 0);
2676 #if defined (PROGRAMMABLE_COMPLETION)
2677 set_itemlist_dirty (&it_functions
);
2680 func
= (SHELL_VAR
*)elt
->data
;
2683 if (exported_p (func
))
2684 array_needs_making
++;
2685 dispose_variable (func
);
2694 #if defined (DEBUGGER)
2696 unbind_function_def (name
)
2699 BUCKET_CONTENTS
*elt
;
2700 FUNCTION_DEF
*funcdef
;
2702 elt
= hash_remove (name
, shell_function_defs
, 0);
2707 funcdef
= (FUNCTION_DEF
*)elt
->data
;
2709 dispose_function_def (funcdef
);
2716 #endif /* DEBUGGER */
2718 /* Make the variable associated with NAME go away. HASH_LIST is the
2719 hash table from which this variable should be deleted (either
2720 shell_variables or shell_functions).
2721 Returns non-zero if the variable couldn't be found. */
2723 makunbound (name
, vc
)
2727 BUCKET_CONTENTS
*elt
, *new_elt
;
2732 for (elt
= (BUCKET_CONTENTS
*)NULL
, v
= vc
; v
; v
= v
->down
)
2733 if (elt
= hash_remove (name
, v
->table
, 0))
2739 old_var
= (SHELL_VAR
*)elt
->data
;
2741 if (old_var
&& exported_p (old_var
))
2742 array_needs_making
++;
2744 /* If we're unsetting a local variable and we're still executing inside
2745 the function, just mark the variable as invisible. The function
2746 eventually called by pop_var_context() will clean it up later. This
2747 must be done so that if the variable is subsequently assigned a new
2748 value inside the function, the `local' attribute is still present.
2749 We also need to add it back into the correct hash table. */
2750 if (old_var
&& local_p (old_var
) && variable_context
== old_var
->context
)
2752 if (nofree_p (old_var
))
2753 var_setvalue (old_var
, (char *)NULL
);
2754 #if defined (ARRAY_VARS)
2755 else if (array_p (old_var
))
2756 array_dispose (array_cell (old_var
));
2757 else if (assoc_p (old_var
))
2758 assoc_dispose (assoc_cell (old_var
));
2761 FREE (value_cell (old_var
));
2762 /* Reset the attributes. Preserve the export attribute if the variable
2763 came from a temporary environment. Make sure it stays local, and
2764 make it invisible. */
2765 old_var
->attributes
= (exported_p (old_var
) && tempvar_p (old_var
)) ? att_exported
: 0;
2766 VSETATTR (old_var
, att_local
);
2767 VSETATTR (old_var
, att_invisible
);
2768 var_setvalue (old_var
, (char *)NULL
);
2769 INVALIDATE_EXPORTSTR (old_var
);
2771 new_elt
= hash_insert (savestring (old_var
->name
), v
->table
, 0);
2772 new_elt
->data
= (PTR_T
)old_var
;
2773 stupidly_hack_special_variables (old_var
->name
);
2780 /* Have to save a copy of name here, because it might refer to
2781 old_var->name. If so, stupidly_hack_special_variables will
2782 reference freed memory. */
2783 t
= savestring (name
);
2788 dispose_variable (old_var
);
2789 stupidly_hack_special_variables (t
);
2795 /* Get rid of all of the variables in the current context. */
2797 kill_all_local_variables ()
2801 for (vc
= shell_variables
; vc
; vc
= vc
->down
)
2802 if (vc_isfuncenv (vc
) && vc
->scope
== variable_context
)
2807 if (vc
->table
&& vc_haslocals (vc
))
2809 delete_all_variables (vc
->table
);
2810 hash_dispose (vc
->table
);
2812 vc
->table
= (HASH_TABLE
*)NULL
;
2816 free_variable_hash_data (data
)
2821 var
= (SHELL_VAR
*)data
;
2822 dispose_variable (var
);
2825 /* Delete the entire contents of the hash table. */
2827 delete_all_variables (hashed_vars
)
2828 HASH_TABLE
*hashed_vars
;
2830 hash_flush (hashed_vars
, free_variable_hash_data
);
2833 /* **************************************************************** */
2835 /* Setting variable attributes */
2837 /* **************************************************************** */
2839 #define FIND_OR_MAKE_VARIABLE(name, entry) \
2842 entry = find_variable (name); \
2845 entry = bind_variable (name, "", 0); \
2846 if (!no_invisible_vars && entry) entry->attributes |= att_invisible; \
2851 /* Make the variable associated with NAME be readonly.
2852 If NAME does not exist yet, create it. */
2854 set_var_read_only (name
)
2859 FIND_OR_MAKE_VARIABLE (name
, entry
);
2860 VSETATTR (entry
, att_readonly
);
2863 #ifdef INCLUDE_UNUSED
2864 /* Make the function associated with NAME be readonly.
2865 If NAME does not exist, we just punt, like auto_export code below. */
2867 set_func_read_only (name
)
2872 entry
= find_function (name
);
2874 VSETATTR (entry
, att_readonly
);
2877 /* Make the variable associated with NAME be auto-exported.
2878 If NAME does not exist yet, create it. */
2880 set_var_auto_export (name
)
2885 FIND_OR_MAKE_VARIABLE (name
, entry
);
2886 set_auto_export (entry
);
2889 /* Make the function associated with NAME be auto-exported. */
2891 set_func_auto_export (name
)
2896 entry
= find_function (name
);
2898 set_auto_export (entry
);
2902 /* **************************************************************** */
2904 /* Creating lists of variables */
2906 /* **************************************************************** */
2909 vlist_alloc (nentries
)
2914 vlist
= (VARLIST
*)xmalloc (sizeof (VARLIST
));
2915 vlist
->list
= (SHELL_VAR
**)xmalloc ((nentries
+ 1) * sizeof (SHELL_VAR
*));
2916 vlist
->list_size
= nentries
;
2917 vlist
->list_len
= 0;
2918 vlist
->list
[0] = (SHELL_VAR
*)NULL
;
2924 vlist_realloc (vlist
, n
)
2929 return (vlist
= vlist_alloc (n
));
2930 if (n
> vlist
->list_size
)
2932 vlist
->list_size
= n
;
2933 vlist
->list
= (SHELL_VAR
**)xrealloc (vlist
->list
, (vlist
->list_size
+ 1) * sizeof (SHELL_VAR
*));
2939 vlist_add (vlist
, var
, flags
)
2946 for (i
= 0; i
< vlist
->list_len
; i
++)
2947 if (STREQ (var
->name
, vlist
->list
[i
]->name
))
2949 if (i
< vlist
->list_len
)
2952 if (i
>= vlist
->list_size
)
2953 vlist
= vlist_realloc (vlist
, vlist
->list_size
+ 16);
2955 vlist
->list
[vlist
->list_len
++] = var
;
2956 vlist
->list
[vlist
->list_len
] = (SHELL_VAR
*)NULL
;
2959 /* Map FUNCTION over the variables in VAR_HASH_TABLE. Return an array of the
2960 variables for which FUNCTION returns a non-zero value. A NULL value
2961 for FUNCTION means to use all variables. */
2963 map_over (function
, vc
)
2964 sh_var_map_func_t
*function
;
2972 for (nentries
= 0, v
= vc
; v
; v
= v
->down
)
2973 nentries
+= HASH_ENTRIES (v
->table
);
2976 return (SHELL_VAR
**)NULL
;
2978 vlist
= vlist_alloc (nentries
);
2980 for (v
= vc
; v
; v
= v
->down
)
2981 flatten (v
->table
, function
, vlist
, 0);
2989 map_over_funcs (function
)
2990 sh_var_map_func_t
*function
;
2995 if (shell_functions
== 0 || HASH_ENTRIES (shell_functions
) == 0)
2996 return ((SHELL_VAR
**)NULL
);
2998 vlist
= vlist_alloc (HASH_ENTRIES (shell_functions
));
3000 flatten (shell_functions
, function
, vlist
, 0);
3007 /* Flatten VAR_HASH_TABLE, applying FUNC to each member and adding those
3008 elements for which FUNC succeeds to VLIST->list. FLAGS is reserved
3009 for future use. Only unique names are added to VLIST. If FUNC is
3010 NULL, each variable in VAR_HASH_TABLE is added to VLIST. If VLIST is
3011 NULL, FUNC is applied to each SHELL_VAR in VAR_HASH_TABLE. If VLIST
3012 and FUNC are both NULL, nothing happens. */
3014 flatten (var_hash_table
, func
, vlist
, flags
)
3015 HASH_TABLE
*var_hash_table
;
3016 sh_var_map_func_t
*func
;
3021 register BUCKET_CONTENTS
*tlist
;
3025 if (var_hash_table
== 0 || (HASH_ENTRIES (var_hash_table
) == 0) || (vlist
== 0 && func
== 0))
3028 for (i
= 0; i
< var_hash_table
->nbuckets
; i
++)
3030 for (tlist
= hash_items (i
, var_hash_table
); tlist
; tlist
= tlist
->next
)
3032 var
= (SHELL_VAR
*)tlist
->data
;
3034 r
= func
? (*func
) (var
) : 1;
3036 vlist_add (vlist
, var
, flags
);
3042 sort_variables (array
)
3045 qsort (array
, strvec_len ((char **)array
), sizeof (SHELL_VAR
*), (QSFUNC
*)qsort_var_comp
);
3049 qsort_var_comp (var1
, var2
)
3050 SHELL_VAR
**var1
, **var2
;
3054 if ((result
= (*var1
)->name
[0] - (*var2
)->name
[0]) == 0)
3055 result
= strcmp ((*var1
)->name
, (*var2
)->name
);
3060 /* Apply FUNC to each variable in SHELL_VARIABLES, adding each one for
3061 which FUNC succeeds to an array of SHELL_VAR *s. Returns the array. */
3064 sh_var_map_func_t
*func
;
3068 list
= map_over (func
, shell_variables
);
3069 if (list
/* && posixly_correct */)
3070 sort_variables (list
);
3074 /* Apply FUNC to each variable in SHELL_FUNCTIONS, adding each one for
3075 which FUNC succeeds to an array of SHELL_VAR *s. Returns the array. */
3078 sh_var_map_func_t
*func
;
3082 list
= map_over_funcs (func
);
3083 if (list
/* && posixly_correct */)
3084 sort_variables (list
);
3088 /* Create a NULL terminated array of all the shell variables. */
3090 all_shell_variables ()
3092 return (vapply ((sh_var_map_func_t
*)NULL
));
3095 /* Create a NULL terminated array of all the shell functions. */
3097 all_shell_functions ()
3099 return (fapply ((sh_var_map_func_t
*)NULL
));
3106 return (invisible_p (var
) == 0);
3110 all_visible_functions ()
3112 return (fapply (visible_var
));
3116 all_visible_variables ()
3118 return (vapply (visible_var
));
3121 /* Return non-zero if the variable VAR is visible and exported. Array
3122 variables cannot be exported. */
3124 visible_and_exported (var
)
3127 return (invisible_p (var
) == 0 && exported_p (var
));
3130 /* Candidate variables for the export environment are either valid variables
3131 with the export attribute or invalid variables inherited from the initial
3132 environment and simply passed through. */
3134 export_environment_candidate (var
)
3137 return (exported_p (var
) && (invisible_p (var
) == 0 || imported_p (var
)));
3140 /* Return non-zero if VAR is a local variable in the current context and
3143 local_and_exported (var
)
3146 return (invisible_p (var
) == 0 && local_p (var
) && var
->context
== variable_context
&& exported_p (var
));
3150 all_exported_variables ()
3152 return (vapply (visible_and_exported
));
3156 local_exported_variables ()
3158 return (vapply (local_and_exported
));
3162 variable_in_context (var
)
3165 return (invisible_p (var
) == 0 && local_p (var
) && var
->context
== variable_context
);
3169 all_local_variables ()
3175 vc
= shell_variables
;
3176 for (vc
= shell_variables
; vc
; vc
= vc
->down
)
3177 if (vc_isfuncenv (vc
) && vc
->scope
== variable_context
)
3182 internal_error (_("all_local_variables: no function context at current scope"));
3183 return (SHELL_VAR
**)NULL
;
3185 if (vc
->table
== 0 || HASH_ENTRIES (vc
->table
) == 0 || vc_haslocals (vc
) == 0)
3186 return (SHELL_VAR
**)NULL
;
3188 vlist
= vlist_alloc (HASH_ENTRIES (vc
->table
));
3190 flatten (vc
->table
, variable_in_context
, vlist
, 0);
3195 sort_variables (ret
);
3199 #if defined (ARRAY_VARS)
3200 /* Return non-zero if the variable VAR is visible and an array. */
3202 visible_array_vars (var
)
3205 return (invisible_p (var
) == 0 && array_p (var
));
3209 all_array_variables ()
3211 return (vapply (visible_array_vars
));
3213 #endif /* ARRAY_VARS */
3216 all_variables_matching_prefix (prefix
)
3219 SHELL_VAR
**varlist
;
3221 int vind
, rind
, plen
;
3223 plen
= STRLEN (prefix
);
3224 varlist
= all_visible_variables ();
3225 for (vind
= 0; varlist
&& varlist
[vind
]; vind
++)
3227 if (varlist
== 0 || vind
== 0)
3228 return ((char **)NULL
);
3229 rlist
= strvec_create (vind
+ 1);
3230 for (vind
= rind
= 0; varlist
[vind
]; vind
++)
3232 if (plen
== 0 || STREQN (prefix
, varlist
[vind
]->name
, plen
))
3233 rlist
[rind
++] = savestring (varlist
[vind
]->name
);
3235 rlist
[rind
] = (char *)0;
3241 /* **************************************************************** */
3243 /* Managing temporary variable scopes */
3245 /* **************************************************************** */
3247 /* Make variable NAME have VALUE in the temporary environment. */
3249 bind_tempenv_variable (name
, value
)
3255 var
= temporary_env
? hash_lookup (name
, temporary_env
) : (SHELL_VAR
*)NULL
;
3259 FREE (value_cell (var
));
3260 var_setvalue (var
, savestring (value
));
3261 INVALIDATE_EXPORTSTR (var
);
3267 /* Find a variable in the temporary environment that is named NAME.
3268 Return the SHELL_VAR *, or NULL if not found. */
3270 find_tempenv_variable (name
)
3273 return (temporary_env
? hash_lookup (name
, temporary_env
) : (SHELL_VAR
*)NULL
);
3276 char **tempvar_list
;
3279 /* Push the variable described by (SHELL_VAR *)DATA down to the next
3280 variable context from the temporary environment. */
3282 push_temp_var (data
)
3286 HASH_TABLE
*binding_table
;
3288 var
= (SHELL_VAR
*)data
;
3290 binding_table
= shell_variables
->table
;
3291 if (binding_table
== 0)
3293 if (shell_variables
== global_variables
)
3294 /* shouldn't happen */
3295 binding_table
= shell_variables
->table
= global_variables
->table
= hash_create (0);
3297 binding_table
= shell_variables
->table
= hash_create (TEMPENV_HASH_BUCKETS
);
3300 v
= bind_variable_internal (var
->name
, value_cell (var
), binding_table
, 0, 0);
3302 /* XXX - should we set the context here? It shouldn't matter because of how
3303 assign_in_env works, but might want to check. */
3304 if (binding_table
== global_variables
->table
) /* XXX */
3305 var
->attributes
&= ~(att_tempvar
|att_propagate
);
3308 var
->attributes
|= att_propagate
;
3309 if (binding_table
== shell_variables
->table
)
3310 shell_variables
->flags
|= VC_HASTMPVAR
;
3312 v
->attributes
|= var
->attributes
;
3314 if (find_special_var (var
->name
) >= 0)
3315 tempvar_list
[tvlist_ind
++] = savestring (var
->name
);
3317 dispose_variable (var
);
3321 propagate_temp_var (data
)
3326 var
= (SHELL_VAR
*)data
;
3327 if (tempvar_p (var
) && (var
->attributes
& att_propagate
))
3328 push_temp_var (data
);
3331 if (find_special_var (var
->name
) >= 0)
3332 tempvar_list
[tvlist_ind
++] = savestring (var
->name
);
3333 dispose_variable (var
);
3337 /* Free the storage used in the hash table for temporary
3338 environment variables. PUSHF is a function to be called
3339 to free each hash table entry. It takes care of pushing variables
3340 to previous scopes if appropriate. PUSHF stores names of variables
3341 that require special handling (e.g., IFS) on tempvar_list, so this
3342 function can call stupidly_hack_special_variables on all the
3343 variables in the list when the temporary hash table is destroyed. */
3345 dispose_temporary_env (pushf
)
3346 sh_free_func_t
*pushf
;
3350 tempvar_list
= strvec_create (HASH_ENTRIES (temporary_env
) + 1);
3351 tempvar_list
[tvlist_ind
= 0] = 0;
3353 hash_flush (temporary_env
, pushf
);
3354 hash_dispose (temporary_env
);
3355 temporary_env
= (HASH_TABLE
*)NULL
;
3357 tempvar_list
[tvlist_ind
] = 0;
3359 array_needs_making
= 1;
3362 sv_ifs ("IFS"); /* XXX here for now -- check setifs in assign_in_env */
3364 for (i
= 0; i
< tvlist_ind
; i
++)
3365 stupidly_hack_special_variables (tempvar_list
[i
]);
3367 strvec_dispose (tempvar_list
);
3373 dispose_used_env_vars ()
3377 dispose_temporary_env (propagate_temp_var
);
3378 maybe_make_export_env ();
3382 /* Take all of the shell variables in the temporary environment HASH_TABLE
3383 and make shell variables from them at the current variable context. */
3385 merge_temporary_env ()
3388 dispose_temporary_env (push_temp_var
);
3391 /* **************************************************************** */
3393 /* Creating and manipulating the environment */
3395 /* **************************************************************** */
3397 static inline char *
3398 mk_env_string (name
, value
)
3399 const char *name
, *value
;
3401 int name_len
, value_len
;
3404 name_len
= strlen (name
);
3405 value_len
= STRLEN (value
);
3406 p
= (char *)xmalloc (2 + name_len
+ value_len
);
3409 if (value
&& *value
)
3410 strcpy (p
+ name_len
+ 1, value
);
3412 p
[name_len
+ 1] = '\0';
3427 internal_error (_("%s has null exportstr"), v
->name
);
3430 if (legal_variable_starter ((unsigned char)*s
) == 0)
3432 internal_error (_("invalid character %d in exportstr for %s"), *s
, v
->name
);
3435 for (s
= v
->exportstr
+ 1; s
&& *s
; s
++)
3439 if (legal_variable_char ((unsigned char)*s
) == 0)
3441 internal_error (_("invalid character %d in exportstr for %s"), *s
, v
->name
);
3447 internal_error (_("no `=' in exportstr for %s"), v
->name
);
3455 make_env_array_from_var_list (vars
)
3458 register int i
, list_index
;
3459 register SHELL_VAR
*var
;
3460 char **list
, *value
;
3462 list
= strvec_create ((1 + strvec_len ((char **)vars
)));
3464 #define USE_EXPORTSTR (value == var->exportstr)
3466 for (i
= 0, list_index
= 0; var
= vars
[i
]; i
++)
3468 #if defined (__CYGWIN__)
3469 /* We don't use the exportstr stuff on Cygwin at all. */
3470 INVALIDATE_EXPORTSTR (var
);
3473 value
= var
->exportstr
;
3474 else if (function_p (var
))
3475 value
= named_function_string ((char *)NULL
, function_cell (var
), 0);
3476 #if defined (ARRAY_VARS)
3477 else if (array_p (var
))
3479 value
= array_to_assignment_string (array_cell (var
));
3481 continue; /* XXX array vars cannot yet be exported */
3483 else if (assoc_p (var
))
3485 value
= assoc_to_assignment_string (assoc_cell (var
));
3487 continue; /* XXX associative array vars cannot yet be exported */
3491 value
= value_cell (var
);
3495 /* Gee, I'd like to get away with not using savestring() if we're
3496 using the cached exportstr... */
3497 list
[list_index
] = USE_EXPORTSTR
? savestring (value
)
3498 : mk_env_string (var
->name
, value
);
3500 if (USE_EXPORTSTR
== 0)
3501 SAVE_EXPORTSTR (var
, list
[list_index
]);
3504 #undef USE_EXPORTSTR
3507 #if defined (ARRAY_VARS)
3508 if (array_p (var
) || assoc_p (var
))
3515 list
[list_index
] = (char *)NULL
;
3519 /* Make an array of assignment statements from the hash table
3520 HASHED_VARS which contains SHELL_VARs. Only visible, exported
3521 variables are eligible. */
3523 make_var_export_array (vcxt
)
3530 vars
= map_over (visible_and_exported
, vcxt
);
3532 vars
= map_over (export_environment_candidate
, vcxt
);
3536 return (char **)NULL
;
3538 list
= make_env_array_from_var_list (vars
);
3545 make_func_export_array ()
3550 vars
= map_over_funcs (visible_and_exported
);
3552 return (char **)NULL
;
3554 list
= make_env_array_from_var_list (vars
);
3560 /* Add ENVSTR to the end of the exported environment, EXPORT_ENV. */
3561 #define add_to_export_env(envstr,do_alloc) \
3564 if (export_env_index >= (export_env_size - 1)) \
3566 export_env_size += 16; \
3567 export_env = strvec_resize (export_env, export_env_size); \
3568 environ = export_env; \
3570 export_env[export_env_index++] = (do_alloc) ? savestring (envstr) : envstr; \
3571 export_env[export_env_index] = (char *)NULL; \
3574 /* Add ASSIGN to EXPORT_ENV, or supercede a previous assignment in the
3575 array with the same left-hand side. Return the new EXPORT_ENV. */
3577 add_or_supercede_exported_var (assign
, do_alloc
)
3584 equal_offset
= assignment (assign
, 0);
3585 if (equal_offset
== 0)
3586 return (export_env
);
3588 /* If this is a function, then only supersede the function definition.
3589 We do this by including the `=() {' in the comparison, like
3590 initialize_shell_variables does. */
3591 if (assign
[equal_offset
+ 1] == '(' &&
3592 strncmp (assign
+ equal_offset
+ 2, ") {", 3) == 0) /* } */
3595 for (i
= 0; i
< export_env_index
; i
++)
3597 if (STREQN (assign
, export_env
[i
], equal_offset
+ 1))
3599 free (export_env
[i
]);
3600 export_env
[i
] = do_alloc
? savestring (assign
) : assign
;
3601 return (export_env
);
3604 add_to_export_env (assign
, do_alloc
);
3605 return (export_env
);
3609 add_temp_array_to_env (temp_array
, do_alloc
, do_supercede
)
3611 int do_alloc
, do_supercede
;
3615 if (temp_array
== 0)
3618 for (i
= 0; temp_array
[i
]; i
++)
3621 export_env
= add_or_supercede_exported_var (temp_array
[i
], do_alloc
);
3623 add_to_export_env (temp_array
[i
], do_alloc
);
3629 /* Make the environment array for the command about to be executed, if the
3630 array needs making. Otherwise, do nothing. If a shell action could
3631 change the array that commands receive for their environment, then the
3632 code should `array_needs_making++'.
3634 The order to add to the array is:
3636 list of var contexts whose head is shell_variables
3639 This is the shell variable lookup order. We add only new variable
3640 names at each step, which allows local variables and variables in
3641 the temporary environments to shadow variables in the global (or
3642 any previous) scope.
3646 n_shell_variables ()
3651 for (n
= 0, vc
= shell_variables
; vc
; vc
= vc
->down
)
3652 n
+= HASH_ENTRIES (vc
->table
);
3657 maybe_make_export_env ()
3659 register char **temp_array
;
3663 if (array_needs_making
)
3666 strvec_flush (export_env
);
3668 /* Make a guess based on how many shell variables and functions we
3669 have. Since there will always be array variables, and array
3670 variables are not (yet) exported, this will always be big enough
3671 for the exported variables and functions. */
3672 new_size
= n_shell_variables () + HASH_ENTRIES (shell_functions
) + 1 +
3673 HASH_ENTRIES (temporary_env
);
3674 if (new_size
> export_env_size
)
3676 export_env_size
= new_size
;
3677 export_env
= strvec_resize (export_env
, export_env_size
);
3678 environ
= export_env
;
3680 export_env
[export_env_index
= 0] = (char *)NULL
;
3682 /* Make a dummy variable context from the temporary_env, stick it on
3683 the front of shell_variables, call make_var_export_array on the
3684 whole thing to flatten it, and convert the list of SHELL_VAR *s
3685 to the form needed by the environment. */
3688 tcxt
= new_var_context ((char *)NULL
, 0);
3689 tcxt
->table
= temporary_env
;
3690 tcxt
->down
= shell_variables
;
3693 tcxt
= shell_variables
;
3695 temp_array
= make_var_export_array (tcxt
);
3697 add_temp_array_to_env (temp_array
, 0, 0);
3699 if (tcxt
!= shell_variables
)
3702 #if defined (RESTRICTED_SHELL)
3703 /* Restricted shells may not export shell functions. */
3704 temp_array
= restricted
? (char **)0 : make_func_export_array ();
3706 temp_array
= make_func_export_array ();
3709 add_temp_array_to_env (temp_array
, 0, 0);
3711 array_needs_making
= 0;
3715 /* This is an efficiency hack. PWD and OLDPWD are auto-exported, so
3716 we will need to remake the exported environment every time we
3717 change directories. `_' is always put into the environment for
3718 every external command, so without special treatment it will always
3719 cause the environment to be remade.
3721 If there is no other reason to make the exported environment, we can
3722 just update the variables in place and mark the exported environment
3723 as no longer needing a remake. */
3725 update_export_env_inplace (env_prefix
, preflen
, value
)
3732 evar
= (char *)xmalloc (STRLEN (value
) + preflen
+ 1);
3733 strcpy (evar
, env_prefix
);
3735 strcpy (evar
+ preflen
, value
);
3736 export_env
= add_or_supercede_exported_var (evar
, 0);
3739 /* We always put _ in the environment as the name of this command. */
3741 put_command_name_into_env (command_name
)
3744 update_export_env_inplace ("_=", 2, command_name
);
3747 #if 0 /* UNUSED -- it caused too many problems */
3749 put_gnu_argv_flags_into_env (pid
, flags_string
)
3759 fl
= strlen (flags_string
);
3761 dummy
= (char *)xmalloc (l
+ fl
+ 30);
3763 strcpy (dummy
+ 1, pbuf
);
3764 strcpy (dummy
+ 1 + l
, "_GNU_nonoption_argv_flags_");
3765 dummy
[l
+ 27] = '=';
3766 strcpy (dummy
+ l
+ 28, flags_string
);
3770 export_env
= add_or_supercede_exported_var (dummy
, 0);
3774 /* **************************************************************** */
3776 /* Managing variable contexts */
3778 /* **************************************************************** */
3780 /* Allocate and return a new variable context with NAME and FLAGS.
3781 NAME can be NULL. */
3784 new_var_context (name
, flags
)
3790 vc
= (VAR_CONTEXT
*)xmalloc (sizeof (VAR_CONTEXT
));
3791 vc
->name
= name
? savestring (name
) : (char *)NULL
;
3792 vc
->scope
= variable_context
;
3795 vc
->up
= vc
->down
= (VAR_CONTEXT
*)NULL
;
3796 vc
->table
= (HASH_TABLE
*)NULL
;
3801 /* Free a variable context and its data, including the hash table. Dispose
3802 all of the variables. */
3804 dispose_var_context (vc
)
3811 delete_all_variables (vc
->table
);
3812 hash_dispose (vc
->table
);
3818 /* Set VAR's scope level to the current variable context. */
3823 return (var
->context
= variable_context
);
3826 /* Make a new variable context with NAME and FLAGS and a HASH_TABLE of
3827 temporary variables, and push it onto shell_variables. This is
3828 for shell functions. */
3830 push_var_context (name
, flags
, tempvars
)
3833 HASH_TABLE
*tempvars
;
3837 vc
= new_var_context (name
, flags
);
3838 vc
->table
= tempvars
;
3841 /* Have to do this because the temp environment was created before
3842 variable_context was incremented. */
3843 flatten (tempvars
, set_context
, (VARLIST
*)NULL
, 0);
3844 vc
->flags
|= VC_HASTMPVAR
;
3846 vc
->down
= shell_variables
;
3847 shell_variables
->up
= vc
;
3849 return (shell_variables
= vc
);
3853 push_func_var (data
)
3858 var
= (SHELL_VAR
*)data
;
3860 if (tempvar_p (var
) && (posixly_correct
|| (var
->attributes
& att_propagate
)))
3862 /* Make sure we have a hash table to store the variable in while it is
3863 being propagated down to the global variables table. Create one if
3865 if ((vc_isfuncenv (shell_variables
) || vc_istempenv (shell_variables
)) && shell_variables
->table
== 0)
3866 shell_variables
->table
= hash_create (0);
3867 /* XXX - should we set v->context here? */
3868 v
= bind_variable_internal (var
->name
, value_cell (var
), shell_variables
->table
, 0, 0);
3869 if (shell_variables
== global_variables
)
3870 var
->attributes
&= ~(att_tempvar
|att_propagate
);
3872 shell_variables
->flags
|= VC_HASTMPVAR
;
3873 v
->attributes
|= var
->attributes
;
3876 stupidly_hack_special_variables (var
->name
); /* XXX */
3878 dispose_variable (var
);
3881 /* Pop the top context off of VCXT and dispose of it, returning the rest of
3886 VAR_CONTEXT
*ret
, *vcxt
;
3888 vcxt
= shell_variables
;
3889 if (vc_isfuncenv (vcxt
) == 0)
3891 internal_error (_("pop_var_context: head of shell_variables not a function context"));
3895 if (ret
= vcxt
->down
)
3897 ret
->up
= (VAR_CONTEXT
*)NULL
;
3898 shell_variables
= ret
;
3900 hash_flush (vcxt
->table
, push_func_var
);
3901 dispose_var_context (vcxt
);
3904 internal_error (_("pop_var_context: no global_variables context"));
3907 /* Delete the HASH_TABLEs for all variable contexts beginning at VCXT, and
3908 all of the VAR_CONTEXTs except GLOBAL_VARIABLES. */
3910 delete_all_contexts (vcxt
)
3915 for (v
= vcxt
; v
!= global_variables
; v
= t
)
3918 dispose_var_context (v
);
3921 delete_all_variables (global_variables
->table
);
3922 shell_variables
= global_variables
;
3925 /* **************************************************************** */
3927 /* Pushing and Popping temporary variable scopes */
3929 /* **************************************************************** */
3932 push_scope (flags
, tmpvars
)
3934 HASH_TABLE
*tmpvars
;
3936 return (push_var_context ((char *)NULL
, flags
, tmpvars
));
3940 push_exported_var (data
)
3945 var
= (SHELL_VAR
*)data
;
3947 /* If a temp var had its export attribute set, or it's marked to be
3948 propagated, bind it in the previous scope before disposing it. */
3949 /* XXX - This isn't exactly right, because all tempenv variables have the
3950 export attribute set. */
3952 if (exported_p (var
) || (var
->attributes
& att_propagate
))
3954 if (tempvar_p (var
) && exported_p (var
) && (var
->attributes
& att_propagate
))
3957 var
->attributes
&= ~att_tempvar
; /* XXX */
3958 v
= bind_variable_internal (var
->name
, value_cell (var
), shell_variables
->table
, 0, 0);
3959 if (shell_variables
== global_variables
)
3960 var
->attributes
&= ~att_propagate
;
3961 v
->attributes
|= var
->attributes
;
3964 stupidly_hack_special_variables (var
->name
); /* XXX */
3966 dispose_variable (var
);
3970 pop_scope (is_special
)
3973 VAR_CONTEXT
*vcxt
, *ret
;
3975 vcxt
= shell_variables
;
3976 if (vc_istempscope (vcxt
) == 0)
3978 internal_error (_("pop_scope: head of shell_variables not a temporary environment scope"));
3984 ret
->up
= (VAR_CONTEXT
*)NULL
;
3986 shell_variables
= ret
;
3988 /* Now we can take care of merging variables in VCXT into set of scopes
3989 whose head is RET (shell_variables). */
3994 hash_flush (vcxt
->table
, push_func_var
);
3996 hash_flush (vcxt
->table
, push_exported_var
);
3997 hash_dispose (vcxt
->table
);
4001 sv_ifs ("IFS"); /* XXX here for now */
4004 /* **************************************************************** */
4006 /* Pushing and Popping function contexts */
4008 /* **************************************************************** */
4010 static WORD_LIST
**dollar_arg_stack
= (WORD_LIST
**)NULL
;
4011 static int dollar_arg_stack_slots
;
4012 static int dollar_arg_stack_index
;
4014 /* XXX - we might want to consider pushing and popping the `getopts' state
4015 when we modify the positional parameters. */
4017 push_context (name
, is_subshell
, tempvars
)
4018 char *name
; /* function name */
4020 HASH_TABLE
*tempvars
;
4022 if (is_subshell
== 0)
4023 push_dollar_vars ();
4025 push_var_context (name
, VC_FUNCENV
, tempvars
);
4028 /* Only called when subshell == 0, so we don't need to check, and can
4029 unconditionally pop the dollar vars off the stack. */
4037 sv_ifs ("IFS"); /* XXX here for now */
4040 /* Save the existing positional parameters on a stack. */
4044 if (dollar_arg_stack_index
+ 2 > dollar_arg_stack_slots
)
4046 dollar_arg_stack
= (WORD_LIST
**)
4047 xrealloc (dollar_arg_stack
, (dollar_arg_stack_slots
+= 10)
4048 * sizeof (WORD_LIST
**));
4050 dollar_arg_stack
[dollar_arg_stack_index
++] = list_rest_of_args ();
4051 dollar_arg_stack
[dollar_arg_stack_index
] = (WORD_LIST
*)NULL
;
4054 /* Restore the positional parameters from our stack. */
4058 if (!dollar_arg_stack
|| dollar_arg_stack_index
== 0)
4061 remember_args (dollar_arg_stack
[--dollar_arg_stack_index
], 1);
4062 dispose_words (dollar_arg_stack
[dollar_arg_stack_index
]);
4063 dollar_arg_stack
[dollar_arg_stack_index
] = (WORD_LIST
*)NULL
;
4064 set_dollar_vars_unchanged ();
4068 dispose_saved_dollar_vars ()
4070 if (!dollar_arg_stack
|| dollar_arg_stack_index
== 0)
4073 dispose_words (dollar_arg_stack
[dollar_arg_stack_index
]);
4074 dollar_arg_stack
[dollar_arg_stack_index
] = (WORD_LIST
*)NULL
;
4077 /* Manipulate the special BASH_ARGV and BASH_ARGC variables. */
4083 #if defined (ARRAY_VARS) && defined (DEBUGGER)
4084 SHELL_VAR
*bash_argv_v
, *bash_argc_v
;
4085 ARRAY
*bash_argv_a
, *bash_argc_a
;
4090 GET_ARRAY_FROM_VAR ("BASH_ARGV", bash_argv_v
, bash_argv_a
);
4091 GET_ARRAY_FROM_VAR ("BASH_ARGC", bash_argc_v
, bash_argc_a
);
4093 for (l
= list
, i
= 0; l
; l
= l
->next
, i
++)
4094 array_push (bash_argv_a
, l
->word
->word
);
4097 array_push (bash_argc_a
, t
);
4099 #endif /* ARRAY_VARS && DEBUGGER */
4102 /* Remove arguments from BASH_ARGV array. Pop top element off BASH_ARGC
4103 array and use that value as the count of elements to remove from
4108 #if defined (ARRAY_VARS) && defined (DEBUGGER)
4109 SHELL_VAR
*bash_argv_v
, *bash_argc_v
;
4110 ARRAY
*bash_argv_a
, *bash_argc_a
;
4114 GET_ARRAY_FROM_VAR ("BASH_ARGV", bash_argv_v
, bash_argv_a
);
4115 GET_ARRAY_FROM_VAR ("BASH_ARGC", bash_argc_v
, bash_argc_a
);
4117 ce
= array_shift (bash_argc_a
, 1, 0);
4118 if (ce
== 0 || legal_number (element_value (ce
), &i
) == 0)
4122 array_pop (bash_argv_a
);
4123 array_dispose_element (ce
);
4124 #endif /* ARRAY_VARS && DEBUGGER */
4127 /*************************************************
4129 * Functions to manage special variables *
4131 *************************************************/
4133 /* Extern declarations for variables this code has to manage. */
4134 extern int eof_encountered
, eof_encountered_limit
, ignoreeof
;
4136 #if defined (READLINE)
4137 extern int hostname_list_initialized
;
4140 /* An alist of name.function for each special variable. Most of the
4141 functions don't do much, and in fact, this would be faster with a
4142 switch statement, but by the end of this file, I am sick of switch
4145 #define SET_INT_VAR(name, intvar) intvar = find_variable (name) != 0
4147 /* This table will be sorted with qsort() the first time it's accessed. */
4148 struct name_and_function
{
4150 sh_sv_func_t
*function
;
4153 static struct name_and_function special_vars
[] = {
4154 { "BASH_XTRACEFD", sv_xtracefd
},
4156 #if defined (READLINE)
4157 # if defined (STRICT_POSIX)
4158 { "COLUMNS", sv_winsize
},
4160 { "COMP_WORDBREAKS", sv_comp_wordbreaks
},
4163 { "FUNCNEST", sv_funcnest
},
4165 { "GLOBIGNORE", sv_globignore
},
4167 #if defined (HISTORY)
4168 { "HISTCONTROL", sv_history_control
},
4169 { "HISTFILESIZE", sv_histsize
},
4170 { "HISTIGNORE", sv_histignore
},
4171 { "HISTSIZE", sv_histsize
},
4172 { "HISTTIMEFORMAT", sv_histtimefmt
},
4175 #if defined (__CYGWIN__)
4176 { "HOME", sv_home
},
4179 #if defined (READLINE)
4180 { "HOSTFILE", sv_hostfile
},
4184 { "IGNOREEOF", sv_ignoreeof
},
4186 { "LANG", sv_locale
},
4187 { "LC_ALL", sv_locale
},
4188 { "LC_COLLATE", sv_locale
},
4189 { "LC_CTYPE", sv_locale
},
4190 { "LC_MESSAGES", sv_locale
},
4191 { "LC_NUMERIC", sv_locale
},
4192 { "LC_TIME", sv_locale
},
4194 #if defined (READLINE) && defined (STRICT_POSIX)
4195 { "LINES", sv_winsize
},
4198 { "MAIL", sv_mail
},
4199 { "MAILCHECK", sv_mail
},
4200 { "MAILPATH", sv_mail
},
4202 { "OPTERR", sv_opterr
},
4203 { "OPTIND", sv_optind
},
4205 { "PATH", sv_path
},
4206 { "POSIXLY_CORRECT", sv_strict_posix
},
4208 #if defined (READLINE)
4209 { "TERM", sv_terminal
},
4210 { "TERMCAP", sv_terminal
},
4211 { "TERMINFO", sv_terminal
},
4212 #endif /* READLINE */
4214 { "TEXTDOMAIN", sv_locale
},
4215 { "TEXTDOMAINDIR", sv_locale
},
4217 #if defined (HAVE_TZSET) && defined (PROMPT_STRING_DECODE)
4221 #if defined (HISTORY) && defined (BANG_HISTORY)
4222 { "histchars", sv_histchars
},
4223 #endif /* HISTORY && BANG_HISTORY */
4225 { "ignoreeof", sv_ignoreeof
},
4227 { (char *)0, (sh_sv_func_t
*)0 }
4230 #define N_SPECIAL_VARS (sizeof (special_vars) / sizeof (special_vars[0]) - 1)
4233 sv_compare (sv1
, sv2
)
4234 struct name_and_function
*sv1
, *sv2
;
4238 if ((r
= sv1
->name
[0] - sv2
->name
[0]) == 0)
4239 r
= strcmp (sv1
->name
, sv2
->name
);
4244 find_special_var (name
)
4249 for (i
= 0; special_vars
[i
].name
; i
++)
4251 r
= special_vars
[i
].name
[0] - name
[0];
4253 r
= strcmp (special_vars
[i
].name
, name
);
4257 /* Can't match any of rest of elements in sorted list. Take this out
4258 if it causes problems in certain environments. */
4264 /* The variable in NAME has just had its state changed. Check to see if it
4265 is one of the special ones where something special happens. */
4267 stupidly_hack_special_variables (name
)
4270 static int sv_sorted
= 0;
4273 if (sv_sorted
== 0) /* shouldn't need, but it's fairly cheap. */
4275 qsort (special_vars
, N_SPECIAL_VARS
, sizeof (special_vars
[0]),
4276 (QSFUNC
*)sv_compare
);
4280 i
= find_special_var (name
);
4282 (*(special_vars
[i
].function
)) (name
);
4285 /* Special variables that need hooks to be run when they are unset as part
4286 of shell reinitialization should have their sv_ functions run here. */
4288 reinit_special_variables ()
4290 #if defined (READLINE)
4291 sv_comp_wordbreaks ("COMP_WORDBREAKS");
4293 sv_globignore ("GLOBIGNORE");
4294 sv_opterr ("OPTERR");
4303 v
= find_variable ("IFS");
4307 /* What to do just after the PATH variable has changed. */
4316 /* What to do just after one of the MAILxxxx variables has changed. NAME
4317 is the name of the variable. This is called with NAME set to one of
4318 MAIL, MAILCHECK, or MAILPATH. */
4323 /* If the time interval for checking the files has changed, then
4324 reset the mail timer. Otherwise, one of the pathname vars
4325 to the users mailbox has changed, so rebuild the array of
4327 if (name
[4] == 'C') /* if (strcmp (name, "MAILCHECK") == 0) */
4328 reset_mail_timer ();
4332 remember_mail_dates ();
4343 v
= find_variable (name
);
4346 else if (legal_number (value_cell (v
), &num
) == 0)
4352 /* What to do when GLOBIGNORE changes. */
4354 sv_globignore (name
)
4357 if (privileged_mode
== 0)
4358 setup_glob_ignore (name
);
4361 #if defined (READLINE)
4363 sv_comp_wordbreaks (name
)
4368 sv
= find_variable (name
);
4370 reset_completer_word_break_chars ();
4373 /* What to do just after one of the TERMxxx variables has changed.
4374 If we are an interactive shell, then try to reset the terminal
4375 information in readline. */
4380 if (interactive_shell
&& no_line_editing
== 0)
4381 rl_reset_terminal (get_string_value ("TERM"));
4390 v
= find_variable (name
);
4392 clear_hostname_list ();
4394 hostname_list_initialized
= 0;
4397 #if defined (STRICT_POSIX)
4398 /* In strict posix mode, we allow assignments to LINES and COLUMNS (and values
4399 found in the initial environment) to override the terminal size reported by
4409 if (posixly_correct
== 0 || interactive_shell
== 0 || no_line_editing
)
4412 v
= find_variable (name
);
4413 if (v
== 0 || var_isnull (v
))
4414 rl_reset_screen_size ();
4417 if (legal_number (value_cell (v
), &xd
) == 0)
4419 winsize_assignment
= 1;
4420 d
= xd
; /* truncate */
4421 if (name
[0] == 'L') /* LINES */
4422 rl_set_screen_size (d
, -1);
4424 rl_set_screen_size (-1, d
);
4425 winsize_assignment
= 0;
4428 #endif /* STRICT_POSIX */
4429 #endif /* READLINE */
4431 /* Update the value of HOME in the export environment so tilde expansion will
4433 #if defined (__CYGWIN__)
4437 array_needs_making
= 1;
4438 maybe_make_export_env ();
4442 #if defined (HISTORY)
4443 /* What to do after the HISTSIZE or HISTFILESIZE variables change.
4444 If there is a value for this HISTSIZE (and it is numeric), then stifle
4445 the history. Otherwise, if there is NO value for this variable,
4446 unstifle the history. If name is HISTFILESIZE, and its value is
4447 numeric, truncate the history file to hold no more than that many
4457 temp
= get_string_value (name
);
4461 if (legal_number (temp
, &num
))
4466 stifle_history (hmax
);
4467 hmax
= where_history ();
4468 if (history_lines_this_session
> hmax
)
4469 history_lines_this_session
= hmax
;
4473 history_truncate_file (get_string_value ("HISTFILE"), hmax
);
4474 if (hmax
<= history_lines_in_file
)
4475 history_lines_in_file
= hmax
;
4479 else if (name
[4] == 'S')
4480 unstifle_history ();
4483 /* What to do after the HISTIGNORE variable changes. */
4485 sv_histignore (name
)
4488 setup_history_ignore (name
);
4491 /* What to do after the HISTCONTROL variable changes. */
4493 sv_history_control (name
)
4500 history_control
= 0;
4501 temp
= get_string_value (name
);
4503 if (temp
== 0 || *temp
== 0)
4507 while (val
= extract_colon_unit (temp
, &tptr
))
4509 if (STREQ (val
, "ignorespace"))
4510 history_control
|= HC_IGNSPACE
;
4511 else if (STREQ (val
, "ignoredups"))
4512 history_control
|= HC_IGNDUPS
;
4513 else if (STREQ (val
, "ignoreboth"))
4514 history_control
|= HC_IGNBOTH
;
4515 else if (STREQ (val
, "erasedups"))
4516 history_control
|= HC_ERASEDUPS
;
4522 #if defined (BANG_HISTORY)
4523 /* Setting/unsetting of the history expansion character. */
4530 temp
= get_string_value (name
);
4533 history_expansion_char
= *temp
;
4534 if (temp
[0] && temp
[1])
4536 history_subst_char
= temp
[1];
4538 history_comment_char
= temp
[2];
4543 history_expansion_char
= '!';
4544 history_subst_char
= '^';
4545 history_comment_char
= '#';
4548 #endif /* BANG_HISTORY */
4551 sv_histtimefmt (name
)
4556 v
= find_variable (name
);
4557 history_write_timestamps
= (v
!= 0);
4559 #endif /* HISTORY */
4561 #if defined (HAVE_TZSET) && defined (PROMPT_STRING_DECODE)
4570 /* If the variable exists, then the value of it can be the number
4571 of times we actually ignore the EOF. The default is small,
4572 (smaller than csh, anyway). */
4580 eof_encountered
= 0;
4582 tmp_var
= find_variable (name
);
4583 ignoreeof
= tmp_var
!= 0;
4584 temp
= tmp_var
? value_cell (tmp_var
) : (char *)NULL
;
4586 eof_encountered_limit
= (*temp
&& all_digits (temp
)) ? atoi (temp
) : 10;
4587 set_shellopts (); /* make sure `ignoreeof' is/is not in $SHELLOPTS */
4597 tt
= get_string_value ("OPTIND");
4602 /* According to POSIX, setting OPTIND=1 resets the internal state
4604 if (s
< 0 || s
== 1)
4618 tt
= get_string_value ("OPTERR");
4619 sh_opterr
= (tt
&& *tt
) ? atoi (tt
) : 1;
4623 sv_strict_posix (name
)
4626 SET_INT_VAR (name
, posixly_correct
);
4627 posix_initialize (posixly_correct
);
4628 #if defined (READLINE)
4629 if (interactive_shell
)
4630 posix_readline_initialize (posixly_correct
);
4631 #endif /* READLINE */
4632 set_shellopts (); /* make sure `posix' is/is not in $SHELLOPTS */
4641 v
= get_string_value (name
);
4642 if (name
[0] == 'L' && name
[1] == 'A') /* LANG */
4645 set_locale_var (name
, v
); /* LC_*, TEXTDOMAIN* */
4648 #if defined (ARRAY_VARS)
4650 set_pipestatus_array (ps
, nproc
)
4658 char *t
, tbuf
[INT_STRLEN_BOUND(int) + 1];
4660 v
= find_variable ("PIPESTATUS");
4662 v
= make_new_array_variable ("PIPESTATUS");
4663 if (array_p (v
) == 0)
4664 return; /* Do nothing if not an array variable. */
4667 if (a
== 0 || array_num_elements (a
) == 0)
4669 for (i
= 0; i
< nproc
; i
++) /* was ps[i] != -1, not i < nproc */
4671 t
= inttostr (ps
[i
], tbuf
, sizeof (tbuf
));
4672 array_insert (a
, i
, t
);
4678 if (array_num_elements (a
) == nproc
&& nproc
== 1)
4680 ae
= element_forw (a
->head
);
4681 free (element_value (ae
));
4682 ae
->value
= itos (ps
[0]);
4684 else if (array_num_elements (a
) <= nproc
)
4686 /* modify in array_num_elements members in place, then add */
4688 for (i
= 0; i
< array_num_elements (a
); i
++)
4690 ae
= element_forw (ae
);
4691 free (element_value (ae
));
4692 ae
->value
= itos (ps
[i
]);
4695 for ( ; i
< nproc
; i
++)
4697 t
= inttostr (ps
[i
], tbuf
, sizeof (tbuf
));
4698 array_insert (a
, i
, t
);
4703 /* deleting elements. it's faster to rebuild the array. */
4705 for (i
= 0; ps
[i
] != -1; i
++)
4707 t
= inttostr (ps
[i
], tbuf
, sizeof (tbuf
));
4708 array_insert (a
, i
, t
);
4714 save_pipestatus_array ()
4719 v
= find_variable ("PIPESTATUS");
4720 if (v
== 0 || array_p (v
) == 0 || array_cell (v
) == 0)
4721 return ((ARRAY
*)NULL
);
4724 a2
= array_copy (array_cell (v
));
4730 restore_pipestatus_array (a
)
4736 v
= find_variable ("PIPESTATUS");
4737 /* XXX - should we still assign even if existing value is NULL? */
4738 if (v
== 0 || array_p (v
) == 0 || array_cell (v
) == 0)
4741 a2
= array_cell (v
);
4742 var_setarray (v
, a
);
4749 set_pipestatus_from_exit (s
)
4752 #if defined (ARRAY_VARS)
4753 static int v
[2] = { 0, -1 };
4756 set_pipestatus_array (v
, 1);
4769 v
= find_variable (name
);
4777 if (t
== 0 || *t
== 0)
4781 fd
= (int)strtol (t
, &e
, 10);
4782 if (e
!= t
&& *e
== '\0' && sh_validfd (fd
))
4784 fp
= fdopen (fd
, "w");
4786 internal_error (_("%s: %s: cannot open as FILE"), name
, value_cell (v
));
4788 xtrace_set (fd
, fp
);
4791 internal_error (_("%s: %s: invalid value for trace file descriptor"), name
, value_cell (v
));