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