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