75b154f8fce65416a0c6029214a210eb470d8bb3
[platform/upstream/bash.git] / builtins / declare.def
1 This file is declare.def, from which is created declare.c.
2 It implements the builtins "declare" and "local" in Bash.
3
4 Copyright (C) 1987-2002 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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
11 version.
12
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
16 for more details.
17
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.
21
22 $PRODUCES declare.c
23
24 $BUILTIN declare
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.
30
31 The flags are:
32
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
40
41 Variables with the integer attribute have arithmetic evaluation (see
42 `let') done when the variable is assigned to.
43
44 When displaying values of variables, -f displays a function's name
45 and definition.  The -F option restricts the display to function
46 name only.
47
48 Using `+' instead of `-' turns off the given attribute instead.  When
49 used in a function, makes NAMEs local, as with the `local' command.
50 $END
51
52 $BUILTIN typeset
53 $FUNCTION declare_builtin
54 $SHORT_DOC typeset [-afFirtx] [-p] name[=value] ...
55 Obsolete.  See `declare'.
56 $END
57
58 #include <config.h>
59
60 #if defined (HAVE_UNISTD_H)
61 #  ifdef _MINIX
62 #    include <sys/types.h>
63 #  endif
64 #  include <unistd.h>
65 #endif
66
67 #include <stdio.h>
68
69 #include "../bashansi.h"
70
71 #include "../shell.h"
72 #include "common.h"
73 #include "builtext.h"
74 #include "bashgetopt.h"
75
76 extern int array_needs_making;
77
78 static int declare_internal __P((register WORD_LIST *, int));
79
80 /* Declare or change variable attributes. */
81 int
82 declare_builtin (list)
83      register WORD_LIST *list;
84 {
85   return (declare_internal (list, 0));
86 }
87
88 $BUILTIN local
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.
94 $END
95 int
96 local_builtin (list)
97      register WORD_LIST *list;
98 {
99   if (variable_context)
100     return (declare_internal (list, 1));
101   else
102     {
103       builtin_error ("can only be used in a function");
104       return (EXECUTION_FAILURE);
105     }
106 }
107
108 #if defined (ARRAY_VARS)
109 #  define DECLARE_OPTS  "+afiprtxF"
110 #else
111 #  define DECLARE_OPTS  "+fiprtxF"
112 #endif
113
114 /* The workhorse function. */
115 static int
116 declare_internal (list, local_var)
117      register WORD_LIST *list;
118      int local_var;
119 {
120   int flags_on, flags_off, *flags, any_failed, assign_error, pflag, nodefs, opt;
121   char *t, *subscript_start;
122   SHELL_VAR *var;
123
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)
127     {
128       flags = list_opttype == '+' ? &flags_off : &flags_on;
129
130       switch (opt)
131         {
132         case 'a':
133 #if defined (ARRAY_VARS)
134           *flags |= att_array;
135 #endif
136           break;
137         case 'p':
138           if (local_var == 0)
139             pflag++;
140           break;
141         case 'F':
142           nodefs++;
143           *flags |= att_function;
144           break;
145         case 'f':
146           *flags |= att_function;
147           break;
148         case 'i':
149           *flags |= att_integer;
150           break;
151         case 'r':
152           *flags |= att_readonly;
153           break;
154         case 't':
155           *flags |= att_trace;
156           break;
157         case 'x':
158           *flags |= att_exported;
159           array_needs_making = 1;
160           break;
161         default:
162           builtin_usage ();
163           return (EX_USAGE);
164         }
165     }
166
167   list = loptend;
168
169   /* If there are no more arguments left, then we just want to show
170      some variables. */
171   if (list == 0)        /* declare -[afFirtx] */
172     {
173       /* Show local variables defined at this context level if this is
174          the `local' builtin. */
175       if (local_var)
176         {
177           register SHELL_VAR **vlist;
178           register int i;
179
180           vlist = all_local_variables ();
181
182           if (vlist)
183             {
184               for (i = 0; vlist[i]; i++)
185                 print_assignment (vlist[i]);
186
187               free (vlist);
188             }
189         }
190       else
191         {
192           if (flags_on == 0)
193             set_builtin ((WORD_LIST *)NULL);
194           else
195             set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs);
196         }
197
198       fflush (stdout);
199       return (EXECUTION_SUCCESS);
200     }
201
202   if (pflag)    /* declare -p [-afFirtx] name [name...] */
203     {
204       for (any_failed = 0; list; list = list->next)
205         {
206           pflag = show_name_attributes (list->word->word, nodefs);
207           if (pflag)
208             {
209               sh_notfound (list->word->word);
210               any_failed++;
211             }
212         }
213       return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
214     }
215
216 #define NEXT_VARIABLE() free (name); list = list->next; continue
217
218   /* There are arguments left, so we are making variables. */
219   while (list)          /* declare [-afFirx] name [name ...] */
220     {
221       char *value, *name;
222       int offset;
223 #if defined (ARRAY_VARS)
224       int making_array_special, compound_array_assign, simple_array_assign;
225 #endif
226
227       name = savestring (list->word->word);
228       offset = assignment (name);
229
230       if (offset)       /* declare [-afFirx] name=value */
231         {
232           name[offset] = '\0';
233           value = name + offset + 1;
234         }
235       else
236         value = "";
237
238 #if defined (ARRAY_VARS)
239       compound_array_assign = simple_array_assign = 0;
240       subscript_start = (char *)NULL;
241       if (t = strchr (name, '['))       /* ] */
242         {
243           subscript_start = t;
244           *t = '\0';
245           making_array_special = 1;
246         }
247       else
248         making_array_special = 0;
249 #endif
250         
251       if (legal_identifier (name) == 0)
252         {
253           sh_invalidid (name);
254           assign_error++;
255           NEXT_VARIABLE ();
256         }
257
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,
260          not global ones. */
261
262       /* XXX - this has consequences when we're making a local copy of a
263                variable that was in the temporary environment.  Watch out
264                for this. */
265       if (variable_context && ((flags_on & att_function) == 0))
266         {
267 #if defined (ARRAY_VARS)
268           if ((flags_on & att_array) || making_array_special)
269             var = make_local_array_variable (name);
270           else
271 #endif
272           var = make_local_variable (name);
273           if (var == 0)
274             {
275               any_failed++;
276               NEXT_VARIABLE ();
277             }
278         }
279       else
280         var = (SHELL_VAR *)NULL;
281
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'. */
284
285       /* There should be a way, however, to let people look at a particular
286          function definition by saying `typeset -f foo'. */
287
288       if (flags_on & att_function)
289         {
290           if (offset)   /* declare -f [-rix] foo=bar */
291             {
292               builtin_error ("cannot use `-f' to make functions");
293               free (name);
294               return (EXECUTION_FAILURE);
295             }
296           else          /* declare -f [-rx] name [name...] */
297             {
298               var = find_function (name);
299
300               if (var)
301                 {
302                   if (readonly_p (var) && (flags_off & att_readonly))
303                     {
304                       builtin_error ("%s: readonly function", name);
305                       any_failed++;
306                       NEXT_VARIABLE ();
307                     }
308
309                   /* declare -[Ff] name [name...] */
310                   if (flags_on == att_function && flags_off == 0)
311                     {
312                       t = nodefs ? var->name
313                                  : named_function_string (name, function_cell (var), 1);
314                       printf ("%s\n", t);
315                     }
316                   else          /* declare -[fF] -[rx] name [name...] */
317                     {
318                       VSETATTR (var, flags_on);
319                       VUNSETATTR (var, flags_off);
320                     }
321                 }
322               else
323                 any_failed++;
324               NEXT_VARIABLE ();
325             }
326         }
327       else              /* declare -[airx] name [name...] */
328         {
329           /* Non-null if we just created or fetched a local variable. */
330           if (var == 0)
331             var = find_variable (name);
332
333           if (var == 0)
334             {
335 #if defined (ARRAY_VARS)
336               if ((flags_on & att_array) || making_array_special)
337                 var = make_new_array_variable (name);
338               else
339 #endif
340               var = bind_variable (name, "");
341             }
342
343           /* Cannot use declare +r to turn off readonly attribute. */ 
344           if (readonly_p (var) && (flags_off & att_readonly))
345             {
346               sh_readonly (name);
347               any_failed++;
348               NEXT_VARIABLE ();
349             }
350
351           /* Cannot use declare to assign value to readonly or noassign
352              variable. */
353           if ((readonly_p (var) || noassign_p (var)) && offset)
354             {
355               if (readonly_p (var))
356                 sh_readonly (name);
357               assign_error++;
358               NEXT_VARIABLE ();
359             }
360
361 #if defined (ARRAY_VARS)
362           if ((making_array_special || (flags_on & att_array) || array_p (var)) && offset)
363             {
364               if (value[0] == '(' && strchr (value, ')'))
365                 compound_array_assign = 1;
366               else
367                 simple_array_assign = 1;
368             }
369
370           /* Cannot use declare +a name to remove an array variable. */
371           if ((flags_off & att_array) && array_p (var))
372             {
373               builtin_error ("%s: cannot destroy array variables in this way", name);
374               any_failed++;
375               NEXT_VARIABLE ();
376             }
377
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 */
382
383           VSETATTR (var, flags_on);
384           VUNSETATTR (var, flags_off);
385
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)
390             {
391               /* declare [-a] name[N]=value */
392               *subscript_start = '[';   /* ] */
393               var = assign_array_element (name, value);
394               *subscript_start = '\0';
395             }
396           else if (simple_array_assign)
397             /* let bind_array_variable take care of this. */
398             bind_array_variable (name, 0, value);
399           else
400 #endif
401           /* bind_variable_value duplicates the essential internals of
402              bind_variable() */
403           if (offset)
404             bind_variable_value (var, value);
405
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
412              global scope.
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))
416             {
417               SHELL_VAR *tv;
418               char *tvalue;
419
420               tv = find_tempenv_variable (var->name);
421               if (tv)
422                 {
423                   tvalue = var_isset (var) ? savestring (value_cell (var)) : savestring ("");
424                   tv = bind_variable (var->name, tvalue);
425                   tv->attributes |= var->attributes & ~att_tempvar;
426                   if (tv->context > 0)
427                     VSETATTR (tv, att_propagate);
428                   free (tvalue);
429                 }
430               VSETATTR (var, att_propagate);
431             }
432         }
433
434       stupidly_hack_special_variables (name);
435
436       NEXT_VARIABLE ();
437     }
438
439   return (assign_error ? EX_BADASSIGN
440                        : ((any_failed == 0) ? EXECUTION_SUCCESS
441                                             : EXECUTION_FAILURE));
442 }