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