Shell: Add POSIX-compliant character escaping

POSIX.1-2017, Shells & Utilities, section 2.2
This commit is contained in:
Aaron Malpas 2019-09-14 19:36:09 +10:00 committed by Andreas Kling
parent 2d24b12a34
commit f44e7dc5d0
Notes: sideshowbarker 2024-07-19 12:06:53 +09:00

View file

@ -84,6 +84,16 @@ Vector<Command> Parser::parse()
m_state = State::InRedirectionPath; m_state = State::InRedirectionPath;
break; break;
} }
if (ch == '\\') {
if (i == m_input.length() - 1) {
fprintf(stderr, "Syntax error: Nothing to escape (\\)\n");
return {};
}
char next_ch = m_input.characters()[i + 1];
m_token.append(next_ch);
++i;
break;
}
if (ch == '\'') { if (ch == '\'') {
m_state = State::InSingleQuotes; m_state = State::InSingleQuotes;
break; break;
@ -147,6 +157,21 @@ Vector<Command> Parser::parse()
m_state = State::Free; m_state = State::Free;
break; break;
} }
if (ch == '\\') {
if (i == m_input.length() - 1) {
fprintf(stderr, "Syntax error: Nothing to escape (\\)\n");
return {};
}
char next_ch = m_input.characters()[i + 1];
if (next_ch == '$' || next_ch == '`'
|| next_ch == '"' || next_ch == '\\') {
m_token.append(next_ch);
++i;
continue;
}
m_token.append('\\');
break;
}
m_token.append(ch); m_token.append(ch);
break; break;
}; };