forked from ofabel/mp-flipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upython_cli.c
82 lines (54 loc) · 1.7 KB
/
upython_cli.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
#include <furi.h>
#include <storage/storage.h>
#include "upython.h"
static FuriStreamBuffer* stdout_buffer = NULL;
static void write_to_stdout_buffer(const char* data, size_t size) {
furi_stream_buffer_send(stdout_buffer, data, size, 0);
}
void upython_cli(Cli* cli, FuriString* args, void* ctx) {
UNUSED(ctx);
if(action != ActionNone) {
printf("%s is busy!\n", TAG);
return;
}
if(furi_string_empty(args)) {
action = ActionRepl;
upython_repl_execute(cli);
action = ActionNone;
} else {
furi_string_set(file_path, args);
stdout_buffer = furi_stream_buffer_alloc(128, 1);
stdout_callback = write_to_stdout_buffer;
action = ActionExec;
char data = '\0';
while(action == ActionExec || !furi_stream_buffer_is_empty(stdout_buffer)) {
if(furi_stream_buffer_receive(stdout_buffer, &data, 1, 0) > 0) {
printf("%c", data);
}
}
furi_stream_buffer_free(stdout_buffer);
}
}
void upython_cli_register(void* args) {
if(args != NULL) {
file_path = furi_string_alloc_set_str(args);
action = ActionExec;
return;
} else {
file_path = furi_string_alloc();
upython_reset_file_path();
action = ActionNone;
}
Cli* cli = furi_record_open(RECORD_CLI);
cli_add_command(cli, CLI, CliCommandFlagParallelSafe, upython_cli, NULL);
furi_record_close(RECORD_CLI);
}
void upython_cli_unregister(void* args) {
furi_string_free(file_path);
if(args != NULL) {
return;
}
Cli* cli = furi_record_open(RECORD_CLI);
cli_delete_command(cli, CLI);
furi_record_close(RECORD_CLI);
}