2014-04-09 22:09:35 +00:00
|
|
|
#include "internal.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/**
|
2014-10-22 19:46:51 +00:00
|
|
|
* Shrink struct bm_item** list pointer.
|
2014-04-10 19:02:47 +00:00
|
|
|
*
|
|
|
|
* Useful helper function for filter functions.
|
|
|
|
*
|
2014-10-22 19:46:51 +00:00
|
|
|
* @param in_out_list Pointer to pointer to list of bm_item pointers.
|
2014-04-10 19:02:47 +00:00
|
|
|
* @param osize Current size of the list.
|
|
|
|
* @param nsize New size the list will be shrinked to.
|
2014-10-22 19:46:51 +00:00
|
|
|
* @return Pointer to list of bm_item pointers.
|
2014-04-10 19:02:47 +00:00
|
|
|
*/
|
2014-10-22 19:46:51 +00:00
|
|
|
static struct bm_item**
|
|
|
|
shrink_list(struct bm_item ***in_out_list, size_t osize, size_t nsize)
|
2014-04-10 19:02:47 +00:00
|
|
|
{
|
2014-10-22 19:46:51 +00:00
|
|
|
assert(in_out_list);
|
2014-04-10 19:02:47 +00:00
|
|
|
|
|
|
|
if (nsize == 0) {
|
2014-10-22 19:46:51 +00:00
|
|
|
free(*in_out_list);
|
|
|
|
return (*in_out_list = NULL);
|
2014-04-10 19:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nsize >= osize)
|
2014-10-22 19:46:51 +00:00
|
|
|
return *in_out_list;
|
2014-04-10 19:02:47 +00:00
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
void *tmp = malloc(sizeof(struct bm_item*) * nsize);
|
2014-04-10 19:02:47 +00:00
|
|
|
if (!tmp)
|
2014-10-22 19:46:51 +00:00
|
|
|
return *in_out_list;
|
2014-04-10 19:02:47 +00:00
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
memcpy(tmp, *in_out_list, sizeof(struct bm_item*) * nsize);
|
|
|
|
free(*in_out_list);
|
|
|
|
return (*in_out_list = tmp);
|
2014-04-10 19:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Text filter tokenizer helper.
|
|
|
|
*
|
2014-10-22 19:46:51 +00:00
|
|
|
* @param menu bm_menu instance which filter to tokenize.
|
|
|
|
* @param out_tokv char pointer reference to list of tokens, this should be freed after use.
|
|
|
|
* @param out_tokc uint32_t reference to number of tokens.
|
2014-04-10 19:02:47 +00:00
|
|
|
* @return Pointer to buffer that contains tokenized string, this should be freed after use.
|
|
|
|
*/
|
2014-10-22 19:46:51 +00:00
|
|
|
static char*
|
|
|
|
tokenize(struct bm_menu *menu, char ***out_tokv, uint32_t *out_tokc)
|
2014-04-10 19:02:47 +00:00
|
|
|
{
|
2014-10-22 19:46:51 +00:00
|
|
|
assert(menu && out_tokv && out_tokc);
|
|
|
|
*out_tokv = NULL;
|
|
|
|
*out_tokc = 0;
|
2014-04-10 19:02:47 +00:00
|
|
|
|
|
|
|
char **tokv = NULL, *buffer = NULL;
|
2014-10-22 19:46:51 +00:00
|
|
|
if (!(buffer = bm_strdup(menu->filter)))
|
2014-04-10 19:02:47 +00:00
|
|
|
goto fail;
|
|
|
|
|
2014-08-20 22:45:52 +00:00
|
|
|
char *s;
|
|
|
|
for (s = buffer; *s && *s == ' '; ++s);
|
|
|
|
|
|
|
|
char **tmp = NULL;
|
2014-04-12 17:16:33 +00:00
|
|
|
size_t pos = 0, next;
|
2014-10-22 19:46:51 +00:00
|
|
|
uint32_t tokc = 0, tokn = 0;
|
|
|
|
for (; (pos = bm_strip_token(s, " ", &next)) > 0; tokv = tmp) {
|
2014-04-12 17:52:01 +00:00
|
|
|
if (++tokc > tokn && !(tmp = realloc(tokv, ++tokn * sizeof(char*))))
|
2014-04-10 19:02:47 +00:00
|
|
|
goto fail;
|
|
|
|
|
2014-04-12 16:59:21 +00:00
|
|
|
tmp[tokc - 1] = s;
|
2014-04-12 17:16:33 +00:00
|
|
|
s += next;
|
2014-08-20 23:10:56 +00:00
|
|
|
for (; *s && *s == ' '; ++s);
|
2014-04-12 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
*out_tokv = tmp;
|
|
|
|
*out_tokc = tokc;
|
2014-04-10 19:02:47 +00:00
|
|
|
return buffer;
|
|
|
|
|
|
|
|
fail:
|
2014-10-22 19:46:51 +00:00
|
|
|
free(buffer);
|
|
|
|
free(tokv);
|
2014-04-10 19:02:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dmenu filterer that accepts substring function.
|
2014-04-09 22:09:35 +00:00
|
|
|
*
|
2014-10-22 19:46:51 +00:00
|
|
|
* @param menu bm_menu instance to filter.
|
2014-04-10 20:05:13 +00:00
|
|
|
* @param addition This will be 1, if filter is same as previous filter with something appended.
|
2014-04-10 19:02:47 +00:00
|
|
|
* @param fstrstr Substring function used to match items.
|
2014-10-22 19:46:51 +00:00
|
|
|
* @param out_nmemb uint32_t reference to filtered items count.
|
|
|
|
* @return Pointer to array of bm_item pointers.
|
2014-04-09 22:09:35 +00:00
|
|
|
*/
|
2020-12-12 22:47:31 +00:00
|
|
|
static struct bm_item**
|
2014-10-22 19:46:51 +00:00
|
|
|
filter_dmenu_fun(struct bm_menu *menu, char addition, char* (*fstrstr)(const char *a, const char *b), int (*fstrncmp)(const char *a, const char *b, size_t len), uint32_t *out_nmemb)
|
2014-04-09 22:09:35 +00:00
|
|
|
{
|
2014-10-22 19:46:51 +00:00
|
|
|
assert(menu && fstrstr && fstrncmp && out_nmemb);
|
|
|
|
*out_nmemb = 0;
|
2014-04-09 22:09:35 +00:00
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
uint32_t count;
|
|
|
|
struct bm_item **items;
|
2014-04-10 20:05:13 +00:00
|
|
|
|
|
|
|
if (addition) {
|
2014-10-22 19:46:51 +00:00
|
|
|
items = bm_menu_get_filtered_items(menu, &count);
|
2014-04-10 20:05:13 +00:00
|
|
|
} else {
|
2014-10-22 19:46:51 +00:00
|
|
|
items = bm_menu_get_items(menu, &count);
|
2014-04-10 20:05:13 +00:00
|
|
|
}
|
2014-04-09 22:09:35 +00:00
|
|
|
|
2014-04-11 20:58:36 +00:00
|
|
|
char *buffer = NULL;
|
2014-10-22 19:46:51 +00:00
|
|
|
struct bm_item **filtered;
|
|
|
|
if (!(filtered = calloc(count, sizeof(struct bm_item*))))
|
2014-04-11 20:58:36 +00:00
|
|
|
goto fail;
|
2014-04-09 22:09:35 +00:00
|
|
|
|
2014-04-10 19:02:47 +00:00
|
|
|
char **tokv;
|
2014-10-22 19:46:51 +00:00
|
|
|
uint32_t tokc;
|
|
|
|
if (!(buffer = tokenize(menu, &tokv, &tokc)))
|
2014-04-11 20:58:36 +00:00
|
|
|
goto fail;
|
2014-04-10 19:02:47 +00:00
|
|
|
|
2023-08-29 15:04:59 +00:00
|
|
|
const char *filter = menu->filter ? menu->filter : "";
|
2014-04-12 09:56:34 +00:00
|
|
|
size_t len = (tokc ? strlen(tokv[0]) : 0);
|
2014-10-22 19:46:51 +00:00
|
|
|
uint32_t i, f, e;
|
|
|
|
for (e = f = i = 0; i < count; ++i) {
|
|
|
|
struct bm_item *item = items[i];
|
2014-04-10 19:02:47 +00:00
|
|
|
if (!item->text && tokc != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (tokc && item->text) {
|
2014-10-22 19:46:51 +00:00
|
|
|
uint32_t t;
|
2014-04-10 19:02:47 +00:00
|
|
|
for (t = 0; t < tokc && fstrstr(item->text, tokv[t]); ++t);
|
|
|
|
if (t < tokc)
|
|
|
|
continue;
|
2014-04-09 22:09:35 +00:00
|
|
|
}
|
2014-04-10 19:02:47 +00:00
|
|
|
|
2023-08-29 15:04:59 +00:00
|
|
|
if (tokc && item->text && strlen(filter) == strlen(item->text) && !fstrncmp(filter, item->text, strlen(filter))) { /* exact matches */
|
2014-10-22 19:46:51 +00:00
|
|
|
memmove(&filtered[1], filtered, f * sizeof(struct bm_item*));
|
2014-04-12 09:56:34 +00:00
|
|
|
filtered[0] = item;
|
|
|
|
e++; /* where do exact matches end */
|
|
|
|
} else if (tokc && item->text && !fstrncmp(tokv[0], item->text, len)) { /* prefixes */
|
2014-10-22 19:46:51 +00:00
|
|
|
memmove(&filtered[e + 1], &filtered[e], (f - e) * sizeof(struct bm_item*));
|
2014-04-12 09:56:34 +00:00
|
|
|
filtered[e] = item;
|
2023-08-29 15:04:59 +00:00
|
|
|
e++; /* where do prefix matches end */
|
2014-04-12 09:56:34 +00:00
|
|
|
} else {
|
|
|
|
filtered[f] = item;
|
|
|
|
}
|
|
|
|
f++; /* where do all matches end */
|
2014-04-09 22:09:35 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
free(buffer);
|
|
|
|
free(tokv);
|
|
|
|
return shrink_list(&filtered, menu->items.count, (*out_nmemb = f));
|
2014-04-11 20:58:36 +00:00
|
|
|
|
|
|
|
fail:
|
2014-10-22 19:46:51 +00:00
|
|
|
free(filtered);
|
|
|
|
free(buffer);
|
2014-04-11 20:58:36 +00:00
|
|
|
return NULL;
|
2014-04-10 19:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter that mimics the vanilla dmenu filtering.
|
|
|
|
*
|
2014-10-22 19:46:51 +00:00
|
|
|
* @param menu bm_menu instance to filter.
|
2014-04-10 20:05:13 +00:00
|
|
|
* @param addition This will be 1, if filter is same as previous filter with something appended.
|
2014-10-22 19:46:51 +00:00
|
|
|
* @param outNmemb uint32_t reference to filtered items count.
|
|
|
|
* @return Pointer to array of bm_item pointers.
|
2014-04-10 19:02:47 +00:00
|
|
|
*/
|
2014-10-22 19:46:51 +00:00
|
|
|
struct bm_item**
|
|
|
|
bm_filter_dmenu(struct bm_menu *menu, bool addition, uint32_t *out_nmemb)
|
2014-04-10 19:02:47 +00:00
|
|
|
{
|
2014-10-22 19:46:51 +00:00
|
|
|
return filter_dmenu_fun(menu, addition, strstr, strncmp, out_nmemb);
|
2014-04-09 22:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter that mimics the vanilla case-insensitive dmenu filtering.
|
|
|
|
*
|
2014-10-22 19:46:51 +00:00
|
|
|
* @param menu bm_menu instance to filter.
|
2014-04-10 20:05:13 +00:00
|
|
|
* @param addition This will be 1, if filter is same as previous filter with something appended.
|
2014-10-22 19:46:51 +00:00
|
|
|
* @param outNmemb uint32_t reference to filtered items count.
|
|
|
|
* @return Pointer to array of bm_item pointers.
|
2014-04-09 22:09:35 +00:00
|
|
|
*/
|
2014-10-22 19:46:51 +00:00
|
|
|
struct bm_item**
|
|
|
|
bm_filter_dmenu_case_insensitive(struct bm_menu *menu, bool addition, uint32_t *out_nmemb)
|
2014-04-09 22:09:35 +00:00
|
|
|
{
|
2014-10-22 19:46:51 +00:00
|
|
|
return filter_dmenu_fun(menu, addition, bm_strupstr, bm_strnupcmp, out_nmemb);
|
2014-04-09 22:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=8 sw=4 tw=0 :*/
|