From be9a2b6fa7b39c27a1d2f44d796041561478c81d Mon Sep 17 00:00:00 2001 From: Sebastian Hammer Date: Wed, 27 Dec 2006 21:11:10 +0000 Subject: [PATCH] Basic configuration functionality in place (not yet used) --- etc/pazpar2.cfg | 12 +++-- src/Makefile | 7 +-- src/config.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/config.h | 51 ++++++++++++++++++ src/pazpar2.c | 10 +++- 5 files changed, 230 insertions(+), 11 deletions(-) diff --git a/etc/pazpar2.cfg b/etc/pazpar2.cfg index ae8b15f..97a5bdd 100644 --- a/etc/pazpar2.cfg +++ b/etc/pazpar2.cfg @@ -13,15 +13,17 @@ configuration could live within one listener (on a single port). --> - - - + + + + - - + + + diff --git a/src/Makefile b/src/Makefile index 1f9b450..dc2ae88 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ # ParaZ. Copyright (C) 2000-2004, Index Data ApS # All rights reserved. -# $Id: Makefile,v 1.2 2006-12-22 04:43:11 quinn Exp $ +# $Id: Makefile,v 1.3 2006-12-27 21:11:10 quinn Exp $ SHELL=/bin/sh @@ -12,7 +12,7 @@ YAZCFLAGS=`$(YAZCONF) --cflags` PROG=pazpar2 PROGO=pazpar2.o eventl.o util.o command.o http.o http_command.o termlists.o \ - reclists.o relevance.o + reclists.o relevance.o config.o all: $(PROG) @@ -30,13 +30,14 @@ clean: command.o: command.c command.h util.h eventl.h pazpar2.h termlists.h \ relevance.h reclists.h +config.o: config.c config.h eventl.o: eventl.c eventl.h http.o: http.c command.h util.h eventl.h pazpar2.h termlists.h \ relevance.h reclists.h http.h http_command.h http_command.o: http_command.c command.h util.h eventl.h pazpar2.h \ termlists.h relevance.h reclists.h http.h http_command.h pazpar2.o: pazpar2.c pazpar2.h termlists.h relevance.h reclists.h \ - eventl.h command.h http.h + eventl.h command.h http.h config.h reclists.o: reclists.c pazpar2.h termlists.h relevance.h reclists.h \ eventl.h relevance.o: relevance.c relevance.h pazpar2.h termlists.h eventl.h \ diff --git a/src/config.c b/src/config.c index ee5ad34..e02c670 100644 --- a/src/config.c +++ b/src/config.c @@ -1 +1,160 @@ -/* $Id: config.c,v 1.1 2006-12-27 02:14:27 quinn Exp $ */ +/* $Id: config.c,v 1.2 2006-12-27 21:11:10 quinn Exp $ */ + +#include + +#include +#include + +#include +#include + +#define CONFIG_NOEXTERNS +#include "config.h" + +static NMEM nmem = 0; + +struct conf_config *config = 0; + +static struct conf_service *parse_service(xmlNode *node) +{ + xmlNode *n; + struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service)); + + r->termlists = 0; + + for (n = node->children; n; n = n->next) + { + if (n->type != XML_ELEMENT_NODE) + continue; + if (!strcmp(n->name, "termlist")) + { + struct conf_termlist *tl = nmem_malloc(nmem, sizeof(struct conf_termlist)); + xmlChar *name = xmlGetProp(n, "name"); + if (!name) + { + yaz_log(YLOG_WARN, "Missing name attribute in termlist"); + continue; + } + tl->name = nmem_strdup(nmem, name); + tl->next = r->termlists; + r->termlists = tl; + } + else + { + yaz_log(YLOG_FATAL, "Bad element: %s", n->name); + return 0; + } + } + return r; +} + +static struct conf_server *parse_server(xmlNode *node) +{ + xmlNode *n; + struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server)); + + r->host = 0; + r->port = 0; + r->proxy_host = 0; + r->proxy_port = 0; + r->service = 0; + r->next = 0; + + for (n = node->children; n; n = n->next) + { + if (n->type != XML_ELEMENT_NODE) + continue; + if (!strcmp(n->name, "listen")) + { + xmlChar *port = xmlGetProp(n, "port"); + xmlChar *host = xmlGetProp(n, "host"); + if (port) + r->port = atoi(port); + if (host) + r->host = nmem_strdup(nmem, host); + } + else if (!strcmp(n->name, "proxy")) + { + xmlChar *port = xmlGetProp(n, "port"); + xmlChar *host = xmlGetProp(n, "host"); + if (port) + r->proxy_port = atoi(port); + if (host) + r->proxy_host = nmem_strdup(nmem, host); + } + else if (!strcmp(n->name, "service")) + { + struct conf_service *s = parse_service(n); + if (!s) + return 0; + r->service = s; + } + else + { + yaz_log(YLOG_FATAL, "Bad element: %s", n->name); + return 0; + } + } + return r; +} + +static struct conf_config *parse_config(xmlNode *root) +{ + xmlNode *n; + struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config)); + + r->servers = 0; + r->queryprofiles = 0; + r->retrievalprofiles = 0; + + for (n = root->children; n; n = n->next) + { + if (n->type != XML_ELEMENT_NODE) + continue; + if (!strcmp(n->name, "server")) + { + struct conf_server *tmp = parse_server(n); + if (!tmp) + return 0; + tmp->next = r->servers; + r->servers = tmp; + } + else if (!strcmp(n->name, "queryprofile")) + { + } + else if (!strcmp(n->name, "retrievalprofile")) + { + } + else + { + yaz_log(YLOG_FATAL, "Bad element: %s", n->name); + return 0; + } + } + return r; +} + +int read_config(const char *fname) +{ + xmlDoc *doc = xmlReadFile(fname, NULL, 0); + if (!nmem) + nmem = nmem_create(); + if (!doc) + { + yaz_log(YLOG_FATAL, "Failed to read %s", fname); + exit(1); + } + if ((config = parse_config(xmlDocGetRootElement(doc)))) + return 1; + else + return 0; +} + + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/config.h b/src/config.h index 801e4cb..9d49ac5 100644 --- a/src/config.h +++ b/src/config.h @@ -1,5 +1,56 @@ #ifndef CONFIG_H #define CONFIG_H +struct conf_termlist +{ + char *name; + struct conf_termlist *next; +}; + +struct conf_service +{ + struct conf_termlist *termlists; +}; + +struct conf_server +{ + char *host; + int port; + char *proxy_host; + int proxy_port; + struct conf_service *service; + struct conf_server *next; +}; + +struct conf_queryprofile +{ +}; + +struct conf_retrievalprofile +{ +}; + +struct conf_config +{ + struct conf_server *servers; + struct conf_queryprofile *queryprofiles; + struct conf_retrievalprofile *retrievalprofiles; +}; + +#ifndef CONFIG_NOEXTERNS + +extern struct conf_config *config; #endif + +int read_config(const char *fname); + +#endif + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/pazpar2.c b/src/pazpar2.c index 84a94c2..8605397 100644 --- a/src/pazpar2.c +++ b/src/pazpar2.c @@ -1,4 +1,4 @@ -/* $Id: pazpar2.c,v 1.5 2006-12-22 04:43:11 quinn Exp $ */; +/* $Id: pazpar2.c,v 1.6 2006-12-27 21:11:10 quinn Exp $ */; #include #include @@ -26,6 +26,7 @@ #include "termlists.h" #include "reclists.h" #include "relevance.h" +#include "config.h" #define PAZPAR2_VERSION "0.1" #define MAX_CHUNK 15 @@ -1372,9 +1373,13 @@ int main(int argc, char **argv) yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0); - while ((ret = options("x:c:h:p:C:s:", argv, argc, &arg)) != -2) + while ((ret = options("f:x:c:h:p:C:s:", argv, argc, &arg)) != -2) { switch (ret) { + case 'f': + if (!read_config(arg)) + exit(1); + break; case 'c': command_init(atoi(arg)); setport++; @@ -1397,6 +1402,7 @@ int main(int argc, char **argv) break; default: fprintf(stderr, "Usage: pazpar2\n" + " -f configfile\n" " -h [host:]port (REST protocol listener)\n" " -c cmdport (telnet-style)\n" " -C cclconfig\n" -- 1.7.10.4