upython_cli.c 1.7 KB

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