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