Imported from ../bash-2.05.tar.gz.
[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 /* Placeholder for future modifications if cross-compiling or building a
31    `fat' binary, e.g. on Apple Rhapsody.  These values are used in multiple
32    files, so they appear here. */
33 #if !defined (RHAPSODY)
34 #  define HOSTTYPE      CONF_HOSTTYPE
35 #  define OSTYPE        CONF_OSTYPE
36 #  define MACHTYPE      CONF_MACHTYPE
37 #else /* RHAPSODY */
38 #  if   defined(__powerpc__) || defined(__ppc__)
39 #    define HOSTTYPE "powerpc"
40 #  elif defined(__i386__)
41 #    define HOSTTYPE "i386"
42 #  else
43 #    define HOSTTYPE CONF_HOSTTYPE
44 #  endif
45
46 #  define OSTYPE CONF_OSTYPE
47 #  define VENDOR CONF_VENDOR
48
49 #  define MACHTYPE HOSTTYPE "-" VENDOR "-" OSTYPE
50 #endif /* RHAPSODY */
51
52 /* What a shell variable looks like. */
53
54 typedef struct variable *DYNAMIC_FUNC ();
55
56 typedef struct variable {
57   char *name;                   /* Symbol that the user types. */
58   char *value;                  /* Value that is returned. */
59   char *exportstr;              /* String for the environment. */
60   DYNAMIC_FUNC *dynamic_value;  /* Function called to return a `dynamic'
61                                    value for a variable, like $SECONDS
62                                    or $RANDOM. */
63   DYNAMIC_FUNC *assign_func;    /* Function called when this `special
64                                    variable' is assigned a value in
65                                    bind_variable. */
66   int attributes;               /* export, readonly, array, invisible... */
67   int context;                  /* Which context this variable belongs to. */
68   struct variable *prev_context; /* Value from previous context or NULL. */
69 } SHELL_VAR;
70
71 /* The various attributes that a given variable can have. */
72 #define att_exported  0x001     /* export to environment */
73 #define att_readonly  0x002     /* cannot change */
74 #define att_invisible 0x004     /* cannot see */
75 #define att_array     0x008     /* value is an array */
76 #define att_nounset   0x010     /* cannot unset */
77 #define att_function  0x020     /* value is a function */
78 #define att_integer   0x040     /* internal representation is int */
79 #define att_imported  0x080     /* came from environment */
80 #define att_local     0x100     /* variable is local to a function */
81 #define att_tempvar   0x200     /* variable came from the temp environment */
82 #define att_importstr 0x400     /* exportstr points into initial environment */
83 #define att_noassign  0x800     /* assignment not allowed */
84
85 #define exported_p(var)         ((((var)->attributes) & (att_exported)))
86 #define readonly_p(var)         ((((var)->attributes) & (att_readonly)))
87 #define invisible_p(var)        ((((var)->attributes) & (att_invisible)))
88 #define array_p(var)            ((((var)->attributes) & (att_array)))
89 #define non_unsettable_p(var)   ((((var)->attributes) & (att_nounset)))
90 #define function_p(var)         ((((var)->attributes) & (att_function)))
91 #define integer_p(var)          ((((var)->attributes) & (att_integer)))
92 #define imported_p(var)         ((((var)->attributes) & (att_imported)))
93 #define local_p(var)            ((((var)->attributes) & (att_local)))
94 #define tempvar_p(var)          ((((var)->attributes) & (att_tempvar)))
95 #define noassign_p(var)         ((((var)->attributes) & (att_noassign)))
96
97 #define value_cell(var) ((var)->value)
98 #define function_cell(var) (COMMAND *)((var)->value)
99 #define array_cell(var) ((ARRAY *)(var)->value)
100
101 #define SETVARATTR(var, attr, undo) \
102         ((undo == 0) ? ((var)->attributes |= (attr)) \
103                      : ((var)->attributes &= ~(attr)))
104
105 #define VSETATTR(var, attr)     ((var)->attributes |= (attr))
106 #define VUNSETATTR(var, attr)   ((var)->attributes &= ~(attr))
107
108 /* Macros to perform various operations on `exportstr' member of a SHELL_VAR. */
109 #define CLEAR_EXPORTSTR(var)    (var)->exportstr = (char *)NULL
110 #define COPY_EXPORTSTR(var)     ((var)->exportstr) ? savestring ((var)->exportstr) : (char *)NULL
111 #define SET_EXPORTSTR(var, value)  (var)->exportstr = (value)
112 #define SAVE_EXPORTSTR(var, value) (var)->exportstr = (value) ? savestring (value) : (char *)NULL
113
114 #define FREE_EXPORTSTR(var) \
115         do { \
116           if ((var)->exportstr) \
117             { \
118               if (((var)->attributes & att_importstr) == 0) \
119                 free ((var)->exportstr); \
120             } \
121         } while (0)
122
123 #if 0
124 #define CACHE_IMPORTSTR(var, value) \
125         do { \
126           (var)->exportstr = value; \
127           (var)->attributes |= att_importstr; \
128         } while (0)
129 #else
130 #define CACHE_IMPORTSTR(var, value) \
131         do { \
132           (var)->exportstr = savestring (value); \
133         } while (0)
134 #endif
135
136 #define INVALIDATE_EXPORTSTR(var) \
137         do { \
138           if ((var)->exportstr) \
139             { \
140               if (((var)->attributes & att_importstr) == 0) \
141                 free ((var)->exportstr); \
142               (var)->exportstr = (char *)NULL; \
143               (var)->attributes &= ~att_importstr; \
144             } \
145         } while (0)
146         
147 /* Stuff for hacking variables. */
148 extern int variable_context;
149 extern HASH_TABLE *shell_variables, *shell_functions;
150 extern char *dollar_vars[];
151 extern char **export_env;
152 extern char **non_unsettable_vars;
153
154 extern void initialize_shell_variables __P((char **, int));
155 extern SHELL_VAR *set_if_not __P((char *, char *));
156 extern void sh_set_lines_and_columns __P((int, int));
157
158 extern void set_pwd __P((void));
159
160 extern void set_ppid __P((void));
161
162 extern void make_funcname_visible __P((int));
163
164 extern SHELL_VAR *find_function __P((char *));
165 extern SHELL_VAR *find_variable __P((char *));
166 extern SHELL_VAR *find_variable_internal __P((char *, int));
167 extern SHELL_VAR *find_tempenv_variable __P((char *));
168 extern SHELL_VAR *copy_variable __P((SHELL_VAR *));
169 extern SHELL_VAR *make_local_variable __P((char *));
170 extern SHELL_VAR *bind_variable __P((char *, char *));
171 extern SHELL_VAR *bind_function __P((char *, COMMAND *));
172
173 extern SHELL_VAR **map_over __P((Function *, HASH_TABLE *));
174 extern SHELL_VAR **all_shell_variables __P((void));
175 extern SHELL_VAR **all_shell_functions __P((void));
176 extern SHELL_VAR **all_visible_variables __P((void));
177 extern SHELL_VAR **all_visible_functions __P((void));
178 extern SHELL_VAR **all_exported_variables __P((void));
179 #if defined (ARRAY_VARS)
180 extern SHELL_VAR **all_array_variables __P((void));
181 #endif
182
183 extern char **all_variables_matching_prefix __P((char *));
184
185 extern char **make_var_array __P((HASH_TABLE *));
186 extern char **add_or_supercede_exported_var __P((char *, int));
187
188 extern char *get_string_value __P((const char *));
189 extern char *make_variable_value __P((SHELL_VAR *, char *));
190
191 extern SHELL_VAR *bind_variable_value __P((SHELL_VAR *, char *));
192 extern SHELL_VAR *bind_int_variable __P((char *, char *));
193
194 extern int assignment __P((char *));
195 extern int variable_in_context __P((SHELL_VAR *));
196 extern int assign_in_env __P((char *));
197 extern int unbind_variable __P((char *));
198 extern int makunbound __P((char *, HASH_TABLE *));
199 extern int kill_local_variable __P((char *));
200 extern void delete_all_variables __P((HASH_TABLE *));
201
202 extern void adjust_shell_level __P((int));
203 extern void non_unsettable __P((char *));
204 extern void dispose_variable __P((SHELL_VAR *));
205 extern void dispose_used_env_vars __P((void));
206 extern void dispose_function_env __P((void));
207 extern void dispose_builtin_env __P((void));
208 extern void merge_temporary_env __P((void));
209 extern void merge_builtin_env __P((void));
210 extern void merge_function_env __P((void));
211 extern void kill_all_local_variables __P((void));
212 extern void set_var_read_only __P((char *));
213 extern void set_func_read_only __P((char *));
214 extern void set_var_auto_export __P((char *));
215 extern void set_func_auto_export __P((char *));
216 extern void sort_variables __P((SHELL_VAR **));
217 extern void maybe_make_export_env __P((void));
218 extern void update_export_env_inplace __P((char *, int, char *));
219 extern void put_command_name_into_env __P((char *));
220 extern void put_gnu_argv_flags_into_env __P((int, char *));
221 extern void print_var_list __P((SHELL_VAR **));
222 extern void print_func_list __P((SHELL_VAR **));
223 extern void print_assignment __P((SHELL_VAR *));
224 extern void print_var_value __P((SHELL_VAR *, int));
225 extern void print_var_function __P((SHELL_VAR *));
226
227 extern char *indirection_level_string __P((void));
228
229 #if defined (ARRAY_VARS)
230 extern SHELL_VAR *make_new_array_variable __P((char *));
231 extern SHELL_VAR *make_local_array_variable __P((char *));
232 extern SHELL_VAR *convert_var_to_array __P((SHELL_VAR *));
233 extern SHELL_VAR *bind_array_variable __P((char *, int, char *));
234 extern SHELL_VAR *assign_array_from_string  __P((char *, char *));
235 extern SHELL_VAR *assign_array_var_from_word_list __P((SHELL_VAR *, WORD_LIST *));
236 extern SHELL_VAR *assign_array_var_from_string __P((SHELL_VAR *, char *));
237 extern int unbind_array_element __P((SHELL_VAR *, char *));
238 extern int skipsubscript __P((char *, int));
239 extern void print_array_assignment __P((SHELL_VAR *, int));
240
241 extern void set_pipestatus_array __P((int *));
242 #endif
243
244 extern void set_pipestatus_from_exit __P((int));
245
246 /* The variable in NAME has just had its state changed.  Check to see if it
247    is one of the special ones where something special happens. */
248 extern void stupidly_hack_special_variables __P((char *));
249
250 extern int get_random_number __P((void));
251
252 /* The `special variable' functions that get called when a particular
253    variable is set. */
254 void sv_path (), sv_mail (), sv_ignoreeof (), sv_strict_posix ();
255 void sv_optind (), sv_opterr (), sv_globignore (), sv_locale ();
256
257 #if defined (READLINE)
258 void sv_terminal (), sv_hostfile ();
259 #endif
260
261 #if defined (HAVE_TZSET) && defined (PROMPT_STRING_DECODE)
262 void sv_tz ();
263 #endif
264
265 #if defined (HISTORY)
266 void sv_histsize (), sv_histignore (), sv_history_control ();
267 #  if defined (BANG_HISTORY)
268 void sv_histchars ();
269 #  endif
270 #endif /* HISTORY */
271
272 #endif /* !_VARIABLES_H_ */