Remove support for DJGPP v1.x (bug#5813).
[bpt/emacs.git] / src / emacs.c
index 517b39e..0d90776 100644 (file)
@@ -1,7 +1,7 @@
 /* Fully extensible Emacs, running on Unix, intended for GNU.
    Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 1998, 1999,
-                 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
-                 Free Software Foundation, Inc.
+                 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
+                 2010  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -26,16 +26,13 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <sys/types.h>
 #include <sys/file.h>
-
-#ifdef VMS
-#include <ssdef.h>
-#endif
+#include <setjmp.h>
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 
-#ifdef BSD_SYSTEM
+#ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
 #endif
 
@@ -46,6 +43,11 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "w32heap.h" /* for prototype of sbrk */
 #endif
 
+#ifdef NS_IMPL_GNUSTEP
+/* At least under Debian, GSConfig is in a subdirectory.  --Stef  */
+#include <GNUstepBase/GSConfig.h>
+#endif
+
 #include "lisp.h"
 #include "commands.h"
 #include "intervals.h"
@@ -98,14 +100,14 @@ int gdb_use_lsb = 1;
 #else
 int gdb_use_lsb = 0;
 #endif
-#ifdef NO_UNION_TYPE
+#ifndef USE_LISP_UNION_TYPE
 int gdb_use_union = 0;
 #else
 int gdb_use_union = 1;
 #endif
 EMACS_INT gdb_valbits = VALBITS;
 EMACS_INT gdb_gctypebits = GCTYPEBITS;
-#ifdef DATA_SEG_BITS
+#if defined (DATA_SEG_BITS) && ! defined (USE_LSB_TAG)
 EMACS_INT gdb_data_seg_bits = DATA_SEG_BITS;
 #else
 EMACS_INT gdb_data_seg_bits = 0;
@@ -130,6 +132,9 @@ Lisp_Object Vinvocation_directory;
    nil means get them only from PATH_LOADSEARCH.  */
 Lisp_Object Vinstallation_directory;
 
+/* The values of `current-time' before and after Emacs initialization.  */
+Lisp_Object Vbefore_init_time, Vafter_init_time;
+
 /* Hook run by `kill-emacs' before it does really anything.  */
 Lisp_Object Vkill_emacs_hook;
 
@@ -152,7 +157,7 @@ void *malloc_state_ptr;
 extern void *malloc_get_state ();
 /* From glibc, a routine that overwrites the malloc internal state.  */
 extern int malloc_set_state ();
-/* Non-zero if the MALLOC_CHECK_ enviroment variable was set while
+/* 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;
 #endif
@@ -193,7 +198,7 @@ int running_asynch_code;
 extern int inherited_pgroup;
 #endif
 
-#ifdef HAVE_X_WINDOWS
+#if defined(HAVE_X_WINDOWS) || defined(HAVE_NS)
 /* If non-zero, -d was specified, meaning we're using some window system.  */
 int display_arg;
 #endif
@@ -208,8 +213,8 @@ 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.  */
+/* 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)
 
 
@@ -221,13 +226,6 @@ 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.  */
-
-unsigned int bss_end = 0;
-#endif
-
 /* Nonzero means running Emacs without interactive terminal.  */
 
 int noninteractive;
@@ -238,6 +236,16 @@ int noninteractive;
 
 int noninteractive1;
 
+/* Nonzero means Emacs was run in --quick mode.  */
+int inhibit_x_resources;
+
+/* Name for the server started by the daemon.*/
+static char *daemon_name;
+
+/* Pipe used to send exit notification to the daemon parent at
+   startup.  */
+int daemon_pipe[2];
+
 /* Save argv and argc.  */
 char **initial_argv;
 int initial_argc;
@@ -260,9 +268,9 @@ read the main documentation for these command-line arguments.\n\
 Initialization options:\n\
 \n\
 --batch                     do not do interactive display; implies -q\n\
+--daemon                    start a server in the background\n\
 --debug-init                enable Emacs Lisp debugger for init file\n\
 --display, -d DISPLAY       use X server DISPLAY\n\
---multibyte, --no-unibyte   inhibit the effect of EMACS_UNIBYTE\n\
 --no-desktop                do not load a saved desktop\n\
 --no-init-file, -q          load neither ~/.emacs nor default.el\n\
 --no-shared-memory, -nl     do not use shared memory\n\
@@ -272,7 +280,6 @@ Initialization options:\n\
 --quick, -Q                 equivalent to -q --no-site-file --no-splash\n\
 --script FILE               run FILE as an Emacs Lisp script\n\
 --terminal, -t DEVICE       use DEVICE for terminal I/O\n\
---unibyte, --no-multibyte   run Emacs in unibyte mode\n\
 --user, -u USER             load ~USER/.emacs instead of your own\n\
 \n%s"
 
@@ -280,8 +287,8 @@ Initialization options:\n\
 Action options:\n\
 \n\
 FILE                    visit FILE using find-file\n\
-+LINE                  go to line LINE in next FILE\n\
-+LINE:COLUMN           go to line LINE, column COLUMN, in next FILE\n\
++LINE                   go to line LINE in next FILE\n\
++LINE:COLUMN            go to line LINE, column COLUMN, in next FILE\n\
 --directory, -L DIR     add DIR to variable load-path\n\
 --eval EXPR             evaluate Emacs Lisp expression EXPR\n\
 --execute EXPR          evaluate Emacs Lisp expression EXPR\n\
@@ -312,6 +319,7 @@ Display options:\n\
 --fullheight, -fh               make the first frame high as the screen\n\
 --fullscreen, -fs               make 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\
 --no-bitmap-icon, -nbi          do not use picture of gnu for Emacs icon\n\
 --iconic                        start Emacs in iconified state\n\
@@ -337,7 +345,7 @@ abbreviation for a --option.\n\
 Various environment variables and window system resources also affect\n\
 Emacs' operation.  See the main documentation.\n\
 \n\
-Report bugs to %s.  First, please see the Bugs\n\
+Report bugs to bug-gnu-emacs@gnu.org.  First, please see the Bugs\n\
 section of the Emacs manual or the file BUGS.\n"
 
 \f
@@ -353,8 +361,8 @@ int fatal_error_in_progress;
 void (*fatal_error_signal_hook) P_ ((void));
 
 #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD
-/* When compiled with GTK and running under Gnome, or Carbon under Mac
-   OS X, multiple threads may be created.  Keep track of our main
+/* When compiled with GTK and running under Gnome,
+   multiple threads may be created.  Keep track of our main
    thread to make sure signals are delivered to it (see syssignal.h).  */
 
 pthread_t main_thread;
@@ -380,9 +388,6 @@ fatal_error_signal (sig)
       shut_down_emacs (sig, 0, Qnil);
     }
 
-#ifdef VMS
-  LIB$STOP (SS$_ABORT);
-#else
   /* Signal the same code; this time it will really be fatal.
      Remember that since we're in a signal handler, the signal we're
      going to send is probably blocked, so we have to unblock it if we
@@ -395,7 +400,6 @@ fatal_error_signal (sig)
     fatal_error_signal_hook ();
 
   kill (getpid (), fatal_error_code);
-#endif /* not VMS */
 }
 
 #ifdef SIGDANGER
@@ -593,14 +597,6 @@ DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
 }
 
 \f
-#ifdef VMS
-#ifdef LINK_CRTL_SHARE
-#ifdef SHARABLE_LIB_BUG
-extern noshare char **environ;
-#endif /* SHARABLE_LIB_BUG */
-#endif /* LINK_CRTL_SHARE */
-#endif /* VMS */
-
 #ifdef HAVE_TZSET
 /* A valid but unlikely value for the TZ environment value.
    It is OK (though a bit slower) if the user actually chooses this value.  */
@@ -750,53 +746,9 @@ void (*__malloc_initialize_hook) () = malloc_initialize_hook;
 #endif /* DOUG_LEA_MALLOC */
 
 
-#define REPORT_EMACS_BUG_ADDRESS "bug-gnu-emacs@gnu.org"
-#define REPORT_EMACS_BUG_PRETEST_ADDRESS "emacs-pretest-bug@gnu.org"
-
-/* This function is used to determine an address to which bug report should
-   be sent.  */
-
-char *
-bug_reporting_address ()
-{
-  int count = 0;
-  Lisp_Object temp;
-  char *string;
-
-  temp = Fsymbol_value (intern ("emacs-version"));
-
-  /* When `emacs-version' is invalid, use normal address.  */
-  if (!STRINGP(temp))
-    return REPORT_EMACS_BUG_ADDRESS;
-
-  string = SDATA (temp);
-
-  /* Count dots in `emacs-version'.  */
-  while (*string)
-    {
-      if (*string == '.')
-       count++;
-      string++;
-    }
-
-  /* When `emacs-version' has at least three dots, it is development or
-     pretest version of Emacs.  */
-  return count >= 3 ? REPORT_EMACS_BUG_PRETEST_ADDRESS : REPORT_EMACS_BUG_ADDRESS;
-}
-
-
 /* ARGSUSED */
 int
-main (argc, argv
-#ifdef VMS
-, envp
-#endif
-)
-     int argc;
-     char **argv;
-#ifdef VMS
-     char **envp;
-#endif
+main (int argc, char **argv)
 {
 #if GC_MARK_STACK
   Lisp_Object dummy;
@@ -812,12 +764,21 @@ main (argc, argv
 #endif
   int no_loadup = 0;
   char *junk = 0;
+  char *dname_arg = 0;
+#ifdef NS_IMPL_COCOA
+  char dname_arg2[80];
+#endif
 
 #if GC_MARK_STACK
   extern Lisp_Object *stack_base;
   stack_base = &dummy;
 #endif
 
+#if defined (USE_GTK) && defined (G_SLICE_ALWAYS_MALLOC)
+  /* This is used by the Cygwin build.  */
+  setenv ("G_SLICE", "always-malloc", 1);
+#endif
+
   if (!initialized)
     {
       extern char my_endbss[];
@@ -829,16 +790,13 @@ main (argc, argv
       heap_bss_diff = (char *)my_heap_start - max (my_endbss, my_endbss_static);
     }
 
-#ifdef LINUX_SBRK_BUG
-  __sbrk (1);
-#endif
-
 #ifdef RUN_TIME_REMAP
   if (initialized)
     run_time_remap (argv[0]);
 #endif
 
-#ifdef MAC_OSX
+/* If using unexmacosx.c (set by s/darwin.h), we must do this. */
+#ifdef DARWIN_OS
   if (!initialized)
     unexec_init_emacs_zone ();
 #endif
@@ -853,8 +811,8 @@ main (argc, argv
       && initialized)
     {
       Lisp_Object tem, tem2;
-      tem = Fsymbol_value (intern ("emacs-version"));
-      tem2 = Fsymbol_value (intern ("emacs-copyright"));
+      tem = Fsymbol_value (intern_c_string ("emacs-version"));
+      tem2 = Fsymbol_value (intern_c_string ("emacs-copyright"));
       if (!STRINGP (tem))
        {
          fprintf (stderr, "Invalid value of `emacs-version'\n");
@@ -879,29 +837,24 @@ 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)
+      && ! getenv ("EMACS_HEAP_EXEC"))
     {
-      if (! getenv ("EMACS_HEAP_EXEC"))
-        {
-          /* Set this so we only do this once.  */
-          putenv("EMACS_HEAP_EXEC=true");
+      /* Set this so we only do this once.  */
+      putenv("EMACS_HEAP_EXEC=true");
 
-         /* A flag to turn off address randomization which is introduced
-          in linux kernel shipped with fedora core 4 */
+      /* A flag to turn off address randomization which is introduced
+         in linux kernel shipped with fedora core 4 */
 #define ADD_NO_RANDOMIZE 0x0040000
-         personality (PER_LINUX32 | ADD_NO_RANDOMIZE);
+      personality (PER_LINUX32 | ADD_NO_RANDOMIZE);
 #undef  ADD_NO_RANDOMIZE
 
-          execvp (argv[0], argv);
+      execvp (argv[0], argv);
 
-          /* If the exec fails, try to dump anyway.  */
-          perror ("execvp");
-        }
+      /* If the exec fails, try to dump anyway.  */
+      perror ("execvp");
     }
 #endif /* HAVE_PERSONALITY_LINUX32 */
 
@@ -922,52 +875,6 @@ main (argc, argv
     }
 #endif
 
-#ifdef NeXT
-  {
-    extern int malloc_cookie;
-    /* This helps out unexnext.c.  */
-    if (initialized)
-      if (malloc_jumpstart (malloc_cookie) != 0)
-       printf ("malloc jumpstart failed!\n");
-  }
-#endif /* NeXT */
-
-#ifdef MAC_OSX
-  /* Skip process serial number passed in the form -psn_x_y as
-     command-line argument.  The WindowServer adds this option when
-     Emacs is invoked from the Finder or by the `open' command.  In
-     these cases, the working directory becomes `/', so we change it
-     to the user's home directory.  */
-  if (argc > skip_args + 1 && strncmp (argv[skip_args+1], "-psn_", 5) == 0)
-    {
-      chdir (getenv ("HOME"));
-      skip_args++;
-    }
-#endif /* MAC_OSX */
-
-#ifdef VMS
-  /* If -map specified, map the data file in.  */
-  {
-    char *file;
-    if (argmatch (argv, argc, "-map", "--map-data", 3, &file, &skip_args))
-      mapin_data (file);
-  }
-
-#ifdef LINK_CRTL_SHARE
-#ifdef SHARABLE_LIB_BUG
-  /* Bletcherous shared libraries!  */
-  if (!stdin)
-    stdin = fdopen (0, "r");
-  if (!stdout)
-    stdout = fdopen (1, "w");
-  if (!stderr)
-    stderr = fdopen (2, "w");
-  if (!environ)
-    environ = envp;
-#endif /* SHARABLE_LIB_BUG */
-#endif /* LINK_CRTL_SHARE */
-#endif /* VMS */
-
 #if defined (HAVE_SETRLIMIT) && defined (RLIMIT_STACK)
   /* Extend the stack space available.
      Don't do that if dumping, since some systems (e.g. DJGPP)
@@ -1011,11 +918,6 @@ main (argc, argv
   /* Record (approximately) where the stack begins.  */
   stack_bottom = &stack_bottom_variable;
 
-#ifdef USG_SHARED_LIBRARIES
-  if (bss_end)
-    brk ((void *)bss_end);
-#endif
-
   clearerr (stdin);
 
 #ifndef SYSTEM_MALLOC
@@ -1043,7 +945,6 @@ main (argc, argv
 #endif /* MSDOS || WINDOWSNT */
 
 #ifdef MSDOS
-#if __DJGPP__ >= 2
   if (!isatty (fileno (stdin)))
     setmode (fileno (stdin), O_BINARY);
   if (!isatty (fileno (stdout)))
@@ -1051,11 +952,6 @@ main (argc, argv
       fflush (stdout);
       setmode (fileno (stdout), O_BINARY);
     }
-#else  /* not __DJGPP__ >= 2 */
-  (stdin)->_flag &= ~_IOTEXT;
-  (stdout)->_flag &= ~_IOTEXT;
-  (stderr)->_flag &= ~_IOTEXT;
-#endif /* not __DJGPP__ >= 2 */
 #endif /* MSDOS */
 
 #ifdef SET_EMACS_PRIORITY
@@ -1140,10 +1036,145 @@ main (argc, argv
     {
       printf (USAGE1, argv[0], USAGE2);
       printf (USAGE3);
-      printf (USAGE4, bug_reporting_address ());
+      printf (USAGE4);
       exit (0);
     }
 
+  if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)
+      || argmatch (argv, argc, "-daemon", "--daemon", 5, &dname_arg, &skip_args))
+    {
+#ifndef DOS_NT
+      pid_t f;
+
+      /* Start as a daemon: fork a new child process which will run the
+        rest of the initialization code, then exit.
+
+        Detaching a daemon requires the following steps:
+        - fork
+        - setsid
+        - exit the parent
+        - close the tty file-descriptors
+
+        We only want to do the last 2 steps once the daemon is ready to
+        serve requests, i.e. after loading .emacs (initialization).
+        OTOH initialization may start subprocesses (e.g. ispell) and these
+        should be run from the proper process (the one that will end up
+        running as daemon) and with the proper "session id" in order for
+        them to keep working after detaching, so fork and setsid need to be
+        performed before initialization.
+
+        We want to avoid exiting before the server socket is ready, so
+        use a pipe for synchronization.  The parent waits for the child
+        to close its end of the pipe (using `daemon-initialized')
+        before exiting.  */
+      if (pipe (daemon_pipe) == -1)
+       {
+         fprintf (stderr, "Cannot pipe!\n");
+         exit (1);
+       }
+
+#ifndef NS_IMPL_COCOA
+      f = fork ();
+#else /* NS_IMPL_COCOA */
+      /* Under Cocoa we must do fork+exec as CoreFoundation lib fails in
+         forked process: http://developer.apple.com/ReleaseNotes/
+                                  CoreFoundation/CoreFoundation.html)
+         We mark being in the exec'd process by a daemon name argument of
+         form "--daemon=\nFD0,FD1\nNAME" where FD are the pipe file descriptors,
+         NAME is the original daemon name, if any. */
+      if (!dname_arg || !strchr (dname_arg, '\n'))
+         f = fork ();  /* in orig */
+      else
+         f = 0;  /* in exec'd */
+#endif /* NS_IMPL_COCOA */
+      if (f > 0)
+       {
+         int retval;
+         char buf[1];
+
+         /* Close unused writing end of the pipe.  */
+         close (daemon_pipe[1]);
+
+         /* Just wait for the child to close its end of the pipe.  */
+         do
+           {
+             retval = read (daemon_pipe[0], &buf, 1);
+           }
+         while (retval == -1 && errno == EINTR);
+
+         if (retval < 0)
+           {
+             fprintf (stderr, "Error reading status from child\n");
+             exit (1);
+           }
+         else if (retval == 0)
+           {
+             fprintf (stderr, "Error: server did not start correctly\n");
+             exit (1);
+           }
+
+         close (daemon_pipe[0]);
+         exit (0);
+       }
+      if (f < 0)
+       {
+         fprintf (stderr, "Cannot fork!\n");
+         exit (1);
+       }
+
+#ifdef NS_IMPL_COCOA
+      {
+        /* In orig process, forked as child, OR in exec'd. */
+        if (!dname_arg || !strchr (dname_arg, '\n'))
+          {  /* In orig, child: now exec w/special daemon name. */
+            char fdStr[80];
+
+            if (dname_arg && strlen (dname_arg) > 70)
+              {
+                fprintf (stderr, "daemon: child name too long\n");
+                exit (1);
+              }
+
+            sprintf (fdStr, "--daemon=\n%d,%d\n%s", daemon_pipe[0],
+                     daemon_pipe[1], dname_arg ? dname_arg : "");
+            argv[skip_args] = fdStr;
+
+            execv (argv[0], argv);
+            fprintf (stderr, "emacs daemon: exec failed: %d\n", errno);
+            exit (1);
+          }
+
+        /* In exec'd: parse special dname into pipe and name info. */
+        if (!dname_arg || !strchr (dname_arg, '\n')
+            || strlen (dname_arg) < 1 || strlen (dname_arg) > 70)
+          {
+            fprintf (stderr, "emacs daemon: daemon name absent or too long\n");
+            exit(1);
+          }
+        dname_arg2[0] = '\0';
+        sscanf (dname_arg, "\n%d,%d\n%s", &(daemon_pipe[0]), &(daemon_pipe[1]),
+                dname_arg2);
+        dname_arg = strlen (dname_arg2) ? dname_arg2 : NULL;
+      }
+#endif /* NS_IMPL_COCOA */
+
+      if (dname_arg)
+               daemon_name = xstrdup (dname_arg);
+      /* Close unused reading end of the pipe.  */
+      close (daemon_pipe[0]);
+      /* Make sure that the used end of the pipe is closed on exec, so
+        that it is not accessible to programs started from .emacs.  */
+      fcntl (daemon_pipe[1], F_SETFD, FD_CLOEXEC);
+
+#ifdef HAVE_SETSID
+      setsid();
+#endif
+#else /* DOS_NT */
+      fprintf (stderr, "This platform does not support the -daemon flag.\n");
+      exit (1);
+#endif /* DOS_NT */
+    }
+
   if (! noninteractive)
     {
 #ifdef BSD_PGRPS
@@ -1300,27 +1331,6 @@ main (argc, argv
         CANNOT_DUMP is defined.  */
       syms_of_keyboard ();
 
-#ifdef MAC_OS8
-      /* init_window_once calls make_terminal_frame which on Mac OS
-         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_term_init, and init_keyboard have already
-         been called.  */
-      syms_of_textprop ();
-      syms_of_macfns ();
-      syms_of_ccl ();
-      syms_of_fontset ();
-      syms_of_macterm ();
-      syms_of_macmenu ();
-      syms_of_macselect ();
-      syms_of_search ();
-      syms_of_frame ();
-
-      init_atimer ();
-      mac_term_init (build_string ("Mac"), NULL, NULL);
-      init_keyboard ();
-#endif
       /* Called before syms_of_fileio, because it sets up Qerror_condition.  */
       syms_of_data ();
       syms_of_fileio ();
@@ -1333,7 +1343,6 @@ main (argc, argv
       syms_of_coding ();       /* This should be after syms_of_fileio.  */
 
       init_window_once ();     /* Init the window system.  */
-      init_fileio_once ();     /* Must precede any path manipulation.  */
 #ifdef HAVE_WINDOW_SYSTEM
       init_fringe_once ();     /* Swap bitmaps if necessary. */
 #endif /* HAVE_WINDOW_SYSTEM */
@@ -1386,8 +1395,8 @@ main (argc, argv
          Lisp_Object old_log_max;
          Lisp_Object symbol, tail;
 
-         symbol = intern ("default-enable-multibyte-characters");
-         Fset (symbol, Qnil);
+         symbol = intern_c_string ("enable-multibyte-characters");
+         Fset_default (symbol, Qnil);
 
          if (initialized)
            {
@@ -1414,12 +1423,35 @@ main (argc, argv
                  set_buffer_temp (current);
                }
            }
+         message ("Warning: unibyte sessions are obsolete and will disappear");
        }
     }
 
   no_loadup
     = argmatch (argv, argc, "-nl", "--no-loadup", 6, NULL, &skip_args);
 
+#ifdef HAVE_NS
+  ns_alloc_autorelease_pool();
+  if (!noninteractive)
+    {
+#ifdef NS_IMPL_COCOA
+      if (skip_args < argc)
+        {
+          if (!strncmp(argv[skip_args], "-psn", 4))
+            {
+              skip_args += 1;
+              chdir (getenv ("HOME"));
+            }
+          else if (skip_args+1 < argc && !strncmp(argv[skip_args+1], "-psn", 4))
+            {
+              skip_args += 2;
+              chdir (getenv ("HOME"));
+            }
+        }
+#endif  /* COCOA */
+    }
+#endif /* HAVE_NS */
+
 #ifdef HAVE_X_WINDOWS
   /* Stupid kludge to catch command-line display spec.  We can't
      handle this argument entirely in window system dependent code
@@ -1492,9 +1524,11 @@ main (argc, argv
   init_ntproc ();      /* must precede init_editfns.  */
 #endif
 
-#if defined (MAC_OSX) && defined (HAVE_CARBON)
+#ifdef HAVE_NS
+#ifndef CANNOT_DUMP
   if (initialized)
-    init_mac_osx_environment ();
+#endif
+    ns_init_paths ();
 #endif
 
   /* egetenv is a pretty low-level facility, which may get called in
@@ -1505,7 +1539,7 @@ main (argc, argv
      if this is not done.  Do it after set_global_environment so that we
      don't pollute Vglobal_environment.  */
   /* Setting LANG here will defeat the startup locale processing...  */
-#ifdef AIX3_2
+#ifdef AIX
   putenv ("LANG=C");
 #endif
 
@@ -1532,11 +1566,8 @@ main (argc, argv
 
   if (!initialized)
     {
-      /* The basic levels of Lisp must come first.  */
-      /* And data must come first of all
-        for the sake of symbols like error-message.  */
-      /* Called before init_window_once for Mac OS Classic.  */
-      syms_of_data ();
+      /* The basic levels of Lisp must come first.  Note that
+        syms_of_data and some others have already been called.  */
       syms_of_chartab ();
       syms_of_lread ();
       syms_of_print ();
@@ -1582,9 +1613,6 @@ main (argc, argv
 #endif
       syms_of_textprop ();
       syms_of_composite ();
-#ifdef VMS
-      syms_of_vmsproc ();
-#endif /* VMS */
 #ifdef WINDOWSNT
       syms_of_ntproc ();
 #endif /* WINDOWSNT */
@@ -1598,7 +1626,9 @@ main (argc, argv
 #ifdef HAVE_X_WINDOWS
       syms_of_xterm ();
       syms_of_xfns ();
+      syms_of_xmenu ();
       syms_of_fontset ();
+      syms_of_xsettings ();
 #ifdef HAVE_X_SM
       syms_of_xsmfns ();
 #endif
@@ -1609,13 +1639,6 @@ main (argc, argv
 
       syms_of_menu ();
 
-#ifndef HAVE_NTGUI
-#ifndef MAC_OS
-      /* Called before init_window_once for Mac OS Classic.  */
-      syms_of_xmenu ();
-#endif
-#endif
-
 #ifdef HAVE_NTGUI
       syms_of_w32term ();
       syms_of_w32fns ();
@@ -1624,13 +1647,17 @@ main (argc, argv
       syms_of_fontset ();
 #endif /* HAVE_NTGUI */
 
-#if defined (MAC_OSX) && defined (HAVE_CARBON)
-      syms_of_macterm ();
-      syms_of_macfns ();
-      syms_of_macmenu ();
-      syms_of_macselect ();
+#ifdef MSDOS
+      syms_of_xmenu ();
+#endif /* MSDOS */
+
+#ifdef HAVE_NS
+      syms_of_nsterm ();
+      syms_of_nsfns ();
+      syms_of_nsmenu ();
+      syms_of_nsselect ();
       syms_of_fontset ();
-#endif /* MAC_OSX && HAVE_CARBON */
+#endif /* HAVE_NS */
 
 #ifdef HAVE_DBUS
       syms_of_dbusbind ();
@@ -1667,16 +1694,8 @@ main (argc, argv
   init_editfns (); /* init_process uses Voperating_system_release. */
   init_process (); /* init_display uses add_keyboard_wait_descriptor. */
   init_keyboard ();    /* This too must precede init_sys_modes.  */
-#ifdef VMS
-  init_vmsproc ();     /* And this too.  */
-#endif /* VMS */
   if (!noninteractive)
-    {
-#ifdef VMS
-      init_vms_input ();/* init_display calls get_tty_size, that needs this.  */
-#endif /* VMS */
-      init_display (); /* Determine terminal type.  Calls init_sys_modes.  */
-    }
+    init_display ();   /* Determine terminal type.  Calls init_sys_modes.  */
   init_fns ();
   init_xdisp ();
 #ifdef HAVE_WINDOW_SYSTEM
@@ -1685,24 +1704,22 @@ main (argc, argv
 #endif /* HAVE_WINDOW_SYSTEM */
   init_macros ();
   init_floatfns ();
-#ifdef VMS
-  init_vmsfns ();
-#endif /* VMS */
 #ifdef HAVE_SOUND
   init_sound ();
 #endif
   init_window ();
-
+  init_font ();
+  
   if (!initialized)
     {
       char *file;
       /* Handle -l loadup, args passed by Makefile.  */
       if (argmatch (argv, argc, "-l", "--load", 3, &file, &skip_args))
-       Vtop_level = Fcons (intern ("load"),
+       Vtop_level = Fcons (intern_c_string ("load"),
                            Fcons (build_string (file), Qnil));
       /* Unless next switch is -nl, load "loadup.el" first thing.  */
       if (! no_loadup)
-       Vtop_level = Fcons (intern ("load"),
+       Vtop_level = Fcons (intern_c_string ("load"),
                            Fcons (build_string ("loadup.el"), Qnil));
     }
 
@@ -1729,9 +1746,7 @@ main (argc, argv
   /* Set up for profiling.  This is known to work on FreeBSD,
      GNU/Linux and MinGW.  It might work on some other systems too.
      Give it a try and tell us if it works on your system.  To compile
-     for profiling, add -pg to the switches your platform uses in
-     CFLAGS and LDFLAGS.  For example:
-       `make CFLAGS="-pg -g -O -DPROFILING=1" LDFLAGS="-pg -g"'.  */
+     for profiling, use the configure option --enable-profiling.  */
 #if defined (__FreeBSD__) || defined (GNU_LINUX) || defined(__MINGW32__)
 #ifdef PROFILING
   if (initialized)
@@ -1781,26 +1796,21 @@ main (argc, argv
 
 struct standard_args
 {
-  char *name;
-  char *longname;
+  const char *name;
+  const char *longname;
   int priority;
   int nargs;
 };
 
-struct standard_args standard_args[] =
+const struct standard_args standard_args[] =
 {
   { "-version", "--version", 150, 0 },
-#ifdef HAVE_SHM
-  { "-nl", "--no-shared-memory", 140, 0 },
-#endif
-#ifdef VMS
-  { "-map", "--map-data", 130, 0 },
-#endif
   { "-t", "--terminal", 120, 1 },
   { "-nw", "--no-window-system", 110, 0 },
   { "-nw", "--no-windows", 110, 0 },
   { "-batch", "--batch", 100, 0 },
   { "-script", "--script", 100, 1 },
+  { "-daemon", "--daemon", 99, 0 },
   { "-help", "--help", 90, 0 },
   { "-no-unibyte", "--no-unibyte", 83, 0 },
   { "-multibyte", "--multibyte", 82, 0 },
@@ -1810,7 +1820,7 @@ struct standard_args standard_args[] =
   /* -d must come last before the options handled in startup.el.  */
   { "-d", "--display", 60, 1 },
   { "-display", 0, 60, 1 },
-  /* Now for the options handled in startup.el.  */
+  /* Now for the options handled in `command-line' (startup.el).  */
   { "-Q", "--quick", 55, 0 },
   { "-quick", 0, 55, 0 },
   { "-q", "--no-init-file", 50, 0 },
@@ -1819,10 +1829,12 @@ struct standard_args standard_args[] =
   { "-u", "--user", 30, 1 },
   { "-user", 0, 30, 1 },
   { "-debug-init", "--debug-init", 20, 0 },
-  { "-nbi", "--no-bitmap-icon", 15, 0 },
   { "-iconic", "--iconic", 15, 0 },
   { "-D", "--basic-display", 12, 0},
   { "-basic-display", 0, 12, 0},
+  { "-nbc", "--no-blinking-cursor", 12, 0 },
+  /* Now for the options handled in `command-line-1' (startup.el).  */
+  { "-nbi", "--no-bitmap-icon", 10, 0 },
   { "-bg", "--background-color", 10, 1 },
   { "-background", 0, 10, 1 },
   { "-fg", "--foreground-color", 10, 1 },
@@ -1832,12 +1844,12 @@ struct standard_args standard_args[] =
   { "-ib", "--internal-border", 10, 1 },
   { "-ms", "--mouse-color", 10, 1 },
   { "-cr", "--cursor-color", 10, 1 },
-  { "-nbc", "--no-blinking-cursor", 10, 0 },
   { "-fn", "--font", 10, 1 },
   { "-font", 0, 10, 1 },
   { "-fs", "--fullscreen", 10, 0 },
   { "-fw", "--fullwidth", 10, 0 },
   { "-fh", "--fullheight", 10, 0 },
+  { "-mm", "--maximized", 10, 0 },
   { "-g", "--geometry", 10, 1 },
   { "-geometry", 0, 10, 1 },
   { "-T", "--title", 10, 1 },
@@ -1853,6 +1865,15 @@ struct standard_args standard_args[] =
   { "-color", "--color", 5, 0},
   { "-no-splash", "--no-splash", 3, 0 },
   { "-no-desktop", "--no-desktop", 3, 0 },
+#ifdef HAVE_NS
+  { "-NSAutoLaunch", 0, 5, 1 },
+  { "-NXAutoLaunch", 0, 5, 1 },
+  { "-disable-font-backend", "--disable-font-backend", 65, 0 },
+  { "-_NSMachLaunch", 0, 85, 1 },
+  { "-MachLaunch", 0, 85, 1 },
+  { "-macosx", 0, 85, 0 },
+  { "-NSHost", 0, 85, 1 },
+#endif
   /* These have the same priority as ordinary file name args,
      so they are not reordered with respect to those.  */
   { "-L", "--directory", 0, 1 },
@@ -1872,6 +1893,13 @@ struct standard_args standard_args[] =
   { "-visit", "--visit", 0, 1 },
   { "-file", "--file", 0, 1 },
   { "-insert", "--insert", 0, 1 },
+#ifdef HAVE_NS
+  { "-NXOpen", 0, 0, 1 },
+  { "-NXOpenTemp", 0, 0, 1 },
+  { "-NSOpen", 0, 0, 1 },
+  { "-NSOpenTemp", 0, 0, 1 },
+  { "-GSFilePath", 0, 0, 1 },
+#endif
   /* This should be processed after ordinary file name args and the like.  */
   { "-kill", "--kill", -10, 0 },
 };
@@ -2058,12 +2086,6 @@ all of which are called before Emacs is actually killed.  */)
 
   UNGCPRO;
 
-/* Is it really necessary to do this deassign
-   when we are going to exit anyway?  */
-/* #ifdef VMS
-  stop_vms_input ();
- #endif  */
-
   shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
 
   /* If we have an auto-save list file,
@@ -2131,10 +2153,6 @@ shut_down_emacs (sig, no_x, stuff)
   unlock_all_files ();
 #endif
 
-#ifdef VMS
-  kill_vms_processes ();
-#endif
-
 #if 0 /* This triggers a bug in XCloseDisplay and is not needed.  */
 #ifdef HAVE_X_WINDOWS
   /* It's not safe to call intern here.  Maybe we are crashing.  */
@@ -2168,45 +2186,16 @@ shut_down_emacs (sig, no_x, stuff)
 #ifdef MSDOS
   dos_cleanup ();
 #endif
+
+#ifdef HAVE_NS
+  ns_term_shutdown (sig);
+#endif
 }
 
 
 \f
 #ifndef CANNOT_DUMP
 
-#ifdef HAVE_SHM
-
-DEFUN ("dump-emacs-data", Fdump_emacs_data, Sdump_emacs_data, 1, 1, 0,
-       doc: /* Dump current state of Emacs into data file FILENAME.
-This function exists on systems that use HAVE_SHM.  */)
-     (filename)
-     Lisp_Object filename;
-{
-  extern char my_edata[];
-  Lisp_Object tem;
-
-  check_pure_size ();
-  CHECK_STRING (filename);
-  filename = Fexpand_file_name (filename, Qnil);
-
-  tem = Vpurify_flag;
-  Vpurify_flag = Qnil;
-
-  fflush (stdout);
-  /* Tell malloc where start of impure now is.  */
-  /* Also arrange for warnings when nearly out of space.  */
-#ifndef SYSTEM_MALLOC
-  memory_warnings (my_edata, malloc_warning);
-#endif
-  map_out_data (SDATA (filename));
-
-  Vpurify_flag = tem;
-
-  return Qnil;
-}
-
-#else /* not HAVE_SHM */
-
 DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
        doc: /* Dump current state of Emacs into executable file FILENAME.
 Take symbols from SYMFILE (presumably the file you executed to run Emacs).
@@ -2267,9 +2256,6 @@ You must run Emacs in batch mode in order to dump it.  */)
 #endif
 
   fflush (stdout);
-#ifdef VMS
-  mapout_data (SDATA (filename));
-#else
   /* Tell malloc where start of impure now is.  */
   /* Also arrange for warnings when nearly out of space.  */
 #ifndef SYSTEM_MALLOC
@@ -2300,15 +2286,12 @@ You must run Emacs in batch mode in order to dump it.  */)
 #ifdef DOUG_LEA_MALLOC
   free (malloc_state_ptr);
 #endif
-#endif /* not VMS */
 
   Vpurify_flag = tem;
 
   return unbind_to (count, Qnil);
 }
 
-#endif /* not HAVE_SHM */
-
 #endif /* not CANNOT_DUMP */
 \f
 #if HAVE_SETLOCALE
@@ -2426,42 +2409,92 @@ decode_env_path (evarname, defalt)
   return Fnreverse (lpath);
 }
 
+DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0,
+       doc: /* Return non-nil if the current emacs process is a daemon.
+If the daemon was given a name argument, return that name. */)
+  ()
+{
+  if (IS_DAEMON)
+    if (daemon_name)
+      return build_string (daemon_name);
+    else
+      return Qt;
+  else
+    return Qnil;
+}
+
+DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0,
+       doc: /* Mark the Emacs daemon as being initialized.
+This finishes the daemonization process by doing the other half of detaching
+from the parent process and its tty file descriptors.  */)
+  ()
+{
+  int nfd;
+
+  if (!IS_DAEMON)
+    error ("This function can only be called if emacs is run as a daemon");
+
+  if (daemon_pipe[1] < 0)
+    error ("The daemon has already been initialized");
+
+  if (NILP (Vafter_init_time))
+    error ("This function can only be called after loading the init files");
+
+  /* Get rid of stdin, stdout and stderr.  */
+  nfd = open ("/dev/null", O_RDWR);
+  dup2 (nfd, 0);
+  dup2 (nfd, 1);
+  dup2 (nfd, 2);
+  close (nfd);
+
+  /* Closing the pipe will notify the parent that it can exit.
+     FIXME: In case some other process inherited the pipe, closing it here
+     won't notify the parent because it's still open elsewhere, so we
+     additionally send a byte, just to make sure the parent really exits.
+     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]);
+  /* Set it to an invalid value so we know we've already run this function.  */
+  daemon_pipe[1] = -1;
+  return Qt;
+}
+
 void
 syms_of_emacs ()
 {
-  Qfile_name_handler_alist = intern ("file-name-handler-alist");
+  Qfile_name_handler_alist = intern_c_string ("file-name-handler-alist");
   staticpro (&Qfile_name_handler_alist);
 
 #ifndef CANNOT_DUMP
-#ifdef HAVE_SHM
-  defsubr (&Sdump_emacs_data);
-#else
   defsubr (&Sdump_emacs);
-#endif
 #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.  */);
 
   DEFVAR_LISP ("system-type", &Vsystem_type,
-              doc: /* Value is symbol indicating type of operating system you are using.
+              doc: /* The value is a symbol indicating the 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);
+  `gnu'          compiled for a GNU Hurd system.
+  `gnu/linux'    compiled for a GNU/Linux system.
+  `gnu/kfreebsd' compiled for a GNU system with a FreeBSD kernel.
+  `darwin'       compiled for Darwin (GNU-Darwin, Mac OS X, ...).
+  `ms-dos'       compiled as an MS-DOS application.
+  `windows-nt'   compiled as a native W32 application.
+  `cygwin'       compiled using the Cygwin library.
+Anything else (in Emacs 23.1, the possibilities are: aix, berkeley-unix,
+hpux, irix, lynxos 3.0.1, usg-unix-v) indicates some sort of Unix system.  */);
+  Vsystem_type = intern_c_string (SYSTEM_TYPE);
 
   DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
               doc: /* Value is string indicating configuration Emacs was built for.
@@ -2534,6 +2567,22 @@ was found.  */);
   DEFVAR_LISP ("previous-system-time-locale", &Vprevious_system_time_locale,
               doc: /* Most recently used system locale for time.  */);
   Vprevious_system_time_locale = Qnil;
+
+  DEFVAR_LISP ("before-init-time", &Vbefore_init_time,
+              doc: /* Value of `current-time' before Emacs begins initialization.  */);
+  Vbefore_init_time = Qnil;
+
+  DEFVAR_LISP ("after-init-time", &Vafter_init_time,
+              doc: /* Value of `current-time' after loading the init files.
+This is nil during initialization.  */);
+  Vafter_init_time = Qnil;
+
+  DEFVAR_BOOL ("inhibit-x-resources", &inhibit_x_resources,
+              doc: /* If non-nil, X resources, Windows Registry settings, and NS defaults are not used.  */);
+  inhibit_x_resources = 0;
+
+  /* Make sure IS_DAEMON starts up as false.  */
+  daemon_pipe[1] = 0;
 }
 
 /* arch-tag: 7bfd356a-c720-4612-8ab6-aa4222931c2e