-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.cpp
164 lines (143 loc) · 5.25 KB
/
parser.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (c) 2018, Yutaka Tsutano
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <sstream>
#include "parser.hpp"
namespace {
enum class shell_token_type {
text,
redirect_cin,
redirect_cout,
append_cout,
pipe,
logical_and,
logical_or,
semicolon
};
shell_token_type get_shell_token_type(const std::string& token)
{
switch (token.size()) {
case 1:
switch (token[0]) {
case '<':
return shell_token_type::redirect_cin;
case '>':
return shell_token_type::redirect_cout;
case '|':
return shell_token_type::pipe;
case ';':
return shell_token_type::semicolon;
}
case 2:
if (token == ">>") {
return shell_token_type::append_cout;
}
else if (token == "&&") {
return shell_token_type::logical_and;
}
else if (token == "||") {
return shell_token_type::logical_or;
}
}
return shell_token_type::text;
}
}
std::vector<shell_command> parse_command_string(const std::string& str)
{
std::vector<shell_command> commands(1);
enum class parser_state {
need_any_token,
need_new_command,
need_in_path,
need_out_path
} state = parser_state::need_new_command;
std::istringstream iss(str);
std::string token;
while (iss >> token) {
auto token_type = get_shell_token_type(token);
switch (state) {
case parser_state::need_any_token:
switch (token_type) {
case shell_token_type::text:
commands.back().args.push_back(token);
break;
case shell_token_type::redirect_cin:
if (commands.back().cin_mode == istream_mode::pipe) {
throw parsing_error("ambiguous input redirect");
}
commands.back().cin_mode = istream_mode::file;
state = parser_state::need_in_path;
break;
case shell_token_type::redirect_cout:
commands.back().cout_mode = ostream_mode::file;
state = parser_state::need_out_path;
break;
case shell_token_type::append_cout:
commands.back().cout_mode = ostream_mode::append;
state = parser_state::need_out_path;
break;
case shell_token_type::pipe:
if (commands.back().cout_mode != ostream_mode::term) {
throw parsing_error("ambiguous output redirect");
}
commands.back().cout_mode = ostream_mode::pipe;
commands.emplace_back();
commands.back().cin_mode = istream_mode::pipe;
state = parser_state::need_new_command;
break;
case shell_token_type::logical_and:
commands.back().next_mode = next_command_mode::on_success;
commands.emplace_back();
state = parser_state::need_new_command;
break;
case shell_token_type::logical_or:
commands.back().next_mode = next_command_mode::on_fail;
commands.emplace_back();
state = parser_state::need_new_command;
break;
case shell_token_type::semicolon:
commands.emplace_back();
state = parser_state::need_new_command;
break;
}
break;
case parser_state::need_new_command:
if (token_type != shell_token_type::text) {
throw parsing_error("expecting a command");
}
commands.back().cmd = token;
state = parser_state::need_any_token;
break;
case parser_state::need_in_path:
if (token_type != shell_token_type::text) {
throw parsing_error("expecting an input path");
}
commands.back().cin_file = token;
state = parser_state::need_any_token;
break;
case parser_state::need_out_path:
if (token_type != shell_token_type::text) {
throw parsing_error("expecting an output path");
}
commands.back().cout_file = token;
state = parser_state::need_any_token;
break;
}
}
if (commands.back().cmd == "") {
commands.pop_back();
}
return commands;
}