XEmacs 21.4.22.
[chise/xemacs-chise.git.1] / src / emacs.c
index d1922a9..7ca2548 100644 (file)
@@ -2,7 +2,7 @@
    Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994
    Free Software Foundation, Inc.
    Copyright (C) 1995 Sun Microsystems, Inc.
    Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994
    Free Software Foundation, Inc.
    Copyright (C) 1995 Sun Microsystems, Inc.
-   Copyright (C) 2000 Ben Wing.
+   Copyright (C) 2000, 2002 Ben Wing.
 
 This file is part of XEmacs.
 
 
 This file is part of XEmacs.
 
@@ -180,6 +180,10 @@ version 18.59 released October 31, 1992.
 #include "sysfile.h"
 #include "systime.h"
 
 #include "sysfile.h"
 #include "systime.h"
 
+#ifdef CYGWIN
+#include "syswindows.h"
+#endif
+
 #ifdef PDUMP
 #include "dumper.h"
 #endif
 #ifdef PDUMP
 #include "dumper.h"
 #endif
@@ -340,7 +344,7 @@ uintptr_t bss_end = 0;
 #endif
 
 /* Number of bytes of writable memory we can expect to be able to get */
 #endif
 
 /* Number of bytes of writable memory we can expect to be able to get */
-unsigned int lim_data;
+unsigned long lim_data;
 
 /* WARNING!
 
 
 /* WARNING!
 
@@ -461,6 +465,9 @@ int fatal_error_in_progress;
    or do other non-essential stuff. */
 int preparing_for_armageddon;
 
    or do other non-essential stuff. */
 int preparing_for_armageddon;
 
+/* Nonzero means we're in an unstable situation and need to skip
+   i18n conversions and such during printing. */
+int inhibit_non_essential_printing_operations;
 
 static JMP_BUF run_temacs_catch;
 
 
 static JMP_BUF run_temacs_catch;
 
@@ -472,6 +479,101 @@ static EMACS_INT run_temacs_args_size;
 
 static void shut_down_emacs (int sig, Lisp_Object stuff, int no_auto_save);
 
 
 static void shut_down_emacs (int sig, Lisp_Object stuff, int no_auto_save);
 
+/* ------------------------------- */
+/*  low-level debugging functions  */
+/* ------------------------------- */
+
+#if defined (WIN32_NATIVE) && defined (DEBUG_XEMACS)
+#define debugging_breakpoint() DebugBreak ()
+#else
+#define debugging_breakpoint()
+#endif
+
+void
+debug_break (void)
+{
+  debugging_breakpoint ();
+}
+
+#if defined (WIN32_NATIVE) || defined (CYGWIN)
+
+/* Return whether all bytes in the specified memory block can be read. */
+int
+debug_can_access_memory (void *ptr, Bytecount len)
+{
+  return !IsBadReadPtr (ptr, len);
+}
+
+#else /* !(defined (WIN32_NATIVE) || defined (CYGWIN)) */
+
+/* #### There must be a better way!!!! */
+
+static JMP_BUF memory_error_jump;
+
+static SIGTYPE
+debug_memory_error (int signum)
+{
+  EMACS_REESTABLISH_SIGNAL (signum, debug_memory_error);
+  EMACS_UNBLOCK_SIGNAL (signum);
+  LONGJMP (memory_error_jump, 1);
+}
+
+/* Return whether all bytes in the specified memory block can be read. */
+int
+debug_can_access_memory (void *ptr, Bytecount len)
+{
+  /* Use volatile to protect variables from being clobbered by longjmp. */
+  SIGTYPE (*volatile old_sigbus) (int);
+  SIGTYPE (*volatile old_sigsegv) (int);
+  volatile int old_errno = errno;
+  volatile int retval = 1;
+
+  if (!SETJMP (memory_error_jump))
+    {
+      old_sigbus =
+       (SIGTYPE (*) (int)) signal (SIGBUS, debug_memory_error);
+      old_sigsegv =
+       (SIGTYPE (*) (int)) signal (SIGSEGV, debug_memory_error);
+
+      if (len > 1)
+       /* If we can, try to avoid problems with super-optimizing compilers
+          that might decide that memcmp (ptr, ptr, len) can be optimized
+          away since its result is always 1. */
+       memcmp (ptr, (char *) ptr + 1, len - 1);
+      else
+       memcmp (ptr, ptr, len);
+    }
+  else
+    retval = 0;
+  signal (SIGBUS, old_sigbus);
+  signal (SIGSEGV, old_sigsegv);
+  errno = old_errno;
+
+  return retval;
+}
+
+#endif /* defined (WIN32_NATIVE) || defined (CYGWIN) */
+
+#ifdef DEBUG_XEMACS
+
+DEFUN ("force-debugging-signal", Fforce_debugging_signal, 0, 1, 0, /*
+Cause XEmacs to enter the debugger.
+On some systems, there may be no way to do this gracefully; if so,
+nothing happens unless ABORT is non-nil, in which case XEmacs will
+ABORT() -- a sure-fire way to immediately get back to the debugger,
+but also a sure-fire way to kill XEmacs (and dump core on Unix
+systems)!
+*/
+       (abort_))
+{
+  debugging_breakpoint ();
+  if (!NILP (abort_))
+    ABORT ();
+  return Qnil;
+}
+
+#endif /* DEBUG_XEMACS */
+
 static void
 ensure_no_quitting_from_now_on (void)
 {
 static void
 ensure_no_quitting_from_now_on (void)
 {
@@ -486,6 +588,7 @@ SIGTYPE
 fatal_error_signal (int sig)
 {
   fatal_error_in_progress++;
 fatal_error_signal (int sig)
 {
   fatal_error_in_progress++;
+  inhibit_non_essential_printing_operations = 1;
   preparing_for_armageddon = 1;
 
   ensure_no_quitting_from_now_on ();
   preparing_for_armageddon = 1;
 
   ensure_no_quitting_from_now_on ();
@@ -557,6 +660,7 @@ mswindows_handle_hardware_exceptions (DWORD code)
   __try
     {
       fatal_error_in_progress++;
   __try
     {
       fatal_error_in_progress++;
+      inhibit_non_essential_printing_operations = 1;
       preparing_for_armageddon = 1;
 
       ensure_no_quitting_from_now_on ();
       preparing_for_armageddon = 1;
 
       ensure_no_quitting_from_now_on ();
@@ -869,6 +973,7 @@ main_1 (int argc, char **argv, char **envp, int restart)
 #endif /* not SYSTEM_MALLOC or HAVE_LIBMCHECK or DOUG_LEA_MALLOC */
 
   noninteractive = 0;
 #endif /* not SYSTEM_MALLOC or HAVE_LIBMCHECK or DOUG_LEA_MALLOC */
 
   noninteractive = 0;
+  inhibit_non_essential_printing_operations = 1;
 
 #ifdef NeXT
   /* 19-Jun-1995 -baw
 
 #ifdef NeXT
   /* 19-Jun-1995 -baw
@@ -1956,6 +2061,7 @@ main_1 (int argc, char **argv, char **envp, int restart)
         */
 
       specifier_vars_of_glyphs ();
         */
 
       specifier_vars_of_glyphs ();
+      specifier_vars_of_glyphs_widget ();
       specifier_vars_of_gutter ();
 #ifdef HAVE_MENUBARS
       specifier_vars_of_menubar ();
       specifier_vars_of_gutter ();
 #ifdef HAVE_MENUBARS
       specifier_vars_of_menubar ();
@@ -2348,6 +2454,7 @@ main_1 (int argc, char **argv, char **envp, int restart)
 #endif /* QUANTIFY */
 
   initialized = 1;
 #endif /* QUANTIFY */
 
   initialized = 1;
+  inhibit_non_essential_printing_operations = 0;
 
   /* This never returns.  */
   initial_command_loop (load_me);
 
   /* This never returns.  */
   initial_command_loop (load_me);
@@ -2549,7 +2656,7 @@ sort_args (int argc, char **argv)
        }
 
       if (best < 0)
        }
 
       if (best < 0)
-       abort ();
+       ABORT ();
 
       /* Copy the highest priority remaining option, with its args, to NEW_ARGV.  */
       new_argv[to++] = argv[best];
 
       /* Copy the highest priority remaining option, with its args, to NEW_ARGV.  */
       new_argv[to++] = argv[best];
@@ -2699,6 +2806,7 @@ main (int argc, char **argv, char **envp)
   quantify_clear_data ();
 #endif /* QUANTIFY */
 
   quantify_clear_data ();
 #endif /* QUANTIFY */
 
+  inhibit_non_essential_printing_operations = 1;
   suppress_early_error_handler_backtrace = 0;
   lim_data = 0; /* force reinitialization of this variable */
 
   suppress_early_error_handler_backtrace = 0;
   lim_data = 0; /* force reinitialization of this variable */
 
@@ -2712,7 +2820,8 @@ main (int argc, char **argv, char **envp)
   if (!initialized)
     {
 #ifdef DOUG_LEA_MALLOC
   if (!initialized)
     {
 #ifdef DOUG_LEA_MALLOC
-      mallopt (M_MMAP_MAX, 0);
+      if (mallopt (M_MMAP_MAX, 0) != 1)
+       ABORT();
 #endif
       run_temacs_argc = 0;
       if (! SETJMP (run_temacs_catch))
 #endif
       run_temacs_argc = 0;
       if (! SETJMP (run_temacs_catch))
@@ -2759,7 +2868,7 @@ main (int argc, char **argv, char **envp)
       if (rc != 0)
        {
          stderr_out ("malloc_set_state failed, rc = %d\n", rc);
       if (rc != 0)
        {
          stderr_out ("malloc_set_state failed, rc = %d\n", rc);
-         abort ();
+         ABORT ();
        }
 #if 0
       free (malloc_state_ptr);
        }
 #if 0
       free (malloc_state_ptr);
@@ -2769,7 +2878,8 @@ main (int argc, char **argv, char **envp)
     defined(_NO_MALLOC_WARNING_) || \
     (defined(__GLIBC__) && __GLIBC_MINOR__ < 1 && !defined(MULE)) || \
     defined(DEBUG_DOUG_LEA_MALLOC)
     defined(_NO_MALLOC_WARNING_) || \
     (defined(__GLIBC__) && __GLIBC_MINOR__ < 1 && !defined(MULE)) || \
     defined(DEBUG_DOUG_LEA_MALLOC)
-      mallopt (M_MMAP_MAX, 64);
+      if(mallopt (M_MMAP_MAX, 0) != 1)
+       ABORT();
 #endif
 #ifdef REL_ALLOC
       r_alloc_reinit ();
 #endif
 #ifdef REL_ALLOC
       r_alloc_reinit ();
@@ -2803,7 +2913,8 @@ voodoo_free_hook (void *mem)
   /* Disable all calls to free() when XEmacs is exiting and it doesn't */
   /* matter. */
   __free_hook =
   /* Disable all calls to free() when XEmacs is exiting and it doesn't */
   /* matter. */
   __free_hook =
-#ifdef __GNUC__ /* prototype of __free_hook varies with glibc version */
+#if defined __GNUC__ || defined __INTEL_COMPILER
+/* prototype of __free_hook varies with glibc version */
     (__typeof__ (__free_hook))
 #endif
     voodoo_free_hook;
     (__typeof__ (__free_hook))
 #endif
     voodoo_free_hook;
@@ -2877,7 +2988,8 @@ all of which are called before XEmacs is actually killed.
 
 #if defined(GNU_MALLOC)
   __free_hook =
 
 #if defined(GNU_MALLOC)
   __free_hook =
-#ifdef __GNUC__ /* prototype of __free_hook varies with glibc version */
+#if defined __GNUC__ || defined __INTEL_COMPILER
+/* prototype of __free_hook varies with glibc version */
     (__typeof__ (__free_hook))
 #endif
     voodoo_free_hook;
     (__typeof__ (__free_hook))
 #endif
     voodoo_free_hook;
@@ -2948,28 +3060,26 @@ shut_down_emacs (int sig, Lisp_Object stuff, int no_auto_save)
        ("Your files have been auto-saved.\n"
         "Use `M-x recover-session' to recover them.\n"
         "\n"
        ("Your files have been auto-saved.\n"
         "Use `M-x recover-session' to recover them.\n"
         "\n"
-         "If you have access to the PROBLEMS file that came with your\n"
-         "version of XEmacs, please check to see if your crash is described\n"
-         "there, as there may be a workaround available.\n"
+         "Your version of XEmacs was distributed with a PROBLEMS file that  may describe\n"
+        "your crash, and with luck a workaround.  Please check it first, but do report\n"
+        "the crash anyway.  "
 #ifdef INFODOCK
 #ifdef INFODOCK
-        "Otherwise, please report this bug by selecting `Report-Bug'\n"
-         "in the InfoDock menu.\n"
+        "\n\nPlease report this bug by selecting `Report-Bug' in the InfoDock menu.\n"
+        "*BE SURE* to include the XEmacs configuration from M-x describe-installation,\n"
+        "or the file Installation in the top directory of the build tree.\n"
 #else
 #else
-        "Otherwise, please report this bug by running the send-pr\n"
-         "script included with XEmacs, or selecting `Send Bug Report'\n"
-         "from the help menu.\n"
-        "As a last resort send ordinary email to `crashes@xemacs.org'.\n"
+        "Please report this bug by invoking M-x report-emacs-bug,\n"
+        "or by selecting `Send Bug Report' from the Help menu.  If necessary, send\n"
+        "ordinary email to `xemacs-beta@xemacs.org'.  *MAKE SURE* to include the XEmacs\n"
+        "configuration from M-x describe-installation, or equivalently the file\n"
+        "Installation in the top of the build tree.\n"
 #endif
 #endif
-        "*MAKE SURE* to include the information in the command\n"
-        "M-x describe-installation.\n"
 #ifndef _MSC_VER
         "\n"
 #ifndef _MSC_VER
         "\n"
-        "If at all possible, *please* try to obtain a C stack backtrace;\n"
-        "it will help us immensely in determining what went wrong.\n"
-        "To do this, locate the core file that was produced as a result\n"
-        "of this crash (it's usually called `core' and is located in the\n"
-        "directory in which you started the editor, or maybe in your home\n"
-        "directory), and type\n"
+        "*Please* try *hard* to obtain a C stack backtrace; without it, we are unlikely\n"
+        "to be able to analyze the problem.  Locate the core file produced as a result\n"
+        "of this crash (often called `core' or `core.<process-id>', and located in\n"
+        "the directory in which you started XEmacs or your home directory), and type\n"
         "\n"
         "  gdb "
 #endif
         "\n"
         "  gdb "
 #endif
@@ -2996,11 +3106,12 @@ shut_down_emacs (int sig, Lisp_Object stuff, int no_auto_save)
          stderr_out ("%s%s", dir, name);
       }
       stderr_out
          stderr_out ("%s%s", dir, name);
       }
       stderr_out
-       (" core\n\n"
-        "then type `where' when the debugger prompt comes up.\n"
-        "(If you don't have GDB on your system, you might have DBX,\n"
-        "or XDB, or SDB.  A similar procedure should work for all of\n"
-        "these.  Ask your system administrator if you need more help.)\n");
+       (" core\n"
+        "\n"
+        "then type `where' at the debugger prompt.  No GDB on your system?  You may\n"
+        "have DBX, or XDB, or SDB.  (Ask your system administrator if you need help.)\n"
+        "If no core file was produced, enable them (often with `ulimit -c unlimited'\n"
+        "in case of future recurrance of the crash.\n");
 #endif /* _MSC_VER */
     }
 
 #endif /* _MSC_VER */
     }
 
@@ -3245,7 +3356,6 @@ Non-nil return value means XEmacs is running without interactive terminal.
    in one session without having to recompile. */
 /* #define ASSERTIONS_DONT_ABORT */
 
    in one session without having to recompile. */
 /* #define ASSERTIONS_DONT_ABORT */
 
-#ifdef USE_ASSERTIONS
 /* This highly dubious kludge ... shut up Jamie, I'm tired of your slagging. */
 
 static int in_assert_failed;
 /* This highly dubious kludge ... shut up Jamie, I'm tired of your slagging. */
 
 static int in_assert_failed;
@@ -3257,8 +3367,6 @@ static const char *assert_failed_expr;
 #undef fprintf
 #endif
 
 #undef fprintf
 #endif
 
-#undef abort   /* avoid infinite #define loop... */
-
 #if defined (WIN32_NATIVE) && defined (DEBUG_XEMACS)
 #define enter_debugger() DebugBreak ()
 #else
 #if defined (WIN32_NATIVE) && defined (DEBUG_XEMACS)
 #define enter_debugger() DebugBreak ()
 #else
@@ -3276,6 +3384,7 @@ assert_failed (const char *file, int line, const char *expr)
   /* We are extremely paranoid so we sensibly deal with recursive
      assertion failures. */
   in_assert_failed++;
   /* We are extremely paranoid so we sensibly deal with recursive
      assertion failures. */
   in_assert_failed++;
+  inhibit_non_essential_printing_operations = 1;
 
   if (in_assert_failed >= 4)
     _exit (-1);
 
   if (in_assert_failed >= 4)
     _exit (-1);
@@ -3314,11 +3423,11 @@ assert_failed (const char *file, int line, const char *expr)
 
   enter_debugger ();
 #if !defined (ASSERTIONS_DONT_ABORT)
 
   enter_debugger ();
 #if !defined (ASSERTIONS_DONT_ABORT)
-  abort ();
+  abort (); /* The real abort(), this time */
 #endif
 #endif
+  inhibit_non_essential_printing_operations = 0;
   in_assert_failed = 0;
 }
   in_assert_failed = 0;
 }
-#endif /* USE_ASSERTIONS */
 
 #ifdef QUANTIFY
 DEFUN ("quantify-start-recording-data", Fquantify_start_recording_data,
 
 #ifdef QUANTIFY
 DEFUN ("quantify-start-recording-data", Fquantify_start_recording_data,
@@ -3365,6 +3474,10 @@ syms_of_emacs (void)
   DEFSUBR (Fkill_emacs);
   DEFSUBR (Fnoninteractive);
 
   DEFSUBR (Fkill_emacs);
   DEFSUBR (Fnoninteractive);
 
+#ifdef DEBUG_XEMACS
+  DEFSUBR (Fforce_debugging_signal);
+#endif
+
 #ifdef QUANTIFY
   DEFSUBR (Fquantify_start_recording_data);
   DEFSUBR (Fquantify_stop_recording_data);
 #ifdef QUANTIFY
   DEFSUBR (Fquantify_start_recording_data);
   DEFSUBR (Fquantify_stop_recording_data);