* files.el (directory-files-no-dot-files-regexp): Doc fix (bug#6298).
[emacs.git] / src / emacs.c
blobc0ef926da754179e4324fa254db99c5256e33d6d
1 /* Fully extensible Emacs, running on Unix, intended for GNU.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 1998, 1999,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
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>
29 #include <setjmp.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
37 #endif
39 #ifdef WINDOWSNT
40 #include <fcntl.h>
41 #include <windows.h> /* just for w32.h */
42 #include "w32.h"
43 #include "w32heap.h" /* for prototype of sbrk */
44 #endif
46 #ifdef NS_IMPL_GNUSTEP
47 /* At least under Debian, GSConfig is in a subdirectory. --Stef */
48 #include <GNUstepBase/GSConfig.h>
49 #endif
51 #include "lisp.h"
52 #include "commands.h"
53 #include "intervals.h"
54 #include "buffer.h"
55 #include "window.h"
57 #include "systty.h"
58 #include "blockinput.h"
59 #include "syssignal.h"
60 #include "process.h"
61 #include "frame.h"
62 #include "termhooks.h"
63 #include "keyboard.h"
64 #include "keymap.h"
66 #ifdef HAVE_SETLOCALE
67 #include <locale.h>
68 #endif
70 #ifdef HAVE_SETRLIMIT
71 #include <sys/time.h>
72 #include <sys/resource.h>
73 #endif
75 #ifdef HAVE_PERSONALITY_LINUX32
76 #include <sys/personality.h>
77 #endif
79 #ifndef O_RDWR
80 #define O_RDWR 2
81 #endif
83 #ifdef HAVE_SETPGID
84 #if !defined (USG)
85 #undef setpgrp
86 #define setpgrp setpgid
87 #endif
88 #endif
90 const char emacs_copyright[] = "Copyright (C) 2010 Free Software Foundation, Inc.";
91 const char emacs_version[] = "24.0.50";
93 extern void malloc_warning P_ ((char *));
94 extern void set_time_zone_rule P_ ((char *));
95 #ifdef HAVE_INDEX
96 extern char *index P_ ((const char *, int));
97 #endif
99 /* Make these values available in GDB, which doesn't see macros. */
101 #ifdef USE_LSB_TAG
102 int gdb_use_lsb = 1;
103 #else
104 int gdb_use_lsb = 0;
105 #endif
106 #ifndef USE_LISP_UNION_TYPE
107 int gdb_use_union = 0;
108 #else
109 int gdb_use_union = 1;
110 #endif
111 EMACS_INT gdb_valbits = VALBITS;
112 EMACS_INT gdb_gctypebits = GCTYPEBITS;
113 #if defined (DATA_SEG_BITS) && ! defined (USE_LSB_TAG)
114 EMACS_INT gdb_data_seg_bits = DATA_SEG_BITS;
115 #else
116 EMACS_INT gdb_data_seg_bits = 0;
117 #endif
118 EMACS_INT PVEC_FLAG = PSEUDOVECTOR_FLAG;
119 EMACS_INT gdb_array_mark_flag = ARRAY_MARK_FLAG;
120 /* GDB might say "No enum type named pvec_type" if we don't have at
121 least one symbol with that type, and then xbacktrace could fail. */
122 enum pvec_type gdb_pvec_type = PVEC_TYPE_MASK;
124 /* Command line args from shell, as list of strings. */
125 Lisp_Object Vcommand_line_args;
127 /* The name under which Emacs was invoked, with any leading directory
128 names discarded. */
129 Lisp_Object Vinvocation_name;
131 /* The directory name from which Emacs was invoked. */
132 Lisp_Object Vinvocation_directory;
134 /* The directory name in which to find subdirs such as lisp and etc.
135 nil means get them only from PATH_LOADSEARCH. */
136 Lisp_Object Vinstallation_directory;
138 /* The values of `current-time' before and after Emacs initialization. */
139 Lisp_Object Vbefore_init_time, Vafter_init_time;
141 /* Hook run by `kill-emacs' before it does really anything. */
142 Lisp_Object Vkill_emacs_hook;
144 /* Empty lisp strings. To avoid having to build any others. */
145 Lisp_Object empty_unibyte_string, empty_multibyte_string;
147 /* Search path separator. */
148 Lisp_Object Vpath_separator;
150 /* Set nonzero after Emacs has started up the first time.
151 Prevents reinitialization of the Lisp world and keymaps
152 on subsequent starts. */
153 int initialized;
155 #ifdef DOUG_LEA_MALLOC
156 /* Preserves a pointer to the memory allocated that copies that
157 static data inside glibc's malloc. */
158 void *malloc_state_ptr;
159 /* From glibc, a routine that returns a copy of the malloc internal state. */
160 extern void *malloc_get_state ();
161 /* From glibc, a routine that overwrites the malloc internal state. */
162 extern int malloc_set_state ();
163 /* Non-zero if the MALLOC_CHECK_ environment variable was set while
164 dumping. Used to work around a bug in glibc's malloc. */
165 int malloc_using_checking;
166 #endif
168 /* Variable whose value is symbol giving operating system type. */
169 Lisp_Object Vsystem_type;
171 /* Variable whose value is string giving configuration built for. */
172 Lisp_Object Vsystem_configuration;
174 /* Variable whose value is string giving configuration options,
175 for use when reporting bugs. */
176 Lisp_Object Vsystem_configuration_options;
178 Lisp_Object Qfile_name_handler_alist;
180 /* Current and previous system locales for messages and time. */
181 Lisp_Object Vsystem_messages_locale;
182 Lisp_Object Vprevious_system_messages_locale;
183 Lisp_Object Vsystem_time_locale;
184 Lisp_Object Vprevious_system_time_locale;
186 /* Copyright and version info. The version number may be updated by
187 Lisp code. */
188 Lisp_Object Vemacs_copyright, Vemacs_version;
190 /* If non-zero, emacs should not attempt to use a window-specific code,
191 but instead should use the virtual terminal under which it was started. */
192 int inhibit_window_system;
194 /* If nonzero, set Emacs to run at this priority. This is also used
195 in child_setup and sys_suspend to make sure subshells run at normal
196 priority; those functions have their own extern declaration. */
197 EMACS_INT emacs_priority;
199 /* If non-zero, a filter or a sentinel is running. Tested to save the match
200 data on the first attempt to change it inside asynchronous code. */
201 int running_asynch_code;
203 #if defined(HAVE_X_WINDOWS) || defined(HAVE_NS)
204 /* If non-zero, -d was specified, meaning we're using some window system. */
205 int display_arg;
206 #endif
208 /* An address near the bottom of the stack.
209 Tells GC how to save a copy of the stack. */
210 char *stack_bottom;
212 /* The address where the heap starts (from the first sbrk (0) call). */
213 static void *my_heap_start;
215 /* The gap between BSS end and heap start as far as we can tell. */
216 static unsigned long heap_bss_diff;
218 /* If the gap between BSS end and heap start is larger than this
219 output a warning in dump-emacs. */
220 #define MAX_HEAP_BSS_DIFF (1024*1024)
223 #ifdef HAVE_WINDOW_SYSTEM
224 extern Lisp_Object Vinitial_window_system;
225 #endif /* HAVE_WINDOW_SYSTEM */
227 extern Lisp_Object Vauto_save_list_file_name;
229 extern Lisp_Object Vinhibit_redisplay;
231 /* Nonzero means running Emacs without interactive terminal. */
233 int noninteractive;
235 /* Value of Lisp variable `noninteractive'.
236 Normally same as C variable `noninteractive'
237 but nothing terrible happens if user sets this one. */
239 int noninteractive1;
241 /* Nonzero means Emacs was run in --quick mode. */
242 int inhibit_x_resources;
244 /* Name for the server started by the daemon.*/
245 static char *daemon_name;
247 /* Pipe used to send exit notification to the daemon parent at
248 startup. */
249 int daemon_pipe[2];
251 /* Save argv and argc. */
252 char **initial_argv;
253 int initial_argc;
255 static void sort_args ();
256 void syms_of_emacs ();
258 /* MSVC needs each string be shorter than 2048 bytes, so the usage
259 strings below are split to not overflow this limit. */
260 #define USAGE1 "\
261 Usage: %s [OPTION-OR-FILENAME]...\n\
263 Run Emacs, the extensible, customizable, self-documenting real-time\n\
264 display editor. The recommended way to start Emacs for normal editing\n\
265 is with no options at all.\n\
267 Run M-x info RET m emacs RET m emacs invocation RET inside Emacs to\n\
268 read the main documentation for these command-line arguments.\n\
270 Initialization options:\n\
272 --batch do not do interactive display; implies -q\n\
273 --chdir DIR change to directory DIR\n\
274 --daemon start a server in the background\n\
275 --debug-init enable Emacs Lisp debugger for init file\n\
276 --display, -d DISPLAY use X server DISPLAY\n\
277 --no-desktop do not load a saved desktop\n\
278 --no-init-file, -q load neither ~/.emacs nor default.el\n\
279 --no-shared-memory, -nl do not use shared memory\n\
280 --no-site-file do not load site-start.el\n\
281 --no-splash do not display a splash screen on startup\n\
282 --no-window-system, -nw do not communicate with X, ignoring $DISPLAY\n\
283 --quick, -Q equivalent to -q --no-site-file --no-splash\n\
284 --script FILE run FILE as an Emacs Lisp script\n\
285 --terminal, -t DEVICE use DEVICE for terminal I/O\n\
286 --user, -u USER load ~USER/.emacs instead of your own\n\
287 \n%s"
289 #define USAGE2 "\
290 Action options:\n\
292 FILE visit FILE using find-file\n\
293 +LINE go to line LINE in next FILE\n\
294 +LINE:COLUMN go to line LINE, column COLUMN, in next FILE\n\
295 --directory, -L DIR add DIR to variable load-path\n\
296 --eval EXPR evaluate Emacs Lisp expression EXPR\n\
297 --execute EXPR evaluate Emacs Lisp expression EXPR\n\
298 --file FILE visit FILE using find-file\n\
299 --find-file FILE visit FILE using find-file\n\
300 --funcall, -f FUNC call Emacs Lisp function FUNC with no arguments\n\
301 --insert FILE insert contents of FILE into current buffer\n\
302 --kill exit without asking for confirmation\n\
303 --load, -l FILE load Emacs Lisp FILE using the load function\n\
304 --visit FILE visit FILE using find-file\n\
307 #define USAGE3 "\
308 Display options:\n\
310 --background-color, -bg COLOR window background color\n\
311 --basic-display, -D disable many display features;\n\
312 used for debugging Emacs\n\
313 --border-color, -bd COLOR main border color\n\
314 --border-width, -bw WIDTH width of main border\n\
315 --color, --color=MODE override color mode for character terminals;\n\
316 MODE defaults to `auto', and can also\n\
317 be `never', `auto', `always',\n\
318 or a mode name like `ansi8'\n\
319 --cursor-color, -cr COLOR color of the Emacs cursor indicating point\n\
320 --font, -fn FONT default font; must be fixed-width\n\
321 --foreground-color, -fg COLOR window foreground color\n\
322 --fullheight, -fh make the first frame high as the screen\n\
323 --fullscreen, -fs make first frame fullscreen\n\
324 --fullwidth, -fw make the first frame wide as the screen\n\
325 --maximized, -mm make the first frame maximized\n\
326 --geometry, -g GEOMETRY window geometry\n\
327 --no-bitmap-icon, -nbi do not use picture of gnu for Emacs icon\n\
328 --iconic start Emacs in iconified state\n\
329 --internal-border, -ib WIDTH width between text and main border\n\
330 --line-spacing, -lsp PIXELS additional space to put between lines\n\
331 --mouse-color, -ms COLOR mouse cursor color in Emacs window\n\
332 --name NAME title for initial Emacs frame\n\
333 --no-blinking-cursor, -nbc disable blinking cursor\n\
334 --reverse-video, -r, -rv switch foreground and background\n\
335 --title, -T TITLE title for initial Emacs frame\n\
336 --vertical-scroll-bars, -vb enable vertical scroll bars\n\
337 --xrm XRESOURCES set additional X resources\n\
338 --parent-id XID set parent window\n\
339 --help display this help and exit\n\
340 --version output version information and exit\n\
343 #define USAGE4 "\
344 You can generally also specify long option names with a single -; for\n\
345 example, -batch as well as --batch. You can use any unambiguous\n\
346 abbreviation for a --option.\n\
348 Various environment variables and window system resources also affect\n\
349 Emacs' operation. See the main documentation.\n\
351 Report bugs to bug-gnu-emacs@gnu.org. First, please see the Bugs\n\
352 section of the Emacs manual or the file BUGS.\n"
355 /* Signal code for the fatal signal that was received. */
356 int fatal_error_code;
358 /* Nonzero if handling a fatal error already. */
359 int fatal_error_in_progress;
361 /* If non-null, call this function from fatal_error_signal before
362 committing suicide. */
364 void (*fatal_error_signal_hook) P_ ((void));
366 #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD
367 /* When compiled with GTK and running under Gnome,
368 multiple threads may be created. Keep track of our main
369 thread to make sure signals are delivered to it (see syssignal.h). */
371 pthread_t main_thread;
372 #endif
375 /* Handle bus errors, invalid instruction, etc. */
376 SIGTYPE
377 fatal_error_signal (sig)
378 int sig;
380 SIGNAL_THREAD_CHECK (sig);
381 fatal_error_code = sig;
382 signal (sig, SIG_DFL);
384 TOTALLY_UNBLOCK_INPUT;
386 /* If fatal error occurs in code below, avoid infinite recursion. */
387 if (! fatal_error_in_progress)
389 fatal_error_in_progress = 1;
391 if (sig == SIGTERM || sig == SIGHUP)
392 Fkill_emacs (make_number (sig));
394 shut_down_emacs (sig, 0, Qnil);
397 /* Signal the same code; this time it will really be fatal.
398 Remember that since we're in a signal handler, the signal we're
399 going to send is probably blocked, so we have to unblock it if we
400 want to really receive it. */
401 #ifndef MSDOS
402 sigunblock (sigmask (fatal_error_code));
403 #endif
405 if (fatal_error_signal_hook)
406 fatal_error_signal_hook ();
408 kill (getpid (), fatal_error_code);
411 #ifdef SIGDANGER
413 /* Handler for SIGDANGER. */
414 SIGTYPE
415 memory_warning_signal (sig)
416 int sig;
418 signal (sig, memory_warning_signal);
419 SIGNAL_THREAD_CHECK (sig);
421 malloc_warning ("Operating system warns that virtual memory is running low.\n");
423 /* It might be unsafe to call do_auto_save now. */
424 force_auto_save_soon ();
426 #endif
428 /* We define abort, rather than using it from the library,
429 so that GDB can return from a breakpoint here.
430 MSDOS has its own definition in msdos.c. */
432 #if ! defined (DOS_NT) && ! defined (NO_ABORT)
434 void
435 abort ()
437 kill (getpid (), SIGABRT);
438 /* This shouldn't be executed, but it prevents a warning. */
439 exit (1);
441 #endif
444 /* Code for dealing with Lisp access to the Unix command line. */
446 static void
447 init_cmdargs (argc, argv, skip_args)
448 int argc;
449 char **argv;
450 int skip_args;
452 register int i;
453 Lisp_Object name, dir, tem;
454 int count = SPECPDL_INDEX ();
455 Lisp_Object raw_name;
457 initial_argv = argv;
458 initial_argc = argc;
460 raw_name = build_string (argv[0]);
462 /* Add /: to the front of the name
463 if it would otherwise be treated as magic. */
464 tem = Ffind_file_name_handler (raw_name, Qt);
465 if (! NILP (tem))
466 raw_name = concat2 (build_string ("/:"), raw_name);
468 Vinvocation_name = Ffile_name_nondirectory (raw_name);
469 Vinvocation_directory = Ffile_name_directory (raw_name);
471 /* If we got no directory in argv[0], search PATH to find where
472 Emacs actually came from. */
473 if (NILP (Vinvocation_directory))
475 Lisp_Object found;
476 int yes = openp (Vexec_path, Vinvocation_name,
477 Vexec_suffixes, &found, make_number (X_OK));
478 if (yes == 1)
480 /* Add /: to the front of the name
481 if it would otherwise be treated as magic. */
482 tem = Ffind_file_name_handler (found, Qt);
483 if (! NILP (tem))
484 found = concat2 (build_string ("/:"), found);
485 Vinvocation_directory = Ffile_name_directory (found);
489 if (!NILP (Vinvocation_directory)
490 && NILP (Ffile_name_absolute_p (Vinvocation_directory)))
491 /* Emacs was started with relative path, like ./emacs.
492 Make it absolute. */
493 Vinvocation_directory = Fexpand_file_name (Vinvocation_directory, Qnil);
495 Vinstallation_directory = Qnil;
497 if (!NILP (Vinvocation_directory))
499 dir = Vinvocation_directory;
500 name = Fexpand_file_name (Vinvocation_name, dir);
501 while (1)
503 Lisp_Object tem, lib_src_exists;
504 Lisp_Object etc_exists, info_exists;
506 /* See if dir contains subdirs for use by Emacs.
507 Check for the ones that would exist in a build directory,
508 not including lisp and info. */
509 tem = Fexpand_file_name (build_string ("lib-src"), dir);
510 lib_src_exists = Ffile_exists_p (tem);
512 #ifdef MSDOS
513 /* MSDOS installations frequently remove lib-src, but we still
514 must set installation-directory, or else info won't find
515 its files (it uses the value of installation-directory). */
516 tem = Fexpand_file_name (build_string ("info"), dir);
517 info_exists = Ffile_exists_p (tem);
518 #else
519 info_exists = Qnil;
520 #endif
522 if (!NILP (lib_src_exists) || !NILP (info_exists))
524 tem = Fexpand_file_name (build_string ("etc"), dir);
525 etc_exists = Ffile_exists_p (tem);
526 if (!NILP (etc_exists))
528 Vinstallation_directory
529 = Ffile_name_as_directory (dir);
530 break;
534 /* See if dir's parent contains those subdirs. */
535 tem = Fexpand_file_name (build_string ("../lib-src"), dir);
536 lib_src_exists = Ffile_exists_p (tem);
539 #ifdef MSDOS
540 /* See the MSDOS commentary above. */
541 tem = Fexpand_file_name (build_string ("../info"), dir);
542 info_exists = Ffile_exists_p (tem);
543 #else
544 info_exists = Qnil;
545 #endif
547 if (!NILP (lib_src_exists) || !NILP (info_exists))
549 tem = Fexpand_file_name (build_string ("../etc"), dir);
550 etc_exists = Ffile_exists_p (tem);
551 if (!NILP (etc_exists))
553 tem = Fexpand_file_name (build_string (".."), dir);
554 Vinstallation_directory
555 = Ffile_name_as_directory (tem);
556 break;
560 /* If the Emacs executable is actually a link,
561 next try the dir that the link points into. */
562 tem = Ffile_symlink_p (name);
563 if (!NILP (tem))
565 name = Fexpand_file_name (tem, dir);
566 dir = Ffile_name_directory (name);
568 else
569 break;
573 Vcommand_line_args = Qnil;
575 for (i = argc - 1; i >= 0; i--)
577 if (i == 0 || i > skip_args)
578 /* For the moment, we keep arguments as is in unibyte strings.
579 They are decoded in the function command-line after we know
580 locale-coding-system. */
581 Vcommand_line_args
582 = Fcons (make_unibyte_string (argv[i], strlen (argv[i])),
583 Vcommand_line_args);
586 unbind_to (count, Qnil);
589 DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
590 doc: /* Return the program name that was used to run Emacs.
591 Any directory names are omitted. */)
594 return Fcopy_sequence (Vinvocation_name);
597 DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
598 0, 0, 0,
599 doc: /* Return the directory name in which the Emacs executable was located. */)
602 return Fcopy_sequence (Vinvocation_directory);
606 #ifdef HAVE_TZSET
607 /* A valid but unlikely value for the TZ environment value.
608 It is OK (though a bit slower) if the user actually chooses this value. */
609 static char dump_tz[] = "UtC0";
610 #endif
612 #ifndef ORDINARY_LINK
613 /* We don't include crtbegin.o and crtend.o in the link,
614 so these functions and variables might be missed.
615 Provide dummy definitions to avoid error.
616 (We don't have any real constructors or destructors.) */
617 #ifdef __GNUC__
618 #ifndef GCC_CTORS_IN_LIBC
619 void __do_global_ctors ()
621 void __do_global_ctors_aux ()
623 void __do_global_dtors ()
625 /* GNU/Linux has a bug in its library; avoid an error. */
626 #ifndef GNU_LINUX
627 char * __CTOR_LIST__[2] = { (char *) (-1), 0 };
628 #endif
629 char * __DTOR_LIST__[2] = { (char *) (-1), 0 };
630 #endif /* GCC_CTORS_IN_LIBC */
631 void __main ()
633 #endif /* __GNUC__ */
634 #endif /* ORDINARY_LINK */
636 /* Test whether the next argument in ARGV matches SSTR or a prefix of
637 LSTR (at least MINLEN characters). If so, then if VALPTR is non-null
638 (the argument is supposed to have a value) store in *VALPTR either
639 the next argument or the portion of this one after the equal sign.
640 ARGV is read starting at position *SKIPPTR; this index is advanced
641 by the number of arguments used.
643 Too bad we can't just use getopt for all of this, but we don't have
644 enough information to do it right. */
646 static int
647 argmatch (argv, argc, sstr, lstr, minlen, valptr, skipptr)
648 char **argv;
649 int argc;
650 char *sstr;
651 char *lstr;
652 int minlen;
653 char **valptr;
654 int *skipptr;
656 char *p = NULL;
657 int arglen;
658 char *arg;
660 /* Don't access argv[argc]; give up in advance. */
661 if (argc <= *skipptr + 1)
662 return 0;
664 arg = argv[*skipptr+1];
665 if (arg == NULL)
666 return 0;
667 if (strcmp (arg, sstr) == 0)
669 if (valptr != NULL)
671 *valptr = argv[*skipptr+2];
672 *skipptr += 2;
674 else
675 *skipptr += 1;
676 return 1;
678 arglen = (valptr != NULL && (p = index (arg, '=')) != NULL
679 ? p - arg : strlen (arg));
680 if (lstr == 0 || arglen < minlen || strncmp (arg, lstr, arglen) != 0)
681 return 0;
682 else if (valptr == NULL)
684 *skipptr += 1;
685 return 1;
687 else if (p != NULL)
689 *valptr = p+1;
690 *skipptr += 1;
691 return 1;
693 else if (argv[*skipptr+2] != NULL)
695 *valptr = argv[*skipptr+2];
696 *skipptr += 2;
697 return 1;
699 else
701 return 0;
705 #ifdef DOUG_LEA_MALLOC
707 /* malloc can be invoked even before main (e.g. by the dynamic
708 linker), so the dumped malloc state must be restored as early as
709 possible using this special hook. */
711 static void
712 malloc_initialize_hook ()
714 #ifndef USE_CRT_DLL
715 extern char **environ;
716 #endif
718 if (initialized)
720 if (!malloc_using_checking)
721 /* Work around a bug in glibc's malloc. MALLOC_CHECK_ must be
722 ignored if the heap to be restored was constructed without
723 malloc checking. Can't use unsetenv, since that calls malloc. */
725 char **p;
727 for (p = environ; p && *p; p++)
728 if (strncmp (*p, "MALLOC_CHECK_=", 14) == 0)
731 *p = p[1];
732 while (*++p);
733 break;
737 malloc_set_state (malloc_state_ptr);
738 #ifndef XMALLOC_OVERRUN_CHECK
739 free (malloc_state_ptr);
740 #endif
742 else
744 if (my_heap_start == 0)
745 my_heap_start = sbrk (0);
746 malloc_using_checking = getenv ("MALLOC_CHECK_") != NULL;
750 void (*__malloc_initialize_hook) () = malloc_initialize_hook;
752 #endif /* DOUG_LEA_MALLOC */
755 /* ARGSUSED */
757 main (int argc, char **argv)
759 #if GC_MARK_STACK
760 Lisp_Object dummy;
761 #endif
762 char stack_bottom_variable;
763 int do_initial_setlocale;
764 int skip_args = 0;
765 #ifdef HAVE_SETRLIMIT
766 struct rlimit rlim;
767 #endif
768 int no_loadup = 0;
769 char *junk = 0;
770 char *dname_arg = 0;
771 #ifdef NS_IMPL_COCOA
772 char dname_arg2[80];
773 #endif
774 char *ch_to_dir;
776 #if GC_MARK_STACK
777 extern Lisp_Object *stack_base;
778 stack_base = &dummy;
779 #endif
781 #if defined (USE_GTK) && defined (G_SLICE_ALWAYS_MALLOC)
782 /* This is used by the Cygwin build. */
783 setenv ("G_SLICE", "always-malloc", 1);
784 #endif
786 if (!initialized)
788 extern char my_endbss[];
789 extern char *my_endbss_static;
791 if (my_heap_start == 0)
792 my_heap_start = sbrk (0);
794 heap_bss_diff = (char *)my_heap_start - max (my_endbss, my_endbss_static);
797 #ifdef RUN_TIME_REMAP
798 if (initialized)
799 run_time_remap (argv[0]);
800 #endif
802 /* If using unexmacosx.c (set by s/darwin.h), we must do this. */
803 #ifdef DARWIN_OS
804 if (!initialized)
805 unexec_init_emacs_zone ();
806 #endif
808 sort_args (argc, argv);
809 argc = 0;
810 while (argv[argc]) argc++;
812 if (argmatch (argv, argc, "-version", "--version", 3, NULL, &skip_args))
814 const char *version, *copyright;
815 if (initialized)
817 Lisp_Object tem, tem2;
818 tem = Fsymbol_value (intern_c_string ("emacs-version"));
819 tem2 = Fsymbol_value (intern_c_string ("emacs-copyright"));
820 if (!STRINGP (tem))
822 fprintf (stderr, "Invalid value of `emacs-version'\n");
823 exit (1);
825 if (!STRINGP (tem2))
827 fprintf (stderr, "Invalid value of `emacs-copyright'\n");
828 exit (1);
830 else
832 version = SDATA (tem);
833 copyright = SDATA (tem2);
836 else
838 version = emacs_version;
839 copyright = emacs_copyright;
841 printf ("GNU Emacs %s\n", version);
842 printf ("%s\n", copyright);
843 printf ("GNU Emacs comes with ABSOLUTELY NO WARRANTY.\n");
844 printf ("You may redistribute copies of Emacs\n");
845 printf ("under the terms of the GNU General Public License.\n");
846 printf ("For more information about these matters, ");
847 printf ("see the file named COPYING.\n");
848 exit (0);
850 if (argmatch (argv, argc, "-chdir", "--chdir", 2, &ch_to_dir, &skip_args))
851 if (chdir (ch_to_dir) == -1)
853 fprintf (stderr, "%s: Can't chdir to %s: %s\n",
854 argv[0], ch_to_dir, strerror (errno));
855 exit (1);
859 #ifdef HAVE_PERSONALITY_LINUX32
860 if (!initialized
861 && (strcmp (argv[argc-1], "dump") == 0
862 || strcmp (argv[argc-1], "bootstrap") == 0)
863 && ! getenv ("EMACS_HEAP_EXEC"))
865 /* Set this so we only do this once. */
866 putenv("EMACS_HEAP_EXEC=true");
868 /* A flag to turn off address randomization which is introduced
869 in linux kernel shipped with fedora core 4 */
870 #define ADD_NO_RANDOMIZE 0x0040000
871 personality (PER_LINUX32 | ADD_NO_RANDOMIZE);
872 #undef ADD_NO_RANDOMIZE
874 execvp (argv[0], argv);
876 /* If the exec fails, try to dump anyway. */
877 perror ("execvp");
879 #endif /* HAVE_PERSONALITY_LINUX32 */
882 /* Map in shared memory, if we are using that. */
883 #ifdef HAVE_SHM
884 if (argmatch (argv, argc, "-nl", "--no-shared-memory", 6, NULL, &skip_args))
886 map_in_data (0);
887 /* The shared memory was just restored, which clobbered this. */
888 skip_args = 1;
890 else
892 map_in_data (1);
893 /* The shared memory was just restored, which clobbered this. */
894 skip_args = 0;
896 #endif
898 #if defined (HAVE_SETRLIMIT) && defined (RLIMIT_STACK)
899 /* Extend the stack space available.
900 Don't do that if dumping, since some systems (e.g. DJGPP)
901 might define a smaller stack limit at that time. */
902 if (1
903 #ifndef CANNOT_DUMP
904 && (!noninteractive || initialized)
905 #endif
906 && !getrlimit (RLIMIT_STACK, &rlim))
908 long newlim;
909 extern size_t re_max_failures;
910 /* Approximate the amount regex.c needs per unit of re_max_failures. */
911 int ratio = 20 * sizeof (char *);
912 /* Then add 33% to cover the size of the smaller stacks that regex.c
913 successively allocates and discards, on its way to the maximum. */
914 ratio += ratio / 3;
915 /* Add in some extra to cover
916 what we're likely to use for other reasons. */
917 newlim = re_max_failures * ratio + 200000;
918 #ifdef __NetBSD__
919 /* NetBSD (at least NetBSD 1.2G and former) has a bug in its
920 stack allocation routine for new process that the allocation
921 fails if stack limit is not on page boundary. So, round up the
922 new limit to page boundary. */
923 newlim = (newlim + getpagesize () - 1) / getpagesize () * getpagesize();
924 #endif
925 if (newlim > rlim.rlim_max)
927 newlim = rlim.rlim_max;
928 /* Don't let regex.c overflow the stack we have. */
929 re_max_failures = (newlim - 200000) / ratio;
931 if (rlim.rlim_cur < newlim)
932 rlim.rlim_cur = newlim;
934 setrlimit (RLIMIT_STACK, &rlim);
936 #endif /* HAVE_SETRLIMIT and RLIMIT_STACK */
938 /* Record (approximately) where the stack begins. */
939 stack_bottom = &stack_bottom_variable;
941 clearerr (stdin);
943 #ifndef SYSTEM_MALLOC
944 /* Arrange to get warning messages as memory fills up. */
945 memory_warnings (0, malloc_warning);
947 /* Call malloc at least once, to run the initial __malloc_hook.
948 Also call realloc and free for consistency. */
949 free (realloc (malloc (4), 4));
951 # ifndef SYNC_INPUT
952 /* Arrange to disable interrupt input inside malloc etc. */
953 uninterrupt_malloc ();
954 # endif /* not SYNC_INPUT */
955 #endif /* not SYSTEM_MALLOC */
957 #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD
958 main_thread = pthread_self ();
959 #endif /* FORWARD_SIGNAL_TO_MAIN_THREAD */
961 #if defined (MSDOS) || defined (WINDOWSNT)
962 /* We do all file input/output as binary files. When we need to translate
963 newlines, we do that manually. */
964 _fmode = O_BINARY;
965 #endif /* MSDOS || WINDOWSNT */
967 #ifdef MSDOS
968 if (!isatty (fileno (stdin)))
969 setmode (fileno (stdin), O_BINARY);
970 if (!isatty (fileno (stdout)))
972 fflush (stdout);
973 setmode (fileno (stdout), O_BINARY);
975 #endif /* MSDOS */
977 #ifdef SET_EMACS_PRIORITY
978 if (emacs_priority)
979 nice (emacs_priority);
980 setuid (getuid ());
981 #endif /* SET_EMACS_PRIORITY */
983 /* Skip initial setlocale if LC_ALL is "C", as it's not needed in that case.
984 The build procedure uses this while dumping, to ensure that the
985 dumped Emacs does not have its system locale tables initialized,
986 as that might cause screwups when the dumped Emacs starts up. */
988 char *lc_all = getenv ("LC_ALL");
989 do_initial_setlocale = ! lc_all || strcmp (lc_all, "C");
992 /* Set locale now, so that initial error messages are localized properly.
993 fixup_locale must wait until later, since it builds strings. */
994 if (do_initial_setlocale)
995 setlocale (LC_ALL, "");
997 inhibit_window_system = 0;
999 /* Handle the -t switch, which specifies filename to use as terminal. */
1000 while (1)
1002 char *term;
1003 if (argmatch (argv, argc, "-t", "--terminal", 4, &term, &skip_args))
1005 int result;
1006 emacs_close (0);
1007 emacs_close (1);
1008 result = emacs_open (term, O_RDWR, 0);
1009 if (result < 0)
1011 char *errstring = strerror (errno);
1012 fprintf (stderr, "%s: %s: %s\n", argv[0], term, errstring);
1013 exit (1);
1015 dup (0);
1016 if (! isatty (0))
1018 fprintf (stderr, "%s: %s: not a tty\n", argv[0], term);
1019 exit (1);
1021 fprintf (stderr, "Using %s\n", term);
1022 #ifdef HAVE_WINDOW_SYSTEM
1023 inhibit_window_system = 1; /* -t => -nw */
1024 #endif
1026 else
1027 break;
1030 /* Command line option --no-windows is deprecated and thus not mentioned
1031 in the manual and usage informations. */
1032 if (argmatch (argv, argc, "-nw", "--no-window-system", 6, NULL, &skip_args)
1033 || argmatch (argv, argc, "-nw", "--no-windows", 6, NULL, &skip_args))
1034 inhibit_window_system = 1;
1036 /* Handle the -batch switch, which means don't do interactive display. */
1037 noninteractive = 0;
1038 if (argmatch (argv, argc, "-batch", "--batch", 5, NULL, &skip_args))
1040 noninteractive = 1;
1041 Vundo_outer_limit = Qnil;
1043 if (argmatch (argv, argc, "-script", "--script", 3, &junk, &skip_args))
1045 noninteractive = 1; /* Set batch mode. */
1046 /* Convert --script to -scriptload, un-skip it, and sort again
1047 so that it will be handled in proper sequence. */
1048 /* FIXME broken for --script=FILE - is that supposed to work? */
1049 argv[skip_args - 1] = "-scriptload";
1050 skip_args -= 2;
1051 sort_args (argc, argv);
1054 /* Handle the --help option, which gives a usage message. */
1055 if (argmatch (argv, argc, "-help", "--help", 3, NULL, &skip_args))
1057 printf (USAGE1, argv[0], USAGE2);
1058 printf (USAGE3);
1059 printf (USAGE4);
1060 exit (0);
1063 if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)
1064 || argmatch (argv, argc, "-daemon", "--daemon", 5, &dname_arg, &skip_args))
1066 #ifndef DOS_NT
1067 pid_t f;
1069 /* Start as a daemon: fork a new child process which will run the
1070 rest of the initialization code, then exit.
1072 Detaching a daemon requires the following steps:
1073 - fork
1074 - setsid
1075 - exit the parent
1076 - close the tty file-descriptors
1078 We only want to do the last 2 steps once the daemon is ready to
1079 serve requests, i.e. after loading .emacs (initialization).
1080 OTOH initialization may start subprocesses (e.g. ispell) and these
1081 should be run from the proper process (the one that will end up
1082 running as daemon) and with the proper "session id" in order for
1083 them to keep working after detaching, so fork and setsid need to be
1084 performed before initialization.
1086 We want to avoid exiting before the server socket is ready, so
1087 use a pipe for synchronization. The parent waits for the child
1088 to close its end of the pipe (using `daemon-initialized')
1089 before exiting. */
1090 if (pipe (daemon_pipe) == -1)
1092 fprintf (stderr, "Cannot pipe!\n");
1093 exit (1);
1096 #ifndef NS_IMPL_COCOA
1097 f = fork ();
1098 #else /* NS_IMPL_COCOA */
1099 /* Under Cocoa we must do fork+exec as CoreFoundation lib fails in
1100 forked process: http://developer.apple.com/ReleaseNotes/
1101 CoreFoundation/CoreFoundation.html)
1102 We mark being in the exec'd process by a daemon name argument of
1103 form "--daemon=\nFD0,FD1\nNAME" where FD are the pipe file descriptors,
1104 NAME is the original daemon name, if any. */
1105 if (!dname_arg || !strchr (dname_arg, '\n'))
1106 f = fork (); /* in orig */
1107 else
1108 f = 0; /* in exec'd */
1109 #endif /* NS_IMPL_COCOA */
1110 if (f > 0)
1112 int retval;
1113 char buf[1];
1115 /* Close unused writing end of the pipe. */
1116 close (daemon_pipe[1]);
1118 /* Just wait for the child to close its end of the pipe. */
1121 retval = read (daemon_pipe[0], &buf, 1);
1123 while (retval == -1 && errno == EINTR);
1125 if (retval < 0)
1127 fprintf (stderr, "Error reading status from child\n");
1128 exit (1);
1130 else if (retval == 0)
1132 fprintf (stderr, "Error: server did not start correctly\n");
1133 exit (1);
1136 close (daemon_pipe[0]);
1137 exit (0);
1139 if (f < 0)
1141 fprintf (stderr, "Cannot fork!\n");
1142 exit (1);
1145 #ifdef NS_IMPL_COCOA
1147 /* In orig process, forked as child, OR in exec'd. */
1148 if (!dname_arg || !strchr (dname_arg, '\n'))
1149 { /* In orig, child: now exec w/special daemon name. */
1150 char fdStr[80];
1152 if (dname_arg && strlen (dname_arg) > 70)
1154 fprintf (stderr, "daemon: child name too long\n");
1155 exit (1);
1158 sprintf (fdStr, "--daemon=\n%d,%d\n%s", daemon_pipe[0],
1159 daemon_pipe[1], dname_arg ? dname_arg : "");
1160 argv[skip_args] = fdStr;
1162 execv (argv[0], argv);
1163 fprintf (stderr, "emacs daemon: exec failed: %d\n", errno);
1164 exit (1);
1167 /* In exec'd: parse special dname into pipe and name info. */
1168 if (!dname_arg || !strchr (dname_arg, '\n')
1169 || strlen (dname_arg) < 1 || strlen (dname_arg) > 70)
1171 fprintf (stderr, "emacs daemon: daemon name absent or too long\n");
1172 exit(1);
1174 dname_arg2[0] = '\0';
1175 sscanf (dname_arg, "\n%d,%d\n%s", &(daemon_pipe[0]), &(daemon_pipe[1]),
1176 dname_arg2);
1177 dname_arg = strlen (dname_arg2) ? dname_arg2 : NULL;
1179 #endif /* NS_IMPL_COCOA */
1181 if (dname_arg)
1182 daemon_name = xstrdup (dname_arg);
1183 /* Close unused reading end of the pipe. */
1184 close (daemon_pipe[0]);
1185 /* Make sure that the used end of the pipe is closed on exec, so
1186 that it is not accessible to programs started from .emacs. */
1187 fcntl (daemon_pipe[1], F_SETFD, FD_CLOEXEC);
1189 #ifdef HAVE_SETSID
1190 setsid();
1191 #endif
1192 #else /* DOS_NT */
1193 fprintf (stderr, "This platform does not support the -daemon flag.\n");
1194 exit (1);
1195 #endif /* DOS_NT */
1198 if (! noninteractive)
1200 #if defined (USG5) && defined (INTERRUPT_INPUT)
1201 setpgrp ();
1202 #endif
1203 #if defined (HAVE_GTK_AND_PTHREAD) && !defined (SYSTEM_MALLOC) && !defined (DOUG_LEA_MALLOC)
1205 extern void malloc_enable_thread P_ ((void));
1207 malloc_enable_thread ();
1209 #endif
1212 init_signals ();
1214 /* Don't catch SIGHUP if dumping. */
1215 if (1
1216 #ifndef CANNOT_DUMP
1217 && initialized
1218 #endif
1221 sigblock (sigmask (SIGHUP));
1222 /* In --batch mode, don't catch SIGHUP if already ignored.
1223 That makes nohup work. */
1224 if (! noninteractive
1225 || signal (SIGHUP, SIG_IGN) != SIG_IGN)
1226 signal (SIGHUP, fatal_error_signal);
1227 sigunblock (sigmask (SIGHUP));
1230 if (
1231 #ifndef CANNOT_DUMP
1232 ! noninteractive || initialized
1233 #else
1235 #endif
1238 /* Don't catch these signals in batch mode if dumping.
1239 On some machines, this sets static data that would make
1240 signal fail to work right when the dumped Emacs is run. */
1241 signal (SIGQUIT, fatal_error_signal);
1242 signal (SIGILL, fatal_error_signal);
1243 signal (SIGTRAP, fatal_error_signal);
1244 #ifdef SIGUSR1
1245 add_user_signal (SIGUSR1, "sigusr1");
1246 #endif
1247 #ifdef SIGUSR2
1248 add_user_signal (SIGUSR2, "sigusr2");
1249 #endif
1250 #ifdef SIGABRT
1251 signal (SIGABRT, fatal_error_signal);
1252 #endif
1253 #ifdef SIGHWE
1254 signal (SIGHWE, fatal_error_signal);
1255 #endif
1256 #ifdef SIGPRE
1257 signal (SIGPRE, fatal_error_signal);
1258 #endif
1259 #ifdef SIGORE
1260 signal (SIGORE, fatal_error_signal);
1261 #endif
1262 #ifdef SIGUME
1263 signal (SIGUME, fatal_error_signal);
1264 #endif
1265 #ifdef SIGDLK
1266 signal (SIGDLK, fatal_error_signal);
1267 #endif
1268 #ifdef SIGCPULIM
1269 signal (SIGCPULIM, fatal_error_signal);
1270 #endif
1271 #ifdef SIGIOT
1272 /* This is missing on some systems - OS/2, for example. */
1273 signal (SIGIOT, fatal_error_signal);
1274 #endif
1275 #ifdef SIGEMT
1276 signal (SIGEMT, fatal_error_signal);
1277 #endif
1278 signal (SIGFPE, fatal_error_signal);
1279 #ifdef SIGBUS
1280 signal (SIGBUS, fatal_error_signal);
1281 #endif
1282 signal (SIGSEGV, fatal_error_signal);
1283 #ifdef SIGSYS
1284 signal (SIGSYS, fatal_error_signal);
1285 #endif
1286 signal (SIGTERM, fatal_error_signal);
1287 #ifdef SIGXCPU
1288 signal (SIGXCPU, fatal_error_signal);
1289 #endif
1290 #ifdef SIGXFSZ
1291 signal (SIGXFSZ, fatal_error_signal);
1292 #endif /* SIGXFSZ */
1294 #ifdef SIGDANGER
1295 /* This just means available memory is getting low. */
1296 signal (SIGDANGER, memory_warning_signal);
1297 #endif
1299 #ifdef AIX
1300 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
1301 signal (SIGXCPU, fatal_error_signal);
1302 #ifndef _I386
1303 signal (SIGIOINT, fatal_error_signal);
1304 #endif
1305 signal (SIGGRANT, fatal_error_signal);
1306 signal (SIGRETRACT, fatal_error_signal);
1307 signal (SIGSOUND, fatal_error_signal);
1308 signal (SIGMSG, fatal_error_signal);
1309 #endif /* AIX */
1312 noninteractive1 = noninteractive;
1314 /* Perform basic initializations (not merely interning symbols). */
1316 if (!initialized)
1318 init_alloc_once ();
1319 init_obarray ();
1320 init_eval_once ();
1321 init_character_once ();
1322 init_charset_once ();
1323 init_coding_once ();
1324 init_syntax_once (); /* Create standard syntax table. */
1325 init_category_once (); /* Create standard category table. */
1326 /* Must be done before init_buffer. */
1327 init_casetab_once ();
1328 init_buffer_once (); /* Create buffer table and some buffers. */
1329 init_minibuf_once (); /* Create list of minibuffers. */
1330 /* Must precede init_window_once. */
1332 /* Call syms_of_xfaces before init_window_once because that
1333 function creates Vterminal_frame. Termcap frames now use
1334 faces, and the face implementation uses some symbols as
1335 face names. */
1336 syms_of_xfaces ();
1337 /* XXX syms_of_keyboard uses some symbols in keymap.c. It would
1338 be better to arrange things not to have this dependency. */
1339 syms_of_keymap ();
1340 /* Call syms_of_keyboard before init_window_once because
1341 keyboard sets up symbols that include some face names that
1342 the X support will want to use. This can happen when
1343 CANNOT_DUMP is defined. */
1344 syms_of_keyboard ();
1346 /* Called before syms_of_fileio, because it sets up Qerror_condition. */
1347 syms_of_data ();
1348 syms_of_fileio ();
1349 /* Before syms_of_coding to initialize Vgc_cons_threshold. */
1350 syms_of_alloc ();
1351 /* Before syms_of_coding because it initializes Qcharsetp. */
1352 syms_of_charset ();
1353 /* Before init_window_once, because it sets up the
1354 Vcoding_system_hash_table. */
1355 syms_of_coding (); /* This should be after syms_of_fileio. */
1357 init_window_once (); /* Init the window system. */
1358 #ifdef HAVE_WINDOW_SYSTEM
1359 init_fringe_once (); /* Swap bitmaps if necessary. */
1360 #endif /* HAVE_WINDOW_SYSTEM */
1363 init_alloc ();
1365 if (do_initial_setlocale)
1367 fixup_locale ();
1368 Vsystem_messages_locale = Vprevious_system_messages_locale;
1369 Vsystem_time_locale = Vprevious_system_time_locale;
1372 init_eval ();
1373 init_data ();
1374 #ifdef CLASH_DETECTION
1375 init_filelock ();
1376 #endif
1377 init_atimer ();
1378 running_asynch_code = 0;
1380 /* Handle --unibyte and the EMACS_UNIBYTE envvar,
1381 but not while dumping. */
1382 if (1)
1384 int inhibit_unibyte = 0;
1386 /* --multibyte overrides EMACS_UNIBYTE. */
1387 if (argmatch (argv, argc, "-no-unibyte", "--no-unibyte", 4, NULL, &skip_args)
1388 || argmatch (argv, argc, "-multibyte", "--multibyte", 4, NULL, &skip_args)
1389 /* Ignore EMACS_UNIBYTE before dumping. */
1390 || (!initialized && noninteractive))
1391 inhibit_unibyte = 1;
1393 /* --unibyte requests that we set up to do everything with single-byte
1394 buffers and strings. We need to handle this before calling
1395 init_lread, init_editfns and other places that generate Lisp strings
1396 from text in the environment. */
1397 /* Actually this shouldn't be needed as of 20.4 in a generally
1398 unibyte environment. As handa says, environment values
1399 aren't now decoded; also existing buffers are now made
1400 unibyte during startup if .emacs sets unibyte. Tested with
1401 8-bit data in environment variables and /etc/passwd, setting
1402 unibyte and Latin-1 in .emacs. -- Dave Love */
1403 if (argmatch (argv, argc, "-unibyte", "--unibyte", 4, NULL, &skip_args)
1404 || argmatch (argv, argc, "-no-multibyte", "--no-multibyte", 4, NULL, &skip_args)
1405 || (getenv ("EMACS_UNIBYTE") && !inhibit_unibyte))
1407 Lisp_Object old_log_max;
1408 Lisp_Object symbol, tail;
1410 symbol = intern_c_string ("enable-multibyte-characters");
1411 Fset_default (symbol, Qnil);
1413 if (initialized)
1415 /* Erase pre-dump messages in *Messages* now so no abort. */
1416 old_log_max = Vmessage_log_max;
1417 XSETFASTINT (Vmessage_log_max, 0);
1418 message_dolog ("", 0, 1, 0);
1419 Vmessage_log_max = old_log_max;
1422 for (tail = Vbuffer_alist; CONSP (tail);
1423 tail = XCDR (tail))
1425 Lisp_Object buffer;
1427 buffer = Fcdr (XCAR (tail));
1428 /* Make a multibyte buffer unibyte. */
1429 if (BUF_Z_BYTE (XBUFFER (buffer)) > BUF_Z (XBUFFER (buffer)))
1431 struct buffer *current = current_buffer;
1433 set_buffer_temp (XBUFFER (buffer));
1434 Fset_buffer_multibyte (Qnil);
1435 set_buffer_temp (current);
1438 message ("Warning: unibyte sessions are obsolete and will disappear");
1442 no_loadup
1443 = argmatch (argv, argc, "-nl", "--no-loadup", 6, NULL, &skip_args);
1445 #ifdef HAVE_NS
1446 ns_alloc_autorelease_pool();
1447 if (!noninteractive)
1449 #ifdef NS_IMPL_COCOA
1450 if (skip_args < argc)
1452 if (!strncmp(argv[skip_args], "-psn", 4))
1454 skip_args += 1;
1455 chdir (getenv ("HOME"));
1457 else if (skip_args+1 < argc && !strncmp(argv[skip_args+1], "-psn", 4))
1459 skip_args += 2;
1460 chdir (getenv ("HOME"));
1463 #endif /* COCOA */
1465 #endif /* HAVE_NS */
1467 #ifdef HAVE_X_WINDOWS
1468 /* Stupid kludge to catch command-line display spec. We can't
1469 handle this argument entirely in window system dependent code
1470 because we don't even know which window system dependent code
1471 to run until we've recognized this argument. */
1473 char *displayname = 0;
1474 int count_before = skip_args;
1476 /* Skip any number of -d options, but only use the last one. */
1477 while (1)
1479 int count_before_this = skip_args;
1481 if (argmatch (argv, argc, "-d", "--display", 3, &displayname, &skip_args))
1482 display_arg = 1;
1483 else if (argmatch (argv, argc, "-display", 0, 3, &displayname, &skip_args))
1484 display_arg = 1;
1485 else
1486 break;
1488 count_before = count_before_this;
1491 /* If we have the form --display=NAME,
1492 convert it into -d name.
1493 This requires inserting a new element into argv. */
1494 if (displayname != 0 && skip_args - count_before == 1)
1496 char **new = (char **) xmalloc (sizeof (char *) * (argc + 2));
1497 int j;
1499 for (j = 0; j < count_before + 1; j++)
1500 new[j] = argv[j];
1501 new[count_before + 1] = "-d";
1502 new[count_before + 2] = displayname;
1503 for (j = count_before + 2; j <argc; j++)
1504 new[j + 1] = argv[j];
1505 argv = new;
1506 argc++;
1508 /* Change --display to -d, when its arg is separate. */
1509 else if (displayname != 0 && skip_args > count_before
1510 && argv[count_before + 1][1] == '-')
1511 argv[count_before + 1] = "-d";
1513 /* Don't actually discard this arg. */
1514 skip_args = count_before;
1516 #endif
1518 /* argmatch must not be used after here,
1519 except when bulding temacs
1520 because the -d argument has not been skipped in skip_args. */
1522 #ifdef MSDOS
1523 /* Call early 'cause init_environment needs it. */
1524 init_dosfns ();
1525 /* Set defaults for several environment variables. */
1526 if (initialized)
1527 init_environment (argc, argv, skip_args);
1528 else
1529 tzset ();
1530 #endif /* MSDOS */
1532 #ifdef WINDOWSNT
1533 globals_of_w32 ();
1534 /* Initialize environment from registry settings. */
1535 init_environment (argv);
1536 init_ntproc (); /* must precede init_editfns. */
1537 #endif
1539 #ifdef HAVE_NS
1540 #ifndef CANNOT_DUMP
1541 if (initialized)
1542 #endif
1543 ns_init_paths ();
1544 #endif
1546 /* Initialize and GC-protect Vinitial_environment and
1547 Vprocess_environment before set_initial_environment fills them
1548 in. */
1549 if (!initialized)
1550 syms_of_callproc ();
1551 /* egetenv is a pretty low-level facility, which may get called in
1552 many circumstances; it seems flimsy to put off initializing it
1553 until calling init_callproc. */
1554 set_initial_environment ();
1555 /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
1556 if this is not done. Do it after set_global_environment so that we
1557 don't pollute Vglobal_environment. */
1558 /* Setting LANG here will defeat the startup locale processing... */
1559 #ifdef AIX
1560 putenv ("LANG=C");
1561 #endif
1563 init_buffer (); /* Init default directory of main buffer. */
1565 init_callproc_1 (); /* Must precede init_cmdargs and init_sys_modes. */
1566 init_cmdargs (argc, argv, skip_args); /* Must precede init_lread. */
1568 if (initialized)
1570 /* Erase any pre-dump messages in the message log, to avoid confusion. */
1571 Lisp_Object old_log_max;
1572 old_log_max = Vmessage_log_max;
1573 XSETFASTINT (Vmessage_log_max, 0);
1574 message_dolog ("", 0, 1, 0);
1575 Vmessage_log_max = old_log_max;
1578 init_callproc (); /* Must follow init_cmdargs but not init_sys_modes. */
1579 init_lread ();
1581 /* Intern the names of all standard functions and variables;
1582 define standard keys. */
1584 if (!initialized)
1586 /* The basic levels of Lisp must come first. Note that
1587 syms_of_data and some others have already been called. */
1588 syms_of_chartab ();
1589 syms_of_lread ();
1590 syms_of_print ();
1591 syms_of_eval ();
1592 syms_of_fns ();
1593 syms_of_floatfns ();
1595 syms_of_buffer ();
1596 syms_of_bytecode ();
1597 syms_of_callint ();
1598 syms_of_casefiddle ();
1599 syms_of_casetab ();
1600 syms_of_category ();
1601 syms_of_ccl ();
1602 syms_of_character ();
1603 syms_of_cmds ();
1604 syms_of_dired ();
1605 syms_of_display ();
1606 syms_of_doc ();
1607 syms_of_editfns ();
1608 syms_of_emacs ();
1609 #ifdef CLASH_DETECTION
1610 syms_of_filelock ();
1611 #endif /* CLASH_DETECTION */
1612 syms_of_indent ();
1613 syms_of_insdel ();
1614 /* syms_of_keymap (); */
1615 syms_of_macros ();
1616 syms_of_marker ();
1617 syms_of_minibuf ();
1618 syms_of_process ();
1619 syms_of_search ();
1620 syms_of_frame ();
1621 syms_of_syntax ();
1622 syms_of_terminal ();
1623 syms_of_term ();
1624 syms_of_undo ();
1625 #ifdef HAVE_SOUND
1626 syms_of_sound ();
1627 #endif
1628 syms_of_textprop ();
1629 syms_of_composite ();
1630 #ifdef WINDOWSNT
1631 syms_of_ntproc ();
1632 #endif /* WINDOWSNT */
1633 syms_of_window ();
1634 syms_of_xdisp ();
1635 syms_of_font ();
1636 #ifdef HAVE_WINDOW_SYSTEM
1637 syms_of_fringe ();
1638 syms_of_image ();
1639 #endif /* HAVE_WINDOW_SYSTEM */
1640 #ifdef HAVE_X_WINDOWS
1641 syms_of_xterm ();
1642 syms_of_xfns ();
1643 syms_of_xmenu ();
1644 syms_of_fontset ();
1645 syms_of_xsettings ();
1646 #ifdef HAVE_X_SM
1647 syms_of_xsmfns ();
1648 #endif
1649 #ifdef HAVE_X11
1650 syms_of_xselect ();
1651 #endif
1652 #endif /* HAVE_X_WINDOWS */
1654 syms_of_menu ();
1656 #ifdef HAVE_NTGUI
1657 syms_of_w32term ();
1658 syms_of_w32fns ();
1659 syms_of_w32select ();
1660 syms_of_w32menu ();
1661 syms_of_fontset ();
1662 #endif /* HAVE_NTGUI */
1664 #ifdef MSDOS
1665 syms_of_xmenu ();
1666 #endif /* MSDOS */
1668 #ifdef HAVE_NS
1669 syms_of_nsterm ();
1670 syms_of_nsfns ();
1671 syms_of_nsmenu ();
1672 syms_of_nsselect ();
1673 syms_of_fontset ();
1674 #endif /* HAVE_NS */
1676 #ifdef HAVE_DBUS
1677 syms_of_dbusbind ();
1678 #endif /* HAVE_DBUS */
1680 #ifdef SYMS_SYSTEM
1681 SYMS_SYSTEM;
1682 #endif
1684 #ifdef SYMS_MACHINE
1685 SYMS_MACHINE;
1686 #endif
1688 keys_of_casefiddle ();
1689 keys_of_cmds ();
1690 keys_of_buffer ();
1691 keys_of_keyboard ();
1692 keys_of_keymap ();
1693 keys_of_window ();
1695 else
1697 /* Initialization that must be done even if the global variable
1698 initialized is non zero. */
1699 #ifdef HAVE_NTGUI
1700 globals_of_w32fns ();
1701 globals_of_w32menu ();
1702 globals_of_w32select ();
1703 #endif /* HAVE_NTGUI */
1706 init_charset ();
1708 init_editfns (); /* init_process uses Voperating_system_release. */
1709 init_process (); /* init_display uses add_keyboard_wait_descriptor. */
1710 init_keyboard (); /* This too must precede init_sys_modes. */
1711 if (!noninteractive)
1712 init_display (); /* Determine terminal type. Calls init_sys_modes. */
1713 init_fns ();
1714 init_xdisp ();
1715 #ifdef HAVE_WINDOW_SYSTEM
1716 init_fringe ();
1717 init_image ();
1718 #endif /* HAVE_WINDOW_SYSTEM */
1719 init_macros ();
1720 init_floatfns ();
1721 #ifdef HAVE_SOUND
1722 init_sound ();
1723 #endif
1724 init_window ();
1725 init_font ();
1727 if (!initialized)
1729 char *file;
1730 /* Handle -l loadup, args passed by Makefile. */
1731 if (argmatch (argv, argc, "-l", "--load", 3, &file, &skip_args))
1732 Vtop_level = Fcons (intern_c_string ("load"),
1733 Fcons (build_string (file), Qnil));
1734 /* Unless next switch is -nl, load "loadup.el" first thing. */
1735 if (! no_loadup)
1736 Vtop_level = Fcons (intern_c_string ("load"),
1737 Fcons (build_string ("loadup.el"), Qnil));
1740 if (initialized)
1742 #ifdef HAVE_TZSET
1744 /* If the execution TZ happens to be the same as the dump TZ,
1745 change it to some other value and then change it back,
1746 to force the underlying implementation to reload the TZ info.
1747 This is needed on implementations that load TZ info from files,
1748 since the TZ file contents may differ between dump and execution. */
1749 char *tz = getenv ("TZ");
1750 if (tz && !strcmp (tz, dump_tz))
1752 ++*tz;
1753 tzset ();
1754 --*tz;
1757 #endif
1760 /* Set up for profiling. This is known to work on FreeBSD,
1761 GNU/Linux and MinGW. It might work on some other systems too.
1762 Give it a try and tell us if it works on your system. To compile
1763 for profiling, use the configure option --enable-profiling. */
1764 #if defined (__FreeBSD__) || defined (GNU_LINUX) || defined(__MINGW32__)
1765 #ifdef PROFILING
1766 if (initialized)
1768 extern void _mcleanup ();
1769 #ifdef __MINGW32__
1770 extern unsigned char etext asm ("etext");
1771 #else
1772 extern char etext;
1773 #endif
1774 extern void safe_bcopy ();
1775 extern void dump_opcode_frequencies ();
1777 atexit (_mcleanup);
1778 /* This uses safe_bcopy because that function comes first in the
1779 Emacs executable. It might be better to use something that
1780 gives the start of the text segment, but start_of_text is not
1781 defined on all systems now. */
1782 monstartup (safe_bcopy, &etext);
1784 else
1785 moncontrol (0);
1786 #endif
1787 #endif
1789 initialized = 1;
1791 #ifdef LOCALTIME_CACHE
1792 /* Some versions of localtime have a bug. They cache the value of the time
1793 zone rather than looking it up every time. Since localtime() is
1794 called to bolt the undumping time into the undumped emacs, this
1795 results in localtime ignoring the TZ environment variable.
1796 This flushes the new TZ value into localtime. */
1797 tzset ();
1798 #endif /* defined (LOCALTIME_CACHE) */
1800 /* Enter editor command loop. This never returns. */
1801 Frecursive_edit ();
1802 /* NOTREACHED */
1803 return 0;
1806 /* Sort the args so we can find the most important ones
1807 at the beginning of argv. */
1809 /* First, here's a table of all the standard options. */
1811 struct standard_args
1813 const char *name;
1814 const char *longname;
1815 int priority;
1816 int nargs;
1819 const struct standard_args standard_args[] =
1821 { "-version", "--version", 150, 0 },
1822 { "-chdir", "--chdir", 130, 1 },
1823 { "-t", "--terminal", 120, 1 },
1824 { "-nw", "--no-window-system", 110, 0 },
1825 { "-nw", "--no-windows", 110, 0 },
1826 { "-batch", "--batch", 100, 0 },
1827 { "-script", "--script", 100, 1 },
1828 { "-daemon", "--daemon", 99, 0 },
1829 { "-help", "--help", 90, 0 },
1830 { "-no-unibyte", "--no-unibyte", 83, 0 },
1831 { "-multibyte", "--multibyte", 82, 0 },
1832 { "-unibyte", "--unibyte", 81, 0 },
1833 { "-no-multibyte", "--no-multibyte", 80, 0 },
1834 { "-nl", "--no-loadup", 70, 0 },
1835 /* -d must come last before the options handled in startup.el. */
1836 { "-d", "--display", 60, 1 },
1837 { "-display", 0, 60, 1 },
1838 /* Now for the options handled in `command-line' (startup.el). */
1839 { "-Q", "--quick", 55, 0 },
1840 { "-quick", 0, 55, 0 },
1841 { "-q", "--no-init-file", 50, 0 },
1842 { "-no-init-file", 0, 50, 0 },
1843 { "-no-site-file", "--no-site-file", 40, 0 },
1844 { "-u", "--user", 30, 1 },
1845 { "-user", 0, 30, 1 },
1846 { "-debug-init", "--debug-init", 20, 0 },
1847 { "-iconic", "--iconic", 15, 0 },
1848 { "-D", "--basic-display", 12, 0},
1849 { "-basic-display", 0, 12, 0},
1850 { "-nbc", "--no-blinking-cursor", 12, 0 },
1851 /* Now for the options handled in `command-line-1' (startup.el). */
1852 { "-nbi", "--no-bitmap-icon", 10, 0 },
1853 { "-bg", "--background-color", 10, 1 },
1854 { "-background", 0, 10, 1 },
1855 { "-fg", "--foreground-color", 10, 1 },
1856 { "-foreground", 0, 10, 1 },
1857 { "-bd", "--border-color", 10, 1 },
1858 { "-bw", "--border-width", 10, 1 },
1859 { "-ib", "--internal-border", 10, 1 },
1860 { "-ms", "--mouse-color", 10, 1 },
1861 { "-cr", "--cursor-color", 10, 1 },
1862 { "-fn", "--font", 10, 1 },
1863 { "-font", 0, 10, 1 },
1864 { "-fs", "--fullscreen", 10, 0 },
1865 { "-fw", "--fullwidth", 10, 0 },
1866 { "-fh", "--fullheight", 10, 0 },
1867 { "-mm", "--maximized", 10, 0 },
1868 { "-g", "--geometry", 10, 1 },
1869 { "-geometry", 0, 10, 1 },
1870 { "-T", "--title", 10, 1 },
1871 { "-title", 0, 10, 1 },
1872 { "-name", "--name", 10, 1 },
1873 { "-xrm", "--xrm", 10, 1 },
1874 { "-parent-id", "--parent-id", 10, 1 },
1875 { "-r", "--reverse-video", 5, 0 },
1876 { "-rv", 0, 5, 0 },
1877 { "-reverse", 0, 5, 0 },
1878 { "-hb", "--horizontal-scroll-bars", 5, 0 },
1879 { "-vb", "--vertical-scroll-bars", 5, 0 },
1880 { "-color", "--color", 5, 0},
1881 { "-no-splash", "--no-splash", 3, 0 },
1882 { "-no-desktop", "--no-desktop", 3, 0 },
1883 #ifdef HAVE_NS
1884 { "-NSAutoLaunch", 0, 5, 1 },
1885 { "-NXAutoLaunch", 0, 5, 1 },
1886 { "-disable-font-backend", "--disable-font-backend", 65, 0 },
1887 { "-_NSMachLaunch", 0, 85, 1 },
1888 { "-MachLaunch", 0, 85, 1 },
1889 { "-macosx", 0, 85, 0 },
1890 { "-NSHost", 0, 85, 1 },
1891 #endif
1892 /* These have the same priority as ordinary file name args,
1893 so they are not reordered with respect to those. */
1894 { "-L", "--directory", 0, 1 },
1895 { "-directory", 0, 0, 1 },
1896 { "-l", "--load", 0, 1 },
1897 { "-load", 0, 0, 1 },
1898 /* This has no longname, because using --scriptload confuses sort_args,
1899 because then the --script long option seems to match twice; ie
1900 you can't have a long option which is a prefix of another long
1901 option. In any case, this is entirely an internal option. */
1902 { "-scriptload", NULL, 0, 1 },
1903 { "-f", "--funcall", 0, 1 },
1904 { "-funcall", 0, 0, 1 },
1905 { "-eval", "--eval", 0, 1 },
1906 { "-execute", "--execute", 0, 1 },
1907 { "-find-file", "--find-file", 0, 1 },
1908 { "-visit", "--visit", 0, 1 },
1909 { "-file", "--file", 0, 1 },
1910 { "-insert", "--insert", 0, 1 },
1911 #ifdef HAVE_NS
1912 { "-NXOpen", 0, 0, 1 },
1913 { "-NXOpenTemp", 0, 0, 1 },
1914 { "-NSOpen", 0, 0, 1 },
1915 { "-NSOpenTemp", 0, 0, 1 },
1916 { "-GSFilePath", 0, 0, 1 },
1917 #endif
1918 /* This should be processed after ordinary file name args and the like. */
1919 { "-kill", "--kill", -10, 0 },
1922 /* Reorder the elements of ARGV (assumed to have ARGC elements)
1923 so that the highest priority ones come first.
1924 Do not change the order of elements of equal priority.
1925 If an option takes an argument, keep it and its argument together.
1927 If an option that takes no argument appears more
1928 than once, eliminate all but one copy of it. */
1930 static void
1931 sort_args (argc, argv)
1932 int argc;
1933 char **argv;
1935 char **new = (char **) xmalloc (sizeof (char *) * argc);
1936 /* For each element of argv,
1937 the corresponding element of options is:
1938 0 for an option that takes no arguments,
1939 1 for an option that takes one argument, etc.
1940 -1 for an ordinary non-option argument. */
1941 int *options = (int *) xmalloc (sizeof (int) * argc);
1942 int *priority = (int *) xmalloc (sizeof (int) * argc);
1943 int to = 1;
1944 int incoming_used = 1;
1945 int from;
1946 int i;
1948 /* Categorize all the options,
1949 and figure out which argv elts are option arguments. */
1950 for (from = 1; from < argc; from++)
1952 options[from] = -1;
1953 priority[from] = 0;
1954 if (argv[from][0] == '-')
1956 int match, thislen;
1957 char *equals;
1959 /* If we have found "--", don't consider
1960 any more arguments as options. */
1961 if (argv[from][1] == '-' && argv[from][2] == 0)
1963 /* Leave the "--", and everything following it, at the end. */
1964 for (; from < argc; from++)
1966 priority[from] = -100;
1967 options[from] = -1;
1969 break;
1972 /* Look for a match with a known old-fashioned option. */
1973 for (i = 0; i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1974 if (!strcmp (argv[from], standard_args[i].name))
1976 options[from] = standard_args[i].nargs;
1977 priority[from] = standard_args[i].priority;
1978 if (from + standard_args[i].nargs >= argc)
1979 fatal ("Option `%s' requires an argument\n", argv[from]);
1980 from += standard_args[i].nargs;
1981 goto done;
1984 /* Look for a match with a known long option.
1985 MATCH is -1 if no match so far, -2 if two or more matches so far,
1986 >= 0 (the table index of the match) if just one match so far. */
1987 if (argv[from][1] == '-')
1989 match = -1;
1990 thislen = strlen (argv[from]);
1991 equals = index (argv[from], '=');
1992 if (equals != 0)
1993 thislen = equals - argv[from];
1995 for (i = 0;
1996 i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1997 if (standard_args[i].longname
1998 && !strncmp (argv[from], standard_args[i].longname,
1999 thislen))
2001 if (match == -1)
2002 match = i;
2003 else
2004 match = -2;
2007 /* If we found exactly one match, use that. */
2008 if (match >= 0)
2010 options[from] = standard_args[match].nargs;
2011 priority[from] = standard_args[match].priority;
2012 /* If --OPTION=VALUE syntax is used,
2013 this option uses just one argv element. */
2014 if (equals != 0)
2015 options[from] = 0;
2016 if (from + options[from] >= argc)
2017 fatal ("Option `%s' requires an argument\n", argv[from]);
2018 from += options[from];
2020 /* FIXME When match < 0, shouldn't there be some error,
2021 or at least indication to the user that there was a
2022 problem? */
2024 done: ;
2028 /* Copy the arguments, in order of decreasing priority, to NEW. */
2029 new[0] = argv[0];
2030 while (incoming_used < argc)
2032 int best = -1;
2033 int best_priority = -9999;
2035 /* Find the highest priority remaining option.
2036 If several have equal priority, take the first of them. */
2037 for (from = 1; from < argc; from++)
2039 if (argv[from] != 0 && priority[from] > best_priority)
2041 best_priority = priority[from];
2042 best = from;
2044 /* Skip option arguments--they are tied to the options. */
2045 if (options[from] > 0)
2046 from += options[from];
2049 if (best < 0)
2050 abort ();
2052 /* Copy the highest priority remaining option, with its args, to NEW.
2053 Unless it is a duplicate of the previous one. */
2054 if (! (options[best] == 0
2055 && ! strcmp (new[to - 1], argv[best])))
2057 new[to++] = argv[best];
2058 for (i = 0; i < options[best]; i++)
2059 new[to++] = argv[best + i + 1];
2062 incoming_used += 1 + (options[best] > 0 ? options[best] : 0);
2064 /* Clear out this option in ARGV. */
2065 argv[best] = 0;
2066 for (i = 0; i < options[best]; i++)
2067 argv[best + i + 1] = 0;
2070 /* If duplicate options were deleted, fill up extra space with null ptrs. */
2071 while (to < argc)
2072 new[to++] = 0;
2074 bcopy (new, argv, sizeof (char *) * argc);
2076 xfree (options);
2077 xfree (new);
2078 xfree (priority);
2081 DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
2082 doc: /* Exit the Emacs job and kill it.
2083 If ARG is an integer, return ARG as the exit program code.
2084 If ARG is a string, stuff it as keyboard input.
2086 The value of `kill-emacs-hook', if not void,
2087 is a list of functions (of no args),
2088 all of which are called before Emacs is actually killed. */)
2089 (arg)
2090 Lisp_Object arg;
2092 struct gcpro gcpro1;
2094 GCPRO1 (arg);
2096 if (feof (stdin))
2097 arg = Qt;
2099 if (!NILP (Vrun_hooks) && !noninteractive)
2100 call1 (Vrun_hooks, intern ("kill-emacs-hook"));
2102 UNGCPRO;
2104 shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
2106 /* If we have an auto-save list file,
2107 kill it because we are exiting Emacs deliberately (not crashing).
2108 Do it after shut_down_emacs, which does an auto-save. */
2109 if (STRINGP (Vauto_save_list_file_name))
2110 unlink (SDATA (Vauto_save_list_file_name));
2112 exit (INTEGERP (arg) ? XINT (arg) : EXIT_SUCCESS);
2113 /* NOTREACHED */
2114 return Qnil;
2118 /* Perform an orderly shutdown of Emacs. Autosave any modified
2119 buffers, kill any child processes, clean up the terminal modes (if
2120 we're in the foreground), and other stuff like that. Don't perform
2121 any redisplay; this may be called when Emacs is shutting down in
2122 the background, or after its X connection has died.
2124 If SIG is a signal number, print a message for it.
2126 This is called by fatal signal handlers, X protocol error handlers,
2127 and Fkill_emacs. */
2129 void
2130 shut_down_emacs (sig, no_x, stuff)
2131 int sig, no_x;
2132 Lisp_Object stuff;
2134 /* Prevent running of hooks from now on. */
2135 Vrun_hooks = Qnil;
2137 /* Don't update display from now on. */
2138 Vinhibit_redisplay = Qt;
2140 /* If we are controlling the terminal, reset terminal modes. */
2141 #ifdef EMACS_HAVE_TTY_PGRP
2143 int pgrp = EMACS_GETPGRP (0);
2145 int tpgrp;
2146 if (EMACS_GET_TTY_PGRP (0, &tpgrp) != -1
2147 && tpgrp == pgrp)
2149 reset_all_sys_modes ();
2150 if (sig && sig != SIGTERM)
2151 fprintf (stderr, "Fatal error (%d)", sig);
2154 #else
2155 fflush (stdout);
2156 reset_all_sys_modes ();
2157 #endif
2159 stuff_buffered_input (stuff);
2161 #ifdef subprocesses
2162 inhibit_sentinels = 1;
2163 #endif
2164 kill_buffer_processes (Qnil);
2165 Fdo_auto_save (Qt, Qnil);
2167 #ifdef CLASH_DETECTION
2168 unlock_all_files ();
2169 #endif
2171 #if 0 /* This triggers a bug in XCloseDisplay and is not needed. */
2172 #ifdef HAVE_X_WINDOWS
2173 /* It's not safe to call intern here. Maybe we are crashing. */
2174 if (!noninteractive && SYMBOLP (Vinitial_window_system)
2175 && SCHARS (SYMBOL_NAME (Vinitial_window_system)) == 1
2176 && SREF (SYMBOL_NAME (Vinitial_window_system), 0) == 'x'
2177 && ! no_x)
2178 Fx_close_current_connection ();
2179 #endif /* HAVE_X_WINDOWS */
2180 #endif
2182 #ifdef SIGIO
2183 /* There is a tendency for a SIGIO signal to arrive within exit,
2184 and cause a SIGHUP because the input descriptor is already closed. */
2185 unrequest_sigio ();
2186 signal (SIGIO, SIG_IGN);
2187 #endif
2189 #ifdef WINDOWSNT
2190 term_ntproc ();
2191 #endif
2193 /* Do this only if terminating normally, we want glyph matrices
2194 etc. in a core dump. */
2195 if (sig == 0 || sig == SIGTERM)
2197 check_glyph_memory ();
2198 check_message_stack ();
2201 #ifdef MSDOS
2202 dos_cleanup ();
2203 #endif
2205 #ifdef HAVE_NS
2206 ns_term_shutdown (sig);
2207 #endif
2212 #ifndef CANNOT_DUMP
2214 DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
2215 doc: /* Dump current state of Emacs into executable file FILENAME.
2216 Take symbols from SYMFILE (presumably the file you executed to run Emacs).
2217 This is used in the file `loadup.el' when building Emacs.
2219 You must run Emacs in batch mode in order to dump it. */)
2220 (filename, symfile)
2221 Lisp_Object filename, symfile;
2223 extern char my_edata[];
2224 Lisp_Object tem;
2225 Lisp_Object symbol;
2226 int count = SPECPDL_INDEX ();
2228 check_pure_size ();
2230 if (! noninteractive)
2231 error ("Dumping Emacs works only in batch mode");
2233 #ifdef GNU_LINUX
2234 if (heap_bss_diff > MAX_HEAP_BSS_DIFF)
2236 fprintf (stderr, "**************************************************\n");
2237 fprintf (stderr, "Warning: Your system has a gap between BSS and the\n");
2238 fprintf (stderr, "heap (%lu bytes). This usually means that exec-shield\n",
2239 heap_bss_diff);
2240 fprintf (stderr, "or something similar is in effect. The dump may\n");
2241 fprintf (stderr, "fail because of this. See the section about\n");
2242 fprintf (stderr, "exec-shield in etc/PROBLEMS for more information.\n");
2243 fprintf (stderr, "**************************************************\n");
2245 #endif /* GNU_LINUX */
2247 /* Bind `command-line-processed' to nil before dumping,
2248 so that the dumped Emacs will process its command line
2249 and set up to work with X windows if appropriate. */
2250 symbol = intern ("command-line-processed");
2251 specbind (symbol, Qnil);
2253 CHECK_STRING (filename);
2254 filename = Fexpand_file_name (filename, Qnil);
2255 if (!NILP (symfile))
2257 CHECK_STRING (symfile);
2258 if (SCHARS (symfile))
2259 symfile = Fexpand_file_name (symfile, Qnil);
2262 tem = Vpurify_flag;
2263 Vpurify_flag = Qnil;
2265 #ifdef HAVE_TZSET
2266 set_time_zone_rule (dump_tz);
2267 #ifndef LOCALTIME_CACHE
2268 /* Force a tz reload, since set_time_zone_rule doesn't. */
2269 tzset ();
2270 #endif
2271 #endif
2273 fflush (stdout);
2274 /* Tell malloc where start of impure now is. */
2275 /* Also arrange for warnings when nearly out of space. */
2276 #ifndef SYSTEM_MALLOC
2277 #ifndef WINDOWSNT
2278 /* On Windows, this was done before dumping, and that once suffices.
2279 Meanwhile, my_edata is not valid on Windows. */
2280 memory_warnings (my_edata, malloc_warning);
2281 #endif /* not WINDOWSNT */
2282 #endif
2283 #if !defined (SYSTEM_MALLOC) && defined (HAVE_GTK_AND_PTHREAD) && !defined SYNC_INPUT
2284 /* Pthread may call malloc before main, and then we will get an endless
2285 loop, because pthread_self (see alloc.c) calls malloc the first time
2286 it is called on some systems. */
2287 reset_malloc_hooks ();
2288 #endif
2289 #ifdef DOUG_LEA_MALLOC
2290 malloc_state_ptr = malloc_get_state ();
2291 #endif
2293 #ifdef USE_MMAP_FOR_BUFFERS
2294 mmap_set_vars (0);
2295 #endif
2296 unexec (SDATA (filename),
2297 !NILP (symfile) ? SDATA (symfile) : 0, my_edata, 0, 0);
2298 #ifdef USE_MMAP_FOR_BUFFERS
2299 mmap_set_vars (1);
2300 #endif
2301 #ifdef DOUG_LEA_MALLOC
2302 free (malloc_state_ptr);
2303 #endif
2305 Vpurify_flag = tem;
2307 return unbind_to (count, Qnil);
2310 #endif /* not CANNOT_DUMP */
2312 #if HAVE_SETLOCALE
2313 /* Recover from setlocale (LC_ALL, ""). */
2314 void
2315 fixup_locale ()
2317 /* The Emacs Lisp reader needs LC_NUMERIC to be "C",
2318 so that numbers are read and printed properly for Emacs Lisp. */
2319 setlocale (LC_NUMERIC, "C");
2322 /* Set system locale CATEGORY, with previous locale *PLOCALE, to
2323 DESIRED_LOCALE. */
2324 static void
2325 synchronize_locale (category, plocale, desired_locale)
2326 int category;
2327 Lisp_Object *plocale;
2328 Lisp_Object desired_locale;
2330 if (! EQ (*plocale, desired_locale))
2332 *plocale = desired_locale;
2333 setlocale (category, (STRINGP (desired_locale)
2334 ? (char *) SDATA (desired_locale)
2335 : ""));
2339 /* Set system time locale to match Vsystem_time_locale, if possible. */
2340 void
2341 synchronize_system_time_locale ()
2343 synchronize_locale (LC_TIME, &Vprevious_system_time_locale,
2344 Vsystem_time_locale);
2347 /* Set system messages locale to match Vsystem_messages_locale, if
2348 possible. */
2349 void
2350 synchronize_system_messages_locale ()
2352 #ifdef LC_MESSAGES
2353 synchronize_locale (LC_MESSAGES, &Vprevious_system_messages_locale,
2354 Vsystem_messages_locale);
2355 #endif
2357 #endif /* HAVE_SETLOCALE */
2359 #ifndef SEPCHAR
2360 #define SEPCHAR ':'
2361 #endif
2363 Lisp_Object
2364 decode_env_path (evarname, defalt)
2365 char *evarname, *defalt;
2367 register char *path, *p;
2368 Lisp_Object lpath, element, tem;
2370 /* It's okay to use getenv here, because this function is only used
2371 to initialize variables when Emacs starts up, and isn't called
2372 after that. */
2373 if (evarname != 0)
2374 path = (char *) getenv (evarname);
2375 else
2376 path = 0;
2377 if (!path)
2378 path = defalt;
2379 #ifdef DOS_NT
2380 /* Ensure values from the environment use the proper directory separator. */
2381 if (path)
2383 p = alloca (strlen (path) + 1);
2384 strcpy (p, path);
2385 path = p;
2387 if ('/' == DIRECTORY_SEP)
2388 dostounix_filename (path);
2389 else
2390 unixtodos_filename (path);
2392 #endif
2393 lpath = Qnil;
2394 while (1)
2396 p = index (path, SEPCHAR);
2397 if (!p) p = path + strlen (path);
2398 element = (p - path ? make_string (path, p - path)
2399 : build_string ("."));
2401 /* Add /: to the front of the name
2402 if it would otherwise be treated as magic. */
2403 tem = Ffind_file_name_handler (element, Qt);
2405 /* However, if the handler says "I'm safe",
2406 don't bother adding /:. */
2407 if (SYMBOLP (tem))
2409 Lisp_Object prop;
2410 prop = Fget (tem, intern ("safe-magic"));
2411 if (! NILP (prop))
2412 tem = Qnil;
2415 if (! NILP (tem))
2416 element = concat2 (build_string ("/:"), element);
2418 lpath = Fcons (element, lpath);
2419 if (*p)
2420 path = p + 1;
2421 else
2422 break;
2424 return Fnreverse (lpath);
2427 DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0,
2428 doc: /* Return non-nil if the current emacs process is a daemon.
2429 If the daemon was given a name argument, return that name. */)
2432 if (IS_DAEMON)
2433 if (daemon_name)
2434 return build_string (daemon_name);
2435 else
2436 return Qt;
2437 else
2438 return Qnil;
2441 DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0,
2442 doc: /* Mark the Emacs daemon as being initialized.
2443 This finishes the daemonization process by doing the other half of detaching
2444 from the parent process and its tty file descriptors. */)
2447 int nfd;
2449 if (!IS_DAEMON)
2450 error ("This function can only be called if emacs is run as a daemon");
2452 if (daemon_pipe[1] < 0)
2453 error ("The daemon has already been initialized");
2455 if (NILP (Vafter_init_time))
2456 error ("This function can only be called after loading the init files");
2458 /* Get rid of stdin, stdout and stderr. */
2459 nfd = open ("/dev/null", O_RDWR);
2460 dup2 (nfd, 0);
2461 dup2 (nfd, 1);
2462 dup2 (nfd, 2);
2463 close (nfd);
2465 /* Closing the pipe will notify the parent that it can exit.
2466 FIXME: In case some other process inherited the pipe, closing it here
2467 won't notify the parent because it's still open elsewhere, so we
2468 additionally send a byte, just to make sure the parent really exits.
2469 Instead, we should probably close the pipe in start-process and
2470 call-process to make sure the pipe is never inherited by
2471 subprocesses. */
2472 write (daemon_pipe[1], "\n", 1);
2473 close (daemon_pipe[1]);
2474 /* Set it to an invalid value so we know we've already run this function. */
2475 daemon_pipe[1] = -1;
2476 return Qt;
2479 void
2480 syms_of_emacs ()
2482 Qfile_name_handler_alist = intern_c_string ("file-name-handler-alist");
2483 staticpro (&Qfile_name_handler_alist);
2485 #ifndef CANNOT_DUMP
2486 defsubr (&Sdump_emacs);
2487 #endif
2489 defsubr (&Skill_emacs);
2491 defsubr (&Sinvocation_name);
2492 defsubr (&Sinvocation_directory);
2493 defsubr (&Sdaemonp);
2494 defsubr (&Sdaemon_initialized);
2496 DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
2497 doc: /* Args passed by shell to Emacs, as a list of strings.
2498 Many arguments are deleted from the list as they are processed. */);
2500 DEFVAR_LISP ("system-type", &Vsystem_type,
2501 doc: /* The value is a symbol indicating the type of operating system you are using.
2502 Special values:
2503 `gnu' compiled for a GNU Hurd system.
2504 `gnu/linux' compiled for a GNU/Linux system.
2505 `gnu/kfreebsd' compiled for a GNU system with a FreeBSD kernel.
2506 `darwin' compiled for Darwin (GNU-Darwin, Mac OS X, ...).
2507 `ms-dos' compiled as an MS-DOS application.
2508 `windows-nt' compiled as a native W32 application.
2509 `cygwin' compiled using the Cygwin library.
2510 Anything else (in Emacs 23.1, the possibilities are: aix, berkeley-unix,
2511 hpux, irix, lynxos 3.0.1, usg-unix-v) indicates some sort of Unix system. */);
2512 Vsystem_type = intern_c_string (SYSTEM_TYPE);
2514 DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
2515 doc: /* Value is string indicating configuration Emacs was built for.
2516 On MS-Windows, the value reflects the OS flavor and version on which
2517 Emacs is running. */);
2518 Vsystem_configuration = build_string (EMACS_CONFIGURATION);
2520 DEFVAR_LISP ("system-configuration-options", &Vsystem_configuration_options,
2521 doc: /* String containing the configuration options Emacs was built with. */);
2522 Vsystem_configuration_options = build_string (EMACS_CONFIG_OPTIONS);
2524 DEFVAR_BOOL ("noninteractive", &noninteractive1,
2525 doc: /* Non-nil means Emacs is running without interactive terminal. */);
2527 DEFVAR_LISP ("kill-emacs-hook", &Vkill_emacs_hook,
2528 doc: /* Hook to be run when `kill-emacs' is called.
2529 Since `kill-emacs' may be invoked when the terminal is disconnected (or
2530 in other similar situations), functions placed on this hook should not
2531 expect to be able to interact with the user. To ask for confirmation,
2532 see `kill-emacs-query-functions' instead.
2534 The hook is not run in batch mode, i.e., if `noninteractive' is non-nil. */);
2535 Vkill_emacs_hook = Qnil;
2537 DEFVAR_INT ("emacs-priority", &emacs_priority,
2538 doc: /* Priority for Emacs to run at.
2539 This value is effective only if set before Emacs is dumped,
2540 and only if the Emacs executable is installed with setuid to permit
2541 it to change priority. (Emacs sets its uid back to the real uid.)
2542 Currently, you need to define SET_EMACS_PRIORITY in `config.h'
2543 before you compile Emacs, to enable the code for this feature. */);
2544 emacs_priority = 0;
2546 DEFVAR_LISP ("path-separator", &Vpath_separator,
2547 doc: /* String containing the character that separates directories in
2548 search paths, such as PATH and other similar environment variables. */);
2550 char c = SEPCHAR;
2551 Vpath_separator = make_string (&c, 1);
2554 DEFVAR_LISP ("invocation-name", &Vinvocation_name,
2555 doc: /* The program name that was used to run Emacs.
2556 Any directory names are omitted. */);
2558 DEFVAR_LISP ("invocation-directory", &Vinvocation_directory,
2559 doc: /* The directory in which the Emacs executable was found, to run it.
2560 The value is nil if that directory's name is not known. */);
2562 DEFVAR_LISP ("installation-directory", &Vinstallation_directory,
2563 doc: /* A directory within which to look for the `lib-src' and `etc' directories.
2564 This is non-nil when we can't find those directories in their standard
2565 installed locations, but we can find them near where the Emacs executable
2566 was found. */);
2567 Vinstallation_directory = Qnil;
2569 DEFVAR_LISP ("system-messages-locale", &Vsystem_messages_locale,
2570 doc: /* System locale for messages. */);
2571 Vsystem_messages_locale = Qnil;
2573 DEFVAR_LISP ("previous-system-messages-locale",
2574 &Vprevious_system_messages_locale,
2575 doc: /* Most recently used system locale for messages. */);
2576 Vprevious_system_messages_locale = Qnil;
2578 DEFVAR_LISP ("system-time-locale", &Vsystem_time_locale,
2579 doc: /* System locale for time. */);
2580 Vsystem_time_locale = Qnil;
2582 DEFVAR_LISP ("previous-system-time-locale", &Vprevious_system_time_locale,
2583 doc: /* Most recently used system locale for time. */);
2584 Vprevious_system_time_locale = Qnil;
2586 DEFVAR_LISP ("before-init-time", &Vbefore_init_time,
2587 doc: /* Value of `current-time' before Emacs begins initialization. */);
2588 Vbefore_init_time = Qnil;
2590 DEFVAR_LISP ("after-init-time", &Vafter_init_time,
2591 doc: /* Value of `current-time' after loading the init files.
2592 This is nil during initialization. */);
2593 Vafter_init_time = Qnil;
2595 DEFVAR_BOOL ("inhibit-x-resources", &inhibit_x_resources,
2596 doc: /* If non-nil, X resources, Windows Registry settings, and NS defaults are not used. */);
2597 inhibit_x_resources = 0;
2599 DEFVAR_LISP ("emacs-copyright", &Vemacs_copyright,
2600 doc: /* Short copyright string for this version of Emacs. */);
2601 Vemacs_copyright = build_string (emacs_copyright);
2603 DEFVAR_LISP ("emacs-version", &Vemacs_version,
2604 doc: /* Version numbers of this version of Emacs. */);
2605 Vemacs_version = build_string (emacs_version);
2607 /* Make sure IS_DAEMON starts up as false. */
2608 daemon_pipe[1] = 0;
2611 /* arch-tag: 7bfd356a-c720-4612-8ab6-aa4222931c2e
2612 (do not change this comment) */