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