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