dmidecode-devel
[Top][All Lists]
Advanced

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

[dmidecode] [PATCH] dmidecode: Prepare for alternative output formats


From: Jean Delvare
Subject: [dmidecode] [PATCH] dmidecode: Prepare for alternative output formats
Date: Tue, 7 Apr 2020 14:43:04 +0200

Declare the output functions as function pointers, and assign the
default plain text output functions to them at run time. In the
future, alternative output formats can be added.

Signed-off-by: Jean Delvare <address@hidden>
---
This patch by itself does nothing, however it serves as an illustration
of how alternative output format support could be plugged into
dmidecode later, if anyone is interested in doing that.

 dmidecode.c |    2 ++
 dmioutput.c |   54 +++++++++++++++++++++++++++++++++++++++++++-----------
 dmioutput.h |   26 +++++++++++++++-----------
 3 files changed, 60 insertions(+), 22 deletions(-)

--- dmidecode.orig/dmidecode.c  2020-04-07 11:39:57.055495381 +0200
+++ dmidecode/dmidecode.c       2020-04-07 11:44:50.508031744 +0200
@@ -5608,6 +5608,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);
 
--- dmidecode.orig/dmioutput.c  2020-04-07 11:39:57.055495381 +0200
+++ dmidecode/dmioutput.c       2020-04-07 11:49:52.936648190 +0200
@@ -23,7 +23,19 @@
 #include <stdio.h>
 #include "dmioutput.h"
 
-void pr_comment(const char *format, ...)
+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, ...);
+
+static void pr_comment_ptext(const char *format, ...)
 {
        va_list args;
 
@@ -34,7 +46,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 +56,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 +72,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 +84,7 @@ void pr_attr(const char *name, const cha
        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 +96,7 @@ void pr_subattr(const char *name, const
        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 +114,7 @@ void pr_list_start(const char *name, con
 
 }
 
-void pr_list_item(const char *format, ...)
+static void pr_list_item_ptext(const char *format, ...)
 {
        va_list args;
 
@@ -114,17 +126,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 +147,23 @@ void pr_struct_err(const char *format, .
        va_end(args);
        printf("\n");
 }
+
+void set_output_format(int ofmt)
+{
+       switch (ofmt)
+       {
+               default:
+                       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;
+               break;
+       }
+}
--- dmidecode.orig/dmioutput.h  2020-04-07 11:39:57.055495381 +0200
+++ dmidecode/dmioutput.h       2020-04-07 11:44:50.508031744 +0200
@@ -21,14 +21,18 @@
 
 #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
+
+extern void (*pr_comment)(const char *format, ...);
+extern void (*pr_info)(const char *format, ...);
+extern void (*pr_handle)(const struct dmi_header *h);
+extern void (*pr_handle_name)(const char *format, ...);
+extern void (*pr_attr)(const char *name, const char *format, ...);
+extern void (*pr_subattr)(const char *name, const char *format, ...);
+extern void (*pr_list_start)(const char *name, const char *format, ...);
+extern void (*pr_list_item)(const char *format, ...);
+extern void (*pr_list_end)(void);
+extern void (*pr_sep)(void);
+extern void (*pr_struct_err)(const char *format, ...);
+
+void set_output_format(int ofmt);


-- 
Jean Delvare
SUSE L3 Support



reply via email to

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