blob: db13c8a9d7e31666a3c1c0116cc6cdbb20efc20d [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);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100543 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100544 reader.js_fill = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100545 /* reader.js_fill = channel_fill; */
546 reader.js_cookie = &ch_idx;
547 if (json_decode(&reader, &listtv) == OK)
548 {
549 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
550 if (item == NULL)
551 clear_tv(&listtv);
552 else
553 {
554 item->value = alloc_tv();
555 if (item->value == NULL)
556 {
557 vim_free(item);
558 clear_tv(&listtv);
559 }
560 else
561 {
562 *item->value = listtv;
563 item->prev = head->prev;
564 head->prev = item;
565 item->next = head;
566 item->prev->next = item;
567 }
568 }
569 }
570}
571
572/*
573 * Remove "node" from the queue that it is in and free it.
574 * Caller should have freed or used node->value.
575 */
576 static void
577remove_json_node(jsonq_T *node)
578{
579 node->prev->next = node->next;
580 node->next->prev = node->prev;
581 vim_free(node);
582}
583
584/*
585 * Get a message from the JSON queue for channel "ch_idx".
586 * When "id" is positive it must match the first number in the list.
587 * When "id" is zero or negative jut get the first message.
588 * Return OK when found and return the value in "rettv".
589 * Return FAIL otherwise.
590 */
591 static int
592channel_get_json(int ch_idx, int id, typval_T **rettv)
593{
594 jsonq_T *head = &channels[ch_idx].ch_json_head;
595 jsonq_T *item = head->next;
596
597 while (item != head)
598 {
599 list_T *l = item->value->vval.v_list;
600 typval_T *tv = &l->lv_first->li_tv;
601
602 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
603 || id <= 0)
604 {
605 *rettv = item->value;
606 remove_json_node(item);
607 return OK;
608 }
609 item = item->next;
610 }
611 return FAIL;
612}
613
614/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100615 * Execute a command received over channel "idx".
616 * "cmd" is the command string, "arg2" the second argument.
617 * "arg3" is the third argument, NULL if missing.
618 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100619 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100620channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100621{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100622 char_u *arg;
623
624 if (arg2->v_type != VAR_STRING)
625 {
626 if (p_verbose > 2)
627 EMSG("E903: received ex command with non-string argument");
628 return;
629 }
630 arg = arg2->vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100631 if (arg == NULL)
632 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100633
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100634 if (STRCMP(cmd, "ex") == 0)
635 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100636 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100637 }
638 else if (STRCMP(cmd, "normal") == 0)
639 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100640 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100641
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100642 ea.arg = arg;
643 ea.addr_count = 0;
644 ea.forceit = TRUE; /* no mapping */
645 ex_normal(&ea);
646 }
647 else if (STRCMP(cmd, "redraw") == 0)
648 {
649 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100650
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100651 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100652 ex_redraw(&ea);
653 showruler(FALSE);
654 setcursor();
655 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100656#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100657 if (gui.in_use)
658 {
659 gui_update_cursor(FALSE, FALSE);
660 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100661 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100662#endif
663 }
664 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
665 {
666 int is_eval = cmd[1] == 'v';
667
668 if (is_eval && arg3->v_type != VAR_NUMBER)
669 {
670 if (p_verbose > 2)
671 EMSG("E904: third argument for eval must be a number");
672 }
673 else
674 {
675 typval_T *tv = eval_expr(arg, NULL);
676 typval_T err_tv;
677 char_u *json;
678
679 if (is_eval)
680 {
681 if (tv == NULL)
682 {
683 err_tv.v_type = VAR_STRING;
684 err_tv.vval.v_string = (char_u *)"ERROR";
685 tv = &err_tv;
686 }
687 json = json_encode_nr_expr(arg3->vval.v_number, tv);
688 channel_send(idx, json, "eval");
689 vim_free(json);
690 }
691 free_tv(tv);
692 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100693 }
694 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100695 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100696}
697
698/*
699 * Invoke a callback for channel "idx" if needed.
700 */
701 static void
702may_invoke_callback(int idx)
703{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100704 char_u *msg = NULL;
705 typval_T *listtv = NULL;
706 list_T *list;
707 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100708 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100709 int seq_nr = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100710 int json_mode = channels[idx].ch_json_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100711
712 if (channel_peek(idx) == NULL)
713 return;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100714 if (channels[idx].ch_close_cb != NULL)
715 /* this channel is handled elsewhere (netbeans) */
716 return;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100717
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100718 if (json_mode)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100719 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100720 /* Get any json message. Return if there isn't one. */
721 channel_read_json(idx);
722 if (channel_get_json(idx, -1, &listtv) == FAIL)
723 return;
724 if (listtv->v_type != VAR_LIST)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100725 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100726 /* TODO: give error */
727 clear_tv(listtv);
728 return;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100729 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100730
731 list = listtv->vval.v_list;
732 if (list->lv_len < 2)
733 {
734 /* TODO: give error */
735 clear_tv(listtv);
736 return;
737 }
738
739 argv[1] = list->lv_first->li_next->li_tv;
740 typetv = &list->lv_first->li_tv;
741 if (typetv->v_type == VAR_STRING)
742 {
743 typval_T *arg3 = NULL;
744 char_u *cmd = typetv->vval.v_string;
745
746 /* ["cmd", arg] */
747 if (list->lv_len == 3)
748 arg3 = &list->lv_last->li_tv;
749 channel_exe_cmd(idx, cmd, &argv[1], arg3);
750 clear_tv(listtv);
751 return;
752 }
753
754 if (typetv->v_type != VAR_NUMBER)
755 {
756 /* TODO: give error */
757 clear_tv(listtv);
758 return;
759 }
760 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100761 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100762 else
763 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100764 /* For a raw channel we don't know where the message ends, just get
765 * everything. */
766 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100767 argv[1].v_type = VAR_STRING;
768 argv[1].vval.v_string = msg;
769 }
770
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100771 if (channels[idx].ch_req_callback != NULL && seq_nr != 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100772 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100773 /* TODO: check the sequence number */
774 /* invoke the one-time callback */
775 invoke_callback(idx, channels[idx].ch_req_callback, argv);
776 channels[idx].ch_req_callback = NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100777 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100778 else if (channels[idx].ch_callback != NULL)
779 {
780 /* invoke the channel callback */
781 invoke_callback(idx, channels[idx].ch_callback, argv);
782 }
783 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100784
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100785 if (listtv != NULL)
786 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100787 vim_free(msg);
788}
789
790/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100791 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100792 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +0100793 */
794 int
795channel_is_open(int idx)
796{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100797 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100798}
799
800/*
801 * Close channel "idx".
802 * This does not trigger the close callback.
803 */
804 void
805channel_close(int idx)
806{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100807 channel_T *channel = &channels[idx];
808 jsonq_T *jhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100809
810 if (channel->ch_fd >= 0)
811 {
812 sock_close(channel->ch_fd);
813 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100814 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100815#ifdef FEAT_GUI
816 channel_gui_unregister(idx);
817#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100818 vim_free(channel->ch_callback);
819 channel->ch_callback = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100820
821 while (channel_peek(idx) != NULL)
822 vim_free(channel_get(idx));
823
824 jhead = &channel->ch_json_head;
825 while (jhead->next != jhead)
826 {
827 clear_tv(jhead->next->value);
828 remove_json_node(jhead->next);
829 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100830 }
831}
832
Bram Moolenaard04a0202016-01-26 23:30:18 +0100833/*
834 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +0100835 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100836 */
Bram Moolenaar83162462016-01-28 23:10:07 +0100837 int
Bram Moolenaard04a0202016-01-26 23:30:18 +0100838channel_save(int idx, char_u *buf, int len)
839{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100840 readq_T *node;
841 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100842
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100843 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100844 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +0100845 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100846 node->buffer = alloc(len + 1);
847 if (node->buffer == NULL)
848 {
849 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +0100850 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100851 }
852 mch_memmove(node->buffer, buf, (size_t)len);
853 node->buffer[len] = NUL;
854
Bram Moolenaard04a0202016-01-26 23:30:18 +0100855 /* insert node at tail of queue */
856 node->next = head;
857 node->prev = head->prev;
858 head->prev->next = node;
859 head->prev = node;
860
861 if (debugfd != NULL)
862 {
863 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +0100864 if (fwrite(buf, len, 1, debugfd) != 1)
865 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100866 fprintf(debugfd, "\n");
867 }
Bram Moolenaar83162462016-01-28 23:10:07 +0100868 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100869}
870
871/*
872 * Return the first buffer from the channel without removing it.
873 * Returns NULL if there is nothing.
874 */
875 char_u *
876channel_peek(int idx)
877{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100878 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100879
880 if (head->next == head || head->next == NULL)
881 return NULL;
882 return head->next->buffer;
883}
884
885/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100886 * Clear the read buffer on channel "idx".
887 */
888 void
889channel_clear(int idx)
890{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100891 readq_T *head = &channels[idx].ch_head;
892 readq_T *node = head->next;
893 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100894
895 while (node != NULL && node != head)
896 {
897 next = node->next;
898 vim_free(node->buffer);
899 vim_free(node);
900 if (next == head)
901 {
902 head->next = head;
903 head->prev = head;
904 break;
905 }
906 node = next;
907 }
908}
909
910/* Sent when the channel is found closed when reading. */
911#define DETACH_MSG "\"DETACH\"\n"
912
913/* Buffer size for reading incoming messages. */
914#define MAXMSGSIZE 4096
915
916/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100917 * Check for reading from "fd" with "timeout" msec.
918 * Return FAIL when there is nothing to read.
919 */
920 static int
921channel_wait(int fd, int timeout)
922{
923#ifdef HAVE_SELECT
924 struct timeval tval;
925 fd_set rfds;
926 int ret;
927
928 FD_ZERO(&rfds);
929 FD_SET(fd, &rfds);
930 tval.tv_sec = timeout / 1000;
931 tval.tv_usec = (timeout % 1000) * 1000;
932 for (;;)
933 {
934 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
935# ifdef EINTR
936 if (ret == -1 && errno == EINTR)
937 continue;
938# endif
939 if (ret <= 0)
940 return FAIL;
941 break;
942 }
943#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +0100944# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100945 struct pollfd fds;
946
947 fds.fd = fd;
948 fds.events = POLLIN;
949 if (poll(&fds, 1, timeout) <= 0)
950 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +0100951# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100952#endif
953 return OK;
954}
955
956/*
957 * Return a unique ID to be used in a message.
958 */
959 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100960channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100961{
962 static int next_id = 1;
963
964 return next_id++;
965}
966
967/*
968 * Read from channel "idx" for as long as there is something to read.
969 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100970 */
971 void
972channel_read(int idx)
973{
974 static char_u *buf = NULL;
975 int len = 0;
976 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100977 channel_T *channel = &channels[idx];
978
979 if (channel->ch_fd < 0)
980 {
981 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
982 return;
983 }
984
985 /* Allocate a buffer to read into. */
986 if (buf == NULL)
987 {
988 buf = alloc(MAXMSGSIZE);
989 if (buf == NULL)
990 return; /* out of memory! */
991 }
992
993 /* Keep on reading for as long as there is something to read.
994 * Use select() or poll() to avoid blocking on a message that is exactly
995 * MAXMSGSIZE long. */
996 for (;;)
997 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100998 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100999 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001000 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1001 if (len <= 0)
1002 break; /* error or nothing more to read */
1003
1004 /* Store the read message in the queue. */
1005 channel_save(idx, buf, len);
1006 readlen += len;
1007 if (len < MAXMSGSIZE)
1008 break; /* did read everything that's available */
1009 }
1010
1011 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1012 if (readlen <= 0)
1013 {
1014 /* Queue a "DETACH" netbeans message in the command queue in order to
1015 * terminate the netbeans session later. Do not end the session here
1016 * directly as we may be running in the context of a call to
1017 * netbeans_parse_messages():
1018 * netbeans_parse_messages
1019 * -> autocmd triggered while processing the netbeans cmd
1020 * -> ui_breakcheck
1021 * -> gui event loop or select loop
1022 * -> channel_read()
1023 */
1024 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1025
1026 channel_close(idx);
1027 if (channel->ch_close_cb != NULL)
1028 (*channel->ch_close_cb)();
1029
1030 if (len < 0)
1031 {
1032 /* Todo: which channel? */
1033 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001034 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001035 }
1036 }
1037
1038#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1039 if (CH_HAS_GUI && gtk_main_level() > 0)
1040 gtk_main_quit();
1041#endif
1042}
1043
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001044/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001045 * Read from raw channel "idx". Blocks until there is something to read or
1046 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001047 * Returns what was read in allocated memory.
1048 * Returns NULL in case of error or timeout.
1049 */
1050 char_u *
1051channel_read_block(int idx)
1052{
1053 if (channel_peek(idx) == NULL)
1054 {
1055 /* Wait for up to 2 seconds.
1056 * TODO: use timeout set on the channel. */
1057 if (channel_wait(channels[idx].ch_fd, 2000) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001058 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001059 channel_read(idx);
1060 }
1061
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001062 return channel_get_all(idx);
1063}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001064
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001065/*
1066 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1067 * result in "rettv".
1068 * Blocks until the message is received.
1069 */
1070 int
1071channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1072{
1073 for (;;)
1074 {
1075 channel_read_json(ch_idx);
1076
1077 /* search for messsage "id" */
1078 if (channel_get_json(ch_idx, id, rettv) == OK)
1079 return OK;
1080
1081 /* Wait for up to 2 seconds.
1082 * TODO: use timeout set on the channel. */
1083 if (channel_wait(channels[ch_idx].ch_fd, 2000) == FAIL)
1084 break;
1085 channel_read(ch_idx);
1086 }
1087 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001088}
1089
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001090# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001091/*
1092 * Lookup the channel index from the socket.
1093 * Returns -1 when the socket isn't found.
1094 */
1095 int
1096channel_socket2idx(sock_T fd)
1097{
1098 int i;
1099
1100 if (fd >= 0)
1101 for (i = 0; i < channel_count; ++i)
1102 if (channels[i].ch_fd == fd)
1103 return i;
1104 return -1;
1105}
1106# endif
1107
Bram Moolenaard04a0202016-01-26 23:30:18 +01001108/*
1109 * Write "buf" (NUL terminated string) to channel "idx".
1110 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001111 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001112 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001113 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001114channel_send(int idx, char_u *buf, char *fun)
1115{
1116 channel_T *channel = &channels[idx];
1117 int len = (int)STRLEN(buf);
1118
1119 if (channel->ch_fd < 0)
1120 {
1121 if (!channel->ch_error && fun != NULL)
1122 {
1123 CHERROR(" %s(): write while not connected\n", fun);
1124 EMSG2("E630: %s(): write while not connected", fun);
1125 }
1126 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001127 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001128 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001129
1130 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001131 {
1132 if (!channel->ch_error && fun != NULL)
1133 {
1134 CHERROR(" %s(): write failed\n", fun);
1135 EMSG2("E631: %s(): write failed", fun);
1136 }
1137 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001138 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001139 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001140
1141 channel->ch_error = FALSE;
1142 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001143}
1144
1145# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001146/*
1147 * Add open channels to the poll struct.
1148 * Return the adjusted struct index.
1149 * The type of "fds" is hidden to avoid problems with the function proto.
1150 */
1151 int
1152channel_poll_setup(int nfd_in, void *fds_in)
1153{
1154 int nfd = nfd_in;
1155 int i;
1156 struct pollfd *fds = fds_in;
1157
1158 for (i = 0; i < channel_count; ++i)
1159 if (channels[i].ch_fd >= 0)
1160 {
1161 channels[i].ch_idx = nfd;
1162 fds[nfd].fd = channels[i].ch_fd;
1163 fds[nfd].events = POLLIN;
1164 nfd++;
1165 }
1166 else
1167 channels[i].ch_idx = -1;
1168
1169 return nfd;
1170}
1171
1172/*
1173 * The type of "fds" is hidden to avoid problems with the function proto.
1174 */
1175 int
1176channel_poll_check(int ret_in, void *fds_in)
1177{
1178 int ret = ret_in;
1179 int i;
1180 struct pollfd *fds = fds_in;
1181
1182 for (i = 0; i < channel_count; ++i)
1183 if (ret > 0 && channels[i].ch_idx != -1
1184 && fds[channels[i].ch_idx].revents & POLLIN)
1185 {
1186 channel_read(i);
1187 --ret;
1188 }
1189
1190 return ret;
1191}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001192# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001193
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001194# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001195/*
1196 * The type of "rfds" is hidden to avoid problems with the function proto.
1197 */
1198 int
1199channel_select_setup(int maxfd_in, void *rfds_in)
1200{
1201 int maxfd = maxfd_in;
1202 int i;
1203 fd_set *rfds = rfds_in;
1204
1205 for (i = 0; i < channel_count; ++i)
1206 if (channels[i].ch_fd >= 0)
1207 {
1208 FD_SET(channels[i].ch_fd, rfds);
1209 if (maxfd < channels[i].ch_fd)
1210 maxfd = channels[i].ch_fd;
1211 }
1212
1213 return maxfd;
1214}
1215
1216/*
1217 * The type of "rfds" is hidden to avoid problems with the function proto.
1218 */
1219 int
1220channel_select_check(int ret_in, void *rfds_in)
1221{
1222 int ret = ret_in;
1223 int i;
1224 fd_set *rfds = rfds_in;
1225
1226 for (i = 0; i < channel_count; ++i)
1227 if (ret > 0 && channels[i].ch_fd >= 0
1228 && FD_ISSET(channels[i].ch_fd, rfds))
1229 {
1230 channel_read(i);
1231 --ret;
1232 }
1233
1234 return ret;
1235}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001236# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001237
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001238/*
1239 * Invoked from the main loop when it's save to execute received commands.
1240 */
1241 void
1242channel_parse_messages(void)
1243{
1244 int i;
1245
1246 for (i = 0; i < channel_count; ++i)
1247 may_invoke_callback(i);
1248}
1249
Bram Moolenaare0874f82016-01-24 20:36:41 +01001250#endif /* FEAT_CHANNEL */