mirror of
https://github.com/swaywm/sway.git
synced 2025-01-11 18:39:43 +00:00
Merge pull request #2941 from ianyfan/commands
commands: clean up checkargs function +other small changes
This commit is contained in:
commit
e33671fd9a
|
@ -39,9 +39,8 @@ struct cmd_results {
|
||||||
};
|
};
|
||||||
|
|
||||||
enum expected_args {
|
enum expected_args {
|
||||||
EXPECTED_MORE_THAN,
|
|
||||||
EXPECTED_AT_LEAST,
|
EXPECTED_AT_LEAST,
|
||||||
EXPECTED_LESS_THAN,
|
EXPECTED_AT_MOST,
|
||||||
EXPECTED_EQUAL_TO
|
EXPECTED_EQUAL_TO
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -18,41 +18,28 @@
|
||||||
|
|
||||||
// Returns error object, or NULL if check succeeds.
|
// Returns error object, or NULL if check succeeds.
|
||||||
struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) {
|
struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) {
|
||||||
struct cmd_results *error = NULL;
|
const char *error_name = NULL;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case EXPECTED_MORE_THAN:
|
|
||||||
if (argc > val) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
|
||||||
"(expected more than %d argument%s, got %d)",
|
|
||||||
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
|
||||||
break;
|
|
||||||
case EXPECTED_AT_LEAST:
|
case EXPECTED_AT_LEAST:
|
||||||
if (argc >= val) {
|
if (argc < val) {
|
||||||
return NULL;
|
error_name = "at least ";
|
||||||
}
|
}
|
||||||
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
|
||||||
"(expected at least %d argument%s, got %d)",
|
|
||||||
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
|
||||||
break;
|
break;
|
||||||
case EXPECTED_LESS_THAN:
|
case EXPECTED_AT_MOST:
|
||||||
if (argc < val) {
|
if (argc > val) {
|
||||||
return NULL;
|
error_name = "at most ";
|
||||||
};
|
}
|
||||||
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
|
||||||
"(expected less than %d argument%s, got %d)",
|
|
||||||
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
|
||||||
break;
|
break;
|
||||||
case EXPECTED_EQUAL_TO:
|
case EXPECTED_EQUAL_TO:
|
||||||
if (argc == val) {
|
if (argc != val) {
|
||||||
return NULL;
|
error_name = "";
|
||||||
};
|
}
|
||||||
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
|
||||||
"(expected %d arguments, got %d)", name, val, argc);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return error;
|
return error_name ?
|
||||||
|
cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
||||||
|
"(expected %s%d argument%s, got %d)",
|
||||||
|
name, error_name, val, val != 1 ? "s" : "", argc)
|
||||||
|
: NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void apply_seat_config(struct seat_config *seat_config) {
|
void apply_seat_config(struct seat_config *seat_config) {
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
struct cmd_results *bar_cmd_bindsym(int argc, char **argv) {
|
struct cmd_results *bar_cmd_bindsym(int argc, char **argv) {
|
||||||
struct cmd_results *error = NULL;
|
struct cmd_results *error = NULL;
|
||||||
if ((error = checkarg(argc, "bar bindsym", EXPECTED_MORE_THAN, 1))) {
|
if ((error = checkarg(argc, "bar bindsym", EXPECTED_AT_LEAST, 2))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
if (!config->current_bar) {
|
if (!config->current_bar) {
|
||||||
|
|
|
@ -40,7 +40,7 @@ struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) {
|
||||||
if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_LEAST, 1))) {
|
if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_LEAST, 1))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
if ((error = checkarg(argc, "hidden_state", EXPECTED_LESS_THAN, 3))) {
|
if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_MOST, 2))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
if (config->reading && argc > 1) {
|
if (config->reading && argc > 1) {
|
||||||
|
|
|
@ -41,7 +41,7 @@ struct cmd_results *bar_cmd_mode(int argc, char **argv) {
|
||||||
if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) {
|
if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
if ((error = checkarg(argc, "mode", EXPECTED_LESS_THAN, 3))) {
|
if ((error = checkarg(argc, "mode", EXPECTED_AT_MOST, 2))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
if (config->reading && argc > 1) {
|
if (config->reading && argc > 1) {
|
||||||
|
|
|
@ -145,7 +145,7 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
|
||||||
const char *bindtype = bindcode ? "bindcode" : "bindsym";
|
const char *bindtype = bindcode ? "bindcode" : "bindsym";
|
||||||
|
|
||||||
struct cmd_results *error = NULL;
|
struct cmd_results *error = NULL;
|
||||||
if ((error = checkarg(argc, bindtype, EXPECTED_MORE_THAN, 1))) {
|
if ((error = checkarg(argc, bindtype, EXPECTED_AT_LEAST, 2))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
struct cmd_results *cmd_exec_always(int argc, char **argv) {
|
struct cmd_results *cmd_exec_always(int argc, char **argv) {
|
||||||
struct cmd_results *error = NULL;
|
struct cmd_results *error = NULL;
|
||||||
if (!config->active) return cmd_results_new(CMD_DEFER, NULL, NULL);
|
if (!config->active) return cmd_results_new(CMD_DEFER, NULL, NULL);
|
||||||
if ((error = checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0))) {
|
if ((error = checkarg(argc, argv[-1], EXPECTED_AT_LEAST, 1))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
|
||||||
if (strcmp(argv[0], "--no-startup-id") == 0) {
|
if (strcmp(argv[0], "--no-startup-id") == 0) {
|
||||||
wlr_log(WLR_INFO, "exec switch '--no-startup-id' not supported, ignored.");
|
wlr_log(WLR_INFO, "exec switch '--no-startup-id' not supported, ignored.");
|
||||||
--argc; ++argv;
|
--argc; ++argv;
|
||||||
if ((error = checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0))) {
|
if ((error = checkarg(argc, argv[-1], EXPECTED_AT_LEAST, 1))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
|
||||||
} else if (pid < 0) {
|
} else if (pid < 0) {
|
||||||
close(fd[0]);
|
close(fd[0]);
|
||||||
close(fd[1]);
|
close(fd[1]);
|
||||||
return cmd_results_new(CMD_FAILURE, "exec_always", "fork() failed");
|
return cmd_results_new(CMD_FAILURE, argv[-1], "fork() failed");
|
||||||
}
|
}
|
||||||
close(fd[1]); // close write
|
close(fd[1]); // close write
|
||||||
ssize_t s = 0;
|
ssize_t s = 0;
|
||||||
|
@ -85,7 +85,7 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
|
||||||
wlr_log(WLR_DEBUG, "Child process created with pid %d", child);
|
wlr_log(WLR_DEBUG, "Child process created with pid %d", child);
|
||||||
root_record_workspace_pid(child);
|
root_record_workspace_pid(child);
|
||||||
} else {
|
} else {
|
||||||
return cmd_results_new(CMD_FAILURE, "exec_always",
|
return cmd_results_new(CMD_FAILURE, argv[-1],
|
||||||
"Second fork() failed");
|
"Second fork() failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
struct cmd_results *cmd_fullscreen(int argc, char **argv) {
|
struct cmd_results *cmd_fullscreen(int argc, char **argv) {
|
||||||
struct cmd_results *error = NULL;
|
struct cmd_results *error = NULL;
|
||||||
if ((error = checkarg(argc, "fullscreen", EXPECTED_LESS_THAN, 2))) {
|
if ((error = checkarg(argc, "fullscreen", EXPECTED_AT_MOST, 1))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
struct sway_node *node = config->handler_context.node;
|
struct sway_node *node = config->handler_context.node;
|
||||||
|
|
|
@ -96,7 +96,7 @@ static enum sway_container_layout get_layout(int argc, char **argv,
|
||||||
|
|
||||||
struct cmd_results *cmd_layout(int argc, char **argv) {
|
struct cmd_results *cmd_layout(int argc, char **argv) {
|
||||||
struct cmd_results *error = NULL;
|
struct cmd_results *error = NULL;
|
||||||
if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) {
|
if ((error = checkarg(argc, "layout", EXPECTED_AT_LEAST, 1))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
struct sway_container *container = config->handler_context.container;
|
struct sway_container *container = config->handler_context.container;
|
||||||
|
|
|
@ -15,7 +15,7 @@ static bool parse_opacity(const char *opacity, float *val) {
|
||||||
|
|
||||||
struct cmd_results *cmd_opacity(int argc, char **argv) {
|
struct cmd_results *cmd_opacity(int argc, char **argv) {
|
||||||
struct cmd_results *error = NULL;
|
struct cmd_results *error = NULL;
|
||||||
if ((error = checkarg(argc, "layout", EXPECTED_EQUAL_TO, 1))) {
|
if ((error = checkarg(argc, "opacity", EXPECTED_EQUAL_TO, 1))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue