blob: af282cdae4a0116853bae7112cd41620868d8e89 [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
Bram Moolenaar4d919d72016-02-05 22:36:41 +010045# undef EWOULDBLOCK
46# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard04a0202016-01-26 23:30:18 +010047# ifdef EINTR
48# undef EINTR
49# endif
50# define EINTR WSAEINTR
51# define sock_write(sd, buf, len) send(sd, buf, len, 0)
52# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
53# define sock_close(sd) closesocket(sd)
54# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
55#else
56# include <netdb.h>
57# include <netinet/in.h>
58
59# include <sys/socket.h>
60# ifdef HAVE_LIBGEN_H
61# include <libgen.h>
62# endif
63# define SOCK_ERRNO
64# define sock_write(sd, buf, len) write(sd, buf, len)
65# define sock_read(sd, buf, len) read(sd, buf, len)
66# define sock_close(sd) close(sd)
67#endif
68
69#ifdef FEAT_GUI_W32
70extern HWND s_hwnd; /* Gvim's Window handle */
71#endif
72
73struct readqueue
74{
75 char_u *buffer;
76 struct readqueue *next;
77 struct readqueue *prev;
78};
Bram Moolenaar19d2f152016-02-01 21:38:19 +010079typedef struct readqueue readq_T;
80
81struct jsonqueue
82{
83 typval_T *value;
84 struct jsonqueue *next;
85 struct jsonqueue *prev;
86};
87typedef struct jsonqueue jsonq_T;
Bram Moolenaard04a0202016-01-26 23:30:18 +010088
Bram Moolenaara07fec92016-02-05 21:04:08 +010089struct cbqueue
90{
91 char_u *callback;
92 int seq_nr;
93 struct cbqueue *next;
94 struct cbqueue *prev;
95};
96typedef struct cbqueue cbq_T;
97
Bram Moolenaare0874f82016-01-24 20:36:41 +010098typedef struct {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010099 sock_T ch_fd; /* the socket, -1 for a closed channel */
100 int ch_idx; /* used by channel_poll_setup() */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100101 readq_T ch_head; /* dummy node, header for circular queue */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100102
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100103 int ch_error; /* When TRUE an error was reported. Avoids giving
Bram Moolenaard04a0202016-01-26 23:30:18 +0100104 * pages full of error messages when the other side
105 * has exited, only mention the first error until the
106 * connection works again. */
107#ifdef FEAT_GUI_X11
108 XtInputId ch_inputHandler; /* Cookie for input */
109#endif
110#ifdef FEAT_GUI_GTK
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100111 gint ch_inputHandler; /* Cookie for input */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100112#endif
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100113#ifdef WIN32
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100114 int ch_inputHandler; /* simply ret.value of WSAAsyncSelect() */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100115#endif
116
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100117 void (*ch_close_cb)(void); /* callback for when channel is closed */
118
Bram Moolenaare56bf152016-02-08 23:23:42 +0100119 int ch_block_id; /* ID that channel_read_json_block() is
120 waiting for */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100121 char_u *ch_callback; /* function to call when a msg is not handled */
Bram Moolenaara07fec92016-02-05 21:04:08 +0100122 cbq_T ch_cb_head; /* dummy node for pre-request callbacks */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100123
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100124 ch_mode_T ch_mode;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100125 jsonq_T ch_json_head; /* dummy node, header for circular queue */
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100126
127 int ch_timeout; /* request timeout in msec */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100128} channel_T;
129
Bram Moolenaard04a0202016-01-26 23:30:18 +0100130/*
131 * Information about all channels.
132 * There can be gaps for closed channels, they will be reused later.
133 */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100134static channel_T *channels = NULL;
135static int channel_count = 0;
136
137/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100138 * TODO: open debug file when desired.
139 */
140FILE *debugfd = NULL;
141
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100142#ifdef _WIN32
143# undef PERROR
144# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
145 (char_u *)msg, (char_u *)strerror_win32(errno))
146
147 static char *
148strerror_win32(int eno)
149{
150 static LPVOID msgbuf = NULL;
151 char_u *ptr;
152
153 if (msgbuf)
154 LocalFree(msgbuf);
155 FormatMessage(
156 FORMAT_MESSAGE_ALLOCATE_BUFFER |
157 FORMAT_MESSAGE_FROM_SYSTEM |
158 FORMAT_MESSAGE_IGNORE_INSERTS,
159 NULL,
160 eno,
161 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
162 (LPTSTR) &msgbuf,
163 0,
164 NULL);
165 /* chomp \r or \n */
166 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
167 switch (*ptr)
168 {
169 case '\r':
170 STRMOVE(ptr, ptr + 1);
171 ptr--;
172 break;
173 case '\n':
174 if (*(ptr + 1) == '\0')
175 *ptr = '\0';
176 else
177 *ptr = ' ';
178 break;
179 }
180 return msgbuf;
181}
182#endif
183
Bram Moolenaard04a0202016-01-26 23:30:18 +0100184/*
Bram Moolenaare0874f82016-01-24 20:36:41 +0100185 * Add a new channel slot, return the index.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100186 * The channel isn't actually used into ch_fd is set >= 0;
187 * Returns -1 if all channels are in use.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100188 */
189 static int
190add_channel(void)
191{
192 int idx;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100193 channel_T *ch;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100194
195 if (channels != NULL)
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100196 {
Bram Moolenaare0874f82016-01-24 20:36:41 +0100197 for (idx = 0; idx < channel_count; ++idx)
198 if (channels[idx].ch_fd < 0)
199 /* re-use a closed channel slot */
200 return idx;
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100201 if (channel_count == MAX_OPEN_CHANNELS)
202 return -1;
203 }
204 else
205 {
206 channels = (channel_T *)alloc((int)sizeof(channel_T)
207 * MAX_OPEN_CHANNELS);
208 if (channels == NULL)
209 return -1;
210 }
211
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100212 ch = &channels[channel_count];
213 (void)vim_memset(ch, 0, sizeof(channel_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100214
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100215 ch->ch_fd = (sock_T)-1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100216#ifdef FEAT_GUI_X11
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100217 ch->ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100218#endif
219#ifdef FEAT_GUI_GTK
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100220 ch->ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100221#endif
222#ifdef FEAT_GUI_W32
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100223 ch->ch_inputHandler = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100224#endif
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100225 /* initialize circular queues */
226 ch->ch_head.next = &ch->ch_head;
227 ch->ch_head.prev = &ch->ch_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +0100228 ch->ch_cb_head.next = &ch->ch_cb_head;
229 ch->ch_cb_head.prev = &ch->ch_cb_head;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100230 ch->ch_json_head.next = &ch->ch_json_head;
231 ch->ch_json_head.prev = &ch->ch_json_head;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100232
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100233 ch->ch_timeout = 2000;
234
Bram Moolenaare0874f82016-01-24 20:36:41 +0100235 return channel_count++;
236}
237
Bram Moolenaard04a0202016-01-26 23:30:18 +0100238#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100239/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100240 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100241 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100242#ifdef FEAT_GUI_X11
243 static void
244messageFromNetbeans(XtPointer clientData,
245 int *unused1 UNUSED,
246 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100247{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100248 channel_read((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100249}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100250#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100251
Bram Moolenaard04a0202016-01-26 23:30:18 +0100252#ifdef FEAT_GUI_GTK
253 static void
254messageFromNetbeans(gpointer clientData,
255 gint unused1 UNUSED,
256 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100257{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100258 channel_read((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100259}
260#endif
261
262 static void
Bram Moolenaard04a0202016-01-26 23:30:18 +0100263channel_gui_register(int idx)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100264{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100265 channel_T *channel = &channels[idx];
266
267 if (!CH_HAS_GUI)
268 return;
269
270# ifdef FEAT_GUI_X11
271 /* tell notifier we are interested in being called
272 * when there is input on the editor connection socket
273 */
274 if (channel->ch_inputHandler == (XtInputId)NULL)
275 channel->ch_inputHandler =
276 XtAppAddInput((XtAppContext)app_context, channel->ch_fd,
277 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100278 messageFromNetbeans, (XtPointer)(long)idx);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100279# else
280# ifdef FEAT_GUI_GTK
281 /*
282 * Tell gdk we are interested in being called when there
283 * is input on the editor connection socket
284 */
285 if (channel->ch_inputHandler == 0)
286 channel->ch_inputHandler =
287 gdk_input_add((gint)channel->ch_fd, (GdkInputCondition)
288 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
289 messageFromNetbeans, (gpointer)(long)idx);
290# else
291# ifdef FEAT_GUI_W32
292 /*
293 * Tell Windows we are interested in receiving message when there
294 * is input on the editor connection socket.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100295 */
296 if (channel->ch_inputHandler == -1)
297 channel->ch_inputHandler =
298 WSAAsyncSelect(channel->ch_fd, s_hwnd, WM_NETBEANS, FD_READ);
299# endif
300# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100301# endif
Bram Moolenaard04a0202016-01-26 23:30:18 +0100302}
303
304/*
305 * Register any of our file descriptors with the GUI event handling system.
306 * Called when the GUI has started.
307 */
308 void
309channel_gui_register_all(void)
310{
311 int i;
312
313 for (i = 0; i < channel_count; ++i)
314 if (channels[i].ch_fd >= 0)
315 channel_gui_register(i);
316}
317
318 static void
319channel_gui_unregister(int idx)
320{
321 channel_T *channel = &channels[idx];
322
323# ifdef FEAT_GUI_X11
324 if (channel->ch_inputHandler != (XtInputId)NULL)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100325 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100326 XtRemoveInput(channel->ch_inputHandler);
327 channel->ch_inputHandler = (XtInputId)NULL;
328 }
329# else
330# ifdef FEAT_GUI_GTK
331 if (channel->ch_inputHandler != 0)
332 {
333 gdk_input_remove(channel->ch_inputHandler);
334 channel->ch_inputHandler = 0;
335 }
336# else
337# ifdef FEAT_GUI_W32
338 if (channel->ch_inputHandler == 0)
339 {
Bram Moolenaar54e09e72016-01-26 23:49:31 +0100340 WSAAsyncSelect(channel->ch_fd, s_hwnd, 0, 0);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100341 channel->ch_inputHandler = -1;
342 }
343# endif
344# endif
345# endif
346}
347
348#endif
349
350/*
351 * Open a channel to "hostname":"port".
352 * Returns the channel number for success.
353 * Returns a negative number for failure.
354 */
355 int
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100356channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100357{
358 int sd;
359 struct sockaddr_in server;
360 struct hostent * host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100361#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100362 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100363 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100364#else
365 int port = port_in;
366#endif
367 int idx;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100368 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100369
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100370#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100371 channel_init_winsock();
372#endif
373
374 idx = add_channel();
375 if (idx < 0)
376 {
377 CHERROR("All channels are in use\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100378 EMSG(_("E897: All channels are in use"));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100379 return -1;
380 }
381
382 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
383 {
384 CHERROR("error in socket() in channel_open()\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100385 PERROR("E898: socket() in channel_open()");
Bram Moolenaard04a0202016-01-26 23:30:18 +0100386 return -1;
387 }
388
389 /* Get the server internet address and put into addr structure */
390 /* fill in the socket address structure and connect to server */
391 vim_memset((char *)&server, 0, sizeof(server));
392 server.sin_family = AF_INET;
393 server.sin_port = htons(port);
394 if ((host = gethostbyname(hostname)) == NULL)
395 {
396 CHERROR("error in gethostbyname() in channel_open()\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100397 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaard04a0202016-01-26 23:30:18 +0100398 sock_close(sd);
399 return -1;
400 }
401 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
402
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100403 if (waittime >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100404 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100405 /* Make connect non-blocking. */
406 if (
407#ifdef _WIN32
408 ioctlsocket(sd, FIONBIO, &val) < 0
409#else
410 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
411#endif
412 )
Bram Moolenaard04a0202016-01-26 23:30:18 +0100413 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100414 SOCK_ERRNO;
415 CHERROR("channel_open: Connect failed with errno %d\n", errno);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100416 sock_close(sd);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100417 return -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100418 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100419 }
420
421 /* Try connecting to the server. */
422 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
423 SOCK_ERRNO;
424 if (ret < 0)
425 {
426 if (errno != EWOULDBLOCK && errno != EINPROGRESS)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100427 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100428 CHERROR("channel_open: Connect failed with errno %d\n", errno);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100429 CHERROR("Cannot connect to port\n", "");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100430 PERROR(_("E902: Cannot connect to port"));
Bram Moolenaard04a0202016-01-26 23:30:18 +0100431 sock_close(sd);
432 return -1;
433 }
434 }
435
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100436 if (waittime >= 0 && ret < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100437 {
438 struct timeval tv;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100439 fd_set wfds;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100440
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100441 FD_ZERO(&wfds);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100442 FD_SET(sd, &wfds);
Bram Moolenaar26dfc412016-02-06 18:18:54 +0100443 tv.tv_sec = waittime / 1000;
444 tv.tv_usec = (waittime % 1000) * 1000;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100445 ret = select((int)sd + 1, NULL, &wfds, NULL, &tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100446 if (ret < 0)
447 {
448 SOCK_ERRNO;
449 CHERROR("channel_open: Connect failed with errno %d\n", errno);
450 CHERROR("Cannot connect to port\n", "");
451 PERROR(_("E902: Cannot connect to port"));
452 sock_close(sd);
453 return -1;
454 }
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100455 if (!FD_ISSET(sd, &wfds))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100456 {
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100457 /* don't give an error, we just timed out. */
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100458 sock_close(sd);
459 return -1;
460 }
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100461 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100462
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100463 if (waittime >= 0)
464 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100465#ifdef _WIN32
466 val = 0;
467 ioctlsocket(sd, FIONBIO, &val);
468#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100469 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100470#endif
471 }
472
Bram Moolenaar0fa98e72016-02-07 22:21:19 +0100473 /* Only retry for netbeans. TODO: can we use a waittime instead? */
474 if (errno == ECONNREFUSED && close_cb != NULL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100475 {
476 sock_close(sd);
477 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
478 {
479 SOCK_ERRNO;
480 CHERROR("socket() retry in channel_open()\n", "");
481 PERROR("E900: socket() retry in channel_open()");
482 return -1;
483 }
484 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
485 {
486 int retries = 36;
487 int success = FALSE;
488
489 SOCK_ERRNO;
490 while (retries-- && ((errno == ECONNREFUSED)
491 || (errno == EINTR)))
492 {
493 CHERROR("retrying...\n", "");
494 mch_delay(3000L, TRUE);
495 ui_breakcheck();
496 if (got_int)
497 {
498 errno = EINTR;
499 break;
500 }
501 if (connect(sd, (struct sockaddr *)&server,
502 sizeof(server)) == 0)
503 {
504 success = TRUE;
505 break;
506 }
507 SOCK_ERRNO;
508 }
509 if (!success)
510 {
511 /* Get here when the server can't be found. */
512 CHERROR("Cannot connect to port after retry\n", "");
513 PERROR(_("E899: Cannot connect to port after retry2"));
514 sock_close(sd);
515 return -1;
516 }
517 }
518 }
519
Bram Moolenaard04a0202016-01-26 23:30:18 +0100520 channels[idx].ch_fd = sd;
521 channels[idx].ch_close_cb = close_cb;
522
523#ifdef FEAT_GUI
524 channel_gui_register(idx);
525#endif
526
527 return idx;
528}
529
530/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100531 * Set the json mode of channel "idx" to "ch_mode".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100532 */
533 void
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100534channel_set_json_mode(int idx, ch_mode_T ch_mode)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100535{
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100536 channels[idx].ch_mode = ch_mode;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100537}
538
539/*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100540 * Set the read timeout of channel "idx".
541 */
542 void
543channel_set_timeout(int idx, int timeout)
544{
545 channels[idx].ch_timeout = timeout;
546}
547
548/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100549 * Set the callback for channel "idx".
550 */
551 void
552channel_set_callback(int idx, char_u *callback)
553{
554 vim_free(channels[idx].ch_callback);
555 channels[idx].ch_callback = vim_strsave(callback);
556}
557
558/*
Bram Moolenaara07fec92016-02-05 21:04:08 +0100559 * Set the callback for channel "idx" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100560 */
561 void
Bram Moolenaara07fec92016-02-05 21:04:08 +0100562channel_set_req_callback(int idx, char_u *callback, int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100563{
Bram Moolenaara07fec92016-02-05 21:04:08 +0100564 cbq_T *cbhead = &channels[idx].ch_cb_head;
565 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
566
567 if (item != NULL)
568 {
569 item->callback = vim_strsave(callback);
570 item->seq_nr = id;
571 item->prev = cbhead->prev;
572 cbhead->prev = item;
573 item->next = cbhead;
574 item->prev->next = item;
575 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100576}
577
578/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100579 * Invoke the "callback" on channel "idx".
580 */
581 static void
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100582invoke_callback(int idx, char_u *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100583{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100584 typval_T rettv;
585 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100586
587 argv[0].v_type = VAR_NUMBER;
588 argv[0].vval.v_number = idx;
589
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100590 call_func(callback, (int)STRLEN(callback),
591 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
592 /* If an echo command was used the cursor needs to be put back where
593 * it belongs. */
594 setcursor();
595 cursor_on();
596 out_flush();
597}
598
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100599/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100600 * Return the first buffer from the channel and remove it.
601 * The caller must free it.
602 * Returns NULL if there is nothing.
603 */
604 char_u *
605channel_get(int idx)
606{
607 readq_T *head = &channels[idx].ch_head;
608 readq_T *node;
609 char_u *p;
610
611 if (head->next == head || head->next == NULL)
612 return NULL;
613 node = head->next;
614 /* dispose of the node but keep the buffer */
615 p = node->buffer;
616 head->next = node->next;
617 node->next->prev = node->prev;
618 vim_free(node);
619 return p;
620}
621
622/*
623 * Returns the whole buffer contents concatenated.
624 */
625 static char_u *
626channel_get_all(int idx)
627{
628 /* Concatenate everything into one buffer.
629 * TODO: avoid multiple allocations. */
630 while (channel_collapse(idx) == OK)
631 ;
632 return channel_get(idx);
633}
634
635/*
636 * Collapses the first and second buffer in the channel "idx".
637 * Returns FAIL if that is not possible.
638 */
639 int
640channel_collapse(int idx)
641{
642 readq_T *head = &channels[idx].ch_head;
643 readq_T *node = head->next;
644 char_u *p;
645
646 if (node == head || node == NULL || node->next == head)
647 return FAIL;
648
649 p = alloc((unsigned)(STRLEN(node->buffer)
650 + STRLEN(node->next->buffer) + 1));
651 if (p == NULL)
652 return FAIL; /* out of memory */
653 STRCPY(p, node->buffer);
654 STRCAT(p, node->next->buffer);
655 vim_free(node->next->buffer);
656 node->next->buffer = p;
657
658 /* dispose of the node and buffer */
659 head->next = node->next;
660 node->next->prev = node->prev;
661 vim_free(node->buffer);
662 vim_free(node);
663 return OK;
664}
665
666/*
Bram Moolenaard7ece102016-02-02 23:23:02 +0100667 * Use the read buffer of channel "ch_idx" and parse a JSON messages that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100668 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +0100669 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100670 */
Bram Moolenaard7ece102016-02-02 23:23:02 +0100671 static int
672channel_parse_json(int ch_idx)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100673{
674 js_read_T reader;
675 typval_T listtv;
676 jsonq_T *item;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100677 channel_T *channel = &channels[ch_idx];
678 jsonq_T *head = &channel->ch_json_head;
Bram Moolenaard7ece102016-02-02 23:23:02 +0100679 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100680
681 if (channel_peek(ch_idx) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100682 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100683
684 /* TODO: make reader work properly */
685 /* reader.js_buf = channel_peek(ch_idx); */
686 reader.js_buf = channel_get_all(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100687 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100688 reader.js_fill = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100689 /* reader.js_fill = channel_fill; */
690 reader.js_cookie = &ch_idx;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100691 ret = json_decode(&reader, &listtv,
692 channel->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100693 if (ret == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100694 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100695 /* Only accept the response when it is a list with at least two
696 * items. */
697 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100698 {
699 /* TODO: give error */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100700 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100701 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100702 else
703 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100704 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
705 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100706 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100707 else
708 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100709 item->value = alloc_tv();
710 if (item->value == NULL)
711 {
712 vim_free(item);
713 clear_tv(&listtv);
714 }
715 else
716 {
717 *item->value = listtv;
718 item->prev = head->prev;
719 head->prev = item;
720 item->next = head;
721 item->prev->next = item;
722 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100723 }
724 }
725 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100726
727 /* Put the unread part back into the channel.
728 * TODO: insert in front */
729 if (reader.js_buf[reader.js_used] != NUL)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100730 {
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100731 channel_save(ch_idx, reader.js_buf + reader.js_used,
732 (int)(reader.js_end - reader.js_buf) - reader.js_used);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100733 ret = TRUE;
734 }
735 else
736 ret = FALSE;
737
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100738 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +0100739 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100740}
741
742/*
743 * Remove "node" from the queue that it is in and free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +0100744 * Also frees the contained callback name.
745 */
746 static void
747remove_cb_node(cbq_T *node)
748{
749 node->prev->next = node->next;
750 node->next->prev = node->prev;
751 vim_free(node->callback);
752 vim_free(node);
753}
754
755/*
756 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100757 * Caller should have freed or used node->value.
758 */
759 static void
760remove_json_node(jsonq_T *node)
761{
762 node->prev->next = node->next;
763 node->next->prev = node->prev;
764 vim_free(node);
765}
766
767/*
768 * Get a message from the JSON queue for channel "ch_idx".
769 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +0100770 * When "id" is zero or negative jut get the first message. But not the one
771 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100772 * Return OK when found and return the value in "rettv".
773 * Return FAIL otherwise.
774 */
775 static int
776channel_get_json(int ch_idx, int id, typval_T **rettv)
777{
Bram Moolenaare56bf152016-02-08 23:23:42 +0100778 channel_T *channel = &channels[ch_idx];
779 jsonq_T *head = &channel->ch_json_head;
780 jsonq_T *item = head->next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100781
782 while (item != head)
783 {
784 list_T *l = item->value->vval.v_list;
785 typval_T *tv = &l->lv_first->li_tv;
786
787 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +0100788 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaarf6157282016-02-10 21:07:14 +0100789 || tv->vval.v_number == 0
Bram Moolenaare56bf152016-02-08 23:23:42 +0100790 || tv->vval.v_number != channel->ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100791 {
792 *rettv = item->value;
793 remove_json_node(item);
794 return OK;
795 }
796 item = item->next;
797 }
798 return FAIL;
799}
800
801/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100802 * Execute a command received over channel "idx".
803 * "cmd" is the command string, "arg2" the second argument.
804 * "arg3" is the third argument, NULL if missing.
805 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100806 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100807channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100808{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100809 char_u *arg;
810
811 if (arg2->v_type != VAR_STRING)
812 {
813 if (p_verbose > 2)
814 EMSG("E903: received ex command with non-string argument");
815 return;
816 }
817 arg = arg2->vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100818 if (arg == NULL)
819 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100820
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100821 if (STRCMP(cmd, "ex") == 0)
822 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100823 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100824 }
825 else if (STRCMP(cmd, "normal") == 0)
826 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100827 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100828
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100829 ea.arg = arg;
830 ea.addr_count = 0;
831 ea.forceit = TRUE; /* no mapping */
832 ex_normal(&ea);
833 }
834 else if (STRCMP(cmd, "redraw") == 0)
835 {
836 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100837
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100838 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100839 ex_redraw(&ea);
840 showruler(FALSE);
841 setcursor();
842 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100843#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100844 if (gui.in_use)
845 {
846 gui_update_cursor(FALSE, FALSE);
847 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100848 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100849#endif
850 }
851 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
852 {
853 int is_eval = cmd[1] == 'v';
854
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100855 if (is_eval && (arg3 == NULL || arg3->v_type != VAR_NUMBER))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100856 {
857 if (p_verbose > 2)
858 EMSG("E904: third argument for eval must be a number");
859 }
860 else
861 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100862 typval_T *tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100863 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +0100864 char_u *json = NULL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100865 channel_T *channel = &channels[idx];
866 int options = channel->ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100867
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100868 /* Don't pollute the display with errors. */
869 ++emsg_skip;
870 tv = eval_expr(arg, NULL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100871 if (is_eval)
872 {
Bram Moolenaar55fab432016-02-07 16:53:13 +0100873 if (tv != NULL)
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100874 json = json_encode_nr_expr(arg3->vval.v_number, tv,
875 options);
Bram Moolenaar55fab432016-02-07 16:53:13 +0100876 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100877 {
Bram Moolenaar55fab432016-02-07 16:53:13 +0100878 /* If evaluation failed or the result can't be encoded
879 * then return the string "ERROR". */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100880 err_tv.v_type = VAR_STRING;
881 err_tv.vval.v_string = (char_u *)"ERROR";
882 tv = &err_tv;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100883 json = json_encode_nr_expr(arg3->vval.v_number, tv,
884 options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100885 }
Bram Moolenaar55fab432016-02-07 16:53:13 +0100886 if (json != NULL)
887 {
888 channel_send(idx, json, "eval");
889 vim_free(json);
890 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100891 }
Bram Moolenaar55fab432016-02-07 16:53:13 +0100892 --emsg_skip;
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100893 if (tv != &err_tv)
894 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100895 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100896 }
897 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100898 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100899}
900
901/*
902 * Invoke a callback for channel "idx" if needed.
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100903 * Return OK when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100904 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100905 static int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100906may_invoke_callback(int idx)
907{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100908 char_u *msg = NULL;
909 typval_T *listtv = NULL;
910 list_T *list;
911 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100912 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100913 int seq_nr = -1;
Bram Moolenaara07fec92016-02-05 21:04:08 +0100914 channel_T *channel = &channels[idx];
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100915 ch_mode_T ch_mode = channel->ch_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100916
Bram Moolenaara07fec92016-02-05 21:04:08 +0100917 if (channel->ch_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100918 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100919 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100920
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100921 if (ch_mode != MODE_RAW)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100922 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100923 /* Get any json message in the queue. */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100924 if (channel_get_json(idx, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100925 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100926 /* Parse readahead, return when there is still no message. */
927 channel_parse_json(idx);
928 if (channel_get_json(idx, -1, &listtv) == FAIL)
929 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100930 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100931
932 list = listtv->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100933 argv[1] = list->lv_first->li_next->li_tv;
934 typetv = &list->lv_first->li_tv;
935 if (typetv->v_type == VAR_STRING)
936 {
937 typval_T *arg3 = NULL;
938 char_u *cmd = typetv->vval.v_string;
939
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100940 /* ["cmd", arg] or ["cmd", arg, arg] */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100941 if (list->lv_len == 3)
942 arg3 = &list->lv_last->li_tv;
943 channel_exe_cmd(idx, cmd, &argv[1], arg3);
944 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100945 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100946 }
947
948 if (typetv->v_type != VAR_NUMBER)
949 {
950 /* TODO: give error */
951 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100952 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100953 }
954 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100955 }
Bram Moolenaard7ece102016-02-02 23:23:02 +0100956 else if (channel_peek(idx) == NULL)
957 {
958 /* nothing to read on raw channel */
959 return FALSE;
960 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100961 else
962 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100963 /* For a raw channel we don't know where the message ends, just get
964 * everything. */
965 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100966 argv[1].v_type = VAR_STRING;
967 argv[1].vval.v_string = msg;
968 }
969
Bram Moolenaara07fec92016-02-05 21:04:08 +0100970 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100971 {
Bram Moolenaara07fec92016-02-05 21:04:08 +0100972 cbq_T *cbhead = &channel->ch_cb_head;
973 cbq_T *cbitem = cbhead->next;
974
975 /* invoke the one-time callback with the matching nr */
976 while (cbitem != cbhead)
977 {
978 if (cbitem->seq_nr == seq_nr)
979 {
980 invoke_callback(idx, cbitem->callback, argv);
981 remove_cb_node(cbitem);
982 break;
983 }
984 cbitem = cbitem->next;
985 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100986 }
Bram Moolenaara07fec92016-02-05 21:04:08 +0100987 else if (channel->ch_callback != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100988 {
989 /* invoke the channel callback */
Bram Moolenaara07fec92016-02-05 21:04:08 +0100990 invoke_callback(idx, channel->ch_callback, argv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100991 }
992 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100993
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100994 if (listtv != NULL)
995 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100996 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100997
998 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100999}
1000
1001/*
Bram Moolenaard04a0202016-01-26 23:30:18 +01001002 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001003 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +01001004 */
1005 int
1006channel_is_open(int idx)
1007{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001008 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001009}
1010
1011/*
1012 * Close channel "idx".
1013 * This does not trigger the close callback.
1014 */
1015 void
1016channel_close(int idx)
1017{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001018 channel_T *channel = &channels[idx];
1019 jsonq_T *jhead;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001020 cbq_T *cbhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001021
1022 if (channel->ch_fd >= 0)
1023 {
1024 sock_close(channel->ch_fd);
1025 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001026 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001027#ifdef FEAT_GUI
1028 channel_gui_unregister(idx);
1029#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001030 vim_free(channel->ch_callback);
1031 channel->ch_callback = NULL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001032 channel->ch_timeout = 2000;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001033
1034 while (channel_peek(idx) != NULL)
1035 vim_free(channel_get(idx));
1036
Bram Moolenaara07fec92016-02-05 21:04:08 +01001037 cbhead = &channel->ch_cb_head;
1038 while (cbhead->next != cbhead)
1039 remove_cb_node(cbhead->next);
1040
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001041 jhead = &channel->ch_json_head;
1042 while (jhead->next != jhead)
1043 {
1044 clear_tv(jhead->next->value);
1045 remove_json_node(jhead->next);
1046 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01001047 }
1048}
1049
Bram Moolenaard04a0202016-01-26 23:30:18 +01001050/*
1051 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +01001052 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001053 */
Bram Moolenaar83162462016-01-28 23:10:07 +01001054 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001055channel_save(int idx, char_u *buf, int len)
1056{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001057 readq_T *node;
1058 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001059
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001060 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001061 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +01001062 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001063 node->buffer = alloc(len + 1);
1064 if (node->buffer == NULL)
1065 {
1066 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +01001067 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001068 }
1069 mch_memmove(node->buffer, buf, (size_t)len);
1070 node->buffer[len] = NUL;
1071
Bram Moolenaard04a0202016-01-26 23:30:18 +01001072 /* insert node at tail of queue */
1073 node->next = head;
1074 node->prev = head->prev;
1075 head->prev->next = node;
1076 head->prev = node;
1077
1078 if (debugfd != NULL)
1079 {
1080 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +01001081 if (fwrite(buf, len, 1, debugfd) != 1)
1082 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001083 fprintf(debugfd, "\n");
1084 }
Bram Moolenaar83162462016-01-28 23:10:07 +01001085 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001086}
1087
1088/*
1089 * Return the first buffer from the channel without removing it.
1090 * Returns NULL if there is nothing.
1091 */
1092 char_u *
1093channel_peek(int idx)
1094{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001095 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001096
1097 if (head->next == head || head->next == NULL)
1098 return NULL;
1099 return head->next->buffer;
1100}
1101
1102/*
Bram Moolenaard04a0202016-01-26 23:30:18 +01001103 * Clear the read buffer on channel "idx".
1104 */
1105 void
1106channel_clear(int idx)
1107{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001108 readq_T *head = &channels[idx].ch_head;
1109 readq_T *node = head->next;
1110 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001111
1112 while (node != NULL && node != head)
1113 {
1114 next = node->next;
1115 vim_free(node->buffer);
1116 vim_free(node);
1117 if (next == head)
1118 {
1119 head->next = head;
1120 head->prev = head;
1121 break;
1122 }
1123 node = next;
1124 }
1125}
1126
1127/* Sent when the channel is found closed when reading. */
1128#define DETACH_MSG "\"DETACH\"\n"
1129
1130/* Buffer size for reading incoming messages. */
1131#define MAXMSGSIZE 4096
1132
1133/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001134 * Check for reading from "fd" with "timeout" msec.
1135 * Return FAIL when there is nothing to read.
Bram Moolenaara8343c12016-02-04 22:09:48 +01001136 * Always returns OK for FEAT_GUI_W32.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001137 */
1138 static int
1139channel_wait(int fd, int timeout)
1140{
Bram Moolenaara8343c12016-02-04 22:09:48 +01001141#if defined(HAVE_SELECT) && !defined(FEAT_GUI_W32)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001142 struct timeval tval;
1143 fd_set rfds;
1144 int ret;
1145
1146 FD_ZERO(&rfds);
1147 FD_SET(fd, &rfds);
1148 tval.tv_sec = timeout / 1000;
1149 tval.tv_usec = (timeout % 1000) * 1000;
1150 for (;;)
1151 {
1152 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
1153# ifdef EINTR
1154 if (ret == -1 && errno == EINTR)
1155 continue;
1156# endif
1157 if (ret <= 0)
1158 return FAIL;
1159 break;
1160 }
1161#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001162# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001163 struct pollfd fds;
1164
1165 fds.fd = fd;
1166 fds.events = POLLIN;
1167 if (poll(&fds, 1, timeout) <= 0)
1168 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001169# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001170#endif
1171 return OK;
1172}
1173
1174/*
1175 * Return a unique ID to be used in a message.
1176 */
1177 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001178channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001179{
1180 static int next_id = 1;
1181
1182 return next_id++;
1183}
1184
1185/*
1186 * Read from channel "idx" for as long as there is something to read.
1187 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001188 */
1189 void
1190channel_read(int idx)
1191{
1192 static char_u *buf = NULL;
1193 int len = 0;
1194 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001195 channel_T *channel = &channels[idx];
1196
1197 if (channel->ch_fd < 0)
1198 {
1199 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
1200 return;
1201 }
1202
1203 /* Allocate a buffer to read into. */
1204 if (buf == NULL)
1205 {
1206 buf = alloc(MAXMSGSIZE);
1207 if (buf == NULL)
1208 return; /* out of memory! */
1209 }
1210
1211 /* Keep on reading for as long as there is something to read.
1212 * Use select() or poll() to avoid blocking on a message that is exactly
1213 * MAXMSGSIZE long. */
1214 for (;;)
1215 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001216 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001217 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001218 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1219 if (len <= 0)
1220 break; /* error or nothing more to read */
1221
1222 /* Store the read message in the queue. */
1223 channel_save(idx, buf, len);
1224 readlen += len;
1225 if (len < MAXMSGSIZE)
1226 break; /* did read everything that's available */
1227 }
Bram Moolenaara8343c12016-02-04 22:09:48 +01001228#ifdef FEAT_GUI_W32
1229 if (len == SOCKET_ERROR)
1230 {
1231 /* For Win32 GUI channel_wait() always returns OK and we handle the
1232 * situation that there is nothing to read here.
1233 * TODO: how about a timeout? */
1234 if (WSAGetLastError() == WSAEWOULDBLOCK)
1235 return;
1236 }
1237#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01001238
1239 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1240 if (readlen <= 0)
1241 {
1242 /* Queue a "DETACH" netbeans message in the command queue in order to
1243 * terminate the netbeans session later. Do not end the session here
1244 * directly as we may be running in the context of a call to
1245 * netbeans_parse_messages():
1246 * netbeans_parse_messages
1247 * -> autocmd triggered while processing the netbeans cmd
1248 * -> ui_breakcheck
1249 * -> gui event loop or select loop
1250 * -> channel_read()
1251 */
1252 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1253
1254 channel_close(idx);
1255 if (channel->ch_close_cb != NULL)
1256 (*channel->ch_close_cb)();
1257
1258 if (len < 0)
1259 {
1260 /* Todo: which channel? */
1261 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001262 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001263 }
1264 }
1265
1266#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1267 if (CH_HAS_GUI && gtk_main_level() > 0)
1268 gtk_main_quit();
1269#endif
1270}
1271
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001272/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001273 * Read from raw channel "idx". Blocks until there is something to read or
1274 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001275 * Returns what was read in allocated memory.
1276 * Returns NULL in case of error or timeout.
1277 */
1278 char_u *
1279channel_read_block(int idx)
1280{
1281 if (channel_peek(idx) == NULL)
1282 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001283 /* Wait for up to the channel timeout. */
1284 if (channel_wait(channels[idx].ch_fd, channels[idx].ch_timeout) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001285 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001286 channel_read(idx);
1287 }
1288
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001289 return channel_get_all(idx);
1290}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001291
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001292/*
1293 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1294 * result in "rettv".
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001295 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001296 */
1297 int
1298channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1299{
Bram Moolenaare56bf152016-02-08 23:23:42 +01001300 int more;
1301 channel_T *channel = &channels[ch_idx];
Bram Moolenaard7ece102016-02-02 23:23:02 +01001302
Bram Moolenaare56bf152016-02-08 23:23:42 +01001303 channel->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001304 for (;;)
1305 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001306 more = channel_parse_json(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001307
1308 /* search for messsage "id" */
1309 if (channel_get_json(ch_idx, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001310 {
1311 channel->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001312 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01001313 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001314
Bram Moolenaard7ece102016-02-02 23:23:02 +01001315 if (!more)
1316 {
1317 /* Handle any other messages in the queue. If done some more
1318 * messages may have arrived. */
1319 if (channel_parse_messages())
1320 continue;
1321
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001322 /* Wait for up to the channel timeout. */
Bram Moolenaare56bf152016-02-08 23:23:42 +01001323 if (channel->ch_fd < 0 || channel_wait(channel->ch_fd,
1324 channel->ch_timeout) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001325 break;
1326 channel_read(ch_idx);
1327 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001328 }
Bram Moolenaare56bf152016-02-08 23:23:42 +01001329 channel->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001330 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001331}
1332
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001333# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001334/*
1335 * Lookup the channel index from the socket.
1336 * Returns -1 when the socket isn't found.
1337 */
1338 int
1339channel_socket2idx(sock_T fd)
1340{
1341 int i;
1342
1343 if (fd >= 0)
1344 for (i = 0; i < channel_count; ++i)
1345 if (channels[i].ch_fd == fd)
1346 return i;
1347 return -1;
1348}
1349# endif
1350
Bram Moolenaard04a0202016-01-26 23:30:18 +01001351/*
1352 * Write "buf" (NUL terminated string) to channel "idx".
1353 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001354 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001355 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001356 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001357channel_send(int idx, char_u *buf, char *fun)
1358{
1359 channel_T *channel = &channels[idx];
1360 int len = (int)STRLEN(buf);
1361
1362 if (channel->ch_fd < 0)
1363 {
1364 if (!channel->ch_error && fun != NULL)
1365 {
1366 CHERROR(" %s(): write while not connected\n", fun);
1367 EMSG2("E630: %s(): write while not connected", fun);
1368 }
1369 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001370 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001371 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001372
1373 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001374 {
1375 if (!channel->ch_error && fun != NULL)
1376 {
1377 CHERROR(" %s(): write failed\n", fun);
1378 EMSG2("E631: %s(): write failed", fun);
1379 }
1380 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001381 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001382 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001383
1384 channel->ch_error = FALSE;
1385 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001386}
1387
1388# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001389/*
1390 * Add open channels to the poll struct.
1391 * Return the adjusted struct index.
1392 * The type of "fds" is hidden to avoid problems with the function proto.
1393 */
1394 int
1395channel_poll_setup(int nfd_in, void *fds_in)
1396{
1397 int nfd = nfd_in;
1398 int i;
1399 struct pollfd *fds = fds_in;
1400
1401 for (i = 0; i < channel_count; ++i)
1402 if (channels[i].ch_fd >= 0)
1403 {
1404 channels[i].ch_idx = nfd;
1405 fds[nfd].fd = channels[i].ch_fd;
1406 fds[nfd].events = POLLIN;
1407 nfd++;
1408 }
1409 else
1410 channels[i].ch_idx = -1;
1411
1412 return nfd;
1413}
1414
1415/*
1416 * The type of "fds" is hidden to avoid problems with the function proto.
1417 */
1418 int
1419channel_poll_check(int ret_in, void *fds_in)
1420{
1421 int ret = ret_in;
1422 int i;
1423 struct pollfd *fds = fds_in;
1424
1425 for (i = 0; i < channel_count; ++i)
1426 if (ret > 0 && channels[i].ch_idx != -1
1427 && fds[channels[i].ch_idx].revents & POLLIN)
1428 {
1429 channel_read(i);
1430 --ret;
1431 }
1432
1433 return ret;
1434}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001435# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001436
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001437# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001438/*
1439 * The type of "rfds" is hidden to avoid problems with the function proto.
1440 */
1441 int
1442channel_select_setup(int maxfd_in, void *rfds_in)
1443{
1444 int maxfd = maxfd_in;
1445 int i;
1446 fd_set *rfds = rfds_in;
1447
1448 for (i = 0; i < channel_count; ++i)
1449 if (channels[i].ch_fd >= 0)
1450 {
1451 FD_SET(channels[i].ch_fd, rfds);
1452 if (maxfd < channels[i].ch_fd)
1453 maxfd = channels[i].ch_fd;
1454 }
1455
1456 return maxfd;
1457}
1458
1459/*
1460 * The type of "rfds" is hidden to avoid problems with the function proto.
1461 */
1462 int
1463channel_select_check(int ret_in, void *rfds_in)
1464{
1465 int ret = ret_in;
1466 int i;
1467 fd_set *rfds = rfds_in;
1468
1469 for (i = 0; i < channel_count; ++i)
1470 if (ret > 0 && channels[i].ch_fd >= 0
1471 && FD_ISSET(channels[i].ch_fd, rfds))
1472 {
1473 channel_read(i);
1474 --ret;
1475 }
1476
1477 return ret;
1478}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001479# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001480
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001481/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01001482 * Execute queued up commands.
1483 * Invoked from the main loop when it's safe to execute received commands.
1484 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001485 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001486 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001487channel_parse_messages(void)
1488{
1489 int i;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001490 int ret = FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001491
1492 for (i = 0; i < channel_count; ++i)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001493 while (may_invoke_callback(i) == OK)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001494 {
1495 i = 0; /* start over */
1496 ret = TRUE;
1497 }
1498 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001499}
1500
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01001501/*
1502 * Mark references to lists used in channels.
1503 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01001504 int
1505set_ref_in_channel(int copyID)
1506{
1507 int i;
1508 int abort = FALSE;
1509
1510 for (i = 0; i < channel_count; ++i)
1511 {
1512 jsonq_T *head = &channels[i].ch_json_head;
1513 jsonq_T *item = head->next;
1514
1515 while (item != head)
1516 {
1517 list_T *l = item->value->vval.v_list;
1518
1519 if (l->lv_copyID != copyID)
1520 {
1521 l->lv_copyID = copyID;
1522 abort = abort || set_ref_in_list(l, copyID, NULL);
1523 }
1524 item = item->next;
1525 }
1526 }
1527 return abort;
1528}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01001529
1530/*
1531 * Return the mode of channel "idx".
1532 * If "idx" is invalid returns MODE_JSON.
1533 */
1534 ch_mode_T
1535channel_get_mode(int idx)
1536{
1537 if (idx < 0 || idx >= channel_count)
1538 return MODE_JSON;
1539 return channels[idx].ch_mode;
1540}
1541
Bram Moolenaare0874f82016-01-24 20:36:41 +01001542#endif /* FEAT_CHANNEL */