(abbreviate-file-name): Don't convert / to ~/ when the home dir is /.
[bpt/emacs.git] / src / emacs.c
CommitLineData
f927c5ae 1/* Fully extensible Emacs, running on Unix, intended for GNU.
be06db9a 2 Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
f927c5ae
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
40be253a 8the Free Software Foundation; either version 2, or (at your option)
f927c5ae
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <signal.h>
22#include <errno.h>
23
18160b98 24#include <config.h>
f927c5ae
JB
25#include <stdio.h>
26
27#include <sys/types.h>
28#include <sys/file.h>
29
30#ifdef VMS
31#include <ssdef.h>
32#endif
33
f927c5ae
JB
34#ifdef BSD
35#include <sys/ioctl.h>
36#endif
37
38#ifdef APOLLO
39#ifndef APOLLO_SR10
40#include <default_acl.h>
41#endif
42#endif
43
f927c5ae
JB
44#include "lisp.h"
45#include "commands.h"
bef79ee4 46#include "intervals.h"
f927c5ae 47
edc8ae07 48#include "systty.h"
8090eb09 49#include "syssignal.h"
6c362a8b 50#include "process.h"
a41f8bed 51
f927c5ae
JB
52#ifndef O_RDWR
53#define O_RDWR 2
54#endif
55
f927c5ae
JB
56/* Command line args from shell, as list of strings */
57Lisp_Object Vcommand_line_args;
58
59653951
JB
59/* The name under which Emacs was invoked, with any leading directory
60 names discarded. */
61Lisp_Object Vinvocation_name;
62
ace40a69
RS
63/* The directory name from which Emacs was invoked. */
64Lisp_Object Vinvocation_directory;
65
e5d77022
JB
66/* Hook run by `kill-emacs' before it does really anything. */
67Lisp_Object Vkill_emacs_hook;
68
f927c5ae
JB
69/* Set nonzero after Emacs has started up the first time.
70 Prevents reinitialization of the Lisp world and keymaps
71 on subsequent starts. */
72int initialized;
73
271c7b7c 74/* Variable whose value is symbol giving operating system type. */
f927c5ae 75Lisp_Object Vsystem_type;
271c7b7c
RS
76
77/* Variable whose value is string giving configuration built for. */
78Lisp_Object Vsystem_configuration;
f927c5ae
JB
79
80/* If non-zero, emacs should not attempt to use an window-specific code,
81 but instead should use the virtual terminal under which it was started */
82int inhibit_window_system;
83
5aa7f46a
JB
84/* If nonzero, set Emacs to run at this priority. This is also used
85 in child_setup and sys_suspend to make sure subshells run at normal
86 priority; Those functions have their own extern declaration. */
3005da00
RS
87int emacs_priority;
88
9ae8f997
JB
89#ifdef BSD
90/* See sysdep.c. */
91extern int inherited_pgroup;
92#endif
93
f927c5ae
JB
94#ifdef HAVE_X_WINDOWS
95/* If non-zero, -d was specified, meaning we're using some window system. */
96int display_arg;
97#endif
98
99/* An address near the bottom of the stack.
100 Tells GC how to save a copy of the stack. */
101char *stack_bottom;
102
103#ifdef HAVE_X_WINDOWS
104extern Lisp_Object Vwindow_system;
105#endif /* HAVE_X_WINDOWS */
106
107#ifdef USG_SHARED_LIBRARIES
108/* If nonzero, this is the place to put the end of the writable segment
109 at startup. */
110
111unsigned int bss_end = 0;
112#endif
113
114/* Nonzero means running Emacs without interactive terminal. */
115
116int noninteractive;
117
118/* Value of Lisp variable `noninteractive'.
119 Normally same as C variable `noninteractive'
120 but nothing terrible happens if user sets this one. */
121
122int noninteractive1;
123\f
124/* Signal code for the fatal signal that was received */
125int fatal_error_code;
126
127/* Nonzero if handling a fatal error already */
128int fatal_error_in_progress;
129
130/* Handle bus errors, illegal instruction, etc. */
2447c626 131SIGTYPE
f927c5ae
JB
132fatal_error_signal (sig)
133 int sig;
134{
f927c5ae
JB
135 fatal_error_code = sig;
136 signal (sig, SIG_DFL);
137
138 /* If fatal error occurs in code below, avoid infinite recursion. */
8090eb09
JB
139 if (! fatal_error_in_progress)
140 {
141 fatal_error_in_progress = 1;
f927c5ae 142
f7ab4e3d 143 shut_down_emacs (sig, 0, Qnil);
8090eb09 144 }
f927c5ae
JB
145
146#ifdef VMS
f927c5ae
JB
147 LIB$STOP (SS$_ABORT);
148#else
8090eb09
JB
149 /* Signal the same code; this time it will really be fatal.
150 Remember that since we're in a signal handler, the signal we're
151 going to send is probably blocked, so we have to unblock it if we
152 want to really receive it. */
29b89fe0 153#ifndef MSDOS
a90538cb 154 sigunblock (sigmask (fatal_error_code));
29b89fe0 155#endif
f927c5ae
JB
156 kill (getpid (), fatal_error_code);
157#endif /* not VMS */
158}
271c7b7c
RS
159
160#ifdef SIGDANGER
161
162/* Handle bus errors, illegal instruction, etc. */
163SIGTYPE
164memory_warning_signal (sig)
165 int sig;
166{
167 signal (sig, memory_warning_signal);
168
169 malloc_warning ("Operating system warns that virtual memory is running low.\n");
170}
171#endif
f927c5ae
JB
172\f
173/* Code for dealing with Lisp access to the Unix command line */
174
175static
176init_cmdargs (argc, argv, skip_args)
177 int argc;
178 char **argv;
179 int skip_args;
180{
181 register int i;
182
cb421be8 183 Vinvocation_name = Ffile_name_nondirectory (build_string (argv[0]));
ace40a69
RS
184 Vinvocation_directory = Ffile_name_directory (build_string (argv[0]));
185 /* If we got no directory in argv[0], search PATH to find where
186 Emacs actually came from. */
187 if (NILP (Vinvocation_directory))
188 {
189 Lisp_Object found;
190 int yes = openp (Vexec_path, Vinvocation_name,
6c362a8b 191 EXEC_SUFFIXES, &found, 1);
e443f843 192 if (yes == 1)
ace40a69
RS
193 Vinvocation_directory = Ffile_name_directory (found);
194 }
59653951 195
f927c5ae
JB
196 Vcommand_line_args = Qnil;
197
198 for (i = argc - 1; i >= 0; i--)
199 {
200 if (i == 0 || i > skip_args)
201 Vcommand_line_args
202 = Fcons (build_string (argv[i]), Vcommand_line_args);
203 }
204}
59653951
JB
205
206DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
207 "Return the program name that was used to run Emacs.\n\
208Any directory names are omitted.")
209 ()
210{
211 return Fcopy_sequence (Vinvocation_name);
212}
213
ace40a69
RS
214DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
215 0, 0, 0,
216 "Return the directory name in which the Emacs executable was located")
217 ()
218{
219 return Fcopy_sequence (Vinvocation_directory);
220}
221
f927c5ae
JB
222\f
223#ifdef VMS
224#ifdef LINK_CRTL_SHARE
225#ifdef SHAREABLE_LIB_BUG
226extern noshare char **environ;
227#endif /* SHAREABLE_LIB_BUG */
228#endif /* LINK_CRTL_SHARE */
229#endif /* VMS */
230
a90538cb 231#ifndef ORDINARY_LINK
efd241cc
RS
232/* We don't include crtbegin.o and crtend.o in the link,
233 so these functions and variables might be missed.
234 Provide dummy definitions to avoid error.
235 (We don't have any real constructors or destructors.) */
236#ifdef __GNUC__
c83a7064 237__do_global_ctors ()
efd241cc 238{}
c83a7064
RS
239__do_global_ctors_aux ()
240{}
33143604
RS
241__do_global_dtors ()
242{}
64c1864a
RS
243/* Linux has a bug in its library; avoid an error. */
244#ifndef LINUX
c83a7064 245char * __CTOR_LIST__[2] = { (char *) (-1), 0 };
64c1864a 246#endif
c83a7064
RS
247char * __DTOR_LIST__[2] = { (char *) (-1), 0 };
248__main ()
efd241cc 249{}
efd241cc 250#endif /* __GNUC__ */
a90538cb 251#endif /* ORDINARY_LINK */
efd241cc 252
f927c5ae
JB
253/* ARGSUSED */
254main (argc, argv, envp)
255 int argc;
256 char **argv;
257 char **envp;
258{
259 char stack_bottom_variable;
260 int skip_args = 0;
261 extern int errno;
262 extern sys_nerr;
be06db9a 263 extern char *strerror ();
f927c5ae
JB
264 extern void malloc_warning ();
265
266/* Map in shared memory, if we are using that. */
267#ifdef HAVE_SHM
268 if (argc > 1 && !strcmp (argv[1], "-nl"))
269 {
270 map_in_data (0);
271 /* The shared memory was just restored, which clobbered this. */
272 skip_args = 1;
273 }
274 else
275 {
276 map_in_data (1);
277 /* The shared memory was just restored, which clobbered this. */
278 skip_args = 0;
279 }
280#endif
281
19a36ec6 282#ifdef NeXT
4b163808 283 extern int malloc_cookie;
19a36ec6
RS
284
285 /* This helps out unexnext.c. */
286 if (initialized)
287 if (malloc_jumpstart (malloc_cookie) != 0)
288 printf ("malloc jumpstart failed!\n");
289#endif /* NeXT */
290
f927c5ae 291#ifdef HAVE_X_WINDOWS
fb8e9847
JB
292 /* Stupid kludge to catch command-line display spec. We can't
293 handle this argument entirely in window system dependent code
294 because we don't even know which window system dependent code
295 to run until we've recognized this argument. */
f927c5ae
JB
296 {
297 int i;
298
299 for (i = 1; (i < argc && ! display_arg); i++)
92381c7c 300 if (!strcmp (argv[i], "-d") || !strcmp (argv[i], "-display"))
f927c5ae
JB
301 display_arg = 1;
302 }
303#endif
304
305#ifdef VMS
306 /* If -map specified, map the data file in */
307 if (argc > 2 && ! strcmp (argv[1], "-map"))
308 {
309 skip_args = 2;
310 mapin_data (argv[2]);
311 }
312
313#ifdef LINK_CRTL_SHARE
314#ifdef SHAREABLE_LIB_BUG
315 /* Bletcherous shared libraries! */
316 if (!stdin)
317 stdin = fdopen (0, "r");
318 if (!stdout)
319 stdout = fdopen (1, "w");
320 if (!stderr)
321 stderr = fdopen (2, "w");
322 if (!environ)
323 environ = envp;
324#endif /* SHAREABLE_LIB_BUG */
325#endif /* LINK_CRTL_SHARE */
326#endif /* VMS */
327
328 /* Record (approximately) where the stack begins. */
329 stack_bottom = &stack_bottom_variable;
330
331#ifdef RUN_TIME_REMAP
332 if (initialized)
333 run_time_remap (argv[0]);
334#endif
335
336#ifdef USG_SHARED_LIBRARIES
337 if (bss_end)
338 brk (bss_end);
339#endif
340
341 clearerr (stdin);
9ae8f997 342
f927c5ae 343#ifdef BSD
e8589b11
RS
344 if (initialized)
345 {
346 inherited_pgroup = EMACS_GETPGRP (0);
347 setpgrp (0, getpid ());
348 }
f927c5ae
JB
349#endif
350
9ae8f997 351
f927c5ae
JB
352#ifdef APOLLO
353#ifndef APOLLO_SR10
354 /* If USE_DOMAIN_ACLS environment variable exists,
355 use ACLs rather than UNIX modes. */
356 if (egetenv ("USE_DOMAIN_ACLS"))
357 default_acl (USE_DEFACL);
358#endif
359#endif /* APOLLO */
360
361#ifndef SYSTEM_MALLOC
362 if (! initialized)
9ac0d9e0
JB
363 {
364 /* Arrange to get warning messages as memory fills up. */
365 memory_warnings (0, malloc_warning);
366
367 /* Arrange to disable interrupt input while malloc and friends are
368 running. */
369 uninterrupt_malloc ();
370 }
f927c5ae
JB
371#endif /* not SYSTEM_MALLOC */
372
29b89fe0
RS
373#ifdef MSDOS
374 /* We do all file input/output as binary files. When we need to translate
375 newlines, we do that manually. */
376 _fmode = O_BINARY;
377 (stdin)->_flag &= ~_IOTEXT;
378 (stdout)->_flag &= ~_IOTEXT;
379 (stderr)->_flag &= ~_IOTEXT;
380#endif /* MSDOS */
381
3005da00
RS
382#ifdef PRIO_PROCESS
383 if (emacs_priority)
5aa7f46a 384 nice (emacs_priority);
f927c5ae 385 setuid (getuid ());
3005da00 386#endif /* PRIO_PROCESS */
f927c5ae 387
f927c5ae
JB
388 inhibit_window_system = 0;
389
4fc0b45b 390 /* Handle the -t switch, which specifies filename to use as terminal */
f927c5ae
JB
391 if (skip_args + 2 < argc && !strcmp (argv[skip_args + 1], "-t"))
392 {
393 int result;
394 skip_args += 2;
395 close (0);
396 close (1);
397 result = open (argv[skip_args], O_RDWR, 2 );
398 if (result < 0)
399 {
be06db9a 400 char *errstring = strerror (errno);
f927c5ae
JB
401 fprintf (stderr, "emacs: %s: %s\n", argv[skip_args], errstring);
402 exit (1);
403 }
404 dup (0);
405 if (! isatty (0))
406 {
407 fprintf (stderr, "emacs: %s: not a tty\n", argv[skip_args]);
408 exit (1);
409 }
410 fprintf (stderr, "Using %s\n", argv[skip_args]);
411#ifdef HAVE_X_WINDOWS
412 inhibit_window_system = 1; /* -t => -nw */
413#endif
414 }
415
416 if (skip_args + 1 < argc
417 && (!strcmp (argv[skip_args + 1], "-nw")))
418 {
419 skip_args += 1;
420 inhibit_window_system = 1;
421 }
422
423/* Handle the -batch switch, which means don't do interactive display. */
424 noninteractive = 0;
425 if (skip_args + 1 < argc && !strcmp (argv[skip_args + 1], "-batch"))
426 {
427 skip_args += 1;
428 noninteractive = 1;
429 }
430
fb8e9847
JB
431#ifdef POSIX_SIGNALS
432 init_signals ();
433#endif
434
f927c5ae
JB
435 if (
436#ifndef CANNOT_DUMP
437 ! noninteractive || initialized
438#else
439 1
440#endif
441 )
442 {
443 /* Don't catch these signals in batch mode if not initialized.
444 On some machines, this sets static data that would make
445 signal fail to work right when the dumped Emacs is run. */
446 signal (SIGHUP, fatal_error_signal);
447 signal (SIGQUIT, fatal_error_signal);
448 signal (SIGILL, fatal_error_signal);
449 signal (SIGTRAP, fatal_error_signal);
a90538cb
JB
450#ifdef SIGIOT
451 /* This is missing on some systems - OS/2, for example. */
f927c5ae 452 signal (SIGIOT, fatal_error_signal);
a90538cb 453#endif
f927c5ae
JB
454#ifdef SIGEMT
455 signal (SIGEMT, fatal_error_signal);
456#endif
457 signal (SIGFPE, fatal_error_signal);
00eaaa32 458#ifdef SIGBUS
f927c5ae 459 signal (SIGBUS, fatal_error_signal);
00eaaa32 460#endif
f927c5ae 461 signal (SIGSEGV, fatal_error_signal);
00eaaa32 462#ifdef SIGSYS
f927c5ae 463 signal (SIGSYS, fatal_error_signal);
00eaaa32 464#endif
f927c5ae
JB
465 signal (SIGTERM, fatal_error_signal);
466#ifdef SIGXCPU
467 signal (SIGXCPU, fatal_error_signal);
468#endif
469#ifdef SIGXFSZ
470 signal (SIGXFSZ, fatal_error_signal);
471#endif /* SIGXFSZ */
472
271c7b7c
RS
473#ifdef SIGDANGER
474 /* This just means available memory is getting low. */
475 signal (SIGDANGER, memory_warning_signal);
476#endif
477
f927c5ae 478#ifdef AIX
f927c5ae
JB
479 signal (20, fatal_error_signal);
480 signal (21, fatal_error_signal);
481 signal (22, fatal_error_signal);
f927c5ae 482 signal (24, fatal_error_signal);
86fb06ae
RS
483#if 0 /* mvn@library.ucla.edu says these are SIGIO on AIX 3.2.4. */
484 signal (23, fatal_error_signal);
bfb61299 485#ifdef SIGIO
f927c5ae
JB
486 signal (SIGAIO, fatal_error_signal);
487 signal (SIGPTY, fatal_error_signal);
bfb61299 488#endif
86fb06ae 489#endif
0aef8561 490#ifndef _I386
f927c5ae 491 signal (SIGIOINT, fatal_error_signal);
0aef8561 492#endif
f927c5ae
JB
493 signal (SIGGRANT, fatal_error_signal);
494 signal (SIGRETRACT, fatal_error_signal);
495 signal (SIGSOUND, fatal_error_signal);
496 signal (SIGMSG, fatal_error_signal);
497#endif /* AIX */
498 }
499
500 noninteractive1 = noninteractive;
501
502/* Perform basic initializations (not merely interning symbols) */
503
504 if (!initialized)
505 {
506 init_alloc_once ();
507 init_obarray ();
508 init_eval_once ();
509 init_syntax_once (); /* Create standard syntax table. */
510 /* Must be done before init_buffer */
511 init_casetab_once ();
512 init_buffer_once (); /* Create buffer table and some buffers */
513 init_minibuf_once (); /* Create list of minibuffers */
514 /* Must precede init_window_once */
515 init_window_once (); /* Init the window system */
516 }
517
518 init_alloc ();
f927c5ae
JB
519 init_eval ();
520 init_data ();
0e956009 521
29b89fe0
RS
522#ifdef MSDOS
523 /* Call early 'cause init_environment needs it. */
524 init_dosfns ();
525 /* Set defaults for several environment variables. */
526 if (initialized) init_environment (argc, argv, skip_args);
527#endif
528
0e956009
JB
529 /* egetenv is a pretty low-level facility, which may get called in
530 many circumstances; it seems flimsy to put off initializing it
531 until calling init_callproc. */
532 set_process_environment ();
93aed04d
RS
533 /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
534 if this is not done. Do it after set_process_environment so that we
535 don't pollute Vprocess_environment. */
536#ifdef AIX
537 putenv ("LANG=C");
538#endif
0e956009 539
ace40a69
RS
540 init_buffer (); /* Init default directory of main buffer */
541
7928f0b5 542 init_callproc_1 (); /* Must precede init_cmdargs and init_sys_modes. */
ace40a69 543 init_cmdargs (argc, argv, skip_args); /* Must precede init_lread. */
7928f0b5 544 init_callproc (); /* Must follow init_cmdargs but not init_sys_modes. */
fb8e9847 545 init_lread ();
f927c5ae 546
f927c5ae
JB
547 if (!noninteractive)
548 {
549#ifdef VMS
1cbd5d9d 550 init_vms_input ();/* init_display calls get_frame_size, that needs this */
f927c5ae
JB
551#endif /* VMS */
552 init_display (); /* Determine terminal type. init_sys_modes uses results */
553 }
554 init_keyboard (); /* This too must precede init_sys_modes */
f927c5ae
JB
555#ifdef VMS
556 init_vmsproc (); /* And this too. */
557#endif /* VMS */
558 init_sys_modes (); /* Init system terminal modes (RAW or CBREAK, etc.) */
559 init_xdisp ();
560 init_macros ();
561 init_editfns ();
562#ifdef LISP_FLOAT_TYPE
563 init_floatfns ();
564#endif
565#ifdef VMS
566 init_vmsfns ();
567#endif /* VMS */
f927c5ae 568 init_process ();
32676c08
JB
569#ifdef CLASH_DETECTION
570 init_filelock ();
571#endif /* CLASH_DETECTION */
f927c5ae
JB
572
573/* Intern the names of all standard functions and variables; define standard keys */
574
575 if (!initialized)
576 {
577 /* The basic levels of Lisp must come first */
578 /* And data must come first of all
579 for the sake of symbols like error-message */
580 syms_of_data ();
581 syms_of_alloc ();
fb8e9847 582 syms_of_lread ();
f927c5ae
JB
583 syms_of_print ();
584 syms_of_eval ();
585 syms_of_fns ();
f927c5ae 586 syms_of_floatfns ();
f927c5ae
JB
587
588 syms_of_abbrev ();
589 syms_of_buffer ();
590 syms_of_bytecode ();
591 syms_of_callint ();
592 syms_of_casefiddle ();
593 syms_of_casetab ();
594 syms_of_callproc ();
595 syms_of_cmds ();
596#ifndef NO_DIR_LIBRARY
597 syms_of_dired ();
598#endif /* not NO_DIR_LIBRARY */
599 syms_of_display ();
600 syms_of_doc ();
601 syms_of_editfns ();
602 syms_of_emacs ();
603 syms_of_fileio ();
604#ifdef CLASH_DETECTION
605 syms_of_filelock ();
606#endif /* CLASH_DETECTION */
607 syms_of_indent ();
608 syms_of_keyboard ();
609 syms_of_keymap ();
610 syms_of_macros ();
611 syms_of_marker ();
612 syms_of_minibuf ();
613 syms_of_mocklisp ();
f927c5ae 614 syms_of_process ();
f927c5ae 615 syms_of_search ();
1cbd5d9d 616 syms_of_frame ();
f927c5ae
JB
617 syms_of_syntax ();
618 syms_of_undo ();
bef79ee4
JA
619
620 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
621 syms_of_textprop ();
f927c5ae
JB
622#ifdef VMS
623 syms_of_vmsproc ();
624#endif /* VMS */
625 syms_of_window ();
626 syms_of_xdisp ();
627#ifdef HAVE_X_WINDOWS
72412588 628 syms_of_xterm ();
f927c5ae 629 syms_of_xfns ();
9c3f23b7 630 syms_of_xfaces ();
72412588
JB
631#ifdef HAVE_X11
632 syms_of_xselect ();
633#endif
dbc4e1c1 634#ifdef HAVE_X_MENU
f927c5ae
JB
635 syms_of_xmenu ();
636#endif /* HAVE_X_MENU */
637#endif /* HAVE_X_WINDOWS */
638
639#ifdef SYMS_SYSTEM
640 SYMS_SYSTEM;
641#endif
642
643#ifdef SYMS_MACHINE
644 SYMS_MACHINE;
645#endif
646
647 keys_of_casefiddle ();
648 keys_of_cmds ();
649 keys_of_buffer ();
650 keys_of_keyboard ();
651 keys_of_keymap ();
652 keys_of_macros ();
653 keys_of_minibuf ();
654 keys_of_window ();
5e67fbc2 655 keys_of_frame ();
f927c5ae
JB
656 }
657
658 if (!initialized)
659 {
660 /* Handle -l loadup-and-dump, args passed by Makefile. */
661 if (argc > 2 + skip_args && !strcmp (argv[1 + skip_args], "-l"))
662 Vtop_level = Fcons (intern ("load"),
663 Fcons (build_string (argv[2 + skip_args]), Qnil));
664#ifdef CANNOT_DUMP
665 /* Unless next switch is -nl, load "loadup.el" first thing. */
666 if (!(argc > 1 + skip_args && !strcmp (argv[1 + skip_args], "-nl")))
667 Vtop_level = Fcons (intern ("load"),
668 Fcons (build_string ("loadup.el"), Qnil));
669#endif /* CANNOT_DUMP */
670 }
671
672 initialized = 1;
673
afe9fae9
RS
674#if defined (sun) || defined (LOCALTIME_CACHE)
675 /* sun's localtime has a bug. it caches the value of the time
279cc2b8
JB
676 zone rather than looking it up every time. Since localtime() is
677 called to bolt the undumping time into the undumped emacs, this
afe9fae9
RS
678 results in localtime ignoring the TZ environment variable.
679 This flushes the new TZ value into localtime. */
680 tzset ();
681#endif /* defined (sun) || defined (LOCALTIME_CACHE) */
279cc2b8 682
f927c5ae
JB
683 /* Enter editor command loop. This never returns. */
684 Frecursive_edit ();
685 /* NOTREACHED */
686}
687\f
688DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
edc8ae07 689 "Exit the Emacs job and kill it.\n\
f927c5ae
JB
690If ARG is an integer, return ARG as the exit program code.\n\
691If ARG is a string, stuff it as keyboard input.\n\n\
692The value of `kill-emacs-hook', if not void,\n\
693is a list of functions (of no args),\n\
694all of which are called before Emacs is actually killed.")
695 (arg)
696 Lisp_Object arg;
697{
698 Lisp_Object hook, hook1;
699 int i;
700 struct gcpro gcpro1;
701
702 GCPRO1 (arg);
703
704 if (feof (stdin))
705 arg = Qt;
706
2447c626 707 if (!NILP (Vrun_hooks) && !noninteractive)
f927c5ae
JB
708 call1 (Vrun_hooks, intern ("kill-emacs-hook"));
709
f927c5ae
JB
710 UNGCPRO;
711
712/* Is it really necessary to do this deassign
713 when we are going to exit anyway? */
714/* #ifdef VMS
715 stop_vms_input ();
716 #endif */
40be253a 717
d0068e25 718 shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
40be253a 719
f927c5ae
JB
720 exit ((XTYPE (arg) == Lisp_Int) ? XINT (arg)
721#ifdef VMS
722 : 1
723#else
724 : 0
725#endif
726 );
727 /* NOTREACHED */
728}
40be253a
JB
729
730
731/* Perform an orderly shutdown of Emacs. Autosave any modified
732 buffers, kill any child processes, clean up the terminal modes (if
733 we're in the foreground), and other stuff like that. Don't perform
734 any redisplay; this may be called when Emacs is shutting down in
735 the background, or after its X connection has died.
736
737 If SIG is a signal number, print a message for it.
738
739 This is called by fatal signal handlers, X protocol error handlers,
740 and Fkill_emacs. */
f7ab4e3d 741
40be253a 742void
f7ab4e3d 743shut_down_emacs (sig, no_x, stuff)
41423a80 744 int sig, no_x;
f7ab4e3d 745 Lisp_Object stuff;
40be253a
JB
746{
747 /* If we are controlling the terminal, reset terminal modes */
748#ifdef EMACS_HAVE_TTY_PGRP
749 {
d04d81d2
RS
750 int pgrp = EMACS_GETPGRP (0);
751
40be253a
JB
752 int tpgrp;
753 if (EMACS_GET_TTY_PGRP (0, &tpgrp) != -1
5a570e37 754 && tpgrp == pgrp)
40be253a
JB
755 {
756 fflush (stdout);
757 reset_sys_modes ();
758 if (sig && sig != SIGTERM)
759 fprintf (stderr, "Fatal error (%d).", sig);
760 }
761 }
762#else
763 fflush (stdout);
764 reset_sys_modes ();
765#endif
766
f7ab4e3d
RS
767 stuff_buffered_input (stuff);
768
40be253a
JB
769 kill_buffer_processes (Qnil);
770 Fdo_auto_save (Qt, Qnil);
771
772#ifdef CLASH_DETECTION
773 unlock_all_files ();
774#endif
775
776#ifdef VMS
777 kill_vms_processes ();
778#endif
779
41423a80
RS
780#ifdef HAVE_X_WINDOWS
781 if (!noninteractive && EQ (Vwindow_system, intern ("x")) && ! no_x)
782 Fx_close_current_connection ();
783#endif /* HAVE_X_WINDOWS */
784
40be253a
JB
785#ifdef SIGIO
786 /* There is a tendency for a SIGIO signal to arrive within exit,
787 and cause a SIGHUP because the input descriptor is already closed. */
788 unrequest_sigio ();
789 signal (SIGIO, SIG_IGN);
790#endif
791}
792
793
f927c5ae
JB
794\f
795#ifndef CANNOT_DUMP
796/* Nothing like this can be implemented on an Apollo.
797 What a loss! */
798
799#ifdef HAVE_SHM
800
801DEFUN ("dump-emacs-data", Fdump_emacs_data, Sdump_emacs_data, 1, 1, 0,
802 "Dump current state of Emacs into data file FILENAME.\n\
803This function exists on systems that use HAVE_SHM.")
804 (intoname)
805 Lisp_Object intoname;
806{
807 extern int my_edata;
808 Lisp_Object tem;
809 extern void malloc_warning ();
810
811 CHECK_STRING (intoname, 0);
812 intoname = Fexpand_file_name (intoname, Qnil);
813
814 tem = Vpurify_flag;
815 Vpurify_flag = Qnil;
816
817 fflush (stdout);
818 /* Tell malloc where start of impure now is */
819 /* Also arrange for warnings when nearly out of space. */
820#ifndef SYSTEM_MALLOC
70eb2c3e 821 memory_warnings (&my_edata, malloc_warning);
f927c5ae
JB
822#endif
823 map_out_data (XSTRING (intoname)->data);
824
825 Vpurify_flag = tem;
826
827 return Qnil;
828}
829
830#else /* not HAVE_SHM */
831
832DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
833 "Dump current state of Emacs into executable file FILENAME.\n\
834Take symbols from SYMFILE (presumably the file you executed to run Emacs).\n\
835This is used in the file `loadup.el' when building Emacs.\n\
836\n\
837Bind `command-line-processed' to nil before dumping,\n\
838if you want the dumped Emacs to process its command line\n\
839and announce itself normally when it is run.")
840 (intoname, symname)
841 Lisp_Object intoname, symname;
842{
843 extern int my_edata;
844 Lisp_Object tem;
845 extern void malloc_warning ();
846
847 CHECK_STRING (intoname, 0);
848 intoname = Fexpand_file_name (intoname, Qnil);
2447c626 849 if (!NILP (symname))
f927c5ae
JB
850 {
851 CHECK_STRING (symname, 0);
852 if (XSTRING (symname)->size)
853 symname = Fexpand_file_name (symname, Qnil);
854 }
855
856 tem = Vpurify_flag;
857 Vpurify_flag = Qnil;
858
859 fflush (stdout);
860#ifdef VMS
861 mapout_data (XSTRING (intoname)->data);
862#else
863 /* Tell malloc where start of impure now is */
864 /* Also arrange for warnings when nearly out of space. */
865#ifndef SYSTEM_MALLOC
70eb2c3e 866 memory_warnings (&my_edata, malloc_warning);
f927c5ae
JB
867#endif
868 unexec (XSTRING (intoname)->data,
2447c626 869 !NILP (symname) ? XSTRING (symname)->data : 0, &my_edata, 0, 0);
f927c5ae
JB
870#endif /* not VMS */
871
872 Vpurify_flag = tem;
873
874 return Qnil;
875}
876
877#endif /* not HAVE_SHM */
878
879#endif /* not CANNOT_DUMP */
880\f
4b163808 881#ifndef SEPCHAR
f927c5ae
JB
882#define SEPCHAR ':'
883#endif
884
885Lisp_Object
886decode_env_path (evarname, defalt)
887 char *evarname, *defalt;
888{
889 register char *path, *p;
890 extern char *index ();
891
892 Lisp_Object lpath;
893
2447c626
JB
894 /* It's okay to use getenv here, because this function is only used
895 to initialize variables when Emacs starts up, and isn't called
896 after that. */
e065a56e
JB
897 if (evarname != 0)
898 path = (char *) getenv (evarname);
899 else
900 path = 0;
f927c5ae
JB
901 if (!path)
902 path = defalt;
903 lpath = Qnil;
904 while (1)
905 {
906 p = index (path, SEPCHAR);
907 if (!p) p = path + strlen (path);
908 lpath = Fcons (p - path ? make_string (path, p - path) : Qnil,
909 lpath);
910 if (*p)
911 path = p + 1;
912 else
913 break;
914 }
915 return Fnreverse (lpath);
916}
917
918syms_of_emacs ()
919{
83591e66 920#ifndef CANNOT_DUMP
f927c5ae
JB
921#ifdef HAVE_SHM
922 defsubr (&Sdump_emacs_data);
923#else
924 defsubr (&Sdump_emacs);
83591e66 925#endif
f927c5ae
JB
926#endif
927
928 defsubr (&Skill_emacs);
929
59653951 930 defsubr (&Sinvocation_name);
ace40a69 931 defsubr (&Sinvocation_directory);
59653951 932
f927c5ae
JB
933 DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
934 "Args passed by shell to Emacs, as a list of strings.");
935
936 DEFVAR_LISP ("system-type", &Vsystem_type,
937 "Value is symbol indicating type of operating system you are using.");
938 Vsystem_type = intern (SYSTEM_TYPE);
939
271c7b7c
RS
940 DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
941 "Value is string indicating configuration Emacs was built for.");
942 Vsystem_configuration = build_string (CONFIGURATION);
943
f927c5ae
JB
944 DEFVAR_BOOL ("noninteractive", &noninteractive1,
945 "Non-nil means Emacs is running without interactive terminal.");
e5d77022 946
e5d77022 947 DEFVAR_LISP ("kill-emacs-hook", &Vkill_emacs_hook,
edc8ae07
JB
948 "Hook to be run whenever kill-emacs is called.\n\
949Since kill-emacs may be invoked when the terminal is disconnected (or\n\
950in other similar situations), functions placed on this hook should not\n\
679aee02 951expect to be able to interact with the user.");
edc8ae07 952 Vkill_emacs_hook = Qnil;
3005da00
RS
953
954 DEFVAR_INT ("emacs-priority", &emacs_priority,
955 "Priority for Emacs to run at.\n\
956This value is effective only if set before Emacs is dumped,\n\
957and only if the Emacs executable is installed with setuid to permit\n\
958it to change priority. (Emacs sets its uid back to the real uid.)");
959 emacs_priority = 0;
59653951
JB
960
961 staticpro (&Vinvocation_name);
962 Vinvocation_name = Qnil;
ace40a69
RS
963 staticpro (&Vinvocation_directory);
964 Vinvocation_directory = Qnil;
f927c5ae 965}