forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.cpp
More file actions
274 lines (251 loc) · 5.84 KB
/
terminal.cpp
File metadata and controls
274 lines (251 loc) · 5.84 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <kernel/terminal.hpp>
#include <net/inet4>
#include <vector>
#include <serial>
#include <cstdio>
Terminal::Terminal()
: iac(false), newline(false), subcmd(0)
{
add_basic_commands();
}
Terminal::Terminal(Connection_ptr csock)
: Terminal()
{
csock->on_read(1024, [this](auto buffer, size_t n) {
this->read((char*)buffer.get(), n);
});
on_write =
[csock] (const char* buffer, size_t len) {
csock->write(buffer, len);
};
on_exit =
[csock] {
csock->close();
};
// after setting up everything, show introduction
intro();
}
Terminal::Terminal(hw::Serial& serial)
: Terminal()
{
serial.on_data(
[this, &serial] (char c)
{
this->read(&c, 1);
if (c == CR) {
c = LF;
this->read(&c, 1);
}
else {
serial.write(c);
}
});
on_write =
[&serial] (const char* buffer, size_t len)
{
for (size_t i = 0; i < len; i++)
serial.write(buffer[i]);
};
on_exit =
[] {
// do nothing
};
// after setting up everything, show introduction
intro();
}
void Terminal::read(const char* buf, size_t len)
{
while (len) {
if (this->subcmd) {
// execute options
option(this->subcmd, (uint8_t) *buf);
this->subcmd = 0;
}
else if (this->iac) {
command(*(uint8_t*) buf);
this->iac = false;
}
else if (*buf == 13 && !newline) {
newline = true;
}
else if (*buf == 10 && newline) {
newline = false;
// parse message
run(buffer);
buffer.clear();
}
else if (*buf == 0) {
// NOP
}
else if ((uint8_t) *buf == 0xFF) {
// Interpret as Command
this->iac = true;
}
else {
buffer.append(buf, 1);
}
buf++; len--;
}
}
void Terminal::command(uint8_t cmd)
{
switch (cmd) {
case 247: // erase char
printf("CMD: Erase char\n");
break;
case 248: // erase line
printf("CMD: Erase line\n");
break;
case 250: // Begin
printf("CMD: Begin...\n");
break;
case 251: // Will USE
case 252: // Won't USE
case 253: // Start USE
case 254: // Stop USE
//printf("CMD: Command %d\n", cmd);
this->subcmd = cmd;
break;
case 255:
//printf("CMD: Command %d\n", cmd);
this->subcmd = cmd;
break;
default:
printf("CMD: Unknown command %d\n", cmd);
}
}
void Terminal::option(uint8_t option, uint8_t cmd)
{
(void) option;
switch (cmd) {
case 24: // terminal type
break;
default:
//printf("CMD: Unknown cmd %d for option %d\n", cmd, option);
break;
}
}
std::vector<std::string>
split(const std::string& text, std::string& command)
{
std::vector<std::string> retv;
size_t x = 0;
size_t p = 0;
// ignore empty messages
if (text.empty()) return retv;
// extract command
{
x = text.find(" ");
// early return for cmd-only msg
if (x == std::string::npos)
{
command = text;
return retv;
}
// command is substring
command = text.substr(0, x);
p = x+1;
}
// parse remainder
do {
x = text.find(" ", p+1);
size_t y = text.find(":", x+1); // find last param
if (y == x+1) {
// single argument
retv.push_back(text.substr(p, x-p));
// ending text argument
retv.push_back(text.substr(y+1));
break;
}
else if (x != std::string::npos) {
// single argument
retv.push_back(text.substr(p, x-p));
}
else {
// last argument
retv.push_back(text.substr(p));
}
p = x+1;
} while (x != std::string::npos);
return retv;
}
void Terminal::run(const std::string& cmd_string)
{
std::string cmd_name;
auto cmd_vec = split(cmd_string, cmd_name);
if (cmd_name.size())
{
printf("Terminal::run(): %s\n", cmd_name.c_str());
auto it = commands.find(cmd_name);
if (it != commands.end()) {
int retv = it->second.main(cmd_vec);
if (retv) write("%s returned: %d\r\n", cmd_name.c_str(), retv);
}
else {
write("No such command: '%s'\r\n", cmd_name.c_str());
}
}
prompt();
}
void Terminal::add_basic_commands()
{
// ?:
add("?", "List available commands",
[this] (const std::vector<std::string>&) -> int
{
for (auto cmd : this->commands)
{
write("%s \t-- %s\r\n", cmd.first.c_str(), cmd.second.desc.c_str());
}
return 0;
});
// exit:
add("exit", "Close the terminal",
[this] (const std::vector<std::string>&) -> int
{
this->on_exit();
return 0;
});
}
void Terminal::intro()
{
std::string banana =
R"baaa(
____ ___
| _ \ ___ _ _.' _ `.
_ | [_) )' _ `._ _ ___ ! \ | | (_) | _
|:;.| _ <| (_) | \ | |' _ `| \| | _ | .:;|
| `.[_) ) _ | \| | (_) | | | | |.',..|
':. `. /| | | | | _ | |\ | | |.' :;::'
!::, `-!_| | | |\ | | | | | \ !_!.' ':;!
!::; ":;:!.!.\_!_!_!.!-'-':;:'' '''!
';:' `::;::;' '' ., .
`: .,. `' .::... . .::;::;'
`..:;::;:.. ::;::;:;:;, :;::;'
"-:;::;:;: ':;::;:'' ;.-'
""`---...________...---'""
> Banana Terminal v1 <
)baaa";
write("%s", banana.c_str());
prompt();
}
void Terminal::prompt()
{
write("%s", "$ ");
}