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