X-Git-Url: http://git.indexdata.com/?p=idzebra-moved-to-github.git;a=blobdiff_plain;f=util%2Fres.c;h=ccd881999c5cd7c5433f0e5214449c3e82d4c165;hp=5df5efdf1b051f3cad1ddb82382259231d75fd50;hb=250de4ed23a44f5eb3552db317eef0d0fbe3265c;hpb=d64c82cd05145e97c4edef0434c383a29c95c8fe diff --git a/util/res.c b/util/res.c index 5df5efd..ccd8819 100644 --- a/util/res.c +++ b/util/res.c @@ -1,8 +1,5 @@ -/* $Id: res.c,v 1.57 2007-05-16 08:57:27 adam Exp $ - Copyright (C) 1995-2007 - Index Data ApS - -This file is part of the Zebra server. +/* This file is part of the Zebra server. + Copyright (C) 2004-2013 Index Data Zebra is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -20,6 +17,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#if HAVE_CONFIG_H +#include +#endif #include #include #include @@ -31,6 +31,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #endif +#include #include #include @@ -124,7 +125,7 @@ static char *xstrdup_env(const char *src) strcpy(dst+j, env_val); j += strlen(env_val); } - else if (src[i] == ':' && src[i+1] == '-') + else if (src[i] == ':' && src[i+1] == '-') { i = i + 2; while (src[i] && !strchr("}\n\r\f", src[i])) @@ -144,12 +145,8 @@ static char *xstrdup_env(const char *src) ZEBRA_RES res_read_file(Res r, const char *fname) { - struct res_entry *resp; - char *line; - char *val_buf; - int val_size, val_max = 256; - char fr_buf[1024]; FILE *fr; + int errors = 0; assert(r); @@ -157,81 +154,80 @@ ZEBRA_RES res_read_file(Res r, const char *fname) if (!fr) { yaz_log(YLOG_WARN|YLOG_ERRNO, "Cannot open `%s'", fname); - return ZEBRA_FAIL; + errors++; } - val_buf = (char*) xmalloc(val_max); - while (1) + else { - line = fgets(fr_buf, sizeof(fr_buf)-1, fr); - if (!line) - break; - if (*line != '#') + char fr_buf[1024]; + char *line; + int lineno = 1; + WRBUF wrbuf_val = wrbuf_alloc(); + yaz_tok_cfg_t yt = yaz_tok_cfg_create(); + + while ((line = fgets(fr_buf, sizeof(fr_buf)-1, fr))) { - int no = 0; - while (1) + yaz_tok_parse_t tp = yaz_tok_parse_buf(yt, line); + int t = yaz_tok_move(tp); + + if (t == YAZ_TOK_STRING) { - if (fr_buf[no] == 0 || fr_buf[no] == '\n' ) + size_t sz; + struct res_entry *resp; + const char *cp = yaz_tok_parse_string(tp); + const char *cp1 = strchr(cp, ':'); + + if (!cp1) { - no = 0; + yaz_log(YLOG_FATAL, "%s:%d missing colon after '%s'", + fname, lineno, cp); + errors++; break; } - if (strchr(": \t", fr_buf[no])) - break; - no++; - } - if (!no) - continue; - fr_buf[no++] = '\0'; - resp = add_entry(r); - resp->name = (char*) xmalloc(no); - strcpy(resp->name, fr_buf); - - while (strchr(" \t", fr_buf[no])) - no++; - val_size = 0; - while (1) - { - if (fr_buf[no] == '\0' || strchr("\n\r\f", fr_buf[no])) + resp = add_entry(r); + sz = cp1 - cp; + resp->name = xmalloc(sz + 1); + memcpy(resp->name, cp, sz); + resp->name[sz] = '\0'; + + wrbuf_rewind(wrbuf_val); + + if (cp1[1]) { - while (val_size > 0 && - (val_buf[val_size-1] == ' ' || - val_buf[val_size-1] == '\t')) - val_size--; - val_buf[val_size] = '\0'; - resp->value = xstrdup_env(val_buf); - yaz_log(YLOG_DEBUG, "(name=%s,value=%s)", - resp->name, resp->value); - break; + /* name:value */ + wrbuf_puts(wrbuf_val, cp1+1); } - else if (fr_buf[no] == '\\' && strchr("\n\r\f", fr_buf[no+1])) + else { - line = fgets(fr_buf, sizeof(fr_buf)-1, fr); - if (!line) + /* name: value */ + t = yaz_tok_move(tp); + + if (t != YAZ_TOK_STRING) { - val_buf[val_size] = '\0'; - resp->value = xstrdup_env(val_buf); + resp->value = xstrdup(""); + yaz_log(YLOG_FATAL, "%s:%d missing value after '%s'", + fname, lineno, resp->name); + errors++; break; } - no = 0; + wrbuf_puts(wrbuf_val, yaz_tok_parse_string(tp)); } - else + while ((t=yaz_tok_move(tp)) == YAZ_TOK_STRING) { - val_buf[val_size++] = fr_buf[no++]; - if (val_size+1 >= val_max) - { - char *nb; - - nb = (char*) xmalloc(val_max+=1024); - memcpy(nb, val_buf, val_size); - xfree(val_buf); - val_buf = nb; - } + wrbuf_putc(wrbuf_val, ' '); + wrbuf_puts(wrbuf_val, yaz_tok_parse_string(tp)); } + resp->value = xstrdup_env(wrbuf_cstr(wrbuf_val)); + /* printf("name=%s value=%s\n", resp->name, resp->value); */ } + lineno++; + yaz_tok_parse_destroy(tp); } - } - xfree(val_buf); - fclose(fr); + fclose(fr); + yaz_tok_cfg_destroy(yt); + wrbuf_destroy(wrbuf_val); + } + if (errors) + return ZEBRA_FAIL; return ZEBRA_OK; } @@ -280,7 +276,7 @@ const char *res_get_prefix(Res r, const char *name, const char *prefix, if (prefix) { char rname[128]; - + if (strlen(name) + strlen(prefix) >= (sizeof(rname)-2)) return 0; strcpy(rname, prefix); @@ -302,7 +298,7 @@ const char *res_get(Res r, const char *name) if (!r) return 0; - + v = res_get(r->over_res, name); if (v) return v; @@ -364,7 +360,7 @@ int res_trav(Res r, const char *prefix, void *p, struct res_entry *re; int l = 0; int no = 0; - + if (!r) return 0; no = res_trav(r->over_res, prefix, p, f); @@ -458,22 +454,22 @@ void res_add(Res r, const char *name, const char *value) re->value = xstrdup_env(value); } -void res_dump(Res r, int level) +void res_dump(Res r, int level) { struct res_entry *re; - + if (!r) return; - + for (re = r->first; re; re=re->next) { printf("%*s - %s:='%s'\n",level * 4,"",re->name,re->value); } - + if (r->def_res) { printf("%*s DEF ",level * 4,""); res_dump(r->def_res, level + 1); } - + if (r->over_res) { printf("%*s OVER ",level * 4,""); res_dump(r->over_res, level + 1); @@ -484,7 +480,7 @@ int res_check(Res r_i, Res r_v) { struct res_entry *e_i; int errors = 0; - + for (e_i = r_i->first; e_i; e_i = e_i->next) { struct res_entry *e_v; @@ -502,7 +498,7 @@ int res_check(Res r_i, Res r_v) prefix_allowed = 1; if (strchr(e_v->value, 's')) suffix_allowed = 1; - + first_dot = strchr(name, '.'); if (prefix_allowed && first_dot) { @@ -548,6 +544,7 @@ int res_check(Res r_i, Res r_v) /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab