5 Author: Giovanni Giacobbi <giovanni@giacobbi.net>
7 Copyright (C) 2002 - 2006 Giovanni Giacobbi
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
23 /* limit debug logging verbosity */
25 #define SILC_CONFIG_DEBUG(fmt) SILC_LOG_DEBUG(fmt)
27 #define SILC_CONFIG_DEBUG(fmt)
32 /* this is the option struct and currently it is only used internally to
33 * the module and other structs. */
34 typedef struct SilcConfigOptionStruct {
35 char *name; /* *lowercase* name of the option */
36 SilcConfigType type; /* important: the type of the returned value */
37 SilcConfigCallback cb; /* the value handler */
38 const SilcConfigTable *subtable; /* used if type is SILC_CONFIG_ARG_BLOCK */
39 void *context; /* context passed to the callback function */
40 struct SilcConfigOptionStruct *next;
43 /* unique for each config file (and included ones) */
44 struct SilcConfigFileObject {
45 char *filename; /* the original filename opened */
46 int level; /* parsing level, how many nested
47 silc_config_main we have */
48 char *base; /* this is a fixed pointer to the base location */
49 char *p; /* the Parser poitner */
50 SilcUInt32 len; /* fixed length of the whole file */
51 SilcUInt32 line; /* current parsing line, strictly linked to p */
52 SilcBool included; /* wether this file is main or included */
55 /* We need the entity to base our block-style parsing on */
56 struct SilcConfigEntityObject {
57 SilcConfigOption *opts; /* known options list */
58 SilcConfigFile *file; /* parsing file object */
61 /* access error descriptions only with silc_config_strerror() */
62 static char *errorstrs[] = {
63 "-OK", /* SILC_CONFIG_OK */
64 "-SILENT", /* SILC_CONFIG_ESILENT */
65 "-PRINTLINE", /* SILC_CONFIG_EPRINTLINE */
66 "Invalid syntax", /* SILC_CONFIG_EGENERIC */
67 "Internal error! Please report this bug", /* SILC_CONFIG_EINTERNAL */
68 "Can't open specified file", /* SILC_CONFIG_ECANTOPEN */
69 "Expected open-brace '{'", /* SILC_CONFIG_EOPENBRACE */
70 "Missing close-brace '}'", /* SILC_CONFIG_ECLOSEBRACE */
71 "Invalid data type", /* SILC_CONFIG_ETYPE */
72 "Unknown option", /* SILC_CONFIG_EBADOPTION */
73 "Invalid text", /* SILC_CONFIG_EINVALIDTEXT */
74 "Double option specification", /* SILC_CONFIG_EDOUBLE */
75 "Expected data but not found", /* SILC_CONFIG_EEXPECTED */
76 "Expected '='", /* SILC_CONFIG_EEXPECTEDEQUAL */
77 "Unexpected data", /* SILC_CONFIG_EUNEXPECTED */
78 "Missing mandatory fields", /* SILC_CONFIG_EMISSFIELDS */
79 "Missing ';'", /* SILC_CONFIG_EMISSCOLON */
82 /* return string describing SilcConfig's error code */
83 char *silc_config_strerror(int errnum)
85 if ((errnum < 0) || (errnum >= sizeof(errorstrs)/sizeof(*errorstrs)) ||
86 (errorstrs[errnum] == NULL)) {
87 char *defret = "-INVALIDERROR";
90 return errorstrs[errnum];
93 /* Begin of internal SilcConfig's text util functions */
95 /* Points the first non-space character */
96 static void my_trim_spaces(SilcConfigFile *file)
98 register char *r = file->p;
99 while ((*r != '\0' && *r != EOF) && isspace((int)*r))
100 if (*r++ == '\n') file->line++;
104 /* Skips the current line until newline (lf or cr) */
105 static void my_skip_line(SilcConfigFile *file)
107 register char *r = file->p;
108 while ((*r != '\0' && *r != EOF) && (*r != '\n') && (*r != '\r')) r++;
109 file->p = ((*r != '\0' && *r != EOF) ? r + 1 : r);
113 /* Obtains a text token from the current position until first separator.
114 * a separator is any non alphanumeric character nor "_" or "-" */
115 static char *my_next_token(SilcConfigFile *file, char *to)
117 unsigned int count = 0;
119 my_trim_spaces(file);
121 while ((isalnum((int)*o) || (*o == '_') || (*o == '-')) && count < BUF_SIZE) {
130 /* Obtains a string from the current position. The only difference from
131 * next_token() is that quoted-strings are also accepted */
132 static char *my_get_string(SilcConfigFile *file, char *to)
135 my_trim_spaces(file);
138 unsigned int count = 0;
140 while (count < BUF_SIZE) {
151 if (count >= BUF_SIZE) { /* XXX FIXME: gotta do something here */
152 fprintf(stderr, "Bullshit, missing matching \"");
156 /* update stream pointer */
159 /* we don't need quote parsing, fall-back to token extractor */
160 my_next_token(file, to);
165 /* Skips all comment lines and spaces lines until first useful character */
166 static void my_skip_comments(SilcConfigFile *file)
169 my_trim_spaces(file);
170 if (*file->p != '#') return;
175 /* End of internal text functions
176 * Next section contains SilcConfig internal config utils */
178 /* find an option in the list by name and returns its pointer */
179 static SilcConfigOption *silc_config_find_option(SilcConfigEntity ent,
182 SilcConfigOption *tmp;
183 for (tmp = ent->opts; tmp; tmp = tmp->next) {
184 if (!strcasecmp(tmp->name, name))
190 /* Converts a string in the type specified. returns a dynamically
191 * allocated pointer. */
192 static void *silc_config_marshall(SilcConfigType type, const char *val)
196 SilcBool val_boolean;
201 case SILC_CONFIG_ARG_TOGGLE:
202 if (!strcasecmp(val, "yes") || !strcasecmp(val, "true") ||
203 !strcasecmp(val, "on") || !strcasecmp(val, "1")) {
206 else if (!strcasecmp(val, "no") || !strcasecmp(val, "false") ||
207 !strcasecmp(val, "off") || !strcasecmp(val, "0")) {
212 pt = silc_calloc(1, sizeof(val_boolean));
213 *(SilcBool *)pt = (SilcBool) val_boolean;
215 case SILC_CONFIG_ARG_INT:
216 val_int = (int) strtol(val, &val_tmp, 0);
217 if (*val_tmp) /* error converting string */
219 pt = silc_calloc(1, sizeof(val_int));
220 *(int *)pt = val_int;
222 case SILC_CONFIG_ARG_SIZE:
223 val_size = (SilcUInt32) strtol(val, &val_tmp, 0);
225 return NULL; /* really wrong, there must be at least one digit */
226 /* Search for a designator */
227 switch (tolower((int)val_tmp[0])) {
228 case '\0': /* None */
230 case 'k': /* Kilobytes */
231 val_size *= (SilcUInt32) 1024;
233 case 'm': /* Megabytes */
234 val_size *= (SilcUInt32) (1024 * 1024);
237 val_size *= (SilcUInt32) (1024 * 1024 * 1024);
242 /* the string must die here */
245 pt = silc_calloc(1, sizeof(val_size));
246 *(SilcUInt32 *)pt = val_size;
248 case SILC_CONFIG_ARG_STR: /* the only difference between STR and STRE is */
249 if (!val[0]) /* that STR cannot be empty, while STRE can. */
251 case SILC_CONFIG_ARG_STRE:
252 pt = (void *) strdup(val);
254 /* following types are not supposed to have a return value */
255 case SILC_CONFIG_ARG_BLOCK:
256 case SILC_CONFIG_ARG_NONE:
265 /* End of internal functions */
268 /* Tries to open the config file and returns a valid SilcConfigFile object
269 * or NULL if failed */
271 SilcConfigFile *silc_config_open(const char *configfile)
277 if (!(buffer = silc_file_readfile(configfile, &filelen)))
280 ret = silc_calloc(1, sizeof(*ret));
281 ret->filename = strdup(configfile);
282 ret->base = ret->p = buffer;
284 ret->line = 1; /* line count, start from first line */
288 /* Frees a file object */
290 void silc_config_close(SilcConfigFile *file)
293 silc_free(file->filename);
294 memset(file->base, 'F', file->len);
295 silc_free(file->base);
296 memset(file, 'F', sizeof(*file));
301 /* initializes a SilcConfigEntity pointer allocation */
303 SilcConfigEntity silc_config_init(SilcConfigFile *file)
305 SilcConfigEntity ret;
310 SILC_CONFIG_DEBUG(("Allocating new config entity"));
311 ret = silc_calloc(1, sizeof(*ret));
316 /* Returns the original filename of the object file */
318 char *silc_config_get_filename(SilcConfigFile *file)
321 return file->filename;
325 /* Returns the current line that file parsing arrived at */
327 SilcUInt32 silc_config_get_line(SilcConfigFile *file)
334 /* Returns a pointer to the beginning of the requested line. If the line
335 * was not found, NULL is returned */
337 char *silc_config_read_line(SilcConfigFile *file, SilcUInt32 line)
341 char *ret = NULL, *endbuf;
343 if (!file || (line <= 0))
345 for (p = file->base; *p && (*p != EOF); p++) {
354 if ((endbuf = strchr(p, '\n'))) {
357 ret = silc_memdup(p, len);
359 ret = silc_memdup(p, strlen(p));
364 /* Convenience function to read the current parsed line */
366 char *silc_config_read_current_line(SilcConfigFile *file)
368 return silc_config_read_line(file, file->line);
371 /* (Private) destroy a SilcConfigEntity */
373 static void silc_config_destroy(SilcConfigEntity ent, SilcBool destroy_opts)
375 SilcConfigOption *oldopt, *nextopt;
376 SILC_CONFIG_DEBUG(("Freeing config entity [ent=0x%x] [opts=0x%x]",
377 (SilcUInt32) ent, (SilcUInt32) ent->opts));
379 /* if she wants to preserve options just free the object struct */
383 for (oldopt = ent->opts; oldopt; oldopt = nextopt) {
384 nextopt = oldopt->next;
385 memset(oldopt->name, 'F', strlen(oldopt->name) + 1);
386 silc_free(oldopt->name);
387 memset(oldopt, 'F', sizeof(*oldopt));
392 memset(ent, 'F', sizeof(*ent));
396 /* Registers a new option in the specified entity.
397 * Returns TRUE on success, FALSE if already registered. */
399 SilcBool silc_config_register(SilcConfigEntity ent, const char *name,
400 SilcConfigType type, SilcConfigCallback cb,
401 const SilcConfigTable *subtable, void *context)
403 SilcConfigOption *newopt;
404 SILC_CONFIG_DEBUG(("Register new option=\"%s\" "
405 "type=%u cb=0x%08x context=0x%08x",
406 name, type, (SilcUInt32) cb, (SilcUInt32) context));
408 /* if we are registering a block, make sure there is a specified sub-table */
409 if (!ent || !name || ((type == SILC_CONFIG_ARG_BLOCK) && !subtable))
412 /* don't register a reserved tag */
413 if (!strcasecmp(name, "include"))
416 /* check if an option was previously registered */
417 if (silc_config_find_option(ent, name)) {
418 SILC_LOG_DEBUG(("Error: Can't register \"%s\" twice.", name));
422 /* allocate and append the new option */
423 newopt = silc_calloc(1, sizeof(*newopt));
424 newopt->name = strdup(name);
427 newopt->subtable = subtable;
428 newopt->context = context;
430 /* append this option to the list */
434 SilcConfigOption *tmp;
435 for (tmp = ent->opts; tmp->next; tmp = tmp->next);
441 /* Register a new option table in the specified config entity */
443 SilcBool silc_config_register_table(SilcConfigEntity ent,
444 const SilcConfigTable table[], void *context)
449 SILC_CONFIG_DEBUG(("Registering table"));
450 /* XXX FIXME: some potability checks needed - really? */
451 for (i = 0; table[i].name; i++) {
452 if (!silc_config_register(ent, table[i].name, table[i].type,
453 table[i].callback, table[i].subtable, context))
461 static int silc_config_main_internal(SilcConfigEntity ent)
463 SilcConfigFile *file = ent->file;
466 /* loop throught statements */
469 SilcConfigOption *thisopt;
471 /* makes it pointing to the next interesting char */
472 my_skip_comments(file);
474 if (**p == '\0' || **p == EOF) {
475 if (file->level > 1) /* cannot get eof in a sub-level! */
476 return SILC_CONFIG_EEXPECTED;
479 /* check if we completed this (sub) section (it doesn't matter if this
480 * is the main section) */
482 if (file->level < 2) /* can't be! must be at least one sub-block */
483 return SILC_CONFIG_EUNEXPECTED;
487 //SILC_LOG_HEXDUMP(("Preparing lookup at line=%lu", file->line), *p, 16);
489 /* obtain the keyword */
490 my_next_token(file, buf);
491 SILC_CONFIG_DEBUG(("Looking up keyword=\"%s\" [line=%lu]",
494 /* handle special directive */
495 if (!strcasecmp(buf, "include")) {
497 SilcConfigFile *inc_file;
498 SilcConfigEntity inc_ent;
500 my_trim_spaces(file); /* prepare next char */
502 /* Now trying to include the specified file. The included file will
503 * be allowed to include sub-files but it will preserve the block-level
504 * of the including block. Note that the included file won't be allowed
505 * to raise the block level of the including block. */
507 my_get_string(file, buf); /* get the filename */
508 SILC_LOG_DEBUG(("Including file \"%s\"", buf));
509 /* before getting on, check if this row is REALLY complete */
511 return SILC_CONFIG_EMISSCOLON;
513 /* open the file and start the parsing */
514 inc_file = silc_config_open(buf);
515 if (!inc_file) /* does it point a valid filename? */
516 return SILC_CONFIG_ECANTOPEN;
517 inc_file->included = TRUE;
519 /* create a new entity and hack it to use the same options */
520 inc_ent = silc_config_init(inc_file);
521 inc_ent->opts = ent->opts;
522 ret = silc_config_main(inc_ent);
525 * If the included file returned an error, the application will probably
526 * want to output some kind of error message. Because of this, we can't
527 * destroy THIS file object. The hack works this way: The application
528 * expects to destroy the originally created object file, so we'll swap
529 * the original file with the included file. */
531 SilcConfigFile tmp_file;
532 SILC_CONFIG_DEBUG(("SWAPPING FILE OBJECTS"));
533 memcpy(&tmp_file, inc_file, sizeof(tmp_file));
534 memcpy(inc_file, file, sizeof(tmp_file));
535 silc_config_close(inc_file);
536 memcpy(file, &tmp_file, sizeof(tmp_file));
539 /* otherwise if no errors encoured, continue normally */
540 silc_config_close(inc_file);
541 continue; /* this one is handled */
544 /* we have a registered option (it can also be a sub-block) */
545 thisopt = silc_config_find_option(ent, buf);
547 return SILC_CONFIG_EBADOPTION;
549 my_trim_spaces(file); /* prepare next char */
551 /* option type is a block? */
552 if (thisopt->type == SILC_CONFIG_ARG_BLOCK) {
554 SilcConfigEntity sub_ent;
556 SILC_CONFIG_DEBUG(("Entering sub-block"));
558 return SILC_CONFIG_EOPENBRACE;
559 /* build the new entity for this sub-block */
560 sub_ent = silc_config_init(ent->file);
561 /* use the previous specified table to describe this block's options */
562 silc_config_register_table(sub_ent, thisopt->subtable, thisopt->context);
563 /* run this block! */
564 ret = silc_config_main(sub_ent);
565 SILC_CONFIG_DEBUG(("Returned from sub-block [ret=%d]", ret));
567 if (ret) /* now check the result */
570 /* now call block clean-up callback (if any) */
573 SILC_CONFIG_DEBUG(("Now calling clean-up callback"));
574 ret = thisopt->cb(thisopt->type, thisopt->name, file->line, NULL,
577 SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
581 /* Do we want ';' to be mandatory after close brace? */
583 return SILC_CONFIG_EMISSCOLON;
585 else if (thisopt->type == SILC_CONFIG_ARG_NONE) {
586 /* before getting on, check if this row is REALLY complete */
588 return SILC_CONFIG_EMISSCOLON;
589 SILC_CONFIG_DEBUG(("Triggering callback for none"));
591 thisopt->cb(thisopt->type, thisopt->name, file->line,
592 NULL, thisopt->context);
597 int ret = 0; /* very important in case of no cb */
600 return SILC_CONFIG_EEXPECTEDEQUAL;
602 my_get_string(file, buf); /* get the option argument */
603 SILC_CONFIG_DEBUG(("With argument=\"%s\"", buf));
605 /* before getting on, check if this row is REALLY complete */
607 return SILC_CONFIG_EMISSCOLON;
609 /* convert the option argument to the right format */
610 pt = silc_config_marshall(thisopt->type, buf);
612 return SILC_CONFIG_EINVALIDTEXT;
614 ret = thisopt->cb(thisopt->type, thisopt->name, file->line,
615 pt, thisopt->context);
617 /* since we have to free "pt" both on failure and on success, we
618 assume that ret == 0 if we didn't actually call any cb. */
621 SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
631 return SILC_CONFIG_OK;
636 int silc_config_main(SilcConfigEntity ent)
638 SilcConfigFile *file = ent->file;
641 /* don't silently accept a NULL entity */
643 ret = SILC_CONFIG_EGENERIC;
647 /* call the real main and store the result */
649 SILC_CONFIG_DEBUG(("[Lev=%d] Entering config parsing core", file->level));
650 ret = silc_config_main_internal(ent);
651 SILC_CONFIG_DEBUG(("[Lev=%d] Quitting main [ret=%d]", file->level, ret));
652 if (!file->level) /* when swap happens, we could close a file twice */
656 /* If this file was included don't destroy the options set because it is
657 * the same of the including block. Although if this entity is in a
658 * sub-block created inside the included file, this options set must be
661 if ((file->level != 0) || (file->included != TRUE))
662 silc_config_destroy(ent, TRUE);
664 silc_config_destroy(ent, FALSE);