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