Refactor parse_command

parse_command returns a struct that contains useful information about
the result of a command as a whole (instead of the intermediate
representation used during parsing).

parse_command now requires the caller to allocate the yajl_gen used for
generating a json reply. This is passed as the second parameter to
parse_command. If NULL is passed, no json reply will be generated.
This commit is contained in:
Tony Crisci
2014-05-28 02:01:50 -04:00
committed by Michael Stapelberg
parent 62ea60ba42
commit c7aae56030
6 changed files with 99 additions and 32 deletions

View File

@ -17,7 +17,7 @@
* internally use this struct when calling cmd_floating and cmd_border.
*/
struct CommandResultIR {
/* The JSON generator to append a reply to. */
/* The JSON generator to append a reply to (may be NULL). */
yajl_gen json_gen;
/* The next state to transition to. Passed to the function so that we can
@ -29,4 +29,31 @@ struct CommandResultIR {
bool needs_tree_render;
};
struct CommandResultIR *parse_command(const char *input);
typedef struct CommandResult CommandResult;
/**
* A struct that contains useful information about the result of a command as a
* whole (e.g. a compound command like "floating enable, border none").
* needs_tree_render is true if needs_tree_render of any individual command was
* true.
*/
struct CommandResult {
bool parse_error;
/* the error_message is currently only set for parse errors */
char *error_message;
bool needs_tree_render;
};
/**
* Parses and executes the given command. If a caller-allocated yajl_gen is
* passed, a json reply will be generated in the format specified by the ipc
* protocol. Pass NULL if no json reply is required.
*
* Free the returned CommandResult with command_result_free().
*/
CommandResult *parse_command(const char *input, yajl_gen gen);
/**
* Frees a CommandResult
*/
void command_result_free(CommandResult *result);