zsh-workers
 help / color / mirror / code / Atom feed
* inherit OLDPWD from environment variable
@ 2024-07-17  7:59 Franklin Yu
  2024-07-17  9:02 ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Franklin Yu @ 2024-07-17  7:59 UTC (permalink / raw)
  To: zsh-workers

Hi folks, this could be either a bug report or feature request.

When I start Zsh with $OLDPWD set to a path different from $PWD, Zsh overwrites
$OLDPWD with $PWD. Is this expected, or is it a side effect of making sure
$OLDPWD is non-empty?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: inherit OLDPWD from environment variable
  2024-07-17  7:59 inherit OLDPWD from environment variable Franklin Yu
@ 2024-07-17  9:02 ` Peter Stephenson
  0 siblings, 0 replies; 7+ messages in thread
From: Peter Stephenson @ 2024-07-17  9:02 UTC (permalink / raw)
  To: Franklin Yu, zsh-workers

> On 17/07/2024 08:59 BST Franklin Yu <franklinyu@hotmail.com> wrote:
> Hi folks, this could be either a bug report or feature request.
> 
> When I start Zsh with $OLDPWD set to a path different from $PWD, Zsh overwrites
> $OLDPWD with $PWD. Is this expected, or is it a side effect of making sure
> $OLDPWD is non-empty?

I think it's just making sure OLDPWD is set to something on
initialisation.  The only comment says "initialize `OLDPWD' = `PWD'".

I can't think of any obvious objection to importing a value from the
environment and only setting the variable to $PWD if that was empty.
Until the directory within the shell changes its value is pretty
arbitrary.

pws


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: inherit OLDPWD from environment variable
  2024-07-21 19:30     ` Peter Stephenson
@ 2024-07-21 20:48       ` Peter Stephenson
  0 siblings, 0 replies; 7+ messages in thread
From: Peter Stephenson @ 2024-07-21 20:48 UTC (permalink / raw)
  To: zsh-workers

> On 21/07/2024 20:30 BST Peter Stephenson > Looks fine to me, I'll commit it assuming nobody comes up with any gotchas.

One correction.

diff --git a/Src/init.c b/Src/init.c
index ec21521b1..0aecb5db9 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -1245,7 +1245,11 @@ setupvals(char *cmd, char *runscript, char *zsh_name)
 	pwd = metafy(zgetcwd(), -1, META_DUP);
     }
 
-    oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */
+    oldpwd = zgetenv("OLDPWD");
+    if (oldpwd == NULL)
+        oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */
+    else
+	oldpwd = ztrdup(oldpwd);
 
     inittyptab();     /* initialize the ztypes table */
     initlextabs();    /* initialize lexing tables    */


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: inherit OLDPWD from environment variable
  2024-07-20  7:56   ` Franklin Yu
@ 2024-07-21 19:30     ` Peter Stephenson
  2024-07-21 20:48       ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2024-07-21 19:30 UTC (permalink / raw)
  To: zsh-workers

> On 20/07/2024 08:56 BST Franklin Yu <franklinyu@hotmail.com> wrote:
> Good catch. Indeed Bash accepts empty value. Then it becomes
> 
> diff --git a/Src/init.c b/Src/init.c
> index ec21521b1..092273228 100644
> --- a/Src/init.c
> +++ b/Src/init.c
> @@ -1245,7 +1245,9 @@ setupvals(char *cmd, char *runscript, char *zsh_name)
>         pwd = metafy(zgetcwd(), -1, META_DUP);
>      }
> 
> -    oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */
> +    oldpwd = zgetenv("OLDPWD");
> +    if (oldpwd == NULL)
> +        oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */
> 
>      inittyptab();     /* initialize the ztypes table */
>      initlextabs();    /* initialize lexing tables    */
> 

Looks fine to me, I'll commit it assuming nobody comes up with any gotchas.

pws


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: inherit OLDPWD from environment variable
  2024-07-20  2:46 ` Lawrence Velázquez
@ 2024-07-20  7:56   ` Franklin Yu
  2024-07-21 19:30     ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Franklin Yu @ 2024-07-20  7:56 UTC (permalink / raw)
  To: Lawrence Velázquez; +Cc: zsh-workers

Good catch. Indeed Bash accepts empty value. Then it becomes

diff --git a/Src/init.c b/Src/init.c
index ec21521b1..092273228 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -1245,7 +1245,9 @@ setupvals(char *cmd, char *runscript, char *zsh_name)
        pwd = metafy(zgetcwd(), -1, META_DUP);
     }

-    oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */
+    oldpwd = zgetenv("OLDPWD");
+    if (oldpwd == NULL)
+        oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */

     inittyptab();     /* initialize the ztypes table */
     initlextabs();    /* initialize lexing tables    */

________________________________________
From: Lawrence Velázquez <larryv@zsh.org>
Sent: 19 July 2024 19:46
To: Franklin Yu <franklinyu@hotmail.com>
Cc: zsh-workers@zsh.org <zsh-workers@zsh.org>
Subject: Re: inherit OLDPWD from environment variable
 
On Fri, Jul 19, 2024, at 9:55 PM, Franklin Yu wrote:
> -    oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */
> +    oldpwd = zgetenv("OLDPWD");
> +    if (oldpwd == NULL || oldpwd[0] == '\0')
> +        oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */

If we're going to begin importing OLDPWD from the environment, we
might as well align with other shells for consistency, which means
accepting a zero-length value as is.  This is how bash 5.2.26, dash
0.5.12, ksh93u+ 2012-08-01, mksh R59, and yash 2.56.1 all behave.

--
vq

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: inherit OLDPWD from environment variable
  2024-07-20  1:55 Franklin Yu
@ 2024-07-20  2:46 ` Lawrence Velázquez
  2024-07-20  7:56   ` Franklin Yu
  0 siblings, 1 reply; 7+ messages in thread
From: Lawrence Velázquez @ 2024-07-20  2:46 UTC (permalink / raw)
  To: Franklin Yu; +Cc: zsh-workers

On Fri, Jul 19, 2024, at 9:55 PM, Franklin Yu wrote:
> -    oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */
> +    oldpwd = zgetenv("OLDPWD");
> +    if (oldpwd == NULL || oldpwd[0] == '\0')
> +        oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */

If we're going to begin importing OLDPWD from the environment, we
might as well align with other shells for consistency, which means
accepting a zero-length value as is.  This is how bash 5.2.26, dash
0.5.12, ksh93u+ 2012-08-01, mksh R59, and yash 2.56.1 all behave.

-- 
vq


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: inherit OLDPWD from environment variable
@ 2024-07-20  1:55 Franklin Yu
  2024-07-20  2:46 ` Lawrence Velázquez
  0 siblings, 1 reply; 7+ messages in thread
From: Franklin Yu @ 2024-07-20  1:55 UTC (permalink / raw)
  To: zsh-workers

> > On 17/07/2024 08:59 BST Franklin Yu <franklinyu@hotmail.com> wrote:
> > Hi folks, this could be either a bug report or feature request.
> >
> > When I start Zsh with $OLDPWD set to a path different from $PWD, Zsh overwrites
> > $OLDPWD with $PWD. Is this expected, or is it a side effect of making sure
> > $OLDPWD is non-empty?
> 
> I think it's just making sure OLDPWD is set to something on
> initialisation.  The only comment says "initialize `OLDPWD' = `PWD'".
> 
> I can't think of any obvious objection to importing a value from the
> environment and only setting the variable to $PWD if that was empty.
> Until the directory within the shell changes its value is pretty
> arbitrary.
> 
> pws

Good to hear that this idea works for you. Shall we adopt this idea, then? I
think it is as simple as:

diff --git a/Src/init.c b/Src/init.c
index ec21521b1..092273228 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -1245,7 +1245,9 @@ setupvals(char *cmd, char *runscript, char *zsh_name)
        pwd = metafy(zgetcwd(), -1, META_DUP);
     }

-    oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */
+    oldpwd = zgetenv("OLDPWD");
+    if (oldpwd == NULL || oldpwd[0] == '\0')
+        oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */

     inittyptab();     /* initialize the ztypes table */
     initlextabs();    /* initialize lexing tables    */

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-07-21 20:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-17  7:59 inherit OLDPWD from environment variable Franklin Yu
2024-07-17  9:02 ` Peter Stephenson
2024-07-20  1:55 Franklin Yu
2024-07-20  2:46 ` Lawrence Velázquez
2024-07-20  7:56   ` Franklin Yu
2024-07-21 19:30     ` Peter Stephenson
2024-07-21 20:48       ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).