]> git.ipfire.org Git - thirdparty/bash.git/blob - variables.h
Bash-5.2 patch 26: fix typo when specifying readline's custom color prefix
[thirdparty/bash.git] / variables.h
1 /* variables.h -- data structures for shell variables. */
2
3 /* Copyright (C) 1987-2018 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
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.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #if !defined (_VARIABLES_H_)
22 #define _VARIABLES_H_
23
24 #include "stdc.h"
25 #include "array.h"
26 #include "assoc.h"
27
28 /* Shell variables and functions are stored in hash tables. */
29 #include "hashlib.h"
30
31 #include "conftypes.h"
32
33 /* A variable context. */
34 typedef 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
62 /* What a shell variable looks like. */
63
64 typedef struct variable *sh_var_value_func_t __P((struct variable *));
65 typedef struct variable *sh_var_assign_func_t __P((struct variable *, char *, arrayind_t, char *));
66
67 /* For the future */
68 union _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 */
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 */
80 };
81
82 typedef struct variable {
83 char *name; /* Symbol that the user types. */
84 char *value; /* Value that is returned. */
85 char *exportstr; /* String for the environment. */
86 sh_var_value_func_t *dynamic_value; /* Function called to return a `dynamic'
87 value for a variable, like $SECONDS
88 or $RANDOM. */
89 sh_var_assign_func_t *assign_func; /* Function called when this `special
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. */
94 } SHELL_VAR;
95
96 typedef struct _vlist {
97 SHELL_VAR **list;
98 int list_size; /* allocated size */
99 int list_len; /* current number of entries */
100 } VARLIST;
101
102 /* The various attributes that a given variable can have. */
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 */
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 */
115 #define att_nameref 0x0000800 /* word is a name reference */
116
117 #define user_attrs (att_exported|att_readonly|att_integer|att_local|att_trace|att_uppercase|att_lowercase|att_capcase|att_nameref)
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 */
127 #define att_nofree 0x0020000 /* do not free value on unset */
128 #define att_regenerate 0x0040000 /* regenerate when exported */
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
137
138 #define exported_p(var) ((((var)->attributes) & (att_exported)))
139 #define readonly_p(var) ((((var)->attributes) & (att_readonly)))
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)))
143 #define local_p(var) ((((var)->attributes) & (att_local)))
144 #define assoc_p(var) ((((var)->attributes) & (att_assoc)))
145 #define trace_p(var) ((((var)->attributes) & (att_trace)))
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)))
149 #define nameref_p(var) ((((var)->attributes) & (att_nameref)))
150
151 #define invisible_p(var) ((((var)->attributes) & (att_invisible)))
152 #define non_unsettable_p(var) ((((var)->attributes) & (att_nounset)))
153 #define noassign_p(var) ((((var)->attributes) & (att_noassign)))
154 #define imported_p(var) ((((var)->attributes) & (att_imported)))
155 #define specialvar_p(var) ((((var)->attributes) & (att_special)))
156 #define nofree_p(var) ((((var)->attributes) & (att_nofree)))
157 #define regen_p(var) ((((var)->attributes) & (att_regenerate)))
158
159 #define tempvar_p(var) ((((var)->attributes) & (att_tempvar)))
160 #define propagate_p(var) ((((var)->attributes) & (att_propagate)))
161
162 /* Variable names: lvalues */
163 #define name_cell(var) ((var)->name)
164
165 /* Acessing variable values: rvalues */
166 #define value_cell(var) ((var)->value)
167 #define function_cell(var) (COMMAND *)((var)->value)
168 #define array_cell(var) (ARRAY *)((var)->value)
169 #define assoc_cell(var) (HASH_TABLE *)((var)->value)
170 #define nameref_cell(var) ((var)->value) /* so it can change later */
171
172 #define NAMEREF_MAX 8 /* only 8 levels of nameref indirection */
173
174 #define var_isset(var) ((var)->value != 0)
175 #define var_isunset(var) ((var)->value == 0)
176 #define var_isnull(var) ((var)->value && *(var)->value == 0)
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))
182 #define var_setassoc(var, arr) ((var)->value = (char *)(arr))
183 #define var_setref(var, str) ((var)->value = (str))
184
185 /* Make VAR be auto-exported. */
186 #define set_auto_export(var) \
187 do { (var)->attributes |= att_exported; array_needs_making = 1; } while (0)
188
189 #define SETVARATTR(var, attr, undo) \
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
196 #define VGETFLAGS(var) ((var)->attributes)
197
198 #define VSETFLAGS(var, flags) ((var)->attributes = (flags))
199 #define VCLRFLAGS(var) ((var)->attributes = 0)
200
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) \
208 do { if ((var)->exportstr) free ((var)->exportstr); } while (0)
209
210 #define CACHE_IMPORTSTR(var, value) \
211 (var)->exportstr = savestring (value)
212
213 #define INVALIDATE_EXPORTSTR(var) \
214 do { \
215 if ((var)->exportstr) \
216 { \
217 free ((var)->exportstr); \
218 (var)->exportstr = (char *)NULL; \
219 } \
220 } while (0)
221
222 #define ifsname(s) ((s)[0] == 'I' && (s)[1] == 'F' && (s)[2] == 'S' && (s)[3] == '\0')
223
224 /* Flag values for make_local_variable */
225 #define MKLOC_INHERIT 0x01
226
227 /* Special value for nameref with invalid value for creation or assignment */
228 extern SHELL_VAR nameref_invalid_value;
229 #define INVALID_NAMEREF_VALUE (void *)&nameref_invalid_value
230
231 /* Stuff for hacking variables. */
232 typedef int sh_var_map_func_t __P((SHELL_VAR *));
233
234 /* Where we keep the variables and functions */
235 extern VAR_CONTEXT *global_variables;
236 extern VAR_CONTEXT *shell_variables;
237
238 extern HASH_TABLE *shell_functions;
239 extern HASH_TABLE *temporary_env;
240
241 extern int variable_context;
242 extern char *dollar_vars[];
243 extern char **export_env;
244
245 extern int tempenv_assign_error;
246 extern int array_needs_making;
247 extern int shell_level;
248
249 /* XXX */
250 extern WORD_LIST *rest_of_args;
251 extern pid_t dollar_dollar_pid;
252
253 extern void initialize_shell_variables __P((char **, int));
254
255 extern int validate_inherited_value __P((SHELL_VAR *, int));
256
257 extern SHELL_VAR *set_if_not __P((char *, char *));
258
259 extern void sh_set_lines_and_columns __P((int, int));
260 extern void set_pwd __P((void));
261 extern void set_ppid __P((void));
262 extern void make_funcname_visible __P((int));
263
264 extern SHELL_VAR *var_lookup __P((const char *, VAR_CONTEXT *));
265
266 extern SHELL_VAR *find_function __P((const char *));
267 extern FUNCTION_DEF *find_function_def __P((const char *));
268 extern SHELL_VAR *find_variable __P((const char *));
269 extern SHELL_VAR *find_variable_noref __P((const char *));
270 extern SHELL_VAR *find_variable_last_nameref __P((const char *, int));
271 extern SHELL_VAR *find_global_variable_last_nameref __P((const char *, int));
272 extern SHELL_VAR *find_variable_nameref __P((SHELL_VAR *));
273 extern SHELL_VAR *find_variable_nameref_for_create __P((const char *, int));
274 extern SHELL_VAR *find_variable_nameref_for_assignment __P((const char *, int));
275 /*extern SHELL_VAR *find_variable_internal __P((const char *, int));*/
276 extern SHELL_VAR *find_variable_tempenv __P((const char *));
277 extern SHELL_VAR *find_variable_notempenv __P((const char *));
278 extern SHELL_VAR *find_global_variable __P((const char *));
279 extern SHELL_VAR *find_global_variable_noref __P((const char *));
280 extern SHELL_VAR *find_shell_variable __P((const char *));
281 extern SHELL_VAR *find_tempenv_variable __P((const char *));
282 extern SHELL_VAR *find_variable_no_invisible __P((const char *));
283 extern SHELL_VAR *find_variable_for_assignment __P((const char *));
284 extern char *nameref_transform_name __P((char *, int));
285 extern SHELL_VAR *copy_variable __P((SHELL_VAR *));
286 extern SHELL_VAR *make_local_variable __P((const char *, int));
287 extern SHELL_VAR *bind_variable __P((const char *, char *, int));
288 extern SHELL_VAR *bind_global_variable __P((const char *, char *, int));
289 extern SHELL_VAR *bind_function __P((const char *, COMMAND *));
290
291 extern void bind_function_def __P((const char *, FUNCTION_DEF *, int));
292
293 extern SHELL_VAR **map_over __P((sh_var_map_func_t *, VAR_CONTEXT *));
294 SHELL_VAR **map_over_funcs __P((sh_var_map_func_t *));
295
296 extern SHELL_VAR **all_shell_variables __P((void));
297 extern SHELL_VAR **all_shell_functions __P((void));
298 extern SHELL_VAR **all_visible_variables __P((void));
299 extern SHELL_VAR **all_visible_functions __P((void));
300 extern SHELL_VAR **all_exported_variables __P((void));
301 extern SHELL_VAR **local_exported_variables __P((void));
302 extern SHELL_VAR **all_local_variables __P((void));
303 #if defined (ARRAY_VARS)
304 extern SHELL_VAR **all_array_variables __P((void));
305 #endif
306 extern char **all_variables_matching_prefix __P((const char *));
307
308 extern char **make_var_array __P((HASH_TABLE *));
309 extern char **add_or_supercede_exported_var __P((char *, int));
310
311 extern char *get_variable_value __P((SHELL_VAR *));
312 extern char *get_string_value __P((const char *));
313 extern char *sh_get_env_value __P((const char *));
314 extern char *make_variable_value __P((SHELL_VAR *, char *, int));
315
316 extern SHELL_VAR *bind_variable_value __P((SHELL_VAR *, char *, int));
317 extern SHELL_VAR *bind_int_variable __P((char *, char *, int));
318 extern SHELL_VAR *bind_var_to_int __P((char *, intmax_t));
319
320 extern int assign_in_env __P((WORD_DESC *, int));
321
322 extern int unbind_variable __P((const char *));
323 extern int check_unbind_variable __P((const char *));
324 extern int unbind_nameref __P((const char *));
325 extern int unbind_variable_noref __P((const char *));
326 extern int unbind_func __P((const char *));
327 extern int unbind_function_def __P((const char *));
328 extern int delete_var __P((const char *, VAR_CONTEXT *));
329 extern int makunbound __P((const char *, VAR_CONTEXT *));
330 extern int kill_local_variable __P((const char *));
331 extern void delete_all_variables __P((HASH_TABLE *));
332 extern void delete_all_contexts __P((VAR_CONTEXT *));
333
334 extern VAR_CONTEXT *new_var_context __P((char *, int));
335 extern void dispose_var_context __P((VAR_CONTEXT *));
336 extern VAR_CONTEXT *push_var_context __P((char *, int, HASH_TABLE *));
337 extern void pop_var_context __P((void));
338 extern VAR_CONTEXT *push_scope __P((int, HASH_TABLE *));
339 extern void pop_scope __P((int));
340
341 extern void push_context __P((char *, int, HASH_TABLE *));
342 extern void pop_context __P((void));
343 extern void push_dollar_vars __P((void));
344 extern void pop_dollar_vars __P((void));
345 extern void dispose_saved_dollar_vars __P((void));
346
347 extern void init_bash_argv __P((void));
348 extern void save_bash_argv __P((void));
349 extern void push_args __P((WORD_LIST *));
350 extern void pop_args __P((void));
351
352 extern void adjust_shell_level __P((int));
353 extern void non_unsettable __P((char *));
354 extern void dispose_variable __P((SHELL_VAR *));
355 extern void dispose_used_env_vars __P((void));
356 extern void dispose_function_env __P((void));
357 extern void dispose_builtin_env __P((void));
358 extern void merge_temporary_env __P((void));
359 extern void flush_temporary_env __P((void));
360 extern void merge_builtin_env __P((void));
361 extern void kill_all_local_variables __P((void));
362
363 extern void set_var_read_only __P((char *));
364 extern void set_func_read_only __P((const char *));
365 extern void set_var_auto_export __P((char *));
366 extern void set_func_auto_export __P((const char *));
367
368 extern void sort_variables __P((SHELL_VAR **));
369
370 extern int chkexport __P((char *));
371 extern void maybe_make_export_env __P((void));
372 extern void update_export_env_inplace __P((char *, int, char *));
373 extern void put_command_name_into_env __P((char *));
374 extern void put_gnu_argv_flags_into_env __P((intmax_t, char *));
375
376 extern void print_var_list __P((SHELL_VAR **));
377 extern void print_func_list __P((SHELL_VAR **));
378 extern void print_assignment __P((SHELL_VAR *));
379 extern void print_var_value __P((SHELL_VAR *, int));
380 extern void print_var_function __P((SHELL_VAR *));
381
382 #if defined (ARRAY_VARS)
383 extern SHELL_VAR *make_new_array_variable __P((char *));
384 extern SHELL_VAR *make_local_array_variable __P((char *, int));
385
386 extern SHELL_VAR *make_new_assoc_variable __P((char *));
387 extern SHELL_VAR *make_local_assoc_variable __P((char *, int));
388
389 extern void set_pipestatus_array __P((int *, int));
390 extern ARRAY *save_pipestatus_array __P((void));
391 extern void restore_pipestatus_array __P((ARRAY *));
392 #endif
393
394 extern void set_pipestatus_from_exit __P((int));
395
396 /* The variable in NAME has just had its state changed. Check to see if it
397 is one of the special ones where something special happens. */
398 extern void stupidly_hack_special_variables __P((char *));
399
400 /* Reinitialize some special variables that have external effects upon unset
401 when the shell reinitializes itself. */
402 extern void reinit_special_variables __P((void));
403
404 extern int get_random_number __P((void));
405
406 /* The `special variable' functions that get called when a particular
407 variable is set. */
408 extern void sv_ifs __P((char *));
409 extern void sv_path __P((char *));
410 extern void sv_mail __P((char *));
411 extern void sv_funcnest __P((char *));
412 extern void sv_execignore __P((char *));
413 extern void sv_globignore __P((char *));
414 extern void sv_ignoreeof __P((char *));
415 extern void sv_strict_posix __P((char *));
416 extern void sv_optind __P((char *));
417 extern void sv_opterr __P((char *));
418 extern void sv_locale __P((char *));
419 extern void sv_xtracefd __P((char *));
420 extern void sv_shcompat __P((char *));
421
422 #if defined (READLINE)
423 extern void sv_comp_wordbreaks __P((char *));
424 extern void sv_terminal __P((char *));
425 extern void sv_hostfile __P((char *));
426 extern void sv_winsize __P((char *));
427 #endif
428
429 #if defined (__CYGWIN__)
430 extern void sv_home __P((char *));
431 #endif
432
433 #if defined (HISTORY)
434 extern void sv_histsize __P((char *));
435 extern void sv_histignore __P((char *));
436 extern void sv_history_control __P((char *));
437 # if defined (BANG_HISTORY)
438 extern void sv_histchars __P((char *));
439 # endif
440 extern void sv_histtimefmt __P((char *));
441 #endif /* HISTORY */
442
443 #if defined (HAVE_TZSET)
444 extern void sv_tz __P((char *));
445 #endif
446
447 #if defined (JOB_CONTROL)
448 extern void sv_childmax __P((char *));
449 #endif
450
451 #endif /* !_VARIABLES_H_ */