silcconfig.c
- Author: Johnny Mnemonic <johnny@themnemonic.org>
+ Author: Giovanni Giacobbi <giovanni@giacobbi.net>
- Copyright (C) 1997 - 2002 Pekka Riikonen
+ Copyright (C) 2002 - 2003 Giovanni Giacobbi
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
/* 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 */
+ 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 */
+ bool included; /* wether this file is main or included */
};
/* We need the entity to base our block-style parsing on */
/* 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 */
static void my_trim_spaces(SilcConfigFile *file)
{
register char *r = file->p;
- while (isspace(*r))
+ while ((*r != '\0' && *r != EOF) && isspace(*r))
if (*r++ == '\n') file->line++;
file->p = r;
}
static void my_skip_line(SilcConfigFile *file)
{
register char *r = file->p;
- while (*r && (*r != '\n') && (*r != '\r')) r++;
- file->p = (*r ? r + 1 : r);
+ while ((*r != '\0' && *r != EOF) && (*r != '\n') && (*r != '\r')) r++;
+ file->p = ((*r != '\0' && *r != EOF) ? r + 1 : r);
file->line++;
}
/* Obtains a text token from the current position until first separator.
}
return NULL;
}
-/* ... */
+/* Converts a string in the type specified. returns a dynamically
+ * allocated pointer. */
static void *silc_config_marshall(SilcConfigType type, const char *val)
{
void *pt;
void silc_config_close(SilcConfigFile *file)
{
if (file) {
- /* XXX FIXME: this check could probably be removed later */
- 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");
- abort();
- }
silc_free(file->filename);
memset(file->base, 'F', file->len);
silc_free(file->base);
{
register char *p;
int len;
- char *ret, *endbuf;
+ char *ret = NULL, *endbuf;
if (!file || (line <= 0))
return NULL;
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;
}
/* (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]",
(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);
memset(oldopt, 'F', sizeof(*oldopt));
silc_free(oldopt);
}
+
+ skip_sect:
memset(ent, 'F', sizeof(*ent));
silc_free(ent);
}
const SilcConfigTable *subtable, void *context)
{
SilcConfigOption *newopt;
- SILC_CONFIG_DEBUG(("Register new option=\"%s\" type=%u cb=0x%08x context=0x%08x",
- name, type, (SilcUInt32) cb, (SilcUInt32) 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))
/* 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")) {
}
else {
void *pt;
- int ret;
+ int ret = 0; /* very important in case of no cb */
if (*(*p)++ != '=')
return SILC_CONFIG_EEXPECTEDEQUAL;
pt = silc_config_marshall(thisopt->type, buf);
if (!pt)
return SILC_CONFIG_EINVALIDTEXT;
- if (thisopt->cb) {
+ if (thisopt->cb)
ret = thisopt->cb(thisopt->type, thisopt->name, file->line,
pt, thisopt->context);
- if (ret) {
- SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
- return ret;
- }
- }
+
+ /* since we have to free "pt" both on failure and on success, we
+ assume that ret == 0 if we didn't actually call any cb. */
silc_free(pt);
+ if (ret) {
+ SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
+ return ret;
+ }
}
continue;
* 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;