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