]> mj.ucw.cz Git - libucw.git/commitdiff
added declaration and initialization of sections
authorRobert Spalek <robert@ucw.cz>
Thu, 20 Apr 2006 17:31:05 +0000 (19:31 +0200)
committerRobert Spalek <robert@ucw.cz>
Thu, 20 Apr 2006 17:31:05 +0000 (19:31 +0200)
lib/conf2.c
lib/conf2.h

index 88e17fc7f1663a091705e901c6fd074ce71aa24e..b82210bda50fd007adba5ab087988eba38e1090e 100644 (file)
@@ -11,6 +11,7 @@
 #include "lib/lib.h"
 #include "lib/conf2.h"
 #include "lib/mempool.h"
+#include "lib/clists.h"
 
 #include <stdlib.h>
 #include <string.h>
@@ -137,8 +138,54 @@ journal_rollback_section(uns new_pool, struct journal_item *oldj, byte *msg)
   }
 }
 
+/* Initialization */
+
+static struct section {
+  struct section *prev;
+  byte *name;
+  struct cf_section *sec;
+} *sections;
+
+void
+cf_declare_section(byte *name, struct cf_section *sec)
+{
+  struct section *s = sections;
+  for (; s; s=s->prev)
+    if (!strcasecmp(s->name, name))
+      die("Cannot register cf_section %s twice", name);
+  s = xmalloc(sizeof(struct section));
+  s->prev = sections;
+  s->name = name;
+  s->sec = sec;
+  sections = s;
+}
+
+void
+cf_init_section(byte *name, struct cf_section *sec, void *ptr)
+{
+  if (sec->size)
+    bzero(ptr, sec->size);
+  for (uns i=0; sec->cfg[i].cls; i++)
+    if (sec->cfg[i].cls == CC_SECTION)
+      cf_init_section(sec->cfg[i].name, sec->cfg[i].u.sec, ptr + (addr_int_t) sec->cfg[i].ptr);
+    else if (sec->cfg[i].cls == CC_LIST)
+      clist_init(sec->cfg[i].ptr);
+  byte *msg = sec->init(ptr);
+  if (msg)
+    die("Cannot initialize section %s: %s", name, msg);
+}
+
+static void
+global_init(void)
+{
+  for (struct section *s=sections; s; s=s->prev)
+    cf_init_section(s->name, s->sec, NULL);
+}
+
 /* Safe loading and reloading */
 
+byte *cf_def_file = DEFAULT_CONFIG;
+
 static byte *load_file(byte *file);
 static byte *load_string(byte *string);
 
@@ -348,3 +395,4 @@ cf_parse_dyn(uns number, byte **pars, void **ptr, enum cf_type type)
   * (uns*) (*ptr - parsers[type].size) = number;
   return ((cf_parser*) parsers[type].parser) (number, pars, *ptr);
 }
+
index cd2deb51be0312e506d358098c949a3bf3a5bcc0..94e85160557e2606a07628855448ee6835a436e6 100644 (file)
@@ -48,7 +48,7 @@ struct cf_item {
   void *ptr;                           // pointer to a global variable or an offset in a section
   union {
     enum cf_type type;                 // type of a static or dynamic attribute
-    struct cf_section *sub;            // declaration of a section or a list
+    struct cf_section *sec;            // declaration of a section or a list
     cf_parser *par;                    // parser function
   } u;
 };
@@ -71,8 +71,8 @@ struct clist;
 #define CF_STATIC(n,p,T,t,c)   { .cls = CC_STATIC, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*), .u.type = CT_##T }
 #define CF_DYNAMIC(n,p,T,t,c)  { .cls = CC_DYNAMIC, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**), .u.type = CT_##T }
 #define CF_PARSER(n,p,f,c)     { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f }
-#define CF_SECTION(n,p,s)      { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sub = s }
-#define CF_LIST(n,p,s)         { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,struct clist*), .u.sub = s }
+#define CF_SECTION(n,p,s)      { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s }
+#define CF_LIST(n,p,s)         { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,struct clist*), .u.sec = s }
 /* Configuration items for basic types */
 #define CF_INT(n,p)            CF_STATIC(n,p,INT,int,1)
 #define CF_INT_ARY(n,p,c)      CF_STATIC(n,p,INT,int,c)
@@ -104,7 +104,12 @@ byte *cf_printf(char *fmt, ...);
 extern uns cf_need_journal;
 void cf_journal_block(void *ptr, uns len);
 
+/* Declaration */
+void cf_declare_section(byte *name, struct cf_section *sec);
+void cf_init_section(byte *name, struct cf_section *sec, void *ptr);
+
 /* Safe reloading and loading of configuration files */
+extern byte *cf_def_file;
 byte *cf_reload(byte *file);
 byte *cf_load(byte *file);
 byte *cf_set(byte *string);