forked from ofabel/mp-flipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upython_repl.c
394 lines (281 loc) · 10.4 KB
/
upython_repl.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <stdio.h>
#include <cli/cli.h>
#include <furi.h>
#include <genhdr/mpversion.h>
#include <mp_flipper_compiler.h>
#include <mp_flipper_repl.h>
#include "upython.h"
#define AUTOCOMPLETE_MANY_MATCHES (size_t)(-1)
#define HISTORY_SIZE 16
typedef struct {
FuriString** stack;
size_t pointer;
size_t size;
} mp_flipper_repl_history_t;
typedef struct {
mp_flipper_repl_history_t* history;
FuriString* line;
FuriString* code;
size_t cursor;
bool is_ps2;
} mp_flipper_repl_context_t;
static mp_flipper_repl_history_t* mp_flipper_repl_history_alloc() {
mp_flipper_repl_history_t* history = malloc(sizeof(mp_flipper_repl_history_t));
history->stack = malloc(HISTORY_SIZE * sizeof(FuriString*));
history->pointer = 0;
history->size = 1;
for(size_t i = 0; i < HISTORY_SIZE; i++) {
history->stack[i] = furi_string_alloc();
}
return history;
}
static void mp_flipper_repl_history_free(mp_flipper_repl_history_t* history) {
for(size_t i = 0; i < HISTORY_SIZE; i++) {
furi_string_free(history->stack[i]);
}
free(history);
}
static mp_flipper_repl_context_t* mp_flipper_repl_context_alloc() {
mp_flipper_repl_context_t* context = malloc(sizeof(mp_flipper_repl_context_t));
context->history = mp_flipper_repl_history_alloc();
context->code = furi_string_alloc();
context->line = furi_string_alloc();
context->cursor = 0;
context->is_ps2 = false;
return context;
}
static void mp_flipper_repl_context_free(mp_flipper_repl_context_t* context) {
mp_flipper_repl_history_free(context->history);
furi_string_free(context->code);
furi_string_free(context->line);
free(context);
}
static void print_full_psx(mp_flipper_repl_context_t* context) {
const char* psx = context->is_ps2 ? "... " : ">>> ";
printf("\e[2K\r%s%s", psx, furi_string_get_cstr(context->line));
fflush(stdout);
for(size_t i = context->cursor; i < furi_string_size(context->line); i++) {
printf("\e[D");
}
fflush(stdout);
}
inline static void handle_arrow_keys(char character, mp_flipper_repl_context_t* context) {
mp_flipper_repl_history_t* history = context->history;
do {
bool update_by_history = false;
// up arrow
if(character == 'A' && history->pointer == 0) {
furi_string_set(history->stack[0], context->line);
}
if(character == 'A' && history->pointer < history->size) {
history->pointer += (history->pointer + 1) == history->size ? 0 : 1;
update_by_history = true;
}
// down arrow
if(character == 'B' && history->pointer > 0) {
history->pointer--;
update_by_history = true;
}
if(update_by_history) {
furi_string_set(context->line, history->stack[history->pointer]);
context->cursor = furi_string_size(context->line);
break;
}
// right arrow
if(character == 'C' && context->cursor != furi_string_size(context->line)) {
context->cursor++;
break;
}
// left arrow
if(character == 'D' && context->cursor > 0) {
context->cursor--;
break;
}
} while(false);
print_full_psx(context);
}
inline static void handle_backspace(mp_flipper_repl_context_t* context) {
// skip backspace at begin of line
if(context->cursor == 0) {
return;
}
const char* line = furi_string_get_cstr(context->line);
size_t before = context->cursor - 1;
size_t after = furi_string_size(context->line) - context->cursor;
furi_string_printf(context->line, "%.*s%.*s", before, line, after, line + context->cursor);
context->cursor--;
printf("\e[D\e[1P");
fflush(stdout);
}
inline static bool is_indent_required(mp_flipper_repl_context_t* context) {
for(size_t i = 0; context->is_ps2 && i < context->cursor; i++) {
if(furi_string_get_char(context->line, i) != ' ') {
return false;
}
}
return context->is_ps2;
}
inline static void handle_autocomplete(mp_flipper_repl_context_t* context) {
// check if ps2 is active and just a tab character is required
if(is_indent_required(context)) {
furi_string_replace_at(context->line, context->cursor, 0, " ");
context->cursor += 4;
print_full_psx(context);
return;
}
const char* new_line = furi_string_get_cstr(context->line);
FuriString* orig_line = furi_string_alloc_printf("%s", new_line);
const char* orig_line_str = furi_string_get_cstr(orig_line);
char* completion = malloc(128 * sizeof(char));
mp_print_t* print = malloc(sizeof(mp_print_t));
print->data = mp_flipper_print_data_alloc();
print->print_strn = mp_flipper_print_strn;
size_t length = mp_flipper_repl_autocomplete(new_line, context->cursor, print, &completion);
do {
if(length == 0) {
break;
}
if(length != AUTOCOMPLETE_MANY_MATCHES) {
furi_string_printf(
context->line,
"%.*s%.*s%s",
context->cursor,
orig_line_str,
length,
completion,
orig_line_str + context->cursor);
context->cursor += length;
} else {
printf("%s", mp_flipper_print_get_data(print->data));
}
print_full_psx(context);
} while(false);
mp_flipper_print_data_free(print->data);
furi_string_free(orig_line);
free(completion);
free(print);
}
inline static void update_history(mp_flipper_repl_context_t* context) {
mp_flipper_repl_history_t* history = context->history;
if(!furi_string_empty(context->line) && !furi_string_equal(context->line, history->stack[1])) {
history->size += history->size == HISTORY_SIZE ? 0 : 1;
for(size_t i = history->size - 1; i > 1; i--) {
furi_string_set(history->stack[i], history->stack[i - 1]);
}
furi_string_set(history->stack[1], context->line);
}
furi_string_reset(history->stack[0]);
history->pointer = 0;
}
inline static bool continue_with_input(mp_flipper_repl_context_t* context) {
if(furi_string_empty(context->line)) {
return false;
}
if(!mp_flipper_repl_continue_with_input(furi_string_get_cstr(context->code))) {
return false;
}
return true;
}
void upython_repl_execute(Cli* cli) {
size_t stack;
const size_t heap_size = memmgr_get_free_heap() * 0.1;
const size_t stack_size = 2 * 1024;
uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
printf("MicroPython (%s, %s) on Flipper Zero\r\n", MICROPY_GIT_TAG, MICROPY_BUILD_DATE);
printf("Quit: Ctrl+D | Heap: %zu bytes | Stack: %zu bytes\r\n", heap_size, stack_size);
printf(" To do a reboot, press Left+Back for 5 seconds.\r\n");
printf("Docs: https://ofabel.github.io/mp-flipper\r\n");
mp_flipper_repl_context_t* context = mp_flipper_repl_context_alloc();
mp_flipper_set_root_module_path("/ext");
mp_flipper_init(heap, heap_size, stack_size, &stack);
char character = '\0';
uint8_t* buffer = malloc(sizeof(uint8_t));
bool exit = false;
// REPL loop
do {
furi_string_reset(context->code);
context->is_ps2 = false;
// scan line loop
do {
furi_string_reset(context->line);
context->cursor = 0;
print_full_psx(context);
// scan character loop
do {
character = cli_getc(cli);
// Ctrl + C
if(character == CliSymbolAsciiETX) {
context->cursor = 0;
furi_string_reset(context->line);
furi_string_reset(context->code);
printf("\r\nKeyboardInterrupt\r\n");
break;
}
// Ctrl + D
if(character == CliSymbolAsciiEOT) {
exit = true;
break;
}
// skip line feed
if(character == CliSymbolAsciiLF) {
continue;
}
// handle carriage return
if(character == CliSymbolAsciiCR) {
furi_string_push_back(context->code, '\n');
furi_string_cat(context->code, context->line);
furi_string_trim(context->code);
cli_nl(cli);
break;
}
// handle arrow keys
if(character >= 0x18 && character <= 0x1B) {
character = cli_getc(cli);
character = cli_getc(cli);
handle_arrow_keys(character, context);
continue;
}
// handle tab, do autocompletion
if(character == CliSymbolAsciiTab) {
handle_autocomplete(context);
continue;
}
// handle backspace
if(character == CliSymbolAsciiBackspace || character == CliSymbolAsciiDel) {
handle_backspace(context);
continue;
}
// append at end
if(context->cursor == furi_string_size(context->line)) {
buffer[0] = character;
cli_write(cli, (const uint8_t*)buffer, 1);
furi_string_push_back(context->line, character);
context->cursor++;
continue;
}
// insert between
if(context->cursor < furi_string_size(context->line)) {
const char temp[2] = {character, 0};
furi_string_replace_at(context->line, context->cursor++, 0, temp);
printf("\e[4h%c\e[4l", character);
fflush(stdout);
continue;
}
} while(true);
// Ctrl + D
if(exit) {
break;
}
update_history(context);
} while((context->is_ps2 = continue_with_input(context)));
// Ctrl + D
if(exit) {
break;
}
mp_flipper_exec_str(furi_string_get_cstr(context->code));
} while(true);
mp_flipper_deinit();
mp_flipper_repl_context_free(context);
free(heap);
free(buffer);
}