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