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