upython_repl.c 11 KB

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