From 58b3c730e28b12cfcc85a3845e0f163fd4684a03 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sat, 4 Jan 2014 21:39:13 +0100 Subject: [PATCH] return parse errors via IPC for append_layout --- include/load_layout.h | 2 +- src/commands.c | 15 ++++++++++++--- src/load_layout.c | 4 +++- src/tree.c | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/load_layout.h b/include/load_layout.h index 2458736f..0a5328fa 100644 --- a/include/load_layout.h +++ b/include/load_layout.h @@ -10,4 +10,4 @@ */ #pragma once -void tree_append_json(const char *filename); +void tree_append_json(const char *filename, char **errormsg); diff --git a/src/commands.c b/src/commands.c index d8eff106..7ad3aba0 100644 --- a/src/commands.c +++ b/src/commands.c @@ -869,7 +869,18 @@ void cmd_nop(I3_CMD, char *comment) { void cmd_append_layout(I3_CMD, char *path) { LOG("Appending layout \"%s\"\n", path); Con *parent = focused; - tree_append_json(path); + char *errormsg = NULL; + tree_append_json(path, &errormsg); + if (errormsg != NULL) { + yerror(errormsg); + free(errormsg); + /* Note that we continue executing since tree_append_json() has + * side-effects — user-provided layouts can be partly valid, partly + * invalid, leading to half of the placeholder containers being + * created. */ + } else { + ysuccess(true); + } // XXX: This is a bit of a kludge. Theoretically, render_con(parent, // false); should be enough, but when sending 'workspace 4; append_layout @@ -882,8 +893,6 @@ void cmd_append_layout(I3_CMD, char *path) { restore_open_placeholder_windows(parent); cmd_output->needs_tree_render = true; - // XXX: default reply for now, make this a better reply - ysuccess(true); } /* diff --git a/src/load_layout.c b/src/load_layout.c index fe21ee81..c95bc99f 100644 --- a/src/load_layout.c +++ b/src/load_layout.c @@ -384,7 +384,7 @@ static int json_double(void *ctx, double val) { return 1; } -void tree_append_json(const char *filename) { +void tree_append_json(const char *filename, char **errormsg) { FILE *f; if ((f = fopen(filename, "r")) == NULL) { LOG("Cannot open file \"%s\"\n", filename); @@ -441,6 +441,8 @@ void tree_append_json(const char *filename) { { unsigned char *str = yajl_get_error(hand, 1, (const unsigned char*)buf, n); ELOG("JSON parsing error: %s\n", str); + if (errormsg != NULL) + *errormsg = sstrdup((const char*)str); yajl_free_error(hand, str); /* In case not all containers were restored, we need to fix the diff --git a/src/tree.c b/src/tree.c index 836183ec..046d0b4c 100644 --- a/src/tree.c +++ b/src/tree.c @@ -84,7 +84,7 @@ bool tree_restore(const char *path, xcb_get_geometry_reply_t *geometry) { }; focused = croot; - tree_append_json(globbed); + tree_append_json(globbed, NULL); printf("appended tree, using new root\n"); croot = TAILQ_FIRST(&(croot->nodes_head));