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