blob: f329250973b78d5f8efc618dface76834093ce42 [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/*
526 * Use the read buffer of channel "ch_idx" and parse JSON messages that are
527 * complete. The messages are added to the queue.
528 */
529 void
530channel_read_json(int ch_idx)
531{
532 js_read_T reader;
533 typval_T listtv;
534 jsonq_T *item;
535 jsonq_T *head = &channels[ch_idx].ch_json_head;
536
537 if (channel_peek(ch_idx) == NULL)
538 return;
539
540 /* TODO: make reader work properly */
541 /* reader.js_buf = channel_peek(ch_idx); */
542 reader.js_buf = channel_get_all(ch_idx);
543 reader.js_eof = TRUE;
544 /* reader.js_eof = FALSE; */
545 reader.js_used = 0;
546 /* reader.js_fill = channel_fill; */
547 reader.js_cookie = &ch_idx;
548 if (json_decode(&reader, &listtv) == OK)
549 {
550 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
551 if (item == NULL)
552 clear_tv(&listtv);
553 else
554 {
555 item->value = alloc_tv();
556 if (item->value == NULL)
557 {
558 vim_free(item);
559 clear_tv(&listtv);
560 }
561 else
562 {
563 *item->value = listtv;
564 item->prev = head->prev;
565 head->prev = item;
566 item->next = head;
567 item->prev->next = item;
568 }
569 }
570 }
571}
572
573/*
574 * Remove "node" from the queue that it is in and free it.
575 * Caller should have freed or used node->value.
576 */
577 static void
578remove_json_node(jsonq_T *node)
579{
580 node->prev->next = node->next;
581 node->next->prev = node->prev;
582 vim_free(node);
583}
584
585/*
586 * Get a message from the JSON queue for channel "ch_idx".
587 * When "id" is positive it must match the first number in the list.
588 * When "id" is zero or negative jut get the first message.
589 * Return OK when found and return the value in "rettv".
590 * Return FAIL otherwise.
591 */
592 static int
593channel_get_json(int ch_idx, int id, typval_T **rettv)
594{
595 jsonq_T *head = &channels[ch_idx].ch_json_head;
596 jsonq_T *item = head->next;
597
598 while (item != head)
599 {
600 list_T *l = item->value->vval.v_list;
601 typval_T *tv = &l->lv_first->li_tv;
602
603 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
604 || id <= 0)
605 {
606 *rettv = item->value;
607 remove_json_node(item);
608 return OK;
609 }
610 item = item->next;
611 }
612 return FAIL;
613}
614
615/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100616 * Execute a command received over channel "idx".
617 * "cmd" is the command string, "arg2" the second argument.
618 * "arg3" is the third argument, NULL if missing.
619 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100620 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100621channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100622{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100623 char_u *arg;
624
625 if (arg2->v_type != VAR_STRING)
626 {
627 if (p_verbose > 2)
628 EMSG("E903: received ex command with non-string argument");
629 return;
630 }
631 arg = arg2->vval.v_string;
632
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100633 if (STRCMP(cmd, "ex") == 0)
634 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100635 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100636 }
637 else if (STRCMP(cmd, "normal") == 0)
638 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100639 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100640
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100641 ea.arg = arg;
642 ea.addr_count = 0;
643 ea.forceit = TRUE; /* no mapping */
644 ex_normal(&ea);
645 }
646 else if (STRCMP(cmd, "redraw") == 0)
647 {
648 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100649
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100650 ea.forceit = arg != NULL && *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100651 ex_redraw(&ea);
652 showruler(FALSE);
653 setcursor();
654 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100655#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100656 if (gui.in_use)
657 {
658 gui_update_cursor(FALSE, FALSE);
659 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100660 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100661#endif
662 }
663 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
664 {
665 int is_eval = cmd[1] == 'v';
666
667 if (is_eval && arg3->v_type != VAR_NUMBER)
668 {
669 if (p_verbose > 2)
670 EMSG("E904: third argument for eval must be a number");
671 }
672 else
673 {
674 typval_T *tv = eval_expr(arg, NULL);
675 typval_T err_tv;
676 char_u *json;
677
678 if (is_eval)
679 {
680 if (tv == NULL)
681 {
682 err_tv.v_type = VAR_STRING;
683 err_tv.vval.v_string = (char_u *)"ERROR";
684 tv = &err_tv;
685 }
686 json = json_encode_nr_expr(arg3->vval.v_number, tv);
687 channel_send(idx, json, "eval");
688 vim_free(json);
689 }
690 free_tv(tv);
691 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100692 }
693 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100694 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100695}
696
697/*
698 * Invoke a callback for channel "idx" if needed.
699 */
700 static void
701may_invoke_callback(int idx)
702{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100703 char_u *msg = NULL;
704 typval_T *listtv = NULL;
705 list_T *list;
706 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100707 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100708 int seq_nr = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100709 int json_mode = channels[idx].ch_json_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100710
711 if (channel_peek(idx) == NULL)
712 return;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100713 if (channels[idx].ch_close_cb != NULL)
714 /* this channel is handled elsewhere (netbeans) */
715 return;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100716
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100717 if (json_mode)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100718 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100719 /* Get any json message. Return if there isn't one. */
720 channel_read_json(idx);
721 if (channel_get_json(idx, -1, &listtv) == FAIL)
722 return;
723 if (listtv->v_type != VAR_LIST)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100724 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100725 /* TODO: give error */
726 clear_tv(listtv);
727 return;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100728 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100729
730 list = listtv->vval.v_list;
731 if (list->lv_len < 2)
732 {
733 /* TODO: give error */
734 clear_tv(listtv);
735 return;
736 }
737
738 argv[1] = list->lv_first->li_next->li_tv;
739 typetv = &list->lv_first->li_tv;
740 if (typetv->v_type == VAR_STRING)
741 {
742 typval_T *arg3 = NULL;
743 char_u *cmd = typetv->vval.v_string;
744
745 /* ["cmd", arg] */
746 if (list->lv_len == 3)
747 arg3 = &list->lv_last->li_tv;
748 channel_exe_cmd(idx, cmd, &argv[1], arg3);
749 clear_tv(listtv);
750 return;
751 }
752
753 if (typetv->v_type != VAR_NUMBER)
754 {
755 /* TODO: give error */
756 clear_tv(listtv);
757 return;
758 }
759 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100760 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100761 else
762 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100763 /* For a raw channel we don't know where the message ends, just get
764 * everything. */
765 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100766 argv[1].v_type = VAR_STRING;
767 argv[1].vval.v_string = msg;
768 }
769
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100770 if (channels[idx].ch_req_callback != NULL && seq_nr != 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100771 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100772 /* TODO: check the sequence number */
773 /* invoke the one-time callback */
774 invoke_callback(idx, channels[idx].ch_req_callback, argv);
775 channels[idx].ch_req_callback = NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100776 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100777 else if (channels[idx].ch_callback != NULL)
778 {
779 /* invoke the channel callback */
780 invoke_callback(idx, channels[idx].ch_callback, argv);
781 }
782 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100783
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100784 if (listtv != NULL)
785 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100786 vim_free(msg);
787}
788
789/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100790 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100791 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +0100792 */
793 int
794channel_is_open(int idx)
795{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100796 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100797}
798
799/*
800 * Close channel "idx".
801 * This does not trigger the close callback.
802 */
803 void
804channel_close(int idx)
805{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100806 channel_T *channel = &channels[idx];
807 jsonq_T *jhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100808
809 if (channel->ch_fd >= 0)
810 {
811 sock_close(channel->ch_fd);
812 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100813 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100814#ifdef FEAT_GUI
815 channel_gui_unregister(idx);
816#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100817 vim_free(channel->ch_callback);
818 channel->ch_callback = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100819
820 while (channel_peek(idx) != NULL)
821 vim_free(channel_get(idx));
822
823 jhead = &channel->ch_json_head;
824 while (jhead->next != jhead)
825 {
826 clear_tv(jhead->next->value);
827 remove_json_node(jhead->next);
828 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100829 }
830}
831
Bram Moolenaard04a0202016-01-26 23:30:18 +0100832/*
833 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +0100834 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100835 */
Bram Moolenaar83162462016-01-28 23:10:07 +0100836 int
Bram Moolenaard04a0202016-01-26 23:30:18 +0100837channel_save(int idx, char_u *buf, int len)
838{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100839 readq_T *node;
840 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100841
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100842 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100843 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +0100844 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100845 node->buffer = alloc(len + 1);
846 if (node->buffer == NULL)
847 {
848 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +0100849 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100850 }
851 mch_memmove(node->buffer, buf, (size_t)len);
852 node->buffer[len] = NUL;
853
Bram Moolenaard04a0202016-01-26 23:30:18 +0100854 /* insert node at tail of queue */
855 node->next = head;
856 node->prev = head->prev;
857 head->prev->next = node;
858 head->prev = node;
859
860 if (debugfd != NULL)
861 {
862 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +0100863 if (fwrite(buf, len, 1, debugfd) != 1)
864 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100865 fprintf(debugfd, "\n");
866 }
Bram Moolenaar83162462016-01-28 23:10:07 +0100867 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100868}
869
870/*
871 * Return the first buffer from the channel without removing it.
872 * Returns NULL if there is nothing.
873 */
874 char_u *
875channel_peek(int idx)
876{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100877 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100878
879 if (head->next == head || head->next == NULL)
880 return NULL;
881 return head->next->buffer;
882}
883
884/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100885 * Clear the read buffer on channel "idx".
886 */
887 void
888channel_clear(int idx)
889{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100890 readq_T *head = &channels[idx].ch_head;
891 readq_T *node = head->next;
892 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100893
894 while (node != NULL && node != head)
895 {
896 next = node->next;
897 vim_free(node->buffer);
898 vim_free(node);
899 if (next == head)
900 {
901 head->next = head;
902 head->prev = head;
903 break;
904 }
905 node = next;
906 }
907}
908
909/* Sent when the channel is found closed when reading. */
910#define DETACH_MSG "\"DETACH\"\n"
911
912/* Buffer size for reading incoming messages. */
913#define MAXMSGSIZE 4096
914
915/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100916 * Check for reading from "fd" with "timeout" msec.
917 * Return FAIL when there is nothing to read.
918 */
919 static int
920channel_wait(int fd, int timeout)
921{
922#ifdef HAVE_SELECT
923 struct timeval tval;
924 fd_set rfds;
925 int ret;
926
927 FD_ZERO(&rfds);
928 FD_SET(fd, &rfds);
929 tval.tv_sec = timeout / 1000;
930 tval.tv_usec = (timeout % 1000) * 1000;
931 for (;;)
932 {
933 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
934# ifdef EINTR
935 if (ret == -1 && errno == EINTR)
936 continue;
937# endif
938 if (ret <= 0)
939 return FAIL;
940 break;
941 }
942#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +0100943# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100944 struct pollfd fds;
945
946 fds.fd = fd;
947 fds.events = POLLIN;
948 if (poll(&fds, 1, timeout) <= 0)
949 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +0100950# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100951#endif
952 return OK;
953}
954
955/*
956 * Return a unique ID to be used in a message.
957 */
958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100959channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100960{
961 static int next_id = 1;
962
963 return next_id++;
964}
965
966/*
967 * Read from channel "idx" for as long as there is something to read.
968 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100969 */
970 void
971channel_read(int idx)
972{
973 static char_u *buf = NULL;
974 int len = 0;
975 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100976 channel_T *channel = &channels[idx];
977
978 if (channel->ch_fd < 0)
979 {
980 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
981 return;
982 }
983
984 /* Allocate a buffer to read into. */
985 if (buf == NULL)
986 {
987 buf = alloc(MAXMSGSIZE);
988 if (buf == NULL)
989 return; /* out of memory! */
990 }
991
992 /* Keep on reading for as long as there is something to read.
993 * Use select() or poll() to avoid blocking on a message that is exactly
994 * MAXMSGSIZE long. */
995 for (;;)
996 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100997 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100998 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100999 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1000 if (len <= 0)
1001 break; /* error or nothing more to read */
1002
1003 /* Store the read message in the queue. */
1004 channel_save(idx, buf, len);
1005 readlen += len;
1006 if (len < MAXMSGSIZE)
1007 break; /* did read everything that's available */
1008 }
1009
1010 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1011 if (readlen <= 0)
1012 {
1013 /* Queue a "DETACH" netbeans message in the command queue in order to
1014 * terminate the netbeans session later. Do not end the session here
1015 * directly as we may be running in the context of a call to
1016 * netbeans_parse_messages():
1017 * netbeans_parse_messages
1018 * -> autocmd triggered while processing the netbeans cmd
1019 * -> ui_breakcheck
1020 * -> gui event loop or select loop
1021 * -> channel_read()
1022 */
1023 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1024
1025 channel_close(idx);
1026 if (channel->ch_close_cb != NULL)
1027 (*channel->ch_close_cb)();
1028
1029 if (len < 0)
1030 {
1031 /* Todo: which channel? */
1032 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001033 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001034 }
1035 }
1036
1037#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1038 if (CH_HAS_GUI && gtk_main_level() > 0)
1039 gtk_main_quit();
1040#endif
1041}
1042
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001043/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001044 * Read from raw channel "idx". Blocks until there is something to read or
1045 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001046 * Returns what was read in allocated memory.
1047 * Returns NULL in case of error or timeout.
1048 */
1049 char_u *
1050channel_read_block(int idx)
1051{
1052 if (channel_peek(idx) == NULL)
1053 {
1054 /* Wait for up to 2 seconds.
1055 * TODO: use timeout set on the channel. */
1056 if (channel_wait(channels[idx].ch_fd, 2000) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001057 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001058 channel_read(idx);
1059 }
1060
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001061 return channel_get_all(idx);
1062}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001063
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001064/*
1065 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1066 * result in "rettv".
1067 * Blocks until the message is received.
1068 */
1069 int
1070channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1071{
1072 for (;;)
1073 {
1074 channel_read_json(ch_idx);
1075
1076 /* search for messsage "id" */
1077 if (channel_get_json(ch_idx, id, rettv) == OK)
1078 return OK;
1079
1080 /* Wait for up to 2 seconds.
1081 * TODO: use timeout set on the channel. */
1082 if (channel_wait(channels[ch_idx].ch_fd, 2000) == FAIL)
1083 break;
1084 channel_read(ch_idx);
1085 }
1086 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001087}
1088
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001089# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001090/*
1091 * Lookup the channel index from the socket.
1092 * Returns -1 when the socket isn't found.
1093 */
1094 int
1095channel_socket2idx(sock_T fd)
1096{
1097 int i;
1098
1099 if (fd >= 0)
1100 for (i = 0; i < channel_count; ++i)
1101 if (channels[i].ch_fd == fd)
1102 return i;
1103 return -1;
1104}
1105# endif
1106
Bram Moolenaard04a0202016-01-26 23:30:18 +01001107/*
1108 * Write "buf" (NUL terminated string) to channel "idx".
1109 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001110 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001111 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001112 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001113channel_send(int idx, char_u *buf, char *fun)
1114{
1115 channel_T *channel = &channels[idx];
1116 int len = (int)STRLEN(buf);
1117
1118 if (channel->ch_fd < 0)
1119 {
1120 if (!channel->ch_error && fun != NULL)
1121 {
1122 CHERROR(" %s(): write while not connected\n", fun);
1123 EMSG2("E630: %s(): write while not connected", fun);
1124 }
1125 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001126 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001127 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001128
1129 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001130 {
1131 if (!channel->ch_error && fun != NULL)
1132 {
1133 CHERROR(" %s(): write failed\n", fun);
1134 EMSG2("E631: %s(): write failed", fun);
1135 }
1136 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001137 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001138 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001139
1140 channel->ch_error = FALSE;
1141 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001142}
1143
1144# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001145/*
1146 * Add open channels to the poll struct.
1147 * Return the adjusted struct index.
1148 * The type of "fds" is hidden to avoid problems with the function proto.
1149 */
1150 int
1151channel_poll_setup(int nfd_in, void *fds_in)
1152{
1153 int nfd = nfd_in;
1154 int i;
1155 struct pollfd *fds = fds_in;
1156
1157 for (i = 0; i < channel_count; ++i)
1158 if (channels[i].ch_fd >= 0)
1159 {
1160 channels[i].ch_idx = nfd;
1161 fds[nfd].fd = channels[i].ch_fd;
1162 fds[nfd].events = POLLIN;
1163 nfd++;
1164 }
1165 else
1166 channels[i].ch_idx = -1;
1167
1168 return nfd;
1169}
1170
1171/*
1172 * The type of "fds" is hidden to avoid problems with the function proto.
1173 */
1174 int
1175channel_poll_check(int ret_in, void *fds_in)
1176{
1177 int ret = ret_in;
1178 int i;
1179 struct pollfd *fds = fds_in;
1180
1181 for (i = 0; i < channel_count; ++i)
1182 if (ret > 0 && channels[i].ch_idx != -1
1183 && fds[channels[i].ch_idx].revents & POLLIN)
1184 {
1185 channel_read(i);
1186 --ret;
1187 }
1188
1189 return ret;
1190}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001191# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001192
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001193# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001194/*
1195 * The type of "rfds" is hidden to avoid problems with the function proto.
1196 */
1197 int
1198channel_select_setup(int maxfd_in, void *rfds_in)
1199{
1200 int maxfd = maxfd_in;
1201 int i;
1202 fd_set *rfds = rfds_in;
1203
1204 for (i = 0; i < channel_count; ++i)
1205 if (channels[i].ch_fd >= 0)
1206 {
1207 FD_SET(channels[i].ch_fd, rfds);
1208 if (maxfd < channels[i].ch_fd)
1209 maxfd = channels[i].ch_fd;
1210 }
1211
1212 return maxfd;
1213}
1214
1215/*
1216 * The type of "rfds" is hidden to avoid problems with the function proto.
1217 */
1218 int
1219channel_select_check(int ret_in, void *rfds_in)
1220{
1221 int ret = ret_in;
1222 int i;
1223 fd_set *rfds = rfds_in;
1224
1225 for (i = 0; i < channel_count; ++i)
1226 if (ret > 0 && channels[i].ch_fd >= 0
1227 && FD_ISSET(channels[i].ch_fd, rfds))
1228 {
1229 channel_read(i);
1230 --ret;
1231 }
1232
1233 return ret;
1234}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001235# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001236
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001237/*
1238 * Invoked from the main loop when it's save to execute received commands.
1239 */
1240 void
1241channel_parse_messages(void)
1242{
1243 int i;
1244
1245 for (i = 0; i < channel_count; ++i)
1246 may_invoke_callback(i);
1247}
1248
Bram Moolenaare0874f82016-01-24 20:36:41 +01001249#endif /* FEAT_CHANNEL */