5 Author: Giovanni Giacobbi <giovanni@giacobbi.net>
7 Copyright (C) 2002 - 2008 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.
20 #include "silcruntime.h"
22 /* limit debug logging verbosity */
24 #define SILC_CONFIG_DEBUG(fmt) SILC_LOG_DEBUG(fmt)
26 #define SILC_CONFIG_DEBUG(fmt)
31 /* this is the option struct and currently it is only used internally to
32 * the module and other structs. */
33 typedef struct SilcConfigOptionStruct {
34 char *name; /* *lowercase* name of the option */
35 SilcConfigType type; /* important: the type of the returned value */
36 SilcConfigCallback cb; /* the value handler */
37 const SilcConfigTable *subtable; /* used if type is SILC_CONFIG_ARG_BLOCK */
38 void *context; /* context passed to the callback function */
39 struct SilcConfigOptionStruct *next;
42 /* unique for each config file (and included ones) */
43 struct SilcConfigFileObject {
44 char *filename; /* the original filename opened */
45 int level; /* parsing level, how many nested
46 silc_config_main we have */
47 char *base; /* this is a fixed pointer to the base location */
48 char *p; /* the Parser poitner */
49 SilcUInt32 len; /* fixed length of the whole file */
50 SilcUInt32 line; /* current parsing line, strictly linked to p */
51 SilcBool included; /* wether this file is main or included */
54 /* We need the entity to base our block-style parsing on */
55 struct SilcConfigEntityObject {
56 SilcConfigOption *opts; /* known options list */
57 SilcConfigFile *file; /* parsing file object */
60 /* access error descriptions only with silc_config_strerror() */
61 static char *errorstrs[] = {
62 "-OK", /* SILC_CONFIG_OK */
63 "-SILENT", /* SILC_CONFIG_ESILENT */
64 "-PRINTLINE", /* SILC_CONFIG_EPRINTLINE */
65 "Invalid syntax", /* SILC_CONFIG_EGENERIC */
66 "Internal error! Please report this bug", /* SILC_CONFIG_EINTERNAL */
67 "Can't open specified file", /* SILC_CONFIG_ECANTOPEN */
68 "Expected open-brace '{'", /* SILC_CONFIG_EOPENBRACE */
69 "Missing close-brace '}'", /* SILC_CONFIG_ECLOSEBRACE */
70 "Invalid data type", /* SILC_CONFIG_ETYPE */
71 "Unknown option", /* SILC_CONFIG_EBADOPTION */
72 "Invalid text", /* SILC_CONFIG_EINVALIDTEXT */
73 "Double option specification", /* SILC_CONFIG_EDOUBLE */
74 "Expected data but not found", /* SILC_CONFIG_EEXPECTED */
75 "Expected '='", /* SILC_CONFIG_EEXPECTEDEQUAL */
76 "Unexpected data", /* SILC_CONFIG_EUNEXPECTED */
77 "Missing mandatory fields", /* SILC_CONFIG_EMISSFIELDS */
78 "Missing ';'", /* SILC_CONFIG_EMISSCOLON */
81 /* return string describing SilcConfig's error code */
82 char *silc_config_strerror(int errnum)
84 if ((errnum < 0) || (errnum >= sizeof(errorstrs)/sizeof(*errorstrs)) ||
85 (errorstrs[errnum] == NULL)) {
86 char *defret = "-INVALIDERROR";
89 return errorstrs[errnum];
92 /* Begin of internal SilcConfig's text util functions */
94 /* Points the first non-space character */
95 static void my_trim_spaces(SilcConfigFile *file)
97 register char *r = file->p;
98 while ((*r != '\0' && *r != EOF) && isspace((int)*r))
99 if (*r++ == '\n') file->line++;
103 /* Skips the current line until newline (lf or cr) */
104 static void my_skip_line(SilcConfigFile *file)
106 register char *r = file->p;
107 while ((*r != '\0' && *r != EOF) && (*r != '\n') && (*r != '\r')) r++;
108 file->p = ((*r != '\0' && *r != EOF) ? r + 1 : r);
112 /* Obtains a text token from the current position until first separator.
113 * a separator is any non alphanumeric character nor "_" or "-" */
114 static char *my_next_token(SilcConfigFile *file, char *to)
116 unsigned int count = 0;
118 my_trim_spaces(file);
120 while ((isalnum((int)*o) || (*o == '_') || (*o == '-')) && count < BUF_SIZE) {
129 /* Obtains a string from the current position. The only difference from
130 * next_token() is that quoted-strings are also accepted */
131 static char *my_get_string(SilcConfigFile *file, char *to)
134 my_trim_spaces(file);
137 unsigned int count = 0;
139 while (count < BUF_SIZE) {
150 if (count >= BUF_SIZE) { /* XXX FIXME: gotta do something here */
151 fprintf(stderr, "Bullshit, missing matching \"");
155 /* update stream pointer */
158 /* we don't need quote parsing, fall-back to token extractor */
159 my_next_token(file, to);
164 /* Skips all comment lines and spaces lines until first useful character */
165 static void my_skip_comments(SilcConfigFile *file)
168 my_trim_spaces(file);
169 if (*file->p != '#') return;
174 /* End of internal text functions
175 * Next section contains SilcConfig internal config utils */
177 /* find an option in the list by name and returns its pointer */
178 static SilcConfigOption *silc_config_find_option(SilcConfigEntity ent,
181 SilcConfigOption *tmp;
182 for (tmp = ent->opts; tmp; tmp = tmp->next) {
183 if (!strcasecmp(tmp->name, name))
189 /* Converts a string in the type specified. returns a dynamically
190 * allocated pointer. */
191 static void *silc_config_marshall(SilcConfigType type, const char *val)
195 SilcBool val_boolean;
200 case SILC_CONFIG_ARG_TOGGLE:
201 if (!strcasecmp(val, "yes") || !strcasecmp(val, "true") ||
202 !strcasecmp(val, "on") || !strcasecmp(val, "1")) {
205 else if (!strcasecmp(val, "no") || !strcasecmp(val, "false") ||
206 !strcasecmp(val, "off") || !strcasecmp(val, "0")) {
211 pt = silc_calloc(1, sizeof(val_boolean));
212 *(SilcBool *)pt = (SilcBool) val_boolean;
214 case SILC_CONFIG_ARG_INT:
215 val_int = (int) strtol(val, &val_tmp, 0);
216 if (*val_tmp) /* error converting string */
218 pt = silc_calloc(1, sizeof(val_int));
219 *(int *)pt = val_int;
221 case SILC_CONFIG_ARG_SIZE:
222 val_size = (SilcUInt32) strtol(val, &val_tmp, 0);
224 return NULL; /* really wrong, there must be at least one digit */
225 /* Search for a designator */
226 switch (tolower((int)val_tmp[0])) {
227 case '\0': /* None */
229 case 'k': /* Kilobytes */
230 val_size *= (SilcUInt32) 1024;
232 case 'm': /* Megabytes */
233 val_size *= (SilcUInt32) (1024 * 1024);
236 val_size *= (SilcUInt32) (1024 * 1024 * 1024);
241 /* the string must die here */
244 pt = silc_calloc(1, sizeof(val_size));
245 *(SilcUInt32 *)pt = val_size;
247 case SILC_CONFIG_ARG_STR: /* the only difference between STR and STRE is */
248 if (!val[0]) /* that STR cannot be empty, while STRE can. */
250 case SILC_CONFIG_ARG_STRE:
251 pt = (void *) strdup(val);
253 /* following types are not supposed to have a return value */
254 case SILC_CONFIG_ARG_BLOCK:
255 case SILC_CONFIG_ARG_NONE:
264 /* End of internal functions */
267 /* Tries to open the config file and returns a valid SilcConfigFile object
268 * or NULL if failed */
270 SilcConfigFile *silc_config_open(const char *configfile)
276 if (!(buffer = silc_file_readfile(configfile, &filelen, NULL)))
279 ret = silc_calloc(1, sizeof(*ret));
280 ret->filename = strdup(configfile);
281 ret->base = ret->p = buffer;
283 ret->line = 1; /* line count, start from first line */
287 /* Frees a file object */
289 void silc_config_close(SilcConfigFile *file)
292 silc_free(file->filename);
293 memset(file->base, 'F', file->len);
294 silc_free(file->base);
295 memset(file, 'F', sizeof(*file));
300 /* initializes a SilcConfigEntity pointer allocation */
302 SilcConfigEntity silc_config_init(SilcConfigFile *file)
304 SilcConfigEntity ret;
309 SILC_CONFIG_DEBUG(("Allocating new config entity"));
310 ret = silc_calloc(1, sizeof(*ret));
315 /* Returns the original filename of the object file */
317 char *silc_config_get_filename(SilcConfigFile *file)
320 return file->filename;
324 /* Returns the current line that file parsing arrived at */
326 SilcUInt32 silc_config_get_line(SilcConfigFile *file)
333 /* Returns a pointer to the beginning of the requested line. If the line
334 * was not found, NULL is returned */
336 char *silc_config_read_line(SilcConfigFile *file, SilcUInt32 line)
340 char *ret = NULL, *endbuf;
342 if (!file || (line <= 0))
344 for (p = file->base; *p && (*p != EOF); p++) {
353 if ((endbuf = strchr(p, '\n'))) {
356 ret = silc_memdup(p, len);
358 ret = silc_memdup(p, strlen(p));
363 /* Convenience function to read the current parsed line */
365 char *silc_config_read_current_line(SilcConfigFile *file)
367 return silc_config_read_line(file, file->line);
370 /* (Private) destroy a SilcConfigEntity */
372 static void silc_config_destroy(SilcConfigEntity ent, SilcBool destroy_opts)
374 SilcConfigOption *oldopt, *nextopt;
375 SILC_CONFIG_DEBUG(("Freeing config entity [ent=0x%x] [opts=0x%x]",
376 (SilcUInt32) ent, (SilcUInt32) ent->opts));
378 /* if she wants to preserve options just free the object struct */
382 for (oldopt = ent->opts; oldopt; oldopt = nextopt) {
383 nextopt = oldopt->next;
384 memset(oldopt->name, 'F', strlen(oldopt->name) + 1);
385 silc_free(oldopt->name);
386 memset(oldopt, 'F', sizeof(*oldopt));
391 memset(ent, 'F', sizeof(*ent));
395 /* Registers a new option in the specified entity.
396 * Returns TRUE on success, FALSE if already registered. */
398 SilcBool silc_config_register(SilcConfigEntity ent, const char *name,
399 SilcConfigType type, SilcConfigCallback cb,
400 const SilcConfigTable *subtable, void *context)
402 SilcConfigOption *newopt;
403 SILC_CONFIG_DEBUG(("Register new option=\"%s\" "
404 "type=%u cb=0x%08x context=0x%08x",
405 name, type, (SilcUInt32) cb, (SilcUInt32) context));
407 /* if we are registering a block, make sure there is a specified sub-table */
408 if (!ent || !name || ((type == SILC_CONFIG_ARG_BLOCK) && !subtable))
411 /* don't register a reserved tag */
412 if (!strcasecmp(name, "include"))
415 /* check if an option was previously registered */
416 if (silc_config_find_option(ent, name)) {
417 SILC_LOG_DEBUG(("Error: Can't register \"%s\" twice.", name));
421 /* allocate and append the new option */
422 newopt = silc_calloc(1, sizeof(*newopt));
423 newopt->name = strdup(name);
426 newopt->subtable = subtable;
427 newopt->context = context;
429 /* append this option to the list */
433 SilcConfigOption *tmp;
434 for (tmp = ent->opts; tmp->next; tmp = tmp->next);
440 /* Register a new option table in the specified config entity */
442 SilcBool silc_config_register_table(SilcConfigEntity ent,
443 const SilcConfigTable table[], void *context)
448 SILC_CONFIG_DEBUG(("Registering table"));
449 /* XXX FIXME: some potability checks needed - really? */
450 for (i = 0; table[i].name; i++) {
451 if (!silc_config_register(ent, table[i].name, table[i].type,
452 table[i].callback, table[i].subtable, context))
460 static int silc_config_main_internal(SilcConfigEntity ent)
462 SilcConfigFile *file = ent->file;
465 /* loop throught statements */
468 SilcConfigOption *thisopt;
470 /* makes it pointing to the next interesting char */
471 my_skip_comments(file);
473 if (**p == '\0' || **p == EOF) {
474 if (file->level > 1) /* cannot get eof in a sub-level! */
475 return SILC_CONFIG_EEXPECTED;
478 /* check if we completed this (sub) section (it doesn't matter if this
479 * is the main section) */
481 if (file->level < 2) /* can't be! must be at least one sub-block */
482 return SILC_CONFIG_EUNEXPECTED;
486 //SILC_LOG_HEXDUMP(("Preparing lookup at line=%lu", file->line), *p, 16);
488 /* obtain the keyword */
489 my_next_token(file, buf);
490 SILC_CONFIG_DEBUG(("Looking up keyword=\"%s\" [line=%lu]",
493 /* handle special directive */
494 if (!strcasecmp(buf, "include")) {
496 SilcConfigFile *inc_file;
497 SilcConfigEntity inc_ent;
499 my_trim_spaces(file); /* prepare next char */
501 /* Now trying to include the specified file. The included file will
502 * be allowed to include sub-files but it will preserve the block-level
503 * of the including block. Note that the included file won't be allowed
504 * to raise the block level of the including block. */
506 my_get_string(file, buf); /* get the filename */
507 SILC_LOG_DEBUG(("Including file \"%s\"", buf));
508 /* before getting on, check if this row is REALLY complete */
510 return SILC_CONFIG_EMISSCOLON;
512 /* open the file and start the parsing */
513 inc_file = silc_config_open(buf);
514 if (!inc_file) /* does it point a valid filename? */
515 return SILC_CONFIG_ECANTOPEN;
516 inc_file->included = TRUE;
518 /* create a new entity and hack it to use the same options */
519 inc_ent = silc_config_init(inc_file);
520 inc_ent->opts = ent->opts;
521 ret = silc_config_main(inc_ent);
524 * If the included file returned an error, the application will probably
525 * want to output some kind of error message. Because of this, we can't
526 * destroy THIS file object. The hack works this way: The application
527 * expects to destroy the originally created object file, so we'll swap
528 * the original file with the included file. */
530 SilcConfigFile tmp_file;
531 SILC_CONFIG_DEBUG(("SWAPPING FILE OBJECTS"));
532 memcpy(&tmp_file, inc_file, sizeof(tmp_file));
533 memcpy(inc_file, file, sizeof(tmp_file));
534 silc_config_close(inc_file);
535 memcpy(file, &tmp_file, sizeof(tmp_file));
538 /* otherwise if no errors encoured, continue normally */
539 silc_config_close(inc_file);
540 continue; /* this one is handled */
543 /* we have a registered option (it can also be a sub-block) */
544 thisopt = silc_config_find_option(ent, buf);
546 return SILC_CONFIG_EBADOPTION;
548 my_trim_spaces(file); /* prepare next char */
550 /* option type is a block? */
551 if (thisopt->type == SILC_CONFIG_ARG_BLOCK) {
553 SilcConfigEntity sub_ent;
555 SILC_CONFIG_DEBUG(("Entering sub-block"));
557 return SILC_CONFIG_EOPENBRACE;
558 /* build the new entity for this sub-block */
559 sub_ent = silc_config_init(ent->file);
560 /* use the previous specified table to describe this block's options */
561 silc_config_register_table(sub_ent, thisopt->subtable, thisopt->context);
562 /* run this block! */
563 ret = silc_config_main(sub_ent);
564 SILC_CONFIG_DEBUG(("Returned from sub-block [ret=%d]", ret));
566 if (ret) /* now check the result */
569 /* now call block clean-up callback (if any) */
572 SILC_CONFIG_DEBUG(("Now calling clean-up callback"));
573 ret = thisopt->cb(thisopt->type, thisopt->name, file->line, NULL,
576 SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
580 /* Do we want ';' to be mandatory after close brace? */
582 return SILC_CONFIG_EMISSCOLON;
584 else if (thisopt->type == SILC_CONFIG_ARG_NONE) {
585 /* before getting on, check if this row is REALLY complete */
587 return SILC_CONFIG_EMISSCOLON;
588 SILC_CONFIG_DEBUG(("Triggering callback for none"));
590 thisopt->cb(thisopt->type, thisopt->name, file->line,
591 NULL, thisopt->context);
596 int ret = 0; /* very important in case of no cb */
599 return SILC_CONFIG_EEXPECTEDEQUAL;
601 my_get_string(file, buf); /* get the option argument */
602 SILC_CONFIG_DEBUG(("With argument=\"%s\"", buf));
604 /* before getting on, check if this row is REALLY complete */
606 return SILC_CONFIG_EMISSCOLON;
608 /* convert the option argument to the right format */
609 pt = silc_config_marshall(thisopt->type, buf);
611 return SILC_CONFIG_EINVALIDTEXT;
613 ret = thisopt->cb(thisopt->type, thisopt->name, file->line,
614 pt, thisopt->context);
616 /* since we have to free "pt" both on failure and on success, we
617 assume that ret == 0 if we didn't actually call any cb. */
620 SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
630 return SILC_CONFIG_OK;
635 int silc_config_main(SilcConfigEntity ent)
637 SilcConfigFile *file = ent->file;
640 /* don't silently accept a NULL entity */
642 ret = SILC_CONFIG_EGENERIC;
646 /* call the real main and store the result */
648 SILC_CONFIG_DEBUG(("[Lev=%d] Entering config parsing core", file->level));
649 ret = silc_config_main_internal(ent);
650 SILC_CONFIG_DEBUG(("[Lev=%d] Quitting main [ret=%d]", file->level, ret));
651 if (!file->level) /* when swap happens, we could close a file twice */
655 /* If this file was included don't destroy the options set because it is
656 * the same of the including block. Although if this entity is in a
657 * sub-block created inside the included file, this options set must be
660 if ((file->level != 0) || (file->included != TRUE))
661 silc_config_destroy(ent, TRUE);
663 silc_config_destroy(ent, FALSE);