21#define FIX_UNUSED(X) (void) (X)
27const char *gengetopt_args_info_purpose =
"Moves all integrable objects in an OpenMD file so that the center of mass is at\nthe origin.";
29const char *gengetopt_args_info_usage =
"Usage: recenter [OPTIONS]... [FILES]...";
31const char *gengetopt_args_info_versiontext =
"";
33const char *gengetopt_args_info_description =
"";
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)",
44} cmdline_parser_arg_type;
56cmdline_parser_required2 (
struct gengetopt_args_info *args_info,
const char *prog_name,
const char *additional_error);
60 struct line_list * next;
63static struct line_list *cmd_line_list = 0;
64static struct line_list *cmd_line_list_tmp = 0;
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);
83gengetopt_strdup (
const char *s);
96 FIX_UNUSED (args_info);
107 args_info->
help_help = gengetopt_args_info_help[0] ;
109 args_info->
output_help = gengetopt_args_info_help[2] ;
114cmdline_parser_print_version (
void)
120 if (strlen(gengetopt_args_info_versiontext) > 0)
121 printf(
"\n%s\n", gengetopt_args_info_versiontext);
124static void print_help_common(
void) {
125 cmdline_parser_print_version ();
127 if (strlen(gengetopt_args_info_purpose) > 0)
128 printf(
"\n%s\n", gengetopt_args_info_purpose);
130 if (strlen(gengetopt_args_info_usage) > 0)
131 printf(
"\n%s\n", gengetopt_args_info_usage);
135 if (strlen(gengetopt_args_info_description) > 0)
136 printf(
"%s\n\n", gengetopt_args_info_description);
140cmdline_parser_print_help (
void)
144 while (gengetopt_args_info_help[i])
145 printf(
"%s\n", gengetopt_args_info_help[i++]);
151 clear_given (args_info);
152 clear_args (args_info);
153 init_args_info (args_info);
182free_string_field (
char **s)
201 free (args_info->
inputs [i]);
206 clear_given (args_info);
211write_into_file(FILE *outfile,
const char *opt,
const char *arg,
const char *values[])
215 fprintf(outfile,
"%s=\"%s\"\n", opt, arg);
217 fprintf(outfile,
"%s\n", opt);
234 write_into_file(outfile,
"help", 0, 0 );
236 write_into_file(outfile,
"version", 0, 0 );
238 write_into_file(outfile,
"output", args_info->
output_orig, 0);
251 outfile = fopen(filename,
"w");
268 cmdline_parser_release (args_info);
273gengetopt_strdup (
const char *s)
279 result = (
char*)malloc(strlen(s) + 1);
280 if (result == (
char*)0)
297 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
314 result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0);
322 int result = EXIT_SUCCESS;
324 if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
325 result = EXIT_FAILURE;
331cmdline_parser_required2 (
struct gengetopt_args_info *args_info,
const char *prog_name,
const char *additional_error)
333 int error_occurred = 0;
334 FIX_UNUSED (additional_error);
339 fprintf (stderr,
"%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error :
""));
346 return error_occurred;
404#ifndef required_argument
405#define required_argument 1
408#ifndef optional_argument
409#define optional_argument 2
412struct custom_getopt_data {
454static char *custom_optarg;
471static int custom_optind = 1;
477static int custom_opterr = 1;
483static int custom_optopt =
'?';
493static void exchange(
char **argv,
struct custom_getopt_data *d)
495 int bottom = d->first_nonopt;
496 int middle = d->last_nonopt;
497 int top = d->custom_optind;
506 while (top > middle && middle > bottom) {
507 if (top - middle > middle - bottom) {
509 int len = middle - bottom;
513 for (i = 0; i < len; i++) {
514 tem = argv[bottom + i];
516 argv[top - (middle - bottom) + i];
517 argv[top - (middle - bottom) + i] = tem;
523 int len = top - middle;
527 for (i = 0; i < len; i++) {
528 tem = argv[bottom + i];
529 argv[bottom + i] = argv[middle + i];
530 argv[middle + i] = tem;
537 d->first_nonopt += (d->custom_optind - d->last_nonopt);
538 d->last_nonopt = d->custom_optind;
542static void custom_getopt_initialize(
struct custom_getopt_data *d)
549 d->first_nonopt = d->last_nonopt = d->custom_optind;
554#define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
557static int shuffle_argv(
int argc,
char *
const *argv,
const struct option *longopts,
558 struct custom_getopt_data *d)
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;
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;
581 while (d->custom_optind < argc && NONOPTION_P)
583 d->last_nonopt = d->custom_optind;
589 if (d->custom_optind != argc && !strcmp(argv[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;
603 if (d->custom_optind == argc) {
608 if (d->first_nonopt != d->last_nonopt)
609 d->custom_optind = d->first_nonopt;
617 d->custom_optarg = argv[d->custom_optind++];
624 d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] ==
'-'));
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)
643 const struct option *p;
644 const struct option *pfound = NULL;
650 for (nameend = d->nextchar; *nameend && *nameend !=
'='; nameend++)
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)) {
660 indfound = option_index;
663 }
else if (pfound == NULL) {
666 indfound = option_index;
667 }
else if (pfound->has_arg != p->has_arg
668 || pfound->flag != p->flag
669 || pfound->val != p->val)
673 if (ambig && !exact) {
676 "%s: option `%s' is ambiguous\n",
677 argv[0], argv[d->custom_optind]);
679 d->nextchar += strlen(d->nextchar);
681 d->custom_optopt = 0;
685 option_index = indfound;
688 if (pfound->has_arg != no_argument)
689 d->custom_optarg = nameend + 1;
692 if (argv[d->custom_optind - 1][1] ==
'-') {
694 fprintf(stderr,
"%s: option `--%s' doesn't allow an argument\n",
695 argv[0], pfound->name);
698 fprintf(stderr,
"%s: option `%c%s' doesn't allow an argument\n",
699 argv[0], argv[d->custom_optind - 1][0], pfound->name);
703 d->nextchar += strlen(d->nextchar);
704 d->custom_optopt = pfound->val;
707 }
else if (pfound->has_arg == required_argument) {
708 if (d->custom_optind < argc)
709 d->custom_optarg = argv[d->custom_optind++];
713 "%s: option `%s' requires an argument\n",
715 argv[d->custom_optind - 1]);
717 d->nextchar += strlen(d->nextchar);
718 d->custom_optopt = pfound->val;
719 return optstring[0] ==
':' ?
':' :
'?';
722 d->nextchar += strlen(d->nextchar);
724 *longind = option_index;
726 *(pfound->flag) = pfound->val;
737 if (argv[d->custom_optind][1] ==
'-') {
740 "%s: unrecognized option `--%s'\n",
741 argv[0], d->nextchar);
745 "%s: unrecognized option `%c%s'\n",
746 argv[0], argv[d->custom_optind][0],
750 d->nextchar = (
char *)
"";
752 d->custom_optopt = 0;
756static int check_short_opt(
int argc,
char *
const *argv,
const char *optstring,
757 int print_errors,
struct custom_getopt_data *d)
759 char c = *d->nextchar++;
760 const char *temp = strchr(optstring, c);
763 if (*d->nextchar ==
'\0')
765 if (!temp || c ==
':') {
767 fprintf(stderr,
"%s: invalid option -- %c\n", argv[0], c);
769 d->custom_optopt = c;
772 if (temp[1] ==
':') {
773 if (temp[2] ==
':') {
775 if (*d->nextchar !=
'\0') {
776 d->custom_optarg = d->nextchar;
779 d->custom_optarg = NULL;
783 if (*d->nextchar !=
'\0') {
784 d->custom_optarg = d->nextchar;
791 }
else if (d->custom_optind == argc) {
794 "%s: option requires an argument -- %c\n",
797 d->custom_optopt = c;
798 if (optstring[0] ==
':')
808 d->custom_optarg = argv[d->custom_optind++];
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)
889 int ret, print_errors = d->custom_opterr;
891 if (optstring[0] ==
':')
895 d->custom_optarg = NULL;
901 if (d->custom_optind == 0 || !d->initialized) {
902 if (d->custom_optind == 0)
903 d->custom_optind = 1;
904 custom_getopt_initialize(d);
906 if (d->nextchar == NULL || *d->nextchar ==
'\0') {
907 ret = shuffle_argv(argc, argv, longopts, d);
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);
917static int custom_getopt_internal(
int argc,
char *
const *argv,
const char *optstring,
918 const struct option *longopts,
int *longind)
922 static struct custom_getopt_data d;
924 d.custom_optind = custom_optind;
925 d.custom_opterr = custom_opterr;
926 result = getopt_internal_r(argc, argv, optstring, longopts,
928 custom_optind = d.custom_optind;
929 custom_optarg = d.custom_optarg;
930 custom_optopt = d.custom_optopt;
934static int custom_getopt_long (
int argc,
char *
const *argv,
const char *options,
935 const struct option *long_options,
int *opt_index)
937 return custom_getopt_internal(argc, argv, options, long_options,
942static char *package_name = 0;
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)
974 const char *val = value;
978 FIX_UNUSED (stop_char);
982 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
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 :
""));
989 fprintf (stderr,
"%s: `--%s' option given more than once%s\n",
990 package_name, long_opt,
991 (additional_error ? additional_error :
""));
995 FIX_UNUSED (default_value);
997 if (field_given && *field_given && !
override)
1003 if (possible_values)
1004 val = possible_values[found];
1009 string_field = (
char **)field;
1010 if (!no_free && *string_field)
1011 free (*string_field);
1012 *string_field = gengetopt_strdup (val);
1025 if (value && orig_field) {
1027 *orig_field = value;
1031 *orig_field = gengetopt_strdup (value);
1041cmdline_parser_internal (
1047 int error_occurred = 0;
1053 int check_ambiguity;
1060 package_name = argv[0];
1079 int option_index = 0;
1081 static struct option long_options[] = {
1082 {
"help", 0, NULL,
'h' },
1083 {
"version", 0, NULL,
'V' },
1084 {
"output", 1, NULL,
'o' },
1088 custom_optarg = optarg;
1089 custom_optind = optind;
1090 custom_opterr = opterr;
1091 custom_optopt = optopt;
1093 c = custom_getopt_long (argc, argv,
"hVo:", long_options, &option_index);
1095 optarg = custom_optarg;
1096 optind = custom_optind;
1097 opterr = custom_opterr;
1098 optopt = custom_optopt;
1105 cmdline_parser_print_help ();
1106 cmdline_parser_free (&local_args_info);
1107 exit (EXIT_SUCCESS);
1110 cmdline_parser_print_version ();
1111 cmdline_parser_free (&local_args_info);
1112 exit (EXIT_SUCCESS);
1117 if (update_arg( (
void *)&(args_info->
output_arg),
1119 &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
1120 check_ambiguity,
override, 0, 0,
1133 fprintf (stderr,
"%s: option unknown: %c%s\n",
CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error :
""));
1142 error_occurred += cmdline_parser_required2 (args_info, argv[0], additional_error);
1145 cmdline_parser_release (&local_args_info);
1147 if ( error_occurred )
1148 return (EXIT_FAILURE);
1153 int found_prog_name = 0;
1159 args_info->
inputs_num = argc - optind - found_prog_name;
1161 (
char **)(malloc ((args_info->
inputs_num)*
sizeof(
char *))) ;
1162 while (optind < argc)
1163 args_info->
inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1170 cmdline_parser_release (&local_args_info);
1171 return (EXIT_FAILURE);
1174#ifndef CONFIG_FILE_LINE_SIZE
1175#define CONFIG_FILE_LINE_SIZE 2048
1177#define ADDITIONAL_ERROR " in configuration file "
1179#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1183_cmdline_parser_configfile (
const char *filename,
int *my_argc)
1186 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
1187 char linebuf[CONFIG_FILE_LINE_SIZE];
1189 int result = 0,
equal;
1192 size_t len, next_token;
1195 if ((file = fopen(filename,
"r")) == 0)
1197 fprintf (stderr,
"%s: Error opening configuration file '%s'\n",
1199 return EXIT_FAILURE;
1202 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0)
1206 len = strlen(linebuf);
1207 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
1209 fprintf (stderr,
"%s:%s:%d: Line too long in configuration file\n",
1211 result = EXIT_FAILURE;
1216 next_token = strspn (linebuf,
" \t\r\n");
1217 str_index = linebuf + next_token;
1219 if ( str_index[0] ==
'\0' || str_index[0] ==
'#')
1225 next_token = strcspn (fopt,
" \t\r\n=");
1227 if (fopt[next_token] ==
'\0')
1235 equal = (fopt[next_token] ==
'=');
1236 fopt[next_token++] =
'\0';
1239 next_token += strspn (fopt + next_token,
" \t\r\n");
1243 if ((equal = (fopt[next_token] ==
'=')))
1246 next_token += strspn (fopt + next_token,
" \t\r\n");
1248 str_index += next_token;
1252 if ( farg[0] ==
'\"' || farg[0] ==
'\'' )
1254 str_index = strchr (++farg, str_index[0] );
1259 "%s:%s:%d: unterminated string in configuration file\n",
1261 result = EXIT_FAILURE;
1267 next_token = strcspn (farg,
" \t\r\n#\'\"");
1268 str_index += next_token;
1272 delimiter = *str_index, *str_index++ =
'\0';
1275 if (delimiter !=
'\0' && delimiter !=
'#')
1277 str_index += strspn(str_index,
" \t\r\n");
1278 if (*str_index !=
'\0' && *str_index !=
'#')
1282 "%s:%s:%d: malformed string in configuration file\n",
1284 result = EXIT_FAILURE;
1290 if (!strcmp(fopt,
"include")) {
1291 if (farg && *farg) {
1292 result = _cmdline_parser_configfile(farg, my_argc);
1294 fprintf(stderr,
"%s:%s:%d: include requires a filename argument.\n",
1300 strcat (my_argv, len > 1 ?
"--" :
"-");
1301 strcat (my_argv, fopt);
1302 if (len > 1 && ((farg && *farg) || equal))
1303 strcat (my_argv,
"=");
1305 strcat (my_argv, farg);
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);
1320cmdline_parser_configfile (
1321 const char *filename,
1323 int override,
int initialize,
int check_required)
1333 return cmdline_parser_config_file (filename, args_info, ¶ms);
1337cmdline_parser_config_file (
const char *filename,
1344 char *additional_error;
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;
1352 result = _cmdline_parser_configfile(filename, &my_argc);
1354 if (result != EXIT_FAILURE) {
1355 my_argv_arg = (
char **) malloc((my_argc+1) *
sizeof(
char *));
1356 cmd_line_list_tmp = cmd_line_list;
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;
1363 my_argv_arg[my_argc] = 0;
1365 additional_error = (
char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1366 strcpy (additional_error, ADDITIONAL_ERROR);
1367 strcat (additional_error, filename);
1369 cmdline_parser_internal (my_argc, my_argv_arg, args_info,
1373 free (additional_error);
#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.