Merged from silc_1_0_branch.
[silc.git] / lib / silcutil / silcconfig.c
index dcd55b93792e306b4495b59a489a1f5740c8f670..1f3e4699fdf79d20891961d4463f4b507dd69a73 100644 (file)
@@ -41,13 +41,14 @@ typedef struct SilcConfigOptionStruct {
 
 /* unique for each config file (and included ones) */
 struct SilcConfigFileObject {
-  char *filename; /* the original filename opened */
-  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 */
-  bool included; /* wether this file is main or included */
+  char *filename;      /* the original filename opened */
+  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 */
+  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 */
 };
 
 /* We need the entity to base our block-style parsing on */
@@ -58,22 +59,23 @@ struct SilcConfigEntityObject {
 
 /* access error descriptions only with silc_config_strerror() */
 static char *errorstrs[] = {
-  "-OK",                                       /* SILC_CONFIG_OK */
-  "-SILENT",                                   /* SILC_CONFIG_ESILENT */
-  "Invalid syntax",                            /* SILC_CONFIG_EGENERIC */
-  "Internal error! Please report this bug",    /* SILC_CONFIG_EINTERNAL */
-  "Can't open specified file",                 /* SILC_CONFIG_ECANTOPEN */
-  "Expected open-brace '{'",                   /* SILC_CONFIG_EOPENBRACE */
-  "Missing close-brace '}'",                   /* SILC_CONFIG_ECLOSEBRACE */
-  "Invalid data type",                         /* SILC_CONFIG_ETYPE */
-  "Unknown option",                            /* SILC_CONFIG_EBADOPTION */
-  "Invalid text",                              /* SILC_CONFIG_EINVALIDTEXT */
-  "Double option specification",               /* SILC_CONFIG_EDOUBLE */
-  "Expected data but not found",               /* SILC_CONFIG_EEXPECTED */
-  "Expected '='",                              /* SILC_CONFIG_EEXPECTEDEQUAL */
-  "Unexpected data",                           /* SILC_CONFIG_EUNEXPECTED */
-  "Missing mandatory fields",                  /* SILC_CONFIG_EMISSFIELDS */
-  "Missing ';'",                               /* SILC_CONFIG_EMISSCOLON */
+  "-OK",                                     /* SILC_CONFIG_OK */
+  "-SILENT",                                 /* SILC_CONFIG_ESILENT */
+  "-PRINTLINE",                                      /* SILC_CONFIG_EPRINTLINE */
+  "Invalid syntax",                          /* SILC_CONFIG_EGENERIC */
+  "Internal error! Please report this bug",   /* SILC_CONFIG_EINTERNAL */
+  "Can't open specified file",               /* SILC_CONFIG_ECANTOPEN */
+  "Expected open-brace '{'",                 /* SILC_CONFIG_EOPENBRACE */
+  "Missing close-brace '}'",                 /* SILC_CONFIG_ECLOSEBRACE */
+  "Invalid data type",                       /* SILC_CONFIG_ETYPE */
+  "Unknown option",                          /* SILC_CONFIG_EBADOPTION */
+  "Invalid text",                            /* SILC_CONFIG_EINVALIDTEXT */
+  "Double option specification",             /* SILC_CONFIG_EDOUBLE */
+  "Expected data but not found",             /* SILC_CONFIG_EEXPECTED */
+  "Expected '='",                            /* SILC_CONFIG_EEXPECTEDEQUAL */
+  "Unexpected data",                         /* SILC_CONFIG_EUNEXPECTED */
+  "Missing mandatory fields",                /* SILC_CONFIG_EMISSFIELDS */
+  "Missing ';'",                             /* SILC_CONFIG_EMISSCOLON */
 };
 
 /* return string describing SilcConfig's error code */
@@ -177,7 +179,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 +204,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 +212,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 +227,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.  */
@@ -250,10 +252,10 @@ static void *silc_config_marshall(SilcConfigType type, const char *val)
 /* Tries to open the config file and returns a valid SilcConfigFile object
  * or NULL if failed */
 
-SilcConfigFile *silc_config_open(char *configfile)
+SilcConfigFile *silc_config_open(const char *configfile)
 {
   char *buffer;
-  uint32 filelen;
+  SilcUInt32 filelen;
   SilcConfigFile *ret;
 
   if (!(buffer = silc_file_readfile(configfile, &filelen)))
@@ -272,14 +274,6 @@ SilcConfigFile *silc_config_open(char *configfile)
 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,
-                       file->filename, file->level, file->line));
-    if (my_len != file->len) {
-      fprintf(stderr, "FATAL ERROR: saved len and current len does not match!\n");
-      abort();
-    }
     silc_free(file->filename);
     memset(file->base, 'F', file->len);
     silc_free(file->base);
@@ -314,7 +308,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;
@@ -324,11 +318,11 @@ 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;
-  char *ret, *endbuf;
+  char *ret = NULL, *endbuf;
 
   if (!file || (line <= 0))
     return NULL;
@@ -343,13 +337,10 @@ char *silc_config_read_line(SilcConfigFile *file, uint32 line)
  found:
   if ((endbuf = strchr(p, '\n'))) {
     len = endbuf - p;
-    ret = silc_calloc(len, sizeof(*ret));
-    strncpy(ret, p, len);
-    ret[len] = '\0';
-  }
-  else {
-    ret = silc_calloc(strlen(p), sizeof(*ret));
-    strcpy(ret, p);
+    if (len > 0)
+      ret = silc_memdup(p, len);
+  } else {
+    ret = silc_memdup(p, strlen(p));
   }
   return ret;
 }
@@ -363,11 +354,16 @@ char *silc_config_read_current_line(SilcConfigFile *file)
 
 /* (Private) destroy a SilcConfigEntity */
 
-static void silc_config_destroy(SilcConfigEntity ent)
+static void silc_config_destroy(SilcConfigEntity ent, bool destroy_opts)
 {
   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));
+
+  /* if she wants to preserve options just free the object struct */
+  if (!destroy_opts)
+    goto skip_sect;
+
   for (oldopt = ent->opts; oldopt; oldopt = nextopt) {
     nextopt = oldopt->next;
     memset(oldopt->name, 'F', strlen(oldopt->name) + 1);
@@ -375,6 +371,8 @@ static void silc_config_destroy(SilcConfigEntity ent)
     memset(oldopt, 'F', sizeof(*oldopt));
     silc_free(oldopt);
   }
+
+ skip_sect:
   memset(ent, 'F', sizeof(*ent));
   silc_free(ent);
 }
@@ -387,8 +385,9 @@ bool silc_config_register(SilcConfigEntity ent, const char *name,
                          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));
+  SILC_CONFIG_DEBUG(("Register new option=\"%s\" "
+                    "type=%u cb=0x%08x context=0x%08x",
+                    name, type, (SilcUInt32) cb, (SilcUInt32) context));
 
   /* if we are registering a block, make sure there is a specified sub-table */
   if (!ent || !name || ((type == SILC_CONFIG_ARG_BLOCK) && !subtable))
@@ -473,7 +472,8 @@ static int silc_config_main_internal(SilcConfigEntity ent)
 
     /* obtain the keyword */
     my_next_token(file, buf);
-    SILC_CONFIG_DEBUG(("Looking up keyword=\"%s\" [line=%lu]", buf, file->line));
+    SILC_CONFIG_DEBUG(("Looking up keyword=\"%s\" [line=%lu]",
+                      buf, file->line));
 
     /* handle special directive */
     if (!strcasecmp(buf, "include")) {
@@ -641,7 +641,9 @@ int silc_config_main(SilcConfigEntity ent)
    * destroyed. */
  main_cleanup:
   if ((file->level != 0) || (file->included != TRUE))
-    silc_config_destroy(ent);
+    silc_config_destroy(ent, TRUE);
+  else
+    silc_config_destroy(ent, FALSE);
 
  main_end:
   return ret;