Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: listing options with set -o/+o



The most recent POSIX shell definition, states that `set -o' should
"write the current settings of the options to standard output in an
unspecified format" and `set +o' should "write the current option
settings to standard output in a format that is suitable for reinput to
the shell as commands that achieve the same options settings"

There are a few possibilities with respect to the exact format the
output should take. I've done what I think is best but it is very easy
to change so feel free to make other suggestions.

For set -o, I've duplicated what you get from setopt with the
kshoptionprint option.

For set +o, it only prints those options that are in their non-default
state (which is what ksh93 and pdksh do but not bash).

Oliver

Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.60
diff -u -r1.60 builtins.yo
--- Doc/Zsh/builtins.yo	3 Apr 2003 09:55:48 -0000	1.60
+++ Doc/Zsh/builtins.yo	14 May 2003 10:11:09 -0000
@@ -973,7 +973,7 @@
 cindex(parameters, setting array)
 cindex(array parameters, setting)
 pindex(KSH_ARRAYS, use of)
-item(tt(set) [ {tt(PLUS())|tt(-)}var(options) | {tt(PLUS())|tt(-)}tt(o) var(option_name) ] ... [ {tt(PLUS())|tt(-)}tt(A) [ var(name) ] ] [ var(arg) ... ])(
+item(tt(set) [ {tt(PLUS())|tt(-)}var(options) | {tt(PLUS())|tt(-)}tt(o) [ var(option_name) ] ] ... [ {tt(PLUS())|tt(-)}tt(A) [ var(name) ] ] [ var(arg) ... ])(
 Set the options for the shell and/or set the positional parameters, or
 declare and set an array.  If the tt(-s) option is given, it causes the
 specified arguments to be sorted before assigning them to the positional
@@ -981,7 +981,10 @@
 sort arguments in descending order.  For the meaning of the other flags, see
 ifzman(zmanref(zshoptions))\
 ifnzman(noderef(Options))\
-.  Flags may be specified by name using the tt(-o) option.
+.  Flags may be specified by name using the tt(-o) option. If no option
+name is supplied with tt(-o), the current option states are printed.
+With tt(PLUS()o) they are printed in a form that can be used as input
+to the shell.
 
 If the tt(-A) flag is specified, var(name) is set to an array containing
 the given var(arg)s; if no var(name) is specified, all arrays are printed
Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.100
diff -u -r1.100 builtin.c
--- Src/builtin.c	4 Apr 2003 16:47:02 -0000	1.100
+++ Src/builtin.c	14 May 2003 10:11:10 -0000
@@ -562,9 +562,9 @@
 		if (!*++*args)
 		    args++;
 		if (!*args) {
-		    zwarnnam(nam, "string expected after -o", NULL, 0);
+		    printoptionstates(hadplus);
 		    inittyptab();
-		    return 1;
+		    return 0;
 		}
 		if(!(optno = optlookup(*args)))
 		    zwarnnam(nam, "no such option: %s", *args, 0);
Index: Src/options.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/options.c,v
retrieving revision 1.14
diff -u -r1.14 options.c
--- Src/options.c	4 Dec 2002 13:57:51 -0000	1.14
+++ Src/options.c	14 May 2003 10:11:10 -0000
@@ -705,6 +705,33 @@
     return buf;
 }
 
+/* print options for set -o/+o */
+
+/**/
+void
+printoptionstates(int hadplus)
+{
+    scanhashtable(optiontab, 1, 0, OPT_ALIAS, printoptionnodestate, hadplus);
+}
+
+/**/
+static void
+printoptionnodestate(HashNode hn, int hadplus)
+{
+    Optname on = (Optname) hn;
+    int optno = on->optno;
+
+    if (hadplus) {
+        if (defset(on) != isset(optno))
+	    printf("set -o %s%s\n", defset(on) ? "no" : "", on->nam);
+    } else {
+	if (defset(on))
+	    printf("no%-19s %s\n", on->nam, isset(optno) ? "off" : "on");
+	else
+	    printf("%-21s %s\n", on->nam, isset(optno) ? "on" : "off");
+    }
+}
+
 /* Print option list for --help */
 
 /**/



Messages sorted by: Reverse Date, Date, Thread, Author