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

1020 lines
30 KiB
C

/*
File autogenerated by gengetopt version 2.22.2
generated with the following command:
gengetopt -i buildObservedVoids.ggo -f buildObservedVoids_conf -a buildObservedVoids_info -F buildObservedVoids_conf -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 "buildObservedVoids_conf.h"
const char *buildObservedVoids_info_purpose = "";
const char *buildObservedVoids_info_usage = "Usage: buildObservedVoids [OPTIONS]...";
const char *buildObservedVoids_info_description = "";
const char *buildObservedVoids_info_help[] = {
" -h, --help Print help and exit",
" -V, --version Print version and exit",
" -d, --desc=STRING The description file name for the voids (jozov \n generated)",
" -a, --adj=STRING Adjacent file name (jozov generated)",
" -v, --void=STRING Void/zone bind filename (jozov generated)",
" -m, --minProba=DOUBLE Minimal probability to accept (default=`0.0')",
" --densityThreshold=DOUBLE Density threshold to consider a stable void \n (default=`-0.8')",
" -b, --ramsesDir=STRING Ramses base output directory",
" -r, --ramsesId=INT Ramses output id",
" -f, --configFile=STRING Configuration file",
" -o, --output=STRING Output file (default=`void_info.txt')",
" -g, --gridOutput=STRING 3d grid output of voids \n (default=`all_voids.nc')",
" -q, --quiet Quiet output (default=off)",
0
};
typedef enum {ARG_NO
, ARG_FLAG
, ARG_STRING
, ARG_INT
, ARG_DOUBLE
} buildObservedVoids_conf_arg_type;
static
void clear_given (struct buildObservedVoids_info *args_info);
static
void clear_args (struct buildObservedVoids_info *args_info);
static int
buildObservedVoids_conf_internal (int argc, char * const *argv, struct buildObservedVoids_info *args_info,
struct buildObservedVoids_conf_params *params, const char *additional_error);
static int
buildObservedVoids_conf_required2 (struct buildObservedVoids_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 buildObservedVoids_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->minProba_given = 0 ;
args_info->densityThreshold_given = 0 ;
args_info->ramsesDir_given = 0 ;
args_info->ramsesId_given = 0 ;
args_info->configFile_given = 0 ;
args_info->output_given = 0 ;
args_info->gridOutput_given = 0 ;
args_info->quiet_given = 0 ;
}
static
void clear_args (struct buildObservedVoids_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->minProba_arg = 0.0;
args_info->minProba_orig = NULL;
args_info->densityThreshold_arg = -0.8;
args_info->densityThreshold_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->output_arg = gengetopt_strdup ("void_info.txt");
args_info->output_orig = NULL;
args_info->gridOutput_arg = gengetopt_strdup ("all_voids.nc");
args_info->gridOutput_orig = NULL;
args_info->quiet_flag = 0;
}
static
void init_args_info(struct buildObservedVoids_info *args_info)
{
args_info->help_help = buildObservedVoids_info_help[0] ;
args_info->version_help = buildObservedVoids_info_help[1] ;
args_info->desc_help = buildObservedVoids_info_help[2] ;
args_info->adj_help = buildObservedVoids_info_help[3] ;
args_info->void_help = buildObservedVoids_info_help[4] ;
args_info->minProba_help = buildObservedVoids_info_help[5] ;
args_info->densityThreshold_help = buildObservedVoids_info_help[6] ;
args_info->ramsesDir_help = buildObservedVoids_info_help[7] ;
args_info->ramsesId_help = buildObservedVoids_info_help[8] ;
args_info->configFile_help = buildObservedVoids_info_help[9] ;
args_info->output_help = buildObservedVoids_info_help[10] ;
args_info->gridOutput_help = buildObservedVoids_info_help[11] ;
args_info->quiet_help = buildObservedVoids_info_help[12] ;
}
void
buildObservedVoids_conf_print_version (void)
{
printf ("%s %s\n",
(strlen(BUILDOBSERVEDVOIDS_CONF_PACKAGE_NAME) ? BUILDOBSERVEDVOIDS_CONF_PACKAGE_NAME : BUILDOBSERVEDVOIDS_CONF_PACKAGE),
BUILDOBSERVEDVOIDS_CONF_VERSION);
}
static void print_help_common(void) {
buildObservedVoids_conf_print_version ();
if (strlen(buildObservedVoids_info_purpose) > 0)
printf("\n%s\n", buildObservedVoids_info_purpose);
if (strlen(buildObservedVoids_info_usage) > 0)
printf("\n%s\n", buildObservedVoids_info_usage);
printf("\n");
if (strlen(buildObservedVoids_info_description) > 0)
printf("%s\n\n", buildObservedVoids_info_description);
}
void
buildObservedVoids_conf_print_help (void)
{
int i = 0;
print_help_common();
while (buildObservedVoids_info_help[i])
printf("%s\n", buildObservedVoids_info_help[i++]);
}
void
buildObservedVoids_conf_init (struct buildObservedVoids_info *args_info)
{
clear_given (args_info);
clear_args (args_info);
init_args_info (args_info);
}
void
buildObservedVoids_conf_params_init(struct buildObservedVoids_conf_params *params)
{
if (params)
{
params->override = 0;
params->initialize = 1;
params->check_required = 1;
params->check_ambiguity = 0;
params->print_errors = 1;
}
}
struct buildObservedVoids_conf_params *
buildObservedVoids_conf_params_create(void)
{
struct buildObservedVoids_conf_params *params =
(struct buildObservedVoids_conf_params *)malloc(sizeof(struct buildObservedVoids_conf_params));
buildObservedVoids_conf_params_init(params);
return params;
}
static void
free_string_field (char **s)
{
if (*s)
{
free (*s);
*s = 0;
}
}
static void
buildObservedVoids_conf_release (struct buildObservedVoids_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->minProba_orig));
free_string_field (&(args_info->densityThreshold_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->output_arg));
free_string_field (&(args_info->output_orig));
free_string_field (&(args_info->gridOutput_arg));
free_string_field (&(args_info->gridOutput_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
buildObservedVoids_conf_dump(FILE *outfile, struct buildObservedVoids_info *args_info)
{
int i = 0;
if (!outfile)
{
fprintf (stderr, "%s: cannot dump options to stream\n", BUILDOBSERVEDVOIDS_CONF_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->minProba_given)
write_into_file(outfile, "minProba", args_info->minProba_orig, 0);
if (args_info->densityThreshold_given)
write_into_file(outfile, "densityThreshold", args_info->densityThreshold_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->output_given)
write_into_file(outfile, "output", args_info->output_orig, 0);
if (args_info->gridOutput_given)
write_into_file(outfile, "gridOutput", args_info->gridOutput_orig, 0);
if (args_info->quiet_given)
write_into_file(outfile, "quiet", 0, 0 );
i = EXIT_SUCCESS;
return i;
}
int
buildObservedVoids_conf_file_save(const char *filename, struct buildObservedVoids_info *args_info)
{
FILE *outfile;
int i = 0;
outfile = fopen(filename, "w");
if (!outfile)
{
fprintf (stderr, "%s: cannot open file for writing: %s\n", BUILDOBSERVEDVOIDS_CONF_PACKAGE, filename);
return EXIT_FAILURE;
}
i = buildObservedVoids_conf_dump(outfile, args_info);
fclose (outfile);
return i;
}
void
buildObservedVoids_conf_free (struct buildObservedVoids_info *args_info)
{
buildObservedVoids_conf_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
buildObservedVoids_conf (int argc, char * const *argv, struct buildObservedVoids_info *args_info)
{
return buildObservedVoids_conf2 (argc, argv, args_info, 0, 1, 1);
}
int
buildObservedVoids_conf_ext (int argc, char * const *argv, struct buildObservedVoids_info *args_info,
struct buildObservedVoids_conf_params *params)
{
int result;
result = buildObservedVoids_conf_internal (argc, argv, args_info, params, 0);
if (result == EXIT_FAILURE)
{
buildObservedVoids_conf_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}
int
buildObservedVoids_conf2 (int argc, char * const *argv, struct buildObservedVoids_info *args_info, int override, int initialize, int check_required)
{
int result;
struct buildObservedVoids_conf_params params;
params.override = override;
params.initialize = initialize;
params.check_required = check_required;
params.check_ambiguity = 0;
params.print_errors = 1;
result = buildObservedVoids_conf_internal (argc, argv, args_info, &params, 0);
if (result == EXIT_FAILURE)
{
buildObservedVoids_conf_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}
int
buildObservedVoids_conf_required (struct buildObservedVoids_info *args_info, const char *prog_name)
{
int result = EXIT_SUCCESS;
if (buildObservedVoids_conf_required2(args_info, prog_name, 0) > 0)
result = EXIT_FAILURE;
if (result == EXIT_FAILURE)
{
buildObservedVoids_conf_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}
int
buildObservedVoids_conf_required2 (struct buildObservedVoids_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;
}
/* 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 buildObservedVoids_conf_params.check_ambiguity
* @param override @see buildObservedVoids_conf_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,
buildObservedVoids_conf_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
buildObservedVoids_conf_internal (
int argc, char * const *argv, struct buildObservedVoids_info *args_info,
struct buildObservedVoids_conf_params *params, const char *additional_error)
{
int c; /* Character of the parsed option. */
int error = 0;
struct buildObservedVoids_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)
buildObservedVoids_conf_init (args_info);
buildObservedVoids_conf_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' },
{ "minProba", 1, NULL, 'm' },
{ "densityThreshold", 1, NULL, 0 },
{ "ramsesDir", 1, NULL, 'b' },
{ "ramsesId", 1, NULL, 'r' },
{ "configFile", 1, NULL, 'f' },
{ "output", 1, NULL, 'o' },
{ "gridOutput", 1, NULL, 'g' },
{ "quiet", 0, NULL, 'q' },
{ 0, 0, 0, 0 }
};
c = getopt_long (argc, argv, "hVd:a:v:m:b:r:f:o:g:q", long_options, &option_index);
if (c == -1) break; /* Exit from `while (1)' loop. */
switch (c)
{
case 'h': /* Print help and exit. */
buildObservedVoids_conf_print_help ();
buildObservedVoids_conf_free (&local_args_info);
exit (EXIT_SUCCESS);
case 'V': /* Print version and exit. */
buildObservedVoids_conf_print_version ();
buildObservedVoids_conf_free (&local_args_info);
exit (EXIT_SUCCESS);
case 'd': /* The description file name for the voids (jozov generated). */
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 (jozov generated). */
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 (jozov generated). */
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 'm': /* Minimal probability to accept. */
if (update_arg( (void *)&(args_info->minProba_arg),
&(args_info->minProba_orig), &(args_info->minProba_given),
&(local_args_info.minProba_given), optarg, 0, "0.0", ARG_DOUBLE,
check_ambiguity, override, 0, 0,
"minProba", '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 '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, "void_info.txt", ARG_STRING,
check_ambiguity, override, 0, 0,
"output", 'o',
additional_error))
goto failure;
break;
case 'g': /* 3d grid output of voids. */
if (update_arg( (void *)&(args_info->gridOutput_arg),
&(args_info->gridOutput_orig), &(args_info->gridOutput_given),
&(local_args_info.gridOutput_given), optarg, 0, "all_voids.nc", ARG_STRING,
check_ambiguity, override, 0, 0,
"gridOutput", 'g',
additional_error))
goto failure;
break;
case 'q': /* Quiet output. */
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 0: /* Long option with no short option */
/* Density threshold to consider a stable void. */
if (strcmp (long_options[option_index].name, "densityThreshold") == 0)
{
if (update_arg( (void *)&(args_info->densityThreshold_arg),
&(args_info->densityThreshold_orig), &(args_info->densityThreshold_given),
&(local_args_info.densityThreshold_given), optarg, 0, "-0.8", ARG_DOUBLE,
check_ambiguity, override, 0, 0,
"densityThreshold", '-',
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", BUILDOBSERVEDVOIDS_CONF_PACKAGE, c, (additional_error ? additional_error : ""));
abort ();
} /* switch */
} /* while */
if (check_required)
{
error += buildObservedVoids_conf_required2 (args_info, argv[0], additional_error);
}
buildObservedVoids_conf_release (&local_args_info);
if ( error )
return (EXIT_FAILURE);
return 0;
failure:
buildObservedVoids_conf_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
_buildObservedVoids_conf_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",
BUILDOBSERVEDVOIDS_CONF_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",
BUILDOBSERVEDVOIDS_CONF_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",
BUILDOBSERVEDVOIDS_CONF_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",
BUILDOBSERVEDVOIDS_CONF_PACKAGE, filename, line_num);
result = EXIT_FAILURE;
break;
}
}
noarg:
if (!strcmp(fopt,"include")) {
if (farg && *farg) {
result = _buildObservedVoids_conf_configfile(farg, my_argc);
} else {
fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
BUILDOBSERVEDVOIDS_CONF_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
buildObservedVoids_conf_configfile (
const char *filename,
struct buildObservedVoids_info *args_info,
int override, int initialize, int check_required)
{
struct buildObservedVoids_conf_params params;
params.override = override;
params.initialize = initialize;
params.check_required = check_required;
params.check_ambiguity = 0;
params.print_errors = 1;
return buildObservedVoids_conf_config_file (filename, args_info, &params);
}
int
buildObservedVoids_conf_config_file (const char *filename,
struct buildObservedVoids_info *args_info,
struct buildObservedVoids_conf_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 (BUILDOBSERVEDVOIDS_CONF_PACKAGE);
result = _buildObservedVoids_conf_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 =
buildObservedVoids_conf_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)
{
buildObservedVoids_conf_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}