Skip to content

Commit

Permalink
Remove multiple assignment from e and allow spaces around '='.
Browse files Browse the repository at this point in the history
  • Loading branch information
treseco committed Apr 20, 2024
1 parent 3033f62 commit 5ed6a5e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 50 deletions.
70 changes: 34 additions & 36 deletions librz/core/cmd/cmd_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,45 +365,43 @@ RZ_IPI RzCmdStatus rz_cmd_eval_color_highlight_remove_current_handler(RzCore *co
}

RZ_IPI RzCmdStatus rz_eval_getset_handler(RzCore *core, int argc, const char **argv) {
int i;
for (i = 1; i < argc; i++) {
RzList *l = rz_str_split_duplist_n(argv[i], "=", 1, false);
if (!l) {
return RZ_CMD_STATUS_ERROR;
}
size_t llen = rz_list_length(l);
if (!llen) {
return RZ_CMD_STATUS_ERROR;
}
char *key = rz_list_get_n(l, 0);
if (RZ_STR_ISEMPTY(key)) {
RZ_LOG_ERROR("core: No string specified before `=`. Make sure to use the format <key>=<value> without spaces.\n");
rz_list_free(l);
continue;
}
RzList *l = rz_str_split_duplist_n(argv[1], "=", 1, true);
if (!l) {
return RZ_CMD_STATUS_ERROR;
}
size_t llen = rz_list_length(l);
if (!llen) {
rz_list_free(l);
return RZ_CMD_STATUS_ERROR;
}
char *key = rz_list_get_n(l, 0);
if (RZ_STR_ISEMPTY(key)) {
RZ_LOG_ERROR("core: No string specified before `=`. Make sure to use the format <key>=<value>.\n");
rz_list_free(l);
return RZ_CMD_STATUS_ERROR;
}

if (llen == 1 && rz_str_endswith(key, ".")) {
// no value was set, only key with ".". List possible sub-keys.
RzCmdStateOutput state = { 0 };
rz_cmd_state_output_init(&state, RZ_OUTPUT_MODE_QUIET);
rz_core_config_print_all(core->config, key, &state);
rz_cmd_state_output_print(&state);
rz_cmd_state_output_fini(&state);
} else if (llen == 1) {
// no value was set, show the value of the key
const char *v = rz_config_get(core->config, key);
if (!v) {
RZ_LOG_ERROR("core: Invalid config key '%s'\n", key);
rz_list_free(l);
return RZ_CMD_STATUS_ERROR;
}
rz_cons_printf("%s\n", v);
} else if (llen == 2) {
char *value = rz_list_get_n(l, 1);
rz_config_set(core->config, key, value);
if (llen == 1 && rz_str_endswith(key, ".")) {
// no value was set, only key with ".". List possible sub-keys.
RzCmdStateOutput state = { 0 };
rz_cmd_state_output_init(&state, RZ_OUTPUT_MODE_QUIET);
rz_core_config_print_all(core->config, key, &state);
rz_cmd_state_output_print(&state);
rz_cmd_state_output_fini(&state);
} else if (llen == 1) {
// no value was set, show the value of the key
const char *v = rz_config_get(core->config, key);
if (!v) {
RZ_LOG_ERROR("core: Invalid config key '%s'\n", key);
rz_list_free(l);
return RZ_CMD_STATUS_ERROR;
}
rz_list_free(l);
rz_cons_printf("%s\n", v);
} else if (llen == 2) {
char *value = rz_list_get_n(l, 1);
rz_config_set(core->config, key, value);
}
rz_list_free(l);
return RZ_CMD_STATUS_OK;
}

Expand Down
4 changes: 2 additions & 2 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9946,14 +9946,14 @@ static const RzCmdDescArg eval_getset_args[] = {
{
.name = "key=value",
.type = RZ_CMD_ARG_TYPE_EVAL_FULL,
.flags = RZ_CMD_ARG_FLAG_ARRAY,
.flags = RZ_CMD_ARG_FLAG_LAST,

},
{ 0 },
};
static const RzCmdDescHelp eval_getset_help = {
.summary = "Get/Set value of config variable <key>",
.args_str = " <key>[=<val|?>] [<key>[=<val|?>] ...]]",
.args_str = " <key> [= <val|?>]",
.details = eval_getset_details,
.args = eval_getset_args,
};
Expand Down
4 changes: 2 additions & 2 deletions librz/core/cmd_descs/cmd_eval.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ commands:
- name: e
cname: eval_getset
summary: Get/Set value of config variable <key>
args_str: " <key>[=<val|?>] [<key>[=<val|?>] ...]]"
args_str: " <key> [= <val|?>]"
args:
- name: key=value
type: RZ_CMD_ARG_TYPE_EVAL_FULL
flags: RZ_CMD_ARG_FLAG_ARRAY
flags: RZ_CMD_ARG_FLAG_LAST
details:
- name: Examples
entries:
Expand Down
20 changes: 10 additions & 10 deletions test/db/cmd/cmd_help
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,16 @@ Detailed help for w <string> is provided by w??.
----

Usage: e[?] # List/get/set config evaluable vars
| e <key>[=<val|?>] [<key>[=<val|?>] ...]] # Get/Set value of config variable <key>
| el[j*qlJ] [<key>] # List config variables with their descriptions
| e- # Reset config variables
| e! <key> # Invert the boolean value of config variable <key>
| ec[?] # Set color for given key (prompt, offset, ...)
| ee <key> # Open editor to change the value of config variable <key>
| er <key> # Set config variable <key> as read-only
| es [<key>] # List all config variable spaces or sub-keys/sub-spaces if a <key> is provided
| et <key> # Show type of given config variable <key>
| e <key> [= <val|?>] # Get/Set value of config variable <key>
| el[j*qlJ] [<key>] # List config variables with their descriptions
| e- # Reset config variables
| e! <key> # Invert the boolean value of config variable <key>
| ec[?] # Set color for given key (prompt, offset, ...)
| ee <key> # Open editor to change the value of config variable <key>
| er <key> # Set config variable <key> as read-only
| es [<key>] # List all config variable spaces or sub-keys/sub-spaces if a <key> is provided
| et <key> # Show type of given config variable <key>

Detailed help for e <key>[=<val|?>] [<key>[=<val|?>] ...]] is provided by e??.
Detailed help for e <key> [= <val|?>] is provided by e??.
EOF
RUN

0 comments on commit 5ed6a5e

Please sign in to comment.