1 /* Copyright (C) 1995,1996,1997 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, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
46 /* Everybody has an init function. */
50 #include "backtrace.h"
53 #include "continuations.h"
54 #ifdef DEBUG_EXTENSIONS
88 #include "regex-posix.h"
102 #include "stackchk.h"
107 #include "strorder.h"
108 #include "strports.h"
114 #include "variable.h"
130 static void start_stack
SCM_P ((void *base
));
131 static void restart_stack
SCM_P ((void * base
));
139 root
= scm_permanent_object (scm_make_root (SCM_UNDEFINED
));
140 scm_set_root (SCM_ROOT_STATE (root
));
141 scm_stack_base
= base
;
143 scm_exitval
= SCM_BOOL_F
; /* vestigial */
145 scm_top_level_lookup_closure_var
= SCM_BOOL_F
;
146 scm_system_transformer
= SCM_BOOL_F
;
148 scm_root
->fluids
= scm_make_initial_fluids ();
150 /* Create an object to hold the root continuation.
152 SCM_NEWCELL (scm_rootcont
);
153 SCM_SETJMPBUF (scm_rootcont
, scm_must_malloc ((long) sizeof (scm_contregs
),
155 SCM_SETCAR (scm_rootcont
, scm_tc7_contin
);
156 SCM_SEQ (scm_rootcont
) = 0;
157 /* The root continuation if further initialized by restart_stack. */
159 /* Create the look-aside stack for variables that are shared between
160 * captured continuations.
162 scm_continuation_stack
= scm_make_vector (SCM_MAKINUM (512),
163 SCM_UNDEFINED
, SCM_UNDEFINED
);
164 /* The continuation stack is further initialized by restart_stack. */
166 /* The remainder of stack initialization is factored out to another
167 * function so that if this stack is ever exitted, it can be
168 * re-entered using restart_stack. */
169 restart_stack (base
);
177 scm_dynwinds
= SCM_EOL
;
178 SCM_DYNENV (scm_rootcont
) = SCM_EOL
;
179 SCM_THROW_VALUE (scm_rootcont
) = SCM_EOL
;
180 #ifdef DEBUG_EXTENSIONS
181 SCM_DFRAME (scm_rootcont
) = scm_last_debug_frame
= 0;
183 SCM_BASE (scm_rootcont
) = base
;
184 scm_continuation_stack_ptr
= SCM_MAKINUM (0);
188 static char remsg
[] = "remove\n#define ", addmsg
[] = "add\n#define ";
191 static void fixconfig
SCM_P ((char *s1
, char *s2
, int s
));
194 fixconfig (s1
, s2
, s
)
201 fputs ("\nin ", stderr
);
202 fputs (s
? "setjump" : "scmfig", stderr
);
203 fputs (".h and recompile scm\n", stderr
);
208 static void check_config
SCM_P ((void));
216 if (HEAP_SEG_SIZE
!= j
)
217 fixconfig ("reduce", "size of HEAP_SEG_SIZE", 0);
220 if (sizeof (float) != sizeof (long))
221 fixconfig (remsg
, "SCM_SINGLES", 0);
222 #endif /* def SCM_SINGLES */
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);
234 #ifdef SCM_STACK_GROWS_UP
235 if (((SCM_STACKITEM
*) & j
- stack_start_ptr
) < 0)
236 fixconfig (remsg
, "SCM_STACK_GROWS_UP", 1);
238 if ((stack_start_ptr
- (SCM_STACKITEM
*) & j
) < 0)
239 fixconfig (addmsg
, "SCM_STACK_GROWS_UP", 1);
246 /* initializing standard and current I/O ports */
248 /* Create standard ports from stdio stdin, stdout, and stderr. */
250 scm_init_standard_ports ()
252 /* From the SCSH manual:
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.
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. */
265 scm_def_inp
= scm_stdio_to_port (stdin
,
266 (isatty (fileno (stdin
)) ? "r0" : "r"),
268 scm_def_outp
= scm_stdio_to_port (stdout
, "w", "standard output");
269 scm_def_errp
= scm_stdio_to_port (stderr
, "w", "standard error");
271 scm_cur_inp
= scm_def_inp
;
272 scm_cur_outp
= scm_def_outp
;
273 scm_cur_errp
= scm_def_errp
;
274 scm_cur_loadp
= SCM_BOOL_F
;
280 typedef int setjmp_type
;
282 typedef long setjmp_type
;
285 /* All the data needed to invoke the main function. */
286 struct main_func_closure
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 */
292 char **argv
; /* the argument list it should receive */
296 static void scm_boot_guile_1
SCM_P ((SCM_STACKITEM
*base
,
297 struct main_func_closure
*closure
));
298 static SCM invoke_main_func
SCM_P ((void *body_data
));
301 /* Fire up the Guile Scheme interpreter.
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.
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.
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.
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. */
330 scm_boot_guile (argc
, argv
, main_func
, closure
)
333 void (*main_func
) ();
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. */
340 struct main_func_closure c
;
342 c
.main_func
= main_func
;
347 scm_boot_guile_1 (&dummy
, &c
);
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...) */
356 int scm_boot_guile_1_live
= 0;
359 scm_boot_guile_1 (base
, closure
)
361 struct main_func_closure
*closure
;
363 static int initialized
= 0;
364 /* static int live = 0; */
365 setjmp_type setjmp_val
;
367 /* This function is not re-entrant. */
368 if (scm_boot_guile_1_live
)
371 scm_boot_guile_1_live
= 1;
373 scm_ints_disabled
= 1;
378 restart_stack (base
);
382 scm_ports_prehistory ();
383 scm_smob_prehistory ();
384 scm_tables_prehistory ();
385 scm_init_storage (0);
388 scm_init_threads (base
);
394 scm_init_arbiters ();
398 scm_init_continuations ();
403 scm_init_backtrace (); /* Requires fluids */
425 scm_init_regex_posix ();
428 scm_init_procprop ();
431 #ifdef DEBUG_EXTENSIONS
434 scm_init_stackchk ();
435 scm_init_struct (); /* Requires struct */
437 scm_init_strports ();
441 scm_init_objects (); /* Requires struct */
442 scm_init_print (); /* Requires struct */
446 scm_init_strorder ();
449 scm_init_variable ();
455 #ifdef DEBUG_EXTENSIONS
456 scm_init_debug (); /* Requires macro smobs */
461 scm_init_load_path ();
462 #ifdef HAVE_RL_GETC_FUNCTION
463 scm_init_readline ();
465 scm_init_standard_ports ();
466 scm_init_dynamic_linking ();
471 scm_block_gc
= 0; /* permit the gc to run */
472 /* ints still disabled */
474 #ifdef STACK_CHECKING
475 scm_stack_checking_enabled_p
= SCM_STACK_CHECKING_P
;
478 setjmp_val
= setjmp (SCM_JMPBUF (scm_rootcont
));
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);
486 scm_restore_signals ();
488 /* This tick gives any pending
489 * asyncs a chance to run. This must be done after
490 * the call to scm_restore_signals.
494 /* If the caller doesn't want this, they should return from
495 main_func themselves. */
501 invoke_main_func (body_data
)
504 struct main_func_closure
*closure
= (struct main_func_closure
*) body_data
;
506 (*closure
->main_func
) (closure
->closure
, closure
->argc
, closure
->argv
);
509 return SCM_UNDEFINED
;