* Get next command line option. */
| 213 | * Get next command line option. |
| 214 | */ |
| 215 | static OptID getCmdOption(const CLI_options *options, int argc, char **argv, |
| 216 | char **opt_argv, int *idx) |
| 217 | { |
| 218 | typedef enum { O_SEARCH, O_FOUND, O_NOTFOUND, O_DONE } State; |
| 219 | OptID opt_id = DILLO_CLI_NONE; |
| 220 | int i = 0; |
| 221 | State state = O_SEARCH; |
| 222 | |
| 223 | if (*idx >= argc) { |
| 224 | state = O_DONE; |
| 225 | } else { |
| 226 | state = O_NOTFOUND; |
| 227 | for (i = 0; options[i].shortopt; i++) { |
| 228 | if (strcmp(options[i].shortopt, argv[*idx]) == 0 || |
| 229 | strcmp(options[i].longopt, argv[*idx]) == 0) { |
| 230 | state = O_FOUND; |
| 231 | ++*idx; |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | if (state == O_FOUND) { |
| 237 | int n_arg = options[i].opt_argc; |
| 238 | opt_id = options[i].id; |
| 239 | /* Find the required/optional arguments of the option */ |
| 240 | for (i = 0; *idx < argc && i < abs(n_arg) && argv[*idx][0] != '-'; i++) |
| 241 | opt_argv[i] = argv[(*idx)++]; |
| 242 | opt_argv[i] = NULL; |
| 243 | |
| 244 | /* Optional arguments have opt_argc < 0 */ |
| 245 | if (i < n_arg) { |
| 246 | fprintf(stderr, "Option %s requires %d argument%s\n", |
| 247 | argv[*idx-i-1], n_arg, (n_arg == 1) ? "" : "s"); |
| 248 | opt_id = DILLO_CLI_ERROR; |
| 249 | } |
| 250 | } |
| 251 | if (state == O_NOTFOUND) { |
| 252 | if (strcmp(argv[*idx], "--") == 0) |
| 253 | (*idx)++; |
| 254 | else if (argv[*idx][0] == '-') { |
| 255 | fprintf(stderr, "Command line option \"%s\" not recognized.\n", |
| 256 | argv[*idx]); |
| 257 | opt_id = DILLO_CLI_ERROR; |
| 258 | } |
| 259 | } |
| 260 | return opt_id; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Set FL_NORMAL_LABEL to interpret neither symbols (@) nor shortcuts (&), |