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"
26 #if defined (HAVE_UNISTD_H)
37 #include "execute_cmd.h"
38 #include "mailcheck.h"
41 #include "builtins/common.h"
42 #include <tilde/tilde.h>
45 # include "bashhist.h"
48 /* Variables used here and defined in other files. */
49 extern int posixly_correct;
50 extern int variable_context, line_number;
51 extern int interactive, interactive_shell, login_shell;
52 extern int subshell_environment, indirection_level;
53 extern int build_version, patch_level;
54 extern char *dist_version, *release_status;
55 extern char *shell_name;
56 extern char *primary_prompt, *secondary_prompt;
57 extern char *current_host_name;
58 extern Function *this_shell_builtin;
59 extern char *this_command_name;
60 extern time_t shell_start_time;
62 /* The list of shell variables that the user has created, or that came from
64 HASH_TABLE *shell_variables = (HASH_TABLE *)NULL;
66 /* The list of shell functions that the user has created, or that came from
68 HASH_TABLE *shell_functions = (HASH_TABLE *)NULL;
70 /* The current variable context. This is really a count of how deep into
71 executing functions we are. */
72 int variable_context = 0;
74 /* The array of shell assignments which are made only in the environment
75 for a single command. */
76 char **temporary_env = (char **)NULL;
78 /* The array of shell assignments which are in the environment for the
79 execution of a shell function. */
80 char **function_env = (char **)NULL;
82 /* The array of shell assignments which are made only in the environment
83 for the execution of a shell builtin command which may cause more than
84 one command to be executed (e.g., "source"). */
85 char **builtin_env = (char **)NULL;
87 /* Some funky variables which are known about specially. Here is where
88 "$*", "$1", and all the cruft is kept. */
89 char *dollar_vars[10];
90 WORD_LIST *rest_of_args = (WORD_LIST *)NULL;
92 /* The value of $$. */
93 int dollar_dollar_pid;
95 /* An array which is passed to commands as their environment. It is
96 manufactured from the overlap of the initial environment and the
97 shell variables that are marked for export. */
98 char **export_env = (char **)NULL;
100 /* Non-zero means that we have to remake EXPORT_ENV. */
101 int array_needs_making = 1;
103 /* The number of times BASH has been executed. This is set
104 by initialize_variables (). */
107 static char *have_local_variables;
108 static int local_variable_stack_size;
110 /* Some forward declarations. */
111 static void uidset ();
112 static void initialize_dynamic_variables ();
113 static void make_vers_array ();
114 static void sbrand (); /* set bash random number generator. */
115 static int qsort_var_comp ();
117 /* Make VAR be auto-exported. VAR is a pointer to a SHELL_VAR. */
118 #define set_auto_export(var) \
119 do { var->attributes |= att_exported; array_needs_making = 1; } while (0)
121 /* Initialize the shell variables from the current environment. */
123 initialize_shell_variables (env, no_functions)
125 int no_functions; /* If set, don't import functions from ENV. */
127 char *name, *string, *temp_string;
128 int c, char_index, string_index, string_length;
131 if (!shell_variables)
132 shell_variables = make_hash_table (0);
134 if (!shell_functions)
135 shell_functions = make_hash_table (0);
137 for (string_index = 0; string = env[string_index++]; )
141 string_length = strlen (string);
142 name = xmalloc (1 + string_length);
144 while ((c = *string++) && c != '=')
145 name[char_index++] = c;
147 name[char_index] = '\0';
149 /* If exported function, define it now. */
150 if (no_functions == 0 && STREQN ("() {", string, 4))
152 temp_string = xmalloc (3 + string_length + strlen (name));
153 sprintf (temp_string, "%s %s", name, string);
155 parse_and_execute (temp_string, name, 0);
157 if (name[char_index - 1] == ')')
158 name[char_index - 2] = '\0';
160 if (temp_var = find_function (name))
162 temp_var->attributes |= (att_exported | att_imported);
163 array_needs_making = 1;
166 report_error ("error importing function definition for `%s'", name);
168 #if defined (ARRAY_VARS)
170 /* Array variables may not yet be exported. */
171 else if (*string == '(' && string[1] == '[' && strchr (string, ')'))
174 temp_string = extract_array_assignment_list (string, &string_length);
175 temp_var = assign_array_from_string (name, temp_string);
177 temp_var->attributes |= (att_exported | att_imported);
178 array_needs_making = 1;
184 temp_var = bind_variable (name, string);
185 temp_var->attributes |= (att_exported | att_imported);
186 array_needs_making = 1;
191 /* If we got PWD from the environment, update our idea of the current
192 working directory. In any case, make sure that PWD exists before
193 checking it. It is possible for getcwd () to fail on shell startup,
194 and in that case, PWD would be undefined. */
195 temp_var = find_variable ("PWD");
196 if (temp_var && imported_p (temp_var) &&
197 (temp_string = value_cell (temp_var)) &&
198 same_file (temp_string, ".", (struct stat *)NULL, (struct stat *)NULL))
199 set_working_directory (temp_string);
202 temp_string = get_working_directory ("shell-init");
205 bind_variable ("PWD", temp_string);
210 /* Set up initial value of $_ */
211 temp_var = bind_variable ("_", dollar_vars[0]);
213 /* Remember this pid. */
214 dollar_dollar_pid = (int)getpid ();
216 /* Now make our own defaults in case the vars that we think are
217 important are missing. */
218 temp_var = set_if_not ("PATH", DEFAULT_PATH_VALUE);
219 set_auto_export (temp_var);
221 temp_var = set_if_not ("TERM", "dumb");
222 set_auto_export (temp_var);
224 /* set up the prompts. */
225 if (interactive_shell)
227 set_if_not ("PS1", primary_prompt);
228 set_if_not ("PS2", secondary_prompt);
230 set_if_not ("PS4", "+ ");
232 /* Don't allow IFS to be imported from the environment. */
233 temp_var = bind_variable ("IFS", " \t\n");
235 /* Magic machine types. Pretty convenient. */
236 temp_var = bind_variable ("HOSTTYPE", HOSTTYPE);
237 set_auto_export (temp_var);
238 temp_var = bind_variable ("OSTYPE", OSTYPE);
239 set_auto_export (temp_var);
240 temp_var = bind_variable ("MACHTYPE", MACHTYPE);
241 set_auto_export (temp_var);
242 temp_var = bind_variable ("HOSTNAME", current_host_name);
243 set_auto_export (temp_var);
245 /* Default MAILCHECK for interactive shells. Defer the creation of a
246 default MAILPATH until the startup files are read, because MAIL
247 names a mail file if MAILCHECK is not set, and we should provide a
248 default only if neither is set. */
249 if (interactive_shell)
250 set_if_not ("MAILCHECK", "60");
252 /* Do some things with shell level. */
253 temp_var = set_if_not ("SHLVL", "0");
254 set_auto_export (temp_var);
255 adjust_shell_level (1);
257 /* Make a variable $PPID, which holds the pid of the shell's parent. */
258 name = itos ((int) getppid ());
259 temp_var = find_variable ("PPID");
261 temp_var->attributes &= ~(att_readonly | att_exported);
262 temp_var = bind_variable ("PPID", name);
263 temp_var->attributes |= (att_readonly | att_integer);
266 /* Initialize the `getopts' stuff. */
267 bind_variable ("OPTIND", "1");
268 sv_optind ("OPTIND");
269 bind_variable ("OPTERR", "1");
270 sv_opterr ("OPTERR");
272 /* Get the full pathname to THIS shell, and set the BASH variable
274 if ((login_shell == 1) && (*shell_name != '/'))
276 /* If HOME doesn't exist, set it. */
277 temp_var = set_if_not ("HOME", current_user.home_dir);
278 temp_var->attributes |= att_exported;
280 name = savestring (current_user.shell);
282 else if (*shell_name == '/')
283 name = savestring (shell_name);
289 tname = find_user_command (shell_name);
293 /* Try the current directory. If there is not an executable
294 there, just punt and use the login shell. */
295 s = file_status (shell_name);
298 tname = make_absolute (shell_name, get_string_value ("PWD"));
299 if (*shell_name == '.')
301 name = canonicalize_pathname (tname);
311 name = savestring (current_user.shell);
315 name = full_pathname (tname);
319 temp_var = bind_variable ("BASH", name);
322 /* Make the exported environment variable SHELL be the user's login
323 shell. Note that the `tset' command looks at this variable
324 to determine what style of commands to output; if it ends in "csh",
325 then C-shell commands are output, else Bourne shell commands. */
326 temp_var = set_if_not ("SHELL", current_user.shell);
327 set_auto_export (temp_var);
329 /* Make a variable called BASH_VERSION which contains the version info. */
330 bind_variable ("BASH_VERSION", shell_version_string ());
331 #if defined (ARRAY_VARS)
335 /* Find out if we're supposed to be in Posix.2 mode via an
336 environment variable. */
337 temp_var = find_variable ("POSIXLY_CORRECT");
339 temp_var = find_variable ("POSIX_PEDANTIC");
340 if (temp_var && imported_p (temp_var))
341 sv_strict_posix (temp_var->name);
343 #if defined (HISTORY)
344 /* Set history variables to defaults, and then do whatever we would
345 do if the variable had just been set. Do this only in the case
346 that we are remembering commands on the history list. */
347 if (remember_on_history)
349 name = bash_tilde_expand (posixly_correct ? "~/.sh_history" : "~/.bash_history");
351 set_if_not ("HISTFILE", name);
354 set_if_not ("HISTSIZE", "500");
355 sv_histsize ("HISTSIZE");
359 /* Seed the random number generator. */
360 sbrand (dollar_dollar_pid + (long)shell_start_time);
362 /* Handle some "special" variables that we may have inherited from a
365 temp_var = find_variable ("IGNOREEOF");
367 temp_var = find_variable ("ignoreeof");
368 if (temp_var && imported_p (temp_var))
369 sv_ignoreeof (temp_var->name);
371 #if defined (HISTORY)
372 if (interactive_shell && remember_on_history)
374 sv_history_control ("HISTCONTROL");
375 sv_histignore ("HISTIGNORE");
379 /* Get the user's real and effective user ids. */
382 /* Initialize the dynamic variables, and seed their values. */
383 initialize_dynamic_variables ();
387 adjust_shell_level (change)
390 char *new_level, *old_SHLVL;
393 old_SHLVL = get_string_value ("SHLVL");
395 old_level = atoi (old_SHLVL);
399 shell_level = old_level + change;
402 new_level = itos (shell_level);
403 bind_variable ("SHLVL", new_level);
411 register SHELL_VAR *v;
413 buff = itos (current_user.uid);
414 v = find_variable ("UID");
416 v->attributes &= ~att_readonly;
418 v = bind_variable ("UID", buff);
419 v->attributes |= (att_readonly | att_integer);
421 if (current_user.euid != current_user.uid)
424 buff = itos (current_user.euid);
427 v = find_variable ("EUID");
429 v->attributes &= ~att_readonly;
431 v = bind_variable ("EUID", buff);
432 v->attributes |= (att_readonly | att_integer);
436 #if defined (ARRAY_VARS)
444 makunbound ("BASH_VERSINFO", shell_variables);
446 vv = make_new_array_variable ("BASH_VERSINFO");
447 av = array_cell (vv);
448 strcpy (d, dist_version);
452 array_add_element (av, 0, d);
453 array_add_element (av, 1, s);
454 s = itos (patch_level);
455 array_add_element (av, 2, s);
457 s = itos (build_version);
458 array_add_element (av, 3, s);
460 array_add_element (av, 4, release_status);
461 array_add_element (av, 5, MACHTYPE);
463 vv->attributes |= att_readonly;
465 #endif /* ARRAY_VARS */
467 /* Set the environment variables $LINES and $COLUMNS in response to
468 a window size change. */
470 set_lines_and_columns (lines, cols)
476 bind_variable ("LINES", val);
480 bind_variable ("COLUMNS", val);
484 /* Set NAME to VALUE if NAME has no value. */
486 set_if_not (name, value)
491 v = find_variable (name);
493 v = bind_variable (name, value);
497 /* Map FUNCTION over the variables in VARIABLES. Return an array of the
498 variables that satisfy FUNCTION. Satisfy means that FUNCTION returns
499 a non-zero value for. A NULL value for FUNCTION means to use all
502 map_over (function, var_hash_table)
504 HASH_TABLE* var_hash_table;
507 register BUCKET_CONTENTS *tlist;
508 SHELL_VAR *var, **list = (SHELL_VAR **)NULL;
509 int list_index = 0, list_size = 0;
511 for (i = 0; i < var_hash_table->nbuckets; i++)
513 tlist = get_hash_bucket (i, var_hash_table);
517 var = (SHELL_VAR *)tlist->data;
519 if (!function || (*function) (var))
521 if (list_index + 1 >= list_size)
522 list = (SHELL_VAR **)
523 xrealloc (list, (list_size += 20) * sizeof (SHELL_VAR *));
525 list[list_index++] = var;
526 list[list_index] = (SHELL_VAR *)NULL;
535 sort_variables (array)
538 qsort (array, array_len ((char **)array), sizeof (SHELL_VAR *), qsort_var_comp);
542 qsort_var_comp (var1, var2)
543 SHELL_VAR **var1, **var2;
547 if ((result = (*var1)->name[0] - (*var2)->name[0]) == 0)
548 result = strcmp ((*var1)->name, (*var2)->name);
553 /* Create a NULL terminated array of all the shell variables in TABLE. */
560 list = map_over ((Function *)NULL, table);
561 if (list && posixly_correct)
562 sort_variables (list);
566 /* Create a NULL terminated array of all the shell variables. */
568 all_shell_variables ()
570 return (all_vars (shell_variables));
573 /* Create a NULL terminated array of all the shell functions. */
575 all_shell_functions ()
577 return (all_vars (shell_functions));
580 /* Print VARS to stdout in such a way that they can be read back in. */
582 print_var_list (list)
583 register SHELL_VAR **list;
586 register SHELL_VAR *var;
588 for (i = 0; list && (var = list[i]); i++)
589 if (!invisible_p (var))
590 print_assignment (var);
594 /* Print LIST (a linked list of shell variables) to stdout
595 by printing the names, without the values. Used to support the
598 print_vars_no_values (list)
599 register SHELL_VAR **list;
602 register SHELL_VAR *var;
604 for (i = 0; list && (var = list[i]); i++)
605 if (!invisible_p (var))
606 printf ("%s\n", var->name);
610 /* Print the value of a single SHELL_VAR. No newline is
611 output, but the variable is printed in such a way that
612 it can be read back in. */
614 print_assignment (var)
617 if (function_p (var) && var->value)
619 printf ("%s=", var->name);
620 print_var_function (var);
623 #if defined (ARRAY_VARS)
624 else if (array_p (var) && var->value)
625 print_array_assignment (var, 0);
626 #endif /* ARRAY_VARS */
629 printf ("%s=", var->name);
630 print_var_value (var, 1);
635 /* Print the value cell of VAR, a shell variable. Do not print
636 the name, nor leading/trailing newline. If QUOTE is non-zero,
637 and the value contains shell metacharacters, quote the value
638 in such a way that it can be read back in. */
640 print_var_value (var, quote)
648 if (quote && contains_shell_metas (var->value))
650 t = single_quote (var->value);
655 printf ("%s", var->value);
659 /* Print the function cell of VAR, a shell variable. Do not
660 print the name, nor leading/trailing newline. */
662 print_var_function (var)
665 if (function_p (var) && var->value)
666 printf ("%s", named_function_string ((char *)NULL, function_cell(var), 1));
669 #if defined (ARRAY_VARS)
671 print_array_assignment (var, quoted)
678 vstr = quoted_array_assignment_string (array_cell (var));
680 vstr = array_to_assignment_string (array_cell (var));
683 printf ("%s=%s\n", var->name, quoted ? "'()'" : "()");
686 printf ("%s=%s\n", var->name, vstr);
690 #endif /* ARRAY_VARS */
692 /* **************************************************************** */
694 /* Dynamic Variable Extension */
696 /* **************************************************************** */
700 These are variables whose values are generated anew each time they are
701 referenced. These are implemented using a pair of function pointers
702 in the struct variable: assign_func, which is called from bind_variable,
703 and dynamic_value, which is called from find_variable.
705 assign_func is called from bind_variable, if bind_variable discovers
706 that the variable being assigned to has such a function. The function
708 SHELL_VAR *temp = (*(entry->assign_func)) (entry, value)
709 and the (SHELL_VAR *)temp is returned as the value of bind_variable. It
710 is usually ENTRY (self).
712 dynamic_value is called from find_variable to return a `new' value for
713 the specified dynamic varible. If this function is NULL, the variable
714 is treated as a `normal' shell variable. If it is not, however, then
715 this function is called like this:
716 tempvar = (*(var->dynamic_value)) (var);
718 Sometimes `tempvar' will replace the value of `var'. Other times, the
719 shell will simply use the string value. Pretty object-oriented, huh?
721 Be warned, though: if you `unset' a special variable, it loses its
722 special meaning, even if you subsequently set it.
724 The special assignment code would probably have been better put in
725 subst.c: do_assignment, in the same style as
726 stupidly_hack_special_variables, but I wanted the changes as
727 localized as possible. */
729 /* The value of $SECONDS. This is the number of seconds since shell
730 invocation, or, the number of seconds since the last assignment + the
731 value of the last assignment. */
732 static long seconds_value_assigned;
735 assign_seconds (self, value)
739 seconds_value_assigned = string_to_long (value);
740 shell_start_time = NOW;
748 time_t time_since_start;
751 time_since_start = NOW - shell_start_time;
752 p = itos((int) seconds_value_assigned + time_since_start);
756 var->attributes |= att_integer;
761 /* The random number seed. You can change this by setting RANDOM. */
762 static unsigned long rseed = 1;
763 static unsigned long last_random_value;
765 /* A linear congruential random number generator based on the ANSI
766 C standard. A more complicated one is overkill. */
768 /* Returns a pseudo-random number between 0 and 32767. */
772 rseed = rseed * 1103515245 + 12345;
773 return ((unsigned int)(rseed / 65536) % 32768);
776 /* Set the random number generator seed to SEED. */
785 assign_random (self, value)
789 sbrand (atoi (value));
800 /* Reset for command and process substitution. */
801 if (subshell_environment)
802 sbrand ((int)(getpid() + NOW));
806 while (rv == (int)last_random_value);
808 last_random_value = rv;
813 var->attributes |= att_integer;
818 /* Function which returns the current line number. */
826 ln = executing_line_number ();
834 assign_lineno (var, value)
838 line_number = atoi (value);
842 #if defined (HISTORY)
849 p = itos (history_number ());
856 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
864 l = get_directory_stack ();
865 a = word_list_to_array (l);
866 dispose_array (array_cell (self));
867 self->value = (char *)a;
872 assign_dirstack (self, ind, value)
877 set_dirstack_element (ind, 1, value);
880 #endif /* PUSHD AND POPD && ARRAY_VARS */
883 initialize_dynamic_variables ()
887 v = bind_variable ("SECONDS", (char *)NULL);
888 v->dynamic_value = get_seconds;
889 v->assign_func = assign_seconds;
891 v = bind_variable ("RANDOM", (char *)NULL);
892 v->dynamic_value = get_random;
893 v->assign_func = assign_random;
895 v = bind_variable ("LINENO", (char *)NULL);
896 v->dynamic_value = get_lineno;
897 v->assign_func = assign_lineno;
899 #if defined (HISTORY)
900 v = bind_variable ("HISTCMD", (char *)NULL);
901 v->dynamic_value = get_histcmd;
902 v->assign_func = (DYNAMIC_FUNC *)NULL;
905 #if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
906 v = make_new_array_variable ("DIRSTACK");
907 v->dynamic_value = get_dirstack;
908 v->assign_func = assign_dirstack;
909 #endif /* PUSHD_AND_POPD && ARRAY_VARS */
912 /* How to get a pointer to the shell variable or function named NAME.
913 HASHED_VARS is a pointer to the hash table containing the list
914 of interest (either variables or functions). */
916 var_lookup (name, hashed_vars)
918 HASH_TABLE *hashed_vars;
920 BUCKET_CONTENTS *bucket;
922 bucket = find_hash_item (name, hashed_vars);
923 return (bucket ? (SHELL_VAR *)bucket->data : (SHELL_VAR *)NULL);
926 /* Look up the variable entry named NAME. If SEARCH_TEMPENV is non-zero,
927 then also search the temporarily built list of exported variables. */
929 find_variable_internal (name, search_tempenv)
933 SHELL_VAR *var = (SHELL_VAR *)NULL;
935 /* If explicitly requested, first look in the temporary environment for
936 the variable. This allows constructs such as "foo=x eval 'echo $foo'"
937 to get the `exported' value of $foo. This happens if we are executing
938 a function or builtin, or if we are looking up a variable in a
939 "subshell environment". */
940 if ((search_tempenv || subshell_environment) &&
941 (temporary_env || builtin_env || function_env))
942 var = find_tempenv_variable (name);
945 var = var_lookup (name, shell_variables);
948 return ((SHELL_VAR *)NULL);
950 return (var->dynamic_value ? (*(var->dynamic_value)) (var) : var);
953 /* Look up the variable entry named NAME. Returns the entry or NULL. */
958 return (find_variable_internal
959 (name, (variable_context || this_shell_builtin || builtin_env)));
962 /* Look up the function entry whose name matches STRING.
963 Returns the entry or NULL. */
968 return (var_lookup (name, shell_functions));
971 /* Return the string value of a variable. Return NULL if the variable
972 doesn't exist, or only has a function as a value. Don't cons a new
975 get_string_value (var_name)
978 SHELL_VAR *var = find_variable (var_name);
982 #if defined (ARRAY_VARS)
983 else if (array_p (var))
984 return (array_reference (array_cell (var), 0));
990 /* Create a local variable referenced by NAME. */
992 make_local_variable (name)
995 SHELL_VAR *new_var, *old_var;
996 BUCKET_CONTENTS *elt;
998 /* local foo; local foo; is a no-op. */
999 old_var = find_variable (name);
1000 if (old_var && old_var->context == variable_context)
1003 elt = remove_hash_item (name, shell_variables);
1006 old_var = (SHELL_VAR *)elt->data;
1011 old_var = (SHELL_VAR *)NULL;
1013 /* If a variable does not already exist with this name, then
1014 just make a new one. */
1017 new_var = bind_variable (name, "");
1021 new_var = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
1023 new_var->name = savestring (name);
1024 new_var->value = xmalloc (1);
1025 new_var->value[0] = '\0';
1027 new_var->dynamic_value = (DYNAMIC_FUNC *)NULL;
1028 new_var->assign_func = (DYNAMIC_FUNC *)NULL;
1030 new_var->attributes = exported_p (old_var) ? att_exported : 0;
1032 new_var->prev_context = old_var;
1033 elt = add_hash_item (savestring (name), shell_variables);
1034 elt->data = (char *)new_var;
1037 new_var->context = variable_context;
1038 new_var->attributes |= att_local;
1041 if (variable_context >= local_variable_stack_size)
1043 int old_size = local_variable_stack_size;
1044 RESIZE_MALLOCED_BUFFER (have_local_variables, variable_context, 1,
1045 local_variable_stack_size, 8);
1046 bzero ((char *)have_local_variables + old_size,
1047 local_variable_stack_size - old_size);
1049 have_local_variables[variable_context] = 1; /* XXX */
1054 #if defined (ARRAY_VARS)
1056 make_local_array_variable (name)
1062 var = make_local_variable (name);
1063 array = new_array ();
1065 FREE (value_cell(var));
1066 var->value = (char *)array;
1067 var->attributes |= att_array;
1070 #endif /* ARRAY_VARS */
1072 /* Create a new shell variable with name NAME and add it to the hash table
1073 of shell variables. */
1076 make_new_variable (name)
1080 BUCKET_CONTENTS *elt;
1082 entry = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
1084 entry->attributes = 0;
1085 entry->name = savestring (name);
1086 entry->value = (char *)NULL;
1088 entry->dynamic_value = (DYNAMIC_FUNC *)NULL;
1089 entry->assign_func = (DYNAMIC_FUNC *)NULL;
1091 /* Always assume variables are to be made at toplevel!
1092 make_local_variable has the responsibilty of changing the
1093 variable context. */
1095 entry->prev_context = (SHELL_VAR *)NULL;
1097 elt = add_hash_item (savestring (name), shell_variables);
1098 elt->data = (char *)entry;
1103 #if defined (ARRAY_VARS)
1105 make_new_array_variable (name)
1111 entry = make_new_variable (name);
1112 array = new_array ();
1113 entry->value = (char *)array;
1114 entry->attributes |= att_array;
1120 make_variable_value (var, value)
1127 /* If this variable has had its type set to integer (via `declare -i'),
1128 then do expression evaluation on it and store the result. The
1129 functions in expr.c (evalexp and bind_int_variable) are responsible
1130 for turning off the integer flag if they don't want further
1132 if (integer_p (var))
1134 lval = evalexp (value);
1135 retval = itos (lval);
1140 retval = savestring (value);
1143 retval = xmalloc (1);
1148 retval = (char *)NULL;
1153 /* Bind a variable NAME to VALUE. This conses up the name
1154 and value strings. */
1156 bind_variable (name, value)
1162 entry = var_lookup (name, shell_variables);
1166 entry = make_new_variable (name);
1167 entry->value = make_variable_value (entry, value);
1169 #if defined (ARRAY_VARS)
1170 else if (entry->assign_func && array_p (entry) == 0)
1172 else if (entry->assign_func)
1174 return ((*(entry->assign_func)) (entry, value));
1177 if (readonly_p (entry))
1179 report_error ("%s: readonly variable", name);
1183 /* Variables which are bound are visible. */
1184 entry->attributes &= ~att_invisible;
1186 newval = make_variable_value (entry, value);
1188 #if defined (ARRAY_VARS)
1189 /* XXX -- this bears looking at again -- XXX */
1190 /* If an existing array variable x is being assigned to with x=b or
1191 `read x' or something of that nature, silently convert it to
1192 x[0]=b or `read x[0]'. */
1193 if (array_p (entry))
1194 array_add_element (array_cell (entry), 0, newval);
1197 FREE (entry->value);
1198 entry->value = newval;
1201 FREE (entry->value);
1202 entry->value = newval;
1206 if (mark_modified_vars)
1207 entry->attributes |= att_exported;
1209 if (exported_p (entry))
1210 array_needs_making = 1;
1215 #if defined (ARRAY_VARS)
1216 /* Convert a shell variable to an array variable. The original value is
1217 saved as array[0]. */
1219 convert_var_to_array (var)
1225 oldval = value_cell (var);
1226 array = new_array ();
1227 array_add_element (array, 0, oldval);
1228 FREE (value_cell (var));
1229 var->value = (char *)array;
1230 var->attributes |= att_array;
1231 var->attributes &= ~att_invisible;
1236 /* Perform an array assignment name[ind]=value. If NAME already exists and
1237 is not an array, and IND is 0, perform name=value instead. If NAME exists
1238 and is not an array, and IND is not 0, convert it into an array with the
1239 existing value as name[0].
1241 If NAME does not exist, just create an array variable, no matter what
1242 IND's value may be. */
1244 bind_array_variable (name, ind, value)
1252 entry = var_lookup (name, shell_variables);
1254 if (entry == (SHELL_VAR *) 0)
1255 entry = make_new_array_variable (name);
1256 else if (readonly_p (entry))
1258 report_error ("%s: readonly variable", name);
1261 else if (array_p (entry) == 0)
1262 entry = convert_var_to_array (entry);
1264 /* ENTRY is an array variable, and ARRAY points to the value. */
1265 newval = make_variable_value (entry, value);
1266 if (entry->assign_func)
1267 (*entry->assign_func) (entry, ind, newval);
1269 array_add_element (array_cell (entry), ind, newval);
1276 assign_array_from_string (name, value)
1281 var = find_variable (name);
1283 var = make_new_array_variable (name);
1284 else if (array_p (var) == 0)
1285 var = convert_var_to_array (var);
1287 return (assign_array_var_from_string (var, value));
1291 assign_array_var_from_word_list (var, list)
1296 register WORD_LIST *l;
1299 for (a = array_cell (var), l = list, i = 0; l; l = l->next, i++)
1300 if (var->assign_func)
1301 (*var->assign_func) (var, i, l->word->word);
1303 array_add_element (a, i, l->word->word);
1308 assign_array_var_from_string (var, value)
1313 WORD_LIST *list, *nlist;
1314 char *w, *val, *nval;
1315 int ni, len, ind, last_ind;
1317 a = array_cell (var);
1319 /* Expand the value string into a list of words, performing all the
1320 shell expansions including word splitting. */
1324 val = extract_array_assignment_list (value, &ni);
1327 nlist = expand_string (val, 0);
1331 nlist = expand_string (value, 0);
1333 for (last_ind = 0, list = nlist; list; list = list->next)
1335 w = list->word->word;
1337 /* We have a word of the form [ind]=value */
1340 len = skipsubscript (w, 0);
1342 if (w[len] != ']' || w[len+1] != '=')
1344 nval = make_variable_value (var, w);
1345 if (var->assign_func)
1346 (*var->assign_func) (var, last_ind, nval);
1348 array_add_element (a, last_ind, nval);
1356 report_error ("%s: bad array subscript", w);
1360 if (ALL_ELEMENT_SUB (w[1]) && len == 2)
1362 report_error ("%s: cannot assign to non-numeric index", w);
1366 ind = array_expand_index (w + 1, len);
1369 report_error ("%s: bad array subscript", w);
1375 else /* No [ind]=value, just a stray `=' */
1381 if (integer_p (var))
1382 this_command_name = (char *)NULL; /* no command name for errors */
1383 nval = make_variable_value (var, val);
1384 if (var->assign_func)
1385 (*var->assign_func) (var, ind, nval);
1387 array_add_element (a, ind, nval);
1392 dispose_words (nlist);
1395 #endif /* ARRAY_VARS */
1397 /* Dispose of the information attached to VAR. */
1399 dispose_variable (var)
1405 if (function_p (var))
1406 dispose_command (function_cell (var));
1407 #if defined (ARRAY_VARS)
1408 else if (array_p (var))
1409 dispose_array (array_cell (var));
1412 FREE (value_cell (var));
1416 if (exported_p (var))
1417 array_needs_making = 1;
1422 #if defined (ARRAY_VARS)
1423 /* This function is called with SUB pointing to just after the beginning
1424 `[' of an array subscript. */
1426 unbind_array_element (var, sub)
1433 len = skipsubscript (sub, 0);
1434 if (sub[len] != ']' || len == 0)
1436 builtin_error ("%s[%s: bad array subscript", var->name, sub);
1441 if (ALL_ELEMENT_SUB (sub[0]) && sub[1] == 0)
1443 makunbound (var->name, shell_variables);
1446 ind = array_expand_index (sub, len+1);
1449 builtin_error ("[%s: bad array subscript", sub);
1452 ae = array_delete_element (array_cell (var), ind);
1454 destroy_array_element (ae);
1459 /* Unset the variable referenced by NAME. */
1461 unbind_variable (name)
1464 SHELL_VAR *var = find_variable (name);
1469 /* This function should never be called with an array variable name. */
1470 #if defined (ARRAY_VARS)
1471 if (array_p (var) == 0 && var->value)
1477 var->value = (char *)NULL;
1480 makunbound (name, shell_variables);
1485 /* Make the variable associated with NAME go away. HASH_LIST is the
1486 hash table from which this variable should be deleted (either
1487 shell_variables or shell_functions).
1488 Returns non-zero if the variable couldn't be found. */
1490 makunbound (name, hash_list)
1492 HASH_TABLE *hash_list;
1494 BUCKET_CONTENTS *elt;
1495 SHELL_VAR *old_var, *new_var;
1498 elt = remove_hash_item (name, hash_list);
1503 old_var = (SHELL_VAR *)elt->data;
1504 new_var = old_var->prev_context;
1506 if (old_var && exported_p (old_var))
1507 array_needs_making++;
1509 /* If we're unsetting a local variable and we're still executing inside
1510 the function, just mark the variable as invisible.
1511 kill_all_local_variables will clean it up later. This must be done
1512 so that if the variable is subsequently assigned a new value inside
1513 the function, the `local' attribute is still present. We also need
1514 to add it back into the correct hash table. */
1515 if (old_var && local_p (old_var) && variable_context == old_var->context)
1517 old_var->attributes |= att_invisible;
1518 elt = add_hash_item (savestring (old_var->name), hash_list);
1519 elt->data = (char *)old_var;
1520 stupidly_hack_special_variables (old_var->name);
1526 /* Has to be a variable, functions don't have previous contexts. */
1527 BUCKET_CONTENTS *new_elt;
1529 new_elt = add_hash_item (savestring (new_var->name), hash_list);
1530 new_elt->data = (char *)new_var;
1532 if (exported_p (new_var))
1533 set_auto_export (new_var);
1536 /* Have to save a copy of name here, because it might refer to
1537 old_var->name. If so, stupidly_hack_special_variables will
1538 reference freed memory. */
1539 t = savestring (name);
1544 dispose_variable (old_var);
1545 stupidly_hack_special_variables (t);
1550 /* Remove the variable with NAME if it is a local variable in the
1553 kill_local_variable (name)
1556 SHELL_VAR *temp = find_variable (name);
1558 if (temp && temp->context == variable_context)
1560 makunbound (name, shell_variables);
1566 /* Get rid of all of the variables in the current context. */
1568 variable_in_context (var)
1571 return (var && var->context == variable_context);
1575 kill_all_local_variables ()
1577 register int i, pass;
1578 register SHELL_VAR *var, **list;
1579 HASH_TABLE *varlist;
1581 /* If HAVE_LOCAL_VARIABLES == 0, it means that we don't have any local
1582 variables at all. If VARIABLE_CONTEXT >= LOCAL_VARIABLE_STACK_SIZE,
1583 it means that we have some local variables, but not in this variable
1584 context (level of function nesting). Also, if
1585 HAVE_LOCAL_VARIABLES[VARIABLE_CONTEXT] == 0, we have no local variables
1587 if (have_local_variables == 0 ||
1588 variable_context >= local_variable_stack_size ||
1589 have_local_variables[variable_context] == 0)
1592 for (pass = 0; pass < 2; pass++)
1594 varlist = pass ? shell_functions : shell_variables;
1596 list = map_over (variable_in_context, varlist);
1600 for (i = 0; var = list[i]; i++)
1602 var->attributes &= ~att_local;
1603 makunbound (var->name, varlist);
1609 have_local_variables[variable_context] = 0; /* XXX */
1613 free_variable_hash_data (data)
1616 SHELL_VAR *var, *prev;
1618 var = (SHELL_VAR *)data;
1621 prev = var->prev_context;
1622 dispose_variable (var);
1627 /* Delete the entire contents of the hash table. */
1629 delete_all_variables (hashed_vars)
1630 HASH_TABLE *hashed_vars;
1632 flush_hash_table (hashed_vars, free_variable_hash_data);
1636 new_shell_variable (name)
1641 var = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
1643 bzero ((char *)var, sizeof (SHELL_VAR));
1644 var->name = savestring (name);
1648 /* Do a function binding to a variable. You pass the name and
1649 the command to bind to. This conses the name and command. */
1651 bind_function (name, value)
1657 entry = find_function (name);
1660 BUCKET_CONTENTS *elt;
1662 elt = add_hash_item (savestring (name), shell_functions);
1664 elt->data = (char *)new_shell_variable (name);
1665 entry = (SHELL_VAR *)elt->data;
1666 entry->dynamic_value = entry->assign_func = (DYNAMIC_FUNC *)NULL;
1668 /* Functions are always made at the top level. This allows a
1669 function to define another function (like autoload). */
1674 dispose_command ((COMMAND *)entry->value);
1676 entry->value = value ? (char *)copy_command (value) : (char *)NULL;
1677 entry->attributes |= att_function;
1679 if (mark_modified_vars)
1680 entry->attributes |= att_exported;
1682 entry->attributes &= ~att_invisible; /* Just to be sure */
1684 if (exported_p (entry))
1685 array_needs_making = 1;
1690 /* Copy VAR to a new data structure and return that structure. */
1695 SHELL_VAR *copy = (SHELL_VAR *)NULL;
1699 copy = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
1701 copy->attributes = var->attributes;
1702 copy->name = savestring (var->name);
1704 if (function_p (var))
1705 copy->value = (char *)copy_command (function_cell (var));
1706 #if defined (ARRAY_VARS)
1707 else if (array_p (var))
1708 copy->value = (char *)dup_array (array_cell (var));
1710 else if (value_cell (var))
1711 copy->value = savestring (value_cell (var));
1713 copy->value = (char *)NULL;
1715 copy->dynamic_value = var->dynamic_value;
1716 copy->assign_func = var->assign_func;
1718 copy->context = var->context;
1720 /* Don't bother copying previous contexts along with this variable. */
1721 copy->prev_context = (SHELL_VAR *)NULL;
1726 #define FIND_OR_MAKE_VARIABLE(name, entry) \
1729 entry = find_variable (name); \
1732 entry = bind_variable (name, ""); \
1733 if (!no_invisible_vars) entry->attributes |= att_invisible; \
1738 /* Make the variable associated with NAME be readonly.
1739 If NAME does not exist yet, create it. */
1741 set_var_read_only (name)
1746 FIND_OR_MAKE_VARIABLE (name, entry);
1747 entry->attributes |= att_readonly;
1750 /* Make the function associated with NAME be readonly.
1751 If NAME does not exist, we just punt, like auto_export code below. */
1753 set_func_read_only (name)
1756 SHELL_VAR *entry = find_function (name);
1759 entry->attributes |= att_readonly;
1762 /* Make the variable associated with NAME be auto-exported.
1763 If NAME does not exist yet, create it. */
1765 set_var_auto_export (name)
1770 FIND_OR_MAKE_VARIABLE (name, entry);
1771 set_auto_export (entry);
1774 /* Make the function associated with NAME be auto-exported. */
1776 set_func_auto_export (name)
1781 entry = find_function (name);
1783 set_auto_export (entry);
1786 #if defined (ARRAY_VARS)
1787 /* This function assumes s[i] == '['; returns with s[ret] == ']' if
1788 an array subscript is correctly parsed. */
1790 skipsubscript (s, i)
1796 for (count = 1; count && (c = s[++i]); )
1805 #endif /* ARRAY_VARS */
1807 /* Returns non-zero if STRING is an assignment statement. The returned value
1808 is the index of the `=' sign. */
1813 register int c, newi, indx;
1815 c = string[indx = 0];
1817 if (legal_variable_starter (c) == 0)
1820 while (c = string[indx])
1822 /* The following is safe. Note that '=' at the start of a word
1823 is not an assignment statement. */
1827 #if defined (ARRAY_VARS)
1830 newi = skipsubscript (string, indx);
1831 if (string[newi++] != ']')
1833 return ((string[newi] == '=') ? newi : 0);
1835 #endif /* ARRAY_VARS */
1837 /* Variable names in assignment statements may contain only letters,
1839 if (legal_variable_char (c) == 0)
1851 return (invisible_p (var) == 0);
1855 _visible_names (table)
1860 list = map_over (visible_var, table);
1862 if (list && posixly_correct)
1863 sort_variables (list);
1869 all_visible_variables ()
1871 return (_visible_names (shell_variables));
1875 all_visible_functions ()
1877 return (_visible_names (shell_functions));
1880 /* Return non-zero if the variable VAR is visible and exported. Array
1881 variables cannot be exported. */
1883 visible_and_exported (var)
1886 return (invisible_p (var) == 0 && exported_p (var));
1889 /* Make an array of assignment statements from the hash table
1890 HASHED_VARS which contains SHELL_VARs. Only visible, exported
1891 variables are eligible. */
1893 make_var_array (hashed_vars)
1894 HASH_TABLE *hashed_vars;
1896 register int i, list_index;
1897 register SHELL_VAR *var;
1898 char **list, *value;
1901 list = (char **)NULL;
1902 vars = map_over (visible_and_exported, hashed_vars);
1905 return (char **)NULL;
1907 list = (char **)xmalloc ((1 + array_len ((char **)vars)) * sizeof (char *));
1909 for (i = 0, list_index = 0; var = vars[i]; i++)
1911 if (function_p (var))
1912 value = named_function_string ((char *)NULL, function_cell (var), 0);
1913 #if defined (ARRAY_VARS)
1914 else if (array_p (var))
1916 value = array_to_assignment_string (array_cell (var));
1918 continue; /* XXX array vars cannot yet be exported */
1922 value = value_cell (var);
1926 int name_len, value_len;
1929 name_len = strlen (var->name);
1930 value_len = strlen (value);
1931 p = list[list_index] = xmalloc (2 + name_len + value_len);
1932 strcpy (p, var->name);
1934 strcpy (p + name_len + 1, value);
1936 #if defined (ARRAY_VARS)
1944 list[list_index] = (char *)NULL;
1948 /* Add STRING to the array of foo=bar strings that we already
1949 have to add to the environment. */
1951 assign_in_env (string)
1955 char *name, *temp, *value;
1960 offset = assignment (string);
1961 name = savestring (string);
1962 value = (char *)NULL;
1964 #define freetemp nlen
1965 if (name[offset] == '=')
1969 var = find_variable (name);
1970 if (var && readonly_p (var))
1972 report_error ("%s: readonly variable", name);
1975 temp = name + offset + 1;
1976 freetemp = strchr (temp, '~') != 0;
1978 temp = bash_tilde_expand (temp);
1980 list = expand_string_unsplit (temp, 0);
1981 value = string_list (list);
1984 dispose_words (list);
1991 nlen = strlen (name);
1992 vlen = value ? strlen (value) : 0;
1993 temp = xmalloc (2 + nlen + vlen);
1994 strcpy (temp, name);
1996 temp[nlen + 1] = '\0';
2000 strcpy (temp + nlen + 1, value);
2005 if (temporary_env == 0)
2007 temporary_env = (char **)xmalloc (sizeof (char *));
2008 temporary_env [0] = (char *)NULL;
2011 size = array_len (temporary_env);
2012 temporary_env = (char **)
2013 xrealloc (temporary_env, (size + 2) * (sizeof (char *)));
2015 temporary_env[size] = temp;
2016 temporary_env[size + 1] = (char *)NULL;
2017 array_needs_making = 1;
2019 if (echo_command_at_execute)
2021 /* The Korn shell prints the `+ ' in front of assignment statements,
2023 fprintf (stderr, "%s%s\n", indirection_level_string (), temp);
2030 /* Search for NAME in ARRAY, an array of strings in the same format as the
2031 environment array (i.e, name=value). If NAME is present, make a new
2032 variable and return it. Otherwise, return NULL. */
2034 find_name_in_env_array (name, array)
2041 return ((SHELL_VAR *)NULL);
2043 for (i = 0, l = strlen (name); array[i]; i++)
2045 if (STREQN (array[i], name, l) && array[i][l] == '=')
2050 temp = new_shell_variable (name);
2051 w = array[i] + l + 1;
2053 temp->value = *w ? savestring (w) : (char *)NULL;
2055 temp->attributes = att_exported;
2057 temp->prev_context = (SHELL_VAR *)NULL;
2059 temp->dynamic_value = temp->assign_func = (DYNAMIC_FUNC *)NULL;
2064 return ((SHELL_VAR *)NULL);
2067 /* Find a variable in the temporary environment that is named NAME.
2068 The temporary environment can be either the environment provided
2069 to a simple command, or the environment provided to a shell function.
2070 We only search the function environment if we are currently executing
2071 a shell function body (variable_context > 0). Return a consed variable,
2072 or NULL if not found. */
2074 find_tempenv_variable (name)
2077 SHELL_VAR *var = (SHELL_VAR *)NULL;
2080 var = find_name_in_env_array (name, temporary_env);
2082 /* We don't check this_shell_builtin because the command that needs the
2083 value from builtin_env may be a disk command run inside a script run
2084 with `.' and a temporary env. */
2085 if (!var && builtin_env)
2086 var = find_name_in_env_array (name, builtin_env);
2088 if (!var && variable_context && function_env)
2089 var = find_name_in_env_array (name, function_env);
2094 /* Free the storage allocated to the string array pointed to by ARRAYP, and
2095 make that variable have a null pointer as a value. */
2097 dispose_temporary_vars (arrayp)
2103 free_array (*arrayp);
2104 *arrayp = (char **)NULL;
2105 array_needs_making = 1;
2108 /* Free the storage used in the variable array for temporary
2109 environment variables. */
2111 dispose_used_env_vars ()
2113 dispose_temporary_vars (&temporary_env);
2116 /* Free the storage used for temporary environment variables given to
2117 commands when executing inside of a function body. */
2119 dispose_function_env ()
2121 dispose_temporary_vars (&function_env);
2124 /* Free the storage used for temporary environment variables given to
2125 commands when executing a builtin command such as "source". */
2127 dispose_builtin_env ()
2129 dispose_temporary_vars (&builtin_env);
2132 /* Take all of the shell variables in ENV_ARRAY and make shell variables
2133 from them at the current variable context. */
2135 merge_env_array (env_array)
2145 for (i = 0; env_array[i]; i++)
2147 l = assignment (env_array[i]);
2148 name = env_array[i];
2149 w = env_array[i] + l + 1;
2151 temp = bind_variable (name, w);
2157 merge_temporary_env ()
2159 merge_env_array (temporary_env);
2163 merge_builtin_env ()
2165 merge_env_array (builtin_env);
2168 #define ISFUNC(s, o) ((s[o + 1] == '(') && (s[o + 2] == ')'))
2170 /* Add ASSIGN to ARRAY, or supercede a previous assignment in the
2171 array with the same left-hand side. Return the new array. */
2173 add_or_supercede (assign, array)
2175 register char **array;
2178 int equal_offset = assignment (assign);
2183 /* If this is a function, then only supercede the function definition.
2184 We do this by including the `=(' in the comparison. */
2185 if (assign[equal_offset + 1] == '(')
2188 for (i = 0; array && array[i]; i++)
2190 if (STREQN (assign, array[i], equal_offset + 1))
2193 array[i] = savestring (assign);
2197 array = (char **)xrealloc (array, ((2 + i) * sizeof (char *)));
2198 array[i++] = savestring (assign);
2199 array[i] = (char *)NULL;
2203 /* Make the environment array for the command about to be executed. If the
2204 array needs making. Otherwise, do nothing. If a shell action could
2205 change the array that commands receive for their environment, then the
2206 code should `array_needs_making++'. */
2208 maybe_make_export_env ()
2211 register char **temp_array;
2213 if (array_needs_making)
2216 free_array (export_env);
2218 export_env = (char **)xmalloc (sizeof (char *));
2219 export_env[0] = (char *)NULL;
2221 temp_array = make_var_array (shell_variables);
2222 for (i = 0; temp_array && temp_array[i]; i++)
2223 export_env = add_or_supercede (temp_array[i], export_env);
2224 free_array (temp_array);
2226 temp_array = make_var_array (shell_functions);
2227 for (i = 0; temp_array && temp_array[i]; i++)
2228 export_env = add_or_supercede (temp_array[i], export_env);
2229 free_array (temp_array);
2232 for (i = 0; function_env[i]; i++)
2233 export_env = add_or_supercede (function_env[i], export_env);
2236 for (i = 0; temporary_env[i]; i++)
2237 export_env = add_or_supercede (temporary_env[i], export_env);
2240 /* If we changed the array, then sort it alphabetically. */
2241 if (posixly_correct == 0 && (temporary_env || function_env))
2242 sort_char_array (export_env);
2245 array_needs_making = 0;
2249 /* We always put _ in the environment as the name of this command. */
2251 put_command_name_into_env (command_name)
2256 dummy = xmalloc (4 + strlen (command_name));
2258 /* These three statements replace a call to sprintf */
2261 strcpy (dummy + 2, command_name);
2262 export_env = add_or_supercede (dummy, export_env);
2267 put_gnu_argv_flags_into_env (pid, flags_string)
2277 fl = strlen (flags_string);
2279 dummy = xmalloc (l + fl + 30);
2281 strcpy (dummy + 1, pbuf);
2282 strcpy (dummy + 1 + l, "_GNU_nonoption_argv_flags_");
2283 dummy[l + 27] = '=';
2284 strcpy (dummy + l + 28, flags_string);
2288 export_env = add_or_supercede (dummy, export_env);
2292 /* Return a string denoting what our indirection level is. */
2293 static char indirection_string[100];
2296 indirection_level_string ()
2301 indirection_string[0] = '\0';
2302 ps4 = get_string_value ("PS4");
2304 if (ps4 == 0 || *ps4 == '\0')
2305 return (indirection_string);
2307 ps4 = decode_prompt_string (ps4);
2309 for (i = 0; *ps4 && i < indirection_level && i < 99; i++)
2310 indirection_string[i] = *ps4;
2312 for (j = 1; *ps4 && ps4[j] && i < 99; i++, j++)
2313 indirection_string[i] = ps4[j];
2315 indirection_string[i] = '\0';
2317 return (indirection_string);