about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJun-ichi Takimoto <takimoto-j@kba.biglobe.ne.jp>2023-04-09 20:44:58 +0900
committerJun-ichi Takimoto <takimoto-j@kba.biglobe.ne.jp>2023-04-09 20:44:58 +0900
commit8a9aea907ac4844e2ee7348c4fa6417ae9873991 (patch)
tree55eca9b97d1925990d78edcb333e83fc9489ec1b
parent98b4d4bdca15393d3cce9e072867ee6526de5d2e (diff)
downloadzsh-8a9aea907ac4844e2ee7348c4fa6417ae9873991.tar.gz
zsh-8a9aea907ac4844e2ee7348c4fa6417ae9873991.tar.xz
zsh-8a9aea907ac4844e2ee7348c4fa6417ae9873991.zip
51631: initialize $_ by copying it from environment
-rw-r--r--ChangeLog3
-rw-r--r--Doc/Zsh/params.yo5
-rw-r--r--Src/init.c9
3 files changed, 13 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 1eebe2660..32fd0780d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2023-04-09  Jun-ichi Takimoto  <takimoto-j@kba.biglobe.ne.jp>
 
+	* 51631: Doc/Zsh/params.yo, Src/init.c: initialize $_ by copying
+	it from environment
+
 	* 51632: Src/exec.c: unmetafy $_ when exporting it to child
 
 2023-04-03  Jun-ichi Takimoto  <takimoto-j@kba.biglobe.ne.jp>
diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo
index 528c27f93..2db4210eb 100644
--- a/Doc/Zsh/params.yo
+++ b/Doc/Zsh/params.yo
@@ -779,7 +779,10 @@ last pipeline.
 )
 vindex(_)
 item(tt(_) <S>)(
-The last argument of the previous command.
+Initially, if tt(_) exists in the environment, then this parameter is set to
+its value. This value may be the full pathname of the current zsh
+executable or the script command file.
+Later, this parameter is set to the last argument of the previous command.
 Also, this parameter is set in the environment of every command
 executed to the full pathname of the command.
 )
diff --git a/Src/init.c b/Src/init.c
index 68621a0ad..7e98af44c 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -1084,9 +1084,12 @@ setupvals(char *cmd, char *runscript, char *zsh_name)
 	ztrdup(DEFAULT_IFS_SH) : ztrdup(DEFAULT_IFS);
     wordchars   = ztrdup(DEFAULT_WORDCHARS);
     postedit    = ztrdup("");
-    zunderscore  = (char *) zalloc(underscorelen = 32);
-    underscoreused = 1;
-    *zunderscore = '\0';
+    /* If _ is set in environment then initialize our $_ by copying it */
+    zunderscore = getenv("_");
+    zunderscore = zunderscore ? metafy(zunderscore, -1, META_DUP) : ztrdup("");
+    underscoreused = strlen(zunderscore) + 1;
+    underscorelen = (underscoreused + 31) & ~31;
+    zunderscore = (char *)zrealloc(zunderscore, underscorelen);
 
     zoptarg = ztrdup("");
     zoptind = 1;