]> git.ipfire.org Git - fireperf.git/commitdiff
Parse mode from command line
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 25 Jan 2021 12:59:01 +0000 (12:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 25 Jan 2021 13:00:29 +0000 (13:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
configure.ac
src/main.c
src/main.h [new file with mode: 0644]

index 342253fdd283afe02d2cff160658c4a0355bff4e..8a5413db2982bd937c1b15dc4b1ced7f7d65d1bf 100644 (file)
@@ -60,6 +60,7 @@ bin_PROGRAMS = \
        fireperf
 
 fireperf_SOURCES = \
+       src/main.h \
        src/main.c
 
 # ------------------------------------------------------------------------------
index 18c1890e4cd4ef5c445ad907ca646282075b75a3..b519fc62d45adb4eb1a31b3dfb8d839b04acbf73 100644 (file)
@@ -63,7 +63,9 @@ AS_IF([test "x$enable_debug" = "xyes"], [
 
 AC_CHECK_HEADERS_ONCE([
        arpa/inet.h \
+       getopt.h \
        netinet/in.h \
+       stdio.h \
        string.h \
 ])
 
index 083ada759a8cc280544b25d5354dd984467b48f4..c31e2c4bc1376047c2dcb451a9027c354802fdd1 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <getopt.h>
+#include <stdio.h>
+
+#include "main.h"
+
+static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) {
+       static struct option long_options[] = {
+               {"client",     required_argument, 0, 'c'},
+               {"server",     no_argument,       0, 's'},
+               {0, 0, 0, 0},
+       };
+
+       int option_index = 0;
+       int done = 0;
+
+       while (!done) {
+               int c = getopt_long(argc, argv, "c:s", long_options, &option_index);
+
+               // End
+               if (c == -1)
+                       break;
+
+               switch (c) {
+                       case 0:
+                               if (long_options[option_index].flag != 0)
+                                       break;
+
+                               printf("option %s", long_options[option_index].name);
+
+                               if (optarg)
+                                       printf("  with arg: %s", optarg);
+
+                               printf("\n");
+                               break;
+
+                       case '?':
+                               // getopt_long already printed the error message
+                               return 1;
+
+                       case 'c':
+                               conf->mode = FIREPERF_MODE_CLIENT;
+                               break;
+
+                       case 's':
+                               conf->mode = FIREPERF_MODE_SERVER;
+                               break;
+
+                       default:
+                               done = 1;
+                               break;
+               }
+       }
+
+       return 0;
+}
+
 int main(int argc, char* argv[]) {
+       struct fireperf_config conf;
+       int r;
+
+       // Parse command line
+       r = parse_argv(argc, argv, &conf);
+       if (r)
+               return r;
+
+       // Check if a mode has been selected
+       if (!conf.mode) {
+               fprintf(stderr, "No mode selected\n");
+               return 2;
+       }
+
        return 0;
 }
diff --git a/src/main.h b/src/main.h
new file mode 100644 (file)
index 0000000..22c900f
--- /dev/null
@@ -0,0 +1,27 @@
+/*#############################################################################
+#                                                                             #
+# fireperf - A network benchmarking tool                                      #
+# Copyright (C) 2021 IPFire Development Team                                  #
+#                                                                             #
+# 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 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# 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.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+struct fireperf_config {
+       enum {
+               FIREPERF_MODE_NONE = 0,
+               FIREPERF_MODE_CLIENT,
+               FIREPERF_MODE_SERVER,
+       } mode;
+};