Integer type name change.
[silc.git] / lib / silcutil / silcconfig.c
index 188ffa2b66a46e171b703db2a92162281b537d72..460cfd1214a247a2e149fd77e205f4c3e786eeb7 100644 (file)
@@ -45,8 +45,8 @@ struct SilcConfigFileObject {
   int level;   /* parsing level, how many nested silc_config_main we have */
   char *base;  /* this is a fixed pointer to the base location */
   char *p;     /* the Parser poitner */
-  uint32 len;  /* fixed length of the whole file */
-  uint32 line; /* current parsing line, strictly linked to p */
+  SilcUInt32 len;      /* fixed length of the whole file */
+  SilcUInt32 line;     /* current parsing line, strictly linked to p */
   bool included; /* wether this file is main or included */
 };
 
@@ -72,7 +72,7 @@ static char *errorstrs[] = {
   "Expected data but not found",               /* SILC_CONFIG_EEXPECTED */
   "Expected '='",                              /* SILC_CONFIG_EEXPECTEDEQUAL */
   "Unexpected data",                           /* SILC_CONFIG_EUNEXPECTED */
-  "Missing needed fields",                     /* SILC_CONFIG_EMISSFIELDS */
+  "Missing mandatory fields",                  /* SILC_CONFIG_EMISSFIELDS */
   "Missing ';'",                               /* SILC_CONFIG_EMISSCOLON */
 };
 
@@ -177,7 +177,7 @@ static void *silc_config_marshall(SilcConfigType type, const char *val)
   int val_int;
   bool val_bool;
   char *val_tmp;
-  uint32 val_size;
+  SilcUInt32 val_size;
 
   switch (type) {
     case SILC_CONFIG_ARG_TOGGLE:
@@ -202,7 +202,7 @@ static void *silc_config_marshall(SilcConfigType type, const char *val)
       *(int *)pt = val_int;
       return pt;
     case SILC_CONFIG_ARG_SIZE:
-      val_size = (uint32) strtol(val, &val_tmp, 0);
+      val_size = (SilcUInt32) strtol(val, &val_tmp, 0);
       if (val == val_tmp)
        return NULL; /* really wrong, there must be at least one digit */
       /* Search for a designator */
@@ -210,13 +210,13 @@ static void *silc_config_marshall(SilcConfigType type, const char *val)
        case '\0': /* None */
          break;
        case 'k': /* Kilobytes */
-         val_size *= (uint32) 1024;
+         val_size *= (SilcUInt32) 1024;
          break;
        case 'm': /* Megabytes */
-         val_size *= (uint32) (1024 * 1024);
+         val_size *= (SilcUInt32) (1024 * 1024);
          break;
        case 'g':
-         val_size *= (uint32) (1024 * 1024 * 1024);
+         val_size *= (SilcUInt32) (1024 * 1024 * 1024);
          break;
        default:
          return NULL;
@@ -225,7 +225,7 @@ static void *silc_config_marshall(SilcConfigType type, const char *val)
       if (val_tmp[1])
        return NULL;
       pt = silc_calloc(1, sizeof(val_size));
-      *(uint32 *)pt = val_size;
+      *(SilcUInt32 *)pt = val_size;
       return pt;
     case SILC_CONFIG_ARG_STR: /* the only difference between STR and STRE is */
       if (!val[0])           /* that STR cannot be empty, while STRE can.  */
@@ -253,13 +253,13 @@ static void *silc_config_marshall(SilcConfigType type, const char *val)
 SilcConfigFile *silc_config_open(char *configfile)
 {
   char *buffer;
-  uint32 filelen;
+  SilcUInt32 filelen;
   SilcConfigFile *ret;
 
   if (!(buffer = silc_file_readfile(configfile, &filelen)))
     return NULL;
 
-  ret = (SilcConfigFile *) silc_calloc(1, sizeof(*ret));
+  ret = silc_calloc(1, sizeof(*ret));
   ret->filename = strdup(configfile);
   ret->base = ret->p = buffer;
   ret->len = filelen;
@@ -273,8 +273,8 @@ void silc_config_close(SilcConfigFile *file)
 {
   if (file) {
     /* XXX FIXME: this check could probably be removed later */
-    uint32 my_len = (uint32) (strchr(file->base, EOF) - file->base);
-    SILC_CONFIG_DEBUG(("file=0x%x name=\"%s\" level=%d line=%lu", (uint32) file,
+    SilcUInt32 my_len = (SilcUInt32) (strchr(file->base, EOF) - file->base);
+    SILC_CONFIG_DEBUG(("file=0x%x name=\"%s\" level=%d line=%lu", (SilcUInt32) file,
                        file->filename, file->level, file->line));
     if (my_len != file->len) {
       fprintf(stderr, "FATAL ERROR: saved len and current len does not match!\n");
@@ -293,10 +293,12 @@ void silc_config_close(SilcConfigFile *file)
 SilcConfigEntity silc_config_init(SilcConfigFile *file)
 {
   SilcConfigEntity ret;
+
   if (!file)
     return NULL;
+
   SILC_CONFIG_DEBUG(("Allocating new config entity"));
-  ret = (SilcConfigEntity) silc_calloc(1, sizeof(*ret));
+  ret = silc_calloc(1, sizeof(*ret));
   ret->file = file;
   return ret;
 };
@@ -312,7 +314,7 @@ char *silc_config_get_filename(SilcConfigFile *file)
 
 /* Returns the current line that file parsing arrived at */
 
-uint32 silc_config_get_line(SilcConfigFile *file)
+SilcUInt32 silc_config_get_line(SilcConfigFile *file)
 {
   if (file)
     return file->line;
@@ -322,7 +324,7 @@ uint32 silc_config_get_line(SilcConfigFile *file)
 /* Returns a pointer to the beginning of the requested line.  If the line
  * was not found, NULL is returned */
 
-char *silc_config_read_line(SilcConfigFile *file, uint32 line)
+char *silc_config_read_line(SilcConfigFile *file, SilcUInt32 line)
 {
   register char *p;
   int len;
@@ -365,7 +367,7 @@ static void silc_config_destroy(SilcConfigEntity ent)
 {
   SilcConfigOption *oldopt, *nextopt;
   SILC_CONFIG_DEBUG(("Freeing config entity [ent=0x%x] [opts=0x%x]",
-                       (uint32) ent, (uint32) ent->opts));
+                       (SilcUInt32) ent, (SilcUInt32) ent->opts));
   for (oldopt = ent->opts; oldopt; oldopt = nextopt) {
     nextopt = oldopt->next;
     memset(oldopt->name, 'F', strlen(oldopt->name) + 1);
@@ -377,37 +379,40 @@ static void silc_config_destroy(SilcConfigEntity ent)
   silc_free(ent);
 }
 
-/* Registers a new option in the specified entity */
+/* Registers a new option in the specified entity.
+ * Returns TRUE on success, FALSE if already registered. */
 
-void silc_config_register(SilcConfigEntity ent, const char *name,
+bool silc_config_register(SilcConfigEntity ent, const char *name,
                          SilcConfigType type, SilcConfigCallback cb,
                          const SilcConfigTable *subtable, void *context)
 {
   SilcConfigOption *newopt;
   SILC_CONFIG_DEBUG(("Register new option=\"%s\" type=%u cb=0x%08x context=0x%08x",
-               name, type, (uint32) cb, (uint32) context));
+               name, type, (SilcUInt32) cb, (SilcUInt32) context));
 
-  if (!ent || !name)
-    return;
   /* if we are registering a block, make sure there is a specified sub-table */
-  if ((type == SILC_CONFIG_ARG_BLOCK) && !subtable)
-    return;
-  /* refuse special tag */
+  if (!ent || !name || ((type == SILC_CONFIG_ARG_BLOCK) && !subtable))
+    return FALSE;
+
+  /* don't register a reserved tag */
   if (!strcasecmp(name, "include"))
-    return;
+    return FALSE;
+
+  /* check if an option was previously registered */
   if (silc_config_find_option(ent, name)) {
-    fprintf(stderr, "Internal Error: Option double registered\n");
-    abort();
+    SILC_LOG_DEBUG(("Error: Can't register \"%s\" twice.", name));
+    return FALSE;
   }
 
   /* allocate and append the new option */
-  newopt = (SilcConfigOption *) silc_calloc(1, sizeof(*newopt));
+  newopt = silc_calloc(1, sizeof(*newopt));
   newopt->name = strdup(name);
   newopt->type = type;
   newopt->cb = cb;
   newopt->subtable = subtable;
   newopt->context = context;
 
+  /* append this option to the list */
   if (!ent->opts)
     ent->opts = newopt;
   else {
@@ -415,21 +420,25 @@ void silc_config_register(SilcConfigEntity ent, const char *name,
     for (tmp = ent->opts; tmp->next; tmp = tmp->next);
     tmp->next = newopt;
   }
+  return TRUE;
 }
 
 /* Register a new option table in the specified config entity */
 
-void silc_config_register_table(SilcConfigEntity ent,
+bool silc_config_register_table(SilcConfigEntity ent,
                                const SilcConfigTable table[], void *context)
 {
   int i;
-  if (!ent || !table) return;
+  if (!ent || !table)
+    return FALSE;
   SILC_CONFIG_DEBUG(("Registering table"));
-  /* FIXME: some potability checks needed */
+  /* XXX FIXME: some potability checks needed - really? */
   for (i = 0; table[i].name; i++) {
-    silc_config_register(ent, table[i].name, table[i].type,
-                        table[i].callback, table[i].subtable, context);
+    if (!silc_config_register(ent, table[i].name, table[i].type,
+                             table[i].callback, table[i].subtable, context))
+      return FALSE;
   }
+  return TRUE;
 }
 
 /* ... */
@@ -544,9 +553,14 @@ static int silc_config_main_internal(SilcConfigEntity ent)
 
       /* now call block clean-up callback (if any) */
       if (thisopt->cb) {
-       SILC_CONFIG_DEBUG(("Now calling clean-up callback (if any)"));
-       thisopt->cb(thisopt->type, thisopt->name, file->line,
-                   NULL, thisopt->context);
+       int ret;
+       SILC_CONFIG_DEBUG(("Now calling clean-up callback"));
+       ret = thisopt->cb(thisopt->type, thisopt->name, file->line, NULL,
+                         thisopt->context);
+       if (ret) {
+         SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
+         return ret;
+       }
       }
       /* Do we want ';' to be mandatory after close brace? */
       if (*(*p)++ != ';')