]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/conf-test.c
UCW::CGI: Escaping functions silently convert undef to undef
[libucw.git] / ucw / conf-test.c
index b62cde6c69b8fed0e15228de8b24e9541b76f6a9..e8ba0458c568e90045ff948b56ee709b81d89e77 100644 (file)
@@ -2,19 +2,21 @@
  *     Insane tester of reading configuration files
  *
  *     (c) 2006 Robert Spalek <robert@ucw.cz>
+ *     (c) 2012 Martin Mares <mj@ucw.cz>
  */
 
-#include "ucw/lib.h"
-#include "ucw/conf.h"
-#include "ucw/getopt.h"
-#include "ucw/clists.h"
-#include "ucw/fastbuf.h"
+#include <ucw/lib.h>
+#include <ucw/conf.h>
+#include <ucw/getopt.h>
+#include <ucw/clists.h>
+#include <ucw/fastbuf.h>
 
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
 
 static int verbose;
+static int reload;
 
 struct sub_sect_1 {
   cnode n;
@@ -133,7 +135,7 @@ commit_top(void *ptr UNUSED)
   return NULL;
 }
 
-static char *alphabet[] = { "alpha", "beta", "gamma", "delta", NULL };
+static const char * const alphabet[] = { "alpha", "beta", "gamma", "delta", NULL };
 static struct cf_section cf_top = {
   CF_INIT(init_top),
   CF_COMMIT(commit_top),
@@ -153,7 +155,7 @@ static struct cf_section cf_top = {
     CF_LOOKUP_DYN("look", &look, alphabet, 1000),
     CF_USER_ARY("numbers", numbers, &u16_type, 10),
     CF_BITMAP_INT("bitmap1", &bitmap1),
-    CF_BITMAP_LOOKUP("bitmap2", &bitmap2, ((char*[]) {
+    CF_BITMAP_LOOKUP("bitmap2", &bitmap2, ((const char* const[]) {
          "one", "two", "three", "four", "five", "six", "seven", "eight", 
          "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "seventeen", 
          "eighteen", "nineteen", "twenty", NULL        // hidden joke here
@@ -162,19 +164,20 @@ static struct cf_section cf_top = {
   }
 };
 
-static char short_opts[] = CF_SHORT_OPTS "v";
+static char short_opts[] = CF_SHORT_OPTS "rv";
 static struct option long_opts[] = {
        CF_LONG_OPTS
+       {"reload",      0, 0, 'r'},
        {"verbose",     0, 0, 'v'},
        {NULL,          0, 0, 0}
 };
 
 static char *help = "\
-Usage: conf-test <options>\n\
+Usage: conf-test [ctxt] [nojournal] <options>\n\
 \n\
-Options:\n"
-CF_USAGE
-"-v\t\t\tBe verbose\n\
+Options:\n" CF_USAGE "\
+-r, --reload\t\tReload configuration\n\
+-v, --verbose\t\tBe verbose\n\
 ";
 
 static void NONRET
@@ -192,28 +195,49 @@ int
 main(int argc, char *argv[])
 {
   log_init(argv[0]);
+  struct cf_context *cc = NULL, *prev = NULL;
+
+  // Special arguments which have to be parsed before cf_getopt()
+  while (argc > 1) {
+    if (!strcmp(argv[1], "ctxt")) {
+      cc = cf_new_context();
+      prev = cf_switch_context(cc);
+      argc--, argv++;
+    } else if (!strcmp(argv[1], "nojournal")) {
+      cf_set_journalling(0);
+      argc--, argv++;
+    } else
+      break;
+  }
+
   cf_declare_section("top", &cf_top, 0);
   cf_def_file = "ucw/conf-test.cf";
 
   int opt;
   while ((opt = cf_getopt(argc, argv, short_opts, long_opts, NULL)) >= 0)
     switch (opt) {
+      case 'r': reload++; break;
       case 'v': verbose++; break;
       default: usage("unknown option %c\n", opt);
     }
   if (optind < argc)
     usage("too many parameters (%d more)\n", argc-optind);
 
-  /*
-  cf_load("non-existent file");
-  //cf_reload("non-existent file");
-  cf_load("non-existent file");
-  cf_set("top.d1 -1.1; top.master b");
-  */
+  if (reload) {
+    cf_reload(NULL);
+    cf_reload(NULL);
+  }
+
+  if (verbose) {
+    struct fastbuf *out = bfdopen(1, 1<<14);
+    cf_dump_sections(out);
+    bclose(out);
+  }
 
-  struct fastbuf *out = bfdopen(1, 1<<14);
-  cf_dump_sections(out);
-  bclose(out);
+  if (cc) {
+    cf_switch_context(prev);
+    cf_delete_context(cc);
+  }
 
   return 0;
 }