upython_repl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #include <stdio.h>
  2. #include <cli/cli.h>
  3. #include <cli/cli_ansi.h>
  4. #include <furi.h>
  5. #include <genhdr/mpversion.h>
  6. #include <mp_flipper_compiler.h>
  7. #include <mp_flipper_repl.h>
  8. #include "upython.h"
  9. #define AUTOCOMPLETE_MANY_MATCHES (size_t)(-1)
  10. #define HISTORY_SIZE 16
  11. typedef struct {
  12. FuriString** stack;
  13. size_t pointer;
  14. size_t size;
  15. } mp_flipper_repl_history_t;
  16. typedef struct {
  17. mp_flipper_repl_history_t* history;
  18. FuriString* line;
  19. FuriString* code;
  20. size_t cursor;
  21. bool is_ps2;
  22. } mp_flipper_repl_context_t;
  23. static mp_flipper_repl_history_t* mp_flipper_repl_history_alloc() {
  24. mp_flipper_repl_history_t* history = malloc(sizeof(mp_flipper_repl_history_t));
  25. history->stack = malloc(HISTORY_SIZE * sizeof(FuriString*));
  26. history->pointer = 0;
  27. history->size = 1;
  28. for(size_t i = 0; i < HISTORY_SIZE; i++) {
  29. history->stack[i] = furi_string_alloc();
  30. }
  31. return history;
  32. }
  33. static void mp_flipper_repl_history_free(mp_flipper_repl_history_t* history) {
  34. for(size_t i = 0; i < HISTORY_SIZE; i++) {
  35. furi_string_free(history->stack[i]);
  36. }
  37. free(history);
  38. }
  39. static mp_flipper_repl_context_t* mp_flipper_repl_context_alloc() {
  40. mp_flipper_repl_context_t* context = malloc(sizeof(mp_flipper_repl_context_t));
  41. context->history = mp_flipper_repl_history_alloc();
  42. context->code = furi_string_alloc();
  43. context->line = furi_string_alloc();
  44. context->cursor = 0;
  45. context->is_ps2 = false;
  46. return context;
  47. }
  48. static void mp_flipper_repl_context_free(mp_flipper_repl_context_t* context) {
  49. mp_flipper_repl_history_free(context->history);
  50. furi_string_free(context->code);
  51. furi_string_free(context->line);
  52. free(context);
  53. }
  54. static void print_full_psx(mp_flipper_repl_context_t* context) {
  55. const char* psx = context->is_ps2 ? "... " : ">>> ";
  56. printf("\e[2K\r%s%s", psx, furi_string_get_cstr(context->line));
  57. fflush(stdout);
  58. for(size_t i = context->cursor; i < furi_string_size(context->line); i++) {
  59. printf("\e[D");
  60. }
  61. fflush(stdout);
  62. }
  63. inline static void handle_arrow_keys(char character, mp_flipper_repl_context_t* context) {
  64. mp_flipper_repl_history_t* history = context->history;
  65. do {
  66. bool update_by_history = false;
  67. // up arrow
  68. if(character == 'A' && history->pointer == 0) {
  69. furi_string_set(history->stack[0], context->line);
  70. }
  71. if(character == 'A' && history->pointer < history->size) {
  72. history->pointer += (history->pointer + 1) == history->size ? 0 : 1;
  73. update_by_history = true;
  74. }
  75. // down arrow
  76. if(character == 'B' && history->pointer > 0) {
  77. history->pointer--;
  78. update_by_history = true;
  79. }
  80. if(update_by_history) {
  81. furi_string_set(context->line, history->stack[history->pointer]);
  82. context->cursor = furi_string_size(context->line);
  83. break;
  84. }
  85. // right arrow
  86. if(character == 'C' && context->cursor != furi_string_size(context->line)) {
  87. context->cursor++;
  88. break;
  89. }
  90. // left arrow
  91. if(character == 'D' && context->cursor > 0) {
  92. context->cursor--;
  93. break;
  94. }
  95. } while(false);
  96. print_full_psx(context);
  97. }
  98. inline static void handle_backspace(mp_flipper_repl_context_t* context) {
  99. // skip backspace at begin of line
  100. if(context->cursor == 0) {
  101. return;
  102. }
  103. const char* line = furi_string_get_cstr(context->line);
  104. size_t before = context->cursor - 1;
  105. size_t after = furi_string_size(context->line) - context->cursor;
  106. furi_string_printf(context->line, "%.*s%.*s", before, line, after, line + context->cursor);
  107. context->cursor--;
  108. printf("\e[D\e[1P");
  109. fflush(stdout);
  110. }
  111. inline static bool is_indent_required(mp_flipper_repl_context_t* context) {
  112. for(size_t i = 0; context->is_ps2 && i < context->cursor; i++) {
  113. if(furi_string_get_char(context->line, i) != ' ') {
  114. return false;
  115. }
  116. }
  117. return context->is_ps2;
  118. }
  119. inline static void handle_autocomplete(mp_flipper_repl_context_t* context) {
  120. // check if ps2 is active and just a tab character is required
  121. if(is_indent_required(context)) {
  122. furi_string_replace_at(context->line, context->cursor, 0, " ");
  123. context->cursor += 4;
  124. print_full_psx(context);
  125. return;
  126. }
  127. const char* new_line = furi_string_get_cstr(context->line);
  128. FuriString* orig_line = furi_string_alloc_printf("%s", new_line);
  129. const char* orig_line_str = furi_string_get_cstr(orig_line);
  130. char* completion = malloc(128 * sizeof(char));
  131. mp_print_t* print = malloc(sizeof(mp_print_t));
  132. print->data = mp_flipper_print_data_alloc();
  133. print->print_strn = mp_flipper_print_strn;
  134. size_t length = mp_flipper_repl_autocomplete(new_line, context->cursor, print, &completion);
  135. do {
  136. if(length == 0) {
  137. break;
  138. }
  139. if(length != AUTOCOMPLETE_MANY_MATCHES) {
  140. furi_string_printf(
  141. context->line,
  142. "%.*s%.*s%s",
  143. context->cursor,
  144. orig_line_str,
  145. length,
  146. completion,
  147. orig_line_str + context->cursor);
  148. context->cursor += length;
  149. } else {
  150. printf("%s", mp_flipper_print_get_data(print->data));
  151. }
  152. print_full_psx(context);
  153. } while(false);
  154. mp_flipper_print_data_free(print->data);
  155. furi_string_free(orig_line);
  156. free(completion);
  157. free(print);
  158. }
  159. inline static void update_history(mp_flipper_repl_context_t* context) {
  160. mp_flipper_repl_history_t* history = context->history;
  161. if(!furi_string_empty(context->line) && !furi_string_equal(context->line, history->stack[1])) {
  162. history->size += history->size == HISTORY_SIZE ? 0 : 1;
  163. for(size_t i = history->size - 1; i > 1; i--) {
  164. furi_string_set(history->stack[i], history->stack[i - 1]);
  165. }
  166. furi_string_set(history->stack[1], context->line);
  167. }
  168. furi_string_reset(history->stack[0]);
  169. history->pointer = 0;
  170. }
  171. inline static bool continue_with_input(mp_flipper_repl_context_t* context) {
  172. if(furi_string_empty(context->line)) {
  173. return false;
  174. }
  175. if(!mp_flipper_repl_continue_with_input(furi_string_get_cstr(context->code))) {
  176. return false;
  177. }
  178. return true;
  179. }
  180. void upython_repl_execute(Cli* cli) {
  181. size_t stack;
  182. const size_t heap_size = memmgr_get_free_heap() * 0.1;
  183. const size_t stack_size = 2 * 1024;
  184. uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
  185. printf("MicroPython (%s, %s) on Flipper Zero\r\n", MICROPY_GIT_TAG, MICROPY_BUILD_DATE);
  186. printf("Quit: Ctrl+D | Heap: %zu bytes | Stack: %zu bytes\r\n", heap_size, stack_size);
  187. printf(" To do a reboot, press Left+Back for 5 seconds.\r\n");
  188. printf("Docs: https://ofabel.github.io/mp-flipper\r\n");
  189. mp_flipper_repl_context_t* context = mp_flipper_repl_context_alloc();
  190. mp_flipper_set_root_module_path("/ext");
  191. mp_flipper_init(heap, heap_size, stack_size, &stack);
  192. char character = '\0';
  193. uint8_t* buffer = malloc(sizeof(uint8_t));
  194. bool exit = false;
  195. // REPL loop
  196. do {
  197. furi_string_reset(context->code);
  198. context->is_ps2 = false;
  199. // scan line loop
  200. do {
  201. furi_string_reset(context->line);
  202. context->cursor = 0;
  203. print_full_psx(context);
  204. // scan character loop
  205. do {
  206. character = cli_getc(cli);
  207. // Ctrl + C
  208. if(character == CliKeyETX) {
  209. context->cursor = 0;
  210. furi_string_reset(context->line);
  211. furi_string_reset(context->code);
  212. printf("\r\nKeyboardInterrupt\r\n");
  213. break;
  214. }
  215. // Ctrl + D
  216. if(character == CliKeyEOT) {
  217. exit = true;
  218. break;
  219. }
  220. // skip line feed
  221. if(character == CliKeyLF) {
  222. continue;
  223. }
  224. // handle carriage return
  225. if(character == CliKeyCR) {
  226. furi_string_push_back(context->code, '\n');
  227. furi_string_cat(context->code, context->line);
  228. furi_string_trim(context->code);
  229. cli_nl(cli);
  230. break;
  231. }
  232. // handle arrow keys
  233. if(character >= 0x18 && character <= 0x1B) {
  234. character = cli_getc(cli);
  235. character = cli_getc(cli);
  236. handle_arrow_keys(character, context);
  237. continue;
  238. }
  239. // handle tab, do autocompletion
  240. if(character == CliKeyTab) {
  241. handle_autocomplete(context);
  242. continue;
  243. }
  244. // handle backspace
  245. if(character == CliKeyBackspace || character == CliKeyDEL) {
  246. handle_backspace(context);
  247. continue;
  248. }
  249. // append at end
  250. if(context->cursor == furi_string_size(context->line)) {
  251. buffer[0] = character;
  252. cli_write(cli, (const uint8_t*)buffer, 1);
  253. furi_string_push_back(context->line, character);
  254. context->cursor++;
  255. continue;
  256. }
  257. // insert between
  258. if(context->cursor < furi_string_size(context->line)) {
  259. const char temp[2] = {character, 0};
  260. furi_string_replace_at(context->line, context->cursor++, 0, temp);
  261. printf("\e[4h%c\e[4l", character);
  262. fflush(stdout);
  263. continue;
  264. }
  265. } while(true);
  266. // Ctrl + D
  267. if(exit) {
  268. break;
  269. }
  270. update_history(context);
  271. } while((context->is_ps2 = continue_with_input(context)));
  272. // Ctrl + D
  273. if(exit) {
  274. break;
  275. }
  276. mp_flipper_exec_str(furi_string_get_cstr(context->code));
  277. } while(true);
  278. mp_flipper_deinit();
  279. mp_flipper_repl_context_free(context);
  280. free(heap);
  281. free(buffer);
  282. }