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