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