1 /* Fully extensible Emacs, running on Unix, intended for GNU.
2 Copyright (C) 1985, 86, 87, 93, 94, 95 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #include <sys/types.h>
36 #include <sys/ioctl.h>
41 #include "intervals.h"
44 #include "blockinput.h"
45 #include "syssignal.h"
50 #include <sys/resource.h>
57 extern void malloc_warning ();
58 extern void set_time_zone_rule ();
59 extern char *index ();
60 extern char *strerror ();
62 /* Command line args from shell, as list of strings */
63 Lisp_Object Vcommand_line_args
;
65 /* The name under which Emacs was invoked, with any leading directory
67 Lisp_Object Vinvocation_name
;
69 /* The directory name from which Emacs was invoked. */
70 Lisp_Object Vinvocation_directory
;
72 /* The directory name in which to find subdirs such as lisp and etc.
73 nil means get them only from PATH_LOADSEARCH. */
74 Lisp_Object Vinstallation_directory
;
76 /* Hook run by `kill-emacs' before it does really anything. */
77 Lisp_Object Vkill_emacs_hook
;
79 /* Set nonzero after Emacs has started up the first time.
80 Prevents reinitialization of the Lisp world and keymaps
81 on subsequent starts. */
84 /* Variable whose value is symbol giving operating system type. */
85 Lisp_Object Vsystem_type
;
87 /* Variable whose value is string giving configuration built for. */
88 Lisp_Object Vsystem_configuration
;
90 /* Variable whose value is string giving configuration options,
91 for use when reporting bugs. */
92 Lisp_Object Vsystem_configuration_options
;
94 Lisp_Object Qfile_name_handler_alist
;
96 /* If non-zero, emacs should not attempt to use an window-specific code,
97 but instead should use the virtual terminal under which it was started */
98 int inhibit_window_system
;
100 /* If nonzero, set Emacs to run at this priority. This is also used
101 in child_setup and sys_suspend to make sure subshells run at normal
102 priority; Those functions have their own extern declaration. */
105 /* If non-zero a filter or a sentinel is running. Tested to save the match
106 data on the first attempt to change it inside asynchronous code. */
107 int running_asynch_code
;
111 extern int inherited_pgroup
;
114 #ifdef HAVE_X_WINDOWS
115 /* If non-zero, -d was specified, meaning we're using some window system. */
119 /* An address near the bottom of the stack.
120 Tells GC how to save a copy of the stack. */
123 #ifdef HAVE_WINDOW_SYSTEM
124 extern Lisp_Object Vwindow_system
;
125 #endif /* HAVE_WINDOW_SYSTEM */
127 extern Lisp_Object Vauto_save_list_file_name
;
129 #ifdef USG_SHARED_LIBRARIES
130 /* If nonzero, this is the place to put the end of the writable segment
133 unsigned int bss_end
= 0;
136 /* Nonzero means running Emacs without interactive terminal. */
140 /* Value of Lisp variable `noninteractive'.
141 Normally same as C variable `noninteractive'
142 but nothing terrible happens if user sets this one. */
146 /* Save argv and argc. */
150 static void sort_args ();
152 /* Signal code for the fatal signal that was received */
153 int fatal_error_code
;
155 /* Nonzero if handling a fatal error already */
156 int fatal_error_in_progress
;
158 /* Handle bus errors, illegal instruction, etc. */
160 fatal_error_signal (sig
)
163 fatal_error_code
= sig
;
164 signal (sig
, SIG_DFL
);
166 TOTALLY_UNBLOCK_INPUT
;
168 /* If fatal error occurs in code below, avoid infinite recursion. */
169 if (! fatal_error_in_progress
)
171 fatal_error_in_progress
= 1;
173 shut_down_emacs (sig
, 0, Qnil
);
177 LIB$
STOP (SS$_ABORT
);
179 /* Signal the same code; this time it will really be fatal.
180 Remember that since we're in a signal handler, the signal we're
181 going to send is probably blocked, so we have to unblock it if we
182 want to really receive it. */
184 sigunblock (sigmask (fatal_error_code
));
186 kill (getpid (), fatal_error_code
);
192 /* Handler for SIGDANGER. */
194 memory_warning_signal (sig
)
197 signal (sig
, memory_warning_signal
);
199 malloc_warning ("Operating system warns that virtual memory is running low.\n");
201 /* It might be unsafe to call do_auto_save now. */
202 force_auto_save_soon ();
206 /* Code for dealing with Lisp access to the Unix command line */
209 init_cmdargs (argc
, argv
, skip_args
)
215 Lisp_Object name
, dir
, tem
;
216 int count
= specpdl_ptr
- specpdl
;
217 Lisp_Object raw_name
;
222 raw_name
= build_string (argv
[0]);
224 /* Add /: to the front of the name
225 if it would otherwise be treated as magic. */
226 tem
= Ffind_file_name_handler (raw_name
, Qt
);
228 raw_name
= concat2 (build_string ("/:"), raw_name
);
230 Vinvocation_name
= Ffile_name_nondirectory (raw_name
);
231 Vinvocation_directory
= Ffile_name_directory (raw_name
);
233 /* If we got no directory in argv[0], search PATH to find where
234 Emacs actually came from. */
235 if (NILP (Vinvocation_directory
))
238 int yes
= openp (Vexec_path
, Vinvocation_name
,
239 EXEC_SUFFIXES
, &found
, 1);
242 /* Add /: to the front of the name
243 if it would otherwise be treated as magic. */
244 tem
= Ffind_file_name_handler (found
, Qt
);
246 found
= concat2 (build_string ("/:"), found
);
247 Vinvocation_directory
= Ffile_name_directory (found
);
251 if (!NILP (Vinvocation_directory
)
252 && NILP (Ffile_name_absolute_p (Vinvocation_directory
)))
253 /* Emacs was started with relative path, like ./emacs.
255 Vinvocation_directory
= Fexpand_file_name (Vinvocation_directory
, Qnil
);
257 Vinstallation_directory
= Qnil
;
259 if (!NILP (Vinvocation_directory
))
261 dir
= Vinvocation_directory
;
262 name
= Fexpand_file_name (Vinvocation_name
, dir
);
265 Lisp_Object tem
, lib_src_exists
;
266 Lisp_Object etc_exists
, info_exists
;
268 /* See if dir contains subdirs for use by Emacs.
269 Check for the ones that would exist in a build directory,
270 not including lisp and info. */
271 tem
= Fexpand_file_name (build_string ("lib-src"), dir
);
272 lib_src_exists
= Ffile_exists_p (tem
);
274 /* MSDOS installations frequently remove lib-src, but we still
275 must set installation-directory, or else info won't find
276 its files (it uses the value of installation-directory). */
277 tem
= Fexpand_file_name (build_string ("info"), dir
);
278 info_exists
= Ffile_exists_p (tem
);
280 if (!NILP (lib_src_exists
) || !NILP (info_exists
))
282 tem
= Fexpand_file_name (build_string ("etc"), dir
);
283 etc_exists
= Ffile_exists_p (tem
);
284 if (!NILP (etc_exists
))
286 Vinstallation_directory
287 = Ffile_name_as_directory (dir
);
292 /* See if dir's parent contains those subdirs. */
293 tem
= Fexpand_file_name (build_string ("../lib-src"), dir
);
294 lib_src_exists
= Ffile_exists_p (tem
);
296 tem
= Fexpand_file_name (build_string ("../info"), dir
);
297 info_exists
= Ffile_exists_p (tem
);
299 if (!NILP (lib_src_exists
) || !NILP (info_exists
))
301 tem
= Fexpand_file_name (build_string ("../etc"), dir
);
302 etc_exists
= Ffile_exists_p (tem
);
303 if (!NILP (etc_exists
))
305 tem
= Fexpand_file_name (build_string (".."), dir
);
306 Vinstallation_directory
307 = Ffile_name_as_directory (tem
);
312 /* If the Emacs executable is actually a link,
313 next try the dir that the link points into. */
314 tem
= Ffile_symlink_p (name
);
317 name
= Fexpand_file_name (tem
, dir
);
318 dir
= Ffile_name_directory (name
);
325 Vcommand_line_args
= Qnil
;
327 for (i
= argc
- 1; i
>= 0; i
--)
329 if (i
== 0 || i
> skip_args
)
331 = Fcons (build_string (argv
[i
]), Vcommand_line_args
);
334 unbind_to (count
, Qnil
);
337 DEFUN ("invocation-name", Finvocation_name
, Sinvocation_name
, 0, 0, 0,
338 "Return the program name that was used to run Emacs.\n\
339 Any directory names are omitted.")
342 return Fcopy_sequence (Vinvocation_name
);
345 DEFUN ("invocation-directory", Finvocation_directory
, Sinvocation_directory
,
347 "Return the directory name in which the Emacs executable was located")
350 return Fcopy_sequence (Vinvocation_directory
);
355 #ifdef LINK_CRTL_SHARE
356 #ifdef SHARABLE_LIB_BUG
357 extern noshare
char **environ
;
358 #endif /* SHARABLE_LIB_BUG */
359 #endif /* LINK_CRTL_SHARE */
363 /* A valid but unlikely value for the TZ environment value.
364 It is OK (though a bit slower) if the user actually chooses this value. */
365 static char dump_tz
[] = "UtC0";
368 #ifndef ORDINARY_LINK
369 /* We don't include crtbegin.o and crtend.o in the link,
370 so these functions and variables might be missed.
371 Provide dummy definitions to avoid error.
372 (We don't have any real constructors or destructors.) */
374 #ifndef GCC_CTORS_IN_LIBC
377 __do_global_ctors_aux ()
381 /* Linux has a bug in its library; avoid an error. */
383 char * __CTOR_LIST__
[2] = { (char *) (-1), 0 };
385 char * __DTOR_LIST__
[2] = { (char *) (-1), 0 };
386 #endif /* GCC_CTORS_IN_LIBC */
389 #endif /* __GNUC__ */
390 #endif /* ORDINARY_LINK */
392 /* Test whether the next argument in ARGV matches SSTR or a prefix of
393 LSTR (at least MINLEN characters). If so, then if VALPTR is non-null
394 (the argument is supposed to have a value) store in *VALPTR either
395 the next argument or the portion of this one after the equal sign.
396 ARGV is read starting at position *SKIPPTR; this index is advanced
397 by the number of arguments used.
399 Too bad we can't just use getopt for all of this, but we don't have
400 enough information to do it right. */
403 argmatch (argv
, argc
, sstr
, lstr
, minlen
, valptr
, skipptr
)
416 /* Don't access argv[argc]; give up in advance. */
417 if (argc
<= *skipptr
+ 1)
420 arg
= argv
[*skipptr
+1];
423 if (strcmp (arg
, sstr
) == 0)
427 *valptr
= argv
[*skipptr
+2];
434 arglen
= (valptr
!= NULL
&& (p
= index (arg
, '=')) != NULL
435 ? p
- arg
: strlen (arg
));
436 if (lstr
== 0 || arglen
< minlen
|| strncmp (arg
, lstr
, arglen
) != 0)
438 else if (valptr
== NULL
)
449 else if (argv
[*skipptr
+2] != NULL
)
451 *valptr
= argv
[*skipptr
+2];
462 main (argc
, argv
, envp
)
467 char stack_bottom_variable
;
471 #ifdef HAVE_SETRLIMIT
475 #ifdef LINUX_SBRK_BUG
479 sort_args (argc
, argv
);
481 if (argmatch (argv
, argc
, "-version", "--version", 3, NULL
, &skip_args
))
484 tem
= Fsymbol_value (intern ("emacs-version"));
487 fprintf (stderr
, "Invalid value of `emacs-version'\n");
492 printf ("GNU Emacs %s\n", XSTRING (tem
)->data
);
493 printf ("Copyright (C) 1996 Free Software Foundation, Inc.\n");
494 printf ("GNU Emacs comes with ABSOLUTELY NO WARRANTY.\n");
495 printf ("You may redistribute copies of Emacs\n");
496 printf ("under the terms of the GNU General Public License.\n");
497 printf ("For more information about these matters, ");
498 printf ("see the files named COPYING.\n");
503 /* Map in shared memory, if we are using that. */
505 if (argmatch (argv
, argc
, "-nl", "--no-shared-memory", 6, NULL
, &skip_args
))
508 /* The shared memory was just restored, which clobbered this. */
514 /* The shared memory was just restored, which clobbered this. */
521 extern int malloc_cookie
;
522 /* This helps out unexnext.c. */
524 if (malloc_jumpstart (malloc_cookie
) != 0)
525 printf ("malloc jumpstart failed!\n");
530 /* If -map specified, map the data file in */
533 if (argmatch (argv
, argc
, "-map", "--map-data", 3, &mapin_file
, &skip_args
))
537 #ifdef LINK_CRTL_SHARE
538 #ifdef SHARABLE_LIB_BUG
539 /* Bletcherous shared libraries! */
541 stdin
= fdopen (0, "r");
543 stdout
= fdopen (1, "w");
545 stderr
= fdopen (2, "w");
548 #endif /* SHARABLE_LIB_BUG */
549 #endif /* LINK_CRTL_SHARE */
552 #ifdef HAVE_SETRLIMIT
553 /* Extend the stack space available. */
554 if (!getrlimit (RLIMIT_STACK
, &rlim
))
556 rlim
.rlim_cur
= rlim
.rlim_max
;
557 setrlimit (RLIMIT_STACK
, &rlim
);
561 /* Record (approximately) where the stack begins. */
562 stack_bottom
= &stack_bottom_variable
;
564 #ifdef RUN_TIME_REMAP
566 run_time_remap (argv
[0]);
569 #ifdef USG_SHARED_LIBRARIES
571 brk ((void *)bss_end
);
576 #ifndef SYSTEM_MALLOC
579 /* Arrange to get warning messages as memory fills up. */
580 memory_warnings (0, malloc_warning
);
582 /* Arrange to disable interrupt input while malloc and friends are
584 uninterrupt_malloc ();
586 #endif /* not SYSTEM_MALLOC */
589 /* We do all file input/output as binary files. When we need to translate
590 newlines, we do that manually. */
594 if (!isatty (fileno (stdin
)))
595 setmode (fileno (stdin
), O_BINARY
);
596 if (!isatty (fileno (stdout
)))
599 setmode (fileno (stdout
), O_BINARY
);
601 #else /* not __DJGPP__ >= 2 */
602 (stdin
)->_flag
&= ~_IOTEXT
;
603 (stdout
)->_flag
&= ~_IOTEXT
;
604 (stderr
)->_flag
&= ~_IOTEXT
;
605 #endif /* not __DJGPP__ >= 2 */
608 #ifdef SET_EMACS_PRIORITY
610 nice (emacs_priority
);
612 #endif /* SET_EMACS_PRIORITY */
614 #ifdef EXTRA_INITIALIZE
618 inhibit_window_system
= 0;
620 /* Handle the -t switch, which specifies filename to use as terminal */
623 if (argmatch (argv
, argc
, "-t", "--terminal", 4, &term
, &skip_args
))
628 result
= open (term
, O_RDWR
, 2 );
631 char *errstring
= strerror (errno
);
632 fprintf (stderr
, "emacs: %s: %s\n", term
, errstring
);
638 fprintf (stderr
, "emacs: %s: not a tty\n", term
);
641 fprintf (stderr
, "Using %s\n", term
);
642 #ifdef HAVE_WINDOW_SYSTEM
643 inhibit_window_system
= 1; /* -t => -nw */
647 if (argmatch (argv
, argc
, "-nw", "--no-windows", 6, NULL
, &skip_args
))
648 inhibit_window_system
= 1;
650 /* Handle the -batch switch, which means don't do interactive display. */
652 if (argmatch (argv
, argc
, "-batch", "--batch", 5, NULL
, &skip_args
))
655 /* Handle the --help option, which gives a usage message.. */
656 if (argmatch (argv
, argc
, "-help", "--help", 3, NULL
, &skip_args
))
659 Usage: %s [-t term] [--terminal term] [-nw] [--no-windows] [--batch]\n\
660 [-q] [--no-init-file] [-u user] [--user user] [--debug-init]\n\
661 [--version] [--no-site-file]\n\
662 [-f func] [--funcall func] [-l file] [--load file] [--insert file]\n\
663 [+linenum] file-to-visit [--kill]\n\
664 Report bugs to bug-gnu-emacs@prep.ai.mit.edu. First, please see\n\
665 the Bugs section of the Emacs manual or the file BUGS.", argv
[0]);
669 #ifdef HAVE_X_WINDOWS
670 /* Stupid kludge to catch command-line display spec. We can't
671 handle this argument entirely in window system dependent code
672 because we don't even know which window system dependent code
673 to run until we've recognized this argument. */
675 char *displayname
= 0;
677 int count_before
= skip_args
;
679 if (argmatch (argv
, argc
, "-d", "--display", 3, &displayname
, &skip_args
))
681 else if (argmatch (argv
, argc
, "-display", 0, 3, &displayname
, &skip_args
))
684 /* If we have the form --display=NAME,
685 convert it into -d name.
686 This requires inserting a new element into argv. */
687 if (displayname
!= 0 && skip_args
- count_before
== 1)
689 char **new = (char **) xmalloc (sizeof (char *) * (argc
+ 2));
692 for (j
= 0; j
< count_before
+ 1; j
++)
694 new[count_before
+ 1] = "-d";
695 new[count_before
+ 2] = displayname
;
696 for (j
= count_before
+ 2; j
<argc
; j
++)
697 new[j
+ 1] = argv
[j
];
701 /* Change --display to -d, when its arg is separate. */
702 else if (displayname
!= 0 && skip_args
> count_before
703 && argv
[count_before
+ 1][1] == '-')
704 argv
[count_before
+ 1] = "-d";
706 /* Don't actually discard this arg. */
707 skip_args
= count_before
;
711 if (! noninteractive
)
716 inherited_pgroup
= EMACS_GETPGRP (0);
717 setpgrp (0, getpid ());
720 #if defined (USG5) && defined (INTERRUPT_INPUT)
730 /* Don't catch SIGHUP if dumping. */
737 sigblock (sigmask (SIGHUP
));
738 /* In --batch mode, don't catch SIGHUP if already ignored.
739 That makes nohup work. */
741 || signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
742 signal (SIGHUP
, fatal_error_signal
);
743 sigunblock (sigmask (SIGHUP
));
748 ! noninteractive
|| initialized
754 /* Don't catch these signals in batch mode if dumping.
755 On some machines, this sets static data that would make
756 signal fail to work right when the dumped Emacs is run. */
757 signal (SIGQUIT
, fatal_error_signal
);
758 signal (SIGILL
, fatal_error_signal
);
759 signal (SIGTRAP
, fatal_error_signal
);
761 signal (SIGABRT
, fatal_error_signal
);
764 signal (SIGHWE
, fatal_error_signal
);
767 signal (SIGPRE
, fatal_error_signal
);
770 signal (SIGORE
, fatal_error_signal
);
773 signal (SIGUME
, fatal_error_signal
);
776 signal (SIGDLK
, fatal_error_signal
);
779 signal (SIGCPULIM
, fatal_error_signal
);
782 /* This is missing on some systems - OS/2, for example. */
783 signal (SIGIOT
, fatal_error_signal
);
786 signal (SIGEMT
, fatal_error_signal
);
788 signal (SIGFPE
, fatal_error_signal
);
790 signal (SIGBUS
, fatal_error_signal
);
792 signal (SIGSEGV
, fatal_error_signal
);
794 signal (SIGSYS
, fatal_error_signal
);
796 signal (SIGTERM
, fatal_error_signal
);
798 signal (SIGXCPU
, fatal_error_signal
);
801 signal (SIGXFSZ
, fatal_error_signal
);
805 /* This just means available memory is getting low. */
806 signal (SIGDANGER
, memory_warning_signal
);
810 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
811 signal (SIGXCPU
, fatal_error_signal
);
813 signal (SIGIOINT
, fatal_error_signal
);
815 signal (SIGGRANT
, fatal_error_signal
);
816 signal (SIGRETRACT
, fatal_error_signal
);
817 signal (SIGSOUND
, fatal_error_signal
);
818 signal (SIGMSG
, fatal_error_signal
);
822 noninteractive1
= noninteractive
;
824 /* Perform basic initializations (not merely interning symbols) */
831 init_syntax_once (); /* Create standard syntax table. */
832 /* Must be done before init_buffer */
833 init_casetab_once ();
834 init_buffer_once (); /* Create buffer table and some buffers */
835 init_minibuf_once (); /* Create list of minibuffers */
836 /* Must precede init_window_once */
837 init_window_once (); /* Init the window system */
843 running_asynch_code
= 0;
846 /* Call early 'cause init_environment needs it. */
848 /* Set defaults for several environment variables. */
850 init_environment (argc
, argv
, skip_args
);
856 /* Initialize environment from registry settings. */
858 init_ntproc (); /* must precede init_editfns */
861 /* egetenv is a pretty low-level facility, which may get called in
862 many circumstances; it seems flimsy to put off initializing it
863 until calling init_callproc. */
864 set_process_environment ();
865 /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
866 if this is not done. Do it after set_process_environment so that we
867 don't pollute Vprocess_environment. */
872 init_buffer (); /* Init default directory of main buffer */
874 init_callproc_1 (); /* Must precede init_cmdargs and init_sys_modes. */
875 init_cmdargs (argc
, argv
, skip_args
); /* Must precede init_lread. */
879 /* Erase any pre-dump messages in the message log, to avoid confusion */
880 Lisp_Object old_log_max
;
881 old_log_max
= Vmessage_log_max
;
882 XSETFASTINT (Vmessage_log_max
, 0);
883 message_dolog ("", 0, 1);
884 Vmessage_log_max
= old_log_max
;
887 init_callproc (); /* Must follow init_cmdargs but not init_sys_modes. */
893 init_vms_input ();/* init_display calls get_frame_size, that needs this */
895 init_display (); /* Determine terminal type. init_sys_modes uses results */
897 init_keyboard (); /* This too must precede init_sys_modes */
899 init_vmsproc (); /* And this too. */
901 init_sys_modes (); /* Init system terminal modes (RAW or CBREAK, etc.) */
905 #ifdef LISP_FLOAT_TYPE
912 #ifdef CLASH_DETECTION
914 #endif /* CLASH_DETECTION */
916 /* Intern the names of all standard functions and variables; define standard keys */
920 /* The basic levels of Lisp must come first */
921 /* And data must come first of all
922 for the sake of symbols like error-message */
935 syms_of_casefiddle ();
939 #ifndef NO_DIR_LIBRARY
941 #endif /* not NO_DIR_LIBRARY */
947 #ifdef CLASH_DETECTION
949 #endif /* CLASH_DETECTION */
965 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
972 #endif /* WINDOWSNT */
975 #ifdef HAVE_X_WINDOWS
982 #endif /* HAVE_X_WINDOWS */
984 #if defined (MSDOS) && !defined (HAVE_X_WINDOWS)
996 syms_of_w32select ();
998 #endif /* HAVE_NTGUI */
1008 keys_of_casefiddle ();
1011 keys_of_keyboard ();
1022 /* Handle -l loadup-and-dump, args passed by Makefile. */
1023 if (argmatch (argv
, argc
, "-l", "--load", 3, &file
, &skip_args
))
1024 Vtop_level
= Fcons (intern ("load"),
1025 Fcons (build_string (file
), Qnil
));
1027 /* Unless next switch is -nl, load "loadup.el" first thing. */
1028 if (!argmatch (argv
, argc
, "-nl", "--no-loadup", 6, NULL
, &skip_args
))
1029 Vtop_level
= Fcons (intern ("load"),
1030 Fcons (build_string ("loadup.el"), Qnil
));
1031 #endif /* CANNOT_DUMP */
1038 /* If the execution TZ happens to be the same as the dump TZ,
1039 change it to some other value and then change it back,
1040 to force the underlying implementation to reload the TZ info.
1041 This is needed on implementations that load TZ info from files,
1042 since the TZ file contents may differ between dump and execution. */
1043 char *tz
= getenv ("TZ");
1044 if (tz
&& !strcmp (tz
, dump_tz
))
1056 #ifdef LOCALTIME_CACHE
1057 /* Some versions of localtime have a bug. They cache the value of the time
1058 zone rather than looking it up every time. Since localtime() is
1059 called to bolt the undumping time into the undumped emacs, this
1060 results in localtime ignoring the TZ environment variable.
1061 This flushes the new TZ value into localtime. */
1063 #endif /* defined (LOCALTIME_CACHE) */
1065 /* Enter editor command loop. This never returns. */
1070 /* Sort the args so we can find the most important ones
1071 at the beginning of argv. */
1073 /* First, here's a table of all the standard options. */
1075 struct standard_args
1083 struct standard_args standard_args
[] =
1085 { "-version", "--version", 110, 0 },
1086 { "-help", "--help", 110, 0 },
1087 { "-nl", "--no-shared-memory", 100, 0 },
1089 { "-map", "--map-data", 100, 0 },
1091 { "-t", "--terminal", 90, 1 },
1092 { "-d", "--display", 80, 1 },
1093 { "-display", 0, 80, 1 },
1094 { "-nw", "--no-windows", 70, 0 },
1095 { "-batch", "--batch", 60, 0 },
1096 { "-q", "--no-init-file", 50, 0 },
1097 { "-no-init-file", 0, 50, 0 },
1098 { "-no-site-file", "--no-site-file", 40, 0 },
1099 { "-u", "--user", 30, 1 },
1100 { "-user", 0, 30, 1 },
1101 { "-debug-init", "--debug-init", 20, 0 },
1102 { "-i", "--icon-type", 15, 0 },
1103 { "-itype", 0, 15, 0 },
1104 { "-iconic", "--iconic", 15, 0 },
1105 { "-bg", "--background-color", 10, 1 },
1106 { "-background", 0, 10, 1 },
1107 { "-fg", "--foreground-color", 10, 1 },
1108 { "-foreground", 0, 10, 1 },
1109 { "-bd", "--border-color", 10, 1 },
1110 { "-bw", "--border-width", 10, 1 },
1111 { "-ib", "--internal-border", 10, 1 },
1112 { "-ms", "--mouse-color", 10, 1 },
1113 { "-cr", "--cursor-color", 10, 1 },
1114 { "-fn", "--font", 10, 1 },
1115 { "-font", 0, 10, 1 },
1116 { "-g", "--geometry", 10, 1 },
1117 { "-geometry", 0, 10, 1 },
1118 { "-T", "--title", 10, 1 },
1119 { "-title", 0, 10, 1 },
1120 { "-name", "--name", 10, 1 },
1121 { "-xrm", "--xrm", 10, 1 },
1122 { "-r", "--reverse-video", 5, 0 },
1124 { "-reverse", 0, 5, 0 },
1125 { "-hb", "--horizontal-scroll-bars", 5, 0 },
1126 { "-vb", "--vertical-scroll-bars", 5, 0 },
1127 /* These have the same priority as ordinary file name args,
1128 so they are not reordered with respect to those. */
1129 { "-L", "--directory", 0, 1 },
1130 { "-directory", 0, 0, 1 },
1131 { "-l", "--load", 0, 1 },
1132 { "-load", 0, 0, 1 },
1133 { "-f", "--funcall", 0, 1 },
1134 { "-funcall", 0, 0, 1 },
1135 { "-eval", "--eval", 0, 1 },
1136 { "-find-file", "--find-file", 0, 1 },
1137 { "-visit", "--visit", 0, 1 },
1138 { "-insert", "--insert", 0, 1 },
1139 /* This should be processed after ordinary file name args and the like. */
1140 { "-kill", "--kill", -10, 0 },
1143 /* Reorder the elements of ARGV (assumed to have ARGC elements)
1144 so that the highest priority ones come first.
1145 Do not change the order of elements of equal priority.
1146 If an option takes an argument, keep it and its argument together. */
1149 sort_args (argc
, argv
)
1153 char **new = (char **) xmalloc (sizeof (char *) * argc
);
1154 /* For each element of argv,
1155 the corresponding element of options is:
1156 0 for an option that takes no arguments,
1157 1 for an option that takes one argument, etc.
1158 -1 for an ordinary non-option argument. */
1159 int *options
= (int *) xmalloc (sizeof (int) * argc
);
1160 int *priority
= (int *) xmalloc (sizeof (int) * argc
);
1164 int end_of_options
= argc
;
1166 /* Categorize all the options,
1167 and figure out which argv elts are option arguments. */
1168 for (from
= 1; from
< argc
; from
++)
1172 if (argv
[from
][0] == '-')
1177 /* If we have found "--", don't consider
1178 any more arguments as options. */
1179 if (argv
[from
][1] == '-')
1181 /* Leave the "--", and everything following it, at the end. */
1182 for (; from
< argc
; from
++)
1184 priority
[from
] = -100;
1190 /* Look for a match with a known old-fashioned option. */
1191 for (i
= 0; i
< sizeof (standard_args
) / sizeof (standard_args
[0]); i
++)
1192 if (!strcmp (argv
[from
], standard_args
[i
].name
))
1194 options
[from
] = standard_args
[i
].nargs
;
1195 priority
[from
] = standard_args
[i
].priority
;
1196 if (from
+ standard_args
[i
].nargs
>= argc
)
1197 fatal ("Option `%s' requires an argument\n", argv
[from
]);
1198 from
+= standard_args
[i
].nargs
;
1202 /* Look for a match with a known long option.
1203 MATCH is -1 if no match so far, -2 if two or more matches so far,
1204 >= 0 (the table index of the match) if just one match so far. */
1205 if (argv
[from
][1] == '-')
1208 thislen
= strlen (argv
[from
]);
1209 equals
= index (argv
[from
], '=');
1211 thislen
= equals
- argv
[from
];
1214 i
< sizeof (standard_args
) / sizeof (standard_args
[0]); i
++)
1215 if (standard_args
[i
].longname
1216 && !strncmp (argv
[from
], standard_args
[i
].longname
,
1225 /* If we found exactly one match, use that. */
1228 options
[from
] = standard_args
[match
].nargs
;
1229 priority
[from
] = standard_args
[match
].priority
;
1230 /* If --OPTION=VALUE syntax is used,
1231 this option uses just one argv element. */
1234 if (from
+ options
[from
] >= argc
)
1235 fatal ("Option `%s' requires an argument\n", argv
[from
]);
1236 from
+= options
[from
];
1243 /* Copy the arguments, in order of decreasing priority, to NEW. */
1248 int best_priority
= -9999;
1250 /* Find the highest priority remaining option.
1251 If several have equal priority, take the first of them. */
1252 for (from
= 1; from
< argc
; from
++)
1254 if (argv
[from
] != 0 && priority
[from
] > best_priority
)
1256 best_priority
= priority
[from
];
1259 /* Skip option arguments--they are tied to the options. */
1260 if (options
[from
] > 0)
1261 from
+= options
[from
];
1267 /* Copy the highest priority remaining option, with its args, to NEW. */
1268 new[to
++] = argv
[best
];
1269 for (i
= 0; i
< options
[best
]; i
++)
1270 new[to
++] = argv
[best
+ i
+ 1];
1272 /* Clear out this option in ARGV. */
1274 for (i
= 0; i
< options
[best
]; i
++)
1275 argv
[best
+ i
+ 1] = 0;
1278 bcopy (new, argv
, sizeof (char *) * argc
);
1281 DEFUN ("kill-emacs", Fkill_emacs
, Skill_emacs
, 0, 1, "P",
1282 "Exit the Emacs job and kill it.\n\
1283 If ARG is an integer, return ARG as the exit program code.\n\
1284 If ARG is a string, stuff it as keyboard input.\n\n\
1285 The value of `kill-emacs-hook', if not void,\n\
1286 is a list of functions (of no args),\n\
1287 all of which are called before Emacs is actually killed.")
1291 Lisp_Object hook
, hook1
;
1293 struct gcpro gcpro1
;
1300 if (!NILP (Vrun_hooks
) && !noninteractive
)
1301 call1 (Vrun_hooks
, intern ("kill-emacs-hook"));
1305 /* Is it really necessary to do this deassign
1306 when we are going to exit anyway? */
1311 shut_down_emacs (0, 0, STRINGP (arg
) ? arg
: Qnil
);
1313 /* If we have an auto-save list file,
1314 kill it because we are exiting Emacs deliberately (not crashing).
1315 Do it after shut_down_emacs, which does an auto-save. */
1316 if (STRINGP (Vauto_save_list_file_name
))
1317 unlink (XSTRING (Vauto_save_list_file_name
)->data
);
1319 exit (INTEGERP (arg
) ? XINT (arg
)
1330 /* Perform an orderly shutdown of Emacs. Autosave any modified
1331 buffers, kill any child processes, clean up the terminal modes (if
1332 we're in the foreground), and other stuff like that. Don't perform
1333 any redisplay; this may be called when Emacs is shutting down in
1334 the background, or after its X connection has died.
1336 If SIG is a signal number, print a message for it.
1338 This is called by fatal signal handlers, X protocol error handlers,
1342 shut_down_emacs (sig
, no_x
, stuff
)
1346 /* Prevent running of hooks from now on. */
1349 /* If we are controlling the terminal, reset terminal modes */
1350 #ifdef EMACS_HAVE_TTY_PGRP
1352 int pgrp
= EMACS_GETPGRP (0);
1355 if (EMACS_GET_TTY_PGRP (0, &tpgrp
) != -1
1360 if (sig
&& sig
!= SIGTERM
)
1361 fprintf (stderr
, "Fatal error (%d).", sig
);
1369 stuff_buffered_input (stuff
);
1371 kill_buffer_processes (Qnil
);
1372 Fdo_auto_save (Qt
, Qnil
);
1374 #ifdef CLASH_DETECTION
1375 unlock_all_files ();
1379 kill_vms_processes ();
1382 #if 0 /* This triggers a bug in XCloseDisplay and is not needed. */
1383 #ifdef HAVE_X_WINDOWS
1384 /* It's not safe to call intern here. Maybe we are crashing. */
1385 if (!noninteractive
&& SYMBOLP (Vwindow_system
)
1386 && XSYMBOL (Vwindow_system
)->name
->size
== 1
1387 && XSYMBOL (Vwindow_system
)->name
->data
[0] == 'x'
1389 Fx_close_current_connection ();
1390 #endif /* HAVE_X_WINDOWS */
1394 /* There is a tendency for a SIGIO signal to arrive within exit,
1395 and cause a SIGHUP because the input descriptor is already closed. */
1397 signal (SIGIO
, SIG_IGN
);
1411 DEFUN ("dump-emacs-data", Fdump_emacs_data
, Sdump_emacs_data
, 1, 1, 0,
1412 "Dump current state of Emacs into data file FILENAME.\n\
1413 This function exists on systems that use HAVE_SHM.")
1415 Lisp_Object filename
;
1417 extern char my_edata
[];
1420 CHECK_STRING (filename
, 0);
1421 filename
= Fexpand_file_name (filename
, Qnil
);
1424 Vpurify_flag
= Qnil
;
1427 /* Tell malloc where start of impure now is */
1428 /* Also arrange for warnings when nearly out of space. */
1429 #ifndef SYSTEM_MALLOC
1430 memory_warnings (my_edata
, malloc_warning
);
1432 map_out_data (XSTRING (filename
)->data
);
1439 #else /* not HAVE_SHM */
1441 DEFUN ("dump-emacs", Fdump_emacs
, Sdump_emacs
, 2, 2, 0,
1442 "Dump current state of Emacs into executable file FILENAME.\n\
1443 Take symbols from SYMFILE (presumably the file you executed to run Emacs).\n\
1444 This is used in the file `loadup.el' when building Emacs.\n\
1446 Bind `command-line-processed' to nil before dumping,\n\
1447 if you want the dumped Emacs to process its command line\n\
1448 and announce itself normally when it is run.")
1450 Lisp_Object filename
, symfile
;
1452 extern char my_edata
[];
1455 CHECK_STRING (filename
, 0);
1456 filename
= Fexpand_file_name (filename
, Qnil
);
1457 if (!NILP (symfile
))
1459 CHECK_STRING (symfile
, 0);
1460 if (XSTRING (symfile
)->size
)
1461 symfile
= Fexpand_file_name (symfile
, Qnil
);
1465 Vpurify_flag
= Qnil
;
1468 set_time_zone_rule (dump_tz
);
1469 #ifndef LOCALTIME_CACHE
1470 /* Force a tz reload, since set_time_zone_rule doesn't. */
1477 mapout_data (XSTRING (filename
)->data
);
1479 /* Tell malloc where start of impure now is */
1480 /* Also arrange for warnings when nearly out of space. */
1481 #ifndef SYSTEM_MALLOC
1483 /* On Windows, this was done before dumping, and that once suffices.
1484 Meanwhile, my_edata is not valid on Windows. */
1485 memory_warnings (my_edata
, malloc_warning
);
1486 #endif /* not WINDOWSNT */
1488 unexec (XSTRING (filename
)->data
,
1489 !NILP (symfile
) ? XSTRING (symfile
)->data
: 0, my_edata
, 0, 0);
1490 #endif /* not VMS */
1497 #endif /* not HAVE_SHM */
1499 #endif /* not CANNOT_DUMP */
1506 decode_env_path (evarname
, defalt
)
1507 char *evarname
, *defalt
;
1509 register char *path
, *p
;
1510 Lisp_Object lpath
, element
, tem
;
1512 /* It's okay to use getenv here, because this function is only used
1513 to initialize variables when Emacs starts up, and isn't called
1516 path
= (char *) getenv (evarname
);
1524 p
= index (path
, SEPCHAR
);
1525 if (!p
) p
= path
+ strlen (path
);
1526 element
= (p
- path
? make_string (path
, p
- path
)
1527 : build_string ("."));
1529 /* Add /: to the front of the name
1530 if it would otherwise be treated as magic. */
1531 tem
= Ffind_file_name_handler (element
, Qt
);
1533 element
= concat2 (build_string ("/:"), element
);
1535 lpath
= Fcons (element
, lpath
);
1541 return Fnreverse (lpath
);
1546 Qfile_name_handler_alist
= intern ("file-name-handler-alist");
1547 staticpro (&Qfile_name_handler_alist
);
1551 defsubr (&Sdump_emacs_data
);
1553 defsubr (&Sdump_emacs
);
1557 defsubr (&Skill_emacs
);
1559 defsubr (&Sinvocation_name
);
1560 defsubr (&Sinvocation_directory
);
1562 DEFVAR_LISP ("command-line-args", &Vcommand_line_args
,
1563 "Args passed by shell to Emacs, as a list of strings.");
1565 DEFVAR_LISP ("system-type", &Vsystem_type
,
1566 "Value is symbol indicating type of operating system you are using.");
1567 Vsystem_type
= intern (SYSTEM_TYPE
);
1569 DEFVAR_LISP ("system-configuration", &Vsystem_configuration
,
1570 "Value is string indicating configuration Emacs was built for.");
1571 Vsystem_configuration
= build_string (EMACS_CONFIGURATION
);
1573 DEFVAR_LISP ("system-configuration-options", &Vsystem_configuration_options
,
1574 "String containing the configuration options Emacs was built with.");
1575 Vsystem_configuration_options
= build_string (EMACS_CONFIG_OPTIONS
);
1577 DEFVAR_BOOL ("noninteractive", &noninteractive1
,
1578 "Non-nil means Emacs is running without interactive terminal.");
1580 DEFVAR_LISP ("kill-emacs-hook", &Vkill_emacs_hook
,
1581 "Hook to be run whenever kill-emacs is called.\n\
1582 Since kill-emacs may be invoked when the terminal is disconnected (or\n\
1583 in other similar situations), functions placed on this hook should not\n\
1584 expect to be able to interact with the user. To ask for confirmation,\n\
1585 see `kill-emacs-query-functions' instead.");
1586 Vkill_emacs_hook
= Qnil
;
1588 DEFVAR_INT ("emacs-priority", &emacs_priority
,
1589 "Priority for Emacs to run at.\n\
1590 This value is effective only if set before Emacs is dumped,\n\
1591 and only if the Emacs executable is installed with setuid to permit\n\
1592 it to change priority. (Emacs sets its uid back to the real uid.)\n\
1593 Currently, you need to define SET_EMACS_PRIORITY in `config.h'\n\
1594 before you compile Emacs, to enable the code for this feature.");
1597 DEFVAR_LISP ("invocation-name", &Vinvocation_name
,
1598 "The program name that was used to run Emacs.\n\
1599 Any directory names are omitted.");
1601 DEFVAR_LISP ("invocation-directory", &Vinvocation_directory
,
1602 "The directory in which the Emacs executable was found, to run it.\n\
1603 The value is nil if that directory's name is not known.");
1605 DEFVAR_LISP ("installation-directory", &Vinstallation_directory
,
1606 "A directory within which to look for the `lib-src' and `etc' directories.\n\
1607 This is non-nil when we can't find those directories in their standard\n\
1608 installed locations, but we can find them\n\
1609 near where the Emacs executable was found.");
1610 Vinstallation_directory
= Qnil
;