Source-Changes-HG archive

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

[src/trunk]: src/sbin/drvctl add an optional argument to the -p flag that let...



details:   https://anonhg.NetBSD.org/src/rev/97a134dd9959
branches:  trunk
changeset: 768058:97a134dd9959
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sun Aug 07 12:00:11 2011 +0000

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

diffstat:

 sbin/drvctl/drvctl.8 |  12 ++++++---
 sbin/drvctl/drvctl.c |  68 ++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 69 insertions(+), 11 deletions(-)

diffs (150 lines):

diff -r 48c030ea1f55 -r 97a134dd9959 sbin/drvctl/drvctl.8
--- a/sbin/drvctl/drvctl.8      Sun Aug 07 11:41:50 2011 +0000
+++ b/sbin/drvctl/drvctl.8      Sun Aug 07 12:00:11 2011 +0000
@@ -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 ,
diff -r 48c030ea1f55 -r 97a134dd9959 sbin/drvctl/drvctl.c
--- a/sbin/drvctl/drvctl.c      Sun Aug 07 11:41:50 2011 +0000
+++ b/sbin/drvctl/drvctl.c      Sun Aug 07 12:00:11 2011 +0000
@@ -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);
+}



Home | Main Index | Thread Index | Old Index