/* Return -1 on error, 0 on success */ int parse_command_line(int argc, char * const argv[]) { int option; const char *optstring = "d:hs:uV"; struct option longopts[] = { { "dev-mem", required_argument, NULL, 'd' }, { "help", no_argument, NULL, 'h' }, { "string", required_argument, NULL, 's' }, { "dump", no_argument, NULL, 'u' }, { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; while ((option = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) switch (option) { case 'd': opt.devmem = optarg; break; case 'h': opt.flags |= FLAG_HELP; break; case 's': if (parse_opt_string(optarg) < 0) return -1; opt.flags |= FLAG_QUIET; break; case 'u': opt.flags |= FLAG_DUMP; break; case 'V': opt.flags |= FLAG_VERSION; break; case '?': switch (optopt) { case 's': fprintf(stderr, "String keyword expected\n"); print_opt_string_list(); break; } return -1; } if ((opt.flags & FLAG_DUMP) && opt.string != NULL) { fprintf(stderr, "Options --string and --dump are mutually exclusive\n"); return -1; } return 0; }
static int parse_opt_string(const char *arg) { unsigned int i; if (opt.string) { fprintf(stderr, "Only one string can be specified\n"); return -1; } for (i = 0; i < ARRAY_SIZE(opt_string_keyword); i++) { if (!strcasecmp(arg, opt_string_keyword[i].keyword)) { opt.string = &opt_string_keyword[i]; return 0; } } fprintf(stderr, "Invalid string keyword: %s\n", arg); print_opt_string_list(); return -1; }
static int parse_opt_string(const char *arg) { unsigned int i; if(opt.string_offset) { fprintf(stderr, "Only one string can be specified\n"); return -1; } for(i=0; i<sizeof(opt_string_keyword)/sizeof(struct string_keyword); i++) { if(!strcasecmp(arg, opt_string_keyword[i].keyword)) { opt.string_type=opt_string_keyword[i].type; opt.string_offset=opt_string_keyword[i].offset; return 0; } } fprintf(stderr, "Invalid string keyword: %s\n", arg); print_opt_string_list(); return -1; }
/* Return -1 on error, 0 on success */ int parse_command_line(int argc, char * const argv[]) { int option; const char *optstring = "d:hqs:t:uVi:"; struct option longopts[] = { { "dev-mem", required_argument, NULL, 'd' }, { "input-file", required_argument, NULL, 'i' }, { "help", no_argument, NULL, 'h' }, { "quiet", no_argument, NULL, 'q' }, { "string", required_argument, NULL, 's' }, { "type", required_argument, NULL, 't' }, { "dump", no_argument, NULL, 'u' }, { "dump-bin", required_argument, NULL, 'B' }, { "from-dump", required_argument, NULL, 'F' }, { "version", no_argument, NULL, 'V' }, { 0, 0, 0, 0 } }; while ((option = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) switch (option) { case 'i': opt.devmem = optarg; opt.flags |= FLAG_READFILE; break; case 'B': opt.flags |= FLAG_DUMP_BIN; opt.dumpfile = optarg; break; case 'F': opt.flags |= FLAG_FROM_DUMP; opt.dumpfile = optarg; break; case 'd': opt.devmem = optarg; break; case 'h': opt.flags |= FLAG_HELP; break; case 'q': opt.flags |= FLAG_QUIET; break; case 's': if (parse_opt_string(optarg) < 0) return -1; opt.flags |= FLAG_QUIET; break; case 't': opt.type = parse_opt_type(opt.type, optarg); if (opt.type == NULL) return -1; break; case 'u': opt.flags |= FLAG_DUMP; break; case 'V': opt.flags |= FLAG_VERSION; break; case '?': switch (optopt) { case 's': fprintf(stderr, "String keyword expected\n"); print_opt_string_list(); break; case 't': fprintf(stderr, "Type number or keyword expected\n"); print_opt_type_list(); break; } return -1; } /* Check for mutually exclusive output format options */ if ((opt.string != NULL) + (opt.type != NULL) + !!(opt.flags & FLAG_DUMP_BIN) > 1) { fprintf(stderr, "Options --string, --type and --dump-bin are mutually exclusive\n"); return -1; } if ((opt.flags & FLAG_FROM_DUMP) && (opt.flags & FLAG_DUMP_BIN)) { fprintf(stderr, "Options --from-dump and --dump-bin are mutually exclusive\n"); return -1; } return 0; }
/* Return -1 on error, 0 on success */ int parse_command_line(int argc, char * const argv[]) { int option; const char *optstring = ":d:hqs:t:uV"; struct option longopts[]={ { "dev-mem", required_argument, NULL, 'd' }, { "help", no_argument, NULL, 'h' }, { "quiet", no_argument, NULL, 'q' }, { "string", required_argument, NULL, 's' }, { "type", required_argument, NULL, 't' }, { "dump", no_argument, NULL, 'u' }, { "version", no_argument, NULL, 'V' }, { 0, 0, 0, 0 } }; while((option=getopt_long(argc, argv, optstring, longopts, NULL))!=-1) switch(option) { case 'd': opt.devmem=optarg; break; case 'h': opt.flags|=FLAG_HELP; break; case 'q': opt.flags|=FLAG_QUIET; break; case 's': if(parse_opt_string(optarg)<0) return -1; opt.flags|=FLAG_QUIET; break; case 't': opt.type=parse_opt_type(opt.type, optarg); if(opt.type==NULL) return -1; break; case 'u': opt.flags|=FLAG_DUMP; break; case 'V': opt.flags|=FLAG_VERSION; break; case ':': switch(optopt) { case 's': fprintf(stderr, "String keyword expected\n"); print_opt_string_list(); break; case 't': fprintf(stderr, "Type number or keyword expected\n"); print_opt_type_list(); break; } /* fall through */ case '?': return -1; } if(opt.type!=NULL && opt.string_offset) { fprintf(stderr, "Options --string and --type are mutually exclusive\n"); return -1; } if((opt.flags & FLAG_DUMP) && opt.string_offset) { fprintf(stderr, "Options --string and --dump are mutually exclusive\n"); return -1; } if((opt.flags & FLAG_DUMP) && (opt.flags & FLAG_QUIET)) { fprintf(stderr, "Options --quiet and --dump are mutually exclusive\n"); return -1; } return 0; }