1 /* $Id: res.c,v 1.58 2007-05-16 10:57:06 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/tokenizer.h>
35 #include <yaz/yaz-util.h>
36 #include <idzebra/res.h>
43 struct res_entry *next;
48 struct res_entry *first, *last;
53 static Res res_incref(Res r)
60 static struct res_entry *add_entry(Res r)
62 struct res_entry *resp;
65 resp = r->last = r->first =
66 (struct res_entry *) xmalloc(sizeof(*resp));
69 resp = (struct res_entry *) xmalloc(sizeof(*resp));
77 static char *xstrdup_env(const char *src)
86 if (src[i] == '$' && src[i+1] == '{')
92 while (k < 127 && src[i] && !strchr(":}\n\r\f", src[i]))
93 envname[k++] = src[i++];
96 env_val = getenv(envname);
98 env_strlen += 1 + strlen(env_val);
101 while (src[i] && !strchr("}\n\r\f", src[i]))
109 dst = xmalloc(1 + env_strlen + i);
113 if (src[i] == '$' && src[i+1] == '{')
119 while(k < 127 && src[i] && !strchr(":}\n\r\f", src[i]))
120 envname[k++] = src[i++];
122 env_val = getenv(envname);
125 strcpy(dst+j, env_val);
126 j += strlen(env_val);
128 else if (src[i] == ':' && src[i+1] == '-')
131 while (src[i] && !strchr("}\n\r\f", src[i]))
134 while (src[i] && !strchr("}\n\r\f", src[i]))
146 ZEBRA_RES res_read_file(Res r, const char *fname)
153 fr = fopen(fname, "r");
156 yaz_log(YLOG_WARN|YLOG_ERRNO, "Cannot open `%s'", fname);
164 WRBUF wrbuf_val = wrbuf_alloc();
165 yaz_tok_cfg_t yt = yaz_tok_cfg_create();
167 while ((line = fgets(fr_buf, sizeof(fr_buf)-1, fr)))
169 yaz_tok_parse_t tp = yaz_tok_parse_buf(yt, line);
170 int t = yaz_tok_move(tp);
172 if (t == YAZ_TOK_STRING)
175 struct res_entry *resp;
176 const char *cp = yaz_tok_parse_string(tp);
177 const char *cp1 = strchr(cp, ':');
181 yaz_log(YLOG_FATAL, "%s:%d missing colon after '%s'",
188 resp->name = xmalloc(sz + 1);
189 memcpy(resp->name, cp, sz);
190 resp->name[sz] = '\0';
192 wrbuf_rewind(wrbuf_val);
197 wrbuf_puts(wrbuf_val, cp1+1);
202 t = yaz_tok_move(tp);
204 if (t != YAZ_TOK_STRING)
206 resp->value = xstrdup("");
207 yaz_log(YLOG_FATAL, "%s:%d missing value after '%s'",
208 fname, lineno, resp->name);
212 wrbuf_puts(wrbuf_val, yaz_tok_parse_string(tp));
214 while ((t=yaz_tok_move(tp)) == YAZ_TOK_STRING)
216 wrbuf_putc(wrbuf_val, ' ');
217 wrbuf_puts(wrbuf_val, yaz_tok_parse_string(tp));
219 resp->value = xstrdup_env(wrbuf_cstr(wrbuf_val));
220 /* printf("name=%s value=%s\n", resp->name, resp->value); */
223 yaz_tok_parse_destroy(tp);
226 yaz_tok_cfg_destroy(yt);
227 wrbuf_destroy(wrbuf_val);
234 Res res_open(Res def_res, Res over_res)
237 r = (Res) xmalloc(sizeof(*r));
240 r->first = r->last = NULL;
241 r->def_res = res_incref(def_res);
242 r->over_res = res_incref(over_res);
246 void res_clear(Res r)
248 struct res_entry *re, *re1;
249 for (re = r->first; re; re=re1)
258 r->first = r->last = NULL;
261 void res_close(Res r)
263 if (r && --(r->ref_count) == 0)
266 res_close(r->def_res);
267 res_close(r->over_res);
272 const char *res_get_prefix(Res r, const char *name, const char *prefix,
280 if (strlen(name) + strlen(prefix) >= (sizeof(rname)-2))
282 strcpy(rname, prefix);
285 v = res_get(r, rname);
288 v = res_get(r, name);
294 const char *res_get(Res r, const char *name)
296 struct res_entry *re;
302 v = res_get(r->over_res, name);
306 for (re = r->first; re; re=re->next)
307 if (re->value && !yaz_matchstr(re->name, name))
310 return res_get(r->def_res, name);
313 const char *res_get_def(Res r, const char *name, const char *def)
317 if (!(t = res_get(r, name)))
320 yaz_log(YLOG_DEBUG, "Using default resource %s:%s", name, def);
327 int res_get_match(Res r, const char *name, const char *value, const char *s)
329 const char *cn = res_get(r, name);
333 if (cn && !yaz_matchstr(cn, value))
338 void res_set(Res r, const char *name, const char *value)
340 struct res_entry *re;
345 for (re = r->first; re; re=re->next)
346 if (re->value && !yaz_matchstr(re->name, name))
349 re->value = xstrdup_env(value);
353 re->name = xstrdup(name);
354 re->value = xstrdup_env(value);
357 int res_trav(Res r, const char *prefix, void *p,
358 void (*f)(void *p, const char *name, const char *value))
360 struct res_entry *re;
366 no = res_trav(r->over_res, prefix, p, f);
371 for (re = r->first; re; re=re->next)
373 if (l==0 || !memcmp(re->name, prefix, l))
375 (*f)(p, re->name, re->value);
379 return res_trav(r->def_res, prefix, p, f);
384 ZEBRA_RES res_write_file(Res r, const char *fname)
386 struct res_entry *re;
390 fr = fopen(fname, "w");
393 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Cannot create `%s'", fname);
397 for (re = r->first; re; re=re->next)
400 int lefts = strlen(re->name)+2;
403 fprintf(fr, "%s\n", re->name);
406 fprintf(fr, "%s: ", re->name);
407 while (lefts + strlen(re->value+no) > 78)
410 int ind = no+ 78-lefts;
413 if (re->value[ind] == ' ')
418 ind = no + 78 - lefts;
419 for (i = no; i != ind; i++)
420 putc(re->value[i], fr);
425 fprintf(fr, "%s\n", re->value+no);
432 ZEBRA_RES res_get_int(Res r, const char *name, int *val)
434 const char *cp = res_get(r, name);
437 if (sscanf(cp, "%d", val) == 1)
439 yaz_log(YLOG_WARN, "Expected integer for resource %s", name);
445 void res_add(Res r, const char *name, const char *value)
447 struct res_entry *re;
451 yaz_log(YLOG_RES, "res_add res=%p, name=%s, value=%s", r, name, value);
453 re->name = xstrdup(name);
454 re->value = xstrdup_env(value);
457 void res_dump(Res r, int level)
459 struct res_entry *re;
464 for (re = r->first; re; re=re->next) {
465 printf("%*s - %s:='%s'\n",level * 4,"",re->name,re->value);
469 printf("%*s DEF ",level * 4,"");
470 res_dump(r->def_res, level + 1);
474 printf("%*s OVER ",level * 4,"");
475 res_dump(r->over_res, level + 1);
479 int res_check(Res r_i, Res r_v)
481 struct res_entry *e_i;
484 for (e_i = r_i->first; e_i; e_i = e_i->next)
486 struct res_entry *e_v;
487 for (e_v = r_v->first; e_v; e_v = e_v->next)
489 int prefix_allowed = 0;
490 int suffix_allowed = 0;
491 const char *name = e_i->name;
492 size_t name_len = strlen(e_i->name);
494 const char *first_dot = 0;
495 const char *second_dot = 0;
497 if (strchr(e_v->value, 'p'))
499 if (strchr(e_v->value, 's'))
502 first_dot = strchr(name, '.');
503 if (prefix_allowed && first_dot)
506 name_len = strlen(name);
508 second_dot = strchr(name, '.');
509 if (suffix_allowed && second_dot)
511 name_len = second_dot - name;
513 if (name_len < sizeof(namez)-1)
515 memcpy(namez, name, name_len);
516 namez[name_len] = '\0';
517 if (!yaz_matchstr(namez, e_v->name))
520 /* for case 'a.b' we have to check 'a' as well */
521 if (prefix_allowed && suffix_allowed && first_dot && !second_dot)
524 name_len = first_dot - name;
525 if (name_len < sizeof(namez)-1)
527 memcpy(namez, name, name_len);
528 namez[name_len] = '\0';
529 if (!yaz_matchstr(namez, e_v->name))
536 yaz_log(YLOG_WARN, "The following setting is unrecognized: %s",
547 * indent-tabs-mode: nil
549 * vim: shiftwidth=4 tabstop=8 expandtab