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