zsh-workers
 help / color / mirror / code / Atom feed
* Re: PATCH: funcstack[-1]  (formerly Suppress tracing of "set +x")
       [not found]         ` <20160205160236.1274aa5b@pwslap01u.europe.root.pri>
@ 2016-02-05 22:27           ` Peter Stephenson
  2016-02-07  0:22             ` Daniel Shahaf
  2016-02-14 21:52             ` Assigning to $0 (formerly: PATCH: funcstack[-1]) Bart Schaefer
  0 siblings, 2 replies; 7+ messages in thread
From: Peter Stephenson @ 2016-02-05 22:27 UTC (permalink / raw)
  To: Zsh hackers list

On Fri, 5 Feb 2016 10:44:44 +0000
Peter Stephenson <p.stephenson@samsung.com> wrote:
> Drifting a bit away from the original topic... I tried to improve this
> so you didn't get the "(anon)" in the PS4 output, instead something more
> useful.  I came up with this...
> 
>   () {
>     setopt localoptions promptsubst
>     PS4=${PS4//\%N/'${funcstack[2]:-$0}'}
>     setopt xtrace
>     # some command
>   }
> 
> This sort of works, but it struck me that it's harder than it might be
> to get the name of the top level script.

(Finally moved this to zsh-workers.)

Here's a much simpler way.  It strikes me that actually I could have set
POSIX_ARGZERO, but maybe this is useful.

By the way, looking at the code I just notice that the following isn't
an error even though it doesn't do what you presumably expect:

% setopt posixargzero
% print $0
zsh
% 0=foo
% print $0
zsh

Maybe that's OK so far, but you don't even get the value back when you
unset the option; it's silently lost.  It seems a bit pointless since
you'd get the same value of $0 with POSIX_ARGZERO set even if it stored
the value in the normal argzero space the way it seems to imply it did.

pws

diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo
index 6722092..159a5bb 100644
--- a/Doc/Zsh/params.yo
+++ b/Doc/Zsh/params.yo
@@ -951,6 +951,13 @@ track of versions of the shell during development between releases;
 hence most users should not use it and should instead rely on
 tt($ZSH_VERSION).
 )
+vindex(ZSH_SCRIPT)
+item(tt(ZSH_SCRIPT))(
+If zsh was inovked to run a script, this is the name of the script.
+Otherwise, the name used to invoke the current shell.  This is
+the same as the value of tt($0) when the tt(POSIX_ARGZERO) option is
+set, but is always available.
+)
 item(tt(zsh_scheduled_events))(
 See ifzman(the section `The zsh/sched Module' in zmanref(zshmodules))\
 ifnzman(noderef(The zsh/sched Module)).
diff --git a/Src/params.c b/Src/params.c
index 76ed61c..0233e2b 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -813,6 +813,7 @@ createparamtable(void)
     setsparam("TTY", ztrdup_metafy(ttystrname));
     setsparam("VENDOR", ztrdup_metafy(VENDOR));
     setsparam("ZSH_NAME", ztrdup_metafy(zsh_name));
+    setsparam("ZSH_SCRIPT", ztrdup(posixzero));
     setsparam("ZSH_VERSION", ztrdup_metafy(ZSH_VERSION));
     setsparam("ZSH_PATCHLEVEL", ztrdup_metafy(ZSH_PATCHLEVEL));
     setaparam("signals", sigptr = zalloc((SIGCOUNT+4) * sizeof(char *)));


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

* Re: PATCH: funcstack[-1]  (formerly Suppress tracing of "set +x")
  2016-02-05 22:27           ` PATCH: funcstack[-1] (formerly Suppress tracing of "set +x") Peter Stephenson
@ 2016-02-07  0:22             ` Daniel Shahaf
  2016-02-14 21:52             ` Assigning to $0 (formerly: PATCH: funcstack[-1]) Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Daniel Shahaf @ 2016-02-07  0:22 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh hackers list

Peter Stephenson wrote on Fri, Feb 05, 2016 at 22:27:50 +0000:
> +If zsh was inovked to run a script, this is the name of the script.

Typo: s/inovked/invoked/


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

* Assigning to $0 (formerly: PATCH: funcstack[-1])
  2016-02-05 22:27           ` PATCH: funcstack[-1] (formerly Suppress tracing of "set +x") Peter Stephenson
  2016-02-07  0:22             ` Daniel Shahaf
@ 2016-02-14 21:52             ` Bart Schaefer
  2016-02-15  6:51               ` Mikael Magnusson
  2016-02-15  9:17               ` Peter Stephenson
  1 sibling, 2 replies; 7+ messages in thread
From: Bart Schaefer @ 2016-02-14 21:52 UTC (permalink / raw)
  To: Zsh hackers list

On Feb 5, 10:27pm, Peter Stephenson wrote:
}
} By the way, looking at the code I just notice that the following isn't
} an error even though it doesn't do what you presumably expect:
} 
} % setopt posixargzero
} % print $0
} zsh
} % 0=foo
} % print $0
} zsh
} 
} Maybe that's OK so far, but you don't even get the value back when you
} unset the option; it's silently lost.

So what behavior would be preferable?

1. POSIXARGZERO makes $0 report an error on assignment (read only?)

2. the value is stored and reappears when POSIXZERO is unset?

3. as (2) but a warning is printed?

As another by-the-way:

torch% print $0; () { typeset -g 0=argzero; print $0 }; print $0
Src/zsh
argzero
Src/zsh

So typeset will accept 0 as a valid name and -g as a valid option, but
can't actually set the global $0.


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

* Re: Assigning to $0 (formerly: PATCH: funcstack[-1])
  2016-02-14 21:52             ` Assigning to $0 (formerly: PATCH: funcstack[-1]) Bart Schaefer
@ 2016-02-15  6:51               ` Mikael Magnusson
  2016-02-15  9:17               ` Peter Stephenson
  1 sibling, 0 replies; 7+ messages in thread
From: Mikael Magnusson @ 2016-02-15  6:51 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On Sun, Feb 14, 2016 at 10:52 PM, Bart Schaefer
<schaefer@brasslantern.com> wrote:
> On Feb 5, 10:27pm, Peter Stephenson wrote:
> }
> } By the way, looking at the code I just notice that the following isn't
> } an error even though it doesn't do what you presumably expect:
> }
> } % setopt posixargzero
> } % print $0
> } zsh
> } % 0=foo
> } % print $0
> } zsh
> }
> } Maybe that's OK so far, but you don't even get the value back when you
> } unset the option; it's silently lost.
>
> So what behavior would be preferable?
>
> 1. POSIXARGZERO makes $0 report an error on assignment (read only?)
>
> 2. the value is stored and reappears when POSIXZERO is unset?
>
> 3. as (2) but a warning is printed?
>
> As another by-the-way:
>
> torch% print $0; () { typeset -g 0=argzero; print $0 }; print $0
> Src/zsh
> argzero
> Src/zsh
>
> So typeset will accept 0 as a valid name and -g as a valid option, but
> can't actually set the global $0.

http://www.zsh.org/mla/workers/2015/msg01400.html Some time before
that patch, assigning to $0 was always possible, but at some point it
stopped working.

-- 
Mikael Magnusson


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

* Re: Assigning to $0 (formerly: PATCH: funcstack[-1])
  2016-02-14 21:52             ` Assigning to $0 (formerly: PATCH: funcstack[-1]) Bart Schaefer
  2016-02-15  6:51               ` Mikael Magnusson
@ 2016-02-15  9:17               ` Peter Stephenson
  2016-02-15 17:08                 ` Bart Schaefer
  1 sibling, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2016-02-15  9:17 UTC (permalink / raw)
  To: Zsh hackers list

On Sun, 14 Feb 2016 13:52:47 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Feb 5, 10:27pm, Peter Stephenson wrote:
> }
> } By the way, looking at the code I just notice that the following isn't
> } an error even though it doesn't do what you presumably expect:
> } 
> } % setopt posixargzero
> } % print $0
> } zsh
> } % 0=foo
> } % print $0
> } zsh
> } 
> } Maybe that's OK so far, but you don't even get the value back when you
> } unset the option; it's silently lost.
> 
> So what behavior would be preferable?
> 
> 1. POSIXARGZERO makes $0 report an error on assignment (read only?)

This seems to make the most sense.  Setting a value of $0 you're not
going to see immediately is pointless.

pws


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

* Re: Assigning to $0 (formerly: PATCH: funcstack[-1])
  2016-02-15  9:17               ` Peter Stephenson
@ 2016-02-15 17:08                 ` Bart Schaefer
  2016-02-15 17:12                   ` Mikael Magnusson
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2016-02-15 17:08 UTC (permalink / raw)
  To: Zsh hackers list

On Feb 15,  7:51am, Mikael Magnusson wrote:
}
} > So typeset will accept 0 as a valid name and -g as a valid option, but
} > can't actually set the global $0.
} 
} http://www.zsh.org/mla/workers/2015/msg01400.html Some time before
} that patch, assigning to $0 was always possible, but at some point it
} stopped working.

No, I think it was never possible to assign to the global $0 from inside
a function.

torch% print $ZSH_VERSION
4.2.0
torch% zero() { typeset -g 0=argzero; print $0 }
torch% print $0; zero; print $0
/bin/zsh
argzero
/bin/zsh
torch% 

This is because the "argzero" C global is handled independently of the
usual parameter scope code.

On Feb 15,  9:17am, Peter Stephenson wrote:
}
} > 1. POSIXARGZERO makes $0 report an error on assignment (read only?)
} 
} This seems to make the most sense.  Setting a value of $0 you're not
} going to see immediately is pointless.


diff --git a/Src/params.c b/Src/params.c
index 0233e2b..8bd8a8e 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -4158,7 +4158,9 @@ static void
 argzerosetfn(UNUSED(Param pm), char *x)
 {
     if (x) {
-	if (!isset(POSIXARGZERO)) {
+	if (isset(POSIXARGZERO))
+	    zerr("read-only variable: 0");
+	else {
 	    zsfree(argzero);
 	    argzero = ztrdup(x);
 	}


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

* Re: Assigning to $0 (formerly: PATCH: funcstack[-1])
  2016-02-15 17:08                 ` Bart Schaefer
@ 2016-02-15 17:12                   ` Mikael Magnusson
  0 siblings, 0 replies; 7+ messages in thread
From: Mikael Magnusson @ 2016-02-15 17:12 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On Mon, Feb 15, 2016 at 6:08 PM, Bart Schaefer
<schaefer@brasslantern.com> wrote:
> On Feb 15,  7:51am, Mikael Magnusson wrote:
> }
> } > So typeset will accept 0 as a valid name and -g as a valid option, but
> } > can't actually set the global $0.
> }
> } http://www.zsh.org/mla/workers/2015/msg01400.html Some time before
> } that patch, assigning to $0 was always possible, but at some point it
> } stopped working.
>
> No, I think it was never possible to assign to the global $0 from inside
> a function.
>
> torch% print $ZSH_VERSION
> 4.2.0
> torch% zero() { typeset -g 0=argzero; print $0 }
> torch% print $0; zero; print $0
> /bin/zsh
> argzero
> /bin/zsh
> torch%
>
> This is because the "argzero" C global is handled independently of the
> usual parameter scope code.

Ah, right. I must have assumed on some level that we were talking
about a function-scoped $0 because I have never in my life wanted to
change the top-level $0.

-- 
Mikael Magnusson


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

end of thread, other threads:[~2016-02-15 17:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20151230104531.GA20496@linux.vnet.ibm.com>
     [not found] ` <5683F898.7010907@inlv.org>
     [not found]   ` <20160205100902.GA14979@linux.vnet.ibm.com>
     [not found]     ` <20160205102735.1f09973a@pwslap01u.europe.root.pri>
     [not found]       ` <20160205104444.2437ad33@pwslap01u.europe.root.pri>
     [not found]         ` <20160205160236.1274aa5b@pwslap01u.europe.root.pri>
2016-02-05 22:27           ` PATCH: funcstack[-1] (formerly Suppress tracing of "set +x") Peter Stephenson
2016-02-07  0:22             ` Daniel Shahaf
2016-02-14 21:52             ` Assigning to $0 (formerly: PATCH: funcstack[-1]) Bart Schaefer
2016-02-15  6:51               ` Mikael Magnusson
2016-02-15  9:17               ` Peter Stephenson
2016-02-15 17:08                 ` Bart Schaefer
2016-02-15 17:12                   ` Mikael Magnusson

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).