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