dmidecode-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [dmidecode] [PATCH] dmidecode: Prepare for alternative output format


From: Petter Reinholdtsen
Subject: Re: [dmidecode] [PATCH] dmidecode: Prepare for alternative output formats
Date: Fri, 13 Dec 2024 16:28:28 +0100

[Jean Delvare]
> Petter, is this more in line with how you think this should be
> implemented?

Sorry for the late reply, I failed to notice your question here.

The following patch is more in line with what I had in mind, avoiding
the wrapper methods and using macros to redirect the call.  Note the
struct ofmt_ and the variable ofmt can not have the same name.

diff --git a/dmidecode.c b/dmidecode.c
index 547cb48..c6f4c64 100644
--- a/dmidecode.c
+++ b/dmidecode.c
@@ -6218,6 +6218,8 @@ int main(int argc, char * const argv[])
                goto exit_free;
        }
 
+       set_output_format(OFMT_PLAIN_TEXT);
+
        if (!(opt.flags & FLAG_QUIET))
                pr_comment("dmidecode %s", VERSION);
 
diff --git a/dmioutput.c b/dmioutput.c
index 42f8d32..1bfb33f 100644
--- a/dmioutput.c
+++ b/dmioutput.c
@@ -23,7 +23,7 @@
 #include <stdio.h>
 #include "dmioutput.h"
 
-void pr_comment(const char *format, ...)
+static void pr_comment_ptext(const char *format, ...)
 {
        va_list args;
 
@@ -34,7 +34,7 @@ void pr_comment(const char *format, ...)
        printf("\n");
 }
 
-void pr_info(const char *format, ...)
+static void pr_info_ptext(const char *format, ...)
 {
        va_list args;
 
@@ -44,13 +44,13 @@ void pr_info(const char *format, ...)
        printf("\n");
 }
 
-void pr_handle(const struct dmi_header *h)
+static void pr_handle_ptext(const struct dmi_header *h)
 {
        printf("Handle 0x%04X, DMI type %d, %d bytes\n",
               h->handle, h->type, h->length);
 }
 
-void pr_handle_name(const char *format, ...)
+static void pr_handle_name_ptext(const char *format, ...)
 {
        va_list args;
 
@@ -60,7 +60,7 @@ void pr_handle_name(const char *format, ...)
        printf("\n");
 }
 
-void pr_attr(const char *name, const char *format, ...)
+static void pr_attr_ptext(const char *name, const char *format, ...)
 {
        va_list args;
 
@@ -72,7 +72,7 @@ void pr_attr(const char *name, const char *format, ...)
        printf("\n");
 }
 
-void pr_subattr(const char *name, const char *format, ...)
+static void pr_subattr_ptext(const char *name, const char *format, ...)
 {
        va_list args;
 
@@ -84,7 +84,7 @@ void pr_subattr(const char *name, const char *format, ...)
        printf("\n");
 }
 
-void pr_list_start(const char *name, const char *format, ...)
+static void pr_list_start_ptext(const char *name, const char *format, ...)
 {
        va_list args;
 
@@ -102,7 +102,7 @@ void pr_list_start(const char *name, const char *format, 
...)
 
 }
 
-void pr_list_item(const char *format, ...)
+static void pr_list_item_ptext(const char *format, ...)
 {
        va_list args;
 
@@ -114,17 +114,17 @@ void pr_list_item(const char *format, ...)
        printf("\n");
 }
 
-void pr_list_end(void)
+static void pr_list_end_ptext(void)
 {
        /* a no-op for text output */
 }
 
-void pr_sep(void)
+static void pr_sep_ptext(void)
 {
        printf("\n");
 }
 
-void pr_struct_err(const char *format, ...)
+static void pr_struct_err_ptext(const char *format, ...)
 {
        va_list args;
 
@@ -135,3 +135,41 @@ void pr_struct_err(const char *format, ...)
        va_end(args);
        printf("\n");
 }
+
+#undef pr_comment
+#undef pr_info
+#undef pr_handle
+#undef pr_handle_name
+#undef pr_attr
+#undef pr_subattr
+#undef pr_list_start
+#undef pr_list_item
+#undef pr_list_end
+#undef pr_sep
+#undef pr_struct_err
+static struct ofmt_ ofmt_plain_text =
+{
+       .pr_comment     = pr_comment_ptext,
+       .pr_info        = pr_info_ptext,
+       .pr_handle      = pr_handle_ptext,
+       .pr_handle_name = pr_handle_name_ptext,
+       .pr_attr        = pr_attr_ptext,
+       .pr_subattr     = pr_subattr_ptext,
+       .pr_list_start  = pr_list_start_ptext,
+       .pr_list_item   = pr_list_item_ptext,
+       .pr_list_end    = pr_list_end_ptext,
+       .pr_sep         = pr_sep_ptext,
+       .pr_struct_err  = pr_struct_err_ptext,
+};
+
+struct ofmt_ *ofmt;
+
+void set_output_format(int ofmt_nr)
+{
+       switch (ofmt_nr)
+       {
+               default:
+                       ofmt = &ofmt_plain_text;
+               break;
+       }
+}
diff --git a/dmioutput.h b/dmioutput.h
index a492ec0..7263ea9 100644
--- a/dmioutput.h
+++ b/dmioutput.h
@@ -21,14 +21,35 @@
 
 #include "dmidecode.h"
 
-void pr_comment(const char *format, ...);
-void pr_info(const char *format, ...);
-void pr_handle(const struct dmi_header *h);
-void pr_handle_name(const char *format, ...);
-void pr_attr(const char *name, const char *format, ...);
-void pr_subattr(const char *name, const char *format, ...);
-void pr_list_start(const char *name, const char *format, ...);
-void pr_list_item(const char *format, ...);
-void pr_list_end(void);
-void pr_sep(void);
-void pr_struct_err(const char *format, ...);
+#define OFMT_PLAIN_TEXT                0
+
+struct ofmt_
+{
+       void (*pr_comment)(const char *format, ...);
+       void (*pr_info)(const char *format, ...);
+       void (*pr_handle)(const struct dmi_header *h);
+       void (*pr_handle_name)(const char *format, ...);
+       void (*pr_attr)(const char *name, const char *format, ...);
+       void (*pr_subattr)(const char *name, const char *format, ...);
+       void (*pr_list_start)(const char *name, const char *format, ...);
+       void (*pr_list_item)(const char *format, ...);
+       void (*pr_list_end)(void);
+       void (*pr_sep)(void);
+       void (*pr_struct_err)(const char *format, ...);
+};
+
+extern struct ofmt_ *ofmt;
+
+void set_output_format(int ofmt);
+
+#define pr_comment ofmt->pr_comment
+#define pr_info ofmt->pr_info
+#define pr_handle ofmt->pr_handle
+#define pr_handle_name ofmt->pr_handle_name
+#define pr_attr ofmt->pr_attr
+#define pr_subattr ofmt->pr_subattr
+#define pr_list_start ofmt->pr_list_start
+#define pr_list_item ofmt->pr_list_item
+#define pr_list_end ofmt->pr_list_end
+#define pr_sep ofmt->pr_sep
+#define pr_struct_err ofmt->pr_struct_err

-- 
Happy hacking
Petter Reinholdtsen



reply via email to

[Prev in Thread] Current Thread [Next in Thread]