cli_helpers.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #include <cli/cli.h>
  3. #include "../types/plugin_state.h"
  4. #define TOTP_CLI_COMMAND_NAME "totp"
  5. #define DOCOPT_ARGUMENT(arg) "<" arg ">"
  6. #define DOCOPT_MULTIPLE(arg) arg "..."
  7. #define DOCOPT_OPTIONAL(param) "[" param "]"
  8. #define DOCOPT_REQUIRED(param) "(" param ")"
  9. #define DOCOPT_OPTION(option, value) option " " value
  10. #define DOCOPT_SWITCH(option) option
  11. #define DOCOPT_OPTIONS "[options]"
  12. #define DOCOPT_DEFAULT(val) "[default: " val "]"
  13. #define TOTP_CLI_PRINTF(format, ...) \
  14. do { \
  15. _Pragma(STRINGIFY(GCC diagnostic push)) \
  16. _Pragma(STRINGIFY(GCC diagnostic ignored "-Wdouble-promotion")) \
  17. printf(format, ##__VA_ARGS__); \
  18. _Pragma(STRINGIFY(GCC diagnostic pop)) \
  19. } while(false)
  20. #define TOTP_CLI_PRINTF_COLORFUL(color, format, ...) \
  21. do { \
  22. _Pragma(STRINGIFY(GCC diagnostic push)) \
  23. _Pragma(STRINGIFY(GCC diagnostic ignored "-Wdouble-promotion")) \
  24. printf("\e[%s", color); \
  25. printf(format, ##__VA_ARGS__); \
  26. printf("\e[0m"); \
  27. fflush(stdout); \
  28. _Pragma(STRINGIFY(GCC diagnostic pop)) \
  29. } while(false)
  30. #define TOTP_CLI_COLOR_ERROR "91m"
  31. #define TOTP_CLI_COLOR_WARNING "93m"
  32. #define TOTP_CLI_COLOR_SUCCESS "92m"
  33. #define TOTP_CLI_COLOR_INFO "96m"
  34. #define TOTP_CLI_PRINTF_ERROR(format, ...) \
  35. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_ERROR, format, ##__VA_ARGS__)
  36. #define TOTP_CLI_PRINTF_WARNING(format, ...) \
  37. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_WARNING, format, ##__VA_ARGS__)
  38. #define TOTP_CLI_PRINTF_SUCCESS(format, ...) \
  39. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_SUCCESS, format, ##__VA_ARGS__)
  40. #define TOTP_CLI_PRINTF_INFO(format, ...) \
  41. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_INFO, format, ##__VA_ARGS__)
  42. #define TOTP_CLI_DELETE_LAST_LINE() \
  43. TOTP_CLI_PRINTF("\033[A\33[2K\r"); \
  44. fflush(stdout)
  45. #define TOTP_CLI_DELETE_CURRENT_LINE() \
  46. TOTP_CLI_PRINTF("\33[2K\r"); \
  47. fflush(stdout)
  48. #define TOTP_CLI_DELETE_LAST_CHAR() \
  49. TOTP_CLI_PRINTF("\b \b"); \
  50. fflush(stdout)
  51. #define TOTP_CLI_PRINT_INVALID_ARGUMENTS() \
  52. TOTP_CLI_PRINTF_ERROR( \
  53. "Invalid command arguments. use \"help\" command to get list of available commands")
  54. #define TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE() \
  55. TOTP_CLI_PRINTF_ERROR("An error has occurred during updating config file\r\n")
  56. /**
  57. * @brief Checks whether user is authenticated and entered correct PIN.
  58. * If user is not authenticated it prompts user to enter correct PIN to authenticate.
  59. * @param plugin_state application state
  60. * @param cli pointer to the firmware CLI subsystem
  61. * @return \c true if user is already authenticated or successfully authenticated; \c false otherwise
  62. */
  63. bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli);
  64. /**
  65. * @brief Forces application to be instantly closed
  66. * @param event_queue main app queue
  67. */
  68. void totp_cli_force_close_app(FuriMessageQueue* event_queue);
  69. /**
  70. * @brief Reads line of characters from console
  71. * @param cli pointer to the firmware CLI subsystem
  72. * @param out_str pointer to an output string to put read line to
  73. * @param mask_user_input whether to mask input characters in console or not
  74. * @return \c true if line successfully read and confirmed; \c false otherwise
  75. */
  76. bool totp_cli_read_line(Cli* cli, FuriString* out_str, bool mask_user_input);
  77. /**
  78. * @brief Extracts \c uint8_t value and trims arguments string
  79. * @param args arguments string
  80. * @param[out] value parsed value
  81. * @return \c true if value successfully read and parsed as \c uint8_t ; \c false otherwise
  82. */
  83. bool args_read_uint8_and_trim(FuriString* args, uint8_t* value);
  84. /**
  85. * @brief Free \c FuriString instance in a secure manner by clearing it first
  86. * @param str instance to free
  87. */
  88. void furi_string_secure_free(FuriString* str);