init.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /* Copyright (C) 1995-2004, 2006, 2009-2014 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. /* Include the headers for just about everything.
  19. We call all their initialization functions. */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <gmp.h>
  28. #include "libguile/_scm.h"
  29. /* Everybody has an init function. */
  30. #include "libguile/alist.h"
  31. #include "libguile/arbiters.h"
  32. #include "libguile/async.h"
  33. #include "libguile/backtrace.h"
  34. #include "libguile/bitvectors.h"
  35. #include "libguile/boolean.h"
  36. #include "libguile/bytevectors.h"
  37. #include "libguile/chars.h"
  38. #include "libguile/control.h"
  39. #include "libguile/continuations.h"
  40. #include "libguile/debug.h"
  41. #ifdef GUILE_DEBUG_MALLOC
  42. #include "libguile/debug-malloc.h"
  43. #endif
  44. #include "libguile/deprecation.h"
  45. #include "libguile/dynl.h"
  46. #include "libguile/dynwind.h"
  47. #include "libguile/eq.h"
  48. #include "libguile/error.h"
  49. #include "libguile/eval.h"
  50. #include "libguile/evalext.h"
  51. #include "libguile/expand.h"
  52. #include "libguile/feature.h"
  53. #include "libguile/filesys.h"
  54. #include "libguile/finalizers.h"
  55. #include "libguile/fluids.h"
  56. #include "libguile/fports.h"
  57. #include "libguile/frames.h"
  58. #include "libguile/gc.h"
  59. #include "libguile/generalized-arrays.h"
  60. #include "libguile/generalized-vectors.h"
  61. #include "libguile/goops.h"
  62. #include "libguile/gsubr.h"
  63. #include "libguile/hash.h"
  64. #include "libguile/hashtab.h"
  65. #include "libguile/hooks.h"
  66. #include "libguile/gettext.h"
  67. #include "libguile/i18n.h"
  68. #include "libguile/instructions.h"
  69. #include "libguile/ioext.h"
  70. #include "libguile/keywords.h"
  71. #include "libguile/list.h"
  72. #include "libguile/load.h"
  73. #include "libguile/macros.h"
  74. #include "libguile/mallocs.h"
  75. #include "libguile/memoize.h"
  76. #include "libguile/modules.h"
  77. #include "libguile/net_db.h"
  78. #include "libguile/numbers.h"
  79. #include "libguile/loader.h"
  80. #include "libguile/objprop.h"
  81. #include "libguile/options.h"
  82. #include "libguile/pairs.h"
  83. #include "libguile/poll.h"
  84. #include "libguile/ports.h"
  85. #include "libguile/posix.h"
  86. #ifdef ENABLE_REGEX
  87. #include "libguile/regex-posix.h"
  88. #endif
  89. #include "libguile/print.h"
  90. #include "libguile/procprop.h"
  91. #include "libguile/procs.h"
  92. #include "libguile/programs.h"
  93. #include "libguile/promises.h"
  94. #include "libguile/array-map.h"
  95. #include "libguile/random.h"
  96. #include "libguile/rdelim.h"
  97. #include "libguile/read.h"
  98. #include "libguile/rw.h"
  99. #include "libguile/scmsigs.h"
  100. #include "libguile/script.h"
  101. #include "libguile/simpos.h"
  102. #include "libguile/smob.h"
  103. #include "libguile/socket.h"
  104. #include "libguile/sort.h"
  105. #include "libguile/srcprop.h"
  106. #include "libguile/stackchk.h"
  107. #include "libguile/stacks.h"
  108. #include "libguile/stime.h"
  109. #include "libguile/strings.h"
  110. #include "libguile/srfi-1.h"
  111. #include "libguile/srfi-4.h"
  112. #include "libguile/srfi-13.h"
  113. #include "libguile/srfi-14.h"
  114. #include "libguile/srfi-60.h"
  115. #include "libguile/strorder.h"
  116. #include "libguile/strports.h"
  117. #include "libguile/struct.h"
  118. #include "libguile/symbols.h"
  119. #include "libguile/throw.h"
  120. #include "libguile/arrays.h"
  121. #include "libguile/trees.h"
  122. #include "libguile/unicode.h"
  123. #include "libguile/values.h"
  124. #include "libguile/variable.h"
  125. #include "libguile/vectors.h"
  126. #include "libguile/version.h"
  127. #include "libguile/vm.h"
  128. #include "libguile/vports.h"
  129. #include "libguile/guardians.h"
  130. #include "libguile/extensions.h"
  131. #include "libguile/uniform.h"
  132. #include "libguile/deprecated.h"
  133. #include "libguile/init.h"
  134. #include "libguile/private-options.h"
  135. #ifdef HAVE_STRING_H
  136. #include <string.h>
  137. #endif
  138. #include <unistd.h>
  139. /* initializing standard and current I/O ports */
  140. typedef struct
  141. {
  142. int fdes;
  143. char *mode;
  144. } stream_body_data;
  145. /* proc to be called in scope of exception handler stream_handler. */
  146. static SCM
  147. stream_body (void *data)
  148. {
  149. stream_body_data *body_data = (stream_body_data *) data;
  150. SCM port = scm_fdes_to_port (body_data->fdes, body_data->mode, SCM_BOOL_F);
  151. scm_set_port_revealed_x (port, SCM_INUM1);
  152. return port;
  153. }
  154. /* exception handler for stream_body. */
  155. static SCM
  156. stream_handler (void *data SCM_UNUSED,
  157. SCM tag SCM_UNUSED,
  158. SCM throw_args SCM_UNUSED)
  159. {
  160. return SCM_BOOL_F;
  161. }
  162. /* Convert a file descriptor to a port, using scm_fdes_to_port.
  163. - set the revealed count for FILE's file descriptor to 1, so
  164. that fdes won't be closed when the port object is GC'd.
  165. - catch exceptions: allow Guile to be able to start up even
  166. if it has been handed bogus stdin/stdout/stderr. replace the
  167. bad ports with void ports. */
  168. static SCM
  169. scm_standard_stream_to_port (int fdes, char *mode)
  170. {
  171. SCM port;
  172. stream_body_data body_data;
  173. body_data.fdes = fdes;
  174. body_data.mode = mode;
  175. port = scm_internal_catch (SCM_BOOL_T, stream_body, &body_data,
  176. stream_handler, NULL);
  177. if (scm_is_false (port))
  178. port = scm_void_port (mode);
  179. return port;
  180. }
  181. /* Create standard ports from stdin, stdout, and stderr. */
  182. static void
  183. scm_init_standard_ports ()
  184. {
  185. /* From the SCSH manual:
  186. It can be useful to turn I/O buffering off in some cases, for
  187. example when an I/O stream is to be shared by multiple
  188. subprocesses. For this reason, scsh allocates an unbuffered port
  189. for file descriptor 0 at start-up time.
  190. Because shells frequently share stdin with subprocesses, if the
  191. shell does buffered reads, it might ``steal'' input intended for
  192. a subprocess. For this reason, all shells, including sh, csh,
  193. and scsh, read stdin unbuffered. Applications that can tolerate
  194. buffered input on stdin can reset \ex{(current-input-port)} to
  195. block buffering for higher performance. */
  196. scm_set_current_input_port
  197. (scm_standard_stream_to_port (0, isatty (0) ? "r0" : "r"));
  198. scm_set_current_output_port
  199. (scm_standard_stream_to_port (1, isatty (1) ? "w0" : "w"));
  200. scm_set_current_error_port
  201. (scm_standard_stream_to_port (2, isatty (2) ? "w0" : "w"));
  202. scm_set_current_warning_port (scm_current_error_port ());
  203. }
  204. /* Loading the startup Scheme files. */
  205. /* The boot code "ice-9/boot-9" is only loaded by scm_boot_guile when
  206. this is false. The unexec code uses this, to keep ice_9 from being
  207. loaded into dumped guile executables. */
  208. int scm_ice_9_already_loaded = 0;
  209. void
  210. scm_load_startup_files ()
  211. {
  212. /* We want a path only containing directories from GUILE_LOAD_PATH,
  213. SCM_SITE_DIR and SCM_LIBRARY_DIR when searching for the site init
  214. file, so we do this before loading Ice-9. */
  215. SCM init_path =
  216. scm_sys_search_load_path (scm_from_locale_string ("init.scm"));
  217. /* Load Ice-9. */
  218. if (!scm_ice_9_already_loaded)
  219. {
  220. scm_c_primitive_load_path ("ice-9/boot-9");
  221. /* Load the init.scm file. */
  222. if (scm_is_true (init_path))
  223. scm_primitive_load (init_path);
  224. }
  225. }
  226. /* The main init code. */
  227. #ifdef _UNICOS
  228. typedef int setjmp_type;
  229. #else
  230. typedef long setjmp_type;
  231. #endif
  232. /* All the data needed to invoke the main function. */
  233. struct main_func_closure
  234. {
  235. /* the function to call */
  236. void (*main_func)(void *closure, int argc, char **argv);
  237. void *closure; /* dummy data to pass it */
  238. int argc;
  239. char **argv; /* the argument list it should receive */
  240. };
  241. static void *invoke_main_func(void *body_data);
  242. /* Fire up the Guile Scheme interpreter.
  243. Call MAIN_FUNC, passing it CLOSURE, ARGC, and ARGV. MAIN_FUNC
  244. should do all the work of the program (initializing other packages,
  245. reading user input, etc.) before returning. When MAIN_FUNC
  246. returns, call exit (EXIT_FAILURE); this function never returns.
  247. If you want some other exit value, MAIN_FUNC may call exit itself.
  248. scm_boot_guile arranges for program-arguments to return the strings
  249. given by ARGC and ARGV. If MAIN_FUNC modifies ARGC/ARGV, should
  250. call scm_set_program_arguments with the final list, so Scheme code
  251. will know which arguments have been processed.
  252. scm_boot_guile establishes a catch-all catch handler which prints
  253. an error message and exits the process. This means that Guile
  254. exits in a coherent way when system errors occur and the user isn't
  255. prepared to handle it. If the user doesn't like this behavior,
  256. they can establish their own universal catcher to shadow this one.
  257. Why must the caller do all the real work from MAIN_FUNC? The
  258. garbage collector assumes that all local variables of type SCM will
  259. be above scm_boot_guile's stack frame on the stack. If you try to
  260. manipulate SCM values after this function returns, it's the luck of
  261. the draw whether the GC will be able to find the objects you
  262. allocate. So, scm_boot_guile function exits, rather than
  263. returning, to discourage people from making that mistake. */
  264. void
  265. scm_boot_guile (int argc, char ** argv, void (*main_func) (), void *closure)
  266. {
  267. void *res;
  268. struct main_func_closure c;
  269. /* On Windows, convert backslashes in argv[0] to forward
  270. slashes. */
  271. if (argc > 0)
  272. scm_i_mirror_backslashes (argv[0]);
  273. c.main_func = main_func;
  274. c.closure = closure;
  275. c.argc = argc;
  276. c.argv = argv;
  277. res = scm_with_guile (invoke_main_func, &c);
  278. /* If the caller doesn't want this, they should exit from main_func
  279. themselves.
  280. */
  281. if (res == NULL)
  282. exit (EXIT_FAILURE);
  283. else
  284. exit (EXIT_SUCCESS);
  285. }
  286. static void *
  287. invoke_main_func (void *body_data)
  288. {
  289. struct main_func_closure *closure = (struct main_func_closure *) body_data;
  290. scm_i_set_boot_program_arguments (closure->argc, closure->argv);
  291. (*closure->main_func) (closure->closure, closure->argc, closure->argv);
  292. scm_restore_signals ();
  293. /* This tick gives any pending
  294. * asyncs a chance to run. This must be done after
  295. * the call to scm_restore_signals.
  296. */
  297. SCM_ASYNC_TICK;
  298. /* Indicate success by returning non-NULL.
  299. */
  300. return (void *)1;
  301. }
  302. scm_i_pthread_mutex_t scm_i_init_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  303. int scm_initialized_p = 0;
  304. static void *
  305. really_cleanup_for_exit (void *unused)
  306. {
  307. scm_flush_all_ports ();
  308. return NULL;
  309. }
  310. static void
  311. cleanup_for_exit ()
  312. {
  313. if (scm_i_pthread_mutex_trylock (&scm_i_init_mutex) == 0)
  314. scm_i_pthread_mutex_unlock (&scm_i_init_mutex);
  315. else
  316. {
  317. fprintf (stderr, "Cannot exit gracefully when init is in progress; aborting.\n");
  318. abort ();
  319. }
  320. /* This function might be called in non-guile mode, so we need to
  321. enter it temporarily.
  322. */
  323. scm_with_guile (really_cleanup_for_exit, NULL);
  324. }
  325. void
  326. scm_i_init_guile (void *base)
  327. {
  328. if (scm_initialized_p)
  329. return;
  330. scm_storage_prehistory ();
  331. scm_threads_prehistory (base); /* requires storage_prehistory */
  332. scm_weak_table_prehistory (); /* requires storage_prehistory */
  333. #ifdef GUILE_DEBUG_MALLOC
  334. scm_debug_malloc_prehistory ();
  335. #endif
  336. scm_symbols_prehistory (); /* requires weak_table_prehistory */
  337. scm_modules_prehistory ();
  338. scm_init_array_handle ();
  339. scm_bootstrap_bytevectors (); /* Requires array-handle */
  340. scm_bootstrap_instructions ();
  341. scm_bootstrap_loader ();
  342. scm_bootstrap_programs ();
  343. scm_bootstrap_vm ();
  344. scm_register_r6rs_ports ();
  345. scm_register_foreign ();
  346. scm_register_foreign_object ();
  347. scm_register_srfi_1 ();
  348. scm_register_srfi_60 ();
  349. scm_register_poll ();
  350. scm_init_strings (); /* Requires array-handle */
  351. scm_init_struct (); /* Requires strings */
  352. scm_smob_prehistory ();
  353. scm_init_variable ();
  354. scm_init_continuations (); /* requires smob_prehistory */
  355. scm_init_root (); /* requires continuations */
  356. scm_init_threads (); /* requires smob_prehistory */
  357. scm_init_gsubr ();
  358. scm_init_thread_procs (); /* requires gsubrs */
  359. scm_init_procprop ();
  360. scm_init_alist ();
  361. scm_init_arbiters (); /* requires smob_prehistory */
  362. scm_init_async (); /* requires smob_prehistory */
  363. scm_init_boolean ();
  364. scm_init_chars ();
  365. #ifdef GUILE_DEBUG_MALLOC
  366. scm_init_debug_malloc ();
  367. #endif
  368. scm_init_dynwind (); /* requires smob_prehistory */
  369. scm_init_eq ();
  370. scm_init_error ();
  371. scm_init_finalizers ();
  372. scm_init_fluids ();
  373. scm_init_control (); /* requires fluids */
  374. scm_init_feature ();
  375. scm_init_backtrace ();
  376. scm_init_fports ();
  377. scm_init_strports ();
  378. scm_init_ports ();
  379. scm_init_hash ();
  380. scm_init_hashtab ();
  381. scm_init_deprecation ();
  382. scm_init_objprop ();
  383. scm_init_promises (); /* requires smob_prehistory */
  384. scm_init_hooks (); /* Requires smob_prehistory */
  385. scm_init_stime ();
  386. scm_init_gc (); /* Requires hooks and `get_internal_run_time' */
  387. scm_init_gc_protect_object (); /* requires threads_prehistory */
  388. scm_init_gettext ();
  389. scm_init_ioext ();
  390. scm_init_keywords (); /* Requires smob_prehistory */
  391. scm_init_fports_keywords ();
  392. scm_init_list ();
  393. scm_init_random (); /* Requires smob_prehistory */
  394. scm_init_macros (); /* Requires smob_prehistory and random */
  395. scm_init_mallocs (); /* Requires smob_prehistory */
  396. scm_init_modules (); /* Requires smob_prehistory */
  397. scm_init_numbers ();
  398. scm_init_options ();
  399. scm_init_pairs ();
  400. scm_init_filesys (); /* Requires smob_prehistory */
  401. #ifdef HAVE_POSIX
  402. scm_init_posix ();
  403. #endif
  404. #ifdef ENABLE_REGEX
  405. scm_init_regex_posix (); /* Requires smob_prehistory */
  406. #endif
  407. scm_init_procs ();
  408. scm_init_scmsigs ();
  409. #ifdef HAVE_NETWORKING
  410. scm_init_net_db ();
  411. scm_init_socket ();
  412. #endif
  413. scm_init_sort ();
  414. scm_init_srcprop (); /* requires smob_prehistory */
  415. scm_init_stackchk ();
  416. scm_init_generalized_arrays ();
  417. scm_init_generalized_vectors ();
  418. scm_init_vectors (); /* Requires array-handle, */
  419. scm_init_uniform ();
  420. scm_init_bitvectors (); /* Requires smob_prehistory, array-handle */
  421. scm_init_srfi_4 (); /* Requires smob_prehistory, array-handle */
  422. scm_init_arrays (); /* Requires smob_prehistory, array-handle */
  423. scm_init_array_map ();
  424. scm_init_frames (); /* Requires smob_prehistory */
  425. scm_init_stacks (); /* Requires strings, struct, frames */
  426. scm_init_symbols ();
  427. scm_init_values (); /* Requires struct */
  428. scm_init_load (); /* Requires strings */
  429. scm_init_print (); /* Requires strings, struct, smob */
  430. scm_init_read ();
  431. scm_init_strorder ();
  432. scm_init_srfi_13 ();
  433. scm_init_srfi_14 (); /* Requires smob_prehistory */
  434. scm_init_throw (); /* Requires smob_prehistory */
  435. scm_init_trees ();
  436. scm_init_version ();
  437. scm_init_weak_set ();
  438. scm_init_weak_table ();
  439. scm_init_weak_vectors ();
  440. scm_init_guardians (); /* requires smob_prehistory */
  441. scm_init_vports ();
  442. scm_init_standard_ports (); /* Requires fports */
  443. scm_init_expand (); /* Requires structs */
  444. scm_init_memoize (); /* Requires smob_prehistory */
  445. scm_init_eval (); /* Requires smob_prehistory */
  446. scm_init_load_path ();
  447. scm_init_eval_in_scheme ();
  448. scm_init_evalext ();
  449. scm_init_debug (); /* Requires macro smobs */
  450. scm_init_simpos ();
  451. #if HAVE_MODULES
  452. scm_init_dynamic_linking (); /* Requires smob_prehistory */
  453. #endif
  454. scm_bootstrap_i18n ();
  455. scm_init_script ();
  456. scm_init_unicode ();
  457. scm_init_goops ();
  458. #if SCM_ENABLE_DEPRECATED == 1
  459. scm_i_init_deprecated ();
  460. #endif
  461. scm_initialized_p = 1;
  462. scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
  463. scm_init_rdelim ();
  464. scm_init_rw ();
  465. scm_init_extensions ();
  466. atexit (cleanup_for_exit);
  467. scm_load_startup_files ();
  468. scm_init_load_should_auto_compile ();
  469. /* Capture the dynamic state after loading boot-9, so that new threads end up
  470. in the guile-user module. */
  471. scm_init_threads_default_dynamic_state ();
  472. /* Finally, cause finalizers to run in a separate thread. */
  473. scm_init_finalizer_thread ();
  474. }
  475. /*
  476. Local Variables:
  477. c-file-style: "gnu"
  478. End:
  479. */