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