blob: cab920c22b184d1381722c05b8da0dc817c3ed3f [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 Moolenaar19d2f152016-02-01 21:38:19 +0100122 int ch_json_mode; /* TRUE for a json channel */
123 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);
443 tv.tv_sec = waittime;
444 tv.tv_usec = 0;
445 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
468 fcntl(sd, F_SETFL, 0);
469#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 Moolenaar3b5f9292016-01-28 22:37:01 +0100529 * Set the json mode of channel "idx" to TRUE or FALSE.
530 */
531 void
532channel_set_json_mode(int idx, int json_mode)
533{
534 channels[idx].ch_json_mode = json_mode;
535}
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;
675 jsonq_T *head = &channels[ch_idx].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 Moolenaard7ece102016-02-02 23:23:02 +0100688 ret = json_decode(&reader, &listtv);
689 if (ret == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100690 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100691 if (listtv.v_type != VAR_LIST)
692 {
693 /* TODO: give error */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100694 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100695 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100696 else
697 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100698 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
699 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100700 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100701 else
702 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100703 item->value = alloc_tv();
704 if (item->value == NULL)
705 {
706 vim_free(item);
707 clear_tv(&listtv);
708 }
709 else
710 {
711 *item->value = listtv;
712 item->prev = head->prev;
713 head->prev = item;
714 item->next = head;
715 item->prev->next = item;
716 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100717 }
718 }
719 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100720
721 /* Put the unread part back into the channel.
722 * TODO: insert in front */
723 if (reader.js_buf[reader.js_used] != NUL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100724 {
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100725 channel_save(ch_idx, reader.js_buf + reader.js_used,
726 (int)(reader.js_end - reader.js_buf) - reader.js_used);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100727 ret = TRUE;
728 }
729 else
730 ret = FALSE;
731
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100732 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100733 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100734}
735
736/*
737 * Remove "node" from the queue that it is in and free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +0100738 * Also frees the contained callback name.
739 */
740 static void
741remove_cb_node(cbq_T *node)
742{
743 node->prev->next = node->next;
744 node->next->prev = node->prev;
745 vim_free(node->callback);
746 vim_free(node);
747}
748
749/*
750 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100751 * Caller should have freed or used node->value.
752 */
753 static void
754remove_json_node(jsonq_T *node)
755{
756 node->prev->next = node->next;
757 node->next->prev = node->prev;
758 vim_free(node);
759}
760
761/*
762 * Get a message from the JSON queue for channel "ch_idx".
763 * When "id" is positive it must match the first number in the list.
764 * When "id" is zero or negative jut get the first message.
765 * Return OK when found and return the value in "rettv".
766 * Return FAIL otherwise.
767 */
768 static int
769channel_get_json(int ch_idx, int id, typval_T **rettv)
770{
771 jsonq_T *head = &channels[ch_idx].ch_json_head;
772 jsonq_T *item = head->next;
773
774 while (item != head)
775 {
776 list_T *l = item->value->vval.v_list;
777 typval_T *tv = &l->lv_first->li_tv;
778
779 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100780 || id <= 0)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100781 {
782 *rettv = item->value;
783 remove_json_node(item);
784 return OK;
785 }
786 item = item->next;
787 }
788 return FAIL;
789}
790
791/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100792 * Execute a command received over channel "idx".
793 * "cmd" is the command string, "arg2" the second argument.
794 * "arg3" is the third argument, NULL if missing.
795 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100796 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100797channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100798{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100799 char_u *arg;
800
801 if (arg2->v_type != VAR_STRING)
802 {
803 if (p_verbose > 2)
804 EMSG("E903: received ex command with non-string argument");
805 return;
806 }
807 arg = arg2->vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100808 if (arg == NULL)
809 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100810
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100811 if (STRCMP(cmd, "ex") == 0)
812 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100813 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100814 }
815 else if (STRCMP(cmd, "normal") == 0)
816 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100817 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100818
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100819 ea.arg = arg;
820 ea.addr_count = 0;
821 ea.forceit = TRUE; /* no mapping */
822 ex_normal(&ea);
823 }
824 else if (STRCMP(cmd, "redraw") == 0)
825 {
826 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100827
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100828 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100829 ex_redraw(&ea);
830 showruler(FALSE);
831 setcursor();
832 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100833#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100834 if (gui.in_use)
835 {
836 gui_update_cursor(FALSE, FALSE);
837 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100838 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100839#endif
840 }
841 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
842 {
843 int is_eval = cmd[1] == 'v';
844
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100845 if (is_eval && (arg3 == NULL || arg3->v_type != VAR_NUMBER))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100846 {
847 if (p_verbose > 2)
848 EMSG("E904: third argument for eval must be a number");
849 }
850 else
851 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100852 typval_T *tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100853 typval_T err_tv;
854 char_u *json;
855
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100856 /* Don't pollute the display with errors. */
857 ++emsg_skip;
858 tv = eval_expr(arg, NULL);
859 --emsg_skip;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100860 if (is_eval)
861 {
862 if (tv == NULL)
863 {
864 err_tv.v_type = VAR_STRING;
865 err_tv.vval.v_string = (char_u *)"ERROR";
866 tv = &err_tv;
867 }
868 json = json_encode_nr_expr(arg3->vval.v_number, tv);
869 channel_send(idx, json, "eval");
870 vim_free(json);
871 }
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100872 if (tv != &err_tv)
873 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100874 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100875 }
876 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100877 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100878}
879
880/*
881 * Invoke a callback for channel "idx" if needed.
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100882 * Return OK when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100883 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100884 static int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100885may_invoke_callback(int idx)
886{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100887 char_u *msg = NULL;
888 typval_T *listtv = NULL;
889 list_T *list;
890 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100891 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100892 int seq_nr = -1;
Bram Moolenaara07fec92016-02-05 21:04:08 +0100893 channel_T *channel = &channels[idx];
894 int json_mode = channel->ch_json_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100895
Bram Moolenaara07fec92016-02-05 21:04:08 +0100896 if (channel->ch_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100897 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100898 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100899
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100900 if (json_mode)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100901 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100902 /* Get any json message in the queue. */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100903 if (channel_get_json(idx, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100904 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100905 /* Parse readahead, return when there is still no message. */
906 channel_parse_json(idx);
907 if (channel_get_json(idx, -1, &listtv) == FAIL)
908 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100909 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100910
911 list = listtv->vval.v_list;
912 if (list->lv_len < 2)
913 {
914 /* TODO: give error */
915 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100916 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100917 }
918
919 argv[1] = list->lv_first->li_next->li_tv;
920 typetv = &list->lv_first->li_tv;
921 if (typetv->v_type == VAR_STRING)
922 {
923 typval_T *arg3 = NULL;
924 char_u *cmd = typetv->vval.v_string;
925
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100926 /* ["cmd", arg] or ["cmd", arg, arg] */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100927 if (list->lv_len == 3)
928 arg3 = &list->lv_last->li_tv;
929 channel_exe_cmd(idx, cmd, &argv[1], arg3);
930 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100931 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100932 }
933
934 if (typetv->v_type != VAR_NUMBER)
935 {
936 /* TODO: give error */
937 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100938 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100939 }
940 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100941 }
Bram Moolenaard7ece102016-02-02 23:23:02 +0100942 else if (channel_peek(idx) == NULL)
943 {
944 /* nothing to read on raw channel */
945 return FALSE;
946 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100947 else
948 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100949 /* For a raw channel we don't know where the message ends, just get
950 * everything. */
951 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100952 argv[1].v_type = VAR_STRING;
953 argv[1].vval.v_string = msg;
954 }
955
Bram Moolenaara07fec92016-02-05 21:04:08 +0100956 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100957 {
Bram Moolenaara07fec92016-02-05 21:04:08 +0100958 cbq_T *cbhead = &channel->ch_cb_head;
959 cbq_T *cbitem = cbhead->next;
960
961 /* invoke the one-time callback with the matching nr */
962 while (cbitem != cbhead)
963 {
964 if (cbitem->seq_nr == seq_nr)
965 {
966 invoke_callback(idx, cbitem->callback, argv);
967 remove_cb_node(cbitem);
968 break;
969 }
970 cbitem = cbitem->next;
971 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100972 }
Bram Moolenaara07fec92016-02-05 21:04:08 +0100973 else if (channel->ch_callback != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100974 {
975 /* invoke the channel callback */
Bram Moolenaara07fec92016-02-05 21:04:08 +0100976 invoke_callback(idx, channel->ch_callback, argv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100977 }
978 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100979
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100980 if (listtv != NULL)
981 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100982 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100983
984 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100985}
986
987/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100988 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100989 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +0100990 */
991 int
992channel_is_open(int idx)
993{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100994 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100995}
996
997/*
998 * Close channel "idx".
999 * This does not trigger the close callback.
1000 */
1001 void
1002channel_close(int idx)
1003{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001004 channel_T *channel = &channels[idx];
1005 jsonq_T *jhead;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001006 cbq_T *cbhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001007
1008 if (channel->ch_fd >= 0)
1009 {
1010 sock_close(channel->ch_fd);
1011 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001012 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001013#ifdef FEAT_GUI
1014 channel_gui_unregister(idx);
1015#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001016 vim_free(channel->ch_callback);
1017 channel->ch_callback = NULL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001018 channel->ch_timeout = 2000;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001019
1020 while (channel_peek(idx) != NULL)
1021 vim_free(channel_get(idx));
1022
Bram Moolenaara07fec92016-02-05 21:04:08 +01001023 cbhead = &channel->ch_cb_head;
1024 while (cbhead->next != cbhead)
1025 remove_cb_node(cbhead->next);
1026
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001027 jhead = &channel->ch_json_head;
1028 while (jhead->next != jhead)
1029 {
1030 clear_tv(jhead->next->value);
1031 remove_json_node(jhead->next);
1032 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01001033 }
1034}
1035
Bram Moolenaard04a0202016-01-26 23:30:18 +01001036/*
1037 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +01001038 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001039 */
Bram Moolenaar83162462016-01-28 23:10:07 +01001040 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001041channel_save(int idx, char_u *buf, int len)
1042{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001043 readq_T *node;
1044 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001045
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001046 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001047 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +01001048 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001049 node->buffer = alloc(len + 1);
1050 if (node->buffer == NULL)
1051 {
1052 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +01001053 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001054 }
1055 mch_memmove(node->buffer, buf, (size_t)len);
1056 node->buffer[len] = NUL;
1057
Bram Moolenaard04a0202016-01-26 23:30:18 +01001058 /* insert node at tail of queue */
1059 node->next = head;
1060 node->prev = head->prev;
1061 head->prev->next = node;
1062 head->prev = node;
1063
1064 if (debugfd != NULL)
1065 {
1066 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +01001067 if (fwrite(buf, len, 1, debugfd) != 1)
1068 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001069 fprintf(debugfd, "\n");
1070 }
Bram Moolenaar83162462016-01-28 23:10:07 +01001071 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001072}
1073
1074/*
1075 * Return the first buffer from the channel without removing it.
1076 * Returns NULL if there is nothing.
1077 */
1078 char_u *
1079channel_peek(int idx)
1080{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001081 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001082
1083 if (head->next == head || head->next == NULL)
1084 return NULL;
1085 return head->next->buffer;
1086}
1087
1088/*
Bram Moolenaard04a0202016-01-26 23:30:18 +01001089 * Clear the read buffer on channel "idx".
1090 */
1091 void
1092channel_clear(int idx)
1093{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001094 readq_T *head = &channels[idx].ch_head;
1095 readq_T *node = head->next;
1096 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001097
1098 while (node != NULL && node != head)
1099 {
1100 next = node->next;
1101 vim_free(node->buffer);
1102 vim_free(node);
1103 if (next == head)
1104 {
1105 head->next = head;
1106 head->prev = head;
1107 break;
1108 }
1109 node = next;
1110 }
1111}
1112
1113/* Sent when the channel is found closed when reading. */
1114#define DETACH_MSG "\"DETACH\"\n"
1115
1116/* Buffer size for reading incoming messages. */
1117#define MAXMSGSIZE 4096
1118
1119/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001120 * Check for reading from "fd" with "timeout" msec.
1121 * Return FAIL when there is nothing to read.
Bram Moolenaara8343c12016-02-04 22:09:48 +01001122 * Always returns OK for FEAT_GUI_W32.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001123 */
1124 static int
1125channel_wait(int fd, int timeout)
1126{
Bram Moolenaara8343c12016-02-04 22:09:48 +01001127#if defined(HAVE_SELECT) && !defined(FEAT_GUI_W32)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001128 struct timeval tval;
1129 fd_set rfds;
1130 int ret;
1131
1132 FD_ZERO(&rfds);
1133 FD_SET(fd, &rfds);
1134 tval.tv_sec = timeout / 1000;
1135 tval.tv_usec = (timeout % 1000) * 1000;
1136 for (;;)
1137 {
1138 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
1139# ifdef EINTR
1140 if (ret == -1 && errno == EINTR)
1141 continue;
1142# endif
1143 if (ret <= 0)
1144 return FAIL;
1145 break;
1146 }
1147#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001148# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001149 struct pollfd fds;
1150
1151 fds.fd = fd;
1152 fds.events = POLLIN;
1153 if (poll(&fds, 1, timeout) <= 0)
1154 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001155# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001156#endif
1157 return OK;
1158}
1159
1160/*
1161 * Return a unique ID to be used in a message.
1162 */
1163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001164channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001165{
1166 static int next_id = 1;
1167
1168 return next_id++;
1169}
1170
1171/*
1172 * Read from channel "idx" for as long as there is something to read.
1173 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001174 */
1175 void
1176channel_read(int idx)
1177{
1178 static char_u *buf = NULL;
1179 int len = 0;
1180 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001181 channel_T *channel = &channels[idx];
1182
1183 if (channel->ch_fd < 0)
1184 {
1185 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
1186 return;
1187 }
1188
1189 /* Allocate a buffer to read into. */
1190 if (buf == NULL)
1191 {
1192 buf = alloc(MAXMSGSIZE);
1193 if (buf == NULL)
1194 return; /* out of memory! */
1195 }
1196
1197 /* Keep on reading for as long as there is something to read.
1198 * Use select() or poll() to avoid blocking on a message that is exactly
1199 * MAXMSGSIZE long. */
1200 for (;;)
1201 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001202 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001203 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001204 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1205 if (len <= 0)
1206 break; /* error or nothing more to read */
1207
1208 /* Store the read message in the queue. */
1209 channel_save(idx, buf, len);
1210 readlen += len;
1211 if (len < MAXMSGSIZE)
1212 break; /* did read everything that's available */
1213 }
Bram Moolenaara8343c12016-02-04 22:09:48 +01001214#ifdef FEAT_GUI_W32
1215 if (len == SOCKET_ERROR)
1216 {
1217 /* For Win32 GUI channel_wait() always returns OK and we handle the
1218 * situation that there is nothing to read here.
1219 * TODO: how about a timeout? */
1220 if (WSAGetLastError() == WSAEWOULDBLOCK)
1221 return;
1222 }
1223#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01001224
1225 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1226 if (readlen <= 0)
1227 {
1228 /* Queue a "DETACH" netbeans message in the command queue in order to
1229 * terminate the netbeans session later. Do not end the session here
1230 * directly as we may be running in the context of a call to
1231 * netbeans_parse_messages():
1232 * netbeans_parse_messages
1233 * -> autocmd triggered while processing the netbeans cmd
1234 * -> ui_breakcheck
1235 * -> gui event loop or select loop
1236 * -> channel_read()
1237 */
1238 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1239
1240 channel_close(idx);
1241 if (channel->ch_close_cb != NULL)
1242 (*channel->ch_close_cb)();
1243
1244 if (len < 0)
1245 {
1246 /* Todo: which channel? */
1247 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001248 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001249 }
1250 }
1251
1252#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1253 if (CH_HAS_GUI && gtk_main_level() > 0)
1254 gtk_main_quit();
1255#endif
1256}
1257
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001258/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001259 * Read from raw channel "idx". Blocks until there is something to read or
1260 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001261 * Returns what was read in allocated memory.
1262 * Returns NULL in case of error or timeout.
1263 */
1264 char_u *
1265channel_read_block(int idx)
1266{
1267 if (channel_peek(idx) == NULL)
1268 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001269 /* Wait for up to the channel timeout. */
1270 if (channel_wait(channels[idx].ch_fd, channels[idx].ch_timeout) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001271 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001272 channel_read(idx);
1273 }
1274
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001275 return channel_get_all(idx);
1276}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001277
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001278/*
1279 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1280 * result in "rettv".
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001281 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001282 */
1283 int
1284channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1285{
Bram Moolenaard7ece102016-02-02 23:23:02 +01001286 int more;
1287
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001288 for (;;)
1289 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001290 more = channel_parse_json(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001291
1292 /* search for messsage "id" */
1293 if (channel_get_json(ch_idx, id, rettv) == OK)
1294 return OK;
1295
Bram Moolenaard7ece102016-02-02 23:23:02 +01001296 if (!more)
1297 {
1298 /* Handle any other messages in the queue. If done some more
1299 * messages may have arrived. */
1300 if (channel_parse_messages())
1301 continue;
1302
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001303 /* Wait for up to the channel timeout. */
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001304 if (channels[ch_idx].ch_fd < 0
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001305 || channel_wait(channels[ch_idx].ch_fd,
1306 channels[ch_idx].ch_timeout) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001307 break;
1308 channel_read(ch_idx);
1309 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001310 }
1311 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001312}
1313
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001314# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001315/*
1316 * Lookup the channel index from the socket.
1317 * Returns -1 when the socket isn't found.
1318 */
1319 int
1320channel_socket2idx(sock_T fd)
1321{
1322 int i;
1323
1324 if (fd >= 0)
1325 for (i = 0; i < channel_count; ++i)
1326 if (channels[i].ch_fd == fd)
1327 return i;
1328 return -1;
1329}
1330# endif
1331
Bram Moolenaard04a0202016-01-26 23:30:18 +01001332/*
1333 * Write "buf" (NUL terminated string) to channel "idx".
1334 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001335 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001336 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001337 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001338channel_send(int idx, char_u *buf, char *fun)
1339{
1340 channel_T *channel = &channels[idx];
1341 int len = (int)STRLEN(buf);
1342
1343 if (channel->ch_fd < 0)
1344 {
1345 if (!channel->ch_error && fun != NULL)
1346 {
1347 CHERROR(" %s(): write while not connected\n", fun);
1348 EMSG2("E630: %s(): write while not connected", fun);
1349 }
1350 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001351 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001352 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001353
1354 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001355 {
1356 if (!channel->ch_error && fun != NULL)
1357 {
1358 CHERROR(" %s(): write failed\n", fun);
1359 EMSG2("E631: %s(): write failed", fun);
1360 }
1361 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001362 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001363 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001364
1365 channel->ch_error = FALSE;
1366 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001367}
1368
1369# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001370/*
1371 * Add open channels to the poll struct.
1372 * Return the adjusted struct index.
1373 * The type of "fds" is hidden to avoid problems with the function proto.
1374 */
1375 int
1376channel_poll_setup(int nfd_in, void *fds_in)
1377{
1378 int nfd = nfd_in;
1379 int i;
1380 struct pollfd *fds = fds_in;
1381
1382 for (i = 0; i < channel_count; ++i)
1383 if (channels[i].ch_fd >= 0)
1384 {
1385 channels[i].ch_idx = nfd;
1386 fds[nfd].fd = channels[i].ch_fd;
1387 fds[nfd].events = POLLIN;
1388 nfd++;
1389 }
1390 else
1391 channels[i].ch_idx = -1;
1392
1393 return nfd;
1394}
1395
1396/*
1397 * The type of "fds" is hidden to avoid problems with the function proto.
1398 */
1399 int
1400channel_poll_check(int ret_in, void *fds_in)
1401{
1402 int ret = ret_in;
1403 int i;
1404 struct pollfd *fds = fds_in;
1405
1406 for (i = 0; i < channel_count; ++i)
1407 if (ret > 0 && channels[i].ch_idx != -1
1408 && fds[channels[i].ch_idx].revents & POLLIN)
1409 {
1410 channel_read(i);
1411 --ret;
1412 }
1413
1414 return ret;
1415}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001416# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001417
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001418# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001419/*
1420 * The type of "rfds" is hidden to avoid problems with the function proto.
1421 */
1422 int
1423channel_select_setup(int maxfd_in, void *rfds_in)
1424{
1425 int maxfd = maxfd_in;
1426 int i;
1427 fd_set *rfds = rfds_in;
1428
1429 for (i = 0; i < channel_count; ++i)
1430 if (channels[i].ch_fd >= 0)
1431 {
1432 FD_SET(channels[i].ch_fd, rfds);
1433 if (maxfd < channels[i].ch_fd)
1434 maxfd = channels[i].ch_fd;
1435 }
1436
1437 return maxfd;
1438}
1439
1440/*
1441 * The type of "rfds" is hidden to avoid problems with the function proto.
1442 */
1443 int
1444channel_select_check(int ret_in, void *rfds_in)
1445{
1446 int ret = ret_in;
1447 int i;
1448 fd_set *rfds = rfds_in;
1449
1450 for (i = 0; i < channel_count; ++i)
1451 if (ret > 0 && channels[i].ch_fd >= 0
1452 && FD_ISSET(channels[i].ch_fd, rfds))
1453 {
1454 channel_read(i);
1455 --ret;
1456 }
1457
1458 return ret;
1459}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001460# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001461
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001462/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01001463 * Execute queued up commands.
1464 * Invoked from the main loop when it's safe to execute received commands.
1465 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001466 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001467 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001468channel_parse_messages(void)
1469{
1470 int i;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001471 int ret = FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001472
1473 for (i = 0; i < channel_count; ++i)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001474 while (may_invoke_callback(i) == OK)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001475 {
1476 i = 0; /* start over */
1477 ret = TRUE;
1478 }
1479 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001480}
1481
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01001482 int
1483set_ref_in_channel(int copyID)
1484{
1485 int i;
1486 int abort = FALSE;
1487
1488 for (i = 0; i < channel_count; ++i)
1489 {
1490 jsonq_T *head = &channels[i].ch_json_head;
1491 jsonq_T *item = head->next;
1492
1493 while (item != head)
1494 {
1495 list_T *l = item->value->vval.v_list;
1496
1497 if (l->lv_copyID != copyID)
1498 {
1499 l->lv_copyID = copyID;
1500 abort = abort || set_ref_in_list(l, copyID, NULL);
1501 }
1502 item = item->next;
1503 }
1504 }
1505 return abort;
1506}
Bram Moolenaare0874f82016-01-24 20:36:41 +01001507#endif /* FEAT_CHANNEL */