OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
recenterCmd.cpp
1/*
2 File autogenerated by gengetopt version 2.22.6
3 generated with the following command:
4 /opt/local/bin/gengetopt -C --input=recenter.ggo --no-handle-error --include-getopt --show-required --unamed-opts --file-name=recenterCmd --c-extension=cpp --header-extension=hpp
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
25#include "recenterCmd.hpp"
26
27const char *gengetopt_args_info_purpose = "Moves all integrable objects in an OpenMD file so that the center of mass is at\nthe origin.";
28
29const char *gengetopt_args_info_usage = "Usage: recenter [OPTIONS]... [FILES]...";
30
31const char *gengetopt_args_info_versiontext = "";
32
33const char *gengetopt_args_info_description = "";
34
35const char *gengetopt_args_info_help[] = {
36 " -h, --help Print help and exit",
37 " -V, --version Print version and exit",
38 " -o, --output=STRING Output file name (mandatory)",
39 0
40};
41
42typedef enum {ARG_NO
43 , ARG_STRING
44} cmdline_parser_arg_type;
45
46static
47void clear_given (struct gengetopt_args_info *args_info);
48static
49void clear_args (struct gengetopt_args_info *args_info);
50
51static int
52cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
53 struct cmdline_parser_params *params, const char *additional_error);
54
55static int
56cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
57struct line_list
58{
59 char * string_arg;
60 struct line_list * next;
61};
62
63static struct line_list *cmd_line_list = 0;
64static struct line_list *cmd_line_list_tmp = 0;
65
66static void
67free_cmd_list(void)
68{
69 /* free the list of a previous call */
70 if (cmd_line_list)
71 {
72 while (cmd_line_list) {
73 cmd_line_list_tmp = cmd_line_list;
74 cmd_line_list = cmd_line_list->next;
75 free (cmd_line_list_tmp->string_arg);
76 free (cmd_line_list_tmp);
77 }
78 }
79}
80
81
82static char *
83gengetopt_strdup (const char *s);
84
85static
86void clear_given (struct gengetopt_args_info *args_info)
87{
88 args_info->help_given = 0 ;
89 args_info->version_given = 0 ;
90 args_info->output_given = 0 ;
91}
92
93static
94void clear_args (struct gengetopt_args_info *args_info)
95{
96 FIX_UNUSED (args_info);
97 args_info->output_arg = NULL;
98 args_info->output_orig = NULL;
99
100}
101
102static
103void init_args_info(struct gengetopt_args_info *args_info)
104{
105
106
107 args_info->help_help = gengetopt_args_info_help[0] ;
108 args_info->version_help = gengetopt_args_info_help[1] ;
109 args_info->output_help = gengetopt_args_info_help[2] ;
110
111}
112
113void
114cmdline_parser_print_version (void)
115{
116 printf ("%s %s\n",
119
120 if (strlen(gengetopt_args_info_versiontext) > 0)
121 printf("\n%s\n", gengetopt_args_info_versiontext);
122}
123
124static void print_help_common(void) {
125 cmdline_parser_print_version ();
126
127 if (strlen(gengetopt_args_info_purpose) > 0)
128 printf("\n%s\n", gengetopt_args_info_purpose);
129
130 if (strlen(gengetopt_args_info_usage) > 0)
131 printf("\n%s\n", gengetopt_args_info_usage);
132
133 printf("\n");
134
135 if (strlen(gengetopt_args_info_description) > 0)
136 printf("%s\n\n", gengetopt_args_info_description);
137}
138
139void
140cmdline_parser_print_help (void)
141{
142 int i = 0;
143 print_help_common();
144 while (gengetopt_args_info_help[i])
145 printf("%s\n", gengetopt_args_info_help[i++]);
146}
147
148void
149cmdline_parser_init (struct gengetopt_args_info *args_info)
150{
151 clear_given (args_info);
152 clear_args (args_info);
153 init_args_info (args_info);
154
155 args_info->inputs = 0;
156 args_info->inputs_num = 0;
157}
158
159void
160cmdline_parser_params_init(struct cmdline_parser_params *params)
161{
162 if (params)
163 {
164 params->override = 0;
165 params->initialize = 1;
166 params->check_required = 1;
167 params->check_ambiguity = 0;
168 params->print_errors = 1;
169 }
170}
171
174{
175 struct cmdline_parser_params *params =
176 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
178 return params;
179}
180
181static void
182free_string_field (char **s)
183{
184 if (*s)
185 {
186 free (*s);
187 *s = 0;
188 }
189}
190
191
192static void
193cmdline_parser_release (struct gengetopt_args_info *args_info)
194{
195 unsigned int i;
196 free_string_field (&(args_info->output_arg));
197 free_string_field (&(args_info->output_orig));
198
199
200 for (i = 0; i < args_info->inputs_num; ++i)
201 free (args_info->inputs [i]);
202
203 if (args_info->inputs_num)
204 free (args_info->inputs);
205
206 clear_given (args_info);
207}
208
209
210static void
211write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
212{
213 FIX_UNUSED (values);
214 if (arg) {
215 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
216 } else {
217 fprintf(outfile, "%s\n", opt);
218 }
219}
220
221
222int
223cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
224{
225 int i = 0;
226
227 if (!outfile)
228 {
229 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
230 return EXIT_FAILURE;
231 }
232
233 if (args_info->help_given)
234 write_into_file(outfile, "help", 0, 0 );
235 if (args_info->version_given)
236 write_into_file(outfile, "version", 0, 0 );
237 if (args_info->output_given)
238 write_into_file(outfile, "output", args_info->output_orig, 0);
239
240
241 i = EXIT_SUCCESS;
242 return i;
243}
244
245int
246cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
247{
248 FILE *outfile;
249 int i = 0;
250
251 outfile = fopen(filename, "w");
252
253 if (!outfile)
254 {
255 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
256 return EXIT_FAILURE;
257 }
258
259 i = cmdline_parser_dump(outfile, args_info);
260 fclose (outfile);
261
262 return i;
263}
264
265void
267{
268 cmdline_parser_release (args_info);
269}
270
271/** @brief replacement of strdup, which is not standard */
272char *
273gengetopt_strdup (const char *s)
274{
275 char *result = 0;
276 if (!s)
277 return result;
278
279 result = (char*)malloc(strlen(s) + 1);
280 if (result == (char*)0)
281 return (char*)0;
282 strcpy(result, s);
283 return result;
284}
285
286int
287cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
288{
289 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
290}
291
292int
293cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
294 struct cmdline_parser_params *params)
295{
296 int result;
297 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
298
299 return result;
300}
301
302int
303cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
304{
305 int result;
306 struct cmdline_parser_params params;
307
308 params.override = override;
309 params.initialize = initialize;
311 params.check_ambiguity = 0;
312 params.print_errors = 1;
313
314 result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
315
316 return result;
317}
318
319int
320cmdline_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 return result;
328}
329
330int
331cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
332{
333 int error_occurred = 0;
334 FIX_UNUSED (additional_error);
335
336 /* checks for required options */
337 if (! args_info->output_given)
338 {
339 fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
340 error_occurred = 1;
341 }
342
343
344 /* checks for dependences among options */
345
346 return error_occurred;
347}
348
349/*
350 * Extracted from the glibc source tree, version 2.3.6
351 *
352 * Licensed under the GPL as per the whole glibc source tree.
353 *
354 * This file was modified so that getopt_long can be called
355 * many times without risking previous memory to be spoiled.
356 *
357 * Modified by Andre Noll and Lorenzo Bettini for use in
358 * GNU gengetopt generated files.
359 *
360 */
361
362/*
363 * we must include anything we need since this file is not thought to be
364 * inserted in a file already using getopt.h
365 *
366 * Lorenzo
367 */
368
369struct option
370{
371 const char *name;
372 /* has_arg can't be an enum because some compilers complain about
373 type mismatches in all the code that assumes it is an int. */
374 int has_arg;
375 int *flag;
376 int val;
377};
378
379/* This version of `getopt' appears to the caller like standard Unix `getopt'
380 but it behaves differently for the user, since it allows the user
381 to intersperse the options with the other arguments.
382
383 As `getopt' works, it permutes the elements of ARGV so that,
384 when it is done, all the options precede everything else. Thus
385 all application programs are extended to handle flexible argument order.
386*/
387/*
388 If the field `flag' is not NULL, it points to a variable that is set
389 to the value given in the field `val' when the option is found, but
390 left unchanged if the option is not found.
391
392 To have a long-named option do something other than set an `int' to
393 a compiled-in constant, such as set a value from `custom_optarg', set the
394 option's `flag' field to zero and its `val' field to a nonzero
395 value (the equivalent single-letter option character, if there is
396 one). For long options that have a zero `flag' field, `getopt'
397 returns the contents of the `val' field. */
398
399/* Names for the values of the `has_arg' field of `struct option'. */
400#ifndef no_argument
401#define no_argument 0
402#endif
403
404#ifndef required_argument
405#define required_argument 1
406#endif
407
408#ifndef optional_argument
409#define optional_argument 2
410#endif
411
412struct custom_getopt_data {
413 /*
414 * These have exactly the same meaning as the corresponding global variables,
415 * except that they are used for the reentrant versions of getopt.
416 */
417 int custom_optind;
418 int custom_opterr;
419 int custom_optopt;
420 char *custom_optarg;
421
422 /* True if the internal members have been initialized. */
423 int initialized;
424
425 /*
426 * The next char to be scanned in the option-element in which the last option
427 * character we returned was found. This allows us to pick up the scan where
428 * we left off. If this is zero, or a null string, it means resume the scan by
429 * advancing to the next ARGV-element.
430 */
431 char *nextchar;
432
433 /*
434 * Describe the part of ARGV that contains non-options that have been skipped.
435 * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
436 * the index after the last of them.
437 */
438 int first_nonopt;
439 int last_nonopt;
440};
441
442/*
443 * the variables optarg, optind, opterr and optopt are renamed with
444 * the custom_ prefix so that they don't interfere with getopt ones.
445 *
446 * Moreover they're static so they are visible only from within the
447 * file where this very file will be included.
448 */
449
450/*
451 * For communication from `custom_getopt' to the caller. When `custom_getopt' finds an
452 * option that takes an argument, the argument value is returned here.
453 */
454static char *custom_optarg;
455
456/*
457 * Index in ARGV of the next element to be scanned. This is used for
458 * communication to and from the caller and for communication between
459 * successive calls to `custom_getopt'.
460 *
461 * On entry to `custom_getopt', 1 means this is the first call; initialize.
462 *
463 * When `custom_getopt' returns -1, this is the index of the first of the non-option
464 * elements that the caller should itself scan.
465 *
466 * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
467 * has been scanned so far.
468 *
469 * 1003.2 says this must be 1 before any call.
470 */
471static int custom_optind = 1;
472
473/*
474 * Callers store zero here to inhibit the error message for unrecognized
475 * options.
476 */
477static int custom_opterr = 1;
478
479/*
480 * Set to an option character which was unrecognized. This must be initialized
481 * on some systems to avoid linking in the system's own getopt implementation.
482 */
483static int custom_optopt = '?';
484
485/*
486 * Exchange two adjacent subsequences of ARGV. One subsequence is elements
487 * [first_nonopt,last_nonopt) which contains all the non-options that have been
488 * skipped so far. The other is elements [last_nonopt,custom_optind), which contains
489 * all the options processed since those non-options were skipped.
490 * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
491 * indices of the non-options in ARGV after they are moved.
492 */
493static void exchange(char **argv, struct custom_getopt_data *d)
494{
495 int bottom = d->first_nonopt;
496 int middle = d->last_nonopt;
497 int top = d->custom_optind;
498 char *tem;
499
500 /*
501 * Exchange the shorter segment with the far end of the longer segment.
502 * That puts the shorter segment into the right place. It leaves the
503 * longer segment in the right place overall, but it consists of two
504 * parts that need to be swapped next.
505 */
506 while (top > middle && middle > bottom) {
507 if (top - middle > middle - bottom) {
508 /* Bottom segment is the short one. */
509 int len = middle - bottom;
510 int i;
511
512 /* Swap it with the top part of the top segment. */
513 for (i = 0; i < len; i++) {
514 tem = argv[bottom + i];
515 argv[bottom + i] =
516 argv[top - (middle - bottom) + i];
517 argv[top - (middle - bottom) + i] = tem;
518 }
519 /* Exclude the moved bottom segment from further swapping. */
520 top -= len;
521 } else {
522 /* Top segment is the short one. */
523 int len = top - middle;
524 int i;
525
526 /* Swap it with the bottom part of the bottom segment. */
527 for (i = 0; i < len; i++) {
528 tem = argv[bottom + i];
529 argv[bottom + i] = argv[middle + i];
530 argv[middle + i] = tem;
531 }
532 /* Exclude the moved top segment from further swapping. */
533 bottom += len;
534 }
535 }
536 /* Update records for the slots the non-options now occupy. */
537 d->first_nonopt += (d->custom_optind - d->last_nonopt);
538 d->last_nonopt = d->custom_optind;
539}
540
541/* Initialize the internal data when the first call is made. */
542static void custom_getopt_initialize(struct custom_getopt_data *d)
543{
544 /*
545 * Start processing options with ARGV-element 1 (since ARGV-element 0
546 * is the program name); the sequence of previously skipped non-option
547 * ARGV-elements is empty.
548 */
549 d->first_nonopt = d->last_nonopt = d->custom_optind;
550 d->nextchar = NULL;
551 d->initialized = 1;
552}
553
554#define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
555
556/* return: zero: continue, nonzero: return given value to user */
557static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
558 struct custom_getopt_data *d)
559{
560 /*
561 * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
562 * moved back by the user (who may also have changed the arguments).
563 */
564 if (d->last_nonopt > d->custom_optind)
565 d->last_nonopt = d->custom_optind;
566 if (d->first_nonopt > d->custom_optind)
567 d->first_nonopt = d->custom_optind;
568 /*
569 * If we have just processed some options following some
570 * non-options, exchange them so that the options come first.
571 */
572 if (d->first_nonopt != d->last_nonopt &&
573 d->last_nonopt != d->custom_optind)
574 exchange((char **) argv, d);
575 else if (d->last_nonopt != d->custom_optind)
576 d->first_nonopt = d->custom_optind;
577 /*
578 * Skip any additional non-options and extend the range of
579 * non-options previously skipped.
580 */
581 while (d->custom_optind < argc && NONOPTION_P)
582 d->custom_optind++;
583 d->last_nonopt = d->custom_optind;
584 /*
585 * The special ARGV-element `--' means premature end of options. Skip
586 * it like a null option, then exchange with previous non-options as if
587 * it were an option, then skip everything else like a non-option.
588 */
589 if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
590 d->custom_optind++;
591 if (d->first_nonopt != d->last_nonopt
592 && d->last_nonopt != d->custom_optind)
593 exchange((char **) argv, d);
594 else if (d->first_nonopt == d->last_nonopt)
595 d->first_nonopt = d->custom_optind;
596 d->last_nonopt = argc;
597 d->custom_optind = argc;
598 }
599 /*
600 * If we have done all the ARGV-elements, stop the scan and back over
601 * any non-options that we skipped and permuted.
602 */
603 if (d->custom_optind == argc) {
604 /*
605 * Set the next-arg-index to point at the non-options that we
606 * previously skipped, so the caller will digest them.
607 */
608 if (d->first_nonopt != d->last_nonopt)
609 d->custom_optind = d->first_nonopt;
610 return -1;
611 }
612 /*
613 * If we have come to a non-option and did not permute it, either stop
614 * the scan or describe it to the caller and pass it by.
615 */
616 if (NONOPTION_P) {
617 d->custom_optarg = argv[d->custom_optind++];
618 return 1;
619 }
620 /*
621 * We have found another option-ARGV-element. Skip the initial
622 * punctuation.
623 */
624 d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
625 return 0;
626}
627
628/*
629 * Check whether the ARGV-element is a long option.
630 *
631 * If there's a long option "fubar" and the ARGV-element is "-fu", consider
632 * that an abbreviation of the long option, just like "--fu", and not "-f" with
633 * arg "u".
634 *
635 * This distinction seems to be the most useful approach.
636 *
637 */
638static int check_long_opt(int argc, char *const *argv, const char *optstring,
639 const struct option *longopts, int *longind,
640 int print_errors, struct custom_getopt_data *d)
641{
642 char *nameend;
643 const struct option *p;
644 const struct option *pfound = NULL;
645 int exact = 0;
646 int ambig = 0;
647 int indfound = -1;
648 int option_index;
649
650 for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
651 /* Do nothing. */ ;
652
653 /* Test all long options for either exact match or abbreviated matches */
654 for (p = longopts, option_index = 0; p->name; p++, option_index++)
655 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
656 if ((unsigned int) (nameend - d->nextchar)
657 == (unsigned int) strlen(p->name)) {
658 /* Exact match found. */
659 pfound = p;
660 indfound = option_index;
661 exact = 1;
662 break;
663 } else if (pfound == NULL) {
664 /* First nonexact match found. */
665 pfound = p;
666 indfound = option_index;
667 } else if (pfound->has_arg != p->has_arg
668 || pfound->flag != p->flag
669 || pfound->val != p->val)
670 /* Second or later nonexact match found. */
671 ambig = 1;
672 }
673 if (ambig && !exact) {
674 if (print_errors) {
675 fprintf(stderr,
676 "%s: option `%s' is ambiguous\n",
677 argv[0], argv[d->custom_optind]);
678 }
679 d->nextchar += strlen(d->nextchar);
680 d->custom_optind++;
681 d->custom_optopt = 0;
682 return '?';
683 }
684 if (pfound) {
685 option_index = indfound;
686 d->custom_optind++;
687 if (*nameend) {
688 if (pfound->has_arg != no_argument)
689 d->custom_optarg = nameend + 1;
690 else {
691 if (print_errors) {
692 if (argv[d->custom_optind - 1][1] == '-') {
693 /* --option */
694 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
695 argv[0], pfound->name);
696 } else {
697 /* +option or -option */
698 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
699 argv[0], argv[d->custom_optind - 1][0], pfound->name);
700 }
701
702 }
703 d->nextchar += strlen(d->nextchar);
704 d->custom_optopt = pfound->val;
705 return '?';
706 }
707 } else if (pfound->has_arg == required_argument) {
708 if (d->custom_optind < argc)
709 d->custom_optarg = argv[d->custom_optind++];
710 else {
711 if (print_errors) {
712 fprintf(stderr,
713 "%s: option `%s' requires an argument\n",
714 argv[0],
715 argv[d->custom_optind - 1]);
716 }
717 d->nextchar += strlen(d->nextchar);
718 d->custom_optopt = pfound->val;
719 return optstring[0] == ':' ? ':' : '?';
720 }
721 }
722 d->nextchar += strlen(d->nextchar);
723 if (longind != NULL)
724 *longind = option_index;
725 if (pfound->flag) {
726 *(pfound->flag) = pfound->val;
727 return 0;
728 }
729 return pfound->val;
730 }
731 /*
732 * Can't find it as a long option. If this is not getopt_long_only, or
733 * the option starts with '--' or is not a valid short option, then
734 * it's an error. Otherwise interpret it as a short option.
735 */
736 if (print_errors) {
737 if (argv[d->custom_optind][1] == '-') {
738 /* --option */
739 fprintf(stderr,
740 "%s: unrecognized option `--%s'\n",
741 argv[0], d->nextchar);
742 } else {
743 /* +option or -option */
744 fprintf(stderr,
745 "%s: unrecognized option `%c%s'\n",
746 argv[0], argv[d->custom_optind][0],
747 d->nextchar);
748 }
749 }
750 d->nextchar = (char *) "";
751 d->custom_optind++;
752 d->custom_optopt = 0;
753 return '?';
754}
755
756static int check_short_opt(int argc, char *const *argv, const char *optstring,
757 int print_errors, struct custom_getopt_data *d)
758{
759 char c = *d->nextchar++;
760 const char *temp = strchr(optstring, c);
761
762 /* Increment `custom_optind' when we start to process its last character. */
763 if (*d->nextchar == '\0')
764 ++d->custom_optind;
765 if (!temp || c == ':') {
766 if (print_errors)
767 fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
768
769 d->custom_optopt = c;
770 return '?';
771 }
772 if (temp[1] == ':') {
773 if (temp[2] == ':') {
774 /* This is an option that accepts an argument optionally. */
775 if (*d->nextchar != '\0') {
776 d->custom_optarg = d->nextchar;
777 d->custom_optind++;
778 } else
779 d->custom_optarg = NULL;
780 d->nextchar = NULL;
781 } else {
782 /* This is an option that requires an argument. */
783 if (*d->nextchar != '\0') {
784 d->custom_optarg = d->nextchar;
785 /*
786 * If we end this ARGV-element by taking the
787 * rest as an arg, we must advance to the next
788 * element now.
789 */
790 d->custom_optind++;
791 } else if (d->custom_optind == argc) {
792 if (print_errors) {
793 fprintf(stderr,
794 "%s: option requires an argument -- %c\n",
795 argv[0], c);
796 }
797 d->custom_optopt = c;
798 if (optstring[0] == ':')
799 c = ':';
800 else
801 c = '?';
802 } else
803 /*
804 * We already incremented `custom_optind' once;
805 * increment it again when taking next ARGV-elt
806 * as argument.
807 */
808 d->custom_optarg = argv[d->custom_optind++];
809 d->nextchar = NULL;
810 }
811 }
812 return c;
813}
814
815/*
816 * Scan elements of ARGV for option characters given in OPTSTRING.
817 *
818 * If an element of ARGV starts with '-', and is not exactly "-" or "--",
819 * then it is an option element. The characters of this element
820 * (aside from the initial '-') are option characters. If `getopt'
821 * is called repeatedly, it returns successively each of the option characters
822 * from each of the option elements.
823 *
824 * If `getopt' finds another option character, it returns that character,
825 * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
826 * resume the scan with the following option character or ARGV-element.
827 *
828 * If there are no more option characters, `getopt' returns -1.
829 * Then `custom_optind' is the index in ARGV of the first ARGV-element
830 * that is not an option. (The ARGV-elements have been permuted
831 * so that those that are not options now come last.)
832 *
833 * OPTSTRING is a string containing the legitimate option characters.
834 * If an option character is seen that is not listed in OPTSTRING,
835 * return '?' after printing an error message. If you set `custom_opterr' to
836 * zero, the error message is suppressed but we still return '?'.
837 *
838 * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
839 * so the following text in the same ARGV-element, or the text of the following
840 * ARGV-element, is returned in `custom_optarg'. Two colons mean an option that
841 * wants an optional arg; if there is text in the current ARGV-element,
842 * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
843 *
844 * If OPTSTRING starts with `-' or `+', it requests different methods of
845 * handling the non-option ARGV-elements.
846 * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
847 *
848 * Long-named options begin with `--' instead of `-'.
849 * Their names may be abbreviated as long as the abbreviation is unique
850 * or is an exact match for some defined option. If they have an
851 * argument, it follows the option name in the same ARGV-element, separated
852 * from the option name by a `=', or else the in next ARGV-element.
853 * When `getopt' finds a long-named option, it returns 0 if that option's
854 * `flag' field is nonzero, the value of the option's `val' field
855 * if the `flag' field is zero.
856 *
857 * The elements of ARGV aren't really const, because we permute them.
858 * But we pretend they're const in the prototype to be compatible
859 * with other systems.
860 *
861 * LONGOPTS is a vector of `struct option' terminated by an
862 * element containing a name which is zero.
863 *
864 * LONGIND returns the index in LONGOPT of the long-named option found.
865 * It is only valid when a long-named option has been found by the most
866 * recent call.
867 *
868 * Return the option character from OPTS just read. Return -1 when there are
869 * no more options. For unrecognized options, or options missing arguments,
870 * `custom_optopt' is set to the option letter, and '?' is returned.
871 *
872 * The OPTS string is a list of characters which are recognized option letters,
873 * optionally followed by colons, specifying that that letter takes an
874 * argument, to be placed in `custom_optarg'.
875 *
876 * If a letter in OPTS is followed by two colons, its argument is optional.
877 * This behavior is specific to the GNU `getopt'.
878 *
879 * The argument `--' causes premature termination of argument scanning,
880 * explicitly telling `getopt' that there are no more options. If OPTS begins
881 * with `--', then non-option arguments are treated as arguments to the option
882 * '\0'. This behavior is specific to the GNU `getopt'.
883 */
884
885static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
886 const struct option *longopts, int *longind,
887 struct custom_getopt_data *d)
888{
889 int ret, print_errors = d->custom_opterr;
890
891 if (optstring[0] == ':')
892 print_errors = 0;
893 if (argc < 1)
894 return -1;
895 d->custom_optarg = NULL;
896
897 /*
898 * This is a big difference with GNU getopt, since optind == 0
899 * means initialization while here 1 means first call.
900 */
901 if (d->custom_optind == 0 || !d->initialized) {
902 if (d->custom_optind == 0)
903 d->custom_optind = 1; /* Don't scan ARGV[0], the program name. */
904 custom_getopt_initialize(d);
905 }
906 if (d->nextchar == NULL || *d->nextchar == '\0') {
907 ret = shuffle_argv(argc, argv, longopts, d);
908 if (ret)
909 return ret;
910 }
911 if (longopts && (argv[d->custom_optind][1] == '-' ))
912 return check_long_opt(argc, argv, optstring, longopts,
913 longind, print_errors, d);
914 return check_short_opt(argc, argv, optstring, print_errors, d);
915}
916
917static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
918 const struct option *longopts, int *longind)
919{
920 int result;
921 /* Keep a global copy of all internal members of d */
922 static struct custom_getopt_data d;
923
924 d.custom_optind = custom_optind;
925 d.custom_opterr = custom_opterr;
926 result = getopt_internal_r(argc, argv, optstring, longopts,
927 longind, &d);
928 custom_optind = d.custom_optind;
929 custom_optarg = d.custom_optarg;
930 custom_optopt = d.custom_optopt;
931 return result;
932}
933
934static int custom_getopt_long (int argc, char *const *argv, const char *options,
935 const struct option *long_options, int *opt_index)
936{
937 return custom_getopt_internal(argc, argv, options, long_options,
938 opt_index);
939}
940
941
942static char *package_name = 0;
943
944/**
945 * @brief updates an option
946 * @param field the generic pointer to the field to update
947 * @param orig_field the pointer to the orig field
948 * @param field_given the pointer to the number of occurrence of this option
949 * @param prev_given the pointer to the number of occurrence already seen
950 * @param value the argument for this option (if null no arg was specified)
951 * @param possible_values the possible values for this option (if specified)
952 * @param default_value the default value (in case the option only accepts fixed values)
953 * @param arg_type the type of this option
954 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
955 * @param override @see cmdline_parser_params.override
956 * @param no_free whether to free a possible previous value
957 * @param multiple_option whether this is a multiple option
958 * @param long_opt the corresponding long option
959 * @param short_opt the corresponding short option (or '-' if none)
960 * @param additional_error possible further error specification
961 */
962static
963int update_arg(void *field, char **orig_field,
964 unsigned int *field_given, unsigned int *prev_given,
965 char *value, const char *possible_values[],
966 const char *default_value,
967 cmdline_parser_arg_type arg_type,
968 int check_ambiguity, int override,
969 int no_free, int multiple_option,
970 const char *long_opt, char short_opt,
971 const char *additional_error)
972{
973 char *stop_char = 0;
974 const char *val = value;
975 int found;
976 char **string_field;
977 FIX_UNUSED (field);
978 FIX_UNUSED (stop_char);
979
980 found = 0;
981
982 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
983 {
984 if (short_opt != '-')
985 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
986 package_name, long_opt, short_opt,
987 (additional_error ? additional_error : ""));
988 else
989 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
990 package_name, long_opt,
991 (additional_error ? additional_error : ""));
992 return 1; /* failure */
993 }
994
995 FIX_UNUSED (default_value);
996
997 if (field_given && *field_given && ! override)
998 return 0;
999 if (prev_given)
1000 (*prev_given)++;
1001 if (field_given)
1002 (*field_given)++;
1003 if (possible_values)
1004 val = possible_values[found];
1005
1006 switch(arg_type) {
1007 case ARG_STRING:
1008 if (val) {
1009 string_field = (char **)field;
1010 if (!no_free && *string_field)
1011 free (*string_field); /* free previous string */
1012 *string_field = gengetopt_strdup (val);
1013 }
1014 break;
1015 default:
1016 break;
1017 };
1018
1019
1020 /* store the original value */
1021 switch(arg_type) {
1022 case ARG_NO:
1023 break;
1024 default:
1025 if (value && orig_field) {
1026 if (no_free) {
1027 *orig_field = value;
1028 } else {
1029 if (*orig_field)
1030 free (*orig_field); /* free previous string */
1031 *orig_field = gengetopt_strdup (value);
1032 }
1033 }
1034 };
1035
1036 return 0; /* OK */
1037}
1038
1039
1040int
1041cmdline_parser_internal (
1042 int argc, char **argv, struct gengetopt_args_info *args_info,
1043 struct cmdline_parser_params *params, const char *additional_error)
1044{
1045 int c; /* Character of the parsed option. */
1046
1047 int error_occurred = 0;
1048 struct gengetopt_args_info local_args_info;
1049
1050 int override;
1051 int initialize;
1052 int check_required;
1053 int check_ambiguity;
1054
1055 char *optarg;
1056 int optind;
1057 int opterr;
1058 int optopt;
1059
1060 package_name = argv[0];
1061
1062 override = params->override;
1063 initialize = params->initialize;
1064 check_required = params->check_required;
1065 check_ambiguity = params->check_ambiguity;
1066
1067 if (initialize)
1068 cmdline_parser_init (args_info);
1069
1070 cmdline_parser_init (&local_args_info);
1071
1072 optarg = 0;
1073 optind = 0;
1074 opterr = params->print_errors;
1075 optopt = '?';
1076
1077 while (1)
1078 {
1079 int option_index = 0;
1080
1081 static struct option long_options[] = {
1082 { "help", 0, NULL, 'h' },
1083 { "version", 0, NULL, 'V' },
1084 { "output", 1, NULL, 'o' },
1085 { 0, 0, 0, 0 }
1086 };
1087
1088 custom_optarg = optarg;
1089 custom_optind = optind;
1090 custom_opterr = opterr;
1091 custom_optopt = optopt;
1092
1093 c = custom_getopt_long (argc, argv, "hVo:", long_options, &option_index);
1094
1095 optarg = custom_optarg;
1096 optind = custom_optind;
1097 opterr = custom_opterr;
1098 optopt = custom_optopt;
1099
1100 if (c == -1) break; /* Exit from `while (1)' loop. */
1101
1102 switch (c)
1103 {
1104 case 'h': /* Print help and exit. */
1105 cmdline_parser_print_help ();
1106 cmdline_parser_free (&local_args_info);
1107 exit (EXIT_SUCCESS);
1108
1109 case 'V': /* Print version and exit. */
1110 cmdline_parser_print_version ();
1111 cmdline_parser_free (&local_args_info);
1112 exit (EXIT_SUCCESS);
1113
1114 case 'o': /* Output file name. */
1115
1116
1117 if (update_arg( (void *)&(args_info->output_arg),
1118 &(args_info->output_orig), &(args_info->output_given),
1119 &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
1120 check_ambiguity, override, 0, 0,
1121 "output", 'o',
1122 additional_error))
1123 goto failure;
1124
1125 break;
1126
1127 case 0: /* Long option with no short option */
1128 case '?': /* Invalid option. */
1129 /* `getopt_long' already printed an error message. */
1130 goto failure;
1131
1132 default: /* bug: option not considered. */
1133 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1134 abort ();
1135 } /* switch */
1136 } /* while */
1137
1138
1139
1140 if (check_required)
1141 {
1142 error_occurred += cmdline_parser_required2 (args_info, argv[0], additional_error);
1143 }
1144
1145 cmdline_parser_release (&local_args_info);
1146
1147 if ( error_occurred )
1148 return (EXIT_FAILURE);
1149
1150 if (optind < argc)
1151 {
1152 int i = 0 ;
1153 int found_prog_name = 0;
1154 /* whether program name, i.e., argv[0], is in the remaining args
1155 (this may happen with some implementations of getopt,
1156 but surely not with the one included by gengetopt) */
1157
1158
1159 args_info->inputs_num = argc - optind - found_prog_name;
1160 args_info->inputs =
1161 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1162 while (optind < argc)
1163 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1164 }
1165
1166 return 0;
1167
1168failure:
1169
1170 cmdline_parser_release (&local_args_info);
1171 return (EXIT_FAILURE);
1172}
1173
1174#ifndef CONFIG_FILE_LINE_SIZE
1175#define CONFIG_FILE_LINE_SIZE 2048
1176#endif
1177#define ADDITIONAL_ERROR " in configuration file "
1178
1179#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1180/* 3 is for "--" and "=" */
1181
1182static int
1183_cmdline_parser_configfile (const char *filename, int *my_argc)
1184{
1185 FILE* file;
1186 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
1187 char linebuf[CONFIG_FILE_LINE_SIZE];
1188 int line_num = 0;
1189 int result = 0, equal;
1190 char *fopt, *farg;
1191 char *str_index;
1192 size_t len, next_token;
1193 char delimiter;
1194
1195 if ((file = fopen(filename, "r")) == 0)
1196 {
1197 fprintf (stderr, "%s: Error opening configuration file '%s'\n",
1198 CMDLINE_PARSER_PACKAGE, filename);
1199 return EXIT_FAILURE;
1200 }
1201
1202 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0)
1203 {
1204 ++line_num;
1205 my_argv[0] = '\0';
1206 len = strlen(linebuf);
1207 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
1208 {
1209 fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
1210 CMDLINE_PARSER_PACKAGE, filename, line_num);
1211 result = EXIT_FAILURE;
1212 break;
1213 }
1214
1215 /* find first non-whitespace character in the line */
1216 next_token = strspn (linebuf, " \t\r\n");
1217 str_index = linebuf + next_token;
1218
1219 if ( str_index[0] == '\0' || str_index[0] == '#')
1220 continue; /* empty line or comment line is skipped */
1221
1222 fopt = str_index;
1223
1224 /* truncate fopt at the end of the first non-valid character */
1225 next_token = strcspn (fopt, " \t\r\n=");
1226
1227 if (fopt[next_token] == '\0') /* the line is over */
1228 {
1229 farg = 0;
1230 equal = 0;
1231 goto noarg;
1232 }
1233
1234 /* remember if equal sign is present */
1235 equal = (fopt[next_token] == '=');
1236 fopt[next_token++] = '\0';
1237
1238 /* advance pointers to the next token after the end of fopt */
1239 next_token += strspn (fopt + next_token, " \t\r\n");
1240
1241 /* check for the presence of equal sign, and if so, skip it */
1242 if ( !equal )
1243 if ((equal = (fopt[next_token] == '=')))
1244 {
1245 next_token++;
1246 next_token += strspn (fopt + next_token, " \t\r\n");
1247 }
1248 str_index += next_token;
1249
1250 /* find argument */
1251 farg = str_index;
1252 if ( farg[0] == '\"' || farg[0] == '\'' )
1253 { /* quoted argument */
1254 str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
1255 if (! str_index)
1256 {
1257 fprintf
1258 (stderr,
1259 "%s:%s:%d: unterminated string in configuration file\n",
1260 CMDLINE_PARSER_PACKAGE, filename, line_num);
1261 result = EXIT_FAILURE;
1262 break;
1263 }
1264 }
1265 else
1266 { /* read up the remaining part up to a delimiter */
1267 next_token = strcspn (farg, " \t\r\n#\'\"");
1268 str_index += next_token;
1269 }
1270
1271 /* truncate farg at the delimiter and store it for further check */
1272 delimiter = *str_index, *str_index++ = '\0';
1273
1274 /* everything but comment is illegal at the end of line */
1275 if (delimiter != '\0' && delimiter != '#')
1276 {
1277 str_index += strspn(str_index, " \t\r\n");
1278 if (*str_index != '\0' && *str_index != '#')
1279 {
1280 fprintf
1281 (stderr,
1282 "%s:%s:%d: malformed string in configuration file\n",
1283 CMDLINE_PARSER_PACKAGE, filename, line_num);
1284 result = EXIT_FAILURE;
1285 break;
1286 }
1287 }
1288
1289 noarg:
1290 if (!strcmp(fopt,"include")) {
1291 if (farg && *farg) {
1292 result = _cmdline_parser_configfile(farg, my_argc);
1293 } else {
1294 fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1295 CMDLINE_PARSER_PACKAGE, filename, line_num);
1296 }
1297 continue;
1298 }
1299 len = strlen(fopt);
1300 strcat (my_argv, len > 1 ? "--" : "-");
1301 strcat (my_argv, fopt);
1302 if (len > 1 && ((farg && *farg) || equal))
1303 strcat (my_argv, "=");
1304 if (farg && *farg)
1305 strcat (my_argv, farg);
1306 ++(*my_argc);
1307
1308 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1309 cmd_line_list_tmp->next = cmd_line_list;
1310 cmd_line_list = cmd_line_list_tmp;
1311 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1312 } /* while */
1313
1314 if (file)
1315 fclose(file);
1316 return result;
1317}
1318
1319int
1320cmdline_parser_configfile (
1321 const char *filename,
1322 struct gengetopt_args_info *args_info,
1323 int override, int initialize, int check_required)
1324{
1325 struct cmdline_parser_params params;
1326
1327 params.override = override;
1328 params.initialize = initialize;
1330 params.check_ambiguity = 0;
1331 params.print_errors = 1;
1332
1333 return cmdline_parser_config_file (filename, args_info, &params);
1334}
1335
1336int
1337cmdline_parser_config_file (const char *filename,
1338 struct gengetopt_args_info *args_info,
1339 struct cmdline_parser_params *params)
1340{
1341 int i, result;
1342 int my_argc = 1;
1343 char **my_argv_arg;
1344 char *additional_error;
1345
1346 /* store the program name */
1347 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1348 cmd_line_list_tmp->next = cmd_line_list;
1349 cmd_line_list = cmd_line_list_tmp;
1350 cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE);
1351
1352 result = _cmdline_parser_configfile(filename, &my_argc);
1353
1354 if (result != EXIT_FAILURE) {
1355 my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
1356 cmd_line_list_tmp = cmd_line_list;
1357
1358 for (i = my_argc - 1; i >= 0; --i) {
1359 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1360 cmd_line_list_tmp = cmd_line_list_tmp->next;
1361 }
1362
1363 my_argv_arg[my_argc] = 0;
1364
1365 additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1366 strcpy (additional_error, ADDITIONAL_ERROR);
1367 strcat (additional_error, filename);
1368 result =
1369 cmdline_parser_internal (my_argc, my_argv_arg, args_info,
1370 params,
1371 additional_error);
1372
1373 free (additional_error);
1374 free (my_argv_arg);
1375 }
1376
1377 free_cmd_list();
1378 return result;
1379}
#define CMDLINE_PARSER_VERSION
the program version
#define CMDLINE_PARSER_PACKAGE_NAME
the complete program name (used for help and version)
#define CMDLINE_PARSER_PACKAGE
the program name (used for printing errors)
bool equal(const Polynomial< Real > &p1, const Polynomial< Real > &p2)
Tests if two polynomial have the same exponents.
The header file for the command line option parser generated by GNU Gengetopt version 2....
The additional parameters to pass to parser functions.
int print_errors
whether getopt_long should print an error message for a bad option (default 1)
int check_required
whether to check that all required options were provided (default 1)
int check_ambiguity
whether to check for options already specified in the option structure gengetopt_args_info (default 0...
int initialize
whether to initialize the option structure gengetopt_args_info (default 1)
int override
whether to override possibly already present options (default 0)
Where the command line options are stored.
unsigned int output_given
Whether output was given.
char * output_orig
output file name original value given at command line.
const char * output_help
output file name help description.
unsigned inputs_num
unamed options number
const char * help_help
Print help and exit help description.
char * output_arg
output file name.
char ** inputs
unamed options (options without names)
unsigned int help_given
Whether help was given.
unsigned int version_given
Whether version was given.
const char * version_help
Print version and exit help description.
int cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
Save the contents of the option struct into an already open FILE stream.
int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
The command line parser.
int cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
Save the contents of the option struct into a (text) file.
int cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
The command line parser (version with additional parameters - deprecated)
int cmdline_parser_required(struct gengetopt_args_info *args_info, const char *prog_name)
Checks that all the required options were specified.
struct cmdline_parser_params * cmdline_parser_params_create(void)
Allocates dynamically a cmdline_parser_params structure and initializes all its fields to their defau...
int cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info, struct cmdline_parser_params *params)
The command line parser (version with additional parameters)
void cmdline_parser_init(struct gengetopt_args_info *args_info)
Initializes the passed gengetopt_args_info structure's fields (also set default values for options th...
void cmdline_parser_free(struct gengetopt_args_info *args_info)
Deallocates the string fields of the gengetopt_args_info structure (but does not deallocate the struc...
void cmdline_parser_params_init(struct cmdline_parser_params *params)
Initializes all the fields a cmdline_parser_params structure to their default values.