Changeset 4701

Show
Ignore:
Timestamp:
08/26/07 10:26:20 (7 years ago)
Author:
khali
Message:

Add a parameter to sensors_get_detected_chips(), to optionally let the
caller select which subset of chips it wants. This is slightly better
size-wise than letting all applications do the filtering by themselves.

This will change the way the command line parameters of "sensors" are
interpreted. Beforehand, the chips were always returned in the order
in which they were listed by the library. Also, each chip could be listed
only once. From now on, the chips will be listed in the order in which
they are passed on the command line, which I think makes more sense. A
side effect is that chips can be listed more than once, if that's what
the user asks for. Not very useful though.

This change makes it possible to make sensors_match_chip() internal
to the library. Filtering the list of chips returned by
sensors_get_detected_chips() was the last known external use for this
function.

This patch looks much bigger than it really is, but the largest part is
really only code reindentation.

Location:
lm-sensors/branches/lm-sensors-3.0.0
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/branches/lm-sensors-3.0.0/lib/access.c

    r4695 r4701  
    304304} 
    305305 
    306 const sensors_chip_name *sensors_get_detected_chips(int *nr) 
     306const sensors_chip_name *sensors_get_detected_chips(const sensors_chip_name 
     307                                                    *match, int *nr) 
    307308{ 
    308309        const sensors_chip_name *res; 
    309         res = (*nr >= sensors_proc_chips_count ? 
    310                         NULL : &sensors_proc_chips[*nr].chip); 
    311         (*nr)++; 
    312         return res; 
     310 
     311        while (*nr < sensors_proc_chips_count) { 
     312                res = &sensors_proc_chips[(*nr)++].chip; 
     313                if (!match || sensors_match_chip(res, match)) 
     314                        return res; 
     315        } 
     316        return NULL; 
    313317} 
    314318 
     
    481485        int res = 0; 
    482486 
    483         for (nr = 0; (found_name = sensors_get_detected_chips(&nr));) 
    484                 if (sensors_match_chip(name, found_name)) { 
    485                         this_res = sensors_do_this_chip_sets(found_name); 
    486                         if (!res) 
    487                                 res = this_res; 
    488                 } 
     487        for (nr = 0; (found_name = sensors_get_detected_chips(name, &nr));) { 
     488                this_res = sensors_do_this_chip_sets(found_name); 
     489                if (this_res) 
     490                        res = this_res; 
     491        } 
    489492        return res; 
    490493} 
  • lm-sensors/branches/lm-sensors-3.0.0/lib/libsensors.3

    r4699 r4701  
    4545                      \fBdouble value);\fP 
    4646.B int sensors_do_chip_sets(const sensors_chip_name *name); 
    47 .B const sensors_chip_name *sensors_get_detected_chips(int *nr); 
     47.B const sensors_chip_name *sensors_get_detected_chips(const sensors_chip_name 
     48                                                    \fB*match, int *nr);\fP 
    4849.B const sensors_feature_data *sensors_get_all_features  
    4950             \fB(const sensors_chip_name *name, int *nr);\fP 
     
    99100Execute all set statements for this particular chip. The chip may contain wildcards!  This function will return 0 on success, and <0 on failure. 
    100101 
    101 \fBconst sensors_chip_name *sensors_get_detected_chips 
    102                         (int *nr);\fP 
     102\fBconst sensors_chip_name *sensors_get_detected_chips(const sensors_chip_name 
     103                                                    *match, int *nr);\fP 
    103104.br 
    104 This function returns all detected chips, one by one. To start at the beginning of the list, use 0 for nr; NULL is returned if we are at the end of the list. Do not try to change these chip names, as they point to internal structures! Do not use nr for anything else. 
     105This function returns all detected chips that match a given chip name, 
     106one by one. If no chip name is provided, all detected chips are returned. 
     107To start at the beginning of the list, use 0 for nr; NULL is returned if 
     108we are at the end of the list. Do not try to change these chip names, as 
     109they point to internal structures! 
    105110 
    106111This structure is used when you want to get all features of a specific chip. 
  • lm-sensors/branches/lm-sensors-3.0.0/lib/sensors.h

    r4696 r4701  
    110110int sensors_do_chip_sets(const sensors_chip_name *name); 
    111111 
    112 /* This function returns all detected chips, one by one. To start at the 
    113    beginning of the list, use 0 for nr; NULL is returned if we are 
    114    at the end of the list. Do not try to change these chip names, as 
    115    they point to internal structures! Do not use nr for anything else. */ 
    116 const sensors_chip_name *sensors_get_detected_chips(int *nr); 
     112/* This function returns all detected chips that match a given chip name, 
     113   one by one. If no chip name is provided, all detected chips are returned. 
     114   To start at the beginning of the list, use 0 for nr; NULL is returned if 
     115   we are at the end of the list. Do not try to change these chip names, as 
     116   they point to internal structures! */ 
     117const sensors_chip_name *sensors_get_detected_chips(const sensors_chip_name 
     118                                                    *match, int *nr); 
    117119 
    118120/* These defines are used in the mode field of sensors_feature_data */ 
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/rrd.c

    r4694 r4701  
    140140  int i = 0, j, ret = 0, num = 0; 
    141141 
    142   while ((ret == 0) && ((chip = sensors_get_detected_chips (&i)) != NULL)) { 
    143     for (j = 0; (ret == 0) && (j < numChipNames); ++ j) { 
    144       if (sensors_match_chip (chip, &chipNames[j])) { 
    145         int index0, subindex, chipindex = -1; 
    146         for (index0 = 0; knownChips[index0]; ++ index0) 
    147           for (subindex = 0; knownChips[index0]->names[subindex]; ++ subindex) 
    148             if (!strcmp (chip->prefix, knownChips[index0]->names[subindex])) 
    149               chipindex = index0; 
    150         if (chipindex >= 0) { 
    151           const ChipDescriptor *descriptor = knownChips[chipindex]; 
    152           const FeatureDescriptor *features = descriptor->features; 
    153  
    154           for (index0 = 0; (ret == 0) && (num < MAX_RRD_SENSORS) && features[index0].format; ++ index0) { 
    155             const FeatureDescriptor *feature = features + index0; 
    156             int labelNumber = feature->dataNumbers[0]; 
    157             const char *rawLabel = NULL; 
    158             char *label = NULL; 
    159             int valid = 0; 
    160             if (getValid (chip, labelNumber, &valid)) { 
    161               sensorLog (LOG_ERR, "Error getting sensor validity: %s/#%d", chip->prefix, labelNumber); 
    162               ret = -1; 
    163             } else if (getRawLabel (chip, labelNumber, &rawLabel)) { 
    164               sensorLog (LOG_ERR, "Error getting raw sensor label: %s/#%d", chip->prefix, labelNumber); 
    165               ret = -1; 
    166             } else if (!(label = sensors_get_label (chip, labelNumber))) { 
    167               sensorLog (LOG_ERR, "Error getting sensor label: %s/#%d", chip->prefix, labelNumber); 
    168               ret = -1; 
    169             } else if (valid) { 
    170               rrdCheckLabel (rawLabel, num); 
    171               ret = fn (data, rrdLabels[num], label, feature); 
    172               ++ num; 
    173             } 
    174             if (label) 
    175               free (label); 
     142  for (j = 0; (ret == 0) && (j < numChipNames); ++ j) { 
     143    while ((ret == 0) && ((chip = sensors_get_detected_chips (&chipNames[j], &i)) != NULL)) { 
     144      int index0, subindex, chipindex = -1; 
     145      for (index0 = 0; knownChips[index0]; ++ index0) 
     146        for (subindex = 0; knownChips[index0]->names[subindex]; ++ subindex) 
     147          if (!strcmp (chip->prefix, knownChips[index0]->names[subindex])) 
     148            chipindex = index0; 
     149      if (chipindex >= 0) { 
     150        const ChipDescriptor *descriptor = knownChips[chipindex]; 
     151        const FeatureDescriptor *features = descriptor->features; 
     152 
     153        for (index0 = 0; (ret == 0) && (num < MAX_RRD_SENSORS) && features[index0].format; ++ index0) { 
     154          const FeatureDescriptor *feature = features + index0; 
     155          int labelNumber = feature->dataNumbers[0]; 
     156          const char *rawLabel = NULL; 
     157          char *label = NULL; 
     158          int valid = 0; 
     159          if (getValid (chip, labelNumber, &valid)) { 
     160            sensorLog (LOG_ERR, "Error getting sensor validity: %s/#%d", chip->prefix, labelNumber); 
     161            ret = -1; 
     162          } else if (getRawLabel (chip, labelNumber, &rawLabel)) { 
     163            sensorLog (LOG_ERR, "Error getting raw sensor label: %s/#%d", chip->prefix, labelNumber); 
     164            ret = -1; 
     165          } else if (!(label = sensors_get_label (chip, labelNumber))) { 
     166            sensorLog (LOG_ERR, "Error getting sensor label: %s/#%d", chip->prefix, labelNumber); 
     167            ret = -1; 
     168          } else if (valid) { 
     169            rrdCheckLabel (rawLabel, num); 
     170            ret = fn (data, rrdLabels[num], label, feature); 
     171            ++ num; 
    176172          } 
     173          if (label) 
     174            free (label); 
    177175        } 
    178176      } 
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/sense.c

    r4695 r4701  
    239239  int i = 0, j, ret = 0; 
    240240 
    241   while ((ret == 0) && ((chip = sensors_get_detected_chips (&i)) != NULL)) { 
    242     for (j = 0; (ret == 0) && (j < numChipNames); ++ j) { 
    243       if (sensors_match_chip (chip, &chipNames[j])) { 
    244         ret = doChip (chip, action); 
    245       } 
     241  for (j = 0; (ret == 0) && (j < numChipNames); ++ j) { 
     242    while ((ret == 0) && ((chip = sensors_get_detected_chips (&chipNames[j], &i)) != NULL)) { 
     243      ret = doChip (chip, action); 
    246244    } 
    247245  } 
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensors/main.c

    r4698 r4701  
    252252 
    253253  *error = 0; 
    254   for (chip_nr = 0; (chip = sensors_get_detected_chips(&chip_nr));) 
    255     for(i = 0; i < chips_count; i++) 
    256       if (sensors_match_chip(chip, &chips[i])) { 
    257         if(do_sets) { 
    258           if (do_a_set(chip)) 
    259             *error = 1; 
    260         } else 
    261           do_a_print(chip); 
    262         i = chips_count; 
    263         cnt++; 
    264       } 
    265    return(cnt); 
     254  for (i = 0; i < chips_count; i++) { 
     255    chip_nr = 0; 
     256    while ((chip = sensors_get_detected_chips(&chips[i], &chip_nr))) { 
     257      if (do_sets) { 
     258        if (do_a_set(chip)) 
     259          *error = 1; 
     260      } else 
     261        do_a_print(chip); 
     262      cnt++; 
     263    } 
     264  } 
     265  return cnt; 
    266266} 
    267267