]> git.webhop.me Git - lcd4linux.git/commitdiff
[lcd4linux @ 2004-01-16 07:26:25 by reinelt]
authorreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Fri, 16 Jan 2004 07:26:25 +0000 (07:26 +0000)
committerreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Fri, 16 Jan 2004 07:26:25 +0000 (07:26 +0000)
moved various /proc parsing to own functions
made some progress with /proc/stat parsing

git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@315 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

hash.c
hash.h
plugin_cpuinfo.c
plugin_meminfo.c
plugin_proc_stat.c

diff --git a/hash.c b/hash.c
index fb9e8c1b57d23ae690c840ced1e8e78b89c878c9..32be0a9065c5263f2852636a5af55ab42bb9c1a0 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -1,4 +1,4 @@
-/* $Id: hash.c,v 1.2 2004/01/16 05:04:53 reinelt Exp $
+/* $Id: hash.c,v 1.3 2004/01/16 07:26:25 reinelt Exp $
  *
  * hashes (associative arrays)
  *
  *
  *
  * $Log: hash.c,v $
+ * Revision 1.3  2004/01/16 07:26:25  reinelt
+ * moved various /proc parsing to own functions
+ * made some progress with /proc/stat parsing
+ *
  * Revision 1.2  2004/01/16 05:04:53  reinelt
  * started plugin proc_stat which should parse /proc/stat
  * which again is a paint in the a**
@@ -62,16 +66,6 @@ static int hash_lookup_f (const void *a, const void *b)
 }
 
 
-// bsearch compare function for filter entries
-static int filter_lookup_f (const void *a, const void *b)
-{
-  char *key=(char*)a;
-  FILTER_ITEM *item=(FILTER_ITEM*)b;
-
-  return strcmp(key, item->key);
-}
-
-
 // qsort compare function for hash tables
 static int hash_sort_f (const void *a, const void *b)
 {
@@ -82,17 +76,6 @@ static int hash_sort_f (const void *a, const void *b)
 }
 
 
-// qsort compare function for filter tables
-static int filter_sort_f (const void *a, const void *b)
-{
-  FILTER_ITEM *ha=(FILTER_ITEM*)a;
-  FILTER_ITEM *hb=(FILTER_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.
@@ -155,69 +138,3 @@ void hash_destroy (HASH *Hash)
   Hash->Items=NULL;
 }
 
-
-// insert a key/val pair into the filter 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 filter_set (FILTER *Filter, char *key, char *val)
-{
-  int i;
-
-  // entry already exists?
-  for (i=0;i<Filter->nItems; i++) {
-    if (strcmp(key, Filter->Items[i].key)==0) {
-      // if (Filter->Items[i].val) free (Filter->Items[i].val);
-      // Filter->Items[i].val=strdup(val);
-      return;
-    }
-  }
-  
-  // add entry
-  Filter->sorted=0;
-  Filter->nItems++;
-  Filter->Items=realloc(Filter->Items,Filter->nItems*sizeof(FILTER_ITEM));
-  Filter->Items[i].key=strdup(key);
-
-  
-  Filter->Items[i].Slots=strdup(val);
-}
-
-
-double filter_get (FILTER *Filter, char *key)
-{
-  FILTER_ITEM *Item;
-  
-  if (!Filter->sorted) {
-    qsort(Filter->Items, Filter->nItems, sizeof(FILTER_ITEM), filter_sort_f);
-    Filter->sorted=1;
-  }
-  
-  Item=bsearch(key, Filter->Items, Filter->nItems, sizeof(FILTER_ITEM), filter_lookup_f);
-  if (Item==NULL) return 0.0;
-  if (Item->Slots==NULL) return 0.0;
-  return Item->Slots[0].val;
-  
-}
-
-
-void filter_destroy (FILTER *Filter)
-{
-  int i;
-
-  if (Filter->Items) {
-    
-    // free all entries
-    for (i=0;i<Filter->nItems; i++) {
-      if (Filter->Items[i].key)   free (Filter->Items[i].key);
-      if (Filter->Items[i].Slots) free (Filter->Items[i].Slots);
-    }
-    
-    // free filter table
-    free (Filter->Items);
-  }
-  
-  Filter->nItems=0;
-  Filter->sorted=0;
-  Filter->Items=NULL;
-}
diff --git a/hash.h b/hash.h
index 90cf7606e2d79895c53b4f637441087bc8b19329..d410d3dc306b2a899006a6a4409d18600c1e1a6f 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -1,4 +1,4 @@
-/* $Id: hash.h,v 1.2 2004/01/16 05:04:53 reinelt Exp $
+/* $Id: hash.h,v 1.3 2004/01/16 07:26:25 reinelt Exp $
  *
  * hashes (associative arrays)
  *
  *
  *
  * $Log: hash.h,v $
+ * Revision 1.3  2004/01/16 07:26:25  reinelt
+ * moved various /proc parsing to own functions
+ * made some progress with /proc/stat parsing
+ *
  * Revision 1.2  2004/01/16 05:04:53  reinelt
  * started plugin proc_stat which should parse /proc/stat
  * which again is a paint in the a**
@@ -62,18 +66,8 @@ typedef struct {
   FILTER_SLOT *Slots;
 } FILTER_ITEM;
 
-typedef struct {
-  int         sorted;
-  int         nItems;
-  FILTER_ITEM *Items;
-} FILTER;
-
 void  hash_set     (HASH *Hash, char *key, char *val);
 char *hash_get     (HASH *Hash, char *key);
 void  hash_destroy (HASH *Hash);
 
-void   filter_set     (FILTER *Filter, char *key, double val);
-double filter_get     (FILTER *Filter, char *key);
-void   filter_destroy (FILTER *Filter);
-
 #endif
index e9d40369afc15c8cdf493b692f9691b59f4f3af4..efa5d4a5f3a3a886e41e0b2adfcfac36752a407b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: plugin_cpuinfo.c,v 1.3 2004/01/15 04:29:45 reinelt Exp $
+/* $Id: plugin_cpuinfo.c,v 1.4 2004/01/16 07:26:25 reinelt Exp $
  *
  * plugin for /proc/cpuinfo parsing
  *
  *
  *
  * $Log: plugin_cpuinfo.c,v $
+ * Revision 1.4  2004/01/16 07:26:25  reinelt
+ * moved various /proc parsing to own functions
+ * made some progress with /proc/stat parsing
+ *
  * Revision 1.3  2004/01/15 04:29:45  reinelt
  * moved lcd4linux.conf.sample to *.old
  * lcd4linux.conf.sample with new layout
 #include "hash.h"
 
 
-static void my_cpuinfo (RESULT *result, RESULT *arg1)
+static HASH CPUinfo = { 0, 0, NULL };
+
+
+static int parse_cpuinfo (void)
 {
-  static HASH CPUinfo = { 0, 0, NULL };
-  
   static time_t now=0;
-  char *key, *val;
-  
+  FILE *stream;
+
   // reread every second only
-  if (time(NULL)!=now) {
-    FILE *stream;
+  if (time(NULL)==now) return 0;
+  time(&now);
+  
     
-    // destroy previous hash table
-    hash_destroy (&CPUinfo);
-
-    stream=fopen("/proc/cpuinfo", "r");
-    if (stream==NULL) {
-      error ("fopen(/proc/cpuinfo) failed: %s", strerror(errno));
-      SetResult(&result, R_STRING, ""); 
-      return;
-    }
+  stream=fopen("/proc/cpuinfo", "r");
+  if (stream==NULL) {
+    error ("fopen(/proc/cpuinfo) failed: %s", strerror(errno));
+    return -1;
+  }
     
-    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
-      do *c='\0'; while (isspace(*--c));
-      // 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';
+  while (!feof(stream)) {
+    char buffer[256];
+    char *c, *key, *val;
+    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
+    do *c='\0'; while (isspace(*--c));
+    // 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);
+    // add entry to hash table
+    hash_set (&CPUinfo, key, val);
       
-    }
-    fclose (stream);
-    time(&now);
+  }
+  fclose (stream);
+  return 0;
+}
+  
+
+static void my_cpuinfo (RESULT *result, RESULT *arg1)
+{
+  char *key, *val;
+  
+  if (parse_cpuinfo()<0) {
+    SetResult(&result, R_STRING, ""); 
+    return;
   }
   
   key=R2S(arg1);
   val=hash_get(&CPUinfo, key);
   if (val==NULL) val="";
-
+  
   SetResult(&result, R_STRING, val); 
 }
 
index 6115515d9e8188d845f498e7711e2895ef7e927a..ec8e6119928bc82fc135f5623e47ed3c8c0c40fa 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: plugin_meminfo.c,v 1.1 2004/01/15 04:29:45 reinelt Exp $
+/* $Id: plugin_meminfo.c,v 1.2 2004/01/16 07:26:25 reinelt Exp $
  *
  * plugin for /proc/meminfo parsing
  *
  *
  *
  * $Log: plugin_meminfo.c,v $
+ * Revision 1.2  2004/01/16 07:26:25  reinelt
+ * moved various /proc parsing to own functions
+ * made some progress with /proc/stat parsing
+ *
  * Revision 1.1  2004/01/15 04:29:45  reinelt
  * moved lcd4linux.conf.sample to *.old
  * lcd4linux.conf.sample with new layout
 
 #include "debug.h"
 #include "plugin.h"
+
 #include "hash.h"
 
 
-static void my_meminfo (RESULT *result, RESULT *arg1)
+static HASH MemInfo = { 0, 0, NULL };
+
+
+static int parse_meminfo (void)
 {
-  static HASH MemInfo = { 0, 0, NULL };
-  
   static time_t now=0;
-  char *key, *val;
+  FILE *stream;
   int line;
   
   // reread every second only
-  if (time(NULL)!=now) {
-    FILE *stream;
-    
-    // destroy previous hash table
-    hash_destroy (&MemInfo);
-
-    stream=fopen("/proc/meminfo", "r");
-    if (stream==NULL) {
-      error ("fopen(/proc/meminfo) failed: %s", strerror(errno));
-      SetResult(&result, R_STRING, ""); 
-      return;
-    }
+  if (time(NULL)==now) return 0;
+  time(&now);
+  
+  stream=fopen("/proc/meminfo", "r");
+  if (stream==NULL) {
+    error ("fopen(/proc/meminfo) failed: %s", strerror(errno));
+    return -1;
+  }
     
-    line=0;
-    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
-      do *c='\0'; while (isspace(*--c));
-      // 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';
-      // skip lines that do not end with " kB"
-      if (*c=='B' && *(c-1)=='k' && *(c-2)==' ') {
-       // strip trailing " kB" from value
-       *(c-2)='\0';
-       // add entry to hash table
-       hash_set (&MemInfo, key, val);
-      }
+  line=0;
+  while (!feof(stream)) {
+    char buffer[256];
+    char *c, *key, *val;
+    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
+    do *c='\0'; while (isspace(*--c));
+    // 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';
+    // skip lines that do not end with " kB"
+    if (*c=='B' && *(c-1)=='k' && *(c-2)==' ') {
+      // strip trailing " kB" from value
+      *(c-2)='\0';
+      // add entry to hash table
+      hash_set (&MemInfo, key, val);
     }
-    fclose (stream);
-    time(&now);
+  }
+  fclose (stream);
+  return 0;
+}  
+
+static void my_meminfo (RESULT *result, RESULT *arg1)
+{
+  char *key, *val;
+  
+  if (parse_meminfo()<0) {
+    SetResult(&result, R_STRING, ""); 
+    return;
   }
   
   key=R2S(arg1);
   val=hash_get(&MemInfo, key);
   if (val==NULL) val="";
-
+  
   SetResult(&result, R_STRING, val); 
 }
 
index 1e7ec0b34a4b390d5492964b834c6a513a8dcd31..e7b9bf826e25705e81df87d22ff50387da0e41e8 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: plugin_proc_stat.c,v 1.1 2004/01/16 05:04:53 reinelt Exp $
+/* $Id: plugin_proc_stat.c,v 1.2 2004/01/16 07:26:25 reinelt Exp $
  *
  * plugin for /proc/stat parsing
  *
  *
  *
  * $Log: plugin_proc_stat.c,v $
+ * Revision 1.2  2004/01/16 07:26:25  reinelt
+ * moved various /proc parsing to own functions
+ * made some progress with /proc/stat parsing
+ *
  * Revision 1.1  2004/01/16 05:04:53  reinelt
  * started plugin proc_stat which should parse /proc/stat
  * which again is a paint in the a**
@@ -75,6 +79,16 @@ static int renew(int msec)
 }
 
 
+static void my_hash_set (char *key1, char *key2, char *val) 
+{
+  char key[16];
+  
+  snprintf (key, sizeof(key), "%s.%s", key1, key2);
+  debug ("Michi: hash_set(%s, %s)", key, val);
+  hash_set (&Stat, key, val);
+}
+
+
 static int parse_proc_stat (void)
 {
   FILE *stream;
@@ -82,9 +96,6 @@ static int parse_proc_stat (void)
   // update every 10 msec
   if (!renew(10)) return 0;
   
-  // destroy previous hash table
-  hash_destroy (&Stat);
-
   stream=fopen("/proc/stat", "r");
   if (stream==NULL) {
     error ("fopen(/proc/stat) failed: %s", strerror(errno));
@@ -94,13 +105,14 @@ static int parse_proc_stat (void)
   while (!feof(stream)) {
     char buffer[256];
     fgets (buffer, sizeof(buffer), stream);
-
+    
     if (strncmp(buffer, "cpu", 3)==0) {
-      unsigned long v1, v2, v3, v4;
-      debug ("Michi: buffer=<%s>", buffer);
-      if (sscanf(buffer+4, " %lu %lu %lu %lu", &v1, &v2, &v3, &v4)==4) {
-       debug ("Michi: v1=<%ld> v2=<%ld> v3=<%ld> v4=<%ld>", v1, v2, v3, v4);
-      }
+      char *cpu;
+      cpu=strtok(buffer, " \t");
+      my_hash_set (cpu, "user",   strtok(NULL, " \t"));
+      my_hash_set (cpu, "nice",   strtok(NULL, " \t"));
+      my_hash_set (cpu, "system", strtok(NULL, " \t"));
+      my_hash_set (cpu, "idle",   strtok(NULL, " \t"));
     } 
     else if (strncmp(buffer, "page ", 5)==0) {
       
@@ -129,12 +141,16 @@ static void my_proc_stat (RESULT *result, RESULT *arg1)
 {
   char *key, *val;
   
-  parse_proc_stat();
+  if (parse_proc_stat()<0) {
+    SetResult(&result, R_STRING, ""); 
+    return;
+  }
 
   key=R2S(arg1);
-  val="";
-  
-  SetResult(&result, R_STRING, ""); 
+  val=hash_get(&Stat, key);
+  if (val==NULL) val="";
+
+  SetResult(&result, R_STRING, val); 
 }