init.c 15 KB

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