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