-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroshell.c
321 lines (300 loc) · 11.7 KB
/
microshell.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* @file microshell.c
* @author Shinichiro Nakamura (CuBeatSystems)
* ===============================================================
* MicroShell (Version 0.0.2)
* Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
* ===============================================================
* The MIT License : https://opensource.org/licenses/MIT
*
* Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "microshell.h"
#define PRINT_ACTION_ENABLED (0)
#define PRINT_CODE_ENABLED (0)
#if PRINT_ACTION_ENABLED
static void uart_puts(MICROSHELL *handle, char *str)
{
while (*str) {
handle->uart_putc(*str++);
}
}
static void print_action(MICROSHELL *handle, MSCORE_ACTION action)
{
switch (action) {
case MSCORE_ACTION_IGNORE: uart_puts(handle, "[IGNORE]"); break;
case MSCORE_ACTION_DISPLAYABLE: uart_puts(handle, "[DISPLAYABLE]"); break;
case MSCORE_ACTION_INSERT: uart_puts(handle, "[INSERT]"); break;
case MSCORE_ACTION_ENTER: uart_puts(handle, "[ENTER]"); break;
case MSCORE_ACTION_TAB: uart_puts(handle, "[TAB]"); break;
case MSCORE_ACTION_BS: uart_puts(handle, "[BS]"); break;
case MSCORE_ACTION_DEL: uart_puts(handle, "[DEL]"); break;
case MSCORE_ACTION_CTRL_A: uart_puts(handle, "[Ctrl+A]"); break;
case MSCORE_ACTION_CTRL_B: uart_puts(handle, "[Ctrl+B]"); break;
case MSCORE_ACTION_CTRL_C: uart_puts(handle, "[Ctrl+C]"); break;
case MSCORE_ACTION_CTRL_D: uart_puts(handle, "[Ctrl+D]"); break;
case MSCORE_ACTION_CTRL_E: uart_puts(handle, "[Ctrl+E]"); break;
case MSCORE_ACTION_CTRL_F: uart_puts(handle, "[Ctrl+F]"); break;
case MSCORE_ACTION_CTRL_G: uart_puts(handle, "[Ctrl+G]"); break;
case MSCORE_ACTION_CTRL_H: uart_puts(handle, "[Ctrl+H]"); break;
case MSCORE_ACTION_CTRL_I: uart_puts(handle, "[Ctrl+I]"); break;
case MSCORE_ACTION_CTRL_J: uart_puts(handle, "[Ctrl+J]"); break;
case MSCORE_ACTION_CTRL_K: uart_puts(handle, "[Ctrl+K]"); break;
case MSCORE_ACTION_CTRL_L: uart_puts(handle, "[Ctrl+L]"); break;
case MSCORE_ACTION_CTRL_M: uart_puts(handle, "[Ctrl+M]"); break;
case MSCORE_ACTION_CTRL_N: uart_puts(handle, "[Ctrl+N]"); break;
case MSCORE_ACTION_CTRL_O: uart_puts(handle, "[Ctrl+O]"); break;
case MSCORE_ACTION_CTRL_P: uart_puts(handle, "[Ctrl+P]"); break;
case MSCORE_ACTION_CTRL_Q: uart_puts(handle, "[Ctrl+Q]"); break;
case MSCORE_ACTION_CTRL_R: uart_puts(handle, "[Ctrl+R]"); break;
case MSCORE_ACTION_CTRL_S: uart_puts(handle, "[Ctrl+S]"); break;
case MSCORE_ACTION_CTRL_T: uart_puts(handle, "[Ctrl+T]"); break;
case MSCORE_ACTION_CTRL_U: uart_puts(handle, "[Ctrl+U]"); break;
case MSCORE_ACTION_CTRL_V: uart_puts(handle, "[Ctrl+V]"); break;
case MSCORE_ACTION_CTRL_W: uart_puts(handle, "[Ctrl+W]"); break;
case MSCORE_ACTION_CTRL_X: uart_puts(handle, "[Ctrl+X]"); break;
case MSCORE_ACTION_CTRL_Y: uart_puts(handle, "[Ctrl+Y]"); break;
case MSCORE_ACTION_CTRL_Z: uart_puts(handle, "[Ctrl+Z]"); break;
case MSCORE_ACTION_F1: uart_puts(handle, "[F1]"); break;
case MSCORE_ACTION_F2: uart_puts(handle, "[F2]"); break;
case MSCORE_ACTION_F3: uart_puts(handle, "[F3]"); break;
case MSCORE_ACTION_F4: uart_puts(handle, "[F4]"); break;
case MSCORE_ACTION_F5: uart_puts(handle, "[F5]"); break;
case MSCORE_ACTION_F6: uart_puts(handle, "[F6]"); break;
case MSCORE_ACTION_F7: uart_puts(handle, "[F7]"); break;
case MSCORE_ACTION_F8: uart_puts(handle, "[F8]"); break;
case MSCORE_ACTION_F9: uart_puts(handle, "[F9]"); break;
case MSCORE_ACTION_F10: uart_puts(handle, "[F10]"); break;
case MSCORE_ACTION_F11: uart_puts(handle, "[F11]"); break;
case MSCORE_ACTION_F12: uart_puts(handle, "[F12]"); break;
case MSCORE_ACTION_ARROW_LEFT: uart_puts(handle, "[<]"); break;
case MSCORE_ACTION_ARROW_RIGHT: uart_puts(handle, "[>]"); break;
case MSCORE_ACTION_ARROW_UP: uart_puts(handle, "[UP]"); break;
case MSCORE_ACTION_ARROW_DOWN: uart_puts(handle, "[DOWN]"); break;
case MSCORE_ACTION_HOME: uart_puts(handle, "[HOME]"); break;
case MSCORE_ACTION_END: uart_puts(handle, "[END]"); break;
case MSCORE_ACTION_PAGE_UP: uart_puts(handle, "[PAGE UP]"); break;
case MSCORE_ACTION_PAGE_DOWN: uart_puts(handle, "[PAGE DOWN]"); break;
}
}
#endif
#if PRINT_CODE_ENABLED
static void print_code(MICROSHELL *handle, char c)
{
static const char *txt = "0123456789ABCDEF";
handle->uart_putc('[');
handle->uart_putc('0');
handle->uart_putc('x');
handle->uart_putc(txt[(((unsigned char)c) >> 4) & 0x0F]);
handle->uart_putc(txt[(((unsigned char)c) >> 0) & 0x0F]);
handle->uart_putc(']');
}
#endif
static char *copy_forward(char *buf, int ofs_src, int ofs_des, int siz)
{
int i;
char *p_src = buf + ofs_src;
char *p_des = buf + ofs_des;
for (i = 0; i < siz; i++) {
*p_des++ = *p_src++;
}
return buf;
}
static char *copy_backward(char *buf, int ofs_src, int ofs_des, int siz)
{
int i;
char *p_src = buf + ofs_src + siz;
char *p_des = buf + ofs_des + siz;
for (i = 0; i < siz; i++) {
*p_des-- = *p_src--;
}
return buf;
}
static char *clean_buffer(char *buf, int siz)
{
int i;
char *p = buf;
for (i = 0; i < siz; i++) {
*p++ = 0;
}
return buf;
}
static int print_buffer(MICROSHELL *handle, char *buf, int ofs)
{
int cnt = 0;
char *p = buf + ofs;
while (*p) {
handle->uart_putc(*p++);
cnt++;
}
return cnt;
}
static int print_char(MICROSHELL *handle, char c)
{
handle->uart_putc(c);
return 1;
}
static void print_return(MICROSHELL *handle)
{
handle->uart_putc('\r');
handle->uart_putc('\n');
}
static void cursor_left(MICROSHELL *handle, int n)
{
int i;
for (i = 0; i < n; i++) {
handle->uart_putc('\x1B');
handle->uart_putc('[');
handle->uart_putc('D');
}
}
static void cursor_right(MICROSHELL *handle, int n)
{
int i;
for (i = 0; i < n; i++) {
handle->uart_putc('\x1B');
handle->uart_putc('[');
handle->uart_putc('C');
}
}
void microshell_init(MICROSHELL *handle, MICROSHELL_UART_PUTC uart_putc, MICROSHELL_UART_GETC uart_getc, MICROSHELL_ACTION_HOOK action_hook)
{
mscore_init(&(handle->mscore));
handle->uart_putc = uart_putc;
handle->uart_getc = uart_getc;
handle->action_hook = action_hook;
}
char *microshell_getline(MICROSHELL *handle, char *buf, int siz)
{
char *ptr = buf;
int pos = 0;
int len = 0;
clean_buffer(buf, siz);
while (1) {
char c = handle->uart_getc();
MSCORE_ACTION action = mscore_push(&(handle->mscore), c);
if (handle->action_hook) {
handle->action_hook(action);
}
#if PRINT_ACTION_ENABLED
print_action(handle, action);
#endif
#if PRINT_CODE_ENABLED
print_code(handle, c);
#endif
switch (action) {
case MSCORE_ACTION_DISPLAYABLE:
{
int n = 0;
copy_backward(buf, pos - 1, pos - 0, len - pos + 1);
handle->uart_putc(c);
*ptr++ = c;
pos++;
len++;
n += print_buffer(handle, buf, pos);
cursor_left(handle, n);
if (len >= siz - 1) {
print_return(handle);
goto end;
}
}
break;
case MSCORE_ACTION_ENTER:
case MSCORE_ACTION_CTRL_J:
print_return(handle);
goto end;
break;
case MSCORE_ACTION_CTRL_C:
clean_buffer(buf, siz);
print_char(handle, '^');
print_char(handle, 'C');
print_return(handle);
goto end;
break;
case MSCORE_ACTION_BS:
case MSCORE_ACTION_CTRL_H:
if (pos > 0) {
int n = 0;
copy_forward(buf, pos, pos - 1, len - pos + 1);
ptr--;
pos--;
len--;
cursor_left(handle, 1);
n += print_buffer(handle, buf, pos);
n += print_char(handle, ' ');
cursor_left(handle, n);
}
break;
case MSCORE_ACTION_DEL:
case MSCORE_ACTION_CTRL_D:
if (len > 0) {
int n = 0;
copy_forward(buf, pos + 1, pos + 0, len - pos + 1);
len--;
n += print_buffer(handle, buf, pos);
n += print_char(handle, ' ');
cursor_left(handle, n);
}
break;
case MSCORE_ACTION_ARROW_LEFT:
case MSCORE_ACTION_CTRL_B:
if (pos > 0) {
ptr--;
pos--;
cursor_left(handle, 1);
}
break;
case MSCORE_ACTION_ARROW_RIGHT:
case MSCORE_ACTION_CTRL_F:
if (pos < len) {
ptr++;
pos++;
cursor_right(handle, 1);
}
break;
case MSCORE_ACTION_HOME:
case MSCORE_ACTION_CTRL_A:
if (pos > 0) {
int n = pos;
ptr -= n;
pos = 0;
cursor_left(handle, n);
}
break;
case MSCORE_ACTION_END:
case MSCORE_ACTION_CTRL_E:
if (pos < len) {
int n = len - pos;
ptr += n;
pos = len;
cursor_right(handle, n);
}
break;
default:
break;
}
}
end:
return buf;
}