From: Pavel Charvat Date: Fri, 12 May 2006 07:02:00 +0000 (+0200) Subject: configuration reader: X-Git-Tag: holmes-import~641^2~21 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=481e6b8e96e16e34c60928a1ba007e8766b2c0b1;p=libucw.git configuration reader: - fixes in string defaults parser - simple automatic tests - more beautiful shell arrays --- diff --git a/lib/shell/Makefile b/lib/shell/Makefile index df692103..75326581 100644 --- a/lib/shell/Makefile +++ b/lib/shell/Makefile @@ -6,3 +6,5 @@ DATAFILES+=$(o)/lib/shell/libucw.sh $(o)/lib/shell/config: $(o)/lib/shell/config.o $(LIBSH) $(o)/lib/shell/logger: $(o)/lib/shell/logger.o $(LIBSH) + +TESTS+=$(addprefix $(o)/lib/shell/,config.test) diff --git a/lib/shell/config.c b/lib/shell/config.c index 8a89e4a4..814daa0f 100644 --- a/lib/shell/config.c +++ b/lib/shell/config.c @@ -72,6 +72,7 @@ struct item { uns flags; struct cf_item cf; union value value; + uns index; }; struct section { @@ -185,7 +186,7 @@ parse_section(struct section *section) byte *def = pos, *d = def; while (*pos != ';' && *pos != '}' && !Cspace(*pos)) { - if (*pos == '\'') + if (*pos == '\'' ) { pos++; while (*pos != '\'') @@ -199,6 +200,7 @@ parse_section(struct section *section) else if (*pos == '"') { pos++; + byte *start = d; uns esc = 0; while (*pos != '"' || esc) { @@ -206,9 +208,13 @@ parse_section(struct section *section) die("Unterminated string"); if (*pos == '\\') esc ^= 1; + else + esc = 0; *d++ = *pos++; } pos++; + *d = 0; + d = str_unesc(start, start); } else *d++ = *pos++; @@ -217,7 +223,6 @@ parse_section(struct section *section) byte *buf = mp_alloc(pool, len + 1); memcpy(buf, def, len); buf[len] = 0; - str_unesc(buf, buf); switch (item->cf.type) #define TRY(x) do{byte *_err=(x); if (_err) die(_err); }while(0) { @@ -235,7 +240,7 @@ parse_section(struct section *section) break; default: ASSERT(0); -#undef TRY +#undef TRY } } } @@ -322,7 +327,7 @@ dump_item(struct item *item, void *ptr, uns path_len) if (!ptr) printf("CF_%s_%s='", path.ptr, name); else - printf("CF_%s_%s[${#CF_%s_%s[*]}]='", path.ptr, name, path.ptr, name); + printf("CF_%s_%s[%u]='", path.ptr, name, item->index++); switch (item->cf.type) { case CT_INT: diff --git a/lib/shell/config.t b/lib/shell/config.t new file mode 100644 index 00000000..b28bd393 --- /dev/null +++ b/lib/shell/config.t @@ -0,0 +1,27 @@ +# Tests for configuration parser + +Run: obj/lib/shell/config -C/dev/null -S 'sec1{int1=23; long1=1234567812345678; long2=4321; str1="s1"; str2="s2"}' 'sec1 {#int1; ##long1; -str1; str2; #int2=123; ##long2=1234}; sec2{str3}' +Out: CF_sec1_int1='23' + CF_sec1_long1='1234567812345678' + CF_sec1_str2='s2' + CF_sec1_int2='123' + CF_sec1_long2='4321' + CF_sec2_str3='' + +Run: obj/lib/shell/config -C/dev/null -S 'sec1{list1 1 a1 b1; list1:clear; list1 2 a2 b2 3 a3 b3}' 'sec1 {@list1 {#int1; str1; -str2}}' +Out: CF_sec1_list1_int1[0]='2' + CF_sec1_list1_str1[0]='a2' + CF_sec1_list1_int1[1]='3' + CF_sec1_list1_str1[1]='a3' + +Run: obj/lib/shell/config -C/dev/null -S 'sec1{list1 {str1=1; list2=a b c}; list1 {str1=2; list2=d e}}' 'sec1 {@list1 {str1; @list2{str2}}}' +Out: CF_sec1_list1_str1[0]='1' + CF_sec1_list1_list2_str2[0]='a' + CF_sec1_list1_list2_str2[1]='b' + CF_sec1_list1_list2_str2[2]='c' + CF_sec1_list1_str1[1]='2' + CF_sec1_list1_list2_str2[3]='d' + CF_sec1_list1_list2_str2[4]='e' + +Run: obj/lib/shell/config -C/dev/null 'sec{str=a'\''b"c'\''d"\\e'\''f"g}' +Out: CF_sec_str='ab"cd\e'\''fg'