1 /* variables.c -- Functions for hacking shell variables. */
3 /* Copyright (C) 1987-2004 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 it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bash is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #include "bashtypes.h"
24 #include "posixstat.h"
25 #include "posixtime.h"
31 #if defined (HAVE_UNISTD_H)
36 #include "chartypes.h"
43 #include "execute_cmd.h"
45 #include "mailcheck.h"
50 #include "builtins/getopt.h"
51 #include "builtins/common.h"
53 #if defined (READLINE)
54 # include "bashline.h"
55 # include <readline/readline.h>
57 # include <tilde/tilde.h>
61 # include "bashhist.h"
62 # include <readline/history.h>
65 #if defined (PROGRAMMABLE_COMPLETION)
66 # include "pcomplete.h"
69 #define TEMPENV_HASH_BUCKETS 4 /* must be power of two */
71 #define ifsname(s) ((s)[0] == 'I' && (s)[1] == 'F' && (s)[2] == 'S' && (s)[3] == '\0')
73 /* Variables used here and defined in other files. */
74 extern int posixly_correct;
75 extern int line_number;
76 extern int subshell_environment, indirection_level, subshell_level;
77 extern int build_version, patch_level;
78 extern int expanding_redir;
79 extern char *dist_version, *release_status;
80 extern char *shell_name;
81 extern char *primary_prompt, *secondary_prompt;
82 extern char *current_host_name;
83 extern sh_builtin_func_t *this_shell_builtin;
84 extern SHELL_VAR *this_shell_function;
85 extern char *the_printed_command_except_trap;
86 extern char *this_command_name;
87 extern char *command_execution_string;
88 extern time_t shell_start_time;
90 #if defined (READLINE)
91 extern int perform_hostname_completion;
94 /* The list of shell variables that the user has created at the global
95 scope, or that came from the environment. */
96 VAR_CONTEXT *global_variables = (VAR_CONTEXT *)NULL;
98 /* The current list of shell variables, including function scopes */
99 VAR_CONTEXT *shell_variables = (VAR_CONTEXT *)NULL;
101 /* The list of shell functions that the user has created, or that came from
103 HASH_TABLE *shell_functions = (HASH_TABLE *)NULL;
105 #if defined (DEBUGGER)
106 /* The table of shell function definitions that the user defined or that
107 came from the environment. */
108 HASH_TABLE *shell_function_defs = (HASH_TABLE *)NULL;
111 /* The current variable context. This is really a count of how deep into
112 executing functions we are. */
113 int variable_context = 0;
115 /* The set of shell assignments which are made only in the environment
116 for a single command. */
117 HASH_TABLE *temporary_env = (HASH_TABLE *)NULL;
119 /* Set to non-zero if an assignment error occurs while putting variables
120 into the temporary environment. */
121 int tempenv_assign_error;
123 /* Some funky variables which are known about specially. Here is where
124 "$*", "$1", and all the cruft is kept. */
125 char *dollar_vars[10];
126 WORD_LIST *rest_of_args = (WORD_LIST *)NULL;
128 /* The value of $$. */
129 pid_t dollar_dollar_pid;
131 /* An array which is passed to commands as their environment. It is
132 manufactured from the union of the initial environment and the
133 shell variables that are marked for export. */
134 char **export_env = (char **)NULL;
135 static int export_env_index;
136 static int export_env_size;
138 /* Non-zero means that we have to remake EXPORT_ENV. */
139 int array_needs_making = 1;
141 /* The number of times BASH has been executed. This is set
142 by initialize_variables (). */
145 /* Some forward declarations. */
146 static void set_machine_vars __P((void));
147 static void set_home_var __P((void));
148 static void set_shell_var __P((void));
149 static char *get_bash_name __P((void));
150 static void initialize_shell_level __P((void));
151 static void uidset __P((void));
152 #if defined (ARRAY_VARS)
153 static void make_vers_array __P((void));
156 static SHELL_VAR *null_assign __P((SHELL_VAR *, char *, arrayind_t));
157 #if defined (ARRAY_VARS)
158 static SHELL_VAR *null_array_assign __P((SHELL_VAR *, char *, arrayind_t));
160 static SHELL_VAR *get_self __P((SHELL_VAR *));
162 #if defined (ARRAY_VARS)
163 static SHELL_VAR *init_dynamic_array_var __P((char *, sh_var_value_func_t *, sh_var_assign_func_t *, int));
166 static SHELL_VAR *assign_seconds __P((SHELL_VAR *, char *, arrayind_t));
167 static SHELL_VAR *get_seconds __P((SHELL_VAR *));
168 static SHELL_VAR *init_seconds_var __P((void));
170 static int brand __P((void));
171 static void sbrand __P((unsigned long)); /* set bash random number generator. */
172 static SHELL_VAR *assign_random __P((SHELL_VAR *, char *, arrayind_t));
173 static SHELL_VAR *get_random __P((SHELL_VAR *));
175 static SHELL_VAR *assign_lineno __P((SHELL_VAR *, char *, arrayind_t));
176 static SHELL_VAR *get_lineno __P((SHELL_VAR *));
178 static SHELL_VAR *assign_subshell __P((SHELL_VAR *, char *, arrayind_t));
179 static SHELL_VAR *get_subshell __P((SHELL_VAR *));
181 #if defined (HISTORY)
182 static SHELL_VAR *get_histcmd __P((SHELL_VAR *));
185 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
186 static SHELL_VAR *assign_dirstack __P((SHELL_VAR *, char *, arrayind_t));
187 static SHELL_VAR *get_dirstack __P((SHELL_VAR *));
190 #if defined (ARRAY_VARS)
191 static SHELL_VAR *get_groupset __P((SHELL_VAR *));
194 static SHELL_VAR *get_funcname __P((SHELL_VAR *));
195 static SHELL_VAR *init_funcname_var __P((void));
197 static void initialize_dynamic_variables __P((void));
199 static SHELL_VAR *hash_lookup __P((const char *, HASH_TABLE *));
200 static SHELL_VAR *new_shell_variable __P((const char *));
201 static SHELL_VAR *make_new_variable __P((const char *, HASH_TABLE *));
202 static SHELL_VAR *bind_variable_internal __P((const char *, char *, HASH_TABLE *, int));
204 static void free_variable_hash_data __P((PTR_T));
206 static VARLIST *vlist_alloc __P((int));
207 static VARLIST *vlist_realloc __P((VARLIST *, int));
208 static void vlist_add __P((VARLIST *, SHELL_VAR *, int));
210 static void flatten __P((HASH_TABLE *, sh_var_map_func_t *, VARLIST *, int));
212 static int qsort_var_comp __P((SHELL_VAR **, SHELL_VAR **));
214 static SHELL_VAR **vapply __P((sh_var_map_func_t *));
215 static SHELL_VAR **fapply __P((sh_var_map_func_t *));
217 static int visible_var __P((SHELL_VAR *));
218 static int visible_and_exported __P((SHELL_VAR *));
219 static int local_and_exported __P((SHELL_VAR *));
220 static int variable_in_context __P((SHELL_VAR *));
221 #if defined (ARRAY_VARS)
222 static int visible_array_vars __P((SHELL_VAR *));
225 static SHELL_VAR *bind_tempenv_variable __P((const char *, char *));
226 static void push_temp_var __P((PTR_T));
227 static void propagate_temp_var __P((PTR_T));
228 static void dispose_temporary_env __P((sh_free_func_t *));
230 static inline char *mk_env_string __P((const char *, const char *));
231 static char **make_env_array_from_var_list __P((SHELL_VAR **));
232 static char **make_var_export_array __P((VAR_CONTEXT *));
233 static char **make_func_export_array __P((void));
234 static void add_temp_array_to_env __P((char **, int, int));
236 static int n_shell_variables __P((void));
237 static int set_context __P((SHELL_VAR *));
239 static void push_func_var __P((PTR_T));
240 static void push_exported_var __P((PTR_T));
242 static inline int find_special_var __P((const char *));
244 /* Initialize the shell variables from the current environment.
245 If PRIVMODE is nonzero, don't import functions from ENV or
248 initialize_shell_variables (env, privmode)
252 char *name, *string, *temp_string;
253 int c, char_index, string_index, string_length;
256 if (shell_variables == 0)
258 shell_variables = global_variables = new_var_context ((char *)NULL, 0);
259 shell_variables->scope = 0;
260 shell_variables->table = hash_create (0);
263 if (shell_functions == 0)
264 shell_functions = hash_create (0);
266 #if defined (DEBUGGER)
267 if (shell_function_defs == 0)
268 shell_function_defs = hash_create (0);
271 for (string_index = 0; string = env[string_index++]; )
275 while ((c = *string++) && c != '=')
277 if (string[-1] == '=')
278 char_index = string - name - 1;
280 /* If there are weird things in the environment, like `=xxx' or a
281 string without an `=', just skip them. */
285 /* ASSERT(name[char_index] == '=') */
286 name[char_index] = '\0';
287 /* Now, name = env variable name, string = env variable value, and
288 char_index == strlen (name) */
290 /* If exported function, define it now. Don't import functions from
291 the environment in privileged mode. */
292 if (privmode == 0 && read_but_dont_execute == 0 && STREQN ("() {", string, 4))
294 string_length = strlen (string);
295 temp_string = (char *)xmalloc (3 + string_length + char_index);
297 strcpy (temp_string, name);
298 temp_string[char_index] = ' ';
299 strcpy (temp_string + char_index + 1, string);
301 parse_and_execute (temp_string, name, SEVAL_NONINT|SEVAL_NOHIST);
303 /* Ancient backwards compatibility. Old versions of bash exported
304 functions like name()=() {...} */
305 if (name[char_index - 1] == ')' && name[char_index - 2] == '(')
306 name[char_index - 2] = '\0';
308 if (temp_var = find_function (name))
310 VSETATTR (temp_var, (att_exported|att_imported));
311 array_needs_making = 1;
314 report_error (_("error importing function definition for `%s'"), name);
317 if (name[char_index - 1] == ')' && name[char_index - 2] == '\0')
318 name[char_index - 2] = '('; /* ) */
320 #if defined (ARRAY_VARS)
322 /* Array variables may not yet be exported. */
323 else if (*string == '(' && string[1] == '[' && xstrchr (string, ')'))
326 temp_string = extract_array_assignment_list (string, &string_length);
327 temp_var = assign_array_from_string (name, temp_string);
329 VSETATTR (temp_var, (att_exported | att_imported));
330 array_needs_making = 1;
336 temp_var = bind_variable (name, string);
337 VSETATTR (temp_var, (att_exported | att_imported));
338 array_needs_making = 1;
341 name[char_index] = '=';
342 /* temp_var can be NULL if it was an exported function with a syntax
343 error (a different bug, but it still shouldn't dump core). */
344 if (temp_var && function_p (temp_var) == 0) /* XXX not yet */
346 CACHE_IMPORTSTR (temp_var, name);
352 /* Set up initial value of $_ */
353 temp_var = bind_variable ("_", dollar_vars[0]);
355 /* Remember this pid. */
356 dollar_dollar_pid = getpid ();
358 /* Now make our own defaults in case the vars that we think are
359 important are missing. */
360 temp_var = set_if_not ("PATH", DEFAULT_PATH_VALUE);
362 set_auto_export (temp_var); /* XXX */
365 temp_var = set_if_not ("TERM", "dumb");
367 set_auto_export (temp_var); /* XXX */
371 /* set node id -- don't import it from the environment */
374 qnx_nidtostr (getnid (), node_name, sizeof (node_name));
375 temp_var = bind_variable ("NODE", node_name);
376 set_auto_export (temp_var);
380 /* set up the prompts. */
381 if (interactive_shell)
383 #if defined (PROMPT_STRING_DECODE)
384 set_if_not ("PS1", primary_prompt);
386 if (current_user.uid == -1)
387 get_current_user_info ();
388 set_if_not ("PS1", current_user.euid == 0 ? "# " : primary_prompt);
390 set_if_not ("PS2", secondary_prompt);
392 set_if_not ("PS4", "+ ");
394 /* Don't allow IFS to be imported from the environment. */
395 temp_var = bind_variable ("IFS", " \t\n");
398 /* Magic machine types. Pretty convenient. */
401 /* Default MAILCHECK for interactive shells. Defer the creation of a
402 default MAILPATH until the startup files are read, because MAIL
403 names a mail file if MAILPATH is not set, and we should provide a
404 default only if neither is set. */
405 if (interactive_shell)
406 set_if_not ("MAILCHECK", posixly_correct ? "600" : "60");
408 /* Do some things with shell level. */
409 initialize_shell_level ();
413 /* Initialize the `getopts' stuff. */
414 bind_variable ("OPTIND", "1");
416 bind_variable ("OPTERR", "1");
419 if (login_shell == 1)
422 /* Get the full pathname to THIS shell, and set the BASH variable
424 name = get_bash_name ();
425 temp_var = bind_variable ("BASH", name);
428 /* Make the exported environment variable SHELL be the user's login
429 shell. Note that the `tset' command looks at this variable
430 to determine what style of commands to output; if it ends in "csh",
431 then C-shell commands are output, else Bourne shell commands. */
434 /* Make a variable called BASH_VERSION which contains the version info. */
435 bind_variable ("BASH_VERSION", shell_version_string ());
436 #if defined (ARRAY_VARS)
440 if (command_execution_string)
441 bind_variable ("BASH_EXECUTION_STRING", command_execution_string);
443 /* Find out if we're supposed to be in Posix.2 mode via an
444 environment variable. */
445 temp_var = find_variable ("POSIXLY_CORRECT");
447 temp_var = find_variable ("POSIX_PEDANTIC");
448 if (temp_var && imported_p (temp_var))
449 sv_strict_posix (temp_var->name);
451 #if defined (HISTORY)
452 /* Set history variables to defaults, and then do whatever we would
453 do if the variable had just been set. Do this only in the case
454 that we are remembering commands on the history list. */
455 if (remember_on_history)
457 name = bash_tilde_expand (posixly_correct ? "~/.sh_history" : "~/.bash_history", 0);
459 set_if_not ("HISTFILE", name);
462 set_if_not ("HISTSIZE", "500");
463 sv_histsize ("HISTSIZE");
467 /* Seed the random number generator. */
468 sbrand (dollar_dollar_pid + shell_start_time);
470 /* Handle some "special" variables that we may have inherited from a
472 if (interactive_shell)
474 temp_var = find_variable ("IGNOREEOF");
476 temp_var = find_variable ("ignoreeof");
477 if (temp_var && imported_p (temp_var))
478 sv_ignoreeof (temp_var->name);
481 #if defined (HISTORY)
482 if (interactive_shell && remember_on_history)
484 sv_history_control ("HISTCONTROL");
485 sv_histignore ("HISTIGNORE");
492 * I'm tired of the arguing and bug reports. Bash now leaves SSH_CLIENT
493 * and SSH2_CLIENT alone. I'm going to rely on the shell_level check in
494 * isnetconn() to avoid running the startup files more often than wanted.
495 * That will, of course, only work if the user's login shell is bash, so
496 * I've made that behavior conditional on SSH_SOURCE_BASHRC being defined
500 temp_var = find_variable ("SSH_CLIENT");
501 if (temp_var && imported_p (temp_var))
503 VUNSETATTR (temp_var, att_exported);
504 array_needs_making = 1;
506 temp_var = find_variable ("SSH2_CLIENT");
507 if (temp_var && imported_p (temp_var))
509 VUNSETATTR (temp_var, att_exported);
510 array_needs_making = 1;
514 /* Get the user's real and effective user ids. */
517 /* Initialize the dynamic variables, and seed their values. */
518 initialize_dynamic_variables ();
521 /* **************************************************************** */
523 /* Setting values for special shell variables */
525 /* **************************************************************** */
532 temp_var = set_if_not ("HOSTTYPE", HOSTTYPE);
533 temp_var = set_if_not ("OSTYPE", OSTYPE);
534 temp_var = set_if_not ("MACHTYPE", MACHTYPE);
536 temp_var = set_if_not ("HOSTNAME", current_host_name);
539 /* Set $HOME to the information in the password file if we didn't get
540 it from the environment. */
542 /* This function is not static so the tilde and readline libraries can
547 if (current_user.home_dir == 0)
548 get_current_user_info ();
549 return current_user.home_dir;
557 temp_var = find_variable ("HOME");
559 temp_var = bind_variable ("HOME", sh_get_home_dir ());
561 VSETATTR (temp_var, att_exported);
565 /* Set $SHELL to the user's login shell if it is not already set. Call
566 get_current_user_info if we haven't already fetched the shell. */
572 temp_var = find_variable ("SHELL");
575 if (current_user.shell == 0)
576 get_current_user_info ();
577 temp_var = bind_variable ("SHELL", current_user.shell);
580 VSETATTR (temp_var, att_exported);
589 if ((login_shell == 1) && RELPATH(shell_name))
591 if (current_user.shell == 0)
592 get_current_user_info ();
593 name = savestring (current_user.shell);
595 else if (ABSPATH(shell_name))
596 name = savestring (shell_name);
597 else if (shell_name[0] == '.' && shell_name[1] == '/')
599 /* Fast path for common case. */
603 cdir = get_string_value ("PWD");
607 name = (char *)xmalloc (len + strlen (shell_name) + 1);
609 strcpy (name + len, shell_name + 1);
612 name = savestring (shell_name);
619 tname = find_user_command (shell_name);
623 /* Try the current directory. If there is not an executable
624 there, just punt and use the login shell. */
625 s = file_status (shell_name);
628 tname = make_absolute (shell_name, get_string_value ("PWD"));
629 if (*shell_name == '.')
631 name = sh_canonpath (tname, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
642 if (current_user.shell == 0)
643 get_current_user_info ();
644 name = savestring (current_user.shell);
649 name = full_pathname (tname);
658 adjust_shell_level (change)
661 char new_level[5], *old_SHLVL;
665 old_SHLVL = get_string_value ("SHLVL");
666 if (old_SHLVL == 0 || *old_SHLVL == '\0' || legal_number (old_SHLVL, &old_level) == 0)
669 shell_level = old_level + change;
672 else if (shell_level > 1000)
674 internal_warning (_("shell level (%d) too high, resetting to 1"), shell_level);
678 /* We don't need the full generality of itos here. */
679 if (shell_level < 10)
681 new_level[0] = shell_level + '0';
684 else if (shell_level < 100)
686 new_level[0] = (shell_level / 10) + '0';
687 new_level[1] = (shell_level % 10) + '0';
690 else if (shell_level < 1000)
692 new_level[0] = (shell_level / 100) + '0';
693 old_level = shell_level % 100;
694 new_level[1] = (old_level / 10) + '0';
695 new_level[2] = (old_level % 10) + '0';
699 temp_var = bind_variable ("SHLVL", new_level);
700 set_auto_export (temp_var);
704 initialize_shell_level ()
706 adjust_shell_level (1);
709 /* If we got PWD from the environment, update our idea of the current
710 working directory. In any case, make sure that PWD exists before
711 checking it. It is possible for getcwd () to fail on shell startup,
712 and in that case, PWD would be undefined. If this is an interactive
713 login shell, see if $HOME is the current working directory, and if
714 that's not the same string as $PWD, set PWD=$HOME. */
719 SHELL_VAR *temp_var, *home_var;
720 char *temp_string, *home_string;
722 home_var = find_variable ("HOME");
723 home_string = home_var ? value_cell (home_var) : (char *)NULL;
725 temp_var = find_variable ("PWD");
726 if (temp_var && imported_p (temp_var) &&
727 (temp_string = value_cell (temp_var)) &&
728 same_file (temp_string, ".", (struct stat *)NULL, (struct stat *)NULL))
729 set_working_directory (temp_string);
730 else if (home_string && interactive_shell && login_shell &&
731 same_file (home_string, ".", (struct stat *)NULL, (struct stat *)NULL))
733 set_working_directory (home_string);
734 temp_var = bind_variable ("PWD", home_string);
735 set_auto_export (temp_var);
739 temp_string = get_working_directory ("shell-init");
742 temp_var = bind_variable ("PWD", temp_string);
743 set_auto_export (temp_var);
748 /* According to the Single Unix Specification, v2, $OLDPWD is an
749 `environment variable' and therefore should be auto-exported.
750 Make a dummy invisible variable for OLDPWD, and mark it as exported. */
751 temp_var = bind_variable ("OLDPWD", (char *)NULL);
752 VSETATTR (temp_var, (att_exported | att_invisible));
755 /* Make a variable $PPID, which holds the pid of the shell's parent. */
759 char namebuf[INT_STRLEN_BOUND(pid_t) + 1], *name;
762 name = inttostr (getppid (), namebuf, sizeof(namebuf));
763 temp_var = find_variable ("PPID");
765 VUNSETATTR (temp_var, (att_readonly | att_exported));
766 temp_var = bind_variable ("PPID", name);
767 VSETATTR (temp_var, (att_readonly | att_integer));
773 char buff[INT_STRLEN_BOUND(uid_t) + 1], *b;
774 register SHELL_VAR *v;
776 b = inttostr (current_user.uid, buff, sizeof (buff));
777 v = find_variable ("UID");
780 v = bind_variable ("UID", b);
781 VSETATTR (v, (att_readonly | att_integer));
784 if (current_user.euid != current_user.uid)
785 b = inttostr (current_user.euid, buff, sizeof (buff));
787 v = find_variable ("EUID");
790 v = bind_variable ("EUID", b);
791 VSETATTR (v, (att_readonly | att_integer));
795 #if defined (ARRAY_VARS)
801 char *s, d[32], b[INT_STRLEN_BOUND(int) + 1];
803 unbind_variable ("BASH_VERSINFO");
805 vv = make_new_array_variable ("BASH_VERSINFO");
806 av = array_cell (vv);
807 strcpy (d, dist_version);
808 s = xstrchr (d, '.');
811 array_insert (av, 0, d);
812 array_insert (av, 1, s);
813 s = inttostr (patch_level, b, sizeof (b));
814 array_insert (av, 2, s);
815 s = inttostr (build_version, b, sizeof (b));
816 array_insert (av, 3, s);
817 array_insert (av, 4, release_status);
818 array_insert (av, 5, MACHTYPE);
820 VSETATTR (vv, att_readonly);
822 #endif /* ARRAY_VARS */
824 /* Set the environment variables $LINES and $COLUMNS in response to
825 a window size change. */
827 sh_set_lines_and_columns (lines, cols)
830 char val[INT_STRLEN_BOUND(int) + 1], *v;
832 v = inttostr (lines, val, sizeof (val));
833 bind_variable ("LINES", v);
835 v = inttostr (cols, val, sizeof (val));
836 bind_variable ("COLUMNS", v);
839 /* **************************************************************** */
841 /* Printing variables and values */
843 /* **************************************************************** */
845 /* Print LIST (a list of shell variables) to stdout in such a way that
846 they can be read back in. */
848 print_var_list (list)
849 register SHELL_VAR **list;
852 register SHELL_VAR *var;
854 for (i = 0; list && (var = list[i]); i++)
855 if (invisible_p (var) == 0)
856 print_assignment (var);
859 /* Print LIST (a list of shell functions) to stdout in such a way that
860 they can be read back in. */
862 print_func_list (list)
863 register SHELL_VAR **list;
866 register SHELL_VAR *var;
868 for (i = 0; list && (var = list[i]); i++)
870 printf ("%s ", var->name);
871 print_var_function (var);
876 /* Print the value of a single SHELL_VAR. No newline is
877 output, but the variable is printed in such a way that
878 it can be read back in. */
880 print_assignment (var)
883 if (var_isset (var) == 0)
886 if (function_p (var))
888 printf ("%s", var->name);
889 print_var_function (var);
892 #if defined (ARRAY_VARS)
893 else if (array_p (var))
894 print_array_assignment (var, 0);
895 #endif /* ARRAY_VARS */
898 printf ("%s=", var->name);
899 print_var_value (var, 1);
904 /* Print the value cell of VAR, a shell variable. Do not print
905 the name, nor leading/trailing newline. If QUOTE is non-zero,
906 and the value contains shell metacharacters, quote the value
907 in such a way that it can be read back in. */
909 print_var_value (var, quote)
915 if (var_isset (var) == 0)
918 if (quote && posixly_correct == 0 && ansic_shouldquote (value_cell (var)))
920 t = ansic_quote (value_cell (var), 0, (int *)0);
924 else if (quote && sh_contains_shell_metas (value_cell (var)))
926 t = sh_single_quote (value_cell (var));
931 printf ("%s", value_cell (var));
934 /* Print the function cell of VAR, a shell variable. Do not
935 print the name, nor leading/trailing newline. */
937 print_var_function (var)
940 if (function_p (var) && var_isset (var))
941 printf ("%s", named_function_string ((char *)NULL, function_cell(var), 1));
944 /* **************************************************************** */
946 /* Dynamic Variables */
948 /* **************************************************************** */
952 These are variables whose values are generated anew each time they are
953 referenced. These are implemented using a pair of function pointers
954 in the struct variable: assign_func, which is called from bind_variable
955 and, if arrays are compiled into the shell, some of the functions in
956 arrayfunc.c, and dynamic_value, which is called from find_variable.
958 assign_func is called from bind_variable_internal, if
959 bind_variable_internal discovers that the variable being assigned to
960 has such a function. The function is called as
961 SHELL_VAR *temp = (*(entry->assign_func)) (entry, value, ind)
962 and the (SHELL_VAR *)temp is returned as the value of bind_variable. It
963 is usually ENTRY (self). IND is an index for an array variable, and
966 dynamic_value is called from find_variable_internal to return a `new'
967 value for the specified dynamic varible. If this function is NULL,
968 the variable is treated as a `normal' shell variable. If it is not,
969 however, then this function is called like this:
970 tempvar = (*(var->dynamic_value)) (var);
972 Sometimes `tempvar' will replace the value of `var'. Other times, the
973 shell will simply use the string value. Pretty object-oriented, huh?
975 Be warned, though: if you `unset' a special variable, it loses its
976 special meaning, even if you subsequently set it.
978 The special assignment code would probably have been better put in
979 subst.c: do_assignment_internal, in the same style as
980 stupidly_hack_special_variables, but I wanted the changes as
981 localized as possible. */
983 #define INIT_DYNAMIC_VAR(var, val, gfunc, afunc) \
986 v = bind_variable (var, (val)); \
987 v->dynamic_value = gfunc; \
988 v->assign_func = afunc; \
992 #define INIT_DYNAMIC_ARRAY_VAR(var, gfunc, afunc) \
995 v = make_new_array_variable (var); \
996 v->dynamic_value = gfunc; \
997 v->assign_func = afunc; \
1002 null_assign (self, value, unused)
1010 #if defined (ARRAY_VARS)
1012 null_array_assign (self, value, ind)
1021 /* Degenerate `dynamic_value' function; just returns what's passed without
1030 #if defined (ARRAY_VARS)
1031 /* A generic dynamic array variable initializer. Intialize array variable
1032 NAME with dynamic value function GETFUNC and assignment function SETFUNC. */
1034 init_dynamic_array_var (name, getfunc, setfunc, attrs)
1036 sh_var_value_func_t *getfunc;
1037 sh_var_assign_func_t *setfunc;
1042 v = find_variable (name);
1045 INIT_DYNAMIC_ARRAY_VAR (name, getfunc, setfunc);
1047 VSETATTR (v, attrs);
1053 /* The value of $SECONDS. This is the number of seconds since shell
1054 invocation, or, the number of seconds since the last assignment + the
1055 value of the last assignment. */
1056 static intmax_t seconds_value_assigned;
1059 assign_seconds (self, value, unused)
1064 if (legal_number (value, &seconds_value_assigned) == 0)
1065 seconds_value_assigned = 0;
1066 shell_start_time = NOW;
1074 time_t time_since_start;
1077 time_since_start = NOW - shell_start_time;
1078 p = itos(seconds_value_assigned + time_since_start);
1080 FREE (value_cell (var));
1082 VSETATTR (var, att_integer);
1083 var_setvalue (var, p);
1092 v = find_variable ("SECONDS");
1095 if (legal_number (value_cell(v), &seconds_value_assigned) == 0)
1096 seconds_value_assigned = 0;
1098 INIT_DYNAMIC_VAR ("SECONDS", (v ? value_cell (v) : (char *)NULL), get_seconds, assign_seconds);
1102 /* The random number seed. You can change this by setting RANDOM. */
1103 static unsigned long rseed = 1;
1104 static int last_random_value;
1106 /* A linear congruential random number generator based on the example
1107 one in the ANSI C standard. This one isn't very good, but a more
1108 complicated one is overkill. */
1110 /* Returns a pseudo-random number between 0 and 32767. */
1114 rseed = rseed * 1103515245 + 12345;
1115 return ((unsigned int)((rseed >> 16) & 32767)); /* was % 32768 */
1118 /* Set the random number generator seed to SEED. */
1124 last_random_value = 0;
1128 assign_random (self, value, unused)
1133 sbrand (strtoul (value, (char **)NULL, 10));
1138 get_random_number ()
1142 /* Reset for command and process substitution. */
1143 if (subshell_environment)
1144 sbrand (rseed + getpid() + NOW);
1148 while (rv == last_random_value);
1159 rv = get_random_number ();
1160 last_random_value = rv;
1163 FREE (value_cell (var));
1165 VSETATTR (var, att_integer);
1166 var_setvalue (var, p);
1171 assign_lineno (var, value, unused)
1178 if (value == 0 || *value == '\0' || legal_number (value, &new_value) == 0)
1180 line_number = new_value;
1184 /* Function which returns the current line number. */
1192 ln = executing_line_number ();
1194 FREE (value_cell (var));
1195 var_setvalue (var, p);
1200 assign_subshell (var, value, unused)
1207 if (value == 0 || *value == '\0' || legal_number (value, &new_value) == 0)
1209 subshell_level = new_value;
1219 p = itos (subshell_level);
1220 FREE (value_cell (var));
1221 var_setvalue (var, p);
1226 get_bash_command (var)
1231 p = savestring (the_printed_command_except_trap);
1232 FREE (value_cell (var));
1233 var_setvalue (var, p);
1237 #if defined (HISTORY)
1244 p = itos (history_number ());
1245 FREE (value_cell (var));
1246 var_setvalue (var, p);
1251 #if defined (READLINE)
1252 /* When this function returns, VAR->value points to malloced memory. */
1254 get_comp_wordbreaks (var)
1259 /* If we don't have anything yet, assign a default value. */
1260 if (rl_completer_word_break_characters == 0 && bash_readline_initialized == 0)
1261 enable_hostname_completion (perform_hostname_completion);
1264 FREE (value_cell (var));
1265 p = savestring (rl_completer_word_break_characters);
1267 var_setvalue (var, p);
1269 var_setvalue (var, rl_completer_word_break_characters);
1275 /* When this function returns, rl_completer_word_break_characters points to
1278 assign_comp_wordbreaks (self, value, unused)
1283 if (rl_completer_word_break_characters &&
1284 rl_completer_word_break_characters != rl_basic_word_break_characters)
1285 free (rl_completer_word_break_characters);
1287 rl_completer_word_break_characters = savestring (value);
1290 #endif /* READLINE */
1292 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
1294 assign_dirstack (self, value, ind)
1299 set_dirstack_element (ind, 1, value);
1310 l = get_directory_stack ();
1311 a = array_from_word_list (l);
1312 array_dispose (array_cell (self));
1314 var_setarray (self, a);
1317 #endif /* PUSHD AND POPD && ARRAY_VARS */
1319 #if defined (ARRAY_VARS)
1320 /* We don't want to initialize the group set with a call to getgroups()
1321 unless we're asked to, but we only want to do it once. */
1329 static char **group_set = (char **)NULL;
1333 group_set = get_group_list (&ng);
1334 a = array_cell (self);
1335 for (i = 0; i < ng; i++)
1336 array_insert (a, i, group_set[i]);
1340 #endif /* ARRAY_VARS */
1342 /* If ARRAY_VARS is not defined, this just returns the name of any
1343 currently-executing function. If we have arrays, it's a call stack. */
1348 #if ! defined (ARRAY_VARS)
1350 if (variable_context && this_shell_function)
1352 FREE (value_cell (self));
1353 t = savestring (this_shell_function->name);
1354 var_setvalue (self, t);
1361 make_funcname_visible (on_or_off)
1366 v = find_variable ("FUNCNAME");
1367 if (v == 0 || v->dynamic_value == 0)
1371 VUNSETATTR (v, att_invisible);
1373 VSETATTR (v, att_invisible);
1377 init_funcname_var ()
1381 v = find_variable ("FUNCNAME");
1384 #if defined (ARRAY_VARS)
1385 INIT_DYNAMIC_ARRAY_VAR ("FUNCNAME", get_funcname, null_array_assign);
1387 INIT_DYNAMIC_VAR ("FUNCNAME", (char *)NULL, get_funcname, null_assign);
1389 VSETATTR (v, att_invisible|att_noassign);
1394 initialize_dynamic_variables ()
1398 v = init_seconds_var ();
1400 INIT_DYNAMIC_VAR ("BASH_COMMAND", (char *)NULL, get_bash_command, (sh_var_assign_func_t *)NULL);
1401 INIT_DYNAMIC_VAR ("BASH_SUBSHELL", (char *)NULL, get_subshell, assign_subshell);
1403 INIT_DYNAMIC_VAR ("RANDOM", (char *)NULL, get_random, assign_random);
1404 INIT_DYNAMIC_VAR ("LINENO", (char *)NULL, get_lineno, assign_lineno);
1406 #if defined (HISTORY)
1407 INIT_DYNAMIC_VAR ("HISTCMD", (char *)NULL, get_histcmd, (sh_var_assign_func_t *)NULL);
1410 #if defined (READLINE)
1411 INIT_DYNAMIC_VAR ("COMP_WORDBREAKS", (char *)NULL, get_comp_wordbreaks, assign_comp_wordbreaks);
1414 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
1415 v = init_dynamic_array_var ("DIRSTACK", get_dirstack, assign_dirstack, 0);
1416 #endif /* PUSHD_AND_POPD && ARRAY_VARS */
1418 #if defined (ARRAY_VARS)
1419 v = init_dynamic_array_var ("GROUPS", get_groupset, null_array_assign, att_noassign);
1421 # if defined (DEBUGGER)
1422 v = init_dynamic_array_var ("BASH_ARGC", get_self, null_array_assign, (att_invisible|att_noassign));
1423 v = init_dynamic_array_var ("BASH_ARGV", get_self, null_array_assign, (att_invisible|att_noassign));
1424 # endif /* DEBUGGER */
1425 v = init_dynamic_array_var ("BASH_SOURCE", get_self, null_array_assign, (att_invisible|att_noassign));
1426 v = init_dynamic_array_var ("BASH_LINENO", get_self, null_array_assign, (att_invisible|att_noassign));
1429 v = init_funcname_var ();
1432 /* **************************************************************** */
1434 /* Retrieving variables and values */
1436 /* **************************************************************** */
1438 /* How to get a pointer to the shell variable or function named NAME.
1439 HASHED_VARS is a pointer to the hash table containing the list
1440 of interest (either variables or functions). */
1443 hash_lookup (name, hashed_vars)
1445 HASH_TABLE *hashed_vars;
1447 BUCKET_CONTENTS *bucket;
1449 bucket = hash_search (name, hashed_vars, 0);
1450 return (bucket ? (SHELL_VAR *)bucket->data : (SHELL_VAR *)NULL);
1454 var_lookup (name, vcontext)
1456 VAR_CONTEXT *vcontext;
1461 v = (SHELL_VAR *)NULL;
1462 for (vc = vcontext; vc; vc = vc->down)
1463 if (v = hash_lookup (name, vc->table))
1469 /* Look up the variable entry named NAME. If SEARCH_TEMPENV is non-zero,
1470 then also search the temporarily built list of exported variables.
1471 The lookup order is:
1473 shell_variables list
1477 find_variable_internal (name, force_tempenv)
1484 var = (SHELL_VAR *)NULL;
1486 /* If explicitly requested, first look in the temporary environment for
1487 the variable. This allows constructs such as "foo=x eval 'echo $foo'"
1488 to get the `exported' value of $foo. This happens if we are executing
1489 a function or builtin, or if we are looking up a variable in a
1490 "subshell environment". */
1491 search_tempenv = force_tempenv || (expanding_redir == 0 && subshell_environment);
1493 if (search_tempenv && temporary_env)
1494 var = hash_lookup (name, temporary_env);
1497 var = var_lookup (name, shell_variables);
1500 return ((SHELL_VAR *)NULL);
1502 return (var->dynamic_value ? (*(var->dynamic_value)) (var) : var);
1505 /* Look up the variable entry named NAME. Returns the entry or NULL. */
1507 find_variable (name)
1510 return (find_variable_internal (name, (expanding_redir == 0 && this_shell_builtin != 0)));
1513 /* Look up the function entry whose name matches STRING.
1514 Returns the entry or NULL. */
1516 find_function (name)
1519 return (hash_lookup (name, shell_functions));
1522 /* Find the function definition for the shell function named NAME. Returns
1523 the entry or NULL. */
1525 find_function_def (name)
1528 return ((FUNCTION_DEF *)hash_lookup (name, shell_function_defs));
1531 /* Return the value of VAR. VAR is assumed to have been the result of a
1532 lookup without any subscript, if arrays are compiled into the shell. */
1534 get_variable_value (var)
1538 return ((char *)NULL);
1539 #if defined (ARRAY_VARS)
1540 else if (array_p (var))
1541 return (array_reference (array_cell (var), 0));
1544 return (value_cell (var));
1547 /* Return the string value of a variable. Return NULL if the variable
1548 doesn't exist. Don't cons a new string. This is a potential memory
1549 leak if the variable is found in the temporary environment. Since
1550 functions and variables have separate name spaces, returns NULL if
1551 var_name is a shell function only. */
1553 get_string_value (var_name)
1554 const char *var_name;
1558 var = find_variable (var_name);
1559 return ((var) ? get_variable_value (var) : (char *)NULL);
1562 /* This is present for use by the tilde and readline libraries. */
1564 sh_get_env_value (v)
1567 return get_string_value (v);
1570 /* **************************************************************** */
1572 /* Creating and setting variables */
1574 /* **************************************************************** */
1576 /* Set NAME to VALUE if NAME has no value. */
1578 set_if_not (name, value)
1583 v = find_variable (name);
1585 v = bind_variable_internal (name, value, global_variables->table, HASH_NOSRCH);
1589 /* Create a local variable referenced by NAME. */
1591 make_local_variable (name)
1594 SHELL_VAR *new_var, *old_var;
1599 /* local foo; local foo; is a no-op. */
1600 old_var = find_variable (name);
1601 if (old_var && local_p (old_var) && old_var->context == variable_context)
1604 was_tmpvar = old_var && tempvar_p (old_var);
1606 tmp_value = value_cell (old_var);
1608 for (vc = shell_variables; vc; vc = vc->down)
1609 if (vc_isfuncenv (vc) && vc->scope == variable_context)
1614 internal_error (_("make_local_variable: no function context at current scope"));
1615 return ((SHELL_VAR *)NULL);
1617 else if (vc->table == 0)
1618 vc->table = hash_create (TEMPENV_HASH_BUCKETS);
1620 /* Since this is called only from the local/declare/typeset code, we can
1621 call builtin_error here without worry (of course, it will also work
1622 for anything that sets this_command_name). Variables with the `noassign'
1623 attribute may not be made local. The test against old_var's context
1624 level is to disallow local copies of readonly global variables (since I
1625 believe that this could be a security hole). Readonly copies of calling
1626 function local variables are OK. */
1627 if (old_var && (noassign_p (old_var) ||
1628 (readonly_p (old_var) && old_var->context == 0)))
1630 if (readonly_p (old_var))
1632 return ((SHELL_VAR *)NULL);
1636 new_var = bind_variable_internal (name, "", vc->table, HASH_NOSRCH);
1639 new_var = make_new_variable (name, vc->table);
1641 /* If we found this variable in one of the temporary environments,
1642 inherit its value. Watch to see if this causes problems with
1643 things like `x=4 local x'. */
1645 var_setvalue (new_var, savestring (tmp_value));
1647 new_var->attributes = exported_p (old_var) ? att_exported : 0;
1650 vc->flags |= VC_HASLOCAL;
1652 new_var->context = variable_context;
1653 VSETATTR (new_var, att_local);
1661 #if defined (ARRAY_VARS)
1663 make_local_array_variable (name)
1669 var = make_local_variable (name);
1670 if (var == 0 || array_p (var))
1673 array = array_create ();
1675 FREE (value_cell(var));
1676 var_setarray (var, array);
1677 VSETATTR (var, att_array);
1680 #endif /* ARRAY_VARS */
1682 /* Create a new shell variable with name NAME. */
1684 new_shell_variable (name)
1689 entry = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
1691 entry->name = savestring (name);
1692 var_setvalue (entry, (char *)NULL);
1693 CLEAR_EXPORTSTR (entry);
1695 entry->dynamic_value = (sh_var_value_func_t *)NULL;
1696 entry->assign_func = (sh_var_assign_func_t *)NULL;
1698 entry->attributes = 0;
1700 /* Always assume variables are to be made at toplevel!
1701 make_local_variable has the responsibilty of changing the
1702 variable context. */
1708 /* Create a new shell variable with name NAME and add it to the hash table
1711 make_new_variable (name, table)
1716 BUCKET_CONTENTS *elt;
1718 entry = new_shell_variable (name);
1720 /* Make sure we have a shell_variables hash table to add to. */
1721 if (shell_variables == 0)
1723 shell_variables = global_variables = new_var_context ((char *)NULL, 0);
1724 shell_variables->scope = 0;
1725 shell_variables->table = hash_create (0);
1728 elt = hash_insert (savestring (name), table, HASH_NOSRCH);
1729 elt->data = (PTR_T)entry;
1734 #if defined (ARRAY_VARS)
1736 make_new_array_variable (name)
1742 entry = make_new_variable (name, global_variables->table);
1743 array = array_create ();
1744 var_setarray (entry, array);
1745 VSETATTR (entry, att_array);
1751 make_variable_value (var, value)
1759 /* If this variable has had its type set to integer (via `declare -i'),
1760 then do expression evaluation on it and store the result. The
1761 functions in expr.c (evalexp()) and bind_int_variable() are responsible
1762 for turning off the integer flag if they don't want further
1764 if (integer_p (var))
1766 lval = evalexp (value, &expok);
1768 jump_to_top_level (DISCARD);
1769 retval = itos (lval);
1774 retval = savestring (value);
1777 retval = (char *)xmalloc (1);
1782 retval = (char *)NULL;
1787 /* Bind a variable NAME to VALUE in the HASH_TABLE TABLE, which may be the
1788 temporary environment (but usually is not). */
1790 bind_variable_internal (name, value, table, hflags)
1799 entry = (hflags & HASH_NOSRCH) ? (SHELL_VAR *)NULL : hash_lookup (name, table);
1803 entry = make_new_variable (name, table);
1804 var_setvalue (entry, make_variable_value (entry, value));
1806 else if (entry->assign_func) /* array vars have assign functions now */
1808 INVALIDATE_EXPORTSTR (entry);
1809 return ((*(entry->assign_func)) (entry, value, -1));
1813 if (readonly_p (entry) || noassign_p (entry))
1815 if (readonly_p (entry))
1816 err_readonly (name);
1820 /* Variables which are bound are visible. */
1821 VUNSETATTR (entry, att_invisible);
1823 newval = make_variable_value (entry, value);
1825 /* Invalidate any cached export string */
1826 INVALIDATE_EXPORTSTR (entry);
1828 #if defined (ARRAY_VARS)
1829 /* XXX -- this bears looking at again -- XXX */
1830 /* If an existing array variable x is being assigned to with x=b or
1831 `read x' or something of that nature, silently convert it to
1832 x[0]=b or `read x[0]'. */
1833 if (array_p (entry))
1835 array_insert (array_cell (entry), 0, newval);
1841 FREE (value_cell (entry));
1842 var_setvalue (entry, newval);
1846 if (mark_modified_vars)
1847 VSETATTR (entry, att_exported);
1849 if (exported_p (entry))
1850 array_needs_making = 1;
1855 /* Bind a variable NAME to VALUE. This conses up the name
1856 and value strings. If we have a temporary environment, we bind there
1857 first, then we bind into shell_variables. */
1860 bind_variable (name, value)
1867 if (shell_variables == 0)
1869 shell_variables = global_variables = new_var_context ((char *)NULL, 0);
1870 shell_variables->scope = 0;
1871 shell_variables->table = hash_create (0);
1874 /* If we have a temporary environment, look there first for the variable,
1875 and, if found, modify the value there before modifying it in the
1876 shell_variables table. This allows sourced scripts to modify values
1877 given to them in a temporary environment while modifying the variable
1878 value that the caller sees. */
1880 bind_tempenv_variable (name, value);
1882 /* XXX -- handle local variables here. */
1883 for (vc = shell_variables; vc; vc = vc->down)
1885 if (vc_isfuncenv (vc) || vc_isbltnenv (vc))
1887 v = hash_lookup (name, vc->table);
1889 return (bind_variable_internal (name, value, vc->table, 0));
1892 return (bind_variable_internal (name, value, global_variables->table, 0));
1895 /* Make VAR, a simple shell variable, have value VALUE. Once assigned a
1896 value, variables are no longer invisible. This is a duplicate of part
1897 of the internals of bind_variable. If the variable is exported, or
1898 all modified variables should be exported, mark the variable for export
1899 and note that the export environment needs to be recreated. */
1901 bind_variable_value (var, value)
1907 VUNSETATTR (var, att_invisible);
1909 t = make_variable_value (var, value);
1910 FREE (value_cell (var));
1911 var_setvalue (var, t);
1913 INVALIDATE_EXPORTSTR (var);
1915 if (mark_modified_vars)
1916 VSETATTR (var, att_exported);
1918 if (exported_p (var))
1919 array_needs_making = 1;
1924 /* Bind/create a shell variable with the name LHS to the RHS.
1925 This creates or modifies a variable such that it is an integer.
1927 This used to be in expr.c, but it is here so that all of the
1928 variable binding stuff is localized. Since we don't want any
1929 recursive evaluation from bind_variable() (possible without this code,
1930 since bind_variable() calls the evaluator for variables with the integer
1931 attribute set), we temporarily turn off the integer attribute for each
1932 variable we set here, then turn it back on after binding as necessary. */
1935 bind_int_variable (lhs, rhs)
1938 register SHELL_VAR *v;
1943 #if defined (ARRAY_VARS)
1945 if (t = xstrchr (lhs, '[')) /*]*/
1947 if (valid_array_reference (lhs))
1951 v = array_variable_part (lhs, (char **)0, (int *)0);
1955 v = find_variable (lhs);
1959 isint = integer_p (v);
1960 VUNSETATTR (v, att_integer);
1963 #if defined (ARRAY_VARS)
1965 v = assign_array_element (lhs, rhs);
1968 v = bind_variable (lhs, rhs);
1971 VSETATTR (v, att_integer);
1977 bind_var_to_int (var, val)
1981 char ibuf[INT_STRLEN_BOUND (intmax_t) + 1], *p;
1983 p = fmtulong (val, 10, ibuf, sizeof (ibuf), 0);
1984 return (bind_int_variable (var, p));
1987 /* Do a function binding to a variable. You pass the name and
1988 the command to bind to. This conses the name and command. */
1990 bind_function (name, value)
1996 entry = find_function (name);
1999 BUCKET_CONTENTS *elt;
2001 elt = hash_insert (savestring (name), shell_functions, HASH_NOSRCH);
2002 entry = new_shell_variable (name);
2003 elt->data = (PTR_T)entry;
2006 INVALIDATE_EXPORTSTR (entry);
2008 if (var_isset (entry))
2009 dispose_command (function_cell (entry));
2012 var_setfunc (entry, copy_command (value));
2014 var_setfunc (entry, 0);
2016 VSETATTR (entry, att_function);
2018 if (mark_modified_vars)
2019 VSETATTR (entry, att_exported);
2021 VUNSETATTR (entry, att_invisible); /* Just to be sure */
2023 if (exported_p (entry))
2024 array_needs_making = 1;
2026 #if defined (PROGRAMMABLE_COMPLETION)
2027 set_itemlist_dirty (&it_functions);
2033 /* Bind a function definition, which includes source file and line number
2034 information in addition to the command, into the FUNCTION_DEF hash table.*/
2036 bind_function_def (name, value)
2038 FUNCTION_DEF *value;
2040 FUNCTION_DEF *entry;
2041 BUCKET_CONTENTS *elt;
2044 entry = find_function_def (name);
2047 dispose_function_def_contents (entry);
2048 entry = copy_function_def_contents (value, entry);
2052 cmd = value->command;
2054 entry = copy_function_def (value);
2055 value->command = cmd;
2057 elt = hash_insert (savestring (name), shell_function_defs, HASH_NOSRCH);
2058 elt->data = (PTR_T *)entry;
2062 /* Add STRING, which is of the form foo=bar, to the temporary environment
2063 HASH_TABLE (temporary_env). The functions in execute_cmd.c are
2064 responsible for moving the main temporary env to one of the other
2065 temporary environments. The expansion code in subst.c calls this. */
2067 assign_in_env (string)
2071 char *name, *temp, *value;
2074 offset = assignment (string, 0);
2075 name = savestring (string);
2076 value = (char *)NULL;
2078 if (name[offset] == '=')
2082 var = find_variable (name);
2083 if (var && (readonly_p (var) || noassign_p (var)))
2085 if (readonly_p (var))
2086 err_readonly (name);
2091 temp = name + offset + 1;
2092 temp = (xstrchr (temp, '~') != 0) ? bash_tilde_expand (temp, 1) : savestring (temp);
2094 value = expand_string_unsplit_to_string (temp, 0);
2098 if (temporary_env == 0)
2099 temporary_env = hash_create (TEMPENV_HASH_BUCKETS);
2101 var = hash_lookup (name, temporary_env);
2103 var = make_new_variable (name, temporary_env);
2105 FREE (value_cell (var));
2109 value = (char *)xmalloc (1); /* like do_assignment_internal */
2113 var_setvalue (var, value);
2114 var->attributes |= (att_exported|att_tempvar);
2115 var->context = variable_context; /* XXX */
2117 INVALIDATE_EXPORTSTR (var);
2118 var->exportstr = mk_env_string (name, value);
2120 array_needs_making = 1;
2125 if (echo_command_at_execute)
2126 /* The Korn shell prints the `+ ' in front of assignment statements,
2128 xtrace_print_assignment (name, value, 0, 1);
2134 /* **************************************************************** */
2136 /* Copying variables */
2138 /* **************************************************************** */
2140 #ifdef INCLUDE_UNUSED
2141 /* Copy VAR to a new data structure and return that structure. */
2146 SHELL_VAR *copy = (SHELL_VAR *)NULL;
2150 copy = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
2152 copy->attributes = var->attributes;
2153 copy->name = savestring (var->name);
2155 if (function_p (var))
2156 var_setfunc (copy, copy_command (function_cell (var)));
2157 #if defined (ARRAY_VARS)
2158 else if (array_p (var))
2159 var_setarray (copy, dup_array (array_cell (var)));
2161 else if (value_cell (var))
2162 var_setvalue (copy, savestring (value_cell (var)));
2164 var_setvalue (copy, (char *)NULL);
2166 copy->dynamic_value = var->dynamic_value;
2167 copy->assign_func = var->assign_func;
2169 copy->exportstr = COPY_EXPORTSTR (var);
2171 copy->context = var->context;
2177 /* **************************************************************** */
2179 /* Deleting and unsetting variables */
2181 /* **************************************************************** */
2183 /* Dispose of the information attached to VAR. */
2185 dispose_variable (var)
2191 if (function_p (var))
2192 dispose_command (function_cell (var));
2193 #if defined (ARRAY_VARS)
2194 else if (array_p (var))
2195 array_dispose (array_cell (var));
2198 FREE (value_cell (var));
2200 FREE_EXPORTSTR (var);
2204 if (exported_p (var))
2205 array_needs_making = 1;
2210 /* Unset the shell variable referenced by NAME. */
2212 unbind_variable (name)
2215 return makunbound (name, shell_variables);
2218 /* Unset the shell function named NAME. */
2223 BUCKET_CONTENTS *elt;
2226 elt = hash_remove (name, shell_functions, 0);
2231 #if defined (PROGRAMMABLE_COMPLETION)
2232 set_itemlist_dirty (&it_functions);
2235 func = (SHELL_VAR *)elt->data;
2238 if (exported_p (func))
2239 array_needs_making++;
2240 dispose_variable (func);
2250 unbind_function_def (name)
2253 BUCKET_CONTENTS *elt;
2254 FUNCTION_DEF *funcdef;
2256 elt = hash_remove (name, shell_function_defs, 0);
2261 funcdef = (FUNCTION_DEF *)elt->data;
2263 dispose_function_def (funcdef);
2271 /* Make the variable associated with NAME go away. HASH_LIST is the
2272 hash table from which this variable should be deleted (either
2273 shell_variables or shell_functions).
2274 Returns non-zero if the variable couldn't be found. */
2276 makunbound (name, vc)
2280 BUCKET_CONTENTS *elt, *new_elt;
2285 for (elt = (BUCKET_CONTENTS *)NULL, v = vc; v; v = v->down)
2286 if (elt = hash_remove (name, v->table, 0))
2292 old_var = (SHELL_VAR *)elt->data;
2294 if (old_var && exported_p (old_var))
2295 array_needs_making++;
2297 /* If we're unsetting a local variable and we're still executing inside
2298 the function, just mark the variable as invisible. The function
2299 eventually called by pop_var_context() will clean it up later. This
2300 must be done so that if the variable is subsequently assigned a new
2301 value inside the function, the `local' attribute is still present.
2302 We also need to add it back into the correct hash table. */
2303 if (old_var && local_p (old_var) && variable_context == old_var->context)
2305 /* Reset the attributes. Preserve the export attribute if the variable
2306 came from a temporary environment. Make sure it stays local, and
2307 make it invisible. */
2308 old_var->attributes = (exported_p (old_var) && tempvar_p (old_var)) ? att_exported : 0;
2309 VSETATTR (old_var, att_local);
2310 VSETATTR (old_var, att_invisible);
2311 FREE (value_cell (old_var));
2312 var_setvalue (old_var, (char *)NULL);
2313 INVALIDATE_EXPORTSTR (old_var);
2315 new_elt = hash_insert (savestring (old_var->name), v->table, 0);
2316 new_elt->data = (PTR_T)old_var;
2317 stupidly_hack_special_variables (old_var->name);
2324 /* Have to save a copy of name here, because it might refer to
2325 old_var->name. If so, stupidly_hack_special_variables will
2326 reference freed memory. */
2327 t = savestring (name);
2332 dispose_variable (old_var);
2333 stupidly_hack_special_variables (t);
2339 /* Get rid of all of the variables in the current context. */
2341 kill_all_local_variables ()
2345 for (vc = shell_variables; vc; vc = vc->down)
2346 if (vc_isfuncenv (vc) && vc->scope == variable_context)
2351 if (vc->table && vc_haslocals (vc))
2353 delete_all_variables (vc->table);
2354 hash_dispose (vc->table);
2356 vc->table = (HASH_TABLE *)NULL;
2360 free_variable_hash_data (data)
2365 var = (SHELL_VAR *)data;
2366 dispose_variable (var);
2369 /* Delete the entire contents of the hash table. */
2371 delete_all_variables (hashed_vars)
2372 HASH_TABLE *hashed_vars;
2374 hash_flush (hashed_vars, free_variable_hash_data);
2377 /* **************************************************************** */
2379 /* Setting variable attributes */
2381 /* **************************************************************** */
2383 #define FIND_OR_MAKE_VARIABLE(name, entry) \
2386 entry = find_variable (name); \
2389 entry = bind_variable (name, ""); \
2390 if (!no_invisible_vars) entry->attributes |= att_invisible; \
2395 /* Make the variable associated with NAME be readonly.
2396 If NAME does not exist yet, create it. */
2398 set_var_read_only (name)
2403 FIND_OR_MAKE_VARIABLE (name, entry);
2404 VSETATTR (entry, att_readonly);
2407 #ifdef INCLUDE_UNUSED
2408 /* Make the function associated with NAME be readonly.
2409 If NAME does not exist, we just punt, like auto_export code below. */
2411 set_func_read_only (name)
2416 entry = find_function (name);
2418 VSETATTR (entry, att_readonly);
2421 /* Make the variable associated with NAME be auto-exported.
2422 If NAME does not exist yet, create it. */
2424 set_var_auto_export (name)
2429 FIND_OR_MAKE_VARIABLE (name, entry);
2430 set_auto_export (entry);
2433 /* Make the function associated with NAME be auto-exported. */
2435 set_func_auto_export (name)
2440 entry = find_function (name);
2442 set_auto_export (entry);
2446 /* **************************************************************** */
2448 /* Creating lists of variables */
2450 /* **************************************************************** */
2453 vlist_alloc (nentries)
2458 vlist = (VARLIST *)xmalloc (sizeof (VARLIST));
2459 vlist->list = (SHELL_VAR **)xmalloc ((nentries + 1) * sizeof (SHELL_VAR *));
2460 vlist->list_size = nentries;
2461 vlist->list_len = 0;
2462 vlist->list[0] = (SHELL_VAR *)NULL;
2468 vlist_realloc (vlist, n)
2473 return (vlist = vlist_alloc (n));
2474 if (n > vlist->list_size)
2476 vlist->list_size = n;
2477 vlist->list = (SHELL_VAR **)xrealloc (vlist->list, (vlist->list_size + 1) * sizeof (SHELL_VAR *));
2483 vlist_add (vlist, var, flags)
2490 for (i = 0; i < vlist->list_len; i++)
2491 if (STREQ (var->name, vlist->list[i]->name))
2493 if (i < vlist->list_len)
2496 if (i >= vlist->list_size)
2497 vlist = vlist_realloc (vlist, vlist->list_size + 16);
2499 vlist->list[vlist->list_len++] = var;
2500 vlist->list[vlist->list_len] = (SHELL_VAR *)NULL;
2503 /* Map FUNCTION over the variables in VAR_HASH_TABLE. Return an array of the
2504 variables for which FUNCTION returns a non-zero value. A NULL value
2505 for FUNCTION means to use all variables. */
2507 map_over (function, vc)
2508 sh_var_map_func_t *function;
2516 for (nentries = 0, v = vc; v; v = v->down)
2517 nentries += HASH_ENTRIES (v->table);
2520 return (SHELL_VAR **)NULL;
2522 vlist = vlist_alloc (nentries);
2524 for (v = vc; v; v = v->down)
2525 flatten (v->table, function, vlist, 0);
2533 map_over_funcs (function)
2534 sh_var_map_func_t *function;
2539 if (shell_functions == 0 || HASH_ENTRIES (shell_functions) == 0)
2540 return ((SHELL_VAR **)NULL);
2542 vlist = vlist_alloc (HASH_ENTRIES (shell_functions));
2544 flatten (shell_functions, function, vlist, 0);
2551 /* Flatten VAR_HASH_TABLE, applying FUNC to each member and adding those
2552 elements for which FUNC succeeds to VLIST->list. FLAGS is reserved
2553 for future use. Only unique names are added to VLIST. If FUNC is
2554 NULL, each variable in VAR_HASH_TABLE is added to VLIST. If VLIST is
2555 NULL, FUNC is applied to each SHELL_VAR in VAR_HASH_TABLE. If VLIST
2556 and FUNC are both NULL, nothing happens. */
2558 flatten (var_hash_table, func, vlist, flags)
2559 HASH_TABLE *var_hash_table;
2560 sh_var_map_func_t *func;
2565 register BUCKET_CONTENTS *tlist;
2569 if (var_hash_table == 0 || (HASH_ENTRIES (var_hash_table) == 0) || (vlist == 0 && func == 0))
2572 for (i = 0; i < var_hash_table->nbuckets; i++)
2574 for (tlist = hash_items (i, var_hash_table); tlist; tlist = tlist->next)
2576 var = (SHELL_VAR *)tlist->data;
2578 r = func ? (*func) (var) : 1;
2580 vlist_add (vlist, var, flags);
2586 sort_variables (array)
2589 qsort (array, strvec_len ((char **)array), sizeof (SHELL_VAR *), (QSFUNC *)qsort_var_comp);
2593 qsort_var_comp (var1, var2)
2594 SHELL_VAR **var1, **var2;
2598 if ((result = (*var1)->name[0] - (*var2)->name[0]) == 0)
2599 result = strcmp ((*var1)->name, (*var2)->name);
2604 /* Apply FUNC to each variable in SHELL_VARIABLES, adding each one for
2605 which FUNC succeeds to an array of SHELL_VAR *s. Returns the array. */
2608 sh_var_map_func_t *func;
2612 list = map_over (func, shell_variables);
2613 if (list /* && posixly_correct */)
2614 sort_variables (list);
2618 /* Apply FUNC to each variable in SHELL_FUNCTIONS, adding each one for
2619 which FUNC succeeds to an array of SHELL_VAR *s. Returns the array. */
2622 sh_var_map_func_t *func;
2626 list = map_over_funcs (func);
2627 if (list /* && posixly_correct */)
2628 sort_variables (list);
2632 /* Create a NULL terminated array of all the shell variables. */
2634 all_shell_variables ()
2636 return (vapply ((sh_var_map_func_t *)NULL));
2639 /* Create a NULL terminated array of all the shell functions. */
2641 all_shell_functions ()
2643 return (fapply ((sh_var_map_func_t *)NULL));
2650 return (invisible_p (var) == 0);
2654 all_visible_functions ()
2656 return (fapply (visible_var));
2660 all_visible_variables ()
2662 return (vapply (visible_var));
2665 /* Return non-zero if the variable VAR is visible and exported. Array
2666 variables cannot be exported. */
2668 visible_and_exported (var)
2671 return (invisible_p (var) == 0 && exported_p (var));
2674 /* Return non-zero if VAR is a local variable in the current context and
2677 local_and_exported (var)
2680 return (invisible_p (var) == 0 && local_p (var) && var->context == variable_context && exported_p (var));
2684 all_exported_variables ()
2686 return (vapply (visible_and_exported));
2690 local_exported_variables ()
2692 return (vapply (local_and_exported));
2696 variable_in_context (var)
2699 return (invisible_p (var) == 0 && local_p (var) && var->context == variable_context);
2703 all_local_variables ()
2709 vc = shell_variables;
2710 for (vc = shell_variables; vc; vc = vc->down)
2711 if (vc_isfuncenv (vc) && vc->scope == variable_context)
2716 internal_error (_("all_local_variables: no function context at current scope"));
2717 return (SHELL_VAR **)NULL;
2719 if (vc->table == 0 || HASH_ENTRIES (vc->table) == 0 || vc_haslocals (vc) == 0)
2720 return (SHELL_VAR **)NULL;
2722 vlist = vlist_alloc (HASH_ENTRIES (vc->table));
2724 flatten (vc->table, variable_in_context, vlist, 0);
2729 sort_variables (ret);
2733 #if defined (ARRAY_VARS)
2734 /* Return non-zero if the variable VAR is visible and an array. */
2736 visible_array_vars (var)
2739 return (invisible_p (var) == 0 && array_p (var));
2743 all_array_variables ()
2745 return (vapply (visible_array_vars));
2747 #endif /* ARRAY_VARS */
2750 all_variables_matching_prefix (prefix)
2753 SHELL_VAR **varlist;
2755 int vind, rind, plen;
2757 plen = STRLEN (prefix);
2758 varlist = all_visible_variables ();
2759 for (vind = 0; varlist && varlist[vind]; vind++)
2761 if (varlist == 0 || vind == 0)
2762 return ((char **)NULL);
2763 rlist = strvec_create (vind + 1);
2764 for (vind = rind = 0; varlist[vind]; vind++)
2766 if (plen == 0 || STREQN (prefix, varlist[vind]->name, plen))
2767 rlist[rind++] = savestring (varlist[vind]->name);
2769 rlist[rind] = (char *)0;
2775 /* **************************************************************** */
2777 /* Managing temporary variable scopes */
2779 /* **************************************************************** */
2781 /* Make variable NAME have VALUE in the temporary environment. */
2783 bind_tempenv_variable (name, value)
2789 var = temporary_env ? hash_lookup (name, temporary_env) : (SHELL_VAR *)NULL;
2793 FREE (value_cell (var));
2794 var_setvalue (var, savestring (value));
2795 INVALIDATE_EXPORTSTR (var);
2801 /* Find a variable in the temporary environment that is named NAME.
2802 Return the SHELL_VAR *, or NULL if not found. */
2804 find_tempenv_variable (name)
2807 return (temporary_env ? hash_lookup (name, temporary_env) : (SHELL_VAR *)NULL);
2810 /* Push the variable described by (SHELL_VAR *)DATA down to the next
2811 variable context from the temporary environment. */
2813 push_temp_var (data)
2817 HASH_TABLE *binding_table;
2819 var = (SHELL_VAR *)data;
2821 binding_table = shell_variables->table;
2822 if (binding_table == 0)
2824 if (shell_variables == global_variables)
2825 /* shouldn't happen */
2826 binding_table = shell_variables->table = global_variables->table = hash_create (0);
2828 binding_table = shell_variables->table = hash_create (TEMPENV_HASH_BUCKETS);
2831 v = bind_variable_internal (var->name, value_cell (var), binding_table, 0);
2833 /* XXX - should we set the context here? It shouldn't matter because of how
2834 assign_in_env works, but might want to check. */
2835 if (binding_table == global_variables->table) /* XXX */
2836 var->attributes &= ~(att_tempvar|att_propagate);
2839 var->attributes |= att_propagate;
2840 if (binding_table == shell_variables->table)
2841 shell_variables->flags |= VC_HASTMPVAR;
2843 v->attributes |= var->attributes;
2845 dispose_variable (var);
2849 propagate_temp_var (data)
2854 var = (SHELL_VAR *)data;
2855 if (tempvar_p (var) && (var->attributes & att_propagate))
2856 push_temp_var (data);
2858 dispose_variable (var);
2861 /* Free the storage used in the hash table for temporary
2862 environment variables. PUSHF is a function to be called
2863 to free each hash table entry. It takes care of pushing variables
2864 to previous scopes if appropriate. */
2866 dispose_temporary_env (pushf)
2867 sh_free_func_t *pushf;
2869 hash_flush (temporary_env, pushf);
2870 hash_dispose (temporary_env);
2871 temporary_env = (HASH_TABLE *)NULL;
2873 array_needs_making = 1;
2875 sv_ifs ("IFS"); /* XXX here for now */
2879 dispose_used_env_vars ()
2882 dispose_temporary_env (propagate_temp_var);
2885 /* Take all of the shell variables in the temporary environment HASH_TABLE
2886 and make shell variables from them at the current variable context. */
2888 merge_temporary_env ()
2891 dispose_temporary_env (push_temp_var);
2894 /* **************************************************************** */
2896 /* Creating and manipulating the environment */
2898 /* **************************************************************** */
2900 static inline char *
2901 mk_env_string (name, value)
2902 const char *name, *value;
2904 int name_len, value_len;
2907 name_len = strlen (name);
2908 value_len = STRLEN (value);
2909 p = (char *)xmalloc (2 + name_len + value_len);
2912 if (value && *value)
2913 strcpy (p + name_len + 1, value);
2915 p[name_len + 1] = '\0';
2928 if (legal_variable_starter ((unsigned char)*s) == 0)
2930 internal_error (_("invalid character %d in exportstr for %s"), *s, v->name);
2933 for (s = v->exportstr + 1; s && *s; s++)
2937 if (legal_variable_char ((unsigned char)*s) == 0)
2939 internal_error (_("invalid character %d in exportstr for %s"), *s, v->name);
2945 internal_error (_("no `=' in exportstr for %s"), v->name);
2953 make_env_array_from_var_list (vars)
2956 register int i, list_index;
2957 register SHELL_VAR *var;
2958 char **list, *value;
2960 list = strvec_create ((1 + strvec_len ((char **)vars)));
2962 #define USE_EXPORTSTR (value == var->exportstr)
2964 for (i = 0, list_index = 0; var = vars[i]; i++)
2966 #if defined (__CYGWIN__)
2967 /* We don't use the exportstr stuff on Cygwin at all. */
2968 INVALIDATE_EXPORTSTR (var);
2971 value = var->exportstr;
2972 else if (function_p (var))
2973 value = named_function_string ((char *)NULL, function_cell (var), 0);
2974 #if defined (ARRAY_VARS)
2975 else if (array_p (var))
2977 value = array_to_assignment_string (array_cell (var));
2979 continue; /* XXX array vars cannot yet be exported */
2983 value = value_cell (var);
2987 /* Gee, I'd like to get away with not using savestring() if we're
2988 using the cached exportstr... */
2989 list[list_index] = USE_EXPORTSTR ? savestring (value)
2990 : mk_env_string (var->name, value);
2992 if (USE_EXPORTSTR == 0)
2993 SAVE_EXPORTSTR (var, list[list_index]);
2996 #undef USE_EXPORTSTR
2999 #if defined (ARRAY_VARS)
3007 list[list_index] = (char *)NULL;
3011 /* Make an array of assignment statements from the hash table
3012 HASHED_VARS which contains SHELL_VARs. Only visible, exported
3013 variables are eligible. */
3015 make_var_export_array (vcxt)
3021 vars = map_over (visible_and_exported, vcxt);
3024 return (char **)NULL;
3026 list = make_env_array_from_var_list (vars);
3033 make_func_export_array ()
3038 vars = map_over_funcs (visible_and_exported);
3040 return (char **)NULL;
3042 list = make_env_array_from_var_list (vars);
3048 /* Add ENVSTR to the end of the exported environment, EXPORT_ENV. */
3049 #define add_to_export_env(envstr,do_alloc) \
3052 if (export_env_index >= (export_env_size - 1)) \
3054 export_env_size += 16; \
3055 export_env = strvec_resize (export_env, export_env_size); \
3057 export_env[export_env_index++] = (do_alloc) ? savestring (envstr) : envstr; \
3058 export_env[export_env_index] = (char *)NULL; \
3061 /* Add ASSIGN to EXPORT_ENV, or supercede a previous assignment in the
3062 array with the same left-hand side. Return the new EXPORT_ENV. */
3064 add_or_supercede_exported_var (assign, do_alloc)
3071 equal_offset = assignment (assign, 0);
3072 if (equal_offset == 0)
3073 return (export_env);
3075 /* If this is a function, then only supersede the function definition.
3076 We do this by including the `=() {' in the comparison, like
3077 initialize_shell_variables does. */
3078 if (assign[equal_offset + 1] == '(' &&
3079 strncmp (assign + equal_offset + 2, ") {", 3) == 0) /* } */
3082 for (i = 0; i < export_env_index; i++)
3084 if (STREQN (assign, export_env[i], equal_offset + 1))
3086 free (export_env[i]);
3087 export_env[i] = do_alloc ? savestring (assign) : assign;
3088 return (export_env);
3091 add_to_export_env (assign, do_alloc);
3092 return (export_env);
3096 add_temp_array_to_env (temp_array, do_alloc, do_supercede)
3098 int do_alloc, do_supercede;
3102 if (temp_array == 0)
3105 for (i = 0; temp_array[i]; i++)
3108 export_env = add_or_supercede_exported_var (temp_array[i], do_alloc);
3110 add_to_export_env (temp_array[i], do_alloc);
3116 /* Make the environment array for the command about to be executed, if the
3117 array needs making. Otherwise, do nothing. If a shell action could
3118 change the array that commands receive for their environment, then the
3119 code should `array_needs_making++'.
3121 The order to add to the array is:
3123 list of var contexts whose head is shell_variables
3126 This is the shell variable lookup order. We add only new variable
3127 names at each step, which allows local variables and variables in
3128 the temporary environments to shadow variables in the global (or
3129 any previous) scope.
3133 n_shell_variables ()
3138 for (n = 0, vc = shell_variables; vc; vc = vc->down)
3139 n += HASH_ENTRIES (vc->table);
3144 maybe_make_export_env ()
3146 register char **temp_array;
3150 if (array_needs_making)
3153 strvec_flush (export_env);
3155 /* Make a guess based on how many shell variables and functions we
3156 have. Since there will always be array variables, and array
3157 variables are not (yet) exported, this will always be big enough
3158 for the exported variables and functions. */
3159 new_size = n_shell_variables () + HASH_ENTRIES (shell_functions) + 1 +
3160 HASH_ENTRIES (temporary_env);
3161 if (new_size > export_env_size)
3163 export_env_size = new_size;
3164 export_env = strvec_resize (export_env, export_env_size);
3166 export_env[export_env_index = 0] = (char *)NULL;
3168 /* Make a dummy variable context from the temporary_env, stick it on
3169 the front of shell_variables, call make_var_export_array on the
3170 whole thing to flatten it, and convert the list of SHELL_VAR *s
3171 to the form needed by the environment. */
3174 tcxt = new_var_context ((char *)NULL, 0);
3175 tcxt->table = temporary_env;
3176 tcxt->down = shell_variables;
3179 tcxt = shell_variables;
3181 temp_array = make_var_export_array (tcxt);
3183 add_temp_array_to_env (temp_array, 0, 0);
3185 if (tcxt != shell_variables)
3188 #if defined (RESTRICTED_SHELL)
3189 /* Restricted shells may not export shell functions. */
3190 temp_array = restricted ? (char **)0 : make_func_export_array ();
3192 temp_array = make_func_export_array ();
3195 add_temp_array_to_env (temp_array, 0, 0);
3197 array_needs_making = 0;
3201 /* This is an efficiency hack. PWD and OLDPWD are auto-exported, so
3202 we will need to remake the exported environment every time we
3203 change directories. `_' is always put into the environment for
3204 every external command, so without special treatment it will always
3205 cause the environment to be remade.
3207 If there is no other reason to make the exported environment, we can
3208 just update the variables in place and mark the exported environment
3209 as no longer needing a remake. */
3211 update_export_env_inplace (env_prefix, preflen, value)
3218 evar = (char *)xmalloc (STRLEN (value) + preflen + 1);
3219 strcpy (evar, env_prefix);
3221 strcpy (evar + preflen, value);
3222 export_env = add_or_supercede_exported_var (evar, 0);
3225 /* We always put _ in the environment as the name of this command. */
3227 put_command_name_into_env (command_name)
3230 update_export_env_inplace ("_=", 2, command_name);
3233 #if 0 /* UNUSED -- it caused too many problems */
3235 put_gnu_argv_flags_into_env (pid, flags_string)
3245 fl = strlen (flags_string);
3247 dummy = (char *)xmalloc (l + fl + 30);
3249 strcpy (dummy + 1, pbuf);
3250 strcpy (dummy + 1 + l, "_GNU_nonoption_argv_flags_");
3251 dummy[l + 27] = '=';
3252 strcpy (dummy + l + 28, flags_string);
3256 export_env = add_or_supercede_exported_var (dummy, 0);
3260 /* **************************************************************** */
3262 /* Managing variable contexts */
3264 /* **************************************************************** */
3266 /* Allocate and return a new variable context with NAME and FLAGS.
3267 NAME can be NULL. */
3270 new_var_context (name, flags)
3276 vc = (VAR_CONTEXT *)xmalloc (sizeof (VAR_CONTEXT));
3277 vc->name = name ? savestring (name) : (char *)NULL;
3278 vc->scope = variable_context;
3281 vc->up = vc->down = (VAR_CONTEXT *)NULL;
3282 vc->table = (HASH_TABLE *)NULL;
3287 /* Free a variable context and its data, including the hash table. Dispose
3288 all of the variables. */
3290 dispose_var_context (vc)
3297 delete_all_variables (vc->table);
3298 hash_dispose (vc->table);
3304 /* Set VAR's scope level to the current variable context. */
3309 return (var->context = variable_context);
3312 /* Make a new variable context with NAME and FLAGS and a HASH_TABLE of
3313 temporary variables, and push it onto shell_variables. This is
3314 for shell functions. */
3316 push_var_context (name, flags, tempvars)
3319 HASH_TABLE *tempvars;
3323 vc = new_var_context (name, flags);
3324 vc->table = tempvars;
3327 /* Have to do this because the temp environment was created before
3328 variable_context was incremented. */
3329 flatten (tempvars, set_context, (VARLIST *)NULL, 0);
3330 vc->flags |= VC_HASTMPVAR;
3332 vc->down = shell_variables;
3333 shell_variables->up = vc;
3335 return (shell_variables = vc);
3339 push_func_var (data)
3344 var = (SHELL_VAR *)data;
3346 if (tempvar_p (var) && (posixly_correct || (var->attributes & att_propagate)))
3348 /* XXX - should we set v->context here? */
3349 v = bind_variable_internal (var->name, value_cell (var), shell_variables->table, 0);
3350 if (shell_variables == global_variables)
3351 var->attributes &= ~(att_tempvar|att_propagate);
3353 shell_variables->flags |= VC_HASTMPVAR;
3354 v->attributes |= var->attributes;
3357 dispose_variable (var);
3360 /* Pop the top context off of VCXT and dispose of it, returning the rest of
3365 VAR_CONTEXT *ret, *vcxt;
3367 vcxt = shell_variables;
3368 if (vc_isfuncenv (vcxt) == 0)
3370 internal_error (_("pop_var_context: head of shell_variables not a function context"));
3374 if (ret = vcxt->down)
3376 ret->up = (VAR_CONTEXT *)NULL;
3377 shell_variables = ret;
3379 hash_flush (vcxt->table, push_func_var);
3380 dispose_var_context (vcxt);
3383 internal_error (_("pop_var_context: no global_variables context"));
3386 /* Delete the HASH_TABLEs for all variable contexts beginning at VCXT, and
3387 all of the VAR_CONTEXTs except GLOBAL_VARIABLES. */
3389 delete_all_contexts (vcxt)
3394 for (v = vcxt; v != global_variables; v = t)
3397 dispose_var_context (v);
3400 delete_all_variables (global_variables->table);
3401 shell_variables = global_variables;
3404 /* **************************************************************** */
3406 /* Pushing and Popping temporary variable scopes */
3408 /* **************************************************************** */
3411 push_scope (flags, tmpvars)
3413 HASH_TABLE *tmpvars;
3415 return (push_var_context ((char *)NULL, flags, tmpvars));
3419 push_exported_var (data)
3424 var = (SHELL_VAR *)data;
3426 /* If a temp var had its export attribute set, or it's marked to be
3427 propagated, bind it in the previous scope before disposing it. */
3428 if (exported_p (var) || (var->attributes & att_propagate))
3430 var->attributes &= ~att_tempvar; /* XXX */
3431 v = bind_variable_internal (var->name, value_cell (var), shell_variables->table, 0);
3432 if (shell_variables == global_variables)
3433 var->attributes &= ~att_propagate;
3434 v->attributes |= var->attributes;
3437 dispose_variable (var);
3441 pop_scope (is_special)
3444 VAR_CONTEXT *vcxt, *ret;
3446 vcxt = shell_variables;
3447 if (vc_istempscope (vcxt) == 0)
3449 internal_error (_("pop_scope: head of shell_variables not a temporary environment scope"));
3455 ret->up = (VAR_CONTEXT *)NULL;
3457 shell_variables = ret;
3459 /* Now we can take care of merging variables in VCXT into set of scopes
3460 whose head is RET (shell_variables). */
3465 hash_flush (vcxt->table, push_func_var);
3467 hash_flush (vcxt->table, push_exported_var);
3468 hash_dispose (vcxt->table);
3472 sv_ifs ("IFS"); /* XXX here for now */
3475 /* **************************************************************** */
3477 /* Pushing and Popping function contexts */
3479 /* **************************************************************** */
3481 static WORD_LIST **dollar_arg_stack = (WORD_LIST **)NULL;
3482 static int dollar_arg_stack_slots;
3483 static int dollar_arg_stack_index;
3485 /* XXX - we might want to consider pushing and popping the `getopts' state
3486 when we modify the positional parameters. */
3488 push_context (name, is_subshell, tempvars)
3489 char *name; /* function name */
3491 HASH_TABLE *tempvars;
3493 if (is_subshell == 0)
3494 push_dollar_vars ();
3496 push_var_context (name, VC_FUNCENV, tempvars);
3499 /* Only called when subshell == 0, so we don't need to check, and can
3500 unconditionally pop the dollar vars off the stack. */
3508 sv_ifs ("IFS"); /* XXX here for now */
3511 /* Save the existing positional parameters on a stack. */
3515 if (dollar_arg_stack_index + 2 > dollar_arg_stack_slots)
3517 dollar_arg_stack = (WORD_LIST **)
3518 xrealloc (dollar_arg_stack, (dollar_arg_stack_slots += 10)
3519 * sizeof (WORD_LIST **));
3521 dollar_arg_stack[dollar_arg_stack_index++] = list_rest_of_args ();
3522 dollar_arg_stack[dollar_arg_stack_index] = (WORD_LIST *)NULL;
3525 /* Restore the positional parameters from our stack. */
3529 if (!dollar_arg_stack || dollar_arg_stack_index == 0)
3532 remember_args (dollar_arg_stack[--dollar_arg_stack_index], 1);
3533 dispose_words (dollar_arg_stack[dollar_arg_stack_index]);
3534 dollar_arg_stack[dollar_arg_stack_index] = (WORD_LIST *)NULL;
3535 set_dollar_vars_unchanged ();
3539 dispose_saved_dollar_vars ()
3541 if (!dollar_arg_stack || dollar_arg_stack_index == 0)
3544 dispose_words (dollar_arg_stack[dollar_arg_stack_index]);
3545 dollar_arg_stack[dollar_arg_stack_index] = (WORD_LIST *)NULL;
3548 /* Manipulate the special BASH_ARGV and BASH_ARGC variables. */
3554 #if defined (ARRAY_VARS) && defined (DEBUGGER)
3555 SHELL_VAR *bash_argv_v, *bash_argc_v;
3556 ARRAY *bash_argv_a, *bash_argc_a;
3561 GET_ARRAY_FROM_VAR ("BASH_ARGV", bash_argv_v, bash_argv_a);
3562 GET_ARRAY_FROM_VAR ("BASH_ARGC", bash_argc_v, bash_argc_a);
3564 for (l = list, i = 0; l; l = l->next, i++)
3565 array_push (bash_argv_a, l->word->word);
3568 array_push (bash_argc_a, t);
3570 #endif /* ARRAY_VARS && DEBUGGER */
3573 /* Remove arguments from BASH_ARGV array. Pop top element off BASH_ARGC
3574 array and use that value as the count of elements to remove from
3579 #if defined (ARRAY_VARS) && defined (DEBUGGER)
3580 SHELL_VAR *bash_argv_v, *bash_argc_v;
3581 ARRAY *bash_argv_a, *bash_argc_a;
3585 GET_ARRAY_FROM_VAR ("BASH_ARGV", bash_argv_v, bash_argv_a);
3586 GET_ARRAY_FROM_VAR ("BASH_ARGC", bash_argc_v, bash_argc_a);
3588 ce = array_shift (bash_argc_a, 1, 0);
3589 if (ce == 0 || legal_number (element_value (ce), &i) == 0)
3593 array_pop (bash_argv_a);
3594 array_dispose_element (ce);
3595 #endif /* ARRAY_VARS && DEBUGGER */
3598 /*************************************************
3600 * Functions to manage special variables *
3602 *************************************************/
3604 /* Extern declarations for variables this code has to manage. */
3605 extern int eof_encountered, eof_encountered_limit, ignoreeof;
3607 #if defined (READLINE)
3608 extern int no_line_editing;
3609 extern int hostname_list_initialized;
3612 /* An alist of name.function for each special variable. Most of the
3613 functions don't do much, and in fact, this would be faster with a
3614 switch statement, but by the end of this file, I am sick of switch
3617 #define SET_INT_VAR(name, intvar) intvar = find_variable (name) != 0
3619 /* This table will be sorted with qsort() the first time it's accessed. */
3620 struct name_and_function {
3622 sh_sv_func_t *function;
3625 static struct name_and_function special_vars[] = {
3626 { "GLOBIGNORE", sv_globignore },
3628 #if defined (HISTORY)
3629 { "HISTCONTROL", sv_history_control },
3630 { "HISTFILESIZE", sv_histsize },
3631 { "HISTIGNORE", sv_histignore },
3632 { "HISTSIZE", sv_histsize },
3633 { "HISTTIMEFORMAT", sv_histtimefmt },
3636 #if defined (READLINE)
3637 { "HOSTFILE", sv_hostfile },
3641 { "IGNOREEOF", sv_ignoreeof },
3643 { "LANG", sv_locale },
3644 { "LC_ALL", sv_locale },
3645 { "LC_COLLATE", sv_locale },
3646 { "LC_CTYPE", sv_locale },
3647 { "LC_MESSAGES", sv_locale },
3648 { "LC_NUMERIC", sv_locale },
3650 { "MAIL", sv_mail },
3651 { "MAILCHECK", sv_mail },
3652 { "MAILPATH", sv_mail },
3654 { "OPTERR", sv_opterr },
3655 { "OPTIND", sv_optind },
3657 { "PATH", sv_path },
3658 { "POSIXLY_CORRECT", sv_strict_posix },
3660 #if defined (READLINE)
3661 { "TERM", sv_terminal },
3662 { "TERMCAP", sv_terminal },
3663 { "TERMINFO", sv_terminal },
3664 #endif /* READLINE */
3666 { "TEXTDOMAIN", sv_locale },
3667 { "TEXTDOMAINDIR", sv_locale },
3669 #if defined (HAVE_TZSET) && defined (PROMPT_STRING_DECODE)
3673 #if defined (HISTORY) && defined (BANG_HISTORY)
3674 { "histchars", sv_histchars },
3675 #endif /* HISTORY && BANG_HISTORY */
3677 { "ignoreeof", sv_ignoreeof },
3679 { (char *)0, (sh_sv_func_t *)0 }
3682 #define N_SPECIAL_VARS (sizeof (special_vars) / sizeof (special_vars[0]) - 1)
3685 sv_compare (sv1, sv2)
3686 struct name_and_function *sv1, *sv2;
3690 if ((r = sv1->name[0] - sv2->name[0]) == 0)
3691 r = strcmp (sv1->name, sv2->name);
3696 find_special_var (name)
3701 for (i = 0; special_vars[i].name; i++)
3703 r = special_vars[i].name[0] - name[0];
3705 r = strcmp (special_vars[i].name, name);
3709 /* Can't match any of rest of elements in sorted list. Take this out
3710 if it causes problems in certain environments. */
3716 /* The variable in NAME has just had its state changed. Check to see if it
3717 is one of the special ones where something special happens. */
3719 stupidly_hack_special_variables (name)
3722 static int sv_sorted = 0;
3725 if (sv_sorted == 0) /* shouldn't need, but it's fairly cheap. */
3727 qsort (special_vars, N_SPECIAL_VARS, sizeof (special_vars[0]),
3728 (QSFUNC *)sv_compare);
3732 i = find_special_var (name);
3734 (*(special_vars[i].function)) (name);
3743 v = find_variable ("IFS");
3747 /* What to do just after the PATH variable has changed. */
3756 /* What to do just after one of the MAILxxxx variables has changed. NAME
3757 is the name of the variable. This is called with NAME set to one of
3758 MAIL, MAILCHECK, or MAILPATH. */
3763 /* If the time interval for checking the files has changed, then
3764 reset the mail timer. Otherwise, one of the pathname vars
3765 to the users mailbox has changed, so rebuild the array of
3767 if (name[4] == 'C') /* if (strcmp (name, "MAILCHECK") == 0) */
3768 reset_mail_timer ();
3772 remember_mail_dates ();
3776 /* What to do when GLOBIGNORE changes. */
3778 sv_globignore (name)
3781 setup_glob_ignore (name);
3784 #if defined (READLINE)
3785 /* What to do just after one of the TERMxxx variables has changed.
3786 If we are an interactive shell, then try to reset the terminal
3787 information in readline. */
3792 if (interactive_shell && no_line_editing == 0)
3793 rl_reset_terminal (get_string_value ("TERM"));
3802 v = find_variable (name);
3804 clear_hostname_list ();
3806 hostname_list_initialized = 0;
3808 #endif /* READLINE */
3810 #if defined (HISTORY)
3811 /* What to do after the HISTSIZE or HISTFILESIZE variables change.
3812 If there is a value for this HISTSIZE (and it is numeric), then stifle
3813 the history. Otherwise, if there is NO value for this variable,
3814 unstifle the history. If name is HISTFILESIZE, and its value is
3815 numeric, truncate the history file to hold no more than that many
3824 temp = get_string_value (name);
3828 if (legal_number (temp, &num))
3832 stifle_history (num);
3833 num = where_history ();
3834 if (history_lines_this_session > num)
3835 history_lines_this_session = num;
3839 history_truncate_file (get_string_value ("HISTFILE"), (int)num);
3840 if (num <= history_lines_in_file)
3841 history_lines_in_file = num;
3845 else if (name[4] == 'S')
3846 unstifle_history ();
3849 /* What to do after the HISTIGNORE variable changes. */
3851 sv_histignore (name)
3854 setup_history_ignore (name);
3857 /* What to do after the HISTCONTROL variable changes. */
3859 sv_history_control (name)
3866 history_control = 0;
3867 temp = get_string_value (name);
3869 if (temp == 0 || *temp == 0)
3873 while (val = extract_colon_unit (temp, &tptr))
3875 if (STREQ (val, "ignorespace"))
3876 history_control |= HC_IGNSPACE;
3877 else if (STREQ (val, "ignoredups"))
3878 history_control |= HC_IGNDUPS;
3879 else if (STREQ (val, "ignoreboth"))
3880 history_control |= HC_IGNBOTH;
3881 else if (STREQ (val, "erasedups"))
3882 history_control |= HC_ERASEDUPS;
3888 #if defined (BANG_HISTORY)
3889 /* Setting/unsetting of the history expansion character. */
3896 temp = get_string_value (name);
3899 history_expansion_char = *temp;
3900 if (temp[0] && temp[1])
3902 history_subst_char = temp[1];
3904 history_comment_char = temp[2];
3909 history_expansion_char = '!';
3910 history_subst_char = '^';
3911 history_comment_char = '#';
3914 #endif /* BANG_HISTORY */
3917 sv_histtimefmt (name)
3922 v = find_variable (name);
3923 history_write_timestamps = (v != 0);
3925 #endif /* HISTORY */
3927 #if defined (HAVE_TZSET) && defined (PROMPT_STRING_DECODE)
3936 /* If the variable exists, then the value of it can be the number
3937 of times we actually ignore the EOF. The default is small,
3938 (smaller than csh, anyway). */
3946 eof_encountered = 0;
3948 tmp_var = find_variable (name);
3949 ignoreeof = tmp_var != 0;
3950 temp = tmp_var ? value_cell (tmp_var) : (char *)NULL;
3952 eof_encountered_limit = (*temp && all_digits (temp)) ? atoi (temp) : 10;
3953 set_shellopts (); /* make sure `ignoreeof' is/is not in $SHELLOPTS */
3963 tt = get_string_value ("OPTIND");
3968 /* According to POSIX, setting OPTIND=1 resets the internal state
3970 if (s < 0 || s == 1)
3984 tt = get_string_value ("OPTERR");
3985 sh_opterr = (tt && *tt) ? atoi (tt) : 1;
3989 sv_strict_posix (name)
3992 SET_INT_VAR (name, posixly_correct);
3993 posix_initialize (posixly_correct);
3994 #if defined (READLINE)
3995 if (interactive_shell)
3996 posix_readline_initialize (posixly_correct);
3997 #endif /* READLINE */
3998 set_shellopts (); /* make sure `posix' is/is not in $SHELLOPTS */
4007 v = get_string_value (name);
4008 if (name[0] == 'L' && name[1] == 'A') /* LANG */
4011 set_locale_var (name, v); /* LC_*, TEXTDOMAIN* */
4014 #if defined (ARRAY_VARS)
4016 set_pipestatus_array (ps, nproc)
4024 char *t, tbuf[INT_STRLEN_BOUND(int) + 1];
4026 v = find_variable ("PIPESTATUS");
4028 v = make_new_array_variable ("PIPESTATUS");
4029 if (array_p (v) == 0)
4030 return; /* Do nothing if not an array variable. */
4033 if (a == 0 || array_num_elements (a) == 0)
4035 for (i = 0; i < nproc; i++) /* was ps[i] != -1, not i < nproc */
4037 t = inttostr (ps[i], tbuf, sizeof (tbuf));
4038 array_insert (a, i, t);
4044 if (array_num_elements (a) == nproc && nproc == 1)
4046 ae = element_forw (a->head);
4047 free (element_value (ae));
4048 ae->value = itos (ps[0]);
4050 else if (array_num_elements (a) <= nproc)
4052 /* modify in array_num_elements members in place, then add */
4054 for (i = 0; i < array_num_elements (a); i++)
4056 ae = element_forw (ae);
4057 free (element_value (ae));
4058 ae->value = itos (ps[i]);
4061 for ( ; i < nproc; i++)
4063 t = inttostr (ps[i], tbuf, sizeof (tbuf));
4064 array_insert (a, i, t);
4069 /* deleting elements. it's faster to rebuild the array. */
4071 for (i = 0; ps[i] != -1; i++)
4073 t = inttostr (ps[i], tbuf, sizeof (tbuf));
4074 array_insert (a, i, t);
4081 set_pipestatus_from_exit (s)
4084 #if defined (ARRAY_VARS)
4085 static int v[2] = { 0, -1 };
4088 set_pipestatus_array (v, 1);