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

Re: ksh compatibility: initial value of $_



> 2023/04/05 17:14, dana <dana@xxxxxxx> wrote:

> would it make sense to revive the idea of having a dedicated variable
> for it,

Yes, I've been thinking exactly the same thing.
If we add, say ZSH_EXEPATH, then I think just copying _ from envionment is
enough.
# For the script name, if ZSH_SCRIPT is not an absolute path, then we can
# use ${ZSH_SCRIPT:a} before cd-ing from the initial directory.

The following is the patch for initializing $_ from environment.
If this is OK, and if ZSH_EXEPATH (or any other name?) would be usefull,
then I will try to prepare the patch for it separately.


diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo
index 528c27f93..07529aa35 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 absolute 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;







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