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)
29 /* this is the option struct and currently it is only used internally to
30 * the module and other structs. */
31 typedef struct SilcConfigOptionStruct {
32 char *name; /* *lowercase* name of the option */
33 SilcConfigType type; /* important: the type of the returned value */
34 SilcConfigCallback cb; /* the value handler */
35 const SilcConfigTable *subtable; /* used if type is SILC_CONFIG_ARG_BLOCK */
36 void *context; /* context passed to the callback function */
37 struct SilcConfigOptionStruct *next;
40 /* unique for each config file (and included ones) */
41 struct SilcConfigFileObject {
42 char *filename; /* the original filename opened */
43 int level; /* parsing level, how many nested
44 silc_config_main we have */
45 char *base; /* this is a fixed pointer to the base location */
46 char *p; /* the Parser poitner */
47 SilcUInt32 len; /* fixed length of the whole file */
48 SilcUInt32 line; /* current parsing line, strictly linked to p */
49 SilcBool included; /* wether this file is main or included */
52 /* We need the entity to base our block-style parsing on */
53 struct SilcConfigEntityObject {
54 SilcConfigOption *opts; /* known options list */
55 SilcConfigFile *file; /* parsing file object */
58 /* access error descriptions only with silc_config_strerror() */
59 static char *errorstrs[] = {
60 "-OK", /* SILC_CONFIG_OK */
61 "-SILENT", /* SILC_CONFIG_ESILENT */
62 "-PRINTLINE", /* SILC_CONFIG_EPRINTLINE */
63 "Invalid syntax", /* SILC_CONFIG_EGENERIC */
64 "Internal error! Please report this bug", /* SILC_CONFIG_EINTERNAL */
65 "Can't open specified file", /* SILC_CONFIG_ECANTOPEN */
66 "Expected open-brace '{'", /* SILC_CONFIG_EOPENBRACE */
67 "Missing close-brace '}'", /* SILC_CONFIG_ECLOSEBRACE */
68 "Invalid data type", /* SILC_CONFIG_ETYPE */
69 "Unknown option", /* SILC_CONFIG_EBADOPTION */
70 "Invalid text", /* SILC_CONFIG_EINVALIDTEXT */
71 "Double option specification", /* SILC_CONFIG_EDOUBLE */
72 "Expected data but not found", /* SILC_CONFIG_EEXPECTED */
73 "Expected '='", /* SILC_CONFIG_EEXPECTEDEQUAL */
74 "Unexpected data", /* SILC_CONFIG_EUNEXPECTED */
75 "Missing mandatory fields", /* SILC_CONFIG_EMISSFIELDS */
76 "Missing ';'", /* SILC_CONFIG_EMISSCOLON */
79 /* return string describing SilcConfig's error code */
80 char *silc_config_strerror(int errnum)
82 if ((errnum < 0) || (errnum >= sizeof(errorstrs)/sizeof(*errorstrs)) ||
83 (errorstrs[errnum] == NULL)) {
84 char *defret = "-INVALIDERROR";
87 return errorstrs[errnum];
90 /* Begin of internal SilcConfig's text util functions */
92 /* Points the first non-space character */
93 static void my_trim_spaces(SilcConfigFile *file)
95 register char *r = file->p;
96 while ((*r != '\0' && *r != EOF) && isspace((int)*r))
97 if (*r++ == '\n') file->line++;
101 /* Skips the current line until newline (lf or cr) */
102 static void my_skip_line(SilcConfigFile *file)
104 register char *r = file->p;
105 while ((*r != '\0' && *r != EOF) && (*r != '\n') && (*r != '\r')) r++;
106 file->p = ((*r != '\0' && *r != EOF) ? r + 1 : r);
110 /* Obtains a text token from the current position until first separator.
111 * a separator is any non alphanumeric character nor "_" or "-" */
112 static char *my_next_token(SilcConfigFile *file, char *to)
115 my_trim_spaces(file);
117 while (isalnum((int)*o) || (*o == '_') || (*o == '-'))
124 /* Obtains a string from the current position. The only difference from
125 * next_token() is that quoted-strings are also accepted */
126 static char *my_get_string(SilcConfigFile *file, char *to)
129 my_trim_spaces(file);
132 char *quot = strchr(++o, '"');
134 if (!quot) { /* XXX FIXME: gotta do something here */
135 printf("Bullshit, missing matching \"");
144 /* update stream pointer */
148 /* we don't need quote parsing, fall-back to token extractor */
149 my_next_token(file, to);
153 /* Skips all comment lines and spaces lines until first useful character */
154 static void my_skip_comments(SilcConfigFile *file)
157 my_trim_spaces(file);
158 if (*file->p != '#') return;
163 /* End of internal text functions
164 * Next section contains SilcConfig internal config utils */
166 /* find an option in the list by name and returns its pointer */
167 static SilcConfigOption *silc_config_find_option(SilcConfigEntity ent,
170 SilcConfigOption *tmp;
171 for (tmp = ent->opts; tmp; tmp = tmp->next) {
172 if (!strcasecmp(tmp->name, name))
178 /* Converts a string in the type specified. returns a dynamically
179 * allocated pointer. */
180 static void *silc_config_marshall(SilcConfigType type, const char *val)
184 SilcBool val_boolean;
189 case SILC_CONFIG_ARG_TOGGLE:
190 if (!strcasecmp(val, "yes") || !strcasecmp(val, "true") ||
191 !strcasecmp(val, "on") || !strcasecmp(val, "1")) {
194 else if (!strcasecmp(val, "no") || !strcasecmp(val, "false") ||
195 !strcasecmp(val, "off") || !strcasecmp(val, "0")) {
200 pt = silc_calloc(1, sizeof(val_boolean));
201 *(SilcBool *)pt = (SilcBool) val_boolean;
203 case SILC_CONFIG_ARG_INT:
204 val_int = (int) strtol(val, &val_tmp, 0);
205 if (*val_tmp) /* error converting string */
207 pt = silc_calloc(1, sizeof(val_int));
208 *(int *)pt = val_int;
210 case SILC_CONFIG_ARG_SIZE:
211 val_size = (SilcUInt32) strtol(val, &val_tmp, 0);
213 return NULL; /* really wrong, there must be at least one digit */
214 /* Search for a designator */
215 switch (tolower((int)val_tmp[0])) {
216 case '\0': /* None */
218 case 'k': /* Kilobytes */
219 val_size *= (SilcUInt32) 1024;
221 case 'm': /* Megabytes */
222 val_size *= (SilcUInt32) (1024 * 1024);
225 val_size *= (SilcUInt32) (1024 * 1024 * 1024);
230 /* the string must die here */
233 pt = silc_calloc(1, sizeof(val_size));
234 *(SilcUInt32 *)pt = val_size;
236 case SILC_CONFIG_ARG_STR: /* the only difference between STR and STRE is */
237 if (!val[0]) /* that STR cannot be empty, while STRE can. */
239 case SILC_CONFIG_ARG_STRE:
240 pt = (void *) strdup(val);
242 /* following types are not supposed to have a return value */
243 case SILC_CONFIG_ARG_BLOCK:
244 case SILC_CONFIG_ARG_NONE:
253 /* End of internal functions */
256 /* Tries to open the config file and returns a valid SilcConfigFile object
257 * or NULL if failed */
259 SilcConfigFile *silc_config_open(const char *configfile)
265 if (!(buffer = silc_file_readfile(configfile, &filelen, NULL)))
268 ret = silc_calloc(1, sizeof(*ret));
269 ret->filename = strdup(configfile);
270 ret->base = ret->p = buffer;
272 ret->line = 1; /* line count, start from first line */
276 /* Frees a file object */
278 void silc_config_close(SilcConfigFile *file)
281 silc_free(file->filename);
282 memset(file->base, 'F', file->len);
283 silc_free(file->base);
284 memset(file, 'F', sizeof(*file));
289 /* initializes a SilcConfigEntity pointer allocation */
291 SilcConfigEntity silc_config_init(SilcConfigFile *file)
293 SilcConfigEntity ret;
298 SILC_CONFIG_DEBUG(("Allocating new config entity"));
299 ret = silc_calloc(1, sizeof(*ret));
304 /* Returns the original filename of the object file */
306 char *silc_config_get_filename(SilcConfigFile *file)
309 return file->filename;
313 /* Returns the current line that file parsing arrived at */
315 SilcUInt32 silc_config_get_line(SilcConfigFile *file)
322 /* Returns a pointer to the beginning of the requested line. If the line
323 * was not found, NULL is returned */
325 char *silc_config_read_line(SilcConfigFile *file, SilcUInt32 line)
329 char *ret = NULL, *endbuf;
331 if (!file || (line <= 0))
333 for (p = file->base; *p && (*p != EOF); p++) {
342 if ((endbuf = strchr(p, '\n'))) {
345 ret = silc_memdup(p, len);
347 ret = silc_memdup(p, strlen(p));
352 /* Convenience function to read the current parsed line */
354 char *silc_config_read_current_line(SilcConfigFile *file)
356 return silc_config_read_line(file, file->line);
359 /* (Private) destroy a SilcConfigEntity */
361 static void silc_config_destroy(SilcConfigEntity ent, SilcBool destroy_opts)
363 SilcConfigOption *oldopt, *nextopt;
364 SILC_CONFIG_DEBUG(("Freeing config entity [ent=0x%x] [opts=0x%x]",
365 (SilcUInt32) ent, (SilcUInt32) ent->opts));
367 /* if she wants to preserve options just free the object struct */
371 for (oldopt = ent->opts; oldopt; oldopt = nextopt) {
372 nextopt = oldopt->next;
373 memset(oldopt->name, 'F', strlen(oldopt->name) + 1);
374 silc_free(oldopt->name);
375 memset(oldopt, 'F', sizeof(*oldopt));
380 memset(ent, 'F', sizeof(*ent));
384 /* Registers a new option in the specified entity.
385 * Returns TRUE on success, FALSE if already registered. */
387 SilcBool silc_config_register(SilcConfigEntity ent, const char *name,
388 SilcConfigType type, SilcConfigCallback cb,
389 const SilcConfigTable *subtable, void *context)
391 SilcConfigOption *newopt;
392 SILC_CONFIG_DEBUG(("Register new option=\"%s\" "
393 "type=%u cb=0x%08x context=0x%08x",
394 name, type, (SilcUInt32) cb, (SilcUInt32) context));
396 /* if we are registering a block, make sure there is a specified sub-table */
397 if (!ent || !name || ((type == SILC_CONFIG_ARG_BLOCK) && !subtable))
400 /* don't register a reserved tag */
401 if (!strcasecmp(name, "include"))
404 /* check if an option was previously registered */
405 if (silc_config_find_option(ent, name)) {
406 SILC_LOG_DEBUG(("Error: Can't register \"%s\" twice.", name));
410 /* allocate and append the new option */
411 newopt = silc_calloc(1, sizeof(*newopt));
412 newopt->name = strdup(name);
415 newopt->subtable = subtable;
416 newopt->context = context;
418 /* append this option to the list */
422 SilcConfigOption *tmp;
423 for (tmp = ent->opts; tmp->next; tmp = tmp->next);
429 /* Register a new option table in the specified config entity */
431 SilcBool silc_config_register_table(SilcConfigEntity ent,
432 const SilcConfigTable table[], void *context)
437 SILC_CONFIG_DEBUG(("Registering table"));
438 /* XXX FIXME: some potability checks needed - really? */
439 for (i = 0; table[i].name; i++) {
440 if (!silc_config_register(ent, table[i].name, table[i].type,
441 table[i].callback, table[i].subtable, context))
449 static int silc_config_main_internal(SilcConfigEntity ent)
451 SilcConfigFile *file = ent->file;
454 /* loop throught statements */
457 SilcConfigOption *thisopt;
459 /* makes it pointing to the next interesting char */
460 my_skip_comments(file);
462 if (**p == '\0' || **p == EOF) {
463 if (file->level > 1) /* cannot get eof in a sub-level! */
464 return SILC_CONFIG_EEXPECTED;
467 /* check if we completed this (sub) section (it doesn't matter if this
468 * is the main section) */
470 if (file->level < 2) /* can't be! must be at least one sub-block */
471 return SILC_CONFIG_EUNEXPECTED;
475 //SILC_LOG_HEXDUMP(("Preparing lookup at line=%lu", file->line), *p, 16);
477 /* obtain the keyword */
478 my_next_token(file, buf);
479 SILC_CONFIG_DEBUG(("Looking up keyword=\"%s\" [line=%lu]",
482 /* handle special directive */
483 if (!strcasecmp(buf, "include")) {
485 SilcConfigFile *inc_file;
486 SilcConfigEntity inc_ent;
488 my_trim_spaces(file); /* prepare next char */
490 /* Now trying to include the specified file. The included file will
491 * be allowed to include sub-files but it will preserve the block-level
492 * of the including block. Note that the included file won't be allowed
493 * to raise the block level of the including block. */
495 my_get_string(file, buf); /* get the filename */
496 SILC_LOG_DEBUG(("Including file \"%s\"", buf));
497 /* before getting on, check if this row is REALLY complete */
499 return SILC_CONFIG_EMISSCOLON;
501 /* open the file and start the parsing */
502 inc_file = silc_config_open(buf);
503 if (!inc_file) /* does it point a valid filename? */
504 return SILC_CONFIG_ECANTOPEN;
505 inc_file->included = TRUE;
507 /* create a new entity and hack it to use the same options */
508 inc_ent = silc_config_init(inc_file);
509 inc_ent->opts = ent->opts;
510 ret = silc_config_main(inc_ent);
513 * If the included file returned an error, the application will probably
514 * want to output some kind of error message. Because of this, we can't
515 * destroy THIS file object. The hack works this way: The application
516 * expects to destroy the originally created object file, so we'll swap
517 * the original file with the included file. */
519 SilcConfigFile tmp_file;
520 SILC_CONFIG_DEBUG(("SWAPPING FILE OBJECTS"));
521 memcpy(&tmp_file, inc_file, sizeof(tmp_file));
522 memcpy(inc_file, file, sizeof(tmp_file));
523 silc_config_close(inc_file);
524 memcpy(file, &tmp_file, sizeof(tmp_file));
527 /* otherwise if no errors encoured, continue normally */
528 silc_config_close(inc_file);
529 continue; /* this one is handled */
532 /* we have a registered option (it can also be a sub-block) */
533 thisopt = silc_config_find_option(ent, buf);
535 return SILC_CONFIG_EBADOPTION;
537 my_trim_spaces(file); /* prepare next char */
539 /* option type is a block? */
540 if (thisopt->type == SILC_CONFIG_ARG_BLOCK) {
542 SilcConfigEntity sub_ent;
544 SILC_CONFIG_DEBUG(("Entering sub-block"));
546 return SILC_CONFIG_EOPENBRACE;
547 /* build the new entity for this sub-block */
548 sub_ent = silc_config_init(ent->file);
549 /* use the previous specified table to describe this block's options */
550 silc_config_register_table(sub_ent, thisopt->subtable, thisopt->context);
551 /* run this block! */
552 ret = silc_config_main(sub_ent);
553 SILC_CONFIG_DEBUG(("Returned from sub-block [ret=%d]", ret));
555 if (ret) /* now check the result */
558 /* now call block clean-up callback (if any) */
561 SILC_CONFIG_DEBUG(("Now calling clean-up callback"));
562 ret = thisopt->cb(thisopt->type, thisopt->name, file->line, NULL,
565 SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
569 /* Do we want ';' to be mandatory after close brace? */
571 return SILC_CONFIG_EMISSCOLON;
573 else if (thisopt->type == SILC_CONFIG_ARG_NONE) {
574 /* before getting on, check if this row is REALLY complete */
576 return SILC_CONFIG_EMISSCOLON;
577 SILC_CONFIG_DEBUG(("Triggering callback for none"));
579 thisopt->cb(thisopt->type, thisopt->name, file->line,
580 NULL, thisopt->context);
585 int ret = 0; /* very important in case of no cb */
588 return SILC_CONFIG_EEXPECTEDEQUAL;
590 my_get_string(file, buf); /* get the option argument */
591 SILC_CONFIG_DEBUG(("With argument=\"%s\"", buf));
593 /* before getting on, check if this row is REALLY complete */
595 return SILC_CONFIG_EMISSCOLON;
597 /* convert the option argument to the right format */
598 pt = silc_config_marshall(thisopt->type, buf);
600 return SILC_CONFIG_EINVALIDTEXT;
602 ret = thisopt->cb(thisopt->type, thisopt->name, file->line,
603 pt, thisopt->context);
605 /* since we have to free "pt" both on failure and on success, we
606 assume that ret == 0 if we didn't actually call any cb. */
609 SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
619 return SILC_CONFIG_OK;
624 int silc_config_main(SilcConfigEntity ent)
626 SilcConfigFile *file = ent->file;
629 /* don't silently accept a NULL entity */
631 ret = SILC_CONFIG_EGENERIC;
635 /* call the real main and store the result */
637 SILC_CONFIG_DEBUG(("[Lev=%d] Entering config parsing core", file->level));
638 ret = silc_config_main_internal(ent);
639 SILC_CONFIG_DEBUG(("[Lev=%d] Quitting main [ret=%d]", file->level, ret));
640 if (!file->level) /* when swap happens, we could close a file twice */
644 /* If this file was included don't destroy the options set because it is
645 * the same of the including block. Although if this entity is in a
646 * sub-block created inside the included file, this options set must be
649 if ((file->level != 0) || (file->included != TRUE))
650 silc_config_destroy(ent, TRUE);
652 silc_config_destroy(ent, FALSE);