merge from trunk, fix conflicts
[emacs.git] / src / emacs.c
blobdebf9f2305f9a0e1e0d6563f029ba93b5b7f1176
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 "xwidget.h"
59 #include "systty.h"
60 #include "blockinput.h"
61 #include "syssignal.h"
62 #include "process.h"
63 #include "frame.h"
64 #include "termhooks.h"
65 #include "keyboard.h"
66 #include "keymap.h"
68 #ifdef HAVE_NS
69 #include "nsterm.h"
70 #endif
72 #ifdef HAVE_SETLOCALE
73 #include <locale.h>
74 #endif
76 #ifdef HAVE_SETRLIMIT
77 #include <sys/time.h>
78 #include <sys/resource.h>
79 #endif
81 #ifdef HAVE_PERSONALITY_LINUX32
82 #include <sys/personality.h>
83 #endif
85 #ifndef O_RDWR
86 #define O_RDWR 2
87 #endif
89 #ifdef HAVE_SETPGID
90 #if !defined (USG)
91 #undef setpgrp
92 #define setpgrp setpgid
93 #endif
94 #endif
96 /* If you change the following line, remember to update
97 msdos/mainmake.v2 which gleans the Emacs version from it! */
98 static const char emacs_copyright[] = "Copyright (C) 2010 Free Software Foundation, Inc.";
99 static const char emacs_version[] = "24.0.50";
101 /* Make these values available in GDB, which doesn't see macros. */
103 #ifdef USE_LSB_TAG
104 int gdb_use_lsb = 1;
105 #else
106 int gdb_use_lsb = 0;
107 #endif
108 #ifndef USE_LISP_UNION_TYPE
109 int gdb_use_union = 0;
110 #else
111 int gdb_use_union = 1;
112 #endif
113 EMACS_INT gdb_valbits = VALBITS;
114 EMACS_INT gdb_gctypebits = GCTYPEBITS;
115 #if defined (DATA_SEG_BITS) && ! defined (USE_LSB_TAG)
116 EMACS_INT gdb_data_seg_bits = DATA_SEG_BITS;
117 #else
118 EMACS_INT gdb_data_seg_bits = 0;
119 #endif
120 EMACS_INT PVEC_FLAG = PSEUDOVECTOR_FLAG;
121 EMACS_INT gdb_array_mark_flag = ARRAY_MARK_FLAG;
122 /* GDB might say "No enum type named pvec_type" if we don't have at
123 least one symbol with that type, and then xbacktrace could fail. */
124 enum pvec_type gdb_pvec_type = PVEC_TYPE_MASK;
126 /* Command line args from shell, as list of strings. */
127 Lisp_Object Vcommand_line_args;
129 /* The name under which Emacs was invoked, with any leading directory
130 names discarded. */
131 Lisp_Object Vinvocation_name;
133 /* The directory name from which Emacs was invoked. */
134 Lisp_Object Vinvocation_directory;
136 /* The directory name in which to find subdirs such as lisp and etc.
137 nil means get them only from PATH_LOADSEARCH. */
138 Lisp_Object Vinstallation_directory;
140 /* The values of `current-time' before and after Emacs initialization. */
141 Lisp_Object Vbefore_init_time, Vafter_init_time;
143 /* Hook run by `kill-emacs' before it does really anything. */
144 Lisp_Object Vkill_emacs_hook;
146 /* Empty lisp strings. To avoid having to build any others. */
147 Lisp_Object empty_unibyte_string, empty_multibyte_string;
149 /* Search path separator. */
150 Lisp_Object Vpath_separator;
152 /* Set nonzero after Emacs has started up the first time.
153 Prevents reinitialization of the Lisp world and keymaps
154 on subsequent starts. */
155 int initialized;
157 #ifdef DOUG_LEA_MALLOC
158 /* Preserves a pointer to the memory allocated that copies that
159 static data inside glibc's malloc. */
160 void *malloc_state_ptr;
161 /* From glibc, a routine that returns a copy of the malloc internal state. */
162 extern void *malloc_get_state (void);
163 /* From glibc, a routine that overwrites the malloc internal state. */
164 extern int malloc_set_state (void*);
165 /* Non-zero if the MALLOC_CHECK_ environment variable was set while
166 dumping. Used to work around a bug in glibc's malloc. */
167 int malloc_using_checking;
168 #endif
170 /* Variable whose value is symbol giving operating system type. */
171 Lisp_Object Vsystem_type;
173 /* Variable whose value is string giving configuration built for. */
174 Lisp_Object Vsystem_configuration;
176 /* Variable whose value is string giving configuration options,
177 for use when reporting bugs. */
178 Lisp_Object Vsystem_configuration_options;
180 Lisp_Object Qfile_name_handler_alist;
182 /* Current and previous system locales for messages and time. */
183 Lisp_Object Vsystem_messages_locale;
184 Lisp_Object Vprevious_system_messages_locale;
185 Lisp_Object Vsystem_time_locale;
186 Lisp_Object Vprevious_system_time_locale;
188 /* Copyright and version info. The version number may be updated by
189 Lisp code. */
190 Lisp_Object Vemacs_copyright, Vemacs_version;
192 /* If non-zero, emacs should not attempt to use a window-specific code,
193 but instead should use the virtual terminal under which it was started. */
194 int inhibit_window_system;
196 /* If nonzero, set Emacs to run at this priority. This is also used
197 in child_setup and sys_suspend to make sure subshells run at normal
198 priority; those functions have their own extern declaration. */
199 EMACS_INT emacs_priority;
201 /* If non-zero, a filter or a sentinel is running. Tested to save the match
202 data on the first attempt to change it inside asynchronous code. */
203 int running_asynch_code;
205 #if defined(HAVE_X_WINDOWS) || defined(HAVE_NS)
206 /* If non-zero, -d was specified, meaning we're using some window system. */
207 int display_arg;
208 #endif
210 /* An address near the bottom of the stack.
211 Tells GC how to save a copy of the stack. */
212 char *stack_bottom;
214 /* The address where the heap starts (from the first sbrk (0) call). */
215 static void *my_heap_start;
217 /* The gap between BSS end and heap start as far as we can tell. */
218 static unsigned long heap_bss_diff;
220 /* If the gap between BSS end and heap start is larger than this
221 output a warning in dump-emacs. */
222 #define MAX_HEAP_BSS_DIFF (1024*1024)
224 /* Nonzero means running Emacs without interactive terminal. */
226 int noninteractive;
228 /* Value of Lisp variable `noninteractive'.
229 Normally same as C variable `noninteractive'
230 but nothing terrible happens if user sets this one. */
232 int noninteractive1;
234 /* Nonzero means Emacs was run in --quick mode. */
235 int inhibit_x_resources;
237 /* Name for the server started by the daemon.*/
238 static char *daemon_name;
240 /* Pipe used to send exit notification to the daemon parent at
241 startup. */
242 int daemon_pipe[2];
244 /* Save argv and argc. */
245 char **initial_argv;
246 int initial_argc;
248 static void sort_args (int argc, char **argv);
249 void syms_of_emacs (void);
251 /* MSVC needs each string be shorter than 2048 bytes, so the usage
252 strings below are split to not overflow this limit. */
253 #define USAGE1 "\
254 Usage: %s [OPTION-OR-FILENAME]...\n\
256 Run Emacs, the extensible, customizable, self-documenting real-time\n\
257 display editor. The recommended way to start Emacs for normal editing\n\
258 is with no options at all.\n\
260 Run M-x info RET m emacs RET m emacs invocation RET inside Emacs to\n\
261 read the main documentation for these command-line arguments.\n\
263 Initialization options:\n\
265 --batch do not do interactive display; implies -q\n\
266 --chdir DIR change to directory DIR\n\
267 --daemon start a server in the background\n\
268 --debug-init enable Emacs Lisp debugger for init file\n\
269 --display, -d DISPLAY use X server DISPLAY\n\
270 --no-desktop do not load a saved desktop\n\
271 --no-init-file, -q load neither ~/.emacs nor default.el\n\
272 --no-shared-memory, -nl do not use shared memory\n\
273 --no-site-file do not load site-start.el\n\
274 --no-splash do not display a splash screen on startup\n\
275 --no-window-system, -nw do not communicate with X, ignoring $DISPLAY\n\
276 --quick, -Q equivalent to -q --no-site-file --no-splash\n\
277 --script FILE run FILE as an Emacs Lisp script\n\
278 --terminal, -t DEVICE use DEVICE for terminal I/O\n\
279 --user, -u USER load ~USER/.emacs instead of your own\n\
280 \n%s"
282 #define USAGE2 "\
283 Action options:\n\
285 FILE visit FILE using find-file\n\
286 +LINE go to line LINE in next FILE\n\
287 +LINE:COLUMN go to line LINE, column COLUMN, in next FILE\n\
288 --directory, -L DIR add DIR to variable load-path\n\
289 --eval EXPR evaluate Emacs Lisp expression EXPR\n\
290 --execute EXPR evaluate Emacs Lisp expression EXPR\n\
291 --file FILE visit FILE using find-file\n\
292 --find-file FILE visit FILE using find-file\n\
293 --funcall, -f FUNC call Emacs Lisp function FUNC with no arguments\n\
294 --insert FILE insert contents of FILE into current buffer\n\
295 --kill exit without asking for confirmation\n\
296 --load, -l FILE load Emacs Lisp FILE using the load function\n\
297 --visit FILE visit FILE using find-file\n\
300 #define USAGE3 "\
301 Display options:\n\
303 --background-color, -bg COLOR window background color\n\
304 --basic-display, -D disable many display features;\n\
305 used for debugging Emacs\n\
306 --border-color, -bd COLOR main border color\n\
307 --border-width, -bw WIDTH width of main border\n\
308 --color, --color=MODE override color mode for character terminals;\n\
309 MODE defaults to `auto', and can also\n\
310 be `never', `auto', `always',\n\
311 or a mode name like `ansi8'\n\
312 --cursor-color, -cr COLOR color of the Emacs cursor indicating point\n\
313 --font, -fn FONT default font; must be fixed-width\n\
314 --foreground-color, -fg COLOR window foreground color\n\
315 --fullheight, -fh make the first frame high as the screen\n\
316 --fullscreen, -fs make first frame fullscreen\n\
317 --fullwidth, -fw make the first frame wide as the screen\n\
318 --maximized, -mm make the first frame maximized\n\
319 --geometry, -g GEOMETRY window geometry\n\
320 --no-bitmap-icon, -nbi do not use picture of gnu for Emacs icon\n\
321 --iconic start Emacs in iconified state\n\
322 --internal-border, -ib WIDTH width between text and main border\n\
323 --line-spacing, -lsp PIXELS additional space to put between lines\n\
324 --mouse-color, -ms COLOR mouse cursor color in Emacs window\n\
325 --name NAME title for initial Emacs frame\n\
326 --no-blinking-cursor, -nbc disable blinking cursor\n\
327 --reverse-video, -r, -rv switch foreground and background\n\
328 --title, -T TITLE title for initial Emacs frame\n\
329 --vertical-scroll-bars, -vb enable vertical scroll bars\n\
330 --xrm XRESOURCES set additional X resources\n\
331 --parent-id XID set parent window\n\
332 --help display this help and exit\n\
333 --version output version information and exit\n\
336 #define USAGE4 "\
337 You can generally also specify long option names with a single -; for\n\
338 example, -batch as well as --batch. You can use any unambiguous\n\
339 abbreviation for a --option.\n\
341 Various environment variables and window system resources also affect\n\
342 Emacs' operation. See the main documentation.\n\
344 Report bugs to bug-gnu-emacs@gnu.org. First, please see the Bugs\n\
345 section of the Emacs manual or the file BUGS.\n"
348 /* Signal code for the fatal signal that was received. */
349 int fatal_error_code;
351 /* Nonzero if handling a fatal error already. */
352 int fatal_error_in_progress;
354 /* If non-null, call this function from fatal_error_signal before
355 committing suicide. */
357 void (*fatal_error_signal_hook) (void);
359 #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD
360 /* When compiled with GTK and running under Gnome,
361 multiple threads may be created. Keep track of our main
362 thread to make sure signals are delivered to it (see syssignal.h). */
364 pthread_t main_thread;
365 #endif
368 /* Handle bus errors, invalid instruction, etc. */
369 SIGTYPE
370 fatal_error_signal (int sig)
372 SIGNAL_THREAD_CHECK (sig);
373 fatal_error_code = sig;
374 signal (sig, SIG_DFL);
376 TOTALLY_UNBLOCK_INPUT;
378 /* If fatal error occurs in code below, avoid infinite recursion. */
379 if (! fatal_error_in_progress)
381 fatal_error_in_progress = 1;
383 if (sig == SIGTERM || sig == SIGHUP)
384 Fkill_emacs (make_number (sig));
386 shut_down_emacs (sig, 0, Qnil);
389 /* Signal the same code; this time it will really be fatal.
390 Remember that since we're in a signal handler, the signal we're
391 going to send is probably blocked, so we have to unblock it if we
392 want to really receive it. */
393 #ifndef MSDOS
394 sigunblock (sigmask (fatal_error_code));
395 #endif
397 if (fatal_error_signal_hook)
398 fatal_error_signal_hook ();
400 kill (getpid (), fatal_error_code);
403 #ifdef SIGDANGER
405 /* Handler for SIGDANGER. */
406 SIGTYPE
407 memory_warning_signal (sig)
408 int sig;
410 signal (sig, memory_warning_signal);
411 SIGNAL_THREAD_CHECK (sig);
413 malloc_warning ("Operating system warns that virtual memory is running low.\n");
415 /* It might be unsafe to call do_auto_save now. */
416 force_auto_save_soon ();
418 #endif
420 /* We define abort, rather than using it from the library,
421 so that GDB can return from a breakpoint here.
422 MSDOS has its own definition in msdos.c. */
424 #if ! defined (DOS_NT) && ! defined (NO_ABORT)
426 void
427 abort (void)
429 kill (getpid (), SIGABRT);
430 /* This shouldn't be executed, but it prevents a warning. */
431 exit (1);
433 #endif
436 /* Code for dealing with Lisp access to the Unix command line. */
438 static void
439 init_cmdargs (int argc, char **argv, int skip_args)
441 register int i;
442 Lisp_Object name, dir, tem;
443 int count = SPECPDL_INDEX ();
444 Lisp_Object raw_name;
446 initial_argv = argv;
447 initial_argc = argc;
449 raw_name = build_string (argv[0]);
451 /* Add /: to the front of the name
452 if it would otherwise be treated as magic. */
453 tem = Ffind_file_name_handler (raw_name, Qt);
454 if (! NILP (tem))
455 raw_name = concat2 (build_string ("/:"), raw_name);
457 Vinvocation_name = Ffile_name_nondirectory (raw_name);
458 Vinvocation_directory = Ffile_name_directory (raw_name);
460 /* If we got no directory in argv[0], search PATH to find where
461 Emacs actually came from. */
462 if (NILP (Vinvocation_directory))
464 Lisp_Object found;
465 int yes = openp (Vexec_path, Vinvocation_name,
466 Vexec_suffixes, &found, make_number (X_OK));
467 if (yes == 1)
469 /* Add /: to the front of the name
470 if it would otherwise be treated as magic. */
471 tem = Ffind_file_name_handler (found, Qt);
472 if (! NILP (tem))
473 found = concat2 (build_string ("/:"), found);
474 Vinvocation_directory = Ffile_name_directory (found);
478 if (!NILP (Vinvocation_directory)
479 && NILP (Ffile_name_absolute_p (Vinvocation_directory)))
480 /* Emacs was started with relative path, like ./emacs.
481 Make it absolute. */
482 Vinvocation_directory = Fexpand_file_name (Vinvocation_directory, Qnil);
484 Vinstallation_directory = Qnil;
486 if (!NILP (Vinvocation_directory))
488 dir = Vinvocation_directory;
489 name = Fexpand_file_name (Vinvocation_name, dir);
490 while (1)
492 Lisp_Object tem, lib_src_exists;
493 Lisp_Object etc_exists, info_exists;
495 /* See if dir contains subdirs for use by Emacs.
496 Check for the ones that would exist in a build directory,
497 not including lisp and info. */
498 tem = Fexpand_file_name (build_string ("lib-src"), dir);
499 lib_src_exists = Ffile_exists_p (tem);
501 #ifdef MSDOS
502 /* MSDOS installations frequently remove lib-src, but we still
503 must set installation-directory, or else info won't find
504 its files (it uses the value of installation-directory). */
505 tem = Fexpand_file_name (build_string ("info"), dir);
506 info_exists = Ffile_exists_p (tem);
507 #else
508 info_exists = Qnil;
509 #endif
511 if (!NILP (lib_src_exists) || !NILP (info_exists))
513 tem = Fexpand_file_name (build_string ("etc"), dir);
514 etc_exists = Ffile_exists_p (tem);
515 if (!NILP (etc_exists))
517 Vinstallation_directory
518 = Ffile_name_as_directory (dir);
519 break;
523 /* See if dir's parent contains those subdirs. */
524 tem = Fexpand_file_name (build_string ("../lib-src"), dir);
525 lib_src_exists = Ffile_exists_p (tem);
528 #ifdef MSDOS
529 /* See the MSDOS commentary above. */
530 tem = Fexpand_file_name (build_string ("../info"), dir);
531 info_exists = Ffile_exists_p (tem);
532 #else
533 info_exists = Qnil;
534 #endif
536 if (!NILP (lib_src_exists) || !NILP (info_exists))
538 tem = Fexpand_file_name (build_string ("../etc"), dir);
539 etc_exists = Ffile_exists_p (tem);
540 if (!NILP (etc_exists))
542 tem = Fexpand_file_name (build_string (".."), dir);
543 Vinstallation_directory
544 = Ffile_name_as_directory (tem);
545 break;
549 /* If the Emacs executable is actually a link,
550 next try the dir that the link points into. */
551 tem = Ffile_symlink_p (name);
552 if (!NILP (tem))
554 name = Fexpand_file_name (tem, dir);
555 dir = Ffile_name_directory (name);
557 else
558 break;
562 Vcommand_line_args = Qnil;
564 for (i = argc - 1; i >= 0; i--)
566 if (i == 0 || i > skip_args)
567 /* For the moment, we keep arguments as is in unibyte strings.
568 They are decoded in the function command-line after we know
569 locale-coding-system. */
570 Vcommand_line_args
571 = Fcons (make_unibyte_string (argv[i], strlen (argv[i])),
572 Vcommand_line_args);
575 unbind_to (count, Qnil);
578 DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
579 doc: /* Return the program name that was used to run Emacs.
580 Any directory names are omitted. */)
581 (void)
583 return Fcopy_sequence (Vinvocation_name);
586 DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
587 0, 0, 0,
588 doc: /* Return the directory name in which the Emacs executable was located. */)
589 (void)
591 return Fcopy_sequence (Vinvocation_directory);
595 #ifdef HAVE_TZSET
596 /* A valid but unlikely value for the TZ environment value.
597 It is OK (though a bit slower) if the user actually chooses this value. */
598 static char dump_tz[] = "UtC0";
599 #endif
601 #ifndef ORDINARY_LINK
602 /* We don't include crtbegin.o and crtend.o in the link,
603 so these functions and variables might be missed.
604 Provide dummy definitions to avoid error.
605 (We don't have any real constructors or destructors.) */
606 #ifdef __GNUC__
607 #ifndef GCC_CTORS_IN_LIBC
608 void __do_global_ctors (void)
610 void __do_global_ctors_aux (void)
612 void __do_global_dtors (void)
614 /* GNU/Linux has a bug in its library; avoid an error. */
615 #ifndef GNU_LINUX
616 char * __CTOR_LIST__[2] = { (char *) (-1), 0 };
617 #endif
618 char * __DTOR_LIST__[2] = { (char *) (-1), 0 };
619 #endif /* GCC_CTORS_IN_LIBC */
620 void __main (void)
622 #endif /* __GNUC__ */
623 #endif /* ORDINARY_LINK */
625 /* Test whether the next argument in ARGV matches SSTR or a prefix of
626 LSTR (at least MINLEN characters). If so, then if VALPTR is non-null
627 (the argument is supposed to have a value) store in *VALPTR either
628 the next argument or the portion of this one after the equal sign.
629 ARGV is read starting at position *SKIPPTR; this index is advanced
630 by the number of arguments used.
632 Too bad we can't just use getopt for all of this, but we don't have
633 enough information to do it right. */
635 static int
636 argmatch (char **argv, int argc, const char *sstr, const char *lstr, int minlen, char **valptr, int *skipptr)
638 char *p = NULL;
639 int arglen;
640 char *arg;
642 /* Don't access argv[argc]; give up in advance. */
643 if (argc <= *skipptr + 1)
644 return 0;
646 arg = argv[*skipptr+1];
647 if (arg == NULL)
648 return 0;
649 if (strcmp (arg, sstr) == 0)
651 if (valptr != NULL)
653 *valptr = argv[*skipptr+2];
654 *skipptr += 2;
656 else
657 *skipptr += 1;
658 return 1;
660 arglen = (valptr != NULL && (p = strchr (arg, '=')) != NULL
661 ? p - arg : strlen (arg));
662 if (lstr == 0 || arglen < minlen || strncmp (arg, lstr, arglen) != 0)
663 return 0;
664 else if (valptr == NULL)
666 *skipptr += 1;
667 return 1;
669 else if (p != NULL)
671 *valptr = p+1;
672 *skipptr += 1;
673 return 1;
675 else if (argv[*skipptr+2] != NULL)
677 *valptr = argv[*skipptr+2];
678 *skipptr += 2;
679 return 1;
681 else
683 return 0;
687 #ifdef DOUG_LEA_MALLOC
689 /* malloc can be invoked even before main (e.g. by the dynamic
690 linker), so the dumped malloc state must be restored as early as
691 possible using this special hook. */
693 static void
694 malloc_initialize_hook (void)
696 #ifndef USE_CRT_DLL
697 extern char **environ;
698 #endif
700 if (initialized)
702 if (!malloc_using_checking)
703 /* Work around a bug in glibc's malloc. MALLOC_CHECK_ must be
704 ignored if the heap to be restored was constructed without
705 malloc checking. Can't use unsetenv, since that calls malloc. */
707 char **p;
709 for (p = environ; p && *p; p++)
710 if (strncmp (*p, "MALLOC_CHECK_=", 14) == 0)
713 *p = p[1];
714 while (*++p);
715 break;
719 malloc_set_state (malloc_state_ptr);
720 #ifndef XMALLOC_OVERRUN_CHECK
721 free (malloc_state_ptr);
722 #endif
724 else
726 if (my_heap_start == 0)
727 my_heap_start = sbrk (0);
728 malloc_using_checking = getenv ("MALLOC_CHECK_") != NULL;
732 void (*__malloc_initialize_hook) (void) = malloc_initialize_hook;
734 #endif /* DOUG_LEA_MALLOC */
737 /* ARGSUSED */
739 main (int argc, char **argv)
741 #if GC_MARK_STACK
742 Lisp_Object dummy;
743 #endif
744 char stack_bottom_variable;
745 int do_initial_setlocale;
746 int skip_args = 0;
747 #ifdef HAVE_SETRLIMIT
748 struct rlimit rlim;
749 #endif
750 int no_loadup = 0;
751 char *junk = 0;
752 char *dname_arg = 0;
753 #ifdef NS_IMPL_COCOA
754 char dname_arg2[80];
755 #endif
756 char *ch_to_dir;
758 #if GC_MARK_STACK
759 stack_base = &dummy;
760 #endif
762 #if defined (USE_GTK) && defined (G_SLICE_ALWAYS_MALLOC)
763 /* This is used by the Cygwin build. */
764 setenv ("G_SLICE", "always-malloc", 1);
765 #endif
767 if (!initialized)
769 extern char my_endbss[];
770 extern char *my_endbss_static;
772 if (my_heap_start == 0)
773 my_heap_start = sbrk (0);
775 heap_bss_diff = (char *)my_heap_start - max (my_endbss, my_endbss_static);
778 #ifdef RUN_TIME_REMAP
779 if (initialized)
780 run_time_remap (argv[0]);
781 #endif
783 /* If using unexmacosx.c (set by s/darwin.h), we must do this. */
784 #ifdef DARWIN_OS
785 if (!initialized)
786 unexec_init_emacs_zone ();
787 #endif
789 sort_args (argc, argv);
790 argc = 0;
791 while (argv[argc]) argc++;
793 if (argmatch (argv, argc, "-version", "--version", 3, NULL, &skip_args))
795 const char *version, *copyright;
796 if (initialized)
798 Lisp_Object tem, tem2;
799 tem = Fsymbol_value (intern_c_string ("emacs-version"));
800 tem2 = Fsymbol_value (intern_c_string ("emacs-copyright"));
801 if (!STRINGP (tem))
803 fprintf (stderr, "Invalid value of `emacs-version'\n");
804 exit (1);
806 if (!STRINGP (tem2))
808 fprintf (stderr, "Invalid value of `emacs-copyright'\n");
809 exit (1);
811 else
813 version = SDATA (tem);
814 copyright = SDATA (tem2);
817 else
819 version = emacs_version;
820 copyright = emacs_copyright;
822 printf ("GNU Emacs %s\n", version);
823 printf ("%s\n", copyright);
824 printf ("GNU Emacs comes with ABSOLUTELY NO WARRANTY.\n");
825 printf ("You may redistribute copies of Emacs\n");
826 printf ("under the terms of the GNU General Public License.\n");
827 printf ("For more information about these matters, ");
828 printf ("see the file named COPYING.\n");
829 exit (0);
831 if (argmatch (argv, argc, "-chdir", "--chdir", 2, &ch_to_dir, &skip_args))
832 if (chdir (ch_to_dir) == -1)
834 fprintf (stderr, "%s: Can't chdir to %s: %s\n",
835 argv[0], ch_to_dir, strerror (errno));
836 exit (1);
840 #ifdef HAVE_PERSONALITY_LINUX32
841 if (!initialized
842 && (strcmp (argv[argc-1], "dump") == 0
843 || strcmp (argv[argc-1], "bootstrap") == 0)
844 && ! getenv ("EMACS_HEAP_EXEC"))
846 static char heapexec[] = "EMACS_HEAP_EXEC=true";
847 /* Set this so we only do this once. */
848 putenv(heapexec);
850 /* A flag to turn off address randomization which is introduced
851 in linux kernel shipped with fedora core 4 */
852 #define ADD_NO_RANDOMIZE 0x0040000
853 personality (PER_LINUX32 | ADD_NO_RANDOMIZE);
854 #undef ADD_NO_RANDOMIZE
856 execvp (argv[0], argv);
858 /* If the exec fails, try to dump anyway. */
859 perror ("execvp");
861 #endif /* HAVE_PERSONALITY_LINUX32 */
863 #if defined (HAVE_SETRLIMIT) && defined (RLIMIT_STACK)
864 /* Extend the stack space available.
865 Don't do that if dumping, since some systems (e.g. DJGPP)
866 might define a smaller stack limit at that time. */
867 if (1
868 #ifndef CANNOT_DUMP
869 && (!noninteractive || initialized)
870 #endif
871 && !getrlimit (RLIMIT_STACK, &rlim))
873 long newlim;
874 extern size_t re_max_failures;
875 /* Approximate the amount regex.c needs per unit of re_max_failures. */
876 int ratio = 20 * sizeof (char *);
877 /* Then add 33% to cover the size of the smaller stacks that regex.c
878 successively allocates and discards, on its way to the maximum. */
879 ratio += ratio / 3;
880 /* Add in some extra to cover
881 what we're likely to use for other reasons. */
882 newlim = re_max_failures * ratio + 200000;
883 #ifdef __NetBSD__
884 /* NetBSD (at least NetBSD 1.2G and former) has a bug in its
885 stack allocation routine for new process that the allocation
886 fails if stack limit is not on page boundary. So, round up the
887 new limit to page boundary. */
888 newlim = (newlim + getpagesize () - 1) / getpagesize () * getpagesize();
889 #endif
890 if (newlim > rlim.rlim_max)
892 newlim = rlim.rlim_max;
893 /* Don't let regex.c overflow the stack we have. */
894 re_max_failures = (newlim - 200000) / ratio;
896 if (rlim.rlim_cur < newlim)
897 rlim.rlim_cur = newlim;
899 setrlimit (RLIMIT_STACK, &rlim);
901 #endif /* HAVE_SETRLIMIT and RLIMIT_STACK */
903 /* Record (approximately) where the stack begins. */
904 stack_bottom = &stack_bottom_variable;
906 clearerr (stdin);
908 #ifndef SYSTEM_MALLOC
909 /* Arrange to get warning messages as memory fills up. */
910 memory_warnings (0, malloc_warning);
912 /* Call malloc at least once, to run the initial __malloc_hook.
913 Also call realloc and free for consistency. */
914 free (realloc (malloc (4), 4));
916 # ifndef SYNC_INPUT
917 /* Arrange to disable interrupt input inside malloc etc. */
918 uninterrupt_malloc ();
919 # endif /* not SYNC_INPUT */
920 #endif /* not SYSTEM_MALLOC */
922 #ifdef FORWARD_SIGNAL_TO_MAIN_THREAD
923 main_thread = pthread_self ();
924 #endif /* FORWARD_SIGNAL_TO_MAIN_THREAD */
926 #if defined (MSDOS) || defined (WINDOWSNT)
927 /* We do all file input/output as binary files. When we need to translate
928 newlines, we do that manually. */
929 _fmode = O_BINARY;
930 #endif /* MSDOS || WINDOWSNT */
932 #ifdef MSDOS
933 if (!isatty (fileno (stdin)))
934 setmode (fileno (stdin), O_BINARY);
935 if (!isatty (fileno (stdout)))
937 fflush (stdout);
938 setmode (fileno (stdout), O_BINARY);
940 #endif /* MSDOS */
942 /* Skip initial setlocale if LC_ALL is "C", as it's not needed in that case.
943 The build procedure uses this while dumping, to ensure that the
944 dumped Emacs does not have its system locale tables initialized,
945 as that might cause screwups when the dumped Emacs starts up. */
947 char *lc_all = getenv ("LC_ALL");
948 do_initial_setlocale = ! lc_all || strcmp (lc_all, "C");
951 /* Set locale now, so that initial error messages are localized properly.
952 fixup_locale must wait until later, since it builds strings. */
953 if (do_initial_setlocale)
954 setlocale (LC_ALL, "");
956 inhibit_window_system = 0;
958 /* Handle the -t switch, which specifies filename to use as terminal. */
959 while (1)
961 char *term;
962 if (argmatch (argv, argc, "-t", "--terminal", 4, &term, &skip_args))
964 int result;
965 emacs_close (0);
966 emacs_close (1);
967 result = emacs_open (term, O_RDWR, 0);
968 if (result < 0)
970 char *errstring = strerror (errno);
971 fprintf (stderr, "%s: %s: %s\n", argv[0], term, errstring);
972 exit (1);
974 dup (0);
975 if (! isatty (0))
977 fprintf (stderr, "%s: %s: not a tty\n", argv[0], term);
978 exit (1);
980 fprintf (stderr, "Using %s\n", term);
981 #ifdef HAVE_WINDOW_SYSTEM
982 inhibit_window_system = 1; /* -t => -nw */
983 #endif
985 else
986 break;
989 /* Command line option --no-windows is deprecated and thus not mentioned
990 in the manual and usage informations. */
991 if (argmatch (argv, argc, "-nw", "--no-window-system", 6, NULL, &skip_args)
992 || argmatch (argv, argc, "-nw", "--no-windows", 6, NULL, &skip_args))
993 inhibit_window_system = 1;
995 /* Handle the -batch switch, which means don't do interactive display. */
996 noninteractive = 0;
997 if (argmatch (argv, argc, "-batch", "--batch", 5, NULL, &skip_args))
999 noninteractive = 1;
1000 Vundo_outer_limit = Qnil;
1002 if (argmatch (argv, argc, "-script", "--script", 3, &junk, &skip_args))
1004 noninteractive = 1; /* Set batch mode. */
1005 /* Convert --script to -scriptload, un-skip it, and sort again
1006 so that it will be handled in proper sequence. */
1007 /* FIXME broken for --script=FILE - is that supposed to work? */
1008 argv[skip_args - 1] = "-scriptload";
1009 skip_args -= 2;
1010 sort_args (argc, argv);
1013 /* Handle the --help option, which gives a usage message. */
1014 if (argmatch (argv, argc, "-help", "--help", 3, NULL, &skip_args))
1016 printf (USAGE1, argv[0], USAGE2);
1017 printf (USAGE3);
1018 printf (USAGE4);
1019 exit (0);
1022 if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)
1023 || argmatch (argv, argc, "-daemon", "--daemon", 5, &dname_arg, &skip_args))
1025 #ifndef DOS_NT
1026 pid_t f;
1028 /* Start as a daemon: fork a new child process which will run the
1029 rest of the initialization code, then exit.
1031 Detaching a daemon requires the following steps:
1032 - fork
1033 - setsid
1034 - exit the parent
1035 - close the tty file-descriptors
1037 We only want to do the last 2 steps once the daemon is ready to
1038 serve requests, i.e. after loading .emacs (initialization).
1039 OTOH initialization may start subprocesses (e.g. ispell) and these
1040 should be run from the proper process (the one that will end up
1041 running as daemon) and with the proper "session id" in order for
1042 them to keep working after detaching, so fork and setsid need to be
1043 performed before initialization.
1045 We want to avoid exiting before the server socket is ready, so
1046 use a pipe for synchronization. The parent waits for the child
1047 to close its end of the pipe (using `daemon-initialized')
1048 before exiting. */
1049 if (pipe (daemon_pipe) == -1)
1051 fprintf (stderr, "Cannot pipe!\n");
1052 exit (1);
1055 #ifndef NS_IMPL_COCOA
1056 f = fork ();
1057 #else /* NS_IMPL_COCOA */
1058 /* Under Cocoa we must do fork+exec as CoreFoundation lib fails in
1059 forked process: http://developer.apple.com/ReleaseNotes/
1060 CoreFoundation/CoreFoundation.html)
1061 We mark being in the exec'd process by a daemon name argument of
1062 form "--daemon=\nFD0,FD1\nNAME" where FD are the pipe file descriptors,
1063 NAME is the original daemon name, if any. */
1064 if (!dname_arg || !strchr (dname_arg, '\n'))
1065 f = fork (); /* in orig */
1066 else
1067 f = 0; /* in exec'd */
1068 #endif /* NS_IMPL_COCOA */
1069 if (f > 0)
1071 int retval;
1072 char buf[1];
1074 /* Close unused writing end of the pipe. */
1075 close (daemon_pipe[1]);
1077 /* Just wait for the child to close its end of the pipe. */
1080 retval = read (daemon_pipe[0], &buf, 1);
1082 while (retval == -1 && errno == EINTR);
1084 if (retval < 0)
1086 fprintf (stderr, "Error reading status from child\n");
1087 exit (1);
1089 else if (retval == 0)
1091 fprintf (stderr, "Error: server did not start correctly\n");
1092 exit (1);
1095 close (daemon_pipe[0]);
1096 exit (0);
1098 if (f < 0)
1100 fprintf (stderr, "Cannot fork!\n");
1101 exit (1);
1104 #ifdef NS_IMPL_COCOA
1106 /* In orig process, forked as child, OR in exec'd. */
1107 if (!dname_arg || !strchr (dname_arg, '\n'))
1108 { /* In orig, child: now exec w/special daemon name. */
1109 char fdStr[80];
1111 if (dname_arg && strlen (dname_arg) > 70)
1113 fprintf (stderr, "daemon: child name too long\n");
1114 exit (1);
1117 sprintf (fdStr, "--daemon=\n%d,%d\n%s", daemon_pipe[0],
1118 daemon_pipe[1], dname_arg ? dname_arg : "");
1119 argv[skip_args] = fdStr;
1121 execv (argv[0], argv);
1122 fprintf (stderr, "emacs daemon: exec failed: %d\n", errno);
1123 exit (1);
1126 /* In exec'd: parse special dname into pipe and name info. */
1127 if (!dname_arg || !strchr (dname_arg, '\n')
1128 || strlen (dname_arg) < 1 || strlen (dname_arg) > 70)
1130 fprintf (stderr, "emacs daemon: daemon name absent or too long\n");
1131 exit(1);
1133 dname_arg2[0] = '\0';
1134 sscanf (dname_arg, "\n%d,%d\n%s", &(daemon_pipe[0]), &(daemon_pipe[1]),
1135 dname_arg2);
1136 dname_arg = strlen (dname_arg2) ? dname_arg2 : NULL;
1138 #endif /* NS_IMPL_COCOA */
1140 if (dname_arg)
1141 daemon_name = xstrdup (dname_arg);
1142 /* Close unused reading end of the pipe. */
1143 close (daemon_pipe[0]);
1144 /* Make sure that the used end of the pipe is closed on exec, so
1145 that it is not accessible to programs started from .emacs. */
1146 fcntl (daemon_pipe[1], F_SETFD, FD_CLOEXEC);
1148 #ifdef HAVE_SETSID
1149 setsid();
1150 #endif
1151 #else /* DOS_NT */
1152 fprintf (stderr, "This platform does not support the -daemon flag.\n");
1153 exit (1);
1154 #endif /* DOS_NT */
1157 if (! noninteractive)
1159 #if defined (USG5) && defined (INTERRUPT_INPUT)
1160 setpgrp ();
1161 #endif
1162 #if defined (HAVE_GTK_AND_PTHREAD) && !defined (SYSTEM_MALLOC) && !defined (DOUG_LEA_MALLOC)
1164 extern void malloc_enable_thread (void);
1166 malloc_enable_thread ();
1168 #endif
1171 init_signals ();
1173 /* Don't catch SIGHUP if dumping. */
1174 if (1
1175 #ifndef CANNOT_DUMP
1176 && initialized
1177 #endif
1180 sigblock (sigmask (SIGHUP));
1181 /* In --batch mode, don't catch SIGHUP if already ignored.
1182 That makes nohup work. */
1183 if (! noninteractive
1184 || signal (SIGHUP, SIG_IGN) != SIG_IGN)
1185 signal (SIGHUP, fatal_error_signal);
1186 sigunblock (sigmask (SIGHUP));
1189 if (
1190 #ifndef CANNOT_DUMP
1191 ! noninteractive || initialized
1192 #else
1194 #endif
1197 /* Don't catch these signals in batch mode if dumping.
1198 On some machines, this sets static data that would make
1199 signal fail to work right when the dumped Emacs is run. */
1200 signal (SIGQUIT, fatal_error_signal);
1201 signal (SIGILL, fatal_error_signal);
1202 signal (SIGTRAP, fatal_error_signal);
1203 #ifdef SIGUSR1
1204 add_user_signal (SIGUSR1, "sigusr1");
1205 #endif
1206 #ifdef SIGUSR2
1207 add_user_signal (SIGUSR2, "sigusr2");
1208 #endif
1209 #ifdef SIGABRT
1210 signal (SIGABRT, fatal_error_signal);
1211 #endif
1212 #ifdef SIGHWE
1213 signal (SIGHWE, fatal_error_signal);
1214 #endif
1215 #ifdef SIGPRE
1216 signal (SIGPRE, fatal_error_signal);
1217 #endif
1218 #ifdef SIGORE
1219 signal (SIGORE, fatal_error_signal);
1220 #endif
1221 #ifdef SIGUME
1222 signal (SIGUME, fatal_error_signal);
1223 #endif
1224 #ifdef SIGDLK
1225 signal (SIGDLK, fatal_error_signal);
1226 #endif
1227 #ifdef SIGCPULIM
1228 signal (SIGCPULIM, fatal_error_signal);
1229 #endif
1230 #ifdef SIGIOT
1231 /* This is missing on some systems - OS/2, for example. */
1232 signal (SIGIOT, fatal_error_signal);
1233 #endif
1234 #ifdef SIGEMT
1235 signal (SIGEMT, fatal_error_signal);
1236 #endif
1237 signal (SIGFPE, fatal_error_signal);
1238 #ifdef SIGBUS
1239 signal (SIGBUS, fatal_error_signal);
1240 #endif
1241 signal (SIGSEGV, fatal_error_signal);
1242 #ifdef SIGSYS
1243 signal (SIGSYS, fatal_error_signal);
1244 #endif
1245 signal (SIGTERM, fatal_error_signal);
1246 #ifdef SIGXCPU
1247 signal (SIGXCPU, fatal_error_signal);
1248 #endif
1249 #ifdef SIGXFSZ
1250 signal (SIGXFSZ, fatal_error_signal);
1251 #endif /* SIGXFSZ */
1253 #ifdef SIGDANGER
1254 /* This just means available memory is getting low. */
1255 signal (SIGDANGER, memory_warning_signal);
1256 #endif
1258 #ifdef AIX
1259 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
1260 signal (SIGXCPU, fatal_error_signal);
1261 signal (SIGIOINT, fatal_error_signal);
1262 signal (SIGGRANT, fatal_error_signal);
1263 signal (SIGRETRACT, fatal_error_signal);
1264 signal (SIGSOUND, fatal_error_signal);
1265 signal (SIGMSG, fatal_error_signal);
1266 #endif /* AIX */
1269 noninteractive1 = noninteractive;
1271 /* Perform basic initializations (not merely interning symbols). */
1273 if (!initialized)
1275 init_alloc_once ();
1276 init_obarray ();
1277 init_eval_once ();
1278 init_character_once ();
1279 init_charset_once ();
1280 init_coding_once ();
1281 init_syntax_once (); /* Create standard syntax table. */
1282 init_category_once (); /* Create standard category table. */
1283 /* Must be done before init_buffer. */
1284 init_casetab_once ();
1285 init_buffer_once (); /* Create buffer table and some buffers. */
1286 init_minibuf_once (); /* Create list of minibuffers. */
1287 /* Must precede init_window_once. */
1289 /* Call syms_of_xfaces before init_window_once because that
1290 function creates Vterminal_frame. Termcap frames now use
1291 faces, and the face implementation uses some symbols as
1292 face names. */
1293 syms_of_xfaces ();
1294 /* XXX syms_of_keyboard uses some symbols in keymap.c. It would
1295 be better to arrange things not to have this dependency. */
1296 syms_of_keymap ();
1297 /* Call syms_of_keyboard before init_window_once because
1298 keyboard sets up symbols that include some face names that
1299 the X support will want to use. This can happen when
1300 CANNOT_DUMP is defined. */
1301 syms_of_keyboard ();
1303 /* Called before syms_of_fileio, because it sets up Qerror_condition. */
1304 syms_of_data ();
1305 syms_of_fileio ();
1306 /* Before syms_of_coding to initialize Vgc_cons_threshold. */
1307 syms_of_alloc ();
1308 /* Before syms_of_coding because it initializes Qcharsetp. */
1309 syms_of_charset ();
1310 /* Before init_window_once, because it sets up the
1311 Vcoding_system_hash_table. */
1312 syms_of_coding (); /* This should be after syms_of_fileio. */
1314 init_window_once (); /* Init the window system. */
1315 #ifdef HAVE_WINDOW_SYSTEM
1316 init_fringe_once (); /* Swap bitmaps if necessary. */
1317 #endif /* HAVE_WINDOW_SYSTEM */
1320 init_alloc ();
1322 if (do_initial_setlocale)
1324 fixup_locale ();
1325 Vsystem_messages_locale = Vprevious_system_messages_locale;
1326 Vsystem_time_locale = Vprevious_system_time_locale;
1329 init_eval ();
1330 init_data ();
1331 #ifdef CLASH_DETECTION
1332 init_filelock ();
1333 #endif
1334 init_atimer ();
1335 running_asynch_code = 0;
1337 no_loadup
1338 = argmatch (argv, argc, "-nl", "--no-loadup", 6, NULL, &skip_args);
1340 #ifdef HAVE_NS
1341 ns_alloc_autorelease_pool();
1342 if (!noninteractive)
1344 #ifdef NS_IMPL_COCOA
1345 if (skip_args < argc)
1347 if (!strncmp(argv[skip_args], "-psn", 4))
1349 skip_args += 1;
1350 chdir (getenv ("HOME"));
1352 else if (skip_args+1 < argc && !strncmp(argv[skip_args+1], "-psn", 4))
1354 skip_args += 2;
1355 chdir (getenv ("HOME"));
1358 #endif /* COCOA */
1360 #endif /* HAVE_NS */
1362 #ifdef HAVE_X_WINDOWS
1363 /* Stupid kludge to catch command-line display spec. We can't
1364 handle this argument entirely in window system dependent code
1365 because we don't even know which window system dependent code
1366 to run until we've recognized this argument. */
1368 char *displayname = 0;
1369 int count_before = skip_args;
1371 /* Skip any number of -d options, but only use the last one. */
1372 while (1)
1374 int count_before_this = skip_args;
1376 if (argmatch (argv, argc, "-d", "--display", 3, &displayname, &skip_args))
1377 display_arg = 1;
1378 else if (argmatch (argv, argc, "-display", 0, 3, &displayname, &skip_args))
1379 display_arg = 1;
1380 else
1381 break;
1383 count_before = count_before_this;
1386 /* If we have the form --display=NAME,
1387 convert it into -d name.
1388 This requires inserting a new element into argv. */
1389 if (displayname != 0 && skip_args - count_before == 1)
1391 char **new = (char **) xmalloc (sizeof (char *) * (argc + 2));
1392 int j;
1394 for (j = 0; j < count_before + 1; j++)
1395 new[j] = argv[j];
1396 new[count_before + 1] = "-d";
1397 new[count_before + 2] = displayname;
1398 for (j = count_before + 2; j <argc; j++)
1399 new[j + 1] = argv[j];
1400 argv = new;
1401 argc++;
1403 /* Change --display to -d, when its arg is separate. */
1404 else if (displayname != 0 && skip_args > count_before
1405 && argv[count_before + 1][1] == '-')
1406 argv[count_before + 1] = "-d";
1408 /* Don't actually discard this arg. */
1409 skip_args = count_before;
1411 #endif
1413 /* argmatch must not be used after here,
1414 except when bulding temacs
1415 because the -d argument has not been skipped in skip_args. */
1417 #ifdef MSDOS
1418 /* Call early 'cause init_environment needs it. */
1419 init_dosfns ();
1420 /* Set defaults for several environment variables. */
1421 if (initialized)
1422 init_environment (argc, argv, skip_args);
1423 else
1424 tzset ();
1425 #endif /* MSDOS */
1427 #ifdef WINDOWSNT
1428 globals_of_w32 ();
1429 /* Initialize environment from registry settings. */
1430 init_environment (argv);
1431 init_ntproc (); /* must precede init_editfns. */
1432 #endif
1434 #ifdef HAVE_NS
1435 #ifndef CANNOT_DUMP
1436 if (initialized)
1437 #endif
1438 ns_init_paths ();
1439 #endif
1441 /* Initialize and GC-protect Vinitial_environment and
1442 Vprocess_environment before set_initial_environment fills them
1443 in. */
1444 if (!initialized)
1445 syms_of_callproc ();
1446 /* egetenv is a pretty low-level facility, which may get called in
1447 many circumstances; it seems flimsy to put off initializing it
1448 until calling init_callproc. */
1449 set_initial_environment ();
1450 /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
1451 if this is not done. Do it after set_global_environment so that we
1452 don't pollute Vglobal_environment. */
1453 /* Setting LANG here will defeat the startup locale processing... */
1454 #ifdef AIX
1455 putenv ("LANG=C");
1456 #endif
1458 init_buffer (); /* Init default directory of main buffer. */
1460 init_callproc_1 (); /* Must precede init_cmdargs and init_sys_modes. */
1461 init_cmdargs (argc, argv, skip_args); /* Must precede init_lread. */
1463 if (initialized)
1465 /* Erase any pre-dump messages in the message log, to avoid confusion. */
1466 Lisp_Object old_log_max;
1467 old_log_max = Vmessage_log_max;
1468 XSETFASTINT (Vmessage_log_max, 0);
1469 message_dolog ("", 0, 1, 0);
1470 Vmessage_log_max = old_log_max;
1473 init_callproc (); /* Must follow init_cmdargs but not init_sys_modes. */
1474 init_lread ();
1476 /* Intern the names of all standard functions and variables;
1477 define standard keys. */
1479 if (!initialized)
1481 /* The basic levels of Lisp must come first. Note that
1482 syms_of_data and some others have already been called. */
1483 syms_of_chartab ();
1484 syms_of_lread ();
1485 syms_of_print ();
1486 syms_of_eval ();
1487 syms_of_fns ();
1488 syms_of_floatfns ();
1490 syms_of_buffer ();
1491 syms_of_bytecode ();
1492 syms_of_callint ();
1493 syms_of_casefiddle ();
1494 syms_of_casetab ();
1495 syms_of_category ();
1496 syms_of_ccl ();
1497 syms_of_character ();
1498 syms_of_cmds ();
1499 syms_of_dired ();
1500 syms_of_display ();
1501 syms_of_doc ();
1502 syms_of_editfns ();
1503 syms_of_emacs ();
1504 #ifdef CLASH_DETECTION
1505 syms_of_filelock ();
1506 #endif /* CLASH_DETECTION */
1507 syms_of_indent ();
1508 syms_of_insdel ();
1509 /* syms_of_keymap (); */
1510 syms_of_macros ();
1511 syms_of_marker ();
1512 syms_of_minibuf ();
1513 syms_of_process ();
1514 syms_of_search ();
1515 syms_of_frame ();
1516 syms_of_syntax ();
1517 syms_of_terminal ();
1518 syms_of_term ();
1519 syms_of_undo ();
1520 #ifdef HAVE_SOUND
1521 syms_of_sound ();
1522 #endif
1523 syms_of_textprop ();
1524 syms_of_composite ();
1525 #ifdef WINDOWSNT
1526 syms_of_ntproc ();
1527 #endif /* WINDOWSNT */
1528 syms_of_window ();
1529 syms_of_xdisp ();
1530 syms_of_font ();
1531 #ifdef HAVE_WINDOW_SYSTEM
1532 syms_of_fringe ();
1533 syms_of_image ();
1534 #endif /* HAVE_WINDOW_SYSTEM */
1535 #ifdef HAVE_X_WINDOWS
1536 syms_of_xterm ();
1537 syms_of_xfns ();
1538 syms_of_xmenu ();
1539 syms_of_fontset ();
1540 syms_of_xwidget();
1541 syms_of_xsettings ();
1542 #ifdef HAVE_X_SM
1543 syms_of_xsmfns ();
1544 #endif
1545 #ifdef HAVE_X11
1546 syms_of_xselect ();
1547 #endif
1548 #endif /* HAVE_X_WINDOWS */
1550 syms_of_menu ();
1552 #ifdef HAVE_NTGUI
1553 syms_of_w32term ();
1554 syms_of_w32fns ();
1555 syms_of_w32select ();
1556 syms_of_w32menu ();
1557 syms_of_fontset ();
1558 #endif /* HAVE_NTGUI */
1560 #ifdef MSDOS
1561 syms_of_xmenu ();
1562 syms_of_dosfns();
1563 syms_of_msdos();
1564 syms_of_win16select();
1565 #endif /* MSDOS */
1567 #ifdef HAVE_NS
1568 syms_of_nsterm ();
1569 syms_of_nsfns ();
1570 syms_of_nsmenu ();
1571 syms_of_nsselect ();
1572 syms_of_fontset ();
1573 #endif /* HAVE_NS */
1575 #ifdef HAVE_DBUS
1576 syms_of_dbusbind ();
1577 #endif /* HAVE_DBUS */
1579 #ifdef WINDOWSNT
1580 syms_of_ntterm ();
1581 #endif /* WINDOWSNT */
1583 keys_of_casefiddle ();
1584 keys_of_cmds ();
1585 keys_of_buffer ();
1586 keys_of_keyboard ();
1587 keys_of_keymap ();
1588 keys_of_window ();
1590 else
1592 /* Initialization that must be done even if the global variable
1593 initialized is non zero. */
1594 #ifdef HAVE_NTGUI
1595 globals_of_w32fns ();
1596 globals_of_w32menu ();
1597 globals_of_w32select ();
1598 #endif /* HAVE_NTGUI */
1601 init_charset ();
1603 init_editfns (); /* init_process uses Voperating_system_release. */
1604 init_process (); /* init_display uses add_keyboard_wait_descriptor. */
1605 init_keyboard (); /* This too must precede init_sys_modes. */
1606 if (!noninteractive)
1607 init_display (); /* Determine terminal type. Calls init_sys_modes. */
1608 init_fns ();
1609 init_xdisp ();
1610 #ifdef HAVE_WINDOW_SYSTEM
1611 init_fringe ();
1612 init_image ();
1613 #endif /* HAVE_WINDOW_SYSTEM */
1614 init_macros ();
1615 init_floatfns ();
1616 #ifdef HAVE_SOUND
1617 init_sound ();
1618 #endif
1619 init_window ();
1620 init_font ();
1622 if (!initialized)
1624 char *file;
1625 /* Handle -l loadup, args passed by Makefile. */
1626 if (argmatch (argv, argc, "-l", "--load", 3, &file, &skip_args))
1627 Vtop_level = Fcons (intern_c_string ("load"),
1628 Fcons (build_string (file), Qnil));
1629 /* Unless next switch is -nl, load "loadup.el" first thing. */
1630 if (! no_loadup)
1631 Vtop_level = Fcons (intern_c_string ("load"),
1632 Fcons (build_string ("loadup.el"), Qnil));
1635 if (initialized)
1637 #ifdef HAVE_TZSET
1639 /* If the execution TZ happens to be the same as the dump TZ,
1640 change it to some other value and then change it back,
1641 to force the underlying implementation to reload the TZ info.
1642 This is needed on implementations that load TZ info from files,
1643 since the TZ file contents may differ between dump and execution. */
1644 char *tz = getenv ("TZ");
1645 if (tz && !strcmp (tz, dump_tz))
1647 ++*tz;
1648 tzset ();
1649 --*tz;
1652 #endif
1655 /* Set up for profiling. This is known to work on FreeBSD,
1656 GNU/Linux and MinGW. It might work on some other systems too.
1657 Give it a try and tell us if it works on your system. To compile
1658 for profiling, use the configure option --enable-profiling. */
1659 #if defined (__FreeBSD__) || defined (GNU_LINUX) || defined(__MINGW32__)
1660 #ifdef PROFILING
1661 if (initialized)
1663 extern void _mcleanup ();
1664 #ifdef __MINGW32__
1665 extern unsigned char etext asm ("etext");
1666 #else
1667 extern char etext;
1668 #endif
1669 #ifdef HAVE___EXECUTABLE_START
1670 /* This symbol is defined by GNU ld to the start of the text
1671 segment. */
1672 extern char __executable_start[];
1673 #else
1674 extern void safe_bcopy ();
1675 #endif
1677 atexit (_mcleanup);
1678 #ifdef HAVE___EXECUTABLE_START
1679 monstartup (__executable_start, &etext);
1680 #else
1681 /* This uses safe_bcopy because that function comes first in the
1682 Emacs executable. It might be better to use something that
1683 gives the start of the text segment, but start_of_text is not
1684 defined on all systems now. */
1685 /* FIXME: Does not work on architectures with function
1686 descriptors. */
1687 monstartup (safe_bcopy, &etext);
1688 #endif
1690 else
1691 moncontrol (0);
1692 #endif
1693 #endif
1695 initialized = 1;
1697 #ifdef LOCALTIME_CACHE
1698 /* Some versions of localtime have a bug. They cache the value of the time
1699 zone rather than looking it up every time. Since localtime() is
1700 called to bolt the undumping time into the undumped emacs, this
1701 results in localtime ignoring the TZ environment variable.
1702 This flushes the new TZ value into localtime. */
1703 tzset ();
1704 #endif /* defined (LOCALTIME_CACHE) */
1706 /* Enter editor command loop. This never returns. */
1707 Frecursive_edit ();
1708 /* NOTREACHED */
1709 return 0;
1712 /* Sort the args so we can find the most important ones
1713 at the beginning of argv. */
1715 /* First, here's a table of all the standard options. */
1717 struct standard_args
1719 const char *name;
1720 const char *longname;
1721 int priority;
1722 int nargs;
1725 const struct standard_args standard_args[] =
1727 { "-version", "--version", 150, 0 },
1728 { "-chdir", "--chdir", 130, 1 },
1729 { "-t", "--terminal", 120, 1 },
1730 { "-nw", "--no-window-system", 110, 0 },
1731 { "-nw", "--no-windows", 110, 0 },
1732 { "-batch", "--batch", 100, 0 },
1733 { "-script", "--script", 100, 1 },
1734 { "-daemon", "--daemon", 99, 0 },
1735 { "-help", "--help", 90, 0 },
1736 { "-nl", "--no-loadup", 70, 0 },
1737 /* -d must come last before the options handled in startup.el. */
1738 { "-d", "--display", 60, 1 },
1739 { "-display", 0, 60, 1 },
1740 /* Now for the options handled in `command-line' (startup.el). */
1741 { "-Q", "--quick", 55, 0 },
1742 { "-quick", 0, 55, 0 },
1743 { "-q", "--no-init-file", 50, 0 },
1744 { "-no-init-file", 0, 50, 0 },
1745 { "-no-site-file", "--no-site-file", 40, 0 },
1746 { "-u", "--user", 30, 1 },
1747 { "-user", 0, 30, 1 },
1748 { "-debug-init", "--debug-init", 20, 0 },
1749 { "-iconic", "--iconic", 15, 0 },
1750 { "-D", "--basic-display", 12, 0},
1751 { "-basic-display", 0, 12, 0},
1752 { "-nbc", "--no-blinking-cursor", 12, 0 },
1753 /* Now for the options handled in `command-line-1' (startup.el). */
1754 { "-nbi", "--no-bitmap-icon", 10, 0 },
1755 { "-bg", "--background-color", 10, 1 },
1756 { "-background", 0, 10, 1 },
1757 { "-fg", "--foreground-color", 10, 1 },
1758 { "-foreground", 0, 10, 1 },
1759 { "-bd", "--border-color", 10, 1 },
1760 { "-bw", "--border-width", 10, 1 },
1761 { "-ib", "--internal-border", 10, 1 },
1762 { "-ms", "--mouse-color", 10, 1 },
1763 { "-cr", "--cursor-color", 10, 1 },
1764 { "-fn", "--font", 10, 1 },
1765 { "-font", 0, 10, 1 },
1766 { "-fs", "--fullscreen", 10, 0 },
1767 { "-fw", "--fullwidth", 10, 0 },
1768 { "-fh", "--fullheight", 10, 0 },
1769 { "-mm", "--maximized", 10, 0 },
1770 { "-g", "--geometry", 10, 1 },
1771 { "-geometry", 0, 10, 1 },
1772 { "-T", "--title", 10, 1 },
1773 { "-title", 0, 10, 1 },
1774 { "-name", "--name", 10, 1 },
1775 { "-xrm", "--xrm", 10, 1 },
1776 { "-parent-id", "--parent-id", 10, 1 },
1777 { "-r", "--reverse-video", 5, 0 },
1778 { "-rv", 0, 5, 0 },
1779 { "-reverse", 0, 5, 0 },
1780 { "-hb", "--horizontal-scroll-bars", 5, 0 },
1781 { "-vb", "--vertical-scroll-bars", 5, 0 },
1782 { "-color", "--color", 5, 0},
1783 { "-no-splash", "--no-splash", 3, 0 },
1784 { "-no-desktop", "--no-desktop", 3, 0 },
1785 #ifdef HAVE_NS
1786 { "-NSAutoLaunch", 0, 5, 1 },
1787 { "-NXAutoLaunch", 0, 5, 1 },
1788 { "-disable-font-backend", "--disable-font-backend", 65, 0 },
1789 { "-_NSMachLaunch", 0, 85, 1 },
1790 { "-MachLaunch", 0, 85, 1 },
1791 { "-macosx", 0, 85, 0 },
1792 { "-NSHost", 0, 85, 1 },
1793 #endif
1794 /* These have the same priority as ordinary file name args,
1795 so they are not reordered with respect to those. */
1796 { "-L", "--directory", 0, 1 },
1797 { "-directory", 0, 0, 1 },
1798 { "-l", "--load", 0, 1 },
1799 { "-load", 0, 0, 1 },
1800 /* This has no longname, because using --scriptload confuses sort_args,
1801 because then the --script long option seems to match twice; ie
1802 you can't have a long option which is a prefix of another long
1803 option. In any case, this is entirely an internal option. */
1804 { "-scriptload", NULL, 0, 1 },
1805 { "-f", "--funcall", 0, 1 },
1806 { "-funcall", 0, 0, 1 },
1807 { "-eval", "--eval", 0, 1 },
1808 { "-execute", "--execute", 0, 1 },
1809 { "-find-file", "--find-file", 0, 1 },
1810 { "-visit", "--visit", 0, 1 },
1811 { "-file", "--file", 0, 1 },
1812 { "-insert", "--insert", 0, 1 },
1813 #ifdef HAVE_NS
1814 { "-NXOpen", 0, 0, 1 },
1815 { "-NXOpenTemp", 0, 0, 1 },
1816 { "-NSOpen", 0, 0, 1 },
1817 { "-NSOpenTemp", 0, 0, 1 },
1818 { "-GSFilePath", 0, 0, 1 },
1819 #endif
1820 /* This should be processed after ordinary file name args and the like. */
1821 { "-kill", "--kill", -10, 0 },
1824 /* Reorder the elements of ARGV (assumed to have ARGC elements)
1825 so that the highest priority ones come first.
1826 Do not change the order of elements of equal priority.
1827 If an option takes an argument, keep it and its argument together.
1829 If an option that takes no argument appears more
1830 than once, eliminate all but one copy of it. */
1832 static void
1833 sort_args (int argc, char **argv)
1835 char **new = (char **) xmalloc (sizeof (char *) * argc);
1836 /* For each element of argv,
1837 the corresponding element of options is:
1838 0 for an option that takes no arguments,
1839 1 for an option that takes one argument, etc.
1840 -1 for an ordinary non-option argument. */
1841 int *options = (int *) xmalloc (sizeof (int) * argc);
1842 int *priority = (int *) xmalloc (sizeof (int) * argc);
1843 int to = 1;
1844 int incoming_used = 1;
1845 int from;
1846 int i;
1848 /* Categorize all the options,
1849 and figure out which argv elts are option arguments. */
1850 for (from = 1; from < argc; from++)
1852 options[from] = -1;
1853 priority[from] = 0;
1854 if (argv[from][0] == '-')
1856 int match, thislen;
1857 char *equals;
1859 /* If we have found "--", don't consider
1860 any more arguments as options. */
1861 if (argv[from][1] == '-' && argv[from][2] == 0)
1863 /* Leave the "--", and everything following it, at the end. */
1864 for (; from < argc; from++)
1866 priority[from] = -100;
1867 options[from] = -1;
1869 break;
1872 /* Look for a match with a known old-fashioned option. */
1873 for (i = 0; i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1874 if (!strcmp (argv[from], standard_args[i].name))
1876 options[from] = standard_args[i].nargs;
1877 priority[from] = standard_args[i].priority;
1878 if (from + standard_args[i].nargs >= argc)
1879 fatal ("Option `%s' requires an argument\n", argv[from]);
1880 from += standard_args[i].nargs;
1881 goto done;
1884 /* Look for a match with a known long option.
1885 MATCH is -1 if no match so far, -2 if two or more matches so far,
1886 >= 0 (the table index of the match) if just one match so far. */
1887 if (argv[from][1] == '-')
1889 match = -1;
1890 thislen = strlen (argv[from]);
1891 equals = strchr (argv[from], '=');
1892 if (equals != 0)
1893 thislen = equals - argv[from];
1895 for (i = 0;
1896 i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1897 if (standard_args[i].longname
1898 && !strncmp (argv[from], standard_args[i].longname,
1899 thislen))
1901 if (match == -1)
1902 match = i;
1903 else
1904 match = -2;
1907 /* If we found exactly one match, use that. */
1908 if (match >= 0)
1910 options[from] = standard_args[match].nargs;
1911 priority[from] = standard_args[match].priority;
1912 /* If --OPTION=VALUE syntax is used,
1913 this option uses just one argv element. */
1914 if (equals != 0)
1915 options[from] = 0;
1916 if (from + options[from] >= argc)
1917 fatal ("Option `%s' requires an argument\n", argv[from]);
1918 from += options[from];
1920 /* FIXME When match < 0, shouldn't there be some error,
1921 or at least indication to the user that there was a
1922 problem? */
1924 done: ;
1928 /* Copy the arguments, in order of decreasing priority, to NEW. */
1929 new[0] = argv[0];
1930 while (incoming_used < argc)
1932 int best = -1;
1933 int best_priority = -9999;
1935 /* Find the highest priority remaining option.
1936 If several have equal priority, take the first of them. */
1937 for (from = 1; from < argc; from++)
1939 if (argv[from] != 0 && priority[from] > best_priority)
1941 best_priority = priority[from];
1942 best = from;
1944 /* Skip option arguments--they are tied to the options. */
1945 if (options[from] > 0)
1946 from += options[from];
1949 if (best < 0)
1950 abort ();
1952 /* Copy the highest priority remaining option, with its args, to NEW.
1953 Unless it is a duplicate of the previous one. */
1954 if (! (options[best] == 0
1955 && ! strcmp (new[to - 1], argv[best])))
1957 new[to++] = argv[best];
1958 for (i = 0; i < options[best]; i++)
1959 new[to++] = argv[best + i + 1];
1962 incoming_used += 1 + (options[best] > 0 ? options[best] : 0);
1964 /* Clear out this option in ARGV. */
1965 argv[best] = 0;
1966 for (i = 0; i < options[best]; i++)
1967 argv[best + i + 1] = 0;
1970 /* If duplicate options were deleted, fill up extra space with null ptrs. */
1971 while (to < argc)
1972 new[to++] = 0;
1974 memcpy (argv, new, sizeof (char *) * argc);
1976 xfree (options);
1977 xfree (new);
1978 xfree (priority);
1981 DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
1982 doc: /* Exit the Emacs job and kill it.
1983 If ARG is an integer, return ARG as the exit program code.
1984 If ARG is a string, stuff it as keyboard input.
1986 The value of `kill-emacs-hook', if not void,
1987 is a list of functions (of no args),
1988 all of which are called before Emacs is actually killed. */)
1989 (Lisp_Object arg)
1991 struct gcpro gcpro1;
1993 GCPRO1 (arg);
1995 if (feof (stdin))
1996 arg = Qt;
1998 if (!NILP (Vrun_hooks) && !noninteractive)
1999 call1 (Vrun_hooks, intern ("kill-emacs-hook"));
2001 UNGCPRO;
2003 shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
2005 /* If we have an auto-save list file,
2006 kill it because we are exiting Emacs deliberately (not crashing).
2007 Do it after shut_down_emacs, which does an auto-save. */
2008 if (STRINGP (Vauto_save_list_file_name))
2009 unlink (SDATA (Vauto_save_list_file_name));
2011 exit (INTEGERP (arg) ? XINT (arg) : EXIT_SUCCESS);
2015 /* Perform an orderly shutdown of Emacs. Autosave any modified
2016 buffers, kill any child processes, clean up the terminal modes (if
2017 we're in the foreground), and other stuff like that. Don't perform
2018 any redisplay; this may be called when Emacs is shutting down in
2019 the background, or after its X connection has died.
2021 If SIG is a signal number, print a message for it.
2023 This is called by fatal signal handlers, X protocol error handlers,
2024 and Fkill_emacs. */
2026 void
2027 shut_down_emacs (int sig, int no_x, Lisp_Object stuff)
2029 /* Prevent running of hooks from now on. */
2030 Vrun_hooks = Qnil;
2032 /* Don't update display from now on. */
2033 Vinhibit_redisplay = Qt;
2035 /* If we are controlling the terminal, reset terminal modes. */
2036 #ifndef DOS_NT
2038 int pgrp = EMACS_GETPGRP (0);
2040 int tpgrp;
2041 if (EMACS_GET_TTY_PGRP (0, &tpgrp) != -1
2042 && tpgrp == pgrp)
2044 reset_all_sys_modes ();
2045 if (sig && sig != SIGTERM)
2046 fprintf (stderr, "Fatal error (%d)", sig);
2049 #else
2050 fflush (stdout);
2051 reset_all_sys_modes ();
2052 #endif
2054 stuff_buffered_input (stuff);
2056 inhibit_sentinels = 1;
2057 kill_buffer_processes (Qnil);
2058 Fdo_auto_save (Qt, Qnil);
2060 #ifdef CLASH_DETECTION
2061 unlock_all_files ();
2062 #endif
2064 #if 0 /* This triggers a bug in XCloseDisplay and is not needed. */
2065 #ifdef HAVE_X_WINDOWS
2066 /* It's not safe to call intern here. Maybe we are crashing. */
2067 if (!noninteractive && SYMBOLP (Vinitial_window_system)
2068 && SCHARS (SYMBOL_NAME (Vinitial_window_system)) == 1
2069 && SREF (SYMBOL_NAME (Vinitial_window_system), 0) == 'x'
2070 && ! no_x)
2071 Fx_close_current_connection ();
2072 #endif /* HAVE_X_WINDOWS */
2073 #endif
2075 #ifdef SIGIO
2076 /* There is a tendency for a SIGIO signal to arrive within exit,
2077 and cause a SIGHUP because the input descriptor is already closed. */
2078 unrequest_sigio ();
2079 signal (SIGIO, SIG_IGN);
2080 #endif
2082 #ifdef WINDOWSNT
2083 term_ntproc ();
2084 #endif
2086 /* Do this only if terminating normally, we want glyph matrices
2087 etc. in a core dump. */
2088 if (sig == 0 || sig == SIGTERM)
2090 check_glyph_memory ();
2091 check_message_stack ();
2094 #ifdef MSDOS
2095 dos_cleanup ();
2096 #endif
2098 #ifdef HAVE_NS
2099 ns_term_shutdown (sig);
2100 #endif
2105 #ifndef CANNOT_DUMP
2107 DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
2108 doc: /* Dump current state of Emacs into executable file FILENAME.
2109 Take symbols from SYMFILE (presumably the file you executed to run Emacs).
2110 This is used in the file `loadup.el' when building Emacs.
2112 You must run Emacs in batch mode in order to dump it. */)
2113 (Lisp_Object filename, Lisp_Object symfile)
2115 extern char my_edata[];
2116 Lisp_Object tem;
2117 Lisp_Object symbol;
2118 int count = SPECPDL_INDEX ();
2120 check_pure_size ();
2122 if (! noninteractive)
2123 error ("Dumping Emacs works only in batch mode");
2125 #ifdef GNU_LINUX
2126 if (heap_bss_diff > MAX_HEAP_BSS_DIFF)
2128 fprintf (stderr, "**************************************************\n");
2129 fprintf (stderr, "Warning: Your system has a gap between BSS and the\n");
2130 fprintf (stderr, "heap (%lu bytes). This usually means that exec-shield\n",
2131 heap_bss_diff);
2132 fprintf (stderr, "or something similar is in effect. The dump may\n");
2133 fprintf (stderr, "fail because of this. See the section about\n");
2134 fprintf (stderr, "exec-shield in etc/PROBLEMS for more information.\n");
2135 fprintf (stderr, "**************************************************\n");
2137 #endif /* GNU_LINUX */
2139 /* Bind `command-line-processed' to nil before dumping,
2140 so that the dumped Emacs will process its command line
2141 and set up to work with X windows if appropriate. */
2142 symbol = intern ("command-line-processed");
2143 specbind (symbol, Qnil);
2145 CHECK_STRING (filename);
2146 filename = Fexpand_file_name (filename, Qnil);
2147 if (!NILP (symfile))
2149 CHECK_STRING (symfile);
2150 if (SCHARS (symfile))
2151 symfile = Fexpand_file_name (symfile, Qnil);
2154 tem = Vpurify_flag;
2155 Vpurify_flag = Qnil;
2157 #ifdef HAVE_TZSET
2158 set_time_zone_rule (dump_tz);
2159 #ifndef LOCALTIME_CACHE
2160 /* Force a tz reload, since set_time_zone_rule doesn't. */
2161 tzset ();
2162 #endif
2163 #endif
2165 fflush (stdout);
2166 /* Tell malloc where start of impure now is. */
2167 /* Also arrange for warnings when nearly out of space. */
2168 #ifndef SYSTEM_MALLOC
2169 #ifndef WINDOWSNT
2170 /* On Windows, this was done before dumping, and that once suffices.
2171 Meanwhile, my_edata is not valid on Windows. */
2172 memory_warnings (my_edata, malloc_warning);
2173 #endif /* not WINDOWSNT */
2174 #endif
2175 #if !defined (SYSTEM_MALLOC) && defined (HAVE_GTK_AND_PTHREAD) && !defined SYNC_INPUT
2176 /* Pthread may call malloc before main, and then we will get an endless
2177 loop, because pthread_self (see alloc.c) calls malloc the first time
2178 it is called on some systems. */
2179 reset_malloc_hooks ();
2180 #endif
2181 #ifdef DOUG_LEA_MALLOC
2182 malloc_state_ptr = malloc_get_state ();
2183 #endif
2185 #ifdef USE_MMAP_FOR_BUFFERS
2186 mmap_set_vars (0);
2187 #endif
2188 unexec (SDATA (filename),
2189 !NILP (symfile) ? SDATA (symfile) : 0, my_edata, 0, 0);
2190 #ifdef USE_MMAP_FOR_BUFFERS
2191 mmap_set_vars (1);
2192 #endif
2193 #ifdef DOUG_LEA_MALLOC
2194 free (malloc_state_ptr);
2195 #endif
2197 Vpurify_flag = tem;
2199 return unbind_to (count, Qnil);
2202 #endif /* not CANNOT_DUMP */
2204 #if HAVE_SETLOCALE
2205 /* Recover from setlocale (LC_ALL, ""). */
2206 void
2207 fixup_locale (void)
2209 /* The Emacs Lisp reader needs LC_NUMERIC to be "C",
2210 so that numbers are read and printed properly for Emacs Lisp. */
2211 setlocale (LC_NUMERIC, "C");
2214 /* Set system locale CATEGORY, with previous locale *PLOCALE, to
2215 DESIRED_LOCALE. */
2216 static void
2217 synchronize_locale (int category, Lisp_Object *plocale, Lisp_Object desired_locale)
2219 if (! EQ (*plocale, desired_locale))
2221 *plocale = desired_locale;
2222 setlocale (category, (STRINGP (desired_locale)
2223 ? (char *) SDATA (desired_locale)
2224 : ""));
2228 /* Set system time locale to match Vsystem_time_locale, if possible. */
2229 void
2230 synchronize_system_time_locale (void)
2232 synchronize_locale (LC_TIME, &Vprevious_system_time_locale,
2233 Vsystem_time_locale);
2236 /* Set system messages locale to match Vsystem_messages_locale, if
2237 possible. */
2238 void
2239 synchronize_system_messages_locale (void)
2241 #ifdef LC_MESSAGES
2242 synchronize_locale (LC_MESSAGES, &Vprevious_system_messages_locale,
2243 Vsystem_messages_locale);
2244 #endif
2246 #endif /* HAVE_SETLOCALE */
2248 #ifndef SEPCHAR
2249 #define SEPCHAR ':'
2250 #endif
2252 Lisp_Object
2253 decode_env_path (const char *evarname, const char *defalt)
2255 const char *path, *p;
2256 Lisp_Object lpath, element, tem;
2258 /* It's okay to use getenv here, because this function is only used
2259 to initialize variables when Emacs starts up, and isn't called
2260 after that. */
2261 if (evarname != 0)
2262 path = getenv (evarname);
2263 else
2264 path = 0;
2265 if (!path)
2266 path = defalt;
2267 #ifdef DOS_NT
2268 /* Ensure values from the environment use the proper directory separator. */
2269 if (path)
2271 char *path_copy = alloca (strlen (path) + 1);
2272 strcpy (path_copy, path);
2273 dostounix_filename (path_copy);
2274 path = path_copy;
2276 #endif
2277 lpath = Qnil;
2278 while (1)
2280 p = strchr (path, SEPCHAR);
2281 if (!p)
2282 p = path + strlen (path);
2283 element = (p - path ? make_string (path, p - path)
2284 : build_string ("."));
2286 /* Add /: to the front of the name
2287 if it would otherwise be treated as magic. */
2288 tem = Ffind_file_name_handler (element, Qt);
2290 /* However, if the handler says "I'm safe",
2291 don't bother adding /:. */
2292 if (SYMBOLP (tem))
2294 Lisp_Object prop;
2295 prop = Fget (tem, intern ("safe-magic"));
2296 if (! NILP (prop))
2297 tem = Qnil;
2300 if (! NILP (tem))
2301 element = concat2 (build_string ("/:"), element);
2303 lpath = Fcons (element, lpath);
2304 if (*p)
2305 path = p + 1;
2306 else
2307 break;
2309 return Fnreverse (lpath);
2312 DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0,
2313 doc: /* Return non-nil if the current emacs process is a daemon.
2314 If the daemon was given a name argument, return that name. */)
2315 (void)
2317 if (IS_DAEMON)
2318 if (daemon_name)
2319 return build_string (daemon_name);
2320 else
2321 return Qt;
2322 else
2323 return Qnil;
2326 DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0,
2327 doc: /* Mark the Emacs daemon as being initialized.
2328 This finishes the daemonization process by doing the other half of detaching
2329 from the parent process and its tty file descriptors. */)
2330 (void)
2332 int nfd;
2334 if (!IS_DAEMON)
2335 error ("This function can only be called if emacs is run as a daemon");
2337 if (daemon_pipe[1] < 0)
2338 error ("The daemon has already been initialized");
2340 if (NILP (Vafter_init_time))
2341 error ("This function can only be called after loading the init files");
2343 /* Get rid of stdin, stdout and stderr. */
2344 nfd = open ("/dev/null", O_RDWR);
2345 dup2 (nfd, 0);
2346 dup2 (nfd, 1);
2347 dup2 (nfd, 2);
2348 close (nfd);
2350 /* Closing the pipe will notify the parent that it can exit.
2351 FIXME: In case some other process inherited the pipe, closing it here
2352 won't notify the parent because it's still open elsewhere, so we
2353 additionally send a byte, just to make sure the parent really exits.
2354 Instead, we should probably close the pipe in start-process and
2355 call-process to make sure the pipe is never inherited by
2356 subprocesses. */
2357 write (daemon_pipe[1], "\n", 1);
2358 close (daemon_pipe[1]);
2359 /* Set it to an invalid value so we know we've already run this function. */
2360 daemon_pipe[1] = -1;
2361 return Qt;
2364 void
2365 syms_of_emacs (void)
2367 Qfile_name_handler_alist = intern_c_string ("file-name-handler-alist");
2368 staticpro (&Qfile_name_handler_alist);
2370 #ifndef CANNOT_DUMP
2371 defsubr (&Sdump_emacs);
2372 #endif
2374 defsubr (&Skill_emacs);
2376 defsubr (&Sinvocation_name);
2377 defsubr (&Sinvocation_directory);
2378 defsubr (&Sdaemonp);
2379 defsubr (&Sdaemon_initialized);
2381 DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
2382 doc: /* Args passed by shell to Emacs, as a list of strings.
2383 Many arguments are deleted from the list as they are processed. */);
2385 DEFVAR_LISP ("system-type", &Vsystem_type,
2386 doc: /* The value is a symbol indicating the type of operating system you are using.
2387 Special values:
2388 `gnu' compiled for a GNU Hurd system.
2389 `gnu/linux' compiled for a GNU/Linux system.
2390 `gnu/kfreebsd' compiled for a GNU system with a FreeBSD kernel.
2391 `darwin' compiled for Darwin (GNU-Darwin, Mac OS X, ...).
2392 `ms-dos' compiled as an MS-DOS application.
2393 `windows-nt' compiled as a native W32 application.
2394 `cygwin' compiled using the Cygwin library.
2395 Anything else (in Emacs 23.1, the possibilities are: aix, berkeley-unix,
2396 hpux, irix, lynxos 3.0.1, usg-unix-v) indicates some sort of Unix system. */);
2397 Vsystem_type = intern_c_string (SYSTEM_TYPE);
2399 DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
2400 doc: /* Value is string indicating configuration Emacs was built for.
2401 On MS-Windows, the value reflects the OS flavor and version on which
2402 Emacs is running. */);
2403 Vsystem_configuration = build_string (EMACS_CONFIGURATION);
2405 DEFVAR_LISP ("system-configuration-options", &Vsystem_configuration_options,
2406 doc: /* String containing the configuration options Emacs was built with. */);
2407 Vsystem_configuration_options = build_string (EMACS_CONFIG_OPTIONS);
2409 DEFVAR_BOOL ("noninteractive", &noninteractive1,
2410 doc: /* Non-nil means Emacs is running without interactive terminal. */);
2412 DEFVAR_LISP ("kill-emacs-hook", &Vkill_emacs_hook,
2413 doc: /* Hook to be run when `kill-emacs' is called.
2414 Since `kill-emacs' may be invoked when the terminal is disconnected (or
2415 in other similar situations), functions placed on this hook should not
2416 expect to be able to interact with the user. To ask for confirmation,
2417 see `kill-emacs-query-functions' instead.
2419 The hook is not run in batch mode, i.e., if `noninteractive' is non-nil. */);
2420 Vkill_emacs_hook = Qnil;
2422 DEFVAR_INT ("emacs-priority", &emacs_priority,
2423 doc: /* Priority for Emacs to run at.
2424 This value is effective only if set before Emacs is dumped,
2425 and only if the Emacs executable is installed with setuid to permit
2426 it to change priority. (Emacs sets its uid back to the real uid.)
2427 Currently, you need to define SET_EMACS_PRIORITY in `config.h'
2428 before you compile Emacs, to enable the code for this feature. */);
2429 emacs_priority = 0;
2431 DEFVAR_LISP ("path-separator", &Vpath_separator,
2432 doc: /* String containing the character that separates directories in
2433 search paths, such as PATH and other similar environment variables. */);
2435 char c = SEPCHAR;
2436 Vpath_separator = make_string (&c, 1);
2439 DEFVAR_LISP ("invocation-name", &Vinvocation_name,
2440 doc: /* The program name that was used to run Emacs.
2441 Any directory names are omitted. */);
2443 DEFVAR_LISP ("invocation-directory", &Vinvocation_directory,
2444 doc: /* The directory in which the Emacs executable was found, to run it.
2445 The value is nil if that directory's name is not known. */);
2447 DEFVAR_LISP ("installation-directory", &Vinstallation_directory,
2448 doc: /* A directory within which to look for the `lib-src' and `etc' directories.
2449 This is non-nil when we can't find those directories in their standard
2450 installed locations, but we can find them near where the Emacs executable
2451 was found. */);
2452 Vinstallation_directory = Qnil;
2454 DEFVAR_LISP ("system-messages-locale", &Vsystem_messages_locale,
2455 doc: /* System locale for messages. */);
2456 Vsystem_messages_locale = Qnil;
2458 DEFVAR_LISP ("previous-system-messages-locale",
2459 &Vprevious_system_messages_locale,
2460 doc: /* Most recently used system locale for messages. */);
2461 Vprevious_system_messages_locale = Qnil;
2463 DEFVAR_LISP ("system-time-locale", &Vsystem_time_locale,
2464 doc: /* System locale for time. */);
2465 Vsystem_time_locale = Qnil;
2467 DEFVAR_LISP ("previous-system-time-locale", &Vprevious_system_time_locale,
2468 doc: /* Most recently used system locale for time. */);
2469 Vprevious_system_time_locale = Qnil;
2471 DEFVAR_LISP ("before-init-time", &Vbefore_init_time,
2472 doc: /* Value of `current-time' before Emacs begins initialization. */);
2473 Vbefore_init_time = Qnil;
2475 DEFVAR_LISP ("after-init-time", &Vafter_init_time,
2476 doc: /* Value of `current-time' after loading the init files.
2477 This is nil during initialization. */);
2478 Vafter_init_time = Qnil;
2480 DEFVAR_BOOL ("inhibit-x-resources", &inhibit_x_resources,
2481 doc: /* If non-nil, X resources, Windows Registry settings, and NS defaults are not used. */);
2482 inhibit_x_resources = 0;
2484 DEFVAR_LISP ("emacs-copyright", &Vemacs_copyright,
2485 doc: /* Short copyright string for this version of Emacs. */);
2486 Vemacs_copyright = build_string (emacs_copyright);
2488 DEFVAR_LISP ("emacs-version", &Vemacs_version,
2489 doc: /* Version numbers of this version of Emacs. */);
2490 Vemacs_version = build_string (emacs_version);
2492 /* Make sure IS_DAEMON starts up as false. */
2493 daemon_pipe[1] = 0;
2496 /* arch-tag: 7bfd356a-c720-4612-8ab6-aa4222931c2e
2497 (do not change this comment) */