*** empty log message ***
[bpt/guile.git] / libguile / init.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 Free Software Foundation, Inc.
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, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41\f
42
43#include <stdio.h>
44#include "_scm.h"
45
20e6290e
JB
46/* Everybody has an init function. */
47#include "alist.h"
48#include "append.h"
49#include "arbiters.h"
50#include "async.h"
0e44fcca 51#include "backtrace.h"
20e6290e
JB
52#include "boolean.h"
53#include "chars.h"
54#include "continuations.h"
55#ifdef DEBUG_EXTENSIONS
56#include "debug.h"
57#endif
58#include "dynwind.h"
59#include "eq.h"
60#include "error.h"
61#include "eval.h"
62#include "fdsocket.h"
63#include "feature.h"
20e6290e
JB
64#include "filesys.h"
65#include "fports.h"
66#include "gc.h"
67#include "gdbint.h"
68#include "gsubr.h"
69#include "hash.h"
70#include "hashtab.h"
71#include "ioext.h"
72#include "kw.h"
73#include "list.h"
74#include "load.h"
75#include "mallocs.h"
76#include "mbstrings.h"
77#include "numbers.h"
78#include "objprop.h"
79#include "options.h"
80#include "pairs.h"
81#include "ports.h"
82#include "posix.h"
83#include "print.h"
84#include "procprop.h"
85#include "procs.h"
86#include "ramap.h"
87#include "read.h"
88#include "scmsigs.h"
89#include "sequences.h"
90#include "simpos.h"
a8be22fe 91#include "smob.h"
20e6290e
JB
92#include "socket.h"
93#include "srcprop.h"
94#include "stackchk.h"
0e44fcca 95#include "stacks.h"
20e6290e
JB
96#include "stime.h"
97#include "strings.h"
98#include "strop.h"
99#include "strorder.h"
100#include "strports.h"
101#include "struct.h"
102#include "symbols.h"
103#include "tag.h"
104#include "throw.h"
105#include "unif.h"
106#include "variable.h"
107#include "vectors.h"
108#include "version.h"
109#include "vports.h"
110#include "weaks.h"
111
a8be22fe
JB
112#include "init.h"
113
95b88819
GH
114#ifdef HAVE_STRING_H
115#include <string.h>
116#endif
117#ifdef HAVE_UNISTD_H
118#include <unistd.h>
119#endif
0f2d19dd
JB
120\f
121
1cdaaafb
JB
122static void scm_start_stack SCM_P ((void *base));
123static void scm_restart_stack SCM_P ((void * base));
124
125static void
126scm_start_stack (base)
0f2d19dd 127 void * base;
0f2d19dd 128{
9ef3d0ee 129 SCM root;
0f2d19dd 130
9ef3d0ee
MD
131 root = scm_permanent_object (scm_make_root (SCM_UNDEFINED));
132 scm_set_root (SCM_ROOT_STATE (root));
0f2d19dd
JB
133 scm_stack_base = base;
134
0f2d19dd
JB
135 scm_exitval = SCM_BOOL_F; /* vestigial */
136
137 scm_top_level_lookup_thunk_var = SCM_BOOL_F;
138 scm_system_transformer = SCM_BOOL_F;
139
140 /* Create an object to hold the root continuation.
141 */
142 SCM_NEWCELL (scm_rootcont);
1cdaaafb
JB
143 SCM_SETJMPBUF (scm_rootcont, scm_must_malloc ((long) sizeof (scm_contregs),
144 "continuation"));
898a256f 145 SCM_SETCAR (scm_rootcont, scm_tc7_contin);
4b5166ac 146 SCM_SEQ (scm_rootcont) = 0;
0f2d19dd
JB
147 /* The root continuation if further initialized by scm_restart_stack. */
148
149 /* Create the look-aside stack for variables that are shared between
150 * captured continuations.
151 */
1cdaaafb
JB
152 scm_continuation_stack = scm_make_vector (SCM_MAKINUM (512),
153 SCM_UNDEFINED, SCM_UNDEFINED);
0f2d19dd
JB
154 /* The continuation stack is further initialized by scm_restart_stack. */
155
1cdaaafb
JB
156 /* The remainder of stack initialization is factored out to another
157 * function so that if this stack is ever exitted, it can be
158 * re-entered using scm_restart_stack. */
0f2d19dd
JB
159 scm_restart_stack (base);
160}
161
162
1cdaaafb 163static void
0f2d19dd
JB
164scm_restart_stack (base)
165 void * base;
0f2d19dd
JB
166{
167 scm_dynwinds = SCM_EOL;
168 SCM_DYNENV (scm_rootcont) = SCM_EOL;
169 SCM_THROW_VALUE (scm_rootcont) = SCM_EOL;
4e1caa79 170#ifdef DEBUG_EXTENSIONS
4b5166ac 171 SCM_DFRAME (scm_rootcont) = scm_last_debug_frame = 0;
4e1caa79 172#endif
0f2d19dd
JB
173 SCM_BASE (scm_rootcont) = base;
174 scm_continuation_stack_ptr = SCM_MAKINUM (0);
175}
176
177#if 0
178static char remsg[] = "remove\n#define ", addmsg[] = "add\n#define ";
179
1cc91f1b
JB
180
181static void fixconfig SCM_P ((char *s1, char *s2, int s));
182
0f2d19dd
JB
183static void
184fixconfig (s1, s2, s)
185 char *s1;
186 char *s2;
187 int s;
0f2d19dd
JB
188{
189 fputs (s1, stderr);
190 fputs (s2, stderr);
191 fputs ("\nin ", stderr);
192 fputs (s ? "setjump" : "scmfig", stderr);
193 fputs (".h and recompile scm\n", stderr);
194 exit (1);
195}
196
197
1cc91f1b 198static void check_config SCM_P ((void));
0f2d19dd
JB
199
200static void
201check_config ()
202{
203 scm_sizet j;
204
205 j = HEAP_SEG_SIZE;
206 if (HEAP_SEG_SIZE != j)
207 fixconfig ("reduce", "size of HEAP_SEG_SIZE", 0);
208
209#ifdef SCM_SINGLES
210 if (sizeof (float) != sizeof (long))
211 fixconfig (remsg, "SCM_SINGLES", 0);
212#endif /* def SCM_SINGLES */
213
214
215#ifdef SCM_BIGDIG
216 if (2 * SCM_BITSPERDIG / SCM_CHAR_BIT > sizeof (long))
217 fixconfig (remsg, "SCM_BIGDIG", 0);
218#ifndef SCM_DIGSTOOBIG
219 if (SCM_DIGSPERLONG * sizeof (SCM_BIGDIG) > sizeof (long))
220 fixconfig (addmsg, "SCM_DIGSTOOBIG", 0);
221#endif
222#endif
223
224#ifdef SCM_STACK_GROWS_UP
225 if (((SCM_STACKITEM *) & j - stack_start_ptr) < 0)
226 fixconfig (remsg, "SCM_STACK_GROWS_UP", 1);
227#else
228 if ((stack_start_ptr - (SCM_STACKITEM *) & j) < 0)
229 fixconfig (addmsg, "SCM_STACK_GROWS_UP", 1);
230#endif
231}
232#endif
233
234
235\f
1cdaaafb
JB
236/* initializing standard and current I/O ports */
237
238/* Create standard ports from stdio stdin, stdout, and stderr. */
239static void
240scm_init_standard_ports ()
241{
242 /* I'm not sure why this should be unbuffered when coming from a
243 tty; isn't line buffering more common? */
244 scm_def_inp = scm_stdio_to_port (stdin,
245 (isatty (fileno (stdin)) ? "r0" : "r"),
246 "standard input");
247 scm_def_outp = scm_stdio_to_port (stdout, "w", "standard output");
248 scm_def_errp = scm_stdio_to_port (stderr, "w", "standard error");
249
250 scm_cur_inp = scm_def_inp;
251 scm_cur_outp = scm_def_outp;
252 scm_cur_errp = scm_def_errp;
253}
254
255
256\f
0f2d19dd
JB
257#ifdef _UNICOS
258typedef int setjmp_type;
259#else
260typedef long setjmp_type;
261#endif
262
1cdaaafb
JB
263static void scm_boot_guile_1 SCM_P ((SCM_STACKITEM *base,
264 int argc, char **argv,
265 void (*main_func) (void *closure,
266 int argc,
267 char **argv),
268 void *closure));
269
270
271/* Fire up the Guile Scheme interpreter.
272
273 Call MAIN_FUNC, passing it CLOSURE, ARGC, and ARGV. MAIN_FUNC
274 should do all the work of the program (initializing other packages,
275 reading user input, etc.) before returning. When MAIN_FUNC
276 returns, call exit (0); this function never returns. If you want
277 some other exit value, MAIN_FUNC may call exit itself.
0f2d19dd 278
1cdaaafb
JB
279 scm_boot_guile arranges for program-arguments to return the strings
280 given by ARGC and ARGV. If MAIN_FUNC modifies ARGC/ARGV, should
281 call scm_set_program_arguments with the final list, so Scheme code
282 will know which arguments have been processed.
c275ddc7 283
1cdaaafb
JB
284 Why must the caller do all the real work from MAIN_FUNC? The
285 garbage collector assumes that all local variables of type SCM will
286 be above scm_boot_guile's stack frame on the stack. If you try to
287 manipulate SCM values after this function returns, it's the luck of
288 the draw whether the GC will be able to find the objects you
289 allocate. So, scm_boot_guile function exits, rather than
290 returning, to discourage people from making that mistake. */
291
292
293void
294scm_boot_guile (argc, argv, main_func, closure)
0f2d19dd
JB
295 int argc;
296 char ** argv;
1cdaaafb
JB
297 void (*main_func) ();
298 void *closure;
c275ddc7 299{
1cdaaafb
JB
300 /* The garbage collector uses the address of this variable as one
301 end of the stack, and the address of one of its own local
302 variables as the other end. */
c275ddc7
JB
303 SCM_STACKITEM dummy;
304
1cdaaafb 305 return scm_boot_guile_1 (&dummy, argc, argv, main_func, closure);
c275ddc7
JB
306}
307
1cdaaafb
JB
308
309static void
310scm_boot_guile_1 (base, argc, argv, main_func, closure)
c275ddc7 311 SCM_STACKITEM *base;
c275ddc7 312 int argc;
1cdaaafb
JB
313 char **argv;
314 void (*main_func) ();
315 void *closure;
0f2d19dd
JB
316{
317 static int initialized = 0;
318 static int live = 0;
0f2d19dd 319 setjmp_type setjmp_val;
0f2d19dd 320
1cdaaafb
JB
321 /* This function is not re-entrant. */
322 if (live)
323 abort ();
0f2d19dd
JB
324
325 live = 1;
326
327 scm_ints_disabled = 1;
328 scm_block_gc = 1;
329
330 if (initialized)
331 {
c275ddc7 332 scm_restart_stack (base);
0f2d19dd
JB
333 }
334 else
335 {
336 scm_ports_prehistory ();
337 scm_smob_prehistory ();
338 scm_tables_prehistory ();
339 scm_init_storage (0);
9ef3d0ee
MD
340 scm_init_root ();
341#ifdef USE_THREADS
a239e35b 342 scm_init_threads (base);
9ef3d0ee 343#endif
1cdaaafb 344 scm_start_stack (base);
0f2d19dd
JB
345 scm_init_gsubr ();
346 scm_init_feature ();
347 scm_init_alist ();
348 scm_init_append ();
349 scm_init_arbiters ();
350 scm_init_async ();
0e44fcca 351 scm_init_backtrace ();
0f2d19dd
JB
352 scm_init_boolean ();
353 scm_init_chars ();
354 scm_init_continuations ();
355 scm_init_dynwind ();
356 scm_init_eq ();
357 scm_init_error ();
358 scm_init_fdsocket ();
359 scm_init_fports ();
0f2d19dd
JB
360 scm_init_filesys ();
361 scm_init_gc ();
68cd9c71 362 scm_init_gdbint ();
0f2d19dd
JB
363 scm_init_hash ();
364 scm_init_hashtab ();
365 scm_init_ioext ();
366 scm_init_kw ();
367 scm_init_list ();
368 scm_init_mallocs ();
369 scm_init_numbers ();
370 scm_init_objprop ();
4e1caa79
MD
371#if DEBUG_EXTENSIONS
372 /* Excluding this until it's really needed makes the binary
373 * smaller after linking. */
374 scm_init_options ();
375#endif
0f2d19dd
JB
376 scm_init_pairs ();
377 scm_init_ports ();
378 scm_init_posix ();
379 scm_init_procs ();
380 scm_init_procprop ();
0f2d19dd
JB
381 scm_init_scmsigs ();
382 scm_init_socket ();
4e1caa79
MD
383#ifdef DEBUG_EXTENSIONS
384 scm_init_srcprop ();
385#endif
0f2d19dd 386 scm_init_stackchk ();
7439c0b9 387 scm_init_struct (); /* Requires struct */
0e44fcca 388 scm_init_stacks ();
0f2d19dd 389 scm_init_strports ();
0f2d19dd
JB
390 scm_init_symbols ();
391 scm_init_tag ();
392 scm_init_load ();
90b826c9 393 scm_init_print (); /* Requires struct */
0f2d19dd
JB
394 scm_init_read ();
395 scm_init_sequences ();
396 scm_init_stime ();
397 scm_init_strings ();
398 scm_init_strorder ();
399 scm_init_mbstrings ();
400 scm_init_strop ();
401 scm_init_throw ();
402 scm_init_variable ();
403 scm_init_vectors ();
9d7e1edf 404 scm_init_version ();
0f2d19dd
JB
405 scm_init_weaks ();
406 scm_init_vports ();
407 scm_init_eval ();
0e44fcca
MD
408#ifdef DEBUG_EXTENSIONS
409 scm_init_debug (); /* Requires macro smobs */
410#endif
0f2d19dd
JB
411 scm_init_ramap ();
412 scm_init_unif ();
413 scm_init_simpos ();
24f93c27 414 scm_init_load_path ();
1cdaaafb 415 scm_init_standard_ports ();
0f2d19dd
JB
416 initialized = 1;
417 }
418
419 scm_block_gc = 0; /* permit the gc to run */
420 /* ints still disabled */
421
1cdaaafb
JB
422#ifdef STACK_CHECKING
423 scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
424#endif
0f2d19dd 425
1cdaaafb
JB
426 setjmp_val = setjmp (SCM_JMPBUF (scm_rootcont));
427 if (!setjmp_val)
428 {
429 scm_init_signals ();
0f2d19dd 430
f29de790 431 scm_set_program_arguments (argc, argv, 0);
1cdaaafb
JB
432 (*main_func) (closure, argc, argv);
433 }
0f2d19dd 434
1cdaaafb 435 scm_restore_signals ();
0f2d19dd 436
1cdaaafb
JB
437 /* This tick gives any pending
438 * asyncs a chance to run. This must be done after
439 * the call to scm_restore_signals.
440 */
441 SCM_ASYNC_TICK;
442
443 /* If the caller doesn't want this, they should return from
444 main_func themselves. */
445 exit (0);
0f2d19dd 446}