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