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