1 /* variables.c -- Functions for hacking shell variables. */
3 /* Copyright (C) 1987,1989 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "bashtypes.h"
24 #include "posixstat.h"
30 #if defined (HAVE_UNISTD_H)
41 #include "execute_cmd.h"
43 #include "mailcheck.h"
46 #include "builtins/getopt.h"
47 #include "builtins/common.h"
49 #if defined (READLINE)
50 # include "bashline.h"
51 # include <readline/readline.h>
53 # include <tilde/tilde.h>
57 # include "bashhist.h"
58 # include <readline/history.h>
61 /* Variables used here and defined in other files. */
62 extern int posixly_correct;
63 extern int variable_context, line_number;
64 extern int interactive, interactive_shell, login_shell;
65 extern int subshell_environment, indirection_level;
66 extern int build_version, patch_level;
67 extern char *dist_version, *release_status;
68 extern char *shell_name;
69 extern char *primary_prompt, *secondary_prompt;
70 extern char *current_host_name;
71 extern Function *this_shell_builtin;
72 extern char *this_command_name;
73 extern time_t shell_start_time;
75 /* The list of shell variables that the user has created, or that came from
77 HASH_TABLE *shell_variables = (HASH_TABLE *)NULL;
79 /* The list of shell functions that the user has created, or that came from
81 HASH_TABLE *shell_functions = (HASH_TABLE *)NULL;
83 /* The current variable context. This is really a count of how deep into
84 executing functions we are. */
85 int variable_context = 0;
87 /* The array of shell assignments which are made only in the environment
88 for a single command. */
89 char **temporary_env = (char **)NULL;
91 /* The array of shell assignments which are in the environment for the
92 execution of a shell function. */
93 char **function_env = (char **)NULL;
95 /* The array of shell assignments which are made only in the environment
96 for the execution of a shell builtin command which may cause more than
97 one command to be executed (e.g., "source"). */
98 char **builtin_env = (char **)NULL;
100 /* Some funky variables which are known about specially. Here is where
101 "$*", "$1", and all the cruft is kept. */
102 char *dollar_vars[10];
103 WORD_LIST *rest_of_args = (WORD_LIST *)NULL;
105 /* The value of $$. */
106 int dollar_dollar_pid;
108 /* An array which is passed to commands as their environment. It is
109 manufactured from the union of the initial environment and the
110 shell variables that are marked for export. */
111 char **export_env = (char **)NULL;
112 static int export_env_index;
113 static int export_env_size;
115 /* Non-zero means that we have to remake EXPORT_ENV. */
116 int array_needs_making = 1;
118 /* The number of times BASH has been executed. This is set
119 by initialize_variables (). */
122 static char *have_local_variables;
123 static int local_variable_stack_size;
125 /* Some forward declarations. */
126 static void set_home_var ();
127 static void set_shell_var ();
128 static char *get_bash_name ();
129 static void initialize_shell_level ();
130 static void uidset ();
131 static void initialize_dynamic_variables ();
132 static void make_vers_array ();
133 static void sbrand (); /* set bash random number generator. */
134 static int qsort_var_comp ();
136 /* Make VAR be auto-exported. VAR is a pointer to a SHELL_VAR. */
137 #define set_auto_export(var) \
138 do { var->attributes |= att_exported; array_needs_making = 1; } while (0)
140 /* Initialize the shell variables from the current environment.
141 If PRIVMODE is nonzero, don't import functions from ENV or
144 initialize_shell_variables (env, privmode)
148 char *name, *string, *temp_string;
149 int c, char_index, string_index, string_length;
152 if (shell_variables == 0)
153 shell_variables = make_hash_table (0);
155 if (shell_functions == 0)
156 shell_functions = make_hash_table (0);
158 for (string_index = 0; string = env[string_index++]; )
162 while ((c = *string++) && c != '=')
164 if (string[-1] == '=')
165 char_index = string - name - 1;
167 /* If there are weird things in the environment, like `=xxx' or a
168 string without an `=', just skip them. */
172 /* ASSERT(name[char_index] == '=') */
173 name[char_index] = '\0';
174 /* Now, name = env variable name, string = env variable value, and
175 char_index == strlen (name) */
177 /* If exported function, define it now. */
178 if (privmode == 0 && STREQN ("() {", string, 4))
180 string_length = strlen (string);
181 temp_string = xmalloc (3 + string_length + char_index);
183 strcpy (temp_string, name);
184 temp_string[char_index] = ' ';
185 strcpy (temp_string + char_index + 1, string);
187 sprintf (temp_string, "%s %s", name, string);
190 parse_and_execute (temp_string, name, SEVAL_NONINT|SEVAL_NOHIST);
192 /* Ancient backwards compatibility. Old versions of bash exported
193 functions like name()=() {...} */
194 if (name[char_index - 1] == ')' && name[char_index - 2] == '(')
195 name[char_index - 2] = '\0';
197 if (temp_var = find_function (name))
199 temp_var->attributes |= (att_exported | att_imported);
200 array_needs_making = 1;
203 report_error ("error importing function definition for `%s'", name);
205 if (name[char_index - 1] == ')' && name[char_index - 2] == '\0')
206 name[char_index - 2] = '(';
208 #if defined (ARRAY_VARS)
210 /* Array variables may not yet be exported. */
211 else if (*string == '(' && string[1] == '[' && strchr (string, ')'))
214 temp_string = extract_array_assignment_list (string, &string_length);
215 temp_var = assign_array_from_string (name, temp_string);
217 temp_var->attributes |= (att_exported | att_imported);
218 array_needs_making = 1;
224 temp_var = bind_variable (name, string);
225 temp_var->attributes |= (att_exported | att_imported);
226 array_needs_making = 1;
229 name[char_index] = '=';
232 /* If we got PWD from the environment, update our idea of the current
233 working directory. In any case, make sure that PWD exists before
234 checking it. It is possible for getcwd () to fail on shell startup,
235 and in that case, PWD would be undefined. */
236 temp_var = find_variable ("PWD");
237 if (temp_var && imported_p (temp_var) &&
238 (temp_string = value_cell (temp_var)) &&
239 same_file (temp_string, ".", (struct stat *)NULL, (struct stat *)NULL))
240 set_working_directory (temp_string);
243 temp_string = get_working_directory ("shell-init");
246 temp_var = bind_variable ("PWD", temp_string);
247 set_auto_export (temp_var);
252 /* Set up initial value of $_ */
253 temp_var = bind_variable ("_", dollar_vars[0]);
255 /* Remember this pid. */
256 dollar_dollar_pid = (int)getpid ();
258 /* Now make our own defaults in case the vars that we think are
259 important are missing. */
260 temp_var = set_if_not ("PATH", DEFAULT_PATH_VALUE);
261 set_auto_export (temp_var);
263 temp_var = set_if_not ("TERM", "dumb");
264 set_auto_export (temp_var);
267 /* set node id -- don't import it from the environment */
270 qnx_nidtostr (getnid (), node_name, sizeof (node_name));
271 temp_var = bind_variable ("NODE", node_name);
272 set_auto_export (temp_var);
276 /* set up the prompts. */
277 if (interactive_shell)
279 set_if_not ("PS1", primary_prompt);
280 set_if_not ("PS2", secondary_prompt);
282 set_if_not ("PS4", "+ ");
284 /* Don't allow IFS to be imported from the environment. */
285 temp_var = bind_variable ("IFS", " \t\n");
287 /* Magic machine types. Pretty convenient. */
288 temp_var = bind_variable ("HOSTTYPE", HOSTTYPE);
289 set_auto_export (temp_var);
290 temp_var = bind_variable ("OSTYPE", OSTYPE);
291 set_auto_export (temp_var);
292 temp_var = bind_variable ("MACHTYPE", MACHTYPE);
293 set_auto_export (temp_var);
294 temp_var = bind_variable ("HOSTNAME", current_host_name);
295 set_auto_export (temp_var);
297 /* Default MAILCHECK for interactive shells. Defer the creation of a
298 default MAILPATH until the startup files are read, because MAIL
299 names a mail file if MAILCHECK is not set, and we should provide a
300 default only if neither is set. */
301 if (interactive_shell)
302 set_if_not ("MAILCHECK", "60");
304 /* Do some things with shell level. */
305 initialize_shell_level ();
307 /* Make a variable $PPID, which holds the pid of the shell's parent. */
308 name = itos ((int) getppid ());
309 temp_var = find_variable ("PPID");
311 temp_var->attributes &= ~(att_readonly | att_exported);
312 temp_var = bind_variable ("PPID", name);
313 temp_var->attributes |= (att_readonly | att_integer);
316 /* Initialize the `getopts' stuff. */
317 bind_variable ("OPTIND", "1");
319 bind_variable ("OPTERR", "1");
322 if (login_shell == 1)
325 /* Get the full pathname to THIS shell, and set the BASH variable
327 name = get_bash_name ();
328 temp_var = bind_variable ("BASH", name);
331 /* Make the exported environment variable SHELL be the user's login
332 shell. Note that the `tset' command looks at this variable
333 to determine what style of commands to output; if it ends in "csh",
334 then C-shell commands are output, else Bourne shell commands. */
337 /* Make a variable called BASH_VERSION which contains the version info. */
338 bind_variable ("BASH_VERSION", shell_version_string ());
339 #if defined (ARRAY_VARS)
343 /* Find out if we're supposed to be in Posix.2 mode via an
344 environment variable. */
345 temp_var = find_variable ("POSIXLY_CORRECT");
347 temp_var = find_variable ("POSIX_PEDANTIC");
348 if (temp_var && imported_p (temp_var))
349 sv_strict_posix (temp_var->name);
351 #if defined (HISTORY)
352 /* Set history variables to defaults, and then do whatever we would
353 do if the variable had just been set. Do this only in the case
354 that we are remembering commands on the history list. */
355 if (remember_on_history)
357 name = bash_tilde_expand (posixly_correct ? "~/.sh_history" : "~/.bash_history");
359 set_if_not ("HISTFILE", name);
362 set_if_not ("HISTSIZE", "500");
363 sv_histsize ("HISTSIZE");
367 /* Seed the random number generator. */
368 sbrand (dollar_dollar_pid + (long)shell_start_time);
370 /* Handle some "special" variables that we may have inherited from a
372 if (interactive_shell)
374 temp_var = find_variable ("IGNOREEOF");
376 temp_var = find_variable ("ignoreeof");
377 if (temp_var && imported_p (temp_var))
378 sv_ignoreeof (temp_var->name);
381 #if defined (HISTORY)
382 if (interactive_shell && remember_on_history)
384 sv_history_control ("HISTCONTROL");
385 sv_histignore ("HISTIGNORE");
389 /* Get the user's real and effective user ids. */
392 /* Initialize the dynamic variables, and seed their values. */
393 initialize_dynamic_variables ();
396 /* Set $HOME to the information in the password file if we didn't get
397 it from the environment. */
403 temp_var = find_variable ("HOME");
406 if (current_user.home_dir == 0)
407 get_current_user_info ();
408 temp_var = bind_variable ("HOME", current_user.home_dir);
410 temp_var->attributes |= att_exported;
413 /* Set $SHELL to the user's login shell if it is not already set. Call
414 get_current_user_info if we haven't already fetched the shell. */
420 temp_var = find_variable ("SHELL");
423 if (current_user.shell == 0)
424 get_current_user_info ();
425 temp_var = bind_variable ("SHELL", current_user.shell);
427 temp_var->attributes |= att_exported;
435 if ((login_shell == 1) && (*shell_name != '/'))
437 if (current_user.shell == 0)
438 get_current_user_info ();
439 name = savestring (current_user.shell);
441 else if (*shell_name == '/')
442 name = savestring (shell_name);
443 else if (shell_name[0] == '.' && shell_name[1] == '/')
445 /* Fast path for common case. */
449 cdir = get_string_value ("PWD");
451 name = xmalloc (len + strlen (shell_name) + 1);
453 strcpy (name + len, shell_name + 1);
460 tname = find_user_command (shell_name);
464 /* Try the current directory. If there is not an executable
465 there, just punt and use the login shell. */
466 s = file_status (shell_name);
469 tname = make_absolute (shell_name, get_string_value ("PWD"));
470 if (*shell_name == '.')
472 name = canonicalize_pathname (tname);
483 if (current_user.shell == 0)
484 get_current_user_info ();
485 name = savestring (current_user.shell);
490 name = full_pathname (tname);
499 adjust_shell_level (change)
502 char new_level[5], *old_SHLVL;
506 old_SHLVL = get_string_value ("SHLVL");
507 old_level = old_SHLVL ? atoi (old_SHLVL) : 0;
509 shell_level = old_level + change;
512 else if (shell_level > 1000)
514 internal_warning ("shell level (%d) too high, resetting to 1", shell_level);
518 /* We don't need the full generality of itos here. */
519 if (shell_level < 10)
521 new_level[0] = shell_level + '0';
524 else if (shell_level < 100)
526 new_level[0] = (shell_level / 10) + '0';
527 new_level[1] = (shell_level % 10) + '0';
530 else if (shell_level < 1000)
532 new_level[0] = (shell_level / 100) + '0';
533 old_level = shell_level % 100;
534 new_level[1] = (old_level / 10) + '0';
535 new_level[2] = (old_level % 10) + '0';
539 temp_var = bind_variable ("SHLVL", new_level);
540 set_auto_export (temp_var);
544 initialize_shell_level ()
549 temp_var = set_if_not ("SHLVL", "0");
550 set_auto_export (temp_var);
552 adjust_shell_level (1);
559 register SHELL_VAR *v;
561 buff = itos (current_user.uid);
562 v = find_variable ("UID");
564 v->attributes &= ~att_readonly;
566 v = bind_variable ("UID", buff);
567 v->attributes |= (att_readonly | att_integer);
569 if (current_user.euid != current_user.uid)
572 buff = itos (current_user.euid);
575 v = find_variable ("EUID");
577 v->attributes &= ~att_readonly;
579 v = bind_variable ("EUID", buff);
580 v->attributes |= (att_readonly | att_integer);
584 #if defined (ARRAY_VARS)
592 makunbound ("BASH_VERSINFO", shell_variables);
594 vv = make_new_array_variable ("BASH_VERSINFO");
595 av = array_cell (vv);
596 strcpy (d, dist_version);
600 array_add_element (av, 0, d);
601 array_add_element (av, 1, s);
602 s = itos (patch_level);
603 array_add_element (av, 2, s);
605 s = itos (build_version);
606 array_add_element (av, 3, s);
608 array_add_element (av, 4, release_status);
609 array_add_element (av, 5, MACHTYPE);
611 vv->attributes |= att_readonly;
613 #endif /* ARRAY_VARS */
615 /* Set the environment variables $LINES and $COLUMNS in response to
616 a window size change. */
618 set_lines_and_columns (lines, cols)
624 bind_variable ("LINES", val);
628 bind_variable ("COLUMNS", val);
632 /* Set NAME to VALUE if NAME has no value. */
634 set_if_not (name, value)
639 v = find_variable (name);
641 v = bind_variable (name, value);
645 /* Map FUNCTION over the variables in VARIABLES. Return an array of the
646 variables for which FUNCTION returns a non-zero value. A NULL value
647 for FUNCTION means to use all variables. */
649 map_over (function, var_hash_table)
651 HASH_TABLE* var_hash_table;
654 register BUCKET_CONTENTS *tlist;
655 SHELL_VAR *var, **list;
656 int list_index, list_size;
658 list = (SHELL_VAR **)NULL;
659 for (i = list_index = list_size = 0; i < var_hash_table->nbuckets; i++)
661 tlist = get_hash_bucket (i, var_hash_table);
665 var = (SHELL_VAR *)tlist->data;
667 if (!function || (*function) (var))
669 if (list_index + 1 >= list_size)
670 list = (SHELL_VAR **)
671 xrealloc (list, (list_size += 20) * sizeof (SHELL_VAR *));
673 list[list_index++] = var;
674 list[list_index] = (SHELL_VAR *)NULL;
683 sort_variables (array)
686 qsort (array, array_len ((char **)array), sizeof (SHELL_VAR *), qsort_var_comp);
690 qsort_var_comp (var1, var2)
691 SHELL_VAR **var1, **var2;
695 if ((result = (*var1)->name[0] - (*var2)->name[0]) == 0)
696 result = strcmp ((*var1)->name, (*var2)->name);
701 /* Create a NULL terminated array of all the shell variables in TABLE. */
708 list = map_over ((Function *)NULL, table);
709 if (list /* && posixly_correct */)
710 sort_variables (list);
714 /* Create a NULL terminated array of all the shell variables. */
716 all_shell_variables ()
718 return (all_vars (shell_variables));
721 /* Create a NULL terminated array of all the shell functions. */
723 all_shell_functions ()
725 return (all_vars (shell_functions));
728 /* Print VARS to stdout in such a way that they can be read back in. */
730 print_var_list (list)
731 register SHELL_VAR **list;
734 register SHELL_VAR *var;
736 for (i = 0; list && (var = list[i]); i++)
737 if (!invisible_p (var))
738 print_assignment (var);
742 /* Print LIST (a linked list of shell variables) to stdout
743 by printing the names, without the values. Used to support the
746 print_vars_no_values (list)
747 register SHELL_VAR **list;
750 register SHELL_VAR *var;
752 for (i = 0; list && (var = list[i]); i++)
753 if (!invisible_p (var))
754 printf ("%s\n", var->name);
758 /* Print the value of a single SHELL_VAR. No newline is
759 output, but the variable is printed in such a way that
760 it can be read back in. */
762 print_assignment (var)
765 if (function_p (var) && var->value)
767 printf ("%s=", var->name);
768 print_var_function (var);
771 #if defined (ARRAY_VARS)
772 else if (array_p (var) && var->value)
773 print_array_assignment (var, 0);
774 #endif /* ARRAY_VARS */
777 printf ("%s=", var->name);
778 print_var_value (var, 1);
783 /* Print the value cell of VAR, a shell variable. Do not print
784 the name, nor leading/trailing newline. If QUOTE is non-zero,
785 and the value contains shell metacharacters, quote the value
786 in such a way that it can be read back in. */
788 print_var_value (var, quote)
796 if (quote && contains_shell_metas (var->value))
798 t = single_quote (var->value);
803 printf ("%s", var->value);
807 /* Print the function cell of VAR, a shell variable. Do not
808 print the name, nor leading/trailing newline. */
810 print_var_function (var)
813 if (function_p (var) && var->value)
814 printf ("%s", named_function_string ((char *)NULL, function_cell(var), 1));
817 #if defined (ARRAY_VARS)
819 print_array_assignment (var, quoted)
826 vstr = quoted_array_assignment_string (array_cell (var));
828 vstr = array_to_assignment_string (array_cell (var));
831 printf ("%s=%s\n", var->name, quoted ? "'()'" : "()");
834 printf ("%s=%s\n", var->name, vstr);
838 #endif /* ARRAY_VARS */
840 /* **************************************************************** */
842 /* Dynamic Variable Extension */
844 /* **************************************************************** */
848 These are variables whose values are generated anew each time they are
849 referenced. These are implemented using a pair of function pointers
850 in the struct variable: assign_func, which is called from bind_variable,
851 and dynamic_value, which is called from find_variable.
853 assign_func is called from bind_variable, if bind_variable discovers
854 that the variable being assigned to has such a function. The function
856 SHELL_VAR *temp = (*(entry->assign_func)) (entry, value)
857 and the (SHELL_VAR *)temp is returned as the value of bind_variable. It
858 is usually ENTRY (self).
860 dynamic_value is called from find_variable to return a `new' value for
861 the specified dynamic varible. If this function is NULL, the variable
862 is treated as a `normal' shell variable. If it is not, however, then
863 this function is called like this:
864 tempvar = (*(var->dynamic_value)) (var);
866 Sometimes `tempvar' will replace the value of `var'. Other times, the
867 shell will simply use the string value. Pretty object-oriented, huh?
869 Be warned, though: if you `unset' a special variable, it loses its
870 special meaning, even if you subsequently set it.
872 The special assignment code would probably have been better put in
873 subst.c: do_assignment, in the same style as
874 stupidly_hack_special_variables, but I wanted the changes as
875 localized as possible. */
877 /* The value of $SECONDS. This is the number of seconds since shell
878 invocation, or, the number of seconds since the last assignment + the
879 value of the last assignment. */
880 static long seconds_value_assigned;
883 assign_seconds (self, value)
887 seconds_value_assigned = strtol (value, (char **)NULL, 10);
888 shell_start_time = NOW;
896 time_t time_since_start;
899 time_since_start = NOW - shell_start_time;
900 p = itos((int) seconds_value_assigned + time_since_start);
904 var->attributes |= att_integer;
909 /* The random number seed. You can change this by setting RANDOM. */
910 static unsigned long rseed = 1;
911 static unsigned long last_random_value;
913 /* A linear congruential random number generator based on the ANSI
914 C standard. This one isn't very good (the values are alternately
915 odd and even, for example), but a more complicated one is overkill. */
917 /* Returns a pseudo-random number between 0 and 32767. */
921 rseed = rseed * 1103515245 + 12345;
922 return ((unsigned int)(rseed & 32767)); /* was % 32768 */
925 /* Set the random number generator seed to SEED. */
931 last_random_value = 0;
935 assign_random (self, value)
939 sbrand (atoi (value));
950 /* Reset for command and process substitution. */
951 if (subshell_environment)
952 sbrand (rseed + (int)(getpid() + NOW));
956 while (rv == (int)last_random_value);
958 last_random_value = rv;
963 var->attributes |= att_integer;
968 /* Function which returns the current line number. */
976 ln = executing_line_number ();
984 assign_lineno (var, value)
988 line_number = atoi (value);
992 #if defined (HISTORY)
999 p = itos (history_number ());
1006 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
1014 l = get_directory_stack ();
1015 a = word_list_to_array (l);
1016 dispose_array (array_cell (self));
1018 self->value = (char *)a;
1023 assign_dirstack (self, ind, value)
1028 set_dirstack_element (ind, 1, value);
1031 #endif /* PUSHD AND POPD && ARRAY_VARS */
1033 #if defined (ARRAY_VARS)
1034 /* We don't want to initialize the group set with a call to getgroups()
1035 unless we're asked to, but we only want to do it once. */
1043 static char **group_set = (char **)NULL;
1047 group_set = get_group_list (&ng);
1048 a = array_cell (self);
1049 for (i = 0; i < ng; i++)
1050 array_add_element (a, i, group_set[i]);
1054 #endif /* ARRAY_VARS */
1057 initialize_dynamic_variables ()
1061 v = bind_variable ("SECONDS", (char *)NULL);
1062 v->dynamic_value = get_seconds;
1063 v->assign_func = assign_seconds;
1065 v = bind_variable ("RANDOM", (char *)NULL);
1066 v->dynamic_value = get_random;
1067 v->assign_func = assign_random;
1069 v = bind_variable ("LINENO", (char *)NULL);
1070 v->dynamic_value = get_lineno;
1071 v->assign_func = assign_lineno;
1073 #if defined (HISTORY)
1074 v = bind_variable ("HISTCMD", (char *)NULL);
1075 v->dynamic_value = get_histcmd;
1076 v->assign_func = (DYNAMIC_FUNC *)NULL;
1079 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
1080 v = make_new_array_variable ("DIRSTACK");
1081 v->dynamic_value = get_dirstack;
1082 v->assign_func = assign_dirstack;
1083 #endif /* PUSHD_AND_POPD && ARRAY_VARS */
1085 #if defined (ARRAY_VARS)
1086 v = make_new_array_variable ("GROUPS");
1087 v->dynamic_value = get_groupset;
1088 v->assign_func = (DYNAMIC_FUNC *)NULL;
1089 v->attributes |= att_readonly;
1093 /* How to get a pointer to the shell variable or function named NAME.
1094 HASHED_VARS is a pointer to the hash table containing the list
1095 of interest (either variables or functions). */
1097 var_lookup (name, hashed_vars)
1099 HASH_TABLE *hashed_vars;
1101 BUCKET_CONTENTS *bucket;
1103 bucket = find_hash_item (name, hashed_vars);
1104 return (bucket ? (SHELL_VAR *)bucket->data : (SHELL_VAR *)NULL);
1107 /* Look up the variable entry named NAME. If SEARCH_TEMPENV is non-zero,
1108 then also search the temporarily built list of exported variables. */
1110 find_variable_internal (name, search_tempenv)
1114 SHELL_VAR *var = (SHELL_VAR *)NULL;
1116 /* If explicitly requested, first look in the temporary environment for
1117 the variable. This allows constructs such as "foo=x eval 'echo $foo'"
1118 to get the `exported' value of $foo. This happens if we are executing
1119 a function or builtin, or if we are looking up a variable in a
1120 "subshell environment". */
1121 if ((search_tempenv || subshell_environment) &&
1122 (temporary_env || builtin_env || function_env))
1123 var = find_tempenv_variable (name);
1126 var = var_lookup (name, shell_variables);
1129 return ((SHELL_VAR *)NULL);
1131 return (var->dynamic_value ? (*(var->dynamic_value)) (var) : var);
1134 /* Look up the variable entry named NAME. Returns the entry or NULL. */
1136 find_variable (name)
1139 return (find_variable_internal
1140 (name, (variable_context || this_shell_builtin || builtin_env)));
1143 /* Look up the function entry whose name matches STRING.
1144 Returns the entry or NULL. */
1146 find_function (name)
1149 return (var_lookup (name, shell_functions));
1152 /* Return the string value of a variable. Return NULL if the variable
1153 doesn't exist, or only has a function as a value. Don't cons a new
1154 string. This is a potential memory leak if the variable is found
1155 in the temporary environment. */
1157 get_string_value (var_name)
1162 var = find_variable (var_name);
1165 return (char *)NULL;
1166 #if defined (ARRAY_VARS)
1167 else if (array_p (var))
1168 return (array_reference (array_cell (var), 0));
1171 return (var->value);
1174 /* Create a local variable referenced by NAME. */
1176 make_local_variable (name)
1179 SHELL_VAR *new_var, *old_var;
1180 BUCKET_CONTENTS *elt;
1182 /* local foo; local foo; is a no-op. */
1183 old_var = find_variable (name);
1184 if (old_var && old_var->context == variable_context)
1187 elt = remove_hash_item (name, shell_variables);
1190 old_var = (SHELL_VAR *)elt->data;
1195 old_var = (SHELL_VAR *)NULL;
1197 /* If a variable does not already exist with this name, then
1198 just make a new one. */
1200 new_var = bind_variable (name, "");
1203 new_var = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
1205 new_var->name = savestring (name);
1206 new_var->value = xmalloc (1);
1207 new_var->value[0] = '\0';
1209 new_var->dynamic_value = (DYNAMIC_FUNC *)NULL;
1210 new_var->assign_func = (DYNAMIC_FUNC *)NULL;
1212 new_var->attributes = exported_p (old_var) ? att_exported : 0;
1214 new_var->prev_context = old_var;
1215 elt = add_hash_item (savestring (name), shell_variables);
1216 elt->data = (char *)new_var;
1219 new_var->context = variable_context;
1220 new_var->attributes |= att_local;
1223 if (variable_context >= local_variable_stack_size)
1225 int old_size = local_variable_stack_size;
1226 RESIZE_MALLOCED_BUFFER (have_local_variables, variable_context, 1,
1227 local_variable_stack_size, 8);
1228 bzero ((char *)have_local_variables + old_size,
1229 local_variable_stack_size - old_size);
1231 have_local_variables[variable_context] = 1; /* XXX */
1236 #if defined (ARRAY_VARS)
1238 make_local_array_variable (name)
1244 var = make_local_variable (name);
1245 array = new_array ();
1247 FREE (value_cell(var));
1248 var->value = (char *)array;
1249 var->attributes |= att_array;
1252 #endif /* ARRAY_VARS */
1254 /* Create a new shell variable with name NAME and add it to the hash table
1255 of shell variables. */
1258 make_new_variable (name)
1262 BUCKET_CONTENTS *elt;
1264 entry = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
1266 entry->attributes = 0;
1267 entry->name = savestring (name);
1268 entry->value = (char *)NULL;
1270 entry->dynamic_value = (DYNAMIC_FUNC *)NULL;
1271 entry->assign_func = (DYNAMIC_FUNC *)NULL;
1273 /* Always assume variables are to be made at toplevel!
1274 make_local_variable has the responsibilty of changing the
1275 variable context. */
1277 entry->prev_context = (SHELL_VAR *)NULL;
1279 elt = add_hash_item (savestring (name), shell_variables);
1280 elt->data = (char *)entry;
1285 #if defined (ARRAY_VARS)
1287 make_new_array_variable (name)
1293 entry = make_new_variable (name);
1294 array = new_array ();
1295 entry->value = (char *)array;
1296 entry->attributes |= att_array;
1302 make_variable_value (var, value)
1310 /* If this variable has had its type set to integer (via `declare -i'),
1311 then do expression evaluation on it and store the result. The
1312 functions in expr.c (evalexp and bind_int_variable) are responsible
1313 for turning off the integer flag if they don't want further
1315 if (integer_p (var))
1317 lval = evalexp (value, &expok);
1319 jump_to_top_level (DISCARD);
1320 retval = itos (lval);
1325 retval = savestring (value);
1328 retval = xmalloc (1);
1333 retval = (char *)NULL;
1338 /* Bind a variable NAME to VALUE. This conses up the name
1339 and value strings. */
1341 bind_variable (name, value)
1347 entry = var_lookup (name, shell_variables);
1351 entry = make_new_variable (name);
1352 entry->value = make_variable_value (entry, value);
1354 #if defined (ARRAY_VARS)
1355 else if (entry->assign_func && array_p (entry) == 0)
1357 else if (entry->assign_func)
1359 return ((*(entry->assign_func)) (entry, value));
1362 if (readonly_p (entry))
1364 report_error ("%s: readonly variable", name);
1368 /* Variables which are bound are visible. */
1369 entry->attributes &= ~att_invisible;
1371 newval = make_variable_value (entry, value);
1373 #if defined (ARRAY_VARS)
1374 /* XXX -- this bears looking at again -- XXX */
1375 /* If an existing array variable x is being assigned to with x=b or
1376 `read x' or something of that nature, silently convert it to
1377 x[0]=b or `read x[0]'. */
1378 if (array_p (entry))
1380 array_add_element (array_cell (entry), 0, newval);
1385 FREE (entry->value);
1386 entry->value = newval;
1389 FREE (entry->value);
1390 entry->value = newval;
1394 if (mark_modified_vars)
1395 entry->attributes |= att_exported;
1397 if (exported_p (entry))
1398 array_needs_making = 1;
1403 #if defined (ARRAY_VARS)
1404 /* Convert a shell variable to an array variable. The original value is
1405 saved as array[0]. */
1407 convert_var_to_array (var)
1413 oldval = value_cell (var);
1414 array = new_array ();
1415 array_add_element (array, 0, oldval);
1416 FREE (value_cell (var));
1417 var->value = (char *)array;
1418 var->attributes |= att_array;
1419 var->attributes &= ~att_invisible;
1424 /* Perform an array assignment name[ind]=value. If NAME already exists and
1425 is not an array, and IND is 0, perform name=value instead. If NAME exists
1426 and is not an array, and IND is not 0, convert it into an array with the
1427 existing value as name[0].
1429 If NAME does not exist, just create an array variable, no matter what
1430 IND's value may be. */
1432 bind_array_variable (name, ind, value)
1440 entry = var_lookup (name, shell_variables);
1442 if (entry == (SHELL_VAR *) 0)
1443 entry = make_new_array_variable (name);
1444 else if (readonly_p (entry))
1446 report_error ("%s: readonly variable", name);
1449 else if (array_p (entry) == 0)
1450 entry = convert_var_to_array (entry);
1452 /* ENTRY is an array variable, and ARRAY points to the value. */
1453 newval = make_variable_value (entry, value);
1454 if (entry->assign_func)
1455 (*entry->assign_func) (entry, ind, newval);
1457 array_add_element (array_cell (entry), ind, newval);
1463 /* Perform a compound assignment statement for array NAME, where VALUE is
1464 the text between the parens: NAME=( VALUE ) */
1466 assign_array_from_string (name, value)
1471 var = find_variable (name);
1473 var = make_new_array_variable (name);
1474 else if (readonly_p (var))
1476 report_error ("%s: readonly variable", name);
1477 return ((SHELL_VAR *)NULL);
1479 else if (array_p (var) == 0)
1480 var = convert_var_to_array (var);
1482 return (assign_array_var_from_string (var, value));
1486 assign_array_var_from_word_list (var, list)
1491 register WORD_LIST *l;
1494 for (a = array_cell (var), l = list, i = 0; l; l = l->next, i++)
1495 if (var->assign_func)
1496 (*var->assign_func) (var, i, l->word->word);
1498 array_add_element (a, i, l->word->word);
1502 /* Perform a compound array assignment: VAR->name=( VALUE ). The
1503 VALUE has already had the parentheses stripped. */
1505 assign_array_var_from_string (var, value)
1510 WORD_LIST *list, *nlist;
1511 char *w, *val, *nval;
1512 int ni, len, ind, last_ind;
1517 /* If this is called from declare_builtin, value[0] == '(' and
1518 strchr(value, ')') != 0. In this case, we need to extract
1519 the value from between the parens before going on. */
1520 if (*value == '(') /*)*/
1523 val = extract_array_assignment_list (value, &ni);
1530 /* Expand the value string into a list of words, performing all the
1531 shell expansions including word splitting. */
1533 /* First we split the string on whitespace, using the shell parser
1534 (ksh93 seems to do this). */
1535 list = parse_string_to_word_list (val, "array assign");
1536 /* Now that we've split it, perform the shell expansions on each
1537 word in the list. */
1538 nlist = list ? expand_words_shellexp (list) : (WORD_LIST *)NULL;
1539 dispose_words (list);
1541 nlist = expand_string (val, 0);
1547 a = array_cell (var);
1549 /* Now that we are ready to assign values to the array, kill the existing
1554 for (last_ind = 0, list = nlist; list; list = list->next)
1556 w = list->word->word;
1558 /* We have a word of the form [ind]=value */
1561 len = skipsubscript (w, 0);
1563 if (w[len] != ']' || w[len+1] != '=')
1565 nval = make_variable_value (var, w);
1566 if (var->assign_func)
1567 (*var->assign_func) (var, last_ind, nval);
1569 array_add_element (a, last_ind, nval);
1577 report_error ("%s: bad array subscript", w);
1581 if (ALL_ELEMENT_SUB (w[1]) && len == 2)
1583 report_error ("%s: cannot assign to non-numeric index", w);
1587 ind = array_expand_index (w + 1, len);
1590 report_error ("%s: bad array subscript", w);
1596 else /* No [ind]=value, just a stray `=' */
1602 if (integer_p (var))
1603 this_command_name = (char *)NULL; /* no command name for errors */
1604 nval = make_variable_value (var, val);
1605 if (var->assign_func)
1606 (*var->assign_func) (var, ind, nval);
1608 array_add_element (a, ind, nval);
1613 dispose_words (nlist);
1616 #endif /* ARRAY_VARS */
1618 /* Dispose of the information attached to VAR. */
1620 dispose_variable (var)
1626 if (function_p (var))
1627 dispose_command (function_cell (var));
1628 #if defined (ARRAY_VARS)
1629 else if (array_p (var))
1630 dispose_array (array_cell (var));
1633 FREE (value_cell (var));
1637 if (exported_p (var))
1638 array_needs_making = 1;
1643 #if defined (ARRAY_VARS)
1644 /* This function is called with SUB pointing to just after the beginning
1645 `[' of an array subscript. */
1647 unbind_array_element (var, sub)
1654 len = skipsubscript (sub, 0);
1655 if (sub[len] != ']' || len == 0)
1657 builtin_error ("%s[%s: bad array subscript", var->name, sub);
1662 if (ALL_ELEMENT_SUB (sub[0]) && sub[1] == 0)
1664 makunbound (var->name, shell_variables);
1667 ind = array_expand_index (sub, len+1);
1670 builtin_error ("[%s]: bad array subscript", sub);
1673 ae = array_delete_element (array_cell (var), ind);
1675 destroy_array_element (ae);
1680 /* Unset the variable referenced by NAME. */
1682 unbind_variable (name)
1687 var = find_variable (name);
1691 /* This function should never be called with an array variable name. */
1692 #if defined (ARRAY_VARS)
1693 if (array_p (var) == 0 && var->value)
1699 var->value = (char *)NULL;
1702 makunbound (name, shell_variables);
1707 /* Make the variable associated with NAME go away. HASH_LIST is the
1708 hash table from which this variable should be deleted (either
1709 shell_variables or shell_functions).
1710 Returns non-zero if the variable couldn't be found. */
1712 makunbound (name, hash_list)
1714 HASH_TABLE *hash_list;
1716 BUCKET_CONTENTS *elt, *new_elt;
1717 SHELL_VAR *old_var, *new_var;
1720 elt = remove_hash_item (name, hash_list);
1725 old_var = (SHELL_VAR *)elt->data;
1726 new_var = old_var->prev_context;
1728 if (old_var && exported_p (old_var))
1729 array_needs_making++;
1731 /* If we're unsetting a local variable and we're still executing inside
1732 the function, just mark the variable as invisible.
1733 kill_all_local_variables will clean it up later. This must be done
1734 so that if the variable is subsequently assigned a new value inside
1735 the function, the `local' attribute is still present. We also need
1736 to add it back into the correct hash table. */
1737 if (old_var && local_p (old_var) && variable_context == old_var->context)
1739 old_var->attributes |= att_invisible;
1740 new_elt = add_hash_item (savestring (old_var->name), hash_list);
1741 new_elt->data = (char *)old_var;
1742 stupidly_hack_special_variables (old_var->name);
1750 /* Has to be a variable, functions don't have previous contexts. */
1751 new_elt = add_hash_item (savestring (new_var->name), hash_list);
1752 new_elt->data = (char *)new_var;
1754 if (exported_p (new_var))
1755 set_auto_export (new_var);
1758 /* Have to save a copy of name here, because it might refer to
1759 old_var->name. If so, stupidly_hack_special_variables will
1760 reference freed memory. */
1761 t = savestring (name);
1766 dispose_variable (old_var);
1767 stupidly_hack_special_variables (t);
1772 #ifdef INCLUDE_UNUSED
1773 /* Remove the variable with NAME if it is a local variable in the
1776 kill_local_variable (name)
1781 temp = find_variable (name);
1782 if (temp && temp->context == variable_context)
1784 makunbound (name, shell_variables);
1791 /* Get rid of all of the variables in the current context. */
1793 variable_in_context (var)
1796 return (var && var->context == variable_context);
1800 kill_all_local_variables ()
1802 register int i, pass;
1803 register SHELL_VAR *var, **list;
1804 HASH_TABLE *varlist;
1806 /* If HAVE_LOCAL_VARIABLES == 0, it means that we don't have any local
1807 variables at all. If VARIABLE_CONTEXT >= LOCAL_VARIABLE_STACK_SIZE,
1808 it means that we have some local variables, but not in this variable
1809 context (level of function nesting). Also, if
1810 HAVE_LOCAL_VARIABLES[VARIABLE_CONTEXT] == 0, we have no local variables
1812 if (have_local_variables == 0 ||
1813 variable_context >= local_variable_stack_size ||
1814 have_local_variables[variable_context] == 0)
1817 for (pass = 0; pass < 2; pass++)
1819 varlist = pass ? shell_functions : shell_variables;
1821 list = map_over (variable_in_context, varlist);
1825 for (i = 0; var = list[i]; i++)
1827 var->attributes &= ~att_local;
1828 makunbound (var->name, varlist);
1834 have_local_variables[variable_context] = 0; /* XXX */
1838 free_variable_hash_data (data)
1841 SHELL_VAR *var, *prev;
1843 var = (SHELL_VAR *)data;
1846 prev = var->prev_context;
1847 dispose_variable (var);
1852 /* Delete the entire contents of the hash table. */
1854 delete_all_variables (hashed_vars)
1855 HASH_TABLE *hashed_vars;
1857 flush_hash_table (hashed_vars, free_variable_hash_data);
1861 new_shell_variable (name)
1866 var = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
1868 bzero ((char *)var, sizeof (SHELL_VAR));
1869 var->name = savestring (name);
1873 /* Do a function binding to a variable. You pass the name and
1874 the command to bind to. This conses the name and command. */
1876 bind_function (name, value)
1882 entry = find_function (name);
1885 BUCKET_CONTENTS *elt;
1887 elt = add_hash_item (savestring (name), shell_functions);
1889 elt->data = (char *)new_shell_variable (name);
1890 entry = (SHELL_VAR *)elt->data;
1891 entry->dynamic_value = entry->assign_func = (DYNAMIC_FUNC *)NULL;
1893 /* Functions are always made at the top level. This allows a
1894 function to define another function (like autoload). */
1899 dispose_command ((COMMAND *)entry->value);
1901 entry->value = value ? (char *)copy_command (value) : (char *)NULL;
1902 entry->attributes |= att_function;
1904 if (mark_modified_vars)
1905 entry->attributes |= att_exported;
1907 entry->attributes &= ~att_invisible; /* Just to be sure */
1909 if (exported_p (entry))
1910 array_needs_making = 1;
1915 #ifdef INCLUDE_UNUSED
1916 /* Copy VAR to a new data structure and return that structure. */
1921 SHELL_VAR *copy = (SHELL_VAR *)NULL;
1925 copy = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
1927 copy->attributes = var->attributes;
1928 copy->name = savestring (var->name);
1930 if (function_p (var))
1931 copy->value = (char *)copy_command (function_cell (var));
1932 #if defined (ARRAY_VARS)
1933 else if (array_p (var))
1934 copy->value = (char *)dup_array (array_cell (var));
1936 else if (value_cell (var))
1937 copy->value = savestring (value_cell (var));
1939 copy->value = (char *)NULL;
1941 copy->dynamic_value = var->dynamic_value;
1942 copy->assign_func = var->assign_func;
1944 copy->context = var->context;
1946 /* Don't bother copying previous contexts along with this variable. */
1947 copy->prev_context = (SHELL_VAR *)NULL;
1953 #define FIND_OR_MAKE_VARIABLE(name, entry) \
1956 entry = find_variable (name); \
1959 entry = bind_variable (name, ""); \
1960 if (!no_invisible_vars) entry->attributes |= att_invisible; \
1965 /* Make the variable associated with NAME be readonly.
1966 If NAME does not exist yet, create it. */
1968 set_var_read_only (name)
1973 FIND_OR_MAKE_VARIABLE (name, entry);
1974 entry->attributes |= att_readonly;
1977 #ifdef INCLUDE_UNUSED
1978 /* Make the function associated with NAME be readonly.
1979 If NAME does not exist, we just punt, like auto_export code below. */
1981 set_func_read_only (name)
1986 entry = find_function (name);
1988 entry->attributes |= att_readonly;
1991 /* Make the variable associated with NAME be auto-exported.
1992 If NAME does not exist yet, create it. */
1994 set_var_auto_export (name)
1999 FIND_OR_MAKE_VARIABLE (name, entry);
2000 set_auto_export (entry);
2003 /* Make the function associated with NAME be auto-exported. */
2005 set_func_auto_export (name)
2010 entry = find_function (name);
2012 set_auto_export (entry);
2016 #if defined (ARRAY_VARS)
2017 /* This function assumes s[i] == '['; returns with s[ret] == ']' if
2018 an array subscript is correctly parsed. */
2020 skipsubscript (s, i)
2026 for (count = 1; count && (c = s[++i]); )
2035 #endif /* ARRAY_VARS */
2037 /* Returns non-zero if STRING is an assignment statement. The returned value
2038 is the index of the `=' sign. */
2043 register int c, newi, indx;
2045 c = string[indx = 0];
2047 if (legal_variable_starter (c) == 0)
2050 while (c = string[indx])
2052 /* The following is safe. Note that '=' at the start of a word
2053 is not an assignment statement. */
2057 #if defined (ARRAY_VARS)
2060 newi = skipsubscript (string, indx);
2061 if (string[newi++] != ']')
2063 return ((string[newi] == '=') ? newi : 0);
2065 #endif /* ARRAY_VARS */
2067 /* Variable names in assignment statements may contain only letters,
2069 if (legal_variable_char (c) == 0)
2083 return (invisible_p (var) == 0);
2087 _visible_names (table)
2092 list = map_over (visible_var, table);
2094 if (list /* && posixly_correct */)
2095 sort_variables (list);
2101 all_visible_variables ()
2103 return (_visible_names (shell_variables));
2107 all_visible_functions ()
2109 return (_visible_names (shell_functions));
2112 #endif /* READLINE */
2114 /* Return non-zero if the variable VAR is visible and exported. Array
2115 variables cannot be exported. */
2117 visible_and_exported (var)
2120 return (invisible_p (var) == 0 && exported_p (var));
2123 /* Make an array of assignment statements from the hash table
2124 HASHED_VARS which contains SHELL_VARs. Only visible, exported
2125 variables are eligible. */
2127 make_var_array (hashed_vars)
2128 HASH_TABLE *hashed_vars;
2130 register int i, list_index;
2131 register SHELL_VAR *var;
2132 char **list, *value;
2135 vars = map_over (visible_and_exported, hashed_vars);
2138 return (char **)NULL;
2140 list = (char **)xmalloc ((1 + array_len ((char **)vars)) * sizeof (char *));
2142 for (i = 0, list_index = 0; var = vars[i]; i++)
2144 if (function_p (var))
2145 value = named_function_string ((char *)NULL, function_cell (var), 0);
2146 #if defined (ARRAY_VARS)
2147 else if (array_p (var))
2149 value = array_to_assignment_string (array_cell (var));
2151 continue; /* XXX array vars cannot yet be exported */
2155 value = value_cell (var);
2159 int name_len, value_len;
2162 name_len = strlen (var->name);
2163 value_len = strlen (value);
2164 p = list[list_index] = xmalloc (2 + name_len + value_len);
2165 strcpy (p, var->name);
2167 strcpy (p + name_len + 1, value);
2169 #if defined (ARRAY_VARS)
2177 list[list_index] = (char *)NULL;
2181 /* Add STRING to the array of foo=bar strings that we already
2182 have to add to the environment. */
2184 assign_in_env (string)
2188 char *name, *temp, *value;
2193 offset = assignment (string);
2194 name = savestring (string);
2195 value = (char *)NULL;
2197 if (name[offset] == '=')
2201 var = find_variable (name);
2202 if (var && readonly_p (var))
2204 report_error ("%s: readonly variable", name);
2208 temp = name + offset + 1;
2209 temp = (strchr (temp, '~') != 0) ? bash_tilde_expand (temp) : savestring (temp);
2211 list = expand_string_unsplit (temp, 0);
2212 value = string_list (list);
2215 dispose_words (list);
2220 nlen = strlen (name);
2221 vlen = value ? strlen (value) : 0;
2222 temp = xmalloc (2 + nlen + vlen);
2223 strcpy (temp, name);
2225 temp[nlen + 1] = '\0';
2229 strcpy (temp + nlen + 1, value);
2234 if (temporary_env == 0)
2236 temporary_env = (char **)xmalloc (sizeof (char *));
2237 temporary_env [0] = (char *)NULL;
2240 size = array_len (temporary_env);
2241 temporary_env = (char **)
2242 xrealloc (temporary_env, (size + 2) * (sizeof (char *)));
2244 temporary_env[size] = temp;
2245 temporary_env[size + 1] = (char *)NULL;
2246 array_needs_making = 1;
2248 if (echo_command_at_execute)
2250 /* The Korn shell prints the `+ ' in front of assignment statements,
2252 fprintf (stderr, "%s%s\n", indirection_level_string (), temp);
2259 /* Search for NAME in ARRAY, an array of strings in the same format as the
2260 environment array (i.e, name=value). If NAME is present, make a new
2261 variable and return it. Otherwise, return NULL. */
2263 find_name_in_env_array (name, array)
2270 return ((SHELL_VAR *)NULL);
2272 for (i = 0, l = strlen (name); array[i]; i++)
2274 if (STREQN (array[i], name, l) && array[i][l] == '=')
2279 /* This is a potential memory leak. The code should really save
2280 the created variables in some auxiliary data structure, which
2281 can be disposed of at the appropriate time. */
2282 temp = new_shell_variable (name);
2283 w = array[i] + l + 1;
2285 temp->value = *w ? savestring (w) : (char *)NULL;
2287 temp->attributes = att_exported|att_tempvar;
2289 temp->prev_context = (SHELL_VAR *)NULL;
2291 temp->dynamic_value = temp->assign_func = (DYNAMIC_FUNC *)NULL;
2296 return ((SHELL_VAR *)NULL);
2299 /* Find a variable in the temporary environment that is named NAME.
2300 The temporary environment can be either the environment provided
2301 to a simple command, or the environment provided to a shell function.
2302 We only search the function environment if we are currently executing
2303 a shell function body (variable_context > 0). Return a consed variable,
2304 or NULL if not found. */
2306 find_tempenv_variable (name)
2311 var = (SHELL_VAR *)NULL;
2314 var = find_name_in_env_array (name, temporary_env);
2316 /* We don't check this_shell_builtin because the command that needs the
2317 value from builtin_env may be a disk command run inside a script run
2318 with `.' and a temporary env. */
2319 if (!var && builtin_env)
2320 var = find_name_in_env_array (name, builtin_env);
2322 if (!var && variable_context && function_env)
2323 var = find_name_in_env_array (name, function_env);
2328 /* Free the storage allocated to the string array pointed to by ARRAYP, and
2329 make that variable have a null pointer as a value. */
2331 dispose_temporary_vars (arrayp)
2337 free_array (*arrayp);
2338 *arrayp = (char **)NULL;
2339 array_needs_making = 1;
2342 /* Free the storage used in the variable array for temporary
2343 environment variables. */
2345 dispose_used_env_vars ()
2347 dispose_temporary_vars (&temporary_env);
2350 /* Free the storage used for temporary environment variables given to
2351 commands when executing inside of a function body. */
2353 dispose_function_env ()
2355 dispose_temporary_vars (&function_env);
2358 /* Free the storage used for temporary environment variables given to
2359 commands when executing a builtin command such as "source". */
2361 dispose_builtin_env ()
2363 dispose_temporary_vars (&builtin_env);
2366 /* Take all of the shell variables in ENV_ARRAY and make shell variables
2367 from them at the current variable context. */
2369 merge_env_array (env_array)
2379 for (i = 0; env_array[i]; i++)
2381 l = assignment (env_array[i]);
2382 name = env_array[i];
2383 val = env_array[i] + l + 1;
2385 temp = bind_variable (name, val);
2391 merge_temporary_env ()
2393 merge_env_array (temporary_env);
2397 merge_builtin_env ()
2399 merge_env_array (builtin_env);
2402 /* Add ENVSTR to the end of the exported environment, EXPORT_ENV. */
2403 #define add_to_export_env(envstr,do_alloc) \
2406 if (export_env_index >= (export_env_size - 1)) \
2408 export_env_size += 16; \
2409 export_env = (char **)xrealloc (export_env, export_env_size * sizeof (char *)); \
2411 export_env[export_env_index++] = (do_alloc) ? savestring (envstr) : envstr; \
2412 export_env[export_env_index] = (char *)NULL; \
2415 #define ISFUNCTION(s, o) ((s[o + 1] == '(') && (s[o + 2] == ')'))
2417 /* Add ASSIGN to EXPORT_ENV, or supercede a previous assignment in the
2418 array with the same left-hand side. Return the new EXPORT_ENV. */
2420 add_or_supercede_exported_var (assign, do_alloc)
2427 equal_offset = assignment (assign);
2428 if (equal_offset == 0)
2429 return (export_env);
2431 /* If this is a function, then only supercede the function definition.
2432 We do this by including the `=(' in the comparison. */
2433 if (assign[equal_offset + 1] == '(')
2436 for (i = 0; i < export_env_index; i++)
2438 if (STREQN (assign, export_env[i], equal_offset + 1))
2440 free (export_env[i]);
2441 export_env[i] = do_alloc ? savestring (assign) : assign;
2442 return (export_env);
2445 add_to_export_env (assign, do_alloc);
2446 return (export_env);
2449 /* Make the environment array for the command about to be executed, if the
2450 array needs making. Otherwise, do nothing. If a shell action could
2451 change the array that commands receive for their environment, then the
2452 code should `array_needs_making++'. */
2454 maybe_make_export_env ()
2457 register char **temp_array;
2460 if (array_needs_making)
2463 free_array_members (export_env);
2465 /* Make a guess based on how many shell variables and functions we
2466 have. Since there will always be array variables, and array
2467 variables are not (yet) exported, this will always be big enough
2468 for the exported variables and functions, without any temporary
2469 or function environments. */
2470 new_size = shell_variables->nentries + shell_functions->nentries + 1;
2471 if (new_size > export_env_size)
2473 export_env_size = new_size;
2474 export_env = (char **)xrealloc (export_env, export_env_size * sizeof (char *));
2476 export_env[export_env_index = 0] = (char *)NULL;
2478 temp_array = make_var_array (shell_variables);
2481 for (i = 0; temp_array[i]; i++)
2482 add_to_export_env (temp_array[i], 0);
2486 temp_array = make_var_array (shell_functions);
2489 for (i = 0; temp_array[i]; i++)
2490 add_to_export_env (temp_array[i], 0);
2495 for (i = 0; function_env[i]; i++)
2496 export_env = add_or_supercede_exported_var (function_env[i], 1);
2499 for (i = 0; temporary_env[i]; i++)
2500 export_env = add_or_supercede_exported_var (temporary_env[i], 1);
2503 /* If we changed the array, then sort it alphabetically. */
2504 if (posixly_correct == 0 && (temporary_env || function_env))
2505 sort_char_array (export_env);
2508 array_needs_making = 0;
2512 /* We always put _ in the environment as the name of this command. */
2514 put_command_name_into_env (command_name)
2519 dummy = xmalloc (4 + strlen (command_name));
2521 /* These three statements replace a call to sprintf */
2524 strcpy (dummy + 2, command_name);
2525 export_env = add_or_supercede_exported_var (dummy, 0);
2528 #if 0 /* UNUSED -- it caused too many problems */
2530 put_gnu_argv_flags_into_env (pid, flags_string)
2540 fl = strlen (flags_string);
2542 dummy = xmalloc (l + fl + 30);
2544 strcpy (dummy + 1, pbuf);
2545 strcpy (dummy + 1 + l, "_GNU_nonoption_argv_flags_");
2546 dummy[l + 27] = '=';
2547 strcpy (dummy + l + 28, flags_string);
2551 export_env = add_or_supercede_exported_var (dummy, 0);
2555 /* Return a string denoting what our indirection level is. */
2556 static char indirection_string[100];
2559 indirection_level_string ()
2564 indirection_string[0] = '\0';
2565 ps4 = get_string_value ("PS4");
2567 if (ps4 == 0 || *ps4 == '\0')
2568 return (indirection_string);
2570 ps4 = decode_prompt_string (ps4);
2572 for (i = 0; *ps4 && i < indirection_level && i < 99; i++)
2573 indirection_string[i] = *ps4;
2575 for (j = 1; *ps4 && ps4[j] && i < 99; i++, j++)
2576 indirection_string[i] = ps4[j];
2578 indirection_string[i] = '\0';
2580 return (indirection_string);
2583 /*************************************************
2585 * Functions to manage special variables *
2587 *************************************************/
2589 /* Extern declarations for variables this code has to manage. */
2590 extern int eof_encountered, eof_encountered_limit, ignoreeof;
2592 #if defined (READLINE)
2593 extern int no_line_editing;
2594 extern int hostname_list_initialized;
2597 /* An alist of name.function for each special variable. Most of the
2598 functions don't do much, and in fact, this would be faster with a
2599 switch statement, but by the end of this file, I am sick of switch
2602 #define SET_INT_VAR(name, intvar) intvar = find_variable (name) != 0
2604 struct name_and_function {
2606 VFunction *function;
2607 } special_vars[] = {
2608 { "PATH", sv_path },
2609 { "MAIL", sv_mail },
2610 { "MAILPATH", sv_mail },
2611 { "MAILCHECK", sv_mail },
2613 { "POSIXLY_CORRECT", sv_strict_posix },
2614 { "GLOBIGNORE", sv_globignore },
2616 /* Variables which only do something special when READLINE is defined. */
2617 #if defined (READLINE)
2618 { "TERM", sv_terminal },
2619 { "TERMCAP", sv_terminal },
2620 { "TERMINFO", sv_terminal },
2621 { "HOSTFILE", sv_hostfile },
2622 #endif /* READLINE */
2624 /* Variables which only do something special when HISTORY is defined. */
2625 #if defined (HISTORY)
2626 { "HISTIGNORE", sv_histignore },
2627 { "HISTSIZE", sv_histsize },
2628 { "HISTFILESIZE", sv_histsize },
2629 { "HISTCONTROL", sv_history_control },
2630 # if defined (BANG_HISTORY)
2631 { "histchars", sv_histchars },
2632 # endif /* BANG_HISTORY */
2633 #endif /* HISTORY */
2635 { "IGNOREEOF", sv_ignoreeof },
2636 { "ignoreeof", sv_ignoreeof },
2638 { "OPTIND", sv_optind },
2639 { "OPTERR", sv_opterr },
2641 { "TEXTDOMAIN", sv_locale },
2642 { "TEXTDOMAINDIR", sv_locale },
2643 { "LC_ALL", sv_locale },
2644 { "LC_COLLATE", sv_locale },
2645 { "LC_CTYPE", sv_locale },
2646 { "LC_MESSAGES", sv_locale },
2647 { "LANG", sv_locale },
2649 #if defined (HAVE_TZSET) && defined (PROMPT_STRING_DECODE)
2653 { (char *)0, (VFunction *)0 }
2656 /* The variable in NAME has just had its state changed. Check to see if it
2657 is one of the special ones where something special happens. */
2659 stupidly_hack_special_variables (name)
2664 for (i = 0; special_vars[i].name; i++)
2666 if (STREQ (special_vars[i].name, name))
2668 (*(special_vars[i].function)) (name);
2674 /* What to do just after the PATH variable has changed. */
2680 flush_hashed_filenames ();
2683 /* What to do just after one of the MAILxxxx variables has changed. NAME
2684 is the name of the variable. This is called with NAME set to one of
2685 MAIL, MAILCHECK, or MAILPATH. */
2690 /* If the time interval for checking the files has changed, then
2691 reset the mail timer. Otherwise, one of the pathname vars
2692 to the users mailbox has changed, so rebuild the array of
2694 if (name[4] == 'C') /* if (strcmp (name, "MAILCHECK") == 0) */
2695 reset_mail_timer ();
2699 remember_mail_dates ();
2703 /* What to do when GLOBIGNORE changes. */
2705 sv_globignore (name)
2708 setup_glob_ignore (name);
2711 #if defined (READLINE)
2712 /* What to do just after one of the TERMxxx variables has changed.
2713 If we are an interactive shell, then try to reset the terminal
2714 information in readline. */
2719 if (interactive_shell && no_line_editing == 0)
2720 rl_reset_terminal (get_string_value ("TERM"));
2727 hostname_list_initialized = 0;
2729 #endif /* READLINE */
2731 #if defined (HISTORY)
2732 /* What to do after the HISTSIZE or HISTFILESIZE variables change.
2733 If there is a value for this HISTSIZE (and it is numeric), then stifle
2734 the history. Otherwise, if there is NO value for this variable,
2735 unstifle the history. If name is HISTFILESIZE, and its value is
2736 numeric, truncate the history file to hold no more than that many
2745 temp = get_string_value (name);
2749 if (legal_number (temp, &num))
2753 stifle_history (num);
2754 num = where_history ();
2755 if (history_lines_this_session > num)
2756 history_lines_this_session = num;
2760 history_truncate_file (get_string_value ("HISTFILE"), (int)num);
2761 if (num <= history_lines_in_file)
2762 history_lines_in_file = num;
2766 else if (name[4] == 'S')
2767 unstifle_history ();
2770 /* What to do after the HISTIGNORE variable changes. */
2772 sv_histignore (name)
2775 setup_history_ignore (name);
2778 /* What to do after the HISTCONTROL variable changes. */
2780 sv_history_control (name)
2785 history_control = 0;
2786 temp = get_string_value (name);
2788 if (temp && *temp && STREQN (temp, "ignore", 6))
2790 if (temp[6] == 's') /* ignorespace */
2791 history_control = 1;
2792 else if (temp[6] == 'd') /* ignoredups */
2793 history_control = 2;
2794 else if (temp[6] == 'b') /* ignoreboth */
2795 history_control = 3;
2799 #if defined (BANG_HISTORY)
2800 /* Setting/unsetting of the history expansion character. */
2807 temp = get_string_value (name);
2810 history_expansion_char = *temp;
2811 if (temp[0] && temp[1])
2813 history_subst_char = temp[1];
2815 history_comment_char = temp[2];
2820 history_expansion_char = '!';
2821 history_subst_char = '^';
2822 history_comment_char = '#';
2825 #endif /* BANG_HISTORY */
2826 #endif /* HISTORY */
2828 #if defined (HAVE_TZSET) && defined (PROMPT_STRING_DECODE)
2837 /* If the variable exists, then the value of it can be the number
2838 of times we actually ignore the EOF. The default is small,
2839 (smaller than csh, anyway). */
2847 eof_encountered = 0;
2849 tmp_var = find_variable (name);
2850 ignoreeof = tmp_var != 0;
2851 temp = tmp_var ? value_cell (tmp_var) : (char *)NULL;
2853 eof_encountered_limit = (*temp && all_digits (temp)) ? atoi (temp) : 10;
2854 set_shellopts (); /* make sure `ignoreeof' is/is not in $SHELLOPTS */
2864 tt = get_string_value ("OPTIND");
2869 /* According to POSIX, setting OPTIND=1 resets the internal state
2871 if (s < 0 || s == 1)
2885 tt = get_string_value ("OPTERR");
2886 sh_opterr = (tt && *tt) ? atoi (tt) : 1;
2890 sv_strict_posix (name)
2893 SET_INT_VAR (name, posixly_correct);
2894 posix_initialize (posixly_correct);
2895 #if defined (READLINE)
2896 if (interactive_shell)
2897 posix_readline_initialize (posixly_correct);
2898 #endif /* READLINE */
2899 set_shellopts (); /* make sure `posix' is/is not in $SHELLOPTS */
2908 v = get_string_value (name);
2909 if (name[0] == 'L' && name[1] == 'A') /* LANG */
2912 set_locale_var (name, v); /* LC_*, TEXTDOMAIN* */
2915 #if defined (ARRAY_VARS)
2917 set_pipestatus_array (ps)
2925 v = find_variable ("PIPESTATUS");
2927 v = make_new_array_variable ("PIPESTATUS");
2928 if (array_p (v) == 0)
2929 return; /* Do nothing if not an array variable. */
2933 for (i = 0; ps[i] != -1; i++)
2936 array_add_element (a, i, t);
2943 set_pipestatus_from_exit (s)
2946 #if defined (ARRAY_VARS)
2947 static int v[2] = { 0, -1 };
2950 set_pipestatus_array (v);