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