2020-01-18 08:38:21 +00: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-06 23:12:08 +00:00
|
|
|
#include "Parser.h"
|
2020-04-30 00:56:16 +00:00
|
|
|
#include <ctype.h>
|
2019-05-06 23:12:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-01-25 11:16:45 +00:00
|
|
|
void Parser::commit_token(AllowEmptyToken allow_empty)
|
2019-05-06 23:12:08 +00:00
|
|
|
{
|
2020-01-25 11:16:45 +00:00
|
|
|
if (allow_empty == AllowEmptyToken::No && m_token.is_empty())
|
2019-05-06 23:12:08 +00:00
|
|
|
return;
|
2020-04-30 00:56:16 +00:00
|
|
|
if (state() == InRedirectionPath) {
|
2019-05-06 23:12:08 +00:00
|
|
|
m_redirections.last().path = String::copy(m_token);
|
|
|
|
m_token.clear_with_capacity();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_tokens.append(String::copy(m_token));
|
|
|
|
m_token.clear_with_capacity();
|
|
|
|
};
|
|
|
|
|
|
|
|
void Parser::commit_subcommand()
|
|
|
|
{
|
|
|
|
if (m_tokens.is_empty())
|
|
|
|
return;
|
2019-06-04 18:36:08 +00:00
|
|
|
m_subcommands.append({ move(m_tokens), move(m_redirections), {} });
|
2019-05-06 23:12:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
void Parser::commit_command()
|
|
|
|
{
|
|
|
|
if (m_subcommands.is_empty())
|
|
|
|
return;
|
|
|
|
m_commands.append({ move(m_subcommands) });
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:12:08 +00: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 00:56:16 +00:00
|
|
|
bool Parser::in_state(State state) const
|
|
|
|
{
|
|
|
|
for (auto s : m_state_stack)
|
|
|
|
if (s == state)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
Vector<Command> Parser::parse()
|
2019-05-06 23:12:08 +00:00
|
|
|
{
|
2019-12-09 16:45:40 +00:00
|
|
|
for (size_t i = 0; i < m_input.length(); ++i) {
|
2019-05-06 23:12:08 +00:00
|
|
|
char ch = m_input.characters()[i];
|
2020-04-30 00:56:16 +00:00
|
|
|
switch (state()) {
|
2019-05-06 23:12:08 +00:00
|
|
|
case State::Free:
|
|
|
|
if (ch == ' ') {
|
|
|
|
commit_token();
|
|
|
|
break;
|
|
|
|
}
|
2019-08-30 04:54:05 +00:00
|
|
|
if (ch == ';') {
|
|
|
|
commit_token();
|
|
|
|
commit_subcommand();
|
|
|
|
commit_command();
|
|
|
|
break;
|
|
|
|
}
|
2019-05-06 23:12:08 +00:00
|
|
|
if (ch == '|') {
|
|
|
|
commit_token();
|
|
|
|
if (m_tokens.is_empty()) {
|
|
|
|
fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
|
2019-06-07 09:49:21 +00:00
|
|
|
return {};
|
2019-05-06 23:12:08 +00:00
|
|
|
}
|
|
|
|
do_pipe();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '>') {
|
|
|
|
commit_token();
|
|
|
|
begin_redirect_write(STDOUT_FILENO);
|
2019-05-25 22:38:11 +00:00
|
|
|
|
|
|
|
// Search for another > for append.
|
2020-04-30 00:56:16 +00:00
|
|
|
push_state(State::InWriteAppendOrRedirectionPath);
|
2019-05-06 23:12:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '<') {
|
|
|
|
commit_token();
|
|
|
|
begin_redirect_read(STDIN_FILENO);
|
2020-04-30 00:56:16 +00:00
|
|
|
push_state(State::InRedirectionPath);
|
2019-05-06 23:12:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-14 09:36:09 +00: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-06 23:12:08 +00:00
|
|
|
if (ch == '\'') {
|
2020-04-30 00:56:16 +00:00
|
|
|
push_state(State::InSingleQuotes);
|
2019-05-06 23:12:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '\"') {
|
2020-04-30 00:56:16 +00:00
|
|
|
push_state(State::InDoubleQuotes);
|
2019-05-06 23:12:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-04-30 00:56:16 +00:00
|
|
|
|
2019-12-04 12:31:53 +00:00
|
|
|
// redirection from zsh-style multi-digit fd, such as {10}>file
|
|
|
|
if (ch == '{') {
|
|
|
|
bool is_multi_fd_redirection = false;
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t redir_end = i + 1;
|
2019-12-04 12:31:53 +00: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) {
|
|
|
|
commit_token();
|
|
|
|
|
|
|
|
int fd = atoi(&m_input.characters()[i + 1]);
|
|
|
|
|
|
|
|
if (m_input.characters()[redir_end] == '>') {
|
|
|
|
begin_redirect_write(fd);
|
|
|
|
// Search for another > for append.
|
2020-04-30 00:56:16 +00:00
|
|
|
push_state(State::InWriteAppendOrRedirectionPath);
|
2019-12-04 12:31:53 +00:00
|
|
|
}
|
|
|
|
if (m_input.characters()[redir_end] == '<') {
|
|
|
|
begin_redirect_read(fd);
|
2020-04-30 00:56:16 +00:00
|
|
|
push_state(State::InRedirectionPath);
|
2019-12-04 12:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i = redir_end;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 16:38:32 +00:00
|
|
|
if (isdigit(ch)) {
|
|
|
|
if (i != m_input.length() - 1) {
|
|
|
|
char next_ch = m_input.characters()[i + 1];
|
|
|
|
if (next_ch == '>') {
|
|
|
|
commit_token();
|
|
|
|
begin_redirect_write(ch - '0');
|
|
|
|
++i;
|
|
|
|
|
|
|
|
// Search for another > for append.
|
2020-04-30 00:56:16 +00:00
|
|
|
push_state(State::InWriteAppendOrRedirectionPath);
|
2019-12-03 16:38:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (next_ch == '<') {
|
|
|
|
commit_token();
|
|
|
|
begin_redirect_read(ch - '0');
|
|
|
|
++i;
|
|
|
|
|
2020-04-30 00:56:16 +00:00
|
|
|
push_state(State::InRedirectionPath);
|
2019-12-03 16:38:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-06 23:12:08 +00:00
|
|
|
m_token.append(ch);
|
|
|
|
break;
|
2019-05-25 22:38:11 +00:00
|
|
|
case State::InWriteAppendOrRedirectionPath:
|
|
|
|
if (ch == '>') {
|
|
|
|
commit_token();
|
2020-04-30 00:56:16 +00:00
|
|
|
pop_state();
|
|
|
|
push_state(State::InRedirectionPath);
|
2019-05-25 22:38:11 +00:00
|
|
|
ASSERT(m_redirections.size());
|
|
|
|
m_redirections[m_redirections.size() - 1].type = Redirection::FileWriteAppend;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not another > means that it's probably a path.
|
2020-04-30 00:56:16 +00:00
|
|
|
pop_state();
|
|
|
|
push_state(InRedirectionPath);
|
2019-05-25 22:38:11 +00:00
|
|
|
[[fallthrough]];
|
2019-05-06 23:12:08 +00:00
|
|
|
case State::InRedirectionPath:
|
|
|
|
if (ch == '<') {
|
|
|
|
commit_token();
|
|
|
|
begin_redirect_read(STDIN_FILENO);
|
2020-04-30 00:56:16 +00:00
|
|
|
pop_state();
|
|
|
|
push_state(State::InRedirectionPath);
|
2019-05-06 23:12:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '>') {
|
|
|
|
commit_token();
|
|
|
|
begin_redirect_read(STDOUT_FILENO);
|
2020-04-30 00:56:16 +00:00
|
|
|
pop_state();
|
|
|
|
push_state(State::InRedirectionPath);
|
2019-05-06 23:12:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '|') {
|
|
|
|
commit_token();
|
|
|
|
if (m_tokens.is_empty()) {
|
|
|
|
fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
|
2019-06-07 09:49:21 +00:00
|
|
|
return {};
|
2019-05-06 23:12:08 +00:00
|
|
|
}
|
|
|
|
do_pipe();
|
2020-04-30 00:56:16 +00:00
|
|
|
pop_state();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '"') {
|
|
|
|
push_state(State::InDoubleQuotes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '\'') {
|
|
|
|
push_state(State::InSingleQuotes);
|
2019-05-06 23:12:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == ' ')
|
|
|
|
break;
|
|
|
|
m_token.append(ch);
|
|
|
|
break;
|
|
|
|
case State::InSingleQuotes:
|
|
|
|
if (ch == '\'') {
|
2020-04-30 00:56:16 +00:00
|
|
|
if (!in_state(State::InRedirectionPath))
|
|
|
|
commit_token(AllowEmptyToken::Yes);
|
|
|
|
pop_state();
|
2019-05-06 23:12:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_token.append(ch);
|
|
|
|
break;
|
|
|
|
case State::InDoubleQuotes:
|
|
|
|
if (ch == '\"') {
|
2020-04-30 00:56:16 +00:00
|
|
|
if (!in_state(State::InRedirectionPath))
|
|
|
|
commit_token(AllowEmptyToken::Yes);
|
|
|
|
pop_state();
|
2019-05-06 23:12:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-14 09:36:09 +00: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-06 23:12:08 +00:00
|
|
|
m_token.append(ch);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
2020-04-30 00:56:16 +00:00
|
|
|
|
|
|
|
while (m_state_stack.size() > 1) {
|
|
|
|
auto allow_empty = AllowEmptyToken::No;
|
|
|
|
if (state() == State::InDoubleQuotes || state() == State::InSingleQuotes)
|
|
|
|
allow_empty = AllowEmptyToken::Yes;
|
|
|
|
commit_token(allow_empty);
|
|
|
|
pop_state();
|
|
|
|
}
|
|
|
|
ASSERT(state() == State::Free);
|
|
|
|
|
2019-05-06 23:12:08 +00:00
|
|
|
commit_token();
|
|
|
|
commit_subcommand();
|
2019-08-30 04:54:05 +00:00
|
|
|
commit_command();
|
2019-05-06 23:12:08 +00: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 09:49:21 +00:00
|
|
|
return {};
|
2019-05-06 23:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
return move(m_commands);
|
2019-05-06 23:12:08 +00:00
|
|
|
}
|