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 [-[frxi]] name[=value] ...
27 Declare variables and/or give them attributes. If no NAMEs are
28 given, then display the values of variables instead.
32 -f to select from among function names only,
33 -r to make NAMEs readonly,
34 -x to make NAMEs export,
35 -i to make NAMEs have the `integer' attribute set.
37 Variables with the integer attribute have arithmetic evaluation (see
38 `let') done when the variable is assigned to.
40 Using `+' instead of `-' turns off the given attribute instead. When
41 used in a function, makes NAMEs local, as with the `local' command.
45 $FUNCTION declare_builtin
46 $SHORT_DOC typeset [-[frxi]] name[=value] ...
47 Obsolete. See `declare'.
52 #if defined (HAVE_STRING_H)
54 #else /* !HAVE_STRING_H */
56 #endif /* !HAVE_STRING_H */
60 extern int variable_context, array_needs_making;
62 static int declare_internal ();
64 /* Declare or change variable attributes. */
66 declare_builtin (list)
67 register WORD_LIST *list;
69 return (declare_internal (list, 0));
73 $FUNCTION local_builtin
74 $SHORT_DOC local name[=value] ...
75 Create a local variable called NAME, and give it VALUE. LOCAL
76 can only be used within a function; it makes the variable NAME
77 have a visible scope restricted to that function and its children.
81 register WORD_LIST *list;
84 return (declare_internal (list, 1));
87 builtin_error ("Can only be used in a function");
88 return (EXECUTION_FAILURE);
92 /* The workhorse function. */
94 declare_internal (list, local_var)
95 register WORD_LIST *list;
98 int flags_on = 0, flags_off = 0;
103 register char *t = list->word->word;
106 if (t[0] == '-' && t[1] == '-' && t[2] == '\0')
112 if (*t != '+' && *t != '-')
125 *flags |= att_function, t++;
127 *flags |= att_exported, t++, array_needs_making = 1;
129 *flags |= att_readonly, t++;
131 *flags |= att_integer, t++;
134 builtin_error ("unknown option: `-%c'", *t);
142 /* If there are no more arguments left, then we just want to show
146 /* Show local variables defined at this context level if this is
147 the `local' builtin. */
150 register SHELL_VAR **vlist;
153 vlist = map_over (variable_in_context, shell_variables);
157 for (i = 0; vlist[i]; i++)
158 print_assignment (vlist[i]);
166 set_builtin ((WORD_LIST *)NULL);
168 set_or_show_attributes ((WORD_LIST *)NULL, flags_on);
172 return (EXECUTION_SUCCESS);
175 #define NEXT_VARIABLE() free (name); list = list->next; continue
177 /* There are arguments left, so we are making variables. */
180 char *value, *name = savestring (list->word->word);
181 int offset = assignment (name);
186 value = name + offset + 1;
191 if (legal_identifier (name) == 0)
193 builtin_error ("%s: not a legal variable name", name);
198 /* If VARIABLE_CONTEXT has a non-zero value, then we are executing
199 inside of a function. This means we should make local variables,
202 if (variable_context)
203 make_local_variable (name);
205 /* If we are declaring a function, then complain about it in some way.
206 We don't let people make functions by saying `typeset -f foo=bar'. */
208 /* There should be a way, however, to let people look at a particular
209 function definition by saying `typeset -f foo'. */
211 if (flags_on & att_function)
215 builtin_error ("Can't use `-f' to make functions");
216 return (EXECUTION_FAILURE);
220 SHELL_VAR *find_function (), *funvar;
222 funvar = find_function (name);
226 if (readonly_p (funvar) && (flags_off & att_readonly))
228 builtin_error ("%s: readonly function", name);
233 if (flags_on == att_function && flags_off == 0)
235 char *result = named_function_string
236 (name, (COMMAND *)function_cell (funvar), 1);
237 printf ("%s\n", result);
241 funvar->attributes |= flags_on;
242 funvar->attributes &= ~flags_off;
254 var = find_variable (name);
257 var = bind_variable (name, "");
259 if (readonly_p (var) && (flags_off & att_readonly))
261 builtin_error ("%s: readonly variable", name);
266 var->attributes |= flags_on;
267 var->attributes &= ~flags_off;
274 long val, evalexp ();
277 val = evalexp (value);
278 var->value = itos ((int)val);
281 var->value = savestring (value);
285 stupidly_hack_special_variables (name);
289 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);