Imported from ../bash-2.05b.tar.gz.
[platform/upstream/bash.git] / variables.h
index f0da3ef..213f6ff 100644 (file)
@@ -1,6 +1,6 @@
 /* variables.h -- data structures for shell variables. */
 
-/* Copyright (C) 1987,1991 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
 
 #include "conftypes.h"
 
+/* A variable context. */
+typedef struct var_context {
+  char *name;          /* empty or NULL means global context */
+  int scope;           /* 0 means global context */
+  int flags;
+  struct var_context *up;      /* previous function calls */
+  struct var_context *down;    /* down towards global context */
+  HASH_TABLE *table;           /* variables at this scope */
+} VAR_CONTEXT;
+
+/* Flags for var_context->flags */
+#define VC_HASLOCAL    0x01
+#define VC_HASTMPVAR   0x02
+#define VC_FUNCENV     0x04    /* also function if name != NULL */
+#define VC_BLTNENV     0x08    /* builtin_env */
+#define VC_TEMPENV     0x10    /* temporary_env */
+
+#define VC_TEMPFLAGS   (VC_FUNCENV|VC_BLTNENV|VC_TEMPENV)
+
+/* Accessing macros */
+#define vc_isfuncenv(vc)       (((vc)->flags & VC_FUNCENV) != 0)
+#define vc_isbltnenv(vc)       (((vc)->flags & VC_BLTNENV) != 0)
+#define vc_istempenv(vc)       (((vc)->flags & (VC_TEMPFLAGS)) == VC_TEMPENV)
+
+#define vc_istempscope(vc)     (((vc)->flags & (VC_TEMPENV|VC_BLTNENV)) != 0)
+
+#define vc_haslocals(vc)       (((vc)->flags & VC_HASLOCAL) != 0)
+#define vc_hastmpvars(vc)      (((vc)->flags & VC_HASTMPVAR) != 0)
+
 /* What a shell variable looks like. */
 
-typedef struct variable *DYNAMIC_FUNC ();
+typedef struct variable *sh_var_value_func_t __P((struct variable *));
+typedef struct variable *sh_var_assign_func_t __P((struct variable *, char *, arrayind_t));
+
+/* For the future */
+union _value {
+  char *s;                     /* string value */
+  intmax_t i;                  /* int value */
+  COMMAND *f;                  /* function */
+  ARRAY *a;                    /* array */
+  HASH_TABLE *h;               /* associative array */
+  double d;                    /* floating point number */
+  void *v;                     /* opaque data for future use */
+};
 
 typedef struct variable {
   char *name;                  /* Symbol that the user types. */
   char *value;                 /* Value that is returned. */
   char *exportstr;             /* String for the environment. */
-  DYNAMIC_FUNC *dynamic_value; /* Function called to return a `dynamic'
+  sh_var_value_func_t *dynamic_value;  /* Function called to return a `dynamic'
                                   value for a variable, like $SECONDS
                                   or $RANDOM. */
-  DYNAMIC_FUNC *assign_func;   /* Function called when this `special
+  sh_var_assign_func_t *assign_func; /* Function called when this `special
                                   variable' is assigned a value in
                                   bind_variable. */
   int attributes;              /* export, readonly, array, invisible... */
   int context;                 /* Which context this variable belongs to. */
-  struct variable *prev_context; /* Value from previous context or NULL. */
 } SHELL_VAR;
 
+typedef struct _vlist {
+  SHELL_VAR **list;
+  int list_size;       /* allocated size */
+  int list_len;                /* current number of entries */
+} VARLIST;
+
 /* The various attributes that a given variable can have. */
-#define att_exported  0x001    /* export to environment */
-#define att_readonly  0x002    /* cannot change */
-#define att_invisible 0x004    /* cannot see */
-#define att_array     0x008    /* value is an array */
-#define att_nounset   0x010    /* cannot unset */
-#define att_function  0x020    /* value is a function */
-#define att_integer   0x040    /* internal representation is int */
-#define att_imported  0x080    /* came from environment */
-#define att_local     0x100    /* variable is local to a function */
-#define att_tempvar   0x200    /* variable came from the temp environment */
-#define att_importstr 0x400    /* exportstr points into initial environment */
-#define att_noassign  0x800    /* assignment not allowed */
+/* First, the user-visible attributes */
+#define att_exported   0x0000001       /* export to environment */
+#define att_readonly   0x0000002       /* cannot change */
+#define att_array      0x0000004       /* value is an array */
+#define att_function   0x0000008       /* value is a function */
+#define att_integer    0x0000010       /* internal representation is int */
+#define att_local      0x0000020       /* variable is local to a function */
+#define att_assoc      0x0000040       /* variable is an associative array */
+#define att_trace      0x0000080       /* function is traced with DEBUG trap */
+
+#define attmask_user   0x0000fff
+
+/* Internal attributes used for bookkeeping */
+#define att_invisible  0x0001000       /* cannot see */
+#define att_nounset    0x0002000       /* cannot unset */
+#define att_noassign   0x0004000       /* assignment not allowed */
+#define att_imported   0x0008000       /* came from environment */
+#define att_special    0x0010000       /* requires special handling */
+
+#define        attmask_int     0x00ff000
+
+/* Internal attributes used for variable scoping. */
+#define att_tempvar    0x0100000       /* variable came from the temp environment */
+#define att_propagate  0x0200000       /* propagate to previous scope */
+
+#define attmask_scope  0x0f00000
 
 #define exported_p(var)                ((((var)->attributes) & (att_exported)))
 #define readonly_p(var)                ((((var)->attributes) & (att_readonly)))
-#define invisible_p(var)       ((((var)->attributes) & (att_invisible)))
 #define array_p(var)           ((((var)->attributes) & (att_array)))
-#define non_unsettable_p(var)  ((((var)->attributes) & (att_nounset)))
 #define function_p(var)                ((((var)->attributes) & (att_function)))
 #define integer_p(var)         ((((var)->attributes) & (att_integer)))
-#define imported_p(var)                ((((var)->attributes) & (att_imported)))
 #define local_p(var)           ((((var)->attributes) & (att_local)))
-#define tempvar_p(var)         ((((var)->attributes) & (att_tempvar)))
+#define assoc_p(var)           ((((var)->attributes) & (att_assoc)))
+#define trace_p(var)           ((((var)->attributes) & (att_trace)))
+
+#define invisible_p(var)       ((((var)->attributes) & (att_invisible)))
+#define non_unsettable_p(var)  ((((var)->attributes) & (att_nounset)))
 #define noassign_p(var)                ((((var)->attributes) & (att_noassign)))
+#define imported_p(var)                ((((var)->attributes) & (att_imported)))
+#define specialvar_p(var)      ((((var)->attributes) & (att_special)))
+
+#define tempvar_p(var)         ((((var)->attributes) & (att_tempvar)))
+
+/* Acessing variable values: rvalues */
+#define value_cell(var)                ((var)->value)
+#define function_cell(var)     (COMMAND *)((var)->value)
+#define array_cell(var)                (ARRAY *)((var)->value)
+
+#define var_isnull(var)                ((var)->value == 0)
+#define var_isset(var)         ((var)->value != 0)
+
+/* Assigning variable values: lvalues */
+#define var_setvalue(var, str) ((var)->value = (str))
+#define var_setfunc(var, func) ((var)->value = (char *)(func))
+#define var_setarray(var, arr) ((var)->value = (char *)(arr))
 
-#define value_cell(var) ((var)->value)
-#define function_cell(var) (COMMAND *)((var)->value)
-#define array_cell(var) ((ARRAY *)(var)->value)
+/* Make VAR be auto-exported. */
+#define set_auto_export(var) \
+  do { (var)->attributes |= att_exported; array_needs_making = 1; } while (0)
 
 #define SETVARATTR(var, attr, undo) \
        ((undo == 0) ? ((var)->attributes |= (attr)) \
@@ -97,58 +175,43 @@ typedef struct variable {
 #define SAVE_EXPORTSTR(var, value) (var)->exportstr = (value) ? savestring (value) : (char *)NULL
 
 #define FREE_EXPORTSTR(var) \
-       do { \
-         if ((var)->exportstr) \
-           { \
-             if (((var)->attributes & att_importstr) == 0) \
-               free ((var)->exportstr); \
-           } \
-       } while (0)
+       do { if ((var)->exportstr) free ((var)->exportstr); } while (0)
 
-#if 0
 #define CACHE_IMPORTSTR(var, value) \
-       do { \
-         (var)->exportstr = value; \
-         (var)->attributes |= att_importstr; \
-       } while (0)
-#else
-#define CACHE_IMPORTSTR(var, value) \
-       do { \
-         (var)->exportstr = savestring (value); \
-       } while (0)
-#endif
+       (var)->exportstr = savestring (value)
 
 #define INVALIDATE_EXPORTSTR(var) \
        do { \
          if ((var)->exportstr) \
            { \
-             if (((var)->attributes & att_importstr) == 0) \
-               free ((var)->exportstr); \
+             free ((var)->exportstr); \
              (var)->exportstr = (char *)NULL; \
-             (var)->attributes &= ~att_importstr; \
            } \
        } while (0)
        
 /* Stuff for hacking variables. */
 typedef int sh_var_map_func_t __P((SHELL_VAR *));
 
+/* Where we keep the variables and functions */
+extern VAR_CONTEXT *global_variables;
+extern VAR_CONTEXT *shell_variables;
+
+extern HASH_TABLE *shell_functions;
+extern HASH_TABLE *temporary_env;
+
 extern int variable_context;
-extern HASH_TABLE *shell_variables, *shell_functions;
 extern char *dollar_vars[];
 extern char **export_env;
-extern char **non_unsettable_vars;
 
 extern void initialize_shell_variables __P((char **, int));
 extern SHELL_VAR *set_if_not __P((char *, char *));
-extern void sh_set_lines_and_columns __P((int, int));
 
+extern void sh_set_lines_and_columns __P((int, int));
 extern void set_pwd __P((void));
-
 extern void set_ppid __P((void));
-
 extern void make_funcname_visible __P((int));
 
-extern SHELL_VAR *var_lookup __P((const char *, HASH_TABLE *));
+extern SHELL_VAR *var_lookup __P((const char *, VAR_CONTEXT *));
 
 extern SHELL_VAR *find_function __P((const char *));
 extern SHELL_VAR *find_variable __P((const char *));
@@ -159,35 +222,53 @@ extern SHELL_VAR *make_local_variable __P((const char *));
 extern SHELL_VAR *bind_variable __P((const char *, char *));
 extern SHELL_VAR *bind_function __P((const char *, COMMAND *));
 
-extern SHELL_VAR **map_over __P((sh_var_map_func_t *, HASH_TABLE *));
+extern SHELL_VAR **map_over __P((sh_var_map_func_t *, VAR_CONTEXT *));
+SHELL_VAR **map_over_funcs __P((sh_var_map_func_t *));
+     
 extern SHELL_VAR **all_shell_variables __P((void));
 extern SHELL_VAR **all_shell_functions __P((void));
 extern SHELL_VAR **all_visible_variables __P((void));
 extern SHELL_VAR **all_visible_functions __P((void));
 extern SHELL_VAR **all_exported_variables __P((void));
+extern SHELL_VAR **local_exported_variables __P((void));
+extern SHELL_VAR **all_local_variables __P((void));
 #if defined (ARRAY_VARS)
 extern SHELL_VAR **all_array_variables __P((void));
 #endif
-
 extern char **all_variables_matching_prefix __P((const char *));
 
 extern char **make_var_array __P((HASH_TABLE *));
 extern char **add_or_supercede_exported_var __P((char *, int));
 
+extern char *get_variable_value __P((SHELL_VAR *));
 extern char *get_string_value __P((const char *));
+extern char *sh_get_env_value __P((const char *));
 extern char *make_variable_value __P((SHELL_VAR *, char *));
 
 extern SHELL_VAR *bind_variable_value __P((SHELL_VAR *, char *));
 extern SHELL_VAR *bind_int_variable __P((char *, char *));
-extern SHELL_VAR *bind_var_to_int __P((char *, long));
+extern SHELL_VAR *bind_var_to_int __P((char *, intmax_t));
 
-extern int assignment __P((const char *));
-extern int variable_in_context __P((SHELL_VAR *));
 extern int assign_in_env __P((const char *));
 extern int unbind_variable __P((const char *));
-extern int makunbound __P((const char *, HASH_TABLE *));
+extern int unbind_func __P((const char *));
+extern int makunbound __P((const char *, VAR_CONTEXT *));
 extern int kill_local_variable __P((const char *));
 extern void delete_all_variables __P((HASH_TABLE *));
+extern void delete_all_contexts __P((VAR_CONTEXT *));
+
+extern VAR_CONTEXT *new_var_context __P((char *, int));
+extern void dispose_var_context __P((VAR_CONTEXT *));
+extern VAR_CONTEXT *push_var_context __P((char *, int, HASH_TABLE *));
+extern void pop_var_context __P((void));
+extern VAR_CONTEXT *push_scope __P((int, HASH_TABLE *));
+extern void pop_scope __P((int));
+
+extern void push_context __P((char *, int, HASH_TABLE *));
+extern void pop_context __P((void));
+extern void push_dollar_vars __P((void));
+extern void pop_dollar_vars __P((void));
+extern void dispose_saved_dollar_vars __P((void));
 
 extern void adjust_shell_level __P((int));
 extern void non_unsettable __P((char *));
@@ -197,30 +278,31 @@ extern void dispose_function_env __P((void));
 extern void dispose_builtin_env __P((void));
 extern void merge_temporary_env __P((void));
 extern void merge_builtin_env __P((void));
-extern void merge_function_env __P((void));
 extern void kill_all_local_variables __P((void));
+
 extern void set_var_read_only __P((char *));
 extern void set_func_read_only __P((const char *));
 extern void set_var_auto_export __P((char *));
 extern void set_func_auto_export __P((const char *));
+
 extern void sort_variables __P((SHELL_VAR **));
+
 extern void maybe_make_export_env __P((void));
 extern void update_export_env_inplace __P((char *, int, char *));
 extern void put_command_name_into_env __P((char *));
-extern void put_gnu_argv_flags_into_env __P((long, char *));
+extern void put_gnu_argv_flags_into_env __P((intmax_t, char *));
+
 extern void print_var_list __P((SHELL_VAR **));
 extern void print_func_list __P((SHELL_VAR **));
 extern void print_assignment __P((SHELL_VAR *));
 extern void print_var_value __P((SHELL_VAR *, int));
 extern void print_var_function __P((SHELL_VAR *));
 
-extern char *indirection_level_string __P((void));
-
 #if defined (ARRAY_VARS)
 extern SHELL_VAR *make_new_array_variable __P((char *));
 extern SHELL_VAR *make_local_array_variable __P((char *));
 
-extern void set_pipestatus_array __P((int *));
+extern void set_pipestatus_array __P((int *, int));
 #endif
 
 extern void set_pipestatus_from_exit __P((int));
@@ -233,6 +315,7 @@ extern int get_random_number __P((void));
 
 /* The `special variable' functions that get called when a particular
    variable is set. */
+extern void sv_ifs __P((char *));
 extern void sv_path __P((char *));
 extern void sv_mail __P((char *));
 extern void sv_globignore __P((char *));