ipc/parser: commands can now return custom JSON replies

Also, finally add include/cmdparse.h
This commit is contained in:
Michael Stapelberg 2010-07-17 15:15:37 +02:00
parent 189635a5dc
commit d066341261
4 changed files with 21 additions and 7 deletions

View File

@ -49,5 +49,6 @@
#include "render.h"
#include "window.h"
#include "match.h"
#include "cmdparse.h"
#endif

6
include/cmdparse.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _CMDPARSE_H
#define _CMDPARSE_H
char *parse_cmd(const char *new);
#endif

View File

@ -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 dont 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);
}
;

View File

@ -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));
}