| 279 | */ |
| 280 | char ** completion_matches(const char *, char *(*)(const char *, int)); |
| 281 | char ** |
| 282 | completion_matches(const char *text, char *(*genfunc)(const char *, int)) |
| 283 | { |
| 284 | char **match_list = NULL, *retstr, *prevstr; |
| 285 | size_t match_list_len, max_equal, which, i; |
| 286 | size_t matches; |
| 287 | |
| 288 | matches = 0; |
| 289 | match_list_len = 1; |
| 290 | while ((retstr = (*genfunc) (text, (int)matches)) != NULL) { |
| 291 | /* allow for list terminator here */ |
| 292 | if (matches + 3 >= match_list_len) { |
| 293 | char **nmatch_list; |
| 294 | while (matches + 3 >= match_list_len) |
| 295 | match_list_len <<= 1; |
| 296 | nmatch_list = el_realloc(match_list, |
| 297 | match_list_len * sizeof(*nmatch_list)); |
| 298 | if (nmatch_list == NULL) { |
| 299 | el_free(match_list); |
| 300 | return NULL; |
| 301 | } |
| 302 | match_list = nmatch_list; |
| 303 | |
| 304 | } |
| 305 | match_list[++matches] = retstr; |
| 306 | } |
| 307 | |
| 308 | if (!match_list) |
| 309 | return NULL; /* nothing found */ |
| 310 | |
| 311 | /* find least denominator and insert it to match_list[0] */ |
| 312 | which = 2; |
| 313 | prevstr = match_list[1]; |
| 314 | max_equal = strlen(prevstr); |
| 315 | for (; which <= matches; which++) { |
| 316 | for (i = 0; i < max_equal && |
| 317 | prevstr[i] == match_list[which][i]; i++) |
| 318 | continue; |
| 319 | max_equal = i; |
| 320 | } |
| 321 | |
| 322 | retstr = el_malloc((max_equal + 1) * sizeof(*retstr)); |
| 323 | if (retstr == NULL) { |
| 324 | el_free(match_list); |
| 325 | return NULL; |
| 326 | } |
| 327 | (void)strncpy(retstr, match_list[1], max_equal); |
| 328 | retstr[max_equal] = '\0'; |
| 329 | match_list[0] = retstr; |
| 330 | |
| 331 | /* add NULL as last pointer to the array */ |
| 332 | match_list[matches + 1] = NULL; |
| 333 | |
| 334 | return match_list; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Sort function for qsort(). Just wrapper around strcasecmp(). |