*** empty log message ***
[bpt/emacs.git] / src / emacs.c
index 43f300e..cdc80ae 100644 (file)
@@ -1,6 +1,6 @@
 /* Fully extensible Emacs, running on Unix, intended for GNU.
-   Copyright (C) 1985,86,87,93,94,95,97,98,1999,2001,02,2003
-      Free Software Foundation, Inc.
+   Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 1998, 1999, 2001,
+     2002, 2003, 2004, 2005  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -42,6 +42,8 @@ Boston, MA 02111-1307, USA.  */
 
 #ifdef WINDOWSNT
 #include <fcntl.h>
+#include <windows.h> /* just for w32.h */
+#include "w32.h"
 #endif
 
 #include "lisp.h"
@@ -67,6 +69,10 @@ Boston, MA 02111-1307, USA.  */
 #include <sys/resource.h>
 #endif
 
+#ifdef HAVE_PERSONALITY_LINUX32
+#include <sys/personality.h>
+#endif
+
 #ifndef O_RDWR
 #define O_RDWR 2
 #endif
@@ -86,15 +92,25 @@ extern char *index P_ ((const char *, int));
 
 /* Make these values available in GDB, which doesn't see macros.  */
 
+#ifdef USE_LSB_TAG
+int gdb_use_lsb = 1;
+#else
+int gdb_use_lsb = 0;
+#endif
+#ifdef NO_UNION_TYPE
+int gdb_use_union = 0;
+#else
+int gdb_use_union = 1;
+#endif
 EMACS_INT gdb_valbits = VALBITS;
 EMACS_INT gdb_gctypebits = GCTYPEBITS;
-EMACS_INT gdb_emacs_intbits = sizeof (EMACS_INT) * BITS_PER_CHAR;
 #ifdef DATA_SEG_BITS
 EMACS_INT gdb_data_seg_bits = DATA_SEG_BITS;
 #else
 EMACS_INT gdb_data_seg_bits = 0;
 #endif
 EMACS_INT PVEC_FLAG = PSEUDOVECTOR_FLAG;
+EMACS_INT gdb_array_mark_flag = ARRAY_MARK_FLAG;
 
 /* Command line args from shell, as list of strings.  */
 Lisp_Object Vcommand_line_args;
@@ -116,14 +132,6 @@ Lisp_Object Vkill_emacs_hook;
 /* An empty lisp string.  To avoid having to build any other.  */
 Lisp_Object empty_string;
 
-#ifdef SIGUSR1
-/* Hooks for signal USR1 and USR2 handling.  */
-Lisp_Object Vsignal_USR1_hook;
-#ifdef SIGUSR2
-Lisp_Object Vsignal_USR2_hook;
-#endif
-#endif
-
 /* Search path separator.  */
 Lisp_Object Vpath_separator;
 
@@ -190,12 +198,25 @@ int display_arg;
    Tells GC how to save a copy of the stack.  */
 char *stack_bottom;
 
+/* The address where the heap starts (from the first sbrk (0) call).  */
+static void *my_heap_start;
+
+/* The gap between BSS end and heap start as far as we can tell.  */
+static unsigned long heap_bss_diff;
+
+/* If the gap between BSS end and heap start is larger than this we try to
+   work around it, and if that fails, output a warning in dump-emacs.  */
+#define MAX_HEAP_BSS_DIFF (1024*1024)
+
+
 #ifdef HAVE_WINDOW_SYSTEM
 extern Lisp_Object Vwindow_system;
 #endif /* HAVE_WINDOW_SYSTEM */
 
 extern Lisp_Object Vauto_save_list_file_name;
 
+extern Lisp_Object Vinhibit_redisplay;
+
 #ifdef USG_SHARED_LIBRARIES
 /* If nonzero, this is the place to put the end of the writable segment
    at startup.  */
@@ -286,7 +307,6 @@ Display options:\n\
 --fullscreen, -fs               make first frame fullscreen\n\
 --fullwidth, -fw                make the first frame wide as the screen\n\
 --geometry, -g GEOMETRY         window geometry\n\
---horizontal-scroll-bars, -hb   enable horizontal scroll bars\n\
 --icon-type, -i                 use picture of gnu for Emacs icon\n\
 --iconic                        start Emacs in iconified state\n\
 --internal-border, -ib WIDTH    width between text and main border\n\
@@ -324,6 +344,14 @@ int fatal_error_in_progress;
 
 void (*fatal_error_signal_hook) P_ ((void));
 
+#ifdef HAVE_GTK_AND_PTHREAD
+/* When compiled with GTK and running under Gnome, multiple threads meay be
+   created.  Keep track of our main thread to make sure signals are delivered
+   to it (see syssignal.h).  */
+
+pthread_t main_thread;
+#endif
+
 
 #ifdef SIGUSR1
 SIGTYPE
@@ -332,6 +360,7 @@ handle_USR1_signal (sig)
 {
   struct input_event buf;
 
+  SIGNAL_THREAD_CHECK (sig);
   bzero (&buf, sizeof buf);
   buf.kind = USER_SIGNAL_EVENT;
   buf.frame_or_window = selected_frame;
@@ -347,6 +376,7 @@ handle_USR2_signal (sig)
 {
   struct input_event buf;
 
+  SIGNAL_THREAD_CHECK (sig);
   bzero (&buf, sizeof buf);
   buf.kind = USER_SIGNAL_EVENT;
   buf.code = 1;
@@ -361,6 +391,7 @@ SIGTYPE
 fatal_error_signal (sig)
      int sig;
 {
+  SIGNAL_THREAD_CHECK (sig);
   fatal_error_code = sig;
   signal (sig, SIG_DFL);
 
@@ -400,6 +431,7 @@ memory_warning_signal (sig)
      int sig;
 {
   signal (sig, memory_warning_signal);
+  SIGNAL_THREAD_CHECK (sig);
 
   malloc_warning ("Operating system warns that virtual memory is running low.\n");
 
@@ -562,8 +594,12 @@ init_cmdargs (argc, argv, skip_args)
   for (i = argc - 1; i >= 0; i--)
     {
       if (i == 0 || i > skip_args)
+       /* For the moment, we keep arguments as is in unibyte strings.
+          They are decoded in the function command-line after we know
+          locale-coding-system.  */
        Vcommand_line_args
-         = Fcons (build_string (argv[i]), Vcommand_line_args);
+         = Fcons (make_unibyte_string (argv[i], strlen (argv[i])),
+                  Vcommand_line_args);
     }
 
   unbind_to (count, Qnil);
@@ -726,10 +762,16 @@ malloc_initialize_hook ()
        }
 
       malloc_set_state (malloc_state_ptr);
+#ifndef XMALLOC_OVERRUN_CHECK
       free (malloc_state_ptr);
+#endif
     }
   else
-    malloc_using_checking = getenv ("MALLOC_CHECK_") != NULL;
+    {
+      if (my_heap_start == 0)
+        my_heap_start = sbrk (0);
+      malloc_using_checking = getenv ("MALLOC_CHECK_") != NULL;
+    }
 }
 
 void (*__malloc_initialize_hook) () = malloc_initialize_hook;
@@ -805,6 +847,17 @@ main (argc, argv
   stack_base = &dummy;
 #endif
 
+  if (!initialized)
+    {
+      extern char my_endbss[];
+      extern char *my_endbss_static;
+
+      if (my_heap_start == 0)
+        my_heap_start = sbrk (0);
+
+      heap_bss_diff = (char *)my_heap_start - max (my_endbss, my_endbss_static);
+    }
+
 #ifdef LINUX_SBRK_BUG
   __sbrk (1);
 #endif
@@ -838,7 +891,7 @@ main (argc, argv
       else
        {
          printf ("GNU Emacs %s\n", SDATA (tem));
-         printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
+         printf ("Copyright (C) 2004 Free Software Foundation, Inc.\n");
          printf ("GNU Emacs comes with ABSOLUTELY NO WARRANTY.\n");
          printf ("You may redistribute copies of Emacs\n");
          printf ("under the terms of the GNU General Public License.\n");
@@ -848,6 +901,28 @@ main (argc, argv
        }
     }
 
+#ifdef HAVE_PERSONALITY_LINUX32
+  /* See if there is a gap between the end of BSS and the heap.
+     In that case, set personality and exec ourself again.  */
+  if (!initialized
+      && (strcmp (argv[argc-1], "dump") == 0
+          || strcmp (argv[argc-1], "bootstrap") == 0)
+      && heap_bss_diff > MAX_HEAP_BSS_DIFF)
+    {
+      if (! getenv ("EMACS_HEAP_EXEC"))
+        {
+          /* Set this so we only do this once.  */
+          putenv("EMACS_HEAP_EXEC=true");
+          personality (PER_LINUX32);
+          execvp (argv[0], argv);
+
+          /* If the exec fails, try to dump anyway.  */
+          perror ("execvp");
+        }
+    }
+#endif /* HAVE_PERSONALITY_LINUX32 */
+
+
 /* Map in shared memory, if we are using that.  */
 #ifdef HAVE_SHM
   if (argmatch (argv, argc, "-nl", "--no-shared-memory", 6, NULL, &skip_args))
@@ -885,7 +960,7 @@ main (argc, argv
   /* If -map specified, map the data file in.  */
   {
     char *file;
-    if (argmatch (argv, argc, "-map", "--map-data", 3, &mapin_file, &skip_args))
+    if (argmatch (argv, argc, "-map", "--map-data", 3, &file, &skip_args))
       mapin_data (file);
   }
 
@@ -962,10 +1037,16 @@ main (argc, argv
      Also call realloc and free for consistency.  */
   free (realloc (malloc (4), 4));
 
+# ifndef SYNC_INPUT
   /* Arrange to disable interrupt input inside malloc etc.  */
   uninterrupt_malloc ();
+# endif /* not SYNC_INPUT */
 #endif /* not SYSTEM_MALLOC */
 
+#ifdef HAVE_GTK_AND_PTHREAD
+  main_thread = pthread_self ();
+#endif /* HAVE_GTK_AND_PTHREAD */
+
 #if defined (MSDOS) || defined (WINDOWSNT)
   /* We do all file input/output as binary files.  When we need to translate
      newlines, we do that manually.  */
@@ -1054,7 +1135,10 @@ main (argc, argv
   /* Handle the -batch switch, which means don't do interactive display.  */
   noninteractive = 0;
   if (argmatch (argv, argc, "-batch", "--batch", 5, NULL, &skip_args))
-    noninteractive = 1;
+    {
+      noninteractive = 1;
+      Vundo_outer_limit = Qnil;
+    }
   if (argmatch (argv, argc, "-script", "--script", 3, &junk, &skip_args))
     {
       noninteractive = 1;      /* Set batch mode.  */
@@ -1224,7 +1308,7 @@ main (argc, argv
          creates a full-fledge output_mac type frame.  This does not
          work correctly before syms_of_textprop, syms_of_macfns,
          syms_of_ccl, syms_of_fontset, syms_of_xterm, syms_of_search,
-         syms_of_frame, mac_initialize, and init_keyboard have already
+         syms_of_frame, mac_term_init, and init_keyboard have already
          been called.  */
       syms_of_textprop ();
       syms_of_macfns ();
@@ -1236,7 +1320,7 @@ main (argc, argv
       syms_of_search ();
       syms_of_frame ();
 
-      mac_initialize ();
+      mac_term_init (build_string ("Mac"), NULL, NULL);
       init_keyboard ();
 #endif
 
@@ -1565,16 +1649,15 @@ main (argc, argv
       keys_of_minibuf ();
       keys_of_window ();
     }
-       else
+  else
     {
-      /*
-        Initialization that must be done even if the global variable
-        initialized is non zero
-      */
+      /* Initialization that must be done even if the global variable
+        initialized is non zero.  */
 #ifdef HAVE_NTGUI
       globals_of_w32fns ();
       globals_of_w32menu ();
-#endif  /* end #ifdef HAVE_NTGUI */
+      globals_of_w32select ();
+#endif  /* HAVE_NTGUI */
     }
 
   if (!noninteractive)
@@ -1927,9 +2010,9 @@ sort_args (argc, argv)
 
   bcopy (new, argv, sizeof (char *) * argc);
 
-  free (options);
-  free (new);
-  free (priority);
+  xfree (options);
+  xfree (new);
+  xfree (priority);
 }
 \f
 DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
@@ -1999,6 +2082,9 @@ shut_down_emacs (sig, no_x, stuff)
   /* Prevent running of hooks from now on.  */
   Vrun_hooks = Qnil;
 
+  /* Don't update display from now on.  */
+  Vinhibit_redisplay = Qt;
+
   /* If we are controlling the terminal, reset terminal modes.  */
 #ifdef EMACS_HAVE_TTY_PGRP
   {
@@ -2123,6 +2209,20 @@ You must run Emacs in batch mode in order to dump it.  */)
   if (! noninteractive)
     error ("Dumping Emacs works only in batch mode");
 
+#ifdef __linux__
+  if (heap_bss_diff > MAX_HEAP_BSS_DIFF)
+    {
+      fprintf (stderr, "**************************************************\n");
+      fprintf (stderr, "Warning: Your system has a gap between BSS and the\n");
+      fprintf (stderr, "heap (%lu byte).  This usually means that exec-shield\n",
+               heap_bss_diff);
+      fprintf (stderr, "or something similar is in effect.  The dump may\n");
+      fprintf (stderr, "fail because of this.  See the section about \n");
+      fprintf (stderr, "exec-shield in etc/PROBLEMS for more information.\n");
+      fprintf (stderr, "**************************************************\n");
+    }
+#endif /* __linux__ */
+
   /* Bind `command-line-processed' to nil before dumping,
      so that the dumped Emacs will process its command line
      and set up to work with X windows if appropriate.  */
@@ -2162,6 +2262,12 @@ You must run Emacs in batch mode in order to dump it.  */)
   memory_warnings (my_edata, malloc_warning);
 #endif /* not WINDOWSNT */
 #endif
+#if !defined (SYSTEM_MALLOC) && defined (HAVE_GTK_AND_PTHREAD) && !defined SYNC_INPUT
+  /* Pthread may call malloc before main, and then we will get an endless
+     loop, because pthread_self (see alloc.c) calls malloc the first time
+     it is called on some systems.  */
+  reset_malloc_hooks ();
+#endif
 #ifdef DOUG_LEA_MALLOC
   malloc_state_ptr = malloc_get_state ();
 #endif
@@ -2210,7 +2316,7 @@ synchronize_locale (category, plocale, desired_locale)
     {
       *plocale = desired_locale;
       setlocale (category, (STRINGP (desired_locale)
-                           ? (char *)(SDATA (desired_locale))
+                           ? (char *) SDATA (desired_locale)
                            : ""));
     }
 }
@@ -2327,7 +2433,16 @@ syms_of_emacs ()
 Many arguments are deleted from the list as they are processed.  */);
 
   DEFVAR_LISP ("system-type", &Vsystem_type,
-              doc: /* Value is symbol indicating type of operating system you are using.  */);
++             doc: /* Value is symbol indicating type of operating system you are using.
++Special values:
++  `gnu/linux'   compiled for a GNU/Linux system.
++  `darwin'      compiled for Darwin (GNU-Darwin, Mac OS X, ...).
++  `macos'       compiled for Mac OS 9.
++  `ms-dos'      compiled as an MS-DOS application.
++  `windows-nt'  compiled as a native W32 application.
++  `cygwin'      compiled using the Cygwin library.
++  `vax-vms' or `axp-vms': compiled for a (Open)VMS system.
++Anything else indicates some sort of Unix system.  */);
   Vsystem_type = intern (SYSTEM_TYPE);
 
   DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
@@ -2356,18 +2471,6 @@ The hook is not run in batch mode, i.e., if `noninteractive' is non-nil.  */);
   empty_string = build_string ("");
   staticpro (&empty_string);
 
-#ifdef SIGUSR1
-  DEFVAR_LISP ("signal-USR1-hook", &Vsignal_USR1_hook,
-              doc: /* Hook to be run whenever emacs receives a USR1 signal.  */);
-  Vsignal_USR1_hook = Qnil;
-#ifdef SIGUSR2
-  DEFVAR_LISP ("signal-USR2-hook", &Vsignal_USR2_hook,
-              doc: /* Hook to be run whenever emacs receives a USR2 signal.  */);
-  Vsignal_USR2_hook = Qnil;
-#endif
-#endif
-
-
   DEFVAR_INT ("emacs-priority", &emacs_priority,
              doc: /* Priority for Emacs to run at.
 This value is effective only if set before Emacs is dumped,