1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <yaz/tokenizer.h>
32 #include <yaz/yaz-util.h>
33 #include <idzebra/res.h>
40 struct res_entry *next;
45 struct res_entry *first, *last;
50 static Res res_incref(Res r)
57 static struct res_entry *add_entry(Res r)
59 struct res_entry *resp;
62 resp = r->last = r->first =
63 (struct res_entry *) xmalloc(sizeof(*resp));
66 resp = (struct res_entry *) xmalloc(sizeof(*resp));
74 static char *xstrdup_env(const char *src)
83 if (src[i] == '$' && src[i+1] == '{')
89 while (k < 127 && src[i] && !strchr(":}\n\r\f", src[i]))
90 envname[k++] = src[i++];
93 env_val = getenv(envname);
95 env_strlen += 1 + strlen(env_val);
98 while (src[i] && !strchr("}\n\r\f", src[i]))
106 dst = xmalloc(1 + env_strlen + i);
110 if (src[i] == '$' && src[i+1] == '{')
116 while(k < 127 && src[i] && !strchr(":}\n\r\f", src[i]))
117 envname[k++] = src[i++];
119 env_val = getenv(envname);
122 strcpy(dst+j, env_val);
123 j += strlen(env_val);
125 else if (src[i] == ':' && src[i+1] == '-')
128 while (src[i] && !strchr("}\n\r\f", src[i]))
131 while (src[i] && !strchr("}\n\r\f", src[i]))
143 ZEBRA_RES res_read_file(Res r, const char *fname)
150 fr = fopen(fname, "r");
153 yaz_log(YLOG_WARN|YLOG_ERRNO, "Cannot open `%s'", fname);
161 WRBUF wrbuf_val = wrbuf_alloc();
162 yaz_tok_cfg_t yt = yaz_tok_cfg_create();
164 while ((line = fgets(fr_buf, sizeof(fr_buf)-1, fr)))
166 yaz_tok_parse_t tp = yaz_tok_parse_buf(yt, line);
167 int t = yaz_tok_move(tp);
169 if (t == YAZ_TOK_STRING)
172 struct res_entry *resp;
173 const char *cp = yaz_tok_parse_string(tp);
174 const char *cp1 = strchr(cp, ':');
178 yaz_log(YLOG_FATAL, "%s:%d missing colon after '%s'",
185 resp->name = xmalloc(sz + 1);
186 memcpy(resp->name, cp, sz);
187 resp->name[sz] = '\0';
189 wrbuf_rewind(wrbuf_val);
194 wrbuf_puts(wrbuf_val, cp1+1);
199 t = yaz_tok_move(tp);
201 if (t != YAZ_TOK_STRING)
203 resp->value = xstrdup("");
204 yaz_log(YLOG_FATAL, "%s:%d missing value after '%s'",
205 fname, lineno, resp->name);
209 wrbuf_puts(wrbuf_val, yaz_tok_parse_string(tp));
211 while ((t=yaz_tok_move(tp)) == YAZ_TOK_STRING)
213 wrbuf_putc(wrbuf_val, ' ');
214 wrbuf_puts(wrbuf_val, yaz_tok_parse_string(tp));
216 resp->value = xstrdup_env(wrbuf_cstr(wrbuf_val));
217 /* printf("name=%s value=%s\n", resp->name, resp->value); */
220 yaz_tok_parse_destroy(tp);
223 yaz_tok_cfg_destroy(yt);
224 wrbuf_destroy(wrbuf_val);
231 Res res_open(Res def_res, Res over_res)
234 r = (Res) xmalloc(sizeof(*r));
237 r->first = r->last = NULL;
238 r->def_res = res_incref(def_res);
239 r->over_res = res_incref(over_res);
243 void res_clear(Res r)
245 struct res_entry *re, *re1;
246 for (re = r->first; re; re=re1)
255 r->first = r->last = NULL;
258 void res_close(Res r)
260 if (r && --(r->ref_count) == 0)
263 res_close(r->def_res);
264 res_close(r->over_res);
269 const char *res_get_prefix(Res r, const char *name, const char *prefix,
277 if (strlen(name) + strlen(prefix) >= (sizeof(rname)-2))
279 strcpy(rname, prefix);
282 v = res_get(r, rname);
285 v = res_get(r, name);
291 const char *res_get(Res r, const char *name)
293 struct res_entry *re;
299 v = res_get(r->over_res, name);
303 for (re = r->first; re; re=re->next)
304 if (re->value && !yaz_matchstr(re->name, name))
307 return res_get(r->def_res, name);
310 const char *res_get_def(Res r, const char *name, const char *def)
314 if (!(t = res_get(r, name)))
317 yaz_log(YLOG_DEBUG, "Using default resource %s:%s", name, def);
324 int res_get_match(Res r, const char *name, const char *value, const char *s)
326 const char *cn = res_get(r, name);
330 if (cn && !yaz_matchstr(cn, value))
335 void res_set(Res r, const char *name, const char *value)
337 struct res_entry *re;
342 for (re = r->first; re; re=re->next)
343 if (re->value && !yaz_matchstr(re->name, name))
346 re->value = xstrdup_env(value);
350 re->name = xstrdup(name);
351 re->value = xstrdup_env(value);
354 int res_trav(Res r, const char *prefix, void *p,
355 void (*f)(void *p, const char *name, const char *value))
357 struct res_entry *re;
363 no = res_trav(r->over_res, prefix, p, f);
368 for (re = r->first; re; re=re->next)
370 if (l==0 || !memcmp(re->name, prefix, l))
372 (*f)(p, re->name, re->value);
376 return res_trav(r->def_res, prefix, p, f);
381 ZEBRA_RES res_write_file(Res r, const char *fname)
383 struct res_entry *re;
387 fr = fopen(fname, "w");
390 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Cannot create `%s'", fname);
394 for (re = r->first; re; re=re->next)
397 int lefts = strlen(re->name)+2;
400 fprintf(fr, "%s\n", re->name);
403 fprintf(fr, "%s: ", re->name);
404 while (lefts + strlen(re->value+no) > 78)
407 int ind = no+ 78-lefts;
410 if (re->value[ind] == ' ')
415 ind = no + 78 - lefts;
416 for (i = no; i != ind; i++)
417 putc(re->value[i], fr);
422 fprintf(fr, "%s\n", re->value+no);
429 ZEBRA_RES res_get_int(Res r, const char *name, int *val)
431 const char *cp = res_get(r, name);
434 if (sscanf(cp, "%d", val) == 1)
436 yaz_log(YLOG_WARN, "Expected integer for resource %s", name);
442 void res_add(Res r, const char *name, const char *value)
444 struct res_entry *re;
448 yaz_log(YLOG_RES, "res_add res=%p, name=%s, value=%s", r, name, value);
450 re->name = xstrdup(name);
451 re->value = xstrdup_env(value);
454 void res_dump(Res r, int level)
456 struct res_entry *re;
461 for (re = r->first; re; re=re->next) {
462 printf("%*s - %s:='%s'\n",level * 4,"",re->name,re->value);
466 printf("%*s DEF ",level * 4,"");
467 res_dump(r->def_res, level + 1);
471 printf("%*s OVER ",level * 4,"");
472 res_dump(r->over_res, level + 1);
476 int res_check(Res r_i, Res r_v)
478 struct res_entry *e_i;
481 for (e_i = r_i->first; e_i; e_i = e_i->next)
483 struct res_entry *e_v;
484 for (e_v = r_v->first; e_v; e_v = e_v->next)
486 int prefix_allowed = 0;
487 int suffix_allowed = 0;
488 const char *name = e_i->name;
489 size_t name_len = strlen(e_i->name);
491 const char *first_dot = 0;
492 const char *second_dot = 0;
494 if (strchr(e_v->value, 'p'))
496 if (strchr(e_v->value, 's'))
499 first_dot = strchr(name, '.');
500 if (prefix_allowed && first_dot)
503 name_len = strlen(name);
505 second_dot = strchr(name, '.');
506 if (suffix_allowed && second_dot)
508 name_len = second_dot - name;
510 if (name_len < sizeof(namez)-1)
512 memcpy(namez, name, name_len);
513 namez[name_len] = '\0';
514 if (!yaz_matchstr(namez, e_v->name))
517 /* for case 'a.b' we have to check 'a' as well */
518 if (prefix_allowed && suffix_allowed && first_dot && !second_dot)
521 name_len = first_dot - name;
522 if (name_len < sizeof(namez)-1)
524 memcpy(namez, name, name_len);
525 namez[name_len] = '\0';
526 if (!yaz_matchstr(namez, e_v->name))
533 yaz_log(YLOG_WARN, "The following setting is unrecognized: %s",
544 * c-file-style: "Stroustrup"
545 * indent-tabs-mode: nil
547 * vim: shiftwidth=4 tabstop=8 expandtab