blob: 1d12ee72240d940df346e8351d357a2e06aa2b1f [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 Moolenaar4d919d72016-02-05 22:36:41 +0100434 if (waittime >= 0)
435 {
436 struct timeval tv;
437 fd_set rfds, wfds;
438
439 FD_ZERO(&rfds);
440 FD_ZERO(&wfds);
441 FD_SET(sd, &rfds);
442 FD_SET(sd, &wfds);
Bram Moolenaar26dfc412016-02-06 18:18:54 +0100443 tv.tv_sec = waittime / 1000;
444 tv.tv_usec = (waittime % 1000) * 1000;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100445 ret = select((int)sd+1, &rfds, &wfds, NULL, &tv);
446 if (ret < 0)
447 {
448 SOCK_ERRNO;
449 CHERROR("channel_open: Connect failed with errno %d\n", errno);
450 CHERROR("Cannot connect to port\n", "");
451 PERROR(_("E902: Cannot connect to port"));
452 sock_close(sd);
453 return -1;
454 }
455 if (!FD_ISSET(sd, &rfds) && !FD_ISSET(sd, &wfds))
456 {
457 errno = ECONNREFUSED;
458 CHERROR("Cannot connect to port\n", "");
459 PERROR(_("E902: Cannot connect to port"));
460 sock_close(sd);
461 return -1;
462 }
463
464#ifdef _WIN32
465 val = 0;
466 ioctlsocket(sd, FIONBIO, &val);
467#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100468 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100469#endif
470 }
471
472 if (errno == ECONNREFUSED)
473 {
474 sock_close(sd);
475 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
476 {
477 SOCK_ERRNO;
478 CHERROR("socket() retry in channel_open()\n", "");
479 PERROR("E900: socket() retry in channel_open()");
480 return -1;
481 }
482 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
483 {
484 int retries = 36;
485 int success = FALSE;
486
487 SOCK_ERRNO;
488 while (retries-- && ((errno == ECONNREFUSED)
489 || (errno == EINTR)))
490 {
491 CHERROR("retrying...\n", "");
492 mch_delay(3000L, TRUE);
493 ui_breakcheck();
494 if (got_int)
495 {
496 errno = EINTR;
497 break;
498 }
499 if (connect(sd, (struct sockaddr *)&server,
500 sizeof(server)) == 0)
501 {
502 success = TRUE;
503 break;
504 }
505 SOCK_ERRNO;
506 }
507 if (!success)
508 {
509 /* Get here when the server can't be found. */
510 CHERROR("Cannot connect to port after retry\n", "");
511 PERROR(_("E899: Cannot connect to port after retry2"));
512 sock_close(sd);
513 return -1;
514 }
515 }
516 }
517
Bram Moolenaard04a0202016-01-26 23:30:18 +0100518 channels[idx].ch_fd = sd;
519 channels[idx].ch_close_cb = close_cb;
520
521#ifdef FEAT_GUI
522 channel_gui_register(idx);
523#endif
524
525 return idx;
526}
527
528/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100529 * Set the json mode of channel "idx" to "ch_mode".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100530 */
531 void
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100532channel_set_json_mode(int idx, ch_mode_T ch_mode)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100533{
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100534 channels[idx].ch_mode = ch_mode;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100535}
536
537/*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100538 * Set the read timeout of channel "idx".
539 */
540 void
541channel_set_timeout(int idx, int timeout)
542{
543 channels[idx].ch_timeout = timeout;
544}
545
546/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100547 * Set the callback for channel "idx".
548 */
549 void
550channel_set_callback(int idx, char_u *callback)
551{
552 vim_free(channels[idx].ch_callback);
553 channels[idx].ch_callback = vim_strsave(callback);
554}
555
556/*
Bram Moolenaara07fec92016-02-05 21:04:08 +0100557 * Set the callback for channel "idx" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100558 */
559 void
Bram Moolenaara07fec92016-02-05 21:04:08 +0100560channel_set_req_callback(int idx, char_u *callback, int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100561{
Bram Moolenaara07fec92016-02-05 21:04:08 +0100562 cbq_T *cbhead = &channels[idx].ch_cb_head;
563 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
564
565 if (item != NULL)
566 {
567 item->callback = vim_strsave(callback);
568 item->seq_nr = id;
569 item->prev = cbhead->prev;
570 cbhead->prev = item;
571 item->next = cbhead;
572 item->prev->next = item;
573 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100574}
575
576/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100577 * Invoke the "callback" on channel "idx".
578 */
579 static void
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100580invoke_callback(int idx, char_u *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100581{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100582 typval_T rettv;
583 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100584
585 argv[0].v_type = VAR_NUMBER;
586 argv[0].vval.v_number = idx;
587
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100588 call_func(callback, (int)STRLEN(callback),
589 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
590 /* If an echo command was used the cursor needs to be put back where
591 * it belongs. */
592 setcursor();
593 cursor_on();
594 out_flush();
595}
596
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100597/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100598 * Return the first buffer from the channel and remove it.
599 * The caller must free it.
600 * Returns NULL if there is nothing.
601 */
602 char_u *
603channel_get(int idx)
604{
605 readq_T *head = &channels[idx].ch_head;
606 readq_T *node;
607 char_u *p;
608
609 if (head->next == head || head->next == NULL)
610 return NULL;
611 node = head->next;
612 /* dispose of the node but keep the buffer */
613 p = node->buffer;
614 head->next = node->next;
615 node->next->prev = node->prev;
616 vim_free(node);
617 return p;
618}
619
620/*
621 * Returns the whole buffer contents concatenated.
622 */
623 static char_u *
624channel_get_all(int idx)
625{
626 /* Concatenate everything into one buffer.
627 * TODO: avoid multiple allocations. */
628 while (channel_collapse(idx) == OK)
629 ;
630 return channel_get(idx);
631}
632
633/*
634 * Collapses the first and second buffer in the channel "idx".
635 * Returns FAIL if that is not possible.
636 */
637 int
638channel_collapse(int idx)
639{
640 readq_T *head = &channels[idx].ch_head;
641 readq_T *node = head->next;
642 char_u *p;
643
644 if (node == head || node == NULL || node->next == head)
645 return FAIL;
646
647 p = alloc((unsigned)(STRLEN(node->buffer)
648 + STRLEN(node->next->buffer) + 1));
649 if (p == NULL)
650 return FAIL; /* out of memory */
651 STRCPY(p, node->buffer);
652 STRCAT(p, node->next->buffer);
653 vim_free(node->next->buffer);
654 node->next->buffer = p;
655
656 /* dispose of the node and buffer */
657 head->next = node->next;
658 node->next->prev = node->prev;
659 vim_free(node->buffer);
660 vim_free(node);
661 return OK;
662}
663
664/*
Bram Moolenaard7ece102016-02-02 23:23:02 +0100665 * Use the read buffer of channel "ch_idx" and parse a JSON messages that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100666 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +0100667 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100668 */
Bram Moolenaard7ece102016-02-02 23:23:02 +0100669 static int
670channel_parse_json(int ch_idx)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100671{
672 js_read_T reader;
673 typval_T listtv;
674 jsonq_T *item;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100675 channel_T *channel = &channels[ch_idx];
676 jsonq_T *head = &channel->ch_json_head;
Bram Moolenaard7ece102016-02-02 23:23:02 +0100677 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100678
679 if (channel_peek(ch_idx) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100680 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100681
682 /* TODO: make reader work properly */
683 /* reader.js_buf = channel_peek(ch_idx); */
684 reader.js_buf = channel_get_all(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100685 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100686 reader.js_fill = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100687 /* reader.js_fill = channel_fill; */
688 reader.js_cookie = &ch_idx;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100689 ret = json_decode(&reader, &listtv,
690 channel->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100691 if (ret == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100692 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100693 /* Only accept the response when it is a list with at least two
694 * items. */
695 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100696 {
697 /* TODO: give error */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100698 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100699 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100700 else
701 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100702 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
703 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100704 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100705 else
706 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100707 item->value = alloc_tv();
708 if (item->value == NULL)
709 {
710 vim_free(item);
711 clear_tv(&listtv);
712 }
713 else
714 {
715 *item->value = listtv;
716 item->prev = head->prev;
717 head->prev = item;
718 item->next = head;
719 item->prev->next = item;
720 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100721 }
722 }
723 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100724
725 /* Put the unread part back into the channel.
726 * TODO: insert in front */
727 if (reader.js_buf[reader.js_used] != NUL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100728 {
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100729 channel_save(ch_idx, reader.js_buf + reader.js_used,
730 (int)(reader.js_end - reader.js_buf) - reader.js_used);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100731 ret = TRUE;
732 }
733 else
734 ret = FALSE;
735
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100736 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100737 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100738}
739
740/*
741 * Remove "node" from the queue that it is in and free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +0100742 * Also frees the contained callback name.
743 */
744 static void
745remove_cb_node(cbq_T *node)
746{
747 node->prev->next = node->next;
748 node->next->prev = node->prev;
749 vim_free(node->callback);
750 vim_free(node);
751}
752
753/*
754 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100755 * Caller should have freed or used node->value.
756 */
757 static void
758remove_json_node(jsonq_T *node)
759{
760 node->prev->next = node->next;
761 node->next->prev = node->prev;
762 vim_free(node);
763}
764
765/*
766 * Get a message from the JSON queue for channel "ch_idx".
767 * When "id" is positive it must match the first number in the list.
768 * When "id" is zero or negative jut get the first message.
769 * Return OK when found and return the value in "rettv".
770 * Return FAIL otherwise.
771 */
772 static int
773channel_get_json(int ch_idx, int id, typval_T **rettv)
774{
775 jsonq_T *head = &channels[ch_idx].ch_json_head;
776 jsonq_T *item = head->next;
777
778 while (item != head)
779 {
780 list_T *l = item->value->vval.v_list;
781 typval_T *tv = &l->lv_first->li_tv;
782
783 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100784 || id <= 0)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100785 {
786 *rettv = item->value;
787 remove_json_node(item);
788 return OK;
789 }
790 item = item->next;
791 }
792 return FAIL;
793}
794
795/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100796 * Execute a command received over channel "idx".
797 * "cmd" is the command string, "arg2" the second argument.
798 * "arg3" is the third argument, NULL if missing.
799 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100800 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100801channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100802{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100803 char_u *arg;
804
805 if (arg2->v_type != VAR_STRING)
806 {
807 if (p_verbose > 2)
808 EMSG("E903: received ex command with non-string argument");
809 return;
810 }
811 arg = arg2->vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100812 if (arg == NULL)
813 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100814
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100815 if (STRCMP(cmd, "ex") == 0)
816 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100817 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100818 }
819 else if (STRCMP(cmd, "normal") == 0)
820 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100821 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100822
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100823 ea.arg = arg;
824 ea.addr_count = 0;
825 ea.forceit = TRUE; /* no mapping */
826 ex_normal(&ea);
827 }
828 else if (STRCMP(cmd, "redraw") == 0)
829 {
830 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100831
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100832 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100833 ex_redraw(&ea);
834 showruler(FALSE);
835 setcursor();
836 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100837#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100838 if (gui.in_use)
839 {
840 gui_update_cursor(FALSE, FALSE);
841 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100842 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100843#endif
844 }
845 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
846 {
847 int is_eval = cmd[1] == 'v';
848
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100849 if (is_eval && (arg3 == NULL || arg3->v_type != VAR_NUMBER))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100850 {
851 if (p_verbose > 2)
852 EMSG("E904: third argument for eval must be a number");
853 }
854 else
855 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100856 typval_T *tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100857 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +0100858 char_u *json = NULL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100859 channel_T *channel = &channels[idx];
860 int options = channel->ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100861
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100862 /* Don't pollute the display with errors. */
863 ++emsg_skip;
864 tv = eval_expr(arg, NULL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100865 if (is_eval)
866 {
Bram Moolenaar55fab432016-02-07 16:53:13 +0100867 if (tv != NULL)
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100868 json = json_encode_nr_expr(arg3->vval.v_number, tv,
869 options);
Bram Moolenaar55fab432016-02-07 16:53:13 +0100870 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100871 {
Bram Moolenaar55fab432016-02-07 16:53:13 +0100872 /* If evaluation failed or the result can't be encoded
873 * then return the string "ERROR". */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100874 err_tv.v_type = VAR_STRING;
875 err_tv.vval.v_string = (char_u *)"ERROR";
876 tv = &err_tv;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100877 json = json_encode_nr_expr(arg3->vval.v_number, tv,
878 options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100879 }
Bram Moolenaar55fab432016-02-07 16:53:13 +0100880 if (json != NULL)
881 {
882 channel_send(idx, json, "eval");
883 vim_free(json);
884 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100885 }
Bram Moolenaar55fab432016-02-07 16:53:13 +0100886 --emsg_skip;
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100887 if (tv != &err_tv)
888 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100889 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100890 }
891 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100892 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100893}
894
895/*
896 * Invoke a callback for channel "idx" if needed.
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100897 * Return OK when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100898 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100899 static int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100900may_invoke_callback(int idx)
901{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100902 char_u *msg = NULL;
903 typval_T *listtv = NULL;
904 list_T *list;
905 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100906 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100907 int seq_nr = -1;
Bram Moolenaara07fec92016-02-05 21:04:08 +0100908 channel_T *channel = &channels[idx];
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100909 ch_mode_T ch_mode = channel->ch_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100910
Bram Moolenaara07fec92016-02-05 21:04:08 +0100911 if (channel->ch_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100912 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100913 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100914
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100915 if (ch_mode != MODE_RAW)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100916 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100917 /* Get any json message in the queue. */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100918 if (channel_get_json(idx, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100919 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100920 /* Parse readahead, return when there is still no message. */
921 channel_parse_json(idx);
922 if (channel_get_json(idx, -1, &listtv) == FAIL)
923 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100924 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100925
926 list = listtv->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100927 argv[1] = list->lv_first->li_next->li_tv;
928 typetv = &list->lv_first->li_tv;
929 if (typetv->v_type == VAR_STRING)
930 {
931 typval_T *arg3 = NULL;
932 char_u *cmd = typetv->vval.v_string;
933
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100934 /* ["cmd", arg] or ["cmd", arg, arg] */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100935 if (list->lv_len == 3)
936 arg3 = &list->lv_last->li_tv;
937 channel_exe_cmd(idx, cmd, &argv[1], arg3);
938 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100939 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100940 }
941
942 if (typetv->v_type != VAR_NUMBER)
943 {
944 /* TODO: give error */
945 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100946 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100947 }
948 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100949 }
Bram Moolenaard7ece102016-02-02 23:23:02 +0100950 else if (channel_peek(idx) == NULL)
951 {
952 /* nothing to read on raw channel */
953 return FALSE;
954 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100955 else
956 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100957 /* For a raw channel we don't know where the message ends, just get
958 * everything. */
959 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100960 argv[1].v_type = VAR_STRING;
961 argv[1].vval.v_string = msg;
962 }
963
Bram Moolenaara07fec92016-02-05 21:04:08 +0100964 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100965 {
Bram Moolenaara07fec92016-02-05 21:04:08 +0100966 cbq_T *cbhead = &channel->ch_cb_head;
967 cbq_T *cbitem = cbhead->next;
968
969 /* invoke the one-time callback with the matching nr */
970 while (cbitem != cbhead)
971 {
972 if (cbitem->seq_nr == seq_nr)
973 {
974 invoke_callback(idx, cbitem->callback, argv);
975 remove_cb_node(cbitem);
976 break;
977 }
978 cbitem = cbitem->next;
979 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100980 }
Bram Moolenaara07fec92016-02-05 21:04:08 +0100981 else if (channel->ch_callback != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100982 {
983 /* invoke the channel callback */
Bram Moolenaara07fec92016-02-05 21:04:08 +0100984 invoke_callback(idx, channel->ch_callback, argv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100985 }
986 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100987
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100988 if (listtv != NULL)
989 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100990 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100991
992 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100993}
994
995/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100996 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100997 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +0100998 */
999 int
1000channel_is_open(int idx)
1001{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001002 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001003}
1004
1005/*
1006 * Close channel "idx".
1007 * This does not trigger the close callback.
1008 */
1009 void
1010channel_close(int idx)
1011{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001012 channel_T *channel = &channels[idx];
1013 jsonq_T *jhead;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001014 cbq_T *cbhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001015
1016 if (channel->ch_fd >= 0)
1017 {
1018 sock_close(channel->ch_fd);
1019 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001020 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001021#ifdef FEAT_GUI
1022 channel_gui_unregister(idx);
1023#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001024 vim_free(channel->ch_callback);
1025 channel->ch_callback = NULL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001026 channel->ch_timeout = 2000;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001027
1028 while (channel_peek(idx) != NULL)
1029 vim_free(channel_get(idx));
1030
Bram Moolenaara07fec92016-02-05 21:04:08 +01001031 cbhead = &channel->ch_cb_head;
1032 while (cbhead->next != cbhead)
1033 remove_cb_node(cbhead->next);
1034
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001035 jhead = &channel->ch_json_head;
1036 while (jhead->next != jhead)
1037 {
1038 clear_tv(jhead->next->value);
1039 remove_json_node(jhead->next);
1040 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01001041 }
1042}
1043
Bram Moolenaard04a0202016-01-26 23:30:18 +01001044/*
1045 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +01001046 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001047 */
Bram Moolenaar83162462016-01-28 23:10:07 +01001048 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001049channel_save(int idx, char_u *buf, int len)
1050{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001051 readq_T *node;
1052 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001053
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001054 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001055 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +01001056 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001057 node->buffer = alloc(len + 1);
1058 if (node->buffer == NULL)
1059 {
1060 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +01001061 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001062 }
1063 mch_memmove(node->buffer, buf, (size_t)len);
1064 node->buffer[len] = NUL;
1065
Bram Moolenaard04a0202016-01-26 23:30:18 +01001066 /* insert node at tail of queue */
1067 node->next = head;
1068 node->prev = head->prev;
1069 head->prev->next = node;
1070 head->prev = node;
1071
1072 if (debugfd != NULL)
1073 {
1074 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +01001075 if (fwrite(buf, len, 1, debugfd) != 1)
1076 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001077 fprintf(debugfd, "\n");
1078 }
Bram Moolenaar83162462016-01-28 23:10:07 +01001079 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001080}
1081
1082/*
1083 * Return the first buffer from the channel without removing it.
1084 * Returns NULL if there is nothing.
1085 */
1086 char_u *
1087channel_peek(int idx)
1088{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001089 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001090
1091 if (head->next == head || head->next == NULL)
1092 return NULL;
1093 return head->next->buffer;
1094}
1095
1096/*
Bram Moolenaard04a0202016-01-26 23:30:18 +01001097 * Clear the read buffer on channel "idx".
1098 */
1099 void
1100channel_clear(int idx)
1101{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001102 readq_T *head = &channels[idx].ch_head;
1103 readq_T *node = head->next;
1104 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001105
1106 while (node != NULL && node != head)
1107 {
1108 next = node->next;
1109 vim_free(node->buffer);
1110 vim_free(node);
1111 if (next == head)
1112 {
1113 head->next = head;
1114 head->prev = head;
1115 break;
1116 }
1117 node = next;
1118 }
1119}
1120
1121/* Sent when the channel is found closed when reading. */
1122#define DETACH_MSG "\"DETACH\"\n"
1123
1124/* Buffer size for reading incoming messages. */
1125#define MAXMSGSIZE 4096
1126
1127/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001128 * Check for reading from "fd" with "timeout" msec.
1129 * Return FAIL when there is nothing to read.
Bram Moolenaara8343c12016-02-04 22:09:48 +01001130 * Always returns OK for FEAT_GUI_W32.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001131 */
1132 static int
1133channel_wait(int fd, int timeout)
1134{
Bram Moolenaara8343c12016-02-04 22:09:48 +01001135#if defined(HAVE_SELECT) && !defined(FEAT_GUI_W32)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001136 struct timeval tval;
1137 fd_set rfds;
1138 int ret;
1139
1140 FD_ZERO(&rfds);
1141 FD_SET(fd, &rfds);
1142 tval.tv_sec = timeout / 1000;
1143 tval.tv_usec = (timeout % 1000) * 1000;
1144 for (;;)
1145 {
1146 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
1147# ifdef EINTR
1148 if (ret == -1 && errno == EINTR)
1149 continue;
1150# endif
1151 if (ret <= 0)
1152 return FAIL;
1153 break;
1154 }
1155#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001156# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001157 struct pollfd fds;
1158
1159 fds.fd = fd;
1160 fds.events = POLLIN;
1161 if (poll(&fds, 1, timeout) <= 0)
1162 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001163# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001164#endif
1165 return OK;
1166}
1167
1168/*
1169 * Return a unique ID to be used in a message.
1170 */
1171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001172channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001173{
1174 static int next_id = 1;
1175
1176 return next_id++;
1177}
1178
1179/*
1180 * Read from channel "idx" for as long as there is something to read.
1181 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001182 */
1183 void
1184channel_read(int idx)
1185{
1186 static char_u *buf = NULL;
1187 int len = 0;
1188 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001189 channel_T *channel = &channels[idx];
1190
1191 if (channel->ch_fd < 0)
1192 {
1193 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
1194 return;
1195 }
1196
1197 /* Allocate a buffer to read into. */
1198 if (buf == NULL)
1199 {
1200 buf = alloc(MAXMSGSIZE);
1201 if (buf == NULL)
1202 return; /* out of memory! */
1203 }
1204
1205 /* Keep on reading for as long as there is something to read.
1206 * Use select() or poll() to avoid blocking on a message that is exactly
1207 * MAXMSGSIZE long. */
1208 for (;;)
1209 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001210 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001211 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001212 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1213 if (len <= 0)
1214 break; /* error or nothing more to read */
1215
1216 /* Store the read message in the queue. */
1217 channel_save(idx, buf, len);
1218 readlen += len;
1219 if (len < MAXMSGSIZE)
1220 break; /* did read everything that's available */
1221 }
Bram Moolenaara8343c12016-02-04 22:09:48 +01001222#ifdef FEAT_GUI_W32
1223 if (len == SOCKET_ERROR)
1224 {
1225 /* For Win32 GUI channel_wait() always returns OK and we handle the
1226 * situation that there is nothing to read here.
1227 * TODO: how about a timeout? */
1228 if (WSAGetLastError() == WSAEWOULDBLOCK)
1229 return;
1230 }
1231#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01001232
1233 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1234 if (readlen <= 0)
1235 {
1236 /* Queue a "DETACH" netbeans message in the command queue in order to
1237 * terminate the netbeans session later. Do not end the session here
1238 * directly as we may be running in the context of a call to
1239 * netbeans_parse_messages():
1240 * netbeans_parse_messages
1241 * -> autocmd triggered while processing the netbeans cmd
1242 * -> ui_breakcheck
1243 * -> gui event loop or select loop
1244 * -> channel_read()
1245 */
1246 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1247
1248 channel_close(idx);
1249 if (channel->ch_close_cb != NULL)
1250 (*channel->ch_close_cb)();
1251
1252 if (len < 0)
1253 {
1254 /* Todo: which channel? */
1255 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001256 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001257 }
1258 }
1259
1260#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1261 if (CH_HAS_GUI && gtk_main_level() > 0)
1262 gtk_main_quit();
1263#endif
1264}
1265
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001266/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001267 * Read from raw channel "idx". Blocks until there is something to read or
1268 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001269 * Returns what was read in allocated memory.
1270 * Returns NULL in case of error or timeout.
1271 */
1272 char_u *
1273channel_read_block(int idx)
1274{
1275 if (channel_peek(idx) == NULL)
1276 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001277 /* Wait for up to the channel timeout. */
1278 if (channel_wait(channels[idx].ch_fd, channels[idx].ch_timeout) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001279 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001280 channel_read(idx);
1281 }
1282
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001283 return channel_get_all(idx);
1284}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001285
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001286/*
1287 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1288 * result in "rettv".
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001289 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001290 */
1291 int
1292channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1293{
Bram Moolenaard7ece102016-02-02 23:23:02 +01001294 int more;
1295
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001296 for (;;)
1297 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001298 more = channel_parse_json(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001299
1300 /* search for messsage "id" */
1301 if (channel_get_json(ch_idx, id, rettv) == OK)
1302 return OK;
1303
Bram Moolenaard7ece102016-02-02 23:23:02 +01001304 if (!more)
1305 {
1306 /* Handle any other messages in the queue. If done some more
1307 * messages may have arrived. */
1308 if (channel_parse_messages())
1309 continue;
1310
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001311 /* Wait for up to the channel timeout. */
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001312 if (channels[ch_idx].ch_fd < 0
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001313 || channel_wait(channels[ch_idx].ch_fd,
1314 channels[ch_idx].ch_timeout) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001315 break;
1316 channel_read(ch_idx);
1317 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001318 }
1319 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001320}
1321
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001322# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001323/*
1324 * Lookup the channel index from the socket.
1325 * Returns -1 when the socket isn't found.
1326 */
1327 int
1328channel_socket2idx(sock_T fd)
1329{
1330 int i;
1331
1332 if (fd >= 0)
1333 for (i = 0; i < channel_count; ++i)
1334 if (channels[i].ch_fd == fd)
1335 return i;
1336 return -1;
1337}
1338# endif
1339
Bram Moolenaard04a0202016-01-26 23:30:18 +01001340/*
1341 * Write "buf" (NUL terminated string) to channel "idx".
1342 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001343 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001344 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001345 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001346channel_send(int idx, char_u *buf, char *fun)
1347{
1348 channel_T *channel = &channels[idx];
1349 int len = (int)STRLEN(buf);
1350
1351 if (channel->ch_fd < 0)
1352 {
1353 if (!channel->ch_error && fun != NULL)
1354 {
1355 CHERROR(" %s(): write while not connected\n", fun);
1356 EMSG2("E630: %s(): write while not connected", fun);
1357 }
1358 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001359 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001360 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001361
1362 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001363 {
1364 if (!channel->ch_error && fun != NULL)
1365 {
1366 CHERROR(" %s(): write failed\n", fun);
1367 EMSG2("E631: %s(): write failed", fun);
1368 }
1369 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001370 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001371 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001372
1373 channel->ch_error = FALSE;
1374 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001375}
1376
1377# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001378/*
1379 * Add open channels to the poll struct.
1380 * Return the adjusted struct index.
1381 * The type of "fds" is hidden to avoid problems with the function proto.
1382 */
1383 int
1384channel_poll_setup(int nfd_in, void *fds_in)
1385{
1386 int nfd = nfd_in;
1387 int i;
1388 struct pollfd *fds = fds_in;
1389
1390 for (i = 0; i < channel_count; ++i)
1391 if (channels[i].ch_fd >= 0)
1392 {
1393 channels[i].ch_idx = nfd;
1394 fds[nfd].fd = channels[i].ch_fd;
1395 fds[nfd].events = POLLIN;
1396 nfd++;
1397 }
1398 else
1399 channels[i].ch_idx = -1;
1400
1401 return nfd;
1402}
1403
1404/*
1405 * The type of "fds" is hidden to avoid problems with the function proto.
1406 */
1407 int
1408channel_poll_check(int ret_in, void *fds_in)
1409{
1410 int ret = ret_in;
1411 int i;
1412 struct pollfd *fds = fds_in;
1413
1414 for (i = 0; i < channel_count; ++i)
1415 if (ret > 0 && channels[i].ch_idx != -1
1416 && fds[channels[i].ch_idx].revents & POLLIN)
1417 {
1418 channel_read(i);
1419 --ret;
1420 }
1421
1422 return ret;
1423}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001424# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001425
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001426# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001427/*
1428 * The type of "rfds" is hidden to avoid problems with the function proto.
1429 */
1430 int
1431channel_select_setup(int maxfd_in, void *rfds_in)
1432{
1433 int maxfd = maxfd_in;
1434 int i;
1435 fd_set *rfds = rfds_in;
1436
1437 for (i = 0; i < channel_count; ++i)
1438 if (channels[i].ch_fd >= 0)
1439 {
1440 FD_SET(channels[i].ch_fd, rfds);
1441 if (maxfd < channels[i].ch_fd)
1442 maxfd = channels[i].ch_fd;
1443 }
1444
1445 return maxfd;
1446}
1447
1448/*
1449 * The type of "rfds" is hidden to avoid problems with the function proto.
1450 */
1451 int
1452channel_select_check(int ret_in, void *rfds_in)
1453{
1454 int ret = ret_in;
1455 int i;
1456 fd_set *rfds = rfds_in;
1457
1458 for (i = 0; i < channel_count; ++i)
1459 if (ret > 0 && channels[i].ch_fd >= 0
1460 && FD_ISSET(channels[i].ch_fd, rfds))
1461 {
1462 channel_read(i);
1463 --ret;
1464 }
1465
1466 return ret;
1467}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001468# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001469
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001470/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01001471 * Execute queued up commands.
1472 * Invoked from the main loop when it's safe to execute received commands.
1473 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001474 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001475 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001476channel_parse_messages(void)
1477{
1478 int i;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001479 int ret = FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001480
1481 for (i = 0; i < channel_count; ++i)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001482 while (may_invoke_callback(i) == OK)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001483 {
1484 i = 0; /* start over */
1485 ret = TRUE;
1486 }
1487 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001488}
1489
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01001490/*
1491 * Mark references to lists used in channels.
1492 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01001493 int
1494set_ref_in_channel(int copyID)
1495{
1496 int i;
1497 int abort = FALSE;
1498
1499 for (i = 0; i < channel_count; ++i)
1500 {
1501 jsonq_T *head = &channels[i].ch_json_head;
1502 jsonq_T *item = head->next;
1503
1504 while (item != head)
1505 {
1506 list_T *l = item->value->vval.v_list;
1507
1508 if (l->lv_copyID != copyID)
1509 {
1510 l->lv_copyID = copyID;
1511 abort = abort || set_ref_in_list(l, copyID, NULL);
1512 }
1513 item = item->next;
1514 }
1515 }
1516 return abort;
1517}
Bram Moolenaare0874f82016-01-24 20:36:41 +01001518#endif /* FEAT_CHANNEL */