Module Name:    src
Committed By:   mlelstv
Date:           Sat Nov 14 16:32:53 UTC 2020

Modified Files:
        src/usr.sbin/envstat: envstat.8 envstat.c

Log Message:
Add option -n to print only the value of a sensor.
If statistics are selected with -T, then also display max, min and average.
The empty line between each display is skipped when one or more sensors are
selected with -s.


To generate a diff of this commit:
cvs rdiff -u -r1.64 -r1.65 src/usr.sbin/envstat/envstat.8
cvs rdiff -u -r1.99 -r1.100 src/usr.sbin/envstat/envstat.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/envstat/envstat.8
diff -u src/usr.sbin/envstat/envstat.8:1.64 src/usr.sbin/envstat/envstat.8:1.65
--- src/usr.sbin/envstat/envstat.8:1.64	Sat Nov 14 12:36:49 2020
+++ src/usr.sbin/envstat/envstat.8	Sat Nov 14 16:32:53 2020
@@ -1,4 +1,4 @@
-.\"	$NetBSD: envstat.8,v 1.64 2020/11/14 12:36:49 mlelstv Exp $
+.\"	$NetBSD: envstat.8,v 1.65 2020/11/14 16:32:53 mlelstv Exp $
 .\"
 .\" Copyright (c) 2000, 2007, 2008, 2009, 2014 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -35,7 +35,7 @@
 .Nd utility to handle environmental sensors
 .Sh SYNOPSIS
 .Nm
-.Op Fl DfIklSTW
+.Op Fl DfIklnSTW
 .Op Fl c Ar file
 .Op Fl d Ar device
 .Op Fl i Ar interval
@@ -106,6 +106,11 @@ List the names of all supported sensors,
 Use of this flag causes
 .Nm
 to ignore all other option flags.
+.It Fl n
+Print only the current value of a sensor and no headers.
+If statistics are selected with
+.Fl T
+then also display max, min and average.
 .It Fl r
 This flag is provided for compatibility reasons and there's no need
 to use it.

Index: src/usr.sbin/envstat/envstat.c
diff -u src/usr.sbin/envstat/envstat.c:1.99 src/usr.sbin/envstat/envstat.c:1.100
--- src/usr.sbin/envstat/envstat.c:1.99	Sat Nov 14 12:36:49 2020
+++ src/usr.sbin/envstat/envstat.c	Sat Nov 14 16:32:53 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: envstat.c,v 1.99 2020/11/14 12:36:49 mlelstv Exp $ */
+/* $NetBSD: envstat.c,v 1.100 2020/11/14 16:32:53 mlelstv Exp $ */
 
 /*-
  * Copyright (c) 2007, 2008 Juan Romero Pardines.
@@ -27,7 +27,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: envstat.c,v 1.99 2020/11/14 12:36:49 mlelstv Exp $");
+__RCSID("$NetBSD: envstat.c,v 1.100 2020/11/14 16:32:53 mlelstv Exp $");
 #endif /* not lint */
 
 #include <stdio.h>
@@ -58,6 +58,7 @@ __RCSID("$NetBSD: envstat.c,v 1.99 2020/
 #define ENVSYS_IFLAG 	0x00000010	/* skip invalid sensors */
 #define ENVSYS_SFLAG	0x00000020	/* remove all properties set */
 #define ENVSYS_TFLAG	0x00000040	/* make statistics */
+#define ENVSYS_NFLAG	0x00000080	/* print value only */
 #define ENVSYS_KFLAG	0x00000100	/* show temp in kelvin */
 
 /* Sensors */
@@ -130,7 +131,7 @@ int main(int argc, char **argv)
 
 	setprogname(argv[0]);
 
-	while ((c = getopt(argc, argv, "c:Dd:fIi:klrSs:Tw:Wx")) != -1) {
+	while ((c = getopt(argc, argv, "c:Dd:fIi:klnrSs:Tw:Wx")) != -1) {
 		switch (c) {
 		case 'c':	/* configuration file */
 			configfile = optarg;
@@ -158,6 +159,9 @@ int main(int argc, char **argv)
 		case 'l':	/* list sensors */
 			flags |= ENVSYS_LFLAG;
 			break;
+		case 'n':	/* print value only */
+			flags |= ENVSYS_NFLAG;
+			break;
 		case 'r':
 			/*
 			 * This flag is noop.. it's only here for
@@ -577,7 +581,7 @@ parse_dictionary(int fd)
 		rval = check_sensors(sensors);
 	if ((flags & ENVSYS_LFLAG) == 0 && (flags & ENVSYS_DFLAG) == 0)
 		print_sensors();
-	if (interval)
+	if (interval && ((flags & ENVSYS_NFLAG) == 0 || sensors == NULL))
 		(void)printf("\n");
 
 out:
@@ -856,6 +860,9 @@ print_sensors(void)
 	double temp = 0;
 	const char *invalid = "N/A", *degrees, *tmpstr, *stype;
 	const char *a, *b, *c, *d, *e, *units;
+	const char *sep;
+	int flen;
+	bool nflag = (flags & ENVSYS_NFLAG) != 0;
 
 	tmpstr = stype = d = e = NULL;
 
@@ -888,21 +895,29 @@ print_sensors(void)
 		e = "CritMin";
 	}
 
-	if (!sensors || (!header_passes && sensors) ||
-	    (header_passes == 10 && sensors)) {
-		if (statistics)
-			(void)printf("%s%*s  %9s %8s %8s %8s %6s\n",
-			    mydevname ? "" : "  ", (int)maxlen,
-			    "", a, b, c, d, units);
-		else
-			(void)printf("%s%*s  %9s %8s %8s %8s %8s %5s\n",
-			    mydevname ? "" : "  ", (int)maxlen,
-			    "", a, b, c, d, e, units);
-		if (sensors && header_passes == 10)
-			header_passes = 0;
+	if (!nflag) {
+		if (!sensors || (!header_passes && sensors) ||
+		    (header_passes == 10 && sensors)) {
+			if (statistics)
+				(void)printf("%s%*s  %9s %8s %8s %8s %6s\n",
+				    mydevname ? "" : "  ", (int)maxlen,
+				    "", a, b, c, d, units);
+			else
+				(void)printf("%s%*s  %9s %8s %8s %8s %8s %5s\n",
+				    mydevname ? "" : "  ", (int)maxlen,
+				    "", a, b, c, d, e, units);
+			if (sensors && header_passes == 10)
+				header_passes = 0;
+		}
+		if (sensors)
+			header_passes++;
+
+		sep = ":";
+		flen = 10;
+	} else {
+		sep = "";
+		flen = 1;
 	}
-	if (sensors)
-		header_passes++;
 
 	/* print the sensors */
 	SIMPLEQ_FOREACH(sensor, &sensors_list, entries) {
@@ -915,7 +930,7 @@ print_sensors(void)
 			continue;
 
 		/* print device name */
-		if (!mydevname) {
+		if (!nflag && !mydevname) {
 			if (tmpstr == NULL || strcmp(tmpstr, sensor->dvname))
 				printf("[%s]\n", sensor->dvname);
 
@@ -931,13 +946,16 @@ print_sensors(void)
 			}
 		}
 
-		/* print sensor description */
-		(void)printf("%s%*.*s", mydevname ? "" : "  ", (int)maxlen,
-		    (int)maxlen, sensor->desc);
+		if (!nflag) {
+			/* print sensor description */
+			(void)printf("%s%*.*s", mydevname ? "" : "  ",
+			    (int)maxlen,
+			    (int)maxlen, sensor->desc);
+		}
 
 		/* print invalid string */
 		if (sensor->invalid) {
-			(void)printf(": %9s\n", invalid);
+			(void)printf("%s%*s\n", sep, flen, invalid);
 			continue;
 		}
 
@@ -947,7 +965,8 @@ print_sensors(void)
 		if ((strcmp(sensor->type, "Indicator") == 0) ||
 		    (strcmp(sensor->type, "Battery charge") == 0)) {
 
-			(void)printf(":%10s", sensor->cur_value ? "TRUE" : "FALSE");
+			(void)printf("%s%*s", sep, flen,
+			     sensor->cur_value ? "TRUE" : "FALSE");
 
 /* convert and print a temp value in degC, degF, or Kelvin */
 #define PRINTTEMP(a)						\
@@ -963,8 +982,8 @@ do {								\
 			temp = temp - 273.15;			\
 			degrees = "degC";			\
 		}						\
-		(void)printf("%*.3f ", (int)ilen, temp);	\
-		ilen = 8;					\
+		(void)printf("%*.3f", (int)ilen, temp);	\
+		ilen = 9;					\
 	} else							\
 		ilen += 9;					\
 } while (/* CONSTCOND */ 0)
@@ -972,9 +991,9 @@ do {								\
 		/* temperatures */
 		} else if (strcmp(sensor->type, "Temperature") == 0) {
 
-			ilen = 10;
+			ilen = nflag ? 1 : 10;
 			degrees = "";
-			(void)printf(":");
+			(void)printf("%s",sep);
 			PRINTTEMP(sensor->cur_value);
 			stype = degrees;
 
@@ -984,51 +1003,52 @@ do {								\
 				PRINTTEMP(stats->min);
 				PRINTTEMP(stats->avg);
 				ilen += 2;
-			} else {
+			} else if (!nflag) {
 				PRINTTEMP(sensor->critmax_value);
 				PRINTTEMP(sensor->warnmax_value);
 				PRINTTEMP(sensor->warnmin_value);
 				PRINTTEMP(sensor->critmin_value);
 			}
-			(void)printf("%*s", (int)ilen - 3, stype);
+			if (!nflag)
+				(void)printf("%*s", (int)ilen - 3, stype);
 #undef PRINTTEMP
 
 		/* fans */
 		} else if (strcmp(sensor->type, "Fan") == 0) {
 			stype = "RPM";
 
-			(void)printf(":%10u ", sensor->cur_value);
+			(void)printf("%s%*u", sep, flen, sensor->cur_value);
 
 			ilen = 8;
 			if (statistics) {
 				/* show statistics if flag set */
-				(void)printf("%8u %8u %8u ",
+				(void)printf(" %8u %8u %8u",
 				    stats->max, stats->min, stats->avg);
 				ilen += 2;
-			} else {
+			} else if (!nflag) {
 				if (sensor->critmax_value) {
-					(void)printf("%*u ", (int)ilen,
+					(void)printf(" %*u", (int)ilen,
 					    sensor->critmax_value);
 					ilen = 8;
 				} else
 					ilen += 9;
 
 				if (sensor->warnmax_value) {
-					(void)printf("%*u ", (int)ilen,
+					(void)printf(" %*u", (int)ilen,
 					    sensor->warnmax_value);
 					ilen = 8;
 				} else
 					ilen += 9;
 
 				if (sensor->warnmin_value) {
-					(void)printf("%*u ", (int)ilen,
+					(void)printf(" %*u", (int)ilen,
 					    sensor->warnmin_value);
 					ilen = 8;
 				} else
 					ilen += 9;
 
 				if (sensor->critmin_value) {
-					(void)printf( "%*u ", (int)ilen,
+					(void)printf( " %*u", (int)ilen,
 					    sensor->critmin_value);
 					ilen = 8;
 				} else
@@ -1036,14 +1056,15 @@ do {								\
 
 			}
 
-			(void)printf("%*s", (int)ilen - 3, stype);
+			if (!nflag)
+				(void)printf(" %*s", (int)ilen - 3, stype);
 
 		/* integers */
 		} else if (strcmp(sensor->type, "Integer") == 0) {
 
 			stype = "none";
 
-			(void)printf(":%10d ", sensor->cur_value);
+			(void)printf("%s%*d", sep, flen, sensor->cur_value);
 
 			ilen = 8;
 
@@ -1051,7 +1072,7 @@ do {								\
 #define PRINTPCT(a)							\
 do {									\
 	if (sensor->max_value) {					\
-		(void)printf("%*.3f%%", (int)ilen,			\
+		(void)printf(" %*.3f%%", (int)ilen,			\
 			((a) * 100.0) / sensor->max_value);		\
 		ilen = 8;						\
 	} else								\
@@ -1061,11 +1082,22 @@ do {									\
 /* Print an integer sensor value */
 #define PRINTINT(a)							\
 do {									\
-	(void)printf("%*u ", (int)ilen, (a));				\
+	(void)printf(" %*u", (int)ilen, (a));				\
 	ilen = 8;							\
 } while ( /* CONSTCOND*/ 0 )
 
-			if (!statistics) {
+			if (statistics) {
+				if (sensor->percentage) {
+					PRINTPCT(stats->max);
+					PRINTPCT(stats->min);
+					PRINTPCT(stats->avg);
+				} else {
+					PRINTINT(stats->max);
+					PRINTINT(stats->min);
+					PRINTINT(stats->avg);
+				}
+				ilen += 2;
+			} else if (!nflag) {
 				if (sensor->percentage) {
 					PRINTPCT(sensor->critmax_value);
 					PRINTPCT(sensor->warnmax_value);
@@ -1077,20 +1109,10 @@ do {									\
 					PRINTINT(sensor->warnmin_value);
 					PRINTINT(sensor->critmin_value);
 				}
-			} else {
-				if (sensor->percentage) {
-					PRINTPCT(stats->max);
-					PRINTPCT(stats->min);
-					PRINTPCT(stats->avg);
-				} else {
-					PRINTINT(stats->max);
-					PRINTINT(stats->min);
-					PRINTINT(stats->avg);
-				}
-				ilen += 2;
 			}
 
-			(void)printf("%*s", (int)ilen - 3, stype);
+			if (!nflag)
+				(void)printf("%*s", (int)ilen - 3, stype);
 
 #undef PRINTINT
 #undef PRINTPCT
@@ -1098,50 +1120,50 @@ do {									\
 		/* drives  */
 		} else if (strcmp(sensor->type, "Drive") == 0) {
 
-			(void)printf(":%10s", sensor->drvstate);
+			(void)printf("%s%*s", sep, flen, sensor->drvstate);
 
 		/* Battery capacity */
 		} else if (strcmp(sensor->type, "Battery capacity") == 0) {
 
-			(void)printf(":%10s", sensor->battcap);
+			(void)printf("%s%*s", sep, flen, sensor->battcap);
 
 		/* Illuminance */
 		} else if (strcmp(sensor->type, "Illuminance") == 0) {
 
 			stype = "lux";
 
-			(void)printf(":%10u ", sensor->cur_value);
+			(void)printf("%s%*u", sep, flen, sensor->cur_value);
 
 			ilen = 8;
 			if (statistics) {
 				/* show statistics if flag set */
-				(void)printf("%8u %8u %8u ",
+				(void)printf(" %8u %8u %8u",
 				    stats->max, stats->min, stats->avg);
 				ilen += 2;
-			} else {
+			} else if (!nflag) {
 				if (sensor->critmax_value) {
-					(void)printf("%*u ", (int)ilen,
+					(void)printf(" %*u", (int)ilen,
 					    sensor->critmax_value);
 					ilen = 8;
 				} else
 					ilen += 9;
 
 				if (sensor->warnmax_value) {
-					(void)printf("%*u ", (int)ilen,
+					(void)printf(" %*u", (int)ilen,
 					    sensor->warnmax_value);
 					ilen = 8;
 				} else
 					ilen += 9;
 
 				if (sensor->warnmin_value) {
-					(void)printf("%*u ", (int)ilen,
+					(void)printf(" %*u", (int)ilen,
 					    sensor->warnmin_value);
 					ilen = 8;
 				} else
 					ilen += 9;
 
 				if (sensor->critmin_value) {
-					(void)printf( "%*u ", (int)ilen,
+					(void)printf( " %*u", (int)ilen,
 					    sensor->critmin_value);
 					ilen = 8;
 				} else
@@ -1149,7 +1171,8 @@ do {									\
 
 			}
 
-			(void)printf("%*s", (int)ilen - 3, stype);
+			if (!nflag)
+				(void)printf(" %*s", (int)ilen - 3, stype);
 
 		/* everything else */
 		} else {
@@ -1172,10 +1195,10 @@ do {									\
 			else
 				stype = "?";
 
-			(void)printf(":%10.3f ",
+			(void)printf("%s%*.3f", sep, flen,
 			    sensor->cur_value / 1000000.0);
 
-			ilen = 8;
+			ilen = 9;
 
 /* Print percentage of max_value */
 #define PRINTPCT(a)							\
@@ -1192,13 +1215,24 @@ do {									\
 #define PRINTVAL(a)							\
 do {									\
 	if ((a)) {							\
-		(void)printf("%*.3f ", (int)ilen, (a) / 1000000.0);	\
-		ilen = 8;						\
+		(void)printf("%*.3f", (int)ilen, (a) / 1000000.0);	\
+		ilen = 9;						\
 	} else								\
 		ilen += 9;						\
 } while ( /* CONSTCOND*/ 0 )
 
-			if (!statistics) {
+			if (statistics) {
+				if (sensor->percentage) {
+					PRINTPCT(stats->max);
+					PRINTPCT(stats->min);
+					PRINTPCT(stats->avg);
+				} else {
+					PRINTVAL(stats->max);
+					PRINTVAL(stats->min);
+					PRINTVAL(stats->avg);
+				}
+				ilen += 2;
+			} else if (!nflag) {
 				if (sensor->percentage) {
 					PRINTPCT(sensor->critmax_value);
 					PRINTPCT(sensor->warnmax_value);
@@ -1211,27 +1245,17 @@ do {									\
 					PRINTVAL(sensor->warnmin_value);
 					PRINTVAL(sensor->critmin_value);
 				}
-			} else {
-				if (sensor->percentage) {
-					PRINTPCT(stats->max);
-					PRINTPCT(stats->min);
-					PRINTPCT(stats->avg);
-				} else {
-					PRINTVAL(stats->max);
-					PRINTVAL(stats->min);
-					PRINTVAL(stats->avg);
-				}
-				ilen += 2;
 			}
 #undef PRINTPCT
 #undef PRINTVAL
 
-			(void)printf("%*s", (int)ilen - 3, stype);
-
-			if (sensor->percentage && sensor->max_value) {
-				(void)printf(" (%5.2f%%)",
-				    (sensor->cur_value * 100.0) /
-				    sensor->max_value);
+			if (!nflag) {
+				(void)printf(" %*s", (int)ilen - 4, stype);
+				if (sensor->percentage && sensor->max_value) {
+					(void)printf(" (%5.2f%%)",
+					    (sensor->cur_value * 100.0) /
+					    sensor->max_value);
+				}
 			}
 		}
 		(void)printf("\n");
@@ -1241,7 +1265,7 @@ do {									\
 static int
 usage(void)
 {
-	(void)fprintf(stderr, "Usage: %s [-DfIklrST] ", getprogname());
+	(void)fprintf(stderr, "Usage: %s [-DfIklnrST] ", getprogname());
 	(void)fprintf(stderr, "[-c file] [-d device] [-i interval] ");
 	(void)fprintf(stderr, "[-s device:sensor,...] [-w width]\n");
 	(void)fprintf(stderr, "       %s ", getprogname());

Reply via email to