Skip to content

Commit

Permalink
nix-shell: support single quotes in shebangs
Browse files Browse the repository at this point in the history
  • Loading branch information
ncfavier committed Jun 7, 2023
1 parent bf7dc3c commit ac7e219
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ static std::vector<std::string> shellwords(const std::string & s)
std::string cur;
enum state {
sBegin,
sQuote
sSingleQuote,
sDoubleQuote
};
state st = sBegin;
auto it = begin;
Expand All @@ -56,15 +57,26 @@ static std::vector<std::string> shellwords(const std::string & s)
}
}
switch (*it) {
case '\'':
if (st != sDoubleQuote) {
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sSingleQuote : sBegin;
}
break;
case '"':
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sQuote : sBegin;
if (st != sSingleQuote) {
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sDoubleQuote : sBegin;
}
break;
case '\\':
/* perl shellwords mostly just treats the next char as part of the string with no special processing */
cur.append(begin, it);
begin = ++it;
if (st != sSingleQuote) {
/* perl shellwords mostly just treats the next char as part of the string with no special processing */
cur.append(begin, it);
begin = ++it;
}
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/shell.shebang.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! @ENV_PROG@ nix-shell
#! nix-shell -I nixpkgs=shell.nix --no-substitute
#! nix-shell --pure -i bash -p foo bar
#! nix-shell --pure -i bash -p '(_: foo) "absurd"' "b\ar"
echo "$(foo) $(bar) $@"

0 comments on commit ac7e219

Please sign in to comment.