Command line option parsing, configuration parsing

Made config file parsing a bit more fool proof
Config file can now contain a modules-dir element that holds the location of the DSO's
Prepared module loading function to be able to dry-run
Removed some unneeded header files
Added command line options parsing
This commit is contained in:
Gergely Polonkai (W00d5t0ck) 2011-01-25 17:37:55 +01:00
parent 5cc379461a
commit 5c26ca28a6
5 changed files with 117 additions and 21 deletions

View File

@ -10,13 +10,18 @@
#include "modules.h"
int
wxmppd_processConfigfile(const char *file, int startup)
wxmppd_processConfigfile(const char *file, int startup, wxmppd_config_t **config)
{
xmlDocPtr doc;
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObject;
int i;
if (config)
{
*config = g_new0(wxmppd_config_t, 1);
}
if ((doc = xmlParseFile(file)) == NULL)
{
printf("Config file error!\n");
@ -31,24 +36,60 @@ wxmppd_processConfigfile(const char *file, int startup)
return WXMPPD_CONFIG_BADFILE;
}
printf("%d nodes found.\n", xpathObject->nodesetval->nodeNr);
for (i = 0; i < xpathObject->nodesetval->nodeNr; i++)
if (xpathObject->nodesetval)
{
xmlNodePtr textNode;
if (xmlChildElementCount(xpathObject->nodesetval->nodeTab[i]) > 0)
for (i = 0; i < xpathObject->nodesetval->nodeNr; i++)
{
printf("Config file error! modules/load elements cannot have children!\n");
xmlXPathFreeContext(xpathCtx);
xmlNodePtr textNode;
if (xmlChildElementCount(xpathObject->nodesetval->nodeTab[i]) > 0)
{
printf("Config file error! modules/load elements cannot have children!\n");
xmlXPathFreeContext(xpathCtx);
xmlXPathFreeObject(xpathObject);
return WXMPPD_CONFIG_BADFILE;
}
wxmppd_loadModule(xpathObject->nodesetval->nodeTab[i]->children->content, (config == NULL));
}
}
xmlXPathFreeObject(xpathObject);
if ((xpathObject = xmlXPathEvalExpression("/wxmppd/modules/module-dir", xpathCtx)) == NULL)
{
printf("Config file error during xpath!\n");
xmlXPathFreeContext(xpathCtx);
return WXMPPD_CONFIG_BADFILE;
}
if (xpathObject->nodesetval)
{
if (xpathObject->nodesetval->nodeNr > 1)
{
printf("Config file cannot contain more than one module-dir definition!\n");
xmlXPathFreeObject(xpathObject);
xmlXPathFreeContext(xpathCtx);
return WXMPPD_CONFIG_BADFILE;
}
else if (xpathObject->nodesetval->nodeNr == 1)
{
if (xmlChildElementCount(xpathObject->nodesetval->nodeTab[0]) > 0)
{
printf("Config file error! modules/load elements cannot have children!\n");
xmlXPathFreeContext(xpathCtx);
xmlXPathFreeObject(xpathObject);
return WXMPPD_CONFIG_BADFILE;
}
wxmppd_loadModule(xpathObject->nodesetval->nodeTab[i]->children->content);
if (config)
{
(*config)->modules_dir = g_strdup(xpathObject->nodesetval->nodeTab[0]->children->content);
}
}
}
xmlXPathFreeContext(xpathCtx);
xmlXPathFreeObject(xpathObject);
return WXMPPD_CONFIG_SUCCESS;
}

View File

@ -6,7 +6,11 @@ enum {
WXMPPD_CONFIG_BADFILE,
};
int wxmppd_processConfigfile(const char *file, int startup);
typedef struct _wxmppd_config_t {
char *modules_dir;
} wxmppd_config_t;
int wxmppd_processConfigfile(const char *file, int startup, wxmppd_config_t **config);
#endif /* __WXMPPD_CONFIGFILES_H */

View File

@ -1,9 +1,10 @@
#include <stdio.h>
#include <glib.h>
#include "modules.h"
int
wxmppd_loadModule(char *moduleName)
wxmppd_loadModule(char *moduleName, gboolean dryRun)
{
printf("Will load module %s\n", moduleName);
}

View File

@ -1 +1,8 @@
int wxmppd_loadModule(char *moduleName);
#ifndef __WXMPPD_MODULES_H
# define __WXMPPD_MODULES_H
# include <glib.h>
int wxmppd_loadModule(char *moduleName, gboolean dryRun);
#endif /* __WXMPPD_MODULES_H */

View File

@ -1,24 +1,67 @@
#define CONFIG_DIR "/home/polesz/Projektek/wxmppd/data"
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <glib.h>
#include <libxml/parser.h>
#include "configfiles.h"
char *config_dir = NULL;
gchar *config_dir = NULL;
gchar *cl_modules_dir = NULL;
gchar *cl_keys_file = NULL;
static GOptionEntry optionEntries[] = {
{ "config-dir", 'c', 0, G_OPTION_ARG_FILENAME, &config_dir, "Directory where configuration files can be found", "DIRECTORY" },
{ "module-dir", 'm', 0, G_OPTION_ARG_FILENAME, &cl_modules_dir, "Directory of the module files", "DIRECTORY" },
{ "keys-file", 'k', 0, G_OPTION_ARG_FILENAME, &cl_keys_file, "Path to key store file", "FILE" },
{ NULL },
};
int
main(int argc, char **argv)
{
GError *err;
GOptionContext *optContext;
gchar *configFile;
wxmppd_config_t *configObject;
optContext = g_option_context_new("- XMPP daemon");
g_option_context_add_main_entries(optContext, optionEntries, NULL);
if (!g_option_context_parse(optContext, &argc, &argv, &err))
{
g_print("Option parsing failed (%s)!\n", err->message);
return 1;
}
if (config_dir == NULL)
{
config_dir = g_strdup_printf("%s/wxmppd", SYSCONFDIR);
}
configFile = g_strdup_printf("%s/wxmppd.xml", config_dir);
xmlInitParser();
LIBXML_TEST_VERSION;
printf("%s\n", SYSCONFDIR);
if (wxmppd_processConfigfile(configFile, 1, &configObject) != WXMPPD_CONFIG_SUCCESS)
{
return 1;
}
wxmppd_processConfigfile(CONFIG_DIR "/wxmppd.xml", 1);
if (cl_modules_dir != NULL)
{
if (configObject->modules_dir != NULL)
{
g_free(configObject->modules_dir);
}
configObject->modules_dir = g_strdup(cl_modules_dir);
}
if (configObject->modules_dir == NULL)
{
configObject->modules_dir = g_strdup_printf("%s/wxmppd", LIBDIR);
}
printf("%s\n", configObject->modules_dir);
xmlCleanupParser();