blob: 811d87ae1c2b7c1651059f6ab72a930081612581 [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);
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 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 Moolenaar6076fe12016-02-05 22:49:56 +0100691 /* Only accept the response when it is a list with at least two
692 * items. */
693 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100694 {
695 /* TODO: give error */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100696 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100697 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100698 else
699 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100700 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
701 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100702 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100703 else
704 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100705 item->value = alloc_tv();
706 if (item->value == NULL)
707 {
708 vim_free(item);
709 clear_tv(&listtv);
710 }
711 else
712 {
713 *item->value = listtv;
714 item->prev = head->prev;
715 head->prev = item;
716 item->next = head;
717 item->prev->next = item;
718 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100719 }
720 }
721 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100722
723 /* Put the unread part back into the channel.
724 * TODO: insert in front */
725 if (reader.js_buf[reader.js_used] != NUL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100726 {
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100727 channel_save(ch_idx, reader.js_buf + reader.js_used,
728 (int)(reader.js_end - reader.js_buf) - reader.js_used);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100729 ret = TRUE;
730 }
731 else
732 ret = FALSE;
733
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100734 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100735 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100736}
737
738/*
739 * Remove "node" from the queue that it is in and free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +0100740 * Also frees the contained callback name.
741 */
742 static void
743remove_cb_node(cbq_T *node)
744{
745 node->prev->next = node->next;
746 node->next->prev = node->prev;
747 vim_free(node->callback);
748 vim_free(node);
749}
750
751/*
752 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100753 * Caller should have freed or used node->value.
754 */
755 static void
756remove_json_node(jsonq_T *node)
757{
758 node->prev->next = node->next;
759 node->next->prev = node->prev;
760 vim_free(node);
761}
762
763/*
764 * Get a message from the JSON queue for channel "ch_idx".
765 * When "id" is positive it must match the first number in the list.
766 * When "id" is zero or negative jut get the first message.
767 * Return OK when found and return the value in "rettv".
768 * Return FAIL otherwise.
769 */
770 static int
771channel_get_json(int ch_idx, int id, typval_T **rettv)
772{
773 jsonq_T *head = &channels[ch_idx].ch_json_head;
774 jsonq_T *item = head->next;
775
776 while (item != head)
777 {
778 list_T *l = item->value->vval.v_list;
779 typval_T *tv = &l->lv_first->li_tv;
780
781 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100782 || id <= 0)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100783 {
784 *rettv = item->value;
785 remove_json_node(item);
786 return OK;
787 }
788 item = item->next;
789 }
790 return FAIL;
791}
792
793/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100794 * Execute a command received over channel "idx".
795 * "cmd" is the command string, "arg2" the second argument.
796 * "arg3" is the third argument, NULL if missing.
797 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100798 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100799channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100800{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100801 char_u *arg;
802
803 if (arg2->v_type != VAR_STRING)
804 {
805 if (p_verbose > 2)
806 EMSG("E903: received ex command with non-string argument");
807 return;
808 }
809 arg = arg2->vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100810 if (arg == NULL)
811 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100812
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100813 if (STRCMP(cmd, "ex") == 0)
814 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100815 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100816 }
817 else if (STRCMP(cmd, "normal") == 0)
818 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100819 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100820
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100821 ea.arg = arg;
822 ea.addr_count = 0;
823 ea.forceit = TRUE; /* no mapping */
824 ex_normal(&ea);
825 }
826 else if (STRCMP(cmd, "redraw") == 0)
827 {
828 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100829
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100830 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100831 ex_redraw(&ea);
832 showruler(FALSE);
833 setcursor();
834 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100835#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100836 if (gui.in_use)
837 {
838 gui_update_cursor(FALSE, FALSE);
839 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100840 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100841#endif
842 }
843 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
844 {
845 int is_eval = cmd[1] == 'v';
846
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100847 if (is_eval && (arg3 == NULL || arg3->v_type != VAR_NUMBER))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100848 {
849 if (p_verbose > 2)
850 EMSG("E904: third argument for eval must be a number");
851 }
852 else
853 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100854 typval_T *tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100855 typval_T err_tv;
856 char_u *json;
857
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100858 /* Don't pollute the display with errors. */
859 ++emsg_skip;
860 tv = eval_expr(arg, NULL);
861 --emsg_skip;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100862 if (is_eval)
863 {
864 if (tv == NULL)
865 {
866 err_tv.v_type = VAR_STRING;
867 err_tv.vval.v_string = (char_u *)"ERROR";
868 tv = &err_tv;
869 }
870 json = json_encode_nr_expr(arg3->vval.v_number, tv);
871 channel_send(idx, json, "eval");
872 vim_free(json);
873 }
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100874 if (tv != &err_tv)
875 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100876 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100877 }
878 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100879 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100880}
881
882/*
883 * Invoke a callback for channel "idx" if needed.
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100884 * Return OK when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100885 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100886 static int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100887may_invoke_callback(int idx)
888{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100889 char_u *msg = NULL;
890 typval_T *listtv = NULL;
891 list_T *list;
892 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100893 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100894 int seq_nr = -1;
Bram Moolenaara07fec92016-02-05 21:04:08 +0100895 channel_T *channel = &channels[idx];
896 int json_mode = channel->ch_json_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100897
Bram Moolenaara07fec92016-02-05 21:04:08 +0100898 if (channel->ch_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100899 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100900 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100901
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100902 if (json_mode)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100903 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100904 /* Get any json message in the queue. */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100905 if (channel_get_json(idx, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100906 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100907 /* Parse readahead, return when there is still no message. */
908 channel_parse_json(idx);
909 if (channel_get_json(idx, -1, &listtv) == FAIL)
910 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100911 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100912
913 list = listtv->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100914 argv[1] = list->lv_first->li_next->li_tv;
915 typetv = &list->lv_first->li_tv;
916 if (typetv->v_type == VAR_STRING)
917 {
918 typval_T *arg3 = NULL;
919 char_u *cmd = typetv->vval.v_string;
920
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100921 /* ["cmd", arg] or ["cmd", arg, arg] */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100922 if (list->lv_len == 3)
923 arg3 = &list->lv_last->li_tv;
924 channel_exe_cmd(idx, cmd, &argv[1], arg3);
925 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100926 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100927 }
928
929 if (typetv->v_type != VAR_NUMBER)
930 {
931 /* TODO: give error */
932 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100933 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100934 }
935 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100936 }
Bram Moolenaard7ece102016-02-02 23:23:02 +0100937 else if (channel_peek(idx) == NULL)
938 {
939 /* nothing to read on raw channel */
940 return FALSE;
941 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100942 else
943 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100944 /* For a raw channel we don't know where the message ends, just get
945 * everything. */
946 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100947 argv[1].v_type = VAR_STRING;
948 argv[1].vval.v_string = msg;
949 }
950
Bram Moolenaara07fec92016-02-05 21:04:08 +0100951 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100952 {
Bram Moolenaara07fec92016-02-05 21:04:08 +0100953 cbq_T *cbhead = &channel->ch_cb_head;
954 cbq_T *cbitem = cbhead->next;
955
956 /* invoke the one-time callback with the matching nr */
957 while (cbitem != cbhead)
958 {
959 if (cbitem->seq_nr == seq_nr)
960 {
961 invoke_callback(idx, cbitem->callback, argv);
962 remove_cb_node(cbitem);
963 break;
964 }
965 cbitem = cbitem->next;
966 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100967 }
Bram Moolenaara07fec92016-02-05 21:04:08 +0100968 else if (channel->ch_callback != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100969 {
970 /* invoke the channel callback */
Bram Moolenaara07fec92016-02-05 21:04:08 +0100971 invoke_callback(idx, channel->ch_callback, argv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100972 }
973 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100974
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100975 if (listtv != NULL)
976 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100977 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100978
979 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100980}
981
982/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100983 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100984 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +0100985 */
986 int
987channel_is_open(int idx)
988{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100989 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100990}
991
992/*
993 * Close channel "idx".
994 * This does not trigger the close callback.
995 */
996 void
997channel_close(int idx)
998{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100999 channel_T *channel = &channels[idx];
1000 jsonq_T *jhead;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001001 cbq_T *cbhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001002
1003 if (channel->ch_fd >= 0)
1004 {
1005 sock_close(channel->ch_fd);
1006 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001007 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001008#ifdef FEAT_GUI
1009 channel_gui_unregister(idx);
1010#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001011 vim_free(channel->ch_callback);
1012 channel->ch_callback = NULL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001013 channel->ch_timeout = 2000;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001014
1015 while (channel_peek(idx) != NULL)
1016 vim_free(channel_get(idx));
1017
Bram Moolenaara07fec92016-02-05 21:04:08 +01001018 cbhead = &channel->ch_cb_head;
1019 while (cbhead->next != cbhead)
1020 remove_cb_node(cbhead->next);
1021
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001022 jhead = &channel->ch_json_head;
1023 while (jhead->next != jhead)
1024 {
1025 clear_tv(jhead->next->value);
1026 remove_json_node(jhead->next);
1027 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01001028 }
1029}
1030
Bram Moolenaard04a0202016-01-26 23:30:18 +01001031/*
1032 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +01001033 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001034 */
Bram Moolenaar83162462016-01-28 23:10:07 +01001035 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001036channel_save(int idx, char_u *buf, int len)
1037{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001038 readq_T *node;
1039 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001040
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001041 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001042 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +01001043 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001044 node->buffer = alloc(len + 1);
1045 if (node->buffer == NULL)
1046 {
1047 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +01001048 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001049 }
1050 mch_memmove(node->buffer, buf, (size_t)len);
1051 node->buffer[len] = NUL;
1052
Bram Moolenaard04a0202016-01-26 23:30:18 +01001053 /* insert node at tail of queue */
1054 node->next = head;
1055 node->prev = head->prev;
1056 head->prev->next = node;
1057 head->prev = node;
1058
1059 if (debugfd != NULL)
1060 {
1061 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +01001062 if (fwrite(buf, len, 1, debugfd) != 1)
1063 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001064 fprintf(debugfd, "\n");
1065 }
Bram Moolenaar83162462016-01-28 23:10:07 +01001066 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001067}
1068
1069/*
1070 * Return the first buffer from the channel without removing it.
1071 * Returns NULL if there is nothing.
1072 */
1073 char_u *
1074channel_peek(int idx)
1075{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001076 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001077
1078 if (head->next == head || head->next == NULL)
1079 return NULL;
1080 return head->next->buffer;
1081}
1082
1083/*
Bram Moolenaard04a0202016-01-26 23:30:18 +01001084 * Clear the read buffer on channel "idx".
1085 */
1086 void
1087channel_clear(int idx)
1088{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001089 readq_T *head = &channels[idx].ch_head;
1090 readq_T *node = head->next;
1091 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001092
1093 while (node != NULL && node != head)
1094 {
1095 next = node->next;
1096 vim_free(node->buffer);
1097 vim_free(node);
1098 if (next == head)
1099 {
1100 head->next = head;
1101 head->prev = head;
1102 break;
1103 }
1104 node = next;
1105 }
1106}
1107
1108/* Sent when the channel is found closed when reading. */
1109#define DETACH_MSG "\"DETACH\"\n"
1110
1111/* Buffer size for reading incoming messages. */
1112#define MAXMSGSIZE 4096
1113
1114/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001115 * Check for reading from "fd" with "timeout" msec.
1116 * Return FAIL when there is nothing to read.
Bram Moolenaara8343c12016-02-04 22:09:48 +01001117 * Always returns OK for FEAT_GUI_W32.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001118 */
1119 static int
1120channel_wait(int fd, int timeout)
1121{
Bram Moolenaara8343c12016-02-04 22:09:48 +01001122#if defined(HAVE_SELECT) && !defined(FEAT_GUI_W32)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001123 struct timeval tval;
1124 fd_set rfds;
1125 int ret;
1126
1127 FD_ZERO(&rfds);
1128 FD_SET(fd, &rfds);
1129 tval.tv_sec = timeout / 1000;
1130 tval.tv_usec = (timeout % 1000) * 1000;
1131 for (;;)
1132 {
1133 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
1134# ifdef EINTR
1135 if (ret == -1 && errno == EINTR)
1136 continue;
1137# endif
1138 if (ret <= 0)
1139 return FAIL;
1140 break;
1141 }
1142#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001143# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001144 struct pollfd fds;
1145
1146 fds.fd = fd;
1147 fds.events = POLLIN;
1148 if (poll(&fds, 1, timeout) <= 0)
1149 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001150# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001151#endif
1152 return OK;
1153}
1154
1155/*
1156 * Return a unique ID to be used in a message.
1157 */
1158 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001159channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001160{
1161 static int next_id = 1;
1162
1163 return next_id++;
1164}
1165
1166/*
1167 * Read from channel "idx" for as long as there is something to read.
1168 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001169 */
1170 void
1171channel_read(int idx)
1172{
1173 static char_u *buf = NULL;
1174 int len = 0;
1175 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001176 channel_T *channel = &channels[idx];
1177
1178 if (channel->ch_fd < 0)
1179 {
1180 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
1181 return;
1182 }
1183
1184 /* Allocate a buffer to read into. */
1185 if (buf == NULL)
1186 {
1187 buf = alloc(MAXMSGSIZE);
1188 if (buf == NULL)
1189 return; /* out of memory! */
1190 }
1191
1192 /* Keep on reading for as long as there is something to read.
1193 * Use select() or poll() to avoid blocking on a message that is exactly
1194 * MAXMSGSIZE long. */
1195 for (;;)
1196 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001197 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001198 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001199 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1200 if (len <= 0)
1201 break; /* error or nothing more to read */
1202
1203 /* Store the read message in the queue. */
1204 channel_save(idx, buf, len);
1205 readlen += len;
1206 if (len < MAXMSGSIZE)
1207 break; /* did read everything that's available */
1208 }
Bram Moolenaara8343c12016-02-04 22:09:48 +01001209#ifdef FEAT_GUI_W32
1210 if (len == SOCKET_ERROR)
1211 {
1212 /* For Win32 GUI channel_wait() always returns OK and we handle the
1213 * situation that there is nothing to read here.
1214 * TODO: how about a timeout? */
1215 if (WSAGetLastError() == WSAEWOULDBLOCK)
1216 return;
1217 }
1218#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01001219
1220 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1221 if (readlen <= 0)
1222 {
1223 /* Queue a "DETACH" netbeans message in the command queue in order to
1224 * terminate the netbeans session later. Do not end the session here
1225 * directly as we may be running in the context of a call to
1226 * netbeans_parse_messages():
1227 * netbeans_parse_messages
1228 * -> autocmd triggered while processing the netbeans cmd
1229 * -> ui_breakcheck
1230 * -> gui event loop or select loop
1231 * -> channel_read()
1232 */
1233 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1234
1235 channel_close(idx);
1236 if (channel->ch_close_cb != NULL)
1237 (*channel->ch_close_cb)();
1238
1239 if (len < 0)
1240 {
1241 /* Todo: which channel? */
1242 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001243 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001244 }
1245 }
1246
1247#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1248 if (CH_HAS_GUI && gtk_main_level() > 0)
1249 gtk_main_quit();
1250#endif
1251}
1252
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001253/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001254 * Read from raw channel "idx". Blocks until there is something to read or
1255 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001256 * Returns what was read in allocated memory.
1257 * Returns NULL in case of error or timeout.
1258 */
1259 char_u *
1260channel_read_block(int idx)
1261{
1262 if (channel_peek(idx) == NULL)
1263 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001264 /* Wait for up to the channel timeout. */
1265 if (channel_wait(channels[idx].ch_fd, channels[idx].ch_timeout) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001266 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001267 channel_read(idx);
1268 }
1269
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001270 return channel_get_all(idx);
1271}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001272
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001273/*
1274 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1275 * result in "rettv".
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001276 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001277 */
1278 int
1279channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1280{
Bram Moolenaard7ece102016-02-02 23:23:02 +01001281 int more;
1282
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001283 for (;;)
1284 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001285 more = channel_parse_json(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001286
1287 /* search for messsage "id" */
1288 if (channel_get_json(ch_idx, id, rettv) == OK)
1289 return OK;
1290
Bram Moolenaard7ece102016-02-02 23:23:02 +01001291 if (!more)
1292 {
1293 /* Handle any other messages in the queue. If done some more
1294 * messages may have arrived. */
1295 if (channel_parse_messages())
1296 continue;
1297
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001298 /* Wait for up to the channel timeout. */
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001299 if (channels[ch_idx].ch_fd < 0
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001300 || channel_wait(channels[ch_idx].ch_fd,
1301 channels[ch_idx].ch_timeout) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001302 break;
1303 channel_read(ch_idx);
1304 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001305 }
1306 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001307}
1308
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001309# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001310/*
1311 * Lookup the channel index from the socket.
1312 * Returns -1 when the socket isn't found.
1313 */
1314 int
1315channel_socket2idx(sock_T fd)
1316{
1317 int i;
1318
1319 if (fd >= 0)
1320 for (i = 0; i < channel_count; ++i)
1321 if (channels[i].ch_fd == fd)
1322 return i;
1323 return -1;
1324}
1325# endif
1326
Bram Moolenaard04a0202016-01-26 23:30:18 +01001327/*
1328 * Write "buf" (NUL terminated string) to channel "idx".
1329 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001330 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001331 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001332 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001333channel_send(int idx, char_u *buf, char *fun)
1334{
1335 channel_T *channel = &channels[idx];
1336 int len = (int)STRLEN(buf);
1337
1338 if (channel->ch_fd < 0)
1339 {
1340 if (!channel->ch_error && fun != NULL)
1341 {
1342 CHERROR(" %s(): write while not connected\n", fun);
1343 EMSG2("E630: %s(): write while not connected", fun);
1344 }
1345 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001346 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001347 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001348
1349 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001350 {
1351 if (!channel->ch_error && fun != NULL)
1352 {
1353 CHERROR(" %s(): write failed\n", fun);
1354 EMSG2("E631: %s(): write failed", fun);
1355 }
1356 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001357 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001358 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001359
1360 channel->ch_error = FALSE;
1361 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001362}
1363
1364# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001365/*
1366 * Add open channels to the poll struct.
1367 * Return the adjusted struct index.
1368 * The type of "fds" is hidden to avoid problems with the function proto.
1369 */
1370 int
1371channel_poll_setup(int nfd_in, void *fds_in)
1372{
1373 int nfd = nfd_in;
1374 int i;
1375 struct pollfd *fds = fds_in;
1376
1377 for (i = 0; i < channel_count; ++i)
1378 if (channels[i].ch_fd >= 0)
1379 {
1380 channels[i].ch_idx = nfd;
1381 fds[nfd].fd = channels[i].ch_fd;
1382 fds[nfd].events = POLLIN;
1383 nfd++;
1384 }
1385 else
1386 channels[i].ch_idx = -1;
1387
1388 return nfd;
1389}
1390
1391/*
1392 * The type of "fds" is hidden to avoid problems with the function proto.
1393 */
1394 int
1395channel_poll_check(int ret_in, void *fds_in)
1396{
1397 int ret = ret_in;
1398 int i;
1399 struct pollfd *fds = fds_in;
1400
1401 for (i = 0; i < channel_count; ++i)
1402 if (ret > 0 && channels[i].ch_idx != -1
1403 && fds[channels[i].ch_idx].revents & POLLIN)
1404 {
1405 channel_read(i);
1406 --ret;
1407 }
1408
1409 return ret;
1410}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001411# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001412
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001413# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001414/*
1415 * The type of "rfds" is hidden to avoid problems with the function proto.
1416 */
1417 int
1418channel_select_setup(int maxfd_in, void *rfds_in)
1419{
1420 int maxfd = maxfd_in;
1421 int i;
1422 fd_set *rfds = rfds_in;
1423
1424 for (i = 0; i < channel_count; ++i)
1425 if (channels[i].ch_fd >= 0)
1426 {
1427 FD_SET(channels[i].ch_fd, rfds);
1428 if (maxfd < channels[i].ch_fd)
1429 maxfd = channels[i].ch_fd;
1430 }
1431
1432 return maxfd;
1433}
1434
1435/*
1436 * The type of "rfds" is hidden to avoid problems with the function proto.
1437 */
1438 int
1439channel_select_check(int ret_in, void *rfds_in)
1440{
1441 int ret = ret_in;
1442 int i;
1443 fd_set *rfds = rfds_in;
1444
1445 for (i = 0; i < channel_count; ++i)
1446 if (ret > 0 && channels[i].ch_fd >= 0
1447 && FD_ISSET(channels[i].ch_fd, rfds))
1448 {
1449 channel_read(i);
1450 --ret;
1451 }
1452
1453 return ret;
1454}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001455# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001456
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001457/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01001458 * Execute queued up commands.
1459 * Invoked from the main loop when it's safe to execute received commands.
1460 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001461 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001462 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001463channel_parse_messages(void)
1464{
1465 int i;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001466 int ret = FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001467
1468 for (i = 0; i < channel_count; ++i)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001469 while (may_invoke_callback(i) == OK)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001470 {
1471 i = 0; /* start over */
1472 ret = TRUE;
1473 }
1474 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001475}
1476
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01001477/*
1478 * Mark references to lists used in channels.
1479 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01001480 int
1481set_ref_in_channel(int copyID)
1482{
1483 int i;
1484 int abort = FALSE;
1485
1486 for (i = 0; i < channel_count; ++i)
1487 {
1488 jsonq_T *head = &channels[i].ch_json_head;
1489 jsonq_T *item = head->next;
1490
1491 while (item != head)
1492 {
1493 list_T *l = item->value->vval.v_list;
1494
1495 if (l->lv_copyID != copyID)
1496 {
1497 l->lv_copyID = copyID;
1498 abort = abort || set_ref_in_list(l, copyID, NULL);
1499 }
1500 item = item->next;
1501 }
1502 }
1503 return abort;
1504}
Bram Moolenaare0874f82016-01-24 20:36:41 +01001505#endif /* FEAT_CHANNEL */