blob: 72484ecb0483d7c1c4ca0f98a94625163c4ea8c6 [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
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
22/* Note: when making changes here also adjust configure.in. */
23#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaar187db502016-02-27 14:44:26 +010057/* Whether a redraw is needed for appending a line to a buffer. */
58static int channel_need_redraw = FALSE;
59
60
Bram Moolenaard8070362016-02-15 21:56:54 +010061#ifdef WIN32
62 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010063fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010064{
65 HANDLE h = (HANDLE)fd;
66 DWORD nread;
67
68 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
69 return -1;
70 return (int)nread;
71}
72
73 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010074fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010075{
76 HANDLE h = (HANDLE)fd;
77 DWORD nwrite;
78
79 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
80 return -1;
81 return (int)nwrite;
82}
83
84 static void
85fd_close(sock_T fd)
86{
87 HANDLE h = (HANDLE)fd;
88
89 CloseHandle(h);
90}
91#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010092
Bram Moolenaar6463ca22016-02-13 17:04:46 +010093/* Log file opened with ch_logfile(). */
94static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +010095#ifdef FEAT_RELTIME
96static proftime_T log_start;
97#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010098
99 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100100ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100101{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100102 FILE *file = NULL;
103
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100104 if (log_fd != NULL)
105 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100106
107 if (*fname != NUL)
108 {
109 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
110 if (file == NULL)
111 {
112 EMSG2(_(e_notopen), fname);
113 return;
114 }
115 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100116 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100117
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100118 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100119 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100120 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100121#ifdef FEAT_RELTIME
122 profile_start(&log_start);
123#endif
124 }
125}
126
127 int
128ch_log_active()
129{
130 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100131}
132
133 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100134ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100135{
136 if (log_fd != NULL)
137 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100138#ifdef FEAT_RELTIME
139 proftime_T log_now;
140
141 profile_start(&log_now);
142 profile_sub(&log_now, &log_start);
143 fprintf(log_fd, "%s ", profile_msg(&log_now));
144#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100145 if (ch != NULL)
146 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100147 else
148 fprintf(log_fd, "%s: ", what);
149 }
150}
151
Bram Moolenaard0b65022016-03-06 21:50:33 +0100152static int did_log_msg = TRUE;
153
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100154 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100155ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100156{
157 if (log_fd != NULL)
158 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100159 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100160 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100162 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100163 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164 }
165}
166
167 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100168ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169{
170 if (log_fd != NULL)
171 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100172 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100173 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100174 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100176 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100177 }
178}
179
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100180 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100181ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182{
183 if (log_fd != NULL)
184 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100185 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100186 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100187 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100188 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100189 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 }
191}
192
193 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100194ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195{
196 if (log_fd != NULL)
197 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100198 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100199 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100200 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100202 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100203 }
204}
205
206 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100207ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208{
209 if (log_fd != NULL)
210 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100211 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100212 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100213 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100214 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100215 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100216 }
217}
218
219 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100220ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221{
222 if (log_fd != NULL)
223 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100224 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100225 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100226 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100228 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100229 }
230}
231
232 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100233ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234{
235 if (log_fd != NULL)
236 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100237 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100238 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100239 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100240 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100241 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100242 }
243}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100244
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100245#ifdef _WIN32
246# undef PERROR
247# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
248 (char_u *)msg, (char_u *)strerror_win32(errno))
249
250 static char *
251strerror_win32(int eno)
252{
253 static LPVOID msgbuf = NULL;
254 char_u *ptr;
255
256 if (msgbuf)
257 LocalFree(msgbuf);
258 FormatMessage(
259 FORMAT_MESSAGE_ALLOCATE_BUFFER |
260 FORMAT_MESSAGE_FROM_SYSTEM |
261 FORMAT_MESSAGE_IGNORE_INSERTS,
262 NULL,
263 eno,
264 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
265 (LPTSTR) &msgbuf,
266 0,
267 NULL);
268 /* chomp \r or \n */
269 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
270 switch (*ptr)
271 {
272 case '\r':
273 STRMOVE(ptr, ptr + 1);
274 ptr--;
275 break;
276 case '\n':
277 if (*(ptr + 1) == '\0')
278 *ptr = '\0';
279 else
280 *ptr = ' ';
281 break;
282 }
283 return msgbuf;
284}
285#endif
286
Bram Moolenaar77073442016-02-13 23:23:53 +0100287/*
288 * The list of all allocated channels.
289 */
290static channel_T *first_channel = NULL;
291static int next_ch_id = 0;
292
293/*
294 * Allocate a new channel. The refcount is set to 1.
295 * The channel isn't actually used until it is opened.
296 * Returns NULL if out of memory.
297 */
298 channel_T *
299add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100300{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100301 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100302 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100303
Bram Moolenaar77073442016-02-13 23:23:53 +0100304 if (channel == NULL)
305 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100306
Bram Moolenaar77073442016-02-13 23:23:53 +0100307 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100308 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100309
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100310 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100311 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100315#endif
316#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100317 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100318#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100320 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100321
Bram Moolenaar77073442016-02-13 23:23:53 +0100322 if (first_channel != NULL)
323 {
324 first_channel->ch_prev = channel;
325 channel->ch_next = first_channel;
326 }
327 first_channel = channel;
328
329 channel->ch_refcount = 1;
330 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100331}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100332
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100333/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100334 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100335 * Return TRUE if "channel" has a callback and the associated job wasn't
336 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100337 */
338 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100339channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100340{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100341 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100342 int has_out_msg;
343 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100344
345 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100346 if (channel->ch_job_killed && channel->ch_job == NULL)
347 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100348
349 /* If there is a close callback it may still need to be invoked. */
350 if (channel->ch_close_cb != NULL)
351 return TRUE;
352
353 /* If there is no callback then nobody can get readahead. If the fd is
354 * closed and there is no readahead then the callback won't be called. */
355 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
356 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
357 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
359 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
360 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
361 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
362 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
363 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100364 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100365 || has_out_msg || has_err_msg))
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100366 || (channel->ch_part[PART_OUT].ch_callback != NULL && has_out_msg)
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100367 || (channel->ch_part[PART_ERR].ch_callback != NULL && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100368}
369
370/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200371 * Close a channel and free all its resources.
372 */
373 static void
374channel_free_contents(channel_T *channel)
375{
376 channel_close(channel, TRUE);
377 channel_clear(channel);
378 ch_log(channel, "Freeing channel");
379}
380
381 static void
382channel_free_channel(channel_T *channel)
383{
384 if (channel->ch_next != NULL)
385 channel->ch_next->ch_prev = channel->ch_prev;
386 if (channel->ch_prev == NULL)
387 first_channel = channel->ch_next;
388 else
389 channel->ch_prev->ch_next = channel->ch_next;
390 vim_free(channel);
391}
392
393 static void
394channel_free(channel_T *channel)
395{
396 if (!in_free_unref_items)
397 {
398 channel_free_contents(channel);
399 channel_free_channel(channel);
400 }
401}
402
403/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100404 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100405 * possible, there is no callback to be invoked or the associated job was
406 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100407 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100408 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100409 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100410channel_may_free(channel_T *channel)
411{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100412 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100413 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100414 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100415 return TRUE;
416 }
417 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100418}
419
420/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100421 * Decrement the reference count on "channel" and maybe free it when it goes
422 * down to zero. Don't free it if there is a pending action.
423 * Returns TRUE when the channel is no longer referenced.
424 */
425 int
426channel_unref(channel_T *channel)
427{
428 if (channel != NULL && --channel->ch_refcount <= 0)
429 return channel_may_free(channel);
430 return FALSE;
431}
432
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200433 int
434free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100435{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200436 int did_free = FALSE;
437 channel_T *ch;
438
439 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
440 if ((ch->ch_copyID & mask) != (copyID & mask))
441 {
442 /* Free the channel and ordinary items it contains, but don't
443 * recurse into Lists, Dictionaries etc. */
444 channel_free_contents(ch);
445 did_free = TRUE;
446 }
447 return did_free;
448}
449
450 void
451free_unused_channels(int copyID, int mask)
452{
453 channel_T *ch;
454 channel_T *ch_next;
455
456 for (ch = first_channel; ch != NULL; ch = ch_next)
457 {
458 ch_next = ch->ch_next;
459 if ((ch->ch_copyID & mask) != (copyID & mask))
460 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200461 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200462 channel_free_channel(ch);
463 }
464 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100465}
466
Bram Moolenaard04a0202016-01-26 23:30:18 +0100467#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100468
469#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
470 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100471channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100472{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100473 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100474 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100475
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100476 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100477 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100478 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100479 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100480 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100481}
482#endif
483
Bram Moolenaare0874f82016-01-24 20:36:41 +0100484/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100485 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100486 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100487#ifdef FEAT_GUI_X11
488 static void
489messageFromNetbeans(XtPointer clientData,
490 int *unused1 UNUSED,
491 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100492{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100493 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100494}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100495#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496
Bram Moolenaard04a0202016-01-26 23:30:18 +0100497#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100498# if GTK_CHECK_VERSION(3,0,0)
499 static gboolean
500messageFromNetbeans(GIOChannel *unused1 UNUSED,
501 GIOCondition unused2 UNUSED,
502 gpointer clientData)
503{
504 channel_read_fd(GPOINTER_TO_INT(clientData));
505 return TRUE; /* Return FALSE instead in case the event source is to
506 * be removed after this function returns. */
507}
508# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100509 static void
510messageFromNetbeans(gpointer clientData,
511 gint unused1 UNUSED,
512 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100513{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100514 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100515}
Bram Moolenaar98921892016-02-23 17:14:37 +0100516# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517#endif
518
519 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100520channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100521{
Bram Moolenaarde279892016-03-11 22:19:44 +0100522 if (!CH_HAS_GUI)
523 return;
524
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100525# ifdef FEAT_GUI_X11
526 /* Tell notifier we are interested in being called
527 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100528 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
529 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100530 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100531 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100532 (XtPointer)(XtInputReadMask + XtInputExceptMask),
533 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100534 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100535# else
536# ifdef FEAT_GUI_GTK
537 /* Tell gdk we are interested in being called when there
538 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100539 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100540# if GTK_CHECK_VERSION(3,0,0)
541 {
542 GIOChannel *chnnl = g_io_channel_unix_new(
543 (gint)channel->ch_part[part].ch_fd);
544
545 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
546 chnnl,
547 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
548 messageFromNetbeans,
549 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
550
551 g_io_channel_unref(chnnl);
552 }
553# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100554 channel->ch_part[part].ch_inputHandler = gdk_input_add(
555 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100556 (GdkInputCondition)
557 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100558 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100559 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100560# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100561# endif
562# endif
563}
564
Bram Moolenaarde279892016-03-11 22:19:44 +0100565 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100566channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100567{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100568 if (channel->CH_SOCK_FD != INVALID_FD)
569 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100570 if (channel->CH_OUT_FD != INVALID_FD)
571 channel_gui_register_one(channel, PART_OUT);
572 if (channel->CH_ERR_FD != INVALID_FD)
573 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100574}
575
576/*
577 * Register any of our file descriptors with the GUI event handling system.
578 * Called when the GUI has started.
579 */
580 void
581channel_gui_register_all(void)
582{
Bram Moolenaar77073442016-02-13 23:23:53 +0100583 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100584
Bram Moolenaar77073442016-02-13 23:23:53 +0100585 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100586 channel_gui_register(channel);
587}
588
589 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100590channel_gui_unregister_one(channel_T *channel, int part)
591{
592# ifdef FEAT_GUI_X11
593 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
594 {
595 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
596 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
597 }
598# else
599# ifdef FEAT_GUI_GTK
600 if (channel->ch_part[part].ch_inputHandler != 0)
601 {
602# if GTK_CHECK_VERSION(3,0,0)
603 g_source_remove(channel->ch_part[part].ch_inputHandler);
604# else
605 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
606# endif
607 channel->ch_part[part].ch_inputHandler = 0;
608 }
609# endif
610# endif
611}
612
613 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100614channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100615{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100616 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100617
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100618 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100619 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100620}
621
622#endif
623
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100624static char *e_cannot_connect = N_("E902: Cannot connect to port");
625
Bram Moolenaard04a0202016-01-26 23:30:18 +0100626/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100627 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100628 * "waittime" is the time in msec to wait for the connection.
629 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100630 * Returns the channel for success.
631 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100632 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100633 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100634channel_open(
635 char *hostname,
636 int port_in,
637 int waittime,
638 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100639{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100640 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100641 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100642 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100643#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100644 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100645 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100646#else
647 int port = port_in;
648#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100649 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100650 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100651
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100652#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100653 channel_init_winsock();
654#endif
655
Bram Moolenaar77073442016-02-13 23:23:53 +0100656 channel = add_channel();
657 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100659 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100660 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100661 }
662
663 /* Get the server internet address and put into addr structure */
664 /* fill in the socket address structure and connect to server */
665 vim_memset((char *)&server, 0, sizeof(server));
666 server.sin_family = AF_INET;
667 server.sin_port = htons(port);
668 if ((host = gethostbyname(hostname)) == NULL)
669 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100670 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100671 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100672 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100673 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100674 }
675 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
676
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100677 /* On Mac and Solaris a zero timeout almost never works. At least wait
678 * one millisecond. Let's do it for all systems, because we don't know why
679 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 if (waittime == 0)
681 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682
683 /*
684 * For Unix we need to call connect() again after connect() failed.
685 * On Win32 one time is sufficient.
686 */
687 while (TRUE)
688 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100689 long elapsed_msec = 0;
690 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100691
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100692 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100694 sd = socket(AF_INET, SOCK_STREAM, 0);
695 if (sd == -1)
696 {
697 ch_error(channel, "in socket() in channel_open().");
698 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100699 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100700 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100701 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100702
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100703 if (waittime >= 0)
704 {
705 /* Make connect() non-blocking. */
706 if (
707#ifdef _WIN32
708 ioctlsocket(sd, FIONBIO, &val) < 0
709#else
710 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
711#endif
712 )
713 {
714 SOCK_ERRNO;
715 ch_errorn(channel,
716 "channel_open: Connect failed with errno %d", errno);
717 sock_close(sd);
718 channel_free(channel);
719 return NULL;
720 }
721 }
722
723 /* Try connecting to the server. */
724 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
725 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
726
Bram Moolenaar045a2842016-03-08 22:33:07 +0100727 if (ret == 0)
728 /* The connection could be established. */
729 break;
730
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100732 if (waittime < 0 || (errno != EWOULDBLOCK
733 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100734#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100735 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100736#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100737 ))
738 {
739 ch_errorn(channel,
740 "channel_open: Connect failed with errno %d", errno);
741 PERROR(_(e_cannot_connect));
742 sock_close(sd);
743 channel_free(channel);
744 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100745 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100746
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100747 /* Limit the waittime to 50 msec. If it doesn't work within this
748 * time we close the socket and try creating it again. */
749 waitnow = waittime > 50 ? 50 : waittime;
750
Bram Moolenaar045a2842016-03-08 22:33:07 +0100751 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100752 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100753#ifndef WIN32
754 if (errno != ECONNREFUSED)
755#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100756 {
757 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100758 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100759 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100760#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100761 int so_error = 0;
762 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100763 struct timeval start_tv;
764 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100765#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100766 FD_ZERO(&rfds);
767 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100768 FD_ZERO(&wfds);
769 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100770
Bram Moolenaar562ca712016-03-09 21:50:05 +0100771 tv.tv_sec = waitnow / 1000;
772 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100773#ifndef WIN32
774 gettimeofday(&start_tv, NULL);
775#endif
776 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100777 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100778 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100779
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100780 if (ret < 0)
781 {
782 SOCK_ERRNO;
783 ch_errorn(channel,
784 "channel_open: Connect failed with errno %d", errno);
785 PERROR(_(e_cannot_connect));
786 sock_close(sd);
787 channel_free(channel);
788 return NULL;
789 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100790
Bram Moolenaare081e212016-02-28 22:33:46 +0100791#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100792 /* On Win32: select() is expected to work and wait for up to
793 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100794 if (FD_ISSET(sd, &wfds))
795 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100796 elapsed_msec = waitnow;
797 if (waittime > 1 && elapsed_msec < waittime)
798 {
799 waittime -= elapsed_msec;
800 continue;
801 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100802#else
803 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100804 * After putting the socket in non-blocking mode, connect() will
805 * return EINPROGRESS, select() will not wait (as if writing is
806 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100807 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100809 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100810 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100811 {
812 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100813 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100814 if (ret < 0 || (so_error != 0
815 && so_error != EWOULDBLOCK
816 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100817# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100819# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100820 ))
821 {
822 ch_errorn(channel,
823 "channel_open: Connect failed with errno %d",
824 so_error);
825 PERROR(_(e_cannot_connect));
826 sock_close(sd);
827 channel_free(channel);
828 return NULL;
829 }
830 }
831
Bram Moolenaar045a2842016-03-08 22:33:07 +0100832 if (FD_ISSET(sd, &wfds) && so_error == 0)
833 /* Did not detect an error, connection is established. */
834 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100835
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836 gettimeofday(&end_tv, NULL);
837 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
838 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100839#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100840 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100841
842#ifndef WIN32
843 if (waittime > 1 && elapsed_msec < waittime)
844 {
845 /* The port isn't ready but we also didn't get an error.
846 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100847 * yet. Select() may return early, wait until the remaining
848 * "waitnow" and try again. */
849 waitnow -= elapsed_msec;
850 waittime -= elapsed_msec;
851 if (waitnow > 0)
852 {
853 mch_delay((long)waitnow, TRUE);
854 ui_breakcheck();
855 waittime -= waitnow;
856 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100857 if (!got_int)
858 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100859 if (waittime <= 0)
860 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100861 waittime = 1;
862 continue;
863 }
864 /* we were interrupted, behave as if timed out */
865 }
866#endif
867
868 /* We timed out. */
869 ch_error(channel, "Connection timed out");
870 sock_close(sd);
871 channel_free(channel);
872 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100873 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100874
Bram Moolenaar045a2842016-03-08 22:33:07 +0100875 ch_log(channel, "Connection made");
876
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100877 if (waittime >= 0)
878 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100879#ifdef _WIN32
880 val = 0;
881 ioctlsocket(sd, FIONBIO, &val);
882#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100883 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100884#endif
885 }
886
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100887 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100888 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100889 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
890 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100891
892#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100893 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100894#endif
895
Bram Moolenaar77073442016-02-13 23:23:53 +0100896 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100897}
898
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100899/*
900 * Implements ch_open().
901 */
902 channel_T *
903channel_open_func(typval_T *argvars)
904{
905 char_u *address;
906 char_u *p;
907 char *rest;
908 int port;
909 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200910 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100911
912 address = get_tv_string(&argvars[0]);
913 if (argvars[1].v_type != VAR_UNKNOWN
914 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
915 {
916 EMSG(_(e_invarg));
917 return NULL;
918 }
919
920 /* parse address */
921 p = vim_strchr(address, ':');
922 if (p == NULL)
923 {
924 EMSG2(_(e_invarg2), address);
925 return NULL;
926 }
927 *p++ = NUL;
928 port = strtol((char *)p, &rest, 10);
929 if (*address == NUL || port <= 0 || *rest != NUL)
930 {
931 p[-1] = ':';
932 EMSG2(_(e_invarg2), address);
933 return NULL;
934 }
935
936 /* parse options */
937 clear_job_options(&opt);
938 opt.jo_mode = MODE_JSON;
939 opt.jo_timeout = 2000;
940 if (get_job_options(&argvars[1], &opt,
941 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200942 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100943 if (opt.jo_timeout < 0)
944 {
945 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200946 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100947 }
948
949 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
950 if (channel != NULL)
951 {
952 opt.jo_set = JO_ALL;
953 channel_set_options(channel, &opt);
954 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200955theend:
956 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100957 return channel;
958}
959
Bram Moolenaarde279892016-03-11 22:19:44 +0100960 static void
961may_close_part(sock_T *fd)
962{
963 if (*fd != INVALID_FD)
964 {
965 fd_close(*fd);
966 *fd = INVALID_FD;
967 }
968}
969
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100970 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100971channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100972{
Bram Moolenaarde279892016-03-11 22:19:44 +0100973 if (in != INVALID_FD)
974 {
975 may_close_part(&channel->CH_IN_FD);
976 channel->CH_IN_FD = in;
977 }
978 if (out != INVALID_FD)
979 {
980# if defined(FEAT_GUI)
981 channel_gui_unregister_one(channel, PART_OUT);
982# endif
983 may_close_part(&channel->CH_OUT_FD);
984 channel->CH_OUT_FD = out;
985# if defined(FEAT_GUI)
986 channel_gui_register_one(channel, PART_OUT);
987# endif
988 }
989 if (err != INVALID_FD)
990 {
991# if defined(FEAT_GUI)
992 channel_gui_unregister_one(channel, PART_ERR);
993# endif
994 may_close_part(&channel->CH_ERR_FD);
995 channel->CH_ERR_FD = err;
996# if defined(FEAT_GUI)
997 channel_gui_register_one(channel, PART_ERR);
998# endif
999 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001000}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001001
Bram Moolenaard6051b52016-02-28 15:49:03 +01001002/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001003 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001004 * This does not keep a refcount, when the job is freed ch_job is cleared.
1005 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001006 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001007channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001008{
Bram Moolenaar77073442016-02-13 23:23:53 +01001009 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001010
1011 channel_set_options(channel, options);
1012
1013 if (job->jv_in_buf != NULL)
1014 {
1015 chanpart_T *in_part = &channel->ch_part[PART_IN];
1016
1017 in_part->ch_buffer = job->jv_in_buf;
1018 ch_logs(channel, "reading from buffer '%s'",
1019 (char *)in_part->ch_buffer->b_ffname);
1020 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001021 {
1022 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1023 {
1024 /* Special mode: send last-but-one line when appending a line
1025 * to the buffer. */
1026 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001027 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001028 in_part->ch_buf_top =
1029 in_part->ch_buffer->b_ml.ml_line_count + 1;
1030 }
1031 else
1032 in_part->ch_buf_top = options->jo_in_top;
1033 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001034 else
1035 in_part->ch_buf_top = 1;
1036 if (options->jo_set & JO_IN_BOT)
1037 in_part->ch_buf_bot = options->jo_in_bot;
1038 else
1039 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1040 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001041}
1042
1043/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001044 * Find a buffer matching "name" or create a new one.
1045 */
1046 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001047find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001048{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001049 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001050 buf_T *save_curbuf = curbuf;
1051
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001052 if (name != NULL && *name != NUL)
1053 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001054 if (buf == NULL)
1055 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001056 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001057 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001058 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001059 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001060#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001061 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1062 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001063#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001064 if (curbuf->b_ml.ml_mfp == NULL)
1065 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001066 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1067 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001068 changed_bytes(1, 0);
1069 curbuf = save_curbuf;
1070 }
1071
1072 return buf;
1073}
1074
1075/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001076 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001077 */
1078 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001079channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001080{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001081 int part;
1082 char_u **cbp;
1083 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001084
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001085 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001086 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001087 channel->ch_part[part].ch_mode = opt->jo_mode;
1088 if (opt->jo_set & JO_IN_MODE)
1089 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1090 if (opt->jo_set & JO_OUT_MODE)
1091 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1092 if (opt->jo_set & JO_ERR_MODE)
1093 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001094
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001095 if (opt->jo_set & JO_TIMEOUT)
1096 for (part = PART_SOCK; part <= PART_IN; ++part)
1097 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1098 if (opt->jo_set & JO_OUT_TIMEOUT)
1099 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1100 if (opt->jo_set & JO_ERR_TIMEOUT)
1101 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001102 if (opt->jo_set & JO_BLOCK_WRITE)
1103 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001104
1105 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001106 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001107 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001108 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001109 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001110 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001111 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1112 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001113 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001114 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001115 *pp = opt->jo_partial;
1116 if (*pp != NULL)
1117 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001118 }
1119 if (opt->jo_set & JO_OUT_CALLBACK)
1120 {
1121 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001122 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001123 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001124 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001125 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1126 *cbp = vim_strsave(opt->jo_out_cb);
1127 else
1128 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001129 *pp = opt->jo_out_partial;
1130 if (*pp != NULL)
1131 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001132 }
1133 if (opt->jo_set & JO_ERR_CALLBACK)
1134 {
1135 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001136 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001137 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001138 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001139 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1140 *cbp = vim_strsave(opt->jo_err_cb);
1141 else
1142 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001143 *pp = opt->jo_err_partial;
1144 if (*pp != NULL)
1145 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001146 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001147 if (opt->jo_set & JO_CLOSE_CALLBACK)
1148 {
1149 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001150 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001151 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001152 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001153 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1154 *cbp = vim_strsave(opt->jo_close_cb);
1155 else
1156 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001157 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001158 if (*pp != NULL)
1159 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001160 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001161
1162 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1163 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001164 /* writing output to a buffer. Default mode is NL. */
1165 if (!(opt->jo_set & JO_OUT_MODE))
1166 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001167 if (opt->jo_set & JO_OUT_BUF)
1168 channel->ch_part[PART_OUT].ch_buffer =
1169 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1170 else
1171 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001172 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1173 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001174 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1175 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001176
1177 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1178 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1179 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1180 {
1181 /* writing err to a buffer. Default mode is NL. */
1182 if (!(opt->jo_set & JO_ERR_MODE))
1183 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1184 if (opt->jo_io[PART_ERR] == JIO_OUT)
1185 channel->ch_part[PART_ERR].ch_buffer =
1186 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001187 else if (opt->jo_set & JO_ERR_BUF)
1188 channel->ch_part[PART_ERR].ch_buffer =
1189 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001190 else
1191 channel->ch_part[PART_ERR].ch_buffer =
1192 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1193 ch_logs(channel, "writing err to buffer '%s'",
1194 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1195 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001196
1197 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1198 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1199 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001200}
1201
1202/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001203 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001204 */
1205 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001206channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001207 channel_T *channel,
1208 int part,
1209 char_u *callback,
1210 partial_T *partial,
1211 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001212{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001213 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001214 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1215
1216 if (item != NULL)
1217 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001218 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001219 item->cq_partial = partial;
1220 if (partial != NULL)
1221 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001222 item->cq_seq_nr = id;
1223 item->cq_prev = head->cq_prev;
1224 head->cq_prev = item;
1225 item->cq_next = NULL;
1226 if (item->cq_prev == NULL)
1227 head->cq_next = item;
1228 else
1229 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001230 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001231}
1232
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001233 static void
1234write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1235{
1236 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001237 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001238 char_u *p;
1239
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001240 if ((p = alloc(len + 2)) == NULL)
1241 return;
1242 STRCPY(p, line);
1243 p[len] = NL;
1244 p[len + 1] = NUL;
1245 channel_send(channel, PART_IN, p, "write_buf_line()");
1246 vim_free(p);
1247}
1248
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001249/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001250 * Return TRUE if "channel" can be written to.
1251 * Returns FALSE if the input is closed or the write would block.
1252 */
1253 static int
1254can_write_buf_line(channel_T *channel)
1255{
1256 chanpart_T *in_part = &channel->ch_part[PART_IN];
1257
1258 if (in_part->ch_fd == INVALID_FD)
1259 return FALSE; /* pipe was closed */
1260
1261 /* for testing: block every other attempt to write */
1262 if (in_part->ch_block_write == 1)
1263 in_part->ch_block_write = -1;
1264 else if (in_part->ch_block_write == -1)
1265 in_part->ch_block_write = 1;
1266
1267 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1268#ifndef WIN32
1269 {
1270# if defined(HAVE_SELECT)
1271 struct timeval tval;
1272 fd_set wfds;
1273 int ret;
1274
1275 FD_ZERO(&wfds);
1276 FD_SET((int)in_part->ch_fd, &wfds);
1277 tval.tv_sec = 0;
1278 tval.tv_usec = 0;
1279 for (;;)
1280 {
1281 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1282# ifdef EINTR
1283 SOCK_ERRNO;
1284 if (ret == -1 && errno == EINTR)
1285 continue;
1286# endif
1287 if (ret <= 0 || in_part->ch_block_write == 1)
1288 {
1289 if (ret > 0)
1290 ch_log(channel, "FAKED Input not ready for writing");
1291 else
1292 ch_log(channel, "Input not ready for writing");
1293 return FALSE;
1294 }
1295 break;
1296 }
1297# else
1298 struct pollfd fds;
1299
1300 fds.fd = in_part->ch_fd;
1301 fds.events = POLLOUT;
1302 if (poll(&fds, 1, 0) <= 0)
1303 {
1304 ch_log(channel, "Input not ready for writing");
1305 return FALSE;
1306 }
1307 if (in_part->ch_block_write == 1)
1308 {
1309 ch_log(channel, "FAKED Input not ready for writing");
1310 return FALSE;
1311 }
1312# endif
1313 }
1314#endif
1315 return TRUE;
1316}
1317
1318/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001319 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001320 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001321 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001322channel_write_in(channel_T *channel)
1323{
1324 chanpart_T *in_part = &channel->ch_part[PART_IN];
1325 linenr_T lnum;
1326 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001327 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001328
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001329 if (buf == NULL || in_part->ch_buf_append)
1330 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001331 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1332 {
1333 /* buffer was wiped out or unloaded */
1334 in_part->ch_buffer = NULL;
1335 return;
1336 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001337
1338 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1339 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1340 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001341 if (!can_write_buf_line(channel))
1342 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001343 write_buf_line(buf, lnum, channel);
1344 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001345 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001346
1347 if (written == 1)
1348 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1349 else if (written > 1)
1350 ch_logn(channel, "written %d lines to channel", written);
1351
Bram Moolenaar014069a2016-03-03 22:51:40 +01001352 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001353 if (lnum > buf->b_ml.ml_line_count)
1354 {
1355 /* Writing is done, no longer need the buffer. */
1356 in_part->ch_buffer = NULL;
1357 ch_log(channel, "Finished writing all lines to channel");
1358 }
1359 else
1360 ch_logn(channel, "Still %d more lines to write",
1361 buf->b_ml.ml_line_count - lnum + 1);
1362}
1363
1364/*
1365 * Write any lines waiting to be written to a channel.
1366 */
1367 void
1368channel_write_any_lines()
1369{
1370 channel_T *channel;
1371
1372 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1373 {
1374 chanpart_T *in_part = &channel->ch_part[PART_IN];
1375
1376 if (in_part->ch_buffer != NULL)
1377 {
1378 if (in_part->ch_buf_append)
1379 channel_write_new_lines(in_part->ch_buffer);
1380 else
1381 channel_write_in(channel);
1382 }
1383 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001384}
1385
1386/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001387 * Write appended lines above the last one in "buf" to the channel.
1388 */
1389 void
1390channel_write_new_lines(buf_T *buf)
1391{
1392 channel_T *channel;
1393 int found_one = FALSE;
1394
1395 /* There could be more than one channel for the buffer, loop over all of
1396 * them. */
1397 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1398 {
1399 chanpart_T *in_part = &channel->ch_part[PART_IN];
1400 linenr_T lnum;
1401 int written = 0;
1402
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001403 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001404 {
1405 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001406 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001407 found_one = TRUE;
1408 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1409 ++lnum)
1410 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001411 if (!can_write_buf_line(channel))
1412 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001413 write_buf_line(buf, lnum, channel);
1414 ++written;
1415 }
1416
1417 if (written == 1)
1418 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1419 else if (written > 1)
1420 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001421 if (lnum < buf->b_ml.ml_line_count)
1422 ch_logn(channel, "Still %d more lines to write",
1423 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001424
1425 in_part->ch_buf_bot = lnum;
1426 }
1427 }
1428 if (!found_one)
1429 buf->b_write_to_channel = FALSE;
1430}
1431
1432/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001433 * Invoke the "callback" on channel "channel".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001434 */
1435 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001436invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1437 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001438{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001439 typval_T rettv;
1440 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001441
Bram Moolenaar77073442016-02-13 23:23:53 +01001442 argv[0].v_type = VAR_CHANNEL;
1443 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001444
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001445 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001446 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001447 clear_tv(&rettv);
1448
Bram Moolenaara3dc5e92016-03-15 23:19:14 +01001449 redraw_after_callback();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001450}
1451
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001452/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001453 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001454 * The caller must free it.
1455 * Returns NULL if there is nothing.
1456 */
1457 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001458channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001459{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001460 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001461 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001462 char_u *p;
1463
Bram Moolenaar77073442016-02-13 23:23:53 +01001464 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001465 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001466 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001467 p = node->rq_buffer;
1468 head->rq_next = node->rq_next;
1469 if (node->rq_next == NULL)
1470 head->rq_prev = NULL;
1471 else
1472 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001473 vim_free(node);
1474 return p;
1475}
1476
1477/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001478 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001479 */
1480 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001481channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001482{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001483 readq_T *head = &channel->ch_part[part].ch_head;
1484 readq_T *node = head->rq_next;
1485 long_u len = 1;
1486 char_u *res;
1487 char_u *p;
1488
1489 /* If there is only one buffer just get that one. */
1490 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1491 return channel_get(channel, part);
1492
1493 /* Concatenate everything into one buffer. */
1494 for (node = head->rq_next; node != NULL; node = node->rq_next)
1495 len += (long_u)STRLEN(node->rq_buffer);
1496 res = lalloc(len, TRUE);
1497 if (res == NULL)
1498 return NULL;
1499 *res = NUL;
1500 for (node = head->rq_next; node != NULL; node = node->rq_next)
1501 STRCAT(res, node->rq_buffer);
1502
1503 /* Free all buffers */
1504 do
1505 {
1506 p = channel_get(channel, part);
1507 vim_free(p);
1508 } while (p != NULL);
1509
1510 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001511}
1512
1513/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001514 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001515 * Returns FAIL if that is not possible.
1516 */
1517 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001518channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001519{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001520 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001521 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001522 char_u *p;
1523
Bram Moolenaar77073442016-02-13 23:23:53 +01001524 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001525 return FAIL;
1526
Bram Moolenaar77073442016-02-13 23:23:53 +01001527 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1528 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001529 if (p == NULL)
1530 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001531 STRCPY(p, node->rq_buffer);
1532 STRCAT(p, node->rq_next->rq_buffer);
1533 vim_free(node->rq_next->rq_buffer);
1534 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001535
Bram Moolenaar77073442016-02-13 23:23:53 +01001536 /* dispose of the node and its buffer */
1537 head->rq_next = node->rq_next;
1538 head->rq_next->rq_prev = NULL;
1539 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001540 vim_free(node);
1541 return OK;
1542}
1543
1544/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001545 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001546 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001547 * Returns OK or FAIL.
1548 */
1549 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001550channel_save(channel_T *channel, int part, char_u *buf, int len,
1551 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001552{
1553 readq_T *node;
1554 readq_T *head = &channel->ch_part[part].ch_head;
1555 char_u *p;
1556 int i;
1557
1558 node = (readq_T *)alloc(sizeof(readq_T));
1559 if (node == NULL)
1560 return FAIL; /* out of memory */
1561 node->rq_buffer = alloc(len + 1);
1562 if (node->rq_buffer == NULL)
1563 {
1564 vim_free(node);
1565 return FAIL; /* out of memory */
1566 }
1567
1568 if (channel->ch_part[part].ch_mode == MODE_NL)
1569 {
1570 /* Drop any CR before a NL. */
1571 p = node->rq_buffer;
1572 for (i = 0; i < len; ++i)
1573 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1574 *p++ = buf[i];
1575 *p = NUL;
1576 }
1577 else
1578 {
1579 mch_memmove(node->rq_buffer, buf, len);
1580 node->rq_buffer[len] = NUL;
1581 }
1582
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001583 if (prepend)
1584 {
1585 /* preend node to the head of the queue */
1586 node->rq_next = head->rq_next;
1587 node->rq_prev = NULL;
1588 if (head->rq_next == NULL)
1589 head->rq_prev = node;
1590 else
1591 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001592 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001593 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001594 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001595 {
1596 /* append node to the tail of the queue */
1597 node->rq_next = NULL;
1598 node->rq_prev = head->rq_prev;
1599 if (head->rq_prev == NULL)
1600 head->rq_next = node;
1601 else
1602 head->rq_prev->rq_next = node;
1603 head->rq_prev = node;
1604 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001605
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001606 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001607 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001608 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001609 fprintf(log_fd, "'");
1610 if (fwrite(buf, len, 1, log_fd) != 1)
1611 return FAIL;
1612 fprintf(log_fd, "'\n");
1613 }
1614 return OK;
1615}
1616
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001617 static int
1618channel_fill(js_read_T *reader)
1619{
1620 channel_T *channel = (channel_T *)reader->js_cookie;
1621 int part = reader->js_cookie_arg;
1622 char_u *next = channel_get(channel, part);
1623 int unused;
1624 int len;
1625 char_u *p;
1626
1627 if (next == NULL)
1628 return FALSE;
1629
1630 unused = reader->js_end - reader->js_buf - reader->js_used;
1631 if (unused > 0)
1632 {
1633 /* Prepend unused text. */
1634 len = (int)STRLEN(next);
1635 p = alloc(unused + len + 1);
1636 if (p == NULL)
1637 {
1638 vim_free(next);
1639 return FALSE;
1640 }
1641 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1642 mch_memmove(p + unused, next, len + 1);
1643 vim_free(next);
1644 next = p;
1645 }
1646
1647 vim_free(reader->js_buf);
1648 reader->js_buf = next;
1649 reader->js_used = 0;
1650 return TRUE;
1651}
1652
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001653/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001654 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001655 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001656 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001657 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001658 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001659channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001660{
1661 js_read_T reader;
1662 typval_T listtv;
1663 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001664 chanpart_T *chanpart = &channel->ch_part[part];
1665 jsonq_T *head = &chanpart->ch_json_head;
1666 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001667 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001668
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001669 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001670 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001671
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001672 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001674 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001675 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001676 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001677
1678 /* When a message is incomplete we wait for a short while for more to
1679 * arrive. After the delay drop the input, otherwise a truncated string
1680 * or list will make us hang. */
1681 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001682 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001683 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001684 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001685 /* Only accept the response when it is a list with at least two
1686 * items. */
1687 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001688 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001689 if (listtv.v_type != VAR_LIST)
1690 ch_error(channel, "Did not receive a list, discarding");
1691 else
1692 ch_errorn(channel, "Expected list with two items, got %d",
1693 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001694 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001695 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001696 else
1697 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001698 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1699 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001700 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001701 else
1702 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001703 item->jq_value = alloc_tv();
1704 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001705 {
1706 vim_free(item);
1707 clear_tv(&listtv);
1708 }
1709 else
1710 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001711 *item->jq_value = listtv;
1712 item->jq_prev = head->jq_prev;
1713 head->jq_prev = item;
1714 item->jq_next = NULL;
1715 if (item->jq_prev == NULL)
1716 head->jq_next = item;
1717 else
1718 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001719 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001720 }
1721 }
1722 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001723
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001724 if (status == OK)
1725 chanpart->ch_waiting = FALSE;
1726 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001727 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001728 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001729 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001730 /* First time encountering incomplete message, set a deadline of
1731 * 100 msec. */
1732 ch_log(channel, "Incomplete message - wait for more");
1733 reader.js_used = 0;
1734 chanpart->ch_waiting = TRUE;
1735#ifdef WIN32
1736 chanpart->ch_deadline = GetTickCount() + 100L;
1737#else
1738 gettimeofday(&chanpart->ch_deadline, NULL);
1739 chanpart->ch_deadline.tv_usec += 100 * 1000;
1740 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1741 {
1742 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1743 ++chanpart->ch_deadline.tv_sec;
1744 }
1745#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001746 }
1747 else
1748 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001749 int timeout;
1750#ifdef WIN32
1751 timeout = GetTickCount() > chanpart->ch_deadline;
1752#else
1753 {
1754 struct timeval now_tv;
1755
1756 gettimeofday(&now_tv, NULL);
1757 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1758 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1759 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1760 }
1761#endif
1762 if (timeout)
1763 {
1764 status = FAIL;
1765 chanpart->ch_waiting = FALSE;
1766 }
1767 else
1768 {
1769 reader.js_used = 0;
1770 ch_log(channel, "still waiting on incomplete message");
1771 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001772 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001773 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001774
1775 if (status == FAIL)
1776 {
1777 ch_error(channel, "Decoding failed - discarding input");
1778 ret = FALSE;
1779 chanpart->ch_waiting = FALSE;
1780 }
1781 else if (reader.js_buf[reader.js_used] != NUL)
1782 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001783 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001784 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001785 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1786 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001787 ret = status == MAYBE ? FALSE: TRUE;
1788 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001789 else
1790 ret = FALSE;
1791
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001792 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001793 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001794}
1795
1796/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001797 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001798 */
1799 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001800remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001801{
Bram Moolenaar77073442016-02-13 23:23:53 +01001802 if (node->cq_prev == NULL)
1803 head->cq_next = node->cq_next;
1804 else
1805 node->cq_prev->cq_next = node->cq_next;
1806 if (node->cq_next == NULL)
1807 head->cq_prev = node->cq_prev;
1808 else
1809 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001810}
1811
1812/*
1813 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001814 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001815 */
1816 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001817remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001818{
Bram Moolenaar77073442016-02-13 23:23:53 +01001819 if (node->jq_prev == NULL)
1820 head->jq_next = node->jq_next;
1821 else
1822 node->jq_prev->jq_next = node->jq_next;
1823 if (node->jq_next == NULL)
1824 head->jq_prev = node->jq_prev;
1825 else
1826 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001827 vim_free(node);
1828}
1829
1830/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001831 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001832 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001833 * When "id" is zero or negative jut get the first message. But not the one
1834 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001835 * Return OK when found and return the value in "rettv".
1836 * Return FAIL otherwise.
1837 */
1838 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001839channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001840{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001841 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001842 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001843
Bram Moolenaar77073442016-02-13 23:23:53 +01001844 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001845 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001846 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001847 typval_T *tv = &l->lv_first->li_tv;
1848
1849 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001850 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001851 || tv->vval.v_number == 0
1852 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001853 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001854 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001855 if (tv->v_type == VAR_NUMBER)
1856 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001857 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001858 return OK;
1859 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001860 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001861 }
1862 return FAIL;
1863}
1864
Bram Moolenaarece61b02016-02-20 21:39:05 +01001865#define CH_JSON_MAX_ARGS 4
1866
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001867/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001868 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001869 * "argv[0]" is the command string.
1870 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001871 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001872 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001873channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001874{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001875 char_u *cmd = argv[0].vval.v_string;
1876 char_u *arg;
1877 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001878
Bram Moolenaarece61b02016-02-20 21:39:05 +01001879 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001880 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001881 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001882 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001883 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001884 return;
1885 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001886 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001887 if (arg == NULL)
1888 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001889
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001890 if (STRCMP(cmd, "ex") == 0)
1891 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001892 int save_called_emsg = called_emsg;
1893
1894 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001895 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001896 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001897 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001898 --emsg_silent;
1899 if (called_emsg)
1900 ch_logs(channel, "Ex command error: '%s'",
1901 (char *)get_vim_var_str(VV_ERRMSG));
1902 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001903 }
1904 else if (STRCMP(cmd, "normal") == 0)
1905 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001906 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001907
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001908 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001909 ea.arg = arg;
1910 ea.addr_count = 0;
1911 ea.forceit = TRUE; /* no mapping */
1912 ex_normal(&ea);
1913 }
1914 else if (STRCMP(cmd, "redraw") == 0)
1915 {
1916 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001917
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001918 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001919 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001920 ex_redraw(&ea);
1921 showruler(FALSE);
1922 setcursor();
1923 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001924#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001925 if (gui.in_use)
1926 {
1927 gui_update_cursor(FALSE, FALSE);
1928 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001929 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001930#endif
1931 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001932 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001933 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001934 int is_call = cmd[0] == 'c';
1935 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001936
Bram Moolenaarece61b02016-02-20 21:39:05 +01001937 if (argv[id_idx].v_type != VAR_UNKNOWN
1938 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001939 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001940 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001941 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001942 EMSG("E904: last argument for expr/call must be a number");
1943 }
1944 else if (is_call && argv[2].v_type != VAR_LIST)
1945 {
1946 ch_error(channel, "third argument for call must be a list");
1947 if (p_verbose > 2)
1948 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001949 }
1950 else
1951 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001952 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001953 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001954 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001955 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001956
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001957 /* Don't pollute the display with errors. */
1958 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001959 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001960 {
1961 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001962 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001963 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001964 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001965 {
1966 ch_logs(channel, "Calling '%s'", (char *)arg);
1967 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1968 tv = &res_tv;
1969 else
1970 tv = NULL;
1971 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001972
1973 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001974 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001975 int id = argv[id_idx].vval.v_number;
1976
Bram Moolenaar55fab432016-02-07 16:53:13 +01001977 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001978 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001979 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001980 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001981 /* If evaluation failed or the result can't be encoded
1982 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001983 vim_free(json);
1984 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001985 err_tv.v_type = VAR_STRING;
1986 err_tv.vval.v_string = (char_u *)"ERROR";
1987 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001988 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001989 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001990 if (json != NULL)
1991 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001992 channel_send(channel,
1993 part == PART_SOCK ? PART_SOCK : PART_IN,
1994 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001995 vim_free(json);
1996 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001997 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001998 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001999 if (tv == &res_tv)
2000 clear_tv(tv);
2001 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002002 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002003 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002004 }
2005 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002006 {
2007 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002008 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002009 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002010}
2011
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002012 static void
2013invoke_one_time_callback(
2014 channel_T *channel,
2015 cbq_T *cbhead,
2016 cbq_T *item,
2017 typval_T *argv)
2018{
2019 ch_logs(channel, "Invoking one-time callback %s",
2020 (char *)item->cq_callback);
2021 /* Remove the item from the list first, if the callback
2022 * invokes ch_close() the list will be cleared. */
2023 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002024 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002025 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002026 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002027 vim_free(item);
2028}
2029
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002030 static void
2031append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2032{
2033 buf_T *save_curbuf = curbuf;
2034 linenr_T lnum = buffer->b_ml.ml_line_count;
2035 int save_write_to = buffer->b_write_to_channel;
2036
2037 /* If the buffer is also used as input insert above the last
2038 * line. Don't write these lines. */
2039 if (save_write_to)
2040 {
2041 --lnum;
2042 buffer->b_write_to_channel = FALSE;
2043 }
2044
2045 /* Append to the buffer */
2046 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2047
2048 curbuf = buffer;
2049 u_sync(TRUE);
2050 /* ignore undo failure, undo is not very useful here */
2051 ignored = u_save(lnum, lnum + 1);
2052
2053 ml_append(lnum, msg, 0, FALSE);
2054 appended_lines_mark(lnum, 1L);
2055 curbuf = save_curbuf;
2056
2057 if (buffer->b_nwindows > 0)
2058 {
2059 win_T *wp;
2060 win_T *save_curwin;
2061
2062 FOR_ALL_WINDOWS(wp)
2063 {
2064 if (wp->w_buffer == buffer
2065 && (save_write_to
2066 ? wp->w_cursor.lnum == lnum + 1
2067 : (wp->w_cursor.lnum == lnum
2068 && wp->w_cursor.col == 0)))
2069 {
2070 ++wp->w_cursor.lnum;
2071 save_curwin = curwin;
2072 curwin = wp;
2073 curbuf = curwin->w_buffer;
2074 scroll_cursor_bot(0, FALSE);
2075 curwin = save_curwin;
2076 curbuf = curwin->w_buffer;
2077 }
2078 }
2079 redraw_buf_later(buffer, VALID);
2080 channel_need_redraw = TRUE;
2081 }
2082
2083 if (save_write_to)
2084 {
2085 channel_T *ch;
2086
2087 /* Find channels reading from this buffer and adjust their
2088 * next-to-read line number. */
2089 buffer->b_write_to_channel = TRUE;
2090 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2091 {
2092 chanpart_T *in_part = &ch->ch_part[PART_IN];
2093
2094 if (in_part->ch_buffer == buffer)
2095 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2096 }
2097 }
2098}
2099
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002100/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002101 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002102 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002103 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002104 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002105may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002106{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002107 char_u *msg = NULL;
2108 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002109 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002110 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002111 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002112 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002113 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002114 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002115 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002116 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002117
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002118 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002119 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002120 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002121
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002122 /* Use a message-specific callback, part callback or channel callback */
2123 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2124 if (cbitem->cq_seq_nr == 0)
2125 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002126 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002127 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002128 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002129 partial = cbitem->cq_partial;
2130 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002131 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002132 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002133 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002134 partial = channel->ch_part[part].ch_partial;
2135 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002136 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002137 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002138 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002139 partial = channel->ch_partial;
2140 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002141
Bram Moolenaar187db502016-02-27 14:44:26 +01002142 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002143 if (buffer != NULL && !buf_valid(buffer))
2144 {
2145 /* buffer was wiped out */
2146 channel->ch_part[part].ch_buffer = NULL;
2147 buffer = NULL;
2148 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002149
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002150 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002151 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002152 listitem_T *item;
2153 int argc = 0;
2154
Bram Moolenaard7ece102016-02-02 23:23:02 +01002155 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002156 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002157 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002158 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002159 channel_parse_json(channel, part);
2160 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002161 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002162 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002163
Bram Moolenaarece61b02016-02-20 21:39:05 +01002164 for (item = listtv->vval.v_list->lv_first;
2165 item != NULL && argc < CH_JSON_MAX_ARGS;
2166 item = item->li_next)
2167 argv[argc++] = item->li_tv;
2168 while (argc < CH_JSON_MAX_ARGS)
2169 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002170
Bram Moolenaarece61b02016-02-20 21:39:05 +01002171 if (argv[0].v_type == VAR_STRING)
2172 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002173 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002174 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002175 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002176 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002177 }
2178
Bram Moolenaarece61b02016-02-20 21:39:05 +01002179 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002180 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002181 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002182 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002183 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002184 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002185 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002186 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002187 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002188 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002189 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002190 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002191 return FALSE;
2192 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002193 else
2194 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002195 /* If there is no callback or buffer drop the message. */
2196 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002197 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002198 while ((msg = channel_get(channel, part)) != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002199 {
2200 ch_logs(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002201 vim_free(msg);
Bram Moolenaard6051b52016-02-28 15:49:03 +01002202 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002203 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002204 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002205
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002206 if (ch_mode == MODE_NL)
2207 {
2208 char_u *nl;
2209 char_u *buf;
2210
2211 /* See if we have a message ending in NL in the first buffer. If
2212 * not try to concatenate the first and the second buffer. */
2213 while (TRUE)
2214 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002215 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002216 nl = vim_strchr(buf, NL);
2217 if (nl != NULL)
2218 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002219 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002220 return FALSE; /* incomplete message */
2221 }
2222 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002223 {
2224 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002225 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002226 *nl = NUL;
2227 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002228 else
2229 {
2230 /* Copy the message into allocated memory and remove it from
2231 * the buffer. */
2232 msg = vim_strnsave(buf, (int)(nl - buf));
2233 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2234 }
2235 }
2236 else
2237 /* For a raw channel we don't know where the message ends, just
2238 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002239 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002240
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002241 if (msg == NULL)
2242 return FALSE; /* out of memory (and avoids Coverity warning) */
2243
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002244 argv[1].v_type = VAR_STRING;
2245 argv[1].vval.v_string = msg;
2246 }
2247
Bram Moolenaara07fec92016-02-05 21:04:08 +01002248 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002249 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002250 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002251
2252 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002253 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002254 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002255 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002256 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002257 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002258 break;
2259 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002260 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002261 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002262 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002263 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002264 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002265 if (buffer != NULL)
2266 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002267 if (msg == NULL)
2268 /* JSON or JS mode: re-encode the message. */
2269 msg = json_encode(listtv, ch_mode);
2270 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002271 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002272 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002273
Bram Moolenaar187db502016-02-27 14:44:26 +01002274 if (callback != NULL)
2275 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002276 if (cbitem != NULL)
2277 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2278 else
2279 {
2280 /* invoke the channel callback */
2281 ch_logs(channel, "Invoking channel callback %s",
2282 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002283 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002284 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002285 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002286 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002287 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002288 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002289
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002290 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002291 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002292 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002293
2294 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002295}
2296
2297/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002298 * Return TRUE when channel "channel" is open for writing to.
2299 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002300 */
2301 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002302channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002303{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002304 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002305 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002306}
2307
2308/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002309 * Return TRUE when channel "channel" is open for reading or writing.
2310 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002311 */
2312 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002313channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002314{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002315 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002316 || channel->CH_IN_FD != INVALID_FD
2317 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002318 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002319}
2320
2321/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002322 * Return a string indicating the status of the channel.
2323 */
2324 char *
2325channel_status(channel_T *channel)
2326{
2327 if (channel == NULL)
2328 return "fail";
2329 if (channel_is_open(channel))
2330 return "open";
2331 return "closed";
2332}
2333
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002334 static void
2335channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2336{
2337 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002338 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002339 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002340 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002341
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002342 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002343 STRCAT(namebuf, "_");
2344 tail = STRLEN(namebuf);
2345
2346 STRCPY(namebuf + tail, "status");
2347 dict_add_nr_str(dict, namebuf, 0,
2348 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2349
2350 STRCPY(namebuf + tail, "mode");
2351 switch (chanpart->ch_mode)
2352 {
2353 case MODE_NL: s = "NL"; break;
2354 case MODE_RAW: s = "RAW"; break;
2355 case MODE_JSON: s = "JSON"; break;
2356 case MODE_JS: s = "JS"; break;
2357 }
2358 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2359
2360 STRCPY(namebuf + tail, "io");
2361 if (part == PART_SOCK)
2362 s = "socket";
2363 else switch (chanpart->ch_io)
2364 {
2365 case JIO_NULL: s = "null"; break;
2366 case JIO_PIPE: s = "pipe"; break;
2367 case JIO_FILE: s = "file"; break;
2368 case JIO_BUFFER: s = "buffer"; break;
2369 case JIO_OUT: s = "out"; break;
2370 }
2371 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2372
2373 STRCPY(namebuf + tail, "timeout");
2374 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2375}
2376
2377 void
2378channel_info(channel_T *channel, dict_T *dict)
2379{
2380 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2381 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2382
2383 if (channel->ch_hostname != NULL)
2384 {
2385 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2386 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2387 channel_part_info(channel, dict, "sock", PART_SOCK);
2388 }
2389 else
2390 {
2391 channel_part_info(channel, dict, "out", PART_OUT);
2392 channel_part_info(channel, dict, "err", PART_ERR);
2393 channel_part_info(channel, dict, "in", PART_IN);
2394 }
2395}
2396
Bram Moolenaar77073442016-02-13 23:23:53 +01002397/*
2398 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002399 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002400 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002401 */
2402 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002403channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002404{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002405 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002406
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002407#ifdef FEAT_GUI
2408 channel_gui_unregister(channel);
2409#endif
2410
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002411 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002412 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002413 sock_close(channel->CH_SOCK_FD);
2414 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002415 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002416 may_close_part(&channel->CH_IN_FD);
2417 may_close_part(&channel->CH_OUT_FD);
2418 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002419
Bram Moolenaar8b374212016-02-24 20:43:06 +01002420 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002421 {
2422 typval_T argv[1];
2423 typval_T rettv;
2424 int dummy;
2425
2426 /* invoke the close callback; increment the refcount to avoid it
2427 * being freed halfway */
Bram Moolenaard6051b52016-02-28 15:49:03 +01002428 ch_logs(channel, "Invoking close callback %s",
2429 (char *)channel->ch_close_cb);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002430 argv[0].v_type = VAR_CHANNEL;
2431 argv[0].vval.v_channel = channel;
2432 ++channel->ch_refcount;
2433 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002434 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2435 channel->ch_close_partial, NULL);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002436 clear_tv(&rettv);
2437 --channel->ch_refcount;
2438
2439 /* the callback is only called once */
2440 vim_free(channel->ch_close_cb);
2441 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002442 partial_unref(channel->ch_close_partial);
2443 channel->ch_close_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002444 }
2445
2446 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002447}
2448
Bram Moolenaard04a0202016-01-26 23:30:18 +01002449/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002450 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002451 * Returns NULL if there is nothing.
2452 */
2453 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002454channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002455{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002456 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002457
Bram Moolenaar77073442016-02-13 23:23:53 +01002458 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002459 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002460 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002461}
2462
2463/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002464 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002465 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002466 static void
2467channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002468{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002469 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2470 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002471
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002472 while (channel_peek(channel, part) != NULL)
2473 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002474
2475 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002476 {
2477 cbq_T *node = cb_head->cq_next;
2478
2479 remove_cb_node(cb_head, node);
2480 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002481 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002482 vim_free(node);
2483 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002484
2485 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002486 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002487 free_tv(json_head->jq_next->jq_value);
2488 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002489 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002490
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002491 vim_free(channel->ch_part[part].ch_callback);
2492 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002493 partial_unref(channel->ch_part[part].ch_partial);
2494 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002495}
2496
2497/*
2498 * Clear all the read buffers on "channel".
2499 */
2500 void
2501channel_clear(channel_T *channel)
2502{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002503 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002504 vim_free(channel->ch_hostname);
2505 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002506 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002507 channel_clear_one(channel, PART_OUT);
2508 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002509 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002510 vim_free(channel->ch_callback);
2511 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002512 partial_unref(channel->ch_partial);
2513 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002514 vim_free(channel->ch_close_cb);
2515 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002516 partial_unref(channel->ch_close_partial);
2517 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002518}
2519
Bram Moolenaar77073442016-02-13 23:23:53 +01002520#if defined(EXITFREE) || defined(PROTO)
2521 void
2522channel_free_all(void)
2523{
2524 channel_T *channel;
2525
Bram Moolenaard6051b52016-02-28 15:49:03 +01002526 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002527 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2528 channel_clear(channel);
2529}
2530#endif
2531
2532
Bram Moolenaard04a0202016-01-26 23:30:18 +01002533/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002534#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002535
2536/* Buffer size for reading incoming messages. */
2537#define MAXMSGSIZE 4096
2538
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002539#if defined(HAVE_SELECT)
2540/*
2541 * Add write fds where we are waiting for writing to be possible.
2542 */
2543 static int
2544channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2545{
2546 int maxfd = maxfd_arg;
2547 channel_T *ch;
2548
2549 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2550 {
2551 chanpart_T *in_part = &ch->ch_part[PART_IN];
2552
2553 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2554 {
2555 FD_SET((int)in_part->ch_fd, wfds);
2556 if ((int)in_part->ch_fd >= maxfd)
2557 maxfd = (int)in_part->ch_fd + 1;
2558 }
2559 }
2560 return maxfd;
2561}
2562#else
2563/*
2564 * Add write fds where we are waiting for writing to be possible.
2565 */
2566 static int
2567channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2568{
2569 int nfd = nfd_in;
2570 channel_T *ch;
2571
2572 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2573 {
2574 chanpart_T *in_part = &ch->ch_part[PART_IN];
2575
2576 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2577 {
2578 in_part->ch_poll_idx = nfd;
2579 fds[nfd].fd = in_part->ch_fd;
2580 fds[nfd].events = POLLOUT;
2581 ++nfd;
2582 }
2583 else
2584 in_part->ch_poll_idx = -1;
2585 }
2586 return nfd;
2587}
2588#endif
2589
Bram Moolenaard04a0202016-01-26 23:30:18 +01002590/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002591 * Check for reading from "fd" with "timeout" msec.
2592 * Return FAIL when there is nothing to read.
2593 */
2594 static int
Bram Moolenaard8070362016-02-15 21:56:54 +01002595channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002596{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002597 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002598 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002599
Bram Moolenaard8070362016-02-15 21:56:54 +01002600# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002601 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002602 {
2603 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002604 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002605 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002606 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002607
2608 /* reading from a pipe, not a socket */
2609 while (TRUE)
2610 {
Bram Moolenaare74e8e72016-02-16 22:01:30 +01002611 if (PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL)
2612 && nread > 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002613 return OK;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002614
2615 /* perhaps write some buffer lines */
2616 channel_write_any_lines();
2617
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002618 sleep_time = deadline - GetTickCount();
2619 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002620 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002621 /* Wait for a little while. Very short at first, up to 10 msec
2622 * after looping a few times. */
2623 if (sleep_time > delay)
2624 sleep_time = delay;
2625 Sleep(sleep_time);
2626 delay = delay * 2;
2627 if (delay > 10)
2628 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002629 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002630 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002631 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002632#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002633 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002634#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002635 struct timeval tval;
2636 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002637 fd_set wfds;
2638 int ret;
2639 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002640
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002641 tval.tv_sec = timeout / 1000;
2642 tval.tv_usec = (timeout % 1000) * 1000;
2643 for (;;)
2644 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002645 FD_ZERO(&rfds);
2646 FD_SET((int)fd, &rfds);
2647
2648 /* Write lines to a pipe when a pipe can be written to. Need to
2649 * set this every time, some buffers may be done. */
2650 maxfd = (int)fd + 1;
2651 FD_ZERO(&wfds);
2652 maxfd = channel_fill_wfds(maxfd, &wfds);
2653
2654 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002655# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002656 SOCK_ERRNO;
2657 if (ret == -1 && errno == EINTR)
2658 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002659# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002660 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002661 {
2662 if (FD_ISSET(fd, &rfds))
2663 return OK;
2664 channel_write_any_lines();
2665 continue;
2666 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002667 break;
2668 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002669#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002670 for (;;)
2671 {
2672 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2673 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002674
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002675 fds[0].fd = fd;
2676 fds[0].events = POLLIN;
2677 nfd = channel_fill_poll_write(nfd, fds);
2678 if (poll(fds, nfd, timeout) > 0)
2679 {
2680 if (fds[0].revents & POLLIN)
2681 return OK;
2682 channel_write_any_lines();
2683 continue;
2684 }
2685 break;
2686 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002687#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002688 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002689 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002690}
2691
2692/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002693 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002694 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002695 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002696 */
2697 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002698channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002699{
2700 static char_u *buf = NULL;
2701 int len = 0;
2702 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002703 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002704 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002705
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002706 fd = channel->ch_part[part].ch_fd;
2707 if (fd == INVALID_FD)
2708 {
2709 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002710 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002711 }
2712 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002713
2714 /* Allocate a buffer to read into. */
2715 if (buf == NULL)
2716 {
2717 buf = alloc(MAXMSGSIZE);
2718 if (buf == NULL)
2719 return; /* out of memory! */
2720 }
2721
2722 /* Keep on reading for as long as there is something to read.
2723 * Use select() or poll() to avoid blocking on a message that is exactly
2724 * MAXMSGSIZE long. */
2725 for (;;)
2726 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002727 if (channel_wait(channel, fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002728 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002729 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002730 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002731 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002732 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002733 if (len <= 0)
2734 break; /* error or nothing more to read */
2735
2736 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002737 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002738 readlen += len;
2739 if (len < MAXMSGSIZE)
2740 break; /* did read everything that's available */
2741 }
2742
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002743 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002744 if (readlen <= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002745 {
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002746 /* Do not give an error message, most likely the other end just
2747 * exited. */
2748 ch_errors(channel, "%s(): Cannot read from channel", func);
2749
Bram Moolenaard04a0202016-01-26 23:30:18 +01002750 /* Queue a "DETACH" netbeans message in the command queue in order to
2751 * terminate the netbeans session later. Do not end the session here
2752 * directly as we may be running in the context of a call to
2753 * netbeans_parse_messages():
2754 * netbeans_parse_messages
2755 * -> autocmd triggered while processing the netbeans cmd
2756 * -> ui_breakcheck
2757 * -> gui event loop or select loop
2758 * -> channel_read()
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002759 * Don't send "DETACH" for a JS or JSON channel.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002760 */
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002761 if (channel->ch_part[part].ch_mode == MODE_RAW
2762 || channel->ch_part[part].ch_mode == MODE_NL)
2763 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002764 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002765
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02002766 /* When reading from stdout is not possible, assume the other side has
2767 * died. */
Bram Moolenaar8b374212016-02-24 20:43:06 +01002768 channel_close(channel, TRUE);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002769 if (channel->ch_nb_close_cb != NULL)
2770 (*channel->ch_nb_close_cb)();
Bram Moolenaard04a0202016-01-26 23:30:18 +01002771 }
2772
2773#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002774 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002775 if (CH_HAS_GUI && gtk_main_level() > 0)
2776 gtk_main_quit();
2777#endif
2778}
2779
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002780/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002781 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002782 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002783 * Returns what was read in allocated memory.
2784 * Returns NULL in case of error or timeout.
2785 */
2786 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002787channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002788{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002789 char_u *buf;
2790 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002791 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002792 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002793 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002794
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002795 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002796 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002797
2798 while (TRUE)
2799 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002800 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002801 if (buf != NULL && (mode == MODE_RAW
2802 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2803 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002804 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002805 continue;
2806
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002807 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002808 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002809 return NULL;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002810 if (channel_wait(channel, fd, timeout) == FAIL)
2811 {
2812 ch_log(channel, "Timed out");
2813 return NULL;
2814 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002815 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002816 }
2817
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002818 if (mode == MODE_RAW)
2819 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002820 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002821 }
2822 else
2823 {
2824 nl = vim_strchr(buf, NL);
2825 if (nl[1] == NUL)
2826 {
2827 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002828 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002829 *nl = NUL;
2830 }
2831 else
2832 {
2833 /* Copy the message into allocated memory and remove it from the
2834 * buffer. */
2835 msg = vim_strnsave(buf, (int)(nl - buf));
2836 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2837 }
2838 }
2839 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002840 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002841 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002842}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002843
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002844/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002845 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002846 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002847 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002848 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002849 */
2850 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002851channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002852 channel_T *channel,
2853 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002854 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002855 int id,
2856 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002857{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002858 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002859 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002860 int timeout;
2861 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002862
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002863 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002864 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002865 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002866 for (;;)
2867 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002868 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002869
2870 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002871 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002872 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002873 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002874 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002875 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002876
Bram Moolenaard7ece102016-02-02 23:23:02 +01002877 if (!more)
2878 {
2879 /* Handle any other messages in the queue. If done some more
2880 * messages may have arrived. */
2881 if (channel_parse_messages())
2882 continue;
2883
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002884 /* Wait for up to the timeout. If there was an incomplete message
2885 * use the deadline for that. */
2886 timeout = timeout_arg;
2887 if (chanpart->ch_waiting)
2888 {
2889#ifdef WIN32
2890 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2891#else
2892 {
2893 struct timeval now_tv;
2894
2895 gettimeofday(&now_tv, NULL);
2896 timeout = (chanpart->ch_deadline.tv_sec
2897 - now_tv.tv_sec) * 1000
2898 + (chanpart->ch_deadline.tv_usec
2899 - now_tv.tv_usec) / 1000
2900 + 1;
2901 }
2902#endif
2903 if (timeout < 0)
2904 {
2905 /* Something went wrong, channel_parse_json() didn't
2906 * discard message. Cancel waiting. */
2907 chanpart->ch_waiting = FALSE;
2908 timeout = timeout_arg;
2909 }
2910 else if (timeout > timeout_arg)
2911 timeout = timeout_arg;
2912 }
2913 fd = chanpart->ch_fd;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002914 if (fd == INVALID_FD || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002915 {
2916 if (timeout == timeout_arg)
2917 {
2918 if (fd != INVALID_FD)
2919 ch_log(channel, "Timed out");
2920 break;
2921 }
2922 }
2923 else
2924 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01002925 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002926 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002927 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002928 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002929}
2930
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002931/*
2932 * Common for ch_read() and ch_readraw().
2933 */
2934 void
2935common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
2936{
2937 channel_T *channel;
2938 int part;
2939 jobopt_T opt;
2940 int mode;
2941 int timeout;
2942 int id = -1;
2943 typval_T *listtv = NULL;
2944
2945 /* return an empty string by default */
2946 rettv->v_type = VAR_STRING;
2947 rettv->vval.v_string = NULL;
2948
2949 clear_job_options(&opt);
2950 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
2951 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02002952 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002953
2954 channel = get_channel_arg(&argvars[0], TRUE);
2955 if (channel != NULL)
2956 {
2957 if (opt.jo_set & JO_PART)
2958 part = opt.jo_part;
2959 else
2960 part = channel_part_read(channel);
2961 mode = channel_get_mode(channel, part);
2962 timeout = channel_get_timeout(channel, part);
2963 if (opt.jo_set & JO_TIMEOUT)
2964 timeout = opt.jo_timeout;
2965
2966 if (raw || mode == MODE_RAW || mode == MODE_NL)
2967 rettv->vval.v_string = channel_read_block(channel, part, timeout);
2968 else
2969 {
2970 if (opt.jo_set & JO_ID)
2971 id = opt.jo_id;
2972 channel_read_json_block(channel, part, timeout, id, &listtv);
2973 if (listtv != NULL)
2974 {
2975 *rettv = *listtv;
2976 vim_free(listtv);
2977 }
2978 else
2979 {
2980 rettv->v_type = VAR_SPECIAL;
2981 rettv->vval.v_number = VVAL_NONE;
2982 }
2983 }
2984 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02002985
2986theend:
2987 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002988}
2989
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002990# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
2991 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002992/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002993 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01002994 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002995 */
Bram Moolenaar77073442016-02-13 23:23:53 +01002996 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002997channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002998{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002999 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003000 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003001
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003002 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003003 for (channel = first_channel; channel != NULL;
3004 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003005 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003006 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003007 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003008 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003009 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003010 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003011 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003012 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003013 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003014}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003015# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003016
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003017# if defined(WIN32) || defined(PROTO)
3018/*
3019 * Check the channels for anything that is ready to be read.
3020 * The data is put in the read queue.
3021 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003022 void
3023channel_handle_events(void)
3024{
3025 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003026 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003027 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003028
3029 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3030 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003031 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003032 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003033 {
3034 fd = channel->ch_part[part].ch_fd;
3035 if (fd != INVALID_FD && channel_wait(channel, fd, 0) == OK)
3036 channel_read(channel, part, "channel_handle_events");
3037 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003038 }
3039}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003040# endif
3041
Bram Moolenaard04a0202016-01-26 23:30:18 +01003042/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003043 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003044 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003045 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003046 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003047 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003048channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003049{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003050 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003051 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003052 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003053
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003054 fd = channel->ch_part[part].ch_fd;
3055 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003056 {
3057 if (!channel->ch_error && fun != NULL)
3058 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003059 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003060 EMSG2("E630: %s(): write while not connected", fun);
3061 }
3062 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003063 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003064 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003065
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003066 if (log_fd != NULL)
3067 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003068 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003069 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003070 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003071 fprintf(log_fd, "'\n");
3072 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003073 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003074 }
3075
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003076 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003077 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003078 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003079 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003080 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003081 {
3082 if (!channel->ch_error && fun != NULL)
3083 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003084 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003085 EMSG2("E631: %s(): write failed", fun);
3086 }
3087 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003088 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003089 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003090
3091 channel->ch_error = FALSE;
3092 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003093}
3094
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003095/*
3096 * Common for "ch_sendexpr()" and "ch_sendraw()".
3097 * Returns the channel if the caller should read the response.
3098 * Sets "part_read" to the the read fd.
3099 * Otherwise returns NULL.
3100 */
3101 channel_T *
3102send_common(
3103 typval_T *argvars,
3104 char_u *text,
3105 int id,
3106 int eval,
3107 jobopt_T *opt,
3108 char *fun,
3109 int *part_read)
3110{
3111 channel_T *channel;
3112 int part_send;
3113
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003114 clear_job_options(opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003115 channel = get_channel_arg(&argvars[0], TRUE);
3116 if (channel == NULL)
3117 return NULL;
3118 part_send = channel_part_send(channel);
3119 *part_read = channel_part_read(channel);
3120
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003121 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3122 return NULL;
3123
3124 /* Set the callback. An empty callback means no callback and not reading
3125 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3126 * allowed. */
3127 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3128 {
3129 if (eval)
3130 {
3131 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3132 return NULL;
3133 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003134 channel_set_req_callback(channel, part_send,
3135 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003136 }
3137
3138 if (channel_send(channel, part_send, text, fun) == OK
3139 && opt->jo_callback == NULL)
3140 return channel;
3141 return NULL;
3142}
3143
3144/*
3145 * common for "ch_evalexpr()" and "ch_sendexpr()"
3146 */
3147 void
3148ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3149{
3150 char_u *text;
3151 typval_T *listtv;
3152 channel_T *channel;
3153 int id;
3154 ch_mode_T ch_mode;
3155 int part_send;
3156 int part_read;
3157 jobopt_T opt;
3158 int timeout;
3159
3160 /* return an empty string by default */
3161 rettv->v_type = VAR_STRING;
3162 rettv->vval.v_string = NULL;
3163
3164 channel = get_channel_arg(&argvars[0], TRUE);
3165 if (channel == NULL)
3166 return;
3167 part_send = channel_part_send(channel);
3168
3169 ch_mode = channel_get_mode(channel, part_send);
3170 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3171 {
3172 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3173 return;
3174 }
3175
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003176 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003177 text = json_encode_nr_expr(id, &argvars[1],
3178 ch_mode == MODE_JS ? JSON_JS : 0);
3179 if (text == NULL)
3180 return;
3181
3182 channel = send_common(argvars, text, id, eval, &opt,
3183 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3184 vim_free(text);
3185 if (channel != NULL && eval)
3186 {
3187 if (opt.jo_set & JO_TIMEOUT)
3188 timeout = opt.jo_timeout;
3189 else
3190 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003191 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3192 == OK)
3193 {
3194 list_T *list = listtv->vval.v_list;
3195
3196 /* Move the item from the list and then change the type to
3197 * avoid the value being freed. */
3198 *rettv = list->lv_last->li_tv;
3199 list->lv_last->li_tv.v_type = VAR_NUMBER;
3200 free_tv(listtv);
3201 }
3202 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003203 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003204}
3205
3206/*
3207 * common for "ch_evalraw()" and "ch_sendraw()"
3208 */
3209 void
3210ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3211{
3212 char_u buf[NUMBUFLEN];
3213 char_u *text;
3214 channel_T *channel;
3215 int part_read;
3216 jobopt_T opt;
3217 int timeout;
3218
3219 /* return an empty string by default */
3220 rettv->v_type = VAR_STRING;
3221 rettv->vval.v_string = NULL;
3222
3223 text = get_tv_string_buf(&argvars[1], buf);
3224 channel = send_common(argvars, text, 0, eval, &opt,
3225 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3226 if (channel != NULL && eval)
3227 {
3228 if (opt.jo_set & JO_TIMEOUT)
3229 timeout = opt.jo_timeout;
3230 else
3231 timeout = channel_get_timeout(channel, part_read);
3232 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3233 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003234 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003235}
3236
Bram Moolenaard04a0202016-01-26 23:30:18 +01003237# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003238/*
3239 * Add open channels to the poll struct.
3240 * Return the adjusted struct index.
3241 * The type of "fds" is hidden to avoid problems with the function proto.
3242 */
3243 int
3244channel_poll_setup(int nfd_in, void *fds_in)
3245{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003246 int nfd = nfd_in;
3247 channel_T *channel;
3248 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003249 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003250
Bram Moolenaar77073442016-02-13 23:23:53 +01003251 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003252 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003253 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003254 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003255 chanpart_T *ch_part = &channel->ch_part[part];
3256
3257 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003258 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003259 ch_part->ch_poll_idx = nfd;
3260 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003261 fds[nfd].events = POLLIN;
3262 nfd++;
3263 }
3264 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003265 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003266 }
3267 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003268
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003269 nfd = channel_fill_poll_write(nfd, fds);
3270
Bram Moolenaare0874f82016-01-24 20:36:41 +01003271 return nfd;
3272}
3273
3274/*
3275 * The type of "fds" is hidden to avoid problems with the function proto.
3276 */
3277 int
3278channel_poll_check(int ret_in, void *fds_in)
3279{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003280 int ret = ret_in;
3281 channel_T *channel;
3282 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003283 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003284 int idx;
3285 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003286
Bram Moolenaar77073442016-02-13 23:23:53 +01003287 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003288 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003289 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003290 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003291 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003292
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003293 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003294 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003295 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003296 --ret;
3297 }
3298 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003299
3300 in_part = &channel->ch_part[PART_IN];
3301 idx = in_part->ch_poll_idx;
3302 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3303 {
3304 if (in_part->ch_buf_append)
3305 {
3306 if (in_part->ch_buffer != NULL)
3307 channel_write_new_lines(in_part->ch_buffer);
3308 }
3309 else
3310 channel_write_in(channel);
3311 --ret;
3312 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003313 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003314
3315 return ret;
3316}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003317# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003318
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003319# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003320/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003321 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003322 */
3323 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003324channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003325{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003326 int maxfd = maxfd_in;
3327 channel_T *channel;
3328 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003329 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003330 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003331
Bram Moolenaar77073442016-02-13 23:23:53 +01003332 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003333 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003334 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003335 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003336 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003337
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003338 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003339 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003340 FD_SET((int)fd, rfds);
3341 if (maxfd < (int)fd)
3342 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003343 }
3344 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003345 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003346
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003347 maxfd = channel_fill_wfds(maxfd, wfds);
3348
Bram Moolenaare0874f82016-01-24 20:36:41 +01003349 return maxfd;
3350}
3351
3352/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003353 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003354 */
3355 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003356channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003357{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003358 int ret = ret_in;
3359 channel_T *channel;
3360 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003361 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003362 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003363 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003364
Bram Moolenaar77073442016-02-13 23:23:53 +01003365 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003366 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003367 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003368 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003369 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003370
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003371 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003372 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003373 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003374 --ret;
3375 }
3376 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003377
3378 in_part = &channel->ch_part[PART_IN];
3379 if (ret > 0 && in_part->ch_fd != INVALID_FD
3380 && FD_ISSET(in_part->ch_fd, wfds))
3381 {
3382 if (in_part->ch_buf_append)
3383 {
3384 if (in_part->ch_buffer != NULL)
3385 channel_write_new_lines(in_part->ch_buffer);
3386 }
3387 else
3388 channel_write_in(channel);
3389 --ret;
3390 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003391 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003392
3393 return ret;
3394}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003395# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003396
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003397/*
Bram Moolenaar187db502016-02-27 14:44:26 +01003398 * Return TRUE if "channel" has JSON or other typeahead.
3399 */
3400 static int
3401channel_has_readahead(channel_T *channel, int part)
3402{
3403 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
3404
3405 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
3406 {
3407 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3408 jsonq_T *item = head->jq_next;
3409
3410 return item != NULL;
3411 }
3412 return channel_peek(channel, part) != NULL;
3413}
3414
3415/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003416 * Execute queued up commands.
3417 * Invoked from the main loop when it's safe to execute received commands.
3418 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003419 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003420 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003421channel_parse_messages(void)
3422{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003423 channel_T *channel = first_channel;
3424 int ret = FALSE;
3425 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003426 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003427
Bram Moolenaard0b65022016-03-06 21:50:33 +01003428 /* Only do this message when another message was given, otherwise we get
3429 * lots of them. */
3430 if (did_log_msg)
3431 {
3432 ch_log(NULL, "looking for messages on channels");
3433 did_log_msg = FALSE;
3434 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003435 while (channel != NULL)
3436 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003437 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003438 {
3439 /* channel is no longer useful, free it */
3440 channel_free(channel);
3441 channel = first_channel;
3442 part = PART_SOCK;
3443 continue;
3444 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003445 if (channel->ch_part[part].ch_fd != INVALID_FD
3446 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003447 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003448 /* Increase the refcount, in case the handler causes the channel
3449 * to be unreferenced or closed. */
3450 ++channel->ch_refcount;
3451 r = may_invoke_callback(channel, part);
3452 if (r == OK)
3453 ret = TRUE;
3454 if (channel_unref(channel) || r == OK)
3455 {
3456 /* channel was freed or something was done, start over */
3457 channel = first_channel;
3458 part = PART_SOCK;
3459 continue;
3460 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003461 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003462 if (part < PART_ERR)
3463 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003464 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003465 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003466 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003467 part = PART_SOCK;
3468 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003469 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003470
3471 if (channel_need_redraw && must_redraw)
3472 {
3473 channel_need_redraw = FALSE;
3474 update_screen(0);
3475 setcursor();
3476 cursor_on();
3477 out_flush();
3478 }
3479
Bram Moolenaard7ece102016-02-02 23:23:02 +01003480 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003481}
3482
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003483/*
3484 * Mark references to lists used in channels.
3485 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003486 int
3487set_ref_in_channel(int copyID)
3488{
Bram Moolenaar77073442016-02-13 23:23:53 +01003489 int abort = FALSE;
3490 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003491 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003492
Bram Moolenaar77073442016-02-13 23:23:53 +01003493 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003494 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003495 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003496 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003497 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3498 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003499
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003500 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003501 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003502 list_T *l = item->jq_value->vval.v_list;
3503
3504 if (l->lv_copyID != copyID)
3505 {
3506 l->lv_copyID = copyID;
3507 abort = abort || set_ref_in_list(l, copyID, NULL);
3508 }
3509 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003510 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003511 }
3512 }
3513 return abort;
3514}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003515
3516/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003517 * Return the "part" to write to for "channel".
3518 */
3519 int
3520channel_part_send(channel_T *channel)
3521{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003522 if (channel->CH_SOCK_FD == INVALID_FD)
3523 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003524 return PART_SOCK;
3525}
3526
3527/*
3528 * Return the default "part" to read from for "channel".
3529 */
3530 int
3531channel_part_read(channel_T *channel)
3532{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003533 if (channel->CH_SOCK_FD == INVALID_FD)
3534 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003535 return PART_SOCK;
3536}
3537
3538/*
3539 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003540 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003541 */
3542 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003543channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003544{
Bram Moolenaar77073442016-02-13 23:23:53 +01003545 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003546 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003547 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003548}
3549
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003550/*
3551 * Return the timeout of "channel"/"part"
3552 */
3553 int
3554channel_get_timeout(channel_T *channel, int part)
3555{
3556 return channel->ch_part[part].ch_timeout;
3557}
3558
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003559 static int
3560handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3561{
3562 char_u *val = get_tv_string(item);
3563
3564 opt->jo_set |= jo;
3565 if (STRCMP(val, "nl") == 0)
3566 *modep = MODE_NL;
3567 else if (STRCMP(val, "raw") == 0)
3568 *modep = MODE_RAW;
3569 else if (STRCMP(val, "js") == 0)
3570 *modep = MODE_JS;
3571 else if (STRCMP(val, "json") == 0)
3572 *modep = MODE_JSON;
3573 else
3574 {
3575 EMSG2(_(e_invarg2), val);
3576 return FAIL;
3577 }
3578 return OK;
3579}
3580
3581 static int
3582handle_io(typval_T *item, int part, jobopt_T *opt)
3583{
3584 char_u *val = get_tv_string(item);
3585
3586 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3587 if (STRCMP(val, "null") == 0)
3588 opt->jo_io[part] = JIO_NULL;
3589 else if (STRCMP(val, "pipe") == 0)
3590 opt->jo_io[part] = JIO_PIPE;
3591 else if (STRCMP(val, "file") == 0)
3592 opt->jo_io[part] = JIO_FILE;
3593 else if (STRCMP(val, "buffer") == 0)
3594 opt->jo_io[part] = JIO_BUFFER;
3595 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3596 opt->jo_io[part] = JIO_OUT;
3597 else
3598 {
3599 EMSG2(_(e_invarg2), val);
3600 return FAIL;
3601 }
3602 return OK;
3603}
3604
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003605/*
3606 * Clear a jobopt_T before using it.
3607 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003608 void
3609clear_job_options(jobopt_T *opt)
3610{
3611 vim_memset(opt, 0, sizeof(jobopt_T));
3612}
3613
3614/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003615 * Free any members of a jobopt_T.
3616 */
3617 void
3618free_job_options(jobopt_T *opt)
3619{
3620 if (opt->jo_partial != NULL)
3621 partial_unref(opt->jo_partial);
3622 if (opt->jo_out_partial != NULL)
3623 partial_unref(opt->jo_out_partial);
3624 if (opt->jo_err_partial != NULL)
3625 partial_unref(opt->jo_err_partial);
3626 if (opt->jo_close_partial != NULL)
3627 partial_unref(opt->jo_close_partial);
3628}
3629
3630/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003631 * Get the PART_ number from the first character of an option name.
3632 */
3633 static int
3634part_from_char(int c)
3635{
3636 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3637}
3638
3639/*
3640 * Get the option entries from the dict in "tv", parse them and put the result
3641 * in "opt".
3642 * Only accept options in "supported".
3643 * If an option value is invalid return FAIL.
3644 */
3645 int
3646get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3647{
3648 typval_T *item;
3649 char_u *val;
3650 dict_T *dict;
3651 int todo;
3652 hashitem_T *hi;
3653 int part;
3654
3655 opt->jo_set = 0;
3656 if (tv->v_type == VAR_UNKNOWN)
3657 return OK;
3658 if (tv->v_type != VAR_DICT)
3659 {
3660 EMSG(_(e_invarg));
3661 return FAIL;
3662 }
3663 dict = tv->vval.v_dict;
3664 if (dict == NULL)
3665 return OK;
3666
3667 todo = (int)dict->dv_hashtab.ht_used;
3668 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3669 if (!HASHITEM_EMPTY(hi))
3670 {
3671 item = &dict_lookup(hi)->di_tv;
3672
3673 if (STRCMP(hi->hi_key, "mode") == 0)
3674 {
3675 if (!(supported & JO_MODE))
3676 break;
3677 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3678 return FAIL;
3679 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003680 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003681 {
3682 if (!(supported & JO_IN_MODE))
3683 break;
3684 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3685 == FAIL)
3686 return FAIL;
3687 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003688 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003689 {
3690 if (!(supported & JO_OUT_MODE))
3691 break;
3692 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3693 == FAIL)
3694 return FAIL;
3695 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003696 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003697 {
3698 if (!(supported & JO_ERR_MODE))
3699 break;
3700 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3701 == FAIL)
3702 return FAIL;
3703 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003704 else if (STRCMP(hi->hi_key, "in_io") == 0
3705 || STRCMP(hi->hi_key, "out_io") == 0
3706 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003707 {
3708 if (!(supported & JO_OUT_IO))
3709 break;
3710 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3711 return FAIL;
3712 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003713 else if (STRCMP(hi->hi_key, "in_name") == 0
3714 || STRCMP(hi->hi_key, "out_name") == 0
3715 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003716 {
3717 part = part_from_char(*hi->hi_key);
3718
3719 if (!(supported & JO_OUT_IO))
3720 break;
3721 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3722 opt->jo_io_name[part] =
3723 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3724 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003725 else if (STRCMP(hi->hi_key, "in_buf") == 0
3726 || STRCMP(hi->hi_key, "out_buf") == 0
3727 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003728 {
3729 part = part_from_char(*hi->hi_key);
3730
3731 if (!(supported & JO_OUT_IO))
3732 break;
3733 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3734 opt->jo_io_buf[part] = get_tv_number(item);
3735 if (opt->jo_io_buf[part] <= 0)
3736 {
3737 EMSG2(_(e_invarg2), get_tv_string(item));
3738 return FAIL;
3739 }
3740 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3741 {
3742 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3743 return FAIL;
3744 }
3745 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003746 else if (STRCMP(hi->hi_key, "in_top") == 0
3747 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003748 {
3749 linenr_T *lp;
3750
3751 if (!(supported & JO_OUT_IO))
3752 break;
3753 if (hi->hi_key[3] == 't')
3754 {
3755 lp = &opt->jo_in_top;
3756 opt->jo_set |= JO_IN_TOP;
3757 }
3758 else
3759 {
3760 lp = &opt->jo_in_bot;
3761 opt->jo_set |= JO_IN_BOT;
3762 }
3763 *lp = get_tv_number(item);
3764 if (*lp < 0)
3765 {
3766 EMSG2(_(e_invarg2), get_tv_string(item));
3767 return FAIL;
3768 }
3769 }
3770 else if (STRCMP(hi->hi_key, "channel") == 0)
3771 {
3772 if (!(supported & JO_OUT_IO))
3773 break;
3774 opt->jo_set |= JO_CHANNEL;
3775 if (item->v_type != VAR_CHANNEL)
3776 {
3777 EMSG2(_(e_invarg2), "channel");
3778 return FAIL;
3779 }
3780 opt->jo_channel = item->vval.v_channel;
3781 }
3782 else if (STRCMP(hi->hi_key, "callback") == 0)
3783 {
3784 if (!(supported & JO_CALLBACK))
3785 break;
3786 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003787 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003788 if (opt->jo_callback == NULL)
3789 {
3790 EMSG2(_(e_invarg2), "callback");
3791 return FAIL;
3792 }
3793 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003794 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003795 {
3796 if (!(supported & JO_OUT_CALLBACK))
3797 break;
3798 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003799 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003800 if (opt->jo_out_cb == NULL)
3801 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003802 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003803 return FAIL;
3804 }
3805 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003806 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003807 {
3808 if (!(supported & JO_ERR_CALLBACK))
3809 break;
3810 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003811 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003812 if (opt->jo_err_cb == NULL)
3813 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003814 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003815 return FAIL;
3816 }
3817 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003818 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003819 {
3820 if (!(supported & JO_CLOSE_CALLBACK))
3821 break;
3822 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003823 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003824 if (opt->jo_close_cb == NULL)
3825 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003826 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003827 return FAIL;
3828 }
3829 }
3830 else if (STRCMP(hi->hi_key, "waittime") == 0)
3831 {
3832 if (!(supported & JO_WAITTIME))
3833 break;
3834 opt->jo_set |= JO_WAITTIME;
3835 opt->jo_waittime = get_tv_number(item);
3836 }
3837 else if (STRCMP(hi->hi_key, "timeout") == 0)
3838 {
3839 if (!(supported & JO_TIMEOUT))
3840 break;
3841 opt->jo_set |= JO_TIMEOUT;
3842 opt->jo_timeout = get_tv_number(item);
3843 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003844 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003845 {
3846 if (!(supported & JO_OUT_TIMEOUT))
3847 break;
3848 opt->jo_set |= JO_OUT_TIMEOUT;
3849 opt->jo_out_timeout = get_tv_number(item);
3850 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003851 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003852 {
3853 if (!(supported & JO_ERR_TIMEOUT))
3854 break;
3855 opt->jo_set |= JO_ERR_TIMEOUT;
3856 opt->jo_err_timeout = get_tv_number(item);
3857 }
3858 else if (STRCMP(hi->hi_key, "part") == 0)
3859 {
3860 if (!(supported & JO_PART))
3861 break;
3862 opt->jo_set |= JO_PART;
3863 val = get_tv_string(item);
3864 if (STRCMP(val, "err") == 0)
3865 opt->jo_part = PART_ERR;
3866 else
3867 {
3868 EMSG2(_(e_invarg2), val);
3869 return FAIL;
3870 }
3871 }
3872 else if (STRCMP(hi->hi_key, "id") == 0)
3873 {
3874 if (!(supported & JO_ID))
3875 break;
3876 opt->jo_set |= JO_ID;
3877 opt->jo_id = get_tv_number(item);
3878 }
3879 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3880 {
3881 if (!(supported & JO_STOPONEXIT))
3882 break;
3883 opt->jo_set |= JO_STOPONEXIT;
3884 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3885 opt->jo_soe_buf);
3886 if (opt->jo_stoponexit == NULL)
3887 {
3888 EMSG2(_(e_invarg2), "stoponexit");
3889 return FAIL;
3890 }
3891 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003892 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003893 {
3894 if (!(supported & JO_EXIT_CB))
3895 break;
3896 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003897 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3898 {
3899 opt->jo_exit_partial = item->vval.v_partial;
3900 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3901 }
3902 else
3903 opt->jo_exit_cb = get_tv_string_buf_chk(
3904 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003905 if (opt->jo_exit_cb == NULL)
3906 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003907 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003908 return FAIL;
3909 }
3910 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003911 else if (STRCMP(hi->hi_key, "block_write") == 0)
3912 {
3913 if (!(supported & JO_BLOCK_WRITE))
3914 break;
3915 opt->jo_set |= JO_BLOCK_WRITE;
3916 opt->jo_block_write = get_tv_number(item);
3917 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003918 else
3919 break;
3920 --todo;
3921 }
3922 if (todo > 0)
3923 {
3924 EMSG2(_(e_invarg2), hi->hi_key);
3925 return FAIL;
3926 }
3927
3928 return OK;
3929}
3930
3931/*
3932 * Get the channel from the argument.
3933 * Returns NULL if the handle is invalid.
3934 */
3935 channel_T *
3936get_channel_arg(typval_T *tv, int check_open)
3937{
3938 channel_T *channel = NULL;
3939
3940 if (tv->v_type == VAR_JOB)
3941 {
3942 if (tv->vval.v_job != NULL)
3943 channel = tv->vval.v_job->jv_channel;
3944 }
3945 else if (tv->v_type == VAR_CHANNEL)
3946 {
3947 channel = tv->vval.v_channel;
3948 }
3949 else
3950 {
3951 EMSG2(_(e_invarg2), get_tv_string(tv));
3952 return NULL;
3953 }
3954
3955 if (check_open && (channel == NULL || !channel_is_open(channel)))
3956 {
3957 EMSG(_("E906: not an open channel"));
3958 return NULL;
3959 }
3960 return channel;
3961}
3962
3963static job_T *first_job = NULL;
3964
3965 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003966job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003967{
3968 ch_log(job->jv_channel, "Freeing job");
3969 if (job->jv_channel != NULL)
3970 {
3971 /* The link from the channel to the job doesn't count as a reference,
3972 * thus don't decrement the refcount of the job. The reference from
3973 * the job to the channel does count the refrence, decrement it and
3974 * NULL the reference. We don't set ch_job_killed, unreferencing the
3975 * job doesn't mean it stops running. */
3976 job->jv_channel->ch_job = NULL;
3977 channel_unref(job->jv_channel);
3978 }
3979 mch_clear_job(job);
3980
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003981 vim_free(job->jv_stoponexit);
3982 vim_free(job->jv_exit_cb);
3983 partial_unref(job->jv_exit_partial);
3984}
3985
3986 static void
3987job_free_job(job_T *job)
3988{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003989 if (job->jv_next != NULL)
3990 job->jv_next->jv_prev = job->jv_prev;
3991 if (job->jv_prev == NULL)
3992 first_job = job->jv_next;
3993 else
3994 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003995 vim_free(job);
3996}
3997
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003998 static void
3999job_free(job_T *job)
4000{
4001 if (!in_free_unref_items)
4002 {
4003 job_free_contents(job);
4004 job_free_job(job);
4005 }
4006}
4007
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004008/*
4009 * Return TRUE if the job should not be freed yet. Do not free the job when
4010 * it has not ended yet and there is a "stoponexit" flag or an exit callback.
4011 */
4012 static int
4013job_still_useful(job_T *job)
4014{
4015 return job->jv_status == JOB_STARTED
4016 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4017}
4018
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004019 void
4020job_unref(job_T *job)
4021{
4022 if (job != NULL && --job->jv_refcount <= 0)
4023 {
4024 /* Do not free the job when it has not ended yet and there is a
4025 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004026 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004027 {
4028 job_free(job);
4029 }
4030 else if (job->jv_channel != NULL)
4031 {
4032 /* Do remove the link to the channel, otherwise it hangs
4033 * around until Vim exits. See job_free() for refcount. */
4034 job->jv_channel->ch_job = NULL;
4035 channel_unref(job->jv_channel);
4036 job->jv_channel = NULL;
4037 }
4038 }
4039}
4040
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004041 int
4042free_unused_jobs_contents(int copyID, int mask)
4043{
4044 int did_free = FALSE;
4045 job_T *job;
4046
4047 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004048 if ((job->jv_copyID & mask) != (copyID & mask)
4049 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004050 {
4051 /* Free the channel and ordinary items it contains, but don't
4052 * recurse into Lists, Dictionaries etc. */
4053 job_free_contents(job);
4054 did_free = TRUE;
4055 }
4056 return did_free;
4057}
4058
4059 void
4060free_unused_jobs(int copyID, int mask)
4061{
4062 job_T *job;
4063 job_T *job_next;
4064
4065 for (job = first_job; job != NULL; job = job_next)
4066 {
4067 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004068 if ((job->jv_copyID & mask) != (copyID & mask)
4069 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004070 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004071 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004072 job_free_job(job);
4073 }
4074 }
4075}
4076
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004077/*
4078 * Allocate a job. Sets the refcount to one and sets options default.
4079 */
4080 static job_T *
4081job_alloc(void)
4082{
4083 job_T *job;
4084
4085 job = (job_T *)alloc_clear(sizeof(job_T));
4086 if (job != NULL)
4087 {
4088 job->jv_refcount = 1;
4089 job->jv_stoponexit = vim_strsave((char_u *)"term");
4090
4091 if (first_job != NULL)
4092 {
4093 first_job->jv_prev = job;
4094 job->jv_next = first_job;
4095 }
4096 first_job = job;
4097 }
4098 return job;
4099}
4100
4101 void
4102job_set_options(job_T *job, jobopt_T *opt)
4103{
4104 if (opt->jo_set & JO_STOPONEXIT)
4105 {
4106 vim_free(job->jv_stoponexit);
4107 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4108 job->jv_stoponexit = NULL;
4109 else
4110 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4111 }
4112 if (opt->jo_set & JO_EXIT_CB)
4113 {
4114 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004115 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004116 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004117 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004118 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004119 job->jv_exit_partial = NULL;
4120 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004121 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004122 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004123 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004124 job->jv_exit_partial = opt->jo_exit_partial;
4125 if (job->jv_exit_partial != NULL)
4126 ++job->jv_exit_partial->pt_refcount;
4127 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004128 }
4129}
4130
4131/*
4132 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4133 */
4134 void
4135job_stop_on_exit()
4136{
4137 job_T *job;
4138
4139 for (job = first_job; job != NULL; job = job->jv_next)
4140 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4141 mch_stop_job(job, job->jv_stoponexit);
4142}
4143
4144/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004145 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004146 */
4147 void
4148job_check_ended(void)
4149{
4150 static time_t last_check = 0;
4151 time_t now;
4152 job_T *job;
4153 job_T *next;
4154
4155 /* Only do this once in 10 seconds. */
4156 now = time(NULL);
4157 if (last_check + 10 < now)
4158 {
4159 last_check = now;
4160 for (job = first_job; job != NULL; job = next)
4161 {
4162 next = job->jv_next;
4163 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4164 job_status(job); /* may free "job" */
4165 }
4166 }
4167}
4168
4169/*
4170 * "job_start()" function
4171 */
4172 job_T *
4173job_start(typval_T *argvars)
4174{
4175 job_T *job;
4176 char_u *cmd = NULL;
4177#if defined(UNIX)
4178# define USE_ARGV
4179 char **argv = NULL;
4180 int argc = 0;
4181#else
4182 garray_T ga;
4183#endif
4184 jobopt_T opt;
4185 int part;
4186
4187 job = job_alloc();
4188 if (job == NULL)
4189 return NULL;
4190
4191 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004192#ifndef USE_ARGV
4193 ga_init2(&ga, (int)sizeof(char*), 20);
4194#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004195
4196 /* Default mode is NL. */
4197 clear_job_options(&opt);
4198 opt.jo_mode = MODE_NL;
4199 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004200 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4201 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004202 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004203
4204 /* Check that when io is "file" that there is a file name. */
4205 for (part = PART_OUT; part <= PART_IN; ++part)
4206 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4207 && opt.jo_io[part] == JIO_FILE
4208 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4209 || *opt.jo_io_name[part] == NUL))
4210 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004211 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004212 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004213 }
4214
4215 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4216 {
4217 buf_T *buf = NULL;
4218
4219 /* check that we can find the buffer before starting the job */
4220 if (opt.jo_set & JO_IN_BUF)
4221 {
4222 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4223 if (buf == NULL)
4224 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4225 }
4226 else if (!(opt.jo_set & JO_IN_NAME))
4227 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004228 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004229 }
4230 else
4231 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4232 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004233 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004234 if (buf->b_ml.ml_mfp == NULL)
4235 {
4236 char_u numbuf[NUMBUFLEN];
4237 char_u *s;
4238
4239 if (opt.jo_set & JO_IN_BUF)
4240 {
4241 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4242 s = numbuf;
4243 }
4244 else
4245 s = opt.jo_io_name[PART_IN];
4246 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004247 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004248 }
4249 job->jv_in_buf = buf;
4250 }
4251
4252 job_set_options(job, &opt);
4253
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004254 if (argvars[0].v_type == VAR_STRING)
4255 {
4256 /* Command is a string. */
4257 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004258 if (cmd == NULL || *cmd == NUL)
4259 {
4260 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004261 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004262 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004263#ifdef USE_ARGV
4264 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004265 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004266 argv[argc] = NULL;
4267#endif
4268 }
4269 else if (argvars[0].v_type != VAR_LIST
4270 || argvars[0].vval.v_list == NULL
4271 || argvars[0].vval.v_list->lv_len < 1)
4272 {
4273 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004274 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004275 }
4276 else
4277 {
4278 list_T *l = argvars[0].vval.v_list;
4279 listitem_T *li;
4280 char_u *s;
4281
4282#ifdef USE_ARGV
4283 /* Pass argv[] to mch_call_shell(). */
4284 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4285 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004286 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004287#endif
4288 for (li = l->lv_first; li != NULL; li = li->li_next)
4289 {
4290 s = get_tv_string_chk(&li->li_tv);
4291 if (s == NULL)
4292 goto theend;
4293#ifdef USE_ARGV
4294 argv[argc++] = (char *)s;
4295#else
4296 /* Only escape when needed, double quotes are not always allowed. */
4297 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4298 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004299# ifdef WIN32
4300 int old_ssl = p_ssl;
4301
4302 /* This is using CreateProcess, not cmd.exe. Always use
4303 * double quote and backslashes. */
4304 p_ssl = 0;
4305# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004306 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004307# ifdef WIN32
4308 p_ssl = old_ssl;
4309# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004310 if (s == NULL)
4311 goto theend;
4312 ga_concat(&ga, s);
4313 vim_free(s);
4314 }
4315 else
4316 ga_concat(&ga, s);
4317 if (li->li_next != NULL)
4318 ga_append(&ga, ' ');
4319#endif
4320 }
4321#ifdef USE_ARGV
4322 argv[argc] = NULL;
4323#else
4324 cmd = ga.ga_data;
4325#endif
4326 }
4327
4328#ifdef USE_ARGV
4329 if (ch_log_active())
4330 {
4331 garray_T ga;
4332 int i;
4333
4334 ga_init2(&ga, (int)sizeof(char), 200);
4335 for (i = 0; i < argc; ++i)
4336 {
4337 if (i > 0)
4338 ga_concat(&ga, (char_u *)" ");
4339 ga_concat(&ga, (char_u *)argv[i]);
4340 }
4341 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4342 ga_clear(&ga);
4343 }
4344 mch_start_job(argv, job, &opt);
4345#else
4346 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4347 mch_start_job((char *)cmd, job, &opt);
4348#endif
4349
4350 /* If the channel is reading from a buffer, write lines now. */
4351 if (job->jv_channel != NULL)
4352 channel_write_in(job->jv_channel);
4353
4354theend:
4355#ifdef USE_ARGV
4356 vim_free(argv);
4357#else
4358 vim_free(ga.ga_data);
4359#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004360 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004361 return job;
4362}
4363
4364/*
4365 * Get the status of "job" and invoke the exit callback when needed.
4366 * The returned string is not allocated.
4367 */
4368 char *
4369job_status(job_T *job)
4370{
4371 char *result;
4372
4373 if (job->jv_status == JOB_ENDED)
4374 /* No need to check, dead is dead. */
4375 result = "dead";
4376 else if (job->jv_status == JOB_FAILED)
4377 result = "fail";
4378 else
4379 {
4380 result = mch_job_status(job);
4381 if (job->jv_status == JOB_ENDED)
4382 ch_log(job->jv_channel, "Job ended");
4383 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4384 {
4385 typval_T argv[3];
4386 typval_T rettv;
4387 int dummy;
4388
4389 /* invoke the exit callback; make sure the refcount is > 0 */
4390 ++job->jv_refcount;
4391 argv[0].v_type = VAR_JOB;
4392 argv[0].vval.v_job = job;
4393 argv[1].v_type = VAR_NUMBER;
4394 argv[1].vval.v_number = job->jv_exitval;
4395 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004396 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4397 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004398 clear_tv(&rettv);
4399 --job->jv_refcount;
4400 }
4401 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4402 {
4403 /* The job was already unreferenced, now that it ended it can be
4404 * freed. Careful: caller must not use "job" after this! */
4405 job_free(job);
4406 }
4407 }
4408 return result;
4409}
4410
Bram Moolenaar8950a562016-03-12 15:22:55 +01004411/*
4412 * Implementation of job_info().
4413 */
4414 void
4415job_info(job_T *job, dict_T *dict)
4416{
4417 dictitem_T *item;
4418 varnumber_T nr;
4419
4420 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4421
4422 item = dictitem_alloc((char_u *)"channel");
4423 if (item == NULL)
4424 return;
4425 item->di_tv.v_lock = 0;
4426 item->di_tv.v_type = VAR_CHANNEL;
4427 item->di_tv.vval.v_channel = job->jv_channel;
4428 if (job->jv_channel != NULL)
4429 ++job->jv_channel->ch_refcount;
4430 if (dict_add(dict, item) == FAIL)
4431 dictitem_free(item);
4432
4433#ifdef UNIX
4434 nr = job->jv_pid;
4435#else
4436 nr = job->jv_proc_info.dwProcessId;
4437#endif
4438 dict_add_nr_str(dict, "process", nr, NULL);
4439
4440 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004441 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004442 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4443}
4444
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004445 int
4446job_stop(job_T *job, typval_T *argvars)
4447{
4448 char_u *arg;
4449
4450 if (argvars[1].v_type == VAR_UNKNOWN)
4451 arg = (char_u *)"";
4452 else
4453 {
4454 arg = get_tv_string_chk(&argvars[1]);
4455 if (arg == NULL)
4456 {
4457 EMSG(_(e_invarg));
4458 return 0;
4459 }
4460 }
4461 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4462 if (mch_stop_job(job, arg) == FAIL)
4463 return 0;
4464
4465 /* Assume that "hup" does not kill the job. */
4466 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4467 job->jv_channel->ch_job_killed = TRUE;
4468
4469 /* We don't try freeing the job, obviously the caller still has a
4470 * reference to it. */
4471 return 1;
4472}
4473
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004474#endif /* FEAT_JOB_CHANNEL */