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