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