]> mj.ucw.cz Git - libucw.git/commitdiff
conf2: tested a few more test-cases and fixed/polished the code
authorRobert Spalek <robert@ucw.cz>
Sun, 23 Apr 2006 19:13:08 +0000 (21:13 +0200)
committerRobert Spalek <robert@ucw.cz>
Sun, 23 Apr 2006 19:13:08 +0000 (21:13 +0200)
- journal blocks are "transactions", don't overload the word "sections" too much
- added 2 long-term TODO's and 1 short-term FIXME
- the include command must be the last one on a line

lib/conf2-test.c
lib/conf2.c
lib/conf2.t

index fae56c5288dc59a5679c933616506679f4b2f865..d2d4b09cbcf8fd8ef6c8b000ea0b6a56050f680f 100644 (file)
@@ -139,10 +139,14 @@ CF_USAGE
 ";
 
 static void NONRET
-usage(void)
+usage(byte *msg, ...)
 {
-       fputs(help, stderr);
-       exit(1);
+  va_list va;
+  va_start(va, msg);
+  if (msg)
+    vfprintf(stderr, msg, va);
+  fputs(help, stderr);
+  exit(1);
 }
 
 int
@@ -156,10 +160,10 @@ main(int argc, char *argv[])
   while ((opt = cf_get_opt(argc, argv, short_opts, long_opts, NULL)) >= 0)
     switch (opt) {
       case 'v': verbose++; break;
-      default: usage();
+      default: usage("unknown option %c\n", opt);
     }
   if (optind < argc)
-    usage();
+    usage("too many parameters (%d more)\n", argc-optind);
 
   //cf_reload("non-existent file");
 
index ec9a532f192152b3a7ee585ba88728be098842ee..b8f0ea9437719a7fdaa8dd29088091da8d79d926 100644 (file)
@@ -104,7 +104,7 @@ journal_swap(void)
 }
 
 static struct journal_item *
-journal_new_section(uns new_pool)
+journal_new_transaction(uns new_pool)
 {
   if (new_pool)
     cf_pool = mp_new(1<<10);
@@ -114,7 +114,7 @@ journal_new_section(uns new_pool)
 }
 
 static void
-journal_commit_section(uns new_pool, struct journal_item *oldj)
+journal_commit_transaction(uns new_pool, struct journal_item *oldj)
 {
   if (new_pool)
   {
@@ -133,7 +133,7 @@ journal_commit_section(uns new_pool, struct journal_item *oldj)
 }
 
 static void
-journal_rollback_section(uns new_pool, struct journal_item *oldj)
+journal_rollback_transaction(uns new_pool, struct journal_item *oldj)
 {
   if (!cf_need_journal)
     die("Cannot rollback the configuration, because the journal is disabled.");
@@ -323,7 +323,7 @@ int
 cf_reload(byte *file)
 {
   journal_swap();
-  struct journal_item *oldj = journal_new_section(1);
+  struct journal_item *oldj = journal_new_transaction(1);
   int err = load_file(file);
   if (!err)
   {
@@ -332,11 +332,11 @@ cf_reload(byte *file)
       pools = p->prev;
       mp_delete(p->pool);
     }
-    journal_commit_section(1, NULL);
+    journal_commit_transaction(1, NULL);
   }
   else
   {
-    journal_rollback_section(1, oldj);
+    journal_rollback_transaction(1, oldj);
     journal_swap();
   }
   return err;
@@ -345,24 +345,24 @@ cf_reload(byte *file)
 int
 cf_load(byte *file)
 {
-  struct journal_item *oldj = journal_new_section(1);
+  struct journal_item *oldj = journal_new_transaction(1);
   int err = load_file(file);
   if (!err)
-    journal_commit_section(1, oldj);
+    journal_commit_transaction(1, oldj);
   else
-    journal_rollback_section(1, oldj);
+    journal_rollback_transaction(1, oldj);
   return err;
 }
 
 int
 cf_set(byte *string)
 {
-  struct journal_item *oldj = journal_new_section(0);
+  struct journal_item *oldj = journal_new_transaction(0);
   int err = load_string(string);
   if (!err)
-    journal_commit_section(0, oldj);
+    journal_commit_transaction(0, oldj);
   else
-    journal_rollback_section(0, oldj);
+    journal_rollback_transaction(0, oldj);
   return err;
 }
 
@@ -1050,6 +1050,8 @@ get_word(uns is_command_name)
     }
     line++;
 
+    // FIXME: this is utterly bogus, since printf() only expands these
+    // percents, and the \n, \x1b codes are actually expanded by the compiler
     for (byte *c=copy_buf.ptr+start_copy; *c; c++)
       if (*c == '%') {
        if (c[1] != '%')
@@ -1169,14 +1171,14 @@ parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
       pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
     if (!strcasecmp(name, "include"))
     {
-      if (words != 2) {
+      if (words != 2)
        msg = "Expecting one filename";
-       goto error;
-      }
-      if (depth > 8) {
+      else if (depth > 8)
        msg = "Too many nested files";
+      else if (*line && *line != '#')          // because the contents of line_buf is not re-entrant and will be cleared
+       msg = "The input command must be the last one on a line";
+      if (msg)
        goto error;
-      }
       struct fastbuf *new_fb = bopen_safe(pars[0]);
       if (!new_fb) {
        msg = "Cannot open file";
@@ -1363,3 +1365,8 @@ cf_dump_sections(struct fastbuf *fb)
 {
   dump_section(fb, &sections, 0, NULL);
 }
+
+/* TODO
+ * - more space efficient journal
+ * - don't commit all, but recognize from the journal which sections have been changed
+ */
index 42b35f9c739ce41f2c4eb8f4c7116fcad2ff0f6b..95216e137c9466699d4ab34a38e67b691a44135f 100644 (file)
@@ -1,5 +1,6 @@
 # test config file
-  #include lib/conf2.t
+#include lib/conf2.t ; top.xa=1
+#include 'non-existent file'; #top.xa=1
 Top { \
 
   nr1=16       #!!!
@@ -8,7 +9,7 @@ Top { \
            \
            17M
   nrs2 3 3k 3 3 3 ; \
-  str1 "hello, world\n"
+  str1 "hello, world%%\n"
   str2 'Hagenuk,
       the best' "\
       " qu'est-ce que c'est?