|
@@ -13,6 +13,7 @@
|
|
#include <sys/utsname.h>
|
|
#include <sys/utsname.h>
|
|
#include <AK/FileSystemPath.h>
|
|
#include <AK/FileSystemPath.h>
|
|
#include <LibCore/CElapsedTimer.h>
|
|
#include <LibCore/CElapsedTimer.h>
|
|
|
|
+#include "Parser.h"
|
|
|
|
|
|
//#define SH_DEBUG
|
|
//#define SH_DEBUG
|
|
|
|
|
|
@@ -144,184 +145,6 @@ static bool handle_builtin(int argc, char** argv, int& retval)
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
-struct Redirection {
|
|
|
|
- enum Type { Pipe, FileWrite, FileRead, Rewire };
|
|
|
|
- Type type;
|
|
|
|
- int fd { -1 };
|
|
|
|
- int rewire_fd { -1 };
|
|
|
|
- String path { };
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-struct Subcommand {
|
|
|
|
- Vector<String> args;
|
|
|
|
- Vector<Redirection> redirections;
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-class Parser {
|
|
|
|
-public:
|
|
|
|
- explicit Parser(const String& input) : m_input(input) { }
|
|
|
|
-
|
|
|
|
- Vector<Subcommand> parse();
|
|
|
|
-
|
|
|
|
-private:
|
|
|
|
- void commit_token();
|
|
|
|
- void commit_subcommand();
|
|
|
|
- void do_pipe();
|
|
|
|
- void begin_redirect_read(int fd);
|
|
|
|
- void begin_redirect_write(int fd);
|
|
|
|
-
|
|
|
|
- enum State {
|
|
|
|
- Free,
|
|
|
|
- InSingleQuotes,
|
|
|
|
- InDoubleQuotes,
|
|
|
|
- InRedirectionPath,
|
|
|
|
- };
|
|
|
|
- State m_state { Free };
|
|
|
|
- String m_input;
|
|
|
|
-
|
|
|
|
- Vector<Subcommand> m_subcommands;
|
|
|
|
- Vector<String> m_tokens;
|
|
|
|
- Vector<Redirection> m_redirections;
|
|
|
|
- Vector<char> m_token;
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-void Parser::commit_token()
|
|
|
|
-{
|
|
|
|
- if (m_token.is_empty())
|
|
|
|
- return;
|
|
|
|
- if (m_state == InRedirectionPath) {
|
|
|
|
- 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;
|
|
|
|
- m_subcommands.append({ move(m_tokens), move(m_redirections) });
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-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 });
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-Vector<Subcommand> Parser::parse()
|
|
|
|
-{
|
|
|
|
- for (int i = 0; i < m_input.length(); ++i) {
|
|
|
|
- char ch = m_input.characters()[i];
|
|
|
|
- switch (m_state) {
|
|
|
|
- case State::Free:
|
|
|
|
- if (ch == ' ') {
|
|
|
|
- commit_token();
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- if (ch == '|') {
|
|
|
|
- commit_token();
|
|
|
|
- if (m_tokens.is_empty()) {
|
|
|
|
- fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
|
|
|
|
- return { };
|
|
|
|
- }
|
|
|
|
- do_pipe();
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- if (ch == '>') {
|
|
|
|
- commit_token();
|
|
|
|
- begin_redirect_write(STDOUT_FILENO);
|
|
|
|
- m_state = State::InRedirectionPath;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- if (ch == '<') {
|
|
|
|
- commit_token();
|
|
|
|
- begin_redirect_read(STDIN_FILENO);
|
|
|
|
- m_state = State::InRedirectionPath;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- if (ch == '\'') {
|
|
|
|
- m_state = State::InSingleQuotes;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- if (ch == '\"') {
|
|
|
|
- m_state = State::InDoubleQuotes;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- m_token.append(ch);
|
|
|
|
- break;
|
|
|
|
- case State::InRedirectionPath:
|
|
|
|
- if (ch == '<') {
|
|
|
|
- commit_token();
|
|
|
|
- begin_redirect_read(STDIN_FILENO);
|
|
|
|
- m_state = State::InRedirectionPath;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- if (ch == '>') {
|
|
|
|
- commit_token();
|
|
|
|
- begin_redirect_read(STDOUT_FILENO);
|
|
|
|
- m_state = State::InRedirectionPath;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- if (ch == '|') {
|
|
|
|
- commit_token();
|
|
|
|
- if (m_tokens.is_empty()) {
|
|
|
|
- fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
|
|
|
|
- return { };
|
|
|
|
- }
|
|
|
|
- do_pipe();
|
|
|
|
- m_state = State::Free;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- if (ch == ' ')
|
|
|
|
- break;
|
|
|
|
- m_token.append(ch);
|
|
|
|
- break;
|
|
|
|
- case State::InSingleQuotes:
|
|
|
|
- if (ch == '\'') {
|
|
|
|
- commit_token();
|
|
|
|
- m_state = State::Free;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- m_token.append(ch);
|
|
|
|
- break;
|
|
|
|
- case State::InDoubleQuotes:
|
|
|
|
- if (ch == '\"') {
|
|
|
|
- commit_token();
|
|
|
|
- m_state = State::Free;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- m_token.append(ch);
|
|
|
|
- break;
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
- commit_token();
|
|
|
|
- commit_subcommand();
|
|
|
|
-
|
|
|
|
- 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");
|
|
|
|
- return { };
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return move(m_subcommands);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
class FileDescriptorCollector {
|
|
class FileDescriptorCollector {
|
|
public:
|
|
public:
|
|
FileDescriptorCollector() { }
|
|
FileDescriptorCollector() { }
|