From c827c4c9e5d1658f991d48b917a49c624e724ef6 Mon Sep 17 00:00:00 2001 From: Johan Malm Date: Mon, 9 Oct 2023 20:43:43 +0100 Subject: [PATCH] menu: split parse_xml() in readiness for pipemenus --- src/menu/menu.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/menu/menu.c b/src/menu/menu.c index 5c7712f1..c51141c4 100644 --- a/src/menu/menu.c +++ b/src/menu/menu.c @@ -524,26 +524,18 @@ xml_tree_walk(xmlNode *node, struct server *server) } } +/* + * @stream can come from either of the following: + * - fopen() in the case of reading a file such as menu.xml + * - popen() when processing pipemenus + */ static void -parse_xml(const char *filename, struct server *server) +parse(struct server *server, FILE *stream) { - FILE *stream; char *line = NULL; size_t len = 0; struct buf b; - static char menuxml[4096] = { 0 }; - - if (!rc.config_dir) { - return; - } - snprintf(menuxml, sizeof(menuxml), "%s/%s", rc.config_dir, filename); - stream = fopen(menuxml, "r"); - if (!stream) { - wlr_log(WLR_ERROR, "cannot read %s", menuxml); - return; - } - wlr_log(WLR_INFO, "read menu file %s", menuxml); buf_init(&b); while (getline(&line, &len, stream) != -1) { char *p = strrchr(line, '\n'); @@ -553,7 +545,6 @@ parse_xml(const char *filename, struct server *server) buf_add(&b, line); } free(line); - fclose(stream); xmlDoc *d = xmlParseMemory(b.buf, b.len); if (!d) { wlr_log(WLR_ERROR, "xmlParseMemory()"); @@ -566,6 +557,26 @@ err: free(b.buf); } +static void +parse_xml(const char *filename, struct server *server) +{ + static char buf[4096] = { 0 }; + + if (!rc.config_dir) { + return; + } + snprintf(buf, sizeof(buf), "%s/%s", rc.config_dir, filename); + + FILE *stream = fopen(buf, "r"); + if (!stream) { + wlr_log(WLR_ERROR, "cannot read %s", buf); + return; + } + wlr_log(WLR_INFO, "read menu file %s", buf); + parse(server, stream); + fclose(stream); +} + static int menu_get_full_width(struct menu *menu) { -- 2.52.0