From ace5fe0ceecb73c93fb21bcc5016f7da3a013dea Mon Sep 17 00:00:00 2001 From: Robin H. Johnson Date: Sat, 13 Mar 2010 11:21:46 +0000 Subject: [PATCH] Introduce -v option to declare to NOT display the contents of variables. --- This is still a bit broken. WORKS: # declare -p -v FAILS: # declare -v ... Needs work then, and lots more docs patches before upstream will take it. diff -Nuar bash-4.1.orig/builtins/common.h bash-4.1/builtins/common.h --- bash-4.1.orig/builtins/common.h 2010-03-13 11:49:06.708329545 +0000 +++ bash-4.1/builtins/common.h 2010-03-13 11:44:38.468356358 +0000 @@ -149,10 +149,10 @@ extern int describe_command __P((char *, int)); /* Functions from setattr.def */ -extern int set_or_show_attributes __P((WORD_LIST *, int, int)); -extern int show_all_var_attributes __P((int, int)); -extern int show_var_attributes __P((SHELL_VAR *, int, int)); -extern int show_name_attributes __P((char *, int)); +extern int set_or_show_attributes __P((WORD_LIST *, int, int, int)); +extern int show_all_var_attributes __P((int, int, int)); +extern int show_var_attributes __P((SHELL_VAR *, int, int, int)); +extern int show_name_attributes __P((char *, int, int)); extern void set_var_attribute __P((char *, int, int)); /* Functions from pushd.def */ diff -Nuar bash-4.1.orig/builtins/declare.def bash-4.1/builtins/declare.def --- bash-4.1.orig/builtins/declare.def 2010-03-13 11:49:06.708329545 +0000 +++ bash-4.1/builtins/declare.def 2010-03-13 11:50:25.747506220 +0000 @@ -22,7 +22,7 @@ $BUILTIN declare $FUNCTION declare_builtin -$SHORT_DOC declare [-aAfFilrtux] [-p] [name[=value] ...] +$SHORT_DOC declare [-aAfFilrtux] [-pv] [name[=value] ...] Set variable values and attributes. Declare variables and give them attributes. If no NAMEs are given, @@ -33,6 +33,7 @@ -F restrict display to function names only (plus line number and source file when debugging) -p display the attributes and value of each NAME + -v do not display the value of each NAME Options which set attributes: -a to make NAMEs indexed arrays (if supported) @@ -58,7 +59,7 @@ $BUILTIN typeset $FUNCTION declare_builtin -$SHORT_DOC typeset [-aAfFilrtux] [-p] name[=value] ... +$SHORT_DOC typeset [-aAfFilrtux] [-pv] name[=value] ... Set variable values and attributes. Obsolete. See `help declare'. @@ -125,9 +126,9 @@ } #if defined (ARRAY_VARS) -# define DECLARE_OPTS "+acfilprtuxAF" +# define DECLARE_OPTS "+acfilprtuvxAF" #else -# define DECLARE_OPTS "+cfilprtuxF" +# define DECLARE_OPTS "+cfilprtuvxF" #endif /* The workhorse function. */ @@ -137,12 +138,12 @@ int local_var; { int flags_on, flags_off, *flags; - int any_failed, assign_error, pflag, nodefs, opt; + int any_failed, assign_error, pflag, vflag, nodefs, opt; char *t, *subscript_start; SHELL_VAR *var; FUNCTION_DEF *shell_fn; - flags_on = flags_off = any_failed = assign_error = pflag = nodefs = 0; + flags_on = flags_off = any_failed = assign_error = pflag = vflag = nodefs = 0; reset_internal_getopt (); while ((opt = internal_getopt (list, DECLARE_OPTS)) != EOF) { @@ -170,7 +171,10 @@ if (local_var == 0) pflag++; break; - case 'F': + case 'v': + vflag++; + break; + case 'F': nodefs++; *flags |= att_function; break; @@ -239,11 +243,11 @@ } } else if (pflag && (flags_on == 0 || flags_on == att_function)) - show_all_var_attributes (flags_on == 0, nodefs); + show_all_var_attributes (flags_on == 0, vflag, nodefs); else if (flags_on == 0) return (set_builtin ((WORD_LIST *)NULL)); else - set_or_show_attributes ((WORD_LIST *)NULL, flags_on, nodefs); + set_or_show_attributes ((WORD_LIST *)NULL, flags_on, vflag, nodefs); return (sh_chkwrite (EXECUTION_SUCCESS)); } @@ -252,7 +256,7 @@ { for (any_failed = 0; list; list = list->next) { - pflag = show_name_attributes (list->word->word, nodefs); + pflag = show_name_attributes (list->word->word, vflag, nodefs); if (pflag) { sh_notfound (list->word->word); diff -Nuar bash-4.1.orig/builtins/setattr.def bash-4.1/builtins/setattr.def --- bash-4.1.orig/builtins/setattr.def 2010-03-13 11:49:06.708329545 +0000 +++ bash-4.1/builtins/setattr.def 2010-03-13 11:57:27.298279436 +0000 @@ -77,7 +77,7 @@ export_builtin (list) register WORD_LIST *list; { - return (set_or_show_attributes (list, att_exported, 0)); + return (set_or_show_attributes (list, att_exported, 0, 0)); } $BUILTIN readonly @@ -107,7 +107,7 @@ readonly_builtin (list) register WORD_LIST *list; { - return (set_or_show_attributes (list, att_readonly, 0)); + return (set_or_show_attributes (list, att_readonly, 0, 0)); } #if defined (ARRAY_VARS) @@ -120,9 +120,9 @@ ATTRIBUTE. An arg of `-n' says to remove the attribute from the the remaining names in LIST (doesn't work for readonly). */ int -set_or_show_attributes (list, attribute, nodefs) +set_or_show_attributes (list, attribute, vattr, nodefs) register WORD_LIST *list; - int attribute, nodefs; + int attribute, vattr, nodefs; { register SHELL_VAR *var; int assign, undo, any_failed, assign_error, opt; @@ -297,7 +297,7 @@ #endif if ((var->attributes & attribute)) { - show_var_attributes (var, READONLY_OR_EXPORT, nodefs); + show_var_attributes (var, READONLY_OR_EXPORT, vattr, nodefs); if (any_failed = sh_chkwrite (any_failed)) break; } @@ -314,8 +314,8 @@ /* Show all variable variables (v == 1) or functions (v == 0) with attributes. */ int -show_all_var_attributes (v, nodefs) - int v, nodefs; +show_all_var_attributes (v, vattr, nodefs) + int v, vattr, nodefs; { SHELL_VAR **variable_list, *var; int any_failed; @@ -327,7 +327,7 @@ for (i = any_failed = 0; var = variable_list[i]; i++) { - show_var_attributes (var, READONLY_OR_EXPORT, nodefs); + show_var_attributes (var, READONLY_OR_EXPORT, vattr, nodefs); if (any_failed = sh_chkwrite (any_failed)) break; } @@ -340,11 +340,13 @@ non-zero, it indicates we're being called from `export' or `readonly'. In POSIX mode, this prints the name of the calling builtin (`export' or `readonly') instead of `declare', and doesn't print function defs - when called by `export' or `readonly'. */ + when called by `export' or `readonly'. If VATTR is non-zero, it means we + only want the names, without the contents. + */ int -show_var_attributes (var, pattr, nodefs) +show_var_attributes (var, pattr, vattr, nodefs) SHELL_VAR *var; - int pattr, nodefs; + int pattr, vattr, nodefs; { char flags[16], *x; int i; @@ -421,7 +423,9 @@ printf ("%s ", this_command_name); #if defined (ARRAY_VARS) - if (array_p (var)) + if (vattr && (array_p (var) | assoc_p (var))) + printf ("%s\n", var->name); + else if (array_p (var)) print_array_assignment (var, 1); else if (assoc_p (var)) print_assoc_assignment (var, 1); @@ -435,6 +439,8 @@ printf ("%s\n", named_function_string (var->name, function_cell (var), FUNC_MULTILINE|FUNC_EXTERNAL)); else if (invisible_p (var)) printf ("%s\n", var->name); + else if (vattr) + printf ("%s\n", var->name); else { x = sh_double_quote (var_isset (var) ? value_cell (var) : ""); @@ -445,9 +451,9 @@ } int -show_name_attributes (name, nodefs) +show_name_attributes (name, vattr, nodefs) char *name; - int nodefs; + int nodefs, vattr; { SHELL_VAR *var; @@ -455,7 +461,7 @@ if (var && invisible_p (var) == 0) { - show_var_attributes (var, READONLY_OR_EXPORT, nodefs); + show_var_attributes (var, READONLY_OR_EXPORT, vattr, nodefs); return (0); } else