.\" See section COPYING for conditions for redistribution.
-.TH EMACSCLIENT 1 "2021-11-05" "GNU Emacs" "GNU"
+.TH EMACSCLIENT 1 "2022-09-05" "GNU Emacs" "GNU"
.\" NAME should be all caps, SECTION should be 1-8, maybe w/ subsection
.\" other params are allowed: see man(7), man(1)
.SH NAME
This can also be specified via the EMACS_SERVER_FILE environment variable.
.TP
.B \-n, \-\-no-wait
-Return
-immediately without waiting for you to "finish" the buffer in Emacs.
-If combined with --eval, this option is ignored.
+Return immediately without waiting for you to "finish" the buffer in
+Emacs. If combined with --eval, this option is ignored.
+.TP
+.B \-w, \-\-timeout=N
+How long to wait, in seconds, for Emacs to respond before giving up.
+The default is 0, which means to wait forever.
.TP
.B \-nw, \-t, \-\-tty
Open a new Emacs frame on the current terminal.
/* Client process that communicates with GNU Emacs acting as server.
-Copyright (C) 1986-1987, 1994, 1999-2022 Free Software Foundation, Inc.
+Copyright (C) 1986-2022 Free Software Foundation, Inc.
This file is part of GNU Emacs.
# include <sys/socket.h>
# include <sys/un.h>
+# define DEFAULT_TIMEOUT (30)
+
# define SOCKETS_IN_FILE_SYSTEM
# define INVALID_SOCKET (-1)
/* If non-NULL, the filename of the authentication file. */
static char const *server_file;
+/* Seconds to wait before timing out (0 means wait forever). */
+static uintmax_t timeout;
+
/* If non-NULL, the tramp prefix emacs must use to find the files. */
static char const *tramp_prefix;
{ "server-file", required_argument, NULL, 'f' },
{ "display", required_argument, NULL, 'd' },
{ "parent-id", required_argument, NULL, 'p' },
+ { "timeout", required_argument, NULL, 'w' },
{ "tramp", required_argument, NULL, 'T' },
{ 0, 0, 0, 0 }
};
/* Short options, in the same order as the corresponding long options.
There is no '-p' short option. */
static char const shortopts[] =
- "nqueHVtca:F:"
+ "nqueHVtca:F:w:"
#ifdef SOCKETS_IN_FILE_SYSTEM
"s:"
#endif
if (opt < 0)
break;
+ char* endptr;
switch (opt)
{
case 0:
nowait = true;
break;
+ case 'w':
+ timeout = strtoumax (optarg, &endptr, 10);
+ if (timeout <= 0 ||
+ ((timeout == INTMAX_MAX || timeout == INTMAX_MIN)
+ && errno == ERANGE))
+ {
+ fprintf (stderr, "Invalid timeout: \"%s\"\n", optarg);
+ exit (EXIT_FAILURE);
+ }
+ break;
+
case 'e':
eval = true;
break;
Set the parameters of a new frame\n\
-e, --eval Evaluate the FILE arguments as ELisp expressions\n\
-n, --no-wait Don't wait for the server to return\n\
+-w, --timeout Seconds to wait before timing out\n\
-q, --quiet Don't display messages on success\n\
-u, --suppress-output Don't display return values from the server\n\
-d DISPLAY, --display=DISPLAY\n\
return emacs_socket;
}
+static void
+set_socket_timeout (HSOCKET socket, int seconds)
+{
+#ifndef WINDOWSNT
+ struct timeval timeout;
+ timeout.tv_sec = seconds;
+ timeout.tv_usec = 0;
+ setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
+#else
+ DWORD timeout = seconds * 1000;
+ setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, sizeof timeout);
+#endif
+}
+
+static bool
+check_socket_timeout (int rl)
+{
+#ifndef WINDOWSNT
+ return (rl == -1)
+ && (errno == EAGAIN)
+ && (errno == EWOULDBLOCK);
+#else
+ return (rl == SOCKET_ERROR)
+ && (WSAGetLastError() == WSAETIMEDOUT);
+#endif
+}
+
int
main (int argc, char **argv)
{
}
fflush (stdout);
+ set_socket_timeout (emacs_socket, timeout > 0 ? timeout : DEFAULT_TIMEOUT);
+ bool saw_response = false;
/* Now, wait for an answer and print any messages. */
while (exit_status == EXIT_SUCCESS)
{
+ bool retry = true;
+ bool msg_showed = quiet;
do
{
act_on_signals (emacs_socket);
rl = recv (emacs_socket, string, BUFSIZ, 0);
+ retry = check_socket_timeout (rl);
+ if (retry)
+ {
+ if (timeout > 0 && !saw_response)
+ {
+ /* Don't retry if we were given a --timeout flag. */
+ fprintf (stderr, "\nServer not responding; timed out after %lu seconds",
+ timeout);
+ retry = false;
+ }
+ else if (!msg_showed)
+ {
+ msg_showed = true;
+ fprintf (stderr, "\nServer not responding; use Ctrl+C to break");
+ }
+ }
}
- while (rl < 0 && errno == EINTR);
+ while ((rl < 0 && errno == EINTR) || retry);
if (rl <= 0)
break;
+ if (msg_showed)
+ fprintf (stderr, "\nGot response from server");
+ saw_response = true;
string[rl] = '\0';
/* Loop over all NL-terminated messages. */