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