Skip to content

Commit

Permalink
Clang format ran
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov committed Oct 25, 2022
1 parent 3a08fee commit 1a51c11
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 76 deletions.
3 changes: 2 additions & 1 deletion scenes/token_menu/totp_scene_token_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ bool totp_scene_token_menu_handle_event(PluginEvent* const event, PluginState* p
dialog_message_show(plugin_state->dialogs, message);
dialog_message_free(message);
if(dialog_result == DialogMessageButtonRight) {
ListNode* list_node = list_element_at(plugin_state->tokens_list, scene_state->current_token_index);
ListNode* list_node = list_element_at(
plugin_state->tokens_list, scene_state->current_token_index);

TokenInfo* tokenInfo = list_node->data;
token_info_free(tokenInfo);
Expand Down
9 changes: 6 additions & 3 deletions services/cli/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#define TOTP_CLI_COMMAND_HELP "help"

static void totp_cli_print_unknown_command(FuriString* unknown_command) {
TOTP_CLI_PRINTF("Command \"%s\" is unknown. Use \"help\" command to get list of available commands.", furi_string_get_cstr(unknown_command));
TOTP_CLI_PRINTF(
"Command \"%s\" is unknown. Use \"help\" command to get list of available commands.",
furi_string_get_cstr(unknown_command));
}

static void totp_cli_print_help() {
Expand All @@ -27,7 +29,7 @@ static void totp_cli_print_help() {
}

static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
PluginState* plugin_state = (PluginState* )context;
PluginState* plugin_state = (PluginState*)context;

FuriString* cmd = furi_string_alloc();

Expand All @@ -52,7 +54,8 @@ static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {

void totp_cli_register_command_handler(PluginState* plugin_state) {
Cli* cli = furi_record_open(RECORD_CLI);
cli_add_command(cli, TOTP_CLI_COMMAND_NAME, CliCommandFlagParallelSafe, totp_cli_handler, plugin_state);
cli_add_command(
cli, TOTP_CLI_COMMAND_NAME, CliCommandFlagParallelSafe, totp_cli_handler, plugin_state);
furi_record_close(RECORD_CLI);
}

Expand Down
12 changes: 7 additions & 5 deletions services/cli/cli_common_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
#include <cli/cli.h>

void totp_cli_print_invalid_arguments() {
TOTP_CLI_PRINTF("Invalid command arguments. use \"help\" command to get list of available commands");
TOTP_CLI_PRINTF(
"Invalid command arguments. use \"help\" command to get list of available commands");
}

bool totp_cli_ensure_authenticated(PluginState* plugin_state, Cli* cli) {
if (plugin_state->current_scene == TotpSceneAuthentication) {
if(plugin_state->current_scene == TotpSceneAuthentication) {
TOTP_CLI_PRINTF("Pleases enter PIN on your flipper device\r\n");

while (plugin_state->current_scene == TotpSceneAuthentication && !cli_cmd_interrupt_received(cli)) {

while(plugin_state->current_scene == TotpSceneAuthentication &&
!cli_cmd_interrupt_received(cli)) {
furi_delay_tick(0);
}

TOTP_CLI_PRINTF("\033[A\33[2K\r");
fflush(stdout);

if (plugin_state->current_scene == TotpSceneAuthentication) {
if(plugin_state->current_scene == TotpSceneAuthentication) {
return false;
}
}
Expand Down
6 changes: 3 additions & 3 deletions services/cli/cli_common_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#define TOTP_CLI_OPTIONAL_PARAM(param) "[" param "]"
#define TOTP_CLI_OPTIONAL_PARAM_MARK "[OPTIONAL]"

#define TOTP_CLI_PRINTF(format, ...) \
_Pragma(STRINGIFY(GCC diagnostic push)); \
#define TOTP_CLI_PRINTF(format, ...) \
_Pragma(STRINGIFY(GCC diagnostic push)); \
_Pragma(STRINGIFY(GCC diagnostic ignored "-Wdouble-promotion")); \
printf(format, ##__VA_ARGS__); \
printf(format, ##__VA_ARGS__); \
_Pragma(STRINGIFY(GCC diagnostic pop));

void totp_cli_print_invalid_arguments();
Expand Down
82 changes: 50 additions & 32 deletions services/cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ static bool token_info_set_algo_from_str(TokenInfo* token_info, FuriString* str)
if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
token_info->algo = SHA1;
return true;
}
}

if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME) == 0) {
token_info->algo = SHA256;
return true;
}
}

if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME) == 0) {
token_info->algo = SHA512;
return true;
Expand All @@ -47,11 +47,21 @@ static bool token_info_set_algo_from_str(TokenInfo* token_info, FuriString* str)
}

void totp_cli_command_add_print_help() {
TOTP_CLI_PRINTF("\t" TOTP_CLI_COMMAND_ADD " " TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_NAME) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX " " TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_ALGO)) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX " " TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_DIGITS)) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX) " - add new token\r\n");
TOTP_CLI_PRINTF("\t" TOTP_CLI_COMMAND_ADD " " TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_NAME) " " TOTP_CLI_OPTIONAL_PARAM(
TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX " " TOTP_CLI_ARG(
TOTP_CLI_COMMAND_ADD_ARG_ALGO)) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
" " TOTP_CLI_ARG(
TOTP_CLI_COMMAND_ADD_ARG_DIGITS)) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX) " - add new token\r\n");
TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_NAME) " - token name\r\n");
TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_ALGO) " - " TOTP_CLI_OPTIONAL_PARAM_MARK " token hashing algorithm, could be one of: sha1, sha256, sha512; default: sha1\r\n");
TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_DIGITS) " - " TOTP_CLI_OPTIONAL_PARAM_MARK " number of digits to generate, one of: 6, 8; default: 6\r\n");
TOTP_CLI_PRINTF("\t\t" TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX " - " TOTP_CLI_OPTIONAL_PARAM_MARK " to show console user input as-is without masking; default: show masked\r\n\r\n");
TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(
TOTP_CLI_COMMAND_ADD_ARG_ALGO) " - " TOTP_CLI_OPTIONAL_PARAM_MARK
" token hashing algorithm, could be one of: sha1, sha256, sha512; default: sha1\r\n");
TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(
TOTP_CLI_COMMAND_ADD_ARG_DIGITS) " - " TOTP_CLI_OPTIONAL_PARAM_MARK
" number of digits to generate, one of: 6, 8; default: 6\r\n");
TOTP_CLI_PRINTF(
"\t\t" TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX " - " TOTP_CLI_OPTIONAL_PARAM_MARK
" to show console user input as-is without masking; default: show masked\r\n\r\n");
}

void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
Expand All @@ -61,7 +71,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
TokenInfo* token_info = token_info_alloc();

// Reading token name
if (!args_read_probably_quoted_string_and_trim(args, temp_str)) {
if(!args_read_probably_quoted_string_and_trim(args, temp_str)) {
totp_cli_print_invalid_arguments();
furi_string_free(temp_str);
token_info_free(token_info);
Expand All @@ -74,30 +84,39 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl

// Read optional arguments
bool mask_user_input = true;
while (args_read_string_and_trim(args, temp_str)) {
while(args_read_string_and_trim(args, temp_str)) {
bool parsed = false;
if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX) == 0) {
if (!args_read_string_and_trim(args, temp_str)) {
TOTP_CLI_PRINTF("Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX "\"\r\n");
} else if (!token_info_set_algo_from_str(token_info, temp_str)) {
TOTP_CLI_PRINTF("\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX "\"\r\n", furi_string_get_cstr(temp_str));
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX) == 0) {
if(!args_read_string_and_trim(args, temp_str)) {
TOTP_CLI_PRINTF("Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX
"\"\r\n");
} else if(!token_info_set_algo_from_str(token_info, temp_str)) {
TOTP_CLI_PRINTF(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX
"\"\r\n",
furi_string_get_cstr(temp_str));
} else {
parsed = true;
}
} else if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX) == 0) {
if (!args_read_string_and_trim(args, temp_str)) {
TOTP_CLI_PRINTF("Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX "\"\r\n");
} else if (!token_info_set_digits_from_str(token_info, temp_str)) {
TOTP_CLI_PRINTF("\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX "\"\r\n", furi_string_get_cstr(temp_str));
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX) == 0) {
if(!args_read_string_and_trim(args, temp_str)) {
TOTP_CLI_PRINTF(
"Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
"\"\r\n");
} else if(!token_info_set_digits_from_str(token_info, temp_str)) {
TOTP_CLI_PRINTF(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
"\"\r\n",
furi_string_get_cstr(temp_str));
} else {
parsed = true;
}
} else if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX) == 0) {
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX) == 0) {
mask_user_input = false;
parsed = true;
}

if (!parsed) {
if(!parsed) {
totp_cli_print_invalid_arguments();
furi_string_free(temp_str);
token_info_free(token_info);
Expand All @@ -111,7 +130,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl

uint8_t c;
while(cli_read(cli, &c, 1) == 1) {
if (c == CliSymbolAsciiEsc) {
if(c == CliSymbolAsciiEsc) {
uint8_t c2;
cli_read_timeout(cli, &c2, 1, 0);
cli_read_timeout(cli, &c2, 1, 0);
Expand All @@ -121,36 +140,35 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
token_info_free(token_info);
return;
} else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
if (mask_user_input) {
if(mask_user_input) {
putc('*', stdout);
} else {
putc(c, stdout);
}
fflush(stdout);
furi_string_push_back(temp_str, c);
} else if (c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
size_t temp_str_size = furi_string_size(temp_str);
if (temp_str_size > 0) {
if(temp_str_size > 0) {
TOTP_CLI_PRINTF("\b \b");
fflush(stdout);
furi_string_left(temp_str, temp_str_size - 1);
}
}
else if(c == CliSymbolAsciiCR) {
} else if(c == CliSymbolAsciiCR) {
cli_nl();
break;
}
}

temp_cstr = furi_string_get_cstr(temp_str);

if (!totp_cli_ensure_authenticated(plugin_state, cli)) {
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
furi_string_free(temp_str);
token_info_free(token_info);
return;
}

if (!token_info_set_secret(token_info, temp_cstr, strlen(temp_cstr), plugin_state->iv)) {
if(!token_info_set_secret(token_info, temp_cstr, strlen(temp_cstr), plugin_state->iv)) {
TOTP_CLI_PRINTF("Token secret seems to be invalid and can not be parsed\r\n");
furi_string_free(temp_str);
token_info_free(token_info);
Expand All @@ -161,7 +179,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
furi_string_free(temp_str);

bool load_generate_token_scene = false;
if (plugin_state->current_scene == TotpSceneGenerateToken) {
if(plugin_state->current_scene == TotpSceneGenerateToken) {
totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
load_generate_token_scene = true;
}
Expand All @@ -174,7 +192,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
plugin_state->tokens_count++;
totp_config_file_save_new_token(token_info);

if (load_generate_token_scene) {
if(load_generate_token_scene) {
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
}

Expand Down
46 changes: 26 additions & 20 deletions services/cli/commands/delete/delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,29 @@
#define TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX "-f"

void totp_cli_command_delete_print_help() {
TOTP_CLI_PRINTF("\t" TOTP_CLI_COMMAND_DELETE " " TOTP_CLI_ARG(TOTP_CLI_COMMAND_DELETE_ARG_INDEX) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX) " - delete token\r\n");
TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_DELETE_ARG_INDEX) " - token index in the list\r\n");
TOTP_CLI_PRINTF("\t\t" TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX " - " TOTP_CLI_OPTIONAL_PARAM_MARK " force command to do not ask user for interactive confirmation\r\n\r\n");
TOTP_CLI_PRINTF(
"\t" TOTP_CLI_COMMAND_DELETE
" " TOTP_CLI_ARG(TOTP_CLI_COMMAND_DELETE_ARG_INDEX) " " TOTP_CLI_OPTIONAL_PARAM(
TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX) " - delete token\r\n");
TOTP_CLI_PRINTF(
"\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_DELETE_ARG_INDEX) " - token index in the list\r\n");
TOTP_CLI_PRINTF("\t\t" TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX
" - " TOTP_CLI_OPTIONAL_PARAM_MARK
" force command to do not ask user for interactive confirmation\r\n\r\n");
}

void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
int token_number;
if (!args_read_int_and_trim(args, &token_number) || token_number <= 0 || token_number > plugin_state->tokens_count) {
if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
token_number > plugin_state->tokens_count) {
totp_cli_print_invalid_arguments();
return;
}

FuriString* temp_str = furi_string_alloc();
bool confirm_needed = true;
if (args_read_string_and_trim(args, temp_str)) {
if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX) == 0) {
if(args_read_string_and_trim(args, temp_str)) {
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX) == 0) {
confirm_needed = false;
} else {
TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
Expand All @@ -38,7 +45,7 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,
}
furi_string_free(temp_str);

if (!totp_cli_ensure_authenticated(plugin_state, cli)) {
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
return;
}

Expand All @@ -47,40 +54,39 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,
TokenInfo* token_info = list_node->data;

bool confirmed = !confirm_needed;
if (confirm_needed) {
if(confirm_needed) {
TOTP_CLI_PRINTF("WARNING!\r\n");
TOTP_CLI_PRINTF("TOKEN \"%s\" WILL BE PERMANENTLY DELETED WITHOUT ABILITY TO RECOVER IT.\r\n", token_info->name);
TOTP_CLI_PRINTF(
"TOKEN \"%s\" WILL BE PERMANENTLY DELETED WITHOUT ABILITY TO RECOVER IT.\r\n",
token_info->name);
TOTP_CLI_PRINTF("Confirm? [y/n]\r\n");
fflush(stdout);
char user_pick;
do {
user_pick = tolower(cli_getc(cli));
} while (user_pick != 'y' &&
user_pick != 'n' &&
user_pick != CliSymbolAsciiCR &&
user_pick != CliSymbolAsciiETX &&
user_pick != CliSymbolAsciiEsc);
} while(user_pick != 'y' && user_pick != 'n' && user_pick != CliSymbolAsciiCR &&
user_pick != CliSymbolAsciiETX && user_pick != CliSymbolAsciiEsc);

confirmed = user_pick == 'y' || user_pick == CliSymbolAsciiCR;
}

if (confirmed) {
if (!totp_cli_ensure_authenticated(plugin_state, cli)) {
if(confirmed) {
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
return;
}

bool activate_generate_token_scene = false;
if (plugin_state->current_scene != TotpSceneAuthentication) {
if(plugin_state->current_scene != TotpSceneAuthentication) {
totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
activate_generate_token_scene = true;
}

plugin_state->tokens_list = list_remove(plugin_state->tokens_list, list_node);
plugin_state->tokens_count--;

totp_full_save_config_file(plugin_state);
if (activate_generate_token_scene) {

if(activate_generate_token_scene) {
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
}

Expand Down
13 changes: 9 additions & 4 deletions services/cli/commands/list/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ void totp_cli_command_list_print_help() {
}

void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
if (!totp_cli_ensure_authenticated(plugin_state, cli)) {
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
return;
}

if (plugin_state->tokens_list == NULL) {
if(plugin_state->tokens_list == NULL) {
TOTP_CLI_PRINTF("There are no tokens");
return;
}
Expand All @@ -50,9 +50,14 @@ void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
uint16_t index = 1;
while(node != NULL) {
TokenInfo* token_info = (TokenInfo* )node->data;
TokenInfo* token_info = (TokenInfo*)node->data;
token_info_get_digits_count(token_info);
TOTP_CLI_PRINTF("| %-3" PRIu16 " | %-27.27s | %-6s | %-6" PRIu8 " |\r\n", index, token_info->name, get_algo_as_cstr(token_info->algo), get_digits_as_int(token_info->digits));
TOTP_CLI_PRINTF(
"| %-3" PRIu16 " | %-27.27s | %-6s | %-6" PRIu8 " |\r\n",
index,
token_info->name,
get_algo_as_cstr(token_info->algo),
get_digits_as_int(token_info->digits));
node = node->next;
index++;
}
Expand Down
Loading

0 comments on commit 1a51c11

Please sign in to comment.