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-2003 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 (and line number and source file name if
36 debugging) without definitions
37 -i to make NAMEs have the `integer' attribute
38 -r to make NAMEs readonly
39 -t to make NAMEs have the `trace' attribute
40 -x to make NAMEs export
42 Variables with the integer attribute have arithmetic evaluation (see
43 `let') done when the variable is assigned to.
45 When displaying values of variables, -f displays a function's name
46 and definition. The -F option restricts the display to function
49 Using `+' instead of `-' turns off the given attribute instead. When
50 used in a function, makes NAMEs local, as with the `local' command.
54 $FUNCTION declare_builtin
55 $SHORT_DOC typeset [-afFirtx] [-p] name[=value] ...
56 Obsolete. See `declare'.
61 #if defined (HAVE_UNISTD_H)
63 # include <sys/types.h>
70 #include "../bashansi.h"
71 #include "../bashintl.h"
76 #include "bashgetopt.h"
78 extern int array_needs_making;
80 static int declare_internal __P((register WORD_LIST *, int));
82 /* Declare or change variable attributes. */
84 declare_builtin (list)
85 register WORD_LIST *list;
87 return (declare_internal (list, 0));
91 $FUNCTION local_builtin
92 $SHORT_DOC local name[=value] ...
93 Create a local variable called NAME, and give it VALUE. LOCAL
94 can only be used within a function; it makes the variable NAME
95 have a visible scope restricted to that function and its children.
99 register WORD_LIST *list;
101 if (variable_context)
102 return (declare_internal (list, 1));
105 builtin_error (_("can only be used in a function"));
106 return (EXECUTION_FAILURE);
110 #if defined (ARRAY_VARS)
111 # define DECLARE_OPTS "+afiprtxF"
113 # define DECLARE_OPTS "+fiprtxF"
116 /* The workhorse function. */
118 declare_internal (list, local_var)
119 register WORD_LIST *list;
122 int flags_on, flags_off, *flags, any_failed, assign_error, pflag, nodefs, opt;
123 char *t, *subscript_start;
125 FUNCTION_DEF *shell_fn;
127 flags_on = flags_off = any_failed = assign_error = pflag = nodefs = 0;
128 reset_internal_getopt ();
129 while ((opt = internal_getopt (list, DECLARE_OPTS)) != EOF)
131 flags = list_opttype == '+' ? &flags_off : &flags_on;
136 #if defined (ARRAY_VARS)
146 *flags |= att_function;
149 *flags |= att_function;
152 *flags |= att_integer;
155 *flags |= att_readonly;
161 *flags |= att_exported;
162 array_needs_making = 1;
172 /* If there are no more arguments left, then we just want to show
174 if (list == 0) /* declare -[afFirtx] */
176 /* Show local variables defined at this context level if this is
177 the `local' builtin. */
180 register SHELL_VAR **vlist;
183 vlist = all_local_variables ();
187 for (i = 0; vlist[i]; i++)
188 print_assignment (vlist[i]);
196 set_builtin ((WORD_LIST *)NULL);
198 set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs);
202 return (EXECUTION_SUCCESS);
205 if (pflag) /* declare -p [-afFirtx] name [name...] */
207 for (any_failed = 0; list; list = list->next)
209 pflag = show_name_attributes (list->word->word, nodefs);
212 sh_notfound (list->word->word);
216 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
219 #define NEXT_VARIABLE() free (name); list = list->next; continue
221 /* There are arguments left, so we are making variables. */
222 while (list) /* declare [-afFirx] name [name ...] */
226 #if defined (ARRAY_VARS)
227 int making_array_special, compound_array_assign, simple_array_assign;
230 name = savestring (list->word->word);
231 offset = assignment (name, 0);
233 if (offset) /* declare [-afFirx] name=value */
236 value = name + offset + 1;
241 #if defined (ARRAY_VARS)
242 compound_array_assign = simple_array_assign = 0;
243 subscript_start = (char *)NULL;
244 if (t = strchr (name, '[')) /* ] */
248 making_array_special = 1;
251 making_array_special = 0;
254 if (legal_identifier (name) == 0)
261 /* If VARIABLE_CONTEXT has a non-zero value, then we are executing
262 inside of a function. This means we should make local variables,
265 /* XXX - this has consequences when we're making a local copy of a
266 variable that was in the temporary environment. Watch out
268 if (variable_context && ((flags_on & att_function) == 0))
270 #if defined (ARRAY_VARS)
271 if ((flags_on & att_array) || making_array_special)
272 var = make_local_array_variable (name);
275 var = make_local_variable (name);
283 var = (SHELL_VAR *)NULL;
285 /* If we are declaring a function, then complain about it in some way.
286 We don't let people make functions by saying `typeset -f foo=bar'. */
288 /* There should be a way, however, to let people look at a particular
289 function definition by saying `typeset -f foo'. */
291 if (flags_on & att_function)
293 if (offset) /* declare -f [-rix] foo=bar */
295 builtin_error (_("cannot use `-f' to make functions"));
297 return (EXECUTION_FAILURE);
299 else /* declare -f [-rx] name [name...] */
301 var = find_function (name);
305 if (readonly_p (var) && (flags_off & att_readonly))
307 builtin_error (_("%s: readonly function"), name);
312 /* declare -[Ff] name [name...] */
313 if (flags_on == att_function && flags_off == 0)
315 #if defined (DEBUGGER)
316 if (nodefs && debugging_mode)
318 shell_fn = find_function_def (var->name);
320 printf ("%s %d %s\n", var->name, shell_fn->line, shell_fn->source_file);
322 printf ("%s\n", var->name);
325 #endif /* DEBUGGER */
327 t = nodefs ? var->name
328 : named_function_string (name, function_cell (var), 1);
332 else /* declare -[fF] -[rx] name [name...] */
334 VSETATTR (var, flags_on);
335 VUNSETATTR (var, flags_off);
343 else /* declare -[airx] name [name...] */
345 /* Non-null if we just created or fetched a local variable. */
347 var = find_variable (name);
351 #if defined (ARRAY_VARS)
352 if ((flags_on & att_array) || making_array_special)
353 var = make_new_array_variable (name);
356 var = bind_variable (name, "");
359 /* Cannot use declare +r to turn off readonly attribute. */
360 if (readonly_p (var) && (flags_off & att_readonly))
367 /* Cannot use declare to assign value to readonly or noassign
369 if ((readonly_p (var) || noassign_p (var)) && offset)
371 if (readonly_p (var))
377 #if defined (ARRAY_VARS)
378 if ((making_array_special || (flags_on & att_array) || array_p (var)) && offset)
380 if (value[0] == '(' && strchr (value, ')'))
381 compound_array_assign = 1;
383 simple_array_assign = 1;
386 /* Cannot use declare +a name to remove an array variable. */
387 if ((flags_off & att_array) && array_p (var))
389 builtin_error (_("%s: cannot destroy array variables in this way"), name);
394 /* declare -a name makes name an array variable. */
395 if ((making_array_special || (flags_on & att_array)) && array_p (var) == 0)
396 var = convert_var_to_array (var);
397 #endif /* ARRAY_VARS */
399 VSETATTR (var, flags_on);
400 VUNSETATTR (var, flags_off);
402 #if defined (ARRAY_VARS)
403 if (offset && compound_array_assign)
404 assign_array_var_from_string (var, value);
405 else if (simple_array_assign && subscript_start)
407 /* declare [-a] name[N]=value */
408 *subscript_start = '['; /* ] */
409 var = assign_array_element (name, value);
410 *subscript_start = '\0';
412 else if (simple_array_assign)
413 /* let bind_array_variable take care of this. */
414 bind_array_variable (name, 0, value);
417 /* bind_variable_value duplicates the essential internals of
420 bind_variable_value (var, value);
422 /* If we found this variable in the temporary environment, as with
423 `var=value declare -x var', make sure it is treated identically
424 to `var=value export var'. Do the same for `declare -r' and
425 `readonly'. Preserve the attributes, except for att_tempvar. */
426 /* XXX -- should this create a variable in the global scope, or
427 modify the local variable flags? ksh93 has it modify the
429 Need to handle case like in set_var_attribute where a temporary
430 variable is in the same table as the function local vars. */
431 if ((flags_on & (att_exported|att_readonly)) && tempvar_p (var))
436 tv = find_tempenv_variable (var->name);
439 tvalue = var_isset (var) ? savestring (value_cell (var)) : savestring ("");
440 tv = bind_variable (var->name, tvalue);
441 tv->attributes |= var->attributes & ~att_tempvar;
443 VSETATTR (tv, att_propagate);
446 VSETATTR (var, att_propagate);
450 stupidly_hack_special_variables (name);
455 return (assign_error ? EX_BADASSIGN
456 : ((any_failed == 0) ? EXECUTION_SUCCESS
457 : EXECUTION_FAILURE));