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