Parse command-line args
authorJoseph C. Lehner <joseph.c.lehner@gmail.com>
Mon, 25 Jan 2016 13:11:49 +0000 (14:11 +0100)
committerJoseph C. Lehner <joseph.c.lehner@gmail.com>
Mon, 25 Jan 2016 13:11:49 +0000 (14:11 +0100)
main.c

diff --git a/main.c b/main.c
index 7ee086c41294310fc146da2142bfd65ac30f6b76..39f327c32e3efc720fe8cd5a7c1e651b79912df0 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,44 +1,97 @@
+#include <getopt.h>
+#include <stdlib.h>
 #include <stdio.h>
 #include "nmrpd.h"
 
-void usage()
+void usage(FILE *fp)
 {
-       printf(
-                       "Usage: nmrpd [options] [command] [command args]\n"
+       fprintf(fp,
+                       "Usage: nmrpd [OPTIONS...]\n"
                        "\n"
-                       "Available options:\n"
-                       " -m [mac]        MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
-                       " -a [ipaddr]     IP address to assign to target device\n"
-                       " -M [netmask]    Subnet mask to assign to target device\n"
-                       " -t [timeout]    Timeout (in milliseconds) for regular messages\n"
-                       " -T [timeout]    Time to wait after successfull TFTP upload\n"
-                       " -p [port]       Port to use for TFTP upload\n"
-                       " -i [interface]  Network interface directly connected to device\n"
+                       "Options:\n"
+                       " -a <ipaddr>     IP address to assign to target device\n"
+                       " -f <firmware>   Firmware file\n"
+                       " -i <interface>  Network interface directly connected to device\n"
+                       " -m <mac>        MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
+                       " -M <netmask>    Subnet mask to assign to target device\n"
+                       " -t <timeout>    Timeout (in milliseconds) for regular messages\n"
+                       " -T <timeout>    Time to wait after successfull TFTP upload\n"
+                       " -p <port>       Port to use for TFTP upload\n"
+                       " -h              Show this screen\n"
                        "\n"
-                       "Available commands:\n"
-                       " set-region      Set region of device\n"
-                       " upload-firmware Upload new firmware\n"
-                       " upload-strings  Upload string table\n"
+                       "Options -a, -i and -f are mandatory!\n"
                        "\n"
          );
 }
 
-
-
 int main(int argc, char **argv)
 {
+       int c, val, max;
        struct nmrpd_args args = {
                .rx_timeout = 200,
                .ul_timeout = 60000,
-               .filename = argc >= 2 ? argv[1] : NULL,
-               .ipaddr = "192.168.2.2",
+               .filename = NULL,
+               .ipaddr = NULL,
                .ipmask = "255.255.255.0",
-               .intf = "enp4s0",
+               .intf = NULL,
                .mac = "ff:ff:ff:ff:ff:ff",
                .op = NMRP_UPLOAD_FW,
                .port = 69,
-               .force_root = 0
+               .force_root = 1
        };
 
+       opterr = 0;
+
+       while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:")) != -1) {
+               max = 0xffffffff;
+               switch (c) {
+                       case 'a':
+                               args.ipaddr = optarg;
+                               break;
+                       case 'f':
+                               args.filename = optarg;
+                               break;
+                       case 'i':
+                               args.intf = optarg;
+                               break;
+                       case 'm':
+                               args.mac = optarg;
+                               break;
+                       case 'M':
+                               args.ipmask = optarg;
+                               break;
+                       case 'p':
+                               max = 0xffff;
+                       case 'T':
+                       case 't':
+                               val = atoi(optarg);
+                               if (val <= 0 || val > max) {
+                                       fprintf(stderr, "Invalid numeric value for -%c.\n", c);
+                                       return 1;
+                               }
+
+                               if (c == 'p') {
+                                       args.port = val;
+                               } else if (c == 't') {
+                                       args.rx_timeout = val;
+                               } else {
+                                       args.ul_timeout = val;
+                               }
+
+                               break;
+                       case 'h':
+                               usage(stdout);
+                               return 0;
+                       default:
+                               usage(stderr);
+                               return 1;
+               }
+       }
+
+       if (!args.filename || !args.intf || !args.ipaddr) {
+               usage(stderr);
+               return 1;
+       }
+
        return nmrp_do(&args);
 }