upython_repl.c 10 KB

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