From: Joseph C. Lehner Date: Mon, 25 Jan 2016 13:11:49 +0000 (+0100) Subject: Parse command-line args X-Git-Tag: v0.9~100 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=4d69e844c42a2e813a5b1073725b9c0c5c3e9298;p=oweals%2Fnmrpflash.git Parse command-line args --- diff --git a/main.c b/main.c index 7ee086c..39f327c 100644 --- a/main.c +++ b/main.c @@ -1,44 +1,97 @@ +#include +#include #include #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 IP address to assign to target device\n" + " -f Firmware file\n" + " -i Network interface directly connected to device\n" + " -m MAC address of target device (xx:xx:xx:xx:xx:xx)\n" + " -M Subnet mask to assign to target device\n" + " -t Timeout (in milliseconds) for regular messages\n" + " -T Time to wait after successfull TFTP upload\n" + " -p 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); }