1 This file is declare.def, from which is created declare.c.
2 It implements the builtins "declare" and "local" in Bash.
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 1, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $FUNCTION declare_builtin
26 $SHORT_DOC declare [-afFrxi] [-p] name[=value] ...
27 Declare variables and/or give them attributes. If no NAMEs are
28 given, then display the values of variables instead. The -p option
29 will display the attributes and values of each NAME.
33 -a to make NAMEs arrays (if supported)
34 -f to select from among function names only
35 -F to display function names without definitions
36 -r to make NAMEs readonly
37 -x to make NAMEs export
38 -i to make NAMEs have the `integer' attribute set
40 Variables with the integer attribute have arithmetic evaluation (see
41 `let') done when the variable is assigned to.
43 When displaying values of variables, -f displays a function's name
44 and definition. The -F option restricts the display to function
47 Using `+' instead of `-' turns off the given attribute instead. When
48 used in a function, makes NAMEs local, as with the `local' command.
52 $FUNCTION declare_builtin
53 $SHORT_DOC typeset [-afFrxi] [-p] name[=value] ...
54 Obsolete. See `declare'.
59 #if defined (HAVE_UNISTD_H)
61 # include <sys/types.h>
68 #include "../bashansi.h"
74 extern int variable_context, array_needs_making;
76 static int declare_internal ();
78 /* Declare or change variable attributes. */
80 declare_builtin (list)
81 register WORD_LIST *list;
83 return (declare_internal (list, 0));
87 $FUNCTION local_builtin
88 $SHORT_DOC local name[=value] ...
89 Create a local variable called NAME, and give it VALUE. LOCAL
90 can only be used within a function; it makes the variable NAME
91 have a visible scope restricted to that function and its children.
95 register WORD_LIST *list;
98 return (declare_internal (list, 1));
101 builtin_error ("can only be used in a function");
102 return (EXECUTION_FAILURE);
106 /* The workhorse function. */
108 declare_internal (list, local_var)
109 register WORD_LIST *list;
112 int flags_on, flags_off, *flags, any_failed, assign_error, pflag, nodefs;
116 flags_on = flags_off = any_failed = assign_error = pflag = nodefs = 0;
119 t = list->word->word;
120 if (t[0] == '-' && t[1] == '-' && t[2] == '\0')
126 if (*t != '+' && *t != '-')
129 flags = (*t++ == '+') ? &flags_off : &flags_on;
133 if (*t == 'p' && local_var == 0)
138 *flags |= att_function; t++;
141 *flags |= att_function, t++;
143 *flags |= att_exported, t++, array_needs_making = 1;
145 *flags |= att_readonly, t++;
147 *flags |= att_integer, t++;
148 #if defined (ARRAY_VARS)
150 *flags |= att_array, t++;
154 builtin_error ("unknown option: `-%c'", *t);
163 /* If there are no more arguments left, then we just want to show
165 if (list == 0) /* declare -[afFirx] */
167 /* Show local variables defined at this context level if this is
168 the `local' builtin. */
171 register SHELL_VAR **vlist;
174 vlist = map_over (variable_in_context, shell_variables);
178 for (i = 0; vlist[i]; i++)
179 print_assignment (vlist[i]);
187 set_builtin ((WORD_LIST *)NULL);
189 set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs);
193 return (EXECUTION_SUCCESS);
196 if (pflag) /* declare -p [-afFirx] name [name...] */
198 for (any_failed = 0; list; list = list->next)
200 pflag = show_name_attributes (list->word->word, nodefs);
203 builtin_error ("%s: not found", list->word->word);
207 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
210 #define NEXT_VARIABLE() free (name); list = list->next; continue
212 /* There are arguments left, so we are making variables. */
213 while (list) /* declare [-afFirx] name [name ...] */
217 #if defined (ARRAY_VARS)
218 int making_array_special, assigning_array_special;
221 name = savestring (list->word->word);
222 offset = assignment (name);
224 if (offset) /* declare [-afFirx] name=value */
227 value = name + offset + 1;
232 #if defined (ARRAY_VARS)
233 assigning_array_special = 0;
234 if (t = strchr (name, '['))
237 making_array_special = 1;
240 making_array_special = 0;
243 if (legal_identifier (name) == 0)
245 builtin_error ("`%s': not a valid identifier", name);
250 /* If VARIABLE_CONTEXT has a non-zero value, then we are executing
251 inside of a function. This means we should make local variables,
254 if (variable_context)
256 #if defined (ARRAY_VARS)
257 if ((flags_on & att_array) || making_array_special)
258 make_local_array_variable (name);
261 make_local_variable (name);
264 /* If we are declaring a function, then complain about it in some way.
265 We don't let people make functions by saying `typeset -f foo=bar'. */
267 /* There should be a way, however, to let people look at a particular
268 function definition by saying `typeset -f foo'. */
270 if (flags_on & att_function)
272 if (offset) /* declare -f [-rix] foo=bar */
274 builtin_error ("cannot use `-f' to make functions");
276 return (EXECUTION_FAILURE);
278 else /* declare -f [-rx] name [name...] */
280 var = find_function (name);
284 if (readonly_p (var) && (flags_off & att_readonly))
286 builtin_error ("%s: readonly function", name);
291 /* declare -[Ff] name [name...] */
292 if (flags_on == att_function && flags_off == 0)
294 t = nodefs ? var->name
295 : named_function_string (name, function_cell (var), 1);
298 else /* declare -[fF] -[rx] name [name...] */
300 var->attributes |= flags_on;
301 var->attributes &= ~flags_off;
309 else /* declare -[airx] name [name...] */
311 var = find_variable (name);
315 #if defined (ARRAY_VARS)
316 if ((flags_on & att_array) || making_array_special)
317 var = make_new_array_variable (name);
320 var = bind_variable (name, "");
323 /* Cannot use declare +r to turn off readonly attribute. */
324 if (readonly_p (var) && (flags_off & att_readonly))
326 builtin_error ("%s: readonly variable", name);
331 /* Cannot use declare to assign value to readonly variable. */
332 if (readonly_p (var) && offset)
334 builtin_error ("%s: readonly variable", name);
339 #if defined (ARRAY_VARS)
340 /* declare -a name=value does not work; declare name=value when
341 name is already an array does not work. */
342 if ((making_array_special || (flags_on & att_array) || array_p (var)) && offset)
344 if (value[0] == '(' && strchr (value, ')'))
345 assigning_array_special = 1;
348 builtin_error ("%s: cannot assign to array variables in this way", name);
354 /* Cannot use declare +a name to remove an array variable. */
355 if ((flags_off & att_array) && array_p (var))
357 builtin_error ("%s: cannot destroy array variables in this way", name);
362 /* declare -a name makes name an array variable. */
363 if ((making_array_special || (flags_on & att_array)) && array_p (var) == 0)
364 var = convert_var_to_array (var);
365 #endif /* ARRAY_VARS */
367 var->attributes |= flags_on;
368 var->attributes &= ~flags_off;
370 #if defined (ARRAY_VARS)
371 if (offset && assigning_array_special)
372 assign_array_var_from_string (var, value);
375 /* This essentially duplicates the internals of bind_variable() */
378 var->attributes &= ~att_invisible;
379 t = make_variable_value (var, value);
384 /* If we found this variable in the temporary environment, as with
385 `var=value declare -x var', make sure it is treated identically
386 to `var=value export var'. Do the same for `declare -r' and
387 `readonly'. Preserve the attributes, except for att_tempvar. */
388 if ((flags_on & (att_exported|att_readonly)) && tempvar_p (var))
391 tv = bind_variable (var->name, var->value ? var->value : "");
392 tv->attributes = var->attributes & ~att_tempvar;
393 dispose_variable (var);
397 stupidly_hack_special_variables (name);
402 return (assign_error ? EX_BADASSIGN
403 : ((any_failed == 0) ? EXECUTION_SUCCESS
404 : EXECUTION_FAILURE));