XEmacs 21.2.30 "Hygeia".
[chise/xemacs-chise.git.1] / src / emacs.c
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    Copyright (C) 1995 Sun Microsystems, Inc.
5
6 This file is part of XEmacs.
7
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* Synched up with: Mule 2.0, FSF 19.28. */
24
25 /* Note: It is necessary to specify <config.h> and not "config.h" in
26    order for the --srcdir type of compilation to work properly.
27    Otherwise the config.h from the srcdir, rather than the one from
28    the build dir, will be used. */
29
30 #include <config.h>
31 #include "lisp.h"
32
33 #include "backtrace.h" /* run-emacs-from-temacs needs this */
34 #include "buffer.h"
35 #include "commands.h"
36 #include "console.h"
37 #include "process.h"
38 #include "redisplay.h"
39 #include "frame.h"
40 #include "sysdep.h"
41
42 #include "syssignal.h" /* Always include before systty.h */
43 #include "systty.h"
44 #include "sysfile.h"
45 #include "systime.h"
46
47 #ifdef QUANTIFY
48 #include <quantify.h>
49 #endif
50
51 #ifdef HAVE_SHLIB
52 #include "sysdll.h"
53 #endif
54
55 #if defined (HAVE_LOCALE_H) && \
56    (defined (I18N2) || defined (I18N3) || defined (I18N4))
57 #include <locale.h>
58 #endif
59
60 #ifdef TOOLTALK
61 #include TT_C_H_PATH
62 #endif
63
64 #if defined (WINDOWSNT)
65 #include <windows.h>
66 #endif
67
68 /* For PATH_EXEC */
69 #include <paths.h>
70
71 #ifdef HEAP_IN_DATA
72 void report_sheap_usage (int die_if_pure_storage_exceeded);
73 #endif
74
75 #if !defined (SYSTEM_MALLOC) && !defined (DOUG_LEA_MALLOC)
76 extern void *(*__malloc_hook)(size_t);
77 extern void *(*__realloc_hook)(void *, size_t);
78 extern void (*__free_hook)(void *);
79 #endif  /* not SYSTEM_MALLOC && not DOUG_LEA_MALLOC */
80
81 /* Command line args from shell, as list of strings */
82 Lisp_Object Vcommand_line_args;
83
84 /* Set nonzero after XEmacs has started up the first time.
85   Prevents reinitialization of the Lisp world and keymaps
86   on subsequent starts.  */
87 int initialized;
88
89 #ifdef DOUG_LEA_MALLOC
90 # include <malloc.h>
91 /* Preserves a pointer to the memory allocated that copies that
92    static data inside glibc's malloc.  */
93 static void *malloc_state_ptr;
94 #endif /* DOUG_LEA_MALLOC */
95
96 # ifdef REL_ALLOC
97 void r_alloc_reinit (void);
98 # endif
99
100 /* Variable whose value is symbol giving operating system type. */
101 Lisp_Object Vsystem_type;
102
103 /* Variable whose value is string giving configuration built for.  */
104 Lisp_Object Vsystem_configuration;
105
106 /* Variable whose value is string containing the configuration options
107    XEmacs was built with.  */
108 Lisp_Object Vsystem_configuration_options;
109
110 /* Version numbers and strings */
111 Lisp_Object Vemacs_major_version;
112 Lisp_Object Vemacs_minor_version;
113 Lisp_Object Vemacs_patch_level;
114 Lisp_Object Vemacs_beta_version;
115 Lisp_Object Vxemacs_codename;
116 #ifdef INFODOCK
117 Lisp_Object Vinfodock_major_version;
118 Lisp_Object Vinfodock_minor_version;
119 Lisp_Object Vinfodock_build_version;
120 #endif
121
122 /* The path under which XEmacs was invoked. */
123 Lisp_Object Vinvocation_path;
124
125 /* The name under which XEmacs was invoked, with any leading directory
126    names discarded.  */
127 Lisp_Object Vinvocation_name;
128
129 /* The directory name from which XEmacs was invoked.  */
130 Lisp_Object Vinvocation_directory;
131
132 #if 0 /* FSFmacs */
133 /* The directory name in which to find subdirs such as lisp and etc.
134    nil means get them only from PATH_LOADSEARCH.  */
135 Lisp_Object Vinstallation_directory;
136 #endif
137
138 Lisp_Object Vemacs_program_name, Vemacs_program_version;
139 Lisp_Object Vexec_path;
140 Lisp_Object Vexec_directory, Vconfigure_exec_directory;
141 Lisp_Object Vlisp_directory, Vconfigure_lisp_directory;
142 Lisp_Object Vmodule_directory, Vconfigure_module_directory;
143 Lisp_Object Vsite_module_directory, Vconfigure_site_module_directory;
144 Lisp_Object Vconfigure_package_path;
145 Lisp_Object Vdata_directory, Vconfigure_data_directory;
146 Lisp_Object Vdoc_directory, Vconfigure_doc_directory;
147 Lisp_Object Vconfigure_lock_directory;
148 Lisp_Object Vdata_directory_list;
149 Lisp_Object Vconfigure_info_directory;
150 Lisp_Object Vsite_directory, Vconfigure_site_directory;
151 Lisp_Object Vconfigure_info_path;
152 Lisp_Object Vinternal_error_checking;
153 Lisp_Object Vmail_lock_methods, Vconfigure_mail_lock_method;
154 Lisp_Object Vpath_separator;
155
156 /* The default base directory XEmacs is installed under. */
157 Lisp_Object Vconfigure_exec_prefix_directory, Vconfigure_prefix_directory;
158
159 /* If nonzero, set XEmacs to run at this priority.  This is also used
160    in child_setup and sys_suspend to make sure subshells run at normal
161    priority. */
162 int emacs_priority;
163
164 /* If non-zero a filter or a sentinel is running.  Tested to save the match
165    data on the first attempt to change it inside asynchronous code. */
166 int running_asynch_code;
167
168 /* If non-zero, a window-system was specified on the command line. */
169 int display_arg;
170
171 /* Type of display specified.  We cannot use a Lisp symbol here because
172    Lisp symbols may not initialized at the time that we set this
173    variable. */
174 const char *display_use;
175
176 /* If non-zero, then the early error handler will only print the error
177    message and exit. */
178 int suppress_early_error_handler_backtrace;
179
180 /* An address near the bottom of the stack.
181    Tells GC how to save a copy of the stack.  */
182 char *stack_bottom;
183
184 #ifdef USG_SHARED_LIBRARIES
185 /* If nonzero, this is the place to put the end of the writable segment
186    at startup.  */
187
188 uintptr_t bss_end = 0;
189 #endif
190
191 /* Number of bytes of writable memory we can expect to be able to get */
192 unsigned int lim_data;
193
194 /* Nonzero means running XEmacs without interactive terminal.  */
195
196 int noninteractive;
197
198 /* Value of Lisp variable `noninteractive'.
199    Normally same as C variable `noninteractive'
200    but nothing terrible happens if user sets this one.  */
201
202 int noninteractive1;
203
204 /* Nonzero means don't perform site-lisp searches at startup */
205 int inhibit_site_lisp;
206
207 /* Nonzero means don't perform site-modules searches at startup */
208 int inhibit_site_modules;
209
210 /* Nonzero means don't respect early packages at startup */
211 int inhibit_early_packages;
212
213 /* Nonzero means don't load package autoloads at startup */
214 int inhibit_autoloads;
215
216 /* Nonzero means print debug information about path searching */
217 int debug_paths;
218
219 /* Save argv and argc.  */
220 static char **initial_argv;
221 static int initial_argc;
222
223 static void sort_args (int argc, char **argv);
224
225 Lisp_Object Qkill_emacs_hook;
226 Lisp_Object Qsave_buffers_kill_emacs;
227
228 extern Lisp_Object Vlisp_EXEC_SUFFIXES;
229
230 \f
231 /* Signal code for the fatal signal that was received */
232 static int fatal_error_code;
233
234 /* Nonzero if handling a fatal error already */
235 static int fatal_error_in_progress;
236
237 static void shut_down_emacs (int sig, Lisp_Object stuff);
238
239 /* Handle bus errors, illegal instruction, etc. */
240 SIGTYPE
241 fatal_error_signal (int sig)
242 {
243   fatal_error_code = sig;
244   signal (sig, SIG_DFL);
245   /* Unblock the signal so that if the same signal gets sent in the
246      code below, we avoid a deadlock. */
247   EMACS_UNBLOCK_SIGNAL (fatal_error_code);
248
249   /* If fatal error occurs in code below, avoid infinite recursion.  */
250   if (! fatal_error_in_progress)
251     {
252       fatal_error_in_progress = dont_check_for_quit = 1;
253       shut_down_emacs (sig, Qnil);
254       stderr_out ("\nLisp backtrace follows:\n\n");
255       Fbacktrace (Qexternal_debugging_output, Qt);
256 # if 0  /* This is evil, rarely useful, and causes grief in some cases. */
257       /* Check for Sun-style stack printing via /proc */
258       {
259         const char *pstack = "/usr/proc/bin/pstack";
260         if (access (pstack, X_OK) == 0)
261           {
262             char buf[100];
263             stderr_out ("\nC backtrace follows:\n"
264                        "(A real debugger may provide better information)\n\n");
265             sprintf (buf, "%s %d >&2", pstack, (int)getpid());
266             system (buf);
267           }
268       }
269 # endif
270     }
271   /* Signal the same code; this time it will really be fatal. */
272   kill (getpid (), fatal_error_code);
273   SIGRETURN;
274 }
275 \f
276
277 DOESNT_RETURN
278 fatal (const char *fmt, ...)
279 {
280   va_list args;
281   va_start (args, fmt);
282
283   fprintf (stderr, "\nXEmacs: ");
284   vfprintf (stderr, GETTEXT (fmt), args);
285   fprintf (stderr, "\n");
286
287   va_end (args);
288   fflush (stderr);
289   exit (1);
290 }
291
292 /* #### The following two functions should be replaced with
293    calls to emacs_doprnt_*() functions, with STREAM set to send out
294    to stdout or stderr.  This is the only way to ensure that
295    I18N3 works properly (many implementations of the *printf()
296    functions, including the ones included in glibc, do not implement
297    the %###$ argument-positioning syntax). */
298
299 /* exactly equivalent to fprintf (stderr, fmt, ...) except that it calls
300    GETTEXT on the format string. */
301
302 int
303 stderr_out (const char *fmt, ...)
304 {
305   int retval;
306   va_list args;
307   va_start (args, fmt);
308
309   retval = vfprintf (stderr, GETTEXT (fmt), args);
310
311   va_end (args);
312   /* fflush (stderr); */
313   return retval;
314 }
315
316 /* exactly equivalent to fprintf (stdout, fmt, ...) except that it calls
317    GETTEXT on the format string. */
318
319 int
320 stdout_out (const char *fmt, ...)
321 {
322   int retval;
323   va_list args;
324   va_start (args, fmt);
325
326   retval = vfprintf (stdout, GETTEXT (fmt), args);
327
328   va_end (args);
329   return retval;
330 }
331
332 #ifdef SIGDANGER
333
334 /* Handler for SIGDANGER.  */
335 SIGTYPE
336 memory_warning_signal (int sig)
337 {
338   /* #### bad bad bad; this function shouldn't do anything except
339      set a flag, or weird corruption could happen. */
340   signal (sig, memory_warning_signal);
341
342   malloc_warning
343     (GETTEXT ("Operating system warns that virtual memory is running low.\n"));
344
345   /* It might be unsafe to call do_auto_save now.  */
346   force_auto_save_soon ();
347 }
348 #endif /* SIGDANGER */
349 \f
350 /* Code for dealing with Lisp access to the Unix command line */
351
352 static Lisp_Object
353 make_arg_list_1 (int argc, char **argv, int skip_args)
354 {
355   Lisp_Object result = Qnil;
356   REGISTER int i;
357
358   for (i = argc - 1; i >= 0; i--)
359     {
360       if (i == 0 || i > skip_args)
361         {
362 #ifdef WINDOWSNT
363           if (i == 0)
364             {
365               /* Do not trust to what crt0 has stuffed into argv[0] */
366               char full_exe_path [MAX_PATH];
367               GetModuleFileName (NULL, full_exe_path, MAX_PATH);
368               result = Fcons (build_ext_string (full_exe_path, Qfile_name),
369                               result);
370 #if defined(HAVE_SHLIB)
371               (void)dll_init(full_exe_path);
372 #endif
373             }
374           else
375 #endif
376             result = Fcons (build_ext_string (argv [i], Qfile_name),
377                             result);
378         }
379     }
380   return result;
381 }
382
383 Lisp_Object
384 make_arg_list (int argc, char **argv)
385 {
386   return make_arg_list_1 (argc, argv, 0);
387 }
388
389 /* Calling functions are also responsible for calling free_argc_argv
390    when they are done with the generated list. */
391 void
392 make_argc_argv (Lisp_Object argv_list, int *argc, char ***argv)
393 {
394   Lisp_Object next;
395   int n = XINT (Flength (argv_list));
396   REGISTER int i;
397   *argv = (char**) xmalloc ((n+1) * sizeof (char*));
398
399   for (i = 0, next = argv_list; i < n; i++, next = XCDR (next))
400     {
401       const char *temp;
402       CHECK_STRING (XCAR (next));
403
404       TO_EXTERNAL_FORMAT (LISP_STRING, XCAR (next),
405                           C_STRING_ALLOCA, temp,
406                           Qnative);
407       (*argv) [i] = xstrdup (temp);
408     }
409   (*argv) [n] = 0;
410   *argc = i;
411 }
412
413 void
414 free_argc_argv (char **argv)
415 {
416   int elt = 0;
417
418   while (argv[elt])
419     {
420       xfree (argv[elt]);
421       elt++;
422     }
423   xfree (argv);
424 }
425
426 static void
427 init_cmdargs (int argc, char **argv, int skip_args)
428 {
429   initial_argv = argv;
430   initial_argc = argc;
431
432   Vcommand_line_args = make_arg_list_1 (argc, argv, skip_args);
433 }
434
435 DEFUN ("invocation-name", Finvocation_name, 0, 0, 0, /*
436 Return the program name that was used to run XEmacs.
437 Any directory names are omitted.
438 */
439        ())
440 {
441   return Fcopy_sequence (Vinvocation_name);
442 }
443
444 DEFUN ("invocation-directory", Finvocation_directory, 0, 0, 0, /*
445 Return the directory name in which the Emacs executable was located.
446 */
447        ())
448 {
449   return Fcopy_sequence (Vinvocation_directory);
450 }
451
452 \f
453 #ifdef I18N4
454                                 /* #### - don't know why I18N4 on SunOS/JLE
455                                    can't deal with this.  It's a potential
456                                    bug that needs to be looked at. */
457 # undef RUN_TIME_REMAP
458 #endif
459
460 #if defined (MULE) && defined (MSDOS) && defined (EMX)
461 /* Setup all of files be input/output'ed with binary translation mode. */
462 asm ("  .text");
463 asm ("L_setbinmode:");
464 asm ("  movl    $1, __fmode_bin");
465 asm ("  ret");
466 asm ("  .stabs  \"___CTOR_LIST__\", 23, 0, 0, L_setbinmode");
467 #endif
468
469 /* Test whether the next argument in ARGV matches SSTR or a prefix of
470    LSTR (at least MINLEN characters).  If so, then if VALPTR is non-null
471    (the argument is supposed to have a value) store in *VALPTR either
472    the next argument or the portion of this one after the equal sign.
473    ARGV is read starting at position *SKIPPTR; this index is advanced
474    by the number of arguments used.
475
476    Too bad we can't just use getopt for all of this, but we don't have
477    enough information to do it right.  */
478
479 static int
480 argmatch (char **argv, int argc, char *sstr, char *lstr,
481           int minlen, char **valptr, int *skipptr)
482 {
483   char *p = NULL;
484   int arglen;
485   char *arg;
486
487   /* Don't access argv[argc]; give up in advance.  */
488   if (argc <= *skipptr + 1)
489     return 0;
490
491   arg = argv[*skipptr+1];
492   if (arg == NULL)
493     return 0;
494   if (strcmp (arg, sstr) == 0)
495     {
496       if (valptr != NULL)
497         {
498           *valptr = argv[*skipptr+2];
499           *skipptr += 2;
500         }
501       else
502         *skipptr += 1;
503       return 1;
504     }
505   arglen = (valptr != NULL && (p = strchr (arg, '=')) != NULL
506             ? p - arg : strlen (arg));
507   if (lstr == 0 || arglen < minlen || strncmp (arg, lstr, arglen) != 0)
508     return 0;
509   else if (valptr == NULL)
510     {
511       *skipptr += 1;
512       return 1;
513     }
514   else if (p != NULL)
515     {
516       *valptr = p+1;
517       *skipptr += 1;
518       return 1;
519     }
520   else if (argv[*skipptr+2] != NULL)
521     {
522       *valptr = argv[*skipptr+2];
523       *skipptr += 2;
524       return 1;
525     }
526   else
527     {
528       return 0;
529     }
530 }
531
532 /* Make stack traces always identify version + configuration */
533 #define main_1 STACK_TRACE_EYE_CATCHER
534
535 /* This function is not static, so that the compiler is less likely to
536    inline it, which would make it not show up in stack traces.  */
537 DECLARE_DOESNT_RETURN (main_1 (int, char **, char **, int));
538 DOESNT_RETURN
539 main_1 (int argc, char **argv, char **envp, int restart)
540 {
541   char stack_bottom_variable;
542   int skip_args = 0;
543   Lisp_Object load_me;
544   int inhibit_window_system;
545 #ifdef NeXT
546   extern int malloc_cookie;
547 #endif
548
549 #if (!defined (SYSTEM_MALLOC) && !defined (HAVE_LIBMCHECK)      \
550      && !defined (DOUG_LEA_MALLOC))
551   /* Make sure that any libraries we link against haven't installed a
552      hook for a gmalloc of a potentially incompatible version. */
553   /* If we're using libmcheck, the hooks have already been initialized, */
554   /* don't touch them. -slb */
555   __malloc_hook = NULL;
556   __realloc_hook = NULL;
557   __free_hook = NULL;
558 #endif /* not SYSTEM_MALLOC or HAVE_LIBMCHECK or DOUG_LEA_MALLOC */
559
560   noninteractive = 0;
561
562 #ifdef NeXT
563   /* 19-Jun-1995 -baw
564    * NeXT secret magic, ripped from Emacs-for-NS by Carl Edman
565    * <cedman@princeton.edu>.  Note that even Carl doesn't know what this
566    * does; it was provided by NeXT, and it presumable makes NS's mallocator
567    * work with dumping.  But malloc_jumpstart() and malloc_freezedry() in
568    * unexnext.c are both completely undocumented, even in NS header files!
569    * But hey, it solves all NS related memory problems, so who's
570    * complaining? */
571   if (initialized && malloc_jumpstart (malloc_cookie) != 0)
572     fprintf (stderr, "malloc jumpstart failed!\n");
573 #endif /* NeXT */
574
575   /*
576 #if defined (GNU_MALLOC) && \
577     defined (ERROR_CHECK_MALLOC) && \
578     !defined (HAVE_LIBMCHECK)
579   */
580 #if defined(LOSING_GCC_DESTRUCTOR_FREE_BUG)
581   /* Prior to XEmacs 21, this was `#if 0'ed out.  */
582   /* I'm enabling this because it is the only reliable way I've found to */
583   /* prevent a very annoying problem where GCC will attempt to free(3) */
584   /* memory at exit() and cause a coredump. */
585   init_free_hook ();
586 #endif
587
588   sort_args (argc, argv);
589
590 #if (defined (MSDOS) && defined (EMX)) || defined (WIN32) || defined (_SCO_DS)
591   environ = envp;
592 #endif
593
594   /* Record (approximately) where the stack begins.  */
595   stack_bottom = &stack_bottom_variable;
596
597 #ifdef USG_SHARED_LIBRARIES
598   if (bss_end)
599     brk ((void *) bss_end);
600 #endif
601
602   clearerr (stdin);
603
604 #if defined (HAVE_MMAP) && defined (REL_ALLOC)
605   /* ralloc can only be used if using the GNU memory allocator. */
606   init_ralloc ();
607 #elif defined (REL_ALLOC) && !defined(DOUG_LEA_MALLOC)
608   if (initialized)
609     init_ralloc();
610 #endif
611
612 #ifdef HAVE_SOCKS
613   if (initialized)
614     SOCKSinit (argv[0]);
615 #endif /* HAVE_SOCKS */
616
617 #ifndef SYSTEM_MALLOC
618   if (!initialized)
619     /* Arrange to get warning messages as memory fills up.  */
620     memory_warnings (0, malloc_warning);
621 #endif  /* not SYSTEM_MALLOC */
622
623 #ifdef MSDOS
624   /* We do all file input/output as binary files.  When we need to translate
625      newlines, we do that manually.  */
626   _fmode = O_BINARY;
627   (stdin) ->_flag &= ~_IOTEXT;
628   (stdout)->_flag &= ~_IOTEXT;
629   (stderr)->_flag &= ~_IOTEXT;
630 #endif /* MSDOS */
631
632 #ifdef SET_EMACS_PRIORITY
633   if (emacs_priority != 0)
634     nice (-emacs_priority);
635   setuid (getuid ());
636 #endif /* SET_EMACS_PRIORITY */
637
638 #ifdef EXTRA_INITIALIZE
639   EXTRA_INITIALIZE;
640 #endif
641
642 #ifdef HAVE_WINDOW_SYSTEM
643   inhibit_window_system = 0;
644 #else
645   inhibit_window_system = 1;
646 #endif
647
648   /* Handle the -t switch, which specifies filename to use as terminal */
649   {
650     char *term;
651     if (argmatch (argv, argc, "-t", "--terminal", 4, &term, &skip_args))
652       {
653         close (0);
654         close (1);
655         if (open (term, O_RDWR | OPEN_BINARY, 2) < 0)
656           fatal ("%s: %s", term, strerror (errno));
657         dup (0);
658         if (! isatty (0))
659           fatal ("%s: not a tty", term);
660
661 #if 0
662         stderr_out ("Using %s", ttyname (0));
663 #endif
664         stderr_out ("Using %s", term);
665         inhibit_window_system = 1;      /* -t => -nw */
666       }
667   }
668
669   /* Handle -nw switch */
670   if (argmatch (argv, argc, "-nw", "--no-windows", 6, NULL, &skip_args))
671     inhibit_window_system = 1;
672
673   /* Handle the -batch switch, which means don't do interactive display.  */
674   if (argmatch (argv, argc, "-batch", "--batch", 5, NULL, &skip_args))
675     {
676 #if 0 /* I don't think this is correct. */
677       inhibit_autoloads = 1;
678 #endif
679       noninteractive = 1;
680     }
681
682   if (argmatch (argv, argc, "-debug-paths", "--debug-paths",
683                 11, NULL, &skip_args))
684       debug_paths = 1;
685
686   /* Partially handle -no-autoloads, -no-early-packages and -vanilla.  Packages */
687   /* are searched prior to the rest of the command line being parsed in */
688   /* startup.el */
689   if (argmatch (argv, argc, "-no-early-packages", "--no-early-packages",
690                 6, NULL, &skip_args))
691     {
692       inhibit_early_packages = 1;
693       skip_args--;
694     }
695 #ifdef HAVE_SHLIB
696   if (argmatch (argv, argc, "-no-site-modules", "--no-site-modules",
697                 9, NULL, &skip_args))
698     {
699       inhibit_site_modules = 1;
700       skip_args--;
701     }
702 #else
703   inhibit_site_modules = 1;
704 #endif
705   if (argmatch (argv, argc, "-vanilla", "--vanilla",
706                 7, NULL, &skip_args))
707     {
708       inhibit_early_packages = 1;
709       skip_args--;
710     }
711
712   if (argmatch (argv, argc, "-no-autoloads", "--no-autoloads",
713                 7, NULL, &skip_args))
714     {
715       /* Inhibit everything */
716       inhibit_autoloads = 1;
717       skip_args--;
718     }
719
720   if (argmatch (argv, argc, "-debug-paths", "--debug-paths",
721                 6, NULL, &skip_args))
722     {
723       debug_paths = 1;
724       skip_args--;
725     }
726
727
728   /* Partially handle the -version and -help switches: they imply -batch,
729      but are not removed from the list. */
730   if (argmatch (argv, argc, "-help", "--help",   3, NULL, &skip_args))
731     noninteractive = 1, skip_args--;
732
733   if (argmatch (argv, argc, "-version", "--version", 3, NULL, &skip_args) ||
734       argmatch (argv, argc, "-V",    0,              2, NULL, &skip_args))
735       noninteractive = 1, skip_args--;
736
737   /* Now, figure out which type of console is our first console. */
738
739   display_arg = 0;
740
741   if (noninteractive)
742     display_use = "stream";
743   else
744     display_use = "tty";
745
746 #ifndef HAVE_TTY
747   if (inhibit_window_system)
748     fatal ("Sorry, this XEmacs was not compiled with TTY support");
749 #endif
750
751 #ifdef HAVE_WINDOW_SYSTEM
752   /* Stupid kludge to catch command-line display spec.  We can't
753      handle this argument entirely in window-system-dependent code
754      because we don't even know which window-system-dependent code
755      to run until we've recognized this argument.  */
756   if (!inhibit_window_system && !noninteractive)
757     {
758 #ifdef HAVE_X_WINDOWS
759       char *dpy = 0;
760       int count_before = skip_args;
761
762       if (argmatch (argv, argc, "-d", "--display", 3, &dpy, &skip_args) ||
763           argmatch (argv, argc, "-display", 0,     3, &dpy, &skip_args))
764         {
765           display_arg = 1;
766           display_use = "x";
767         }
768       /* If we have the form --display=NAME,
769          convert it into  -d name.
770          This requires inserting a new element into argv.  */
771       if (dpy != 0 && skip_args - count_before == 1)
772         {
773           char **new = (char **) xmalloc (sizeof (char *) * (argc + 2));
774           int j;
775
776           for (j = 0; j < count_before + 1; j++)
777             new[j] = argv[j];
778           new[count_before + 1] = "-d";
779           new[count_before + 2] = dpy;
780           for (j = count_before + 2; j <argc; j++)
781             new[j + 1] = argv[j];
782           argv = new;
783           argc++;
784         }
785       /* Change --display to -d, when its arg is separate.  */
786       else if (dpy != 0 && skip_args > count_before
787                && argv[count_before + 1][1] == '-')
788         argv[count_before + 1] = "-d";
789
790       /* Don't actually discard this arg.  */
791       skip_args = count_before;
792
793       /* If there is a non-empty environment var DISPLAY, set
794          `display_use', but not `display_arg', which is only to be set
795          if the display was specified on the command line. */
796       if ((dpy = getenv ("DISPLAY")) && dpy[0])
797         display_use = "x";
798
799 #endif /* HAVE_X_WINDOWS */
800 #ifdef HAVE_MS_WINDOWS
801       if (strcmp(display_use, "x") != 0)
802         display_use = "mswindows";
803 #endif /* HAVE_MS_WINDOWS */
804     }
805 #endif /* HAVE_WINDOW_SYSTEM */
806
807   noninteractive1 = noninteractive;
808
809   /****** Now initialize everything *******/
810
811   /* First, do really basic environment initialization -- catching signals
812      and the like.  These functions have no dependence on any part of
813      the Lisp engine and need to be done both at dump time and at run time. */
814
815   init_signals_very_early ();
816   init_data_very_early (); /* Catch math errors. */
817 #ifdef LISP_FLOAT_TYPE
818   init_floatfns_very_early (); /* Catch floating-point math errors. */
819 #endif
820   init_process_times_very_early (); /* Initialize our process timers.
821                                        As early as possible, of course,
822                                        so we can be fairly accurate. */
823   init_intl_very_early (); /* set up the locale and domain for gettext and
824                               such. */
825
826   /* Now initialize the Lisp engine and the like.  Done only during
827      dumping.  No dependence on anything that may be in the user's
828      environment when the dumped XEmacs is run.
829
830      We try to do things in an order that minimizes the non-obvious
831      dependencies between functions. */
832
833   /* purify_flag 1 is correct even if CANNOT_DUMP.
834    * loadup.el will set to nil at end. */
835
836   purify_flag = 0;
837 #ifdef PDUMP
838   if (restart)
839     initialized = 1;
840   else {
841     initialized = pdump_load ();
842     purify_flag = !initialized;
843   }
844 #else
845   if (!initialized)
846     purify_flag = 1;
847 #endif
848
849   if (!initialized)
850     {
851       /* Initialize things so that new Lisp objects
852          can be created and objects can be staticpro'd.
853          Must be basically the very first thing done
854          because pretty much all of the initialization
855          routines below create new objects. */
856       init_alloc_once_early ();
857
858       /* Initialize Qnil, Qt, Qunbound, and the
859          obarray.  After this, symbols can be
860          interned.  This depends on init_alloc_once_early(). */
861       init_symbols_once_early ();
862
863       /* Declare the basic symbols pertaining to errors,
864          So that deferror() can be called. */
865       init_errors_once_early ();
866
867       /* Make sure that opaque pointers can be created. */
868       init_opaque_once_early ();
869
870       /* Now declare all the symbols and define all the Lisp primitives.
871
872          The *only* thing that the syms_of_*() functions are allowed to do
873          is call one of the following three functions:
874
875          INIT_LRECORD_IMPLEMENTATION()
876          defsymbol()
877          defsubr() (i.e. DEFSUBR)
878          deferror()
879          defkeyword()
880
881          Order does not matter in these functions.
882          */
883
884       syms_of_abbrev ();
885       syms_of_alloc ();
886       syms_of_buffer ();
887       syms_of_bytecode ();
888       syms_of_callint ();
889       syms_of_callproc ();
890       syms_of_casefiddle ();
891       syms_of_casetab ();
892       syms_of_chartab ();
893       syms_of_cmdloop ();
894       syms_of_cmds ();
895       syms_of_console ();
896       syms_of_data ();
897 #ifdef DEBUG_XEMACS
898       syms_of_debug ();
899       syms_of_tests ();
900 #endif /* DEBUG_XEMACS */
901       syms_of_device ();
902 #ifdef HAVE_DIALOGS
903       syms_of_dialog ();
904 #endif
905       syms_of_dired ();
906       syms_of_doc ();
907       syms_of_editfns ();
908       syms_of_elhash ();
909       syms_of_emacs ();
910       syms_of_eval ();
911 #ifdef HAVE_X_WINDOWS
912       syms_of_event_Xt ();
913 #endif
914 #ifdef HAVE_DRAGNDROP
915       syms_of_dragdrop ();
916 #endif
917       syms_of_event_stream ();
918       syms_of_events ();
919       syms_of_extents ();
920       syms_of_faces ();
921       syms_of_fileio ();
922 #ifdef CLASH_DETECTION
923       syms_of_filelock ();
924 #endif /* CLASH_DETECTION */
925       syms_of_floatfns ();
926       syms_of_fns ();
927       syms_of_font_lock ();
928       syms_of_frame ();
929       syms_of_general ();
930       syms_of_glyphs ();
931       syms_of_glyphs_eimage ();
932       syms_of_glyphs_widget ();
933 #if defined (HAVE_MENUBARS) || defined (HAVE_SCROLLBARS) || defined (HAVE_DIALOGS) || defined (HAVE_TOOLBARS)
934       syms_of_gui ();
935 #endif
936       syms_of_gutter ();
937       syms_of_indent ();
938       syms_of_intl ();
939       syms_of_keymap ();
940       syms_of_lread ();
941       syms_of_macros ();
942       syms_of_marker ();
943       syms_of_md5 ();
944 #ifdef HAVE_DATABASE
945       syms_of_database ();
946 #endif
947 #ifdef HAVE_MENUBARS
948       syms_of_menubar ();
949 #endif
950       syms_of_minibuf ();
951 #ifdef HAVE_SHLIB
952       syms_of_module ();
953 #endif
954       syms_of_objects ();
955       syms_of_print ();
956 #if !defined (NO_SUBPROCESSES)
957       syms_of_process ();
958 #ifdef HAVE_WIN32_PROCESSES
959       syms_of_process_nt ();
960 #endif
961 #endif
962       syms_of_profile ();
963 #if defined (HAVE_MMAP) && defined (REL_ALLOC) && !defined(DOUG_LEA_MALLOC)
964       syms_of_ralloc ();
965 #endif /* HAVE_MMAP && REL_ALLOC */
966       syms_of_rangetab ();
967       syms_of_redisplay ();
968       syms_of_search ();
969       syms_of_select ();
970       syms_of_signal ();
971       syms_of_sound ();
972       syms_of_specifier ();
973       syms_of_symbols ();
974       syms_of_syntax ();
975 #ifdef HAVE_SCROLLBARS
976       syms_of_scrollbar ();
977 #endif
978 #ifdef HAVE_TOOLBARS
979       syms_of_toolbar ();
980 #endif
981       syms_of_undo ();
982       syms_of_widget ();
983       syms_of_window ();
984
985 #ifdef HAVE_TTY
986       syms_of_console_tty ();
987       syms_of_device_tty ();
988       syms_of_objects_tty ();
989 #endif
990
991 #ifdef HAVE_X_WINDOWS
992       syms_of_balloon_x ();
993       syms_of_device_x ();
994 #ifdef HAVE_DIALOGS
995       syms_of_dialog_x ();
996 #endif
997       syms_of_frame_x ();
998       syms_of_glyphs_x ();
999       syms_of_objects_x ();
1000 #ifdef HAVE_MENUBARS
1001       syms_of_menubar_x ();
1002 #endif
1003       syms_of_select_x ();
1004 #if defined (HAVE_MENUBARS) || defined (HAVE_SCROLLBARS) || defined (HAVE_DIALOGS) || defined (HAVE_TOOLBARS)
1005       syms_of_gui_x ();
1006 #endif
1007 #ifdef HAVE_XIM
1008 #ifdef XIM_XLIB
1009       syms_of_input_method_xlib ();
1010 #endif
1011 #endif /* HAVE_XIM */
1012 #endif /* HAVE_X_WINDOWS */
1013
1014 #ifdef HAVE_MS_WINDOWS
1015       syms_of_console_mswindows ();
1016       syms_of_device_mswindows ();
1017       syms_of_frame_mswindows ();
1018       syms_of_objects_mswindows ();
1019       syms_of_select_mswindows ();
1020       syms_of_glyphs_mswindows ();
1021       syms_of_gui_mswindows ();
1022 #ifdef HAVE_MENUBARS
1023       syms_of_menubar_mswindows ();
1024 #endif
1025 #ifdef HAVE_SCROLLBARS
1026       syms_of_scrollbar_mswindows ();
1027 #endif
1028 #ifdef HAVE_MSW_C_DIRED
1029       syms_of_dired_mswindows ();
1030 #endif
1031 #ifdef WINDOWSNT
1032       syms_of_ntproc ();
1033 #endif
1034 #endif  /* HAVE_MS_WINDOWS */
1035
1036 #ifdef MULE
1037       syms_of_mule ();
1038       syms_of_mule_ccl ();
1039       syms_of_mule_charset ();
1040 #endif
1041 #ifdef FILE_CODING
1042       syms_of_file_coding ();
1043 #endif
1044 #ifdef MULE
1045 #ifdef HAVE_WNN
1046       syms_of_mule_wnn ();
1047 #endif
1048 #ifdef HAVE_CANNA
1049       syms_of_mule_canna ();
1050 #endif /* HAVE_CANNA */
1051 #endif /* MULE */
1052
1053 #ifdef SYMS_SYSTEM
1054       SYMS_SYSTEM;
1055 #endif
1056
1057 #ifdef SYMS_MACHINE
1058       SYMS_MACHINE;
1059 #endif
1060
1061       /*
1062 #if defined (GNU_MALLOC) && \
1063     defined (ERROR_CHECK_MALLOC) && \
1064     !defined (HAVE_LIBMCHECK)
1065       */
1066       /* Prior to XEmacs 21, this was `#if 0'ed out. -slb */
1067 #if defined (LOSING_GCC_DESTRUCTOR_FREE_BUG)
1068       syms_of_free_hook ();
1069 #endif
1070
1071 #ifdef TOOLTALK
1072       syms_of_tooltalk ();
1073 #endif
1074
1075 #ifdef SUNPRO
1076       syms_of_sunpro ();
1077 #endif
1078
1079 #ifdef HAVE_LDAP
1080       syms_of_eldap ();
1081 #endif
1082
1083 #ifdef HAVE_GPM
1084           syms_of_gpmevent ();
1085 #endif
1086
1087       /* Now create the subtypes for the types that have them.
1088          We do this before the vars_*() because more symbols
1089          may get initialized here. */
1090
1091       /* Now initialize the console types and associated symbols.
1092          Other than the first function below, the functions may
1093          make exactly the following function/macro calls:
1094
1095          INITIALIZE_CONSOLE_TYPE()
1096          CONSOLE_HAS_METHOD()
1097
1098          For any given console type, the former macro must be called
1099          before the any calls to the latter macro. */
1100
1101       console_type_create ();
1102
1103       console_type_create_stream ();
1104
1105 #ifdef HAVE_TTY
1106       console_type_create_tty ();
1107       console_type_create_device_tty ();
1108       console_type_create_frame_tty ();
1109       console_type_create_objects_tty ();
1110       console_type_create_redisplay_tty ();
1111 #endif
1112
1113 #ifdef HAVE_X_WINDOWS
1114       console_type_create_x ();
1115       console_type_create_device_x ();
1116       console_type_create_frame_x ();
1117       console_type_create_glyphs_x ();
1118       console_type_create_select_x ();
1119 #ifdef HAVE_MENUBARS
1120       console_type_create_menubar_x ();
1121 #endif
1122       console_type_create_objects_x ();
1123       console_type_create_redisplay_x ();
1124 #ifdef HAVE_SCROLLBARS
1125       console_type_create_scrollbar_x ();
1126 #endif
1127 #ifdef HAVE_TOOLBARS
1128       console_type_create_toolbar_x ();
1129 #endif
1130 #ifdef HAVE_DIALOGS
1131       console_type_create_dialog_x ();
1132 #endif
1133 #endif /* HAVE_X_WINDOWS */
1134
1135 #ifdef HAVE_MS_WINDOWS
1136       console_type_create_mswindows ();
1137       console_type_create_device_mswindows ();
1138       console_type_create_frame_mswindows ();
1139       console_type_create_objects_mswindows ();
1140       console_type_create_redisplay_mswindows ();
1141       console_type_create_glyphs_mswindows ();
1142       console_type_create_select_mswindows ();
1143 # ifdef HAVE_SCROLLBARS
1144       console_type_create_scrollbar_mswindows ();
1145 # endif
1146 #ifdef HAVE_MENUBARS
1147       console_type_create_menubar_mswindows ();
1148 #endif
1149 #ifdef HAVE_TOOLBARS
1150       console_type_create_toolbar_mswindows ();
1151 #endif
1152 #ifdef HAVE_DIALOGS
1153       console_type_create_dialog_mswindows ();
1154 #endif
1155 #endif
1156
1157       /* Now initialize the specifier types and associated symbols.
1158          Other than the first function below, the functions may
1159          make exactly the following function/macro calls:
1160
1161          INITIALIZE_SPECIFIER_TYPE()
1162          SPECIFIER_HAS_METHOD()
1163
1164          For any given specifier type, the former macro must be called
1165          before the any calls to the latter macro. */
1166
1167       specifier_type_create ();
1168
1169       specifier_type_create_image ();
1170       specifier_type_create_gutter ();
1171       specifier_type_create_objects ();
1172 #ifdef HAVE_TOOLBARS
1173       specifier_type_create_toolbar ();
1174 #endif
1175
1176       /* Now initialize the structure types and associated symbols.
1177          Other than the first function below, the functions may
1178          make exactly the following function/macro calls:
1179
1180          define_structure_type()
1181          define_structure_type_keyword()
1182
1183          */
1184
1185       structure_type_create ();
1186
1187       structure_type_create_chartab ();
1188       structure_type_create_faces ();
1189       structure_type_create_rangetab ();
1190       structure_type_create_hash_table ();
1191
1192       /* Now initialize the image instantiator formats and associated symbols.
1193          Other than the first function below, the functions may
1194          make exactly the following function/macro calls:
1195
1196          INITIALIZE_IMAGE_INSTANTIATOR_FORMAT()
1197          IIFORMAT_HAS_METHOD()
1198          IIFORMAT_VALID_KEYWORD()
1199
1200          For any given image instantiator format, the first macro must be
1201          called before the any calls to the other macros. */
1202
1203       image_instantiator_format_create ();
1204       image_instantiator_format_create_glyphs_eimage ();
1205       image_instantiator_format_create_glyphs_widget ();
1206 #ifdef HAVE_TTY
1207       image_instantiator_format_create_glyphs_tty ();
1208 #endif
1209 #ifdef HAVE_X_WINDOWS
1210       image_instantiator_format_create_glyphs_x ();
1211 #endif /* HAVE_X_WINDOWS */
1212 #ifdef HAVE_MS_WINDOWS
1213       image_instantiator_format_create_glyphs_mswindows ();
1214 #endif /* HAVE_MSWINDOWS_WINDOWS */
1215
1216       /* Now initialize the lstream types and associated symbols.
1217          Other than the first function below, the functions may
1218          make exactly the following function/macro calls:
1219
1220          LSTREAM_HAS_METHOD()
1221
1222          */
1223
1224       lstream_type_create ();
1225 #ifdef FILE_CODING
1226       lstream_type_create_file_coding ();
1227 #endif
1228 #if defined (HAVE_MS_WINDOWS) && !defined(HAVE_MSG_SELECT)
1229       lstream_type_create_mswindows_selectable ();
1230 #endif
1231
1232       /* Initialize processes implementation.
1233          The functions may make exactly the following function/macro calls:
1234
1235          PROCESS_HAS_METHOD()
1236       */
1237 #ifdef HAVE_UNIX_PROCESSES
1238       process_type_create_unix ();
1239 #endif
1240 #ifdef HAVE_WIN32_PROCESSES
1241       process_type_create_nt ();
1242 #endif
1243
1244       /* Now initialize most variables.
1245
1246          These functions may do exactly the following:
1247
1248          DEFVAR_INT()
1249          DEFVAR_LISP()
1250          DEFVAR_BOOL()
1251          DEFER_GETTEXT()
1252          Dynarr_*()
1253          Blocktype_*()
1254          staticpro()
1255          Fprovide(symbol)
1256          intern()
1257          Fput()
1258          xmalloc()
1259          defsymbol(), if it's absolutely necessary and you're sure that
1260            the symbol isn't referenced anywhere else in the initialization
1261            code
1262          Fset() on a symbol that is unbound
1263          assigning a symbol or constant value to a variable
1264          using a global variable that has been initialized
1265            earlier on in the same function
1266
1267          Any of the object-creating functions on alloc.c: e.g.
1268
1269          make_pure_*()
1270          make_string()
1271          build_string()
1272          make_vector()
1273          make_int()
1274          make_extent()
1275          alloc_lcrecord()
1276          Fcons()
1277          listN()
1278          make_opaque_ptr()
1279
1280          perhaps a few others.
1281        */
1282
1283       /* Now allow Fprovide() statements to be made. */
1284       init_provide_once ();
1285
1286       /* Do that before any specifier creation (esp. vars_of_glyphs()) */
1287       vars_of_specifier ();
1288
1289       vars_of_abbrev ();
1290       vars_of_alloc ();
1291       vars_of_buffer ();
1292       vars_of_bytecode ();
1293       vars_of_callint ();
1294       vars_of_callproc ();
1295       vars_of_chartab ();
1296       vars_of_cmdloop ();
1297       vars_of_cmds ();
1298       vars_of_console ();
1299       vars_of_data ();
1300 #ifdef DEBUG_XEMACS
1301       vars_of_debug ();
1302       vars_of_tests ();
1303 #endif
1304       vars_of_console_stream ();
1305       vars_of_device ();
1306 #ifdef HAVE_DIALOGS
1307       vars_of_dialog ();
1308 #endif
1309       vars_of_dired ();
1310       vars_of_doc ();
1311 #ifdef HAVE_DRAGNDROP
1312       vars_of_dragdrop ();
1313 #endif
1314       vars_of_editfns ();
1315       vars_of_elhash ();
1316       vars_of_emacs ();
1317       vars_of_eval ();
1318
1319 #ifdef HAVE_X_WINDOWS
1320       vars_of_event_Xt ();
1321 #endif
1322 #if defined(HAVE_TTY) && (defined (DEBUG_TTY_EVENT_STREAM) || !defined (HAVE_X_WINDOWS))
1323       vars_of_event_tty ();
1324 #endif
1325 #ifdef HAVE_MS_WINDOWS
1326       vars_of_event_mswindows ();
1327 #endif
1328       vars_of_event_stream ();
1329
1330       vars_of_events ();
1331       vars_of_extents ();
1332       vars_of_faces ();
1333       vars_of_fileio ();
1334       vars_of_floatfns ();
1335       vars_of_font_lock ();
1336       vars_of_frame ();
1337       vars_of_glyphs ();
1338       vars_of_glyphs_eimage ();
1339       vars_of_glyphs_widget ();
1340 #if defined (HAVE_MENUBARS) || defined (HAVE_SCROLLBARS) || defined (HAVE_DIALOGS) || defined (HAVE_TOOLBARS)
1341       vars_of_gui ();
1342 #endif
1343       vars_of_gutter ();
1344       vars_of_indent ();
1345       vars_of_insdel ();
1346       vars_of_intl ();
1347 #ifdef HAVE_XIM
1348 #ifdef XIM_MOTIF
1349       vars_of_input_method_motif ();
1350 #else /* XIM_XLIB */
1351       vars_of_input_method_xlib ();
1352 #endif
1353 #endif /* HAVE_XIM */
1354       vars_of_keymap ();
1355       vars_of_lread ();
1356       vars_of_lstream ();
1357       vars_of_macros ();
1358       vars_of_md5 ();
1359 #ifdef HAVE_DATABASE
1360       vars_of_database ();
1361 #endif
1362 #ifdef HAVE_MENUBARS
1363       vars_of_menubar ();
1364 #endif
1365       vars_of_minibuf ();
1366 #ifdef HAVE_SHLIB
1367       vars_of_module ();
1368 #endif
1369 #ifdef WINDOWSNT
1370       vars_of_nt ();
1371       vars_of_ntproc ();
1372 #endif
1373       vars_of_objects ();
1374       vars_of_print ();
1375
1376 #ifndef NO_SUBPROCESSES
1377       vars_of_process ();
1378 #ifdef HAVE_UNIX_PROCESSES
1379       vars_of_process_unix ();
1380 #endif
1381 #ifdef HAVE_WIN32_PROCESSES
1382       vars_of_process_nt ();
1383 #endif
1384 #endif
1385
1386       vars_of_profile ();
1387 #if defined (HAVE_MMAP) && defined (REL_ALLOC) && !defined(DOUG_LEA_MALLOC)
1388       vars_of_ralloc ();
1389 #endif /* HAVE_MMAP && REL_ALLOC */
1390       vars_of_redisplay ();
1391 #ifdef HAVE_SCROLLBARS
1392       vars_of_scrollbar ();
1393 #endif
1394       vars_of_search ();
1395       vars_of_select ();
1396       vars_of_sound ();
1397       vars_of_symbols ();
1398       vars_of_syntax ();
1399 #ifdef HAVE_TOOLBARS
1400       vars_of_toolbar ();
1401 #endif
1402       vars_of_undo ();
1403       vars_of_window ();
1404
1405 #ifdef HAVE_TTY
1406       vars_of_console_tty ();
1407       vars_of_frame_tty ();
1408       vars_of_objects_tty ();
1409 #endif
1410
1411 #ifdef HAVE_X_WINDOWS
1412       vars_of_balloon_x ();
1413       vars_of_device_x ();
1414 #ifdef HAVE_DIALOGS
1415       vars_of_dialog_x ();
1416 #endif
1417       vars_of_frame_x ();
1418       vars_of_glyphs_x ();
1419 #ifdef HAVE_MENUBARS
1420       vars_of_menubar_x ();
1421 #endif
1422       vars_of_objects_x ();
1423       vars_of_select_x ();
1424 #ifdef HAVE_SCROLLBARS
1425       vars_of_scrollbar_x ();
1426 #endif
1427 #if defined (HAVE_MENUBARS) || defined (HAVE_SCROLLBARS) || defined (HAVE_DIALOGS) || defined (HAVE_TOOLBARS)
1428       vars_of_gui_x ();
1429 #endif
1430 #endif /* HAVE_X_WINDOWS */
1431
1432 #ifdef HAVE_MS_WINDOWS
1433       vars_of_device_mswindows ();
1434       vars_of_console_mswindows ();
1435       vars_of_frame_mswindows ();
1436       vars_of_objects_mswindows ();
1437       vars_of_select_mswindows ();
1438       vars_of_glyphs_mswindows ();
1439 #ifdef HAVE_SCROLLBARS
1440       vars_of_scrollbar_mswindows ();
1441 #endif
1442 #ifdef HAVE_MENUBARS
1443       vars_of_menubar_mswindows ();
1444 #endif
1445 #ifdef HAVE_MSW_C_DIRED
1446       vars_of_dired_mswindows ();
1447 #endif
1448 #ifdef HAVE_DIALOGS
1449       vars_of_dialog_mswindows ();
1450 #endif
1451 #endif  /* HAVE_MS_WINDOWS */
1452
1453 #ifdef MULE
1454       vars_of_mule ();
1455       vars_of_mule_ccl ();
1456       vars_of_mule_charset ();
1457 #endif
1458 #ifdef FILE_CODING
1459       vars_of_file_coding ();
1460 #endif
1461 #ifdef MULE
1462 #ifdef HAVE_WNN
1463       vars_of_mule_wnn ();
1464 #endif
1465 #ifdef HAVE_CANNA
1466       vars_of_mule_canna ();
1467 #endif /* HAVE_CANNA */
1468 #endif /* MULE */
1469
1470 #ifdef TOOLTALK
1471       vars_of_tooltalk ();
1472 #endif
1473
1474 #ifdef SUNPRO
1475       vars_of_sunpro ();
1476 #endif
1477
1478 #ifdef HAVE_LDAP
1479       vars_of_eldap ();
1480 #endif
1481
1482 #ifdef HAVE_GPM
1483           vars_of_gpmevent ();
1484 #endif
1485
1486       /* Now initialize any specifier variables.  We do this later
1487          because it has some dependence on the vars initialized
1488          above.
1489
1490          These functions should *only* initialize specifier variables,
1491          and may make use of the following functions/macros in addition
1492          to the ones listed above:
1493
1494          DEFVAR_SPECIFIER()
1495          Fmake_specifier()
1496          set_specifier_fallback()
1497          set_specifier_caching()
1498          */
1499
1500       specifier_vars_of_glyphs ();
1501       specifier_vars_of_gutter ();
1502 #ifdef HAVE_MENUBARS
1503       specifier_vars_of_menubar ();
1504 #endif
1505       specifier_vars_of_redisplay ();
1506 #ifdef HAVE_SCROLLBARS
1507       specifier_vars_of_scrollbar ();
1508 #endif
1509 #ifdef HAVE_TOOLBARS
1510       specifier_vars_of_toolbar ();
1511 #endif
1512       specifier_vars_of_window ();
1513
1514       /* Now comes all the rest of the variables that couldn't
1515          be handled above.  There may be dependencies on variables
1516          initialized above, and dependencies between one complex_vars_()
1517          function and another. */
1518
1519       /* Calls Fmake_range_table(). */
1520       complex_vars_of_regex ();
1521       /* Calls Fmake_range_table(). */
1522       complex_vars_of_search ();
1523
1524       /* Calls make_lisp_hash_table(). */
1525       complex_vars_of_extents ();
1526
1527       /* Depends on hash tables and specifiers. */
1528       complex_vars_of_faces ();
1529
1530 #ifdef MULE
1531       /* These two depend on hash tables and various variables declared
1532          earlier.  The second may also depend on the first. */
1533       complex_vars_of_mule_charset ();
1534 #endif
1535 #ifdef FILE_CODING
1536       complex_vars_of_file_coding ();
1537 #endif
1538
1539       /* This calls allocate_glyph(), which creates specifiers
1540          and also relies on a variable (Vthe_nothing_vector) initialized
1541          above.  It also calls make_ext_string(), which under Mule
1542          could require that the charsets be initialized. */
1543       complex_vars_of_glyphs ();
1544
1545       /* These rely on the glyphs just created in the previous function,
1546          and call Fadd_spec_to_specifier(), which relies on various
1547          variables initialized above. */
1548 #ifdef HAVE_X_WINDOWS
1549       complex_vars_of_glyphs_x ();
1550 #endif
1551 #ifdef HAVE_MS_WINDOWS
1552       complex_vars_of_glyphs_mswindows ();
1553 #endif
1554
1555       /* This calls Fmake_glyph_internal(). */
1556       complex_vars_of_alloc ();
1557
1558       /* This calls Fmake_glyph_internal(). */
1559 #ifdef HAVE_MENUBARS
1560       complex_vars_of_menubar ();
1561 #endif
1562
1563       /* This calls Fmake_glyph_internal(). */
1564 #ifdef HAVE_SCROLLBARS
1565       complex_vars_of_scrollbar ();
1566 #endif
1567
1568       /* This calls allocate_glyph(). */
1569       complex_vars_of_frame ();
1570
1571       /* This calls Fcopy_category_table() under Mule, which calls who
1572          knows what. */
1573       complex_vars_of_chartab ();
1574
1575       /* This calls set_string_char(), which (under Mule) depends on the
1576          charsets being initialized. */
1577       complex_vars_of_casetab ();
1578
1579       /* This calls Fcopy_syntax_table(), which relies on char tables. */
1580       complex_vars_of_syntax ();
1581
1582       /* This initializes buffer-local variables, sets things up so
1583          that buffers can be created, and creates a couple of basic
1584          buffers.  This depends on Vstandard_syntax_table and
1585          Vstandard_category_table (initialized in the previous
1586          functions), as well as a whole horde of variables that may
1587          have been initialized above. */
1588       complex_vars_of_buffer ();
1589
1590       /* This initializes console-local variables. */
1591       complex_vars_of_console ();
1592
1593       /* This creates a couple more buffers, and depends on the
1594          previous function. */
1595       complex_vars_of_minibuf ();
1596
1597       /* These two might call Ffile_name_as_directory(), which
1598          might depend on all sorts of things; I'm not sure. */
1599       complex_vars_of_emacs ();
1600
1601       /* This creates a couple of basic keymaps and depends on Lisp
1602          hash tables and Ffset() (both of which depend on some variables
1603          initialized in the vars_of_*() section) and possibly other
1604          stuff. */
1605       complex_vars_of_keymap ();
1606
1607       /* Calls make_lisp_hash_table() and creates a keymap */
1608       complex_vars_of_event_stream ();
1609
1610 #ifdef ERROR_CHECK_GC
1611       {
1612         extern int always_gc;
1613         if (always_gc)                /* purification debugging hack */
1614           garbage_collect_1 ();
1615       }
1616 #endif
1617 #ifdef PDUMP
1618     } else if (!restart) {
1619       reinit_alloc_once_early ();
1620       reinit_symbols_once_early ();
1621       reinit_opaque_once_early ();
1622
1623       reinit_console_type_create_stream ();
1624 #ifdef HAVE_TTY
1625       reinit_console_type_create_tty ();
1626 #endif
1627 #ifdef HAVE_X_WINDOWS
1628       reinit_console_type_create_x ();
1629       reinit_console_type_create_device_x ();
1630 #endif
1631 #ifdef HAVE_MS_WINDOWS
1632       reinit_console_type_create_mswindows ();
1633 #endif
1634
1635       reinit_specifier_type_create ();
1636       reinit_specifier_type_create_image ();
1637       reinit_specifier_type_create_gutter ();
1638       reinit_specifier_type_create_objects ();
1639 #ifdef HAVE_TOOLBARS
1640       reinit_specifier_type_create_toolbar ();
1641 #endif
1642
1643       structure_type_create ();
1644
1645       structure_type_create_chartab ();
1646       structure_type_create_faces ();
1647       structure_type_create_rangetab ();
1648       structure_type_create_hash_table ();
1649
1650       lstream_type_create ();
1651 #ifdef FILE_CODING
1652       lstream_type_create_file_coding ();
1653 #endif
1654 #if defined (HAVE_MS_WINDOWS) && !defined(HAVE_MSG_SELECT)
1655       lstream_type_create_mswindows_selectable ();
1656 #endif
1657 #ifdef HAVE_UNIX_PROCESSES
1658       process_type_create_unix ();
1659 #endif
1660 #ifdef HAVE_WIN32_PROCESSES
1661       process_type_create_nt ();
1662 #endif
1663
1664       reinit_vars_of_buffer ();
1665       reinit_vars_of_console ();
1666 #ifdef DEBUG_XEMACS
1667       reinit_vars_of_debug ();
1668 #endif
1669       reinit_vars_of_device ();
1670       reinit_vars_of_eval ();
1671 #ifdef HAVE_X_WINDOWS
1672       reinit_vars_of_event_Xt ();
1673 #endif
1674 #if defined(HAVE_TTY) && (defined (DEBUG_TTY_EVENT_STREAM) || !defined (HAVE_X_WINDOWS))
1675       reinit_vars_of_event_tty ();
1676 #endif
1677 #ifdef HAVE_MS_WINDOWS
1678       reinit_vars_of_event_mswindows ();
1679 #endif
1680       reinit_vars_of_event_stream ();
1681       reinit_vars_of_events ();
1682       reinit_vars_of_extents ();
1683       reinit_vars_of_font_lock ();
1684       reinit_vars_of_glyphs ();
1685       reinit_vars_of_glyphs_widget ();
1686       reinit_vars_of_insdel ();
1687       reinit_vars_of_lread ();
1688       reinit_vars_of_lstream ();
1689       reinit_vars_of_minibuf ();
1690 #ifdef HAVE_SHLIB
1691       reinit_vars_of_module ();
1692 #endif
1693       reinit_vars_of_objects ();
1694       reinit_vars_of_print ();
1695       reinit_vars_of_redisplay ();
1696       reinit_vars_of_search ();
1697       reinit_vars_of_undo ();
1698       reinit_vars_of_window ();
1699
1700 #ifdef HAVE_MS_WINDOWS
1701       reinit_vars_of_frame_mswindows ();
1702 #endif
1703
1704 #ifdef HAVE_X_WINDOWS
1705       reinit_vars_of_device_x ();
1706 #ifdef HAVE_SCROLLBARS
1707       reinit_vars_of_scrollbar_x ();
1708 #endif
1709 #ifdef HAVE_MENUBARS
1710       reinit_vars_of_menubar_x ();
1711 #endif
1712       reinit_vars_of_select_x ();
1713 #if defined (HAVE_MENUBARS) || defined (HAVE_SCROLLBARS) || defined (HAVE_DIALOGS) || defined (HAVE_TOOLBARS)
1714       reinit_vars_of_gui_x ();
1715 #endif
1716 #endif /* HAVE_X_WINDOWS */
1717
1718 #if defined(MULE) && defined(HAVE_WNN)
1719       reinit_vars_of_mule_wnn ();
1720 #endif
1721
1722       reinit_complex_vars_of_buffer ();
1723       reinit_complex_vars_of_console ();
1724       reinit_complex_vars_of_minibuf ();
1725 #endif /* PDUMP */
1726     }
1727
1728
1729   /* CONGRATULATIONS!!!  We have successfully initialized the Lisp
1730      engine. */
1731
1732   if (initialized)
1733     {
1734       /* Stuff that needs to be reset at run time.  Order below should
1735          not matter. */
1736       reinit_alloc ();
1737       reinit_eval ();
1738 #ifdef MULE_REGEXP
1739       reinit_mule_category ();
1740 #endif
1741     }
1742
1743   /* Now do further initialization/setup of stuff that is not needed by the
1744      syms_of_() routines.  This involves stuff that only is enabled in
1745      an interactive run (redisplay, user input, etc.) and stuff that is
1746      not needed until we start loading Lisp code (the reader).  A lot
1747      of this stuff involves querying the current environment and needs
1748      to be done both at dump time and at run time. */
1749
1750   init_initial_directory();             /* get the directory to use for the
1751                                            "*scratch*" buffer, etc. */
1752
1753 #ifdef WINDOWSNT
1754   /*
1755    * For Win32, call init_environment() now, so that environment/registry
1756    * variables will be properly entered into Vprocess_environment.
1757    */
1758   init_environment();
1759 #endif
1760
1761   init_callproc ();     /* Set up the process environment (so that egetenv
1762                            works), the basic directory variables
1763                            (exec-directory and so on), and stuff
1764                            related to subprocesses.  This should be
1765                            first because many of the functions below
1766                            call egetenv() to get environment variables. */
1767   init_lread ();        /* Set up the Lisp reader. */
1768 #ifdef MSDOS
1769   /* Call early 'cause init_environment needs it.  */
1770   init_dosfns ();
1771   /* Set defaults for several environment variables.  */
1772   init_environment (argc, argv, skip_args);
1773 #endif
1774   init_cmdargs (argc, argv, skip_args); /* Create list Vcommand_line_args */
1775   init_buffer ();       /* Set default directory of *scratch* buffer */
1776
1777 #ifdef WINDOWSNT
1778   init_ntproc();
1779 #endif
1780
1781   init_redisplay ();      /* Determine terminal type.
1782                              init_sys_modes uses results */
1783   init_frame ();
1784   init_event_stream (); /* Set up so we can get user input. */
1785   init_macros (); /* set up so we can run macros. */
1786   init_editfns (); /* Determine the name of the user we're running as */
1787   init_xemacs_process (); /* set up for calling subprocesses */
1788 #ifdef SUNPRO
1789   init_sunpro (); /* Set up Sunpro usage tracking */
1790 #endif
1791 #if defined (HAVE_NATIVE_SOUND) && defined (hp9000s800)
1792   init_hpplay ();
1793 #endif
1794 #ifdef HAVE_TTY
1795   init_device_tty ();
1796 #endif
1797   init_console_stream (); /* Create the first console */
1798
1799   /* try to get the actual pathname of the exec file we are running */
1800   if (!restart)
1801   {
1802     Vinvocation_name = Fcar (Vcommand_line_args);
1803     if (XSTRING_DATA(Vinvocation_name)[0] == '-')
1804       {
1805         /* XEmacs as a login shell, oh goody! */
1806         Vinvocation_name = build_string(getenv("SHELL"));
1807       }
1808     Vinvocation_directory = Vinvocation_name;
1809
1810     if (!NILP (Ffile_name_directory (Vinvocation_name)))
1811       {
1812         /* invocation-name includes a directory component -- presumably it
1813            is relative to cwd, not $PATH */
1814         Vinvocation_directory = Fexpand_file_name (Vinvocation_name,
1815                                                    Qnil);
1816         Vinvocation_path = Qnil;
1817       }
1818     else
1819       {
1820         Vinvocation_path = decode_env_path ("PATH", NULL);
1821         locate_file (Vinvocation_path, Vinvocation_name,
1822                      Vlisp_EXEC_SUFFIXES,
1823                      &Vinvocation_directory, X_OK);
1824       }
1825
1826     if (NILP (Vinvocation_directory))
1827       Vinvocation_directory = Vinvocation_name;
1828
1829     Vinvocation_name = Ffile_name_nondirectory (Vinvocation_directory);
1830     Vinvocation_directory = Ffile_name_directory (Vinvocation_directory);
1831   }
1832
1833 #if defined(HAVE_SHLIB) && !defined(WINDOWSNT)
1834   /* This is Unix only.  MS Windows NT has a library call that does
1835      The Right Thing on that system.  Rumor has it, this must be
1836      called for GNU dld in temacs and xemacs.  */
1837   {
1838     char *buf = (char *)alloca (XSTRING_LENGTH (Vinvocation_directory)
1839                                 + XSTRING_LENGTH (Vinvocation_name)
1840                                 + 2);
1841     sprintf (buf, "%s/%s", XSTRING_DATA (Vinvocation_directory),
1842              XSTRING_DATA (Vinvocation_name));
1843
1844     /* All we can do is cry if an error happens, so ignore it. */
1845     (void) dll_init (buf);
1846   }
1847 #endif
1848
1849 #if defined (LOCALTIME_CACHE) && defined (HAVE_TZSET)
1850   /* sun's localtime() has a bug.  it caches the value of the time
1851      zone rather than looking it up every time.  Since localtime() is
1852      called to bolt the undumping time into the undumped emacs, this
1853      results in localtime() ignoring the TZ environment variable.
1854      This flushes the new TZ value into localtime(). */
1855   tzset ();
1856 #endif /* LOCALTIME_CACHE and TZSET */
1857
1858   load_me = Qnil;
1859   if (!initialized)
1860     {
1861       /* Handle -l loadup-and-dump, args passed by Makefile. */
1862       if (argc > 2 + skip_args && !strcmp (argv[1 + skip_args], "-l"))
1863         load_me = build_string (argv[2 + skip_args]);
1864 #if 0 /* CANNOT_DUMP - this can never be right in XEmacs --andyp */
1865       /* Unless next switch is -nl, load "loadup.el" first thing.  */
1866       if (!(argc > 1 + skip_args && !strcmp (argv[1 + skip_args], "-nl")))
1867         load_me = build_string ("loadup.el");
1868 #endif /* CANNOT_DUMP */
1869     }
1870
1871 #ifdef QUANTIFY
1872   if (initialized)
1873     quantify_start_recording_data ();
1874 #endif /* QUANTIFY */
1875
1876   initialized = 1;
1877
1878   /* This never returns.  */
1879   initial_command_loop (load_me);
1880   /* NOTREACHED */
1881 }
1882
1883 \f
1884 /* Sort the args so we can find the most important ones
1885    at the beginning of argv.  */
1886
1887 /* First, here's a table of all the standard options.  */
1888
1889 struct standard_args
1890 {
1891   const char *name;
1892   const char *longname;
1893   int priority;
1894   int nargs;
1895 };
1896
1897 static const struct standard_args standard_args[] =
1898 {
1899   /* Handled by main_1 above: */
1900   { "-nl", "--no-shared-memory", 100, 0 },
1901   { "-t", "--terminal", 95, 1 },
1902   { "-nw", "--no-windows", 90, 0 },
1903   { "-batch", "--batch", 85, 0 },
1904   { "-debug-paths", "--debug-paths", 82, 0 },
1905   { "-help", "--help", 80, 0 },
1906   { "-version", "--version", 75, 0 },
1907   { "-V", 0, 75, 0 },
1908   { "-d", "--display", 80, 1 },
1909   { "-display", 0, 80, 1 },
1910   { "-NXHost",  0, 79, 0 },
1911   { "-MachLaunch", 0, 79, 0},
1912
1913   /* Handled by command-line-early in startup.el: */
1914   { "-q", "--no-init-file", 50, 0 },
1915   { "-unmapped", 0, 50, 0 },
1916   { "-no-init-file", 0, 50, 0 },
1917   { "-vanilla", "--vanilla", 50, 0 },
1918   { "-no-autoloads", "--no-autoloads", 50, 0 },
1919   { "-no-site-file", "--no-site-file", 40, 0 },
1920   { "-no-early-packages", "--no-early-packages", 35, 0 },
1921   { "-u", "--user", 30, 1 },
1922   { "-user", 0, 30, 1 },
1923   { "-debug-init", "--debug-init", 20, 0 },
1924   { "-debug-paths", "--debug-paths", 20, 0 },
1925
1926   /* Xt options: */
1927   { "-i", "--icon-type", 15, 0 },
1928   { "-itype", 0, 15, 0 },
1929   { "-iconic", "--iconic", 15, 0 },
1930   { "-bg", "--background-color", 10, 1 },
1931   { "-background", 0, 10, 1 },
1932   { "-fg", "--foreground-color", 10, 1 },
1933   { "-foreground", 0, 10, 1 },
1934   { "-bd", "--border-color", 10, 1 },
1935   { "-bw", "--border-width", 10, 1 },
1936   { "-ib", "--internal-border", 10, 1 },
1937   { "-ms", "--mouse-color", 10, 1 },
1938   { "-cr", "--cursor-color", 10, 1 },
1939   { "-fn", "--font", 10, 1 },
1940   { "-font", 0, 10, 1 },
1941   { "-g", "--geometry", 10, 1 },
1942   { "-geometry", 0, 10, 1 },
1943   { "-T", "--title", 10, 1 },
1944   { "-title", 0, 10, 1 },
1945   { "-name", "--name", 10, 1 },
1946   { "-xrm", "--xrm", 10, 1 },
1947   { "-r", "--reverse-video", 5, 0 },
1948   { "-rv", 0, 5, 0 },
1949   { "-reverse", 0, 5, 0 },
1950   { "-hb", "--horizontal-scroll-bars", 5, 0 },
1951   { "-vb", "--vertical-scroll-bars", 5, 0 },
1952
1953   /* These have the same priority as ordinary file name args,
1954      so they are not reordered with respect to those.  */
1955   { "-L", "--directory", 0, 1 },
1956   { "-directory", 0, 0, 1 },
1957   { "-l", "--load", 0, 1 },
1958   { "-load", 0, 0, 1 },
1959   { "-f", "--funcall", 0, 1 },
1960   { "-funcall", 0, 0, 1 },
1961   { "-eval", "--eval", 0, 1 },
1962   { "-insert", "--insert", 0, 1 },
1963   /* This should be processed after ordinary file name args and the like.  */
1964   { "-kill", "--kill", -10, 0 },
1965 };
1966
1967 /* Reorder the elements of ARGV (assumed to have ARGC elements)
1968    so that the highest priority ones come first.
1969    Do not change the order of elements of equal priority.
1970    If an option takes an argument, keep it and its argument together.  */
1971
1972 static void
1973 sort_args (int argc, char **argv)
1974 {
1975   char **new_argv = xnew_array (char *, argc);
1976   /* For each element of argv,
1977      the corresponding element of options is:
1978      0 for an option that takes no arguments,
1979      1 for an option that takes one argument, etc.
1980      -1 for an ordinary non-option argument.  */
1981   int *options  = xnew_array (int, argc);
1982   int *priority = xnew_array (int, argc);
1983   int to = 1;
1984   int from;
1985   int i;
1986   int end_of_options_p = 0;
1987
1988   /* Categorize all the options,
1989      and figure out which argv elts are option arguments.  */
1990   for (from = 1; from < argc; from++)
1991     {
1992       options[from] = -1;
1993       priority[from] = 0;
1994       /* Pseudo options "--" and "run-temacs" indicate end of options */
1995       if (!strcmp (argv[from], "--") ||
1996           !strcmp (argv[from], "run-temacs"))
1997         end_of_options_p = 1;
1998       if (!end_of_options_p && argv[from][0] == '-')
1999         {
2000           int match, thislen;
2001           char *equals;
2002
2003           /* Look for a match with a known old-fashioned option.  */
2004           for (i = 0; i < countof (standard_args); i++)
2005             if (!strcmp (argv[from], standard_args[i].name))
2006               {
2007                 options[from]  = standard_args[i].nargs;
2008                 priority[from] = standard_args[i].priority;
2009                 if (from + standard_args[i].nargs >= argc)
2010                   fatal ("Option `%s' requires an argument\n", argv[from]);
2011                 from += standard_args[i].nargs;
2012                 goto done;
2013               }
2014
2015           /* Look for a match with a known long option.
2016              MATCH is -1 if no match so far, -2 if two or more matches so far,
2017              >= 0 (the table index of the match) if just one match so far.  */
2018           if (argv[from][1] == '-')
2019             {
2020               match = -1;
2021               thislen = strlen (argv[from]);
2022               equals = strchr (argv[from], '=');
2023               if (equals != 0)
2024                 thislen = equals - argv[from];
2025
2026               for (i = 0; i < countof (standard_args); i++)
2027                 if (standard_args[i].longname
2028                     && !strncmp (argv[from], standard_args[i].longname,
2029                                  thislen))
2030                   {
2031                     if (match == -1)
2032                       match = i;
2033                     else
2034                       match = -2;
2035                   }
2036
2037               /* If we found exactly one match, use that.  */
2038               if (match >= 0)
2039                 {
2040                   options[from]  = standard_args[match].nargs;
2041                   priority[from] = standard_args[match].priority;
2042                   /* If --OPTION=VALUE syntax is used,
2043                      this option uses just one argv element.  */
2044                   if (equals != 0)
2045                     options[from] = 0;
2046                   if (from + options[from] >= argc)
2047                     fatal ("Option `%s' requires an argument\n", argv[from]);
2048                   from += options[from];
2049                 }
2050             }
2051         done: ;
2052         }
2053     }
2054
2055   /* Copy the arguments, in order of decreasing priority, to NEW_ARGV.  */
2056   new_argv[0] = argv[0];
2057   while (to < argc)
2058     {
2059       int best = -1;
2060       int best_priority = -9999;
2061
2062       /* Find the highest priority remaining option.
2063          If several have equal priority, take the first of them.  */
2064       for (from = 1; from < argc; from++)
2065         {
2066           if (argv[from] != 0 && priority[from] > best_priority)
2067             {
2068               best_priority = priority[from];
2069               best = from;
2070             }
2071           /* Skip option arguments--they are tied to the options.  */
2072           if (options[from] > 0)
2073             from += options[from];
2074         }
2075
2076       if (best < 0)
2077         abort ();
2078
2079       /* Copy the highest priority remaining option, with its args, to NEW_ARGV.  */
2080       new_argv[to++] = argv[best];
2081       for (i = 0; i < options[best]; i++)
2082         new_argv[to++] = argv[best + i + 1];
2083
2084       /* Clear out this option in ARGV.  */
2085       argv[best] = 0;
2086       for (i = 0; i < options[best]; i++)
2087         argv[best + i + 1] = 0;
2088     }
2089
2090   memcpy (argv, new_argv, sizeof (char *) * argc);
2091   xfree (new_argv);
2092   xfree (options);
2093   xfree (priority);
2094 }
2095
2096 static JMP_BUF run_temacs_catch;
2097
2098 static int run_temacs_argc;
2099 static char **run_temacs_argv;
2100 static char *run_temacs_args;
2101 static size_t run_temacs_argv_size;
2102 static size_t run_temacs_args_size;
2103
2104 DEFUN ("running-temacs-p", Frunning_temacs_p, 0, 0, 0, /*
2105 True if running temacs.  This means we are in the dumping stage.
2106 This is false during normal execution of the `xemacs' program, and
2107 becomes false once `run-emacs-from-temacs' is run.
2108 */
2109        ())
2110 {
2111   return run_temacs_argc >= 0 ? Qt : Qnil;
2112 }
2113
2114 DEFUN ("run-emacs-from-temacs", Frun_emacs_from_temacs, 0, MANY, 0, /*
2115 Do not call this.  It will reinitialize your XEmacs.  You'll be sorry.
2116 */
2117 /* If this function is called from startup.el, it will be possible to run
2118    temacs as an editor using 'temacs -batch -l loadup.el run-temacs', instead
2119    of having to dump an emacs and then run that (when debugging emacs itself,
2120    this can be much faster)). [Actually, the speed difference isn't that
2121    much as long as your filesystem is local, and you don't end up with
2122    a dumped version in case you want to rerun it.  This function is most
2123    useful when used as part of the `make all-elc' command. --ben]
2124    This will "restart" emacs with the specified command-line arguments.
2125
2126    Martin thinks this function is most useful when using debugging
2127    tools like Purify or tcov that get confused by XEmacs' dumping.  */
2128      (int nargs, Lisp_Object *args))
2129 {
2130   int ac;
2131   const Extbyte *wampum;
2132   int namesize;
2133   int total_len;
2134   Lisp_Object orig_invoc_name = Fcar (Vcommand_line_args);
2135   const Extbyte **wampum_all = alloca_array (const Extbyte *, nargs);
2136   int *wampum_all_len  = alloca_array (int, nargs);
2137
2138   assert (!gc_in_progress);
2139
2140   if (run_temacs_argc < 0)
2141     error ("I've lost my temacs-hood.");
2142
2143   /* Need to convert the orig_invoc_name and all of the arguments
2144      to external format. */
2145
2146   TO_EXTERNAL_FORMAT (LISP_STRING, orig_invoc_name,
2147                       ALLOCA, (wampum, namesize),
2148                       Qnative);
2149   namesize++;
2150
2151   for (ac = 0, total_len = namesize; ac < nargs; ac++)
2152     {
2153       CHECK_STRING (args[ac]);
2154       TO_EXTERNAL_FORMAT (LISP_STRING, args[ac],
2155                           ALLOCA, (wampum_all[ac], wampum_all_len[ac]),
2156                           Qnative);
2157       wampum_all_len[ac]++;
2158       total_len += wampum_all_len[ac];
2159     }
2160   DO_REALLOC (run_temacs_args, run_temacs_args_size, total_len, char);
2161   DO_REALLOC (run_temacs_argv, run_temacs_argv_size, nargs+2, char *);
2162
2163   memcpy (run_temacs_args, wampum, namesize);
2164   run_temacs_argv [0] = run_temacs_args;
2165   for (ac = 0; ac < nargs; ac++)
2166     {
2167       memcpy (run_temacs_args + namesize,
2168               wampum_all[ac], wampum_all_len[ac]);
2169       run_temacs_argv [ac + 1] = run_temacs_args + namesize;
2170       namesize += wampum_all_len[ac];
2171     }
2172   run_temacs_argv [nargs + 1] = 0;
2173   catchlist = NULL; /* Important!  Otherwise free_cons() calls in
2174                        condition_case_unwind() may lead to GC death. */
2175   unbind_to (0, Qnil); /* this closes loadup.el */
2176   purify_flag = 0;
2177   run_temacs_argc = nargs + 1;
2178 #ifdef HEAP_IN_DATA
2179   report_sheap_usage (0);
2180 #endif
2181   LONGJMP (run_temacs_catch, 1);
2182   return Qnil; /* not reached; warning suppression */
2183 }
2184
2185 /* ARGSUSED */
2186 int
2187 main (int argc, char **argv, char **envp)
2188 {
2189   int     volatile vol_argc = argc;
2190   char ** volatile vol_argv = argv;
2191   char ** volatile vol_envp = envp;
2192   /* This is hairy.  We need to compute where the XEmacs binary was invoked
2193      from because temacs initialization requires it to find the lisp
2194      directories.  The code that recomputes the path is guarded by the
2195      restarted flag.  There are three possible paths I've found so far
2196      through this:
2197
2198      temacs -- When running temacs for basic build stuff, the first main_1
2199       will be the only one invoked.  It must compute the path else there
2200       will be a very ugly bomb in startup.el (can't find obvious location
2201       for doc-directory data-directory, etc.).
2202
2203      temacs w/ run-temacs on the command line -- This is run to bytecompile
2204       all the out of date dumped lisp.  It will execute both of the main_1
2205       calls and the second one must not touch the first computation because
2206       argc/argv are hosed the second time through.
2207
2208      xemacs -- Only the second main_1 is executed.  The invocation path must
2209       computed but this only matters when running in place or when running
2210       as a login shell.
2211
2212      As a bonus for straightening this out, XEmacs can now be run in place
2213      as a login shell.  This never used to work.
2214
2215      As another bonus, we can now guarantee that
2216      (concat invocation-directory invocation-name) contains the filename
2217      of the XEmacs binary we are running.  This can now be used in a
2218      definite test for out of date dumped files.  -slb */
2219   int restarted = 0;
2220 #ifdef QUANTIFY
2221   quantify_stop_recording_data ();
2222   quantify_clear_data ();
2223 #endif /* QUANTIFY */
2224
2225   suppress_early_error_handler_backtrace = 0;
2226   lim_data = 0; /* force reinitialization of this variable */
2227
2228   /* Lisp_Object must fit in a word; check VALBITS and GCTYPEBITS */
2229   assert (sizeof (Lisp_Object) == sizeof (void *));
2230
2231 #ifdef LINUX_SBRK_BUG
2232   sbrk (1);
2233 #endif
2234
2235   if (!initialized)
2236     {
2237 #ifdef DOUG_LEA_MALLOC
2238       mallopt (M_MMAP_MAX, 0);
2239 #endif
2240       run_temacs_argc = 0;
2241       if (! SETJMP (run_temacs_catch))
2242         {
2243           main_1 (vol_argc, vol_argv, vol_envp, 0);
2244         }
2245       /* run-emacs-from-temacs called */
2246       restarted = 1;
2247       vol_argc = run_temacs_argc;
2248       vol_argv = run_temacs_argv;
2249 #ifdef _SCO_DS
2250       /* This makes absolutely no sense to anyone involved.  There are
2251          several people using this stuff.  We've compared versions on
2252          everything we can think of.  We can find no difference.
2253          However, on both my systems environ is a plain old global
2254          variable initialized to zero.  _environ is the one that
2255          contains pointers to the actual environment.
2256
2257          Since we can't figure out the difference (and we're hours
2258          away from a release), this takes a very cowardly approach and
2259          is bracketed with both a system specific preprocessor test
2260          and a runtime "do you have this problem" test
2261
2262          06/20/96 robertl@dgii.com */
2263       {
2264         extern char *_environ;
2265         if ((unsigned) environ == 0)
2266           environ=_environ;
2267       }
2268 #endif /* _SCO_DS */
2269       vol_envp = environ;
2270     }
2271 #ifdef RUN_TIME_REMAP
2272   else
2273     /* obviously no-one uses this because where it was before initialized was
2274      *always* true */
2275     run_time_remap (argv[0]);
2276 #endif
2277
2278 #ifdef DOUG_LEA_MALLOC
2279   if (initialized && (malloc_state_ptr != NULL))
2280     {
2281       int rc = malloc_set_state (malloc_state_ptr);
2282       if (rc != 0)
2283         {
2284           fprintf (stderr, "malloc_set_state failed, rc = %d\n", rc);
2285           abort ();
2286         }
2287 #if 0
2288       free (malloc_state_ptr);
2289 #endif
2290       /* mmap works in glibc-2.1, glibc-2.0 (Non-Mule only) and Linux libc5 */
2291 #if (defined(__GLIBC__) && __GLIBC_MINOR__ >= 1) || \
2292     defined(_NO_MALLOC_WARNING_) || \
2293     (defined(__GLIBC__) && __GLIBC_MINOR__ < 1 && !defined(MULE)) || \
2294     defined(DEBUG_DOUG_LEA_MALLOC)
2295       mallopt (M_MMAP_MAX, 64);
2296 #endif
2297 #ifdef REL_ALLOC
2298       r_alloc_reinit ();
2299 #endif
2300     }
2301 #endif /* DOUG_LEA_MALLOC */
2302
2303   run_temacs_argc = -1;
2304
2305   main_1 (vol_argc, vol_argv, vol_envp, restarted);
2306   return 0; /* unreached */
2307 }
2308
2309 \f
2310 /* Dumping apparently isn't supported by versions of GCC >= 2.8. */
2311 /* The following needs conditionalization on whether either XEmacs or */
2312 /* various system shared libraries have been built and linked with */
2313 /* GCC >= 2.8.  -slb */
2314 #if defined(GNU_MALLOC)
2315 static void
2316 voodoo_free_hook (void *mem)
2317 {
2318   /* Disable all calls to free() when XEmacs is exiting and it doesn't */
2319   /* matter. */
2320   __free_hook =
2321 #ifdef __GNUC__ /* prototype of __free_hook varies with glibc version */
2322     (__typeof__ (__free_hook))
2323 #endif
2324     voodoo_free_hook;
2325 }
2326 #endif /* GNU_MALLOC */
2327
2328 DEFUN ("kill-emacs", Fkill_emacs, 0, 1, "P", /*
2329 Exit the XEmacs job and kill it.  Ask for confirmation, without argument.
2330 If ARG is an integer, return ARG as the exit program code.
2331 If ARG is a string, stuff it as keyboard input.
2332
2333 The value of `kill-emacs-hook', if not void,
2334 is a list of functions (of no args),
2335 all of which are called before XEmacs is actually killed.
2336 */
2337        (arg))
2338 {
2339   /* This function can GC */
2340   struct gcpro gcpro1;
2341
2342   GCPRO1 (arg);
2343
2344   if (feof (stdin))
2345     arg = Qt;
2346
2347   if (!preparing_for_armageddon && !noninteractive)
2348     run_hook (Qkill_emacs_hook);
2349
2350   /* make sure no quitting from now on!! */
2351   dont_check_for_quit = 1;
2352   Vinhibit_quit = Qt;
2353
2354   if (!preparing_for_armageddon)
2355     {
2356       Lisp_Object concons, nextcons;
2357
2358       /* Normally, go ahead and delete all the consoles now.
2359          Some unmentionably lame window systems (MS Wwwww...... eek,
2360          I can't even say it) don't properly clean up after themselves,
2361          and even for those that do, it might be cleaner this way.
2362          If we're going down, however, we don't do this (might
2363          be too dangerous), and if we get a crash somewhere within
2364          this loop, we'll still autosave and won't try this again. */
2365
2366       LIST_LOOP_DELETING(concons, nextcons, Vconsole_list)
2367         {
2368           /* There is very little point in deleting the stream console.
2369              It uses stdio, which should flush any buffered output and
2370              something can only go wrong. -slb */
2371           /* I changed my mind.  There's a stupid hack in close to add
2372              a trailing newline. */
2373           /*if (!CONSOLE_STREAM_P (XCONSOLE (XCAR (concons))))*/
2374             delete_console_internal (XCONSOLE (XCAR (concons)), 1, 1, 0);
2375         }
2376     }
2377
2378   UNGCPRO;
2379
2380   shut_down_emacs (0, STRINGP (arg) ? arg : Qnil);
2381
2382 #if defined(GNU_MALLOC)
2383   __free_hook =
2384 #ifdef __GNUC__ /* prototype of __free_hook varies with glibc version */
2385     (__typeof__ (__free_hook))
2386 #endif
2387     voodoo_free_hook;
2388 #endif
2389
2390   exit (INTP (arg) ? XINT (arg) : 0);
2391   /* NOTREACHED */
2392   return Qnil; /* I'm sick of the compiler warning */
2393 }
2394
2395 /* Perform an orderly shutdown of XEmacs.  Autosave any modified
2396    buffers, kill any child processes, clean up the terminal modes (if
2397    we're in the foreground), and other stuff like that.  Don't perform
2398    any redisplay; this may be called when XEmacs is shutting down in
2399    the background, or after its X connection has died.
2400
2401    If SIG is a signal number, print a message for it.
2402
2403    This is called by fatal signal handlers, X protocol error handlers,
2404    and Fkill_emacs.  */
2405 static void
2406 shut_down_emacs (int sig, Lisp_Object stuff)
2407 {
2408   /* This function can GC */
2409   /* Prevent running of hooks and other non-essential stuff
2410      from now on.  */
2411   preparing_for_armageddon = 1;
2412
2413   /* In case frames or windows are screwed up, avoid assertion
2414      failures here */
2415   Vinhibit_quit = Qt;
2416
2417 #ifdef QUANTIFY
2418   quantify_stop_recording_data ();
2419 #endif /* QUANTIFY */
2420
2421 #if 0
2422   /* This is absolutely the most important thing to do, so make sure
2423      we do it now, before anything else.  We might have crashed and
2424      be in a weird inconsistent state, and potentially anything could
2425      set off another protection fault and cause us to bail out
2426      immediately. */
2427   /* I'm not removing the code entirely, yet.  We have run up against
2428      a spate of problems in diagnosing crashes due to crashes within
2429      crashes.  It has very definitely been determined that code called
2430      during auto-saving cannot work if XEmacs crashed inside of GC.
2431      We already auto-save on an itimer so there cannot be too much
2432      unsaved stuff around, and if we get better crash reports we might
2433      be able to get more problems fixed so I'm disabling this.  -slb */
2434   Fdo_auto_save (Qt, Qnil); /* do this before anything hazardous */
2435 #endif
2436
2437   fflush (stdout);
2438   reset_all_consoles ();
2439   if (sig && sig != SIGTERM)
2440     {
2441       stderr_out ("\nFatal error (%d).\n", sig);
2442       stderr_out
2443         ("Your files have been auto-saved.\n"
2444          "Use `M-x recover-session' to recover them.\n"
2445          "\n"
2446          "If you have access to the PROBLEMS file that came with your\n"
2447          "version of XEmacs, please check to see if your crash is described\n"
2448          "there, as there may be a workaround available.\n"
2449 #ifdef INFODOCK
2450          "Otherwise, please report this bug by selecting `Report-Bug'\n"
2451          "in the InfoDock menu.\n"
2452 #else
2453          "Otherwise, please report this bug by running the send-pr\n"
2454          "script included with XEmacs, or selecting `Send Bug Report'\n"
2455          "from the help menu.\n"
2456          "As a last resort send ordinary email to `crashes@xemacs.org'.\n"
2457 #endif
2458          "*MAKE SURE* to include the information in the command\n"
2459          "M-x describe-installation.\n"
2460          "\n"
2461          "If at all possible, *please* try to obtain a C stack backtrace;\n"
2462          "it will help us immensely in determining what went wrong.\n"
2463          "To do this, locate the core file that was produced as a result\n"
2464          "of this crash (it's usually called `core' and is located in the\n"
2465          "directory in which you started the editor, or maybe in your home\n"
2466          "directory), and type\n"
2467          "\n"
2468          "  gdb ");
2469       {
2470         const char *name;
2471         char *dir = 0;
2472
2473         /* Now try to determine the actual path to the executable,
2474            to try to make the backtrace-determination process as foolproof
2475            as possible. */
2476         if (STRINGP (Vinvocation_name))
2477           name = (char *) XSTRING_DATA (Vinvocation_name);
2478         else
2479           name = "xemacs";
2480         if (STRINGP (Vinvocation_directory))
2481           dir = (char *) XSTRING_DATA (Vinvocation_directory);
2482         if (!dir || dir[0] != '/')
2483           stderr_out ("`which %s`", name);
2484         else if (dir[strlen (dir) - 1] != '/')
2485           stderr_out ("%s/%s", dir, name);
2486         else
2487           stderr_out ("%s%s", dir, name);
2488       }
2489       stderr_out
2490         (" core\n\n"
2491          "then type `where' when the debugger prompt comes up.\n"
2492          "(If you don't have GDB on your system, you might have DBX,\n"
2493          "or XDB, or SDB.  A similar procedure should work for all of\n"
2494          "these.  Ask your system administrator if you need more help.)\n");
2495     }
2496
2497   stuff_buffered_input (stuff);
2498
2499   kill_buffer_processes (Qnil);
2500
2501 #ifdef CLASH_DETECTION
2502   unlock_all_files ();
2503 #endif
2504
2505 #ifdef TOOLTALK
2506   tt_session_quit (tt_default_session ());
2507 #if 0
2508   /* The following crashes when built on X11R5 and run on X11R6 */
2509   tt_close ();
2510 #endif
2511 #endif /* TOOLTALK */
2512
2513 }
2514
2515 \f
2516 #ifndef CANNOT_DUMP
2517
2518 #if !defined(PDUMP) || !defined(SYSTEM_MALLOC)
2519 extern char my_edata[];
2520 #endif
2521
2522 extern void disable_free_hook (void);
2523
2524 DEFUN ("dump-emacs", Fdump_emacs, 2, 2, 0, /*
2525 Dump current state of XEmacs into executable file FILENAME.
2526 Take symbols from SYMFILE (presumably the file you executed to run XEmacs).
2527 This is used in the file `loadup.el' when building XEmacs.
2528
2529 Remember to set `command-line-processed' to nil before dumping
2530 if you want the dumped XEmacs to process its command line
2531 and announce itself normally when it is run.
2532 */
2533        (intoname, symname))
2534 {
2535   /* This function can GC */
2536   struct gcpro gcpro1, gcpro2;
2537   int opurify;
2538
2539   GCPRO2 (intoname, symname);
2540
2541 #ifdef FREE_CHECKING
2542   Freally_free (Qnil);
2543
2544   /* When we're dumping, we can't use the debugging free() */
2545   disable_free_hook ();
2546 #endif
2547
2548   CHECK_STRING (intoname);
2549   intoname = Fexpand_file_name (intoname, Qnil);
2550   if (!NILP (symname))
2551     {
2552       CHECK_STRING (symname);
2553       if (XSTRING_LENGTH (symname) > 0)
2554         symname = Fexpand_file_name (symname, Qnil);
2555       else
2556         symname = Qnil;
2557     }
2558
2559   opurify = purify_flag;
2560   purify_flag = 0;
2561
2562 #ifdef HEAP_IN_DATA
2563   report_sheap_usage (1);
2564 #endif
2565
2566   clear_message ();
2567
2568   fflush (stderr);
2569   fflush (stdout);
2570
2571   disksave_object_finalization ();
2572   release_breathing_space ();
2573
2574   /* Tell malloc where start of impure now is */
2575   /* Also arrange for warnings when nearly out of space.  */
2576 #ifndef SYSTEM_MALLOC
2577   memory_warnings (my_edata, malloc_warning);
2578 #endif
2579
2580   UNGCPRO;
2581
2582 #if defined (MSDOS) && defined (EMX)
2583   {
2584     int fd = open ((char *) XSTRING_DATA (intoname),
2585                    O_WRONLY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE);
2586     if (!fd) {
2587       error ("Failure operating on %s", XSTRING_DATA (intoname));
2588     } else {
2589       _core (fd);
2590       close (fd);
2591     }
2592   }
2593 #else /* not MSDOS and EMX */
2594   {
2595     char *intoname_ext;
2596     char *symname_ext;
2597
2598     TO_EXTERNAL_FORMAT (LISP_STRING, intoname,
2599                         C_STRING_ALLOCA, intoname_ext,
2600                         Qfile_name);
2601
2602     if (STRINGP (symname))
2603       TO_EXTERNAL_FORMAT (LISP_STRING, symname,
2604                           C_STRING_ALLOCA, symname_ext,
2605                           Qfile_name);
2606     else
2607       symname_ext = 0;
2608
2609     garbage_collect_1 ();
2610
2611 #ifdef PDUMP
2612     pdump ();
2613 #else
2614
2615 #ifdef DOUG_LEA_MALLOC
2616     malloc_state_ptr = malloc_get_state ();
2617 #endif
2618   /* here we break our rule that the filename conversion should
2619      be performed at the actual time that the system call is made.
2620      It's a whole lot easier to do the conversion here than to
2621      modify all the unexec routines to ensure that filename
2622      conversion is applied everywhere.  Don't worry about memory
2623      leakage because this call only happens once. */
2624     unexec (intoname_ext, symname_ext, (uintptr_t) my_edata, 0, 0);
2625 #ifdef DOUG_LEA_MALLOC
2626     free (malloc_state_ptr);
2627 #endif
2628 #endif /* not PDUMP */
2629   }
2630 #endif /* not MSDOS and EMX */
2631
2632   purify_flag = opurify;
2633
2634   return Qnil;
2635 }
2636
2637 #endif /* not CANNOT_DUMP */
2638 \f
2639 #ifndef SEPCHAR
2640 #define SEPCHAR ':'
2641 #endif
2642
2643 /* Split STRING into a list of substrings.  The substrings are the
2644    parts of original STRING separated by SEPCHAR.  */
2645 static Lisp_Object
2646 split_string_by_emchar_1 (const Bufbyte *string, Bytecount size,
2647                           Emchar sepchar)
2648 {
2649   Lisp_Object result = Qnil;
2650   const Bufbyte *end = string + size;
2651
2652   while (1)
2653     {
2654       const Bufbyte *p = string;
2655       while (p < end)
2656         {
2657           if (charptr_emchar (p) == sepchar)
2658             break;
2659           INC_CHARPTR (p);
2660         }
2661       result = Fcons (make_string (string, p - string), result);
2662       if (p < end)
2663         {
2664           string = p;
2665           INC_CHARPTR (string); /* skip sepchar */
2666         }
2667       else
2668         break;
2669     }
2670   return Fnreverse (result);
2671 }
2672
2673 /* The same as the above, except PATH is an external C string (it is
2674    converted using Qfile_name), and sepchar is hardcoded to SEPCHAR
2675    (':' or whatever).  */
2676 Lisp_Object
2677 decode_path (const char *path)
2678 {
2679   Bytecount newlen;
2680   Bufbyte *newpath;
2681   if (!path)
2682     return Qnil;
2683
2684   TO_INTERNAL_FORMAT (C_STRING, path, ALLOCA, (newpath, newlen), Qfile_name);
2685
2686   /* #### Does this make sense?  It certainly does for
2687      decode_env_path(), but it looks dubious here.  Does any code
2688      depend on decode_path("") returning nil instead of an empty
2689      string?  */
2690   if (!newlen)
2691     return Qnil;
2692
2693   return split_string_by_emchar_1 (newpath, newlen, SEPCHAR);
2694 }
2695
2696 Lisp_Object
2697 decode_env_path (const char *evarname, const char *default_)
2698 {
2699   const char *path = 0;
2700   if (evarname)
2701     path = egetenv (evarname);
2702   if (!path)
2703     path = default_;
2704   return decode_path (path);
2705 }
2706
2707 /* Ben thinks this function should not exist or be exported to Lisp.
2708    We use it to define split-path-string in subr.el (not!).  */
2709
2710 DEFUN ("split-string-by-char", Fsplit_string_by_char, 1, 2, 0, /*
2711 Split STRING into a list of substrings originally separated by SEPCHAR.
2712 */
2713        (string, sepchar))
2714 {
2715   CHECK_STRING (string);
2716   CHECK_CHAR (sepchar);
2717   return split_string_by_emchar_1 (XSTRING_DATA (string),
2718                                    XSTRING_LENGTH (string),
2719                                    XCHAR (sepchar));
2720 }
2721
2722 /* #### This was supposed to be in subr.el, but is used VERY early in
2723    the bootstrap process, so it goes here.  Damn.  */
2724
2725 DEFUN ("split-path", Fsplit_path, 1, 1, 0, /*
2726 Explode a search path into a list of strings.
2727 The path components are separated with the characters specified
2728 with `path-separator'.
2729 */
2730        (path))
2731 {
2732   CHECK_STRING (path);
2733
2734   while (!STRINGP (Vpath_separator)
2735          || (XSTRING_CHAR_LENGTH (Vpath_separator) != 1))
2736     Vpath_separator = signal_simple_continuable_error
2737       ("`path-separator' should be set to a single-character string",
2738        Vpath_separator);
2739
2740   return (split_string_by_emchar_1
2741           (XSTRING_DATA (path), XSTRING_LENGTH (path),
2742            charptr_emchar (XSTRING_DATA (Vpath_separator))));
2743 }
2744 \f
2745 DEFUN ("noninteractive", Fnoninteractive, 0, 0, 0, /*
2746 Non-nil return value means XEmacs is running without interactive terminal.
2747 */
2748        ())
2749 {
2750   return noninteractive ? Qt : Qnil;
2751 }
2752
2753 /* This flag is useful to define if you're under a debugger; this way, you
2754    can put a breakpoint of assert_failed() and debug multiple problems
2755    in one session without having to recompile. */
2756 /* #define ASSERTIONS_DONT_ABORT */
2757
2758 #ifdef USE_ASSERTIONS
2759 /* This highly dubious kludge ... shut up Jamie, I'm tired of your slagging. */
2760
2761 DOESNT_RETURN
2762 assert_failed (const char *file, int line, const char *expr)
2763 {
2764   stderr_out ("Fatal error: assertion failed, file %s, line %d, %s\n",
2765               file, line, expr);
2766 #undef abort    /* avoid infinite #define loop... */
2767 #if defined (WINDOWSNT) && defined (DEBUG_XEMACS)
2768   DebugBreak ();
2769 #elif !defined (ASSERTIONS_DONT_ABORT)
2770   abort ();
2771 #endif
2772 }
2773 #endif /* USE_ASSERTIONS */
2774
2775 #ifdef QUANTIFY
2776 DEFUN ("quantify-start-recording-data", Fquantify_start_recording_data,
2777        0, 0, "", /*
2778 Start recording Quantify data.
2779 */
2780        ())
2781 {
2782   quantify_start_recording_data ();
2783   return Qnil;
2784 }
2785
2786 DEFUN ("quantify-stop-recording-data", Fquantify_stop_recording_data,
2787        0, 0, "", /*
2788 Stop recording Quantify data.
2789 */
2790        ())
2791 {
2792   quantify_stop_recording_data ();
2793   return Qnil;
2794 }
2795
2796 DEFUN ("quantify-clear-data", Fquantify_clear_data, 0, 0, "", /*
2797 Clear all Quantify data.
2798 */
2799        ())
2800 {
2801   quantify_clear_data ();
2802   return Qnil;
2803 }
2804 #endif /* QUANTIFY */
2805
2806 void
2807 syms_of_emacs (void)
2808 {
2809 #ifndef CANNOT_DUMP
2810   DEFSUBR (Fdump_emacs);
2811 #endif /* !CANNOT_DUMP */
2812
2813   DEFSUBR (Frun_emacs_from_temacs);
2814   DEFSUBR (Frunning_temacs_p);
2815   DEFSUBR (Finvocation_name);
2816   DEFSUBR (Finvocation_directory);
2817   DEFSUBR (Fkill_emacs);
2818   DEFSUBR (Fnoninteractive);
2819
2820 #ifdef QUANTIFY
2821   DEFSUBR (Fquantify_start_recording_data);
2822   DEFSUBR (Fquantify_stop_recording_data);
2823   DEFSUBR (Fquantify_clear_data);
2824 #endif /* QUANTIFY */
2825
2826   DEFSUBR (Fsplit_string_by_char);
2827   DEFSUBR (Fsplit_path);        /* #### */
2828
2829   defsymbol (&Qkill_emacs_hook, "kill-emacs-hook");
2830   defsymbol (&Qsave_buffers_kill_emacs, "save-buffers-kill-emacs");
2831 }
2832
2833 void
2834 vars_of_emacs (void)
2835 {
2836   DEFVAR_BOOL ("suppress-early-error-handler-backtrace",
2837                &suppress_early_error_handler_backtrace /*
2838 Non-nil means early error handler shouldn't print a backtrace.
2839 */ );
2840
2841   DEFVAR_LISP ("command-line-args", &Vcommand_line_args /*
2842 Args passed by shell to XEmacs, as a list of strings.
2843 */ );
2844
2845   DEFVAR_LISP ("invocation-name", &Vinvocation_name /*
2846 The program name that was used to run XEmacs.
2847 Any directory names are omitted.
2848 */ );
2849
2850   DEFVAR_LISP ("invocation-directory", &Vinvocation_directory /*
2851 The directory in which the XEmacs executable was found, to run it.
2852 The value is simply the program name if that directory's name is not known.
2853 */ );
2854
2855   DEFVAR_LISP ("invocation-path", &Vinvocation_path /*
2856 The path in which the XEmacs executable was found, to run it.
2857 The value is simply the value of environment variable PATH on startup
2858 if XEmacs was found there.
2859 */ );
2860
2861 #if 0 /* FSFmacs */
2862   xxDEFVAR_LISP ("installation-directory", &Vinstallation_directory,
2863     "A directory within which to look for the `lib-src' and `etc' directories.\n"
2864 "This is non-nil when we can't find those directories in their standard\n"
2865 "installed locations, but we can find them\n"
2866 "near where the XEmacs executable was found.");
2867 #endif
2868
2869   DEFVAR_LISP ("system-type", &Vsystem_type /*
2870 Symbol indicating type of operating system you are using.
2871 */ );
2872   Vsystem_type = intern (SYSTEM_TYPE);
2873   Fprovide (intern(SYSTEM_TYPE));
2874
2875 #ifndef EMACS_CONFIGURATION
2876 # define EMACS_CONFIGURATION "UNKNOWN"
2877 #endif
2878   DEFVAR_LISP ("system-configuration", &Vsystem_configuration /*
2879 String naming the configuration XEmacs was built for.
2880 */ );
2881   Vsystem_configuration = build_string (EMACS_CONFIGURATION);
2882
2883 #ifndef EMACS_CONFIG_OPTIONS
2884 # define EMACS_CONFIG_OPTIONS "UNKNOWN"
2885 #endif
2886   DEFVAR_LISP ("system-configuration-options", &Vsystem_configuration_options /*
2887 String containing the configuration options XEmacs was built with.
2888 */ );
2889   Vsystem_configuration_options = build_string (EMACS_CONFIG_OPTIONS);
2890
2891   DEFVAR_LISP ("emacs-major-version", &Vemacs_major_version /*
2892 Major version number of this version of Emacs, as an integer.
2893 Warning: this variable did not exist in Emacs versions earlier than:
2894   FSF Emacs:   19.23
2895   XEmacs:      19.10
2896 */ );
2897   Vemacs_major_version = make_int (EMACS_MAJOR_VERSION);
2898
2899   DEFVAR_LISP ("emacs-minor-version", &Vemacs_minor_version /*
2900 Minor version number of this version of Emacs, as an integer.
2901 Warning: this variable did not exist in Emacs versions earlier than:
2902   FSF Emacs:   19.23
2903   XEmacs:      19.10
2904 */ );
2905   Vemacs_minor_version = make_int (EMACS_MINOR_VERSION);
2906
2907   DEFVAR_LISP ("emacs-patch-level", &Vemacs_patch_level /*
2908 The patch level of this version of Emacs, as an integer.
2909 The value is non-nil if this version of XEmacs is part of a series of
2910 stable XEmacsen, but has bug fixes applied.
2911 Warning: this variable does not exist in FSF Emacs or in XEmacs versions
2912 earlier than 21.1.1
2913 */ );
2914 #ifdef EMACS_PATCH_LEVEL
2915   Vemacs_patch_level = make_int (EMACS_PATCH_LEVEL);
2916 #else
2917   Vemacs_patch_level = Qnil;
2918 #endif
2919
2920     DEFVAR_LISP ("emacs-beta-version", &Vemacs_beta_version /*
2921 Beta number of this version of Emacs, as an integer.
2922 The value is nil if this is an officially released version of XEmacs.
2923 Warning: this variable does not exist in FSF Emacs or in XEmacs versions
2924 earlier than 20.3.
2925 */ );
2926 #ifdef EMACS_BETA_VERSION
2927   Vemacs_beta_version = make_int (EMACS_BETA_VERSION);
2928 #else
2929   Vemacs_beta_version = Qnil;
2930 #endif
2931
2932 #ifdef INFODOCK
2933   DEFVAR_LISP ("infodock-major-version", &Vinfodock_major_version /*
2934 Major version number of this InfoDock release.
2935 */ );
2936   Vinfodock_major_version = make_int (INFODOCK_MAJOR_VERSION);
2937
2938   DEFVAR_LISP ("infodock-minor-version", &Vinfodock_minor_version /*
2939 Minor version number of this InfoDock release.
2940 */ );
2941   Vinfodock_minor_version = make_int (INFODOCK_MINOR_VERSION);
2942
2943   DEFVAR_LISP ("infodock-build-version", &Vinfodock_build_version /*
2944 Build version of this InfoDock release.
2945 */ );
2946   Vinfodock_build_version = make_int (INFODOCK_BUILD_VERSION);
2947 #endif
2948
2949   DEFVAR_LISP ("xemacs-codename", &Vxemacs_codename /*
2950 Codename of this version of Emacs (a string).
2951 */ );
2952 #ifndef XEMACS_CODENAME
2953 #define XEMACS_CODENAME "Noname"
2954 #endif
2955   Vxemacs_codename = build_string (XEMACS_CODENAME);
2956
2957   DEFVAR_BOOL ("noninteractive", &noninteractive1 /*
2958 Non-nil means XEmacs is running without interactive terminal.
2959 */ );
2960
2961   DEFVAR_BOOL ("inhibit-early-packages", &inhibit_early_packages /*
2962 Set to non-nil when the early packages should not be respected at startup.
2963 */ );
2964
2965   DEFVAR_BOOL ("inhibit-autoloads", &inhibit_autoloads /*
2966 Set to non-nil when autoloads should not be loaded at startup.
2967 */ );
2968
2969   DEFVAR_BOOL ("debug-paths", &debug_paths /*
2970 Set to non-nil when debug information about paths should be printed.
2971 */ );
2972
2973   DEFVAR_BOOL ("inhibit-site-lisp", &inhibit_site_lisp /*
2974 Set to non-nil when the site-lisp should not be searched at startup.
2975 */ );
2976 #ifdef INHIBIT_SITE_LISP
2977   inhibit_site_lisp = 1;
2978 #endif
2979
2980   DEFVAR_BOOL ("inhibit-site-modules", &inhibit_site_modules /*
2981 Set to non-nil when site-modules should not be searched at startup.
2982 */ );
2983 #ifdef INHIBIT_SITE_MODULES
2984   inhibit_site_modules = 1;
2985 #endif
2986
2987   DEFVAR_INT ("emacs-priority", &emacs_priority /*
2988 Priority for XEmacs to run at.
2989 This value is effective only if set before XEmacs is dumped,
2990 and only if the XEmacs executable is installed with setuid to permit
2991 it to change priority.  (XEmacs sets its uid back to the real uid.)
2992 Currently, you need to define SET_EMACS_PRIORITY in `config.h'
2993 before you compile XEmacs, to enable the code for this feature.
2994 */ );
2995   emacs_priority = 0;
2996
2997   DEFVAR_CONST_LISP ("internal-error-checking", &Vinternal_error_checking /*
2998 Internal error checking built-in into this instance of XEmacs.
2999 This is a list of symbols, initialized at build-time.  Legal symbols
3000 are:
3001
3002 extents         - check extents prior to each extent change;
3003 typecheck       - check types strictly, aborting in case of error;
3004 malloc          - check operation of malloc;
3005 gc              - check garbage collection;
3006 bufpos          - check buffer positions.
3007 */ );
3008   Vinternal_error_checking = Qnil;
3009 #ifdef ERROR_CHECK_EXTENTS
3010   Vinternal_error_checking = Fcons (intern ("extents"),
3011                                     Vinternal_error_checking);
3012 #endif
3013 #ifdef ERROR_CHECK_TYPECHECK
3014   Vinternal_error_checking = Fcons (intern ("typecheck"),
3015                                     Vinternal_error_checking);
3016 #endif
3017 #ifdef ERROR_CHECK_MALLOC
3018   Vinternal_error_checking = Fcons (intern ("malloc"),
3019                                     Vinternal_error_checking);
3020 #endif
3021 #ifdef ERROR_CHECK_GC
3022   Vinternal_error_checking = Fcons (intern ("gc"),
3023                                     Vinternal_error_checking);
3024 #endif
3025 #ifdef ERROR_CHECK_BUFPOS
3026   Vinternal_error_checking = Fcons (intern ("bufpos"),
3027                                     Vinternal_error_checking);
3028 #endif
3029
3030   DEFVAR_CONST_LISP ("mail-lock-methods", &Vmail_lock_methods /*
3031 Mail spool locking methods supported by this instance of XEmacs.
3032 This is a list of symbols.  Each of the symbols is one of the
3033 following: dot, lockf, flock, locking, mmdf.
3034 */ );
3035   {
3036     Vmail_lock_methods = Qnil;
3037     Vmail_lock_methods = Fcons (intern ("dot"), Vmail_lock_methods);
3038 #ifdef HAVE_LOCKF
3039     Vmail_lock_methods = Fcons (intern ("lockf"), Vmail_lock_methods);
3040 #endif
3041 #ifdef HAVE_FLOCK
3042     Vmail_lock_methods = Fcons (intern ("flock"), Vmail_lock_methods);
3043 #endif
3044 #ifdef HAVE_MMDF
3045     Vmail_lock_methods = Fcons (intern ("mmdf"), Vmail_lock_methods);
3046 #endif
3047 #ifdef HAVE_LOCKING
3048     Vmail_lock_methods = Fcons (intern ("locking"), Vmail_lock_methods);
3049 #endif
3050   }
3051
3052   DEFVAR_CONST_LISP ("configure-mail-lock-method", &Vconfigure_mail_lock_method /*
3053 Mail spool locking method suggested by configure.  This is one
3054 of the symbols in MAIL-LOCK-METHODS.
3055 */ );
3056   {
3057 #if defined(MAIL_LOCK_FLOCK) && defined(HAVE_FLOCK)
3058     Vconfigure_mail_lock_method = intern("flock");
3059 #elif defined(MAIL_LOCK_LOCKF) && defined(HAVE_LOCKF)
3060     Vconfigure_mail_lock_method = intern("lockf");
3061 #elif defined(MAIL_LOCK_MMDF) && defined(HAVE_MMDF)
3062     Vconfigure_mail_lock_method = intern("mmdf");
3063 #elif defined(MAIL_LOCK_LOCKING) && defined(HAVE_LOCKING)
3064     Vconfigure_mail_lock_method = intern("locking");
3065 #else
3066     Vconfigure_mail_lock_method = intern("dot");
3067 #endif
3068   }
3069
3070   DEFVAR_LISP ("path-separator", &Vpath_separator /*
3071 The directory separator in search paths, as a string.
3072 */ );
3073   {
3074     char c = SEPCHAR;
3075     Vpath_separator = make_string ((Bufbyte *)&c, 1);
3076   }
3077 }
3078
3079 void
3080 complex_vars_of_emacs (void)
3081 {
3082   /* This is all related to path searching. */
3083
3084   DEFVAR_LISP ("emacs-program-name", &Vemacs_program_name /*
3085 *Name of the Emacs variant.
3086 For example, this may be \"xemacs\" or \"infodock\".
3087 This is mainly meant for use in path searching.
3088 */ );
3089   Vemacs_program_name = build_string ((char *) PATH_PROGNAME);
3090
3091   DEFVAR_LISP ("emacs-program-version", &Vemacs_program_version /*
3092 *Version of the Emacs variant.
3093 This typically has the form XX.XX[-bXX].
3094 This is mainly meant for use in path searching.
3095 */ );
3096   Vemacs_program_version = build_string ((char *) PATH_VERSION);
3097
3098   DEFVAR_LISP ("exec-path", &Vexec_path /*
3099 *List of directories to search programs to run in subprocesses.
3100 Each element is a string (directory name) or nil (try default directory).
3101 */ );
3102   Vexec_path = Qnil;
3103
3104   DEFVAR_LISP ("exec-directory", &Vexec_directory /*
3105 *Directory of architecture-dependent files that come with XEmacs,
3106 especially executable programs intended for XEmacs to invoke.
3107 */ );
3108   Vexec_directory = Qnil;
3109
3110   DEFVAR_LISP ("configure-exec-directory", &Vconfigure_exec_directory /*
3111 For internal use by the build procedure only.
3112 configure's idea of what EXEC-DIRECTORY will be.
3113 */ );
3114 #ifdef PATH_EXEC
3115   Vconfigure_exec_directory = Ffile_name_as_directory
3116     (build_string ((char *) PATH_EXEC));
3117 #else
3118   Vconfigure_exec_directory = Qnil;
3119 #endif
3120
3121   DEFVAR_LISP ("lisp-directory", &Vlisp_directory /*
3122 *Directory of core Lisp files that come with XEmacs.
3123 */ );
3124   Vlisp_directory = Qnil;
3125
3126   DEFVAR_LISP ("configure-lisp-directory", &Vconfigure_lisp_directory /*
3127 For internal use by the build procedure only.
3128 configure's idea of what LISP-DIRECTORY will be.
3129 */ );
3130 #ifdef PATH_LOADSEARCH
3131   Vconfigure_lisp_directory = Ffile_name_as_directory
3132     (build_string ((char *) PATH_LOADSEARCH));
3133 #else
3134   Vconfigure_lisp_directory = Qnil;
3135 #endif
3136
3137   DEFVAR_LISP ("module-directory", &Vmodule_directory /*
3138 *Directory of core dynamic modules that come with XEmacs.
3139 */ );
3140   Vmodule_directory = Qnil;
3141
3142   DEFVAR_LISP ("configure-module-directory", &Vconfigure_module_directory /*
3143 For internal use by the build procedure only.
3144 configure's idea of what MODULE-DIRECTORY will be.
3145 */ );
3146 #ifdef PATH_MODULESEARCH
3147   Vconfigure_module_directory = Ffile_name_as_directory
3148     (build_string ((char *) PATH_MODULESEARCH));
3149 #else
3150   Vconfigure_module_directory = Qnil;
3151 #endif
3152
3153   DEFVAR_LISP ("configure-package-path", &Vconfigure_package_path /*
3154 For internal use by the build procedure only.
3155 configure's idea of what the package path will be.
3156 */ );
3157 #ifdef PATH_PACKAGEPATH
3158   Vconfigure_package_path = decode_path (PATH_PACKAGEPATH);
3159 #else
3160   Vconfigure_package_path = Qnil;
3161 #endif
3162
3163   DEFVAR_LISP ("data-directory", &Vdata_directory /*
3164 *Directory of architecture-independent files that come with XEmacs,
3165 intended for XEmacs to use.
3166 Use of this variable in new code is almost never correct.  See the
3167 function `locate-data-directory' and the variable `data-directory-list'.
3168 */ );
3169   Vdata_directory = Qnil;
3170
3171   DEFVAR_LISP ("configure-data-directory", &Vconfigure_data_directory /*
3172 For internal use by the build procedure only.
3173 configure's idea of what DATA-DIRECTORY will be.
3174 */ );
3175 #ifdef PATH_DATA
3176   Vconfigure_data_directory = Ffile_name_as_directory
3177     (build_string ((char *) PATH_DATA));
3178 #else
3179   Vconfigure_data_directory = Qnil;
3180 #endif
3181
3182   DEFVAR_LISP ("data-directory-list", &Vdata_directory_list /*
3183 *List of directories of architecture-independent files that come with XEmacs
3184 or were installed as packages, and are intended for XEmacs to use.
3185 */ );
3186   Vdata_directory_list = Qnil;
3187
3188 #ifdef CLASH_DETECTION
3189   DEFVAR_LISP ("configure-lock-directory", &Vconfigure_lock_directory /*
3190 For internal use by the build procedure only.
3191 configure's idea of what LOCK-DIRECTORY will be.
3192 */ );
3193 #ifdef PATH_LOCK
3194   Vconfigure_lock_directory = Ffile_name_as_directory
3195     (build_string ((char *) PATH_LOCK));
3196 #else
3197   Vconfigure_lock_directory = Qnil;
3198 #endif
3199 #endif /* CLASH_DETECTION */
3200
3201   DEFVAR_LISP ("site-directory", &Vsite_directory /*
3202 *Directory of site-specific Lisp files that come with XEmacs.
3203 */ );
3204   Vsite_directory = Qnil;
3205
3206   DEFVAR_LISP ("configure-site-directory", &Vconfigure_site_directory /*
3207 For internal use by the build procedure only.
3208 configure's idea of what SITE-DIRECTORY will be.
3209 */ );
3210 #ifdef PATH_SITE
3211   Vconfigure_site_directory = Ffile_name_as_directory
3212     (build_string ((char *) PATH_SITE));
3213 #else
3214   Vconfigure_site_directory = Qnil;
3215 #endif
3216
3217   DEFVAR_LISP ("site-module-directory", &Vsite_module_directory /*
3218 *Directory of site-specific loadable modules that come with XEmacs.
3219 */ );
3220   Vsite_module_directory = Qnil;
3221
3222   DEFVAR_LISP ("configure-site-module-directory", &Vconfigure_site_module_directory /*
3223 For internal use by the build procedure only.
3224 configure's idea of what SITE-DIRECTORY will be.
3225 */ );
3226 #ifdef PATH_SITE_MODULES
3227   Vconfigure_site_module_directory = Ffile_name_as_directory
3228     (build_string ((char *) PATH_SITE_MODULES));
3229 #else
3230   Vconfigure_site_module_directory = Qnil;
3231 #endif
3232
3233   DEFVAR_LISP ("doc-directory", &Vdoc_directory /*
3234 *Directory containing the DOC file that comes with XEmacs.
3235 This is usually the same as exec-directory.
3236 */ );
3237   Vdoc_directory = Qnil;
3238
3239   DEFVAR_LISP ("configure-doc-directory", &Vconfigure_doc_directory /*
3240 For internal use by the build procedure only.
3241 configure's idea of what DOC-DIRECTORY will be.
3242 */ );
3243 #ifdef PATH_DOC
3244   Vconfigure_doc_directory = Ffile_name_as_directory
3245     (build_string ((char *) PATH_DOC));
3246 #else
3247   Vconfigure_doc_directory = Qnil;
3248 #endif
3249
3250   DEFVAR_LISP ("configure-exec-prefix-directory", &Vconfigure_exec_prefix_directory /*
3251 For internal use by the build procedure only.
3252 configure's idea of what EXEC-PREFIX-DIRECTORY will be.
3253 */ );
3254 #ifdef PATH_EXEC_PREFIX
3255   Vconfigure_exec_prefix_directory = Ffile_name_as_directory
3256     (build_string ((char *) PATH_EXEC_PREFIX));
3257 #else
3258   Vconfigure_exec_prefix_directory = Qnil;
3259 #endif
3260
3261   DEFVAR_LISP ("configure-prefix-directory", &Vconfigure_prefix_directory /*
3262 For internal use by the build procedure only.
3263 configure's idea of what PREFIX-DIRECTORY will be.
3264 */ );
3265 #ifdef PATH_PREFIX
3266   Vconfigure_prefix_directory = Ffile_name_as_directory
3267     (build_string ((char *) PATH_PREFIX));
3268 #else
3269   Vconfigure_prefix_directory = Qnil;
3270 #endif
3271
3272   DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory /*
3273 For internal use by the build procedure only.
3274 This is the name of the directory in which the build procedure installed
3275 Emacs's info files; the default value for Info-default-directory-list
3276 includes this.
3277 */ );
3278 #ifdef PATH_INFO
3279   Vconfigure_info_directory =
3280     Ffile_name_as_directory (build_string (PATH_INFO));
3281 #else
3282   Vconfigure_info_directory = Qnil;
3283 #endif
3284
3285   DEFVAR_LISP ("configure-info-path", &Vconfigure_info_path /*
3286 The configured initial path for info documentation.
3287 */ );
3288 #ifdef PATH_INFOPATH
3289   Vconfigure_info_path = decode_path (PATH_INFOPATH);
3290 #else
3291   Vconfigure_info_path = Qnil;
3292 #endif
3293 }
3294
3295 #if defined(__sgi) && !defined(PDUMP)
3296 /* This is so tremendously ugly I'd puke. But then, it works.
3297  * The target is to override the static constructor from the
3298  * libiflPNG.so library which is maskerading as libz, and
3299  * cores on us when re-started from the dumped executable.
3300  * This will have to go for 21.1  -- OG.
3301  */
3302 void __sti__iflPNGFile_c___(void);
3303 void __sti__iflPNGFile_c___()
3304 {
3305 }
3306
3307 #endif