xref: /aosp_15_r20/external/flashrom/cli_getopt.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  * It comes originally from the musl libc project and is licensed under the
4  * terms of the MIT license.
5  *
6  * Copyringht (C) 2023 Rich Felker and the musl authors
7  * Adjusted for flashrom by Thomas Heijligen<[email protected]>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 #include <unistd.h>
29 #include <wchar.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <stdlib.h>
33 #include "cli_classic.h"
34 #include "flash.h"
35 
36 char *optarg;
37 int optind=1, opterr=1, optopt, optpos;
38 
getopt_msg(const char * a,const char * b,const char * c,size_t l)39 static void getopt_msg(const char *a, const char *b, const char *c, size_t l)
40 {
41 	msg_gerr("%s%s%*c\n", a, b, l, c);
42 }
43 
getopt(int argc,char * const argv[],const char * optstring)44 int getopt(int argc, char * const argv[], const char *optstring)
45 {
46 	int i;
47 	wchar_t c, d;
48 	int k, l;
49 	char *optchar;
50 
51 	if (!optind) {
52 		optind = 1;
53 		optpos = 0;
54 	}
55 
56 	if (optind >= argc || !argv[optind])
57 		return -1;
58 
59 	if (argv[optind][0] != '-') {
60 		if (optstring[0] == '-') {
61 			optarg = argv[optind++];
62 			return 1;
63 		}
64 		return -1;
65 	}
66 
67 	if (!argv[optind][1])
68 		return -1;
69 
70 	if (argv[optind][1] == '-' && !argv[optind][2])
71 		return optind++, -1;
72 
73 	if (!optpos)
74 		optpos++;
75 	if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
76 		k = 1;
77 		c = 0xfffd; /* replacement char */
78 	}
79 	optchar = argv[optind]+optpos;
80 	optpos += k;
81 
82 	if (!argv[optind][optpos]) {
83 		optind++;
84 		optpos = 0;
85 	}
86 
87 	if (optstring[0] == '-' || optstring[0] == '+')
88 		optstring++;
89 
90 	i = 0;
91 	d = 0;
92 	do {
93 		l = mbtowc(&d, optstring+i, MB_LEN_MAX);
94 		if (l>0) i+=l; else i++;
95 	} while (l && d != c);
96 
97 	if (d != c || c == ':') {
98 		optopt = c;
99 		if (optstring[0] != ':' && opterr)
100 			getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
101 		return '?';
102 	}
103 	if (optstring[i] == ':') {
104 		optarg = 0;
105 		if (optstring[i+1] != ':' || optpos) {
106 			optarg = argv[optind++] + optpos;
107 			optpos = 0;
108 		}
109 		if (optind > argc) {
110 			optopt = c;
111 			if (optstring[0] == ':')
112 				return ':';
113 			if (opterr) getopt_msg(argv[0],
114 				": option requires an argument: ",
115 				optchar, k);
116 			return '?';
117 		}
118 	}
119 	return c;
120 }
121 
__getopt_long_core(int argc,char * const * argv,const char * optstring,const struct option * longopts,int * idx,int longonly)122 static int __getopt_long_core(int argc, char *const *argv, const char *optstring,
123 		const struct option *longopts, int *idx, int longonly)
124 {
125 	optarg = 0;
126 	if (longopts && argv[optind][0] == '-' &&
127 		((longonly && argv[optind][1] && argv[optind][1] != '-') ||
128 		 (argv[optind][1] == '-' && argv[optind][2])))
129 	{
130 		int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
131 		int i, cnt, match = 0;
132 		char *arg = NULL, *opt, *start = argv[optind]+1;
133 		for (cnt=i=0; longopts[i].name; i++) {
134 			const char *name = longopts[i].name;
135 			opt = start;
136 			if (*opt == '-') opt++;
137 			while (*opt && *opt != '=' && *opt == *name)
138 				name++, opt++;
139 			if (*opt && *opt != '=') continue;
140 			arg = opt;
141 			match = i;
142 			if (!*name) {
143 				cnt = 1;
144 				break;
145 			}
146 			cnt++;
147 		}
148 		if (cnt==1 && longonly && arg-start == mblen(start, MB_LEN_MAX)) {
149 			int l = arg-start;
150 			for (i=0; optstring[i]; i++) {
151 				int j;
152 				for (j=0; j<l && start[j]==optstring[i+j]; j++);
153 				if (j==l) {
154 					cnt++;
155 					break;
156 				}
157 			}
158 		}
159 		if (cnt==1) {
160 			i = match;
161 			opt = arg;
162 			optind++;
163 			if (*opt == '=') {
164 				if (!longopts[i].has_arg) {
165 					optopt = longopts[i].val;
166 					if (colon || !opterr)
167 						return '?';
168 					getopt_msg(argv[0],
169 						": option does not take an argument: ",
170 						longopts[i].name,
171 						strlen(longopts[i].name));
172 					return '?';
173 				}
174 				optarg = opt+1;
175 			} else if (longopts[i].has_arg == required_argument) {
176 				if (!(optarg = argv[optind])) {
177 					optopt = longopts[i].val;
178 					if (colon) return ':';
179 					if (!opterr) return '?';
180 					getopt_msg(argv[0],
181 						": option requires an argument: ",
182 						longopts[i].name,
183 						strlen(longopts[i].name));
184 					return '?';
185 				}
186 				optind++;
187 			}
188 			if (idx)
189 				*idx = i;
190 			if (longopts[i].flag) {
191 				*longopts[i].flag = longopts[i].val;
192 				return 0;
193 			}
194 			return longopts[i].val;
195 		}
196 		if (argv[optind][1] == '-') {
197 			optopt = 0;
198 			if (!colon && opterr)
199 				getopt_msg(argv[0], cnt ?
200 					": option is ambiguous: " :
201 					": unrecognized option: ",
202 					argv[optind]+2,
203 					strlen(argv[optind]+2));
204 			optind++;
205 			return '?';
206 		}
207 	}
208 	return getopt(argc, argv, optstring);
209 }
210 
permute(char * const * argv,int dest,int src)211 static void permute(char *const *argv, int dest, int src)
212 {
213 	char **av = (char **)argv;
214 	char *tmp = av[src];
215 	int i;
216 	for (i=src; i>dest; i--)
217 		av[i] = av[i-1];
218 	av[dest] = tmp;
219 }
220 
__getopt_long(int argc,char * const * argv,const char * optstring,const struct option * longopts,int * idx,int longonly)221 static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
222 {
223 	int ret, skipped, resumed;
224 	if (!optind) {
225 		optind = 1;
226 		optpos = 0;
227 	}
228 
229 	if (optind >= argc || !argv[optind])
230 		return -1;
231 	skipped = optind;
232 	if (optstring[0] != '+' && optstring[0] != '-') {
233 		int i;
234 		for (i=optind; ; i++) {
235 			if (i >= argc || !argv[i])
236 				return -1;
237 			if (argv[i][0] == '-' && argv[i][1])
238 				break;
239 		}
240 		optind = i;
241 	}
242 	resumed = optind;
243 	ret = __getopt_long_core(argc, argv, optstring, longopts, idx, longonly);
244 	if (resumed > skipped) {
245 		int i, cnt = optind-resumed;
246 		for (i=0; i<cnt; i++)
247 			permute(argv, skipped, optind-1);
248 		optind = skipped + cnt;
249 	}
250 	return ret;
251 }
252 
getopt_long(int argc,char * const * argv,const char * optstring,const struct option * longopts,int * idx)253 int getopt_long(int argc, char *const *argv, const char *optstring,
254 		const struct option *longopts, int *idx)
255 {
256 	return __getopt_long(argc, argv, optstring, longopts, idx, 0);
257 }
258 
getopt_long_only(int argc,char * const * argv,const char * optstring,const struct option * longopts,int * idx)259 int getopt_long_only(int argc, char *const *argv, const char *optstring,
260 		const struct option *longopts, int *idx)
261 {
262 	return __getopt_long(argc, argv, optstring, longopts, idx, 1);
263 }
264