jcs ratpoison hax
1/* Copyright (C) 2000, 2001, 2002, 2003, 2004 Shawn Betts <sabetts@vcn.bc.ca>
2 *
3 * This file is part of ratpoison.
4 *
5 * ratpoison is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * ratpoison is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
19 */
20
21/* A hook is simply a list of strings that get passed to command() in
22 sequence. */
23
24#include "ratpoison.h"
25
26#include <string.h>
27
28void
29hook_add (struct list_head *hook, struct sbuf *s)
30{
31 struct sbuf *cur;
32
33 /* Check if it's in the list already. */
34 list_for_each_entry (cur, hook, node)
35 {
36 if (!strcmp (sbuf_get (cur), sbuf_get (s)))
37 {
38 sbuf_free (s);
39 return;
40 }
41 }
42
43 /* It's not in the list, so add it. */
44 list_add_tail (&s->node, hook);
45}
46
47void
48hook_remove (struct list_head *hook, struct sbuf *s)
49{
50 struct list_head *tmp, *iter;
51 struct sbuf *cur;
52
53 /* If it's in the list, delete it. */
54 list_for_each_safe_entry (cur, iter, tmp, hook, node)
55 {
56 if (!strcmp (sbuf_get (cur), sbuf_get (s)))
57 {
58 list_del (&cur->node);
59 sbuf_free (cur);
60 }
61 }
62}
63
64void
65hook_run (struct list_head *hook)
66{
67 struct sbuf *cur;
68 cmdret *result;
69
70 list_for_each_entry (cur, hook, node)
71 {
72 result = command (1, sbuf_get (cur));
73 if (result)
74 {
75 if (result->output)
76 message (result->output);
77 cmdret_free (result);
78 }
79 }
80}
81
82struct list_head *
83hook_lookup (char *s)
84{
85 struct rp_hook_db_entry *entry;
86
87 for (entry = rp_hook_db; entry->name; entry++)
88 {
89 if (!strcmp (s, entry->name))
90 {
91 return entry->hook;
92 }
93 }
94
95 return NULL;
96}