diff mbox series

[20/22] tools/utils: add wrapper for readfile()/readdata() functions

Message ID 52d5a8411767aa17ce0db845471ad38dd49f6594.1690990428.git.ehem+xen@m5p.com (mailing list archive)
State New, archived
Headers show
Series Cleanup and splitting of xl.cfg parsing | expand

Commit Message

Elliott Mitchell July 26, 2023, 3:26 a.m. UTC
These functions needs to cross the boundary between core and lower-layer.
As such split them in two.  Pass most of the values from XLU_Config as
they can be used by the lower-layer.

Signed-off-by: Elliott Mitchell <ehem+xen@m5p.com>
---
 tools/libs/util/libxlu_cfg.c      | 47 ++++++++++++++++++++++---------
 tools/libs/util/libxlu_internal.h |  6 ++++
 2 files changed, 39 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/tools/libs/util/libxlu_cfg.c b/tools/libs/util/libxlu_cfg.c
index 3c0703f9df..cb99b0aab0 100644
--- a/tools/libs/util/libxlu_cfg.c
+++ b/tools/libs/util/libxlu_cfg.c
@@ -110,39 +110,58 @@  static void parse(CfgParseContext *ctx) {
     }
 }
 
-int xlu_cfg_readfile(XLU_Config *cfg, const char *real_filename) {
-    FILE *f = 0;
+int xlu_cfg_readfile(XLU_Config *cfg, const char *name)
+{
+    FILE *file;
+    int ret;
+
+    file = fopen(name, "r");
+    if (file) {
+        ret = xlu__cfg_readfile(cfg, cfg->report, cfg->config_source,
+            &cfg->settings, file, name);
+        fclose(file);
+    } else {
+        ret = errno;
+        fprintf(cfg->report,"%s: unable to open configuration file: %s\n",
+                name, strerror(ret));
+    }
+
+    return ret;
+}
+
+int xlu__cfg_readfile(XLU_Config *cfg, FILE *msgfile, const char *msgprefix,
+    XLU_ConfigSetting **psettings, FILE *f, const char *name)
+{
     int e;
 
     CfgParseContext ctx;
-    e = ctx_prep(&ctx, cfg, &cfg->settings);
+    e = ctx_prep(&ctx, cfg, psettings);
     if (e) { ctx.err= e; goto xe; }
 
-    f= fopen(real_filename, "r");
-    if (!f) {
-        ctx.err = errno;
-        fprintf(cfg->report,"%s: unable to open configuration file: %s\n",
-                real_filename, strerror(e));
-        goto xe;
-    }
-
     xlu__cfg_yyrestart(f, ctx.scanner);
 
     parse(&ctx);
 
  xe:
     ctx_dispose(&ctx);
-    if (f) fclose(f);
 
     return ctx.err;
 }
 
-int xlu_cfg_readdata(XLU_Config *cfg, const char *data, int length) {
+int xlu_cfg_readdata(XLU_Config *cfg, const char *data, int length)
+{
+    return xlu__cfg_readdata(cfg, cfg->report, cfg->config_source,
+        &cfg->settings, data, length);
+}
+
+int xlu__cfg_readdata(XLU_Config *cfg, FILE *msgfile, const char *msgprefix,
+    XLU_ConfigSetting **psettings, const char *data, int length)
+{
     int e;
     YY_BUFFER_STATE buf= 0;
 
     CfgParseContext ctx;
-    e = ctx_prep(&ctx, cfg, &cfg->settings);
+    e = ctx_prep(&ctx, cfg, psettings);
     if (e) { ctx.err= e; goto xe; }
 
     buf = xlu__cfg_yy_scan_bytes(data, length, ctx.scanner);
diff --git a/tools/libs/util/libxlu_internal.h b/tools/libs/util/libxlu_internal.h
index cc98efba27..34c6c7a443 100644
--- a/tools/libs/util/libxlu_internal.h
+++ b/tools/libs/util/libxlu_internal.h
@@ -39,6 +39,12 @@  extern int xlu_cfg_printf(const XLU_Config *cfg, const char *format, ...)
  * Internals for file parser *only*, NOT to be used by other parsing/lexing
  */
 extern void xlu__cfg_set_free(XLU_ConfigSetting *set);
+extern int xlu__cfg_readfile(XLU_Config *cfg, FILE *msgfile,
+    const char *msgprefix, XLU_ConfigSetting **psettings, FILE *file,
+    const char *name);
+extern int xlu__cfg_readdata(XLU_Config *cfg, FILE *msgfile,
+    const char *msgprefix, XLU_ConfigSetting **psettings, const char *data,
+    int length);
 
 #endif /*LIBXLU_INTERNAL_H*/