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/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/errno.h>
46 SET_DECLARE(MPS_DATASET(top), struct mpsutil_command);
47 SET_DECLARE(MPS_DATASET(usage), struct mpsutil_usage);
55 struct mpsutil_usage **cmd;
56 const char *args, *desc;
58 fprintf(stderr, "usage: %s [-u unit] <command> ...\n\n", getprogname());
59 fprintf(stderr, "Commands include:\n");
60 SET_FOREACH(cmd, MPS_DATASET(usage)) {
62 fprintf(stderr, "\n");
64 (*cmd)->handler(&args, &desc);
65 if (strncmp((*cmd)->set, "top", 3) == 0)
66 fprintf(stderr, "%-16s %-28s%s\n",
67 (*cmd)->name, args, desc);
69 fprintf(stderr, "%-5s %-10s %-28s%s\n",
70 (*cmd)->set, (*cmd)->name, args, desc);
77 version(int ac, char **av)
80 printf("%s: version %s", MPSUTIL_VERSION, getprogname());
88 MPS_COMMAND(top, version, version, "", "Version number")
91 main(int ac, char **av)
93 struct mpsutil_command **cmd;
98 is_mps = !strcmp(getprogname(), "mpsutil");
100 while ((ch = getopt(ac, av, "u:h?")) != -1) {
103 if (strncmp(optarg, _PATH_DEV, strlen(_PATH_DEV)) == 0) {
104 optarg += strlen(_PATH_DEV);
105 if (strncmp(optarg, is_mps ? "mps" : "mpr", 3) != 0)
106 errx(1, "Invalid device: %s", optarg);
108 if (strncmp(optarg, is_mps ? "mps" : "mpr", 3) == 0)
110 unit = strtoumax(optarg, &end, 10);
111 if (*end != '\0' || unit == UINTMAX_MAX || unit > INT_MAX)
112 errx(1, "Invalid unit: %s", optarg);
125 /* getopt() eats av[0], so we can't use mpt_table_handler() directly. */
131 SET_FOREACH(cmd, MPS_DATASET(top)) {
132 if (strcmp((*cmd)->name, av[0]) == 0) {
133 if ((*cmd)->handler(ac, av))
139 warnx("Unknown command %s.", av[0]);
144 mps_table_handler(struct mpsutil_command **start, struct mpsutil_command **end,
147 struct mpsutil_command **cmd;
150 warnx("The %s command requires a sub-command.", av[0]);
153 for (cmd = start; cmd < end; cmd++) {
154 if (strcmp((*cmd)->name, av[1]) == 0)
155 return ((*cmd)->handler(ac - 1, av + 1));
158 warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
163 hexdump(const void *ptr, int length, const char *hdr, int flags)
167 const unsigned char *cp;
170 if ((flags & HD_DELIM_MASK) != 0)
171 delim = (flags & HD_DELIM_MASK) >> 8;
175 if ((flags & HD_COLUMN_MASK) != 0)
176 cols = flags & HD_COLUMN_MASK;
181 for (i = 0; i < length; i+= cols) {
185 if ((flags & HD_OMIT_COUNT) == 0)
188 if ((flags & HD_OMIT_HEX) == 0) {
189 for (j = 0; j < cols; j++) {
190 if (flags & HD_REVERSED)
191 k = i + (cols - 1 - j);
195 printf("%c%02x", delim, cp[k]);
201 if ((flags & HD_OMIT_CHARS) == 0) {
203 for (j = 0; j < cols; j++) {
204 if (flags & HD_REVERSED)
205 k = i + (cols - 1 - j);
210 else if (cp[k] >= ' ' && cp[k] <= '~')
221 #define PCHAR(c) { if (retval < tmpsz) { *outbuf++ = (c); retval++; } }
224 mps_parse_flags(uintmax_t num, const char *q, char *outbuf, int tmpsz)
226 int n, tmp, retval = 0;
231 /* %b conversion flag format. */
235 if (num & (1 << (n - 1))) {
236 PCHAR(retval != tmp ? ',' : '<');
237 for (; (n = *q) > ' '; ++q)
240 for (; *q > ' '; ++q)