Make it possible to run ./temacs.
[bpt/emacs.git] / src / emacs.c
index 4455e6b..3a7c5c0 100644 (file)
@@ -29,6 +29,8 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <setjmp.h>
 #include <unistd.h>
 
+#include "lisp.h"
+
 #ifdef WINDOWSNT
 #include <fcntl.h>
 #include <windows.h> /* just for w32.h */
@@ -41,7 +43,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <GNUstepBase/GSConfig.h>
 #endif
 
-#include "lisp.h"
 #include "commands.h"
 #include "intervals.h"
 #include "buffer.h"
@@ -64,6 +65,10 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "nsterm.h"
 #endif
 
+#ifdef HAVE_X_WINDOWS
+#include "xterm.h"
+#endif
+
 #ifdef HAVE_SETLOCALE
 #include <locale.h>
 #endif
@@ -127,21 +132,21 @@ int initialized;
 #ifdef DOUG_LEA_MALLOC
 /* Preserves a pointer to the memory allocated that copies that
    static data inside glibc's malloc.  */
-void *malloc_state_ptr;
+static void *malloc_state_ptr;
 /* From glibc, a routine that returns a copy of the malloc internal state.  */
 extern void *malloc_get_state (void);
 /* From glibc, a routine that overwrites the malloc internal state.  */
 extern int malloc_set_state (void*);
 /* Non-zero if the MALLOC_CHECK_ environment variable was set while
    dumping.  Used to work around a bug in glibc's malloc.  */
-int malloc_using_checking;
+static int malloc_using_checking;
 #endif
 
 Lisp_Object Qfile_name_handler_alist;
 
 Lisp_Object Qrisky_local_variable;
 
-/* If non-zero, emacs should not attempt to use a window-specific code,
+/* If non-zero, Emacs should not attempt to use a window-specific code,
    but instead should use the virtual terminal under which it was started.  */
 int inhibit_window_system;
 
@@ -164,12 +169,7 @@ 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
-   output a warning in dump-emacs.  */
-#define MAX_HEAP_BSS_DIFF (1024*1024)
-
 /* Nonzero means running Emacs without interactive terminal.  */
-
 int noninteractive;
 
 /* Nonzero means remove site-lisp directories from load-path.  */
@@ -187,7 +187,7 @@ char **initial_argv;
 int initial_argc;
 
 static void sort_args (int argc, char **argv);
-void syms_of_emacs (void);
+static void syms_of_emacs (void);
 
 /* MSVC needs each string be shorter than 2048 bytes, so the usage
    strings below are split to not overflow this limit.  */
@@ -249,14 +249,14 @@ Display options:\n\
 --border-color, -bd COLOR       main border color\n\
 --border-width, -bw WIDTH       width of main border\n\
 --color, --color=MODE           override color mode for character terminals;\n\
-                                  MODE defaults to `auto', and can also\n\
-                                  be `never', `auto', `always',\n\
+                                  MODE defaults to `auto', and\n\
+                                  can also be `never', `always',\n\
                                   or a mode name like `ansi8'\n\
 --cursor-color, -cr COLOR       color of the Emacs cursor indicating point\n\
 --font, -fn FONT                default font; must be fixed-width\n\
 --foreground-color, -fg COLOR   window foreground color\n\
 --fullheight, -fh               make the first frame high as the screen\n\
---fullscreen, -fs               make first frame fullscreen\n\
+--fullscreen, -fs               make the first frame fullscreen\n\
 --fullwidth, -fw                make the first frame wide as the screen\n\
 --maximized, -mm                make the first frame maximized\n\
 --geometry, -g GEOMETRY         window geometry\n\
@@ -289,7 +289,7 @@ section of the Emacs manual or the file BUGS.\n"
 
 \f
 /* Signal code for the fatal signal that was received.  */
-int fatal_error_code;
+static int fatal_error_code;
 
 /* Nonzero if handling a fatal error already.  */
 int fatal_error_in_progress;
@@ -297,7 +297,7 @@ int fatal_error_in_progress;
 /* If non-null, call this function from fatal_error_signal before
    committing suicide.  */
 
-void (*fatal_error_signal_hook) (void);
+static void (*fatal_error_signal_hook) (void);
 
 #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD
 /* When compiled with GTK and running under Gnome,
@@ -309,7 +309,10 @@ pthread_t main_thread;
 
 
 /* Handle bus errors, invalid instruction, etc.  */
-SIGTYPE
+#ifndef FLOAT_CATCH_SIGILL
+static
+#endif
+void
 fatal_error_signal (int sig)
 {
   SIGNAL_THREAD_CHECK (sig);
@@ -346,7 +349,7 @@ fatal_error_signal (int sig)
 #ifdef SIGDANGER
 
 /* Handler for SIGDANGER.  */
-SIGTYPE
+void
 memory_warning_signal (sig)
      int sig;
 {
@@ -382,7 +385,7 @@ static void
 init_cmdargs (int argc, char **argv, int skip_args)
 {
   register int i;
-  Lisp_Object name, dir, tem;
+  Lisp_Object name, dir, handler;
   int count = SPECPDL_INDEX ();
   Lisp_Object raw_name;
 
@@ -393,8 +396,8 @@ init_cmdargs (int argc, char **argv, int skip_args)
 
   /* Add /: to the front of the name
      if it would otherwise be treated as magic.  */
-  tem = Ffind_file_name_handler (raw_name, Qt);
-  if (! NILP (tem))
+  handler = Ffind_file_name_handler (raw_name, Qt);
+  if (! NILP (handler))
     raw_name = concat2 (build_string ("/:"), raw_name);
 
   Vinvocation_name = Ffile_name_nondirectory (raw_name);
@@ -411,8 +414,8 @@ init_cmdargs (int argc, char **argv, int skip_args)
        {
          /* Add /: to the front of the name
             if it would otherwise be treated as magic.  */
-         tem = Ffind_file_name_handler (found, Qt);
-         if (! NILP (tem))
+         handler = Ffind_file_name_handler (found, Qt);
+         if (! NILP (handler))
            found = concat2 (build_string ("/:"), found);
          Vinvocation_directory = Ffile_name_directory (found);
        }
@@ -547,21 +550,23 @@ static char dump_tz[] = "UtC0";
    Provide dummy definitions to avoid error.
    (We don't have any real constructors or destructors.)  */
 #ifdef __GNUC__
+
+/* Define a dummy function F.  Declare F too, to pacify gcc
+   -Wmissing-prototypes.  */
+#define DEFINE_DUMMY_FUNCTION(f) \
+  void f (void) EXTERNALLY_VISIBLE; void f (void) {}
+
 #ifndef GCC_CTORS_IN_LIBC
-void __do_global_ctors (void)
-{}
-void __do_global_ctors_aux (void)
-{}
-void __do_global_dtors (void)
-{}
+DEFINE_DUMMY_FUNCTION (__do_global_ctors)
+DEFINE_DUMMY_FUNCTION (__do_global_ctors_aux)
+DEFINE_DUMMY_FUNCTION (__do_global_dtors)
 /* GNU/Linux has a bug in its library; avoid an error.  */
 #ifndef GNU_LINUX
-char * __CTOR_LIST__[2] = { (char *) (-1), 0 };
+char * __CTOR_LIST__[2] EXTERNALLY_VISIBLE = { (char *) (-1), 0 };
 #endif
-char * __DTOR_LIST__[2] = { (char *) (-1), 0 };
+char * __DTOR_LIST__[2] EXTERNALLY_VISIBLE = { (char *) (-1), 0 };
 #endif /* GCC_CTORS_IN_LIBC */
-void __main (void)
-{}
+DEFINE_DUMMY_FUNCTION (__main)
 #endif /* __GNUC__ */
 #endif /* ORDINARY_LINK */
 
@@ -576,7 +581,8 @@ void __main (void)
    enough information to do it right.  */
 
 static int
-argmatch (char **argv, int argc, const char *sstr, const char *lstr, int minlen, char **valptr, int *skipptr)
+argmatch (char **argv, int argc, const char *sstr, const char *lstr,
+          int minlen, char **valptr, int *skipptr)
 {
   char *p = NULL;
   int arglen;
@@ -948,7 +954,7 @@ main (int argc, char **argv)
       /* Convert --script to -scriptload, un-skip it, and sort again
         so that it will be handled in proper sequence.  */
       /* FIXME broken for --script=FILE - is that supposed to work?  */
-      argv[skip_args - 1] = "-scriptload";
+      argv[skip_args - 1] = (char *) "-scriptload";
       skip_args -= 2;
       sort_args (argc, argv);
     }
@@ -1347,7 +1353,7 @@ main (int argc, char **argv)
 
        for (j = 0; j < count_before + 1; j++)
          new[j] = argv[j];
-       new[count_before + 1] = "-d";
+       new[count_before + 1] = (char *) "-d";
        new[count_before + 2] = displayname;
        for (j = count_before + 2; j <argc; j++)
          new[j + 1] = argv[j];
@@ -1357,7 +1363,7 @@ main (int argc, char **argv)
     /* Change --display to -d, when its arg is separate.  */
     else if (displayname != 0 && skip_args > count_before
             && argv[count_before + 1][1] == '-')
-      argv[count_before + 1] = "-d";
+      argv[count_before + 1] = (char *) "-d";
 
     if (! no_site_lisp)
       {
@@ -1417,8 +1423,11 @@ main (int argc, char **argv)
     syms_of_callproc ();
   /* egetenv is a pretty low-level facility, which may get called in
      many circumstances; it seems flimsy to put off initializing it
-     until calling init_callproc.  */
-  set_initial_environment ();
+     until calling init_callproc.  Do not do it when dumping.  */
+  if (initialized || ((strcmp (argv[argc-1], "dump") != 0
+                      && strcmp (argv[argc-1], "bootstrap") != 0)))
+    set_initial_environment ();
+
   /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
      if this is not done.  Do it after set_global_environment so that we
      don't pollute Vglobal_environment.  */
@@ -1699,7 +1708,7 @@ struct standard_args
   int nargs;
 };
 
-const struct standard_args standard_args[] =
+static const struct standard_args standard_args[] =
 {
   { "-version", "--version", 150, 0 },
   { "-chdir", "--chdir", 130, 1 },
@@ -1971,17 +1980,23 @@ all of which are called before Emacs is actually killed.  */)
   (Lisp_Object arg)
 {
   struct gcpro gcpro1;
+  Lisp_Object hook;
 
   GCPRO1 (arg);
 
   if (feof (stdin))
     arg = Qt;
 
-  if (!NILP (Vrun_hooks))
-    call1 (Vrun_hooks, intern ("kill-emacs-hook"));
+  hook = intern ("kill-emacs-hook");
+  Frun_hooks (1, &hook);
 
   UNGCPRO;
 
+#ifdef HAVE_X_WINDOWS
+  /* Transfer any clipboards we own to the clipboard manager.  */
+  x_clipboard_manager_save_all ();
+#endif
+
   shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
 
   /* If we have an auto-save list file,
@@ -2084,9 +2099,7 @@ shut_down_emacs (int sig, int no_x, Lisp_Object stuff)
 \f
 #ifndef CANNOT_DUMP
 
-/* FIXME: maybe this should go into header file, config.h seems the
-   only one appropriate. */
-extern int unexec (const char *, const char *);
+#include "unexec.h"
 
 DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
        doc: /* Dump current state of Emacs into executable file FILENAME.
@@ -2096,7 +2109,6 @@ This is used in the file `loadup.el' when building Emacs.
 You must run Emacs in batch mode in order to dump it.  */)
   (Lisp_Object filename, Lisp_Object symfile)
 {
-  extern char my_edata[];
   Lisp_Object tem;
   Lisp_Object symbol;
   int count = SPECPDL_INDEX ();
@@ -2107,6 +2119,10 @@ You must run Emacs in batch mode in order to dump it.  */)
     error ("Dumping Emacs works only in batch mode");
 
 #ifdef GNU_LINUX
+
+  /* Warn if the gap between BSS end and heap start is larger than this.  */
+# define MAX_HEAP_BSS_DIFF (1024*1024)
+
   if (heap_bss_diff > MAX_HEAP_BSS_DIFF)
     {
       fprintf (stderr, "**************************************************\n");
@@ -2153,7 +2169,10 @@ You must run Emacs in batch mode in order to dump it.  */)
 #ifndef WINDOWSNT
   /* On Windows, this was done before dumping, and that once suffices.
      Meanwhile, my_edata is not valid on Windows.  */
-  memory_warnings (my_edata, malloc_warning);
+  {
+    extern char my_edata[];
+    memory_warnings (my_edata, malloc_warning);
+  }
 #endif /* not WINDOWSNT */
 #if defined (HAVE_GTK_AND_PTHREAD) && !defined SYNC_INPUT
   /* Pthread may call malloc before main, and then we will get an endless
@@ -2313,6 +2332,7 @@ from the parent process and its tty file descriptors.  */)
   (void)
 {
   int nfd;
+  int err = 0;
 
   if (!IS_DAEMON)
     error ("This function can only be called if emacs is run as a daemon");
@@ -2325,10 +2345,11 @@ from the parent process and its tty file descriptors.  */)
 
   /* Get rid of stdin, stdout and stderr.  */
   nfd = open ("/dev/null", O_RDWR);
-  dup2 (nfd, 0);
-  dup2 (nfd, 1);
-  dup2 (nfd, 2);
-  close (nfd);
+  err |= nfd < 0;
+  err |= dup2 (nfd, 0) < 0;
+  err |= dup2 (nfd, 1) < 0;
+  err |= dup2 (nfd, 2) < 0;
+  err |= close (nfd) != 0;
 
   /* Closing the pipe will notify the parent that it can exit.
      FIXME: In case some other process inherited the pipe, closing it here
@@ -2337,10 +2358,13 @@ from the parent process and its tty file descriptors.  */)
      Instead, we should probably close the pipe in start-process and
      call-process to make sure the pipe is never inherited by
      subprocesses.  */
-  write (daemon_pipe[1], "\n", 1);
-  close (daemon_pipe[1]);
+  err |= write (daemon_pipe[1], "\n", 1) < 0;
+  err |= close (daemon_pipe[1]) != 0;
   /* Set it to an invalid value so we know we've already run this function.  */
   daemon_pipe[1] = -1;
+
+  if (err)
+    error ("I/O error during daemon initialization");
   return Qt;
 }
 
@@ -2380,7 +2404,7 @@ Special values:
 Anything else (in Emacs 24.1, the possibilities are: aix, berkeley-unix,
 hpux, irix, usg-unix-v) indicates some sort of Unix system.  */);
   Vsystem_type = intern_c_string (SYSTEM_TYPE);
-  /* Above values are from SYSTEM_TYPE in src/s/*.h.  */
+  /* The above values are from SYSTEM_TYPE in include files under src/s.  */
 
   DEFVAR_LISP ("system-configuration", Vsystem_configuration,
               doc: /* Value is string indicating configuration Emacs was built for.