Module Name:    src
Committed By:   jmcneill
Date:           Sun Aug  7 12:00:11 UTC 2011

Modified Files:
        src/sbin/drvctl: drvctl.8 drvctl.c

Log Message:
add an optional argument to the -p flag that lets you extract specific
property values from the command-line:

  $ drvctl -p wd0 disk-info/geometry/cylinders-per-unit
  620181
  $ drvctl -p wd0 device-driver device-unit
  wd
  0
  $ drvctl -p wd0 nonexistent || echo "not found"
  not found


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sbin/drvctl/drvctl.8 src/sbin/drvctl/drvctl.c

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

Modified files:

Index: src/sbin/drvctl/drvctl.8
diff -u src/sbin/drvctl/drvctl.8:1.10 src/sbin/drvctl/drvctl.8:1.11
--- src/sbin/drvctl/drvctl.8:1.10	Mon Apr 20 22:07:23 2009
+++ src/sbin/drvctl/drvctl.8	Sun Aug  7 12:00:11 2011
@@ -1,4 +1,4 @@
-.\" $NetBSD: drvctl.8,v 1.10 2009/04/20 22:07:23 wiz Exp $
+.\" $NetBSD: drvctl.8,v 1.11 2011/08/07 12:00:11 jmcneill Exp $
 .\"
 .\" Copyright (c) 2004
 .\" 	Matthias Drochner.  All rights reserved.
@@ -24,7 +24,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd April 20, 2009
+.Dd August 7, 2011
 .Dt DRVCTL 8
 .Os
 .Sh NAME
@@ -46,6 +46,7 @@
 .Nm
 .Fl p
 .Ar device
+.Op Ar property ...
 .Nm
 .Fl Q
 .Ar device
@@ -96,10 +97,13 @@
 .Fl l
 output.
 .It Fl p
-Get the properties for the device specified by the
+Get properties for the device specified by the
 .Ar device
 argument.
-The properties are displayed as an XML property list.
+If
+.Ar property
+is specified, the value of that property is printed, otherwise
+the properties are displayed as an XML property list.
 .It Fl Q
 Resume the ancestors of
 .Ar device ,
Index: src/sbin/drvctl/drvctl.c
diff -u src/sbin/drvctl/drvctl.c:1.10 src/sbin/drvctl/drvctl.c:1.11
--- src/sbin/drvctl/drvctl.c:1.10	Mon Apr 20 21:41:50 2009
+++ src/sbin/drvctl/drvctl.c	Sun Aug  7 12:00:11 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: drvctl.c,v 1.10 2009/04/20 21:41:50 dyoung Exp $ */
+/* $NetBSD: drvctl.c,v 1.11 2011/08/07 12:00:11 jmcneill Exp $ */
 
 /*
  * Copyright (c) 2004
@@ -26,6 +26,7 @@
  * SUCH DAMAGE.
  */
 
+#include <inttypes.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -43,6 +44,7 @@
 					  : O_RDONLY)
 
 static void usage(void);
+static void extract_property(prop_dictionary_t, const char *);
 
 static void
 usage(void)
@@ -51,7 +53,7 @@
 	fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
 	    "       %s -d device\n"
 	    "       %s [-n] -l [device]\n"
-	    "       %s -p device\n"
+	    "       %s -p device [prop]\n"
 	    "       %s -Q device\n"
 	    "       %s -R device\n"
 	    "       %s -S device\n",
@@ -219,12 +221,17 @@
 			errx(3, "get-properties: failed to return result data");
 		}
 
-		xml = prop_dictionary_externalize(data_dict);
-		prop_object_release(results_dict);
+		if (argc == 1) {
+			xml = prop_dictionary_externalize(data_dict);
+			printf("Properties for device `%s':\n%s",
+			       argv[0], xml);
+			free(xml);
+		} else {
+			for (i = 1; i < argc; i++)
+				extract_property(data_dict, argv[i]);
+		}
 
-		printf("Properties for device `%s':\n%s",
-		       argv[0], xml);
-		free(xml);
+		prop_object_release(results_dict);
 		break;
 	default:
 		errx(4, "unknown command");
@@ -232,3 +239,50 @@
 
 	return (0);
 }
+
+static void
+extract_property(prop_dictionary_t dict, const char *prop)
+{
+	char *s, *p, *cur, *ep = NULL, *xml;
+	prop_object_t obj;
+
+	s = strdup(prop);
+	p = strtok_r(s, "/", &ep);
+	while (p) {
+		cur = p;
+		p = strtok_r(NULL, "/", &ep);
+		if (p) {
+			if (prop_dictionary_get_dict(dict, cur, &dict) == false)
+				exit(EXIT_FAILURE);
+		} else {
+			obj = prop_dictionary_get(dict, cur);
+			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_integer_value(obj));
+				break;
+			case PROP_TYPE_STRING:
+				printf("%s\n",
+				    prop_string_cstring_nocopy(obj));
+				break;
+			case PROP_TYPE_DICTIONARY:
+				xml = prop_dictionary_externalize(obj);
+				printf("%s", xml);
+				free(xml);
+				break;
+			default:
+				fprintf(stderr, "unhandled type %d\n",
+				    prop_object_type(obj));
+				exit(EXIT_FAILURE);
+			}
+		}
+	}
+
+	free(s);
+}

Reply via email to