intel: aubinator: use getopt to parse arguments

Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Reviewed-by: Sirisha Gandikota <sirisha.gandikota@intel.com>
This commit is contained in:
Lionel Landwerlin
2016-10-04 16:29:55 +01:00
parent a198883bf7
commit 0b10152b80

View File

@@ -24,6 +24,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <getopt.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@@ -1040,30 +1041,13 @@ print_help(const char *progname, FILE *file)
progname); progname);
} }
static bool
is_prefix(const char *arg, const char *prefix, const char **value)
{
int l = strlen(prefix);
if (strncmp(arg, prefix, l) == 0 && (arg[l] == '\0' || arg[l] == '=')) {
if (arg[l] == '=')
*value = arg + l + 1;
else
*value = NULL;
return true;
}
return false;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct gen_spec *spec; struct gen_spec *spec;
struct aub_file *file; struct aub_file *file;
int i; int c, i;
bool found_arg_gen = false, pager = true; bool help = false, pager = true;
const char *value, *input_file; const char *input_file = NULL;
char gen_file[256], gen_val[24]; char gen_file[256], gen_val[24];
const struct { const struct {
const char *name; const char *name;
@@ -1080,55 +1064,47 @@ int main(int argc, char *argv[])
{ "kbl", 0x591D, 9, 0 }, /* Intel(R) Kabylake GT2 */ { "kbl", 0x591D, 9, 0 }, /* Intel(R) Kabylake GT2 */
{ "bxt", 0x0A84, 9, 0 } /* Intel(R) HD Graphics (Broxton) */ { "bxt", 0x0A84, 9, 0 } /* Intel(R) HD Graphics (Broxton) */
}, *gen = NULL; }, *gen = NULL;
const struct option aubinator_opts[] = {
{ "help", no_argument, (int *) &help, true },
{ "no-pager", no_argument, (int *) &pager, false },
{ "no-offsets", no_argument, (int *) &option_print_offsets, false },
{ "gen", required_argument, NULL, 'g' },
{ "headers", no_argument, (int *) &option_full_decode, false },
{ "color", required_argument, NULL, 'c' },
{ NULL, 0, NULL, 0 }
};
if (argc == 1) { i = 0;
print_help(argv[0], stderr); while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
exit(EXIT_FAILURE); switch (c) {
} case 'g':
snprintf(gen_val, sizeof(gen_val), "%s", optarg);
for (i = 1; i < argc; ++i) { break;
if (strcmp(argv[i], "--no-pager") == 0) { case 'c':
pager = false; if (optarg == NULL || strcmp(optarg, "always") == 0)
} else if (strcmp(argv[i], "--no-offsets") == 0) {
option_print_offsets = false;
} else if (is_prefix(argv[i], "--gen", &value)) {
if (value == NULL) {
fprintf(stderr, "option '--gen' requires an argument\n");
exit(EXIT_FAILURE);
}
found_arg_gen = true;
snprintf(gen_val, sizeof(gen_val), "%s", value);
} else if (strcmp(argv[i], "--headers") == 0) {
option_full_decode = false;
} else if (is_prefix(argv[i], "--color", &value)) {
if (value == NULL || strcmp(value, "always") == 0)
option_color = COLOR_ALWAYS; option_color = COLOR_ALWAYS;
else if (strcmp(value, "never") == 0) else if (strcmp(optarg, "never") == 0)
option_color = COLOR_NEVER; option_color = COLOR_NEVER;
else if (strcmp(value, "auto") == 0) else if (strcmp(optarg, "auto") == 0)
option_color = COLOR_AUTO; option_color = COLOR_AUTO;
else { else {
fprintf(stderr, "invalid value for --color: %s", value); fprintf(stderr, "invalid value for --color: %s", optarg);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else if (strcmp(argv[i], "--help") == 0) { break;
print_help(argv[0], stdout); default:
exit(EXIT_SUCCESS);
} else {
if (argv[i][0] == '-') {
fprintf(stderr, "unknown option %s\n", argv[i]);
exit(EXIT_FAILURE);
}
input_file = argv[i];
break; break;
} }
} }
if (!found_arg_gen) { if (help || argc == 1) {
fprintf(stderr, "argument --gen is required\n"); print_help(argv[0], stderr);
exit(EXIT_FAILURE); exit(0);
} }
if (optind < argc)
input_file = argv[optind];
for (i = 0; i < ARRAY_SIZE(gens); i++) { for (i = 0; i < ARRAY_SIZE(gens); i++) {
if (!strcmp(gen_val, gens[i].name)) { if (!strcmp(gen_val, gens[i].name)) {
gen = &gens[i]; gen = &gens[i];