upython_repl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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("\33[2K\r%s%s", psx, furi_string_get_cstr(context->line));
  56. for(size_t i = context->cursor; i < furi_string_size(context->line); i++) {
  57. printf("\e[D");
  58. }
  59. fflush(stdout);
  60. }
  61. inline static void handle_arrow_keys(char character, mp_flipper_repl_context_t* context) {
  62. mp_flipper_repl_history_t* history = context->history;
  63. do {
  64. bool update_by_history = false;
  65. // up arrow
  66. if(character == 'A' && history->pointer == 0) {
  67. furi_string_set(history->stack[0], context->line);
  68. }
  69. if(character == 'A' && history->pointer < history->size) {
  70. history->pointer += (history->pointer + 1) == history->size ? 0 : 1;
  71. update_by_history = true;
  72. }
  73. // down arrow
  74. if(character == 'B' && history->pointer > 0) {
  75. history->pointer--;
  76. update_by_history = true;
  77. }
  78. if(update_by_history) {
  79. furi_string_set(context->line, history->stack[history->pointer]);
  80. context->cursor = furi_string_size(context->line);
  81. break;
  82. }
  83. // right arrow
  84. if(character == 'C' && context->cursor != furi_string_size(context->line)) {
  85. context->cursor++;
  86. break;
  87. }
  88. // left arrow
  89. if(character == 'D' && context->cursor > 0) {
  90. context->cursor--;
  91. break;
  92. }
  93. } while(false);
  94. print_full_psx(context);
  95. }
  96. inline static void handle_backspace(mp_flipper_repl_context_t* context) {
  97. // skip backspace at begin of line
  98. if(context->cursor == 0) {
  99. return;
  100. }
  101. const char* line = furi_string_get_cstr(context->line);
  102. size_t before = context->cursor - 1;
  103. size_t after = furi_string_size(context->line) - context->cursor;
  104. furi_string_printf(context->line, "%.*s%.*s", before, line, after, line + context->cursor);
  105. context->cursor--;
  106. printf("\e[D\e[1P");
  107. fflush(stdout);
  108. }
  109. inline static bool is_indent_required(mp_flipper_repl_context_t* context) {
  110. for(size_t i = 0; context->is_ps2 && i < context->cursor; i++) {
  111. if(furi_string_get_char(context->line, i) != ' ') {
  112. return false;
  113. }
  114. }
  115. return context->is_ps2;
  116. }
  117. inline static void handle_autocomplete(mp_flipper_repl_context_t* context) {
  118. // check if ps2 is active and just a tab character is required
  119. if(is_indent_required(context)) {
  120. furi_string_replace_at(context->line, context->cursor, 0, " ");
  121. context->cursor += 4;
  122. print_full_psx(context);
  123. return;
  124. }
  125. const char* new_line = furi_string_get_cstr(context->line);
  126. FuriString* orig_line = furi_string_alloc_printf("%s", new_line);
  127. const char* orig_line_str = furi_string_get_cstr(orig_line);
  128. char* completion = malloc(128 * sizeof(char));
  129. mp_print_t* print = malloc(sizeof(mp_print_t));
  130. print->data = mp_flipper_print_data_alloc();
  131. print->print_strn = mp_flipper_print_strn;
  132. size_t length = mp_flipper_repl_autocomplete(new_line, context->cursor, print, &completion);
  133. do {
  134. if(length == 0) {
  135. break;
  136. }
  137. if(length != AUTOCOMPLETE_MANY_MATCHES) {
  138. furi_string_printf(
  139. context->line,
  140. "%.*s%.*s%s",
  141. context->cursor,
  142. orig_line_str,
  143. length,
  144. completion,
  145. orig_line_str + context->cursor);
  146. context->cursor += length;
  147. } else {
  148. printf("%s", mp_flipper_print_get_data(print->data));
  149. }
  150. print_full_psx(context);
  151. } while(false);
  152. mp_flipper_print_data_free(print->data);
  153. furi_string_free(orig_line);
  154. free(completion);
  155. free(print);
  156. }
  157. inline static void update_history(mp_flipper_repl_context_t* context) {
  158. mp_flipper_repl_history_t* history = context->history;
  159. if(!furi_string_empty(context->line) && !furi_string_equal(context->line, history->stack[1])) {
  160. history->size += history->size == HISTORY_SIZE ? 0 : 1;
  161. for(size_t i = history->size - 1; i > 1; i--) {
  162. furi_string_set(history->stack[i], history->stack[i - 1]);
  163. }
  164. furi_string_set(history->stack[1], context->line);
  165. }
  166. furi_string_reset(history->stack[0]);
  167. history->pointer = 0;
  168. }
  169. inline static bool continue_with_input(mp_flipper_repl_context_t* context) {
  170. if(furi_string_empty(context->line)) {
  171. return false;
  172. }
  173. if(!mp_flipper_repl_continue_with_input(furi_string_get_cstr(context->code))) {
  174. return false;
  175. }
  176. return true;
  177. }
  178. void upython_repl_execute(Cli* cli) {
  179. size_t stack;
  180. const size_t heap_size = memmgr_get_free_heap() * 0.1;
  181. const size_t stack_size = 2 * 1024;
  182. uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
  183. printf("MicroPython (%s, %s) on Flipper Zero\r\n", MICROPY_GIT_TAG, MICROPY_BUILD_DATE);
  184. printf("Quit: Ctrl+D | Heap: %zu bytes | Stack: %zu bytes\r\n", heap_size, stack_size);
  185. mp_flipper_repl_context_t* context = mp_flipper_repl_context_alloc();
  186. mp_flipper_set_root_module_path("/ext");
  187. mp_flipper_init(heap, heap_size, stack_size, &stack);
  188. char character = '\0';
  189. uint8_t* buffer = malloc(sizeof(uint8_t));
  190. bool exit = false;
  191. bool wait_for_lf = false;
  192. // REPL loop
  193. do {
  194. furi_string_reset(context->code);
  195. context->is_ps2 = false;
  196. // scan line loop
  197. do {
  198. furi_string_reset(context->line);
  199. context->cursor = 0;
  200. print_full_psx(context);
  201. // scan character loop
  202. do {
  203. character = cli_getc(cli);
  204. // Ctrl + C
  205. if(character == CliSymbolAsciiETX) {
  206. context->cursor = 0;
  207. furi_string_reset(context->line);
  208. furi_string_reset(context->code);
  209. printf("\r\nKeyboardInterrupt\r\n");
  210. break;
  211. }
  212. // Ctrl + D
  213. if(character == CliSymbolAsciiEOT) {
  214. exit = true;
  215. break;
  216. }
  217. // wait for line feed character
  218. if(character == CliSymbolAsciiCR) {
  219. wait_for_lf = true;
  220. continue;
  221. }
  222. // skip if we don't wait for line feed
  223. if(!wait_for_lf && character == CliSymbolAsciiLF) {
  224. continue;
  225. }
  226. // handle line feed
  227. if(wait_for_lf && character == CliSymbolAsciiLF) {
  228. furi_string_push_back(context->code, '\n');
  229. furi_string_cat(context->code, context->line);
  230. furi_string_trim(context->code);
  231. cli_nl(cli);
  232. break;
  233. }
  234. // handle arrow keys
  235. if(character >= 0x18 && character <= 0x1B) {
  236. character = cli_getc(cli);
  237. character = cli_getc(cli);
  238. handle_arrow_keys(character, context);
  239. continue;
  240. }
  241. // handle tab, do autocompletion
  242. if(character == CliSymbolAsciiTab) {
  243. handle_autocomplete(context);
  244. continue;
  245. }
  246. // handle backspace
  247. if(character == CliSymbolAsciiBackspace) {
  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. cli_write(cli, (const uint8_t*)buffer, 1);
  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. }