summaryrefslogtreecommitdiff
path: root/Shell/Parser.h
blob: 68687bc6d7604d3fe6c86335745f22d5441c5c26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once

#include <AK/String.h>
#include <AK/Vector.h>

struct Redirection {
    enum Type {
        Pipe,
        FileWrite,
        FileWriteAppend,
        FileRead,
    };
    Type type;
    int fd { -1 };
    int rewire_fd { -1 };
    String path {};
};

struct Rewiring {
    int fd { -1 };
    int rewire_fd { -1 };
};

struct Subcommand {
    Vector<String> args;
    Vector<Redirection> redirections;
    Vector<Rewiring> rewirings;
};

struct Command {
    Vector<Subcommand> subcommands;
};

class Parser {
public:
    explicit Parser(const String& input)
        : m_input(input)
    {
    }

    Vector<Command> parse();

private:
    void commit_token();
    void commit_subcommand();
    void commit_command();
    void do_pipe();
    void begin_redirect_read(int fd);
    void begin_redirect_write(int fd);

    enum State {
        Free,
        InSingleQuotes,
        InDoubleQuotes,
        InWriteAppendOrRedirectionPath,
        InRedirectionPath,
    };
    State m_state { Free };
    String m_input;

    Vector<Command> m_commands;
    Vector<Subcommand> m_subcommands;
    Vector<String> m_tokens;
    Vector<Redirection> m_redirections;
    Vector<char> m_token;
};