* keyboard.c (Qratio): New symbol.
[emacs.git] / src / emacs.c
blob521225687dfdfeedab75e145e8b1b2239cf0015e
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
81 EMACS_INT PVEC_FLAG = PSEUDOVECTOR_FLAG;
83 /* Command line args from shell, as list of strings */
84 Lisp_Object Vcommand_line_args;
86 /* The name under which Emacs was invoked, with any leading directory
87 names discarded. */
88 Lisp_Object Vinvocation_name;
90 /* The directory name from which Emacs was invoked. */
91 Lisp_Object Vinvocation_directory;
93 /* The directory name in which to find subdirs such as lisp and etc.
94 nil means get them only from PATH_LOADSEARCH. */
95 Lisp_Object Vinstallation_directory;
97 /* Hook run by `kill-emacs' before it does really anything. */
98 Lisp_Object Vkill_emacs_hook;
100 #ifdef SIGUSR1
101 /* Hooks for signal USR1 and USR2 handing */
102 Lisp_Object Vsignal_USR1_hook;
103 #ifdef SIGUSR2
104 Lisp_Object Vsignal_USR2_hook;
105 #endif
106 #endif
108 /* Search path separator. */
109 Lisp_Object Vpath_separator;
111 /* Set nonzero after Emacs has started up the first time.
112 Prevents reinitialization of the Lisp world and keymaps
113 on subsequent starts. */
114 int initialized;
116 #ifdef DOUG_LEA_MALLOC
117 /* Preserves a pointer to the memory allocated that copies that
118 static data inside glibc's malloc. */
119 void *malloc_state_ptr;
120 /* From glibc, a routine that returns a copy of the malloc internal state. */
121 extern void *malloc_get_state ();
122 /* From glibc, a routine that overwrites the malloc internal state. */
123 extern void malloc_set_state ();
124 /* Non-zero if the MALLOC_CHECK_ enviroment variable was set while
125 dumping. Used to work around a bug in glibc's malloc. */
126 int malloc_using_checking;
127 #endif
129 /* Variable whose value is symbol giving operating system type. */
130 Lisp_Object Vsystem_type;
132 /* Variable whose value is string giving configuration built for. */
133 Lisp_Object Vsystem_configuration;
135 /* Variable whose value is string giving configuration options,
136 for use when reporting bugs. */
137 Lisp_Object Vsystem_configuration_options;
139 Lisp_Object Qfile_name_handler_alist;
141 /* Current and previous system locales for messages and time. */
142 Lisp_Object Vsystem_messages_locale;
143 Lisp_Object Vprevious_system_messages_locale;
144 Lisp_Object Vsystem_time_locale;
145 Lisp_Object Vprevious_system_time_locale;
147 /* If non-zero, emacs should not attempt to use an window-specific code,
148 but instead should use the virtual terminal under which it was started */
149 int inhibit_window_system;
151 /* If nonzero, set Emacs to run at this priority. This is also used
152 in child_setup and sys_suspend to make sure subshells run at normal
153 priority; Those functions have their own extern declaration. */
154 int emacs_priority;
156 /* If non-zero a filter or a sentinel is running. Tested to save the match
157 data on the first attempt to change it inside asynchronous code. */
158 int running_asynch_code;
160 #ifdef BSD_PGRPS
161 /* See sysdep.c. */
162 extern int inherited_pgroup;
163 #endif
165 #ifdef HAVE_X_WINDOWS
166 /* If non-zero, -d was specified, meaning we're using some window system. */
167 int display_arg;
168 #endif
170 /* An address near the bottom of the stack.
171 Tells GC how to save a copy of the stack. */
172 char *stack_bottom;
174 #ifdef HAVE_WINDOW_SYSTEM
175 extern Lisp_Object Vwindow_system;
176 #endif /* HAVE_WINDOW_SYSTEM */
178 extern Lisp_Object Vauto_save_list_file_name;
180 #ifdef USG_SHARED_LIBRARIES
181 /* If nonzero, this is the place to put the end of the writable segment
182 at startup. */
184 unsigned int bss_end = 0;
185 #endif
187 /* Nonzero means running Emacs without interactive terminal. */
189 int noninteractive;
191 /* Value of Lisp variable `noninteractive'.
192 Normally same as C variable `noninteractive'
193 but nothing terrible happens if user sets this one. */
195 int noninteractive1;
197 /* Save argv and argc. */
198 char **initial_argv;
199 int initial_argc;
201 static void sort_args ();
202 void syms_of_emacs ();
204 /* Signal code for the fatal signal that was received */
205 int fatal_error_code;
207 /* Nonzero if handling a fatal error already */
208 int fatal_error_in_progress;
210 #ifdef SIGUSR1
211 SIGTYPE
212 handle_USR1_signal (sig)
213 int sig;
215 struct input_event buf;
217 buf.kind = user_signal;
218 buf.code = 0;
219 buf.frame_or_window = selected_frame;
220 buf.modifiers = 0;
221 buf.timestamp = 0;
223 kbd_buffer_store_event (&buf);
225 #endif /* SIGUSR1 */
227 #ifdef SIGUSR2
228 SIGTYPE
229 handle_USR2_signal (sig)
230 int sig;
232 struct input_event buf;
234 buf.kind = user_signal;
235 buf.code = 1;
236 buf.frame_or_window = selected_frame;
237 buf.modifiers = 0;
238 buf.timestamp = 0;
240 kbd_buffer_store_event (&buf);
242 #endif /* SIGUSR2 */
244 /* Handle bus errors, illegal instruction, etc. */
245 SIGTYPE
246 fatal_error_signal (sig)
247 int sig;
249 fatal_error_code = sig;
250 signal (sig, SIG_DFL);
252 TOTALLY_UNBLOCK_INPUT;
254 /* If fatal error occurs in code below, avoid infinite recursion. */
255 if (! fatal_error_in_progress)
257 fatal_error_in_progress = 1;
259 shut_down_emacs (sig, 0, Qnil);
262 #ifdef VMS
263 LIB$STOP (SS$_ABORT);
264 #else
265 /* Signal the same code; this time it will really be fatal.
266 Remember that since we're in a signal handler, the signal we're
267 going to send is probably blocked, so we have to unblock it if we
268 want to really receive it. */
269 #ifndef MSDOS
270 sigunblock (sigmask (fatal_error_code));
271 #endif
272 kill (getpid (), fatal_error_code);
273 #endif /* not VMS */
276 #ifdef SIGDANGER
278 /* Handler for SIGDANGER. */
279 SIGTYPE
280 memory_warning_signal (sig)
281 int sig;
283 signal (sig, memory_warning_signal);
285 malloc_warning ("Operating system warns that virtual memory is running low.\n");
287 /* It might be unsafe to call do_auto_save now. */
288 force_auto_save_soon ();
290 #endif
292 /* We define abort, rather than using it from the library,
293 so that GDB can return from a breakpoint here.
294 MSDOS has its own definition on msdos.c */
296 #if ! defined (DOS_NT) && ! defined (NO_ABORT)
298 #ifndef ABORT_RETURN_TYPE
299 #define ABORT_RETURN_TYPE void
300 #endif
302 ABORT_RETURN_TYPE
303 abort ()
305 kill (getpid (), SIGABRT);
306 /* This shouldn't be executed, but it prevents a warning. */
307 exit (1);
309 #endif
312 /* Code for dealing with Lisp access to the Unix command line */
314 static void
315 init_cmdargs (argc, argv, skip_args)
316 int argc;
317 char **argv;
318 int skip_args;
320 register int i;
321 Lisp_Object name, dir, tem;
322 int count = specpdl_ptr - specpdl;
323 Lisp_Object raw_name;
325 initial_argv = argv;
326 initial_argc = argc;
328 raw_name = build_string (argv[0]);
330 /* Add /: to the front of the name
331 if it would otherwise be treated as magic. */
332 tem = Ffind_file_name_handler (raw_name, Qt);
333 if (! NILP (tem))
334 raw_name = concat2 (build_string ("/:"), raw_name);
336 Vinvocation_name = Ffile_name_nondirectory (raw_name);
337 Vinvocation_directory = Ffile_name_directory (raw_name);
339 /* If we got no directory in argv[0], search PATH to find where
340 Emacs actually came from. */
341 if (NILP (Vinvocation_directory))
343 Lisp_Object found;
344 int yes = openp (Vexec_path, Vinvocation_name,
345 EXEC_SUFFIXES, &found, 1);
346 if (yes == 1)
348 /* Add /: to the front of the name
349 if it would otherwise be treated as magic. */
350 tem = Ffind_file_name_handler (found, Qt);
351 if (! NILP (tem))
352 found = concat2 (build_string ("/:"), found);
353 Vinvocation_directory = Ffile_name_directory (found);
357 if (!NILP (Vinvocation_directory)
358 && NILP (Ffile_name_absolute_p (Vinvocation_directory)))
359 /* Emacs was started with relative path, like ./emacs.
360 Make it absolute. */
361 Vinvocation_directory = Fexpand_file_name (Vinvocation_directory, Qnil);
363 Vinstallation_directory = Qnil;
365 if (!NILP (Vinvocation_directory))
367 dir = Vinvocation_directory;
368 name = Fexpand_file_name (Vinvocation_name, dir);
369 while (1)
371 Lisp_Object tem, lib_src_exists;
372 Lisp_Object etc_exists, info_exists;
374 /* See if dir contains subdirs for use by Emacs.
375 Check for the ones that would exist in a build directory,
376 not including lisp and info. */
377 tem = Fexpand_file_name (build_string ("lib-src"), dir);
378 lib_src_exists = Ffile_exists_p (tem);
380 #ifdef MSDOS
381 /* MSDOS installations frequently remove lib-src, but we still
382 must set installation-directory, or else info won't find
383 its files (it uses the value of installation-directory). */
384 tem = Fexpand_file_name (build_string ("info"), dir);
385 info_exists = Ffile_exists_p (tem);
386 #else
387 info_exists = Qnil;
388 #endif
390 if (!NILP (lib_src_exists) || !NILP (info_exists))
392 tem = Fexpand_file_name (build_string ("etc"), dir);
393 etc_exists = Ffile_exists_p (tem);
394 if (!NILP (etc_exists))
396 Vinstallation_directory
397 = Ffile_name_as_directory (dir);
398 break;
402 /* See if dir's parent contains those subdirs. */
403 tem = Fexpand_file_name (build_string ("../lib-src"), dir);
404 lib_src_exists = Ffile_exists_p (tem);
407 #ifdef MSDOS
408 /* See the MSDOS commentary above. */
409 tem = Fexpand_file_name (build_string ("../info"), dir);
410 info_exists = Ffile_exists_p (tem);
411 #else
412 info_exists = Qnil;
413 #endif
415 if (!NILP (lib_src_exists) || !NILP (info_exists))
417 tem = Fexpand_file_name (build_string ("../etc"), dir);
418 etc_exists = Ffile_exists_p (tem);
419 if (!NILP (etc_exists))
421 tem = Fexpand_file_name (build_string (".."), dir);
422 Vinstallation_directory
423 = Ffile_name_as_directory (tem);
424 break;
428 /* If the Emacs executable is actually a link,
429 next try the dir that the link points into. */
430 tem = Ffile_symlink_p (name);
431 if (!NILP (tem))
433 name = Fexpand_file_name (tem, dir);
434 dir = Ffile_name_directory (name);
436 else
437 break;
441 Vcommand_line_args = Qnil;
443 for (i = argc - 1; i >= 0; i--)
445 if (i == 0 || i > skip_args)
446 Vcommand_line_args
447 = Fcons (build_string (argv[i]), Vcommand_line_args);
450 unbind_to (count, Qnil);
453 DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
454 "Return the program name that was used to run Emacs.\n\
455 Any directory names are omitted.")
458 return Fcopy_sequence (Vinvocation_name);
461 DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
462 0, 0, 0,
463 "Return the directory name in which the Emacs executable was located")
466 return Fcopy_sequence (Vinvocation_directory);
470 #ifdef VMS
471 #ifdef LINK_CRTL_SHARE
472 #ifdef SHARABLE_LIB_BUG
473 extern noshare char **environ;
474 #endif /* SHARABLE_LIB_BUG */
475 #endif /* LINK_CRTL_SHARE */
476 #endif /* VMS */
478 #ifdef HAVE_TZSET
479 /* A valid but unlikely value for the TZ environment value.
480 It is OK (though a bit slower) if the user actually chooses this value. */
481 static char dump_tz[] = "UtC0";
482 #endif
484 #ifndef ORDINARY_LINK
485 /* We don't include crtbegin.o and crtend.o in the link,
486 so these functions and variables might be missed.
487 Provide dummy definitions to avoid error.
488 (We don't have any real constructors or destructors.) */
489 #ifdef __GNUC__
490 #ifndef GCC_CTORS_IN_LIBC
491 void __do_global_ctors ()
493 void __do_global_ctors_aux ()
495 void __do_global_dtors ()
497 /* Linux has a bug in its library; avoid an error. */
498 #ifndef LINUX
499 char * __CTOR_LIST__[2] = { (char *) (-1), 0 };
500 #endif
501 char * __DTOR_LIST__[2] = { (char *) (-1), 0 };
502 #endif /* GCC_CTORS_IN_LIBC */
503 void __main ()
505 #endif /* __GNUC__ */
506 #endif /* ORDINARY_LINK */
508 /* Test whether the next argument in ARGV matches SSTR or a prefix of
509 LSTR (at least MINLEN characters). If so, then if VALPTR is non-null
510 (the argument is supposed to have a value) store in *VALPTR either
511 the next argument or the portion of this one after the equal sign.
512 ARGV is read starting at position *SKIPPTR; this index is advanced
513 by the number of arguments used.
515 Too bad we can't just use getopt for all of this, but we don't have
516 enough information to do it right. */
518 static int
519 argmatch (argv, argc, sstr, lstr, minlen, valptr, skipptr)
520 char **argv;
521 int argc;
522 char *sstr;
523 char *lstr;
524 int minlen;
525 char **valptr;
526 int *skipptr;
528 char *p;
529 int arglen;
530 char *arg;
532 /* Don't access argv[argc]; give up in advance. */
533 if (argc <= *skipptr + 1)
534 return 0;
536 arg = argv[*skipptr+1];
537 if (arg == NULL)
538 return 0;
539 if (strcmp (arg, sstr) == 0)
541 if (valptr != NULL)
543 *valptr = argv[*skipptr+2];
544 *skipptr += 2;
546 else
547 *skipptr += 1;
548 return 1;
550 arglen = (valptr != NULL && (p = index (arg, '=')) != NULL
551 ? p - arg : strlen (arg));
552 if (lstr == 0 || arglen < minlen || strncmp (arg, lstr, arglen) != 0)
553 return 0;
554 else if (valptr == NULL)
556 *skipptr += 1;
557 return 1;
559 else if (p != NULL)
561 *valptr = p+1;
562 *skipptr += 1;
563 return 1;
565 else if (argv[*skipptr+2] != NULL)
567 *valptr = argv[*skipptr+2];
568 *skipptr += 2;
569 return 1;
571 else
573 return 0;
577 #ifdef DOUG_LEA_MALLOC
579 /* malloc can be invoked even before main (e.g. by the dynamic
580 linker), so the dumped malloc state must be restored as early as
581 possible using this special hook. */
583 static void
584 malloc_initialize_hook ()
586 extern char **environ;
588 if (initialized)
590 if (!malloc_using_checking)
591 /* Work around a bug in glibc's malloc. MALLOC_CHECK_ must be
592 ignored if the heap to be restored was constructed without
593 malloc checking. Can't use unsetenv, since that calls malloc. */
595 char **p;
597 for (p = environ; *p; p++)
598 if (strncmp (*p, "MALLOC_CHECK_=", 14) == 0)
601 *p = p[1];
602 while (*++p);
603 break;
607 malloc_set_state (malloc_state_ptr);
608 free (malloc_state_ptr);
610 else
611 malloc_using_checking = getenv ("MALLOC_CHECK_") != NULL;
614 void (*__malloc_initialize_hook) () = malloc_initialize_hook;
616 #endif /* DOUG_LEA_MALLOC */
618 /* ARGSUSED */
620 main (argc, argv, envp)
621 int argc;
622 char **argv;
623 char **envp;
625 char stack_bottom_variable;
626 int do_initial_setlocale;
627 int skip_args = 0;
628 extern int errno;
629 extern int sys_nerr;
630 #ifdef HAVE_SETRLIMIT
631 struct rlimit rlim;
632 #endif
633 int no_loadup = 0;
635 #ifdef LINUX_SBRK_BUG
636 __sbrk (1);
637 #endif
639 #ifdef RUN_TIME_REMAP
640 if (initialized)
641 run_time_remap (argv[0]);
642 #endif
644 sort_args (argc, argv);
645 argc = 0;
646 while (argv[argc]) argc++;
648 if (argmatch (argv, argc, "-version", "--version", 3, NULL, &skip_args)
649 /* We don't know the version number unless this is a dumped Emacs.
650 So ignore --version otherwise. */
651 && initialized)
653 Lisp_Object tem;
654 tem = Fsymbol_value (intern ("emacs-version"));
655 if (!STRINGP (tem))
657 fprintf (stderr, "Invalid value of `emacs-version'\n");
658 exit (1);
660 else
662 printf ("GNU Emacs %s\n", XSTRING (tem)->data);
663 printf ("Copyright (C) 1999 Free Software Foundation, Inc.\n");
664 printf ("GNU Emacs comes with ABSOLUTELY NO WARRANTY.\n");
665 printf ("You may redistribute copies of Emacs\n");
666 printf ("under the terms of the GNU General Public License.\n");
667 printf ("For more information about these matters, ");
668 printf ("see the file named COPYING.\n");
669 exit (0);
673 /* Map in shared memory, if we are using that. */
674 #ifdef HAVE_SHM
675 if (argmatch (argv, argc, "-nl", "--no-shared-memory", 6, NULL, &skip_args))
677 map_in_data (0);
678 /* The shared memory was just restored, which clobbered this. */
679 skip_args = 1;
681 else
683 map_in_data (1);
684 /* The shared memory was just restored, which clobbered this. */
685 skip_args = 0;
687 #endif
689 #ifdef NeXT
691 extern int malloc_cookie;
692 /* This helps out unexnext.c. */
693 if (initialized)
694 if (malloc_jumpstart (malloc_cookie) != 0)
695 printf ("malloc jumpstart failed!\n");
697 #endif /* NeXT */
699 #ifdef VMS
700 /* If -map specified, map the data file in */
702 char *file;
703 if (argmatch (argv, argc, "-map", "--map-data", 3, &mapin_file, &skip_args))
704 mapin_data (file);
707 #ifdef LINK_CRTL_SHARE
708 #ifdef SHARABLE_LIB_BUG
709 /* Bletcherous shared libraries! */
710 if (!stdin)
711 stdin = fdopen (0, "r");
712 if (!stdout)
713 stdout = fdopen (1, "w");
714 if (!stderr)
715 stderr = fdopen (2, "w");
716 if (!environ)
717 environ = envp;
718 #endif /* SHARABLE_LIB_BUG */
719 #endif /* LINK_CRTL_SHARE */
720 #endif /* VMS */
722 #if defined (HAVE_SETRLIMIT) && defined (RLIMIT_STACK)
723 /* Extend the stack space available.
724 Don't do that if dumping, since some systems (e.g. DJGPP)
725 might define a smaller stack limit at that time. */
726 if (1
727 #ifndef CANNOT_DUMP
728 && (!noninteractive || initialized)
729 #endif
730 && !getrlimit (RLIMIT_STACK, &rlim))
732 long newlim;
733 extern int re_max_failures;
734 /* Approximate the amount regex.c needs per unit of re_max_failures. */
735 int ratio = 20 * sizeof (char *);
736 /* Then add 33% to cover the size of the smaller stacks that regex.c
737 successively allocates and discards, on its way to the maximum. */
738 ratio += ratio / 3;
739 /* Add in some extra to cover
740 what we're likely to use for other reasons. */
741 newlim = re_max_failures * ratio + 200000;
742 #ifdef __NetBSD__
743 /* NetBSD (at least NetBSD 1.2G and former) has a bug in its
744 stack allocation routine for new process that the allocation
745 fails if stack limit is not on page boundary. So, round up the
746 new limit to page boundary. */
747 newlim = (newlim + getpagesize () - 1) / getpagesize () * getpagesize();
748 #endif
749 if (newlim > rlim.rlim_max)
751 newlim = rlim.rlim_max;
752 /* Don't let regex.c overflow the stack we have. */
753 re_max_failures = (newlim - 200000) / ratio;
755 if (rlim.rlim_cur < newlim)
756 rlim.rlim_cur = newlim;
758 setrlimit (RLIMIT_STACK, &rlim);
760 #endif /* HAVE_SETRLIMIT and RLIMIT_STACK */
762 /* Record (approximately) where the stack begins. */
763 stack_bottom = &stack_bottom_variable;
765 #ifdef USG_SHARED_LIBRARIES
766 if (bss_end)
767 brk ((void *)bss_end);
768 #endif
770 clearerr (stdin);
772 #ifndef SYSTEM_MALLOC
773 /* Arrange to get warning messages as memory fills up. */
774 memory_warnings (0, malloc_warning);
776 /* Call malloc at least once, to run the initial __malloc_hook.
777 Also call realloc and free for consistency. */
778 free (realloc (malloc (4), 4));
780 /* Arrange to disable interrupt input inside malloc etc. */
781 uninterrupt_malloc ();
782 #endif /* not SYSTEM_MALLOC */
784 #ifdef MSDOS
785 /* We do all file input/output as binary files. When we need to translate
786 newlines, we do that manually. */
787 _fmode = O_BINARY;
789 #if __DJGPP__ >= 2
790 if (!isatty (fileno (stdin)))
791 setmode (fileno (stdin), O_BINARY);
792 if (!isatty (fileno (stdout)))
794 fflush (stdout);
795 setmode (fileno (stdout), O_BINARY);
797 #else /* not __DJGPP__ >= 2 */
798 (stdin)->_flag &= ~_IOTEXT;
799 (stdout)->_flag &= ~_IOTEXT;
800 (stderr)->_flag &= ~_IOTEXT;
801 #endif /* not __DJGPP__ >= 2 */
802 #endif /* MSDOS */
804 #ifdef SET_EMACS_PRIORITY
805 if (emacs_priority)
806 nice (emacs_priority);
807 setuid (getuid ());
808 #endif /* SET_EMACS_PRIORITY */
810 /* Skip initial setlocale if LC_ALL is "C", as it's not needed in that case.
811 The build procedure uses this while dumping, to ensure that the
812 dumped Emacs does not have its system locale tables initialized,
813 as that might cause screwups when the dumped Emacs starts up. */
815 char *lc_all = getenv ("LC_ALL");
816 do_initial_setlocale = ! lc_all || strcmp (lc_all, "C");
819 /* Set locale now, so that initial error messages are localized properly.
820 fixup_locale must wait until later, since it builds strings. */
821 if (do_initial_setlocale)
822 setlocale (LC_ALL, "");
824 #ifdef EXTRA_INITIALIZE
825 EXTRA_INITIALIZE;
826 #endif
828 inhibit_window_system = 0;
830 /* Handle the -t switch, which specifies filename to use as terminal */
831 while (1)
833 char *term;
834 if (argmatch (argv, argc, "-t", "--terminal", 4, &term, &skip_args))
836 int result;
837 emacs_close (0);
838 emacs_close (1);
839 result = emacs_open (term, O_RDWR, 0);
840 if (result < 0)
842 char *errstring = strerror (errno);
843 fprintf (stderr, "emacs: %s: %s\n", term, errstring);
844 exit (1);
846 dup (0);
847 if (! isatty (0))
849 fprintf (stderr, "emacs: %s: not a tty\n", term);
850 exit (1);
852 fprintf (stderr, "Using %s\n", term);
853 #ifdef HAVE_WINDOW_SYSTEM
854 inhibit_window_system = 1; /* -t => -nw */
855 #endif
857 else
858 break;
861 if (argmatch (argv, argc, "-nw", "--no-windows", 6, NULL, &skip_args))
862 inhibit_window_system = 1;
864 /* Handle the -batch switch, which means don't do interactive display. */
865 noninteractive = 0;
866 if (argmatch (argv, argc, "-batch", "--batch", 5, NULL, &skip_args))
867 noninteractive = 1;
869 /* Handle the --help option, which gives a usage message.. */
870 if (argmatch (argv, argc, "-help", "--help", 3, NULL, &skip_args))
872 printf ("\
873 Usage: %s [--batch] [-t term] [--terminal term]\n\
874 [-d display] [--display display] [-nw] [--no-windows]\n\
875 [-q] [--no-init-file] [-u user] [--user user] [--debug-init]\n\
876 [--unibyte] [--multibyte] [--version] [--no-site-file]\n\
877 [-f func] [--funcall func] [-l file] [--load file] [--eval expr]\n\
878 [--execute expr] [--visit file] [--file file] [--insert file]\n\
879 [+linenum] file-to-visit [--kill]\n\
880 Report bugs to bug-gnu-emacs@gnu.org. First, please see\n\
881 the Bugs section of the Emacs manual or the file BUGS.\n", argv[0]);
882 exit (0);
885 if (! noninteractive)
887 #ifdef BSD_PGRPS
888 if (initialized)
890 inherited_pgroup = EMACS_GETPGRP (0);
891 setpgrp (0, getpid ());
893 #else
894 #if defined (USG5) && defined (INTERRUPT_INPUT)
895 setpgrp ();
896 #endif
897 #endif
900 init_signals ();
902 /* Don't catch SIGHUP if dumping. */
903 if (1
904 #ifndef CANNOT_DUMP
905 && initialized
906 #endif
909 sigblock (sigmask (SIGHUP));
910 /* In --batch mode, don't catch SIGHUP if already ignored.
911 That makes nohup work. */
912 if (! noninteractive
913 || signal (SIGHUP, SIG_IGN) != SIG_IGN)
914 signal (SIGHUP, fatal_error_signal);
915 sigunblock (sigmask (SIGHUP));
918 if (
919 #ifndef CANNOT_DUMP
920 ! noninteractive || initialized
921 #else
923 #endif
926 /* Don't catch these signals in batch mode if dumping.
927 On some machines, this sets static data that would make
928 signal fail to work right when the dumped Emacs is run. */
929 signal (SIGQUIT, fatal_error_signal);
930 signal (SIGILL, fatal_error_signal);
931 signal (SIGTRAP, fatal_error_signal);
932 #ifdef SIGUSR1
933 signal (SIGUSR1, handle_USR1_signal);
934 #ifdef SIGUSR2
935 signal (SIGUSR2, handle_USR2_signal);
936 #endif
937 #endif
938 #ifdef SIGABRT
939 signal (SIGABRT, fatal_error_signal);
940 #endif
941 #ifdef SIGHWE
942 signal (SIGHWE, fatal_error_signal);
943 #endif
944 #ifdef SIGPRE
945 signal (SIGPRE, fatal_error_signal);
946 #endif
947 #ifdef SIGORE
948 signal (SIGORE, fatal_error_signal);
949 #endif
950 #ifdef SIGUME
951 signal (SIGUME, fatal_error_signal);
952 #endif
953 #ifdef SIGDLK
954 signal (SIGDLK, fatal_error_signal);
955 #endif
956 #ifdef SIGCPULIM
957 signal (SIGCPULIM, fatal_error_signal);
958 #endif
959 #ifdef SIGIOT
960 /* This is missing on some systems - OS/2, for example. */
961 signal (SIGIOT, fatal_error_signal);
962 #endif
963 #ifdef SIGEMT
964 signal (SIGEMT, fatal_error_signal);
965 #endif
966 signal (SIGFPE, fatal_error_signal);
967 #ifdef SIGBUS
968 signal (SIGBUS, fatal_error_signal);
969 #endif
970 signal (SIGSEGV, fatal_error_signal);
971 #ifdef SIGSYS
972 signal (SIGSYS, fatal_error_signal);
973 #endif
974 signal (SIGTERM, fatal_error_signal);
975 #ifdef SIGXCPU
976 signal (SIGXCPU, fatal_error_signal);
977 #endif
978 #ifdef SIGXFSZ
979 signal (SIGXFSZ, fatal_error_signal);
980 #endif /* SIGXFSZ */
982 #ifdef SIGDANGER
983 /* This just means available memory is getting low. */
984 signal (SIGDANGER, memory_warning_signal);
985 #endif
987 #ifdef AIX
988 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
989 signal (SIGXCPU, fatal_error_signal);
990 #ifndef _I386
991 signal (SIGIOINT, fatal_error_signal);
992 #endif
993 signal (SIGGRANT, fatal_error_signal);
994 signal (SIGRETRACT, fatal_error_signal);
995 signal (SIGSOUND, fatal_error_signal);
996 signal (SIGMSG, fatal_error_signal);
997 #endif /* AIX */
1000 noninteractive1 = noninteractive;
1002 /* Perform basic initializations (not merely interning symbols) */
1004 if (!initialized)
1006 init_alloc_once ();
1007 init_obarray ();
1008 init_eval_once ();
1009 init_charset_once ();
1010 init_coding_once ();
1011 init_syntax_once (); /* Create standard syntax table. */
1012 init_category_once (); /* Create standard category table. */
1013 /* Must be done before init_buffer */
1014 init_casetab_once ();
1015 init_buffer_once (); /* Create buffer table and some buffers */
1016 init_minibuf_once (); /* Create list of minibuffers */
1017 /* Must precede init_window_once */
1019 /* Call syms_of_xfaces before init_window_once because that
1020 function creates Vterminal_frame. Termcap frames now use
1021 faces, and the face implementation uses some symbols as
1022 face names. */
1023 #ifndef HAVE_NTGUI
1024 syms_of_xfaces ();
1025 #endif
1027 init_window_once (); /* Init the window system */
1028 init_fileio_once (); /* Must precede any path manipulation. */
1031 init_alloc ();
1033 if (do_initial_setlocale)
1035 fixup_locale ();
1036 Vsystem_messages_locale = Vprevious_system_messages_locale;
1037 Vsystem_time_locale = Vprevious_system_time_locale;
1040 init_eval ();
1041 init_coding ();
1042 init_data ();
1043 #ifdef CLASH_DETECTION
1044 init_filelock ();;
1045 #endif
1046 running_asynch_code = 0;
1048 /* Handle --unibyte and the EMACS_UNIBYTE envvar,
1049 but not while dumping. */
1050 if (
1051 #ifndef CANNOT_DUMP
1052 ! noninteractive || initialized
1053 #else
1055 #endif
1058 int inhibit_unibyte = 0;
1060 /* --multibyte overrides EMACS_UNIBYTE. */
1061 if (argmatch (argv, argc, "-no-unibyte", "--no-unibyte", 4, NULL, &skip_args)
1062 || argmatch (argv, argc, "-multibyte", "--multibyte", 4, NULL, &skip_args))
1063 inhibit_unibyte = 1;
1065 /* --unibyte requests that we set up to do everything with single-byte
1066 buffers and strings. We need to handle this before calling
1067 init_lread, init_editfns and other places that generate Lisp strings
1068 from text in the environment. */
1069 /* Actually this shouldn't be needed as of 20.4 in a generally
1070 unibyte environment. As handa says, environment values
1071 aren't now decoded; also existing buffers are now made
1072 unibyte during startup if .emacs sets unibyte. Tested with
1073 8-bit data in environment variables and /etc/passwd, setting
1074 unibyte and Latin-1 in .emacs. -- Dave Love */
1075 if (argmatch (argv, argc, "-unibyte", "--unibyte", 4, NULL, &skip_args)
1076 || argmatch (argv, argc, "-no-multibyte", "--no-multibyte", 4, NULL, &skip_args)
1077 || (getenv ("EMACS_UNIBYTE") && !inhibit_unibyte))
1079 Lisp_Object old_log_max;
1080 Lisp_Object symbol, tail;
1082 symbol = intern ("default-enable-multibyte-characters");
1083 Fset (symbol, Qnil);
1085 if (initialized)
1087 /* Erase pre-dump messages in *Messages* now so no abort. */
1088 old_log_max = Vmessage_log_max;
1089 XSETFASTINT (Vmessage_log_max, 0);
1090 message_dolog ("", 0, 1, 0);
1091 Vmessage_log_max = old_log_max;
1094 for (tail = Vbuffer_alist; CONSP (tail);
1095 tail = XCDR (tail))
1097 Lisp_Object buffer;
1099 buffer = Fcdr (XCAR (tail));
1100 /* Verify that all buffers are empty now, as they
1101 ought to be. */
1102 if (BUF_Z (XBUFFER (buffer)) > BUF_BEG (XBUFFER (buffer)))
1103 abort ();
1104 /* It is safe to do this crudely in an empty buffer. */
1105 XBUFFER (buffer)->enable_multibyte_characters = Qnil;
1110 no_loadup
1111 = !argmatch (argv, argc, "-nl", "--no-loadup", 6, NULL, &skip_args);
1114 #ifdef HAVE_X_WINDOWS
1115 /* Stupid kludge to catch command-line display spec. We can't
1116 handle this argument entirely in window system dependent code
1117 because we don't even know which window system dependent code
1118 to run until we've recognized this argument. */
1120 char *displayname = 0;
1121 int count_before = skip_args;
1123 /* Skip any number of -d options, but only use the last one. */
1124 while (1)
1126 int count_before_this = skip_args;
1128 if (argmatch (argv, argc, "-d", "--display", 3, &displayname, &skip_args))
1129 display_arg = 1;
1130 else if (argmatch (argv, argc, "-display", 0, 3, &displayname, &skip_args))
1131 display_arg = 1;
1132 else
1133 break;
1135 count_before = count_before_this;
1138 /* If we have the form --display=NAME,
1139 convert it into -d name.
1140 This requires inserting a new element into argv. */
1141 if (displayname != 0 && skip_args - count_before == 1)
1143 char **new = (char **) xmalloc (sizeof (char *) * (argc + 2));
1144 int j;
1146 for (j = 0; j < count_before + 1; j++)
1147 new[j] = argv[j];
1148 new[count_before + 1] = "-d";
1149 new[count_before + 2] = displayname;
1150 for (j = count_before + 2; j <argc; j++)
1151 new[j + 1] = argv[j];
1152 argv = new;
1153 argc++;
1155 /* Change --display to -d, when its arg is separate. */
1156 else if (displayname != 0 && skip_args > count_before
1157 && argv[count_before + 1][1] == '-')
1158 argv[count_before + 1] = "-d";
1160 /* Don't actually discard this arg. */
1161 skip_args = count_before;
1163 #endif
1165 /* argmatch must not be used after here,
1166 except when bulding temacs
1167 because the -d argument has not been skipped in skip_args. */
1169 #ifdef MSDOS
1170 /* Call early 'cause init_environment needs it. */
1171 init_dosfns ();
1172 /* Set defaults for several environment variables. */
1173 if (initialized)
1174 init_environment (argc, argv, skip_args);
1175 else
1176 tzset ();
1177 #endif /* MSDOS */
1179 #ifdef WINDOWSNT
1180 /* Initialize environment from registry settings. */
1181 init_environment (argv);
1182 init_ntproc (); /* must precede init_editfns */
1183 #endif
1185 /* egetenv is a pretty low-level facility, which may get called in
1186 many circumstances; it seems flimsy to put off initializing it
1187 until calling init_callproc. */
1188 set_process_environment ();
1189 /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
1190 if this is not done. Do it after set_process_environment so that we
1191 don't pollute Vprocess_environment. */
1192 /* Setting LANG here will defeat the startup locale processing... */
1193 #ifdef AIX3_2
1194 putenv ("LANG=C");
1195 #endif
1197 init_buffer (); /* Init default directory of main buffer */
1199 init_callproc_1 (); /* Must precede init_cmdargs and init_sys_modes. */
1200 init_cmdargs (argc, argv, skip_args); /* Must precede init_lread. */
1202 if (initialized)
1204 /* Erase any pre-dump messages in the message log, to avoid confusion */
1205 Lisp_Object old_log_max;
1206 old_log_max = Vmessage_log_max;
1207 XSETFASTINT (Vmessage_log_max, 0);
1208 message_dolog ("", 0, 1, 0);
1209 Vmessage_log_max = old_log_max;
1212 init_callproc (); /* Must follow init_cmdargs but not init_sys_modes. */
1213 init_lread ();
1215 /* Intern the names of all standard functions and variables;
1216 define standard keys. */
1218 if (!initialized)
1220 /* The basic levels of Lisp must come first */
1221 /* And data must come first of all
1222 for the sake of symbols like error-message */
1223 syms_of_data ();
1224 syms_of_alloc ();
1225 syms_of_lread ();
1226 syms_of_print ();
1227 syms_of_eval ();
1228 syms_of_fns ();
1229 syms_of_floatfns ();
1231 syms_of_abbrev ();
1232 syms_of_buffer ();
1233 syms_of_bytecode ();
1234 syms_of_callint ();
1235 syms_of_casefiddle ();
1236 syms_of_casetab ();
1237 syms_of_callproc ();
1238 syms_of_category ();
1239 syms_of_ccl ();
1240 syms_of_charset ();
1241 syms_of_cmds ();
1242 #ifndef NO_DIR_LIBRARY
1243 syms_of_dired ();
1244 #endif /* not NO_DIR_LIBRARY */
1245 syms_of_display ();
1246 syms_of_doc ();
1247 syms_of_editfns ();
1248 syms_of_emacs ();
1249 syms_of_fileio ();
1250 syms_of_coding (); /* This should be after syms_of_fileio. */
1251 #ifdef CLASH_DETECTION
1252 syms_of_filelock ();
1253 #endif /* CLASH_DETECTION */
1254 syms_of_indent ();
1255 syms_of_insdel ();
1256 syms_of_keyboard ();
1257 syms_of_keymap ();
1258 syms_of_macros ();
1259 syms_of_marker ();
1260 syms_of_minibuf ();
1261 syms_of_mocklisp ();
1262 syms_of_process ();
1263 syms_of_search ();
1264 syms_of_frame ();
1265 syms_of_syntax ();
1266 syms_of_term ();
1267 syms_of_undo ();
1268 #ifdef HAVE_SOUND
1269 syms_of_sound ();
1270 #endif
1272 syms_of_textprop ();
1273 #ifdef VMS
1274 syms_of_vmsproc ();
1275 #endif /* VMS */
1276 #ifdef WINDOWSNT
1277 syms_of_ntproc ();
1278 #endif /* WINDOWSNT */
1279 syms_of_window ();
1280 syms_of_xdisp ();
1281 #ifdef HAVE_X_WINDOWS
1282 syms_of_xterm ();
1283 syms_of_xfns ();
1284 syms_of_fontset ();
1285 #ifdef HAVE_X11
1286 syms_of_xselect ();
1287 #endif
1288 #endif /* HAVE_X_WINDOWS */
1290 #ifndef HAVE_NTGUI
1291 syms_of_xmenu ();
1292 #endif
1294 #ifdef HAVE_NTGUI
1295 syms_of_w32term ();
1296 syms_of_w32fns ();
1297 syms_of_w32faces ();
1298 syms_of_w32select ();
1299 syms_of_w32menu ();
1300 syms_of_fontset ();
1301 #endif /* HAVE_NTGUI */
1303 #ifdef SYMS_SYSTEM
1304 SYMS_SYSTEM;
1305 #endif
1307 #ifdef SYMS_MACHINE
1308 SYMS_MACHINE;
1309 #endif
1311 keys_of_casefiddle ();
1312 keys_of_cmds ();
1313 keys_of_buffer ();
1314 keys_of_keyboard ();
1315 keys_of_keymap ();
1316 keys_of_macros ();
1317 keys_of_minibuf ();
1318 keys_of_window ();
1319 keys_of_frame ();
1322 if (!noninteractive)
1324 #ifdef VMS
1325 init_vms_input ();/* init_display calls get_frame_size, that needs this */
1326 #endif /* VMS */
1327 init_display (); /* Determine terminal type. init_sys_modes uses results */
1329 init_keyboard (); /* This too must precede init_sys_modes */
1330 #ifdef VMS
1331 init_vmsproc (); /* And this too. */
1332 #endif /* VMS */
1333 init_sys_modes (); /* Init system terminal modes (RAW or CBREAK, etc.) */
1334 #ifdef HAVE_X_WINDOWS
1335 init_xfns ();
1336 #endif /* HAVE_X_WINDOWS */
1337 init_fns ();
1338 init_xdisp ();
1339 init_macros ();
1340 init_editfns ();
1341 #ifdef LISP_FLOAT_TYPE
1342 init_floatfns ();
1343 #endif
1344 #ifdef VMS
1345 init_vmsfns ();
1346 #endif /* VMS */
1347 init_process ();
1348 #ifdef HAVE_SOUND
1349 init_sound ();
1350 #endif
1352 if (!initialized)
1354 char *file;
1355 /* Handle -l loadup, args passed by Makefile. */
1356 if (argmatch (argv, argc, "-l", "--load", 3, &file, &skip_args))
1357 Vtop_level = Fcons (intern ("load"),
1358 Fcons (build_string (file), Qnil));
1359 #ifdef CANNOT_DUMP
1360 /* Unless next switch is -nl, load "loadup.el" first thing. */
1361 if (! no_loadup)
1362 Vtop_level = Fcons (intern ("load"),
1363 Fcons (build_string ("loadup.el"), Qnil));
1364 #endif /* CANNOT_DUMP */
1367 if (initialized)
1369 #ifdef HAVE_TZSET
1371 /* If the execution TZ happens to be the same as the dump TZ,
1372 change it to some other value and then change it back,
1373 to force the underlying implementation to reload the TZ info.
1374 This is needed on implementations that load TZ info from files,
1375 since the TZ file contents may differ between dump and execution. */
1376 char *tz = getenv ("TZ");
1377 if (tz && !strcmp (tz, dump_tz))
1379 ++*tz;
1380 tzset ();
1381 --*tz;
1384 #endif
1387 /* Gerd Moellmann <gerd@acm.org> says this makes profiling work on
1388 FreeBSD. It might work on some other systems too.
1389 Give it a try and tell me if it works on your system. */
1390 #if defined (__FreeBSD__) || defined (__linux)
1391 #ifdef PROFILING
1392 if (initialized)
1394 extern void _mcleanup ();
1395 extern char etext;
1396 extern void safe_bcopy ();
1397 extern void dump_opcode_frequencies ();
1399 atexit (_mcleanup);
1400 // atexit (dump_opcode_frequencies);
1401 /* This uses safe_bcopy because that function comes first in the
1402 Emacs executable. It might be better to use something that
1403 gives the start of the text segment, but start_of_text is not
1404 defined on all systems now. */
1405 monstartup (safe_bcopy, &etext);
1407 else
1408 moncontrol (0);
1409 #endif
1410 #endif
1412 initialized = 1;
1414 #ifdef LOCALTIME_CACHE
1415 /* Some versions of localtime have a bug. They cache the value of the time
1416 zone rather than looking it up every time. Since localtime() is
1417 called to bolt the undumping time into the undumped emacs, this
1418 results in localtime ignoring the TZ environment variable.
1419 This flushes the new TZ value into localtime. */
1420 tzset ();
1421 #endif /* defined (LOCALTIME_CACHE) */
1423 /* Enter editor command loop. This never returns. */
1424 Frecursive_edit ();
1425 /* NOTREACHED */
1428 /* Sort the args so we can find the most important ones
1429 at the beginning of argv. */
1431 /* First, here's a table of all the standard options. */
1433 struct standard_args
1435 char *name;
1436 char *longname;
1437 int priority;
1438 int nargs;
1441 struct standard_args standard_args[] =
1443 { "-version", "--version", 150, 0 },
1444 #ifdef HAVE_SHM
1445 { "-nl", "--no-shared-memory", 140, 0 },
1446 #endif
1447 #ifdef VMS
1448 { "-map", "--map-data", 130, 0 },
1449 #endif
1450 { "-t", "--terminal", 120, 1 },
1451 { "-nw", "--no-windows", 110, 0 },
1452 { "-batch", "--batch", 100, 0 },
1453 { "-help", "--help", 90, 0 },
1454 { "-no-unibyte", "--no-unibyte", 83, 0 },
1455 { "-multibyte", "--multibyte", 82, 0 },
1456 { "-unibyte", "--unibyte", 81, 0 },
1457 { "-no-multibyte", "--no-multibyte", 80, 0 },
1458 #ifdef CANNOT_DUMP
1459 { "-nl", "--no-loadup", 70, 0 },
1460 #endif
1461 /* -d must come last before the options handled in startup.el. */
1462 { "-d", "--display", 60, 1 },
1463 { "-display", 0, 60, 1 },
1464 /* Now for the options handled in startup.el. */
1465 { "-q", "--no-init-file", 50, 0 },
1466 { "-no-init-file", 0, 50, 0 },
1467 { "-no-site-file", "--no-site-file", 40, 0 },
1468 { "-u", "--user", 30, 1 },
1469 { "-user", 0, 30, 1 },
1470 { "-debug-init", "--debug-init", 20, 0 },
1471 { "-i", "--icon-type", 15, 0 },
1472 { "-itype", 0, 15, 0 },
1473 { "-iconic", "--iconic", 15, 0 },
1474 { "-bg", "--background-color", 10, 1 },
1475 { "-background", 0, 10, 1 },
1476 { "-fg", "--foreground-color", 10, 1 },
1477 { "-foreground", 0, 10, 1 },
1478 { "-bd", "--border-color", 10, 1 },
1479 { "-bw", "--border-width", 10, 1 },
1480 { "-ib", "--internal-border", 10, 1 },
1481 { "-ms", "--mouse-color", 10, 1 },
1482 { "-cr", "--cursor-color", 10, 1 },
1483 { "-fn", "--font", 10, 1 },
1484 { "-font", 0, 10, 1 },
1485 { "-g", "--geometry", 10, 1 },
1486 { "-geometry", 0, 10, 1 },
1487 { "-T", "--title", 10, 1 },
1488 { "-title", 0, 10, 1 },
1489 { "-name", "--name", 10, 1 },
1490 { "-xrm", "--xrm", 10, 1 },
1491 { "-r", "--reverse-video", 5, 0 },
1492 { "-rv", 0, 5, 0 },
1493 { "-reverse", 0, 5, 0 },
1494 { "-hb", "--horizontal-scroll-bars", 5, 0 },
1495 { "-vb", "--vertical-scroll-bars", 5, 0 },
1496 /* These have the same priority as ordinary file name args,
1497 so they are not reordered with respect to those. */
1498 { "-L", "--directory", 0, 1 },
1499 { "-directory", 0, 0, 1 },
1500 { "-l", "--load", 0, 1 },
1501 { "-load", 0, 0, 1 },
1502 { "-f", "--funcall", 0, 1 },
1503 { "-funcall", 0, 0, 1 },
1504 { "-eval", "--eval", 0, 1 },
1505 { "-execute", "--execute", 0, 1 },
1506 { "-find-file", "--find-file", 0, 1 },
1507 { "-visit", "--visit", 0, 1 },
1508 { "-file", "--file", 0, 1 },
1509 { "-insert", "--insert", 0, 1 },
1510 /* This should be processed after ordinary file name args and the like. */
1511 { "-kill", "--kill", -10, 0 },
1514 /* Reorder the elements of ARGV (assumed to have ARGC elements)
1515 so that the highest priority ones come first.
1516 Do not change the order of elements of equal priority.
1517 If an option takes an argument, keep it and its argument together.
1519 If an option that takes no argument appears more
1520 than once, eliminate all but one copy of it. */
1522 static void
1523 sort_args (argc, argv)
1524 int argc;
1525 char **argv;
1527 char **new = (char **) xmalloc (sizeof (char *) * argc);
1528 /* For each element of argv,
1529 the corresponding element of options is:
1530 0 for an option that takes no arguments,
1531 1 for an option that takes one argument, etc.
1532 -1 for an ordinary non-option argument. */
1533 int *options = (int *) xmalloc (sizeof (int) * argc);
1534 int *priority = (int *) xmalloc (sizeof (int) * argc);
1535 int to = 1;
1536 int incoming_used = 1;
1537 int from;
1538 int i;
1540 /* Categorize all the options,
1541 and figure out which argv elts are option arguments. */
1542 for (from = 1; from < argc; from++)
1544 options[from] = -1;
1545 priority[from] = 0;
1546 if (argv[from][0] == '-')
1548 int match, thislen;
1549 char *equals;
1551 /* If we have found "--", don't consider
1552 any more arguments as options. */
1553 if (argv[from][1] == '-' && argv[from][2] == 0)
1555 /* Leave the "--", and everything following it, at the end. */
1556 for (; from < argc; from++)
1558 priority[from] = -100;
1559 options[from] = -1;
1561 break;
1564 /* Look for a match with a known old-fashioned option. */
1565 for (i = 0; i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1566 if (!strcmp (argv[from], standard_args[i].name))
1568 options[from] = standard_args[i].nargs;
1569 priority[from] = standard_args[i].priority;
1570 if (from + standard_args[i].nargs >= argc)
1571 fatal ("Option `%s' requires an argument\n", argv[from]);
1572 from += standard_args[i].nargs;
1573 goto done;
1576 /* Look for a match with a known long option.
1577 MATCH is -1 if no match so far, -2 if two or more matches so far,
1578 >= 0 (the table index of the match) if just one match so far. */
1579 if (argv[from][1] == '-')
1581 match = -1;
1582 thislen = strlen (argv[from]);
1583 equals = index (argv[from], '=');
1584 if (equals != 0)
1585 thislen = equals - argv[from];
1587 for (i = 0;
1588 i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1589 if (standard_args[i].longname
1590 && !strncmp (argv[from], standard_args[i].longname,
1591 thislen))
1593 if (match == -1)
1594 match = i;
1595 else
1596 match = -2;
1599 /* If we found exactly one match, use that. */
1600 if (match >= 0)
1602 options[from] = standard_args[match].nargs;
1603 priority[from] = standard_args[match].priority;
1604 /* If --OPTION=VALUE syntax is used,
1605 this option uses just one argv element. */
1606 if (equals != 0)
1607 options[from] = 0;
1608 if (from + options[from] >= argc)
1609 fatal ("Option `%s' requires an argument\n", argv[from]);
1610 from += options[from];
1613 done: ;
1617 /* Copy the arguments, in order of decreasing priority, to NEW. */
1618 new[0] = argv[0];
1619 while (incoming_used < argc)
1621 int best = -1;
1622 int best_priority = -9999;
1624 /* Find the highest priority remaining option.
1625 If several have equal priority, take the first of them. */
1626 for (from = 1; from < argc; from++)
1628 if (argv[from] != 0 && priority[from] > best_priority)
1630 best_priority = priority[from];
1631 best = from;
1633 /* Skip option arguments--they are tied to the options. */
1634 if (options[from] > 0)
1635 from += options[from];
1638 if (best < 0)
1639 abort ();
1641 /* Copy the highest priority remaining option, with its args, to NEW.
1642 Unless it is a duplicate of the previous one. */
1643 if (! (options[best] == 0
1644 && ! strcmp (new[to - 1], argv[best])))
1646 new[to++] = argv[best];
1647 for (i = 0; i < options[best]; i++)
1648 new[to++] = argv[best + i + 1];
1651 incoming_used += 1 + (options[best] > 0 ? options[best] : 0);
1653 /* Clear out this option in ARGV. */
1654 argv[best] = 0;
1655 for (i = 0; i < options[best]; i++)
1656 argv[best + i + 1] = 0;
1659 /* If duplicate options were deleted, fill up extra space with null ptrs. */
1660 while (to < argc)
1661 new[to++] = 0;
1663 bcopy (new, argv, sizeof (char *) * argc);
1665 free (options);
1666 free (new);
1667 free (priority);
1670 DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
1671 "Exit the Emacs job and kill it.\n\
1672 If ARG is an integer, return ARG as the exit program code.\n\
1673 If ARG is a string, stuff it as keyboard input.\n\n\
1674 The value of `kill-emacs-hook', if not void,\n\
1675 is a list of functions (of no args),\n\
1676 all of which are called before Emacs is actually killed.")
1677 (arg)
1678 Lisp_Object arg;
1680 struct gcpro gcpro1;
1682 GCPRO1 (arg);
1684 if (feof (stdin))
1685 arg = Qt;
1687 if (!NILP (Vrun_hooks) && !noninteractive)
1688 call1 (Vrun_hooks, intern ("kill-emacs-hook"));
1690 UNGCPRO;
1692 /* Is it really necessary to do this deassign
1693 when we are going to exit anyway? */
1694 /* #ifdef VMS
1695 stop_vms_input ();
1696 #endif */
1698 shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
1700 /* If we have an auto-save list file,
1701 kill it because we are exiting Emacs deliberately (not crashing).
1702 Do it after shut_down_emacs, which does an auto-save. */
1703 if (STRINGP (Vauto_save_list_file_name))
1704 unlink (XSTRING (Vauto_save_list_file_name)->data);
1706 exit (INTEGERP (arg) ? XINT (arg)
1707 #ifdef VMS
1709 #else
1711 #endif
1713 /* NOTREACHED */
1717 /* Perform an orderly shutdown of Emacs. Autosave any modified
1718 buffers, kill any child processes, clean up the terminal modes (if
1719 we're in the foreground), and other stuff like that. Don't perform
1720 any redisplay; this may be called when Emacs is shutting down in
1721 the background, or after its X connection has died.
1723 If SIG is a signal number, print a message for it.
1725 This is called by fatal signal handlers, X protocol error handlers,
1726 and Fkill_emacs. */
1728 void
1729 shut_down_emacs (sig, no_x, stuff)
1730 int sig, no_x;
1731 Lisp_Object stuff;
1733 /* Prevent running of hooks from now on. */
1734 Vrun_hooks = Qnil;
1736 /* If we are controlling the terminal, reset terminal modes */
1737 #ifdef EMACS_HAVE_TTY_PGRP
1739 int pgrp = EMACS_GETPGRP (0);
1741 int tpgrp;
1742 if (EMACS_GET_TTY_PGRP (0, &tpgrp) != -1
1743 && tpgrp == pgrp)
1745 fflush (stdout);
1746 reset_sys_modes ();
1747 if (sig && sig != SIGTERM)
1748 fprintf (stderr, "Fatal error (%d).", sig);
1751 #else
1752 fflush (stdout);
1753 reset_sys_modes ();
1754 #endif
1756 stuff_buffered_input (stuff);
1758 kill_buffer_processes (Qnil);
1759 Fdo_auto_save (Qt, Qnil);
1761 #ifdef CLASH_DETECTION
1762 unlock_all_files ();
1763 #endif
1765 #ifdef VMS
1766 kill_vms_processes ();
1767 #endif
1769 #if 0 /* This triggers a bug in XCloseDisplay and is not needed. */
1770 #ifdef HAVE_X_WINDOWS
1771 /* It's not safe to call intern here. Maybe we are crashing. */
1772 if (!noninteractive && SYMBOLP (Vwindow_system)
1773 && XSYMBOL (Vwindow_system)->name->size == 1
1774 && XSYMBOL (Vwindow_system)->name->data[0] == 'x'
1775 && ! no_x)
1776 Fx_close_current_connection ();
1777 #endif /* HAVE_X_WINDOWS */
1778 #endif
1780 #ifdef SIGIO
1781 /* There is a tendency for a SIGIO signal to arrive within exit,
1782 and cause a SIGHUP because the input descriptor is already closed. */
1783 unrequest_sigio ();
1784 signal (SIGIO, SIG_IGN);
1785 #endif
1787 #ifdef WINDOWSNT
1788 term_ntproc ();
1789 #endif
1791 check_glyph_memory ();
1792 check_message_stack ();
1794 #ifdef MSDOS
1795 dos_cleanup ();
1796 #endif
1801 #ifndef CANNOT_DUMP
1803 #ifdef HAVE_SHM
1805 DEFUN ("dump-emacs-data", Fdump_emacs_data, Sdump_emacs_data, 1, 1, 0,
1806 "Dump current state of Emacs into data file FILENAME.\n\
1807 This function exists on systems that use HAVE_SHM.")
1808 (filename)
1809 Lisp_Object filename;
1811 extern char my_edata[];
1812 Lisp_Object tem;
1814 CHECK_STRING (filename, 0);
1815 filename = Fexpand_file_name (filename, Qnil);
1817 tem = Vpurify_flag;
1818 Vpurify_flag = Qnil;
1820 fflush (stdout);
1821 /* Tell malloc where start of impure now is */
1822 /* Also arrange for warnings when nearly out of space. */
1823 #ifndef SYSTEM_MALLOC
1824 memory_warnings (my_edata, malloc_warning);
1825 #endif
1826 map_out_data (XSTRING (filename)->data);
1828 Vpurify_flag = tem;
1830 return Qnil;
1833 #else /* not HAVE_SHM */
1835 DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
1836 "Dump current state of Emacs into executable file FILENAME.\n\
1837 Take symbols from SYMFILE (presumably the file you executed to run Emacs).\n\
1838 This is used in the file `loadup.el' when building Emacs.\n\
1840 You must run Emacs in batch mode in order to dump it.")
1841 (filename, symfile)
1842 Lisp_Object filename, symfile;
1844 extern char my_edata[];
1845 Lisp_Object tem;
1846 Lisp_Object symbol;
1847 int count = specpdl_ptr - specpdl;
1849 if (! noninteractive)
1850 error ("Dumping Emacs works only in batch mode");
1852 /* Bind `command-line-processed' to nil before dumping,
1853 so that the dumped Emacs will process its command line
1854 and set up to work with X windows if appropriate. */
1855 symbol = intern ("command-line-process");
1856 specbind (symbol, Qnil);
1858 CHECK_STRING (filename, 0);
1859 filename = Fexpand_file_name (filename, Qnil);
1860 if (!NILP (symfile))
1862 CHECK_STRING (symfile, 0);
1863 if (XSTRING (symfile)->size)
1864 symfile = Fexpand_file_name (symfile, Qnil);
1867 tem = Vpurify_flag;
1868 Vpurify_flag = Qnil;
1870 #ifdef HAVE_TZSET
1871 set_time_zone_rule (dump_tz);
1872 #ifndef LOCALTIME_CACHE
1873 /* Force a tz reload, since set_time_zone_rule doesn't. */
1874 tzset ();
1875 #endif
1876 #endif
1878 fflush (stdout);
1879 #ifdef VMS
1880 mapout_data (XSTRING (filename)->data);
1881 #else
1882 /* Tell malloc where start of impure now is */
1883 /* Also arrange for warnings when nearly out of space. */
1884 #ifndef SYSTEM_MALLOC
1885 #ifndef WINDOWSNT
1886 /* On Windows, this was done before dumping, and that once suffices.
1887 Meanwhile, my_edata is not valid on Windows. */
1888 memory_warnings (my_edata, malloc_warning);
1889 #endif /* not WINDOWSNT */
1890 #endif
1891 #ifdef DOUG_LEA_MALLOC
1892 malloc_state_ptr = malloc_get_state ();
1893 #endif
1894 unexec (XSTRING (filename)->data,
1895 !NILP (symfile) ? XSTRING (symfile)->data : 0, my_edata, 0, 0);
1896 #ifdef DOUG_LEA_MALLOC
1897 free (malloc_state_ptr);
1898 #endif
1899 #endif /* not VMS */
1901 Vpurify_flag = tem;
1903 return unbind_to (count, Qnil);
1906 #endif /* not HAVE_SHM */
1908 #endif /* not CANNOT_DUMP */
1910 #if HAVE_SETLOCALE
1911 /* Recover from setlocale (LC_ALL, ""). */
1912 void
1913 fixup_locale ()
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");
1920 /* Set system locale CATEGORY, with previous locale *PLOCALE, to
1921 DESIRED_LOCALE. */
1922 static void
1923 synchronize_locale (category, plocale, desired_locale)
1924 int category;
1925 Lisp_Object *plocale;
1926 Lisp_Object desired_locale;
1928 if (! EQ (*plocale, desired_locale))
1930 *plocale = desired_locale;
1931 setlocale (category, (STRINGP (desired_locale)
1932 ? (char *)(XSTRING (desired_locale)->data)
1933 : ""));
1937 /* Set system time locale to match Vsystem_time_locale, if possible. */
1938 void
1939 synchronize_system_time_locale ()
1941 synchronize_locale (LC_TIME, &Vprevious_system_time_locale,
1942 Vsystem_time_locale);
1945 /* Set system messages locale to match Vsystem_messages_locale, if
1946 possible. */
1947 void
1948 synchronize_system_messages_locale ()
1950 #ifdef LC_MESSAGES
1951 synchronize_locale (LC_MESSAGES, &Vprevious_system_messages_locale,
1952 Vsystem_messages_locale);
1953 #endif
1955 #endif /* HAVE_SETLOCALE */
1957 #ifndef SEPCHAR
1958 #define SEPCHAR ':'
1959 #endif
1961 Lisp_Object
1962 decode_env_path (evarname, defalt)
1963 char *evarname, *defalt;
1965 register char *path, *p;
1966 Lisp_Object lpath, element, tem;
1968 /* It's okay to use getenv here, because this function is only used
1969 to initialize variables when Emacs starts up, and isn't called
1970 after that. */
1971 if (evarname != 0)
1972 path = (char *) getenv (evarname);
1973 else
1974 path = 0;
1975 if (!path)
1976 path = defalt;
1977 #ifdef DOS_NT
1978 /* Ensure values from the environment use the proper directory separator. */
1979 if (path)
1981 p = alloca (strlen (path) + 1);
1982 strcpy (p, path);
1983 path = p;
1985 if ('/' == DIRECTORY_SEP)
1986 dostounix_filename (path);
1987 else
1988 unixtodos_filename (path);
1990 #endif
1991 lpath = Qnil;
1992 while (1)
1994 p = index (path, SEPCHAR);
1995 if (!p) p = path + strlen (path);
1996 element = (p - path ? make_string (path, p - path)
1997 : build_string ("."));
1999 /* Add /: to the front of the name
2000 if it would otherwise be treated as magic. */
2001 tem = Ffind_file_name_handler (element, Qt);
2002 if (! NILP (tem))
2003 element = concat2 (build_string ("/:"), element);
2005 lpath = Fcons (element, lpath);
2006 if (*p)
2007 path = p + 1;
2008 else
2009 break;
2011 return Fnreverse (lpath);
2014 void
2015 syms_of_emacs ()
2017 Qfile_name_handler_alist = intern ("file-name-handler-alist");
2018 staticpro (&Qfile_name_handler_alist);
2020 #ifndef CANNOT_DUMP
2021 #ifdef HAVE_SHM
2022 defsubr (&Sdump_emacs_data);
2023 #else
2024 defsubr (&Sdump_emacs);
2025 #endif
2026 #endif
2028 defsubr (&Skill_emacs);
2030 defsubr (&Sinvocation_name);
2031 defsubr (&Sinvocation_directory);
2033 DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
2034 "Args passed by shell to Emacs, as a list of strings.");
2036 DEFVAR_LISP ("system-type", &Vsystem_type,
2037 "Value is symbol indicating type of operating system you are using.");
2038 Vsystem_type = intern (SYSTEM_TYPE);
2040 DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
2041 "Value is string indicating configuration Emacs was built for.");
2042 Vsystem_configuration = build_string (EMACS_CONFIGURATION);
2044 DEFVAR_LISP ("system-configuration-options", &Vsystem_configuration_options,
2045 "String containing the configuration options Emacs was built with.");
2046 Vsystem_configuration_options = build_string (EMACS_CONFIG_OPTIONS);
2048 DEFVAR_BOOL ("noninteractive", &noninteractive1,
2049 "Non-nil means Emacs is running without interactive terminal.");
2051 DEFVAR_LISP ("kill-emacs-hook", &Vkill_emacs_hook,
2052 "Hook to be run whenever kill-emacs is called.\n\
2053 Since kill-emacs may be invoked when the terminal is disconnected (or\n\
2054 in other similar situations), functions placed on this hook should not\n\
2055 expect to be able to interact with the user. To ask for confirmation,\n\
2056 see `kill-emacs-query-functions' instead.");
2057 Vkill_emacs_hook = Qnil;
2059 #ifdef SIGUSR1
2060 DEFVAR_LISP ("signal-USR1-hook", &Vsignal_USR1_hook,
2061 "Hook to be run whenever emacs receives a USR1 signal");
2062 Vsignal_USR1_hook = Qnil;
2063 #ifdef SIGUSR2
2064 DEFVAR_LISP ("signal-USR2-hook", &Vsignal_USR2_hook,
2065 "Hook to be run whenever emacs receives a USR2 signal");
2066 Vsignal_USR2_hook = Qnil;
2067 #endif
2068 #endif
2071 DEFVAR_INT ("emacs-priority", &emacs_priority,
2072 "Priority for Emacs to run at.\n\
2073 This value is effective only if set before Emacs is dumped,\n\
2074 and only if the Emacs executable is installed with setuid to permit\n\
2075 it to change priority. (Emacs sets its uid back to the real uid.)\n\
2076 Currently, you need to define SET_EMACS_PRIORITY in `config.h'\n\
2077 before you compile Emacs, to enable the code for this feature.");
2078 emacs_priority = 0;
2080 DEFVAR_LISP ("path-separator", &Vpath_separator,
2081 "The directory separator in search paths, as a string.");
2083 char c = SEPCHAR;
2084 Vpath_separator = make_string (&c, 1);
2087 DEFVAR_LISP ("invocation-name", &Vinvocation_name,
2088 "The program name that was used to run Emacs.\n\
2089 Any directory names are omitted.");
2091 DEFVAR_LISP ("invocation-directory", &Vinvocation_directory,
2092 "The directory in which the Emacs executable was found, to run it.\n\
2093 The value is nil if that directory's name is not known.");
2095 DEFVAR_LISP ("installation-directory", &Vinstallation_directory,
2096 "A directory within which to look for the `lib-src' and `etc' directories.\n\
2097 This is non-nil when we can't find those directories in their standard\n\
2098 installed locations, but we can find them\n\
2099 near where the Emacs executable was found.");
2100 Vinstallation_directory = Qnil;
2102 DEFVAR_LISP ("system-messages-locale", &Vsystem_messages_locale,
2103 "System locale for messages.");
2104 Vsystem_messages_locale = Qnil;
2106 DEFVAR_LISP ("previous-system-messages-locale",
2107 &Vprevious_system_messages_locale,
2108 "Most recently used system locale for messages.");
2109 Vprevious_system_messages_locale = Qnil;
2111 DEFVAR_LISP ("system-time-locale", &Vsystem_time_locale,
2112 "System locale for time.");
2113 Vsystem_time_locale = Qnil;
2115 DEFVAR_LISP ("previous-system-time-locale", &Vprevious_system_time_locale,
2116 "Most recently used system locale for time.");
2117 Vprevious_system_time_locale = Qnil;