2 * Copyright (c) 2015 Netflix, Inc.
4 * Written by: Scott Long <scottl@freebsd.org>
6 * Copyright (c) 2008 Yahoo!, Inc.
8 * Written by: John Baldwin <jhb@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/errno.h>
47 SET_DECLARE(MPS_DATASET(top), struct mpsutil_command);
48 SET_DECLARE(MPS_DATASET(usage), struct mpsutil_usage);
56 struct mpsutil_usage **cmd;
57 const char *args, *desc;
59 fprintf(stderr, "usage: %s [-u unit] <command> ...\n\n", getprogname());
60 fprintf(stderr, "Commands include:\n");
61 SET_FOREACH(cmd, MPS_DATASET(usage)) {
63 fprintf(stderr, "\n");
65 (*cmd)->handler(&args, &desc);
66 if (strncmp((*cmd)->set, "top", 3) == 0)
67 fprintf(stderr, "%s %-30s\t%s\n",
68 (*cmd)->name, args, desc);
70 fprintf(stderr, "%s %s %-30s\t%s\n",
71 (*cmd)->set, (*cmd)->name, args, desc);
78 version(int ac, char **av)
81 printf("%s: version %s", MPSUTIL_VERSION, getprogname());
89 MPS_COMMAND(top, version, version, "", "version")
92 main(int ac, char **av)
94 struct mpsutil_command **cmd;
97 is_mps = !strcmp(getprogname(), "mpsutil");
99 while ((ch = getopt(ac, av, "u:h?")) != -1) {
102 mps_unit = atoi(optarg);
114 /* getopt() eats av[0], so we can't use mpt_table_handler() directly. */
120 SET_FOREACH(cmd, MPS_DATASET(top)) {
121 if (strcmp((*cmd)->name, av[0]) == 0) {
122 if ((*cmd)->handler(ac, av))
128 warnx("Unknown command %s.", av[0]);
133 mps_table_handler(struct mpsutil_command **start, struct mpsutil_command **end,
136 struct mpsutil_command **cmd;
139 warnx("The %s command requires a sub-command.", av[0]);
142 for (cmd = start; cmd < end; cmd++) {
143 if (strcmp((*cmd)->name, av[1]) == 0)
144 return ((*cmd)->handler(ac - 1, av + 1));
147 warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
152 hexdump(const void *ptr, int length, const char *hdr, int flags)
156 const unsigned char *cp;
159 if ((flags & HD_DELIM_MASK) != 0)
160 delim = (flags & HD_DELIM_MASK) >> 8;
164 if ((flags & HD_COLUMN_MASK) != 0)
165 cols = flags & HD_COLUMN_MASK;
170 for (i = 0; i < length; i+= cols) {
174 if ((flags & HD_OMIT_COUNT) == 0)
177 if ((flags & HD_OMIT_HEX) == 0) {
178 for (j = 0; j < cols; j++) {
179 if (flags & HD_REVERSED)
180 k = i + (cols - 1 - j);
184 printf("%c%02x", delim, cp[k]);
190 if ((flags & HD_OMIT_CHARS) == 0) {
192 for (j = 0; j < cols; j++) {
193 if (flags & HD_REVERSED)
194 k = i + (cols - 1 - j);
199 else if (cp[k] >= ' ' && cp[k] <= '~')
210 #define PCHAR(c) { if (retval < tmpsz) { *outbuf++ = (c); retval++; } }
213 mps_parse_flags(uintmax_t num, const char *q, char *outbuf, int tmpsz)
215 int n, tmp, retval = 0;
220 /* %b conversion flag format. */
224 if (num & (1 << (n - 1))) {
225 PCHAR(retval != tmp ? ',' : '<');
226 for (; (n = *q) > ' '; ++q)
229 for (; *q > ' '; ++q)