]> git.ipfire.org Git - thirdparty/bash.git/blame - variables.h
Bash-4.3 patch 32
[thirdparty/bash.git] / variables.h
CommitLineData
726f6388
JA
1/* variables.h -- data structures for shell variables. */
2
ac50fbac 3/* Copyright (C) 1987-2012 Free Software Foundation, Inc.
bb70624e
JA
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
3185942a
JA
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
bb70624e 11
3185942a
JA
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
bb70624e
JA
16
17 You should have received a copy of the GNU General Public License
3185942a
JA
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
bb70624e 20
726f6388
JA
21#if !defined (_VARIABLES_H_)
22#define _VARIABLES_H_
23
24#include "stdc.h"
ccc6cda3 25#include "array.h"
3185942a 26#include "assoc.h"
726f6388
JA
27
28/* Shell variables and functions are stored in hash tables. */
ccc6cda3 29#include "hashlib.h"
726f6388 30
f73dda09 31#include "conftypes.h"
b72432fd 32
7117c2d2
JA
33/* A variable context. */
34typedef struct var_context {
35 char *name; /* empty or NULL means global context */
36 int scope; /* 0 means global context */
37 int flags;
38 struct var_context *up; /* previous function calls */
39 struct var_context *down; /* down towards global context */
40 HASH_TABLE *table; /* variables at this scope */
41} VAR_CONTEXT;
42
43/* Flags for var_context->flags */
44#define VC_HASLOCAL 0x01
45#define VC_HASTMPVAR 0x02
46#define VC_FUNCENV 0x04 /* also function if name != NULL */
47#define VC_BLTNENV 0x08 /* builtin_env */
48#define VC_TEMPENV 0x10 /* temporary_env */
49
50#define VC_TEMPFLAGS (VC_FUNCENV|VC_BLTNENV|VC_TEMPENV)
51
52/* Accessing macros */
53#define vc_isfuncenv(vc) (((vc)->flags & VC_FUNCENV) != 0)
54#define vc_isbltnenv(vc) (((vc)->flags & VC_BLTNENV) != 0)
55#define vc_istempenv(vc) (((vc)->flags & (VC_TEMPFLAGS)) == VC_TEMPENV)
56
57#define vc_istempscope(vc) (((vc)->flags & (VC_TEMPENV|VC_BLTNENV)) != 0)
58
59#define vc_haslocals(vc) (((vc)->flags & VC_HASLOCAL) != 0)
60#define vc_hastmpvars(vc) (((vc)->flags & VC_HASTMPVAR) != 0)
61
726f6388
JA
62/* What a shell variable looks like. */
63
7117c2d2 64typedef struct variable *sh_var_value_func_t __P((struct variable *));
3185942a 65typedef struct variable *sh_var_assign_func_t __P((struct variable *, char *, arrayind_t, char *));
7117c2d2
JA
66
67/* For the future */
68union _value {
69 char *s; /* string value */
70 intmax_t i; /* int value */
71 COMMAND *f; /* function */
72 ARRAY *a; /* array */
73 HASH_TABLE *h; /* associative array */
74 double d; /* floating point number */
b80f6443
JA
75#if defined (HAVE_LONG_DOUBLE)
76 long double ld; /* long double */
77#endif
78 struct variable *v; /* possible indirect variable use */
79 void *opaque; /* opaque data for future use */
7117c2d2 80};
726f6388
JA
81
82typedef struct variable {
83 char *name; /* Symbol that the user types. */
84 char *value; /* Value that is returned. */
bb70624e 85 char *exportstr; /* String for the environment. */
7117c2d2 86 sh_var_value_func_t *dynamic_value; /* Function called to return a `dynamic'
726f6388
JA
87 value for a variable, like $SECONDS
88 or $RANDOM. */
7117c2d2 89 sh_var_assign_func_t *assign_func; /* Function called when this `special
726f6388
JA
90 variable' is assigned a value in
91 bind_variable. */
92 int attributes; /* export, readonly, array, invisible... */
93 int context; /* Which context this variable belongs to. */
726f6388
JA
94} SHELL_VAR;
95
7117c2d2
JA
96typedef struct _vlist {
97 SHELL_VAR **list;
98 int list_size; /* allocated size */
99 int list_len; /* current number of entries */
100} VARLIST;
101
cce855bc 102/* The various attributes that a given variable can have. */
7117c2d2
JA
103/* First, the user-visible attributes */
104#define att_exported 0x0000001 /* export to environment */
105#define att_readonly 0x0000002 /* cannot change */
106#define att_array 0x0000004 /* value is an array */
107#define att_function 0x0000008 /* value is a function */
108#define att_integer 0x0000010 /* internal representation is int */
109#define att_local 0x0000020 /* variable is local to a function */
110#define att_assoc 0x0000040 /* variable is an associative array */
111#define att_trace 0x0000080 /* function is traced with DEBUG trap */
3185942a
JA
112#define att_uppercase 0x0000100 /* word converted to uppercase on assignment */
113#define att_lowercase 0x0000200 /* word converted to lowercase on assignment */
114#define att_capcase 0x0000400 /* word capitalized on assignment */
ac50fbac 115#define att_nameref 0x0000800 /* word is a name reference */
3185942a 116
ac50fbac 117#define user_attrs (att_exported|att_readonly|att_integer|att_local|att_trace|att_uppercase|att_lowercase|att_capcase|att_nameref)
7117c2d2
JA
118
119#define attmask_user 0x0000fff
120
121/* Internal attributes used for bookkeeping */
122#define att_invisible 0x0001000 /* cannot see */
123#define att_nounset 0x0002000 /* cannot unset */
124#define att_noassign 0x0004000 /* assignment not allowed */
125#define att_imported 0x0008000 /* came from environment */
126#define att_special 0x0010000 /* requires special handling */
3185942a 127#define att_nofree 0x0020000 /* do not free value on unset */
7117c2d2
JA
128
129#define attmask_int 0x00ff000
130
131/* Internal attributes used for variable scoping. */
132#define att_tempvar 0x0100000 /* variable came from the temp environment */
133#define att_propagate 0x0200000 /* propagate to previous scope */
134
135#define attmask_scope 0x0f00000
726f6388
JA
136
137#define exported_p(var) ((((var)->attributes) & (att_exported)))
138#define readonly_p(var) ((((var)->attributes) & (att_readonly)))
726f6388
JA
139#define array_p(var) ((((var)->attributes) & (att_array)))
140#define function_p(var) ((((var)->attributes) & (att_function)))
141#define integer_p(var) ((((var)->attributes) & (att_integer)))
ccc6cda3 142#define local_p(var) ((((var)->attributes) & (att_local)))
7117c2d2
JA
143#define assoc_p(var) ((((var)->attributes) & (att_assoc)))
144#define trace_p(var) ((((var)->attributes) & (att_trace)))
3185942a
JA
145#define uppercase_p(var) ((((var)->attributes) & (att_uppercase)))
146#define lowercase_p(var) ((((var)->attributes) & (att_lowercase)))
147#define capcase_p(var) ((((var)->attributes) & (att_capcase)))
ac50fbac 148#define nameref_p(var) ((((var)->attributes) & (att_nameref)))
7117c2d2
JA
149
150#define invisible_p(var) ((((var)->attributes) & (att_invisible)))
151#define non_unsettable_p(var) ((((var)->attributes) & (att_nounset)))
28ef6c31 152#define noassign_p(var) ((((var)->attributes) & (att_noassign)))
7117c2d2
JA
153#define imported_p(var) ((((var)->attributes) & (att_imported)))
154#define specialvar_p(var) ((((var)->attributes) & (att_special)))
3185942a 155#define nofree_p(var) ((((var)->attributes) & (att_nofree)))
7117c2d2
JA
156
157#define tempvar_p(var) ((((var)->attributes) & (att_tempvar)))
158
159/* Acessing variable values: rvalues */
160#define value_cell(var) ((var)->value)
161#define function_cell(var) (COMMAND *)((var)->value)
162#define array_cell(var) (ARRAY *)((var)->value)
3185942a 163#define assoc_cell(var) (HASH_TABLE *)((var)->value)
ac50fbac
CR
164#define nameref_cell(var) ((var)->value) /* so it can change later */
165
166#define NAMEREF_MAX 8 /* only 8 levels of nameref indirection */
7117c2d2
JA
167
168#define var_isnull(var) ((var)->value == 0)
169#define var_isset(var) ((var)->value != 0)
170
171/* Assigning variable values: lvalues */
172#define var_setvalue(var, str) ((var)->value = (str))
173#define var_setfunc(var, func) ((var)->value = (char *)(func))
174#define var_setarray(var, arr) ((var)->value = (char *)(arr))
3185942a 175#define var_setassoc(var, arr) ((var)->value = (char *)(arr))
ac50fbac 176#define var_setref(var, str) ((var)->value = (str))
726f6388 177
7117c2d2
JA
178/* Make VAR be auto-exported. */
179#define set_auto_export(var) \
180 do { (var)->attributes |= att_exported; array_needs_making = 1; } while (0)
ccc6cda3
JA
181
182#define SETVARATTR(var, attr, undo) \
bb70624e
JA
183 ((undo == 0) ? ((var)->attributes |= (attr)) \
184 : ((var)->attributes &= ~(attr)))
185
186#define VSETATTR(var, attr) ((var)->attributes |= (attr))
187#define VUNSETATTR(var, attr) ((var)->attributes &= ~(attr))
188
f73dda09
JA
189#define VGETFLAGS(var) ((var)->attributes)
190
191#define VSETFLAGS(var, flags) ((var)->attributes = (flags))
192#define VCLRFLAGS(var) ((var)->attributes = 0)
193
bb70624e
JA
194/* Macros to perform various operations on `exportstr' member of a SHELL_VAR. */
195#define CLEAR_EXPORTSTR(var) (var)->exportstr = (char *)NULL
196#define COPY_EXPORTSTR(var) ((var)->exportstr) ? savestring ((var)->exportstr) : (char *)NULL
197#define SET_EXPORTSTR(var, value) (var)->exportstr = (value)
198#define SAVE_EXPORTSTR(var, value) (var)->exportstr = (value) ? savestring (value) : (char *)NULL
199
200#define FREE_EXPORTSTR(var) \
7117c2d2 201 do { if ((var)->exportstr) free ((var)->exportstr); } while (0)
bb70624e 202
bb70624e 203#define CACHE_IMPORTSTR(var, value) \
7117c2d2 204 (var)->exportstr = savestring (value)
bb70624e
JA
205
206#define INVALIDATE_EXPORTSTR(var) \
207 do { \
208 if ((var)->exportstr) \
209 { \
7117c2d2 210 free ((var)->exportstr); \
bb70624e 211 (var)->exportstr = (char *)NULL; \
bb70624e
JA
212 } \
213 } while (0)
214
726f6388 215/* Stuff for hacking variables. */
f73dda09
JA
216typedef int sh_var_map_func_t __P((SHELL_VAR *));
217
7117c2d2
JA
218/* Where we keep the variables and functions */
219extern VAR_CONTEXT *global_variables;
220extern VAR_CONTEXT *shell_variables;
221
222extern HASH_TABLE *shell_functions;
223extern HASH_TABLE *temporary_env;
224
726f6388 225extern int variable_context;
726f6388
JA
226extern char *dollar_vars[];
227extern char **export_env;
726f6388 228
ccc6cda3
JA
229extern void initialize_shell_variables __P((char **, int));
230extern SHELL_VAR *set_if_not __P((char *, char *));
28ef6c31 231
7117c2d2 232extern void sh_set_lines_and_columns __P((int, int));
28ef6c31 233extern void set_pwd __P((void));
b72432fd 234extern void set_ppid __P((void));
bb70624e
JA
235extern void make_funcname_visible __P((int));
236
7117c2d2 237extern SHELL_VAR *var_lookup __P((const char *, VAR_CONTEXT *));
f73dda09
JA
238
239extern SHELL_VAR *find_function __P((const char *));
b80f6443 240extern FUNCTION_DEF *find_function_def __P((const char *));
f73dda09 241extern SHELL_VAR *find_variable __P((const char *));
ac50fbac
CR
242extern SHELL_VAR *find_variable_noref __P((const char *));
243extern SHELL_VAR *find_variable_last_nameref __P((const char *));
244extern SHELL_VAR *find_global_variable_last_nameref __P((const char *));
245extern SHELL_VAR *find_variable_nameref __P((SHELL_VAR *));
f73dda09 246extern SHELL_VAR *find_variable_internal __P((const char *, int));
ac50fbac
CR
247extern SHELL_VAR *find_variable_tempenv __P((const char *));
248extern SHELL_VAR *find_variable_notempenv __P((const char *));
495aee44 249extern SHELL_VAR *find_global_variable __P((const char *));
ac50fbac
CR
250extern SHELL_VAR *find_global_variable_noref __P((const char *));
251extern SHELL_VAR *find_shell_variable __P((const char *));
252extern SHELL_VAR *find_tempenv_variable __P((const char *));
726f6388 253extern SHELL_VAR *copy_variable __P((SHELL_VAR *));
f73dda09 254extern SHELL_VAR *make_local_variable __P((const char *));
95732b49 255extern SHELL_VAR *bind_variable __P((const char *, char *, int));
ac50fbac 256extern SHELL_VAR *bind_global_variable __P((const char *, char *, int));
f73dda09 257extern SHELL_VAR *bind_function __P((const char *, COMMAND *));
bb70624e 258
b80f6443
JA
259extern void bind_function_def __P((const char *, FUNCTION_DEF *));
260
7117c2d2
JA
261extern SHELL_VAR **map_over __P((sh_var_map_func_t *, VAR_CONTEXT *));
262SHELL_VAR **map_over_funcs __P((sh_var_map_func_t *));
263
726f6388
JA
264extern SHELL_VAR **all_shell_variables __P((void));
265extern SHELL_VAR **all_shell_functions __P((void));
266extern SHELL_VAR **all_visible_variables __P((void));
267extern SHELL_VAR **all_visible_functions __P((void));
bb70624e 268extern SHELL_VAR **all_exported_variables __P((void));
7117c2d2
JA
269extern SHELL_VAR **local_exported_variables __P((void));
270extern SHELL_VAR **all_local_variables __P((void));
bb70624e
JA
271#if defined (ARRAY_VARS)
272extern SHELL_VAR **all_array_variables __P((void));
273#endif
f73dda09 274extern char **all_variables_matching_prefix __P((const char *));
726f6388
JA
275
276extern char **make_var_array __P((HASH_TABLE *));
d166f048 277extern char **add_or_supercede_exported_var __P((char *, int));
726f6388 278
7117c2d2 279extern char *get_variable_value __P((SHELL_VAR *));
28ef6c31 280extern char *get_string_value __P((const char *));
7117c2d2 281extern char *sh_get_env_value __P((const char *));
95732b49 282extern char *make_variable_value __P((SHELL_VAR *, char *, int));
726f6388 283
95732b49 284extern SHELL_VAR *bind_variable_value __P((SHELL_VAR *, char *, int));
bb70624e 285extern SHELL_VAR *bind_int_variable __P((char *, char *));
7117c2d2 286extern SHELL_VAR *bind_var_to_int __P((char *, intmax_t));
bb70624e 287
495aee44 288extern int assign_in_env __P((WORD_DESC *, int));
95732b49 289
f73dda09 290extern int unbind_variable __P((const char *));
ac50fbac 291extern int unbind_nameref __P((const char *));
7117c2d2 292extern int unbind_func __P((const char *));
b80f6443 293extern int unbind_function_def __P((const char *));
ac50fbac 294extern int delete_var __P((const char *, VAR_CONTEXT *));
7117c2d2 295extern int makunbound __P((const char *, VAR_CONTEXT *));
f73dda09 296extern int kill_local_variable __P((const char *));
726f6388 297extern void delete_all_variables __P((HASH_TABLE *));
7117c2d2
JA
298extern void delete_all_contexts __P((VAR_CONTEXT *));
299
300extern VAR_CONTEXT *new_var_context __P((char *, int));
301extern void dispose_var_context __P((VAR_CONTEXT *));
302extern VAR_CONTEXT *push_var_context __P((char *, int, HASH_TABLE *));
303extern void pop_var_context __P((void));
304extern VAR_CONTEXT *push_scope __P((int, HASH_TABLE *));
305extern void pop_scope __P((int));
306
307extern void push_context __P((char *, int, HASH_TABLE *));
308extern void pop_context __P((void));
309extern void push_dollar_vars __P((void));
310extern void pop_dollar_vars __P((void));
311extern void dispose_saved_dollar_vars __P((void));
726f6388 312
b80f6443
JA
313extern void push_args __P((WORD_LIST *));
314extern void pop_args __P((void));
315
726f6388
JA
316extern void adjust_shell_level __P((int));
317extern void non_unsettable __P((char *));
318extern void dispose_variable __P((SHELL_VAR *));
ccc6cda3 319extern void dispose_used_env_vars __P((void));
726f6388
JA
320extern void dispose_function_env __P((void));
321extern void dispose_builtin_env __P((void));
ccc6cda3
JA
322extern void merge_temporary_env __P((void));
323extern void merge_builtin_env __P((void));
726f6388 324extern void kill_all_local_variables __P((void));
7117c2d2 325
726f6388 326extern void set_var_read_only __P((char *));
f73dda09 327extern void set_func_read_only __P((const char *));
726f6388 328extern void set_var_auto_export __P((char *));
f73dda09 329extern void set_func_auto_export __P((const char *));
7117c2d2 330
726f6388 331extern void sort_variables __P((SHELL_VAR **));
7117c2d2 332
2bbe8058 333extern int chkexport __P((char *));
726f6388 334extern void maybe_make_export_env __P((void));
b72432fd 335extern void update_export_env_inplace __P((char *, int, char *));
726f6388 336extern void put_command_name_into_env __P((char *));
7117c2d2
JA
337extern void put_gnu_argv_flags_into_env __P((intmax_t, char *));
338
726f6388 339extern void print_var_list __P((SHELL_VAR **));
28ef6c31 340extern void print_func_list __P((SHELL_VAR **));
726f6388 341extern void print_assignment __P((SHELL_VAR *));
ccc6cda3 342extern void print_var_value __P((SHELL_VAR *, int));
726f6388
JA
343extern void print_var_function __P((SHELL_VAR *));
344
ccc6cda3
JA
345#if defined (ARRAY_VARS)
346extern SHELL_VAR *make_new_array_variable __P((char *));
ac50fbac 347extern SHELL_VAR *make_local_array_variable __P((char *, int));
cce855bc 348
3185942a
JA
349extern SHELL_VAR *make_new_assoc_variable __P((char *));
350extern SHELL_VAR *make_local_assoc_variable __P((char *));
351
7117c2d2 352extern void set_pipestatus_array __P((int *, int));
495aee44
CR
353extern ARRAY *save_pipestatus_array __P((void));
354extern void restore_pipestatus_array __P((ARRAY *));
ccc6cda3
JA
355#endif
356
cce855bc
JA
357extern void set_pipestatus_from_exit __P((int));
358
359/* The variable in NAME has just had its state changed. Check to see if it
360 is one of the special ones where something special happens. */
361extern void stupidly_hack_special_variables __P((char *));
362
3185942a
JA
363/* Reinitialize some special variables that have external effects upon unset
364 when the shell reinitializes itself. */
365extern void reinit_special_variables __P((void));
366
28ef6c31
JA
367extern int get_random_number __P((void));
368
cce855bc
JA
369/* The `special variable' functions that get called when a particular
370 variable is set. */
7117c2d2 371extern void sv_ifs __P((char *));
f73dda09
JA
372extern void sv_path __P((char *));
373extern void sv_mail __P((char *));
495aee44 374extern void sv_funcnest __P((char *));
f73dda09
JA
375extern void sv_globignore __P((char *));
376extern void sv_ignoreeof __P((char *));
377extern void sv_strict_posix __P((char *));
378extern void sv_optind __P((char *));
379extern void sv_opterr __P((char *));
380extern void sv_locale __P((char *));
0001803f 381extern void sv_xtracefd __P((char *));
ac50fbac 382extern void sv_shcompat __P((char *));
cce855bc
JA
383
384#if defined (READLINE)
b80f6443 385extern void sv_comp_wordbreaks __P((char *));
f73dda09
JA
386extern void sv_terminal __P((char *));
387extern void sv_hostfile __P((char *));
95732b49 388extern void sv_winsize __P((char *));
cce855bc
JA
389#endif
390
95732b49
JA
391#if defined (__CYGWIN__)
392extern void sv_home __P((char *));
cce855bc
JA
393#endif
394
395#if defined (HISTORY)
f73dda09
JA
396extern void sv_histsize __P((char *));
397extern void sv_histignore __P((char *));
398extern void sv_history_control __P((char *));
cce855bc 399# if defined (BANG_HISTORY)
f73dda09 400extern void sv_histchars __P((char *));
cce855bc 401# endif
b80f6443 402extern void sv_histtimefmt __P((char *));
cce855bc
JA
403#endif /* HISTORY */
404
ac50fbac 405#if defined (HAVE_TZSET)
95732b49
JA
406extern void sv_tz __P((char *));
407#endif
408
ac50fbac
CR
409#if defined (JOB_CONTROL)
410extern void sv_childmax __P((char *));
411#endif
412
726f6388 413#endif /* !_VARIABLES_H_ */