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