blob: 2e5965bc7bb6e33f95f83d833f3cbb74a06d563a [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;
136 channel_T *new_channels;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100137 channel_T *ch;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100138
139 if (channels != NULL)
140 for (idx = 0; idx < channel_count; ++idx)
141 if (channels[idx].ch_fd < 0)
142 /* re-use a closed channel slot */
143 return idx;
144 if (channel_count == MAX_OPEN_CHANNELS)
145 return -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100146 new_channels = (channel_T *)alloc(sizeof(channel_T) * (channel_count + 1));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100147 if (new_channels == NULL)
148 return -1;
149 if (channels != NULL)
150 mch_memmove(new_channels, channels, sizeof(channel_T) * channel_count);
151 channels = new_channels;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100152 ch = &channels[channel_count];
153 (void)vim_memset(ch, 0, sizeof(channel_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100154
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100155 ch->ch_fd = (sock_T)-1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100156#ifdef FEAT_GUI_X11
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100157 ch->ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100158#endif
159#ifdef FEAT_GUI_GTK
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100160 ch->ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100161#endif
162#ifdef FEAT_GUI_W32
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100163 ch->ch_inputHandler = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100164#endif
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100165 /* initialize circular queues */
166 ch->ch_head.next = &ch->ch_head;
167 ch->ch_head.prev = &ch->ch_head;
168 ch->ch_json_head.next = &ch->ch_json_head;
169 ch->ch_json_head.prev = &ch->ch_json_head;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100170
171 return channel_count++;
172}
173
Bram Moolenaard04a0202016-01-26 23:30:18 +0100174#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100175/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100176 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100177 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100178#ifdef FEAT_GUI_X11
179 static void
180messageFromNetbeans(XtPointer clientData,
181 int *unused1 UNUSED,
182 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100183{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100184 channel_read((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100185}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100186#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100187
Bram Moolenaard04a0202016-01-26 23:30:18 +0100188#ifdef FEAT_GUI_GTK
189 static void
190messageFromNetbeans(gpointer clientData,
191 gint unused1 UNUSED,
192 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100193{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100194 channel_read((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100195}
196#endif
197
198 static void
Bram Moolenaard04a0202016-01-26 23:30:18 +0100199channel_gui_register(int idx)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100200{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100201 channel_T *channel = &channels[idx];
202
203 if (!CH_HAS_GUI)
204 return;
205
206# ifdef FEAT_GUI_X11
207 /* tell notifier we are interested in being called
208 * when there is input on the editor connection socket
209 */
210 if (channel->ch_inputHandler == (XtInputId)NULL)
211 channel->ch_inputHandler =
212 XtAppAddInput((XtAppContext)app_context, channel->ch_fd,
213 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100214 messageFromNetbeans, (XtPointer)(long)idx);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100215# else
216# ifdef FEAT_GUI_GTK
217 /*
218 * Tell gdk we are interested in being called when there
219 * is input on the editor connection socket
220 */
221 if (channel->ch_inputHandler == 0)
222 channel->ch_inputHandler =
223 gdk_input_add((gint)channel->ch_fd, (GdkInputCondition)
224 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
225 messageFromNetbeans, (gpointer)(long)idx);
226# else
227# ifdef FEAT_GUI_W32
228 /*
229 * Tell Windows we are interested in receiving message when there
230 * is input on the editor connection socket.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100231 */
232 if (channel->ch_inputHandler == -1)
233 channel->ch_inputHandler =
234 WSAAsyncSelect(channel->ch_fd, s_hwnd, WM_NETBEANS, FD_READ);
235# endif
236# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100237# endif
Bram Moolenaard04a0202016-01-26 23:30:18 +0100238}
239
240/*
241 * Register any of our file descriptors with the GUI event handling system.
242 * Called when the GUI has started.
243 */
244 void
245channel_gui_register_all(void)
246{
247 int i;
248
249 for (i = 0; i < channel_count; ++i)
250 if (channels[i].ch_fd >= 0)
251 channel_gui_register(i);
252}
253
254 static void
255channel_gui_unregister(int idx)
256{
257 channel_T *channel = &channels[idx];
258
259# ifdef FEAT_GUI_X11
260 if (channel->ch_inputHandler != (XtInputId)NULL)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100261 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100262 XtRemoveInput(channel->ch_inputHandler);
263 channel->ch_inputHandler = (XtInputId)NULL;
264 }
265# else
266# ifdef FEAT_GUI_GTK
267 if (channel->ch_inputHandler != 0)
268 {
269 gdk_input_remove(channel->ch_inputHandler);
270 channel->ch_inputHandler = 0;
271 }
272# else
273# ifdef FEAT_GUI_W32
274 if (channel->ch_inputHandler == 0)
275 {
Bram Moolenaar54e09e72016-01-26 23:49:31 +0100276 WSAAsyncSelect(channel->ch_fd, s_hwnd, 0, 0);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100277 channel->ch_inputHandler = -1;
278 }
279# endif
280# endif
281# endif
282}
283
284#endif
285
286/*
287 * Open a channel to "hostname":"port".
288 * Returns the channel number for success.
289 * Returns a negative number for failure.
290 */
291 int
292channel_open(char *hostname, int port_in, void (*close_cb)(void))
293{
294 int sd;
295 struct sockaddr_in server;
296 struct hostent * host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100297#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100298 u_short port = port_in;
299#else
300 int port = port_in;
301#endif
302 int idx;
303
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100304#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100305 channel_init_winsock();
306#endif
307
308 idx = add_channel();
309 if (idx < 0)
310 {
311 CHERROR("All channels are in use\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100312 EMSG(_("E897: All channels are in use"));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313 return -1;
314 }
315
316 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
317 {
318 CHERROR("error in socket() in channel_open()\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100319 PERROR("E898: socket() in channel_open()");
Bram Moolenaard04a0202016-01-26 23:30:18 +0100320 return -1;
321 }
322
323 /* Get the server internet address and put into addr structure */
324 /* fill in the socket address structure and connect to server */
325 vim_memset((char *)&server, 0, sizeof(server));
326 server.sin_family = AF_INET;
327 server.sin_port = htons(port);
328 if ((host = gethostbyname(hostname)) == NULL)
329 {
330 CHERROR("error in gethostbyname() in channel_open()\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100331 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaard04a0202016-01-26 23:30:18 +0100332 sock_close(sd);
333 return -1;
334 }
335 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
336
337 /* Connect to server */
338 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
339 {
340 SOCK_ERRNO;
341 CHERROR("channel_open: Connect failed with errno %d\n", errno);
342 if (errno == ECONNREFUSED)
343 {
344 sock_close(sd);
345 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
346 {
347 SOCK_ERRNO;
348 CHERROR("socket() retry in channel_open()\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100349 PERROR("E900: socket() retry in channel_open()");
Bram Moolenaard04a0202016-01-26 23:30:18 +0100350 return -1;
351 }
352 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
353 {
354 int retries = 36;
355 int success = FALSE;
356
357 SOCK_ERRNO;
358 while (retries-- && ((errno == ECONNREFUSED)
359 || (errno == EINTR)))
360 {
361 CHERROR("retrying...\n", "");
362 mch_delay(3000L, TRUE);
363 ui_breakcheck();
364 if (got_int)
365 {
366 errno = EINTR;
367 break;
368 }
369 if (connect(sd, (struct sockaddr *)&server,
370 sizeof(server)) == 0)
371 {
372 success = TRUE;
373 break;
374 }
375 SOCK_ERRNO;
376 }
377 if (!success)
378 {
379 /* Get here when the server can't be found. */
380 CHERROR("Cannot connect to port after retry\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100381 PERROR(_("E899: Cannot connect to port after retry2"));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100382 sock_close(sd);
383 return -1;
384 }
385 }
386 }
387 else
388 {
389 CHERROR("Cannot connect to port\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100390 PERROR(_("E902: Cannot connect to port"));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100391 sock_close(sd);
392 return -1;
393 }
394 }
395
396 channels[idx].ch_fd = sd;
397 channels[idx].ch_close_cb = close_cb;
398
399#ifdef FEAT_GUI
400 channel_gui_register(idx);
401#endif
402
403 return idx;
404}
405
406/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100407 * Set the json mode of channel "idx" to TRUE or FALSE.
408 */
409 void
410channel_set_json_mode(int idx, int json_mode)
411{
412 channels[idx].ch_json_mode = json_mode;
413}
414
415/*
416 * Set the callback for channel "idx".
417 */
418 void
419channel_set_callback(int idx, char_u *callback)
420{
421 vim_free(channels[idx].ch_callback);
422 channels[idx].ch_callback = vim_strsave(callback);
423}
424
425/*
426 * Set the callback for channel "idx" for the next response.
427 */
428 void
429channel_set_req_callback(int idx, char_u *callback)
430{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100431 /* TODO: make a list of callbacks */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100432 vim_free(channels[idx].ch_req_callback);
433 channels[idx].ch_req_callback = callback == NULL
434 ? NULL : vim_strsave(callback);
435}
436
437/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100438 * Invoke the "callback" on channel "idx".
439 */
440 static void
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100441invoke_callback(int idx, char_u *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100442{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100443 typval_T rettv;
444 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100445
446 argv[0].v_type = VAR_NUMBER;
447 argv[0].vval.v_number = idx;
448
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100449 call_func(callback, (int)STRLEN(callback),
450 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
451 /* If an echo command was used the cursor needs to be put back where
452 * it belongs. */
453 setcursor();
454 cursor_on();
455 out_flush();
456}
457
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100458/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100459 * Return the first buffer from the channel and remove it.
460 * The caller must free it.
461 * Returns NULL if there is nothing.
462 */
463 char_u *
464channel_get(int idx)
465{
466 readq_T *head = &channels[idx].ch_head;
467 readq_T *node;
468 char_u *p;
469
470 if (head->next == head || head->next == NULL)
471 return NULL;
472 node = head->next;
473 /* dispose of the node but keep the buffer */
474 p = node->buffer;
475 head->next = node->next;
476 node->next->prev = node->prev;
477 vim_free(node);
478 return p;
479}
480
481/*
482 * Returns the whole buffer contents concatenated.
483 */
484 static char_u *
485channel_get_all(int idx)
486{
487 /* Concatenate everything into one buffer.
488 * TODO: avoid multiple allocations. */
489 while (channel_collapse(idx) == OK)
490 ;
491 return channel_get(idx);
492}
493
494/*
495 * Collapses the first and second buffer in the channel "idx".
496 * Returns FAIL if that is not possible.
497 */
498 int
499channel_collapse(int idx)
500{
501 readq_T *head = &channels[idx].ch_head;
502 readq_T *node = head->next;
503 char_u *p;
504
505 if (node == head || node == NULL || node->next == head)
506 return FAIL;
507
508 p = alloc((unsigned)(STRLEN(node->buffer)
509 + STRLEN(node->next->buffer) + 1));
510 if (p == NULL)
511 return FAIL; /* out of memory */
512 STRCPY(p, node->buffer);
513 STRCAT(p, node->next->buffer);
514 vim_free(node->next->buffer);
515 node->next->buffer = p;
516
517 /* dispose of the node and buffer */
518 head->next = node->next;
519 node->next->prev = node->prev;
520 vim_free(node->buffer);
521 vim_free(node);
522 return OK;
523}
524
525/*
Bram Moolenaard7ece102016-02-02 23:23:02 +0100526 * Use the read buffer of channel "ch_idx" and parse a JSON messages that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100527 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +0100528 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100529 */
Bram Moolenaard7ece102016-02-02 23:23:02 +0100530 static int
531channel_parse_json(int ch_idx)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100532{
533 js_read_T reader;
534 typval_T listtv;
535 jsonq_T *item;
536 jsonq_T *head = &channels[ch_idx].ch_json_head;
Bram Moolenaard7ece102016-02-02 23:23:02 +0100537 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100538
539 if (channel_peek(ch_idx) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100540 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100541
542 /* TODO: make reader work properly */
543 /* reader.js_buf = channel_peek(ch_idx); */
544 reader.js_buf = channel_get_all(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100545 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100546 reader.js_fill = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100547 /* reader.js_fill = channel_fill; */
548 reader.js_cookie = &ch_idx;
Bram Moolenaard7ece102016-02-02 23:23:02 +0100549 ret = json_decode(&reader, &listtv);
550 if (ret == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100551 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100552 if (listtv.v_type != VAR_LIST)
553 {
554 /* TODO: give error */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100555 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100556 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100557 else
558 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100559 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
560 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100561 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100562 else
563 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100564 item->value = alloc_tv();
565 if (item->value == NULL)
566 {
567 vim_free(item);
568 clear_tv(&listtv);
569 }
570 else
571 {
572 *item->value = listtv;
573 item->prev = head->prev;
574 head->prev = item;
575 item->next = head;
576 item->prev->next = item;
577 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100578 }
579 }
580 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100581
582 /* Put the unread part back into the channel.
583 * TODO: insert in front */
584 if (reader.js_buf[reader.js_used] != NUL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100585 {
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100586 channel_save(ch_idx, reader.js_buf + reader.js_used,
587 (int)(reader.js_end - reader.js_buf) - reader.js_used);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100588 ret = TRUE;
589 }
590 else
591 ret = FALSE;
592
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100593 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100594 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100595}
596
597/*
598 * Remove "node" from the queue that it is in and free it.
599 * Caller should have freed or used node->value.
600 */
601 static void
602remove_json_node(jsonq_T *node)
603{
604 node->prev->next = node->next;
605 node->next->prev = node->prev;
606 vim_free(node);
607}
608
609/*
610 * Get a message from the JSON queue for channel "ch_idx".
611 * When "id" is positive it must match the first number in the list.
612 * When "id" is zero or negative jut get the first message.
613 * Return OK when found and return the value in "rettv".
614 * Return FAIL otherwise.
615 */
616 static int
617channel_get_json(int ch_idx, int id, typval_T **rettv)
618{
619 jsonq_T *head = &channels[ch_idx].ch_json_head;
620 jsonq_T *item = head->next;
621
622 while (item != head)
623 {
624 list_T *l = item->value->vval.v_list;
625 typval_T *tv = &l->lv_first->li_tv;
626
627 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100628 || (id <= 0
629 && (tv->v_type != VAR_NUMBER || tv->vval.v_number < 0)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100630 {
631 *rettv = item->value;
632 remove_json_node(item);
633 return OK;
634 }
635 item = item->next;
636 }
637 return FAIL;
638}
639
640/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100641 * Execute a command received over channel "idx".
642 * "cmd" is the command string, "arg2" the second argument.
643 * "arg3" is the third argument, NULL if missing.
644 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100645 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100646channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100647{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100648 char_u *arg;
649
650 if (arg2->v_type != VAR_STRING)
651 {
652 if (p_verbose > 2)
653 EMSG("E903: received ex command with non-string argument");
654 return;
655 }
656 arg = arg2->vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100657 if (arg == NULL)
658 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100659
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100660 if (STRCMP(cmd, "ex") == 0)
661 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100662 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100663 }
664 else if (STRCMP(cmd, "normal") == 0)
665 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100666 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100667
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100668 ea.arg = arg;
669 ea.addr_count = 0;
670 ea.forceit = TRUE; /* no mapping */
671 ex_normal(&ea);
672 }
673 else if (STRCMP(cmd, "redraw") == 0)
674 {
675 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100676
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100677 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100678 ex_redraw(&ea);
679 showruler(FALSE);
680 setcursor();
681 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100682#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100683 if (gui.in_use)
684 {
685 gui_update_cursor(FALSE, FALSE);
686 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100687 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100688#endif
689 }
690 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
691 {
692 int is_eval = cmd[1] == 'v';
693
694 if (is_eval && arg3->v_type != VAR_NUMBER)
695 {
696 if (p_verbose > 2)
697 EMSG("E904: third argument for eval must be a number");
698 }
699 else
700 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100701 typval_T *tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100702 typval_T err_tv;
703 char_u *json;
704
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100705 /* Don't pollute the display with errors. */
706 ++emsg_skip;
707 tv = eval_expr(arg, NULL);
708 --emsg_skip;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100709 if (is_eval)
710 {
711 if (tv == NULL)
712 {
713 err_tv.v_type = VAR_STRING;
714 err_tv.vval.v_string = (char_u *)"ERROR";
715 tv = &err_tv;
716 }
717 json = json_encode_nr_expr(arg3->vval.v_number, tv);
718 channel_send(idx, json, "eval");
719 vim_free(json);
720 }
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100721 if (tv != &err_tv)
722 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100723 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100724 }
725 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100726 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100727}
728
729/*
730 * Invoke a callback for channel "idx" if needed.
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100731 * Return OK when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100732 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100733 static int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100734may_invoke_callback(int idx)
735{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100736 char_u *msg = NULL;
737 typval_T *listtv = NULL;
738 list_T *list;
739 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100740 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100741 int seq_nr = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100742 int json_mode = channels[idx].ch_json_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100743
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100744 if (channels[idx].ch_close_cb != NULL)
745 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100746 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100747
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100748 if (json_mode)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100749 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100750 /* Get any json message in the queue. */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100751 if (channel_get_json(idx, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100752 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100753 /* Parse readahead, return when there is still no message. */
754 channel_parse_json(idx);
755 if (channel_get_json(idx, -1, &listtv) == FAIL)
756 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100757 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100758
759 list = listtv->vval.v_list;
760 if (list->lv_len < 2)
761 {
762 /* TODO: give error */
763 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100764 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100765 }
766
767 argv[1] = list->lv_first->li_next->li_tv;
768 typetv = &list->lv_first->li_tv;
769 if (typetv->v_type == VAR_STRING)
770 {
771 typval_T *arg3 = NULL;
772 char_u *cmd = typetv->vval.v_string;
773
774 /* ["cmd", arg] */
775 if (list->lv_len == 3)
776 arg3 = &list->lv_last->li_tv;
777 channel_exe_cmd(idx, cmd, &argv[1], arg3);
778 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100779 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100780 }
781
782 if (typetv->v_type != VAR_NUMBER)
783 {
784 /* TODO: give error */
785 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100786 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100787 }
788 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100789 }
Bram Moolenaard7ece102016-02-02 23:23:02 +0100790 else if (channel_peek(idx) == NULL)
791 {
792 /* nothing to read on raw channel */
793 return FALSE;
794 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100795 else
796 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100797 /* For a raw channel we don't know where the message ends, just get
798 * everything. */
799 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100800 argv[1].v_type = VAR_STRING;
801 argv[1].vval.v_string = msg;
802 }
803
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100804 if (channels[idx].ch_req_callback != NULL && seq_nr != 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100805 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100806 /* TODO: check the sequence number */
807 /* invoke the one-time callback */
808 invoke_callback(idx, channels[idx].ch_req_callback, argv);
809 channels[idx].ch_req_callback = NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100810 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100811 else if (channels[idx].ch_callback != NULL)
812 {
813 /* invoke the channel callback */
814 invoke_callback(idx, channels[idx].ch_callback, argv);
815 }
816 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100817
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100818 if (listtv != NULL)
819 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100820 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100821
822 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100823}
824
825/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100826 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100827 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +0100828 */
829 int
830channel_is_open(int idx)
831{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100832 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100833}
834
835/*
836 * Close channel "idx".
837 * This does not trigger the close callback.
838 */
839 void
840channel_close(int idx)
841{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100842 channel_T *channel = &channels[idx];
843 jsonq_T *jhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100844
845 if (channel->ch_fd >= 0)
846 {
847 sock_close(channel->ch_fd);
848 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100849 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100850#ifdef FEAT_GUI
851 channel_gui_unregister(idx);
852#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100853 vim_free(channel->ch_callback);
854 channel->ch_callback = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100855
856 while (channel_peek(idx) != NULL)
857 vim_free(channel_get(idx));
858
859 jhead = &channel->ch_json_head;
860 while (jhead->next != jhead)
861 {
862 clear_tv(jhead->next->value);
863 remove_json_node(jhead->next);
864 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100865 }
866}
867
Bram Moolenaard04a0202016-01-26 23:30:18 +0100868/*
869 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +0100870 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100871 */
Bram Moolenaar83162462016-01-28 23:10:07 +0100872 int
Bram Moolenaard04a0202016-01-26 23:30:18 +0100873channel_save(int idx, char_u *buf, int len)
874{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100875 readq_T *node;
876 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100877
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100878 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100879 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +0100880 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100881 node->buffer = alloc(len + 1);
882 if (node->buffer == NULL)
883 {
884 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +0100885 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100886 }
887 mch_memmove(node->buffer, buf, (size_t)len);
888 node->buffer[len] = NUL;
889
Bram Moolenaard04a0202016-01-26 23:30:18 +0100890 /* insert node at tail of queue */
891 node->next = head;
892 node->prev = head->prev;
893 head->prev->next = node;
894 head->prev = node;
895
896 if (debugfd != NULL)
897 {
898 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +0100899 if (fwrite(buf, len, 1, debugfd) != 1)
900 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100901 fprintf(debugfd, "\n");
902 }
Bram Moolenaar83162462016-01-28 23:10:07 +0100903 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100904}
905
906/*
907 * Return the first buffer from the channel without removing it.
908 * Returns NULL if there is nothing.
909 */
910 char_u *
911channel_peek(int idx)
912{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100913 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100914
915 if (head->next == head || head->next == NULL)
916 return NULL;
917 return head->next->buffer;
918}
919
920/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100921 * Clear the read buffer on channel "idx".
922 */
923 void
924channel_clear(int idx)
925{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100926 readq_T *head = &channels[idx].ch_head;
927 readq_T *node = head->next;
928 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100929
930 while (node != NULL && node != head)
931 {
932 next = node->next;
933 vim_free(node->buffer);
934 vim_free(node);
935 if (next == head)
936 {
937 head->next = head;
938 head->prev = head;
939 break;
940 }
941 node = next;
942 }
943}
944
945/* Sent when the channel is found closed when reading. */
946#define DETACH_MSG "\"DETACH\"\n"
947
948/* Buffer size for reading incoming messages. */
949#define MAXMSGSIZE 4096
950
951/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100952 * Check for reading from "fd" with "timeout" msec.
953 * Return FAIL when there is nothing to read.
954 */
955 static int
956channel_wait(int fd, int timeout)
957{
958#ifdef HAVE_SELECT
959 struct timeval tval;
960 fd_set rfds;
961 int ret;
962
963 FD_ZERO(&rfds);
964 FD_SET(fd, &rfds);
965 tval.tv_sec = timeout / 1000;
966 tval.tv_usec = (timeout % 1000) * 1000;
967 for (;;)
968 {
969 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
970# ifdef EINTR
971 if (ret == -1 && errno == EINTR)
972 continue;
973# endif
974 if (ret <= 0)
975 return FAIL;
976 break;
977 }
978#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +0100979# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100980 struct pollfd fds;
981
982 fds.fd = fd;
983 fds.events = POLLIN;
984 if (poll(&fds, 1, timeout) <= 0)
985 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +0100986# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100987#endif
988 return OK;
989}
990
991/*
992 * Return a unique ID to be used in a message.
993 */
994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100995channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100996{
997 static int next_id = 1;
998
999 return next_id++;
1000}
1001
1002/*
1003 * Read from channel "idx" for as long as there is something to read.
1004 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001005 */
1006 void
1007channel_read(int idx)
1008{
1009 static char_u *buf = NULL;
1010 int len = 0;
1011 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001012 channel_T *channel = &channels[idx];
1013
1014 if (channel->ch_fd < 0)
1015 {
1016 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
1017 return;
1018 }
1019
1020 /* Allocate a buffer to read into. */
1021 if (buf == NULL)
1022 {
1023 buf = alloc(MAXMSGSIZE);
1024 if (buf == NULL)
1025 return; /* out of memory! */
1026 }
1027
1028 /* Keep on reading for as long as there is something to read.
1029 * Use select() or poll() to avoid blocking on a message that is exactly
1030 * MAXMSGSIZE long. */
1031 for (;;)
1032 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001033 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001034 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001035 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1036 if (len <= 0)
1037 break; /* error or nothing more to read */
1038
1039 /* Store the read message in the queue. */
1040 channel_save(idx, buf, len);
1041 readlen += len;
1042 if (len < MAXMSGSIZE)
1043 break; /* did read everything that's available */
1044 }
1045
1046 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1047 if (readlen <= 0)
1048 {
1049 /* Queue a "DETACH" netbeans message in the command queue in order to
1050 * terminate the netbeans session later. Do not end the session here
1051 * directly as we may be running in the context of a call to
1052 * netbeans_parse_messages():
1053 * netbeans_parse_messages
1054 * -> autocmd triggered while processing the netbeans cmd
1055 * -> ui_breakcheck
1056 * -> gui event loop or select loop
1057 * -> channel_read()
1058 */
1059 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1060
1061 channel_close(idx);
1062 if (channel->ch_close_cb != NULL)
1063 (*channel->ch_close_cb)();
1064
1065 if (len < 0)
1066 {
1067 /* Todo: which channel? */
1068 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001069 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001070 }
1071 }
1072
1073#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1074 if (CH_HAS_GUI && gtk_main_level() > 0)
1075 gtk_main_quit();
1076#endif
1077}
1078
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001079/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001080 * Read from raw channel "idx". Blocks until there is something to read or
1081 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001082 * Returns what was read in allocated memory.
1083 * Returns NULL in case of error or timeout.
1084 */
1085 char_u *
1086channel_read_block(int idx)
1087{
1088 if (channel_peek(idx) == NULL)
1089 {
1090 /* Wait for up to 2 seconds.
1091 * TODO: use timeout set on the channel. */
1092 if (channel_wait(channels[idx].ch_fd, 2000) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001093 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001094 channel_read(idx);
1095 }
1096
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001097 return channel_get_all(idx);
1098}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001099
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001100/*
1101 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1102 * result in "rettv".
1103 * Blocks until the message is received.
1104 */
1105 int
1106channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1107{
Bram Moolenaard7ece102016-02-02 23:23:02 +01001108 int more;
1109
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001110 for (;;)
1111 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001112 more = channel_parse_json(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001113
1114 /* search for messsage "id" */
1115 if (channel_get_json(ch_idx, id, rettv) == OK)
1116 return OK;
1117
Bram Moolenaard7ece102016-02-02 23:23:02 +01001118 if (!more)
1119 {
1120 /* Handle any other messages in the queue. If done some more
1121 * messages may have arrived. */
1122 if (channel_parse_messages())
1123 continue;
1124
1125 /* Wait for up to 2 seconds.
1126 * TODO: use timeout set on the channel. */
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001127 if (channels[ch_idx].ch_fd < 0
1128 || channel_wait(channels[ch_idx].ch_fd, 2000) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001129 break;
1130 channel_read(ch_idx);
1131 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001132 }
1133 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001134}
1135
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001136# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001137/*
1138 * Lookup the channel index from the socket.
1139 * Returns -1 when the socket isn't found.
1140 */
1141 int
1142channel_socket2idx(sock_T fd)
1143{
1144 int i;
1145
1146 if (fd >= 0)
1147 for (i = 0; i < channel_count; ++i)
1148 if (channels[i].ch_fd == fd)
1149 return i;
1150 return -1;
1151}
1152# endif
1153
Bram Moolenaard04a0202016-01-26 23:30:18 +01001154/*
1155 * Write "buf" (NUL terminated string) to channel "idx".
1156 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001157 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001158 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001159 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001160channel_send(int idx, char_u *buf, char *fun)
1161{
1162 channel_T *channel = &channels[idx];
1163 int len = (int)STRLEN(buf);
1164
1165 if (channel->ch_fd < 0)
1166 {
1167 if (!channel->ch_error && fun != NULL)
1168 {
1169 CHERROR(" %s(): write while not connected\n", fun);
1170 EMSG2("E630: %s(): write while not connected", fun);
1171 }
1172 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001173 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001174 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001175
1176 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001177 {
1178 if (!channel->ch_error && fun != NULL)
1179 {
1180 CHERROR(" %s(): write failed\n", fun);
1181 EMSG2("E631: %s(): write failed", fun);
1182 }
1183 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001184 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001185 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001186
1187 channel->ch_error = FALSE;
1188 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001189}
1190
1191# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001192/*
1193 * Add open channels to the poll struct.
1194 * Return the adjusted struct index.
1195 * The type of "fds" is hidden to avoid problems with the function proto.
1196 */
1197 int
1198channel_poll_setup(int nfd_in, void *fds_in)
1199{
1200 int nfd = nfd_in;
1201 int i;
1202 struct pollfd *fds = fds_in;
1203
1204 for (i = 0; i < channel_count; ++i)
1205 if (channels[i].ch_fd >= 0)
1206 {
1207 channels[i].ch_idx = nfd;
1208 fds[nfd].fd = channels[i].ch_fd;
1209 fds[nfd].events = POLLIN;
1210 nfd++;
1211 }
1212 else
1213 channels[i].ch_idx = -1;
1214
1215 return nfd;
1216}
1217
1218/*
1219 * The type of "fds" is hidden to avoid problems with the function proto.
1220 */
1221 int
1222channel_poll_check(int ret_in, void *fds_in)
1223{
1224 int ret = ret_in;
1225 int i;
1226 struct pollfd *fds = fds_in;
1227
1228 for (i = 0; i < channel_count; ++i)
1229 if (ret > 0 && channels[i].ch_idx != -1
1230 && fds[channels[i].ch_idx].revents & POLLIN)
1231 {
1232 channel_read(i);
1233 --ret;
1234 }
1235
1236 return ret;
1237}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001238# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001239
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001240# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001241/*
1242 * The type of "rfds" is hidden to avoid problems with the function proto.
1243 */
1244 int
1245channel_select_setup(int maxfd_in, void *rfds_in)
1246{
1247 int maxfd = maxfd_in;
1248 int i;
1249 fd_set *rfds = rfds_in;
1250
1251 for (i = 0; i < channel_count; ++i)
1252 if (channels[i].ch_fd >= 0)
1253 {
1254 FD_SET(channels[i].ch_fd, rfds);
1255 if (maxfd < channels[i].ch_fd)
1256 maxfd = channels[i].ch_fd;
1257 }
1258
1259 return maxfd;
1260}
1261
1262/*
1263 * The type of "rfds" is hidden to avoid problems with the function proto.
1264 */
1265 int
1266channel_select_check(int ret_in, void *rfds_in)
1267{
1268 int ret = ret_in;
1269 int i;
1270 fd_set *rfds = rfds_in;
1271
1272 for (i = 0; i < channel_count; ++i)
1273 if (ret > 0 && channels[i].ch_fd >= 0
1274 && FD_ISSET(channels[i].ch_fd, rfds))
1275 {
1276 channel_read(i);
1277 --ret;
1278 }
1279
1280 return ret;
1281}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001282# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001283
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001284/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01001285 * Execute queued up commands.
1286 * Invoked from the main loop when it's safe to execute received commands.
1287 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001288 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001289 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001290channel_parse_messages(void)
1291{
1292 int i;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001293 int ret = FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001294
1295 for (i = 0; i < channel_count; ++i)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001296 while (may_invoke_callback(i) == OK)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001297 {
1298 i = 0; /* start over */
1299 ret = TRUE;
1300 }
1301 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001302}
1303
Bram Moolenaare0874f82016-01-24 20:36:41 +01001304#endif /* FEAT_CHANNEL */