summary refs log tree commit diff
diff options
context:
space:
mode:
authorBarton E. Schaefer <schaefer@zsh.org>2016-10-24 07:14:39 -0700
committerBarton E. Schaefer <schaefer@zsh.org>2016-10-24 07:14:39 -0700
commit0f5e670cde5f844680a20f986786249dfe983584 (patch)
treef912a4991610294666f2ec708dff1d87a58ea0b5
parent71dd0ab62ecf67e3a4a60749e9f5b1cfc9ac0ec4 (diff)
downloadzsh-0f5e670cde5f844680a20f986786249dfe983584.tar.gz
zsh-0f5e670cde5f844680a20f986786249dfe983584.tar.xz
zsh-0f5e670cde5f844680a20f986786249dfe983584.zip
"typeset -p" uses "export" commands or the "-g" option for parameters that are not local to the current scope
-rw-r--r--ChangeLog9
-rw-r--r--NEWS3
-rw-r--r--README5
-rw-r--r--Src/params.c22
-rw-r--r--Test/B02typeset.ztst4
-rw-r--r--Test/B03print.ztst4
-rw-r--r--Test/V10private.ztst2
7 files changed, 39 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index 717f25e76..ee1fd94dc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2016-10-24  Barton E. Schaefer  <schaefer@zsh.org>
+
+	* unposted: NEWS, README: update for 39704.
+	
+	* 39704: Src/params.c, Test/B02typeset.ztst, Test/B03print.ztst,
+	Test/V10private.ztst: the output of "typeset -p" uses "export"
+	commands or the "-g" option for parameters that are not local to
+	the current scope.
+
 2016-10-24  Daniel Shahaf  <d.s@daniel.shahaf.name>
 
 	* 39706: Completion/Unix/Type/_tilde_files, Doc/Zsh/compsys.yo:
diff --git a/NEWS b/NEWS
index 65b246d33..35e0e2e5d 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,9 @@ removed, even when /before/here is itself a symbolic link.  It is
 recommended to review uses of ':A' and, if appropriate, convert them
 to ':P' as soon as compatibility with 5.2 is no longer a requirement.
 
+The output of "typeset -p" uses "export" commands or the "-g" option
+for parameters that are not local to the current scope.
+
 Changes from 5.1.1 to 5.2
 -------------------------
 
diff --git a/README b/README
index ed2183d8d..1a2e73936 100644
--- a/README
+++ b/README
@@ -110,6 +110,11 @@ possible to return a non-zero status to the parent shell from a command
 executed as a replacement, and the new implementation is more consistent
 with other shells.
 
+7) The output of "typeset -p" (and synonyms) now takes into account the
+function scope and export state of each parameter.  Exported parameters
+are output as "export" commands unless the parameter is also local, and
+other parameters not local to the scope are output with the "-g" option.
+
 Incompatibilities between 5.0.8 and 5.2
 ---------------------------------------
 
diff --git a/Src/params.c b/Src/params.c
index 1418021aa..3084b1ffe 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -5225,7 +5225,7 @@ printparamvalue(Param p, int printflags)
 {
     char *t, **u;
 
-    if (p->node.flags & PM_AUTOLOAD) {
+    if ((p->node.flags & PM_EXPORTED) && !p->env) {
 	putchar('\n');
 	return;
     }
@@ -5312,9 +5312,13 @@ printparamnode(HashNode hn, int printflags)
 	     */
 	    printflags |= PRINT_NAMEONLY;
 	}
+	else if (p->node.flags & PM_EXPORTED)
+	    printflags |= PRINT_NAMEONLY;
 	else
 	    return;
     }
+    if (p->node.flags & PM_AUTOLOAD)
+	printflags |= PRINT_NAMEONLY;
 
     if (printflags & PRINT_TYPESET) {
 	if ((p->node.flags & (PM_READONLY|PM_SPECIAL)) ==
@@ -5326,7 +5330,14 @@ printparamnode(HashNode hn, int printflags)
 	     */
 	    return;
 	}
-	printf("typeset ");
+	if (locallevel && p->level >= locallevel) {
+	    printf("typeset ");	    /* printf("local "); */
+	} else if (p->node.flags & PM_EXPORTED) {
+	    printf("export ");
+	} else if (locallevel) {
+	    printf("typeset -g ");
+	} else
+	    printf("typeset ");
     }
 
     /* Print the attributes of the parameter */
@@ -5339,7 +5350,9 @@ printparamnode(HashNode hn, int printflags)
 	    if (pmptr->flags & PMTF_TEST_LEVEL) {
 		if (p->level)
 		    doprint = 1;
-	    } else if (p->node.flags & pmptr->binflag)
+	    } else if ((pmptr->binflag != PM_EXPORTED ||
+			((p->node.flags & PM_LOCAL) || p->level)) &&
+		       (p->node.flags & pmptr->binflag))
 		doprint = 1;
 
 	    if (doprint) {
@@ -5351,9 +5364,8 @@ printparamnode(HashNode hn, int printflags)
 			}
 			putchar(pmptr->typeflag);
 		    }
-		} else {
+		} else
 		    printf("%s ", pmptr->string);
-		}
 		if ((pmptr->flags & PMTF_USE_BASE) && p->base) {
 		    printf("%d ", p->base);
 		    doneminus = 0;
diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index d6d24210b..6d85a63fe 100644
--- a/Test/B02typeset.ztst
+++ b/Test/B02typeset.ztst
@@ -454,7 +454,7 @@
  fn() { typeset -p array nonexistent; }
  fn
 1:declare -p shouldn't create scoped values
->typeset -a array=( foo bar )
+>typeset -g -a array=( foo bar )
 ?fn:typeset: no such variable: nonexistent
 
  unsetopt typesetsilent
@@ -490,7 +490,7 @@
 ?0
 ?(eval):5: read-only variable: pbro
 ?(eval):6: read-only variable: pbro
-?typeset -r pbro
+?typeset -g -r pbro
 ?0
 ?(eval):10: read-only variable: pbro
 
diff --git a/Test/B03print.ztst b/Test/B03print.ztst
index befe2f2dd..a4431cbc8 100644
--- a/Test/B03print.ztst
+++ b/Test/B03print.ztst
@@ -308,5 +308,5 @@
  printf -v foo "%s\0%s-" into the breach
  typeset -p foo
 0:print and printf into a variable
->typeset foo='once more'
->typeset foo=$'into\C-@the-breach\C-@-'
+>typeset -g foo='once more'
+>typeset -g foo=$'into\C-@the-breach\C-@-'
diff --git a/Test/V10private.ztst b/Test/V10private.ztst
index 320e35764..7ebf5a87f 100644
--- a/Test/V10private.ztst
+++ b/Test/V10private.ztst
@@ -129,7 +129,7 @@
 0:private hides value from surrounding scope in nested scope
 >typeset -a hash_test=( top level )
 >typeset -A hash_test=( in function )
->typeset -a hash_test=( top level )
+>typeset -g -a hash_test=( top level )
 >array-local top level
 >top level
 F:note "typeset" rather than "private" in output from outer