]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Don't remove newlines when parsing config, menu and XBM
authortokyo4j <hrak1529@gmail.com>
Wed, 15 Oct 2025 07:36:01 +0000 (16:36 +0900)
committerHiroaki Yamamoto <hrak1529@gmail.com>
Fri, 17 Oct 2025 13:08:41 +0000 (22:08 +0900)
Removing newlines in rc.xml and menu.xml caused parser error with
following content:

<!--
 -
 - Some comments
 -
-->

...though it is a valid XML.

Let's not do that. I moved `grab_file()` to `buf.c` and renamed it to
`buf_from_file()`, because it now directly touches `struct buf` and
I don't like having a source file only for one function.

include/common/buf.h
include/common/grab-file.h [deleted file]
src/common/buf.c
src/common/grab-file.c [deleted file]
src/common/meson.build
src/config/rcxml.c
src/img/img-xbm.c
src/menu/menu.c

index 24158630884f89f4042a5f6aadb9e165c66463ad..1298cac20f6b48e36551b5887e8ed77b6a55e417 100644 (file)
@@ -109,4 +109,11 @@ void buf_reset(struct buf *s);
  */
 void buf_move(struct buf *dst, struct buf *src);
 
+/**
+ * buf_from_file - read file into memory buffer
+ * @filename: file to read
+ * Free returned buffer with buf_reset().
+ */
+struct buf buf_from_file(const char *filename);
+
 #endif /* LABWC_BUF_H */
diff --git a/include/common/grab-file.h b/include/common/grab-file.h
deleted file mode 100644 (file)
index 6a6de56..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-/*
- * Read file into memory
- *
- * Copyright Johan Malm 2020
- */
-
-#ifndef LABWC_GRAB_FILE_H
-#define LABWC_GRAB_FILE_H
-
-#include "common/buf.h"
-
-/**
- * grab_file - read file into memory buffer
- * @filename: file to read
- * Free returned buffer with buf_reset().
- */
-struct buf grab_file(const char *filename);
-
-#endif /* LABWC_GRAB_FILE_H */
index bd8e82d0f03e4b79467496b62a508ee1386c91aa..c141b62e3f8db60fe5c676a76ac4bd87e76c734a 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
+#include <wlr/util/log.h>
 #include "common/macros.h"
 #include "common/mem.h"
 #include "common/string-helpers.h"
@@ -203,3 +204,37 @@ buf_move(struct buf *dst, struct buf *src)
        *dst = *src;
        *src = BUF_INIT;
 }
+
+struct buf
+buf_from_file(const char *filename)
+{
+       struct buf buf = BUF_INIT;
+       FILE *stream = fopen(filename, "r");
+       if (!stream) {
+               return buf;
+       }
+
+       if (fseek(stream, 0, SEEK_END) == -1) {
+               wlr_log_errno(WLR_ERROR, "fseek(%s)", filename);
+               fclose(stream);
+               return buf;
+       }
+       long size = ftell(stream);
+       if (size == -1) {
+               wlr_log_errno(WLR_ERROR, "ftell(%s)", filename);
+               fclose(stream);
+               return buf;
+       }
+       rewind(stream);
+
+       buf_expand(&buf, size + 1);
+       if (fread(buf.data, 1, size, stream) == (size_t)size) {
+               buf.len = size;
+               buf.data[size] = '\0';
+       } else {
+               wlr_log_errno(WLR_ERROR, "fread(%s)", filename);
+               buf_reset(&buf);
+       }
+       fclose(stream);
+       return buf;
+}
diff --git a/src/common/grab-file.c b/src/common/grab-file.c
deleted file mode 100644 (file)
index 9c90045..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/*
- * Read file into memory
- *
- * Copyright Johan Malm 2020
- */
-
-#define _POSIX_C_SOURCE 200809L
-#include "common/grab-file.h"
-#include "common/buf.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-struct buf
-grab_file(const char *filename)
-{
-       char *line = NULL;
-       size_t len = 0;
-       FILE *stream = fopen(filename, "r");
-       if (!stream) {
-               return BUF_INIT;
-       }
-       struct buf buffer = BUF_INIT;
-       while ((getline(&line, &len, stream) != -1)) {
-               char *p = strrchr(line, '\n');
-               if (p) {
-                       *p = '\0';
-               }
-               buf_add(&buffer, line);
-       }
-       free(line);
-       fclose(stream);
-       return buffer;
-}
index 39b4d4b4dbead4eda081f68100b5136be2154560..4cf52023907b86e377d087a40b6abd12de8c97f1 100644 (file)
@@ -6,7 +6,6 @@ labwc_sources += files(
   'fd-util.c',
   'file-helpers.c',
   'font.c',
-  'grab-file.c',
   'graphic-helpers.c',
   'lab-scene-rect.c',
   'match.c',
index 6d0caf80edb215d0dda25630fea3cc0bb491347c..a966577cd3b48fee99f7ef13a481dbad918ae706 100644 (file)
@@ -1872,25 +1872,13 @@ rcxml_read(const char *filename)
         */
        for (struct wl_list *elm = iter(&paths); elm != &paths; elm = iter(elm)) {
                struct path *path = wl_container_of(elm, path, link);
-               FILE *stream = fopen(path->string, "r");
-               if (!stream) {
+               struct buf b = buf_from_file(path->string);
+               if (!b.len) {
                        continue;
                }
 
                wlr_log(WLR_INFO, "read config file %s", path->string);
 
-               struct buf b = BUF_INIT;
-               char *line = NULL;
-               size_t len = 0;
-               while (getline(&line, &len, stream) != -1) {
-                       char *p = strrchr(line, '\n');
-                       if (p) {
-                               *p = '\0';
-                       }
-                       buf_add(&b, line);
-               }
-               zfree(line);
-               fclose(stream);
                rcxml_parse_xml(&b);
                buf_reset(&b);
                if (!should_merge_config) {
index 6ecbbc9a33855c464ce8b47b9a949b0b6e06681a..91269f64474207a6724a3a197a2591ea75479467 100644 (file)
@@ -12,7 +12,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include "common/grab-file.h"
+#include "common/buf.h"
 #include "common/mem.h"
 #include "common/string-helpers.h"
 #include "buffer.h"
@@ -273,7 +273,7 @@ img_xbm_load(const char *filename, float *rgba)
        uint32_t color = argb32(rgba);
 
        /* Read file into memory as it's easier to tokenize that way */
-       struct buf token_buf = grab_file(filename);
+       struct buf token_buf = buf_from_file(filename);
        if (token_buf.len) {
                struct token *tokens = tokenize_xbm(token_buf.data);
                pixmap = parse_xbm_tokens(tokens, color);
index 3770e7f6f603289ac595306a6319d7b2545eb633..bc0fdcad4fdaa2aedc6faa3f0f3290bb0cede56f 100644 (file)
@@ -679,30 +679,6 @@ parse_buf(struct server *server, struct menu *parent, struct buf *buf)
        return true;
 }
 
-/*
- * @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_stream(struct server *server, FILE *stream)
-{
-       char *line = NULL;
-       size_t len = 0;
-       struct buf b = BUF_INIT;
-
-       while (getline(&line, &len, stream) != -1) {
-               char *p = strrchr(line, '\n');
-               if (p) {
-                       *p = '\0';
-               }
-               buf_add(&b, line);
-       }
-       free(line);
-       parse_buf(server, NULL, &b);
-       buf_reset(&b);
-}
-
 static void
 parse_xml(const char *filename, struct server *server)
 {
@@ -715,13 +691,13 @@ parse_xml(const char *filename, struct server *server)
 
        for (struct wl_list *elm = iter(&paths); elm != &paths; elm = iter(elm)) {
                struct path *path = wl_container_of(elm, path, link);
-               FILE *stream = fopen(path->string, "r");
-               if (!stream) {
+               struct buf buf = buf_from_file(path->string);
+               if (!buf.len) {
                        continue;
                }
                wlr_log(WLR_INFO, "read menu file %s", path->string);
-               parse_stream(server, stream);
-               fclose(stream);
+               parse_buf(server, /*parent*/ NULL, &buf);
+               buf_reset(&buf);
                if (!should_merge_config) {
                        break;
                }