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