From d0c7529cb9833ede3595051df5599a11fe03be48 Mon Sep 17 00:00:00 2001 From: Reinhard Tartler Date: Thu, 5 Apr 2007 15:25:19 +0200 Subject: import new upstream --- evaluator.c | 185 ++++++++++-------------------------------------------------- 1 file changed, 29 insertions(+), 156 deletions(-) (limited to 'evaluator.c') diff --git a/evaluator.c b/evaluator.c index 8b4dbc9..f297491 100644 --- a/evaluator.c +++ b/evaluator.c @@ -1,4 +1,5 @@ -/* $Id: evaluator.c,v 1.31 2006/02/25 13:36:33 geronet Exp $ +/* $Id: evaluator.c 754 2007-01-21 06:19:40Z michael $ + * $URL: https://ssl.bulix.org/svn/lcd4linux/branches/0.10.1/evaluator.c $ * * expression evaluation * @@ -21,129 +22,6 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * - * - * $Log: evaluator.c,v $ - * Revision 1.31 2006/02/25 13:36:33 geronet - * updated indent.sh, applied coding style - * - * Revision 1.30 2006/01/30 12:53:08 reinelt - * replaced strncpy with strcpy where possible - * - * Revision 1.29 2006/01/30 06:11:36 reinelt - * changed Result->length to Result->size - * - * Revision 1.28 2006/01/30 05:47:38 reinelt - * graphic subsystem changed to full-color RGBA - * - * Revision 1.27 2006/01/28 15:36:18 harbaum - * Fix: string termination bug in eval() - * - * Revision 1.26 2006/01/21 09:40:20 reinelt - * Big Memory Leak in Evaluator fixed (thanks to Oliver Gehrke) - * - * Revision 1.25 2005/05/08 04:32:44 reinelt - * CodingStyle added and applied - * - * Revision 1.24 2005/03/30 04:57:50 reinelt - * Evaluator speedup: use bsearch for finding functions and variables - * - * Revision 1.23 2005/01/18 06:30:23 reinelt - * added (C) to all copyright statements - * - * Revision 1.22 2004/06/26 12:04:59 reinelt - * - * uh-oh... the last CVS log message messed up things a lot... - * - * Revision 1.21 2004/06/26 09:27:21 reinelt - * - * added '-W' to CFLAGS - * changed all C++ comments to C ones - * cleaned up a lot of signed/unsigned mistakes - * - * Revision 1.20 2004/06/20 10:09:56 reinelt - * - * 'const'ified the whole source - * - * Revision 1.19 2004/04/12 11:12:25 reinelt - * added plugin_isdn, removed old ISDN client - * fixed some real bad bugs in the evaluator - * - * Revision 1.18 2004/03/11 06:39:58 reinelt - * big patch from Martin: - * - reuse filehandles - * - memory leaks fixed - * - earlier busy-flag checking with HD44780 - * - reuse memory for strings in RESULT and hash - * - netdev_fast to wavid time-consuming regex - * - * Revision 1.17 2004/03/08 18:45:52 hejl - * fixed segfault when using string concatenation - * - * Revision 1.16 2004/03/08 04:33:08 reinelt - * string concatenation fixed - * - * Revision 1.15 2004/03/06 20:31:16 reinelt - * Complete rewrite of the evaluator to get rid of the code - * from mark Morley (because of license issues). - * The new Evaluator does a pre-compile of expressions, and - * stores them in trees. Therefore it should be reasonable faster... - * - * Revision 1.14 2004/03/03 03:47:04 reinelt - * big patch from Martin Hejl: - * - use qprintf() where appropriate - * - save CPU cycles on gettimeofday() - * - add quit() functions to free allocated memory - * - fixed lots of memory leaks - * - * Revision 1.13 2004/02/26 21:42:45 reinelt - * memory leak fixes from Martin - * - * Revision 1.12 2004/02/05 07:10:23 reinelt - * evaluator function names are no longer case-sensitive - * Crystalfontz Fan PWM control, Fan RPM monitoring, temperature monitoring - * - * Revision 1.11 2004/01/30 20:57:56 reinelt - * HD44780 patch from Martin Hejl - * dmalloc integrated - * - * Revision 1.10 2004/01/29 04:40:02 reinelt - * every .c file includes "config.h" now - * - * Revision 1.9 2004/01/15 07:47:02 reinelt - * debian/ postinst and watch added (did CVS forget about them?) - * evaluator: conditional expressions (a?b:c) added - * text widget nearly finished - * - * Revision 1.8 2004/01/12 03:51:01 reinelt - * evaluating the 'Variables' section in the config file - * - * Revision 1.7 2004/01/07 10:15:41 reinelt - * small glitch in evaluator fixed - * made config table sorted and access with bsearch(), - * which should be much faster - * - * Revision 1.6 2004/01/06 23:01:37 reinelt - * more copyright issues - * - * Revision 1.5 2004/01/06 17:33:45 reinelt - * Evaluator: functions with variable argument lists - * Evaluator: plugin_sample.c and README.Plugins added - * - * Revision 1.4 2004/01/06 15:19:12 reinelt - * Evaluator rearrangements... - * - * Revision 1.3 2003/10/11 06:01:52 reinelt - * renamed expression.{c,h} to client.{c,h} - * added config file client - * new functions 'AddNumericVariable()' and 'AddStringVariable()' - * new parameter '-i' for interactive mode - * - * Revision 1.2 2003/10/06 05:47:27 reinelt - * operators: ==, \!=, <=, >= - * - * Revision 1.1 2003/10/06 04:34:06 reinelt - * expression evaluator added - * */ @@ -314,11 +192,11 @@ static char *Word = NULL; static TOKEN Token = T_UNDEF; static OPERATOR Operator = O_UNDEF; -static VARIABLE *Variable = NULL; -static int nVariable = 0; +static VARIABLE Variable[255]; +static unsigned int nVariable = 0; static FUNCTION *Function = NULL; -static int nFunction = 0; +static unsigned int nFunction = 0; void DelResult(RESULT * result) @@ -396,7 +274,7 @@ RESULT *SetResult(RESULT ** result, const int type, const void *value) } -static RESULT *CopyResult(RESULT ** result, RESULT * value) +RESULT *CopyResult(RESULT ** result, RESULT * value) { if (*result == NULL) { if ((*result = NewResult()) == NULL) @@ -432,6 +310,10 @@ double R2N(RESULT * result) return 0.0; } + if (result->type == 0) { + return 0.0; + } + if (result->type & R_NUMBER) { return result->number; } @@ -454,6 +336,10 @@ char *R2S(RESULT * result) return NULL; } + if (result->type == 0) { + return NULL; + } + if (result->type & R_STRING) { return result->string; } @@ -474,29 +360,16 @@ char *R2S(RESULT * result) } -/* bsearch compare function for variables */ -static int LookupVariable(const void *a, const void *b) -{ - char *key = (char *) a; - VARIABLE *var = (VARIABLE *) b; - - return strcmp(key, var->name); -} - - -/* qsort compare function for variables */ -static int SortVariable(const void *a, const void *b) -{ - VARIABLE *va = (VARIABLE *) a; - VARIABLE *vb = (VARIABLE *) b; - - return strcmp(va->name, vb->name); -} - - static VARIABLE *FindVariable(const char *name) { - return bsearch(name, Variable, nVariable, sizeof(VARIABLE), LookupVariable); + unsigned int i; + + for (i = 0; i < nVariable; i++) { + if (strcmp(name, Variable[i].name) == 0) { + return &Variable[i]; + } + } + return NULL; } @@ -510,14 +383,16 @@ int SetVariable(const char *name, RESULT * value) return 1; } + if (nVariable >= sizeof(Variable) / sizeof(Variable[0])) { + error("Evaluator: cannot set variable <%s>: out of slots", name); + return -1; + } + nVariable++; - Variable = realloc(Variable, nVariable * sizeof(VARIABLE)); Variable[nVariable - 1].name = strdup(name); Variable[nVariable - 1].value = NULL; CopyResult(&Variable[nVariable - 1].value, value); - qsort(Variable, nVariable, sizeof(VARIABLE), SortVariable); - return 0; } @@ -546,14 +421,12 @@ int SetVariableString(const char *name, const char *value) void DeleteVariables(void) { - int i; + unsigned int i; for (i = 0; i < nVariable; i++) { free(Variable[i].name); FreeResult(Variable[i].value); } - free(Variable); - Variable = NULL; nVariable = 0; } @@ -600,7 +473,7 @@ int AddFunction(const char *name, const int argc, void (*func) ()) void DeleteFunctions(void) { - int i; + unsigned int i; for (i = 0; i < nFunction; i++) { free(Function[i].name); -- cgit v1.2.3