Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/envstat Add option -n to print only the value of a ...



details:   https://anonhg.NetBSD.org/src/rev/0518e382b7b2
branches:  trunk
changeset: 957021:0518e382b7b2
user:      mlelstv <mlelstv%NetBSD.org@localhost>
date:      Sat Nov 14 16:32:53 2020 +0000

description:
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.

diffstat:

 usr.sbin/envstat/envstat.8 |    9 +-
 usr.sbin/envstat/envstat.c |  198 +++++++++++++++++++++++++-------------------
 2 files changed, 118 insertions(+), 89 deletions(-)

diffs (truncated from 493 to 300 lines):

diff -r 048cdcce895e -r 0518e382b7b2 usr.sbin/envstat/envstat.8
--- a/usr.sbin/envstat/envstat.8        Sat Nov 14 16:09:08 2020 +0000
+++ b/usr.sbin/envstat/envstat.8        Sat Nov 14 16:32:53 2020 +0000
@@ -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 @@
 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.
diff -r 048cdcce895e -r 0518e382b7b2 usr.sbin/envstat/envstat.c
--- a/usr.sbin/envstat/envstat.c        Sat Nov 14 16:09:08 2020 +0000
+++ b/usr.sbin/envstat/envstat.c        Sat Nov 14 16:32:53 2020 +0000
@@ -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 @@
 #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 @@
 
        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 @@
                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 @@
                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 @@
        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 @@
                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 @@
                        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 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 @@
                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 @@
                        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 @@
                /* 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 @@
                                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 @@
 
                        }
 
-                       (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 @@
 #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 @@
 /* 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) {



Home | Main Index | Thread Index | Old Index