(fatal_error_signal_hook): New variable.
[bpt/emacs.git] / src / emacs.c
CommitLineData
f927c5ae 1/* Fully extensible Emacs, running on Unix, intended for GNU.
6c145d35
EZ
2 Copyright (C) 1985,86,87,93,94,95,97,98,1999,2001
3 Free Software Foundation, Inc.
f927c5ae
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
40be253a 9the Free Software Foundation; either version 2, or (at your option)
f927c5ae
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
5a7670bf
RS
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
f927c5ae
JB
21
22
68c45bf0 23#include <config.h>
f927c5ae
JB
24#include <signal.h>
25#include <errno.h>
f927c5ae
JB
26#include <stdio.h>
27
28#include <sys/types.h>
29#include <sys/file.h>
30
31#ifdef VMS
32#include <ssdef.h>
33#endif
34
cda4a5fd
GM
35#ifdef HAVE_UNISTD_H
36#include <unistd.h>
37#endif
38
488b7cb3 39#ifdef BSD_SYSTEM
f927c5ae
JB
40#include <sys/ioctl.h>
41#endif
42
f927c5ae
JB
43#include "lisp.h"
44#include "commands.h"
bef79ee4 45#include "intervals.h"
a08a816a 46#include "buffer.h"
f927c5ae 47
edc8ae07 48#include "systty.h"
6c3a4e9d 49#include "blockinput.h"
8090eb09 50#include "syssignal.h"
6c362a8b 51#include "process.h"
5bda49c6 52#include "termhooks.h"
dfcf069d 53#include "keyboard.h"
a41f8bed 54
68c45bf0
PE
55#ifdef HAVE_SETLOCALE
56#include <locale.h>
57#endif
58
53c58b5d
RS
59#ifdef HAVE_SETRLIMIT
60#include <sys/time.h>
61#include <sys/resource.h>
62#endif
63
f927c5ae
JB
64#ifndef O_RDWR
65#define O_RDWR 2
66#endif
67
f95c3f91 68#ifdef HAVE_SETPGID
2b7e8799 69#if !defined (USG) || defined (BSD_PGRPS)
90553aa3 70#undef setpgrp
f95c3f91
GM
71#define setpgrp setpgid
72#endif
2b7e8799 73#endif
f95c3f91 74
90553aa3
DL
75extern void malloc_warning P_ ((char *));
76extern void set_time_zone_rule P_ ((char *));
8892f40b
GM
77#ifdef HAVE_INDEX
78extern char *index P_ ((const char *, int));
79#endif
43165242 80
30aa3f19
GM
81/* Make these values available in GDB, which doesn't see macros. */
82
83EMACS_INT gdb_valbits = VALBITS;
84EMACS_INT gdb_gctypebits = GCTYPEBITS;
85EMACS_INT gdb_emacs_intbits = sizeof (EMACS_INT) * BITS_PER_CHAR;
86#ifdef DATA_SEG_BITS
87EMACS_INT gdb_data_seg_bits = DATA_SEG_BITS;
88#else
89EMACS_INT gdb_data_seg_bits = 0;
90#endif
00da0e4a 91EMACS_INT PVEC_FLAG = PSEUDOVECTOR_FLAG;
30aa3f19 92
f927c5ae
JB
93/* Command line args from shell, as list of strings */
94Lisp_Object Vcommand_line_args;
95
59653951
JB
96/* The name under which Emacs was invoked, with any leading directory
97 names discarded. */
98Lisp_Object Vinvocation_name;
99
ace40a69
RS
100/* The directory name from which Emacs was invoked. */
101Lisp_Object Vinvocation_directory;
102
07f4d123
RS
103/* The directory name in which to find subdirs such as lisp and etc.
104 nil means get them only from PATH_LOADSEARCH. */
105Lisp_Object Vinstallation_directory;
106
e5d77022
JB
107/* Hook run by `kill-emacs' before it does really anything. */
108Lisp_Object Vkill_emacs_hook;
109
7317d9e8
RS
110#ifdef SIGUSR1
111/* Hooks for signal USR1 and USR2 handing */
112Lisp_Object Vsignal_USR1_hook;
113#ifdef SIGUSR2
114Lisp_Object Vsignal_USR2_hook;
115#endif
116#endif
117
074a066b
GV
118/* Search path separator. */
119Lisp_Object Vpath_separator;
120
f927c5ae
JB
121/* Set nonzero after Emacs has started up the first time.
122 Prevents reinitialization of the Lisp world and keymaps
123 on subsequent starts. */
124int initialized;
125
15aaf1b5
RS
126#ifdef DOUG_LEA_MALLOC
127/* Preserves a pointer to the memory allocated that copies that
128 static data inside glibc's malloc. */
129void *malloc_state_ptr;
130/* From glibc, a routine that returns a copy of the malloc internal state. */
131extern void *malloc_get_state ();
132/* From glibc, a routine that overwrites the malloc internal state. */
133extern void malloc_set_state ();
7c9cd446
AS
134/* Non-zero if the MALLOC_CHECK_ enviroment variable was set while
135 dumping. Used to work around a bug in glibc's malloc. */
136int malloc_using_checking;
15aaf1b5
RS
137#endif
138
271c7b7c 139/* Variable whose value is symbol giving operating system type. */
f927c5ae 140Lisp_Object Vsystem_type;
271c7b7c
RS
141
142/* Variable whose value is string giving configuration built for. */
143Lisp_Object Vsystem_configuration;
f0fc0b1a
KH
144
145/* Variable whose value is string giving configuration options,
146 for use when reporting bugs. */
147Lisp_Object Vsystem_configuration_options;
148
213d0b1f
RS
149Lisp_Object Qfile_name_handler_alist;
150
68c45bf0 151/* Current and previous system locales for messages and time. */
ca9c0567
PE
152Lisp_Object Vsystem_messages_locale;
153Lisp_Object Vprevious_system_messages_locale;
154Lisp_Object Vsystem_time_locale;
155Lisp_Object Vprevious_system_time_locale;
68c45bf0 156
f927c5ae
JB
157/* If non-zero, emacs should not attempt to use an window-specific code,
158 but instead should use the virtual terminal under which it was started */
159int inhibit_window_system;
160
5aa7f46a
JB
161/* If nonzero, set Emacs to run at this priority. This is also used
162 in child_setup and sys_suspend to make sure subshells run at normal
163 priority; Those functions have their own extern declaration. */
3005da00
RS
164int emacs_priority;
165
7074fde6
FP
166/* If non-zero a filter or a sentinel is running. Tested to save the match
167 data on the first attempt to change it inside asynchronous code. */
168int running_asynch_code;
169
647cec74 170#ifdef BSD_PGRPS
9ae8f997
JB
171/* See sysdep.c. */
172extern int inherited_pgroup;
173#endif
174
f927c5ae
JB
175#ifdef HAVE_X_WINDOWS
176/* If non-zero, -d was specified, meaning we're using some window system. */
177int display_arg;
178#endif
179
180/* An address near the bottom of the stack.
181 Tells GC how to save a copy of the stack. */
182char *stack_bottom;
183
8ba50e1a 184#ifdef HAVE_WINDOW_SYSTEM
f927c5ae 185extern Lisp_Object Vwindow_system;
8ba50e1a 186#endif /* HAVE_WINDOW_SYSTEM */
f927c5ae 187
1090a161
RS
188extern Lisp_Object Vauto_save_list_file_name;
189
f927c5ae
JB
190#ifdef USG_SHARED_LIBRARIES
191/* If nonzero, this is the place to put the end of the writable segment
192 at startup. */
193
194unsigned int bss_end = 0;
195#endif
196
197/* Nonzero means running Emacs without interactive terminal. */
198
199int noninteractive;
200
201/* Value of Lisp variable `noninteractive'.
202 Normally same as C variable `noninteractive'
203 but nothing terrible happens if user sets this one. */
204
205int noninteractive1;
e29f86e4
RS
206
207/* Save argv and argc. */
208char **initial_argv;
209int initial_argc;
081bef73
RS
210
211static void sort_args ();
dfcf069d 212void syms_of_emacs ();
bd4590ba 213
73ebcd25 214#define USAGE1 "\
bd4590ba
GM
215Usage: %s [OPTION-OR-FILENAME]...\n\
216\n\
217Run Emacs, the extensible, customizable, self-documenting real-time\n\
218display editor. The recommended way to start Emacs for normal editing\n\
219is with no options at all.\n\
220\n\
221Run M-x info RET m emacs RET m command arguments RET inside Emacs to\n\
222read the main documentation for these command-line arguments.\n\
223\n\
224Initialization options:\n\
225\n\
226--batch do not do interactive display; implies -q\n\
227--debug-init enable Emacs Lisp debugger during init file\n\
228--help display this help message and exit\n\
229--multibyte, --no-unibyte run Emacs in multibyte mode\n\
230--no-init-file, -q load neither ~/.emacs nor default.el\n\
231--no-shared-memory, -nl do not use shared memory\n\
232--no-site-file do not load site-start.el\n\
233--no-windows, -nw don't communicate with X, ignoring $DISPLAY\n\
234--terminal, -t DEVICE use DEVICE for terminal I/O\n\
235--unibyte, --no-multibyte run Emacs in unibyte mode\n\
236--user, -u USER load ~USER/.emacs instead of your own\n\
237--version display version information and exit\n\
238\n\
239Action options:\n\
240\n\
241FILE visit FILE using find-file\n\
5333e22e
GM
242+LINE FILE visit FILE using find-file, then go to line LINE\n\
243+LINE:COLUMN FILE visit FILE using find-file, then go to line LINE,\n\
244 column COLUMN\n\
bd4590ba
GM
245--directory, -L DIR add DIR to variable load-path\n\
246--eval EXPR evaluate Emacs Lisp expression EXPR\n\
247--execute EXPR evaluate Emacs Lisp expression EXPR\n\
248--find-file FILE visit FILE\n\
249--funcall, -f FUNC call Emacs function FUNC with no arguments\n\
250--insert FILE insert contents of FILE into current buffer\n\
251--kill exit without asking for confirmation\n\
252--load, -l FILE load FILE of Emacs Lisp code using the load function\n\
253--visit FILE visit FILE\n\
73ebcd25
AI
254\n"
255
256#define USAGE2 "\
5b2ca26b 257Display options:\n\
bd4590ba
GM
258\n\
259--background-color, -bg COLOR window background color\n\
260--border-color, -bd COLOR main border color\n\
261--border-width, -bw WIDTH width of main border\n\
262--cursor-color, -cr COLOR color of the Emacs cursor indicating point\n\
263--display, -d DISPLAY use X server DISPLAY\n\
264--font, -fn FONT default font; must be fixed-widthp\n\
265--foreground-color, -fg COLOR window foreground color\n\
266--geometry, -g GEOMETRY window geometry\n\
267--iconic start Emacs in iconified state\n\
268--icon-type, -i use picture of gnu for Emacs icon\n\
269--internal-border, -ib WIDTH width between text and main border\n\
ecb5709c 270--line-spacing, -lsp PIXELS additional space to put between lines\n\
bd4590ba
GM
271--mouse-color, -ms COLOR mouse cursor color in Emacs window\n\
272--name NAME title of main Emacs window\n\
273--reverse-video, -r, -rv switch foreground and background\n\
274--title, -T, -wn, TITLE title for Emacs windows\n\
275--vertical-scroll-bars, -vb enable vertical scroll bars\n\
276--xrm XRESOURCES set additional X resources\n\
277\n\
278You can generally also specify long option names with a single -; for\n\
279example, -batch as well as --batch. You can use any unambiguous\n\
280abbreviation for a --option.\n\
281\n\
282Various environment variables and window system resources also affect\n\
283Emacs' operation. See the main documentation.\n\
284\n\
285Report bugs to bug-gnu-emacs@gnu.org. First, please see the Bugs\n\
286section of the Emacs manual or the file BUGS.\n"
287
f927c5ae
JB
288\f
289/* Signal code for the fatal signal that was received */
290int fatal_error_code;
291
292/* Nonzero if handling a fatal error already */
293int fatal_error_in_progress;
294
4fab758d
GM
295/* If non-null, call this function from fata_error_signal before
296 committing suicide. */
297
298void (*fatal_error_signal_hook) P_ ((void));
299
300
7317d9e8 301#ifdef SIGUSR1
7317d9e8
RS
302SIGTYPE
303handle_USR1_signal (sig)
304 int sig;
305{
5bda49c6
RS
306 struct input_event buf;
307
75816372 308 bzero (&buf, sizeof buf);
5b50dae1 309 buf.kind = USER_SIGNAL_EVENT;
74c2d1c6 310 buf.frame_or_window = selected_frame;
5bda49c6
RS
311
312 kbd_buffer_store_event (&buf);
7317d9e8 313}
5bda49c6 314#endif /* SIGUSR1 */
fcf01ad3 315
7317d9e8 316#ifdef SIGUSR2
7317d9e8
RS
317SIGTYPE
318handle_USR2_signal (sig)
319 int sig;
320{
5bda49c6
RS
321 struct input_event buf;
322
75816372 323 bzero (&buf, sizeof buf);
5b50dae1 324 buf.kind = USER_SIGNAL_EVENT;
e2fa15ff 325 buf.code = 1;
74c2d1c6 326 buf.frame_or_window = selected_frame;
5bda49c6
RS
327
328 kbd_buffer_store_event (&buf);
7317d9e8 329}
5bda49c6 330#endif /* SIGUSR2 */
7317d9e8 331
9ab8560d 332/* Handle bus errors, invalid instruction, etc. */
2447c626 333SIGTYPE
f927c5ae
JB
334fatal_error_signal (sig)
335 int sig;
336{
f927c5ae
JB
337 fatal_error_code = sig;
338 signal (sig, SIG_DFL);
339
061b7f94
RS
340 TOTALLY_UNBLOCK_INPUT;
341
f927c5ae 342 /* If fatal error occurs in code below, avoid infinite recursion. */
8090eb09
JB
343 if (! fatal_error_in_progress)
344 {
345 fatal_error_in_progress = 1;
f927c5ae 346
f7ab4e3d 347 shut_down_emacs (sig, 0, Qnil);
8090eb09 348 }
f927c5ae
JB
349
350#ifdef VMS
f927c5ae
JB
351 LIB$STOP (SS$_ABORT);
352#else
8090eb09
JB
353 /* Signal the same code; this time it will really be fatal.
354 Remember that since we're in a signal handler, the signal we're
355 going to send is probably blocked, so we have to unblock it if we
356 want to really receive it. */
29b89fe0 357#ifndef MSDOS
a90538cb 358 sigunblock (sigmask (fatal_error_code));
29b89fe0 359#endif
4fab758d
GM
360
361 if (fatal_error_signal_hook)
362 fatal_error_signal_hook ();
363
f927c5ae
JB
364 kill (getpid (), fatal_error_code);
365#endif /* not VMS */
366}
271c7b7c
RS
367
368#ifdef SIGDANGER
369
16c323ee 370/* Handler for SIGDANGER. */
271c7b7c
RS
371SIGTYPE
372memory_warning_signal (sig)
373 int sig;
374{
375 signal (sig, memory_warning_signal);
376
377 malloc_warning ("Operating system warns that virtual memory is running low.\n");
55796183
RS
378
379 /* It might be unsafe to call do_auto_save now. */
380 force_auto_save_soon ();
271c7b7c
RS
381}
382#endif
fcf01ad3
RS
383
384/* We define abort, rather than using it from the library,
c9fe9cce
KH
385 so that GDB can return from a breakpoint here.
386 MSDOS has its own definition on msdos.c */
fcf01ad3 387
37e2fdd2 388#if ! defined (DOS_NT) && ! defined (NO_ABORT)
ca9c0567
PE
389
390#ifndef ABORT_RETURN_TYPE
391#define ABORT_RETURN_TYPE void
392#endif
393
394ABORT_RETURN_TYPE
fcf01ad3
RS
395abort ()
396{
397 kill (getpid (), SIGABRT);
37e2fdd2
RS
398 /* This shouldn't be executed, but it prevents a warning. */
399 exit (1);
fcf01ad3 400}
c9fe9cce 401#endif
fcf01ad3 402
f927c5ae
JB
403\f
404/* Code for dealing with Lisp access to the Unix command line */
405
dfcf069d 406static void
f927c5ae
JB
407init_cmdargs (argc, argv, skip_args)
408 int argc;
409 char **argv;
410 int skip_args;
411{
412 register int i;
213d0b1f
RS
413 Lisp_Object name, dir, tem;
414 int count = specpdl_ptr - specpdl;
415 Lisp_Object raw_name;
f927c5ae 416
e29f86e4
RS
417 initial_argv = argv;
418 initial_argc = argc;
419
213d0b1f
RS
420 raw_name = build_string (argv[0]);
421
422 /* Add /: to the front of the name
423 if it would otherwise be treated as magic. */
424 tem = Ffind_file_name_handler (raw_name, Qt);
425 if (! NILP (tem))
426 raw_name = concat2 (build_string ("/:"), raw_name);
427
428 Vinvocation_name = Ffile_name_nondirectory (raw_name);
429 Vinvocation_directory = Ffile_name_directory (raw_name);
430
ace40a69
RS
431 /* If we got no directory in argv[0], search PATH to find where
432 Emacs actually came from. */
433 if (NILP (Vinvocation_directory))
434 {
435 Lisp_Object found;
436 int yes = openp (Vexec_path, Vinvocation_name,
6c362a8b 437 EXEC_SUFFIXES, &found, 1);
e443f843 438 if (yes == 1)
213d0b1f
RS
439 {
440 /* Add /: to the front of the name
441 if it would otherwise be treated as magic. */
442 tem = Ffind_file_name_handler (found, Qt);
443 if (! NILP (tem))
444 found = concat2 (build_string ("/:"), found);
445 Vinvocation_directory = Ffile_name_directory (found);
446 }
ace40a69 447 }
59653951 448
4133b300
RS
449 if (!NILP (Vinvocation_directory)
450 && NILP (Ffile_name_absolute_p (Vinvocation_directory)))
213d0b1f
RS
451 /* Emacs was started with relative path, like ./emacs.
452 Make it absolute. */
4133b300
RS
453 Vinvocation_directory = Fexpand_file_name (Vinvocation_directory, Qnil);
454
07f4d123
RS
455 Vinstallation_directory = Qnil;
456
457 if (!NILP (Vinvocation_directory))
458 {
459 dir = Vinvocation_directory;
460 name = Fexpand_file_name (Vinvocation_name, dir);
461 while (1)
462 {
f5ab9736 463 Lisp_Object tem, lib_src_exists;
07f4d123
RS
464 Lisp_Object etc_exists, info_exists;
465
f5ab9736
RS
466 /* See if dir contains subdirs for use by Emacs.
467 Check for the ones that would exist in a build directory,
468 not including lisp and info. */
469 tem = Fexpand_file_name (build_string ("lib-src"), dir);
470 lib_src_exists = Ffile_exists_p (tem);
de004cc6 471
70344b34 472#ifdef MSDOS
de004cc6
RS
473 /* MSDOS installations frequently remove lib-src, but we still
474 must set installation-directory, or else info won't find
475 its files (it uses the value of installation-directory). */
476 tem = Fexpand_file_name (build_string ("info"), dir);
477 info_exists = Ffile_exists_p (tem);
70344b34
EZ
478#else
479 info_exists = Qnil;
480#endif
de004cc6
RS
481
482 if (!NILP (lib_src_exists) || !NILP (info_exists))
07f4d123 483 {
f5ab9736
RS
484 tem = Fexpand_file_name (build_string ("etc"), dir);
485 etc_exists = Ffile_exists_p (tem);
486 if (!NILP (etc_exists))
07f4d123 487 {
f5ab9736
RS
488 Vinstallation_directory
489 = Ffile_name_as_directory (dir);
490 break;
07f4d123
RS
491 }
492 }
493
494 /* See if dir's parent contains those subdirs. */
f5ab9736
RS
495 tem = Fexpand_file_name (build_string ("../lib-src"), dir);
496 lib_src_exists = Ffile_exists_p (tem);
de004cc6 497
70344b34
EZ
498
499#ifdef MSDOS
500 /* See the MSDOS commentary above. */
de004cc6
RS
501 tem = Fexpand_file_name (build_string ("../info"), dir);
502 info_exists = Ffile_exists_p (tem);
70344b34
EZ
503#else
504 info_exists = Qnil;
505#endif
de004cc6
RS
506
507 if (!NILP (lib_src_exists) || !NILP (info_exists))
07f4d123 508 {
f5ab9736
RS
509 tem = Fexpand_file_name (build_string ("../etc"), dir);
510 etc_exists = Ffile_exists_p (tem);
511 if (!NILP (etc_exists))
07f4d123 512 {
f5ab9736
RS
513 tem = Fexpand_file_name (build_string (".."), dir);
514 Vinstallation_directory
515 = Ffile_name_as_directory (tem);
516 break;
07f4d123
RS
517 }
518 }
519
520 /* If the Emacs executable is actually a link,
521 next try the dir that the link points into. */
522 tem = Ffile_symlink_p (name);
523 if (!NILP (tem))
524 {
260ec24d 525 name = Fexpand_file_name (tem, dir);
07f4d123
RS
526 dir = Ffile_name_directory (name);
527 }
528 else
529 break;
530 }
531 }
532
f927c5ae
JB
533 Vcommand_line_args = Qnil;
534
535 for (i = argc - 1; i >= 0; i--)
536 {
537 if (i == 0 || i > skip_args)
538 Vcommand_line_args
539 = Fcons (build_string (argv[i]), Vcommand_line_args);
540 }
213d0b1f
RS
541
542 unbind_to (count, Qnil);
f927c5ae 543}
59653951
JB
544
545DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
546 "Return the program name that was used to run Emacs.\n\
547Any directory names are omitted.")
548 ()
549{
550 return Fcopy_sequence (Vinvocation_name);
551}
552
ace40a69
RS
553DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
554 0, 0, 0,
555 "Return the directory name in which the Emacs executable was located")
556 ()
557{
558 return Fcopy_sequence (Vinvocation_directory);
559}
560
f927c5ae
JB
561\f
562#ifdef VMS
563#ifdef LINK_CRTL_SHARE
34035df3 564#ifdef SHARABLE_LIB_BUG
f927c5ae 565extern noshare char **environ;
34035df3 566#endif /* SHARABLE_LIB_BUG */
f927c5ae
JB
567#endif /* LINK_CRTL_SHARE */
568#endif /* VMS */
569
0269dedb
RS
570#ifdef HAVE_TZSET
571/* A valid but unlikely value for the TZ environment value.
572 It is OK (though a bit slower) if the user actually chooses this value. */
573static char dump_tz[] = "UtC0";
574#endif
575
a90538cb 576#ifndef ORDINARY_LINK
efd241cc
RS
577/* We don't include crtbegin.o and crtend.o in the link,
578 so these functions and variables might be missed.
579 Provide dummy definitions to avoid error.
580 (We don't have any real constructors or destructors.) */
581#ifdef __GNUC__
46e65b73 582#ifndef GCC_CTORS_IN_LIBC
dfcf069d 583void __do_global_ctors ()
efd241cc 584{}
dfcf069d 585void __do_global_ctors_aux ()
c83a7064 586{}
dfcf069d 587void __do_global_dtors ()
33143604 588{}
64c1864a
RS
589/* Linux has a bug in its library; avoid an error. */
590#ifndef LINUX
c83a7064 591char * __CTOR_LIST__[2] = { (char *) (-1), 0 };
64c1864a 592#endif
c83a7064 593char * __DTOR_LIST__[2] = { (char *) (-1), 0 };
46e65b73 594#endif /* GCC_CTORS_IN_LIBC */
dfcf069d 595void __main ()
efd241cc 596{}
efd241cc 597#endif /* __GNUC__ */
a90538cb 598#endif /* ORDINARY_LINK */
efd241cc 599
e2925360
KH
600/* Test whether the next argument in ARGV matches SSTR or a prefix of
601 LSTR (at least MINLEN characters). If so, then if VALPTR is non-null
602 (the argument is supposed to have a value) store in *VALPTR either
603 the next argument or the portion of this one after the equal sign.
604 ARGV is read starting at position *SKIPPTR; this index is advanced
605 by the number of arguments used.
606
607 Too bad we can't just use getopt for all of this, but we don't have
608 enough information to do it right. */
081bef73 609
e2925360 610static int
df6530f8 611argmatch (argv, argc, sstr, lstr, minlen, valptr, skipptr)
e2925360 612 char **argv;
df6530f8 613 int argc;
e2925360
KH
614 char *sstr;
615 char *lstr;
616 int minlen;
617 char **valptr;
618 int *skipptr;
619{
6bbd7a29 620 char *p = NULL;
e2925360 621 int arglen;
df6530f8
RS
622 char *arg;
623
624 /* Don't access argv[argc]; give up in advance. */
625 if (argc <= *skipptr + 1)
626 return 0;
627
628 arg = argv[*skipptr+1];
e2925360
KH
629 if (arg == NULL)
630 return 0;
631 if (strcmp (arg, sstr) == 0)
632 {
633 if (valptr != NULL)
634 {
635 *valptr = argv[*skipptr+2];
636 *skipptr += 2;
637 }
638 else
639 *skipptr += 1;
640 return 1;
641 }
642 arglen = (valptr != NULL && (p = index (arg, '=')) != NULL
643 ? p - arg : strlen (arg));
c03e1113 644 if (lstr == 0 || arglen < minlen || strncmp (arg, lstr, arglen) != 0)
e2925360
KH
645 return 0;
646 else if (valptr == NULL)
647 {
648 *skipptr += 1;
649 return 1;
650 }
651 else if (p != NULL)
652 {
653 *valptr = p+1;
654 *skipptr += 1;
655 return 1;
656 }
657 else if (argv[*skipptr+2] != NULL)
658 {
659 *valptr = argv[*skipptr+2];
660 *skipptr += 2;
661 return 1;
662 }
663 else
664 {
665 return 0;
666 }
667}
668
b6779252 669#ifdef DOUG_LEA_MALLOC
f927c5ae 670
b6779252
KH
671/* malloc can be invoked even before main (e.g. by the dynamic
672 linker), so the dumped malloc state must be restored as early as
673 possible using this special hook. */
674
675static void
676malloc_initialize_hook ()
677{
1f9c3929 678#ifndef USE_CRT_DLL
b6779252 679 extern char **environ;
1f9c3929 680#endif
6000fe37 681
15aaf1b5
RS
682 if (initialized)
683 {
7c9cd446
AS
684 if (!malloc_using_checking)
685 /* Work around a bug in glibc's malloc. MALLOC_CHECK_ must be
686 ignored if the heap to be restored was constructed without
d942e12a
AS
687 malloc checking. Can't use unsetenv, since that calls malloc. */
688 {
689 char **p;
690
a57c4026 691 for (p = environ; p && *p; p++)
d942e12a
AS
692 if (strncmp (*p, "MALLOC_CHECK_=", 14) == 0)
693 {
694 do
695 *p = p[1];
696 while (*++p);
697 break;
698 }
699 }
b6779252 700
15aaf1b5
RS
701 malloc_set_state (malloc_state_ptr);
702 free (malloc_state_ptr);
703 }
7c9cd446
AS
704 else
705 malloc_using_checking = getenv ("MALLOC_CHECK_") != NULL;
b6779252
KH
706}
707
708void (*__malloc_initialize_hook) () = malloc_initialize_hook;
709
710#endif /* DOUG_LEA_MALLOC */
711
712/* ARGSUSED */
713int
714main (argc, argv, envp)
715 int argc;
716 char **argv;
717 char **envp;
718{
2313132f 719#if GC_MARK_STACK
39ec21ea
GM
720 Lisp_Object dummy;
721#endif
b6779252 722 char stack_bottom_variable;
68c45bf0 723 int do_initial_setlocale;
b6779252 724 int skip_args = 0;
1f9c3929 725#ifndef USE_CRT_DLL
b6779252 726 extern int errno;
1f9c3929 727#endif
b6779252
KH
728#ifdef HAVE_SETRLIMIT
729 struct rlimit rlim;
730#endif
731 int no_loadup = 0;
732
2313132f 733#if GC_MARK_STACK
39ec21ea
GM
734 extern Lisp_Object *stack_base;
735 stack_base = &dummy;
736#endif
737
b6779252
KH
738#ifdef LINUX_SBRK_BUG
739 __sbrk (1);
15aaf1b5
RS
740#endif
741
d785cf9e
RS
742#ifdef RUN_TIME_REMAP
743 if (initialized)
744 run_time_remap (argv[0]);
745#endif
746
081bef73 747 sort_args (argc, argv);
956e3c7e
RS
748 argc = 0;
749 while (argv[argc]) argc++;
081bef73 750
3270ae6e
KH
751 if (argmatch (argv, argc, "-version", "--version", 3, NULL, &skip_args)
752 /* We don't know the version number unless this is a dumped Emacs.
753 So ignore --version otherwise. */
754 && initialized)
1702afef
RS
755 {
756 Lisp_Object tem;
757 tem = Fsymbol_value (intern ("emacs-version"));
758 if (!STRINGP (tem))
759 {
760 fprintf (stderr, "Invalid value of `emacs-version'\n");
761 exit (1);
762 }
763 else
764 {
30ce1583 765 printf ("GNU Emacs %s\n", XSTRING (tem)->data);
6c145d35 766 printf ("Copyright (C) 2001 Free Software Foundation, Inc.\n");
b455c665
RS
767 printf ("GNU Emacs comes with ABSOLUTELY NO WARRANTY.\n");
768 printf ("You may redistribute copies of Emacs\n");
769 printf ("under the terms of the GNU General Public License.\n");
770 printf ("For more information about these matters, ");
94487c4e 771 printf ("see the file named COPYING.\n");
1702afef
RS
772 exit (0);
773 }
774 }
775
f927c5ae
JB
776/* Map in shared memory, if we are using that. */
777#ifdef HAVE_SHM
df6530f8 778 if (argmatch (argv, argc, "-nl", "--no-shared-memory", 6, NULL, &skip_args))
f927c5ae
JB
779 {
780 map_in_data (0);
781 /* The shared memory was just restored, which clobbered this. */
782 skip_args = 1;
783 }
784 else
785 {
786 map_in_data (1);
787 /* The shared memory was just restored, which clobbered this. */
788 skip_args = 0;
789 }
790#endif
791
19a36ec6 792#ifdef NeXT
b9df9faa
KH
793 {
794 extern int malloc_cookie;
795 /* This helps out unexnext.c. */
796 if (initialized)
797 if (malloc_jumpstart (malloc_cookie) != 0)
798 printf ("malloc jumpstart failed!\n");
799 }
19a36ec6
RS
800#endif /* NeXT */
801
f927c5ae
JB
802#ifdef VMS
803 /* If -map specified, map the data file in */
e2925360
KH
804 {
805 char *file;
df6530f8 806 if (argmatch (argv, argc, "-map", "--map-data", 3, &mapin_file, &skip_args))
e2925360
KH
807 mapin_data (file);
808 }
f927c5ae
JB
809
810#ifdef LINK_CRTL_SHARE
34035df3 811#ifdef SHARABLE_LIB_BUG
f927c5ae
JB
812 /* Bletcherous shared libraries! */
813 if (!stdin)
814 stdin = fdopen (0, "r");
815 if (!stdout)
816 stdout = fdopen (1, "w");
817 if (!stderr)
818 stderr = fdopen (2, "w");
819 if (!environ)
820 environ = envp;
34035df3 821#endif /* SHARABLE_LIB_BUG */
f927c5ae
JB
822#endif /* LINK_CRTL_SHARE */
823#endif /* VMS */
824
ea2acec5 825#if defined (HAVE_SETRLIMIT) && defined (RLIMIT_STACK)
6c2935e9
RS
826 /* Extend the stack space available.
827 Don't do that if dumping, since some systems (e.g. DJGPP)
828 might define a smaller stack limit at that time. */
829 if (1
830#ifndef CANNOT_DUMP
831 && (!noninteractive || initialized)
832#endif
833 && !getrlimit (RLIMIT_STACK, &rlim))
53c58b5d 834 {
509a8fcd 835 long newlim;
6c2935e9 836 extern int re_max_failures;
03effc23
KH
837 /* Approximate the amount regex.c needs per unit of re_max_failures. */
838 int ratio = 20 * sizeof (char *);
839 /* Then add 33% to cover the size of the smaller stacks that regex.c
840 successively allocates and discards, on its way to the maximum. */
841 ratio += ratio / 3;
842 /* Add in some extra to cover
843 what we're likely to use for other reasons. */
844 newlim = re_max_failures * ratio + 200000;
d0381a7f
RS
845#ifdef __NetBSD__
846 /* NetBSD (at least NetBSD 1.2G and former) has a bug in its
847 stack allocation routine for new process that the allocation
848 fails if stack limit is not on page boundary. So, round up the
849 new limit to page boundary. */
850 newlim = (newlim + getpagesize () - 1) / getpagesize () * getpagesize();
851#endif
509a8fcd 852 if (newlim > rlim.rlim_max)
6c2935e9
RS
853 {
854 newlim = rlim.rlim_max;
03effc23
KH
855 /* Don't let regex.c overflow the stack we have. */
856 re_max_failures = (newlim - 200000) / ratio;
6c2935e9 857 }
509a8fcd
RS
858 if (rlim.rlim_cur < newlim)
859 rlim.rlim_cur = newlim;
860
53c58b5d
RS
861 setrlimit (RLIMIT_STACK, &rlim);
862 }
ea2acec5 863#endif /* HAVE_SETRLIMIT and RLIMIT_STACK */
53c58b5d 864
f927c5ae
JB
865 /* Record (approximately) where the stack begins. */
866 stack_bottom = &stack_bottom_variable;
867
f927c5ae
JB
868#ifdef USG_SHARED_LIBRARIES
869 if (bss_end)
1d233b80 870 brk ((void *)bss_end);
f927c5ae
JB
871#endif
872
873 clearerr (stdin);
9ae8f997 874
f927c5ae 875#ifndef SYSTEM_MALLOC
bf7f4e90
RS
876 /* Arrange to get warning messages as memory fills up. */
877 memory_warnings (0, malloc_warning);
9ac0d9e0 878
f5a3c8c4
AI
879 /* Call malloc at least once, to run the initial __malloc_hook.
880 Also call realloc and free for consistency. */
881 free (realloc (malloc (4), 4));
bf7f4e90
RS
882
883 /* Arrange to disable interrupt input inside malloc etc. */
884 uninterrupt_malloc ();
f927c5ae
JB
885#endif /* not SYSTEM_MALLOC */
886
29b89fe0
RS
887#ifdef MSDOS
888 /* We do all file input/output as binary files. When we need to translate
889 newlines, we do that manually. */
890 _fmode = O_BINARY;
18198bb2
RS
891
892#if __DJGPP__ >= 2
893 if (!isatty (fileno (stdin)))
894 setmode (fileno (stdin), O_BINARY);
895 if (!isatty (fileno (stdout)))
896 {
897 fflush (stdout);
898 setmode (fileno (stdout), O_BINARY);
899 }
900#else /* not __DJGPP__ >= 2 */
29b89fe0
RS
901 (stdin)->_flag &= ~_IOTEXT;
902 (stdout)->_flag &= ~_IOTEXT;
903 (stderr)->_flag &= ~_IOTEXT;
18198bb2 904#endif /* not __DJGPP__ >= 2 */
29b89fe0
RS
905#endif /* MSDOS */
906
a422068f 907#ifdef SET_EMACS_PRIORITY
3005da00 908 if (emacs_priority)
5aa7f46a 909 nice (emacs_priority);
f927c5ae 910 setuid (getuid ());
a422068f 911#endif /* SET_EMACS_PRIORITY */
f927c5ae 912
68c45bf0
PE
913 /* Skip initial setlocale if LC_ALL is "C", as it's not needed in that case.
914 The build procedure uses this while dumping, to ensure that the
915 dumped Emacs does not have its system locale tables initialized,
916 as that might cause screwups when the dumped Emacs starts up. */
917 {
918 char *lc_all = getenv ("LC_ALL");
919 do_initial_setlocale = ! lc_all || strcmp (lc_all, "C");
920 }
921
922 /* Set locale now, so that initial error messages are localized properly.
923 fixup_locale must wait until later, since it builds strings. */
924 if (do_initial_setlocale)
925 setlocale (LC_ALL, "");
926
d8b3a65d 927#ifdef EXTRA_INITIALIZE
a9260219 928 EXTRA_INITIALIZE;
d8b3a65d
RS
929#endif
930
f927c5ae
JB
931 inhibit_window_system = 0;
932
4fc0b45b 933 /* Handle the -t switch, which specifies filename to use as terminal */
956e3c7e
RS
934 while (1)
935 {
936 char *term;
937 if (argmatch (argv, argc, "-t", "--terminal", 4, &term, &skip_args))
938 {
939 int result;
68c45bf0
PE
940 emacs_close (0);
941 emacs_close (1);
942 result = emacs_open (term, O_RDWR, 0);
956e3c7e
RS
943 if (result < 0)
944 {
945 char *errstring = strerror (errno);
946 fprintf (stderr, "emacs: %s: %s\n", term, errstring);
947 exit (1);
948 }
949 dup (0);
950 if (! isatty (0))
951 {
952 fprintf (stderr, "emacs: %s: not a tty\n", term);
953 exit (1);
954 }
955 fprintf (stderr, "Using %s\n", term);
8ba50e1a 956#ifdef HAVE_WINDOW_SYSTEM
956e3c7e 957 inhibit_window_system = 1; /* -t => -nw */
f927c5ae 958#endif
956e3c7e
RS
959 }
960 else
961 break;
962 }
963
df6530f8 964 if (argmatch (argv, argc, "-nw", "--no-windows", 6, NULL, &skip_args))
e2925360 965 inhibit_window_system = 1;
f927c5ae 966
e2925360 967 /* Handle the -batch switch, which means don't do interactive display. */
f927c5ae 968 noninteractive = 0;
df6530f8 969 if (argmatch (argv, argc, "-batch", "--batch", 5, NULL, &skip_args))
e2925360
KH
970 noninteractive = 1;
971
972 /* Handle the --help option, which gives a usage message.. */
df6530f8 973 if (argmatch (argv, argc, "-help", "--help", 3, NULL, &skip_args))
f927c5ae 974 {
73ebcd25
AI
975 printf (USAGE1, argv[0]);
976 printf (USAGE2);
e2925360 977 exit (0);
f927c5ae
JB
978 }
979
edb85f59
RS
980 if (! noninteractive)
981 {
982#ifdef BSD_PGRPS
983 if (initialized)
984 {
985 inherited_pgroup = EMACS_GETPGRP (0);
986 setpgrp (0, getpid ());
987 }
988#else
989#if defined (USG5) && defined (INTERRUPT_INPUT)
990 setpgrp ();
991#endif
992#endif
993 }
994
fb8e9847 995 init_signals ();
fb8e9847 996
1efa2983
KH
997 /* Don't catch SIGHUP if dumping. */
998 if (1
999#ifndef CANNOT_DUMP
1000 && initialized
1001#endif
1002 )
1003 {
57e3d22a 1004 sigblock (sigmask (SIGHUP));
1efa2983
KH
1005 /* In --batch mode, don't catch SIGHUP if already ignored.
1006 That makes nohup work. */
1007 if (! noninteractive
1008 || signal (SIGHUP, SIG_IGN) != SIG_IGN)
1009 signal (SIGHUP, fatal_error_signal);
57e3d22a 1010 sigunblock (sigmask (SIGHUP));
1efa2983
KH
1011 }
1012
f927c5ae
JB
1013 if (
1014#ifndef CANNOT_DUMP
1015 ! noninteractive || initialized
1016#else
1017 1
1018#endif
1019 )
1020 {
1efa2983 1021 /* Don't catch these signals in batch mode if dumping.
f927c5ae
JB
1022 On some machines, this sets static data that would make
1023 signal fail to work right when the dumped Emacs is run. */
f927c5ae
JB
1024 signal (SIGQUIT, fatal_error_signal);
1025 signal (SIGILL, fatal_error_signal);
1026 signal (SIGTRAP, fatal_error_signal);
7317d9e8
RS
1027#ifdef SIGUSR1
1028 signal (SIGUSR1, handle_USR1_signal);
1029#ifdef SIGUSR2
1030 signal (SIGUSR2, handle_USR2_signal);
1031#endif
1032#endif
99e372cd
RS
1033#ifdef SIGABRT
1034 signal (SIGABRT, fatal_error_signal);
1035#endif
1036#ifdef SIGHWE
1037 signal (SIGHWE, fatal_error_signal);
1038#endif
1039#ifdef SIGPRE
1040 signal (SIGPRE, fatal_error_signal);
1041#endif
1042#ifdef SIGORE
1043 signal (SIGORE, fatal_error_signal);
1044#endif
1045#ifdef SIGUME
1046 signal (SIGUME, fatal_error_signal);
1047#endif
1048#ifdef SIGDLK
1049 signal (SIGDLK, fatal_error_signal);
1050#endif
1051#ifdef SIGCPULIM
1052 signal (SIGCPULIM, fatal_error_signal);
1053#endif
a90538cb
JB
1054#ifdef SIGIOT
1055 /* This is missing on some systems - OS/2, for example. */
f927c5ae 1056 signal (SIGIOT, fatal_error_signal);
a90538cb 1057#endif
f927c5ae
JB
1058#ifdef SIGEMT
1059 signal (SIGEMT, fatal_error_signal);
1060#endif
1061 signal (SIGFPE, fatal_error_signal);
00eaaa32 1062#ifdef SIGBUS
f927c5ae 1063 signal (SIGBUS, fatal_error_signal);
00eaaa32 1064#endif
f927c5ae 1065 signal (SIGSEGV, fatal_error_signal);
00eaaa32 1066#ifdef SIGSYS
f927c5ae 1067 signal (SIGSYS, fatal_error_signal);
00eaaa32 1068#endif
f927c5ae
JB
1069 signal (SIGTERM, fatal_error_signal);
1070#ifdef SIGXCPU
1071 signal (SIGXCPU, fatal_error_signal);
1072#endif
1073#ifdef SIGXFSZ
1074 signal (SIGXFSZ, fatal_error_signal);
1075#endif /* SIGXFSZ */
1076
271c7b7c
RS
1077#ifdef SIGDANGER
1078 /* This just means available memory is getting low. */
1079 signal (SIGDANGER, memory_warning_signal);
1080#endif
1081
f927c5ae 1082#ifdef AIX
56e034fa
RS
1083/* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
1084 signal (SIGXCPU, fatal_error_signal);
0aef8561 1085#ifndef _I386
f927c5ae 1086 signal (SIGIOINT, fatal_error_signal);
0aef8561 1087#endif
f927c5ae
JB
1088 signal (SIGGRANT, fatal_error_signal);
1089 signal (SIGRETRACT, fatal_error_signal);
1090 signal (SIGSOUND, fatal_error_signal);
1091 signal (SIGMSG, fatal_error_signal);
1092#endif /* AIX */
1093 }
1094
1095 noninteractive1 = noninteractive;
1096
1097/* Perform basic initializations (not merely interning symbols) */
1098
1099 if (!initialized)
1100 {
1101 init_alloc_once ();
1102 init_obarray ();
1103 init_eval_once ();
270ce821
KH
1104 init_charset_once ();
1105 init_coding_once ();
f927c5ae 1106 init_syntax_once (); /* Create standard syntax table. */
270ce821 1107 init_category_once (); /* Create standard category table. */
f927c5ae
JB
1108 /* Must be done before init_buffer */
1109 init_casetab_once ();
1110 init_buffer_once (); /* Create buffer table and some buffers */
1111 init_minibuf_once (); /* Create list of minibuffers */
90d920b6
GM
1112 /* Must precede init_window_once */
1113
1114 /* Call syms_of_xfaces before init_window_once because that
1115 function creates Vterminal_frame. Termcap frames now use
1116 faces, and the face implementation uses some symbols as
1117 face names. */
90d920b6 1118 syms_of_xfaces ();
7a18af49
KR
1119 /* Call syms_of_keyboard before init_window_once because
1120 keyboard sets up symbols that include some face names that
1121 the X support will want to use. This can happen when
1122 CANNOT_DUMP is defined. */
1123 syms_of_keyboard ();
90d920b6 1124
1a578e9b 1125#ifdef macintosh
c1e279c2
AC
1126 /* init_window_once calls make_terminal_frame which on Mac OS
1127 creates a full-fledge output_mac type frame. This does not
1128 work correctly before syms_of_textprop, syms_of_macfns,
1129 syms_of_ccl, syms_of_fontset, syms_of_xterm, syms_of_search,
1130 syms_of_frame, x_term_init, and init_keyboard have already
1131 been called. */
1a578e9b
AC
1132 syms_of_textprop ();
1133 syms_of_macfns ();
1134 syms_of_ccl ();
1135 syms_of_fontset ();
1136 syms_of_macterm ();
1137 syms_of_macmenu ();
1138 syms_of_data ();
1139 syms_of_search ();
c1e279c2 1140 syms_of_frame ();
1a578e9b
AC
1141
1142 x_term_init ();
1143 init_keyboard ();
1144#endif
1145
f927c5ae 1146 init_window_once (); /* Init the window system */
680256a8 1147 init_fileio_once (); /* Must precede any path manipulation. */
f927c5ae
JB
1148 }
1149
1150 init_alloc ();
68c45bf0
PE
1151
1152 if (do_initial_setlocale)
1153 {
1154 fixup_locale ();
ca9c0567
PE
1155 Vsystem_messages_locale = Vprevious_system_messages_locale;
1156 Vsystem_time_locale = Vprevious_system_time_locale;
68c45bf0
PE
1157 }
1158
f927c5ae
JB
1159 init_eval ();
1160 init_data ();
ad067ea6
KH
1161#ifdef CLASH_DETECTION
1162 init_filelock ();;
1163#endif
ab5d0358 1164 init_atimer ();
7074fde6 1165 running_asynch_code = 0;
0e956009 1166
a08a816a
RS
1167 /* Handle --unibyte and the EMACS_UNIBYTE envvar,
1168 but not while dumping. */
1169 if (
1170#ifndef CANNOT_DUMP
1171 ! noninteractive || initialized
1172#else
1173 1
1174#endif
1175 )
1176 {
1177 int inhibit_unibyte = 0;
1178
1179 /* --multibyte overrides EMACS_UNIBYTE. */
1180 if (argmatch (argv, argc, "-no-unibyte", "--no-unibyte", 4, NULL, &skip_args)
1181 || argmatch (argv, argc, "-multibyte", "--multibyte", 4, NULL, &skip_args))
1182 inhibit_unibyte = 1;
1183
1184 /* --unibyte requests that we set up to do everything with single-byte
1185 buffers and strings. We need to handle this before calling
1186 init_lread, init_editfns and other places that generate Lisp strings
1187 from text in the environment. */
c0218e1b
DL
1188 /* Actually this shouldn't be needed as of 20.4 in a generally
1189 unibyte environment. As handa says, environment values
1190 aren't now decoded; also existing buffers are now made
1191 unibyte during startup if .emacs sets unibyte. Tested with
1192 8-bit data in environment variables and /etc/passwd, setting
1193 unibyte and Latin-1 in .emacs. -- Dave Love */
a08a816a
RS
1194 if (argmatch (argv, argc, "-unibyte", "--unibyte", 4, NULL, &skip_args)
1195 || argmatch (argv, argc, "-no-multibyte", "--no-multibyte", 4, NULL, &skip_args)
1196 || (getenv ("EMACS_UNIBYTE") && !inhibit_unibyte))
1197 {
1198 Lisp_Object old_log_max;
1199 Lisp_Object symbol, tail;
1200
1201 symbol = intern ("default-enable-multibyte-characters");
1202 Fset (symbol, Qnil);
1203
6524291c
KH
1204 if (initialized)
1205 {
1206 /* Erase pre-dump messages in *Messages* now so no abort. */
1207 old_log_max = Vmessage_log_max;
1208 XSETFASTINT (Vmessage_log_max, 0);
1209 message_dolog ("", 0, 1, 0);
1210 Vmessage_log_max = old_log_max;
1211 }
a08a816a 1212
3e75bb9e 1213 for (tail = Vbuffer_alist; CONSP (tail);
7539e11f 1214 tail = XCDR (tail))
a08a816a
RS
1215 {
1216 Lisp_Object buffer;
1217
7539e11f 1218 buffer = Fcdr (XCAR (tail));
a08a816a
RS
1219 /* Verify that all buffers are empty now, as they
1220 ought to be. */
1221 if (BUF_Z (XBUFFER (buffer)) > BUF_BEG (XBUFFER (buffer)))
1222 abort ();
1223 /* It is safe to do this crudely in an empty buffer. */
1224 XBUFFER (buffer)->enable_multibyte_characters = Qnil;
1225 }
1226 }
1227 }
1228
956e3c7e 1229 no_loadup
b96f9fb7 1230 = argmatch (argv, argc, "-nl", "--no-loadup", 6, NULL, &skip_args);
956e3c7e
RS
1231
1232
1233#ifdef HAVE_X_WINDOWS
1234 /* Stupid kludge to catch command-line display spec. We can't
1235 handle this argument entirely in window system dependent code
1236 because we don't even know which window system dependent code
1237 to run until we've recognized this argument. */
1238 {
1239 char *displayname = 0;
956e3c7e
RS
1240 int count_before = skip_args;
1241
1242 /* Skip any number of -d options, but only use the last one. */
1243 while (1)
1244 {
1245 int count_before_this = skip_args;
1246
1247 if (argmatch (argv, argc, "-d", "--display", 3, &displayname, &skip_args))
1248 display_arg = 1;
1249 else if (argmatch (argv, argc, "-display", 0, 3, &displayname, &skip_args))
1250 display_arg = 1;
1251 else
1252 break;
1253
1254 count_before = count_before_this;
1255 }
1256
1257 /* If we have the form --display=NAME,
1258 convert it into -d name.
1259 This requires inserting a new element into argv. */
1260 if (displayname != 0 && skip_args - count_before == 1)
1261 {
1262 char **new = (char **) xmalloc (sizeof (char *) * (argc + 2));
1263 int j;
1264
1265 for (j = 0; j < count_before + 1; j++)
1266 new[j] = argv[j];
1267 new[count_before + 1] = "-d";
1268 new[count_before + 2] = displayname;
1269 for (j = count_before + 2; j <argc; j++)
1270 new[j + 1] = argv[j];
1271 argv = new;
1272 argc++;
1273 }
1274 /* Change --display to -d, when its arg is separate. */
1275 else if (displayname != 0 && skip_args > count_before
1276 && argv[count_before + 1][1] == '-')
1277 argv[count_before + 1] = "-d";
1278
1279 /* Don't actually discard this arg. */
1280 skip_args = count_before;
1281 }
1282#endif
1283
1284 /* argmatch must not be used after here,
1285 except when bulding temacs
1286 because the -d argument has not been skipped in skip_args. */
1287
29b89fe0
RS
1288#ifdef MSDOS
1289 /* Call early 'cause init_environment needs it. */
1290 init_dosfns ();
1291 /* Set defaults for several environment variables. */
18198bb2
RS
1292 if (initialized)
1293 init_environment (argc, argv, skip_args);
1294 else
d1fc6752 1295 tzset ();
18198bb2 1296#endif /* MSDOS */
29b89fe0 1297
8ba50e1a
GV
1298#ifdef WINDOWSNT
1299 /* Initialize environment from registry settings. */
a3a58294 1300 init_environment (argv);
41f339d4 1301 init_ntproc (); /* must precede init_editfns */
8ba50e1a
GV
1302#endif
1303
0e956009
JB
1304 /* egetenv is a pretty low-level facility, which may get called in
1305 many circumstances; it seems flimsy to put off initializing it
1306 until calling init_callproc. */
1307 set_process_environment ();
93aed04d
RS
1308 /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
1309 if this is not done. Do it after set_process_environment so that we
1310 don't pollute Vprocess_environment. */
fa09a82d
DL
1311 /* Setting LANG here will defeat the startup locale processing... */
1312#ifdef AIX3_2
93aed04d
RS
1313 putenv ("LANG=C");
1314#endif
0e956009 1315
ace40a69
RS
1316 init_buffer (); /* Init default directory of main buffer */
1317
7928f0b5 1318 init_callproc_1 (); /* Must precede init_cmdargs and init_sys_modes. */
ace40a69 1319 init_cmdargs (argc, argv, skip_args); /* Must precede init_lread. */
380e25b8
RS
1320
1321 if (initialized)
1322 {
1323 /* Erase any pre-dump messages in the message log, to avoid confusion */
1324 Lisp_Object old_log_max;
1325 old_log_max = Vmessage_log_max;
1326 XSETFASTINT (Vmessage_log_max, 0);
f6fe7bb5 1327 message_dolog ("", 0, 1, 0);
380e25b8
RS
1328 Vmessage_log_max = old_log_max;
1329 }
1330
7928f0b5 1331 init_callproc (); /* Must follow init_cmdargs but not init_sys_modes. */
fb8e9847 1332 init_lread ();
f927c5ae 1333
8bb697c0
RS
1334 /* Intern the names of all standard functions and variables;
1335 define standard keys. */
f927c5ae
JB
1336
1337 if (!initialized)
1338 {
1339 /* The basic levels of Lisp must come first */
1340 /* And data must come first of all
1341 for the sake of symbols like error-message */
1a578e9b
AC
1342#ifndef macintosh
1343 /* Called before init_window_once for Mac OS. */
f927c5ae 1344 syms_of_data ();
1a578e9b 1345#endif
f927c5ae 1346 syms_of_alloc ();
fb8e9847 1347 syms_of_lread ();
f927c5ae
JB
1348 syms_of_print ();
1349 syms_of_eval ();
1350 syms_of_fns ();
f927c5ae 1351 syms_of_floatfns ();
f927c5ae
JB
1352
1353 syms_of_abbrev ();
1354 syms_of_buffer ();
1355 syms_of_bytecode ();
1356 syms_of_callint ();
1357 syms_of_casefiddle ();
1358 syms_of_casetab ();
1359 syms_of_callproc ();
270ce821 1360 syms_of_category ();
1a578e9b
AC
1361#ifndef macintosh
1362 /* Called before init_window_once for Mac OS. */
270ce821 1363 syms_of_ccl ();
1a578e9b 1364#endif
270ce821 1365 syms_of_charset ();
f927c5ae
JB
1366 syms_of_cmds ();
1367#ifndef NO_DIR_LIBRARY
1368 syms_of_dired ();
1369#endif /* not NO_DIR_LIBRARY */
1370 syms_of_display ();
1371 syms_of_doc ();
1372 syms_of_editfns ();
1373 syms_of_emacs ();
1374 syms_of_fileio ();
270ce821 1375 syms_of_coding (); /* This should be after syms_of_fileio. */
f927c5ae
JB
1376#ifdef CLASH_DETECTION
1377 syms_of_filelock ();
1378#endif /* CLASH_DETECTION */
1379 syms_of_indent ();
c2c5ed2c 1380 syms_of_insdel ();
f927c5ae
JB
1381 syms_of_keymap ();
1382 syms_of_macros ();
1383 syms_of_marker ();
1384 syms_of_minibuf ();
1385 syms_of_mocklisp ();
f927c5ae 1386 syms_of_process ();
1a578e9b
AC
1387#ifndef macintosh
1388 /* Called before init_window_once for Mac OS. */
f927c5ae 1389 syms_of_search ();
1cbd5d9d 1390 syms_of_frame ();
c1e279c2 1391#endif
f927c5ae 1392 syms_of_syntax ();
0d934e7b 1393 syms_of_term ();
f927c5ae 1394 syms_of_undo ();
90d920b6
GM
1395#ifdef HAVE_SOUND
1396 syms_of_sound ();
1397#endif
1a578e9b
AC
1398#ifndef macintosh
1399 /* Called before init_window_once for Mac OS. */
bef79ee4 1400 syms_of_textprop ();
1a578e9b 1401#endif
9d100795 1402 syms_of_composite ();
f927c5ae
JB
1403#ifdef VMS
1404 syms_of_vmsproc ();
1405#endif /* VMS */
05687c54
RS
1406#ifdef WINDOWSNT
1407 syms_of_ntproc ();
1408#endif /* WINDOWSNT */
f927c5ae
JB
1409 syms_of_window ();
1410 syms_of_xdisp ();
1411#ifdef HAVE_X_WINDOWS
72412588 1412 syms_of_xterm ();
f927c5ae 1413 syms_of_xfns ();
270ce821 1414 syms_of_fontset ();
72412588
JB
1415#ifdef HAVE_X11
1416 syms_of_xselect ();
1417#endif
f927c5ae
JB
1418#endif /* HAVE_X_WINDOWS */
1419
1e9c210b 1420#ifndef HAVE_NTGUI
1a578e9b 1421#ifndef macintosh
6c850f3c 1422 syms_of_xmenu ();
1e9c210b 1423#endif
1a578e9b 1424#endif
6c850f3c 1425
8ba50e1a 1426#ifdef HAVE_NTGUI
fbd6baed
GV
1427 syms_of_w32term ();
1428 syms_of_w32fns ();
fbd6baed
GV
1429 syms_of_w32select ();
1430 syms_of_w32menu ();
02062ac3 1431 syms_of_fontset ();
8ba50e1a
GV
1432#endif /* HAVE_NTGUI */
1433
f927c5ae
JB
1434#ifdef SYMS_SYSTEM
1435 SYMS_SYSTEM;
1436#endif
1437
1438#ifdef SYMS_MACHINE
1439 SYMS_MACHINE;
1440#endif
1441
1442 keys_of_casefiddle ();
1443 keys_of_cmds ();
1444 keys_of_buffer ();
1445 keys_of_keyboard ();
1446 keys_of_keymap ();
1447 keys_of_macros ();
1448 keys_of_minibuf ();
1449 keys_of_window ();
5e67fbc2 1450 keys_of_frame ();
f927c5ae
JB
1451 }
1452
8bb697c0
RS
1453 if (!noninteractive)
1454 {
1455#ifdef VMS
1456 init_vms_input ();/* init_display calls get_frame_size, that needs this */
1457#endif /* VMS */
1458 init_display (); /* Determine terminal type. init_sys_modes uses results */
1459 }
1a578e9b
AC
1460#ifndef macintosh
1461 /* Called before init_window_once for Mac OS. */
8bb697c0 1462 init_keyboard (); /* This too must precede init_sys_modes */
1a578e9b 1463#endif
8bb697c0
RS
1464#ifdef VMS
1465 init_vmsproc (); /* And this too. */
1466#endif /* VMS */
1467 init_sys_modes (); /* Init system terminal modes (RAW or CBREAK, etc.) */
90d920b6
GM
1468#ifdef HAVE_X_WINDOWS
1469 init_xfns ();
1470#endif /* HAVE_X_WINDOWS */
1471 init_fns ();
8bb697c0
RS
1472 init_xdisp ();
1473 init_macros ();
1474 init_editfns ();
8bb697c0 1475 init_floatfns ();
8bb697c0
RS
1476#ifdef VMS
1477 init_vmsfns ();
1478#endif /* VMS */
1479 init_process ();
90d920b6
GM
1480#ifdef HAVE_SOUND
1481 init_sound ();
1482#endif
75816372 1483 init_window ();
8bb697c0 1484
f927c5ae
JB
1485 if (!initialized)
1486 {
e2925360 1487 char *file;
303901fb 1488 /* Handle -l loadup, args passed by Makefile. */
df6530f8 1489 if (argmatch (argv, argc, "-l", "--load", 3, &file, &skip_args))
f927c5ae 1490 Vtop_level = Fcons (intern ("load"),
e2925360 1491 Fcons (build_string (file), Qnil));
f927c5ae
JB
1492#ifdef CANNOT_DUMP
1493 /* Unless next switch is -nl, load "loadup.el" first thing. */
956e3c7e 1494 if (! no_loadup)
f927c5ae
JB
1495 Vtop_level = Fcons (intern ("load"),
1496 Fcons (build_string ("loadup.el"), Qnil));
1497#endif /* CANNOT_DUMP */
1498 }
1499
93572b43
KH
1500 if (initialized)
1501 {
0269dedb
RS
1502#ifdef HAVE_TZSET
1503 {
1504 /* If the execution TZ happens to be the same as the dump TZ,
1505 change it to some other value and then change it back,
1506 to force the underlying implementation to reload the TZ info.
1507 This is needed on implementations that load TZ info from files,
1508 since the TZ file contents may differ between dump and execution. */
1509 char *tz = getenv ("TZ");
1510 if (tz && !strcmp (tz, dump_tz))
1511 {
1512 ++*tz;
1513 tzset ();
1514 --*tz;
1515 }
1516 }
1517#endif
93572b43
KH
1518 }
1519
37a0ae84
GM
1520 /* Set up for profiling. This is known to work on FreeBSD and
1521 GNU/Linux. It might work on some other systems too. Give it a
1522 try and tell us if it works on your system. To compile for
1523 profiling use something like `make CFLAGS="-pg -g -O -DPROFILING=1'. */
6a97197e 1524#if defined (__FreeBSD__) || defined (__linux)
e610ea43
RS
1525#ifdef PROFILING
1526 if (initialized)
1527 {
1528 extern void _mcleanup ();
1529 extern char etext;
90d920b6 1530 extern void safe_bcopy ();
67a5596f
GM
1531 extern void dump_opcode_frequencies ();
1532
e610ea43 1533 atexit (_mcleanup);
90d920b6
GM
1534 /* This uses safe_bcopy because that function comes first in the
1535 Emacs executable. It might be better to use something that
1536 gives the start of the text segment, but start_of_text is not
1537 defined on all systems now. */
1538 monstartup (safe_bcopy, &etext);
e610ea43
RS
1539 }
1540 else
1541 moncontrol (0);
1542#endif
1543#endif
1544
f927c5ae
JB
1545 initialized = 1;
1546
e7536cff
RS
1547#ifdef LOCALTIME_CACHE
1548 /* Some versions of localtime have a bug. They cache the value of the time
279cc2b8
JB
1549 zone rather than looking it up every time. Since localtime() is
1550 called to bolt the undumping time into the undumped emacs, this
afe9fae9
RS
1551 results in localtime ignoring the TZ environment variable.
1552 This flushes the new TZ value into localtime. */
1553 tzset ();
e7536cff 1554#endif /* defined (LOCALTIME_CACHE) */
279cc2b8 1555
f927c5ae
JB
1556 /* Enter editor command loop. This never returns. */
1557 Frecursive_edit ();
1558 /* NOTREACHED */
6bbd7a29 1559 return 0;
f927c5ae
JB
1560}
1561\f
081bef73
RS
1562/* Sort the args so we can find the most important ones
1563 at the beginning of argv. */
1564
1565/* First, here's a table of all the standard options. */
1566
1567struct standard_args
1568{
1569 char *name;
1570 char *longname;
1571 int priority;
1572 int nargs;
1573};
1574
1575struct standard_args standard_args[] =
1576{
05922407
RS
1577 { "-version", "--version", 150, 0 },
1578#ifdef HAVE_SHM
1579 { "-nl", "--no-shared-memory", 140, 0 },
1580#endif
2725719a 1581#ifdef VMS
05922407 1582 { "-map", "--map-data", 130, 0 },
2725719a 1583#endif
05922407
RS
1584 { "-t", "--terminal", 120, 1 },
1585 { "-nw", "--no-windows", 110, 0 },
1586 { "-batch", "--batch", 100, 0 },
1587 { "-help", "--help", 90, 0 },
956e3c7e
RS
1588 { "-no-unibyte", "--no-unibyte", 83, 0 },
1589 { "-multibyte", "--multibyte", 82, 0 },
1590 { "-unibyte", "--unibyte", 81, 0 },
1591 { "-no-multibyte", "--no-multibyte", 80, 0 },
05922407 1592#ifdef CANNOT_DUMP
956e3c7e 1593 { "-nl", "--no-loadup", 70, 0 },
05922407 1594#endif
956e3c7e
RS
1595 /* -d must come last before the options handled in startup.el. */
1596 { "-d", "--display", 60, 1 },
1597 { "-display", 0, 60, 1 },
05922407 1598 /* Now for the options handled in startup.el. */
081bef73
RS
1599 { "-q", "--no-init-file", 50, 0 },
1600 { "-no-init-file", 0, 50, 0 },
1601 { "-no-site-file", "--no-site-file", 40, 0 },
1602 { "-u", "--user", 30, 1 },
1603 { "-user", 0, 30, 1 },
1604 { "-debug-init", "--debug-init", 20, 0 },
adab4483
KH
1605 { "-i", "--icon-type", 15, 0 },
1606 { "-itype", 0, 15, 0 },
f2bc3538 1607 { "-iconic", "--iconic", 15, 0 },
081bef73
RS
1608 { "-bg", "--background-color", 10, 1 },
1609 { "-background", 0, 10, 1 },
1610 { "-fg", "--foreground-color", 10, 1 },
1611 { "-foreground", 0, 10, 1 },
1612 { "-bd", "--border-color", 10, 1 },
1613 { "-bw", "--border-width", 10, 1 },
1614 { "-ib", "--internal-border", 10, 1 },
1615 { "-ms", "--mouse-color", 10, 1 },
1616 { "-cr", "--cursor-color", 10, 1 },
1617 { "-fn", "--font", 10, 1 },
1618 { "-font", 0, 10, 1 },
1619 { "-g", "--geometry", 10, 1 },
1620 { "-geometry", 0, 10, 1 },
1621 { "-T", "--title", 10, 1 },
ae63ae52 1622 { "-title", 0, 10, 1 },
081bef73
RS
1623 { "-name", "--name", 10, 1 },
1624 { "-xrm", "--xrm", 10, 1 },
fcdeb5d9
RS
1625 { "-r", "--reverse-video", 5, 0 },
1626 { "-rv", 0, 5, 0 },
1627 { "-reverse", 0, 5, 0 },
ae63ae52 1628 { "-hb", "--horizontal-scroll-bars", 5, 0 },
fcdeb5d9 1629 { "-vb", "--vertical-scroll-bars", 5, 0 },
fcdeb5d9
RS
1630 /* These have the same priority as ordinary file name args,
1631 so they are not reordered with respect to those. */
4af9e0b3
RS
1632 { "-L", "--directory", 0, 1 },
1633 { "-directory", 0, 0, 1 },
fcdeb5d9
RS
1634 { "-l", "--load", 0, 1 },
1635 { "-load", 0, 0, 1 },
1636 { "-f", "--funcall", 0, 1 },
1637 { "-funcall", 0, 0, 1 },
575985b1 1638 { "-eval", "--eval", 0, 1 },
67a5596f 1639 { "-execute", "--execute", 0, 1 },
2e13f8e9
RS
1640 { "-find-file", "--find-file", 0, 1 },
1641 { "-visit", "--visit", 0, 1 },
67a5596f 1642 { "-file", "--file", 0, 1 },
fcdeb5d9 1643 { "-insert", "--insert", 0, 1 },
f2bc3538 1644 /* This should be processed after ordinary file name args and the like. */
fcdeb5d9 1645 { "-kill", "--kill", -10, 0 },
081bef73
RS
1646};
1647
1648/* Reorder the elements of ARGV (assumed to have ARGC elements)
1649 so that the highest priority ones come first.
1650 Do not change the order of elements of equal priority.
956e3c7e
RS
1651 If an option takes an argument, keep it and its argument together.
1652
1653 If an option that takes no argument appears more
1654 than once, eliminate all but one copy of it. */
081bef73
RS
1655
1656static void
1657sort_args (argc, argv)
1658 int argc;
1659 char **argv;
1660{
1661 char **new = (char **) xmalloc (sizeof (char *) * argc);
1662 /* For each element of argv,
1663 the corresponding element of options is:
1664 0 for an option that takes no arguments,
1665 1 for an option that takes one argument, etc.
1666 -1 for an ordinary non-option argument. */
6dad9359 1667 int *options = (int *) xmalloc (sizeof (int) * argc);
081bef73
RS
1668 int *priority = (int *) xmalloc (sizeof (int) * argc);
1669 int to = 1;
956e3c7e 1670 int incoming_used = 1;
081bef73
RS
1671 int from;
1672 int i;
1673
1674 /* Categorize all the options,
1675 and figure out which argv elts are option arguments. */
1676 for (from = 1; from < argc; from++)
1677 {
1678 options[from] = -1;
fcdeb5d9 1679 priority[from] = 0;
081bef73
RS
1680 if (argv[from][0] == '-')
1681 {
1682 int match, thislen;
1683 char *equals;
1684
c96f26f4
RS
1685 /* If we have found "--", don't consider
1686 any more arguments as options. */
249443b6 1687 if (argv[from][1] == '-' && argv[from][2] == 0)
c96f26f4
RS
1688 {
1689 /* Leave the "--", and everything following it, at the end. */
1690 for (; from < argc; from++)
1691 {
1692 priority[from] = -100;
1693 options[from] = -1;
1694 }
1695 break;
1696 }
1697
081bef73
RS
1698 /* Look for a match with a known old-fashioned option. */
1699 for (i = 0; i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1700 if (!strcmp (argv[from], standard_args[i].name))
1701 {
1702 options[from] = standard_args[i].nargs;
1703 priority[from] = standard_args[i].priority;
fd76ec52
RS
1704 if (from + standard_args[i].nargs >= argc)
1705 fatal ("Option `%s' requires an argument\n", argv[from]);
081bef73
RS
1706 from += standard_args[i].nargs;
1707 goto done;
1708 }
1709
1710 /* Look for a match with a known long option.
1711 MATCH is -1 if no match so far, -2 if two or more matches so far,
1712 >= 0 (the table index of the match) if just one match so far. */
1713 if (argv[from][1] == '-')
1714 {
1715 match = -1;
1716 thislen = strlen (argv[from]);
1717 equals = index (argv[from], '=');
1718 if (equals != 0)
1719 thislen = equals - argv[from];
1720
f609ef57
KH
1721 for (i = 0;
1722 i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1723 if (standard_args[i].longname
1724 && !strncmp (argv[from], standard_args[i].longname,
1725 thislen))
081bef73
RS
1726 {
1727 if (match == -1)
1728 match = i;
1729 else
1730 match = -2;
1731 }
1732
1733 /* If we found exactly one match, use that. */
1734 if (match >= 0)
1735 {
1736 options[from] = standard_args[match].nargs;
1737 priority[from] = standard_args[match].priority;
1738 /* If --OPTION=VALUE syntax is used,
1739 this option uses just one argv element. */
1740 if (equals != 0)
1741 options[from] = 0;
fd76ec52
RS
1742 if (from + options[from] >= argc)
1743 fatal ("Option `%s' requires an argument\n", argv[from]);
081bef73
RS
1744 from += options[from];
1745 }
1746 }
1747 done: ;
1748 }
1749 }
1750
1751 /* Copy the arguments, in order of decreasing priority, to NEW. */
1752 new[0] = argv[0];
956e3c7e 1753 while (incoming_used < argc)
081bef73
RS
1754 {
1755 int best = -1;
2c70c992 1756 int best_priority = -9999;
081bef73
RS
1757
1758 /* Find the highest priority remaining option.
1759 If several have equal priority, take the first of them. */
1760 for (from = 1; from < argc; from++)
1761 {
1762 if (argv[from] != 0 && priority[from] > best_priority)
1763 {
1764 best_priority = priority[from];
1765 best = from;
1766 }
1767 /* Skip option arguments--they are tied to the options. */
1768 if (options[from] > 0)
1769 from += options[from];
1770 }
1771
1772 if (best < 0)
1773 abort ();
1774
956e3c7e
RS
1775 /* Copy the highest priority remaining option, with its args, to NEW.
1776 Unless it is a duplicate of the previous one. */
1777 if (! (options[best] == 0
1778 && ! strcmp (new[to - 1], argv[best])))
1779 {
1780 new[to++] = argv[best];
1781 for (i = 0; i < options[best]; i++)
1782 new[to++] = argv[best + i + 1];
1783 }
1784
1785 incoming_used += 1 + (options[best] > 0 ? options[best] : 0);
081bef73
RS
1786
1787 /* Clear out this option in ARGV. */
1788 argv[best] = 0;
1789 for (i = 0; i < options[best]; i++)
1790 argv[best + i + 1] = 0;
1791 }
1792
81b7af72
RS
1793 /* If duplicate options were deleted, fill up extra space with null ptrs. */
1794 while (to < argc)
1795 new[to++] = 0;
1796
6dad9359 1797 bcopy (new, argv, sizeof (char *) * argc);
0bf591da
RS
1798
1799 free (options);
1800 free (new);
1801 free (priority);
081bef73
RS
1802}
1803\f
f927c5ae 1804DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
edc8ae07 1805 "Exit the Emacs job and kill it.\n\
f927c5ae
JB
1806If ARG is an integer, return ARG as the exit program code.\n\
1807If ARG is a string, stuff it as keyboard input.\n\n\
1808The value of `kill-emacs-hook', if not void,\n\
1809is a list of functions (of no args),\n\
1810all of which are called before Emacs is actually killed.")
1811 (arg)
1812 Lisp_Object arg;
1813{
f927c5ae
JB
1814 struct gcpro gcpro1;
1815
1816 GCPRO1 (arg);
1817
1818 if (feof (stdin))
1819 arg = Qt;
1820
2447c626 1821 if (!NILP (Vrun_hooks) && !noninteractive)
f927c5ae
JB
1822 call1 (Vrun_hooks, intern ("kill-emacs-hook"));
1823
f927c5ae
JB
1824 UNGCPRO;
1825
1826/* Is it really necessary to do this deassign
1827 when we are going to exit anyway? */
1828/* #ifdef VMS
1829 stop_vms_input ();
1830 #endif */
40be253a 1831
d0068e25 1832 shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
40be253a 1833
58545838
KH
1834 /* If we have an auto-save list file,
1835 kill it because we are exiting Emacs deliberately (not crashing).
1836 Do it after shut_down_emacs, which does an auto-save. */
1837 if (STRINGP (Vauto_save_list_file_name))
1838 unlink (XSTRING (Vauto_save_list_file_name)->data);
1839
55ccc0b3 1840 exit (INTEGERP (arg) ? XINT (arg)
f927c5ae
JB
1841#ifdef VMS
1842 : 1
1843#else
1844 : 0
1845#endif
1846 );
1847 /* NOTREACHED */
1848}
40be253a
JB
1849
1850
1851/* Perform an orderly shutdown of Emacs. Autosave any modified
1852 buffers, kill any child processes, clean up the terminal modes (if
1853 we're in the foreground), and other stuff like that. Don't perform
1854 any redisplay; this may be called when Emacs is shutting down in
1855 the background, or after its X connection has died.
1856
1857 If SIG is a signal number, print a message for it.
1858
1859 This is called by fatal signal handlers, X protocol error handlers,
1860 and Fkill_emacs. */
f7ab4e3d 1861
40be253a 1862void
f7ab4e3d 1863shut_down_emacs (sig, no_x, stuff)
41423a80 1864 int sig, no_x;
f7ab4e3d 1865 Lisp_Object stuff;
40be253a 1866{
829d872b
RS
1867 /* Prevent running of hooks from now on. */
1868 Vrun_hooks = Qnil;
1869
40be253a
JB
1870 /* If we are controlling the terminal, reset terminal modes */
1871#ifdef EMACS_HAVE_TTY_PGRP
1872 {
d04d81d2
RS
1873 int pgrp = EMACS_GETPGRP (0);
1874
40be253a
JB
1875 int tpgrp;
1876 if (EMACS_GET_TTY_PGRP (0, &tpgrp) != -1
5a570e37 1877 && tpgrp == pgrp)
40be253a
JB
1878 {
1879 fflush (stdout);
1880 reset_sys_modes ();
1881 if (sig && sig != SIGTERM)
1882 fprintf (stderr, "Fatal error (%d).", sig);
1883 }
1884 }
1885#else
1886 fflush (stdout);
1887 reset_sys_modes ();
1888#endif
1889
f7ab4e3d
RS
1890 stuff_buffered_input (stuff);
1891
40be253a
JB
1892 kill_buffer_processes (Qnil);
1893 Fdo_auto_save (Qt, Qnil);
1894
1895#ifdef CLASH_DETECTION
1896 unlock_all_files ();
1897#endif
1898
1899#ifdef VMS
1900 kill_vms_processes ();
1901#endif
1902
5e7f8733 1903#if 0 /* This triggers a bug in XCloseDisplay and is not needed. */
41423a80 1904#ifdef HAVE_X_WINDOWS
f7511647
RS
1905 /* It's not safe to call intern here. Maybe we are crashing. */
1906 if (!noninteractive && SYMBOLP (Vwindow_system)
1907 && XSYMBOL (Vwindow_system)->name->size == 1
1908 && XSYMBOL (Vwindow_system)->name->data[0] == 'x'
1909 && ! no_x)
41423a80
RS
1910 Fx_close_current_connection ();
1911#endif /* HAVE_X_WINDOWS */
5e7f8733 1912#endif
41423a80 1913
40be253a
JB
1914#ifdef SIGIO
1915 /* There is a tendency for a SIGIO signal to arrive within exit,
1916 and cause a SIGHUP because the input descriptor is already closed. */
1917 unrequest_sigio ();
1918 signal (SIGIO, SIG_IGN);
1919#endif
41f339d4
RS
1920
1921#ifdef WINDOWSNT
1922 term_ntproc ();
1923#endif
d546e578 1924
cd8d4168
GM
1925 /* Do this only if terminating normally, we want glyph matrices
1926 etc. in a core dump. */
200f868e 1927 if (sig == 0 || sig == SIGTERM)
cd8d4168
GM
1928 {
1929 check_glyph_memory ();
1930 check_message_stack ();
1931 }
90d920b6 1932
d546e578
EZ
1933#ifdef MSDOS
1934 dos_cleanup ();
1935#endif
40be253a
JB
1936}
1937
1938
f927c5ae
JB
1939\f
1940#ifndef CANNOT_DUMP
f927c5ae
JB
1941
1942#ifdef HAVE_SHM
1943
1944DEFUN ("dump-emacs-data", Fdump_emacs_data, Sdump_emacs_data, 1, 1, 0,
1945 "Dump current state of Emacs into data file FILENAME.\n\
1946This function exists on systems that use HAVE_SHM.")
c9aae259
EN
1947 (filename)
1948 Lisp_Object filename;
f927c5ae 1949{
55697f5b 1950 extern char my_edata[];
f927c5ae 1951 Lisp_Object tem;
f927c5ae 1952
4fab758d 1953 check_pure_size ();
c9aae259
EN
1954 CHECK_STRING (filename, 0);
1955 filename = Fexpand_file_name (filename, Qnil);
f927c5ae
JB
1956
1957 tem = Vpurify_flag;
1958 Vpurify_flag = Qnil;
1959
1960 fflush (stdout);
1961 /* Tell malloc where start of impure now is */
1962 /* Also arrange for warnings when nearly out of space. */
1963#ifndef SYSTEM_MALLOC
1090a161 1964 memory_warnings (my_edata, malloc_warning);
f927c5ae 1965#endif
c9aae259 1966 map_out_data (XSTRING (filename)->data);
f927c5ae
JB
1967
1968 Vpurify_flag = tem;
1969
1970 return Qnil;
1971}
1972
1973#else /* not HAVE_SHM */
1974
1975DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
1976 "Dump current state of Emacs into executable file FILENAME.\n\
1977Take symbols from SYMFILE (presumably the file you executed to run Emacs).\n\
1978This is used in the file `loadup.el' when building Emacs.\n\
1979\n\
87a98b1a 1980You must run Emacs in batch mode in order to dump it.")
c9aae259
EN
1981 (filename, symfile)
1982 Lisp_Object filename, symfile;
f927c5ae 1983{
55697f5b 1984 extern char my_edata[];
f927c5ae 1985 Lisp_Object tem;
1b7ddf4f 1986 Lisp_Object symbol;
4fab758d
GM
1987 int count = BINDING_STACK_SIZE ();
1988
1989 check_pure_size ();
f927c5ae 1990
87a98b1a
RS
1991 if (! noninteractive)
1992 error ("Dumping Emacs works only in batch mode");
1993
1b7ddf4f
RS
1994 /* Bind `command-line-processed' to nil before dumping,
1995 so that the dumped Emacs will process its command line
1996 and set up to work with X windows if appropriate. */
1997 symbol = intern ("command-line-process");
1998 specbind (symbol, Qnil);
1999
c9aae259
EN
2000 CHECK_STRING (filename, 0);
2001 filename = Fexpand_file_name (filename, Qnil);
2002 if (!NILP (symfile))
f927c5ae 2003 {
c9aae259
EN
2004 CHECK_STRING (symfile, 0);
2005 if (XSTRING (symfile)->size)
2006 symfile = Fexpand_file_name (symfile, Qnil);
f927c5ae
JB
2007 }
2008
2009 tem = Vpurify_flag;
2010 Vpurify_flag = Qnil;
2011
0269dedb
RS
2012#ifdef HAVE_TZSET
2013 set_time_zone_rule (dump_tz);
2014#ifndef LOCALTIME_CACHE
2015 /* Force a tz reload, since set_time_zone_rule doesn't. */
2016 tzset ();
2017#endif
2018#endif
2019
f927c5ae
JB
2020 fflush (stdout);
2021#ifdef VMS
c9aae259 2022 mapout_data (XSTRING (filename)->data);
f927c5ae
JB
2023#else
2024 /* Tell malloc where start of impure now is */
2025 /* Also arrange for warnings when nearly out of space. */
2026#ifndef SYSTEM_MALLOC
cc5f52cb
RS
2027#ifndef WINDOWSNT
2028 /* On Windows, this was done before dumping, and that once suffices.
2029 Meanwhile, my_edata is not valid on Windows. */
cb37cf78 2030 memory_warnings (my_edata, malloc_warning);
cc5f52cb 2031#endif /* not WINDOWSNT */
15aaf1b5
RS
2032#endif
2033#ifdef DOUG_LEA_MALLOC
2034 malloc_state_ptr = malloc_get_state ();
f927c5ae 2035#endif
2b7377ca 2036
a74c5ec1 2037#ifdef USE_MMAP_FOR_BUFFERS
2b7377ca
GM
2038 mmap_set_vars (0);
2039#endif
c9aae259
EN
2040 unexec (XSTRING (filename)->data,
2041 !NILP (symfile) ? XSTRING (symfile)->data : 0, my_edata, 0, 0);
a74c5ec1 2042#ifdef USE_MMAP_FOR_BUFFERS
2b7377ca
GM
2043 mmap_set_vars (1);
2044#endif
15aaf1b5
RS
2045#ifdef DOUG_LEA_MALLOC
2046 free (malloc_state_ptr);
2047#endif
f927c5ae
JB
2048#endif /* not VMS */
2049
2050 Vpurify_flag = tem;
2051
1b7ddf4f 2052 return unbind_to (count, Qnil);
f927c5ae
JB
2053}
2054
2055#endif /* not HAVE_SHM */
2056
2057#endif /* not CANNOT_DUMP */
2058\f
68c45bf0
PE
2059#if HAVE_SETLOCALE
2060/* Recover from setlocale (LC_ALL, ""). */
2061void
2062fixup_locale ()
2063{
68c45bf0
PE
2064 /* The Emacs Lisp reader needs LC_NUMERIC to be "C",
2065 so that numbers are read and printed properly for Emacs Lisp. */
2066 setlocale (LC_NUMERIC, "C");
68c45bf0
PE
2067}
2068
0c8559bb
PE
2069/* Set system locale CATEGORY, with previous locale *PLOCALE, to
2070 DESIRED_LOCALE. */
68c45bf0
PE
2071static void
2072synchronize_locale (category, plocale, desired_locale)
2073 int category;
2074 Lisp_Object *plocale;
2075 Lisp_Object desired_locale;
2076{
0c8559bb
PE
2077 if (! EQ (*plocale, desired_locale))
2078 {
2079 *plocale = desired_locale;
2080 setlocale (category, (STRINGP (desired_locale)
59c65f1e 2081 ? (char *)(XSTRING (desired_locale)->data)
0c8559bb
PE
2082 : ""));
2083 }
68c45bf0
PE
2084}
2085
ca9c0567 2086/* Set system time locale to match Vsystem_time_locale, if possible. */
68c45bf0 2087void
ca9c0567 2088synchronize_system_time_locale ()
68c45bf0 2089{
ca9c0567
PE
2090 synchronize_locale (LC_TIME, &Vprevious_system_time_locale,
2091 Vsystem_time_locale);
68c45bf0
PE
2092}
2093
ca9c0567
PE
2094/* Set system messages locale to match Vsystem_messages_locale, if
2095 possible. */
68c45bf0 2096void
ca9c0567 2097synchronize_system_messages_locale ()
68c45bf0
PE
2098{
2099#ifdef LC_MESSAGES
ca9c0567
PE
2100 synchronize_locale (LC_MESSAGES, &Vprevious_system_messages_locale,
2101 Vsystem_messages_locale);
68c45bf0
PE
2102#endif
2103}
2104#endif /* HAVE_SETLOCALE */
2105\f
4b163808 2106#ifndef SEPCHAR
f927c5ae
JB
2107#define SEPCHAR ':'
2108#endif
2109
2110Lisp_Object
2111decode_env_path (evarname, defalt)
2112 char *evarname, *defalt;
2113{
2114 register char *path, *p;
213d0b1f 2115 Lisp_Object lpath, element, tem;
f927c5ae 2116
2447c626
JB
2117 /* It's okay to use getenv here, because this function is only used
2118 to initialize variables when Emacs starts up, and isn't called
2119 after that. */
e065a56e
JB
2120 if (evarname != 0)
2121 path = (char *) getenv (evarname);
2122 else
2123 path = 0;
f927c5ae
JB
2124 if (!path)
2125 path = defalt;
6a30e6d6
RS
2126#ifdef DOS_NT
2127 /* Ensure values from the environment use the proper directory separator. */
2128 if (path)
2129 {
2130 p = alloca (strlen (path) + 1);
2131 strcpy (p, path);
2132 path = p;
2133
2134 if ('/' == DIRECTORY_SEP)
2135 dostounix_filename (path);
2136 else
2137 unixtodos_filename (path);
2138 }
2139#endif
f927c5ae
JB
2140 lpath = Qnil;
2141 while (1)
2142 {
2143 p = index (path, SEPCHAR);
2144 if (!p) p = path + strlen (path);
213d0b1f
RS
2145 element = (p - path ? make_string (path, p - path)
2146 : build_string ("."));
2147
2148 /* Add /: to the front of the name
2149 if it would otherwise be treated as magic. */
2150 tem = Ffind_file_name_handler (element, Qt);
2151 if (! NILP (tem))
2152 element = concat2 (build_string ("/:"), element);
2153
2154 lpath = Fcons (element, lpath);
f927c5ae
JB
2155 if (*p)
2156 path = p + 1;
2157 else
2158 break;
2159 }
2160 return Fnreverse (lpath);
2161}
2162
dfcf069d 2163void
f927c5ae
JB
2164syms_of_emacs ()
2165{
213d0b1f
RS
2166 Qfile_name_handler_alist = intern ("file-name-handler-alist");
2167 staticpro (&Qfile_name_handler_alist);
2168
83591e66 2169#ifndef CANNOT_DUMP
f927c5ae
JB
2170#ifdef HAVE_SHM
2171 defsubr (&Sdump_emacs_data);
2172#else
2173 defsubr (&Sdump_emacs);
83591e66 2174#endif
f927c5ae
JB
2175#endif
2176
2177 defsubr (&Skill_emacs);
2178
59653951 2179 defsubr (&Sinvocation_name);
ace40a69 2180 defsubr (&Sinvocation_directory);
59653951 2181
f927c5ae
JB
2182 DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
2183 "Args passed by shell to Emacs, as a list of strings.");
2184
2185 DEFVAR_LISP ("system-type", &Vsystem_type,
2186 "Value is symbol indicating type of operating system you are using.");
2187 Vsystem_type = intern (SYSTEM_TYPE);
2188
271c7b7c 2189 DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
69bf528e
AI
2190 "Value is string indicating configuration Emacs was built for.\n\
2191On MS-Windows, the value reflects the OS flavor and version on which\n\
2192Emacs is running.");
f7511647 2193 Vsystem_configuration = build_string (EMACS_CONFIGURATION);
271c7b7c 2194
f0fc0b1a
KH
2195 DEFVAR_LISP ("system-configuration-options", &Vsystem_configuration_options,
2196 "String containing the configuration options Emacs was built with.");
2197 Vsystem_configuration_options = build_string (EMACS_CONFIG_OPTIONS);
2198
f927c5ae
JB
2199 DEFVAR_BOOL ("noninteractive", &noninteractive1,
2200 "Non-nil means Emacs is running without interactive terminal.");
e5d77022 2201
e5d77022 2202 DEFVAR_LISP ("kill-emacs-hook", &Vkill_emacs_hook,
edc8ae07
JB
2203 "Hook to be run whenever kill-emacs is called.\n\
2204Since kill-emacs may be invoked when the terminal is disconnected (or\n\
2205in other similar situations), functions placed on this hook should not\n\
9f232a5d
RS
2206expect to be able to interact with the user. To ask for confirmation,\n\
2207see `kill-emacs-query-functions' instead.");
edc8ae07 2208 Vkill_emacs_hook = Qnil;
3005da00 2209
7317d9e8
RS
2210#ifdef SIGUSR1
2211 DEFVAR_LISP ("signal-USR1-hook", &Vsignal_USR1_hook,
3e75bb9e 2212 "Hook to be run whenever emacs receives a USR1 signal");
7317d9e8
RS
2213 Vsignal_USR1_hook = Qnil;
2214#ifdef SIGUSR2
2215 DEFVAR_LISP ("signal-USR2-hook", &Vsignal_USR2_hook,
3e75bb9e 2216 "Hook to be run whenever emacs receives a USR2 signal");
7317d9e8
RS
2217 Vsignal_USR2_hook = Qnil;
2218#endif
2219#endif
2220
2221
3005da00
RS
2222 DEFVAR_INT ("emacs-priority", &emacs_priority,
2223 "Priority for Emacs to run at.\n\
2224This value is effective only if set before Emacs is dumped,\n\
2225and only if the Emacs executable is installed with setuid to permit\n\
621ecf99 2226it to change priority. (Emacs sets its uid back to the real uid.)\n\
a422068f 2227Currently, you need to define SET_EMACS_PRIORITY in `config.h'\n\
e7a9e1c3
RS
2228before you compile Emacs, to enable the code for this feature.");
2229 emacs_priority = 0;
074a066b
GV
2230
2231 DEFVAR_LISP ("path-separator", &Vpath_separator,
2232 "The directory separator in search paths, as a string.");
2233 {
2234 char c = SEPCHAR;
2235 Vpath_separator = make_string (&c, 1);
2236 }
59653951 2237
f67de86f
RS
2238 DEFVAR_LISP ("invocation-name", &Vinvocation_name,
2239 "The program name that was used to run Emacs.\n\
2240Any directory names are omitted.");
2241
2242 DEFVAR_LISP ("invocation-directory", &Vinvocation_directory,
2243 "The directory in which the Emacs executable was found, to run it.\n\
2244The value is nil if that directory's name is not known.");
2245
2246 DEFVAR_LISP ("installation-directory", &Vinstallation_directory,
2247 "A directory within which to look for the `lib-src' and `etc' directories.\n\
2248This is non-nil when we can't find those directories in their standard\n\
2249installed locations, but we can find them\n\
2250near where the Emacs executable was found.");
07f4d123 2251 Vinstallation_directory = Qnil;
68c45bf0 2252
ca9c0567 2253 DEFVAR_LISP ("system-messages-locale", &Vsystem_messages_locale,
68c45bf0 2254 "System locale for messages.");
ca9c0567 2255 Vsystem_messages_locale = Qnil;
68c45bf0 2256
ca9c0567
PE
2257 DEFVAR_LISP ("previous-system-messages-locale",
2258 &Vprevious_system_messages_locale,
68c45bf0 2259 "Most recently used system locale for messages.");
ca9c0567 2260 Vprevious_system_messages_locale = Qnil;
68c45bf0 2261
ca9c0567 2262 DEFVAR_LISP ("system-time-locale", &Vsystem_time_locale,
68c45bf0 2263 "System locale for time.");
ca9c0567 2264 Vsystem_time_locale = Qnil;
68c45bf0 2265
ca9c0567 2266 DEFVAR_LISP ("previous-system-time-locale", &Vprevious_system_time_locale,
68c45bf0 2267 "Most recently used system locale for time.");
ca9c0567 2268 Vprevious_system_time_locale = Qnil;
f927c5ae 2269}