Commit | Line | Data |
---|---|---|
c35738c1 | 1 | /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003 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 | ||
abd95148 MD |
42 | |
43 | \f | |
44 | ||
45 | #include <stdio.h> | |
46 | #include "libguile/_scm.h" | |
47 | ||
48 | #include "libguile/eval.h" | |
49 | #include "libguile/ports.h" | |
50 | #include "libguile/procprop.h" | |
51 | #include "libguile/root.h" | |
52 | #include "libguile/smob.h" | |
53 | #include "libguile/strings.h" | |
54 | ||
55 | #include "libguile/validate.h" | |
56 | #include "libguile/hooks.h" | |
57 | ||
58 | \f | |
59 | /* C level hooks | |
60 | * | |
61 | * Currently, this implementation is separate from the Scheme level | |
62 | * hooks. The possibility exists to implement the Scheme level hooks | |
63 | * using C level hooks. | |
64 | */ | |
65 | ||
66 | void | |
387d418c | 67 | scm_c_hook_init (scm_t_c_hook *hook, void *hook_data, scm_t_c_hook_type type) |
abd95148 MD |
68 | { |
69 | hook->first = 0; | |
70 | hook->type = type; | |
71 | hook->data = hook_data; | |
72 | } | |
73 | ||
74 | void | |
92c2555f MV |
75 | scm_c_hook_add (scm_t_c_hook *hook, |
76 | scm_t_c_hook_function func, | |
abd95148 MD |
77 | void *func_data, |
78 | int appendp) | |
79 | { | |
4c9419ac | 80 | scm_t_c_hook_entry *entry = scm_malloc (sizeof (scm_t_c_hook_entry)); |
92c2555f | 81 | scm_t_c_hook_entry **loc = &hook->first; |
abd95148 MD |
82 | if (appendp) |
83 | while (*loc) | |
c35738c1 | 84 | loc = &(*loc)->next; |
abd95148 MD |
85 | entry->next = *loc; |
86 | entry->func = func; | |
87 | entry->data = func_data; | |
88 | *loc = entry; | |
89 | } | |
90 | ||
91 | void | |
92c2555f MV |
92 | scm_c_hook_remove (scm_t_c_hook *hook, |
93 | scm_t_c_hook_function func, | |
abd95148 MD |
94 | void *func_data) |
95 | { | |
92c2555f | 96 | scm_t_c_hook_entry **loc = &hook->first; |
abd95148 MD |
97 | while (*loc) |
98 | { | |
99 | if ((*loc)->func == func && (*loc)->data == func_data) | |
100 | { | |
92c2555f | 101 | scm_t_c_hook_entry *entry = *loc; |
abd95148 | 102 | *loc = (*loc)->next; |
4c9419ac | 103 | free (entry); |
abd95148 MD |
104 | return; |
105 | } | |
106 | loc = &(*loc)->next; | |
107 | } | |
108 | fprintf (stderr, "Attempt to remove non-existent hook function\n"); | |
109 | abort (); | |
110 | } | |
111 | ||
112 | void * | |
92c2555f | 113 | scm_c_hook_run (scm_t_c_hook *hook, void *data) |
abd95148 | 114 | { |
92c2555f | 115 | scm_t_c_hook_entry *entry = hook->first; |
387d418c | 116 | scm_t_c_hook_type type = hook->type; |
abd95148 MD |
117 | void *res = 0; |
118 | while (entry) | |
119 | { | |
120 | res = (entry->func) (hook->data, entry->data, data); | |
121 | if (res) | |
122 | { | |
123 | if (type == SCM_C_HOOK_OR) | |
124 | break; | |
125 | } | |
126 | else | |
127 | { | |
128 | if (type == SCM_C_HOOK_AND) | |
129 | break; | |
130 | } | |
131 | entry = entry->next; | |
132 | } | |
133 | return res; | |
134 | } | |
135 | ||
136 | \f | |
137 | /* Scheme level hooks | |
138 | * | |
139 | * A hook is basically a list of procedures to be called at well defined | |
140 | * points in time. | |
141 | * | |
e11f8b42 DH |
142 | * Hook arity is not a full member of this type and therefore lacks an |
143 | * accessor. It exists to aid debugging and is not intended to be used in | |
144 | * programs. | |
abd95148 MD |
145 | */ |
146 | ||
92c2555f | 147 | scm_t_bits scm_tc16_hook; |
abd95148 MD |
148 | |
149 | ||
abd95148 | 150 | static int |
e841c3e0 | 151 | hook_print (SCM hook, SCM port, scm_print_state *pstate) |
abd95148 MD |
152 | { |
153 | SCM ls, name; | |
154 | scm_puts ("#<hook ", port); | |
abd95148 MD |
155 | scm_intprint (SCM_HOOK_ARITY (hook), 10, port); |
156 | scm_putc (' ', port); | |
157 | scm_intprint (SCM_UNPACK (hook), 16, port); | |
158 | ls = SCM_HOOK_PROCEDURES (hook); | |
159 | while (SCM_NIMP (ls)) | |
160 | { | |
161 | scm_putc (' ', port); | |
162 | name = scm_procedure_name (SCM_CAR (ls)); | |
56e55ac7 | 163 | if (!SCM_FALSEP (name)) |
abd95148 MD |
164 | scm_iprin1 (name, port, pstate); |
165 | else | |
166 | scm_putc ('?', port); | |
167 | ls = SCM_CDR (ls); | |
168 | } | |
169 | scm_putc ('>', port); | |
170 | return 1; | |
171 | } | |
172 | ||
abd95148 | 173 | |
abd95148 MD |
174 | SCM_DEFINE (scm_make_hook, "make-hook", 0, 1, 0, |
175 | (SCM n_args), | |
f91e4547 MG |
176 | "Create a hook for storing procedure of arity @var{n_args}.\n" |
177 | "@var{n_args} defaults to zero. The returned value is a hook\n" | |
178 | "object to be used with the other hook procedures.") | |
abd95148 MD |
179 | #define FUNC_NAME s_scm_make_hook |
180 | { | |
fde50407 ML |
181 | int n; |
182 | ||
183 | if (SCM_UNBNDP (n_args)) | |
184 | { | |
185 | n = 0; | |
186 | } | |
187 | else | |
188 | { | |
189 | SCM_VALIDATE_INUM_COPY (SCM_ARG1, n_args, n); | |
190 | if (n < 0 || n > 16) | |
191 | SCM_OUT_OF_RANGE (SCM_ARG1, n_args); | |
192 | } | |
193 | ||
194 | SCM_RETURN_NEWSMOB (scm_tc16_hook + (n << 16), SCM_UNPACK (SCM_EOL)); | |
abd95148 MD |
195 | } |
196 | #undef FUNC_NAME | |
197 | ||
198 | ||
199 | SCM_DEFINE (scm_hook_p, "hook?", 1, 0, 0, | |
200 | (SCM x), | |
4d66be54 | 201 | "Return @code{#t} if @var{x} is a hook, @code{#f} otherwise.") |
abd95148 MD |
202 | #define FUNC_NAME s_scm_hook_p |
203 | { | |
204 | return SCM_BOOL (SCM_HOOKP (x)); | |
205 | } | |
206 | #undef FUNC_NAME | |
207 | ||
208 | ||
209 | SCM_DEFINE (scm_hook_empty_p, "hook-empty?", 1, 0, 0, | |
210 | (SCM hook), | |
4d66be54 MG |
211 | "Return @code{#t} if @var{hook} is an empty hook, @code{#f}\n" |
212 | "otherwise.") | |
abd95148 MD |
213 | #define FUNC_NAME s_scm_hook_empty_p |
214 | { | |
215 | SCM_VALIDATE_HOOK (1, hook); | |
216 | return SCM_BOOL (SCM_NULLP (SCM_HOOK_PROCEDURES (hook))); | |
217 | } | |
218 | #undef FUNC_NAME | |
219 | ||
220 | ||
221 | SCM_DEFINE (scm_add_hook_x, "add-hook!", 2, 1, 0, | |
222 | (SCM hook, SCM proc, SCM append_p), | |
8d1b3ae9 MG |
223 | "Add the procedure @var{proc} to the hook @var{hook}. The\n" |
224 | "procedure is added to the end if @var{append_p} is true,\n" | |
f91e4547 MG |
225 | "otherwise it is added to the front. The return value of this\n" |
226 | "procedure is not specified.") | |
abd95148 MD |
227 | #define FUNC_NAME s_scm_add_hook_x |
228 | { | |
229 | SCM arity, rest; | |
230 | int n_args; | |
34d19ef6 | 231 | SCM_VALIDATE_HOOK (1, hook); |
8ff45739 | 232 | SCM_ASSERT (!SCM_FALSEP (arity = scm_i_procedure_arity (proc)), |
abd95148 MD |
233 | proc, SCM_ARG2, FUNC_NAME); |
234 | n_args = SCM_HOOK_ARITY (hook); | |
235 | if (SCM_INUM (SCM_CAR (arity)) > n_args | |
236 | || (SCM_FALSEP (SCM_CADDR (arity)) | |
237 | && (SCM_INUM (SCM_CAR (arity)) + SCM_INUM (SCM_CADR (arity)) | |
238 | < n_args))) | |
239 | scm_wrong_type_arg (FUNC_NAME, 2, proc); | |
240 | rest = scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook)); | |
241 | SCM_SET_HOOK_PROCEDURES (hook, | |
e11f8b42 | 242 | (!SCM_UNBNDP (append_p) && !SCM_FALSEP (append_p) |
1afff620 | 243 | ? scm_append_x (scm_list_2 (rest, scm_list_1 (proc))) |
abd95148 MD |
244 | : scm_cons (proc, rest))); |
245 | return SCM_UNSPECIFIED; | |
246 | } | |
247 | #undef FUNC_NAME | |
248 | ||
249 | ||
250 | SCM_DEFINE (scm_remove_hook_x, "remove-hook!", 2, 0, 0, | |
251 | (SCM hook, SCM proc), | |
f91e4547 MG |
252 | "Remove the procedure @var{proc} from the hook @var{hook}. The\n" |
253 | "return value of this procedure is not specified.") | |
abd95148 MD |
254 | #define FUNC_NAME s_scm_remove_hook_x |
255 | { | |
256 | SCM_VALIDATE_HOOK (1, hook); | |
257 | SCM_SET_HOOK_PROCEDURES (hook, | |
258 | scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook))); | |
259 | return SCM_UNSPECIFIED; | |
260 | } | |
261 | #undef FUNC_NAME | |
262 | ||
263 | ||
264 | SCM_DEFINE (scm_reset_hook_x, "reset-hook!", 1, 0, 0, | |
265 | (SCM hook), | |
f91e4547 MG |
266 | "Remove all procedures from the hook @var{hook}. The return\n" |
267 | "value of this procedure is not specified.") | |
abd95148 MD |
268 | #define FUNC_NAME s_scm_reset_hook_x |
269 | { | |
34d19ef6 | 270 | SCM_VALIDATE_HOOK (1, hook); |
abd95148 MD |
271 | SCM_SET_HOOK_PROCEDURES (hook, SCM_EOL); |
272 | return SCM_UNSPECIFIED; | |
273 | } | |
274 | #undef FUNC_NAME | |
275 | ||
276 | ||
277 | SCM_DEFINE (scm_run_hook, "run-hook", 1, 0, 1, | |
278 | (SCM hook, SCM args), | |
8d1b3ae9 | 279 | "Apply all procedures from the hook @var{hook} to the arguments\n" |
4d66be54 | 280 | "@var{args}. The order of the procedure application is first to\n" |
f91e4547 | 281 | "last. The return value of this procedure is not specified.") |
abd95148 MD |
282 | #define FUNC_NAME s_scm_run_hook |
283 | { | |
34d19ef6 | 284 | SCM_VALIDATE_HOOK (1, hook); |
abd95148 MD |
285 | if (scm_ilength (args) != SCM_HOOK_ARITY (hook)) |
286 | SCM_MISC_ERROR ("Hook ~S requires ~A arguments", | |
1afff620 | 287 | scm_list_2 (hook, SCM_MAKINUM (SCM_HOOK_ARITY (hook)))); |
abd95148 MD |
288 | scm_c_run_hook (hook, args); |
289 | return SCM_UNSPECIFIED; | |
290 | } | |
291 | #undef FUNC_NAME | |
292 | ||
293 | ||
294 | void | |
295 | scm_c_run_hook (SCM hook, SCM args) | |
296 | { | |
297 | SCM procs = SCM_HOOK_PROCEDURES (hook); | |
298 | while (SCM_NIMP (procs)) | |
299 | { | |
fdc28395 | 300 | scm_apply_0 (SCM_CAR (procs), args); |
abd95148 MD |
301 | procs = SCM_CDR (procs); |
302 | } | |
303 | } | |
304 | ||
305 | ||
306 | SCM_DEFINE (scm_hook_to_list, "hook->list", 1, 0, 0, | |
307 | (SCM hook), | |
8d1b3ae9 | 308 | "Convert the procedure list of @var{hook} to a list.") |
abd95148 MD |
309 | #define FUNC_NAME s_scm_hook_to_list |
310 | { | |
311 | SCM_VALIDATE_HOOK (1, hook); | |
312 | return scm_list_copy (SCM_HOOK_PROCEDURES (hook)); | |
313 | } | |
314 | #undef FUNC_NAME | |
315 | ||
316 | ||
317 | \f | |
318 | ||
319 | void | |
320 | scm_init_hooks () | |
321 | { | |
322 | scm_tc16_hook = scm_make_smob_type ("hook", 0); | |
323 | scm_set_smob_mark (scm_tc16_hook, scm_markcdr); | |
e841c3e0 | 324 | scm_set_smob_print (scm_tc16_hook, hook_print); |
abd95148 MD |
325 | #include "libguile/hooks.x" |
326 | } | |
327 | ||
328 | /* | |
329 | Local Variables: | |
330 | c-file-style: "gnu" | |
331 | End: | |
332 | */ |