Commit | Line | Data |
---|---|---|
56e55ac7 | 1 | /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc. |
abd95148 MD |
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 | * | |
e11f8b42 DH |
145 | * Hook arity is not a full member of this type and therefore lacks an |
146 | * accessor. It exists to aid debugging and is not intended to be used in | |
147 | * programs. | |
abd95148 MD |
148 | */ |
149 | ||
e841c3e0 | 150 | scm_bits_t scm_tc16_hook; |
abd95148 MD |
151 | |
152 | ||
153 | static SCM | |
e11f8b42 | 154 | make_hook (SCM n_args, const char *subr) |
abd95148 MD |
155 | { |
156 | int n; | |
e11f8b42 | 157 | |
abd95148 | 158 | if (SCM_UNBNDP (n_args)) |
e11f8b42 DH |
159 | { |
160 | n = 0; | |
161 | } | |
abd95148 MD |
162 | else |
163 | { | |
164 | SCM_ASSERT (SCM_INUMP (n_args), n_args, SCM_ARGn, subr); | |
165 | n = SCM_INUM (n_args); | |
685c0d71 DH |
166 | if (n < 0 || n > 16) |
167 | scm_out_of_range (subr, n_args); | |
abd95148 | 168 | } |
78a3503e | 169 | SCM_RETURN_NEWSMOB (scm_tc16_hook + (n << 16), SCM_UNPACK (SCM_EOL)); |
abd95148 MD |
170 | } |
171 | ||
172 | ||
173 | static int | |
e841c3e0 | 174 | hook_print (SCM hook, SCM port, scm_print_state *pstate) |
abd95148 MD |
175 | { |
176 | SCM ls, name; | |
177 | scm_puts ("#<hook ", port); | |
abd95148 MD |
178 | scm_intprint (SCM_HOOK_ARITY (hook), 10, port); |
179 | scm_putc (' ', port); | |
180 | scm_intprint (SCM_UNPACK (hook), 16, port); | |
181 | ls = SCM_HOOK_PROCEDURES (hook); | |
182 | while (SCM_NIMP (ls)) | |
183 | { | |
184 | scm_putc (' ', port); | |
185 | name = scm_procedure_name (SCM_CAR (ls)); | |
56e55ac7 | 186 | if (!SCM_FALSEP (name)) |
abd95148 MD |
187 | scm_iprin1 (name, port, pstate); |
188 | else | |
189 | scm_putc ('?', port); | |
190 | ls = SCM_CDR (ls); | |
191 | } | |
192 | scm_putc ('>', port); | |
193 | return 1; | |
194 | } | |
195 | ||
196 | ||
197 | SCM | |
198 | scm_create_hook (const char* name, int n_args) | |
199 | { | |
e11f8b42 | 200 | SCM hook = make_hook (SCM_MAKINUM (n_args), "scm_create_hook"); |
86d31dfe | 201 | scm_c_define (name, hook); |
f8eaf8b9 | 202 | scm_protect_object (hook); |
abd95148 MD |
203 | return hook; |
204 | } | |
205 | ||
206 | ||
abd95148 MD |
207 | SCM_DEFINE (scm_make_hook, "make-hook", 0, 1, 0, |
208 | (SCM n_args), | |
4d66be54 MG |
209 | "Create a hook for storing procedure of arity\n" |
210 | "@var{n_args}. @var{n_args} defaults to zero.") | |
abd95148 MD |
211 | #define FUNC_NAME s_scm_make_hook |
212 | { | |
e11f8b42 | 213 | return make_hook (n_args, FUNC_NAME); |
abd95148 MD |
214 | } |
215 | #undef FUNC_NAME | |
216 | ||
217 | ||
218 | SCM_DEFINE (scm_hook_p, "hook?", 1, 0, 0, | |
219 | (SCM x), | |
4d66be54 | 220 | "Return @code{#t} if @var{x} is a hook, @code{#f} otherwise.") |
abd95148 MD |
221 | #define FUNC_NAME s_scm_hook_p |
222 | { | |
223 | return SCM_BOOL (SCM_HOOKP (x)); | |
224 | } | |
225 | #undef FUNC_NAME | |
226 | ||
227 | ||
228 | SCM_DEFINE (scm_hook_empty_p, "hook-empty?", 1, 0, 0, | |
229 | (SCM hook), | |
4d66be54 MG |
230 | "Return @code{#t} if @var{hook} is an empty hook, @code{#f}\n" |
231 | "otherwise.") | |
abd95148 MD |
232 | #define FUNC_NAME s_scm_hook_empty_p |
233 | { | |
234 | SCM_VALIDATE_HOOK (1, hook); | |
235 | return SCM_BOOL (SCM_NULLP (SCM_HOOK_PROCEDURES (hook))); | |
236 | } | |
237 | #undef FUNC_NAME | |
238 | ||
239 | ||
240 | SCM_DEFINE (scm_add_hook_x, "add-hook!", 2, 1, 0, | |
241 | (SCM hook, SCM proc, SCM append_p), | |
8d1b3ae9 MG |
242 | "Add the procedure @var{proc} to the hook @var{hook}. The\n" |
243 | "procedure is added to the end if @var{append_p} is true,\n" | |
244 | "otherwise it is added to the front.") | |
abd95148 MD |
245 | #define FUNC_NAME s_scm_add_hook_x |
246 | { | |
247 | SCM arity, rest; | |
248 | int n_args; | |
249 | SCM_VALIDATE_HOOK (1,hook); | |
8ff45739 | 250 | SCM_ASSERT (!SCM_FALSEP (arity = scm_i_procedure_arity (proc)), |
abd95148 MD |
251 | proc, SCM_ARG2, FUNC_NAME); |
252 | n_args = SCM_HOOK_ARITY (hook); | |
253 | if (SCM_INUM (SCM_CAR (arity)) > n_args | |
254 | || (SCM_FALSEP (SCM_CADDR (arity)) | |
255 | && (SCM_INUM (SCM_CAR (arity)) + SCM_INUM (SCM_CADR (arity)) | |
256 | < n_args))) | |
257 | scm_wrong_type_arg (FUNC_NAME, 2, proc); | |
258 | rest = scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook)); | |
259 | SCM_SET_HOOK_PROCEDURES (hook, | |
e11f8b42 | 260 | (!SCM_UNBNDP (append_p) && !SCM_FALSEP (append_p) |
abd95148 MD |
261 | ? scm_append_x (SCM_LIST2 (rest, SCM_LIST1 (proc))) |
262 | : scm_cons (proc, rest))); | |
263 | return SCM_UNSPECIFIED; | |
264 | } | |
265 | #undef FUNC_NAME | |
266 | ||
267 | ||
268 | SCM_DEFINE (scm_remove_hook_x, "remove-hook!", 2, 0, 0, | |
269 | (SCM hook, SCM proc), | |
8d1b3ae9 | 270 | "Remove the procedure @var{proc} from the hook @var{hook}.") |
abd95148 MD |
271 | #define FUNC_NAME s_scm_remove_hook_x |
272 | { | |
273 | SCM_VALIDATE_HOOK (1, hook); | |
274 | SCM_SET_HOOK_PROCEDURES (hook, | |
275 | scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook))); | |
276 | return SCM_UNSPECIFIED; | |
277 | } | |
278 | #undef FUNC_NAME | |
279 | ||
280 | ||
281 | SCM_DEFINE (scm_reset_hook_x, "reset-hook!", 1, 0, 0, | |
282 | (SCM hook), | |
8d1b3ae9 | 283 | "Remove all procedures from the hook @var{hook}.") |
abd95148 MD |
284 | #define FUNC_NAME s_scm_reset_hook_x |
285 | { | |
286 | SCM_VALIDATE_HOOK (1,hook); | |
287 | SCM_SET_HOOK_PROCEDURES (hook, SCM_EOL); | |
288 | return SCM_UNSPECIFIED; | |
289 | } | |
290 | #undef FUNC_NAME | |
291 | ||
292 | ||
293 | SCM_DEFINE (scm_run_hook, "run-hook", 1, 0, 1, | |
294 | (SCM hook, SCM args), | |
8d1b3ae9 | 295 | "Apply all procedures from the hook @var{hook} to the arguments\n" |
4d66be54 MG |
296 | "@var{args}. The order of the procedure application is first to\n" |
297 | "last.") | |
abd95148 MD |
298 | #define FUNC_NAME s_scm_run_hook |
299 | { | |
300 | SCM_VALIDATE_HOOK (1,hook); | |
abd95148 MD |
301 | if (scm_ilength (args) != SCM_HOOK_ARITY (hook)) |
302 | SCM_MISC_ERROR ("Hook ~S requires ~A arguments", | |
303 | SCM_LIST2 (hook,SCM_MAKINUM (SCM_HOOK_ARITY (hook)))); | |
304 | scm_c_run_hook (hook, args); | |
305 | return SCM_UNSPECIFIED; | |
306 | } | |
307 | #undef FUNC_NAME | |
308 | ||
309 | ||
310 | void | |
311 | scm_c_run_hook (SCM hook, SCM args) | |
312 | { | |
313 | SCM procs = SCM_HOOK_PROCEDURES (hook); | |
314 | while (SCM_NIMP (procs)) | |
315 | { | |
316 | scm_apply (SCM_CAR (procs), args, SCM_EOL); | |
317 | procs = SCM_CDR (procs); | |
318 | } | |
319 | } | |
320 | ||
321 | ||
322 | SCM_DEFINE (scm_hook_to_list, "hook->list", 1, 0, 0, | |
323 | (SCM hook), | |
8d1b3ae9 | 324 | "Convert the procedure list of @var{hook} to a list.") |
abd95148 MD |
325 | #define FUNC_NAME s_scm_hook_to_list |
326 | { | |
327 | SCM_VALIDATE_HOOK (1, hook); | |
328 | return scm_list_copy (SCM_HOOK_PROCEDURES (hook)); | |
329 | } | |
330 | #undef FUNC_NAME | |
331 | ||
332 | ||
333 | \f | |
334 | ||
335 | void | |
336 | scm_init_hooks () | |
337 | { | |
338 | scm_tc16_hook = scm_make_smob_type ("hook", 0); | |
339 | scm_set_smob_mark (scm_tc16_hook, scm_markcdr); | |
e841c3e0 | 340 | scm_set_smob_print (scm_tc16_hook, hook_print); |
8dc9439f | 341 | #ifndef SCM_MAGIC_SNARFER |
abd95148 | 342 | #include "libguile/hooks.x" |
8dc9439f | 343 | #endif |
abd95148 MD |
344 | } |
345 | ||
346 | /* | |
347 | Local Variables: | |
348 | c-file-style: "gnu" | |
349 | End: | |
350 | */ |