From 55d5bb44cbca6993494b05a374d4f09ec03c9102 Mon Sep 17 00:00:00 2001
From: Justin Pettit <jpettit@nicira.com>
Date: Tue, 2 Aug 2011 12:16:44 -0700
Subject: [PATCH] util: Introduce get_program_version function.

Useful in an upcoming commit.
---
 lib/util.c                          | 35 +++++++++++++++++++++++------
 lib/util.h                          | 10 ++++-----
 ovsdb/ovsdb-client.c                |  2 +-
 ovsdb/ovsdb-server.c                |  2 +-
 ovsdb/ovsdb-tool.c                  |  2 +-
 tests/test-openflowd.c              |  2 +-
 utilities/ovs-appctl.c              |  2 +-
 utilities/ovs-benchmark.c           |  2 +-
 utilities/ovs-controller.c          |  2 +-
 utilities/ovs-dpctl.c               |  2 +-
 utilities/ovs-ofctl.c               |  2 +-
 utilities/ovs-vlan-bug-workaround.c |  2 +-
 utilities/ovs-vsctl.c               |  2 +-
 vswitchd/ovs-brcompatd.c            |  2 +-
 vswitchd/ovs-vswitchd.c             |  2 +-
 15 files changed, 46 insertions(+), 25 deletions(-)

diff --git a/lib/util.c b/lib/util.c
index 639424dd..0b82318c 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -33,6 +33,7 @@ VLOG_DEFINE_THIS_MODULE(util);
 COVERAGE_DEFINE(util_xalloc);
 
 const char *program_name;
+static char *program_version;
 
 void
 out_of_memory(void)
@@ -277,21 +278,41 @@ ovs_retval_to_string(int retval)
     return unknown;
 }
 
-/* Sets program_name based on 'argv0'.  Should be called at the beginning of
- * main(), as "set_program_name(argv[0]);".  */
-void set_program_name(const char *argv0)
+/* Sets global "program_name" and "program_version" variables.  Should
+ * be called at the beginning of main() with "argv[0]" as the argument
+ * to 'argv0'.
+ *
+ * The 'date' and 'time' arguments should likely be called with
+ * "__DATE__" and "__TIME__" to use the time the binary was built.
+ * Alternatively, the "set_program_name" macro may be called to do this
+ * automatically.
+ */
+void
+set_program_name__(const char *argv0, const char *date, const char *time)
 {
     const char *slash = strrchr(argv0, '/');
     program_name = slash ? slash + 1 : argv0;
+
+    free(program_version);
+    program_version = xasprintf("%s (Open vSwitch) "VERSION BUILDNR"\n"
+                                "Compiled %s %s\n",
+                                program_name, date, time);
+}
+
+/* Returns a pointer to a string describing the program version.  The
+ * caller must not modify or free the returned string.
+ */ 
+const char *
+get_program_version()
+{
+    return program_version;
 }
 
 /* Print the version information for the program.  */
 void
-ovs_print_version(char *date, char *time,
-                  uint8_t min_ofp, uint8_t max_ofp)
+ovs_print_version(uint8_t min_ofp, uint8_t max_ofp)
 {
-    printf("%s (Open vSwitch) "VERSION BUILDNR"\n", program_name);
-    printf("Compiled %s %s\n", date, time);
+    printf("%s", program_version);
     if (min_ofp || max_ofp) {
         printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp);
     }
diff --git a/lib/util.h b/lib/util.h
index 601f49f1..1649c59c 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -140,12 +140,12 @@ extern const char *program_name;
 extern "C" {
 #endif
 
-void set_program_name(const char *);
+void set_program_name__(const char *name, const char *date, const char *time);
+#define set_program_name(name) \
+        set_program_name__(name, __DATE__, __TIME__)
 
-void ovs_print_version(char *date, char *time,
-                       uint8_t min_ofp, uint8_t max_ofp);
-#define OVS_PRINT_VERSION(min_ofp, max_ofp) \
-        ovs_print_version(__DATE__, __TIME__, (min_ofp), (max_ofp))
+const char *get_program_version(void);
+void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp);
 
 void out_of_memory(void) NO_RETURN;
 void *xmalloc(size_t) MALLOC_LIKE;
diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c
index 2ed11895..d2a9de10 100644
--- a/ovsdb/ovsdb-client.c
+++ b/ovsdb/ovsdb-client.c
@@ -190,7 +190,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case 'v':
diff --git a/ovsdb/ovsdb-server.c b/ovsdb/ovsdb-server.c
index 2d332fec..06ac98be 100644
--- a/ovsdb/ovsdb-server.c
+++ b/ovsdb/ovsdb-server.c
@@ -709,7 +709,7 @@ parse_options(int argc, char *argv[], char **file_namep,
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         VLOG_OPTION_HANDLERS
diff --git a/ovsdb/ovsdb-tool.c b/ovsdb/ovsdb-tool.c
index 74dfa5a5..0e4a5207 100644
--- a/ovsdb/ovsdb-tool.c
+++ b/ovsdb/ovsdb-tool.c
@@ -85,7 +85,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case 'v':
diff --git a/tests/test-openflowd.c b/tests/test-openflowd.c
index b22b2aad..2c913108 100644
--- a/tests/test-openflowd.c
+++ b/tests/test-openflowd.c
@@ -429,7 +429,7 @@ parse_options(int argc, char *argv[], struct ofsettings *s)
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
+            ovs_print_version(OFP_VERSION, OFP_VERSION);
             exit(EXIT_SUCCESS);
 
         DAEMON_OPTION_HANDLERS
diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c
index cd059bf1..699ff7bd 100644
--- a/utilities/ovs-appctl.c
+++ b/utilities/ovs-appctl.c
@@ -147,7 +147,7 @@ parse_command_line(int argc, char *argv[])
             break;
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case '?':
diff --git a/utilities/ovs-benchmark.c b/utilities/ovs-benchmark.c
index e4e92257..bc28dab6 100644
--- a/utilities/ovs-benchmark.c
+++ b/utilities/ovs-benchmark.c
@@ -192,7 +192,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case '?':
diff --git a/utilities/ovs-controller.c b/utilities/ovs-controller.c
index 022b1a4d..cb70e4f7 100644
--- a/utilities/ovs-controller.c
+++ b/utilities/ovs-controller.c
@@ -397,7 +397,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
+            ovs_print_version(OFP_VERSION, OFP_VERSION);
             exit(EXIT_SUCCESS);
 
         VLOG_OPTION_HANDLERS
diff --git a/utilities/ovs-dpctl.c b/utilities/ovs-dpctl.c
index 415d276e..7962c7a6 100644
--- a/utilities/ovs-dpctl.c
+++ b/utilities/ovs-dpctl.c
@@ -108,7 +108,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         VLOG_OPTION_HANDLERS
diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c
index 6be899b1..39ebe400 100644
--- a/utilities/ovs-ofctl.c
+++ b/utilities/ovs-ofctl.c
@@ -138,7 +138,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
+            ovs_print_version(OFP_VERSION, OFP_VERSION);
             exit(EXIT_SUCCESS);
 
         case OPT_STRICT:
diff --git a/utilities/ovs-vlan-bug-workaround.c b/utilities/ovs-vlan-bug-workaround.c
index 309d64e2..9722c278 100644
--- a/utilities/ovs-vlan-bug-workaround.c
+++ b/utilities/ovs-vlan-bug-workaround.c
@@ -130,7 +130,7 @@ parse_options(int argc, char *argv[])
             break;
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case '?':
diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
index b59d8861..573c9481 100644
--- a/utilities/ovs-vsctl.c
+++ b/utilities/ovs-vsctl.c
@@ -262,7 +262,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case 't':
diff --git a/vswitchd/ovs-brcompatd.c b/vswitchd/ovs-brcompatd.c
index 3dd25c37..d9b3bc38 100644
--- a/vswitchd/ovs-brcompatd.c
+++ b/vswitchd/ovs-brcompatd.c
@@ -885,7 +885,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case OPT_APPCTL:
diff --git a/vswitchd/ovs-vswitchd.c b/vswitchd/ovs-vswitchd.c
index 7d4e4d77..b2b20821 100644
--- a/vswitchd/ovs-vswitchd.c
+++ b/vswitchd/ovs-vswitchd.c
@@ -151,7 +151,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
+            ovs_print_version(OFP_VERSION, OFP_VERSION);
             exit(EXIT_SUCCESS);
 
         case OPT_MLOCKALL:
-- 
2.30.2