[Libguestfs] [PATCH 1/2] src: Move long_options short_options, is_short_name to a header file.

Richard W.M. Jones rjones at redhat.com
Tue Nov 13 22:51:29 UTC 2018


This refactoring is simply so the exact same options can be reused in
another program.
---
 src/Makefile.am |   1 +
 src/main.c      |  70 +------------------------------
 src/options.h   | 109 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 111 insertions(+), 69 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index c094ed4..3490c0f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -46,6 +46,7 @@ nbdkit_SOURCES = \
 	log-stderr.c \
 	log-syslog.c \
 	main.c \
+	options.h \
 	plugins.c \
 	protocol.h \
 	sockets.c \
diff --git a/src/main.c b/src/main.c
index 224955b..0c7dbce 100644
--- a/src/main.c
+++ b/src/main.c
@@ -55,11 +55,11 @@
 #include <dlfcn.h>
 
 #include "internal.h"
+#include "options.h"
 #include "exit-with-parent.h"
 
 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
 
-static int is_short_name (const char *);
 static char *make_random_fifo (void);
 static struct backend *open_plugin_so (size_t i, const char *filename, int short_name);
 static struct backend *open_filter_so (struct backend *next, size_t i, const char *filename, int short_name);
@@ -114,67 +114,6 @@ struct backend *backend;
 static char *random_fifo_dir = NULL;
 static char *random_fifo = NULL;
 
-enum {
-  HELP_OPTION = CHAR_MAX + 1,
-  DUMP_CONFIG_OPTION,
-  DUMP_PLUGIN_OPTION,
-  EXIT_WITH_PARENT_OPTION,
-  FILTER_OPTION,
-  LOG_OPTION,
-  LONG_OPTIONS_OPTION,
-  RUN_OPTION,
-  SELINUX_LABEL_OPTION,
-  SHORT_OPTIONS_OPTION,
-  TLS_OPTION,
-  TLS_CERTIFICATES_OPTION,
-  TLS_PSK_OPTION,
-  TLS_VERIFY_PEER_OPTION,
-};
-
-static const char *short_options = "D:e:fg:i:nop:P:rst:u:U:vV";
-static const struct option long_options[] = {
-  { "debug",            required_argument, NULL, 'D' },
-  { "dump-config",      no_argument,       NULL, DUMP_CONFIG_OPTION },
-  { "dump-plugin",      no_argument,       NULL, DUMP_PLUGIN_OPTION },
-  { "exit-with-parent", no_argument,       NULL, EXIT_WITH_PARENT_OPTION },
-  { "export",           required_argument, NULL, 'e' },
-  { "export-name",      required_argument, NULL, 'e' },
-  { "exportname",       required_argument, NULL, 'e' },
-  { "filter",           required_argument, NULL, FILTER_OPTION },
-  { "foreground",       no_argument,       NULL, 'f' },
-  { "no-fork",          no_argument,       NULL, 'f' },
-  { "group",            required_argument, NULL, 'g' },
-  { "help",             no_argument,       NULL, HELP_OPTION },
-  { "ip-addr",          required_argument, NULL, 'i' },
-  { "ipaddr",           required_argument, NULL, 'i' },
-  { "log",              required_argument, NULL, LOG_OPTION },
-  { "long-options",     no_argument,       NULL, LONG_OPTIONS_OPTION },
-  { "new-style",        no_argument,       NULL, 'n' },
-  { "newstyle",         no_argument,       NULL, 'n' },
-  { "old-style",        no_argument,       NULL, 'o' },
-  { "oldstyle",         no_argument,       NULL, 'o' },
-  { "pid-file",         required_argument, NULL, 'P' },
-  { "pidfile",          required_argument, NULL, 'P' },
-  { "port",             required_argument, NULL, 'p' },
-  { "read-only",        no_argument,       NULL, 'r' },
-  { "readonly",         no_argument,       NULL, 'r' },
-  { "run",              required_argument, NULL, RUN_OPTION },
-  { "selinux-label",    required_argument, NULL, SELINUX_LABEL_OPTION },
-  { "short-options",    no_argument,       NULL, SHORT_OPTIONS_OPTION },
-  { "single",           no_argument,       NULL, 's' },
-  { "stdin",            no_argument,       NULL, 's' },
-  { "threads",          required_argument, NULL, 't' },
-  { "tls",              required_argument, NULL, TLS_OPTION },
-  { "tls-certificates", required_argument, NULL, TLS_CERTIFICATES_OPTION },
-  { "tls-psk",          required_argument, NULL, TLS_PSK_OPTION },
-  { "tls-verify-peer",  no_argument,       NULL, TLS_VERIFY_PEER_OPTION },
-  { "unix",             required_argument, NULL, 'U' },
-  { "user",             required_argument, NULL, 'u' },
-  { "verbose",          no_argument,       NULL, 'v' },
-  { "version",          no_argument,       NULL, 'V' },
-  { NULL },
-};
-
 static void
 usage (void)
 {
@@ -748,13 +687,6 @@ main (int argc, char *argv[])
   exit (EXIT_SUCCESS);
 }
 
-/* Is it a plugin or filter name relative to the plugindir/filterdir? */
-static int
-is_short_name (const char *filename)
-{
-  return strchr (filename, '.') == NULL && strchr (filename, '/') == NULL;
-}
-
 /* Implementation of '-U -' */
 static char *
 make_random_fifo (void)
diff --git a/src/options.h b/src/options.h
new file mode 100644
index 0000000..2bbc96e
--- /dev/null
+++ b/src/options.h
@@ -0,0 +1,109 @@
+/* nbdkit
+ * Copyright (C) 2013-2018 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_OPTIONS_H
+#define NBDKIT_OPTIONS_H
+
+#include <getopt.h>
+#include <limits.h>
+#include <string.h>
+
+enum {
+  HELP_OPTION = CHAR_MAX + 1,
+  DUMP_CONFIG_OPTION,
+  DUMP_PLUGIN_OPTION,
+  EXIT_WITH_PARENT_OPTION,
+  FILTER_OPTION,
+  LOG_OPTION,
+  LONG_OPTIONS_OPTION,
+  RUN_OPTION,
+  SELINUX_LABEL_OPTION,
+  SHORT_OPTIONS_OPTION,
+  TLS_OPTION,
+  TLS_CERTIFICATES_OPTION,
+  TLS_PSK_OPTION,
+  TLS_VERIFY_PEER_OPTION,
+};
+
+static const char *short_options = "D:e:fg:i:nop:P:rst:u:U:vV";
+static const struct option long_options[] = {
+  { "debug",            required_argument, NULL, 'D' },
+  { "dump-config",      no_argument,       NULL, DUMP_CONFIG_OPTION },
+  { "dump-plugin",      no_argument,       NULL, DUMP_PLUGIN_OPTION },
+  { "exit-with-parent", no_argument,       NULL, EXIT_WITH_PARENT_OPTION },
+  { "export",           required_argument, NULL, 'e' },
+  { "export-name",      required_argument, NULL, 'e' },
+  { "exportname",       required_argument, NULL, 'e' },
+  { "filter",           required_argument, NULL, FILTER_OPTION },
+  { "foreground",       no_argument,       NULL, 'f' },
+  { "no-fork",          no_argument,       NULL, 'f' },
+  { "group",            required_argument, NULL, 'g' },
+  { "help",             no_argument,       NULL, HELP_OPTION },
+  { "ip-addr",          required_argument, NULL, 'i' },
+  { "ipaddr",           required_argument, NULL, 'i' },
+  { "log",              required_argument, NULL, LOG_OPTION },
+  { "long-options",     no_argument,       NULL, LONG_OPTIONS_OPTION },
+  { "new-style",        no_argument,       NULL, 'n' },
+  { "newstyle",         no_argument,       NULL, 'n' },
+  { "old-style",        no_argument,       NULL, 'o' },
+  { "oldstyle",         no_argument,       NULL, 'o' },
+  { "pid-file",         required_argument, NULL, 'P' },
+  { "pidfile",          required_argument, NULL, 'P' },
+  { "port",             required_argument, NULL, 'p' },
+  { "read-only",        no_argument,       NULL, 'r' },
+  { "readonly",         no_argument,       NULL, 'r' },
+  { "run",              required_argument, NULL, RUN_OPTION },
+  { "selinux-label",    required_argument, NULL, SELINUX_LABEL_OPTION },
+  { "short-options",    no_argument,       NULL, SHORT_OPTIONS_OPTION },
+  { "single",           no_argument,       NULL, 's' },
+  { "stdin",            no_argument,       NULL, 's' },
+  { "threads",          required_argument, NULL, 't' },
+  { "tls",              required_argument, NULL, TLS_OPTION },
+  { "tls-certificates", required_argument, NULL, TLS_CERTIFICATES_OPTION },
+  { "tls-psk",          required_argument, NULL, TLS_PSK_OPTION },
+  { "tls-verify-peer",  no_argument,       NULL, TLS_VERIFY_PEER_OPTION },
+  { "unix",             required_argument, NULL, 'U' },
+  { "user",             required_argument, NULL, 'u' },
+  { "verbose",          no_argument,       NULL, 'v' },
+  { "version",          no_argument,       NULL, 'V' },
+  { NULL },
+};
+
+/* Is it a plugin or filter name relative to the plugindir/filterdir? */
+static inline int
+is_short_name (const char *filename)
+{
+  return strchr (filename, '.') == NULL && strchr (filename, '/') == NULL;
+}
+
+#endif /* NBDKIT_OPTIONS_H */
-- 
2.19.0.rc0




More information about the Libguestfs mailing list