1 /* $Id: res.c,v 1.48 2006-03-26 14:05:19 adam Exp $
2 Copyright (C) 1995-2005
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
34 #include <yaz/yaz-util.h>
35 #include <idzebra/res.h>
42 struct res_entry *next;
46 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 char *xstrdup_env(const char *src)
77 if (src[i] == '$' && src[i+1] == '{')
83 while (k < 127 && src[i] && !strchr(":}\n\r\f", src[i]))
84 envname[k++] = src[i++];
87 env_val = getenv(envname);
89 env_strlen += 1 + strlen(env_val);
92 while (src[i] && !strchr("}\n\r\f", src[i]))
100 dst = xmalloc(1 + env_strlen + i);
104 if (src[i] == '$' && src[i+1] == '{')
110 while(k < 127 && src[i] && !strchr(":}\n\r\f", src[i]))
111 envname[k++] = src[i++];
113 env_val = getenv(envname);
116 strcpy(dst+j, env_val);
117 j += strlen(env_val);
119 else if (src[i] == ':' && src[i+1] == '-')
122 while (src[i] && !strchr("}\n\r\f", src[i]))
125 while (src[i] && !strchr("}\n\r\f", src[i]))
137 ZEBRA_RES res_read_file(Res r, const char *fname)
139 struct res_entry *resp;
142 int val_size, val_max = 256;
148 fr = fopen(fname, "r");
151 yaz_log(YLOG_WARN|YLOG_ERRNO, "Cannot open `%s'", fname);
154 val_buf = (char*) xmalloc(val_max);
157 line = fgets(fr_buf, sizeof(fr_buf)-1, fr);
165 if (fr_buf[no] == 0 || fr_buf[no] == '\n' )
170 if (strchr(": \t", fr_buf[no]))
178 resp->name = (char*) xmalloc(no);
179 strcpy(resp->name, fr_buf);
181 while (strchr(" \t", fr_buf[no]))
186 if (fr_buf[no] == '\0' || strchr("\n\r\f", fr_buf[no]))
188 while (val_size > 0 &&
189 (val_buf[val_size-1] == ' ' ||
190 val_buf[val_size-1] == '\t'))
192 val_buf[val_size] = '\0';
193 resp->value = xstrdup_env(val_buf);
194 yaz_log(YLOG_DEBUG, "(name=%s,value=%s)",
195 resp->name, resp->value);
198 else if (fr_buf[no] == '\\' && strchr("\n\r\f", fr_buf[no+1]))
200 line = fgets(fr_buf, sizeof(fr_buf)-1, fr);
203 val_buf[val_size] = '\0';
204 resp->value = xstrdup_env(val_buf);
211 val_buf[val_size++] = fr_buf[no++];
212 if (val_size+1 >= val_max)
216 nb = (char*) xmalloc(val_max+=1024);
217 memcpy(nb, val_buf, val_size);
229 Res res_open(Res def_res, Res over_res)
232 r = (Res) xmalloc(sizeof(*r));
233 r->first = r->last = NULL;
234 r->def_res = def_res;
235 r->over_res = over_res;
239 void res_clear(Res r)
241 struct res_entry *re, *re1;
242 for (re = r->first; re; re=re1)
251 r->first = r->last = NULL;
254 void res_close(Res r)
263 const char *res_get_prefix(Res r, const char *name, const char *prefix,
271 if (strlen(name) + strlen(prefix) >= (sizeof(rname)-2))
273 strcpy(rname, prefix);
276 v = res_get(r, rname);
279 v = res_get(r, name);
285 const char *res_get(Res r, const char *name)
287 struct res_entry *re;
293 v = res_get(r->over_res, name);
297 for (re = r->first; re; re=re->next)
298 if (re->value && !yaz_matchstr(re->name, name))
301 return res_get(r->def_res, name);
304 const char *res_get_def(Res r, const char *name, const char *def)
308 if (!(t = res_get(r, name)))
310 yaz_log(YLOG_DEBUG, "CAUTION: Using default resource %s:%s", name, def);
317 int res_get_match(Res r, const char *name, const char *value, const char *s)
319 const char *cn = res_get(r, name);
323 if (cn && !yaz_matchstr(cn, value))
328 void res_set(Res r, const char *name, const char *value)
330 struct res_entry *re;
333 for (re = r->first; re; re=re->next)
334 if (re->value && !yaz_matchstr(re->name, name))
337 re->value = xstrdup_env(value);
341 re->name = xstrdup(name);
342 re->value = xstrdup_env(value);
345 int res_trav(Res r, const char *prefix, void *p,
346 void (*f)(void *p, const char *name, const char *value))
348 struct res_entry *re;
354 no = res_trav(r->over_res, prefix, p, f);
359 for (re = r->first; re; re=re->next)
361 if (l==0 || !memcmp(re->name, prefix, l))
363 (*f)(p, re->name, re->value);
367 return res_trav(r->def_res, prefix, p, f);
372 ZEBRA_RES res_write_file(Res r, const char *fname)
374 struct res_entry *re;
378 fr = fopen(fname, "w");
381 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Cannot create `%s'", fname);
385 for (re = r->first; re; re=re->next)
388 int lefts = strlen(re->name)+2;
391 fprintf(fr, "%s\n", re->name);
394 fprintf(fr, "%s: ", re->name);
395 while (lefts + strlen(re->value+no) > 78)
398 int ind = no+ 78-lefts;
401 if (re->value[ind] == ' ')
406 ind = no + 78 - lefts;
407 for (i = no; i != ind; i++)
408 putc(re->value[i], fr);
413 fprintf(fr, "%s\n", re->value+no);
420 ZEBRA_RES res_get_int(Res r, const char *name, int *val)
422 const char *cp = res_get(r, name);
425 if (sscanf(cp, "%d", val) == 1)
427 yaz_log(YLOG_WARN, "Expected integer for resource %s", name);
432 /* == pop ================================================================= */
433 Res res_add_over (Res p, Res t)
445 void res_remove_over (Res r)
452 void res_close_over (Res r)
457 res_close(r->over_res);
461 void res_add (Res r, const char *name, const char *value)
463 struct res_entry *re;
465 if ((name) && (value))
466 yaz_log (YLOG_RES, "res_add res=%p, name=%s, value=%s", r, name, value);
469 re->name = xstrdup (name);
470 re->value = xstrdup_env (value);
473 char **res_2_array (Res r)
475 struct res_entry *re;
482 list = xmalloc(sizeof(char *));
484 for (re = r->first; re; re=re->next) {
485 list = xrealloc(list, ((i+3) * sizeof(char *)));
486 list[i++] = strdup(re->name);
488 list[i++] = strdup(re->value);
490 list[i++] = strdup("");
491 yaz_log(YLOG_RES, "res2array: %s=%s",re->name, re->value);
497 char **res_get_array(Res r, const char* name)
499 struct res_entry *re;
506 list = xmalloc(sizeof(char *));
508 for (re = r->first; re; re=re->next)
509 if (re->value && !yaz_matchstr (re->name, name))
511 list = xrealloc(list, (i+2) * sizeof(char *));
512 list[i++] = xstrdup(re->value);
516 return (res_get_array(r->def_res, name));
522 void res_dump (Res r, int level)
524 struct res_entry *re;
529 for (re = r->first; re; re=re->next) {
530 printf("%*s - %s:='%s'\n",level * 4,"",re->name,re->value);
534 printf ("%*s DEF ",level * 4,"");
535 res_dump (r->def_res, level + 1);
539 printf ("%*s OVER ",level * 4,"");
540 res_dump (r->over_res, level + 1);