usbhidaction.c revision 1.20       1  1.20  christos /*      $NetBSD: usbhidaction.c,v 1.20 2006/05/01 00:03:15 christos Exp $ */
      2   1.2  augustss 
      3   1.2  augustss /*
      4   1.7  augustss  * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
      5   1.2  augustss  * All rights reserved.
      6   1.2  augustss  *
      7   1.2  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2  augustss  * by Lennart Augustsson <lennart (at) augustsson.net>.
      9   1.2  augustss  *
     10   1.2  augustss  * Redistribution and use in source and binary forms, with or without
     11   1.2  augustss  * modification, are permitted provided that the following conditions
     12   1.2  augustss  * are met:
     13   1.2  augustss  * 1. Redistributions of source code must retain the above copyright
     14   1.2  augustss  *    notice, this list of conditions and the following disclaimer.
     15   1.2  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2  augustss  *    notice, this list of conditions and the following disclaimer in the
     17   1.2  augustss  *    documentation and/or other materials provided with the distribution.
     18   1.2  augustss  * 3. All advertising materials mentioning features or use of this software
     19   1.2  augustss  *    must display the following acknowledgement:
     20   1.2  augustss  *        This product includes software developed by the NetBSD
     21   1.2  augustss  *        Foundation, Inc. and its contributors.
     22   1.2  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.2  augustss  *    contributors may be used to endorse or promote products derived
     24   1.2  augustss  *    from this software without specific prior written permission.
     25   1.2  augustss  *
     26   1.2  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.2  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.2  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.2  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.2  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.2  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.2  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.2  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.2  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.2  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.2  augustss  * POSSIBILITY OF SUCH DAMAGE.
     37   1.2  augustss  */
     38  1.10       agc #include <sys/cdefs.h>
     39  1.10       agc 
     40  1.10       agc #ifndef lint
     41  1.20  christos __RCSID("$NetBSD: usbhidaction.c,v 1.20 2006/05/01 00:03:15 christos Exp $");
     42  1.10       agc #endif
     43   1.2  augustss 
     44   1.1  augustss #include <stdio.h>
     45   1.1  augustss #include <stdlib.h>
     46   1.1  augustss #include <string.h>
     47   1.1  augustss #include <ctype.h>
     48   1.1  augustss #include <err.h>
     49   1.1  augustss #include <fcntl.h>
     50   1.5  augustss #include <limits.h>
     51   1.1  augustss #include <unistd.h>
     52   1.1  augustss #include <sys/types.h>
     53   1.1  augustss #include <sys/ioctl.h>
     54   1.1  augustss #include <dev/usb/usb.h>
     55   1.1  augustss #include <dev/usb/usbhid.h>
     56   1.4  augustss #include <usbhid.h>
     57   1.1  augustss #include <util.h>
     58   1.7  augustss #include <syslog.h>
     59   1.7  augustss #include <signal.h>
     60   1.1  augustss 
     61   1.1  augustss int verbose = 0;
     62   1.7  augustss int isdemon = 0;
     63  1.13  augustss int reparse = 0;
     64   1.1  augustss 
     65   1.1  augustss struct command {
     66   1.1  augustss 	struct command *next;
     67   1.1  augustss 	int line;
     68   1.1  augustss 
     69   1.1  augustss 	struct hid_item item;
     70   1.1  augustss 	int value;
     71   1.1  augustss 	char anyvalue;
     72   1.1  augustss 	char *name;
     73   1.1  augustss 	char *action;
     74   1.1  augustss };
     75   1.1  augustss struct command *commands;
     76   1.1  augustss 
     77   1.1  augustss #define SIZE 4000
     78   1.1  augustss 
     79   1.1  augustss void usage(void);
     80   1.7  augustss struct command *parse_conf(const char *, report_desc_t, int, int);
     81   1.5  augustss void docmd(struct command *, int, const char *, int, char **);
     82   1.7  augustss void freecommands(struct command *);
     83   1.7  augustss 
     84   1.7  augustss static void
     85   1.7  augustss sighup(int sig)
     86   1.7  augustss {
     87   1.7  augustss 	reparse = 1;
     88   1.7  augustss }
     89   1.1  augustss 
     90   1.1  augustss int
     91   1.1  augustss main(int argc, char **argv)
     92   1.1  augustss {
     93   1.1  augustss 	const char *conf = NULL;
     94   1.5  augustss 	const char *dev = NULL;
     95   1.5  augustss 	int fd, ch, sz, n, val, i;
     96   1.1  augustss 	int demon, ignore;
     97   1.1  augustss 	report_desc_t repd;
     98   1.1  augustss 	char buf[100];
     99   1.5  augustss 	char devnamebuf[PATH_MAX];
    100   1.1  augustss 	struct command *cmd;
    101   1.4  augustss 	int reportid;
    102  1.14  augustss 	const char *table = NULL;
    103   1.1  augustss 
    104  1.16  augustss 	setlinebuf(stdout);
    105  1.16  augustss 
    106   1.1  augustss 	demon = 1;
    107   1.1  augustss 	ignore = 0;
    108  1.14  augustss 	while ((ch = getopt(argc, argv, "c:df:it:v")) != -1) {
    109   1.1  augustss 		switch(ch) {
    110   1.1  augustss 		case 'c':
    111   1.1  augustss 			conf = optarg;
    112   1.1  augustss 			break;
    113   1.1  augustss 		case 'd':
    114   1.1  augustss 			demon ^= 1;
    115   1.1  augustss 			break;
    116   1.1  augustss 		case 'i':
    117   1.1  augustss 			ignore++;
    118   1.1  augustss 			break;
    119   1.1  augustss 		case 'f':
    120   1.5  augustss 			dev = optarg;
    121   1.1  augustss 			break;
    122  1.14  augustss 		case 't':
    123  1.14  augustss 			table = optarg;
    124  1.14  augustss 			break;
    125   1.1  augustss 		case 'v':
    126   1.1  augustss 			demon = 0;
    127   1.1  augustss 			verbose++;
    128   1.1  augustss 			break;
    129   1.1  augustss 		case '?':
    130   1.1  augustss 		default:
    131   1.1  augustss 			usage();
    132   1.1  augustss 		}
    133   1.1  augustss 	}
    134   1.1  augustss 	argc -= optind;
    135   1.1  augustss 	argv += optind;
    136   1.1  augustss 
    137   1.5  augustss 	if (conf == NULL || dev == NULL)
    138   1.1  augustss 		usage();
    139   1.1  augustss 
    140  1.14  augustss 	hid_init(table);
    141   1.1  augustss 
    142   1.5  augustss 	if (dev[0] != '/') {
    143   1.5  augustss 		snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
    144  1.17       dsl 			 isdigit((unsigned char)dev[0]) ? "uhid" : "", dev);
    145   1.5  augustss 		dev = devnamebuf;
    146   1.5  augustss 	}
    147   1.5  augustss 
    148  1.18  augustss 	if (demon && conf[0] != '/')
    149  1.18  augustss 		errx(1, "config file must have an absolute path, %s", conf);
    150  1.18  augustss 
    151   1.5  augustss 	fd = open(dev, O_RDWR);
    152   1.1  augustss 	if (fd < 0)
    153   1.5  augustss 		err(1, "%s", dev);
    154  1.19   dsainty 
    155  1.19   dsainty 	/* Avoid passing the device file descriptor to executed commands */
    156  1.19   dsainty 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
    157  1.19   dsainty 		err(1, "fcntl(F_SETFD, FD_CLOEXEC)");
    158  1.19   dsainty 
    159   1.4  augustss 	if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
    160   1.4  augustss 		reportid = -1;
    161   1.1  augustss 	repd = hid_get_report_desc(fd);
    162   1.1  augustss 	if (repd == NULL)
    163   1.8    itojun 		err(1, "hid_get_report_desc() failed");
    164   1.1  augustss 
    165   1.7  augustss 	commands = parse_conf(conf, repd, reportid, ignore);
    166   1.1  augustss 
    167   1.5  augustss 	sz = hid_report_size(repd, hid_input, reportid);
    168   1.1  augustss 
    169   1.1  augustss 	if (verbose)
    170   1.1  augustss 		printf("report size %d\n", sz);
    171   1.1  augustss 	if (sz > sizeof buf)
    172   1.1  augustss 		errx(1, "report too large");
    173   1.1  augustss 
    174   1.7  augustss 	(void)signal(SIGHUP, sighup);
    175   1.7  augustss 
    176   1.1  augustss 	if (demon) {
    177   1.1  augustss 		if (daemon(0, 0) < 0)
    178   1.1  augustss 			err(1, "daemon()");
    179   1.1  augustss 		pidfile(NULL);
    180   1.7  augustss 		isdemon = 1;
    181   1.1  augustss 	}
    182   1.1  augustss 
    183   1.1  augustss 	for(;;) {
    184   1.1  augustss 		n = read(fd, buf, sz);
    185   1.5  augustss 		if (verbose > 2) {
    186   1.5  augustss 			printf("read %d bytes:", n);
    187   1.5  augustss 			for (i = 0; i < n; i++)
    188   1.5  augustss 				printf(" %02x", buf[i]);
    189   1.5  augustss 			printf("\n");
    190   1.5  augustss 		}
    191   1.1  augustss 		if (n < 0) {
    192   1.1  augustss 			if (verbose)
    193   1.1  augustss 				err(1, "read");
    194   1.1  augustss 			else
    195   1.1  augustss 				exit(1);
    196   1.1  augustss 		}
    197   1.5  augustss #if 0
    198   1.5  augustss 		if (n != sz) {
    199   1.5  augustss 			err(2, "read size");
    200   1.5  augustss 		}
    201   1.5  augustss #endif
    202   1.1  augustss 		for (cmd = commands; cmd; cmd = cmd->next) {
    203   1.1  augustss 			val = hid_get_data(buf, &cmd->item);
    204   1.1  augustss 			if (cmd->value == val || cmd->anyvalue)
    205   1.5  augustss 				docmd(cmd, val, dev, argc, argv);
    206   1.1  augustss 		}
    207   1.7  augustss 		if (reparse) {
    208   1.7  augustss 			struct command *cmds =
    209   1.7  augustss 			    parse_conf(conf, repd, reportid, ignore);
    210   1.7  augustss 			if (cmds) {
    211   1.7  augustss 				freecommands(commands);
    212   1.7  augustss 				commands = cmds;
    213   1.7  augustss 			}
    214   1.7  augustss 			reparse = 0;
    215   1.7  augustss 		}
    216   1.1  augustss 	}
    217   1.1  augustss 
    218   1.1  augustss 	exit(0);
    219   1.1  augustss }
    220   1.1  augustss 
    221   1.1  augustss void
    222   1.1  augustss usage(void)
    223   1.1  augustss {
    224   1.1  augustss 
    225  1.11      jmmv 	fprintf(stderr, "usage: %s -c config_file [-d] -f hid_dev "
    226  1.14  augustss 		"[-i] [-t table] [-v]\n", getprogname());
    227   1.1  augustss 	exit(1);
    228   1.1  augustss }
    229   1.1  augustss 
    230   1.1  augustss static int
    231   1.1  augustss peek(FILE *f)
    232   1.1  augustss {
    233   1.1  augustss 	int c;
    234   1.1  augustss 
    235   1.1  augustss 	c = getc(f);
    236   1.1  augustss 	if (c != EOF)
    237   1.1  augustss 		ungetc(c, f);
    238   1.1  augustss 	return c;
    239   1.1  augustss }
    240   1.1  augustss 
    241   1.7  augustss struct command *
    242   1.4  augustss parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
    243   1.1  augustss {
    244   1.1  augustss 	FILE *f;
    245   1.1  augustss 	char *p;
    246   1.1  augustss 	int line;
    247   1.1  augustss 	char buf[SIZE], name[SIZE], value[SIZE], action[SIZE];
    248   1.1  augustss 	char usage[SIZE], coll[SIZE];
    249   1.7  augustss 	struct command *cmd, *cmds;
    250   1.1  augustss 	struct hid_data *d;
    251   1.1  augustss 	struct hid_item h;
    252   1.6  augustss 	int u, lo, hi, range;
    253   1.7  augustss 
    254   1.1  augustss 	f = fopen(conf, "r");
    255   1.1  augustss 	if (f == NULL)
    256   1.1  augustss 		err(1, "%s", conf);
    257   1.1  augustss 
    258   1.7  augustss 	cmds = NULL;
    259   1.1  augustss 	for (line = 1; ; line++) {
    260   1.1  augustss 		if (fgets(buf, sizeof buf, f) == NULL)
    261   1.1  augustss 			break;
    262   1.1  augustss 		if (buf[0] == '#' || buf[0] == '\n')
    263   1.1  augustss 			continue;
    264   1.1  augustss 		p = strchr(buf, '\n');
    265   1.1  augustss 		while (p && isspace(peek(f))) {
    266   1.1  augustss 			if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
    267   1.1  augustss 				break;
    268   1.1  augustss 			p = strchr(buf, '\n');
    269   1.1  augustss 		}
    270   1.1  augustss 		if (p)
    271   1.1  augustss 			*p = 0;
    272   1.9    itojun 		/* XXX SIZE == 4000 */
    273   1.9    itojun 		if (sscanf(buf, "%3999s %3999s %[^\n]", name, value, action) != 3) {
    274   1.7  augustss 			if (isdemon) {
    275   1.7  augustss 				syslog(LOG_WARNING, "config file `%s', line %d"
    276   1.7  augustss 				       ", syntax error: %s", conf, line, buf);
    277   1.7  augustss 				freecommands(cmds);
    278  1.20  christos 				fclose(f);
    279   1.7  augustss 				return (NULL);
    280   1.7  augustss 			} else {
    281   1.7  augustss 				errx(1, "config file `%s', line %d,"
    282   1.7  augustss 				     ", syntax error: %s", conf, line, buf);
    283   1.7  augustss 			}
    284   1.1  augustss 		}
    285   1.1  augustss 
    286   1.1  augustss 		cmd = malloc(sizeof *cmd);
    287   1.1  augustss 		if (cmd == NULL)
    288   1.1  augustss 			err(1, "malloc failed");
    289   1.7  augustss 		cmd->next = cmds;
    290   1.7  augustss 		cmds = cmd;
    291   1.1  augustss 		cmd->line = line;
    292   1.1  augustss 
    293   1.1  augustss 		if (strcmp(value, "*") == 0) {
    294   1.1  augustss 			cmd->anyvalue = 1;
    295   1.1  augustss 		} else {
    296   1.1  augustss 			cmd->anyvalue = 0;
    297   1.7  augustss 			if (sscanf(value, "%d", &cmd->value) != 1) {
    298   1.7  augustss 				if (isdemon) {
    299   1.7  augustss 					syslog(LOG_WARNING,
    300   1.7  augustss 					       "config file `%s', line %d, "
    301   1.7  augustss 					       "bad value: %s\n",
    302   1.7  augustss 					       conf, line, value);
    303   1.7  augustss 					freecommands(cmds);
    304  1.20  christos 					fclose(f);
    305   1.7  augustss 					return (NULL);
    306   1.7  augustss 				} else {
    307   1.7  augustss 					errx(1, "config file `%s', line %d, "
    308   1.7  augustss 					     "bad value: %s\n",
    309   1.7  augustss 					     conf, line, value);
    310   1.7  augustss 				}
    311   1.7  augustss 			}
    312   1.1  augustss 		}
    313   1.1  augustss 
    314   1.1  augustss 		coll[0] = 0;
    315   1.4  augustss 		for (d = hid_start_parse(repd, 1 << hid_input, reportid);
    316   1.1  augustss 		     hid_get_item(d, &h); ) {
    317   1.1  augustss 			if (verbose > 2)
    318  1.13  augustss 				printf("kind=%d usage=%x flags=%x\n",
    319  1.13  augustss 				       h.kind, h.usage, h.flags);
    320   1.1  augustss 			switch (h.kind) {
    321   1.1  augustss 			case hid_input:
    322  1.15  augustss 				if (h.flags & HIO_CONST)
    323  1.15  augustss 					continue;
    324   1.6  augustss 				if (h.usage_minimum != 0 ||
    325   1.6  augustss 				    h.usage_maximum != 0) {
    326   1.6  augustss 					lo = h.usage_minimum;
    327   1.6  augustss 					hi = h.usage_maximum;
    328   1.6  augustss 					range = 1;
    329   1.6  augustss 				} else {
    330   1.6  augustss 					lo = h.usage;
    331   1.6  augustss 					hi = h.usage;
    332   1.6  augustss 					range = 0;
    333   1.6  augustss 				}
    334   1.6  augustss 				for (u = lo; u <= hi; u++) {
    335   1.6  augustss 					snprintf(usage, sizeof usage,  "%s:%s",
    336   1.6  augustss 						 hid_usage_page(HID_PAGE(u)),
    337   1.6  augustss 						 hid_usage_in_page(u));
    338   1.1  augustss 					if (verbose > 2)
    339   1.1  augustss 						printf("usage %s\n", usage);
    340   1.6  augustss 					if (!strcasecmp(usage, name))
    341   1.1  augustss 						goto foundhid;
    342   1.6  augustss 					if (coll[0]) {
    343   1.6  augustss 						snprintf(usage, sizeof usage,
    344   1.6  augustss 						  "%s.%s:%s", coll+1,
    345   1.6  augustss 						  hid_usage_page(HID_PAGE(u)),
    346   1.6  augustss 						  hid_usage_in_page(u));
    347   1.6  augustss 						if (verbose > 2)
    348   1.6  augustss 							printf("usage %s\n",
    349   1.6  augustss 							       usage);
    350   1.6  augustss 						if (!strcasecmp(usage, name))
    351   1.6  augustss 							goto foundhid;
    352   1.6  augustss 					}
    353   1.1  augustss 				}
    354   1.1  augustss 				break;
    355   1.1  augustss 			case hid_collection:
    356   1.1  augustss 				snprintf(coll + strlen(coll),
    357   1.1  augustss 				    sizeof coll - strlen(coll),  ".%s:%s",
    358   1.1  augustss 				    hid_usage_page(HID_PAGE(h.usage)),
    359   1.1  augustss 				    hid_usage_in_page(h.usage));
    360  1.13  augustss 				if (verbose > 2)
    361  1.13  augustss 					printf("coll '%s'\n", coll);
    362   1.1  augustss 				break;
    363   1.1  augustss 			case hid_endcollection:
    364   1.1  augustss 				if (coll[0])
    365   1.1  augustss 					*strrchr(coll, '.') = 0;
    366   1.1  augustss 				break;
    367   1.1  augustss 			default:
    368   1.1  augustss 				break;
    369   1.1  augustss 			}
    370   1.1  augustss 		}
    371   1.1  augustss 		if (ignore) {
    372   1.1  augustss 			if (verbose)
    373   1.8    itojun 				warnx("ignore item '%s'", name);
    374   1.1  augustss 			continue;
    375   1.1  augustss 		}
    376   1.7  augustss 		if (isdemon) {
    377   1.7  augustss 			syslog(LOG_WARNING, "config file `%s', line %d, HID "
    378  1.12       mrg 			       "item not found: `%s'", conf, line, name);
    379   1.7  augustss 			freecommands(cmds);
    380  1.20  christos 			fclose(f);
    381   1.7  augustss 			return (NULL);
    382   1.7  augustss 		} else {
    383   1.7  augustss 			errx(1, "config file `%s', line %d, HID item "
    384  1.12       mrg 			     "not found: `%s'", conf, line, name);
    385   1.7  augustss 		}
    386   1.1  augustss 
    387   1.1  augustss 	foundhid:
    388   1.1  augustss 		hid_end_parse(d);
    389   1.1  augustss 		cmd->item = h;
    390   1.1  augustss 		cmd->name = strdup(name);
    391   1.1  augustss 		cmd->action = strdup(action);
    392   1.6  augustss 		if (range) {
    393   1.6  augustss 			if (cmd->value == 1)
    394   1.6  augustss 				cmd->value = u - lo;
    395   1.6  augustss 			else
    396   1.6  augustss 				cmd->value = -1;
    397   1.6  augustss 		}
    398   1.1  augustss 
    399   1.1  augustss 		if (verbose)
    400   1.1  augustss 			printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
    401   1.1  augustss 			       cmd->value, cmd->action);
    402   1.1  augustss 	}
    403   1.1  augustss 	fclose(f);
    404   1.7  augustss 	return (cmds);
    405   1.1  augustss }
    406   1.1  augustss 
    407   1.1  augustss void
    408   1.1  augustss docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
    409   1.1  augustss {
    410   1.1  augustss 	char cmdbuf[SIZE], *p, *q;
    411   1.1  augustss 	size_t len;
    412   1.1  augustss 	int n, r;
    413   1.1  augustss 
    414   1.1  augustss 	for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
    415   1.1  augustss 		if (*p == '$') {
    416   1.1  augustss 			p++;
    417   1.1  augustss 			len = &cmdbuf[SIZE-1] - q;
    418  1.17       dsl 			if (isdigit((unsigned char)*p)) {
    419   1.1  augustss 				n = strtol(p, &p, 10) - 1;
    420   1.1  augustss 				if (n >= 0 && n < argc) {
    421   1.1  augustss 					strncpy(q, argv[n], len);
    422   1.1  augustss 					q += strlen(q);
    423   1.1  augustss 				}
    424   1.1  augustss 			} else if (*p == 'V') {
    425   1.1  augustss 				p++;
    426   1.1  augustss 				snprintf(q, len, "%d", value);
    427   1.1  augustss 				q += strlen(q);
    428   1.1  augustss 			} else if (*p == 'N') {
    429   1.1  augustss 				p++;
    430   1.1  augustss 				strncpy(q, cmd->name, len);
    431   1.1  augustss 				q += strlen(q);
    432   1.1  augustss 			} else if (*p == 'H') {
    433   1.1  augustss 				p++;
    434   1.1  augustss 				strncpy(q, hid, len);
    435   1.1  augustss 				q += strlen(q);
    436   1.1  augustss 			} else if (*p) {
    437   1.1  augustss 				*q++ = *p++;
    438   1.1  augustss 			}
    439   1.1  augustss 		} else {
    440   1.1  augustss 			*q++ = *p++;
    441   1.1  augustss 		}
    442   1.1  augustss 	}
    443   1.1  augustss 	*q = 0;
    444   1.1  augustss 
    445   1.1  augustss 	if (verbose)
    446   1.1  augustss 		printf("system '%s'\n", cmdbuf);
    447   1.1  augustss 	r = system(cmdbuf);
    448   1.1  augustss 	if (verbose > 1 && r)
    449   1.1  augustss 		printf("return code = 0x%x\n", r);
    450   1.7  augustss }
    451   1.7  augustss 
    452   1.7  augustss void
    453   1.7  augustss freecommands(struct command *cmd)
    454   1.7  augustss {
    455   1.7  augustss 	struct command *next;
    456   1.7  augustss 
    457   1.7  augustss 	while (cmd) {
    458   1.7  augustss 		next = cmd->next;
    459   1.7  augustss 		free(cmd);
    460   1.7  augustss 		cmd = next;
    461   1.7  augustss 	}
    462   1.1  augustss }
    463