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