upython_cli.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <furi.h>
  2. #include <storage/storage.h>
  3. #include <toolbox/pipe.h>
  4. #include "upython.h"
  5. static FuriStreamBuffer* stdout_buffer = NULL;
  6. static void write_to_stdout_buffer(const char* data, size_t size, void* context) {
  7. UNUSED(context);
  8. furi_stream_buffer_send(stdout_buffer, data, size, 0);
  9. }
  10. void upython_cli(PipeSide* pipe, FuriString* args, void* ctx) {
  11. UNUSED(ctx);
  12. pipe_install_as_stdio(pipe);
  13. if(action != ActionNone) {
  14. printf("%s is busy!\n", TAG);
  15. return;
  16. }
  17. if(furi_string_empty(args)) {
  18. action = ActionRepl;
  19. upython_repl_execute();
  20. action = ActionNone;
  21. } else {
  22. furi_string_set(file_path, args);
  23. stdout_buffer = furi_stream_buffer_alloc(128, 1);
  24. stdout_callback = write_to_stdout_buffer;
  25. action = ActionExec;
  26. char data = '\0';
  27. while(action == ActionExec || !furi_stream_buffer_is_empty(stdout_buffer)) {
  28. if(furi_stream_buffer_receive(stdout_buffer, &data, 1, 0) > 0) {
  29. printf("%c", data);
  30. }
  31. }
  32. furi_stream_buffer_free(stdout_buffer);
  33. }
  34. }
  35. void upython_cli_register(void* args) {
  36. if(args != NULL) {
  37. file_path = furi_string_alloc_set_str(args);
  38. action = ActionExec;
  39. return;
  40. } else {
  41. file_path = furi_string_alloc();
  42. upython_reset_file_path();
  43. action = ActionNone;
  44. }
  45. CliRegistry* cli = furi_record_open(RECORD_CLI);
  46. cli_registry_add_command(cli, CLI, CliCommandFlagParallelSafe, upython_cli, NULL);
  47. furi_record_close(RECORD_CLI);
  48. }
  49. void upython_cli_unregister(void* args) {
  50. furi_string_free(file_path);
  51. if(args != NULL) {
  52. return;
  53. }
  54. CliRegistry* cli = furi_record_open(RECORD_CLI);
  55. cli_registry_delete_command(cli, CLI);
  56. furi_record_close(RECORD_CLI);
  57. }