21 #define FIX_UNUSED(X) (void) (X)
32 const char *gengetopt_args_info_versiontext =
"";
37 " -h, --help Print help and exit",
38 " -V, --version Print version and exit",
39 " -r, --raw output raw crunched data without header (default=off)",
40 " -a, --algorithm=ENUM crunching algorithm to use (possible values=\"xipz\",\n \"qadz\" default=`xipz')",
41 " -j, --jump=INT address to jump to (-1 = load address) (default=`-1')",
49 } cmdline_parser_arg_type;
64 gengetopt_strdup (
const char *s);
79 FIX_UNUSED (args_info);
108 if (strlen(gengetopt_args_info_versiontext) > 0)
109 printf(
"\n%s\n", gengetopt_args_info_versiontext);
112 static void print_help_common(
void)
120 if (len_purpose > 0) {
124 if (len_usage || len_purpose) {
145 clear_given (args_info);
146 clear_args (args_info);
147 init_args_info (args_info);
176 free_string_field (
char **s)
191 free_string_field (&(args_info->
jump_orig));
195 free (args_info->
inputs [i]);
200 clear_given (args_info);
211 check_possible_values(
const char *val,
const char *values[])
221 for (i = 0, len = strlen(val); values[i]; ++i)
223 if (strncmp(val, values[i], len) == 0)
227 if (strlen(values[i]) == len)
235 return (found ? -2 : -1);
240 write_into_file(FILE *outfile,
const char *opt,
const char *arg,
const char *values[])
245 found = check_possible_values(arg, values);
248 fprintf(outfile,
"%s=\"%s\" # %s\n", opt, arg, values[found]);
250 fprintf(outfile,
"%s=\"%s\"\n", opt, arg);
252 fprintf(outfile,
"%s\n", opt);
269 write_into_file(outfile,
"help", 0, 0 );
271 write_into_file(outfile,
"version", 0, 0 );
273 write_into_file(outfile,
"raw", 0, 0 );
277 write_into_file(outfile,
"jump", args_info->
jump_orig, 0);
290 outfile = fopen(filename,
"w");
307 cmdline_parser_release (args_info);
312 gengetopt_strdup (
const char *s)
318 result = (
char*)malloc(strlen(s) + 1);
319 if (result == (
char*)0)
336 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
338 if (result == EXIT_FAILURE)
359 result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0);
361 if (result == EXIT_FAILURE)
373 FIX_UNUSED (args_info);
374 FIX_UNUSED (prog_name);
379 static char *package_name = 0;
400 int update_arg(
void *field,
char **orig_field,
401 unsigned int *field_given,
unsigned int *prev_given,
402 char *value,
const char *possible_values[],
403 const char *default_value,
404 cmdline_parser_arg_type arg_type,
406 int no_free,
int multiple_option,
407 const char *long_opt,
char short_opt,
408 const char *additional_error)
411 const char *val = value;
418 if (!multiple_option && prev_given && (*prev_given || (
check_ambiguity && *field_given)))
420 if (short_opt !=
'-')
421 fprintf (stderr,
"%s: `--%s' (`-%c') option given more than once%s\n",
422 package_name, long_opt, short_opt,
423 (additional_error ? additional_error :
""));
425 fprintf (stderr,
"%s: `--%s' option given more than once%s\n",
426 package_name, long_opt,
427 (additional_error ? additional_error :
""));
431 if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0)
433 if (short_opt !=
'-')
434 fprintf (stderr,
"%s: %s argument, \"%s\", for option `--%s' (`-%c')%s\n",
435 package_name, (found == -2) ?
"ambiguous" :
"invalid", value, long_opt, short_opt,
436 (additional_error ? additional_error :
""));
438 fprintf (stderr,
"%s: %s argument, \"%s\", for option `--%s'%s\n",
439 package_name, (found == -2) ?
"ambiguous" :
"invalid", value, long_opt,
440 (additional_error ? additional_error :
""));
444 if (field_given && *field_given && !
override)
451 val = possible_values[found];
455 *((
int *)field) = !*((
int *)field);
458 if (val) *((
int *)field) = strtol (val, &stop_char, 0);
461 if (val) *((
int *)field) = found;
470 if (val && !(stop_char && *stop_char ==
'\0')) {
471 fprintf(stderr,
"%s: invalid numeric value: %s\n", package_name, val);
485 if (value && orig_field) {
491 *orig_field = gengetopt_strdup (value);
501 cmdline_parser_internal (
507 int error_occurred = 0;
515 package_name = argv[0];
519 FIX_UNUSED(
override);
526 FIX_UNUSED(check_ambiguity);
540 int option_index = 0;
542 static struct option long_options[] = {
543 {
"help", 0, NULL,
'h' },
544 {
"version", 0, NULL,
'V' },
545 {
"raw", 0, NULL,
'r' },
546 {
"algorithm", 1, NULL,
'a' },
547 {
"jump", 1, NULL,
'j' },
551 c = getopt_long (argc, argv,
"hVra:j:", long_options, &option_index);
571 &(local_args_info.raw_given), optarg, 0, 0, ARG_FLAG,
572 check_ambiguity,
override, 1, 0,
"raw",
'r',
583 check_ambiguity,
override, 0, 0,
592 if (update_arg( (
void *)&(args_info->
jump_arg),
594 &(local_args_info.jump_given), optarg, 0,
"-1", ARG_INT,
595 check_ambiguity,
override, 0, 0,
608 fprintf (stderr,
"%s: option unknown: %c%s\n",
CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error :
""));
615 FIX_UNUSED(check_required);
617 cmdline_parser_release (&local_args_info);
619 if ( error_occurred )
620 return (EXIT_FAILURE);
625 int found_prog_name = 0;
632 if (argv[i++] == argv[0]) {
638 args_info->
inputs_num = argc - optind - found_prog_name;
640 (
char **)(malloc ((args_info->
inputs_num)*
sizeof(
char *))) ;
641 while (optind < argc)
642 if (argv[optind++] != argv[0])
643 args_info->
inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
650 cmdline_parser_release (&local_args_info);
651 return (EXIT_FAILURE);