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