*** empty log message ***
[bpt/guile.git] / libguile / feature.c
CommitLineData
78a0461a 1/* Copyright (C) 1995, 1996, 1998, 1999 Free Software Foundation, Inc.
0f2d19dd
JB
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
0f2d19dd
JB
41\f
42
43#include <stdio.h>
44#include "_scm.h"
45
1330700c 46#include "eval.h"
b90d369e 47#include "procprop.h"
26425129 48#include "smob.h"
1330700c 49
20e6290e
JB
50#include "feature.h"
51
95b88819
GH
52#ifdef HAVE_STRING_H
53#include <string.h>
54#endif
0f2d19dd
JB
55\f
56
f072db0c 57static SCM *scm_loc_features;
0f2d19dd 58
0f2d19dd 59void
b90d369e 60scm_add_feature (str)
3eeba8d4 61 const char* str;
0f2d19dd 62{
b90d369e
MD
63 *scm_loc_features = scm_cons (SCM_CAR (scm_intern (str, strlen (str))),
64 *scm_loc_features);
0f2d19dd
JB
65}
66
67
68\f
0f2d19dd 69SCM_PROC(s_program_arguments, "program-arguments", 0, 0, 0, scm_program_arguments);
0b886892 70
0f2d19dd
JB
71SCM
72scm_program_arguments ()
0f2d19dd
JB
73{
74 return scm_progargs;
75}
76
f29de790
JB
77/* Set the value returned by program-arguments, given ARGC and ARGV.
78
79 If FIRST is non-zero, make it the first element; we do this in
80 situations where other code (like getopt) has parsed out a few
81 arguments, but we still want the script name to be the first
82 element. */
0b886892 83void
f29de790 84scm_set_program_arguments (argc, argv, first)
0b886892
JB
85 int argc;
86 char **argv;
f29de790 87 char *first;
0b886892
JB
88{
89 scm_progargs = scm_makfromstrs (argc, argv);
f29de790
JB
90 if (first)
91 scm_progargs = scm_cons (scm_makfrom0str (first), scm_progargs);
0b886892
JB
92}
93
0f2d19dd 94
0f2d19dd 95\f
36399a6d
MD
96/* Hooks
97 *
98 * A hook is basically a list of procedures to be called at well defined
99 * points in time.
100 *
101 * Hook name and arity are not full members of this type and therefore
102 * lack accessors. They are added to aid debugging and are not
103 * intended to be used in programs.
104 *
105 */
1330700c 106
26425129
MD
107long scm_tc16_hook;
108
1330700c 109
36399a6d
MD
110static SCM
111make_hook (SCM name, SCM n_args, const char *subr)
1330700c 112{
26425129 113 int n;
36399a6d
MD
114 SCM_ASSERT (SCM_FALSEP (name) || (SCM_NIMP (name) && SCM_SYMBOLP (name)),
115 name,
116 SCM_ARG1,
117 subr);
b90d369e 118 if (SCM_UNBNDP (n_args))
26425129 119 n = 0;
b90d369e 120 else
26425129 121 {
36399a6d 122 SCM_ASSERT (SCM_INUMP (n_args), n_args, SCM_ARGn, subr);
26425129
MD
123 n = SCM_INUM (n_args);
124 }
36399a6d
MD
125 SCM_ASSERT (n >= 0 && n <= 16, n_args, SCM_OUTOFRANGE, subr);
126 SCM_RETURN_NEWSMOB (scm_tc16_hook + (n << 16), SCM_LIST1 (name));
127}
128
129
130static int
131print_hook (SCM hook, SCM port, scm_print_state *pstate)
132{
133 SCM ls, name;
134 scm_puts ("#<hook ", port);
135 if (SCM_NFALSEP (SCM_HOOK_NAME (hook)))
136 {
137 scm_iprin1 (SCM_HOOK_NAME (hook), port, pstate);
138 scm_putc (' ', port);
139 }
140 scm_intprint (SCM_HOOK_ARITY (hook), 10, port);
141 scm_putc (' ', port);
142 scm_intprint (hook, 16, port);
143 ls = SCM_HOOK_PROCEDURES (hook);
144 while (SCM_NIMP (ls))
145 {
146 scm_putc (' ', port);
147 name = scm_procedure_name (SCM_CAR (ls));
148 if (SCM_NFALSEP (name))
149 scm_iprin1 (name, port, pstate);
150 else
151 scm_putc ('?', port);
152 ls = SCM_CDR (ls);
153 }
154 scm_putc ('>', port);
155 return 1;
1330700c
MD
156}
157
26425129 158
1330700c 159SCM
36399a6d 160scm_create_hook (const char* name, int n_args)
1330700c 161{
36399a6d
MD
162 SCM vcell = scm_sysintern0 (name);
163 SCM hook = make_hook (SCM_CAR (vcell), SCM_MAKINUM (n_args),
164 "scm_create_hook");
165 SCM_SETCDR (vcell, hook);
166 scm_protect_object (vcell);
1330700c
MD
167 return hook;
168}
169
26425129 170
36399a6d
MD
171/* This function is deprecated. It will be removed in next release. */
172SCM
173scm_make_named_hook (const char* name, int n_args)
174{
175 return scm_create_hook (name, n_args);
176}
177
178
179SCM_PROC (s_make_hook_with_name, "make-hook-with-name", 1, 1, 0, scm_make_hook_with_name);
180
181SCM
182scm_make_hook_with_name (SCM name, SCM n_args)
183{
184 return make_hook (name, n_args, s_make_hook_with_name);
185}
186
187
188SCM_PROC (s_make_hook, "make-hook", 0, 1, 0, scm_make_hook);
189
190SCM
191scm_make_hook (SCM n_args)
192{
193 return make_hook (SCM_BOOL_F, n_args, s_make_hook);
194}
195
196
26425129
MD
197SCM_PROC (s_hook_p, "hook?", 1, 0, 0, scm_hook_p);
198
199SCM
200scm_hook_p (SCM x)
201{
202 return SCM_NIMP (x) && SCM_HOOKP (x) ? SCM_BOOL_T : SCM_BOOL_F;
203}
204
205
206SCM_PROC (s_hook_empty_p, "hook-empty?", 1, 0, 0, scm_hook_empty_p);
207
208SCM
209scm_hook_empty_p (SCM hook)
210{
211 SCM_ASSERT (SCM_NIMP (hook) && SCM_HOOKP (hook),
212 hook, SCM_ARG1, s_hook_empty_p);
213 return SCM_NULLP (SCM_HOOK_PROCEDURES (hook)) ? SCM_BOOL_T : SCM_BOOL_F;
214}
215
216
1330700c
MD
217SCM_PROC (s_add_hook_x, "add-hook!", 2, 1, 0, scm_add_hook_x);
218
219SCM
b90d369e 220scm_add_hook_x (SCM hook, SCM proc, SCM append_p)
1330700c 221{
b90d369e
MD
222 SCM arity, rest;
223 int n_args;
26425129 224 SCM_ASSERT (SCM_NIMP (hook) && SCM_HOOKP (hook),
1330700c 225 hook, SCM_ARG1, s_add_hook_x);
b90d369e
MD
226 SCM_ASSERT (SCM_NFALSEP (arity = scm_i_procedure_arity (proc)),
227 proc, SCM_ARG2, s_add_hook_x);
26425129 228 n_args = SCM_HOOK_ARITY (hook);
b90d369e
MD
229 if (SCM_INUM (SCM_CAR (arity)) > n_args
230 || (SCM_FALSEP (SCM_CADDR (arity))
231 && (SCM_INUM (SCM_CAR (arity)) + SCM_INUM (SCM_CADR (arity))
232 < n_args)))
233 scm_misc_error (s_add_hook_x,
234 "This hook requires %s arguments",
26425129
MD
235 SCM_LIST1 (SCM_MAKINUM (SCM_HOOK_ARITY (hook))));
236 rest = scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook));
237 SCM_SET_HOOK_PROCEDURES (hook,
238 (!SCM_UNBNDP (append_p) && SCM_NFALSEP (append_p)
239 ? scm_append_x (SCM_LIST2 (rest, SCM_LIST1 (proc)))
240 : scm_cons (proc, rest)));
1330700c
MD
241 return SCM_UNSPECIFIED;
242}
243
26425129 244
1330700c
MD
245SCM_PROC (s_remove_hook_x, "remove-hook!", 2, 0, 0, scm_remove_hook_x);
246
247SCM
26425129 248scm_remove_hook_x (SCM hook, SCM proc)
1330700c 249{
26425129 250 SCM_ASSERT (SCM_NIMP (hook) && SCM_HOOKP (hook),
b90d369e 251 hook, SCM_ARG1, s_remove_hook_x);
26425129
MD
252 SCM_SET_HOOK_PROCEDURES (hook,
253 scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook)));
1330700c
MD
254 return SCM_UNSPECIFIED;
255}
256
26425129 257
b90d369e 258SCM_PROC (s_reset_hook_x, "reset-hook!", 1, 0, 0, scm_reset_hook_x);
1330700c
MD
259
260SCM
b90d369e 261scm_reset_hook_x (SCM hook)
1330700c 262{
26425129 263 SCM_ASSERT (SCM_NIMP (hook) && SCM_HOOKP (hook),
b90d369e 264 hook, SCM_ARG1, s_reset_hook_x);
26425129 265 SCM_SET_HOOK_PROCEDURES (hook, SCM_EOL);
b90d369e
MD
266 return SCM_UNSPECIFIED;
267}
268
26425129 269
b90d369e
MD
270SCM_PROC (s_run_hook, "run-hook", 1, 0, 1, scm_run_hook);
271
272SCM
273scm_run_hook (SCM hook, SCM args)
274{
26425129 275 SCM_ASSERT (SCM_NIMP (hook) && SCM_HOOKP (hook),
b90d369e
MD
276 hook, SCM_ARG1, s_run_hook);
277 if (SCM_UNBNDP (args))
278 args = SCM_EOL;
26425129 279 if (scm_ilength (args) != SCM_HOOK_ARITY (hook))
b90d369e
MD
280 scm_misc_error (s_add_hook_x,
281 "This hook requires %s arguments",
26425129 282 SCM_LIST1 (SCM_MAKINUM (SCM_HOOK_ARITY (hook))));
ef1ae563
MD
283 scm_c_run_hook (hook, args);
284 return SCM_UNSPECIFIED;
285}
286
26425129 287
ef1ae563
MD
288void
289scm_c_run_hook (SCM hook, SCM args)
290{
26425129
MD
291 SCM procs = SCM_HOOK_PROCEDURES (hook);
292 while (SCM_NIMP (procs))
293 {
294 scm_apply (SCM_CAR (procs), args, SCM_EOL);
295 procs = SCM_CDR (procs);
296 }
297}
298
299
300SCM_PROC (s_hook_to_list, "hook->list", 1, 0, 0, scm_hook_to_list);
301
302SCM
303scm_hook_to_list (SCM hook)
304{
305 SCM_ASSERT (SCM_NIMP (hook) && SCM_HOOKP (hook),
306 hook, SCM_ARG1, s_hook_to_list);
307 return scm_list_copy (SCM_HOOK_PROCEDURES (hook));
1330700c
MD
308}
309
26425129 310
1330700c 311\f
0f2d19dd 312
0f2d19dd
JB
313void
314scm_init_feature()
0f2d19dd 315{
25d8012c 316 scm_loc_features = SCM_CDRLOC (scm_sysintern ("*features*", SCM_EOL));
cf7c17e9 317#ifdef SCM_RECKLESS
0f2d19dd
JB
318 scm_add_feature("reckless");
319#endif
320#ifndef _Windows
321 scm_add_feature("system");
322#endif
323#ifdef vms
324 scm_add_feature(s_ed);
325#endif
326#ifdef SICP
327 scm_add_feature("sicp");
328#endif
329#ifndef GO32
330 scm_add_feature("char-ready?");
331#endif
332#ifndef CHEAP_CONTINUATIONS
333 scm_add_feature ("full-continuation");
334#endif
a6cba733
MD
335#ifdef USE_THREADS
336 scm_add_feature ("threads");
337#endif
338
e2806c10 339 scm_sysintern ("char-code-limit", SCM_MAKINUM (SCM_CHAR_CODE_LIMIT));
26425129
MD
340
341 scm_tc16_hook = scm_make_smob_type ("hook", 0);
342 scm_set_smob_mark (scm_tc16_hook, scm_markcdr);
36399a6d 343 scm_set_smob_print (scm_tc16_hook, print_hook);
26425129 344
0f2d19dd
JB
345#include "feature.x"
346}