use guile subrs
[bpt/emacs.git] / src / emacs.c
index 57f7131..01b8368 100644 (file)
@@ -27,6 +27,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <sys/types.h>
 #include <sys/file.h>
 #include <unistd.h>
+#include <libguile.h>
 
 #include <close-stream.h>
 
@@ -80,6 +81,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "composite.h"
 #include "dispextern.h"
 #include "syntax.h"
+#include "sysselect.h"
 #include "systime.h"
 
 #ifdef HAVE_GNUTLS
@@ -105,6 +107,10 @@ extern void moncontrol (int mode);
 #include <sys/personality.h>
 #endif
 
+Lisp_Object symbol_module;
+Lisp_Object function_module;
+Lisp_Object plist_module;
+
 static const char emacs_version[] = PACKAGE_VERSION;
 static const char emacs_copyright[] = COPYRIGHT;
 static const char emacs_bugreport[] = PACKAGE_BUGREPORT;
@@ -164,10 +170,6 @@ bool running_asynch_code;
 bool display_arg;
 #endif
 
-/* An address near the bottom of the stack.
-   Tells GC how to save a copy of the stack.  */
-char *stack_bottom;
-
 #if defined (DOUG_LEA_MALLOC) || defined (GNU_LINUX)
 /* The address where the heap starts (from the first sbrk (0) call).  */
 static void *my_heap_start;
@@ -392,7 +394,7 @@ init_cmdargs (int argc, char **argv, int skip_args, char *original_pwd)
 {
   register int i;
   Lisp_Object name, dir, handler;
-  ptrdiff_t count = SPECPDL_INDEX ();
+  dynwind_begin ();
   Lisp_Object raw_name;
 
   initial_argv = argv;
@@ -551,7 +553,7 @@ init_cmdargs (int argc, char **argv, int skip_args, char *original_pwd)
          = Fcons (build_unibyte_string (argv[i]), Vcommand_line_args);
     }
 
-  unbind_to (count, Qnil);
+  dynwind_end ();
 }
 
 DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
@@ -669,9 +671,7 @@ malloc_initialize_hook (void)
        }
 
       malloc_set_state (malloc_state_ptr);
-#ifndef XMALLOC_OVERRUN_CHECK
       free (malloc_state_ptr);
-#endif
     }
   else
     {
@@ -700,14 +700,20 @@ close_output_streams (void)
      _exit (EXIT_FAILURE);
 }
 
-/* ARGSUSED */
+static int main2 (void *, int, char **);
+
 int
 main (int argc, char **argv)
 {
-#if GC_MARK_STACK
-  Lisp_Object dummy;
-#endif
-  char stack_bottom_variable;
+  /* Override Guile's libgc configuration. */
+  xputenv ("GC_ALL_INTERIOR_POINTERS=1");
+  scm_boot_guile (argc, argv, main2, NULL);
+}
+
+/* ARGSUSED */
+static int
+main2 (void *ignore, int argc, char **argv)
+{
   bool do_initial_setlocale;
   bool dumping;
   int skip_args = 0;
@@ -725,10 +731,6 @@ main (int argc, char **argv)
   /* If we use --chdir, this records the original directory.  */
   char *original_pwd = 0;
 
-#if GC_MARK_STACK
-  stack_base = &dummy;
-#endif
-
 #ifdef G_SLICE_ALWAYS_MALLOC
   /* This is used by the Cygwin build.  It's not needed starting with
      cygwin-1.7.24, but it doesn't do any harm.  */
@@ -902,9 +904,6 @@ main (int argc, char **argv)
     }
 #endif /* HAVE_SETRLIMIT and RLIMIT_STACK */
 
-  /* Record (approximately) where the stack begins.  */
-  stack_bottom = &stack_bottom_variable;
-
   clearerr (stdin);
 
 #ifndef SYSTEM_MALLOC
@@ -1169,7 +1168,25 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
 
   if (!initialized)
     {
+      symbol_module = scm_call (scm_c_public_ref ("guile", "define-module*"),
+                                scm_list_1 (scm_from_utf8_symbol ("elisp-symbols")),
+                                scm_from_locale_keyword ("pure"),
+                                SCM_BOOL_T,
+                                SCM_UNDEFINED);
+      function_module = scm_call (scm_c_public_ref ("guile", "define-module*"),
+                                  scm_list_1 (scm_from_utf8_symbol ("elisp-functions")),
+                                  scm_from_locale_keyword ("pure"),
+                                  SCM_BOOL_T,
+                                  SCM_UNDEFINED);
+      plist_module = scm_call (scm_c_public_ref ("guile", "define-module*"),
+                                  scm_list_1 (scm_from_utf8_symbol ("elisp-plists")),
+                                  scm_from_locale_keyword ("pure"),
+                                  SCM_BOOL_T,
+                                  SCM_UNDEFINED);
+
       init_alloc_once ();
+      init_guile ();
+      init_fns_once ();
       init_obarray ();
       init_eval_once ();
       init_charset_once ();
@@ -1181,6 +1198,11 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
       init_minibuf_once ();    /* Create list of minibuffers.  */
                                /* Must precede init_window_once.  */
 
+      /* Called before syms_of_fileio, because it sets up
+         Qerror_condition.  Called before other symbol-initialization
+         functions because it sets up symbols used by defsubr.  */
+      syms_of_data ();
+
       /* Call syms_of_xfaces before init_window_once because that
         function creates Vterminal_frame.  Termcap frames now use
         faces, and the face implementation uses some symbols as
@@ -1195,8 +1217,6 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
         CANNOT_DUMP is defined.  */
       syms_of_keyboard ();
 
-      /* Called before syms_of_fileio, because it sets up Qerror_condition.  */
-      syms_of_data ();
       syms_of_fns ();     /* Before syms_of_charset which uses hashtables.  */
       syms_of_fileio ();
       /* Before syms_of_coding to initialize Vgc_cons_threshold.  */
@@ -1787,8 +1807,8 @@ sort_args (int argc, char **argv)
      0 for an option that takes no arguments,
      1 for an option that takes one argument, etc.
      -1 for an ordinary non-option argument.  */
-  int *options = xnmalloc (argc, sizeof *options);
-  int *priority = xnmalloc (argc, sizeof *priority);
+  int *options = xnmalloc_atomic (argc, sizeof *options);
+  int *priority = xnmalloc_atomic (argc, sizeof *priority);
   int to = 1;
   int incoming_used = 1;
   int from;
@@ -2081,7 +2101,7 @@ You must run Emacs in batch mode in order to dump it.  */)
 {
   Lisp_Object tem;
   Lisp_Object symbol;
-  ptrdiff_t count = SPECPDL_INDEX ();
+  dynwind_begin ();
 
   check_pure_size ();
 
@@ -2171,7 +2191,8 @@ You must run Emacs in batch mode in order to dump it.  */)
 
   Vpurify_flag = tem;
 
-  return unbind_to (count, Qnil);
+  dynwind_end ();
+  return Qnil;
 }
 
 #endif /* not CANNOT_DUMP */
@@ -2411,22 +2432,13 @@ from the parent process and its tty file descriptors.  */)
 void
 syms_of_emacs (void)
 {
+#include "emacs.x"
+
   DEFSYM (Qfile_name_handler_alist, "file-name-handler-alist");
   DEFSYM (Qrisky_local_variable, "risky-local-variable");
   DEFSYM (Qkill_emacs, "kill-emacs");
   DEFSYM (Qkill_emacs_hook, "kill-emacs-hook");
 
-#ifndef CANNOT_DUMP
-  defsubr (&Sdump_emacs);
-#endif
-
-  defsubr (&Skill_emacs);
-
-  defsubr (&Sinvocation_name);
-  defsubr (&Sinvocation_directory);
-  defsubr (&Sdaemonp);
-  defsubr (&Sdaemon_initialized);
-
   DEFVAR_LISP ("command-line-args", Vcommand_line_args,
               doc: /* Args passed by shell to Emacs, as a list of strings.
 Many arguments are deleted from the list as they are processed.  */);