]> mj.ucw.cz Git - libucw.git/commitdiff
conf2: verified and fixed all parsers
authorRobert Spalek <robert@ucw.cz>
Sun, 23 Apr 2006 10:51:54 +0000 (12:51 +0200)
committerRobert Spalek <robert@ucw.cz>
Sun, 23 Apr 2006 10:51:54 +0000 (12:51 +0200)
lib/conf2-test.c
lib/conf2.c
lib/conf2.t

index 5a38355d392fcaf24e5a268a7203397e372a9d5a..99b9b0a846f230e912c1f3ec376876d426d5cb11 100644 (file)
@@ -68,6 +68,7 @@ static double d1 = -1.1;
 static struct sub_sect_1 sec1 = { {}, "Charlie", "WBAFC", { 0, -1} };
 static struct clist secs;
 static time_t t1, t2;
+static u32 ip;
 
 static byte *
 init_top(void *ptr UNUSED)
@@ -112,6 +113,7 @@ static struct cf_section cf_top = {
     CF_PARSER("SecondTime", &t2, time_parser, 1),
     CF_SECTION("master", &sec1, &cf_sec_1),
     CF_LIST("slaves", &secs, &cf_sec_1),
+    CF_IP("ip", &ip),
     CF_END
   }
 };
index 07513f5dbb1a5f8f863213c54e6aefa91e157ee7..43529e3850f5fc8ff048bbd43e0634090e73f576 100644 (file)
@@ -396,7 +396,6 @@ lookup_unit(byte *value, byte *end, byte **msg)
 
 static char cf_rngerr[] = "Number out of range";
 
-//FIXME: parsers should handle well empty strings, unwanted suffixes etc.
 byte *
 cf_parse_int(byte *str, int *ptr)
 {
@@ -463,12 +462,11 @@ cf_parse_double(byte *str, double *ptr)
     msg = "Missing number";
   else {
     const struct unit *u;
-    char *end;
-    errno = 0;
-    double x = strtoul(str, &end, 0);
-    if (errno == ERANGE)
-      msg = cf_rngerr;
-    else if (u = lookup_unit(str, end, &msg))
+    double x;
+    uns read_chars;
+    if (sscanf(str, "%lf%n", &x, &read_chars) != 1)
+      msg = "Invalid number";
+    else if (u = lookup_unit(str, str + read_chars, &msg))
       *ptr = x * u->num / u->den;
     else
       *ptr = x;
@@ -482,11 +480,13 @@ cf_parse_ip(byte *p, u32 *varp)
   if (!*p)
     return "Missing IP address";
   uns x = 0;
+  char *p2;
   if (*p == '0' && p[1] | 32 == 'X') {
     errno = 0;
-    x = strtoul(p + 2, NULL, 16);
-    if (errno == ERANGE || x > 0xffffffff)
+    x = strtoul(p + 2, &p2, 16);
+    if (errno == ERANGE || p2 == (char*) (p+2) || x > 0xffffffff)
       goto error;
+    p = p2;
   }
   else
     for (uns i = 0; i < 4; i++) {
@@ -495,15 +495,14 @@ cf_parse_ip(byte *p, u32 *varp)
          goto error;
       }
       errno = 0;
-      char *p2;
       uns y = strtoul(p, &p2, 10);
-      p = p2;
-      if (errno == ERANGE || y > 255)
+      if (errno == ERANGE || p2 == (char*) p || y > 255)
        goto error;
+      p = p2;
       x = (x << 8) + y;
     }
   *varp = x;
-  return NULL;
+  return *p ? "Trailing characters" : NULL;
 error:
   return "Invalid IP address";
 }
@@ -1279,8 +1278,8 @@ dump_basic(struct fastbuf *fb, void *ptr, enum cf_type type)
 {
   switch (type) {
     case CT_INT:       bprintf(fb, "%d ", *(uns*)ptr); break;
-    case CT_U64:       bprintf(fb, "%lld ", *(u64*)ptr); break;
-    case CT_DOUBLE:    bprintf(fb, "%lf ", *(double*)ptr); break;
+    case CT_U64:       bprintf(fb, "%llu ", *(u64*)ptr); break;
+    case CT_DOUBLE:    bprintf(fb, "%lg ", *(double*)ptr); break;
     case CT_IP:                bprintf(fb, "%08x ", *(uns*)ptr); break;
     case CT_STRING:    bprintf(fb, "'%s' ", *(byte**)ptr); break;
   }
index 7bdbb43afe0f81d6476cac44e6ef301ebba39206..836c9fd0fea135ce5197c6523daba41a8db12ac8 100644 (file)
@@ -13,11 +13,13 @@ Top { \
       the best' "\
       " qu'est-ce que c'est?
   u1   0xbadcafebadbeefc0
-  #d1  -1.14e-25
   d1 7%
+  d1   -1.14e-25
   firsttime ; secondtime 56
   ^top.master:set      alice HB8+
   slaves:clear
+  ip 0xa
+  ip 195.113.31.123
 };;;;;;
 
 unknown.ignored :-)
@@ -35,5 +37,5 @@ top.slaveS:before {level pum}{
 }
 
 topp.a=15
-top.nr1=   15
+top.nr1=   ' 15'
 a { ;-D }