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