Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.29
      1  1.29   xtraeme /* $NetBSD: envstat.c,v 1.29 2007/07/05 14:08:37 xtraeme Exp $ */
      2   1.1      groo 
      3   1.1      groo /*-
      4  1.25   xtraeme  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5   1.1      groo  * All rights reserved.
      6   1.1      groo  *
      7   1.1      groo  * This code is derived from software contributed to The NetBSD Foundation
      8  1.25   xtraeme  * by Juan Romero Pardines.
      9   1.1      groo  *
     10   1.1      groo  * Redistribution and use in source and binary forms, with or without
     11   1.1      groo  * modification, are permitted provided that the following conditions
     12   1.1      groo  * are met:
     13   1.1      groo  * 1. Redistributions of source code must retain the above copyright
     14   1.1      groo  *    notice, this list of conditions and the following disclaimer.
     15   1.1      groo  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      groo  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      groo  *    documentation and/or other materials provided with the distribution.
     18   1.1      groo  * 3. All advertising materials mentioning features or use of this software
     19   1.1      groo  *    must display the following acknowledgement:
     20  1.25   xtraeme  *      This product includes software developed by Juan Romero Pardines
     21  1.25   xtraeme  *      for the NetBSD Foundation, Inc. and its contributors.
     22   1.1      groo  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1      groo  *    contributors may be used to endorse or promote products derived
     24   1.1      groo  *    from this software without specific prior written permission.
     25   1.1      groo  *
     26   1.1      groo  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1      groo  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1      groo  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1      groo  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1      groo  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1      groo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1      groo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1      groo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1      groo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1      groo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1      groo  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1      groo  */
     38   1.1      groo 
     39  1.25   xtraeme /*
     40  1.25   xtraeme  * TODO:
     41  1.25   xtraeme  *
     42  1.25   xtraeme  * 	o Some checks should be added to ensure that the user does not
     43  1.25   xtraeme  * 	  set unwanted values for the critical limits.
     44  1.25   xtraeme  */
     45   1.1      groo 
     46   1.1      groo #include <stdio.h>
     47   1.1      groo #include <stdlib.h>
     48  1.25   xtraeme #include <stdbool.h>
     49   1.1      groo #include <string.h>
     50   1.1      groo #include <unistd.h>
     51  1.25   xtraeme #include <fcntl.h>
     52   1.3   thorpej #include <err.h>
     53  1.25   xtraeme #include <errno.h>
     54  1.25   xtraeme #include <prop/proplib.h>
     55   1.1      groo #include <sys/envsys.h>
     56   1.1      groo 
     57  1.25   xtraeme #define _PATH_DEV_SYSMON	"/dev/sysmon"
     58   1.1      groo 
     59  1.25   xtraeme #define ENVSYS_DFLAG	0x00000001	/* list registered devices */
     60  1.25   xtraeme #define ENVSYS_FFLAG	0x00000002	/* show temp in farenheit */
     61  1.25   xtraeme #define ENVSYS_LFLAG	0x00000004	/* list sensors */
     62  1.25   xtraeme #define ENVSYS_XFLAG	0x00000008	/* externalize dictionary */
     63   1.6  explorer 
     64  1.25   xtraeme /*
     65  1.25   xtraeme  * Operation flags for -m.
     66  1.25   xtraeme  */
     67  1.25   xtraeme #define USERF_SCRITICAL 0x00000001	/* set a critical limit */
     68  1.25   xtraeme #define USERF_RCRITICAL 0x00000002	/* remove a critical limit */
     69  1.25   xtraeme #define USERF_SCRITMAX	0x00000004	/* set a critical max limit */
     70  1.25   xtraeme #define USERF_RCRITMAX	0x00000008	/* remove a critical max limit */
     71  1.25   xtraeme #define USERF_SCRITMIN	0x00000010	/* set a critical min limit */
     72  1.25   xtraeme #define USERF_RCRITMIN	0x00000020	/* remove a critical min limit */
     73  1.25   xtraeme #define USERF_SRFACT	0x00000040	/* set a new rfact */
     74  1.25   xtraeme #define USERF_SDESCR	0x00000080	/* set a new description */
     75  1.25   xtraeme 
     76  1.25   xtraeme struct envsys_sensor {
     77  1.26   xtraeme 	bool	invalid;
     78  1.25   xtraeme 	bool	visible;
     79  1.25   xtraeme 	bool	percentage;
     80  1.25   xtraeme 	int32_t	cur_value;
     81  1.25   xtraeme 	int32_t	max_value;
     82  1.25   xtraeme 	int32_t	min_value;
     83  1.25   xtraeme 	int32_t	avg_value;
     84  1.25   xtraeme 	int32_t critcap_value;
     85  1.25   xtraeme 	int32_t	critmin_value;
     86  1.25   xtraeme 	int32_t	critmax_value;
     87  1.25   xtraeme 	char	desc[ENVSYS_DESCLEN];
     88  1.25   xtraeme 	char	type[ENVSYS_DESCLEN];
     89  1.25   xtraeme 	char	drvstate[ENVSYS_DESCLEN];
     90  1.25   xtraeme };
     91  1.25   xtraeme 
     92  1.25   xtraeme static int interval, flags, width;
     93  1.25   xtraeme static char *mydevname, *sensors, *userreq;
     94  1.25   xtraeme static struct envsys_sensor *gesen;
     95  1.25   xtraeme static size_t gnelems;
     96  1.25   xtraeme 
     97  1.25   xtraeme static int parse_dictionary(int);
     98  1.25   xtraeme static int send_dictionary(int);
     99  1.25   xtraeme static int find_sensors(prop_array_t);
    100  1.25   xtraeme static void print_sensors(struct envsys_sensor *, size_t);
    101  1.25   xtraeme static int check_sensors(struct envsys_sensor *, char *, size_t);
    102  1.25   xtraeme static int usage(void);
    103  1.25   xtraeme 
    104  1.25   xtraeme 
    105  1.25   xtraeme int main(int argc, char **argv)
    106  1.25   xtraeme {
    107  1.25   xtraeme 	prop_dictionary_t dict;
    108  1.25   xtraeme 	int c, fd, rval;
    109  1.25   xtraeme 	char *endptr;
    110  1.25   xtraeme 
    111  1.25   xtraeme 	rval = flags = interval = width = 0;
    112  1.25   xtraeme 
    113  1.25   xtraeme 	setprogname(argv[0]);
    114  1.25   xtraeme 
    115  1.25   xtraeme 	while ((c = getopt(argc, argv, "Dd:fi:lm:s:w:x")) != -1) {
    116  1.25   xtraeme 		switch (c) {
    117  1.25   xtraeme 		case 'd':	/* show sensors of a specific device */
    118  1.25   xtraeme 			mydevname = strdup(optarg);
    119  1.25   xtraeme 			if (mydevname == NULL)
    120  1.25   xtraeme 				err(ENOMEM, "out of memory");
    121  1.25   xtraeme 			break;
    122  1.25   xtraeme 		case 'i':	/* wait time between intervals */
    123  1.25   xtraeme 			interval = strtoul(optarg, &endptr, 10);
    124  1.25   xtraeme 			if (*endptr != '\0')
    125  1.25   xtraeme 				errx(1, "interval must be an integer");
    126  1.25   xtraeme 			break;
    127  1.25   xtraeme 		case 'D':	/* list registered devices */
    128  1.25   xtraeme 			flags |= ENVSYS_DFLAG;
    129  1.25   xtraeme 			break;
    130  1.25   xtraeme 		case 'f':	/* display temperature in Farenheit */
    131  1.25   xtraeme 			flags |= ENVSYS_FFLAG;
    132  1.25   xtraeme 			break;
    133  1.25   xtraeme 		case 'l':	/* list sensors */
    134  1.25   xtraeme 			flags |= ENVSYS_LFLAG;
    135  1.25   xtraeme 			break;
    136  1.25   xtraeme 		case 'w':	/* width value for the lines */
    137  1.25   xtraeme 			width = strtoul(optarg, &endptr, 10);
    138  1.25   xtraeme 			if (*endptr != '\0')
    139  1.25   xtraeme 				errx(1, "width must be an integer");
    140   1.6  explorer 			break;
    141  1.25   xtraeme 		case 'x':	/* print the dictionary in raw format */
    142  1.25   xtraeme 			flags |= ENVSYS_XFLAG;
    143   1.1      groo 			break;
    144  1.25   xtraeme 		case 's':	/* only show specified sensors */
    145  1.25   xtraeme 			sensors = strdup(optarg);
    146   1.1      groo 			if (sensors == NULL)
    147  1.25   xtraeme 				err(ENOMEM, "out of memory");
    148   1.1      groo 			break;
    149  1.25   xtraeme 		case 'm':
    150  1.25   xtraeme 			userreq = strdup(optarg);
    151  1.25   xtraeme 			if (userreq == NULL)
    152  1.25   xtraeme 				err(ENOMEM, "out of memory");
    153   1.1      groo 			break;
    154   1.1      groo 		case '?':
    155   1.1      groo 		default:
    156   1.1      groo 			usage();
    157   1.1      groo 			/* NOTREACHED */
    158   1.1      groo 		}
    159   1.1      groo 	}
    160   1.1      groo 
    161  1.25   xtraeme 	if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
    162  1.25   xtraeme 		err(EXIT_FAILURE, "open");
    163   1.1      groo 
    164  1.25   xtraeme 	if (!interval && (flags & ENVSYS_XFLAG)) {
    165  1.25   xtraeme 		if (prop_dictionary_recv_ioctl(fd,
    166  1.25   xtraeme 		    			       ENVSYS_GETDICTIONARY,
    167  1.25   xtraeme 					       &dict)) {
    168  1.25   xtraeme 			(void)close(fd);
    169  1.25   xtraeme 			err(EINVAL, "recv_ioctl");
    170  1.25   xtraeme 		}
    171  1.25   xtraeme 	}
    172   1.1      groo 
    173  1.25   xtraeme 	if (argc == 1) {
    174  1.25   xtraeme 		rval = parse_dictionary(fd);
    175   1.3   thorpej 
    176  1.25   xtraeme 	} else if (userreq) {
    177  1.25   xtraeme 		if (!sensors || !mydevname) {
    178  1.25   xtraeme 			(void)fprintf(stderr, "%s: -m cannot be used without "
    179  1.25   xtraeme 			    "-s and -d\n", getprogname());
    180  1.25   xtraeme 			return EINVAL;
    181  1.25   xtraeme 		}
    182  1.25   xtraeme 
    183  1.25   xtraeme 		rval = send_dictionary(fd);
    184  1.25   xtraeme 		goto out;
    185  1.25   xtraeme 
    186  1.25   xtraeme 	} else if (interval) {
    187  1.25   xtraeme 		for (;;) {
    188  1.25   xtraeme 			rval = parse_dictionary(fd);
    189  1.25   xtraeme 			if (rval)
    190  1.25   xtraeme 				goto out;
    191  1.25   xtraeme 			(void)fflush(stdout);
    192  1.25   xtraeme 			(void)sleep(interval);
    193  1.25   xtraeme 		}
    194   1.1      groo 
    195  1.25   xtraeme 	} else if (!interval) {
    196  1.25   xtraeme 		if (flags & ENVSYS_XFLAG)
    197  1.25   xtraeme 			(void)printf("%s", prop_dictionary_externalize(dict));
    198  1.25   xtraeme 		else
    199  1.25   xtraeme 			rval = parse_dictionary(fd);
    200  1.25   xtraeme 
    201  1.25   xtraeme 	} else
    202  1.25   xtraeme 		usage();
    203  1.25   xtraeme 
    204  1.25   xtraeme out:
    205  1.25   xtraeme 	if (sensors)
    206  1.25   xtraeme 		free(sensors);
    207  1.25   xtraeme 	if (userreq)
    208  1.25   xtraeme 		free(userreq);
    209  1.25   xtraeme 	if (mydevname)
    210  1.25   xtraeme 		free(mydevname);
    211  1.25   xtraeme 	(void)close(fd);
    212  1.25   xtraeme 	return rval;
    213  1.25   xtraeme }
    214  1.25   xtraeme 
    215  1.25   xtraeme static int
    216  1.25   xtraeme send_dictionary(int fd)
    217  1.25   xtraeme {
    218  1.25   xtraeme 	prop_dictionary_t dict, udict;
    219  1.25   xtraeme 	prop_object_t obj;
    220  1.25   xtraeme 	char *buf, *target, *endptr;
    221  1.25   xtraeme 	int error, i, uflag;
    222  1.25   xtraeme 	double val;
    223  1.25   xtraeme 
    224  1.25   xtraeme 	error = uflag = val = 0;
    225  1.25   xtraeme 
    226  1.25   xtraeme 	/*
    227  1.25   xtraeme 	 * part 1: kernel dictionary.
    228  1.25   xtraeme 	 *
    229  1.25   xtraeme 	 * This parts consists in parsing the kernel dictionary
    230  1.25   xtraeme 	 * to check for unknown device or sensor and we must
    231  1.25   xtraeme 	 * know what type of sensor are we trying to set
    232  1.25   xtraeme 	 * a critical condition.
    233  1.25   xtraeme 	 */
    234  1.25   xtraeme 	if (prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict))
    235  1.25   xtraeme 		return EINVAL;
    236   1.1      groo 
    237  1.25   xtraeme 	if (mydevname) {
    238  1.25   xtraeme 		obj = prop_dictionary_get(dict, mydevname);
    239  1.25   xtraeme 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    240  1.25   xtraeme 			warnx("unknown device `%s'", mydevname);
    241  1.25   xtraeme 			prop_object_release(dict);
    242  1.25   xtraeme 			return EINVAL;
    243  1.25   xtraeme 		}
    244   1.1      groo 
    245  1.25   xtraeme 		if (find_sensors(obj)) {
    246  1.25   xtraeme 			prop_object_release(dict);
    247  1.25   xtraeme 			return EINVAL;
    248  1.25   xtraeme 		}
    249   1.1      groo 	}
    250   1.1      groo 
    251  1.25   xtraeme 	/* find the type for selected sensor */
    252  1.25   xtraeme 	for (i = 0; i < gnelems; i++)
    253  1.25   xtraeme 		if (strcmp(sensors, gesen[i].desc) == 0)
    254  1.25   xtraeme 			break;
    255   1.1      groo 
    256  1.25   xtraeme 	/* we know the type of the sensor now, release kernel dict */
    257  1.25   xtraeme 	prop_object_release(dict);
    258  1.25   xtraeme 
    259  1.25   xtraeme 	/*
    260  1.25   xtraeme 	 * part 2: userland dictionary.
    261  1.25   xtraeme 	 *
    262  1.25   xtraeme 	 * This parts consists in setting the values provided
    263  1.25   xtraeme 	 * by the user and convert when necesssary to send
    264  1.25   xtraeme 	 * them to the kernel again.
    265  1.25   xtraeme 	 */
    266  1.25   xtraeme 	udict = prop_dictionary_create();
    267   1.1      groo 
    268  1.25   xtraeme 	/* create the driver-name object */
    269  1.25   xtraeme 	obj = prop_string_create_cstring_nocopy(mydevname);
    270  1.25   xtraeme 	if (obj == NULL || !prop_dictionary_set(udict, "driver-name", obj)) {
    271  1.25   xtraeme 		error = EINVAL;
    272  1.25   xtraeme 		goto out;
    273  1.25   xtraeme 	}
    274  1.25   xtraeme 
    275  1.25   xtraeme 	prop_object_release(obj);
    276  1.25   xtraeme 
    277  1.25   xtraeme 	/* create the sensor-name object */
    278  1.25   xtraeme 	obj = prop_string_create_cstring_nocopy(sensors);
    279  1.25   xtraeme 	if (obj == NULL || !prop_dictionary_set(udict, "sensor-name", obj)) {
    280  1.25   xtraeme 		error = EINVAL;
    281  1.25   xtraeme 		goto out;
    282  1.25   xtraeme 	}
    283  1.25   xtraeme 
    284  1.25   xtraeme 	prop_object_release(obj);
    285  1.25   xtraeme 	/*
    286  1.25   xtraeme 	 * parse the -m argument; we understand the following ways:
    287  1.25   xtraeme 	 *
    288  1.25   xtraeme 	 * 	-m critical/crit{max,min}=value
    289  1.25   xtraeme 	 * 	-m critical/crit{max,min}=remove
    290  1.25   xtraeme 	 * 	-m desc="BLAH"
    291  1.25   xtraeme 	 * 	-m rfact=value
    292  1.25   xtraeme 	 */
    293  1.25   xtraeme 	if (userreq) {
    294  1.25   xtraeme 		buf = strtok(userreq, "=");
    295  1.25   xtraeme 		target = strdup(buf);
    296  1.25   xtraeme 		if (target == NULL) {
    297  1.25   xtraeme 			error = ENOMEM;
    298  1.25   xtraeme 			goto out;
    299  1.25   xtraeme 		}
    300   1.1      groo 
    301  1.25   xtraeme 		while (buf != NULL) {
    302  1.25   xtraeme 			/*
    303  1.25   xtraeme 			 * skip current string if it's the same
    304  1.25   xtraeme 			 * than target requested.
    305  1.25   xtraeme 			 */
    306  1.25   xtraeme 			if (strcmp(target, buf) == 0)
    307  1.25   xtraeme 				buf = strtok(NULL, "=");
    308  1.25   xtraeme 
    309  1.25   xtraeme 			/* check what target was requested */
    310  1.25   xtraeme 			if (strcmp(target, "desc") == 0) {
    311  1.25   xtraeme 				uflag |= USERF_SDESCR;
    312  1.25   xtraeme 				obj = prop_string_create_cstring_nocopy(buf);
    313  1.25   xtraeme 				break;
    314  1.25   xtraeme #define SETNCHECKVAL(a, b)						\
    315  1.25   xtraeme do {									\
    316  1.25   xtraeme 	if (strcmp(buf, "remove") == 0)					\
    317  1.25   xtraeme 		uflag |= (a);						\
    318  1.25   xtraeme 	else {								\
    319  1.25   xtraeme 		uflag |= (b);						\
    320  1.25   xtraeme 		val = strtod(buf, &endptr);				\
    321  1.25   xtraeme 		if (*endptr != '\0') {					\
    322  1.25   xtraeme 			(void)printf("%s: invalid value\n",		\
    323  1.25   xtraeme 			    getprogname());				\
    324  1.25   xtraeme 			error = EINVAL;					\
    325  1.25   xtraeme 			goto out;					\
    326  1.25   xtraeme 		}							\
    327  1.25   xtraeme 	}								\
    328  1.25   xtraeme } while (/* CONSTCOND */ 0)
    329   1.6  explorer 
    330  1.25   xtraeme 			} else if (strcmp(target, "critical") == 0) {
    331  1.25   xtraeme 				SETNCHECKVAL(USERF_RCRITICAL, USERF_SCRITICAL);
    332  1.25   xtraeme 				break;
    333  1.25   xtraeme 			} else if (strcmp(target, "critmax") == 0) {
    334  1.25   xtraeme 				SETNCHECKVAL(USERF_RCRITMAX, USERF_SCRITMAX);
    335  1.25   xtraeme 				break;
    336  1.25   xtraeme 			} else if (strcmp(target, "critmin") == 0) {
    337  1.25   xtraeme 				SETNCHECKVAL(USERF_RCRITMIN, USERF_SCRITMIN);
    338  1.25   xtraeme 				break;
    339  1.25   xtraeme 			} else if (strcmp(target, "rfact") == 0) {
    340  1.25   xtraeme 				uflag |= USERF_SRFACT;
    341  1.25   xtraeme 				val = strtod(buf, &endptr);
    342  1.25   xtraeme 				if (*endptr != '\0') {
    343  1.25   xtraeme 					(void)printf("%s: invalid value\n",
    344  1.25   xtraeme 					    getprogname());
    345  1.25   xtraeme 					error = EINVAL;
    346  1.25   xtraeme 					goto out;
    347  1.25   xtraeme 				}
    348  1.25   xtraeme 				break;
    349  1.25   xtraeme 			} else {
    350  1.25   xtraeme 				(void)printf("%s: invalid target\n",
    351  1.25   xtraeme 				    getprogname());
    352  1.25   xtraeme 				error =  EINVAL;
    353  1.25   xtraeme 				goto out;
    354  1.25   xtraeme 			}
    355   1.6  explorer 		}
    356  1.25   xtraeme 		free(target);
    357   1.6  explorer 	}
    358   1.6  explorer 
    359  1.25   xtraeme 	/* critical capacity for percentage sensors */
    360  1.25   xtraeme 	if (uflag & USERF_SCRITICAL) {
    361  1.25   xtraeme 		/* sanity check */
    362  1.25   xtraeme 		if (val < 0 || val > 100) {
    363  1.25   xtraeme 			(void)printf("%s: invalid value (0><100)\n",
    364  1.25   xtraeme 			    getprogname());
    365  1.25   xtraeme 			error = EINVAL;
    366  1.25   xtraeme 			goto out;
    367  1.25   xtraeme 		}
    368   1.6  explorer 
    369  1.25   xtraeme 		/* ok... convert the value */
    370  1.25   xtraeme 		val = (val / 100) * gesen[i].max_value;
    371  1.25   xtraeme 		obj = prop_number_create_unsigned_integer(val);
    372   1.1      groo 	}
    373   1.1      groo 
    374  1.25   xtraeme 	/*
    375  1.25   xtraeme 	 * conversions required to send a proper value to the kernel.
    376  1.25   xtraeme 	 */
    377  1.25   xtraeme 	if ((uflag & USERF_SCRITMAX) || (uflag & USERF_SCRITMIN)) {
    378  1.25   xtraeme 		/* temperatures */
    379  1.25   xtraeme 		if (strcmp(gesen[i].type, "Temperature") == 0) {
    380  1.25   xtraeme 			/* convert from farenheit to celsius */
    381  1.25   xtraeme 			if (flags & ENVSYS_FFLAG)
    382  1.25   xtraeme 				val = (val - 32.0) * (5.0 / 9.0);
    383  1.25   xtraeme 
    384  1.25   xtraeme 			/* convert to microKelvin */
    385  1.25   xtraeme 			val = val * 1000000 + 273150000;
    386  1.25   xtraeme 			/* printf("val=%d\n", (int)val); */
    387  1.25   xtraeme 			obj = prop_number_create_unsigned_integer(val);
    388  1.25   xtraeme 		/* fans */
    389  1.25   xtraeme 		} else if (strcmp(gesen[i].type, "Fan") == 0) {
    390  1.25   xtraeme 			if (val < 0 || val > 10000) {
    391  1.25   xtraeme 				error = EINVAL;
    392  1.25   xtraeme 				goto out;
    393  1.25   xtraeme 			}
    394  1.25   xtraeme 			/* printf("val=%d\n", (int)val); */
    395  1.25   xtraeme 			obj = prop_number_create_unsigned_integer(val);
    396   1.1      groo 
    397  1.25   xtraeme 		/* volts, watts, ohms, etc */
    398  1.25   xtraeme 		} else {
    399  1.25   xtraeme 			/* convert to m[V,W,Ohms] again */
    400  1.25   xtraeme 			val *= 1000000.0;
    401  1.25   xtraeme 			/* printf("val=%5.0f\n", val); */
    402  1.25   xtraeme 			obj = prop_number_create_integer(val);
    403   1.1      groo 		}
    404   1.1      groo 	}
    405   1.1      groo 
    406  1.25   xtraeme 	/* user wanted to set a new description */
    407  1.25   xtraeme 	if (uflag & USERF_SDESCR) {
    408  1.25   xtraeme 		if (!prop_dictionary_set(udict, "new-description", obj)) {
    409  1.25   xtraeme 			error = EINVAL;
    410  1.25   xtraeme 			goto out;
    411  1.25   xtraeme 		}
    412   1.1      groo 
    413  1.25   xtraeme 	/* user wanted to set a new critical capacity */
    414  1.25   xtraeme 	} else if (uflag & USERF_SCRITICAL) {
    415  1.25   xtraeme 		if (!prop_dictionary_set(udict, "critical-capacity", obj)) {
    416  1.25   xtraeme 			error = EINVAL;
    417  1.25   xtraeme 			goto out;
    418  1.25   xtraeme 		}
    419  1.19       mrg 
    420  1.25   xtraeme 	} else if (uflag & USERF_RCRITICAL) {
    421  1.25   xtraeme 		obj = prop_bool_create(1);
    422  1.25   xtraeme 		if (!prop_dictionary_set(udict, "remove-critical-cap", obj)) {
    423  1.25   xtraeme 			error = EINVAL;
    424  1.25   xtraeme 			goto out;
    425  1.25   xtraeme 		}
    426  1.19       mrg 
    427  1.25   xtraeme 	/* user wanted to remove a critical min limit */
    428  1.25   xtraeme 	} else if (uflag & USERF_RCRITMIN) {
    429  1.25   xtraeme 		obj = prop_bool_create(1);
    430  1.25   xtraeme 		if (!prop_dictionary_set(udict, "remove-cmin-limit", obj)) {
    431  1.25   xtraeme 			error = EINVAL;
    432  1.25   xtraeme 			goto out;
    433  1.25   xtraeme 		}
    434  1.19       mrg 
    435  1.25   xtraeme 	/* user wanted to remove a critical max limit */
    436  1.25   xtraeme 	} else if (uflag & USERF_RCRITMAX) {
    437  1.25   xtraeme 		obj = prop_bool_create(1);
    438  1.25   xtraeme 		if (!prop_dictionary_set(udict, "remove-cmax-limit", obj)) {
    439  1.25   xtraeme 			error = EINVAL;
    440  1.25   xtraeme 			goto out;
    441  1.25   xtraeme 		}
    442  1.19       mrg 
    443  1.25   xtraeme 	/* user wanted to set a new critical min value */
    444  1.25   xtraeme 	} else if (uflag & USERF_SCRITMIN) {
    445  1.25   xtraeme 		if (!prop_dictionary_set(udict, "critical-min-limit", obj)) {
    446  1.25   xtraeme 			error = EINVAL;
    447  1.25   xtraeme 			goto out;
    448  1.19       mrg 		}
    449  1.25   xtraeme 
    450  1.25   xtraeme 	/* user wanted to set a new critical max value */
    451  1.25   xtraeme 	} else if (uflag & USERF_SCRITMAX) {
    452  1.25   xtraeme 		if (!prop_dictionary_set(udict, "critical-max-limit", obj)) {
    453  1.25   xtraeme 			error = EINVAL;
    454  1.25   xtraeme 			goto out;
    455  1.19       mrg 		}
    456  1.19       mrg 
    457  1.25   xtraeme 	/* user wanted to set a new rfact */
    458  1.25   xtraeme 	} else if (uflag & USERF_SRFACT) {
    459  1.25   xtraeme 		obj = prop_number_create_integer(val);
    460  1.25   xtraeme 		if (!prop_dictionary_set(udict, "new-rfact", obj)) {
    461  1.25   xtraeme 			error = EINVAL;
    462  1.25   xtraeme 			goto out;
    463  1.19       mrg 		}
    464  1.19       mrg 
    465  1.25   xtraeme 	} else {
    466  1.25   xtraeme 		(void)printf("%s: unknown operation\n", getprogname());
    467  1.25   xtraeme 		error = EINVAL;
    468  1.25   xtraeme 		goto out;
    469  1.19       mrg 	}
    470  1.19       mrg 
    471  1.25   xtraeme 	prop_object_release(obj);
    472   1.1      groo 
    473  1.25   xtraeme #if 0
    474  1.25   xtraeme 	printf("%s", prop_dictionary_externalize(udict));
    475  1.25   xtraeme 	return error;
    476  1.25   xtraeme #endif
    477   1.1      groo 
    478  1.25   xtraeme 	/* all done? send our dictionary now */
    479  1.25   xtraeme 	error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
    480   1.6  explorer 
    481  1.25   xtraeme 	if (error)
    482  1.25   xtraeme 		(void)printf("%s: %s\n", getprogname(), strerror(error));
    483  1.25   xtraeme out:
    484  1.25   xtraeme 	prop_object_release(udict);
    485  1.25   xtraeme 	return error;
    486  1.25   xtraeme }
    487  1.25   xtraeme 
    488  1.25   xtraeme static int
    489  1.25   xtraeme parse_dictionary(int fd)
    490  1.25   xtraeme {
    491  1.25   xtraeme 	prop_array_t array;
    492  1.25   xtraeme 	prop_dictionary_t dict;
    493  1.25   xtraeme 	prop_object_iterator_t iter;
    494  1.25   xtraeme 	prop_object_t obj;
    495  1.25   xtraeme 	const char *dnp = NULL;
    496  1.25   xtraeme 	int rval = 0;
    497  1.25   xtraeme 
    498  1.25   xtraeme 	/* receive dictionary from kernel */
    499  1.25   xtraeme 	if (prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict))
    500  1.25   xtraeme 		return EINVAL;
    501  1.25   xtraeme 
    502  1.25   xtraeme 	if (mydevname) {
    503  1.25   xtraeme 		obj = prop_dictionary_get(dict, mydevname);
    504  1.25   xtraeme 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    505  1.25   xtraeme 			warnx("unknown device `%s'", mydevname);
    506  1.25   xtraeme 			rval = EINVAL;
    507  1.25   xtraeme 			goto out;
    508   1.1      groo 		}
    509   1.1      groo 
    510  1.25   xtraeme 		rval = find_sensors(obj);
    511  1.25   xtraeme 		if (rval)
    512  1.25   xtraeme 			goto out;
    513  1.25   xtraeme 
    514  1.25   xtraeme 		if (interval)
    515  1.25   xtraeme 			(void)printf("\n");
    516  1.25   xtraeme 
    517  1.25   xtraeme 	} else {
    518  1.25   xtraeme 		iter = prop_dictionary_iterator(dict);
    519  1.25   xtraeme 		if (iter == NULL) {
    520  1.25   xtraeme 			rval = EINVAL;
    521  1.25   xtraeme 			goto out;
    522  1.25   xtraeme 		}
    523   1.6  explorer 
    524  1.25   xtraeme 		/* iterate over the dictionary returned by the kernel */
    525  1.25   xtraeme 		while ((obj = prop_object_iterator_next(iter)) != NULL) {
    526   1.1      groo 
    527  1.25   xtraeme 			array = prop_dictionary_get_keysym(dict, obj);
    528  1.25   xtraeme 			if (prop_object_type(array) != PROP_TYPE_ARRAY) {
    529  1.25   xtraeme 				warnx("no sensors found");
    530  1.25   xtraeme 				rval = EINVAL;
    531  1.25   xtraeme 				goto out;
    532   1.1      groo 			}
    533   1.1      groo 
    534  1.25   xtraeme 			dnp = prop_dictionary_keysym_cstring_nocopy(obj);
    535  1.25   xtraeme 
    536  1.25   xtraeme 			if (flags & ENVSYS_DFLAG) {
    537  1.25   xtraeme 				(void)printf("%s", dnp);
    538  1.25   xtraeme 			} else {
    539  1.25   xtraeme 				rval = find_sensors(array);
    540  1.25   xtraeme 				if (rval)
    541  1.25   xtraeme 					goto out;
    542   1.1      groo 			}
    543  1.25   xtraeme 			if (prop_dictionary_count(dict) > 1 || interval)
    544  1.25   xtraeme 				(void)printf("\n");
    545   1.1      groo 		}
    546  1.25   xtraeme 		prop_object_iterator_release(iter);
    547  1.25   xtraeme 		if ((flags & ENVSYS_DFLAG) && prop_dictionary_count(dict) == 1)
    548  1.25   xtraeme 			(void)printf("\n");
    549  1.25   xtraeme 	}
    550  1.25   xtraeme 
    551  1.25   xtraeme out:
    552  1.25   xtraeme 	prop_object_release(dict);
    553  1.25   xtraeme 	return rval;
    554   1.1      groo }
    555   1.1      groo 
    556  1.25   xtraeme static int
    557  1.25   xtraeme find_sensors(prop_array_t array)
    558  1.25   xtraeme {
    559  1.25   xtraeme 	prop_object_iterator_t iter;
    560  1.25   xtraeme 	prop_object_t obj, obj1;
    561  1.25   xtraeme 	prop_string_t state, desc = NULL;
    562  1.25   xtraeme 	struct envsys_sensor *esen;
    563  1.25   xtraeme 	int i, rval;
    564  1.25   xtraeme 	size_t nelems = 0;
    565  1.25   xtraeme 	char *str = NULL;
    566  1.25   xtraeme 
    567  1.25   xtraeme 	i = rval = 0;
    568  1.25   xtraeme 
    569  1.25   xtraeme 	esen = (struct envsys_sensor *)calloc(prop_array_count(array),
    570  1.25   xtraeme 	    sizeof(struct envsys_sensor));
    571  1.25   xtraeme 	if (esen == NULL)
    572  1.25   xtraeme 		return ENOMEM;
    573  1.25   xtraeme 
    574  1.25   xtraeme 	iter = prop_array_iterator(array);
    575  1.25   xtraeme 	if (iter == NULL) {
    576  1.25   xtraeme 		rval = EINVAL;
    577  1.25   xtraeme 		goto out;
    578  1.25   xtraeme 	}
    579  1.25   xtraeme 
    580  1.25   xtraeme 	/* iterate over the array of dictionaries */
    581  1.25   xtraeme 	while ((obj = prop_object_iterator_next(iter)) != NULL) {
    582  1.25   xtraeme 		/* check sensor's state */
    583  1.25   xtraeme 		state = prop_dictionary_get(obj, "state");
    584   1.1      groo 
    585  1.26   xtraeme 		/* mark invalid sensors */
    586  1.25   xtraeme 		if (prop_string_equals_cstring(state, "invalid"))
    587  1.26   xtraeme 			esen[i].invalid = true;
    588  1.25   xtraeme 
    589  1.25   xtraeme 		/* description string */
    590  1.25   xtraeme 		desc = prop_dictionary_get(obj, "description");
    591  1.25   xtraeme 		/* copy description */
    592  1.25   xtraeme 		(void)strlcpy(esen[i].desc, prop_string_cstring_nocopy(desc),
    593  1.25   xtraeme 		    sizeof(esen[i].desc));
    594  1.25   xtraeme 
    595  1.25   xtraeme 		/* type string */
    596  1.25   xtraeme 		obj1  = prop_dictionary_get(obj, "type");
    597  1.25   xtraeme 		/* copy type */
    598  1.25   xtraeme 		(void)strlcpy(esen[i].type, prop_string_cstring_nocopy(obj1),
    599  1.25   xtraeme 		    sizeof(esen[i].type));
    600  1.25   xtraeme 
    601  1.25   xtraeme 		/* get current drive state string */
    602  1.25   xtraeme 		obj1 = prop_dictionary_get(obj, "drive-state");
    603  1.25   xtraeme 		if (obj1 != NULL)
    604  1.25   xtraeme 			(void)strlcpy(esen[i].drvstate,
    605  1.25   xtraeme 			    prop_string_cstring_nocopy(obj1),
    606  1.25   xtraeme 			    sizeof(esen[i].drvstate));
    607  1.25   xtraeme 
    608  1.25   xtraeme 		/* get current value */
    609  1.25   xtraeme 		obj1 = prop_dictionary_get(obj, "cur-value");
    610  1.25   xtraeme 		esen[i].cur_value = prop_number_integer_value(obj1);
    611  1.25   xtraeme 
    612  1.25   xtraeme 		/* get max value */
    613  1.25   xtraeme 		obj1 = prop_dictionary_get(obj, "max-value");
    614  1.25   xtraeme 		if (obj1 != NULL)
    615  1.25   xtraeme 			esen[i].max_value = prop_number_integer_value(obj1);
    616  1.25   xtraeme 
    617  1.25   xtraeme 		/* get min value */
    618  1.25   xtraeme 		obj1 = prop_dictionary_get(obj, "min-value");
    619  1.25   xtraeme 		if (obj1 != NULL)
    620  1.25   xtraeme 			esen[i].min_value = prop_number_integer_value(obj1);
    621  1.25   xtraeme 
    622  1.25   xtraeme 		/* get avg value */
    623  1.25   xtraeme 		obj1 = prop_dictionary_get(obj, "avg-value");
    624  1.25   xtraeme 		if (obj1 != NULL)
    625  1.25   xtraeme 			esen[i].avg_value = prop_number_integer_value(obj1);
    626  1.25   xtraeme 
    627  1.25   xtraeme 		/* get percentage flag */
    628  1.25   xtraeme 		obj1 = prop_dictionary_get(obj, "want-percentage");
    629  1.25   xtraeme 		if (obj1 != NULL)
    630  1.25   xtraeme 			esen[i].percentage = prop_bool_true(obj1);
    631  1.25   xtraeme 
    632  1.25   xtraeme 		/* get critical max value if available */
    633  1.25   xtraeme 		obj1 = prop_dictionary_get(obj, "critical-max-limit");
    634  1.25   xtraeme 		if (obj1 != NULL) {
    635  1.25   xtraeme 			esen[i].critmax_value =
    636  1.25   xtraeme 			    prop_number_integer_value(obj1);
    637  1.25   xtraeme 		}
    638   1.5       cgd 
    639  1.25   xtraeme 		/* get critical min value if available */
    640  1.25   xtraeme 		obj1 = prop_dictionary_get(obj, "critical-min-limit");
    641  1.25   xtraeme 		if (obj1 != NULL) {
    642  1.25   xtraeme 			esen[i].critmin_value =
    643  1.25   xtraeme 			    prop_number_integer_value(obj1);
    644  1.25   xtraeme 		}
    645   1.1      groo 
    646  1.25   xtraeme 		/* get critical capacity value if available */
    647  1.25   xtraeme 		obj1 = prop_dictionary_get(obj, "critical-capacity");
    648  1.25   xtraeme 		if (obj1 != NULL) {
    649  1.25   xtraeme 			esen[i].critcap_value =
    650  1.25   xtraeme 			    prop_number_integer_value(obj1);
    651  1.25   xtraeme 		}
    652   1.1      groo 
    653  1.25   xtraeme 		/* pass to the next struct */
    654  1.25   xtraeme 		i++;
    655  1.25   xtraeme 		nelems++;
    656  1.25   xtraeme 
    657  1.25   xtraeme 		/* print sensor names if -l was given */
    658  1.25   xtraeme 		if (flags & ENVSYS_LFLAG) {
    659  1.25   xtraeme 			if (width)
    660  1.25   xtraeme 				(void)printf("%*s\n", width,
    661  1.25   xtraeme 				    prop_string_cstring_nocopy(desc));
    662  1.25   xtraeme 			else
    663  1.25   xtraeme 				(void)printf("%s\n",
    664  1.25   xtraeme 				    prop_string_cstring_nocopy(desc));
    665  1.25   xtraeme 		}
    666  1.25   xtraeme 	}
    667   1.1      groo 
    668  1.25   xtraeme 	/* free memory */
    669  1.25   xtraeme 	prop_object_iterator_release(iter);
    670   1.1      groo 
    671  1.25   xtraeme 	/*
    672  1.25   xtraeme 	 * if -s was specified, we need a way to mark if a sensor
    673  1.25   xtraeme 	 * was found.
    674  1.25   xtraeme 	 */
    675  1.25   xtraeme 	if (sensors) {
    676  1.25   xtraeme 		str = strdup(sensors);
    677  1.25   xtraeme 		if (str == NULL)
    678  1.25   xtraeme 			return ENOMEM;
    679   1.6  explorer 
    680  1.25   xtraeme 		rval = check_sensors(esen, str, nelems);
    681  1.25   xtraeme 		if (rval)
    682  1.25   xtraeme 			goto out;
    683   1.1      groo 
    684  1.25   xtraeme 		free(str);
    685  1.25   xtraeme 	}
    686   1.1      groo 
    687  1.25   xtraeme 	gesen = esen;
    688  1.25   xtraeme 	gnelems = nelems;
    689   1.1      groo 
    690  1.25   xtraeme 	if (userreq == NULL) {
    691  1.25   xtraeme 		if ((flags & ENVSYS_LFLAG) == 0)
    692  1.25   xtraeme 			print_sensors(esen, nelems);
    693   1.1      groo 	}
    694   1.1      groo 
    695  1.25   xtraeme out:
    696  1.25   xtraeme 	free(esen);
    697  1.25   xtraeme 	return rval;
    698   1.1      groo }
    699   1.1      groo 
    700  1.25   xtraeme static int
    701  1.25   xtraeme check_sensors(struct envsys_sensor *es, char *str, size_t nelems)
    702   1.1      groo {
    703   1.1      groo 	int i;
    704  1.25   xtraeme 	char *sname;
    705  1.25   xtraeme 
    706  1.25   xtraeme 	sname = strtok(str, ",");
    707  1.25   xtraeme 	while (sname != NULL) {
    708  1.25   xtraeme 		for (i = 0; i < nelems; i++) {
    709  1.25   xtraeme 			if (strcmp(sname, es[i].desc) == 0) {
    710  1.25   xtraeme 				es[i].visible = true;
    711  1.25   xtraeme 				break;
    712  1.25   xtraeme 			}
    713  1.25   xtraeme 		}
    714  1.25   xtraeme 		if (i >= nelems) {
    715  1.25   xtraeme 			if (mydevname) {
    716  1.25   xtraeme 				warnx("unknown sensor `%s' for device `%s'",
    717  1.25   xtraeme 				    sname, mydevname);
    718  1.25   xtraeme 				return EINVAL;
    719  1.25   xtraeme 			} else {
    720  1.25   xtraeme 				warnx("unknown sensor `%s'", sname);
    721  1.25   xtraeme 				return EINVAL;
    722  1.25   xtraeme 			}
    723  1.25   xtraeme 		}
    724  1.25   xtraeme 		sname = strtok(NULL, ",");
    725  1.25   xtraeme 	}
    726   1.1      groo 
    727  1.25   xtraeme 	/* check if all sensors were ok, and error out if not */
    728  1.25   xtraeme 	for (i = 0; i < nelems; i++) {
    729  1.25   xtraeme 		if (es[i].visible)
    730  1.25   xtraeme 			return 0;
    731   1.1      groo 	}
    732  1.25   xtraeme 
    733  1.25   xtraeme 	warnx("no sensors selected to display");
    734  1.25   xtraeme 	return EINVAL;
    735   1.1      groo }
    736   1.1      groo 
    737  1.25   xtraeme static void
    738  1.25   xtraeme print_sensors(struct envsys_sensor *es, size_t nelems)
    739   1.1      groo {
    740  1.25   xtraeme 	size_t maxlen = 0;
    741  1.25   xtraeme 	double temp = 0;
    742  1.27   xtraeme 	const char *invalid = "N/A";
    743  1.25   xtraeme 	const char *degrees = NULL;
    744  1.25   xtraeme 	int i;
    745   1.1      groo 
    746  1.25   xtraeme 	/* find the longest description */
    747  1.25   xtraeme 	for (i = 0; i < nelems; ++i) {
    748  1.25   xtraeme 		if (strlen(es[i].desc) > maxlen)
    749  1.25   xtraeme 			maxlen = strlen(es[i].desc);
    750  1.25   xtraeme 	}
    751   1.1      groo 
    752  1.25   xtraeme 	if (width)
    753  1.25   xtraeme 		maxlen = width;
    754   1.1      groo 
    755  1.25   xtraeme 	/* print the sensors */
    756  1.25   xtraeme 	for (i = 0; i < nelems; ++i) {
    757   1.1      groo 
    758  1.25   xtraeme 		/* skip sensors that were not marked as visible */
    759  1.25   xtraeme 		if (sensors && !es[i].visible)
    760  1.25   xtraeme 			continue;
    761   1.1      groo 
    762  1.25   xtraeme 		/* we have a winner... */
    763  1.25   xtraeme 		(void)printf("%*.*s", (int)maxlen, (int)maxlen, es[i].desc);
    764   1.1      groo 
    765  1.29   xtraeme 		if (es[i].invalid) {
    766  1.29   xtraeme 			(void)printf(": %10s\n", invalid);
    767  1.29   xtraeme 			continue;
    768  1.29   xtraeme 		}
    769  1.29   xtraeme 
    770  1.25   xtraeme 		if (strcmp(es[i].type, "Indicator") == 0) {
    771  1.29   xtraeme 
    772  1.29   xtraeme 			(void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");
    773   1.1      groo 
    774  1.25   xtraeme /* converts the value to degC or degF */
    775  1.25   xtraeme #define CONVERTTEMP(a, b, c)					\
    776  1.25   xtraeme do {								\
    777  1.25   xtraeme 	(a) = ((b) / 1000000.0) - 273.15;			\
    778  1.25   xtraeme 	if (flags & ENVSYS_FFLAG) {				\
    779  1.25   xtraeme 		(a) = (9.0 / 5.0) * (a) + 32.0;			\
    780  1.25   xtraeme 		(c) = "degF";					\
    781  1.25   xtraeme 	} else							\
    782  1.25   xtraeme 		(c) = "degC";					\
    783  1.25   xtraeme } while (/* CONSTCOND */ 0)
    784  1.25   xtraeme 
    785  1.25   xtraeme 
    786  1.25   xtraeme 		/* temperatures */
    787  1.27   xtraeme 		} else if (strcmp(es[i].type, "Temperature") == 0) {
    788  1.25   xtraeme 
    789  1.25   xtraeme 			CONVERTTEMP(temp, es[i].cur_value, degrees);
    790  1.29   xtraeme 			(void)printf(": %10.3f %s", temp, degrees);
    791  1.25   xtraeme 
    792  1.25   xtraeme 			if (es[i].critmax_value || es[i].critmin_value)
    793  1.25   xtraeme 				(void)printf("  ");
    794  1.25   xtraeme 
    795  1.25   xtraeme 			if (es[i].critmax_value) {
    796  1.25   xtraeme 				CONVERTTEMP(temp, es[i].critmax_value, degrees);
    797  1.25   xtraeme 				(void)printf("max: %8.3f %s  ", temp, degrees);
    798  1.25   xtraeme 			}
    799  1.25   xtraeme 
    800  1.25   xtraeme 			if (es[i].critmin_value) {
    801  1.25   xtraeme 				CONVERTTEMP(temp, es[i].critmin_value, degrees);
    802  1.25   xtraeme 				(void)printf("min: %8.3f %s", temp, degrees);
    803  1.25   xtraeme 			}
    804  1.25   xtraeme 
    805  1.25   xtraeme 		/* fans */
    806  1.25   xtraeme 		} else if (strcmp(es[i].type, "Fan") == 0) {
    807  1.25   xtraeme 
    808  1.29   xtraeme 			(void)printf(": %10u RPM", es[i].cur_value);
    809  1.25   xtraeme 
    810  1.25   xtraeme 			if (es[i].critmax_value || es[i].critmin_value)
    811  1.25   xtraeme 				(void)printf("   ");
    812  1.25   xtraeme 			if (es[i].critmax_value)
    813  1.25   xtraeme 				(void)printf("max: %8u RPM   ",
    814  1.25   xtraeme 				    es[i].critmax_value);
    815  1.25   xtraeme 			if (es[i].critmin_value)
    816  1.25   xtraeme 				(void)printf("min: %8u RPM",
    817  1.25   xtraeme 				    es[i].critmin_value);
    818  1.25   xtraeme 
    819  1.25   xtraeme 		/* integers */
    820  1.25   xtraeme 		} else if (strcmp(es[i].type, "Integer") == 0) {
    821  1.25   xtraeme 
    822  1.29   xtraeme 			(void)printf(": %10d", es[i].cur_value);
    823  1.25   xtraeme 
    824  1.25   xtraeme 		/* drives */
    825  1.25   xtraeme 		} else if (strcmp(es[i].type, "Drive") == 0) {
    826  1.27   xtraeme 
    827  1.29   xtraeme 			(void)printf(": %s", es[i].drvstate);
    828  1.25   xtraeme 
    829  1.25   xtraeme 		/* everything else */
    830  1.25   xtraeme 		} else {
    831  1.25   xtraeme 			const char *type;
    832  1.25   xtraeme 
    833  1.25   xtraeme 			if (strcmp(es[i].type, "Voltage DC") == 0)
    834  1.25   xtraeme 				type = "V";
    835  1.25   xtraeme 			else if (strcmp(es[i].type, "Voltage AC") == 0)
    836  1.25   xtraeme 				type = "VAC";
    837  1.25   xtraeme 			else if (strcmp(es[i].type, "Ampere") == 0)
    838  1.25   xtraeme 				type = "A";
    839  1.25   xtraeme 			else if (strcmp(es[i].type, "Watts") == 0)
    840  1.25   xtraeme 				type = "W";
    841  1.25   xtraeme 			else if (strcmp(es[i].type, "Ohms") == 0)
    842  1.25   xtraeme 				type = "Ohms";
    843  1.25   xtraeme 			else if (strcmp(es[i].type, "Watt hour") == 0)
    844  1.25   xtraeme 				type = "Wh";
    845  1.25   xtraeme 			else if (strcmp(es[i].type, "Ampere hour") == 0)
    846  1.25   xtraeme 				type = "Ah";
    847  1.25   xtraeme 			else
    848  1.25   xtraeme 				type = NULL;
    849   1.1      groo 
    850  1.29   xtraeme 			(void)printf(": %10.3f %s",
    851  1.29   xtraeme 			    es[i].cur_value / 1000000.0, type);
    852  1.29   xtraeme 
    853  1.29   xtraeme 			if (es[i].percentage && es[i].max_value) {
    854  1.29   xtraeme 				(void)printf(" (%5.2f%%)",
    855  1.29   xtraeme 				    (es[i].cur_value * 100.0) /
    856  1.29   xtraeme 				    es[i].max_value);
    857  1.25   xtraeme 			}
    858  1.25   xtraeme 
    859  1.25   xtraeme 			if (es[i].critcap_value) {
    860  1.25   xtraeme 				(void)printf(" critical (%5.2f%%)",
    861  1.25   xtraeme 				    (es[i].critcap_value * 100.0) /
    862  1.25   xtraeme 				    es[i].max_value);
    863  1.25   xtraeme 			}
    864   1.6  explorer 
    865  1.25   xtraeme 			if (es[i].critmax_value || es[i].critmin_value)
    866  1.25   xtraeme 				(void)printf("     ");
    867  1.25   xtraeme 			if (es[i].critmax_value)
    868  1.25   xtraeme 				(void)printf("max: %8.3f %s     ",
    869  1.25   xtraeme 				    es[i].critmax_value / 1000000.0,
    870  1.25   xtraeme 				    type);
    871  1.25   xtraeme 			if (es[i].critmin_value)
    872  1.25   xtraeme 				(void)printf("min: %8.3f %s",
    873  1.25   xtraeme 				    es[i].critmin_value / 1000000.0,
    874  1.25   xtraeme 				    type);
    875   1.1      groo 
    876  1.25   xtraeme 		}
    877   1.1      groo 
    878  1.25   xtraeme 		(void)printf("\n");
    879   1.1      groo 	}
    880  1.25   xtraeme }
    881   1.1      groo 
    882  1.25   xtraeme static int
    883  1.25   xtraeme usage(void)
    884  1.25   xtraeme {
    885  1.25   xtraeme 	(void)fprintf(stderr, "Usage: %s [-Dflx] ", getprogname());
    886  1.25   xtraeme 	(void)fprintf(stderr, "[-m ...] [-s s1,s2 ] [-w num] ");
    887  1.25   xtraeme 	(void)fprintf(stderr, "[-i num] [-d ...]\n");
    888  1.25   xtraeme 	exit(EXIT_FAILURE);
    889  1.25   xtraeme 	/* NOTREACHED */
    890   1.1      groo }
    891