modernized argument parser (use getopt now)
authorpk910 <philipp@zoelle1.de>
Mon, 6 Feb 2012 03:11:33 +0000 (04:11 +0100)
committerpk910 <philipp@zoelle1.de>
Mon, 6 Feb 2012 03:14:05 +0000 (04:14 +0100)
src/main.c
src/main.h

index fb8abcebbf7e51aace68d7d0950f9a2fcc860c07..d9819ca4b96c5388af2ce216717d963f7199cc9e 100644 (file)
@@ -157,28 +157,38 @@ void exit_daemon() {
 
 int main(int argc, char *argv[]) {
     int run_as_daemon = 1;
-    int argi;
+    int c;
     process_argv = argv;
     process_argc = argc;
     printf("NeonServ v%s\n\n", NEONSERV_VERSION);
-    for(argi = 1; argi < argc; argi++) {
-        if(!strcmp(argv[argi], "-f") || !strcmp(argv[argi], "--foreground")) {
+    struct option options[] = {
+        {"show", 1, 0, 's'},
+        {"foreground", 0, 0, 'f'},
+        {"help", 0, 0, 'h'},
+        {"version", 0, 0, 'v'},
+        {0, 0, 0, 0}
+    };
+    while ((c = getopt_long(argc, argv, "c:dfhkr:v", options, NULL)) != -1) {
+        switch (c) {
+        case 's':
+            print_loglevel = atoi(optarg);
+            break;
+        case 'f':
             run_as_daemon = 0;
-        } else if(!strcmp(argv[argi], "-s") || !strcmp(argv[argi], "--show")) {
-            if(argi+1 == argc) break;
-            argi++;
-            print_loglevel = atoi(argv[argi]);
-        } else if(!strcmp(argv[argi], "-v") || !strcmp(argv[argi], "--version")) {
+            break;
+        case 'v':
             printf("Version: %s.%d (%s)\n", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
             printf("Build: #%s %s (%s lines, " COMPILER ")\n", compilation, creation, codelines);
             exit(0);
-        } else if(!strcmp(argv[argi], "-h") || !strcmp(argv[argi], "--help")) {
+            break;
+        case 'h':
             printf("Usage: ./neonserv [-s loglevel] [-f] [-h|-v]\n");
             printf(" -s, --show           show log lines matching loglevel in stdout.\n");
             printf(" -f, --foreground     run NeonServ in the foreground.\n");
             printf(" -h, --help           prints this usage message.\n");
             printf(" -v, --version        prints this program's version.\n");
             exit(0);
+            break;
         }
     }
     if(geteuid() == 0 || getuid() == 0) {
index 12c689958eb768c0c88c89fd185872a57d4950cf..eb39d49b9acf60083c79e77265afb2c2dfede1e5 100644 (file)
@@ -40,6 +40,7 @@
 #include <sys/wait.h>
 #endif
 #include <unistd.h>
+#include <getopt.h>
 #include <stdarg.h>
 #include <sys/time.h>
 #include <time.h>