* parse_long_options -- * Parse long options in argc/argv argument vector. * Returns -1 if short_too is set and the option does not match long_options. */
| 148 | * Returns -1 if short_too is set and the option does not match long_options. |
| 149 | */ |
| 150 | static int |
| 151 | parse_long_options(char * const *nargv, const char *options, |
| 152 | const struct option *long_options, int *idx, int short_too) |
| 153 | { |
| 154 | char *current_argv, *has_equal; |
| 155 | size_t current_argv_len; |
| 156 | int i, match; |
| 157 | |
| 158 | current_argv = place; |
| 159 | match = -1; |
| 160 | |
| 161 | optind++; |
| 162 | |
| 163 | if ((has_equal = strchr(current_argv, '=')) != NULL) { |
| 164 | /* argument found (--option=arg) */ |
| 165 | current_argv_len = has_equal - current_argv; |
| 166 | has_equal++; |
| 167 | } else |
| 168 | current_argv_len = strlen(current_argv); |
| 169 | |
| 170 | for (i = 0; long_options[i].name; i++) { |
| 171 | /* find matching long option */ |
| 172 | if (strncmp(current_argv, long_options[i].name, |
| 173 | current_argv_len)) |
| 174 | continue; |
| 175 | |
| 176 | if (strlen(long_options[i].name) == current_argv_len) { |
| 177 | /* exact match */ |
| 178 | match = i; |
| 179 | break; |
| 180 | } |
| 181 | /* |
| 182 | * If this is a known short option, don't allow |
| 183 | * a partial match of a single character. |
| 184 | */ |
| 185 | if (short_too && current_argv_len == 1) |
| 186 | continue; |
| 187 | |
| 188 | if (match == -1) /* partial match */ |
| 189 | match = i; |
| 190 | else { |
| 191 | /* ambiguous abbreviation */ |
| 192 | if (PRINT_ERROR) |
| 193 | warnx(ambig, (int)current_argv_len, |
| 194 | current_argv); |
| 195 | optopt = 0; |
| 196 | return (BADCH); |
| 197 | } |
| 198 | } |
| 199 | if (match != -1) { /* option found */ |
| 200 | if (long_options[match].has_arg == no_argument |
| 201 | && has_equal) { |
| 202 | if (PRINT_ERROR) |
| 203 | warnx(noarg, (int)current_argv_len, |
| 204 | current_argv); |
| 205 | /* |
| 206 | * XXX: GNU sets optopt to val regardless of flag |
| 207 | */ |
no test coverage detected