init.c 18 KB

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