aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorokan2014-01-20 18:58:03 +0000
committerokan2014-01-20 18:58:03 +0000
commit749175e23624a43a35824edada4545a6115dd4d1 (patch)
tree36997c1c39ae9c0e0881ff9083394c29aa3a3e12
parent7c3d7c7e009aba5952f235258aecebf4a42fcd3a (diff)
downloadcwm-749175e23624a43a35824edada4545a6115dd4d1.tar.gz
Add a function that adds an entry to a menuq, normalizing a common code
path; from Tiago Cunha.
-rw-r--r--calmwm.h3
-rw-r--r--group.c13
-rw-r--r--kbfunc.c32
-rw-r--r--menu.c19
-rw-r--r--mousefunc.c15
-rw-r--r--search.c13
6 files changed, 40 insertions, 55 deletions
diff --git a/calmwm.h b/calmwm.h
index 29fff99..69d27ba 100644
--- a/calmwm.h
+++ b/calmwm.h
@@ -15,7 +15,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
- * $OpenBSD: calmwm.h,v 1.240 2014/01/03 15:29:06 okan Exp $
+ * $OpenBSD: calmwm.h,v 1.241 2014/01/20 18:58:03 okan Exp $
*/
#ifndef _CALMWM_H_
@@ -508,6 +508,7 @@ struct menu *menu_filter(struct screen_ctx *, struct menu_q *,
char *, char *, int,
void (*)(struct menu_q *, struct menu_q *, char *),
void (*)(struct menu *, int));
+void menuq_add(struct menu_q *, void *, const char *, ...);
void menuq_clear(struct menu_q *);
int parse_config(const char *, struct conf *);
diff --git a/group.c b/group.c
index f9c6a1d..7f8addd 100644
--- a/group.c
+++ b/group.c
@@ -16,7 +16,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
- * $OpenBSD: group.c,v 1.82 2013/12/11 14:09:21 okan Exp $
+ * $OpenBSD: group.c,v 1.83 2014/01/20 18:58:03 okan Exp $
*/
#include <sys/param.h>
@@ -324,15 +324,8 @@ group_menu(struct screen_ctx *sc)
if (TAILQ_EMPTY(&gc->clients))
continue;
- mi = xcalloc(1, sizeof(*mi));
- if (gc->hidden)
- (void)snprintf(mi->text, sizeof(mi->text), "%d: [%s]",
- gc->shortcut, sc->group_names[i]);
- else
- (void)snprintf(mi->text, sizeof(mi->text), "%d: %s",
- gc->shortcut, sc->group_names[i]);
- mi->ctx = gc;
- TAILQ_INSERT_TAIL(&menuq, mi, entry);
+ menuq_add(&menuq, gc, gc->hidden ? "%d: [%s]" : "%d: %s",
+ gc->shortcut, sc->group_names[i]);
}
if (TAILQ_EMPTY(&menuq))
diff --git a/kbfunc.c b/kbfunc.c
index 2c11b1c..191bf22 100644
--- a/kbfunc.c
+++ b/kbfunc.c
@@ -15,7 +15,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
- * $OpenBSD: kbfunc.c,v 1.86 2014/01/03 15:29:06 okan Exp $
+ * $OpenBSD: kbfunc.c,v 1.87 2014/01/20 18:58:03 okan Exp $
*/
#include <sys/param.h>
@@ -151,13 +151,8 @@ kbfunc_client_search(struct client_ctx *cc, union arg *arg)
old_cc = client_current();
TAILQ_INIT(&menuq);
-
- TAILQ_FOREACH(cc, &Clientq, entry) {
- mi = xcalloc(1, sizeof(*mi));
- (void)strlcpy(mi->text, cc->name, sizeof(mi->text));
- mi->ctx = cc;
- TAILQ_INSERT_TAIL(&menuq, mi, entry);
- }
+ TAILQ_FOREACH(cc, &Clientq, entry)
+ menuq_add(&menuq, cc, "%s", cc->name);
if ((mi = menu_filter(sc, &menuq, "window", NULL, 0,
search_match_client, search_print_client)) != NULL) {
@@ -182,13 +177,8 @@ kbfunc_menu_search(struct client_ctx *cc, union arg *arg)
struct menu_q menuq;
TAILQ_INIT(&menuq);
-
- TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
- mi = xcalloc(1, sizeof(*mi));
- (void)strlcpy(mi->text, cmd->label, sizeof(mi->text));
- mi->ctx = cmd;
- TAILQ_INSERT_TAIL(&menuq, mi, entry);
- }
+ TAILQ_FOREACH(cmd, &Conf.cmdq, entry)
+ menuq_add(&menuq, cmd, "%s", cmd->label);
if ((mi = menu_filter(sc, &menuq, "application", NULL, 0,
search_match_text, NULL)) != NULL)
@@ -284,12 +274,8 @@ kbfunc_exec(struct client_ctx *cc, union arg *arg)
/* check for truncation etc */
if (l == -1 || l >= (int)sizeof(tpath))
continue;
- if (access(tpath, X_OK) == 0) {
- mi = xcalloc(1, sizeof(*mi));
- (void)strlcpy(mi->text,
- dp->d_name, sizeof(mi->text));
- TAILQ_INSERT_TAIL(&menuq, mi, entry);
- }
+ if (access(tpath, X_OK) == 0)
+ menuq_add(&menuq, NULL, "%s", dp->d_name);
}
(void)closedir(dirp);
}
@@ -360,9 +346,7 @@ kbfunc_ssh(struct client_ctx *cc, union arg *arg)
if (p - buf + 1 > sizeof(hostbuf))
continue;
(void)strlcpy(hostbuf, buf, p - buf + 1);
- mi = xcalloc(1, sizeof(*mi));
- (void)strlcpy(mi->text, hostbuf, sizeof(mi->text));
- TAILQ_INSERT_TAIL(&menuq, mi, entry);
+ menuq_add(&menuq, NULL, hostbuf);
}
free(lbuf);
(void)fclose(fp);
diff --git a/menu.c b/menu.c
index c19bc4b..790282d 100644
--- a/menu.c
+++ b/menu.c
@@ -16,7 +16,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
- * $OpenBSD: menu.c,v 1.72 2014/01/03 15:29:06 okan Exp $
+ * $OpenBSD: menu.c,v 1.73 2014/01/20 18:58:03 okan Exp $
*/
#include <sys/param.h>
@@ -25,6 +25,7 @@
#include <ctype.h>
#include <err.h>
#include <errno.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -605,6 +606,22 @@ menu_keycode(XKeyEvent *ev, enum ctltype *ctl, char *chr)
}
void
+menuq_add(struct menu_q *mq, void *ctx, const char *fmt, ...)
+{
+ va_list ap;
+ struct menu *mi;
+
+ mi = xcalloc(1, sizeof(*mi));
+ mi->ctx = ctx;
+
+ va_start(ap, fmt);
+ (void)vsnprintf(mi->text, sizeof(mi->text), fmt, ap);
+ va_end(ap);
+
+ TAILQ_INSERT_TAIL(mq, mi, entry);
+}
+
+void
menuq_clear(struct menu_q *mq)
{
struct menu *mi;
diff --git a/mousefunc.c b/mousefunc.c
index b7418ce..3c33aed 100644
--- a/mousefunc.c
+++ b/mousefunc.c
@@ -16,7 +16,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
- * $OpenBSD: mousefunc.c,v 1.65 2014/01/02 21:30:20 okan Exp $
+ * $OpenBSD: mousefunc.c,v 1.66 2014/01/20 18:58:03 okan Exp $
*/
#include <sys/param.h>
@@ -234,11 +234,8 @@ mousefunc_menu_unhide(struct client_ctx *cc, union arg *arg)
if (wname == NULL)
continue;
- mi = xcalloc(1, sizeof(*mi));
- (void)snprintf(mi->text, sizeof(mi->text), "(%d) %s",
+ menuq_add(&menuq, cc, "(%d) %s",
cc->group ? cc->group->shortcut : 0, wname);
- mi->ctx = cc;
- TAILQ_INSERT_TAIL(&menuq, mi, entry);
}
if (TAILQ_EMPTY(&menuq))
@@ -267,12 +264,8 @@ mousefunc_menu_cmd(struct client_ctx *cc, union arg *arg)
TAILQ_INIT(&menuq);
- TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
- mi = xcalloc(1, sizeof(*mi));
- (void)strlcpy(mi->text, cmd->label, sizeof(mi->text));
- mi->ctx = cmd;
- TAILQ_INSERT_TAIL(&menuq, mi, entry);
- }
+ TAILQ_FOREACH(cmd, &Conf.cmdq, entry)
+ menuq_add(&menuq, cmd, "%s", cmd->label);
if (TAILQ_EMPTY(&menuq))
return;
diff --git a/search.c b/search.c
index b9c4130..9a49b13 100644
--- a/search.c
+++ b/search.c
@@ -15,7 +15,7 @@
* 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.35 2014/01/03 15:29:06 okan Exp $
+ * $OpenBSD: search.c,v 1.36 2014/01/20 18:58:03 okan Exp $
*/
#include <sys/param.h>
@@ -172,10 +172,9 @@ search_print_client(struct menu *mi, int list)
static void
search_match_path(struct menu_q *menuq, struct menu_q *resultq, char *search, int flag)
{
- struct menu *mi;
- char pattern[MAXPATHLEN];
- glob_t g;
- int i;
+ char pattern[MAXPATHLEN];
+ glob_t g;
+ int i;
TAILQ_INIT(resultq);
@@ -187,9 +186,7 @@ search_match_path(struct menu_q *menuq, struct menu_q *resultq, char *search, in
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);
+ menuq_add(resultq, NULL, "%s", g.gl_pathv[i]);
}
globfree(&g);
}