1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (compat_getopt.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include <retro_miscellaneous.h>
33 #include <compat/getopt.h>
34 #include <compat/strl.h>
35 #include <compat/strcasestr.h>
36 #include <compat/posix_string.h>
39 int optind, opterr, optopt;
41 static bool is_short_option(const char *str)
43 return str[0] == '-' && str[1] != '-';
46 static bool is_long_option(const char *str)
48 return str[0] == '-' && str[1] == '-';
51 static int find_short_index(char * const *argv)
54 for (idx = 0; argv[idx]; idx++)
56 if (is_short_option(argv[idx]))
63 static int find_long_index(char * const *argv)
66 for (idx = 0; argv[idx]; idx++)
68 if (is_long_option(argv[idx]))
75 static int parse_short(const char *optstring, char * const *argv)
77 bool extra_opt, takes_arg, embedded_arg;
78 const char *opt = NULL;
79 char arg = argv[0][1];
84 opt = strchr(optstring, arg);
88 extra_opt = argv[0][2];
89 takes_arg = opt[1] == ':';
91 /* If we take an argument, and we see additional characters,
92 * this is in fact the argument (i.e. -cfoo is same as -c foo). */
93 embedded_arg = extra_opt && takes_arg;
108 return optarg ? opt[0] : '?';
113 /* If we see additional characters,
114 * and they don't take arguments, this
115 * means we have multiple flags in one. */
116 memmove(&argv[0][1], &argv[0][2], strlen(&argv[0][2]) + 1);
124 static int parse_long(const struct option *longopts, char * const *argv)
128 char *argv0 = strdup(&argv[0][2]);
129 char *token = strtok_r(argv0, "=", &save);
130 const struct option *opt = NULL;
132 for (indice = 0; longopts[indice].name; indice++)
134 if (token && !strcmp(longopts[indice].name, token))
136 opt = &longopts[indice];
147 /* Handle args with '=' instead of space */
150 char *special_arg = strchr(argv[0], '=');
153 optarg = ++special_arg;
159 /* getopt_long has an "optional" arg, but we don't bother with that. */
160 if (opt->has_arg && !argv[1])
173 *opt->flag = opt->val;
180 static void shuffle_block(char **begin, char **last, char **end)
182 ptrdiff_t len = last - begin;
183 const char **tmp = (const char**)calloc(len, sizeof(const char*));
185 memcpy((void*)tmp, begin, len * sizeof(const char*));
186 memmove(begin, last, (end - last) * sizeof(const char*));
187 memcpy(end - len, tmp, len * sizeof(const char*));
192 int getopt_long(int argc, char *argv[],
193 const char *optstring, const struct option *longopts, int *longindex)
195 int short_index, long_index;
203 short_index = find_short_index(&argv[optind]);
204 long_index = find_long_index(&argv[optind]);
206 /* We're done here. */
207 if (short_index == -1 && long_index == -1)
210 /* Reorder argv so that non-options come last.
211 * Non-POSIXy, but that's what getopt does by default. */
212 if ((short_index > 0) && ((short_index < long_index) || (long_index == -1)))
214 shuffle_block(&argv[optind], &argv[optind + short_index], &argv[argc]);
217 else if ((long_index > 0) && ((long_index < short_index)
218 || (short_index == -1)))
220 shuffle_block(&argv[optind], &argv[optind + long_index], &argv[argc]);
224 if (short_index == 0)
225 return parse_short(optstring, &argv[optind]);
227 return parse_long(longopts, &argv[optind]);