]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
charon-cmd: move command line options to separate file, obsolete short options
authorMartin Willi <martin@revosec.ch>
Tue, 26 Mar 2013 13:10:00 +0000 (14:10 +0100)
committerMartin Willi <martin@revosec.ch>
Mon, 6 May 2013 13:28:26 +0000 (15:28 +0200)
src/charon-cmd/Makefile.am
src/charon-cmd/charon-cmd.c
src/charon-cmd/cmd/cmd_options.c [new file with mode: 0644]
src/charon-cmd/cmd/cmd_options.h [new file with mode: 0644]

index de1580e3b49f47c6b53e36ae285d6d1ed139fff1..cd949e08ff89ffebb940a02974e1269d7dbdfd1a 100644 (file)
@@ -1,7 +1,8 @@
 sbin_PROGRAMS = charon-cmd
 
 charon_cmd_SOURCES = \
-charon-cmd.c
+       cmd/cmd_options.h cmd/cmd_options.c \
+       charon-cmd.c
 
 charon-cmd.o : $(top_builddir)/config.status
 
index b2cb6e8108b1d9e73ff00e35d71cd1ef34da4d71..f93b619db6f8add8a4cd396b054ebbc3fc2689b7 100644 (file)
@@ -32,6 +32,8 @@
 #include <utils/backtrace.h>
 #include <threading/thread.h>
 
+#include "cmd/cmd_options.h"
+
 /**
  * Loglevel configuration
  */
@@ -158,27 +160,6 @@ static void segv_handler(int signal)
        abort();
 }
 
-/**
- * Command line arguments, similar to "struct option", but with descriptions
- */
-static struct {
-       /** long option name */
-       const char *lng;
-       /** short option name */
-       const char shrt;
-       /** takes argument */
-       int has_arg;
-       /** decription of argument */
-       const char *arg;
-       /** description to option */
-       const char *desc;
-} options[] = {
-       { "help", 'h', no_argument, "",
-         "print this usage information and exit" },
-       { "version", 'v', no_argument, "",
-         "show version information and exit" },
-};
-
 /**
  * Print command line usage and exit
  */
@@ -186,9 +167,10 @@ static void usage(FILE *out, char *msg, char *binary)
 {
        int i, pre, post, padto = 0, spacing = 2;
 
-       for (i = 0; i < countof(options); i++)
+       for (i = 0; i < CMD_OPT_COUNT; i++)
        {
-               padto = max(padto, strlen(options[i].lng) + strlen(options[i].arg));
+               padto = max(padto, strlen(cmd_options[i].name) +
+                                                  strlen(cmd_options[i].arg));
        }
        padto += spacing;
 
@@ -197,9 +179,9 @@ static void usage(FILE *out, char *msg, char *binary)
                fprintf(out, "%s\n", msg);
        }
        fprintf(out, "Usage: %s\n", binary);
-       for (i = 0; i < countof(options); i++)
+       for (i = 0; i < CMD_OPT_COUNT; i++)
        {
-               switch (options[i].has_arg)
+               switch (cmd_options[i].has_arg)
                {
                        case required_argument:
                                pre = '<';
@@ -214,11 +196,11 @@ static void usage(FILE *out, char *msg, char *binary)
                                pre = post = ' ';
                                break;
                }
-               fprintf(out, "  --%s (-%-c) %c%s%c %-*s%s\n",
-                       options[i].lng, options[i].shrt,
-                       pre, options[i].arg, post,
-                       padto - strlen(options[i].lng) - strlen(options[i].arg), "",
-                       options[i].desc);
+               fprintf(out, "  --%s %c%s%c %-*s%s\n",
+                       cmd_options[i].name,
+                       pre, cmd_options[i].arg, post,
+                       padto - strlen(cmd_options[i].name) - strlen(cmd_options[i].arg), "",
+                       cmd_options[i].desc);
        }
 }
 
@@ -229,38 +211,24 @@ static void handle_arguments(int argc, char *argv[])
 {
        while (TRUE)
        {
-               struct option long_opts[countof(options) + 1] = {};
-               char optstring[countof(options) * 3 + 1] = {};
-               int i, pos = 0;
+               struct option long_opts[CMD_OPT_COUNT + 1] = {};
+               int i;
 
-               for (i = 0; i < countof(options); i++)
+               for (i = 0; i < CMD_OPT_COUNT; i++)
                {
-                       long_opts[i].name = options[i].lng;
-                       long_opts[i].val = options[i].shrt;
-                       long_opts[i].has_arg = options[i].has_arg;
-                       optstring[pos++] = options[i].shrt;
-                       switch (options[i].has_arg)
-                       {
-                               case optional_argument:
-                                       optstring[pos++] = ':';
-                                       /* FALL */
-                               case required_argument:
-                                       optstring[pos++] = ':';
-                                       /* FALL */
-                               case no_argument:
-                               default:
-                                       break;
-                       }
+                       long_opts[i].name = cmd_options[i].name;
+                       long_opts[i].val = cmd_options[i].id;
+                       long_opts[i].has_arg = cmd_options[i].has_arg;
                }
 
-               switch (getopt_long(argc, argv, optstring, long_opts, NULL))
+               switch (getopt_long(argc, argv, "", long_opts, NULL))
                {
                        case EOF:
                                break;
-                       case 'h':
+                       case CMD_OPT_HELP:
                                usage(stdout, NULL, argv[0]);
                                exit(0);
-                       case 'v':
+                       case CMD_OPT_VERSION:
                                printf("%s, strongSwan %s\n", "charon-cmd", VERSION);
                                exit(0);
                        default:
diff --git a/src/charon-cmd/cmd/cmd_options.c b/src/charon-cmd/cmd/cmd_options.c
new file mode 100644 (file)
index 0000000..997cd34
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2013 Martin Willi
+ * Copyright (C) 2013 revosec AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#include "cmd_options.h"
+
+#include <getopt.h>
+
+/**
+ * See header.
+ */
+cmd_option_t cmd_options[CMD_OPT_COUNT] = {
+       { CMD_OPT_HELP, "help", no_argument, "",
+         "print this usage information and exit" },
+       { CMD_OPT_VERSION, "version", no_argument, "",
+         "show version information and exit" },
+};
diff --git a/src/charon-cmd/cmd/cmd_options.h b/src/charon-cmd/cmd/cmd_options.h
new file mode 100644 (file)
index 0000000..d453a53
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2013 Martin Willi
+ * Copyright (C) 2013 revosec AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup cmd_option cmd_option
+ * @{ @ingroup cmd
+ */
+
+#ifndef CMD_OPTION_H_
+#define CMD_OPTION_H_
+
+typedef struct cmd_option_t cmd_option_t;
+typedef enum cmd_option_type_t cmd_option_type_t;
+
+/**
+ * Command line options
+ */
+enum cmd_option_type_t {
+       CMD_OPT_HELP,
+       CMD_OPT_VERSION,
+
+       CMD_OPT_COUNT
+};
+
+/**
+ * Command line arguments, similar to "struct option", but with descriptions
+ */
+struct cmd_option_t {
+       /** option identifier */
+       cmd_option_type_t id;
+       /** long option name */
+       const char *name;
+       /** takes argument */
+       int has_arg;
+       /** decription of argument */
+       const char *arg;
+       /** description to option */
+       const char *desc;
+};
+
+/**
+ * Registered CMD options.
+ */
+extern cmd_option_t cmd_options[CMD_OPT_COUNT];
+
+#endif /** CMD_OPTION_H_ @}*/