1 /* $Id: res.c,v 1.37.2.1 2004-11-26 11:06:13 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
38 struct res_entry *next;
42 struct res_entry *first, *last;
49 static struct res_entry *add_entry (Res r)
51 struct res_entry *resp;
54 resp = r->last = r->first =
55 (struct res_entry *) xmalloc (sizeof(*resp));
58 resp = (struct res_entry *) xmalloc (sizeof(*resp));
66 static char *xstrdup_env(const char *src)
75 if (src[i] == '$' && src[i+1] == '{')
81 while (k < 127 && src[i] && !strchr(":}\n\r\f", src[i]))
82 envname[k++] = src[i++];
85 env_val = getenv(envname);
87 env_strlen += 1 + strlen(env_val);
90 while (src[i] && !strchr("}\n\r\f", src[i]))
98 dst = xmalloc(1 + env_strlen + i);
102 if (src[i] == '$' && src[i+1] == '{')
108 while(k < 127 && src[i] && !strchr(":}\n\r\f", src[i]))
109 envname[k++] = src[i++];
111 env_val = getenv(envname);
114 strcpy(dst+j, env_val);
115 j += strlen(env_val);
117 else if (src[i] == ':' && src[i+1] == '-')
120 while (src[i] && !strchr("}\n\r\f", src[i]))
123 while (src[i] && !strchr("}\n\r\f", src[i]))
135 static void reread (Res r)
137 struct res_entry *resp;
140 int val_size, val_max = 256;
150 fr = fopen (r->name, "r");
153 logf (LOG_WARN|LOG_ERRNO, "Cannot open `%s'", r->name);
156 val_buf = (char*) xmalloc (val_max);
159 line = fgets (fr_buf, sizeof(fr_buf)-1, fr);
166 while (fr_buf[no] && fr_buf[no] != '\n')
170 resp = add_entry (r);
171 resp->name = (char*) xmalloc (no+1);
173 strcpy (resp->name, fr_buf);
180 if (fr_buf[no] == 0 || fr_buf[no] == '\n' )
185 if (strchr (": \t", fr_buf[no]))
192 resp = add_entry (r);
193 resp->name = (char*) xmalloc (no);
194 strcpy (resp->name, fr_buf);
196 while (strchr (" \t", fr_buf[no]))
201 if (fr_buf[no] == '\0' || strchr("\n\r\f", fr_buf[no]))
203 while (val_size > 0 &&
204 (val_buf[val_size-1] == ' ' ||
205 val_buf[val_size-1] == '\t'))
207 val_buf[val_size] = '\0';
208 resp->value = xstrdup_env(val_buf);
209 logf (LOG_DEBUG, "(name=%s,value=%s)",
210 resp->name, resp->value);
213 else if (fr_buf[no] == '\\' && strchr ("\n\r\f", fr_buf[no+1]))
215 line = fgets (fr_buf, sizeof(fr_buf)-1, fr);
218 val_buf[val_size] = '\0';
219 resp->value = xstrdup_env(val_buf);
226 val_buf[val_size++] = fr_buf[no++];
227 if (val_size+1 >= val_max)
231 nb = (char*) xmalloc (val_max+=1024);
232 memcpy (nb, val_buf, val_size);
244 Res res_open (const char *name, Res def_res, Res over_res)
251 if (access (name, 4))
253 if (access (name, R_OK))
256 logf (LOG_WARN|LOG_ERRNO, "Cannot open `%s'", name);
260 r = (Res) xmalloc (sizeof(*r));
262 r->first = r->last = NULL;
264 r->name = xstrdup (name);
267 r->def_res = def_res;
268 r->over_res = over_res;
272 void res_close (Res r)
278 struct res_entry *re, *re1;
279 for (re = r->first; re; re=re1)
293 const char *res_get_prefix (Res r, const char *name, const char *prefix,
301 if (strlen(name) + strlen(prefix) >= (sizeof(rname)-2))
303 strcpy(rname, prefix);
306 v = res_get(r, rname);
309 v = res_get(r, name);
315 const char *res_get (Res r, const char *name)
317 struct res_entry *re;
323 v = res_get(r->over_res, name);
329 for (re = r->first; re; re=re->next)
330 if (re->value && !yaz_matchstr (re->name, name))
333 return res_get (r->def_res, name);
336 const char *res_get_def (Res r, const char *name, const char *def)
340 if (!(t = res_get (r, name)))
342 logf (LOG_DEBUG, "CAUTION: Using default resource %s:%s", name, def);
349 int res_get_match (Res r, const char *name, const char *value, const char *s)
351 const char *cn = res_get (r, name);
355 if (cn && !yaz_matchstr (cn, value))
360 void res_set (Res r, const char *name, const char *value)
362 struct res_entry *re;
367 for (re = r->first; re; re=re->next)
368 if (re->value && !yaz_matchstr (re->name, name))
371 re->value = xstrdup_env (value);
375 re->name = xstrdup (name);
376 re->value = xstrdup_env (value);
379 int res_trav (Res r, const char *prefix, void *p,
380 void (*f)(void *p, const char *name, const char *value))
382 struct res_entry *re;
392 for (re = r->first; re; re=re->next)
394 if (l==0 || !memcmp (re->name, prefix, l))
396 (*f)(p, re->name, re->value);
400 return res_trav (r->def_res, prefix, p, f);
405 int res_write (Res r)
407 struct res_entry *re;
414 return 0; /* ok, this was not from a file */
415 fr = fopen (r->name, "w");
418 logf (LOG_FATAL|LOG_ERRNO, "Cannot create `%s'", r->name);
422 for (re = r->first; re; re=re->next)
425 int lefts = strlen(re->name)+2;
428 fprintf (fr, "%s\n", re->name);
431 fprintf (fr, "%s: ", re->name);
432 while (lefts + strlen(re->value+no) > 78)
435 int ind = no+ 78-lefts;
438 if (re->value[ind] == ' ')
443 ind = no + 78 - lefts;
444 for (i = no; i != ind; i++)
445 putc (re->value[i], fr);
446 fprintf (fr, "\\\n");
450 fprintf (fr, "%s\n", re->value+no);