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