]>
code.delx.au - gnu-emacs/blob - lib-src/emacsclient.c
1 /* Client process that communicates with GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
31 /* config.h defines these, which disables sockets altogether! */
39 # define NO_SOCKETS_IN_FILE_SYSTEM
41 # define HSOCKET SOCKET
42 # define CLOSE_SOCKET closesocket
43 # define INITIALIZE() (initialize_sockets ())
45 #else /* !WINDOWSNT */
47 # include <sys/types.h>
49 # ifdef HAVE_INET_SOCKETS
50 # include <netinet/in.h>
53 # define INVALID_SOCKET -1
55 # define CLOSE_SOCKET close
58 #endif /* !WINDOWSNT */
75 #else /* not WINDOWSNT */
77 #endif /* not WINDOWSNT */
86 #define DIRECTORY_SEP '/'
88 #ifndef IS_DIRECTORY_SEP
89 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
93 #define IS_DEVICE_SEP(_c_) 0
95 #define IS_DEVICE_SEP(_c_) ((_c_) == DEVICE_SEP)
99 #define IS_ANY_SEP(_c_) (IS_DIRECTORY_SEP (_c_))
104 char *getenv (), *getwd ();
108 #define VERSION "unspecified"
113 #define EXIT_SUCCESS 0
117 #define EXIT_FAILURE 1
132 /* Name used to invoke this program. */
135 /* The first argument to main. */
138 /* The second argument to main. */
141 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
144 /* Nonzero means args are expressions to be evaluated. --eval. */
147 /* Nonzero means don't open a new frame. --current-frame. */
148 int current_frame
= 0;
150 /* Nonzero means open a new graphical frame. */
151 int window_system
= 0;
153 /* The display on which Emacs should work. --display. */
154 char *display
= NULL
;
156 /* Nonzero means open a new Emacs frame on the current terminal. */
159 /* If non-NULL, the name of an editor to fallback to if the server
160 is not running. --alternate-editor. */
161 const char *alternate_editor
= NULL
;
163 /* If non-NULL, the filename of the UNIX socket. */
164 char *socket_name
= NULL
;
166 /* If non-NULL, the filename of the authentication file. */
167 char *server_file
= NULL
;
169 /* PID of the Emacs server process. */
172 void print_help_and_exit () NO_RETURN
;
174 struct option longopts
[] =
176 { "no-wait", no_argument
, NULL
, 'n' },
177 { "eval", no_argument
, NULL
, 'e' },
178 { "help", no_argument
, NULL
, 'H' },
179 { "version", no_argument
, NULL
, 'V' },
180 { "tty", no_argument
, NULL
, 't' },
181 { "current-frame", no_argument
, NULL
, 'c' },
182 { "alternate-editor", required_argument
, NULL
, 'a' },
183 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
184 { "socket-name", required_argument
, NULL
, 's' },
186 { "server-file", required_argument
, NULL
, 'f' },
187 { "display", required_argument
, NULL
, 'd' },
192 /* Like malloc but get fatal error if memory is exhausted. */
198 long *result
= (long *) malloc (size
);
207 /* Like strdup but get a fatal error if memory is exhausted. */
210 xstrdup (const char *s
)
212 char *result
= strdup (s
);
222 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
224 /* Return the current working directory. Returns NULL on errors.
225 Any other returned value must be freed with free. This is used
226 only when get_current_dir_name is not defined on the system. */
228 get_current_dir_name ()
232 struct stat dotstat
, pwdstat
;
233 /* If PWD is accurate, use it instead of calling getwd. PWD is
234 sometimes a nicer name, and using it may avoid a fatal error if a
235 parent directory is searchable but not readable. */
236 if ((pwd
= getenv ("PWD")) != 0
237 && (IS_DIRECTORY_SEP (*pwd
) || (*pwd
&& IS_DEVICE_SEP (pwd
[1])))
238 && stat (pwd
, &pwdstat
) == 0
239 && stat (".", &dotstat
) == 0
240 && dotstat
.st_ino
== pwdstat
.st_ino
241 && dotstat
.st_dev
== pwdstat
.st_dev
243 && strlen (pwd
) < MAXPATHLEN
247 buf
= (char *) xmalloc (strlen (pwd
) + 1);
255 size_t buf_size
= 1024;
256 buf
= (char *) xmalloc (buf_size
);
261 if (getcwd (buf
, buf_size
) == buf
)
265 int tmp_errno
= errno
;
271 buf
= (char *) realloc (buf
, buf_size
);
279 /* We need MAXPATHLEN here. */
280 buf
= (char *) xmalloc (MAXPATHLEN
+ 1);
283 if (getwd (buf
) == NULL
)
285 int tmp_errno
= errno
;
296 /* Message functions. */
302 static int window_app
= -1;
303 char szTitle
[MAX_PATH
];
306 /* Checking for STDOUT does not work; it's a valid handle also in
307 nonconsole apps. Testing for the console title seems to work. */
308 window_app
= (GetConsoleTitleA (szTitle
, MAX_PATH
) == 0);
314 execvp wrapper for Windows. Quotes arguments with embedded spaces.
316 This is necessary due to the broken implementation of exec* routines in
317 the Microsoft libraries: they concatenate the arguments together without
318 quoting special characters, and pass the result to CreateProcess, with
319 predictably bad results. By contrast, Posix execvp passes the arguments
320 directly into the argv array of the child process.
323 w32_execvp (path
, argv
)
329 /* Required to allow a .BAT script as alternate editor. */
330 argv
[0] = (char *) alternate_editor
;
332 for (i
= 0; argv
[i
]; i
++)
333 if (strchr (argv
[i
], ' '))
335 char *quoted
= alloca (strlen (argv
[i
]) + 3);
336 sprintf (quoted
, "\"%s\"", argv
[i
]);
340 return execvp (path
, argv
);
344 #define execvp w32_execvp
346 #endif /* WINDOWSNT */
349 message (int is_error
, char *message
, ...)
354 va_start (args
, message
);
355 vsprintf (msg
, message
, args
);
359 if (w32_window_app ())
362 MessageBox (NULL
, msg
, "Emacsclient ERROR", MB_ICONERROR
);
364 MessageBox (NULL
, msg
, "Emacsclient", MB_ICONINFORMATION
);
369 FILE *f
= is_error
? stderr
: stdout
;
376 /* Decode the options from argv and argc.
377 The global variable `optind' will say how many arguments we used up. */
380 decode_options (argc
, argv
)
384 alternate_editor
= getenv ("ALTERNATE_EDITOR");
385 display
= getenv ("DISPLAY");
386 if (display
&& strlen (display
) == 0)
391 int opt
= getopt_long (argc
, argv
,
392 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
405 /* If getopt returns 0, then it has already processed a
406 long-named option. We should do nothing. */
410 alternate_editor
= optarg
;
413 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
415 socket_name
= optarg
;
420 server_file
= optarg
;
436 message (FALSE
, "emacsclient %s\n", VERSION
);
449 print_help_and_exit ();
453 message (TRUE
, "Try `%s --help' for more information\n", progname
);
464 /* --no-wait implies --current-frame on ttys when there are file
465 arguments or expressions given. */
466 if (nowait
&& tty
&& argc
- optind
> 0)
481 print_help_and_exit ()
484 "Usage: %s [OPTIONS] FILE...\n\
485 Tell the Emacs server to visit the specified files.\n\
486 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
488 The following OPTIONS are accepted:\n\
489 -V, --version Just print version info and return\n\
490 -H, --help Print this usage information message\n\
491 -t, --tty Open a new Emacs frame on the current terminal\n\
492 -c, --current-frame Do not create a new frame; use the current Emacs frame\n\
493 -e, --eval Evaluate the FILE arguments as ELisp expressions\n\
494 -n, --no-wait Don't wait for the server to return\n\
495 -d, --display=DISPLAY Visit the file in the given display\n"
496 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
497 "-s, --socket-name=FILENAME\n\
498 Set filename of the UNIX socket for communication\n"
500 "-f, --server-file=FILENAME\n\
501 Set filename of the TCP authentication file\n\
502 -a, --alternate-editor=EDITOR\n\
503 Editor to fallback to if the server is not running\n\
505 Report bugs to bug-gnu-emacs@gnu.org.\n", progname
);
510 Try to run a different command, or --if no alternate editor is
511 defined-- exit with an errorcode.
516 if (alternate_editor
)
520 execvp (alternate_editor
, main_argv
+ i
);
521 message (TRUE
, "%s: error executing alternate editor \"%s\"\n",
522 progname
, alternate_editor
);
528 #if !defined (HAVE_SOCKETS) || !defined (HAVE_INET_SOCKETS)
538 message (TRUE
, "%s: Sorry, the Emacs server is supported only\n"
539 "on systems with Berkeley sockets.\n",
544 #else /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
547 # include <winsock2.h>
549 # include <sys/types.h>
550 # include <sys/socket.h>
552 # include <sys/stat.h>
556 #define AUTH_KEY_LENGTH 64
557 #define SEND_BUFFER_SIZE 4096
559 extern char *strerror ();
562 /* Buffer to accumulate data to send in TCP connections. */
563 char send_buffer
[SEND_BUFFER_SIZE
+ 1];
564 int sblen
= 0; /* Fill pointer for the send buffer. */
565 /* Socket used to communicate with the Emacs server process. */
566 HSOCKET emacs_socket
= 0;
568 /* Let's send the data to Emacs when either
569 - the data ends in "\n", or
570 - the buffer is full (but this shouldn't happen)
571 Otherwise, we just accumulate it. */
573 send_to_emacs (s
, data
)
579 int dlen
= strlen (data
);
580 if (dlen
+ sblen
>= SEND_BUFFER_SIZE
)
582 int part
= SEND_BUFFER_SIZE
- sblen
;
583 strncpy (&send_buffer
[sblen
], data
, part
);
585 sblen
= SEND_BUFFER_SIZE
;
589 strcpy (&send_buffer
[sblen
], data
);
596 if (sblen
== SEND_BUFFER_SIZE
597 || (sblen
> 0 && send_buffer
[sblen
-1] == '\n'))
599 int sent
= send (s
, send_buffer
, sblen
, 0);
601 strcpy (send_buffer
, &send_buffer
[sent
]);
608 /* In STR, insert a & before each &, each space, each newline, and
609 any initial -. Change spaces to underscores, too, so that the
610 return value never contains a space.
612 Does not change the string. Outputs the result to STREAM. */
614 quote_argument (s
, str
)
618 char *copy
= (char *) xmalloc (strlen (str
) * 2 + 1);
639 if (*p
== '&' || (*p
== '-' && p
== str
))
646 send_to_emacs (s
, copy
);
652 /* The inverse of quote_argument. Removes quoting in string STR by
653 modifying the string in place. Returns STR. */
656 unquote_argument (str
)
688 file_name_absolute_p (filename
)
689 const unsigned char *filename
;
691 /* Sanity check, it shouldn't happen. */
692 if (! filename
) return FALSE
;
694 /* /xxx is always an absolute path. */
695 if (filename
[0] == '/') return TRUE
;
697 /* Empty filenames (which shouldn't happen) are relative. */
698 if (filename
[0] == '\0') return FALSE
;
701 /* X:\xxx is always absolute. */
702 if (isalpha (filename
[0])
703 && filename
[1] == ':' && (filename
[2] == '\\' || filename
[2] == '/'))
706 /* Both \xxx and \\xxx\yyy are absolute. */
707 if (filename
[0] == '\\') return TRUE
;
710 FIXME: There's a corner case not dealt with, "x:y", where:
712 1) x is a valid drive designation (usually a letter in the A-Z range)
713 and y is a path, relative to the current directory on drive x. This
714 is absolute, *after* fixing the y part to include the current
717 2) x is a relative file name, and y is an NTFS stream name. This is a
718 correct relative path, but it is very unusual.
720 The trouble is that first case items are also valid examples of the
721 second case, i.e., "c:test" can be understood as drive:path or as
724 The "right" fix would involve checking whether
725 - the current drive/partition is NTFS,
726 - x is a valid (and accesible) drive designator,
727 - x:y already exists as a file:stream in the current directory,
728 - y already exists on the current directory of drive x,
729 - the auspices are favorable,
730 and then taking an "informed decision" based on the above.
732 Whatever the result, Emacs currently does a very bad job of dealing
733 with NTFS file:streams: it cannot visit them, and the only way to
734 create one is by setting `buffer-file-name' to point to it (either
735 manually or with emacsclient). So perhaps resorting to 1) and ignoring
736 2) for now is the right thing to do.
738 Anyway, something to decide After the Release.
746 /* Wrapper to make WSACleanup a cdecl, as required by atexit. */
748 __cdecl
close_winsock ()
753 /* Initialize the WinSock2 library. */
755 initialize_sockets ()
759 if (WSAStartup (MAKEWORD (2, 0), &wsaData
))
761 message (TRUE
, "%s: error initializing WinSock2", progname
);
765 atexit (close_winsock
);
767 #endif /* WINDOWSNT */
771 * Read the information needed to set up a TCP comm channel with
772 * the Emacs server: host, port, pid and authentication string.
775 get_server_config (server
, authentication
)
776 struct sockaddr_in
*server
;
777 char *authentication
;
784 if (file_name_absolute_p (server_file
))
785 config
= fopen (server_file
, "rb");
788 char *home
= getenv ("HOME");
792 char *path
= alloca (32 + strlen (home
) + strlen (server_file
));
793 sprintf (path
, "%s/.emacs.d/server/%s", home
, server_file
);
794 config
= fopen (path
, "rb");
797 if (!config
&& (home
= getenv ("APPDATA")))
799 char *path
= alloca (32 + strlen (home
) + strlen (server_file
));
800 sprintf (path
, "%s/.emacs.d/server/%s", home
, server_file
);
801 config
= fopen (path
, "rb");
809 if (fgets (dotted
, sizeof dotted
, config
)
810 && (port
= strchr (dotted
, ':'))
811 && (pid
= strchr (port
, ' ')))
818 message (TRUE
, "%s: invalid configuration info", progname
);
822 server
->sin_family
= AF_INET
;
823 server
->sin_addr
.s_addr
= inet_addr (dotted
);
824 server
->sin_port
= htons (atoi (port
));
826 if (! fread (authentication
, AUTH_KEY_LENGTH
, 1, config
))
828 message (TRUE
, "%s: cannot read authentication info", progname
);
834 emacs_pid
= atoi (pid
);
843 struct sockaddr_in server
;
844 struct linger l_arg
= {1, 1};
845 char auth_string
[AUTH_KEY_LENGTH
+ 1];
847 if (! get_server_config (&server
, auth_string
))
848 return INVALID_SOCKET
;
850 if (server
.sin_addr
.s_addr
!= inet_addr ("127.0.0.1"))
851 message (FALSE
, "%s: connected to remote socket at %s\n",
852 progname
, inet_ntoa (server
.sin_addr
));
855 * Open up an AF_INET socket
857 if ((s
= socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
859 message (TRUE
, "%s: socket: %s\n", progname
, strerror (errno
));
860 return INVALID_SOCKET
;
866 if (connect (s
, (struct sockaddr
*) &server
, sizeof server
) < 0)
868 message (TRUE
, "%s: connect: %s\n", progname
, strerror (errno
));
869 return INVALID_SOCKET
;
872 setsockopt (s
, SOL_SOCKET
, SO_LINGER
, (char *) &l_arg
, sizeof l_arg
);
875 * Send the authentication
877 auth_string
[AUTH_KEY_LENGTH
] = '\0';
879 send_to_emacs (s
, "-auth ");
880 send_to_emacs (s
, auth_string
);
881 send_to_emacs (s
, "\n");
887 /* Returns 1 if PREFIX is a prefix of STRING. */
889 strprefix (char *prefix
, char *string
)
898 for (i
= 0; prefix
[i
]; i
++)
899 if (!string
[i
] || string
[i
] != prefix
[i
])
905 #if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
907 /* Three possibilities:
908 2 - can't be `stat'ed (sets errno)
909 1 - isn't owned by us
910 0 - success: none of the above */
913 socket_status (socket_name
)
918 if (stat (socket_name
, &statbfr
) == -1)
921 if (statbfr
.st_uid
!= geteuid ())
928 /* A signal handler that passes the signal to the Emacs process.
929 Useful for SIGWINCH. */
932 pass_signal_to_emacs (int signalnum
)
934 int old_errno
= errno
;
937 kill (emacs_pid
, signalnum
);
939 signal (signalnum
, pass_signal_to_emacs
);
943 /* Signal handler for SIGCONT; notify the Emacs process that it can
944 now resume our tty frame. */
947 handle_sigcont (int signalnum
)
949 int old_errno
= errno
;
951 if (tcgetpgrp (1) == getpgrp ())
953 /* We are in the foreground. */
954 send_to_emacs (emacs_socket
, "-resume \n");
958 /* We are in the background; cancel the continue. */
959 kill (getpid (), SIGSTOP
);
962 signal (signalnum
, handle_sigcont
);
966 /* Signal handler for SIGTSTP; notify the Emacs process that we are
967 going to sleep. Normally the suspend is initiated by Emacs via
968 server-handle-suspend-tty, but if the server gets out of sync with
969 reality, we may get a SIGTSTP on C-z. Handling this signal and
970 notifying Emacs about it should get things under control again. */
973 handle_sigtstp (int signalnum
)
975 int old_errno
= errno
;
979 send_to_emacs (emacs_socket
, "-suspend \n");
981 /* Unblock this signal and call the default handler by temprarily
982 changing the handler and resignalling. */
983 sigprocmask (SIG_BLOCK
, NULL
, &set
);
984 sigdelset (&set
, signalnum
);
985 signal (signalnum
, SIG_DFL
);
986 kill (getpid (), signalnum
);
987 sigprocmask (SIG_SETMASK
, &set
, NULL
); /* Let's the above signal through. */
988 signal (signalnum
, handle_sigtstp
);
992 /* Set up signal handlers before opening a frame on the current tty. */
997 /* Set up signal handlers. */
998 signal (SIGWINCH
, pass_signal_to_emacs
);
1000 /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of
1001 deciding which terminal the signal came from. C-g is now a
1002 normal input event on secondary terminals. */
1004 signal (SIGINT
, pass_signal_to_emacs
);
1005 signal (SIGQUIT
, pass_signal_to_emacs
);
1008 signal (SIGCONT
, handle_sigcont
);
1009 signal (SIGTSTP
, handle_sigtstp
);
1010 signal (SIGTTOU
, handle_sigtstp
);
1018 struct sockaddr_un server
;
1021 * Open up an AF_UNIX socket in this person's home directory
1024 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
1026 message (TRUE
, "%s: socket: %s\n", progname
, strerror (errno
));
1027 return INVALID_SOCKET
;
1030 server
.sun_family
= AF_UNIX
;
1033 int sock_status
= 0;
1034 int default_sock
= !socket_name
;
1035 int saved_errno
= 0;
1037 char *server_name
= "server";
1039 if (socket_name
&& !index (socket_name
, '/') && !index (socket_name
, '\\'))
1040 { /* socket_name is a file name component. */
1041 server_name
= socket_name
;
1043 default_sock
= 1; /* Try both UIDs. */
1048 socket_name
= alloca (100 + strlen (server_name
));
1049 sprintf (socket_name
, "/tmp/emacs%d/%s",
1050 (int) geteuid (), server_name
);
1053 if (strlen (socket_name
) < sizeof (server
.sun_path
))
1054 strcpy (server
.sun_path
, socket_name
);
1057 message (TRUE
, "%s: socket-name %s too long",
1058 progname
, socket_name
);
1062 /* See if the socket exists, and if it's owned by us. */
1063 sock_status
= socket_status (server
.sun_path
);
1064 saved_errno
= errno
;
1065 if (sock_status
&& default_sock
)
1067 /* Failing that, see if LOGNAME or USER exist and differ from
1068 our euid. If so, look for a socket based on the UID
1069 associated with the name. This is reminiscent of the logic
1070 that init_editfns uses to set the global Vuser_full_name. */
1072 char *user_name
= (char *) getenv ("LOGNAME");
1075 user_name
= (char *) getenv ("USER");
1079 struct passwd
*pw
= getpwnam (user_name
);
1081 if (pw
&& (pw
->pw_uid
!= geteuid ()))
1083 /* We're running under su, apparently. */
1084 socket_name
= alloca (100 + strlen (server_name
));
1085 sprintf (socket_name
, "/tmp/emacs%d/%s",
1086 (int) pw
->pw_uid
, server_name
);
1088 if (strlen (socket_name
) < sizeof (server
.sun_path
))
1089 strcpy (server
.sun_path
, socket_name
);
1092 message (TRUE
, "%s: socket-name %s too long",
1093 progname
, socket_name
);
1094 exit (EXIT_FAILURE
);
1097 sock_status
= socket_status (server
.sun_path
);
1098 saved_errno
= errno
;
1101 errno
= saved_errno
;
1105 switch (sock_status
)
1108 /* There's a socket, but it isn't owned by us. This is OK if
1110 if (0 != geteuid ())
1112 message (TRUE
, "%s: Invalid socket owner\n", progname
);
1113 return INVALID_SOCKET
;
1119 if (saved_errno
== ENOENT
)
1121 "%s: can't find socket; have you started the server?\n\
1122 To start the server in Emacs, type \"M-x server-start\".\n",
1125 message (TRUE
, "%s: can't stat %s: %s\n",
1126 progname
, server
.sun_path
, strerror (saved_errno
));
1127 return INVALID_SOCKET
;
1131 if (connect (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2)
1134 message (TRUE
, "%s: connect: %s\n", progname
, strerror (errno
));
1135 return INVALID_SOCKET
;
1140 #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
1149 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1150 /* Explicit --socket-name argument. */
1153 s
= set_local_socket ();
1154 if ((s
!= INVALID_SOCKET
) || alternate_editor
)
1156 message (TRUE
, "%s: error accessing socket \"%s\"",
1157 progname
, socket_name
);
1158 exit (EXIT_FAILURE
);
1162 /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */
1164 server_file
= getenv ("EMACS_SERVER_FILE");
1168 s
= set_tcp_socket ();
1169 if ((s
!= INVALID_SOCKET
) || alternate_editor
)
1172 message (TRUE
, "%s: error accessing server file \"%s\"",
1173 progname
, server_file
);
1174 exit (EXIT_FAILURE
);
1177 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1178 /* Implicit local socket. */
1179 s
= set_local_socket ();
1180 if (s
!= INVALID_SOCKET
)
1184 /* Implicit server file. */
1185 server_file
= "server";
1186 s
= set_tcp_socket ();
1187 if ((s
!= INVALID_SOCKET
) || alternate_editor
)
1190 /* No implicit or explicit socket, and no alternate editor. */
1191 message (TRUE
, "%s: No socket or alternate editor. Please use:\n\n"
1192 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1195 "\t--server-file (or environment variable EMACS_SERVER_FILE)\n\
1196 \t--alternate-editor (or environment variable ALTERNATE_EDITOR)\n",
1198 exit (EXIT_FAILURE
);
1202 FARPROC set_fg
; /* Pointer to AllowSetForegroundWindow. */
1203 FARPROC get_wc
; /* Pointer to RealGetWindowClassA. */
1206 w32_find_emacs_process (hWnd
, lParam
)
1213 /* Reject any window not of class "Emacs". */
1214 if (! get_wc (hWnd
, class, sizeof (class))
1215 || strcmp (class, "Emacs"))
1218 /* We only need the process id, not the thread id. */
1219 (void) GetWindowThreadProcessId (hWnd
, &pid
);
1221 /* Not the one we're looking for. */
1222 if (pid
!= (DWORD
) emacs_pid
) return TRUE
;
1224 /* OK, let's raise it. */
1227 /* Stop enumeration. */
1232 * Search for a window of class "Emacs" and owned by a process with
1233 * process id = emacs_pid. If found, allow it to grab the focus.
1240 /* It shouldn't happen when dealing with TCP sockets. */
1241 if (!emacs_pid
) return;
1243 if (!(hUser32
= LoadLibrary ("user32.dll"))) return;
1245 /* Modern Windows restrict which processes can set the foreground window.
1246 emacsclient can allow Emacs to grab the focus by calling the function
1247 AllowSetForegroundWindow. Unfortunately, older Windows (W95, W98 and
1248 NT) lack this function, so we have to check its availability. */
1249 if ((set_fg
= GetProcAddress (hUser32
, "AllowSetForegroundWindow"))
1250 && (get_wc
= GetProcAddress (hUser32
, "RealGetWindowClassA")))
1251 EnumWindows (w32_find_emacs_process
, (LPARAM
) 0);
1253 FreeLibrary (hUser32
);
1262 int i
, rl
, needlf
= 0;
1264 char string
[BUFSIZ
+1];
1270 /* Process options. */
1271 decode_options (argc
, argv
);
1273 if ((argc
- optind
< 1) && !eval
&& !tty
&& !window_system
)
1275 message (TRUE
, "%s: file name or argument required\n"
1276 "Try `%s --help' for more information\n",
1277 progname
, progname
);
1278 exit (EXIT_FAILURE
);
1281 if ((emacs_socket
= set_socket ()) == INVALID_SOCKET
)
1285 cwd
= get_current_dir_name ();
1288 /* getwd puts message in STRING if it fails. */
1289 message (TRUE
, "%s: %s\n", progname
,
1290 "Cannot get current working directory");
1298 /* First of all, send our version number for verification. */
1299 send_to_emacs (emacs_socket
, "-version ");
1300 send_to_emacs (emacs_socket
, VERSION
);
1301 send_to_emacs (emacs_socket
, " ");
1303 /* Send over our environment. */
1306 extern char **environ
;
1308 for (i
= 0; environ
[i
]; i
++)
1310 char *name
= xstrdup (environ
[i
]);
1311 char *value
= strchr (name
, '=');
1312 send_to_emacs (emacs_socket
, "-env ");
1313 quote_argument (emacs_socket
, environ
[i
]);
1314 send_to_emacs (emacs_socket
, " ");
1318 /* Send over our current directory. */
1321 send_to_emacs (emacs_socket
, "-dir ");
1322 quote_argument (emacs_socket
, cwd
);
1323 send_to_emacs (emacs_socket
, "/");
1324 send_to_emacs (emacs_socket
, " ");
1329 send_to_emacs (emacs_socket
, "-nowait ");
1332 send_to_emacs (emacs_socket
, "-current-frame ");
1336 send_to_emacs (emacs_socket
, "-display ");
1337 quote_argument (emacs_socket
, display
);
1338 send_to_emacs (emacs_socket
, " ");
1343 char *tty_name
= NULL
;
1345 tty_name
= ttyname (fileno (stdin
));
1347 char *type
= getenv ("TERM");
1351 message (TRUE
, "%s: could not get terminal name\n", progname
);
1357 message (TRUE
, "%s: please set the TERM variable to your terminal type\n",
1362 if (! strcmp (type
, "eterm"))
1364 /* This causes nasty, MULTI_KBOARD-related input lockouts. */
1365 message (TRUE
, "%s: opening a frame in an Emacs term buffer"
1366 " is not supported\n", progname
);
1369 #if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
1373 send_to_emacs (emacs_socket
, "-tty ");
1374 quote_argument (emacs_socket
, tty_name
);
1375 send_to_emacs (emacs_socket
, " ");
1376 quote_argument (emacs_socket
, type
);
1377 send_to_emacs (emacs_socket
, " ");
1381 send_to_emacs (emacs_socket
, "-window-system ");
1383 if ((argc
- optind
> 0))
1385 for (i
= optind
; i
< argc
; i
++)
1391 /* Don't prepend cwd or anything like that. */
1392 send_to_emacs (emacs_socket
, "-eval ");
1393 quote_argument (emacs_socket
, argv
[i
]);
1394 send_to_emacs (emacs_socket
, " ");
1398 if (*argv
[i
] == '+')
1400 char *p
= argv
[i
] + 1;
1401 while (isdigit ((unsigned char) *p
) || *p
== ':') p
++;
1404 send_to_emacs (emacs_socket
, "-position ");
1405 quote_argument (emacs_socket
, argv
[i
]);
1406 send_to_emacs (emacs_socket
, " ");
1412 else if (! file_name_absolute_p (argv
[i
]))
1415 send_to_emacs (emacs_socket
, "-file ");
1418 quote_argument (emacs_socket
, cwd
);
1419 send_to_emacs (emacs_socket
, "/");
1421 quote_argument (emacs_socket
, argv
[i
]);
1422 send_to_emacs (emacs_socket
, " ");
1427 if (!tty
&& !window_system
)
1429 while ((str
= fgets (string
, BUFSIZ
, stdin
)))
1432 send_to_emacs (emacs_socket
, "-eval ");
1434 send_to_emacs (emacs_socket
, "-file ");
1435 quote_argument (emacs_socket
, str
);
1437 send_to_emacs (emacs_socket
, " ");
1441 send_to_emacs (emacs_socket
, "\n");
1443 /* Wait for an answer. */
1444 if (!eval
&& !tty
&& !nowait
)
1446 printf ("Waiting for Emacs...");
1452 /* Now, wait for an answer and print any messages. */
1453 while ((rl
= recv (emacs_socket
, string
, BUFSIZ
, 0)) > 0)
1458 p
= string
+ strlen (string
) - 1;
1459 while (p
> string
&& *p
== '\n')
1462 if (strprefix ("-good-version ", string
))
1464 /* -good-version: The versions match. */
1466 else if (strprefix ("-emacs-pid ", string
))
1468 /* -emacs-pid PID: The process id of the Emacs process. */
1469 emacs_pid
= strtol (string
+ strlen ("-emacs-pid"), NULL
, 10);
1471 else if (strprefix ("-window-system-unsupported ", string
))
1473 /* -window-system-unsupported: Emacs was compiled without X
1474 support. Try again on the terminal. */
1480 else if (strprefix ("-print ", string
))
1482 /* -print STRING: Print STRING on the terminal. */
1483 str
= unquote_argument (string
+ strlen ("-print "));
1487 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
1489 else if (strprefix ("-error ", string
))
1491 /* -error DESCRIPTION: Signal an error on the terminal. */
1492 str
= unquote_argument (string
+ strlen ("-error "));
1495 fprintf (stderr
, "*ERROR*: %s", str
);
1496 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
1499 else if (strprefix ("-suspend ", string
))
1501 /* -suspend: Suspend this terminal, i.e., stop the process. */
1510 /* Unknown command. */
1513 printf ("*ERROR*: Unknown message: %s", string
);
1514 needlf
= string
[0] == '\0' ? needlf
: string
[strlen (string
) - 1] != '\n';
1523 CLOSE_SOCKET (emacs_socket
);
1524 return EXIT_SUCCESS
;
1527 #endif /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
1530 #ifndef HAVE_STRERROR
1535 extern char *sys_errlist
[];
1536 extern int sys_nerr
;
1538 if (errnum
>= 0 && errnum
< sys_nerr
)
1539 return sys_errlist
[errnum
];
1540 return (char *) "Unknown error";
1543 #endif /* ! HAVE_STRERROR */
1545 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
1546 (do not change this comment) */
1548 /* emacsclient.c ends here */