| 1 | /* gopt.c PUBILC DOMAIN 2015 [email protected] */cxx . . $ `<file>/tools/gopt.c`/ |
| 2 | |
| 3 | /* <http:///www.purposeful.co.uk/software/gopt> */ |
| 4 | |
| 5 | /* |
| 6 | I, Tom Vajzovic, am the author of this software and its documentation. |
| 7 | I permanently abandon all intellectual property rights in them, including |
| 8 | copyright, trademarks, design rights, database right, patents, and the right |
| 9 | to be identified as the author. |
| 10 | |
| 11 | I am fairly certain that the software does what the documentation says it |
| 12 | does, but I do not guarantee that it does, or that it does what you think it |
| 13 | should. I do not guarantee that it will not have undesirable side effects. |
| 14 | |
| 15 | You are free to use, modify and distribute this software as you please, but |
| 16 | you do so at your own risk. If you do not pass on this warning then you may |
| 17 | be responsible for any problems encountered by those who obtain the software |
| 18 | through you. |
| 19 | */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | #ifdef _WIN32 |
| 24 | #define strncasecmp _strnicmp |
| 25 | #else |
| 26 | #include <strings.h> |
| 27 | #endif |
| 28 | |
| 29 | #include "gopt.h"cxx . . $ `<file>/tools/gopt.h`/ |
| 30 | |
| 31 | |
| 32 | /* returns index of first exact match or only prefix match, or last index */ |
| 33 | static unsigned int long_option_get_indexcxx . . $ long_option_get_index(cbbb4d16e9caa93). (const char *arg, const struct optioncxx . . $ option# *options) |
| 34 | { |
| 35 | unsigned int count = 0; |
| 36 | unsigned int found; |
| 37 | unsigned int i; |
| 38 | |
| 39 | size_tcxx . . $ size_t# arg_len = strcspncxx . . $ strcspn(335c1dc86f5c867f). (arg, "="); |
| 40 | |
| 41 | for (i = 0; !(options[i].flagscxx . . $ option#flags. & GOPT_LASTcxx . . $ `tools/gopt.h:52:9`!); i++) |
| 42 | { |
| 43 | if (options[i].long_namecxx . . $ option#long_name.) |
| 44 | { |
| 45 | size_tcxx . . $ size_t# full_len = strlencxx . . $ strlen(751346f8406fd082). (options[i].long_namecxx . . $ option#long_name.); |
| 46 | |
| 47 | if ((arg_len <= full_len) && !strncasecmpcxx . . $ strncasecmp(fd278af2f295fa2e). (options[i].long_namecxx . . $ option#long_name., arg, arg_len)) |
| 48 | { |
| 49 | if (arg_len == full_len) |
| 50 | { |
| 51 | return i; |
| 52 | } |
| 53 | |
| 54 | found = i; |
| 55 | count++; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return ((count == 1) ? found : i); |
| 61 | } |
| 62 | |
| 63 | /* returns index of first match, or last index */ |
| 64 | static unsigned int short_option_get_indexcxx . . $ short_option_get_index(7badd6a7b82bb9c1). (char c, const struct optioncxx . . $ option# *options) |
| 65 | { |
| 66 | unsigned int i; |
| 67 | |
| 68 | for (i = 0; !(options[i].flagscxx . . $ option#flags. & GOPT_LASTcxx . . $ `tools/gopt.h:52:9`!); i++) |
| 69 | { |
| 70 | if (options[i].short_namecxx . . $ option#short_name. == c) |
| 71 | { |
| 72 | return i; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return i; |
| 77 | } |
| 78 | |
| 79 | int goptcxx . . $ gopt(213a04db008bd700). (char **argv, struct optioncxx . . $ option# *options) |
| 80 | { |
| 81 | unsigned int operand_count = 1; |
| 82 | unsigned int doubledash = 0; |
| 83 | unsigned int expecting = 0; |
| 84 | unsigned int option_index = 0; |
| 85 | unsigned int i, j; |
| 86 | |
| 87 | for (i = 0; !(options[i].flagscxx . . $ option#flags. & GOPT_LASTcxx . . $ `tools/gopt.h:52:9`!); i++) |
| 88 | { |
| 89 | options[i].argumentcxx . . $ option#argument. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 90 | options[i].countcxx . . $ option#count. = 0; |
| 91 | } |
| 92 | |
| 93 | options[i].short_namecxx . . $ option#short_name. = 0; |
| 94 | options[i].long_namecxx . . $ option#long_name. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 95 | options[i].argumentcxx . . $ option#argument. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 96 | options[i].countcxx . . $ option#count. = 0; |
| 97 | |
| 98 | for (i = 1; argv[i]; i++) |
| 99 | { |
| 100 | if (doubledash) |
| 101 | { |
| 102 | argv[operand_count] = argv[i]; |
| 103 | operand_count++; |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | if (expecting) |
| 108 | { |
| 109 | if ((argv[i][0] != '-') || !argv[i][1] || !(options[option_index].flagscxx . . $ option#flags. & GOPT_ARGUMENT_NO_HYPHENcxx . . $ `tools/gopt.h:46:9`!)) |
| 110 | { |
| 111 | options[option_index].flagscxx . . $ option#flags. |= expecting; |
| 112 | options[option_index].argumentcxx . . $ option#argument. = argv[i]; |
| 113 | expecting = 0; |
| 114 | continue; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | options[option_index].flagscxx . . $ option#flags. |= (expecting >> 1); /* change _WITH to _WITHOUT */ |
| 119 | options[option_index].argumentcxx . . $ option#argument. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 120 | expecting = 0; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if ((argv[i][0] == '-') && (argv[i][1] == '-') && (argv[i][2] == 0)) |
| 125 | { |
| 126 | doubledash = 1; |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | if ((argv[i][0] == '-') && (argv[i][1] == '-')) |
| 131 | { |
| 132 | char *eq = strchrcxx . . $ strchr(2050848944e00d54). (&argv[i][2], '='); |
| 133 | |
| 134 | option_index = long_option_get_indexcxx . . $ long_option_get_index(cbbb4d16e9caa93). (&argv[i][2], options); |
| 135 | |
| 136 | options[option_index].countcxx . . $ option#count.++; |
| 137 | |
| 138 | if ((options[option_index].flagscxx . . $ option#flags. & GOPT_LASTcxx . . $ `tools/gopt.h:52:9`!) && !options[option_index].long_namecxx . . $ option#long_name.) |
| 139 | { |
| 140 | options[option_index].long_namecxx . . $ option#long_name. = &argv[i][2]; |
| 141 | } |
| 142 | |
| 143 | if (eq) |
| 144 | { |
| 145 | options[option_index].argumentcxx . . $ option#argument. = (eq + 1); |
| 146 | options[option_index].flagscxx . . $ option#flags. |= GOPT_SEEN_LONG_WITHcxx . . $ `tools/gopt.h:51:9`!; |
| 147 | } |
| 148 | else if (options[option_index].flagscxx . . $ option#flags. & GOPT_ARGUMENT_REQUIREDcxx . . $ `tools/gopt.h:45:9`!) |
| 149 | { |
| 150 | expecting = GOPT_SEEN_LONG_WITHcxx . . $ `tools/gopt.h:51:9`!; |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | options[option_index].argumentcxx . . $ option#argument. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 155 | options[option_index].flagscxx . . $ option#flags. |= GOPT_SEEN_LONG_WITHOUTcxx . . $ `tools/gopt.h:50:9`!; |
| 156 | } |
| 157 | } |
| 158 | else if ((argv[i][0] == '-') && argv[i][1]) |
| 159 | { |
| 160 | for (j = 1; argv[i][j]; j++) |
| 161 | { |
| 162 | option_index = short_option_get_indexcxx . . $ short_option_get_index(7badd6a7b82bb9c1). (argv[i][j], options); |
| 163 | |
| 164 | options[option_index].countcxx . . $ option#count.++; |
| 165 | |
| 166 | if (options[option_index].flagscxx . . $ option#flags. & GOPT_LASTcxx . . $ `tools/gopt.h:52:9`!) |
| 167 | { |
| 168 | if (!options[option_index].short_namecxx . . $ option#short_name.) |
| 169 | { |
| 170 | options[option_index].short_namecxx . . $ option#short_name. = argv[i][j]; |
| 171 | } |
| 172 | |
| 173 | if (argv[i][j+1]) |
| 174 | { |
| 175 | options[option_index].argumentcxx . . $ option#argument. = &argv[i][j+1]; |
| 176 | options[option_index].flagscxx . . $ option#flags. |= GOPT_SEEN_SHORT_WITHcxx . . $ `tools/gopt.h:49:9`!; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | options[option_index].argumentcxx . . $ option#argument. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 181 | options[option_index].flagscxx . . $ option#flags. |= GOPT_SEEN_SHORT_WITHOUTcxx . . $ `tools/gopt.h:48:9`!; |
| 182 | } |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | if (options[option_index].flagscxx . . $ option#flags. & GOPT_ARGUMENT_FORBIDDENcxx . . $ `tools/gopt.h:44:9`!) |
| 187 | { |
| 188 | options[option_index].argumentcxx . . $ option#argument. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 189 | options[option_index].flagscxx . . $ option#flags. |= GOPT_SEEN_SHORT_WITHOUTcxx . . $ `tools/gopt.h:48:9`!; |
| 190 | } |
| 191 | |
| 192 | else if (argv[i][j+1]) |
| 193 | { |
| 194 | options[option_index].argumentcxx . . $ option#argument. = &argv[i][j+1]; |
| 195 | options[option_index].flagscxx . . $ option#flags. |= GOPT_SEEN_SHORT_WITHcxx . . $ `tools/gopt.h:49:9`!; |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | else if (options[option_index].flagscxx . . $ option#flags. & GOPT_ARGUMENT_REQUIREDcxx . . $ `tools/gopt.h:45:9`!) |
| 200 | { |
| 201 | expecting = GOPT_SEEN_SHORT_WITHcxx . . $ `tools/gopt.h:49:9`!; |
| 202 | } |
| 203 | |
| 204 | else |
| 205 | { |
| 206 | options[option_index].argumentcxx . . $ option#argument. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 207 | options[option_index].flagscxx . . $ option#flags. |= GOPT_SEEN_SHORT_WITHOUTcxx . . $ `tools/gopt.h:48:9`!; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | argv[operand_count] = argv[i]; |
| 214 | operand_count++; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (expecting) |
| 219 | { |
| 220 | options[option_index].flagscxx . . $ option#flags. |= (expecting >> 1); /* change _WITH to _WITHOUT */ |
| 221 | options[option_index].argumentcxx . . $ option#argument. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 222 | } |
| 223 | |
| 224 | argv[operand_count] = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 225 | return operand_count; |
| 226 | } |