f0da3ef33d570bad7a3217595ecd87c94bc9b856
[platform/upstream/bash.git] / variables.h
1 /* variables.h -- data structures for shell variables. */
2
3 /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
7    Bash is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
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.
16
17    You should have received a copy of the GNU General Public License
18    along with Bash; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
21 #if !defined (_VARIABLES_H_)
22 #define _VARIABLES_H_
23
24 #include "stdc.h"
25 #include "array.h"
26
27 /* Shell variables and functions are stored in hash tables. */
28 #include "hashlib.h"
29
30 #include "conftypes.h"
31
32 /* What a shell variable looks like. */
33
34 typedef struct variable *DYNAMIC_FUNC ();
35
36 typedef struct variable {
37   char *name;                   /* Symbol that the user types. */
38   char *value;                  /* Value that is returned. */
39   char *exportstr;              /* String for the environment. */
40   DYNAMIC_FUNC *dynamic_value;  /* Function called to return a `dynamic'
41                                    value for a variable, like $SECONDS
42                                    or $RANDOM. */
43   DYNAMIC_FUNC *assign_func;    /* Function called when this `special
44                                    variable' is assigned a value in
45                                    bind_variable. */
46   int attributes;               /* export, readonly, array, invisible... */
47   int context;                  /* Which context this variable belongs to. */
48   struct variable *prev_context; /* Value from previous context or NULL. */
49 } SHELL_VAR;
50
51 /* The various attributes that a given variable can have. */
52 #define att_exported  0x001     /* export to environment */
53 #define att_readonly  0x002     /* cannot change */
54 #define att_invisible 0x004     /* cannot see */
55 #define att_array     0x008     /* value is an array */
56 #define att_nounset   0x010     /* cannot unset */
57 #define att_function  0x020     /* value is a function */
58 #define att_integer   0x040     /* internal representation is int */
59 #define att_imported  0x080     /* came from environment */
60 #define att_local     0x100     /* variable is local to a function */
61 #define att_tempvar   0x200     /* variable came from the temp environment */
62 #define att_importstr 0x400     /* exportstr points into initial environment */
63 #define att_noassign  0x800     /* assignment not allowed */
64
65 #define exported_p(var)         ((((var)->attributes) & (att_exported)))
66 #define readonly_p(var)         ((((var)->attributes) & (att_readonly)))
67 #define invisible_p(var)        ((((var)->attributes) & (att_invisible)))
68 #define array_p(var)            ((((var)->attributes) & (att_array)))
69 #define non_unsettable_p(var)   ((((var)->attributes) & (att_nounset)))
70 #define function_p(var)         ((((var)->attributes) & (att_function)))
71 #define integer_p(var)          ((((var)->attributes) & (att_integer)))
72 #define imported_p(var)         ((((var)->attributes) & (att_imported)))
73 #define local_p(var)            ((((var)->attributes) & (att_local)))
74 #define tempvar_p(var)          ((((var)->attributes) & (att_tempvar)))
75 #define noassign_p(var)         ((((var)->attributes) & (att_noassign)))
76
77 #define value_cell(var) ((var)->value)
78 #define function_cell(var) (COMMAND *)((var)->value)
79 #define array_cell(var) ((ARRAY *)(var)->value)
80
81 #define SETVARATTR(var, attr, undo) \
82         ((undo == 0) ? ((var)->attributes |= (attr)) \
83                      : ((var)->attributes &= ~(attr)))
84
85 #define VSETATTR(var, attr)     ((var)->attributes |= (attr))
86 #define VUNSETATTR(var, attr)   ((var)->attributes &= ~(attr))
87
88 #define VGETFLAGS(var)          ((var)->attributes)
89
90 #define VSETFLAGS(var, flags)   ((var)->attributes = (flags))
91 #define VCLRFLAGS(var)          ((var)->attributes = 0)
92
93 /* Macros to perform various operations on `exportstr' member of a SHELL_VAR. */
94 #define CLEAR_EXPORTSTR(var)    (var)->exportstr = (char *)NULL
95 #define COPY_EXPORTSTR(var)     ((var)->exportstr) ? savestring ((var)->exportstr) : (char *)NULL
96 #define SET_EXPORTSTR(var, value)  (var)->exportstr = (value)
97 #define SAVE_EXPORTSTR(var, value) (var)->exportstr = (value) ? savestring (value) : (char *)NULL
98
99 #define FREE_EXPORTSTR(var) \
100         do { \
101           if ((var)->exportstr) \
102             { \
103               if (((var)->attributes & att_importstr) == 0) \
104                 free ((var)->exportstr); \
105             } \
106         } while (0)
107
108 #if 0
109 #define CACHE_IMPORTSTR(var, value) \
110         do { \
111           (var)->exportstr = value; \
112           (var)->attributes |= att_importstr; \
113         } while (0)
114 #else
115 #define CACHE_IMPORTSTR(var, value) \
116         do { \
117           (var)->exportstr = savestring (value); \
118         } while (0)
119 #endif
120
121 #define INVALIDATE_EXPORTSTR(var) \
122         do { \
123           if ((var)->exportstr) \
124             { \
125               if (((var)->attributes & att_importstr) == 0) \
126                 free ((var)->exportstr); \
127               (var)->exportstr = (char *)NULL; \
128               (var)->attributes &= ~att_importstr; \
129             } \
130         } while (0)
131         
132 /* Stuff for hacking variables. */
133 typedef int sh_var_map_func_t __P((SHELL_VAR *));
134
135 extern int variable_context;
136 extern HASH_TABLE *shell_variables, *shell_functions;
137 extern char *dollar_vars[];
138 extern char **export_env;
139 extern char **non_unsettable_vars;
140
141 extern void initialize_shell_variables __P((char **, int));
142 extern SHELL_VAR *set_if_not __P((char *, char *));
143 extern void sh_set_lines_and_columns __P((int, int));
144
145 extern void set_pwd __P((void));
146
147 extern void set_ppid __P((void));
148
149 extern void make_funcname_visible __P((int));
150
151 extern SHELL_VAR *var_lookup __P((const char *, HASH_TABLE *));
152
153 extern SHELL_VAR *find_function __P((const char *));
154 extern SHELL_VAR *find_variable __P((const char *));
155 extern SHELL_VAR *find_variable_internal __P((const char *, int));
156 extern SHELL_VAR *find_tempenv_variable __P((const char *));
157 extern SHELL_VAR *copy_variable __P((SHELL_VAR *));
158 extern SHELL_VAR *make_local_variable __P((const char *));
159 extern SHELL_VAR *bind_variable __P((const char *, char *));
160 extern SHELL_VAR *bind_function __P((const char *, COMMAND *));
161
162 extern SHELL_VAR **map_over __P((sh_var_map_func_t *, HASH_TABLE *));
163 extern SHELL_VAR **all_shell_variables __P((void));
164 extern SHELL_VAR **all_shell_functions __P((void));
165 extern SHELL_VAR **all_visible_variables __P((void));
166 extern SHELL_VAR **all_visible_functions __P((void));
167 extern SHELL_VAR **all_exported_variables __P((void));
168 #if defined (ARRAY_VARS)
169 extern SHELL_VAR **all_array_variables __P((void));
170 #endif
171
172 extern char **all_variables_matching_prefix __P((const char *));
173
174 extern char **make_var_array __P((HASH_TABLE *));
175 extern char **add_or_supercede_exported_var __P((char *, int));
176
177 extern char *get_string_value __P((const char *));
178 extern char *make_variable_value __P((SHELL_VAR *, char *));
179
180 extern SHELL_VAR *bind_variable_value __P((SHELL_VAR *, char *));
181 extern SHELL_VAR *bind_int_variable __P((char *, char *));
182 extern SHELL_VAR *bind_var_to_int __P((char *, long));
183
184 extern int assignment __P((const char *));
185 extern int variable_in_context __P((SHELL_VAR *));
186 extern int assign_in_env __P((const char *));
187 extern int unbind_variable __P((const char *));
188 extern int makunbound __P((const char *, HASH_TABLE *));
189 extern int kill_local_variable __P((const char *));
190 extern void delete_all_variables __P((HASH_TABLE *));
191
192 extern void adjust_shell_level __P((int));
193 extern void non_unsettable __P((char *));
194 extern void dispose_variable __P((SHELL_VAR *));
195 extern void dispose_used_env_vars __P((void));
196 extern void dispose_function_env __P((void));
197 extern void dispose_builtin_env __P((void));
198 extern void merge_temporary_env __P((void));
199 extern void merge_builtin_env __P((void));
200 extern void merge_function_env __P((void));
201 extern void kill_all_local_variables __P((void));
202 extern void set_var_read_only __P((char *));
203 extern void set_func_read_only __P((const char *));
204 extern void set_var_auto_export __P((char *));
205 extern void set_func_auto_export __P((const char *));
206 extern void sort_variables __P((SHELL_VAR **));
207 extern void maybe_make_export_env __P((void));
208 extern void update_export_env_inplace __P((char *, int, char *));
209 extern void put_command_name_into_env __P((char *));
210 extern void put_gnu_argv_flags_into_env __P((long, char *));
211 extern void print_var_list __P((SHELL_VAR **));
212 extern void print_func_list __P((SHELL_VAR **));
213 extern void print_assignment __P((SHELL_VAR *));
214 extern void print_var_value __P((SHELL_VAR *, int));
215 extern void print_var_function __P((SHELL_VAR *));
216
217 extern char *indirection_level_string __P((void));
218
219 #if defined (ARRAY_VARS)
220 extern SHELL_VAR *make_new_array_variable __P((char *));
221 extern SHELL_VAR *make_local_array_variable __P((char *));
222
223 extern void set_pipestatus_array __P((int *));
224 #endif
225
226 extern void set_pipestatus_from_exit __P((int));
227
228 /* The variable in NAME has just had its state changed.  Check to see if it
229    is one of the special ones where something special happens. */
230 extern void stupidly_hack_special_variables __P((char *));
231
232 extern int get_random_number __P((void));
233
234 /* The `special variable' functions that get called when a particular
235    variable is set. */
236 extern void sv_path __P((char *));
237 extern void sv_mail __P((char *));
238 extern void sv_globignore __P((char *));
239 extern void sv_ignoreeof __P((char *));
240 extern void sv_strict_posix __P((char *));
241 extern void sv_optind __P((char *));
242 extern void sv_opterr __P((char *));
243 extern void sv_locale __P((char *));
244
245 #if defined (READLINE)
246 extern void sv_terminal __P((char *));
247 extern void sv_hostfile __P((char *));
248 #endif
249
250 #if defined (HAVE_TZSET) && defined (PROMPT_STRING_DECODE)
251 extern void sv_tz __P((char *));
252 #endif
253
254 #if defined (HISTORY)
255 extern void sv_histsize __P((char *));
256 extern void sv_histignore __P((char *));
257 extern void sv_history_control __P((char *));
258 #  if defined (BANG_HISTORY)
259 extern void sv_histchars __P((char *));
260 #  endif
261 #endif /* HISTORY */
262
263 #endif /* !_VARIABLES_H_ */