From b626a0b1272e703d01a8001ddd7bab7b3cd5b6e3 Mon Sep 17 00:00:00 2001 From: reinelt <> Date: Tue, 13 Jan 2004 10:03:01 +0000 Subject: [lcd4linux @ 2004-01-13 10:03:01 by reinelt] new util 'hash' for associative arrays new plugin 'cpuinfo' --- Makefile.am | 4 +- Makefile.in | 37 ++++++++-------- hash.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ hash.h | 52 ++++++++++++++++++++++ plugin.c | 12 ++++-- plugin_cfg.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++ plugin_cpuinfo.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ plugin_sample.c | 7 ++- 8 files changed, 458 insertions(+), 23 deletions(-) create mode 100644 hash.c create mode 100644 hash.h create mode 100644 plugin_cfg.c create mode 100644 plugin_cpuinfo.c diff --git a/Makefile.am b/Makefile.am index 0702780..5e2058c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,6 +18,7 @@ lcd4linux_DEPENDENCIES = @DRIVERS@ lcd4linux_SOURCES = \ lcd4linux.c \ pid.c pid.h \ +hash.c hash.h \ parser.c parser.h \ processor.c processor.h \ layout.c layout.h \ @@ -29,8 +30,9 @@ plugin.c plugin.h \ plugin_math.c \ plugin_string.c \ plugin_cfg.c \ -plugin_xmms.c \ +plugin_cpuinfo.c \ plugin_i2c_sensors.c \ +plugin_xmms.c \ system.c system.h \ isdn.c isdn.h \ wifi.c wifi.h \ diff --git a/Makefile.in b/Makefile.in index b2114d0..483130b 100644 --- a/Makefile.in +++ b/Makefile.in @@ -104,7 +104,7 @@ lcd4linux_LDADD = @DRIVERS@ @DRVLIBS@ #remove next line for liblcd4linux lcd4linux_DEPENDENCIES = @DRIVERS@ -lcd4linux_SOURCES = lcd4linux.c pid.c pid.h parser.c parser.h processor.c processor.h layout.c layout.h timer.c timer.h evaluator.c evaluator.h widget.c widget.h widget_text.c widget_text.h plugin.c plugin.h plugin_math.c plugin_string.c plugin_cfg.c plugin_xmms.c plugin_i2c_sensors.c system.c system.h isdn.c isdn.h wifi.c wifi.h mail.c mail.h seti.c seti.h battery.c battery.h dvb.c dvb.h filter.c filter.h exec.c exec.h expr.c expr.h mail2.c socket.c socket.h imon.c imon.h display.c display.h drv.c drv.h debug.c debug.h cfg.c cfg.h lock.c lock.h pixmap.c pixmap.h bar.c bar.h icon.c icon.h fontmap.c fontmap.h udelay.c udelay.h +lcd4linux_SOURCES = lcd4linux.c pid.c pid.h hash.c hash.h parser.c parser.h processor.c processor.h layout.c layout.h timer.c timer.h evaluator.c evaluator.h widget.c widget.h widget_text.c widget_text.h plugin.c plugin.h plugin_math.c plugin_string.c plugin_cfg.c plugin_cpuinfo.c plugin_i2c_sensors.c plugin_xmms.c system.c system.h isdn.c isdn.h wifi.c wifi.h mail.c mail.h seti.c seti.h battery.c battery.h dvb.c dvb.h filter.c filter.h exec.c exec.h expr.c expr.h mail2.c socket.c socket.h imon.c imon.h display.c display.h drv.c drv.h debug.c debug.h cfg.c cfg.h lock.c lock.h pixmap.c pixmap.h bar.c bar.h icon.c icon.h fontmap.c fontmap.h udelay.c udelay.h #liblcd4linux_la_DEPENDENCIES = @DRIVERS@ @@ -144,17 +144,18 @@ X_CFLAGS = @X_CFLAGS@ X_LIBS = @X_LIBS@ X_EXTRA_LIBS = @X_EXTRA_LIBS@ X_PRE_LIBS = @X_PRE_LIBS@ -lcd4linux_OBJECTS = lcd4linux.$(OBJEXT) pid.$(OBJEXT) parser.$(OBJEXT) \ -processor.$(OBJEXT) layout.$(OBJEXT) timer.$(OBJEXT) \ +lcd4linux_OBJECTS = lcd4linux.$(OBJEXT) pid.$(OBJEXT) hash.$(OBJEXT) \ +parser.$(OBJEXT) processor.$(OBJEXT) layout.$(OBJEXT) timer.$(OBJEXT) \ evaluator.$(OBJEXT) widget.$(OBJEXT) widget_text.$(OBJEXT) \ plugin.$(OBJEXT) plugin_math.$(OBJEXT) plugin_string.$(OBJEXT) \ -plugin_cfg.$(OBJEXT) plugin_xmms.$(OBJEXT) plugin_i2c_sensors.$(OBJEXT) \ -system.$(OBJEXT) isdn.$(OBJEXT) wifi.$(OBJEXT) mail.$(OBJEXT) \ -seti.$(OBJEXT) battery.$(OBJEXT) dvb.$(OBJEXT) filter.$(OBJEXT) \ -exec.$(OBJEXT) expr.$(OBJEXT) mail2.$(OBJEXT) socket.$(OBJEXT) \ -imon.$(OBJEXT) display.$(OBJEXT) drv.$(OBJEXT) debug.$(OBJEXT) \ -cfg.$(OBJEXT) lock.$(OBJEXT) pixmap.$(OBJEXT) bar.$(OBJEXT) \ -icon.$(OBJEXT) fontmap.$(OBJEXT) udelay.$(OBJEXT) +plugin_cfg.$(OBJEXT) plugin_cpuinfo.$(OBJEXT) \ +plugin_i2c_sensors.$(OBJEXT) plugin_xmms.$(OBJEXT) system.$(OBJEXT) \ +isdn.$(OBJEXT) wifi.$(OBJEXT) mail.$(OBJEXT) seti.$(OBJEXT) \ +battery.$(OBJEXT) dvb.$(OBJEXT) filter.$(OBJEXT) exec.$(OBJEXT) \ +expr.$(OBJEXT) mail2.$(OBJEXT) socket.$(OBJEXT) imon.$(OBJEXT) \ +display.$(OBJEXT) drv.$(OBJEXT) debug.$(OBJEXT) cfg.$(OBJEXT) \ +lock.$(OBJEXT) pixmap.$(OBJEXT) bar.$(OBJEXT) icon.$(OBJEXT) \ +fontmap.$(OBJEXT) udelay.$(OBJEXT) CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) @@ -176,14 +177,14 @@ DEP_FILES = .deps/BeckmannEgle.P .deps/Crystalfontz.P .deps/Cwlinux.P \ .deps/T6963.P .deps/Text.P .deps/USBLCD.P .deps/XWindow.P .deps/bar.P \ .deps/battery.P .deps/cfg.P .deps/debug.P .deps/display.P .deps/drv.P \ .deps/drv_MatrixOrbital.P .deps/dvb.P .deps/evaluator.P .deps/exec.P \ -.deps/expr.P .deps/filter.P .deps/fontmap.P .deps/icon.P .deps/imon.P \ -.deps/isdn.P .deps/layout.P .deps/lcd4linux.P .deps/lock.P .deps/mail.P \ -.deps/mail2.P .deps/parport.P .deps/parser.P .deps/pid.P .deps/pixmap.P \ -.deps/plugin.P .deps/plugin_cfg.P .deps/plugin_i2c_sensors.P \ -.deps/plugin_math.P .deps/plugin_string.P .deps/plugin_xmms.P \ -.deps/processor.P .deps/seti.P .deps/socket.P .deps/system.P \ -.deps/timer.P .deps/udelay.P .deps/widget.P .deps/widget_text.P \ -.deps/wifi.P +.deps/expr.P .deps/filter.P .deps/fontmap.P .deps/hash.P .deps/icon.P \ +.deps/imon.P .deps/isdn.P .deps/layout.P .deps/lcd4linux.P .deps/lock.P \ +.deps/mail.P .deps/mail2.P .deps/parport.P .deps/parser.P .deps/pid.P \ +.deps/pixmap.P .deps/plugin.P .deps/plugin_cfg.P .deps/plugin_cpuinfo.P \ +.deps/plugin_i2c_sensors.P .deps/plugin_math.P .deps/plugin_string.P \ +.deps/plugin_xmms.P .deps/processor.P .deps/seti.P .deps/socket.P \ +.deps/system.P .deps/timer.P .deps/udelay.P .deps/widget.P \ +.deps/widget_text.P .deps/wifi.P SOURCES = $(lcd4linux_SOURCES) $(EXTRA_lcd4linux_SOURCES) OBJECTS = $(lcd4linux_OBJECTS) diff --git a/hash.c b/hash.c new file mode 100644 index 0000000..02f0f65 --- /dev/null +++ b/hash.c @@ -0,0 +1,129 @@ +/* $Id: hash.c,v 1.1 2004/01/13 10:03:01 reinelt Exp $ + * + * hashes (associative arrays) + * + * Copyright 1999-2003 Michael Reinelt + * Copyright 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. + * + * + * $Log: hash.c,v $ + * Revision 1.1 2004/01/13 10:03:01 reinelt + * new util 'hash' for associative arrays + * new plugin 'cpuinfo' + * + */ + +/* + * exported functions: + * + * hash_anything + * Fixme: document me! + * + */ + + +#include +#include +#include + +#include "debug.h" +#include "hash.h" + + +// bsearch compare function for hash entries +static int hash_lookup_f (const void *a, const void *b) +{ + char *key=(char*)a; + HASH_ITEM *item=(HASH_ITEM*)b; + + return strcmp(key, item->key); +} + + +// qsort compare function for hash tables +static int hash_sort_f (const void *a, const void *b) +{ + HASH_ITEM *ha=(HASH_ITEM*)a; + HASH_ITEM *hb=(HASH_ITEM*)b; + + return strcasecmp(ha->key, hb->key); +} + + +// insert a key/val pair into the hash table +// the tbale will be searched linearly if the entry +// does already exist, for the table may not be sorted. +// the table will be flagged 'unsorted' afterwards +void hash_set (HASH *Hash, char *key, char *val) +{ + int i; + + // entry already exists? + for (i=0;inItems; i++) { + if (strcmp(key, Hash->Items[i].key)==0) { + if (Hash->Items[i].val) free (Hash->Items[i].val); + Hash->Items[i].val=strdup(val); + return; + } + } + + // add entry + Hash->sorted=0; + Hash->nItems++; + Hash->Items=realloc(Hash->Items,Hash->nItems*sizeof(HASH_ITEM)); + Hash->Items[i].key=strdup(key); + Hash->Items[i].val=strdup(val); +} + + +void *hash_get (HASH *Hash, char *key) +{ + HASH_ITEM *Item; + + if (!Hash->sorted) { + qsort(Hash->Items, Hash->nItems, sizeof(HASH_ITEM), hash_sort_f); + Hash->sorted=1; + } + + Item=bsearch(key, Hash->Items, Hash->nItems, sizeof(HASH_ITEM), hash_lookup_f); + + return Item?Item->val:NULL; +} + + +void hash_destroy (HASH *Hash) +{ + int i; + + if (Hash->Items) { + + // free all entries + for (i=0;inItems; i++) { + if (Hash->Items[i].key) free (Hash->Items[i].key); + if (Hash->Items[i].val) free (Hash->Items[i].val); + } + + // free hash table + free (Hash->Items); + } + + Hash->nItems=0; + Hash->sorted=0; + Hash->Items=NULL; +} diff --git a/hash.h b/hash.h new file mode 100644 index 0000000..ff85719 --- /dev/null +++ b/hash.h @@ -0,0 +1,52 @@ +/* $Id: hash.h,v 1.1 2004/01/13 10:03:01 reinelt Exp $ + * + * hashes (associative arrays) + * + * Copyright 1999-2003 Michael Reinelt + * Copyright 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. + * + * + * $Log: hash.h,v $ + * Revision 1.1 2004/01/13 10:03:01 reinelt + * new util 'hash' for associative arrays + * new plugin 'cpuinfo' + * + */ + +#ifndef _HASH_H_ +#define _HASH_H_ + + +typedef struct HASH_ITEM { + char *key; + char *val; +} HASH_ITEM; + +typedef struct HASH { + int nItems; + int sorted; + HASH_ITEM *Items; +} HASH; + + +void hash_set (HASH *Hash, char *key, char *val); +void *hash_get (HASH *Hash, char *key); +void hash_destroy (HASH *Hash); + +#endif diff --git a/plugin.c b/plugin.c index ec60dbe..3304a00 100644 --- a/plugin.c +++ b/plugin.c @@ -1,4 +1,4 @@ -/* $Id: plugin.c,v 1.9 2004/01/11 18:26:02 reinelt Exp $ +/* $Id: plugin.c,v 1.10 2004/01/13 10:03:01 reinelt Exp $ * * plugin handler for the Evaluator * @@ -22,6 +22,10 @@ * * * $Log: plugin.c,v $ + * Revision 1.10 2004/01/13 10:03:01 reinelt + * new util 'hash' for associative arrays + * new plugin 'cpuinfo' + * * Revision 1.9 2004/01/11 18:26:02 reinelt * further widget and layout processing * @@ -97,8 +101,9 @@ int plugin_init_math (void); int plugin_init_string (void); int plugin_init_cfg (void); -int plugin_init_xmms (void); +int plugin_init_cpuinfo (void); int plugin_init_i2c_sensors (void); +int plugin_init_xmms (void); int plugin_init (void) @@ -106,9 +111,10 @@ int plugin_init (void) plugin_init_math(); plugin_init_string(); plugin_init_cfg(); - plugin_init_xmms(); + plugin_init_cpuinfo(); // MR: segfaults here // plugin_init_i2c_sensors(); + plugin_init_xmms(); return 0; } diff --git a/plugin_cfg.c b/plugin_cfg.c new file mode 100644 index 0000000..0b1a229 --- /dev/null +++ b/plugin_cfg.c @@ -0,0 +1,125 @@ +/* $Id: plugin_cfg.c,v 1.1 2004/01/13 10:03:01 reinelt Exp $ + * + * plugin for config file access + * + * Copyright 2003, 2004 Michael Reinelt + * Copyright 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. + * + * + * $Log: plugin_cfg.c,v $ + * Revision 1.1 2004/01/13 10:03:01 reinelt + * new util 'hash' for associative arrays + * new plugin 'cpuinfo' + * + */ + +/* + * exported functions: + * + * int plugin_init_cfg (void) + * adds cfg() function for config access + * initializes variables from the config file + * + */ + + +#include +#include +#include + +#include "debug.h" +#include "evaluator.h" +#include "plugin.h" +#include "cfg.h" + + +static void load_variables (void) +{ + char *section="Variables"; + char *list, *key; + char *expression; + RESULT result = {0, 0.0, NULL}; + + list=cfg_list(section); + key=strtok(list, "|"); + while (key!=NULL) { + if (strchr(key, '.')!=NULL || strchr(key, ':') !=0) { + error ("ignoring variable '%s' from %s: structures not allowed", key, cfg_source()); + } else { + expression=cfg_get (section, key, ""); + if (expression!=NULL && *expression!='\0') { + if (Eval(expression, &result)==0) { + debug ("Variable %s = '%s' (%f)", key, R2S(&result), R2N(&result)); + SetVariable (key, &result); + DelResult (&result); + } else { + error ("error evaluating variable '%s' from %s", key, cfg_source()); + } + } + } + key=strtok(NULL, "|"); + } + free (list); + +} + + +static void my_cfg (RESULT *result, int argc, RESULT *argv[]) +{ + int i, len; + char *value; + char *buffer; + + // calculate key length + len=0; + for (i=0; i + * Copyright 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. + * + * + * $Log: plugin_cpuinfo.c,v $ + * Revision 1.1 2004/01/13 10:03:01 reinelt + * new util 'hash' for associative arrays + * new plugin 'cpuinfo' + * + */ + +/* + * exported functions: + * + * int plugin_init_cpuinfo (void) + * adds functions to access /proc/cpuinfo + * + */ + + +#include +#include +#include +#include +#include +#include + +#include "debug.h" +#include "plugin.h" +#include "hash.h" + + +static void my_cpuinfo (RESULT *result, RESULT *arg1) +{ + static HASH CPUinfo = { 0, 0, NULL }; + + static time_t now=0; + char *key, *val; + + // reread every second only + if (time(NULL)!=now) { + FILE *stream; + + // destroy previous hash table + hash_destroy (&CPUinfo); + + stream=fopen("/proc/cpuinfo", "r"); + if (stream==NULL) { + error ("fopen(/proc/cpuinfo) failed: %s", strerror(errno)); + goto error; + } + + while (!feof(stream)) { + char buffer[256]; + char *c; + fgets (buffer, sizeof(buffer), stream); + c=strchr(buffer, ':'); + if (c==NULL) continue; + key=buffer; val=c+1; + // strip leading blanks from key + while (isspace(*key)) *key++='\0'; + // strip trailing blanks from key + while (isspace(*--c)) *c='\0'; + // strip leading blanks from value + while (isspace(*val)) *val++='\0'; + // strip trailing blanks from value + for (c=val; *c!='\0';c++); + while (isspace(*--c)) *c='\0'; + + // add entry to hash table + hash_set (&CPUinfo, key, val); + + } + + fclose (stream); + + time(&now); + } + + key=R2S(arg1); + val=hash_get(&CPUinfo, key); + if (val==NULL) val=""; + + error: + // val=""; + SetResult(&result, R_STRING, val); +} + + +int plugin_init_cpuinfo (void) +{ + AddFunction ("cpuinfo", 1, my_cpuinfo); + return 0; +} + diff --git a/plugin_sample.c b/plugin_sample.c index d9b8eae..92c2a29 100644 --- a/plugin_sample.c +++ b/plugin_sample.c @@ -1,8 +1,9 @@ -/* $Id: plugin_sample.c,v 1.1 2004/01/06 17:33:45 reinelt Exp $ +/* $Id: plugin_sample.c,v 1.2 2004/01/13 10:03:01 reinelt Exp $ * * plugin template * * Copyright 2003 Michael Reinelt + * Copyright 2004 The LCD4Linux Team * * This file is part of LCD4Linux. * @@ -22,6 +23,10 @@ * * * $Log: plugin_sample.c,v $ + * Revision 1.2 2004/01/13 10:03:01 reinelt + * new util 'hash' for associative arrays + * new plugin 'cpuinfo' + * * Revision 1.1 2004/01/06 17:33:45 reinelt * * Evaluator: functions with variable argument lists -- cgit v1.2.3