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;
79 extern int posixly_correct;
81 static int declare_internal __P((register WORD_LIST *, int));
83 /* Declare or change variable attributes. */
85 declare_builtin (list)
86 register WORD_LIST *list;
88 return (declare_internal (list, 0));
92 $FUNCTION local_builtin
93 $SHORT_DOC local name[=value] ...
94 Create a local variable called NAME, and give it VALUE. LOCAL
95 can only be used within a function; it makes the variable NAME
96 have a visible scope restricted to that function and its children.
100 register WORD_LIST *list;
102 if (variable_context)
103 return (declare_internal (list, 1));
106 builtin_error (_("can only be used in a function"));
107 return (EXECUTION_FAILURE);
111 #if defined (ARRAY_VARS)
112 # define DECLARE_OPTS "+afiprtxF"
114 # define DECLARE_OPTS "+fiprtxF"
117 /* The workhorse function. */
119 declare_internal (list, local_var)
120 register WORD_LIST *list;
123 int flags_on, flags_off, *flags, any_failed, assign_error, pflag, nodefs, opt;
124 char *t, *subscript_start;
126 FUNCTION_DEF *shell_fn;
128 flags_on = flags_off = any_failed = assign_error = pflag = nodefs = 0;
129 reset_internal_getopt ();
130 while ((opt = internal_getopt (list, DECLARE_OPTS)) != EOF)
132 flags = list_opttype == '+' ? &flags_off : &flags_on;
137 #if defined (ARRAY_VARS)
147 *flags |= att_function;
150 *flags |= att_function;
153 *flags |= att_integer;
156 *flags |= att_readonly;
162 *flags |= att_exported;
163 array_needs_making = 1;
173 /* If there are no more arguments left, then we just want to show
175 if (list == 0) /* declare -[afFirtx] */
177 /* Show local variables defined at this context level if this is
178 the `local' builtin. */
181 register SHELL_VAR **vlist;
184 vlist = all_local_variables ();
188 for (i = 0; vlist[i]; i++)
189 print_assignment (vlist[i]);
197 set_builtin ((WORD_LIST *)NULL);
199 set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs);
203 return (EXECUTION_SUCCESS);
206 if (pflag) /* declare -p [-afFirtx] name [name...] */
208 for (any_failed = 0; list; list = list->next)
210 pflag = show_name_attributes (list->word->word, nodefs);
213 sh_notfound (list->word->word);
217 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
220 #define NEXT_VARIABLE() free (name); list = list->next; continue
222 /* There are arguments left, so we are making variables. */
223 while (list) /* declare [-afFirx] name [name ...] */
227 #if defined (ARRAY_VARS)
228 int making_array_special, compound_array_assign, simple_array_assign;
231 name = savestring (list->word->word);
232 offset = assignment (name, 0);
235 if (offset) /* declare [-afFirx] name=value */
238 value = name + offset + 1;
239 if (name[offset - 1] == '+')
241 aflags |= ASS_APPEND;
242 name[offset - 1] = '\0';
248 #if defined (ARRAY_VARS)
249 compound_array_assign = simple_array_assign = 0;
250 subscript_start = (char *)NULL;
251 if (t = strchr (name, '[')) /* ] */
255 making_array_special = 1;
258 making_array_special = 0;
261 /* If we're in posix mode or not looking for a shell function (since
262 shell function names don't have to be valid identifiers when the
263 shell's not in posix mode), check whether or not the argument is a
264 valid, well-formed shell identifier. */
265 if ((posixly_correct || (flags_on & att_function) == 0) && legal_identifier (name) == 0)
272 /* If VARIABLE_CONTEXT has a non-zero value, then we are executing
273 inside of a function. This means we should make local variables,
276 /* XXX - this has consequences when we're making a local copy of a
277 variable that was in the temporary environment. Watch out
279 if (variable_context && ((flags_on & att_function) == 0))
281 #if defined (ARRAY_VARS)
282 if ((flags_on & att_array) || making_array_special)
283 var = make_local_array_variable (name);
286 var = make_local_variable (name);
294 var = (SHELL_VAR *)NULL;
296 /* If we are declaring a function, then complain about it in some way.
297 We don't let people make functions by saying `typeset -f foo=bar'. */
299 /* There should be a way, however, to let people look at a particular
300 function definition by saying `typeset -f foo'. */
302 if (flags_on & att_function)
304 if (offset) /* declare -f [-rix] foo=bar */
306 builtin_error (_("cannot use `-f' to make functions"));
308 return (EXECUTION_FAILURE);
310 else /* declare -f [-rx] name [name...] */
312 var = find_function (name);
316 if (readonly_p (var) && (flags_off & att_readonly))
318 builtin_error (_("%s: readonly function"), name);
323 /* declare -[Ff] name [name...] */
324 if (flags_on == att_function && flags_off == 0)
326 #if defined (DEBUGGER)
327 if (nodefs && debugging_mode)
329 shell_fn = find_function_def (var->name);
331 printf ("%s %d %s\n", var->name, shell_fn->line, shell_fn->source_file);
333 printf ("%s\n", var->name);
336 #endif /* DEBUGGER */
338 t = nodefs ? var->name
339 : named_function_string (name, function_cell (var), 1);
343 else /* declare -[fF] -[rx] name [name...] */
345 VSETATTR (var, flags_on);
346 VUNSETATTR (var, flags_off);
354 else /* declare -[airx] name [name...] */
356 /* Non-null if we just created or fetched a local variable. */
358 var = find_variable (name);
362 #if defined (ARRAY_VARS)
363 if ((flags_on & att_array) || making_array_special)
364 var = make_new_array_variable (name);
367 var = bind_variable (name, "", 0);
370 /* Cannot use declare +r to turn off readonly attribute. */
371 if (readonly_p (var) && (flags_off & att_readonly))
378 /* Cannot use declare to assign value to readonly or noassign
380 if ((readonly_p (var) || noassign_p (var)) && offset)
382 if (readonly_p (var))
388 #if defined (ARRAY_VARS)
389 if ((making_array_special || (flags_on & att_array) || array_p (var)) && offset)
392 vlen = STRLEN (value);
394 if (value[0] == '(' && strchr (value, ')'))
396 if (value[0] == '(' && value[vlen-1] == ')')
398 compound_array_assign = 1;
400 simple_array_assign = 1;
403 /* Cannot use declare +a name to remove an array variable. */
404 if ((flags_off & att_array) && array_p (var))
406 builtin_error (_("%s: cannot destroy array variables in this way"), name);
411 /* declare -a name makes name an array variable. */
412 if ((making_array_special || (flags_on & att_array)) && array_p (var) == 0)
413 var = convert_var_to_array (var);
414 #endif /* ARRAY_VARS */
416 VSETATTR (var, flags_on);
417 VUNSETATTR (var, flags_off);
419 #if defined (ARRAY_VARS)
420 if (offset && compound_array_assign)
421 assign_array_var_from_string (var, value, aflags);
422 else if (simple_array_assign && subscript_start)
424 /* declare [-a] name[N]=value */
425 *subscript_start = '['; /* ] */
426 var = assign_array_element (name, value, 0); /* XXX - not aflags */
427 *subscript_start = '\0';
429 else if (simple_array_assign)
430 /* let bind_array_variable take care of this. */
431 bind_array_variable (name, 0, value, aflags);
434 /* bind_variable_value duplicates the essential internals of
437 bind_variable_value (var, value, aflags);
439 /* If we found this variable in the temporary environment, as with
440 `var=value declare -x var', make sure it is treated identically
441 to `var=value export var'. Do the same for `declare -r' and
442 `readonly'. Preserve the attributes, except for att_tempvar. */
443 /* XXX -- should this create a variable in the global scope, or
444 modify the local variable flags? ksh93 has it modify the
446 Need to handle case like in set_var_attribute where a temporary
447 variable is in the same table as the function local vars. */
448 if ((flags_on & (att_exported|att_readonly)) && tempvar_p (var))
453 tv = find_tempenv_variable (var->name);
456 tvalue = var_isset (var) ? savestring (value_cell (var)) : savestring ("");
457 tv = bind_variable (var->name, tvalue, 0);
458 tv->attributes |= var->attributes & ~att_tempvar;
460 VSETATTR (tv, att_propagate);
463 VSETATTR (var, att_propagate);
467 stupidly_hack_special_variables (name);
472 return (assign_error ? EX_BADASSIGN
473 : ((any_failed == 0) ? EXECUTION_SUCCESS
474 : EXECUTION_FAILURE));