Change Guile license to LGPLv3+
[bpt/guile.git] / libguile / hooks.c
1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2006, 2008 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include "libguile/_scm.h"
27
28 #include "libguile/eval.h"
29 #include "libguile/ports.h"
30 #include "libguile/procprop.h"
31 #include "libguile/root.h"
32 #include "libguile/smob.h"
33 #include "libguile/strings.h"
34
35 #include "libguile/validate.h"
36 #include "libguile/hooks.h"
37
38 \f
39 /* C level hooks
40 *
41 * Currently, this implementation is separate from the Scheme level
42 * hooks. The possibility exists to implement the Scheme level hooks
43 * using C level hooks.
44 */
45
46 void
47 scm_c_hook_init (scm_t_c_hook *hook, void *hook_data, scm_t_c_hook_type type)
48 {
49 hook->first = 0;
50 hook->type = type;
51 hook->data = hook_data;
52 }
53
54 void
55 scm_c_hook_add (scm_t_c_hook *hook,
56 scm_t_c_hook_function func,
57 void *fn_data,
58 int appendp)
59 {
60 scm_t_c_hook_entry *entry = scm_malloc (sizeof (scm_t_c_hook_entry));
61 scm_t_c_hook_entry **loc = &hook->first;
62 if (appendp)
63 while (*loc)
64 loc = &(*loc)->next;
65 entry->next = *loc;
66 entry->func = func;
67 entry->data = fn_data;
68 *loc = entry;
69 }
70
71 void
72 scm_c_hook_remove (scm_t_c_hook *hook,
73 scm_t_c_hook_function func,
74 void *fn_data)
75 {
76 scm_t_c_hook_entry **loc = &hook->first;
77 while (*loc)
78 {
79 if ((*loc)->func == func && (*loc)->data == fn_data)
80 {
81 scm_t_c_hook_entry *entry = *loc;
82 *loc = (*loc)->next;
83 free (entry);
84 return;
85 }
86 loc = &(*loc)->next;
87 }
88 fprintf (stderr, "Attempt to remove non-existent hook function\n");
89 abort ();
90 }
91
92 void *
93 scm_c_hook_run (scm_t_c_hook *hook, void *data)
94 {
95 scm_t_c_hook_entry *entry = hook->first;
96 scm_t_c_hook_type type = hook->type;
97 void *res = 0;
98 while (entry)
99 {
100 res = (entry->func) (hook->data, entry->data, data);
101 if (res)
102 {
103 if (type == SCM_C_HOOK_OR)
104 break;
105 }
106 else
107 {
108 if (type == SCM_C_HOOK_AND)
109 break;
110 }
111 entry = entry->next;
112 }
113 return res;
114 }
115
116 \f
117 /* Scheme level hooks
118 *
119 * A hook is basically a list of procedures to be called at well defined
120 * points in time.
121 *
122 * Hook arity is not a full member of this type and therefore lacks an
123 * accessor. It exists to aid debugging and is not intended to be used in
124 * programs.
125 */
126
127 scm_t_bits scm_tc16_hook;
128
129
130 static int
131 hook_print (SCM hook, SCM port, scm_print_state *pstate)
132 {
133 SCM ls, name;
134 scm_puts ("#<hook ", port);
135 scm_intprint (SCM_HOOK_ARITY (hook), 10, port);
136 scm_putc (' ', port);
137 scm_uintprint (SCM_UNPACK (hook), 16, port);
138 ls = SCM_HOOK_PROCEDURES (hook);
139 while (SCM_NIMP (ls))
140 {
141 scm_putc (' ', port);
142 name = scm_procedure_name (SCM_CAR (ls));
143 if (scm_is_true (name))
144 scm_iprin1 (name, port, pstate);
145 else
146 scm_putc ('?', port);
147 ls = SCM_CDR (ls);
148 }
149 scm_putc ('>', port);
150 return 1;
151 }
152
153
154 SCM_DEFINE (scm_make_hook, "make-hook", 0, 1, 0,
155 (SCM n_args),
156 "Create a hook for storing procedure of arity @var{n_args}.\n"
157 "@var{n_args} defaults to zero. The returned value is a hook\n"
158 "object to be used with the other hook procedures.")
159 #define FUNC_NAME s_scm_make_hook
160 {
161 unsigned int n;
162
163 if (SCM_UNBNDP (n_args))
164 n = 0;
165 else
166 n = scm_to_unsigned_integer (n_args, 0, 16);
167
168 SCM_RETURN_NEWSMOB (scm_tc16_hook + (n << 16), SCM_UNPACK (SCM_EOL));
169 }
170 #undef FUNC_NAME
171
172
173 SCM_DEFINE (scm_hook_p, "hook?", 1, 0, 0,
174 (SCM x),
175 "Return @code{#t} if @var{x} is a hook, @code{#f} otherwise.")
176 #define FUNC_NAME s_scm_hook_p
177 {
178 return scm_from_bool (SCM_HOOKP (x));
179 }
180 #undef FUNC_NAME
181
182
183 SCM_DEFINE (scm_hook_empty_p, "hook-empty?", 1, 0, 0,
184 (SCM hook),
185 "Return @code{#t} if @var{hook} is an empty hook, @code{#f}\n"
186 "otherwise.")
187 #define FUNC_NAME s_scm_hook_empty_p
188 {
189 SCM_VALIDATE_HOOK (1, hook);
190 return scm_from_bool (scm_is_null (SCM_HOOK_PROCEDURES (hook)));
191 }
192 #undef FUNC_NAME
193
194
195 SCM_DEFINE (scm_add_hook_x, "add-hook!", 2, 1, 0,
196 (SCM hook, SCM proc, SCM append_p),
197 "Add the procedure @var{proc} to the hook @var{hook}. The\n"
198 "procedure is added to the end if @var{append_p} is true,\n"
199 "otherwise it is added to the front. The return value of this\n"
200 "procedure is not specified.")
201 #define FUNC_NAME s_scm_add_hook_x
202 {
203 SCM arity, rest;
204 int n_args;
205 SCM_VALIDATE_HOOK (1, hook);
206 SCM_ASSERT (scm_is_true (arity = scm_i_procedure_arity (proc)),
207 proc, SCM_ARG2, FUNC_NAME);
208 n_args = SCM_HOOK_ARITY (hook);
209 if (scm_to_int (SCM_CAR (arity)) > n_args
210 || (scm_is_false (SCM_CADDR (arity))
211 && (scm_to_int (SCM_CAR (arity)) + scm_to_int (SCM_CADR (arity))
212 < n_args)))
213 scm_wrong_type_arg (FUNC_NAME, 2, proc);
214 rest = scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook));
215 SCM_SET_HOOK_PROCEDURES (hook,
216 (!SCM_UNBNDP (append_p) && scm_is_true (append_p)
217 ? scm_append_x (scm_list_2 (rest, scm_list_1 (proc)))
218 : scm_cons (proc, rest)));
219 return SCM_UNSPECIFIED;
220 }
221 #undef FUNC_NAME
222
223
224 SCM_DEFINE (scm_remove_hook_x, "remove-hook!", 2, 0, 0,
225 (SCM hook, SCM proc),
226 "Remove the procedure @var{proc} from the hook @var{hook}. The\n"
227 "return value of this procedure is not specified.")
228 #define FUNC_NAME s_scm_remove_hook_x
229 {
230 SCM_VALIDATE_HOOK (1, hook);
231 SCM_SET_HOOK_PROCEDURES (hook,
232 scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook)));
233 return SCM_UNSPECIFIED;
234 }
235 #undef FUNC_NAME
236
237
238 SCM_DEFINE (scm_reset_hook_x, "reset-hook!", 1, 0, 0,
239 (SCM hook),
240 "Remove all procedures from the hook @var{hook}. The return\n"
241 "value of this procedure is not specified.")
242 #define FUNC_NAME s_scm_reset_hook_x
243 {
244 SCM_VALIDATE_HOOK (1, hook);
245 SCM_SET_HOOK_PROCEDURES (hook, SCM_EOL);
246 return SCM_UNSPECIFIED;
247 }
248 #undef FUNC_NAME
249
250
251 SCM_DEFINE (scm_run_hook, "run-hook", 1, 0, 1,
252 (SCM hook, SCM args),
253 "Apply all procedures from the hook @var{hook} to the arguments\n"
254 "@var{args}. The order of the procedure application is first to\n"
255 "last. The return value of this procedure is not specified.")
256 #define FUNC_NAME s_scm_run_hook
257 {
258 SCM_VALIDATE_HOOK (1, hook);
259 if (scm_ilength (args) != SCM_HOOK_ARITY (hook))
260 SCM_MISC_ERROR ("Hook ~S requires ~A arguments",
261 scm_list_2 (hook, scm_from_int (SCM_HOOK_ARITY (hook))));
262 scm_c_run_hook (hook, args);
263 return SCM_UNSPECIFIED;
264 }
265 #undef FUNC_NAME
266
267
268 void
269 scm_c_run_hook (SCM hook, SCM args)
270 {
271 SCM procs = SCM_HOOK_PROCEDURES (hook);
272 while (SCM_NIMP (procs))
273 {
274 scm_apply_0 (SCM_CAR (procs), args);
275 procs = SCM_CDR (procs);
276 }
277 }
278
279
280 SCM_DEFINE (scm_hook_to_list, "hook->list", 1, 0, 0,
281 (SCM hook),
282 "Convert the procedure list of @var{hook} to a list.")
283 #define FUNC_NAME s_scm_hook_to_list
284 {
285 SCM_VALIDATE_HOOK (1, hook);
286 return scm_list_copy (SCM_HOOK_PROCEDURES (hook));
287 }
288 #undef FUNC_NAME
289
290
291 \f
292
293 void
294 scm_init_hooks ()
295 {
296 scm_tc16_hook = scm_make_smob_type ("hook", 0);
297 scm_set_smob_mark (scm_tc16_hook, scm_markcdr);
298 scm_set_smob_print (scm_tc16_hook, hook_print);
299 #include "libguile/hooks.x"
300 }
301
302 /*
303 Local Variables:
304 c-file-style: "gnu"
305 End:
306 */