vide_public/mytools/zobovConf.c
Guilhem Lavaux 0ca75f1846 updated
2010-09-15 20:43:23 +02:00

1101 lines
31 KiB
C

/*
File autogenerated by gengetopt version 2.22.2
generated with the following command:
gengetopt -i showZobov.ggo -f zobovConf -a zobovConf_info -F zobovConf -C
The developers of gengetopt consider the fixed text that goes in all
gengetopt output files to be in the public domain:
we make no copyright claims on it.
*/
/* If we use autoconf. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef FIX_UNUSED
#define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
#endif
#include "getopt.h"
#include "zobovConf.h"
const char *zobovConf_info_purpose = "";
const char *zobovConf_info_usage = "Usage: showZobov [OPTIONS]...";
const char *zobovConf_info_description = "";
const char *zobovConf_info_help[] = {
" -h, --help Print help and exit",
" -V, --version Print version and exit",
" -d, --desc=STRING The description file name for the voids",
" -a, --adj=STRING Adjacent file name",
" -v, --void=STRING Void/zone bind filename",
" -i, --id=INT Void id to extract",
" -s, --sProba Sort against probability (default=off)",
" -l, --sVol Sort against volume (default=on)",
" -m, --mProba=DOUBLE Minimal probability to accept (default=`0.0')",
" -b, --ramsesDir=STRING Ramses base output directory",
" -r, --ramsesId=INT Ramses output id",
" -f, --configFile=STRING Configuration file",
" -q, --quiet Quiet mode (default=off)",
" -p, --move=STRING Move the center by (x,y,z) (default=`(0,0,0)')",
" --galax Output is galax particles (default=off)",
" -o, --output=STRING Output file (default=`voidDesc')",
" --interactive Go in interactive mode (default=off)",
0
};
typedef enum {ARG_NO
, ARG_FLAG
, ARG_STRING
, ARG_INT
, ARG_DOUBLE
} zobovConf_arg_type;
static
void clear_given (struct zobovConf_info *args_info);
static
void clear_args (struct zobovConf_info *args_info);
static int
zobovConf_internal (int argc, char * const *argv, struct zobovConf_info *args_info,
struct zobovConf_params *params, const char *additional_error);
static int
zobovConf_required2 (struct zobovConf_info *args_info, const char *prog_name, const char *additional_error);
struct line_list
{
char * string_arg;
struct line_list * next;
};
static struct line_list *cmd_line_list = 0;
static struct line_list *cmd_line_list_tmp = 0;
static void
free_cmd_list(void)
{
/* free the list of a previous call */
if (cmd_line_list)
{
while (cmd_line_list) {
cmd_line_list_tmp = cmd_line_list;
cmd_line_list = cmd_line_list->next;
free (cmd_line_list_tmp->string_arg);
free (cmd_line_list_tmp);
}
}
}
static char *
gengetopt_strdup (const char *s);
static
void clear_given (struct zobovConf_info *args_info)
{
args_info->help_given = 0 ;
args_info->version_given = 0 ;
args_info->desc_given = 0 ;
args_info->adj_given = 0 ;
args_info->void_given = 0 ;
args_info->id_given = 0 ;
args_info->sProba_given = 0 ;
args_info->sVol_given = 0 ;
args_info->mProba_given = 0 ;
args_info->ramsesDir_given = 0 ;
args_info->ramsesId_given = 0 ;
args_info->configFile_given = 0 ;
args_info->quiet_given = 0 ;
args_info->move_given = 0 ;
args_info->galax_given = 0 ;
args_info->output_given = 0 ;
args_info->interactive_given = 0 ;
}
static
void clear_args (struct zobovConf_info *args_info)
{
FIX_UNUSED (args_info);
args_info->desc_arg = NULL;
args_info->desc_orig = NULL;
args_info->adj_arg = NULL;
args_info->adj_orig = NULL;
args_info->void_arg = NULL;
args_info->void_orig = NULL;
args_info->id_orig = NULL;
args_info->sProba_flag = 0;
args_info->sVol_flag = 1;
args_info->mProba_arg = 0.0;
args_info->mProba_orig = NULL;
args_info->ramsesDir_arg = NULL;
args_info->ramsesDir_orig = NULL;
args_info->ramsesId_orig = NULL;
args_info->configFile_arg = NULL;
args_info->configFile_orig = NULL;
args_info->quiet_flag = 0;
args_info->move_arg = gengetopt_strdup ("(0,0,0)");
args_info->move_orig = NULL;
args_info->galax_flag = 0;
args_info->output_arg = gengetopt_strdup ("voidDesc");
args_info->output_orig = NULL;
args_info->interactive_flag = 0;
}
static
void init_args_info(struct zobovConf_info *args_info)
{
args_info->help_help = zobovConf_info_help[0] ;
args_info->version_help = zobovConf_info_help[1] ;
args_info->desc_help = zobovConf_info_help[2] ;
args_info->adj_help = zobovConf_info_help[3] ;
args_info->void_help = zobovConf_info_help[4] ;
args_info->id_help = zobovConf_info_help[5] ;
args_info->sProba_help = zobovConf_info_help[6] ;
args_info->sVol_help = zobovConf_info_help[7] ;
args_info->mProba_help = zobovConf_info_help[8] ;
args_info->ramsesDir_help = zobovConf_info_help[9] ;
args_info->ramsesId_help = zobovConf_info_help[10] ;
args_info->configFile_help = zobovConf_info_help[11] ;
args_info->quiet_help = zobovConf_info_help[12] ;
args_info->move_help = zobovConf_info_help[13] ;
args_info->galax_help = zobovConf_info_help[14] ;
args_info->output_help = zobovConf_info_help[15] ;
args_info->interactive_help = zobovConf_info_help[16] ;
}
void
zobovConf_print_version (void)
{
printf ("%s %s\n",
(strlen(ZOBOVCONF_PACKAGE_NAME) ? ZOBOVCONF_PACKAGE_NAME : ZOBOVCONF_PACKAGE),
ZOBOVCONF_VERSION);
}
static void print_help_common(void) {
zobovConf_print_version ();
if (strlen(zobovConf_info_purpose) > 0)
printf("\n%s\n", zobovConf_info_purpose);
if (strlen(zobovConf_info_usage) > 0)
printf("\n%s\n", zobovConf_info_usage);
printf("\n");
if (strlen(zobovConf_info_description) > 0)
printf("%s\n\n", zobovConf_info_description);
}
void
zobovConf_print_help (void)
{
int i = 0;
print_help_common();
while (zobovConf_info_help[i])
printf("%s\n", zobovConf_info_help[i++]);
}
void
zobovConf_init (struct zobovConf_info *args_info)
{
clear_given (args_info);
clear_args (args_info);
init_args_info (args_info);
}
void
zobovConf_params_init(struct zobovConf_params *params)
{
if (params)
{
params->override = 0;
params->initialize = 1;
params->check_required = 1;
params->check_ambiguity = 0;
params->print_errors = 1;
}
}
struct zobovConf_params *
zobovConf_params_create(void)
{
struct zobovConf_params *params =
(struct zobovConf_params *)malloc(sizeof(struct zobovConf_params));
zobovConf_params_init(params);
return params;
}
static void
free_string_field (char **s)
{
if (*s)
{
free (*s);
*s = 0;
}
}
static void
zobovConf_release (struct zobovConf_info *args_info)
{
free_string_field (&(args_info->desc_arg));
free_string_field (&(args_info->desc_orig));
free_string_field (&(args_info->adj_arg));
free_string_field (&(args_info->adj_orig));
free_string_field (&(args_info->void_arg));
free_string_field (&(args_info->void_orig));
free_string_field (&(args_info->id_orig));
free_string_field (&(args_info->mProba_orig));
free_string_field (&(args_info->ramsesDir_arg));
free_string_field (&(args_info->ramsesDir_orig));
free_string_field (&(args_info->ramsesId_orig));
free_string_field (&(args_info->configFile_arg));
free_string_field (&(args_info->configFile_orig));
free_string_field (&(args_info->move_arg));
free_string_field (&(args_info->move_orig));
free_string_field (&(args_info->output_arg));
free_string_field (&(args_info->output_orig));
clear_given (args_info);
}
static void
write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
{
FIX_UNUSED (values);
if (arg) {
fprintf(outfile, "%s=\"%s\"\n", opt, arg);
} else {
fprintf(outfile, "%s\n", opt);
}
}
int
zobovConf_dump(FILE *outfile, struct zobovConf_info *args_info)
{
int i = 0;
if (!outfile)
{
fprintf (stderr, "%s: cannot dump options to stream\n", ZOBOVCONF_PACKAGE);
return EXIT_FAILURE;
}
if (args_info->help_given)
write_into_file(outfile, "help", 0, 0 );
if (args_info->version_given)
write_into_file(outfile, "version", 0, 0 );
if (args_info->desc_given)
write_into_file(outfile, "desc", args_info->desc_orig, 0);
if (args_info->adj_given)
write_into_file(outfile, "adj", args_info->adj_orig, 0);
if (args_info->void_given)
write_into_file(outfile, "void", args_info->void_orig, 0);
if (args_info->id_given)
write_into_file(outfile, "id", args_info->id_orig, 0);
if (args_info->sProba_given)
write_into_file(outfile, "sProba", 0, 0 );
if (args_info->sVol_given)
write_into_file(outfile, "sVol", 0, 0 );
if (args_info->mProba_given)
write_into_file(outfile, "mProba", args_info->mProba_orig, 0);
if (args_info->ramsesDir_given)
write_into_file(outfile, "ramsesDir", args_info->ramsesDir_orig, 0);
if (args_info->ramsesId_given)
write_into_file(outfile, "ramsesId", args_info->ramsesId_orig, 0);
if (args_info->configFile_given)
write_into_file(outfile, "configFile", args_info->configFile_orig, 0);
if (args_info->quiet_given)
write_into_file(outfile, "quiet", 0, 0 );
if (args_info->move_given)
write_into_file(outfile, "move", args_info->move_orig, 0);
if (args_info->galax_given)
write_into_file(outfile, "galax", 0, 0 );
if (args_info->output_given)
write_into_file(outfile, "output", args_info->output_orig, 0);
if (args_info->interactive_given)
write_into_file(outfile, "interactive", 0, 0 );
i = EXIT_SUCCESS;
return i;
}
int
zobovConf_file_save(const char *filename, struct zobovConf_info *args_info)
{
FILE *outfile;
int i = 0;
outfile = fopen(filename, "w");
if (!outfile)
{
fprintf (stderr, "%s: cannot open file for writing: %s\n", ZOBOVCONF_PACKAGE, filename);
return EXIT_FAILURE;
}
i = zobovConf_dump(outfile, args_info);
fclose (outfile);
return i;
}
void
zobovConf_free (struct zobovConf_info *args_info)
{
zobovConf_release (args_info);
}
/** @brief replacement of strdup, which is not standard */
char *
gengetopt_strdup (const char *s)
{
char *result = 0;
if (!s)
return result;
result = (char*)malloc(strlen(s) + 1);
if (result == (char*)0)
return (char*)0;
strcpy(result, s);
return result;
}
int
zobovConf (int argc, char * const *argv, struct zobovConf_info *args_info)
{
return zobovConf2 (argc, argv, args_info, 0, 1, 1);
}
int
zobovConf_ext (int argc, char * const *argv, struct zobovConf_info *args_info,
struct zobovConf_params *params)
{
int result;
result = zobovConf_internal (argc, argv, args_info, params, 0);
if (result == EXIT_FAILURE)
{
zobovConf_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}
int
zobovConf2 (int argc, char * const *argv, struct zobovConf_info *args_info, int override, int initialize, int check_required)
{
int result;
struct zobovConf_params params;
params.override = override;
params.initialize = initialize;
params.check_required = check_required;
params.check_ambiguity = 0;
params.print_errors = 1;
result = zobovConf_internal (argc, argv, args_info, &params, 0);
if (result == EXIT_FAILURE)
{
zobovConf_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}
int
zobovConf_required (struct zobovConf_info *args_info, const char *prog_name)
{
int result = EXIT_SUCCESS;
if (zobovConf_required2(args_info, prog_name, 0) > 0)
result = EXIT_FAILURE;
if (result == EXIT_FAILURE)
{
zobovConf_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}
int
zobovConf_required2 (struct zobovConf_info *args_info, const char *prog_name, const char *additional_error)
{
FIX_UNUSED (additional_error);
int error = 0;
/* checks for required options */
if (! args_info->desc_given)
{
fprintf (stderr, "%s: '--desc' ('-d') option required%s\n", prog_name, (additional_error ? additional_error : ""));
error = 1;
}
if (! args_info->adj_given)
{
fprintf (stderr, "%s: '--adj' ('-a') option required%s\n", prog_name, (additional_error ? additional_error : ""));
error = 1;
}
if (! args_info->void_given)
{
fprintf (stderr, "%s: '--void' ('-v') option required%s\n", prog_name, (additional_error ? additional_error : ""));
error = 1;
}
if (! args_info->ramsesDir_given)
{
fprintf (stderr, "%s: '--ramsesDir' ('-b') option required%s\n", prog_name, (additional_error ? additional_error : ""));
error = 1;
}
if (! args_info->ramsesId_given)
{
fprintf (stderr, "%s: '--ramsesId' ('-r') option required%s\n", prog_name, (additional_error ? additional_error : ""));
error = 1;
}
/* checks for dependences among options */
return error;
}
static char *package_name = 0;
/**
* @brief updates an option
* @param field the generic pointer to the field to update
* @param orig_field the pointer to the orig field
* @param field_given the pointer to the number of occurrence of this option
* @param prev_given the pointer to the number of occurrence already seen
* @param value the argument for this option (if null no arg was specified)
* @param possible_values the possible values for this option (if specified)
* @param default_value the default value (in case the option only accepts fixed values)
* @param arg_type the type of this option
* @param check_ambiguity @see zobovConf_params.check_ambiguity
* @param override @see zobovConf_params.override
* @param no_free whether to free a possible previous value
* @param multiple_option whether this is a multiple option
* @param long_opt the corresponding long option
* @param short_opt the corresponding short option (or '-' if none)
* @param additional_error possible further error specification
*/
static
int update_arg(void *field, char **orig_field,
unsigned int *field_given, unsigned int *prev_given,
char *value, const char *possible_values[],
const char *default_value,
zobovConf_arg_type arg_type,
int check_ambiguity, int override,
int no_free, int multiple_option,
const char *long_opt, char short_opt,
const char *additional_error)
{
FIX_UNUSED (field);
char *stop_char = 0;
const char *val = value;
int found;
char **string_field;
stop_char = 0;
found = 0;
if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
{
if (short_opt != '-')
fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
package_name, long_opt, short_opt,
(additional_error ? additional_error : ""));
else
fprintf (stderr, "%s: `--%s' option given more than once%s\n",
package_name, long_opt,
(additional_error ? additional_error : ""));
return 1; /* failure */
}
FIX_UNUSED (default_value);
if (field_given && *field_given && ! override)
return 0;
if (prev_given)
(*prev_given)++;
if (field_given)
(*field_given)++;
if (possible_values)
val = possible_values[found];
switch(arg_type) {
case ARG_FLAG:
*((int *)field) = !*((int *)field);
break;
case ARG_INT:
if (val) *((int *)field) = strtol (val, &stop_char, 0);
break;
case ARG_DOUBLE:
if (val) *((double *)field) = strtod (val, &stop_char);
break;
case ARG_STRING:
if (val) {
string_field = (char **)field;
if (!no_free && *string_field)
free (*string_field); /* free previous string */
*string_field = gengetopt_strdup (val);
}
break;
default:
break;
};
/* check numeric conversion */
switch(arg_type) {
case ARG_INT:
case ARG_DOUBLE:
if (val && !(stop_char && *stop_char == '\0')) {
fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
return 1; /* failure */
}
break;
default:
;
};
/* store the original value */
switch(arg_type) {
case ARG_NO:
case ARG_FLAG:
break;
default:
if (value && orig_field) {
if (no_free) {
*orig_field = value;
} else {
if (*orig_field)
free (*orig_field); /* free previous string */
*orig_field = gengetopt_strdup (value);
}
}
};
return 0; /* OK */
}
int
zobovConf_internal (
int argc, char * const *argv, struct zobovConf_info *args_info,
struct zobovConf_params *params, const char *additional_error)
{
int c; /* Character of the parsed option. */
int error = 0;
struct zobovConf_info local_args_info;
int override;
int initialize;
int check_required;
int check_ambiguity;
package_name = argv[0];
override = params->override;
initialize = params->initialize;
check_required = params->check_required;
check_ambiguity = params->check_ambiguity;
if (initialize)
zobovConf_init (args_info);
zobovConf_init (&local_args_info);
optarg = 0;
optind = 0;
opterr = params->print_errors;
optopt = '?';
while (1)
{
int option_index = 0;
static struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ "desc", 1, NULL, 'd' },
{ "adj", 1, NULL, 'a' },
{ "void", 1, NULL, 'v' },
{ "id", 1, NULL, 'i' },
{ "sProba", 0, NULL, 's' },
{ "sVol", 0, NULL, 'l' },
{ "mProba", 1, NULL, 'm' },
{ "ramsesDir", 1, NULL, 'b' },
{ "ramsesId", 1, NULL, 'r' },
{ "configFile", 1, NULL, 'f' },
{ "quiet", 0, NULL, 'q' },
{ "move", 1, NULL, 'p' },
{ "galax", 0, NULL, 0 },
{ "output", 1, NULL, 'o' },
{ "interactive", 0, NULL, 0 },
{ 0, 0, 0, 0 }
};
c = getopt_long (argc, argv, "hVd:a:v:i:slm:b:r:f:qp:o:", long_options, &option_index);
if (c == -1) break; /* Exit from `while (1)' loop. */
switch (c)
{
case 'h': /* Print help and exit. */
zobovConf_print_help ();
zobovConf_free (&local_args_info);
exit (EXIT_SUCCESS);
case 'V': /* Print version and exit. */
zobovConf_print_version ();
zobovConf_free (&local_args_info);
exit (EXIT_SUCCESS);
case 'd': /* The description file name for the voids. */
if (update_arg( (void *)&(args_info->desc_arg),
&(args_info->desc_orig), &(args_info->desc_given),
&(local_args_info.desc_given), optarg, 0, 0, ARG_STRING,
check_ambiguity, override, 0, 0,
"desc", 'd',
additional_error))
goto failure;
break;
case 'a': /* Adjacent file name. */
if (update_arg( (void *)&(args_info->adj_arg),
&(args_info->adj_orig), &(args_info->adj_given),
&(local_args_info.adj_given), optarg, 0, 0, ARG_STRING,
check_ambiguity, override, 0, 0,
"adj", 'a',
additional_error))
goto failure;
break;
case 'v': /* Void/zone bind filename. */
if (update_arg( (void *)&(args_info->void_arg),
&(args_info->void_orig), &(args_info->void_given),
&(local_args_info.void_given), optarg, 0, 0, ARG_STRING,
check_ambiguity, override, 0, 0,
"void", 'v',
additional_error))
goto failure;
break;
case 'i': /* Void id to extract. */
if (update_arg( (void *)&(args_info->id_arg),
&(args_info->id_orig), &(args_info->id_given),
&(local_args_info.id_given), optarg, 0, 0, ARG_INT,
check_ambiguity, override, 0, 0,
"id", 'i',
additional_error))
goto failure;
break;
case 's': /* Sort against probability. */
if (update_arg((void *)&(args_info->sProba_flag), 0, &(args_info->sProba_given),
&(local_args_info.sProba_given), optarg, 0, 0, ARG_FLAG,
check_ambiguity, override, 1, 0, "sProba", 's',
additional_error))
goto failure;
break;
case 'l': /* Sort against volume. */
if (update_arg((void *)&(args_info->sVol_flag), 0, &(args_info->sVol_given),
&(local_args_info.sVol_given), optarg, 0, 0, ARG_FLAG,
check_ambiguity, override, 1, 0, "sVol", 'l',
additional_error))
goto failure;
break;
case 'm': /* Minimal probability to accept. */
if (update_arg( (void *)&(args_info->mProba_arg),
&(args_info->mProba_orig), &(args_info->mProba_given),
&(local_args_info.mProba_given), optarg, 0, "0.0", ARG_DOUBLE,
check_ambiguity, override, 0, 0,
"mProba", 'm',
additional_error))
goto failure;
break;
case 'b': /* Ramses base output directory. */
if (update_arg( (void *)&(args_info->ramsesDir_arg),
&(args_info->ramsesDir_orig), &(args_info->ramsesDir_given),
&(local_args_info.ramsesDir_given), optarg, 0, 0, ARG_STRING,
check_ambiguity, override, 0, 0,
"ramsesDir", 'b',
additional_error))
goto failure;
break;
case 'r': /* Ramses output id. */
if (update_arg( (void *)&(args_info->ramsesId_arg),
&(args_info->ramsesId_orig), &(args_info->ramsesId_given),
&(local_args_info.ramsesId_given), optarg, 0, 0, ARG_INT,
check_ambiguity, override, 0, 0,
"ramsesId", 'r',
additional_error))
goto failure;
break;
case 'f': /* Configuration file. */
if (update_arg( (void *)&(args_info->configFile_arg),
&(args_info->configFile_orig), &(args_info->configFile_given),
&(local_args_info.configFile_given), optarg, 0, 0, ARG_STRING,
check_ambiguity, override, 0, 0,
"configFile", 'f',
additional_error))
goto failure;
break;
case 'q': /* Quiet mode. */
if (update_arg((void *)&(args_info->quiet_flag), 0, &(args_info->quiet_given),
&(local_args_info.quiet_given), optarg, 0, 0, ARG_FLAG,
check_ambiguity, override, 1, 0, "quiet", 'q',
additional_error))
goto failure;
break;
case 'p': /* Move the center by (x,y,z). */
if (update_arg( (void *)&(args_info->move_arg),
&(args_info->move_orig), &(args_info->move_given),
&(local_args_info.move_given), optarg, 0, "(0,0,0)", ARG_STRING,
check_ambiguity, override, 0, 0,
"move", 'p',
additional_error))
goto failure;
break;
case 'o': /* Output file. */
if (update_arg( (void *)&(args_info->output_arg),
&(args_info->output_orig), &(args_info->output_given),
&(local_args_info.output_given), optarg, 0, "voidDesc", ARG_STRING,
check_ambiguity, override, 0, 0,
"output", 'o',
additional_error))
goto failure;
break;
case 0: /* Long option with no short option */
/* Output is galax particles. */
if (strcmp (long_options[option_index].name, "galax") == 0)
{
if (update_arg((void *)&(args_info->galax_flag), 0, &(args_info->galax_given),
&(local_args_info.galax_given), optarg, 0, 0, ARG_FLAG,
check_ambiguity, override, 1, 0, "galax", '-',
additional_error))
goto failure;
}
/* Go in interactive mode. */
else if (strcmp (long_options[option_index].name, "interactive") == 0)
{
if (update_arg((void *)&(args_info->interactive_flag), 0, &(args_info->interactive_given),
&(local_args_info.interactive_given), optarg, 0, 0, ARG_FLAG,
check_ambiguity, override, 1, 0, "interactive", '-',
additional_error))
goto failure;
}
break;
case '?': /* Invalid option. */
/* `getopt_long' already printed an error message. */
goto failure;
default: /* bug: option not considered. */
fprintf (stderr, "%s: option unknown: %c%s\n", ZOBOVCONF_PACKAGE, c, (additional_error ? additional_error : ""));
abort ();
} /* switch */
} /* while */
if (check_required)
{
error += zobovConf_required2 (args_info, argv[0], additional_error);
}
zobovConf_release (&local_args_info);
if ( error )
return (EXIT_FAILURE);
return 0;
failure:
zobovConf_release (&local_args_info);
return (EXIT_FAILURE);
}
#ifndef CONFIG_FILE_LINE_SIZE
#define CONFIG_FILE_LINE_SIZE 2048
#endif
#define ADDITIONAL_ERROR " in configuration file "
#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
/* 3 is for "--" and "=" */
static int
_zobovConf_configfile (const char *filename, int *my_argc)
{
FILE* file;
char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
char linebuf[CONFIG_FILE_LINE_SIZE];
int line_num = 0;
int result = 0, equal;
char *fopt, *farg;
char *str_index;
size_t len, next_token;
char delimiter;
if ((file = fopen(filename, "r")) == 0)
{
fprintf (stderr, "%s: Error opening configuration file '%s'\n",
ZOBOVCONF_PACKAGE, filename);
return EXIT_FAILURE;
}
while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0)
{
++line_num;
my_argv[0] = '\0';
len = strlen(linebuf);
if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
{
fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
ZOBOVCONF_PACKAGE, filename, line_num);
result = EXIT_FAILURE;
break;
}
/* find first non-whitespace character in the line */
next_token = strspn (linebuf, " \t\r\n");
str_index = linebuf + next_token;
if ( str_index[0] == '\0' || str_index[0] == '#')
continue; /* empty line or comment line is skipped */
fopt = str_index;
/* truncate fopt at the end of the first non-valid character */
next_token = strcspn (fopt, " \t\r\n=");
if (fopt[next_token] == '\0') /* the line is over */
{
farg = 0;
equal = 0;
goto noarg;
}
/* remember if equal sign is present */
equal = (fopt[next_token] == '=');
fopt[next_token++] = '\0';
/* advance pointers to the next token after the end of fopt */
next_token += strspn (fopt + next_token, " \t\r\n");
/* check for the presence of equal sign, and if so, skip it */
if ( !equal )
if ((equal = (fopt[next_token] == '=')))
{
next_token++;
next_token += strspn (fopt + next_token, " \t\r\n");
}
str_index += next_token;
/* find argument */
farg = str_index;
if ( farg[0] == '\"' || farg[0] == '\'' )
{ /* quoted argument */
str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
if (! str_index)
{
fprintf
(stderr,
"%s:%s:%d: unterminated string in configuration file\n",
ZOBOVCONF_PACKAGE, filename, line_num);
result = EXIT_FAILURE;
break;
}
}
else
{ /* read up the remaining part up to a delimiter */
next_token = strcspn (farg, " \t\r\n#\'\"");
str_index += next_token;
}
/* truncate farg at the delimiter and store it for further check */
delimiter = *str_index, *str_index++ = '\0';
/* everything but comment is illegal at the end of line */
if (delimiter != '\0' && delimiter != '#')
{
str_index += strspn(str_index, " \t\r\n");
if (*str_index != '\0' && *str_index != '#')
{
fprintf
(stderr,
"%s:%s:%d: malformed string in configuration file\n",
ZOBOVCONF_PACKAGE, filename, line_num);
result = EXIT_FAILURE;
break;
}
}
noarg:
if (!strcmp(fopt,"include")) {
if (farg && *farg) {
result = _zobovConf_configfile(farg, my_argc);
} else {
fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
ZOBOVCONF_PACKAGE, filename, line_num);
}
continue;
}
len = strlen(fopt);
strcat (my_argv, len > 1 ? "--" : "-");
strcat (my_argv, fopt);
if (len > 1 && ((farg && *farg) || equal))
strcat (my_argv, "=");
if (farg && *farg)
strcat (my_argv, farg);
++(*my_argc);
cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
cmd_line_list_tmp->next = cmd_line_list;
cmd_line_list = cmd_line_list_tmp;
cmd_line_list->string_arg = gengetopt_strdup(my_argv);
} /* while */
if (file)
fclose(file);
return result;
}
int
zobovConf_configfile (
const char *filename,
struct zobovConf_info *args_info,
int override, int initialize, int check_required)
{
struct zobovConf_params params;
params.override = override;
params.initialize = initialize;
params.check_required = check_required;
params.check_ambiguity = 0;
params.print_errors = 1;
return zobovConf_config_file (filename, args_info, &params);
}
int
zobovConf_config_file (const char *filename,
struct zobovConf_info *args_info,
struct zobovConf_params *params)
{
int i, result;
int my_argc = 1;
char **my_argv_arg;
char *additional_error;
/* store the program name */
cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
cmd_line_list_tmp->next = cmd_line_list;
cmd_line_list = cmd_line_list_tmp;
cmd_line_list->string_arg = gengetopt_strdup (ZOBOVCONF_PACKAGE);
result = _zobovConf_configfile(filename, &my_argc);
if (result != EXIT_FAILURE) {
my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
cmd_line_list_tmp = cmd_line_list;
for (i = my_argc - 1; i >= 0; --i) {
my_argv_arg[i] = cmd_line_list_tmp->string_arg;
cmd_line_list_tmp = cmd_line_list_tmp->next;
}
my_argv_arg[my_argc] = 0;
additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
strcpy (additional_error, ADDITIONAL_ERROR);
strcat (additional_error, filename);
result =
zobovConf_internal (my_argc, my_argv_arg, args_info,
params,
additional_error);
free (additional_error);
free (my_argv_arg);
}
free_cmd_list();
if (result == EXIT_FAILURE)
{
zobovConf_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}