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