Imported from ../bash-2.05b.tar.gz.
[platform/upstream/bash.git] / variables.h
1 /* variables.h -- data structures for shell variables. */
2
3 /* Copyright (C) 1987-2002 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 /* A variable context. */
33 typedef struct var_context {
34   char *name;           /* empty or NULL means global context */
35   int scope;            /* 0 means global context */
36   int flags;
37   struct var_context *up;       /* previous function calls */
38   struct var_context *down;     /* down towards global context */
39   HASH_TABLE *table;            /* variables at this scope */
40 } VAR_CONTEXT;
41
42 /* Flags for var_context->flags */
43 #define VC_HASLOCAL     0x01
44 #define VC_HASTMPVAR    0x02
45 #define VC_FUNCENV      0x04    /* also function if name != NULL */
46 #define VC_BLTNENV      0x08    /* builtin_env */
47 #define VC_TEMPENV      0x10    /* temporary_env */
48
49 #define VC_TEMPFLAGS    (VC_FUNCENV|VC_BLTNENV|VC_TEMPENV)
50
51 /* Accessing macros */
52 #define vc_isfuncenv(vc)        (((vc)->flags & VC_FUNCENV) != 0)
53 #define vc_isbltnenv(vc)        (((vc)->flags & VC_BLTNENV) != 0)
54 #define vc_istempenv(vc)        (((vc)->flags & (VC_TEMPFLAGS)) == VC_TEMPENV)
55
56 #define vc_istempscope(vc)      (((vc)->flags & (VC_TEMPENV|VC_BLTNENV)) != 0)
57
58 #define vc_haslocals(vc)        (((vc)->flags & VC_HASLOCAL) != 0)
59 #define vc_hastmpvars(vc)       (((vc)->flags & VC_HASTMPVAR) != 0)
60
61 /* What a shell variable looks like. */
62
63 typedef struct variable *sh_var_value_func_t __P((struct variable *));
64 typedef struct variable *sh_var_assign_func_t __P((struct variable *, char *, arrayind_t));
65
66 /* For the future */
67 union _value {
68   char *s;                      /* string value */
69   intmax_t i;                   /* int value */
70   COMMAND *f;                   /* function */
71   ARRAY *a;                     /* array */
72   HASH_TABLE *h;                /* associative array */
73   double d;                     /* floating point number */
74   void *v;                      /* opaque data for future use */
75 };
76
77 typedef struct variable {
78   char *name;                   /* Symbol that the user types. */
79   char *value;                  /* Value that is returned. */
80   char *exportstr;              /* String for the environment. */
81   sh_var_value_func_t *dynamic_value;   /* Function called to return a `dynamic'
82                                    value for a variable, like $SECONDS
83                                    or $RANDOM. */
84   sh_var_assign_func_t *assign_func; /* Function called when this `special
85                                    variable' is assigned a value in
86                                    bind_variable. */
87   int attributes;               /* export, readonly, array, invisible... */
88   int context;                  /* Which context this variable belongs to. */
89 } SHELL_VAR;
90
91 typedef struct _vlist {
92   SHELL_VAR **list;
93   int list_size;        /* allocated size */
94   int list_len;         /* current number of entries */
95 } VARLIST;
96
97 /* The various attributes that a given variable can have. */
98 /* First, the user-visible attributes */
99 #define att_exported    0x0000001       /* export to environment */
100 #define att_readonly    0x0000002       /* cannot change */
101 #define att_array       0x0000004       /* value is an array */
102 #define att_function    0x0000008       /* value is a function */
103 #define att_integer     0x0000010       /* internal representation is int */
104 #define att_local       0x0000020       /* variable is local to a function */
105 #define att_assoc       0x0000040       /* variable is an associative array */
106 #define att_trace       0x0000080       /* function is traced with DEBUG trap */
107
108 #define attmask_user    0x0000fff
109
110 /* Internal attributes used for bookkeeping */
111 #define att_invisible   0x0001000       /* cannot see */
112 #define att_nounset     0x0002000       /* cannot unset */
113 #define att_noassign    0x0004000       /* assignment not allowed */
114 #define att_imported    0x0008000       /* came from environment */
115 #define att_special     0x0010000       /* requires special handling */
116
117 #define attmask_int     0x00ff000
118
119 /* Internal attributes used for variable scoping. */
120 #define att_tempvar     0x0100000       /* variable came from the temp environment */
121 #define att_propagate   0x0200000       /* propagate to previous scope */
122
123 #define attmask_scope   0x0f00000
124
125 #define exported_p(var)         ((((var)->attributes) & (att_exported)))
126 #define readonly_p(var)         ((((var)->attributes) & (att_readonly)))
127 #define array_p(var)            ((((var)->attributes) & (att_array)))
128 #define function_p(var)         ((((var)->attributes) & (att_function)))
129 #define integer_p(var)          ((((var)->attributes) & (att_integer)))
130 #define local_p(var)            ((((var)->attributes) & (att_local)))
131 #define assoc_p(var)            ((((var)->attributes) & (att_assoc)))
132 #define trace_p(var)            ((((var)->attributes) & (att_trace)))
133
134 #define invisible_p(var)        ((((var)->attributes) & (att_invisible)))
135 #define non_unsettable_p(var)   ((((var)->attributes) & (att_nounset)))
136 #define noassign_p(var)         ((((var)->attributes) & (att_noassign)))
137 #define imported_p(var)         ((((var)->attributes) & (att_imported)))
138 #define specialvar_p(var)       ((((var)->attributes) & (att_special)))
139
140 #define tempvar_p(var)          ((((var)->attributes) & (att_tempvar)))
141
142 /* Acessing variable values: rvalues */
143 #define value_cell(var)         ((var)->value)
144 #define function_cell(var)      (COMMAND *)((var)->value)
145 #define array_cell(var)         (ARRAY *)((var)->value)
146
147 #define var_isnull(var)         ((var)->value == 0)
148 #define var_isset(var)          ((var)->value != 0)
149
150 /* Assigning variable values: lvalues */
151 #define var_setvalue(var, str)  ((var)->value = (str))
152 #define var_setfunc(var, func)  ((var)->value = (char *)(func))
153 #define var_setarray(var, arr)  ((var)->value = (char *)(arr))
154
155 /* Make VAR be auto-exported. */
156 #define set_auto_export(var) \
157   do { (var)->attributes |= att_exported; array_needs_making = 1; } while (0)
158
159 #define SETVARATTR(var, attr, undo) \
160         ((undo == 0) ? ((var)->attributes |= (attr)) \
161                      : ((var)->attributes &= ~(attr)))
162
163 #define VSETATTR(var, attr)     ((var)->attributes |= (attr))
164 #define VUNSETATTR(var, attr)   ((var)->attributes &= ~(attr))
165
166 #define VGETFLAGS(var)          ((var)->attributes)
167
168 #define VSETFLAGS(var, flags)   ((var)->attributes = (flags))
169 #define VCLRFLAGS(var)          ((var)->attributes = 0)
170
171 /* Macros to perform various operations on `exportstr' member of a SHELL_VAR. */
172 #define CLEAR_EXPORTSTR(var)    (var)->exportstr = (char *)NULL
173 #define COPY_EXPORTSTR(var)     ((var)->exportstr) ? savestring ((var)->exportstr) : (char *)NULL
174 #define SET_EXPORTSTR(var, value)  (var)->exportstr = (value)
175 #define SAVE_EXPORTSTR(var, value) (var)->exportstr = (value) ? savestring (value) : (char *)NULL
176
177 #define FREE_EXPORTSTR(var) \
178         do { if ((var)->exportstr) free ((var)->exportstr); } while (0)
179
180 #define CACHE_IMPORTSTR(var, value) \
181         (var)->exportstr = savestring (value)
182
183 #define INVALIDATE_EXPORTSTR(var) \
184         do { \
185           if ((var)->exportstr) \
186             { \
187               free ((var)->exportstr); \
188               (var)->exportstr = (char *)NULL; \
189             } \
190         } while (0)
191         
192 /* Stuff for hacking variables. */
193 typedef int sh_var_map_func_t __P((SHELL_VAR *));
194
195 /* Where we keep the variables and functions */
196 extern VAR_CONTEXT *global_variables;
197 extern VAR_CONTEXT *shell_variables;
198
199 extern HASH_TABLE *shell_functions;
200 extern HASH_TABLE *temporary_env;
201
202 extern int variable_context;
203 extern char *dollar_vars[];
204 extern char **export_env;
205
206 extern void initialize_shell_variables __P((char **, int));
207 extern SHELL_VAR *set_if_not __P((char *, char *));
208
209 extern void sh_set_lines_and_columns __P((int, int));
210 extern void set_pwd __P((void));
211 extern void set_ppid __P((void));
212 extern void make_funcname_visible __P((int));
213
214 extern SHELL_VAR *var_lookup __P((const char *, VAR_CONTEXT *));
215
216 extern SHELL_VAR *find_function __P((const char *));
217 extern SHELL_VAR *find_variable __P((const char *));
218 extern SHELL_VAR *find_variable_internal __P((const char *, int));
219 extern SHELL_VAR *find_tempenv_variable __P((const char *));
220 extern SHELL_VAR *copy_variable __P((SHELL_VAR *));
221 extern SHELL_VAR *make_local_variable __P((const char *));
222 extern SHELL_VAR *bind_variable __P((const char *, char *));
223 extern SHELL_VAR *bind_function __P((const char *, COMMAND *));
224
225 extern SHELL_VAR **map_over __P((sh_var_map_func_t *, VAR_CONTEXT *));
226 SHELL_VAR **map_over_funcs __P((sh_var_map_func_t *));
227      
228 extern SHELL_VAR **all_shell_variables __P((void));
229 extern SHELL_VAR **all_shell_functions __P((void));
230 extern SHELL_VAR **all_visible_variables __P((void));
231 extern SHELL_VAR **all_visible_functions __P((void));
232 extern SHELL_VAR **all_exported_variables __P((void));
233 extern SHELL_VAR **local_exported_variables __P((void));
234 extern SHELL_VAR **all_local_variables __P((void));
235 #if defined (ARRAY_VARS)
236 extern SHELL_VAR **all_array_variables __P((void));
237 #endif
238 extern char **all_variables_matching_prefix __P((const char *));
239
240 extern char **make_var_array __P((HASH_TABLE *));
241 extern char **add_or_supercede_exported_var __P((char *, int));
242
243 extern char *get_variable_value __P((SHELL_VAR *));
244 extern char *get_string_value __P((const char *));
245 extern char *sh_get_env_value __P((const char *));
246 extern char *make_variable_value __P((SHELL_VAR *, char *));
247
248 extern SHELL_VAR *bind_variable_value __P((SHELL_VAR *, char *));
249 extern SHELL_VAR *bind_int_variable __P((char *, char *));
250 extern SHELL_VAR *bind_var_to_int __P((char *, intmax_t));
251
252 extern int assign_in_env __P((const char *));
253 extern int unbind_variable __P((const char *));
254 extern int unbind_func __P((const char *));
255 extern int makunbound __P((const char *, VAR_CONTEXT *));
256 extern int kill_local_variable __P((const char *));
257 extern void delete_all_variables __P((HASH_TABLE *));
258 extern void delete_all_contexts __P((VAR_CONTEXT *));
259
260 extern VAR_CONTEXT *new_var_context __P((char *, int));
261 extern void dispose_var_context __P((VAR_CONTEXT *));
262 extern VAR_CONTEXT *push_var_context __P((char *, int, HASH_TABLE *));
263 extern void pop_var_context __P((void));
264 extern VAR_CONTEXT *push_scope __P((int, HASH_TABLE *));
265 extern void pop_scope __P((int));
266
267 extern void push_context __P((char *, int, HASH_TABLE *));
268 extern void pop_context __P((void));
269 extern void push_dollar_vars __P((void));
270 extern void pop_dollar_vars __P((void));
271 extern void dispose_saved_dollar_vars __P((void));
272
273 extern void adjust_shell_level __P((int));
274 extern void non_unsettable __P((char *));
275 extern void dispose_variable __P((SHELL_VAR *));
276 extern void dispose_used_env_vars __P((void));
277 extern void dispose_function_env __P((void));
278 extern void dispose_builtin_env __P((void));
279 extern void merge_temporary_env __P((void));
280 extern void merge_builtin_env __P((void));
281 extern void kill_all_local_variables __P((void));
282
283 extern void set_var_read_only __P((char *));
284 extern void set_func_read_only __P((const char *));
285 extern void set_var_auto_export __P((char *));
286 extern void set_func_auto_export __P((const char *));
287
288 extern void sort_variables __P((SHELL_VAR **));
289
290 extern void maybe_make_export_env __P((void));
291 extern void update_export_env_inplace __P((char *, int, char *));
292 extern void put_command_name_into_env __P((char *));
293 extern void put_gnu_argv_flags_into_env __P((intmax_t, char *));
294
295 extern void print_var_list __P((SHELL_VAR **));
296 extern void print_func_list __P((SHELL_VAR **));
297 extern void print_assignment __P((SHELL_VAR *));
298 extern void print_var_value __P((SHELL_VAR *, int));
299 extern void print_var_function __P((SHELL_VAR *));
300
301 #if defined (ARRAY_VARS)
302 extern SHELL_VAR *make_new_array_variable __P((char *));
303 extern SHELL_VAR *make_local_array_variable __P((char *));
304
305 extern void set_pipestatus_array __P((int *, int));
306 #endif
307
308 extern void set_pipestatus_from_exit __P((int));
309
310 /* The variable in NAME has just had its state changed.  Check to see if it
311    is one of the special ones where something special happens. */
312 extern void stupidly_hack_special_variables __P((char *));
313
314 extern int get_random_number __P((void));
315
316 /* The `special variable' functions that get called when a particular
317    variable is set. */
318 extern void sv_ifs __P((char *));
319 extern void sv_path __P((char *));
320 extern void sv_mail __P((char *));
321 extern void sv_globignore __P((char *));
322 extern void sv_ignoreeof __P((char *));
323 extern void sv_strict_posix __P((char *));
324 extern void sv_optind __P((char *));
325 extern void sv_opterr __P((char *));
326 extern void sv_locale __P((char *));
327
328 #if defined (READLINE)
329 extern void sv_terminal __P((char *));
330 extern void sv_hostfile __P((char *));
331 #endif
332
333 #if defined (HAVE_TZSET) && defined (PROMPT_STRING_DECODE)
334 extern void sv_tz __P((char *));
335 #endif
336
337 #if defined (HISTORY)
338 extern void sv_histsize __P((char *));
339 extern void sv_histignore __P((char *));
340 extern void sv_history_control __P((char *));
341 #  if defined (BANG_HISTORY)
342 extern void sv_histchars __P((char *));
343 #  endif
344 #endif /* HISTORY */
345
346 #endif /* !_VARIABLES_H_ */