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