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