root/lm-sensors/trunk/prog/sensors/main.c @ 502

Revision 502, 7.8 KB (checked in by kmalkki, 15 years ago)

(Kyösti)

  • removed separation of GL518SM to revision 0x00 and 0x80
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    main.c - Part of sensors, a user-space program for hardware monitoring
3    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <getopt.h>
23#include <string.h>
24#include <errno.h>
25
26#include "lib/sensors.h"
27#include "lib/error.h"
28#include "chips.h"
29
30#define PROGRAM "sensors"
31#define VERSION "0.0"
32#define DEFAULT_CONFIG_FILE_NAME "sensors.conf"
33
34static char *config_file_name;
35FILE *config_file;
36static const char *config_file_path[] = 
37  { "/etc", "/usr/lib/sensors", "/usr/local/lib/sensors", "/usr/lib",
38    "/usr/local/lib", ".", 0 };
39
40extern int main(int argc, char *arv[]);
41static void print_short_help(void);
42static void print_long_help(void);
43static void print_version(void);
44static void open_config_file(void);
45static int open_this_config_file(char *filename);
46static void do_a_print(sensors_chip_name name);
47static void do_a_set(sensors_chip_name name);
48static void do_the_real_work(void);
49static const char *sprintf_chip_name(sensors_chip_name name);
50
51#define CHIPS_MAX 20
52sensors_chip_name chips[CHIPS_MAX];
53int chips_count=0;
54int do_sets;
55
56void print_short_help(void)
57{
58  printf("Try `%s -h' for more information\n",PROGRAM);
59}
60
61void print_long_help(void)
62{
63  printf("Usage: %s [OPTION]... [CHIP]...\n",PROGRAM);
64  printf("  -c, --config-file     Specify a config file\n");
65  printf("  -h, --help            Display this help text\n");
66  printf("  -s, --set             Execute `set' statements too (root only)\n");
67  printf("  -v, --version         Display the program version\n");
68  printf("\n");
69  printf("By default, a list of directories is examined for the config file `sensors.conf'\n");
70  printf("Use `-' after `-c' to read the config file from stdin\n");
71  printf("If no chips are specified, all chip info will be printed\n");
72}
73
74void print_version(void)
75{
76  printf("%s version %s\n", PROGRAM, VERSION);
77}
78
79/* This examines global var config_file, and leaves the name there too.
80   It also opens config_file. */
81void open_config_file(void)
82{
83#define MAX_FILENAME_LEN 1024
84  char *filename;
85  char buffer[MAX_FILENAME_LEN];
86  int res,i;
87
88  if (config_file_name && !strcmp(config_file_name,"-")) {
89    config_file = stdin;
90    return;
91  } else if (config_file_name && index(config_file_name,'/')) {
92    if ((res = open_this_config_file(config_file_name))) {
93      fprintf(stderr,"Could not locate or open config file\n");
94      fprintf(stderr,"%s: %s\n",config_file_name,strerror(res));
95      exit(1);
96    }
97  }
98  else {
99    if (config_file_name)
100      filename = config_file_name;
101    else
102      filename = strdup(DEFAULT_CONFIG_FILE_NAME);
103    for (i = 0; config_file_path[i]; i++) {
104      if ((snprintf(buffer,MAX_FILENAME_LEN,
105                   "%s/%s",config_file_path[i],filename)) < 1) {
106        fprintf(stderr,
107                "open_config_file: ridiculous long config file name!\n");
108        exit(1);
109      }
110      if (!open_this_config_file(buffer)) {
111        free(config_file_name);
112        config_file_name = strdup(buffer);
113        return;
114      }
115    }
116    fprintf(stderr,"Could not locate or open config file!\n");
117    exit(1);
118  }
119}
120   
121int open_this_config_file(char *filename)
122{
123  config_file = fopen(filename,"r");
124  if (! config_file)
125    return -errno;
126  return 0;
127}
128 
129
130int main (int argc, char *argv[])
131{
132  int c,res,i;
133
134  struct option long_opts[] =  {
135    { "help", no_argument, NULL, 'h' },
136    { "set", no_argument, NULL, 's' },
137    { "version", no_argument, NULL, 'v'},
138    { "config-file", required_argument, NULL, 'c' },
139    { 0,0,0,0 }
140  };
141
142  do_sets = 0;
143  while (1) {
144    c = getopt_long(argc,argv,"hvsc:",long_opts,NULL);
145    if (c == EOF)
146      break;
147    switch(c) {
148    case ':':
149    case '?':
150      print_short_help();
151      exit(1);
152    case 'h':
153      print_long_help();
154      exit(0);
155    case 'v':
156      print_version();
157      exit(0);
158    case 'c':
159      config_file_name = strdup(optarg);
160      break;
161    case 's':
162      do_sets = 1;
163      break;
164    default:
165      fprintf(stderr,"Internal error while parsing options!\n");
166      exit(1);
167    }
168  }
169
170  if (optind == argc) {
171    chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
172    chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY;
173    chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
174    chips_count = 1;
175  } else 
176    for(i = optind; i < argc; i++) 
177      if ((res = sensors_parse_chip_name(argv[i],chips+chips_count))) {
178        fprintf(stderr,"Parse error in chip name `%s'\n",argv[i]);
179        exit(1);
180      } else if (++chips_count == CHIPS_MAX) {
181        fprintf(stderr,"Too many chips on command line!\n");
182        exit(1);
183      }
184
185
186  open_config_file();
187
188  if ((res = sensors_init(config_file))) {
189    if (res == SENSORS_ERR_PROC)
190      fprintf(stderr,
191              "/proc/sys/dev/sensors/chips or /proc/bus/i2c unreadable:\n"
192              "Make sure you have inserted modules sensors.o and i2c-proc.o!");
193    else
194      fprintf(stderr,"%s\n",sensors_strerror(res));
195    exit(1);
196  }
197
198  do_the_real_work();
199  exit(0);
200}
201
202void do_the_real_work(void)
203{
204  const sensors_chip_name *chip;
205  int chip_nr,i;
206
207  for (chip_nr = 0; (chip = sensors_get_detected_chips(&chip_nr));)
208    for(i = 0; i < chips_count; i++)
209      if (sensors_match_chip(*chip,chips[i])) {
210        if(do_sets)
211          do_a_set(*chip);
212        else
213          do_a_print(*chip);
214        i = chips_count;
215      }
216}
217
218void do_a_set(sensors_chip_name name)
219{
220  int res;
221  if ((res = sensors_do_chip_sets(name))) 
222    fprintf(stderr,"%s: %s\n",sprintf_chip_name(name),sensors_strerror(res));
223}
224
225const char *sprintf_chip_name(sensors_chip_name name)
226{
227  #define BUF_SIZE 200
228  static char buf[BUF_SIZE];
229
230  if (name.bus == SENSORS_CHIP_NAME_BUS_ISA)
231    snprintf(buf,BUF_SIZE,"%s-isa-%04x",name.prefix,name.addr);
232  else
233    snprintf(buf,BUF_SIZE,"%s-i2c-%d-%02x",name.prefix,name.bus,name.addr);
234  return buf;
235}
236
237void do_a_print(sensors_chip_name name)
238{
239  const char *algo,*adap;
240
241  printf("%s\n",sprintf_chip_name(name));
242  adap = sensors_get_adapter_name(name.bus);
243  if (adap)
244    printf("Adapter: %s\n",adap);
245  algo = sensors_get_algorithm_name(name.bus);
246  if (algo)
247    printf("Algorithm: %s\n",algo);
248  if (!algo || !adap)
249    printf(" ERROR: Can't get adapter or algorithm?!?\n");
250  if (!strcmp(name.prefix,"lm75"))
251    print_lm75(&name);
252  else if (!strcmp(name.prefix,"adm1021") || !strcmp(name.prefix,"max1617") ||
253           !strcmp(name.prefix,"max1617a"))
254    print_adm1021(&name);
255  else if (!strcmp(name.prefix,"adm9240") || !strcmp(name.prefix,"ds1780"))
256    print_adm9240(&name);
257  else if (!strcmp(name.prefix,"lm78") || !strcmp(name.prefix,"lm78-j") ||
258           !strcmp(name.prefix,"lm79") || !strcmp(name.prefix,"sis5595"))
259    print_lm78(&name);
260  else if (!strcmp(name.prefix,"sis5595"))
261    print_sis5595(&name);
262  else if (!strcmp(name.prefix,"lm80"))
263    print_lm80(&name);
264  else if (!strcmp(name.prefix,"gl518sm"))
265    print_gl518(&name);
266  else if ((!strcmp(name.prefix,"w83781d")) ||
267           (!strcmp(name.prefix,"w83782d")) ||
268           (!strcmp(name.prefix,"w83783s")))
269    print_w83781d(&name);
270  else if (!strcmp(name.prefix,"maxilife-cg") ||
271           !strcmp(name.prefix,"maxilife-co") ||
272           !strcmp(name.prefix,"maxilife-as"))
273    print_maxilife(&name);
274  else
275    print_unknown_chip(&name);
276  printf("\n");
277}
Note: See TracBrowser for help on using the browser.