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