*** empty log message ***
[bpt/emacs.git] / src / emacs.c
1 /* Fully extensible Emacs, running on Unix, intended for GNU.
2 Copyright (C) 1985,86,87,93,94,95,97,98,1999,2001,02,03,2004
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include <config.h>
24 #include <signal.h>
25 #include <errno.h>
26 #include <stdio.h>
27
28 #include <sys/types.h>
29 #include <sys/file.h>
30
31 #ifdef VMS
32 #include <ssdef.h>
33 #endif
34
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #ifdef BSD_SYSTEM
40 #include <sys/ioctl.h>
41 #endif
42
43 #ifdef WINDOWSNT
44 #include <fcntl.h>
45 #endif
46
47 #include "lisp.h"
48 #include "commands.h"
49 #include "intervals.h"
50 #include "buffer.h"
51 #include "window.h"
52
53 #include "systty.h"
54 #include "blockinput.h"
55 #include "syssignal.h"
56 #include "process.h"
57 #include "termhooks.h"
58 #include "keyboard.h"
59 #include "keymap.h"
60
61 #ifdef HAVE_SETLOCALE
62 #include <locale.h>
63 #endif
64
65 #ifdef HAVE_SETRLIMIT
66 #include <sys/time.h>
67 #include <sys/resource.h>
68 #endif
69
70 #ifndef O_RDWR
71 #define O_RDWR 2
72 #endif
73
74 #ifdef HAVE_SETPGID
75 #if !defined (USG) || defined (BSD_PGRPS)
76 #undef setpgrp
77 #define setpgrp setpgid
78 #endif
79 #endif
80
81 extern void malloc_warning P_ ((char *));
82 extern void set_time_zone_rule P_ ((char *));
83 #ifdef HAVE_INDEX
84 extern char *index P_ ((const char *, int));
85 #endif
86
87 /* Make these values available in GDB, which doesn't see macros. */
88
89 #ifdef USE_LSB_TAG
90 int gdb_use_lsb = 1;
91 #else
92 int gdb_use_lsb = 0;
93 #endif
94 #ifdef NO_UNION_TYPE
95 int gdb_use_union = 0;
96 #else
97 int gdb_use_union = 1;
98 #endif
99 EMACS_INT gdb_valbits = VALBITS;
100 EMACS_INT gdb_gctypebits = GCTYPEBITS;
101 #ifdef DATA_SEG_BITS
102 EMACS_INT gdb_data_seg_bits = DATA_SEG_BITS;
103 #else
104 EMACS_INT gdb_data_seg_bits = 0;
105 #endif
106 EMACS_INT PVEC_FLAG = PSEUDOVECTOR_FLAG;
107 EMACS_INT gdb_array_mark_flag = ARRAY_MARK_FLAG;
108
109 /* Command line args from shell, as list of strings. */
110 Lisp_Object Vcommand_line_args;
111
112 /* The name under which Emacs was invoked, with any leading directory
113 names discarded. */
114 Lisp_Object Vinvocation_name;
115
116 /* The directory name from which Emacs was invoked. */
117 Lisp_Object Vinvocation_directory;
118
119 /* The directory name in which to find subdirs such as lisp and etc.
120 nil means get them only from PATH_LOADSEARCH. */
121 Lisp_Object Vinstallation_directory;
122
123 /* Hook run by `kill-emacs' before it does really anything. */
124 Lisp_Object Vkill_emacs_hook;
125
126 /* An empty lisp string. To avoid having to build any other. */
127 Lisp_Object empty_string;
128
129 /* Search path separator. */
130 Lisp_Object Vpath_separator;
131
132 /* Set nonzero after Emacs has started up the first time.
133 Prevents reinitialization of the Lisp world and keymaps
134 on subsequent starts. */
135 int initialized;
136
137 #ifdef DOUG_LEA_MALLOC
138 /* Preserves a pointer to the memory allocated that copies that
139 static data inside glibc's malloc. */
140 void *malloc_state_ptr;
141 /* From glibc, a routine that returns a copy of the malloc internal state. */
142 extern void *malloc_get_state ();
143 /* From glibc, a routine that overwrites the malloc internal state. */
144 extern void malloc_set_state ();
145 /* Non-zero if the MALLOC_CHECK_ enviroment variable was set while
146 dumping. Used to work around a bug in glibc's malloc. */
147 int malloc_using_checking;
148 #endif
149
150 /* Variable whose value is symbol giving operating system type. */
151 Lisp_Object Vsystem_type;
152
153 /* Variable whose value is string giving configuration built for. */
154 Lisp_Object Vsystem_configuration;
155
156 /* Variable whose value is string giving configuration options,
157 for use when reporting bugs. */
158 Lisp_Object Vsystem_configuration_options;
159
160 Lisp_Object Qfile_name_handler_alist;
161
162 /* Current and previous system locales for messages and time. */
163 Lisp_Object Vsystem_messages_locale;
164 Lisp_Object Vprevious_system_messages_locale;
165 Lisp_Object Vsystem_time_locale;
166 Lisp_Object Vprevious_system_time_locale;
167
168 /* If non-zero, emacs should not attempt to use a window-specific code,
169 but instead should use the virtual terminal under which it was started. */
170 int inhibit_window_system;
171
172 /* If nonzero, set Emacs to run at this priority. This is also used
173 in child_setup and sys_suspend to make sure subshells run at normal
174 priority; those functions have their own extern declaration. */
175 EMACS_INT emacs_priority;
176
177 /* If non-zero, a filter or a sentinel is running. Tested to save the match
178 data on the first attempt to change it inside asynchronous code. */
179 int running_asynch_code;
180
181 #ifdef BSD_PGRPS
182 /* See sysdep.c. */
183 extern int inherited_pgroup;
184 #endif
185
186 #ifdef HAVE_X_WINDOWS
187 /* If non-zero, -d was specified, meaning we're using some window system. */
188 int display_arg;
189 #endif
190
191 /* An address near the bottom of the stack.
192 Tells GC how to save a copy of the stack. */
193 char *stack_bottom;
194
195 #ifdef HAVE_WINDOW_SYSTEM
196 extern Lisp_Object Vwindow_system;
197 #endif /* HAVE_WINDOW_SYSTEM */
198
199 extern Lisp_Object Vauto_save_list_file_name;
200
201 extern Lisp_Object Vinhibit_redisplay;
202
203 #ifdef USG_SHARED_LIBRARIES
204 /* If nonzero, this is the place to put the end of the writable segment
205 at startup. */
206
207 unsigned int bss_end = 0;
208 #endif
209
210 /* Nonzero means running Emacs without interactive terminal. */
211
212 int noninteractive;
213
214 /* Value of Lisp variable `noninteractive'.
215 Normally same as C variable `noninteractive'
216 but nothing terrible happens if user sets this one. */
217
218 int noninteractive1;
219
220 /* Save argv and argc. */
221 char **initial_argv;
222 int initial_argc;
223
224 static void sort_args ();
225 void syms_of_emacs ();
226
227 /* MSVC needs each string be shorter than 2048 bytes, so the usage
228 strings below are split to not overflow this limit. */
229 #define USAGE1 "\
230 Usage: %s [OPTION-OR-FILENAME]...\n\
231 \n\
232 Run Emacs, the extensible, customizable, self-documenting real-time\n\
233 display editor. The recommended way to start Emacs for normal editing\n\
234 is with no options at all.\n\
235 \n\
236 Run M-x info RET m emacs RET m command arguments RET inside Emacs to\n\
237 read the main documentation for these command-line arguments.\n\
238 \n\
239 Initialization options:\n\
240 \n\
241 --batch do not do interactive display; implies -q\n\
242 --debug-init enable Emacs Lisp debugger for init file\n\
243 --display, -d DISPLAY use X server DISPLAY\n\
244 --multibyte, --no-unibyte inhibit the effect of EMACS_UNIBYTE\n\
245 --no-desktop do not load a saved desktop\n\
246 --no-init-file, -q load neither ~/.emacs nor default.el\n\
247 --no-shared-memory, -nl do not use shared memory\n\
248 --no-site-file do not load site-start.el\n\
249 --no-splash do not display a splash screen on startup\n\
250 --no-window-system, -nw don't communicate with X, ignoring $DISPLAY\n\
251 --script FILE run FILE as an Emacs Lisp script\n\
252 --terminal, -t DEVICE use DEVICE for terminal I/O\n\
253 --unibyte, --no-multibyte run Emacs in unibyte mode\n\
254 --user, -u USER load ~USER/.emacs instead of your own\n\
255 \n%s"
256
257 #define USAGE2 "\
258 Action options:\n\
259 \n\
260 FILE visit FILE using find-file\n\
261 +LINE FILE visit FILE using find-file, then go to line LINE\n\
262 +LINE:COLUMN FILE visit FILE using find-file, then go to line LINE,\n\
263 column COLUMN\n\
264 --directory, -L DIR add DIR to variable load-path\n\
265 --eval EXPR evaluate Emacs Lisp expression EXPR\n\
266 --execute EXPR evaluate Emacs Lisp expression EXPR\n\
267 --file FILE visit FILE using find-file\n\
268 --find-file FILE visit FILE using find-file\n\
269 --funcall, -f FUNC call Emacs Lisp function FUNC with no arguments\n\
270 --insert FILE insert contents of FILE into current buffer\n\
271 --kill exit without asking for confirmation\n\
272 --load, -l FILE load Emacs Lisp FILE using the load function\n\
273 --visit FILE visit FILE using find-file\n\
274 \n"
275
276 #define USAGE3 "\
277 Display options:\n\
278 \n\
279 --background-color, -bg COLOR window background color\n\
280 --border-color, -bd COLOR main border color\n\
281 --border-width, -bw WIDTH width of main border\n\
282 --color MODE color mode for character terminals;\n\
283 MODE defaults to `auto', and can also\n\
284 be `never', `auto', `always',\n\
285 or a mode name like `ansi8'\n\
286 --cursor-color, -cr COLOR color of the Emacs cursor indicating point\n\
287 --font, -fn FONT default font; must be fixed-width\n\
288 --foreground-color, -fg COLOR window foreground color\n\
289 --fullheight, -fh make the first frame high as the screen\n\
290 --fullscreen, -fs make first frame fullscreen\n\
291 --fullwidth, -fw make the first frame wide as the screen\n\
292 --geometry, -g GEOMETRY window geometry\n\
293 --horizontal-scroll-bars, -hb enable horizontal scroll bars\n\
294 --icon-type, -i use picture of gnu for Emacs icon\n\
295 --iconic start Emacs in iconified state\n\
296 --internal-border, -ib WIDTH width between text and main border\n\
297 --line-spacing, -lsp PIXELS additional space to put between lines\n\
298 --mouse-color, -ms COLOR mouse cursor color in Emacs window\n\
299 --name NAME title for initial Emacs frame\n\
300 --reverse-video, -r, -rv switch foreground and background\n\
301 --title, -T TITLE title for initial Emacs frame\n\
302 --vertical-scroll-bars, -vb enable vertical scroll bars\n\
303 --xrm XRESOURCES set additional X resources\n\
304 --help display this help and exit\n\
305 --version output version information and exit\n\
306 \n\
307 You can generally also specify long option names with a single -; for\n\
308 example, -batch as well as --batch. You can use any unambiguous\n\
309 abbreviation for a --option.\n\
310 \n\
311 Various environment variables and window system resources also affect\n\
312 Emacs' operation. See the main documentation.\n\
313 \n"
314
315 #define USAGE4 "\
316 Report bugs to %s. First, please see the Bugs\n\
317 section of the Emacs manual or the file BUGS.\n"
318
319 \f
320 /* Signal code for the fatal signal that was received. */
321 int fatal_error_code;
322
323 /* Nonzero if handling a fatal error already. */
324 int fatal_error_in_progress;
325
326 /* If non-null, call this function from fatal_error_signal before
327 committing suicide. */
328
329 void (*fatal_error_signal_hook) P_ ((void));
330
331
332 #ifdef SIGUSR1
333 SIGTYPE
334 handle_USR1_signal (sig)
335 int sig;
336 {
337 struct input_event buf;
338
339 bzero (&buf, sizeof buf);
340 buf.kind = USER_SIGNAL_EVENT;
341 buf.frame_or_window = selected_frame;
342
343 kbd_buffer_store_event (&buf);
344 }
345 #endif /* SIGUSR1 */
346
347 #ifdef SIGUSR2
348 SIGTYPE
349 handle_USR2_signal (sig)
350 int sig;
351 {
352 struct input_event buf;
353
354 bzero (&buf, sizeof buf);
355 buf.kind = USER_SIGNAL_EVENT;
356 buf.code = 1;
357 buf.frame_or_window = selected_frame;
358
359 kbd_buffer_store_event (&buf);
360 }
361 #endif /* SIGUSR2 */
362
363 /* Handle bus errors, invalid instruction, etc. */
364 SIGTYPE
365 fatal_error_signal (sig)
366 int sig;
367 {
368 fatal_error_code = sig;
369 signal (sig, SIG_DFL);
370
371 TOTALLY_UNBLOCK_INPUT;
372
373 /* If fatal error occurs in code below, avoid infinite recursion. */
374 if (! fatal_error_in_progress)
375 {
376 fatal_error_in_progress = 1;
377
378 shut_down_emacs (sig, 0, Qnil);
379 }
380
381 #ifdef VMS
382 LIB$STOP (SS$_ABORT);
383 #else
384 /* Signal the same code; this time it will really be fatal.
385 Remember that since we're in a signal handler, the signal we're
386 going to send is probably blocked, so we have to unblock it if we
387 want to really receive it. */
388 #ifndef MSDOS
389 sigunblock (sigmask (fatal_error_code));
390 #endif
391
392 if (fatal_error_signal_hook)
393 fatal_error_signal_hook ();
394
395 kill (getpid (), fatal_error_code);
396 #endif /* not VMS */
397 }
398
399 #ifdef SIGDANGER
400
401 /* Handler for SIGDANGER. */
402 SIGTYPE
403 memory_warning_signal (sig)
404 int sig;
405 {
406 signal (sig, memory_warning_signal);
407
408 malloc_warning ("Operating system warns that virtual memory is running low.\n");
409
410 /* It might be unsafe to call do_auto_save now. */
411 force_auto_save_soon ();
412 }
413 #endif
414
415 /* We define abort, rather than using it from the library,
416 so that GDB can return from a breakpoint here.
417 MSDOS has its own definition in msdos.c. */
418
419 #if ! defined (DOS_NT) && ! defined (NO_ABORT)
420
421 #ifndef ABORT_RETURN_TYPE
422 #define ABORT_RETURN_TYPE void
423 #endif
424
425 ABORT_RETURN_TYPE
426 abort ()
427 {
428 kill (getpid (), SIGABRT);
429 /* This shouldn't be executed, but it prevents a warning. */
430 exit (1);
431 }
432 #endif
433
434 \f
435 /* Code for dealing with Lisp access to the Unix command line. */
436
437 static void
438 init_cmdargs (argc, argv, skip_args)
439 int argc;
440 char **argv;
441 int skip_args;
442 {
443 register int i;
444 Lisp_Object name, dir, tem;
445 int count = SPECPDL_INDEX ();
446 Lisp_Object raw_name;
447
448 initial_argv = argv;
449 initial_argc = argc;
450
451 raw_name = build_string (argv[0]);
452
453 /* Add /: to the front of the name
454 if it would otherwise be treated as magic. */
455 tem = Ffind_file_name_handler (raw_name, Qt);
456 if (! NILP (tem))
457 raw_name = concat2 (build_string ("/:"), raw_name);
458
459 Vinvocation_name = Ffile_name_nondirectory (raw_name);
460 Vinvocation_directory = Ffile_name_directory (raw_name);
461
462 /* If we got no directory in argv[0], search PATH to find where
463 Emacs actually came from. */
464 if (NILP (Vinvocation_directory))
465 {
466 Lisp_Object found;
467 int yes = openp (Vexec_path, Vinvocation_name,
468 Vexec_suffixes, &found, make_number (X_OK));
469 if (yes == 1)
470 {
471 /* Add /: to the front of the name
472 if it would otherwise be treated as magic. */
473 tem = Ffind_file_name_handler (found, Qt);
474 if (! NILP (tem))
475 found = concat2 (build_string ("/:"), found);
476 Vinvocation_directory = Ffile_name_directory (found);
477 }
478 }
479
480 if (!NILP (Vinvocation_directory)
481 && NILP (Ffile_name_absolute_p (Vinvocation_directory)))
482 /* Emacs was started with relative path, like ./emacs.
483 Make it absolute. */
484 Vinvocation_directory = Fexpand_file_name (Vinvocation_directory, Qnil);
485
486 Vinstallation_directory = Qnil;
487
488 if (!NILP (Vinvocation_directory))
489 {
490 dir = Vinvocation_directory;
491 name = Fexpand_file_name (Vinvocation_name, dir);
492 while (1)
493 {
494 Lisp_Object tem, lib_src_exists;
495 Lisp_Object etc_exists, info_exists;
496
497 /* See if dir contains subdirs for use by Emacs.
498 Check for the ones that would exist in a build directory,
499 not including lisp and info. */
500 tem = Fexpand_file_name (build_string ("lib-src"), dir);
501 lib_src_exists = Ffile_exists_p (tem);
502
503 #ifdef MSDOS
504 /* MSDOS installations frequently remove lib-src, but we still
505 must set installation-directory, or else info won't find
506 its files (it uses the value of installation-directory). */
507 tem = Fexpand_file_name (build_string ("info"), dir);
508 info_exists = Ffile_exists_p (tem);
509 #else
510 info_exists = Qnil;
511 #endif
512
513 if (!NILP (lib_src_exists) || !NILP (info_exists))
514 {
515 tem = Fexpand_file_name (build_string ("etc"), dir);
516 etc_exists = Ffile_exists_p (tem);
517 if (!NILP (etc_exists))
518 {
519 Vinstallation_directory
520 = Ffile_name_as_directory (dir);
521 break;
522 }
523 }
524
525 /* See if dir's parent contains those subdirs. */
526 tem = Fexpand_file_name (build_string ("../lib-src"), dir);
527 lib_src_exists = Ffile_exists_p (tem);
528
529
530 #ifdef MSDOS
531 /* See the MSDOS commentary above. */
532 tem = Fexpand_file_name (build_string ("../info"), dir);
533 info_exists = Ffile_exists_p (tem);
534 #else
535 info_exists = Qnil;
536 #endif
537
538 if (!NILP (lib_src_exists) || !NILP (info_exists))
539 {
540 tem = Fexpand_file_name (build_string ("../etc"), dir);
541 etc_exists = Ffile_exists_p (tem);
542 if (!NILP (etc_exists))
543 {
544 tem = Fexpand_file_name (build_string (".."), dir);
545 Vinstallation_directory
546 = Ffile_name_as_directory (tem);
547 break;
548 }
549 }
550
551 /* If the Emacs executable is actually a link,
552 next try the dir that the link points into. */
553 tem = Ffile_symlink_p (name);
554 if (!NILP (tem))
555 {
556 name = Fexpand_file_name (tem, dir);
557 dir = Ffile_name_directory (name);
558 }
559 else
560 break;
561 }
562 }
563
564 Vcommand_line_args = Qnil;
565
566 for (i = argc - 1; i >= 0; i--)
567 {
568 if (i == 0 || i > skip_args)
569 Vcommand_line_args
570 = Fcons (build_string (argv[i]), Vcommand_line_args);
571 }
572
573 unbind_to (count, Qnil);
574 }
575
576 DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
577 doc: /* Return the program name that was used to run Emacs.
578 Any directory names are omitted. */)
579 ()
580 {
581 return Fcopy_sequence (Vinvocation_name);
582 }
583
584 DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
585 0, 0, 0,
586 doc: /* Return the directory name in which the Emacs executable was located. */)
587 ()
588 {
589 return Fcopy_sequence (Vinvocation_directory);
590 }
591
592 \f
593 #ifdef VMS
594 #ifdef LINK_CRTL_SHARE
595 #ifdef SHARABLE_LIB_BUG
596 extern noshare char **environ;
597 #endif /* SHARABLE_LIB_BUG */
598 #endif /* LINK_CRTL_SHARE */
599 #endif /* VMS */
600
601 #ifdef HAVE_TZSET
602 /* A valid but unlikely value for the TZ environment value.
603 It is OK (though a bit slower) if the user actually chooses this value. */
604 static char dump_tz[] = "UtC0";
605 #endif
606
607 #ifndef ORDINARY_LINK
608 /* We don't include crtbegin.o and crtend.o in the link,
609 so these functions and variables might be missed.
610 Provide dummy definitions to avoid error.
611 (We don't have any real constructors or destructors.) */
612 #ifdef __GNUC__
613 #ifndef GCC_CTORS_IN_LIBC
614 void __do_global_ctors ()
615 {}
616 void __do_global_ctors_aux ()
617 {}
618 void __do_global_dtors ()
619 {}
620 /* GNU/Linux has a bug in its library; avoid an error. */
621 #ifndef GNU_LINUX
622 char * __CTOR_LIST__[2] = { (char *) (-1), 0 };
623 #endif
624 char * __DTOR_LIST__[2] = { (char *) (-1), 0 };
625 #endif /* GCC_CTORS_IN_LIBC */
626 void __main ()
627 {}
628 #endif /* __GNUC__ */
629 #endif /* ORDINARY_LINK */
630
631 /* Test whether the next argument in ARGV matches SSTR or a prefix of
632 LSTR (at least MINLEN characters). If so, then if VALPTR is non-null
633 (the argument is supposed to have a value) store in *VALPTR either
634 the next argument or the portion of this one after the equal sign.
635 ARGV is read starting at position *SKIPPTR; this index is advanced
636 by the number of arguments used.
637
638 Too bad we can't just use getopt for all of this, but we don't have
639 enough information to do it right. */
640
641 static int
642 argmatch (argv, argc, sstr, lstr, minlen, valptr, skipptr)
643 char **argv;
644 int argc;
645 char *sstr;
646 char *lstr;
647 int minlen;
648 char **valptr;
649 int *skipptr;
650 {
651 char *p = NULL;
652 int arglen;
653 char *arg;
654
655 /* Don't access argv[argc]; give up in advance. */
656 if (argc <= *skipptr + 1)
657 return 0;
658
659 arg = argv[*skipptr+1];
660 if (arg == NULL)
661 return 0;
662 if (strcmp (arg, sstr) == 0)
663 {
664 if (valptr != NULL)
665 {
666 *valptr = argv[*skipptr+2];
667 *skipptr += 2;
668 }
669 else
670 *skipptr += 1;
671 return 1;
672 }
673 arglen = (valptr != NULL && (p = index (arg, '=')) != NULL
674 ? p - arg : strlen (arg));
675 if (lstr == 0 || arglen < minlen || strncmp (arg, lstr, arglen) != 0)
676 return 0;
677 else if (valptr == NULL)
678 {
679 *skipptr += 1;
680 return 1;
681 }
682 else if (p != NULL)
683 {
684 *valptr = p+1;
685 *skipptr += 1;
686 return 1;
687 }
688 else if (argv[*skipptr+2] != NULL)
689 {
690 *valptr = argv[*skipptr+2];
691 *skipptr += 2;
692 return 1;
693 }
694 else
695 {
696 return 0;
697 }
698 }
699
700 #ifdef DOUG_LEA_MALLOC
701
702 /* malloc can be invoked even before main (e.g. by the dynamic
703 linker), so the dumped malloc state must be restored as early as
704 possible using this special hook. */
705
706 static void
707 malloc_initialize_hook ()
708 {
709 #ifndef USE_CRT_DLL
710 extern char **environ;
711 #endif
712
713 if (initialized)
714 {
715 if (!malloc_using_checking)
716 /* Work around a bug in glibc's malloc. MALLOC_CHECK_ must be
717 ignored if the heap to be restored was constructed without
718 malloc checking. Can't use unsetenv, since that calls malloc. */
719 {
720 char **p;
721
722 for (p = environ; p && *p; p++)
723 if (strncmp (*p, "MALLOC_CHECK_=", 14) == 0)
724 {
725 do
726 *p = p[1];
727 while (*++p);
728 break;
729 }
730 }
731
732 malloc_set_state (malloc_state_ptr);
733 free (malloc_state_ptr);
734 }
735 else
736 malloc_using_checking = getenv ("MALLOC_CHECK_") != NULL;
737 }
738
739 void (*__malloc_initialize_hook) () = malloc_initialize_hook;
740
741 #endif /* DOUG_LEA_MALLOC */
742
743
744 #define REPORT_EMACS_BUG_ADDRESS "bug-gnu-emacs@gnu.org"
745 #define REPORT_EMACS_BUG_PRETEST_ADDRESS "emacs-pretest-bug@gnu.org"
746
747 /* This function is used to determine an address to which bug report should
748 be sent. */
749
750 char *
751 bug_reporting_address ()
752 {
753 int count = 0;
754 Lisp_Object temp;
755 char *string;
756
757 temp = Fsymbol_value (intern ("emacs-version"));
758
759 /* When `emacs-version' is invalid, use normal address. */
760 if (!STRINGP(temp))
761 return REPORT_EMACS_BUG_ADDRESS;
762
763 string = SDATA (temp);
764
765 /* Count dots in `emacs-version'. */
766 while (*string)
767 {
768 if (*string == '.')
769 count++;
770 string++;
771 }
772
773 /* When `emacs-version' has at least three dots, it is development or
774 pretest version of Emacs. */
775 return count >= 3 ? REPORT_EMACS_BUG_PRETEST_ADDRESS : REPORT_EMACS_BUG_ADDRESS;
776 }
777
778
779 /* ARGSUSED */
780 int
781 main (argc, argv
782 #ifdef VMS
783 , envp
784 #endif
785 )
786 int argc;
787 char **argv;
788 #ifdef VMS
789 char **envp;
790 #endif
791 {
792 #if GC_MARK_STACK
793 Lisp_Object dummy;
794 #endif
795 char stack_bottom_variable;
796 int do_initial_setlocale;
797 int skip_args = 0;
798 #ifndef USE_CRT_DLL
799 extern int errno;
800 #endif
801 #ifdef HAVE_SETRLIMIT
802 struct rlimit rlim;
803 #endif
804 int no_loadup = 0;
805 char *junk = 0;
806
807 #if GC_MARK_STACK
808 extern Lisp_Object *stack_base;
809 stack_base = &dummy;
810 #endif
811
812 #ifdef LINUX_SBRK_BUG
813 __sbrk (1);
814 #endif
815
816 #ifdef RUN_TIME_REMAP
817 if (initialized)
818 run_time_remap (argv[0]);
819 #endif
820
821 #ifdef MAC_OSX
822 if (!initialized)
823 unexec_init_emacs_zone ();
824 #endif
825
826 sort_args (argc, argv);
827 argc = 0;
828 while (argv[argc]) argc++;
829
830 if (argmatch (argv, argc, "-version", "--version", 3, NULL, &skip_args)
831 /* We don't know the version number unless this is a dumped Emacs.
832 So ignore --version otherwise. */
833 && initialized)
834 {
835 Lisp_Object tem;
836 tem = Fsymbol_value (intern ("emacs-version"));
837 if (!STRINGP (tem))
838 {
839 fprintf (stderr, "Invalid value of `emacs-version'\n");
840 exit (1);
841 }
842 else
843 {
844 printf ("GNU Emacs %s\n", SDATA (tem));
845 printf ("Copyright (C) 2004 Free Software Foundation, Inc.\n");
846 printf ("GNU Emacs comes with ABSOLUTELY NO WARRANTY.\n");
847 printf ("You may redistribute copies of Emacs\n");
848 printf ("under the terms of the GNU General Public License.\n");
849 printf ("For more information about these matters, ");
850 printf ("see the file named COPYING.\n");
851 exit (0);
852 }
853 }
854
855 /* Map in shared memory, if we are using that. */
856 #ifdef HAVE_SHM
857 if (argmatch (argv, argc, "-nl", "--no-shared-memory", 6, NULL, &skip_args))
858 {
859 map_in_data (0);
860 /* The shared memory was just restored, which clobbered this. */
861 skip_args = 1;
862 }
863 else
864 {
865 map_in_data (1);
866 /* The shared memory was just restored, which clobbered this. */
867 skip_args = 0;
868 }
869 #endif
870
871 #ifdef NeXT
872 {
873 extern int malloc_cookie;
874 /* This helps out unexnext.c. */
875 if (initialized)
876 if (malloc_jumpstart (malloc_cookie) != 0)
877 printf ("malloc jumpstart failed!\n");
878 }
879 #endif /* NeXT */
880
881 #ifdef MAC_OSX
882 /* Skip process serial number passed in the form -psn_x_y as
883 command-line argument. */
884 if (argc > skip_args + 1 && strncmp (argv[skip_args+1], "-psn_", 5) == 0)
885 skip_args++;
886 #endif /* MAC_OSX */
887
888 #ifdef VMS
889 /* If -map specified, map the data file in. */
890 {
891 char *file;
892 if (argmatch (argv, argc, "-map", "--map-data", 3, &file, &skip_args))
893 mapin_data (file);
894 }
895
896 #ifdef LINK_CRTL_SHARE
897 #ifdef SHARABLE_LIB_BUG
898 /* Bletcherous shared libraries! */
899 if (!stdin)
900 stdin = fdopen (0, "r");
901 if (!stdout)
902 stdout = fdopen (1, "w");
903 if (!stderr)
904 stderr = fdopen (2, "w");
905 if (!environ)
906 environ = envp;
907 #endif /* SHARABLE_LIB_BUG */
908 #endif /* LINK_CRTL_SHARE */
909 #endif /* VMS */
910
911 #if defined (HAVE_SETRLIMIT) && defined (RLIMIT_STACK)
912 /* Extend the stack space available.
913 Don't do that if dumping, since some systems (e.g. DJGPP)
914 might define a smaller stack limit at that time. */
915 if (1
916 #ifndef CANNOT_DUMP
917 && (!noninteractive || initialized)
918 #endif
919 && !getrlimit (RLIMIT_STACK, &rlim))
920 {
921 long newlim;
922 extern int re_max_failures;
923 /* Approximate the amount regex.c needs per unit of re_max_failures. */
924 int ratio = 20 * sizeof (char *);
925 /* Then add 33% to cover the size of the smaller stacks that regex.c
926 successively allocates and discards, on its way to the maximum. */
927 ratio += ratio / 3;
928 /* Add in some extra to cover
929 what we're likely to use for other reasons. */
930 newlim = re_max_failures * ratio + 200000;
931 #ifdef __NetBSD__
932 /* NetBSD (at least NetBSD 1.2G and former) has a bug in its
933 stack allocation routine for new process that the allocation
934 fails if stack limit is not on page boundary. So, round up the
935 new limit to page boundary. */
936 newlim = (newlim + getpagesize () - 1) / getpagesize () * getpagesize();
937 #endif
938 if (newlim > rlim.rlim_max)
939 {
940 newlim = rlim.rlim_max;
941 /* Don't let regex.c overflow the stack we have. */
942 re_max_failures = (newlim - 200000) / ratio;
943 }
944 if (rlim.rlim_cur < newlim)
945 rlim.rlim_cur = newlim;
946
947 setrlimit (RLIMIT_STACK, &rlim);
948 }
949 #endif /* HAVE_SETRLIMIT and RLIMIT_STACK */
950
951 /* Record (approximately) where the stack begins. */
952 stack_bottom = &stack_bottom_variable;
953
954 #ifdef USG_SHARED_LIBRARIES
955 if (bss_end)
956 brk ((void *)bss_end);
957 #endif
958
959 clearerr (stdin);
960
961 #ifndef SYSTEM_MALLOC
962 /* Arrange to get warning messages as memory fills up. */
963 memory_warnings (0, malloc_warning);
964
965 /* Call malloc at least once, to run the initial __malloc_hook.
966 Also call realloc and free for consistency. */
967 free (realloc (malloc (4), 4));
968
969 /* Arrange to disable interrupt input inside malloc etc. */
970 uninterrupt_malloc ();
971 #endif /* not SYSTEM_MALLOC */
972
973 #if defined (MSDOS) || defined (WINDOWSNT)
974 /* We do all file input/output as binary files. When we need to translate
975 newlines, we do that manually. */
976 _fmode = O_BINARY;
977 #endif /* MSDOS || WINDOWSNT */
978
979 #ifdef MSDOS
980 #if __DJGPP__ >= 2
981 if (!isatty (fileno (stdin)))
982 setmode (fileno (stdin), O_BINARY);
983 if (!isatty (fileno (stdout)))
984 {
985 fflush (stdout);
986 setmode (fileno (stdout), O_BINARY);
987 }
988 #else /* not __DJGPP__ >= 2 */
989 (stdin)->_flag &= ~_IOTEXT;
990 (stdout)->_flag &= ~_IOTEXT;
991 (stderr)->_flag &= ~_IOTEXT;
992 #endif /* not __DJGPP__ >= 2 */
993 #endif /* MSDOS */
994
995 #ifdef SET_EMACS_PRIORITY
996 if (emacs_priority)
997 nice (emacs_priority);
998 setuid (getuid ());
999 #endif /* SET_EMACS_PRIORITY */
1000
1001 /* Skip initial setlocale if LC_ALL is "C", as it's not needed in that case.
1002 The build procedure uses this while dumping, to ensure that the
1003 dumped Emacs does not have its system locale tables initialized,
1004 as that might cause screwups when the dumped Emacs starts up. */
1005 {
1006 char *lc_all = getenv ("LC_ALL");
1007 do_initial_setlocale = ! lc_all || strcmp (lc_all, "C");
1008 }
1009
1010 /* Set locale now, so that initial error messages are localized properly.
1011 fixup_locale must wait until later, since it builds strings. */
1012 if (do_initial_setlocale)
1013 setlocale (LC_ALL, "");
1014
1015 #ifdef EXTRA_INITIALIZE
1016 EXTRA_INITIALIZE;
1017 #endif
1018
1019 inhibit_window_system = 0;
1020
1021 /* Handle the -t switch, which specifies filename to use as terminal. */
1022 while (1)
1023 {
1024 char *term;
1025 if (argmatch (argv, argc, "-t", "--terminal", 4, &term, &skip_args))
1026 {
1027 int result;
1028 emacs_close (0);
1029 emacs_close (1);
1030 result = emacs_open (term, O_RDWR, 0);
1031 if (result < 0)
1032 {
1033 char *errstring = strerror (errno);
1034 fprintf (stderr, "%s: %s: %s\n", argv[0], term, errstring);
1035 exit (1);
1036 }
1037 dup (0);
1038 if (! isatty (0))
1039 {
1040 fprintf (stderr, "%s: %s: not a tty\n", argv[0], term);
1041 exit (1);
1042 }
1043 fprintf (stderr, "Using %s\n", term);
1044 #ifdef HAVE_WINDOW_SYSTEM
1045 inhibit_window_system = 1; /* -t => -nw */
1046 #endif
1047 }
1048 else
1049 break;
1050 }
1051
1052 /* Command line option --no-windows is deprecated and thus not mentioned
1053 in the manual and usage informations. */
1054 if (argmatch (argv, argc, "-nw", "--no-window-system", 6, NULL, &skip_args)
1055 || argmatch (argv, argc, "-nw", "--no-windows", 6, NULL, &skip_args))
1056 inhibit_window_system = 1;
1057
1058 /* Handle the -batch switch, which means don't do interactive display. */
1059 noninteractive = 0;
1060 if (argmatch (argv, argc, "-batch", "--batch", 5, NULL, &skip_args))
1061 noninteractive = 1;
1062 if (argmatch (argv, argc, "-script", "--script", 3, &junk, &skip_args))
1063 {
1064 noninteractive = 1; /* Set batch mode. */
1065 /* Convert --script to -l, un-skip it, and sort again so that -l will be
1066 handled in proper sequence. */
1067 argv[skip_args - 1] = "-l";
1068 skip_args -= 2;
1069 sort_args (argc, argv);
1070 }
1071
1072 /* Handle the --help option, which gives a usage message. */
1073 if (argmatch (argv, argc, "-help", "--help", 3, NULL, &skip_args))
1074 {
1075 printf (USAGE1, argv[0], USAGE2);
1076 printf (USAGE3);
1077 printf (USAGE4, bug_reporting_address ());
1078 exit (0);
1079 }
1080
1081 if (! noninteractive)
1082 {
1083 #ifdef BSD_PGRPS
1084 if (initialized)
1085 {
1086 inherited_pgroup = EMACS_GETPGRP (0);
1087 setpgrp (0, getpid ());
1088 }
1089 #else
1090 #if defined (USG5) && defined (INTERRUPT_INPUT)
1091 setpgrp ();
1092 #endif
1093 #endif
1094 }
1095
1096 init_signals ();
1097
1098 /* Don't catch SIGHUP if dumping. */
1099 if (1
1100 #ifndef CANNOT_DUMP
1101 && initialized
1102 #endif
1103 )
1104 {
1105 sigblock (sigmask (SIGHUP));
1106 /* In --batch mode, don't catch SIGHUP if already ignored.
1107 That makes nohup work. */
1108 if (! noninteractive
1109 || signal (SIGHUP, SIG_IGN) != SIG_IGN)
1110 signal (SIGHUP, fatal_error_signal);
1111 sigunblock (sigmask (SIGHUP));
1112 }
1113
1114 if (
1115 #ifndef CANNOT_DUMP
1116 ! noninteractive || initialized
1117 #else
1118 1
1119 #endif
1120 )
1121 {
1122 /* Don't catch these signals in batch mode if dumping.
1123 On some machines, this sets static data that would make
1124 signal fail to work right when the dumped Emacs is run. */
1125 signal (SIGQUIT, fatal_error_signal);
1126 signal (SIGILL, fatal_error_signal);
1127 signal (SIGTRAP, fatal_error_signal);
1128 #ifdef SIGUSR1
1129 signal (SIGUSR1, handle_USR1_signal);
1130 #ifdef SIGUSR2
1131 signal (SIGUSR2, handle_USR2_signal);
1132 #endif
1133 #endif
1134 #ifdef SIGABRT
1135 signal (SIGABRT, fatal_error_signal);
1136 #endif
1137 #ifdef SIGHWE
1138 signal (SIGHWE, fatal_error_signal);
1139 #endif
1140 #ifdef SIGPRE
1141 signal (SIGPRE, fatal_error_signal);
1142 #endif
1143 #ifdef SIGORE
1144 signal (SIGORE, fatal_error_signal);
1145 #endif
1146 #ifdef SIGUME
1147 signal (SIGUME, fatal_error_signal);
1148 #endif
1149 #ifdef SIGDLK
1150 signal (SIGDLK, fatal_error_signal);
1151 #endif
1152 #ifdef SIGCPULIM
1153 signal (SIGCPULIM, fatal_error_signal);
1154 #endif
1155 #ifdef SIGIOT
1156 /* This is missing on some systems - OS/2, for example. */
1157 signal (SIGIOT, fatal_error_signal);
1158 #endif
1159 #ifdef SIGEMT
1160 signal (SIGEMT, fatal_error_signal);
1161 #endif
1162 signal (SIGFPE, fatal_error_signal);
1163 #ifdef SIGBUS
1164 signal (SIGBUS, fatal_error_signal);
1165 #endif
1166 signal (SIGSEGV, fatal_error_signal);
1167 #ifdef SIGSYS
1168 signal (SIGSYS, fatal_error_signal);
1169 #endif
1170 signal (SIGTERM, fatal_error_signal);
1171 #ifdef SIGXCPU
1172 signal (SIGXCPU, fatal_error_signal);
1173 #endif
1174 #ifdef SIGXFSZ
1175 signal (SIGXFSZ, fatal_error_signal);
1176 #endif /* SIGXFSZ */
1177
1178 #ifdef SIGDANGER
1179 /* This just means available memory is getting low. */
1180 signal (SIGDANGER, memory_warning_signal);
1181 #endif
1182
1183 #ifdef AIX
1184 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
1185 signal (SIGXCPU, fatal_error_signal);
1186 #ifndef _I386
1187 signal (SIGIOINT, fatal_error_signal);
1188 #endif
1189 signal (SIGGRANT, fatal_error_signal);
1190 signal (SIGRETRACT, fatal_error_signal);
1191 signal (SIGSOUND, fatal_error_signal);
1192 signal (SIGMSG, fatal_error_signal);
1193 #endif /* AIX */
1194 }
1195
1196 noninteractive1 = noninteractive;
1197
1198 /* Perform basic initializations (not merely interning symbols). */
1199
1200 if (!initialized)
1201 {
1202 init_alloc_once ();
1203 init_obarray ();
1204 init_eval_once ();
1205 init_charset_once ();
1206 init_coding_once ();
1207 init_syntax_once (); /* Create standard syntax table. */
1208 init_category_once (); /* Create standard category table. */
1209 /* Must be done before init_buffer. */
1210 init_casetab_once ();
1211 init_buffer_once (); /* Create buffer table and some buffers. */
1212 init_minibuf_once (); /* Create list of minibuffers. */
1213 /* Must precede init_window_once. */
1214
1215 /* Call syms_of_xfaces before init_window_once because that
1216 function creates Vterminal_frame. Termcap frames now use
1217 faces, and the face implementation uses some symbols as
1218 face names. */
1219 syms_of_xfaces ();
1220 /* Call syms_of_keyboard before init_window_once because
1221 keyboard sets up symbols that include some face names that
1222 the X support will want to use. This can happen when
1223 CANNOT_DUMP is defined. */
1224 syms_of_keyboard ();
1225
1226 #ifdef MAC_OS8
1227 /* init_window_once calls make_terminal_frame which on Mac OS
1228 creates a full-fledge output_mac type frame. This does not
1229 work correctly before syms_of_textprop, syms_of_macfns,
1230 syms_of_ccl, syms_of_fontset, syms_of_xterm, syms_of_search,
1231 syms_of_frame, mac_initialize, and init_keyboard have already
1232 been called. */
1233 syms_of_textprop ();
1234 syms_of_macfns ();
1235 syms_of_ccl ();
1236 syms_of_fontset ();
1237 syms_of_macterm ();
1238 syms_of_macmenu ();
1239 syms_of_data ();
1240 syms_of_search ();
1241 syms_of_frame ();
1242
1243 mac_initialize ();
1244 init_keyboard ();
1245 #endif
1246
1247 init_window_once (); /* Init the window system. */
1248 init_fileio_once (); /* Must precede any path manipulation. */
1249 #ifdef HAVE_WINDOW_SYSTEM
1250 init_fringe_once (); /* Swap bitmaps if necessary. */
1251 #endif /* HAVE_WINDOW_SYSTEM */
1252 }
1253
1254 init_alloc ();
1255
1256 if (do_initial_setlocale)
1257 {
1258 fixup_locale ();
1259 Vsystem_messages_locale = Vprevious_system_messages_locale;
1260 Vsystem_time_locale = Vprevious_system_time_locale;
1261 }
1262
1263 init_eval ();
1264 init_data ();
1265 #ifdef CLASH_DETECTION
1266 init_filelock ();
1267 #endif
1268 init_atimer ();
1269 running_asynch_code = 0;
1270
1271 /* Handle --unibyte and the EMACS_UNIBYTE envvar,
1272 but not while dumping. */
1273 if (1)
1274 {
1275 int inhibit_unibyte = 0;
1276
1277 /* --multibyte overrides EMACS_UNIBYTE. */
1278 if (argmatch (argv, argc, "-no-unibyte", "--no-unibyte", 4, NULL, &skip_args)
1279 || argmatch (argv, argc, "-multibyte", "--multibyte", 4, NULL, &skip_args)
1280 /* Ignore EMACS_UNIBYTE before dumping. */
1281 || (!initialized && noninteractive))
1282 inhibit_unibyte = 1;
1283
1284 /* --unibyte requests that we set up to do everything with single-byte
1285 buffers and strings. We need to handle this before calling
1286 init_lread, init_editfns and other places that generate Lisp strings
1287 from text in the environment. */
1288 /* Actually this shouldn't be needed as of 20.4 in a generally
1289 unibyte environment. As handa says, environment values
1290 aren't now decoded; also existing buffers are now made
1291 unibyte during startup if .emacs sets unibyte. Tested with
1292 8-bit data in environment variables and /etc/passwd, setting
1293 unibyte and Latin-1 in .emacs. -- Dave Love */
1294 if (argmatch (argv, argc, "-unibyte", "--unibyte", 4, NULL, &skip_args)
1295 || argmatch (argv, argc, "-no-multibyte", "--no-multibyte", 4, NULL, &skip_args)
1296 || (getenv ("EMACS_UNIBYTE") && !inhibit_unibyte))
1297 {
1298 Lisp_Object old_log_max;
1299 Lisp_Object symbol, tail;
1300
1301 symbol = intern ("default-enable-multibyte-characters");
1302 Fset (symbol, Qnil);
1303
1304 if (initialized)
1305 {
1306 /* Erase pre-dump messages in *Messages* now so no abort. */
1307 old_log_max = Vmessage_log_max;
1308 XSETFASTINT (Vmessage_log_max, 0);
1309 message_dolog ("", 0, 1, 0);
1310 Vmessage_log_max = old_log_max;
1311 }
1312
1313 for (tail = Vbuffer_alist; CONSP (tail);
1314 tail = XCDR (tail))
1315 {
1316 Lisp_Object buffer;
1317
1318 buffer = Fcdr (XCAR (tail));
1319 /* Verify that all buffers are empty now, as they
1320 ought to be. */
1321 if (BUF_Z (XBUFFER (buffer)) > BUF_BEG (XBUFFER (buffer)))
1322 abort ();
1323 /* It is safe to do this crudely in an empty buffer. */
1324 XBUFFER (buffer)->enable_multibyte_characters = Qnil;
1325 }
1326 }
1327 }
1328
1329 no_loadup
1330 = argmatch (argv, argc, "-nl", "--no-loadup", 6, NULL, &skip_args);
1331
1332
1333 #ifdef HAVE_X_WINDOWS
1334 /* Stupid kludge to catch command-line display spec. We can't
1335 handle this argument entirely in window system dependent code
1336 because we don't even know which window system dependent code
1337 to run until we've recognized this argument. */
1338 {
1339 char *displayname = 0;
1340 int count_before = skip_args;
1341
1342 /* Skip any number of -d options, but only use the last one. */
1343 while (1)
1344 {
1345 int count_before_this = skip_args;
1346
1347 if (argmatch (argv, argc, "-d", "--display", 3, &displayname, &skip_args))
1348 display_arg = 1;
1349 else if (argmatch (argv, argc, "-display", 0, 3, &displayname, &skip_args))
1350 display_arg = 1;
1351 else
1352 break;
1353
1354 count_before = count_before_this;
1355 }
1356
1357 /* If we have the form --display=NAME,
1358 convert it into -d name.
1359 This requires inserting a new element into argv. */
1360 if (displayname != 0 && skip_args - count_before == 1)
1361 {
1362 char **new = (char **) xmalloc (sizeof (char *) * (argc + 2));
1363 int j;
1364
1365 for (j = 0; j < count_before + 1; j++)
1366 new[j] = argv[j];
1367 new[count_before + 1] = "-d";
1368 new[count_before + 2] = displayname;
1369 for (j = count_before + 2; j <argc; j++)
1370 new[j + 1] = argv[j];
1371 argv = new;
1372 argc++;
1373 }
1374 /* Change --display to -d, when its arg is separate. */
1375 else if (displayname != 0 && skip_args > count_before
1376 && argv[count_before + 1][1] == '-')
1377 argv[count_before + 1] = "-d";
1378
1379 /* Don't actually discard this arg. */
1380 skip_args = count_before;
1381 }
1382 #endif
1383
1384 /* argmatch must not be used after here,
1385 except when bulding temacs
1386 because the -d argument has not been skipped in skip_args. */
1387
1388 #ifdef MSDOS
1389 /* Call early 'cause init_environment needs it. */
1390 init_dosfns ();
1391 /* Set defaults for several environment variables. */
1392 if (initialized)
1393 init_environment (argc, argv, skip_args);
1394 else
1395 tzset ();
1396 #endif /* MSDOS */
1397
1398 #ifdef WINDOWSNT
1399 globals_of_w32 ();
1400 /* Initialize environment from registry settings. */
1401 init_environment (argv);
1402 init_ntproc (); /* must precede init_editfns. */
1403 #endif
1404
1405 #ifdef HAVE_CARBON
1406 if (initialized)
1407 init_mac_osx_environment ();
1408 #endif
1409
1410 /* egetenv is a pretty low-level facility, which may get called in
1411 many circumstances; it seems flimsy to put off initializing it
1412 until calling init_callproc. */
1413 set_process_environment ();
1414 /* AIX crashes are reported in system versions 3.2.3 and 3.2.4
1415 if this is not done. Do it after set_process_environment so that we
1416 don't pollute Vprocess_environment. */
1417 /* Setting LANG here will defeat the startup locale processing... */
1418 #ifdef AIX3_2
1419 putenv ("LANG=C");
1420 #endif
1421
1422 init_buffer (); /* Init default directory of main buffer. */
1423
1424 init_callproc_1 (); /* Must precede init_cmdargs and init_sys_modes. */
1425 init_cmdargs (argc, argv, skip_args); /* Must precede init_lread. */
1426
1427 if (initialized)
1428 {
1429 /* Erase any pre-dump messages in the message log, to avoid confusion. */
1430 Lisp_Object old_log_max;
1431 old_log_max = Vmessage_log_max;
1432 XSETFASTINT (Vmessage_log_max, 0);
1433 message_dolog ("", 0, 1, 0);
1434 Vmessage_log_max = old_log_max;
1435 }
1436
1437 init_callproc (); /* Must follow init_cmdargs but not init_sys_modes. */
1438 init_lread ();
1439
1440 /* Intern the names of all standard functions and variables;
1441 define standard keys. */
1442
1443 if (!initialized)
1444 {
1445 /* The basic levels of Lisp must come first. */
1446 /* And data must come first of all
1447 for the sake of symbols like error-message. */
1448 #ifndef MAC_OS8
1449 /* Called before init_window_once for Mac OS Classic. */
1450 syms_of_data ();
1451 #endif
1452 syms_of_alloc ();
1453 syms_of_lread ();
1454 syms_of_print ();
1455 syms_of_eval ();
1456 syms_of_fns ();
1457 syms_of_floatfns ();
1458
1459 syms_of_abbrev ();
1460 syms_of_buffer ();
1461 syms_of_bytecode ();
1462 syms_of_callint ();
1463 syms_of_casefiddle ();
1464 syms_of_casetab ();
1465 syms_of_callproc ();
1466 syms_of_category ();
1467 #ifndef MAC_OS8
1468 /* Called before init_window_once for Mac OS Classic. */
1469 syms_of_ccl ();
1470 #endif
1471 syms_of_charset ();
1472 syms_of_cmds ();
1473 #ifndef NO_DIR_LIBRARY
1474 syms_of_dired ();
1475 #endif /* not NO_DIR_LIBRARY */
1476 syms_of_display ();
1477 syms_of_doc ();
1478 syms_of_editfns ();
1479 syms_of_emacs ();
1480 syms_of_fileio ();
1481 syms_of_coding (); /* This should be after syms_of_fileio. */
1482 #ifdef CLASH_DETECTION
1483 syms_of_filelock ();
1484 #endif /* CLASH_DETECTION */
1485 syms_of_indent ();
1486 syms_of_insdel ();
1487 syms_of_keymap ();
1488 syms_of_macros ();
1489 syms_of_marker ();
1490 syms_of_minibuf ();
1491 syms_of_process ();
1492 #ifndef MAC_OS8
1493 /* Called before init_window_once for Mac OS Classic. */
1494 syms_of_search ();
1495 syms_of_frame ();
1496 #endif
1497 syms_of_syntax ();
1498 syms_of_term ();
1499 syms_of_undo ();
1500 #ifdef HAVE_SOUND
1501 syms_of_sound ();
1502 #endif
1503 #ifndef MAC_OS8
1504 /* Called before init_window_once for Mac OS Classic. */
1505 syms_of_textprop ();
1506 #endif
1507 syms_of_composite ();
1508 #ifdef VMS
1509 syms_of_vmsproc ();
1510 #endif /* VMS */
1511 #ifdef WINDOWSNT
1512 syms_of_ntproc ();
1513 #endif /* WINDOWSNT */
1514 syms_of_window ();
1515 syms_of_xdisp ();
1516 #ifdef HAVE_WINDOW_SYSTEM
1517 syms_of_fringe ();
1518 syms_of_image ();
1519 #endif /* HAVE_WINDOW_SYSTEM */
1520 #ifdef HAVE_X_WINDOWS
1521 syms_of_xterm ();
1522 syms_of_xfns ();
1523 syms_of_fontset ();
1524 #ifdef HAVE_X_SM
1525 syms_of_xsmfns ();
1526 #endif
1527 #ifdef HAVE_X11
1528 syms_of_xselect ();
1529 #endif
1530 #endif /* HAVE_X_WINDOWS */
1531
1532 #ifdef HAVE_MENUS
1533 #ifndef HAVE_NTGUI
1534 #ifndef MAC_OS
1535 /* Called before init_window_once for Mac OS Classic. */
1536 syms_of_xmenu ();
1537 #endif
1538 #endif
1539 #endif
1540
1541 #ifdef HAVE_NTGUI
1542 syms_of_w32term ();
1543 syms_of_w32fns ();
1544 syms_of_w32select ();
1545 syms_of_w32menu ();
1546 syms_of_fontset ();
1547 #endif /* HAVE_NTGUI */
1548
1549 #ifdef HAVE_CARBON
1550 syms_of_macterm ();
1551 syms_of_macfns ();
1552 syms_of_macmenu ();
1553 syms_of_fontset ();
1554 #endif /* HAVE_CARBON */
1555
1556 #ifdef SYMS_SYSTEM
1557 SYMS_SYSTEM;
1558 #endif
1559
1560 #ifdef SYMS_MACHINE
1561 SYMS_MACHINE;
1562 #endif
1563
1564 keys_of_casefiddle ();
1565 keys_of_cmds ();
1566 keys_of_buffer ();
1567 keys_of_keyboard ();
1568 keys_of_keymap ();
1569 keys_of_minibuf ();
1570 keys_of_window ();
1571 }
1572 else
1573 {
1574 /*
1575 Initialization that must be done even if the global variable
1576 initialized is non zero
1577 */
1578 #ifdef HAVE_NTGUI
1579 globals_of_w32fns ();
1580 globals_of_w32menu ();
1581 #endif /* end #ifdef HAVE_NTGUI */
1582 }
1583
1584 if (!noninteractive)
1585 {
1586 #ifdef VMS
1587 init_vms_input ();/* init_display calls get_frame_size, that needs this. */
1588 #endif /* VMS */
1589 init_display (); /* Determine terminal type. init_sys_modes uses results. */
1590 }
1591 #ifndef MAC_OS8
1592 /* Called before init_window_once for Mac OS Classic. */
1593 init_keyboard (); /* This too must precede init_sys_modes. */
1594 #endif
1595 #ifdef VMS
1596 init_vmsproc (); /* And this too. */
1597 #endif /* VMS */
1598 init_sys_modes (); /* Init system terminal modes (RAW or CBREAK, etc.). */
1599 init_fns ();
1600 init_xdisp ();
1601 #ifdef HAVE_WINDOW_SYSTEM
1602 init_fringe ();
1603 init_image ();
1604 #endif /* HAVE_WINDOW_SYSTEM */
1605 init_macros ();
1606 init_editfns ();
1607 init_floatfns ();
1608 #ifdef VMS
1609 init_vmsfns ();
1610 #endif /* VMS */
1611 init_process ();
1612 #ifdef HAVE_SOUND
1613 init_sound ();
1614 #endif
1615 init_window ();
1616
1617 if (!initialized)
1618 {
1619 char *file;
1620 /* Handle -l loadup, args passed by Makefile. */
1621 if (argmatch (argv, argc, "-l", "--load", 3, &file, &skip_args))
1622 Vtop_level = Fcons (intern ("load"),
1623 Fcons (build_string (file), Qnil));
1624 /* Unless next switch is -nl, load "loadup.el" first thing. */
1625 if (! no_loadup)
1626 Vtop_level = Fcons (intern ("load"),
1627 Fcons (build_string ("loadup.el"), Qnil));
1628 }
1629
1630 if (initialized)
1631 {
1632 #ifdef HAVE_TZSET
1633 {
1634 /* If the execution TZ happens to be the same as the dump TZ,
1635 change it to some other value and then change it back,
1636 to force the underlying implementation to reload the TZ info.
1637 This is needed on implementations that load TZ info from files,
1638 since the TZ file contents may differ between dump and execution. */
1639 char *tz = getenv ("TZ");
1640 if (tz && !strcmp (tz, dump_tz))
1641 {
1642 ++*tz;
1643 tzset ();
1644 --*tz;
1645 }
1646 }
1647 #endif
1648 }
1649
1650 /* Set up for profiling. This is known to work on FreeBSD and
1651 GNU/Linux. It might work on some other systems too. Give it a
1652 try and tell us if it works on your system. To compile for
1653 profiling use something like `make CFLAGS="-pg -g -O -DPROFILING=1'. */
1654 #if defined (__FreeBSD__) || defined (__linux)
1655 #ifdef PROFILING
1656 if (initialized)
1657 {
1658 extern void _mcleanup ();
1659 extern char etext;
1660 extern void safe_bcopy ();
1661 extern void dump_opcode_frequencies ();
1662
1663 atexit (_mcleanup);
1664 /* This uses safe_bcopy because that function comes first in the
1665 Emacs executable. It might be better to use something that
1666 gives the start of the text segment, but start_of_text is not
1667 defined on all systems now. */
1668 monstartup (safe_bcopy, &etext);
1669 }
1670 else
1671 moncontrol (0);
1672 #endif
1673 #endif
1674
1675 initialized = 1;
1676
1677 #ifdef LOCALTIME_CACHE
1678 /* Some versions of localtime have a bug. They cache the value of the time
1679 zone rather than looking it up every time. Since localtime() is
1680 called to bolt the undumping time into the undumped emacs, this
1681 results in localtime ignoring the TZ environment variable.
1682 This flushes the new TZ value into localtime. */
1683 tzset ();
1684 #endif /* defined (LOCALTIME_CACHE) */
1685
1686 /* Enter editor command loop. This never returns. */
1687 Frecursive_edit ();
1688 /* NOTREACHED */
1689 return 0;
1690 }
1691 \f
1692 /* Sort the args so we can find the most important ones
1693 at the beginning of argv. */
1694
1695 /* First, here's a table of all the standard options. */
1696
1697 struct standard_args
1698 {
1699 char *name;
1700 char *longname;
1701 int priority;
1702 int nargs;
1703 };
1704
1705 struct standard_args standard_args[] =
1706 {
1707 { "-version", "--version", 150, 0 },
1708 #ifdef HAVE_SHM
1709 { "-nl", "--no-shared-memory", 140, 0 },
1710 #endif
1711 #ifdef VMS
1712 { "-map", "--map-data", 130, 0 },
1713 #endif
1714 { "-t", "--terminal", 120, 1 },
1715 { "-nw", "--no-window-system", 110, 0 },
1716 { "-nw", "--no-windows", 110, 0 },
1717 { "-batch", "--batch", 100, 0 },
1718 { "-script", "--script", 100, 1 },
1719 { "-help", "--help", 90, 0 },
1720 { "-no-unibyte", "--no-unibyte", 83, 0 },
1721 { "-multibyte", "--multibyte", 82, 0 },
1722 { "-unibyte", "--unibyte", 81, 0 },
1723 { "-no-multibyte", "--no-multibyte", 80, 0 },
1724 { "-nl", "--no-loadup", 70, 0 },
1725 /* -d must come last before the options handled in startup.el. */
1726 { "-d", "--display", 60, 1 },
1727 { "-display", 0, 60, 1 },
1728 /* Now for the options handled in startup.el. */
1729 { "-q", "--no-init-file", 50, 0 },
1730 { "-no-init-file", 0, 50, 0 },
1731 { "-no-site-file", "--no-site-file", 40, 0 },
1732 { "-no-splash", "--no-splash", 40, 0 },
1733 { "-u", "--user", 30, 1 },
1734 { "-user", 0, 30, 1 },
1735 { "-debug-init", "--debug-init", 20, 0 },
1736 { "-i", "--icon-type", 15, 0 },
1737 { "-itype", 0, 15, 0 },
1738 { "-iconic", "--iconic", 15, 0 },
1739 { "-bg", "--background-color", 10, 1 },
1740 { "-background", 0, 10, 1 },
1741 { "-fg", "--foreground-color", 10, 1 },
1742 { "-foreground", 0, 10, 1 },
1743 { "-bd", "--border-color", 10, 1 },
1744 { "-bw", "--border-width", 10, 1 },
1745 { "-ib", "--internal-border", 10, 1 },
1746 { "-ms", "--mouse-color", 10, 1 },
1747 { "-cr", "--cursor-color", 10, 1 },
1748 { "-fn", "--font", 10, 1 },
1749 { "-font", 0, 10, 1 },
1750 { "-fs", "--fullscreen", 10, 0 },
1751 { "-fw", "--fullwidth", 10, 0 },
1752 { "-fh", "--fullheight", 10, 0 },
1753 { "-g", "--geometry", 10, 1 },
1754 { "-geometry", 0, 10, 1 },
1755 { "-T", "--title", 10, 1 },
1756 { "-title", 0, 10, 1 },
1757 { "-name", "--name", 10, 1 },
1758 { "-xrm", "--xrm", 10, 1 },
1759 { "-r", "--reverse-video", 5, 0 },
1760 { "-rv", 0, 5, 0 },
1761 { "-reverse", 0, 5, 0 },
1762 { "-hb", "--horizontal-scroll-bars", 5, 0 },
1763 { "-vb", "--vertical-scroll-bars", 5, 0 },
1764 { "-color", "--color", 5, 0},
1765 /* These have the same priority as ordinary file name args,
1766 so they are not reordered with respect to those. */
1767 { "-L", "--directory", 0, 1 },
1768 { "-directory", 0, 0, 1 },
1769 { "-l", "--load", 0, 1 },
1770 { "-load", 0, 0, 1 },
1771 { "-f", "--funcall", 0, 1 },
1772 { "-funcall", 0, 0, 1 },
1773 { "-eval", "--eval", 0, 1 },
1774 { "-execute", "--execute", 0, 1 },
1775 { "-find-file", "--find-file", 0, 1 },
1776 { "-visit", "--visit", 0, 1 },
1777 { "-file", "--file", 0, 1 },
1778 { "-insert", "--insert", 0, 1 },
1779 /* This should be processed after ordinary file name args and the like. */
1780 { "-kill", "--kill", -10, 0 },
1781 };
1782
1783 /* Reorder the elements of ARGV (assumed to have ARGC elements)
1784 so that the highest priority ones come first.
1785 Do not change the order of elements of equal priority.
1786 If an option takes an argument, keep it and its argument together.
1787
1788 If an option that takes no argument appears more
1789 than once, eliminate all but one copy of it. */
1790
1791 static void
1792 sort_args (argc, argv)
1793 int argc;
1794 char **argv;
1795 {
1796 char **new = (char **) xmalloc (sizeof (char *) * argc);
1797 /* For each element of argv,
1798 the corresponding element of options is:
1799 0 for an option that takes no arguments,
1800 1 for an option that takes one argument, etc.
1801 -1 for an ordinary non-option argument. */
1802 int *options = (int *) xmalloc (sizeof (int) * argc);
1803 int *priority = (int *) xmalloc (sizeof (int) * argc);
1804 int to = 1;
1805 int incoming_used = 1;
1806 int from;
1807 int i;
1808
1809 /* Categorize all the options,
1810 and figure out which argv elts are option arguments. */
1811 for (from = 1; from < argc; from++)
1812 {
1813 options[from] = -1;
1814 priority[from] = 0;
1815 if (argv[from][0] == '-')
1816 {
1817 int match, thislen;
1818 char *equals;
1819
1820 /* If we have found "--", don't consider
1821 any more arguments as options. */
1822 if (argv[from][1] == '-' && argv[from][2] == 0)
1823 {
1824 /* Leave the "--", and everything following it, at the end. */
1825 for (; from < argc; from++)
1826 {
1827 priority[from] = -100;
1828 options[from] = -1;
1829 }
1830 break;
1831 }
1832
1833 /* Look for a match with a known old-fashioned option. */
1834 for (i = 0; i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1835 if (!strcmp (argv[from], standard_args[i].name))
1836 {
1837 options[from] = standard_args[i].nargs;
1838 priority[from] = standard_args[i].priority;
1839 if (from + standard_args[i].nargs >= argc)
1840 fatal ("Option `%s' requires an argument\n", argv[from]);
1841 from += standard_args[i].nargs;
1842 goto done;
1843 }
1844
1845 /* Look for a match with a known long option.
1846 MATCH is -1 if no match so far, -2 if two or more matches so far,
1847 >= 0 (the table index of the match) if just one match so far. */
1848 if (argv[from][1] == '-')
1849 {
1850 match = -1;
1851 thislen = strlen (argv[from]);
1852 equals = index (argv[from], '=');
1853 if (equals != 0)
1854 thislen = equals - argv[from];
1855
1856 for (i = 0;
1857 i < sizeof (standard_args) / sizeof (standard_args[0]); i++)
1858 if (standard_args[i].longname
1859 && !strncmp (argv[from], standard_args[i].longname,
1860 thislen))
1861 {
1862 if (match == -1)
1863 match = i;
1864 else
1865 match = -2;
1866 }
1867
1868 /* If we found exactly one match, use that. */
1869 if (match >= 0)
1870 {
1871 options[from] = standard_args[match].nargs;
1872 priority[from] = standard_args[match].priority;
1873 /* If --OPTION=VALUE syntax is used,
1874 this option uses just one argv element. */
1875 if (equals != 0)
1876 options[from] = 0;
1877 if (from + options[from] >= argc)
1878 fatal ("Option `%s' requires an argument\n", argv[from]);
1879 from += options[from];
1880 }
1881 }
1882 done: ;
1883 }
1884 }
1885
1886 /* Copy the arguments, in order of decreasing priority, to NEW. */
1887 new[0] = argv[0];
1888 while (incoming_used < argc)
1889 {
1890 int best = -1;
1891 int best_priority = -9999;
1892
1893 /* Find the highest priority remaining option.
1894 If several have equal priority, take the first of them. */
1895 for (from = 1; from < argc; from++)
1896 {
1897 if (argv[from] != 0 && priority[from] > best_priority)
1898 {
1899 best_priority = priority[from];
1900 best = from;
1901 }
1902 /* Skip option arguments--they are tied to the options. */
1903 if (options[from] > 0)
1904 from += options[from];
1905 }
1906
1907 if (best < 0)
1908 abort ();
1909
1910 /* Copy the highest priority remaining option, with its args, to NEW.
1911 Unless it is a duplicate of the previous one. */
1912 if (! (options[best] == 0
1913 && ! strcmp (new[to - 1], argv[best])))
1914 {
1915 new[to++] = argv[best];
1916 for (i = 0; i < options[best]; i++)
1917 new[to++] = argv[best + i + 1];
1918 }
1919
1920 incoming_used += 1 + (options[best] > 0 ? options[best] : 0);
1921
1922 /* Clear out this option in ARGV. */
1923 argv[best] = 0;
1924 for (i = 0; i < options[best]; i++)
1925 argv[best + i + 1] = 0;
1926 }
1927
1928 /* If duplicate options were deleted, fill up extra space with null ptrs. */
1929 while (to < argc)
1930 new[to++] = 0;
1931
1932 bcopy (new, argv, sizeof (char *) * argc);
1933
1934 free (options);
1935 free (new);
1936 free (priority);
1937 }
1938 \f
1939 DEFUN ("kill-emacs", Fkill_emacs, Skill_emacs, 0, 1, "P",
1940 doc: /* Exit the Emacs job and kill it.
1941 If ARG is an integer, return ARG as the exit program code.
1942 If ARG is a string, stuff it as keyboard input.
1943
1944 The value of `kill-emacs-hook', if not void,
1945 is a list of functions (of no args),
1946 all of which are called before Emacs is actually killed. */)
1947 (arg)
1948 Lisp_Object arg;
1949 {
1950 struct gcpro gcpro1;
1951
1952 GCPRO1 (arg);
1953
1954 if (feof (stdin))
1955 arg = Qt;
1956
1957 if (!NILP (Vrun_hooks) && !noninteractive)
1958 call1 (Vrun_hooks, intern ("kill-emacs-hook"));
1959
1960 UNGCPRO;
1961
1962 /* Is it really necessary to do this deassign
1963 when we are going to exit anyway? */
1964 /* #ifdef VMS
1965 stop_vms_input ();
1966 #endif */
1967
1968 shut_down_emacs (0, 0, STRINGP (arg) ? arg : Qnil);
1969
1970 /* If we have an auto-save list file,
1971 kill it because we are exiting Emacs deliberately (not crashing).
1972 Do it after shut_down_emacs, which does an auto-save. */
1973 if (STRINGP (Vauto_save_list_file_name))
1974 unlink (SDATA (Vauto_save_list_file_name));
1975
1976 exit (INTEGERP (arg) ? XINT (arg)
1977 #ifdef VMS
1978 : 1
1979 #else
1980 : 0
1981 #endif
1982 );
1983 /* NOTREACHED */
1984 }
1985
1986
1987 /* Perform an orderly shutdown of Emacs. Autosave any modified
1988 buffers, kill any child processes, clean up the terminal modes (if
1989 we're in the foreground), and other stuff like that. Don't perform
1990 any redisplay; this may be called when Emacs is shutting down in
1991 the background, or after its X connection has died.
1992
1993 If SIG is a signal number, print a message for it.
1994
1995 This is called by fatal signal handlers, X protocol error handlers,
1996 and Fkill_emacs. */
1997
1998 void
1999 shut_down_emacs (sig, no_x, stuff)
2000 int sig, no_x;
2001 Lisp_Object stuff;
2002 {
2003 /* Prevent running of hooks from now on. */
2004 Vrun_hooks = Qnil;
2005
2006 /* Don't update display from now on. */
2007 Vinhibit_redisplay = Qt;
2008
2009 /* If we are controlling the terminal, reset terminal modes. */
2010 #ifdef EMACS_HAVE_TTY_PGRP
2011 {
2012 int pgrp = EMACS_GETPGRP (0);
2013
2014 int tpgrp;
2015 if (EMACS_GET_TTY_PGRP (0, &tpgrp) != -1
2016 && tpgrp == pgrp)
2017 {
2018 fflush (stdout);
2019 reset_sys_modes ();
2020 if (sig && sig != SIGTERM)
2021 fprintf (stderr, "Fatal error (%d)", sig);
2022 }
2023 }
2024 #else
2025 fflush (stdout);
2026 reset_sys_modes ();
2027 #endif
2028
2029 stuff_buffered_input (stuff);
2030
2031 kill_buffer_processes (Qnil);
2032 Fdo_auto_save (Qt, Qnil);
2033
2034 #ifdef CLASH_DETECTION
2035 unlock_all_files ();
2036 #endif
2037
2038 #ifdef VMS
2039 kill_vms_processes ();
2040 #endif
2041
2042 #if 0 /* This triggers a bug in XCloseDisplay and is not needed. */
2043 #ifdef HAVE_X_WINDOWS
2044 /* It's not safe to call intern here. Maybe we are crashing. */
2045 if (!noninteractive && SYMBOLP (Vwindow_system)
2046 && SCHARS (SYMBOL_NAME (Vwindow_system)) == 1
2047 && SREF (SYMBOL_NAME (Vwindow_system), 0) == 'x'
2048 && ! no_x)
2049 Fx_close_current_connection ();
2050 #endif /* HAVE_X_WINDOWS */
2051 #endif
2052
2053 #ifdef SIGIO
2054 /* There is a tendency for a SIGIO signal to arrive within exit,
2055 and cause a SIGHUP because the input descriptor is already closed. */
2056 unrequest_sigio ();
2057 signal (SIGIO, SIG_IGN);
2058 #endif
2059
2060 #ifdef WINDOWSNT
2061 term_ntproc ();
2062 #endif
2063
2064 /* Do this only if terminating normally, we want glyph matrices
2065 etc. in a core dump. */
2066 if (sig == 0 || sig == SIGTERM)
2067 {
2068 check_glyph_memory ();
2069 check_message_stack ();
2070 }
2071
2072 #ifdef MSDOS
2073 dos_cleanup ();
2074 #endif
2075 }
2076
2077
2078 \f
2079 #ifndef CANNOT_DUMP
2080
2081 #ifdef HAVE_SHM
2082
2083 DEFUN ("dump-emacs-data", Fdump_emacs_data, Sdump_emacs_data, 1, 1, 0,
2084 doc: /* Dump current state of Emacs into data file FILENAME.
2085 This function exists on systems that use HAVE_SHM. */)
2086 (filename)
2087 Lisp_Object filename;
2088 {
2089 extern char my_edata[];
2090 Lisp_Object tem;
2091
2092 check_pure_size ();
2093 CHECK_STRING (filename);
2094 filename = Fexpand_file_name (filename, Qnil);
2095
2096 tem = Vpurify_flag;
2097 Vpurify_flag = Qnil;
2098
2099 fflush (stdout);
2100 /* Tell malloc where start of impure now is. */
2101 /* Also arrange for warnings when nearly out of space. */
2102 #ifndef SYSTEM_MALLOC
2103 memory_warnings (my_edata, malloc_warning);
2104 #endif
2105 map_out_data (SDATA (filename));
2106
2107 Vpurify_flag = tem;
2108
2109 return Qnil;
2110 }
2111
2112 #else /* not HAVE_SHM */
2113
2114 DEFUN ("dump-emacs", Fdump_emacs, Sdump_emacs, 2, 2, 0,
2115 doc: /* Dump current state of Emacs into executable file FILENAME.
2116 Take symbols from SYMFILE (presumably the file you executed to run Emacs).
2117 This is used in the file `loadup.el' when building Emacs.
2118
2119 You must run Emacs in batch mode in order to dump it. */)
2120 (filename, symfile)
2121 Lisp_Object filename, symfile;
2122 {
2123 extern char my_edata[];
2124 Lisp_Object tem;
2125 Lisp_Object symbol;
2126 int count = SPECPDL_INDEX ();
2127
2128 check_pure_size ();
2129
2130 if (! noninteractive)
2131 error ("Dumping Emacs works only in batch mode");
2132
2133 /* Bind `command-line-processed' to nil before dumping,
2134 so that the dumped Emacs will process its command line
2135 and set up to work with X windows if appropriate. */
2136 symbol = intern ("command-line-processed");
2137 specbind (symbol, Qnil);
2138
2139 CHECK_STRING (filename);
2140 filename = Fexpand_file_name (filename, Qnil);
2141 if (!NILP (symfile))
2142 {
2143 CHECK_STRING (symfile);
2144 if (SCHARS (symfile))
2145 symfile = Fexpand_file_name (symfile, Qnil);
2146 }
2147
2148 tem = Vpurify_flag;
2149 Vpurify_flag = Qnil;
2150
2151 #ifdef HAVE_TZSET
2152 set_time_zone_rule (dump_tz);
2153 #ifndef LOCALTIME_CACHE
2154 /* Force a tz reload, since set_time_zone_rule doesn't. */
2155 tzset ();
2156 #endif
2157 #endif
2158
2159 fflush (stdout);
2160 #ifdef VMS
2161 mapout_data (SDATA (filename));
2162 #else
2163 /* Tell malloc where start of impure now is. */
2164 /* Also arrange for warnings when nearly out of space. */
2165 #ifndef SYSTEM_MALLOC
2166 #ifndef WINDOWSNT
2167 /* On Windows, this was done before dumping, and that once suffices.
2168 Meanwhile, my_edata is not valid on Windows. */
2169 memory_warnings (my_edata, malloc_warning);
2170 #endif /* not WINDOWSNT */
2171 #endif
2172 #ifdef DOUG_LEA_MALLOC
2173 malloc_state_ptr = malloc_get_state ();
2174 #endif
2175
2176 #ifdef USE_MMAP_FOR_BUFFERS
2177 mmap_set_vars (0);
2178 #endif
2179 unexec (SDATA (filename),
2180 !NILP (symfile) ? SDATA (symfile) : 0, my_edata, 0, 0);
2181 #ifdef USE_MMAP_FOR_BUFFERS
2182 mmap_set_vars (1);
2183 #endif
2184 #ifdef DOUG_LEA_MALLOC
2185 free (malloc_state_ptr);
2186 #endif
2187 #endif /* not VMS */
2188
2189 Vpurify_flag = tem;
2190
2191 return unbind_to (count, Qnil);
2192 }
2193
2194 #endif /* not HAVE_SHM */
2195
2196 #endif /* not CANNOT_DUMP */
2197 \f
2198 #if HAVE_SETLOCALE
2199 /* Recover from setlocale (LC_ALL, ""). */
2200 void
2201 fixup_locale ()
2202 {
2203 /* The Emacs Lisp reader needs LC_NUMERIC to be "C",
2204 so that numbers are read and printed properly for Emacs Lisp. */
2205 setlocale (LC_NUMERIC, "C");
2206 }
2207
2208 /* Set system locale CATEGORY, with previous locale *PLOCALE, to
2209 DESIRED_LOCALE. */
2210 static void
2211 synchronize_locale (category, plocale, desired_locale)
2212 int category;
2213 Lisp_Object *plocale;
2214 Lisp_Object desired_locale;
2215 {
2216 if (! EQ (*plocale, desired_locale))
2217 {
2218 *plocale = desired_locale;
2219 setlocale (category, (STRINGP (desired_locale)
2220 ? (char *)(SDATA (desired_locale))
2221 : ""));
2222 }
2223 }
2224
2225 /* Set system time locale to match Vsystem_time_locale, if possible. */
2226 void
2227 synchronize_system_time_locale ()
2228 {
2229 synchronize_locale (LC_TIME, &Vprevious_system_time_locale,
2230 Vsystem_time_locale);
2231 }
2232
2233 /* Set system messages locale to match Vsystem_messages_locale, if
2234 possible. */
2235 void
2236 synchronize_system_messages_locale ()
2237 {
2238 #ifdef LC_MESSAGES
2239 synchronize_locale (LC_MESSAGES, &Vprevious_system_messages_locale,
2240 Vsystem_messages_locale);
2241 #endif
2242 }
2243 #endif /* HAVE_SETLOCALE */
2244 \f
2245 #ifndef SEPCHAR
2246 #define SEPCHAR ':'
2247 #endif
2248
2249 Lisp_Object
2250 decode_env_path (evarname, defalt)
2251 char *evarname, *defalt;
2252 {
2253 register char *path, *p;
2254 Lisp_Object lpath, element, tem;
2255
2256 /* It's okay to use getenv here, because this function is only used
2257 to initialize variables when Emacs starts up, and isn't called
2258 after that. */
2259 if (evarname != 0)
2260 path = (char *) getenv (evarname);
2261 else
2262 path = 0;
2263 if (!path)
2264 path = defalt;
2265 #ifdef DOS_NT
2266 /* Ensure values from the environment use the proper directory separator. */
2267 if (path)
2268 {
2269 p = alloca (strlen (path) + 1);
2270 strcpy (p, path);
2271 path = p;
2272
2273 if ('/' == DIRECTORY_SEP)
2274 dostounix_filename (path);
2275 else
2276 unixtodos_filename (path);
2277 }
2278 #endif
2279 lpath = Qnil;
2280 while (1)
2281 {
2282 p = index (path, SEPCHAR);
2283 if (!p) p = path + strlen (path);
2284 element = (p - path ? make_string (path, p - path)
2285 : build_string ("."));
2286
2287 /* Add /: to the front of the name
2288 if it would otherwise be treated as magic. */
2289 tem = Ffind_file_name_handler (element, Qt);
2290
2291 /* However, if the handler says "I'm safe",
2292 don't bother adding /:. */
2293 if (SYMBOLP (tem))
2294 {
2295 Lisp_Object prop;
2296 prop = Fget (tem, intern ("safe-magic"));
2297 if (! NILP (prop))
2298 tem = Qnil;
2299 }
2300
2301 if (! NILP (tem))
2302 element = concat2 (build_string ("/:"), element);
2303
2304 lpath = Fcons (element, lpath);
2305 if (*p)
2306 path = p + 1;
2307 else
2308 break;
2309 }
2310 return Fnreverse (lpath);
2311 }
2312
2313 void
2314 syms_of_emacs ()
2315 {
2316 Qfile_name_handler_alist = intern ("file-name-handler-alist");
2317 staticpro (&Qfile_name_handler_alist);
2318
2319 #ifndef CANNOT_DUMP
2320 #ifdef HAVE_SHM
2321 defsubr (&Sdump_emacs_data);
2322 #else
2323 defsubr (&Sdump_emacs);
2324 #endif
2325 #endif
2326
2327 defsubr (&Skill_emacs);
2328
2329 defsubr (&Sinvocation_name);
2330 defsubr (&Sinvocation_directory);
2331
2332 DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
2333 doc: /* Args passed by shell to Emacs, as a list of strings.
2334 Many arguments are deleted from the list as they are processed. */);
2335
2336 DEFVAR_LISP ("system-type", &Vsystem_type,
2337 doc: /* Value is symbol indicating type of operating system you are using. */);
2338 Vsystem_type = intern (SYSTEM_TYPE);
2339
2340 DEFVAR_LISP ("system-configuration", &Vsystem_configuration,
2341 doc: /* Value is string indicating configuration Emacs was built for.
2342 On MS-Windows, the value reflects the OS flavor and version on which
2343 Emacs is running. */);
2344 Vsystem_configuration = build_string (EMACS_CONFIGURATION);
2345
2346 DEFVAR_LISP ("system-configuration-options", &Vsystem_configuration_options,
2347 doc: /* String containing the configuration options Emacs was built with. */);
2348 Vsystem_configuration_options = build_string (EMACS_CONFIG_OPTIONS);
2349
2350 DEFVAR_BOOL ("noninteractive", &noninteractive1,
2351 doc: /* Non-nil means Emacs is running without interactive terminal. */);
2352
2353 DEFVAR_LISP ("kill-emacs-hook", &Vkill_emacs_hook,
2354 doc: /* Hook to be run when kill-emacs is called.
2355 Since `kill-emacs' may be invoked when the terminal is disconnected (or
2356 in other similar situations), functions placed on this hook should not
2357 expect to be able to interact with the user. To ask for confirmation,
2358 see `kill-emacs-query-functions' instead.
2359
2360 The hook is not run in batch mode, i.e., if `noninteractive' is non-nil. */);
2361 Vkill_emacs_hook = Qnil;
2362
2363 empty_string = build_string ("");
2364 staticpro (&empty_string);
2365
2366 DEFVAR_INT ("emacs-priority", &emacs_priority,
2367 doc: /* Priority for Emacs to run at.
2368 This value is effective only if set before Emacs is dumped,
2369 and only if the Emacs executable is installed with setuid to permit
2370 it to change priority. (Emacs sets its uid back to the real uid.)
2371 Currently, you need to define SET_EMACS_PRIORITY in `config.h'
2372 before you compile Emacs, to enable the code for this feature. */);
2373 emacs_priority = 0;
2374
2375 DEFVAR_LISP ("path-separator", &Vpath_separator,
2376 doc: /* The directory separator in search paths, as a string. */);
2377 {
2378 char c = SEPCHAR;
2379 Vpath_separator = make_string (&c, 1);
2380 }
2381
2382 DEFVAR_LISP ("invocation-name", &Vinvocation_name,
2383 doc: /* The program name that was used to run Emacs.
2384 Any directory names are omitted. */);
2385
2386 DEFVAR_LISP ("invocation-directory", &Vinvocation_directory,
2387 doc: /* The directory in which the Emacs executable was found, to run it.
2388 The value is nil if that directory's name is not known. */);
2389
2390 DEFVAR_LISP ("installation-directory", &Vinstallation_directory,
2391 doc: /* A directory within which to look for the `lib-src' and `etc' directories.
2392 This is non-nil when we can't find those directories in their standard
2393 installed locations, but we can find them
2394 near where the Emacs executable was found. */);
2395 Vinstallation_directory = Qnil;
2396
2397 DEFVAR_LISP ("system-messages-locale", &Vsystem_messages_locale,
2398 doc: /* System locale for messages. */);
2399 Vsystem_messages_locale = Qnil;
2400
2401 DEFVAR_LISP ("previous-system-messages-locale",
2402 &Vprevious_system_messages_locale,
2403 doc: /* Most recently used system locale for messages. */);
2404 Vprevious_system_messages_locale = Qnil;
2405
2406 DEFVAR_LISP ("system-time-locale", &Vsystem_time_locale,
2407 doc: /* System locale for time. */);
2408 Vsystem_time_locale = Qnil;
2409
2410 DEFVAR_LISP ("previous-system-time-locale", &Vprevious_system_time_locale,
2411 doc: /* Most recently used system locale for time. */);
2412 Vprevious_system_time_locale = Qnil;
2413 }
2414
2415 /* arch-tag: 7bfd356a-c720-4612-8ab6-aa4222931c2e
2416 (do not change this comment) */