root/lm-sensors/branches/lm-sensors-3.0.0/prog/sensors/main.c @ 4728

Revision 4728, 7.2 KB (checked in by khali, 6 years ago)

Remove "generic" from all printing function names. This is the default
mode now so no need to mention it explicitly.
Rename print_vid_info to print_chip_vid and move it around for
consistency.

  • 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#include <locale.h>
26#include <langinfo.h>
27
28#ifndef __UCLIBC__
29#include <iconv.h>
30#define HAVE_ICONV
31#endif
32
33#include "lib/sensors.h"
34#include "lib/error.h"
35#include "main.h"
36#include "chips.h"
37#include "version.h"
38
39#define PROGRAM                 "sensors"
40#define VERSION                 LM_VERSION
41#define DEFAULT_CONFIG_FILE     ETCDIR "/sensors.conf"
42
43static int do_sets, do_raw, hide_adapter;
44
45int fahrenheit;
46char degstr[5]; /* store the correct string to print degrees */
47
48static void print_short_help(void)
49{
50        printf("Try `%s -h' for more information\n", PROGRAM);
51}
52
53static void print_long_help(void)
54{
55        printf("Usage: %s [OPTION]... [CHIP]...\n", PROGRAM);
56        printf("  -c, --config-file     Specify a config file (default: %s)\n",
57               DEFAULT_CONFIG_FILE);
58        puts("  -h, --help            Display this help text\n"
59             "  -s, --set             Execute `set' statements (root only)\n"
60             "  -f, --fahrenheit      Show temperatures in degrees fahrenheit\n"
61             "  -A, --no-adapter      Do not show adapter for each chip\n"
62             "  -u                    Raw output (debugging only)\n"
63             "  -v, --version         Display the program version\n"
64             "\n"
65             "Use `-' after `-c' to read the config file from stdin.\n"
66             "If no chips are specified, all chip info will be printed.\n"
67             "Example chip names:\n"
68             "\tlm78-i2c-0-2d\t*-i2c-0-2d\n"
69             "\tlm78-i2c-0-*\t*-i2c-0-*\n"
70             "\tlm78-i2c-*-2d\t*-i2c-*-2d\n"
71             "\tlm78-i2c-*-*\t*-i2c-*-*\n"
72             "\tlm78-isa-0290\t*-isa-0290\n"
73             "\tlm78-isa-*\t*-isa-*\n"
74             "\tlm78-*");
75}
76
77static void print_version(void)
78{
79        printf("%s version %s with libsensors version %s\n", PROGRAM, VERSION,
80               libsensors_version);
81}
82
83/* Return 0 on success, and an exit error code otherwise */
84static int read_config_file(const char *config_file_name)
85{
86        FILE *config_file;
87        int err;
88
89        if (!strcmp(config_file_name, "-"))
90                config_file = stdin;
91        else
92                config_file = fopen(config_file_name, "r");
93
94        if (!config_file) {
95                fprintf(stderr, "Could not open config file\n");
96                perror(config_file_name);
97                return 1;
98        }
99
100        err = sensors_init(config_file);
101        if (err) {
102                fprintf(stderr, "sensors_init: %s\n", sensors_strerror(err));
103                return 1;
104        }
105
106        if (fclose(config_file) == EOF)
107                perror(config_file_name);
108
109        return 0;
110}
111
112static void set_degstr(void)
113{
114        const char *deg_default_text[2] = { " C", " F" };
115
116#ifdef HAVE_ICONV
117        /* Size hardcoded for better performance.
118           Don't forget to count the trailing \0! */
119        size_t deg_latin1_size = 3;
120        char *deg_latin1_text[2] = { "\260C", "\260F" };
121        size_t nconv;
122        size_t degstr_size = sizeof(degstr);
123        char *degstr_ptr = degstr;
124
125        iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1");
126        if (cd != (iconv_t) -1) {
127                nconv = iconv(cd, &(deg_latin1_text[fahrenheit]),
128                              &deg_latin1_size, &degstr_ptr, &degstr_size);
129                iconv_close(cd);
130
131                if (nconv != (size_t) -1)
132                        return;
133        }
134#endif /* HAVE_ICONV */
135
136        /* There was an error during the conversion, use the default text */
137        strcpy(degstr, deg_default_text[fahrenheit]);
138}
139
140static const char *sprintf_chip_name(const sensors_chip_name *name)
141{
142#define BUF_SIZE 200
143        static char buf[BUF_SIZE];
144
145        if (sensors_snprintf_chip_name(buf, BUF_SIZE, name) < 0)
146                return NULL;
147        return buf;
148}
149
150static void do_a_print(const sensors_chip_name *name)
151{
152        printf("%s\n", sprintf_chip_name(name));
153        if (!hide_adapter) {
154                const char *adap = sensors_get_adapter_name(&name->bus);
155                if (adap)
156                        printf("Adapter: %s\n", adap);
157                else
158                        fprintf(stderr, "Can't get adapter name\n");
159        }
160        if (do_raw)
161                print_chip_raw(name);
162        else
163                print_chip(name);
164        printf("\n");
165}
166
167/* returns 1 on error */
168static int do_a_set(const sensors_chip_name *name)
169{
170        int res;
171
172        if ((res = sensors_do_chip_sets(name))) {
173                if (res == -SENSORS_ERR_PROC) {
174                        fprintf(stderr, "%s: %s for writing;\n",
175                                sprintf_chip_name(name),
176                                sensors_strerror(res));
177                        fprintf(stderr, "Run as root?\n");
178                        return 1;
179                } else if (res == -SENSORS_ERR_ACCESS_W) {
180                        fprintf(stderr,
181                                "%s: At least one \"set\" statement failed\n",
182                                sprintf_chip_name(name));
183                } else {
184                        fprintf(stderr, "%s: %s\n", sprintf_chip_name(name),
185                                sensors_strerror(res));
186                }
187        }
188        return 0;
189}
190
191/* returns number of chips found */
192static int do_the_real_work(const sensors_chip_name *match, int *error)
193{
194        const sensors_chip_name *chip;
195        int chip_nr;
196        int cnt = 0;
197
198        chip_nr = 0;
199        while ((chip = sensors_get_detected_chips(match, &chip_nr))) {
200                if (do_sets) {
201                        if (do_a_set(chip))
202                                *error = 1;
203                } else
204                        do_a_print(chip);
205                cnt++;
206        }
207        return cnt;
208}
209
210int main(int argc, char *argv[])
211{
212        int c, res, i, error;
213        const char *config_file_name = DEFAULT_CONFIG_FILE;
214
215        struct option long_opts[] =  {
216                { "help", no_argument, NULL, 'h' },
217                { "set", no_argument, NULL, 's' },
218                { "version", no_argument, NULL, 'v'},
219                { "fahrenheit", no_argument, NULL, 'f' },
220                { "no-adapter", no_argument, NULL, 'A' },
221                { "config-file", required_argument, NULL, 'c' },
222                { 0, 0, 0, 0 }
223        };
224
225        setlocale(LC_CTYPE, "");
226
227        do_raw = 0;
228        do_sets = 0;
229        hide_adapter = 0;
230        while (1) {
231                c = getopt_long(argc, argv, "hsvfAc:u", long_opts, NULL);
232                if (c == EOF)
233                        break;
234                switch(c) {
235                case ':':
236                case '?':
237                        print_short_help();
238                        exit(1);
239                case 'h':
240                        print_long_help();
241                        exit(0);
242                case 'v':
243                        print_version();
244                        exit(0);
245                case 'c':
246                        config_file_name = optarg;
247                        break;
248                case 's':
249                        do_sets = 1;
250                        break;
251                case 'f':
252                        fahrenheit = 1;
253                        break;
254                case 'A':
255                        hide_adapter = 1;
256                        break;
257                case 'u':
258                        do_raw = 1;
259                        break;
260                default:
261                        fprintf(stderr,
262                                "Internal error while parsing options!\n");
263                        exit(1);
264                }
265        }
266
267        res = read_config_file(config_file_name);
268        if (res)
269                exit(res);
270
271        /* build the degrees string */
272        set_degstr();
273
274        if (optind == argc) { /* No chip name on command line */
275                if (!do_the_real_work(NULL, &error)) {
276                        fprintf(stderr,
277                                "No sensors found!\n"
278                                "Make sure you loaded all the kernel drivers you need.\n"
279                                "Try sensors-detect to find out which these are.\n");
280                        error = 1;
281                }
282        } else {
283                int cnt = 0;
284                sensors_chip_name chip;
285
286                for (i = optind; i < argc; i++) {
287                        if (sensors_parse_chip_name(argv[i], &chip)) {
288                                fprintf(stderr,
289                                        "Parse error in chip name `%s'\n",
290                                        argv[i]);
291                                print_short_help();
292                                error = 1;
293                                goto exit;
294                        }
295                        cnt += do_the_real_work(&chip, &error);
296                }
297
298                if (!cnt) {
299                        fprintf(stderr, "Specified sensor(s) not found!\n");
300                        error = 1;
301                }
302        }
303
304exit:
305        sensors_cleanup();
306        exit(res);
307}
Note: See TracBrowser for help on using the browser.