Fix handling of fatal signals and exceptions on MS-Windows.
[bpt/emacs.git] / src / emacs.c
CommitLineData
f927c5ae 1/* Fully extensible Emacs, running on Unix, intended for GNU.
bd6bc222 2
acaf905b 3Copyright (C) 1985-1987, 1993-1995, 1997-1999, 2001-2012
bd6bc222 4 Free Software Foundation, Inc.
f927c5ae
JB
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
f927c5ae 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
f927c5ae
JB
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
f927c5ae
JB
20
21
68c45bf0 22#include <config.h>
f927c5ae 23#include <errno.h>
f927c5ae
JB
24#include <stdio.h>
25
26#include <sys/types.h>
27#include <sys/file.h>
cda4a5fd 28#include <unistd.h>
cda4a5fd 29
01108e3f
PE
30#include <ignore-value.h>
31
0898ca10
JB
32#include "lisp.h"
33
17a2cbbd
DC
34#ifdef HAVE_WINDOW_SYSTEM
35#include TERM_HEADER
36#endif /* HAVE_WINDOW_SYSTEM */
37
edd3ff1d
JR
38#ifdef WINDOWSNT
39#include <fcntl.h>
52c7f9ee
JR
40#include <windows.h> /* just for w32.h */
41#include "w32.h"
c68a2829 42#include "w32heap.h" /* for prototype of sbrk */
edd3ff1d
JR
43#endif
44
edfda783
AR
45#ifdef NS_IMPL_GNUSTEP
46/* At least under Debian, GSConfig is in a subdirectory. --Stef */
47#include <GNUstepBase/GSConfig.h>
48#endif
49
f927c5ae 50#include "commands.h"
bef79ee4 51#include "intervals.h"
e5560ff7 52#include "character.h"
a08a816a 53#include "buffer.h"
3f6abfd7 54#include "window.h"
f927c5ae 55
edc8ae07 56#include "systty.h"
4d7e6e51 57#include "atimer.h"
6c3a4e9d 58#include "blockinput.h"
8090eb09 59#include "syssignal.h"
6c362a8b 60#include "process.h"
428a555e 61#include "frame.h"
5bda49c6 62#include "termhooks.h"
dfcf069d 63#include "keyboard.h"
e35f6ff7 64#include "keymap.h"
a41f8bed 65
8af55556
TZ
66#ifdef HAVE_GNUTLS
67#include "gnutls.h"
68#endif
69
9e4bf381
PE
70#if (defined PROFILING \
71 && (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__))
72# include <sys/gmon.h>
73extern void moncontrol (int mode);
74#endif
75
68c45bf0
PE
76#ifdef HAVE_SETLOCALE
77#include <locale.h>
78#endif
79
53c58b5d
RS
80#ifdef HAVE_SETRLIMIT
81#include <sys/time.h>
82#include <sys/resource.h>
83#endif
84
dede2792
JD
85#ifdef HAVE_PERSONALITY_LINUX32
86#include <sys/personality.h>
87#endif
88
f927c5ae
JB
89#ifndef O_RDWR
90#define O_RDWR 2
91#endif
92
16fab143 93static const char emacs_version[] = VERSION;
78f83752 94static const char emacs_copyright[] = COPYRIGHT;
8c5ff6dd 95
7e9fa161
JB
96/* Empty lisp strings. To avoid having to build any others. */
97Lisp_Object empty_unibyte_string, empty_multibyte_string;
f9a6326d 98
1882aa38
PE
99/* Set after Emacs has started up the first time.
100 Prevents reinitialization of the Lisp world and keymaps
101 on subsequent starts. */
102bool initialized;
f927c5ae 103
4393663b
JD
104#ifdef DARWIN_OS
105extern void unexec_init_emacs_zone (void);
106#endif
107
15aaf1b5
RS
108#ifdef DOUG_LEA_MALLOC
109/* Preserves a pointer to the memory allocated that copies that
110 static data inside glibc's malloc. */
35f08c38 111static void *malloc_state_ptr;
15aaf1b5 112/* From glibc, a routine that returns a copy of the malloc internal state. */
dd4c5104 113extern void *malloc_get_state (void);
15aaf1b5 114/* From glibc, a routine that overwrites the malloc internal state. */
dd4c5104 115extern int malloc_set_state (void*);
1882aa38 116/* True if the MALLOC_CHECK_ environment variable was set while
7c9cd446 117 dumping. Used to work around a bug in glibc's malloc. */
1882aa38 118static bool malloc_using_checking;
15aaf1b5
RS
119#endif
120
213d0b1f
RS
121Lisp_Object Qfile_name_handler_alist;
122
9dc3366b
GM
123Lisp_Object Qrisky_local_variable;
124
6c07aac2
AS
125Lisp_Object Qkill_emacs;
126
1882aa38 127/* If true, Emacs should not attempt to use a window-specific code,
7db35a48 128 but instead should use the virtual terminal under which it was started. */
1882aa38 129bool inhibit_window_system;
f927c5ae 130
1882aa38 131/* If true, a filter or a sentinel is running. Tested to save the match
7db35a48 132 data on the first attempt to change it inside asynchronous code. */
1882aa38 133bool running_asynch_code;
7074fde6 134
5e617bc2 135#if defined (HAVE_X_WINDOWS) || defined (HAVE_NS)
1882aa38
PE
136/* If true, -d was specified, meaning we're using some window system. */
137bool display_arg;
f927c5ae
JB
138#endif
139
140/* An address near the bottom of the stack.
141 Tells GC how to save a copy of the stack. */
142char *stack_bottom;
143
2949f33b 144#if defined (DOUG_LEA_MALLOC) || defined (GNU_LINUX)
dede2792
JD
145/* The address where the heap starts (from the first sbrk (0) call). */
146static void *my_heap_start;
2949f33b 147#endif
dede2792 148
b312a492 149#ifdef GNU_LINUX
dede2792 150/* The gap between BSS end and heap start as far as we can tell. */
b312a492
PE
151static uprintmax_t heap_bss_diff;
152#endif
dede2792 153
1882aa38
PE
154/* True means running Emacs without interactive terminal. */
155bool noninteractive;
f927c5ae 156
1882aa38
PE
157/* True means remove site-lisp directories from load-path. */
158bool no_site_lisp;
66b7b0fe 159
4ff029f6
DN
160/* Name for the server started by the daemon.*/
161static char *daemon_name;
eab2ee89 162
5790ef40
DN
163/* Pipe used to send exit notification to the daemon parent at
164 startup. */
ff808935 165int daemon_pipe[2];
fc012771 166
e29f86e4
RS
167/* Save argv and argc. */
168char **initial_argv;
169int initial_argc;
081bef73 170
dd4c5104 171static void sort_args (int argc, char **argv);
35f08c38 172static void syms_of_emacs (void);
bd4590ba 173
6b61353c
KH
174/* MSVC needs each string be shorter than 2048 bytes, so the usage
175 strings below are split to not overflow this limit. */
73ebcd25 176#define USAGE1 "\
bd4590ba
GM
177Usage: %s [OPTION-OR-FILENAME]...\n\
178\n\
179Run Emacs, the extensible, customizable, self-documenting real-time\n\
180display editor. The recommended way to start Emacs for normal editing\n\
181is with no options at all.\n\
182\n\
49ca717b 183Run M-x info RET m emacs RET m emacs invocation RET inside Emacs to\n\
bd4590ba
GM
184read the main documentation for these command-line arguments.\n\
185\n\
186Initialization options:\n\
187\n\
6b61353c 188--batch do not do interactive display; implies -q\n\
f63d0028 189--chdir DIR change to directory DIR\n\
598898a7 190--daemon start a server in the background\n\
6b61353c
KH
191--debug-init enable Emacs Lisp debugger for init file\n\
192--display, -d DISPLAY use X server DISPLAY\n\
6b61353c
KH
193--no-desktop do not load a saved desktop\n\
194--no-init-file, -q load neither ~/.emacs nor default.el\n\
195--no-shared-memory, -nl do not use shared memory\n\
196--no-site-file do not load site-start.el\n\
66b7b0fe 197--no-site-lisp, -nsl do not add site-lisp directories to load-path\n\
6b61353c 198--no-splash do not display a splash screen on startup\n\
2ff86b4e 199--no-window-system, -nw do not communicate with X, ignoring $DISPLAY\n\
66b7b0fe
GM
200--quick, -Q equivalent to:\n\
201 -q --no-site-file --no-site-lisp --no-splash\n\
6b61353c
KH
202--script FILE run FILE as an Emacs Lisp script\n\
203--terminal, -t DEVICE use DEVICE for terminal I/O\n\
6b61353c
KH
204--user, -u USER load ~USER/.emacs instead of your own\n\
205\n%s"
206
207#define USAGE2 "\
bd4590ba
GM
208Action options:\n\
209\n\
6b61353c 210FILE visit FILE using find-file\n\
9020b223
GM
211+LINE go to line LINE in next FILE\n\
212+LINE:COLUMN go to line LINE, column COLUMN, in next FILE\n\
6b61353c
KH
213--directory, -L DIR add DIR to variable load-path\n\
214--eval EXPR evaluate Emacs Lisp expression EXPR\n\
215--execute EXPR evaluate Emacs Lisp expression EXPR\n\
216--file FILE visit FILE using find-file\n\
217--find-file FILE visit FILE using find-file\n\
218--funcall, -f FUNC call Emacs Lisp function FUNC with no arguments\n\
219--insert FILE insert contents of FILE into current buffer\n\
220--kill exit without asking for confirmation\n\
221--load, -l FILE load Emacs Lisp FILE using the load function\n\
222--visit FILE visit FILE using find-file\n\
73ebcd25
AI
223\n"
224
6b61353c 225#define USAGE3 "\
5b2ca26b 226Display options:\n\
bd4590ba 227\n\
6b61353c 228--background-color, -bg COLOR window background color\n\
2ff86b4e
NR
229--basic-display, -D disable many display features;\n\
230 used for debugging Emacs\n\
6b61353c
KH
231--border-color, -bd COLOR main border color\n\
232--border-width, -bw WIDTH width of main border\n\
ced74849 233--color, --color=MODE override color mode for character terminals;\n\
62973b41
JB
234 MODE defaults to `auto', and\n\
235 can also be `never', `always',\n\
6b61353c
KH
236 or a mode name like `ansi8'\n\
237--cursor-color, -cr COLOR color of the Emacs cursor indicating point\n\
238--font, -fn FONT default font; must be fixed-width\n\
239--foreground-color, -fg COLOR window foreground color\n\
240--fullheight, -fh make the first frame high as the screen\n\
62973b41 241--fullscreen, -fs make the first frame fullscreen\n\
6b61353c 242--fullwidth, -fw make the first frame wide as the screen\n\
3f1c6666 243--maximized, -mm make the first frame maximized\n\
6b61353c 244--geometry, -g GEOMETRY window geometry\n\
f9e36a6d 245--no-bitmap-icon, -nbi do not use picture of gnu for Emacs icon\n\
6b61353c
KH
246--iconic start Emacs in iconified state\n\
247--internal-border, -ib WIDTH width between text and main border\n\
248--line-spacing, -lsp PIXELS additional space to put between lines\n\
249--mouse-color, -ms COLOR mouse cursor color in Emacs window\n\
250--name NAME title for initial Emacs frame\n\
49ca717b 251--no-blinking-cursor, -nbc disable blinking cursor\n\
6b61353c
KH
252--reverse-video, -r, -rv switch foreground and background\n\
253--title, -T TITLE title for initial Emacs frame\n\
254--vertical-scroll-bars, -vb enable vertical scroll bars\n\
255--xrm XRESOURCES set additional X resources\n\
1540a61a 256--parent-id XID set parent window\n\
6b61353c
KH
257--help display this help and exit\n\
258--version output version information and exit\n\
07beadff
JR
259\n"
260
261#define USAGE4 "\
bd4590ba
GM
262You can generally also specify long option names with a single -; for\n\
263example, -batch as well as --batch. You can use any unambiguous\n\
264abbreviation for a --option.\n\
265\n\
266Various environment variables and window system resources also affect\n\
267Emacs' operation. See the main documentation.\n\
07beadff 268\n\
17284745 269Report bugs to bug-gnu-emacs@gnu.org. First, please see the Bugs\n\
bd4590ba
GM
270section of the Emacs manual or the file BUGS.\n"
271
f927c5ae 272\f
1882aa38
PE
273/* True if handling a fatal error already. */
274bool fatal_error_in_progress;
f927c5ae 275
204ee57f
JD
276#ifdef HAVE_NS
277/* NS autrelease pool, for memory management. */
278static void *ns_pool;
9e4bf381
PE
279#endif
280
204ee57f 281
4fab758d 282
cf29dd84
PE
283/* Report a fatal error due to signal SIG, output a backtrace of at
284 most BACKTRACE_LIMIT lines, and exit. */
285_Noreturn void
4d7e6e51 286terminate_due_to_signal (int sig, int backtrace_limit)
cf29dd84 287{
62a1d661 288 signal (sig, SIG_DFL);
4d7e6e51 289 totally_unblock_input ();
061b7f94 290
f927c5ae 291 /* If fatal error occurs in code below, avoid infinite recursion. */
8090eb09
JB
292 if (! fatal_error_in_progress)
293 {
294 fatal_error_in_progress = 1;
f927c5ae 295
9c524fcb 296 if (sig == SIGTERM || sig == SIGHUP || sig == SIGINT)
f63d0028
JD
297 Fkill_emacs (make_number (sig));
298
1882aa38 299 shut_down_emacs (sig, Qnil);
cf29dd84 300 emacs_backtrace (backtrace_limit);
8090eb09 301 }
f927c5ae 302
8090eb09 303 /* Signal the same code; this time it will really be fatal.
4d7e6e51
PE
304 Since we're in a signal handler, the signal is blocked, so we
305 have to unblock it if we want to really receive it. */
29b89fe0 306#ifndef MSDOS
2fe28299
PE
307 {
308 sigset_t unblocked;
309 sigemptyset (&unblocked);
4d7e6e51 310 sigaddset (&unblocked, sig);
2fe28299
PE
311 pthread_sigmask (SIG_UNBLOCK, &unblocked, 0);
312 }
29b89fe0 313#endif
4fab758d 314
4d7e6e51 315 emacs_raise (sig);
cf29dd84
PE
316
317 /* This shouldn't be executed, but it prevents a warning. */
318 exit (1);
f927c5ae 319}
271c7b7c
RS
320
321#ifdef SIGDANGER
322
16c323ee 323/* Handler for SIGDANGER. */
20ef56db
PE
324static void
325handle_danger_signal (int sig)
326{
271c7b7c 327 malloc_warning ("Operating system warns that virtual memory is running low.\n");
55796183
RS
328
329 /* It might be unsafe to call do_auto_save now. */
330 force_auto_save_soon ();
271c7b7c 331}
20ef56db
PE
332
333static void
334deliver_danger_signal (int sig)
335{
4d7e6e51 336 deliver_process_signal (sig, handle_danger_signal);
20ef56db 337}
271c7b7c 338#endif
f927c5ae 339\f
7db35a48 340/* Code for dealing with Lisp access to the Unix command line. */
f927c5ae 341
dfcf069d 342static void
dd4c5104 343init_cmdargs (int argc, char **argv, int skip_args)
f927c5ae
JB
344{
345 register int i;
74f10ca7 346 Lisp_Object name, dir, handler;
d311d28c 347 ptrdiff_t count = SPECPDL_INDEX ();
213d0b1f 348 Lisp_Object raw_name;
f927c5ae 349
e29f86e4
RS
350 initial_argv = argv;
351 initial_argc = argc;
352
213d0b1f
RS
353 raw_name = build_string (argv[0]);
354
355 /* Add /: to the front of the name
356 if it would otherwise be treated as magic. */
74f10ca7
PE
357 handler = Ffind_file_name_handler (raw_name, Qt);
358 if (! NILP (handler))
213d0b1f
RS
359 raw_name = concat2 (build_string ("/:"), raw_name);
360
361 Vinvocation_name = Ffile_name_nondirectory (raw_name);
362 Vinvocation_directory = Ffile_name_directory (raw_name);
363
ace40a69
RS
364 /* If we got no directory in argv[0], search PATH to find where
365 Emacs actually came from. */
366 if (NILP (Vinvocation_directory))
367 {
368 Lisp_Object found;
369 int yes = openp (Vexec_path, Vinvocation_name,
10c0915e 370 Vexec_suffixes, &found, make_number (X_OK));
e443f843 371 if (yes == 1)
213d0b1f
RS
372 {
373 /* Add /: to the front of the name
374 if it would otherwise be treated as magic. */
74f10ca7
PE
375 handler = Ffind_file_name_handler (found, Qt);
376 if (! NILP (handler))
213d0b1f
RS
377 found = concat2 (build_string ("/:"), found);
378 Vinvocation_directory = Ffile_name_directory (found);
379 }
ace40a69 380 }
59653951 381
4133b300
RS
382 if (!NILP (Vinvocation_directory)
383 && NILP (Ffile_name_absolute_p (Vinvocation_directory)))
213d0b1f
RS
384 /* Emacs was started with relative path, like ./emacs.
385 Make it absolute. */
4133b300
RS
386 Vinvocation_directory = Fexpand_file_name (Vinvocation_directory, Qnil);
387
07f4d123
RS
388 Vinstallation_directory = Qnil;
389
390 if (!NILP (Vinvocation_directory))
391 {
392 dir = Vinvocation_directory;
76151e2c
EZ
393#ifdef WINDOWSNT
394 /* If we are running from the build directory, set DIR to the
395 src subdirectory of the Emacs tree, like on Posix
396 platforms. */
397 if (SBYTES (dir) > sizeof ("/i386/") - 1
398 && 0 == strcmp (SSDATA (dir) + SBYTES (dir) - sizeof ("/i386/") + 1,
399 "/i386/"))
400 dir = Fexpand_file_name (build_string ("../.."), dir);
401#else /* !WINDOWSNT */
402#endif
07f4d123
RS
403 name = Fexpand_file_name (Vinvocation_name, dir);
404 while (1)
405 {
f5ab9736 406 Lisp_Object tem, lib_src_exists;
07f4d123
RS
407 Lisp_Object etc_exists, info_exists;
408
f5ab9736
RS
409 /* See if dir contains subdirs for use by Emacs.
410 Check for the ones that would exist in a build directory,
411 not including lisp and info. */
412 tem = Fexpand_file_name (build_string ("lib-src"), dir);
413 lib_src_exists = Ffile_exists_p (tem);
de004cc6 414
70344b34 415#ifdef MSDOS
de004cc6
RS
416 /* MSDOS installations frequently remove lib-src, but we still
417 must set installation-directory, or else info won't find
418 its files (it uses the value of installation-directory). */
419 tem = Fexpand_file_name (build_string ("info"), dir);
420 info_exists = Ffile_exists_p (tem);
70344b34
EZ
421#else
422 info_exists = Qnil;
423#endif
de004cc6
RS
424
425 if (!NILP (lib_src_exists) || !NILP (info_exists))
07f4d123 426 {
f5ab9736
RS
427 tem = Fexpand_file_name (build_string ("etc"), dir);
428 etc_exists = Ffile_exists_p (tem);
429 if (!NILP (etc_exists))
07f4d123 430 {
f5ab9736
RS
431 Vinstallation_directory
432 = Ffile_name_as_directory (dir);
433 break;
07f4d123
RS
434 }
435 }
436
437 /* See if dir's parent contains those subdirs. */
f5ab9736
RS
438 tem = Fexpand_file_name (build_string ("../lib-src"), dir);
439 lib_src_exists = Ffile_exists_p (tem);
de004cc6 440
70344b34
EZ
441
442#ifdef MSDOS
443 /* See the MSDOS commentary above. */
de004cc6
RS
444 tem = Fexpand_file_name (build_string ("../info"), dir);
445 info_exists = Ffile_exists_p (tem);
70344b34
EZ
446#else
447 info_exists = Qnil;
448#endif
de004cc6
RS
449
450 if (!NILP (lib_src_exists) || !NILP (info_exists))
07f4d123 451 {
f5ab9736
RS
452 tem = Fexpand_file_name (build_string ("../etc"), dir);
453 etc_exists = Ffile_exists_p (tem);
454 if (!NILP (etc_exists))
07f4d123 455 {
f5ab9736
RS
456 tem = Fexpand_file_name (build_string (".."), dir);
457 Vinstallation_directory
458 = Ffile_name_as_directory (tem);
459 break;
07f4d123
RS
460 }
461 }
462
463 /* If the Emacs executable is actually a link,
464 next try the dir that the link points into. */
465 tem = Ffile_symlink_p (name);
466 if (!NILP (tem))
467 {
260ec24d 468 name = Fexpand_file_name (tem, dir);
07f4d123
RS
469 dir = Ffile_name_directory (name);
470 }
471 else
472 break;
473 }
474 }
475
f927c5ae
JB
476 Vcommand_line_args = Qnil;
477
478 for (i = argc - 1; i >= 0; i--)
479 {
480 if (i == 0 || i > skip_args)
a520393d
KH
481 /* For the moment, we keep arguments as is in unibyte strings.
482 They are decoded in the function command-line after we know
483 locale-coding-system. */
f927c5ae 484 Vcommand_line_args
68db37aa 485 = Fcons (make_unibyte_string (argv[i], strlen (argv[i])),
a520393d 486 Vcommand_line_args);
f927c5ae 487 }
213d0b1f
RS
488
489 unbind_to (count, Qnil);
f927c5ae 490}
59653951
JB
491
492DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
7db35a48
PJ
493 doc: /* Return the program name that was used to run Emacs.
494Any directory names are omitted. */)
5842a27b 495 (void)
59653951
JB
496{
497 return Fcopy_sequence (Vinvocation_name);
498}
499
ace40a69 500DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
7db35a48
PJ
501 0, 0, 0,
502 doc: /* Return the directory name in which the Emacs executable was located. */)
5842a27b 503 (void)
ace40a69
RS
504{
505 return Fcopy_sequence (Vinvocation_directory);
506}
507
f927c5ae 508\f
0269dedb
RS
509#ifdef HAVE_TZSET
510/* A valid but unlikely value for the TZ environment value.
511 It is OK (though a bit slower) if the user actually chooses this value. */
512static char dump_tz[] = "UtC0";
513#endif
514
a90538cb 515#ifndef ORDINARY_LINK
efd241cc
RS
516/* We don't include crtbegin.o and crtend.o in the link,
517 so these functions and variables might be missed.
518 Provide dummy definitions to avoid error.
519 (We don't have any real constructors or destructors.) */
520#ifdef __GNUC__
244fc23d
PE
521
522/* Define a dummy function F. Declare F too, to pacify gcc
523 -Wmissing-prototypes. */
35f08c38 524#define DEFINE_DUMMY_FUNCTION(f) \
5994c183 525 void f (void) ATTRIBUTE_CONST EXTERNALLY_VISIBLE; void f (void) {}
244fc23d 526
46e65b73 527#ifndef GCC_CTORS_IN_LIBC
244fc23d
PE
528DEFINE_DUMMY_FUNCTION (__do_global_ctors)
529DEFINE_DUMMY_FUNCTION (__do_global_ctors_aux)
530DEFINE_DUMMY_FUNCTION (__do_global_dtors)
554061d8 531/* GNU/Linux has a bug in its library; avoid an error. */
8a2a6032 532#ifndef GNU_LINUX
35f08c38 533char * __CTOR_LIST__[2] EXTERNALLY_VISIBLE = { (char *) (-1), 0 };
64c1864a 534#endif
35f08c38 535char * __DTOR_LIST__[2] EXTERNALLY_VISIBLE = { (char *) (-1), 0 };
46e65b73 536#endif /* GCC_CTORS_IN_LIBC */
244fc23d 537DEFINE_DUMMY_FUNCTION (__main)
efd241cc 538#endif /* __GNUC__ */
a90538cb 539#endif /* ORDINARY_LINK */
efd241cc 540
e2925360
KH
541/* Test whether the next argument in ARGV matches SSTR or a prefix of
542 LSTR (at least MINLEN characters). If so, then if VALPTR is non-null
543 (the argument is supposed to have a value) store in *VALPTR either
544 the next argument or the portion of this one after the equal sign.
545 ARGV is read starting at position *SKIPPTR; this index is advanced
546 by the number of arguments used.
547
548 Too bad we can't just use getopt for all of this, but we don't have
549 enough information to do it right. */
081bef73 550
1882aa38 551static bool
62973b41
JB
552argmatch (char **argv, int argc, const char *sstr, const char *lstr,
553 int minlen, char **valptr, int *skipptr)
e2925360 554{
6bbd7a29 555 char *p = NULL;
0b963a93 556 ptrdiff_t arglen;
df6530f8
RS
557 char *arg;
558
559 /* Don't access argv[argc]; give up in advance. */
560 if (argc <= *skipptr + 1)
561 return 0;
562
563 arg = argv[*skipptr+1];
e2925360
KH
564 if (arg == NULL)
565 return 0;
566 if (strcmp (arg, sstr) == 0)
567 {
568 if (valptr != NULL)
569 {
570 *valptr = argv[*skipptr+2];
571 *skipptr += 2;
572 }
573 else
574 *skipptr += 1;
575 return 1;
576 }
8966b757 577 arglen = (valptr != NULL && (p = strchr (arg, '=')) != NULL
e2925360 578 ? p - arg : strlen (arg));
c03e1113 579 if (lstr == 0 || arglen < minlen || strncmp (arg, lstr, arglen) != 0)
e2925360
KH
580 return 0;
581 else if (valptr == NULL)
582 {
583 *skipptr += 1;
584 return 1;
585 }
586 else if (p != NULL)
587 {
588 *valptr = p+1;
589 *skipptr += 1;
590 return 1;
591 }
592 else if (argv[*skipptr+2] != NULL)
593 {
594 *valptr = argv[*skipptr+2];
595 *skipptr += 2;
596 return 1;
597 }
598 else
599 {
600 return 0;
601 }
602}
603
b6779252 604#ifdef DOUG_LEA_MALLOC
f927c5ae 605
b6779252
KH
606/* malloc can be invoked even before main (e.g. by the dynamic
607 linker), so the dumped malloc state must be restored as early as
608 possible using this special hook. */
609
610static void
dd4c5104 611malloc_initialize_hook (void)
b6779252 612{
15aaf1b5
RS
613 if (initialized)
614 {
7c9cd446
AS
615 if (!malloc_using_checking)
616 /* Work around a bug in glibc's malloc. MALLOC_CHECK_ must be
617 ignored if the heap to be restored was constructed without
d942e12a
AS
618 malloc checking. Can't use unsetenv, since that calls malloc. */
619 {
620 char **p;
621
a57c4026 622 for (p = environ; p && *p; p++)
d942e12a
AS
623 if (strncmp (*p, "MALLOC_CHECK_=", 14) == 0)
624 {
625 do
626 *p = p[1];
627 while (*++p);
628 break;
629 }
630 }
b6779252 631
15aaf1b5 632 malloc_set_state (malloc_state_ptr);
68db37aa 633#ifndef XMALLOC_OVERRUN_CHECK
15aaf1b5 634 free (malloc_state_ptr);
68db37aa 635#endif
15aaf1b5 636 }
7c9cd446 637 else
dede2792
JD
638 {
639 if (my_heap_start == 0)
640 my_heap_start = sbrk (0);
641 malloc_using_checking = getenv ("MALLOC_CHECK_") != NULL;
642 }
b6779252
KH
643}
644
698d32e2 645void (*__malloc_initialize_hook) (void) EXTERNALLY_VISIBLE = malloc_initialize_hook;
b6779252
KH
646
647#endif /* DOUG_LEA_MALLOC */
648
d883731c 649
b6779252
KH
650/* ARGSUSED */
651int
7c2fb837 652main (int argc, char **argv)
b6779252 653{
2313132f 654#if GC_MARK_STACK
39ec21ea
GM
655 Lisp_Object dummy;
656#endif
b6779252 657 char stack_bottom_variable;
1882aa38 658 bool do_initial_setlocale;
4d7e6e51 659 bool dumping;
b6779252 660 int skip_args = 0;
b6779252
KH
661#ifdef HAVE_SETRLIMIT
662 struct rlimit rlim;
663#endif
1882aa38 664 bool no_loadup = 0;
6e910e07 665 char *junk = 0;
4ff029f6 666 char *dname_arg = 0;
b3243e6f
AR
667#ifdef NS_IMPL_COCOA
668 char dname_arg2[80];
669#endif
f63d0028 670 char *ch_to_dir;
b6779252 671
2313132f 672#if GC_MARK_STACK
39ec21ea
GM
673 stack_base = &dummy;
674#endif
675
a4ef73c8
CY
676#if defined (USE_GTK) && defined (G_SLICE_ALWAYS_MALLOC)
677 /* This is used by the Cygwin build. */
678 setenv ("G_SLICE", "always-malloc", 1);
679#endif
680
b312a492 681#ifdef GNU_LINUX
dede2792
JD
682 if (!initialized)
683 {
684 extern char my_endbss[];
685 extern char *my_endbss_static;
686
687 if (my_heap_start == 0)
688 my_heap_start = sbrk (0);
689
690 heap_bss_diff = (char *)my_heap_start - max (my_endbss, my_endbss_static);
691 }
b312a492 692#endif
dede2792 693
d785cf9e
RS
694#ifdef RUN_TIME_REMAP
695 if (initialized)
696 run_time_remap (argv[0]);
697#endif
698
14145fa3
AR
699/* If using unexmacosx.c (set by s/darwin.h), we must do this. */
700#ifdef DARWIN_OS
e0f712ba
AC
701 if (!initialized)
702 unexec_init_emacs_zone ();
703#endif
704
081bef73 705 sort_args (argc, argv);
956e3c7e
RS
706 argc = 0;
707 while (argv[argc]) argc++;
081bef73 708
8c5ff6dd 709 if (argmatch (argv, argc, "-version", "--version", 3, NULL, &skip_args))
1702afef 710 {
8c5ff6dd
KR
711 const char *version, *copyright;
712 if (initialized)
5cf9ca93 713 {
8c5ff6dd
KR
714 Lisp_Object tem, tem2;
715 tem = Fsymbol_value (intern_c_string ("emacs-version"));
716 tem2 = Fsymbol_value (intern_c_string ("emacs-copyright"));
717 if (!STRINGP (tem))
718 {
719 fprintf (stderr, "Invalid value of `emacs-version'\n");
720 exit (1);
721 }
722 if (!STRINGP (tem2))
723 {
724 fprintf (stderr, "Invalid value of `emacs-copyright'\n");
725 exit (1);
726 }
727 else
728 {
42a5b22f
PE
729 version = SSDATA (tem);
730 copyright = SSDATA (tem2);
8c5ff6dd 731 }
5cf9ca93 732 }
1702afef
RS
733 else
734 {
8c5ff6dd
KR
735 version = emacs_version;
736 copyright = emacs_copyright;
1702afef 737 }
8c5ff6dd
KR
738 printf ("GNU Emacs %s\n", version);
739 printf ("%s\n", copyright);
740 printf ("GNU Emacs comes with ABSOLUTELY NO WARRANTY.\n");
741 printf ("You may redistribute copies of Emacs\n");
742 printf ("under the terms of the GNU General Public License.\n");
743 printf ("For more information about these matters, ");
744 printf ("see the file named COPYING.\n");
745 exit (0);
1702afef 746 }
947f5e01
JM
747
748 if (argmatch (argv, argc, "-chdir", "--chdir", 4, &ch_to_dir, &skip_args))
749 if (chdir (ch_to_dir) == -1)
750 {
751 fprintf (stderr, "%s: Can't chdir to %s: %s\n",
752 argv[0], ch_to_dir, strerror (errno));
753 exit (1);
754 }
f63d0028 755
4d7e6e51
PE
756 dumping = !initialized && (strcmp (argv[argc - 1], "dump") == 0
757 || strcmp (argv[argc - 1], "bootstrap") == 0);
1702afef 758
dede2792 759#ifdef HAVE_PERSONALITY_LINUX32
4d7e6e51 760 if (dumping && ! getenv ("EMACS_HEAP_EXEC"))
dede2792 761 {
42ca4633 762 static char heapexec[] = "EMACS_HEAP_EXEC=true";
fd75ddb2 763 /* Set this so we only do this once. */
5e617bc2 764 putenv (heapexec);
711877f3 765
fd75ddb2
JD
766 /* A flag to turn off address randomization which is introduced
767 in linux kernel shipped with fedora core 4 */
711877f3 768#define ADD_NO_RANDOMIZE 0x0040000
fd75ddb2 769 personality (PER_LINUX32 | ADD_NO_RANDOMIZE);
711877f3
MY
770#undef ADD_NO_RANDOMIZE
771
fd75ddb2 772 execvp (argv[0], argv);
dede2792 773
fd75ddb2
JD
774 /* If the exec fails, try to dump anyway. */
775 perror ("execvp");
dede2792
JD
776 }
777#endif /* HAVE_PERSONALITY_LINUX32 */
778
ea2acec5 779#if defined (HAVE_SETRLIMIT) && defined (RLIMIT_STACK)
6c2935e9
RS
780 /* Extend the stack space available.
781 Don't do that if dumping, since some systems (e.g. DJGPP)
782 might define a smaller stack limit at that time. */
783 if (1
784#ifndef CANNOT_DUMP
785 && (!noninteractive || initialized)
786#endif
787 && !getrlimit (RLIMIT_STACK, &rlim))
53c58b5d 788 {
509a8fcd 789 long newlim;
fa8459a3 790 extern size_t re_max_failures;
03effc23
KH
791 /* Approximate the amount regex.c needs per unit of re_max_failures. */
792 int ratio = 20 * sizeof (char *);
793 /* Then add 33% to cover the size of the smaller stacks that regex.c
794 successively allocates and discards, on its way to the maximum. */
795 ratio += ratio / 3;
796 /* Add in some extra to cover
797 what we're likely to use for other reasons. */
798 newlim = re_max_failures * ratio + 200000;
d0381a7f
RS
799#ifdef __NetBSD__
800 /* NetBSD (at least NetBSD 1.2G and former) has a bug in its
801 stack allocation routine for new process that the allocation
802 fails if stack limit is not on page boundary. So, round up the
803 new limit to page boundary. */
5e617bc2 804 newlim = (newlim + getpagesize () - 1) / getpagesize () * getpagesize ();
d0381a7f 805#endif
509a8fcd 806 if (newlim > rlim.rlim_max)
6c2935e9
RS
807 {
808 newlim = rlim.rlim_max;
03effc23
KH
809 /* Don't let regex.c overflow the stack we have. */
810 re_max_failures = (newlim - 200000) / ratio;
6c2935e9 811 }
509a8fcd
RS
812 if (rlim.rlim_cur < newlim)
813 rlim.rlim_cur = newlim;
814
53c58b5d
RS
815 setrlimit (RLIMIT_STACK, &rlim);
816 }
ea2acec5 817#endif /* HAVE_SETRLIMIT and RLIMIT_STACK */
53c58b5d 818
f927c5ae
JB
819 /* Record (approximately) where the stack begins. */
820 stack_bottom = &stack_bottom_variable;
821
f927c5ae 822 clearerr (stdin);
9ae8f997 823
f927c5ae 824#ifndef SYSTEM_MALLOC
bf7f4e90
RS
825 /* Arrange to get warning messages as memory fills up. */
826 memory_warnings (0, malloc_warning);
9ac0d9e0 827
0caaedb1 828 /* Call malloc at least once, to run malloc_initialize_hook.
f5a3c8c4
AI
829 Also call realloc and free for consistency. */
830 free (realloc (malloc (4), 4));
bf7f4e90 831
f927c5ae
JB
832#endif /* not SYSTEM_MALLOC */
833
edd3ff1d 834#if defined (MSDOS) || defined (WINDOWSNT)
29b89fe0
RS
835 /* We do all file input/output as binary files. When we need to translate
836 newlines, we do that manually. */
837 _fmode = O_BINARY;
edd3ff1d 838#endif /* MSDOS || WINDOWSNT */
18198bb2 839
edd3ff1d 840#ifdef MSDOS
18198bb2
RS
841 if (!isatty (fileno (stdin)))
842 setmode (fileno (stdin), O_BINARY);
843 if (!isatty (fileno (stdout)))
844 {
845 fflush (stdout);
846 setmode (fileno (stdout), O_BINARY);
847 }
29b89fe0
RS
848#endif /* MSDOS */
849
68c45bf0
PE
850 /* Skip initial setlocale if LC_ALL is "C", as it's not needed in that case.
851 The build procedure uses this while dumping, to ensure that the
852 dumped Emacs does not have its system locale tables initialized,
853 as that might cause screwups when the dumped Emacs starts up. */
854 {
855 char *lc_all = getenv ("LC_ALL");
856 do_initial_setlocale = ! lc_all || strcmp (lc_all, "C");
857 }
858
859 /* Set locale now, so that initial error messages are localized properly.
860 fixup_locale must wait until later, since it builds strings. */
861 if (do_initial_setlocale)
862 setlocale (LC_ALL, "");
863
f927c5ae
JB
864 inhibit_window_system = 0;
865
7db35a48 866 /* Handle the -t switch, which specifies filename to use as terminal. */
956e3c7e
RS
867 while (1)
868 {
869 char *term;
870 if (argmatch (argv, argc, "-t", "--terminal", 4, &term, &skip_args))
871 {
872 int result;
68c45bf0
PE
873 emacs_close (0);
874 emacs_close (1);
875 result = emacs_open (term, O_RDWR, 0);
9055082e 876 if (result < 0 || dup (0) < 0)
956e3c7e
RS
877 {
878 char *errstring = strerror (errno);
186486eb 879 fprintf (stderr, "%s: %s: %s\n", argv[0], term, errstring);
956e3c7e
RS
880 exit (1);
881 }
956e3c7e
RS
882 if (! isatty (0))
883 {
186486eb 884 fprintf (stderr, "%s: %s: not a tty\n", argv[0], term);
956e3c7e
RS
885 exit (1);
886 }
887 fprintf (stderr, "Using %s\n", term);
8ba50e1a 888#ifdef HAVE_WINDOW_SYSTEM
956e3c7e 889 inhibit_window_system = 1; /* -t => -nw */
f927c5ae 890#endif
956e3c7e
RS
891 }
892 else
893 break;
894 }
895
400d6fa9 896 /* Command line option --no-windows is deprecated and thus not mentioned
333f9019 897 in the manual and usage information. */
400d6fa9
PJ
898 if (argmatch (argv, argc, "-nw", "--no-window-system", 6, NULL, &skip_args)
899 || argmatch (argv, argc, "-nw", "--no-windows", 6, NULL, &skip_args))
e2925360 900 inhibit_window_system = 1;
f927c5ae 901
e2925360 902 /* Handle the -batch switch, which means don't do interactive display. */
f927c5ae 903 noninteractive = 0;
df6530f8 904 if (argmatch (argv, argc, "-batch", "--batch", 5, NULL, &skip_args))
55f4edbc
RS
905 {
906 noninteractive = 1;
907 Vundo_outer_limit = Qnil;
908 }
6e910e07
RS
909 if (argmatch (argv, argc, "-script", "--script", 3, &junk, &skip_args))
910 {
911 noninteractive = 1; /* Set batch mode. */
21a4ff8e 912 /* Convert --script to -scriptload, un-skip it, and sort again
e09b9180 913 so that it will be handled in proper sequence. */
a09a5b5b 914 /* FIXME broken for --script=FILE - is that supposed to work? */
c03cd23f 915 argv[skip_args - 1] = (char *) "-scriptload";
6e910e07
RS
916 skip_args -= 2;
917 sort_args (argc, argv);
918 }
e2925360 919
7db35a48 920 /* Handle the --help option, which gives a usage message. */
df6530f8 921 if (argmatch (argv, argc, "-help", "--help", 3, NULL, &skip_args))
f927c5ae 922 {
6b61353c
KH
923 printf (USAGE1, argv[0], USAGE2);
924 printf (USAGE3);
17284745 925 printf (USAGE4);
e2925360 926 exit (0);
f927c5ae
JB
927 }
928
4ff029f6
DN
929 if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)
930 || argmatch (argv, argc, "-daemon", "--daemon", 5, &dname_arg, &skip_args))
eab2ee89 931 {
9d29095c 932#ifndef DOS_NT
5790ef40
DN
933 pid_t f;
934
935 /* Start as a daemon: fork a new child process which will run the
936 rest of the initialization code, then exit.
937
fc012771
SM
938 Detaching a daemon requires the following steps:
939 - fork
940 - setsid
941 - exit the parent
942 - close the tty file-descriptors
943
944 We only want to do the last 2 steps once the daemon is ready to
945 serve requests, i.e. after loading .emacs (initialization).
946 OTOH initialization may start subprocesses (e.g. ispell) and these
947 should be run from the proper process (the one that will end up
948 running as daemon) and with the proper "session id" in order for
949 them to keep working after detaching, so fork and setsid need to be
950 performed before initialization.
951
5790ef40
DN
952 We want to avoid exiting before the server socket is ready, so
953 use a pipe for synchronization. The parent waits for the child
954 to close its end of the pipe (using `daemon-initialized')
955 before exiting. */
956 if (pipe (daemon_pipe) == -1)
957 {
958 fprintf (stderr, "Cannot pipe!\n");
959 exit (1);
960 }
961
b3243e6f 962#ifndef NS_IMPL_COCOA
8b3115e7
DN
963#ifdef USE_GTK
964 fprintf (stderr, "\nWarning: due to a long standing Gtk+ bug\nhttp://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
965Emacs might crash when run in daemon mode and the X11 connection is unexpectedly lost.\n\
966Using an Emacs configured with --with-x-toolkit=lucid does not have this problem.\n");
967#endif
5790ef40 968 f = fork ();
cfa6accb
AR
969#else /* NS_IMPL_COCOA */
970 /* Under Cocoa we must do fork+exec as CoreFoundation lib fails in
971 forked process: http://developer.apple.com/ReleaseNotes/
972 CoreFoundation/CoreFoundation.html)
b3243e6f
AR
973 We mark being in the exec'd process by a daemon name argument of
974 form "--daemon=\nFD0,FD1\nNAME" where FD are the pipe file descriptors,
975 NAME is the original daemon name, if any. */
976 if (!dname_arg || !strchr (dname_arg, '\n'))
977 f = fork (); /* in orig */
978 else
979 f = 0; /* in exec'd */
cfa6accb 980#endif /* NS_IMPL_COCOA */
eab2ee89 981 if (f > 0)
5790ef40
DN
982 {
983 int retval;
984 char buf[1];
985
986 /* Close unused writing end of the pipe. */
987 close (daemon_pipe[1]);
988
989 /* Just wait for the child to close its end of the pipe. */
990 do
991 {
992 retval = read (daemon_pipe[0], &buf, 1);
993 }
994 while (retval == -1 && errno == EINTR);
995
996 if (retval < 0)
997 {
998 fprintf (stderr, "Error reading status from child\n");
999 exit (1);
1000 }
fd95644b
DN
1001 else if (retval == 0)
1002 {
1003 fprintf (stderr, "Error: server did not start correctly\n");
1004 exit (1);
1005 }
5790ef40
DN
1006
1007 close (daemon_pipe[0]);
1008 exit (0);
1009 }
eab2ee89
DN
1010 if (f < 0)
1011 {
1012 fprintf (stderr, "Cannot fork!\n");
9d0a6e42 1013 exit (1);
eab2ee89
DN
1014 }
1015
b3243e6f
AR
1016#ifdef NS_IMPL_COCOA
1017 {
cfa6accb 1018 /* In orig process, forked as child, OR in exec'd. */
b3243e6f 1019 if (!dname_arg || !strchr (dname_arg, '\n'))
cfa6accb 1020 { /* In orig, child: now exec w/special daemon name. */
b3243e6f 1021 char fdStr[80];
66c6fdd5
PE
1022 int fdStrlen =
1023 snprintf (fdStr, sizeof fdStr,
1024 "--daemon=\n%d,%d\n%s", daemon_pipe[0],
1025 daemon_pipe[1], dname_arg ? dname_arg : "");
b3243e6f 1026
66c6fdd5 1027 if (! (0 <= fdStrlen && fdStrlen < sizeof fdStr))
b3243e6f
AR
1028 {
1029 fprintf (stderr, "daemon: child name too long\n");
1030 exit (1);
1031 }
1032
b3243e6f
AR
1033 argv[skip_args] = fdStr;
1034
1035 execv (argv[0], argv);
cff11156 1036 fprintf (stderr, "emacs daemon: exec failed: %d\n", errno);
b3243e6f
AR
1037 exit (1);
1038 }
1039
cfa6accb 1040 /* In exec'd: parse special dname into pipe and name info. */
b3243e6f
AR
1041 if (!dname_arg || !strchr (dname_arg, '\n')
1042 || strlen (dname_arg) < 1 || strlen (dname_arg) > 70)
1043 {
1044 fprintf (stderr, "emacs daemon: daemon name absent or too long\n");
5e617bc2 1045 exit (1);
b3243e6f
AR
1046 }
1047 dname_arg2[0] = '\0';
1048 sscanf (dname_arg, "\n%d,%d\n%s", &(daemon_pipe[0]), &(daemon_pipe[1]),
1049 dname_arg2);
0b963a93 1050 dname_arg = *dname_arg2 ? dname_arg2 : NULL;
b3243e6f 1051 }
cfa6accb 1052#endif /* NS_IMPL_COCOA */
b3243e6f 1053
4ff029f6
DN
1054 if (dname_arg)
1055 daemon_name = xstrdup (dname_arg);
5790ef40
DN
1056 /* Close unused reading end of the pipe. */
1057 close (daemon_pipe[0]);
7c19d3ae
DN
1058 /* Make sure that the used end of the pipe is closed on exec, so
1059 that it is not accessible to programs started from .emacs. */
1060 fcntl (daemon_pipe[1], F_SETFD, FD_CLOEXEC);
1061
eab2ee89 1062#ifdef HAVE_SETSID
5e617bc2 1063 setsid ();
eab2ee89 1064#endif
eab2ee89 1065#else /* DOS_NT */
9d29095c
DN
1066 fprintf (stderr, "This platform does not support the -daemon flag.\n");
1067 exit (1);
eab2ee89 1068#endif /* DOS_NT */
9d29095c 1069 }
eab2ee89 1070
edb85f59
RS
1071 if (! noninteractive)
1072 {
edb85f59
RS
1073#if defined (USG5) && defined (INTERRUPT_INPUT)
1074 setpgrp ();
1075#endif
ae9e757a 1076#if defined (HAVE_PTHREAD) && !defined (SYSTEM_MALLOC) && !defined (DOUG_LEA_MALLOC)
5467331d 1077 {
f57e2426 1078 extern void malloc_enable_thread (void);
5467331d
YM
1079
1080 malloc_enable_thread ();
1081 }
edb85f59
RS
1082#endif
1083 }
1084
4d7e6e51 1085 init_signals (dumping);
f927c5ae
JB
1086
1087 noninteractive1 = noninteractive;
1088
7db35a48 1089/* Perform basic initializations (not merely interning symbols). */
f927c5ae
JB
1090
1091 if (!initialized)
1092 {
1093 init_alloc_once ();
1094 init_obarray ();
1095 init_eval_once ();
270ce821
KH
1096 init_charset_once ();
1097 init_coding_once ();
f927c5ae 1098 init_syntax_once (); /* Create standard syntax table. */
270ce821 1099 init_category_once (); /* Create standard category table. */
7db35a48 1100 /* Must be done before init_buffer. */
f927c5ae 1101 init_casetab_once ();
7db35a48
PJ
1102 init_buffer_once (); /* Create buffer table and some buffers. */
1103 init_minibuf_once (); /* Create list of minibuffers. */
1104 /* Must precede init_window_once. */
1105
90d920b6
GM
1106 /* Call syms_of_xfaces before init_window_once because that
1107 function creates Vterminal_frame. Termcap frames now use
1108 faces, and the face implementation uses some symbols as
1109 face names. */
90d920b6 1110 syms_of_xfaces ();
4ea81208
KL
1111 /* XXX syms_of_keyboard uses some symbols in keymap.c. It would
1112 be better to arrange things not to have this dependency. */
1113 syms_of_keymap ();
7a18af49
KR
1114 /* Call syms_of_keyboard before init_window_once because
1115 keyboard sets up symbols that include some face names that
1116 the X support will want to use. This can happen when
1117 CANNOT_DUMP is defined. */
1118 syms_of_keyboard ();
90d920b6 1119
985773c9
MB
1120 /* Called before syms_of_fileio, because it sets up Qerror_condition. */
1121 syms_of_data ();
1122 syms_of_fileio ();
1123 /* Before syms_of_coding to initialize Vgc_cons_threshold. */
1124 syms_of_alloc ();
1125 /* Before syms_of_coding because it initializes Qcharsetp. */
1126 syms_of_charset ();
1127 /* Before init_window_once, because it sets up the
1128 Vcoding_system_hash_table. */
1129 syms_of_coding (); /* This should be after syms_of_fileio. */
1a578e9b 1130
7db35a48 1131 init_window_once (); /* Init the window system. */
6b61353c
KH
1132#ifdef HAVE_WINDOW_SYSTEM
1133 init_fringe_once (); /* Swap bitmaps if necessary. */
1134#endif /* HAVE_WINDOW_SYSTEM */
f927c5ae
JB
1135 }
1136
1137 init_alloc ();
68c45bf0
PE
1138
1139 if (do_initial_setlocale)
1140 {
1141 fixup_locale ();
ca9c0567
PE
1142 Vsystem_messages_locale = Vprevious_system_messages_locale;
1143 Vsystem_time_locale = Vprevious_system_time_locale;
68c45bf0
PE
1144 }
1145
f927c5ae 1146 init_eval ();
ab5d0358 1147 init_atimer ();
7074fde6 1148 running_asynch_code = 0;
0e23ef9d 1149 init_random ();
0e956009 1150
956e3c7e 1151 no_loadup
b96f9fb7 1152 = argmatch (argv, argc, "-nl", "--no-loadup", 6, NULL, &skip_args);
956e3c7e 1153
66b7b0fe
GM
1154 no_site_lisp
1155 = argmatch (argv, argc, "-nsl", "--no-site-lisp", 11, NULL, &skip_args);
1156
edfda783 1157#ifdef HAVE_NS
204ee57f 1158 ns_pool = ns_alloc_autorelease_pool ();
edfda783
AR
1159 if (!noninteractive)
1160 {
edfda783
AR
1161#ifdef NS_IMPL_COCOA
1162 if (skip_args < argc)
1163 {
32299e33
PE
1164 /* FIXME: Do the right thing if getenv returns NULL, or if
1165 chdir fails. */
5e617bc2 1166 if (!strncmp (argv[skip_args], "-psn", 4))
edfda783
AR
1167 {
1168 skip_args += 1;
3d0a4431 1169 chdir (getenv ("HOME"));
edfda783 1170 }
5e617bc2 1171 else if (skip_args+1 < argc && !strncmp (argv[skip_args+1], "-psn", 4))
edfda783 1172 {
95889848 1173 skip_args += 2;
3d0a4431 1174 chdir (getenv ("HOME"));
edfda783
AR
1175 }
1176 }
9b68317c 1177#endif /* COCOA */
edfda783
AR
1178 }
1179#endif /* HAVE_NS */
1180
956e3c7e
RS
1181#ifdef HAVE_X_WINDOWS
1182 /* Stupid kludge to catch command-line display spec. We can't
1183 handle this argument entirely in window system dependent code
1184 because we don't even know which window system dependent code
1185 to run until we've recognized this argument. */
1186 {
1187 char *displayname = 0;
956e3c7e
RS
1188 int count_before = skip_args;
1189
1190 /* Skip any number of -d options, but only use the last one. */
1191 while (1)
1192 {
1193 int count_before_this = skip_args;
1194
1195 if (argmatch (argv, argc, "-d", "--display", 3, &displayname, &skip_args))
1196 display_arg = 1;
1197 else if (argmatch (argv, argc, "-display", 0, 3, &displayname, &skip_args))
1198 display_arg = 1;
1199 else
1200 break;
1201
1202 count_before = count_before_this;
1203 }
1204
1205 /* If we have the form --display=NAME,
1206 convert it into -d name.
1207 This requires inserting a new element into argv. */
0065d054 1208 if (displayname && count_before < skip_args)
956e3c7e 1209 {
0065d054
PE
1210 if (skip_args == count_before + 1)
1211 {
1212 memmove (argv + count_before + 3, argv + count_before + 2,
1213 (argc - (count_before + 2)) * sizeof *argv);
1214 argv[count_before + 2] = displayname;
1215 argc++;
1216 }
1217 argv[count_before + 1] = (char *) "-d";
956e3c7e 1218 }
956e3c7e 1219
66b7b0fe
GM
1220 if (! no_site_lisp)
1221 {
1222 if (argmatch (argv, argc, "-Q", "--quick", 3, NULL, &skip_args)
1223 || argmatch (argv, argc, "-quick", 0, 2, NULL, &skip_args))
1224 no_site_lisp = 1;
1225 }
1226
956e3c7e
RS
1227 /* Don't actually discard this arg. */
1228 skip_args = count_before;
1229 }
66b7b0fe
GM
1230#else /* !HAVE_X_WINDOWS */
1231 if (! no_site_lisp)
1232 {
1233 int count_before = skip_args;
1234
1235 if (argmatch (argv, argc, "-Q", "--quick", 3, NULL, &skip_args)
1236 || argmatch (argv, argc, "-quick", 0, 2, NULL, &skip_args))
1237 no_site_lisp = 1;
1238
1239 skip_args = count_before;
1240 }
956e3c7e
RS
1241#endif
1242
1243 /* argmatch must not be used after here,
91af3942 1244 except when building temacs
956e3c7e
RS
1245 because the -d argument has not been skipped in skip_args. */
1246
29b89fe0
RS
1247#ifdef MSDOS
1248 /* Call early 'cause init_environment needs it. */
1249 init_dosfns ();
1250 /* Set defaults for several environment variables. */
18198bb2
RS
1251 if (initialized)
1252 init_environment (argc, argv, skip_args);
1253 else
d1fc6752 1254 tzset ();
18198bb2 1255#endif /* MSDOS */
29b89fe0 1256
8ba50e1a 1257#ifdef WINDOWSNT
9785d95b 1258 globals_of_w32 ();
8ba50e1a 1259 /* Initialize environment from registry settings. */
a3a58294 1260 init_environment (argv);
16b22fef 1261 init_ntproc (dumping); /* must precede init_editfns. */
8ba50e1a
GV
1262#endif
1263
8b4eb796
KR
1264 /* Initialize and GC-protect Vinitial_environment and
1265 Vprocess_environment before set_initial_environment fills them
1266 in. */
1267 if (!initialized)
1268 syms_of_callproc ();
0e956009
JB
1269 /* egetenv is a pretty low-level facility, which may get called in
1270 many circumstances; it seems flimsy to put off initializing it
738db178 1271 until calling init_callproc. Do not do it when dumping. */
4d7e6e51 1272 if (! dumping)
738db178
DN
1273 set_initial_environment ();
1274
93aed04d 1275 /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
5990851d
KL
1276 if this is not done. Do it after set_global_environment so that we
1277 don't pollute Vglobal_environment. */
fa09a82d 1278 /* Setting LANG here will defeat the startup locale processing... */
f2a77c3a 1279#ifdef AIX
93aed04d
RS
1280 putenv ("LANG=C");
1281#endif
0e956009 1282
7db35a48 1283 init_buffer (); /* Init default directory of main buffer. */
ace40a69 1284
7928f0b5 1285 init_callproc_1 (); /* Must precede init_cmdargs and init_sys_modes. */
ace40a69 1286 init_cmdargs (argc, argv, skip_args); /* Must precede init_lread. */
380e25b8
RS
1287
1288 if (initialized)
1289 {
7db35a48 1290 /* Erase any pre-dump messages in the message log, to avoid confusion. */
380e25b8
RS
1291 Lisp_Object old_log_max;
1292 old_log_max = Vmessage_log_max;
1293 XSETFASTINT (Vmessage_log_max, 0);
f6fe7bb5 1294 message_dolog ("", 0, 1, 0);
380e25b8
RS
1295 Vmessage_log_max = old_log_max;
1296 }
1297
7928f0b5 1298 init_callproc (); /* Must follow init_cmdargs but not init_sys_modes. */
fb8e9847 1299 init_lread ();
76151e2c
EZ
1300#ifdef WINDOWSNT
1301 /* Check to see if Emacs has been installed correctly. */
1302 check_windows_init_file ();
1303#endif
f927c5ae 1304
8bb697c0
RS
1305 /* Intern the names of all standard functions and variables;
1306 define standard keys. */
f927c5ae
JB
1307
1308 if (!initialized)
1309 {
4230ab74
KR
1310 /* The basic levels of Lisp must come first. Note that
1311 syms_of_data and some others have already been called. */
e37d7195 1312 syms_of_chartab ();
fb8e9847 1313 syms_of_lread ();
f927c5ae
JB
1314 syms_of_print ();
1315 syms_of_eval ();
1316 syms_of_fns ();
f927c5ae 1317 syms_of_floatfns ();
f927c5ae 1318
f927c5ae
JB
1319 syms_of_buffer ();
1320 syms_of_bytecode ();
1321 syms_of_callint ();
1322 syms_of_casefiddle ();
1323 syms_of_casetab ();
270ce821
KH
1324 syms_of_category ();
1325 syms_of_ccl ();
e37d7195 1326 syms_of_character ();
f927c5ae 1327 syms_of_cmds ();
f927c5ae 1328 syms_of_dired ();
f927c5ae
JB
1329 syms_of_display ();
1330 syms_of_doc ();
1331 syms_of_editfns ();
1332 syms_of_emacs ();
f927c5ae 1333 syms_of_filelock ();
f927c5ae 1334 syms_of_indent ();
c2c5ed2c 1335 syms_of_insdel ();
4ea81208 1336 /* syms_of_keymap (); */
f927c5ae
JB
1337 syms_of_macros ();
1338 syms_of_marker ();
1339 syms_of_minibuf ();
f927c5ae 1340 syms_of_process ();
f927c5ae 1341 syms_of_search ();
1cbd5d9d 1342 syms_of_frame ();
f927c5ae 1343 syms_of_syntax ();
ed8dad6b 1344 syms_of_terminal ();
0d934e7b 1345 syms_of_term ();
f927c5ae 1346 syms_of_undo ();
90d920b6
GM
1347#ifdef HAVE_SOUND
1348 syms_of_sound ();
1349#endif
bef79ee4 1350 syms_of_textprop ();
9d100795 1351 syms_of_composite ();
05687c54
RS
1352#ifdef WINDOWSNT
1353 syms_of_ntproc ();
1354#endif /* WINDOWSNT */
f927c5ae
JB
1355 syms_of_window ();
1356 syms_of_xdisp ();
d53f587b 1357 syms_of_font ();
4fa9161d 1358#ifdef HAVE_WINDOW_SYSTEM
6b61353c
KH
1359 syms_of_fringe ();
1360 syms_of_image ();
1361#endif /* HAVE_WINDOW_SYSTEM */
f927c5ae 1362#ifdef HAVE_X_WINDOWS
72412588 1363 syms_of_xterm ();
f927c5ae 1364 syms_of_xfns ();
edfda783 1365 syms_of_xmenu ();
270ce821 1366 syms_of_fontset ();
637fa988 1367 syms_of_xsettings ();
28b1b672
JD
1368#ifdef HAVE_X_SM
1369 syms_of_xsmfns ();
1370#endif
72412588
JB
1371#ifdef HAVE_X11
1372 syms_of_xselect ();
1373#endif
f927c5ae
JB
1374#endif /* HAVE_X_WINDOWS */
1375
381408e2
LMI
1376#ifdef HAVE_LIBXML2
1377 syms_of_xml ();
1378#endif
1379
4e77ce60 1380 syms_of_menu ();
4e77ce60 1381
8ba50e1a 1382#ifdef HAVE_NTGUI
fbd6baed
GV
1383 syms_of_w32term ();
1384 syms_of_w32fns ();
fbd6baed
GV
1385 syms_of_w32select ();
1386 syms_of_w32menu ();
02062ac3 1387 syms_of_fontset ();
8ba50e1a
GV
1388#endif /* HAVE_NTGUI */
1389
40da1153
EZ
1390#ifdef MSDOS
1391 syms_of_xmenu ();
5e617bc2
JB
1392 syms_of_dosfns ();
1393 syms_of_msdos ();
1394 syms_of_win16select ();
40da1153
EZ
1395#endif /* MSDOS */
1396
edfda783
AR
1397#ifdef HAVE_NS
1398 syms_of_nsterm ();
1399 syms_of_nsfns ();
1400 syms_of_nsmenu ();
1401 syms_of_nsselect ();
1402 syms_of_fontset ();
1403#endif /* HAVE_NS */
1404
8af55556
TZ
1405#ifdef HAVE_GNUTLS
1406 syms_of_gnutls ();
1407#endif
1408
033b73e2
MA
1409#ifdef HAVE_DBUS
1410 syms_of_dbusbind ();
1411#endif /* HAVE_DBUS */
1412
2e31d424
DN
1413#ifdef WINDOWSNT
1414 syms_of_ntterm ();
1415#endif /* WINDOWSNT */
f927c5ae
JB
1416
1417 keys_of_casefiddle ();
1418 keys_of_cmds ();
1419 keys_of_buffer ();
1420 keys_of_keyboard ();
1421 keys_of_keymap ();
f927c5ae 1422 keys_of_window ();
9785d95b 1423 }
3b6536b1 1424 else
9785d95b 1425 {
3b6536b1
AS
1426 /* Initialization that must be done even if the global variable
1427 initialized is non zero. */
9785d95b 1428#ifdef HAVE_NTGUI
db4f02f2 1429 globals_of_w32font ();
9785d95b
BK
1430 globals_of_w32fns ();
1431 globals_of_w32menu ();
52c7f9ee 1432 globals_of_w32select ();
3b6536b1 1433#endif /* HAVE_NTGUI */
f927c5ae
JB
1434 }
1435
cf411e8d
KH
1436 init_charset ();
1437
b82da769
GM
1438 init_editfns (); /* init_process_emacs uses Voperating_system_release. */
1439 init_process_emacs (); /* init_display uses add_keyboard_wait_descriptor. */
7db35a48 1440 init_keyboard (); /* This too must precede init_sys_modes. */
0a125897 1441 if (!noninteractive)
7c2fb837 1442 init_display (); /* Determine terminal type. Calls init_sys_modes. */
8bb697c0 1443 init_xdisp ();
6b61353c
KH
1444#ifdef HAVE_WINDOW_SYSTEM
1445 init_fringe ();
6b61353c 1446#endif /* HAVE_WINDOW_SYSTEM */
8bb697c0 1447 init_macros ();
75816372 1448 init_window ();
74d75424 1449 init_font ();
087fc47a 1450
f927c5ae
JB
1451 if (!initialized)
1452 {
e2925360 1453 char *file;
7db35a48 1454 /* Handle -l loadup, args passed by Makefile. */
df6530f8 1455 if (argmatch (argv, argc, "-l", "--load", 3, &file, &skip_args))
d67b4f80 1456 Vtop_level = Fcons (intern_c_string ("load"),
e2925360 1457 Fcons (build_string (file), Qnil));
f927c5ae 1458 /* Unless next switch is -nl, load "loadup.el" first thing. */
956e3c7e 1459 if (! no_loadup)
d67b4f80 1460 Vtop_level = Fcons (intern_c_string ("load"),
f927c5ae 1461 Fcons (build_string ("loadup.el"), Qnil));
f927c5ae
JB
1462 }
1463
93572b43
KH
1464 if (initialized)
1465 {
0269dedb
RS
1466#ifdef HAVE_TZSET
1467 {
1468 /* If the execution TZ happens to be the same as the dump TZ,
1469 change it to some other value and then change it back,
1470 to force the underlying implementation to reload the TZ info.
1471 This is needed on implementations that load TZ info from files,
1472 since the TZ file contents may differ between dump and execution. */
1473 char *tz = getenv ("TZ");
1474 if (tz && !strcmp (tz, dump_tz))
1475 {
1476 ++*tz;
1477 tzset ();
1478 --*tz;
1479 }
1480 }
1481#endif
93572b43
KH
1482 }
1483
ecf783fa
EZ
1484 /* Set up for profiling. This is known to work on FreeBSD,
1485 GNU/Linux and MinGW. It might work on some other systems too.
1486 Give it a try and tell us if it works on your system. To compile
7b3a82d7 1487 for profiling, use the configure option --enable-profiling. */
5e617bc2 1488#if defined (__FreeBSD__) || defined (GNU_LINUX) || defined (__MINGW32__)
e610ea43
RS
1489#ifdef PROFILING
1490 if (initialized)
1491 {
ecf783fa
EZ
1492#ifdef __MINGW32__
1493 extern unsigned char etext asm ("etext");
1494#else
e610ea43 1495 extern char etext;
ecf783fa 1496#endif
67a5596f 1497
e610ea43 1498 atexit (_mcleanup);
9e4bf381 1499 monstartup ((uintptr_t) __executable_start, (uintptr_t) &etext);
e610ea43
RS
1500 }
1501 else
1502 moncontrol (0);
1503#endif
1504#endif
1505
f927c5ae
JB
1506 initialized = 1;
1507
e7536cff
RS
1508#ifdef LOCALTIME_CACHE
1509 /* Some versions of localtime have a bug. They cache the value of the time
279cc2b8
JB
1510 zone rather than looking it up every time. Since localtime() is
1511 called to bolt the undumping time into the undumped emacs, this
afe9fae9 1512 results in localtime ignoring the TZ environment variable.
7db35a48 1513 This flushes the new TZ value into localtime. */
afe9fae9 1514 tzset ();
e7536cff 1515#endif /* defined (LOCALTIME_CACHE) */
279cc2b8 1516
f927c5ae
JB
1517 /* Enter editor command loop. This never returns. */
1518 Frecursive_edit ();
1519 /* NOTREACHED */
6bbd7a29 1520 return 0;
f927c5ae
JB
1521}
1522\f
081bef73
RS
1523/* Sort the args so we can find the most important ones
1524 at the beginning of argv. */
1525
1526/* First, here's a table of all the standard options. */
1527
1528struct standard_args
1529{
5e2327cf
DN
1530 const char *name;
1531 const char *longname;
081bef73
RS
1532 int priority;
1533 int nargs;
1534};
1535
35f08c38 1536static const struct standard_args standard_args[] =
081bef73 1537{
05922407 1538 { "-version", "--version", 150, 0 },
f63d0028 1539 { "-chdir", "--chdir", 130, 1 },
05922407 1540 { "-t", "--terminal", 120, 1 },
400d6fa9 1541 { "-nw", "--no-window-system", 110, 0 },
05922407
RS
1542 { "-nw", "--no-windows", 110, 0 },
1543 { "-batch", "--batch", 100, 0 },
6e910e07 1544 { "-script", "--script", 100, 1 },
eab2ee89 1545 { "-daemon", "--daemon", 99, 0 },
05922407 1546 { "-help", "--help", 90, 0 },
956e3c7e 1547 { "-nl", "--no-loadup", 70, 0 },
66b7b0fe 1548 { "-nsl", "--no-site-lisp", 65, 0 },
956e3c7e
RS
1549 /* -d must come last before the options handled in startup.el. */
1550 { "-d", "--display", 60, 1 },
1551 { "-display", 0, 60, 1 },
4fe22cdf 1552 /* Now for the options handled in `command-line' (startup.el). */
66b7b0fe 1553 /* (Note that to imply -nsl, -Q is partially handled here.) */
a1716a57
RS
1554 { "-Q", "--quick", 55, 0 },
1555 { "-quick", 0, 55, 0 },
081bef73
RS
1556 { "-q", "--no-init-file", 50, 0 },
1557 { "-no-init-file", 0, 50, 0 },
1558 { "-no-site-file", "--no-site-file", 40, 0 },
1559 { "-u", "--user", 30, 1 },
1560 { "-user", 0, 30, 1 },
1561 { "-debug-init", "--debug-init", 20, 0 },
f2bc3538 1562 { "-iconic", "--iconic", 15, 0 },
a1716a57 1563 { "-D", "--basic-display", 12, 0},
e09b9180 1564 { "-basic-display", 0, 12, 0},
4fe22cdf
CY
1565 { "-nbc", "--no-blinking-cursor", 12, 0 },
1566 /* Now for the options handled in `command-line-1' (startup.el). */
1567 { "-nbi", "--no-bitmap-icon", 10, 0 },
081bef73
RS
1568 { "-bg", "--background-color", 10, 1 },
1569 { "-background", 0, 10, 1 },
1570 { "-fg", "--foreground-color", 10, 1 },
1571 { "-foreground", 0, 10, 1 },
1572 { "-bd", "--border-color", 10, 1 },
1573 { "-bw", "--border-width", 10, 1 },
1574 { "-ib", "--internal-border", 10, 1 },
1575 { "-ms", "--mouse-color", 10, 1 },
1576 { "-cr", "--cursor-color", 10, 1 },
1577 { "-fn", "--font", 10, 1 },
1578 { "-font", 0, 10, 1 },
94452530
EZ
1579 { "-fs", "--fullscreen", 10, 0 },
1580 { "-fw", "--fullwidth", 10, 0 },
1581 { "-fh", "--fullheight", 10, 0 },
3f1c6666 1582 { "-mm", "--maximized", 10, 0 },
081bef73
RS
1583 { "-g", "--geometry", 10, 1 },
1584 { "-geometry", 0, 10, 1 },
1585 { "-T", "--title", 10, 1 },
ae63ae52 1586 { "-title", 0, 10, 1 },
081bef73
RS
1587 { "-name", "--name", 10, 1 },
1588 { "-xrm", "--xrm", 10, 1 },
1540a61a 1589 { "-parent-id", "--parent-id", 10, 1 },
fcdeb5d9
RS
1590 { "-r", "--reverse-video", 5, 0 },
1591 { "-rv", 0, 5, 0 },
1592 { "-reverse", 0, 5, 0 },
ae63ae52 1593 { "-hb", "--horizontal-scroll-bars", 5, 0 },
fcdeb5d9 1594 { "-vb", "--vertical-scroll-bars", 5, 0 },
d20e1b1e 1595 { "-color", "--color", 5, 0},
16706228
JL
1596 { "-no-splash", "--no-splash", 3, 0 },
1597 { "-no-desktop", "--no-desktop", 3, 0 },
edfda783
AR
1598#ifdef HAVE_NS
1599 { "-NSAutoLaunch", 0, 5, 1 },
1600 { "-NXAutoLaunch", 0, 5, 1 },
1601 { "-disable-font-backend", "--disable-font-backend", 65, 0 },
1602 { "-_NSMachLaunch", 0, 85, 1 },
1603 { "-MachLaunch", 0, 85, 1 },
1604 { "-macosx", 0, 85, 0 },
1605 { "-NSHost", 0, 85, 1 },
1606#endif
fcdeb5d9
RS
1607 /* These have the same priority as ordinary file name args,
1608 so they are not reordered with respect to those. */
4af9e0b3
RS
1609 { "-L", "--directory", 0, 1 },
1610 { "-directory", 0, 0, 1 },
fcdeb5d9
RS
1611 { "-l", "--load", 0, 1 },
1612 { "-load", 0, 0, 1 },
21a4ff8e
GM
1613 /* This has no longname, because using --scriptload confuses sort_args,
1614 because then the --script long option seems to match twice; ie
1615 you can't have a long option which is a prefix of another long
1616 option. In any case, this is entirely an internal option. */
1617 { "-scriptload", NULL, 0, 1 },
fcdeb5d9
RS
1618 { "-f", "--funcall", 0, 1 },
1619 { "-funcall", 0, 0, 1 },
575985b1 1620 { "-eval", "--eval", 0, 1 },
67a5596f 1621 { "-execute", "--execute", 0, 1 },
2e13f8e9
RS
1622 { "-find-file", "--find-file", 0, 1 },
1623 { "-visit", "--visit", 0, 1 },
67a5596f 1624 { "-file", "--file", 0, 1 },
fcdeb5d9 1625 { "-insert", "--insert", 0, 1 },
edfda783
AR
1626#ifdef HAVE_NS
1627 { "-NXOpen", 0, 0, 1 },
1628 { "-NXOpenTemp", 0, 0, 1 },
1629 { "-NSOpen", 0, 0, 1 },
1630 { "-NSOpenTemp", 0, 0, 1 },
1631 { "-GSFilePath", 0, 0, 1 },
1632#endif
f2bc3538 1633 /* This should be processed after ordinary file name args and the like. */
fcdeb5d9 1634 { "-kill", "--kill", -10, 0 },
081bef73
RS
1635};
1636
1637/* Reorder the elements of ARGV (assumed to have ARGC elements)
1638 so that the highest priority ones come first.
1639 Do not change the order of elements of equal priority.
956e3c7e
RS
1640 If an option takes an argument, keep it and its argument together.
1641
1642 If an option that takes no argument appears more
1643 than once, eliminate all but one copy of it. */
081bef73
RS
1644
1645static void
dd4c5104 1646sort_args (int argc, char **argv)
081bef73 1647{
38182d90 1648 char **new = xmalloc (argc * sizeof *new);
081bef73
RS
1649 /* For each element of argv,
1650 the corresponding element of options is:
1651 0 for an option that takes no arguments,
1652 1 for an option that takes one argument, etc.
1653 -1 for an ordinary non-option argument. */
0065d054
PE
1654 int *options = xnmalloc (argc, sizeof *options);
1655 int *priority = xnmalloc (argc, sizeof *priority);
081bef73 1656 int to = 1;
956e3c7e 1657 int incoming_used = 1;
081bef73
RS
1658 int from;
1659 int i;
1660
1661 /* Categorize all the options,
1662 and figure out which argv elts are option arguments. */
1663 for (from = 1; from < argc; from++)
1664 {
1665 options[from] = -1;
fcdeb5d9 1666 priority[from] = 0;
081bef73
RS
1667 if (argv[from][0] == '-')
1668 {
0b963a93 1669 int match;
081bef73 1670
c96f26f4
RS
1671 /* If we have found "--", don't consider
1672 any more arguments as options. */
249443b6 1673 if (argv[from][1] == '-' && argv[from][2] == 0)
c96f26f4
RS
1674 {
1675 /* Leave the "--", and everything following it, at the end. */
1676 for (; from < argc; from++)
1677 {
1678 priority[from] = -100;
1679 options[from] = -1;
1680 }
1681 break;
1682 }
1683
081bef73
RS
1684 /* Look for a match with a known old-fashioned option. */
1685 for (i = 0; i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1686 if (!strcmp (argv[from], standard_args[i].name))
1687 {
1688 options[from] = standard_args[i].nargs;
1689 priority[from] = standard_args[i].priority;
fd76ec52
RS
1690 if (from + standard_args[i].nargs >= argc)
1691 fatal ("Option `%s' requires an argument\n", argv[from]);
081bef73
RS
1692 from += standard_args[i].nargs;
1693 goto done;
1694 }
1695
1696 /* Look for a match with a known long option.
1697 MATCH is -1 if no match so far, -2 if two or more matches so far,
1698 >= 0 (the table index of the match) if just one match so far. */
1699 if (argv[from][1] == '-')
1700 {
0b963a93
PE
1701 char const *equals = strchr (argv[from], '=');
1702 ptrdiff_t thislen =
1703 equals ? equals - argv[from] : strlen (argv[from]);
1704
081bef73 1705 match = -1;
081bef73 1706
f609ef57
KH
1707 for (i = 0;
1708 i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1709 if (standard_args[i].longname
1710 && !strncmp (argv[from], standard_args[i].longname,
1711 thislen))
081bef73
RS
1712 {
1713 if (match == -1)
1714 match = i;
1715 else
1716 match = -2;
1717 }
1718
1719 /* If we found exactly one match, use that. */
1720 if (match >= 0)
1721 {
1722 options[from] = standard_args[match].nargs;
1723 priority[from] = standard_args[match].priority;
1724 /* If --OPTION=VALUE syntax is used,
1725 this option uses just one argv element. */
1726 if (equals != 0)
1727 options[from] = 0;
fd76ec52
RS
1728 if (from + options[from] >= argc)
1729 fatal ("Option `%s' requires an argument\n", argv[from]);
081bef73
RS
1730 from += options[from];
1731 }
a09a5b5b
GM
1732 /* FIXME When match < 0, shouldn't there be some error,
1733 or at least indication to the user that there was a
1734 problem? */
081bef73
RS
1735 }
1736 done: ;
1737 }
1738 }
1739
1740 /* Copy the arguments, in order of decreasing priority, to NEW. */
1741 new[0] = argv[0];
956e3c7e 1742 while (incoming_used < argc)
081bef73
RS
1743 {
1744 int best = -1;
2c70c992 1745 int best_priority = -9999;
081bef73
RS
1746
1747 /* Find the highest priority remaining option.
1748 If several have equal priority, take the first of them. */
1749 for (from = 1; from < argc; from++)
1750 {
1751 if (argv[from] != 0 && priority[from] > best_priority)
1752 {
1753 best_priority = priority[from];
1754 best = from;
1755 }
1756 /* Skip option arguments--they are tied to the options. */
1757 if (options[from] > 0)
1758 from += options[from];
1759 }
7db35a48 1760
081bef73 1761 if (best < 0)
1088b922 1762 emacs_abort ();
081bef73 1763
956e3c7e
RS
1764 /* Copy the highest priority remaining option, with its args, to NEW.
1765 Unless it is a duplicate of the previous one. */
1766 if (! (options[best] == 0
1767 && ! strcmp (new[to - 1], argv[best])))
1768 {
1769 new[to++] = argv[best];
1770 for (i = 0; i < options[best]; i++)
1771 new[to++] = argv[best + i + 1];
1772 }
1773
1774 incoming_used += 1 + (options[best] > 0 ? options[best] : 0);
081bef73
RS
1775
1776 /* Clear out this option in ARGV. */
1777 argv[best] = 0;
1778 for (i = 0; i < options[best]; i++)
1779 argv[best + i + 1] = 0;
1780 }
1781
81b7af72
RS
1782 /* If duplicate options were deleted, fill up extra space with null ptrs. */
1783 while (to < argc)
1784 new[to++] = 0;
1785
72af86bd 1786 memcpy (argv, new, sizeof (char *) * argc);
0bf591da 1787
68db37aa
KS
1788 xfree (options);
1789 xfree (new);
1790 xfree (priority);
081bef73
RS
1791}
1792\f
a7ca3326 1793DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
7db35a48
PJ
1794 doc: /* Exit the Emacs job and kill it.
1795If ARG is an integer, return ARG as the exit program code.
b224de9b 1796If ARG is a string, stuff it as keyboard input.
7db35a48 1797
9c524fcb 1798This function is called upon receipt of the signals SIGTERM
383ebd15 1799or SIGHUP, and upon SIGINT in batch mode.
9c524fcb 1800
7db35a48
PJ
1801The value of `kill-emacs-hook', if not void,
1802is a list of functions (of no args),
1803all of which are called before Emacs is actually killed. */)
5842a27b 1804 (Lisp_Object arg)
f927c5ae 1805{
f927c5ae 1806 struct gcpro gcpro1;
dee091a3 1807 Lisp_Object hook;
8eca8a7c 1808 int exit_code;
f927c5ae
JB
1809
1810 GCPRO1 (arg);
1811
1812 if (feof (stdin))
1813 arg = Qt;
1814
dee091a3
JD
1815 hook = intern ("kill-emacs-hook");
1816 Frun_hooks (1, &hook);
f927c5ae 1817
f927c5ae
JB
1818 UNGCPRO;
1819
1dd3c2d9
CY
1820#ifdef HAVE_X_WINDOWS
1821 /* Transfer any clipboards we own to the clipboard manager. */
1822 x_clipboard_manager_save_all ();
1823#endif
1824
1882aa38 1825 shut_down_emacs (0, STRINGP (arg) ? arg : Qnil);
40be253a 1826
204ee57f
JD
1827#ifdef HAVE_NS
1828 ns_release_autorelease_pool (ns_pool);
1829#endif
1830
58545838
KH
1831 /* If we have an auto-save list file,
1832 kill it because we are exiting Emacs deliberately (not crashing).
1833 Do it after shut_down_emacs, which does an auto-save. */
1834 if (STRINGP (Vauto_save_list_file_name))
42a5b22f 1835 unlink (SSDATA (Vauto_save_list_file_name));
58545838 1836
5895d7b9
PE
1837 if (INTEGERP (arg))
1838 exit_code = (XINT (arg) < 0
1839 ? XINT (arg) | INT_MIN
1840 : XINT (arg) & INT_MAX);
1841 else if (noninteractive && (fflush (stdout) || ferror (stdout)))
8eca8a7c 1842 exit_code = EXIT_FAILURE;
d311d28c 1843 else
5895d7b9
PE
1844 exit_code = EXIT_SUCCESS;
1845 exit (exit_code);
f927c5ae 1846}
40be253a
JB
1847
1848
1849/* Perform an orderly shutdown of Emacs. Autosave any modified
1850 buffers, kill any child processes, clean up the terminal modes (if
1851 we're in the foreground), and other stuff like that. Don't perform
1852 any redisplay; this may be called when Emacs is shutting down in
1853 the background, or after its X connection has died.
1854
1855 If SIG is a signal number, print a message for it.
1856
1857 This is called by fatal signal handlers, X protocol error handlers,
1858 and Fkill_emacs. */
f7ab4e3d 1859
40be253a 1860void
1882aa38 1861shut_down_emacs (int sig, Lisp_Object stuff)
40be253a 1862{
829d872b
RS
1863 /* Prevent running of hooks from now on. */
1864 Vrun_hooks = Qnil;
1865
9db03f6c
KS
1866 /* Don't update display from now on. */
1867 Vinhibit_redisplay = Qt;
1868
7db35a48 1869 /* If we are controlling the terminal, reset terminal modes. */
a7ebc409 1870#ifndef DOS_NT
40be253a 1871 {
d04d81d2 1872 int pgrp = EMACS_GETPGRP (0);
12e610e8
DN
1873 int tpgrp = tcgetpgrp (0);
1874 if ((tpgrp != -1) && tpgrp == pgrp)
40be253a 1875 {
28d440ab 1876 reset_all_sys_modes ();
40be253a 1877 if (sig && sig != SIGTERM)
01108e3f 1878 {
703342f8
PE
1879 static char const format[] = "Fatal error %d: ";
1880 char buf[sizeof format - 2 + INT_STRLEN_BOUND (int)];
1881 int buflen = sprintf (buf, format, sig);
1882 char const *sig_desc = strsignal (sig);
01108e3f 1883 ignore_value (write (STDERR_FILENO, buf, buflen));
703342f8 1884 ignore_value (write (STDERR_FILENO, sig_desc, strlen (sig_desc)));
01108e3f 1885 }
40be253a
JB
1886 }
1887 }
1888#else
1889 fflush (stdout);
0a125897 1890 reset_all_sys_modes ();
40be253a
JB
1891#endif
1892
f7ab4e3d
RS
1893 stuff_buffered_input (stuff);
1894
92d835a4 1895 inhibit_sentinels = 1;
40be253a
JB
1896 kill_buffer_processes (Qnil);
1897 Fdo_auto_save (Qt, Qnil);
1898
1899#ifdef CLASH_DETECTION
1900 unlock_all_files ();
1901#endif
1902
40be253a
JB
1903 /* There is a tendency for a SIGIO signal to arrive within exit,
1904 and cause a SIGHUP because the input descriptor is already closed. */
1905 unrequest_sigio ();
4a4bbad2 1906 ignore_sigio ();
41f339d4
RS
1907
1908#ifdef WINDOWSNT
16b22fef 1909 term_ntproc (0);
41f339d4 1910#endif
d546e578 1911
cd8d4168
GM
1912 /* Do this only if terminating normally, we want glyph matrices
1913 etc. in a core dump. */
200f868e 1914 if (sig == 0 || sig == SIGTERM)
cd8d4168
GM
1915 {
1916 check_glyph_memory ();
1917 check_message_stack ();
1918 }
90d920b6 1919
d546e578
EZ
1920#ifdef MSDOS
1921 dos_cleanup ();
1922#endif
edfda783
AR
1923
1924#ifdef HAVE_NS
1925 ns_term_shutdown (sig);
1926#endif
fcb901a7
LMI
1927
1928#ifdef HAVE_LIBXML2
9078ead6 1929 xml_cleanup_parser ();
fcb901a7 1930#endif
40be253a
JB
1931}
1932
1933
f927c5ae
JB
1934\f
1935#ifndef CANNOT_DUMP
f927c5ae 1936
ce701a33 1937#include "unexec.h"
dd5ecd6b 1938
f927c5ae 1939DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
7db35a48
PJ
1940 doc: /* Dump current state of Emacs into executable file FILENAME.
1941Take symbols from SYMFILE (presumably the file you executed to run Emacs).
1942This is used in the file `loadup.el' when building Emacs.
1943
1944You must run Emacs in batch mode in order to dump it. */)
5842a27b 1945 (Lisp_Object filename, Lisp_Object symfile)
f927c5ae 1946{
f927c5ae 1947 Lisp_Object tem;
1b7ddf4f 1948 Lisp_Object symbol;
d311d28c 1949 ptrdiff_t count = SPECPDL_INDEX ();
4fab758d
GM
1950
1951 check_pure_size ();
f927c5ae 1952
87a98b1a
RS
1953 if (! noninteractive)
1954 error ("Dumping Emacs works only in batch mode");
1955
848a925e 1956#ifdef GNU_LINUX
c339dc2e
PE
1957
1958 /* Warn if the gap between BSS end and heap start is larger than this. */
1959# define MAX_HEAP_BSS_DIFF (1024*1024)
1960
dede2792
JD
1961 if (heap_bss_diff > MAX_HEAP_BSS_DIFF)
1962 {
1963 fprintf (stderr, "**************************************************\n");
1964 fprintf (stderr, "Warning: Your system has a gap between BSS and the\n");
b312a492 1965 fprintf (stderr, "heap (%"pMu" bytes). This usually means that exec-shield\n",
c37caf9d
JD
1966 heap_bss_diff);
1967 fprintf (stderr, "or something similar is in effect. The dump may\n");
e79beb56 1968 fprintf (stderr, "fail because of this. See the section about\n");
c37caf9d 1969 fprintf (stderr, "exec-shield in etc/PROBLEMS for more information.\n");
dede2792
JD
1970 fprintf (stderr, "**************************************************\n");
1971 }
848a925e 1972#endif /* GNU_LINUX */
dede2792 1973
1b7ddf4f
RS
1974 /* Bind `command-line-processed' to nil before dumping,
1975 so that the dumped Emacs will process its command line
1976 and set up to work with X windows if appropriate. */
4aaa3607 1977 symbol = intern ("command-line-processed");
1b7ddf4f
RS
1978 specbind (symbol, Qnil);
1979
b7826503 1980 CHECK_STRING (filename);
c9aae259
EN
1981 filename = Fexpand_file_name (filename, Qnil);
1982 if (!NILP (symfile))
f927c5ae 1983 {
b7826503 1984 CHECK_STRING (symfile);
d5db4077 1985 if (SCHARS (symfile))
c9aae259 1986 symfile = Fexpand_file_name (symfile, Qnil);
f927c5ae
JB
1987 }
1988
1989 tem = Vpurify_flag;
1990 Vpurify_flag = Qnil;
1991
0269dedb
RS
1992#ifdef HAVE_TZSET
1993 set_time_zone_rule (dump_tz);
1994#ifndef LOCALTIME_CACHE
1995 /* Force a tz reload, since set_time_zone_rule doesn't. */
1996 tzset ();
1997#endif
1998#endif
1999
f927c5ae 2000 fflush (stdout);
7db35a48 2001 /* Tell malloc where start of impure now is. */
f927c5ae
JB
2002 /* Also arrange for warnings when nearly out of space. */
2003#ifndef SYSTEM_MALLOC
cc5f52cb
RS
2004#ifndef WINDOWSNT
2005 /* On Windows, this was done before dumping, and that once suffices.
2006 Meanwhile, my_edata is not valid on Windows. */
c339dc2e
PE
2007 {
2008 extern char my_edata[];
2009 memory_warnings (my_edata, malloc_warning);
2010 }
cc5f52cb 2011#endif /* not WINDOWSNT */
dd5ecd6b 2012#endif /* not SYSTEM_MALLOC */
15aaf1b5
RS
2013#ifdef DOUG_LEA_MALLOC
2014 malloc_state_ptr = malloc_get_state ();
f927c5ae 2015#endif
2b7377ca 2016
a74c5ec1 2017#ifdef USE_MMAP_FOR_BUFFERS
2b7377ca
GM
2018 mmap_set_vars (0);
2019#endif
42a5b22f 2020 unexec (SSDATA (filename), !NILP (symfile) ? SSDATA (symfile) : 0);
a74c5ec1 2021#ifdef USE_MMAP_FOR_BUFFERS
2b7377ca
GM
2022 mmap_set_vars (1);
2023#endif
15aaf1b5
RS
2024#ifdef DOUG_LEA_MALLOC
2025 free (malloc_state_ptr);
2026#endif
f927c5ae
JB
2027
2028 Vpurify_flag = tem;
2029
1b7ddf4f 2030 return unbind_to (count, Qnil);
f927c5ae
JB
2031}
2032
f927c5ae
JB
2033#endif /* not CANNOT_DUMP */
2034\f
68c45bf0
PE
2035#if HAVE_SETLOCALE
2036/* Recover from setlocale (LC_ALL, ""). */
2037void
d5a3eaaf 2038fixup_locale (void)
68c45bf0 2039{
68c45bf0
PE
2040 /* The Emacs Lisp reader needs LC_NUMERIC to be "C",
2041 so that numbers are read and printed properly for Emacs Lisp. */
2042 setlocale (LC_NUMERIC, "C");
68c45bf0
PE
2043}
2044
0c8559bb
PE
2045/* Set system locale CATEGORY, with previous locale *PLOCALE, to
2046 DESIRED_LOCALE. */
68c45bf0 2047static void
dd4c5104 2048synchronize_locale (int category, Lisp_Object *plocale, Lisp_Object desired_locale)
68c45bf0 2049{
0c8559bb
PE
2050 if (! EQ (*plocale, desired_locale))
2051 {
2052 *plocale = desired_locale;
2053 setlocale (category, (STRINGP (desired_locale)
51b59d79 2054 ? SSDATA (desired_locale)
0c8559bb
PE
2055 : ""));
2056 }
68c45bf0
PE
2057}
2058
ca9c0567 2059/* Set system time locale to match Vsystem_time_locale, if possible. */
68c45bf0 2060void
d5a3eaaf 2061synchronize_system_time_locale (void)
68c45bf0 2062{
ca9c0567
PE
2063 synchronize_locale (LC_TIME, &Vprevious_system_time_locale,
2064 Vsystem_time_locale);
68c45bf0
PE
2065}
2066
ca9c0567
PE
2067/* Set system messages locale to match Vsystem_messages_locale, if
2068 possible. */
68c45bf0 2069void
d5a3eaaf 2070synchronize_system_messages_locale (void)
68c45bf0
PE
2071{
2072#ifdef LC_MESSAGES
ca9c0567
PE
2073 synchronize_locale (LC_MESSAGES, &Vprevious_system_messages_locale,
2074 Vsystem_messages_locale);
68c45bf0
PE
2075#endif
2076}
2077#endif /* HAVE_SETLOCALE */
2078\f
f927c5ae
JB
2079
2080Lisp_Object
a8fe7202 2081decode_env_path (const char *evarname, const char *defalt)
f927c5ae 2082{
a8fe7202 2083 const char *path, *p;
213d0b1f 2084 Lisp_Object lpath, element, tem;
76151e2c 2085#ifdef WINDOWSNT
1882aa38 2086 bool defaulted = 0;
76151e2c
EZ
2087 const char *emacs_dir = egetenv ("emacs_dir");
2088 static const char *emacs_dir_env = "%emacs_dir%/";
2089 const size_t emacs_dir_len = strlen (emacs_dir_env);
2090#endif
f927c5ae 2091
2447c626
JB
2092 /* It's okay to use getenv here, because this function is only used
2093 to initialize variables when Emacs starts up, and isn't called
2094 after that. */
e065a56e 2095 if (evarname != 0)
a8fe7202 2096 path = getenv (evarname);
e065a56e
JB
2097 else
2098 path = 0;
f927c5ae 2099 if (!path)
76151e2c
EZ
2100 {
2101 path = defalt;
8671676c 2102#ifdef WINDOWSNT
76151e2c 2103 defaulted = 1;
8671676c 2104#endif
76151e2c 2105 }
6a30e6d6
RS
2106#ifdef DOS_NT
2107 /* Ensure values from the environment use the proper directory separator. */
2108 if (path)
2109 {
a8fe7202
AS
2110 char *path_copy = alloca (strlen (path) + 1);
2111 strcpy (path_copy, path);
2112 dostounix_filename (path_copy);
2113 path = path_copy;
6a30e6d6
RS
2114 }
2115#endif
f927c5ae
JB
2116 lpath = Qnil;
2117 while (1)
2118 {
8966b757 2119 p = strchr (path, SEPCHAR);
a8fe7202
AS
2120 if (!p)
2121 p = path + strlen (path);
213d0b1f
RS
2122 element = (p - path ? make_string (path, p - path)
2123 : build_string ("."));
76151e2c
EZ
2124#ifdef WINDOWSNT
2125 /* Relative file names in the default path are interpreted as
2126 being relative to $emacs_dir. */
2127 if (emacs_dir && defaulted
2128 && strncmp (path, emacs_dir_env, emacs_dir_len) == 0)
2129 element = Fexpand_file_name (Fsubstring (element,
2130 make_number (emacs_dir_len),
2131 Qnil),
2132 build_string (emacs_dir));
2133#endif
213d0b1f
RS
2134
2135 /* Add /: to the front of the name
2136 if it would otherwise be treated as magic. */
2137 tem = Ffind_file_name_handler (element, Qt);
ca3df2d5
RS
2138
2139 /* However, if the handler says "I'm safe",
2140 don't bother adding /:. */
2141 if (SYMBOLP (tem))
2142 {
2143 Lisp_Object prop;
2144 prop = Fget (tem, intern ("safe-magic"));
2145 if (! NILP (prop))
2146 tem = Qnil;
2147 }
2148
213d0b1f
RS
2149 if (! NILP (tem))
2150 element = concat2 (build_string ("/:"), element);
2151
2152 lpath = Fcons (element, lpath);
f927c5ae
JB
2153 if (*p)
2154 path = p + 1;
2155 else
2156 break;
2157 }
2158 return Fnreverse (lpath);
2159}
2160
eab2ee89 2161DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0,
4ff029f6
DN
2162 doc: /* Return non-nil if the current emacs process is a daemon.
2163If the daemon was given a name argument, return that name. */)
5842a27b 2164 (void)
eab2ee89 2165{
fc012771 2166 if (IS_DAEMON)
4ff029f6
DN
2167 if (daemon_name)
2168 return build_string (daemon_name);
2169 else
2170 return Qt;
2171 else
2172 return Qnil;
eab2ee89
DN
2173}
2174
5790ef40 2175DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0,
fc012771
SM
2176 doc: /* Mark the Emacs daemon as being initialized.
2177This finishes the daemonization process by doing the other half of detaching
2178from the parent process and its tty file descriptors. */)
5842a27b 2179 (void)
5790ef40
DN
2180{
2181 int nfd;
1882aa38 2182 bool err = 0;
5790ef40 2183
fc012771 2184 if (!IS_DAEMON)
5790ef40
DN
2185 error ("This function can only be called if emacs is run as a daemon");
2186
2187 if (daemon_pipe[1] < 0)
2188 error ("The daemon has already been initialized");
2189
2190 if (NILP (Vafter_init_time))
2191 error ("This function can only be called after loading the init files");
2192
2193 /* Get rid of stdin, stdout and stderr. */
29dadb54 2194 nfd = open ("/dev/null", O_RDWR);
dc1ca6a8
PE
2195 err |= nfd < 0;
2196 err |= dup2 (nfd, 0) < 0;
2197 err |= dup2 (nfd, 1) < 0;
2198 err |= dup2 (nfd, 2) < 0;
2199 err |= close (nfd) != 0;
5790ef40 2200
fc012771
SM
2201 /* Closing the pipe will notify the parent that it can exit.
2202 FIXME: In case some other process inherited the pipe, closing it here
2203 won't notify the parent because it's still open elsewhere, so we
2204 additionally send a byte, just to make sure the parent really exits.
2205 Instead, we should probably close the pipe in start-process and
2206 call-process to make sure the pipe is never inherited by
2207 subprocesses. */
dc1ca6a8
PE
2208 err |= write (daemon_pipe[1], "\n", 1) < 0;
2209 err |= close (daemon_pipe[1]) != 0;
5790ef40
DN
2210 /* Set it to an invalid value so we know we've already run this function. */
2211 daemon_pipe[1] = -1;
dc1ca6a8
PE
2212
2213 if (err)
2214 error ("I/O error during daemon initialization");
5790ef40
DN
2215 return Qt;
2216}
2217
dfcf069d 2218void
d5a3eaaf 2219syms_of_emacs (void)
f927c5ae 2220{
cd3520a4
JB
2221 DEFSYM (Qfile_name_handler_alist, "file-name-handler-alist");
2222 DEFSYM (Qrisky_local_variable, "risky-local-variable");
6c07aac2 2223 DEFSYM (Qkill_emacs, "kill-emacs");
213d0b1f 2224
83591e66 2225#ifndef CANNOT_DUMP
f927c5ae
JB
2226 defsubr (&Sdump_emacs);
2227#endif
2228
2229 defsubr (&Skill_emacs);
2230
59653951 2231 defsubr (&Sinvocation_name);
ace40a69 2232 defsubr (&Sinvocation_directory);
eab2ee89 2233 defsubr (&Sdaemonp);
5790ef40 2234 defsubr (&Sdaemon_initialized);
59653951 2235
29208e82 2236 DEFVAR_LISP ("command-line-args", Vcommand_line_args,
2a9d2ed6
RS
2237 doc: /* Args passed by shell to Emacs, as a list of strings.
2238Many arguments are deleted from the list as they are processed. */);
f927c5ae 2239
29208e82 2240 DEFVAR_LISP ("system-type", Vsystem_type,
cf59a374 2241 doc: /* The value is a symbol indicating the type of operating system you are using.
40a8993a 2242Special values:
cf59a374
GM
2243 `gnu' compiled for a GNU Hurd system.
2244 `gnu/linux' compiled for a GNU/Linux system.
2245 `gnu/kfreebsd' compiled for a GNU system with a FreeBSD kernel.
2246 `darwin' compiled for Darwin (GNU-Darwin, Mac OS X, ...).
2247 `ms-dos' compiled as an MS-DOS application.
2248 `windows-nt' compiled as a native W32 application.
2249 `cygwin' compiled using the Cygwin library.
bd6bc222
GM
2250Anything else (in Emacs 24.1, the possibilities are: aix, berkeley-unix,
2251hpux, irix, usg-unix-v) indicates some sort of Unix system. */);
d67b4f80 2252 Vsystem_type = intern_c_string (SYSTEM_TYPE);
6edd2f3d 2253 /* See configure.ac (and config.nt) for the possible SYSTEM_TYPEs. */
f927c5ae 2254
29208e82 2255 DEFVAR_LISP ("system-configuration", Vsystem_configuration,
7db35a48
PJ
2256 doc: /* Value is string indicating configuration Emacs was built for.
2257On MS-Windows, the value reflects the OS flavor and version on which
2258Emacs is running. */);
f7511647 2259 Vsystem_configuration = build_string (EMACS_CONFIGURATION);
271c7b7c 2260
29208e82 2261 DEFVAR_LISP ("system-configuration-options", Vsystem_configuration_options,
7db35a48 2262 doc: /* String containing the configuration options Emacs was built with. */);
f0fc0b1a
KH
2263 Vsystem_configuration_options = build_string (EMACS_CONFIG_OPTIONS);
2264
29208e82 2265 DEFVAR_BOOL ("noninteractive", noninteractive1,
7db35a48 2266 doc: /* Non-nil means Emacs is running without interactive terminal. */);
e5d77022 2267
29208e82 2268 DEFVAR_LISP ("kill-emacs-hook", Vkill_emacs_hook,
e5d9c0d1 2269 doc: /* Hook run when `kill-emacs' is called.
d77b70e5 2270Since `kill-emacs' may be invoked when the terminal is disconnected (or
7db35a48
PJ
2271in other similar situations), functions placed on this hook should not
2272expect to be able to interact with the user. To ask for confirmation,
d77b70e5
MR
2273see `kill-emacs-query-functions' instead.
2274
9c524fcb
GM
2275Before Emacs 24.1, the hook was not run in batch mode, i.e., if
2276`noninteractive' was non-nil. */);
edc8ae07 2277 Vkill_emacs_hook = Qnil;
3005da00 2278
29208e82 2279 DEFVAR_LISP ("path-separator", Vpath_separator,
c7ed3276
EZ
2280 doc: /* String containing the character that separates directories in
2281search paths, such as PATH and other similar environment variables. */);
074a066b
GV
2282 {
2283 char c = SEPCHAR;
2284 Vpath_separator = make_string (&c, 1);
2285 }
59653951 2286
29208e82 2287 DEFVAR_LISP ("invocation-name", Vinvocation_name,
7db35a48
PJ
2288 doc: /* The program name that was used to run Emacs.
2289Any directory names are omitted. */);
f67de86f 2290
29208e82 2291 DEFVAR_LISP ("invocation-directory", Vinvocation_directory,
7db35a48
PJ
2292 doc: /* The directory in which the Emacs executable was found, to run it.
2293The value is nil if that directory's name is not known. */);
f67de86f 2294
29208e82 2295 DEFVAR_LISP ("installation-directory", Vinstallation_directory,
7db35a48 2296 doc: /* A directory within which to look for the `lib-src' and `etc' directories.
74cc8ff9
GM
2297In an installed Emacs, this is normally nil. It is non-nil if
2298both `lib-src' (on MS-DOS, `info') and `etc' directories are found
1e2c5d95
GM
2299within the variable `invocation-directory' or its parent. For example,
2300this is the case when running an uninstalled Emacs executable from its
74cc8ff9 2301build directory. */);
07f4d123 2302 Vinstallation_directory = Qnil;
68c45bf0 2303
29208e82 2304 DEFVAR_LISP ("system-messages-locale", Vsystem_messages_locale,
7db35a48 2305 doc: /* System locale for messages. */);
ca9c0567 2306 Vsystem_messages_locale = Qnil;
68c45bf0 2307
ca9c0567 2308 DEFVAR_LISP ("previous-system-messages-locale",
29208e82 2309 Vprevious_system_messages_locale,
7db35a48 2310 doc: /* Most recently used system locale for messages. */);
ca9c0567 2311 Vprevious_system_messages_locale = Qnil;
68c45bf0 2312
29208e82 2313 DEFVAR_LISP ("system-time-locale", Vsystem_time_locale,
7db35a48 2314 doc: /* System locale for time. */);
ca9c0567 2315 Vsystem_time_locale = Qnil;
68c45bf0 2316
29208e82 2317 DEFVAR_LISP ("previous-system-time-locale", Vprevious_system_time_locale,
7db35a48 2318 doc: /* Most recently used system locale for time. */);
ca9c0567 2319 Vprevious_system_time_locale = Qnil;
90503d96 2320
29208e82 2321 DEFVAR_LISP ("before-init-time", Vbefore_init_time,
90503d96
CY
2322 doc: /* Value of `current-time' before Emacs begins initialization. */);
2323 Vbefore_init_time = Qnil;
2324
29208e82 2325 DEFVAR_LISP ("after-init-time", Vafter_init_time,
90503d96
CY
2326 doc: /* Value of `current-time' after loading the init files.
2327This is nil during initialization. */);
2328 Vafter_init_time = Qnil;
fc012771 2329
29208e82 2330 DEFVAR_BOOL ("inhibit-x-resources", inhibit_x_resources,
9b68317c 2331 doc: /* If non-nil, X resources, Windows Registry settings, and NS defaults are not used. */);
8686ac71
JB
2332 inhibit_x_resources = 0;
2333
29208e82 2334 DEFVAR_LISP ("emacs-copyright", Vemacs_copyright,
8c5ff6dd
KR
2335 doc: /* Short copyright string for this version of Emacs. */);
2336 Vemacs_copyright = build_string (emacs_copyright);
2337
29208e82 2338 DEFVAR_LISP ("emacs-version", Vemacs_version,
8c5ff6dd
KR
2339 doc: /* Version numbers of this version of Emacs. */);
2340 Vemacs_version = build_string (emacs_version);
2341
29208e82 2342 DEFVAR_LISP ("dynamic-library-alist", Vdynamic_library_alist,
2e288d54
JB
2343 doc: /* Alist of dynamic libraries vs external files implementing them.
2344Each element is a list (LIBRARY FILE...), where the car is a symbol
2345representing a supported external library, and the rest are strings giving
2346alternate filenames for that library.
2347
2348Emacs tries to load the library from the files in the order they appear on
2349the list; if none is loaded, the running session of Emacs won't have access
2350to that library.
2351
2352Note that image types `pbm' and `xbm' do not need entries in this variable
2353because they do not depend on external libraries and are always available.
2354
2355Also note that this is not a generic facility for accessing external
2356libraries; only those already known by Emacs will be loaded. */);
2357 Vdynamic_library_alist = Qnil;
2358 Fput (intern_c_string ("dynamic-library-alist"), Qrisky_local_variable, Qt);
2359
fc012771
SM
2360 /* Make sure IS_DAEMON starts up as false. */
2361 daemon_pipe[1] = 0;
f927c5ae 2362}