1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-24 22:11:15 +02:00

nix-shell: support single quotes in shebangs

Single quotes are a basic feature of shell syntax that people expect to
work. They are also more convenient for writing literal code expressions
with less escaping.
This commit is contained in:
Naïm Favier 2023-06-07 18:12:35 +02:00 committed by Robert Hensing
parent 8b68bbb777
commit fa9642ec45
3 changed files with 24 additions and 10 deletions

View file

@ -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;
@ -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;
}
}