2 * Copyright (c) 2015 Netflix, Inc.
3 * Written by: Scott Long <scottl@freebsd.org>
5 * Copyright (c) 2008 Yahoo!, Inc.
7 * Written by: John Baldwin <jhb@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/errno.h>
45 SET_DECLARE(MPS_DATASET(top), struct mpsutil_command);
46 SET_DECLARE(MPS_DATASET(usage), struct mpsutil_usage);
54 struct mpsutil_usage **cmd;
55 const char *args, *desc;
57 fprintf(stderr, "usage: %s [-u unit] <command> ...\n\n", getprogname());
58 fprintf(stderr, "Commands include:\n");
59 SET_FOREACH(cmd, MPS_DATASET(usage)) {
61 fprintf(stderr, "\n");
63 (*cmd)->handler(&args, &desc);
64 if (strncmp((*cmd)->set, "top", 3) == 0)
65 fprintf(stderr, "%-16s %-28s%s\n",
66 (*cmd)->name, args, desc);
68 fprintf(stderr, "%-5s %-10s %-28s%s\n",
69 (*cmd)->set, (*cmd)->name, args, desc);
76 version(int ac, char **av)
79 printf("%s: version %s", MPSUTIL_VERSION, getprogname());
87 MPS_COMMAND(top, version, version, "", "Version number")
90 main(int ac, char **av)
92 struct mpsutil_command **cmd;
97 is_mps = !strcmp(getprogname(), "mpsutil");
99 while ((ch = getopt(ac, av, "u:h?")) != -1) {
102 if (strncmp(optarg, _PATH_DEV, strlen(_PATH_DEV)) == 0) {
103 optarg += strlen(_PATH_DEV);
104 if (strncmp(optarg, is_mps ? "mps" : "mpr", 3) != 0)
105 errx(1, "Invalid device: %s", optarg);
107 if (strncmp(optarg, is_mps ? "mps" : "mpr", 3) == 0)
109 unit = strtoumax(optarg, &end, 10);
110 if (*end != '\0' || unit == UINTMAX_MAX || unit > INT_MAX)
111 errx(1, "Invalid unit: %s", optarg);
124 /* getopt() eats av[0], so we can't use mpt_table_handler() directly. */
130 SET_FOREACH(cmd, MPS_DATASET(top)) {
131 if (strcmp((*cmd)->name, av[0]) == 0) {
132 if ((*cmd)->handler(ac, av))
138 warnx("Unknown command %s.", av[0]);
143 mps_table_handler(struct mpsutil_command **start, struct mpsutil_command **end,
146 struct mpsutil_command **cmd;
149 warnx("The %s command requires a sub-command.", av[0]);
152 for (cmd = start; cmd < end; cmd++) {
153 if (strcmp((*cmd)->name, av[1]) == 0)
154 return ((*cmd)->handler(ac - 1, av + 1));
157 warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
162 hexdump(const void *ptr, int length, const char *hdr, int flags)
166 const unsigned char *cp;
169 if ((flags & HD_DELIM_MASK) != 0)
170 delim = (flags & HD_DELIM_MASK) >> 8;
174 if ((flags & HD_COLUMN_MASK) != 0)
175 cols = flags & HD_COLUMN_MASK;
180 for (i = 0; i < length; i+= cols) {
184 if ((flags & HD_OMIT_COUNT) == 0)
187 if ((flags & HD_OMIT_HEX) == 0) {
188 for (j = 0; j < cols; j++) {
189 if (flags & HD_REVERSED)
190 k = i + (cols - 1 - j);
194 printf("%c%02x", delim, cp[k]);
200 if ((flags & HD_OMIT_CHARS) == 0) {
202 for (j = 0; j < cols; j++) {
203 if (flags & HD_REVERSED)
204 k = i + (cols - 1 - j);
209 else if (cp[k] >= ' ' && cp[k] <= '~')
220 #define PCHAR(c) { if (retval < tmpsz) { *outbuf++ = (c); retval++; } }
223 mps_parse_flags(uintmax_t num, const char *q, char *outbuf, int tmpsz)
225 int n, tmp, retval = 0;
230 /* %b conversion flag format. */
234 if (num & (1 << (n - 1))) {
235 PCHAR(retval != tmp ? ',' : '<');
236 for (; (n = *q) > ' '; ++q)
239 for (; *q > ' '; ++q)