Debian Codegraph @

examples/ConvertExample/cmdline.c

raw
1 /*
2 File autogenerated by gengetopt version 2.22.6
3 generated with the following command:
4 gengetopt -N --no-version --input=hepmcconvert.ggo --include-getopt --show-required -uinputfile outputfile
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
25 #include "cmdline.h"
26
27 const char *gengetopt_args_info_purpose = "Convert between different file formats of Monte Carlo event record.\nExample:\n convert_example -i hepmc2 -o treeroot input.hepmc output.root\n";
28
29 const char *gengetopt_args_info_usage = "Usage: convert_example [OPTIONS]... [inputfile outputfile]...";
30
31 const char *gengetopt_args_info_versiontext = "";
32
33 const char *gengetopt_args_info_description = "";
34
35 const char *gengetopt_args_info_help[] = {
36 " -h, --help Print help and exit",
37 " -i, --input-format=STRING Input format (possible values=\"hepmc2\",\n \"hepmc3\", \"hpe\", \"root\", \"treeroot\",\n \"lhef\",\"gz\") (mandatory)",
38 " -o, --output-format=STRING Output format (possible values=\"hepmc2\",\n \"hepmc3\", \"hpe\", \"root\", \"treeroot\",\n \"treerootopal\", \"hpezeus\", \"dump\",\n \"dot\", \"none\") (mandatory)",
39 " -e, --extensions=STRING Extensions, in a form extension=value, could be\n passed to readers/writers",
40 " --events-limit=LONG Limit of events to read from input\n (default=`100000000')",
41 " --first-event-number=LONG Lowest allowed event number\n (default=`-100000000')",
42 " --last-event-number=LONG Highest allowed event number\n (default=`100000000')",
43 " --print-every-events-parsed=LONG\n Frequency of parsing information printouts\n (default=`100')",
44 0
45 };
46
47 typedef enum {ARG_NO
48 , ARG_STRING
49 , ARG_LONG
50 } cmdline_parser_arg_type;
51
52 static
53 void clear_given (struct gengetopt_args_info *args_info);
54 static
55 void clear_args (struct gengetopt_args_info *args_info);
56
57 static int
58 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
59 struct cmdline_parser_params *params, const char *additional_error);
60
61 static int
62 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
63
64 const char *cmdline_parser_input_format_values[] = {"hepmc2", "hepmc3", "hpe", "root", "treeroot", "lhef" , "gz", 0}; /*< Possible values for input-format. */
65 const char *cmdline_parser_output_format_values[] = {"hepmc2", "hepmc3", "hpe", "root", "treeroot", "treerootopal", "hpezeus", "dump", "dot", 0}; /*< Possible values for output-format. */
66
67 static char *
68 gengetopt_strdup (const char *s);
69
70 static
71 void clear_given (struct gengetopt_args_info *args_info)
72 {
73 args_info->help_given = 0 ;
74 args_info->input_format_given = 0 ;
75 args_info->output_format_given = 0 ;
76 args_info->extensions_given = 0 ;
77 args_info->events_limit_given = 0 ;
78 args_info->first_event_number_given = 0 ;
79 args_info->last_event_number_given = 0 ;
80 args_info->print_every_events_parsed_given = 0 ;
81 }
82
83 static
84 void clear_args (struct gengetopt_args_info *args_info)
85 {
86 FIX_UNUSED (args_info);
87 args_info->input_format_arg = NULL;
88 args_info->input_format_orig = NULL;
89 args_info->output_format_arg = NULL;
90 args_info->output_format_orig = NULL;
91 args_info->extensions_arg = NULL;
92 args_info->extensions_orig = NULL;
93 args_info->events_limit_arg = 100000000;
94 args_info->events_limit_orig = NULL;
95 args_info->first_event_number_arg = -100000000;
96 args_info->first_event_number_orig = NULL;
97 args_info->last_event_number_arg = 100000000;
98 args_info->last_event_number_orig = NULL;
99 args_info->print_every_events_parsed_arg = 100;
100 args_info->print_every_events_parsed_orig = NULL;
101
102 }
103
104 static
105 void init_args_info(struct gengetopt_args_info *args_info)
106 {
107
108
109 args_info->help_help = gengetopt_args_info_help[0] ;
110 args_info->input_format_help = gengetopt_args_info_help[1] ;
111 args_info->output_format_help = gengetopt_args_info_help[2] ;
112 args_info->extensions_help = gengetopt_args_info_help[3] ;
113 args_info->extensions_min = 0;
114 args_info->extensions_max = 0;
115 args_info->events_limit_help = gengetopt_args_info_help[4] ;
116 args_info->first_event_number_help = gengetopt_args_info_help[5] ;
117 args_info->last_event_number_help = gengetopt_args_info_help[6] ;
118 args_info->print_every_events_parsed_help = gengetopt_args_info_help[7] ;
119
120 }
121
122 void
123 cmdline_parser_print_version (void)
124 {
125 printf ("%s %s\n",
126 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
127 CMDLINE_PARSER_VERSION);
128
129 if (strlen(gengetopt_args_info_versiontext) > 0)
130 printf("\n%s\n", gengetopt_args_info_versiontext);
131 }
132
133 static void print_help_common(void) {
134 cmdline_parser_print_version ();
135
136 if (strlen(gengetopt_args_info_purpose) > 0)
137 printf("\n%s\n", gengetopt_args_info_purpose);
138
139 if (strlen(gengetopt_args_info_usage) > 0)
140 printf("\n%s\n", gengetopt_args_info_usage);
141
142 printf("\n");
143
144 if (strlen(gengetopt_args_info_description) > 0)
145 printf("%s\n\n", gengetopt_args_info_description);
146 }
147
148 void
149 cmdline_parser_print_help (void)
150 {
151 int i = 0;
152 print_help_common();
153 while (gengetopt_args_info_help[i])
154 printf("%s\n", gengetopt_args_info_help[i++]);
155 }
156
157 void
158 cmdline_parser_init (struct gengetopt_args_info *args_info)
159 {
160 clear_given (args_info);
161 clear_args (args_info);
162 init_args_info (args_info);
163
164 args_info->inputs = 0;
165 args_info->inputs_num = 0;
166 }
167
168 void
169 cmdline_parser_params_init(struct cmdline_parser_params *params)
170 {
171 if (params)
172 {
173 params->override = 0;
174 params->initialize = 1;
175 params->check_required = 1;
176 params->check_ambiguity = 0;
177 params->print_errors = 1;
178 }
179 }
180
181 struct cmdline_parser_params *
182 cmdline_parser_params_create(void)
183 {
184 struct cmdline_parser_params *params =
185 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
186 cmdline_parser_params_init(params);
187 return params;
188 }
189
190 static void
191 free_string_field (char **s)
192 {
193 if (*s)
194 {
195 free (*s);
196 *s = 0;
197 }
198 }
199
200 /** @brief generic value variable */
201 union generic_value {
202 long long_arg;
203 char *string_arg;
204 const char *default_string_arg;
205 };
206
207 /** @brief holds temporary values for multiple options */
208 struct generic_list
209 {
210 union generic_value arg;
211 char *orig;
212 struct generic_list *next;
213 };
214
215 /**
216 * @brief add a node at the head of the list
217 */
218 static void add_node(struct generic_list **list) {
219 struct generic_list *new_node = (struct generic_list *) malloc (sizeof (struct generic_list));
220 new_node->next = *list;
221 *list = new_node;
222 new_node->arg.string_arg = 0;
223 new_node->orig = 0;
224 }
225
226
227 static void
228 free_multiple_string_field(unsigned int len, char ***arg, char ***orig)
229 {
230 unsigned int i;
231 if (*arg) {
232 for (i = 0; i < len; ++i)
233 {
234 free_string_field(&((*arg)[i]));
235 free_string_field(&((*orig)[i]));
236 }
237 free_string_field(&((*arg)[0])); /* free default string */
238
239 free (*arg);
240 *arg = 0;
241 free (*orig);
242 *orig = 0;
243 }
244 }
245
246 static void
247 cmdline_parser_release (struct gengetopt_args_info *args_info)
248 {
249 unsigned int i;
250 free_string_field (&(args_info->input_format_arg));
251 free_string_field (&(args_info->input_format_orig));
252 free_string_field (&(args_info->output_format_arg));
253 free_string_field (&(args_info->output_format_orig));
254 free_multiple_string_field (args_info->extensions_given, &(args_info->extensions_arg), &(args_info->extensions_orig));
255 free_string_field (&(args_info->events_limit_orig));
256 free_string_field (&(args_info->first_event_number_orig));
257 free_string_field (&(args_info->last_event_number_orig));
258 free_string_field (&(args_info->print_every_events_parsed_orig));
259
260
261 for (i = 0; i < args_info->inputs_num; ++i)
262 free (args_info->inputs [i]);
263
264 if (args_info->inputs_num)
265 free (args_info->inputs);
266
267 clear_given (args_info);
268 }
269
270 /**
271 * @param val the value to check
272 * @param values the possible values
273 * @return the index of the matched value:
274 * -1 if no value matched,
275 * -2 if more than one value has matched
276 */
277 static int
278 check_possible_values(const char *val, const char *values[])
279 {
280 int i, found, last;
281 size_t len;
282
283 if (!val) /* otherwise strlen() crashes below */
284 return -1; /* -1 means no argument for the option */
285
286 found = last = 0;
287
288 for (i = 0, len = strlen(val); values[i]; ++i)
289 {
290 if (strncmp(val, values[i], len) == 0)
291 {
292 ++found;
293 last = i;
294 if (strlen(values[i]) == len)
295 return i; /* exact macth no need to check more */
296 }
297 }
298
299 if (found == 1) /* one match: OK */
300 return last;
301
302 return (found ? -2 : -1); /* return many values or none matched */
303 }
304
305
306 static void
307 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
308 {
309 int found = -1;
310 if (arg) {
311 if (values) {
312 found = check_possible_values(arg, values);
313 }
314 if (found >= 0)
315 fprintf(outfile, "%s=\"%s\" # %s\n", opt, arg, values[found]);
316 else
317 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
318 } else {
319 fprintf(outfile, "%s\n", opt);
320 }
321 }
322
323 static void
324 write_multiple_into_file(FILE *outfile, int len, const char *opt, char **arg, const char *values[])
325 {
326 int i;
327
328 for (i = 0; i < len; ++i)
329 write_into_file(outfile, opt, (arg ? arg[i] : 0), values);
330 }
331
332 int
333 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
334 {
335 int i = 0;
336
337 if (!outfile)
338 {
339 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
340 return EXIT_FAILURE;
341 }
342
343 if (args_info->help_given)
344 write_into_file(outfile, "help", 0, 0 );
345 if (args_info->input_format_given)
346 write_into_file(outfile, "input-format", args_info->input_format_orig, cmdline_parser_input_format_values);
347 if (args_info->output_format_given)
348 write_into_file(outfile, "output-format", args_info->output_format_orig, cmdline_parser_output_format_values);
349 write_multiple_into_file(outfile, args_info->extensions_given, "extensions", args_info->extensions_orig, 0);
350 if (args_info->events_limit_given)
351 write_into_file(outfile, "events-limit", args_info->events_limit_orig, 0);
352 if (args_info->first_event_number_given)
353 write_into_file(outfile, "first-event-number", args_info->first_event_number_orig, 0);
354 if (args_info->last_event_number_given)
355 write_into_file(outfile, "last-event-number", args_info->last_event_number_orig, 0);
356 if (args_info->print_every_events_parsed_given)
357 write_into_file(outfile, "print-every-events-parsed", args_info->print_every_events_parsed_orig, 0);
358
359
360 i = EXIT_SUCCESS;
361 return i;
362 }
363
364 int
365 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
366 {
367 FILE *outfile;
368 int i = 0;
369
370 outfile = fopen(filename, "w");
371
372 if (!outfile)
373 {
374 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
375 return EXIT_FAILURE;
376 }
377
378 i = cmdline_parser_dump(outfile, args_info);
379 fclose (outfile);
380
381 return i;
382 }
383
384 void
385 cmdline_parser_free (struct gengetopt_args_info *args_info)
386 {
387 cmdline_parser_release (args_info);
388 }
389
390 /** @brief replacement of strdup, which is not standard */
391 char *
392 gengetopt_strdup (const char *s)
393 {
394 char *result = 0;
395 if (!s)
396 return result;
397
398 result = (char*)malloc(strlen(s) + 1);
399 if (result == (char*)0)
400 return (char*)0;
401 strcpy(result, s);
402 return result;
403 }
404
405 static char *
406 get_multiple_arg_token(const char *arg)
407 {
408 const char *tok;
409 char *ret;
410 size_t len, num_of_escape, i, j;
411
412 if (!arg)
413 return 0;
414
415 tok = strchr (arg, ',');
416 num_of_escape = 0;
417
418 /* make sure it is not escaped */
419 while (tok)
420 {
421 if (*(tok-1) == '\\')
422 {
423 /* find the next one */
424 tok = strchr (tok+1, ',');
425 ++num_of_escape;
426 }
427 else
428 break;
429 }
430
431 if (tok)
432 len = (size_t)(tok - arg + 1);
433 else
434 len = strlen (arg) + 1;
435
436 len -= num_of_escape;
437
438 ret = (char *) malloc (len);
439
440 i = 0;
441 j = 0;
442 while (arg[i] && (j < len-1))
443 {
444 if (arg[i] == '\\' &&
445 arg[ i + 1 ] &&
446 arg[ i + 1 ] == ',')
447 ++i;
448
449 ret[j++] = arg[i++];
450 }
451
452 ret[len-1] = '\0';
453
454 return ret;
455 }
456
457 static const char *
458 get_multiple_arg_token_next(const char *arg)
459 {
460 const char *tok;
461
462 if (!arg)
463 return 0;
464
465 tok = strchr (arg, ',');
466
467 /* make sure it is not escaped */
468 while (tok)
469 {
470 if (*(tok-1) == '\\')
471 {
472 /* find the next one */
473 tok = strchr (tok+1, ',');
474 }
475 else
476 break;
477 }
478
479 if (! tok || strlen(tok) == 1)
480 return 0;
481
482 return tok+1;
483 }
484
485 static int
486 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc);
487
488 int
489 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc)
490 {
491 int error_occurred = 0;
492
493 if (option_given && (min > 0 || max > 0))
494 {
495 if (min > 0 && max > 0)
496 {
497 if (min == max)
498 {
499 /* specific occurrences */
500 if (option_given != (unsigned int) min)
501 {
502 fprintf (stderr, "%s: %s option occurrences must be %d\n",
503 prog_name, option_desc, min);
504 error_occurred = 1;
505 }
506 }
507 else if (option_given < (unsigned int) min
508 || option_given > (unsigned int) max)
509 {
510 /* range occurrences */
511 fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n",
512 prog_name, option_desc, min, max);
513 error_occurred = 1;
514 }
515 }
516 else if (min > 0)
517 {
518 /* at least check */
519 if (option_given < min)
520 {
521 fprintf (stderr, "%s: %s option occurrences must be at least %d\n",
522 prog_name, option_desc, min);
523 error_occurred = 1;
524 }
525 }
526 else if (max > 0)
527 {
528 /* at most check */
529 if (option_given > max)
530 {
531 fprintf (stderr, "%s: %s option occurrences must be at most %d\n",
532 prog_name, option_desc, max);
533 error_occurred = 1;
534 }
535 }
536 }
537
538 return error_occurred;
539 }
540 int
541 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
542 {
543 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
544 }
545
546 int
547 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
548 struct cmdline_parser_params *params)
549 {
550 int result;
551 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
552
553 if (result == EXIT_FAILURE)
554 {
555 cmdline_parser_free (args_info);
556 exit (EXIT_FAILURE);
557 }
558
559 return result;
560 }
561
562 int
563 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
564 {
565 int result;
566 struct cmdline_parser_params params;
567
568 params.override = override;
569 params.initialize = initialize;
570 params.check_required = check_required;
571 params.check_ambiguity = 0;
572 params.print_errors = 1;
573
574 result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
575
576 if (result == EXIT_FAILURE)
577 {
578 cmdline_parser_free (args_info);
579 exit (EXIT_FAILURE);
580 }
581
582 return result;
583 }
584
585 int
586 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
587 {
588 int result = EXIT_SUCCESS;
589
590 if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
591 result = EXIT_FAILURE;
592
593 if (result == EXIT_FAILURE)
594 {
595 cmdline_parser_free (args_info);
596 exit (EXIT_FAILURE);
597 }
598
599 return result;
600 }
601
602 int
603 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
604 {
605 int error_occurred = 0;
606 FIX_UNUSED (additional_error);
607
608 /* checks for required options */
609 if (! args_info->input_format_given)
610 {
611 fprintf (stderr, "%s: '--input-format' ('-i') option required%s\n", prog_name, (additional_error ? additional_error : ""));
612 error_occurred = 1;
613 }
614
615 if (! args_info->output_format_given)
616 {
617 fprintf (stderr, "%s: '--output-format' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
618 error_occurred = 1;
619 }
620
621 if (check_multiple_option_occurrences(prog_name, args_info->extensions_given, args_info->extensions_min, args_info->extensions_max, "'--extensions' ('-e')"))
622 error_occurred = 1;
623
624
625 /* checks for dependences among options */
626
627 return error_occurred;
628 }
629
630 /*
631 * Extracted from the glibc source tree, version 2.3.6
632 *
633 * Licensed under the GPL as per the whole glibc source tree.
634 *
635 * This file was modified so that getopt_long can be called
636 * many times without risking previous memory to be spoiled.
637 *
638 * Modified by Andre Noll and Lorenzo Bettini for use in
639 * GNU gengetopt generated files.
640 *
641 */
642
643 /*
644 * we must include anything we need since this file is not thought to be
645 * inserted in a file already using getopt.h
646 *
647 * Lorenzo
648 */
649
650 struct option
651 {
652 const char *name;
653 /* has_arg can't be an enum because some compilers complain about
654 type mismatches in all the code that assumes it is an int. */
655 int has_arg;
656 int *flag;
657 int val;
658 };
659
660 /* This version of `getopt' appears to the caller like standard Unix `getopt'
661 but it behaves differently for the user, since it allows the user
662 to intersperse the options with the other arguments.
663
664 As `getopt' works, it permutes the elements of ARGV so that,
665 when it is done, all the options precede everything else. Thus
666 all application programs are extended to handle flexible argument order.
667 */
668 /*
669 If the field `flag' is not NULL, it points to a variable that is set
670 to the value given in the field `val' when the option is found, but
671 left unchanged if the option is not found.
672
673 To have a long-named option do something other than set an `int' to
674 a compiled-in constant, such as set a value from `custom_optarg', set the
675 option's `flag' field to zero and its `val' field to a nonzero
676 value (the equivalent single-letter option character, if there is
677 one). For long options that have a zero `flag' field, `getopt'
678 returns the contents of the `val' field. */
679
680 /* Names for the values of the `has_arg' field of `struct option'. */
681 #ifndef no_argument
682 #define no_argument 0
683 #endif
684
685 #ifndef required_argument
686 #define required_argument 1
687 #endif
688
689 #ifndef optional_argument
690 #define optional_argument 2
691 #endif
692
693 struct custom_getopt_data {
694 /*
695 * These have exactly the same meaning as the corresponding global variables,
696 * except that they are used for the reentrant versions of getopt.
697 */
698 int custom_optind;
699 int custom_opterr;
700 int custom_optopt;
701 char *custom_optarg;
702
703 /* True if the internal members have been initialized. */
704 int initialized;
705
706 /*
707 * The next char to be scanned in the option-element in which the last option
708 * character we returned was found. This allows us to pick up the scan where
709 * we left off. If this is zero, or a null string, it means resume the scan by
710 * advancing to the next ARGV-element.
711 */
712 char *nextchar;
713
714 /*
715 * Describe the part of ARGV that contains non-options that have been skipped.
716 * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
717 * the index after the last of them.
718 */
719 int first_nonopt;
720 int last_nonopt;
721 };
722
723 /*
724 * the variables optarg, optind, opterr and optopt are renamed with
725 * the custom_ prefix so that they don't interfere with getopt ones.
726 *
727 * Moreover they're static so they are visible only from within the
728 * file where this very file will be included.
729 */
730
731 /*
732 * For communication from `custom_getopt' to the caller. When `custom_getopt' finds an
733 * option that takes an argument, the argument value is returned here.
734 */
735 static char *custom_optarg;
736
737 /*
738 * Index in ARGV of the next element to be scanned. This is used for
739 * communication to and from the caller and for communication between
740 * successive calls to `custom_getopt'.
741 *
742 * On entry to `custom_getopt', 1 means this is the first call; initialize.
743 *
744 * When `custom_getopt' returns -1, this is the index of the first of the non-option
745 * elements that the caller should itself scan.
746 *
747 * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
748 * has been scanned so far.
749 *
750 * 1003.2 says this must be 1 before any call.
751 */
752 static int custom_optind = 1;
753
754 /*
755 * Callers store zero here to inhibit the error message for unrecognized
756 * options.
757 */
758 static int custom_opterr = 1;
759
760 /*
761 * Set to an option character which was unrecognized. This must be initialized
762 * on some systems to avoid linking in the system's own getopt implementation.
763 */
764 static int custom_optopt = '?';
765
766 /*
767 * Exchange two adjacent subsequences of ARGV. One subsequence is elements
768 * [first_nonopt,last_nonopt) which contains all the non-options that have been
769 * skipped so far. The other is elements [last_nonopt,custom_optind), which contains
770 * all the options processed since those non-options were skipped.
771 * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
772 * indices of the non-options in ARGV after they are moved.
773 */
774 static void exchange(char **argv, struct custom_getopt_data *d)
775 {
776 int bottom = d->first_nonopt;
777 int middle = d->last_nonopt;
778 int top = d->custom_optind;
779 char *tem;
780
781 /*
782 * Exchange the shorter segment with the far end of the longer segment.
783 * That puts the shorter segment into the right place. It leaves the
784 * longer segment in the right place overall, but it consists of two
785 * parts that need to be swapped next.
786 */
787 while (top > middle && middle > bottom) {
788 if (top - middle > middle - bottom) {
789 /* Bottom segment is the short one. */
790 int len = middle - bottom;
791 int i;
792
793 /* Swap it with the top part of the top segment. */
794 for (i = 0; i < len; i++) {
795 tem = argv[bottom + i];
796 argv[bottom + i] =
797 argv[top - (middle - bottom) + i];
798 argv[top - (middle - bottom) + i] = tem;
799 }
800 /* Exclude the moved bottom segment from further swapping. */
801 top -= len;
802 } else {
803 /* Top segment is the short one. */
804 int len = top - middle;
805 int i;
806
807 /* Swap it with the bottom part of the bottom segment. */
808 for (i = 0; i < len; i++) {
809 tem = argv[bottom + i];
810 argv[bottom + i] = argv[middle + i];
811 argv[middle + i] = tem;
812 }
813 /* Exclude the moved top segment from further swapping. */
814 bottom += len;
815 }
816 }
817 /* Update records for the slots the non-options now occupy. */
818 d->first_nonopt += (d->custom_optind - d->last_nonopt);
819 d->last_nonopt = d->custom_optind;
820 }
821
822 /* Initialize the internal data when the first call is made. */
823 static void custom_getopt_initialize(struct custom_getopt_data *d)
824 {
825 /*
826 * Start processing options with ARGV-element 1 (since ARGV-element 0
827 * is the program name); the sequence of previously skipped non-option
828 * ARGV-elements is empty.
829 */
830 d->first_nonopt = d->last_nonopt = d->custom_optind;
831 d->nextchar = NULL;
832 d->initialized = 1;
833 }
834
835 #define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
836
837 /* return: zero: continue, nonzero: return given value to user */
838 static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
839 struct custom_getopt_data *d)
840 {
841 /*
842 * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
843 * moved back by the user (who may also have changed the arguments).
844 */
845 if (d->last_nonopt > d->custom_optind)
846 d->last_nonopt = d->custom_optind;
847 if (d->first_nonopt > d->custom_optind)
848 d->first_nonopt = d->custom_optind;
849 /*
850 * If we have just processed some options following some
851 * non-options, exchange them so that the options come first.
852 */
853 if (d->first_nonopt != d->last_nonopt &&
854 d->last_nonopt != d->custom_optind)
855 exchange((char **) argv, d);
856 else if (d->last_nonopt != d->custom_optind)
857 d->first_nonopt = d->custom_optind;
858 /*
859 * Skip any additional non-options and extend the range of
860 * non-options previously skipped.
861 */
862 while (d->custom_optind < argc && NONOPTION_P)
863 d->custom_optind++;
864 d->last_nonopt = d->custom_optind;
865 /*
866 * The special ARGV-element `--' means premature end of options. Skip
867 * it like a null option, then exchange with previous non-options as if
868 * it were an option, then skip everything else like a non-option.
869 */
870 if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
871 d->custom_optind++;
872 if (d->first_nonopt != d->last_nonopt
873 && d->last_nonopt != d->custom_optind)
874 exchange((char **) argv, d);
875 else if (d->first_nonopt == d->last_nonopt)
876 d->first_nonopt = d->custom_optind;
877 d->last_nonopt = argc;
878 d->custom_optind = argc;
879 }
880 /*
881 * If we have done all the ARGV-elements, stop the scan and back over
882 * any non-options that we skipped and permuted.
883 */
884 if (d->custom_optind == argc) {
885 /*
886 * Set the next-arg-index to point at the non-options that we
887 * previously skipped, so the caller will digest them.
888 */
889 if (d->first_nonopt != d->last_nonopt)
890 d->custom_optind = d->first_nonopt;
891 return -1;
892 }
893 /*
894 * If we have come to a non-option and did not permute it, either stop
895 * the scan or describe it to the caller and pass it by.
896 */
897 if (NONOPTION_P) {
898 d->custom_optarg = argv[d->custom_optind++];
899 return 1;
900 }
901 /*
902 * We have found another option-ARGV-element. Skip the initial
903 * punctuation.
904 */
905 d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
906 return 0;
907 }
908
909 /*
910 * Check whether the ARGV-element is a long option.
911 *
912 * If there's a long option "fubar" and the ARGV-element is "-fu", consider
913 * that an abbreviation of the long option, just like "--fu", and not "-f" with
914 * arg "u".
915 *
916 * This distinction seems to be the most useful approach.
917 *
918 */
919 static int check_long_opt(int argc, char *const *argv, const char *optstring,
920 const struct option *longopts, int *longind,
921 int print_errors, struct custom_getopt_data *d)
922 {
923 char *nameend;
924 const struct option *p;
925 const struct option *pfound = NULL;
926 int exact = 0;
927 int ambig = 0;
928 int indfound = -1;
929 int option_index;
930
931 for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
932 /* Do nothing. */ ;
933
934 /* Test all long options for either exact match or abbreviated matches */
935 for (p = longopts, option_index = 0; p->name; p++, option_index++)
936 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
937 if ((unsigned int) (nameend - d->nextchar)
938 == (unsigned int) strlen(p->name)) {
939 /* Exact match found. */
940 pfound = p;
941 indfound = option_index;
942 exact = 1;
943 break;
944 } else if (pfound == NULL) {
945 /* First nonexact match found. */
946 pfound = p;
947 indfound = option_index;
948 } else if (pfound->has_arg != p->has_arg
949 || pfound->flag != p->flag
950 || pfound->val != p->val)
951 /* Second or later nonexact match found. */
952 ambig = 1;
953 }
954 if (ambig && !exact) {
955 if (print_errors) {
956 fprintf(stderr,
957 "%s: option `%s' is ambiguous\n",
958 argv[0], argv[d->custom_optind]);
959 }
960 d->nextchar += strlen(d->nextchar);
961 d->custom_optind++;
962 d->custom_optopt = 0;
963 return '?';
964 }
965 if (pfound) {
966 option_index = indfound;
967 d->custom_optind++;
968 if (*nameend) {
969 if (pfound->has_arg != no_argument)
970 d->custom_optarg = nameend + 1;
971 else {
972 if (print_errors) {
973 if (argv[d->custom_optind - 1][1] == '-') {
974 /* --option */
975 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
976 argv[0], pfound->name);
977 } else {
978 /* +option or -option */
979 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
980 argv[0], argv[d->custom_optind - 1][0], pfound->name);
981 }
982
983 }
984 d->nextchar += strlen(d->nextchar);
985 d->custom_optopt = pfound->val;
986 return '?';
987 }
988 } else if (pfound->has_arg == required_argument) {
989 if (d->custom_optind < argc)
990 d->custom_optarg = argv[d->custom_optind++];
991 else {
992 if (print_errors) {
993 fprintf(stderr,
994 "%s: option `%s' requires an argument\n",
995 argv[0],
996 argv[d->custom_optind - 1]);
997 }
998 d->nextchar += strlen(d->nextchar);
999 d->custom_optopt = pfound->val;
1000 return optstring[0] == ':' ? ':' : '?';
1001 }
1002 }
1003 d->nextchar += strlen(d->nextchar);
1004 if (longind != NULL)
1005 *longind = option_index;
1006 if (pfound->flag) {
1007 *(pfound->flag) = pfound->val;
1008 return 0;
1009 }
1010 return pfound->val;
1011 }
1012 /*
1013 * Can't find it as a long option. If this is not getopt_long_only, or
1014 * the option starts with '--' or is not a valid short option, then
1015 * it's an error. Otherwise interpret it as a short option.
1016 */
1017 if (print_errors) {
1018 if (argv[d->custom_optind][1] == '-') {
1019 /* --option */
1020 fprintf(stderr,
1021 "%s: unrecognized option `--%s'\n",
1022 argv[0], d->nextchar);
1023 } else {
1024 /* +option or -option */
1025 fprintf(stderr,
1026 "%s: unrecognized option `%c%s'\n",
1027 argv[0], argv[d->custom_optind][0],
1028 d->nextchar);
1029 }
1030 }
1031 d->nextchar = (char *) "";
1032 d->custom_optind++;
1033 d->custom_optopt = 0;
1034 return '?';
1035 }
1036
1037 static int check_short_opt(int argc, char *const *argv, const char *optstring,
1038 int print_errors, struct custom_getopt_data *d)
1039 {
1040 char c = *d->nextchar++;
1041 const char *temp = strchr(optstring, c);
1042
1043 /* Increment `custom_optind' when we start to process its last character. */
1044 if (*d->nextchar == '\0')
1045 ++d->custom_optind;
1046 if (!temp || c == ':') {
1047 if (print_errors)
1048 fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
1049
1050 d->custom_optopt = c;
1051 return '?';
1052 }
1053 if (temp[1] == ':') {
1054 if (temp[2] == ':') {
1055 /* This is an option that accepts an argument optionally. */
1056 if (*d->nextchar != '\0') {
1057 d->custom_optarg = d->nextchar;
1058 d->custom_optind++;
1059 } else
1060 d->custom_optarg = NULL;
1061 d->nextchar = NULL;
1062 } else {
1063 /* This is an option that requires an argument. */
1064 if (*d->nextchar != '\0') {
1065 d->custom_optarg = d->nextchar;
1066 /*
1067 * If we end this ARGV-element by taking the
1068 * rest as an arg, we must advance to the next
1069 * element now.
1070 */
1071 d->custom_optind++;
1072 } else if (d->custom_optind == argc) {
1073 if (print_errors) {
1074 fprintf(stderr,
1075 "%s: option requires an argument -- %c\n",
1076 argv[0], c);
1077 }
1078 d->custom_optopt = c;
1079 if (optstring[0] == ':')
1080 c = ':';
1081 else
1082 c = '?';
1083 } else
1084 /*
1085 * We already incremented `custom_optind' once;
1086 * increment it again when taking next ARGV-elt
1087 * as argument.
1088 */
1089 d->custom_optarg = argv[d->custom_optind++];
1090 d->nextchar = NULL;
1091 }
1092 }
1093 return c;
1094 }
1095
1096 /*
1097 * Scan elements of ARGV for option characters given in OPTSTRING.
1098 *
1099 * If an element of ARGV starts with '-', and is not exactly "-" or "--",
1100 * then it is an option element. The characters of this element
1101 * (aside from the initial '-') are option characters. If `getopt'
1102 * is called repeatedly, it returns successively each of the option characters
1103 * from each of the option elements.
1104 *
1105 * If `getopt' finds another option character, it returns that character,
1106 * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
1107 * resume the scan with the following option character or ARGV-element.
1108 *
1109 * If there are no more option characters, `getopt' returns -1.
1110 * Then `custom_optind' is the index in ARGV of the first ARGV-element
1111 * that is not an option. (The ARGV-elements have been permuted
1112 * so that those that are not options now come last.)
1113 *
1114 * OPTSTRING is a string containing the legitimate option characters.
1115 * If an option character is seen that is not listed in OPTSTRING,
1116 * return '?' after printing an error message. If you set `custom_opterr' to
1117 * zero, the error message is suppressed but we still return '?'.
1118 *
1119 * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
1120 * so the following text in the same ARGV-element, or the text of the following
1121 * ARGV-element, is returned in `custom_optarg'. Two colons mean an option that
1122 * wants an optional arg; if there is text in the current ARGV-element,
1123 * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
1124 *
1125 * If OPTSTRING starts with `-' or `+', it requests different methods of
1126 * handling the non-option ARGV-elements.
1127 * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
1128 *
1129 * Long-named options begin with `--' instead of `-'.
1130 * Their names may be abbreviated as long as the abbreviation is unique
1131 * or is an exact match for some defined option. If they have an
1132 * argument, it follows the option name in the same ARGV-element, separated
1133 * from the option name by a `=', or else the in next ARGV-element.
1134 * When `getopt' finds a long-named option, it returns 0 if that option's
1135 * `flag' field is nonzero, the value of the option's `val' field
1136 * if the `flag' field is zero.
1137 *
1138 * The elements of ARGV aren't really const, because we permute them.
1139 * But we pretend they're const in the prototype to be compatible
1140 * with other systems.
1141 *
1142 * LONGOPTS is a vector of `struct option' terminated by an
1143 * element containing a name which is zero.
1144 *
1145 * LONGIND returns the index in LONGOPT of the long-named option found.
1146 * It is only valid when a long-named option has been found by the most
1147 * recent call.
1148 *
1149 * Return the option character from OPTS just read. Return -1 when there are
1150 * no more options. For unrecognized options, or options missing arguments,
1151 * `custom_optopt' is set to the option letter, and '?' is returned.
1152 *
1153 * The OPTS string is a list of characters which are recognized option letters,
1154 * optionally followed by colons, specifying that that letter takes an
1155 * argument, to be placed in `custom_optarg'.
1156 *
1157 * If a letter in OPTS is followed by two colons, its argument is optional.
1158 * This behavior is specific to the GNU `getopt'.
1159 *
1160 * The argument `--' causes premature termination of argument scanning,
1161 * explicitly telling `getopt' that there are no more options. If OPTS begins
1162 * with `--', then non-option arguments are treated as arguments to the option
1163 * '\0'. This behavior is specific to the GNU `getopt'.
1164 */
1165
1166 static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
1167 const struct option *longopts, int *longind,
1168 struct custom_getopt_data *d)
1169 {
1170 int ret, print_errors = d->custom_opterr;
1171
1172 if (optstring[0] == ':')
1173 print_errors = 0;
1174 if (argc < 1)
1175 return -1;
1176 d->custom_optarg = NULL;
1177
1178 /*
1179 * This is a big difference with GNU getopt, since optind == 0
1180 * means initialization while here 1 means first call.
1181 */
1182 if (d->custom_optind == 0 || !d->initialized) {
1183 if (d->custom_optind == 0)
1184 d->custom_optind = 1; /* Don't scan ARGV[0], the program name. */
1185 custom_getopt_initialize(d);
1186 }
1187 if (d->nextchar == NULL || *d->nextchar == '\0') {
1188 ret = shuffle_argv(argc, argv, longopts, d);
1189 if (ret)
1190 return ret;
1191 }
1192 if (longopts && (argv[d->custom_optind][1] == '-' ))
1193 return check_long_opt(argc, argv, optstring, longopts,
1194 longind, print_errors, d);
1195 return check_short_opt(argc, argv, optstring, print_errors, d);
1196 }
1197
1198 static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
1199 const struct option *longopts, int *longind)
1200 {
1201 int result;
1202 /* Keep a global copy of all internal members of d */
1203 static struct custom_getopt_data d;
1204
1205 d.custom_optind = custom_optind;
1206 d.custom_opterr = custom_opterr;
1207 result = getopt_internal_r(argc, argv, optstring, longopts,
1208 longind, &d);
1209 custom_optind = d.custom_optind;
1210 custom_optarg = d.custom_optarg;
1211 custom_optopt = d.custom_optopt;
1212 return result;
1213 }
1214
1215 static int custom_getopt_long (int argc, char *const *argv, const char *options,
1216 const struct option *long_options, int *opt_index)
1217 {
1218 return custom_getopt_internal(argc, argv, options, long_options,
1219 opt_index);
1220 }
1221
1222
1223 static char *package_name = 0;
1224
1225 /**
1226 * @brief updates an option
1227 * @param field the generic pointer to the field to update
1228 * @param orig_field the pointer to the orig field
1229 * @param field_given the pointer to the number of occurrence of this option
1230 * @param prev_given the pointer to the number of occurrence already seen
1231 * @param value the argument for this option (if null no arg was specified)
1232 * @param possible_values the possible values for this option (if specified)
1233 * @param default_value the default value (in case the option only accepts fixed values)
1234 * @param arg_type the type of this option
1235 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
1236 * @param override @see cmdline_parser_params.override
1237 * @param no_free whether to free a possible previous value
1238 * @param multiple_option whether this is a multiple option
1239 * @param long_opt the corresponding long option
1240 * @param short_opt the corresponding short option (or '-' if none)
1241 * @param additional_error possible further error specification
1242 */
1243 static
1244 int update_arg(void *field, char **orig_field,
1245 unsigned int *field_given, unsigned int *prev_given,
1246 char *value, const char *possible_values[],
1247 const char *default_value,
1248 cmdline_parser_arg_type arg_type,
1249 int check_ambiguity, int override,
1250 int no_free, int multiple_option,
1251 const char *long_opt, char short_opt,
1252 const char *additional_error)
1253 {
1254 char *stop_char = 0;
1255 const char *val = value;
1256 int found;
1257 char **string_field;
1258 FIX_UNUSED (field);
1259
1260 stop_char = 0;
1261 found = 0;
1262
1263 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
1264 {
1265 if (short_opt != '-')
1266 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
1267 package_name, long_opt, short_opt,
1268 (additional_error ? additional_error : ""));
1269 else
1270 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
1271 package_name, long_opt,
1272 (additional_error ? additional_error : ""));
1273 return 1; /* failure */
1274 }
1275
1276 if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0)
1277 {
1278 if (short_opt != '-')
1279 fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s' (`-%c')%s\n",
1280 package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt, short_opt,
1281 (additional_error ? additional_error : ""));
1282 else
1283 fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s'%s\n",
1284 package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt,
1285 (additional_error ? additional_error : ""));
1286 return 1; /* failure */
1287 }
1288
1289 if (field_given && *field_given && ! override)
1290 return 0;
1291 if (prev_given)
1292 (*prev_given)++;
1293 if (field_given)
1294 (*field_given)++;
1295 if (possible_values)
1296 val = possible_values[found];
1297
1298 switch(arg_type) {
1299 case ARG_LONG:
1300 if (val) *((long *)field) = (long)strtol (val, &stop_char, 0);
1301 break;
1302 case ARG_STRING:
1303 if (val) {
1304 string_field = (char **)field;
1305 if (!no_free && *string_field)
1306 free (*string_field); /* free previous string */
1307 *string_field = gengetopt_strdup (val);
1308 }
1309 break;
1310 default:
1311 break;
1312 };
1313
1314 /* check numeric conversion */
1315 switch(arg_type) {
1316 case ARG_LONG:
1317 if (val && !(stop_char && *stop_char == '\0')) {
1318 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
1319 return 1; /* failure */
1320 }
1321 break;
1322 default:
1323 ;
1324 };
1325
1326 /* store the original value */
1327 switch(arg_type) {
1328 case ARG_NO:
1329 break;
1330 default:
1331 if (value && orig_field) {
1332 if (no_free) {
1333 *orig_field = value;
1334 } else {
1335 if (*orig_field)
1336 free (*orig_field); /* free previous string */
1337 *orig_field = gengetopt_strdup (value);
1338 }
1339 }
1340 };
1341
1342 return 0; /* OK */
1343 }
1344
1345 /**
1346 * @brief store information about a multiple option in a temporary list
1347 * @param list where to (temporarily) store multiple options
1348 */
1349 static
1350 int update_multiple_arg_temp(struct generic_list **list,
1351 unsigned int *prev_given, const char *val,
1352 const char *possible_values[], const char *default_value,
1353 cmdline_parser_arg_type arg_type,
1354 const char *long_opt, char short_opt,
1355 const char *additional_error)
1356 {
1357 /* store single arguments */
1358 char *multi_token;
1359 const char *multi_next;
1360
1361 if (arg_type == ARG_NO) {
1362 (*prev_given)++;
1363 return 0; /* OK */
1364 }
1365
1366 multi_token = get_multiple_arg_token(val);
1367 multi_next = get_multiple_arg_token_next (val);
1368
1369 while (1)
1370 {
1371 add_node (list);
1372 if (update_arg((void *)&((*list)->arg), &((*list)->orig), 0,
1373 prev_given, multi_token, possible_values, default_value,
1374 arg_type, 0, 1, 1, 1, long_opt, short_opt, additional_error)) {
1375 if (multi_token) free(multi_token);
1376 return 1; /* failure */
1377 }
1378
1379 if (multi_next)
1380 {
1381 multi_token = get_multiple_arg_token(multi_next);
1382 multi_next = get_multiple_arg_token_next (multi_next);
1383 }
1384 else
1385 break;
1386 }
1387
1388 return 0; /* OK */
1389 }
1390
1391 /**
1392 * @brief free the passed list (including possible string argument)
1393 */
1394 static
1395 void free_list(struct generic_list *list, short string_arg)
1396 {
1397 if (list) {
1398 struct generic_list *tmp;
1399 while (list)
1400 {
1401 tmp = list;
1402 if (string_arg && list->arg.string_arg)
1403 free (list->arg.string_arg);
1404 if (list->orig)
1405 free (list->orig);
1406 list = list->next;
1407 free (tmp);
1408 }
1409 }
1410 }
1411
1412 /**
1413 * @brief updates a multiple option starting from the passed list
1414 */
1415 static
1416 void update_multiple_arg(void *field, char ***orig_field,
1417 unsigned int field_given, unsigned int prev_given, union generic_value *default_value,
1418 cmdline_parser_arg_type arg_type,
1419 struct generic_list *list)
1420 {
1421 int i;
1422 struct generic_list *tmp;
1423
1424 if (prev_given && list) {
1425 *orig_field = (char **) realloc (*orig_field, (field_given + prev_given) * sizeof (char *));
1426
1427 switch(arg_type) {
1428 case ARG_LONG:
1429 *((long **)field) = (long *)realloc (*((long **)field), (field_given + prev_given) * sizeof (long)); break;
1430 case ARG_STRING:
1431 *((char ***)field) = (char **)realloc (*((char ***)field), (field_given + prev_given) * sizeof (char *)); break;
1432 default:
1433 break;
1434 };
1435
1436 for (i = (prev_given - 1); i >= 0; --i)
1437 {
1438 tmp = list;
1439
1440 switch(arg_type) {
1441 case ARG_LONG:
1442 (*((long **)field))[i + field_given] = tmp->arg.long_arg; break;
1443 case ARG_STRING:
1444 (*((char ***)field))[i + field_given] = tmp->arg.string_arg; break;
1445 default:
1446 break;
1447 }
1448 (*orig_field) [i + field_given] = list->orig;
1449 list = list->next;
1450 free (tmp);
1451 }
1452 } else { /* set the default value */
1453 if (default_value && ! field_given) {
1454 switch(arg_type) {
1455 case ARG_LONG:
1456 if (! *((long **)field)) {
1457 *((long **)field) = (long *)malloc (sizeof (long));
1458 (*((long **)field))[0] = default_value->long_arg;
1459 }
1460 break;
1461 case ARG_STRING:
1462 if (! *((char ***)field)) {
1463 *((char ***)field) = (char **)malloc (sizeof (char *));
1464 (*((char ***)field))[0] = gengetopt_strdup(default_value->string_arg);
1465 }
1466 break;
1467 default: break;
1468 }
1469 if (!(*orig_field)) {
1470 *orig_field = (char **) malloc (sizeof (char *));
1471 (*orig_field)[0] = 0;
1472 }
1473 }
1474 }
1475 }
1476
1477 int
1478 cmdline_parser_internal (
1479 int argc, char **argv, struct gengetopt_args_info *args_info,
1480 struct cmdline_parser_params *params, const char *additional_error)
1481 {
1482 int c; /* Character of the parsed option. */
1483
1484 struct generic_list * extensions_list = NULL;
1485 int error_occurred = 0;
1486 struct gengetopt_args_info local_args_info;
1487
1488 int override;
1489 int initialize;
1490 int check_required;
1491 int check_ambiguity;
1492
1493 char *optarg;
1494 int optind;
1495 int opterr;
1496 int optopt;
1497
1498 package_name = argv[0];
1499
1500 override = params->override;
1501 initialize = params->initialize;
1502 check_required = params->check_required;
1503 check_ambiguity = params->check_ambiguity;
1504
1505 if (initialize)
1506 cmdline_parser_init (args_info);
1507
1508 cmdline_parser_init (&local_args_info);
1509
1510 optarg = 0;
1511 optind = 0;
1512 opterr = params->print_errors;
1513 optopt = '?';
1514
1515 while (1)
1516 {
1517 int option_index = 0;
1518
1519 static struct option long_options[] = {
1520 { "help", 0, NULL, 'h' },
1521 { "input-format", 1, NULL, 'i' },
1522 { "output-format", 1, NULL, 'o' },
1523 { "extensions", 1, NULL, 'e' },
1524 { "events-limit", 1, NULL, 0 },
1525 { "first-event-number", 1, NULL, 0 },
1526 { "last-event-number", 1, NULL, 0 },
1527 { "print-every-events-parsed", 1, NULL, 0 },
1528 { 0, 0, 0, 0 }
1529 };
1530
1531 custom_optarg = optarg;
1532 custom_optind = optind;
1533 custom_opterr = opterr;
1534 custom_optopt = optopt;
1535
1536 c = custom_getopt_long (argc, argv, "hi:o:e:", long_options, &option_index);
1537
1538 optarg = custom_optarg;
1539 optind = custom_optind;
1540 opterr = custom_opterr;
1541 optopt = custom_optopt;
1542
1543 if (c == -1) break; /* Exit from `while (1)' loop. */
1544
1545 switch (c)
1546 {
1547 case 'h': /* Print help and exit. */
1548 cmdline_parser_print_help ();
1549 cmdline_parser_free (&local_args_info);
1550 exit (EXIT_SUCCESS);
1551
1552 case 'i': /* Input format. */
1553
1554
1555 if (update_arg( (void *)&(args_info->input_format_arg),
1556 &(args_info->input_format_orig), &(args_info->input_format_given),
1557 &(local_args_info.input_format_given), optarg, cmdline_parser_input_format_values, 0, ARG_STRING,
1558 check_ambiguity, override, 0, 0,
1559 "input-format", 'i',
1560 additional_error))
1561 goto failure;
1562
1563 break;
1564 case 'o': /* Output format. */
1565
1566
1567 if (update_arg( (void *)&(args_info->output_format_arg),
1568 &(args_info->output_format_orig), &(args_info->output_format_given),
1569 &(local_args_info.output_format_given), optarg, cmdline_parser_output_format_values, 0, ARG_STRING,
1570 check_ambiguity, override, 0, 0,
1571 "output-format", 'o',
1572 additional_error))
1573 goto failure;
1574
1575 break;
1576 case 'e': /* Extensions, in a form extension=value, could be passed to readers/writers. */
1577
1578 if (update_multiple_arg_temp(&extensions_list,
1579 &(local_args_info.extensions_given), optarg, 0, 0, ARG_STRING,
1580 "extensions", 'e',
1581 additional_error))
1582 goto failure;
1583
1584 break;
1585
1586 case 0: /* Long option with no short option */
1587 /* Limit of events to read from input. */
1588 if (strcmp (long_options[option_index].name, "events-limit") == 0)
1589 {
1590
1591
1592 if (update_arg( (void *)&(args_info->events_limit_arg),
1593 &(args_info->events_limit_orig), &(args_info->events_limit_given),
1594 &(local_args_info.events_limit_given), optarg, 0, "100000000", ARG_LONG,
1595 check_ambiguity, override, 0, 0,
1596 "events-limit", '-',
1597 additional_error))
1598 goto failure;
1599
1600 }
1601 /* Lowest allowed event number. */
1602 else if (strcmp (long_options[option_index].name, "first-event-number") == 0)
1603 {
1604
1605
1606 if (update_arg( (void *)&(args_info->first_event_number_arg),
1607 &(args_info->first_event_number_orig), &(args_info->first_event_number_given),
1608 &(local_args_info.first_event_number_given), optarg, 0, "-100000000", ARG_LONG,
1609 check_ambiguity, override, 0, 0,
1610 "first-event-number", '-',
1611 additional_error))
1612 goto failure;
1613
1614 }
1615 /* Highest allowed event number. */
1616 else if (strcmp (long_options[option_index].name, "last-event-number") == 0)
1617 {
1618
1619
1620 if (update_arg( (void *)&(args_info->last_event_number_arg),
1621 &(args_info->last_event_number_orig), &(args_info->last_event_number_given),
1622 &(local_args_info.last_event_number_given), optarg, 0, "100000000", ARG_LONG,
1623 check_ambiguity, override, 0, 0,
1624 "last-event-number", '-',
1625 additional_error))
1626 goto failure;
1627
1628 }
1629 /* Frequency of parsing information printouts. */
1630 else if (strcmp (long_options[option_index].name, "print-every-events-parsed") == 0)
1631 {
1632
1633
1634 if (update_arg( (void *)&(args_info->print_every_events_parsed_arg),
1635 &(args_info->print_every_events_parsed_orig), &(args_info->print_every_events_parsed_given),
1636 &(local_args_info.print_every_events_parsed_given), optarg, 0, "100", ARG_LONG,
1637 check_ambiguity, override, 0, 0,
1638 "print-every-events-parsed", '-',
1639 additional_error))
1640 goto failure;
1641
1642 }
1643
1644 break;
1645 case '?': /* Invalid option. */
1646 /* `getopt_long' already printed an error message. */
1647 goto failure;
1648
1649 default: /* bug: option not considered. */
1650 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1651 abort ();
1652 } /* switch */
1653 } /* while */
1654
1655
1656 update_multiple_arg((void *)&(args_info->extensions_arg),
1657 &(args_info->extensions_orig), args_info->extensions_given,
1658 local_args_info.extensions_given, 0,
1659 ARG_STRING, extensions_list);
1660
1661 args_info->extensions_given += local_args_info.extensions_given;
1662 local_args_info.extensions_given = 0;
1663
1664 if (check_required)
1665 {
1666 error_occurred += cmdline_parser_required2 (args_info, argv[0], additional_error);
1667 }
1668
1669 cmdline_parser_release (&local_args_info);
1670
1671 if ( error_occurred )
1672 return (EXIT_FAILURE);
1673
1674 if (optind < argc)
1675 {
1676 int i = 0 ;
1677 int found_prog_name = 0;
1678 /* whether program name, i.e., argv[0], is in the remaining args
1679 (this may happen with some implementations of getopt,
1680 but surely not with the one included by gengetopt) */
1681
1682
1683 args_info->inputs_num = argc - optind - found_prog_name;
1684 args_info->inputs =
1685 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1686 while (optind < argc)
1687 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1688 }
1689
1690 return 0;
1691
1692 failure:
1693 free_list (extensions_list, 1 );
1694
1695 cmdline_parser_release (&local_args_info);
1696 return (EXIT_FAILURE);
1697 }