]>
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 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"
111 #define SEND_STRING(data) (send_to_emacs (s, (data)))
112 #define SEND_QUOTED(data) (quote_argument (s, (data)))
115 #define EXIT_SUCCESS 0
119 #define EXIT_FAILURE 1
134 /* Name used to invoke this program. */
137 /* The first argument to main. */
140 /* The second argument to main. */
143 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
146 /* Nonzero means args are expressions to be evaluated. --eval. */
149 /* Nonzero means don't open a new frame. --current-frame. */
150 int current_frame
= 0;
152 /* Nonzero means open a new graphical frame. */
153 int window_system
= 0;
155 /* The display on which Emacs should work. --display. */
156 char *display
= NULL
;
158 /* Nonzero means open a new Emacs frame on the current terminal. */
161 /* If non-NULL, the name of an editor to fallback to if the server
162 is not running. --alternate-editor. */
163 const char *alternate_editor
= NULL
;
165 /* If non-NULL, the filename of the UNIX socket. */
166 char *socket_name
= NULL
;
168 /* If non-NULL, the filename of the authentication file. */
169 char *server_file
= NULL
;
171 /* PID of the Emacs server process. */
174 /* Socket used to communicate with the Emacs server process. */
177 void print_help_and_exit () NO_RETURN
;
179 struct option longopts
[] =
181 { "no-wait", no_argument
, NULL
, 'n' },
182 { "eval", no_argument
, NULL
, 'e' },
183 { "help", no_argument
, NULL
, 'H' },
184 { "version", no_argument
, NULL
, 'V' },
185 { "tty", no_argument
, NULL
, 't' },
186 { "current-frame", no_argument
, NULL
, 'c' },
187 { "alternate-editor", required_argument
, NULL
, 'a' },
188 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
189 { "socket-name", required_argument
, NULL
, 's' },
191 { "server-file", required_argument
, NULL
, 'f' },
192 { "display", required_argument
, NULL
, 'd' },
197 /* Like malloc but get fatal error if memory is exhausted. */
203 long *result
= (long *) malloc (size
);
212 /* Like strdup but get a fatal error if memory is exhausted. */
215 xstrdup (const char *s
)
217 char *result
= strdup (s
);
227 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
229 /* Return the current working directory. Returns NULL on errors.
230 Any other returned value must be freed with free. This is used
231 only when get_current_dir_name is not defined on the system. */
233 get_current_dir_name ()
237 struct stat dotstat
, pwdstat
;
238 /* If PWD is accurate, use it instead of calling getwd. PWD is
239 sometimes a nicer name, and using it may avoid a fatal error if a
240 parent directory is searchable but not readable. */
241 if ((pwd
= getenv ("PWD")) != 0
242 && (IS_DIRECTORY_SEP (*pwd
) || (*pwd
&& IS_DEVICE_SEP (pwd
[1])))
243 && stat (pwd
, &pwdstat
) == 0
244 && stat (".", &dotstat
) == 0
245 && dotstat
.st_ino
== pwdstat
.st_ino
246 && dotstat
.st_dev
== pwdstat
.st_dev
248 && strlen (pwd
) < MAXPATHLEN
252 buf
= (char *) xmalloc (strlen (pwd
) + 1);
260 size_t buf_size
= 1024;
261 buf
= (char *) xmalloc (buf_size
);
266 if (getcwd (buf
, buf_size
) == buf
)
270 int tmp_errno
= errno
;
276 buf
= (char *) realloc (buf
, buf_size
);
284 /* We need MAXPATHLEN here. */
285 buf
= (char *) xmalloc (MAXPATHLEN
+ 1);
288 if (getwd (buf
) == NULL
)
290 int tmp_errno
= errno
;
301 /* Message functions. */
307 static int window_app
= -1;
308 char szTitle
[MAX_PATH
];
311 /* Checking for STDOUT does not work; it's a valid handle also in
312 nonconsole apps. Testing for the console title seems to work. */
313 window_app
= (GetConsoleTitleA (szTitle
, MAX_PATH
) == 0);
320 message (int is_error
, char *message
, ...)
325 va_start (args
, message
);
326 vsprintf (msg
, message
, args
);
330 if (w32_window_app ())
333 MessageBox (NULL
, msg
, "Emacsclient ERROR", MB_ICONERROR
);
335 MessageBox (NULL
, msg
, "Emacsclient", MB_ICONINFORMATION
);
340 FILE *f
= is_error
? stderr
: stdout
;
347 /* Decode the options from argv and argc.
348 The global variable `optind' will say how many arguments we used up. */
351 decode_options (argc
, argv
)
355 alternate_editor
= getenv ("ALTERNATE_EDITOR");
356 display
= getenv ("DISPLAY");
357 if (display
&& strlen (display
) == 0)
362 int opt
= getopt_long (argc
, argv
,
363 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
376 /* If getopt returns 0, then it has already processed a
377 long-named option. We should do nothing. */
381 alternate_editor
= optarg
;
384 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
386 socket_name
= optarg
;
391 server_file
= optarg
;
407 message (FALSE
, "emacsclient %s\n", VERSION
);
420 print_help_and_exit ();
424 message (TRUE
, "Try `%s --help' for more information\n", progname
);
435 /* --no-wait implies --current-frame on ttys when there are file
436 arguments or expressions given. */
437 if (nowait
&& tty
&& argc
- optind
> 0)
452 print_help_and_exit ()
455 "Usage: %s [OPTIONS] FILE...\n\
456 Tell the Emacs server to visit the specified files.\n\
457 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
459 The following OPTIONS are accepted:\n\
460 -V, --version Just print version info and return\n\
461 -H, --help Print this usage information message\n\
462 -t, --tty Open a new Emacs frame on the current terminal\n\
463 -c, --current-frame Do not create a new frame; use the current Emacs frame\n\
464 -e, --eval Evaluate the FILE arguments as ELisp expressions\n\
465 -n, --no-wait Don't wait for the server to return\n\
466 -d, --display=DISPLAY Visit the file in the given display\n"
467 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
468 "-s, --socket-name=FILENAME\n\
469 Set filename of the UNIX socket for communication\n"
471 "-f, --server-file=FILENAME\n\
472 Set filename of the TCP authentication file\n\
473 -a, --alternate-editor=EDITOR\n\
474 Editor to fallback to if the server is not running\n\
476 Report bugs to bug-gnu-emacs@gnu.org.\n", progname
);
481 Try to run a different command, or --if no alternate editor is
482 defined-- exit with an errorcode.
487 if (alternate_editor
)
491 execvp (alternate_editor
, main_argv
+ i
);
492 message (TRUE
, "%s: error executing alternate editor \"%s\"\n",
493 progname
, alternate_editor
);
499 #if !defined (HAVE_SOCKETS) || !defined (HAVE_INET_SOCKETS)
509 message (TRUE
, "%s: Sorry, the Emacs server is supported only\n"
510 "on systems with Berkeley sockets.\n",
515 #else /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
518 # include <winsock2.h>
520 # include <sys/types.h>
521 # include <sys/socket.h>
523 # include <sys/stat.h>
527 #define AUTH_KEY_LENGTH 64
528 #define SEND_BUFFER_SIZE 4096
530 extern char *strerror ();
533 /* Buffer to accumulate data to send in TCP connections. */
534 char send_buffer
[SEND_BUFFER_SIZE
+ 1];
535 int sblen
= 0; /* Fill pointer for the send buffer. */
537 /* Let's send the data to Emacs when either
538 - the data ends in "\n", or
539 - the buffer is full (but this shouldn't happen)
540 Otherwise, we just accumulate it. */
542 send_to_emacs (s
, data
)
548 int dlen
= strlen (data
);
549 if (dlen
+ sblen
>= SEND_BUFFER_SIZE
)
551 int part
= SEND_BUFFER_SIZE
- sblen
;
552 strncpy (&send_buffer
[sblen
], data
, part
);
554 sblen
= SEND_BUFFER_SIZE
;
558 strcpy (&send_buffer
[sblen
], data
);
565 if (sblen
== SEND_BUFFER_SIZE
566 || (sblen
> 0 && send_buffer
[sblen
-1] == '\n'))
568 int sent
= send (s
, send_buffer
, sblen
, 0);
570 strcpy (send_buffer
, &send_buffer
[sent
]);
577 /* In STR, insert a & before each &, each space, each newline, and
578 any initial -. Change spaces to underscores, too, so that the
579 return value never contains a space.
581 Does not change the string. Outputs the result to STREAM. */
583 quote_argument (s
, str
)
587 char *copy
= (char *) xmalloc (strlen (str
) * 2 + 1);
608 if (*p
== '&' || (*p
== '-' && p
== str
))
621 /* The inverse of quote_argument. Removes quoting in string STR by
622 modifying the string in place. Returns STR. */
625 unquote_argument (str
)
657 file_name_absolute_p (filename
)
658 const unsigned char *filename
;
660 /* Sanity check, it shouldn't happen. */
661 if (! filename
) return FALSE
;
663 /* /xxx is always an absolute path. */
664 if (filename
[0] == '/') return TRUE
;
666 /* Empty filenames (which shouldn't happen) are relative. */
667 if (filename
[0] == '\0') return FALSE
;
670 /* X:\xxx is always absolute; X:xxx is an error and will fail. */
671 if (isalpha (filename
[0])
672 && filename
[1] == ':' && (filename
[2] == '\\' || filename
[2] == '/'))
675 /* Both \xxx and \\xxx\yyy are absolute. */
676 if (filename
[0] == '\\') return TRUE
;
683 /* Wrapper to make WSACleanup a cdecl, as required by atexit. */
685 __cdecl
close_winsock ()
690 /* Initialize the WinSock2 library. */
692 initialize_sockets ()
696 if (WSAStartup (MAKEWORD (2, 0), &wsaData
))
698 message (TRUE
, "%s: error initializing WinSock2", progname
);
702 atexit (close_winsock
);
704 #endif /* WINDOWSNT */
710 execvp wrapper for Windows. Quotes arguments with embedded spaces.
712 This is necessary due to the broken implementation of exec* routines in
713 the Microsoft libraries: they concatenate the arguments together without
714 quoting special characters, and pass the result to CreateProcess, with
715 predictably bad results. By contrast, Posix execvp passes the arguments
716 directly into the argv array of the child process.
719 w32_execvp (path
, argv
)
725 /* Required to allow a .BAT script as alternate editor. */
726 argv
[0] = (char *) alternate_editor
;
728 for (i
= 0; argv
[i
]; i
++)
729 if (strchr (argv
[i
], ' '))
731 char *quoted
= alloca (strlen (argv
[i
]) + 3);
732 sprintf (quoted
, "\"%s\"", argv
[i
]);
736 return execvp (path
, argv
);
740 #define execvp w32_execvp
742 #endif /* WINDOWSNT */
745 * Read the information needed to set up a TCP comm channel with
746 * the Emacs server: host, port, pid and authentication string.
749 get_server_config (server
, authentication
)
750 struct sockaddr_in
*server
;
751 char *authentication
;
758 if (file_name_absolute_p (server_file
))
759 config
= fopen (server_file
, "rb");
762 char *home
= getenv ("HOME");
766 char *path
= alloca (32 + strlen (home
) + strlen (server_file
));
767 sprintf (path
, "%s/.emacs.d/server/%s", home
, server_file
);
768 config
= fopen (path
, "rb");
771 if (!config
&& (home
= getenv ("APPDATA")))
773 char *path
= alloca (32 + strlen (home
) + strlen (server_file
));
774 sprintf (path
, "%s/.emacs.d/server/%s", home
, server_file
);
775 config
= fopen (path
, "rb");
783 if (fgets (dotted
, sizeof dotted
, config
)
784 && (port
= strchr (dotted
, ':'))
785 && (pid
= strchr (port
, ' ')))
792 message (TRUE
, "%s: invalid configuration info", progname
);
796 server
->sin_family
= AF_INET
;
797 server
->sin_addr
.s_addr
= inet_addr (dotted
);
798 server
->sin_port
= htons (atoi (port
));
800 if (! fread (authentication
, AUTH_KEY_LENGTH
, 1, config
))
802 message (TRUE
, "%s: cannot read authentication info", progname
);
808 emacs_pid
= atoi (pid
);
817 struct sockaddr_in server
;
818 struct linger l_arg
= {1, 1};
819 char auth_string
[AUTH_KEY_LENGTH
+ 1];
821 if (! get_server_config (&server
, auth_string
))
822 return INVALID_SOCKET
;
824 if (server
.sin_addr
.s_addr
!= inet_addr ("127.0.0.1"))
825 message (FALSE
, "%s: connected to remote socket at %s\n",
826 progname
, inet_ntoa (server
.sin_addr
));
829 * Open up an AF_INET socket
831 if ((s
= socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
833 message (TRUE
, "%s: socket: %s\n", progname
, strerror (errno
));
834 return INVALID_SOCKET
;
840 if (connect (s
, (struct sockaddr
*) &server
, sizeof server
) < 0)
842 message (TRUE
, "%s: connect: %s\n", progname
, strerror (errno
));
843 return INVALID_SOCKET
;
846 setsockopt (s
, SOL_SOCKET
, SO_LINGER
, (char *) &l_arg
, sizeof l_arg
);
849 * Send the authentication
851 auth_string
[AUTH_KEY_LENGTH
] = '\0';
853 SEND_STRING ("-auth ");
854 SEND_STRING (auth_string
);
860 #if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
862 /* Three possibilities:
863 2 - can't be `stat'ed (sets errno)
864 1 - isn't owned by us
865 0 - success: none of the above */
868 socket_status (socket_name
)
873 if (stat (socket_name
, &statbfr
) == -1)
876 if (statbfr
.st_uid
!= geteuid ())
883 /* A signal handler that passes the signal to the Emacs process.
884 Useful for SIGWINCH. */
887 pass_signal_to_emacs (int signalnum
)
889 int old_errno
= errno
;
892 kill (emacs_pid
, signalnum
);
894 signal (signalnum
, pass_signal_to_emacs
);
898 /* Signal handler for SIGCONT; notify the Emacs process that it can
899 now resume our tty frame. */
902 handle_sigcont (int signalnum
)
904 int old_errno
= errno
;
906 if (tcgetpgrp (1) == getpgrp ())
908 /* We are in the foreground. */
909 SEND_STRING ("-resume \n");
913 /* We are in the background; cancel the continue. */
914 kill (getpid (), SIGSTOP
);
917 signal (signalnum
, handle_sigcont
);
921 /* Signal handler for SIGTSTP; notify the Emacs process that we are
922 going to sleep. Normally the suspend is initiated by Emacs via
923 server-handle-suspend-tty, but if the server gets out of sync with
924 reality, we may get a SIGTSTP on C-z. Handling this signal and
925 notifying Emacs about it should get things under control again. */
928 handle_sigtstp (int signalnum
)
930 int old_errno
= errno
;
934 SEND_STRING ("-suspend \n");
936 /* Unblock this signal and call the default handler by temprarily
937 changing the handler and resignalling. */
938 sigprocmask (SIG_BLOCK
, NULL
, &set
);
939 sigdelset (&set
, signalnum
);
940 signal (signalnum
, SIG_DFL
);
941 kill (getpid (), signalnum
);
942 sigprocmask (SIG_SETMASK
, &set
, NULL
); /* Let's the above signal through. */
943 signal (signalnum
, handle_sigtstp
);
948 /* Set up signal handlers before opening a frame on the current tty. */
953 /* Set up signal handlers. */
954 signal (SIGWINCH
, pass_signal_to_emacs
);
956 /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of
957 deciding which terminal the signal came from. C-g is now a
958 normal input event on secondary terminals. */
960 signal (SIGINT
, pass_signal_to_emacs
);
961 signal (SIGQUIT
, pass_signal_to_emacs
);
964 signal (SIGCONT
, handle_sigcont
);
965 signal (SIGTSTP
, handle_sigtstp
);
966 signal (SIGTTOU
, handle_sigtstp
);
971 /* Returns 1 if PREFIX is a prefix of STRING. */
973 strprefix (char *prefix
, char *string
)
982 for (i
= 0; prefix
[i
]; i
++)
983 if (!string
[i
] || string
[i
] != prefix
[i
])
993 struct sockaddr_un server
;
996 * Open up an AF_UNIX socket in this person's home directory
999 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
1001 message (TRUE
, "%s: socket: %s\n", progname
, strerror (errno
));
1002 return INVALID_SOCKET
;
1005 server
.sun_family
= AF_UNIX
;
1008 int sock_status
= 0;
1009 int default_sock
= !socket_name
;
1010 int saved_errno
= 0;
1012 char *server_name
= "server";
1014 if (socket_name
&& !index (socket_name
, '/') && !index (socket_name
, '\\'))
1015 { /* socket_name is a file name component. */
1016 server_name
= socket_name
;
1018 default_sock
= 1; /* Try both UIDs. */
1023 socket_name
= alloca (100 + strlen (server_name
));
1024 sprintf (socket_name
, "/tmp/emacs%d/%s",
1025 (int) geteuid (), server_name
);
1028 if (strlen (socket_name
) < sizeof (server
.sun_path
))
1029 strcpy (server
.sun_path
, socket_name
);
1032 message (TRUE
, "%s: socket-name %s too long",
1033 progname
, socket_name
);
1037 /* See if the socket exists, and if it's owned by us. */
1038 sock_status
= socket_status (server
.sun_path
);
1039 saved_errno
= errno
;
1040 if (sock_status
&& default_sock
)
1042 /* Failing that, see if LOGNAME or USER exist and differ from
1043 our euid. If so, look for a socket based on the UID
1044 associated with the name. This is reminiscent of the logic
1045 that init_editfns uses to set the global Vuser_full_name. */
1047 char *user_name
= (char *) getenv ("LOGNAME");
1050 user_name
= (char *) getenv ("USER");
1054 struct passwd
*pw
= getpwnam (user_name
);
1056 if (pw
&& (pw
->pw_uid
!= geteuid ()))
1058 /* We're running under su, apparently. */
1059 socket_name
= alloca (100 + strlen (server_name
));
1060 sprintf (socket_name
, "/tmp/emacs%d/%s",
1061 (int) pw
->pw_uid
, server_name
);
1063 if (strlen (socket_name
) < sizeof (server
.sun_path
))
1064 strcpy (server
.sun_path
, socket_name
);
1067 message (TRUE
, "%s: socket-name %s too long",
1068 progname
, socket_name
);
1069 exit (EXIT_FAILURE
);
1072 sock_status
= socket_status (server
.sun_path
);
1073 saved_errno
= errno
;
1076 errno
= saved_errno
;
1080 switch (sock_status
)
1083 /* There's a socket, but it isn't owned by us. This is OK if
1085 if (0 != geteuid ())
1087 message (TRUE
, "%s: Invalid socket owner\n", progname
);
1088 return INVALID_SOCKET
;
1094 if (saved_errno
== ENOENT
)
1096 "%s: can't find socket; have you started the server?\n\
1097 To start the server in Emacs, type \"M-x server-start\".\n",
1100 message (TRUE
, "%s: can't stat %s: %s\n",
1101 progname
, server
.sun_path
, strerror (saved_errno
));
1102 return INVALID_SOCKET
;
1106 if (connect (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2)
1109 message (TRUE
, "%s: connect: %s\n", progname
, strerror (errno
));
1110 return INVALID_SOCKET
;
1115 #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
1124 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1125 /* Explicit --socket-name argument. */
1128 s
= set_local_socket ();
1129 if ((s
!= INVALID_SOCKET
) || alternate_editor
)
1131 message (TRUE
, "%s: error accessing socket \"%s\"",
1132 progname
, socket_name
);
1133 exit (EXIT_FAILURE
);
1137 /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */
1139 server_file
= getenv ("EMACS_SERVER_FILE");
1143 s
= set_tcp_socket ();
1144 if ((s
!= INVALID_SOCKET
) || alternate_editor
)
1147 message (TRUE
, "%s: error accessing server file \"%s\"",
1148 progname
, server_file
);
1149 exit (EXIT_FAILURE
);
1152 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1153 /* Implicit local socket. */
1154 s
= set_local_socket ();
1155 if (s
!= INVALID_SOCKET
)
1159 /* Implicit server file. */
1160 server_file
= "server";
1161 s
= set_tcp_socket ();
1162 if ((s
!= INVALID_SOCKET
) || alternate_editor
)
1165 /* No implicit or explicit socket, and no alternate editor. */
1166 message (TRUE
, "%s: No socket or alternate editor. Please use:\n\n"
1167 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1170 "\t--server-file (or environment variable EMACS_SERVER_FILE)\n\
1171 \t--alternate-editor (or environment variable ALTERNATE_EDITOR)\n",
1173 exit (EXIT_FAILURE
);
1177 FARPROC set_fg
; /* Pointer to AllowSetForegroundWindow. */
1178 FARPROC get_wc
; /* Pointer to RealGetWindowClassA. */
1181 w32_find_emacs_process (hWnd
, lParam
)
1188 /* Reject any window not of class "Emacs". */
1189 if (! get_wc (hWnd
, class, sizeof (class))
1190 || strcmp (class, "Emacs"))
1193 /* We only need the process id, not the thread id. */
1194 (void) GetWindowThreadProcessId (hWnd
, &pid
);
1196 /* Not the one we're looking for. */
1197 if (pid
!= (DWORD
) emacs_pid
) return TRUE
;
1199 /* OK, let's raise it. */
1202 /* Stop enumeration. */
1207 * Search for a window of class "Emacs" and owned by a process with
1208 * process id = emacs_pid. If found, allow it to grab the focus.
1215 /* It should'nt happen when dealing with TCP sockets. */
1216 if (!emacs_pid
) return;
1218 if (!(hUser32
= LoadLibrary ("user32.dll"))) return;
1220 /* Modern Windows restrict which processes can set the foreground window.
1221 emacsclient can allow Emacs to grab the focus by calling the function
1222 AllowSetForegroundWindow. Unfortunately, older Windows (W95, W98 and
1223 NT) lack this function, so we have to check its availability. */
1224 if ((set_fg
= GetProcAddress (hUser32
, "AllowSetForegroundWindow"))
1225 && (get_wc
= GetProcAddress (hUser32
, "RealGetWindowClassA")))
1226 EnumWindows (w32_find_emacs_process
, (LPARAM
) 0);
1228 FreeLibrary (hUser32
);
1237 int i
, rl
, needlf
= 0;
1239 char string
[BUFSIZ
+1];
1245 /* Process options. */
1246 decode_options (argc
, argv
);
1248 if ((argc
- optind
< 1) && !eval
&& !tty
&& !window_system
)
1250 message (TRUE
, "%s: file name or argument required\n"
1251 "Try `%s --help' for more information\n",
1252 progname
, progname
);
1253 exit (EXIT_FAILURE
);
1256 if ((s
= set_socket ()) == INVALID_SOCKET
)
1260 cwd
= get_current_dir_name ();
1263 /* getwd puts message in STRING if it fails. */
1264 message (TRUE
, "%s: %s\n", progname
,
1265 "Cannot get current working directory");
1273 /* First of all, send our version number for verification. */
1274 SEND_STRING ("-version ");
1275 SEND_STRING (VERSION
);
1278 /* Send over our environment. */
1281 extern char **environ
;
1283 for (i
= 0; environ
[i
]; i
++)
1285 char *name
= xstrdup (environ
[i
]);
1286 char *value
= strchr (name
, '=');
1287 SEND_STRING ("-env ");
1288 SEND_QUOTED (environ
[i
]);
1293 /* Send over our current directory. */
1296 SEND_STRING ("-dir ");
1304 SEND_STRING ("-nowait ");
1307 SEND_STRING ("-current-frame ");
1311 SEND_STRING ("-display ");
1312 SEND_QUOTED (display
);
1318 char *tty_name
= ttyname (fileno (stdin
));
1319 char *type
= getenv ("TERM");
1323 message (TRUE
, "%s: could not get terminal name\n", progname
);
1329 message (TRUE
, "%s: please set the TERM variable to your terminal type\n",
1334 if (! strcmp (type
, "eterm"))
1336 /* This causes nasty, MULTI_KBOARD-related input lockouts. */
1337 message (TRUE
, "%s: opening a frame in an Emacs term buffer"
1338 " is not supported\n", progname
);
1344 SEND_STRING ("-tty ");
1345 SEND_QUOTED (tty_name
);
1352 SEND_STRING ("-window-system ");
1354 if ((argc
- optind
> 0))
1356 for (i
= optind
; i
< argc
; i
++)
1362 /* Don't prepend cwd or anything like that. */
1363 SEND_STRING ("-eval ");
1364 SEND_QUOTED (argv
[i
]);
1369 if (*argv
[i
] == '+')
1371 char *p
= argv
[i
] + 1;
1372 while (isdigit ((unsigned char) *p
) || *p
== ':') p
++;
1375 SEND_STRING ("-position ");
1376 SEND_QUOTED (argv
[i
]);
1383 else if (! file_name_absolute_p (argv
[i
]))
1386 SEND_STRING ("-file ");
1392 SEND_QUOTED (argv
[i
]);
1398 if (!tty
&& !window_system
)
1400 while ((str
= fgets (string
, BUFSIZ
, stdin
)))
1403 SEND_STRING ("-eval ");
1405 SEND_STRING ("-file ");
1414 /* Wait for an answer. */
1415 if (!eval
&& !tty
&& !nowait
)
1417 printf ("Waiting for Emacs...");
1423 /* Now, wait for an answer and print any messages. */
1424 while ((rl
= recv (s
, string
, BUFSIZ
, 0)) > 0)
1429 p
= string
+ strlen (string
) - 1;
1430 while (p
> string
&& *p
== '\n')
1433 if (strprefix ("-good-version ", string
))
1435 /* -good-version: The versions match. */
1437 else if (strprefix ("-emacs-pid ", string
))
1439 /* -emacs-pid PID: The process id of the Emacs process. */
1440 emacs_pid
= strtol (string
+ strlen ("-emacs-pid"), NULL
, 10);
1442 else if (strprefix ("-window-system-unsupported ", string
))
1444 /* -window-system-unsupported: Emacs was compiled without X
1445 support. Try again on the terminal. */
1451 else if (strprefix ("-print ", string
))
1453 /* -print STRING: Print STRING on the terminal. */
1454 str
= unquote_argument (string
+ strlen ("-print "));
1458 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
1460 else if (strprefix ("-error ", string
))
1462 /* -error DESCRIPTION: Signal an error on the terminal. */
1463 str
= unquote_argument (string
+ strlen ("-error "));
1466 fprintf (stderr
, "*ERROR*: %s", str
);
1467 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
1469 else if (strprefix ("-suspend ", string
))
1471 /* -suspend: Suspend this terminal, i.e., stop the process. */
1479 /* Unknown command. */
1482 printf ("*ERROR*: Unknown message: %s", string
);
1483 needlf
= string
[0] == '\0' ? needlf
: string
[strlen (string
) - 1] != '\n';
1493 return EXIT_SUCCESS
;
1496 #endif /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
1499 #ifndef HAVE_STRERROR
1504 extern char *sys_errlist
[];
1505 extern int sys_nerr
;
1507 if (errnum
>= 0 && errnum
< sys_nerr
)
1508 return sys_errlist
[errnum
];
1509 return (char *) "Unknown error";
1512 #endif /* ! HAVE_STRERROR */
1514 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
1515 (do not change this comment) */
1517 /* emacsclient.c ends here */