1 /*- 2 * Copyright (c) 2015 Netflix, Inc. 3 * All rights reserved. 4 * Written by: Scott Long <scottl@freebsd.org> 5 * 6 * Copyright (c) 2008 Yahoo!, Inc. 7 * All rights reserved. 8 * Written by: John Baldwin <jhb@FreeBSD.org> 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __RCSID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/errno.h> 40 #include <err.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include "mpsutil.h" 46 47 SET_DECLARE(MPS_DATASET(top), struct mpsutil_command); 48 SET_DECLARE(MPS_DATASET(usage), struct mpsutil_usage); 49 50 int mps_unit; 51 int is_mps; 52 53 static void 54 usage(void) 55 { 56 struct mpsutil_usage **cmd; 57 const char *args, *desc; 58 59 fprintf(stderr, "usage: %s [-u unit] <command> ...\n\n", getprogname()); 60 fprintf(stderr, "Commands include:\n"); 61 SET_FOREACH(cmd, MPS_DATASET(usage)) { 62 if (*cmd == NULL) 63 fprintf(stderr, "\n"); 64 else 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); 69 else 70 fprintf(stderr, "%s %s %-30s\t%s\n", 71 (*cmd)->set, (*cmd)->name, args, desc); 72 } 73 exit(1); 74 } 75 76 static int 77 version(int ac, char **av) 78 { 79 80 printf("%s: version %s", MPSUTIL_VERSION, getprogname()); 81 #ifdef DEBUG 82 printf(" (DEBUG)"); 83 #endif 84 printf("\n"); 85 return (0); 86 } 87 88 MPS_COMMAND(top, version, version, "", "version") 89 90 int 91 main(int ac, char **av) 92 { 93 struct mpsutil_command **cmd; 94 int ch; 95 96 is_mps = !strcmp(getprogname(), "mpsutil"); 97 98 while ((ch = getopt(ac, av, "u:h?")) != -1) { 99 switch (ch) { 100 case 'u': 101 mps_unit = atoi(optarg); 102 break; 103 case 'h': 104 case '?': 105 usage(); 106 return (1); 107 } 108 } 109 110 av += optind; 111 ac -= optind; 112 113 /* getopt() eats av[0], so we can't use mpt_table_handler() directly. */ 114 if (ac == 0) { 115 usage(); 116 return (1); 117 } 118 119 SET_FOREACH(cmd, MPS_DATASET(top)) { 120 if (strcmp((*cmd)->name, av[0]) == 0) { 121 if ((*cmd)->handler(ac, av)) 122 return (1); 123 else 124 return (0); 125 } 126 } 127 warnx("Unknown command %s.", av[0]); 128 return (1); 129 } 130 131 int 132 mps_table_handler(struct mpsutil_command **start, struct mpsutil_command **end, 133 int ac, char **av) 134 { 135 struct mpsutil_command **cmd; 136 137 if (ac < 2) { 138 warnx("The %s command requires a sub-command.", av[0]); 139 return (EINVAL); 140 } 141 for (cmd = start; cmd < end; cmd++) { 142 if (strcmp((*cmd)->name, av[1]) == 0) 143 return ((*cmd)->handler(ac - 1, av + 1)); 144 } 145 146 warnx("%s is not a valid sub-command of %s.", av[1], av[0]); 147 return (ENOENT); 148 } 149 150 void 151 hexdump(const void *ptr, int length, const char *hdr, int flags) 152 { 153 int i, j, k; 154 int cols; 155 const unsigned char *cp; 156 char delim; 157 158 if ((flags & HD_DELIM_MASK) != 0) 159 delim = (flags & HD_DELIM_MASK) >> 8; 160 else 161 delim = ' '; 162 163 if ((flags & HD_COLUMN_MASK) != 0) 164 cols = flags & HD_COLUMN_MASK; 165 else 166 cols = 16; 167 168 cp = ptr; 169 for (i = 0; i < length; i+= cols) { 170 if (hdr != NULL) 171 printf("%s", hdr); 172 173 if ((flags & HD_OMIT_COUNT) == 0) 174 printf("%04x ", i); 175 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); 180 else 181 k = i + j; 182 if (k < length) 183 printf("%c%02x", delim, cp[k]); 184 else 185 printf(" "); 186 } 187 } 188 189 if ((flags & HD_OMIT_CHARS) == 0) { 190 printf(" |"); 191 for (j = 0; j < cols; j++) { 192 if (flags & HD_REVERSED) 193 k = i + (cols - 1 - j); 194 else 195 k = i + j; 196 if (k >= length) 197 printf(" "); 198 else if (cp[k] >= ' ' && cp[k] <= '~') 199 printf("%c", cp[k]); 200 else 201 printf("."); 202 } 203 printf("|"); 204 } 205 printf("\n"); 206 } 207 } 208