i-bash/builtins/declare.def

574 lines
15 KiB
Modula-2
Raw Normal View History

1996-08-26 18:22:31 +00:00
This file is declare.def, from which is created declare.c.
It implements the builtins "declare" and "local" in Bash.
Copyright (C) 1987-2010 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
2009-01-12 13:36:28 +00:00
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
1996-08-26 18:22:31 +00:00
$PRODUCES declare.c
$BUILTIN declare
$FUNCTION declare_builtin
$SHORT_DOC declare [-aAfFgilrtux] [-p] [name[=value] ...]
2009-01-12 13:36:28 +00:00
Set variable values and attributes.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
Declare variables and give them attributes. If no NAMEs are given,
display the attributes and values of all variables.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
Options:
-f restrict action or display to function names and definitions
-F restrict display to function names only (plus line number and
source file when debugging)
-g create global variables when used in a shell function; otherwise
ignored
2009-01-12 13:36:28 +00:00
-p display the attributes and value of each NAME
Options which set attributes:
-a to make NAMEs indexed arrays (if supported)
-A to make NAMEs associative arrays (if supported)
2002-07-17 14:10:11 +00:00
-i to make NAMEs have the `integer' attribute
2009-01-12 13:36:28 +00:00
-l to convert NAMEs to lower case on assignment
1996-12-23 17:02:34 +00:00
-r to make NAMEs readonly
2002-07-17 14:10:11 +00:00
-t to make NAMEs have the `trace' attribute
2009-01-12 13:36:28 +00:00
-u to convert NAMEs to upper case on assignment
1996-12-23 17:02:34 +00:00
-x to make NAMEs export
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
Using `+' instead of `-' turns off the given attribute.
1996-08-26 18:22:31 +00:00
Variables with the integer attribute have arithmetic evaluation (see
2009-01-12 13:36:28 +00:00
the `let' command) performed when the variable is assigned a value.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
When used in a function, `declare' makes NAMEs local, as with the `local'
command. The `-g' option suppresses this behavior.
1996-12-23 17:02:34 +00:00
2009-01-12 13:36:28 +00:00
Exit Status:
Returns success unless an invalid option is supplied or an error occurs.
1996-08-26 18:22:31 +00:00
$END
$BUILTIN typeset
$FUNCTION declare_builtin
$SHORT_DOC typeset [-aAfFgilrtux] [-p] name[=value] ...
2009-01-12 13:36:28 +00:00
Set variable values and attributes.
Obsolete. See `help declare'.
1996-08-26 18:22:31 +00:00
$END
1996-12-23 17:02:34 +00:00
#include <config.h>
#if defined (HAVE_UNISTD_H)
1998-04-17 19:52:44 +00:00
# ifdef _MINIX
# include <sys/types.h>
# endif
1996-12-23 17:02:34 +00:00
# include <unistd.h>
#endif
1996-08-26 18:22:31 +00:00
#include <stdio.h>
1996-12-23 17:02:34 +00:00
#include "../bashansi.h"
2004-07-27 13:29:18 +00:00
#include "../bashintl.h"
1996-08-26 18:22:31 +00:00
#include "../shell.h"
1996-12-23 17:02:34 +00:00
#include "common.h"
#include "builtext.h"
2002-07-17 14:10:11 +00:00
#include "bashgetopt.h"
1996-08-26 18:22:31 +00:00
2001-11-13 17:56:06 +00:00
extern int array_needs_making;
2006-10-10 14:15:34 +00:00
extern int posixly_correct;
1996-08-26 18:22:31 +00:00
2001-11-13 17:56:06 +00:00
static int declare_internal __P((register WORD_LIST *, int));
1996-08-26 18:22:31 +00:00
/* Declare or change variable attributes. */
int
declare_builtin (list)
register WORD_LIST *list;
{
return (declare_internal (list, 0));
}
$BUILTIN local
$FUNCTION local_builtin
2009-01-12 13:36:28 +00:00
$SHORT_DOC local [option] name[=value] ...
Define local variables.
Create a local variable called NAME, and give it VALUE. OPTION can
be any option accepted by `declare'.
Local variables can only be used within a function; they are visible
only to the function where they are defined and its children.
Exit Status:
Returns success unless an invalid option is supplied, an error occurs,
or the shell is not executing a function.
1996-08-26 18:22:31 +00:00
$END
int
local_builtin (list)
register WORD_LIST *list;
{
if (variable_context)
return (declare_internal (list, 1));
else
{
2004-07-27 13:29:18 +00:00
builtin_error (_("can only be used in a function"));
1996-08-26 18:22:31 +00:00
return (EXECUTION_FAILURE);
}
}
2002-07-17 14:10:11 +00:00
#if defined (ARRAY_VARS)
# define DECLARE_OPTS "+acfgilprtuxAF"
2002-07-17 14:10:11 +00:00
#else
# define DECLARE_OPTS "+cfgilprtuxF"
2002-07-17 14:10:11 +00:00
#endif
1996-08-26 18:22:31 +00:00
/* The workhorse function. */
static int
declare_internal (list, local_var)
register WORD_LIST *list;
int local_var;
{
2009-01-12 13:36:28 +00:00
int flags_on, flags_off, *flags;
int any_failed, assign_error, pflag, nodefs, opt, mkglobal;
2001-11-13 17:56:06 +00:00
char *t, *subscript_start;
1996-12-23 17:02:34 +00:00
SHELL_VAR *var;
2004-07-27 13:29:18 +00:00
FUNCTION_DEF *shell_fn;
1996-08-26 18:22:31 +00:00
flags_on = flags_off = any_failed = assign_error = pflag = nodefs = mkglobal = 0;
2002-07-17 14:10:11 +00:00
reset_internal_getopt ();
while ((opt = internal_getopt (list, DECLARE_OPTS)) != EOF)
1996-08-26 18:22:31 +00:00
{
2002-07-17 14:10:11 +00:00
flags = list_opttype == '+' ? &flags_off : &flags_on;
1996-08-26 18:22:31 +00:00
2002-07-17 14:10:11 +00:00
switch (opt)
1996-08-26 18:22:31 +00:00
{
2002-07-17 14:10:11 +00:00
case 'a':
1996-12-23 17:02:34 +00:00
#if defined (ARRAY_VARS)
2002-07-17 14:10:11 +00:00
*flags |= att_array;
2009-01-12 13:36:28 +00:00
break;
#else
builtin_usage ();
return (EX_USAGE);
1996-12-23 17:02:34 +00:00
#endif
2009-01-12 13:36:28 +00:00
case 'A':
#if defined (ARRAY_VARS)
*flags |= att_assoc;
2002-07-17 14:10:11 +00:00
break;
2009-01-12 13:36:28 +00:00
#else
builtin_usage ();
return (EX_USAGE);
#endif
2002-07-17 14:10:11 +00:00
case 'p':
if (local_var == 0)
pflag++;
break;
case 'F':
nodefs++;
*flags |= att_function;
break;
case 'f':
*flags |= att_function;
break;
case 'g':
if (flags == &flags_on)
mkglobal = 1;
break;
2002-07-17 14:10:11 +00:00
case 'i':
*flags |= att_integer;
break;
case 'r':
*flags |= att_readonly;
break;
case 't':
*flags |= att_trace;
break;
case 'x':
*flags |= att_exported;
array_needs_making = 1;
break;
2009-01-12 13:36:28 +00:00
#if defined (CASEMOD_ATTRS)
# if defined (CASEMOD_CAPCASE)
case 'c':
*flags |= att_capcase;
if (flags == &flags_on)
flags_off |= att_uppercase|att_lowercase;
break;
# endif
case 'l':
*flags |= att_lowercase;
if (flags == &flags_on)
flags_off |= att_capcase|att_uppercase;
break;
case 'u':
*flags |= att_uppercase;
if (flags == &flags_on)
flags_off |= att_capcase|att_lowercase;
break;
#endif /* CASEMOD_ATTRS */
2002-07-17 14:10:11 +00:00
default:
builtin_usage ();
return (EX_USAGE);
1996-08-26 18:22:31 +00:00
}
}
2002-07-17 14:10:11 +00:00
list = loptend;
1996-08-26 18:22:31 +00:00
/* If there are no more arguments left, then we just want to show
some variables. */
2009-01-12 13:36:28 +00:00
if (list == 0) /* declare -[aAfFirtx] */
1996-08-26 18:22:31 +00:00
{
/* Show local variables defined at this context level if this is
the `local' builtin. */
if (local_var)
{
register SHELL_VAR **vlist;
register int i;
2002-07-17 14:10:11 +00:00
vlist = all_local_variables ();
1996-08-26 18:22:31 +00:00
if (vlist)
{
for (i = 0; vlist[i]; i++)
print_assignment (vlist[i]);
free (vlist);
}
}
2009-01-12 13:36:28 +00:00
else if (pflag && (flags_on == 0 || flags_on == att_function))
show_all_var_attributes (flags_on == 0, nodefs);
else if (flags_on == 0)
return (set_builtin ((WORD_LIST *)NULL));
1996-08-26 18:22:31 +00:00
else
2009-01-12 13:36:28 +00:00
set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs);
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
return (sh_chkwrite (EXECUTION_SUCCESS));
1996-08-26 18:22:31 +00:00
}
2009-01-12 13:36:28 +00:00
if (pflag) /* declare -p [-aAfFirtx] name [name...] */
1996-12-23 17:02:34 +00:00
{
for (any_failed = 0; list; list = list->next)
{
pflag = show_name_attributes (list->word->word, nodefs);
if (pflag)
{
2002-07-17 14:10:11 +00:00
sh_notfound (list->word->word);
1996-12-23 17:02:34 +00:00
any_failed++;
}
}
2009-01-12 13:36:28 +00:00
return (sh_chkwrite (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS));
1996-12-23 17:02:34 +00:00
}
1996-08-26 18:22:31 +00:00
#define NEXT_VARIABLE() free (name); list = list->next; continue
/* There are arguments left, so we are making variables. */
2009-01-12 13:36:28 +00:00
while (list) /* declare [-aAfFirx] name [name ...] */
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
char *value, *name;
2005-12-07 14:08:12 +00:00
int offset, aflags;
1996-12-23 17:02:34 +00:00
#if defined (ARRAY_VARS)
2001-11-13 17:56:06 +00:00
int making_array_special, compound_array_assign, simple_array_assign;
1996-12-23 17:02:34 +00:00
#endif
name = savestring (list->word->word);
2004-07-27 13:29:18 +00:00
offset = assignment (name, 0);
2005-12-07 14:08:12 +00:00
aflags = 0;
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
if (offset) /* declare [-aAfFirx] name=value */
1996-08-26 18:22:31 +00:00
{
name[offset] = '\0';
value = name + offset + 1;
2005-12-07 14:08:12 +00:00
if (name[offset - 1] == '+')
{
aflags |= ASS_APPEND;
name[offset - 1] = '\0';
}
1996-08-26 18:22:31 +00:00
}
else
value = "";
1996-12-23 17:02:34 +00:00
#if defined (ARRAY_VARS)
2001-11-13 17:56:06 +00:00
compound_array_assign = simple_array_assign = 0;
subscript_start = (char *)NULL;
if (t = strchr (name, '[')) /* ] */
1996-12-23 17:02:34 +00:00
{
2011-11-21 20:49:12 -05:00
/* If offset != 0 we have already validated any array reference */
if (offset == 0 && valid_array_reference (name) == 0)
{
sh_invalidid (name);
assign_error++;
NEXT_VARIABLE ();
}
2001-11-13 17:56:06 +00:00
subscript_start = t;
1996-12-23 17:02:34 +00:00
*t = '\0';
making_array_special = 1;
}
else
making_array_special = 0;
#endif
2006-10-10 14:15:34 +00:00
/* If we're in posix mode or not looking for a shell function (since
shell function names don't have to be valid identifiers when the
shell's not in posix mode), check whether or not the argument is a
valid, well-formed shell identifier. */
if ((posixly_correct || (flags_on & att_function) == 0) && legal_identifier (name) == 0)
1996-08-26 18:22:31 +00:00
{
2002-07-17 14:10:11 +00:00
sh_invalidid (name);
1996-12-23 17:02:34 +00:00
assign_error++;
1996-08-26 18:22:31 +00:00
NEXT_VARIABLE ();
}
/* If VARIABLE_CONTEXT has a non-zero value, then we are executing
inside of a function. This means we should make local variables,
not global ones. */
2002-07-17 14:10:11 +00:00
/* XXX - this has consequences when we're making a local copy of a
variable that was in the temporary environment. Watch out
for this. */
if (variable_context && mkglobal == 0 && ((flags_on & att_function) == 0))
1996-12-23 17:02:34 +00:00
{
#if defined (ARRAY_VARS)
2009-01-12 13:36:28 +00:00
if (flags_on & att_assoc)
var = make_local_assoc_variable (name);
else if ((flags_on & att_array) || making_array_special)
2000-03-17 21:46:59 +00:00
var = make_local_array_variable (name);
1996-12-23 17:02:34 +00:00
else
#endif
2000-03-17 21:46:59 +00:00
var = make_local_variable (name);
if (var == 0)
{
any_failed++;
NEXT_VARIABLE ();
}
1996-12-23 17:02:34 +00:00
}
2002-07-17 14:10:11 +00:00
else
var = (SHELL_VAR *)NULL;
1996-08-26 18:22:31 +00:00
/* If we are declaring a function, then complain about it in some way.
We don't let people make functions by saying `typeset -f foo=bar'. */
/* There should be a way, however, to let people look at a particular
function definition by saying `typeset -f foo'. */
if (flags_on & att_function)
{
1996-12-23 17:02:34 +00:00
if (offset) /* declare -f [-rix] foo=bar */
1996-08-26 18:22:31 +00:00
{
2004-07-27 13:29:18 +00:00
builtin_error (_("cannot use `-f' to make functions"));
1997-06-05 14:59:13 +00:00
free (name);
1996-08-26 18:22:31 +00:00
return (EXECUTION_FAILURE);
}
1996-12-23 17:02:34 +00:00
else /* declare -f [-rx] name [name...] */
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
var = find_function (name);
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
if (var)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
if (readonly_p (var) && (flags_off & att_readonly))
1996-08-26 18:22:31 +00:00
{
2004-07-27 13:29:18 +00:00
builtin_error (_("%s: readonly function"), name);
1996-08-26 18:22:31 +00:00
any_failed++;
NEXT_VARIABLE ();
}
1996-12-23 17:02:34 +00:00
/* declare -[Ff] name [name...] */
1996-08-26 18:22:31 +00:00
if (flags_on == att_function && flags_off == 0)
{
2004-07-27 13:29:18 +00:00
#if defined (DEBUGGER)
if (nodefs && debugging_mode)
{
shell_fn = find_function_def (var->name);
if (shell_fn)
printf ("%s %d %s\n", var->name, shell_fn->line, shell_fn->source_file);
else
printf ("%s\n", var->name);
}
else
#endif /* DEBUGGER */
{
t = nodefs ? var->name
2009-01-12 13:36:28 +00:00
: named_function_string (name, function_cell (var), FUNC_MULTILINE|FUNC_EXTERNAL);
2004-07-27 13:29:18 +00:00
printf ("%s\n", t);
2009-01-12 13:36:28 +00:00
any_failed = sh_chkwrite (any_failed);
2004-07-27 13:29:18 +00:00
}
1996-08-26 18:22:31 +00:00
}
1996-12-23 17:02:34 +00:00
else /* declare -[fF] -[rx] name [name...] */
1996-08-26 18:22:31 +00:00
{
2000-03-17 21:46:59 +00:00
VSETATTR (var, flags_on);
VUNSETATTR (var, flags_off);
1996-08-26 18:22:31 +00:00
}
}
else
any_failed++;
NEXT_VARIABLE ();
}
}
2009-01-12 13:36:28 +00:00
else /* declare -[aAirx] name [name...] */
1996-08-26 18:22:31 +00:00
{
2002-07-17 14:10:11 +00:00
/* Non-null if we just created or fetched a local variable. */
if (var == 0)
var = mkglobal ? find_global_variable (name) : find_variable (name);
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
if (var == 0)
{
#if defined (ARRAY_VARS)
2009-01-12 13:36:28 +00:00
if (flags_on & att_assoc)
var = make_new_assoc_variable (name);
else if ((flags_on & att_array) || making_array_special)
1996-12-23 17:02:34 +00:00
var = make_new_array_variable (name);
else
#endif
2009-01-12 13:36:28 +00:00
if (offset)
var = bind_variable (name, "", 0);
else
{
var = bind_variable (name, (char *)NULL, 0);
VSETATTR (var, att_invisible);
}
1996-12-23 17:02:34 +00:00
}
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
/* Cannot use declare +r to turn off readonly attribute. */
1996-08-26 18:22:31 +00:00
if (readonly_p (var) && (flags_off & att_readonly))
{
2002-07-17 14:10:11 +00:00
sh_readonly (name);
1996-08-26 18:22:31 +00:00
any_failed++;
NEXT_VARIABLE ();
}
2001-04-06 19:14:31 +00:00
/* Cannot use declare to assign value to readonly or noassign
variable. */
if ((readonly_p (var) || noassign_p (var)) && offset)
1996-12-23 17:02:34 +00:00
{
2001-04-06 19:14:31 +00:00
if (readonly_p (var))
2002-07-17 14:10:11 +00:00
sh_readonly (name);
1996-12-23 17:02:34 +00:00
assign_error++;
NEXT_VARIABLE ();
}
#if defined (ARRAY_VARS)
2009-01-12 13:36:28 +00:00
if ((making_array_special || (flags_on & (att_array|att_assoc)) || array_p (var) || assoc_p (var)) && offset)
1996-12-23 17:02:34 +00:00
{
2005-12-07 14:08:12 +00:00
int vlen;
vlen = STRLEN (value);
2009-01-12 13:36:28 +00:00
2005-12-07 14:08:12 +00:00
if (value[0] == '(' && value[vlen-1] == ')')
2001-11-13 17:56:06 +00:00
compound_array_assign = 1;
1996-12-23 17:02:34 +00:00
else
2001-11-13 17:56:06 +00:00
simple_array_assign = 1;
1996-12-23 17:02:34 +00:00
}
2009-01-12 13:36:28 +00:00
/* Cannot use declare +a name or declare +A name to remove an
array variable. */
if (((flags_off & att_array) && array_p (var)) || ((flags_off & att_assoc) && assoc_p (var)))
1996-12-23 17:02:34 +00:00
{
2004-07-27 13:29:18 +00:00
builtin_error (_("%s: cannot destroy array variables in this way"), name);
1996-12-23 17:02:34 +00:00
any_failed++;
NEXT_VARIABLE ();
}
2009-01-12 13:36:28 +00:00
if ((flags_on & att_array) && assoc_p (var))
{
builtin_error (_("%s: cannot convert associative to indexed array"), name);
any_failed++;
NEXT_VARIABLE ();
}
if ((flags_on & att_assoc) && array_p (var))
{
builtin_error (_("%s: cannot convert indexed to associative array"), name);
any_failed++;
NEXT_VARIABLE ();
}
/* declare -A name[[n]] makes name an associative array variable. */
if (flags_on & att_assoc)
{
if (assoc_p (var) == 0)
var = convert_var_to_assoc (var);
}
/* declare -a name[[n]] or declare name[n] makes name an indexed
array variable. */
2011-11-21 20:49:12 -05:00
else if ((making_array_special || (flags_on & att_array)) && array_p (var) == 0 && assoc_p (var) == 0)
1996-12-23 17:02:34 +00:00
var = convert_var_to_array (var);
#endif /* ARRAY_VARS */
2000-03-17 21:46:59 +00:00
VSETATTR (var, flags_on);
VUNSETATTR (var, flags_off);
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
#if defined (ARRAY_VARS)
2001-11-13 17:56:06 +00:00
if (offset && compound_array_assign)
2005-12-07 14:08:12 +00:00
assign_array_var_from_string (var, value, aflags);
2001-11-13 17:56:06 +00:00
else if (simple_array_assign && subscript_start)
{
2011-11-21 20:51:19 -05:00
/* declare [-aA] name[N]=value */
2001-11-13 17:56:06 +00:00
*subscript_start = '['; /* ] */
2005-12-07 14:08:12 +00:00
var = assign_array_element (name, value, 0); /* XXX - not aflags */
2001-11-13 17:56:06 +00:00
*subscript_start = '\0';
2011-11-22 20:03:23 -05:00
if (var == 0) /* some kind of assignment error */
{
assign_error++;
NEXT_VARIABLE ();
}
2001-11-13 17:56:06 +00:00
}
else if (simple_array_assign)
2011-11-21 20:51:19 -05:00
{
/* let bind_{array,assoc}_variable take care of this. */
if (assoc_p (var))
2011-11-21 20:57:16 -05:00
bind_assoc_variable (var, name, savestring ("0"), value, aflags);
2011-11-21 20:51:19 -05:00
else
bind_array_variable (name, 0, value, aflags);
}
1996-12-23 17:02:34 +00:00
else
#endif
2000-03-17 21:46:59 +00:00
/* bind_variable_value duplicates the essential internals of
bind_variable() */
1996-08-26 18:22:31 +00:00
if (offset)
2005-12-07 14:08:12 +00:00
bind_variable_value (var, value, aflags);
1998-04-17 19:52:44 +00:00
/* If we found this variable in the temporary environment, as with
`var=value declare -x var', make sure it is treated identically
to `var=value export var'. Do the same for `declare -r' and
`readonly'. Preserve the attributes, except for att_tempvar. */
2002-07-17 14:10:11 +00:00
/* XXX -- should this create a variable in the global scope, or
modify the local variable flags? ksh93 has it modify the
global scope.
Need to handle case like in set_var_attribute where a temporary
variable is in the same table as the function local vars. */
1998-04-17 19:52:44 +00:00
if ((flags_on & (att_exported|att_readonly)) && tempvar_p (var))
{
SHELL_VAR *tv;
2002-07-17 14:10:11 +00:00
char *tvalue;
tv = find_tempenv_variable (var->name);
if (tv)
{
tvalue = var_isset (var) ? savestring (value_cell (var)) : savestring ("");
2005-12-07 14:08:12 +00:00
tv = bind_variable (var->name, tvalue, 0);
2002-07-17 14:10:11 +00:00
tv->attributes |= var->attributes & ~att_tempvar;
if (tv->context > 0)
VSETATTR (tv, att_propagate);
free (tvalue);
}
VSETATTR (var, att_propagate);
1998-04-17 19:52:44 +00:00
}
1996-08-26 18:22:31 +00:00
}
stupidly_hack_special_variables (name);
NEXT_VARIABLE ();
}
1996-12-23 17:02:34 +00:00
return (assign_error ? EX_BADASSIGN
: ((any_failed == 0) ? EXECUTION_SUCCESS
: EXECUTION_FAILURE));
1996-08-26 18:22:31 +00:00
}