blob: 7183ff61fc6ab56e560e5b8ce4358c1679e5b70e [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 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100570
571 /* Put the unread part back into the channel.
572 * TODO: insert in front */
573 if (reader.js_buf[reader.js_used] != NUL)
574 channel_save(ch_idx, reader.js_buf + reader.js_used,
575 (int)(reader.js_end - reader.js_buf) - reader.js_used);
576 vim_free(reader.js_buf);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100577}
578
579/*
580 * Remove "node" from the queue that it is in and free it.
581 * Caller should have freed or used node->value.
582 */
583 static void
584remove_json_node(jsonq_T *node)
585{
586 node->prev->next = node->next;
587 node->next->prev = node->prev;
588 vim_free(node);
589}
590
591/*
592 * Get a message from the JSON queue for channel "ch_idx".
593 * When "id" is positive it must match the first number in the list.
594 * When "id" is zero or negative jut get the first message.
595 * Return OK when found and return the value in "rettv".
596 * Return FAIL otherwise.
597 */
598 static int
599channel_get_json(int ch_idx, int id, typval_T **rettv)
600{
601 jsonq_T *head = &channels[ch_idx].ch_json_head;
602 jsonq_T *item = head->next;
603
604 while (item != head)
605 {
606 list_T *l = item->value->vval.v_list;
607 typval_T *tv = &l->lv_first->li_tv;
608
609 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
610 || id <= 0)
611 {
612 *rettv = item->value;
613 remove_json_node(item);
614 return OK;
615 }
616 item = item->next;
617 }
618 return FAIL;
619}
620
621/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100622 * Execute a command received over channel "idx".
623 * "cmd" is the command string, "arg2" the second argument.
624 * "arg3" is the third argument, NULL if missing.
625 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100626 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100627channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100628{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100629 char_u *arg;
630
631 if (arg2->v_type != VAR_STRING)
632 {
633 if (p_verbose > 2)
634 EMSG("E903: received ex command with non-string argument");
635 return;
636 }
637 arg = arg2->vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100638 if (arg == NULL)
639 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100640
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100641 if (STRCMP(cmd, "ex") == 0)
642 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100643 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100644 }
645 else if (STRCMP(cmd, "normal") == 0)
646 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100647 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100648
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100649 ea.arg = arg;
650 ea.addr_count = 0;
651 ea.forceit = TRUE; /* no mapping */
652 ex_normal(&ea);
653 }
654 else if (STRCMP(cmd, "redraw") == 0)
655 {
656 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100657
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100658 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100659 ex_redraw(&ea);
660 showruler(FALSE);
661 setcursor();
662 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100663#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100664 if (gui.in_use)
665 {
666 gui_update_cursor(FALSE, FALSE);
667 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100668 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100669#endif
670 }
671 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
672 {
673 int is_eval = cmd[1] == 'v';
674
675 if (is_eval && arg3->v_type != VAR_NUMBER)
676 {
677 if (p_verbose > 2)
678 EMSG("E904: third argument for eval must be a number");
679 }
680 else
681 {
682 typval_T *tv = eval_expr(arg, NULL);
683 typval_T err_tv;
684 char_u *json;
685
686 if (is_eval)
687 {
688 if (tv == NULL)
689 {
690 err_tv.v_type = VAR_STRING;
691 err_tv.vval.v_string = (char_u *)"ERROR";
692 tv = &err_tv;
693 }
694 json = json_encode_nr_expr(arg3->vval.v_number, tv);
695 channel_send(idx, json, "eval");
696 vim_free(json);
697 }
698 free_tv(tv);
699 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100700 }
701 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100702 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100703}
704
705/*
706 * Invoke a callback for channel "idx" if needed.
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100707 * Return OK when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100708 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100709 static int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100710may_invoke_callback(int idx)
711{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100712 char_u *msg = NULL;
713 typval_T *listtv = NULL;
714 list_T *list;
715 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100716 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100717 int seq_nr = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100718 int json_mode = channels[idx].ch_json_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100719
720 if (channel_peek(idx) == NULL)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100721 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100722 if (channels[idx].ch_close_cb != NULL)
723 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100724 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100725
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100726 if (json_mode)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100727 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100728 /* Get any json message. Return if there isn't one. */
729 channel_read_json(idx);
730 if (channel_get_json(idx, -1, &listtv) == FAIL)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100731 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100732 if (listtv->v_type != VAR_LIST)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100733 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100734 /* TODO: give error */
735 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100736 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100737 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100738
739 list = listtv->vval.v_list;
740 if (list->lv_len < 2)
741 {
742 /* TODO: give error */
743 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100744 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100745 }
746
747 argv[1] = list->lv_first->li_next->li_tv;
748 typetv = &list->lv_first->li_tv;
749 if (typetv->v_type == VAR_STRING)
750 {
751 typval_T *arg3 = NULL;
752 char_u *cmd = typetv->vval.v_string;
753
754 /* ["cmd", arg] */
755 if (list->lv_len == 3)
756 arg3 = &list->lv_last->li_tv;
757 channel_exe_cmd(idx, cmd, &argv[1], arg3);
758 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100759 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100760 }
761
762 if (typetv->v_type != VAR_NUMBER)
763 {
764 /* TODO: give error */
765 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100766 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100767 }
768 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100769 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100770 else
771 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100772 /* For a raw channel we don't know where the message ends, just get
773 * everything. */
774 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100775 argv[1].v_type = VAR_STRING;
776 argv[1].vval.v_string = msg;
777 }
778
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100779 if (channels[idx].ch_req_callback != NULL && seq_nr != 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100780 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100781 /* TODO: check the sequence number */
782 /* invoke the one-time callback */
783 invoke_callback(idx, channels[idx].ch_req_callback, argv);
784 channels[idx].ch_req_callback = NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100785 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100786 else if (channels[idx].ch_callback != NULL)
787 {
788 /* invoke the channel callback */
789 invoke_callback(idx, channels[idx].ch_callback, argv);
790 }
791 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100792
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100793 if (listtv != NULL)
794 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100795 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100796
797 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100798}
799
800/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100801 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100802 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +0100803 */
804 int
805channel_is_open(int idx)
806{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100807 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100808}
809
810/*
811 * Close channel "idx".
812 * This does not trigger the close callback.
813 */
814 void
815channel_close(int idx)
816{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100817 channel_T *channel = &channels[idx];
818 jsonq_T *jhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100819
820 if (channel->ch_fd >= 0)
821 {
822 sock_close(channel->ch_fd);
823 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100824 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100825#ifdef FEAT_GUI
826 channel_gui_unregister(idx);
827#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100828 vim_free(channel->ch_callback);
829 channel->ch_callback = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100830
831 while (channel_peek(idx) != NULL)
832 vim_free(channel_get(idx));
833
834 jhead = &channel->ch_json_head;
835 while (jhead->next != jhead)
836 {
837 clear_tv(jhead->next->value);
838 remove_json_node(jhead->next);
839 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100840 }
841}
842
Bram Moolenaard04a0202016-01-26 23:30:18 +0100843/*
844 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +0100845 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100846 */
Bram Moolenaar83162462016-01-28 23:10:07 +0100847 int
Bram Moolenaard04a0202016-01-26 23:30:18 +0100848channel_save(int idx, char_u *buf, int len)
849{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100850 readq_T *node;
851 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100852
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100853 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100854 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +0100855 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100856 node->buffer = alloc(len + 1);
857 if (node->buffer == NULL)
858 {
859 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +0100860 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100861 }
862 mch_memmove(node->buffer, buf, (size_t)len);
863 node->buffer[len] = NUL;
864
Bram Moolenaard04a0202016-01-26 23:30:18 +0100865 /* insert node at tail of queue */
866 node->next = head;
867 node->prev = head->prev;
868 head->prev->next = node;
869 head->prev = node;
870
871 if (debugfd != NULL)
872 {
873 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +0100874 if (fwrite(buf, len, 1, debugfd) != 1)
875 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100876 fprintf(debugfd, "\n");
877 }
Bram Moolenaar83162462016-01-28 23:10:07 +0100878 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100879}
880
881/*
882 * Return the first buffer from the channel without removing it.
883 * Returns NULL if there is nothing.
884 */
885 char_u *
886channel_peek(int idx)
887{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100888 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100889
890 if (head->next == head || head->next == NULL)
891 return NULL;
892 return head->next->buffer;
893}
894
895/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100896 * Clear the read buffer on channel "idx".
897 */
898 void
899channel_clear(int idx)
900{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100901 readq_T *head = &channels[idx].ch_head;
902 readq_T *node = head->next;
903 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100904
905 while (node != NULL && node != head)
906 {
907 next = node->next;
908 vim_free(node->buffer);
909 vim_free(node);
910 if (next == head)
911 {
912 head->next = head;
913 head->prev = head;
914 break;
915 }
916 node = next;
917 }
918}
919
920/* Sent when the channel is found closed when reading. */
921#define DETACH_MSG "\"DETACH\"\n"
922
923/* Buffer size for reading incoming messages. */
924#define MAXMSGSIZE 4096
925
926/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100927 * Check for reading from "fd" with "timeout" msec.
928 * Return FAIL when there is nothing to read.
929 */
930 static int
931channel_wait(int fd, int timeout)
932{
933#ifdef HAVE_SELECT
934 struct timeval tval;
935 fd_set rfds;
936 int ret;
937
938 FD_ZERO(&rfds);
939 FD_SET(fd, &rfds);
940 tval.tv_sec = timeout / 1000;
941 tval.tv_usec = (timeout % 1000) * 1000;
942 for (;;)
943 {
944 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
945# ifdef EINTR
946 if (ret == -1 && errno == EINTR)
947 continue;
948# endif
949 if (ret <= 0)
950 return FAIL;
951 break;
952 }
953#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +0100954# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100955 struct pollfd fds;
956
957 fds.fd = fd;
958 fds.events = POLLIN;
959 if (poll(&fds, 1, timeout) <= 0)
960 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +0100961# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100962#endif
963 return OK;
964}
965
966/*
967 * Return a unique ID to be used in a message.
968 */
969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100970channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100971{
972 static int next_id = 1;
973
974 return next_id++;
975}
976
977/*
978 * Read from channel "idx" for as long as there is something to read.
979 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100980 */
981 void
982channel_read(int idx)
983{
984 static char_u *buf = NULL;
985 int len = 0;
986 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100987 channel_T *channel = &channels[idx];
988
989 if (channel->ch_fd < 0)
990 {
991 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
992 return;
993 }
994
995 /* Allocate a buffer to read into. */
996 if (buf == NULL)
997 {
998 buf = alloc(MAXMSGSIZE);
999 if (buf == NULL)
1000 return; /* out of memory! */
1001 }
1002
1003 /* Keep on reading for as long as there is something to read.
1004 * Use select() or poll() to avoid blocking on a message that is exactly
1005 * MAXMSGSIZE long. */
1006 for (;;)
1007 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001008 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001009 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001010 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1011 if (len <= 0)
1012 break; /* error or nothing more to read */
1013
1014 /* Store the read message in the queue. */
1015 channel_save(idx, buf, len);
1016 readlen += len;
1017 if (len < MAXMSGSIZE)
1018 break; /* did read everything that's available */
1019 }
1020
1021 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1022 if (readlen <= 0)
1023 {
1024 /* Queue a "DETACH" netbeans message in the command queue in order to
1025 * terminate the netbeans session later. Do not end the session here
1026 * directly as we may be running in the context of a call to
1027 * netbeans_parse_messages():
1028 * netbeans_parse_messages
1029 * -> autocmd triggered while processing the netbeans cmd
1030 * -> ui_breakcheck
1031 * -> gui event loop or select loop
1032 * -> channel_read()
1033 */
1034 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1035
1036 channel_close(idx);
1037 if (channel->ch_close_cb != NULL)
1038 (*channel->ch_close_cb)();
1039
1040 if (len < 0)
1041 {
1042 /* Todo: which channel? */
1043 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001044 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001045 }
1046 }
1047
1048#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1049 if (CH_HAS_GUI && gtk_main_level() > 0)
1050 gtk_main_quit();
1051#endif
1052}
1053
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001054/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001055 * Read from raw channel "idx". Blocks until there is something to read or
1056 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001057 * Returns what was read in allocated memory.
1058 * Returns NULL in case of error or timeout.
1059 */
1060 char_u *
1061channel_read_block(int idx)
1062{
1063 if (channel_peek(idx) == NULL)
1064 {
1065 /* Wait for up to 2 seconds.
1066 * TODO: use timeout set on the channel. */
1067 if (channel_wait(channels[idx].ch_fd, 2000) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001068 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001069 channel_read(idx);
1070 }
1071
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001072 return channel_get_all(idx);
1073}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001074
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001075/*
1076 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1077 * result in "rettv".
1078 * Blocks until the message is received.
1079 */
1080 int
1081channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1082{
1083 for (;;)
1084 {
1085 channel_read_json(ch_idx);
1086
1087 /* search for messsage "id" */
1088 if (channel_get_json(ch_idx, id, rettv) == OK)
1089 return OK;
1090
1091 /* Wait for up to 2 seconds.
1092 * TODO: use timeout set on the channel. */
1093 if (channel_wait(channels[ch_idx].ch_fd, 2000) == FAIL)
1094 break;
1095 channel_read(ch_idx);
1096 }
1097 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001098}
1099
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001100# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001101/*
1102 * Lookup the channel index from the socket.
1103 * Returns -1 when the socket isn't found.
1104 */
1105 int
1106channel_socket2idx(sock_T fd)
1107{
1108 int i;
1109
1110 if (fd >= 0)
1111 for (i = 0; i < channel_count; ++i)
1112 if (channels[i].ch_fd == fd)
1113 return i;
1114 return -1;
1115}
1116# endif
1117
Bram Moolenaard04a0202016-01-26 23:30:18 +01001118/*
1119 * Write "buf" (NUL terminated string) to channel "idx".
1120 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001121 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001122 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001123 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001124channel_send(int idx, char_u *buf, char *fun)
1125{
1126 channel_T *channel = &channels[idx];
1127 int len = (int)STRLEN(buf);
1128
1129 if (channel->ch_fd < 0)
1130 {
1131 if (!channel->ch_error && fun != NULL)
1132 {
1133 CHERROR(" %s(): write while not connected\n", fun);
1134 EMSG2("E630: %s(): write while not connected", 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 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001141 {
1142 if (!channel->ch_error && fun != NULL)
1143 {
1144 CHERROR(" %s(): write failed\n", fun);
1145 EMSG2("E631: %s(): write failed", fun);
1146 }
1147 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001148 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001149 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001150
1151 channel->ch_error = FALSE;
1152 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001153}
1154
1155# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001156/*
1157 * Add open channels to the poll struct.
1158 * Return the adjusted struct index.
1159 * The type of "fds" is hidden to avoid problems with the function proto.
1160 */
1161 int
1162channel_poll_setup(int nfd_in, void *fds_in)
1163{
1164 int nfd = nfd_in;
1165 int i;
1166 struct pollfd *fds = fds_in;
1167
1168 for (i = 0; i < channel_count; ++i)
1169 if (channels[i].ch_fd >= 0)
1170 {
1171 channels[i].ch_idx = nfd;
1172 fds[nfd].fd = channels[i].ch_fd;
1173 fds[nfd].events = POLLIN;
1174 nfd++;
1175 }
1176 else
1177 channels[i].ch_idx = -1;
1178
1179 return nfd;
1180}
1181
1182/*
1183 * The type of "fds" is hidden to avoid problems with the function proto.
1184 */
1185 int
1186channel_poll_check(int ret_in, void *fds_in)
1187{
1188 int ret = ret_in;
1189 int i;
1190 struct pollfd *fds = fds_in;
1191
1192 for (i = 0; i < channel_count; ++i)
1193 if (ret > 0 && channels[i].ch_idx != -1
1194 && fds[channels[i].ch_idx].revents & POLLIN)
1195 {
1196 channel_read(i);
1197 --ret;
1198 }
1199
1200 return ret;
1201}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001202# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001203
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001204# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001205/*
1206 * The type of "rfds" is hidden to avoid problems with the function proto.
1207 */
1208 int
1209channel_select_setup(int maxfd_in, void *rfds_in)
1210{
1211 int maxfd = maxfd_in;
1212 int i;
1213 fd_set *rfds = rfds_in;
1214
1215 for (i = 0; i < channel_count; ++i)
1216 if (channels[i].ch_fd >= 0)
1217 {
1218 FD_SET(channels[i].ch_fd, rfds);
1219 if (maxfd < channels[i].ch_fd)
1220 maxfd = channels[i].ch_fd;
1221 }
1222
1223 return maxfd;
1224}
1225
1226/*
1227 * The type of "rfds" is hidden to avoid problems with the function proto.
1228 */
1229 int
1230channel_select_check(int ret_in, void *rfds_in)
1231{
1232 int ret = ret_in;
1233 int i;
1234 fd_set *rfds = rfds_in;
1235
1236 for (i = 0; i < channel_count; ++i)
1237 if (ret > 0 && channels[i].ch_fd >= 0
1238 && FD_ISSET(channels[i].ch_fd, rfds))
1239 {
1240 channel_read(i);
1241 --ret;
1242 }
1243
1244 return ret;
1245}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001246# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001247
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001248/*
1249 * Invoked from the main loop when it's save to execute received commands.
1250 */
1251 void
1252channel_parse_messages(void)
1253{
1254 int i;
1255
1256 for (i = 0; i < channel_count; ++i)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001257 while (may_invoke_callback(i) == OK)
1258 ;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001259}
1260
Bram Moolenaare0874f82016-01-24 20:36:41 +01001261#endif /* FEAT_CHANNEL */