init.c 15 KB

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