blob: 2cc46fecb704e52e441b74e4d31bcf87226f81ef [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 Moolenaarb2658a12016-04-26 17:16:24 +020057static void channel_read(channel_T *channel, int part, char *func);
58
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaard8070362016-02-15 21:56:54 +010068#ifdef WIN32
69 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
83 HANDLE h = (HANDLE)fd;
84 DWORD nwrite;
85
86 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
87 return -1;
88 return (int)nwrite;
89}
90
91 static void
92fd_close(sock_T fd)
93{
94 HANDLE h = (HANDLE)fd;
95
96 CloseHandle(h);
97}
98#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010099
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100/* Log file opened with ch_logfile(). */
101static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100102#ifdef FEAT_RELTIME
103static proftime_T log_start;
104#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100105
106 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100107ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100108{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100109 FILE *file = NULL;
110
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100111 if (log_fd != NULL)
112 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100113
114 if (*fname != NUL)
115 {
116 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
117 if (file == NULL)
118 {
119 EMSG2(_(e_notopen), fname);
120 return;
121 }
122 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100124
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100125 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100126 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100128#ifdef FEAT_RELTIME
129 profile_start(&log_start);
130#endif
131 }
132}
133
134 int
135ch_log_active()
136{
137 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138}
139
140 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100141ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100142{
143 if (log_fd != NULL)
144 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100145#ifdef FEAT_RELTIME
146 proftime_T log_now;
147
148 profile_start(&log_now);
149 profile_sub(&log_now, &log_start);
150 fprintf(log_fd, "%s ", profile_msg(&log_now));
151#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100152 if (ch != NULL)
153 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 else
155 fprintf(log_fd, "%s: ", what);
156 }
157}
158
Bram Moolenaard0b65022016-03-06 21:50:33 +0100159static int did_log_msg = TRUE;
160
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100162ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100166 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100168 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100170 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171 }
172}
173
174 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100175ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100176{
177 if (log_fd != NULL)
178 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100181 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100183 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184 }
185}
186
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100187 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100188ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100189{
190 if (log_fd != NULL)
191 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100192 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100193 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100194 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100196 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197 }
198}
199
200 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100201ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100202{
203 if (log_fd != NULL)
204 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100205 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100206 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100207 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100209 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210 }
211}
212
213 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100214ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100215{
216 if (log_fd != NULL)
217 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100218 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100220 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100222 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223 }
224}
225
226 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100227ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100228{
229 if (log_fd != NULL)
230 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100233 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100235 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236 }
237}
238
239 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100240ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100241{
242 if (log_fd != NULL)
243 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100244 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100245 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100246 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100247 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100248 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100249 }
250}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100251
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252#ifdef _WIN32
253# undef PERROR
254# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
255 (char_u *)msg, (char_u *)strerror_win32(errno))
256
257 static char *
258strerror_win32(int eno)
259{
260 static LPVOID msgbuf = NULL;
261 char_u *ptr;
262
263 if (msgbuf)
264 LocalFree(msgbuf);
265 FormatMessage(
266 FORMAT_MESSAGE_ALLOCATE_BUFFER |
267 FORMAT_MESSAGE_FROM_SYSTEM |
268 FORMAT_MESSAGE_IGNORE_INSERTS,
269 NULL,
270 eno,
271 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
272 (LPTSTR) &msgbuf,
273 0,
274 NULL);
275 /* chomp \r or \n */
276 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
277 switch (*ptr)
278 {
279 case '\r':
280 STRMOVE(ptr, ptr + 1);
281 ptr--;
282 break;
283 case '\n':
284 if (*(ptr + 1) == '\0')
285 *ptr = '\0';
286 else
287 *ptr = ' ';
288 break;
289 }
290 return msgbuf;
291}
292#endif
293
Bram Moolenaar77073442016-02-13 23:23:53 +0100294/*
295 * The list of all allocated channels.
296 */
297static channel_T *first_channel = NULL;
298static int next_ch_id = 0;
299
300/*
301 * Allocate a new channel. The refcount is set to 1.
302 * The channel isn't actually used until it is opened.
303 * Returns NULL if out of memory.
304 */
305 channel_T *
306add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100307{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100308 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100309 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100310
Bram Moolenaar77073442016-02-13 23:23:53 +0100311 if (channel == NULL)
312 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313
Bram Moolenaar77073442016-02-13 23:23:53 +0100314 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100315 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100316
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100317 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100318 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100320#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100321 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100322#endif
323#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100324 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100325#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100326 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100327 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100328
Bram Moolenaar77073442016-02-13 23:23:53 +0100329 if (first_channel != NULL)
330 {
331 first_channel->ch_prev = channel;
332 channel->ch_next = first_channel;
333 }
334 first_channel = channel;
335
336 channel->ch_refcount = 1;
337 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100338}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100339
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100340/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100341 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100342 * Return TRUE if "channel" has a callback and the associated job wasn't
343 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100344 */
345 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100346channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100347{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100348 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100349 int has_out_msg;
350 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100351
352 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100353 if (channel->ch_job_killed && channel->ch_job == NULL)
354 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100355
356 /* If there is a close callback it may still need to be invoked. */
357 if (channel->ch_close_cb != NULL)
358 return TRUE;
359
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200360 /* If reading from or a buffer it's still useful. */
361 if (channel->ch_part[PART_IN].ch_buffer != NULL)
362 return TRUE;
363
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100364 /* If there is no callback then nobody can get readahead. If the fd is
365 * closed and there is no readahead then the callback won't be called. */
366 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
367 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
368 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100369 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
370 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
371 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
372 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
373 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
374 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100375 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100376 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200377 || ((channel->ch_part[PART_OUT].ch_callback != NULL
378 || channel->ch_part[PART_OUT].ch_buffer) && has_out_msg)
379 || ((channel->ch_part[PART_ERR].ch_callback != NULL
380 || channel->ch_part[PART_ERR].ch_buffer) && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100381}
382
383/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200384 * Close a channel and free all its resources.
385 */
386 static void
387channel_free_contents(channel_T *channel)
388{
389 channel_close(channel, TRUE);
390 channel_clear(channel);
391 ch_log(channel, "Freeing channel");
392}
393
394 static void
395channel_free_channel(channel_T *channel)
396{
397 if (channel->ch_next != NULL)
398 channel->ch_next->ch_prev = channel->ch_prev;
399 if (channel->ch_prev == NULL)
400 first_channel = channel->ch_next;
401 else
402 channel->ch_prev->ch_next = channel->ch_next;
403 vim_free(channel);
404}
405
406 static void
407channel_free(channel_T *channel)
408{
409 if (!in_free_unref_items)
410 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200411 if (safe_to_invoke_callback == 0)
412 {
413 channel->ch_to_be_freed = TRUE;
414 }
415 else
416 {
417 channel_free_contents(channel);
418 channel_free_channel(channel);
419 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200420 }
421}
422
423/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100424 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100425 * possible, there is no callback to be invoked or the associated job was
426 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100427 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100428 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100429 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100430channel_may_free(channel_T *channel)
431{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100432 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100433 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100435 return TRUE;
436 }
437 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100438}
439
440/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100441 * Decrement the reference count on "channel" and maybe free it when it goes
442 * down to zero. Don't free it if there is a pending action.
443 * Returns TRUE when the channel is no longer referenced.
444 */
445 int
446channel_unref(channel_T *channel)
447{
448 if (channel != NULL && --channel->ch_refcount <= 0)
449 return channel_may_free(channel);
450 return FALSE;
451}
452
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200453 int
454free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100455{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200456 int did_free = FALSE;
457 channel_T *ch;
458
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200459 /* This is invoked from the garbage collector, which only runs at a safe
460 * point. */
461 ++safe_to_invoke_callback;
462
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200463 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200464 if (!channel_still_useful(ch)
465 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 {
467 /* Free the channel and ordinary items it contains, but don't
468 * recurse into Lists, Dictionaries etc. */
469 channel_free_contents(ch);
470 did_free = TRUE;
471 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200472
473 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200474 return did_free;
475}
476
477 void
478free_unused_channels(int copyID, int mask)
479{
480 channel_T *ch;
481 channel_T *ch_next;
482
483 for (ch = first_channel; ch != NULL; ch = ch_next)
484 {
485 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200486 if (!channel_still_useful(ch)
487 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200488 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200489 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200490 channel_free_channel(ch);
491 }
492 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100493}
494
Bram Moolenaard04a0202016-01-26 23:30:18 +0100495#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100496
497#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
498 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100499channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100500{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100501 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100502 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100503
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100504 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100505 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100506 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100507 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200508 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100509}
510#endif
511
Bram Moolenaare0874f82016-01-24 20:36:41 +0100512/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100514 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100515#ifdef FEAT_GUI_X11
516 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200517messageFromServer(XtPointer clientData,
518 int *unused1 UNUSED,
519 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100520{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100521 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100523#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100524
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100526# if GTK_CHECK_VERSION(3,0,0)
527 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200528messageFromServer(GIOChannel *unused1 UNUSED,
529 GIOCondition unused2 UNUSED,
530 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100531{
532 channel_read_fd(GPOINTER_TO_INT(clientData));
533 return TRUE; /* Return FALSE instead in case the event source is to
534 * be removed after this function returns. */
535}
536# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100537 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200538messageFromServer(gpointer clientData,
539 gint unused1 UNUSED,
540 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100541{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100542 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100543}
Bram Moolenaar98921892016-02-23 17:14:37 +0100544# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100545#endif
546
547 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100548channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100549{
Bram Moolenaarde279892016-03-11 22:19:44 +0100550 if (!CH_HAS_GUI)
551 return;
552
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100553# ifdef FEAT_GUI_X11
554 /* Tell notifier we are interested in being called
555 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100556 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
557 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100558 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100559 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200561 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100562 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100563# else
564# ifdef FEAT_GUI_GTK
565 /* Tell gdk we are interested in being called when there
566 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100567 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100568# if GTK_CHECK_VERSION(3,0,0)
569 {
570 GIOChannel *chnnl = g_io_channel_unix_new(
571 (gint)channel->ch_part[part].ch_fd);
572
573 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
574 chnnl,
575 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200576 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100577 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
578
579 g_io_channel_unref(chnnl);
580 }
581# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100582 channel->ch_part[part].ch_inputHandler = gdk_input_add(
583 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100584 (GdkInputCondition)
585 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200586 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100587 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100588# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100589# endif
590# endif
591}
592
Bram Moolenaarde279892016-03-11 22:19:44 +0100593 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100594channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100595{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100596 if (channel->CH_SOCK_FD != INVALID_FD)
597 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100598 if (channel->CH_OUT_FD != INVALID_FD)
599 channel_gui_register_one(channel, PART_OUT);
600 if (channel->CH_ERR_FD != INVALID_FD)
601 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100602}
603
604/*
605 * Register any of our file descriptors with the GUI event handling system.
606 * Called when the GUI has started.
607 */
608 void
609channel_gui_register_all(void)
610{
Bram Moolenaar77073442016-02-13 23:23:53 +0100611 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100612
Bram Moolenaar77073442016-02-13 23:23:53 +0100613 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100614 channel_gui_register(channel);
615}
616
617 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100618channel_gui_unregister_one(channel_T *channel, int part)
619{
620# ifdef FEAT_GUI_X11
621 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
622 {
623 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
624 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
625 }
626# else
627# ifdef FEAT_GUI_GTK
628 if (channel->ch_part[part].ch_inputHandler != 0)
629 {
630# if GTK_CHECK_VERSION(3,0,0)
631 g_source_remove(channel->ch_part[part].ch_inputHandler);
632# else
633 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
634# endif
635 channel->ch_part[part].ch_inputHandler = 0;
636 }
637# endif
638# endif
639}
640
641 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100642channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100643{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100644 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100645
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100646 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100647 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100648}
649
650#endif
651
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100652static char *e_cannot_connect = N_("E902: Cannot connect to port");
653
Bram Moolenaard04a0202016-01-26 23:30:18 +0100654/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100655 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100656 * "waittime" is the time in msec to wait for the connection.
657 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100658 * Returns the channel for success.
659 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100660 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100661 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100662channel_open(
663 char *hostname,
664 int port_in,
665 int waittime,
666 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100667{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100668 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100669 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100670 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100671#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100672 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100673 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100674#else
675 int port = port_in;
676#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100677 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100678 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100680#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100681 channel_init_winsock();
682#endif
683
Bram Moolenaar77073442016-02-13 23:23:53 +0100684 channel = add_channel();
685 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100686 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100687 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100688 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100689 }
690
691 /* Get the server internet address and put into addr structure */
692 /* fill in the socket address structure and connect to server */
693 vim_memset((char *)&server, 0, sizeof(server));
694 server.sin_family = AF_INET;
695 server.sin_port = htons(port);
696 if ((host = gethostbyname(hostname)) == NULL)
697 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100698 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100699 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100700 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100701 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100702 }
703 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
704
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100705 /* On Mac and Solaris a zero timeout almost never works. At least wait
706 * one millisecond. Let's do it for all systems, because we don't know why
707 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708 if (waittime == 0)
709 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100710
711 /*
712 * For Unix we need to call connect() again after connect() failed.
713 * On Win32 one time is sufficient.
714 */
715 while (TRUE)
716 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100717 long elapsed_msec = 0;
718 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100719
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100721 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100722 sd = socket(AF_INET, SOCK_STREAM, 0);
723 if (sd == -1)
724 {
725 ch_error(channel, "in socket() in channel_open().");
726 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100727 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100728 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100729 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100730
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731 if (waittime >= 0)
732 {
733 /* Make connect() non-blocking. */
734 if (
735#ifdef _WIN32
736 ioctlsocket(sd, FIONBIO, &val) < 0
737#else
738 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
739#endif
740 )
741 {
742 SOCK_ERRNO;
743 ch_errorn(channel,
744 "channel_open: Connect failed with errno %d", errno);
745 sock_close(sd);
746 channel_free(channel);
747 return NULL;
748 }
749 }
750
751 /* Try connecting to the server. */
752 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
753 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
754
Bram Moolenaar045a2842016-03-08 22:33:07 +0100755 if (ret == 0)
756 /* The connection could be established. */
757 break;
758
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100759 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100760 if (waittime < 0 || (errno != EWOULDBLOCK
761 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100762#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100763 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100764#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100765 ))
766 {
767 ch_errorn(channel,
768 "channel_open: Connect failed with errno %d", errno);
769 PERROR(_(e_cannot_connect));
770 sock_close(sd);
771 channel_free(channel);
772 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100773 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100774
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100775 /* Limit the waittime to 50 msec. If it doesn't work within this
776 * time we close the socket and try creating it again. */
777 waitnow = waittime > 50 ? 50 : waittime;
778
Bram Moolenaar045a2842016-03-08 22:33:07 +0100779 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100780 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100781#ifndef WIN32
782 if (errno != ECONNREFUSED)
783#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100784 {
785 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100786 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100787 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100788#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100789 int so_error = 0;
790 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100791 struct timeval start_tv;
792 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100793#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100794 FD_ZERO(&rfds);
795 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100796 FD_ZERO(&wfds);
797 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100798
Bram Moolenaar562ca712016-03-09 21:50:05 +0100799 tv.tv_sec = waitnow / 1000;
800 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100801#ifndef WIN32
802 gettimeofday(&start_tv, NULL);
803#endif
804 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100805 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100807
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 if (ret < 0)
809 {
810 SOCK_ERRNO;
811 ch_errorn(channel,
812 "channel_open: Connect failed with errno %d", errno);
813 PERROR(_(e_cannot_connect));
814 sock_close(sd);
815 channel_free(channel);
816 return NULL;
817 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818
Bram Moolenaare081e212016-02-28 22:33:46 +0100819#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100820 /* On Win32: select() is expected to work and wait for up to
821 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100822 if (FD_ISSET(sd, &wfds))
823 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100824 elapsed_msec = waitnow;
825 if (waittime > 1 && elapsed_msec < waittime)
826 {
827 waittime -= elapsed_msec;
828 continue;
829 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100830#else
831 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100832 * After putting the socket in non-blocking mode, connect() will
833 * return EINPROGRESS, select() will not wait (as if writing is
834 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100835 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100837 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100838 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100839 {
840 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100841 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100842 if (ret < 0 || (so_error != 0
843 && so_error != EWOULDBLOCK
844 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100845# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100846 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100847# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100848 ))
849 {
850 ch_errorn(channel,
851 "channel_open: Connect failed with errno %d",
852 so_error);
853 PERROR(_(e_cannot_connect));
854 sock_close(sd);
855 channel_free(channel);
856 return NULL;
857 }
858 }
859
Bram Moolenaar045a2842016-03-08 22:33:07 +0100860 if (FD_ISSET(sd, &wfds) && so_error == 0)
861 /* Did not detect an error, connection is established. */
862 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100863
Bram Moolenaar045a2842016-03-08 22:33:07 +0100864 gettimeofday(&end_tv, NULL);
865 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
866 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100867#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100868 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100869
870#ifndef WIN32
871 if (waittime > 1 && elapsed_msec < waittime)
872 {
873 /* The port isn't ready but we also didn't get an error.
874 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100875 * yet. Select() may return early, wait until the remaining
876 * "waitnow" and try again. */
877 waitnow -= elapsed_msec;
878 waittime -= elapsed_msec;
879 if (waitnow > 0)
880 {
881 mch_delay((long)waitnow, TRUE);
882 ui_breakcheck();
883 waittime -= waitnow;
884 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100885 if (!got_int)
886 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100887 if (waittime <= 0)
888 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100889 waittime = 1;
890 continue;
891 }
892 /* we were interrupted, behave as if timed out */
893 }
894#endif
895
896 /* We timed out. */
897 ch_error(channel, "Connection timed out");
898 sock_close(sd);
899 channel_free(channel);
900 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100901 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100902
Bram Moolenaar045a2842016-03-08 22:33:07 +0100903 ch_log(channel, "Connection made");
904
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100905 if (waittime >= 0)
906 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100907#ifdef _WIN32
908 val = 0;
909 ioctlsocket(sd, FIONBIO, &val);
910#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100911 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100912#endif
913 }
914
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100915 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100916 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100917 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
918 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100919
920#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100921 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100922#endif
923
Bram Moolenaar77073442016-02-13 23:23:53 +0100924 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100925}
926
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100927/*
928 * Implements ch_open().
929 */
930 channel_T *
931channel_open_func(typval_T *argvars)
932{
933 char_u *address;
934 char_u *p;
935 char *rest;
936 int port;
937 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200938 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100939
940 address = get_tv_string(&argvars[0]);
941 if (argvars[1].v_type != VAR_UNKNOWN
942 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
943 {
944 EMSG(_(e_invarg));
945 return NULL;
946 }
947
948 /* parse address */
949 p = vim_strchr(address, ':');
950 if (p == NULL)
951 {
952 EMSG2(_(e_invarg2), address);
953 return NULL;
954 }
955 *p++ = NUL;
956 port = strtol((char *)p, &rest, 10);
957 if (*address == NUL || port <= 0 || *rest != NUL)
958 {
959 p[-1] = ':';
960 EMSG2(_(e_invarg2), address);
961 return NULL;
962 }
963
964 /* parse options */
965 clear_job_options(&opt);
966 opt.jo_mode = MODE_JSON;
967 opt.jo_timeout = 2000;
968 if (get_job_options(&argvars[1], &opt,
969 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200970 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100971 if (opt.jo_timeout < 0)
972 {
973 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200974 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100975 }
976
977 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
978 if (channel != NULL)
979 {
980 opt.jo_set = JO_ALL;
981 channel_set_options(channel, &opt);
982 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200983theend:
984 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100985 return channel;
986}
987
Bram Moolenaarde279892016-03-11 22:19:44 +0100988 static void
989may_close_part(sock_T *fd)
990{
991 if (*fd != INVALID_FD)
992 {
993 fd_close(*fd);
994 *fd = INVALID_FD;
995 }
996}
997
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100998 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100999channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001000{
Bram Moolenaarde279892016-03-11 22:19:44 +01001001 if (in != INVALID_FD)
1002 {
1003 may_close_part(&channel->CH_IN_FD);
1004 channel->CH_IN_FD = in;
1005 }
1006 if (out != INVALID_FD)
1007 {
1008# if defined(FEAT_GUI)
1009 channel_gui_unregister_one(channel, PART_OUT);
1010# endif
1011 may_close_part(&channel->CH_OUT_FD);
1012 channel->CH_OUT_FD = out;
1013# if defined(FEAT_GUI)
1014 channel_gui_register_one(channel, PART_OUT);
1015# endif
1016 }
1017 if (err != INVALID_FD)
1018 {
1019# if defined(FEAT_GUI)
1020 channel_gui_unregister_one(channel, PART_ERR);
1021# endif
1022 may_close_part(&channel->CH_ERR_FD);
1023 channel->CH_ERR_FD = err;
1024# if defined(FEAT_GUI)
1025 channel_gui_register_one(channel, PART_ERR);
1026# endif
1027 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001028}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001029
Bram Moolenaard6051b52016-02-28 15:49:03 +01001030/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001031 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001032 * This does not keep a refcount, when the job is freed ch_job is cleared.
1033 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001034 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001035channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001036{
Bram Moolenaar77073442016-02-13 23:23:53 +01001037 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001038
1039 channel_set_options(channel, options);
1040
1041 if (job->jv_in_buf != NULL)
1042 {
1043 chanpart_T *in_part = &channel->ch_part[PART_IN];
1044
1045 in_part->ch_buffer = job->jv_in_buf;
1046 ch_logs(channel, "reading from buffer '%s'",
1047 (char *)in_part->ch_buffer->b_ffname);
1048 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001049 {
1050 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1051 {
1052 /* Special mode: send last-but-one line when appending a line
1053 * to the buffer. */
1054 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001055 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001056 in_part->ch_buf_top =
1057 in_part->ch_buffer->b_ml.ml_line_count + 1;
1058 }
1059 else
1060 in_part->ch_buf_top = options->jo_in_top;
1061 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001062 else
1063 in_part->ch_buf_top = 1;
1064 if (options->jo_set & JO_IN_BOT)
1065 in_part->ch_buf_bot = options->jo_in_bot;
1066 else
1067 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1068 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001069}
1070
1071/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001072 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001073 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001074 */
1075 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001076find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001077{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001078 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001079 buf_T *save_curbuf = curbuf;
1080
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001081 if (name != NULL && *name != NUL)
1082 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001083 if (buf == NULL)
1084 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001085 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001086 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001087 if (buf == NULL)
1088 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001089 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001090 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001091#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001092 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1093 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001094#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001095 if (curbuf->b_ml.ml_mfp == NULL)
1096 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001097 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1098 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001099 changed_bytes(1, 0);
1100 curbuf = save_curbuf;
1101 }
1102
1103 return buf;
1104}
1105
1106/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001107 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001108 */
1109 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001110channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001111{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001112 int part;
1113 char_u **cbp;
1114 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001115
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001116 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001117 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001118 channel->ch_part[part].ch_mode = opt->jo_mode;
1119 if (opt->jo_set & JO_IN_MODE)
1120 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1121 if (opt->jo_set & JO_OUT_MODE)
1122 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1123 if (opt->jo_set & JO_ERR_MODE)
1124 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001125
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001126 if (opt->jo_set & JO_TIMEOUT)
1127 for (part = PART_SOCK; part <= PART_IN; ++part)
1128 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1129 if (opt->jo_set & JO_OUT_TIMEOUT)
1130 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1131 if (opt->jo_set & JO_ERR_TIMEOUT)
1132 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001133 if (opt->jo_set & JO_BLOCK_WRITE)
1134 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001135
1136 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001137 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001138 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001139 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001140 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001141 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001142 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1143 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001144 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001145 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001146 *pp = opt->jo_partial;
1147 if (*pp != NULL)
1148 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001149 }
1150 if (opt->jo_set & JO_OUT_CALLBACK)
1151 {
1152 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001153 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001154 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001155 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001156 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1157 *cbp = vim_strsave(opt->jo_out_cb);
1158 else
1159 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001160 *pp = opt->jo_out_partial;
1161 if (*pp != NULL)
1162 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001163 }
1164 if (opt->jo_set & JO_ERR_CALLBACK)
1165 {
1166 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001167 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001168 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001169 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001170 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1171 *cbp = vim_strsave(opt->jo_err_cb);
1172 else
1173 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001174 *pp = opt->jo_err_partial;
1175 if (*pp != NULL)
1176 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001177 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001178 if (opt->jo_set & JO_CLOSE_CALLBACK)
1179 {
1180 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001181 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001182 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001183 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001184 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1185 *cbp = vim_strsave(opt->jo_close_cb);
1186 else
1187 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001188 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001189 if (*pp != NULL)
1190 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001191 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001192
1193 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1194 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001195 buf_T *buf;
1196
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001197 /* writing output to a buffer. Default mode is NL. */
1198 if (!(opt->jo_set & JO_OUT_MODE))
1199 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001200 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001201 {
1202 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1203 if (buf == NULL)
1204 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1205 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001206 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001207 {
1208 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1209 }
1210 if (buf != NULL)
1211 {
1212 ch_logs(channel, "writing out to buffer '%s'",
1213 (char *)buf->b_ffname);
1214 channel->ch_part[PART_OUT].ch_buffer = buf;
1215 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001216 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001217
1218 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1219 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1220 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1221 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001222 buf_T *buf;
1223
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001224 /* writing err to a buffer. Default mode is NL. */
1225 if (!(opt->jo_set & JO_ERR_MODE))
1226 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1227 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001228 buf = channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001229 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001230 {
1231 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1232 if (buf == NULL)
1233 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1234 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001235 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001236 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1237 if (buf != NULL)
1238 {
1239 ch_logs(channel, "writing err to buffer '%s'",
1240 (char *)buf->b_ffname);
1241 channel->ch_part[PART_ERR].ch_buffer = buf;
1242 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001243 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001244
1245 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1246 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1247 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001248}
1249
1250/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001251 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001252 */
1253 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001254channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001255 channel_T *channel,
1256 int part,
1257 char_u *callback,
1258 partial_T *partial,
1259 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001260{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001261 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001262 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1263
1264 if (item != NULL)
1265 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001266 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001267 item->cq_partial = partial;
1268 if (partial != NULL)
1269 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001270 item->cq_seq_nr = id;
1271 item->cq_prev = head->cq_prev;
1272 head->cq_prev = item;
1273 item->cq_next = NULL;
1274 if (item->cq_prev == NULL)
1275 head->cq_next = item;
1276 else
1277 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001278 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001279}
1280
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001281 static void
1282write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1283{
1284 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001285 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001286 char_u *p;
1287
Bram Moolenaar655da312016-05-28 22:22:34 +02001288 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001289 if ((p = alloc(len + 2)) == NULL)
1290 return;
1291 STRCPY(p, line);
1292 p[len] = NL;
1293 p[len + 1] = NUL;
1294 channel_send(channel, PART_IN, p, "write_buf_line()");
1295 vim_free(p);
1296}
1297
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001298/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001299 * Return TRUE if "channel" can be written to.
1300 * Returns FALSE if the input is closed or the write would block.
1301 */
1302 static int
1303can_write_buf_line(channel_T *channel)
1304{
1305 chanpart_T *in_part = &channel->ch_part[PART_IN];
1306
1307 if (in_part->ch_fd == INVALID_FD)
1308 return FALSE; /* pipe was closed */
1309
1310 /* for testing: block every other attempt to write */
1311 if (in_part->ch_block_write == 1)
1312 in_part->ch_block_write = -1;
1313 else if (in_part->ch_block_write == -1)
1314 in_part->ch_block_write = 1;
1315
1316 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1317#ifndef WIN32
1318 {
1319# if defined(HAVE_SELECT)
1320 struct timeval tval;
1321 fd_set wfds;
1322 int ret;
1323
1324 FD_ZERO(&wfds);
1325 FD_SET((int)in_part->ch_fd, &wfds);
1326 tval.tv_sec = 0;
1327 tval.tv_usec = 0;
1328 for (;;)
1329 {
1330 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1331# ifdef EINTR
1332 SOCK_ERRNO;
1333 if (ret == -1 && errno == EINTR)
1334 continue;
1335# endif
1336 if (ret <= 0 || in_part->ch_block_write == 1)
1337 {
1338 if (ret > 0)
1339 ch_log(channel, "FAKED Input not ready for writing");
1340 else
1341 ch_log(channel, "Input not ready for writing");
1342 return FALSE;
1343 }
1344 break;
1345 }
1346# else
1347 struct pollfd fds;
1348
1349 fds.fd = in_part->ch_fd;
1350 fds.events = POLLOUT;
1351 if (poll(&fds, 1, 0) <= 0)
1352 {
1353 ch_log(channel, "Input not ready for writing");
1354 return FALSE;
1355 }
1356 if (in_part->ch_block_write == 1)
1357 {
1358 ch_log(channel, "FAKED Input not ready for writing");
1359 return FALSE;
1360 }
1361# endif
1362 }
1363#endif
1364 return TRUE;
1365}
1366
1367/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001368 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001369 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001370 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001371channel_write_in(channel_T *channel)
1372{
1373 chanpart_T *in_part = &channel->ch_part[PART_IN];
1374 linenr_T lnum;
1375 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001376 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001377
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001378 if (buf == NULL || in_part->ch_buf_append)
1379 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001380 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1381 {
1382 /* buffer was wiped out or unloaded */
1383 in_part->ch_buffer = NULL;
1384 return;
1385 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001386
1387 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1388 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1389 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001390 if (!can_write_buf_line(channel))
1391 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001392 write_buf_line(buf, lnum, channel);
1393 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001394 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001395
1396 if (written == 1)
1397 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1398 else if (written > 1)
1399 ch_logn(channel, "written %d lines to channel", written);
1400
Bram Moolenaar014069a2016-03-03 22:51:40 +01001401 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001402 if (lnum > buf->b_ml.ml_line_count)
1403 {
1404 /* Writing is done, no longer need the buffer. */
1405 in_part->ch_buffer = NULL;
1406 ch_log(channel, "Finished writing all lines to channel");
1407 }
1408 else
1409 ch_logn(channel, "Still %d more lines to write",
1410 buf->b_ml.ml_line_count - lnum + 1);
1411}
1412
1413/*
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001414 * Handle buffer "buf" beeing freed, remove it from any channels.
1415 */
1416 void
1417channel_buffer_free(buf_T *buf)
1418{
1419 channel_T *channel;
1420 int part;
1421
1422 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1423 for (part = PART_SOCK; part <= PART_IN; ++part)
1424 {
1425 chanpart_T *ch_part = &channel->ch_part[part];
1426
1427 if (ch_part->ch_buffer == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001428 {
1429 ch_logs(channel, "%s buffer has been wiped out",
1430 part_names[part]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001431 ch_part->ch_buffer = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001432 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001433 }
1434}
1435
1436/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001437 * Write any lines waiting to be written to a channel.
1438 */
1439 void
1440channel_write_any_lines()
1441{
1442 channel_T *channel;
1443
1444 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1445 {
1446 chanpart_T *in_part = &channel->ch_part[PART_IN];
1447
1448 if (in_part->ch_buffer != NULL)
1449 {
1450 if (in_part->ch_buf_append)
1451 channel_write_new_lines(in_part->ch_buffer);
1452 else
1453 channel_write_in(channel);
1454 }
1455 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001456}
1457
1458/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001459 * Write appended lines above the last one in "buf" to the channel.
1460 */
1461 void
1462channel_write_new_lines(buf_T *buf)
1463{
1464 channel_T *channel;
1465 int found_one = FALSE;
1466
1467 /* There could be more than one channel for the buffer, loop over all of
1468 * them. */
1469 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1470 {
1471 chanpart_T *in_part = &channel->ch_part[PART_IN];
1472 linenr_T lnum;
1473 int written = 0;
1474
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001475 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001476 {
1477 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001478 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001479 found_one = TRUE;
1480 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1481 ++lnum)
1482 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001483 if (!can_write_buf_line(channel))
1484 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001485 write_buf_line(buf, lnum, channel);
1486 ++written;
1487 }
1488
1489 if (written == 1)
1490 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1491 else if (written > 1)
1492 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001493 if (lnum < buf->b_ml.ml_line_count)
1494 ch_logn(channel, "Still %d more lines to write",
1495 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001496
1497 in_part->ch_buf_bot = lnum;
1498 }
1499 }
1500 if (!found_one)
1501 buf->b_write_to_channel = FALSE;
1502}
1503
1504/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001505 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001506 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001507 */
1508 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001509invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1510 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001511{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001512 typval_T rettv;
1513 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001514
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001515 if (safe_to_invoke_callback == 0)
1516 EMSG("INTERNAL: Invoking callback when it is not safe");
1517
Bram Moolenaar77073442016-02-13 23:23:53 +01001518 argv[0].v_type = VAR_CHANNEL;
1519 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001520
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001521 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001522 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001523 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001524 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001525}
1526
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001527/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001528 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001529 * The caller must free it.
1530 * Returns NULL if there is nothing.
1531 */
1532 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001533channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001534{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001535 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001536 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001537 char_u *p;
1538
Bram Moolenaar77073442016-02-13 23:23:53 +01001539 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001540 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001541 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001542 p = node->rq_buffer;
1543 head->rq_next = node->rq_next;
1544 if (node->rq_next == NULL)
1545 head->rq_prev = NULL;
1546 else
1547 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001548 vim_free(node);
1549 return p;
1550}
1551
1552/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001553 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001554 */
1555 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001556channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001557{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001558 readq_T *head = &channel->ch_part[part].ch_head;
1559 readq_T *node = head->rq_next;
1560 long_u len = 1;
1561 char_u *res;
1562 char_u *p;
1563
1564 /* If there is only one buffer just get that one. */
1565 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1566 return channel_get(channel, part);
1567
1568 /* Concatenate everything into one buffer. */
1569 for (node = head->rq_next; node != NULL; node = node->rq_next)
1570 len += (long_u)STRLEN(node->rq_buffer);
1571 res = lalloc(len, TRUE);
1572 if (res == NULL)
1573 return NULL;
1574 *res = NUL;
1575 for (node = head->rq_next; node != NULL; node = node->rq_next)
1576 STRCAT(res, node->rq_buffer);
1577
1578 /* Free all buffers */
1579 do
1580 {
1581 p = channel_get(channel, part);
1582 vim_free(p);
1583 } while (p != NULL);
1584
1585 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001586}
1587
1588/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001589 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001590 * Returns FAIL if that is not possible.
1591 */
1592 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001593channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001594{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001595 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001596 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001597 char_u *p;
1598
Bram Moolenaar77073442016-02-13 23:23:53 +01001599 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001600 return FAIL;
1601
Bram Moolenaar77073442016-02-13 23:23:53 +01001602 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1603 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001604 if (p == NULL)
1605 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001606 STRCPY(p, node->rq_buffer);
1607 STRCAT(p, node->rq_next->rq_buffer);
1608 vim_free(node->rq_next->rq_buffer);
1609 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001610
Bram Moolenaar77073442016-02-13 23:23:53 +01001611 /* dispose of the node and its buffer */
1612 head->rq_next = node->rq_next;
1613 head->rq_next->rq_prev = NULL;
1614 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001615 vim_free(node);
1616 return OK;
1617}
1618
1619/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001620 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001621 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001622 * Returns OK or FAIL.
1623 */
1624 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001625channel_save(channel_T *channel, int part, char_u *buf, int len,
1626 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001627{
1628 readq_T *node;
1629 readq_T *head = &channel->ch_part[part].ch_head;
1630 char_u *p;
1631 int i;
1632
1633 node = (readq_T *)alloc(sizeof(readq_T));
1634 if (node == NULL)
1635 return FAIL; /* out of memory */
1636 node->rq_buffer = alloc(len + 1);
1637 if (node->rq_buffer == NULL)
1638 {
1639 vim_free(node);
1640 return FAIL; /* out of memory */
1641 }
1642
1643 if (channel->ch_part[part].ch_mode == MODE_NL)
1644 {
1645 /* Drop any CR before a NL. */
1646 p = node->rq_buffer;
1647 for (i = 0; i < len; ++i)
1648 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1649 *p++ = buf[i];
1650 *p = NUL;
1651 }
1652 else
1653 {
1654 mch_memmove(node->rq_buffer, buf, len);
1655 node->rq_buffer[len] = NUL;
1656 }
1657
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001658 if (prepend)
1659 {
1660 /* preend node to the head of the queue */
1661 node->rq_next = head->rq_next;
1662 node->rq_prev = NULL;
1663 if (head->rq_next == NULL)
1664 head->rq_prev = node;
1665 else
1666 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001667 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001668 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001669 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001670 {
1671 /* append node to the tail of the queue */
1672 node->rq_next = NULL;
1673 node->rq_prev = head->rq_prev;
1674 if (head->rq_prev == NULL)
1675 head->rq_next = node;
1676 else
1677 head->rq_prev->rq_next = node;
1678 head->rq_prev = node;
1679 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001680
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001681 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001682 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001683 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001684 fprintf(log_fd, "'");
1685 if (fwrite(buf, len, 1, log_fd) != 1)
1686 return FAIL;
1687 fprintf(log_fd, "'\n");
1688 }
1689 return OK;
1690}
1691
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001692 static int
1693channel_fill(js_read_T *reader)
1694{
1695 channel_T *channel = (channel_T *)reader->js_cookie;
1696 int part = reader->js_cookie_arg;
1697 char_u *next = channel_get(channel, part);
1698 int unused;
1699 int len;
1700 char_u *p;
1701
1702 if (next == NULL)
1703 return FALSE;
1704
1705 unused = reader->js_end - reader->js_buf - reader->js_used;
1706 if (unused > 0)
1707 {
1708 /* Prepend unused text. */
1709 len = (int)STRLEN(next);
1710 p = alloc(unused + len + 1);
1711 if (p == NULL)
1712 {
1713 vim_free(next);
1714 return FALSE;
1715 }
1716 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1717 mch_memmove(p + unused, next, len + 1);
1718 vim_free(next);
1719 next = p;
1720 }
1721
1722 vim_free(reader->js_buf);
1723 reader->js_buf = next;
1724 reader->js_used = 0;
1725 return TRUE;
1726}
1727
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001728/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001729 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001730 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001731 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001732 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001733 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001734channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001735{
1736 js_read_T reader;
1737 typval_T listtv;
1738 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001739 chanpart_T *chanpart = &channel->ch_part[part];
1740 jsonq_T *head = &chanpart->ch_json_head;
1741 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001742 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001743
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001744 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001745 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001746
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001747 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001748 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001749 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001750 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001751 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001752
1753 /* When a message is incomplete we wait for a short while for more to
1754 * arrive. After the delay drop the input, otherwise a truncated string
1755 * or list will make us hang. */
1756 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001757 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001758 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001759 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001760 /* Only accept the response when it is a list with at least two
1761 * items. */
1762 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001763 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001764 if (listtv.v_type != VAR_LIST)
1765 ch_error(channel, "Did not receive a list, discarding");
1766 else
1767 ch_errorn(channel, "Expected list with two items, got %d",
1768 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001769 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001770 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001771 else
1772 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001773 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1774 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001775 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001776 else
1777 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001778 item->jq_value = alloc_tv();
1779 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001780 {
1781 vim_free(item);
1782 clear_tv(&listtv);
1783 }
1784 else
1785 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001786 *item->jq_value = listtv;
1787 item->jq_prev = head->jq_prev;
1788 head->jq_prev = item;
1789 item->jq_next = NULL;
1790 if (item->jq_prev == NULL)
1791 head->jq_next = item;
1792 else
1793 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001794 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001795 }
1796 }
1797 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001798
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001799 if (status == OK)
1800 chanpart->ch_waiting = FALSE;
1801 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001802 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001803 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001804 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001805 /* First time encountering incomplete message, set a deadline of
1806 * 100 msec. */
1807 ch_log(channel, "Incomplete message - wait for more");
1808 reader.js_used = 0;
1809 chanpart->ch_waiting = TRUE;
1810#ifdef WIN32
1811 chanpart->ch_deadline = GetTickCount() + 100L;
1812#else
1813 gettimeofday(&chanpart->ch_deadline, NULL);
1814 chanpart->ch_deadline.tv_usec += 100 * 1000;
1815 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1816 {
1817 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1818 ++chanpart->ch_deadline.tv_sec;
1819 }
1820#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001821 }
1822 else
1823 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001824 int timeout;
1825#ifdef WIN32
1826 timeout = GetTickCount() > chanpart->ch_deadline;
1827#else
1828 {
1829 struct timeval now_tv;
1830
1831 gettimeofday(&now_tv, NULL);
1832 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1833 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1834 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1835 }
1836#endif
1837 if (timeout)
1838 {
1839 status = FAIL;
1840 chanpart->ch_waiting = FALSE;
1841 }
1842 else
1843 {
1844 reader.js_used = 0;
1845 ch_log(channel, "still waiting on incomplete message");
1846 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001847 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001848 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001849
1850 if (status == FAIL)
1851 {
1852 ch_error(channel, "Decoding failed - discarding input");
1853 ret = FALSE;
1854 chanpart->ch_waiting = FALSE;
1855 }
1856 else if (reader.js_buf[reader.js_used] != NUL)
1857 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001858 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001859 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001860 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1861 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001862 ret = status == MAYBE ? FALSE: TRUE;
1863 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001864 else
1865 ret = FALSE;
1866
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001867 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001868 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001869}
1870
1871/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001872 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001873 */
1874 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001875remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001876{
Bram Moolenaar77073442016-02-13 23:23:53 +01001877 if (node->cq_prev == NULL)
1878 head->cq_next = node->cq_next;
1879 else
1880 node->cq_prev->cq_next = node->cq_next;
1881 if (node->cq_next == NULL)
1882 head->cq_prev = node->cq_prev;
1883 else
1884 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001885}
1886
1887/*
1888 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001889 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001890 */
1891 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001892remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001893{
Bram Moolenaar77073442016-02-13 23:23:53 +01001894 if (node->jq_prev == NULL)
1895 head->jq_next = node->jq_next;
1896 else
1897 node->jq_prev->jq_next = node->jq_next;
1898 if (node->jq_next == NULL)
1899 head->jq_prev = node->jq_prev;
1900 else
1901 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001902 vim_free(node);
1903}
1904
1905/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001906 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001907 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001908 * When "id" is zero or negative jut get the first message. But not the one
1909 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001910 * Return OK when found and return the value in "rettv".
1911 * Return FAIL otherwise.
1912 */
1913 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001914channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001915{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001916 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001917 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001918
Bram Moolenaar77073442016-02-13 23:23:53 +01001919 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001920 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001921 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001922 typval_T *tv = &l->lv_first->li_tv;
1923
1924 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001925 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001926 || tv->vval.v_number == 0
1927 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001928 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001929 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001930 if (tv->v_type == VAR_NUMBER)
1931 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001932 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001933 return OK;
1934 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001935 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001936 }
1937 return FAIL;
1938}
1939
Bram Moolenaarece61b02016-02-20 21:39:05 +01001940#define CH_JSON_MAX_ARGS 4
1941
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001942/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001943 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001944 * "argv[0]" is the command string.
1945 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001946 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001947 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001948channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001949{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001950 char_u *cmd = argv[0].vval.v_string;
1951 char_u *arg;
1952 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001953
Bram Moolenaarece61b02016-02-20 21:39:05 +01001954 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001955 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001956 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001957 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001958 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001959 return;
1960 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001961 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001962 if (arg == NULL)
1963 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001964
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001965 if (STRCMP(cmd, "ex") == 0)
1966 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001967 int save_called_emsg = called_emsg;
1968
1969 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001970 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001971 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001972 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001973 --emsg_silent;
1974 if (called_emsg)
1975 ch_logs(channel, "Ex command error: '%s'",
1976 (char *)get_vim_var_str(VV_ERRMSG));
1977 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001978 }
1979 else if (STRCMP(cmd, "normal") == 0)
1980 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001981 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001982
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001983 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001984 ea.arg = arg;
1985 ea.addr_count = 0;
1986 ea.forceit = TRUE; /* no mapping */
1987 ex_normal(&ea);
1988 }
1989 else if (STRCMP(cmd, "redraw") == 0)
1990 {
1991 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001992
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001993 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001994 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001995 ex_redraw(&ea);
1996 showruler(FALSE);
1997 setcursor();
1998 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001999#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002000 if (gui.in_use)
2001 {
2002 gui_update_cursor(FALSE, FALSE);
2003 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002004 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002005#endif
2006 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002007 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002008 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002009 int is_call = cmd[0] == 'c';
2010 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002011
Bram Moolenaarece61b02016-02-20 21:39:05 +01002012 if (argv[id_idx].v_type != VAR_UNKNOWN
2013 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002014 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002015 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002016 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002017 EMSG("E904: last argument for expr/call must be a number");
2018 }
2019 else if (is_call && argv[2].v_type != VAR_LIST)
2020 {
2021 ch_error(channel, "third argument for call must be a list");
2022 if (p_verbose > 2)
2023 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002024 }
2025 else
2026 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002027 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002028 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002029 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002030 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002031
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002032 /* Don't pollute the display with errors. */
2033 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002034 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002035 {
2036 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002037 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002038 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002039 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002040 {
2041 ch_logs(channel, "Calling '%s'", (char *)arg);
2042 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2043 tv = &res_tv;
2044 else
2045 tv = NULL;
2046 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002047
2048 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002049 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002050 int id = argv[id_idx].vval.v_number;
2051
Bram Moolenaar55fab432016-02-07 16:53:13 +01002052 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002053 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002054 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002055 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002056 /* If evaluation failed or the result can't be encoded
2057 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002058 vim_free(json);
2059 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002060 err_tv.v_type = VAR_STRING;
2061 err_tv.vval.v_string = (char_u *)"ERROR";
2062 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002063 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002064 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002065 if (json != NULL)
2066 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002067 channel_send(channel,
2068 part == PART_SOCK ? PART_SOCK : PART_IN,
2069 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002070 vim_free(json);
2071 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002072 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002073 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002074 if (tv == &res_tv)
2075 clear_tv(tv);
2076 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002077 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002078 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002079 }
2080 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002081 {
2082 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002083 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002084 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002085}
2086
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002087/*
2088 * Invoke the callback at "cbhead".
2089 * Does not redraw but sets channel_need_redraw.
2090 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002091 static void
2092invoke_one_time_callback(
2093 channel_T *channel,
2094 cbq_T *cbhead,
2095 cbq_T *item,
2096 typval_T *argv)
2097{
2098 ch_logs(channel, "Invoking one-time callback %s",
2099 (char *)item->cq_callback);
2100 /* Remove the item from the list first, if the callback
2101 * invokes ch_close() the list will be cleared. */
2102 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002103 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002104 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002105 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002106 vim_free(item);
2107}
2108
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002109 static void
2110append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2111{
2112 buf_T *save_curbuf = curbuf;
2113 linenr_T lnum = buffer->b_ml.ml_line_count;
2114 int save_write_to = buffer->b_write_to_channel;
2115
2116 /* If the buffer is also used as input insert above the last
2117 * line. Don't write these lines. */
2118 if (save_write_to)
2119 {
2120 --lnum;
2121 buffer->b_write_to_channel = FALSE;
2122 }
2123
2124 /* Append to the buffer */
2125 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2126
2127 curbuf = buffer;
2128 u_sync(TRUE);
2129 /* ignore undo failure, undo is not very useful here */
2130 ignored = u_save(lnum, lnum + 1);
2131
2132 ml_append(lnum, msg, 0, FALSE);
2133 appended_lines_mark(lnum, 1L);
2134 curbuf = save_curbuf;
2135
2136 if (buffer->b_nwindows > 0)
2137 {
2138 win_T *wp;
2139 win_T *save_curwin;
2140
2141 FOR_ALL_WINDOWS(wp)
2142 {
2143 if (wp->w_buffer == buffer
2144 && (save_write_to
2145 ? wp->w_cursor.lnum == lnum + 1
2146 : (wp->w_cursor.lnum == lnum
2147 && wp->w_cursor.col == 0)))
2148 {
2149 ++wp->w_cursor.lnum;
2150 save_curwin = curwin;
2151 curwin = wp;
2152 curbuf = curwin->w_buffer;
2153 scroll_cursor_bot(0, FALSE);
2154 curwin = save_curwin;
2155 curbuf = curwin->w_buffer;
2156 }
2157 }
2158 redraw_buf_later(buffer, VALID);
2159 channel_need_redraw = TRUE;
2160 }
2161
2162 if (save_write_to)
2163 {
2164 channel_T *ch;
2165
2166 /* Find channels reading from this buffer and adjust their
2167 * next-to-read line number. */
2168 buffer->b_write_to_channel = TRUE;
2169 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2170 {
2171 chanpart_T *in_part = &ch->ch_part[PART_IN];
2172
2173 if (in_part->ch_buffer == buffer)
2174 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2175 }
2176 }
2177}
2178
Bram Moolenaar437905c2016-04-26 19:01:05 +02002179 static void
2180drop_messages(channel_T *channel, int part)
2181{
2182 char_u *msg;
2183
2184 while ((msg = channel_get(channel, part)) != NULL)
2185 {
2186 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2187 vim_free(msg);
2188 }
2189}
2190
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002191/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002192 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002193 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002194 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002195 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002196 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002197may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002198{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002199 char_u *msg = NULL;
2200 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002201 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002202 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002203 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002204 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002205 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002206 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002207 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002208 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002209
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002210 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002211 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002212 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002213
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002214 /* Use a message-specific callback, part callback or channel callback */
2215 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2216 if (cbitem->cq_seq_nr == 0)
2217 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002218 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002219 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002220 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002221 partial = cbitem->cq_partial;
2222 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002223 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002224 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002225 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002226 partial = channel->ch_part[part].ch_partial;
2227 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002228 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002229 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002230 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002231 partial = channel->ch_partial;
2232 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002233
Bram Moolenaar187db502016-02-27 14:44:26 +01002234 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002235 if (buffer != NULL && !buf_valid(buffer))
2236 {
2237 /* buffer was wiped out */
2238 channel->ch_part[part].ch_buffer = NULL;
2239 buffer = NULL;
2240 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002241
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002242 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002243 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002244 listitem_T *item;
2245 int argc = 0;
2246
Bram Moolenaard7ece102016-02-02 23:23:02 +01002247 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002248 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002249 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002250 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002251 channel_parse_json(channel, part);
2252 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002253 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002254 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002255
Bram Moolenaarece61b02016-02-20 21:39:05 +01002256 for (item = listtv->vval.v_list->lv_first;
2257 item != NULL && argc < CH_JSON_MAX_ARGS;
2258 item = item->li_next)
2259 argv[argc++] = item->li_tv;
2260 while (argc < CH_JSON_MAX_ARGS)
2261 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002262
Bram Moolenaarece61b02016-02-20 21:39:05 +01002263 if (argv[0].v_type == VAR_STRING)
2264 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002265 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002266 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002267 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002268 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002269 }
2270
Bram Moolenaarece61b02016-02-20 21:39:05 +01002271 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002272 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002273 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002274 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002275 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002276 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002277 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002278 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002279 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002280 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002281 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002282 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002283 return FALSE;
2284 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002285 else
2286 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002287 /* If there is no callback or buffer drop the message. */
2288 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002289 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002290 /* If there is a close callback it may use ch_read() to get the
2291 * messages. */
2292 if (channel->ch_close_cb == NULL)
2293 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002294 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002295 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002296
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002297 if (ch_mode == MODE_NL)
2298 {
2299 char_u *nl;
2300 char_u *buf;
2301
2302 /* See if we have a message ending in NL in the first buffer. If
2303 * not try to concatenate the first and the second buffer. */
2304 while (TRUE)
2305 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002306 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002307 nl = vim_strchr(buf, NL);
2308 if (nl != NULL)
2309 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002310 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002311 return FALSE; /* incomplete message */
2312 }
2313 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002314 {
2315 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002316 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002317 *nl = NUL;
2318 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002319 else
2320 {
2321 /* Copy the message into allocated memory and remove it from
2322 * the buffer. */
2323 msg = vim_strnsave(buf, (int)(nl - buf));
2324 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2325 }
2326 }
2327 else
2328 /* For a raw channel we don't know where the message ends, just
2329 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002330 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002331
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002332 if (msg == NULL)
2333 return FALSE; /* out of memory (and avoids Coverity warning) */
2334
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002335 argv[1].v_type = VAR_STRING;
2336 argv[1].vval.v_string = msg;
2337 }
2338
Bram Moolenaara07fec92016-02-05 21:04:08 +01002339 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002340 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002341 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002342
2343 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002344 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002345 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002346 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002347 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002348 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002349 break;
2350 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002351 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002352 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002353 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002354 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002355 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002356 if (buffer != NULL)
2357 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002358 if (msg == NULL)
2359 /* JSON or JS mode: re-encode the message. */
2360 msg = json_encode(listtv, ch_mode);
2361 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002362 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002363 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002364
Bram Moolenaar187db502016-02-27 14:44:26 +01002365 if (callback != NULL)
2366 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002367 if (cbitem != NULL)
2368 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2369 else
2370 {
2371 /* invoke the channel callback */
2372 ch_logs(channel, "Invoking channel callback %s",
2373 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002374 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002375 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002376 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002377 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002378 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002379 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002380
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002381 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002382 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002383 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002384
2385 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002386}
2387
2388/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002389 * Return TRUE when channel "channel" is open for writing to.
2390 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002391 */
2392 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002393channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002394{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002395 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002396 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002397}
2398
2399/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002400 * Return TRUE when channel "channel" is open for reading or writing.
2401 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002402 */
2403 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002404channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002405{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002406 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002407 || channel->CH_IN_FD != INVALID_FD
2408 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002409 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002410}
2411
2412/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002413 * Return TRUE if "channel" has JSON or other typeahead.
2414 */
2415 static int
2416channel_has_readahead(channel_T *channel, int part)
2417{
2418 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2419
2420 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2421 {
2422 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2423 jsonq_T *item = head->jq_next;
2424
2425 return item != NULL;
2426 }
2427 return channel_peek(channel, part) != NULL;
2428}
2429
2430/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002431 * Return a string indicating the status of the channel.
2432 */
2433 char *
2434channel_status(channel_T *channel)
2435{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002436 int part;
2437 int has_readahead = FALSE;
2438
Bram Moolenaar77073442016-02-13 23:23:53 +01002439 if (channel == NULL)
2440 return "fail";
2441 if (channel_is_open(channel))
2442 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002443 for (part = PART_SOCK; part <= PART_ERR; ++part)
2444 if (channel_has_readahead(channel, part))
2445 {
2446 has_readahead = TRUE;
2447 break;
2448 }
2449
2450 if (has_readahead)
2451 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002452 return "closed";
2453}
2454
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002455 static void
2456channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2457{
2458 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002459 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002460 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002461 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002462
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002463 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002464 STRCAT(namebuf, "_");
2465 tail = STRLEN(namebuf);
2466
2467 STRCPY(namebuf + tail, "status");
2468 dict_add_nr_str(dict, namebuf, 0,
2469 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2470
2471 STRCPY(namebuf + tail, "mode");
2472 switch (chanpart->ch_mode)
2473 {
2474 case MODE_NL: s = "NL"; break;
2475 case MODE_RAW: s = "RAW"; break;
2476 case MODE_JSON: s = "JSON"; break;
2477 case MODE_JS: s = "JS"; break;
2478 }
2479 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2480
2481 STRCPY(namebuf + tail, "io");
2482 if (part == PART_SOCK)
2483 s = "socket";
2484 else switch (chanpart->ch_io)
2485 {
2486 case JIO_NULL: s = "null"; break;
2487 case JIO_PIPE: s = "pipe"; break;
2488 case JIO_FILE: s = "file"; break;
2489 case JIO_BUFFER: s = "buffer"; break;
2490 case JIO_OUT: s = "out"; break;
2491 }
2492 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2493
2494 STRCPY(namebuf + tail, "timeout");
2495 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2496}
2497
2498 void
2499channel_info(channel_T *channel, dict_T *dict)
2500{
2501 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2502 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2503
2504 if (channel->ch_hostname != NULL)
2505 {
2506 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2507 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2508 channel_part_info(channel, dict, "sock", PART_SOCK);
2509 }
2510 else
2511 {
2512 channel_part_info(channel, dict, "out", PART_OUT);
2513 channel_part_info(channel, dict, "err", PART_ERR);
2514 channel_part_info(channel, dict, "in", PART_IN);
2515 }
2516}
2517
Bram Moolenaar77073442016-02-13 23:23:53 +01002518/*
2519 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002520 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002521 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002522 */
2523 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002524channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002525{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002526 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002527
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002528#ifdef FEAT_GUI
2529 channel_gui_unregister(channel);
2530#endif
2531
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002532 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002533 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002534 sock_close(channel->CH_SOCK_FD);
2535 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002536 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002537 may_close_part(&channel->CH_IN_FD);
2538 may_close_part(&channel->CH_OUT_FD);
2539 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002540
Bram Moolenaar8b374212016-02-24 20:43:06 +01002541 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002542 {
2543 typval_T argv[1];
2544 typval_T rettv;
2545 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002546 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002547
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002548 /* Invoke callbacks before the close callback, since it's weird to
2549 * first invoke the close callback. Increment the refcount to avoid
2550 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002551 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002552 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002553 for (part = PART_SOCK; part <= PART_ERR; ++part)
2554 while (may_invoke_callback(channel, part))
2555 ;
2556
2557 /* Invoke the close callback, if still set. */
2558 if (channel->ch_close_cb != NULL)
2559 {
2560 ch_logs(channel, "Invoking close callback %s",
2561 (char *)channel->ch_close_cb);
2562 argv[0].v_type = VAR_CHANNEL;
2563 argv[0].vval.v_channel = channel;
2564 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002565 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2566 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002567 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002568 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002569 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002570
2571 /* the callback is only called once */
2572 vim_free(channel->ch_close_cb);
2573 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002574 partial_unref(channel->ch_close_partial);
2575 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002576
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002577 --channel->ch_refcount;
2578
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002579 if (channel_need_redraw)
2580 {
2581 channel_need_redraw = FALSE;
2582 redraw_after_callback();
2583 }
2584
Bram Moolenaar437905c2016-04-26 19:01:05 +02002585 /* any remaining messages are useless now */
2586 for (part = PART_SOCK; part <= PART_ERR; ++part)
2587 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002588 }
2589
2590 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002591}
2592
Bram Moolenaard04a0202016-01-26 23:30:18 +01002593/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002594 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002595 * Returns NULL if there is nothing.
2596 */
2597 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002598channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002599{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002600 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002601
Bram Moolenaar77073442016-02-13 23:23:53 +01002602 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002603 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002604 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002605}
2606
2607/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002608 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002609 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002610 static void
2611channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002612{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002613 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2614 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002615
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002616 while (channel_peek(channel, part) != NULL)
2617 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002618
2619 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002620 {
2621 cbq_T *node = cb_head->cq_next;
2622
2623 remove_cb_node(cb_head, node);
2624 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002625 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002626 vim_free(node);
2627 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002628
2629 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002630 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002631 free_tv(json_head->jq_next->jq_value);
2632 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002633 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002634
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002635 vim_free(channel->ch_part[part].ch_callback);
2636 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002637 partial_unref(channel->ch_part[part].ch_partial);
2638 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002639}
2640
2641/*
2642 * Clear all the read buffers on "channel".
2643 */
2644 void
2645channel_clear(channel_T *channel)
2646{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002647 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002648 vim_free(channel->ch_hostname);
2649 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002650 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002651 channel_clear_one(channel, PART_OUT);
2652 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002653 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002654 vim_free(channel->ch_callback);
2655 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002656 partial_unref(channel->ch_partial);
2657 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002658 vim_free(channel->ch_close_cb);
2659 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002660 partial_unref(channel->ch_close_partial);
2661 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002662}
2663
Bram Moolenaar77073442016-02-13 23:23:53 +01002664#if defined(EXITFREE) || defined(PROTO)
2665 void
2666channel_free_all(void)
2667{
2668 channel_T *channel;
2669
Bram Moolenaard6051b52016-02-28 15:49:03 +01002670 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002671 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2672 channel_clear(channel);
2673}
2674#endif
2675
2676
Bram Moolenaar715d2852016-04-30 17:06:31 +02002677/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002678#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002679
2680/* Buffer size for reading incoming messages. */
2681#define MAXMSGSIZE 4096
2682
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002683#if defined(HAVE_SELECT)
2684/*
2685 * Add write fds where we are waiting for writing to be possible.
2686 */
2687 static int
2688channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2689{
2690 int maxfd = maxfd_arg;
2691 channel_T *ch;
2692
2693 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2694 {
2695 chanpart_T *in_part = &ch->ch_part[PART_IN];
2696
2697 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2698 {
2699 FD_SET((int)in_part->ch_fd, wfds);
2700 if ((int)in_part->ch_fd >= maxfd)
2701 maxfd = (int)in_part->ch_fd + 1;
2702 }
2703 }
2704 return maxfd;
2705}
2706#else
2707/*
2708 * Add write fds where we are waiting for writing to be possible.
2709 */
2710 static int
2711channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2712{
2713 int nfd = nfd_in;
2714 channel_T *ch;
2715
2716 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2717 {
2718 chanpart_T *in_part = &ch->ch_part[PART_IN];
2719
2720 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2721 {
2722 in_part->ch_poll_idx = nfd;
2723 fds[nfd].fd = in_part->ch_fd;
2724 fds[nfd].events = POLLOUT;
2725 ++nfd;
2726 }
2727 else
2728 in_part->ch_poll_idx = -1;
2729 }
2730 return nfd;
2731}
2732#endif
2733
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002734typedef enum {
2735 CW_READY,
2736 CW_NOT_READY,
2737 CW_ERROR
2738} channel_wait_result;
2739
Bram Moolenaard04a0202016-01-26 23:30:18 +01002740/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002741 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002742 * Return CW_READY when there is something to read.
2743 * Return CW_NOT_READY when there is nothing to read.
2744 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002745 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002746 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002747channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002748{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002749 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002750 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002751
Bram Moolenaard8070362016-02-15 21:56:54 +01002752# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002753 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002754 {
2755 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002756 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002757 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002758 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002759
2760 /* reading from a pipe, not a socket */
2761 while (TRUE)
2762 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002763 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2764
2765 if (r && nread > 0)
2766 return CW_READY;
2767 if (r == 0)
2768 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002769
2770 /* perhaps write some buffer lines */
2771 channel_write_any_lines();
2772
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002773 sleep_time = deadline - GetTickCount();
2774 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002775 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002776 /* Wait for a little while. Very short at first, up to 10 msec
2777 * after looping a few times. */
2778 if (sleep_time > delay)
2779 sleep_time = delay;
2780 Sleep(sleep_time);
2781 delay = delay * 2;
2782 if (delay > 10)
2783 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002784 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002785 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002786 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002787#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002788 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002789#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002790 struct timeval tval;
2791 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002792 fd_set wfds;
2793 int ret;
2794 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002795
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002796 tval.tv_sec = timeout / 1000;
2797 tval.tv_usec = (timeout % 1000) * 1000;
2798 for (;;)
2799 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002800 FD_ZERO(&rfds);
2801 FD_SET((int)fd, &rfds);
2802
2803 /* Write lines to a pipe when a pipe can be written to. Need to
2804 * set this every time, some buffers may be done. */
2805 maxfd = (int)fd + 1;
2806 FD_ZERO(&wfds);
2807 maxfd = channel_fill_wfds(maxfd, &wfds);
2808
2809 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002810# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002811 SOCK_ERRNO;
2812 if (ret == -1 && errno == EINTR)
2813 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002814# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002815 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002816 {
2817 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002818 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002819 channel_write_any_lines();
2820 continue;
2821 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002822 break;
2823 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002824#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002825 for (;;)
2826 {
2827 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2828 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002829
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002830 fds[0].fd = fd;
2831 fds[0].events = POLLIN;
2832 nfd = channel_fill_poll_write(nfd, fds);
2833 if (poll(fds, nfd, timeout) > 0)
2834 {
2835 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002836 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002837 channel_write_any_lines();
2838 continue;
2839 }
2840 break;
2841 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002842#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002843 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002844 return CW_NOT_READY;
2845}
2846
2847 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002848channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002849{
2850 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002851 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2852 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002853
2854 /* Queue a "DETACH" netbeans message in the command queue in order to
2855 * terminate the netbeans session later. Do not end the session here
2856 * directly as we may be running in the context of a call to
2857 * netbeans_parse_messages():
2858 * netbeans_parse_messages
2859 * -> autocmd triggered while processing the netbeans cmd
2860 * -> ui_breakcheck
2861 * -> gui event loop or select loop
2862 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002863 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002864 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002865 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002866 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002867 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2868
2869 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002870 * died. Don't close the channel right away, it may be the wrong moment
2871 * to invoke callbacks. */
2872 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02002873
2874#ifdef FEAT_GUI
2875 /* Stop listening to GUI events right away. */
2876 channel_gui_unregister(channel);
2877#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002878}
2879
2880 static void
2881channel_close_now(channel_T *channel)
2882{
2883 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002884 channel_close(channel, TRUE);
2885 if (channel->ch_nb_close_cb != NULL)
2886 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002887}
2888
2889/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002890 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002891 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02002892 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002893 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002894 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002895channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002896{
2897 static char_u *buf = NULL;
2898 int len = 0;
2899 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002900 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002901 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002902
Bram Moolenaar5850a762016-05-27 19:59:48 +02002903 /* If we detected a read error don't try reading again. */
2904 if (channel->ch_to_be_closed)
2905 return;
2906
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002907 fd = channel->ch_part[part].ch_fd;
2908 if (fd == INVALID_FD)
2909 {
2910 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002911 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002912 }
2913 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002914
2915 /* Allocate a buffer to read into. */
2916 if (buf == NULL)
2917 {
2918 buf = alloc(MAXMSGSIZE);
2919 if (buf == NULL)
2920 return; /* out of memory! */
2921 }
2922
2923 /* Keep on reading for as long as there is something to read.
2924 * Use select() or poll() to avoid blocking on a message that is exactly
2925 * MAXMSGSIZE long. */
2926 for (;;)
2927 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002928 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002929 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002930 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002931 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002932 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002933 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002934 if (len <= 0)
2935 break; /* error or nothing more to read */
2936
2937 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002938 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002939 readlen += len;
2940 if (len < MAXMSGSIZE)
2941 break; /* did read everything that's available */
2942 }
2943
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002944 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002945 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002946 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002947
2948#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002949 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002950 if (CH_HAS_GUI && gtk_main_level() > 0)
2951 gtk_main_quit();
2952#endif
2953}
2954
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002955/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002956 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002957 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002958 * Returns what was read in allocated memory.
2959 * Returns NULL in case of error or timeout.
2960 */
2961 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002962channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002963{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002964 char_u *buf;
2965 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002966 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002967 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002968 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002969
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002970 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002971 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002972
2973 while (TRUE)
2974 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002975 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002976 if (buf != NULL && (mode == MODE_RAW
2977 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2978 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002979 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002980 continue;
2981
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002982 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002983 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002984 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002985 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002986 {
2987 ch_log(channel, "Timed out");
2988 return NULL;
2989 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002990 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002991 }
2992
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002993 if (mode == MODE_RAW)
2994 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002995 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002996 }
2997 else
2998 {
2999 nl = vim_strchr(buf, NL);
3000 if (nl[1] == NUL)
3001 {
3002 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003003 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003004 *nl = NUL;
3005 }
3006 else
3007 {
3008 /* Copy the message into allocated memory and remove it from the
3009 * buffer. */
3010 msg = vim_strnsave(buf, (int)(nl - buf));
3011 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
3012 }
3013 }
3014 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003015 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003016 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003017}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003018
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003019/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003020 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003021 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003022 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003023 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003024 */
3025 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003026channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003027 channel_T *channel,
3028 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003029 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003030 int id,
3031 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003032{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003033 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003034 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003035 int timeout;
3036 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003037
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003038 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003039 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003040 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003041 for (;;)
3042 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003043 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003044
3045 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003046 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003047 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003048 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003049 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003050 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003051
Bram Moolenaard7ece102016-02-02 23:23:02 +01003052 if (!more)
3053 {
3054 /* Handle any other messages in the queue. If done some more
3055 * messages may have arrived. */
3056 if (channel_parse_messages())
3057 continue;
3058
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003059 /* Wait for up to the timeout. If there was an incomplete message
3060 * use the deadline for that. */
3061 timeout = timeout_arg;
3062 if (chanpart->ch_waiting)
3063 {
3064#ifdef WIN32
3065 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3066#else
3067 {
3068 struct timeval now_tv;
3069
3070 gettimeofday(&now_tv, NULL);
3071 timeout = (chanpart->ch_deadline.tv_sec
3072 - now_tv.tv_sec) * 1000
3073 + (chanpart->ch_deadline.tv_usec
3074 - now_tv.tv_usec) / 1000
3075 + 1;
3076 }
3077#endif
3078 if (timeout < 0)
3079 {
3080 /* Something went wrong, channel_parse_json() didn't
3081 * discard message. Cancel waiting. */
3082 chanpart->ch_waiting = FALSE;
3083 timeout = timeout_arg;
3084 }
3085 else if (timeout > timeout_arg)
3086 timeout = timeout_arg;
3087 }
3088 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003089 if (fd == INVALID_FD
3090 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003091 {
3092 if (timeout == timeout_arg)
3093 {
3094 if (fd != INVALID_FD)
3095 ch_log(channel, "Timed out");
3096 break;
3097 }
3098 }
3099 else
3100 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003101 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003102 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003103 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003104 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003105}
3106
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003107/*
3108 * Common for ch_read() and ch_readraw().
3109 */
3110 void
3111common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3112{
3113 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003114 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003115 jobopt_T opt;
3116 int mode;
3117 int timeout;
3118 int id = -1;
3119 typval_T *listtv = NULL;
3120
3121 /* return an empty string by default */
3122 rettv->v_type = VAR_STRING;
3123 rettv->vval.v_string = NULL;
3124
3125 clear_job_options(&opt);
3126 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3127 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003128 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003129
Bram Moolenaar437905c2016-04-26 19:01:05 +02003130 if (opt.jo_set & JO_PART)
3131 part = opt.jo_part;
3132 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003133 if (channel != NULL)
3134 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003135 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003136 part = channel_part_read(channel);
3137 mode = channel_get_mode(channel, part);
3138 timeout = channel_get_timeout(channel, part);
3139 if (opt.jo_set & JO_TIMEOUT)
3140 timeout = opt.jo_timeout;
3141
3142 if (raw || mode == MODE_RAW || mode == MODE_NL)
3143 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3144 else
3145 {
3146 if (opt.jo_set & JO_ID)
3147 id = opt.jo_id;
3148 channel_read_json_block(channel, part, timeout, id, &listtv);
3149 if (listtv != NULL)
3150 {
3151 *rettv = *listtv;
3152 vim_free(listtv);
3153 }
3154 else
3155 {
3156 rettv->v_type = VAR_SPECIAL;
3157 rettv->vval.v_number = VVAL_NONE;
3158 }
3159 }
3160 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003161
3162theend:
3163 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003164}
3165
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003166# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3167 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003168/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003169 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003170 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003171 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003172 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003173channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003174{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003175 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003176 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003177
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003178 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003179 for (channel = first_channel; channel != NULL;
3180 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003181 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003182 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003183 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003184 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003185 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003186 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003187 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003188 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003189 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003190}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003191# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003192
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003193# if defined(WIN32) || defined(PROTO)
3194/*
3195 * Check the channels for anything that is ready to be read.
3196 * The data is put in the read queue.
3197 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003198 void
3199channel_handle_events(void)
3200{
3201 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003202 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003203 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003204
3205 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3206 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003207 /* If we detected a read error don't try reading again. */
3208 if (channel->ch_to_be_closed)
3209 continue;
3210
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003211 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003212 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003213 {
3214 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003215 if (fd != INVALID_FD)
3216 {
3217 int r = channel_wait(channel, fd, 0);
3218
3219 if (r == CW_READY)
3220 channel_read(channel, part, "channel_handle_events");
3221 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003222 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003223 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003224 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003225 }
3226}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003227# endif
3228
Bram Moolenaard04a0202016-01-26 23:30:18 +01003229/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003230 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003231 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003232 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003233 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003234 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003235channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003236{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003237 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003238 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003239 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003240
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003241 fd = channel->ch_part[part].ch_fd;
3242 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003243 {
3244 if (!channel->ch_error && fun != NULL)
3245 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003246 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003247 EMSG2("E630: %s(): write while not connected", fun);
3248 }
3249 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003250 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003251 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003252
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003253 if (log_fd != NULL)
3254 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003255 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003256 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003257 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003258 fprintf(log_fd, "'\n");
3259 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003260 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003261 }
3262
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003263 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003264 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003265 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003266 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003267 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003268 {
3269 if (!channel->ch_error && fun != NULL)
3270 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003271 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003272 EMSG2("E631: %s(): write failed", fun);
3273 }
3274 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003275 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003276 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003277
3278 channel->ch_error = FALSE;
3279 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003280}
3281
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003282/*
3283 * Common for "ch_sendexpr()" and "ch_sendraw()".
3284 * Returns the channel if the caller should read the response.
3285 * Sets "part_read" to the the read fd.
3286 * Otherwise returns NULL.
3287 */
3288 channel_T *
3289send_common(
3290 typval_T *argvars,
3291 char_u *text,
3292 int id,
3293 int eval,
3294 jobopt_T *opt,
3295 char *fun,
3296 int *part_read)
3297{
3298 channel_T *channel;
3299 int part_send;
3300
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003301 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003302 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003303 if (channel == NULL)
3304 return NULL;
3305 part_send = channel_part_send(channel);
3306 *part_read = channel_part_read(channel);
3307
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003308 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3309 return NULL;
3310
3311 /* Set the callback. An empty callback means no callback and not reading
3312 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3313 * allowed. */
3314 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3315 {
3316 if (eval)
3317 {
3318 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3319 return NULL;
3320 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003321 channel_set_req_callback(channel, part_send,
3322 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003323 }
3324
3325 if (channel_send(channel, part_send, text, fun) == OK
3326 && opt->jo_callback == NULL)
3327 return channel;
3328 return NULL;
3329}
3330
3331/*
3332 * common for "ch_evalexpr()" and "ch_sendexpr()"
3333 */
3334 void
3335ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3336{
3337 char_u *text;
3338 typval_T *listtv;
3339 channel_T *channel;
3340 int id;
3341 ch_mode_T ch_mode;
3342 int part_send;
3343 int part_read;
3344 jobopt_T opt;
3345 int timeout;
3346
3347 /* return an empty string by default */
3348 rettv->v_type = VAR_STRING;
3349 rettv->vval.v_string = NULL;
3350
Bram Moolenaar437905c2016-04-26 19:01:05 +02003351 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003352 if (channel == NULL)
3353 return;
3354 part_send = channel_part_send(channel);
3355
3356 ch_mode = channel_get_mode(channel, part_send);
3357 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3358 {
3359 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3360 return;
3361 }
3362
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003363 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003364 text = json_encode_nr_expr(id, &argvars[1],
3365 ch_mode == MODE_JS ? JSON_JS : 0);
3366 if (text == NULL)
3367 return;
3368
3369 channel = send_common(argvars, text, id, eval, &opt,
3370 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3371 vim_free(text);
3372 if (channel != NULL && eval)
3373 {
3374 if (opt.jo_set & JO_TIMEOUT)
3375 timeout = opt.jo_timeout;
3376 else
3377 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003378 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3379 == OK)
3380 {
3381 list_T *list = listtv->vval.v_list;
3382
3383 /* Move the item from the list and then change the type to
3384 * avoid the value being freed. */
3385 *rettv = list->lv_last->li_tv;
3386 list->lv_last->li_tv.v_type = VAR_NUMBER;
3387 free_tv(listtv);
3388 }
3389 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003390 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003391}
3392
3393/*
3394 * common for "ch_evalraw()" and "ch_sendraw()"
3395 */
3396 void
3397ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3398{
3399 char_u buf[NUMBUFLEN];
3400 char_u *text;
3401 channel_T *channel;
3402 int part_read;
3403 jobopt_T opt;
3404 int timeout;
3405
3406 /* return an empty string by default */
3407 rettv->v_type = VAR_STRING;
3408 rettv->vval.v_string = NULL;
3409
3410 text = get_tv_string_buf(&argvars[1], buf);
3411 channel = send_common(argvars, text, 0, eval, &opt,
3412 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3413 if (channel != NULL && eval)
3414 {
3415 if (opt.jo_set & JO_TIMEOUT)
3416 timeout = opt.jo_timeout;
3417 else
3418 timeout = channel_get_timeout(channel, part_read);
3419 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3420 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003421 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003422}
3423
Bram Moolenaard04a0202016-01-26 23:30:18 +01003424# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003425/*
3426 * Add open channels to the poll struct.
3427 * Return the adjusted struct index.
3428 * The type of "fds" is hidden to avoid problems with the function proto.
3429 */
3430 int
3431channel_poll_setup(int nfd_in, void *fds_in)
3432{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003433 int nfd = nfd_in;
3434 channel_T *channel;
3435 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003436 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003437
Bram Moolenaar77073442016-02-13 23:23:53 +01003438 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003439 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003440 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003441 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003442 chanpart_T *ch_part = &channel->ch_part[part];
3443
3444 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003445 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003446 ch_part->ch_poll_idx = nfd;
3447 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003448 fds[nfd].events = POLLIN;
3449 nfd++;
3450 }
3451 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003452 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003453 }
3454 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003455
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003456 nfd = channel_fill_poll_write(nfd, fds);
3457
Bram Moolenaare0874f82016-01-24 20:36:41 +01003458 return nfd;
3459}
3460
3461/*
3462 * The type of "fds" is hidden to avoid problems with the function proto.
3463 */
3464 int
3465channel_poll_check(int ret_in, void *fds_in)
3466{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003467 int ret = ret_in;
3468 channel_T *channel;
3469 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003470 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003471 int idx;
3472 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003473
Bram Moolenaar77073442016-02-13 23:23:53 +01003474 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003475 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003476 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003477 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003478 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003479
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003480 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003481 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003482 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003483 --ret;
3484 }
3485 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003486
3487 in_part = &channel->ch_part[PART_IN];
3488 idx = in_part->ch_poll_idx;
3489 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3490 {
3491 if (in_part->ch_buf_append)
3492 {
3493 if (in_part->ch_buffer != NULL)
3494 channel_write_new_lines(in_part->ch_buffer);
3495 }
3496 else
3497 channel_write_in(channel);
3498 --ret;
3499 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003500 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003501
3502 return ret;
3503}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003504# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003505
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003506# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003507/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003508 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003509 */
3510 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003511channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003512{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003513 int maxfd = maxfd_in;
3514 channel_T *channel;
3515 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003516 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003517 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003518
Bram Moolenaar77073442016-02-13 23:23:53 +01003519 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003520 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003521 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003522 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003523 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003524
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003525 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003526 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003527 FD_SET((int)fd, rfds);
3528 if (maxfd < (int)fd)
3529 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003530 }
3531 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003532 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003533
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003534 maxfd = channel_fill_wfds(maxfd, wfds);
3535
Bram Moolenaare0874f82016-01-24 20:36:41 +01003536 return maxfd;
3537}
3538
3539/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003540 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003541 */
3542 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003543channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003544{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003545 int ret = ret_in;
3546 channel_T *channel;
3547 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003548 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003549 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003550 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003551
Bram Moolenaar77073442016-02-13 23:23:53 +01003552 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003553 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003554 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003555 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003556 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003557
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003558 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003559 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003560 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003561 --ret;
3562 }
3563 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003564
3565 in_part = &channel->ch_part[PART_IN];
3566 if (ret > 0 && in_part->ch_fd != INVALID_FD
3567 && FD_ISSET(in_part->ch_fd, wfds))
3568 {
3569 if (in_part->ch_buf_append)
3570 {
3571 if (in_part->ch_buffer != NULL)
3572 channel_write_new_lines(in_part->ch_buffer);
3573 }
3574 else
3575 channel_write_in(channel);
3576 --ret;
3577 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003578 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003579
3580 return ret;
3581}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003582# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003583
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003584/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003585 * Execute queued up commands.
3586 * Invoked from the main loop when it's safe to execute received commands.
3587 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003588 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003589 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003590channel_parse_messages(void)
3591{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003592 channel_T *channel = first_channel;
3593 int ret = FALSE;
3594 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003595 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003596
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003597 ++safe_to_invoke_callback;
3598
Bram Moolenaard0b65022016-03-06 21:50:33 +01003599 /* Only do this message when another message was given, otherwise we get
3600 * lots of them. */
3601 if (did_log_msg)
3602 {
3603 ch_log(NULL, "looking for messages on channels");
3604 did_log_msg = FALSE;
3605 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003606 while (channel != NULL)
3607 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003608 if (channel->ch_to_be_closed)
3609 {
3610 channel->ch_to_be_closed = FALSE;
3611 channel_close_now(channel);
3612 /* channel may have been freed, start over */
3613 channel = first_channel;
3614 continue;
3615 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003616 if (channel->ch_to_be_freed)
3617 {
3618 channel_free(channel);
3619 /* channel has been freed, start over */
3620 channel = first_channel;
3621 continue;
3622 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003623 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003624 {
3625 /* channel is no longer useful, free it */
3626 channel_free(channel);
3627 channel = first_channel;
3628 part = PART_SOCK;
3629 continue;
3630 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003631 if (channel->ch_part[part].ch_fd != INVALID_FD
3632 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003633 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003634 /* Increase the refcount, in case the handler causes the channel
3635 * to be unreferenced or closed. */
3636 ++channel->ch_refcount;
3637 r = may_invoke_callback(channel, part);
3638 if (r == OK)
3639 ret = TRUE;
3640 if (channel_unref(channel) || r == OK)
3641 {
3642 /* channel was freed or something was done, start over */
3643 channel = first_channel;
3644 part = PART_SOCK;
3645 continue;
3646 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003647 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003648 if (part < PART_ERR)
3649 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003650 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003651 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003652 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003653 part = PART_SOCK;
3654 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003655 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003656
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003657 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003658 {
3659 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003660 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003661 }
3662
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003663 --safe_to_invoke_callback;
3664
Bram Moolenaard7ece102016-02-02 23:23:02 +01003665 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003666}
3667
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003668/*
3669 * Mark references to lists used in channels.
3670 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003671 int
3672set_ref_in_channel(int copyID)
3673{
Bram Moolenaar77073442016-02-13 23:23:53 +01003674 int abort = FALSE;
3675 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003676 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003677
Bram Moolenaar77073442016-02-13 23:23:53 +01003678 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003679 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003680 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003681 tv.v_type = VAR_CHANNEL;
3682 tv.vval.v_channel = channel;
3683 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003684 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003685 return abort;
3686}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003687
3688/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003689 * Return the "part" to write to for "channel".
3690 */
3691 int
3692channel_part_send(channel_T *channel)
3693{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003694 if (channel->CH_SOCK_FD == INVALID_FD)
3695 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003696 return PART_SOCK;
3697}
3698
3699/*
3700 * Return the default "part" to read from for "channel".
3701 */
3702 int
3703channel_part_read(channel_T *channel)
3704{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003705 if (channel->CH_SOCK_FD == INVALID_FD)
3706 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003707 return PART_SOCK;
3708}
3709
3710/*
3711 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003712 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003713 */
3714 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003715channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003716{
Bram Moolenaar77073442016-02-13 23:23:53 +01003717 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003718 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003719 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003720}
3721
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003722/*
3723 * Return the timeout of "channel"/"part"
3724 */
3725 int
3726channel_get_timeout(channel_T *channel, int part)
3727{
3728 return channel->ch_part[part].ch_timeout;
3729}
3730
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003731 static int
3732handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3733{
3734 char_u *val = get_tv_string(item);
3735
3736 opt->jo_set |= jo;
3737 if (STRCMP(val, "nl") == 0)
3738 *modep = MODE_NL;
3739 else if (STRCMP(val, "raw") == 0)
3740 *modep = MODE_RAW;
3741 else if (STRCMP(val, "js") == 0)
3742 *modep = MODE_JS;
3743 else if (STRCMP(val, "json") == 0)
3744 *modep = MODE_JSON;
3745 else
3746 {
3747 EMSG2(_(e_invarg2), val);
3748 return FAIL;
3749 }
3750 return OK;
3751}
3752
3753 static int
3754handle_io(typval_T *item, int part, jobopt_T *opt)
3755{
3756 char_u *val = get_tv_string(item);
3757
3758 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3759 if (STRCMP(val, "null") == 0)
3760 opt->jo_io[part] = JIO_NULL;
3761 else if (STRCMP(val, "pipe") == 0)
3762 opt->jo_io[part] = JIO_PIPE;
3763 else if (STRCMP(val, "file") == 0)
3764 opt->jo_io[part] = JIO_FILE;
3765 else if (STRCMP(val, "buffer") == 0)
3766 opt->jo_io[part] = JIO_BUFFER;
3767 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3768 opt->jo_io[part] = JIO_OUT;
3769 else
3770 {
3771 EMSG2(_(e_invarg2), val);
3772 return FAIL;
3773 }
3774 return OK;
3775}
3776
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003777/*
3778 * Clear a jobopt_T before using it.
3779 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003780 void
3781clear_job_options(jobopt_T *opt)
3782{
3783 vim_memset(opt, 0, sizeof(jobopt_T));
3784}
3785
3786/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003787 * Free any members of a jobopt_T.
3788 */
3789 void
3790free_job_options(jobopt_T *opt)
3791{
3792 if (opt->jo_partial != NULL)
3793 partial_unref(opt->jo_partial);
3794 if (opt->jo_out_partial != NULL)
3795 partial_unref(opt->jo_out_partial);
3796 if (opt->jo_err_partial != NULL)
3797 partial_unref(opt->jo_err_partial);
3798 if (opt->jo_close_partial != NULL)
3799 partial_unref(opt->jo_close_partial);
3800}
3801
3802/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003803 * Get the PART_ number from the first character of an option name.
3804 */
3805 static int
3806part_from_char(int c)
3807{
3808 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3809}
3810
3811/*
3812 * Get the option entries from the dict in "tv", parse them and put the result
3813 * in "opt".
3814 * Only accept options in "supported".
3815 * If an option value is invalid return FAIL.
3816 */
3817 int
3818get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3819{
3820 typval_T *item;
3821 char_u *val;
3822 dict_T *dict;
3823 int todo;
3824 hashitem_T *hi;
3825 int part;
3826
3827 opt->jo_set = 0;
3828 if (tv->v_type == VAR_UNKNOWN)
3829 return OK;
3830 if (tv->v_type != VAR_DICT)
3831 {
3832 EMSG(_(e_invarg));
3833 return FAIL;
3834 }
3835 dict = tv->vval.v_dict;
3836 if (dict == NULL)
3837 return OK;
3838
3839 todo = (int)dict->dv_hashtab.ht_used;
3840 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3841 if (!HASHITEM_EMPTY(hi))
3842 {
3843 item = &dict_lookup(hi)->di_tv;
3844
3845 if (STRCMP(hi->hi_key, "mode") == 0)
3846 {
3847 if (!(supported & JO_MODE))
3848 break;
3849 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3850 return FAIL;
3851 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003852 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003853 {
3854 if (!(supported & JO_IN_MODE))
3855 break;
3856 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3857 == FAIL)
3858 return FAIL;
3859 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003860 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003861 {
3862 if (!(supported & JO_OUT_MODE))
3863 break;
3864 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3865 == FAIL)
3866 return FAIL;
3867 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003868 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003869 {
3870 if (!(supported & JO_ERR_MODE))
3871 break;
3872 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3873 == FAIL)
3874 return FAIL;
3875 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003876 else if (STRCMP(hi->hi_key, "in_io") == 0
3877 || STRCMP(hi->hi_key, "out_io") == 0
3878 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003879 {
3880 if (!(supported & JO_OUT_IO))
3881 break;
3882 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3883 return FAIL;
3884 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003885 else if (STRCMP(hi->hi_key, "in_name") == 0
3886 || STRCMP(hi->hi_key, "out_name") == 0
3887 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003888 {
3889 part = part_from_char(*hi->hi_key);
3890
3891 if (!(supported & JO_OUT_IO))
3892 break;
3893 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3894 opt->jo_io_name[part] =
3895 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3896 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003897 else if (STRCMP(hi->hi_key, "in_buf") == 0
3898 || STRCMP(hi->hi_key, "out_buf") == 0
3899 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003900 {
3901 part = part_from_char(*hi->hi_key);
3902
3903 if (!(supported & JO_OUT_IO))
3904 break;
3905 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3906 opt->jo_io_buf[part] = get_tv_number(item);
3907 if (opt->jo_io_buf[part] <= 0)
3908 {
3909 EMSG2(_(e_invarg2), get_tv_string(item));
3910 return FAIL;
3911 }
3912 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3913 {
3914 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3915 return FAIL;
3916 }
3917 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003918 else if (STRCMP(hi->hi_key, "in_top") == 0
3919 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003920 {
3921 linenr_T *lp;
3922
3923 if (!(supported & JO_OUT_IO))
3924 break;
3925 if (hi->hi_key[3] == 't')
3926 {
3927 lp = &opt->jo_in_top;
3928 opt->jo_set |= JO_IN_TOP;
3929 }
3930 else
3931 {
3932 lp = &opt->jo_in_bot;
3933 opt->jo_set |= JO_IN_BOT;
3934 }
3935 *lp = get_tv_number(item);
3936 if (*lp < 0)
3937 {
3938 EMSG2(_(e_invarg2), get_tv_string(item));
3939 return FAIL;
3940 }
3941 }
3942 else if (STRCMP(hi->hi_key, "channel") == 0)
3943 {
3944 if (!(supported & JO_OUT_IO))
3945 break;
3946 opt->jo_set |= JO_CHANNEL;
3947 if (item->v_type != VAR_CHANNEL)
3948 {
3949 EMSG2(_(e_invarg2), "channel");
3950 return FAIL;
3951 }
3952 opt->jo_channel = item->vval.v_channel;
3953 }
3954 else if (STRCMP(hi->hi_key, "callback") == 0)
3955 {
3956 if (!(supported & JO_CALLBACK))
3957 break;
3958 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003959 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003960 if (opt->jo_callback == NULL)
3961 {
3962 EMSG2(_(e_invarg2), "callback");
3963 return FAIL;
3964 }
3965 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003966 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003967 {
3968 if (!(supported & JO_OUT_CALLBACK))
3969 break;
3970 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003971 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003972 if (opt->jo_out_cb == NULL)
3973 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003974 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003975 return FAIL;
3976 }
3977 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003978 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003979 {
3980 if (!(supported & JO_ERR_CALLBACK))
3981 break;
3982 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003983 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003984 if (opt->jo_err_cb == NULL)
3985 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003986 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003987 return FAIL;
3988 }
3989 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003990 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003991 {
3992 if (!(supported & JO_CLOSE_CALLBACK))
3993 break;
3994 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003995 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003996 if (opt->jo_close_cb == NULL)
3997 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003998 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003999 return FAIL;
4000 }
4001 }
4002 else if (STRCMP(hi->hi_key, "waittime") == 0)
4003 {
4004 if (!(supported & JO_WAITTIME))
4005 break;
4006 opt->jo_set |= JO_WAITTIME;
4007 opt->jo_waittime = get_tv_number(item);
4008 }
4009 else if (STRCMP(hi->hi_key, "timeout") == 0)
4010 {
4011 if (!(supported & JO_TIMEOUT))
4012 break;
4013 opt->jo_set |= JO_TIMEOUT;
4014 opt->jo_timeout = get_tv_number(item);
4015 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004016 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004017 {
4018 if (!(supported & JO_OUT_TIMEOUT))
4019 break;
4020 opt->jo_set |= JO_OUT_TIMEOUT;
4021 opt->jo_out_timeout = get_tv_number(item);
4022 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004023 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004024 {
4025 if (!(supported & JO_ERR_TIMEOUT))
4026 break;
4027 opt->jo_set |= JO_ERR_TIMEOUT;
4028 opt->jo_err_timeout = get_tv_number(item);
4029 }
4030 else if (STRCMP(hi->hi_key, "part") == 0)
4031 {
4032 if (!(supported & JO_PART))
4033 break;
4034 opt->jo_set |= JO_PART;
4035 val = get_tv_string(item);
4036 if (STRCMP(val, "err") == 0)
4037 opt->jo_part = PART_ERR;
4038 else
4039 {
4040 EMSG2(_(e_invarg2), val);
4041 return FAIL;
4042 }
4043 }
4044 else if (STRCMP(hi->hi_key, "id") == 0)
4045 {
4046 if (!(supported & JO_ID))
4047 break;
4048 opt->jo_set |= JO_ID;
4049 opt->jo_id = get_tv_number(item);
4050 }
4051 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4052 {
4053 if (!(supported & JO_STOPONEXIT))
4054 break;
4055 opt->jo_set |= JO_STOPONEXIT;
4056 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4057 opt->jo_soe_buf);
4058 if (opt->jo_stoponexit == NULL)
4059 {
4060 EMSG2(_(e_invarg2), "stoponexit");
4061 return FAIL;
4062 }
4063 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004064 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004065 {
4066 if (!(supported & JO_EXIT_CB))
4067 break;
4068 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004069 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
4070 {
4071 opt->jo_exit_partial = item->vval.v_partial;
4072 opt->jo_exit_cb = item->vval.v_partial->pt_name;
4073 }
4074 else
4075 opt->jo_exit_cb = get_tv_string_buf_chk(
4076 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004077 if (opt->jo_exit_cb == NULL)
4078 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004079 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004080 return FAIL;
4081 }
4082 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004083 else if (STRCMP(hi->hi_key, "block_write") == 0)
4084 {
4085 if (!(supported & JO_BLOCK_WRITE))
4086 break;
4087 opt->jo_set |= JO_BLOCK_WRITE;
4088 opt->jo_block_write = get_tv_number(item);
4089 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004090 else
4091 break;
4092 --todo;
4093 }
4094 if (todo > 0)
4095 {
4096 EMSG2(_(e_invarg2), hi->hi_key);
4097 return FAIL;
4098 }
4099
4100 return OK;
4101}
4102
4103/*
4104 * Get the channel from the argument.
4105 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004106 * When "check_open" is TRUE check that the channel can be used.
4107 * When "reading" is TRUE "check_open" considers typeahead useful.
4108 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004109 */
4110 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004111get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004112{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004113 channel_T *channel = NULL;
4114 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004115
4116 if (tv->v_type == VAR_JOB)
4117 {
4118 if (tv->vval.v_job != NULL)
4119 channel = tv->vval.v_job->jv_channel;
4120 }
4121 else if (tv->v_type == VAR_CHANNEL)
4122 {
4123 channel = tv->vval.v_channel;
4124 }
4125 else
4126 {
4127 EMSG2(_(e_invarg2), get_tv_string(tv));
4128 return NULL;
4129 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004130 if (channel != NULL && reading)
4131 has_readahead = channel_has_readahead(channel,
4132 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004133
Bram Moolenaar437905c2016-04-26 19:01:05 +02004134 if (check_open && (channel == NULL || (!channel_is_open(channel)
4135 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004136 {
4137 EMSG(_("E906: not an open channel"));
4138 return NULL;
4139 }
4140 return channel;
4141}
4142
4143static job_T *first_job = NULL;
4144
4145 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004146job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004147{
4148 ch_log(job->jv_channel, "Freeing job");
4149 if (job->jv_channel != NULL)
4150 {
4151 /* The link from the channel to the job doesn't count as a reference,
4152 * thus don't decrement the refcount of the job. The reference from
4153 * the job to the channel does count the refrence, decrement it and
4154 * NULL the reference. We don't set ch_job_killed, unreferencing the
4155 * job doesn't mean it stops running. */
4156 job->jv_channel->ch_job = NULL;
4157 channel_unref(job->jv_channel);
4158 }
4159 mch_clear_job(job);
4160
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004161 vim_free(job->jv_stoponexit);
4162 vim_free(job->jv_exit_cb);
4163 partial_unref(job->jv_exit_partial);
4164}
4165
4166 static void
4167job_free_job(job_T *job)
4168{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004169 if (job->jv_next != NULL)
4170 job->jv_next->jv_prev = job->jv_prev;
4171 if (job->jv_prev == NULL)
4172 first_job = job->jv_next;
4173 else
4174 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004175 vim_free(job);
4176}
4177
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004178 static void
4179job_free(job_T *job)
4180{
4181 if (!in_free_unref_items)
4182 {
4183 job_free_contents(job);
4184 job_free_job(job);
4185 }
4186}
4187
Bram Moolenaar655da312016-05-28 22:22:34 +02004188#if defined(EXITFREE) || defined(PROTO)
4189 void
4190job_free_all(void)
4191{
4192 while (first_job != NULL)
4193 job_free(first_job);
4194}
4195#endif
4196
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004197/*
4198 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004199 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4200 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004201 */
4202 static int
4203job_still_useful(job_T *job)
4204{
4205 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004206 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4207 || (job->jv_channel != NULL
4208 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004209}
4210
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004211/*
4212 * Mark references in jobs that are still useful.
4213 */
4214 int
4215set_ref_in_job(int copyID)
4216{
4217 int abort = FALSE;
4218 job_T *job;
4219 typval_T tv;
4220
4221 for (job = first_job; job != NULL; job = job->jv_next)
4222 if (job_still_useful(job))
4223 {
4224 tv.v_type = VAR_JOB;
4225 tv.vval.v_job = job;
4226 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4227 }
4228 return abort;
4229}
4230
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004231 void
4232job_unref(job_T *job)
4233{
4234 if (job != NULL && --job->jv_refcount <= 0)
4235 {
4236 /* Do not free the job when it has not ended yet and there is a
4237 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004238 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004239 {
4240 job_free(job);
4241 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004242 else if (job->jv_channel != NULL
4243 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004244 {
4245 /* Do remove the link to the channel, otherwise it hangs
4246 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004247 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004248 job->jv_channel->ch_job = NULL;
4249 channel_unref(job->jv_channel);
4250 job->jv_channel = NULL;
4251 }
4252 }
4253}
4254
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004255 int
4256free_unused_jobs_contents(int copyID, int mask)
4257{
4258 int did_free = FALSE;
4259 job_T *job;
4260
4261 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004262 if ((job->jv_copyID & mask) != (copyID & mask)
4263 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004264 {
4265 /* Free the channel and ordinary items it contains, but don't
4266 * recurse into Lists, Dictionaries etc. */
4267 job_free_contents(job);
4268 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004269 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004270 return did_free;
4271}
4272
4273 void
4274free_unused_jobs(int copyID, int mask)
4275{
4276 job_T *job;
4277 job_T *job_next;
4278
4279 for (job = first_job; job != NULL; job = job_next)
4280 {
4281 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004282 if ((job->jv_copyID & mask) != (copyID & mask)
4283 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004284 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004285 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004286 job_free_job(job);
4287 }
4288 }
4289}
4290
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004291/*
4292 * Allocate a job. Sets the refcount to one and sets options default.
4293 */
4294 static job_T *
4295job_alloc(void)
4296{
4297 job_T *job;
4298
4299 job = (job_T *)alloc_clear(sizeof(job_T));
4300 if (job != NULL)
4301 {
4302 job->jv_refcount = 1;
4303 job->jv_stoponexit = vim_strsave((char_u *)"term");
4304
4305 if (first_job != NULL)
4306 {
4307 first_job->jv_prev = job;
4308 job->jv_next = first_job;
4309 }
4310 first_job = job;
4311 }
4312 return job;
4313}
4314
4315 void
4316job_set_options(job_T *job, jobopt_T *opt)
4317{
4318 if (opt->jo_set & JO_STOPONEXIT)
4319 {
4320 vim_free(job->jv_stoponexit);
4321 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4322 job->jv_stoponexit = NULL;
4323 else
4324 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4325 }
4326 if (opt->jo_set & JO_EXIT_CB)
4327 {
4328 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004329 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004330 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004331 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004332 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004333 job->jv_exit_partial = NULL;
4334 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004335 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004336 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004337 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004338 job->jv_exit_partial = opt->jo_exit_partial;
4339 if (job->jv_exit_partial != NULL)
4340 ++job->jv_exit_partial->pt_refcount;
4341 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004342 }
4343}
4344
4345/*
4346 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4347 */
4348 void
4349job_stop_on_exit()
4350{
4351 job_T *job;
4352
4353 for (job = first_job; job != NULL; job = job->jv_next)
4354 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4355 mch_stop_job(job, job->jv_stoponexit);
4356}
4357
4358/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004359 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004360 */
4361 void
4362job_check_ended(void)
4363{
4364 static time_t last_check = 0;
4365 time_t now;
4366 job_T *job;
4367 job_T *next;
4368
4369 /* Only do this once in 10 seconds. */
4370 now = time(NULL);
4371 if (last_check + 10 < now)
4372 {
4373 last_check = now;
4374 for (job = first_job; job != NULL; job = next)
4375 {
4376 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004377 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004378 job_status(job); /* may free "job" */
4379 }
4380 }
4381}
4382
4383/*
4384 * "job_start()" function
4385 */
4386 job_T *
4387job_start(typval_T *argvars)
4388{
4389 job_T *job;
4390 char_u *cmd = NULL;
4391#if defined(UNIX)
4392# define USE_ARGV
4393 char **argv = NULL;
4394 int argc = 0;
4395#else
4396 garray_T ga;
4397#endif
4398 jobopt_T opt;
4399 int part;
4400
4401 job = job_alloc();
4402 if (job == NULL)
4403 return NULL;
4404
4405 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004406#ifndef USE_ARGV
4407 ga_init2(&ga, (int)sizeof(char*), 20);
4408#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004409
4410 /* Default mode is NL. */
4411 clear_job_options(&opt);
4412 opt.jo_mode = MODE_NL;
4413 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004414 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4415 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004416 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004417
4418 /* Check that when io is "file" that there is a file name. */
4419 for (part = PART_OUT; part <= PART_IN; ++part)
4420 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4421 && opt.jo_io[part] == JIO_FILE
4422 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4423 || *opt.jo_io_name[part] == NUL))
4424 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004425 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004426 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004427 }
4428
4429 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4430 {
4431 buf_T *buf = NULL;
4432
4433 /* check that we can find the buffer before starting the job */
4434 if (opt.jo_set & JO_IN_BUF)
4435 {
4436 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4437 if (buf == NULL)
4438 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4439 }
4440 else if (!(opt.jo_set & JO_IN_NAME))
4441 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004442 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004443 }
4444 else
4445 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4446 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004447 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004448 if (buf->b_ml.ml_mfp == NULL)
4449 {
4450 char_u numbuf[NUMBUFLEN];
4451 char_u *s;
4452
4453 if (opt.jo_set & JO_IN_BUF)
4454 {
4455 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4456 s = numbuf;
4457 }
4458 else
4459 s = opt.jo_io_name[PART_IN];
4460 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004461 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004462 }
4463 job->jv_in_buf = buf;
4464 }
4465
4466 job_set_options(job, &opt);
4467
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004468 if (argvars[0].v_type == VAR_STRING)
4469 {
4470 /* Command is a string. */
4471 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004472 if (cmd == NULL || *cmd == NUL)
4473 {
4474 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004475 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004476 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004477#ifdef USE_ARGV
4478 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004479 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004480 argv[argc] = NULL;
4481#endif
4482 }
4483 else if (argvars[0].v_type != VAR_LIST
4484 || argvars[0].vval.v_list == NULL
4485 || argvars[0].vval.v_list->lv_len < 1)
4486 {
4487 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004488 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004489 }
4490 else
4491 {
4492 list_T *l = argvars[0].vval.v_list;
4493 listitem_T *li;
4494 char_u *s;
4495
4496#ifdef USE_ARGV
4497 /* Pass argv[] to mch_call_shell(). */
4498 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4499 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004500 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004501#endif
4502 for (li = l->lv_first; li != NULL; li = li->li_next)
4503 {
4504 s = get_tv_string_chk(&li->li_tv);
4505 if (s == NULL)
4506 goto theend;
4507#ifdef USE_ARGV
4508 argv[argc++] = (char *)s;
4509#else
4510 /* Only escape when needed, double quotes are not always allowed. */
4511 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4512 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004513# ifdef WIN32
4514 int old_ssl = p_ssl;
4515
4516 /* This is using CreateProcess, not cmd.exe. Always use
4517 * double quote and backslashes. */
4518 p_ssl = 0;
4519# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004520 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004521# ifdef WIN32
4522 p_ssl = old_ssl;
4523# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004524 if (s == NULL)
4525 goto theend;
4526 ga_concat(&ga, s);
4527 vim_free(s);
4528 }
4529 else
4530 ga_concat(&ga, s);
4531 if (li->li_next != NULL)
4532 ga_append(&ga, ' ');
4533#endif
4534 }
4535#ifdef USE_ARGV
4536 argv[argc] = NULL;
4537#else
4538 cmd = ga.ga_data;
4539#endif
4540 }
4541
4542#ifdef USE_ARGV
4543 if (ch_log_active())
4544 {
4545 garray_T ga;
4546 int i;
4547
4548 ga_init2(&ga, (int)sizeof(char), 200);
4549 for (i = 0; i < argc; ++i)
4550 {
4551 if (i > 0)
4552 ga_concat(&ga, (char_u *)" ");
4553 ga_concat(&ga, (char_u *)argv[i]);
4554 }
4555 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4556 ga_clear(&ga);
4557 }
4558 mch_start_job(argv, job, &opt);
4559#else
4560 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4561 mch_start_job((char *)cmd, job, &opt);
4562#endif
4563
4564 /* If the channel is reading from a buffer, write lines now. */
4565 if (job->jv_channel != NULL)
4566 channel_write_in(job->jv_channel);
4567
4568theend:
4569#ifdef USE_ARGV
4570 vim_free(argv);
4571#else
4572 vim_free(ga.ga_data);
4573#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004574 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004575 return job;
4576}
4577
4578/*
4579 * Get the status of "job" and invoke the exit callback when needed.
4580 * The returned string is not allocated.
4581 */
4582 char *
4583job_status(job_T *job)
4584{
4585 char *result;
4586
4587 if (job->jv_status == JOB_ENDED)
4588 /* No need to check, dead is dead. */
4589 result = "dead";
4590 else if (job->jv_status == JOB_FAILED)
4591 result = "fail";
4592 else
4593 {
4594 result = mch_job_status(job);
4595 if (job->jv_status == JOB_ENDED)
4596 ch_log(job->jv_channel, "Job ended");
4597 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4598 {
4599 typval_T argv[3];
4600 typval_T rettv;
4601 int dummy;
4602
4603 /* invoke the exit callback; make sure the refcount is > 0 */
4604 ++job->jv_refcount;
4605 argv[0].v_type = VAR_JOB;
4606 argv[0].vval.v_job = job;
4607 argv[1].v_type = VAR_NUMBER;
4608 argv[1].vval.v_number = job->jv_exitval;
4609 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004610 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4611 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004612 clear_tv(&rettv);
4613 --job->jv_refcount;
4614 }
4615 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4616 {
4617 /* The job was already unreferenced, now that it ended it can be
4618 * freed. Careful: caller must not use "job" after this! */
4619 job_free(job);
4620 }
4621 }
4622 return result;
4623}
4624
Bram Moolenaar8950a562016-03-12 15:22:55 +01004625/*
4626 * Implementation of job_info().
4627 */
4628 void
4629job_info(job_T *job, dict_T *dict)
4630{
4631 dictitem_T *item;
4632 varnumber_T nr;
4633
4634 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4635
4636 item = dictitem_alloc((char_u *)"channel");
4637 if (item == NULL)
4638 return;
4639 item->di_tv.v_lock = 0;
4640 item->di_tv.v_type = VAR_CHANNEL;
4641 item->di_tv.vval.v_channel = job->jv_channel;
4642 if (job->jv_channel != NULL)
4643 ++job->jv_channel->ch_refcount;
4644 if (dict_add(dict, item) == FAIL)
4645 dictitem_free(item);
4646
4647#ifdef UNIX
4648 nr = job->jv_pid;
4649#else
4650 nr = job->jv_proc_info.dwProcessId;
4651#endif
4652 dict_add_nr_str(dict, "process", nr, NULL);
4653
4654 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004655 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004656 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4657}
4658
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004659 int
4660job_stop(job_T *job, typval_T *argvars)
4661{
4662 char_u *arg;
4663
4664 if (argvars[1].v_type == VAR_UNKNOWN)
4665 arg = (char_u *)"";
4666 else
4667 {
4668 arg = get_tv_string_chk(&argvars[1]);
4669 if (arg == NULL)
4670 {
4671 EMSG(_(e_invarg));
4672 return 0;
4673 }
4674 }
4675 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4676 if (mch_stop_job(job, arg) == FAIL)
4677 return 0;
4678
4679 /* Assume that "hup" does not kill the job. */
4680 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4681 job->jv_channel->ch_job_killed = TRUE;
4682
4683 /* We don't try freeing the job, obviously the caller still has a
4684 * reference to it. */
4685 return 1;
4686}
4687
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004688#endif /* FEAT_JOB_CHANNEL */