All of lore.kernel.org
 help / color / mirror / Atom feed
* [lm-sensors] [PATCH 3/3] sensors3: Optimize command line handling
@ 2007-08-25 13:14 Jean Delvare
  2007-08-25 17:23 ` [lm-sensors] [PATCH 3/3] sensors3: Optimize command line Hans de Goede
  2007-08-27  8:28 ` Jean Delvare
  0 siblings, 2 replies; 3+ messages in thread
From: Jean Delvare @ 2007-08-25 13:14 UTC (permalink / raw)
  To: lm-sensors

Thanks to the new prototype of sensors_get_detected_chips, we no
longer need to build an array from the chip names passed on the
command line. Instead, we can process them one after the other
directly. This saves some memory and allows for minor code cleanups.

---
 prog/sensors/main.c |   86 +++++++++++++++++++++++----------------------------
 1 file changed, 40 insertions(+), 46 deletions(-)

--- lm-sensors-3.orig/prog/sensors/main.c	2007-08-25 13:53:15.000000000 +0200
+++ lm-sensors-3/prog/sensors/main.c	2007-08-25 14:19:45.000000000 +0200
@@ -48,12 +48,9 @@ static void print_long_help(void);
 static void print_version(void);
 static void do_a_print(const sensors_chip_name *name);
 static int do_a_set(const sensors_chip_name *name);
-static int do_the_real_work(int *error);
+static int do_the_real_work(const sensors_chip_name *chip, int *error);
 static const char *sprintf_chip_name(const sensors_chip_name *name);
 
-#define CHIPS_MAX 20
-sensors_chip_name chips[CHIPS_MAX];
-int chips_count=0;
 int do_sets, do_raw, fahrenheit, hide_adapter;
 
 char degstr[5]; /* store the correct string to print degrees */
@@ -200,23 +197,6 @@ int main (int argc, char *argv[])
     }
   }
 
-  if (optind = argc) {
-    chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
-    chips[0].bus.type = SENSORS_BUS_TYPE_ANY;
-    chips[0].bus.nr = SENSORS_BUS_NR_ANY;
-    chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
-    chips_count = 1;
-  } else 
-    for(i = optind; i < argc; i++) 
-      if ((res = sensors_parse_chip_name(argv[i],chips+chips_count))) {
-        fprintf(stderr,"Parse error in chip name `%s'\n",argv[i]);
-        print_short_help();
-        exit(1);
-      } else if (++chips_count = CHIPS_MAX) {
-        fprintf(stderr,"Too many chips on command line!\n");
-        exit(1);
-      }
-
   open_config_file(config_file_name);
   if ((res = sensors_init(config_file))) {
     fprintf(stderr, "sensors_init: %s\n", sensors_strerror(res));
@@ -227,40 +207,54 @@ int main (int argc, char *argv[])
   /* build the degrees string */
   set_degstr();
 
-  if(do_the_real_work(&error)) {
-    sensors_cleanup();
-    exit(error);
+  if (optind = argc) { /* No chip name on command line */
+    if (!do_the_real_work(NULL, &error)) {
+      fprintf(stderr,
+              "No sensors found!\n"
+              "Make sure you loaded all the kernel drivers you need.\n"
+              "Try sensors-detect to find out which these are.\n");
+      error = 1;
+    }
   } else {
-    if (optind = argc) /* No chip name on command line */
-	    fprintf(stderr,
-	            "No sensors found!\n"
-	            "Make sure you loaded all the kernel drivers you need.\n"
-	            "Try sensors-detect to find out which these are.\n");
-    else
-	    fprintf(stderr,"Specified sensor(s) not found!\n");
-    sensors_cleanup();
-    exit(1);
+    int cnt = 0;
+    sensors_chip_name chip;
+
+    for (i = optind; i < argc; i++) {
+      if (sensors_parse_chip_name(argv[i], &chip)) {
+        fprintf(stderr, "Parse error in chip name `%s'\n", argv[i]);
+        print_short_help();
+        error = 1;
+        goto exit;
+      }
+      cnt += do_the_real_work(&chip, &error);
+    }
+
+    if (!cnt) {
+      fprintf(stderr, "Specified sensor(s) not found!\n");
+      error = 1;
+    }
   }
+
+exit:
+  sensors_cleanup();
+  exit(res);
 }
 
 /* returns number of chips found */
-int do_the_real_work(int *error)
+int do_the_real_work(const sensors_chip_name *match, int *error)
 {
   const sensors_chip_name *chip;
-  int chip_nr,i;
+  int chip_nr;
   int cnt = 0;
 
-  *error = 0;
-  for (i = 0; i < chips_count; i++) {
-    chip_nr = 0;
-    while ((chip = sensors_get_detected_chips(&chips[i], &chip_nr))) {
-      if (do_sets) {
-        if (do_a_set(chip))
-          *error = 1;
-      } else
-        do_a_print(chip);
-      cnt++;
-    }
+  chip_nr = 0;
+  while ((chip = sensors_get_detected_chips(match, &chip_nr))) {
+    if (do_sets) {
+      if (do_a_set(chip))
+        *error = 1;
+    } else
+      do_a_print(chip);
+    cnt++;
   }
   return cnt;
 }


-- 
Jean Delvare

_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [lm-sensors] [PATCH 3/3] sensors3: Optimize command line
  2007-08-25 13:14 [lm-sensors] [PATCH 3/3] sensors3: Optimize command line handling Jean Delvare
@ 2007-08-25 17:23 ` Hans de Goede
  2007-08-27  8:28 ` Jean Delvare
  1 sibling, 0 replies; 3+ messages in thread
From: Hans de Goede @ 2007-08-25 17:23 UTC (permalink / raw)
  To: lm-sensors

Jean Delvare wrote:
> Thanks to the new prototype of sensors_get_detected_chips, we no
> longer need to build an array from the chip names passed on the
> command line. Instead, we can process them one after the other
> directly. This saves some memory and allows for minor code cleanups.
> 
Looks good to me,

Regards,

Hans



_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [lm-sensors] [PATCH 3/3] sensors3: Optimize command line
  2007-08-25 13:14 [lm-sensors] [PATCH 3/3] sensors3: Optimize command line handling Jean Delvare
  2007-08-25 17:23 ` [lm-sensors] [PATCH 3/3] sensors3: Optimize command line Hans de Goede
@ 2007-08-27  8:28 ` Jean Delvare
  1 sibling, 0 replies; 3+ messages in thread
From: Jean Delvare @ 2007-08-27  8:28 UTC (permalink / raw)
  To: lm-sensors

On Sat, 25 Aug 2007 19:23:42 +0200, Hans de Goede wrote:
> Jean Delvare wrote:
> > Thanks to the new prototype of sensors_get_detected_chips, we no
> > longer need to build an array from the chip names passed on the
> > command line. Instead, we can process them one after the other
> > directly. This saves some memory and allows for minor code cleanups.
> > 
> Looks good to me,

Thanks. All 3 patches applied now. I also made a big cleanup to
"sensors", mainly to better understand what exactly it needs from
libsensors, in the hope it'll help me in the last part of the API
review: sensors_get_all_features() and all the underlying features
gathering, designation and storage code. I'll work on this as time
permits during the week, and hopefully I can send a proposal for
discussion at the end of the week.

-- 
Jean Delvare

_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-08-27  8:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-08-25 13:14 [lm-sensors] [PATCH 3/3] sensors3: Optimize command line handling Jean Delvare
2007-08-25 17:23 ` [lm-sensors] [PATCH 3/3] sensors3: Optimize command line Hans de Goede
2007-08-27  8:28 ` Jean Delvare

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.