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

PATCH: readonly -p with POSIX_BUILTINS



This fixes the POSIX_BUILTINS aspect of "readonly -p" output, i.e. it
needs to show unset variables (the standard is explicit about this).

It doesn't fix the non-POSIX_BUILTINS aspect of "readonly -p" output,
i.e. "er, this output is all a bit broken, isn't it?"  The object
of "readonly -p" is to have a set of commands to restore the current
state: that means it should work without complaint on a new shell (I
don't think it can possibly be required to work once you've already run
it as POSIX doesn't allow you to remove the readonly attribute).  I
think that means not printing values for readonly specials.  Feel free
to argue it shouldn't show readonly specials at all --- if you ran a
script so generated immediately on starting a shell and it included a
readonly special that was loaded from a module, then I think you'd be
screwed.  I'm vaguely inclining that way myself; you don't use the -p
option if you're just listing things for information.

diff --git a/Src/params.c b/Src/params.c
index d53b6ca..9eab51a 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -5039,8 +5039,19 @@ printparamnode(HashNode hn, int printflags)
     Param p = (Param) hn;
     char *t, **u;
 
-    if (p->node.flags & PM_UNSET)
-	return;
+    if (p->node.flags & PM_UNSET) {
+	if (isset(POSIXBUILTINS) && (p->node.flags & PM_READONLY) &&
+	    (printflags & PRINT_TYPESET))
+	{
+	    /*
+	     * Special POSIX rules: show the parameter as readonly
+	     * even though it's unset, but with no value.
+	     */
+	    printflags |= PRINT_NAMEONLY;
+	}
+	else
+	    return;
+    }
 
     if (printflags & PRINT_TYPESET)
 	printf("typeset ");
diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index f4fb8ec..2edbb0b 100644
--- a/Test/B02typeset.ztst
+++ b/Test/B02typeset.ztst
@@ -475,13 +475,15 @@
    print ${+pbro} >&2
    (typeset pbro=3)
    (pbro=4)
+   readonly -p | grep pbro >&2  # shows up as "readonly" although unset
    typeset -r pbro        # idempotent (no error)...
    print ${+pbro} >&2     # ...so still readonly...
    typeset +r pbro        # ...can't turn it off
  )
-1:Readonly with POSIX_BUILTINS
+1:readonly with POSIX_BUILTINS
 ?0
 ?(eval):5: read-only variable: pbro
 ?(eval):6: read-only variable: pbro
+?typeset -r pbro
 ?0
-?(eval):9: read-only variable: pbro
+?(eval):10: read-only variable: pbro



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