* aclocal.m4: Fixes for building with coop threads in a
[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
70122cd3 58#include "dynl.h"
20e6290e
JB
59#include "dynwind.h"
60#include "eq.h"
61#include "error.h"
62#include "eval.h"
20e6290e 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"
370312ae 77#include "net_db.h"
20e6290e
JB
78#include "numbers.h"
79#include "objprop.h"
80#include "options.h"
81#include "pairs.h"
82#include "ports.h"
83#include "posix.h"
84#include "print.h"
85#include "procprop.h"
86#include "procs.h"
87#include "ramap.h"
88#include "read.h"
89#include "scmsigs.h"
964edde7 90#include "script.h"
20e6290e
JB
91#include "sequences.h"
92#include "simpos.h"
a8be22fe 93#include "smob.h"
20e6290e
JB
94#include "socket.h"
95#include "srcprop.h"
96#include "stackchk.h"
0e44fcca 97#include "stacks.h"
20e6290e
JB
98#include "stime.h"
99#include "strings.h"
100#include "strop.h"
101#include "strorder.h"
102#include "strports.h"
103#include "struct.h"
104#include "symbols.h"
105#include "tag.h"
106#include "throw.h"
107#include "unif.h"
108#include "variable.h"
109#include "vectors.h"
110#include "version.h"
111#include "vports.h"
112#include "weaks.h"
113
a8be22fe
JB
114#include "init.h"
115
95b88819
GH
116#ifdef HAVE_STRING_H
117#include <string.h>
118#endif
119#ifdef HAVE_UNISTD_H
120#include <unistd.h>
121#endif
0f2d19dd
JB
122\f
123
1cdaaafb
JB
124static void scm_start_stack SCM_P ((void *base));
125static void scm_restart_stack SCM_P ((void * base));
126
127static void
128scm_start_stack (base)
0f2d19dd 129 void * base;
0f2d19dd 130{
9ef3d0ee 131 SCM root;
0f2d19dd 132
9ef3d0ee
MD
133 root = scm_permanent_object (scm_make_root (SCM_UNDEFINED));
134 scm_set_root (SCM_ROOT_STATE (root));
0f2d19dd
JB
135 scm_stack_base = base;
136
0f2d19dd
JB
137 scm_exitval = SCM_BOOL_F; /* vestigial */
138
dc19d1d2 139 scm_top_level_lookup_closure_var = SCM_BOOL_F;
0f2d19dd
JB
140 scm_system_transformer = SCM_BOOL_F;
141
5aab5d96
MD
142 scm_the_last_stack_var = SCM_BOOL_F;
143
0f2d19dd
JB
144 /* Create an object to hold the root continuation.
145 */
146 SCM_NEWCELL (scm_rootcont);
1cdaaafb
JB
147 SCM_SETJMPBUF (scm_rootcont, scm_must_malloc ((long) sizeof (scm_contregs),
148 "continuation"));
898a256f 149 SCM_SETCAR (scm_rootcont, scm_tc7_contin);
4b5166ac 150 SCM_SEQ (scm_rootcont) = 0;
0f2d19dd
JB
151 /* The root continuation if further initialized by scm_restart_stack. */
152
153 /* Create the look-aside stack for variables that are shared between
154 * captured continuations.
155 */
1cdaaafb
JB
156 scm_continuation_stack = scm_make_vector (SCM_MAKINUM (512),
157 SCM_UNDEFINED, SCM_UNDEFINED);
0f2d19dd
JB
158 /* The continuation stack is further initialized by scm_restart_stack. */
159
1cdaaafb
JB
160 /* The remainder of stack initialization is factored out to another
161 * function so that if this stack is ever exitted, it can be
162 * re-entered using scm_restart_stack. */
0f2d19dd
JB
163 scm_restart_stack (base);
164}
165
166
1cdaaafb 167static void
0f2d19dd
JB
168scm_restart_stack (base)
169 void * base;
0f2d19dd
JB
170{
171 scm_dynwinds = SCM_EOL;
172 SCM_DYNENV (scm_rootcont) = SCM_EOL;
173 SCM_THROW_VALUE (scm_rootcont) = SCM_EOL;
4e1caa79 174#ifdef DEBUG_EXTENSIONS
4b5166ac 175 SCM_DFRAME (scm_rootcont) = scm_last_debug_frame = 0;
4e1caa79 176#endif
0f2d19dd
JB
177 SCM_BASE (scm_rootcont) = base;
178 scm_continuation_stack_ptr = SCM_MAKINUM (0);
179}
180
181#if 0
182static char remsg[] = "remove\n#define ", addmsg[] = "add\n#define ";
183
1cc91f1b
JB
184
185static void fixconfig SCM_P ((char *s1, char *s2, int s));
186
0f2d19dd
JB
187static void
188fixconfig (s1, s2, s)
189 char *s1;
190 char *s2;
191 int s;
0f2d19dd
JB
192{
193 fputs (s1, stderr);
194 fputs (s2, stderr);
195 fputs ("\nin ", stderr);
196 fputs (s ? "setjump" : "scmfig", stderr);
197 fputs (".h and recompile scm\n", stderr);
198 exit (1);
199}
200
201
1cc91f1b 202static void check_config SCM_P ((void));
0f2d19dd
JB
203
204static void
205check_config ()
206{
207 scm_sizet j;
208
209 j = HEAP_SEG_SIZE;
210 if (HEAP_SEG_SIZE != j)
211 fixconfig ("reduce", "size of HEAP_SEG_SIZE", 0);
212
213#ifdef SCM_SINGLES
214 if (sizeof (float) != sizeof (long))
215 fixconfig (remsg, "SCM_SINGLES", 0);
216#endif /* def SCM_SINGLES */
217
218
219#ifdef SCM_BIGDIG
220 if (2 * SCM_BITSPERDIG / SCM_CHAR_BIT > sizeof (long))
221 fixconfig (remsg, "SCM_BIGDIG", 0);
222#ifndef SCM_DIGSTOOBIG
223 if (SCM_DIGSPERLONG * sizeof (SCM_BIGDIG) > sizeof (long))
224 fixconfig (addmsg, "SCM_DIGSTOOBIG", 0);
225#endif
226#endif
227
228#ifdef SCM_STACK_GROWS_UP
229 if (((SCM_STACKITEM *) & j - stack_start_ptr) < 0)
230 fixconfig (remsg, "SCM_STACK_GROWS_UP", 1);
231#else
232 if ((stack_start_ptr - (SCM_STACKITEM *) & j) < 0)
233 fixconfig (addmsg, "SCM_STACK_GROWS_UP", 1);
234#endif
235}
236#endif
237
238
239\f
1cdaaafb
JB
240/* initializing standard and current I/O ports */
241
242/* Create standard ports from stdio stdin, stdout, and stderr. */
243static void
244scm_init_standard_ports ()
245{
246 /* I'm not sure why this should be unbuffered when coming from a
247 tty; isn't line buffering more common? */
248 scm_def_inp = scm_stdio_to_port (stdin,
249 (isatty (fileno (stdin)) ? "r0" : "r"),
250 "standard input");
251 scm_def_outp = scm_stdio_to_port (stdout, "w", "standard output");
252 scm_def_errp = scm_stdio_to_port (stderr, "w", "standard error");
253
254 scm_cur_inp = scm_def_inp;
255 scm_cur_outp = scm_def_outp;
256 scm_cur_errp = scm_def_errp;
257}
258
259
260\f
0f2d19dd
JB
261#ifdef _UNICOS
262typedef int setjmp_type;
263#else
264typedef long setjmp_type;
265#endif
266
816a6f06
JB
267/* All the data needed to invoke the main function. */
268struct main_func_closure
269{
270 /* the function to call */
271 void (*main_func) SCM_P ((void *closure, int argc, char **argv));
272 void *closure; /* dummy data to pass it */
273 int argc;
274 char **argv; /* the argument list it should receive */
275};
276
277
278static void scm_boot_guile_1 SCM_P ((SCM_STACKITEM *base,
279 struct main_func_closure *closure));
280static SCM invoke_main_func SCM_P ((void *body_data, SCM jmpbuf));
1cdaaafb
JB
281
282
283/* Fire up the Guile Scheme interpreter.
284
285 Call MAIN_FUNC, passing it CLOSURE, ARGC, and ARGV. MAIN_FUNC
286 should do all the work of the program (initializing other packages,
287 reading user input, etc.) before returning. When MAIN_FUNC
288 returns, call exit (0); this function never returns. If you want
289 some other exit value, MAIN_FUNC may call exit itself.
0f2d19dd 290
1cdaaafb
JB
291 scm_boot_guile arranges for program-arguments to return the strings
292 given by ARGC and ARGV. If MAIN_FUNC modifies ARGC/ARGV, should
293 call scm_set_program_arguments with the final list, so Scheme code
294 will know which arguments have been processed.
c275ddc7 295
816a6f06
JB
296 scm_boot_guile establishes a catch-all catch handler which prints
297 an error message and exits the process. This means that Guile
298 exits in a coherent way when system errors occur and the user isn't
299 prepared to handle it. If the user doesn't like this behavior,
300 they can establish their own universal catcher to shadow this one.
301
1cdaaafb
JB
302 Why must the caller do all the real work from MAIN_FUNC? The
303 garbage collector assumes that all local variables of type SCM will
304 be above scm_boot_guile's stack frame on the stack. If you try to
305 manipulate SCM values after this function returns, it's the luck of
306 the draw whether the GC will be able to find the objects you
307 allocate. So, scm_boot_guile function exits, rather than
308 returning, to discourage people from making that mistake. */
309
310
311void
312scm_boot_guile (argc, argv, main_func, closure)
0f2d19dd
JB
313 int argc;
314 char ** argv;
1cdaaafb
JB
315 void (*main_func) ();
316 void *closure;
c275ddc7 317{
1cdaaafb
JB
318 /* The garbage collector uses the address of this variable as one
319 end of the stack, and the address of one of its own local
320 variables as the other end. */
c275ddc7 321 SCM_STACKITEM dummy;
816a6f06 322 struct main_func_closure c;
c275ddc7 323
816a6f06
JB
324 c.main_func = main_func;
325 c.closure = closure;
326 c.argc = argc;
327 c.argv = argv;
328
1595aa56 329 scm_boot_guile_1 (&dummy, &c);
c275ddc7
JB
330}
331
816a6f06 332
70122cd3
MV
333/* Record here whether SCM_BOOT_GUILE_1 has already been called. This
334 variable is now here and not inside SCM_BOOT_GUILE_1 so that one
335 can tweak it. This is necessary for unexec to work. (Hey, "1-live"
336 is the name of a local radiostation...) */
337
338int scm_boot_guile_1_live = 0;
1cdaaafb
JB
339
340static void
816a6f06 341scm_boot_guile_1 (base, closure)
c275ddc7 342 SCM_STACKITEM *base;
816a6f06 343 struct main_func_closure *closure;
0f2d19dd
JB
344{
345 static int initialized = 0;
70122cd3 346 /* static int live = 0; */
0f2d19dd 347 setjmp_type setjmp_val;
0f2d19dd 348
1cdaaafb 349 /* This function is not re-entrant. */
70122cd3 350 if (scm_boot_guile_1_live)
1cdaaafb 351 abort ();
0f2d19dd 352
70122cd3 353 scm_boot_guile_1_live = 1;
0f2d19dd
JB
354
355 scm_ints_disabled = 1;
356 scm_block_gc = 1;
357
358 if (initialized)
359 {
c275ddc7 360 scm_restart_stack (base);
0f2d19dd
JB
361 }
362 else
363 {
364 scm_ports_prehistory ();
365 scm_smob_prehistory ();
366 scm_tables_prehistory ();
367 scm_init_storage (0);
9ef3d0ee
MD
368 scm_init_root ();
369#ifdef USE_THREADS
a239e35b 370 scm_init_threads (base);
9ef3d0ee 371#endif
1cdaaafb 372 scm_start_stack (base);
0f2d19dd
JB
373 scm_init_gsubr ();
374 scm_init_feature ();
375 scm_init_alist ();
376 scm_init_append ();
377 scm_init_arbiters ();
378 scm_init_async ();
0e44fcca 379 scm_init_backtrace ();
0f2d19dd
JB
380 scm_init_boolean ();
381 scm_init_chars ();
382 scm_init_continuations ();
383 scm_init_dynwind ();
384 scm_init_eq ();
385 scm_init_error ();
0f2d19dd 386 scm_init_fports ();
0f2d19dd
JB
387 scm_init_filesys ();
388 scm_init_gc ();
68cd9c71 389 scm_init_gdbint ();
0f2d19dd
JB
390 scm_init_hash ();
391 scm_init_hashtab ();
392 scm_init_ioext ();
393 scm_init_kw ();
394 scm_init_list ();
395 scm_init_mallocs ();
370312ae 396 scm_init_net_db ();
0f2d19dd
JB
397 scm_init_numbers ();
398 scm_init_objprop ();
4e1caa79
MD
399#if DEBUG_EXTENSIONS
400 /* Excluding this until it's really needed makes the binary
401 * smaller after linking. */
402 scm_init_options ();
403#endif
0f2d19dd
JB
404 scm_init_pairs ();
405 scm_init_ports ();
406 scm_init_posix ();
407 scm_init_procs ();
408 scm_init_procprop ();
0f2d19dd
JB
409 scm_init_scmsigs ();
410 scm_init_socket ();
4e1caa79
MD
411#ifdef DEBUG_EXTENSIONS
412 scm_init_srcprop ();
413#endif
0f2d19dd 414 scm_init_stackchk ();
7439c0b9 415 scm_init_struct (); /* Requires struct */
0e44fcca 416 scm_init_stacks ();
0f2d19dd 417 scm_init_strports ();
0f2d19dd
JB
418 scm_init_symbols ();
419 scm_init_tag ();
420 scm_init_load ();
90b826c9 421 scm_init_print (); /* Requires struct */
0f2d19dd
JB
422 scm_init_read ();
423 scm_init_sequences ();
424 scm_init_stime ();
425 scm_init_strings ();
426 scm_init_strorder ();
427 scm_init_mbstrings ();
428 scm_init_strop ();
429 scm_init_throw ();
430 scm_init_variable ();
431 scm_init_vectors ();
9d7e1edf 432 scm_init_version ();
0f2d19dd
JB
433 scm_init_weaks ();
434 scm_init_vports ();
435 scm_init_eval ();
0e44fcca
MD
436#ifdef DEBUG_EXTENSIONS
437 scm_init_debug (); /* Requires macro smobs */
438#endif
0f2d19dd
JB
439 scm_init_ramap ();
440 scm_init_unif ();
441 scm_init_simpos ();
24f93c27 442 scm_init_load_path ();
1cdaaafb 443 scm_init_standard_ports ();
70122cd3 444 scm_init_dynamic_linking ();
0487b82f 445 scm_init_script ();
0f2d19dd
JB
446 initialized = 1;
447 }
448
449 scm_block_gc = 0; /* permit the gc to run */
450 /* ints still disabled */
451
1cdaaafb
JB
452#ifdef STACK_CHECKING
453 scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
454#endif
0f2d19dd 455
1cdaaafb
JB
456 setjmp_val = setjmp (SCM_JMPBUF (scm_rootcont));
457 if (!setjmp_val)
458 {
459 scm_init_signals ();
0f2d19dd 460
816a6f06
JB
461 scm_set_program_arguments (closure->argc, closure->argv, 0);
462 scm_internal_catch (SCM_BOOL_T, invoke_main_func, closure,
463 scm_handle_by_message, 0);
1cdaaafb 464 }
0f2d19dd 465
1cdaaafb 466 scm_restore_signals ();
0f2d19dd 467
1cdaaafb
JB
468 /* This tick gives any pending
469 * asyncs a chance to run. This must be done after
470 * the call to scm_restore_signals.
471 */
472 SCM_ASYNC_TICK;
473
474 /* If the caller doesn't want this, they should return from
475 main_func themselves. */
476 exit (0);
0f2d19dd 477}
816a6f06
JB
478
479
480static SCM
481invoke_main_func (body_data, jmpbuf)
482 void *body_data;
483 SCM jmpbuf;
484{
485 struct main_func_closure *closure = (struct main_func_closure *) body_data;
486
487 (*closure->main_func) (closure->closure, closure->argc, closure->argv);
488
489 /* never reached */
490 return SCM_UNDEFINED;
491}