1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 04:58:13 +00:00

Shell: Parse and correctly evaluate 'command &' and 'command &&'

This commit adds the InBackground and ShortCircuitOnFailure attributes
to commands, which respectively cause the command to be run in the
background, and abort the command chain with exit status != 0.
This commit is contained in:
AnotherTest 2020-05-24 23:00:46 +04:30 committed by Andreas Kling
parent 143be7234f
commit c23c354779
3 changed files with 70 additions and 5 deletions

View file

@ -49,11 +49,11 @@ void Parser::commit_subcommand()
m_subcommands.append({ move(m_tokens), move(m_redirections), {} });
}
void Parser::commit_command()
void Parser::commit_command(Attributes attributes)
{
if (m_subcommands.is_empty())
return;
m_commands.append({ move(m_subcommands) });
m_commands.append({ move(m_subcommands), attributes });
}
void Parser::do_pipe()
@ -109,6 +109,31 @@ Vector<Command> Parser::parse()
commit_command();
break;
}
if (ch == '&') {
commit_token(Token::Special);
if (i + 1 >= m_input.length()) {
in_background:;
// Nothing interesting past this token, commit with InBackground
commit_subcommand();
commit_command(Attributes::InBackground);
break;
}
ch = m_input.characters()[++i];
++m_position;
if (ch == '&') {
// This is '&&', commit with ShortCircuit
commit_subcommand();
commit_command(Attributes::ShortCircuitOnFailure);
break;
}
--i;
--m_position;
goto in_background;
}
if (ch == '|') {
commit_token(Token::Special);
if (m_tokens.is_empty()) {