Commit | Line | Data |
---|---|---|
abd95148 MD |
1 | /* Copyright (C) 1995, 1996, 1998, 1999, 2000 Free Software Foundation, Inc. |
2 | * | |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program 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 | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
16 | * Boston, MA 02111-1307 USA | |
17 | * | |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
40 | * If you do not wish that, delete this exception notice. */ | |
41 | ||
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
45 | \f | |
46 | ||
47 | #include <stdio.h> | |
48 | #include "libguile/_scm.h" | |
49 | ||
50 | #include "libguile/eval.h" | |
51 | #include "libguile/ports.h" | |
52 | #include "libguile/procprop.h" | |
53 | #include "libguile/root.h" | |
54 | #include "libguile/smob.h" | |
55 | #include "libguile/strings.h" | |
56 | ||
57 | #include "libguile/validate.h" | |
58 | #include "libguile/hooks.h" | |
59 | ||
60 | \f | |
61 | /* C level hooks | |
62 | * | |
63 | * Currently, this implementation is separate from the Scheme level | |
64 | * hooks. The possibility exists to implement the Scheme level hooks | |
65 | * using C level hooks. | |
66 | */ | |
67 | ||
68 | void | |
69 | scm_c_hook_init (scm_c_hook_t *hook, void *hook_data, scm_c_hook_type_t type) | |
70 | { | |
71 | hook->first = 0; | |
72 | hook->type = type; | |
73 | hook->data = hook_data; | |
74 | } | |
75 | ||
76 | void | |
77 | scm_c_hook_add (scm_c_hook_t *hook, | |
78 | scm_c_hook_function_t func, | |
79 | void *func_data, | |
80 | int appendp) | |
81 | { | |
82 | scm_c_hook_entry_t *entry = scm_must_malloc (sizeof (scm_c_hook_entry_t), | |
83 | "C level hook entry"); | |
84 | scm_c_hook_entry_t **loc = &hook->first; | |
85 | if (appendp) | |
86 | while (*loc) | |
87 | *loc = (*loc)->next; | |
88 | entry->next = *loc; | |
89 | entry->func = func; | |
90 | entry->data = func_data; | |
91 | *loc = entry; | |
92 | } | |
93 | ||
94 | void | |
95 | scm_c_hook_remove (scm_c_hook_t *hook, | |
96 | scm_c_hook_function_t func, | |
97 | void *func_data) | |
98 | { | |
99 | scm_c_hook_entry_t **loc = &hook->first; | |
100 | while (*loc) | |
101 | { | |
102 | if ((*loc)->func == func && (*loc)->data == func_data) | |
103 | { | |
104 | scm_c_hook_entry_t *entry = *loc; | |
105 | *loc = (*loc)->next; | |
106 | scm_must_free (entry); | |
107 | return; | |
108 | } | |
109 | loc = &(*loc)->next; | |
110 | } | |
111 | fprintf (stderr, "Attempt to remove non-existent hook function\n"); | |
112 | abort (); | |
113 | } | |
114 | ||
115 | void * | |
116 | scm_c_hook_run (scm_c_hook_t *hook, void *data) | |
117 | { | |
118 | scm_c_hook_entry_t *entry = hook->first; | |
119 | scm_c_hook_type_t type = hook->type; | |
120 | void *res = 0; | |
121 | while (entry) | |
122 | { | |
123 | res = (entry->func) (hook->data, entry->data, data); | |
124 | if (res) | |
125 | { | |
126 | if (type == SCM_C_HOOK_OR) | |
127 | break; | |
128 | } | |
129 | else | |
130 | { | |
131 | if (type == SCM_C_HOOK_AND) | |
132 | break; | |
133 | } | |
134 | entry = entry->next; | |
135 | } | |
136 | return res; | |
137 | } | |
138 | ||
139 | \f | |
140 | /* Scheme level hooks | |
141 | * | |
142 | * A hook is basically a list of procedures to be called at well defined | |
143 | * points in time. | |
144 | * | |
145 | * Hook name and arity are not full members of this type and therefore | |
146 | * lack accessors. They exist to aid debugging and are not intended | |
147 | * to be used in programs. | |
148 | * | |
149 | */ | |
150 | ||
151 | long scm_tc16_hook; | |
152 | ||
153 | ||
154 | static SCM | |
155 | make_hook (SCM name, SCM n_args, const char *subr) | |
156 | { | |
157 | int n; | |
158 | SCM_ASSERT (SCM_FALSEP (name) || SCM_SYMBOLP (name), | |
159 | name, | |
160 | SCM_ARG1, | |
161 | subr); | |
162 | if (SCM_UNBNDP (n_args)) | |
163 | n = 0; | |
164 | else | |
165 | { | |
166 | SCM_ASSERT (SCM_INUMP (n_args), n_args, SCM_ARGn, subr); | |
167 | n = SCM_INUM (n_args); | |
168 | } | |
169 | SCM_ASSERT (n >= 0 && n <= 16, n_args, SCM_OUTOFRANGE, subr); | |
170 | SCM_RETURN_NEWSMOB (scm_tc16_hook + (n << 16), SCM_UNPACK (SCM_LIST1 (name))); | |
171 | } | |
172 | ||
173 | ||
174 | static int | |
175 | print_hook (SCM hook, SCM port, scm_print_state *pstate) | |
176 | { | |
177 | SCM ls, name; | |
178 | scm_puts ("#<hook ", port); | |
179 | if (SCM_NFALSEP (SCM_HOOK_NAME (hook))) | |
180 | { | |
181 | scm_iprin1 (SCM_HOOK_NAME (hook), port, pstate); | |
182 | scm_putc (' ', port); | |
183 | } | |
184 | scm_intprint (SCM_HOOK_ARITY (hook), 10, port); | |
185 | scm_putc (' ', port); | |
186 | scm_intprint (SCM_UNPACK (hook), 16, port); | |
187 | ls = SCM_HOOK_PROCEDURES (hook); | |
188 | while (SCM_NIMP (ls)) | |
189 | { | |
190 | scm_putc (' ', port); | |
191 | name = scm_procedure_name (SCM_CAR (ls)); | |
192 | if (SCM_NFALSEP (name)) | |
193 | scm_iprin1 (name, port, pstate); | |
194 | else | |
195 | scm_putc ('?', port); | |
196 | ls = SCM_CDR (ls); | |
197 | } | |
198 | scm_putc ('>', port); | |
199 | return 1; | |
200 | } | |
201 | ||
202 | ||
203 | SCM | |
204 | scm_create_hook (const char* name, int n_args) | |
205 | { | |
206 | SCM vcell = scm_sysintern0 (name); | |
207 | SCM hook = make_hook (SCM_CAR (vcell), SCM_MAKINUM (n_args), | |
208 | "scm_create_hook"); | |
209 | SCM_SETCDR (vcell, hook); | |
210 | scm_protect_object (vcell); | |
211 | return hook; | |
212 | } | |
213 | ||
214 | ||
215 | SCM_DEFINE (scm_make_hook_with_name, "make-hook-with-name", 1, 1, 0, | |
216 | (SCM name, SCM n_args), | |
217 | "") | |
218 | #define FUNC_NAME s_scm_make_hook_with_name | |
219 | { | |
220 | return make_hook (name, n_args, FUNC_NAME); | |
221 | } | |
222 | #undef FUNC_NAME | |
223 | ||
224 | ||
225 | SCM_DEFINE (scm_make_hook, "make-hook", 0, 1, 0, | |
226 | (SCM n_args), | |
227 | "") | |
228 | #define FUNC_NAME s_scm_make_hook | |
229 | { | |
230 | return make_hook (SCM_BOOL_F, n_args, FUNC_NAME); | |
231 | } | |
232 | #undef FUNC_NAME | |
233 | ||
234 | ||
235 | SCM_DEFINE (scm_hook_p, "hook?", 1, 0, 0, | |
236 | (SCM x), | |
237 | "") | |
238 | #define FUNC_NAME s_scm_hook_p | |
239 | { | |
240 | return SCM_BOOL (SCM_HOOKP (x)); | |
241 | } | |
242 | #undef FUNC_NAME | |
243 | ||
244 | ||
245 | SCM_DEFINE (scm_hook_empty_p, "hook-empty?", 1, 0, 0, | |
246 | (SCM hook), | |
247 | "") | |
248 | #define FUNC_NAME s_scm_hook_empty_p | |
249 | { | |
250 | SCM_VALIDATE_HOOK (1, hook); | |
251 | return SCM_BOOL (SCM_NULLP (SCM_HOOK_PROCEDURES (hook))); | |
252 | } | |
253 | #undef FUNC_NAME | |
254 | ||
255 | ||
256 | SCM_DEFINE (scm_add_hook_x, "add-hook!", 2, 1, 0, | |
257 | (SCM hook, SCM proc, SCM append_p), | |
258 | "") | |
259 | #define FUNC_NAME s_scm_add_hook_x | |
260 | { | |
261 | SCM arity, rest; | |
262 | int n_args; | |
263 | SCM_VALIDATE_HOOK (1,hook); | |
264 | SCM_ASSERT (SCM_NFALSEP (arity = scm_i_procedure_arity (proc)), | |
265 | proc, SCM_ARG2, FUNC_NAME); | |
266 | n_args = SCM_HOOK_ARITY (hook); | |
267 | if (SCM_INUM (SCM_CAR (arity)) > n_args | |
268 | || (SCM_FALSEP (SCM_CADDR (arity)) | |
269 | && (SCM_INUM (SCM_CAR (arity)) + SCM_INUM (SCM_CADR (arity)) | |
270 | < n_args))) | |
271 | scm_wrong_type_arg (FUNC_NAME, 2, proc); | |
272 | rest = scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook)); | |
273 | SCM_SET_HOOK_PROCEDURES (hook, | |
274 | (!SCM_UNBNDP (append_p) && SCM_NFALSEP (append_p) | |
275 | ? scm_append_x (SCM_LIST2 (rest, SCM_LIST1 (proc))) | |
276 | : scm_cons (proc, rest))); | |
277 | return SCM_UNSPECIFIED; | |
278 | } | |
279 | #undef FUNC_NAME | |
280 | ||
281 | ||
282 | SCM_DEFINE (scm_remove_hook_x, "remove-hook!", 2, 0, 0, | |
283 | (SCM hook, SCM proc), | |
284 | "") | |
285 | #define FUNC_NAME s_scm_remove_hook_x | |
286 | { | |
287 | SCM_VALIDATE_HOOK (1, hook); | |
288 | SCM_SET_HOOK_PROCEDURES (hook, | |
289 | scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook))); | |
290 | return SCM_UNSPECIFIED; | |
291 | } | |
292 | #undef FUNC_NAME | |
293 | ||
294 | ||
295 | SCM_DEFINE (scm_reset_hook_x, "reset-hook!", 1, 0, 0, | |
296 | (SCM hook), | |
297 | "") | |
298 | #define FUNC_NAME s_scm_reset_hook_x | |
299 | { | |
300 | SCM_VALIDATE_HOOK (1,hook); | |
301 | SCM_SET_HOOK_PROCEDURES (hook, SCM_EOL); | |
302 | return SCM_UNSPECIFIED; | |
303 | } | |
304 | #undef FUNC_NAME | |
305 | ||
306 | ||
307 | SCM_DEFINE (scm_run_hook, "run-hook", 1, 0, 1, | |
308 | (SCM hook, SCM args), | |
309 | "") | |
310 | #define FUNC_NAME s_scm_run_hook | |
311 | { | |
312 | SCM_VALIDATE_HOOK (1,hook); | |
abd95148 MD |
313 | if (scm_ilength (args) != SCM_HOOK_ARITY (hook)) |
314 | SCM_MISC_ERROR ("Hook ~S requires ~A arguments", | |
315 | SCM_LIST2 (hook,SCM_MAKINUM (SCM_HOOK_ARITY (hook)))); | |
316 | scm_c_run_hook (hook, args); | |
317 | return SCM_UNSPECIFIED; | |
318 | } | |
319 | #undef FUNC_NAME | |
320 | ||
321 | ||
322 | void | |
323 | scm_c_run_hook (SCM hook, SCM args) | |
324 | { | |
325 | SCM procs = SCM_HOOK_PROCEDURES (hook); | |
326 | while (SCM_NIMP (procs)) | |
327 | { | |
328 | scm_apply (SCM_CAR (procs), args, SCM_EOL); | |
329 | procs = SCM_CDR (procs); | |
330 | } | |
331 | } | |
332 | ||
333 | ||
334 | SCM_DEFINE (scm_hook_to_list, "hook->list", 1, 0, 0, | |
335 | (SCM hook), | |
336 | "") | |
337 | #define FUNC_NAME s_scm_hook_to_list | |
338 | { | |
339 | SCM_VALIDATE_HOOK (1, hook); | |
340 | return scm_list_copy (SCM_HOOK_PROCEDURES (hook)); | |
341 | } | |
342 | #undef FUNC_NAME | |
343 | ||
344 | ||
345 | \f | |
346 | ||
347 | void | |
348 | scm_init_hooks () | |
349 | { | |
350 | scm_tc16_hook = scm_make_smob_type ("hook", 0); | |
351 | scm_set_smob_mark (scm_tc16_hook, scm_markcdr); | |
352 | scm_set_smob_print (scm_tc16_hook, print_hook); | |
353 | ||
354 | #include "libguile/hooks.x" | |
355 | } | |
356 | ||
357 | /* | |
358 | Local Variables: | |
359 | c-file-style: "gnu" | |
360 | End: | |
361 | */ |