2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001, 2002 Dima Dorfman.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/queue.h>
53 static ctbl_t ctbl_main = {
54 { "rule", rule_main },
55 { "ruleset", ruleset_main },
60 main(int ac, char **av)
67 while ((ch = getopt(ac, av, "m:")) != -1)
82 mpfd = open(mountpt, O_RDONLY);
84 err(1, "open: %s", mountpt);
86 for (c = ctbl_main; c->name != NULL; ++c)
87 if (strcmp(c->name, av[0]) == 0)
88 exit((*c->handler)(ac, av));
89 errx(1, "unknown command: %s", av[0]);
93 * Convert an integer to a "number" (ruleset numbers and rule numbers
94 * are 16-bit). If the conversion is successful, num contains the
95 * integer representation of s and 1 is returned; otherwise, 0 is
96 * returned and num is unchanged.
99 atonum(const char *s, uint16_t *num)
104 ul = strtoul(s, &cp, 10);
105 if (ul > UINT16_MAX || *cp != '\0')
112 * Convert user input in ASCII to an integer.
120 l = strtol(s, &cp, 10);
121 if (l > INT_MAX || *cp != '\0')
122 errx(1, "error converting to integer: %s", s);
127 * As atonum(), but the result of failure is death.
130 eatonum(const char *s)
134 if (!atonum(s, &num))
135 errx(1, "error converting to number: %s", s); /* XXX clarify */
140 * Read a line from a /FILE/. If the return value isn't 0, it is the
141 * length of the line, a pointer to which exists in /line/. It is the
142 * caller's responsibility to free(3) it. If the return value is 0,
143 * there was an error or we reached EOF, and /line/ is undefined (so,
144 * obviously, the caller shouldn't try to free(3) it).
147 efgetln(FILE *fp, char **line)
152 cp = fgetln(fp, &rv);
157 if (cp[rv - 1] == '\n') {
161 errx(1, "cannot allocate memory");
164 *line = malloc(rv + 1);
166 errx(1, "cannot allocate memory");
167 memcpy(*line, cp, rv);
170 assert(rv == strlen(*line));
175 STAILQ_ENTRY(ptrstq) tq;
180 * Create an argument vector from /line/. The caller must free(3)
181 * /avp/, and /avp[0]/ when the argument vector is no longer
182 * needed unless /acp/ is 0, in which case /avp/ is undefined.
183 * /avp/ is NULL-terminated, so it is actually one longer than /acp/.
186 tokenize(const char *line, int *acp, char ***avp)
188 static const char *delims = " \t\n";
190 STAILQ_HEAD(, ptrstq) plist;
191 char **ap, *cp, *wline, *xcp;
193 line += strspn(line, delims);
194 wline = strdup(line);
196 errx(1, "cannot allocate memory");
199 for (xcp = wline, *acp = 0;
200 (cp = strsep(&xcp, delims)) != NULL;)
202 pt = calloc(1, sizeof(*pt));
204 errx(1, "cannot allocate memory");
206 STAILQ_INSERT_TAIL(&plist, pt, tq);
211 assert(STAILQ_FIRST(&plist)->ptr == wline);
212 *avp = malloc(sizeof(**avp) * (*acp + 1));
214 errx(1, "cannot allocate memory");
215 for (ap = *avp; !STAILQ_EMPTY(&plist);) {
216 pt = STAILQ_FIRST(&plist);
219 assert(ap <= *avp + (*acp));
220 STAILQ_REMOVE_HEAD(&plist, tq);
230 fprintf(stderr, "usage: %s\n%s\n",
231 "\tdevfs [-m mount-point] [-s ruleset] rule ...",
232 "\tdevfs [-m mount-point] ruleset ...");