1 /* $Id: res.c,v 1.32 2002-09-09 09:35:48 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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;
50 static struct res_entry *add_entry (Res r)
52 struct res_entry *resp;
55 resp = r->last = r->first =
56 (struct res_entry *) xmalloc (sizeof(*resp));
59 resp = (struct res_entry *) xmalloc (sizeof(*resp));
67 static void reread (Res r)
69 struct res_entry *resp;
72 int val_size, val_max = 256;
79 val_buf = (char*) xmalloc (val_max);
81 fr = fopen (r->name, "r");
84 logf (LOG_WARN|LOG_ERRNO, "Cannot open `%s'", r->name);
89 line = fgets (fr_buf, sizeof(fr_buf)-1, fr);
96 while (fr_buf[no] && fr_buf[no] != '\n')
100 resp = add_entry (r);
101 resp->name = (char*) xmalloc (no+1);
103 strcpy (resp->name, fr_buf);
110 if (fr_buf[no] == 0 || fr_buf[no] == '\n' )
115 if (strchr (": \t", fr_buf[no]))
122 resp = add_entry (r);
123 resp->name = (char*) xmalloc (no);
124 strcpy (resp->name, fr_buf);
126 while (strchr (" \t", fr_buf[no]))
131 if (fr_buf[no] == '\0' || strchr("\n\r\f", fr_buf[no]))
133 while (val_size > 0 &&
134 (val_buf[val_size-1] == ' ' ||
135 val_buf[val_size-1] == '\t'))
137 val_buf[val_size++] = '\0';
138 resp->value = (char*) xmalloc (val_size);
139 strcpy (resp->value, val_buf);
140 logf (LOG_DEBUG, "(name=%s,value=%s)",
141 resp->name, resp->value);
144 else if (fr_buf[no] == '\\' && strchr ("\n\r\f", fr_buf[no+1]))
146 line = fgets (fr_buf, sizeof(fr_buf)-1, fr);
149 resp->value = (char*) xmalloc (val_size);
150 strcpy (resp->value, val_buf);
157 val_buf[val_size++] = fr_buf[no++];
158 if (val_size+1 >= val_max)
162 nb = (char*) xmalloc (val_max+=1024);
163 memcpy (nb, val_buf, val_size);
175 Res res_open (const char *name, Res def_res)
179 if (access (name, 4))
181 if (access (name, R_OK))
184 logf (LOG_WARN|LOG_ERRNO, "Cannot open `%s'", name);
187 r = (Res) xmalloc (sizeof(*r));
189 r->first = r->last = NULL;
190 r->name = xstrdup (name);
191 r->def_res = def_res;
195 void res_close (Res r)
201 struct res_entry *re, *re1;
202 for (re = r->first; re; re=re1)
216 char *res_get (Res r, const char *name)
218 struct res_entry *re;
224 for (re = r->first; re; re=re->next)
225 if (re->value && !yaz_matchstr (re->name, name))
228 return res_get (r->def_res, name);
231 char *res_get_def (Res r, const char *name, char *def)
235 if (!(t = res_get (r, name)))
237 logf (LOG_DEBUG, "CAUTION: Using default resource %s:%s", name, def);
244 int res_get_match (Res r, const char *name, const char *value, const char *s)
246 const char *cn = res_get (r, name);
250 if (cn && !yaz_matchstr (cn, value))
255 void res_put (Res r, const char *name, const char *value)
257 struct res_entry *re;
262 for (re = r->first; re; re=re->next)
263 if (re->value && !yaz_matchstr (re->name, name))
266 re->value = xstrdup (value);
270 re->name = xstrdup (name);
271 re->value = xstrdup (value);
274 int res_trav (Res r, const char *prefix, void *p,
275 void (*f)(void *p, const char *name, const char *value))
277 struct res_entry *re;
287 for (re = r->first; re; re=re->next)
289 if (l==0 || !memcmp (re->name, prefix, l))
291 (*f)(p, re->name, re->value);
295 return res_trav (r->def_res, prefix, p, f);
300 int res_write (Res r)
302 struct res_entry *re;
308 fr = fopen (r->name, "w");
311 logf (LOG_FATAL|LOG_ERRNO, "Cannot create `%s'", r->name);
315 for (re = r->first; re; re=re->next)
318 int lefts = strlen(re->name)+2;
321 fprintf (fr, "%s\n", re->name);
324 fprintf (fr, "%s: ", re->name);
325 while (lefts + strlen(re->value+no) > 78)
328 int ind = no+ 78-lefts;
331 if (re->value[ind] == ' ')
336 ind = no + 78 - lefts;
337 for (i = no; i != ind; i++)
338 putc (re->value[i], fr);
339 fprintf (fr, "\\\n");
343 fprintf (fr, "%s\n", re->value+no);