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