blob: 8e36808a4bee2060676a3e2decfcf3232ecd5065 [file] [log] [blame]
Bram Moolenaare0874f82016-01-24 20:36:41 +01001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
15#if defined(FEAT_CHANNEL) || defined(PROTO)
16
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/*
18 * Change the zero to 1 to enable debugging.
19 * This will write a file "channel_debug.log".
20 */
21#if 0
22# define CHERROR(fmt, arg) cherror(fmt, arg)
23# define CHLOG(idx, send, buf) chlog(idx, send, buf)
24# define CHFILE "channel_debug.log"
25
26static void cherror(char *fmt, char *arg);
27static void chlog(int send, char_u *buf);
28#else
29# define CHERROR(fmt, arg)
30# define CHLOG(idx, send, buf)
31#endif
32
33/* TRUE when netbeans is running with a GUI. */
34#ifdef FEAT_GUI
35# define CH_HAS_GUI (gui.in_use || gui.starting)
36#endif
37
38/* Note: when making changes here also adjust configure.in. */
39#ifdef WIN32
40/* WinSock API is separated from C API, thus we can't use read(), write(),
41 * errno... */
42# define SOCK_ERRNO errno = WSAGetLastError()
43# undef ECONNREFUSED
44# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010045# undef EWOULDBLOCK
46# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard04a0202016-01-26 23:30:18 +010047# ifdef EINTR
48# undef EINTR
49# endif
50# define EINTR WSAEINTR
51# define sock_write(sd, buf, len) send(sd, buf, len, 0)
52# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
53# define sock_close(sd) closesocket(sd)
54# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
55#else
56# include <netdb.h>
57# include <netinet/in.h>
58
59# include <sys/socket.h>
60# ifdef HAVE_LIBGEN_H
61# include <libgen.h>
62# endif
63# define SOCK_ERRNO
64# define sock_write(sd, buf, len) write(sd, buf, len)
65# define sock_read(sd, buf, len) read(sd, buf, len)
66# define sock_close(sd) close(sd)
67#endif
68
69#ifdef FEAT_GUI_W32
70extern HWND s_hwnd; /* Gvim's Window handle */
71#endif
72
73struct readqueue
74{
75 char_u *buffer;
76 struct readqueue *next;
77 struct readqueue *prev;
78};
Bram Moolenaar19d2f152016-02-01 21:38:19 +010079typedef struct readqueue readq_T;
80
81struct jsonqueue
82{
83 typval_T *value;
84 struct jsonqueue *next;
85 struct jsonqueue *prev;
86};
87typedef struct jsonqueue jsonq_T;
Bram Moolenaard04a0202016-01-26 23:30:18 +010088
Bram Moolenaara07fec92016-02-05 21:04:08 +010089struct cbqueue
90{
91 char_u *callback;
92 int seq_nr;
93 struct cbqueue *next;
94 struct cbqueue *prev;
95};
96typedef struct cbqueue cbq_T;
97
Bram Moolenaare0874f82016-01-24 20:36:41 +010098typedef struct {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010099 sock_T ch_fd; /* the socket, -1 for a closed channel */
100 int ch_idx; /* used by channel_poll_setup() */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100101 readq_T ch_head; /* dummy node, header for circular queue */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100102
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100103 int ch_error; /* When TRUE an error was reported. Avoids giving
Bram Moolenaard04a0202016-01-26 23:30:18 +0100104 * pages full of error messages when the other side
105 * has exited, only mention the first error until the
106 * connection works again. */
107#ifdef FEAT_GUI_X11
108 XtInputId ch_inputHandler; /* Cookie for input */
109#endif
110#ifdef FEAT_GUI_GTK
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100111 gint ch_inputHandler; /* Cookie for input */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100112#endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100113#ifdef WIN32
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100114 int ch_inputHandler; /* simply ret.value of WSAAsyncSelect() */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100115#endif
116
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100117 void (*ch_close_cb)(void); /* callback for when channel is closed */
118
119 char_u *ch_callback; /* function to call when a msg is not handled */
Bram Moolenaara07fec92016-02-05 21:04:08 +0100120 cbq_T ch_cb_head; /* dummy node for pre-request callbacks */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100121
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100122 ch_mode_T ch_mode;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100123 jsonq_T ch_json_head; /* dummy node, header for circular queue */
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100124
125 int ch_timeout; /* request timeout in msec */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100126} channel_T;
127
Bram Moolenaard04a0202016-01-26 23:30:18 +0100128/*
129 * Information about all channels.
130 * There can be gaps for closed channels, they will be reused later.
131 */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100132static channel_T *channels = NULL;
133static int channel_count = 0;
134
135/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100136 * TODO: open debug file when desired.
137 */
138FILE *debugfd = NULL;
139
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100140#ifdef _WIN32
141# undef PERROR
142# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
143 (char_u *)msg, (char_u *)strerror_win32(errno))
144
145 static char *
146strerror_win32(int eno)
147{
148 static LPVOID msgbuf = NULL;
149 char_u *ptr;
150
151 if (msgbuf)
152 LocalFree(msgbuf);
153 FormatMessage(
154 FORMAT_MESSAGE_ALLOCATE_BUFFER |
155 FORMAT_MESSAGE_FROM_SYSTEM |
156 FORMAT_MESSAGE_IGNORE_INSERTS,
157 NULL,
158 eno,
159 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
160 (LPTSTR) &msgbuf,
161 0,
162 NULL);
163 /* chomp \r or \n */
164 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
165 switch (*ptr)
166 {
167 case '\r':
168 STRMOVE(ptr, ptr + 1);
169 ptr--;
170 break;
171 case '\n':
172 if (*(ptr + 1) == '\0')
173 *ptr = '\0';
174 else
175 *ptr = ' ';
176 break;
177 }
178 return msgbuf;
179}
180#endif
181
Bram Moolenaard04a0202016-01-26 23:30:18 +0100182/*
Bram Moolenaare0874f82016-01-24 20:36:41 +0100183 * Add a new channel slot, return the index.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100184 * The channel isn't actually used into ch_fd is set >= 0;
185 * Returns -1 if all channels are in use.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100186 */
187 static int
188add_channel(void)
189{
190 int idx;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100191 channel_T *ch;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100192
193 if (channels != NULL)
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100194 {
Bram Moolenaare0874f82016-01-24 20:36:41 +0100195 for (idx = 0; idx < channel_count; ++idx)
196 if (channels[idx].ch_fd < 0)
197 /* re-use a closed channel slot */
198 return idx;
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100199 if (channel_count == MAX_OPEN_CHANNELS)
200 return -1;
201 }
202 else
203 {
204 channels = (channel_T *)alloc((int)sizeof(channel_T)
205 * MAX_OPEN_CHANNELS);
206 if (channels == NULL)
207 return -1;
208 }
209
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100210 ch = &channels[channel_count];
211 (void)vim_memset(ch, 0, sizeof(channel_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100212
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100213 ch->ch_fd = (sock_T)-1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100214#ifdef FEAT_GUI_X11
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100215 ch->ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100216#endif
217#ifdef FEAT_GUI_GTK
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100218 ch->ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100219#endif
220#ifdef FEAT_GUI_W32
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100221 ch->ch_inputHandler = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100222#endif
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100223 /* initialize circular queues */
224 ch->ch_head.next = &ch->ch_head;
225 ch->ch_head.prev = &ch->ch_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +0100226 ch->ch_cb_head.next = &ch->ch_cb_head;
227 ch->ch_cb_head.prev = &ch->ch_cb_head;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100228 ch->ch_json_head.next = &ch->ch_json_head;
229 ch->ch_json_head.prev = &ch->ch_json_head;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100230
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100231 ch->ch_timeout = 2000;
232
Bram Moolenaare0874f82016-01-24 20:36:41 +0100233 return channel_count++;
234}
235
Bram Moolenaard04a0202016-01-26 23:30:18 +0100236#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100237/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100238 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100239 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100240#ifdef FEAT_GUI_X11
241 static void
242messageFromNetbeans(XtPointer clientData,
243 int *unused1 UNUSED,
244 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100245{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100246 channel_read((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100247}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100248#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100249
Bram Moolenaard04a0202016-01-26 23:30:18 +0100250#ifdef FEAT_GUI_GTK
251 static void
252messageFromNetbeans(gpointer clientData,
253 gint unused1 UNUSED,
254 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100255{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100256 channel_read((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100257}
258#endif
259
260 static void
Bram Moolenaard04a0202016-01-26 23:30:18 +0100261channel_gui_register(int idx)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100262{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100263 channel_T *channel = &channels[idx];
264
265 if (!CH_HAS_GUI)
266 return;
267
268# ifdef FEAT_GUI_X11
269 /* tell notifier we are interested in being called
270 * when there is input on the editor connection socket
271 */
272 if (channel->ch_inputHandler == (XtInputId)NULL)
273 channel->ch_inputHandler =
274 XtAppAddInput((XtAppContext)app_context, channel->ch_fd,
275 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100276 messageFromNetbeans, (XtPointer)(long)idx);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100277# else
278# ifdef FEAT_GUI_GTK
279 /*
280 * Tell gdk we are interested in being called when there
281 * is input on the editor connection socket
282 */
283 if (channel->ch_inputHandler == 0)
284 channel->ch_inputHandler =
285 gdk_input_add((gint)channel->ch_fd, (GdkInputCondition)
286 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
287 messageFromNetbeans, (gpointer)(long)idx);
288# else
289# ifdef FEAT_GUI_W32
290 /*
291 * Tell Windows we are interested in receiving message when there
292 * is input on the editor connection socket.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100293 */
294 if (channel->ch_inputHandler == -1)
295 channel->ch_inputHandler =
296 WSAAsyncSelect(channel->ch_fd, s_hwnd, WM_NETBEANS, FD_READ);
297# endif
298# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100299# endif
Bram Moolenaard04a0202016-01-26 23:30:18 +0100300}
301
302/*
303 * Register any of our file descriptors with the GUI event handling system.
304 * Called when the GUI has started.
305 */
306 void
307channel_gui_register_all(void)
308{
309 int i;
310
311 for (i = 0; i < channel_count; ++i)
312 if (channels[i].ch_fd >= 0)
313 channel_gui_register(i);
314}
315
316 static void
317channel_gui_unregister(int idx)
318{
319 channel_T *channel = &channels[idx];
320
321# ifdef FEAT_GUI_X11
322 if (channel->ch_inputHandler != (XtInputId)NULL)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100323 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100324 XtRemoveInput(channel->ch_inputHandler);
325 channel->ch_inputHandler = (XtInputId)NULL;
326 }
327# else
328# ifdef FEAT_GUI_GTK
329 if (channel->ch_inputHandler != 0)
330 {
331 gdk_input_remove(channel->ch_inputHandler);
332 channel->ch_inputHandler = 0;
333 }
334# else
335# ifdef FEAT_GUI_W32
336 if (channel->ch_inputHandler == 0)
337 {
Bram Moolenaar54e09e72016-01-26 23:49:31 +0100338 WSAAsyncSelect(channel->ch_fd, s_hwnd, 0, 0);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100339 channel->ch_inputHandler = -1;
340 }
341# endif
342# endif
343# endif
344}
345
346#endif
347
348/*
349 * Open a channel to "hostname":"port".
350 * Returns the channel number for success.
351 * Returns a negative number for failure.
352 */
353 int
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100354channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100355{
356 int sd;
357 struct sockaddr_in server;
358 struct hostent * host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100359#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100360 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100361 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100362#else
363 int port = port_in;
364#endif
365 int idx;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100366 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100367
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100368#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100369 channel_init_winsock();
370#endif
371
372 idx = add_channel();
373 if (idx < 0)
374 {
375 CHERROR("All channels are in use\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100376 EMSG(_("E897: All channels are in use"));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100377 return -1;
378 }
379
380 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
381 {
382 CHERROR("error in socket() in channel_open()\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100383 PERROR("E898: socket() in channel_open()");
Bram Moolenaard04a0202016-01-26 23:30:18 +0100384 return -1;
385 }
386
387 /* Get the server internet address and put into addr structure */
388 /* fill in the socket address structure and connect to server */
389 vim_memset((char *)&server, 0, sizeof(server));
390 server.sin_family = AF_INET;
391 server.sin_port = htons(port);
392 if ((host = gethostbyname(hostname)) == NULL)
393 {
394 CHERROR("error in gethostbyname() in channel_open()\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100395 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaard04a0202016-01-26 23:30:18 +0100396 sock_close(sd);
397 return -1;
398 }
399 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
400
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100401 if (waittime >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100402 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100403 /* Make connect non-blocking. */
404 if (
405#ifdef _WIN32
406 ioctlsocket(sd, FIONBIO, &val) < 0
407#else
408 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
409#endif
410 )
Bram Moolenaard04a0202016-01-26 23:30:18 +0100411 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100412 SOCK_ERRNO;
413 CHERROR("channel_open: Connect failed with errno %d\n", errno);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100414 sock_close(sd);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100415 return -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100416 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100417 }
418
419 /* Try connecting to the server. */
420 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
421 SOCK_ERRNO;
422 if (ret < 0)
423 {
424 if (errno != EWOULDBLOCK && errno != EINPROGRESS)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100425 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100426 CHERROR("channel_open: Connect failed with errno %d\n", errno);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100427 CHERROR("Cannot connect to port\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100428 PERROR(_("E902: Cannot connect to port"));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100429 sock_close(sd);
430 return -1;
431 }
432 }
433
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100434 if (waittime >= 0 && ret < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100435 {
436 struct timeval tv;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100437 fd_set wfds;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100438
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100439 FD_ZERO(&wfds);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100440 FD_SET(sd, &wfds);
Bram Moolenaar26dfc412016-02-06 18:18:54 +0100441 tv.tv_sec = waittime / 1000;
442 tv.tv_usec = (waittime % 1000) * 1000;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100443 ret = select((int)sd + 1, NULL, &wfds, NULL, &tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100444 if (ret < 0)
445 {
446 SOCK_ERRNO;
447 CHERROR("channel_open: Connect failed with errno %d\n", errno);
448 CHERROR("Cannot connect to port\n", "");
449 PERROR(_("E902: Cannot connect to port"));
450 sock_close(sd);
451 return -1;
452 }
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100453 if (!FD_ISSET(sd, &wfds))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100454 {
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100455 /* don't give an error, we just timed out. */
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100456 sock_close(sd);
457 return -1;
458 }
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100459 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100460
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100461 if (waittime >= 0)
462 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100463#ifdef _WIN32
464 val = 0;
465 ioctlsocket(sd, FIONBIO, &val);
466#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100467 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100468#endif
469 }
470
471 if (errno == ECONNREFUSED)
472 {
473 sock_close(sd);
474 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
475 {
476 SOCK_ERRNO;
477 CHERROR("socket() retry in channel_open()\n", "");
478 PERROR("E900: socket() retry in channel_open()");
479 return -1;
480 }
481 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
482 {
483 int retries = 36;
484 int success = FALSE;
485
486 SOCK_ERRNO;
487 while (retries-- && ((errno == ECONNREFUSED)
488 || (errno == EINTR)))
489 {
490 CHERROR("retrying...\n", "");
491 mch_delay(3000L, TRUE);
492 ui_breakcheck();
493 if (got_int)
494 {
495 errno = EINTR;
496 break;
497 }
498 if (connect(sd, (struct sockaddr *)&server,
499 sizeof(server)) == 0)
500 {
501 success = TRUE;
502 break;
503 }
504 SOCK_ERRNO;
505 }
506 if (!success)
507 {
508 /* Get here when the server can't be found. */
509 CHERROR("Cannot connect to port after retry\n", "");
510 PERROR(_("E899: Cannot connect to port after retry2"));
511 sock_close(sd);
512 return -1;
513 }
514 }
515 }
516
Bram Moolenaard04a0202016-01-26 23:30:18 +0100517 channels[idx].ch_fd = sd;
518 channels[idx].ch_close_cb = close_cb;
519
520#ifdef FEAT_GUI
521 channel_gui_register(idx);
522#endif
523
524 return idx;
525}
526
527/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100528 * Set the json mode of channel "idx" to "ch_mode".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100529 */
530 void
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100531channel_set_json_mode(int idx, ch_mode_T ch_mode)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100532{
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100533 channels[idx].ch_mode = ch_mode;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100534}
535
536/*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100537 * Set the read timeout of channel "idx".
538 */
539 void
540channel_set_timeout(int idx, int timeout)
541{
542 channels[idx].ch_timeout = timeout;
543}
544
545/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100546 * Set the callback for channel "idx".
547 */
548 void
549channel_set_callback(int idx, char_u *callback)
550{
551 vim_free(channels[idx].ch_callback);
552 channels[idx].ch_callback = vim_strsave(callback);
553}
554
555/*
Bram Moolenaara07fec92016-02-05 21:04:08 +0100556 * Set the callback for channel "idx" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100557 */
558 void
Bram Moolenaara07fec92016-02-05 21:04:08 +0100559channel_set_req_callback(int idx, char_u *callback, int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100560{
Bram Moolenaara07fec92016-02-05 21:04:08 +0100561 cbq_T *cbhead = &channels[idx].ch_cb_head;
562 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
563
564 if (item != NULL)
565 {
566 item->callback = vim_strsave(callback);
567 item->seq_nr = id;
568 item->prev = cbhead->prev;
569 cbhead->prev = item;
570 item->next = cbhead;
571 item->prev->next = item;
572 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100573}
574
575/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100576 * Invoke the "callback" on channel "idx".
577 */
578 static void
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100579invoke_callback(int idx, char_u *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100580{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100581 typval_T rettv;
582 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100583
584 argv[0].v_type = VAR_NUMBER;
585 argv[0].vval.v_number = idx;
586
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100587 call_func(callback, (int)STRLEN(callback),
588 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
589 /* If an echo command was used the cursor needs to be put back where
590 * it belongs. */
591 setcursor();
592 cursor_on();
593 out_flush();
594}
595
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100596/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100597 * Return the first buffer from the channel and remove it.
598 * The caller must free it.
599 * Returns NULL if there is nothing.
600 */
601 char_u *
602channel_get(int idx)
603{
604 readq_T *head = &channels[idx].ch_head;
605 readq_T *node;
606 char_u *p;
607
608 if (head->next == head || head->next == NULL)
609 return NULL;
610 node = head->next;
611 /* dispose of the node but keep the buffer */
612 p = node->buffer;
613 head->next = node->next;
614 node->next->prev = node->prev;
615 vim_free(node);
616 return p;
617}
618
619/*
620 * Returns the whole buffer contents concatenated.
621 */
622 static char_u *
623channel_get_all(int idx)
624{
625 /* Concatenate everything into one buffer.
626 * TODO: avoid multiple allocations. */
627 while (channel_collapse(idx) == OK)
628 ;
629 return channel_get(idx);
630}
631
632/*
633 * Collapses the first and second buffer in the channel "idx".
634 * Returns FAIL if that is not possible.
635 */
636 int
637channel_collapse(int idx)
638{
639 readq_T *head = &channels[idx].ch_head;
640 readq_T *node = head->next;
641 char_u *p;
642
643 if (node == head || node == NULL || node->next == head)
644 return FAIL;
645
646 p = alloc((unsigned)(STRLEN(node->buffer)
647 + STRLEN(node->next->buffer) + 1));
648 if (p == NULL)
649 return FAIL; /* out of memory */
650 STRCPY(p, node->buffer);
651 STRCAT(p, node->next->buffer);
652 vim_free(node->next->buffer);
653 node->next->buffer = p;
654
655 /* dispose of the node and buffer */
656 head->next = node->next;
657 node->next->prev = node->prev;
658 vim_free(node->buffer);
659 vim_free(node);
660 return OK;
661}
662
663/*
Bram Moolenaard7ece102016-02-02 23:23:02 +0100664 * Use the read buffer of channel "ch_idx" and parse a JSON messages that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100665 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +0100666 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100667 */
Bram Moolenaard7ece102016-02-02 23:23:02 +0100668 static int
669channel_parse_json(int ch_idx)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100670{
671 js_read_T reader;
672 typval_T listtv;
673 jsonq_T *item;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100674 channel_T *channel = &channels[ch_idx];
675 jsonq_T *head = &channel->ch_json_head;
Bram Moolenaard7ece102016-02-02 23:23:02 +0100676 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100677
678 if (channel_peek(ch_idx) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100679 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100680
681 /* TODO: make reader work properly */
682 /* reader.js_buf = channel_peek(ch_idx); */
683 reader.js_buf = channel_get_all(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100684 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100685 reader.js_fill = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100686 /* reader.js_fill = channel_fill; */
687 reader.js_cookie = &ch_idx;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100688 ret = json_decode(&reader, &listtv,
689 channel->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100690 if (ret == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100691 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100692 /* Only accept the response when it is a list with at least two
693 * items. */
694 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100695 {
696 /* TODO: give error */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100697 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100698 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100699 else
700 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100701 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
702 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100703 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100704 else
705 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100706 item->value = alloc_tv();
707 if (item->value == NULL)
708 {
709 vim_free(item);
710 clear_tv(&listtv);
711 }
712 else
713 {
714 *item->value = listtv;
715 item->prev = head->prev;
716 head->prev = item;
717 item->next = head;
718 item->prev->next = item;
719 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100720 }
721 }
722 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100723
724 /* Put the unread part back into the channel.
725 * TODO: insert in front */
726 if (reader.js_buf[reader.js_used] != NUL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100727 {
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100728 channel_save(ch_idx, reader.js_buf + reader.js_used,
729 (int)(reader.js_end - reader.js_buf) - reader.js_used);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100730 ret = TRUE;
731 }
732 else
733 ret = FALSE;
734
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100735 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100736 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100737}
738
739/*
740 * Remove "node" from the queue that it is in and free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +0100741 * Also frees the contained callback name.
742 */
743 static void
744remove_cb_node(cbq_T *node)
745{
746 node->prev->next = node->next;
747 node->next->prev = node->prev;
748 vim_free(node->callback);
749 vim_free(node);
750}
751
752/*
753 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100754 * Caller should have freed or used node->value.
755 */
756 static void
757remove_json_node(jsonq_T *node)
758{
759 node->prev->next = node->next;
760 node->next->prev = node->prev;
761 vim_free(node);
762}
763
764/*
765 * Get a message from the JSON queue for channel "ch_idx".
766 * When "id" is positive it must match the first number in the list.
767 * When "id" is zero or negative jut get the first message.
768 * Return OK when found and return the value in "rettv".
769 * Return FAIL otherwise.
770 */
771 static int
772channel_get_json(int ch_idx, int id, typval_T **rettv)
773{
774 jsonq_T *head = &channels[ch_idx].ch_json_head;
775 jsonq_T *item = head->next;
776
777 while (item != head)
778 {
779 list_T *l = item->value->vval.v_list;
780 typval_T *tv = &l->lv_first->li_tv;
781
782 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100783 || id <= 0)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100784 {
785 *rettv = item->value;
786 remove_json_node(item);
787 return OK;
788 }
789 item = item->next;
790 }
791 return FAIL;
792}
793
794/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100795 * Execute a command received over channel "idx".
796 * "cmd" is the command string, "arg2" the second argument.
797 * "arg3" is the third argument, NULL if missing.
798 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100799 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100800channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100801{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100802 char_u *arg;
803
804 if (arg2->v_type != VAR_STRING)
805 {
806 if (p_verbose > 2)
807 EMSG("E903: received ex command with non-string argument");
808 return;
809 }
810 arg = arg2->vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100811 if (arg == NULL)
812 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100813
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100814 if (STRCMP(cmd, "ex") == 0)
815 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100816 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100817 }
818 else if (STRCMP(cmd, "normal") == 0)
819 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100820 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100821
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100822 ea.arg = arg;
823 ea.addr_count = 0;
824 ea.forceit = TRUE; /* no mapping */
825 ex_normal(&ea);
826 }
827 else if (STRCMP(cmd, "redraw") == 0)
828 {
829 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100830
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100831 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100832 ex_redraw(&ea);
833 showruler(FALSE);
834 setcursor();
835 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100836#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100837 if (gui.in_use)
838 {
839 gui_update_cursor(FALSE, FALSE);
840 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100841 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100842#endif
843 }
844 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
845 {
846 int is_eval = cmd[1] == 'v';
847
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100848 if (is_eval && (arg3 == NULL || arg3->v_type != VAR_NUMBER))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100849 {
850 if (p_verbose > 2)
851 EMSG("E904: third argument for eval must be a number");
852 }
853 else
854 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100855 typval_T *tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100856 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +0100857 char_u *json = NULL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100858 channel_T *channel = &channels[idx];
859 int options = channel->ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100860
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100861 /* Don't pollute the display with errors. */
862 ++emsg_skip;
863 tv = eval_expr(arg, NULL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100864 if (is_eval)
865 {
Bram Moolenaar55fab432016-02-07 16:53:13 +0100866 if (tv != NULL)
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100867 json = json_encode_nr_expr(arg3->vval.v_number, tv,
868 options);
Bram Moolenaar55fab432016-02-07 16:53:13 +0100869 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100870 {
Bram Moolenaar55fab432016-02-07 16:53:13 +0100871 /* If evaluation failed or the result can't be encoded
872 * then return the string "ERROR". */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100873 err_tv.v_type = VAR_STRING;
874 err_tv.vval.v_string = (char_u *)"ERROR";
875 tv = &err_tv;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100876 json = json_encode_nr_expr(arg3->vval.v_number, tv,
877 options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100878 }
Bram Moolenaar55fab432016-02-07 16:53:13 +0100879 if (json != NULL)
880 {
881 channel_send(idx, json, "eval");
882 vim_free(json);
883 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100884 }
Bram Moolenaar55fab432016-02-07 16:53:13 +0100885 --emsg_skip;
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100886 if (tv != &err_tv)
887 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100888 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100889 }
890 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100891 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100892}
893
894/*
895 * Invoke a callback for channel "idx" if needed.
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100896 * Return OK when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100897 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100898 static int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100899may_invoke_callback(int idx)
900{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100901 char_u *msg = NULL;
902 typval_T *listtv = NULL;
903 list_T *list;
904 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100905 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100906 int seq_nr = -1;
Bram Moolenaara07fec92016-02-05 21:04:08 +0100907 channel_T *channel = &channels[idx];
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100908 ch_mode_T ch_mode = channel->ch_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100909
Bram Moolenaara07fec92016-02-05 21:04:08 +0100910 if (channel->ch_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100911 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100912 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100913
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100914 if (ch_mode != MODE_RAW)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100915 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100916 /* Get any json message in the queue. */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100917 if (channel_get_json(idx, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100918 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100919 /* Parse readahead, return when there is still no message. */
920 channel_parse_json(idx);
921 if (channel_get_json(idx, -1, &listtv) == FAIL)
922 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100923 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100924
925 list = listtv->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100926 argv[1] = list->lv_first->li_next->li_tv;
927 typetv = &list->lv_first->li_tv;
928 if (typetv->v_type == VAR_STRING)
929 {
930 typval_T *arg3 = NULL;
931 char_u *cmd = typetv->vval.v_string;
932
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100933 /* ["cmd", arg] or ["cmd", arg, arg] */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100934 if (list->lv_len == 3)
935 arg3 = &list->lv_last->li_tv;
936 channel_exe_cmd(idx, cmd, &argv[1], arg3);
937 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100938 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100939 }
940
941 if (typetv->v_type != VAR_NUMBER)
942 {
943 /* TODO: give error */
944 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100945 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100946 }
947 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100948 }
Bram Moolenaard7ece102016-02-02 23:23:02 +0100949 else if (channel_peek(idx) == NULL)
950 {
951 /* nothing to read on raw channel */
952 return FALSE;
953 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100954 else
955 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100956 /* For a raw channel we don't know where the message ends, just get
957 * everything. */
958 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100959 argv[1].v_type = VAR_STRING;
960 argv[1].vval.v_string = msg;
961 }
962
Bram Moolenaara07fec92016-02-05 21:04:08 +0100963 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100964 {
Bram Moolenaara07fec92016-02-05 21:04:08 +0100965 cbq_T *cbhead = &channel->ch_cb_head;
966 cbq_T *cbitem = cbhead->next;
967
968 /* invoke the one-time callback with the matching nr */
969 while (cbitem != cbhead)
970 {
971 if (cbitem->seq_nr == seq_nr)
972 {
973 invoke_callback(idx, cbitem->callback, argv);
974 remove_cb_node(cbitem);
975 break;
976 }
977 cbitem = cbitem->next;
978 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100979 }
Bram Moolenaara07fec92016-02-05 21:04:08 +0100980 else if (channel->ch_callback != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100981 {
982 /* invoke the channel callback */
Bram Moolenaara07fec92016-02-05 21:04:08 +0100983 invoke_callback(idx, channel->ch_callback, argv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100984 }
985 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100986
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100987 if (listtv != NULL)
988 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100989 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100990
991 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100992}
993
994/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100995 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100996 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +0100997 */
998 int
999channel_is_open(int idx)
1000{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001001 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001002}
1003
1004/*
1005 * Close channel "idx".
1006 * This does not trigger the close callback.
1007 */
1008 void
1009channel_close(int idx)
1010{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001011 channel_T *channel = &channels[idx];
1012 jsonq_T *jhead;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001013 cbq_T *cbhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001014
1015 if (channel->ch_fd >= 0)
1016 {
1017 sock_close(channel->ch_fd);
1018 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001019 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001020#ifdef FEAT_GUI
1021 channel_gui_unregister(idx);
1022#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001023 vim_free(channel->ch_callback);
1024 channel->ch_callback = NULL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001025 channel->ch_timeout = 2000;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001026
1027 while (channel_peek(idx) != NULL)
1028 vim_free(channel_get(idx));
1029
Bram Moolenaara07fec92016-02-05 21:04:08 +01001030 cbhead = &channel->ch_cb_head;
1031 while (cbhead->next != cbhead)
1032 remove_cb_node(cbhead->next);
1033
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001034 jhead = &channel->ch_json_head;
1035 while (jhead->next != jhead)
1036 {
1037 clear_tv(jhead->next->value);
1038 remove_json_node(jhead->next);
1039 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01001040 }
1041}
1042
Bram Moolenaard04a0202016-01-26 23:30:18 +01001043/*
1044 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +01001045 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001046 */
Bram Moolenaar83162462016-01-28 23:10:07 +01001047 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001048channel_save(int idx, char_u *buf, int len)
1049{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001050 readq_T *node;
1051 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001052
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001053 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001054 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +01001055 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001056 node->buffer = alloc(len + 1);
1057 if (node->buffer == NULL)
1058 {
1059 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +01001060 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001061 }
1062 mch_memmove(node->buffer, buf, (size_t)len);
1063 node->buffer[len] = NUL;
1064
Bram Moolenaard04a0202016-01-26 23:30:18 +01001065 /* insert node at tail of queue */
1066 node->next = head;
1067 node->prev = head->prev;
1068 head->prev->next = node;
1069 head->prev = node;
1070
1071 if (debugfd != NULL)
1072 {
1073 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +01001074 if (fwrite(buf, len, 1, debugfd) != 1)
1075 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001076 fprintf(debugfd, "\n");
1077 }
Bram Moolenaar83162462016-01-28 23:10:07 +01001078 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001079}
1080
1081/*
1082 * Return the first buffer from the channel without removing it.
1083 * Returns NULL if there is nothing.
1084 */
1085 char_u *
1086channel_peek(int idx)
1087{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001088 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001089
1090 if (head->next == head || head->next == NULL)
1091 return NULL;
1092 return head->next->buffer;
1093}
1094
1095/*
Bram Moolenaard04a0202016-01-26 23:30:18 +01001096 * Clear the read buffer on channel "idx".
1097 */
1098 void
1099channel_clear(int idx)
1100{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001101 readq_T *head = &channels[idx].ch_head;
1102 readq_T *node = head->next;
1103 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001104
1105 while (node != NULL && node != head)
1106 {
1107 next = node->next;
1108 vim_free(node->buffer);
1109 vim_free(node);
1110 if (next == head)
1111 {
1112 head->next = head;
1113 head->prev = head;
1114 break;
1115 }
1116 node = next;
1117 }
1118}
1119
1120/* Sent when the channel is found closed when reading. */
1121#define DETACH_MSG "\"DETACH\"\n"
1122
1123/* Buffer size for reading incoming messages. */
1124#define MAXMSGSIZE 4096
1125
1126/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001127 * Check for reading from "fd" with "timeout" msec.
1128 * Return FAIL when there is nothing to read.
Bram Moolenaara8343c12016-02-04 22:09:48 +01001129 * Always returns OK for FEAT_GUI_W32.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001130 */
1131 static int
1132channel_wait(int fd, int timeout)
1133{
Bram Moolenaara8343c12016-02-04 22:09:48 +01001134#if defined(HAVE_SELECT) && !defined(FEAT_GUI_W32)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001135 struct timeval tval;
1136 fd_set rfds;
1137 int ret;
1138
1139 FD_ZERO(&rfds);
1140 FD_SET(fd, &rfds);
1141 tval.tv_sec = timeout / 1000;
1142 tval.tv_usec = (timeout % 1000) * 1000;
1143 for (;;)
1144 {
1145 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
1146# ifdef EINTR
1147 if (ret == -1 && errno == EINTR)
1148 continue;
1149# endif
1150 if (ret <= 0)
1151 return FAIL;
1152 break;
1153 }
1154#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001155# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001156 struct pollfd fds;
1157
1158 fds.fd = fd;
1159 fds.events = POLLIN;
1160 if (poll(&fds, 1, timeout) <= 0)
1161 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001162# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001163#endif
1164 return OK;
1165}
1166
1167/*
1168 * Return a unique ID to be used in a message.
1169 */
1170 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001171channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001172{
1173 static int next_id = 1;
1174
1175 return next_id++;
1176}
1177
1178/*
1179 * Read from channel "idx" for as long as there is something to read.
1180 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001181 */
1182 void
1183channel_read(int idx)
1184{
1185 static char_u *buf = NULL;
1186 int len = 0;
1187 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001188 channel_T *channel = &channels[idx];
1189
1190 if (channel->ch_fd < 0)
1191 {
1192 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
1193 return;
1194 }
1195
1196 /* Allocate a buffer to read into. */
1197 if (buf == NULL)
1198 {
1199 buf = alloc(MAXMSGSIZE);
1200 if (buf == NULL)
1201 return; /* out of memory! */
1202 }
1203
1204 /* Keep on reading for as long as there is something to read.
1205 * Use select() or poll() to avoid blocking on a message that is exactly
1206 * MAXMSGSIZE long. */
1207 for (;;)
1208 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001209 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001210 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001211 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1212 if (len <= 0)
1213 break; /* error or nothing more to read */
1214
1215 /* Store the read message in the queue. */
1216 channel_save(idx, buf, len);
1217 readlen += len;
1218 if (len < MAXMSGSIZE)
1219 break; /* did read everything that's available */
1220 }
Bram Moolenaara8343c12016-02-04 22:09:48 +01001221#ifdef FEAT_GUI_W32
1222 if (len == SOCKET_ERROR)
1223 {
1224 /* For Win32 GUI channel_wait() always returns OK and we handle the
1225 * situation that there is nothing to read here.
1226 * TODO: how about a timeout? */
1227 if (WSAGetLastError() == WSAEWOULDBLOCK)
1228 return;
1229 }
1230#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01001231
1232 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1233 if (readlen <= 0)
1234 {
1235 /* Queue a "DETACH" netbeans message in the command queue in order to
1236 * terminate the netbeans session later. Do not end the session here
1237 * directly as we may be running in the context of a call to
1238 * netbeans_parse_messages():
1239 * netbeans_parse_messages
1240 * -> autocmd triggered while processing the netbeans cmd
1241 * -> ui_breakcheck
1242 * -> gui event loop or select loop
1243 * -> channel_read()
1244 */
1245 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1246
1247 channel_close(idx);
1248 if (channel->ch_close_cb != NULL)
1249 (*channel->ch_close_cb)();
1250
1251 if (len < 0)
1252 {
1253 /* Todo: which channel? */
1254 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001255 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001256 }
1257 }
1258
1259#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1260 if (CH_HAS_GUI && gtk_main_level() > 0)
1261 gtk_main_quit();
1262#endif
1263}
1264
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001265/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001266 * Read from raw channel "idx". Blocks until there is something to read or
1267 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001268 * Returns what was read in allocated memory.
1269 * Returns NULL in case of error or timeout.
1270 */
1271 char_u *
1272channel_read_block(int idx)
1273{
1274 if (channel_peek(idx) == NULL)
1275 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001276 /* Wait for up to the channel timeout. */
1277 if (channel_wait(channels[idx].ch_fd, channels[idx].ch_timeout) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001278 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001279 channel_read(idx);
1280 }
1281
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001282 return channel_get_all(idx);
1283}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001284
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001285/*
1286 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1287 * result in "rettv".
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001288 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001289 */
1290 int
1291channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1292{
Bram Moolenaard7ece102016-02-02 23:23:02 +01001293 int more;
1294
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001295 for (;;)
1296 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001297 more = channel_parse_json(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001298
1299 /* search for messsage "id" */
1300 if (channel_get_json(ch_idx, id, rettv) == OK)
1301 return OK;
1302
Bram Moolenaard7ece102016-02-02 23:23:02 +01001303 if (!more)
1304 {
1305 /* Handle any other messages in the queue. If done some more
1306 * messages may have arrived. */
1307 if (channel_parse_messages())
1308 continue;
1309
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001310 /* Wait for up to the channel timeout. */
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001311 if (channels[ch_idx].ch_fd < 0
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001312 || channel_wait(channels[ch_idx].ch_fd,
1313 channels[ch_idx].ch_timeout) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001314 break;
1315 channel_read(ch_idx);
1316 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001317 }
1318 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001319}
1320
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001321# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001322/*
1323 * Lookup the channel index from the socket.
1324 * Returns -1 when the socket isn't found.
1325 */
1326 int
1327channel_socket2idx(sock_T fd)
1328{
1329 int i;
1330
1331 if (fd >= 0)
1332 for (i = 0; i < channel_count; ++i)
1333 if (channels[i].ch_fd == fd)
1334 return i;
1335 return -1;
1336}
1337# endif
1338
Bram Moolenaard04a0202016-01-26 23:30:18 +01001339/*
1340 * Write "buf" (NUL terminated string) to channel "idx".
1341 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001342 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001343 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001344 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001345channel_send(int idx, char_u *buf, char *fun)
1346{
1347 channel_T *channel = &channels[idx];
1348 int len = (int)STRLEN(buf);
1349
1350 if (channel->ch_fd < 0)
1351 {
1352 if (!channel->ch_error && fun != NULL)
1353 {
1354 CHERROR(" %s(): write while not connected\n", fun);
1355 EMSG2("E630: %s(): write while not connected", fun);
1356 }
1357 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001358 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001359 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001360
1361 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001362 {
1363 if (!channel->ch_error && fun != NULL)
1364 {
1365 CHERROR(" %s(): write failed\n", fun);
1366 EMSG2("E631: %s(): write failed", fun);
1367 }
1368 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001369 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001370 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001371
1372 channel->ch_error = FALSE;
1373 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001374}
1375
1376# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001377/*
1378 * Add open channels to the poll struct.
1379 * Return the adjusted struct index.
1380 * The type of "fds" is hidden to avoid problems with the function proto.
1381 */
1382 int
1383channel_poll_setup(int nfd_in, void *fds_in)
1384{
1385 int nfd = nfd_in;
1386 int i;
1387 struct pollfd *fds = fds_in;
1388
1389 for (i = 0; i < channel_count; ++i)
1390 if (channels[i].ch_fd >= 0)
1391 {
1392 channels[i].ch_idx = nfd;
1393 fds[nfd].fd = channels[i].ch_fd;
1394 fds[nfd].events = POLLIN;
1395 nfd++;
1396 }
1397 else
1398 channels[i].ch_idx = -1;
1399
1400 return nfd;
1401}
1402
1403/*
1404 * The type of "fds" is hidden to avoid problems with the function proto.
1405 */
1406 int
1407channel_poll_check(int ret_in, void *fds_in)
1408{
1409 int ret = ret_in;
1410 int i;
1411 struct pollfd *fds = fds_in;
1412
1413 for (i = 0; i < channel_count; ++i)
1414 if (ret > 0 && channels[i].ch_idx != -1
1415 && fds[channels[i].ch_idx].revents & POLLIN)
1416 {
1417 channel_read(i);
1418 --ret;
1419 }
1420
1421 return ret;
1422}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001423# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001424
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001425# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001426/*
1427 * The type of "rfds" is hidden to avoid problems with the function proto.
1428 */
1429 int
1430channel_select_setup(int maxfd_in, void *rfds_in)
1431{
1432 int maxfd = maxfd_in;
1433 int i;
1434 fd_set *rfds = rfds_in;
1435
1436 for (i = 0; i < channel_count; ++i)
1437 if (channels[i].ch_fd >= 0)
1438 {
1439 FD_SET(channels[i].ch_fd, rfds);
1440 if (maxfd < channels[i].ch_fd)
1441 maxfd = channels[i].ch_fd;
1442 }
1443
1444 return maxfd;
1445}
1446
1447/*
1448 * The type of "rfds" is hidden to avoid problems with the function proto.
1449 */
1450 int
1451channel_select_check(int ret_in, void *rfds_in)
1452{
1453 int ret = ret_in;
1454 int i;
1455 fd_set *rfds = rfds_in;
1456
1457 for (i = 0; i < channel_count; ++i)
1458 if (ret > 0 && channels[i].ch_fd >= 0
1459 && FD_ISSET(channels[i].ch_fd, rfds))
1460 {
1461 channel_read(i);
1462 --ret;
1463 }
1464
1465 return ret;
1466}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001467# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001468
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001469/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01001470 * Execute queued up commands.
1471 * Invoked from the main loop when it's safe to execute received commands.
1472 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001473 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001474 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001475channel_parse_messages(void)
1476{
1477 int i;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001478 int ret = FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001479
1480 for (i = 0; i < channel_count; ++i)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001481 while (may_invoke_callback(i) == OK)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001482 {
1483 i = 0; /* start over */
1484 ret = TRUE;
1485 }
1486 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001487}
1488
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01001489/*
1490 * Mark references to lists used in channels.
1491 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01001492 int
1493set_ref_in_channel(int copyID)
1494{
1495 int i;
1496 int abort = FALSE;
1497
1498 for (i = 0; i < channel_count; ++i)
1499 {
1500 jsonq_T *head = &channels[i].ch_json_head;
1501 jsonq_T *item = head->next;
1502
1503 while (item != head)
1504 {
1505 list_T *l = item->value->vval.v_list;
1506
1507 if (l->lv_copyID != copyID)
1508 {
1509 l->lv_copyID = copyID;
1510 abort = abort || set_ref_in_list(l, copyID, NULL);
1511 }
1512 item = item->next;
1513 }
1514 }
1515 return abort;
1516}
Bram Moolenaare0874f82016-01-24 20:36:41 +01001517#endif /* FEAT_CHANNEL */