Imported from ../bash-2.05a.tar.gz.
[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, 1989, 1991 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 [-afFrxi] [-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   -r    to make NAMEs readonly
37   -x    to make NAMEs export
38   -i    to make NAMEs have the `integer' attribute set
39
40 Variables with the integer attribute have arithmetic evaluation (see
41 `let') done when the variable is assigned to.
42
43 When displaying values of variables, -f displays a function's name
44 and definition.  The -F option restricts the display to function
45 name only.
46
47 Using `+' instead of `-' turns off the given attribute instead.  When
48 used in a function, makes NAMEs local, as with the `local' command.
49 $END
50
51 $BUILTIN typeset
52 $FUNCTION declare_builtin
53 $SHORT_DOC typeset [-afFrxi] [-p] name[=value] ...
54 Obsolete.  See `declare'.
55 $END
56
57 #include <config.h>
58
59 #if defined (HAVE_UNISTD_H)
60 #  ifdef _MINIX
61 #    include <sys/types.h>
62 #  endif
63 #  include <unistd.h>
64 #endif
65
66 #include <stdio.h>
67
68 #include "../bashansi.h"
69
70 #include "../shell.h"
71 #include "common.h"
72 #include "builtext.h"
73
74 extern int array_needs_making;
75
76 static int declare_internal __P((register WORD_LIST *, int));
77
78 /* Declare or change variable attributes. */
79 int
80 declare_builtin (list)
81      register WORD_LIST *list;
82 {
83   return (declare_internal (list, 0));
84 }
85
86 $BUILTIN local
87 $FUNCTION local_builtin
88 $SHORT_DOC local name[=value] ...
89 Create a local variable called NAME, and give it VALUE.  LOCAL
90 can only be used within a function; it makes the variable NAME
91 have a visible scope restricted to that function and its children.
92 $END
93 int
94 local_builtin (list)
95      register WORD_LIST *list;
96 {
97   if (variable_context)
98     return (declare_internal (list, 1));
99   else
100     {
101       builtin_error ("can only be used in a function");
102       return (EXECUTION_FAILURE);
103     }
104 }
105
106 /* The workhorse function. */
107 static int
108 declare_internal (list, local_var)
109      register WORD_LIST *list;
110      int local_var;
111 {
112   int flags_on, flags_off, *flags, any_failed, assign_error, pflag, nodefs;
113   char *t, *subscript_start;
114   SHELL_VAR *var;
115
116   flags_on = flags_off = any_failed = assign_error = pflag = nodefs = 0;
117   while (list)
118     {
119       t = list->word->word;
120       if (t[0] == '-' && t[1] == '-' && t[2] == '\0')
121         {
122           list = list->next;
123           break;
124         }
125
126       if (*t != '+' && *t != '-')
127         break;
128
129       flags = (*t++ == '+') ? &flags_off : &flags_on;
130
131       while (*t)
132         {
133           if (*t == 'p' && local_var == 0)
134             pflag++, t++;
135           else if (*t == 'F')
136             {
137               nodefs++;
138               *flags |= att_function; t++;
139             }
140           else if (*t == 'f')
141             *flags |= att_function, t++;
142           else if (*t == 'x')
143             *flags |= att_exported, t++, array_needs_making = 1;
144           else if (*t == 'r')
145             *flags |= att_readonly, t++;
146           else if (*t == 'i')
147             *flags |= att_integer, t++;
148 #if defined (ARRAY_VARS)
149           else if (*t == 'a')
150             *flags |= att_array, t++;
151 #endif
152           else
153             {
154               builtin_error ("unknown option: `-%c'", *t);
155               builtin_usage ();
156               return (EX_USAGE);
157             }
158         }
159
160       list = list->next;
161     }
162
163   /* If there are no more arguments left, then we just want to show
164      some variables. */
165   if (list == 0)        /* declare -[afFirx] */
166     {
167       /* Show local variables defined at this context level if this is
168          the `local' builtin. */
169       if (local_var)
170         {
171           register SHELL_VAR **vlist;
172           register int i;
173
174           vlist = map_over (variable_in_context, shell_variables);
175
176           if (vlist)
177             {
178               for (i = 0; vlist[i]; i++)
179                 print_assignment (vlist[i]);
180
181               free (vlist);
182             }
183         }
184       else
185         {
186           if (flags_on == 0)
187             set_builtin ((WORD_LIST *)NULL);
188           else
189             set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs);
190         }
191
192       fflush (stdout);
193       return (EXECUTION_SUCCESS);
194     }
195
196   if (pflag)    /* declare -p [-afFirx] name [name...] */
197     {
198       for (any_failed = 0; list; list = list->next)
199         {
200           pflag = show_name_attributes (list->word->word, nodefs);
201           if (pflag)
202             {
203               builtin_error ("%s: not found", list->word->word);
204               any_failed++;
205             }
206         }
207       return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
208     }
209
210 #define NEXT_VARIABLE() free (name); list = list->next; continue
211
212   /* There are arguments left, so we are making variables. */
213   while (list)          /* declare [-afFirx] name [name ...] */
214     {
215       char *value, *name;
216       int offset;
217 #if defined (ARRAY_VARS)
218       int making_array_special, compound_array_assign, simple_array_assign;
219 #endif
220
221       name = savestring (list->word->word);
222       offset = assignment (name);
223
224       if (offset)       /* declare [-afFirx] name=value */
225         {
226           name[offset] = '\0';
227           value = name + offset + 1;
228         }
229       else
230         value = "";
231
232 #if defined (ARRAY_VARS)
233       compound_array_assign = simple_array_assign = 0;
234       subscript_start = (char *)NULL;
235       if (t = strchr (name, '['))       /* ] */
236         {
237           subscript_start = t;
238           *t = '\0';
239           making_array_special = 1;
240         }
241       else
242         making_array_special = 0;
243 #endif
244         
245       if (legal_identifier (name) == 0)
246         {
247           builtin_error ("`%s': not a valid identifier", name);
248           assign_error++;
249           NEXT_VARIABLE ();
250         }
251
252       /* If VARIABLE_CONTEXT has a non-zero value, then we are executing
253          inside of a function.  This means we should make local variables,
254          not global ones. */
255
256       if (variable_context)
257         {
258 #if defined (ARRAY_VARS)
259           if ((flags_on & att_array) || making_array_special)
260             var = make_local_array_variable (name);
261           else
262 #endif
263           var = make_local_variable (name);
264           if (var == 0)
265             {
266               any_failed++;
267               NEXT_VARIABLE ();
268             }
269         }
270
271       /* If we are declaring a function, then complain about it in some way.
272          We don't let people make functions by saying `typeset -f foo=bar'. */
273
274       /* There should be a way, however, to let people look at a particular
275          function definition by saying `typeset -f foo'. */
276
277       if (flags_on & att_function)
278         {
279           if (offset)   /* declare -f [-rix] foo=bar */
280             {
281               builtin_error ("cannot use `-f' to make functions");
282               free (name);
283               return (EXECUTION_FAILURE);
284             }
285           else          /* declare -f [-rx] name [name...] */
286             {
287               var = find_function (name);
288
289               if (var)
290                 {
291                   if (readonly_p (var) && (flags_off & att_readonly))
292                     {
293                       builtin_error ("%s: readonly function", name);
294                       any_failed++;
295                       NEXT_VARIABLE ();
296                     }
297
298                   /* declare -[Ff] name [name...] */
299                   if (flags_on == att_function && flags_off == 0)
300                     {
301                       t = nodefs ? var->name
302                                  : named_function_string (name, function_cell (var), 1);
303                       printf ("%s\n", t);
304                     }
305                   else          /* declare -[fF] -[rx] name [name...] */
306                     {
307                       VSETATTR (var, flags_on);
308                       VUNSETATTR (var, flags_off);
309                     }
310                 }
311               else
312                 any_failed++;
313               NEXT_VARIABLE ();
314             }
315         }
316       else              /* declare -[airx] name [name...] */
317         {
318           var = find_variable (name);
319
320           if (var == 0)
321             {
322 #if defined (ARRAY_VARS)
323               if ((flags_on & att_array) || making_array_special)
324                 var = make_new_array_variable (name);
325               else
326 #endif
327               var = bind_variable (name, "");
328             }
329
330           /* Cannot use declare +r to turn off readonly attribute. */ 
331           if (readonly_p (var) && (flags_off & att_readonly))
332             {
333               builtin_error ("%s: readonly variable", name);
334               any_failed++;
335               NEXT_VARIABLE ();
336             }
337
338           /* Cannot use declare to assign value to readonly or noassign
339              variable. */
340           if ((readonly_p (var) || noassign_p (var)) && offset)
341             {
342               if (readonly_p (var))
343                 builtin_error ("%s: readonly variable", name);
344               assign_error++;
345               NEXT_VARIABLE ();
346             }
347
348 #if defined (ARRAY_VARS)
349           if ((making_array_special || (flags_on & att_array) || array_p (var)) && offset)
350             {
351               if (value[0] == '(' && strchr (value, ')'))
352                 compound_array_assign = 1;
353               else
354                 simple_array_assign = 1;
355             }
356
357           /* Cannot use declare +a name to remove an array variable. */
358           if ((flags_off & att_array) && array_p (var))
359             {
360               builtin_error ("%s: cannot destroy array variables in this way", name);
361               any_failed++;
362               NEXT_VARIABLE ();
363             }
364
365           /* declare -a name makes name an array variable. */
366           if ((making_array_special || (flags_on & att_array)) && array_p (var) == 0)
367             var = convert_var_to_array (var);
368 #endif /* ARRAY_VARS */
369
370           VSETATTR (var, flags_on);
371           VUNSETATTR (var, flags_off);
372
373 #if defined (ARRAY_VARS)
374           if (offset && compound_array_assign)
375             assign_array_var_from_string (var, value);
376           else if (simple_array_assign && subscript_start)
377             {
378               /* declare [-a] name[N]=value */
379               *subscript_start = '[';   /* ] */
380               var = assign_array_element (name, value);
381               *subscript_start = '\0';
382             }
383           else if (simple_array_assign)
384             /* let bind_array_variable take care of this. */
385             bind_array_variable (name, 0, value);
386           else
387 #endif
388           /* bind_variable_value duplicates the essential internals of
389              bind_variable() */
390           if (offset)
391             bind_variable_value (var, value);
392
393           /* If we found this variable in the temporary environment, as with
394              `var=value declare -x var', make sure it is treated identically
395              to `var=value export var'.  Do the same for `declare -r' and
396              `readonly'.  Preserve the attributes, except for att_tempvar. */
397           if ((flags_on & (att_exported|att_readonly)) && tempvar_p (var))
398             {
399               SHELL_VAR *tv;
400               tv = bind_variable (var->name, var->value ? var->value : "");
401               tv->attributes = var->attributes & ~att_tempvar;
402               dispose_variable (var);
403             }
404         }
405
406       stupidly_hack_special_variables (name);
407
408       NEXT_VARIABLE ();
409     }
410
411   return (assign_error ? EX_BADASSIGN
412                        : ((any_failed == 0) ? EXECUTION_SUCCESS
413                                             : EXECUTION_FAILURE));
414 }