blob: 50dd18087c8ef9f1888fc933da6f76f89fc0f95c [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
789 || tv->vval.v_number != channel->ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100790 {
791 *rettv = item->value;
792 remove_json_node(item);
793 return OK;
794 }
795 item = item->next;
796 }
797 return FAIL;
798}
799
800/*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100801 * Execute a command received over channel "idx".
802 * "cmd" is the command string, "arg2" the second argument.
803 * "arg3" is the third argument, NULL if missing.
804 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100805 static void
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100806channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100807{
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100808 char_u *arg;
809
810 if (arg2->v_type != VAR_STRING)
811 {
812 if (p_verbose > 2)
813 EMSG("E903: received ex command with non-string argument");
814 return;
815 }
816 arg = arg2->vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100817 if (arg == NULL)
818 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100819
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100820 if (STRCMP(cmd, "ex") == 0)
821 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100822 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100823 }
824 else if (STRCMP(cmd, "normal") == 0)
825 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100826 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100827
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100828 ea.arg = arg;
829 ea.addr_count = 0;
830 ea.forceit = TRUE; /* no mapping */
831 ex_normal(&ea);
832 }
833 else if (STRCMP(cmd, "redraw") == 0)
834 {
835 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100836
Bram Moolenaar14ad6112016-02-01 21:47:13 +0100837 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100838 ex_redraw(&ea);
839 showruler(FALSE);
840 setcursor();
841 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100842#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100843 if (gui.in_use)
844 {
845 gui_update_cursor(FALSE, FALSE);
846 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100847 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100848#endif
849 }
850 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0)
851 {
852 int is_eval = cmd[1] == 'v';
853
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100854 if (is_eval && (arg3 == NULL || arg3->v_type != VAR_NUMBER))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100855 {
856 if (p_verbose > 2)
857 EMSG("E904: third argument for eval must be a number");
858 }
859 else
860 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100861 typval_T *tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100862 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +0100863 char_u *json = NULL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100864 channel_T *channel = &channels[idx];
865 int options = channel->ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100866
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100867 /* Don't pollute the display with errors. */
868 ++emsg_skip;
869 tv = eval_expr(arg, NULL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100870 if (is_eval)
871 {
Bram Moolenaar55fab432016-02-07 16:53:13 +0100872 if (tv != NULL)
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100873 json = json_encode_nr_expr(arg3->vval.v_number, tv,
874 options);
Bram Moolenaar55fab432016-02-07 16:53:13 +0100875 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100876 {
Bram Moolenaar55fab432016-02-07 16:53:13 +0100877 /* If evaluation failed or the result can't be encoded
878 * then return the string "ERROR". */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100879 err_tv.v_type = VAR_STRING;
880 err_tv.vval.v_string = (char_u *)"ERROR";
881 tv = &err_tv;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100882 json = json_encode_nr_expr(arg3->vval.v_number, tv,
883 options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100884 }
Bram Moolenaar55fab432016-02-07 16:53:13 +0100885 if (json != NULL)
886 {
887 channel_send(idx, json, "eval");
888 vim_free(json);
889 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100890 }
Bram Moolenaar55fab432016-02-07 16:53:13 +0100891 --emsg_skip;
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100892 if (tv != &err_tv)
893 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100894 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100895 }
896 else if (p_verbose > 2)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100897 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100898}
899
900/*
901 * Invoke a callback for channel "idx" if needed.
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100902 * Return OK when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100903 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100904 static int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100905may_invoke_callback(int idx)
906{
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100907 char_u *msg = NULL;
908 typval_T *listtv = NULL;
909 list_T *list;
910 typval_T *typetv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100911 typval_T argv[3];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100912 int seq_nr = -1;
Bram Moolenaara07fec92016-02-05 21:04:08 +0100913 channel_T *channel = &channels[idx];
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100914 ch_mode_T ch_mode = channel->ch_mode;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100915
Bram Moolenaara07fec92016-02-05 21:04:08 +0100916 if (channel->ch_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100917 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100918 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100919
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100920 if (ch_mode != MODE_RAW)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100921 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100922 /* Get any json message in the queue. */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100923 if (channel_get_json(idx, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100924 {
Bram Moolenaard7ece102016-02-02 23:23:02 +0100925 /* Parse readahead, return when there is still no message. */
926 channel_parse_json(idx);
927 if (channel_get_json(idx, -1, &listtv) == FAIL)
928 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100929 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100930
931 list = listtv->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100932 argv[1] = list->lv_first->li_next->li_tv;
933 typetv = &list->lv_first->li_tv;
934 if (typetv->v_type == VAR_STRING)
935 {
936 typval_T *arg3 = NULL;
937 char_u *cmd = typetv->vval.v_string;
938
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100939 /* ["cmd", arg] or ["cmd", arg, arg] */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100940 if (list->lv_len == 3)
941 arg3 = &list->lv_last->li_tv;
942 channel_exe_cmd(idx, cmd, &argv[1], arg3);
943 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100944 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100945 }
946
947 if (typetv->v_type != VAR_NUMBER)
948 {
949 /* TODO: give error */
950 clear_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100951 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100952 }
953 seq_nr = typetv->vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100954 }
Bram Moolenaard7ece102016-02-02 23:23:02 +0100955 else if (channel_peek(idx) == NULL)
956 {
957 /* nothing to read on raw channel */
958 return FALSE;
959 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100960 else
961 {
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100962 /* For a raw channel we don't know where the message ends, just get
963 * everything. */
964 msg = channel_get_all(idx);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100965 argv[1].v_type = VAR_STRING;
966 argv[1].vval.v_string = msg;
967 }
968
Bram Moolenaara07fec92016-02-05 21:04:08 +0100969 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100970 {
Bram Moolenaara07fec92016-02-05 21:04:08 +0100971 cbq_T *cbhead = &channel->ch_cb_head;
972 cbq_T *cbitem = cbhead->next;
973
974 /* invoke the one-time callback with the matching nr */
975 while (cbitem != cbhead)
976 {
977 if (cbitem->seq_nr == seq_nr)
978 {
979 invoke_callback(idx, cbitem->callback, argv);
980 remove_cb_node(cbitem);
981 break;
982 }
983 cbitem = cbitem->next;
984 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100985 }
Bram Moolenaara07fec92016-02-05 21:04:08 +0100986 else if (channel->ch_callback != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100987 {
988 /* invoke the channel callback */
Bram Moolenaara07fec92016-02-05 21:04:08 +0100989 invoke_callback(idx, channel->ch_callback, argv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100990 }
991 /* else: drop the message TODO: give error */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +0100992
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100993 if (listtv != NULL)
994 clear_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100995 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +0100996
997 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100998}
999
1000/*
Bram Moolenaard04a0202016-01-26 23:30:18 +01001001 * Return TRUE when channel "idx" is open.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001002 * Also returns FALSE or invalid "idx".
Bram Moolenaard04a0202016-01-26 23:30:18 +01001003 */
1004 int
1005channel_is_open(int idx)
1006{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001007 return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001008}
1009
1010/*
1011 * Close channel "idx".
1012 * This does not trigger the close callback.
1013 */
1014 void
1015channel_close(int idx)
1016{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001017 channel_T *channel = &channels[idx];
1018 jsonq_T *jhead;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001019 cbq_T *cbhead;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001020
1021 if (channel->ch_fd >= 0)
1022 {
1023 sock_close(channel->ch_fd);
1024 channel->ch_fd = -1;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001025 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001026#ifdef FEAT_GUI
1027 channel_gui_unregister(idx);
1028#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001029 vim_free(channel->ch_callback);
1030 channel->ch_callback = NULL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001031 channel->ch_timeout = 2000;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001032
1033 while (channel_peek(idx) != NULL)
1034 vim_free(channel_get(idx));
1035
Bram Moolenaara07fec92016-02-05 21:04:08 +01001036 cbhead = &channel->ch_cb_head;
1037 while (cbhead->next != cbhead)
1038 remove_cb_node(cbhead->next);
1039
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001040 jhead = &channel->ch_json_head;
1041 while (jhead->next != jhead)
1042 {
1043 clear_tv(jhead->next->value);
1044 remove_json_node(jhead->next);
1045 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01001046 }
1047}
1048
Bram Moolenaard04a0202016-01-26 23:30:18 +01001049/*
1050 * Store "buf[len]" on channel "idx".
Bram Moolenaar83162462016-01-28 23:10:07 +01001051 * Returns OK or FAIL.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001052 */
Bram Moolenaar83162462016-01-28 23:10:07 +01001053 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001054channel_save(int idx, char_u *buf, int len)
1055{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001056 readq_T *node;
1057 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001058
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001059 node = (readq_T *)alloc(sizeof(readq_T));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001060 if (node == NULL)
Bram Moolenaar83162462016-01-28 23:10:07 +01001061 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001062 node->buffer = alloc(len + 1);
1063 if (node->buffer == NULL)
1064 {
1065 vim_free(node);
Bram Moolenaar83162462016-01-28 23:10:07 +01001066 return FAIL; /* out of memory */
Bram Moolenaard04a0202016-01-26 23:30:18 +01001067 }
1068 mch_memmove(node->buffer, buf, (size_t)len);
1069 node->buffer[len] = NUL;
1070
Bram Moolenaard04a0202016-01-26 23:30:18 +01001071 /* insert node at tail of queue */
1072 node->next = head;
1073 node->prev = head->prev;
1074 head->prev->next = node;
1075 head->prev = node;
1076
1077 if (debugfd != NULL)
1078 {
1079 fprintf(debugfd, "RECV on %d: ", idx);
Bram Moolenaar83162462016-01-28 23:10:07 +01001080 if (fwrite(buf, len, 1, debugfd) != 1)
1081 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001082 fprintf(debugfd, "\n");
1083 }
Bram Moolenaar83162462016-01-28 23:10:07 +01001084 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001085}
1086
1087/*
1088 * Return the first buffer from the channel without removing it.
1089 * Returns NULL if there is nothing.
1090 */
1091 char_u *
1092channel_peek(int idx)
1093{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001094 readq_T *head = &channels[idx].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001095
1096 if (head->next == head || head->next == NULL)
1097 return NULL;
1098 return head->next->buffer;
1099}
1100
1101/*
Bram Moolenaard04a0202016-01-26 23:30:18 +01001102 * Clear the read buffer on channel "idx".
1103 */
1104 void
1105channel_clear(int idx)
1106{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001107 readq_T *head = &channels[idx].ch_head;
1108 readq_T *node = head->next;
1109 readq_T *next;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001110
1111 while (node != NULL && node != head)
1112 {
1113 next = node->next;
1114 vim_free(node->buffer);
1115 vim_free(node);
1116 if (next == head)
1117 {
1118 head->next = head;
1119 head->prev = head;
1120 break;
1121 }
1122 node = next;
1123 }
1124}
1125
1126/* Sent when the channel is found closed when reading. */
1127#define DETACH_MSG "\"DETACH\"\n"
1128
1129/* Buffer size for reading incoming messages. */
1130#define MAXMSGSIZE 4096
1131
1132/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001133 * Check for reading from "fd" with "timeout" msec.
1134 * Return FAIL when there is nothing to read.
Bram Moolenaara8343c12016-02-04 22:09:48 +01001135 * Always returns OK for FEAT_GUI_W32.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001136 */
1137 static int
1138channel_wait(int fd, int timeout)
1139{
Bram Moolenaara8343c12016-02-04 22:09:48 +01001140#if defined(HAVE_SELECT) && !defined(FEAT_GUI_W32)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001141 struct timeval tval;
1142 fd_set rfds;
1143 int ret;
1144
1145 FD_ZERO(&rfds);
1146 FD_SET(fd, &rfds);
1147 tval.tv_sec = timeout / 1000;
1148 tval.tv_usec = (timeout % 1000) * 1000;
1149 for (;;)
1150 {
1151 ret = select(fd + 1, &rfds, NULL, NULL, &tval);
1152# ifdef EINTR
1153 if (ret == -1 && errno == EINTR)
1154 continue;
1155# endif
1156 if (ret <= 0)
1157 return FAIL;
1158 break;
1159 }
1160#else
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001161# ifdef HAVE_POLL
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001162 struct pollfd fds;
1163
1164 fds.fd = fd;
1165 fds.events = POLLIN;
1166 if (poll(&fds, 1, timeout) <= 0)
1167 return FAIL;
Bram Moolenaarb8b65112016-01-28 23:01:49 +01001168# endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001169#endif
1170 return OK;
1171}
1172
1173/*
1174 * Return a unique ID to be used in a message.
1175 */
1176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001177channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001178{
1179 static int next_id = 1;
1180
1181 return next_id++;
1182}
1183
1184/*
1185 * Read from channel "idx" for as long as there is something to read.
1186 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001187 */
1188 void
1189channel_read(int idx)
1190{
1191 static char_u *buf = NULL;
1192 int len = 0;
1193 int readlen = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001194 channel_T *channel = &channels[idx];
1195
1196 if (channel->ch_fd < 0)
1197 {
1198 CHLOG(idx, FALSE, "channel_read() called while socket is closed\n");
1199 return;
1200 }
1201
1202 /* Allocate a buffer to read into. */
1203 if (buf == NULL)
1204 {
1205 buf = alloc(MAXMSGSIZE);
1206 if (buf == NULL)
1207 return; /* out of memory! */
1208 }
1209
1210 /* Keep on reading for as long as there is something to read.
1211 * Use select() or poll() to avoid blocking on a message that is exactly
1212 * MAXMSGSIZE long. */
1213 for (;;)
1214 {
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001215 if (channel_wait(channel->ch_fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001216 break;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001217 len = sock_read(channel->ch_fd, buf, MAXMSGSIZE);
1218 if (len <= 0)
1219 break; /* error or nothing more to read */
1220
1221 /* Store the read message in the queue. */
1222 channel_save(idx, buf, len);
1223 readlen += len;
1224 if (len < MAXMSGSIZE)
1225 break; /* did read everything that's available */
1226 }
Bram Moolenaara8343c12016-02-04 22:09:48 +01001227#ifdef FEAT_GUI_W32
1228 if (len == SOCKET_ERROR)
1229 {
1230 /* For Win32 GUI channel_wait() always returns OK and we handle the
1231 * situation that there is nothing to read here.
1232 * TODO: how about a timeout? */
1233 if (WSAGetLastError() == WSAEWOULDBLOCK)
1234 return;
1235 }
1236#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01001237
1238 /* Reading a socket disconnection (readlen == 0), or a socket error. */
1239 if (readlen <= 0)
1240 {
1241 /* Queue a "DETACH" netbeans message in the command queue in order to
1242 * terminate the netbeans session later. Do not end the session here
1243 * directly as we may be running in the context of a call to
1244 * netbeans_parse_messages():
1245 * netbeans_parse_messages
1246 * -> autocmd triggered while processing the netbeans cmd
1247 * -> ui_breakcheck
1248 * -> gui event loop or select loop
1249 * -> channel_read()
1250 */
1251 channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG));
1252
1253 channel_close(idx);
1254 if (channel->ch_close_cb != NULL)
1255 (*channel->ch_close_cb)();
1256
1257 if (len < 0)
1258 {
1259 /* Todo: which channel? */
1260 CHERROR("%s(): cannot from channel\n", "channel_read");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001261 PERROR(_("E896: read from channel"));
Bram Moolenaard04a0202016-01-26 23:30:18 +01001262 }
1263 }
1264
1265#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
1266 if (CH_HAS_GUI && gtk_main_level() > 0)
1267 gtk_main_quit();
1268#endif
1269}
1270
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001271/*
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001272 * Read from raw channel "idx". Blocks until there is something to read or
1273 * the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001274 * Returns what was read in allocated memory.
1275 * Returns NULL in case of error or timeout.
1276 */
1277 char_u *
1278channel_read_block(int idx)
1279{
1280 if (channel_peek(idx) == NULL)
1281 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001282 /* Wait for up to the channel timeout. */
1283 if (channel_wait(channels[idx].ch_fd, channels[idx].ch_timeout) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001284 return NULL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001285 channel_read(idx);
1286 }
1287
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001288 return channel_get_all(idx);
1289}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001290
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001291/*
1292 * Read one JSON message from channel "ch_idx" with ID "id" and store the
1293 * result in "rettv".
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001294 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001295 */
1296 int
1297channel_read_json_block(int ch_idx, int id, typval_T **rettv)
1298{
Bram Moolenaare56bf152016-02-08 23:23:42 +01001299 int more;
1300 channel_T *channel = &channels[ch_idx];
Bram Moolenaard7ece102016-02-02 23:23:02 +01001301
Bram Moolenaare56bf152016-02-08 23:23:42 +01001302 channel->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001303 for (;;)
1304 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001305 more = channel_parse_json(ch_idx);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001306
1307 /* search for messsage "id" */
1308 if (channel_get_json(ch_idx, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001309 {
1310 channel->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001311 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01001312 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001313
Bram Moolenaard7ece102016-02-02 23:23:02 +01001314 if (!more)
1315 {
1316 /* Handle any other messages in the queue. If done some more
1317 * messages may have arrived. */
1318 if (channel_parse_messages())
1319 continue;
1320
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001321 /* Wait for up to the channel timeout. */
Bram Moolenaare56bf152016-02-08 23:23:42 +01001322 if (channel->ch_fd < 0 || channel_wait(channel->ch_fd,
1323 channel->ch_timeout) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001324 break;
1325 channel_read(ch_idx);
1326 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001327 }
Bram Moolenaare56bf152016-02-08 23:23:42 +01001328 channel->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001329 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001330}
1331
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001332# if defined(WIN32) || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01001333/*
1334 * Lookup the channel index from the socket.
1335 * Returns -1 when the socket isn't found.
1336 */
1337 int
1338channel_socket2idx(sock_T fd)
1339{
1340 int i;
1341
1342 if (fd >= 0)
1343 for (i = 0; i < channel_count; ++i)
1344 if (channels[i].ch_fd == fd)
1345 return i;
1346 return -1;
1347}
1348# endif
1349
Bram Moolenaard04a0202016-01-26 23:30:18 +01001350/*
1351 * Write "buf" (NUL terminated string) to channel "idx".
1352 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001353 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001354 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001355 int
Bram Moolenaard04a0202016-01-26 23:30:18 +01001356channel_send(int idx, char_u *buf, char *fun)
1357{
1358 channel_T *channel = &channels[idx];
1359 int len = (int)STRLEN(buf);
1360
1361 if (channel->ch_fd < 0)
1362 {
1363 if (!channel->ch_error && fun != NULL)
1364 {
1365 CHERROR(" %s(): write while not connected\n", fun);
1366 EMSG2("E630: %s(): write while not connected", fun);
1367 }
1368 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001369 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001370 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001371
1372 if (sock_write(channel->ch_fd, buf, len) != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001373 {
1374 if (!channel->ch_error && fun != NULL)
1375 {
1376 CHERROR(" %s(): write failed\n", fun);
1377 EMSG2("E631: %s(): write failed", fun);
1378 }
1379 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001380 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001381 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001382
1383 channel->ch_error = FALSE;
1384 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01001385}
1386
1387# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001388/*
1389 * Add open channels to the poll struct.
1390 * Return the adjusted struct index.
1391 * The type of "fds" is hidden to avoid problems with the function proto.
1392 */
1393 int
1394channel_poll_setup(int nfd_in, void *fds_in)
1395{
1396 int nfd = nfd_in;
1397 int i;
1398 struct pollfd *fds = fds_in;
1399
1400 for (i = 0; i < channel_count; ++i)
1401 if (channels[i].ch_fd >= 0)
1402 {
1403 channels[i].ch_idx = nfd;
1404 fds[nfd].fd = channels[i].ch_fd;
1405 fds[nfd].events = POLLIN;
1406 nfd++;
1407 }
1408 else
1409 channels[i].ch_idx = -1;
1410
1411 return nfd;
1412}
1413
1414/*
1415 * The type of "fds" is hidden to avoid problems with the function proto.
1416 */
1417 int
1418channel_poll_check(int ret_in, void *fds_in)
1419{
1420 int ret = ret_in;
1421 int i;
1422 struct pollfd *fds = fds_in;
1423
1424 for (i = 0; i < channel_count; ++i)
1425 if (ret > 0 && channels[i].ch_idx != -1
1426 && fds[channels[i].ch_idx].revents & POLLIN)
1427 {
1428 channel_read(i);
1429 --ret;
1430 }
1431
1432 return ret;
1433}
Bram Moolenaard04a0202016-01-26 23:30:18 +01001434# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001435
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001436# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01001437/*
1438 * The type of "rfds" is hidden to avoid problems with the function proto.
1439 */
1440 int
1441channel_select_setup(int maxfd_in, void *rfds_in)
1442{
1443 int maxfd = maxfd_in;
1444 int i;
1445 fd_set *rfds = rfds_in;
1446
1447 for (i = 0; i < channel_count; ++i)
1448 if (channels[i].ch_fd >= 0)
1449 {
1450 FD_SET(channels[i].ch_fd, rfds);
1451 if (maxfd < channels[i].ch_fd)
1452 maxfd = channels[i].ch_fd;
1453 }
1454
1455 return maxfd;
1456}
1457
1458/*
1459 * The type of "rfds" is hidden to avoid problems with the function proto.
1460 */
1461 int
1462channel_select_check(int ret_in, void *rfds_in)
1463{
1464 int ret = ret_in;
1465 int i;
1466 fd_set *rfds = rfds_in;
1467
1468 for (i = 0; i < channel_count; ++i)
1469 if (ret > 0 && channels[i].ch_fd >= 0
1470 && FD_ISSET(channels[i].ch_fd, rfds))
1471 {
1472 channel_read(i);
1473 --ret;
1474 }
1475
1476 return ret;
1477}
Bram Moolenaarf12d9832016-01-29 21:11:25 +01001478# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01001479
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001480/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01001481 * Execute queued up commands.
1482 * Invoked from the main loop when it's safe to execute received commands.
1483 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001484 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001485 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001486channel_parse_messages(void)
1487{
1488 int i;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001489 int ret = FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001490
1491 for (i = 0; i < channel_count; ++i)
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001492 while (may_invoke_callback(i) == OK)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001493 {
1494 i = 0; /* start over */
1495 ret = TRUE;
1496 }
1497 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001498}
1499
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01001500/*
1501 * Mark references to lists used in channels.
1502 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01001503 int
1504set_ref_in_channel(int copyID)
1505{
1506 int i;
1507 int abort = FALSE;
1508
1509 for (i = 0; i < channel_count; ++i)
1510 {
1511 jsonq_T *head = &channels[i].ch_json_head;
1512 jsonq_T *item = head->next;
1513
1514 while (item != head)
1515 {
1516 list_T *l = item->value->vval.v_list;
1517
1518 if (l->lv_copyID != copyID)
1519 {
1520 l->lv_copyID = copyID;
1521 abort = abort || set_ref_in_list(l, copyID, NULL);
1522 }
1523 item = item->next;
1524 }
1525 }
1526 return abort;
1527}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01001528
1529/*
1530 * Return the mode of channel "idx".
1531 * If "idx" is invalid returns MODE_JSON.
1532 */
1533 ch_mode_T
1534channel_get_mode(int idx)
1535{
1536 if (idx < 0 || idx >= channel_count)
1537 return MODE_JSON;
1538 return channels[idx].ch_mode;
1539}
1540
Bram Moolenaare0874f82016-01-24 20:36:41 +01001541#endif /* FEAT_CHANNEL */