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: Jean Delvare
Subject: Re: [dmidecode] [PATCH] dmidecode: Prepare for alternative output formats
Date: Wed, 8 Apr 2020 15:01:49 +0200

So I wasn't able to use macros, the syntax for macros with variable
parameters doesn't seem fully standardized and what I tried wouldn't
build. Using regular wrapper functions seems to work though.

I'm slightly concerned by the overhead as we now must call two functions
instead of one for every print statement, but I suppose it is marginal.

The diffstat is a bit larger than with the initial version.

Petter, is this more in line with how you think this should be
implemented?
---
 dmidecode.c |    2 
 dmioutput.c |  154 ++++++++++++++++++++++++++++++++++++++++++++----------------
 dmioutput.h |   21 ++++++++
 3 files changed, 138 insertions(+), 39 deletions(-)

--- dmidecode.orig/dmidecode.c  2020-04-07 11:54:45.877151175 +0200
+++ dmidecode/dmidecode.c       2020-04-08 10:50:38.578737082 +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:54:45.877151175 +0200
+++ dmidecode/dmioutput.c       2020-04-08 14:39:52.505469443 +0200
@@ -23,15 +23,105 @@
 #include <stdio.h>
 #include "dmioutput.h"
 
+static void pr_comment_ptext(const char *format, va_list args)
+{
+       printf("# ");
+       vprintf(format, args);
+       printf("\n");
+}
+
+static void pr_info_ptext(const char *format, va_list args)
+{
+       vprintf(format, args);
+       printf("\n");
+}
+
+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);
+}
+
+static void pr_handle_name_ptext(const char *format, va_list args)
+{
+       vprintf(format, args);
+       printf("\n");
+}
+
+static void pr_attr_ptext(const char *name, const char *format, va_list args)
+{
+       printf("\t%s: ", name);
+       vprintf(format, args);
+       printf("\n");
+}
+
+static void pr_subattr_ptext(const char *name, const char *format, va_list 
args)
+{
+       printf("\t\t%s: ", name);
+       vprintf(format, args);
+       printf("\n");
+}
+
+static void pr_list_start_ptext(const char *name, const char *format, va_list 
args)
+{
+       printf("\t%s:", name);
+       /* format is optional, skip value if not provided */
+       if (format)
+       {
+               printf(" ");
+               vprintf(format, args);
+       }
+       printf("\n");
+}
+
+static void pr_list_item_ptext(const char *format, va_list args)
+{
+       printf("\t\t");
+       vprintf(format, args);
+       printf("\n");
+}
+
+static void pr_list_end_ptext(void)
+{
+       /* a no-op for text output */
+}
+
+static void pr_sep_ptext(void)
+{
+       printf("\n");
+}
+
+static void pr_struct_err_ptext(const char *format, va_list args)
+{
+       printf("\t");
+       vprintf(format, args);
+       printf("\n");
+}
+
+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 pr_comment(const char *format, ...)
 {
        va_list args;
 
-       printf("# ");
        va_start(args, format);
-       vprintf(format, args);
+       ofmt->pr_comment(format, args);
        va_end(args);
-       printf("\n");
 }
 
 void pr_info(const char *format, ...)
@@ -39,15 +129,13 @@ void pr_info(const char *format, ...)
        va_list args;
 
        va_start(args, format);
-       vprintf(format, args);
+       ofmt->pr_info(format, args);
        va_end(args);
-       printf("\n");
 }
 
 void pr_handle(const struct dmi_header *h)
 {
-       printf("Handle 0x%04X, DMI type %d, %d bytes\n",
-              h->handle, h->type, h->length);
+       ofmt->pr_handle(h);
 }
 
 void pr_handle_name(const char *format, ...)
@@ -55,83 +143,71 @@ void pr_handle_name(const char *format,
        va_list args;
 
        va_start(args, format);
-       vprintf(format, args);
+       ofmt->pr_handle_name(format, args);
        va_end(args);
-       printf("\n");
 }
 
 void pr_attr(const char *name, const char *format, ...)
 {
        va_list args;
 
-       printf("\t%s: ", name);
-
        va_start(args, format);
-       vprintf(format, args);
+       ofmt->pr_attr(name, format, args);
        va_end(args);
-       printf("\n");
 }
 
 void pr_subattr(const char *name, const char *format, ...)
 {
        va_list args;
 
-       printf("\t\t%s: ", name);
-
        va_start(args, format);
-       vprintf(format, args);
+       ofmt->pr_subattr(name, format, args);
        va_end(args);
-       printf("\n");
 }
 
 void pr_list_start(const char *name, const char *format, ...)
 {
        va_list args;
 
-       printf("\t%s:", name);
-
-       /* format is optional, skip value if not provided */
-       if (format)
-       {
-               printf(" ");
-               va_start(args, format);
-               vprintf(format, args);
-               va_end(args);
-       }
-       printf("\n");
-
+       va_start(args, format);
+       ofmt->pr_list_start(name, format, args);
+       va_end(args);
 }
 
 void pr_list_item(const char *format, ...)
 {
        va_list args;
 
-       printf("\t\t");
-
        va_start(args, format);
-       vprintf(format, args);
+       ofmt->pr_list_item(format, args);
        va_end(args);
-       printf("\n");
 }
 
 void pr_list_end(void)
 {
-       /* a no-op for text output */
+       ofmt->pr_list_end();
 }
 
 void pr_sep(void)
 {
-       printf("\n");
+       ofmt->pr_sep();
 }
 
 void pr_struct_err(const char *format, ...)
 {
        va_list args;
 
-       printf("\t");
-
        va_start(args, format);
-       vprintf(format, args);
+       ofmt->pr_struct_err(format, args);
        va_end(args);
-       printf("\n");
+}
+
+void set_output_format(int ofmt_nr)
+{
+       switch (ofmt_nr)
+       {
+               default:
+                       ofmt = &ofmt_plain_text;
+               break;
+       }
 }
--- dmidecode.orig/dmioutput.h  2020-04-07 11:54:45.877151175 +0200
+++ dmidecode/dmioutput.h       2020-04-08 14:39:39.521315056 +0200
@@ -32,3 +32,24 @@ 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, va_list args);
+       void (*pr_info)(const char *format, va_list args);
+       void (*pr_handle)(const struct dmi_header *h);
+       void (*pr_handle_name)(const char *format, va_list args);
+       void (*pr_attr)(const char *name, const char *format, va_list args);
+       void (*pr_subattr)(const char *name, const char *format, va_list args);
+       void (*pr_list_start)(const char *name, const char *format, va_list 
args);
+       void (*pr_list_item)(const char *format, va_list args);
+       void (*pr_list_end)(void);
+       void (*pr_sep)(void);
+       void (*pr_struct_err)(const char *format, va_list args);
+};
+
+extern struct ofmt *ofmt;
+
+void set_output_format(int ofmt);


-- 
Jean Delvare
SUSE L3 Support



reply via email to

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