Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.59
      1 /* $NetBSD: envstat.c,v 1.59 2008/04/17 20:51:48 xtraeme Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007, 2008 Juan Romero Pardines.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 #ifndef lint
     30 __RCSID("$NetBSD: envstat.c,v 1.59 2008/04/17 20:51:48 xtraeme Exp $");
     31 #endif /* not lint */
     32 
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <stdbool.h>
     36 #include <string.h>
     37 #include <unistd.h>
     38 #include <fcntl.h>
     39 #include <err.h>
     40 #include <errno.h>
     41 #include <syslog.h>
     42 #include <sys/envsys.h>
     43 #include <sys/types.h>
     44 #include <sys/queue.h>
     45 #include <prop/proplib.h>
     46 
     47 #include "envstat.h"
     48 
     49 #define _PATH_DEV_SYSMON	"/dev/sysmon"
     50 
     51 #define ENVSYS_DFLAG	0x00000001	/* list registered devices */
     52 #define ENVSYS_FFLAG	0x00000002	/* show temp in farenheit */
     53 #define ENVSYS_LFLAG	0x00000004	/* list sensors */
     54 #define ENVSYS_XFLAG	0x00000008	/* externalize dictionary */
     55 #define ENVSYS_IFLAG 	0x00000010	/* skips invalid sensors */
     56 #define ENVSYS_SFLAG	0x00000020	/* removes all properties set */
     57 
     58 typedef struct envsys_sensor {
     59 	SIMPLEQ_ENTRY(envsys_sensor) entries;
     60 	int32_t	cur_value;
     61 	int32_t	max_value;
     62 	int32_t	min_value;
     63 	int32_t	avg_value;
     64 	int32_t critcap_value;
     65 	int32_t	critmin_value;
     66 	int32_t	critmax_value;
     67 	char	desc[ENVSYS_DESCLEN];
     68 	char	type[ENVSYS_DESCLEN];
     69 	char	drvstate[ENVSYS_DESCLEN];
     70 	char	battcap[ENVSYS_DESCLEN];
     71 	char 	dvname[ENVSYS_DESCLEN];
     72 	bool	invalid;
     73 	bool	visible;
     74 	bool	percentage;
     75 	bool	dvnprinted;
     76 } *sensor_t;
     77 
     78 typedef struct envsys_dvprops {
     79 	uint64_t	refresh_timo;
     80 	char 		refresh_units[ENVSYS_DESCLEN];
     81 	/* more values could be added in the future */
     82 } *dvprops_t;
     83 
     84 /* A simple queue to manage all sensors */
     85 static SIMPLEQ_HEAD(, envsys_sensor) sensors_list =
     86     SIMPLEQ_HEAD_INITIALIZER(sensors_list);
     87 
     88 static unsigned int 	interval, flags, width;
     89 static char 		*mydevname, *sensors;
     90 
     91 static int 		parse_dictionary(int);
     92 static int 		send_dictionary(FILE *, int);
     93 static int 		find_sensors(prop_array_t, const char *, dvprops_t);
     94 static void 		print_sensors(void);
     95 static int 		check_sensors(char *);
     96 static int 		usage(void);
     97 
     98 
     99 int main(int argc, char **argv)
    100 {
    101 	prop_dictionary_t dict;
    102 	int c, fd, rval = 0;
    103 	char *endptr, *configfile = NULL;
    104 	FILE *cf;
    105 
    106 	setprogname(argv[0]);
    107 
    108 	while ((c = getopt(argc, argv, "c:Dd:fIi:lrSs:w:x")) != -1) {
    109 		switch (c) {
    110 		case 'c':	/* configuration file */
    111 			configfile = strdup(optarg);
    112 			if (configfile == NULL)
    113 				err(EXIT_FAILURE, "strdup");
    114 			break;
    115 		case 'D':	/* list registered devices */
    116 			flags |= ENVSYS_DFLAG;
    117 			break;
    118 		case 'd':	/* show sensors of a specific device */
    119 			mydevname = strdup(optarg);
    120 			if (mydevname == NULL)
    121 				err(EXIT_FAILURE, "strdup");
    122 			break;
    123 		case 'f':	/* display temperature in Farenheit */
    124 			flags |= ENVSYS_FFLAG;
    125 			break;
    126 		case 'I':	/* Skips invalid sensors */
    127 			flags |= ENVSYS_IFLAG;
    128 			break;
    129 		case 'i':	/* wait time between intervals */
    130 			interval = (unsigned int)strtoul(optarg, &endptr, 10);
    131 			if (*endptr != '\0')
    132 				errx(EXIT_FAILURE, "bad interval '%s'", optarg);
    133 			break;
    134 		case 'l':	/* list sensors */
    135 			flags |= ENVSYS_LFLAG;
    136 			break;
    137 		case 'r':
    138 			/*
    139 			 * This flag is noop.. it's only here for
    140 			 * compatibility with the old implementation.
    141 			 */
    142 			break;
    143 		case 'S':
    144 			flags |= ENVSYS_SFLAG;
    145 			break;
    146 		case 's':	/* only show specified sensors */
    147 			sensors = strdup(optarg);
    148 			if (sensors == NULL)
    149 				err(EXIT_FAILURE, "strdup");
    150 			break;
    151 		case 'w':	/* width value for the lines */
    152 			width = strtoul(optarg, &endptr, 10);
    153 			if (*endptr != '\0')
    154 				errx(EXIT_FAILURE, "bad width '%s'", optarg);
    155 			break;
    156 		case 'x':	/* print the dictionary in raw format */
    157 			flags |= ENVSYS_XFLAG;
    158 			break;
    159 		case '?':
    160 		default:
    161 			usage();
    162 			/* NOTREACHED */
    163 		}
    164 	}
    165 
    166 	argc -= optind;
    167 	argv += optind;
    168 
    169 	if (argc > 0)
    170 		usage();
    171 
    172 	if (mydevname && sensors)
    173 		errx(EXIT_FAILURE, "-d flag cannot be used with -s");
    174 
    175 	/* Open the device in ro mode */
    176 	if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
    177 		err(EXIT_FAILURE, "%s", _PATH_DEV_SYSMON);
    178 
    179 	/* Print dictionary in raw mode */
    180 	if (flags & ENVSYS_XFLAG) {
    181 		rval = prop_dictionary_recv_ioctl(fd,
    182 						  ENVSYS_GETDICTIONARY,
    183 						  &dict);
    184 		if (rval)
    185 			errx(EXIT_FAILURE, "%s", strerror(rval));
    186 
    187 		config_dict_dump(dict);
    188 
    189 	/* Remove all properties set in dictionary */
    190 	} else if (flags & ENVSYS_SFLAG) {
    191 		/* Close the ro descriptor */
    192 		(void)close(fd);
    193 
    194 		/* open the fd in rw mode */
    195 		if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1)
    196 			err(EXIT_FAILURE, "%s", _PATH_DEV_SYSMON);
    197 
    198 		dict = prop_dictionary_create();
    199 		if (!dict)
    200 			err(EXIT_FAILURE, "prop_dictionary_create");
    201 
    202 		rval = prop_dictionary_set_bool(dict,
    203 						"envsys-remove-props",
    204 					        true);
    205 		if (!rval)
    206 			err(EXIT_FAILURE, "prop_dict_set_bool");
    207 
    208 		/* send the dictionary to the kernel now */
    209 		rval = prop_dictionary_send_ioctl(dict, fd, ENVSYS_REMOVEPROPS);
    210 		if (rval)
    211 			warnx("%s", strerror(rval));
    212 
    213 	/* Set properties in dictionary */
    214 	} else if (configfile) {
    215 		/*
    216 		 * Parse the configuration file.
    217 		 */
    218 		if ((cf = fopen(configfile, "r")) == NULL) {
    219 			syslog(LOG_ERR, "fopen failed: %s", strerror(errno));
    220 			errx(EXIT_FAILURE, "%s", strerror(errno));
    221 		}
    222 
    223 		rval = send_dictionary(cf, fd);
    224 		(void)fclose(cf);
    225 
    226 	/* Show sensors with interval */
    227 	} else if (interval) {
    228 		for (;;) {
    229 			rval = parse_dictionary(fd);
    230 			if (rval)
    231 				break;
    232 
    233 			(void)fflush(stdout);
    234 			(void)sleep(interval);
    235 		}
    236 	/* Show sensors without interval */
    237 	} else {
    238 		rval = parse_dictionary(fd);
    239 	}
    240 
    241 	if (sensors)
    242 		free(sensors);
    243 	if (mydevname)
    244 		free(mydevname);
    245 	(void)close(fd);
    246 
    247 	return rval ? EXIT_FAILURE : EXIT_SUCCESS;
    248 }
    249 
    250 static int
    251 send_dictionary(FILE *cf, int fd)
    252 {
    253 	prop_dictionary_t kdict, udict;
    254 	int error = 0;
    255 
    256 	/* Retrieve dictionary from kernel */
    257 	error = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &kdict);
    258       	if (error)
    259 		return error;
    260 
    261 	config_parse(cf, kdict);
    262 
    263 	/*
    264 	 * Dictionary built by the parser from the configuration file.
    265 	 */
    266 	udict = config_dict_parsed();
    267 
    268 	/*
    269 	 * Close the read only descriptor and open a new one read write.
    270 	 */
    271 	(void)close(fd);
    272 	if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1) {
    273 		error = errno;
    274 		warn("%s", _PATH_DEV_SYSMON);
    275 		return error;
    276 	}
    277 
    278 	/*
    279 	 * Send our sensor properties dictionary to the kernel then.
    280 	 */
    281 	error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
    282 	if (error)
    283 		warnx("%s", strerror(error));
    284 
    285 	prop_object_release(udict);
    286 	return error;
    287 }
    288 
    289 static int
    290 parse_dictionary(int fd)
    291 {
    292 	sensor_t sensor = NULL;
    293 	dvprops_t edp = NULL;
    294 	prop_array_t array;
    295 	prop_dictionary_t dict;
    296 	prop_object_iterator_t iter;
    297 	prop_object_t obj;
    298 	const char *dnp = NULL;
    299 	int rval = 0;
    300 
    301 	/* receive dictionary from kernel */
    302 	rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
    303 	if (rval)
    304 		return rval;
    305 
    306 	/* No drivers registered? */
    307 	if (prop_dictionary_count(dict) == 0) {
    308 		warnx("no drivers registered");
    309 		goto out;
    310 	}
    311 
    312 	if (mydevname) {
    313 		/* -d flag specified, print sensors only for this device */
    314 		obj = prop_dictionary_get(dict, mydevname);
    315 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    316 			warnx("unknown device `%s'", mydevname);
    317 			rval = EINVAL;
    318 			goto out;
    319 		}
    320 
    321 		rval = find_sensors(obj, mydevname, NULL);
    322 		if (rval)
    323 			goto out;
    324 
    325 	} else {
    326 		/* print sensors for all devices registered */
    327 		iter = prop_dictionary_iterator(dict);
    328 		if (iter == NULL) {
    329 			rval = EINVAL;
    330 			goto out;
    331 		}
    332 
    333 		/* iterate over the dictionary returned by the kernel */
    334 		while ((obj = prop_object_iterator_next(iter)) != NULL) {
    335 			array = prop_dictionary_get_keysym(dict, obj);
    336 			if (prop_object_type(array) != PROP_TYPE_ARRAY) {
    337 				warnx("no sensors found");
    338 				rval = EINVAL;
    339 				goto out;
    340 			}
    341 
    342 			edp = calloc(1, sizeof(*edp));
    343 			if (!edp) {
    344 				rval = ENOMEM;
    345 				goto out;
    346 			}
    347 
    348 			dnp = prop_dictionary_keysym_cstring_nocopy(obj);
    349 			rval = find_sensors(array, dnp, edp);
    350 			if (rval)
    351 				goto out;
    352 
    353 			if (((flags & ENVSYS_LFLAG) == 0) &&
    354 			    (flags & ENVSYS_DFLAG)) {
    355 				(void)printf("%s (checking events every ",
    356 				    dnp);
    357 				if (edp->refresh_timo == 1)
    358 					(void)printf("second)\n");
    359 				else
    360 					(void)printf("%d seconds)\n",
    361 					    (int)edp->refresh_timo);
    362 			}
    363 
    364 			free(edp);
    365 			edp = NULL;
    366 		}
    367 		prop_object_iterator_release(iter);
    368 	}
    369 
    370 	/* print sensors now */
    371 	if (sensors) {
    372 		char *str = strdup(sensors);
    373 		if (!str) {
    374 			rval = ENOMEM;
    375 			goto out;
    376 		}
    377 		rval = check_sensors(str);
    378 		if (rval) {
    379 			free(str);
    380 			goto out;
    381 		}
    382 		free(str);
    383 	}
    384 	if ((flags & ENVSYS_LFLAG) == 0 && (flags & ENVSYS_DFLAG) == 0)
    385 		print_sensors();
    386 	if (interval)
    387 		(void)printf("\n");
    388 
    389 out:
    390 	while ((sensor = SIMPLEQ_FIRST(&sensors_list))) {
    391 		SIMPLEQ_REMOVE_HEAD(&sensors_list, entries);
    392 		free(sensor);
    393 	}
    394 	if (edp)
    395 		free(edp);
    396 	prop_object_release(dict);
    397 	return rval;
    398 }
    399 
    400 static int
    401 find_sensors(prop_array_t array, const char *dvname, dvprops_t edp)
    402 {
    403 	prop_object_iterator_t iter;
    404 	prop_object_t obj, obj1, obj2;
    405 	prop_string_t state, desc = NULL;
    406 	sensor_t sensor = NULL;
    407 
    408 	iter = prop_array_iterator(array);
    409 	if (!iter)
    410 		return ENOMEM;
    411 
    412 	/* iterate over the array of dictionaries */
    413 	while ((obj = prop_object_iterator_next(iter)) != NULL) {
    414 		/* get the refresh-timeout property */
    415 		obj2 = prop_dictionary_get(obj, "device-properties");
    416 		if (obj2) {
    417 			if (!edp)
    418 				continue;
    419 			if (!prop_dictionary_get_uint64(obj2,
    420 							"refresh-timeout",
    421 							&edp->refresh_timo))
    422 				continue;
    423 		}
    424 
    425 		/* new sensor coming */
    426 		sensor = calloc(1, sizeof(*sensor));
    427 		if (sensor == NULL)
    428 			return ENOMEM;
    429 
    430 		/* copy device name */
    431 		(void)strlcpy(sensor->dvname, dvname, sizeof(sensor->dvname));
    432 
    433 		/* description string */
    434 		desc = prop_dictionary_get(obj, "description");
    435 		if (desc) {
    436 			/* copy description */
    437 			(void)strlcpy(sensor->desc,
    438 			    prop_string_cstring_nocopy(desc),
    439 		    	    sizeof(sensor->desc));
    440 		} else {
    441 			free(sensor);
    442 			continue;
    443 		}
    444 
    445 		/* type string */
    446 		obj1  = prop_dictionary_get(obj, "type");
    447 		if (obj1) {
    448 			/* copy type */
    449 			(void)strlcpy(sensor->type,
    450 		    	    prop_string_cstring_nocopy(obj1),
    451 		    	    sizeof(sensor->type));
    452 		} else {
    453 			free(sensor);
    454 			continue;
    455 		}
    456 
    457 		/* check sensor's state */
    458 		state = prop_dictionary_get(obj, "state");
    459 
    460 		/* mark sensors with invalid/unknown state */
    461 		if ((prop_string_equals_cstring(state, "invalid") ||
    462 		     prop_string_equals_cstring(state, "unknown")))
    463 			sensor->invalid = true;
    464 
    465 		/* get current drive state string */
    466 		obj1 = prop_dictionary_get(obj, "drive-state");
    467 		if (obj1) {
    468 			(void)strlcpy(sensor->drvstate,
    469 			    prop_string_cstring_nocopy(obj1),
    470 			    sizeof(sensor->drvstate));
    471 		}
    472 
    473 		/* get current battery capacity string */
    474 		obj1 = prop_dictionary_get(obj, "battery-capacity");
    475 		if (obj1) {
    476 			(void)strlcpy(sensor->battcap,
    477 			    prop_string_cstring_nocopy(obj1),
    478 			    sizeof(sensor->battcap));
    479 		}
    480 
    481 		/* get current value */
    482 		obj1 = prop_dictionary_get(obj, "cur-value");
    483 		if (obj1)
    484 			sensor->cur_value = prop_number_integer_value(obj1);
    485 
    486 		/* get max value */
    487 		obj1 = prop_dictionary_get(obj, "max-value");
    488 		if (obj1)
    489 			sensor->max_value = prop_number_integer_value(obj1);
    490 
    491 		/* get min value */
    492 		obj1 = prop_dictionary_get(obj, "min-value");
    493 		if (obj1)
    494 			sensor->min_value = prop_number_integer_value(obj1);
    495 
    496 		/* get avg value */
    497 		obj1 = prop_dictionary_get(obj, "avg-value");
    498 		if (obj1)
    499 			sensor->avg_value = prop_number_integer_value(obj1);
    500 
    501 		/* get percentage flag */
    502 		obj1 = prop_dictionary_get(obj, "want-percentage");
    503 		if (obj1)
    504 			sensor->percentage = prop_bool_true(obj1);
    505 
    506 		/* get critical max value if available */
    507 		obj1 = prop_dictionary_get(obj, "critical-max");
    508 		if (obj1)
    509 			sensor->critmax_value = prop_number_integer_value(obj1);
    510 
    511 		/* get critical min value if available */
    512 		obj1 = prop_dictionary_get(obj, "critical-min");
    513 		if (obj1)
    514 			sensor->critmin_value = prop_number_integer_value(obj1);
    515 
    516 		/* get critical capacity value if available */
    517 		obj1 = prop_dictionary_get(obj, "critical-capacity");
    518 		if (obj1)
    519 			sensor->critcap_value = prop_number_integer_value(obj1);
    520 
    521 		/* print sensor names if -l was given */
    522 		if (flags & ENVSYS_LFLAG) {
    523 			if (width)
    524 				(void)printf("%*s\n", width,
    525 				    prop_string_cstring_nocopy(desc));
    526 			else
    527 				(void)printf("%s\n",
    528 				    prop_string_cstring_nocopy(desc));
    529 		}
    530 
    531 		/* Add the sensor into the list */
    532 		SIMPLEQ_INSERT_TAIL(&sensors_list, sensor, entries);
    533 	}
    534 
    535 	/* free memory */
    536 	prop_object_iterator_release(iter);
    537 	return 0;
    538 }
    539 
    540 static int
    541 check_sensors(char *str)
    542 {
    543 	sensor_t sensor = NULL;
    544 	char *dvstring, *sstring, *p, *last;
    545 	bool sensor_found = false;
    546 
    547 	/*
    548 	 * Parse device name and sensor description and find out
    549 	 * if the sensor is valid.
    550 	 */
    551 	for ((p = strtok_r(str, ",", &last)); p;
    552 	     (p = strtok_r(NULL, ",", &last))) {
    553 		/* get device name */
    554 		dvstring = strtok(p, ":");
    555 		if (dvstring == NULL) {
    556 			warnx("missing device name");
    557 			return EINVAL;
    558 		}
    559 
    560 		/* get sensor description */
    561 		sstring = strtok(NULL, ":");
    562 		if (sstring == NULL) {
    563 			warnx("missing sensor description");
    564 			return EINVAL;
    565 		}
    566 
    567 		SIMPLEQ_FOREACH(sensor, &sensors_list, entries) {
    568 			/* skip until we match device */
    569 			if (strcmp(dvstring, sensor->dvname))
    570 				continue;
    571 			if (strcmp(sstring, sensor->desc) == 0) {
    572 				sensor->visible = true;
    573 				sensor_found = true;
    574 				break;
    575 			}
    576 		}
    577 		if (sensor_found == false) {
    578 			warnx("unknown sensor `%s' for device `%s'",
    579 		       	    sstring, dvstring);
    580 			return EINVAL;
    581 		}
    582 		sensor_found = false;
    583 	}
    584 
    585 	/* check if all sensors were ok, and error out if not */
    586 	SIMPLEQ_FOREACH(sensor, &sensors_list, entries)
    587 		if (sensor->visible)
    588 			return 0;
    589 
    590 	warnx("no sensors selected to display");
    591 	return EINVAL;
    592 }
    593 
    594 static void
    595 print_sensors(void)
    596 {
    597 	sensor_t sensor;
    598 	size_t maxlen = 0;
    599 	double temp = 0;
    600 	const char *invalid = "N/A", *degrees = NULL, *tmpstr = NULL;
    601 
    602 	/* find the longest description */
    603 	SIMPLEQ_FOREACH(sensor, &sensors_list, entries)
    604 		if (strlen(sensor->desc) > maxlen)
    605 			maxlen = strlen(sensor->desc);
    606 
    607 	if (width)
    608 		maxlen = width;
    609 
    610 	/* print the sensors */
    611 	SIMPLEQ_FOREACH(sensor, &sensors_list, entries) {
    612 		/* skip sensors that were not marked as visible */
    613 		if (sensors && !sensor->visible)
    614 			continue;
    615 
    616 		/* skip invalid sensors if -I is set */
    617 		if ((flags & ENVSYS_IFLAG) && sensor->invalid)
    618 			continue;
    619 
    620 		/* print device name */
    621 		if (!mydevname) {
    622 			if (tmpstr == NULL || strcmp(tmpstr, sensor->dvname))
    623 				printf("[%s]\n", sensor->dvname);
    624 
    625 			tmpstr = sensor->dvname;
    626 		}
    627 
    628 		/* print sensor description */
    629 		(void)printf("%s%*.*s", mydevname ? "" : "  ", (int)maxlen,
    630 		    (int)maxlen, sensor->desc);
    631 
    632 		/* print invalid string */
    633 		if (sensor->invalid) {
    634 			(void)printf(": %10s\n", invalid);
    635 			continue;
    636 		}
    637 
    638 		/*
    639 		 * Indicator and Battery charge sensors.
    640 		 */
    641 		if ((strcmp(sensor->type, "Indicator") == 0) ||
    642 		    (strcmp(sensor->type, "Battery charge") == 0)) {
    643 
    644 			(void)printf(": %10s", sensor->cur_value ? "ON" : "OFF");
    645 
    646 /* converts the value to degC or degF */
    647 #define CONVERTTEMP(a, b, c)					\
    648 do {								\
    649 	if (b) 							\
    650 		(a) = ((b) / 1000000.0) - 273.15;		\
    651 	if (flags & ENVSYS_FFLAG) {				\
    652 		if (b)						\
    653 			(a) = (9.0 / 5.0) * (a) + 32.0;		\
    654 		(c) = "degF";					\
    655 	} else							\
    656 		(c) = "degC";					\
    657 } while (/* CONSTCOND */ 0)
    658 
    659 
    660 		/* temperatures */
    661 		} else if (strcmp(sensor->type, "Temperature") == 0) {
    662 
    663 			CONVERTTEMP(temp, sensor->cur_value, degrees);
    664 			(void)printf(": %10.3f %s", temp, degrees);
    665 
    666 			if (sensor->critmax_value || sensor->critmin_value)
    667 				(void)printf("  ");
    668 
    669 			if (sensor->critmax_value) {
    670 				CONVERTTEMP(temp, sensor->critmax_value,
    671 				    degrees);
    672 				(void)printf("max: %8.3f %s  ", temp, degrees);
    673 			}
    674 
    675 			if (sensor->critmin_value) {
    676 				CONVERTTEMP(temp, sensor->critmin_value,
    677 				    degrees);
    678 				(void)printf("min: %8.3f %s", temp, degrees);
    679 			}
    680 #undef CONVERTTEMP
    681 
    682 		/* fans */
    683 		} else if (strcmp(sensor->type, "Fan") == 0) {
    684 
    685 			(void)printf(": %10u RPM", sensor->cur_value);
    686 
    687 			if (sensor->critmax_value || sensor->critmin_value)
    688 				(void)printf("   ");
    689 			if (sensor->critmax_value)
    690 				(void)printf("max: %8u RPM   ",
    691 				    sensor->critmax_value);
    692 			if (sensor->critmin_value)
    693 				(void)printf("min: %8u RPM",
    694 				    sensor->critmin_value);
    695 
    696 		/* integers */
    697 		} else if (strcmp(sensor->type, "Integer") == 0) {
    698 
    699 			(void)printf(": %10d", sensor->cur_value);
    700 
    701 		/* drives  */
    702 		} else if (strcmp(sensor->type, "Drive") == 0) {
    703 
    704 			(void)printf(": %10s", sensor->drvstate);
    705 
    706 		/* Battery capacity */
    707 		} else if (strcmp(sensor->type, "Battery capacity") == 0) {
    708 
    709 			(void)printf(": %10s", sensor->battcap);
    710 
    711 		/* everything else */
    712 		} else {
    713 			const char *type;
    714 
    715 			if (strcmp(sensor->type, "Voltage DC") == 0)
    716 				type = "V";
    717 			else if (strcmp(sensor->type, "Voltage AC") == 0)
    718 				type = "VAC";
    719 			else if (strcmp(sensor->type, "Ampere") == 0)
    720 				type = "A";
    721 			else if (strcmp(sensor->type, "Watts") == 0)
    722 				type = "W";
    723 			else if (strcmp(sensor->type, "Ohms") == 0)
    724 				type = "Ohms";
    725 			else if (strcmp(sensor->type, "Watt hour") == 0)
    726 				type = "Wh";
    727 			else if (strcmp(sensor->type, "Ampere hour") == 0)
    728 				type = "Ah";
    729 			else
    730 				type = NULL;
    731 
    732 			(void)printf(": %10.3f %s",
    733 			    sensor->cur_value / 1000000.0, type);
    734 
    735 			if (sensor->percentage && sensor->max_value) {
    736 				(void)printf(" (%5.2f%%)",
    737 				    (sensor->cur_value * 100.0) /
    738 				    sensor->max_value);
    739 			}
    740 
    741 			if (sensor->critcap_value) {
    742 				(void)printf(" critical (%5.2f%%)",
    743 				    (sensor->critcap_value * 100.0) /
    744 				    sensor->max_value);
    745 			}
    746 
    747 			if (sensor->critmax_value || sensor->critmin_value)
    748 				(void)printf("     ");
    749 			if (sensor->critmax_value)
    750 				(void)printf("max: %8.3f %s     ",
    751 				    sensor->critmax_value / 1000000.0,
    752 				    type);
    753 			if (sensor->critmin_value)
    754 				(void)printf("min: %8.3f %s",
    755 				    sensor->critmin_value / 1000000.0,
    756 				    type);
    757 
    758 		}
    759 
    760 		(void)printf("\n");
    761 	}
    762 }
    763 
    764 static int
    765 usage(void)
    766 {
    767 	(void)fprintf(stderr, "Usage: %s [-DfIlrSx] ", getprogname());
    768 	(void)fprintf(stderr, "[-c file] [-d device] [-i interval] ");
    769 	(void)fprintf(stderr, "[-s device:sensor,...] [-w width]\n");
    770 	exit(EXIT_FAILURE);
    771 	/* NOTREACHED */
    772 }
    773