1 /* $Id: res.c,v 1.34 2004-01-22 11:27:22 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
35 #include <yaz/yaz-util.h>
40 struct res_entry *next;
44 struct res_entry *first, *last;
51 static struct res_entry *add_entry (Res r)
53 struct res_entry *resp;
56 resp = r->last = r->first =
57 (struct res_entry *) xmalloc (sizeof(*resp));
60 resp = (struct res_entry *) xmalloc (sizeof(*resp));
68 static void reread (Res r)
70 struct res_entry *resp;
73 int val_size, val_max = 256;
80 val_buf = (char*) xmalloc (val_max);
85 fr = fopen (r->name, "r");
88 logf (LOG_WARN|LOG_ERRNO, "Cannot open `%s'", r->name);
93 line = fgets (fr_buf, sizeof(fr_buf)-1, fr);
100 while (fr_buf[no] && fr_buf[no] != '\n')
104 resp = add_entry (r);
105 resp->name = (char*) xmalloc (no+1);
107 strcpy (resp->name, fr_buf);
114 if (fr_buf[no] == 0 || fr_buf[no] == '\n' )
119 if (strchr (": \t", fr_buf[no]))
126 resp = add_entry (r);
127 resp->name = (char*) xmalloc (no);
128 strcpy (resp->name, fr_buf);
130 while (strchr (" \t", fr_buf[no]))
135 if (fr_buf[no] == '\0' || strchr("\n\r\f", fr_buf[no]))
137 while (val_size > 0 &&
138 (val_buf[val_size-1] == ' ' ||
139 val_buf[val_size-1] == '\t'))
141 val_buf[val_size++] = '\0';
142 resp->value = (char*) xmalloc (val_size);
143 strcpy (resp->value, val_buf);
144 logf (LOG_DEBUG, "(name=%s,value=%s)",
145 resp->name, resp->value);
148 else if (fr_buf[no] == '\\' && strchr ("\n\r\f", fr_buf[no+1]))
150 line = fgets (fr_buf, sizeof(fr_buf)-1, fr);
153 resp->value = (char*) xmalloc (val_size);
154 strcpy (resp->value, val_buf);
161 val_buf[val_size++] = fr_buf[no++];
162 if (val_size+1 >= val_max)
166 nb = (char*) xmalloc (val_max+=1024);
167 memcpy (nb, val_buf, val_size);
179 Res res_open (const char *name, Res def_res, Res over_res)
186 if (access (name, 4))
188 if (access (name, R_OK))
191 logf (LOG_WARN|LOG_ERRNO, "Cannot open `%s'", name);
195 r = (Res) xmalloc (sizeof(*r));
197 r->first = r->last = NULL;
199 r->name = xstrdup (name);
202 r->def_res = def_res;
203 r->over_res = over_res;
207 void res_close (Res r)
213 struct res_entry *re, *re1;
214 for (re = r->first; re; re=re1)
228 const char *res_get_prefix (Res r, const char *name, const char *prefix,
236 if (strlen(name) + strlen(prefix) >= (sizeof(rname)-2))
238 strcpy(rname, prefix);
241 v = res_get(r, rname);
244 v = res_get(r, name);
250 const char *res_get (Res r, const char *name)
252 struct res_entry *re;
258 v = res_get(r->over_res, name);
264 for (re = r->first; re; re=re->next)
265 if (re->value && !yaz_matchstr (re->name, name))
268 return res_get (r->def_res, name);
271 const char *res_get_def (Res r, const char *name, const char *def)
275 if (!(t = res_get (r, name)))
277 logf (LOG_DEBUG, "CAUTION: Using default resource %s:%s", name, def);
284 int res_get_match (Res r, const char *name, const char *value, const char *s)
286 const char *cn = res_get (r, name);
290 if (cn && !yaz_matchstr (cn, value))
295 void res_set (Res r, const char *name, const char *value)
297 struct res_entry *re;
302 for (re = r->first; re; re=re->next)
303 if (re->value && !yaz_matchstr (re->name, name))
306 re->value = xstrdup (value);
310 re->name = xstrdup (name);
311 re->value = xstrdup (value);
314 int res_trav (Res r, const char *prefix, void *p,
315 void (*f)(void *p, const char *name, const char *value))
317 struct res_entry *re;
327 for (re = r->first; re; re=re->next)
329 if (l==0 || !memcmp (re->name, prefix, l))
331 (*f)(p, re->name, re->value);
335 return res_trav (r->def_res, prefix, p, f);
340 int res_write (Res r)
342 struct res_entry *re;
349 return 0; /* ok, this was not from a file */
350 fr = fopen (r->name, "w");
353 logf (LOG_FATAL|LOG_ERRNO, "Cannot create `%s'", r->name);
357 for (re = r->first; re; re=re->next)
360 int lefts = strlen(re->name)+2;
363 fprintf (fr, "%s\n", re->name);
366 fprintf (fr, "%s: ", re->name);
367 while (lefts + strlen(re->value+no) > 78)
370 int ind = no+ 78-lefts;
373 if (re->value[ind] == ' ')
378 ind = no + 78 - lefts;
379 for (i = no; i != ind; i++)
380 putc (re->value[i], fr);
381 fprintf (fr, "\\\n");
385 fprintf (fr, "%s\n", re->value+no);