2 * Copyright (c) 2001, 2002 Dima Dorfman.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/queue.h>
51 static ctbl_t ctbl_main = {
52 { "rule", rule_main },
53 { "ruleset", ruleset_main },
58 main(int ac, char **av)
65 while ((ch = getopt(ac, av, "m:")) != -1)
80 mpfd = open(mountpt, O_RDONLY);
82 err(1, "open: %s", mountpt);
84 for (c = ctbl_main; c->name != NULL; ++c)
85 if (strcmp(c->name, av[0]) == 0)
86 exit((*c->handler)(ac, av));
87 errx(1, "unknown command: %s", av[0]);
91 * Convert an integer to a "number" (ruleset numbers and rule numbers
92 * are 16-bit). If the conversion is successful, num contains the
93 * integer representation of s and 1 is returned; otherwise, 0 is
94 * returned and num is unchanged.
97 atonum(const char *s, uint16_t *num)
102 ul = strtoul(s, &cp, 10);
103 if (ul > UINT16_MAX || *cp != '\0')
110 * Convert user input in ASCII to an integer.
118 l = strtol(s, &cp, 10);
119 if (l > INT_MAX || *cp != '\0')
120 errx(1, "error converting to integer: %s", s);
125 * As atonum(), but the result of failure is death.
128 eatonum(const char *s)
132 if (!atonum(s, &num))
133 errx(1, "error converting to number: %s", s); /* XXX clarify */
138 * Read a line from a /FILE/. If the return value isn't 0, it is the
139 * length of the line, a pointer to which exists in /line/. It is the
140 * caller's responsibility to free(3) it. If the return value is 0,
141 * there was an error or we reached EOF, and /line/ is undefined (so,
142 * obviously, the caller shouldn't try to free(3) it).
145 efgetln(FILE *fp, char **line)
150 cp = fgetln(fp, &rv);
155 if (cp[rv - 1] == '\n') {
159 errx(1, "cannot allocate memory");
162 *line = malloc(rv + 1);
164 errx(1, "cannot allocate memory");
165 memcpy(*line, cp, rv);
168 assert(rv == strlen(*line));
173 STAILQ_ENTRY(ptrstq) tq;
178 * Create an argument vector from /line/. The caller must free(3)
179 * /avp/, and /avp[0]/ when the argument vector is no longer
180 * needed unless /acp/ is 0, in which case /avp/ is undefined.
181 * /avp/ is NULL-terminated, so it is actually one longer than /acp/.
184 tokenize(const char *line, int *acp, char ***avp)
186 static const char *delims = " \t\n";
188 STAILQ_HEAD(, ptrstq) plist;
189 char **ap, *cp, *wline, *xcp;
191 line += strspn(line, delims);
192 wline = strdup(line);
194 errx(1, "cannot allocate memory");
197 for (xcp = wline, *acp = 0;
198 (cp = strsep(&xcp, delims)) != NULL;)
200 pt = calloc(1, sizeof(*pt));
202 errx(1, "cannot allocate memory");
204 STAILQ_INSERT_TAIL(&plist, pt, tq);
209 assert(STAILQ_FIRST(&plist)->ptr == wline);
210 *avp = malloc(sizeof(**avp) * (*acp + 1));
212 errx(1, "cannot allocate memory");
213 for (ap = *avp; !STAILQ_EMPTY(&plist);) {
214 pt = STAILQ_FIRST(&plist);
217 assert(ap <= *avp + (*acp));
218 STAILQ_REMOVE_HEAD(&plist, tq);
228 fprintf(stderr, "usage: %s\n%s\n",
229 "\tdevfs [-m mount-point] [-s ruleset] rule ...",
230 "\tdevfs [-m mount-point] ruleset ...");