| 1 | /* Copyright (C) 2010-2020 The RetroArch team |
| 2 | * |
| 3 | * --------------------------------------------------------------------------------------- |
| 4 | * The following license statement only applies to this file (compat_getopt.c). |
| 5 | * --------------------------------------------------------------------------------------- |
| 6 | * |
| 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: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 14 | * |
| 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. |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <ctype.h> |
| 25 | |
| 26 | #include <string.h> |
| 27 | #include <boolean.h> |
| 28 | #include <stddef.h> |
| 29 | #include <stdlib.h> |
| 30 | |
| 31 | #include <retro_miscellaneous.h> |
| 32 | |
| 33 | #include <compat/getopt.h> |
| 34 | #include <compat/strl.h> |
| 35 | #include <compat/strcasestr.h> |
| 36 | #include <compat/posix_string.h> |
| 37 | |
| 38 | char *optarg; |
| 39 | int optind, opterr, optopt; |
| 40 | |
| 41 | static bool is_short_option(const char *str) |
| 42 | { |
| 43 | return str[0] == '-' && str[1] != '-'; |
| 44 | } |
| 45 | |
| 46 | static bool is_long_option(const char *str) |
| 47 | { |
| 48 | return str[0] == '-' && str[1] == '-'; |
| 49 | } |
| 50 | |
| 51 | static int find_short_index(char * const *argv) |
| 52 | { |
| 53 | int idx; |
| 54 | for (idx = 0; argv[idx]; idx++) |
| 55 | { |
| 56 | if (is_short_option(argv[idx])) |
| 57 | return idx; |
| 58 | } |
| 59 | |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | static int find_long_index(char * const *argv) |
| 64 | { |
| 65 | int idx; |
| 66 | for (idx = 0; argv[idx]; idx++) |
| 67 | { |
| 68 | if (is_long_option(argv[idx])) |
| 69 | return idx; |
| 70 | } |
| 71 | |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | static int parse_short(const char *optstring, char * const *argv) |
| 76 | { |
| 77 | bool extra_opt, takes_arg, embedded_arg; |
| 78 | const char *opt = NULL; |
| 79 | char arg = argv[0][1]; |
| 80 | |
| 81 | if (arg == ':') |
| 82 | return '?'; |
| 83 | |
| 84 | opt = strchr(optstring, arg); |
| 85 | if (!opt) |
| 86 | return '?'; |
| 87 | |
| 88 | extra_opt = argv[0][2]; |
| 89 | takes_arg = opt[1] == ':'; |
| 90 | |
| 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; |
| 94 | |
| 95 | if (takes_arg) |
| 96 | { |
| 97 | if (embedded_arg) |
| 98 | { |
| 99 | optarg = argv[0] + 2; |
| 100 | optind++; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | optarg = argv[1]; |
| 105 | optind += 2; |
| 106 | } |
| 107 | |
| 108 | return optarg ? opt[0] : '?'; |
| 109 | } |
| 110 | |
| 111 | if (embedded_arg) |
| 112 | { |
| 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); |
| 117 | return opt[0]; |
| 118 | } |
| 119 | |
| 120 | optind++; |
| 121 | return opt[0]; |
| 122 | } |
| 123 | |
| 124 | static int parse_long(const struct option *longopts, char * const *argv) |
| 125 | { |
| 126 | size_t indice; |
| 127 | char *save = NULL; |
| 128 | char *argv0 = strdup(&argv[0][2]); |
| 129 | char *token = strtok_r(argv0, "=", &save); |
| 130 | const struct option *opt = NULL; |
| 131 | |
| 132 | for (indice = 0; longopts[indice].name; indice++) |
| 133 | { |
| 134 | if (token && !strcmp(longopts[indice].name, token)) |
| 135 | { |
| 136 | opt = &longopts[indice]; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | free(argv0); |
| 142 | argv0 = NULL; |
| 143 | |
| 144 | if (!opt) |
| 145 | return '?'; |
| 146 | |
| 147 | /* Handle args with '=' instead of space */ |
| 148 | if (opt->has_arg) |
| 149 | { |
| 150 | char *special_arg = strchr(argv[0], '='); |
| 151 | if (special_arg) |
| 152 | { |
| 153 | optarg = ++special_arg; |
| 154 | optind++; |
| 155 | return opt->val; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* getopt_long has an "optional" arg, but we don't bother with that. */ |
| 160 | if (opt->has_arg && !argv[1]) |
| 161 | return '?'; |
| 162 | |
| 163 | if (opt->has_arg) |
| 164 | { |
| 165 | optarg = argv[1]; |
| 166 | optind += 2; |
| 167 | } |
| 168 | else |
| 169 | optind++; |
| 170 | |
| 171 | if (opt->flag) |
| 172 | { |
| 173 | *opt->flag = opt->val; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | return opt->val; |
| 178 | } |
| 179 | |
| 180 | static void shuffle_block(char **begin, char **last, char **end) |
| 181 | { |
| 182 | ptrdiff_t len = last - begin; |
| 183 | const char **tmp = (const char**)calloc(len, sizeof(const char*)); |
| 184 | |
| 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*)); |
| 188 | |
| 189 | free((void*)tmp); |
| 190 | } |
| 191 | |
| 192 | int getopt_long(int argc, char *argv[], |
| 193 | const char *optstring, const struct option *longopts, int *longindex) |
| 194 | { |
| 195 | int short_index, long_index; |
| 196 | |
| 197 | if (optind == 0) |
| 198 | optind = 1; |
| 199 | |
| 200 | if (argc < 2) |
| 201 | return -1; |
| 202 | |
| 203 | short_index = find_short_index(&argv[optind]); |
| 204 | long_index = find_long_index(&argv[optind]); |
| 205 | |
| 206 | /* We're done here. */ |
| 207 | if (short_index == -1 && long_index == -1) |
| 208 | return -1; |
| 209 | |
| 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))) |
| 213 | { |
| 214 | shuffle_block(&argv[optind], &argv[optind + short_index], &argv[argc]); |
| 215 | short_index = 0; |
| 216 | } |
| 217 | else if ((long_index > 0) && ((long_index < short_index) |
| 218 | || (short_index == -1))) |
| 219 | { |
| 220 | shuffle_block(&argv[optind], &argv[optind + long_index], &argv[argc]); |
| 221 | long_index = 0; |
| 222 | } |
| 223 | |
| 224 | if (short_index == 0) |
| 225 | return parse_short(optstring, &argv[optind]); |
| 226 | if (long_index == 0) |
| 227 | return parse_long(longopts, &argv[optind]); |
| 228 | |
| 229 | return '?'; |
| 230 | } |