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

@ -117,20 +117,24 @@ IPC_HANDLER(command) {
char *command = scalloc(message_size + 1);
strncpy(command, (const char*)message, message_size);
LOG("IPC: received: *%s*\n", command);
struct CommandResultIR *command_output = parse_command((const char*)command);
yajl_gen gen = yajl_gen_alloc(NULL);
CommandResult *result = parse_command((const char*)command, gen);
free(command);
if (command_output->needs_tree_render)
if (result->needs_tree_render)
tree_render();
command_result_free(result);
const unsigned char *reply;
ylength length;
yajl_gen_get_buf(command_output->json_gen, &reply, &length);
yajl_gen_get_buf(gen, &reply, &length);
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_COMMAND,
(const uint8_t*)reply);
yajl_gen_free(command_output->json_gen);
yajl_gen_free(gen);
}
static void dump_rect(yajl_gen gen, const char *name, Rect r) {