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);
77 version(int ac, char **av)
80 printf("%s: version %s", MPSUTIL_VERSION, getprogname());
88 MPS_COMMAND(top, version, version, "", "version")
91 main(int ac, char **av)
93 struct mpsutil_command **cmd;
96 is_mps = !strcmp(getprogname(), "mpsutil");
98 while ((ch = getopt(ac, av, "u:h?")) != -1) {
101 mps_unit = atoi(optarg);
113 /* getopt() eats av[0], so we can't use mpt_table_handler() directly. */
119 SET_FOREACH(cmd, MPS_DATASET(top)) {
120 if (strcmp((*cmd)->name, av[0]) == 0) {
121 if ((*cmd)->handler(ac, av))
127 warnx("Unknown command %s.", av[0]);
132 mps_table_handler(struct mpsutil_command **start, struct mpsutil_command **end,
135 struct mpsutil_command **cmd;
138 warnx("The %s command requires a sub-command.", av[0]);
141 for (cmd = start; cmd < end; cmd++) {
142 if (strcmp((*cmd)->name, av[1]) == 0)
143 return ((*cmd)->handler(ac - 1, av + 1));
146 warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
151 hexdump(const void *ptr, int length, const char *hdr, int flags)
155 const unsigned char *cp;
158 if ((flags & HD_DELIM_MASK) != 0)
159 delim = (flags & HD_DELIM_MASK) >> 8;
163 if ((flags & HD_COLUMN_MASK) != 0)
164 cols = flags & HD_COLUMN_MASK;
169 for (i = 0; i < length; i+= cols) {
173 if ((flags & HD_OMIT_COUNT) == 0)
176 if ((flags & HD_OMIT_HEX) == 0) {
177 for (j = 0; j < cols; j++) {
178 if (flags & HD_REVERSED)
179 k = i + (cols - 1 - j);
183 printf("%c%02x", delim, cp[k]);
189 if ((flags & HD_OMIT_CHARS) == 0) {
191 for (j = 0; j < cols; j++) {
192 if (flags & HD_REVERSED)
193 k = i + (cols - 1 - j);
198 else if (cp[k] >= ' ' && cp[k] <= '~')