home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / emacs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-29  |  43.8 KB  |  1,668 lines

  1. /* XEmacs -- Fully extensible Emacs, running on Unix and other platforms.
  2.    Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994
  3.    Free Software Foundation, Inc.
  4.  
  5. This file is part of XEmacs.
  6.  
  7. XEmacs is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with XEmacs; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Synched up with: Mule 2.0, FSF 19.28. */
  22.  
  23. /* Note: It is necessary to specify <config.h> and not "config.h" in
  24.    order for the --srcdir type of compilation to work properly.
  25.    Otherwise the config.h from the srcdir, rather than the one from
  26.    the build dir, will be used. */
  27.  
  28. #include <config.h>
  29. #include "lisp.h"
  30.  
  31. #include "backtrace.h" /* run-emacs-from-temacs needs this */
  32. #include "commands.h"
  33. #include "device.h"
  34. #include "process.h"
  35. #include "sysdep.h"
  36.  
  37. #include <setjmp.h>
  38. #include "syssignal.h" /* Always include before systty.h */
  39. #include "systty.h"
  40. #include "sysfile.h"
  41. #include "systime.h"
  42.  
  43. #if defined (I18N2) || defined (I18N3) || defined (I18N4)
  44. #include <locale.h>
  45. #endif
  46.  
  47. #ifdef TOOLTALK
  48. #include <tt_c.h>
  49. #endif
  50.  
  51. #ifdef VMS
  52. #include <ssdef.h>
  53. #endif
  54.  
  55. #ifdef APOLLO
  56. #ifndef APOLLO_SR10
  57. #include <default_acl.h>
  58. #endif
  59. #endif
  60.  
  61. extern void memory_warnings ();
  62.  
  63. /* Command line args from shell, as list of strings */
  64. Lisp_Object Vcommand_line_args;
  65.  
  66. /* Set nonzero after XEmacs has started up the first time.
  67.   Prevents reinitialization of the Lisp world and keymaps
  68.   on subsequent starts.  */
  69. int initialized;
  70.  
  71. /* Variable whose value is symbol giving operating system type. */
  72. Lisp_Object Vsystem_type;
  73.  
  74. /* Variable whose value is string giving configuration built for.  */
  75. Lisp_Object Vsystem_configuration;
  76.   
  77. /* The name under which XEmacs was invoked, with any leading directory
  78.    names discarded.  */
  79. Lisp_Object Vinvocation_name;
  80.  
  81. /* The directory name from which XEmacs was invoked.  */
  82. Lisp_Object Vinvocation_directory;
  83.  
  84. #if 0 /* FSFmacs */
  85. /* The directory name in which to find subdirs such as lisp and etc.
  86.    nil means get them only from PATH_LOADSEARCH.  */
  87. Lisp_Object Vinstallation_directory;
  88. #endif
  89.  
  90. /* If nonzero, set XEmacs to run at this priority.  This is also used
  91.    in child_setup and sys_suspend to make sure subshells run at normal
  92.    priority. */
  93. int emacs_priority;
  94.  
  95. /* If non-zero, a window-system was specified on the command line. */
  96. int display_arg;
  97.  
  98. /* Type of display specified.  We cannot use a Lisp symbol here because
  99.    Lisp symbols may not initialized at the time that we set this
  100.    variable. */
  101. char *display_use;
  102.  
  103. /* If non-zero, then the early error handler will only print the error
  104.    message and exit. */
  105. int suppress_early_backtrace;
  106.  
  107. /* An address near the bottom of the stack.
  108.    Tells GC how to save a copy of the stack.  */
  109. char *stack_bottom;
  110.  
  111. #ifdef USG_SHARED_LIBRARIES
  112. /* If nonzero, this is the place to put the end of the writable segment
  113.    at startup.  */
  114.  
  115. unsigned int bss_end = 0;
  116. #endif
  117.  
  118. /* Number of bytes of writable memory we can expect to be able to get */
  119. unsigned int lim_data;
  120.  
  121. /* Nonzero means running XEmacs without interactive terminal.  */
  122.  
  123. int noninteractive;
  124.  
  125. /* Value of Lisp variable `noninteractive'.
  126.    Normally same as C variable `noninteractive'
  127.    but nothing terrible happens if user sets this one.  */
  128.  
  129. int noninteractive1;
  130.  
  131. /* Save argv and argc.  */
  132. char **initial_argv;
  133. int initial_argc;
  134.  
  135. extern int always_gc;           /* hack */
  136.  
  137. Lisp_Object Qkill_emacs_hook;
  138. Lisp_Object Qsave_buffers_kill_emacs;
  139.  
  140.  
  141. /* Signal code for the fatal signal that was received */
  142. static int fatal_error_code;
  143.  
  144. /* Nonzero if handling a fatal error already */
  145. static int fatal_error_in_progress;
  146.  
  147. static void shut_down_emacs (int sig, int no_x, Lisp_Object stuff);
  148.  
  149. /* Handle bus errors, illegal instruction, etc. */
  150. SIGTYPE
  151. fatal_error_signal (int sig)
  152. {
  153.   fatal_error_code = sig;
  154.   signal (sig, SIG_DFL);
  155.   /* Unblock the signal so that if the same signal gets sent in the
  156.      code below, we avoid a deadlock. */
  157.   EMACS_UNBLOCK_SIGNAL (fatal_error_code);
  158.  
  159.   /* If fatal error occurs in code below, avoid infinite recursion.  */
  160.   if (! fatal_error_in_progress)
  161.     {
  162.       fatal_error_in_progress = 1;
  163.  
  164.       shut_down_emacs (sig, 0, Qnil);
  165.     }
  166. #ifdef VMS
  167.   LIB$STOP (SS$_ABORT);
  168. #else
  169.   /* Signal the same code; this time it will really be fatal. */
  170.   kill (getpid (), fatal_error_code);
  171. #endif /* not VMS */
  172.   SIGRETURN;
  173. }
  174.  
  175. /* dump an error message; called like printf */
  176.  
  177. DOESNT_RETURN
  178. error (CONST char *fmt, ...)
  179. {
  180.   Lisp_Object obj;
  181.   va_list args;
  182.  
  183.   va_start (args, fmt);
  184.   obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
  185.                 args);
  186.   va_end (args);
  187.  
  188.   /* Fsignal GC-protects its args */
  189.   signal_error (Qerror, list1 (obj));
  190. }
  191.  
  192. Lisp_Object
  193. continuable_error (CONST char *fmt, ...)
  194. {
  195.   Lisp_Object obj;
  196.   va_list args;
  197.  
  198.   va_start (args, fmt);
  199.   obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
  200.                 args);
  201.   va_end (args);
  202.  
  203.   /* Fsignal GC-protects its args */
  204.   return Fsignal (Qerror, list1 (obj));
  205. }
  206.  
  207. DOESNT_RETURN
  208. fatal (CONST char *fmt, ...)
  209. {
  210.   va_list args;
  211.   va_start (args, fmt);
  212.  
  213.   fprintf (stderr, "\nXEmacs: ");
  214.   vfprintf (stderr, GETTEXT (fmt), args);
  215.   fprintf (stderr, "\n");
  216.  
  217.   va_end (args);
  218.   fflush (stderr);
  219.   exit (1);
  220. }
  221.  
  222. /* #### The following two functions should be replaced with
  223.    calls to emacs_doprnt_*() functions, with STREAM set to send out
  224.    to stdout or stderr.  This is the only way to ensure that
  225.    I18N3 works properly (many implementations of the *printf()
  226.    functions, including the ones included in glibc, do not implement
  227.    the %###$ argument-positioning syntax). */
  228.  
  229. /* exactly equivalent to fprintf (stderr, fmt, ...) except that it calls
  230.    GETTEXT on the format string. */
  231.  
  232. int
  233. stderr_out (CONST char *fmt, ...)
  234. {
  235.   int retval;
  236.   va_list args;
  237.   va_start (args, fmt);
  238.  
  239.   retval = vfprintf (stderr, GETTEXT (fmt), args);
  240.  
  241.   va_end (args);
  242.   /* fflush (stderr); */
  243.   return retval;
  244. }
  245.  
  246. /* exactly equivalent to fprintf (stdout, fmt, ...) except that it calls
  247.    GETTEXT on the format string. */
  248.  
  249. int
  250. stdout_out (CONST char *fmt, ...)
  251. {
  252.   int retval;
  253.   va_list args;
  254.   va_start (args, fmt);
  255.  
  256.   retval = vfprintf (stdout, GETTEXT (fmt), args);
  257.  
  258.   va_end (args);
  259.   return retval;
  260. }
  261.  
  262. #ifdef SIGDANGER
  263.  
  264. /* Handler for SIGDANGER.  */
  265. SIGTYPE
  266. memory_warning_signal (int sig)
  267. {
  268.   /* #### bad bad bad; this function shouldn't do anything except
  269.      set a flag, or weird corruption could happen. */
  270.   signal (sig, memory_warning_signal);
  271.  
  272.   malloc_warning
  273.     (GETTEXT ("Operating system warns that virtual memory is running low.\n"));
  274.  
  275.   /* It might be unsafe to call do_auto_save now.  */
  276.   force_auto_save_soon ();
  277. }
  278. #endif
  279.  
  280. /* Code for dealing with Lisp access to the Unix command line */
  281.  
  282. static Lisp_Object
  283. make_arg_list_1 (int argc, char **argv, int skip_args)
  284. {
  285.   Lisp_Object result = Qnil;
  286.   REGISTER int i;
  287.  
  288.   for (i = argc - 1; i >= 0; i--)
  289.     {
  290.       if (i == 0 || i > skip_args)
  291.     result = Fcons (build_ext_string (argv [i]), result);
  292.     }
  293.   return result;
  294. }
  295.  
  296. Lisp_Object
  297. make_arg_list (int argc, char **argv)
  298. {
  299.   return make_arg_list_1 (argc, argv, 0);
  300. }
  301.  
  302. /* Calling functions are also responsible for calling free_argc_argv
  303.    when they are done with the generated list. */
  304. void
  305. make_argc_argv (Lisp_Object argv_list, int *argc, char ***argv)
  306. {
  307.   Lisp_Object next;
  308.   int n = XINT (Flength (argv_list));
  309.   REGISTER int i;
  310.   *argv = (char**) xmalloc ((n+1) * sizeof (char*));
  311.  
  312.   for (i = 0, next = argv_list; i < n; i++, next = XCDR (next))
  313.     {
  314.       CHECK_STRING (XCAR (next), 0);
  315.       (*argv) [i] = xstrdup (string_ext_data (XSTRING (XCAR (next))));
  316.     }
  317.   (*argv) [n] = 0;
  318.   *argc = i;
  319. }
  320.  
  321. void
  322. free_argc_argv (char **argv)
  323. {
  324.   int elt = 0;
  325.  
  326.   while (argv[elt])
  327.     {
  328.       xfree (argv[elt]);
  329.       elt++;
  330.     }
  331.   xfree (argv);
  332. }
  333.  
  334. static void
  335. init_cmdargs (int argc, char **argv, int skip_args)
  336. {
  337.   initial_argv = argv;
  338.   initial_argc = argc;
  339.  
  340.   Vcommand_line_args = make_arg_list_1 (argc, argv, skip_args);
  341. }
  342.  
  343. DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
  344.   "Return the program name that was used to run XEmacs.\n\
  345. Any directory names are omitted.")
  346.   ()
  347. {
  348.   return Fcopy_sequence (Vinvocation_name);
  349. }
  350.  
  351.  
  352. #ifdef I18N4
  353.                 /* #### - don't know why I18N4 on SunOS/JLE
  354.                    can't deal with this.  It's a potential
  355.                    bug that needs to be looked at. */
  356. # undef RUN_TIME_REMAP
  357. #endif
  358.  
  359. #if defined (MULE) && defined (MSDOS) && defined (EMX)
  360. /* Setup all of files be input/output'ed with binary translation mdoe. */
  361. asm ("    .text");
  362. asm ("L_setbinmode:");
  363. asm ("    movl    $1, __fmode_bin");
  364. asm ("    ret");
  365. asm ("    .stabs    \"___CTOR_LIST__\", 23, 0, 0, L_setbinmode");
  366. #endif
  367.  
  368. static DOESNT_RETURN
  369. main_1 (int argc, char **argv, char **envp)
  370. {
  371.   char stack_bottom_variable;
  372.   int skip_args = 0;
  373.   Lisp_Object load_me;
  374.   int inhibit_window_system;
  375.  
  376. #ifdef NeXT
  377.   extern int malloc_cookie;
  378.   
  379.   /* 19-Jun-1995 -baw
  380.    * NeXT secret magic, ripped from Emacs-for-NS by Carl Edman
  381.    * <cedman@princton.edu>.  Note that even Carl doesn't know what this
  382.    * does; it was provided by NeXT, and it presumable makes NS's mallocator
  383.    * work with dumping.  But malloc_jumpstart() and malloc_freezedry() in
  384.    * unexnext.c are both completely undocumented, even in NS header files!
  385.    * But hey, it solves all NS related memory problems, so who's
  386.    * complaining?
  387.    */
  388.   if (initialized)
  389.     if (malloc_jumpstart (malloc_cookie) != 0)
  390.       printf ("malloc jumpstart failed!\n");
  391. #endif /* NeXT */
  392.  
  393. #if defined (GNU_MALLOC) && defined (ERROR_CHECK_MALLOC)
  394. #if 0
  395.   if (!initialized)
  396.     init_free_hook ();
  397. #endif
  398. #endif
  399.  
  400.   /* Map in shared memory, if we are using that.  */
  401. #ifdef HAVE_SHM
  402.   if (argc > 1 && !strcmp (argv[1], "-nl"))
  403.     {
  404.       map_in_data (0);
  405.       /* The shared memory was just restored, which clobbered this.  */
  406.       skip_args = 1;
  407.     }
  408.   else
  409.     {
  410.       map_in_data (1);
  411.       /* The shared memory was just restored, which clobbered this.  */
  412.       skip_args = 0;
  413.     }
  414. #endif
  415.  
  416. #ifdef VMS
  417.   /* If -map specified, map the data file in */
  418.   if (argc > 2 && ! strcmp (argv[1], "-map"))
  419.     {
  420.       skip_args = 2;
  421.       mapin_data (argv[2]);
  422.     }
  423.  
  424. #ifdef LINK_CRTL_SHARE
  425. #ifdef SHAREABLE_LIB_BUG
  426.   /* Bletcherous shared libraries! */
  427.   if (!stdin)
  428.     stdin = fdopen (0, "r");
  429.   if (!stdout)
  430.     stdout = fdopen (1, "w");
  431.   if (!stderr)
  432.     stderr = fdopen (2, "w");
  433.   if (!environ)
  434.     environ = envp;
  435. #endif /* SHAREABLE_LIB_BUG */
  436. #endif /* LINK_CRTL_SHARE */
  437. #endif /* VMS */
  438. #if (defined (MSDOS) && defined (EMX)) || defined (WIN32)
  439.   environ = envp;
  440. #endif
  441.  
  442.   /* Record (approximately) where the stack begins.  */
  443.   stack_bottom = &stack_bottom_variable;
  444.  
  445. #ifdef RUN_TIME_REMAP
  446.   if (initialized)
  447.     run_time_remap (argv[0]);
  448. #endif
  449.  
  450. #ifdef USG_SHARED_LIBRARIES
  451.   if (bss_end)
  452.     brk ((void *) bss_end);
  453. #endif
  454.  
  455.   clearerr (stdin);
  456.  
  457. #ifdef APOLLO
  458. #ifndef APOLLO_SR10
  459.   /* If USE_DOMAIN_ACLS environment variable exists,
  460.      use ACLs rather than UNIX modes. */
  461.   if (egetenv ("USE_DOMAIN_ACLS"))
  462.     default_acl (USE_DEFACL);
  463. #endif
  464. #endif /* APOLLO */
  465.  
  466. #if defined (HAVE_MMAP) && defined (REL_ALLOC)
  467.   /* ralloc can only be used if using the GNU memory allocator. */
  468.   init_ralloc ();
  469. #endif
  470.  
  471. #ifndef SYSTEM_MALLOC
  472.  
  473. #ifdef HAVE_SOCKS
  474.   if (initialized)
  475.     SOCKSinit (argv[0]);
  476. #endif /* HAVE_SOCKS */
  477.  
  478.   if (!initialized)
  479.     {
  480.       /* Arrange to get warning messages as memory fills up.  */
  481.       memory_warnings (0, malloc_warning);
  482.     }
  483. #endif    /* not SYSTEM_MALLOC */
  484.  
  485. #ifdef MSDOS
  486.   /* We do all file input/output as binary files.  When we need to translate
  487.      newlines, we do that manually.  */
  488.   _fmode = O_BINARY;
  489.   (stdin)->_flag &= ~_IOTEXT;
  490.   (stdout)->_flag &= ~_IOTEXT;
  491.   (stderr)->_flag &= ~_IOTEXT;
  492. #endif /* MSDOS */
  493.  
  494. #ifdef SET_EMACS_PRIORITY
  495.   if (emacs_priority != 0)
  496.     nice (-emacs_priority);
  497.   setuid (getuid ());
  498. #endif /* SET_EMACS_PRIORITY */
  499.  
  500. #ifdef EXTRA_INITIALIZE
  501.   EXTRA_INITIALIZE;
  502. #endif
  503.  
  504. #ifdef HAVE_WINDOW_SYSTEM
  505.   inhibit_window_system = 0;
  506. #else
  507.   inhibit_window_system = 1;
  508. #endif
  509.  
  510.   /* Handle the -t switch, which specifies filename to use as terminal */
  511.   if (skip_args + 2 < argc && !strcmp (argv[skip_args + 1], "-t"))
  512.     {
  513.       int result;
  514.  
  515.       skip_args += 2;
  516.       close (0);
  517.       close (1);
  518.       result = open (argv[skip_args], O_RDWR, 2 );
  519.       if (result < 0)
  520.     {
  521.       fatal ("%s: %s", argv[skip_args], strerror (errno));
  522.     }
  523.       dup (0);
  524.       if (! isatty (0))
  525.     fatal ("%s: not a tty", argv[skip_args]);
  526.  
  527.       stderr_out ("Using %s", ttyname (0));
  528. #if 0
  529.       stderr_out ("Using %s", argv[skip_args]);
  530. #endif
  531.       inhibit_window_system = 1;    /* -t => -nw */
  532.     }
  533.  
  534.   if (skip_args + 1 < argc
  535.       && (!strcmp (argv[skip_args + 1], "-nw")))
  536.     {
  537.       skip_args += 1;
  538.       inhibit_window_system = 1;
  539.     }
  540.  
  541.   /* Handle the -batch switch, which means don't do interactive display.  */
  542.   noninteractive = 0;
  543.   if (skip_args + 1 < argc &&
  544.       (!strcmp (argv[skip_args + 1], "-batch") ||
  545.        !strcmp (argv[skip_args + 1], "--batch")))
  546.     {
  547.       skip_args += 1;
  548.       noninteractive = 1;
  549.     }
  550.  
  551.   /* Partially handle the -version and -help switches: they imply -batch,
  552.      but are not removed from the list.
  553.    */
  554.   if (skip_args + 1 < argc &&
  555.       (!strcmp (argv[skip_args + 1], "-version") ||
  556.        !strcmp (argv[skip_args + 1], "--version") ||
  557.        !strcmp (argv[skip_args + 1], "-help") ||
  558.        !strcmp (argv[skip_args + 1], "--help")))
  559.     noninteractive = 1;
  560.  
  561.   /* Now, figure out which type of device is our first device. */
  562.  
  563.   display_arg = 0;
  564.  
  565.   if (noninteractive)
  566.     display_use = "stream";
  567.   else
  568.     display_use = "tty";
  569.  
  570. #ifdef HAVE_WINDOW_SYSTEM
  571.   /* Stupid kludge to catch command-line display spec.  We can't
  572.      handle this argument entirely in window system dependent code
  573.      because we don't even know which window system dependent code
  574.      to run until we've recognized this argument.  */
  575.   if (!inhibit_window_system && !noninteractive)
  576.     {
  577.       int i;
  578.       char *disp;
  579.       
  580.       for (i = 1; (i < argc && !display_arg); i++)
  581.     {
  582. #ifdef HAVE_X_WINDOWS
  583.       if (!strcmp (argv[i], "-d") || !strcmp (argv[i], "-display"))
  584.         {
  585.           display_arg = 1;
  586.           display_use = "x";
  587.         }
  588. #endif /* HAVE_X_WINDOWS */
  589. #ifdef HAVE_NEXTSTEP
  590.       if (!strcmp (argv[i], "-NXHost") || !strcmp (argv[i], "-MachLaunch"))
  591.         {
  592.           display_arg = 1;
  593.           display_use = "ns";
  594.         }
  595. #endif /* HAVE_NEXTSTEP */
  596.     }
  597.       
  598. #ifdef HAVE_X_WINDOWS
  599.       disp = getenv ("DISPLAY");
  600.  
  601.       /* null string doesn't count as a display */
  602.       if (disp && disp[0])
  603.     display_use = "x";
  604.  
  605.       /* Do not set display_arg here.  It is only to be set if the display
  606.      was specified on the command line. */
  607. #endif
  608.     }
  609. #endif /* HAVE_WINDOW_SYSTEM */
  610.  
  611.   noninteractive1 = noninteractive;
  612.  
  613.   /****** Now initialize everything *******/
  614.  
  615.   /* First, do really basic environment initialization -- catching signals
  616.      and the like.  These functions have no dependence on any part of
  617.      the Lisp engine and need to be done both at dump time and at run time. */
  618.  
  619.   init_signals_very_early ();
  620.   init_data_very_early (); /* Catch math errors. */
  621. #ifdef LISP_FLOAT_TYPE
  622.   init_floatfns_very_early (); /* Catch floating-point math errors. */
  623. #endif
  624.   init_intl_very_early (); /* set up the locale and domain for gettext and
  625.                   such. */
  626.  
  627.   /* Now initialize the Lisp engine and the like.  Done only during
  628.      dumping.  No dependence on anything that may be in the user's
  629.      environment when the dumped XEmacs is run.
  630.  
  631.      We try to do things in an order that minimizes the non-obvious
  632.      dependencies between functions. */
  633.  
  634.   if (!initialized)
  635.     {
  636.       /* Initialize things so that new Lisp objects
  637.      can be created and objects can be staticpro'd.
  638.      Must be basically the very first thing done
  639.      because pretty much all of the initialization
  640.      routines below create new objects. */
  641.       init_alloc_once_early ();
  642.  
  643.       /* Initialize Qnil, Qt, Qunbound, and the
  644.      obarray.  After this, symbols can be
  645.      interned.  This depends on init_alloc_once(). */
  646.       init_symbols_once_early ();
  647.  
  648.       /* Declare the basic symbols pertaining to errors,
  649.      So that deferror() can be called. */
  650.       init_errors_once_early ();
  651.  
  652.       /* Now declare all the symbols and define all the Lisp primitives.
  653.  
  654.      The *only* thing that the syms_of_*() functions are allowed to do
  655.      is call one of the following three functions:
  656.  
  657.      defsymbol()
  658.      defsubr()
  659.      deferror()
  660.      defkeyword()
  661.  
  662.      Order does not matter in these functions.
  663.      */
  664.  
  665.       syms_of_abbrev ();
  666.       syms_of_alloc ();
  667.       syms_of_buffer ();
  668.       syms_of_bytecode ();
  669.       syms_of_callint ();
  670.       syms_of_callproc ();
  671.       syms_of_casefiddle ();
  672.       syms_of_casetab ();
  673.       syms_of_cmds ();
  674.       syms_of_data ();
  675. #ifdef DEBUG_XEMACS
  676.       syms_of_debug ();
  677. #endif /* DEBUG_XEMACS */
  678.       syms_of_device_tty ();
  679.       syms_of_device ();
  680.       syms_of_dired ();
  681.       syms_of_doc ();
  682.       syms_of_editfns ();
  683.       syms_of_elhash ();
  684.       syms_of_emacs ();
  685.       syms_of_eval ();
  686.       syms_of_event_stream ();
  687.       syms_of_events ();
  688.       syms_of_extents ();
  689.       syms_of_faces ();
  690.       syms_of_fileio ();
  691. #ifdef CLASH_DETECTION
  692.       syms_of_filelock ();
  693. #endif /* CLASH_DETECTION */
  694.       syms_of_floatfns ();
  695.       syms_of_fns ();
  696.       syms_of_font_lock ();
  697.       syms_of_frame ();
  698.       syms_of_general ();
  699.       syms_of_glyphs ();
  700.       syms_of_indent ();
  701.       syms_of_intl ();
  702.       syms_of_keyboard ();
  703.       syms_of_keymap ();
  704.       syms_of_lread ();
  705.       syms_of_lstream ();
  706.       syms_of_macros ();
  707.       syms_of_marker ();
  708.       syms_of_md5 ();
  709.       syms_of_menubar ();
  710.       syms_of_minibuf ();
  711. #ifdef MOCKLISP_SUPPORT
  712.       syms_of_mocklisp ();
  713. #endif
  714.       syms_of_objects_tty ();
  715.       syms_of_objects ();
  716.       syms_of_print ();
  717. #if !defined (NO_SUBPROCESSES)
  718.       syms_of_process ();
  719. #endif
  720. #if defined (HAVE_MMAP) && defined (REL_ALLOC)
  721.       syms_of_ralloc ();
  722. #endif /* HAVE_MMAP && REL_ALLOC */
  723.       syms_of_redisplay ();
  724.       syms_of_search ();
  725.       syms_of_signal ();
  726.       syms_of_sound ();
  727.       syms_of_specifier ();
  728.       syms_of_symbols ();
  729.       syms_of_syntax ();
  730.       syms_of_scrollbar ();
  731.       syms_of_toolbar ();
  732.       syms_of_undo ();
  733.       syms_of_window ();
  734. #ifdef HAVE_X_WINDOWS
  735.       syms_of_device_x ();
  736.       syms_of_event_Xt ();
  737.       syms_of_frame_x ();
  738.       syms_of_glyphs_x ();
  739.       syms_of_objects_x ();
  740.       syms_of_menubar_x ();
  741.       syms_of_xselect ();
  742. #ifdef EPOCH
  743.       syms_of_epoch ();
  744. #endif
  745. #endif /* HAVE_X_WINDOWS */
  746.  
  747. #ifdef HAVE_NEXTSTEP
  748.       syms_of_device_ns ();
  749.       syms_of_frame_ns ();
  750.       syms_of_glyphs_ns ();
  751.       syms_of_objects_ns ();
  752.       syms_of_menubar_ns ();
  753.       syms_of_nsselect ();
  754.       syms_of_scrollbar_ns ();
  755. #endif /* HAVE_NEXTSTEP */
  756.  
  757. #ifdef MULE
  758. #ifdef MULE_REGEXP
  759.       syms_of_mule_category ();
  760. #endif
  761.       syms_of_mule_ccl ();
  762.       syms_of_mule_charset ();
  763.       syms_of_mule_coding ();
  764.       syms_of_mule ();
  765. #ifdef HAVE_WNN
  766.       syms_of_mule_wnn ();
  767. #endif
  768. #ifdef HAVE_CANNA
  769.       syms_of_mule_canna ();
  770. #endif /* HAVE_CANNA */
  771. #endif /* MULE */
  772.  
  773. #ifdef SYMS_SYSTEM
  774.       SYMS_SYSTEM;
  775. #endif
  776.  
  777. #ifdef SYMS_MACHINE
  778.       SYMS_MACHINE;
  779. #endif
  780.  
  781. #ifdef EMACS_BTL
  782.       syms_of_btl (); 
  783. #endif
  784.  
  785. #ifdef ENERGIZE
  786.       syms_of_energize ();
  787. #endif
  788.  
  789. #if defined (GNU_MALLOC) && defined (ERROR_CHECK_MALLOC)
  790. #if 0
  791.       syms_of_free_hook ();
  792. #endif
  793. #endif
  794.  
  795. #ifdef TOOLTALK
  796.       syms_of_tooltalk ();
  797. #endif
  798.  
  799. #ifdef SUNPRO
  800.       syms_of_sunpro ();
  801. #endif
  802.  
  803.       /* Now create the subtypes for the types that have them.
  804.      We do this before the vars_*() because more symbols
  805.      may get initialized here. */
  806.  
  807.       /* Now initialize the device types and associated symbols.
  808.          Other than the first function below, the functions may
  809.      make exactly the following function/macro calls:
  810.  
  811.      INITIALIZE_DEVICE_TYPE()
  812.      DEVICE_HAS_METHOD()
  813.  
  814.      For any given device type, the former macro must be called
  815.      before the any calls to the latter macro. */
  816.  
  817.       device_type_create ();
  818.  
  819.       device_type_create_stream ();
  820.  
  821.       device_type_create_tty ();
  822.       device_type_create_frame_tty ();
  823.       device_type_create_objects_tty ();
  824.       device_type_create_redisplay_tty ();
  825.  
  826. #ifdef HAVE_X_WINDOWS
  827.       device_type_create_x ();
  828.       device_type_create_frame_x ();
  829.       device_type_create_glyphs_x ();
  830.       device_type_create_menubar_x ();
  831.       
  832.       device_type_create_objects_x ();
  833.       device_type_create_redisplay_x ();
  834.       device_type_create_scrollbar_x ();
  835.       device_type_create_toolbar_x ();
  836. #endif /* HAVE_X_WINDOWS */
  837.  
  838.       /* Now initialize the specifier types and associated symbols.
  839.          Other than the first function below, the functions may
  840.      make exactly the following function/macro calls:
  841.  
  842.      INITIALIZE_SPECIFIER_TYPE()
  843.      SPECIFIER_HAS_METHOD()
  844.  
  845.      For any given specifier type, the former macro must be called
  846.      before the any calls to the latter macro. */
  847.  
  848.       specifier_type_create ();
  849.  
  850.       specifier_type_create_image ();
  851.       specifier_type_create_objects ();
  852.       specifier_type_create_toolbar ();
  853.  
  854.       /* Now initialize the image instantiator types and associated symbols.
  855.          Other than the first function below, the functions may
  856.      make exactly the following function/macro calls:
  857.  
  858.      INITIALIZE_IMAGE_INSTANTIATOR_TYPE()
  859.      IITYPE_HAS_METHOD()
  860.      IITYPE_VALID_KEYWORD()
  861.  
  862.      For any given image instantiator type, the first macro must be
  863.      called before the any calls to the other macros. */
  864.  
  865.       image_instantiator_type_create ();
  866. #ifdef HAVE_X_WINDOWS
  867.       image_instantiator_type_create_glyphs_x ();
  868. #endif /* HAVE_X_WINDOWS */
  869.  
  870.       /* Now initialize most variables.
  871.  
  872.      These functions may do exactly the following:
  873.  
  874.      DEFVAR_INT()
  875.      DEFVAR_LISP()
  876.      DEFVAR_BOOL()
  877.      DEFER_GETTEXT()
  878.      Dynarr_*()
  879.      Blocktype_*()
  880.      staticpro()
  881.      Fprovide(symbol)
  882.      intern()
  883.      pure_put()
  884.      xmalloc()
  885.      defsymbol(), if it's absolutely necessary and you're sure that
  886.        the symbol isn't referenced anywhere else in the initialization
  887.        code
  888.      Fset() on a symbol that is unbound
  889.      assigning a symbol or constant value to a variable
  890.      using a global variable that has been initialized
  891.        earlier on in the same function
  892.  
  893.      Any of the object-creating functions on alloc.c: e.g.
  894.  
  895.      make_pure_*()
  896.      Fpurecopy()
  897.      make_string()
  898.      build_string()
  899.      make_vector()
  900.      make_number()
  901.      make_extent()
  902.      alloc_lcrecord()
  903.      Fcons()
  904.      listN()
  905.  
  906.      perhaps a few others.
  907.        */
  908.  
  909.       /* Now allow Fprovide() statements to be made. */
  910.       init_provide_once ();
  911.  
  912.       vars_of_abbrev ();
  913.       vars_of_alloc ();
  914.       vars_of_buffer ();
  915.       vars_of_bytecode ();
  916.       vars_of_callint ();
  917.       vars_of_callproc ();
  918.       vars_of_casetab ();
  919.       vars_of_cmds ();
  920. #ifdef DEBUG_XEMACS
  921.       vars_of_debug ();
  922. #endif
  923.       vars_of_device_stream ();
  924.       vars_of_device_tty ();
  925.       vars_of_device ();
  926.       vars_of_dired ();
  927.       vars_of_doc ();
  928.       vars_of_editfns ();
  929.       vars_of_elhash ();
  930.       vars_of_emacs ();
  931.       vars_of_eval ();
  932.       vars_of_event_stream ();
  933.       vars_of_event_tty ();
  934.       vars_of_events ();
  935.       vars_of_extents ();
  936.       vars_of_faces ();
  937.       vars_of_fileio ();
  938. #ifdef CLASH_DETECTION
  939.       vars_of_filelock ();
  940. #endif
  941.       vars_of_floatfns ();
  942.       vars_of_font_lock ();
  943.       vars_of_frame_tty ();
  944.       vars_of_frame ();
  945.       vars_of_glyphs ();
  946.       vars_of_indent ();
  947.       vars_of_insdel ();
  948.       vars_of_intl ();
  949.       vars_of_keyboard ();
  950.       vars_of_keymap ();
  951.       vars_of_lread ();
  952.       vars_of_macros ();
  953.       vars_of_md5 ();
  954.       vars_of_menubar ();
  955.       vars_of_minibuf ();
  956. #ifdef MOCKLISP_SUPPORT
  957.       vars_of_mocklisp ();
  958. #endif
  959.       vars_of_print ();
  960. #ifndef NO_SUBPROCESSES
  961.       vars_of_process ();
  962. #endif
  963. #if defined (HAVE_MMAP) && defined (REL_ALLOC)
  964.       vars_of_ralloc ();
  965. #endif /* HAVE_MMAP && REL_ALLOC */
  966.       vars_of_redisplay ();
  967.       vars_of_search ();
  968.       vars_of_sound ();
  969.       vars_of_specifier ();
  970.       vars_of_symbols ();
  971.       vars_of_syntax ();
  972.       vars_of_toolbar ();
  973.       vars_of_undo ();
  974.       vars_of_window ();
  975.  
  976. #ifdef HAVE_X_WINDOWS
  977.       vars_of_device_x ();
  978.       vars_of_event_Xt ();
  979.       vars_of_frame_x ();
  980.       vars_of_glyphs_x ();
  981.       vars_of_menubar_x ();
  982.       vars_of_objects_x ();
  983.       vars_of_xselect ();
  984. #ifdef EPOCH
  985.       vars_of_epoch ();
  986. #endif
  987.       vars_of_scrollbar_x ();
  988. #endif
  989.  
  990. #ifdef ENERGIZE
  991.       vars_of_energize ();
  992. #endif
  993.  
  994. #ifdef TOOLTALK
  995.       vars_of_tooltalk ();
  996. #endif
  997.  
  998. #ifdef SUNPRO
  999.       vars_of_sunpro ();
  1000. #endif
  1001.  
  1002.       /* Now initialize any specifier variables.  We do this later
  1003.      because it has some dependence on the vars initialized
  1004.      above.
  1005.  
  1006.      These functions should *only* initialize specifier variables,
  1007.      and may make use of the following functions/macros in addition
  1008.      to the ones listed above:
  1009.  
  1010.      DEFVAR_SPECIFIER()
  1011.      Fmake_specifier()
  1012.      set_specifier_fallback()
  1013.      set_specifier_caching()
  1014.      */
  1015.  
  1016.       specifier_vars_of_glyphs ();
  1017.       specifier_vars_of_menubar ();
  1018.       specifier_vars_of_redisplay ();
  1019.       specifier_vars_of_scrollbar ();
  1020.       specifier_vars_of_toolbar ();
  1021.       specifier_vars_of_window ();
  1022.  
  1023.       /* Now comes all the rest of the variables that couldn't
  1024.      be handled above.  There may be dependencies on variables
  1025.      initialized above, and dependencies between one complex_vars_()
  1026.      function and another. */
  1027.  
  1028.       /* This calls Fmake_glyph_internal(), which creates specifiers
  1029.      and also relies on a variable (Vthe_nothing_vector) initialized
  1030.      above. */
  1031.       complex_vars_of_glyphs ();
  1032.  
  1033.       /* This relies on the glyphs just created in the previous function,
  1034.      and calls Fadd_spec_to_specifier(), which relies on various
  1035.      variables initialized above. */
  1036. #ifdef HAVE_X_WINDOWS
  1037.       complex_vars_of_glyphs_x ();
  1038. #endif
  1039.  
  1040.       /* This calls make_strings_hashtable().  That doesn't currently
  1041.      rely on anything, but it's strange enough not to add it to
  1042.      the "safe" list of functions. */
  1043. #ifdef ENERGIZE
  1044.       complex_vars_of_energize ();
  1045. #endif
  1046.  
  1047.       /* This calls Fcopy_syntax_table(), which calls concat(), which
  1048.      may rely on some variables initialized above. */
  1049.       complex_vars_of_syntax ();
  1050.  
  1051.       /* This initializes buffer-local variables, sets things up so that
  1052.      buffers can be created, and creates a couple of basic buffers.
  1053.      This depends on Vstandard_syntax_table (initialized in the
  1054.      previous function), as well as a whole horde of variables
  1055.      that may have been initialized above. */
  1056.       complex_vars_of_buffer ();
  1057.  
  1058.       /* This creates a couple more buffers, and depends on the
  1059.      previous function. */
  1060.       complex_vars_of_minibuf ();
  1061.  
  1062.       /* This calls Fmake_variable_buffer_local(), which (might)
  1063.      depend on buffer-local variables being initialized. */
  1064.       complex_vars_of_event_stream ();
  1065.  
  1066.       /* These two might call Ffile_name_as_directory(), which
  1067.      might depend on all sorts of things; I'm not sure. */
  1068.       complex_vars_of_callproc ();
  1069. #ifdef CLASH_DETECTION
  1070.       complex_vars_of_filelock ();
  1071. #endif /* CLASH_DETECTION */
  1072.  
  1073.       /* This creates a couple of basic keymaps and depends on Lisp
  1074.      hashtables and Ffset() (both of which depend on some variables
  1075.      initialized in the vars_of_*() section) and possibly other
  1076.      stuff. */
  1077.       complex_vars_of_keymap ();
  1078.  
  1079.       if (always_gc)                /* purification debugging hack */
  1080.     garbage_collect_1 ();
  1081.     }
  1082.  
  1083.   /* CONGRATULATIONS!!!  We have successfully initialized the Lisp
  1084.      engine. */
  1085.  
  1086.   if (initialized)
  1087.     {
  1088.       /* Stuff that needs to be reset at run time.  Order below should
  1089.      not matter. */
  1090.       reinit_alloc ();
  1091.       reinit_eval ();
  1092. #ifdef MULE_REGEXP
  1093.       reinit_mule_category ();
  1094. #endif
  1095.     }
  1096.  
  1097.   /* Now do further initialization/setup of stuff that is not needed by the
  1098.      syms_of_() routines.  This involves stuff that only is enabled in
  1099.      an interactive run (redisplay, user input, etc.) and stuff that is
  1100.      not needed until we start loading Lisp code (the reader).  A lot
  1101.      of this stuff involves querying the current environment and needs
  1102.      to be done both at dump time and at run time. */
  1103.  
  1104.   init_callproc ();    /* Set up the process environment (so that egetenv
  1105.                works), the basic directory variables
  1106.                (exec-directory and so on), and stuff
  1107.                related to subprocesses.  This should be
  1108.                first because many of the functions below
  1109.                call egetenv() to get environment variables. */
  1110.   init_lread ();    /* Set up the Lisp reader. */
  1111. #ifdef MSDOS
  1112.   /* Call early 'cause init_environment needs it.  */
  1113.   init_dosfns ();
  1114.   /* Set defaults for several environment variables.  */
  1115.   init_environment (argc, argv, skip_args);
  1116. #endif
  1117.   init_cmdargs (argc, argv, skip_args);    /* Create list Vcommand_line_args */
  1118.   init_buffer (); /* Init default directory of *scratch* buffer */
  1119. #ifdef VMS
  1120.   init_vms_input ();    /* init_redisplay calls get_tty_device_size,
  1121.                that needs this */
  1122. #endif /* VMS */
  1123.   init_redisplay ();      /* Determine terminal type.
  1124.                  init_sys_modes uses results */
  1125. #ifdef VMS
  1126.   init_vmsproc ();
  1127.   init_vmsfns ();
  1128. #endif /* VMS */
  1129.   init_event_stream (); /* Set up so we can get user input. */
  1130.   init_macros (); /* set up so we can run macros. */
  1131.   init_editfns (); /* Determine the name of the user we're running as */
  1132.   init_xemacs_process (); /* set up for calling subprocesses */
  1133. #ifdef SUNPRO
  1134.   init_sunpro (); /* Set up Sunpro usage tracking */
  1135. #endif
  1136.   init_keyboard (); /* misc. stuff pertaining to user input. */
  1137.   init_faces (); /* Initialize the faces from resources */
  1138. #if defined (HAVE_NATIVE_SOUND) && defined (hp9000s800)
  1139.   init_hpplay ();
  1140. #endif
  1141.   init_device_tty ();
  1142.   init_device_stream (); /* Create the first device */
  1143.  
  1144.   /* try to get the actually pathname of the exec file we are running */
  1145.   {
  1146.     Vinvocation_name = Fcar (Vcommand_line_args);
  1147.     Vinvocation_directory = Vinvocation_name;
  1148.  
  1149.     if (!NILP (Ffile_name_directory (Vinvocation_name)))
  1150.       /* invocation-name includes a directory component -- presumably it
  1151.          is relative to cwd, not $PATH */
  1152.       Vinvocation_directory = Fexpand_file_name (Vinvocation_name,
  1153.                        Qnil);
  1154.     else
  1155.       locate_file (decode_env_path ("PATH", 0),
  1156.            Vinvocation_name, EXEC_SUFFIXES,
  1157.            &Vinvocation_directory, X_OK);
  1158.  
  1159.     if (NILP (Vinvocation_directory))
  1160.       Vinvocation_directory = Vinvocation_name;
  1161.  
  1162.     Vinvocation_name = Ffile_name_nondirectory (Vinvocation_directory);
  1163.     Vinvocation_directory = Ffile_name_directory (Vinvocation_directory);
  1164.   }
  1165.  
  1166. #if defined (LOCALTIME_CACHE)
  1167.   /* sun's localtime() has a bug.  it caches the value of the time
  1168.      zone rather than looking it up every time.  Since localtime() is
  1169.      called to bolt the undumping time into the undumped emacs, this
  1170.      results in localtime() ignoring the TZ environment variable.
  1171.      This flushes the new TZ value into localtime(). */
  1172.   tzset ();
  1173. #endif /* LOCALTIME_CACHE */
  1174.  
  1175.   load_me = Qnil;
  1176.   if (!initialized)
  1177.     {
  1178.       /* Handle -l loadup-and-dump, args passed by Makefile. */
  1179.       if (argc > 2 + skip_args && !strcmp (argv[1 + skip_args], "-l"))
  1180.         load_me = build_string (argv[2 + skip_args]);
  1181. #ifdef CANNOT_DUMP
  1182.       /* Unless next switch is -nl, load "loadup.el" first thing.  */
  1183.       if (!(argc > 1 + skip_args && !strcmp (argv[1 + skip_args], "-nl")))
  1184.     load_me = build_string ("loadup.el");
  1185. #endif /* CANNOT_DUMP */
  1186.     }
  1187.  
  1188. #ifdef QUANTIFY
  1189.   if (initialized)
  1190.     quantify_start_recording_data ();
  1191. #endif /* QUANTIFY */
  1192.  
  1193.   initialized = 1;
  1194.  
  1195.   /* This never returns.  */
  1196.   initial_command_loop (load_me);
  1197.   /* NOTREACHED */
  1198. }
  1199.  
  1200. static jmp_buf run_temacs_catch;
  1201.  
  1202. static int run_temacs_argc;
  1203. static char **run_temacs_argv;
  1204. static char *run_temacs_args;
  1205. static int run_temacs_argv_size;
  1206. static int run_temacs_args_size;
  1207.  
  1208. extern int gc_in_progress;
  1209.  
  1210. DEFUN ("run-emacs-from-temacs",
  1211.        Frun_emacs_from_temacs, Srun_emacs_from_temacs, 0, MANY, 0,
  1212.   "Do not call this.  It will reinitialize your XEmacs.  You'll be sorry.")
  1213. /* If this function is called from startup.el, it will be possible to run
  1214.    temacs as an editor using 'temacs -batch -l loadup.el run-temacs', instead
  1215.    of having to dump an emacs and then run that (when debugging emacs itself,
  1216.    this can be much faster). [Actually, the speed difference isn't that
  1217.    much as long as your filesystem is local, and you don't end up with
  1218.    a dumped version in case you want to rerun it.  This function is most
  1219.    useful when used as part of the `make all-elc' command. --ben]
  1220.    This will \"restart\" emacs with the specified command-line arguments.
  1221.  */
  1222.     (nargs, args)
  1223.     int nargs;
  1224.     Lisp_Object *args;
  1225. {
  1226.   int ac;
  1227.   int namesize;
  1228.   int total_len;
  1229.   Lisp_Object orig_invoc_name = Fcar (Vcommand_line_args);
  1230.  
  1231.   assert (!gc_in_progress);
  1232.  
  1233.   if (run_temacs_argc < 0)
  1234.     error ("I've lost my temacs-hood.");
  1235.  
  1236.   namesize = string_length (XSTRING (orig_invoc_name)) + 1;
  1237.  
  1238.   for (ac = 0, total_len = namesize; ac < nargs; ac++)
  1239.     {
  1240.       int s;
  1241.       CHECK_STRING (args[ac], ac);
  1242.       s = string_length (XSTRING (args[ac])) + 1;
  1243.       total_len += s;
  1244.     }
  1245.   DO_REALLOC (run_temacs_args, run_temacs_args_size, total_len, char);
  1246.   DO_REALLOC (run_temacs_argv, run_temacs_argv_size, nargs+1, char *);
  1247.  
  1248.   memcpy (run_temacs_args, (char *) string_data (XSTRING (orig_invoc_name)),
  1249.       namesize);
  1250.   run_temacs_argv [0] = run_temacs_args;
  1251.   for (ac = 0; ac < nargs; ac++)
  1252.     {
  1253.       int s;
  1254.       s = string_length (XSTRING (args[ac])) + 1;
  1255.       memcpy (run_temacs_args + namesize, string_data (XSTRING (args[ac])), s);
  1256.       run_temacs_argv [ac + 1] = run_temacs_args + namesize;
  1257.       namesize += s;
  1258.     }
  1259.   run_temacs_argv [nargs + 1] = 0;
  1260.   catchlist = NULL; /* Important!  Otherwise free_cons() calls in
  1261.                condition_case_unwind() may lead to GC death. */
  1262.   unbind_to (0, Qnil); /* this closes loadup.el */
  1263.   purify_flag = 0;
  1264.   run_temacs_argc = nargs + 1;
  1265.   report_pure_usage (1, 0);
  1266.   longjmp (run_temacs_catch, 1);
  1267.   return Qnil; /* not reached; warning suppression */
  1268. }
  1269.  
  1270. /* ARGSUSED */
  1271. DOESNT_RETURN
  1272. main (int argc, char **argv, char **envp)
  1273. {
  1274. #ifdef QUANTIFY
  1275.   quantify_stop_recording_data ();
  1276.   quantify_clear_data ();
  1277. #endif /* QUANTIFY */
  1278.  
  1279.   suppress_early_backtrace = 0;
  1280.   lim_data = 0; /* force reinitialization of this variable */
  1281.  
  1282.   if (sizeof (Lisp_Object) != sizeof (void *))
  1283.     abort (); /* Lisp_Object must fit in a word;
  1284.          check VALBITS and GCTYPEBITS */
  1285.   if (!initialized)
  1286.   {
  1287.     run_temacs_argc = 0;
  1288.     if (! setjmp (run_temacs_catch))
  1289.       main_1 (argc, argv, envp);
  1290.     /* run-emacs-from-temacs called */
  1291.     argc = run_temacs_argc;
  1292.     run_temacs_argc = 0;
  1293.     argv = run_temacs_argv;
  1294.     envp = environ;
  1295.   }
  1296.   run_temacs_argc = -1;
  1297.  
  1298.   main_1 (argc, argv, envp);
  1299. }
  1300.  
  1301.  
  1302. DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
  1303.   "Exit the XEmacs job and kill it.  Ask for confirmation, without argument.\n\
  1304. If ARG is an integer, return ARG as the exit program code.\n\
  1305. If ARG is a  string, stuff it as keyboard input.\n\n\
  1306. The value of `kill-emacs-hook', if not void,\n\
  1307. is a list of functions (of no args),\n\
  1308. all of which are called before XEmacs is actually killed.")
  1309.   (arg)
  1310.      Lisp_Object arg;
  1311. {
  1312.   /* This function can GC */
  1313.   struct gcpro gcpro1;
  1314.  
  1315.   GCPRO1 (arg);
  1316.  
  1317.   if (feof (stdin))
  1318.     arg = Qt;
  1319.  
  1320.   if (!NILP (Vrun_hooks) && !noninteractive)
  1321.     call1 (Vrun_hooks, Qkill_emacs_hook);
  1322.  
  1323.   UNGCPRO;
  1324.  
  1325. /* Is it really necessary to do this deassign
  1326.    when we are going to exit anyway?  */
  1327. /* #ifdef VMS
  1328.   stop_vms_input ();
  1329.  #endif  */
  1330.  
  1331.   shut_down_emacs (0, 0, ((STRINGP (arg)) ? arg : Qnil));
  1332.  
  1333.   exit ((INTP (arg)) ? XINT (arg)
  1334. #ifdef VMS
  1335.     : 1
  1336. #else
  1337.     : 0
  1338. #endif
  1339.     );
  1340.   /* NOTREACHED */
  1341.   return Qnil; /* I'm sick of the compiler warning */
  1342. }
  1343.  
  1344. /* Perform an orderly shutdown of XEmacs.  Autosave any modified
  1345.    buffers, kill any child processes, clean up the terminal modes (if
  1346.    we're in the foreground), and other stuff like that.  Don't perform
  1347.    any redisplay; this may be called when XEmacs is shutting down in
  1348.    the background, or after its X connection has died.
  1349.  
  1350.    If SIG is a signal number, print a message for it.
  1351.  
  1352.    This is called by fatal signal handlers, X protocol error handlers,
  1353.    and Fkill_emacs.  */
  1354. static void
  1355. shut_down_emacs (int sig, int no_x, Lisp_Object stuff)
  1356. {
  1357.   Lisp_Object dev;
  1358.  
  1359.   /* This function can GC */
  1360.   /* Prevent running of hooks from now on.  */
  1361.   Vrun_hooks = Qnil;
  1362.  
  1363. #ifdef QUANTIFY
  1364.   quantify_stop_recording_data ();
  1365. #endif /* QUANTIFY */
  1366.  
  1367.   fflush (stdout);
  1368.   reset_all_devices ();
  1369.   if (sig && sig != SIGTERM)
  1370.     {
  1371.       stderr_out ("\nFatal error (%d).\n", sig);
  1372.       stderr_out ("Your files have been auto-saved.\n");
  1373.     }
  1374.  
  1375.   stuff_buffered_input (stuff);
  1376.  
  1377.   Fdo_auto_save (Qt,
  1378.          Qnil); /* do this before anything hazardous */
  1379.   kill_buffer_processes (Qnil);
  1380.  
  1381. #ifdef CLASH_DETECTION
  1382.   unlock_all_files ();
  1383. #endif
  1384.  
  1385. #ifdef TOOLTALK
  1386.   tt_session_quit (tt_default_session ());
  1387.   tt_close ();
  1388. #endif
  1389.  
  1390. #ifdef VMS
  1391.   kill_vms_processes ();
  1392. #endif
  1393.  
  1394.   DEVICE_LOOP (dev)
  1395.     Fdelete_device (XCAR (dev));
  1396. }
  1397.  
  1398.  
  1399. #ifndef CANNOT_DUMP
  1400. /* Nothing like this can be implemented on an Apollo.
  1401.    What a loss!  */
  1402.  
  1403. extern int my_edata;
  1404.  
  1405. #ifdef HAVE_SHM
  1406.  
  1407. DEFUN ("dump-emacs-data", Fdump_emacs_data, Sdump_emacs_data, 1, 1, 0,
  1408.   "Dump current state of XEmacs into data file FILENAME.\n\
  1409. This function exists on systems that use HAVE_SHM.")
  1410.   (intoname)
  1411.      Lisp_Object intoname;
  1412. {
  1413.   /* This function can GC */
  1414.   int opurify;
  1415.   struct gcpro gcpro1;
  1416.   GCPRO1 (intoname);
  1417.  
  1418.   CHECK_STRING (intoname, 0);
  1419.   intoname = Fexpand_file_name (intoname, Qnil);
  1420.  
  1421.   opurify = purify_flag;
  1422.   purify_flag = 0;
  1423.  
  1424.   fflush (stderr);
  1425.   fflush (stdout);
  1426.  
  1427.   disksave_object_finalization ();
  1428.   release_breathing_space ();
  1429.  
  1430.   /* Tell malloc where start of impure now is */
  1431.   /* Also arrange for warnings when nearly out of space.  */
  1432. #ifndef SYSTEM_MALLOC
  1433.   memory_warnings (&my_edata, malloc_warning);
  1434. #endif
  1435.   UNGCPRO;
  1436.   map_out_data (string_data (XSTRING (intoname)));
  1437.  
  1438.   purify_flag = opurify;
  1439.  
  1440.   return Qnil;
  1441. }
  1442.  
  1443. #else /* not HAVE_SHM */
  1444.  
  1445. DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
  1446.   "Dump current state of XEmacs into executable file FILENAME.\n\
  1447. Take symbols from SYMFILE (presumably the file you executed to run XEmacs).\n\
  1448. This is used in the file `loadup.el' when building XEmacs.\n\
  1449. \n\
  1450. Remember to set `command-line-processed' to nil before dumping\n\
  1451. if you want the dumped XEmacs to process its command line\n\
  1452. and announce itself normally when it is run.")
  1453.   (intoname, symname)
  1454.      Lisp_Object intoname, symname;
  1455. {
  1456.   /* This function can GC */
  1457.   struct gcpro gcpro1, gcpro2;
  1458.   int opurify;
  1459.  
  1460.   GCPRO2 (intoname, symname);
  1461.  
  1462. #ifdef FREE_CHECKING
  1463.   Freally_free (Qnil);
  1464.  
  1465.   /* When we're dumping, we can't use the debugging free() */
  1466.  
  1467.   disable_free_hook ();
  1468. #endif
  1469.  
  1470.   CHECK_STRING (intoname, 0);
  1471.   intoname = Fexpand_file_name (intoname, Qnil);
  1472.   if (!NILP (symname))
  1473.     {
  1474.       CHECK_STRING (symname, 0);
  1475.       if (string_length (XSTRING (symname)) > 0)
  1476.     symname = Fexpand_file_name (symname, Qnil);
  1477.       else
  1478.     symname = Qnil;
  1479.     }
  1480.  
  1481.   opurify = purify_flag;
  1482.   purify_flag = 0;
  1483.  
  1484.   report_pure_usage (1, 1);
  1485.  
  1486.   fflush (stderr);
  1487.   fflush (stdout);
  1488.  
  1489.   disksave_object_finalization ();
  1490.   release_breathing_space ();
  1491.  
  1492. #ifdef VMS
  1493.   mapout_data (string_data (XSTRING (intoname)));
  1494. #else
  1495.   /* Tell malloc where start of impure now is */
  1496.   /* Also arrange for warnings when nearly out of space.  */
  1497. #ifndef SYSTEM_MALLOC
  1498.   memory_warnings (&my_edata, malloc_warning);
  1499. #endif
  1500.  
  1501.   UNGCPRO;
  1502.  
  1503. #if defined (MSDOS) && defined (EMX)
  1504.   {
  1505.     int fd = open ((char *) string_data (XSTRING (intoname)),
  1506.                    O_WRONLY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE);
  1507.     if (!fd) {
  1508.       error ("Failure operating on %s", string_data (XSTRING (intoname)));
  1509.     } else {
  1510.       _core (fd);
  1511.       close (fd);
  1512.     }
  1513.   }
  1514. #else /* not MSDOS and EMX */
  1515.   /* here we break our rule that the filename conversion should
  1516.      be performed at the actual time that the system call is made.
  1517.      It's a whole lot easier to do the conversion here than to
  1518.      modify all the unexec routines to ensure that filename
  1519.      conversion is applied everywhere.  Don't worry about memory
  1520.      leakage because this call only happens once. */
  1521.   unexec (string_ext_data_malloc (XSTRING (intoname)),
  1522.       (STRINGP (symname) ? string_ext_data_malloc (XSTRING (symname)) : 0),
  1523.       (unsigned int) &my_edata,
  1524.       0, 0);
  1525. #endif /* not MSDOS and EMX */
  1526. #endif /* not VMS */
  1527.  
  1528.   purify_flag = opurify;
  1529.  
  1530.   return Qnil;
  1531. }
  1532.  
  1533. #endif /* not HAVE_SHM */
  1534.  
  1535. #endif /* not CANNOT_DUMP */
  1536.  
  1537. #ifndef SEPCHAR
  1538. #define SEPCHAR ':'
  1539. #endif
  1540.  
  1541. Lisp_Object
  1542. decode_env_path (CONST char *evarname, CONST char *defalt)
  1543. {
  1544.   REGISTER CONST char *path = 0;
  1545.   REGISTER CONST char *p;
  1546.   Lisp_Object lpath = Qnil;
  1547.  
  1548.   if (evarname)
  1549.     path = (char *) egetenv (evarname);
  1550.   if (!path)
  1551.     path = defalt;
  1552.   if (!path)
  1553.     return (Qnil);
  1554.  
  1555. #if defined (MSDOS) || defined (WIN32)
  1556.   dostounix_filename (path);
  1557. #endif
  1558.  
  1559.   while (1)
  1560.     {
  1561.       p = strchr (path, SEPCHAR);
  1562.       if (!p) p = path + strlen (path);
  1563.       lpath = Fcons (((p != path) ? make_string ((Bufbyte *) path, p - path) :
  1564.               Qnil), lpath);
  1565.       if (*p)
  1566.     path = p + 1;
  1567.       else
  1568.     break;
  1569.     }
  1570.   return Fnreverse (lpath);
  1571. }
  1572.  
  1573. DEFUN ("noninteractive", Fnoninteractive, Snoninteractive, 0, 0, 0,
  1574.   "Non-nil return value means XEmacs is running without interactive terminal.")
  1575.   ()
  1576. {
  1577.   return ((noninteractive) ? Qt : Qnil);
  1578. }
  1579.  
  1580. /* This flag is useful to define if you're under a debugger; this way, you
  1581.    can put a breakpoint of assert_failed() and debug multiple problems
  1582.    in one session without having to recompile. */
  1583. /* #define ASSERTIONS_DONT_ABORT */
  1584.  
  1585. #ifdef USE_ASSERTIONS
  1586. /* This highly dubious kludge ... shut up Jamie, I'm tired of your slagging. */
  1587.  
  1588. void assert_failed (CONST char *file, int line, CONST char *expr)
  1589. {
  1590.   stderr_out ("Fatal error: assertion failed, file %s, line %d, %s\n",
  1591.           file, line, expr);
  1592. #undef abort    /* avoid infinite #define loop... */
  1593. #ifndef ASSERTIONS_DONT_ABORT
  1594.   abort ();
  1595. #endif
  1596. }
  1597. #endif /* USE_ASSERTIONS */
  1598.  
  1599. void
  1600. syms_of_emacs (void)
  1601. {
  1602. #ifndef CANNOT_DUMP
  1603. #ifdef HAVE_SHM
  1604.   defsubr (&Sdump_emacs_data);
  1605. #else
  1606.   defsubr (&Sdump_emacs);
  1607. #endif
  1608. #endif /* !CANNOT_DUMP */
  1609.  
  1610.   defsubr (&Srun_emacs_from_temacs);
  1611.   defsubr (&Sinvocation_name);
  1612.   defsubr (&Skill_emacs);
  1613.   defsubr (&Snoninteractive);
  1614.  
  1615.   defsymbol (&Qkill_emacs_hook, "kill-emacs-hook");
  1616.   defsymbol (&Qsave_buffers_kill_emacs, "save-buffers-kill-emacs");
  1617. }
  1618.  
  1619. void
  1620. vars_of_emacs (void)
  1621. {
  1622.   DEFVAR_BOOL ("suppress-early-error-handler-backtrace",
  1623.            &suppress_early_backtrace,
  1624.     "Non-nil means early error handler shouldn't print a backtrace");
  1625.  
  1626.   DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
  1627.     "Args passed by shell to XEmacs, as a list of strings.");
  1628.  
  1629.   DEFVAR_LISP ("invocation-name", &Vinvocation_name,
  1630.     "The program name that was used to run XEmacs.\n\
  1631. Any directory names are omitted.");
  1632.  
  1633.   DEFVAR_LISP ("invocation-directory", &Vinvocation_directory,
  1634.     "The directory in which the XEmacs executable was found, to run it.\n\
  1635. The value is simply the program name if that directory's name is not known.");
  1636.  
  1637. #if 0 /* FSFmacs */
  1638.   xxDEFVAR_LISP ("installation-directory", &Vinstallation_directory,
  1639.     "A directory within which to look for the `lib-src' and `etc' directories.\n\
  1640. This is non-nil when we can't find those directories in their standard\n\
  1641. installed locations, but we can find them\n\
  1642. near where the XEmacs executable was found.");
  1643. #endif
  1644.  
  1645.   DEFVAR_LISP ("system-type", &Vsystem_type,
  1646.     "Value is symbol indicating type of operating system you are using.");
  1647.   Vsystem_type = intern (SYSTEM_TYPE);
  1648.  
  1649. #ifndef EMACS_CONFIGURATION
  1650. # define EMACS_CONFIGURATION "UNKNOWN"
  1651. #endif
  1652.   DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
  1653.     "Value is string indicating configuration XEmacs was built for.");
  1654.   Vsystem_configuration = Fpurecopy (build_string (EMACS_CONFIGURATION));
  1655.  
  1656.   DEFVAR_BOOL ("noninteractive", &noninteractive1,
  1657.     "Non-nil means XEmacs is running without interactive terminal.");
  1658.  
  1659.   DEFVAR_INT ("emacs-priority", &emacs_priority,
  1660.     "Priority for XEmacs to run at.\n\
  1661. This value is effective only if set before XEmacs is dumped,\n\
  1662. and only if the XEmacs executable is installed with setuid to permit\n\
  1663. it to change priority.  (XEmacs sets its uid back to the real uid.)\n\
  1664. Currently, you need to define SET_EMACS_PRIORITY in `config.h'\n\
  1665. before you compile XEmacs, to enable the code for this feature.");
  1666.   emacs_priority = 0;
  1667. }
  1668.