-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathparser.cpp
191 lines (164 loc) · 4.05 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
This file is part of chAT.
Copyright (C) 2022-2023 Reimu NotMoe <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "chAT.hpp"
namespace SudoMaker::chAT {
ATParser::ATParser() {
reset();
}
void ATParser::reset() {
malformed = false;
state = ParseState::Keyword;
cmd_mode = CommandMode::Run;
keyword_count = 0;
command.clear();
args.clear();
args_quote = false;
args_escape_count = 0;
#ifdef CHAT_STRICT_CRLF
last_data[0] = 0;
last_data[1] = 0;
#endif
bytes_parsed = 0;
}
void ATParser::show() {
const char *mode_str = nullptr;
switch (cmd_mode) {
case CommandMode::Run:
mode_str = "run";
break;
case CommandMode::Write:
mode_str = "write";
break;
case CommandMode::Read:
mode_str = "read";
break;
case CommandMode::Test:
mode_str = "test";
break;
}
printf("[parser] malformed: %s, mode: %s, args_count: %zu\n", malformed ? "true" : "false", mode_str, args.size());
if (!command.empty()) {
printf("[parser] command: %s\n", command.c_str());
}
if (!args.empty()) {
printf("[parser] args: ");
for (size_t i=0; i<args.size(); i++) {
printf("[%zu]=%s ", i, args[i].c_str());
}
printf("\n");
}
}
size_t ATParser::parse(const uint8_t *buf, size_t len) {
static const uint8_t keyword[2] = {'A', 'T'};
for (size_t i = 0; i < len; i++) {
uint8_t c = buf[i];
#ifdef CHAT_STRICT_CRLF
last_data[0] = last_data[1];
last_data[1] = c;
if (last_data[0] == '\r' && last_data[1] == '\n') {
#else
if (c == '\n') {
#endif
if (state == ParseState::Malformed) {
malformed = true;
}
state = ParseState::Done;
bytes_parsed += (i+1);
return i+1;
}
switch (state) {
case ParseState::Keyword: {
uint8_t kc = keyword[keyword_count];
if (c == kc) {
keyword_count++;
} else {
state = ParseState::Malformed;
}
if (keyword_count == sizeof(keyword)) {
state = ParseState::Command;
}
break;
}
case ParseState::Command: {
switch (c) {
case '?':
if (cmd_mode == CommandMode::Run) {
cmd_mode = CommandMode::Read;
} else if (cmd_mode == CommandMode::Write) {
cmd_mode = CommandMode::Test;
} else {
state = ParseState::Malformed;
}
break;
case '=':
if (cmd_mode == CommandMode::Run) {
cmd_mode = CommandMode::Write;
} else {
state = ParseState::Malformed;
}
break;
case '\r':
case '\n':
break;
default:
if (cmd_mode == CommandMode::Run) {
command.push_back((char) c);
} else {
args.emplace_back();
state = ParseState::Argument;
i--;
}
break;
}
break;
}
case ParseState::Argument: {
auto &cur_arg = args.back();
if (args_escape_count) {
cur_arg.push_back((char)c);
args_escape_count--;
} else {
switch (c) {
case '\\':
args_escape_count++;
break;
case '"':
args_quote = !args_quote;
break;
case ',':
if (args_quote) {
cur_arg.push_back(',');
} else {
args.emplace_back();
}
break;
case '\r':
case '\n':
break;
default:
cur_arg.push_back((char)c);
break;
}
}
break;
}
default:
break;
}
}
bytes_parsed += len;
return len;
}
}