zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: zsh-workers@zsh.org
Subject: Re: a way to get the value of the -c option
Date: Thu, 03 Dec 2015 17:01:29 +0000	[thread overview]
Message-ID: <20151203170129.49ab6658@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <20151203134929.GA9581@chaz.gmail.com>

On Thu, 03 Dec 2015 13:49:29 +0000
Stephane Chazelas <stephane.chazelas@gmail.com> wrote:
> Note that bash calls it $BASH_EXECUTION_STRING
> 
> It could be useful to have zsh's equivalent being
> $ZSH_EXECUTION_STRING.

Strikes me as a bit wordy and non-specific, but it would probably help
to be similar...

pws

diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo
index 21bb874..6722092 100644
--- a/Doc/Zsh/params.yo
+++ b/Doc/Zsh/params.yo
@@ -933,6 +933,11 @@ tt(zsh/zutil) module.
 )
 enditem()
 )
+vindex(ZSH_EXECUTION_STRING)
+item(tt(ZSH_EXECUTION_STRING))(
+If the shell was started with the option tt(-c), this contains
+the argument passed to the option.  Otherwise it is not set.
+)
 vindex(ZSH_NAME)
 item(tt(ZSH_NAME))(
 Expands to the basename of the command used to invoke this instance
diff --git a/Src/init.c b/Src/init.c
index dcce1d7..7894893 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -240,13 +240,11 @@ loop(int toplevel, int justonce)
     return LOOP_OK;
 }
 
-/* Shared among parseargs(), parseopts(), init_io(), and init_misc() */
-static char *cmd;
 static int restricted;
 
 /**/
 static void
-parseargs(char **argv, char **runscript)
+parseargs(char **argv, char **runscript, char **cmdptr)
 {
     char **x;
     LinkList paramlist;
@@ -272,7 +270,7 @@ parseargs(char **argv, char **runscript)
     opts[SHINSTDIN] = 0;
     opts[SINGLECOMMAND] = 0;
 
-    if (parseopts(NULL, &argv, opts, &cmd, NULL))
+    if (parseopts(NULL, &argv, opts, cmdptr, NULL))
 	exit(1);
 
     /*
@@ -290,7 +288,7 @@ parseargs(char **argv, char **runscript)
     if (*argv) {
 	if (unset(SHINSTDIN)) {
 	    posixzero = *argv;
-	    if (cmd)
+	    if (*cmdptr)
 		argzero = *argv;
 	    else
 		*runscript = *argv;
@@ -299,7 +297,7 @@ parseargs(char **argv, char **runscript)
 	}
 	while (*argv)
 	    zaddlinknode(paramlist, ztrdup(*argv++));
-    } else if (!cmd)
+    } else if (!*cmdptr)
 	opts[SHINSTDIN] = 1;
     if(isset(SINGLECOMMAND))
 	opts[INTERACTIVE] &= 1;
@@ -497,7 +495,7 @@ printhelp(void)
 
 /**/
 mod_export void
-init_io(void)
+init_io(char *cmd)
 {
     static char outbuf[BUFSIZ], errbuf[BUFSIZ];
 
@@ -802,7 +800,7 @@ init_term(void)
 
 /**/
 void
-setupvals(void)
+setupvals(char *cmd)
 {
 #ifdef USE_GETPWUID
     struct passwd *pswd;
@@ -1086,6 +1084,9 @@ setupvals(void)
 
     /* Colour sequences for outputting colours in prompts and zle */
     set_default_colour_sequences();
+
+    if (cmd)
+	setsparam("ZSH_EXECUTION_STRING", ztrdup(cmd));
 }
 
 /*
@@ -1267,7 +1268,7 @@ run_init_scripts(void)
 
 /**/
 void
-init_misc(void)
+init_misc(char *cmd)
 {
 #ifndef RESTRICTED_R
     if ( restricted )
@@ -1604,6 +1605,7 @@ mod_export int
 zsh_main(UNUSED(int argc), char **argv)
 {
     char **t, *runscript = NULL;
+    char *cmd;			/* argument to -c */
     int t0;
 #ifdef USE_LOCALE
     setlocale(LC_ALL, "");
@@ -1652,18 +1654,18 @@ zsh_main(UNUSED(int argc), char **argv)
     opts[LOGINSHELL] = (**argv == '-');
     opts[PRIVILEGED] = (getuid() != geteuid() || getgid() != getegid());
     /* sets ZLE, INTERACTIVE, SHINSTDIN and SINGLECOMMAND */
-    parseargs(argv, &runscript);
+    parseargs(argv, &runscript, &cmd);
 
     SHTTY = -1;
-    init_io();
-    setupvals();
+    init_io(cmd);
+    setupvals(cmd);
 
     init_signals();
     init_bltinmods();
     init_builtins();
     run_init_scripts();
     setupshin(runscript);
-    init_misc();
+    init_misc(cmd);
 
     for (;;) {
 	/*


  reply	other threads:[~2015-12-03 17:01 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-03 11:54 Vincent Lefevre
2015-12-03 12:45 ` Peter Stephenson
2015-12-03 13:36   ` Vincent Lefevre
2015-12-03 14:01     ` Peter Stephenson
2015-12-03 14:23       ` Peter Stephenson
2015-12-03 14:40         ` Peter Stephenson
2015-12-03 14:39       ` Vincent Lefevre
2015-12-03 13:49   ` Stephane Chazelas
2015-12-03 17:01     ` Peter Stephenson [this message]
2015-12-03 14:36 ` Nikolay Aleksandrovich Pavlov (ZyX)

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=20151203170129.49ab6658@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-workers@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).