aboutsummaryrefslogtreecommitdiffstats
path: root/src/emacs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/emacs.c')
-rw-r--r--src/emacs.c100
1 files changed, 60 insertions, 40 deletions
diff --git a/src/emacs.c b/src/emacs.c
index a1ad2d44b3b..08609ee6a08 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -28,12 +28,12 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
28#include <unistd.h> 28#include <unistd.h>
29 29
30#include <close-stream.h> 30#include <close-stream.h>
31#include <ignore-value.h>
32 31
33#include "lisp.h" 32#include "lisp.h"
34 33
35#ifdef WINDOWSNT 34#ifdef WINDOWSNT
36#include <fcntl.h> 35#include <fcntl.h>
36#include <sys/socket.h>
37#include "w32.h" 37#include "w32.h"
38#include "w32heap.h" 38#include "w32heap.h"
39#endif 39#endif
@@ -204,10 +204,10 @@ int initial_argc;
204static void sort_args (int argc, char **argv); 204static void sort_args (int argc, char **argv);
205static void syms_of_emacs (void); 205static void syms_of_emacs (void);
206 206
207/* MSVC needs each string be shorter than 2048 bytes, so the usage 207/* C89 needs each string be at most 509 characters, so the usage
208 strings below are split to not overflow this limit. */ 208 strings below are split to not overflow this limit. */
209#define USAGE1 "\ 209static char const *const usage_message[] =
210Usage: %s [OPTION-OR-FILENAME]...\n\ 210 { "\
211\n\ 211\n\
212Run Emacs, the extensible, customizable, self-documenting real-time\n\ 212Run Emacs, the extensible, customizable, self-documenting real-time\n\
213display editor. The recommended way to start Emacs for normal editing\n\ 213display editor. The recommended way to start Emacs for normal editing\n\
@@ -218,11 +218,15 @@ read the main documentation for these command-line arguments.\n\
218\n\ 218\n\
219Initialization options:\n\ 219Initialization options:\n\
220\n\ 220\n\
221",
222 "\
221--batch do not do interactive display; implies -q\n\ 223--batch do not do interactive display; implies -q\n\
222--chdir DIR change to directory DIR\n\ 224--chdir DIR change to directory DIR\n\
223--daemon start a server in the background\n\ 225--daemon start a server in the background\n\
224--debug-init enable Emacs Lisp debugger for init file\n\ 226--debug-init enable Emacs Lisp debugger for init file\n\
225--display, -d DISPLAY use X server DISPLAY\n\ 227--display, -d DISPLAY use X server DISPLAY\n\
228",
229 "\
226--no-desktop do not load a saved desktop\n\ 230--no-desktop do not load a saved desktop\n\
227--no-init-file, -q load neither ~/.emacs nor default.el\n\ 231--no-init-file, -q load neither ~/.emacs nor default.el\n\
228--no-shared-memory, -nl do not use shared memory\n\ 232--no-shared-memory, -nl do not use shared memory\n\
@@ -230,14 +234,16 @@ Initialization options:\n\
230--no-site-lisp, -nsl do not add site-lisp directories to load-path\n\ 234--no-site-lisp, -nsl do not add site-lisp directories to load-path\n\
231--no-splash do not display a splash screen on startup\n\ 235--no-splash do not display a splash screen on startup\n\
232--no-window-system, -nw do not communicate with X, ignoring $DISPLAY\n\ 236--no-window-system, -nw do not communicate with X, ignoring $DISPLAY\n\
237",
238 "\
233--quick, -Q equivalent to:\n\ 239--quick, -Q equivalent to:\n\
234 -q --no-site-file --no-site-lisp --no-splash\n\ 240 -q --no-site-file --no-site-lisp --no-splash\n\
235--script FILE run FILE as an Emacs Lisp script\n\ 241--script FILE run FILE as an Emacs Lisp script\n\
236--terminal, -t DEVICE use DEVICE for terminal I/O\n\ 242--terminal, -t DEVICE use DEVICE for terminal I/O\n\
237--user, -u USER load ~USER/.emacs instead of your own\n\ 243--user, -u USER load ~USER/.emacs instead of your own\n\
238\n%s" 244\n\
239 245",
240#define USAGE2 "\ 246 "\
241Action options:\n\ 247Action options:\n\
242\n\ 248\n\
243FILE visit FILE using find-file\n\ 249FILE visit FILE using find-file\n\
@@ -246,6 +252,8 @@ FILE visit FILE using find-file\n\
246--directory, -L DIR add DIR to variable load-path\n\ 252--directory, -L DIR add DIR to variable load-path\n\
247--eval EXPR evaluate Emacs Lisp expression EXPR\n\ 253--eval EXPR evaluate Emacs Lisp expression EXPR\n\
248--execute EXPR evaluate Emacs Lisp expression EXPR\n\ 254--execute EXPR evaluate Emacs Lisp expression EXPR\n\
255",
256 "\
249--file FILE visit FILE using find-file\n\ 257--file FILE visit FILE using find-file\n\
250--find-file FILE visit FILE using find-file\n\ 258--find-file FILE visit FILE using find-file\n\
251--funcall, -f FUNC call Emacs Lisp function FUNC with no arguments\n\ 259--funcall, -f FUNC call Emacs Lisp function FUNC with no arguments\n\
@@ -253,9 +261,9 @@ FILE visit FILE using find-file\n\
253--kill exit without asking for confirmation\n\ 261--kill exit without asking for confirmation\n\
254--load, -l FILE load Emacs Lisp FILE using the load function\n\ 262--load, -l FILE load Emacs Lisp FILE using the load function\n\
255--visit FILE visit FILE using find-file\n\ 263--visit FILE visit FILE using find-file\n\
256\n" 264\n\
257 265",
258#define USAGE3 "\ 266 "\
259Display options:\n\ 267Display options:\n\
260\n\ 268\n\
261--background-color, -bg COLOR window background color\n\ 269--background-color, -bg COLOR window background color\n\
@@ -263,6 +271,8 @@ Display options:\n\
263 used for debugging Emacs\n\ 271 used for debugging Emacs\n\
264--border-color, -bd COLOR main border color\n\ 272--border-color, -bd COLOR main border color\n\
265--border-width, -bw WIDTH width of main border\n\ 273--border-width, -bw WIDTH width of main border\n\
274",
275 "\
266--color, --color=MODE override color mode for character terminals;\n\ 276--color, --color=MODE override color mode for character terminals;\n\
267 MODE defaults to `auto', and\n\ 277 MODE defaults to `auto', and\n\
268 can also be `never', `always',\n\ 278 can also be `never', `always',\n\
@@ -270,17 +280,23 @@ Display options:\n\
270--cursor-color, -cr COLOR color of the Emacs cursor indicating point\n\ 280--cursor-color, -cr COLOR color of the Emacs cursor indicating point\n\
271--font, -fn FONT default font; must be fixed-width\n\ 281--font, -fn FONT default font; must be fixed-width\n\
272--foreground-color, -fg COLOR window foreground color\n\ 282--foreground-color, -fg COLOR window foreground color\n\
283",
284 "\
273--fullheight, -fh make the first frame high as the screen\n\ 285--fullheight, -fh make the first frame high as the screen\n\
274--fullscreen, -fs make the first frame fullscreen\n\ 286--fullscreen, -fs make the first frame fullscreen\n\
275--fullwidth, -fw make the first frame wide as the screen\n\ 287--fullwidth, -fw make the first frame wide as the screen\n\
276--maximized, -mm make the first frame maximized\n\ 288--maximized, -mm make the first frame maximized\n\
277--geometry, -g GEOMETRY window geometry\n\ 289--geometry, -g GEOMETRY window geometry\n\
290",
291 "\
278--no-bitmap-icon, -nbi do not use picture of gnu for Emacs icon\n\ 292--no-bitmap-icon, -nbi do not use picture of gnu for Emacs icon\n\
279--iconic start Emacs in iconified state\n\ 293--iconic start Emacs in iconified state\n\
280--internal-border, -ib WIDTH width between text and main border\n\ 294--internal-border, -ib WIDTH width between text and main border\n\
281--line-spacing, -lsp PIXELS additional space to put between lines\n\ 295--line-spacing, -lsp PIXELS additional space to put between lines\n\
282--mouse-color, -ms COLOR mouse cursor color in Emacs window\n\ 296--mouse-color, -ms COLOR mouse cursor color in Emacs window\n\
283--name NAME title for initial Emacs frame\n\ 297--name NAME title for initial Emacs frame\n\
298",
299 "\
284--no-blinking-cursor, -nbc disable blinking cursor\n\ 300--no-blinking-cursor, -nbc disable blinking cursor\n\
285--reverse-video, -r, -rv switch foreground and background\n\ 301--reverse-video, -r, -rv switch foreground and background\n\
286--title, -T TITLE title for initial Emacs frame\n\ 302--title, -T TITLE title for initial Emacs frame\n\
@@ -289,9 +305,9 @@ Display options:\n\
289--parent-id XID set parent window\n\ 305--parent-id XID set parent window\n\
290--help display this help and exit\n\ 306--help display this help and exit\n\
291--version output version information and exit\n\ 307--version output version information and exit\n\
292\n" 308\n\
293 309",
294#define USAGE4 "\ 310 "\
295You can generally also specify long option names with a single -; for\n\ 311You can generally also specify long option names with a single -; for\n\
296example, -batch as well as --batch. You can use any unambiguous\n\ 312example, -batch as well as --batch. You can use any unambiguous\n\
297abbreviation for a --option.\n\ 313abbreviation for a --option.\n\
@@ -301,6 +317,7 @@ Emacs' operation. See the main documentation.\n\
301\n\ 317\n\
302Report bugs to bug-gnu-emacs@gnu.org. First, please see the Bugs\n\ 318Report bugs to bug-gnu-emacs@gnu.org. First, please see the Bugs\n\
303section of the Emacs manual or the file BUGS.\n" 319section of the Emacs manual or the file BUGS.\n"
320 };
304 321
305 322
306/* True if handling a fatal error already. */ 323/* True if handling a fatal error already. */
@@ -648,9 +665,7 @@ close_output_streams (void)
648{ 665{
649 if (close_stream (stdout) != 0) 666 if (close_stream (stdout) != 0)
650 { 667 {
651 fprintf (stderr, "Write error to standard output: %s\n", 668 emacs_perror ("Write error to standard output");
652 strerror (errno));
653 fflush (stderr);
654 _exit (EXIT_FAILURE); 669 _exit (EXIT_FAILURE);
655 } 670 }
656 671
@@ -791,7 +806,7 @@ main (int argc, char **argv)
791 execvp (argv[0], argv); 806 execvp (argv[0], argv);
792 807
793 /* If the exec fails, try to dump anyway. */ 808 /* If the exec fails, try to dump anyway. */
794 perror ("execvp"); 809 emacs_perror (argv[0]);
795 } 810 }
796#endif /* HAVE_PERSONALITY_LINUX32 */ 811#endif /* HAVE_PERSONALITY_LINUX32 */
797 812
@@ -892,7 +907,7 @@ main (int argc, char **argv)
892 emacs_close (0); 907 emacs_close (0);
893 emacs_close (1); 908 emacs_close (1);
894 result = emacs_open (term, O_RDWR, 0); 909 result = emacs_open (term, O_RDWR, 0);
895 if (result < 0 || dup (0) < 0) 910 if (result < 0 || fcntl (0, F_DUPFD_CLOEXEC, 1) < 0)
896 { 911 {
897 char *errstring = strerror (errno); 912 char *errstring = strerror (errno);
898 fprintf (stderr, "%s: %s: %s\n", argv[0], term, errstring); 913 fprintf (stderr, "%s: %s: %s\n", argv[0], term, errstring);
@@ -939,9 +954,10 @@ main (int argc, char **argv)
939 /* Handle the --help option, which gives a usage message. */ 954 /* Handle the --help option, which gives a usage message. */
940 if (argmatch (argv, argc, "-help", "--help", 3, NULL, &skip_args)) 955 if (argmatch (argv, argc, "-help", "--help", 3, NULL, &skip_args))
941 { 956 {
942 printf (USAGE1, argv[0], USAGE2); 957 int i;
943 printf (USAGE3); 958 printf ("Usage: %s [OPTION-OR-FILENAME]...\n", argv[0]);
944 printf (USAGE4); 959 for (i = 0; i < sizeof usage_message / sizeof *usage_message; i++)
960 fputs (usage_message[i], stdout);
945 exit (0); 961 exit (0);
946 } 962 }
947 963
@@ -972,7 +988,7 @@ main (int argc, char **argv)
972 use a pipe for synchronization. The parent waits for the child 988 use a pipe for synchronization. The parent waits for the child
973 to close its end of the pipe (using `daemon-initialized') 989 to close its end of the pipe (using `daemon-initialized')
974 before exiting. */ 990 before exiting. */
975 if (pipe (daemon_pipe) == -1) 991 if (pipe2 (daemon_pipe, O_CLOEXEC) != 0)
976 { 992 {
977 fprintf (stderr, "Cannot pipe!\n"); 993 fprintf (stderr, "Cannot pipe!\n");
978 exit (1); 994 exit (1);
@@ -997,7 +1013,7 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
997 char buf[1]; 1013 char buf[1];
998 1014
999 /* Close unused writing end of the pipe. */ 1015 /* Close unused writing end of the pipe. */
1000 close (daemon_pipe[1]); 1016 emacs_close (daemon_pipe[1]);
1001 1017
1002 /* Just wait for the child to close its end of the pipe. */ 1018 /* Just wait for the child to close its end of the pipe. */
1003 do 1019 do
@@ -1017,13 +1033,13 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
1017 exit (1); 1033 exit (1);
1018 } 1034 }
1019 1035
1020 close (daemon_pipe[0]); 1036 emacs_close (daemon_pipe[0]);
1021 exit (0); 1037 exit (0);
1022 } 1038 }
1023 if (f < 0) 1039 if (f < 0)
1024 { 1040 {
1025 fprintf (stderr, "Cannot fork!\n"); 1041 emacs_perror ("fork");
1026 exit (1); 1042 exit (EXIT_CANCELED);
1027 } 1043 }
1028 1044
1029#ifdef DAEMON_MUST_EXEC 1045#ifdef DAEMON_MUST_EXEC
@@ -1040,14 +1056,14 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
1040 if (! (0 <= fdStrlen && fdStrlen < sizeof fdStr)) 1056 if (! (0 <= fdStrlen && fdStrlen < sizeof fdStr))
1041 { 1057 {
1042 fprintf (stderr, "daemon: child name too long\n"); 1058 fprintf (stderr, "daemon: child name too long\n");
1043 exit (1); 1059 exit (EXIT_CANNOT_INVOKE);
1044 } 1060 }
1045 1061
1046 argv[skip_args] = fdStr; 1062 argv[skip_args] = fdStr;
1047 1063
1048 execvp (argv[0], argv); 1064 execvp (argv[0], argv);
1049 fprintf (stderr, "emacs daemon: exec failed: %d\n", errno); 1065 emacs_perror (argv[0]);
1050 exit (1); 1066 exit (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
1051 } 1067 }
1052 1068
1053 /* In exec'd: parse special dname into pipe and name info. */ 1069 /* In exec'd: parse special dname into pipe and name info. */
@@ -1055,7 +1071,7 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
1055 || strlen (dname_arg) < 1 || strlen (dname_arg) > 70) 1071 || strlen (dname_arg) < 1 || strlen (dname_arg) > 70)
1056 { 1072 {
1057 fprintf (stderr, "emacs daemon: daemon name absent or too long\n"); 1073 fprintf (stderr, "emacs daemon: daemon name absent or too long\n");
1058 exit (1); 1074 exit (EXIT_CANNOT_INVOKE);
1059 } 1075 }
1060 dname_arg2[0] = '\0'; 1076 dname_arg2[0] = '\0';
1061 sscanf (dname_arg, "\n%d,%d\n%s", &(daemon_pipe[0]), &(daemon_pipe[1]), 1077 sscanf (dname_arg, "\n%d,%d\n%s", &(daemon_pipe[0]), &(daemon_pipe[1]),
@@ -1067,10 +1083,7 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
1067 if (dname_arg) 1083 if (dname_arg)
1068 daemon_name = xstrdup (dname_arg); 1084 daemon_name = xstrdup (dname_arg);
1069 /* Close unused reading end of the pipe. */ 1085 /* Close unused reading end of the pipe. */
1070 close (daemon_pipe[0]); 1086 emacs_close (daemon_pipe[0]);
1071 /* Make sure that the used end of the pipe is closed on exec, so
1072 that it is not accessible to programs started from .emacs. */
1073 fcntl (daemon_pipe[1], F_SETFD, FD_CLOEXEC);
1074 1087
1075 setsid (); 1088 setsid ();
1076#else /* DOS_NT */ 1089#else /* DOS_NT */
@@ -1080,7 +1093,14 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
1080 } 1093 }
1081 1094
1082#if defined HAVE_PTHREAD && !defined SYSTEM_MALLOC && !defined DOUG_LEA_MALLOC 1095#if defined HAVE_PTHREAD && !defined SYSTEM_MALLOC && !defined DOUG_LEA_MALLOC
1083 malloc_enable_thread (); 1096# ifndef CANNOT_DUMP
1097 /* Do not make gmalloc thread-safe when creating bootstrap-emacs, as
1098 that causes an infinite recursive loop with FreeBSD. But do make
1099 it thread-safe when creating emacs, otherwise bootstrap-emacs
1100 fails on Cygwin. See Bug#14569. */
1101 if (!noninteractive || initialized)
1102# endif
1103 malloc_enable_thread ();
1084#endif 1104#endif
1085 1105
1086 init_signals (dumping); 1106 init_signals (dumping);
@@ -1917,8 +1937,8 @@ shut_down_emacs (int sig, Lisp_Object stuff)
1917 char buf[sizeof format - 2 + INT_STRLEN_BOUND (int)]; 1937 char buf[sizeof format - 2 + INT_STRLEN_BOUND (int)];
1918 int buflen = sprintf (buf, format, sig); 1938 int buflen = sprintf (buf, format, sig);
1919 char const *sig_desc = safe_strsignal (sig); 1939 char const *sig_desc = safe_strsignal (sig);
1920 ignore_value (write (STDERR_FILENO, buf, buflen)); 1940 emacs_write (STDERR_FILENO, buf, buflen);
1921 ignore_value (write (STDERR_FILENO, sig_desc, strlen (sig_desc))); 1941 emacs_write (STDERR_FILENO, sig_desc, strlen (sig_desc));
1922 } 1942 }
1923 } 1943 }
1924 } 1944 }
@@ -2235,12 +2255,12 @@ from the parent process and its tty file descriptors. */)
2235 error ("This function can only be called after loading the init files"); 2255 error ("This function can only be called after loading the init files");
2236 2256
2237 /* Get rid of stdin, stdout and stderr. */ 2257 /* Get rid of stdin, stdout and stderr. */
2238 nfd = open ("/dev/null", O_RDWR); 2258 nfd = emacs_open ("/dev/null", O_RDWR, 0);
2239 err |= nfd < 0; 2259 err |= nfd < 0;
2240 err |= dup2 (nfd, 0) < 0; 2260 err |= dup2 (nfd, 0) < 0;
2241 err |= dup2 (nfd, 1) < 0; 2261 err |= dup2 (nfd, 1) < 0;
2242 err |= dup2 (nfd, 2) < 0; 2262 err |= dup2 (nfd, 2) < 0;
2243 err |= close (nfd) != 0; 2263 err |= emacs_close (nfd) != 0;
2244 2264
2245 /* Closing the pipe will notify the parent that it can exit. 2265 /* Closing the pipe will notify the parent that it can exit.
2246 FIXME: In case some other process inherited the pipe, closing it here 2266 FIXME: In case some other process inherited the pipe, closing it here
@@ -2250,7 +2270,7 @@ from the parent process and its tty file descriptors. */)
2250 call-process to make sure the pipe is never inherited by 2270 call-process to make sure the pipe is never inherited by
2251 subprocesses. */ 2271 subprocesses. */
2252 err |= write (daemon_pipe[1], "\n", 1) < 0; 2272 err |= write (daemon_pipe[1], "\n", 1) < 0;
2253 err |= close (daemon_pipe[1]) != 0; 2273 err |= emacs_close (daemon_pipe[1]) != 0;
2254 /* Set it to an invalid value so we know we've already run this function. */ 2274 /* Set it to an invalid value so we know we've already run this function. */
2255 daemon_pipe[1] = -1; 2275 daemon_pipe[1] = -1;
2256 2276