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-2004 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);
234 if (offset) /* declare [-afFirx] name=value */
237 value = name + offset + 1;
238 if (name[offset - 1] == '+')
240 aflags |= ASS_APPEND;
241 name[offset - 1] = '\0';
247 #if defined (ARRAY_VARS)
248 compound_array_assign = simple_array_assign = 0;
249 subscript_start = (char *)NULL;
250 if (t = strchr (name, '[')) /* ] */
254 making_array_special = 1;
257 making_array_special = 0;
260 if (legal_identifier (name) == 0)
267 /* If VARIABLE_CONTEXT has a non-zero value, then we are executing
268 inside of a function. This means we should make local variables,
271 /* XXX - this has consequences when we're making a local copy of a
272 variable that was in the temporary environment. Watch out
274 if (variable_context && ((flags_on & att_function) == 0))
276 #if defined (ARRAY_VARS)
277 if ((flags_on & att_array) || making_array_special)
278 var = make_local_array_variable (name);
281 var = make_local_variable (name);
289 var = (SHELL_VAR *)NULL;
291 /* If we are declaring a function, then complain about it in some way.
292 We don't let people make functions by saying `typeset -f foo=bar'. */
294 /* There should be a way, however, to let people look at a particular
295 function definition by saying `typeset -f foo'. */
297 if (flags_on & att_function)
299 if (offset) /* declare -f [-rix] foo=bar */
301 builtin_error (_("cannot use `-f' to make functions"));
303 return (EXECUTION_FAILURE);
305 else /* declare -f [-rx] name [name...] */
307 var = find_function (name);
311 if (readonly_p (var) && (flags_off & att_readonly))
313 builtin_error (_("%s: readonly function"), name);
318 /* declare -[Ff] name [name...] */
319 if (flags_on == att_function && flags_off == 0)
321 #if defined (DEBUGGER)
322 if (nodefs && debugging_mode)
324 shell_fn = find_function_def (var->name);
326 printf ("%s %d %s\n", var->name, shell_fn->line, shell_fn->source_file);
328 printf ("%s\n", var->name);
331 #endif /* DEBUGGER */
333 t = nodefs ? var->name
334 : named_function_string (name, function_cell (var), 1);
338 else /* declare -[fF] -[rx] name [name...] */
340 VSETATTR (var, flags_on);
341 VUNSETATTR (var, flags_off);
349 else /* declare -[airx] name [name...] */
351 /* Non-null if we just created or fetched a local variable. */
353 var = find_variable (name);
357 #if defined (ARRAY_VARS)
358 if ((flags_on & att_array) || making_array_special)
359 var = make_new_array_variable (name);
362 var = bind_variable (name, "", 0);
365 /* Cannot use declare +r to turn off readonly attribute. */
366 if (readonly_p (var) && (flags_off & att_readonly))
373 /* Cannot use declare to assign value to readonly or noassign
375 if ((readonly_p (var) || noassign_p (var)) && offset)
377 if (readonly_p (var))
383 #if defined (ARRAY_VARS)
384 if ((making_array_special || (flags_on & att_array) || array_p (var)) && offset)
387 vlen = STRLEN (value);
389 if (value[0] == '(' && strchr (value, ')'))
391 if (value[0] == '(' && value[vlen-1] == ')')
393 compound_array_assign = 1;
395 simple_array_assign = 1;
398 /* Cannot use declare +a name to remove an array variable. */
399 if ((flags_off & att_array) && array_p (var))
401 builtin_error (_("%s: cannot destroy array variables in this way"), name);
406 /* declare -a name makes name an array variable. */
407 if ((making_array_special || (flags_on & att_array)) && array_p (var) == 0)
408 var = convert_var_to_array (var);
409 #endif /* ARRAY_VARS */
411 VSETATTR (var, flags_on);
412 VUNSETATTR (var, flags_off);
414 #if defined (ARRAY_VARS)
415 if (offset && compound_array_assign)
416 assign_array_var_from_string (var, value, aflags);
417 else if (simple_array_assign && subscript_start)
419 /* declare [-a] name[N]=value */
420 *subscript_start = '['; /* ] */
421 var = assign_array_element (name, value, 0); /* XXX - not aflags */
422 *subscript_start = '\0';
424 else if (simple_array_assign)
425 /* let bind_array_variable take care of this. */
426 bind_array_variable (name, 0, value, aflags);
429 /* bind_variable_value duplicates the essential internals of
432 bind_variable_value (var, value, aflags);
434 /* If we found this variable in the temporary environment, as with
435 `var=value declare -x var', make sure it is treated identically
436 to `var=value export var'. Do the same for `declare -r' and
437 `readonly'. Preserve the attributes, except for att_tempvar. */
438 /* XXX -- should this create a variable in the global scope, or
439 modify the local variable flags? ksh93 has it modify the
441 Need to handle case like in set_var_attribute where a temporary
442 variable is in the same table as the function local vars. */
443 if ((flags_on & (att_exported|att_readonly)) && tempvar_p (var))
448 tv = find_tempenv_variable (var->name);
451 tvalue = var_isset (var) ? savestring (value_cell (var)) : savestring ("");
452 tv = bind_variable (var->name, tvalue, 0);
453 tv->attributes |= var->attributes & ~att_tempvar;
455 VSETATTR (tv, att_propagate);
458 VSETATTR (var, att_propagate);
462 stupidly_hack_special_variables (name);
467 return (assign_error ? EX_BADASSIGN
468 : ((any_failed == 0) ? EXECUTION_SUCCESS
469 : EXECUTION_FAILURE));