xenocara/app/cwm/search.c

266 lines
6.3 KiB
C
Raw Normal View History

/*
* calmwm - the calm window manager
*
* Copyright (c) 2004 Marius Aamodt Eriksen <marius@monkey.org>
2011-05-11 07:53:51 -06:00
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* $OpenBSD: search.c,v 1.65 2017/12/07 16:03:10 okan Exp $
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <err.h>
#include <errno.h>
#include <fnmatch.h>
2012-11-08 20:52:02 -07:00
#include <glob.h>
#include <limits.h>
2012-11-08 20:52:02 -07:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "calmwm.h"
#define PATH_ANY 0x0001
#define PATH_EXEC 0x0002
2017-12-07 08:40:54 -07:00
static void match_path_type(struct menu_q *, char *, int);
static int match_substr(char *, char *, int);
static int
match_substr(char *sub, char *str, int zeroidx)
{
size_t len, sublen;
unsigned int n, flen;
if (sub == NULL || str == NULL)
return(0);
len = strlen(str);
sublen = strlen(sub);
if (sublen > len)
return(0);
if (zeroidx)
flen = 0;
else
flen = len - sublen;
for (n = 0; n <= flen; n++)
if (strncasecmp(sub, str + n, sublen) == 0)
return(1);
return(0);
}
void
search_match_client(struct menu_q *menuq, struct menu_q *resultq, char *search)
{
struct winname *wn;
struct menu *mi, *tierp[4], *before = NULL;
(void)memset(tierp, 0, sizeof(tierp));
2017-12-07 08:40:54 -07:00
TAILQ_INIT(resultq);
TAILQ_FOREACH(mi, menuq, entry) {
int tier = -1, t;
2015-06-30 12:42:50 -06:00
struct client_ctx *cc = (struct client_ctx *)mi->ctx;
/* Match on label. */
2017-12-07 08:40:54 -07:00
if (match_substr(search, cc->label, 0))
tier = 0;
/* Match on window name history, from present to past. */
if (tier < 0) {
TAILQ_FOREACH_REVERSE(wn, &cc->nameq, name_q, entry)
2017-12-07 08:40:54 -07:00
if (match_substr(search, wn->name, 0)) {
2008-04-15 14:24:41 -06:00
tier = 2;
break;
}
}
/* Match on window resource class. */
2017-12-07 08:40:54 -07:00
if ((tier < 0) && match_substr(search, cc->ch.res_class, 0))
tier = 3;
if (tier < 0)
continue;
/* Current window is ranked down. */
if ((tier < nitems(tierp) - 1) && (cc->flags & CLIENT_ACTIVE))
tier++;
/* Hidden window is ranked up. */
if ((tier > 0) && (cc->flags & CLIENT_HIDDEN))
tier--;
2015-06-28 13:50:46 -06:00
if (tier >= nitems(tierp))
errx(1, "%s: invalid tier", __func__);
/*
* If you have a tierp, insert after it, and make it
* the new tierp. If you don't have a tierp, find the
* first nonzero tierp above you, insert after it.
* Always make your current tierp the newly inserted
* entry.
*/
for (t = tier; t >= 0 && ((before = tierp[t]) == NULL); t--)
;
if (before == NULL)
TAILQ_INSERT_HEAD(resultq, mi, resultentry);
else
TAILQ_INSERT_AFTER(resultq, before, mi, resultentry);
tierp[tier] = mi;
}
}
void
search_match_cmd(struct menu_q *menuq, struct menu_q *resultq, char *search)
{
struct menu *mi;
TAILQ_INIT(resultq);
TAILQ_FOREACH(mi, menuq, entry) {
struct cmd_ctx *cmd = (struct cmd_ctx *)mi->ctx;
if (match_substr(search, cmd->name, 0))
TAILQ_INSERT_TAIL(resultq, mi, resultentry);
}
}
void
search_match_group(struct menu_q *menuq, struct menu_q *resultq, char *search)
{
struct menu *mi;
char *s;
TAILQ_INIT(resultq);
TAILQ_FOREACH(mi, menuq, entry) {
struct group_ctx *gc = (struct group_ctx *)mi->ctx;
xasprintf(&s, "%d %s", gc->num, gc->name);
if (match_substr(search, s, 0))
TAILQ_INSERT_TAIL(resultq, mi, resultentry);
free(s);
}
}
static void
2017-12-07 08:40:54 -07:00
match_path_type(struct menu_q *resultq, char *search, int flag)
{
struct menu *mi;
char pattern[PATH_MAX];
glob_t g;
int i;
(void)strlcpy(pattern, search, sizeof(pattern));
(void)strlcat(pattern, "*", sizeof(pattern));
if (glob(pattern, GLOB_MARK, NULL, &g) != 0)
return;
for (i = 0; i < g.gl_pathc; i++) {
if ((flag & PATH_EXEC) && access(g.gl_pathv[i], X_OK))
continue;
mi = xcalloc(1, sizeof(*mi));
(void)strlcpy(mi->text, g.gl_pathv[i], sizeof(mi->text));
TAILQ_INSERT_TAIL(resultq, mi, resultentry);
}
globfree(&g);
}
void
search_match_exec(struct menu_q *menuq, struct menu_q *resultq, char *search)
{
struct menu *mi, *mj;
int r;
TAILQ_INIT(resultq);
TAILQ_FOREACH(mi, menuq, entry) {
2017-12-07 08:40:54 -07:00
if (match_substr(search, mi->text, 1) == 0 &&
fnmatch(search, mi->text, 0) == FNM_NOMATCH)
continue;
TAILQ_FOREACH(mj, resultq, resultentry) {
r = strcmp(mi->text, mj->text);
if (r < 0)
TAILQ_INSERT_BEFORE(mj, mi, resultentry);
if (r <= 0)
break;
}
if (mj == NULL)
TAILQ_INSERT_TAIL(resultq, mi, resultentry);
}
if (TAILQ_EMPTY(resultq))
2017-12-07 08:40:54 -07:00
match_path_type(resultq, search, PATH_EXEC);
}
2017-12-07 08:40:54 -07:00
void
search_match_path(struct menu_q *menuq, struct menu_q *resultq, char *search)
{
2017-12-07 08:40:54 -07:00
TAILQ_INIT(resultq);
match_path_type(resultq, search, PATH_ANY);
}
2017-12-07 08:40:54 -07:00
void
search_match_text(struct menu_q *menuq, struct menu_q *resultq, char *search)
{
struct menu *mi;
2017-12-07 08:40:54 -07:00
TAILQ_INIT(resultq);
TAILQ_FOREACH(mi, menuq, entry) {
if (match_substr(search, mi->text, 0))
TAILQ_INSERT_TAIL(resultq, mi, resultentry);
}
}
2017-12-07 08:40:54 -07:00
void
search_print_client(struct menu *mi, int listing)
{
struct client_ctx *cc = (struct client_ctx *)mi->ctx;
char flag = ' ';
2017-12-07 08:40:54 -07:00
if (cc->flags & CLIENT_ACTIVE)
flag = '!';
else if (cc->flags & CLIENT_HIDDEN)
flag = '&';
2017-12-07 08:40:54 -07:00
(void)snprintf(mi->print, sizeof(mi->print), "(%d) %c[%s] %s",
(cc->gc) ? cc->gc->num : 0, flag,
(cc->label) ? cc->label : "", cc->name);
}
2017-12-07 08:40:54 -07:00
void
search_print_cmd(struct menu *mi, int listing)
{
struct cmd_ctx *cmd = (struct cmd_ctx *)mi->ctx;
(void)snprintf(mi->print, sizeof(mi->print), "%s", cmd->name);
}
void
search_print_group(struct menu *mi, int listing)
{
struct group_ctx *gc = (struct group_ctx *)mi->ctx;
(void)snprintf(mi->print, sizeof(mi->print),
(group_holds_only_hidden(gc)) ? "%d: [%s]" : "%d: %s",
gc->num, gc->name);
}
void
search_print_text(struct menu *mi, int listing)
{
(void)snprintf(mi->print, sizeof(mi->print), "%s", mi->text);
}