upython_repl.c 10 KB

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