mirror of
https://review.coreboot.org/flashrom.git
synced 2025-04-28 07:23:43 +02:00
cli_classic: Tidy up some repeated handling patterns into funcs
Introduce cli_classic_single_operation() to consolidate the repeating pattern of multiple CLI operations at once. Also modify cli_classic_abort_usage() to take an optional error abort string and print it to stderr, this allows for trimming a few more lines off the cli implementation. V.2: A few fixes upon review: - Trim off some unnecessary braces for single line branches. - Pass 'operation_specified' by reference. - Rename a function. V.3: Fix print order of cli_classic_abort_usage(). Change-Id: I54598efdaee2b95cb278b0f2aac05f48bbd95bef Signed-off-by: Edward O'Callaghan <quasisec@chromium.org> Reviewed-on: https://review.coreboot.org/c/flashrom/+/35611 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Nico Huber <nico.h@gmx.de>
This commit is contained in:
parent
16ec45c0fc
commit
8b60fc7a5b
195
cli_classic.c
195
cli_classic.c
@ -76,12 +76,21 @@ static void cli_classic_usage(const char *name)
|
|||||||
"If no operation is specified, flashrom will only probe for flash chips.\n");
|
"If no operation is specified, flashrom will only probe for flash chips.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cli_classic_abort_usage(void)
|
static void cli_classic_abort_usage(const char *msg)
|
||||||
{
|
{
|
||||||
|
if (msg)
|
||||||
|
fprintf(stderr, "%s", msg);
|
||||||
printf("Please run \"flashrom --help\" for usage info.\n");
|
printf("Please run \"flashrom --help\" for usage info.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void cli_classic_validate_singleop(int *operation_specified)
|
||||||
|
{
|
||||||
|
if (++(*operation_specified) > 1) {
|
||||||
|
cli_classic_abort_usage("More than one operation specified. Aborting.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int check_filename(char *filename, const char *type)
|
static int check_filename(char *filename, const char *type)
|
||||||
{
|
{
|
||||||
if (!filename || (filename[0] == '\0')) {
|
if (!filename || (filename[0] == '\0')) {
|
||||||
@ -177,41 +186,27 @@ int main(int argc, char *argv[])
|
|||||||
long_options, &option_index)) != EOF) {
|
long_options, &option_index)) != EOF) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'r':
|
case 'r':
|
||||||
if (++operation_specified > 1) {
|
cli_classic_validate_singleop(&operation_specified);
|
||||||
fprintf(stderr, "More than one operation "
|
|
||||||
"specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
filename = strdup(optarg);
|
filename = strdup(optarg);
|
||||||
read_it = 1;
|
read_it = 1;
|
||||||
break;
|
break;
|
||||||
case 'w':
|
case 'w':
|
||||||
if (++operation_specified > 1) {
|
cli_classic_validate_singleop(&operation_specified);
|
||||||
fprintf(stderr, "More than one operation "
|
|
||||||
"specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
filename = strdup(optarg);
|
filename = strdup(optarg);
|
||||||
write_it = 1;
|
write_it = 1;
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
//FIXME: gracefully handle superfluous -v
|
//FIXME: gracefully handle superfluous -v
|
||||||
if (++operation_specified > 1) {
|
cli_classic_validate_singleop(&operation_specified);
|
||||||
fprintf(stderr, "More than one operation "
|
|
||||||
"specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
if (dont_verify_it) {
|
if (dont_verify_it) {
|
||||||
fprintf(stderr, "--verify and --noverify are mutually exclusive. Aborting.\n");
|
cli_classic_abort_usage("--verify and --noverify are mutually exclusive. Aborting.\n");
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
}
|
||||||
filename = strdup(optarg);
|
filename = strdup(optarg);
|
||||||
verify_it = 1;
|
verify_it = 1;
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
if (verify_it) {
|
if (verify_it) {
|
||||||
fprintf(stderr, "--verify and --noverify are mutually exclusive. Aborting.\n");
|
cli_classic_abort_usage("--verify and --noverify are mutually exclusive. Aborting.\n");
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
}
|
||||||
dont_verify_it = 1;
|
dont_verify_it = 1;
|
||||||
break;
|
break;
|
||||||
@ -227,132 +222,87 @@ int main(int argc, char *argv[])
|
|||||||
verbose_logfile = verbose_screen;
|
verbose_logfile = verbose_screen;
|
||||||
break;
|
break;
|
||||||
case 'E':
|
case 'E':
|
||||||
if (++operation_specified > 1) {
|
cli_classic_validate_singleop(&operation_specified);
|
||||||
fprintf(stderr, "More than one operation "
|
|
||||||
"specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
erase_it = 1;
|
erase_it = 1;
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
force = 1;
|
force = 1;
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
if (layoutfile) {
|
if (layoutfile)
|
||||||
fprintf(stderr, "Error: --layout specified "
|
cli_classic_abort_usage("Error: --layout specified more than once. Aborting.\n");
|
||||||
"more than once. Aborting.\n");
|
if (ifd)
|
||||||
cli_classic_abort_usage();
|
cli_classic_abort_usage("Error: --layout and --ifd both specified. Aborting.\n");
|
||||||
}
|
if (fmap)
|
||||||
if (ifd) {
|
cli_classic_abort_usage("Error: --layout and --fmap-file both specified. Aborting.\n");
|
||||||
fprintf(stderr, "Error: --layout and --ifd both specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
if (fmap) {
|
|
||||||
fprintf(stderr, "Error: --layout and --fmap-file both specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
layoutfile = strdup(optarg);
|
layoutfile = strdup(optarg);
|
||||||
break;
|
break;
|
||||||
case OPTION_IFD:
|
case OPTION_IFD:
|
||||||
if (layoutfile) {
|
if (layoutfile)
|
||||||
fprintf(stderr, "Error: --layout and --ifd both specified. Aborting.\n");
|
cli_classic_abort_usage("Error: --layout and --ifd both specified. Aborting.\n");
|
||||||
cli_classic_abort_usage();
|
if (fmap)
|
||||||
}
|
cli_classic_abort_usage("Error: --fmap-file and --ifd both specified. Aborting.\n");
|
||||||
if (fmap) {
|
|
||||||
fprintf(stderr, "Error: --fmap-file and --ifd both specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
ifd = 1;
|
ifd = 1;
|
||||||
break;
|
break;
|
||||||
case OPTION_FMAP_FILE:
|
case OPTION_FMAP_FILE:
|
||||||
if (fmap) {
|
if (fmap)
|
||||||
fprintf(stderr, "Error: --fmap or --fmap-file specified "
|
cli_classic_abort_usage("Error: --fmap or --fmap-file specified "
|
||||||
"more than once. Aborting.\n");
|
"more than once. Aborting.\n");
|
||||||
cli_classic_abort_usage();
|
if (ifd)
|
||||||
}
|
cli_classic_abort_usage("Error: --fmap-file and --ifd both specified. Aborting.\n");
|
||||||
if (ifd) {
|
if (layoutfile)
|
||||||
fprintf(stderr, "Error: --fmap-file and --ifd both specified. Aborting.\n");
|
cli_classic_abort_usage("Error: --fmap-file and --layout both specified. Aborting.\n");
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
if (layoutfile) {
|
|
||||||
fprintf(stderr, "Error: --fmap-file and --layout both specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
fmapfile = strdup(optarg);
|
fmapfile = strdup(optarg);
|
||||||
fmap = 1;
|
fmap = 1;
|
||||||
break;
|
break;
|
||||||
case OPTION_FMAP:
|
case OPTION_FMAP:
|
||||||
if (fmap) {
|
if (fmap)
|
||||||
fprintf(stderr, "Error: --fmap or --fmap-file specified "
|
cli_classic_abort_usage("Error: --fmap or --fmap-file specified "
|
||||||
"more than once. Aborting.\n");
|
"more than once. Aborting.\n");
|
||||||
cli_classic_abort_usage();
|
if (ifd)
|
||||||
}
|
cli_classic_abort_usage("Error: --fmap and --ifd both specified. Aborting.\n");
|
||||||
if (ifd) {
|
if (layoutfile)
|
||||||
fprintf(stderr, "Error: --fmap and --ifd both specified. Aborting.\n");
|
cli_classic_abort_usage("Error: --layout and --fmap both specified. Aborting.\n");
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
if (layoutfile) {
|
|
||||||
fprintf(stderr, "Error: --layout and --fmap both specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
fmap = 1;
|
fmap = 1;
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
tempstr = strdup(optarg);
|
tempstr = strdup(optarg);
|
||||||
if (register_include_arg(&include_args, tempstr)) {
|
if (register_include_arg(&include_args, tempstr)) {
|
||||||
free(tempstr);
|
free(tempstr);
|
||||||
cli_classic_abort_usage();
|
cli_classic_abort_usage(NULL);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case OPTION_FLASH_CONTENTS:
|
case OPTION_FLASH_CONTENTS:
|
||||||
referencefile = strdup(optarg);
|
referencefile = strdup(optarg);
|
||||||
break;
|
break;
|
||||||
case OPTION_FLASH_NAME:
|
case OPTION_FLASH_NAME:
|
||||||
if (++operation_specified > 1) {
|
cli_classic_validate_singleop(&operation_specified);
|
||||||
fprintf(stderr, "More than one operation "
|
|
||||||
"specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
flash_name = 1;
|
flash_name = 1;
|
||||||
break;
|
break;
|
||||||
case OPTION_FLASH_SIZE:
|
case OPTION_FLASH_SIZE:
|
||||||
if (++operation_specified > 1) {
|
cli_classic_validate_singleop(&operation_specified);
|
||||||
fprintf(stderr, "More than one operation "
|
|
||||||
"specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
flash_size = 1;
|
flash_size = 1;
|
||||||
break;
|
break;
|
||||||
case 'L':
|
case 'L':
|
||||||
if (++operation_specified > 1) {
|
cli_classic_validate_singleop(&operation_specified);
|
||||||
fprintf(stderr, "More than one operation "
|
|
||||||
"specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
list_supported = 1;
|
list_supported = 1;
|
||||||
break;
|
break;
|
||||||
case 'z':
|
case 'z':
|
||||||
#if CONFIG_PRINT_WIKI == 1
|
#if CONFIG_PRINT_WIKI == 1
|
||||||
if (++operation_specified > 1) {
|
cli_classic_validate_singleop(&operation_specified);
|
||||||
fprintf(stderr, "More than one operation "
|
|
||||||
"specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
list_supported_wiki = 1;
|
list_supported_wiki = 1;
|
||||||
#else
|
#else
|
||||||
fprintf(stderr, "Error: Wiki output was not compiled "
|
cli_classic_abort_usage("Error: Wiki output was not"
|
||||||
"in. Aborting.\n");
|
"compiled in. Aborting.\n");
|
||||||
cli_classic_abort_usage();
|
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
if (prog != PROGRAMMER_INVALID) {
|
if (prog != PROGRAMMER_INVALID) {
|
||||||
fprintf(stderr, "Error: --programmer specified "
|
cli_classic_abort_usage("Error: --programmer specified "
|
||||||
"more than once. You can separate "
|
"more than once. You can separate "
|
||||||
"multiple\nparameters for a programmer "
|
"multiple\nparameters for a programmer "
|
||||||
"with \",\". Please see the man page "
|
"with \",\". Please see the man page "
|
||||||
"for details.\n");
|
"for details.\n");
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
}
|
||||||
for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
|
for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
|
||||||
name = programmer_table[prog].name;
|
name = programmer_table[prog].name;
|
||||||
@ -384,31 +334,22 @@ int main(int argc, char *argv[])
|
|||||||
optarg);
|
optarg);
|
||||||
list_programmers_linebreak(0, 80, 0);
|
list_programmers_linebreak(0, 80, 0);
|
||||||
msg_ginfo(".\n");
|
msg_ginfo(".\n");
|
||||||
cli_classic_abort_usage();
|
cli_classic_abort_usage(NULL);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'R':
|
case 'R':
|
||||||
/* print_version() is always called during startup. */
|
/* print_version() is always called during startup. */
|
||||||
if (++operation_specified > 1) {
|
cli_classic_validate_singleop(&operation_specified);
|
||||||
fprintf(stderr, "More than one operation "
|
|
||||||
"specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
exit(0);
|
exit(0);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
if (++operation_specified > 1) {
|
cli_classic_validate_singleop(&operation_specified);
|
||||||
fprintf(stderr, "More than one operation "
|
|
||||||
"specified. Aborting.\n");
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
cli_classic_usage(argv[0]);
|
cli_classic_usage(argv[0]);
|
||||||
exit(0);
|
exit(0);
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
#ifdef STANDALONE
|
#ifdef STANDALONE
|
||||||
fprintf(stderr, "Log file not supported in standalone mode. Aborting.\n");
|
cli_classic_abort_usage("Log file not supported in standalone mode. Aborting.\n");
|
||||||
cli_classic_abort_usage();
|
|
||||||
#else /* STANDALONE */
|
#else /* STANDALONE */
|
||||||
if (logfile) {
|
if (logfile) {
|
||||||
fprintf(stderr, "Warning: -o/--output specified multiple times.\n");
|
fprintf(stderr, "Warning: -o/--output specified multiple times.\n");
|
||||||
@ -417,40 +358,32 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
logfile = strdup(optarg);
|
logfile = strdup(optarg);
|
||||||
if (logfile[0] == '\0') {
|
if (logfile[0] == '\0') {
|
||||||
fprintf(stderr, "No log filename specified.\n");
|
cli_classic_abort_usage("No log filename specified.\n");
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
}
|
||||||
#endif /* STANDALONE */
|
#endif /* STANDALONE */
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
cli_classic_abort_usage();
|
cli_classic_abort_usage(NULL);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (optind < argc) {
|
if (optind < argc)
|
||||||
fprintf(stderr, "Error: Extra parameter found.\n");
|
cli_classic_abort_usage("Error: Extra parameter found.\n");
|
||||||
cli_classic_abort_usage();
|
if ((read_it | write_it | verify_it) && check_filename(filename, "image"))
|
||||||
}
|
cli_classic_abort_usage(NULL);
|
||||||
|
if (layoutfile && check_filename(layoutfile, "layout"))
|
||||||
if ((read_it | write_it | verify_it) && check_filename(filename, "image")) {
|
cli_classic_abort_usage(NULL);
|
||||||
cli_classic_abort_usage();
|
if (fmapfile && check_filename(fmapfile, "fmap"))
|
||||||
}
|
cli_classic_abort_usage(NULL);
|
||||||
if (layoutfile && check_filename(layoutfile, "layout")) {
|
if (referencefile && check_filename(referencefile, "reference"))
|
||||||
cli_classic_abort_usage();
|
cli_classic_abort_usage(NULL);
|
||||||
}
|
|
||||||
if (fmapfile && check_filename(fmapfile, "fmap")) {
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
if (referencefile && check_filename(referencefile, "reference")) {
|
|
||||||
cli_classic_abort_usage();
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef STANDALONE
|
#ifndef STANDALONE
|
||||||
if (logfile && check_filename(logfile, "log"))
|
if (logfile && check_filename(logfile, "log"))
|
||||||
cli_classic_abort_usage();
|
cli_classic_abort_usage(NULL);
|
||||||
if (logfile && open_logfile(logfile))
|
if (logfile && open_logfile(logfile))
|
||||||
cli_classic_abort_usage();
|
cli_classic_abort_usage(NULL);
|
||||||
#endif /* !STANDALONE */
|
#endif /* !STANDALONE */
|
||||||
|
|
||||||
#if CONFIG_PRINT_WIKI == 1
|
#if CONFIG_PRINT_WIKI == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user