Imported from ../bash-4.0-rc1.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-2009 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
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bash.  If not, see <http://www.gnu.org/licenses/>.
20
21 $PRODUCES declare.c
22
23 $BUILTIN declare
24 $FUNCTION declare_builtin
25 $SHORT_DOC declare [-aAfFilrtux] [-p] [name[=value] ...]
26 Set variable values and attributes.
27
28 Declare variables and give them attributes.  If no NAMEs are given,
29 display the attributes and values of all variables.
30
31 Options:
32   -f    restrict action or display to function names and definitions
33   -F    restrict display to function names only (plus line number and
34         source file when debugging)
35   -p    display the attributes and value of each NAME
36
37 Options which set attributes:
38   -a    to make NAMEs indexed arrays (if supported)
39   -A    to make NAMEs associative arrays (if supported)
40   -i    to make NAMEs have the `integer' attribute
41   -l    to convert NAMEs to lower case on assignment
42   -r    to make NAMEs readonly
43   -t    to make NAMEs have the `trace' attribute
44   -u    to convert NAMEs to upper case on assignment
45   -x    to make NAMEs export
46
47 Using `+' instead of `-' turns off the given attribute.
48
49 Variables with the integer attribute have arithmetic evaluation (see
50 the `let' command) performed when the variable is assigned a value.
51
52 When used in a function, `declare' makes NAMEs local, as with the `local'
53 command.
54
55 Exit Status:
56 Returns success unless an invalid option is supplied or an error occurs.
57 $END
58
59 $BUILTIN typeset
60 $FUNCTION declare_builtin
61 $SHORT_DOC typeset [-aAfFilrtux] [-p] name[=value] ...
62 Set variable values and attributes.
63
64 Obsolete.  See `help declare'.
65 $END
66
67 #include <config.h>
68
69 #if defined (HAVE_UNISTD_H)
70 #  ifdef _MINIX
71 #    include <sys/types.h>
72 #  endif
73 #  include <unistd.h>
74 #endif
75
76 #include <stdio.h>
77
78 #include "../bashansi.h"
79 #include "../bashintl.h"
80
81 #include "../shell.h"
82 #include "common.h"
83 #include "builtext.h"
84 #include "bashgetopt.h"
85
86 extern int array_needs_making;
87 extern int posixly_correct;
88
89 static int declare_internal __P((register WORD_LIST *, int));
90
91 /* Declare or change variable attributes. */
92 int
93 declare_builtin (list)
94      register WORD_LIST *list;
95 {
96   return (declare_internal (list, 0));
97 }
98
99 $BUILTIN local
100 $FUNCTION local_builtin
101 $SHORT_DOC local [option] name[=value] ...
102 Define local variables.
103
104 Create a local variable called NAME, and give it VALUE.  OPTION can
105 be any option accepted by `declare'.
106
107 Local variables can only be used within a function; they are visible
108 only to the function where they are defined and its children.
109
110 Exit Status:
111 Returns success unless an invalid option is supplied, an error occurs,
112 or the shell is not executing a function.
113 $END
114 int
115 local_builtin (list)
116      register WORD_LIST *list;
117 {
118   if (variable_context)
119     return (declare_internal (list, 1));
120   else
121     {
122       builtin_error (_("can only be used in a function"));
123       return (EXECUTION_FAILURE);
124     }
125 }
126
127 #if defined (ARRAY_VARS)
128 #  define DECLARE_OPTS  "+acfilprtuxAF"
129 #else
130 #  define DECLARE_OPTS  "+cfilprtuxF"
131 #endif
132
133 /* The workhorse function. */
134 static int
135 declare_internal (list, local_var)
136      register WORD_LIST *list;
137      int local_var;
138 {
139   int flags_on, flags_off, *flags;
140   int any_failed, assign_error, pflag, nodefs, opt;
141   char *t, *subscript_start;
142   SHELL_VAR *var;
143   FUNCTION_DEF *shell_fn;
144
145   flags_on = flags_off = any_failed = assign_error = pflag = nodefs = 0;
146   reset_internal_getopt ();
147   while ((opt = internal_getopt (list, DECLARE_OPTS)) != EOF)
148     {
149       flags = list_opttype == '+' ? &flags_off : &flags_on;
150
151       switch (opt)
152         {
153         case 'a':
154 #if defined (ARRAY_VARS)
155           *flags |= att_array;
156           break;
157 #else
158           builtin_usage ();
159           return (EX_USAGE);
160 #endif
161         case 'A':
162 #if defined (ARRAY_VARS)
163           *flags |= att_assoc;
164           break;
165 #else
166           builtin_usage ();
167           return (EX_USAGE);
168 #endif
169         case 'p':
170           if (local_var == 0)
171             pflag++;
172           break;
173         case 'F':
174           nodefs++;
175           *flags |= att_function;
176           break;
177         case 'f':
178           *flags |= att_function;
179           break;
180         case 'i':
181           *flags |= att_integer;
182           break;
183         case 'r':
184           *flags |= att_readonly;
185           break;
186         case 't':
187           *flags |= att_trace;
188           break;
189         case 'x':
190           *flags |= att_exported;
191           array_needs_making = 1;
192           break;
193 #if defined (CASEMOD_ATTRS)
194 #  if defined (CASEMOD_CAPCASE)
195          case 'c':
196           *flags |= att_capcase;
197           if (flags == &flags_on)
198             flags_off |= att_uppercase|att_lowercase;
199           break;
200 #  endif
201         case 'l':
202           *flags |= att_lowercase;
203           if (flags == &flags_on)
204             flags_off |= att_capcase|att_uppercase;
205           break;
206         case 'u':
207           *flags |= att_uppercase;
208           if (flags == &flags_on)
209             flags_off |= att_capcase|att_lowercase;
210           break;
211 #endif /* CASEMOD_ATTRS */
212         default:
213           builtin_usage ();
214           return (EX_USAGE);
215         }
216     }
217
218   list = loptend;
219
220   /* If there are no more arguments left, then we just want to show
221      some variables. */
222   if (list == 0)        /* declare -[aAfFirtx] */
223     {
224       /* Show local variables defined at this context level if this is
225          the `local' builtin. */
226       if (local_var)
227         {
228           register SHELL_VAR **vlist;
229           register int i;
230
231           vlist = all_local_variables ();
232
233           if (vlist)
234             {
235               for (i = 0; vlist[i]; i++)
236                 print_assignment (vlist[i]);
237
238               free (vlist);
239             }
240         }
241       else if (pflag && (flags_on == 0 || flags_on == att_function))
242         show_all_var_attributes (flags_on == 0, nodefs);
243       else if (flags_on == 0)
244         return (set_builtin ((WORD_LIST *)NULL));
245       else
246         set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs);
247
248       return (sh_chkwrite (EXECUTION_SUCCESS));
249     }
250
251   if (pflag)    /* declare -p [-aAfFirtx] name [name...] */
252     {
253       for (any_failed = 0; list; list = list->next)
254         {
255           pflag = show_name_attributes (list->word->word, nodefs);
256           if (pflag)
257             {
258               sh_notfound (list->word->word);
259               any_failed++;
260             }
261         }
262       return (sh_chkwrite (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS));
263     }
264
265 #define NEXT_VARIABLE() free (name); list = list->next; continue
266
267   /* There are arguments left, so we are making variables. */
268   while (list)          /* declare [-aAfFirx] name [name ...] */
269     {
270       char *value, *name;
271       int offset, aflags;
272 #if defined (ARRAY_VARS)
273       int making_array_special, compound_array_assign, simple_array_assign;
274 #endif
275
276       name = savestring (list->word->word);
277       offset = assignment (name, 0);
278       aflags = 0;
279
280       if (offset)       /* declare [-aAfFirx] name=value */
281         {
282           name[offset] = '\0';
283           value = name + offset + 1;
284           if (name[offset - 1] == '+')
285             {
286               aflags |= ASS_APPEND;
287               name[offset - 1] = '\0';
288             }
289         }
290       else
291         value = "";
292
293 #if defined (ARRAY_VARS)
294       compound_array_assign = simple_array_assign = 0;
295       subscript_start = (char *)NULL;
296       if (t = strchr (name, '['))       /* ] */
297         {
298           subscript_start = t;
299           *t = '\0';
300           making_array_special = 1;
301         }
302       else
303         making_array_special = 0;
304 #endif
305
306       /* If we're in posix mode or not looking for a shell function (since
307          shell function names don't have to be valid identifiers when the
308          shell's not in posix mode), check whether or not the argument is a
309          valid, well-formed shell identifier. */
310       if ((posixly_correct || (flags_on & att_function) == 0) && legal_identifier (name) == 0)
311         {
312           sh_invalidid (name);
313           assign_error++;
314           NEXT_VARIABLE ();
315         }
316
317       /* If VARIABLE_CONTEXT has a non-zero value, then we are executing
318          inside of a function.  This means we should make local variables,
319          not global ones. */
320
321       /* XXX - this has consequences when we're making a local copy of a
322                variable that was in the temporary environment.  Watch out
323                for this. */
324       if (variable_context && ((flags_on & att_function) == 0))
325         {
326 #if defined (ARRAY_VARS)
327           if (flags_on & att_assoc)
328             var = make_local_assoc_variable (name);
329           else if ((flags_on & att_array) || making_array_special)
330             var = make_local_array_variable (name);
331           else
332 #endif
333           var = make_local_variable (name);
334           if (var == 0)
335             {
336               any_failed++;
337               NEXT_VARIABLE ();
338             }
339         }
340       else
341         var = (SHELL_VAR *)NULL;
342
343       /* If we are declaring a function, then complain about it in some way.
344          We don't let people make functions by saying `typeset -f foo=bar'. */
345
346       /* There should be a way, however, to let people look at a particular
347          function definition by saying `typeset -f foo'. */
348
349       if (flags_on & att_function)
350         {
351           if (offset)   /* declare -f [-rix] foo=bar */
352             {
353               builtin_error (_("cannot use `-f' to make functions"));
354               free (name);
355               return (EXECUTION_FAILURE);
356             }
357           else          /* declare -f [-rx] name [name...] */
358             {
359               var = find_function (name);
360
361               if (var)
362                 {
363                   if (readonly_p (var) && (flags_off & att_readonly))
364                     {
365                       builtin_error (_("%s: readonly function"), name);
366                       any_failed++;
367                       NEXT_VARIABLE ();
368                     }
369
370                   /* declare -[Ff] name [name...] */
371                   if (flags_on == att_function && flags_off == 0)
372                     {
373 #if defined (DEBUGGER)
374                       if (nodefs && debugging_mode)
375                         {
376                           shell_fn = find_function_def (var->name);
377                           if (shell_fn)
378                             printf ("%s %d %s\n", var->name, shell_fn->line, shell_fn->source_file);
379                           else
380                             printf ("%s\n", var->name);
381                         }
382                       else
383 #endif /* DEBUGGER */
384                         {       
385                           t = nodefs ? var->name
386                                      : named_function_string (name, function_cell (var), FUNC_MULTILINE|FUNC_EXTERNAL);
387                           printf ("%s\n", t);
388                           any_failed = sh_chkwrite (any_failed);
389                         }
390                     }
391                   else          /* declare -[fF] -[rx] name [name...] */
392                     {
393                       VSETATTR (var, flags_on);
394                       VUNSETATTR (var, flags_off);
395                     }
396                 }
397               else
398                 any_failed++;
399               NEXT_VARIABLE ();
400             }
401         }
402       else              /* declare -[aAirx] name [name...] */
403         {
404           /* Non-null if we just created or fetched a local variable. */
405           if (var == 0)
406             var = find_variable (name);
407
408           if (var == 0)
409             {
410 #if defined (ARRAY_VARS)
411               if (flags_on & att_assoc)
412                 var = make_new_assoc_variable (name);
413               else if ((flags_on & att_array) || making_array_special)
414                 var = make_new_array_variable (name);
415               else
416 #endif
417
418               if (offset)
419                 var = bind_variable (name, "", 0);
420               else
421                 {
422                   var = bind_variable (name, (char *)NULL, 0);
423                   VSETATTR (var, att_invisible);
424                 }
425             }
426
427           /* Cannot use declare +r to turn off readonly attribute. */ 
428           if (readonly_p (var) && (flags_off & att_readonly))
429             {
430               sh_readonly (name);
431               any_failed++;
432               NEXT_VARIABLE ();
433             }
434
435           /* Cannot use declare to assign value to readonly or noassign
436              variable. */
437           if ((readonly_p (var) || noassign_p (var)) && offset)
438             {
439               if (readonly_p (var))
440                 sh_readonly (name);
441               assign_error++;
442               NEXT_VARIABLE ();
443             }
444
445 #if defined (ARRAY_VARS)
446           if ((making_array_special || (flags_on & (att_array|att_assoc)) || array_p (var) || assoc_p (var)) && offset)
447             {
448               int vlen;
449               vlen = STRLEN (value);
450
451               if (value[0] == '(' && value[vlen-1] == ')')
452                 compound_array_assign = 1;
453               else
454                 simple_array_assign = 1;
455             }
456
457           /* Cannot use declare +a name or declare +A name to remove an
458              array variable. */
459           if (((flags_off & att_array) && array_p (var)) || ((flags_off & att_assoc) && assoc_p (var)))
460             {
461               builtin_error (_("%s: cannot destroy array variables in this way"), name);
462               any_failed++;
463               NEXT_VARIABLE ();
464             }
465
466           if ((flags_on & att_array) && assoc_p (var))
467             {
468               builtin_error (_("%s: cannot convert associative to indexed array"), name);
469               any_failed++;
470               NEXT_VARIABLE ();
471             }
472           if ((flags_on & att_assoc) && array_p (var))
473             {
474               builtin_error (_("%s: cannot convert indexed to associative array"), name);
475               any_failed++;
476               NEXT_VARIABLE ();
477             }
478
479           /* declare -A name[[n]] makes name an associative array variable. */
480           if (flags_on & att_assoc)
481             {
482               if (assoc_p (var) == 0)
483                 var = convert_var_to_assoc (var);
484             }
485           /* declare -a name[[n]] or declare name[n] makes name an indexed
486              array variable. */
487           else if ((making_array_special || (flags_on & att_array)) && array_p (var) == 0)
488             var = convert_var_to_array (var);
489 #endif /* ARRAY_VARS */
490
491           VSETATTR (var, flags_on);
492           VUNSETATTR (var, flags_off);
493
494 #if defined (ARRAY_VARS)
495           if (offset && compound_array_assign)
496             assign_array_var_from_string (var, value, aflags);
497           else if (simple_array_assign && subscript_start)
498             {
499               /* declare [-a] name[N]=value */
500               *subscript_start = '[';   /* ] */
501               var = assign_array_element (name, value, 0);      /* XXX - not aflags */
502               *subscript_start = '\0';
503             }
504           else if (simple_array_assign)
505             /* let bind_array_variable take care of this. */
506             bind_array_variable (name, 0, value, aflags);
507           else
508 #endif
509           /* bind_variable_value duplicates the essential internals of
510              bind_variable() */
511           if (offset)
512             bind_variable_value (var, value, aflags);
513
514           /* If we found this variable in the temporary environment, as with
515              `var=value declare -x var', make sure it is treated identically
516              to `var=value export var'.  Do the same for `declare -r' and
517              `readonly'.  Preserve the attributes, except for att_tempvar. */
518           /* XXX -- should this create a variable in the global scope, or
519              modify the local variable flags?  ksh93 has it modify the
520              global scope.
521              Need to handle case like in set_var_attribute where a temporary
522              variable is in the same table as the function local vars. */
523           if ((flags_on & (att_exported|att_readonly)) && tempvar_p (var))
524             {
525               SHELL_VAR *tv;
526               char *tvalue;
527
528               tv = find_tempenv_variable (var->name);
529               if (tv)
530                 {
531                   tvalue = var_isset (var) ? savestring (value_cell (var)) : savestring ("");
532                   tv = bind_variable (var->name, tvalue, 0);
533                   tv->attributes |= var->attributes & ~att_tempvar;
534                   if (tv->context > 0)
535                     VSETATTR (tv, att_propagate);
536                   free (tvalue);
537                 }
538               VSETATTR (var, att_propagate);
539             }
540         }
541
542       stupidly_hack_special_variables (name);
543
544       NEXT_VARIABLE ();
545     }
546
547   return (assign_error ? EX_BADASSIGN
548                        : ((any_failed == 0) ? EXECUTION_SUCCESS
549                                             : EXECUTION_FAILURE));
550 }