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-2002 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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION declare_builtin
26 $SHORT_DOC declare [-afFirtx] [-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 -i to make NAMEs have the `integer' attribute
37 -r to make NAMEs readonly
38 -t to make NAMEs have the `trace' attribute
39 -x to make NAMEs export
41 Variables with the integer attribute have arithmetic evaluation (see
42 `let') done when the variable is assigned to.
44 When displaying values of variables, -f displays a function's name
45 and definition. The -F option restricts the display to function
48 Using `+' instead of `-' turns off the given attribute instead. When
49 used in a function, makes NAMEs local, as with the `local' command.
53 $FUNCTION declare_builtin
54 $SHORT_DOC typeset [-afFirtx] [-p] name[=value] ...
55 Obsolete. See `declare'.
60 #if defined (HAVE_UNISTD_H)
62 # include <sys/types.h>
69 #include "../bashansi.h"
74 #include "bashgetopt.h"
76 extern int array_needs_making;
78 static int declare_internal __P((register WORD_LIST *, int));
80 /* Declare or change variable attributes. */
82 declare_builtin (list)
83 register WORD_LIST *list;
85 return (declare_internal (list, 0));
89 $FUNCTION local_builtin
90 $SHORT_DOC local name[=value] ...
91 Create a local variable called NAME, and give it VALUE. LOCAL
92 can only be used within a function; it makes the variable NAME
93 have a visible scope restricted to that function and its children.
97 register WORD_LIST *list;
100 return (declare_internal (list, 1));
103 builtin_error ("can only be used in a function");
104 return (EXECUTION_FAILURE);
108 #if defined (ARRAY_VARS)
109 # define DECLARE_OPTS "+afiprtxF"
111 # define DECLARE_OPTS "+fiprtxF"
114 /* The workhorse function. */
116 declare_internal (list, local_var)
117 register WORD_LIST *list;
120 int flags_on, flags_off, *flags, any_failed, assign_error, pflag, nodefs, opt;
121 char *t, *subscript_start;
124 flags_on = flags_off = any_failed = assign_error = pflag = nodefs = 0;
125 reset_internal_getopt ();
126 while ((opt = internal_getopt (list, DECLARE_OPTS)) != EOF)
128 flags = list_opttype == '+' ? &flags_off : &flags_on;
133 #if defined (ARRAY_VARS)
143 *flags |= att_function;
146 *flags |= att_function;
149 *flags |= att_integer;
152 *flags |= att_readonly;
158 *flags |= att_exported;
159 array_needs_making = 1;
169 /* If there are no more arguments left, then we just want to show
171 if (list == 0) /* declare -[afFirtx] */
173 /* Show local variables defined at this context level if this is
174 the `local' builtin. */
177 register SHELL_VAR **vlist;
180 vlist = all_local_variables ();
184 for (i = 0; vlist[i]; i++)
185 print_assignment (vlist[i]);
193 set_builtin ((WORD_LIST *)NULL);
195 set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs);
199 return (EXECUTION_SUCCESS);
202 if (pflag) /* declare -p [-afFirtx] name [name...] */
204 for (any_failed = 0; list; list = list->next)
206 pflag = show_name_attributes (list->word->word, nodefs);
209 sh_notfound (list->word->word);
213 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
216 #define NEXT_VARIABLE() free (name); list = list->next; continue
218 /* There are arguments left, so we are making variables. */
219 while (list) /* declare [-afFirx] name [name ...] */
223 #if defined (ARRAY_VARS)
224 int making_array_special, compound_array_assign, simple_array_assign;
227 name = savestring (list->word->word);
228 offset = assignment (name);
230 if (offset) /* declare [-afFirx] name=value */
233 value = name + offset + 1;
238 #if defined (ARRAY_VARS)
239 compound_array_assign = simple_array_assign = 0;
240 subscript_start = (char *)NULL;
241 if (t = strchr (name, '[')) /* ] */
245 making_array_special = 1;
248 making_array_special = 0;
251 if (legal_identifier (name) == 0)
258 /* If VARIABLE_CONTEXT has a non-zero value, then we are executing
259 inside of a function. This means we should make local variables,
262 /* XXX - this has consequences when we're making a local copy of a
263 variable that was in the temporary environment. Watch out
265 if (variable_context && ((flags_on & att_function) == 0))
267 #if defined (ARRAY_VARS)
268 if ((flags_on & att_array) || making_array_special)
269 var = make_local_array_variable (name);
272 var = make_local_variable (name);
280 var = (SHELL_VAR *)NULL;
282 /* If we are declaring a function, then complain about it in some way.
283 We don't let people make functions by saying `typeset -f foo=bar'. */
285 /* There should be a way, however, to let people look at a particular
286 function definition by saying `typeset -f foo'. */
288 if (flags_on & att_function)
290 if (offset) /* declare -f [-rix] foo=bar */
292 builtin_error ("cannot use `-f' to make functions");
294 return (EXECUTION_FAILURE);
296 else /* declare -f [-rx] name [name...] */
298 var = find_function (name);
302 if (readonly_p (var) && (flags_off & att_readonly))
304 builtin_error ("%s: readonly function", name);
309 /* declare -[Ff] name [name...] */
310 if (flags_on == att_function && flags_off == 0)
312 t = nodefs ? var->name
313 : named_function_string (name, function_cell (var), 1);
316 else /* declare -[fF] -[rx] name [name...] */
318 VSETATTR (var, flags_on);
319 VUNSETATTR (var, flags_off);
327 else /* declare -[airx] name [name...] */
329 /* Non-null if we just created or fetched a local variable. */
331 var = find_variable (name);
335 #if defined (ARRAY_VARS)
336 if ((flags_on & att_array) || making_array_special)
337 var = make_new_array_variable (name);
340 var = bind_variable (name, "");
343 /* Cannot use declare +r to turn off readonly attribute. */
344 if (readonly_p (var) && (flags_off & att_readonly))
351 /* Cannot use declare to assign value to readonly or noassign
353 if ((readonly_p (var) || noassign_p (var)) && offset)
355 if (readonly_p (var))
361 #if defined (ARRAY_VARS)
362 if ((making_array_special || (flags_on & att_array) || array_p (var)) && offset)
364 if (value[0] == '(' && strchr (value, ')'))
365 compound_array_assign = 1;
367 simple_array_assign = 1;
370 /* Cannot use declare +a name to remove an array variable. */
371 if ((flags_off & att_array) && array_p (var))
373 builtin_error ("%s: cannot destroy array variables in this way", name);
378 /* declare -a name makes name an array variable. */
379 if ((making_array_special || (flags_on & att_array)) && array_p (var) == 0)
380 var = convert_var_to_array (var);
381 #endif /* ARRAY_VARS */
383 VSETATTR (var, flags_on);
384 VUNSETATTR (var, flags_off);
386 #if defined (ARRAY_VARS)
387 if (offset && compound_array_assign)
388 assign_array_var_from_string (var, value);
389 else if (simple_array_assign && subscript_start)
391 /* declare [-a] name[N]=value */
392 *subscript_start = '['; /* ] */
393 var = assign_array_element (name, value);
394 *subscript_start = '\0';
396 else if (simple_array_assign)
397 /* let bind_array_variable take care of this. */
398 bind_array_variable (name, 0, value);
401 /* bind_variable_value duplicates the essential internals of
404 bind_variable_value (var, value);
406 /* If we found this variable in the temporary environment, as with
407 `var=value declare -x var', make sure it is treated identically
408 to `var=value export var'. Do the same for `declare -r' and
409 `readonly'. Preserve the attributes, except for att_tempvar. */
410 /* XXX -- should this create a variable in the global scope, or
411 modify the local variable flags? ksh93 has it modify the
413 Need to handle case like in set_var_attribute where a temporary
414 variable is in the same table as the function local vars. */
415 if ((flags_on & (att_exported|att_readonly)) && tempvar_p (var))
420 tv = find_tempenv_variable (var->name);
423 tvalue = var_isset (var) ? savestring (value_cell (var)) : savestring ("");
424 tv = bind_variable (var->name, tvalue);
425 tv->attributes |= var->attributes & ~att_tempvar;
427 VSETATTR (tv, att_propagate);
430 VSETATTR (var, att_propagate);
434 stupidly_hack_special_variables (name);
439 return (assign_error ? EX_BADASSIGN
440 : ((any_failed == 0) ? EXECUTION_SUCCESS
441 : EXECUTION_FAILURE));