util: Introduce get_program_version function.
authorJustin Pettit <jpettit@nicira.com>
Tue, 2 Aug 2011 19:16:44 +0000 (12:16 -0700)
committerJustin Pettit <jpettit@nicira.com>
Thu, 4 Aug 2011 18:15:43 +0000 (11:15 -0700)
Useful in an upcoming commit.

15 files changed:
lib/util.c
lib/util.h
ovsdb/ovsdb-client.c
ovsdb/ovsdb-server.c
ovsdb/ovsdb-tool.c
tests/test-openflowd.c
utilities/ovs-appctl.c
utilities/ovs-benchmark.c
utilities/ovs-controller.c
utilities/ovs-dpctl.c
utilities/ovs-ofctl.c
utilities/ovs-vlan-bug-workaround.c
utilities/ovs-vsctl.c
vswitchd/ovs-brcompatd.c
vswitchd/ovs-vswitchd.c

index 639424d..0b82318 100644 (file)
@@ -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);
     }
index 601f49f..1649c59 100644 (file)
@@ -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;
index 2ed1189..d2a9de1 100644 (file)
@@ -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':
index 2d332fe..06ac98b 100644 (file)
@@ -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
index 74dfa5a..0e4a520 100644 (file)
@@ -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':
index b22b2aa..2c91310 100644 (file)
@@ -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
index cd059bf..699ff7b 100644 (file)
@@ -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 '?':
index e4e9225..bc28dab 100644 (file)
@@ -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 '?':
index 022b1a4..cb70e4f 100644 (file)
@@ -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
index 415d276..7962c7a 100644 (file)
@@ -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
index 6be899b..39ebe40 100644 (file)
@@ -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:
index 309d64e..9722c27 100644 (file)
@@ -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 '?':
index b59d886..573c948 100644 (file)
@@ -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':
index 3dd25c3..d9b3bc3 100644 (file)
@@ -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:
index 7d4e4d7..b2b2082 100644 (file)
@@ -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: