upython_repl.c 11 KB

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