upython_repl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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(PipeSide* pipe) {
  181. UNUSED(pipe);
  182. size_t stack;
  183. const size_t heap_size = memmgr_get_free_heap() * 0.1;
  184. const size_t stack_size = 2 * 1024;
  185. uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
  186. printf("MicroPython (%s, %s) on Flipper Zero\r\n", MICROPY_GIT_TAG, MICROPY_BUILD_DATE);
  187. printf("Quit: Ctrl+D | Heap: %zu bytes | Stack: %zu bytes\r\n", heap_size, stack_size);
  188. printf(" To do a reboot, press Left+Back for 5 seconds.\r\n");
  189. printf("Docs: https://ofabel.github.io/mp-flipper\r\n");
  190. mp_flipper_repl_context_t* context = mp_flipper_repl_context_alloc();
  191. mp_flipper_set_root_module_path("/ext");
  192. mp_flipper_init(heap, heap_size, stack_size, &stack);
  193. char character = '\0';
  194. uint8_t* buffer = malloc(sizeof(uint8_t));
  195. bool exit = false;
  196. // REPL loop
  197. do {
  198. furi_string_reset(context->code);
  199. context->is_ps2 = false;
  200. // scan line loop
  201. do {
  202. furi_string_reset(context->line);
  203. context->cursor = 0;
  204. print_full_psx(context);
  205. // scan character loop
  206. do {
  207. character = getchar();
  208. // Ctrl + C
  209. if(character == CliKeyETX) {
  210. context->cursor = 0;
  211. furi_string_reset(context->line);
  212. furi_string_reset(context->code);
  213. printf("\r\nKeyboardInterrupt\r\n");
  214. break;
  215. }
  216. // Ctrl + D
  217. if(character == CliKeyEOT) {
  218. exit = true;
  219. break;
  220. }
  221. // skip line feed
  222. if(character == CliKeyLF) {
  223. continue;
  224. }
  225. // handle carriage return
  226. if(character == CliKeyCR) {
  227. furi_string_push_back(context->code, '\n');
  228. furi_string_cat(context->code, context->line);
  229. furi_string_trim(context->code);
  230. printf("\r\n");
  231. break;
  232. }
  233. // handle arrow keys
  234. if(character >= 0x18 && character <= 0x1B) {
  235. character = getchar();
  236. character = getchar();
  237. handle_arrow_keys(character, context);
  238. continue;
  239. }
  240. // handle tab, do autocompletion
  241. if(character == CliKeyTab) {
  242. handle_autocomplete(context);
  243. continue;
  244. }
  245. // handle backspace
  246. if(character == CliKeyBackspace || character == CliKeyDEL) {
  247. handle_backspace(context);
  248. continue;
  249. }
  250. // append at end
  251. if(context->cursor == furi_string_size(context->line)) {
  252. buffer[0] = character;
  253. putchar(character);
  254. furi_string_push_back(context->line, character);
  255. context->cursor++;
  256. continue;
  257. }
  258. // insert between
  259. if(context->cursor < furi_string_size(context->line)) {
  260. const char temp[2] = {character, 0};
  261. furi_string_replace_at(context->line, context->cursor++, 0, temp);
  262. printf("\e[4h%c\e[4l", character);
  263. fflush(stdout);
  264. continue;
  265. }
  266. } while(true);
  267. // Ctrl + D
  268. if(exit) {
  269. break;
  270. }
  271. update_history(context);
  272. } while((context->is_ps2 = continue_with_input(context)));
  273. // Ctrl + D
  274. if(exit) {
  275. break;
  276. }
  277. mp_flipper_exec_str(furi_string_get_cstr(context->code));
  278. } while(true);
  279. mp_flipper_deinit();
  280. mp_flipper_repl_context_free(context);
  281. free(heap);
  282. free(buffer);
  283. }