Unspecified location of chown for Linux (Linux doesn't put chown in /etc).
[bpt/emacs.git] / src / emacs.c
index c38811f..56d4ecf 100644 (file)
@@ -1,5 +1,5 @@
 /* Fully extensible Emacs, running on Unix, intended for GNU.
-   Copyright (C) 1985, 1986, 1987, 1993 Free Software Foundation, Inc.
+   Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -21,7 +21,7 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include <signal.h>
 #include <errno.h>
 
-#include "config.h"
+#include <config.h>
 #include <stdio.h>
 
 #include <sys/types.h>
@@ -47,12 +47,15 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #include "systty.h"
 #include "syssignal.h"
+#include "process.h"
 
 #ifndef O_RDWR
 #define O_RDWR 2
 #endif
 
-#define PRIO_PROCESS 0
+extern void malloc_warning ();
+extern char *index ();
+extern char *strerror ();
 
 /* Command line args from shell, as list of strings */
 Lisp_Object Vcommand_line_args;
@@ -61,6 +64,13 @@ Lisp_Object Vcommand_line_args;
    names discarded.  */
 Lisp_Object Vinvocation_name;
 
+/* The directory name from which Emacs was invoked.  */
+Lisp_Object Vinvocation_directory;
+
+/* The directory name in which to find subdirs such as lisp and etc.
+   nil means get them only from PATH_LOADSEARCH.  */
+Lisp_Object Vinstallation_directory;
+
 /* Hook run by `kill-emacs' before it does really anything.  */
 Lisp_Object Vkill_emacs_hook;
 
@@ -69,8 +79,11 @@ Lisp_Object Vkill_emacs_hook;
   on subsequent starts.  */
 int initialized;
 
-/* Variable whose value is symbol giving operating system type */
+/* Variable whose value is symbol giving operating system type */
 Lisp_Object Vsystem_type;
+
+/* Variable whose value is string giving configuration built for.  */
+Lisp_Object Vsystem_configuration;
   
 /* If non-zero, emacs should not attempt to use an window-specific code,
    but instead should use the virtual terminal under which it was started */
@@ -81,7 +94,7 @@ int inhibit_window_system;
    priority; Those functions have their own extern declaration.  */
 int emacs_priority;
 
-#ifdef BSD
+#ifdef BSD_PGRPS
 /* See sysdep.c.  */
 extern int inherited_pgroup;
 #endif
@@ -145,10 +158,28 @@ fatal_error_signal (sig)
      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
      want to really receive it.  */
+#ifndef MSDOS
   sigunblock (sigmask (fatal_error_code));
+#endif
   kill (getpid (), fatal_error_code);
 #endif /* not VMS */
 }
+
+#ifdef SIGDANGER
+
+/* Handler for SIGDANGER.  */
+SIGTYPE
+memory_warning_signal (sig)
+     int sig;
+{
+  signal (sig, memory_warning_signal);
+
+  malloc_warning ("Operating system warns that virtual memory is running low.\n");
+
+  /* It might be unsafe to call do_auto_save now.  */
+  force_auto_save_soon ();
+}
+#endif
 \f
 /* Code for dealing with Lisp access to the Unix command line */
 
@@ -159,8 +190,77 @@ init_cmdargs (argc, argv, skip_args)
      int skip_args;
 {
   register int i;
+  Lisp_Object name, dir;
 
   Vinvocation_name = Ffile_name_nondirectory (build_string (argv[0]));
+  Vinvocation_directory = Ffile_name_directory (build_string (argv[0]));
+  /* If we got no directory in argv[0], search PATH to find where
+     Emacs actually came from.  */
+  if (NILP (Vinvocation_directory))
+    {
+      Lisp_Object found;
+      int yes = openp (Vexec_path, Vinvocation_name,
+                      EXEC_SUFFIXES, &found, 1);
+      if (yes == 1)
+       Vinvocation_directory = Ffile_name_directory (found);
+    }
+
+  Vinstallation_directory = Qnil;
+
+  if (!NILP (Vinvocation_directory))
+    {
+      dir = Vinvocation_directory;
+      name = Fexpand_file_name (Vinvocation_name, dir);
+      while (1)
+       {
+         Lisp_Object tem, lib_src_exists;
+         Lisp_Object etc_exists, info_exists;
+
+         /* See if dir contains subdirs for use by Emacs.
+            Check for the ones that would exist in a build directory,
+            not including lisp and info.  */
+         tem = Fexpand_file_name (build_string ("lib-src"), dir);
+         lib_src_exists = Ffile_exists_p (tem);
+         if (!NILP (lib_src_exists))
+           {
+             tem = Fexpand_file_name (build_string ("etc"), dir);
+             etc_exists = Ffile_exists_p (tem);
+             if (!NILP (etc_exists))
+               {
+                 Vinstallation_directory
+                   = Ffile_name_as_directory (dir);
+                 break;
+               }
+           }
+
+         /* See if dir's parent contains those subdirs.  */
+         tem = Fexpand_file_name (build_string ("../lib-src"), dir);
+         lib_src_exists = Ffile_exists_p (tem);
+         if (!NILP (lib_src_exists))
+           {
+             tem = Fexpand_file_name (build_string ("../etc"), dir);
+             etc_exists = Ffile_exists_p (tem);
+             if (!NILP (etc_exists))
+               {
+                 tem = Fexpand_file_name (build_string (".."), dir);
+                 Vinstallation_directory
+                   = Ffile_name_as_directory (tem);
+                 break;
+               }
+           }
+
+         /* If the Emacs executable is actually a link,
+            next try the dir that the link points into.  */
+         tem = Ffile_symlink_p (name);
+         if (!NILP (tem))
+           {
+             name = tem;
+             dir = Ffile_name_directory (name);
+           }
+         else
+           break;
+       }
+    }
 
   Vcommand_line_args = Qnil;
 
@@ -180,6 +280,14 @@ Any directory names are omitted.")
   return Fcopy_sequence (Vinvocation_name);
 }
 
+DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
+  0, 0, 0,
+  "Return the directory name in which the Emacs executable was located")
+  ()
+{
+  return Fcopy_sequence (Vinvocation_directory);
+}
+
 \f
 #ifdef VMS
 #ifdef LINK_CRTL_SHARE
@@ -195,14 +303,19 @@ extern noshare char **environ;
    Provide dummy definitions to avoid error.
    (We don't have any real constructors or destructors.)  */
 #ifdef __GNUC__
+#ifndef GCC_CTORS_IN_LIBC
 __do_global_ctors ()
 {}
 __do_global_ctors_aux ()
 {}
 __do_global_dtors ()
 {}
+/* Linux has a bug in its library; avoid an error.  */
+#ifndef LINUX
 char * __CTOR_LIST__[2] = { (char *) (-1), 0 };
+#endif
 char * __DTOR_LIST__[2] = { (char *) (-1), 0 };
+#endif /* GCC_CTORS_IN_LIBC */
 __main ()
 {}
 #endif /* __GNUC__ */
@@ -218,8 +331,6 @@ main (argc, argv, envp)
   int skip_args = 0;
   extern int errno;
   extern sys_nerr;
-  extern char *sys_errlist[];
-  extern void malloc_warning ();
 
 /* Map in shared memory, if we are using that.  */
 #ifdef HAVE_SHM
@@ -298,11 +409,16 @@ main (argc, argv, envp)
 
   clearerr (stdin);
 
-#ifdef BSD
-  {
-    inherited_pgroup = getpgrp (0);
-    setpgrp (0, getpid ());
-  }
+#ifdef BSD_PGRPS
+  if (initialized)
+    {
+      inherited_pgroup = EMACS_GETPGRP (0);
+      setpgrp (0, getpid ());
+    }
+#else
+#if defined (USG5) && defined (INTERRUPT_INPUT)
+  setpgrp ();
+#endif
 #endif
 
 
@@ -327,12 +443,25 @@ main (argc, argv, envp)
     }
 #endif /* not SYSTEM_MALLOC */
 
+#ifdef MSDOS
+  /* We do all file input/output as binary files.  When we need to translate
+     newlines, we do that manually.  */
+  _fmode = O_BINARY;
+  (stdin)->_flag &= ~_IOTEXT;
+  (stdout)->_flag &= ~_IOTEXT;
+  (stderr)->_flag &= ~_IOTEXT;
+#endif /* MSDOS */
+
 #ifdef PRIO_PROCESS
   if (emacs_priority)
     nice (emacs_priority);
   setuid (getuid ());
 #endif /* PRIO_PROCESS */
 
+#ifdef EXTRA_INITIALIZE
+  EXTRA_INITIALIZE;
+#endif
+
   inhibit_window_system = 0;
 
   /* Handle the -t switch, which specifies filename to use as terminal */
@@ -345,12 +474,7 @@ main (argc, argv, envp)
       result = open (argv[skip_args], O_RDWR, 2 );
       if (result < 0)
        {
-         char *errstring;
-
-         if (errno >= 0 && errno < sys_nerr)
-           errstring = sys_errlist[errno];
-         else
-           errstring = "undocumented error code";
+         char *errstring = strerror (errno);
          fprintf (stderr, "emacs: %s: %s\n", argv[skip_args], errstring);
          exit (1);
        }
@@ -423,17 +547,14 @@ main (argc, argv, envp)
       signal (SIGXFSZ, fatal_error_signal);
 #endif /* SIGXFSZ */
 
-#ifdef AIX
-      signal (SIGDANGER, fatal_error_signal);
-      signal (20, fatal_error_signal);
-      signal (21, fatal_error_signal);
-      signal (22, fatal_error_signal);
-      signal (23, fatal_error_signal);
-      signal (24, fatal_error_signal);
-#ifdef SIGIO
-      signal (SIGAIO, fatal_error_signal);
-      signal (SIGPTY, fatal_error_signal);
+#ifdef SIGDANGER
+      /* This just means available memory is getting low.  */
+      signal (SIGDANGER, memory_warning_signal);
 #endif
+
+#ifdef AIX
+/* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU.  */
+      signal (SIGXCPU, fatal_error_signal);
 #ifndef _I386
       signal (SIGIOINT, fatal_error_signal);
 #endif
@@ -466,15 +587,31 @@ main (argc, argv, envp)
   init_eval ();
   init_data ();
 
+#ifdef MSDOS
+  /* Call early 'cause init_environment needs it.  */
+  init_dosfns ();
+  /* Set defaults for several environment variables.  */
+  if (initialized) init_environment (argc, argv, skip_args);
+#endif
+
   /* 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_process_environment ();
+  /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
+     if this is not done.  Do it after set_process_environment so that we
+     don't pollute Vprocess_environment.  */
+#ifdef AIX
+  putenv ("LANG=C");
+#endif
+
+  init_buffer ();      /* Init default directory of main buffer */
 
+  init_callproc_1 ();  /* Must precede init_cmdargs and init_sys_modes.  */
+  init_cmdargs (argc, argv, skip_args);        /* Must precede init_lread.  */
+  init_callproc ();    /* Must follow init_cmdargs but not init_sys_modes.  */
   init_lread ();
 
-  init_cmdargs (argc, argv, skip_args);        /* Create list Vcommand_line_args */
-  init_buffer ();      /* Init default directory of main buffer */
   if (!noninteractive)
     {
 #ifdef VMS
@@ -483,7 +620,6 @@ main (argc, argv, envp)
       init_display (); /* Determine terminal type.  init_sys_modes uses results */
     }
   init_keyboard ();    /* This too must precede init_sys_modes */
-  init_callproc ();    /* And this too. */
 #ifdef VMS
   init_vmsproc ();     /* And this too. */
 #endif /* VMS */
@@ -515,9 +651,7 @@ main (argc, argv, envp)
       syms_of_print ();
       syms_of_eval ();
       syms_of_fns ();
-#ifdef LISP_FLOAT_TYPE
       syms_of_floatfns ();
-#endif
 
       syms_of_abbrev ();
       syms_of_buffer ();
@@ -549,6 +683,7 @@ main (argc, argv, envp)
       syms_of_search ();
       syms_of_frame ();
       syms_of_syntax ();
+      syms_of_term ();
       syms_of_undo ();
 
       /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
@@ -605,14 +740,14 @@ main (argc, argv, envp)
 
   initialized = 1;
 
-#ifdef sun
-  /* sun's localtime() has a bug.  it caches the value of the time
+#if defined (sun) || defined (LOCALTIME_CACHE)
+  /* sun's localtime has a bug.  it caches the value of the time
      zone rather than looking it up every time.  Since localtime() is
      called to bolt the undumping time into the undumped emacs, this
-     results in localtime() ignoring the TZ environment variable.
-     This flushes the new TZ value into localtime(). */
-  tzset();
-#endif /* sun */
+     results in localtime ignoring the TZ environment variable.
+     This flushes the new TZ value into localtime. */
+  tzset ();
+#endif /* defined (sun) || defined (LOCALTIME_CACHE) */
 
   /* Enter editor command loop.  This never returns.  */
   Frecursive_edit ();
@@ -649,7 +784,7 @@ all of which are called before Emacs is actually killed.")
   stop_vms_input ();
  #endif  */
 
-  shut_down_emacs (0, 0);
+  shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
 
   exit ((XTYPE (arg) == Lisp_Int) ? XINT (arg)
 #ifdef VMS
@@ -678,12 +813,17 @@ shut_down_emacs (sig, no_x, stuff)
      int sig, no_x;
      Lisp_Object stuff;
 {
+  /* Prevent running of hooks from now on.  */
+  Vrun_hooks = Qnil;
+
   /* If we are controlling the terminal, reset terminal modes */
 #ifdef EMACS_HAVE_TTY_PGRP
   {
+    int pgrp = EMACS_GETPGRP (0);
+
     int tpgrp;
     if (EMACS_GET_TTY_PGRP (0, &tpgrp) != -1
-       && tpgrp == getpgrp (0))
+       && tpgrp == pgrp)
       {
        fflush (stdout);
        reset_sys_modes ();
@@ -738,7 +878,6 @@ This function exists on systems that use HAVE_SHM.")
 {
   extern int my_edata;
   Lisp_Object tem;
-  extern void malloc_warning ();
 
   CHECK_STRING (intoname, 0);
   intoname = Fexpand_file_name (intoname, Qnil);
@@ -774,7 +913,6 @@ and announce itself normally when it is run.")
 {
   extern int my_edata;
   Lisp_Object tem;
-  extern void malloc_warning ();
 
   CHECK_STRING (intoname, 0);
   intoname = Fexpand_file_name (intoname, Qnil);
@@ -819,7 +957,6 @@ decode_env_path (evarname, defalt)
      char *evarname, *defalt;
 {
   register char *path, *p;
-  extern char *index ();
 
   Lisp_Object lpath;
 
@@ -860,6 +997,7 @@ syms_of_emacs ()
   defsubr (&Skill_emacs);
 
   defsubr (&Sinvocation_name);
+  defsubr (&Sinvocation_directory);
 
   DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
     "Args passed by shell to Emacs, as a list of strings.");
@@ -868,6 +1006,10 @@ syms_of_emacs ()
     "Value is symbol indicating type of operating system you are using.");
   Vsystem_type = intern (SYSTEM_TYPE);
 
+  DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
+    "Value is string indicating configuration Emacs was built for.");
+  Vsystem_configuration = build_string (CONFIGURATION);
+
   DEFVAR_BOOL ("noninteractive", &noninteractive1,
     "Non-nil means Emacs is running without interactive terminal.");
 
@@ -882,9 +1024,23 @@ expect to be able to interact with the user.");
     "Priority for Emacs to run at.\n\
 This value is effective only if set before Emacs is dumped,\n\
 and only if the Emacs executable is installed with setuid to permit\n\
-it to change priority.  (Emacs sets its uid back to the real uid.)");
+it to change priority.  (Emacs sets its uid back to the real uid.)\n\
+Currently, you need to define PRIO_PROCESS in `config.h'\n\
+before you compile Emacs, to enable the code for this feature.");
   emacs_priority = 0;
 
-  staticpro (&Vinvocation_name);
-  Vinvocation_name = Qnil;
+  DEFVAR_LISP ("invocation-name", &Vinvocation_name,
+    "The program name that was used to run Emacs.\n\
+Any directory names are omitted.");
+
+  DEFVAR_LISP ("invocation-directory", &Vinvocation_directory,
+    "The directory in which the Emacs executable was found, to run it.\n\
+The value is nil if that directory's name is not known.");
+
+  DEFVAR_LISP ("installation-directory", &Vinstallation_directory,
+    "A directory within which to look for the `lib-src' and `etc' directories.\n\
+This is non-nil when we can't find those directories in their standard\n\
+installed locations, but we can find them\n\
+near where the Emacs executable was found.");
+  Vinstallation_directory = Qnil;
 }