1 |
/* |
2 |
File autogenerated by gengetopt version 2.22.4 |
3 |
generated with the following command: |
4 |
gengetopt --file-name=nanorodBuilderCmd --unamed-opts |
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 |
#include <getopt.h> |
25 |
|
26 |
#include "nanorodBuilderCmd.h" |
27 |
|
28 |
const char *gengetopt_args_info_purpose = "Builds pill-shaped, ellipsoidal, or pentagonal nanoparticles and outputs an \nOpenMD startup file"; |
29 |
|
30 |
const char *gengetopt_args_info_usage = "Usage: nanorodBuilder [OPTIONS]... [FILES]..."; |
31 |
|
32 |
const char *gengetopt_args_info_description = ""; |
33 |
|
34 |
const char *gengetopt_args_info_help[] = { |
35 |
" -h, --help Print help and exit", |
36 |
" -V, --version Print version and exit", |
37 |
" -o, --output=STRING Output file name", |
38 |
" --latticeConstant=DOUBLE Lattice spacing in Angstroms for cubic lattice.", |
39 |
" --radius=DOUBLE Nanorod radius in Angstroms", |
40 |
" --length=DOUBLE Nanorod length in Angstroms", |
41 |
" -e, --ellipsoid Build an Ellipsoid instead of a rod. \n (default=off)", |
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 |
|
50 |
typedef enum {ARG_NO |
51 |
, ARG_FLAG |
52 |
, ARG_STRING |
53 |
, ARG_DOUBLE |
54 |
} cmdline_parser_arg_type; |
55 |
|
56 |
static |
57 |
void clear_given (struct gengetopt_args_info *args_info); |
58 |
static |
59 |
void clear_args (struct gengetopt_args_info *args_info); |
60 |
|
61 |
static int |
62 |
cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info, |
63 |
struct cmdline_parser_params *params, const char *additional_error); |
64 |
|
65 |
static int |
66 |
cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error); |
67 |
|
68 |
static char * |
69 |
gengetopt_strdup (const char *s); |
70 |
|
71 |
static |
72 |
void clear_given (struct gengetopt_args_info *args_info) |
73 |
{ |
74 |
args_info->help_given = 0 ; |
75 |
args_info->version_given = 0 ; |
76 |
args_info->output_given = 0 ; |
77 |
args_info->latticeConstant_given = 0 ; |
78 |
args_info->radius_given = 0 ; |
79 |
args_info->length_given = 0 ; |
80 |
args_info->ellipsoid_given = 0 ; |
81 |
args_info->shellRadius_given = 0 ; |
82 |
args_info->molFraction_given = 0 ; |
83 |
args_info->vacancyPercent_given = 0 ; |
84 |
args_info->vacancyInnerRadius_given = 0 ; |
85 |
args_info->vacancyOuterRadius_given = 0 ; |
86 |
} |
87 |
|
88 |
static |
89 |
void clear_args (struct gengetopt_args_info *args_info) |
90 |
{ |
91 |
FIX_UNUSED (args_info); |
92 |
args_info->output_arg = NULL; |
93 |
args_info->output_orig = NULL; |
94 |
args_info->latticeConstant_orig = NULL; |
95 |
args_info->radius_orig = NULL; |
96 |
args_info->length_orig = NULL; |
97 |
args_info->ellipsoid_flag = 0; |
98 |
args_info->shellRadius_arg = NULL; |
99 |
args_info->shellRadius_orig = NULL; |
100 |
args_info->molFraction_arg = NULL; |
101 |
args_info->molFraction_orig = NULL; |
102 |
args_info->vacancyPercent_orig = NULL; |
103 |
args_info->vacancyInnerRadius_orig = NULL; |
104 |
args_info->vacancyOuterRadius_orig = NULL; |
105 |
|
106 |
} |
107 |
|
108 |
static |
109 |
void init_args_info(struct gengetopt_args_info *args_info) |
110 |
{ |
111 |
|
112 |
|
113 |
args_info->help_help = gengetopt_args_info_help[0] ; |
114 |
args_info->version_help = gengetopt_args_info_help[1] ; |
115 |
args_info->output_help = gengetopt_args_info_help[2] ; |
116 |
args_info->latticeConstant_help = gengetopt_args_info_help[3] ; |
117 |
args_info->radius_help = gengetopt_args_info_help[4] ; |
118 |
args_info->length_help = gengetopt_args_info_help[5] ; |
119 |
args_info->ellipsoid_help = gengetopt_args_info_help[6] ; |
120 |
args_info->shellRadius_help = gengetopt_args_info_help[7] ; |
121 |
args_info->shellRadius_min = 0; |
122 |
args_info->shellRadius_max = 0; |
123 |
args_info->molFraction_help = gengetopt_args_info_help[8] ; |
124 |
args_info->molFraction_min = 0; |
125 |
args_info->molFraction_max = 0; |
126 |
args_info->vacancyPercent_help = gengetopt_args_info_help[9] ; |
127 |
args_info->vacancyInnerRadius_help = gengetopt_args_info_help[10] ; |
128 |
args_info->vacancyOuterRadius_help = gengetopt_args_info_help[11] ; |
129 |
|
130 |
} |
131 |
|
132 |
void |
133 |
cmdline_parser_print_version (void) |
134 |
{ |
135 |
printf ("%s %s\n", |
136 |
(strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE), |
137 |
CMDLINE_PARSER_VERSION); |
138 |
} |
139 |
|
140 |
static void print_help_common(void) { |
141 |
cmdline_parser_print_version (); |
142 |
|
143 |
if (strlen(gengetopt_args_info_purpose) > 0) |
144 |
printf("\n%s\n", gengetopt_args_info_purpose); |
145 |
|
146 |
if (strlen(gengetopt_args_info_usage) > 0) |
147 |
printf("\n%s\n", gengetopt_args_info_usage); |
148 |
|
149 |
printf("\n"); |
150 |
|
151 |
if (strlen(gengetopt_args_info_description) > 0) |
152 |
printf("%s\n\n", gengetopt_args_info_description); |
153 |
} |
154 |
|
155 |
void |
156 |
cmdline_parser_print_help (void) |
157 |
{ |
158 |
int i = 0; |
159 |
print_help_common(); |
160 |
while (gengetopt_args_info_help[i]) |
161 |
printf("%s\n", gengetopt_args_info_help[i++]); |
162 |
} |
163 |
|
164 |
void |
165 |
cmdline_parser_init (struct gengetopt_args_info *args_info) |
166 |
{ |
167 |
clear_given (args_info); |
168 |
clear_args (args_info); |
169 |
init_args_info (args_info); |
170 |
|
171 |
args_info->inputs = 0; |
172 |
args_info->inputs_num = 0; |
173 |
} |
174 |
|
175 |
void |
176 |
cmdline_parser_params_init(struct cmdline_parser_params *params) |
177 |
{ |
178 |
if (params) |
179 |
{ |
180 |
params->override = 0; |
181 |
params->initialize = 1; |
182 |
params->check_required = 1; |
183 |
params->check_ambiguity = 0; |
184 |
params->print_errors = 1; |
185 |
} |
186 |
} |
187 |
|
188 |
struct cmdline_parser_params * |
189 |
cmdline_parser_params_create(void) |
190 |
{ |
191 |
struct cmdline_parser_params *params = |
192 |
(struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params)); |
193 |
cmdline_parser_params_init(params); |
194 |
return params; |
195 |
} |
196 |
|
197 |
static void |
198 |
free_string_field (char **s) |
199 |
{ |
200 |
if (*s) |
201 |
{ |
202 |
free (*s); |
203 |
*s = 0; |
204 |
} |
205 |
} |
206 |
|
207 |
/** @brief generic value variable */ |
208 |
union generic_value { |
209 |
double double_arg; |
210 |
char *string_arg; |
211 |
const char *default_string_arg; |
212 |
}; |
213 |
|
214 |
/** @brief holds temporary values for multiple options */ |
215 |
struct generic_list |
216 |
{ |
217 |
union generic_value arg; |
218 |
char *orig; |
219 |
struct generic_list *next; |
220 |
}; |
221 |
|
222 |
/** |
223 |
* @brief add a node at the head of the list |
224 |
*/ |
225 |
static void add_node(struct generic_list **list) { |
226 |
struct generic_list *new_node = (struct generic_list *) malloc (sizeof (struct generic_list)); |
227 |
new_node->next = *list; |
228 |
*list = new_node; |
229 |
new_node->arg.string_arg = 0; |
230 |
new_node->orig = 0; |
231 |
} |
232 |
|
233 |
/** |
234 |
* The passed arg parameter is NOT set to 0 from this function |
235 |
*/ |
236 |
static void |
237 |
free_multiple_field(unsigned int len, void *arg, char ***orig) |
238 |
{ |
239 |
unsigned int i; |
240 |
if (arg) { |
241 |
for (i = 0; i < len; ++i) |
242 |
{ |
243 |
free_string_field(&((*orig)[i])); |
244 |
} |
245 |
|
246 |
free (arg); |
247 |
free (*orig); |
248 |
*orig = 0; |
249 |
} |
250 |
} |
251 |
|
252 |
|
253 |
static void |
254 |
cmdline_parser_release (struct gengetopt_args_info *args_info) |
255 |
{ |
256 |
unsigned int i; |
257 |
free_string_field (&(args_info->output_arg)); |
258 |
free_string_field (&(args_info->output_orig)); |
259 |
free_string_field (&(args_info->latticeConstant_orig)); |
260 |
free_string_field (&(args_info->radius_orig)); |
261 |
free_string_field (&(args_info->length_orig)); |
262 |
free_multiple_field (args_info->shellRadius_given, (void *)(args_info->shellRadius_arg), &(args_info->shellRadius_orig)); |
263 |
args_info->shellRadius_arg = 0; |
264 |
free_multiple_field (args_info->molFraction_given, (void *)(args_info->molFraction_arg), &(args_info->molFraction_orig)); |
265 |
args_info->molFraction_arg = 0; |
266 |
free_string_field (&(args_info->vacancyPercent_orig)); |
267 |
free_string_field (&(args_info->vacancyInnerRadius_orig)); |
268 |
free_string_field (&(args_info->vacancyOuterRadius_orig)); |
269 |
|
270 |
|
271 |
for (i = 0; i < args_info->inputs_num; ++i) |
272 |
free (args_info->inputs [i]); |
273 |
|
274 |
if (args_info->inputs_num) |
275 |
free (args_info->inputs); |
276 |
|
277 |
clear_given (args_info); |
278 |
} |
279 |
|
280 |
|
281 |
static void |
282 |
write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[]) |
283 |
{ |
284 |
FIX_UNUSED (values); |
285 |
if (arg) { |
286 |
fprintf(outfile, "%s=\"%s\"\n", opt, arg); |
287 |
} else { |
288 |
fprintf(outfile, "%s\n", opt); |
289 |
} |
290 |
} |
291 |
|
292 |
static void |
293 |
write_multiple_into_file(FILE *outfile, int len, const char *opt, char **arg, const char *values[]) |
294 |
{ |
295 |
int i; |
296 |
|
297 |
for (i = 0; i < len; ++i) |
298 |
write_into_file(outfile, opt, (arg ? arg[i] : 0), values); |
299 |
} |
300 |
|
301 |
int |
302 |
cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info) |
303 |
{ |
304 |
int i = 0; |
305 |
|
306 |
if (!outfile) |
307 |
{ |
308 |
fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE); |
309 |
return EXIT_FAILURE; |
310 |
} |
311 |
|
312 |
if (args_info->help_given) |
313 |
write_into_file(outfile, "help", 0, 0 ); |
314 |
if (args_info->version_given) |
315 |
write_into_file(outfile, "version", 0, 0 ); |
316 |
if (args_info->output_given) |
317 |
write_into_file(outfile, "output", args_info->output_orig, 0); |
318 |
if (args_info->latticeConstant_given) |
319 |
write_into_file(outfile, "latticeConstant", args_info->latticeConstant_orig, 0); |
320 |
if (args_info->radius_given) |
321 |
write_into_file(outfile, "radius", args_info->radius_orig, 0); |
322 |
if (args_info->length_given) |
323 |
write_into_file(outfile, "length", args_info->length_orig, 0); |
324 |
if (args_info->ellipsoid_given) |
325 |
write_into_file(outfile, "ellipsoid", 0, 0 ); |
326 |
write_multiple_into_file(outfile, args_info->shellRadius_given, "shellRadius", args_info->shellRadius_orig, 0); |
327 |
write_multiple_into_file(outfile, args_info->molFraction_given, "molFraction", args_info->molFraction_orig, 0); |
328 |
if (args_info->vacancyPercent_given) |
329 |
write_into_file(outfile, "vacancyPercent", args_info->vacancyPercent_orig, 0); |
330 |
if (args_info->vacancyInnerRadius_given) |
331 |
write_into_file(outfile, "vacancyInnerRadius", args_info->vacancyInnerRadius_orig, 0); |
332 |
if (args_info->vacancyOuterRadius_given) |
333 |
write_into_file(outfile, "vacancyOuterRadius", args_info->vacancyOuterRadius_orig, 0); |
334 |
|
335 |
|
336 |
i = EXIT_SUCCESS; |
337 |
return i; |
338 |
} |
339 |
|
340 |
int |
341 |
cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info) |
342 |
{ |
343 |
FILE *outfile; |
344 |
int i = 0; |
345 |
|
346 |
outfile = fopen(filename, "w"); |
347 |
|
348 |
if (!outfile) |
349 |
{ |
350 |
fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename); |
351 |
return EXIT_FAILURE; |
352 |
} |
353 |
|
354 |
i = cmdline_parser_dump(outfile, args_info); |
355 |
fclose (outfile); |
356 |
|
357 |
return i; |
358 |
} |
359 |
|
360 |
void |
361 |
cmdline_parser_free (struct gengetopt_args_info *args_info) |
362 |
{ |
363 |
cmdline_parser_release (args_info); |
364 |
} |
365 |
|
366 |
/** @brief replacement of strdup, which is not standard */ |
367 |
char * |
368 |
gengetopt_strdup (const char *s) |
369 |
{ |
370 |
char *result = 0; |
371 |
if (!s) |
372 |
return result; |
373 |
|
374 |
result = (char*)malloc(strlen(s) + 1); |
375 |
if (result == (char*)0) |
376 |
return (char*)0; |
377 |
strcpy(result, s); |
378 |
return result; |
379 |
} |
380 |
|
381 |
static char * |
382 |
get_multiple_arg_token(const char *arg) |
383 |
{ |
384 |
const char *tok; |
385 |
char *ret; |
386 |
size_t len, num_of_escape, i, j; |
387 |
|
388 |
if (!arg) |
389 |
return 0; |
390 |
|
391 |
tok = strchr (arg, ','); |
392 |
num_of_escape = 0; |
393 |
|
394 |
/* make sure it is not escaped */ |
395 |
while (tok) |
396 |
{ |
397 |
if (*(tok-1) == '\\') |
398 |
{ |
399 |
/* find the next one */ |
400 |
tok = strchr (tok+1, ','); |
401 |
++num_of_escape; |
402 |
} |
403 |
else |
404 |
break; |
405 |
} |
406 |
|
407 |
if (tok) |
408 |
len = (size_t)(tok - arg + 1); |
409 |
else |
410 |
len = strlen (arg) + 1; |
411 |
|
412 |
len -= num_of_escape; |
413 |
|
414 |
ret = (char *) malloc (len); |
415 |
|
416 |
i = 0; |
417 |
j = 0; |
418 |
while (arg[i] && (j < len-1)) |
419 |
{ |
420 |
if (arg[i] == '\\' && |
421 |
arg[ i + 1 ] && |
422 |
arg[ i + 1 ] == ',') |
423 |
++i; |
424 |
|
425 |
ret[j++] = arg[i++]; |
426 |
} |
427 |
|
428 |
ret[len-1] = '\0'; |
429 |
|
430 |
return ret; |
431 |
} |
432 |
|
433 |
static const char * |
434 |
get_multiple_arg_token_next(const char *arg) |
435 |
{ |
436 |
const char *tok; |
437 |
|
438 |
if (!arg) |
439 |
return 0; |
440 |
|
441 |
tok = strchr (arg, ','); |
442 |
|
443 |
/* make sure it is not escaped */ |
444 |
while (tok) |
445 |
{ |
446 |
if (*(tok-1) == '\\') |
447 |
{ |
448 |
/* find the next one */ |
449 |
tok = strchr (tok+1, ','); |
450 |
} |
451 |
else |
452 |
break; |
453 |
} |
454 |
|
455 |
if (! tok || strlen(tok) == 1) |
456 |
return 0; |
457 |
|
458 |
return tok+1; |
459 |
} |
460 |
|
461 |
static int |
462 |
check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc); |
463 |
|
464 |
int |
465 |
check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc) |
466 |
{ |
467 |
int error = 0; |
468 |
|
469 |
if (option_given && (min > 0 || max > 0)) |
470 |
{ |
471 |
if (min > 0 && max > 0) |
472 |
{ |
473 |
if (min == max) |
474 |
{ |
475 |
/* specific occurrences */ |
476 |
if (option_given != (unsigned int) min) |
477 |
{ |
478 |
fprintf (stderr, "%s: %s option occurrences must be %d\n", |
479 |
prog_name, option_desc, min); |
480 |
error = 1; |
481 |
} |
482 |
} |
483 |
else if (option_given < (unsigned int) min |
484 |
|| option_given > (unsigned int) max) |
485 |
{ |
486 |
/* range occurrences */ |
487 |
fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n", |
488 |
prog_name, option_desc, min, max); |
489 |
error = 1; |
490 |
} |
491 |
} |
492 |
else if (min > 0) |
493 |
{ |
494 |
/* at least check */ |
495 |
if (option_given < min) |
496 |
{ |
497 |
fprintf (stderr, "%s: %s option occurrences must be at least %d\n", |
498 |
prog_name, option_desc, min); |
499 |
error = 1; |
500 |
} |
501 |
} |
502 |
else if (max > 0) |
503 |
{ |
504 |
/* at most check */ |
505 |
if (option_given > max) |
506 |
{ |
507 |
fprintf (stderr, "%s: %s option occurrences must be at most %d\n", |
508 |
prog_name, option_desc, max); |
509 |
error = 1; |
510 |
} |
511 |
} |
512 |
} |
513 |
|
514 |
return error; |
515 |
} |
516 |
int |
517 |
cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info) |
518 |
{ |
519 |
return cmdline_parser2 (argc, argv, args_info, 0, 1, 1); |
520 |
} |
521 |
|
522 |
int |
523 |
cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info, |
524 |
struct cmdline_parser_params *params) |
525 |
{ |
526 |
int result; |
527 |
result = cmdline_parser_internal (argc, argv, args_info, params, 0); |
528 |
|
529 |
if (result == EXIT_FAILURE) |
530 |
{ |
531 |
cmdline_parser_free (args_info); |
532 |
exit (EXIT_FAILURE); |
533 |
} |
534 |
|
535 |
return result; |
536 |
} |
537 |
|
538 |
int |
539 |
cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required) |
540 |
{ |
541 |
int result; |
542 |
struct cmdline_parser_params params; |
543 |
|
544 |
params.override = override; |
545 |
params.initialize = initialize; |
546 |
params.check_required = check_required; |
547 |
params.check_ambiguity = 0; |
548 |
params.print_errors = 1; |
549 |
|
550 |
result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0); |
551 |
|
552 |
if (result == EXIT_FAILURE) |
553 |
{ |
554 |
cmdline_parser_free (args_info); |
555 |
exit (EXIT_FAILURE); |
556 |
} |
557 |
|
558 |
return result; |
559 |
} |
560 |
|
561 |
int |
562 |
cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name) |
563 |
{ |
564 |
int result = EXIT_SUCCESS; |
565 |
|
566 |
if (cmdline_parser_required2(args_info, prog_name, 0) > 0) |
567 |
result = EXIT_FAILURE; |
568 |
|
569 |
if (result == EXIT_FAILURE) |
570 |
{ |
571 |
cmdline_parser_free (args_info); |
572 |
exit (EXIT_FAILURE); |
573 |
} |
574 |
|
575 |
return result; |
576 |
} |
577 |
|
578 |
int |
579 |
cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error) |
580 |
{ |
581 |
int error = 0; |
582 |
FIX_UNUSED (additional_error); |
583 |
|
584 |
/* checks for required options */ |
585 |
if (! args_info->output_given) |
586 |
{ |
587 |
fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : "")); |
588 |
error = 1; |
589 |
} |
590 |
|
591 |
if (! args_info->latticeConstant_given) |
592 |
{ |
593 |
fprintf (stderr, "%s: '--latticeConstant' option required%s\n", prog_name, (additional_error ? additional_error : "")); |
594 |
error = 1; |
595 |
} |
596 |
|
597 |
if (! args_info->radius_given) |
598 |
{ |
599 |
fprintf (stderr, "%s: '--radius' option required%s\n", prog_name, (additional_error ? additional_error : "")); |
600 |
error = 1; |
601 |
} |
602 |
|
603 |
if (! args_info->length_given) |
604 |
{ |
605 |
fprintf (stderr, "%s: '--length' option required%s\n", prog_name, (additional_error ? additional_error : "")); |
606 |
error = 1; |
607 |
} |
608 |
|
609 |
if (check_multiple_option_occurrences(prog_name, args_info->shellRadius_given, args_info->shellRadius_min, args_info->shellRadius_max, "'--shellRadius'")) |
610 |
error = 1; |
611 |
|
612 |
if (check_multiple_option_occurrences(prog_name, args_info->molFraction_given, args_info->molFraction_min, args_info->molFraction_max, "'--molFraction'")) |
613 |
error = 1; |
614 |
|
615 |
|
616 |
/* checks for dependences among options */ |
617 |
|
618 |
return error; |
619 |
} |
620 |
|
621 |
|
622 |
static char *package_name = 0; |
623 |
|
624 |
/** |
625 |
* @brief updates an option |
626 |
* @param field the generic pointer to the field to update |
627 |
* @param orig_field the pointer to the orig field |
628 |
* @param field_given the pointer to the number of occurrence of this option |
629 |
* @param prev_given the pointer to the number of occurrence already seen |
630 |
* @param value the argument for this option (if null no arg was specified) |
631 |
* @param possible_values the possible values for this option (if specified) |
632 |
* @param default_value the default value (in case the option only accepts fixed values) |
633 |
* @param arg_type the type of this option |
634 |
* @param check_ambiguity @see cmdline_parser_params.check_ambiguity |
635 |
* @param override @see cmdline_parser_params.override |
636 |
* @param no_free whether to free a possible previous value |
637 |
* @param multiple_option whether this is a multiple option |
638 |
* @param long_opt the corresponding long option |
639 |
* @param short_opt the corresponding short option (or '-' if none) |
640 |
* @param additional_error possible further error specification |
641 |
*/ |
642 |
static |
643 |
int update_arg(void *field, char **orig_field, |
644 |
unsigned int *field_given, unsigned int *prev_given, |
645 |
char *value, const char *possible_values[], |
646 |
const char *default_value, |
647 |
cmdline_parser_arg_type arg_type, |
648 |
int check_ambiguity, int override, |
649 |
int no_free, int multiple_option, |
650 |
const char *long_opt, char short_opt, |
651 |
const char *additional_error) |
652 |
{ |
653 |
char *stop_char = 0; |
654 |
const char *val = value; |
655 |
int found; |
656 |
char **string_field; |
657 |
FIX_UNUSED (field); |
658 |
|
659 |
stop_char = 0; |
660 |
found = 0; |
661 |
|
662 |
if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given))) |
663 |
{ |
664 |
if (short_opt != '-') |
665 |
fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", |
666 |
package_name, long_opt, short_opt, |
667 |
(additional_error ? additional_error : "")); |
668 |
else |
669 |
fprintf (stderr, "%s: `--%s' option given more than once%s\n", |
670 |
package_name, long_opt, |
671 |
(additional_error ? additional_error : "")); |
672 |
return 1; /* failure */ |
673 |
} |
674 |
|
675 |
FIX_UNUSED (default_value); |
676 |
|
677 |
if (field_given && *field_given && ! override) |
678 |
return 0; |
679 |
if (prev_given) |
680 |
(*prev_given)++; |
681 |
if (field_given) |
682 |
(*field_given)++; |
683 |
if (possible_values) |
684 |
val = possible_values[found]; |
685 |
|
686 |
switch(arg_type) { |
687 |
case ARG_FLAG: |
688 |
*((int *)field) = !*((int *)field); |
689 |
break; |
690 |
case ARG_DOUBLE: |
691 |
if (val) *((double *)field) = strtod (val, &stop_char); |
692 |
break; |
693 |
case ARG_STRING: |
694 |
if (val) { |
695 |
string_field = (char **)field; |
696 |
if (!no_free && *string_field) |
697 |
free (*string_field); /* free previous string */ |
698 |
*string_field = gengetopt_strdup (val); |
699 |
} |
700 |
break; |
701 |
default: |
702 |
break; |
703 |
}; |
704 |
|
705 |
/* check numeric conversion */ |
706 |
switch(arg_type) { |
707 |
case ARG_DOUBLE: |
708 |
if (val && !(stop_char && *stop_char == '\0')) { |
709 |
fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val); |
710 |
return 1; /* failure */ |
711 |
} |
712 |
break; |
713 |
default: |
714 |
; |
715 |
}; |
716 |
|
717 |
/* store the original value */ |
718 |
switch(arg_type) { |
719 |
case ARG_NO: |
720 |
case ARG_FLAG: |
721 |
break; |
722 |
default: |
723 |
if (value && orig_field) { |
724 |
if (no_free) { |
725 |
*orig_field = value; |
726 |
} else { |
727 |
if (*orig_field) |
728 |
free (*orig_field); /* free previous string */ |
729 |
*orig_field = gengetopt_strdup (value); |
730 |
} |
731 |
} |
732 |
}; |
733 |
|
734 |
return 0; /* OK */ |
735 |
} |
736 |
|
737 |
/** |
738 |
* @brief store information about a multiple option in a temporary list |
739 |
* @param list where to (temporarily) store multiple options |
740 |
*/ |
741 |
static |
742 |
int update_multiple_arg_temp(struct generic_list **list, |
743 |
unsigned int *prev_given, const char *val, |
744 |
const char *possible_values[], const char *default_value, |
745 |
cmdline_parser_arg_type arg_type, |
746 |
const char *long_opt, char short_opt, |
747 |
const char *additional_error) |
748 |
{ |
749 |
/* store single arguments */ |
750 |
char *multi_token; |
751 |
const char *multi_next; |
752 |
|
753 |
if (arg_type == ARG_NO) { |
754 |
(*prev_given)++; |
755 |
return 0; /* OK */ |
756 |
} |
757 |
|
758 |
multi_token = get_multiple_arg_token(val); |
759 |
multi_next = get_multiple_arg_token_next (val); |
760 |
|
761 |
while (1) |
762 |
{ |
763 |
add_node (list); |
764 |
if (update_arg((void *)&((*list)->arg), &((*list)->orig), 0, |
765 |
prev_given, multi_token, possible_values, default_value, |
766 |
arg_type, 0, 1, 1, 1, long_opt, short_opt, additional_error)) { |
767 |
if (multi_token) free(multi_token); |
768 |
return 1; /* failure */ |
769 |
} |
770 |
|
771 |
if (multi_next) |
772 |
{ |
773 |
multi_token = get_multiple_arg_token(multi_next); |
774 |
multi_next = get_multiple_arg_token_next (multi_next); |
775 |
} |
776 |
else |
777 |
break; |
778 |
} |
779 |
|
780 |
return 0; /* OK */ |
781 |
} |
782 |
|
783 |
/** |
784 |
* @brief free the passed list (including possible string argument) |
785 |
*/ |
786 |
static |
787 |
void free_list(struct generic_list *list, short string_arg) |
788 |
{ |
789 |
if (list) { |
790 |
struct generic_list *tmp; |
791 |
while (list) |
792 |
{ |
793 |
tmp = list; |
794 |
if (string_arg && list->arg.string_arg) |
795 |
free (list->arg.string_arg); |
796 |
if (list->orig) |
797 |
free (list->orig); |
798 |
list = list->next; |
799 |
free (tmp); |
800 |
} |
801 |
} |
802 |
} |
803 |
|
804 |
/** |
805 |
* @brief updates a multiple option starting from the passed list |
806 |
*/ |
807 |
static |
808 |
void update_multiple_arg(void *field, char ***orig_field, |
809 |
unsigned int field_given, unsigned int prev_given, union generic_value *default_value, |
810 |
cmdline_parser_arg_type arg_type, |
811 |
struct generic_list *list) |
812 |
{ |
813 |
int i; |
814 |
struct generic_list *tmp; |
815 |
|
816 |
if (prev_given && list) { |
817 |
*orig_field = (char **) realloc (*orig_field, (field_given + prev_given) * sizeof (char *)); |
818 |
|
819 |
switch(arg_type) { |
820 |
case ARG_DOUBLE: |
821 |
*((double **)field) = (double *)realloc (*((double **)field), (field_given + prev_given) * sizeof (double)); break; |
822 |
case ARG_STRING: |
823 |
*((char ***)field) = (char **)realloc (*((char ***)field), (field_given + prev_given) * sizeof (char *)); break; |
824 |
default: |
825 |
break; |
826 |
}; |
827 |
|
828 |
for (i = (prev_given - 1); i >= 0; --i) |
829 |
{ |
830 |
tmp = list; |
831 |
|
832 |
switch(arg_type) { |
833 |
case ARG_DOUBLE: |
834 |
(*((double **)field))[i + field_given] = tmp->arg.double_arg; break; |
835 |
case ARG_STRING: |
836 |
(*((char ***)field))[i + field_given] = tmp->arg.string_arg; break; |
837 |
default: |
838 |
break; |
839 |
} |
840 |
(*orig_field) [i + field_given] = list->orig; |
841 |
list = list->next; |
842 |
free (tmp); |
843 |
} |
844 |
} else { /* set the default value */ |
845 |
if (default_value && ! field_given) { |
846 |
switch(arg_type) { |
847 |
case ARG_DOUBLE: |
848 |
if (! *((double **)field)) { |
849 |
*((double **)field) = (double *)malloc (sizeof (double)); |
850 |
(*((double **)field))[0] = default_value->double_arg; |
851 |
} |
852 |
break; |
853 |
case ARG_STRING: |
854 |
if (! *((char ***)field)) { |
855 |
*((char ***)field) = (char **)malloc (sizeof (char *)); |
856 |
(*((char ***)field))[0] = gengetopt_strdup(default_value->string_arg); |
857 |
} |
858 |
break; |
859 |
default: break; |
860 |
} |
861 |
if (!(*orig_field)) { |
862 |
*orig_field = (char **) malloc (sizeof (char *)); |
863 |
(*orig_field)[0] = 0; |
864 |
} |
865 |
} |
866 |
} |
867 |
} |
868 |
|
869 |
int |
870 |
cmdline_parser_internal ( |
871 |
int argc, char **argv, struct gengetopt_args_info *args_info, |
872 |
struct cmdline_parser_params *params, const char *additional_error) |
873 |
{ |
874 |
int c; /* Character of the parsed option. */ |
875 |
|
876 |
struct generic_list * shellRadius_list = NULL; |
877 |
struct generic_list * molFraction_list = NULL; |
878 |
int error = 0; |
879 |
struct gengetopt_args_info local_args_info; |
880 |
|
881 |
int override; |
882 |
int initialize; |
883 |
int check_required; |
884 |
int check_ambiguity; |
885 |
|
886 |
package_name = argv[0]; |
887 |
|
888 |
override = params->override; |
889 |
initialize = params->initialize; |
890 |
check_required = params->check_required; |
891 |
check_ambiguity = params->check_ambiguity; |
892 |
|
893 |
if (initialize) |
894 |
cmdline_parser_init (args_info); |
895 |
|
896 |
cmdline_parser_init (&local_args_info); |
897 |
|
898 |
optarg = 0; |
899 |
optind = 0; |
900 |
opterr = params->print_errors; |
901 |
optopt = '?'; |
902 |
|
903 |
while (1) |
904 |
{ |
905 |
int option_index = 0; |
906 |
|
907 |
static struct option long_options[] = { |
908 |
{ "help", 0, NULL, 'h' }, |
909 |
{ "version", 0, NULL, 'V' }, |
910 |
{ "output", 1, NULL, 'o' }, |
911 |
{ "latticeConstant", 1, NULL, 0 }, |
912 |
{ "radius", 1, NULL, 0 }, |
913 |
{ "length", 1, NULL, 0 }, |
914 |
{ "ellipsoid", 0, NULL, 'e' }, |
915 |
{ "shellRadius", 1, NULL, 0 }, |
916 |
{ "molFraction", 1, NULL, 0 }, |
917 |
{ "vacancyPercent", 1, NULL, 0 }, |
918 |
{ "vacancyInnerRadius", 1, NULL, 0 }, |
919 |
{ "vacancyOuterRadius", 1, NULL, 0 }, |
920 |
{ 0, 0, 0, 0 } |
921 |
}; |
922 |
|
923 |
c = getopt_long (argc, argv, "hVo:e", long_options, &option_index); |
924 |
|
925 |
if (c == -1) break; /* Exit from `while (1)' loop. */ |
926 |
|
927 |
switch (c) |
928 |
{ |
929 |
case 'h': /* Print help and exit. */ |
930 |
cmdline_parser_print_help (); |
931 |
cmdline_parser_free (&local_args_info); |
932 |
exit (EXIT_SUCCESS); |
933 |
|
934 |
case 'V': /* Print version and exit. */ |
935 |
cmdline_parser_print_version (); |
936 |
cmdline_parser_free (&local_args_info); |
937 |
exit (EXIT_SUCCESS); |
938 |
|
939 |
case 'o': /* Output file name. */ |
940 |
|
941 |
|
942 |
if (update_arg( (void *)&(args_info->output_arg), |
943 |
&(args_info->output_orig), &(args_info->output_given), |
944 |
&(local_args_info.output_given), optarg, 0, 0, ARG_STRING, |
945 |
check_ambiguity, override, 0, 0, |
946 |
"output", 'o', |
947 |
additional_error)) |
948 |
goto failure; |
949 |
|
950 |
break; |
951 |
case 'e': /* Build an Ellipsoid instead of a rod.. */ |
952 |
|
953 |
|
954 |
if (update_arg((void *)&(args_info->ellipsoid_flag), 0, &(args_info->ellipsoid_given), |
955 |
&(local_args_info.ellipsoid_given), optarg, 0, 0, ARG_FLAG, |
956 |
check_ambiguity, override, 1, 0, "ellipsoid", 'e', |
957 |
additional_error)) |
958 |
goto failure; |
959 |
|
960 |
break; |
961 |
|
962 |
case 0: /* Long option with no short option */ |
963 |
/* Lattice spacing in Angstroms for cubic lattice.. */ |
964 |
if (strcmp (long_options[option_index].name, "latticeConstant") == 0) |
965 |
{ |
966 |
|
967 |
|
968 |
if (update_arg( (void *)&(args_info->latticeConstant_arg), |
969 |
&(args_info->latticeConstant_orig), &(args_info->latticeConstant_given), |
970 |
&(local_args_info.latticeConstant_given), optarg, 0, 0, ARG_DOUBLE, |
971 |
check_ambiguity, override, 0, 0, |
972 |
"latticeConstant", '-', |
973 |
additional_error)) |
974 |
goto failure; |
975 |
|
976 |
} |
977 |
/* Nanorod radius in Angstroms. */ |
978 |
else if (strcmp (long_options[option_index].name, "radius") == 0) |
979 |
{ |
980 |
|
981 |
|
982 |
if (update_arg( (void *)&(args_info->radius_arg), |
983 |
&(args_info->radius_orig), &(args_info->radius_given), |
984 |
&(local_args_info.radius_given), optarg, 0, 0, ARG_DOUBLE, |
985 |
check_ambiguity, override, 0, 0, |
986 |
"radius", '-', |
987 |
additional_error)) |
988 |
goto failure; |
989 |
|
990 |
} |
991 |
/* Nanorod length in Angstroms. */ |
992 |
else if (strcmp (long_options[option_index].name, "length") == 0) |
993 |
{ |
994 |
|
995 |
|
996 |
if (update_arg( (void *)&(args_info->length_arg), |
997 |
&(args_info->length_orig), &(args_info->length_given), |
998 |
&(local_args_info.length_given), optarg, 0, 0, ARG_DOUBLE, |
999 |
check_ambiguity, override, 0, 0, |
1000 |
"length", '-', |
1001 |
additional_error)) |
1002 |
goto failure; |
1003 |
|
1004 |
} |
1005 |
/* Radius containing within it only molecules of a specific component. Specified for each component > 1 in the template file.. */ |
1006 |
else if (strcmp (long_options[option_index].name, "shellRadius") == 0) |
1007 |
{ |
1008 |
|
1009 |
if (update_multiple_arg_temp(&shellRadius_list, |
1010 |
&(local_args_info.shellRadius_given), optarg, 0, 0, ARG_DOUBLE, |
1011 |
"shellRadius", '-', |
1012 |
additional_error)) |
1013 |
goto failure; |
1014 |
|
1015 |
} |
1016 |
/* Builds a multi-component random alloy nanoparticle. A mole Fraction must be specified for each component > 1 in the template file.. */ |
1017 |
else if (strcmp (long_options[option_index].name, "molFraction") == 0) |
1018 |
{ |
1019 |
|
1020 |
if (update_multiple_arg_temp(&molFraction_list, |
1021 |
&(local_args_info.molFraction_given), optarg, 0, 0, ARG_DOUBLE, |
1022 |
"molFraction", '-', |
1023 |
additional_error)) |
1024 |
goto failure; |
1025 |
|
1026 |
} |
1027 |
/* Percentage of atoms to remove from within vacancy range. */ |
1028 |
else if (strcmp (long_options[option_index].name, "vacancyPercent") == 0) |
1029 |
{ |
1030 |
|
1031 |
|
1032 |
if (update_arg( (void *)&(args_info->vacancyPercent_arg), |
1033 |
&(args_info->vacancyPercent_orig), &(args_info->vacancyPercent_given), |
1034 |
&(local_args_info.vacancyPercent_given), optarg, 0, 0, ARG_DOUBLE, |
1035 |
check_ambiguity, override, 0, 0, |
1036 |
"vacancyPercent", '-', |
1037 |
additional_error)) |
1038 |
goto failure; |
1039 |
|
1040 |
} |
1041 |
/* Radius arround core-shell where vacancies should be located.. */ |
1042 |
else if (strcmp (long_options[option_index].name, "vacancyInnerRadius") == 0) |
1043 |
{ |
1044 |
|
1045 |
|
1046 |
if (update_arg( (void *)&(args_info->vacancyInnerRadius_arg), |
1047 |
&(args_info->vacancyInnerRadius_orig), &(args_info->vacancyInnerRadius_given), |
1048 |
&(local_args_info.vacancyInnerRadius_given), optarg, 0, 0, ARG_DOUBLE, |
1049 |
check_ambiguity, override, 0, 0, |
1050 |
"vacancyInnerRadius", '-', |
1051 |
additional_error)) |
1052 |
goto failure; |
1053 |
|
1054 |
} |
1055 |
/* Radius arround core-shell where vacancies should be located.. */ |
1056 |
else if (strcmp (long_options[option_index].name, "vacancyOuterRadius") == 0) |
1057 |
{ |
1058 |
|
1059 |
|
1060 |
if (update_arg( (void *)&(args_info->vacancyOuterRadius_arg), |
1061 |
&(args_info->vacancyOuterRadius_orig), &(args_info->vacancyOuterRadius_given), |
1062 |
&(local_args_info.vacancyOuterRadius_given), optarg, 0, 0, ARG_DOUBLE, |
1063 |
check_ambiguity, override, 0, 0, |
1064 |
"vacancyOuterRadius", '-', |
1065 |
additional_error)) |
1066 |
goto failure; |
1067 |
|
1068 |
} |
1069 |
|
1070 |
break; |
1071 |
case '?': /* Invalid option. */ |
1072 |
/* `getopt_long' already printed an error message. */ |
1073 |
goto failure; |
1074 |
|
1075 |
default: /* bug: option not considered. */ |
1076 |
fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); |
1077 |
abort (); |
1078 |
} /* switch */ |
1079 |
} /* while */ |
1080 |
|
1081 |
|
1082 |
update_multiple_arg((void *)&(args_info->shellRadius_arg), |
1083 |
&(args_info->shellRadius_orig), args_info->shellRadius_given, |
1084 |
local_args_info.shellRadius_given, 0, |
1085 |
ARG_DOUBLE, shellRadius_list); |
1086 |
update_multiple_arg((void *)&(args_info->molFraction_arg), |
1087 |
&(args_info->molFraction_orig), args_info->molFraction_given, |
1088 |
local_args_info.molFraction_given, 0, |
1089 |
ARG_DOUBLE, molFraction_list); |
1090 |
|
1091 |
args_info->shellRadius_given += local_args_info.shellRadius_given; |
1092 |
local_args_info.shellRadius_given = 0; |
1093 |
args_info->molFraction_given += local_args_info.molFraction_given; |
1094 |
local_args_info.molFraction_given = 0; |
1095 |
|
1096 |
if (check_required) |
1097 |
{ |
1098 |
error += cmdline_parser_required2 (args_info, argv[0], additional_error); |
1099 |
} |
1100 |
|
1101 |
cmdline_parser_release (&local_args_info); |
1102 |
|
1103 |
if ( error ) |
1104 |
return (EXIT_FAILURE); |
1105 |
|
1106 |
if (optind < argc) |
1107 |
{ |
1108 |
int i = 0 ; |
1109 |
int found_prog_name = 0; |
1110 |
/* whether program name, i.e., argv[0], is in the remaining args |
1111 |
(this may happen with some implementations of getopt, |
1112 |
but surely not with the one included by gengetopt) */ |
1113 |
|
1114 |
i = optind; |
1115 |
while (i < argc) |
1116 |
if (argv[i++] == argv[0]) { |
1117 |
found_prog_name = 1; |
1118 |
break; |
1119 |
} |
1120 |
i = 0; |
1121 |
|
1122 |
args_info->inputs_num = argc - optind - found_prog_name; |
1123 |
args_info->inputs = |
1124 |
(char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; |
1125 |
while (optind < argc) |
1126 |
if (argv[optind++] != argv[0]) |
1127 |
args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ; |
1128 |
} |
1129 |
|
1130 |
return 0; |
1131 |
|
1132 |
failure: |
1133 |
free_list (shellRadius_list, 0 ); |
1134 |
free_list (molFraction_list, 0 ); |
1135 |
|
1136 |
cmdline_parser_release (&local_args_info); |
1137 |
return (EXIT_FAILURE); |
1138 |
} |