aboutsummaryrefslogtreecommitdiffstats
path: root/icemulti
diff options
context:
space:
mode:
authorRoland Lutz <rlutz@hedmen.org>2017-07-10 14:15:31 +0200
committerRoland Lutz <rlutz@hedmen.org>2017-09-04 15:51:03 +0200
commit395b36f293849585e961de1454acb44058fc44f6 (patch)
treeb8b54769bc6df02e91828cd67dda65e12cd5ca44 /icemulti
parentca90cc78e1a85ae902e2bc8cc2c6f8b4f7dd7b96 (diff)
downloadicestorm-395b36f293849585e961de1454acb44058fc44f6.tar.gz
icestorm-395b36f293849585e961de1454acb44058fc44f6.tar.bz2
icestorm-395b36f293849585e961de1454acb44058fc44f6.zip
icemulti: Use getopt to parse arguments
Diffstat (limited to 'icemulti')
-rw-r--r--icemulti/icemulti.cc82
1 files changed, 48 insertions, 34 deletions
diff --git a/icemulti/icemulti.cc b/icemulti/icemulti.cc
index 5c5f24c..60b6fea 100644
--- a/icemulti/icemulti.cc
+++ b/icemulti/icemulti.cc
@@ -19,6 +19,7 @@
#include <cstdint>
#include <memory>
+#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -194,6 +195,8 @@ void usage()
int main(int argc, char **argv)
{
+ int c;
+ char *endptr = NULL;
bool coldboot = false;
int por_image = 0;
int image_count = 0;
@@ -203,52 +206,63 @@ int main(int argc, char **argv)
std::unique_ptr<Image> images[NUM_IMAGES];
const char *outfile_name = NULL;
+ static struct option long_options[] = {
+ {NULL, 0, NULL, 0}
+ };
+
program_short_name = strrchr(argv[0], '/');
if (program_short_name == NULL)
program_short_name = argv[0];
else
program_short_name++;
- for (int i = 1; i < argc; i++)
- {
- if (argv[i][0] == '-' && argv[i][1]) {
- for (int j = 1; argv[i][j]; j++)
- if (argv[i][j] == 'c') {
- coldboot = true;
- } else if (argv[i][j] == 'p' && argv[i][j+1]) {
- por_image = argv[i][++j] - '0';
- } else if (argv[i][j] == 'a' || argv[i][j] == 'A') {
- align_first = argv[i][j] == 'A';
- if (argv[i][j+1])
- align_bits = atoi(&argv[i][j+1]);
- else if(i+1 < argc)
- align_bits = atoi(argv[++i]);
- else
- usage();
- break;
- } else if (argv[i][j] == 'o') {
- if (argv[i][j+1])
- outfile_name = &argv[i][j+1];
- else if(i+1 < argc)
- outfile_name = argv[++i];
- else
- usage();
- break;
- } else if (argv[i][j] == 'v') {
- log_level++;
- } else
- usage();
- continue;
+ while ((c = getopt_long(argc, argv, "cp:a:A:o:v",
+ long_options, NULL)) != -1)
+ switch (c) {
+ case 'c':
+ coldboot = true;
+ break;
+ case 'p':
+ if (optarg[0] == '0' && optarg[1] == '\0')
+ por_image = 0;
+ else if (optarg[0] == '1' && optarg[1] == '\0')
+ por_image = 1;
+ else if (optarg[0] == '2' && optarg[1] == '\0')
+ por_image = 2;
+ else if (optarg[0] == '3' && optarg[1] == '\0')
+ por_image = 3;
+ else
+ error("`%s' is not a valid power-on/reset image (must be 0, 1, 2, or 3)\n", optarg);
+ break;
+ case 'A':
+ align_first = true;
+ /* fallthrough */
+ case 'a':
+ align_bits = strtol(optarg, &endptr, 0);
+ if (*endptr != '\0')
+ error("`%s' is not a valid number\n", optarg);
+ break;
+ case 'o':
+ outfile_name = optarg;
+ break;
+ case 'v':
+ log_level++;
+ break;
+ default:
+ usage();
}
+ if (optind == argc) {
+ fprintf(stderr, "%s: missing argument\n", program_short_name);
+ usage();
+ }
+
+ while (optind != argc) {
if (image_count >= NUM_IMAGES)
error("Too many images supplied\n");
- images[image_count++].reset(new Image(argv[i]));
+ images[image_count++].reset(new Image(argv[optind++]));
}
- if (!image_count)
- usage();
-
if (coldboot && por_image != 0)
error("Can't select power on reset boot image in cold boot mode\n");