/* $Id$ * $URL$ * * plugin for config file access * * Copyright (C) 2003, 2004 Michael Reinelt * Copyright (C) 2004 The LCD4Linux Team * * This file is part of LCD4Linux. * * LCD4Linux is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * LCD4Linux is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * exported functions: * * int plugin_init_cfg (void) * adds cfg() function for config access * initializes variables from the config file * */ #include "config.h" #include #include #include #include "debug.h" #include "evaluator.h" #include "plugin.h" #include "cfg.h" #ifdef WITH_DMALLOC #include #endif static void load_variables(void) { char *section = "Variables"; char *list, *l, *p; char *expression; void *tree; RESULT result = { 0, 0, 0, NULL }; list = cfg_list(section); l = list; while (l != NULL) { while (*l == '|') l++; if ((p = strchr(l, '|')) != NULL) *p = '\0'; if (strchr(l, '.') != NULL || strchr(l, ':') != 0) { error("ignoring variable '%s' from %s: structures not allowed", l, cfg_source()); } else { expression = cfg_get_raw(section, l, ""); if (expression != NULL && *expression != '\0') { tree = NULL; if (Compile(expression, &tree) == 0 && Eval(tree, &result) == 0) { SetVariable(l, &result); debug("Variable %s = '%s' (%g)", l, R2S(&result), R2N(&result)); DelResult(&result); } else { error("error evaluating variable '%s' from %s", list, cfg_source()); } DelTree(tree); } } l = p ? p + 1 : NULL; } free(list); } static void my_cfg(RESULT * result, const int argc, RESULT * argv[]) { int i, len; char *value; char *buffer; /* calculate key length */ len = 0; for (i = 0; i < argc; i++) { len += strlen(R2S(argv[i])) + 1; } /* allocate key buffer */ buffer = malloc(len + 1); /* prepare key buffer */ *buffer = '\0'; for (i = 0; i < argc; i++) { strcat(buffer, "."); strcat(buffer, R2S(argv[i])); } /* buffer starts with '.', so cut off first char */ value = cfg_get("", buffer + 1, ""); /* store result */ SetResult(&result, R_STRING, value); /* free buffer again */ free(buffer); free(value); } int plugin_init_cfg(void) { /* load "Variables" section from cfg */ load_variables(); /* register plugin */ AddFunction("cfg", -1, my_cfg); return 0; } void plugin_exit_cfg(void) { /* empty */ } er&id=3d24b9bd4612dd293bde38976798288ecb4e1722&follow=1'>[lcd4linux @ 2001-03-16 16:40:17 by ltoetsch]ltoetsch12-24/+112 2001-03-16[lcd4linux @ 2001-03-16 09:28:08 by ltoetsch]ltoetsch5-11/+35 2001-03-15[lcd4linux @ 2001-03-15 15:49:22 by ltoetsch]ltoetsch3-14/+27 2001-03-15[lcd4linux @ 2001-03-15 14:25:05 by ltoetsch]ltoetsch11-125/+278 2001-03-15[lcd4linux @ 2001-03-15 11:10:53 by ltoetsch]ltoetsch1-1/+8 2001-03-15[lcd4linux @ 2001-03-15 09:47:13 by reinelt]reinelt3-16/+31 2001-03-15[lcd4linux @ 2001-03-15 09:13:22 by ltoetsch]ltoetsch1-2/+9 2001-03-14[lcd4linux @ 2001-03-14 16:47:41 by reinelt]reinelt3-41/+59 2001-03-14[lcd4linux @ 2001-03-14 15:30:53 by reinelt]reinelt1-21/+34 2001-03-14[lcd4linux @ 2001-03-14 15:14:59 by reinelt]reinelt8-152/+365 2001-03-14[lcd4linux @ 2001-03-14 13:19:29 by ltoetsch]ltoetsch16-128/+773 2001-03-13[lcd4linux @ 2001-03-13 08:34:15 by reinelt]reinelt6-29/+50 2001-03-13[lcd4linux @ 2001-03-13 08:03:41 by reinelt]reinelt3-1/+35 2001-03-13[lcd4linux @ 2001-03-13 07:53:00 by reinelt]reinelt2-13/+15 2001-03-13[lcd4linux @ 2001-03-13 07:41:22 by reinelt]reinelt3-2/+52 2001-03-12[lcd4linux @ 2001-03-12 13:44:58 by reinelt]reinelt8-58/+232 2001-03-12[lcd4linux @ 2001-03-12 12:49:24 by reinelt]reinelt2-63/+71 2001-03-12[lcd4linux @ 2001-03-12 12:39:36 by reinelt]reinelt14-448/+830 2001-03-09[lcd4linux @ 2001-03-09 16:24:30 by ltoetsch]ltoetsch2-155/+345 2001-03-09[lcd4linux @ 2001-03-09 15:04:53 by reinelt]reinelt3-103/+109 2001-03-09[lcd4linux @ 2001-03-09 14:30:01 by reinelt]reinelt1-0/+7 2001-03-09[lcd4linux @ 2001-03-09 14:24:49 by ltoetsch]ltoetsch3-7/+18 2001-03-09[lcd4linux @ 2001-03-09 13:08:11 by ltoetsch]ltoetsch12-86/+384 2001-03-09[lcd4linux @ 2001-03-09 12:14:24 by reinelt]reinelt4-21/+34 2001-03-08[lcd4linux @ 2001-03-08 15:25:38 by ltoetsch]ltoetsch9-93/+214 2001-03-08[lcd4linux @ 2001-03-08 09:02:04 by reinelt]reinelt4-133/+21 2001-03-08[lcd4linux @ 2001-03-08 08:39:54 by reinelt]reinelt2-4/+12 2001-03-07[lcd4linux @ 2001-03-07 18:10:21 by ltoetsch]ltoetsch10-110/+319 2001-03-05[lcd4linux @ 2001-03-05 18:20:21 by reinelt]reinelt2-13/+25 2001-03-04[lcd4linux @ 2001-03-04 15:01:12 by ltoetsch]ltoetsch1-3/+3 2001-03-02[lcd4linux @ 2001-03-02 20:18:12 by reinelt]reinelt3-36/+89