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