From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: Detect if a script is being sourced vs executed
Date: Fri, 12 Sep 2014 20:05:09 +0100 [thread overview]
Message-ID: <20140912200509.6cac2742@pws-pc.ntlworld.com> (raw)
In-Reply-To: <541333FF.3090601@thequod.de>
On Fri, 12 Sep 2014 19:57:19 +0200
Daniel Hahler <dhahler@gmail.com> wrote:
> I want to detect if a script is being sourced instead of being called.
I think this is difficult for older versions of zsh, but Bart will
probably come up with something.
I was surprised to find the zsh/parameter modules series of array
parameters that allow you to trace back the functions, sourced files,
etc. being executed don't give this information, which is present
internally. This adds $functypestack to do this. So you need to
see if $functypestack[1] is "source".
diff --git a/Doc/Zsh/mod_parameter.yo b/Doc/Zsh/mod_parameter.yo
index 32d4796..0dde813 100644
--- a/Doc/Zsh/mod_parameter.yo
+++ b/Doc/Zsh/mod_parameter.yo
@@ -201,7 +201,10 @@ The format of each element is var(filename)tt(:)var(lineno).
For functions autoloaded from a file in native zsh format, where only the
body of the function occurs in the file, or for files that have been
executed by the tt(source) or `tt(.)' builtins, the trace information is
-shown as var(filename)tt(:)var(0), since the entire file is the definition.
+shown as var(filename)tt(:)var(0), since the entire file is the
+definition.
+
+The most recent call is the first element in the array.
Most users will be interested in the information in the
tt(funcfiletrace) array instead.
@@ -221,4 +224,13 @@ The format of each element is var(name)tt(:)var(lineno).
Callers are also shown for sourced files; the caller is the point
where the tt(source) or `tt(.)' command was executed.
)
+vindex(functypestack)
+item(tt(functrace))(
+This array corresponds element by element with tt(funcstack) and
+prints out `tt(source)' for a sourced file, `tt(function)' for a
+function and `tt(eval)' for an eval'd expression. At the top
+level of execution of a series of commands, tt($functypestack[1]) is
+empty if the commands are being executed as a script, or one of the
+strings above if the commands are being executed as that type.
+)
enditem()
diff --git a/Src/Modules/parameter.c b/Src/Modules/parameter.c
index 0385a70..02a642b 100644
--- a/Src/Modules/parameter.c
+++ b/Src/Modules/parameter.c
@@ -640,6 +640,41 @@ funcfiletracegetfn(UNUSED(Param pm))
return ret;
}
+/* Functions for the functypestack special parameter. */
+
+static char **
+functypestackgetfn(UNUSED(Param pm))
+{
+ Funcstack f;
+ int num;
+ char **ret, **p;
+
+ for (f = funcstack, num = 0; f; f = f->prev, num++);
+
+ ret = (char **) zhalloc((num + 1) * sizeof(char *));
+
+ for (f = funcstack, p = ret; f; f = f->prev, p++)
+ {
+ switch (f->tp)
+ {
+ case FS_SOURCE:
+ *p = "source";
+ break;
+
+ case FS_FUNC:
+ *p = "function";
+ break;
+
+ case FS_EVAL:
+ *p = "eval";
+ break;
+ }
+ }
+ *p = NULL;
+
+ return ret;
+}
+
/* Functions for the builtins special parameter. */
/**/
@@ -2046,6 +2081,8 @@ static const struct gsu_array funcsourcetrace_gsu =
{ funcsourcetracegetfn, arrsetfn, stdunsetfn };
static const struct gsu_array funcfiletrace_gsu =
{ funcfiletracegetfn, arrsetfn, stdunsetfn };
+static const struct gsu_array functypestack_gsu =
+{ functypestackgetfn, arrsetfn, stdunsetfn };
static const struct gsu_array reswords_gsu =
{ reswordsgetfn, arrsetfn, stdunsetfn };
static const struct gsu_array disreswords_gsu =
@@ -2090,6 +2127,8 @@ static struct paramdef partab[] = {
scanpmfunctions),
SPECIALPMDEF("functrace", PM_ARRAY|PM_READONLY,
&functrace_gsu, NULL, NULL),
+ SPECIALPMDEF("functypestack", PM_ARRAY|PM_READONLY,
+ &functypestack_gsu, NULL, NULL),
SPECIALPMDEF("galiases", 0,
&pmgaliases_gsu, getpmgalias, scanpmgaliases),
SPECIALPMDEF("history", PM_READONLY,
--
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
next prev parent reply other threads:[~2014-09-12 19:10 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-12 17:57 Daniel Hahler
2014-09-12 19:05 ` Peter Stephenson [this message]
2014-09-12 23:21 ` Bart Schaefer
2014-09-13 8:01 ` Peter Stephenson
2014-09-16 19:14 ` Peter Stephenson
2014-09-12 20:09 ` Kurtis Rader
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140912200509.6cac2742@pws-pc.ntlworld.com \
--to=p.w.stephenson@ntlworld.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).