home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / src / emacs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-14  |  27.2 KB  |  1,072 lines

  1. /* Fully extensible Emacs, running on Unix, intended for GNU.
  2.    Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <signal.h>
  22. #include <errno.h>
  23.  
  24. #include <config.h>
  25. #include <stdio.h>
  26.  
  27. #include <sys/types.h>
  28. #include <sys/file.h>
  29.  
  30. #ifdef VMS
  31. #include <ssdef.h>
  32. #endif
  33.  
  34. #ifdef BSD
  35. #include <sys/ioctl.h>
  36. #endif
  37.  
  38. #ifdef APOLLO
  39. #ifndef APOLLO_SR10
  40. #include <default_acl.h>
  41. #endif
  42. #endif
  43.  
  44. #include "lisp.h"
  45. #include "paths.h"
  46. #include "commands.h"
  47. #include "intervals.h"
  48.  
  49. #include "systty.h"
  50. #include "syssignal.h"
  51. #include "process.h"
  52.  
  53. #ifdef AMIGA_DUMP
  54. #define HAVE_SHM        /* Simplifies the ifdefs */
  55. #endif
  56.  
  57. #ifndef O_RDWR
  58. #define O_RDWR 2
  59. #endif
  60.  
  61. extern void malloc_warning ();
  62. extern char *index ();
  63. extern char *strerror ();
  64.  
  65. /* Command line args from shell, as list of strings */
  66. Lisp_Object Vcommand_line_args;
  67.  
  68. /* The name under which Emacs was invoked, with any leading directory
  69.    names discarded.  */
  70. Lisp_Object Vinvocation_name;
  71.  
  72. /* The directory name from which Emacs was invoked.  */
  73. Lisp_Object Vinvocation_directory;
  74.  
  75. /* The directory name in which to find subdirs such as lisp and etc.
  76.    nil means get them only from PATH_LOADSEARCH.  */
  77. Lisp_Object Vinstallation_directory;
  78.  
  79. /* Hook run by `kill-emacs' before it does really anything.  */
  80. Lisp_Object Vkill_emacs_hook;
  81.  
  82. /* Set nonzero after Emacs has started up the first time.
  83.   Prevents reinitialization of the Lisp world and keymaps
  84.   on subsequent starts.  */
  85. int initialized;
  86.  
  87. /* Variable whose value is symbol giving operating system type.  */
  88. Lisp_Object Vsystem_type;
  89.  
  90. /* Variable whose value is string giving configuration built for.  */
  91. Lisp_Object Vsystem_configuration;
  92.   
  93. /* If non-zero, emacs should not attempt to use an window-specific code,
  94.    but instead should use the virtual terminal under which it was started */
  95. int inhibit_window_system;
  96.  
  97. /* If nonzero, set Emacs to run at this priority.  This is also used
  98.    in child_setup and sys_suspend to make sure subshells run at normal
  99.    priority; Those functions have their own extern declaration.  */
  100. int emacs_priority;
  101.  
  102. #ifdef BSD_PGRPS
  103. /* See sysdep.c.  */
  104. extern int inherited_pgroup;
  105. #endif
  106.  
  107. #ifdef HAVE_X_WINDOWS
  108. /* If non-zero, -d was specified, meaning we're using some window system. */
  109. int display_arg;
  110. #endif
  111.  
  112. /* An address near the bottom of the stack.
  113.    Tells GC how to save a copy of the stack.  */
  114. char *stack_bottom;
  115.  
  116. #ifdef HAVE_X_WINDOWS
  117. extern Lisp_Object Vwindow_system;
  118. #endif /* HAVE_X_WINDOWS */
  119.  
  120. #ifdef USG_SHARED_LIBRARIES
  121. /* If nonzero, this is the place to put the end of the writable segment
  122.    at startup.  */
  123.  
  124. unsigned int bss_end = 0;
  125. #endif
  126.  
  127. /* Nonzero means running Emacs without interactive terminal.  */
  128.  
  129. int noninteractive;
  130.  
  131. /* Value of Lisp variable `noninteractive'.
  132.    Normally same as C variable `noninteractive'
  133.    but nothing terrible happens if user sets this one.  */
  134.  
  135. int noninteractive1;
  136.  
  137. /* Signal code for the fatal signal that was received */
  138. int fatal_error_code;
  139.  
  140. /* Nonzero if handling a fatal error already */
  141. int fatal_error_in_progress;
  142.  
  143. /* Handle bus errors, illegal instruction, etc. */
  144. SIGTYPE
  145. fatal_error_signal (sig)
  146.      int sig;
  147. {
  148.   fatal_error_code = sig;
  149.   signal (sig, SIG_DFL);
  150.  
  151.   /* If fatal error occurs in code below, avoid infinite recursion.  */
  152.   if (! fatal_error_in_progress)
  153.     {
  154.       fatal_error_in_progress = 1;
  155.  
  156.       shut_down_emacs (sig, 0, Qnil);
  157.     }
  158.  
  159. #ifdef VMS
  160.   LIB$STOP (SS$_ABORT);
  161. #else
  162.   /* Signal the same code; this time it will really be fatal.
  163.      Remember that since we're in a signal handler, the signal we're
  164.      going to send is probably blocked, so we have to unblock it if we
  165.      want to really receive it.  */
  166. #ifndef MSDOS
  167.   sigunblock (sigmask (fatal_error_code));
  168. #endif
  169.   kill (getpid (), fatal_error_code);
  170. #endif /* not VMS */
  171. }
  172.  
  173. #ifdef SIGDANGER
  174.  
  175. /* Handler for SIGDANGER.  */
  176. SIGTYPE
  177. memory_warning_signal (sig)
  178.      int sig;
  179. {
  180.   signal (sig, memory_warning_signal);
  181.  
  182.   malloc_warning ("Operating system warns that virtual memory is running low.\n");
  183.  
  184.   /* It might be unsafe to call do_auto_save now.  */
  185.   force_auto_save_soon ();
  186. }
  187. #endif
  188.  
  189. /* Code for dealing with Lisp access to the Unix command line */
  190.  
  191. static
  192. init_cmdargs (argc, argv, skip_args)
  193.      int argc;
  194.      char **argv;
  195.      int skip_args;
  196. {
  197.   register int i;
  198.   Lisp_Object name, dir;
  199.  
  200.   Vinvocation_name = Ffile_name_nondirectory (build_string (argv[0]));
  201.   Vinvocation_directory = Ffile_name_directory (build_string (argv[0]));
  202.   /* If we got no directory in argv[0], search PATH to find where
  203.      Emacs actually came from.  */
  204.   if (NILP (Vinvocation_directory))
  205.     {
  206.       Lisp_Object found;
  207.       int yes = openp (Vexec_path, Vinvocation_name,
  208.                EXEC_SUFFIXES, &found, 1);
  209.       if (yes == 1)
  210.     Vinvocation_directory = Ffile_name_directory (found);
  211.     }
  212.  
  213.   Vinstallation_directory = Qnil;
  214.  
  215.   if (!NILP (Vinvocation_directory))
  216.     {
  217.       dir = Vinvocation_directory;
  218.       name = Fexpand_file_name (Vinvocation_name, dir);
  219.       while (1)
  220.     {
  221.       Lisp_Object tem, lib_src_exists;
  222.       Lisp_Object etc_exists, info_exists;
  223.  
  224.       /* See if dir contains subdirs for use by Emacs.
  225.          Check for the ones that would exist in a build directory,
  226.          not including lisp and info.  */
  227.       tem = Fexpand_file_name (build_string ("lib-src"), dir);
  228.       lib_src_exists = Ffile_exists_p (tem);
  229.       if (!NILP (lib_src_exists))
  230.         {
  231.           tem = Fexpand_file_name (build_string ("etc"), dir);
  232.           etc_exists = Ffile_exists_p (tem);
  233.           if (!NILP (etc_exists))
  234.         {
  235.           Vinstallation_directory
  236.             = Ffile_name_as_directory (dir);
  237.           break;
  238.         }
  239.         }
  240.  
  241.       /* See if dir's parent contains those subdirs.  */
  242. #ifdef RELPATH_LIBSRC
  243.       tem = Fexpand_file_name (build_string (RELPATH_LIBSRC), dir);
  244. #else
  245.       tem = Fexpand_file_name (build_string ("../lib-src"), dir);
  246. #endif
  247.       lib_src_exists = Ffile_exists_p (tem);
  248.       if (!NILP (lib_src_exists))
  249.         {
  250. #ifdef RELPATH_ETC
  251.           tem = Fexpand_file_name (build_string (RELPATH_ETC), dir);
  252. #else
  253.           tem = Fexpand_file_name (build_string ("../etc"), dir);
  254. #endif
  255.           etc_exists = Ffile_exists_p (tem);
  256.           if (!NILP (etc_exists))
  257.         {
  258. #ifdef RELPATH_DOTDOT
  259.           tem = Fexpand_file_name (build_string (RELPATH_DOTDOT), dir);
  260. #else
  261.           tem = Fexpand_file_name (build_string (".."), dir);
  262. #endif
  263.           Vinstallation_directory
  264.             = Ffile_name_as_directory (tem);
  265.           break;
  266.         }
  267.         }
  268.  
  269.       /* If the Emacs executable is actually a link,
  270.          next try the dir that the link points into.  */
  271.       tem = Ffile_symlink_p (name);
  272.       if (!NILP (tem))
  273.         {
  274.           name = tem;
  275.           dir = Ffile_name_directory (name);
  276.         }
  277.       else
  278.         break;
  279.     }
  280.     }
  281.  
  282.   Vcommand_line_args = Qnil;
  283.  
  284.   for (i = argc - 1; i >= 0; i--)
  285.     {
  286.       if (i == 0 || i > skip_args)
  287.     Vcommand_line_args
  288.       = Fcons (build_string (argv[i]), Vcommand_line_args);
  289.     }
  290. }
  291.  
  292. DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
  293.   "Return the program name that was used to run Emacs.\n\
  294. Any directory names are omitted.")
  295.   ()
  296. {
  297.   return Fcopy_sequence (Vinvocation_name);
  298. }
  299.  
  300. DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
  301.   0, 0, 0,
  302.   "Return the directory name in which the Emacs executable was located")
  303.   ()
  304. {
  305.   return Fcopy_sequence (Vinvocation_directory);
  306. }
  307.  
  308.  
  309. #ifdef VMS
  310. #ifdef LINK_CRTL_SHARE
  311. #ifdef SHAREABLE_LIB_BUG
  312. extern noshare char **environ;
  313. #endif /* SHAREABLE_LIB_BUG */
  314. #endif /* LINK_CRTL_SHARE */
  315. #endif /* VMS */
  316.  
  317. #ifndef ORDINARY_LINK
  318. /* We don't include crtbegin.o and crtend.o in the link,
  319.    so these functions and variables might be missed.
  320.    Provide dummy definitions to avoid error.
  321.    (We don't have any real constructors or destructors.)  */
  322. #ifdef __GNUC__
  323. #ifndef GCC_CTORS_IN_LIBC
  324. __do_global_ctors ()
  325. {}
  326. __do_global_ctors_aux ()
  327. {}
  328. __do_global_dtors ()
  329. {}
  330. /* Linux has a bug in its library; avoid an error.  */
  331. #ifndef LINUX
  332. char * __CTOR_LIST__[2] = { (char *) (-1), 0 };
  333. #endif
  334. char * __DTOR_LIST__[2] = { (char *) (-1), 0 };
  335. #endif /* GCC_CTORS_IN_LIBC */
  336. __main ()
  337. {}
  338. #endif /* __GNUC__ */
  339. #endif /* ORDINARY_LINK */
  340.  
  341. /* ARGSUSED */
  342. main (argc, argv, envp)
  343.      int argc;
  344.      char **argv;
  345.      char **envp;
  346. {
  347.   char stack_bottom_variable;
  348.   int skip_args = 0;
  349.   extern int errno;
  350.   extern sys_nerr;
  351.  
  352. /* Map in shared memory, if we are using that.  */
  353. #ifdef HAVE_SHM
  354.   if (argc > 1 && !strcmp (argv[1], "-nl"))
  355.     {
  356.       map_in_data (0);
  357.       /* The shared memory was just restored, which clobbered this.  */
  358.       skip_args = 1;
  359.     }
  360.   else
  361.     {
  362.       map_in_data (1);
  363.       /* The shared memory was just restored, which clobbered this.  */
  364.       skip_args = 0;
  365.     }
  366. #endif
  367.  
  368. #ifdef NeXT
  369.   extern int malloc_cookie;
  370.  
  371.   /* This helps out unexnext.c.  */
  372.   if (initialized)
  373.     if (malloc_jumpstart (malloc_cookie) != 0)
  374.       printf ("malloc jumpstart failed!\n");
  375. #endif /* NeXT */
  376.  
  377. #ifdef HAVE_X_WINDOWS
  378.   /* Stupid kludge to catch command-line display spec.  We can't
  379.      handle this argument entirely in window system dependent code
  380.      because we don't even know which window system dependent code
  381.      to run until we've recognized this argument.  */
  382.   {
  383.     int i;
  384.  
  385.     for (i = 1; (i < argc && ! display_arg); i++)
  386.       if (!strcmp (argv[i], "-d") || !strcmp (argv[i], "-display"))
  387.     display_arg = 1;
  388.   }
  389. #endif
  390.  
  391. #ifdef VMS
  392.   /* If -map specified, map the data file in */
  393.   if (argc > 2 && ! strcmp (argv[1], "-map"))
  394.     {
  395.       skip_args = 2;
  396.       mapin_data (argv[2]);
  397.     }
  398.  
  399. #ifdef LINK_CRTL_SHARE
  400. #ifdef SHAREABLE_LIB_BUG
  401.   /* Bletcherous shared libraries! */
  402.   if (!stdin)
  403.     stdin = fdopen (0, "r");
  404.   if (!stdout)
  405.     stdout = fdopen (1, "w");
  406.   if (!stderr)
  407.     stderr = fdopen (2, "w");
  408.   if (!environ)
  409.     environ = envp;
  410. #endif /* SHAREABLE_LIB_BUG */
  411. #endif /* LINK_CRTL_SHARE */
  412. #endif /* VMS */
  413.  
  414.   /* Record (approximately) where the stack begins.  */
  415.   stack_bottom = &stack_bottom_variable;
  416.  
  417. #ifdef RUN_TIME_REMAP
  418.   if (initialized)
  419.     run_time_remap (argv[0]);
  420. #endif
  421.  
  422. #ifdef USG_SHARED_LIBRARIES
  423.   if (bss_end)
  424.     brk (bss_end);
  425. #endif
  426.  
  427.   clearerr (stdin);
  428.  
  429.   if (! noninteractive1)
  430.     {
  431. #ifdef BSD_PGRPS
  432.       if (initialized)
  433.     {
  434.       inherited_pgroup = EMACS_GETPGRP (0);
  435.       setpgrp (0, getpid ());
  436.     }
  437. #else
  438. #if defined (USG5) && defined (INTERRUPT_INPUT)
  439.       setpgrp ();
  440. #endif
  441. #endif
  442.     }
  443.  
  444. #ifdef APOLLO
  445. #ifndef APOLLO_SR10
  446.   /* If USE_DOMAIN_ACLS environment variable exists,
  447.      use ACLs rather than UNIX modes. */
  448.   if (egetenv ("USE_DOMAIN_ACLS"))
  449.     default_acl (USE_DEFACL);
  450. #endif
  451. #endif /* APOLLO */
  452.  
  453. #ifndef SYSTEM_MALLOC
  454.   if (! initialized)
  455.     {
  456.       /* Arrange to get warning messages as memory fills up.  */
  457.       memory_warnings (0, malloc_warning);
  458.  
  459.       /* Arrange to disable interrupt input while malloc and friends are
  460.      running.  */
  461.       uninterrupt_malloc ();
  462.     }
  463. #endif    /* not SYSTEM_MALLOC */
  464.  
  465. #ifdef MSDOS
  466.   /* We do all file input/output as binary files.  When we need to translate
  467.      newlines, we do that manually.  */
  468.   _fmode = O_BINARY;
  469.   (stdin)->_flag &= ~_IOTEXT;
  470.   (stdout)->_flag &= ~_IOTEXT;
  471.   (stderr)->_flag &= ~_IOTEXT;
  472. #endif /* MSDOS */
  473.  
  474. #ifdef SET_EMACS_PRIORITY
  475.   if (emacs_priority)
  476.     nice (emacs_priority);
  477.   setuid (getuid ());
  478. #endif /* SET_EMACS_PRIORITY */
  479.  
  480. #ifdef EXTRA_INITIALIZE
  481.   EXTRA_INITIALIZE;
  482. #endif
  483.  
  484.   inhibit_window_system = 0;
  485.  
  486. #ifndef AMIGA
  487.   /* Handle the -t switch, which specifies filename to use as terminal */
  488.   if (skip_args + 2 < argc && !strcmp (argv[skip_args + 1], "-t"))
  489.     {
  490.       int result;
  491.       skip_args += 2;
  492.       close (0);
  493.       close (1);
  494.       result = open (argv[skip_args], O_RDWR, 2 );
  495.       if (result < 0)
  496.     {
  497.       char *errstring = strerror (errno);
  498.       fprintf (stderr, "emacs: %s: %s\n", argv[skip_args], errstring);
  499.       exit (1);
  500.     }
  501.       dup (0);
  502.       if (! isatty (0))
  503.     {
  504.       fprintf (stderr, "emacs: %s: not a tty\n", argv[skip_args]);
  505.       exit (1);
  506.     }
  507.       fprintf (stderr, "Using %s\n", argv[skip_args]);
  508. #ifdef HAVE_X_WINDOWS
  509.       inhibit_window_system = 1;    /* -t => -nw */
  510. #endif
  511.     }
  512. #endif /* not AMIGA */
  513.   
  514.   if (skip_args + 1 < argc
  515.       && (!strcmp (argv[skip_args + 1], "-nw")))
  516.     {
  517.       skip_args += 1;
  518.       inhibit_window_system = 1;
  519.     }
  520.  
  521. /* Handle the -batch switch, which means don't do interactive display.  */
  522.   noninteractive = 0;
  523.   if (skip_args + 1 < argc && !strcmp (argv[skip_args + 1], "-batch"))
  524.     {
  525.       skip_args += 1;
  526.       noninteractive = 1;
  527.     }
  528.  
  529. #ifdef POSIX_SIGNALS
  530.   init_signals ();
  531. #endif
  532.  
  533.   if (
  534. #ifndef CANNOT_DUMP
  535.       ! noninteractive || initialized
  536. #else
  537.       1
  538. #endif
  539.       )
  540.     {
  541.       /* Don't catch these signals in batch mode if not initialized.
  542.      On some machines, this sets static data that would make
  543.      signal fail to work right when the dumped Emacs is run.  */
  544.       signal (SIGHUP, fatal_error_signal);
  545.       signal (SIGQUIT, fatal_error_signal);
  546.       signal (SIGILL, fatal_error_signal);
  547.       signal (SIGTRAP, fatal_error_signal);
  548. #ifdef SIGIOT
  549.       /* This is missing on some systems - OS/2, for example.  */
  550.       signal (SIGIOT, fatal_error_signal);
  551. #endif
  552. #ifdef SIGEMT
  553.       signal (SIGEMT, fatal_error_signal);
  554. #endif
  555.       signal (SIGFPE, fatal_error_signal);
  556. #ifdef SIGBUS
  557.       signal (SIGBUS, fatal_error_signal);
  558. #endif
  559.       signal (SIGSEGV, fatal_error_signal);
  560. #ifdef SIGSYS
  561.       signal (SIGSYS, fatal_error_signal);
  562. #endif
  563.       signal (SIGTERM, fatal_error_signal);
  564. #ifdef SIGXCPU
  565.       signal (SIGXCPU, fatal_error_signal);
  566. #endif
  567. #ifdef SIGXFSZ
  568.       signal (SIGXFSZ, fatal_error_signal);
  569. #endif /* SIGXFSZ */
  570.  
  571. #ifdef SIGDANGER
  572.       /* This just means available memory is getting low.  */
  573.       signal (SIGDANGER, memory_warning_signal);
  574. #endif
  575.  
  576. #ifdef AIX
  577. /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU.  */
  578.       signal (SIGXCPU, fatal_error_signal);
  579. #ifndef _I386
  580.       signal (SIGIOINT, fatal_error_signal);
  581. #endif
  582.       signal (SIGGRANT, fatal_error_signal);
  583.       signal (SIGRETRACT, fatal_error_signal);
  584.       signal (SIGSOUND, fatal_error_signal);
  585.       signal (SIGMSG, fatal_error_signal);
  586. #endif /* AIX */
  587.     }
  588.  
  589.   noninteractive1 = noninteractive;
  590.  
  591. /* Perform basic initializations (not merely interning symbols) */
  592.  
  593.   if (!initialized)
  594.     {
  595.       init_alloc_once ();
  596.       init_obarray ();
  597.       init_eval_once ();
  598.       init_syntax_once ();    /* Create standard syntax table.  */
  599.               /* Must be done before init_buffer */
  600.       init_casetab_once ();
  601.       init_buffer_once ();    /* Create buffer table and some buffers */
  602.       init_minibuf_once ();    /* Create list of minibuffers */
  603.                   /* Must precede init_window_once */
  604.       init_window_once ();    /* Init the window system */
  605.     }
  606.  
  607.   init_alloc ();
  608.   init_eval ();
  609.   init_data ();
  610.  
  611. #ifdef MSDOS
  612.   /* Call early 'cause init_environment needs it.  */
  613.   init_dosfns ();
  614.   /* Set defaults for several environment variables.  */
  615.   if (initialized) init_environment (argc, argv, skip_args);
  616. #endif
  617.  
  618.   /* egetenv is a pretty low-level facility, which may get called in
  619.      many circumstances; it seems flimsy to put off initializing it
  620.      until calling init_callproc.  */
  621.   set_process_environment ();
  622.   /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
  623.      if this is not done.  Do it after set_process_environment so that we
  624.      don't pollute Vprocess_environment.  */
  625. #ifdef AIX
  626.   putenv ("LANG=C");
  627. #endif
  628.  
  629.   init_buffer ();    /* Init default directory of main buffer */
  630.  
  631.   init_callproc_1 ();    /* Must precede init_cmdargs and init_sys_modes.  */
  632.   init_cmdargs (argc, argv, skip_args);    /* Must precede init_lread.  */
  633.   init_callproc ();    /* Must follow init_cmdargs but not init_sys_modes.  */
  634.   init_lread ();
  635.  
  636.   if (!noninteractive)
  637.     {
  638. #ifdef VMS
  639.       init_vms_input ();/* init_display calls get_frame_size, that needs this */
  640. #endif /* VMS */
  641.       init_display ();    /* Determine terminal type.  init_sys_modes uses results */
  642.     }
  643.   init_keyboard ();    /* This too must precede init_sys_modes */
  644. #ifdef VMS
  645.   init_vmsproc ();    /* And this too. */
  646. #endif /* VMS */
  647.   init_sys_modes ();    /* Init system terminal modes (RAW or CBREAK, etc.) */
  648.   init_xdisp ();
  649.   init_macros ();
  650.   init_editfns ();
  651. #ifdef LISP_FLOAT_TYPE
  652.   init_floatfns ();
  653. #endif
  654. #ifdef VMS
  655.   init_vmsfns ();
  656. #endif /* VMS */
  657.   init_process ();
  658. #ifdef CLASH_DETECTION
  659.   init_filelock ();
  660. #endif /* CLASH_DETECTION */
  661.  
  662. /* Intern the names of all standard functions and variables; define standard keys */
  663.  
  664.   if (!initialized)
  665.     {
  666.       /* The basic levels of Lisp must come first */
  667.       /* And data must come first of all
  668.      for the sake of symbols like error-message */
  669.       syms_of_data ();
  670.       syms_of_alloc ();
  671.       syms_of_lread ();
  672.       syms_of_print ();
  673.       syms_of_eval ();
  674.       syms_of_fns ();
  675.       syms_of_floatfns ();
  676.  
  677.       syms_of_abbrev ();
  678.       syms_of_buffer ();
  679.       syms_of_bytecode ();
  680.       syms_of_callint ();
  681.       syms_of_casefiddle ();
  682.       syms_of_casetab ();
  683.       syms_of_callproc ();
  684.       syms_of_cmds ();
  685. #ifndef NO_DIR_LIBRARY
  686.       syms_of_dired ();
  687. #endif /* not NO_DIR_LIBRARY */
  688.       syms_of_display ();
  689.       syms_of_doc ();
  690.       syms_of_editfns ();
  691.       syms_of_emacs ();
  692.       syms_of_fileio ();
  693. #ifdef CLASH_DETECTION
  694.       syms_of_filelock ();
  695. #endif /* CLASH_DETECTION */
  696.       syms_of_indent ();
  697.       syms_of_keyboard ();
  698.       syms_of_keymap ();
  699.       syms_of_macros ();
  700.       syms_of_marker ();
  701.       syms_of_minibuf ();
  702.       syms_of_mocklisp ();
  703.       syms_of_process ();
  704.       syms_of_search ();
  705.       syms_of_frame ();
  706.       syms_of_syntax ();
  707.       syms_of_term ();
  708.       syms_of_undo ();
  709.  
  710.       /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
  711.       syms_of_textprop ();
  712. #ifdef VMS
  713.       syms_of_vmsproc ();
  714. #endif /* VMS */
  715.       syms_of_window ();
  716.       syms_of_xdisp ();
  717. #ifdef HAVE_X_WINDOWS
  718.       syms_of_xterm ();
  719.       syms_of_xfns ();
  720.       syms_of_xfaces ();
  721. #ifdef HAVE_X11
  722.       syms_of_xselect ();
  723. #endif
  724. #ifdef HAVE_X_MENU
  725.       syms_of_xmenu ();
  726. #endif /* HAVE_X_MENU */
  727. #endif /* HAVE_X_WINDOWS */
  728.  
  729. #ifdef SYMS_SYSTEM
  730.       SYMS_SYSTEM;
  731. #endif
  732.  
  733. #ifdef SYMS_MACHINE
  734.       SYMS_MACHINE;
  735. #endif
  736.  
  737.       keys_of_casefiddle ();
  738.       keys_of_cmds ();
  739.       keys_of_buffer ();
  740.       keys_of_keyboard ();
  741.       keys_of_keymap ();
  742.       keys_of_macros ();
  743.       keys_of_minibuf ();
  744.       keys_of_window ();
  745.       keys_of_frame ();
  746.     }
  747.  
  748.   if (!initialized)
  749.     {
  750.       /* Handle -l loadup-and-dump, args passed by Makefile. */
  751.       if (argc > 2 + skip_args && !strcmp (argv[1 + skip_args], "-l"))
  752.     Vtop_level = Fcons (intern ("load"),
  753.                 Fcons (build_string (argv[2 + skip_args]), Qnil));
  754. #ifdef CANNOT_DUMP
  755.       /* Unless next switch is -nl, load "loadup.el" first thing.  */
  756.       if (!(argc > 1 + skip_args && !strcmp (argv[1 + skip_args], "-nl")))
  757.     Vtop_level = Fcons (intern ("load"),
  758.                 Fcons (build_string ("loadup.el"), Qnil));
  759. #endif /* CANNOT_DUMP */
  760.     }
  761.  
  762.   initialized = 1;
  763.  
  764. #if defined (sun) || defined (LOCALTIME_CACHE)
  765.   /* sun's localtime has a bug.  it caches the value of the time
  766.      zone rather than looking it up every time.  Since localtime() is
  767.      called to bolt the undumping time into the undumped emacs, this
  768.      results in localtime ignoring the TZ environment variable.
  769.      This flushes the new TZ value into localtime. */
  770.   tzset ();
  771. #endif /* defined (sun) || defined (LOCALTIME_CACHE) */
  772.  
  773.   /* Enter editor command loop.  This never returns.  */
  774.   Frecursive_edit ();
  775.   /* NOTREACHED */
  776. }
  777.  
  778. DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
  779.   "Exit the Emacs job and kill it.\n\
  780. If ARG is an integer, return ARG as the exit program code.\n\
  781. If ARG is a  string, stuff it as keyboard input.\n\n\
  782. The value of `kill-emacs-hook', if not void,\n\
  783. is a list of functions (of no args),\n\
  784. all of which are called before Emacs is actually killed.")
  785.   (arg)
  786.      Lisp_Object arg;
  787. {
  788.   Lisp_Object hook, hook1;
  789.   int i;
  790.   struct gcpro gcpro1;
  791.  
  792.   GCPRO1 (arg);
  793.  
  794.   if (feof (stdin))
  795.     arg = Qt;
  796.  
  797.   if (!NILP (Vrun_hooks) && !noninteractive)
  798.     call1 (Vrun_hooks, intern ("kill-emacs-hook"));
  799.  
  800.   UNGCPRO;
  801.  
  802. /* Is it really necessary to do this deassign
  803.    when we are going to exit anyway?  */
  804. /* #ifdef VMS
  805.   stop_vms_input ();
  806.  #endif  */
  807.  
  808.   shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
  809.  
  810.   exit ((XTYPE (arg) == Lisp_Int) ? XINT (arg)
  811. #ifdef VMS
  812.     : 1
  813. #else
  814.     : 0
  815. #endif
  816.     );
  817.   /* NOTREACHED */
  818. }
  819.  
  820.  
  821. /* Perform an orderly shutdown of Emacs.  Autosave any modified
  822.    buffers, kill any child processes, clean up the terminal modes (if
  823.    we're in the foreground), and other stuff like that.  Don't perform
  824.    any redisplay; this may be called when Emacs is shutting down in
  825.    the background, or after its X connection has died.
  826.  
  827.    If SIG is a signal number, print a message for it.
  828.  
  829.    This is called by fatal signal handlers, X protocol error handlers,
  830.    and Fkill_emacs.  */
  831.  
  832. void
  833. shut_down_emacs (sig, no_x, stuff)
  834.      int sig, no_x;
  835.      Lisp_Object stuff;
  836. {
  837.   /* Prevent running of hooks from now on.  */
  838.   Vrun_hooks = Qnil;
  839.  
  840.   /* If we are controlling the terminal, reset terminal modes */
  841. #ifdef EMACS_HAVE_TTY_PGRP
  842.   {
  843.     int pgrp = EMACS_GETPGRP (0);
  844.  
  845.     int tpgrp;
  846.     if (EMACS_GET_TTY_PGRP (0, &tpgrp) != -1
  847.     && tpgrp == pgrp)
  848.       {
  849.     fflush (stdout);
  850.     reset_sys_modes ();
  851.     if (sig && sig != SIGTERM)
  852.       fprintf (stderr, "Fatal error (%d).", sig);
  853.       }
  854.   }
  855. #else
  856.   fflush (stdout);
  857.   reset_sys_modes ();
  858. #endif
  859.  
  860.   stuff_buffered_input (stuff);
  861.  
  862.   kill_buffer_processes (Qnil);
  863.   Fdo_auto_save (Qt, Qnil);
  864.  
  865. #ifdef CLASH_DETECTION
  866.   unlock_all_files ();
  867. #endif
  868.  
  869. #ifdef VMS
  870.   kill_vms_processes ();
  871. #endif
  872.  
  873. #ifdef HAVE_X_WINDOWS
  874.   /* It's not safe to call intern here.  Maybe we are crashing.  */
  875.   if (!noninteractive && SYMBOLP (Vwindow_system)
  876.       && XSYMBOL (Vwindow_system)->name->size == 1
  877.       && XSYMBOL (Vwindow_system)->name->data[0] == 'x'
  878.       && ! no_x)
  879.     Fx_close_current_connection ();
  880. #endif /* HAVE_X_WINDOWS */
  881.  
  882. #ifdef SIGIO
  883.   /* There is a tendency for a SIGIO signal to arrive within exit,
  884.      and cause a SIGHUP because the input descriptor is already closed.  */
  885.   unrequest_sigio ();
  886.   signal (SIGIO, SIG_IGN);
  887. #endif
  888. }
  889.  
  890.  
  891.  
  892. #ifndef CANNOT_DUMP
  893. /* Nothing like this can be implemented on an Apollo.
  894.    What a loss!  */
  895.  
  896. #ifdef HAVE_SHM
  897.  
  898. DEFUN ("dump-emacs-data", Fdump_emacs_data, Sdump_emacs_data, 1, 1, 0,
  899.   "Dump current state of Emacs into data file FILENAME.\n\
  900. This function exists on systems that use HAVE_SHM.")
  901.   (intoname)
  902.      Lisp_Object intoname;
  903. {
  904.   extern int my_edata;
  905.   Lisp_Object tem;
  906.  
  907.   CHECK_STRING (intoname, 0);
  908.   intoname = Fexpand_file_name (intoname, Qnil);
  909.  
  910.   tem = Vpurify_flag;
  911.   Vpurify_flag = Qnil;
  912.  
  913.   fflush (stdout);
  914.   /* Tell malloc where start of impure now is */
  915.   /* Also arrange for warnings when nearly out of space.  */
  916. #ifndef SYSTEM_MALLOC
  917.   memory_warnings (&my_edata, malloc_warning);
  918. #endif
  919.   map_out_data (XSTRING (intoname)->data);
  920.  
  921.   Vpurify_flag = tem;
  922.  
  923.   return Qnil;
  924. }
  925.  
  926. #else /* not HAVE_SHM */
  927.  
  928. DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
  929.   "Dump current state of Emacs into executable file FILENAME.\n\
  930. Take symbols from SYMFILE (presumably the file you executed to run Emacs).\n\
  931. This is used in the file `loadup.el' when building Emacs.\n\
  932. \n\
  933. Bind `command-line-processed' to nil before dumping,\n\
  934. if you want the dumped Emacs to process its command line\n\
  935. and announce itself normally when it is run.")
  936.   (intoname, symname)
  937.      Lisp_Object intoname, symname;
  938. {
  939.   extern int my_edata;
  940.   Lisp_Object tem;
  941.  
  942.   CHECK_STRING (intoname, 0);
  943.   intoname = Fexpand_file_name (intoname, Qnil);
  944.   if (!NILP (symname))
  945.     {
  946.       CHECK_STRING (symname, 0);
  947.       if (XSTRING (symname)->size)
  948.     symname = Fexpand_file_name (symname, Qnil);
  949.     }
  950.  
  951.   tem = Vpurify_flag;
  952.   Vpurify_flag = Qnil;
  953.  
  954.   fflush (stdout);
  955. #ifdef VMS
  956.   mapout_data (XSTRING (intoname)->data);
  957. #else
  958.   /* Tell malloc where start of impure now is */
  959.   /* Also arrange for warnings when nearly out of space.  */
  960. #ifndef SYSTEM_MALLOC
  961.   memory_warnings (&my_edata, malloc_warning);
  962. #endif
  963.   unexec (XSTRING (intoname)->data,
  964.       !NILP (symname) ? XSTRING (symname)->data : 0, &my_edata, 0, 0);
  965. #endif /* not VMS */
  966.  
  967.   Vpurify_flag = tem;
  968.  
  969.   return Qnil;
  970. }
  971.  
  972. #endif /* not HAVE_SHM */
  973.  
  974. #endif /* not CANNOT_DUMP */
  975.  
  976. #ifndef SEPCHAR
  977. #define SEPCHAR ':'
  978. #endif
  979.  
  980. Lisp_Object
  981. decode_env_path (evarname, defalt)
  982.      char *evarname, *defalt;
  983. {
  984.   register char *path, *p;
  985.  
  986.   Lisp_Object lpath;
  987.  
  988.   /* It's okay to use getenv here, because this function is only used
  989.      to initialize variables when Emacs starts up, and isn't called
  990.      after that.  */
  991.   if (evarname != 0)
  992.     path = (char *) getenv (evarname);
  993.   else
  994.     path = 0;
  995.   if (!path)
  996.     path = defalt;
  997.   lpath = Qnil;
  998.   while (1)
  999.     {
  1000.       p = index (path, SEPCHAR);
  1001.       if (!p) p = path + strlen (path);
  1002.       lpath = Fcons (p - path ? make_string (path, p - path) : Qnil,
  1003.              lpath);
  1004.       if (*p)
  1005.     path = p + 1;
  1006.       else
  1007.     break;
  1008.     }
  1009.   return Fnreverse (lpath);
  1010. }
  1011.  
  1012. syms_of_emacs ()
  1013. {
  1014. #ifndef CANNOT_DUMP
  1015. #ifdef HAVE_SHM
  1016.   defsubr (&Sdump_emacs_data);
  1017. #else
  1018.   defsubr (&Sdump_emacs);
  1019. #endif
  1020. #endif
  1021.  
  1022.   defsubr (&Skill_emacs);
  1023.  
  1024.   defsubr (&Sinvocation_name);
  1025.   defsubr (&Sinvocation_directory);
  1026.  
  1027.   DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
  1028.     "Args passed by shell to Emacs, as a list of strings.");
  1029.  
  1030.   DEFVAR_LISP ("system-type", &Vsystem_type,
  1031.     "Value is symbol indicating type of operating system you are using.");
  1032.   Vsystem_type = intern (SYSTEM_TYPE);
  1033.  
  1034.   DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
  1035.     "Value is string indicating configuration Emacs was built for.");
  1036.   Vsystem_configuration = build_string (EMACS_CONFIGURATION);
  1037.  
  1038.   DEFVAR_BOOL ("noninteractive", &noninteractive1,
  1039.     "Non-nil means Emacs is running without interactive terminal.");
  1040.  
  1041.   DEFVAR_LISP ("kill-emacs-hook", &Vkill_emacs_hook,
  1042.     "Hook to be run whenever kill-emacs is called.\n\
  1043. Since kill-emacs may be invoked when the terminal is disconnected (or\n\
  1044. in other similar situations), functions placed on this hook should not\n\
  1045. expect to be able to interact with the user.");
  1046.   Vkill_emacs_hook = Qnil;
  1047.  
  1048.   DEFVAR_INT ("emacs-priority", &emacs_priority,
  1049.     "Priority for Emacs to run at.\n\
  1050. This value is effective only if set before Emacs is dumped,\n\
  1051. and only if the Emacs executable is installed with setuid to permit\n\
  1052. it to change priority.  (Emacs sets its uid back to the real uid.)\n\
  1053. Currently, you need to define SET_EMACS_PRIORITY in `config.h'\n\
  1054. before you compile Emacs, to enable the code for this feature.");
  1055.   emacs_priority = 0;
  1056.  
  1057.   DEFVAR_LISP ("invocation-name", &Vinvocation_name,
  1058.     "The program name that was used to run Emacs.\n\
  1059. Any directory names are omitted.");
  1060.  
  1061.   DEFVAR_LISP ("invocation-directory", &Vinvocation_directory,
  1062.     "The directory in which the Emacs executable was found, to run it.\n\
  1063. The value is nil if that directory's name is not known.");
  1064.  
  1065.   DEFVAR_LISP ("installation-directory", &Vinstallation_directory,
  1066.     "A directory within which to look for the `lib-src' and `etc' directories.\n\
  1067. This is non-nil when we can't find those directories in their standard\n\
  1068. installed locations, but we can find them\n\
  1069. near where the Emacs executable was found.");
  1070.   Vinstallation_directory = Qnil;
  1071. }
  1072.