| 1 | /* gopt.c PUBILC DOMAIN 2015 [email protected] */ |
| 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" |
| 30 | |
| 31 | |
| 32 | /* returns index of first exact match or only prefix match, or last index */ |
| 33 | static unsigned int long_option_get_index (const char *arg, const struct option *options) |
| 34 | { |
| 35 | unsigned int count = 0; |
| 36 | unsigned int found; |
| 37 | unsigned int i; |
| 38 | |
| 39 | size_t arg_len = strcspn (arg, "="); |
| 40 | |
| 41 | for (i = 0; !(options[i].flags & GOPT_LAST); i++) |
| 42 | { |
| 43 | if (options[i].long_name) |
| 44 | { |
| 45 | size_t full_len = strlen (options[i].long_name); |
| 46 | |
| 47 | if ((arg_len <= full_len) && !strncasecmp (options[i].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_index (char c, const struct option *options) |
| 65 | { |
| 66 | unsigned int i; |
| 67 | |
| 68 | for (i = 0; !(options[i].flags & GOPT_LAST); i++) |
| 69 | { |
| 70 | if (options[i].short_name == c) |
| 71 | { |
| 72 | return i; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return i; |
| 77 | } |
| 78 | |
| 79 | int gopt (char **argv, struct 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].flags & GOPT_LAST); i++) |
| 88 | { |
| 89 | options[i].argument = NULL; |
| 90 | options[i].count = 0; |
| 91 | } |
| 92 | |
| 93 | options[i].short_name = 0; |
| 94 | options[i].long_name = NULL; |
| 95 | options[i].argument = NULL; |
| 96 | options[i].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].flags & GOPT_ARGUMENT_NO_HYPHEN)) |
| 110 | { |
| 111 | options[option_index].flags |= expecting; |
| 112 | options[option_index].argument = argv[i]; |
| 113 | expecting = 0; |
| 114 | continue; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | options[option_index].flags |= (expecting >> 1); /* change _WITH to _WITHOUT */ |
| 119 | options[option_index].argument = NULL; |
| 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 = strchr (&argv[i][2], '='); |
| 133 | |
| 134 | option_index = long_option_get_index (&argv[i][2], options); |
| 135 | |
| 136 | options[option_index].count++; |
| 137 | |
| 138 | if ((options[option_index].flags & GOPT_LAST) && !options[option_index].long_name) |
| 139 | { |
| 140 | options[option_index].long_name = &argv[i][2]; |
| 141 | } |
| 142 | |
| 143 | if (eq) |
| 144 | { |
| 145 | options[option_index].argument = (eq + 1); |
| 146 | options[option_index].flags |= GOPT_SEEN_LONG_WITH; |
| 147 | } |
| 148 | else if (options[option_index].flags & GOPT_ARGUMENT_REQUIRED) |
| 149 | { |
| 150 | expecting = GOPT_SEEN_LONG_WITH; |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | options[option_index].argument = NULL; |
| 155 | options[option_index].flags |= GOPT_SEEN_LONG_WITHOUT; |
| 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_index (argv[i][j], options); |
| 163 | |
| 164 | options[option_index].count++; |
| 165 | |
| 166 | if (options[option_index].flags & GOPT_LAST) |
| 167 | { |
| 168 | if (!options[option_index].short_name) |
| 169 | { |
| 170 | options[option_index].short_name = argv[i][j]; |
| 171 | } |
| 172 | |
| 173 | if (argv[i][j+1]) |
| 174 | { |
| 175 | options[option_index].argument = &argv[i][j+1]; |
| 176 | options[option_index].flags |= GOPT_SEEN_SHORT_WITH; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | options[option_index].argument = NULL; |
| 181 | options[option_index].flags |= GOPT_SEEN_SHORT_WITHOUT; |
| 182 | } |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | if (options[option_index].flags & GOPT_ARGUMENT_FORBIDDEN) |
| 187 | { |
| 188 | options[option_index].argument = NULL; |
| 189 | options[option_index].flags |= GOPT_SEEN_SHORT_WITHOUT; |
| 190 | } |
| 191 | |
| 192 | else if (argv[i][j+1]) |
| 193 | { |
| 194 | options[option_index].argument = &argv[i][j+1]; |
| 195 | options[option_index].flags |= GOPT_SEEN_SHORT_WITH; |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | else if (options[option_index].flags & GOPT_ARGUMENT_REQUIRED) |
| 200 | { |
| 201 | expecting = GOPT_SEEN_SHORT_WITH; |
| 202 | } |
| 203 | |
| 204 | else |
| 205 | { |
| 206 | options[option_index].argument = NULL; |
| 207 | options[option_index].flags |= GOPT_SEEN_SHORT_WITHOUT; |
| 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].flags |= (expecting >> 1); /* change _WITH to _WITHOUT */ |
| 221 | options[option_index].argument = NULL; |
| 222 | } |
| 223 | |
| 224 | argv[operand_count] = NULL; |
| 225 | return operand_count; |
| 226 | } |