-/* $Id: config.c,v 1.19 2007-03-30 02:45:07 quinn Exp $ */
+/* $Id: config.c,v 1.37 2007-06-19 10:15:44 adam Exp $
+ Copyright (c) 2006-2007, Index Data.
+
+This file is part of Pazpar2.
+
+Pazpar2 is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Pazpar2; see the file LICENSE. If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+ */
+
+/* $Id: config.c,v 1.37 2007-06-19 10:15:44 adam Exp $ */
#include <string.h>
#include <yaz/yaz-util.h>
#include <yaz/nmem.h>
+#include <yaz/snprintf.h>
#define CONFIG_NOEXTERNS
#include "config.h"
+
static NMEM nmem = 0;
static char confdir[256] = ".";
struct conf_config *config = 0;
+
+struct conf_metadata * conf_metadata_assign(NMEM nmem,
+ struct conf_metadata * metadata,
+ const char *name,
+ enum conf_metadata_type type,
+ enum conf_metadata_merge merge,
+ int brief,
+ int termlist,
+ int rank,
+ int sortkey_offset)
+{
+ if (!nmem || !metadata || !name)
+ return 0;
+
+ metadata->name = nmem_strdup(nmem, name);
+
+ // enforcing that merge_range is always type_year
+ if (merge == Metadata_merge_range)
+ metadata->type = Metadata_type_year;
+ else
+ metadata->type = type;
+
+ // enforcing that type_year is always range_merge
+ if (metadata->type == Metadata_type_year)
+ metadata->merge = Metadata_merge_range;
+ else
+ metadata->merge = merge;
+
+ metadata->brief = brief;
+ metadata->termlist = termlist;
+ metadata->rank = rank;
+ metadata->sortkey_offset = sortkey_offset;
+ return metadata;
+}
+
+
+struct conf_sortkey * conf_sortkey_assign(NMEM nmem,
+ struct conf_sortkey * sortkey,
+ const char *name,
+ enum conf_sortkey_type type)
+{
+ if (!nmem || !sortkey || !name)
+ return 0;
+
+ sortkey->name = nmem_strdup(nmem, name);
+ sortkey->type = type;
+
+ return sortkey;
+}
+
+
+struct conf_service * conf_service_create(NMEM nmem,
+ int num_metadata, int num_sortkeys)
+{
+ struct conf_service * service = 0;
+
+ //assert(nmem);
+
+ service = nmem_malloc(nmem, sizeof(struct conf_service));
+
+ service->num_metadata = num_metadata;
+ service->metadata = 0;
+ if (service->num_metadata)
+ service->metadata
+ = nmem_malloc(nmem,
+ sizeof(struct conf_metadata) * service->num_metadata);
+ service->num_sortkeys = num_sortkeys;
+ service->sortkeys = 0;
+ if (service->num_sortkeys)
+ service->sortkeys
+ = nmem_malloc(nmem,
+ sizeof(struct conf_sortkey) * service->num_sortkeys);
+
+ return service;
+}
+
+struct conf_metadata* conf_service_add_metadata(NMEM nmem,
+ struct conf_service *service,
+ int field_id,
+ const char *name,
+ enum conf_metadata_type type,
+ enum conf_metadata_merge merge,
+ int brief,
+ int termlist,
+ int rank,
+ int sortkey_offset)
+{
+ struct conf_metadata * md = 0;
+
+ if (!service || !service->metadata || !service->num_metadata
+ || field_id < 0 || !(field_id < service->num_metadata))
+ return 0;
+
+ //md = &((service->metadata)[field_id]);
+ md = service->metadata + field_id;
+ md = conf_metadata_assign(nmem, md, name, type, merge,
+ brief, termlist, rank, sortkey_offset);
+ return md;
+}
+
+
+struct conf_sortkey * conf_service_add_sortkey(NMEM nmem,
+ struct conf_service *service,
+ int field_id,
+ const char *name,
+ enum conf_sortkey_type type)
+{
+ struct conf_sortkey * sk = 0;
+
+ if (!service || !service->sortkeys || !service->num_sortkeys
+ || field_id < 0 || !(field_id < service->num_sortkeys))
+ return 0;
+
+ //sk = &((service->sortkeys)[field_id]);
+ sk = service->sortkeys + field_id;
+ sk = conf_sortkey_assign(nmem, sk, name, type);
+
+ return sk;
+}
+
+
+int conf_service_metadata_field_id(struct conf_service *service,
+ const char * name)
+{
+ int i = 0;
+
+ if (!service || !service->metadata || !service->num_metadata)
+ return -1;
+
+ for(i = 0; i < service->num_metadata; i++) {
+ if (!strcmp(name, (service->metadata[i]).name))
+ return i;
+ }
+
+ return -1;
+};
+
+
+int conf_service_sortkey_field_id(struct conf_service *service,
+ const char * name)
+{
+ int i = 0;
+
+ if (!service || !service->sortkeys || !service->num_sortkeys)
+ return -1;
+
+ for(i = 0; i < service->num_sortkeys; i++) {
+ if (!strcmp(name, (service->sortkeys[i]).name))
+ return i;
+ }
+
+ return -1;
+};
+
+
+
/* Code to parse configuration file */
/* ==================================================== */
static struct conf_service *parse_service(xmlNode *node)
{
xmlNode *n;
- struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
int md_node = 0;
int sk_node = 0;
- r->num_sortkeys = r->num_metadata = 0;
- // Allocate array of conf metadata and sortkey tructs, if necessary
+ struct conf_service *service = 0;
+ int num_metadata = 0;
+ int num_sortkeys = 0;
+
+ // count num_metadata and num_sortkeys
for (n = node->children; n; n = n->next)
if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
n->name, "metadata"))
{
xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
- r->num_metadata++;
+ num_metadata++;
if (sortkey && strcmp((const char *) sortkey, "no"))
- r->num_sortkeys++;
+ num_sortkeys++;
xmlFree(sortkey);
}
- if (r->num_metadata)
- r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
- else
- r->metadata = 0;
- if (r->num_sortkeys)
- r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
- else
- r->sortkeys = 0;
+
+ service = conf_service_create(nmem, num_metadata, num_sortkeys);
for (n = node->children; n; n = n->next)
{
continue;
if (!strcmp((const char *) n->name, (const char *) "metadata"))
{
- struct conf_metadata *md = &r->metadata[md_node];
- xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
- xmlChar *brief = xmlGetProp(n, (xmlChar *) "brief");
- xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
- xmlChar *merge = xmlGetProp(n, (xmlChar *) "merge");
- xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
- xmlChar *termlist = xmlGetProp(n, (xmlChar *) "termlist");
- xmlChar *rank = xmlGetProp(n, (xmlChar *) "rank");
-
- if (!name)
+ xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name");
+ xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief");
+ xmlChar *xml_sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
+ xmlChar *xml_merge = xmlGetProp(n, (xmlChar *) "merge");
+ xmlChar *xml_type = xmlGetProp(n, (xmlChar *) "type");
+ xmlChar *xml_termlist = xmlGetProp(n, (xmlChar *) "termlist");
+ xmlChar *xml_rank = xmlGetProp(n, (xmlChar *) "rank");
+
+ enum conf_metadata_type type = Metadata_type_generic;
+ enum conf_metadata_merge merge = Metadata_merge_no;
+ int brief = 0;
+ int termlist = 0;
+ int rank = 0;
+ int sortkey_offset = 0;
+ enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
+
+ // now do the parsing logic
+ if (!xml_name)
{
yaz_log(YLOG_FATAL, "Must specify name in metadata element");
return 0;
}
- md->name = nmem_strdup(nmem, (const char *) name);
- if (brief)
+ if (xml_brief)
{
- if (!strcmp((const char *) brief, "yes"))
- md->brief = 1;
- else if (strcmp((const char *) brief, "no"))
+ if (!strcmp((const char *) xml_brief, "yes"))
+ brief = 1;
+ else if (strcmp((const char *) xml_brief, "no"))
{
yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
return 0;
}
}
else
- md->brief = 0;
+ brief = 0;
- if (termlist)
+ if (xml_termlist)
{
- if (!strcmp((const char *) termlist, "yes"))
- md->termlist = 1;
- else if (strcmp((const char *) termlist, "no"))
+ if (!strcmp((const char *) xml_termlist, "yes"))
+ termlist = 1;
+ else if (strcmp((const char *) xml_termlist, "no"))
{
yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
return 0;
}
}
else
- md->termlist = 0;
+ termlist = 0;
- if (rank)
- md->rank = atoi((const char *) rank);
+ if (xml_rank)
+ rank = atoi((const char *) xml_rank);
else
- md->rank = 0;
+ rank = 0;
- if (type)
+ if (xml_type)
{
- if (!strcmp((const char *) type, "generic"))
- md->type = Metadata_type_generic;
- else if (!strcmp((const char *) type, "year"))
- md->type = Metadata_type_year;
+ if (!strcmp((const char *) xml_type, "generic"))
+ type = Metadata_type_generic;
+ else if (!strcmp((const char *) xml_type, "year"))
+ type = Metadata_type_year;
else
{
- yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
+ yaz_log(YLOG_FATAL,
+ "Unknown value for metadata/type: %s", xml_type);
return 0;
}
}
else
- md->type = Metadata_type_generic;
+ type = Metadata_type_generic;
- if (merge)
+ if (xml_merge)
{
- if (!strcmp((const char *) merge, "no"))
- md->merge = Metadata_merge_no;
- else if (!strcmp((const char *) merge, "unique"))
- md->merge = Metadata_merge_unique;
- else if (!strcmp((const char *) merge, "longest"))
- md->merge = Metadata_merge_longest;
- else if (!strcmp((const char *) merge, "range"))
- md->merge = Metadata_merge_range;
- else if (!strcmp((const char *) merge, "all"))
- md->merge = Metadata_merge_all;
+ if (!strcmp((const char *) xml_merge, "no"))
+ merge = Metadata_merge_no;
+ else if (!strcmp((const char *) xml_merge, "unique"))
+ merge = Metadata_merge_unique;
+ else if (!strcmp((const char *) xml_merge, "longest"))
+ merge = Metadata_merge_longest;
+ else if (!strcmp((const char *) xml_merge, "range"))
+ merge = Metadata_merge_range;
+ else if (!strcmp((const char *) xml_merge, "all"))
+ merge = Metadata_merge_all;
else
{
- yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
+ yaz_log(YLOG_FATAL,
+ "Unknown value for metadata/merge: %s", xml_merge);
return 0;
}
}
else
- md->merge = Metadata_merge_no;
+ merge = Metadata_merge_no;
- if (sortkey && strcmp((const char *) sortkey, "no"))
+ // add a sortkey if so specified
+ if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
{
- struct conf_sortkey *sk = &r->sortkeys[sk_node];
- if (md->merge == Metadata_merge_no)
+ if (merge == Metadata_merge_no)
{
- yaz_log(YLOG_FATAL, "Can't specify sortkey on a non-merged field");
+ yaz_log(YLOG_FATAL,
+ "Can't specify sortkey on a non-merged field");
return 0;
}
- if (!strcmp((const char *) sortkey, "numeric"))
- sk->type = Metadata_sortkey_numeric;
- else if (!strcmp((const char *) sortkey, "skiparticle"))
- sk->type = Metadata_sortkey_skiparticle;
+ if (!strcmp((const char *) xml_sortkey, "numeric"))
+ sk_type = Metadata_sortkey_numeric;
+ else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
+ sk_type = Metadata_sortkey_skiparticle;
else
{
- yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
+ yaz_log(YLOG_FATAL,
+ "Unknown sortkey in metadata element: %s",
+ xml_sortkey);
return 0;
}
- sk->name = md->name;
- md->sortkey_offset = sk_node;
+ sortkey_offset = sk_node;
+
+ conf_service_add_sortkey(nmem, service, sk_node,
+ (const char *) xml_name, sk_type);
+
sk_node++;
}
else
- md->sortkey_offset = -1;
-
- xmlFree(name);
- xmlFree(brief);
- xmlFree(sortkey);
- xmlFree(merge);
- xmlFree(termlist);
- xmlFree(rank);
+ sortkey_offset = -1;
+
+ // metadata known, assign values
+ conf_service_add_metadata(nmem, service, md_node,
+ (const char *) xml_name,
+ type, merge,
+ brief, termlist, rank, sortkey_offset);
+
+ xmlFree(xml_name);
+ xmlFree(xml_brief);
+ xmlFree(xml_sortkey);
+ xmlFree(xml_merge);
+ xmlFree(xml_type);
+ xmlFree(xml_termlist);
+ xmlFree(xml_rank);
md_node++;
}
else
return 0;
}
}
- return r;
+ return service;
}
static char *parse_settings(xmlNode *node)
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->myurl = 0;
- r->zproxy_host = 0;
- r->zproxy_port = 0;
- r->service = 0;
- r->next = 0;
- r->settings = 0;
+ struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server));
+
+ server->host = 0;
+ server->port = 0;
+ server->proxy_host = 0;
+ server->proxy_port = 0;
+ server->myurl = 0;
+ //server->zproxy_host = 0;
+ //server->zproxy_port = 0;
+ server->service = 0;
+ server->next = 0;
+ server->settings = 0;
+
+#ifdef HAVE_ICU
+ server->icu_chn = 0;
+#endif // HAVE_ICU
+
for (n = node->children; n; n = n->next)
{
xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
if (port)
- r->port = atoi((const char *) port);
+ server->port = atoi((const char *) port);
if (host)
- r->host = nmem_strdup(nmem, (const char *) host);
+ server->host = nmem_strdup(nmem, (const char *) host);
xmlFree(port);
xmlFree(host);
}
xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
if (port)
- r->proxy_port = atoi((const char *) port);
+ server->proxy_port = atoi((const char *) port);
if (host)
- r->proxy_host = nmem_strdup(nmem, (const char *) host);
+ server->proxy_host = nmem_strdup(nmem, (const char *) host);
if (myurl)
- r->myurl = nmem_strdup(nmem, (const char *) myurl);
+ server->myurl = nmem_strdup(nmem, (const char *) myurl);
#ifdef GAGA
else
{
xmlFree(host);
xmlFree(myurl);
}
- else if (!strcmp((const char *) n->name, "zproxy"))
- {
- xmlChar *port = 0;
- xmlChar *host = 0;
-
- port = xmlGetProp(n, (xmlChar *) "port");
- host = xmlGetProp(n, (xmlChar *) "host");
-
- if (port)
- r->zproxy_port = atoi((const char *) port);
- if (host)
- r->zproxy_host = nmem_strdup(nmem, (const char *) host);
-
- xmlFree(port);
- xmlFree(host);
- }
else if (!strcmp((const char *) n->name, "settings"))
{
- if (r->settings)
+ if (server->settings)
{
yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
return 0;
}
- if (!(r->settings = parse_settings(n)))
+ if (!(server->settings = parse_settings(n)))
return 0;
}
+ else if (!strcmp((const char *) n->name, "icu_chain"))
+ {
+#ifdef HAVE_ICU
+ UErrorCode status = U_ZERO_ERROR;
+ struct icu_chain *chain = icu_chain_xml_config(n, &status);
+ if (!chain || U_FAILURE(status)){
+ //xmlDocPtr icu_doc = 0;
+ //xmlChar *xmlstr = 0;
+ //int size = 0;
+ //xmlDocDumpMemory(icu_doc, size);
+
+ yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
+ "<%s>\n ... \n</%s>",
+ n->name, n->name);
+ return 0;
+ }
+ server->icu_chn = chain;
+#else // HAVE_ICU
+ yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
+ "<%s>\n ... \n</%s>",
+ n->name, n->name);
+ yaz_log(YLOG_FATAL,
+ "But no ICU support compiled into pazpar2 server.");
+ yaz_log(YLOG_FATAL,
+ "Please install libicu36-dev and icu-doc or similar, "
+ "re-configure and re-compile");
+ return 0;
+#endif // HAVE_ICU
+ }
else if (!strcmp((const char *) n->name, "service"))
{
struct conf_service *s = parse_service(n);
if (!s)
return 0;
- r->service = s;
+ server->service = s;
}
else
{
return 0;
}
}
- return r;
+ return server;
}
-static xsltStylesheet *load_stylesheet(const char *fname)
+xsltStylesheet *conf_load_stylesheet(const char *fname)
{
char path[256];
- sprintf(path, "%s/%s", confdir, fname);
+ if (*fname == '/')
+ yaz_snprintf(path, sizeof(path), fname);
+ else
+ yaz_snprintf(path, sizeof(path), "%s/%s", confdir, fname);
return xsltParseStylesheetFile((xmlChar *) path);
}
-static void setup_marc(struct conf_retrievalprofile *r)
-{
- yaz_iconv_t cm;
- r->yaz_marc = yaz_marc_create();
- if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
- {
- yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
- return;
- }
- yaz_marc_iconv(r->yaz_marc, cm);
-}
-
-static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
-{
- struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
- xmlNode *n;
- struct conf_retrievalmap **rm = &r->maplist;
-
- r->requestsyntax = 0;
- r->native_syntax = Nativesyn_xml;
- r->native_format = Nativeform_na;
- r->native_encoding = 0;
- r->native_mapto = Nativemapto_na;
- r->yaz_marc = 0;
- r->maplist = 0;
- r->next = 0;
-
- for (n = node->children; n; n = n->next)
- {
- if (n->type != XML_ELEMENT_NODE)
- continue;
- if (!strcmp((const char *) n->name, "requestsyntax"))
- {
- xmlChar *content = xmlNodeGetContent(n);
- if (content)
- r->requestsyntax = nmem_strdup(nmem, (const char *) content);
- }
- else if (!strcmp((const char *) n->name, "nativesyntax"))
- {
- xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
- xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
- xmlChar *encoding = xmlGetProp(n, (xmlChar *) "encoding");
- xmlChar *mapto = xmlGetProp(n, (xmlChar *) "mapto");
- if (!name)
- {
- yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
- return 0;
- }
- if (encoding)
- r->native_encoding = (char *) encoding;
- if (!strcmp((const char *) name, "iso2709"))
- {
- r->native_syntax = Nativesyn_iso2709;
- // Set a few defaults, too
- r->native_format = Nativeform_marc21;
- r->native_mapto = Nativemapto_marcxml;
- if (!r->native_encoding)
- r->native_encoding = "marc-8";
- setup_marc(r);
- }
- else if (!strcmp((const char *) name, "xml"))
- r->native_syntax = Nativesyn_xml;
- else
- {
- yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
- return 0;
- }
- if (format)
- {
- if (!strcmp((const char *) format, "marc21")
- || !strcmp((const char *) format, "usmarc"))
- r->native_format = Nativeform_marc21;
- else
- {
- yaz_log(YLOG_WARN, "Unknown native format name %s", format);
- return 0;
- }
- }
- if (mapto)
- {
- if (!strcmp((const char *) mapto, "marcxml"))
- r->native_mapto = Nativemapto_marcxml;
- else if (!strcmp((const char *)mapto, "marcxchange"))
- r->native_mapto = Nativemapto_marcxchange;
- else
- {
- yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
- return 0;
- }
- }
- xmlFree(name);
- xmlFree(format);
- xmlFree(encoding);
- xmlFree(mapto);
- }
- else if (!strcmp((const char *) n->name, "map"))
- {
- struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
- xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
- xmlChar *charset = xmlGetProp(n, (xmlChar *) "charset");
- xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
- xmlChar *stylesheet = xmlGetProp(n, (xmlChar *) "stylesheet");
- memset(m, 0, sizeof(*m));
- if (type)
- {
- if (!strcmp((const char *) type, "xslt"))
- m->type = Map_xslt;
- else
- {
- yaz_log(YLOG_WARN, "Unknown map type: %s", type);
- return 0;
- }
- }
- if (charset)
- m->charset = nmem_strdup(nmem, (const char *) charset);
- if (format)
- m->format = nmem_strdup(nmem, (const char *) format);
- if (stylesheet)
- {
- if (!(m->stylesheet = load_stylesheet((char *) stylesheet)))
- return 0;
- }
- *rm = m;
- rm = &m->next;
- xmlFree(type);
- xmlFree(charset);
- xmlFree(format);
- xmlFree(stylesheet);
- }
- else
- {
- yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
- return 0;
- }
- }
-
- return r;
-}
-
static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
{
struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
{
xmlNode *n;
struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
- struct conf_retrievalprofile **rp = &r->retrievalprofiles;
r->servers = 0;
- r->retrievalprofiles = 0;
r->targetprofiles = 0;
for (n = root->children; n; n = n->next)
tmp->next = r->servers;
r->servers = tmp;
}
- else if (!strcmp((const char *) n->name, "retrievalprofile"))
- {
- if (!(*rp = parse_retrievalprofile(n)))
- return 0;
- rp = &(*rp)->next;
- }
else if (!strcmp((const char *) n->name, "targetprofiles"))
{
// It would be fun to be able to fix this sometime
if ((p = strrchr(fname, '/')))
{
int len = p - fname;
+ if (len >= sizeof(confdir))
+ len = sizeof(confdir)-1;
strncpy(confdir, fname, len);
confdir[len] = '\0';
}