* 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. */
| 116 | * Returns -1 if short_too is set and the option does not match long_options. |
| 117 | */ |
| 118 | static int |
| 119 | parse_long_options(char **nargv, const char *options, |
| 120 | const struct option *long_options, int *idx, int short_too) |
| 121 | { |
| 122 | const char *current_argv; |
| 123 | char *has_equal; |
| 124 | size_t current_argv_len; |
| 125 | int i, match; |
| 126 | |
| 127 | current_argv = place; |
| 128 | match = -1; |
| 129 | |
| 130 | optind++; |
| 131 | |
| 132 | has_equal = strchr(current_argv, '='); |
| 133 | if (has_equal != NULL) { |
| 134 | /* argument found (--option=arg) */ |
| 135 | current_argv_len = has_equal - current_argv; |
| 136 | has_equal++; |
| 137 | } else |
| 138 | current_argv_len = strlen(current_argv); |
| 139 | |
| 140 | for (i = 0; long_options[i].name; i++) { |
| 141 | /* find matching long option */ |
| 142 | if (strncmp(current_argv, long_options[i].name, |
| 143 | current_argv_len)) |
| 144 | continue; |
| 145 | |
| 146 | if (strlen(long_options[i].name) == current_argv_len) { |
| 147 | /* exact match */ |
| 148 | match = i; |
| 149 | break; |
| 150 | } |
| 151 | /* |
| 152 | * If this is a known short option, don't allow |
| 153 | * a partial match of a single character. |
| 154 | */ |
| 155 | if (short_too && current_argv_len == 1) |
| 156 | continue; |
| 157 | |
| 158 | if (match == -1) /* partial match */ |
| 159 | match = i; |
| 160 | else { |
| 161 | /* ambiguous abbreviation */ |
| 162 | if (PRINT_ERROR) |
| 163 | warnx(ambig, (int)current_argv_len, |
| 164 | current_argv); |
| 165 | optopt = 0; |
| 166 | return BADCH; |
| 167 | } |
| 168 | } |
| 169 | if (match != -1) { /* option found */ |
| 170 | if (long_options[match].has_arg == no_argument |
| 171 | && has_equal) { |
| 172 | if (PRINT_ERROR) |
| 173 | warnx(noarg, (int)current_argv_len, |
| 174 | current_argv); |
| 175 | /* |
no test coverage detected