1 /* $Id: res.c,v 1.56 2007-01-15 15:10:26 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <yaz/yaz-util.h>
35 #include <idzebra/res.h>
42 struct res_entry *next;
47 struct res_entry *first, *last;
52 static Res res_incref(Res r)
59 static struct res_entry *add_entry(Res r)
61 struct res_entry *resp;
64 resp = r->last = r->first =
65 (struct res_entry *) xmalloc(sizeof(*resp));
68 resp = (struct res_entry *) xmalloc(sizeof(*resp));
76 static char *xstrdup_env(const char *src)
85 if (src[i] == '$' && src[i+1] == '{')
91 while (k < 127 && src[i] && !strchr(":}\n\r\f", src[i]))
92 envname[k++] = src[i++];
95 env_val = getenv(envname);
97 env_strlen += 1 + strlen(env_val);
100 while (src[i] && !strchr("}\n\r\f", src[i]))
108 dst = xmalloc(1 + env_strlen + i);
112 if (src[i] == '$' && src[i+1] == '{')
118 while(k < 127 && src[i] && !strchr(":}\n\r\f", src[i]))
119 envname[k++] = src[i++];
121 env_val = getenv(envname);
124 strcpy(dst+j, env_val);
125 j += strlen(env_val);
127 else if (src[i] == ':' && src[i+1] == '-')
130 while (src[i] && !strchr("}\n\r\f", src[i]))
133 while (src[i] && !strchr("}\n\r\f", src[i]))
145 ZEBRA_RES res_read_file(Res r, const char *fname)
147 struct res_entry *resp;
150 int val_size, val_max = 256;
156 fr = fopen(fname, "r");
159 yaz_log(YLOG_WARN|YLOG_ERRNO, "Cannot open `%s'", fname);
162 val_buf = (char*) xmalloc(val_max);
165 line = fgets(fr_buf, sizeof(fr_buf)-1, fr);
173 if (fr_buf[no] == 0 || fr_buf[no] == '\n' )
178 if (strchr(": \t", fr_buf[no]))
186 resp->name = (char*) xmalloc(no);
187 strcpy(resp->name, fr_buf);
189 while (strchr(" \t", fr_buf[no]))
194 if (fr_buf[no] == '\0' || strchr("\n\r\f", fr_buf[no]))
196 while (val_size > 0 &&
197 (val_buf[val_size-1] == ' ' ||
198 val_buf[val_size-1] == '\t'))
200 val_buf[val_size] = '\0';
201 resp->value = xstrdup_env(val_buf);
202 yaz_log(YLOG_DEBUG, "(name=%s,value=%s)",
203 resp->name, resp->value);
206 else if (fr_buf[no] == '\\' && strchr("\n\r\f", fr_buf[no+1]))
208 line = fgets(fr_buf, sizeof(fr_buf)-1, fr);
211 val_buf[val_size] = '\0';
212 resp->value = xstrdup_env(val_buf);
219 val_buf[val_size++] = fr_buf[no++];
220 if (val_size+1 >= val_max)
224 nb = (char*) xmalloc(val_max+=1024);
225 memcpy(nb, val_buf, val_size);
238 Res res_open(Res def_res, Res over_res)
241 r = (Res) xmalloc(sizeof(*r));
244 r->first = r->last = NULL;
245 r->def_res = res_incref(def_res);
246 r->over_res = res_incref(over_res);
250 void res_clear(Res r)
252 struct res_entry *re, *re1;
253 for (re = r->first; re; re=re1)
262 r->first = r->last = NULL;
265 void res_close(Res r)
267 if (r && --(r->ref_count) == 0)
270 res_close(r->def_res);
271 res_close(r->over_res);
276 const char *res_get_prefix(Res r, const char *name, const char *prefix,
284 if (strlen(name) + strlen(prefix) >= (sizeof(rname)-2))
286 strcpy(rname, prefix);
289 v = res_get(r, rname);
292 v = res_get(r, name);
298 const char *res_get(Res r, const char *name)
300 struct res_entry *re;
306 v = res_get(r->over_res, name);
310 for (re = r->first; re; re=re->next)
311 if (re->value && !yaz_matchstr(re->name, name))
314 return res_get(r->def_res, name);
317 const char *res_get_def(Res r, const char *name, const char *def)
321 if (!(t = res_get(r, name)))
324 yaz_log(YLOG_DEBUG, "Using default resource %s:%s", name, def);
331 int res_get_match(Res r, const char *name, const char *value, const char *s)
333 const char *cn = res_get(r, name);
337 if (cn && !yaz_matchstr(cn, value))
342 void res_set(Res r, const char *name, const char *value)
344 struct res_entry *re;
349 for (re = r->first; re; re=re->next)
350 if (re->value && !yaz_matchstr(re->name, name))
353 re->value = xstrdup_env(value);
357 re->name = xstrdup(name);
358 re->value = xstrdup_env(value);
361 int res_trav(Res r, const char *prefix, void *p,
362 void (*f)(void *p, const char *name, const char *value))
364 struct res_entry *re;
370 no = res_trav(r->over_res, prefix, p, f);
375 for (re = r->first; re; re=re->next)
377 if (l==0 || !memcmp(re->name, prefix, l))
379 (*f)(p, re->name, re->value);
383 return res_trav(r->def_res, prefix, p, f);
388 ZEBRA_RES res_write_file(Res r, const char *fname)
390 struct res_entry *re;
394 fr = fopen(fname, "w");
397 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Cannot create `%s'", fname);
401 for (re = r->first; re; re=re->next)
404 int lefts = strlen(re->name)+2;
407 fprintf(fr, "%s\n", re->name);
410 fprintf(fr, "%s: ", re->name);
411 while (lefts + strlen(re->value+no) > 78)
414 int ind = no+ 78-lefts;
417 if (re->value[ind] == ' ')
422 ind = no + 78 - lefts;
423 for (i = no; i != ind; i++)
424 putc(re->value[i], fr);
429 fprintf(fr, "%s\n", re->value+no);
436 ZEBRA_RES res_get_int(Res r, const char *name, int *val)
438 const char *cp = res_get(r, name);
441 if (sscanf(cp, "%d", val) == 1)
443 yaz_log(YLOG_WARN, "Expected integer for resource %s", name);
448 /* == pop ================================================================= */
449 Res res_add_over (Res p, Res t)
461 void res_remove_over (Res r)
468 void res_close_over (Res r)
473 res_close(r->over_res);
477 void res_add (Res r, const char *name, const char *value)
479 struct res_entry *re;
483 yaz_log (YLOG_RES, "res_add res=%p, name=%s, value=%s", r, name, value);
485 re->name = xstrdup (name);
486 re->value = xstrdup_env (value);
489 char **res_2_array (Res r)
491 struct res_entry *re;
498 list = xmalloc(sizeof(char *));
500 for (re = r->first; re; re=re->next) {
501 list = xrealloc(list, ((i+3) * sizeof(char *)));
502 list[i++] = strdup(re->name);
504 list[i++] = strdup(re->value);
506 list[i++] = strdup("");
507 yaz_log(YLOG_RES, "res2array: %s=%s",re->name, re->value);
513 char **res_get_array(Res r, const char* name)
515 struct res_entry *re;
522 list = xmalloc(sizeof(char *));
524 for (re = r->first; re; re=re->next)
525 if (re->value && !yaz_matchstr (re->name, name))
527 list = xrealloc(list, (i+2) * sizeof(char *));
528 list[i++] = xstrdup(re->value);
532 return (res_get_array(r->def_res, name));
538 void res_dump (Res r, int level)
540 struct res_entry *re;
545 for (re = r->first; re; re=re->next) {
546 printf("%*s - %s:='%s'\n",level * 4,"",re->name,re->value);
550 printf ("%*s DEF ",level * 4,"");
551 res_dump (r->def_res, level + 1);
555 printf ("%*s OVER ",level * 4,"");
556 res_dump (r->over_res, level + 1);
560 int res_check(Res r_i, Res r_v)
562 struct res_entry *e_i;
565 for (e_i = r_i->first; e_i; e_i = e_i->next)
567 struct res_entry *e_v;
568 for (e_v = r_v->first; e_v; e_v = e_v->next)
570 int prefix_allowed = 0;
571 int suffix_allowed = 0;
572 const char *name = e_i->name;
573 size_t name_len = strlen(e_i->name);
575 const char *first_dot = 0;
576 const char *second_dot = 0;
578 if (strchr(e_v->value, 'p'))
580 if (strchr(e_v->value, 's'))
583 first_dot = strchr(name, '.');
584 if (prefix_allowed && first_dot)
587 name_len = strlen(name);
589 second_dot = strchr(name, '.');
590 if (suffix_allowed && second_dot)
592 name_len = second_dot - name;
594 if (name_len < sizeof(namez)-1)
596 memcpy(namez, name, name_len);
597 namez[name_len] = '\0';
598 if (!yaz_matchstr(namez, e_v->name))
601 /* for case 'a.b' we have to check 'a' as well */
602 if (prefix_allowed && suffix_allowed && first_dot && !second_dot)
605 name_len = first_dot - name;
606 if (name_len < sizeof(namez)-1)
608 memcpy(namez, name, name_len);
609 namez[name_len] = '\0';
610 if (!yaz_matchstr(namez, e_v->name))
617 yaz_log(YLOG_WARN, "The following setting is unrecognized: %s",
628 * indent-tabs-mode: nil
630 * vim: shiftwidth=4 tabstop=8 expandtab