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