aboutsummaryrefslogtreecommitdiffhomepage
path: root/exec.cpp
diff options
context:
space:
mode:
authorGravatar Kevin Ballard <kevin@sb.org>2014-09-19 17:31:34 -0700
committerGravatar Kevin Ballard <kevin@sb.org>2014-09-19 17:38:54 -0700
commit940f264ee6b75e013658d4ba7f6a6dd77ae014d3 (patch)
treecb14835d78554378343c4464167062ff7a650823 /exec.cpp
parent761be8ab7fb62965b8dc9dfd6ac6451cda578ad0 (diff)
Decrement SHLVL when running `exec`
`exec` removes fish from the shell "stack", so SHLVL needs to be decremented to match. This means `exec fish` will result in the same SHLVL in the new fish instance. Also tweak the SHLVL logic to interpret an environment SHLVL of "3foo" as garbage instead of as the value "3". Fixes #1693.
Diffstat (limited to 'exec.cpp')
-rw-r--r--exec.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/exec.cpp b/exec.cpp
index 3b5506da..7706dcc5 100644
--- a/exec.cpp
+++ b/exec.cpp
@@ -651,6 +651,21 @@ void exec_job(parser_t &parser, job_t *j)
/* PCA This is for handling exec. Passing all_ios here matches what fish 2.0.0 and 1.x did. It's known to be wrong - for example, it means that redirections bound for subsequent commands in the pipeline will apply to exec. However, using exec in a pipeline doesn't really make sense, so I'm not trying to fix it here. */
if (!setup_child_process(j, 0, all_ios))
{
+ /* decrement SHLVL as we're removing ourselves from the shell "stack" */
+ const env_var_t shlvl_str = env_get_string(L"SHLVL", ENV_GLOBAL | ENV_EXPORT);
+ wcstring nshlvl_str = L"0";
+ if (!shlvl_str.missing())
+ {
+ wchar_t *end;
+ long shlvl_i = wcstol(shlvl_str.c_str(), &end, 10);
+ while (iswspace(*end)) ++end; /* skip trailing whitespace */
+ if (shlvl_i > 0 && *end == '\0')
+ {
+ nshlvl_str = to_string<long>(shlvl_i - 1);
+ }
+ }
+ env_set(L"SHLVL", nshlvl_str.c_str(), ENV_GLOBAL | ENV_EXPORT);
+
/*
launch_process _never_ returns
*/