Bring in FreeBSD's usbhidaction(1).
authorSascha Wildner <saw@online.de>
Mon, 10 Mar 2014 21:02:44 +0000 (22:02 +0100)
committerSascha Wildner <saw@online.de>
Mon, 10 Mar 2014 21:02:44 +0000 (22:02 +0100)
Makefile_upgrade.inc
usr.bin/Makefile
usr.bin/usbhidaction/Makefile [new file with mode: 0644]
usr.bin/usbhidaction/usbhidaction.1 [new file with mode: 0644]
usr.bin/usbhidaction/usbhidaction.c [new file with mode: 0644]

index c83f671..092884c 100644 (file)
@@ -2512,4 +2512,7 @@ TO_REMOVE+=/usr/share/man/cat4/urtwnfw.4.gz
 TO_REMOVE+=/usr/share/man/man4/urtwnfw.4.gz
 TO_REMOVE+=/usr/share/man/cat4/usb_quirk.4.gz
 TO_REMOVE+=/usr/share/man/man4/usb_quirk.4.gz
+TO_REMOVE+=/usr/bin/usbhidaction
+TO_REMOVE+=/usr/share/man/cat1/usbhidaction.1.gz
+TO_REMOVE+=/usr/share/man/man1/usbhidaction.1.gz
 .endif
index 6b3ce3d..d1a73c2 100644 (file)
@@ -216,6 +216,7 @@ SUBDIR=     alias \
        uniq \
        units \
        unvis \
+       ${_usbhidaction} \
        ${_usbhidctl} \
        users \
        uudecode \
@@ -253,6 +254,7 @@ SUBDIR+=hesinfo
 
 .if !defined(WANT_OLDUSB)
 _usbhidctl=    u4bhidctl
+_usbhidaction= usbhidaction
 .else
 _usbhidctl=    usbhidctl
 .endif
diff --git a/usr.bin/usbhidaction/Makefile b/usr.bin/usbhidaction/Makefile
new file mode 100644 (file)
index 0000000..07504c4
--- /dev/null
@@ -0,0 +1,9 @@
+#      $FreeBSD: head/usr.bin/usbhidaction/Makefile 140941 2005-01-28 16:08:11Z ru $
+#      $NetBSD: Makefile,v 1.4 2002/02/02 16:54:26 veego Exp $
+
+PROG=  usbhidaction
+
+LDADD= -lusbhid
+DPADD= ${LIBUSBHID}
+
+.include <bsd.prog.mk>
diff --git a/usr.bin/usbhidaction/usbhidaction.1 b/usr.bin/usbhidaction/usbhidaction.1
new file mode 100644 (file)
index 0000000..1570e64
--- /dev/null
@@ -0,0 +1,174 @@
+.\" $FreeBSD: head/usr.bin/usbhidaction/usbhidaction.1 225839 2011-09-28 14:52:25Z mav $
+.\" $NetBSD: usbhidaction.1,v 1.8 2003/02/25 10:35:59 wiz Exp $
+.\"
+.\" Copyright (c) 2000 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Lennart Augustsson (lennart@augustsson.net).
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd April 9, 2003
+.Dt USBHIDACTION 1
+.Os
+.Sh NAME
+.Nm usbhidaction
+.Nd perform actions according to USB HID controls
+.Sh SYNOPSIS
+.Nm
+.Op Fl diev
+.Fl c Ar config-file
+.Fl f Ar device
+.Op Fl p Ar pidfile
+.Op Fl t Ar tablefile
+.Ar arg ...
+.Sh DESCRIPTION
+The
+.Nm
+utility
+can be used to execute commands when certain values appear on HID controls.
+The normal operation for this program is to read the configuration file
+and then become a daemon and execute commands as the HID items specify.
+If a read from the HID device fails, the program dies; this will make it
+die when the USB device is unplugged.
+.Pp
+The options are as follows:
+.Bl -tag -width indent
+.It Fl d
+Toggle the daemon flag.
+.It Fl e
+Instruct
+.Nm
+to die early.
+Useful when specified with multiple verbose options to see how files are parsed.
+.It Fl i
+Ignore HID items in the configuration file that do not exist in the device.
+.It Fl v
+Be verbose, and do not become a daemon.
+.It Fl c Ar config-file
+Specify a path name for the configuration file.
+.It Fl t Ar tablefile
+Specify a path name for the HID usage table file.
+.It Fl f Ar device
+Specify a path name for the device to operate on.
+If
+.Ar device
+is numeric, it is taken to be the USB HID device number.
+If it is a relative
+path, it is taken to be the name of the device under
+.Pa /dev .
+An absolute path is taken to be the literal device pathname.
+.It Fl p Ar pidfile
+Specify an alternate file in which to store the process ID.
+.El
+.Pp
+The configuration file will be re-read if the process gets a
+.Dv SIGHUP
+signal.
+.Sh CONFIGURATION
+The configuration file has a very simple format.
+Each line describes an
+action; if a line begins with a whitespace, it is considered a continuation
+of the previous line.
+Lines beginning with
+.Ql #
+are considered as comments.
+.Pp
+Each line has four parts: a name of a USB HID item, a value for that item,
+a debounce value, and an action.
+There must be whitespace between the parts.
+.Pp
+The item names are similar to those used by
+.Xr usbhidctl 1 .
+.Pp
+The value is simply a numeric value.
+When the item reports this value,
+the action will be performed.
+If the value is
+.Ql * ,
+it will match any value.
+.Pp
+The debounce value is an integer not less than 0.
+The value of 0 indicates that no debouncing should occur.
+A value of 1 will only execute the action when the state changes.
+Values greater than one specify that an action should be performed
+only when the value changes by that amount.
+.Pp
+The action is a normal command that is executed with
+.Xr system 3 .
+Before it is executed some substitution will occur:
+.Ql $n
+will be replaced by the
+.Ar n Ns th
+argument on the command line,
+.Ql $V
+will be replaced by the numeric value of the HID item,
+.Ql $N
+will be replaced by the name of the control, and
+.Ql $H
+will be replaced by the name of the HID device.
+.Sh FILES
+.Bl -tag -width ".Pa /usr/share/misc/usb_hid_usages"
+.It Pa /usr/share/misc/usb_hid_usages
+The HID usage table.
+.It Pa /var/run/usbaction.pid
+The default location of the PID file.
+.El
+.Sh EXAMPLES
+The following configuration file can be used to control a pair
+of Philips USB speakers with the HID controls on the speakers.
+.Bd -literal -offset indent
+# Configuration for various Philips USB speakers
+Consumer:Volume_Up                      1 0 mixer -f $1 vol +1
+Consumer:Volume_Down                    1 0 mixer -f $1 vol -1
+# mute not supported
+#Consumer:Mute                          1 0 mixer -f $1 mute
+Consumer:Channel_Top.Microsoft:Base_Up  1 0 mixer -f $1 bass +1
+Consumer:Channel_Top.Microsoft:Base_Down 1 0 mixer -f $1 bass -1
+.Ed
+.Pp
+A sample invocation using this configuration would be
+.Pp
+.Dl "usbhidaction -f /dev/uhid1 -c conf /dev/mixer1"
+.Pp
+The following example controls the mixer volume using a Logitech Wingman.
+Notice the debounce of 1 for buttons and 5 for the slider.
+.Bd -literal -offset indent
+Button:Button_1          1 1   mixer vol +10
+Button:Button_2          1 1   mixer vol -10
+Generic_Desktop:Z * 5  mixer vol `echo $V | awk '{print int($$1/255*100)}'`
+.Ed
+.Sh SEE ALSO
+.Xr usbhidctl 1 ,
+.Xr usbhid 3 ,
+.Xr uhid 4 ,
+.Xr usb 4
+.Sh HISTORY
+The
+.Nm
+command first appeared in
+.Nx 1.6 .
+The
+.Nm
+command appeared in
+.Fx 5.1 .
diff --git a/usr.bin/usbhidaction/usbhidaction.c b/usr.bin/usbhidaction/usbhidaction.c
new file mode 100644 (file)
index 0000000..6cebed4
--- /dev/null
@@ -0,0 +1,520 @@
+/*      $NetBSD: usbhidaction.c,v 1.8 2002/06/11 06:06:21 itojun Exp $ */
+/*     $FreeBSD: head/usr.bin/usbhidaction/usbhidaction.c 227195 2011-11-06 08:18:23Z ed $ */
+
+/*
+ * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Lennart Augustsson <lennart@augustsson.net>.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <err.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <bus/u4b/usbhid.h>
+#include <usbhid.h>
+#include <syslog.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+static int     verbose = 0;
+static int     isdemon = 0;
+static int     reparse = 1;
+static const char *    pidfile = "/var/run/usbaction.pid";
+
+struct command {
+       struct command *next;
+       int line;
+
+       struct hid_item item;
+       int value;
+       int lastseen;
+       int lastused;
+       int debounce;
+       char anyvalue;
+       char *name;
+       char *action;
+};
+static struct command *commands;
+
+#define SIZE 4000
+
+void usage(void);
+struct command *parse_conf(const char *, report_desc_t, int, int);
+void docmd(struct command *, int, const char *, int, char **);
+void freecommands(struct command *);
+
+static void
+sighup(int sig __unused)
+{
+       reparse = 1;
+}
+
+int
+main(int argc, char **argv)
+{
+       const char *conf = NULL;
+       const char *dev = NULL;
+       const char *table = NULL;
+       int fd, fp, ch, n, val, i;
+       size_t sz, sz1;
+       int demon, ignore, dieearly;
+       report_desc_t repd;
+       char buf[100];
+       char devnamebuf[PATH_MAX];
+       struct command *cmd;
+       int reportid = -1;
+
+       demon = 1;
+       ignore = 0;
+       dieearly = 0;
+       while ((ch = getopt(argc, argv, "c:def:ip:r:t:v")) != -1) {
+               switch(ch) {
+               case 'c':
+                       conf = optarg;
+                       break;
+               case 'd':
+                       demon ^= 1;
+                       break;
+               case 'e':
+                       dieearly = 1;
+                       break;
+               case 'i':
+                       ignore++;
+                       break;
+               case 'f':
+                       dev = optarg;
+                       break;
+               case 'p':
+                       pidfile = optarg;
+                       break;
+               case 'r':
+                       reportid = atoi(optarg);
+                       break;
+               case 't':
+                       table = optarg;
+                       break;
+               case 'v':
+                       demon = 0;
+                       verbose++;
+                       break;
+               case '?':
+               default:
+                       usage();
+               }
+       }
+       argc -= optind;
+       argv += optind;
+
+       if (conf == NULL || dev == NULL)
+               usage();
+
+       hid_init(table);
+
+       if (dev[0] != '/') {
+               snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
+                        isdigit(dev[0]) ? "uhid" : "", dev);
+               dev = devnamebuf;
+       }
+
+       fd = open(dev, O_RDWR);
+       if (fd < 0)
+               err(1, "%s", dev);
+       repd = hid_get_report_desc(fd);
+       if (repd == NULL)
+               err(1, "hid_get_report_desc() failed");
+
+       commands = parse_conf(conf, repd, reportid, ignore);
+
+       sz = (size_t)hid_report_size(repd, hid_input, -1);
+
+       if (verbose)
+               printf("report size %zu\n", sz);
+       if (sz > sizeof buf)
+               errx(1, "report too large");
+
+       (void)signal(SIGHUP, sighup);
+
+       if (demon) {
+               fp = open(pidfile, O_WRONLY|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH);
+               if (fp >= 0) {
+                       sz1 = snprintf(buf, sizeof buf, "%ld\n", 
+                           (long)getpid());
+                       if (sz1 > sizeof buf)
+                               sz1 = sizeof buf;
+                       write(fp, buf, sz1);
+                       close(fp);
+               } else
+                       err(1, "%s", pidfile);
+               if (daemon(0, 0) < 0)
+                       err(1, "daemon()");
+               isdemon = 1;
+       }
+
+       for(;;) {
+               n = read(fd, buf, sz);
+               if (verbose > 2) {
+                       printf("read %d bytes:", n);
+                       for (i = 0; i < n; i++)
+                               printf(" %02x", buf[i]);
+                       printf("\n");
+               }
+               if (n < 0) {
+                       if (verbose)
+                               err(1, "read");
+                       else
+                               exit(1);
+               }
+#if 0
+               if (n != sz) {
+                       err(2, "read size");
+               }
+#endif
+               for (cmd = commands; cmd; cmd = cmd->next) {
+                       if (cmd->item.report_ID != 0 &&
+                           buf[0] != cmd->item.report_ID)
+                               continue;
+                       if (cmd->item.flags & HIO_VARIABLE)
+                               val = hid_get_data(buf, &cmd->item);
+                       else {
+                               uint32_t pos = cmd->item.pos;
+                               for (i = 0; i < cmd->item.report_count; i++) {
+                                       val = hid_get_data(buf, &cmd->item);
+                                       if (val == cmd->value)
+                                               break;
+                                       cmd->item.pos += cmd->item.report_size;
+                               }
+                               cmd->item.pos = pos;
+                               val = (i < cmd->item.report_count) ?
+                                   cmd->value : -1;
+                       }
+                       if (cmd->value != val && cmd->anyvalue == 0)
+                               goto next;
+                       if ((cmd->debounce == 0) ||
+                           ((cmd->debounce == 1) && ((cmd->lastseen == -1) ||
+                                              (cmd->lastseen != val)))) {
+                               docmd(cmd, val, dev, argc, argv);
+                               goto next;
+                       }
+                       if ((cmd->debounce > 1) &&
+                           ((cmd->lastused == -1) ||
+                            (abs(cmd->lastused - val) >= cmd->debounce))) {
+                               docmd(cmd, val, dev, argc, argv);
+                               cmd->lastused = val;
+                               goto next;
+                       }
+next:
+                       cmd->lastseen = val;
+               }
+
+               if (dieearly)
+                       exit(0);
+
+               if (reparse) {
+                       struct command *cmds =
+                           parse_conf(conf, repd, reportid, ignore);
+                       if (cmds) {
+                               freecommands(commands);
+                               commands = cmds;
+                       }
+                       reparse = 0;
+               }
+       }
+
+       exit(0);
+}
+
+void
+usage(void)
+{
+
+       fprintf(stderr, "Usage: %s [-deiv] -c config_file -f hid_dev "
+               "[-p pidfile] [-t tablefile]\n", getprogname());
+       exit(1);
+}
+
+static int
+peek(FILE *f)
+{
+       int c;
+
+       c = getc(f);
+       if (c != EOF)
+               ungetc(c, f);
+       return c;
+}
+
+struct command *
+parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
+{
+       FILE *f;
+       char *p;
+       int line;
+       char buf[SIZE], name[SIZE], value[SIZE], debounce[SIZE], action[SIZE];
+       char usbuf[SIZE], coll[SIZE], *tmp;
+       struct command *cmd, *cmds;
+       struct hid_data *d;
+       struct hid_item h;
+       int inst, cinst, u, lo, hi, range, t;
+
+       f = fopen(conf, "r");
+       if (f == NULL)
+               err(1, "%s", conf);
+
+       cmds = NULL;
+       for (line = 1; ; line++) {
+               if (fgets(buf, sizeof buf, f) == NULL)
+                       break;
+               if (buf[0] == '#' || buf[0] == '\n')
+                       continue;
+               p = strchr(buf, '\n');
+               while (p && isspace(peek(f))) {
+                       if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
+                               break;
+                       p = strchr(buf, '\n');
+               }
+               if (p)
+                       *p = 0;
+               if (sscanf(buf, "%s %s %s %[^\n]",
+                          name, value, debounce, action) != 4) {
+                       if (isdemon) {
+                               syslog(LOG_WARNING, "config file `%s', line %d"
+                                      ", syntax error: %s", conf, line, buf);
+                               freecommands(cmds);
+                               return (NULL);
+                       } else {
+                               errx(1, "config file `%s', line %d,"
+                                    ", syntax error: %s", conf, line, buf);
+                       }
+               }
+               tmp = strchr(name, '#');
+               if (tmp != NULL) {
+                       *tmp = 0;
+                       inst = atoi(tmp + 1);
+               } else
+                       inst = 0;
+
+               cmd = malloc(sizeof *cmd);
+               if (cmd == NULL)
+                       err(1, "malloc failed");
+               cmd->next = cmds;
+               cmds = cmd;
+               cmd->line = line;
+
+               if (strcmp(value, "*") == 0) {
+                       cmd->anyvalue = 1;
+               } else {
+                       cmd->anyvalue = 0;
+                       if (sscanf(value, "%d", &cmd->value) != 1) {
+                               if (isdemon) {
+                                       syslog(LOG_WARNING,
+                                              "config file `%s', line %d, "
+                                              "bad value: %s (should be * or a number)\n",
+                                              conf, line, value);
+                                       freecommands(cmds);
+                                       return (NULL);
+                               } else {
+                                       errx(1, "config file `%s', line %d, "
+                                            "bad value: %s (should be * or a number)\n",
+                                            conf, line, value);
+                               }
+                       }
+               }
+
+               if (sscanf(debounce, "%d", &cmd->debounce) != 1) {
+                       if (isdemon) {
+                               syslog(LOG_WARNING,
+                                      "config file `%s', line %d, "
+                                      "bad value: %s (should be a number >= 0)\n",
+                                      conf, line, debounce);
+                               freecommands(cmds);
+                               return (NULL);
+                       } else {
+                               errx(1, "config file `%s', line %d, "
+                                    "bad value: %s (should be a number >= 0)\n",
+                                    conf, line, debounce);
+                       }
+               }
+
+               coll[0] = 0;
+               cinst = 0;
+               for (d = hid_start_parse(repd, 1 << hid_input, reportid);
+                    hid_get_item(d, &h); ) {
+                       if (verbose > 2)
+                               printf("kind=%d usage=%x\n", h.kind, h.usage);
+                       if (h.flags & HIO_CONST)
+                               continue;
+                       switch (h.kind) {
+                       case hid_input:
+                               if (h.usage_minimum != 0 ||
+                                   h.usage_maximum != 0) {
+                                       lo = h.usage_minimum;
+                                       hi = h.usage_maximum;
+                                       range = 1;
+                               } else {
+                                       lo = h.usage;
+                                       hi = h.usage;
+                                       range = 0;
+                               }
+                               for (u = lo; u <= hi; u++) {
+                                       if (coll[0]) {
+                                               snprintf(usbuf, sizeof usbuf,
+                                                 "%s.%s:%s", coll+1,
+                                                 hid_usage_page(HID_PAGE(u)),
+                                                 hid_usage_in_page(u));
+                                       } else {
+                                               snprintf(usbuf, sizeof usbuf,
+                                                 "%s:%s",
+                                                 hid_usage_page(HID_PAGE(u)),
+                                                 hid_usage_in_page(u));
+                                       }
+                                       if (verbose > 2)
+                                               printf("usage %s\n", usbuf);
+                                       t = strlen(usbuf) - strlen(name);
+                                       if (t > 0) {
+                                               if (strcmp(usbuf + t, name))
+                                                       continue;
+                                               if (usbuf[t - 1] != '.')
+                                                       continue;
+                                       } else if (strcmp(usbuf, name))
+                                               continue;
+                                       if (inst == cinst++)
+                                               goto foundhid;
+                               }
+                               break;
+                       case hid_collection:
+                               snprintf(coll + strlen(coll),
+                                   sizeof coll - strlen(coll),  ".%s:%s",
+                                   hid_usage_page(HID_PAGE(h.usage)), 
+                                   hid_usage_in_page(h.usage));
+                               break;
+                       case hid_endcollection:
+                               if (coll[0])
+                                       *strrchr(coll, '.') = 0;
+                               break;
+                       default:
+                               break;
+                       }
+               }
+               if (ignore) {
+                       if (verbose)
+                               warnx("ignore item '%s'", name);
+                       continue;
+               }
+               if (isdemon) {
+                       syslog(LOG_WARNING, "config file `%s', line %d, HID "
+                              "item not found: `%s'\n", conf, line, name);
+                       freecommands(cmds);
+                       return (NULL);
+               } else {
+                       errx(1, "config file `%s', line %d, HID item "
+                            "not found: `%s'\n", conf, line, name);
+               }
+
+       foundhid:
+               hid_end_parse(d);
+               cmd->lastseen = -1;
+               cmd->lastused = -1;
+               cmd->item = h;
+               cmd->name = strdup(name);
+               cmd->action = strdup(action);
+               if (range) {
+                       if (cmd->value == 1)
+                               cmd->value = u - lo;
+                       else
+                               cmd->value = -1;
+               }
+
+               if (verbose)
+                       printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
+                              cmd->value, cmd->action);
+       }
+       fclose(f);
+       return (cmds);
+}
+
+void
+docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
+{
+       char cmdbuf[SIZE], *p, *q;
+       size_t len;
+       int n, r;
+
+       for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
+               if (*p == '$') {
+                       p++;
+                       len = &cmdbuf[SIZE-1] - q;
+                       if (isdigit(*p)) {
+                               n = strtol(p, &p, 10) - 1;
+                               if (n >= 0 && n < argc) {
+                                       strncpy(q, argv[n], len);
+                                       q += strlen(q);
+                               }
+                       } else if (*p == 'V') {
+                               p++;
+                               snprintf(q, len, "%d", value);
+                               q += strlen(q);
+                       } else if (*p == 'N') {
+                               p++;
+                               strncpy(q, cmd->name, len);
+                               q += strlen(q);
+                       } else if (*p == 'H') {
+                               p++;
+                               strncpy(q, hid, len);
+                               q += strlen(q);
+                       } else if (*p) {
+                               *q++ = *p++;
+                       }
+               } else {
+                       *q++ = *p++;
+               }
+       }
+       *q = 0;
+
+       if (verbose)
+               printf("system '%s'\n", cmdbuf);
+       r = system(cmdbuf);
+       if (verbose > 1 && r)
+               printf("return code = 0x%x\n", r);
+}
+
+void
+freecommands(struct command *cmd)
+{
+       struct command *next;
+
+       while (cmd) {
+               next = cmd->next;
+               free(cmd);
+               cmd = next;
+       }
+}