init.c 14 KB

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