1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_types.h>
28*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_argvec.h>
29*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_string.h>
30*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
31*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_stdlib.h>
32*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
33*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_frame.h>
34*7c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <alloca.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #define	AV_DEFSZ	16	/* Initial size of argument vector */
39*7c478bd9Sstevel@tonic-gate #define	AV_GROW		2	/* Multiplier for growing argument vector */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate void
mdb_argvec_create(mdb_argvec_t * vec)42*7c478bd9Sstevel@tonic-gate mdb_argvec_create(mdb_argvec_t *vec)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	vec->a_data = NULL;
45*7c478bd9Sstevel@tonic-gate 	vec->a_nelems = 0;
46*7c478bd9Sstevel@tonic-gate 	vec->a_size = 0;
47*7c478bd9Sstevel@tonic-gate }
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate void
mdb_argvec_destroy(mdb_argvec_t * vec)50*7c478bd9Sstevel@tonic-gate mdb_argvec_destroy(mdb_argvec_t *vec)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	if (vec->a_data != NULL) {
53*7c478bd9Sstevel@tonic-gate 		mdb_argvec_reset(vec);
54*7c478bd9Sstevel@tonic-gate 		mdb_free(vec->a_data, sizeof (mdb_arg_t) * vec->a_size);
55*7c478bd9Sstevel@tonic-gate 	}
56*7c478bd9Sstevel@tonic-gate }
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate void
mdb_argvec_append(mdb_argvec_t * vec,const mdb_arg_t * arg)59*7c478bd9Sstevel@tonic-gate mdb_argvec_append(mdb_argvec_t *vec, const mdb_arg_t *arg)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	if (vec->a_nelems >= vec->a_size) {
62*7c478bd9Sstevel@tonic-gate 		size_t size = vec->a_size ? vec->a_size * AV_GROW : AV_DEFSZ;
63*7c478bd9Sstevel@tonic-gate 		void *data = mdb_alloc(sizeof (mdb_arg_t) * size, UM_NOSLEEP);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 		if (data == NULL) {
66*7c478bd9Sstevel@tonic-gate 			warn("failed to grow argument vector");
67*7c478bd9Sstevel@tonic-gate 			longjmp(mdb.m_frame->f_pcb, MDB_ERR_NOMEM);
68*7c478bd9Sstevel@tonic-gate 		}
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 		bcopy(vec->a_data, data, sizeof (mdb_arg_t) * vec->a_size);
71*7c478bd9Sstevel@tonic-gate 		mdb_free(vec->a_data, sizeof (mdb_arg_t) * vec->a_size);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 		vec->a_data = data;
74*7c478bd9Sstevel@tonic-gate 		vec->a_size = size;
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	bcopy(arg, &vec->a_data[vec->a_nelems++], sizeof (mdb_arg_t));
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate void
mdb_argvec_reset(mdb_argvec_t * vec)81*7c478bd9Sstevel@tonic-gate mdb_argvec_reset(mdb_argvec_t *vec)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	size_t nelems = vec->a_nelems;
84*7c478bd9Sstevel@tonic-gate 	mdb_arg_t *arg;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	for (arg = vec->a_data; nelems != 0; nelems--, arg++) {
87*7c478bd9Sstevel@tonic-gate 		if (arg->a_type == MDB_TYPE_STRING && arg->a_un.a_str != NULL)
88*7c478bd9Sstevel@tonic-gate 			strfree((char *)arg->a_un.a_str);
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	vec->a_nelems = 0;
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate void
mdb_argvec_zero(mdb_argvec_t * vec)95*7c478bd9Sstevel@tonic-gate mdb_argvec_zero(mdb_argvec_t *vec)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
98*7c478bd9Sstevel@tonic-gate 	size_t i;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < vec->a_size; i++) {
101*7c478bd9Sstevel@tonic-gate 		vec->a_data[i].a_type = UMEM_UNINITIALIZED_PATTERN;
102*7c478bd9Sstevel@tonic-gate 		vec->a_data[i].a_un.a_val =
103*7c478bd9Sstevel@tonic-gate 		    ((u_longlong_t)UMEM_UNINITIALIZED_PATTERN << 32) |
104*7c478bd9Sstevel@tonic-gate 		    ((u_longlong_t)UMEM_UNINITIALIZED_PATTERN);
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate #endif
107*7c478bd9Sstevel@tonic-gate 	vec->a_nelems = 0;
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate void
mdb_argvec_copy(mdb_argvec_t * dst,const mdb_argvec_t * src)111*7c478bd9Sstevel@tonic-gate mdb_argvec_copy(mdb_argvec_t *dst, const mdb_argvec_t *src)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate 	if (src->a_nelems > dst->a_size) {
114*7c478bd9Sstevel@tonic-gate 		mdb_arg_t *data =
115*7c478bd9Sstevel@tonic-gate 		    mdb_alloc(sizeof (mdb_arg_t) * src->a_nelems, UM_NOSLEEP);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 		if (data == NULL) {
118*7c478bd9Sstevel@tonic-gate 			warn("failed to grow argument vector");
119*7c478bd9Sstevel@tonic-gate 			longjmp(mdb.m_frame->f_pcb, MDB_ERR_NOMEM);
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 		if (dst->a_data != NULL)
123*7c478bd9Sstevel@tonic-gate 			mdb_free(dst->a_data, sizeof (mdb_arg_t) * dst->a_size);
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 		dst->a_data = data;
126*7c478bd9Sstevel@tonic-gate 		dst->a_size = src->a_nelems;
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	bcopy(src->a_data, dst->a_data, sizeof (mdb_arg_t) * src->a_nelems);
130*7c478bd9Sstevel@tonic-gate 	dst->a_nelems = src->a_nelems;
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate static int
argvec_process_subopt(const mdb_opt_t * opt,const mdb_arg_t * arg)134*7c478bd9Sstevel@tonic-gate argvec_process_subopt(const mdb_opt_t *opt, const mdb_arg_t *arg)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	mdb_subopt_t *sop;
137*7c478bd9Sstevel@tonic-gate 	const char *start;
138*7c478bd9Sstevel@tonic-gate 	const char *next;
139*7c478bd9Sstevel@tonic-gate 	char error[32];
140*7c478bd9Sstevel@tonic-gate 	size_t len;
141*7c478bd9Sstevel@tonic-gate 	uint_t value = 0;
142*7c478bd9Sstevel@tonic-gate 	uint_t i;
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	start = arg->a_un.a_str;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	for (i = 0; ; i++) {
147*7c478bd9Sstevel@tonic-gate 		next = strchr(start, ',');
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		if (next == NULL)
150*7c478bd9Sstevel@tonic-gate 			len = strlen(start);
151*7c478bd9Sstevel@tonic-gate 		else
152*7c478bd9Sstevel@tonic-gate 			len = next - start;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 		/*
155*7c478bd9Sstevel@tonic-gate 		 * Record the index of the subopt if a match if found.
156*7c478bd9Sstevel@tonic-gate 		 */
157*7c478bd9Sstevel@tonic-gate 		for (sop = opt->opt_subopts; sop->sop_flag; sop++) {
158*7c478bd9Sstevel@tonic-gate 			if (strlen(sop->sop_str) == len &&
159*7c478bd9Sstevel@tonic-gate 			    strncmp(sop->sop_str, start, len) == 0) {
160*7c478bd9Sstevel@tonic-gate 				value |= sop->sop_flag;
161*7c478bd9Sstevel@tonic-gate 				sop->sop_index = i;
162*7c478bd9Sstevel@tonic-gate 				goto found;
163*7c478bd9Sstevel@tonic-gate 			}
164*7c478bd9Sstevel@tonic-gate 		}
165*7c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(error, len + 1, "%s", start);
166*7c478bd9Sstevel@tonic-gate 		warn("invalid option for -%c: \"%s\"\n", opt->opt_char, error);
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 		return (-1);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate found:
171*7c478bd9Sstevel@tonic-gate 		if (next == NULL)
172*7c478bd9Sstevel@tonic-gate 			break;
173*7c478bd9Sstevel@tonic-gate 		start = next + 1;
174*7c478bd9Sstevel@tonic-gate 	}
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	*((uint_t *)opt->opt_valp) = value;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	return (0);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate static int
argvec_process_opt(const mdb_opt_t * opt,const mdb_arg_t * arg)183*7c478bd9Sstevel@tonic-gate argvec_process_opt(const mdb_opt_t *opt, const mdb_arg_t *arg)
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate 	uint64_t ui64;
186*7c478bd9Sstevel@tonic-gate 	uintptr_t uip;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	switch (opt->opt_type) {
189*7c478bd9Sstevel@tonic-gate 	case MDB_OPT_SETBITS:
190*7c478bd9Sstevel@tonic-gate 		*((uint_t *)opt->opt_valp) |= opt->opt_bits;
191*7c478bd9Sstevel@tonic-gate 		break;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	case MDB_OPT_CLRBITS:
194*7c478bd9Sstevel@tonic-gate 		*((uint_t *)opt->opt_valp) &= ~opt->opt_bits;
195*7c478bd9Sstevel@tonic-gate 		break;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	case MDB_OPT_STR:
198*7c478bd9Sstevel@tonic-gate 		if (arg->a_type != MDB_TYPE_STRING) {
199*7c478bd9Sstevel@tonic-gate 			warn("string argument required for -%c\n",
200*7c478bd9Sstevel@tonic-gate 			    opt->opt_char);
201*7c478bd9Sstevel@tonic-gate 			return (-1);
202*7c478bd9Sstevel@tonic-gate 		}
203*7c478bd9Sstevel@tonic-gate 		*((const char **)opt->opt_valp) = arg->a_un.a_str;
204*7c478bd9Sstevel@tonic-gate 		break;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	case MDB_OPT_UINTPTR_SET:
207*7c478bd9Sstevel@tonic-gate 		*opt->opt_flag = TRUE;
208*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
209*7c478bd9Sstevel@tonic-gate 	case MDB_OPT_UINTPTR:
210*7c478bd9Sstevel@tonic-gate 		if (arg->a_type == MDB_TYPE_STRING)
211*7c478bd9Sstevel@tonic-gate 			uip = (uintptr_t)mdb_strtoull(arg->a_un.a_str);
212*7c478bd9Sstevel@tonic-gate 		else
213*7c478bd9Sstevel@tonic-gate 			uip = (uintptr_t)arg->a_un.a_val;
214*7c478bd9Sstevel@tonic-gate 		*((uintptr_t *)opt->opt_valp) = uip;
215*7c478bd9Sstevel@tonic-gate 		break;
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	case MDB_OPT_UINT64:
218*7c478bd9Sstevel@tonic-gate 		if (arg->a_type == MDB_TYPE_STRING)
219*7c478bd9Sstevel@tonic-gate 			ui64 = mdb_strtoull(arg->a_un.a_str);
220*7c478bd9Sstevel@tonic-gate 		else
221*7c478bd9Sstevel@tonic-gate 			ui64 = arg->a_un.a_val;
222*7c478bd9Sstevel@tonic-gate 		*((uint64_t *)opt->opt_valp) = ui64;
223*7c478bd9Sstevel@tonic-gate 		break;
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	case MDB_OPT_SUBOPTS:
226*7c478bd9Sstevel@tonic-gate 		if (arg->a_type != MDB_TYPE_STRING) {
227*7c478bd9Sstevel@tonic-gate 			warn("string argument required for -%c\n",
228*7c478bd9Sstevel@tonic-gate 			    opt->opt_char);
229*7c478bd9Sstevel@tonic-gate 			return (-1);
230*7c478bd9Sstevel@tonic-gate 		}
231*7c478bd9Sstevel@tonic-gate 		return (argvec_process_subopt(opt, arg));
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	default:
234*7c478bd9Sstevel@tonic-gate 		warn("internal: bad opt=%p type=%hx\n",
235*7c478bd9Sstevel@tonic-gate 		    (void *)opt, opt->opt_type);
236*7c478bd9Sstevel@tonic-gate 		return (-1);
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	return (0);
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate static const mdb_opt_t *
argvec_findopt(const mdb_opt_t * opts,char c)243*7c478bd9Sstevel@tonic-gate argvec_findopt(const mdb_opt_t *opts, char c)
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate 	const mdb_opt_t *optp;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	for (optp = opts; optp->opt_char != 0; optp++) {
248*7c478bd9Sstevel@tonic-gate 		if (optp->opt_char == c)
249*7c478bd9Sstevel@tonic-gate 			return (optp);
250*7c478bd9Sstevel@tonic-gate 	}
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	return (NULL);
253*7c478bd9Sstevel@tonic-gate }
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate static int
argvec_getopts(const mdb_opt_t * opts,const mdb_arg_t * argv,int argc)256*7c478bd9Sstevel@tonic-gate argvec_getopts(const mdb_opt_t *opts, const mdb_arg_t *argv, int argc)
257*7c478bd9Sstevel@tonic-gate {
258*7c478bd9Sstevel@tonic-gate 	const mdb_opt_t *optp;
259*7c478bd9Sstevel@tonic-gate 	const mdb_arg_t *argp;
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 	mdb_arg_t arg;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	const char *p;
264*7c478bd9Sstevel@tonic-gate 	int i;
265*7c478bd9Sstevel@tonic-gate 	int nargs;	/* Number of arguments consumed in an iteration */
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++, argv++) {
268*7c478bd9Sstevel@tonic-gate 		/*
269*7c478bd9Sstevel@tonic-gate 		 * Each option must begin with a string argument whose first
270*7c478bd9Sstevel@tonic-gate 		 * character is '-' and has additional characters afterward.
271*7c478bd9Sstevel@tonic-gate 		 */
272*7c478bd9Sstevel@tonic-gate 		if (argv->a_type != MDB_TYPE_STRING ||
273*7c478bd9Sstevel@tonic-gate 		    argv->a_un.a_str[0] != '-' || argv->a_un.a_str[1] == '\0')
274*7c478bd9Sstevel@tonic-gate 			return (i);
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 		/*
277*7c478bd9Sstevel@tonic-gate 		 * The special prefix '--' ends option processing.
278*7c478bd9Sstevel@tonic-gate 		 */
279*7c478bd9Sstevel@tonic-gate 		if (strncmp(argv->a_un.a_str, "--", 2) == 0)
280*7c478bd9Sstevel@tonic-gate 			return (i);
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 		for (p = &argv->a_un.a_str[1]; *p != '\0'; p++) {
283*7c478bd9Sstevel@tonic-gate 			/*
284*7c478bd9Sstevel@tonic-gate 			 * Locate an option struct whose opt_char field
285*7c478bd9Sstevel@tonic-gate 			 * matches the current option letter.
286*7c478bd9Sstevel@tonic-gate 			 */
287*7c478bd9Sstevel@tonic-gate 			if ((optp = argvec_findopt(opts, *p)) == NULL) {
288*7c478bd9Sstevel@tonic-gate 				warn("illegal option -- %c\n", *p);
289*7c478bd9Sstevel@tonic-gate 				return (i);
290*7c478bd9Sstevel@tonic-gate 			}
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 			/*
293*7c478bd9Sstevel@tonic-gate 			 * Require an argument for strings, immediate
294*7c478bd9Sstevel@tonic-gate 			 * values, subopt-lists and callback functions
295*7c478bd9Sstevel@tonic-gate 			 * which require arguments.
296*7c478bd9Sstevel@tonic-gate 			 */
297*7c478bd9Sstevel@tonic-gate 			if (optp->opt_type == MDB_OPT_STR ||
298*7c478bd9Sstevel@tonic-gate 			    optp->opt_type == MDB_OPT_UINTPTR ||
299*7c478bd9Sstevel@tonic-gate 			    optp->opt_type == MDB_OPT_UINTPTR_SET ||
300*7c478bd9Sstevel@tonic-gate 			    optp->opt_type == MDB_OPT_SUBOPTS ||
301*7c478bd9Sstevel@tonic-gate 			    optp->opt_type == MDB_OPT_UINT64) {
302*7c478bd9Sstevel@tonic-gate 				/*
303*7c478bd9Sstevel@tonic-gate 				 * More text after the option letter:
304*7c478bd9Sstevel@tonic-gate 				 * forge a string argument from remainder.
305*7c478bd9Sstevel@tonic-gate 				 */
306*7c478bd9Sstevel@tonic-gate 				if (p[1] != '\0') {
307*7c478bd9Sstevel@tonic-gate 					arg.a_type = MDB_TYPE_STRING;
308*7c478bd9Sstevel@tonic-gate 					arg.a_un.a_str = ++p;
309*7c478bd9Sstevel@tonic-gate 					argp = &arg;
310*7c478bd9Sstevel@tonic-gate 					p += strlen(p) - 1;
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 					nargs = 0;
313*7c478bd9Sstevel@tonic-gate 				/*
314*7c478bd9Sstevel@tonic-gate 				 * Otherwise use the next argv element as
315*7c478bd9Sstevel@tonic-gate 				 * the argument if there is one.
316*7c478bd9Sstevel@tonic-gate 				 */
317*7c478bd9Sstevel@tonic-gate 				} else if (++i == argc) {
318*7c478bd9Sstevel@tonic-gate 					warn("option requires an "
319*7c478bd9Sstevel@tonic-gate 					    "argument -- %c\n", *p);
320*7c478bd9Sstevel@tonic-gate 					return (i - 1);
321*7c478bd9Sstevel@tonic-gate 				} else {
322*7c478bd9Sstevel@tonic-gate 					argp = ++argv;
323*7c478bd9Sstevel@tonic-gate 					nargs = 1;
324*7c478bd9Sstevel@tonic-gate 				}
325*7c478bd9Sstevel@tonic-gate 			} else {
326*7c478bd9Sstevel@tonic-gate 				argp = NULL;
327*7c478bd9Sstevel@tonic-gate 				nargs = 0;
328*7c478bd9Sstevel@tonic-gate 			}
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 			/*
331*7c478bd9Sstevel@tonic-gate 			 * Perform type-specific handling for this option.
332*7c478bd9Sstevel@tonic-gate 			 */
333*7c478bd9Sstevel@tonic-gate 			if (argvec_process_opt(optp, argp) == -1)
334*7c478bd9Sstevel@tonic-gate 				return (i - nargs);
335*7c478bd9Sstevel@tonic-gate 		}
336*7c478bd9Sstevel@tonic-gate 	}
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	return (i);
339*7c478bd9Sstevel@tonic-gate }
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate int
mdb_getopts(int argc,const mdb_arg_t * argv,...)342*7c478bd9Sstevel@tonic-gate mdb_getopts(int argc, const mdb_arg_t *argv, ...)
343*7c478bd9Sstevel@tonic-gate {
344*7c478bd9Sstevel@tonic-gate 	/*
345*7c478bd9Sstevel@tonic-gate 	 * For simplicity just declare enough options on the stack to handle
346*7c478bd9Sstevel@tonic-gate 	 * a-z and A-Z and an extra terminator.
347*7c478bd9Sstevel@tonic-gate 	 */
348*7c478bd9Sstevel@tonic-gate 	mdb_opt_t opts[53], *op = &opts[0];
349*7c478bd9Sstevel@tonic-gate 	va_list alist;
350*7c478bd9Sstevel@tonic-gate 	int c, i = 0;
351*7c478bd9Sstevel@tonic-gate 	mdb_subopt_t *sop;
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	va_start(alist, argv);
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < (sizeof (opts) / sizeof (opts[0]) - 1); i++, op++) {
356*7c478bd9Sstevel@tonic-gate 		if ((c = va_arg(alist, int)) == 0)
357*7c478bd9Sstevel@tonic-gate 			break; /* end of options */
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 		op->opt_char = (char)c;
360*7c478bd9Sstevel@tonic-gate 		op->opt_type = va_arg(alist, uint_t);
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 		if (op->opt_type == MDB_OPT_SETBITS ||
363*7c478bd9Sstevel@tonic-gate 		    op->opt_type == MDB_OPT_CLRBITS) {
364*7c478bd9Sstevel@tonic-gate 			op->opt_bits = va_arg(alist, uint_t);
365*7c478bd9Sstevel@tonic-gate 		} else if (op->opt_type == MDB_OPT_UINTPTR_SET) {
366*7c478bd9Sstevel@tonic-gate 			op->opt_flag = va_arg(alist, boolean_t *);
367*7c478bd9Sstevel@tonic-gate 		} else if (op->opt_type == MDB_OPT_SUBOPTS) {
368*7c478bd9Sstevel@tonic-gate 			op->opt_subopts = va_arg(alist, mdb_subopt_t *);
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 			for (sop = op->opt_subopts; sop->sop_flag; sop++)
371*7c478bd9Sstevel@tonic-gate 				sop->sop_index = -1;
372*7c478bd9Sstevel@tonic-gate 		}
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 		op->opt_valp = va_arg(alist, void *);
375*7c478bd9Sstevel@tonic-gate 	}
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	bzero(&opts[i], sizeof (mdb_opt_t));
378*7c478bd9Sstevel@tonic-gate 	va_end(alist);
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 	return (argvec_getopts(opts, argv, argc));
381*7c478bd9Sstevel@tonic-gate }
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate /*
384*7c478bd9Sstevel@tonic-gate  * The old adb breakpoint and watchpoint routines did not accept any arguments;
385*7c478bd9Sstevel@tonic-gate  * all characters after the verb were concatenated to form the string callback.
386*7c478bd9Sstevel@tonic-gate  * This utility function concatenates all arguments in argv[] into a single
387*7c478bd9Sstevel@tonic-gate  * string to simplify the implementation of these legacy routines.
388*7c478bd9Sstevel@tonic-gate  */
389*7c478bd9Sstevel@tonic-gate char *
mdb_argv_to_str(int argc,const mdb_arg_t * argv)390*7c478bd9Sstevel@tonic-gate mdb_argv_to_str(int argc, const mdb_arg_t *argv)
391*7c478bd9Sstevel@tonic-gate {
392*7c478bd9Sstevel@tonic-gate 	char *s = NULL;
393*7c478bd9Sstevel@tonic-gate 	size_t n = 0;
394*7c478bd9Sstevel@tonic-gate 	int i;
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
397*7c478bd9Sstevel@tonic-gate 		if (argv[i].a_type == MDB_TYPE_STRING)
398*7c478bd9Sstevel@tonic-gate 			n += strlen(argv[i].a_un.a_str);
399*7c478bd9Sstevel@tonic-gate 	}
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 	if (n != 0) {
402*7c478bd9Sstevel@tonic-gate 		s = mdb_zalloc(n + argc, UM_SLEEP);
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc - 1; i++, argv++) {
405*7c478bd9Sstevel@tonic-gate 			(void) strcat(s, argv->a_un.a_str);
406*7c478bd9Sstevel@tonic-gate 			(void) strcat(s, " ");
407*7c478bd9Sstevel@tonic-gate 		}
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 		(void) strcat(s, argv->a_un.a_str);
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	return (s);
413*7c478bd9Sstevel@tonic-gate }
414