Refactor yaml loader so we can load strings or files.

remotes/origin/master-1.0.x
Jason Ish 16 years ago committed by Victor Julien
parent 4175206417
commit d39a291427

@ -8,8 +8,8 @@
* YAML configuration loader. * YAML configuration loader.
*/ */
#include "eidps-common.h"
#include <yaml.h> #include <yaml.h>
#include "eidps-common.h"
#include "conf.h" #include "conf.h"
#include "util-debug.h" #include "util-debug.h"
@ -68,18 +68,15 @@ GetKeyName(char **key, int level)
} }
/** /**
* \brief Load a configuration file. * \brief Process a YAML parser.
* *
* Loads the IDS configuration file. On failure, the program will * Loads a configuration from a setup YAML parser.
* exist with an error message.
* *
* \param filename Name of the filename to load. * \param parser A YAML parser setup for processing.
*/ */
void static void
LoadYamlConf(const char *filename) ConfYamlParse(yaml_parser_t *parser)
{ {
FILE *conf_file;
yaml_parser_t parser;
yaml_event_t event; yaml_event_t event;
int done; int done;
int level; int level;
@ -89,27 +86,14 @@ LoadYamlConf(const char *filename)
memset(key, 0, sizeof(key)); memset(key, 0, sizeof(key));
if (yaml_parser_initialize(&parser) != 1) {
fprintf(stderr, "Failed to initialize yaml parser.\n");
exit(EXIT_FAILURE);
}
conf_file = fopen(filename, "r");
if (conf_file == NULL) {
fprintf(stderr, "Failed to open file: %s: %s\n", filename,
strerror(errno));
exit(EXIT_FAILURE);
}
yaml_parser_set_input_file(&parser, conf_file);
state = CONF_KEY; state = CONF_KEY;
done = 0; done = 0;
level = -1; level = -1;
inseq = 0; inseq = 0;
while (!done) { while (!done) {
if (!yaml_parser_parse(&parser, &event)) { if (!yaml_parser_parse(parser, &event)) {
fprintf(stderr, "Failed to parse configuration file: %s\n", fprintf(stderr, "Failed to parse configuration file: %s\n",
parser.problem); parser->problem);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (level > -1) { if (level > -1) {
@ -190,7 +174,47 @@ LoadYamlConf(const char *filename)
} }
yaml_event_delete(&event); yaml_event_delete(&event);
} }
}
/**
* \brief Load configuration from a YAML file.
*/
void
ConfYamlLoadFile(const char *filename)
{
FILE *infile;
yaml_parser_t parser;
if (yaml_parser_initialize(&parser) != 1) {
fprintf(stderr, "Failed to initialize yaml parser.\n");
exit(EXIT_FAILURE);
}
infile = fopen(filename, "r");
if (infile == NULL) {
fprintf(stderr, "Failed to open file: %s: %s\n", filename,
strerror(errno));
exit(EXIT_FAILURE);
}
yaml_parser_set_input_file(&parser, infile);
ConfYamlParse(&parser);
yaml_parser_delete(&parser);
fclose(infile);
}
/**
* \brief Load configuration from a YAML string.
*/
static void
ConfYamlLoadString(const u_char *string, size_t len)
{
yaml_parser_t parser;
if (yaml_parser_initialize(&parser) != 1) {
fprintf(stderr, "Failed to initialize yaml parser.\n");
exit(EXIT_FAILURE);
}
yaml_parser_set_input_string(&parser, string, len);
ConfYamlParse(&parser);
yaml_parser_delete(&parser); yaml_parser_delete(&parser);
fclose(conf_file);
} }

@ -3,6 +3,6 @@
#ifndef __CONF_YAML_LOADER_H__ #ifndef __CONF_YAML_LOADER_H__
#define __CONF_YAML_LOADER_H__ #define __CONF_YAML_LOADER_H__
void LoadYamlConf(const char *); void ConfYamlLoadFile(const char *);
#endif /* !__CONF_YAML_LOADER_H__ */ #endif /* !__CONF_YAML_LOADER_H__ */

@ -359,7 +359,7 @@ int main(int argc, char **argv)
/* Load yaml configuration file if provided. */ /* Load yaml configuration file if provided. */
if (conf_filename != NULL) { if (conf_filename != NULL) {
LoadYamlConf(conf_filename); ConfYamlLoadFile(conf_filename);
} }
if (dump_config) { if (dump_config) {

Loading…
Cancel
Save