From: Michael Stapelberg Date: Sat, 17 Jul 2010 13:15:37 +0000 (+0200) Subject: ipc/parser: commands can now return custom JSON replies X-Git-Tag: tree-pr1~147 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=d066341261ea0ca247df75b07b01c766222c4b0a;p=i3%2Fi3 ipc/parser: commands can now return custom JSON replies Also, finally add include/cmdparse.h --- diff --git a/include/all.h b/include/all.h index 2890b9c6..5851141e 100644 --- a/include/all.h +++ b/include/all.h @@ -49,5 +49,6 @@ #include "render.h" #include "window.h" #include "match.h" +#include "cmdparse.h" #endif diff --git a/include/cmdparse.h b/include/cmdparse.h new file mode 100644 index 00000000..09d56bae --- /dev/null +++ b/include/cmdparse.h @@ -0,0 +1,6 @@ +#ifndef _CMDPARSE_H +#define _CMDPARSE_H + +char *parse_cmd(const char *new); + +#endif diff --git a/src/cmdparse.y b/src/cmdparse.y index f4ad6e07..6718e8b2 100644 --- a/src/cmdparse.y +++ b/src/cmdparse.y @@ -37,6 +37,10 @@ typedef struct owindow { } owindow; static TAILQ_HEAD(owindows_head, owindow) owindows; +/* Holds the JSON which will be returned via IPC or NULL for the default return + * message */ +static char *json_output; + /* We don’t need yydebug for now, as we got decent error messages using * yyerror(). Should you ever want to extend the parser, it might be handy * to just comment it in again, so it stays here. */ @@ -61,7 +65,7 @@ int cmdyywrap() { return 1; } -void parse_cmd(const char *new) { +char *parse_cmd(const char *new) { //const char *new = "[level-up workspace] attach $output, focus"; @@ -69,14 +73,16 @@ void parse_cmd(const char *new) { context = scalloc(sizeof(struct context)); context->filename = "cmd"; + FREE(json_output); if (cmdyyparse() != 0) { fprintf(stderr, "Could not parse configfile\n"); exit(1); } - printf("done\n"); + printf("done, json output = %s\n", json_output); FREE(context->line_copy); free(context); + return json_output; } %} @@ -392,7 +398,8 @@ open: TOK_OPEN { printf("opening new container\n"); - tree_open_con(NULL); + Con *con = tree_open_con(NULL); + asprintf(&json_output, "{\"success\":true, \"id\":%d}", (long int)con); } ; diff --git a/src/ipc.c b/src/ipc.c index 7c0a0f37..2c105acc 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -115,13 +115,13 @@ IPC_HANDLER(command) { char *command = scalloc(message_size + 1); strncpy(command, (const char*)message, message_size); LOG("IPC: received: *%s*\n", command); - parse_cmd((const char*)command); + const char *reply = parse_cmd((const char*)command); tree_render(); free(command); - /* For now, every command gets a positive acknowledge - * (will change with the new command parser) */ - const char *reply = "{\"success\":true}"; + /* If no reply was provided, we just use the default success message */ + if (reply == NULL) + reply = "{\"success\":true}"; ipc_send_message(fd, (const unsigned char*)reply, I3_IPC_REPLY_TYPE_COMMAND, strlen(reply)); }