init.c 18 KB

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