OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
equationofstateCmd.cpp
1/*
2 File autogenerated by gengetopt version 2.23
3 generated with the following command:
4 gengetopt --no-handle-error --include-getopt --show-required --unamed-opts --file-name=equationofstateCmd --c-extension=cpp --header-extension=hpp
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
26
27const char *gengetopt_args_info_purpose = "This takes an OpenMD (.omd) file and generates equation of state for a \ncrystal. The periodic box is affine-scaled between starting and ending \nratios of the original box geometry, the energy is calculated at each \nscaled geometry, and the energy vs. scaling data is put into a\nspecified output file.\n\nExample:\n equationofstate -i Al2O3.omd -o Al2O3.eos -s 0.5 -e 1.5 -n 50";
28
29const char *gengetopt_args_info_usage = "Usage: equationofstate [OPTION]... [FILE]...";
30
31const char *gengetopt_args_info_versiontext = "";
32
33const char *gengetopt_args_info_description = "";
34
35const char *gengetopt_args_info_help[] = {
36 " -h, --help Print help and exit",
37 " -V, --version Print version and exit",
38 " -i, --input=filename use specified input (.omd) file (mandatory)",
39 " -o, --output=filename use specified output file (mandatory)",
40 " -s, --start=DOUBLE starting affine scale (default=`0.8')",
41 " -e, --end=DOUBLE ending affine scale (default=`1.2')",
42 " -n, --number=INT number of data points (default=`50')",
43 0
44};
45
46typedef enum {ARG_NO
47 , ARG_STRING
48 , ARG_INT
49 , ARG_DOUBLE
50} cmdline_parser_arg_type;
51
52static
53void clear_given (struct gengetopt_args_info *args_info);
54static
55void clear_args (struct gengetopt_args_info *args_info);
56
57static int
58cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
59 struct cmdline_parser_params *params, const char *additional_error);
60
61static int
62cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
63
64static char *
65gengetopt_strdup (const char *s);
66
67static
68void clear_given (struct gengetopt_args_info *args_info)
69{
70 args_info->help_given = 0 ;
71 args_info->version_given = 0 ;
72 args_info->input_given = 0 ;
73 args_info->output_given = 0 ;
74 args_info->start_given = 0 ;
75 args_info->end_given = 0 ;
76 args_info->number_given = 0 ;
77}
78
79static
80void clear_args (struct gengetopt_args_info *args_info)
81{
82 FIX_UNUSED (args_info);
83 args_info->input_arg = NULL;
84 args_info->input_orig = NULL;
85 args_info->output_arg = NULL;
86 args_info->output_orig = NULL;
87 args_info->start_arg = 0.8;
88 args_info->start_orig = NULL;
89 args_info->end_arg = 1.2;
90 args_info->end_orig = NULL;
91 args_info->number_arg = 50;
92 args_info->number_orig = NULL;
93
94}
95
96static
97void init_args_info(struct gengetopt_args_info *args_info)
98{
99
100
101 args_info->help_help = gengetopt_args_info_help[0] ;
102 args_info->version_help = gengetopt_args_info_help[1] ;
103 args_info->input_help = gengetopt_args_info_help[2] ;
104 args_info->output_help = gengetopt_args_info_help[3] ;
105 args_info->start_help = gengetopt_args_info_help[4] ;
106 args_info->end_help = gengetopt_args_info_help[5] ;
107 args_info->number_help = gengetopt_args_info_help[6] ;
108
109}
110
111void
112cmdline_parser_print_version (void)
113{
114 printf ("%s %s\n",
117
118 if (strlen(gengetopt_args_info_versiontext) > 0)
119 printf("\n%s\n", gengetopt_args_info_versiontext);
120}
121
122static void print_help_common(void)
123{
124 size_t len_purpose = strlen(gengetopt_args_info_purpose);
125 size_t len_usage = strlen(gengetopt_args_info_usage);
126
127 if (len_usage > 0) {
128 printf("%s\n", gengetopt_args_info_usage);
129 }
130 if (len_purpose > 0) {
131 printf("%s\n", gengetopt_args_info_purpose);
132 }
133
134 if (len_usage || len_purpose) {
135 printf("\n");
136 }
137
138 if (strlen(gengetopt_args_info_description) > 0) {
139 printf("%s\n\n", gengetopt_args_info_description);
140 }
141}
142
143void
144cmdline_parser_print_help (void)
145{
146 int i = 0;
147 print_help_common();
148 while (gengetopt_args_info_help[i])
149 printf("%s\n", gengetopt_args_info_help[i++]);
150}
151
152void
153cmdline_parser_init (struct gengetopt_args_info *args_info)
154{
155 clear_given (args_info);
156 clear_args (args_info);
157 init_args_info (args_info);
158
159 args_info->inputs = 0;
160 args_info->inputs_num = 0;
161}
162
163void
164cmdline_parser_params_init(struct cmdline_parser_params *params)
165{
166 if (params)
167 {
168 params->override = 0;
169 params->initialize = 1;
170 params->check_required = 1;
171 params->check_ambiguity = 0;
172 params->print_errors = 1;
173 }
174}
175
178{
179 struct cmdline_parser_params *params =
180 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
182 return params;
183}
184
185static void
186free_string_field (char **s)
187{
188 if (*s)
189 {
190 free (*s);
191 *s = 0;
192 }
193}
194
195
196static void
197cmdline_parser_release (struct gengetopt_args_info *args_info)
198{
199 unsigned int i;
200 free_string_field (&(args_info->input_arg));
201 free_string_field (&(args_info->input_orig));
202 free_string_field (&(args_info->output_arg));
203 free_string_field (&(args_info->output_orig));
204 free_string_field (&(args_info->start_orig));
205 free_string_field (&(args_info->end_orig));
206 free_string_field (&(args_info->number_orig));
207
208
209 for (i = 0; i < args_info->inputs_num; ++i)
210 free (args_info->inputs [i]);
211
212 if (args_info->inputs_num)
213 free (args_info->inputs);
214
215 clear_given (args_info);
216}
217
218
219static void
220write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
221{
222 FIX_UNUSED (values);
223 if (arg) {
224 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
225 } else {
226 fprintf(outfile, "%s\n", opt);
227 }
228}
229
230
231int
232cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
233{
234 int i = 0;
235
236 if (!outfile)
237 {
238 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
239 return EXIT_FAILURE;
240 }
241
242 if (args_info->help_given)
243 write_into_file(outfile, "help", 0, 0 );
244 if (args_info->version_given)
245 write_into_file(outfile, "version", 0, 0 );
246 if (args_info->input_given)
247 write_into_file(outfile, "input", args_info->input_orig, 0);
248 if (args_info->output_given)
249 write_into_file(outfile, "output", args_info->output_orig, 0);
250 if (args_info->start_given)
251 write_into_file(outfile, "start", args_info->start_orig, 0);
252 if (args_info->end_given)
253 write_into_file(outfile, "end", args_info->end_orig, 0);
254 if (args_info->number_given)
255 write_into_file(outfile, "number", args_info->number_orig, 0);
256
257
258 i = EXIT_SUCCESS;
259 return i;
260}
261
262int
263cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
264{
265 FILE *outfile;
266 int i = 0;
267
268 outfile = fopen(filename, "w");
269
270 if (!outfile)
271 {
272 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
273 return EXIT_FAILURE;
274 }
275
276 i = cmdline_parser_dump(outfile, args_info);
277 fclose (outfile);
278
279 return i;
280}
281
282void
284{
285 cmdline_parser_release (args_info);
286}
287
288/** @brief replacement of strdup, which is not standard */
289char *
290gengetopt_strdup (const char *s)
291{
292 char *result = 0;
293 if (!s)
294 return result;
295
296 result = (char*)malloc(strlen(s) + 1);
297 if (result == (char*)0)
298 return (char*)0;
299 strcpy(result, s);
300 return result;
301}
302
303int
304cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
305{
306 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
307}
308
309int
310cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
311 struct cmdline_parser_params *params)
312{
313 int result;
314 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
315
316 return result;
317}
318
319int
320cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
321{
322 int result;
323 struct cmdline_parser_params params;
324
325 params.override = override;
326 params.initialize = initialize;
328 params.check_ambiguity = 0;
329 params.print_errors = 1;
330
331 result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
332
333 return result;
334}
335
336int
337cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
338{
339 int result = EXIT_SUCCESS;
340
341 if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
342 result = EXIT_FAILURE;
343
344 return result;
345}
346
347int
348cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
349{
350 int error_occurred = 0;
351 FIX_UNUSED (additional_error);
352
353 /* checks for required options */
354 if (! args_info->input_given)
355 {
356 fprintf (stderr, "%s: '--input' ('-i') option required%s\n", prog_name, (additional_error ? additional_error : ""));
357 error_occurred = 1;
358 }
359
360 if (! args_info->output_given)
361 {
362 fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
363 error_occurred = 1;
364 }
365
366
367 /* checks for dependences among options */
368
369 return error_occurred;
370}
371
372/*
373 * Extracted from the glibc source tree, version 2.3.6
374 *
375 * Licensed under the GPL as per the whole glibc source tree.
376 *
377 * This file was modified so that getopt_long can be called
378 * many times without risking previous memory to be spoiled.
379 *
380 * Modified by Andre Noll and Lorenzo Bettini for use in
381 * GNU gengetopt generated files.
382 *
383 */
384
385/*
386 * we must include anything we need since this file is not thought to be
387 * inserted in a file already using getopt.h
388 *
389 * Lorenzo
390 */
391
392struct option
393{
394 const char *name;
395 /* has_arg can't be an enum because some compilers complain about
396 type mismatches in all the code that assumes it is an int. */
397 int has_arg;
398 int *flag;
399 int val;
400};
401
402/* This version of `getopt' appears to the caller like standard Unix `getopt'
403 but it behaves differently for the user, since it allows the user
404 to intersperse the options with the other arguments.
405
406 As `getopt' works, it permutes the elements of ARGV so that,
407 when it is done, all the options precede everything else. Thus
408 all application programs are extended to handle flexible argument order.
409*/
410/*
411 If the field `flag' is not NULL, it points to a variable that is set
412 to the value given in the field `val' when the option is found, but
413 left unchanged if the option is not found.
414
415 To have a long-named option do something other than set an `int' to
416 a compiled-in constant, such as set a value from `custom_optarg', set the
417 option's `flag' field to zero and its `val' field to a nonzero
418 value (the equivalent single-letter option character, if there is
419 one). For long options that have a zero `flag' field, `getopt'
420 returns the contents of the `val' field. */
421
422/* Names for the values of the `has_arg' field of `struct option'. */
423#ifndef no_argument
424#define no_argument 0
425#endif
426
427#ifndef required_argument
428#define required_argument 1
429#endif
430
431#ifndef optional_argument
432#define optional_argument 2
433#endif
434
435struct custom_getopt_data {
436 /*
437 * These have exactly the same meaning as the corresponding global variables,
438 * except that they are used for the reentrant versions of getopt.
439 */
440 int custom_optind;
441 int custom_opterr;
442 int custom_optopt;
443 char *custom_optarg;
444
445 /* True if the internal members have been initialized. */
446 int initialized;
447
448 /*
449 * The next char to be scanned in the option-element in which the last option
450 * character we returned was found. This allows us to pick up the scan where
451 * we left off. If this is zero, or a null string, it means resume the scan by
452 * advancing to the next ARGV-element.
453 */
454 char *nextchar;
455
456 /*
457 * Describe the part of ARGV that contains non-options that have been skipped.
458 * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
459 * the index after the last of them.
460 */
461 int first_nonopt;
462 int last_nonopt;
463};
464
465/*
466 * the variables optarg, optind, opterr and optopt are renamed with
467 * the custom_ prefix so that they don't interfere with getopt ones.
468 *
469 * Moreover they're static so they are visible only from within the
470 * file where this very file will be included.
471 */
472
473/*
474 * For communication from `custom_getopt' to the caller. When `custom_getopt' finds an
475 * option that takes an argument, the argument value is returned here.
476 */
477static char *custom_optarg;
478
479/*
480 * Index in ARGV of the next element to be scanned. This is used for
481 * communication to and from the caller and for communication between
482 * successive calls to `custom_getopt'.
483 *
484 * On entry to `custom_getopt', 1 means this is the first call; initialize.
485 *
486 * When `custom_getopt' returns -1, this is the index of the first of the non-option
487 * elements that the caller should itself scan.
488 *
489 * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
490 * has been scanned so far.
491 *
492 * 1003.2 says this must be 1 before any call.
493 */
494static int custom_optind = 1;
495
496/*
497 * Callers store zero here to inhibit the error message for unrecognized
498 * options.
499 */
500static int custom_opterr = 1;
501
502/*
503 * Set to an option character which was unrecognized. This must be initialized
504 * on some systems to avoid linking in the system's own getopt implementation.
505 */
506static int custom_optopt = '?';
507
508/*
509 * Exchange two adjacent subsequences of ARGV. One subsequence is elements
510 * [first_nonopt,last_nonopt) which contains all the non-options that have been
511 * skipped so far. The other is elements [last_nonopt,custom_optind), which contains
512 * all the options processed since those non-options were skipped.
513 * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
514 * indices of the non-options in ARGV after they are moved.
515 */
516static void exchange(char **argv, struct custom_getopt_data *d)
517{
518 int bottom = d->first_nonopt;
519 int middle = d->last_nonopt;
520 int top = d->custom_optind;
521 char *tem;
522
523 /*
524 * Exchange the shorter segment with the far end of the longer segment.
525 * That puts the shorter segment into the right place. It leaves the
526 * longer segment in the right place overall, but it consists of two
527 * parts that need to be swapped next.
528 */
529 while (top > middle && middle > bottom) {
530 if (top - middle > middle - bottom) {
531 /* Bottom segment is the short one. */
532 int len = middle - bottom;
533 int i;
534
535 /* Swap it with the top part of the top segment. */
536 for (i = 0; i < len; i++) {
537 tem = argv[bottom + i];
538 argv[bottom + i] =
539 argv[top - (middle - bottom) + i];
540 argv[top - (middle - bottom) + i] = tem;
541 }
542 /* Exclude the moved bottom segment from further swapping. */
543 top -= len;
544 } else {
545 /* Top segment is the short one. */
546 int len = top - middle;
547 int i;
548
549 /* Swap it with the bottom part of the bottom segment. */
550 for (i = 0; i < len; i++) {
551 tem = argv[bottom + i];
552 argv[bottom + i] = argv[middle + i];
553 argv[middle + i] = tem;
554 }
555 /* Exclude the moved top segment from further swapping. */
556 bottom += len;
557 }
558 }
559 /* Update records for the slots the non-options now occupy. */
560 d->first_nonopt += (d->custom_optind - d->last_nonopt);
561 d->last_nonopt = d->custom_optind;
562}
563
564/* Initialize the internal data when the first call is made. */
565static void custom_getopt_initialize(struct custom_getopt_data *d)
566{
567 /*
568 * Start processing options with ARGV-element 1 (since ARGV-element 0
569 * is the program name); the sequence of previously skipped non-option
570 * ARGV-elements is empty.
571 */
572 d->first_nonopt = d->last_nonopt = d->custom_optind;
573 d->nextchar = NULL;
574 d->initialized = 1;
575}
576
577#define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
578
579/* return: zero: continue, nonzero: return given value to user */
580static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
581 struct custom_getopt_data *d)
582{
583 /*
584 * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
585 * moved back by the user (who may also have changed the arguments).
586 */
587 if (d->last_nonopt > d->custom_optind)
588 d->last_nonopt = d->custom_optind;
589 if (d->first_nonopt > d->custom_optind)
590 d->first_nonopt = d->custom_optind;
591 /*
592 * If we have just processed some options following some
593 * non-options, exchange them so that the options come first.
594 */
595 if (d->first_nonopt != d->last_nonopt &&
596 d->last_nonopt != d->custom_optind)
597 exchange((char **) argv, d);
598 else if (d->last_nonopt != d->custom_optind)
599 d->first_nonopt = d->custom_optind;
600 /*
601 * Skip any additional non-options and extend the range of
602 * non-options previously skipped.
603 */
604 while (d->custom_optind < argc && NONOPTION_P)
605 d->custom_optind++;
606 d->last_nonopt = d->custom_optind;
607 /*
608 * The special ARGV-element `--' means premature end of options. Skip
609 * it like a null option, then exchange with previous non-options as if
610 * it were an option, then skip everything else like a non-option.
611 */
612 if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
613 d->custom_optind++;
614 if (d->first_nonopt != d->last_nonopt
615 && d->last_nonopt != d->custom_optind)
616 exchange((char **) argv, d);
617 else if (d->first_nonopt == d->last_nonopt)
618 d->first_nonopt = d->custom_optind;
619 d->last_nonopt = argc;
620 d->custom_optind = argc;
621 }
622 /*
623 * If we have done all the ARGV-elements, stop the scan and back over
624 * any non-options that we skipped and permuted.
625 */
626 if (d->custom_optind == argc) {
627 /*
628 * Set the next-arg-index to point at the non-options that we
629 * previously skipped, so the caller will digest them.
630 */
631 if (d->first_nonopt != d->last_nonopt)
632 d->custom_optind = d->first_nonopt;
633 return -1;
634 }
635 /*
636 * If we have come to a non-option and did not permute it, either stop
637 * the scan or describe it to the caller and pass it by.
638 */
639 if (NONOPTION_P) {
640 d->custom_optarg = argv[d->custom_optind++];
641 return 1;
642 }
643 /*
644 * We have found another option-ARGV-element. Skip the initial
645 * punctuation.
646 */
647 d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
648 return 0;
649}
650
651/*
652 * Check whether the ARGV-element is a long option.
653 *
654 * If there's a long option "fubar" and the ARGV-element is "-fu", consider
655 * that an abbreviation of the long option, just like "--fu", and not "-f" with
656 * arg "u".
657 *
658 * This distinction seems to be the most useful approach.
659 *
660 */
661static int check_long_opt(int argc, char *const *argv, const char *optstring,
662 const struct option *longopts, int *longind,
663 int print_errors, struct custom_getopt_data *d)
664{
665 char *nameend;
666 const struct option *p;
667 const struct option *pfound = NULL;
668 int exact = 0;
669 int ambig = 0;
670 int indfound = -1;
671 int option_index;
672
673 for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
674 /* Do nothing. */ ;
675
676 /* Test all long options for either exact match or abbreviated matches */
677 for (p = longopts, option_index = 0; p->name; p++, option_index++)
678 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
679 if ((unsigned int) (nameend - d->nextchar)
680 == (unsigned int) strlen(p->name)) {
681 /* Exact match found. */
682 pfound = p;
683 indfound = option_index;
684 exact = 1;
685 break;
686 } else if (pfound == NULL) {
687 /* First nonexact match found. */
688 pfound = p;
689 indfound = option_index;
690 } else if (pfound->has_arg != p->has_arg
691 || pfound->flag != p->flag
692 || pfound->val != p->val)
693 /* Second or later nonexact match found. */
694 ambig = 1;
695 }
696 if (ambig && !exact) {
697 if (print_errors) {
698 fprintf(stderr,
699 "%s: option `%s' is ambiguous\n",
700 argv[0], argv[d->custom_optind]);
701 }
702 d->nextchar += strlen(d->nextchar);
703 d->custom_optind++;
704 d->custom_optopt = 0;
705 return '?';
706 }
707 if (pfound) {
708 option_index = indfound;
709 d->custom_optind++;
710 if (*nameend) {
711 if (pfound->has_arg != no_argument)
712 d->custom_optarg = nameend + 1;
713 else {
714 if (print_errors) {
715 if (argv[d->custom_optind - 1][1] == '-') {
716 /* --option */
717 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
718 argv[0], pfound->name);
719 } else {
720 /* +option or -option */
721 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
722 argv[0], argv[d->custom_optind - 1][0], pfound->name);
723 }
724
725 }
726 d->nextchar += strlen(d->nextchar);
727 d->custom_optopt = pfound->val;
728 return '?';
729 }
730 } else if (pfound->has_arg == required_argument) {
731 if (d->custom_optind < argc)
732 d->custom_optarg = argv[d->custom_optind++];
733 else {
734 if (print_errors) {
735 fprintf(stderr,
736 "%s: option `%s' requires an argument\n",
737 argv[0],
738 argv[d->custom_optind - 1]);
739 }
740 d->nextchar += strlen(d->nextchar);
741 d->custom_optopt = pfound->val;
742 return optstring[0] == ':' ? ':' : '?';
743 }
744 }
745 d->nextchar += strlen(d->nextchar);
746 if (longind != NULL)
747 *longind = option_index;
748 if (pfound->flag) {
749 *(pfound->flag) = pfound->val;
750 return 0;
751 }
752 return pfound->val;
753 }
754 /*
755 * Can't find it as a long option. If this is not getopt_long_only, or
756 * the option starts with '--' or is not a valid short option, then
757 * it's an error. Otherwise interpret it as a short option.
758 */
759 if (print_errors) {
760 if (argv[d->custom_optind][1] == '-') {
761 /* --option */
762 fprintf(stderr,
763 "%s: unrecognized option `--%s'\n",
764 argv[0], d->nextchar);
765 } else {
766 /* +option or -option */
767 fprintf(stderr,
768 "%s: unrecognized option `%c%s'\n",
769 argv[0], argv[d->custom_optind][0],
770 d->nextchar);
771 }
772 }
773 d->nextchar = (char *) "";
774 d->custom_optind++;
775 d->custom_optopt = 0;
776 return '?';
777}
778
779static int check_short_opt(int argc, char *const *argv, const char *optstring,
780 int print_errors, struct custom_getopt_data *d)
781{
782 char c = *d->nextchar++;
783 const char *temp = strchr(optstring, c);
784
785 /* Increment `custom_optind' when we start to process its last character. */
786 if (*d->nextchar == '\0')
787 ++d->custom_optind;
788 if (!temp || c == ':') {
789 if (print_errors)
790 fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
791
792 d->custom_optopt = c;
793 return '?';
794 }
795 if (temp[1] == ':') {
796 if (temp[2] == ':') {
797 /* This is an option that accepts an argument optionally. */
798 if (*d->nextchar != '\0') {
799 d->custom_optarg = d->nextchar;
800 d->custom_optind++;
801 } else
802 d->custom_optarg = NULL;
803 d->nextchar = NULL;
804 } else {
805 /* This is an option that requires an argument. */
806 if (*d->nextchar != '\0') {
807 d->custom_optarg = d->nextchar;
808 /*
809 * If we end this ARGV-element by taking the
810 * rest as an arg, we must advance to the next
811 * element now.
812 */
813 d->custom_optind++;
814 } else if (d->custom_optind == argc) {
815 if (print_errors) {
816 fprintf(stderr,
817 "%s: option requires an argument -- %c\n",
818 argv[0], c);
819 }
820 d->custom_optopt = c;
821 if (optstring[0] == ':')
822 c = ':';
823 else
824 c = '?';
825 } else
826 /*
827 * We already incremented `custom_optind' once;
828 * increment it again when taking next ARGV-elt
829 * as argument.
830 */
831 d->custom_optarg = argv[d->custom_optind++];
832 d->nextchar = NULL;
833 }
834 }
835 return c;
836}
837
838/*
839 * Scan elements of ARGV for option characters given in OPTSTRING.
840 *
841 * If an element of ARGV starts with '-', and is not exactly "-" or "--",
842 * then it is an option element. The characters of this element
843 * (aside from the initial '-') are option characters. If `getopt'
844 * is called repeatedly, it returns successively each of the option characters
845 * from each of the option elements.
846 *
847 * If `getopt' finds another option character, it returns that character,
848 * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
849 * resume the scan with the following option character or ARGV-element.
850 *
851 * If there are no more option characters, `getopt' returns -1.
852 * Then `custom_optind' is the index in ARGV of the first ARGV-element
853 * that is not an option. (The ARGV-elements have been permuted
854 * so that those that are not options now come last.)
855 *
856 * OPTSTRING is a string containing the legitimate option characters.
857 * If an option character is seen that is not listed in OPTSTRING,
858 * return '?' after printing an error message. If you set `custom_opterr' to
859 * zero, the error message is suppressed but we still return '?'.
860 *
861 * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
862 * so the following text in the same ARGV-element, or the text of the following
863 * ARGV-element, is returned in `custom_optarg'. Two colons mean an option that
864 * wants an optional arg; if there is text in the current ARGV-element,
865 * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
866 *
867 * If OPTSTRING starts with `-' or `+', it requests different methods of
868 * handling the non-option ARGV-elements.
869 * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
870 *
871 * Long-named options begin with `--' instead of `-'.
872 * Their names may be abbreviated as long as the abbreviation is unique
873 * or is an exact match for some defined option. If they have an
874 * argument, it follows the option name in the same ARGV-element, separated
875 * from the option name by a `=', or else the in next ARGV-element.
876 * When `getopt' finds a long-named option, it returns 0 if that option's
877 * `flag' field is nonzero, the value of the option's `val' field
878 * if the `flag' field is zero.
879 *
880 * The elements of ARGV aren't really const, because we permute them.
881 * But we pretend they're const in the prototype to be compatible
882 * with other systems.
883 *
884 * LONGOPTS is a vector of `struct option' terminated by an
885 * element containing a name which is zero.
886 *
887 * LONGIND returns the index in LONGOPT of the long-named option found.
888 * It is only valid when a long-named option has been found by the most
889 * recent call.
890 *
891 * Return the option character from OPTS just read. Return -1 when there are
892 * no more options. For unrecognized options, or options missing arguments,
893 * `custom_optopt' is set to the option letter, and '?' is returned.
894 *
895 * The OPTS string is a list of characters which are recognized option letters,
896 * optionally followed by colons, specifying that that letter takes an
897 * argument, to be placed in `custom_optarg'.
898 *
899 * If a letter in OPTS is followed by two colons, its argument is optional.
900 * This behavior is specific to the GNU `getopt'.
901 *
902 * The argument `--' causes premature termination of argument scanning,
903 * explicitly telling `getopt' that there are no more options. If OPTS begins
904 * with `--', then non-option arguments are treated as arguments to the option
905 * '\0'. This behavior is specific to the GNU `getopt'.
906 */
907
908static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
909 const struct option *longopts, int *longind,
910 struct custom_getopt_data *d)
911{
912 int ret, print_errors = d->custom_opterr;
913
914 if (optstring[0] == ':')
915 print_errors = 0;
916 if (argc < 1)
917 return -1;
918 d->custom_optarg = NULL;
919
920 /*
921 * This is a big difference with GNU getopt, since optind == 0
922 * means initialization while here 1 means first call.
923 */
924 if (d->custom_optind == 0 || !d->initialized) {
925 if (d->custom_optind == 0)
926 d->custom_optind = 1; /* Don't scan ARGV[0], the program name. */
927 custom_getopt_initialize(d);
928 }
929 if (d->nextchar == NULL || *d->nextchar == '\0') {
930 ret = shuffle_argv(argc, argv, longopts, d);
931 if (ret)
932 return ret;
933 }
934 if (longopts && (argv[d->custom_optind][1] == '-' ))
935 return check_long_opt(argc, argv, optstring, longopts,
936 longind, print_errors, d);
937 return check_short_opt(argc, argv, optstring, print_errors, d);
938}
939
940static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
941 const struct option *longopts, int *longind)
942{
943 int result;
944 /* Keep a global copy of all internal members of d */
945 static struct custom_getopt_data d;
946
947 d.custom_optind = custom_optind;
948 d.custom_opterr = custom_opterr;
949 result = getopt_internal_r(argc, argv, optstring, longopts,
950 longind, &d);
951 custom_optind = d.custom_optind;
952 custom_optarg = d.custom_optarg;
953 custom_optopt = d.custom_optopt;
954 return result;
955}
956
957static int custom_getopt_long (int argc, char *const *argv, const char *options,
958 const struct option *long_options, int *opt_index)
959{
960 return custom_getopt_internal(argc, argv, options, long_options,
961 opt_index);
962}
963
964
965static char *package_name = 0;
966
967/**
968 * @brief updates an option
969 * @param field the generic pointer to the field to update
970 * @param orig_field the pointer to the orig field
971 * @param field_given the pointer to the number of occurrence of this option
972 * @param prev_given the pointer to the number of occurrence already seen
973 * @param value the argument for this option (if null no arg was specified)
974 * @param possible_values the possible values for this option (if specified)
975 * @param default_value the default value (in case the option only accepts fixed values)
976 * @param arg_type the type of this option
977 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
978 * @param override @see cmdline_parser_params.override
979 * @param no_free whether to free a possible previous value
980 * @param multiple_option whether this is a multiple option
981 * @param long_opt the corresponding long option
982 * @param short_opt the corresponding short option (or '-' if none)
983 * @param additional_error possible further error specification
984 */
985static
986int update_arg(void *field, char **orig_field,
987 unsigned int *field_given, unsigned int *prev_given,
988 char *value, const char *possible_values[],
989 const char *default_value,
990 cmdline_parser_arg_type arg_type,
991 int check_ambiguity, int override,
992 int no_free, int multiple_option,
993 const char *long_opt, char short_opt,
994 const char *additional_error)
995{
996 char *stop_char = 0;
997 const char *val = value;
998 int found;
999 char **string_field;
1000 FIX_UNUSED (field);
1001
1002 stop_char = 0;
1003 found = 0;
1004
1005 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
1006 {
1007 if (short_opt != '-')
1008 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
1009 package_name, long_opt, short_opt,
1010 (additional_error ? additional_error : ""));
1011 else
1012 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
1013 package_name, long_opt,
1014 (additional_error ? additional_error : ""));
1015 return 1; /* failure */
1016 }
1017
1018 FIX_UNUSED (default_value);
1019
1020 if (field_given && *field_given && ! override)
1021 return 0;
1022 if (prev_given)
1023 (*prev_given)++;
1024 if (field_given)
1025 (*field_given)++;
1026 if (possible_values)
1027 val = possible_values[found];
1028
1029 switch(arg_type) {
1030 case ARG_INT:
1031 if (val) *((int *)field) = strtol (val, &stop_char, 0);
1032 break;
1033 case ARG_DOUBLE:
1034 if (val) *((double *)field) = strtod (val, &stop_char);
1035 break;
1036 case ARG_STRING:
1037 if (val) {
1038 string_field = (char **)field;
1039 if (!no_free && *string_field)
1040 free (*string_field); /* free previous string */
1041 *string_field = gengetopt_strdup (val);
1042 }
1043 break;
1044 default:
1045 break;
1046 };
1047
1048 /* check numeric conversion */
1049 switch(arg_type) {
1050 case ARG_INT:
1051 case ARG_DOUBLE:
1052 if (val && !(stop_char && *stop_char == '\0')) {
1053 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
1054 return 1; /* failure */
1055 }
1056 break;
1057 default:
1058 ;
1059 };
1060
1061 /* store the original value */
1062 switch(arg_type) {
1063 case ARG_NO:
1064 break;
1065 default:
1066 if (value && orig_field) {
1067 if (no_free) {
1068 *orig_field = value;
1069 } else {
1070 if (*orig_field)
1071 free (*orig_field); /* free previous string */
1072 *orig_field = gengetopt_strdup (value);
1073 }
1074 }
1075 };
1076
1077 return 0; /* OK */
1078}
1079
1080
1081int
1082cmdline_parser_internal (
1083 int argc, char **argv, struct gengetopt_args_info *args_info,
1084 struct cmdline_parser_params *params, const char *additional_error)
1085{
1086 int c; /* Character of the parsed option. */
1087
1088 int error_occurred = 0;
1089 struct gengetopt_args_info local_args_info;
1090
1091 int override;
1092 int initialize;
1093 int check_required;
1094 int check_ambiguity;
1095
1096 char *optarg;
1097 int optind;
1098 int opterr;
1099 int optopt;
1100
1101 package_name = argv[0];
1102
1103 /* TODO: Why is this here? It is not used anywhere. */
1104 override = params->override;
1105 FIX_UNUSED(override);
1106
1107 initialize = params->initialize;
1108 check_required = params->check_required;
1109
1110 /* TODO: Why is this here? It is not used anywhere. */
1111 check_ambiguity = params->check_ambiguity;
1112 FIX_UNUSED(check_ambiguity);
1113
1114 if (initialize)
1115 cmdline_parser_init (args_info);
1116
1117 cmdline_parser_init (&local_args_info);
1118
1119 optarg = 0;
1120 optind = 0;
1121 opterr = params->print_errors;
1122 optopt = '?';
1123
1124 while (1)
1125 {
1126 int option_index = 0;
1127
1128 static struct option long_options[] = {
1129 { "help", 0, NULL, 'h' },
1130 { "version", 0, NULL, 'V' },
1131 { "input", 1, NULL, 'i' },
1132 { "output", 1, NULL, 'o' },
1133 { "start", 1, NULL, 's' },
1134 { "end", 1, NULL, 'e' },
1135 { "number", 1, NULL, 'n' },
1136 { 0, 0, 0, 0 }
1137 };
1138
1139 custom_optarg = optarg;
1140 custom_optind = optind;
1141 custom_opterr = opterr;
1142 custom_optopt = optopt;
1143
1144 c = custom_getopt_long (argc, argv, "hVi:o:s:e:n:", long_options, &option_index);
1145
1146 optarg = custom_optarg;
1147 optind = custom_optind;
1148 opterr = custom_opterr;
1149 optopt = custom_optopt;
1150
1151 if (c == -1) break; /* Exit from `while (1)' loop. */
1152
1153 switch (c)
1154 {
1155 case 'h': /* Print help and exit. */
1156 cmdline_parser_print_help ();
1157 cmdline_parser_free (&local_args_info);
1158 exit (EXIT_SUCCESS);
1159
1160 case 'V': /* Print version and exit. */
1161 cmdline_parser_print_version ();
1162 cmdline_parser_free (&local_args_info);
1163 exit (EXIT_SUCCESS);
1164
1165 case 'i': /* use specified input (.omd) file. */
1166
1167
1168 if (update_arg( (void *)&(args_info->input_arg),
1169 &(args_info->input_orig), &(args_info->input_given),
1170 &(local_args_info.input_given), optarg, 0, 0, ARG_STRING,
1171 check_ambiguity, override, 0, 0,
1172 "input", 'i',
1173 additional_error))
1174 goto failure;
1175
1176 break;
1177 case 'o': /* use specified output file. */
1178
1179
1180 if (update_arg( (void *)&(args_info->output_arg),
1181 &(args_info->output_orig), &(args_info->output_given),
1182 &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
1183 check_ambiguity, override, 0, 0,
1184 "output", 'o',
1185 additional_error))
1186 goto failure;
1187
1188 break;
1189 case 's': /* starting affine scale. */
1190
1191
1192 if (update_arg( (void *)&(args_info->start_arg),
1193 &(args_info->start_orig), &(args_info->start_given),
1194 &(local_args_info.start_given), optarg, 0, "0.8", ARG_DOUBLE,
1195 check_ambiguity, override, 0, 0,
1196 "start", 's',
1197 additional_error))
1198 goto failure;
1199
1200 break;
1201 case 'e': /* ending affine scale. */
1202
1203
1204 if (update_arg( (void *)&(args_info->end_arg),
1205 &(args_info->end_orig), &(args_info->end_given),
1206 &(local_args_info.end_given), optarg, 0, "1.2", ARG_DOUBLE,
1207 check_ambiguity, override, 0, 0,
1208 "end", 'e',
1209 additional_error))
1210 goto failure;
1211
1212 break;
1213 case 'n': /* number of data points. */
1214
1215
1216 if (update_arg( (void *)&(args_info->number_arg),
1217 &(args_info->number_orig), &(args_info->number_given),
1218 &(local_args_info.number_given), optarg, 0, "50", ARG_INT,
1219 check_ambiguity, override, 0, 0,
1220 "number", 'n',
1221 additional_error))
1222 goto failure;
1223
1224 break;
1225
1226 case 0: /* Long option with no short option */
1227 case '?': /* Invalid option. */
1228 /* `getopt_long' already printed an error message. */
1229 goto failure;
1230
1231 default: /* bug: option not considered. */
1232 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1233 abort ();
1234 } /* switch */
1235 } /* while */
1236
1237
1238
1239 if (check_required)
1240 {
1241 error_occurred += cmdline_parser_required2 (args_info, argv[0], additional_error);
1242 }
1243
1244 cmdline_parser_release (&local_args_info);
1245
1246 if ( error_occurred )
1247 return (EXIT_FAILURE);
1248
1249 if (optind < argc)
1250 {
1251 int i = 0 ;
1252 int found_prog_name = 0;
1253 /* whether program name, i.e., argv[0], is in the remaining args
1254 (this may happen with some implementations of getopt,
1255 but surely not with the one included by gengetopt) */
1256
1257
1258 args_info->inputs_num = argc - optind - found_prog_name;
1259 args_info->inputs =
1260 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1261 while (optind < argc)
1262 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1263 }
1264
1265 return 0;
1266
1267failure:
1268
1269 cmdline_parser_release (&local_args_info);
1270 return (EXIT_FAILURE);
1271}
1272/* vim: set ft=c noet ts=8 sts=8 sw=8 tw=80 nojs spell : */
#define CMDLINE_PARSER_VERSION
the program version
#define CMDLINE_PARSER_PACKAGE_NAME
the complete program name (used for help and version)
#define CMDLINE_PARSER_PACKAGE
the program name (used for printing errors)
The header file for the command line option parser generated by GNU Gengetopt version 2....
The additional parameters to pass to parser functions.
int print_errors
whether getopt_long should print an error message for a bad option (default 1)
int check_required
whether to check that all required options were provided (default 1)
int check_ambiguity
whether to check for options already specified in the option structure gengetopt_args_info (default 0...
int initialize
whether to initialize the option structure gengetopt_args_info (default 1)
int override
whether to override possibly already present options (default 0)
Where the command line options are stored.
unsigned int output_given
Whether output was given.
char * output_orig
output file name original value given at command line.
const char * output_help
output file name help description.
const char * number_help
number of data points help description.
unsigned int end_given
Whether end was given.
char * start_orig
starting affine scale original value given at command line.
unsigned inputs_num
unamed options number
unsigned int number_given
Whether number was given.
const char * help_help
Print help and exit help description.
unsigned int start_given
Whether start was given.
char * output_arg
output file name.
char ** inputs
unamed options (options without names)
int number_arg
number of data points (default='50').
char * input_arg
input dump file.
double start_arg
starting affine scale (default='0.8').
const char * end_help
ending affine scale help description.
char * end_orig
ending affine scale original value given at command line.
unsigned int help_given
Whether help was given.
unsigned int input_given
Whether input was given.
unsigned int version_given
Whether version was given.
double end_arg
ending affine scale (default='1.2').
const char * version_help
Print version and exit help description.
const char * start_help
starting affine scale help description.
char * input_orig
input dump file original value given at command line.
char * number_orig
number of data points original value given at command line.
const char * input_help
input dump file help description.
int cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
Save the contents of the option struct into an already open FILE stream.
int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
The command line parser.
int cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
Save the contents of the option struct into a (text) file.
int cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
The command line parser (version with additional parameters - deprecated)
int cmdline_parser_required(struct gengetopt_args_info *args_info, const char *prog_name)
Checks that all the required options were specified.
struct cmdline_parser_params * cmdline_parser_params_create(void)
Allocates dynamically a cmdline_parser_params structure and initializes all its fields to their defau...
int cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info, struct cmdline_parser_params *params)
The command line parser (version with additional parameters)
void cmdline_parser_init(struct gengetopt_args_info *args_info)
Initializes the passed gengetopt_args_info structure's fields (also set default values for options th...
void cmdline_parser_free(struct gengetopt_args_info *args_info)
Deallocates the string fields of the gengetopt_args_info structure (but does not deallocate the struc...
void cmdline_parser_params_init(struct cmdline_parser_params *params)
Initializes all the fields a cmdline_parser_params structure to their default values.