1 |
/* |
2 |
File autogenerated by gengetopt version 2.22.4 |
3 |
generated with the following command: |
4 |
gengetopt --file-name=icosahedralBuilderCmd --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 "icosahedralBuilderCmd.h" |
27 |
|
28 |
const char *gengetopt_args_info_purpose = "Builds icosahedral nanoparticles and outputs an OpenMD startup file"; |
29 |
|
30 |
const char *gengetopt_args_info_usage = "Usage: icosahedralBuilder [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 |
" -d, --latticeConstant=DOUBLE Lattice spacing in Angstroms for cubic lattice.", |
39 |
" -n, --shells=INT Nanoparticle shells", |
40 |
0 |
41 |
}; |
42 |
|
43 |
typedef enum {ARG_NO |
44 |
, ARG_STRING |
45 |
, ARG_INT |
46 |
, ARG_DOUBLE |
47 |
} cmdline_parser_arg_type; |
48 |
|
49 |
static |
50 |
void clear_given (struct gengetopt_args_info *args_info); |
51 |
static |
52 |
void clear_args (struct gengetopt_args_info *args_info); |
53 |
|
54 |
static int |
55 |
cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info, |
56 |
struct cmdline_parser_params *params, const char *additional_error); |
57 |
|
58 |
static int |
59 |
cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error); |
60 |
|
61 |
static char * |
62 |
gengetopt_strdup (const char *s); |
63 |
|
64 |
static |
65 |
void clear_given (struct gengetopt_args_info *args_info) |
66 |
{ |
67 |
args_info->help_given = 0 ; |
68 |
args_info->version_given = 0 ; |
69 |
args_info->output_given = 0 ; |
70 |
args_info->latticeConstant_given = 0 ; |
71 |
args_info->shells_given = 0 ; |
72 |
} |
73 |
|
74 |
static |
75 |
void clear_args (struct gengetopt_args_info *args_info) |
76 |
{ |
77 |
FIX_UNUSED (args_info); |
78 |
args_info->output_arg = NULL; |
79 |
args_info->output_orig = NULL; |
80 |
args_info->latticeConstant_orig = NULL; |
81 |
args_info->shells_orig = NULL; |
82 |
|
83 |
} |
84 |
|
85 |
static |
86 |
void init_args_info(struct gengetopt_args_info *args_info) |
87 |
{ |
88 |
|
89 |
|
90 |
args_info->help_help = gengetopt_args_info_help[0] ; |
91 |
args_info->version_help = gengetopt_args_info_help[1] ; |
92 |
args_info->output_help = gengetopt_args_info_help[2] ; |
93 |
args_info->latticeConstant_help = gengetopt_args_info_help[3] ; |
94 |
args_info->shells_help = gengetopt_args_info_help[4] ; |
95 |
|
96 |
} |
97 |
|
98 |
void |
99 |
cmdline_parser_print_version (void) |
100 |
{ |
101 |
printf ("%s %s\n", |
102 |
(strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE), |
103 |
CMDLINE_PARSER_VERSION); |
104 |
} |
105 |
|
106 |
static void print_help_common(void) { |
107 |
cmdline_parser_print_version (); |
108 |
|
109 |
if (strlen(gengetopt_args_info_purpose) > 0) |
110 |
printf("\n%s\n", gengetopt_args_info_purpose); |
111 |
|
112 |
if (strlen(gengetopt_args_info_usage) > 0) |
113 |
printf("\n%s\n", gengetopt_args_info_usage); |
114 |
|
115 |
printf("\n"); |
116 |
|
117 |
if (strlen(gengetopt_args_info_description) > 0) |
118 |
printf("%s\n\n", gengetopt_args_info_description); |
119 |
} |
120 |
|
121 |
void |
122 |
cmdline_parser_print_help (void) |
123 |
{ |
124 |
int i = 0; |
125 |
print_help_common(); |
126 |
while (gengetopt_args_info_help[i]) |
127 |
printf("%s\n", gengetopt_args_info_help[i++]); |
128 |
} |
129 |
|
130 |
void |
131 |
cmdline_parser_init (struct gengetopt_args_info *args_info) |
132 |
{ |
133 |
clear_given (args_info); |
134 |
clear_args (args_info); |
135 |
init_args_info (args_info); |
136 |
|
137 |
args_info->inputs = 0; |
138 |
args_info->inputs_num = 0; |
139 |
} |
140 |
|
141 |
void |
142 |
cmdline_parser_params_init(struct cmdline_parser_params *params) |
143 |
{ |
144 |
if (params) |
145 |
{ |
146 |
params->override = 0; |
147 |
params->initialize = 1; |
148 |
params->check_required = 1; |
149 |
params->check_ambiguity = 0; |
150 |
params->print_errors = 1; |
151 |
} |
152 |
} |
153 |
|
154 |
struct cmdline_parser_params * |
155 |
cmdline_parser_params_create(void) |
156 |
{ |
157 |
struct cmdline_parser_params *params = |
158 |
(struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params)); |
159 |
cmdline_parser_params_init(params); |
160 |
return params; |
161 |
} |
162 |
|
163 |
static void |
164 |
free_string_field (char **s) |
165 |
{ |
166 |
if (*s) |
167 |
{ |
168 |
free (*s); |
169 |
*s = 0; |
170 |
} |
171 |
} |
172 |
|
173 |
|
174 |
static void |
175 |
cmdline_parser_release (struct gengetopt_args_info *args_info) |
176 |
{ |
177 |
unsigned int i; |
178 |
free_string_field (&(args_info->output_arg)); |
179 |
free_string_field (&(args_info->output_orig)); |
180 |
free_string_field (&(args_info->latticeConstant_orig)); |
181 |
free_string_field (&(args_info->shells_orig)); |
182 |
|
183 |
|
184 |
for (i = 0; i < args_info->inputs_num; ++i) |
185 |
free (args_info->inputs [i]); |
186 |
|
187 |
if (args_info->inputs_num) |
188 |
free (args_info->inputs); |
189 |
|
190 |
clear_given (args_info); |
191 |
} |
192 |
|
193 |
|
194 |
static void |
195 |
write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[]) |
196 |
{ |
197 |
FIX_UNUSED (values); |
198 |
if (arg) { |
199 |
fprintf(outfile, "%s=\"%s\"\n", opt, arg); |
200 |
} else { |
201 |
fprintf(outfile, "%s\n", opt); |
202 |
} |
203 |
} |
204 |
|
205 |
|
206 |
int |
207 |
cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info) |
208 |
{ |
209 |
int i = 0; |
210 |
|
211 |
if (!outfile) |
212 |
{ |
213 |
fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE); |
214 |
return EXIT_FAILURE; |
215 |
} |
216 |
|
217 |
if (args_info->help_given) |
218 |
write_into_file(outfile, "help", 0, 0 ); |
219 |
if (args_info->version_given) |
220 |
write_into_file(outfile, "version", 0, 0 ); |
221 |
if (args_info->output_given) |
222 |
write_into_file(outfile, "output", args_info->output_orig, 0); |
223 |
if (args_info->latticeConstant_given) |
224 |
write_into_file(outfile, "latticeConstant", args_info->latticeConstant_orig, 0); |
225 |
if (args_info->shells_given) |
226 |
write_into_file(outfile, "shells", args_info->shells_orig, 0); |
227 |
|
228 |
|
229 |
i = EXIT_SUCCESS; |
230 |
return i; |
231 |
} |
232 |
|
233 |
int |
234 |
cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info) |
235 |
{ |
236 |
FILE *outfile; |
237 |
int i = 0; |
238 |
|
239 |
outfile = fopen(filename, "w"); |
240 |
|
241 |
if (!outfile) |
242 |
{ |
243 |
fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename); |
244 |
return EXIT_FAILURE; |
245 |
} |
246 |
|
247 |
i = cmdline_parser_dump(outfile, args_info); |
248 |
fclose (outfile); |
249 |
|
250 |
return i; |
251 |
} |
252 |
|
253 |
void |
254 |
cmdline_parser_free (struct gengetopt_args_info *args_info) |
255 |
{ |
256 |
cmdline_parser_release (args_info); |
257 |
} |
258 |
|
259 |
/** @brief replacement of strdup, which is not standard */ |
260 |
char * |
261 |
gengetopt_strdup (const char *s) |
262 |
{ |
263 |
char *result = 0; |
264 |
if (!s) |
265 |
return result; |
266 |
|
267 |
result = (char*)malloc(strlen(s) + 1); |
268 |
if (result == (char*)0) |
269 |
return (char*)0; |
270 |
strcpy(result, s); |
271 |
return result; |
272 |
} |
273 |
|
274 |
int |
275 |
cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info) |
276 |
{ |
277 |
return cmdline_parser2 (argc, argv, args_info, 0, 1, 1); |
278 |
} |
279 |
|
280 |
int |
281 |
cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info, |
282 |
struct cmdline_parser_params *params) |
283 |
{ |
284 |
int result; |
285 |
result = cmdline_parser_internal (argc, argv, args_info, params, 0); |
286 |
|
287 |
if (result == EXIT_FAILURE) |
288 |
{ |
289 |
cmdline_parser_free (args_info); |
290 |
exit (EXIT_FAILURE); |
291 |
} |
292 |
|
293 |
return result; |
294 |
} |
295 |
|
296 |
int |
297 |
cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required) |
298 |
{ |
299 |
int result; |
300 |
struct cmdline_parser_params params; |
301 |
|
302 |
params.override = override; |
303 |
params.initialize = initialize; |
304 |
params.check_required = check_required; |
305 |
params.check_ambiguity = 0; |
306 |
params.print_errors = 1; |
307 |
|
308 |
result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0); |
309 |
|
310 |
if (result == EXIT_FAILURE) |
311 |
{ |
312 |
cmdline_parser_free (args_info); |
313 |
exit (EXIT_FAILURE); |
314 |
} |
315 |
|
316 |
return result; |
317 |
} |
318 |
|
319 |
int |
320 |
cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name) |
321 |
{ |
322 |
int result = EXIT_SUCCESS; |
323 |
|
324 |
if (cmdline_parser_required2(args_info, prog_name, 0) > 0) |
325 |
result = EXIT_FAILURE; |
326 |
|
327 |
if (result == EXIT_FAILURE) |
328 |
{ |
329 |
cmdline_parser_free (args_info); |
330 |
exit (EXIT_FAILURE); |
331 |
} |
332 |
|
333 |
return result; |
334 |
} |
335 |
|
336 |
int |
337 |
cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error) |
338 |
{ |
339 |
int error = 0; |
340 |
FIX_UNUSED (additional_error); |
341 |
|
342 |
/* checks for required options */ |
343 |
if (! args_info->output_given) |
344 |
{ |
345 |
fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : "")); |
346 |
error = 1; |
347 |
} |
348 |
|
349 |
if (! args_info->shells_given) |
350 |
{ |
351 |
fprintf (stderr, "%s: '--shells' ('-n') option required%s\n", prog_name, (additional_error ? additional_error : "")); |
352 |
error = 1; |
353 |
} |
354 |
|
355 |
|
356 |
/* checks for dependences among options */ |
357 |
|
358 |
return error; |
359 |
} |
360 |
|
361 |
|
362 |
static char *package_name = 0; |
363 |
|
364 |
/** |
365 |
* @brief updates an option |
366 |
* @param field the generic pointer to the field to update |
367 |
* @param orig_field the pointer to the orig field |
368 |
* @param field_given the pointer to the number of occurrence of this option |
369 |
* @param prev_given the pointer to the number of occurrence already seen |
370 |
* @param value the argument for this option (if null no arg was specified) |
371 |
* @param possible_values the possible values for this option (if specified) |
372 |
* @param default_value the default value (in case the option only accepts fixed values) |
373 |
* @param arg_type the type of this option |
374 |
* @param check_ambiguity @see cmdline_parser_params.check_ambiguity |
375 |
* @param override @see cmdline_parser_params.override |
376 |
* @param no_free whether to free a possible previous value |
377 |
* @param multiple_option whether this is a multiple option |
378 |
* @param long_opt the corresponding long option |
379 |
* @param short_opt the corresponding short option (or '-' if none) |
380 |
* @param additional_error possible further error specification |
381 |
*/ |
382 |
static |
383 |
int update_arg(void *field, char **orig_field, |
384 |
unsigned int *field_given, unsigned int *prev_given, |
385 |
char *value, const char *possible_values[], |
386 |
const char *default_value, |
387 |
cmdline_parser_arg_type arg_type, |
388 |
int check_ambiguity, int override, |
389 |
int no_free, int multiple_option, |
390 |
const char *long_opt, char short_opt, |
391 |
const char *additional_error) |
392 |
{ |
393 |
char *stop_char = 0; |
394 |
const char *val = value; |
395 |
int found; |
396 |
char **string_field; |
397 |
FIX_UNUSED (field); |
398 |
|
399 |
stop_char = 0; |
400 |
found = 0; |
401 |
|
402 |
if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given))) |
403 |
{ |
404 |
if (short_opt != '-') |
405 |
fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", |
406 |
package_name, long_opt, short_opt, |
407 |
(additional_error ? additional_error : "")); |
408 |
else |
409 |
fprintf (stderr, "%s: `--%s' option given more than once%s\n", |
410 |
package_name, long_opt, |
411 |
(additional_error ? additional_error : "")); |
412 |
return 1; /* failure */ |
413 |
} |
414 |
|
415 |
FIX_UNUSED (default_value); |
416 |
|
417 |
if (field_given && *field_given && ! override) |
418 |
return 0; |
419 |
if (prev_given) |
420 |
(*prev_given)++; |
421 |
if (field_given) |
422 |
(*field_given)++; |
423 |
if (possible_values) |
424 |
val = possible_values[found]; |
425 |
|
426 |
switch(arg_type) { |
427 |
case ARG_INT: |
428 |
if (val) *((int *)field) = strtol (val, &stop_char, 0); |
429 |
break; |
430 |
case ARG_DOUBLE: |
431 |
if (val) *((double *)field) = strtod (val, &stop_char); |
432 |
break; |
433 |
case ARG_STRING: |
434 |
if (val) { |
435 |
string_field = (char **)field; |
436 |
if (!no_free && *string_field) |
437 |
free (*string_field); /* free previous string */ |
438 |
*string_field = gengetopt_strdup (val); |
439 |
} |
440 |
break; |
441 |
default: |
442 |
break; |
443 |
}; |
444 |
|
445 |
/* check numeric conversion */ |
446 |
switch(arg_type) { |
447 |
case ARG_INT: |
448 |
case ARG_DOUBLE: |
449 |
if (val && !(stop_char && *stop_char == '\0')) { |
450 |
fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val); |
451 |
return 1; /* failure */ |
452 |
} |
453 |
break; |
454 |
default: |
455 |
; |
456 |
}; |
457 |
|
458 |
/* store the original value */ |
459 |
switch(arg_type) { |
460 |
case ARG_NO: |
461 |
break; |
462 |
default: |
463 |
if (value && orig_field) { |
464 |
if (no_free) { |
465 |
*orig_field = value; |
466 |
} else { |
467 |
if (*orig_field) |
468 |
free (*orig_field); /* free previous string */ |
469 |
*orig_field = gengetopt_strdup (value); |
470 |
} |
471 |
} |
472 |
}; |
473 |
|
474 |
return 0; /* OK */ |
475 |
} |
476 |
|
477 |
|
478 |
int |
479 |
cmdline_parser_internal ( |
480 |
int argc, char **argv, struct gengetopt_args_info *args_info, |
481 |
struct cmdline_parser_params *params, const char *additional_error) |
482 |
{ |
483 |
int c; /* Character of the parsed option. */ |
484 |
|
485 |
int error = 0; |
486 |
struct gengetopt_args_info local_args_info; |
487 |
|
488 |
int override; |
489 |
int initialize; |
490 |
int check_required; |
491 |
int check_ambiguity; |
492 |
|
493 |
package_name = argv[0]; |
494 |
|
495 |
override = params->override; |
496 |
initialize = params->initialize; |
497 |
check_required = params->check_required; |
498 |
check_ambiguity = params->check_ambiguity; |
499 |
|
500 |
if (initialize) |
501 |
cmdline_parser_init (args_info); |
502 |
|
503 |
cmdline_parser_init (&local_args_info); |
504 |
|
505 |
optarg = 0; |
506 |
optind = 0; |
507 |
opterr = params->print_errors; |
508 |
optopt = '?'; |
509 |
|
510 |
while (1) |
511 |
{ |
512 |
int option_index = 0; |
513 |
|
514 |
static struct option long_options[] = { |
515 |
{ "help", 0, NULL, 'h' }, |
516 |
{ "version", 0, NULL, 'V' }, |
517 |
{ "output", 1, NULL, 'o' }, |
518 |
{ "latticeConstant", 1, NULL, 'd' }, |
519 |
{ "shells", 1, NULL, 'n' }, |
520 |
{ 0, 0, 0, 0 } |
521 |
}; |
522 |
|
523 |
c = getopt_long (argc, argv, "hVo:d:n:", long_options, &option_index); |
524 |
|
525 |
if (c == -1) break; /* Exit from `while (1)' loop. */ |
526 |
|
527 |
switch (c) |
528 |
{ |
529 |
case 'h': /* Print help and exit. */ |
530 |
cmdline_parser_print_help (); |
531 |
cmdline_parser_free (&local_args_info); |
532 |
exit (EXIT_SUCCESS); |
533 |
|
534 |
case 'V': /* Print version and exit. */ |
535 |
cmdline_parser_print_version (); |
536 |
cmdline_parser_free (&local_args_info); |
537 |
exit (EXIT_SUCCESS); |
538 |
|
539 |
case 'o': /* Output file name. */ |
540 |
|
541 |
|
542 |
if (update_arg( (void *)&(args_info->output_arg), |
543 |
&(args_info->output_orig), &(args_info->output_given), |
544 |
&(local_args_info.output_given), optarg, 0, 0, ARG_STRING, |
545 |
check_ambiguity, override, 0, 0, |
546 |
"output", 'o', |
547 |
additional_error)) |
548 |
goto failure; |
549 |
|
550 |
break; |
551 |
case 'd': /* Lattice spacing in Angstroms for cubic lattice.. */ |
552 |
|
553 |
|
554 |
if (update_arg( (void *)&(args_info->latticeConstant_arg), |
555 |
&(args_info->latticeConstant_orig), &(args_info->latticeConstant_given), |
556 |
&(local_args_info.latticeConstant_given), optarg, 0, 0, ARG_DOUBLE, |
557 |
check_ambiguity, override, 0, 0, |
558 |
"latticeConstant", 'd', |
559 |
additional_error)) |
560 |
goto failure; |
561 |
|
562 |
break; |
563 |
case 'n': /* Nanoparticle shells. */ |
564 |
|
565 |
|
566 |
if (update_arg( (void *)&(args_info->shells_arg), |
567 |
&(args_info->shells_orig), &(args_info->shells_given), |
568 |
&(local_args_info.shells_given), optarg, 0, 0, ARG_INT, |
569 |
check_ambiguity, override, 0, 0, |
570 |
"shells", 'n', |
571 |
additional_error)) |
572 |
goto failure; |
573 |
|
574 |
break; |
575 |
|
576 |
case 0: /* Long option with no short option */ |
577 |
case '?': /* Invalid option. */ |
578 |
/* `getopt_long' already printed an error message. */ |
579 |
goto failure; |
580 |
|
581 |
default: /* bug: option not considered. */ |
582 |
fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); |
583 |
abort (); |
584 |
} /* switch */ |
585 |
} /* while */ |
586 |
|
587 |
|
588 |
|
589 |
if (check_required) |
590 |
{ |
591 |
error += cmdline_parser_required2 (args_info, argv[0], additional_error); |
592 |
} |
593 |
|
594 |
cmdline_parser_release (&local_args_info); |
595 |
|
596 |
if ( error ) |
597 |
return (EXIT_FAILURE); |
598 |
|
599 |
if (optind < argc) |
600 |
{ |
601 |
int i = 0 ; |
602 |
int found_prog_name = 0; |
603 |
/* whether program name, i.e., argv[0], is in the remaining args |
604 |
(this may happen with some implementations of getopt, |
605 |
but surely not with the one included by gengetopt) */ |
606 |
|
607 |
i = optind; |
608 |
while (i < argc) |
609 |
if (argv[i++] == argv[0]) { |
610 |
found_prog_name = 1; |
611 |
break; |
612 |
} |
613 |
i = 0; |
614 |
|
615 |
args_info->inputs_num = argc - optind - found_prog_name; |
616 |
args_info->inputs = |
617 |
(char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; |
618 |
while (optind < argc) |
619 |
if (argv[optind++] != argv[0]) |
620 |
args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ; |
621 |
} |
622 |
|
623 |
return 0; |
624 |
|
625 |
failure: |
626 |
|
627 |
cmdline_parser_release (&local_args_info); |
628 |
return (EXIT_FAILURE); |
629 |
} |