Skip to content

Commit

Permalink
Shell: Add POSIX-compliant character escaping
Browse files Browse the repository at this point in the history
POSIX.1-2017, Shells & Utilities, section 2.2
  • Loading branch information
malpas authored and awesomekling committed Sep 14, 2019
1 parent 2d24b12 commit f44e7dc
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Shell/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ Vector<Command> Parser::parse()
m_state = State::InRedirectionPath;
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 == '\'') {
m_state = State::InSingleQuotes;
break;
Expand Down Expand Up @@ -147,6 +157,21 @@ Vector<Command> Parser::parse()
m_state = State::Free;
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);
break;
};
Expand Down

0 comments on commit f44e7dc

Please sign in to comment.