Imported from ../bash-2.04.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
84 #define exported_p(var)         ((((var)->attributes) & (att_exported)))
85 #define readonly_p(var)         ((((var)->attributes) & (att_readonly)))
86 #define invisible_p(var)        ((((var)->attributes) & (att_invisible)))
87 #define array_p(var)            ((((var)->attributes) & (att_array)))
88 #define non_unsettable_p(var)   ((((var)->attributes) & (att_nounset)))
89 #define function_p(var)         ((((var)->attributes) & (att_function)))
90 #define integer_p(var)          ((((var)->attributes) & (att_integer)))
91 #define imported_p(var)         ((((var)->attributes) & (att_imported)))
92 #define local_p(var)            ((((var)->attributes) & (att_local)))
93 #define tempvar_p(var)          ((((var)->attributes) & (att_tempvar)))
94
95 #define value_cell(var) ((var)->value)
96 #define function_cell(var) (COMMAND *)((var)->value)
97 #define array_cell(var) ((ARRAY *)(var)->value)
98
99 #define SETVARATTR(var, attr, undo) \
100         ((undo == 0) ? ((var)->attributes |= (attr)) \
101                      : ((var)->attributes &= ~(attr)))
102
103 #define VSETATTR(var, attr)     ((var)->attributes |= (attr))
104 #define VUNSETATTR(var, attr)   ((var)->attributes &= ~(attr))
105
106 /* Macros to perform various operations on `exportstr' member of a SHELL_VAR. */
107 #define CLEAR_EXPORTSTR(var)    (var)->exportstr = (char *)NULL
108 #define COPY_EXPORTSTR(var)     ((var)->exportstr) ? savestring ((var)->exportstr) : (char *)NULL
109 #define SET_EXPORTSTR(var, value)  (var)->exportstr = (value)
110 #define SAVE_EXPORTSTR(var, value) (var)->exportstr = (value) ? savestring (value) : (char *)NULL
111
112 #define FREE_EXPORTSTR(var) \
113         do { \
114           if ((var)->exportstr) \
115             { \
116               if (((var)->attributes & att_importstr) == 0) \
117                 free ((var)->exportstr); \
118             } \
119         } while (0)
120
121 #if 0
122 #define CACHE_IMPORTSTR(var, value) \
123         do { \
124           (var)->exportstr = value; \
125           (var)->attributes |= att_importstr; \
126         } while (0)
127 #else
128 #define CACHE_IMPORTSTR(var, value) \
129         do { \
130           (var)->exportstr = savestring (value); \
131         } while (0)
132 #endif
133
134 #define INVALIDATE_EXPORTSTR(var) \
135         do { \
136           if ((var)->exportstr) \
137             { \
138               if (((var)->attributes & att_importstr) == 0) \
139                 free ((var)->exportstr); \
140               (var)->exportstr = (char *)NULL; \
141               (var)->attributes &= ~att_importstr; \
142             } \
143         } while (0)
144         
145 /* Stuff for hacking variables. */
146 extern int variable_context;
147 extern HASH_TABLE *shell_variables, *shell_functions;
148 extern char *dollar_vars[];
149 extern char **export_env;
150 extern char **non_unsettable_vars;
151
152 extern void initialize_shell_variables __P((char **, int));
153 extern SHELL_VAR *set_if_not __P((char *, char *));
154 extern void set_lines_and_columns __P((int, int));
155
156 extern void set_ppid __P((void));
157
158 extern void make_funcname_visible __P((int));
159
160 extern SHELL_VAR *find_function __P((char *));
161 extern SHELL_VAR *find_variable __P((char *));
162 extern SHELL_VAR *find_variable_internal __P((char *, int));
163 extern SHELL_VAR *find_tempenv_variable __P((char *));
164 extern SHELL_VAR *copy_variable __P((SHELL_VAR *));
165 extern SHELL_VAR *make_local_variable __P((char *));
166 extern SHELL_VAR *bind_variable __P((char *, char *));
167 extern SHELL_VAR *bind_function __P((char *, COMMAND *));
168
169 extern SHELL_VAR **map_over __P((Function *, HASH_TABLE *));
170 extern SHELL_VAR **all_shell_variables __P((void));
171 extern SHELL_VAR **all_shell_functions __P((void));
172 extern SHELL_VAR **all_visible_variables __P((void));
173 extern SHELL_VAR **all_visible_functions __P((void));
174 extern SHELL_VAR **all_exported_variables __P((void));
175 #if defined (ARRAY_VARS)
176 extern SHELL_VAR **all_array_variables __P((void));
177 #endif
178
179 extern char **all_variables_matching_prefix __P((char *));
180
181 extern char **make_var_array __P((HASH_TABLE *));
182 extern char **add_or_supercede_exported_var __P((char *, int));
183
184 extern char *get_string_value __P((char *));
185 extern char *make_variable_value __P((SHELL_VAR *, char *));
186
187 extern SHELL_VAR *bind_variable_value __P((SHELL_VAR *, char *));
188 extern SHELL_VAR *bind_int_variable __P((char *, char *));
189
190 extern int assignment __P((char *));
191 extern int variable_in_context __P((SHELL_VAR *));
192 extern int assign_in_env __P((char *));
193 extern int unbind_variable __P((char *));
194 extern int makunbound __P((char *, HASH_TABLE *));
195 extern int kill_local_variable __P((char *));
196 extern void delete_all_variables __P((HASH_TABLE *));
197
198 extern void adjust_shell_level __P((int));
199 extern void non_unsettable __P((char *));
200 extern void dispose_variable __P((SHELL_VAR *));
201 extern void dispose_used_env_vars __P((void));
202 extern void dispose_function_env __P((void));
203 extern void dispose_builtin_env __P((void));
204 extern void merge_temporary_env __P((void));
205 extern void merge_builtin_env __P((void));
206 extern void kill_all_local_variables __P((void));
207 extern void set_var_read_only __P((char *));
208 extern void set_func_read_only __P((char *));
209 extern void set_var_auto_export __P((char *));
210 extern void set_func_auto_export __P((char *));
211 extern void sort_variables __P((SHELL_VAR **));
212 extern void maybe_make_export_env __P((void));
213 extern void update_export_env_inplace __P((char *, int, char *));
214 extern void put_command_name_into_env __P((char *));
215 extern void put_gnu_argv_flags_into_env __P((int, char *));
216 extern void print_var_list __P((SHELL_VAR **));
217 extern void print_assignment __P((SHELL_VAR *));
218 extern void print_var_value __P((SHELL_VAR *, int));
219 extern void print_var_function __P((SHELL_VAR *));
220
221 extern char *indirection_level_string __P((void));
222
223 #if defined (ARRAY_VARS)
224 extern SHELL_VAR *make_new_array_variable __P((char *));
225 extern SHELL_VAR *make_local_array_variable __P((char *));
226 extern SHELL_VAR *convert_var_to_array __P((SHELL_VAR *));
227 extern SHELL_VAR *bind_array_variable __P((char *, int, char *));
228 extern SHELL_VAR *assign_array_from_string  __P((char *, char *));
229 extern SHELL_VAR *assign_array_var_from_word_list __P((SHELL_VAR *, WORD_LIST *));
230 extern SHELL_VAR *assign_array_var_from_string __P((SHELL_VAR *, char *));
231 extern int unbind_array_element __P((SHELL_VAR *, char *));
232 extern int skipsubscript __P((char *, int));
233 extern void print_array_assignment __P((SHELL_VAR *, int));
234
235 extern void set_pipestatus_array __P((int *));
236 #endif
237
238 extern void set_pipestatus_from_exit __P((int));
239
240 /* The variable in NAME has just had its state changed.  Check to see if it
241    is one of the special ones where something special happens. */
242 extern void stupidly_hack_special_variables __P((char *));
243
244 /* The `special variable' functions that get called when a particular
245    variable is set. */
246 void sv_path (), sv_mail (), sv_ignoreeof (), sv_strict_posix ();
247 void sv_optind (), sv_opterr (), sv_globignore (), sv_locale ();
248
249 #if defined (READLINE)
250 void sv_terminal (), sv_hostfile ();
251 #endif
252
253 #if defined (HAVE_TZSET) && defined (PROMPT_STRING_DECODE)
254 void sv_tz ();
255 #endif
256
257 #if defined (HISTORY)
258 void sv_histsize (), sv_histignore (), sv_history_control ();
259 #  if defined (BANG_HISTORY)
260 void sv_histchars ();
261 #  endif
262 #endif /* HISTORY */
263
264 #endif /* !_VARIABLES_H_ */