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