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)
30 /* this is the option struct and currently it is only used internally to
31 * the module and other structs. */
32 typedef struct SilcConfigOptionStruct {
33 char *name; /* *lowercase* name of the option */
34 SilcConfigType type; /* important: the type of the returned value */
35 SilcConfigCallback cb; /* the value handler */
36 const SilcConfigTable *subtable; /* used if type is SILC_CONFIG_ARG_BLOCK */
37 void *context; /* context passed to the callback function */
38 struct SilcConfigOptionStruct *next;
41 /* unique for each config file (and included ones) */
42 struct SilcConfigFileObject {
43 char *filename; /* the original filename opened */
44 int level; /* parsing level, how many nested
45 silc_config_main we have */
46 char *base; /* this is a fixed pointer to the base location */
47 char *p; /* the Parser poitner */
48 SilcUInt32 len; /* fixed length of the whole file */
49 SilcUInt32 line; /* current parsing line, strictly linked to p */
50 SilcBool included; /* wether this file is main or included */
53 /* We need the entity to base our block-style parsing on */
54 struct SilcConfigEntityObject {
55 SilcConfigOption *opts; /* known options list */
56 SilcConfigFile *file; /* parsing file object */
59 /* access error descriptions only with silc_config_strerror() */
60 static char *errorstrs[] = {
61 "-OK", /* SILC_CONFIG_OK */
62 "-SILENT", /* SILC_CONFIG_ESILENT */
63 "-PRINTLINE", /* SILC_CONFIG_EPRINTLINE */
64 "Invalid syntax", /* SILC_CONFIG_EGENERIC */
65 "Internal error! Please report this bug", /* SILC_CONFIG_EINTERNAL */
66 "Can't open specified file", /* SILC_CONFIG_ECANTOPEN */
67 "Expected open-brace '{'", /* SILC_CONFIG_EOPENBRACE */
68 "Missing close-brace '}'", /* SILC_CONFIG_ECLOSEBRACE */
69 "Invalid data type", /* SILC_CONFIG_ETYPE */
70 "Unknown option", /* SILC_CONFIG_EBADOPTION */
71 "Invalid text", /* SILC_CONFIG_EINVALIDTEXT */
72 "Double option specification", /* SILC_CONFIG_EDOUBLE */
73 "Expected data but not found", /* SILC_CONFIG_EEXPECTED */
74 "Expected '='", /* SILC_CONFIG_EEXPECTEDEQUAL */
75 "Unexpected data", /* SILC_CONFIG_EUNEXPECTED */
76 "Missing mandatory fields", /* SILC_CONFIG_EMISSFIELDS */
77 "Missing ';'", /* SILC_CONFIG_EMISSCOLON */
80 /* return string describing SilcConfig's error code */
81 char *silc_config_strerror(int errnum)
83 if ((errnum < 0) || (errnum >= sizeof(errorstrs)/sizeof(*errorstrs)) ||
84 (errorstrs[errnum] == NULL)) {
85 char *defret = "-INVALIDERROR";
88 return errorstrs[errnum];
91 /* Begin of internal SilcConfig's text util functions */
93 /* Points the first non-space character */
94 static void my_trim_spaces(SilcConfigFile *file)
96 register char *r = file->p;
97 while ((*r != '\0' && *r != EOF) && isspace((int)*r))
98 if (*r++ == '\n') file->line++;
102 /* Skips the current line until newline (lf or cr) */
103 static void my_skip_line(SilcConfigFile *file)
105 register char *r = file->p;
106 while ((*r != '\0' && *r != EOF) && (*r != '\n') && (*r != '\r')) r++;
107 file->p = ((*r != '\0' && *r != EOF) ? r + 1 : r);
111 /* Obtains a text token from the current position until first separator.
112 * a separator is any non alphanumeric character nor "_" or "-" */
113 static char *my_next_token(SilcConfigFile *file, char *to)
116 my_trim_spaces(file);
118 while (isalnum((int)*o) || (*o == '_') || (*o == '-'))
125 /* Obtains a string from the current position. The only difference from
126 * next_token() is that quoted-strings are also accepted */
127 static char *my_get_string(SilcConfigFile *file, char *to)
130 my_trim_spaces(file);
133 char *quot = strchr(++o, '"');
135 if (!quot) { /* XXX FIXME: gotta do something here */
136 printf("Bullshit, missing matching \"");
145 /* update stream pointer */
149 /* we don't need quote parsing, fall-back to token extractor */
150 my_next_token(file, to);
154 /* Skips all comment lines and spaces lines until first useful character */
155 static void my_skip_comments(SilcConfigFile *file)
158 my_trim_spaces(file);
159 if (*file->p != '#') return;
164 /* End of internal text functions
165 * Next section contains SilcConfig internal config utils */
167 /* find an option in the list by name and returns its pointer */
168 static SilcConfigOption *silc_config_find_option(SilcConfigEntity ent,
171 SilcConfigOption *tmp;
172 for (tmp = ent->opts; tmp; tmp = tmp->next) {
173 if (!strcasecmp(tmp->name, name))
179 /* Converts a string in the type specified. returns a dynamically
180 * allocated pointer. */
181 static void *silc_config_marshall(SilcConfigType type, const char *val)
185 SilcBool val_boolean;
190 case SILC_CONFIG_ARG_TOGGLE:
191 if (!strcasecmp(val, "yes") || !strcasecmp(val, "true") ||
192 !strcasecmp(val, "on") || !strcasecmp(val, "1")) {
195 else if (!strcasecmp(val, "no") || !strcasecmp(val, "false") ||
196 !strcasecmp(val, "off") || !strcasecmp(val, "0")) {
201 pt = silc_calloc(1, sizeof(val_boolean));
202 *(SilcBool *)pt = (SilcBool) val_boolean;
204 case SILC_CONFIG_ARG_INT:
205 val_int = (int) strtol(val, &val_tmp, 0);
206 if (*val_tmp) /* error converting string */
208 pt = silc_calloc(1, sizeof(val_int));
209 *(int *)pt = val_int;
211 case SILC_CONFIG_ARG_SIZE:
212 val_size = (SilcUInt32) strtol(val, &val_tmp, 0);
214 return NULL; /* really wrong, there must be at least one digit */
215 /* Search for a designator */
216 switch (tolower((int)val_tmp[0])) {
217 case '\0': /* None */
219 case 'k': /* Kilobytes */
220 val_size *= (SilcUInt32) 1024;
222 case 'm': /* Megabytes */
223 val_size *= (SilcUInt32) (1024 * 1024);
226 val_size *= (SilcUInt32) (1024 * 1024 * 1024);
231 /* the string must die here */
234 pt = silc_calloc(1, sizeof(val_size));
235 *(SilcUInt32 *)pt = val_size;
237 case SILC_CONFIG_ARG_STR: /* the only difference between STR and STRE is */
238 if (!val[0]) /* that STR cannot be empty, while STRE can. */
240 case SILC_CONFIG_ARG_STRE:
241 pt = (void *) strdup(val);
243 /* following types are not supposed to have a return value */
244 case SILC_CONFIG_ARG_BLOCK:
245 case SILC_CONFIG_ARG_NONE:
254 /* End of internal functions */
257 /* Tries to open the config file and returns a valid SilcConfigFile object
258 * or NULL if failed */
260 SilcConfigFile *silc_config_open(const char *configfile)
266 if (!(buffer = silc_file_readfile(configfile, &filelen, NULL)))
269 ret = silc_calloc(1, sizeof(*ret));
270 ret->filename = strdup(configfile);
271 ret->base = ret->p = buffer;
273 ret->line = 1; /* line count, start from first line */
277 /* Frees a file object */
279 void silc_config_close(SilcConfigFile *file)
282 silc_free(file->filename);
283 memset(file->base, 'F', file->len);
284 silc_free(file->base);
285 memset(file, 'F', sizeof(*file));
290 /* initializes a SilcConfigEntity pointer allocation */
292 SilcConfigEntity silc_config_init(SilcConfigFile *file)
294 SilcConfigEntity ret;
299 SILC_CONFIG_DEBUG(("Allocating new config entity"));
300 ret = silc_calloc(1, sizeof(*ret));
305 /* Returns the original filename of the object file */
307 char *silc_config_get_filename(SilcConfigFile *file)
310 return file->filename;
314 /* Returns the current line that file parsing arrived at */
316 SilcUInt32 silc_config_get_line(SilcConfigFile *file)
323 /* Returns a pointer to the beginning of the requested line. If the line
324 * was not found, NULL is returned */
326 char *silc_config_read_line(SilcConfigFile *file, SilcUInt32 line)
330 char *ret = NULL, *endbuf;
332 if (!file || (line <= 0))
334 for (p = file->base; *p && (*p != EOF); p++) {
343 if ((endbuf = strchr(p, '\n'))) {
346 ret = silc_memdup(p, len);
348 ret = silc_memdup(p, strlen(p));
353 /* Convenience function to read the current parsed line */
355 char *silc_config_read_current_line(SilcConfigFile *file)
357 return silc_config_read_line(file, file->line);
360 /* (Private) destroy a SilcConfigEntity */
362 static void silc_config_destroy(SilcConfigEntity ent, SilcBool destroy_opts)
364 SilcConfigOption *oldopt, *nextopt;
365 SILC_CONFIG_DEBUG(("Freeing config entity [ent=0x%x] [opts=0x%x]",
366 (SilcUInt32) ent, (SilcUInt32) ent->opts));
368 /* if she wants to preserve options just free the object struct */
372 for (oldopt = ent->opts; oldopt; oldopt = nextopt) {
373 nextopt = oldopt->next;
374 memset(oldopt->name, 'F', strlen(oldopt->name) + 1);
375 silc_free(oldopt->name);
376 memset(oldopt, 'F', sizeof(*oldopt));
381 memset(ent, 'F', sizeof(*ent));
385 /* Registers a new option in the specified entity.
386 * Returns TRUE on success, FALSE if already registered. */
388 SilcBool silc_config_register(SilcConfigEntity ent, const char *name,
389 SilcConfigType type, SilcConfigCallback cb,
390 const SilcConfigTable *subtable, void *context)
392 SilcConfigOption *newopt;
393 SILC_CONFIG_DEBUG(("Register new option=\"%s\" "
394 "type=%u cb=0x%08x context=0x%08x",
395 name, type, (SilcUInt32) cb, (SilcUInt32) context));
397 /* if we are registering a block, make sure there is a specified sub-table */
398 if (!ent || !name || ((type == SILC_CONFIG_ARG_BLOCK) && !subtable))
401 /* don't register a reserved tag */
402 if (!strcasecmp(name, "include"))
405 /* check if an option was previously registered */
406 if (silc_config_find_option(ent, name)) {
407 SILC_LOG_DEBUG(("Error: Can't register \"%s\" twice.", name));
411 /* allocate and append the new option */
412 newopt = silc_calloc(1, sizeof(*newopt));
413 newopt->name = strdup(name);
416 newopt->subtable = subtable;
417 newopt->context = context;
419 /* append this option to the list */
423 SilcConfigOption *tmp;
424 for (tmp = ent->opts; tmp->next; tmp = tmp->next);
430 /* Register a new option table in the specified config entity */
432 SilcBool silc_config_register_table(SilcConfigEntity ent,
433 const SilcConfigTable table[], void *context)
438 SILC_CONFIG_DEBUG(("Registering table"));
439 /* XXX FIXME: some potability checks needed - really? */
440 for (i = 0; table[i].name; i++) {
441 if (!silc_config_register(ent, table[i].name, table[i].type,
442 table[i].callback, table[i].subtable, context))
450 static int silc_config_main_internal(SilcConfigEntity ent)
452 SilcConfigFile *file = ent->file;
455 /* loop throught statements */
458 SilcConfigOption *thisopt;
460 /* makes it pointing to the next interesting char */
461 my_skip_comments(file);
463 if (**p == '\0' || **p == EOF) {
464 if (file->level > 1) /* cannot get eof in a sub-level! */
465 return SILC_CONFIG_EEXPECTED;
468 /* check if we completed this (sub) section (it doesn't matter if this
469 * is the main section) */
471 if (file->level < 2) /* can't be! must be at least one sub-block */
472 return SILC_CONFIG_EUNEXPECTED;
476 //SILC_LOG_HEXDUMP(("Preparing lookup at line=%lu", file->line), *p, 16);
478 /* obtain the keyword */
479 my_next_token(file, buf);
480 SILC_CONFIG_DEBUG(("Looking up keyword=\"%s\" [line=%lu]",
483 /* handle special directive */
484 if (!strcasecmp(buf, "include")) {
486 SilcConfigFile *inc_file;
487 SilcConfigEntity inc_ent;
489 my_trim_spaces(file); /* prepare next char */
491 /* Now trying to include the specified file. The included file will
492 * be allowed to include sub-files but it will preserve the block-level
493 * of the including block. Note that the included file won't be allowed
494 * to raise the block level of the including block. */
496 my_get_string(file, buf); /* get the filename */
497 SILC_LOG_DEBUG(("Including file \"%s\"", buf));
498 /* before getting on, check if this row is REALLY complete */
500 return SILC_CONFIG_EMISSCOLON;
502 /* open the file and start the parsing */
503 inc_file = silc_config_open(buf);
504 if (!inc_file) /* does it point a valid filename? */
505 return SILC_CONFIG_ECANTOPEN;
506 inc_file->included = TRUE;
508 /* create a new entity and hack it to use the same options */
509 inc_ent = silc_config_init(inc_file);
510 inc_ent->opts = ent->opts;
511 ret = silc_config_main(inc_ent);
514 * If the included file returned an error, the application will probably
515 * want to output some kind of error message. Because of this, we can't
516 * destroy THIS file object. The hack works this way: The application
517 * expects to destroy the originally created object file, so we'll swap
518 * the original file with the included file. */
520 SilcConfigFile tmp_file;
521 SILC_CONFIG_DEBUG(("SWAPPING FILE OBJECTS"));
522 memcpy(&tmp_file, inc_file, sizeof(tmp_file));
523 memcpy(inc_file, file, sizeof(tmp_file));
524 silc_config_close(inc_file);
525 memcpy(file, &tmp_file, sizeof(tmp_file));
528 /* otherwise if no errors encoured, continue normally */
529 silc_config_close(inc_file);
530 continue; /* this one is handled */
533 /* we have a registered option (it can also be a sub-block) */
534 thisopt = silc_config_find_option(ent, buf);
536 return SILC_CONFIG_EBADOPTION;
538 my_trim_spaces(file); /* prepare next char */
540 /* option type is a block? */
541 if (thisopt->type == SILC_CONFIG_ARG_BLOCK) {
543 SilcConfigEntity sub_ent;
545 SILC_CONFIG_DEBUG(("Entering sub-block"));
547 return SILC_CONFIG_EOPENBRACE;
548 /* build the new entity for this sub-block */
549 sub_ent = silc_config_init(ent->file);
550 /* use the previous specified table to describe this block's options */
551 silc_config_register_table(sub_ent, thisopt->subtable, thisopt->context);
552 /* run this block! */
553 ret = silc_config_main(sub_ent);
554 SILC_CONFIG_DEBUG(("Returned from sub-block [ret=%d]", ret));
556 if (ret) /* now check the result */
559 /* now call block clean-up callback (if any) */
562 SILC_CONFIG_DEBUG(("Now calling clean-up callback"));
563 ret = thisopt->cb(thisopt->type, thisopt->name, file->line, NULL,
566 SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
570 /* Do we want ';' to be mandatory after close brace? */
572 return SILC_CONFIG_EMISSCOLON;
574 else if (thisopt->type == SILC_CONFIG_ARG_NONE) {
575 /* before getting on, check if this row is REALLY complete */
577 return SILC_CONFIG_EMISSCOLON;
578 SILC_CONFIG_DEBUG(("Triggering callback for none"));
580 thisopt->cb(thisopt->type, thisopt->name, file->line,
581 NULL, thisopt->context);
586 int ret = 0; /* very important in case of no cb */
589 return SILC_CONFIG_EEXPECTEDEQUAL;
591 my_get_string(file, buf); /* get the option argument */
592 SILC_CONFIG_DEBUG(("With argument=\"%s\"", buf));
594 /* before getting on, check if this row is REALLY complete */
596 return SILC_CONFIG_EMISSCOLON;
598 /* convert the option argument to the right format */
599 pt = silc_config_marshall(thisopt->type, buf);
601 return SILC_CONFIG_EINVALIDTEXT;
603 ret = thisopt->cb(thisopt->type, thisopt->name, file->line,
604 pt, thisopt->context);
606 /* since we have to free "pt" both on failure and on success, we
607 assume that ret == 0 if we didn't actually call any cb. */
610 SILC_CONFIG_DEBUG(("Callback refused the value [ret=%d]", ret));
620 return SILC_CONFIG_OK;
625 int silc_config_main(SilcConfigEntity ent)
627 SilcConfigFile *file = ent->file;
630 /* don't silently accept a NULL entity */
632 ret = SILC_CONFIG_EGENERIC;
636 /* call the real main and store the result */
638 SILC_CONFIG_DEBUG(("[Lev=%d] Entering config parsing core", file->level));
639 ret = silc_config_main_internal(ent);
640 SILC_CONFIG_DEBUG(("[Lev=%d] Quitting main [ret=%d]", file->level, ret));
641 if (!file->level) /* when swap happens, we could close a file twice */
645 /* If this file was included don't destroy the options set because it is
646 * the same of the including block. Although if this entity is in a
647 * sub-block created inside the included file, this options set must be
650 if ((file->level != 0) || (file->included != TRUE))
651 silc_config_destroy(ent, TRUE);
653 silc_config_destroy(ent, FALSE);