2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-05-07 02:12:08 +03:00
|
|
|
#include "Parser.h"
|
2020-04-30 03:56:16 +03:00
|
|
|
#include <ctype.h>
|
2019-05-07 02:12:08 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-05-10 09:05:23 +03:00
|
|
|
void Parser::commit_token(Token::Type type, AllowEmptyToken allow_empty)
|
2019-05-07 02:12:08 +03:00
|
|
|
{
|
2020-01-25 14:16:45 +03:00
|
|
|
if (allow_empty == AllowEmptyToken::No && m_token.is_empty())
|
2019-05-07 02:12:08 +03:00
|
|
|
return;
|
2020-05-29 17:28:35 +03:00
|
|
|
Token token { String::copy(m_token), m_position, m_token.size(), type };
|
2020-04-30 03:56:16 +03:00
|
|
|
if (state() == InRedirectionPath) {
|
2020-05-29 17:28:35 +03:00
|
|
|
m_redirections.last().path = move(token);
|
2019-05-07 02:12:08 +03:00
|
|
|
m_token.clear_with_capacity();
|
|
|
|
return;
|
|
|
|
}
|
2020-05-29 17:28:35 +03:00
|
|
|
m_tokens.append(token);
|
2019-05-07 02:12:08 +03:00
|
|
|
m_token.clear_with_capacity();
|
|
|
|
};
|
|
|
|
|
|
|
|
void Parser::commit_subcommand()
|
|
|
|
{
|
|
|
|
if (m_tokens.is_empty())
|
|
|
|
return;
|
2019-06-04 21:36:08 +03:00
|
|
|
m_subcommands.append({ move(m_tokens), move(m_redirections), {} });
|
2019-05-07 02:12:08 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 21:30:46 +03:00
|
|
|
void Parser::commit_command(Attributes attributes)
|
2019-08-30 07:54:05 +03:00
|
|
|
{
|
|
|
|
if (m_subcommands.is_empty())
|
|
|
|
return;
|
2020-05-24 21:30:46 +03:00
|
|
|
m_commands.append({ move(m_subcommands), attributes });
|
2019-08-30 07:54:05 +03:00
|
|
|
}
|
|
|
|
|
2019-05-07 02:12:08 +03:00
|
|
|
void Parser::do_pipe()
|
|
|
|
{
|
|
|
|
m_redirections.append({ Redirection::Pipe, STDOUT_FILENO });
|
|
|
|
commit_subcommand();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::begin_redirect_read(int fd)
|
|
|
|
{
|
|
|
|
m_redirections.append({ Redirection::FileRead, fd });
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::begin_redirect_write(int fd)
|
|
|
|
{
|
|
|
|
m_redirections.append({ Redirection::FileWrite, fd });
|
|
|
|
}
|
|
|
|
|
2020-04-30 03:56:16 +03:00
|
|
|
bool Parser::in_state(State state) const
|
|
|
|
{
|
|
|
|
for (auto s : m_state_stack)
|
|
|
|
if (s == state)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-30 07:54:05 +03:00
|
|
|
Vector<Command> Parser::parse()
|
2019-05-07 02:12:08 +03:00
|
|
|
{
|
2020-05-10 09:05:23 +03:00
|
|
|
for (size_t i = 0; i < m_input.length(); ++i, m_position = i) {
|
2019-05-07 02:12:08 +03:00
|
|
|
char ch = m_input.characters()[i];
|
2020-04-30 03:56:16 +03:00
|
|
|
switch (state()) {
|
2019-05-07 02:12:08 +03:00
|
|
|
case State::Free:
|
2020-05-10 10:17:12 +03:00
|
|
|
if (ch == '#') {
|
|
|
|
commit_token(Token::Bare);
|
|
|
|
|
|
|
|
while (i < m_input.length()) {
|
|
|
|
ch = m_input.characters()[++i];
|
|
|
|
++m_position;
|
|
|
|
if (ch == '\n')
|
|
|
|
break;
|
|
|
|
m_token.append(ch);
|
|
|
|
}
|
|
|
|
commit_token(Token::Comment);
|
|
|
|
break;
|
|
|
|
}
|
2019-05-07 02:12:08 +03:00
|
|
|
if (ch == ' ') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Bare);
|
2019-05-07 02:12:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-08-30 07:54:05 +03:00
|
|
|
if (ch == ';') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2019-08-30 07:54:05 +03:00
|
|
|
commit_subcommand();
|
|
|
|
commit_command();
|
|
|
|
break;
|
|
|
|
}
|
2020-05-24 21:30:46 +03:00
|
|
|
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;
|
|
|
|
}
|
2019-05-07 02:12:08 +03:00
|
|
|
if (ch == '|') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2019-05-07 02:12:08 +03:00
|
|
|
if (m_tokens.is_empty()) {
|
|
|
|
fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
|
2019-06-07 12:49:21 +03:00
|
|
|
return {};
|
2019-05-07 02:12:08 +03:00
|
|
|
}
|
|
|
|
do_pipe();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '>') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2019-05-07 02:12:08 +03:00
|
|
|
begin_redirect_write(STDOUT_FILENO);
|
2020-06-08 10:08:37 +03:00
|
|
|
ASSERT(!m_redirections.is_empty());
|
|
|
|
m_redirections.last().redirection_op_start = m_position;
|
2019-05-26 01:38:11 +03:00
|
|
|
|
|
|
|
// Search for another > for append.
|
2020-04-30 03:56:16 +03:00
|
|
|
push_state(State::InWriteAppendOrRedirectionPath);
|
2019-05-07 02:12:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '<') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2019-05-07 02:12:08 +03:00
|
|
|
begin_redirect_read(STDIN_FILENO);
|
2020-06-08 10:08:37 +03:00
|
|
|
ASSERT(!m_redirections.is_empty());
|
|
|
|
m_redirections.last().redirection_op_start = m_position;
|
2020-04-30 03:56:16 +03:00
|
|
|
push_state(State::InRedirectionPath);
|
2019-05-07 02:12:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-09-14 12:36:09 +03:00
|
|
|
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;
|
|
|
|
}
|
2019-05-07 02:12:08 +03:00
|
|
|
if (ch == '\'') {
|
2020-04-30 03:56:16 +03:00
|
|
|
push_state(State::InSingleQuotes);
|
2019-05-07 02:12:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '\"') {
|
2020-04-30 03:56:16 +03:00
|
|
|
push_state(State::InDoubleQuotes);
|
2019-05-07 02:12:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-04-30 03:56:16 +03:00
|
|
|
|
2019-12-04 15:31:53 +03:00
|
|
|
// redirection from zsh-style multi-digit fd, such as {10}>file
|
|
|
|
if (ch == '{') {
|
|
|
|
bool is_multi_fd_redirection = false;
|
2019-12-09 19:45:40 +03:00
|
|
|
size_t redir_end = i + 1;
|
2019-12-04 15:31:53 +03:00
|
|
|
|
|
|
|
while (redir_end < m_input.length()) {
|
|
|
|
char lookahead_ch = m_input.characters()[redir_end];
|
|
|
|
if (isdigit(lookahead_ch)) {
|
|
|
|
++redir_end;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (lookahead_ch == '}' && redir_end + 1 != m_input.length()) {
|
|
|
|
// Disallow {}> and {}<
|
|
|
|
if (redir_end == i + 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
++redir_end;
|
|
|
|
if (m_input.characters()[redir_end] == '>' || m_input.characters()[redir_end] == '<')
|
|
|
|
is_multi_fd_redirection = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_multi_fd_redirection) {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2019-12-04 15:31:53 +03:00
|
|
|
|
|
|
|
int fd = atoi(&m_input.characters()[i + 1]);
|
|
|
|
|
|
|
|
if (m_input.characters()[redir_end] == '>') {
|
|
|
|
begin_redirect_write(fd);
|
2020-06-08 10:08:37 +03:00
|
|
|
ASSERT(!m_redirections.is_empty());
|
|
|
|
m_redirections.last().redirection_op_start = m_position;
|
2019-12-04 15:31:53 +03:00
|
|
|
// Search for another > for append.
|
2020-04-30 03:56:16 +03:00
|
|
|
push_state(State::InWriteAppendOrRedirectionPath);
|
2019-12-04 15:31:53 +03:00
|
|
|
}
|
|
|
|
if (m_input.characters()[redir_end] == '<') {
|
|
|
|
begin_redirect_read(fd);
|
2020-06-08 10:08:37 +03:00
|
|
|
ASSERT(!m_redirections.is_empty());
|
|
|
|
m_redirections.last().redirection_op_start = m_position;
|
2020-04-30 03:56:16 +03:00
|
|
|
push_state(State::InRedirectionPath);
|
2019-12-04 15:31:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
i = redir_end;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 19:38:32 +03:00
|
|
|
if (isdigit(ch)) {
|
|
|
|
if (i != m_input.length() - 1) {
|
|
|
|
char next_ch = m_input.characters()[i + 1];
|
|
|
|
if (next_ch == '>') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2019-12-03 19:38:32 +03:00
|
|
|
begin_redirect_write(ch - '0');
|
2020-06-08 10:08:37 +03:00
|
|
|
ASSERT(!m_redirections.is_empty());
|
|
|
|
m_redirections.last().redirection_op_start = m_position;
|
2019-12-03 19:38:32 +03:00
|
|
|
++i;
|
|
|
|
|
|
|
|
// Search for another > for append.
|
2020-04-30 03:56:16 +03:00
|
|
|
push_state(State::InWriteAppendOrRedirectionPath);
|
2019-12-03 19:38:32 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (next_ch == '<') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2019-12-03 19:38:32 +03:00
|
|
|
begin_redirect_read(ch - '0');
|
2020-06-08 10:08:37 +03:00
|
|
|
ASSERT(!m_redirections.is_empty());
|
|
|
|
m_redirections.last().redirection_op_start = m_position;
|
2019-12-03 19:38:32 +03:00
|
|
|
++i;
|
|
|
|
|
2020-04-30 03:56:16 +03:00
|
|
|
push_state(State::InRedirectionPath);
|
2019-12-03 19:38:32 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 02:12:08 +03:00
|
|
|
m_token.append(ch);
|
|
|
|
break;
|
2019-05-26 01:38:11 +03:00
|
|
|
case State::InWriteAppendOrRedirectionPath:
|
|
|
|
if (ch == '>') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2020-04-30 03:56:16 +03:00
|
|
|
pop_state();
|
|
|
|
push_state(State::InRedirectionPath);
|
2019-05-26 01:38:11 +03:00
|
|
|
ASSERT(m_redirections.size());
|
2020-06-08 10:08:37 +03:00
|
|
|
m_redirections.last().type = Redirection::FileWriteAppend;
|
2019-05-26 01:38:11 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not another > means that it's probably a path.
|
2020-04-30 03:56:16 +03:00
|
|
|
pop_state();
|
|
|
|
push_state(InRedirectionPath);
|
2019-05-26 01:38:11 +03:00
|
|
|
[[fallthrough]];
|
2019-05-07 02:12:08 +03:00
|
|
|
case State::InRedirectionPath:
|
|
|
|
if (ch == '<') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2019-05-07 02:12:08 +03:00
|
|
|
begin_redirect_read(STDIN_FILENO);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '>') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2019-05-07 02:12:08 +03:00
|
|
|
begin_redirect_read(STDOUT_FILENO);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '|') {
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Special);
|
2019-05-07 02:12:08 +03:00
|
|
|
if (m_tokens.is_empty()) {
|
|
|
|
fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
|
2019-06-07 12:49:21 +03:00
|
|
|
return {};
|
2019-05-07 02:12:08 +03:00
|
|
|
}
|
|
|
|
do_pipe();
|
2020-04-30 03:56:16 +03:00
|
|
|
pop_state();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '"') {
|
|
|
|
push_state(State::InDoubleQuotes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '\'') {
|
|
|
|
push_state(State::InSingleQuotes);
|
2019-05-07 02:12:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-06-08 10:08:37 +03:00
|
|
|
if (ch == ' ') {
|
|
|
|
if (m_token.is_empty()) {
|
|
|
|
// foo > bar
|
|
|
|
// ^ We are at this space, we want to ignore it but not leave the state.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
commit_token(Token::Special);
|
|
|
|
if (m_tokens.is_empty()) {
|
|
|
|
fprintf(stderr, "Syntax error: Redirection without a path\n");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
pop_state();
|
2019-05-07 02:12:08 +03:00
|
|
|
break;
|
2020-06-08 10:08:37 +03:00
|
|
|
}
|
2019-05-07 02:12:08 +03:00
|
|
|
m_token.append(ch);
|
|
|
|
break;
|
|
|
|
case State::InSingleQuotes:
|
|
|
|
if (ch == '\'') {
|
2020-04-30 03:56:16 +03:00
|
|
|
if (!in_state(State::InRedirectionPath))
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::SingleQuoted, AllowEmptyToken::Yes);
|
2020-04-30 03:56:16 +03:00
|
|
|
pop_state();
|
2019-05-07 02:12:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_token.append(ch);
|
|
|
|
break;
|
|
|
|
case State::InDoubleQuotes:
|
|
|
|
if (ch == '\"') {
|
2020-04-30 03:56:16 +03:00
|
|
|
if (!in_state(State::InRedirectionPath))
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::DoubleQuoted, AllowEmptyToken::Yes);
|
2020-04-30 03:56:16 +03:00
|
|
|
pop_state();
|
2019-05-07 02:12:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-09-14 12:36:09 +03:00
|
|
|
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;
|
|
|
|
}
|
2019-05-07 02:12:08 +03:00
|
|
|
m_token.append(ch);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
2020-04-30 03:56:16 +03:00
|
|
|
|
|
|
|
while (m_state_stack.size() > 1) {
|
2020-05-10 09:05:23 +03:00
|
|
|
if (state() == State::InDoubleQuotes) {
|
2020-05-29 17:28:35 +03:00
|
|
|
pop_state();
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::UnterminatedDoubleQuoted, AllowEmptyToken::Yes);
|
|
|
|
} else if (state() == State::InSingleQuotes) {
|
2020-05-29 17:28:35 +03:00
|
|
|
pop_state();
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::UnterminatedSingleQuoted, AllowEmptyToken::Yes);
|
|
|
|
} else {
|
|
|
|
commit_token(Token::Bare, AllowEmptyToken::No);
|
2020-05-29 17:28:35 +03:00
|
|
|
pop_state();
|
2020-05-10 09:05:23 +03:00
|
|
|
}
|
2020-04-30 03:56:16 +03:00
|
|
|
}
|
|
|
|
ASSERT(state() == State::Free);
|
|
|
|
|
2020-05-10 09:05:23 +03:00
|
|
|
commit_token(Token::Bare);
|
2019-05-07 02:12:08 +03:00
|
|
|
commit_subcommand();
|
2019-08-30 07:54:05 +03:00
|
|
|
commit_command();
|
2019-05-07 02:12:08 +03:00
|
|
|
|
|
|
|
if (!m_subcommands.is_empty()) {
|
|
|
|
for (auto& redirection : m_subcommands.last().redirections) {
|
|
|
|
if (redirection.type == Redirection::Pipe) {
|
|
|
|
fprintf(stderr, "Syntax error: Nothing after last pipe (|)\n");
|
2019-06-07 12:49:21 +03:00
|
|
|
return {};
|
2019-05-07 02:12:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 07:54:05 +03:00
|
|
|
return move(m_commands);
|
2019-05-07 02:12:08 +03:00
|
|
|
}
|