mkimage: Convert to use getopt()

The current way of parsing arguments is a bit clumsy. It seems better to
use getopt() which is commonly used for this purpose.

Convert the code to use getopt() and make a few minor adjustments as needed.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2016-02-22 22:55:34 -07:00 committed by Tom Rini
parent 0b443dee6b
commit a02221f29d

View File

@ -70,86 +70,65 @@ static void show_image_types(void)
static void process_args(int argc, char **argv)
{
char *ptr;
int opt;
while (--argc > 0 && **++argv == '-') {
while (*++*argv) {
switch (**argv) {
while ((opt = getopt(argc, argv,
"a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
switch (opt) {
case 'l':
params.lflag = 1;
break;
case 'A':
if ((--argc <= 0) ||
(params.arch =
genimg_get_arch_id (*++argv)) < 0)
params.arch = genimg_get_arch_id(optarg);
if (params.arch < 0)
usage();
goto NXTARG;
break;
case 'c':
if (--argc <= 0)
usage();
params.comment = *++argv;
goto NXTARG;
params.comment = optarg;
break;
case 'C':
if ((--argc <= 0) ||
(params.comp =
genimg_get_comp_id (*++argv)) < 0)
params.comp = genimg_get_comp_id(optarg);
if (params.comp < 0)
usage();
goto NXTARG;
break;
case 'D':
if (--argc <= 0)
usage();
params.dtc = *++argv;
goto NXTARG;
params.dtc = optarg;
break;
case 'O':
if ((--argc <= 0) ||
(params.os =
genimg_get_os_id (*++argv)) < 0)
params.os = genimg_get_os_id(optarg);
if (params.os < 0)
usage();
goto NXTARG;
break;
case 'T':
params.type = -1;
if (--argc >= 0 && argv[1]) {
params.type =
genimg_get_type_id(*++argv);
}
params.type = genimg_get_type_id(optarg);
if (params.type < 0) {
show_image_types();
usage();
}
goto NXTARG;
break;
case 'a':
if (--argc <= 0)
usage();
params.addr = strtoull(*++argv, &ptr, 16);
params.addr = strtoull(optarg, &ptr, 16);
if (*ptr) {
fprintf (stderr,
"%s: invalid load address %s\n",
params.cmdname, *argv);
fprintf(stderr, "%s: invalid load address %s\n",
params.cmdname, optarg);
exit(EXIT_FAILURE);
}
goto NXTARG;
break;
case 'd':
if (--argc <= 0)
usage();
params.datafile = *++argv;
params.datafile = optarg;
params.dflag = 1;
goto NXTARG;
break;
case 'e':
if (--argc <= 0)
usage();
params.ep = strtoull(*++argv, &ptr, 16);
params.ep = strtoull(optarg, &ptr, 16);
if (*ptr) {
fprintf (stderr,
"%s: invalid entry point %s\n",
params.cmdname, *argv);
fprintf(stderr, "%s: invalid entry point %s\n",
params.cmdname, optarg);
exit(EXIT_FAILURE);
}
params.eflag = 1;
goto NXTARG;
break;
case 'f':
if (--argc <= 0)
usage();
params.datafile = *++argv;
params.datafile = optarg;
/* no break */
case 'F':
/*
@ -158,34 +137,26 @@ static void process_args(int argc, char **argv)
*/
params.type = IH_TYPE_FLATDT;
params.fflag = 1;
goto NXTARG;
break;
case 'k':
if (--argc <= 0)
usage();
params.keydir = *++argv;
goto NXTARG;
params.keydir = optarg;
break;
case 'K':
if (--argc <= 0)
usage();
params.keydest = *++argv;
goto NXTARG;
params.keydest = optarg;
break;
case 'n':
if (--argc <= 0)
usage();
params.imagename = *++argv;
goto NXTARG;
params.imagename = optarg;
break;
case 'r':
params.require_keys = 1;
break;
case 'R':
if (--argc <= 0)
usage();
/*
* This entry is for the second configuration
* file, if only one is not enough.
*/
params.imagename2 = *++argv;
goto NXTARG;
params.imagename2 = optarg;
break;
case 's':
params.skipcpy = 1;
break;
@ -202,12 +173,10 @@ static void process_args(int argc, char **argv)
usage();
}
}
NXTARG: ;
}
if (argc != 1)
if (optind >= argc)
usage();
params.imagefile = *argv;
params.imagefile = argv[optind];
}