blob: 0ae90a963897b85e175df91390a3dddd06c3ad93 [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
45# ifdef EINTR
46# undef EINTR
47# endif
48# define EINTR WSAEINTR
49# define sock_write(sd, buf, len) send(sd, buf, len, 0)
50# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
51# define sock_close(sd) closesocket(sd)
52# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
53#else
54# include <netdb.h>
55# include <netinet/in.h>
56
57# include <sys/socket.h>
58# ifdef HAVE_LIBGEN_H
59# include <libgen.h>
60# endif
61# define SOCK_ERRNO
62# define sock_write(sd, buf, len) write(sd, buf, len)
63# define sock_read(sd, buf, len) read(sd, buf, len)
64# define sock_close(sd) close(sd)
65#endif
66
67#ifdef FEAT_GUI_W32
68extern HWND s_hwnd; /* Gvim's Window handle */
69#endif
70
71struct readqueue
72{
73 char_u *buffer;
74 struct readqueue *next;
75 struct readqueue *prev;
76};
Bram Moolenaar19d2f152016-02-01 21:38:19 +010077typedef struct readqueue readq_T;
78
79struct jsonqueue
80{
81 typval_T *value;
82 struct jsonqueue *next;
83 struct jsonqueue *prev;
84};
85typedef struct jsonqueue jsonq_T;
Bram Moolenaard04a0202016-01-26 23:30:18 +010086
Bram Moolenaare0874f82016-01-24 20:36:41 +010087typedef struct {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010088 sock_T ch_fd; /* the socket, -1 for a closed channel */
89 int ch_idx; /* used by channel_poll_setup() */
Bram Moolenaar19d2f152016-02-01 21:38:19 +010090 readq_T ch_head; /* dummy node, header for circular queue */
Bram Moolenaard04a0202016-01-26 23:30:18 +010091
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010092 int ch_error; /* When TRUE an error was reported. Avoids giving
Bram Moolenaard04a0202016-01-26 23:30:18 +010093 * pages full of error messages when the other side
94 * has exited, only mention the first error until the
95 * connection works again. */
96#ifdef FEAT_GUI_X11
97 XtInputId ch_inputHandler; /* Cookie for input */
98#endif
99#ifdef FEAT_GUI_GTK
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100100 gint ch_inputHandler; /* Cookie for input */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100101#endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100102#ifdef WIN32
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100103 int ch_inputHandler; /* simply ret.value of WSAAsyncSelect() */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100104#endif
105
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100106 void (*ch_close_cb)(void); /* callback for when channel is closed */
107
108 char_u *ch_callback; /* function to call when a msg is not handled */
109 char_u *ch_req_callback; /* function to call for current request */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100110
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100111 int ch_json_mode; /* TRUE for a json channel */
112 jsonq_T ch_json_head; /* dummy node, header for circular queue */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100113} channel_T;
114
Bram Moolenaard04a0202016-01-26 23:30:18 +0100115/*
116 * Information about all channels.
117 * There can be gaps for closed channels, they will be reused later.
118 */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100119static channel_T *channels = NULL;
120static int channel_count = 0;
121
122/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100123 * TODO: open debug file when desired.
124 */
125FILE *debugfd = NULL;
126
127/*
Bram Moolenaare0874f82016-01-24 20:36:41 +0100128 * Add a new channel slot, return the index.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100129 * The channel isn't actually used into ch_fd is set >= 0;
130 * Returns -1 if all channels are in use.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100131 */
132 static int
133add_channel(void)
134{
135 int idx;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100136 channel_T *ch;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100137
138 if (channels != NULL)
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100139 {
Bram Moolenaare0874f82016-01-24 20:36:41 +0100140 for (idx = 0; idx < channel_count; ++idx)
141 if (channels[idx].ch_fd < 0)
142 /* re-use a closed channel slot */
143 return idx;
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100144 if (channel_count == MAX_OPEN_CHANNELS)
145 return -1;
146 }
147 else
148 {
149 channels = (channel_T *)alloc((int)sizeof(channel_T)
150 * MAX_OPEN_CHANNELS);
151 if (channels == NULL)
152 return -1;
153 }
154
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100155 ch = &channels[channel_count];
156 (void)vim_memset(ch, 0, sizeof(channel_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100157
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100158 ch->ch_fd = (sock_T)-1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100159#ifdef FEAT_GUI_X11
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100160 ch->ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100161#endif
162#ifdef FEAT_GUI_GTK
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100163 ch->ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100164#endif
165#ifdef FEAT_GUI_W32
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100166 ch->ch_inputHandler = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100167#endif
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100168 /* initialize circular queues */
169 ch->ch_head.next = &ch->ch_head;
170 ch->ch_head.prev = &ch->ch_head;
171 ch->ch_json_head.next = &ch->ch_json_head;
172 ch->ch_json_head.prev = &ch->ch_json_head;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100173
174 return channel_count++;
175}
176
Bram Moolenaard04a0202016-01-26 23:30:18 +0100177#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100178/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100179 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100180 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100181#ifdef FEAT_GUI_X11
182 static void
183messageFromNetbeans(XtPointer clientData,
184 int *unused1 UNUSED,
185 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100186{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100187 channel_read((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100188}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100189#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100190
Bram Moolenaard04a0202016-01-26 23:30:18 +0100191#ifdef FEAT_GUI_GTK
192 static void
193messageFromNetbeans(gpointer clientData,
194 gint unused1 UNUSED,
195 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100196{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100197 channel_read((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100198}
199#endif
200
201 static void
Bram Moolenaard04a0202016-01-26 23:30:18 +0100202channel_gui_register(int idx)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100203{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100204 channel_T *channel = &channels[idx];
205
206 if (!CH_HAS_GUI)
207 return;
208
209# ifdef FEAT_GUI_X11
210 /* tell notifier we are interested in being called
211 * when there is input on the editor connection socket
212 */
213 if (channel->ch_inputHandler == (XtInputId)NULL)
214 channel->ch_inputHandler =
215 XtAppAddInput((XtAppContext)app_context, channel->ch_fd,
216 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100217 messageFromNetbeans, (XtPointer)(long)idx);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100218# else
219# ifdef FEAT_GUI_GTK
220 /*
221 * Tell gdk we are interested in being called when there
222 * is input on the editor connection socket
223 */
224 if (channel->ch_inputHandler == 0)
225 channel->ch_inputHandler =
226 gdk_input_add((gint)channel->ch_fd, (GdkInputCondition)
227 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
228 messageFromNetbeans, (gpointer)(long)idx);
229# else
230# ifdef FEAT_GUI_W32
231 /*
232 * Tell Windows we are interested in receiving message when there
233 * is input on the editor connection socket.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100234 */
235 if (channel->ch_inputHandler == -1)
236 channel->ch_inputHandler =
237 WSAAsyncSelect(channel->ch_fd, s_hwnd, WM_NETBEANS, FD_READ);
238# endif
239# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100240# endif
Bram Moolenaard04a0202016-01-26 23:30:18 +0100241}
242
243/*
244 * Register any of our file descriptors with the GUI event handling system.
245 * Called when the GUI has started.
246 */
247 void
248channel_gui_register_all(void)
249{
250 int i;
251
252 for (i = 0; i < channel_count; ++i)
253 if (channels[i].ch_fd >= 0)
254 channel_gui_register(i);
255}
256
257 static void
258channel_gui_unregister(int idx)
259{
260 channel_T *channel = &channels[idx];
261
262# ifdef FEAT_GUI_X11
263 if (channel->ch_inputHandler != (XtInputId)NULL)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100264 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100265 XtRemoveInput(channel->ch_inputHandler);
266 channel->ch_inputHandler = (XtInputId)NULL;
267 }
268# else
269# ifdef FEAT_GUI_GTK
270 if (channel->ch_inputHandler != 0)
271 {
272 gdk_input_remove(channel->ch_inputHandler);
273 channel->ch_inputHandler = 0;
274 }
275# else
276# ifdef FEAT_GUI_W32
277 if (channel->ch_inputHandler == 0)
278 {
Bram Moolenaar54e09e72016-01-26 23:49:31 +0100279 WSAAsyncSelect(channel->ch_fd, s_hwnd, 0, 0);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100280 channel->ch_inputHandler = -1;
281 }
282# endif
283# endif
284# endif
285}
286
287#endif
288
289/*
290 * Open a channel to "hostname":"port".
291 * Returns the channel number for success.
292 * Returns a negative number for failure.
293 */
294 int
295channel_open(char *hostname, int port_in, void (*close_cb)(void))
296{
297 int sd;
298 struct sockaddr_in server;
299 struct hostent * host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100300#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100301 u_short port = port_in;
302#else
303 int port = port_in;
304#endif
305 int idx;
306
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100307#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100308 channel_init_winsock();
309#endif
310
311 idx = add_channel();
312 if (idx < 0)
313 {
314 CHERROR("All channels are in use\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100315 EMSG(_("E897: All channels are in use"));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100316 return -1;
317 }
318
319 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
320 {
321 CHERROR("error in socket() in channel_open()\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100322 PERROR("E898: socket() in channel_open()");
Bram Moolenaard04a0202016-01-26 23:30:18 +0100323 return -1;
324 }
325
326 /* Get the server internet address and put into addr structure */
327 /* fill in the socket address structure and connect to server */
328 vim_memset((char *)&server, 0, sizeof(server));
329 server.sin_family = AF_INET;
330 server.sin_port = htons(port);
331 if ((host = gethostbyname(hostname)) == NULL)
332 {
333 CHERROR("error in gethostbyname() in channel_open()\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100334 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaard04a0202016-01-26 23:30:18 +0100335 sock_close(sd);
336 return -1;
337 }
338 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
339
340 /* Connect to server */
341 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
342 {
343 SOCK_ERRNO;
344 CHERROR("channel_open: Connect failed with errno %d\n", errno);
345 if (errno == ECONNREFUSED)
346 {
347 sock_close(sd);
348 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
349 {
350 SOCK_ERRNO;
351 CHERROR("socket() retry in channel_open()\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100352 PERROR("E900: socket() retry in channel_open()");
Bram Moolenaard04a0202016-01-26 23:30:18 +0100353 return -1;
354 }
355 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
356 {
357 int retries = 36;
358 int success = FALSE;
359
360 SOCK_ERRNO;
361 while (retries-- && ((errno == ECONNREFUSED)
362 || (errno == EINTR)))
363 {
364 CHERROR("retrying...\n", "");
365 mch_delay(3000L, TRUE);
366 ui_breakcheck();
367 if (got_int)
368 {
369 errno = EINTR;
370 break;
371 }
372 if (connect(sd, (struct sockaddr *)&server,
373 sizeof(server)) == 0)
374 {
375 success = TRUE;
376 break;
377 }
378 SOCK_ERRNO;
379 }
380 if (!success)
381 {
382 /* Get here when the server can't be found. */
383 CHERROR("Cannot connect to port after retry\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100384 PERROR(_("E899: Cannot connect to port after retry2"));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100385 sock_close(sd);
386 return -1;
387 }
388 }
389 }
390 else
391 {
392 CHERROR("Cannot connect to port\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100393 PERROR(_("E902: Cannot connect to port"));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100394 sock_close(sd);
395 return -1;
396 }
397 }
398
399 channels[idx].ch_fd = sd;
400 channels[idx].ch_close_cb = close_cb;
401
402#ifdef FEAT_GUI
403 channel_gui_register(idx);
404#endif
405
406 return idx;
407}
408
409/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100410 * Set the json mode of channel "idx" to TRUE or FALSE.
411 */
412 void
413channel_set_json_mode(int idx, int json_mode)
414{
415 channels[idx].ch_json_mode = json_mode;
416}
417
418/*
419 * Set the callback for channel "idx".
420 */
421 void
422channel_set_callback(int idx, char_u *callback)
423{
424 vim_free(channels[idx].ch_callback);
425 channels[idx].ch_callback = vim_strsave(callback);
426}
427
428/*
429 * Set the callback for channel "idx" for the next response.
430 */
431 void
432channel_set_req_callback(int idx, char_u *callback)
433{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100434 /* TODO: make a list of callbacks */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100435 vim_free(channels[idx].ch_req_callback);
436 channels[idx].ch_req_callback = callback == NULL
437 ? NULL : vim_strsave(callback);
438}
439
440/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100441 * Invoke the "callback" on channel "idx".
442 */
443 static void
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100444invoke_callback(int idx, char_u *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100445{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100446 typval_T rettv;
447 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100448
449 argv[0].v_type = VAR_NUMBER;
450 argv[0].vval.v_number = idx;
451
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100452 call_func(callback, (int)STRLEN(callback),
453 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
454 /* If an echo command was used the cursor needs to be put back where
455 * it belongs. */
456 setcursor();
457 cursor_on();
458 out_flush();
459}
460
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100461/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100462 * Return the first buffer from the channel and remove it.
463 * The caller must free it.
464 * Returns NULL if there is nothing.
465 */
466 char_u *
467channel_get(int idx)
468{
469 readq_T *head = &channels[idx].ch_head;
470 readq_T *node;
471 char_u *p;
472
473 if (head->next == head || head->next == NULL)
474 return NULL;
475 node = head->next;
476 /* dispose of the node but keep the buffer */
477 p = node->buffer;
478 head->next = node->next;
479 node->next->prev = node->prev;
480 vim_free(node);
481 return p;
482}
483
484/*
485 * Returns the whole buffer contents concatenated.
486 */
487 static char_u *
488channel_get_all(int idx)
489{
490 /* Concatenate everything into one buffer.
491 * TODO: avoid multiple allocations. */
492 while (channel_collapse(idx) == OK)
493 ;
494 return channel_get(idx);
495}
496
497/*
498 * Collapses the first and second buffer in the channel "idx".
499 * Returns FAIL if that is not possible.
500 */
501 int
502channel_collapse(int idx)
503{
504 readq_T *head = &channels[idx].ch_head;
505 readq_T *node = head->next;
506 char_u *p;
507
508 if (node == head || node == NULL || node->next == head)
509 return FAIL;
510
511 p = alloc((unsigned)(STRLEN(node->buffer)
512 + STRLEN(node->next->buffer) + 1));
513 if (p == NULL)
514 return FAIL; /* out of memory */
515 STRCPY(p, node->buffer);
516 STRCAT(p, node->next->buffer);
517 vim_free(node->next->buffer);
518 node->next->buffer = p;
519
520 /* dispose of the node and buffer */
521 head->next = node->next;
522 node->next->prev = node->prev;
523 vim_free(node->buffer);
524 vim_free(node);
525 return OK;
526}
527
528/*
Bram Moolenaard7ece102016-02-02 23:23:02 +0100529 * Use the read buffer of channel "ch_idx" and parse a JSON messages that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100530 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +0100531 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100532 */
Bram Moolenaard7ece102016-02-02 23:23:02 +0100533 static int
534channel_parse_json(int ch_idx)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100535{
536 js_read_T reader;
537 typval_T listtv;
538 jsonq_T *item;
539 jsonq_T *head = &channels[ch_idx].ch_json_head;
Bram Moolenaard7ece102016-02-02 23:23:02 +0100540 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100541
542 if (channel_peek(ch_idx) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100543 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100544
545 /* TODO: make reader work properly */
546 /* reader.js_buf = channel_peek(ch_idx); */
547 reader.js_buf = channel_get_all(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100548 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100549 reader.js_fill = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100550 /* reader.js_fill = channel_fill; */
551 reader.js_cookie = &ch_idx;
Bram Moolenaard7ece102016-02-02 23:23:02 +0100552 ret = json_decode(&reader, &listtv);
553 if (ret == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100554 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100555 if (listtv.v_type != VAR_LIST)
556 {
557 /* TODO: give error */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100558 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100559 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100560 else
561 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100562 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
563 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100564 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100565 else
566 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100567 item->value = alloc_tv();
568 if (item->value == NULL)
569 {
570 vim_free(item);
571 clear_tv(&listtv);
572 }
573 else
574 {
575 *item->value = listtv;
576 item->prev = head->prev;
577 head->prev = item;
578 item->next = head;
579 item->prev->next = item;
580 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100581 }
582 }
583 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100584
585 /* Put the unread part back into the channel.
586 * TODO: insert in front */
587 if (reader.js_buf[reader.js_used] != NUL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100588 {
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100589 channel_save(ch_idx, reader.js_buf + reader.js_used,
590 (int)(reader.js_end - reader.js_buf) - reader.js_used);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100591 ret = TRUE;
592 }
593 else
594 ret = FALSE;
595
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100596 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100597 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100598}
599
600/*
601 * Remove "node" from the queue that it is in and free it.
602 * Caller should have freed or used node->value.
603 */
604 static void
605remove_json_node(jsonq_T *node)
606{
607 node->prev->next = node->next;
608 node->next->prev = node->prev;
609 vim_free(node);
610}
611
612/*
613 * Get a message from the JSON queue for channel "ch_idx".
614 * When "id" is positive it must match the first number in the list.
615 * When "id" is zero or negative jut get the first message.
616 * Return OK when found and return the value in "rettv".
617 * Return FAIL otherwise.
618 */
619 static int
620channel_get_json(int ch_idx, int id, typval_T **rettv)
621{
622 jsonq_T *head = &channels[ch_idx].ch_json_head;
623 jsonq_T *item = head->next;
624
625 while (item != head)
626 {
627 list_T *l = item->value->vval.v_list;
628 typval_T *tv = &l->lv_first->li_tv;
629
630 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100631 || (id <= 0
632 && (tv->v_type != VAR_NUMBER || tv->vval.v_number < 0)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100633 {
634 *rettv = item->value;
635 remove_json_node(item);
636 return OK;
637 }
638 item = item->next;
639 }
640 return FAIL;
641}
642
643/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100644 * Execute a command received over channel "idx".
645 * "cmd" is the command string, "arg2" the second argument.
646 * "arg3" is the third argument, NULL if missing.
647 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100648 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100649channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100650{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100651 char_u *arg;
652
653 if (arg2->v_type != VAR_STRING)
654 {
655 if (p_verbose > 2)
656 EMSG("E903: received ex command with non-string argument");
657 return;
658 }
659 arg = arg2->vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100660 if (arg == NULL)
661 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100662
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100663 if (STRCMP(cmd, "ex") == 0)
664 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100665 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100666 }
667 else if (STRCMP(cmd, "normal") == 0)
668 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100669 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100670
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100671 ea.arg = arg;
672 ea.addr_count = 0;
673 ea.forceit = TRUE; /* no mapping */
674 ex_normal(&ea);
675 }
676 else if (STRCMP(cmd, "redraw") == 0)
677 {
678 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100679
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100680 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100681 ex_redraw(&ea);
682 showruler(FALSE);
683 setcursor();
684 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100685#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100686 if (gui.in_use)
687 {
688 gui_update_cursor(FALSE, FALSE);
689 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100690 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100691#endif
692 }
693 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
694 {
695 int is_eval = cmd[1] == 'v';
696
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100697 if (is_eval && (arg3 == NULL || arg3->v_type != VAR_NUMBER))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100698 {
699 if (p_verbose > 2)
700 EMSG("E904: third argument for eval must be a number");
701 }
702 else
703 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100704 typval_T *tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100705 typval_T err_tv;
706 char_u *json;
707
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100708 /* Don't pollute the display with errors. */
709 ++emsg_skip;
710 tv = eval_expr(arg, NULL);
711 --emsg_skip;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100712 if (is_eval)
713 {
714 if (tv == NULL)
715 {
716 err_tv.v_type = VAR_STRING;
717 err_tv.vval.v_string = (char_u *)"ERROR";
718 tv = &err_tv;
719 }
720 json = json_encode_nr_expr(arg3->vval.v_number, tv);
721 channel_send(idx, json, "eval");
722 vim_free(json);
723 }
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100724 if (tv != &err_tv)
725 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100726 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100727 }
728 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100729 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100730}
731
732/*
733 * Invoke a callback for channel "idx" if needed.
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100734 * Return OK when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100735 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100736 static int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100737may_invoke_callback(int idx)
738{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100739 char_u *msg = NULL;
740 typval_T *listtv = NULL;
741 list_T *list;
742 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100743 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100744 int seq_nr = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100745 int json_mode = channels[idx].ch_json_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100746
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100747 if (channels[idx].ch_close_cb != NULL)
748 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100749 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100750
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100751 if (json_mode)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100752 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100753 /* Get any json message in the queue. */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100754 if (channel_get_json(idx, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100755 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100756 /* Parse readahead, return when there is still no message. */
757 channel_parse_json(idx);
758 if (channel_get_json(idx, -1, &listtv) == FAIL)
759 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100760 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100761
762 list = listtv->vval.v_list;
763 if (list->lv_len < 2)
764 {
765 /* TODO: give error */
766 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100767 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100768 }
769
770 argv[1] = list->lv_first->li_next->li_tv;
771 typetv = &list->lv_first->li_tv;
772 if (typetv->v_type == VAR_STRING)
773 {
774 typval_T *arg3 = NULL;
775 char_u *cmd = typetv->vval.v_string;
776
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100777 /* ["cmd", arg] or ["cmd", arg, arg] */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100778 if (list->lv_len == 3)
779 arg3 = &list->lv_last->li_tv;
780 channel_exe_cmd(idx, cmd, &argv[1], arg3);
781 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100782 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100783 }
784
785 if (typetv->v_type != VAR_NUMBER)
786 {
787 /* TODO: give error */
788 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100789 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100790 }
791 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100792 }
Bram Moolenaard7ece102016-02-02 23:23:02 +0100793 else if (channel_peek(idx) == NULL)
794 {
795 /* nothing to read on raw channel */
796 return FALSE;
797 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100798 else
799 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100800 /* For a raw channel we don't know where the message ends, just get
801 * everything. */
802 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100803 argv[1].v_type = VAR_STRING;
804 argv[1].vval.v_string = msg;
805 }
806
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100807 if (channels[idx].ch_req_callback != NULL && seq_nr != 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100808 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100809 /* TODO: check the sequence number */
810 /* invoke the one-time callback */
811 invoke_callback(idx, channels[idx].ch_req_callback, argv);
812 channels[idx].ch_req_callback = NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100813 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100814 else if (channels[idx].ch_callback != NULL)
815 {
816 /* invoke the channel callback */
817 invoke_callback(idx, channels[idx].ch_callback, argv);
818 }
819 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100820
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100821 if (listtv != NULL)
822 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100823 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100824
825 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100826}
827
828/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100829 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100830 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +0100831 */
832 int
833channel_is_open(int idx)
834{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100835 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100836}
837
838/*
839 * Close channel "idx".
840 * This does not trigger the close callback.
841 */
842 void
843channel_close(int idx)
844{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100845 channel_T *channel = &channels[idx];
846 jsonq_T *jhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100847
848 if (channel->ch_fd >= 0)
849 {
850 sock_close(channel->ch_fd);
851 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100852 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100853#ifdef FEAT_GUI
854 channel_gui_unregister(idx);
855#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100856 vim_free(channel->ch_callback);
857 channel->ch_callback = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100858
859 while (channel_peek(idx) != NULL)
860 vim_free(channel_get(idx));
861
862 jhead = &channel->ch_json_head;
863 while (jhead->next != jhead)
864 {
865 clear_tv(jhead->next->value);
866 remove_json_node(jhead->next);
867 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100868 }
869}
870
Bram Moolenaard04a0202016-01-26 23:30:18 +0100871/*
872 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +0100873 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100874 */
Bram Moolenaar83162462016-01-28 23:10:07 +0100875 int
Bram Moolenaard04a0202016-01-26 23:30:18 +0100876channel_save(int idx, char_u *buf, int len)
877{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100878 readq_T *node;
879 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100880
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100881 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100882 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +0100883 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100884 node->buffer = alloc(len + 1);
885 if (node->buffer == NULL)
886 {
887 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +0100888 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100889 }
890 mch_memmove(node->buffer, buf, (size_t)len);
891 node->buffer[len] = NUL;
892
Bram Moolenaard04a0202016-01-26 23:30:18 +0100893 /* insert node at tail of queue */
894 node->next = head;
895 node->prev = head->prev;
896 head->prev->next = node;
897 head->prev = node;
898
899 if (debugfd != NULL)
900 {
901 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +0100902 if (fwrite(buf, len, 1, debugfd) != 1)
903 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100904 fprintf(debugfd, "\n");
905 }
Bram Moolenaar83162462016-01-28 23:10:07 +0100906 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100907}
908
909/*
910 * Return the first buffer from the channel without removing it.
911 * Returns NULL if there is nothing.
912 */
913 char_u *
914channel_peek(int idx)
915{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100916 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100917
918 if (head->next == head || head->next == NULL)
919 return NULL;
920 return head->next->buffer;
921}
922
923/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100924 * Clear the read buffer on channel "idx".
925 */
926 void
927channel_clear(int idx)
928{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100929 readq_T *head = &channels[idx].ch_head;
930 readq_T *node = head->next;
931 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100932
933 while (node != NULL && node != head)
934 {
935 next = node->next;
936 vim_free(node->buffer);
937 vim_free(node);
938 if (next == head)
939 {
940 head->next = head;
941 head->prev = head;
942 break;
943 }
944 node = next;
945 }
946}
947
948/* Sent when the channel is found closed when reading. */
949#define DETACH_MSG "\"DETACH\"\n"
950
951/* Buffer size for reading incoming messages. */
952#define MAXMSGSIZE 4096
953
954/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100955 * Check for reading from "fd" with "timeout" msec.
956 * Return FAIL when there is nothing to read.
Bram Moolenaara8343c12016-02-04 22:09:48 +0100957 * Always returns OK for FEAT_GUI_W32.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100958 */
959 static int
960channel_wait(int fd, int timeout)
961{
Bram Moolenaara8343c12016-02-04 22:09:48 +0100962#if defined(HAVE_SELECT) && !defined(FEAT_GUI_W32)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100963 struct timeval tval;
964 fd_set rfds;
965 int ret;
966
967 FD_ZERO(&rfds);
968 FD_SET(fd, &rfds);
969 tval.tv_sec = timeout / 1000;
970 tval.tv_usec = (timeout % 1000) * 1000;
971 for (;;)
972 {
973 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
974# ifdef EINTR
975 if (ret == -1 && errno == EINTR)
976 continue;
977# endif
978 if (ret <= 0)
979 return FAIL;
980 break;
981 }
982#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +0100983# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100984 struct pollfd fds;
985
986 fds.fd = fd;
987 fds.events = POLLIN;
988 if (poll(&fds, 1, timeout) <= 0)
989 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +0100990# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100991#endif
992 return OK;
993}
994
995/*
996 * Return a unique ID to be used in a message.
997 */
998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100999channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001000{
1001 static int next_id = 1;
1002
1003 return next_id++;
1004}
1005
1006/*
1007 * Read from channel "idx" for as long as there is something to read.
1008 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001009 */
1010 void
1011channel_read(int idx)
1012{
1013 static char_u *buf = NULL;
1014 int len = 0;
1015 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001016 channel_T *channel = &channels[idx];
1017
1018 if (channel->ch_fd < 0)
1019 {
1020 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
1021 return;
1022 }
1023
1024 /* Allocate a buffer to read into. */
1025 if (buf == NULL)
1026 {
1027 buf = alloc(MAXMSGSIZE);
1028 if (buf == NULL)
1029 return; /* out of memory! */
1030 }
1031
1032 /* Keep on reading for as long as there is something to read.
1033 * Use select() or poll() to avoid blocking on a message that is exactly
1034 * MAXMSGSIZE long. */
1035 for (;;)
1036 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001037 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001038 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001039 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1040 if (len <= 0)
1041 break; /* error or nothing more to read */
1042
1043 /* Store the read message in the queue. */
1044 channel_save(idx, buf, len);
1045 readlen += len;
1046 if (len < MAXMSGSIZE)
1047 break; /* did read everything that's available */
1048 }
Bram Moolenaara8343c12016-02-04 22:09:48 +01001049#ifdef FEAT_GUI_W32
1050 if (len == SOCKET_ERROR)
1051 {
1052 /* For Win32 GUI channel_wait() always returns OK and we handle the
1053 * situation that there is nothing to read here.
1054 * TODO: how about a timeout? */
1055 if (WSAGetLastError() == WSAEWOULDBLOCK)
1056 return;
1057 }
1058#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01001059
1060 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1061 if (readlen <= 0)
1062 {
1063 /* Queue a "DETACH" netbeans message in the command queue in order to
1064 * terminate the netbeans session later. Do not end the session here
1065 * directly as we may be running in the context of a call to
1066 * netbeans_parse_messages():
1067 * netbeans_parse_messages
1068 * -> autocmd triggered while processing the netbeans cmd
1069 * -> ui_breakcheck
1070 * -> gui event loop or select loop
1071 * -> channel_read()
1072 */
1073 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1074
1075 channel_close(idx);
1076 if (channel->ch_close_cb != NULL)
1077 (*channel->ch_close_cb)();
1078
1079 if (len < 0)
1080 {
1081 /* Todo: which channel? */
1082 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001083 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001084 }
1085 }
1086
1087#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1088 if (CH_HAS_GUI && gtk_main_level() > 0)
1089 gtk_main_quit();
1090#endif
1091}
1092
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001093/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001094 * Read from raw channel "idx". Blocks until there is something to read or
1095 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001096 * Returns what was read in allocated memory.
1097 * Returns NULL in case of error or timeout.
1098 */
1099 char_u *
1100channel_read_block(int idx)
1101{
1102 if (channel_peek(idx) == NULL)
1103 {
1104 /* Wait for up to 2 seconds.
1105 * TODO: use timeout set on the channel. */
1106 if (channel_wait(channels[idx].ch_fd, 2000) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001107 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001108 channel_read(idx);
1109 }
1110
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001111 return channel_get_all(idx);
1112}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001113
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001114/*
1115 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1116 * result in "rettv".
1117 * Blocks until the message is received.
1118 */
1119 int
1120channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1121{
Bram Moolenaard7ece102016-02-02 23:23:02 +01001122 int more;
1123
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001124 for (;;)
1125 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001126 more = channel_parse_json(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001127
1128 /* search for messsage "id" */
1129 if (channel_get_json(ch_idx, id, rettv) == OK)
1130 return OK;
1131
Bram Moolenaard7ece102016-02-02 23:23:02 +01001132 if (!more)
1133 {
1134 /* Handle any other messages in the queue. If done some more
1135 * messages may have arrived. */
1136 if (channel_parse_messages())
1137 continue;
1138
1139 /* Wait for up to 2 seconds.
1140 * TODO: use timeout set on the channel. */
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001141 if (channels[ch_idx].ch_fd < 0
1142 || channel_wait(channels[ch_idx].ch_fd, 2000) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001143 break;
1144 channel_read(ch_idx);
1145 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001146 }
1147 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001148}
1149
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001150# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001151/*
1152 * Lookup the channel index from the socket.
1153 * Returns -1 when the socket isn't found.
1154 */
1155 int
1156channel_socket2idx(sock_T fd)
1157{
1158 int i;
1159
1160 if (fd >= 0)
1161 for (i = 0; i < channel_count; ++i)
1162 if (channels[i].ch_fd == fd)
1163 return i;
1164 return -1;
1165}
1166# endif
1167
Bram Moolenaard04a0202016-01-26 23:30:18 +01001168/*
1169 * Write "buf" (NUL terminated string) to channel "idx".
1170 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001171 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001172 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001173 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001174channel_send(int idx, char_u *buf, char *fun)
1175{
1176 channel_T *channel = &channels[idx];
1177 int len = (int)STRLEN(buf);
1178
1179 if (channel->ch_fd < 0)
1180 {
1181 if (!channel->ch_error && fun != NULL)
1182 {
1183 CHERROR(" %s(): write while not connected\n", fun);
1184 EMSG2("E630: %s(): write while not connected", fun);
1185 }
1186 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001187 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001188 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001189
1190 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001191 {
1192 if (!channel->ch_error && fun != NULL)
1193 {
1194 CHERROR(" %s(): write failed\n", fun);
1195 EMSG2("E631: %s(): write failed", fun);
1196 }
1197 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001198 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001199 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001200
1201 channel->ch_error = FALSE;
1202 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001203}
1204
1205# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001206/*
1207 * Add open channels to the poll struct.
1208 * Return the adjusted struct index.
1209 * The type of "fds" is hidden to avoid problems with the function proto.
1210 */
1211 int
1212channel_poll_setup(int nfd_in, void *fds_in)
1213{
1214 int nfd = nfd_in;
1215 int i;
1216 struct pollfd *fds = fds_in;
1217
1218 for (i = 0; i < channel_count; ++i)
1219 if (channels[i].ch_fd >= 0)
1220 {
1221 channels[i].ch_idx = nfd;
1222 fds[nfd].fd = channels[i].ch_fd;
1223 fds[nfd].events = POLLIN;
1224 nfd++;
1225 }
1226 else
1227 channels[i].ch_idx = -1;
1228
1229 return nfd;
1230}
1231
1232/*
1233 * The type of "fds" is hidden to avoid problems with the function proto.
1234 */
1235 int
1236channel_poll_check(int ret_in, void *fds_in)
1237{
1238 int ret = ret_in;
1239 int i;
1240 struct pollfd *fds = fds_in;
1241
1242 for (i = 0; i < channel_count; ++i)
1243 if (ret > 0 && channels[i].ch_idx != -1
1244 && fds[channels[i].ch_idx].revents & POLLIN)
1245 {
1246 channel_read(i);
1247 --ret;
1248 }
1249
1250 return ret;
1251}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001252# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001253
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001254# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001255/*
1256 * The type of "rfds" is hidden to avoid problems with the function proto.
1257 */
1258 int
1259channel_select_setup(int maxfd_in, void *rfds_in)
1260{
1261 int maxfd = maxfd_in;
1262 int i;
1263 fd_set *rfds = rfds_in;
1264
1265 for (i = 0; i < channel_count; ++i)
1266 if (channels[i].ch_fd >= 0)
1267 {
1268 FD_SET(channels[i].ch_fd, rfds);
1269 if (maxfd < channels[i].ch_fd)
1270 maxfd = channels[i].ch_fd;
1271 }
1272
1273 return maxfd;
1274}
1275
1276/*
1277 * The type of "rfds" is hidden to avoid problems with the function proto.
1278 */
1279 int
1280channel_select_check(int ret_in, void *rfds_in)
1281{
1282 int ret = ret_in;
1283 int i;
1284 fd_set *rfds = rfds_in;
1285
1286 for (i = 0; i < channel_count; ++i)
1287 if (ret > 0 && channels[i].ch_fd >= 0
1288 && FD_ISSET(channels[i].ch_fd, rfds))
1289 {
1290 channel_read(i);
1291 --ret;
1292 }
1293
1294 return ret;
1295}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001296# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001297
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001298/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01001299 * Execute queued up commands.
1300 * Invoked from the main loop when it's safe to execute received commands.
1301 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001302 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001303 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001304channel_parse_messages(void)
1305{
1306 int i;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001307 int ret = FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001308
1309 for (i = 0; i < channel_count; ++i)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001310 while (may_invoke_callback(i) == OK)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001311 {
1312 i = 0; /* start over */
1313 ret = TRUE;
1314 }
1315 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001316}
1317
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01001318 int
1319set_ref_in_channel(int copyID)
1320{
1321 int i;
1322 int abort = FALSE;
1323
1324 for (i = 0; i < channel_count; ++i)
1325 {
1326 jsonq_T *head = &channels[i].ch_json_head;
1327 jsonq_T *item = head->next;
1328
1329 while (item != head)
1330 {
1331 list_T *l = item->value->vval.v_list;
1332
1333 if (l->lv_copyID != copyID)
1334 {
1335 l->lv_copyID = copyID;
1336 abort = abort || set_ref_in_list(l, copyID, NULL);
1337 }
1338 item = item->next;
1339 }
1340 }
1341 return abort;
1342}
Bram Moolenaare0874f82016-01-24 20:36:41 +01001343#endif /* FEAT_CHANNEL */