]> git.ipfire.org Git - thirdparty/bash.git/blame - variables.h
Bash-5.2 patch 26: fix typo when specifying readline's custom color prefix
[thirdparty/bash.git] / variables.h
CommitLineData
726f6388
JA
1/* variables.h -- data structures for shell variables. */
2
74091dd4 3/* Copyright (C) 1987-2022 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
8868edaf
CR
64typedef struct variable *sh_var_value_func_t PARAMS((struct variable *));
65typedef struct variable *sh_var_assign_func_t PARAMS((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 */
d233b485 128#define att_regenerate 0x0040000 /* regenerate when exported */
7117c2d2
JA
129
130#define attmask_int 0x00ff000
131
132/* Internal attributes used for variable scoping. */
133#define att_tempvar 0x0100000 /* variable came from the temp environment */
134#define att_propagate 0x0200000 /* propagate to previous scope */
135
136#define attmask_scope 0x0f00000
726f6388
JA
137
138#define exported_p(var) ((((var)->attributes) & (att_exported)))
139#define readonly_p(var) ((((var)->attributes) & (att_readonly)))
726f6388
JA
140#define array_p(var) ((((var)->attributes) & (att_array)))
141#define function_p(var) ((((var)->attributes) & (att_function)))
142#define integer_p(var) ((((var)->attributes) & (att_integer)))
ccc6cda3 143#define local_p(var) ((((var)->attributes) & (att_local)))
7117c2d2
JA
144#define assoc_p(var) ((((var)->attributes) & (att_assoc)))
145#define trace_p(var) ((((var)->attributes) & (att_trace)))
3185942a
JA
146#define uppercase_p(var) ((((var)->attributes) & (att_uppercase)))
147#define lowercase_p(var) ((((var)->attributes) & (att_lowercase)))
148#define capcase_p(var) ((((var)->attributes) & (att_capcase)))
ac50fbac 149#define nameref_p(var) ((((var)->attributes) & (att_nameref)))
7117c2d2
JA
150
151#define invisible_p(var) ((((var)->attributes) & (att_invisible)))
152#define non_unsettable_p(var) ((((var)->attributes) & (att_nounset)))
28ef6c31 153#define noassign_p(var) ((((var)->attributes) & (att_noassign)))
7117c2d2
JA
154#define imported_p(var) ((((var)->attributes) & (att_imported)))
155#define specialvar_p(var) ((((var)->attributes) & (att_special)))
3185942a 156#define nofree_p(var) ((((var)->attributes) & (att_nofree)))
d233b485 157#define regen_p(var) ((((var)->attributes) & (att_regenerate)))
7117c2d2
JA
158
159#define tempvar_p(var) ((((var)->attributes) & (att_tempvar)))
d233b485 160#define propagate_p(var) ((((var)->attributes) & (att_propagate)))
7117c2d2 161
a0c0a00f
CR
162/* Variable names: lvalues */
163#define name_cell(var) ((var)->name)
164
8868edaf 165/* Accessing variable values: rvalues */
7117c2d2
JA
166#define value_cell(var) ((var)->value)
167#define function_cell(var) (COMMAND *)((var)->value)
168#define array_cell(var) (ARRAY *)((var)->value)
3185942a 169#define assoc_cell(var) (HASH_TABLE *)((var)->value)
ac50fbac
CR
170#define nameref_cell(var) ((var)->value) /* so it can change later */
171
172#define NAMEREF_MAX 8 /* only 8 levels of nameref indirection */
7117c2d2 173
7117c2d2 174#define var_isset(var) ((var)->value != 0)
a0c0a00f
CR
175#define var_isunset(var) ((var)->value == 0)
176#define var_isnull(var) ((var)->value && *(var)->value == 0)
7117c2d2
JA
177
178/* Assigning variable values: lvalues */
179#define var_setvalue(var, str) ((var)->value = (str))
180#define var_setfunc(var, func) ((var)->value = (char *)(func))
181#define var_setarray(var, arr) ((var)->value = (char *)(arr))
3185942a 182#define var_setassoc(var, arr) ((var)->value = (char *)(arr))
ac50fbac 183#define var_setref(var, str) ((var)->value = (str))
726f6388 184
7117c2d2
JA
185/* Make VAR be auto-exported. */
186#define set_auto_export(var) \
187 do { (var)->attributes |= att_exported; array_needs_making = 1; } while (0)
ccc6cda3
JA
188
189#define SETVARATTR(var, attr, undo) \
bb70624e
JA
190 ((undo == 0) ? ((var)->attributes |= (attr)) \
191 : ((var)->attributes &= ~(attr)))
192
193#define VSETATTR(var, attr) ((var)->attributes |= (attr))
194#define VUNSETATTR(var, attr) ((var)->attributes &= ~(attr))
195
f73dda09
JA
196#define VGETFLAGS(var) ((var)->attributes)
197
198#define VSETFLAGS(var, flags) ((var)->attributes = (flags))
199#define VCLRFLAGS(var) ((var)->attributes = 0)
200
bb70624e
JA
201/* Macros to perform various operations on `exportstr' member of a SHELL_VAR. */
202#define CLEAR_EXPORTSTR(var) (var)->exportstr = (char *)NULL
203#define COPY_EXPORTSTR(var) ((var)->exportstr) ? savestring ((var)->exportstr) : (char *)NULL
204#define SET_EXPORTSTR(var, value) (var)->exportstr = (value)
205#define SAVE_EXPORTSTR(var, value) (var)->exportstr = (value) ? savestring (value) : (char *)NULL
206
207#define FREE_EXPORTSTR(var) \
7117c2d2 208 do { if ((var)->exportstr) free ((var)->exportstr); } while (0)
bb70624e 209
bb70624e 210#define CACHE_IMPORTSTR(var, value) \
7117c2d2 211 (var)->exportstr = savestring (value)
bb70624e
JA
212
213#define INVALIDATE_EXPORTSTR(var) \
214 do { \
215 if ((var)->exportstr) \
216 { \
7117c2d2 217 free ((var)->exportstr); \
bb70624e 218 (var)->exportstr = (char *)NULL; \
bb70624e
JA
219 } \
220 } while (0)
a0c0a00f
CR
221
222#define ifsname(s) ((s)[0] == 'I' && (s)[1] == 'F' && (s)[2] == 'S' && (s)[3] == '\0')
223
8868edaf
CR
224/* Flag values for make_local_variable and its array counterparts */
225#define MKLOC_ASSOCOK 0x01
226#define MKLOC_ARRAYOK 0x02
227#define MKLOC_INHERIT 0x04
d233b485 228
a0c0a00f
CR
229/* Special value for nameref with invalid value for creation or assignment */
230extern SHELL_VAR nameref_invalid_value;
231#define INVALID_NAMEREF_VALUE (void *)&nameref_invalid_value
bb70624e 232
726f6388 233/* Stuff for hacking variables. */
8868edaf 234typedef int sh_var_map_func_t PARAMS((SHELL_VAR *));
f73dda09 235
7117c2d2
JA
236/* Where we keep the variables and functions */
237extern VAR_CONTEXT *global_variables;
238extern VAR_CONTEXT *shell_variables;
239
240extern HASH_TABLE *shell_functions;
241extern HASH_TABLE *temporary_env;
242
726f6388 243extern int variable_context;
726f6388
JA
244extern char *dollar_vars[];
245extern char **export_env;
726f6388 246
d233b485
CR
247extern int tempenv_assign_error;
248extern int array_needs_making;
249extern int shell_level;
250
251/* XXX */
252extern WORD_LIST *rest_of_args;
8868edaf 253extern int posparam_count;
d233b485
CR
254extern pid_t dollar_dollar_pid;
255
8868edaf
CR
256extern int localvar_inherit; /* declared in variables.c */
257
258extern void initialize_shell_variables PARAMS((char **, int));
259
260extern int validate_inherited_value PARAMS((SHELL_VAR *, int));
261
262extern SHELL_VAR *set_if_not PARAMS((char *, char *));
263
264extern void sh_set_lines_and_columns PARAMS((int, int));
265extern void set_pwd PARAMS((void));
266extern void set_ppid PARAMS((void));
267extern void make_funcname_visible PARAMS((int));
268
269extern SHELL_VAR *var_lookup PARAMS((const char *, VAR_CONTEXT *));
270
271extern SHELL_VAR *find_function PARAMS((const char *));
272extern FUNCTION_DEF *find_function_def PARAMS((const char *));
273extern SHELL_VAR *find_variable PARAMS((const char *));
274extern SHELL_VAR *find_variable_noref PARAMS((const char *));
275extern SHELL_VAR *find_variable_last_nameref PARAMS((const char *, int));
276extern SHELL_VAR *find_global_variable_last_nameref PARAMS((const char *, int));
277extern SHELL_VAR *find_variable_nameref PARAMS((SHELL_VAR *));
278extern SHELL_VAR *find_variable_nameref_for_create PARAMS((const char *, int));
279extern SHELL_VAR *find_variable_nameref_for_assignment PARAMS((const char *, int));
280/*extern SHELL_VAR *find_variable_internal PARAMS((const char *, int));*/
281extern SHELL_VAR *find_variable_tempenv PARAMS((const char *));
282extern SHELL_VAR *find_variable_notempenv PARAMS((const char *));
283extern SHELL_VAR *find_global_variable PARAMS((const char *));
284extern SHELL_VAR *find_global_variable_noref PARAMS((const char *));
285extern SHELL_VAR *find_shell_variable PARAMS((const char *));
286extern SHELL_VAR *find_tempenv_variable PARAMS((const char *));
287extern SHELL_VAR *find_variable_no_invisible PARAMS((const char *));
288extern SHELL_VAR *find_variable_for_assignment PARAMS((const char *));
289extern char *nameref_transform_name PARAMS((char *, int));
290extern SHELL_VAR *copy_variable PARAMS((SHELL_VAR *));
291extern SHELL_VAR *make_local_variable PARAMS((const char *, int));
292extern SHELL_VAR *bind_variable PARAMS((const char *, char *, int));
293extern SHELL_VAR *bind_global_variable PARAMS((const char *, char *, int));
294extern SHELL_VAR *bind_function PARAMS((const char *, COMMAND *));
295
296extern void bind_function_def PARAMS((const char *, FUNCTION_DEF *, int));
297
298extern SHELL_VAR **map_over PARAMS((sh_var_map_func_t *, VAR_CONTEXT *));
299SHELL_VAR **map_over_funcs PARAMS((sh_var_map_func_t *));
7117c2d2 300
8868edaf
CR
301extern SHELL_VAR **all_shell_variables PARAMS((void));
302extern SHELL_VAR **all_shell_functions PARAMS((void));
303extern SHELL_VAR **all_visible_variables PARAMS((void));
304extern SHELL_VAR **all_visible_functions PARAMS((void));
305extern SHELL_VAR **all_exported_variables PARAMS((void));
306extern SHELL_VAR **local_exported_variables PARAMS((void));
307extern SHELL_VAR **all_local_variables PARAMS((int));
bb70624e 308#if defined (ARRAY_VARS)
8868edaf 309extern SHELL_VAR **all_array_variables PARAMS((void));
bb70624e 310#endif
8868edaf
CR
311extern char **all_variables_matching_prefix PARAMS((const char *));
312
313extern char **make_var_array PARAMS((HASH_TABLE *));
314extern char **add_or_supercede_exported_var PARAMS((char *, int));
315
316extern char *get_variable_value PARAMS((SHELL_VAR *));
317extern char *get_string_value PARAMS((const char *));
318extern char *sh_get_env_value PARAMS((const char *));
319extern char *make_variable_value PARAMS((SHELL_VAR *, char *, int));
320
321extern SHELL_VAR *bind_variable_value PARAMS((SHELL_VAR *, char *, int));
322extern SHELL_VAR *bind_int_variable PARAMS((char *, char *, int));
74091dd4 323extern SHELL_VAR *bind_var_to_int PARAMS((char *, intmax_t, int));
8868edaf
CR
324
325extern int assign_in_env PARAMS((WORD_DESC *, int));
326
327extern int unbind_variable PARAMS((const char *));
328extern int check_unbind_variable PARAMS((const char *));
329extern int unbind_nameref PARAMS((const char *));
330extern int unbind_variable_noref PARAMS((const char *));
74091dd4
CR
331extern int unbind_global_variable PARAMS((const char *));
332extern int unbind_global_variable_noref PARAMS((const char *));
8868edaf
CR
333extern int unbind_func PARAMS((const char *));
334extern int unbind_function_def PARAMS((const char *));
335extern int delete_var PARAMS((const char *, VAR_CONTEXT *));
336extern int makunbound PARAMS((const char *, VAR_CONTEXT *));
337extern int kill_local_variable PARAMS((const char *));
74091dd4 338
8868edaf
CR
339extern void delete_all_variables PARAMS((HASH_TABLE *));
340extern void delete_all_contexts PARAMS((VAR_CONTEXT *));
74091dd4 341extern void reset_local_contexts PARAMS((void));
8868edaf
CR
342
343extern VAR_CONTEXT *new_var_context PARAMS((char *, int));
344extern void dispose_var_context PARAMS((VAR_CONTEXT *));
345extern VAR_CONTEXT *push_var_context PARAMS((char *, int, HASH_TABLE *));
346extern void pop_var_context PARAMS((void));
347extern VAR_CONTEXT *push_scope PARAMS((int, HASH_TABLE *));
348extern void pop_scope PARAMS((int));
349
350extern void clear_dollar_vars PARAMS((void));
351
352extern void push_context PARAMS((char *, int, HASH_TABLE *));
353extern void pop_context PARAMS((void));
354extern void push_dollar_vars PARAMS((void));
355extern void pop_dollar_vars PARAMS((void));
356extern void dispose_saved_dollar_vars PARAMS((void));
357
358extern void init_bash_argv PARAMS((void));
359extern void save_bash_argv PARAMS((void));
360extern void push_args PARAMS((WORD_LIST *));
361extern void pop_args PARAMS((void));
362
363extern void adjust_shell_level PARAMS((int));
364extern void non_unsettable PARAMS((char *));
365extern void dispose_variable PARAMS((SHELL_VAR *));
366extern void dispose_used_env_vars PARAMS((void));
367extern void dispose_function_env PARAMS((void));
368extern void dispose_builtin_env PARAMS((void));
369extern void merge_temporary_env PARAMS((void));
370extern void flush_temporary_env PARAMS((void));
371extern void merge_builtin_env PARAMS((void));
372extern void kill_all_local_variables PARAMS((void));
373
374extern void set_var_read_only PARAMS((char *));
375extern void set_func_read_only PARAMS((const char *));
376extern void set_var_auto_export PARAMS((char *));
377extern void set_func_auto_export PARAMS((const char *));
378
379extern void sort_variables PARAMS((SHELL_VAR **));
380
381extern int chkexport PARAMS((char *));
382extern void maybe_make_export_env PARAMS((void));
383extern void update_export_env_inplace PARAMS((char *, int, char *));
384extern void put_command_name_into_env PARAMS((char *));
385extern void put_gnu_argv_flags_into_env PARAMS((intmax_t, char *));
386
387extern void print_var_list PARAMS((SHELL_VAR **));
388extern void print_func_list PARAMS((SHELL_VAR **));
389extern void print_assignment PARAMS((SHELL_VAR *));
390extern void print_var_value PARAMS((SHELL_VAR *, int));
391extern void print_var_function PARAMS((SHELL_VAR *));
726f6388 392
ccc6cda3 393#if defined (ARRAY_VARS)
8868edaf
CR
394extern SHELL_VAR *make_new_array_variable PARAMS((char *));
395extern SHELL_VAR *make_local_array_variable PARAMS((char *, int));
cce855bc 396
8868edaf
CR
397extern SHELL_VAR *make_new_assoc_variable PARAMS((char *));
398extern SHELL_VAR *make_local_assoc_variable PARAMS((char *, int));
3185942a 399
8868edaf
CR
400extern void set_pipestatus_array PARAMS((int *, int));
401extern ARRAY *save_pipestatus_array PARAMS((void));
402extern void restore_pipestatus_array PARAMS((ARRAY *));
ccc6cda3
JA
403#endif
404
8868edaf 405extern void set_pipestatus_from_exit PARAMS((int));
cce855bc
JA
406
407/* The variable in NAME has just had its state changed. Check to see if it
408 is one of the special ones where something special happens. */
8868edaf 409extern void stupidly_hack_special_variables PARAMS((char *));
cce855bc 410
3185942a
JA
411/* Reinitialize some special variables that have external effects upon unset
412 when the shell reinitializes itself. */
8868edaf 413extern void reinit_special_variables PARAMS((void));
3185942a 414
8868edaf 415extern int get_random_number PARAMS((void));
28ef6c31 416
cce855bc
JA
417/* The `special variable' functions that get called when a particular
418 variable is set. */
8868edaf
CR
419extern void sv_ifs PARAMS((char *));
420extern void sv_path PARAMS((char *));
421extern void sv_mail PARAMS((char *));
422extern void sv_funcnest PARAMS((char *));
423extern void sv_execignore PARAMS((char *));
424extern void sv_globignore PARAMS((char *));
425extern void sv_ignoreeof PARAMS((char *));
426extern void sv_strict_posix PARAMS((char *));
427extern void sv_optind PARAMS((char *));
428extern void sv_opterr PARAMS((char *));
429extern void sv_locale PARAMS((char *));
430extern void sv_xtracefd PARAMS((char *));
431extern void sv_shcompat PARAMS((char *));
cce855bc
JA
432
433#if defined (READLINE)
8868edaf
CR
434extern void sv_comp_wordbreaks PARAMS((char *));
435extern void sv_terminal PARAMS((char *));
436extern void sv_hostfile PARAMS((char *));
437extern void sv_winsize PARAMS((char *));
cce855bc
JA
438#endif
439
95732b49 440#if defined (__CYGWIN__)
8868edaf 441extern void sv_home PARAMS((char *));
cce855bc
JA
442#endif
443
444#if defined (HISTORY)
8868edaf
CR
445extern void sv_histsize PARAMS((char *));
446extern void sv_histignore PARAMS((char *));
447extern void sv_history_control PARAMS((char *));
cce855bc 448# if defined (BANG_HISTORY)
8868edaf 449extern void sv_histchars PARAMS((char *));
cce855bc 450# endif
8868edaf 451extern void sv_histtimefmt PARAMS((char *));
cce855bc
JA
452#endif /* HISTORY */
453
ac50fbac 454#if defined (HAVE_TZSET)
8868edaf 455extern void sv_tz PARAMS((char *));
95732b49
JA
456#endif
457
ac50fbac 458#if defined (JOB_CONTROL)
8868edaf 459extern void sv_childmax PARAMS((char *));
ac50fbac
CR
460#endif
461
726f6388 462#endif /* !_VARIABLES_H_ */