From af30a1b67c22aa54dad4e1a0ee3aacb537c4ab92 Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Thu, 22 Oct 2015 14:14:13 +0200 Subject: ipc,commands,config: Replace cmd_status enum with cmd_results struct. In i3 the ipc reply will contain a human readable error message, and this patch replicates that behaviour. However, that error message is also useful for logging, which this patch takes advantage of. E.g. instead of logging errors directly in commands.c/checkargs, it is fed back to the caller which eventually ends up logging everything with maximum context available (config.c/read_config). So instead of logging e.g. "Error on line 'exit'" it will now log: "Error on line 'exit': Can't execute from config." --- include/commands.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/commands.h b/include/commands.h index 1b4cd9ca..1e0a1452 100644 --- a/include/commands.h +++ b/include/commands.h @@ -1,22 +1,34 @@ #ifndef _SWAY_COMMANDS_H #define _SWAY_COMMANDS_H #include +#include #include "config.h" enum cmd_status { CMD_SUCCESS, - CMD_FAILURE, - CMD_INVALID, + CMD_FAILURE, // was or at least could be executed + CMD_INVALID, // unknown or parse error CMD_DEFER, // Config Blocks CMD_BLOCK_END, CMD_BLOCK_MODE, }; -enum cmd_status handle_command(char *command); +struct cmd_results { + enum cmd_status status; + + const char *input; + char *error; +}; + +struct cmd_results *handle_command(char *command); // Handles commands during config -enum cmd_status config_command(char *command); +struct cmd_results *config_command(char *command); + +struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *error, ...); +void free_cmd_results(struct cmd_results *results); +const char *cmd_results_to_json(struct cmd_results *results); void remove_view_from_scratchpad(); -- cgit v1.2.3-54-g00ecf From 362413bd5031aa709ab5d7a39c0d5912554e545b Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Fri, 23 Oct 2015 13:03:14 +0200 Subject: commands: cmd_results->input is duplicated/freed. --- include/commands.h | 3 +-- sway/commands.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/commands.h b/include/commands.h index 1e0a1452..8e53c74d 100644 --- a/include/commands.h +++ b/include/commands.h @@ -17,8 +17,7 @@ enum cmd_status { struct cmd_results { enum cmd_status status; - - const char *input; + char *input; char *error; }; diff --git a/sway/commands.c b/sway/commands.c index d742495d..8c45dabe 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -1249,7 +1249,7 @@ struct cmd_results *handle_command(char *_exec) { if (results) { free_cmd_results(results); } - results = cmd_results_new(CMD_INVALID, strdup(cmd), "Unknown/invalid command"); + results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command"); free_argv(argc, argv); goto cleanup; } @@ -1291,7 +1291,7 @@ struct cmd_results *config_command(char *exec) { } struct cmd_handler *handler = find_handler(argv[0]); if (!handler) { - char *input = argv[0] ? strdup(argv[0]) : "(empty)"; + char *input = argv[0] ? argv[0] : "(empty)"; results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); goto cleanup; } @@ -1314,7 +1314,11 @@ struct cmd_results *config_command(char *exec) { struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) { struct cmd_results *results = malloc(sizeof(struct cmd_results)); results->status = status; - results->input = input; // input is the command name + if (input) { + results->input = strdup(input); // input is the command name + } else { + results->input = NULL; + } if (format) { char *error = malloc(256); va_list args; @@ -1329,6 +1333,9 @@ struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, c } void free_cmd_results(struct cmd_results *results) { + if (results->input) { + free(results->input); + } if (results->error) { free(results->error); } -- cgit v1.2.3-54-g00ecf