1 /* $Id: res.c,v 1.33 2002-10-22 09:37:56 heikki 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);
84 fr = fopen (r->name, "r");
87 logf (LOG_WARN|LOG_ERRNO, "Cannot open `%s'", r->name);
92 line = fgets (fr_buf, sizeof(fr_buf)-1, fr);
99 while (fr_buf[no] && fr_buf[no] != '\n')
103 resp = add_entry (r);
104 resp->name = (char*) xmalloc (no+1);
106 strcpy (resp->name, fr_buf);
113 if (fr_buf[no] == 0 || fr_buf[no] == '\n' )
118 if (strchr (": \t", fr_buf[no]))
125 resp = add_entry (r);
126 resp->name = (char*) xmalloc (no);
127 strcpy (resp->name, fr_buf);
129 while (strchr (" \t", fr_buf[no]))
134 if (fr_buf[no] == '\0' || strchr("\n\r\f", fr_buf[no]))
136 while (val_size > 0 &&
137 (val_buf[val_size-1] == ' ' ||
138 val_buf[val_size-1] == '\t'))
140 val_buf[val_size++] = '\0';
141 resp->value = (char*) xmalloc (val_size);
142 strcpy (resp->value, val_buf);
143 logf (LOG_DEBUG, "(name=%s,value=%s)",
144 resp->name, resp->value);
147 else if (fr_buf[no] == '\\' && strchr ("\n\r\f", fr_buf[no+1]))
149 line = fgets (fr_buf, sizeof(fr_buf)-1, fr);
152 resp->value = (char*) xmalloc (val_size);
153 strcpy (resp->value, val_buf);
160 val_buf[val_size++] = fr_buf[no++];
161 if (val_size+1 >= val_max)
165 nb = (char*) xmalloc (val_max+=1024);
166 memcpy (nb, val_buf, val_size);
178 Res res_open (const char *name, Res def_res)
185 if (access (name, 4))
187 if (access (name, R_OK))
190 logf (LOG_WARN|LOG_ERRNO, "Cannot open `%s'", name);
194 r = (Res) xmalloc (sizeof(*r));
196 r->first = r->last = NULL;
198 r->name = xstrdup (name);
201 r->def_res = def_res;
205 void res_close (Res r)
211 struct res_entry *re, *re1;
212 for (re = r->first; re; re=re1)
226 char *res_get (Res r, const char *name)
228 struct res_entry *re;
234 for (re = r->first; re; re=re->next)
235 if (re->value && !yaz_matchstr (re->name, name))
238 return res_get (r->def_res, name);
241 char *res_get_def (Res r, const char *name, char *def)
245 if (!(t = res_get (r, name)))
247 logf (LOG_DEBUG, "CAUTION: Using default resource %s:%s", name, def);
254 int res_get_match (Res r, const char *name, const char *value, const char *s)
256 const char *cn = res_get (r, name);
260 if (cn && !yaz_matchstr (cn, value))
265 void res_put (Res r, const char *name, const char *value)
267 struct res_entry *re;
272 for (re = r->first; re; re=re->next)
273 if (re->value && !yaz_matchstr (re->name, name))
276 re->value = xstrdup (value);
280 re->name = xstrdup (name);
281 re->value = xstrdup (value);
284 int res_trav (Res r, const char *prefix, void *p,
285 void (*f)(void *p, const char *name, const char *value))
287 struct res_entry *re;
297 for (re = r->first; re; re=re->next)
299 if (l==0 || !memcmp (re->name, prefix, l))
301 (*f)(p, re->name, re->value);
305 return res_trav (r->def_res, prefix, p, f);
310 int res_write (Res r)
312 struct res_entry *re;
319 return 0; /* ok, this was not from a file */
320 fr = fopen (r->name, "w");
323 logf (LOG_FATAL|LOG_ERRNO, "Cannot create `%s'", r->name);
327 for (re = r->first; re; re=re->next)
330 int lefts = strlen(re->name)+2;
333 fprintf (fr, "%s\n", re->name);
336 fprintf (fr, "%s: ", re->name);
337 while (lefts + strlen(re->value+no) > 78)
340 int ind = no+ 78-lefts;
343 if (re->value[ind] == ' ')
348 ind = no + 78 - lefts;
349 for (i = no; i != ind; i++)
350 putc (re->value[i], fr);
351 fprintf (fr, "\\\n");
355 fprintf (fr, "%s\n", re->value+no);