]> mj.ucw.cz Git - libucw.git/commitdiff
configuration reader:
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Fri, 12 May 2006 07:02:00 +0000 (09:02 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Fri, 12 May 2006 07:02:00 +0000 (09:02 +0200)
- fixes in string defaults parser
- simple automatic tests
- more beautiful shell arrays

lib/shell/Makefile
lib/shell/config.c
lib/shell/config.t [new file with mode: 0644]

index df69210379bfe3f74628ea3994d5f65d8183ddb3..753265819a3d7561c789a8e751733afc8c0507cc 100644 (file)
@@ -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)
index 8a89e4a4dddcdf29beb1fac3c11a5f6a3bf7dd70..814daa0f55b41c6849d4d5dd81cf8bdee8a3ac47 100644 (file)
@@ -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 (file)
index 0000000..b28bd39
--- /dev/null
@@ -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'