xref: /OK3568_Linux_fs/kernel/tools/lib/subcmd/parse-options.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/compiler.h>
3*4882a593Smuzhiyun #include <linux/string.h>
4*4882a593Smuzhiyun #include <linux/types.h>
5*4882a593Smuzhiyun #include <stdio.h>
6*4882a593Smuzhiyun #include <stdlib.h>
7*4882a593Smuzhiyun #include <stdint.h>
8*4882a593Smuzhiyun #include <string.h>
9*4882a593Smuzhiyun #include <ctype.h>
10*4882a593Smuzhiyun #include "subcmd-util.h"
11*4882a593Smuzhiyun #include "parse-options.h"
12*4882a593Smuzhiyun #include "subcmd-config.h"
13*4882a593Smuzhiyun #include "pager.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define OPT_SHORT 1
16*4882a593Smuzhiyun #define OPT_UNSET 2
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun char *error_buf;
19*4882a593Smuzhiyun 
opterror(const struct option * opt,const char * reason,int flags)20*4882a593Smuzhiyun static int opterror(const struct option *opt, const char *reason, int flags)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun 	if (flags & OPT_SHORT)
23*4882a593Smuzhiyun 		fprintf(stderr, " Error: switch `%c' %s", opt->short_name, reason);
24*4882a593Smuzhiyun 	else if (flags & OPT_UNSET)
25*4882a593Smuzhiyun 		fprintf(stderr, " Error: option `no-%s' %s", opt->long_name, reason);
26*4882a593Smuzhiyun 	else
27*4882a593Smuzhiyun 		fprintf(stderr, " Error: option `%s' %s", opt->long_name, reason);
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	return -1;
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun 
skip_prefix(const char * str,const char * prefix)32*4882a593Smuzhiyun static const char *skip_prefix(const char *str, const char *prefix)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 	size_t len = strlen(prefix);
35*4882a593Smuzhiyun 	return strncmp(str, prefix, len) ? NULL : str + len;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun 
optwarning(const struct option * opt,const char * reason,int flags)38*4882a593Smuzhiyun static void optwarning(const struct option *opt, const char *reason, int flags)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	if (flags & OPT_SHORT)
41*4882a593Smuzhiyun 		fprintf(stderr, " Warning: switch `%c' %s", opt->short_name, reason);
42*4882a593Smuzhiyun 	else if (flags & OPT_UNSET)
43*4882a593Smuzhiyun 		fprintf(stderr, " Warning: option `no-%s' %s", opt->long_name, reason);
44*4882a593Smuzhiyun 	else
45*4882a593Smuzhiyun 		fprintf(stderr, " Warning: option `%s' %s", opt->long_name, reason);
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun 
get_arg(struct parse_opt_ctx_t * p,const struct option * opt,int flags,const char ** arg)48*4882a593Smuzhiyun static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
49*4882a593Smuzhiyun 		   int flags, const char **arg)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	const char *res;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	if (p->opt) {
54*4882a593Smuzhiyun 		res = p->opt;
55*4882a593Smuzhiyun 		p->opt = NULL;
56*4882a593Smuzhiyun 	} else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
57*4882a593Smuzhiyun 		    **(p->argv + 1) == '-')) {
58*4882a593Smuzhiyun 		res = (const char *)opt->defval;
59*4882a593Smuzhiyun 	} else if (p->argc > 1) {
60*4882a593Smuzhiyun 		p->argc--;
61*4882a593Smuzhiyun 		res = *++p->argv;
62*4882a593Smuzhiyun 	} else
63*4882a593Smuzhiyun 		return opterror(opt, "requires a value", flags);
64*4882a593Smuzhiyun 	if (arg)
65*4882a593Smuzhiyun 		*arg = res;
66*4882a593Smuzhiyun 	return 0;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
get_value(struct parse_opt_ctx_t * p,const struct option * opt,int flags)69*4882a593Smuzhiyun static int get_value(struct parse_opt_ctx_t *p,
70*4882a593Smuzhiyun 		     const struct option *opt, int flags)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	const char *s, *arg = NULL;
73*4882a593Smuzhiyun 	const int unset = flags & OPT_UNSET;
74*4882a593Smuzhiyun 	int err;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	if (unset && p->opt)
77*4882a593Smuzhiyun 		return opterror(opt, "takes no value", flags);
78*4882a593Smuzhiyun 	if (unset && (opt->flags & PARSE_OPT_NONEG))
79*4882a593Smuzhiyun 		return opterror(opt, "isn't available", flags);
80*4882a593Smuzhiyun 	if (opt->flags & PARSE_OPT_DISABLED)
81*4882a593Smuzhiyun 		return opterror(opt, "is not usable", flags);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	if (opt->flags & PARSE_OPT_EXCLUSIVE) {
84*4882a593Smuzhiyun 		if (p->excl_opt && p->excl_opt != opt) {
85*4882a593Smuzhiyun 			char msg[128];
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 			if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
88*4882a593Smuzhiyun 			    p->excl_opt->long_name == NULL) {
89*4882a593Smuzhiyun 				snprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
90*4882a593Smuzhiyun 					 p->excl_opt->short_name);
91*4882a593Smuzhiyun 			} else {
92*4882a593Smuzhiyun 				snprintf(msg, sizeof(msg), "cannot be used with %s",
93*4882a593Smuzhiyun 					 p->excl_opt->long_name);
94*4882a593Smuzhiyun 			}
95*4882a593Smuzhiyun 			opterror(opt, msg, flags);
96*4882a593Smuzhiyun 			return -3;
97*4882a593Smuzhiyun 		}
98*4882a593Smuzhiyun 		p->excl_opt = opt;
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 	if (!(flags & OPT_SHORT) && p->opt) {
101*4882a593Smuzhiyun 		switch (opt->type) {
102*4882a593Smuzhiyun 		case OPTION_CALLBACK:
103*4882a593Smuzhiyun 			if (!(opt->flags & PARSE_OPT_NOARG))
104*4882a593Smuzhiyun 				break;
105*4882a593Smuzhiyun 			/* FALLTHROUGH */
106*4882a593Smuzhiyun 		case OPTION_BOOLEAN:
107*4882a593Smuzhiyun 		case OPTION_INCR:
108*4882a593Smuzhiyun 		case OPTION_BIT:
109*4882a593Smuzhiyun 		case OPTION_SET_UINT:
110*4882a593Smuzhiyun 		case OPTION_SET_PTR:
111*4882a593Smuzhiyun 			return opterror(opt, "takes no value", flags);
112*4882a593Smuzhiyun 		case OPTION_END:
113*4882a593Smuzhiyun 		case OPTION_ARGUMENT:
114*4882a593Smuzhiyun 		case OPTION_GROUP:
115*4882a593Smuzhiyun 		case OPTION_STRING:
116*4882a593Smuzhiyun 		case OPTION_INTEGER:
117*4882a593Smuzhiyun 		case OPTION_UINTEGER:
118*4882a593Smuzhiyun 		case OPTION_LONG:
119*4882a593Smuzhiyun 		case OPTION_ULONG:
120*4882a593Smuzhiyun 		case OPTION_U64:
121*4882a593Smuzhiyun 		default:
122*4882a593Smuzhiyun 			break;
123*4882a593Smuzhiyun 		}
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	if (opt->flags & PARSE_OPT_NOBUILD) {
127*4882a593Smuzhiyun 		char reason[128];
128*4882a593Smuzhiyun 		bool noarg = false;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 		err = snprintf(reason, sizeof(reason),
131*4882a593Smuzhiyun 				opt->flags & PARSE_OPT_CANSKIP ?
132*4882a593Smuzhiyun 					"is being ignored because %s " :
133*4882a593Smuzhiyun 					"is not available because %s",
134*4882a593Smuzhiyun 				opt->build_opt);
135*4882a593Smuzhiyun 		reason[sizeof(reason) - 1] = '\0';
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 		if (err < 0)
138*4882a593Smuzhiyun 			strncpy(reason, opt->flags & PARSE_OPT_CANSKIP ?
139*4882a593Smuzhiyun 					"is being ignored" :
140*4882a593Smuzhiyun 					"is not available",
141*4882a593Smuzhiyun 					sizeof(reason));
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		if (!(opt->flags & PARSE_OPT_CANSKIP))
144*4882a593Smuzhiyun 			return opterror(opt, reason, flags);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 		err = 0;
147*4882a593Smuzhiyun 		if (unset)
148*4882a593Smuzhiyun 			noarg = true;
149*4882a593Smuzhiyun 		if (opt->flags & PARSE_OPT_NOARG)
150*4882a593Smuzhiyun 			noarg = true;
151*4882a593Smuzhiyun 		if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
152*4882a593Smuzhiyun 			noarg = true;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 		switch (opt->type) {
155*4882a593Smuzhiyun 		case OPTION_BOOLEAN:
156*4882a593Smuzhiyun 		case OPTION_INCR:
157*4882a593Smuzhiyun 		case OPTION_BIT:
158*4882a593Smuzhiyun 		case OPTION_SET_UINT:
159*4882a593Smuzhiyun 		case OPTION_SET_PTR:
160*4882a593Smuzhiyun 		case OPTION_END:
161*4882a593Smuzhiyun 		case OPTION_ARGUMENT:
162*4882a593Smuzhiyun 		case OPTION_GROUP:
163*4882a593Smuzhiyun 			noarg = true;
164*4882a593Smuzhiyun 			break;
165*4882a593Smuzhiyun 		case OPTION_CALLBACK:
166*4882a593Smuzhiyun 		case OPTION_STRING:
167*4882a593Smuzhiyun 		case OPTION_INTEGER:
168*4882a593Smuzhiyun 		case OPTION_UINTEGER:
169*4882a593Smuzhiyun 		case OPTION_LONG:
170*4882a593Smuzhiyun 		case OPTION_ULONG:
171*4882a593Smuzhiyun 		case OPTION_U64:
172*4882a593Smuzhiyun 		default:
173*4882a593Smuzhiyun 			break;
174*4882a593Smuzhiyun 		}
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 		if (!noarg)
177*4882a593Smuzhiyun 			err = get_arg(p, opt, flags, NULL);
178*4882a593Smuzhiyun 		if (err)
179*4882a593Smuzhiyun 			return err;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 		optwarning(opt, reason, flags);
182*4882a593Smuzhiyun 		return 0;
183*4882a593Smuzhiyun 	}
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	switch (opt->type) {
186*4882a593Smuzhiyun 	case OPTION_BIT:
187*4882a593Smuzhiyun 		if (unset)
188*4882a593Smuzhiyun 			*(int *)opt->value &= ~opt->defval;
189*4882a593Smuzhiyun 		else
190*4882a593Smuzhiyun 			*(int *)opt->value |= opt->defval;
191*4882a593Smuzhiyun 		return 0;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	case OPTION_BOOLEAN:
194*4882a593Smuzhiyun 		*(bool *)opt->value = unset ? false : true;
195*4882a593Smuzhiyun 		if (opt->set)
196*4882a593Smuzhiyun 			*(bool *)opt->set = true;
197*4882a593Smuzhiyun 		return 0;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	case OPTION_INCR:
200*4882a593Smuzhiyun 		*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
201*4882a593Smuzhiyun 		return 0;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	case OPTION_SET_UINT:
204*4882a593Smuzhiyun 		*(unsigned int *)opt->value = unset ? 0 : opt->defval;
205*4882a593Smuzhiyun 		return 0;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	case OPTION_SET_PTR:
208*4882a593Smuzhiyun 		*(void **)opt->value = unset ? NULL : (void *)opt->defval;
209*4882a593Smuzhiyun 		return 0;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	case OPTION_STRING:
212*4882a593Smuzhiyun 		err = 0;
213*4882a593Smuzhiyun 		if (unset)
214*4882a593Smuzhiyun 			*(const char **)opt->value = NULL;
215*4882a593Smuzhiyun 		else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
216*4882a593Smuzhiyun 			*(const char **)opt->value = (const char *)opt->defval;
217*4882a593Smuzhiyun 		else
218*4882a593Smuzhiyun 			err = get_arg(p, opt, flags, (const char **)opt->value);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 		if (opt->set)
221*4882a593Smuzhiyun 			*(bool *)opt->set = true;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 		/* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
224*4882a593Smuzhiyun 		if (opt->flags & PARSE_OPT_NOEMPTY) {
225*4882a593Smuzhiyun 			const char *val = *(const char **)opt->value;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 			if (!val)
228*4882a593Smuzhiyun 				return err;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 			/* Similar to unset if we are given an empty string. */
231*4882a593Smuzhiyun 			if (val[0] == '\0') {
232*4882a593Smuzhiyun 				*(const char **)opt->value = NULL;
233*4882a593Smuzhiyun 				return 0;
234*4882a593Smuzhiyun 			}
235*4882a593Smuzhiyun 		}
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 		return err;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	case OPTION_CALLBACK:
240*4882a593Smuzhiyun 		if (opt->set)
241*4882a593Smuzhiyun 			*(bool *)opt->set = true;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 		if (unset)
244*4882a593Smuzhiyun 			return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
245*4882a593Smuzhiyun 		if (opt->flags & PARSE_OPT_NOARG)
246*4882a593Smuzhiyun 			return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
247*4882a593Smuzhiyun 		if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
248*4882a593Smuzhiyun 			return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
249*4882a593Smuzhiyun 		if (get_arg(p, opt, flags, &arg))
250*4882a593Smuzhiyun 			return -1;
251*4882a593Smuzhiyun 		return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	case OPTION_INTEGER:
254*4882a593Smuzhiyun 		if (unset) {
255*4882a593Smuzhiyun 			*(int *)opt->value = 0;
256*4882a593Smuzhiyun 			return 0;
257*4882a593Smuzhiyun 		}
258*4882a593Smuzhiyun 		if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
259*4882a593Smuzhiyun 			*(int *)opt->value = opt->defval;
260*4882a593Smuzhiyun 			return 0;
261*4882a593Smuzhiyun 		}
262*4882a593Smuzhiyun 		if (get_arg(p, opt, flags, &arg))
263*4882a593Smuzhiyun 			return -1;
264*4882a593Smuzhiyun 		*(int *)opt->value = strtol(arg, (char **)&s, 10);
265*4882a593Smuzhiyun 		if (*s)
266*4882a593Smuzhiyun 			return opterror(opt, "expects a numerical value", flags);
267*4882a593Smuzhiyun 		return 0;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	case OPTION_UINTEGER:
270*4882a593Smuzhiyun 		if (unset) {
271*4882a593Smuzhiyun 			*(unsigned int *)opt->value = 0;
272*4882a593Smuzhiyun 			return 0;
273*4882a593Smuzhiyun 		}
274*4882a593Smuzhiyun 		if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
275*4882a593Smuzhiyun 			*(unsigned int *)opt->value = opt->defval;
276*4882a593Smuzhiyun 			return 0;
277*4882a593Smuzhiyun 		}
278*4882a593Smuzhiyun 		if (get_arg(p, opt, flags, &arg))
279*4882a593Smuzhiyun 			return -1;
280*4882a593Smuzhiyun 		if (arg[0] == '-')
281*4882a593Smuzhiyun 			return opterror(opt, "expects an unsigned numerical value", flags);
282*4882a593Smuzhiyun 		*(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
283*4882a593Smuzhiyun 		if (*s)
284*4882a593Smuzhiyun 			return opterror(opt, "expects a numerical value", flags);
285*4882a593Smuzhiyun 		return 0;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	case OPTION_LONG:
288*4882a593Smuzhiyun 		if (unset) {
289*4882a593Smuzhiyun 			*(long *)opt->value = 0;
290*4882a593Smuzhiyun 			return 0;
291*4882a593Smuzhiyun 		}
292*4882a593Smuzhiyun 		if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
293*4882a593Smuzhiyun 			*(long *)opt->value = opt->defval;
294*4882a593Smuzhiyun 			return 0;
295*4882a593Smuzhiyun 		}
296*4882a593Smuzhiyun 		if (get_arg(p, opt, flags, &arg))
297*4882a593Smuzhiyun 			return -1;
298*4882a593Smuzhiyun 		*(long *)opt->value = strtol(arg, (char **)&s, 10);
299*4882a593Smuzhiyun 		if (*s)
300*4882a593Smuzhiyun 			return opterror(opt, "expects a numerical value", flags);
301*4882a593Smuzhiyun 		return 0;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	case OPTION_ULONG:
304*4882a593Smuzhiyun 		if (unset) {
305*4882a593Smuzhiyun 			*(unsigned long *)opt->value = 0;
306*4882a593Smuzhiyun 			return 0;
307*4882a593Smuzhiyun 		}
308*4882a593Smuzhiyun 		if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
309*4882a593Smuzhiyun 			*(unsigned long *)opt->value = opt->defval;
310*4882a593Smuzhiyun 			return 0;
311*4882a593Smuzhiyun 		}
312*4882a593Smuzhiyun 		if (get_arg(p, opt, flags, &arg))
313*4882a593Smuzhiyun 			return -1;
314*4882a593Smuzhiyun 		*(unsigned long *)opt->value = strtoul(arg, (char **)&s, 10);
315*4882a593Smuzhiyun 		if (*s)
316*4882a593Smuzhiyun 			return opterror(opt, "expects a numerical value", flags);
317*4882a593Smuzhiyun 		return 0;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	case OPTION_U64:
320*4882a593Smuzhiyun 		if (unset) {
321*4882a593Smuzhiyun 			*(u64 *)opt->value = 0;
322*4882a593Smuzhiyun 			return 0;
323*4882a593Smuzhiyun 		}
324*4882a593Smuzhiyun 		if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
325*4882a593Smuzhiyun 			*(u64 *)opt->value = opt->defval;
326*4882a593Smuzhiyun 			return 0;
327*4882a593Smuzhiyun 		}
328*4882a593Smuzhiyun 		if (get_arg(p, opt, flags, &arg))
329*4882a593Smuzhiyun 			return -1;
330*4882a593Smuzhiyun 		if (arg[0] == '-')
331*4882a593Smuzhiyun 			return opterror(opt, "expects an unsigned numerical value", flags);
332*4882a593Smuzhiyun 		*(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
333*4882a593Smuzhiyun 		if (*s)
334*4882a593Smuzhiyun 			return opterror(opt, "expects a numerical value", flags);
335*4882a593Smuzhiyun 		return 0;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	case OPTION_END:
338*4882a593Smuzhiyun 	case OPTION_ARGUMENT:
339*4882a593Smuzhiyun 	case OPTION_GROUP:
340*4882a593Smuzhiyun 	default:
341*4882a593Smuzhiyun 		die("should not happen, someone must be hit on the forehead");
342*4882a593Smuzhiyun 	}
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
parse_short_opt(struct parse_opt_ctx_t * p,const struct option * options)345*4882a593Smuzhiyun static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun retry:
348*4882a593Smuzhiyun 	for (; options->type != OPTION_END; options++) {
349*4882a593Smuzhiyun 		if (options->short_name == *p->opt) {
350*4882a593Smuzhiyun 			p->opt = p->opt[1] ? p->opt + 1 : NULL;
351*4882a593Smuzhiyun 			return get_value(p, options, OPT_SHORT);
352*4882a593Smuzhiyun 		}
353*4882a593Smuzhiyun 	}
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	if (options->parent) {
356*4882a593Smuzhiyun 		options = options->parent;
357*4882a593Smuzhiyun 		goto retry;
358*4882a593Smuzhiyun 	}
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	return -2;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
parse_long_opt(struct parse_opt_ctx_t * p,const char * arg,const struct option * options)363*4882a593Smuzhiyun static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
364*4882a593Smuzhiyun                           const struct option *options)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun 	const char *arg_end = strchr(arg, '=');
367*4882a593Smuzhiyun 	const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
368*4882a593Smuzhiyun 	int abbrev_flags = 0, ambiguous_flags = 0;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	if (!arg_end)
371*4882a593Smuzhiyun 		arg_end = arg + strlen(arg);
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun retry:
374*4882a593Smuzhiyun 	for (; options->type != OPTION_END; options++) {
375*4882a593Smuzhiyun 		const char *rest;
376*4882a593Smuzhiyun 		int flags = 0;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 		if (!options->long_name)
379*4882a593Smuzhiyun 			continue;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 		rest = skip_prefix(arg, options->long_name);
382*4882a593Smuzhiyun 		if (options->type == OPTION_ARGUMENT) {
383*4882a593Smuzhiyun 			if (!rest)
384*4882a593Smuzhiyun 				continue;
385*4882a593Smuzhiyun 			if (*rest == '=')
386*4882a593Smuzhiyun 				return opterror(options, "takes no value", flags);
387*4882a593Smuzhiyun 			if (*rest)
388*4882a593Smuzhiyun 				continue;
389*4882a593Smuzhiyun 			p->out[p->cpidx++] = arg - 2;
390*4882a593Smuzhiyun 			return 0;
391*4882a593Smuzhiyun 		}
392*4882a593Smuzhiyun 		if (!rest) {
393*4882a593Smuzhiyun 			if (strstarts(options->long_name, "no-")) {
394*4882a593Smuzhiyun 				/*
395*4882a593Smuzhiyun 				 * The long name itself starts with "no-", so
396*4882a593Smuzhiyun 				 * accept the option without "no-" so that users
397*4882a593Smuzhiyun 				 * do not have to enter "no-no-" to get the
398*4882a593Smuzhiyun 				 * negation.
399*4882a593Smuzhiyun 				 */
400*4882a593Smuzhiyun 				rest = skip_prefix(arg, options->long_name + 3);
401*4882a593Smuzhiyun 				if (rest) {
402*4882a593Smuzhiyun 					flags |= OPT_UNSET;
403*4882a593Smuzhiyun 					goto match;
404*4882a593Smuzhiyun 				}
405*4882a593Smuzhiyun 				/* Abbreviated case */
406*4882a593Smuzhiyun 				if (strstarts(options->long_name + 3, arg)) {
407*4882a593Smuzhiyun 					flags |= OPT_UNSET;
408*4882a593Smuzhiyun 					goto is_abbreviated;
409*4882a593Smuzhiyun 				}
410*4882a593Smuzhiyun 			}
411*4882a593Smuzhiyun 			/* abbreviated? */
412*4882a593Smuzhiyun 			if (!strncmp(options->long_name, arg, arg_end - arg)) {
413*4882a593Smuzhiyun is_abbreviated:
414*4882a593Smuzhiyun 				if (abbrev_option) {
415*4882a593Smuzhiyun 					/*
416*4882a593Smuzhiyun 					 * If this is abbreviated, it is
417*4882a593Smuzhiyun 					 * ambiguous. So when there is no
418*4882a593Smuzhiyun 					 * exact match later, we need to
419*4882a593Smuzhiyun 					 * error out.
420*4882a593Smuzhiyun 					 */
421*4882a593Smuzhiyun 					ambiguous_option = abbrev_option;
422*4882a593Smuzhiyun 					ambiguous_flags = abbrev_flags;
423*4882a593Smuzhiyun 				}
424*4882a593Smuzhiyun 				if (!(flags & OPT_UNSET) && *arg_end)
425*4882a593Smuzhiyun 					p->opt = arg_end + 1;
426*4882a593Smuzhiyun 				abbrev_option = options;
427*4882a593Smuzhiyun 				abbrev_flags = flags;
428*4882a593Smuzhiyun 				continue;
429*4882a593Smuzhiyun 			}
430*4882a593Smuzhiyun 			/* negated and abbreviated very much? */
431*4882a593Smuzhiyun 			if (strstarts("no-", arg)) {
432*4882a593Smuzhiyun 				flags |= OPT_UNSET;
433*4882a593Smuzhiyun 				goto is_abbreviated;
434*4882a593Smuzhiyun 			}
435*4882a593Smuzhiyun 			/* negated? */
436*4882a593Smuzhiyun 			if (strncmp(arg, "no-", 3))
437*4882a593Smuzhiyun 				continue;
438*4882a593Smuzhiyun 			flags |= OPT_UNSET;
439*4882a593Smuzhiyun 			rest = skip_prefix(arg + 3, options->long_name);
440*4882a593Smuzhiyun 			/* abbreviated and negated? */
441*4882a593Smuzhiyun 			if (!rest && strstarts(options->long_name, arg + 3))
442*4882a593Smuzhiyun 				goto is_abbreviated;
443*4882a593Smuzhiyun 			if (!rest)
444*4882a593Smuzhiyun 				continue;
445*4882a593Smuzhiyun 		}
446*4882a593Smuzhiyun match:
447*4882a593Smuzhiyun 		if (*rest) {
448*4882a593Smuzhiyun 			if (*rest != '=')
449*4882a593Smuzhiyun 				continue;
450*4882a593Smuzhiyun 			p->opt = rest + 1;
451*4882a593Smuzhiyun 		}
452*4882a593Smuzhiyun 		return get_value(p, options, flags);
453*4882a593Smuzhiyun 	}
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	if (ambiguous_option) {
456*4882a593Smuzhiyun 		 fprintf(stderr,
457*4882a593Smuzhiyun 			 " Error: Ambiguous option: %s (could be --%s%s or --%s%s)\n",
458*4882a593Smuzhiyun 			 arg,
459*4882a593Smuzhiyun 			 (ambiguous_flags & OPT_UNSET) ?  "no-" : "",
460*4882a593Smuzhiyun 			 ambiguous_option->long_name,
461*4882a593Smuzhiyun 			 (abbrev_flags & OPT_UNSET) ?  "no-" : "",
462*4882a593Smuzhiyun 			 abbrev_option->long_name);
463*4882a593Smuzhiyun 		 return -1;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 	if (abbrev_option)
466*4882a593Smuzhiyun 		return get_value(p, abbrev_option, abbrev_flags);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	if (options->parent) {
469*4882a593Smuzhiyun 		options = options->parent;
470*4882a593Smuzhiyun 		goto retry;
471*4882a593Smuzhiyun 	}
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	return -2;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun 
check_typos(const char * arg,const struct option * options)476*4882a593Smuzhiyun static void check_typos(const char *arg, const struct option *options)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	if (strlen(arg) < 3)
479*4882a593Smuzhiyun 		return;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	if (strstarts(arg, "no-")) {
482*4882a593Smuzhiyun 		fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)\n", arg);
483*4882a593Smuzhiyun 		exit(129);
484*4882a593Smuzhiyun 	}
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	for (; options->type != OPTION_END; options++) {
487*4882a593Smuzhiyun 		if (!options->long_name)
488*4882a593Smuzhiyun 			continue;
489*4882a593Smuzhiyun 		if (strstarts(options->long_name, arg)) {
490*4882a593Smuzhiyun 			fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)\n", arg);
491*4882a593Smuzhiyun 			exit(129);
492*4882a593Smuzhiyun 		}
493*4882a593Smuzhiyun 	}
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun 
parse_options_start(struct parse_opt_ctx_t * ctx,int argc,const char ** argv,int flags)496*4882a593Smuzhiyun static void parse_options_start(struct parse_opt_ctx_t *ctx,
497*4882a593Smuzhiyun 				int argc, const char **argv, int flags)
498*4882a593Smuzhiyun {
499*4882a593Smuzhiyun 	memset(ctx, 0, sizeof(*ctx));
500*4882a593Smuzhiyun 	ctx->argc = argc - 1;
501*4882a593Smuzhiyun 	ctx->argv = argv + 1;
502*4882a593Smuzhiyun 	ctx->out  = argv;
503*4882a593Smuzhiyun 	ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
504*4882a593Smuzhiyun 	ctx->flags = flags;
505*4882a593Smuzhiyun 	if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
506*4882a593Smuzhiyun 	    (flags & PARSE_OPT_STOP_AT_NON_OPTION))
507*4882a593Smuzhiyun 		die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun static int usage_with_options_internal(const char * const *,
511*4882a593Smuzhiyun 				       const struct option *, int,
512*4882a593Smuzhiyun 				       struct parse_opt_ctx_t *);
513*4882a593Smuzhiyun 
parse_options_step(struct parse_opt_ctx_t * ctx,const struct option * options,const char * const usagestr[])514*4882a593Smuzhiyun static int parse_options_step(struct parse_opt_ctx_t *ctx,
515*4882a593Smuzhiyun 			      const struct option *options,
516*4882a593Smuzhiyun 			      const char * const usagestr[])
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun 	int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
519*4882a593Smuzhiyun 	int excl_short_opt = 1;
520*4882a593Smuzhiyun 	const char *arg;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	/* we must reset ->opt, unknown short option leave it dangling */
523*4882a593Smuzhiyun 	ctx->opt = NULL;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	for (; ctx->argc; ctx->argc--, ctx->argv++) {
526*4882a593Smuzhiyun 		arg = ctx->argv[0];
527*4882a593Smuzhiyun 		if (*arg != '-' || !arg[1]) {
528*4882a593Smuzhiyun 			if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
529*4882a593Smuzhiyun 				break;
530*4882a593Smuzhiyun 			ctx->out[ctx->cpidx++] = ctx->argv[0];
531*4882a593Smuzhiyun 			continue;
532*4882a593Smuzhiyun 		}
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 		if (arg[1] != '-') {
535*4882a593Smuzhiyun 			ctx->opt = ++arg;
536*4882a593Smuzhiyun 			if (internal_help && *ctx->opt == 'h') {
537*4882a593Smuzhiyun 				return usage_with_options_internal(usagestr, options, 0, ctx);
538*4882a593Smuzhiyun 			}
539*4882a593Smuzhiyun 			switch (parse_short_opt(ctx, options)) {
540*4882a593Smuzhiyun 			case -1:
541*4882a593Smuzhiyun 				return parse_options_usage(usagestr, options, arg, 1);
542*4882a593Smuzhiyun 			case -2:
543*4882a593Smuzhiyun 				goto unknown;
544*4882a593Smuzhiyun 			case -3:
545*4882a593Smuzhiyun 				goto exclusive;
546*4882a593Smuzhiyun 			default:
547*4882a593Smuzhiyun 				break;
548*4882a593Smuzhiyun 			}
549*4882a593Smuzhiyun 			if (ctx->opt)
550*4882a593Smuzhiyun 				check_typos(arg, options);
551*4882a593Smuzhiyun 			while (ctx->opt) {
552*4882a593Smuzhiyun 				if (internal_help && *ctx->opt == 'h')
553*4882a593Smuzhiyun 					return usage_with_options_internal(usagestr, options, 0, ctx);
554*4882a593Smuzhiyun 				arg = ctx->opt;
555*4882a593Smuzhiyun 				switch (parse_short_opt(ctx, options)) {
556*4882a593Smuzhiyun 				case -1:
557*4882a593Smuzhiyun 					return parse_options_usage(usagestr, options, arg, 1);
558*4882a593Smuzhiyun 				case -2:
559*4882a593Smuzhiyun 					/* fake a short option thing to hide the fact that we may have
560*4882a593Smuzhiyun 					 * started to parse aggregated stuff
561*4882a593Smuzhiyun 					 *
562*4882a593Smuzhiyun 					 * This is leaky, too bad.
563*4882a593Smuzhiyun 					 */
564*4882a593Smuzhiyun 					ctx->argv[0] = strdup(ctx->opt - 1);
565*4882a593Smuzhiyun 					*(char *)ctx->argv[0] = '-';
566*4882a593Smuzhiyun 					goto unknown;
567*4882a593Smuzhiyun 				case -3:
568*4882a593Smuzhiyun 					goto exclusive;
569*4882a593Smuzhiyun 				default:
570*4882a593Smuzhiyun 					break;
571*4882a593Smuzhiyun 				}
572*4882a593Smuzhiyun 			}
573*4882a593Smuzhiyun 			continue;
574*4882a593Smuzhiyun 		}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 		if (!arg[2]) { /* "--" */
577*4882a593Smuzhiyun 			if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
578*4882a593Smuzhiyun 				ctx->argc--;
579*4882a593Smuzhiyun 				ctx->argv++;
580*4882a593Smuzhiyun 			}
581*4882a593Smuzhiyun 			break;
582*4882a593Smuzhiyun 		}
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 		arg += 2;
585*4882a593Smuzhiyun 		if (internal_help && !strcmp(arg, "help-all"))
586*4882a593Smuzhiyun 			return usage_with_options_internal(usagestr, options, 1, ctx);
587*4882a593Smuzhiyun 		if (internal_help && !strcmp(arg, "help"))
588*4882a593Smuzhiyun 			return usage_with_options_internal(usagestr, options, 0, ctx);
589*4882a593Smuzhiyun 		if (!strcmp(arg, "list-opts"))
590*4882a593Smuzhiyun 			return PARSE_OPT_LIST_OPTS;
591*4882a593Smuzhiyun 		if (!strcmp(arg, "list-cmds"))
592*4882a593Smuzhiyun 			return PARSE_OPT_LIST_SUBCMDS;
593*4882a593Smuzhiyun 		switch (parse_long_opt(ctx, arg, options)) {
594*4882a593Smuzhiyun 		case -1:
595*4882a593Smuzhiyun 			return parse_options_usage(usagestr, options, arg, 0);
596*4882a593Smuzhiyun 		case -2:
597*4882a593Smuzhiyun 			goto unknown;
598*4882a593Smuzhiyun 		case -3:
599*4882a593Smuzhiyun 			excl_short_opt = 0;
600*4882a593Smuzhiyun 			goto exclusive;
601*4882a593Smuzhiyun 		default:
602*4882a593Smuzhiyun 			break;
603*4882a593Smuzhiyun 		}
604*4882a593Smuzhiyun 		continue;
605*4882a593Smuzhiyun unknown:
606*4882a593Smuzhiyun 		if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
607*4882a593Smuzhiyun 			return PARSE_OPT_UNKNOWN;
608*4882a593Smuzhiyun 		ctx->out[ctx->cpidx++] = ctx->argv[0];
609*4882a593Smuzhiyun 		ctx->opt = NULL;
610*4882a593Smuzhiyun 	}
611*4882a593Smuzhiyun 	return PARSE_OPT_DONE;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun exclusive:
614*4882a593Smuzhiyun 	parse_options_usage(usagestr, options, arg, excl_short_opt);
615*4882a593Smuzhiyun 	if ((excl_short_opt && ctx->excl_opt->short_name) ||
616*4882a593Smuzhiyun 	    ctx->excl_opt->long_name == NULL) {
617*4882a593Smuzhiyun 		char opt = ctx->excl_opt->short_name;
618*4882a593Smuzhiyun 		parse_options_usage(NULL, options, &opt, 1);
619*4882a593Smuzhiyun 	} else {
620*4882a593Smuzhiyun 		parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0);
621*4882a593Smuzhiyun 	}
622*4882a593Smuzhiyun 	return PARSE_OPT_HELP;
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
parse_options_end(struct parse_opt_ctx_t * ctx)625*4882a593Smuzhiyun static int parse_options_end(struct parse_opt_ctx_t *ctx)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
628*4882a593Smuzhiyun 	ctx->out[ctx->cpidx + ctx->argc] = NULL;
629*4882a593Smuzhiyun 	return ctx->cpidx + ctx->argc;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun 
parse_options_subcommand(int argc,const char ** argv,const struct option * options,const char * const subcommands[],const char * usagestr[],int flags)632*4882a593Smuzhiyun int parse_options_subcommand(int argc, const char **argv, const struct option *options,
633*4882a593Smuzhiyun 			const char *const subcommands[], const char *usagestr[], int flags)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun 	struct parse_opt_ctx_t ctx;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	/* build usage string if it's not provided */
638*4882a593Smuzhiyun 	if (subcommands && !usagestr[0]) {
639*4882a593Smuzhiyun 		char *buf = NULL;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 		astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 		for (int i = 0; subcommands[i]; i++) {
644*4882a593Smuzhiyun 			if (i)
645*4882a593Smuzhiyun 				astrcat(&buf, "|");
646*4882a593Smuzhiyun 			astrcat(&buf, subcommands[i]);
647*4882a593Smuzhiyun 		}
648*4882a593Smuzhiyun 		astrcat(&buf, "}");
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 		usagestr[0] = buf;
651*4882a593Smuzhiyun 	}
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	parse_options_start(&ctx, argc, argv, flags);
654*4882a593Smuzhiyun 	switch (parse_options_step(&ctx, options, usagestr)) {
655*4882a593Smuzhiyun 	case PARSE_OPT_HELP:
656*4882a593Smuzhiyun 		exit(129);
657*4882a593Smuzhiyun 	case PARSE_OPT_DONE:
658*4882a593Smuzhiyun 		break;
659*4882a593Smuzhiyun 	case PARSE_OPT_LIST_OPTS:
660*4882a593Smuzhiyun 		while (options->type != OPTION_END) {
661*4882a593Smuzhiyun 			if (options->long_name)
662*4882a593Smuzhiyun 				printf("--%s ", options->long_name);
663*4882a593Smuzhiyun 			options++;
664*4882a593Smuzhiyun 		}
665*4882a593Smuzhiyun 		putchar('\n');
666*4882a593Smuzhiyun 		exit(130);
667*4882a593Smuzhiyun 	case PARSE_OPT_LIST_SUBCMDS:
668*4882a593Smuzhiyun 		if (subcommands) {
669*4882a593Smuzhiyun 			for (int i = 0; subcommands[i]; i++)
670*4882a593Smuzhiyun 				printf("%s ", subcommands[i]);
671*4882a593Smuzhiyun 		}
672*4882a593Smuzhiyun 		putchar('\n');
673*4882a593Smuzhiyun 		exit(130);
674*4882a593Smuzhiyun 	default: /* PARSE_OPT_UNKNOWN */
675*4882a593Smuzhiyun 		if (ctx.argv[0][1] == '-')
676*4882a593Smuzhiyun 			astrcatf(&error_buf, "unknown option `%s'",
677*4882a593Smuzhiyun 				 ctx.argv[0] + 2);
678*4882a593Smuzhiyun 		else
679*4882a593Smuzhiyun 			astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
680*4882a593Smuzhiyun 		usage_with_options(usagestr, options);
681*4882a593Smuzhiyun 	}
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	return parse_options_end(&ctx);
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun 
parse_options(int argc,const char ** argv,const struct option * options,const char * const usagestr[],int flags)686*4882a593Smuzhiyun int parse_options(int argc, const char **argv, const struct option *options,
687*4882a593Smuzhiyun 		  const char * const usagestr[], int flags)
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun 	return parse_options_subcommand(argc, argv, options, NULL,
690*4882a593Smuzhiyun 					(const char **) usagestr, flags);
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun #define USAGE_OPTS_WIDTH 24
694*4882a593Smuzhiyun #define USAGE_GAP         2
695*4882a593Smuzhiyun 
print_option_help(const struct option * opts,int full)696*4882a593Smuzhiyun static void print_option_help(const struct option *opts, int full)
697*4882a593Smuzhiyun {
698*4882a593Smuzhiyun 	size_t pos;
699*4882a593Smuzhiyun 	int pad;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	if (opts->type == OPTION_GROUP) {
702*4882a593Smuzhiyun 		fputc('\n', stderr);
703*4882a593Smuzhiyun 		if (*opts->help)
704*4882a593Smuzhiyun 			fprintf(stderr, "%s\n", opts->help);
705*4882a593Smuzhiyun 		return;
706*4882a593Smuzhiyun 	}
707*4882a593Smuzhiyun 	if (!full && (opts->flags & PARSE_OPT_HIDDEN))
708*4882a593Smuzhiyun 		return;
709*4882a593Smuzhiyun 	if (opts->flags & PARSE_OPT_DISABLED)
710*4882a593Smuzhiyun 		return;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	pos = fprintf(stderr, "    ");
713*4882a593Smuzhiyun 	if (opts->short_name)
714*4882a593Smuzhiyun 		pos += fprintf(stderr, "-%c", opts->short_name);
715*4882a593Smuzhiyun 	else
716*4882a593Smuzhiyun 		pos += fprintf(stderr, "    ");
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	if (opts->long_name && opts->short_name)
719*4882a593Smuzhiyun 		pos += fprintf(stderr, ", ");
720*4882a593Smuzhiyun 	if (opts->long_name)
721*4882a593Smuzhiyun 		pos += fprintf(stderr, "--%s", opts->long_name);
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	switch (opts->type) {
724*4882a593Smuzhiyun 	case OPTION_ARGUMENT:
725*4882a593Smuzhiyun 		break;
726*4882a593Smuzhiyun 	case OPTION_LONG:
727*4882a593Smuzhiyun 	case OPTION_ULONG:
728*4882a593Smuzhiyun 	case OPTION_U64:
729*4882a593Smuzhiyun 	case OPTION_INTEGER:
730*4882a593Smuzhiyun 	case OPTION_UINTEGER:
731*4882a593Smuzhiyun 		if (opts->flags & PARSE_OPT_OPTARG)
732*4882a593Smuzhiyun 			if (opts->long_name)
733*4882a593Smuzhiyun 				pos += fprintf(stderr, "[=<n>]");
734*4882a593Smuzhiyun 			else
735*4882a593Smuzhiyun 				pos += fprintf(stderr, "[<n>]");
736*4882a593Smuzhiyun 		else
737*4882a593Smuzhiyun 			pos += fprintf(stderr, " <n>");
738*4882a593Smuzhiyun 		break;
739*4882a593Smuzhiyun 	case OPTION_CALLBACK:
740*4882a593Smuzhiyun 		if (opts->flags & PARSE_OPT_NOARG)
741*4882a593Smuzhiyun 			break;
742*4882a593Smuzhiyun 		/* FALLTHROUGH */
743*4882a593Smuzhiyun 	case OPTION_STRING:
744*4882a593Smuzhiyun 		if (opts->argh) {
745*4882a593Smuzhiyun 			if (opts->flags & PARSE_OPT_OPTARG)
746*4882a593Smuzhiyun 				if (opts->long_name)
747*4882a593Smuzhiyun 					pos += fprintf(stderr, "[=<%s>]", opts->argh);
748*4882a593Smuzhiyun 				else
749*4882a593Smuzhiyun 					pos += fprintf(stderr, "[<%s>]", opts->argh);
750*4882a593Smuzhiyun 			else
751*4882a593Smuzhiyun 				pos += fprintf(stderr, " <%s>", opts->argh);
752*4882a593Smuzhiyun 		} else {
753*4882a593Smuzhiyun 			if (opts->flags & PARSE_OPT_OPTARG)
754*4882a593Smuzhiyun 				if (opts->long_name)
755*4882a593Smuzhiyun 					pos += fprintf(stderr, "[=...]");
756*4882a593Smuzhiyun 				else
757*4882a593Smuzhiyun 					pos += fprintf(stderr, "[...]");
758*4882a593Smuzhiyun 			else
759*4882a593Smuzhiyun 				pos += fprintf(stderr, " ...");
760*4882a593Smuzhiyun 		}
761*4882a593Smuzhiyun 		break;
762*4882a593Smuzhiyun 	default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
763*4882a593Smuzhiyun 	case OPTION_END:
764*4882a593Smuzhiyun 	case OPTION_GROUP:
765*4882a593Smuzhiyun 	case OPTION_BIT:
766*4882a593Smuzhiyun 	case OPTION_BOOLEAN:
767*4882a593Smuzhiyun 	case OPTION_INCR:
768*4882a593Smuzhiyun 	case OPTION_SET_UINT:
769*4882a593Smuzhiyun 	case OPTION_SET_PTR:
770*4882a593Smuzhiyun 		break;
771*4882a593Smuzhiyun 	}
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	if (pos <= USAGE_OPTS_WIDTH)
774*4882a593Smuzhiyun 		pad = USAGE_OPTS_WIDTH - pos;
775*4882a593Smuzhiyun 	else {
776*4882a593Smuzhiyun 		fputc('\n', stderr);
777*4882a593Smuzhiyun 		pad = USAGE_OPTS_WIDTH;
778*4882a593Smuzhiyun 	}
779*4882a593Smuzhiyun 	fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
780*4882a593Smuzhiyun 	if (opts->flags & PARSE_OPT_NOBUILD)
781*4882a593Smuzhiyun 		fprintf(stderr, "%*s(not built-in because %s)\n",
782*4882a593Smuzhiyun 			USAGE_OPTS_WIDTH + USAGE_GAP, "",
783*4882a593Smuzhiyun 			opts->build_opt);
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
option__cmp(const void * va,const void * vb)786*4882a593Smuzhiyun static int option__cmp(const void *va, const void *vb)
787*4882a593Smuzhiyun {
788*4882a593Smuzhiyun 	const struct option *a = va, *b = vb;
789*4882a593Smuzhiyun 	int sa = tolower(a->short_name), sb = tolower(b->short_name), ret;
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	if (sa == 0)
792*4882a593Smuzhiyun 		sa = 'z' + 1;
793*4882a593Smuzhiyun 	if (sb == 0)
794*4882a593Smuzhiyun 		sb = 'z' + 1;
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	ret = sa - sb;
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	if (ret == 0) {
799*4882a593Smuzhiyun 		const char *la = a->long_name ?: "",
800*4882a593Smuzhiyun 			   *lb = b->long_name ?: "";
801*4882a593Smuzhiyun 		ret = strcmp(la, lb);
802*4882a593Smuzhiyun 	}
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	return ret;
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun 
options__order(const struct option * opts)807*4882a593Smuzhiyun static struct option *options__order(const struct option *opts)
808*4882a593Smuzhiyun {
809*4882a593Smuzhiyun 	int nr_opts = 0, len;
810*4882a593Smuzhiyun 	const struct option *o = opts;
811*4882a593Smuzhiyun 	struct option *ordered;
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	for (o = opts; o->type != OPTION_END; o++)
814*4882a593Smuzhiyun 		++nr_opts;
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	len = sizeof(*o) * (nr_opts + 1);
817*4882a593Smuzhiyun 	ordered = malloc(len);
818*4882a593Smuzhiyun 	if (!ordered)
819*4882a593Smuzhiyun 		goto out;
820*4882a593Smuzhiyun 	memcpy(ordered, opts, len);
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	qsort(ordered, nr_opts, sizeof(*o), option__cmp);
823*4882a593Smuzhiyun out:
824*4882a593Smuzhiyun 	return ordered;
825*4882a593Smuzhiyun }
826*4882a593Smuzhiyun 
option__in_argv(const struct option * opt,const struct parse_opt_ctx_t * ctx)827*4882a593Smuzhiyun static bool option__in_argv(const struct option *opt, const struct parse_opt_ctx_t *ctx)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	int i;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	for (i = 1; i < ctx->argc; ++i) {
832*4882a593Smuzhiyun 		const char *arg = ctx->argv[i];
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 		if (arg[0] != '-') {
835*4882a593Smuzhiyun 			if (arg[1] == '\0') {
836*4882a593Smuzhiyun 				if (arg[0] == opt->short_name)
837*4882a593Smuzhiyun 					return true;
838*4882a593Smuzhiyun 				continue;
839*4882a593Smuzhiyun 			}
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 			if (opt->long_name && strcmp(opt->long_name, arg) == 0)
842*4882a593Smuzhiyun 				return true;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 			if (opt->help && strcasestr(opt->help, arg) != NULL)
845*4882a593Smuzhiyun 				return true;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 			continue;
848*4882a593Smuzhiyun 		}
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 		if (arg[1] == opt->short_name ||
851*4882a593Smuzhiyun 		    (arg[1] == '-' && opt->long_name && strcmp(opt->long_name, arg + 2) == 0))
852*4882a593Smuzhiyun 			return true;
853*4882a593Smuzhiyun 	}
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	return false;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun 
usage_with_options_internal(const char * const * usagestr,const struct option * opts,int full,struct parse_opt_ctx_t * ctx)858*4882a593Smuzhiyun static int usage_with_options_internal(const char * const *usagestr,
859*4882a593Smuzhiyun 				       const struct option *opts, int full,
860*4882a593Smuzhiyun 				       struct parse_opt_ctx_t *ctx)
861*4882a593Smuzhiyun {
862*4882a593Smuzhiyun 	struct option *ordered;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	if (!usagestr)
865*4882a593Smuzhiyun 		return PARSE_OPT_HELP;
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 	setup_pager();
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	if (error_buf) {
870*4882a593Smuzhiyun 		fprintf(stderr, "  Error: %s\n", error_buf);
871*4882a593Smuzhiyun 		zfree(&error_buf);
872*4882a593Smuzhiyun 	}
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	fprintf(stderr, "\n Usage: %s\n", *usagestr++);
875*4882a593Smuzhiyun 	while (*usagestr && **usagestr)
876*4882a593Smuzhiyun 		fprintf(stderr, "    or: %s\n", *usagestr++);
877*4882a593Smuzhiyun 	while (*usagestr) {
878*4882a593Smuzhiyun 		fprintf(stderr, "%s%s\n",
879*4882a593Smuzhiyun 				**usagestr ? "    " : "",
880*4882a593Smuzhiyun 				*usagestr);
881*4882a593Smuzhiyun 		usagestr++;
882*4882a593Smuzhiyun 	}
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	if (opts->type != OPTION_GROUP)
885*4882a593Smuzhiyun 		fputc('\n', stderr);
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	ordered = options__order(opts);
888*4882a593Smuzhiyun 	if (ordered)
889*4882a593Smuzhiyun 		opts = ordered;
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	for (  ; opts->type != OPTION_END; opts++) {
892*4882a593Smuzhiyun 		if (ctx && ctx->argc > 1 && !option__in_argv(opts, ctx))
893*4882a593Smuzhiyun 			continue;
894*4882a593Smuzhiyun 		print_option_help(opts, full);
895*4882a593Smuzhiyun 	}
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	fputc('\n', stderr);
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	free(ordered);
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	return PARSE_OPT_HELP;
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun 
usage_with_options(const char * const * usagestr,const struct option * opts)904*4882a593Smuzhiyun void usage_with_options(const char * const *usagestr,
905*4882a593Smuzhiyun 			const struct option *opts)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun 	usage_with_options_internal(usagestr, opts, 0, NULL);
908*4882a593Smuzhiyun 	exit(129);
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun 
usage_with_options_msg(const char * const * usagestr,const struct option * opts,const char * fmt,...)911*4882a593Smuzhiyun void usage_with_options_msg(const char * const *usagestr,
912*4882a593Smuzhiyun 			    const struct option *opts, const char *fmt, ...)
913*4882a593Smuzhiyun {
914*4882a593Smuzhiyun 	va_list ap;
915*4882a593Smuzhiyun 	char *tmp = error_buf;
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	va_start(ap, fmt);
918*4882a593Smuzhiyun 	if (vasprintf(&error_buf, fmt, ap) == -1)
919*4882a593Smuzhiyun 		die("vasprintf failed");
920*4882a593Smuzhiyun 	va_end(ap);
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	free(tmp);
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	usage_with_options_internal(usagestr, opts, 0, NULL);
925*4882a593Smuzhiyun 	exit(129);
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun 
parse_options_usage(const char * const * usagestr,const struct option * opts,const char * optstr,bool short_opt)928*4882a593Smuzhiyun int parse_options_usage(const char * const *usagestr,
929*4882a593Smuzhiyun 			const struct option *opts,
930*4882a593Smuzhiyun 			const char *optstr, bool short_opt)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun 	if (!usagestr)
933*4882a593Smuzhiyun 		goto opt;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	fprintf(stderr, "\n Usage: %s\n", *usagestr++);
936*4882a593Smuzhiyun 	while (*usagestr && **usagestr)
937*4882a593Smuzhiyun 		fprintf(stderr, "    or: %s\n", *usagestr++);
938*4882a593Smuzhiyun 	while (*usagestr) {
939*4882a593Smuzhiyun 		fprintf(stderr, "%s%s\n",
940*4882a593Smuzhiyun 				**usagestr ? "    " : "",
941*4882a593Smuzhiyun 				*usagestr);
942*4882a593Smuzhiyun 		usagestr++;
943*4882a593Smuzhiyun 	}
944*4882a593Smuzhiyun 	fputc('\n', stderr);
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun opt:
947*4882a593Smuzhiyun 	for (  ; opts->type != OPTION_END; opts++) {
948*4882a593Smuzhiyun 		if (short_opt) {
949*4882a593Smuzhiyun 			if (opts->short_name == *optstr) {
950*4882a593Smuzhiyun 				print_option_help(opts, 0);
951*4882a593Smuzhiyun 				break;
952*4882a593Smuzhiyun 			}
953*4882a593Smuzhiyun 			continue;
954*4882a593Smuzhiyun 		}
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 		if (opts->long_name == NULL)
957*4882a593Smuzhiyun 			continue;
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 		if (strstarts(opts->long_name, optstr))
960*4882a593Smuzhiyun 			print_option_help(opts, 0);
961*4882a593Smuzhiyun 		if (strstarts("no-", optstr) &&
962*4882a593Smuzhiyun 		    strstarts(opts->long_name, optstr + 3))
963*4882a593Smuzhiyun 			print_option_help(opts, 0);
964*4882a593Smuzhiyun 	}
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	return PARSE_OPT_HELP;
967*4882a593Smuzhiyun }
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 
parse_opt_verbosity_cb(const struct option * opt,const char * arg __maybe_unused,int unset)970*4882a593Smuzhiyun int parse_opt_verbosity_cb(const struct option *opt,
971*4882a593Smuzhiyun 			   const char *arg __maybe_unused,
972*4882a593Smuzhiyun 			   int unset)
973*4882a593Smuzhiyun {
974*4882a593Smuzhiyun 	int *target = opt->value;
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	if (unset)
977*4882a593Smuzhiyun 		/* --no-quiet, --no-verbose */
978*4882a593Smuzhiyun 		*target = 0;
979*4882a593Smuzhiyun 	else if (opt->short_name == 'v') {
980*4882a593Smuzhiyun 		if (*target >= 0)
981*4882a593Smuzhiyun 			(*target)++;
982*4882a593Smuzhiyun 		else
983*4882a593Smuzhiyun 			*target = 1;
984*4882a593Smuzhiyun 	} else {
985*4882a593Smuzhiyun 		if (*target <= 0)
986*4882a593Smuzhiyun 			(*target)--;
987*4882a593Smuzhiyun 		else
988*4882a593Smuzhiyun 			*target = -1;
989*4882a593Smuzhiyun 	}
990*4882a593Smuzhiyun 	return 0;
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun static struct option *
find_option(struct option * opts,int shortopt,const char * longopt)994*4882a593Smuzhiyun find_option(struct option *opts, int shortopt, const char *longopt)
995*4882a593Smuzhiyun {
996*4882a593Smuzhiyun 	for (; opts->type != OPTION_END; opts++) {
997*4882a593Smuzhiyun 		if ((shortopt && opts->short_name == shortopt) ||
998*4882a593Smuzhiyun 		    (opts->long_name && longopt &&
999*4882a593Smuzhiyun 		     !strcmp(opts->long_name, longopt)))
1000*4882a593Smuzhiyun 			return opts;
1001*4882a593Smuzhiyun 	}
1002*4882a593Smuzhiyun 	return NULL;
1003*4882a593Smuzhiyun }
1004*4882a593Smuzhiyun 
set_option_flag(struct option * opts,int shortopt,const char * longopt,int flag)1005*4882a593Smuzhiyun void set_option_flag(struct option *opts, int shortopt, const char *longopt,
1006*4882a593Smuzhiyun 		     int flag)
1007*4882a593Smuzhiyun {
1008*4882a593Smuzhiyun 	struct option *opt = find_option(opts, shortopt, longopt);
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	if (opt)
1011*4882a593Smuzhiyun 		opt->flags |= flag;
1012*4882a593Smuzhiyun 	return;
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun 
set_option_nobuild(struct option * opts,int shortopt,const char * longopt,const char * build_opt,bool can_skip)1015*4882a593Smuzhiyun void set_option_nobuild(struct option *opts, int shortopt,
1016*4882a593Smuzhiyun 			const char *longopt,
1017*4882a593Smuzhiyun 			const char *build_opt,
1018*4882a593Smuzhiyun 			bool can_skip)
1019*4882a593Smuzhiyun {
1020*4882a593Smuzhiyun 	struct option *opt = find_option(opts, shortopt, longopt);
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 	if (!opt)
1023*4882a593Smuzhiyun 		return;
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun 	opt->flags |= PARSE_OPT_NOBUILD;
1026*4882a593Smuzhiyun 	opt->flags |= can_skip ? PARSE_OPT_CANSKIP : 0;
1027*4882a593Smuzhiyun 	opt->build_opt = build_opt;
1028*4882a593Smuzhiyun }
1029