to github
[svn42.git] / powersensordaemon / options.c
index 92635ed..51aabd9 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
+#include <errno.h>
 
 #include "log.h"
 
       i++;                                               \
     }
 
+#define PARSE_KEY_VALUE(SHORT, LONG, SEP, KV)            \
+    else if(!strcmp(str,SHORT) || !strcmp(str,LONG))     \
+    {                                                    \
+      if(argc < 1 || argv[i+1][0] == '-')                \
+        return i;                                        \
+      char* value = strchr(argv[i+1], SEP);              \
+      if(!value || value[1] == 0)                        \
+        return i+1;                                      \
+      value[0] = 0;                                      \
+      value++;                                           \
+      if(key_value_storage_add(&KV, argv[i+1], value))   \
+        return -2;                                       \
+      argc--;                                            \
+      i++;                                               \
+    }
+
 int options_parse_hex_string(const char* hex, buffer_t* buffer)
 {
   if(!hex || !buffer)
@@ -175,6 +192,7 @@ int options_parse(options_t* opt, int argc, char* argv[])
     PARSE_STRING_PARAM("-s","--socket", opt->command_sock_)
     PARSE_STRING_PARAM("-p","--powerid-file", opt->powerid_file_)
     PARSE_STRING_PARAM("-a","--sampledev-file", opt->sampledev_file_)
+    PARSE_KEY_VALUE("-A","--autosampledev", ',', opt->autosampledevs_)
     else 
       return i;
   }
@@ -182,18 +200,69 @@ int options_parse(options_t* opt, int argc, char* argv[])
   return 0;
 }
 
-void options_parse_post(options_t* opt)
+
+int options_parse_key_value_file(const char* filename, key_value_storage_t* storage)
+{
+  FILE* file = fopen(filename, "r");
+  if(file) {
+    char buf[100];
+    while(fgets(buf, 100, file) != NULL) {
+      char* tmp, *key, *value;
+      for(tmp = buf;*tmp == ' '; ++tmp);
+      if(*(key = tmp) == 0) continue;
+      for(;*tmp != ' ' && *tmp != 0;++tmp);
+      if(*tmp == 0) continue;
+      *tmp=0;
+      ++tmp;
+      for(;*tmp == ' ';++tmp);
+      if(*(value = tmp) == 0) continue;
+      for(;*tmp != ' ' && *tmp != 0 && *tmp != '\n';++tmp);
+      *tmp = 0;
+      
+      if(key_value_storage_add(storage, key, value))
+        return -2;
+    }
+    fclose(file);
+  }
+  else {
+    log_printf(ERROR,"unable to open conf file (%s): %s", filename, strerror(errno));
+    return -1;
+  }
+}
+
+int options_parse_post(options_t* opt)
 {
   if(!opt)
-    return;
+    return -1;
+
+  if(opt->powerid_file_) {
+    int ret = options_parse_key_value_file(opt->powerid_file_, &opt->powerids_);
+    if(ret)
+      return ret;
+  }
 
-/*   if(opt->powerid_file_) { */
-/*         // read powerids */
-/*   }   */
+  if(opt->sampledev_file_) {
+    int ret = options_parse_key_value_file(opt->sampledev_file_, &opt->sampledevs_);
+    if(ret)
+      return ret;
+  }
 
-/*   if(opt->sampledev_file_) { */
-/*         // read powerids */
-/*   }   */
+  string_list_element_t* k = opt->autosampledevs_.keys_.first_;
+  string_list_element_t* v = opt->autosampledevs_.values_.first_;
+  while(k && v) {
+    if(!key_value_storage_find(&opt->sampledevs_, k->string_)) {
+      log_printf(ERROR, "sample dev '%s' not in file '%s'", k->string_, opt->sampledev_file_);
+      return -1;
+    }
+    if(atoi(v->string_) <= 0) {
+      log_printf(ERROR, "invalid rate '%s' for sample dev '%s'", v->string_, k->string_);
+      return -1;
+    }
+    k = k->next_;
+    v = v->next_;
+  }
+  
+  return 0;
 }
 
 void options_default(options_t* opt)
@@ -212,7 +281,10 @@ void options_default(options_t* opt)
   opt->tty_dev_ = strdup("/dev/ttyUSB0");
   opt->command_sock_ = strdup("/var/run/powersensordaemon/cmd.sock");
   opt->powerid_file_ = NULL;
+  key_value_storage_init(&opt->powerids_);
   opt->sampledev_file_ = NULL;
+  key_value_storage_init(&opt->sampledevs_);
+  key_value_storage_init(&opt->autosampledevs_);
 }
 
 void options_clear(options_t* opt)
@@ -238,6 +310,11 @@ void options_clear(options_t* opt)
     free(opt->command_sock_);
   if(opt->powerid_file_)
     free(opt->powerid_file_);
+  key_value_storage_clear(&opt->powerids_);
+  if(opt->sampledev_file_)
+    free(opt->sampledev_file_);
+  key_value_storage_clear(&opt->sampledevs_);
+  key_value_storage_clear(&opt->autosampledevs_);
 }
 
 void options_print_usage()
@@ -256,6 +333,8 @@ void options_print_usage()
   printf("            [-s|--command-sock] <unix sock>     the command socket e.g. /var/run/powersensordaemon/cmd.sock\n");
   printf("            [-p|--powerid-file] <path>          file that contains the power ids\n");
   printf("            [-a|--sampledev-file] <path>        file that contains all sample devices\n");
+  printf("            [-A|--autosampledev] <device>,<delay between samples in seconds>\n");
+  printf("                                                automatic sample this devices, can be invoked several times\n");
 }
 
 void options_print(options_t* opt)
@@ -275,5 +354,11 @@ void options_print(options_t* opt)
   printf("tty_dev: '%s'\n", opt->tty_dev_);
   printf("command_sock: '%s'\n", opt->command_sock_);
   printf("powerid_file: '%s'\n", opt->powerid_file_);
+  printf("powerids: \n");
+  key_value_storage_print(&opt->powerids_, "  '", "' -> '", "'\n");
   printf("sampledev_file: '%s'\n", opt->sampledev_file_);
+  printf("sampledevs: \n");
+  key_value_storage_print(&opt->sampledevs_, "  '", "' -> '", "'\n");
+  printf("autosampledevs: \n");
+  key_value_storage_print(&opt->autosampledevs_, "  '", "' -> '", "'\n");
 }