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