XipZ
Mini packer ▶►▸ for small programs.
cmdline.c
1 /*
2  File autogenerated by gengetopt version 2.23
3  generated with the following command:
4  gengetopt -i command_line.ggo --unnamed-opts
5 
6  The developers of gengetopt consider the fixed text that goes in all
7  gengetopt output files to be in the public domain:
8  we make no copyright claims on it.
9 */
10 
11 /* If we use autoconf. */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #ifndef FIX_UNUSED
21 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22 #endif
23 
24 #include <getopt.h>
25 
26 #include "cmdline.h"
27 
28 const char *gengetopt_args_info_purpose = "";
29 
30 const char *gengetopt_args_info_usage = "Usage: XipZ [OPTION]... [FILE]...";
31 
32 const char *gengetopt_args_info_versiontext = "";
33 
35 
36 const char *gengetopt_args_info_help[] = {
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')",
42  0
43 };
44 
45 typedef enum {ARG_NO
46  , ARG_FLAG
47  , ARG_INT
48  , ARG_ENUM
49 } cmdline_parser_arg_type;
50 
51 static
52 void clear_given (struct gengetopt_args_info *args_info);
53 static
54 void clear_args (struct gengetopt_args_info *args_info);
55 
56 static int
57 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
58  struct cmdline_parser_params *params, const char *additional_error);
59 
60 
61 const char *cmdline_parser_algorithm_values[] = {"xipz", "qadz", 0}; /*< Possible values for algorithm. */
62 
63 static char *
64 gengetopt_strdup (const char *s);
65 
66 static
67 void clear_given (struct gengetopt_args_info *args_info)
68 {
69  args_info->help_given = 0 ;
70  args_info->version_given = 0 ;
71  args_info->raw_given = 0 ;
72  args_info->algorithm_given = 0 ;
73  args_info->jump_given = 0 ;
74 }
75 
76 static
77 void clear_args (struct gengetopt_args_info *args_info)
78 {
79  FIX_UNUSED (args_info);
80  args_info->raw_flag = 0;
81  args_info->algorithm_arg = algorithm_arg_xipz;
82  args_info->algorithm_orig = NULL;
83  args_info->jump_arg = -1;
84  args_info->jump_orig = NULL;
85 
86 }
87 
88 static
89 void init_args_info(struct gengetopt_args_info *args_info)
90 {
91 
92 
93  args_info->help_help = gengetopt_args_info_help[0] ;
94  args_info->version_help = gengetopt_args_info_help[1] ;
95  args_info->raw_help = gengetopt_args_info_help[2] ;
97  args_info->jump_help = gengetopt_args_info_help[4] ;
98 
99 }
100 
101 void
103 {
104  printf ("%s %s\n",
107 
108  if (strlen(gengetopt_args_info_versiontext) > 0)
109  printf("\n%s\n", gengetopt_args_info_versiontext);
110 }
111 
112 static void print_help_common(void)
113 {
114  size_t len_purpose = strlen(gengetopt_args_info_purpose);
115  size_t len_usage = strlen(gengetopt_args_info_usage);
116 
117  if (len_usage > 0) {
118  printf("%s\n", gengetopt_args_info_usage);
119  }
120  if (len_purpose > 0) {
121  printf("%s\n", gengetopt_args_info_purpose);
122  }
123 
124  if (len_usage || len_purpose) {
125  printf("\n");
126  }
127 
128  if (strlen(gengetopt_args_info_description) > 0) {
129  printf("%s\n\n", gengetopt_args_info_description);
130  }
131 }
132 
133 void
135 {
136  int i = 0;
137  print_help_common();
138  while (gengetopt_args_info_help[i])
139  printf("%s\n", gengetopt_args_info_help[i++]);
140 }
141 
142 void
144 {
145  clear_given (args_info);
146  clear_args (args_info);
147  init_args_info (args_info);
148 
149  args_info->inputs = 0;
150  args_info->inputs_num = 0;
151 }
152 
153 void
155 {
156  if (params)
157  {
158  params->override = 0;
159  params->initialize = 1;
160  params->check_required = 1;
161  params->check_ambiguity = 0;
162  params->print_errors = 1;
163  }
164 }
165 
166 struct cmdline_parser_params *
168 {
169  struct cmdline_parser_params *params =
170  (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
172  return params;
173 }
174 
175 static void
176 free_string_field (char **s)
177 {
178  if (*s)
179  {
180  free (*s);
181  *s = 0;
182  }
183 }
184 
185 
186 static void
187 cmdline_parser_release (struct gengetopt_args_info *args_info)
188 {
189  unsigned int i;
190  free_string_field (&(args_info->algorithm_orig));
191  free_string_field (&(args_info->jump_orig));
192 
193 
194  for (i = 0; i < args_info->inputs_num; ++i)
195  free (args_info->inputs [i]);
196 
197  if (args_info->inputs_num)
198  free (args_info->inputs);
199 
200  clear_given (args_info);
201 }
202 
210 static int
211 check_possible_values(const char *val, const char *values[])
212 {
213  int i, found, last;
214  size_t len;
215 
216  if (!val) /* otherwise strlen() crashes below */
217  return -1; /* -1 means no argument for the option */
218 
219  found = last = 0;
220 
221  for (i = 0, len = strlen(val); values[i]; ++i)
222  {
223  if (strncmp(val, values[i], len) == 0)
224  {
225  ++found;
226  last = i;
227  if (strlen(values[i]) == len)
228  return i; /* exact macth no need to check more */
229  }
230  }
231 
232  if (found == 1) /* one match: OK */
233  return last;
234 
235  return (found ? -2 : -1); /* return many values or none matched */
236 }
237 
238 
239 static void
240 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
241 {
242  int found = -1;
243  if (arg) {
244  if (values) {
245  found = check_possible_values(arg, values);
246  }
247  if (found >= 0)
248  fprintf(outfile, "%s=\"%s\" # %s\n", opt, arg, values[found]);
249  else
250  fprintf(outfile, "%s=\"%s\"\n", opt, arg);
251  } else {
252  fprintf(outfile, "%s\n", opt);
253  }
254 }
255 
256 
257 int
258 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
259 {
260  int i = 0;
261 
262  if (!outfile)
263  {
264  fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
265  return EXIT_FAILURE;
266  }
267 
268  if (args_info->help_given)
269  write_into_file(outfile, "help", 0, 0 );
270  if (args_info->version_given)
271  write_into_file(outfile, "version", 0, 0 );
272  if (args_info->raw_given)
273  write_into_file(outfile, "raw", 0, 0 );
274  if (args_info->algorithm_given)
275  write_into_file(outfile, "algorithm", args_info->algorithm_orig, cmdline_parser_algorithm_values);
276  if (args_info->jump_given)
277  write_into_file(outfile, "jump", args_info->jump_orig, 0);
278 
279 
280  i = EXIT_SUCCESS;
281  return i;
282 }
283 
284 int
285 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
286 {
287  FILE *outfile;
288  int i = 0;
289 
290  outfile = fopen(filename, "w");
291 
292  if (!outfile)
293  {
294  fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
295  return EXIT_FAILURE;
296  }
297 
298  i = cmdline_parser_dump(outfile, args_info);
299  fclose (outfile);
300 
301  return i;
302 }
303 
304 void
306 {
307  cmdline_parser_release (args_info);
308 }
309 
311 char *
312 gengetopt_strdup (const char *s)
313 {
314  char *result = 0;
315  if (!s)
316  return result;
317 
318  result = (char*)malloc(strlen(s) + 1);
319  if (result == (char*)0)
320  return (char*)0;
321  strcpy(result, s);
322  return result;
323 }
324 
325 int
326 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
327 {
328  return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
329 }
330 
331 int
332 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
333  struct cmdline_parser_params *params)
334 {
335  int result;
336  result = cmdline_parser_internal (argc, argv, args_info, params, 0);
337 
338  if (result == EXIT_FAILURE)
339  {
340  cmdline_parser_free (args_info);
341  exit (EXIT_FAILURE);
342  }
343 
344  return result;
345 }
346 
347 int
348 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
349 {
350  int result;
351  struct cmdline_parser_params params;
352 
353  params.override = override;
354  params.initialize = initialize;
356  params.check_ambiguity = 0;
357  params.print_errors = 1;
358 
359  result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
360 
361  if (result == EXIT_FAILURE)
362  {
363  cmdline_parser_free (args_info);
364  exit (EXIT_FAILURE);
365  }
366 
367  return result;
368 }
369 
370 int
371 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
372 {
373  FIX_UNUSED (args_info);
374  FIX_UNUSED (prog_name);
375  return EXIT_SUCCESS;
376 }
377 
378 
379 static char *package_name = 0;
380 
399 static
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,
405  int check_ambiguity, int override,
406  int no_free, int multiple_option,
407  const char *long_opt, char short_opt,
408  const char *additional_error)
409 {
410  char *stop_char = 0;
411  const char *val = value;
412  int found;
413  FIX_UNUSED (field);
414 
415  stop_char = 0;
416  found = 0;
417 
418  if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
419  {
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 : ""));
424  else
425  fprintf (stderr, "%s: `--%s' option given more than once%s\n",
426  package_name, long_opt,
427  (additional_error ? additional_error : ""));
428  return 1; /* failure */
429  }
430 
431  if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0)
432  {
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 : ""));
437  else
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 : ""));
441  return 1; /* failure */
442  }
443 
444  if (field_given && *field_given && ! override)
445  return 0;
446  if (prev_given)
447  (*prev_given)++;
448  if (field_given)
449  (*field_given)++;
450  if (possible_values)
451  val = possible_values[found];
452 
453  switch(arg_type) {
454  case ARG_FLAG:
455  *((int *)field) = !*((int *)field);
456  break;
457  case ARG_INT:
458  if (val) *((int *)field) = strtol (val, &stop_char, 0);
459  break;
460  case ARG_ENUM:
461  if (val) *((int *)field) = found;
462  break;
463  default:
464  break;
465  };
466 
467  /* check numeric conversion */
468  switch(arg_type) {
469  case ARG_INT:
470  if (val && !(stop_char && *stop_char == '\0')) {
471  fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
472  return 1; /* failure */
473  }
474  break;
475  default:
476  ;
477  };
478 
479  /* store the original value */
480  switch(arg_type) {
481  case ARG_NO:
482  case ARG_FLAG:
483  break;
484  default:
485  if (value && orig_field) {
486  if (no_free) {
487  *orig_field = value;
488  } else {
489  if (*orig_field)
490  free (*orig_field); /* free previous string */
491  *orig_field = gengetopt_strdup (value);
492  }
493  }
494  };
495 
496  return 0; /* OK */
497 }
498 
499 
500 int
501 cmdline_parser_internal (
502  int argc, char **argv, struct gengetopt_args_info *args_info,
503  struct cmdline_parser_params *params, const char *additional_error)
504 {
505  int c; /* Character of the parsed option. */
506 
507  int error_occurred = 0;
508  struct gengetopt_args_info local_args_info;
509 
510  int override;
511  int initialize;
512  int check_required;
513  int check_ambiguity;
514 
515  package_name = argv[0];
516 
517  /* TODO: Why is this here? It is not used anywhere. */
518  override = params->override;
519  FIX_UNUSED(override);
520 
521  initialize = params->initialize;
522  check_required = params->check_required;
523 
524  /* TODO: Why is this here? It is not used anywhere. */
525  check_ambiguity = params->check_ambiguity;
526  FIX_UNUSED(check_ambiguity);
527 
528  if (initialize)
529  cmdline_parser_init (args_info);
530 
531  cmdline_parser_init (&local_args_info);
532 
533  optarg = 0;
534  optind = 0;
535  opterr = params->print_errors;
536  optopt = '?';
537 
538  while (1)
539  {
540  int option_index = 0;
541 
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' },
548  { 0, 0, 0, 0 }
549  };
550 
551  c = getopt_long (argc, argv, "hVra:j:", long_options, &option_index);
552 
553  if (c == -1) break; /* Exit from `while (1)' loop. */
554 
555  switch (c)
556  {
557  case 'h': /* Print help and exit. */
559  cmdline_parser_free (&local_args_info);
560  exit (EXIT_SUCCESS);
561 
562  case 'V': /* Print version and exit. */
564  cmdline_parser_free (&local_args_info);
565  exit (EXIT_SUCCESS);
566 
567  case 'r': /* output raw crunched data without header. */
568 
569 
570  if (update_arg((void *)&(args_info->raw_flag), 0, &(args_info->raw_given),
571  &(local_args_info.raw_given), optarg, 0, 0, ARG_FLAG,
572  check_ambiguity, override, 1, 0, "raw", 'r',
573  additional_error))
574  goto failure;
575 
576  break;
577  case 'a': /* crunching algorithm to use. */
578 
579 
580  if (update_arg( (void *)&(args_info->algorithm_arg),
581  &(args_info->algorithm_orig), &(args_info->algorithm_given),
582  &(local_args_info.algorithm_given), optarg, cmdline_parser_algorithm_values, "xipz", ARG_ENUM,
583  check_ambiguity, override, 0, 0,
584  "algorithm", 'a',
585  additional_error))
586  goto failure;
587 
588  break;
589  case 'j': /* address to jump to (-1 = load address). */
590 
591 
592  if (update_arg( (void *)&(args_info->jump_arg),
593  &(args_info->jump_orig), &(args_info->jump_given),
594  &(local_args_info.jump_given), optarg, 0, "-1", ARG_INT,
595  check_ambiguity, override, 0, 0,
596  "jump", 'j',
597  additional_error))
598  goto failure;
599 
600  break;
601 
602  case 0: /* Long option with no short option */
603  case '?': /* Invalid option. */
604  /* `getopt_long' already printed an error message. */
605  goto failure;
606 
607  default: /* bug: option not considered. */
608  fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
609  abort ();
610  } /* switch */
611  } /* while */
612 
613 
614 
615  FIX_UNUSED(check_required);
616 
617  cmdline_parser_release (&local_args_info);
618 
619  if ( error_occurred )
620  return (EXIT_FAILURE);
621 
622  if (optind < argc)
623  {
624  int i = 0 ;
625  int found_prog_name = 0;
626  /* whether program name, i.e., argv[0], is in the remaining args
627  (this may happen with some implementations of getopt,
628  but surely not with the one included by gengetopt) */
629 
630  i = optind;
631  while (i < argc)
632  if (argv[i++] == argv[0]) {
633  found_prog_name = 1;
634  break;
635  }
636  i = 0;
637 
638  args_info->inputs_num = argc - optind - found_prog_name;
639  args_info->inputs =
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]) ;
644  }
645 
646  return 0;
647 
648 failure:
649 
650  cmdline_parser_release (&local_args_info);
651  return (EXIT_FAILURE);
652 }
653 /* vim: set ft=c noet ts=8 sts=8 sw=8 tw=80 nojs spell : */
gengetopt_args_info::help_given
unsigned int help_given
Whether help was given.
Definition: cmdline.h:53
CMDLINE_PARSER_PACKAGE_NAME
#define CMDLINE_PARSER_PACKAGE_NAME
the complete program name (used for help and version)
Definition: cmdline.h:29
gengetopt_args_info::jump_given
unsigned int jump_given
Whether jump was given.
Definition: cmdline.h:57
gengetopt_args_info_purpose
const char * gengetopt_args_info_purpose
the purpose string of the program
Definition: cmdline.c:28
cmdline_parser_params_create
struct cmdline_parser_params * cmdline_parser_params_create(void)
Definition: cmdline.c:167
cmdline_parser_file_save
int cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
Definition: cmdline.c:285
cmdline_parser_init
void cmdline_parser_init(struct gengetopt_args_info *args_info)
Definition: cmdline.c:143
gengetopt_args_info::algorithm_given
unsigned int algorithm_given
Whether algorithm was given.
Definition: cmdline.h:56
cmdline_parser_dump
int cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
Definition: cmdline.c:258
cmdline_parser_free
void cmdline_parser_free(struct gengetopt_args_info *args_info)
Definition: cmdline.c:305
cmdline_parser_print_help
void cmdline_parser_print_help(void)
Definition: cmdline.c:134
cmdline_parser
int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
Definition: cmdline.c:326
cmdline_parser_print_version
void cmdline_parser_print_version(void)
Definition: cmdline.c:102
gengetopt_args_info::version_given
unsigned int version_given
Whether version was given.
Definition: cmdline.h:54
gengetopt_args_info::algorithm_arg
enum enum_algorithm algorithm_arg
crunching algorithm to use (default='xipz').
Definition: cmdline.h:46
gengetopt_args_info::jump_orig
char * jump_orig
address to jump to (-1 = load address) original value given at command line.
Definition: cmdline.h:50
gengetopt_args_info::jump_help
const char * jump_help
address to jump to (-1 = load address) help description.
Definition: cmdline.h:51
gengetopt_args_info_help
const char * gengetopt_args_info_help[]
all the lines making the help output
Definition: cmdline.c:36
cmdline_parser_ext
int cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info, struct cmdline_parser_params *params)
Definition: cmdline.c:332
gengetopt_args_info::algorithm_help
const char * algorithm_help
crunching algorithm to use help description.
Definition: cmdline.h:48
gengetopt_args_info::raw_help
const char * raw_help
output raw crunched data without header help description.
Definition: cmdline.h:45
cmdline_parser_params_init
void cmdline_parser_params_init(struct cmdline_parser_params *params)
Definition: cmdline.c:154
cmdline_parser_params::override
int override
whether to override possibly already present options (default 0)
Definition: cmdline.h:66
cmdline_parser_params::check_ambiguity
int check_ambiguity
whether to check for options already specified in the option structure gengetopt_args_info (default 0...
Definition: cmdline.h:69
gengetopt_args_info::inputs
char ** inputs
unnamed options (options without names)
Definition: cmdline.h:59
gengetopt_args_info::raw_given
unsigned int raw_given
Whether raw was given.
Definition: cmdline.h:55
gengetopt_args_info::version_help
const char * version_help
Print version and exit help description.
Definition: cmdline.h:43
cmdline_parser_params::print_errors
int print_errors
whether getopt_long should print an error message for a bad option (default 1)
Definition: cmdline.h:70
gengetopt_args_info::inputs_num
unsigned inputs_num
unnamed options number
Definition: cmdline.h:60
cmdline.h
The header file for the command line option parser generated by GNU Gengetopt version 2....
cmdline_parser2
int cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
Definition: cmdline.c:348
gengetopt_args_info::jump_arg
int jump_arg
address to jump to (-1 = load address) (default='-1').
Definition: cmdline.h:49
cmdline_parser_required
int cmdline_parser_required(struct gengetopt_args_info *args_info, const char *prog_name)
Definition: cmdline.c:371
gengetopt_args_info::algorithm_orig
char * algorithm_orig
crunching algorithm to use original value given at command line.
Definition: cmdline.h:47
gengetopt_args_info
Where the command line options are stored.
Definition: cmdline.h:40
cmdline_parser_params
The additional parameters to pass to parser functions.
Definition: cmdline.h:64
gengetopt_args_info::help_help
const char * help_help
Print help and exit help description.
Definition: cmdline.h:42
cmdline_parser_params::initialize
int initialize
whether to initialize the option structure gengetopt_args_info (default 1)
Definition: cmdline.h:67
CMDLINE_PARSER_PACKAGE
#define CMDLINE_PARSER_PACKAGE
the program name (used for printing errors)
Definition: cmdline.h:24
cmdline_parser_params::check_required
int check_required
whether to check that all required options were provided (default 1)
Definition: cmdline.h:68
gengetopt_args_info_usage
const char * gengetopt_args_info_usage
the usage string of the program
Definition: cmdline.c:30
gengetopt_args_info::raw_flag
int raw_flag
output raw crunched data without header (default=off).
Definition: cmdline.h:44
gengetopt_args_info_description
const char * gengetopt_args_info_description
the description string of the program
Definition: cmdline.c:34
CMDLINE_PARSER_VERSION
#define CMDLINE_PARSER_VERSION
the program version
Definition: cmdline.h:34
cmdline_parser_algorithm_values
const char * cmdline_parser_algorithm_values[]
Possible values for algorithm.
Definition: cmdline.c:61