-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwash.cpp
More file actions
186 lines (176 loc) · 4.67 KB
/
wash.cpp
File metadata and controls
186 lines (176 loc) · 4.67 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
#include "wash.hpp"
#include <iostream>
#include "terminal_mode_interface.hpp"
void Wash::print_prompt() {
emulator.add_input(env["PS1"]);
}
void Wash::input_key(char key, bool ctrl, bool alt, bool shift, bool meta) {
std::cout << "key down!" << std::endl;
if (ctrl)
std::cout << "^";
if (alt)
std::cout << "<alt>";
if (shift)
std::cout << "<shift>";
if (meta)
std::cout << "<meta>";
std::cout << key << std::endl;
bool executing = false;
{
std::unique_lock<std::mutex> lock(buffer_mutex);
executing = is_executing;
}
if (!executing) {
if (key == '\b') {
if (entered_chars.size() != 0) {
entered_chars.pop_back();
emulator.add_char('\b');
}
} else if (key == '\n') {
std::cout << "entered command: " << entered_chars << std::endl;
emulator.add_char('\n');
update_terminal(emulator);
// spin up a thread for command
auto command_handler = [&]() {
std::string command = entered_chars;
{
std::unique_lock<std::mutex> lock(buffer_mutex);
is_executing = true;
entered_chars = "";
}
execute_command(command, emulator);
print_prompt();
update_by_proxy(emulator);
{
std::unique_lock<std::mutex> lock(buffer_mutex);
entered_chars = {begin(buffer), end(buffer)};
emulator.add_input(entered_chars);
update_by_proxy(emulator);
buffer.clear();
is_executing = false;
}
};
std::thread t(command_handler);
t.detach();
} else {
if (ctrl) {
if (key >= 'a' && key <= 'z') {
emulator.add_char('^');
emulator.add_char(key);
if (key == 'c') {
// control C current command
entered_chars = "";
emulator.add_char('\n');
print_prompt();
update_terminal(emulator);
}
}
} else {
entered_chars.push_back(key);
emulator.add_char(key);
}
}
} else {
std::unique_lock<std::mutex> lock(buffer_mutex);
if (ctrl) {
if (key >= 'a' && key <= 'z') {
emulator.add_char('^');
emulator.add_char(key);
buffer.push_back(key - 'a' + 1);
buffer_cv.notify_all();
}
} else {
emulator.add_char(key);
update_terminal(emulator);
buffer.push_back(key);
buffer_cv.notify_all();
}
}
std::cout << "end input key" << std::endl;
update_terminal(emulator);
}
std::set<char> parse_options(int argc, char *const argv[], const char *short_opts, struct option long_opts[]) {
std::set<char> result;
while (1) {
int option_index = 0;
int c = getopt_long(argc, argv, short_opts, long_opts, &option_index);
if (c == -1)
break;
result.insert(static_cast<char>(c));
}
return result;
}
std::string parse_escaped_string(const std::string &escaped) {
std::string result;
result.reserve(escaped.size());
for (size_t i = 1; i < size(escaped);) {
if (escaped[i] == '\\') {
// \n
// \t
// \\
// \'
// \"
++i;
if (i < escaped.size()) {
switch (escaped[i]) {
case 'n':
result += '\n';
break;
case 't':
result += '\t';
break;
case 'e':
result += '\e';
break;
case '\\':
result += '\\';
break;
case '\'':
result += '\'';
break;
case '"':
result += '\"';
break;
case 'x': {
if (i + 2 >= escaped.size())
return result;
char c = 0;
if (escaped[i + 1] >= '0' && escaped[i + 1] <= '9') {
c += (escaped[i + 1] - '0') << 4;
} else if (escaped[i + 1] % 32 <= 6 && escaped[i + 1] >= 'A') {
c += ((escaped[i + 1] - 'A') % 32) << 4;
}
if (escaped[i + 2] >= '0' && escaped[i + 2] <= '9') {
c += (escaped[i + 2] - '0');
} else if (escaped[i + 2] % 32 <= 6 && escaped[i + 2] >= 'A') {
c += ((escaped[i + 2] - 'A') % 32);
}
result += c;
i += 3;
} break;
case '0':
// octal
{
char c = 0;
int num_digits = 0;
++i;
while (i < escaped.size() && escaped[i] >= '0' && escaped[i] <= '7' && num_digits < 3) {
c <<= 3;
c |= (escaped[i++] - '0');
++num_digits;
}
result += c;
}
break;
default:
break;
}
} else {
result += '\\';
}
} else {
result += escaped[i++];
}
}
return result;
}