1/*-
2 * Copyright (c) 2015 Netflix, Inc.
3 * Written by: Scott Long <scottl@freebsd.org>
4 *
5 * Copyright (c) 2008 Yahoo!, Inc.
6 * All rights reserved.
7 * Written by: John Baldwin <jhb@FreeBSD.org>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__RCSID("$FreeBSD$");
36
37#include <sys/param.h>
38#include <sys/errno.h>
39#include <err.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44#include "mpsutil.h"
45
46SET_DECLARE(MPS_DATASET(top), struct mpsutil_command);
47SET_DECLARE(MPS_DATASET(usage), struct mpsutil_usage);
48
49int mps_unit;
50int is_mps;
51
52static void
53usage(void)
54{
55	struct mpsutil_usage **cmd;
56	const char *args, *desc;
57
58	fprintf(stderr, "usage: %s [-u unit] <command> ...\n\n", getprogname());
59	fprintf(stderr, "Commands include:\n");
60	SET_FOREACH(cmd, MPS_DATASET(usage)) {
61		if (*cmd == NULL) {
62			fprintf(stderr, "\n");
63		} else {
64			(*cmd)->handler(&args, &desc);
65			if (strncmp((*cmd)->set, "top", 3) == 0)
66				fprintf(stderr, "%s %-30s\t%s\n",
67				    (*cmd)->name, args, desc);
68			else
69				fprintf(stderr, "%s %s %-30s\t%s\n",
70				    (*cmd)->set, (*cmd)->name, args, desc);
71		}
72	}
73	exit(1);
74}
75
76static int
77version(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
88MPS_COMMAND(top, version, version, "", "version")
89
90int
91main(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
131int
132mps_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
150void
151hexdump(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
209#define PCHAR(c) { if (retval < tmpsz) { *outbuf++ = (c); retval++; } }
210
211int
212mps_parse_flags(uintmax_t num, const char *q, char *outbuf, int tmpsz)
213{
214	int n, tmp, retval = 0;
215
216	if (num == 0)
217		return (retval);
218
219	/* %b conversion flag format. */
220	tmp = retval;
221	while (*q) {
222		n = *q++;
223		if (num & (1 << (n - 1))) {
224			PCHAR(retval != tmp ?  ',' : '<');
225			for (; (n = *q) > ' '; ++q)
226				PCHAR(n);
227		} else
228			for (; *q > ' '; ++q)
229				continue;
230	}
231	if (retval != tmp)
232		PCHAR('>');
233
234	return (retval);
235}
236
237