(make-autoload): Recognize the new `define-minor-mode'.
[emacs.git] / src / emacs.c
blobe33d3c31d31c1f049f17828ca49230f46e659e43
1 /* Fully extensible Emacs, running on Unix, intended for GNU.
2 Copyright (C) 1985,86,87,93,94,95,97,98,1999 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)
9 any later version.
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. */
22 #include <config.h>
23 #include <signal.h>
24 #include <errno.h>
25 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/file.h>
30 #ifdef VMS
31 #include <ssdef.h>
32 #endif
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 #ifdef BSD_SYSTEM
39 #include <sys/ioctl.h>
40 #endif
42 #include "lisp.h"
43 #include "commands.h"
44 #include "intervals.h"
45 #include "buffer.h"
47 #include "systty.h"
48 #include "blockinput.h"
49 #include "syssignal.h"
50 #include "process.h"
51 #include "termhooks.h"
52 #include "keyboard.h"
54 #ifdef HAVE_SETLOCALE
55 #include <locale.h>
56 #endif
58 #ifdef HAVE_SETRLIMIT
59 #include <sys/time.h>
60 #include <sys/resource.h>
61 #endif
63 #ifndef O_RDWR
64 #define O_RDWR 2
65 #endif
67 extern void malloc_warning ();
68 extern void set_time_zone_rule ();
69 extern char *index ();
71 /* Make these values available in GDB, which doesn't see macros. */
73 EMACS_INT gdb_valbits = VALBITS;
74 EMACS_INT gdb_gctypebits = GCTYPEBITS;
75 EMACS_INT gdb_emacs_intbits = sizeof (EMACS_INT) * BITS_PER_CHAR;
76 #ifdef DATA_SEG_BITS
77 EMACS_INT gdb_data_seg_bits = DATA_SEG_BITS;
78 #else
79 EMACS_INT gdb_data_seg_bits = 0;
80 #endif
82 /* Command line args from shell, as list of strings */
83 Lisp_Object Vcommand_line_args;
85 /* The name under which Emacs was invoked, with any leading directory
86 names discarded. */
87 Lisp_Object Vinvocation_name;
89 /* The directory name from which Emacs was invoked. */
90 Lisp_Object Vinvocation_directory;
92 /* The directory name in which to find subdirs such as lisp and etc.
93 nil means get them only from PATH_LOADSEARCH. */
94 Lisp_Object Vinstallation_directory;
96 /* Hook run by `kill-emacs' before it does really anything. */
97 Lisp_Object Vkill_emacs_hook;
99 #ifdef SIGUSR1
100 /* Hooks for signal USR1 and USR2 handing */
101 Lisp_Object Vsignal_USR1_hook;
102 #ifdef SIGUSR2
103 Lisp_Object Vsignal_USR2_hook;
104 #endif
105 #endif
107 /* Search path separator. */
108 Lisp_Object Vpath_separator;
110 /* Set nonzero after Emacs has started up the first time.
111 Prevents reinitialization of the Lisp world and keymaps
112 on subsequent starts. */
113 int initialized;
115 #ifdef DOUG_LEA_MALLOC
116 /* Preserves a pointer to the memory allocated that copies that
117 static data inside glibc's malloc. */
118 void *malloc_state_ptr;
119 /* From glibc, a routine that returns a copy of the malloc internal state. */
120 extern void *malloc_get_state ();
121 /* From glibc, a routine that overwrites the malloc internal state. */
122 extern void malloc_set_state ();
123 /* Non-zero if the MALLOC_CHECK_ enviroment variable was set while
124 dumping. Used to work around a bug in glibc's malloc. */
125 int malloc_using_checking;
126 #endif
128 /* Variable whose value is symbol giving operating system type. */
129 Lisp_Object Vsystem_type;
131 /* Variable whose value is string giving configuration built for. */
132 Lisp_Object Vsystem_configuration;
134 /* Variable whose value is string giving configuration options,
135 for use when reporting bugs. */
136 Lisp_Object Vsystem_configuration_options;
138 Lisp_Object Qfile_name_handler_alist;
140 /* Current and previous system locales for messages and time. */
141 Lisp_Object Vsystem_messages_locale;
142 Lisp_Object Vprevious_system_messages_locale;
143 Lisp_Object Vsystem_time_locale;
144 Lisp_Object Vprevious_system_time_locale;
146 /* If non-zero, emacs should not attempt to use an window-specific code,
147 but instead should use the virtual terminal under which it was started */
148 int inhibit_window_system;
150 /* If nonzero, set Emacs to run at this priority. This is also used
151 in child_setup and sys_suspend to make sure subshells run at normal
152 priority; Those functions have their own extern declaration. */
153 int emacs_priority;
155 /* If non-zero a filter or a sentinel is running. Tested to save the match
156 data on the first attempt to change it inside asynchronous code. */
157 int running_asynch_code;
159 #ifdef BSD_PGRPS
160 /* See sysdep.c. */
161 extern int inherited_pgroup;
162 #endif
164 #ifdef HAVE_X_WINDOWS
165 /* If non-zero, -d was specified, meaning we're using some window system. */
166 int display_arg;
167 #endif
169 /* An address near the bottom of the stack.
170 Tells GC how to save a copy of the stack. */
171 char *stack_bottom;
173 #ifdef HAVE_WINDOW_SYSTEM
174 extern Lisp_Object Vwindow_system;
175 #endif /* HAVE_WINDOW_SYSTEM */
177 extern Lisp_Object Vauto_save_list_file_name;
179 #ifdef USG_SHARED_LIBRARIES
180 /* If nonzero, this is the place to put the end of the writable segment
181 at startup. */
183 unsigned int bss_end = 0;
184 #endif
186 /* Nonzero means running Emacs without interactive terminal. */
188 int noninteractive;
190 /* Value of Lisp variable `noninteractive'.
191 Normally same as C variable `noninteractive'
192 but nothing terrible happens if user sets this one. */
194 int noninteractive1;
196 /* Save argv and argc. */
197 char **initial_argv;
198 int initial_argc;
200 static void sort_args ();
201 void syms_of_emacs ();
203 /* Signal code for the fatal signal that was received */
204 int fatal_error_code;
206 /* Nonzero if handling a fatal error already */
207 int fatal_error_in_progress;
209 #ifdef SIGUSR1
210 SIGTYPE
211 handle_USR1_signal (sig)
212 int sig;
214 struct input_event buf;
216 buf.kind = user_signal;
217 buf.code = 0;
218 buf.frame_or_window = selected_frame;
219 buf.modifiers = 0;
220 buf.timestamp = 0;
222 kbd_buffer_store_event (&buf);
224 #endif /* SIGUSR1 */
226 #ifdef SIGUSR2
227 SIGTYPE
228 handle_USR2_signal (sig)
229 int sig;
231 struct input_event buf;
233 buf.kind = user_signal;
234 buf.code = 1;
235 buf.frame_or_window = selected_frame;
236 buf.modifiers = 0;
237 buf.timestamp = 0;
239 kbd_buffer_store_event (&buf);
241 #endif /* SIGUSR2 */
243 /* Handle bus errors, illegal instruction, etc. */
244 SIGTYPE
245 fatal_error_signal (sig)
246 int sig;
248 fatal_error_code = sig;
249 signal (sig, SIG_DFL);
251 TOTALLY_UNBLOCK_INPUT;
253 /* If fatal error occurs in code below, avoid infinite recursion. */
254 if (! fatal_error_in_progress)
256 fatal_error_in_progress = 1;
258 shut_down_emacs (sig, 0, Qnil);
261 #ifdef VMS
262 LIB$STOP (SS$_ABORT);
263 #else
264 /* Signal the same code; this time it will really be fatal.
265 Remember that since we're in a signal handler, the signal we're
266 going to send is probably blocked, so we have to unblock it if we
267 want to really receive it. */
268 #ifndef MSDOS
269 sigunblock (sigmask (fatal_error_code));
270 #endif
271 kill (getpid (), fatal_error_code);
272 #endif /* not VMS */
275 #ifdef SIGDANGER
277 /* Handler for SIGDANGER. */
278 SIGTYPE
279 memory_warning_signal (sig)
280 int sig;
282 signal (sig, memory_warning_signal);
284 malloc_warning ("Operating system warns that virtual memory is running low.\n");
286 /* It might be unsafe to call do_auto_save now. */
287 force_auto_save_soon ();
289 #endif
291 /* We define abort, rather than using it from the library,
292 so that GDB can return from a breakpoint here.
293 MSDOS has its own definition on msdos.c */
295 #if ! defined (DOS_NT) && ! defined (NO_ABORT)
297 #ifndef ABORT_RETURN_TYPE
298 #define ABORT_RETURN_TYPE void
299 #endif
301 ABORT_RETURN_TYPE
302 abort ()
304 kill (getpid (), SIGABRT);
305 /* This shouldn't be executed, but it prevents a warning. */
306 exit (1);
308 #endif
311 /* Code for dealing with Lisp access to the Unix command line */
313 static void
314 init_cmdargs (argc, argv, skip_args)
315 int argc;
316 char **argv;
317 int skip_args;
319 register int i;
320 Lisp_Object name, dir, tem;
321 int count = specpdl_ptr - specpdl;
322 Lisp_Object raw_name;
324 initial_argv = argv;
325 initial_argc = argc;
327 raw_name = build_string (argv[0]);
329 /* Add /: to the front of the name
330 if it would otherwise be treated as magic. */
331 tem = Ffind_file_name_handler (raw_name, Qt);
332 if (! NILP (tem))
333 raw_name = concat2 (build_string ("/:"), raw_name);
335 Vinvocation_name = Ffile_name_nondirectory (raw_name);
336 Vinvocation_directory = Ffile_name_directory (raw_name);
338 /* If we got no directory in argv[0], search PATH to find where
339 Emacs actually came from. */
340 if (NILP (Vinvocation_directory))
342 Lisp_Object found;
343 int yes = openp (Vexec_path, Vinvocation_name,
344 EXEC_SUFFIXES, &found, 1);
345 if (yes == 1)
347 /* Add /: to the front of the name
348 if it would otherwise be treated as magic. */
349 tem = Ffind_file_name_handler (found, Qt);
350 if (! NILP (tem))
351 found = concat2 (build_string ("/:"), found);
352 Vinvocation_directory = Ffile_name_directory (found);
356 if (!NILP (Vinvocation_directory)
357 && NILP (Ffile_name_absolute_p (Vinvocation_directory)))
358 /* Emacs was started with relative path, like ./emacs.
359 Make it absolute. */
360 Vinvocation_directory = Fexpand_file_name (Vinvocation_directory, Qnil);
362 Vinstallation_directory = Qnil;
364 if (!NILP (Vinvocation_directory))
366 dir = Vinvocation_directory;
367 name = Fexpand_file_name (Vinvocation_name, dir);
368 while (1)
370 Lisp_Object tem, lib_src_exists;
371 Lisp_Object etc_exists, info_exists;
373 /* See if dir contains subdirs for use by Emacs.
374 Check for the ones that would exist in a build directory,
375 not including lisp and info. */
376 tem = Fexpand_file_name (build_string ("lib-src"), dir);
377 lib_src_exists = Ffile_exists_p (tem);
379 #ifdef MSDOS
380 /* MSDOS installations frequently remove lib-src, but we still
381 must set installation-directory, or else info won't find
382 its files (it uses the value of installation-directory). */
383 tem = Fexpand_file_name (build_string ("info"), dir);
384 info_exists = Ffile_exists_p (tem);
385 #else
386 info_exists = Qnil;
387 #endif
389 if (!NILP (lib_src_exists) || !NILP (info_exists))
391 tem = Fexpand_file_name (build_string ("etc"), dir);
392 etc_exists = Ffile_exists_p (tem);
393 if (!NILP (etc_exists))
395 Vinstallation_directory
396 = Ffile_name_as_directory (dir);
397 break;
401 /* See if dir's parent contains those subdirs. */
402 tem = Fexpand_file_name (build_string ("../lib-src"), dir);
403 lib_src_exists = Ffile_exists_p (tem);
406 #ifdef MSDOS
407 /* See the MSDOS commentary above. */
408 tem = Fexpand_file_name (build_string ("../info"), dir);
409 info_exists = Ffile_exists_p (tem);
410 #else
411 info_exists = Qnil;
412 #endif
414 if (!NILP (lib_src_exists) || !NILP (info_exists))
416 tem = Fexpand_file_name (build_string ("../etc"), dir);
417 etc_exists = Ffile_exists_p (tem);
418 if (!NILP (etc_exists))
420 tem = Fexpand_file_name (build_string (".."), dir);
421 Vinstallation_directory
422 = Ffile_name_as_directory (tem);
423 break;
427 /* If the Emacs executable is actually a link,
428 next try the dir that the link points into. */
429 tem = Ffile_symlink_p (name);
430 if (!NILP (tem))
432 name = Fexpand_file_name (tem, dir);
433 dir = Ffile_name_directory (name);
435 else
436 break;
440 Vcommand_line_args = Qnil;
442 for (i = argc - 1; i >= 0; i--)
444 if (i == 0 || i > skip_args)
445 Vcommand_line_args
446 = Fcons (build_string (argv[i]), Vcommand_line_args);
449 unbind_to (count, Qnil);
452 DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
453 "Return the program name that was used to run Emacs.\n\
454 Any directory names are omitted.")
457 return Fcopy_sequence (Vinvocation_name);
460 DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
461 0, 0, 0,
462 "Return the directory name in which the Emacs executable was located")
465 return Fcopy_sequence (Vinvocation_directory);
469 #ifdef VMS
470 #ifdef LINK_CRTL_SHARE
471 #ifdef SHARABLE_LIB_BUG
472 extern noshare char **environ;
473 #endif /* SHARABLE_LIB_BUG */
474 #endif /* LINK_CRTL_SHARE */
475 #endif /* VMS */
477 #ifdef HAVE_TZSET
478 /* A valid but unlikely value for the TZ environment value.
479 It is OK (though a bit slower) if the user actually chooses this value. */
480 static char dump_tz[] = "UtC0";
481 #endif
483 #ifndef ORDINARY_LINK
484 /* We don't include crtbegin.o and crtend.o in the link,
485 so these functions and variables might be missed.
486 Provide dummy definitions to avoid error.
487 (We don't have any real constructors or destructors.) */
488 #ifdef __GNUC__
489 #ifndef GCC_CTORS_IN_LIBC
490 void __do_global_ctors ()
492 void __do_global_ctors_aux ()
494 void __do_global_dtors ()
496 /* Linux has a bug in its library; avoid an error. */
497 #ifndef LINUX
498 char * __CTOR_LIST__[2] = { (char *) (-1), 0 };
499 #endif
500 char * __DTOR_LIST__[2] = { (char *) (-1), 0 };
501 #endif /* GCC_CTORS_IN_LIBC */
502 void __main ()
504 #endif /* __GNUC__ */
505 #endif /* ORDINARY_LINK */
507 /* Test whether the next argument in ARGV matches SSTR or a prefix of
508 LSTR (at least MINLEN characters). If so, then if VALPTR is non-null
509 (the argument is supposed to have a value) store in *VALPTR either
510 the next argument or the portion of this one after the equal sign.
511 ARGV is read starting at position *SKIPPTR; this index is advanced
512 by the number of arguments used.
514 Too bad we can't just use getopt for all of this, but we don't have
515 enough information to do it right. */
517 static int
518 argmatch (argv, argc, sstr, lstr, minlen, valptr, skipptr)
519 char **argv;
520 int argc;
521 char *sstr;
522 char *lstr;
523 int minlen;
524 char **valptr;
525 int *skipptr;
527 char *p;
528 int arglen;
529 char *arg;
531 /* Don't access argv[argc]; give up in advance. */
532 if (argc <= *skipptr + 1)
533 return 0;
535 arg = argv[*skipptr+1];
536 if (arg == NULL)
537 return 0;
538 if (strcmp (arg, sstr) == 0)
540 if (valptr != NULL)
542 *valptr = argv[*skipptr+2];
543 *skipptr += 2;
545 else
546 *skipptr += 1;
547 return 1;
549 arglen = (valptr != NULL && (p = index (arg, '=')) != NULL
550 ? p - arg : strlen (arg));
551 if (lstr == 0 || arglen < minlen || strncmp (arg, lstr, arglen) != 0)
552 return 0;
553 else if (valptr == NULL)
555 *skipptr += 1;
556 return 1;
558 else if (p != NULL)
560 *valptr = p+1;
561 *skipptr += 1;
562 return 1;
564 else if (argv[*skipptr+2] != NULL)
566 *valptr = argv[*skipptr+2];
567 *skipptr += 2;
568 return 1;
570 else
572 return 0;
576 #ifdef DOUG_LEA_MALLOC
578 /* malloc can be invoked even before main (e.g. by the dynamic
579 linker), so the dumped malloc state must be restored as early as
580 possible using this special hook. */
582 static void
583 malloc_initialize_hook ()
585 extern char **environ;
587 if (initialized)
589 if (!malloc_using_checking)
590 /* Work around a bug in glibc's malloc. MALLOC_CHECK_ must be
591 ignored if the heap to be restored was constructed without
592 malloc checking. Can't use unsetenv, since that calls malloc. */
594 char **p;
596 for (p = environ; *p; p++)
597 if (strncmp (*p, "MALLOC_CHECK_=", 14) == 0)
600 *p = p[1];
601 while (*++p);
602 break;
606 malloc_set_state (malloc_state_ptr);
607 free (malloc_state_ptr);
609 else
610 malloc_using_checking = getenv ("MALLOC_CHECK_") != NULL;
613 void (*__malloc_initialize_hook) () = malloc_initialize_hook;
615 #endif /* DOUG_LEA_MALLOC */
617 /* ARGSUSED */
619 main (argc, argv, envp)
620 int argc;
621 char **argv;
622 char **envp;
624 char stack_bottom_variable;
625 int do_initial_setlocale;
626 int skip_args = 0;
627 extern int errno;
628 extern int sys_nerr;
629 #ifdef HAVE_SETRLIMIT
630 struct rlimit rlim;
631 #endif
632 int no_loadup = 0;
634 #ifdef LINUX_SBRK_BUG
635 __sbrk (1);
636 #endif
638 #ifdef RUN_TIME_REMAP
639 if (initialized)
640 run_time_remap (argv[0]);
641 #endif
643 sort_args (argc, argv);
644 argc = 0;
645 while (argv[argc]) argc++;
647 if (argmatch (argv, argc, "-version", "--version", 3, NULL, &skip_args)
648 /* We don't know the version number unless this is a dumped Emacs.
649 So ignore --version otherwise. */
650 && initialized)
652 Lisp_Object tem;
653 tem = Fsymbol_value (intern ("emacs-version"));
654 if (!STRINGP (tem))
656 fprintf (stderr, "Invalid value of `emacs-version'\n");
657 exit (1);
659 else
661 printf ("GNU Emacs %s\n", XSTRING (tem)->data);
662 printf ("Copyright (C) 1999 Free Software Foundation, Inc.\n");
663 printf ("GNU Emacs comes with ABSOLUTELY NO WARRANTY.\n");
664 printf ("You may redistribute copies of Emacs\n");
665 printf ("under the terms of the GNU General Public License.\n");
666 printf ("For more information about these matters, ");
667 printf ("see the file named COPYING.\n");
668 exit (0);
672 /* Map in shared memory, if we are using that. */
673 #ifdef HAVE_SHM
674 if (argmatch (argv, argc, "-nl", "--no-shared-memory", 6, NULL, &skip_args))
676 map_in_data (0);
677 /* The shared memory was just restored, which clobbered this. */
678 skip_args = 1;
680 else
682 map_in_data (1);
683 /* The shared memory was just restored, which clobbered this. */
684 skip_args = 0;
686 #endif
688 #ifdef NeXT
690 extern int malloc_cookie;
691 /* This helps out unexnext.c. */
692 if (initialized)
693 if (malloc_jumpstart (malloc_cookie) != 0)
694 printf ("malloc jumpstart failed!\n");
696 #endif /* NeXT */
698 #ifdef VMS
699 /* If -map specified, map the data file in */
701 char *file;
702 if (argmatch (argv, argc, "-map", "--map-data", 3, &mapin_file, &skip_args))
703 mapin_data (file);
706 #ifdef LINK_CRTL_SHARE
707 #ifdef SHARABLE_LIB_BUG
708 /* Bletcherous shared libraries! */
709 if (!stdin)
710 stdin = fdopen (0, "r");
711 if (!stdout)
712 stdout = fdopen (1, "w");
713 if (!stderr)
714 stderr = fdopen (2, "w");
715 if (!environ)
716 environ = envp;
717 #endif /* SHARABLE_LIB_BUG */
718 #endif /* LINK_CRTL_SHARE */
719 #endif /* VMS */
721 #if defined (HAVE_SETRLIMIT) && defined (RLIMIT_STACK)
722 /* Extend the stack space available.
723 Don't do that if dumping, since some systems (e.g. DJGPP)
724 might define a smaller stack limit at that time. */
725 if (1
726 #ifndef CANNOT_DUMP
727 && (!noninteractive || initialized)
728 #endif
729 && !getrlimit (RLIMIT_STACK, &rlim))
731 long newlim;
732 extern int re_max_failures;
733 /* Approximate the amount regex.c needs per unit of re_max_failures. */
734 int ratio = 20 * sizeof (char *);
735 /* Then add 33% to cover the size of the smaller stacks that regex.c
736 successively allocates and discards, on its way to the maximum. */
737 ratio += ratio / 3;
738 /* Add in some extra to cover
739 what we're likely to use for other reasons. */
740 newlim = re_max_failures * ratio + 200000;
741 #ifdef __NetBSD__
742 /* NetBSD (at least NetBSD 1.2G and former) has a bug in its
743 stack allocation routine for new process that the allocation
744 fails if stack limit is not on page boundary. So, round up the
745 new limit to page boundary. */
746 newlim = (newlim + getpagesize () - 1) / getpagesize () * getpagesize();
747 #endif
748 if (newlim > rlim.rlim_max)
750 newlim = rlim.rlim_max;
751 /* Don't let regex.c overflow the stack we have. */
752 re_max_failures = (newlim - 200000) / ratio;
754 if (rlim.rlim_cur < newlim)
755 rlim.rlim_cur = newlim;
757 setrlimit (RLIMIT_STACK, &rlim);
759 #endif /* HAVE_SETRLIMIT and RLIMIT_STACK */
761 /* Record (approximately) where the stack begins. */
762 stack_bottom = &stack_bottom_variable;
764 #ifdef USG_SHARED_LIBRARIES
765 if (bss_end)
766 brk ((void *)bss_end);
767 #endif
769 clearerr (stdin);
771 #ifndef SYSTEM_MALLOC
772 /* Arrange to get warning messages as memory fills up. */
773 memory_warnings (0, malloc_warning);
775 /* Call malloc at least once, to run the initial __malloc_hook.
776 Also call realloc and free for consistency. */
777 free (realloc (malloc (4), 4));
779 /* Arrange to disable interrupt input inside malloc etc. */
780 uninterrupt_malloc ();
781 #endif /* not SYSTEM_MALLOC */
783 #ifdef MSDOS
784 /* We do all file input/output as binary files. When we need to translate
785 newlines, we do that manually. */
786 _fmode = O_BINARY;
788 #if __DJGPP__ >= 2
789 if (!isatty (fileno (stdin)))
790 setmode (fileno (stdin), O_BINARY);
791 if (!isatty (fileno (stdout)))
793 fflush (stdout);
794 setmode (fileno (stdout), O_BINARY);
796 #else /* not __DJGPP__ >= 2 */
797 (stdin)->_flag &= ~_IOTEXT;
798 (stdout)->_flag &= ~_IOTEXT;
799 (stderr)->_flag &= ~_IOTEXT;
800 #endif /* not __DJGPP__ >= 2 */
801 #endif /* MSDOS */
803 #ifdef SET_EMACS_PRIORITY
804 if (emacs_priority)
805 nice (emacs_priority);
806 setuid (getuid ());
807 #endif /* SET_EMACS_PRIORITY */
809 /* Skip initial setlocale if LC_ALL is "C", as it's not needed in that case.
810 The build procedure uses this while dumping, to ensure that the
811 dumped Emacs does not have its system locale tables initialized,
812 as that might cause screwups when the dumped Emacs starts up. */
814 char *lc_all = getenv ("LC_ALL");
815 do_initial_setlocale = ! lc_all || strcmp (lc_all, "C");
818 /* Set locale now, so that initial error messages are localized properly.
819 fixup_locale must wait until later, since it builds strings. */
820 if (do_initial_setlocale)
821 setlocale (LC_ALL, "");
823 #ifdef EXTRA_INITIALIZE
824 EXTRA_INITIALIZE;
825 #endif
827 inhibit_window_system = 0;
829 /* Handle the -t switch, which specifies filename to use as terminal */
830 while (1)
832 char *term;
833 if (argmatch (argv, argc, "-t", "--terminal", 4, &term, &skip_args))
835 int result;
836 emacs_close (0);
837 emacs_close (1);
838 result = emacs_open (term, O_RDWR, 0);
839 if (result < 0)
841 char *errstring = strerror (errno);
842 fprintf (stderr, "emacs: %s: %s\n", term, errstring);
843 exit (1);
845 dup (0);
846 if (! isatty (0))
848 fprintf (stderr, "emacs: %s: not a tty\n", term);
849 exit (1);
851 fprintf (stderr, "Using %s\n", term);
852 #ifdef HAVE_WINDOW_SYSTEM
853 inhibit_window_system = 1; /* -t => -nw */
854 #endif
856 else
857 break;
860 if (argmatch (argv, argc, "-nw", "--no-windows", 6, NULL, &skip_args))
861 inhibit_window_system = 1;
863 /* Handle the -batch switch, which means don't do interactive display. */
864 noninteractive = 0;
865 if (argmatch (argv, argc, "-batch", "--batch", 5, NULL, &skip_args))
866 noninteractive = 1;
868 /* Handle the --help option, which gives a usage message.. */
869 if (argmatch (argv, argc, "-help", "--help", 3, NULL, &skip_args))
871 printf ("\
872 Usage: %s [--batch] [-t term] [--terminal term]\n\
873 [-d display] [--display display] [-nw] [--no-windows]\n\
874 [-q] [--no-init-file] [-u user] [--user user] [--debug-init]\n\
875 [--unibyte] [--multibyte] [--version] [--no-site-file]\n\
876 [-f func] [--funcall func] [-l file] [--load file] [--eval expr]\n\
877 [--execute expr] [--visit file] [--file file] [--insert file]\n\
878 [+linenum] file-to-visit [--kill]\n\
879 Report bugs to bug-gnu-emacs@gnu.org. First, please see\n\
880 the Bugs section of the Emacs manual or the file BUGS.\n", argv[0]);
881 exit (0);
884 if (! noninteractive)
886 #ifdef BSD_PGRPS
887 if (initialized)
889 inherited_pgroup = EMACS_GETPGRP (0);
890 setpgrp (0, getpid ());
892 #else
893 #if defined (USG5) && defined (INTERRUPT_INPUT)
894 setpgrp ();
895 #endif
896 #endif
899 init_signals ();
901 /* Don't catch SIGHUP if dumping. */
902 if (1
903 #ifndef CANNOT_DUMP
904 && initialized
905 #endif
908 sigblock (sigmask (SIGHUP));
909 /* In --batch mode, don't catch SIGHUP if already ignored.
910 That makes nohup work. */
911 if (! noninteractive
912 || signal (SIGHUP, SIG_IGN) != SIG_IGN)
913 signal (SIGHUP, fatal_error_signal);
914 sigunblock (sigmask (SIGHUP));
917 if (
918 #ifndef CANNOT_DUMP
919 ! noninteractive || initialized
920 #else
922 #endif
925 /* Don't catch these signals in batch mode if dumping.
926 On some machines, this sets static data that would make
927 signal fail to work right when the dumped Emacs is run. */
928 signal (SIGQUIT, fatal_error_signal);
929 signal (SIGILL, fatal_error_signal);
930 signal (SIGTRAP, fatal_error_signal);
931 #ifdef SIGUSR1
932 signal (SIGUSR1, handle_USR1_signal);
933 #ifdef SIGUSR2
934 signal (SIGUSR2, handle_USR2_signal);
935 #endif
936 #endif
937 #ifdef SIGABRT
938 signal (SIGABRT, fatal_error_signal);
939 #endif
940 #ifdef SIGHWE
941 signal (SIGHWE, fatal_error_signal);
942 #endif
943 #ifdef SIGPRE
944 signal (SIGPRE, fatal_error_signal);
945 #endif
946 #ifdef SIGORE
947 signal (SIGORE, fatal_error_signal);
948 #endif
949 #ifdef SIGUME
950 signal (SIGUME, fatal_error_signal);
951 #endif
952 #ifdef SIGDLK
953 signal (SIGDLK, fatal_error_signal);
954 #endif
955 #ifdef SIGCPULIM
956 signal (SIGCPULIM, fatal_error_signal);
957 #endif
958 #ifdef SIGIOT
959 /* This is missing on some systems - OS/2, for example. */
960 signal (SIGIOT, fatal_error_signal);
961 #endif
962 #ifdef SIGEMT
963 signal (SIGEMT, fatal_error_signal);
964 #endif
965 signal (SIGFPE, fatal_error_signal);
966 #ifdef SIGBUS
967 signal (SIGBUS, fatal_error_signal);
968 #endif
969 signal (SIGSEGV, fatal_error_signal);
970 #ifdef SIGSYS
971 signal (SIGSYS, fatal_error_signal);
972 #endif
973 signal (SIGTERM, fatal_error_signal);
974 #ifdef SIGXCPU
975 signal (SIGXCPU, fatal_error_signal);
976 #endif
977 #ifdef SIGXFSZ
978 signal (SIGXFSZ, fatal_error_signal);
979 #endif /* SIGXFSZ */
981 #ifdef SIGDANGER
982 /* This just means available memory is getting low. */
983 signal (SIGDANGER, memory_warning_signal);
984 #endif
986 #ifdef AIX
987 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
988 signal (SIGXCPU, fatal_error_signal);
989 #ifndef _I386
990 signal (SIGIOINT, fatal_error_signal);
991 #endif
992 signal (SIGGRANT, fatal_error_signal);
993 signal (SIGRETRACT, fatal_error_signal);
994 signal (SIGSOUND, fatal_error_signal);
995 signal (SIGMSG, fatal_error_signal);
996 #endif /* AIX */
999 noninteractive1 = noninteractive;
1001 /* Perform basic initializations (not merely interning symbols) */
1003 if (!initialized)
1005 init_alloc_once ();
1006 init_obarray ();
1007 init_eval_once ();
1008 init_charset_once ();
1009 init_coding_once ();
1010 init_syntax_once (); /* Create standard syntax table. */
1011 init_category_once (); /* Create standard category table. */
1012 /* Must be done before init_buffer */
1013 init_casetab_once ();
1014 init_buffer_once (); /* Create buffer table and some buffers */
1015 init_minibuf_once (); /* Create list of minibuffers */
1016 /* Must precede init_window_once */
1018 /* Call syms_of_xfaces before init_window_once because that
1019 function creates Vterminal_frame. Termcap frames now use
1020 faces, and the face implementation uses some symbols as
1021 face names. */
1022 #ifndef HAVE_NTGUI
1023 syms_of_xfaces ();
1024 #endif
1026 init_window_once (); /* Init the window system */
1027 init_fileio_once (); /* Must precede any path manipulation. */
1030 init_alloc ();
1032 if (do_initial_setlocale)
1034 fixup_locale ();
1035 Vsystem_messages_locale = Vprevious_system_messages_locale;
1036 Vsystem_time_locale = Vprevious_system_time_locale;
1039 init_eval ();
1040 init_coding ();
1041 init_data ();
1042 #ifdef CLASH_DETECTION
1043 init_filelock ();;
1044 #endif
1045 running_asynch_code = 0;
1047 /* Handle --unibyte and the EMACS_UNIBYTE envvar,
1048 but not while dumping. */
1049 if (
1050 #ifndef CANNOT_DUMP
1051 ! noninteractive || initialized
1052 #else
1054 #endif
1057 int inhibit_unibyte = 0;
1059 /* --multibyte overrides EMACS_UNIBYTE. */
1060 if (argmatch (argv, argc, "-no-unibyte", "--no-unibyte", 4, NULL, &skip_args)
1061 || argmatch (argv, argc, "-multibyte", "--multibyte", 4, NULL, &skip_args))
1062 inhibit_unibyte = 1;
1064 /* --unibyte requests that we set up to do everything with single-byte
1065 buffers and strings. We need to handle this before calling
1066 init_lread, init_editfns and other places that generate Lisp strings
1067 from text in the environment. */
1068 /* Actually this shouldn't be needed as of 20.4 in a generally
1069 unibyte environment. As handa says, environment values
1070 aren't now decoded; also existing buffers are now made
1071 unibyte during startup if .emacs sets unibyte. Tested with
1072 8-bit data in environment variables and /etc/passwd, setting
1073 unibyte and Latin-1 in .emacs. -- Dave Love */
1074 if (argmatch (argv, argc, "-unibyte", "--unibyte", 4, NULL, &skip_args)
1075 || argmatch (argv, argc, "-no-multibyte", "--no-multibyte", 4, NULL, &skip_args)
1076 || (getenv ("EMACS_UNIBYTE") && !inhibit_unibyte))
1078 Lisp_Object old_log_max;
1079 Lisp_Object symbol, tail;
1081 symbol = intern ("default-enable-multibyte-characters");
1082 Fset (symbol, Qnil);
1084 if (initialized)
1086 /* Erase pre-dump messages in *Messages* now so no abort. */
1087 old_log_max = Vmessage_log_max;
1088 XSETFASTINT (Vmessage_log_max, 0);
1089 message_dolog ("", 0, 1, 0);
1090 Vmessage_log_max = old_log_max;
1093 for (tail = Vbuffer_alist; CONSP (tail);
1094 tail = XCDR (tail))
1096 Lisp_Object buffer;
1098 buffer = Fcdr (XCAR (tail));
1099 /* Verify that all buffers are empty now, as they
1100 ought to be. */
1101 if (BUF_Z (XBUFFER (buffer)) > BUF_BEG (XBUFFER (buffer)))
1102 abort ();
1103 /* It is safe to do this crudely in an empty buffer. */
1104 XBUFFER (buffer)->enable_multibyte_characters = Qnil;
1109 no_loadup
1110 = !argmatch (argv, argc, "-nl", "--no-loadup", 6, NULL, &skip_args);
1113 #ifdef HAVE_X_WINDOWS
1114 /* Stupid kludge to catch command-line display spec. We can't
1115 handle this argument entirely in window system dependent code
1116 because we don't even know which window system dependent code
1117 to run until we've recognized this argument. */
1119 char *displayname = 0;
1120 int count_before = skip_args;
1122 /* Skip any number of -d options, but only use the last one. */
1123 while (1)
1125 int count_before_this = skip_args;
1127 if (argmatch (argv, argc, "-d", "--display", 3, &displayname, &skip_args))
1128 display_arg = 1;
1129 else if (argmatch (argv, argc, "-display", 0, 3, &displayname, &skip_args))
1130 display_arg = 1;
1131 else
1132 break;
1134 count_before = count_before_this;
1137 /* If we have the form --display=NAME,
1138 convert it into -d name.
1139 This requires inserting a new element into argv. */
1140 if (displayname != 0 && skip_args - count_before == 1)
1142 char **new = (char **) xmalloc (sizeof (char *) * (argc + 2));
1143 int j;
1145 for (j = 0; j < count_before + 1; j++)
1146 new[j] = argv[j];
1147 new[count_before + 1] = "-d";
1148 new[count_before + 2] = displayname;
1149 for (j = count_before + 2; j <argc; j++)
1150 new[j + 1] = argv[j];
1151 argv = new;
1152 argc++;
1154 /* Change --display to -d, when its arg is separate. */
1155 else if (displayname != 0 && skip_args > count_before
1156 && argv[count_before + 1][1] == '-')
1157 argv[count_before + 1] = "-d";
1159 /* Don't actually discard this arg. */
1160 skip_args = count_before;
1162 #endif
1164 /* argmatch must not be used after here,
1165 except when bulding temacs
1166 because the -d argument has not been skipped in skip_args. */
1168 #ifdef MSDOS
1169 /* Call early 'cause init_environment needs it. */
1170 init_dosfns ();
1171 /* Set defaults for several environment variables. */
1172 if (initialized)
1173 init_environment (argc, argv, skip_args);
1174 else
1175 tzset ();
1176 #endif /* MSDOS */
1178 #ifdef WINDOWSNT
1179 /* Initialize environment from registry settings. */
1180 init_environment (argv);
1181 init_ntproc (); /* must precede init_editfns */
1182 #endif
1184 /* egetenv is a pretty low-level facility, which may get called in
1185 many circumstances; it seems flimsy to put off initializing it
1186 until calling init_callproc. */
1187 set_process_environment ();
1188 /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
1189 if this is not done. Do it after set_process_environment so that we
1190 don't pollute Vprocess_environment. */
1191 #ifdef AIX
1192 putenv ("LANG=C");
1193 #endif
1195 init_buffer (); /* Init default directory of main buffer */
1197 init_callproc_1 (); /* Must precede init_cmdargs and init_sys_modes. */
1198 init_cmdargs (argc, argv, skip_args); /* Must precede init_lread. */
1200 if (initialized)
1202 /* Erase any pre-dump messages in the message log, to avoid confusion */
1203 Lisp_Object old_log_max;
1204 old_log_max = Vmessage_log_max;
1205 XSETFASTINT (Vmessage_log_max, 0);
1206 message_dolog ("", 0, 1, 0);
1207 Vmessage_log_max = old_log_max;
1210 init_callproc (); /* Must follow init_cmdargs but not init_sys_modes. */
1211 init_lread ();
1213 /* Intern the names of all standard functions and variables;
1214 define standard keys. */
1216 if (!initialized)
1218 /* The basic levels of Lisp must come first */
1219 /* And data must come first of all
1220 for the sake of symbols like error-message */
1221 syms_of_data ();
1222 syms_of_alloc ();
1223 syms_of_lread ();
1224 syms_of_print ();
1225 syms_of_eval ();
1226 syms_of_fns ();
1227 syms_of_floatfns ();
1229 syms_of_abbrev ();
1230 syms_of_buffer ();
1231 syms_of_bytecode ();
1232 syms_of_callint ();
1233 syms_of_casefiddle ();
1234 syms_of_casetab ();
1235 syms_of_callproc ();
1236 syms_of_category ();
1237 syms_of_ccl ();
1238 syms_of_charset ();
1239 syms_of_cmds ();
1240 #ifndef NO_DIR_LIBRARY
1241 syms_of_dired ();
1242 #endif /* not NO_DIR_LIBRARY */
1243 syms_of_display ();
1244 syms_of_doc ();
1245 syms_of_editfns ();
1246 syms_of_emacs ();
1247 syms_of_fileio ();
1248 syms_of_coding (); /* This should be after syms_of_fileio. */
1249 #ifdef CLASH_DETECTION
1250 syms_of_filelock ();
1251 #endif /* CLASH_DETECTION */
1252 syms_of_indent ();
1253 syms_of_insdel ();
1254 syms_of_keyboard ();
1255 syms_of_keymap ();
1256 syms_of_macros ();
1257 syms_of_marker ();
1258 syms_of_minibuf ();
1259 syms_of_mocklisp ();
1260 syms_of_process ();
1261 syms_of_search ();
1262 syms_of_frame ();
1263 syms_of_syntax ();
1264 syms_of_term ();
1265 syms_of_undo ();
1266 #ifdef HAVE_SOUND
1267 syms_of_sound ();
1268 #endif
1270 syms_of_textprop ();
1271 #ifdef VMS
1272 syms_of_vmsproc ();
1273 #endif /* VMS */
1274 #ifdef WINDOWSNT
1275 syms_of_ntproc ();
1276 #endif /* WINDOWSNT */
1277 syms_of_window ();
1278 syms_of_xdisp ();
1279 #ifdef HAVE_X_WINDOWS
1280 syms_of_xterm ();
1281 syms_of_xfns ();
1282 syms_of_fontset ();
1283 #ifdef HAVE_X11
1284 syms_of_xselect ();
1285 #endif
1286 #endif /* HAVE_X_WINDOWS */
1288 #ifndef HAVE_NTGUI
1289 syms_of_xmenu ();
1290 #endif
1292 #ifdef HAVE_NTGUI
1293 syms_of_w32term ();
1294 syms_of_w32fns ();
1295 syms_of_w32faces ();
1296 syms_of_w32select ();
1297 syms_of_w32menu ();
1298 syms_of_fontset ();
1299 #endif /* HAVE_NTGUI */
1301 #ifdef SYMS_SYSTEM
1302 SYMS_SYSTEM;
1303 #endif
1305 #ifdef SYMS_MACHINE
1306 SYMS_MACHINE;
1307 #endif
1309 keys_of_casefiddle ();
1310 keys_of_cmds ();
1311 keys_of_buffer ();
1312 keys_of_keyboard ();
1313 keys_of_keymap ();
1314 keys_of_macros ();
1315 keys_of_minibuf ();
1316 keys_of_window ();
1317 keys_of_frame ();
1320 if (!noninteractive)
1322 #ifdef VMS
1323 init_vms_input ();/* init_display calls get_frame_size, that needs this */
1324 #endif /* VMS */
1325 init_display (); /* Determine terminal type. init_sys_modes uses results */
1327 init_keyboard (); /* This too must precede init_sys_modes */
1328 #ifdef VMS
1329 init_vmsproc (); /* And this too. */
1330 #endif /* VMS */
1331 init_sys_modes (); /* Init system terminal modes (RAW or CBREAK, etc.) */
1332 #ifdef HAVE_X_WINDOWS
1333 init_xfns ();
1334 #endif /* HAVE_X_WINDOWS */
1335 init_fns ();
1336 init_xdisp ();
1337 init_macros ();
1338 init_editfns ();
1339 #ifdef LISP_FLOAT_TYPE
1340 init_floatfns ();
1341 #endif
1342 #ifdef VMS
1343 init_vmsfns ();
1344 #endif /* VMS */
1345 init_process ();
1346 #ifdef HAVE_SOUND
1347 init_sound ();
1348 #endif
1350 if (!initialized)
1352 char *file;
1353 /* Handle -l loadup, args passed by Makefile. */
1354 if (argmatch (argv, argc, "-l", "--load", 3, &file, &skip_args))
1355 Vtop_level = Fcons (intern ("load"),
1356 Fcons (build_string (file), Qnil));
1357 #ifdef CANNOT_DUMP
1358 /* Unless next switch is -nl, load "loadup.el" first thing. */
1359 if (! no_loadup)
1360 Vtop_level = Fcons (intern ("load"),
1361 Fcons (build_string ("loadup.el"), Qnil));
1362 #endif /* CANNOT_DUMP */
1365 if (initialized)
1367 #ifdef HAVE_TZSET
1369 /* If the execution TZ happens to be the same as the dump TZ,
1370 change it to some other value and then change it back,
1371 to force the underlying implementation to reload the TZ info.
1372 This is needed on implementations that load TZ info from files,
1373 since the TZ file contents may differ between dump and execution. */
1374 char *tz = getenv ("TZ");
1375 if (tz && !strcmp (tz, dump_tz))
1377 ++*tz;
1378 tzset ();
1379 --*tz;
1382 #endif
1385 /* Gerd Moellmann <gerd@acm.org> says this makes profiling work on
1386 FreeBSD. It might work on some other systems too.
1387 Give it a try and tell me if it works on your system. */
1388 #if defined (__FreeBSD__) || defined (__linux)
1389 #ifdef PROFILING
1390 if (initialized)
1392 extern void _mcleanup ();
1393 extern char etext;
1394 extern void safe_bcopy ();
1395 extern void dump_opcode_frequencies ();
1397 atexit (_mcleanup);
1398 // atexit (dump_opcode_frequencies);
1399 /* This uses safe_bcopy because that function comes first in the
1400 Emacs executable. It might be better to use something that
1401 gives the start of the text segment, but start_of_text is not
1402 defined on all systems now. */
1403 monstartup (safe_bcopy, &etext);
1405 else
1406 moncontrol (0);
1407 #endif
1408 #endif
1410 initialized = 1;
1412 #ifdef LOCALTIME_CACHE
1413 /* Some versions of localtime have a bug. They cache the value of the time
1414 zone rather than looking it up every time. Since localtime() is
1415 called to bolt the undumping time into the undumped emacs, this
1416 results in localtime ignoring the TZ environment variable.
1417 This flushes the new TZ value into localtime. */
1418 tzset ();
1419 #endif /* defined (LOCALTIME_CACHE) */
1421 /* Enter editor command loop. This never returns. */
1422 Frecursive_edit ();
1423 /* NOTREACHED */
1426 /* Sort the args so we can find the most important ones
1427 at the beginning of argv. */
1429 /* First, here's a table of all the standard options. */
1431 struct standard_args
1433 char *name;
1434 char *longname;
1435 int priority;
1436 int nargs;
1439 struct standard_args standard_args[] =
1441 { "-version", "--version", 150, 0 },
1442 #ifdef HAVE_SHM
1443 { "-nl", "--no-shared-memory", 140, 0 },
1444 #endif
1445 #ifdef VMS
1446 { "-map", "--map-data", 130, 0 },
1447 #endif
1448 { "-t", "--terminal", 120, 1 },
1449 { "-nw", "--no-windows", 110, 0 },
1450 { "-batch", "--batch", 100, 0 },
1451 { "-help", "--help", 90, 0 },
1452 { "-no-unibyte", "--no-unibyte", 83, 0 },
1453 { "-multibyte", "--multibyte", 82, 0 },
1454 { "-unibyte", "--unibyte", 81, 0 },
1455 { "-no-multibyte", "--no-multibyte", 80, 0 },
1456 #ifdef CANNOT_DUMP
1457 { "-nl", "--no-loadup", 70, 0 },
1458 #endif
1459 /* -d must come last before the options handled in startup.el. */
1460 { "-d", "--display", 60, 1 },
1461 { "-display", 0, 60, 1 },
1462 /* Now for the options handled in startup.el. */
1463 { "-q", "--no-init-file", 50, 0 },
1464 { "-no-init-file", 0, 50, 0 },
1465 { "-no-site-file", "--no-site-file", 40, 0 },
1466 { "-u", "--user", 30, 1 },
1467 { "-user", 0, 30, 1 },
1468 { "-debug-init", "--debug-init", 20, 0 },
1469 { "-i", "--icon-type", 15, 0 },
1470 { "-itype", 0, 15, 0 },
1471 { "-iconic", "--iconic", 15, 0 },
1472 { "-bg", "--background-color", 10, 1 },
1473 { "-background", 0, 10, 1 },
1474 { "-fg", "--foreground-color", 10, 1 },
1475 { "-foreground", 0, 10, 1 },
1476 { "-bd", "--border-color", 10, 1 },
1477 { "-bw", "--border-width", 10, 1 },
1478 { "-ib", "--internal-border", 10, 1 },
1479 { "-ms", "--mouse-color", 10, 1 },
1480 { "-cr", "--cursor-color", 10, 1 },
1481 { "-fn", "--font", 10, 1 },
1482 { "-font", 0, 10, 1 },
1483 { "-g", "--geometry", 10, 1 },
1484 { "-geometry", 0, 10, 1 },
1485 { "-T", "--title", 10, 1 },
1486 { "-title", 0, 10, 1 },
1487 { "-name", "--name", 10, 1 },
1488 { "-xrm", "--xrm", 10, 1 },
1489 { "-r", "--reverse-video", 5, 0 },
1490 { "-rv", 0, 5, 0 },
1491 { "-reverse", 0, 5, 0 },
1492 { "-hb", "--horizontal-scroll-bars", 5, 0 },
1493 { "-vb", "--vertical-scroll-bars", 5, 0 },
1494 /* These have the same priority as ordinary file name args,
1495 so they are not reordered with respect to those. */
1496 { "-L", "--directory", 0, 1 },
1497 { "-directory", 0, 0, 1 },
1498 { "-l", "--load", 0, 1 },
1499 { "-load", 0, 0, 1 },
1500 { "-f", "--funcall", 0, 1 },
1501 { "-funcall", 0, 0, 1 },
1502 { "-eval", "--eval", 0, 1 },
1503 { "-execute", "--execute", 0, 1 },
1504 { "-find-file", "--find-file", 0, 1 },
1505 { "-visit", "--visit", 0, 1 },
1506 { "-file", "--file", 0, 1 },
1507 { "-insert", "--insert", 0, 1 },
1508 /* This should be processed after ordinary file name args and the like. */
1509 { "-kill", "--kill", -10, 0 },
1512 /* Reorder the elements of ARGV (assumed to have ARGC elements)
1513 so that the highest priority ones come first.
1514 Do not change the order of elements of equal priority.
1515 If an option takes an argument, keep it and its argument together.
1517 If an option that takes no argument appears more
1518 than once, eliminate all but one copy of it. */
1520 static void
1521 sort_args (argc, argv)
1522 int argc;
1523 char **argv;
1525 char **new = (char **) xmalloc (sizeof (char *) * argc);
1526 /* For each element of argv,
1527 the corresponding element of options is:
1528 0 for an option that takes no arguments,
1529 1 for an option that takes one argument, etc.
1530 -1 for an ordinary non-option argument. */
1531 int *options = (int *) xmalloc (sizeof (int) * argc);
1532 int *priority = (int *) xmalloc (sizeof (int) * argc);
1533 int to = 1;
1534 int incoming_used = 1;
1535 int from;
1536 int i;
1538 /* Categorize all the options,
1539 and figure out which argv elts are option arguments. */
1540 for (from = 1; from < argc; from++)
1542 options[from] = -1;
1543 priority[from] = 0;
1544 if (argv[from][0] == '-')
1546 int match, thislen;
1547 char *equals;
1549 /* If we have found "--", don't consider
1550 any more arguments as options. */
1551 if (argv[from][1] == '-' && argv[from][2] == 0)
1553 /* Leave the "--", and everything following it, at the end. */
1554 for (; from < argc; from++)
1556 priority[from] = -100;
1557 options[from] = -1;
1559 break;
1562 /* Look for a match with a known old-fashioned option. */
1563 for (i = 0; i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1564 if (!strcmp (argv[from], standard_args[i].name))
1566 options[from] = standard_args[i].nargs;
1567 priority[from] = standard_args[i].priority;
1568 if (from + standard_args[i].nargs >= argc)
1569 fatal ("Option `%s' requires an argument\n", argv[from]);
1570 from += standard_args[i].nargs;
1571 goto done;
1574 /* Look for a match with a known long option.
1575 MATCH is -1 if no match so far, -2 if two or more matches so far,
1576 >= 0 (the table index of the match) if just one match so far. */
1577 if (argv[from][1] == '-')
1579 match = -1;
1580 thislen = strlen (argv[from]);
1581 equals = index (argv[from], '=');
1582 if (equals != 0)
1583 thislen = equals - argv[from];
1585 for (i = 0;
1586 i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1587 if (standard_args[i].longname
1588 && !strncmp (argv[from], standard_args[i].longname,
1589 thislen))
1591 if (match == -1)
1592 match = i;
1593 else
1594 match = -2;
1597 /* If we found exactly one match, use that. */
1598 if (match >= 0)
1600 options[from] = standard_args[match].nargs;
1601 priority[from] = standard_args[match].priority;
1602 /* If --OPTION=VALUE syntax is used,
1603 this option uses just one argv element. */
1604 if (equals != 0)
1605 options[from] = 0;
1606 if (from + options[from] >= argc)
1607 fatal ("Option `%s' requires an argument\n", argv[from]);
1608 from += options[from];
1611 done: ;
1615 /* Copy the arguments, in order of decreasing priority, to NEW. */
1616 new[0] = argv[0];
1617 while (incoming_used < argc)
1619 int best = -1;
1620 int best_priority = -9999;
1622 /* Find the highest priority remaining option.
1623 If several have equal priority, take the first of them. */
1624 for (from = 1; from < argc; from++)
1626 if (argv[from] != 0 && priority[from] > best_priority)
1628 best_priority = priority[from];
1629 best = from;
1631 /* Skip option arguments--they are tied to the options. */
1632 if (options[from] > 0)
1633 from += options[from];
1636 if (best < 0)
1637 abort ();
1639 /* Copy the highest priority remaining option, with its args, to NEW.
1640 Unless it is a duplicate of the previous one. */
1641 if (! (options[best] == 0
1642 && ! strcmp (new[to - 1], argv[best])))
1644 new[to++] = argv[best];
1645 for (i = 0; i < options[best]; i++)
1646 new[to++] = argv[best + i + 1];
1649 incoming_used += 1 + (options[best] > 0 ? options[best] : 0);
1651 /* Clear out this option in ARGV. */
1652 argv[best] = 0;
1653 for (i = 0; i < options[best]; i++)
1654 argv[best + i + 1] = 0;
1657 /* If duplicate options were deleted, fill up extra space with null ptrs. */
1658 while (to < argc)
1659 new[to++] = 0;
1661 bcopy (new, argv, sizeof (char *) * argc);
1663 free (options);
1664 free (new);
1665 free (priority);
1668 DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
1669 "Exit the Emacs job and kill it.\n\
1670 If ARG is an integer, return ARG as the exit program code.\n\
1671 If ARG is a string, stuff it as keyboard input.\n\n\
1672 The value of `kill-emacs-hook', if not void,\n\
1673 is a list of functions (of no args),\n\
1674 all of which are called before Emacs is actually killed.")
1675 (arg)
1676 Lisp_Object arg;
1678 struct gcpro gcpro1;
1680 GCPRO1 (arg);
1682 if (feof (stdin))
1683 arg = Qt;
1685 if (!NILP (Vrun_hooks) && !noninteractive)
1686 call1 (Vrun_hooks, intern ("kill-emacs-hook"));
1688 UNGCPRO;
1690 /* Is it really necessary to do this deassign
1691 when we are going to exit anyway? */
1692 /* #ifdef VMS
1693 stop_vms_input ();
1694 #endif */
1696 shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
1698 /* If we have an auto-save list file,
1699 kill it because we are exiting Emacs deliberately (not crashing).
1700 Do it after shut_down_emacs, which does an auto-save. */
1701 if (STRINGP (Vauto_save_list_file_name))
1702 unlink (XSTRING (Vauto_save_list_file_name)->data);
1704 exit (INTEGERP (arg) ? XINT (arg)
1705 #ifdef VMS
1707 #else
1709 #endif
1711 /* NOTREACHED */
1715 /* Perform an orderly shutdown of Emacs. Autosave any modified
1716 buffers, kill any child processes, clean up the terminal modes (if
1717 we're in the foreground), and other stuff like that. Don't perform
1718 any redisplay; this may be called when Emacs is shutting down in
1719 the background, or after its X connection has died.
1721 If SIG is a signal number, print a message for it.
1723 This is called by fatal signal handlers, X protocol error handlers,
1724 and Fkill_emacs. */
1726 void
1727 shut_down_emacs (sig, no_x, stuff)
1728 int sig, no_x;
1729 Lisp_Object stuff;
1731 /* Prevent running of hooks from now on. */
1732 Vrun_hooks = Qnil;
1734 /* If we are controlling the terminal, reset terminal modes */
1735 #ifdef EMACS_HAVE_TTY_PGRP
1737 int pgrp = EMACS_GETPGRP (0);
1739 int tpgrp;
1740 if (EMACS_GET_TTY_PGRP (0, &tpgrp) != -1
1741 && tpgrp == pgrp)
1743 fflush (stdout);
1744 reset_sys_modes ();
1745 if (sig && sig != SIGTERM)
1746 fprintf (stderr, "Fatal error (%d).", sig);
1749 #else
1750 fflush (stdout);
1751 reset_sys_modes ();
1752 #endif
1754 stuff_buffered_input (stuff);
1756 kill_buffer_processes (Qnil);
1757 Fdo_auto_save (Qt, Qnil);
1759 #ifdef CLASH_DETECTION
1760 unlock_all_files ();
1761 #endif
1763 #ifdef VMS
1764 kill_vms_processes ();
1765 #endif
1767 #if 0 /* This triggers a bug in XCloseDisplay and is not needed. */
1768 #ifdef HAVE_X_WINDOWS
1769 /* It's not safe to call intern here. Maybe we are crashing. */
1770 if (!noninteractive && SYMBOLP (Vwindow_system)
1771 && XSYMBOL (Vwindow_system)->name->size == 1
1772 && XSYMBOL (Vwindow_system)->name->data[0] == 'x'
1773 && ! no_x)
1774 Fx_close_current_connection ();
1775 #endif /* HAVE_X_WINDOWS */
1776 #endif
1778 #ifdef SIGIO
1779 /* There is a tendency for a SIGIO signal to arrive within exit,
1780 and cause a SIGHUP because the input descriptor is already closed. */
1781 unrequest_sigio ();
1782 signal (SIGIO, SIG_IGN);
1783 #endif
1785 #ifdef WINDOWSNT
1786 term_ntproc ();
1787 #endif
1789 check_glyph_memory ();
1790 check_message_stack ();
1792 #ifdef MSDOS
1793 dos_cleanup ();
1794 #endif
1799 #ifndef CANNOT_DUMP
1801 #ifdef HAVE_SHM
1803 DEFUN ("dump-emacs-data", Fdump_emacs_data, Sdump_emacs_data, 1, 1, 0,
1804 "Dump current state of Emacs into data file FILENAME.\n\
1805 This function exists on systems that use HAVE_SHM.")
1806 (filename)
1807 Lisp_Object filename;
1809 extern char my_edata[];
1810 Lisp_Object tem;
1812 CHECK_STRING (filename, 0);
1813 filename = Fexpand_file_name (filename, Qnil);
1815 tem = Vpurify_flag;
1816 Vpurify_flag = Qnil;
1818 fflush (stdout);
1819 /* Tell malloc where start of impure now is */
1820 /* Also arrange for warnings when nearly out of space. */
1821 #ifndef SYSTEM_MALLOC
1822 memory_warnings (my_edata, malloc_warning);
1823 #endif
1824 map_out_data (XSTRING (filename)->data);
1826 Vpurify_flag = tem;
1828 return Qnil;
1831 #else /* not HAVE_SHM */
1833 DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
1834 "Dump current state of Emacs into executable file FILENAME.\n\
1835 Take symbols from SYMFILE (presumably the file you executed to run Emacs).\n\
1836 This is used in the file `loadup.el' when building Emacs.\n\
1838 You must run Emacs in batch mode in order to dump it.")
1839 (filename, symfile)
1840 Lisp_Object filename, symfile;
1842 extern char my_edata[];
1843 Lisp_Object tem;
1844 Lisp_Object symbol;
1845 int count = specpdl_ptr - specpdl;
1847 if (! noninteractive)
1848 error ("Dumping Emacs works only in batch mode");
1850 /* Bind `command-line-processed' to nil before dumping,
1851 so that the dumped Emacs will process its command line
1852 and set up to work with X windows if appropriate. */
1853 symbol = intern ("command-line-process");
1854 specbind (symbol, Qnil);
1856 CHECK_STRING (filename, 0);
1857 filename = Fexpand_file_name (filename, Qnil);
1858 if (!NILP (symfile))
1860 CHECK_STRING (symfile, 0);
1861 if (XSTRING (symfile)->size)
1862 symfile = Fexpand_file_name (symfile, Qnil);
1865 tem = Vpurify_flag;
1866 Vpurify_flag = Qnil;
1868 #ifdef HAVE_TZSET
1869 set_time_zone_rule (dump_tz);
1870 #ifndef LOCALTIME_CACHE
1871 /* Force a tz reload, since set_time_zone_rule doesn't. */
1872 tzset ();
1873 #endif
1874 #endif
1876 fflush (stdout);
1877 #ifdef VMS
1878 mapout_data (XSTRING (filename)->data);
1879 #else
1880 /* Tell malloc where start of impure now is */
1881 /* Also arrange for warnings when nearly out of space. */
1882 #ifndef SYSTEM_MALLOC
1883 #ifndef WINDOWSNT
1884 /* On Windows, this was done before dumping, and that once suffices.
1885 Meanwhile, my_edata is not valid on Windows. */
1886 memory_warnings (my_edata, malloc_warning);
1887 #endif /* not WINDOWSNT */
1888 #endif
1889 #ifdef DOUG_LEA_MALLOC
1890 malloc_state_ptr = malloc_get_state ();
1891 #endif
1892 unexec (XSTRING (filename)->data,
1893 !NILP (symfile) ? XSTRING (symfile)->data : 0, my_edata, 0, 0);
1894 #ifdef DOUG_LEA_MALLOC
1895 free (malloc_state_ptr);
1896 #endif
1897 #endif /* not VMS */
1899 Vpurify_flag = tem;
1901 return unbind_to (count, Qnil);
1904 #endif /* not HAVE_SHM */
1906 #endif /* not CANNOT_DUMP */
1908 #if HAVE_SETLOCALE
1909 /* Recover from setlocale (LC_ALL, ""). */
1910 void
1911 fixup_locale ()
1913 char *l;
1915 /* The Emacs Lisp reader needs LC_NUMERIC to be "C",
1916 so that numbers are read and printed properly for Emacs Lisp. */
1917 setlocale (LC_NUMERIC, "C");
1919 #ifdef LC_MESSAGES
1920 l = setlocale (LC_MESSAGES, (char *) 0);
1921 Vprevious_system_messages_locale = l ? build_string (l) : Qnil;
1922 #endif
1923 l = setlocale (LC_TIME, (char *) 0);
1924 Vprevious_system_time_locale = l ? build_string (l) : Qnil;
1927 static void
1928 synchronize_locale (category, plocale, desired_locale)
1929 int category;
1930 Lisp_Object *plocale;
1931 Lisp_Object desired_locale;
1933 if (STRINGP (desired_locale)
1934 && (NILP (*plocale) || NILP (Fstring_equal (*plocale, desired_locale)))
1935 && setlocale (category, XSTRING (desired_locale)->data))
1936 *plocale = desired_locale;
1939 /* Set system time locale to match Vsystem_time_locale, if possible. */
1940 void
1941 synchronize_system_time_locale ()
1943 synchronize_locale (LC_TIME, &Vprevious_system_time_locale,
1944 Vsystem_time_locale);
1947 /* Set system messages locale to match Vsystem_messages_locale, if
1948 possible. */
1949 void
1950 synchronize_system_messages_locale ()
1952 #ifdef LC_MESSAGES
1953 synchronize_locale (LC_MESSAGES, &Vprevious_system_messages_locale,
1954 Vsystem_messages_locale);
1955 #endif
1957 #endif /* HAVE_SETLOCALE */
1959 #ifndef SEPCHAR
1960 #define SEPCHAR ':'
1961 #endif
1963 Lisp_Object
1964 decode_env_path (evarname, defalt)
1965 char *evarname, *defalt;
1967 register char *path, *p;
1968 Lisp_Object lpath, element, tem;
1970 /* It's okay to use getenv here, because this function is only used
1971 to initialize variables when Emacs starts up, and isn't called
1972 after that. */
1973 if (evarname != 0)
1974 path = (char *) getenv (evarname);
1975 else
1976 path = 0;
1977 if (!path)
1978 path = defalt;
1979 #ifdef DOS_NT
1980 /* Ensure values from the environment use the proper directory separator. */
1981 if (path)
1983 p = alloca (strlen (path) + 1);
1984 strcpy (p, path);
1985 path = p;
1987 if ('/' == DIRECTORY_SEP)
1988 dostounix_filename (path);
1989 else
1990 unixtodos_filename (path);
1992 #endif
1993 lpath = Qnil;
1994 while (1)
1996 p = index (path, SEPCHAR);
1997 if (!p) p = path + strlen (path);
1998 element = (p - path ? make_string (path, p - path)
1999 : build_string ("."));
2001 /* Add /: to the front of the name
2002 if it would otherwise be treated as magic. */
2003 tem = Ffind_file_name_handler (element, Qt);
2004 if (! NILP (tem))
2005 element = concat2 (build_string ("/:"), element);
2007 lpath = Fcons (element, lpath);
2008 if (*p)
2009 path = p + 1;
2010 else
2011 break;
2013 return Fnreverse (lpath);
2016 void
2017 syms_of_emacs ()
2019 Qfile_name_handler_alist = intern ("file-name-handler-alist");
2020 staticpro (&Qfile_name_handler_alist);
2022 #ifndef CANNOT_DUMP
2023 #ifdef HAVE_SHM
2024 defsubr (&Sdump_emacs_data);
2025 #else
2026 defsubr (&Sdump_emacs);
2027 #endif
2028 #endif
2030 defsubr (&Skill_emacs);
2032 defsubr (&Sinvocation_name);
2033 defsubr (&Sinvocation_directory);
2035 DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
2036 "Args passed by shell to Emacs, as a list of strings.");
2038 DEFVAR_LISP ("system-type", &Vsystem_type,
2039 "Value is symbol indicating type of operating system you are using.");
2040 Vsystem_type = intern (SYSTEM_TYPE);
2042 DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
2043 "Value is string indicating configuration Emacs was built for.");
2044 Vsystem_configuration = build_string (EMACS_CONFIGURATION);
2046 DEFVAR_LISP ("system-configuration-options", &Vsystem_configuration_options,
2047 "String containing the configuration options Emacs was built with.");
2048 Vsystem_configuration_options = build_string (EMACS_CONFIG_OPTIONS);
2050 DEFVAR_BOOL ("noninteractive", &noninteractive1,
2051 "Non-nil means Emacs is running without interactive terminal.");
2053 DEFVAR_LISP ("kill-emacs-hook", &Vkill_emacs_hook,
2054 "Hook to be run whenever kill-emacs is called.\n\
2055 Since kill-emacs may be invoked when the terminal is disconnected (or\n\
2056 in other similar situations), functions placed on this hook should not\n\
2057 expect to be able to interact with the user. To ask for confirmation,\n\
2058 see `kill-emacs-query-functions' instead.");
2059 Vkill_emacs_hook = Qnil;
2061 #ifdef SIGUSR1
2062 DEFVAR_LISP ("signal-USR1-hook", &Vsignal_USR1_hook,
2063 "Hook to be run whenever emacs receives a USR1 signal");
2064 Vsignal_USR1_hook = Qnil;
2065 #ifdef SIGUSR2
2066 DEFVAR_LISP ("signal-USR2-hook", &Vsignal_USR2_hook,
2067 "Hook to be run whenever emacs receives a USR2 signal");
2068 Vsignal_USR2_hook = Qnil;
2069 #endif
2070 #endif
2073 DEFVAR_INT ("emacs-priority", &emacs_priority,
2074 "Priority for Emacs to run at.\n\
2075 This value is effective only if set before Emacs is dumped,\n\
2076 and only if the Emacs executable is installed with setuid to permit\n\
2077 it to change priority. (Emacs sets its uid back to the real uid.)\n\
2078 Currently, you need to define SET_EMACS_PRIORITY in `config.h'\n\
2079 before you compile Emacs, to enable the code for this feature.");
2080 emacs_priority = 0;
2082 DEFVAR_LISP ("path-separator", &Vpath_separator,
2083 "The directory separator in search paths, as a string.");
2085 char c = SEPCHAR;
2086 Vpath_separator = make_string (&c, 1);
2089 DEFVAR_LISP ("invocation-name", &Vinvocation_name,
2090 "The program name that was used to run Emacs.\n\
2091 Any directory names are omitted.");
2093 DEFVAR_LISP ("invocation-directory", &Vinvocation_directory,
2094 "The directory in which the Emacs executable was found, to run it.\n\
2095 The value is nil if that directory's name is not known.");
2097 DEFVAR_LISP ("installation-directory", &Vinstallation_directory,
2098 "A directory within which to look for the `lib-src' and `etc' directories.\n\
2099 This is non-nil when we can't find those directories in their standard\n\
2100 installed locations, but we can find them\n\
2101 near where the Emacs executable was found.");
2102 Vinstallation_directory = Qnil;
2104 DEFVAR_LISP ("system-messages-locale", &Vsystem_messages_locale,
2105 "System locale for messages.");
2106 Vsystem_messages_locale = Qnil;
2108 DEFVAR_LISP ("previous-system-messages-locale",
2109 &Vprevious_system_messages_locale,
2110 "Most recently used system locale for messages.");
2111 Vprevious_system_messages_locale = Qnil;
2113 DEFVAR_LISP ("system-time-locale", &Vsystem_time_locale,
2114 "System locale for time.");
2115 Vsystem_time_locale = Qnil;
2117 DEFVAR_LISP ("previous-system-time-locale", &Vprevious_system_time_locale,
2118 "Most recently used system locale for time.");
2119 Vprevious_system_time_locale = Qnil;