nix-shell: In #! mode, pass the last argument
"i < argc - 1" should be "i < argc".
This commit is contained in:
parent
ae1e4dfad2
commit
c287e797a8
|
@ -105,6 +105,7 @@ int main(int argc, char ** argv)
|
||||||
std::vector<string> args;
|
std::vector<string> args;
|
||||||
for (int i = 1; i < argc; ++i)
|
for (int i = 1; i < argc; ++i)
|
||||||
args.push_back(argv[i]);
|
args.push_back(argv[i]);
|
||||||
|
|
||||||
// Heuristic to see if we're invoked as a shebang script, namely, if we
|
// Heuristic to see if we're invoked as a shebang script, namely, if we
|
||||||
// have a single argument, it's the name of an executable file, and it
|
// have a single argument, it's the name of an executable file, and it
|
||||||
// starts with "#!".
|
// starts with "#!".
|
||||||
|
@ -115,7 +116,7 @@ int main(int argc, char ** argv)
|
||||||
if (std::regex_search(lines.front(), std::regex("^#!"))) {
|
if (std::regex_search(lines.front(), std::regex("^#!"))) {
|
||||||
lines.pop_front();
|
lines.pop_front();
|
||||||
inShebang = true;
|
inShebang = true;
|
||||||
for (int i = 2; i < argc - 1; ++i)
|
for (int i = 2; i < argc; ++i)
|
||||||
savedArgs.push_back(argv[i]);
|
savedArgs.push_back(argv[i]);
|
||||||
args.clear();
|
args.clear();
|
||||||
for (auto line : lines) {
|
for (auto line : lines) {
|
||||||
|
@ -288,9 +289,8 @@ int main(int argc, char ** argv)
|
||||||
// executes it unless it contains the string "perl" or "indir",
|
// executes it unless it contains the string "perl" or "indir",
|
||||||
// or (undocumented) argv[0] does not contain "perl". Exploit
|
// or (undocumented) argv[0] does not contain "perl". Exploit
|
||||||
// the latter by doing "exec -a".
|
// the latter by doing "exec -a".
|
||||||
if (std::regex_search(interpreter, std::regex("perl"))) {
|
if (std::regex_search(interpreter, std::regex("perl")))
|
||||||
execArgs = "-a PERL";
|
execArgs = "-a PERL";
|
||||||
}
|
|
||||||
|
|
||||||
std::ostringstream joined;
|
std::ostringstream joined;
|
||||||
for (const auto & i : savedArgs)
|
for (const auto & i : savedArgs)
|
||||||
|
@ -301,7 +301,6 @@ int main(int argc, char ** argv)
|
||||||
// read the shebang to understand which packages to read from. Since
|
// read the shebang to understand which packages to read from. Since
|
||||||
// this is handled via nix-shell -p, we wrap our ruby script execution
|
// this is handled via nix-shell -p, we wrap our ruby script execution
|
||||||
// in ruby -e 'load' which ignores the shebangs.
|
// in ruby -e 'load' which ignores the shebangs.
|
||||||
|
|
||||||
envCommand = (format("exec %1% %2% -e 'load(\"%3%\") -- %4%") % execArgs % interpreter % script % joined.str()).str();
|
envCommand = (format("exec %1% %2% -e 'load(\"%3%\") -- %4%") % execArgs % interpreter % script % joined.str()).str();
|
||||||
} else {
|
} else {
|
||||||
envCommand = (format("exec %1% %2% %3% %4%") % execArgs % interpreter % script % joined.str()).str();
|
envCommand = (format("exec %1% %2% %3% %4%") % execArgs % interpreter % script % joined.str()).str();
|
||||||
|
@ -421,7 +420,7 @@ int main(int argc, char ** argv)
|
||||||
// environment variables and shell functions. Also don't lose
|
// environment variables and shell functions. Also don't lose
|
||||||
// the current $PATH directories.
|
// the current $PATH directories.
|
||||||
auto rcfile = (Path) tmpDir + "/rc";
|
auto rcfile = (Path) tmpDir + "/rc";
|
||||||
writeFile(rcfile, (format(
|
writeFile(rcfile, fmt(
|
||||||
"rm -rf '%1%'; "
|
"rm -rf '%1%'; "
|
||||||
"[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc; "
|
"[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc; "
|
||||||
"%2%"
|
"%2%"
|
||||||
|
@ -435,13 +434,12 @@ int main(int argc, char ** argv)
|
||||||
"unset NIX_INDENT_MAKE; "
|
"unset NIX_INDENT_MAKE; "
|
||||||
"shopt -u nullglob; "
|
"shopt -u nullglob; "
|
||||||
"unset TZ; %4%"
|
"unset TZ; %4%"
|
||||||
"%5%"
|
"%5%",
|
||||||
)
|
(Path) tmpDir,
|
||||||
% (Path) tmpDir
|
(pure ? "" : "p=$PATH; "),
|
||||||
% (pure ? "" : "p=$PATH; ")
|
(pure ? "" : "PATH=$PATH:$p; unset p; "),
|
||||||
% (pure ? "" : "PATH=$PATH:$p; unset p; ")
|
(getenv("TZ") ? (string("export TZ='") + getenv("TZ") + "'; ") : ""),
|
||||||
% (getenv("TZ") ? (string("export TZ='") + getenv("TZ") + "'; ") : "")
|
envCommand));
|
||||||
% envCommand).str());
|
|
||||||
|
|
||||||
Strings envStrs;
|
Strings envStrs;
|
||||||
for (auto & i : env)
|
for (auto & i : env)
|
||||||
|
|
|
@ -17,5 +17,5 @@ output=$(NIX_PATH=nixpkgs=shell.nix nix-shell --pure -p foo bar --run 'echo "$(f
|
||||||
sed -e "s|@ENV_PROG@|$(type -p env)|" shell.shebang.sh > $TEST_ROOT/shell.shebang.sh
|
sed -e "s|@ENV_PROG@|$(type -p env)|" shell.shebang.sh > $TEST_ROOT/shell.shebang.sh
|
||||||
chmod a+rx $TEST_ROOT/shell.shebang.sh
|
chmod a+rx $TEST_ROOT/shell.shebang.sh
|
||||||
|
|
||||||
output=$($TEST_ROOT/shell.shebang.sh)
|
output=$($TEST_ROOT/shell.shebang.sh abc def)
|
||||||
[ "$output" = "foo bar" ]
|
[ "$output" = "foo bar abc def" ]
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#! @ENV_PROG@ nix-shell
|
#! @ENV_PROG@ nix-shell
|
||||||
#! nix-shell -I nixpkgs=shell.nix --option use-binary-caches false
|
#! nix-shell -I nixpkgs=shell.nix --option use-binary-caches false
|
||||||
#! nix-shell --pure -i bash -p foo bar
|
#! nix-shell --pure -i bash -p foo bar
|
||||||
echo "$(foo) $(bar)"
|
echo "$(foo) $(bar) $@"
|
||||||
|
|
Loading…
Reference in a new issue