Module Name:    src
Committed By:   mlelstv
Date:           Sat Nov 14 09:11:56 UTC 2020

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

Log Message:
For raw output (-x) allow to extract individual properties like drvctl -p.
E.g.

# envstat -x /vcmbox0/0/cur-value
328150000


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/usr.sbin/envstat/config.c
cvs rdiff -u -r1.62 -r1.63 src/usr.sbin/envstat/envstat.8
cvs rdiff -u -r1.97 -r1.98 src/usr.sbin/envstat/envstat.c
cvs rdiff -u -r1.3 -r1.4 src/usr.sbin/envstat/envstat.h

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/config.c
diff -u src/usr.sbin/envstat/config.c:1.13 src/usr.sbin/envstat/config.c:1.14
--- src/usr.sbin/envstat/config.c:1.13	Sun Jun  7 00:51:48 2020
+++ src/usr.sbin/envstat/config.c	Sat Nov 14 09:11:55 2020
@@ -1,4 +1,4 @@
-/* 	$NetBSD: config.c,v 1.13 2020/06/07 00:51:48 thorpej Exp $	*/
+/* 	$NetBSD: config.c,v 1.14 2020/11/14 09:11:55 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2007 Juan Romero Pardines.
@@ -27,9 +27,11 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: config.c,v 1.13 2020/06/07 00:51:48 thorpej Exp $");
+__RCSID("$NetBSD: config.c,v 1.14 2020/11/14 09:11:55 mlelstv Exp $");
 #endif /* not lint */
 
+#include <inttypes.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -134,7 +136,7 @@ config_dict_mark(void)
 }
 
 /*
- * Only used for debugging purposses.
+ * Show raw data
  */
 void
 config_dict_dump(prop_dictionary_t d)
@@ -146,6 +148,81 @@ config_dict_dump(prop_dictionary_t d)
 	free(buf);
 }
 
+static void
+display_object(prop_object_t obj, bool nflag)
+{
+	char *xml;
+	prop_object_t next_obj;
+	prop_object_iterator_t iter;
+
+	if (obj == NULL)
+		exit(EXIT_FAILURE);
+	switch (prop_object_type(obj)) {
+	case PROP_TYPE_BOOL:
+		printf("%s\n", prop_bool_true(obj) ? "true" : "false");
+		break;
+	case PROP_TYPE_NUMBER:
+		printf("%" PRId64 "\n", prop_number_signed_value(obj));
+		break;
+	case PROP_TYPE_STRING:
+		printf("%s\n", prop_string_value(obj));
+		break;
+	case PROP_TYPE_DICTIONARY:
+		xml = prop_dictionary_externalize(obj);
+		printf("%s", xml);
+		free(xml);
+		break;
+	case PROP_TYPE_ARRAY:
+		iter = prop_array_iterator(obj);
+		if (!nflag)
+			printf("Array:\n");
+		while ((next_obj = prop_object_iterator_next(iter)) != NULL)
+			display_object(next_obj, nflag);
+		break;
+	default:
+		errx(EXIT_FAILURE, "Unhandled type %d", prop_object_type(obj));
+	}
+}
+
+void
+config_dict_extract(prop_dictionary_t dict, const char *prop, bool nflag)
+{
+	char *s, *p, *cur, *ep = NULL;
+	prop_object_t obj;
+	unsigned long ind;
+
+	obj = dict;
+	cur = NULL;
+	s = strdup(prop);
+	p = strtok_r(s, "/", &ep);
+	while (p) {
+		cur = p;
+		p = strtok_r(NULL, "/", &ep);
+
+		switch (prop_object_type(obj)) {
+		case PROP_TYPE_DICTIONARY:
+			obj = prop_dictionary_get(obj, cur);
+			if (obj == NULL)
+				exit(EXIT_FAILURE);
+			break;
+		case PROP_TYPE_ARRAY:
+			ind = strtoul(cur, NULL, 0);
+			obj = prop_array_get(obj, ind);
+			if (obj == NULL)
+				exit(EXIT_FAILURE);
+			break;
+		default:
+			errx(EXIT_FAILURE, "Select neither dict nor array with"
+			" `%s'", cur);
+		}
+	}
+
+	if (obj != NULL && cur != NULL)
+		display_object(obj, nflag);
+
+	free(s);
+}
+
 /*
  * Returns the global dictionary.
  */

Index: src/usr.sbin/envstat/envstat.8
diff -u src/usr.sbin/envstat/envstat.8:1.62 src/usr.sbin/envstat/envstat.8:1.63
--- src/usr.sbin/envstat/envstat.8:1.62	Sun May 18 11:46:24 2014
+++ src/usr.sbin/envstat/envstat.8	Sat Nov 14 09:11:55 2020
@@ -1,4 +1,4 @@
-.\"	$NetBSD: envstat.8,v 1.62 2014/05/18 11:46:24 kardel Exp $
+.\"	$NetBSD: envstat.8,v 1.63 2020/11/14 09:11:55 mlelstv Exp $
 .\"
 .\" Copyright (c) 2000, 2007, 2008, 2009, 2014 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -41,6 +41,7 @@
 .Op Fl i Ar interval
 .Op Fl s Ar "device:sensor,..."
 .Op Fl w Ar width
+.Op Ar property...
 .Sh DESCRIPTION
 .Nm
 is a utility that handles various aspects of the sensors
@@ -139,6 +140,9 @@ Shows the raw XML property list used by 
 .Xr sysmon_envsys 9
 framework that contains details about all registered devices
 and sensors.
+If
+.Ar property
+is specified, the value of that property is printed.
 .El
 .Sh UNITS
 The display mode may show some values with abbreviated units;

Index: src/usr.sbin/envstat/envstat.c
diff -u src/usr.sbin/envstat/envstat.c:1.97 src/usr.sbin/envstat/envstat.c:1.98
--- src/usr.sbin/envstat/envstat.c:1.97	Sun Jun  7 00:51:48 2020
+++ src/usr.sbin/envstat/envstat.c	Sat Nov 14 09:11:55 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: envstat.c,v 1.97 2020/06/07 00:51:48 thorpej Exp $ */
+/* $NetBSD: envstat.c,v 1.98 2020/11/14 09:11:55 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.97 2020/06/07 00:51:48 thorpej Exp $");
+__RCSID("$NetBSD: envstat.c,v 1.98 2020/11/14 09:11:55 mlelstv Exp $");
 #endif /* not lint */
 
 #include <stdio.h>
@@ -192,7 +192,7 @@ int main(int argc, char **argv)
 	argc -= optind;
 	argv += optind;
 
-	if (argc > 0)
+	if (argc > 0 && (flags & ENVSYS_XFLAG) == 0)
 		usage();
 
 	/* Check if we want to make statistics */
@@ -219,7 +219,11 @@ int main(int argc, char **argv)
 		if (rval)
 			errx(EXIT_FAILURE, "%s", strerror(rval));
 
-		config_dict_dump(dict);
+		if (argc > 0) {
+			for (; argc > 0; ++argv, --argc)
+				config_dict_extract(dict, *argv, true);
+		} else
+			config_dict_dump(dict);
 
 	/* Remove all properties set in dictionary */
 	} else if (flags & ENVSYS_SFLAG) {
@@ -1098,6 +1102,7 @@ usage(void)
 	(void)fprintf(stderr, "Usage: %s [-DfIklrSTx] ", getprogname());
 	(void)fprintf(stderr, "[-c file] [-d device] [-i interval] ");
 	(void)fprintf(stderr, "[-s device:sensor,...] [-w width]\n");
+	(void)fprintf(stderr, "       %s -x [property]", getprogname());
 	exit(EXIT_FAILURE);
 	/* NOTREACHED */
 }

Index: src/usr.sbin/envstat/envstat.h
diff -u src/usr.sbin/envstat/envstat.h:1.3 src/usr.sbin/envstat/envstat.h:1.4
--- src/usr.sbin/envstat/envstat.h:1.3	Sun Jun  7 00:51:48 2020
+++ src/usr.sbin/envstat/envstat.h	Sat Nov 14 09:11:55 2020
@@ -1,4 +1,4 @@
-/* 	$NetBSD: envstat.h,v 1.3 2020/06/07 00:51:48 thorpej Exp $	*/
+/* 	$NetBSD: envstat.h,v 1.4 2020/11/14 09:11:55 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2007 Juan Romero Pardines.
@@ -47,6 +47,7 @@ void config_dict_add_prop(const char *, 
 void config_dict_adddev_prop(const char *, const char *, int);
 void config_dict_destroy(prop_dictionary_t);
 void config_dict_dump(prop_dictionary_t);
+void config_dict_extract(prop_dictionary_t, const char *, bool);
 void config_dict_fulldump(void);
 void config_dict_mark(void);
 prop_dictionary_t config_dict_parsed(void);

Reply via email to