1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
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)
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.
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.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
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.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
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.
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.
46 /* Everybody has an init function. */
51 #include "backtrace.h"
54 #include "continuations.h"
55 #ifdef DEBUG_EXTENSIONS
76 #include "mbstrings.h"
90 #include "sequences.h"
100 #include "strorder.h"
101 #include "strports.h"
107 #include "variable.h"
123 static void scm_start_stack
SCM_P ((void *base
));
124 static void scm_restart_stack
SCM_P ((void * base
));
127 scm_start_stack (base
)
132 root
= scm_permanent_object (scm_make_root (SCM_UNDEFINED
));
133 scm_set_root (SCM_ROOT_STATE (root
));
134 scm_stack_base
= base
;
136 scm_exitval
= SCM_BOOL_F
; /* vestigial */
138 scm_top_level_lookup_closure_var
= SCM_BOOL_F
;
139 scm_system_transformer
= SCM_BOOL_F
;
141 scm_the_last_stack_var
= SCM_BOOL_F
;
143 /* Create an object to hold the root continuation.
145 SCM_NEWCELL (scm_rootcont
);
146 SCM_SETJMPBUF (scm_rootcont
, scm_must_malloc ((long) sizeof (scm_contregs
),
148 SCM_SETCAR (scm_rootcont
, scm_tc7_contin
);
149 SCM_SEQ (scm_rootcont
) = 0;
150 /* The root continuation if further initialized by scm_restart_stack. */
152 /* Create the look-aside stack for variables that are shared between
153 * captured continuations.
155 scm_continuation_stack
= scm_make_vector (SCM_MAKINUM (512),
156 SCM_UNDEFINED
, SCM_UNDEFINED
);
157 /* The continuation stack is further initialized by scm_restart_stack. */
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. */
162 scm_restart_stack (base
);
167 scm_restart_stack (base
)
170 scm_dynwinds
= SCM_EOL
;
171 SCM_DYNENV (scm_rootcont
) = SCM_EOL
;
172 SCM_THROW_VALUE (scm_rootcont
) = SCM_EOL
;
173 #ifdef DEBUG_EXTENSIONS
174 SCM_DFRAME (scm_rootcont
) = scm_last_debug_frame
= 0;
176 SCM_BASE (scm_rootcont
) = base
;
177 scm_continuation_stack_ptr
= SCM_MAKINUM (0);
181 static char remsg
[] = "remove\n#define ", addmsg
[] = "add\n#define ";
184 static void fixconfig
SCM_P ((char *s1
, char *s2
, int s
));
187 fixconfig (s1
, s2
, s
)
194 fputs ("\nin ", stderr
);
195 fputs (s
? "setjump" : "scmfig", stderr
);
196 fputs (".h and recompile scm\n", stderr
);
201 static void check_config
SCM_P ((void));
209 if (HEAP_SEG_SIZE
!= j
)
210 fixconfig ("reduce", "size of HEAP_SEG_SIZE", 0);
213 if (sizeof (float) != sizeof (long))
214 fixconfig (remsg
, "SCM_SINGLES", 0);
215 #endif /* def SCM_SINGLES */
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);
227 #ifdef SCM_STACK_GROWS_UP
228 if (((SCM_STACKITEM
*) & j
- stack_start_ptr
) < 0)
229 fixconfig (remsg
, "SCM_STACK_GROWS_UP", 1);
231 if ((stack_start_ptr
- (SCM_STACKITEM
*) & j
) < 0)
232 fixconfig (addmsg
, "SCM_STACK_GROWS_UP", 1);
239 /* initializing standard and current I/O ports */
241 /* Create standard ports from stdio stdin, stdout, and stderr. */
243 scm_init_standard_ports ()
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"),
250 scm_def_outp
= scm_stdio_to_port (stdout
, "w", "standard output");
251 scm_def_errp
= scm_stdio_to_port (stderr
, "w", "standard error");
253 scm_cur_inp
= scm_def_inp
;
254 scm_cur_outp
= scm_def_outp
;
255 scm_cur_errp
= scm_def_errp
;
261 typedef int setjmp_type
;
263 typedef long setjmp_type
;
266 /* All the data needed to invoke the main function. */
267 struct main_func_closure
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 */
273 char **argv
; /* the argument list it should receive */
277 static void scm_boot_guile_1
SCM_P ((SCM_STACKITEM
*base
,
278 struct main_func_closure
*closure
));
279 static SCM invoke_main_func
SCM_P ((void *body_data
, SCM jmpbuf
));
282 /* Fire up the Guile Scheme interpreter.
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.
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.
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.
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. */
311 scm_boot_guile (argc
, argv
, main_func
, closure
)
314 void (*main_func
) ();
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. */
321 struct main_func_closure c
;
323 c
.main_func
= main_func
;
328 scm_boot_guile_1 (&dummy
, &c
);
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...) */
337 int scm_boot_guile_1_live
= 0;
340 scm_boot_guile_1 (base
, closure
)
342 struct main_func_closure
*closure
;
344 static int initialized
= 0;
345 /* static int live = 0; */
346 setjmp_type setjmp_val
;
348 /* This function is not re-entrant. */
349 if (scm_boot_guile_1_live
)
352 scm_boot_guile_1_live
= 1;
354 scm_ints_disabled
= 1;
359 scm_restart_stack (base
);
363 scm_ports_prehistory ();
364 scm_smob_prehistory ();
365 scm_tables_prehistory ();
366 scm_init_storage (0);
369 scm_init_threads (base
);
371 scm_start_stack (base
);
376 scm_init_arbiters ();
378 scm_init_backtrace ();
381 scm_init_continuations ();
399 /* Excluding this until it's really needed makes the binary
400 * smaller after linking. */
407 scm_init_procprop ();
410 #ifdef DEBUG_EXTENSIONS
413 scm_init_stackchk ();
414 scm_init_struct (); /* Requires struct */
416 scm_init_strports ();
420 scm_init_print (); /* Requires struct */
422 scm_init_sequences ();
425 scm_init_strorder ();
426 scm_init_mbstrings ();
429 scm_init_variable ();
435 #ifdef DEBUG_EXTENSIONS
436 scm_init_debug (); /* Requires macro smobs */
441 scm_init_load_path ();
442 scm_init_standard_ports ();
443 scm_init_dynamic_linking ();
447 scm_block_gc
= 0; /* permit the gc to run */
448 /* ints still disabled */
450 #ifdef STACK_CHECKING
451 scm_stack_checking_enabled_p
= SCM_STACK_CHECKING_P
;
454 setjmp_val
= setjmp (SCM_JMPBUF (scm_rootcont
));
459 scm_set_program_arguments (closure
->argc
, closure
->argv
, 0);
460 scm_internal_catch (SCM_BOOL_T
, invoke_main_func
, closure
,
461 scm_handle_by_message
, 0);
464 scm_restore_signals ();
466 /* This tick gives any pending
467 * asyncs a chance to run. This must be done after
468 * the call to scm_restore_signals.
472 /* If the caller doesn't want this, they should return from
473 main_func themselves. */
479 invoke_main_func (body_data
, jmpbuf
)
483 struct main_func_closure
*closure
= (struct main_func_closure
*) body_data
;
485 (*closure
->main_func
) (closure
->closure
, closure
->argc
, closure
->argv
);
488 return SCM_UNDEFINED
;