i-bash/builtins/declare.def

476 lines
12 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.
2005-12-07 14:08:12 +00:00
Copyright (C) 1987-2004 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
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
2000-03-17 21:46:59 +00:00
Software Foundation; either version 2, or (at your option) any later
1996-08-26 18:22:31 +00:00
version.
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.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
2000-03-17 21:46:59 +00:00
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
1996-08-26 18:22:31 +00:00
$PRODUCES declare.c
$BUILTIN declare
$FUNCTION declare_builtin
2004-07-27 13:29:18 +00:00
$SHORT_DOC declare [-afFirtx] [-p] [name[=value] ...]
1996-08-26 18:22:31 +00:00
Declare variables and/or give them attributes. If no NAMEs are
1996-12-23 17:02:34 +00:00
given, then display the values of variables instead. The -p option
will display the attributes and values of each NAME.
1996-08-26 18:22:31 +00:00
The flags are:
1996-12-23 17:02:34 +00:00
-a to make NAMEs arrays (if supported)
-f to select from among function names only
2004-07-27 13:29:18 +00:00
-F to display function names (and line number and source file name if
debugging) without definitions
2002-07-17 14:10:11 +00:00
-i to make NAMEs have the `integer' attribute
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
1996-12-23 17:02:34 +00:00
-x to make NAMEs export
1996-08-26 18:22:31 +00:00
Variables with the integer attribute have arithmetic evaluation (see
`let') done when the variable is assigned to.
1996-12-23 17:02:34 +00:00
When displaying values of variables, -f displays a function's name
and definition. The -F option restricts the display to function
name only.
1996-08-26 18:22:31 +00:00
Using `+' instead of `-' turns off the given attribute instead. When
used in a function, makes NAMEs local, as with the `local' command.
$END
$BUILTIN typeset
$FUNCTION declare_builtin
2002-07-17 14:10:11 +00:00
$SHORT_DOC typeset [-afFirtx] [-p] name[=value] ...
1996-08-26 18:22:31 +00:00
Obsolete. See `declare'.
$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
$SHORT_DOC local name[=value] ...
Create a local variable called NAME, and give it VALUE. LOCAL
can only be used within a function; it makes the variable NAME
have a visible scope restricted to that function and its children.
$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 "+afiprtxF"
#else
# define DECLARE_OPTS "+fiprtxF"
#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;
{
2002-07-17 14:10:11 +00:00
int flags_on, flags_off, *flags, any_failed, assign_error, pflag, nodefs, opt;
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
1996-12-23 17:02:34 +00:00
flags_on = flags_off = any_failed = assign_error = pflag = nodefs = 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;
1996-12-23 17:02:34 +00:00
#endif
2002-07-17 14:10:11 +00:00
break;
case 'p':
if (local_var == 0)
pflag++;
break;
case 'F':
nodefs++;
*flags |= att_function;
break;
case 'f':
*flags |= att_function;
break;
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;
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. */
2002-07-17 14:10:11 +00:00
if (list == 0) /* declare -[afFirtx] */
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);
}
}
else
{
1996-12-23 17:02:34 +00:00
if (flags_on == 0)
1996-08-26 18:22:31 +00:00
set_builtin ((WORD_LIST *)NULL);
else
1996-12-23 17:02:34 +00:00
set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs);
1996-08-26 18:22:31 +00:00
}
fflush (stdout);
return (EXECUTION_SUCCESS);
}
2002-07-17 14:10:11 +00:00
if (pflag) /* declare -p [-afFirtx] 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++;
}
}
return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
}
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. */
1996-12-23 17:02:34 +00:00
while (list) /* declare [-afFirx] 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
1996-12-23 17:02:34 +00:00
if (offset) /* declare [-afFirx] 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
{
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 && ((flags_on & att_function) == 0))
1996-12-23 17:02:34 +00:00
{
#if defined (ARRAY_VARS)
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
: named_function_string (name, function_cell (var), 1);
printf ("%s\n", t);
}
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 ();
}
}
1996-12-23 17:02:34 +00:00
else /* declare -[airx] 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 = 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)
if ((flags_on & att_array) || making_array_special)
var = make_new_array_variable (name);
else
#endif
2005-12-07 14:08:12 +00:00
var = bind_variable (name, "", 0);
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)
if ((making_array_special || (flags_on & att_array) || array_p (var)) && offset)
{
2005-12-07 14:08:12 +00:00
int vlen;
vlen = STRLEN (value);
#if 0
1996-12-23 17:02:34 +00:00
if (value[0] == '(' && strchr (value, ')'))
2005-12-07 14:08:12 +00:00
#else
if (value[0] == '(' && value[vlen-1] == ')')
#endif
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
}
/* Cannot use declare +a name to remove an array variable. */
if ((flags_off & att_array) && array_p (var))
{
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 ();
}
/* declare -a name makes name an array variable. */
if ((making_array_special || (flags_on & att_array)) && array_p (var) == 0)
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)
{
/* declare [-a] name[N]=value */
*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';
}
else if (simple_array_assign)
/* let bind_array_variable take care of this. */
2005-12-07 14:08:12 +00:00
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
}