| 1 |
/* |
| 2 |
* C Reusables |
| 3 |
* <http://github.com/mity/c-reusables> |
| 4 |
* |
| 5 |
* Copyright (c) 2017-2020 Martin Mitáš |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
* copy of this software and associated documentation files (the "Software"), |
| 9 |
* to deal in the Software without restriction, including without limitation |
| 10 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in |
| 15 |
* all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 |
* IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "cmdline.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <string.h> |
| 30 |
|
| 31 |
|
| 32 |
#ifdef _WIN32 |
| 33 |
#define snprintf _snprintf |
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
#define CMDLINE_AUXBUF_SIZE 32 |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static int |
| 42 |
cmdline_handle_short_opt_group(const CMDLINE_OPTION* options, const char* arggroup, |
| 43 |
int (*callback)(int /*optval*/, const char* /*arg*/, void* /*userdata*/), |
| 44 |
void* userdata) |
| 45 |
{ |
| 46 |
const CMDLINE_OPTION* opt; |
| 47 |
int i; |
| 48 |
int ret = 0; |
| 49 |
|
| 50 |
for(i = 0; arggroup[i] != '\0'; i++) { |
| 51 |
for(opt = options; opt->id != 0; opt++) { |
| 52 |
if(arggroup[i] == opt->shortname) |
| 53 |
break; |
| 54 |
} |
| 55 |
|
| 56 |
if(opt->id != 0 && !(opt->flags & CMDLINE_OPTFLAG_REQUIREDARG)) { |
| 57 |
ret = callback(opt->id, NULL, userdata); |
| 58 |
} else { |
| 59 |
/* Unknown option. */ |
| 60 |
char badoptname[3]; |
| 61 |
badoptname[0] = '-'; |
| 62 |
badoptname[1] = arggroup[i]; |
| 63 |
badoptname[2] = '\0'; |
| 64 |
ret = callback((opt->id != 0 ? CMDLINE_OPTID_MISSINGARG : CMDLINE_OPTID_UNKNOWN), |
| 65 |
badoptname, userdata); |
| 66 |
} |
| 67 |
|
| 68 |
if(ret != 0) |
| 69 |
break; |
| 70 |
} |
| 71 |
|
| 72 |
return ret; |
| 73 |
} |
| 74 |
|
| 75 |
int |
| 76 |
cmdline_read(const CMDLINE_OPTION* options, int argc, char** argv, |
| 77 |
int (*callback)(int /*optval*/, const char* /*arg*/, void* /*userdata*/), |
| 78 |
void* userdata) |
| 79 |
{ |
| 80 |
const CMDLINE_OPTION* opt; |
| 81 |
char auxbuf[CMDLINE_AUXBUF_SIZE+1]; |
| 82 |
int fast_optarg_decision = 1; |
| 83 |
int after_doubledash = 0; |
| 84 |
int i = 1; |
| 85 |
int ret = 0; |
| 86 |
|
| 87 |
auxbuf[CMDLINE_AUXBUF_SIZE] = '\0'; |
| 88 |
|
| 89 |
/* Check whether there is any CMDLINE_OPTFLAG_COMPILERLIKE option with |
| 90 |
* a name not starting with '-'. That would imply we can to check for |
| 91 |
* non-option arguments only after refusing all such options. */ |
| 92 |
for(opt = options; opt->id != 0; opt++) { |
| 93 |
if((opt->flags & CMDLINE_OPTFLAG_COMPILERLIKE) && opt->longname[0] != '-') |
| 94 |
fast_optarg_decision = 0; |
| 95 |
} |
| 96 |
|
| 97 |
while(i < argc) { |
| 98 |
if(after_doubledash || strcmp(argv[i], "-") == 0) { |
| 99 |
/* Non-option argument. |
| 100 |
* Standalone "-" usually means "read from stdin" or "write to |
| 101 |
* stdout" so treat it always as a non-option. */ |
| 102 |
ret = callback(CMDLINE_OPTID_NONE, argv[i], userdata); |
| 103 |
} else if(strcmp(argv[i], "--") == 0) { |
| 104 |
/* End of options. All the remaining tokens are non-options |
| 105 |
* even if they start with a dash. */ |
| 106 |
after_doubledash = 1; |
| 107 |
} else if(fast_optarg_decision && argv[i][0] != '-') { |
| 108 |
/* Non-option argument. */ |
| 109 |
ret = callback(CMDLINE_OPTID_NONE, argv[i], userdata); |
| 110 |
} else { |
| 111 |
for(opt = options; opt->id != 0; opt++) { |
| 112 |
if(opt->flags & CMDLINE_OPTFLAG_COMPILERLIKE) { |
| 113 |
size_t len = strlen(opt->longname); |
| 114 |
if(strncmp(argv[i], opt->longname, len) == 0) { |
| 115 |
/* Compiler-like option. */ |
| 116 |
if(argv[i][len] != '\0') |
| 117 |
ret = callback(opt->id, argv[i] + len, userdata); |
| 118 |
else if(i+1 < argc) |
| 119 |
ret = callback(opt->id, argv[++i], userdata); |
| 120 |
else |
| 121 |
ret = callback(CMDLINE_OPTID_MISSINGARG, opt->longname, userdata); |
| 122 |
break; |
| 123 |
} |
| 124 |
} else if(opt->longname != NULL && strncmp(argv[i], "--", 2) == 0) { |
| 125 |
size_t len = strlen(opt->longname); |
| 126 |
if(strncmp(argv[i]+2, opt->longname, len) == 0) { |
| 127 |
/* Regular long option. */ |
| 128 |
if(argv[i][2+len] == '\0') { |
| 129 |
/* with no argument provided. */ |
| 130 |
if(!(opt->flags & CMDLINE_OPTFLAG_REQUIREDARG)) |
| 131 |
ret = callback(opt->id, NULL, userdata); |
| 132 |
else |
| 133 |
ret = callback(CMDLINE_OPTID_MISSINGARG, argv[i], userdata); |
| 134 |
break; |
| 135 |
} else if(argv[i][2+len] == '=') { |
| 136 |
/* with an argument provided. */ |
| 137 |
if(opt->flags & (CMDLINE_OPTFLAG_OPTIONALARG | CMDLINE_OPTFLAG_REQUIREDARG)) { |
| 138 |
ret = callback(opt->id, argv[i]+2+len+1, userdata); |
| 139 |
} else { |
| 140 |
snprintf(auxbuf, CMDLINE_AUXBUF_SIZE, "--%s", opt->longname); |
| 141 |
ret = callback(CMDLINE_OPTID_BOGUSARG, auxbuf, userdata); |
| 142 |
} |
| 143 |
break; |
| 144 |
} else { |
| 145 |
continue; |
| 146 |
} |
| 147 |
} |
| 148 |
} else if(opt->shortname != '\0' && argv[i][0] == '-') { |
| 149 |
if(argv[i][1] == opt->shortname) { |
| 150 |
/* Regular short option. */ |
| 151 |
if(opt->flags & CMDLINE_OPTFLAG_REQUIREDARG) { |
| 152 |
if(argv[i][2] != '\0') |
| 153 |
ret = callback(opt->id, argv[i]+2, userdata); |
| 154 |
else if(i+1 < argc) |
| 155 |
ret = callback(opt->id, argv[++i], userdata); |
| 156 |
else |
| 157 |
ret = callback(CMDLINE_OPTID_MISSINGARG, argv[i], userdata); |
| 158 |
break; |
| 159 |
} else { |
| 160 |
ret = callback(opt->id, NULL, userdata); |
| 161 |
|
| 162 |
/* There might be more (argument-less) short options |
| 163 |
* grouped together. */ |
| 164 |
if(ret == 0 && argv[i][2] != '\0') |
| 165 |
ret = cmdline_handle_short_opt_group(options, argv[i]+2, callback, userdata); |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if(opt->id == 0) { /* still not handled? */ |
| 173 |
if(argv[i][0] != '-') { |
| 174 |
/* Non-option argument. */ |
| 175 |
ret = callback(CMDLINE_OPTID_NONE, argv[i], userdata); |
| 176 |
} else { |
| 177 |
/* Unknown option. */ |
| 178 |
char* badoptname = argv[i]; |
| 179 |
|
| 180 |
if(strncmp(badoptname, "--", 2) == 0) { |
| 181 |
/* Strip any argument from the long option. */ |
| 182 |
char* assignment = strchr(badoptname, '='); |
| 183 |
if(assignment != NULL) { |
| 184 |
size_t len = assignment - badoptname; |
| 185 |
if(len > CMDLINE_AUXBUF_SIZE) |
| 186 |
len = CMDLINE_AUXBUF_SIZE; |
| 187 |
strncpy(auxbuf, badoptname, len); |
| 188 |
auxbuf[len] = '\0'; |
| 189 |
badoptname = auxbuf; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
ret = callback(CMDLINE_OPTID_UNKNOWN, badoptname, userdata); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if(ret != 0) |
| 199 |
return ret; |
| 200 |
i++; |
| 201 |
} |
| 202 |
|
| 203 |
return ret; |
| 204 |
} |
| 205 |
|