blob: f4dc323b6f428b96019c4875b2b53e89233f2685 [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 Moolenaar99ef0622016-03-06 20:22:25 +01001288 if ((p = alloc(len + 2)) == NULL)
1289 return;
1290 STRCPY(p, line);
1291 p[len] = NL;
1292 p[len + 1] = NUL;
1293 channel_send(channel, PART_IN, p, "write_buf_line()");
1294 vim_free(p);
1295}
1296
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001297/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001298 * Return TRUE if "channel" can be written to.
1299 * Returns FALSE if the input is closed or the write would block.
1300 */
1301 static int
1302can_write_buf_line(channel_T *channel)
1303{
1304 chanpart_T *in_part = &channel->ch_part[PART_IN];
1305
1306 if (in_part->ch_fd == INVALID_FD)
1307 return FALSE; /* pipe was closed */
1308
1309 /* for testing: block every other attempt to write */
1310 if (in_part->ch_block_write == 1)
1311 in_part->ch_block_write = -1;
1312 else if (in_part->ch_block_write == -1)
1313 in_part->ch_block_write = 1;
1314
1315 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1316#ifndef WIN32
1317 {
1318# if defined(HAVE_SELECT)
1319 struct timeval tval;
1320 fd_set wfds;
1321 int ret;
1322
1323 FD_ZERO(&wfds);
1324 FD_SET((int)in_part->ch_fd, &wfds);
1325 tval.tv_sec = 0;
1326 tval.tv_usec = 0;
1327 for (;;)
1328 {
1329 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1330# ifdef EINTR
1331 SOCK_ERRNO;
1332 if (ret == -1 && errno == EINTR)
1333 continue;
1334# endif
1335 if (ret <= 0 || in_part->ch_block_write == 1)
1336 {
1337 if (ret > 0)
1338 ch_log(channel, "FAKED Input not ready for writing");
1339 else
1340 ch_log(channel, "Input not ready for writing");
1341 return FALSE;
1342 }
1343 break;
1344 }
1345# else
1346 struct pollfd fds;
1347
1348 fds.fd = in_part->ch_fd;
1349 fds.events = POLLOUT;
1350 if (poll(&fds, 1, 0) <= 0)
1351 {
1352 ch_log(channel, "Input not ready for writing");
1353 return FALSE;
1354 }
1355 if (in_part->ch_block_write == 1)
1356 {
1357 ch_log(channel, "FAKED Input not ready for writing");
1358 return FALSE;
1359 }
1360# endif
1361 }
1362#endif
1363 return TRUE;
1364}
1365
1366/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001367 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001368 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001369 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001370channel_write_in(channel_T *channel)
1371{
1372 chanpart_T *in_part = &channel->ch_part[PART_IN];
1373 linenr_T lnum;
1374 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001375 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001376
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001377 if (buf == NULL || in_part->ch_buf_append)
1378 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001379 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1380 {
1381 /* buffer was wiped out or unloaded */
1382 in_part->ch_buffer = NULL;
1383 return;
1384 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001385
1386 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1387 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1388 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001389 if (!can_write_buf_line(channel))
1390 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001391 write_buf_line(buf, lnum, channel);
1392 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001393 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001394
1395 if (written == 1)
1396 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1397 else if (written > 1)
1398 ch_logn(channel, "written %d lines to channel", written);
1399
Bram Moolenaar014069a2016-03-03 22:51:40 +01001400 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001401 if (lnum > buf->b_ml.ml_line_count)
1402 {
1403 /* Writing is done, no longer need the buffer. */
1404 in_part->ch_buffer = NULL;
1405 ch_log(channel, "Finished writing all lines to channel");
1406 }
1407 else
1408 ch_logn(channel, "Still %d more lines to write",
1409 buf->b_ml.ml_line_count - lnum + 1);
1410}
1411
1412/*
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001413 * Handle buffer "buf" beeing freed, remove it from any channels.
1414 */
1415 void
1416channel_buffer_free(buf_T *buf)
1417{
1418 channel_T *channel;
1419 int part;
1420
1421 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1422 for (part = PART_SOCK; part <= PART_IN; ++part)
1423 {
1424 chanpart_T *ch_part = &channel->ch_part[part];
1425
1426 if (ch_part->ch_buffer == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001427 {
1428 ch_logs(channel, "%s buffer has been wiped out",
1429 part_names[part]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001430 ch_part->ch_buffer = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001431 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001432 }
1433}
1434
1435/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001436 * Write any lines waiting to be written to a channel.
1437 */
1438 void
1439channel_write_any_lines()
1440{
1441 channel_T *channel;
1442
1443 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1444 {
1445 chanpart_T *in_part = &channel->ch_part[PART_IN];
1446
1447 if (in_part->ch_buffer != NULL)
1448 {
1449 if (in_part->ch_buf_append)
1450 channel_write_new_lines(in_part->ch_buffer);
1451 else
1452 channel_write_in(channel);
1453 }
1454 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001455}
1456
1457/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001458 * Write appended lines above the last one in "buf" to the channel.
1459 */
1460 void
1461channel_write_new_lines(buf_T *buf)
1462{
1463 channel_T *channel;
1464 int found_one = FALSE;
1465
1466 /* There could be more than one channel for the buffer, loop over all of
1467 * them. */
1468 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1469 {
1470 chanpart_T *in_part = &channel->ch_part[PART_IN];
1471 linenr_T lnum;
1472 int written = 0;
1473
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001474 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001475 {
1476 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001477 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001478 found_one = TRUE;
1479 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1480 ++lnum)
1481 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001482 if (!can_write_buf_line(channel))
1483 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001484 write_buf_line(buf, lnum, channel);
1485 ++written;
1486 }
1487
1488 if (written == 1)
1489 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1490 else if (written > 1)
1491 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001492 if (lnum < buf->b_ml.ml_line_count)
1493 ch_logn(channel, "Still %d more lines to write",
1494 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001495
1496 in_part->ch_buf_bot = lnum;
1497 }
1498 }
1499 if (!found_one)
1500 buf->b_write_to_channel = FALSE;
1501}
1502
1503/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001504 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001505 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001506 */
1507 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001508invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1509 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001510{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001511 typval_T rettv;
1512 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001513
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001514 if (safe_to_invoke_callback == 0)
1515 EMSG("INTERNAL: Invoking callback when it is not safe");
1516
Bram Moolenaar77073442016-02-13 23:23:53 +01001517 argv[0].v_type = VAR_CHANNEL;
1518 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001519
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001520 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001521 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001522 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001523 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001524}
1525
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001526/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001527 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001528 * The caller must free it.
1529 * Returns NULL if there is nothing.
1530 */
1531 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001532channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001533{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001534 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001535 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001536 char_u *p;
1537
Bram Moolenaar77073442016-02-13 23:23:53 +01001538 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001539 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001540 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001541 p = node->rq_buffer;
1542 head->rq_next = node->rq_next;
1543 if (node->rq_next == NULL)
1544 head->rq_prev = NULL;
1545 else
1546 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001547 vim_free(node);
1548 return p;
1549}
1550
1551/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001552 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001553 */
1554 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001555channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001556{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001557 readq_T *head = &channel->ch_part[part].ch_head;
1558 readq_T *node = head->rq_next;
1559 long_u len = 1;
1560 char_u *res;
1561 char_u *p;
1562
1563 /* If there is only one buffer just get that one. */
1564 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1565 return channel_get(channel, part);
1566
1567 /* Concatenate everything into one buffer. */
1568 for (node = head->rq_next; node != NULL; node = node->rq_next)
1569 len += (long_u)STRLEN(node->rq_buffer);
1570 res = lalloc(len, TRUE);
1571 if (res == NULL)
1572 return NULL;
1573 *res = NUL;
1574 for (node = head->rq_next; node != NULL; node = node->rq_next)
1575 STRCAT(res, node->rq_buffer);
1576
1577 /* Free all buffers */
1578 do
1579 {
1580 p = channel_get(channel, part);
1581 vim_free(p);
1582 } while (p != NULL);
1583
1584 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001585}
1586
1587/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001588 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001589 * Returns FAIL if that is not possible.
1590 */
1591 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001592channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001593{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001594 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001595 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001596 char_u *p;
1597
Bram Moolenaar77073442016-02-13 23:23:53 +01001598 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001599 return FAIL;
1600
Bram Moolenaar77073442016-02-13 23:23:53 +01001601 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1602 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001603 if (p == NULL)
1604 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001605 STRCPY(p, node->rq_buffer);
1606 STRCAT(p, node->rq_next->rq_buffer);
1607 vim_free(node->rq_next->rq_buffer);
1608 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001609
Bram Moolenaar77073442016-02-13 23:23:53 +01001610 /* dispose of the node and its buffer */
1611 head->rq_next = node->rq_next;
1612 head->rq_next->rq_prev = NULL;
1613 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001614 vim_free(node);
1615 return OK;
1616}
1617
1618/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001619 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001620 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001621 * Returns OK or FAIL.
1622 */
1623 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001624channel_save(channel_T *channel, int part, char_u *buf, int len,
1625 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001626{
1627 readq_T *node;
1628 readq_T *head = &channel->ch_part[part].ch_head;
1629 char_u *p;
1630 int i;
1631
1632 node = (readq_T *)alloc(sizeof(readq_T));
1633 if (node == NULL)
1634 return FAIL; /* out of memory */
1635 node->rq_buffer = alloc(len + 1);
1636 if (node->rq_buffer == NULL)
1637 {
1638 vim_free(node);
1639 return FAIL; /* out of memory */
1640 }
1641
1642 if (channel->ch_part[part].ch_mode == MODE_NL)
1643 {
1644 /* Drop any CR before a NL. */
1645 p = node->rq_buffer;
1646 for (i = 0; i < len; ++i)
1647 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1648 *p++ = buf[i];
1649 *p = NUL;
1650 }
1651 else
1652 {
1653 mch_memmove(node->rq_buffer, buf, len);
1654 node->rq_buffer[len] = NUL;
1655 }
1656
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001657 if (prepend)
1658 {
1659 /* preend node to the head of the queue */
1660 node->rq_next = head->rq_next;
1661 node->rq_prev = NULL;
1662 if (head->rq_next == NULL)
1663 head->rq_prev = node;
1664 else
1665 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001666 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001667 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001668 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001669 {
1670 /* append node to the tail of the queue */
1671 node->rq_next = NULL;
1672 node->rq_prev = head->rq_prev;
1673 if (head->rq_prev == NULL)
1674 head->rq_next = node;
1675 else
1676 head->rq_prev->rq_next = node;
1677 head->rq_prev = node;
1678 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001679
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001680 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001681 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001682 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001683 fprintf(log_fd, "'");
1684 if (fwrite(buf, len, 1, log_fd) != 1)
1685 return FAIL;
1686 fprintf(log_fd, "'\n");
1687 }
1688 return OK;
1689}
1690
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001691 static int
1692channel_fill(js_read_T *reader)
1693{
1694 channel_T *channel = (channel_T *)reader->js_cookie;
1695 int part = reader->js_cookie_arg;
1696 char_u *next = channel_get(channel, part);
1697 int unused;
1698 int len;
1699 char_u *p;
1700
1701 if (next == NULL)
1702 return FALSE;
1703
1704 unused = reader->js_end - reader->js_buf - reader->js_used;
1705 if (unused > 0)
1706 {
1707 /* Prepend unused text. */
1708 len = (int)STRLEN(next);
1709 p = alloc(unused + len + 1);
1710 if (p == NULL)
1711 {
1712 vim_free(next);
1713 return FALSE;
1714 }
1715 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1716 mch_memmove(p + unused, next, len + 1);
1717 vim_free(next);
1718 next = p;
1719 }
1720
1721 vim_free(reader->js_buf);
1722 reader->js_buf = next;
1723 reader->js_used = 0;
1724 return TRUE;
1725}
1726
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001727/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001728 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001729 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001730 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001731 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001732 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001733channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001734{
1735 js_read_T reader;
1736 typval_T listtv;
1737 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001738 chanpart_T *chanpart = &channel->ch_part[part];
1739 jsonq_T *head = &chanpart->ch_json_head;
1740 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001741 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001742
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001743 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001744 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001745
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001746 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001747 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001748 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001749 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001750 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001751
1752 /* When a message is incomplete we wait for a short while for more to
1753 * arrive. After the delay drop the input, otherwise a truncated string
1754 * or list will make us hang. */
1755 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001756 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001757 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001758 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001759 /* Only accept the response when it is a list with at least two
1760 * items. */
1761 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001762 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001763 if (listtv.v_type != VAR_LIST)
1764 ch_error(channel, "Did not receive a list, discarding");
1765 else
1766 ch_errorn(channel, "Expected list with two items, got %d",
1767 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001768 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001769 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001770 else
1771 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001772 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1773 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001774 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001775 else
1776 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001777 item->jq_value = alloc_tv();
1778 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001779 {
1780 vim_free(item);
1781 clear_tv(&listtv);
1782 }
1783 else
1784 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001785 *item->jq_value = listtv;
1786 item->jq_prev = head->jq_prev;
1787 head->jq_prev = item;
1788 item->jq_next = NULL;
1789 if (item->jq_prev == NULL)
1790 head->jq_next = item;
1791 else
1792 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001793 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001794 }
1795 }
1796 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001797
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001798 if (status == OK)
1799 chanpart->ch_waiting = FALSE;
1800 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001801 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001802 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001803 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001804 /* First time encountering incomplete message, set a deadline of
1805 * 100 msec. */
1806 ch_log(channel, "Incomplete message - wait for more");
1807 reader.js_used = 0;
1808 chanpart->ch_waiting = TRUE;
1809#ifdef WIN32
1810 chanpart->ch_deadline = GetTickCount() + 100L;
1811#else
1812 gettimeofday(&chanpart->ch_deadline, NULL);
1813 chanpart->ch_deadline.tv_usec += 100 * 1000;
1814 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1815 {
1816 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1817 ++chanpart->ch_deadline.tv_sec;
1818 }
1819#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001820 }
1821 else
1822 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001823 int timeout;
1824#ifdef WIN32
1825 timeout = GetTickCount() > chanpart->ch_deadline;
1826#else
1827 {
1828 struct timeval now_tv;
1829
1830 gettimeofday(&now_tv, NULL);
1831 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1832 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1833 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1834 }
1835#endif
1836 if (timeout)
1837 {
1838 status = FAIL;
1839 chanpart->ch_waiting = FALSE;
1840 }
1841 else
1842 {
1843 reader.js_used = 0;
1844 ch_log(channel, "still waiting on incomplete message");
1845 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001846 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001847 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001848
1849 if (status == FAIL)
1850 {
1851 ch_error(channel, "Decoding failed - discarding input");
1852 ret = FALSE;
1853 chanpart->ch_waiting = FALSE;
1854 }
1855 else if (reader.js_buf[reader.js_used] != NUL)
1856 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001857 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001858 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001859 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1860 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001861 ret = status == MAYBE ? FALSE: TRUE;
1862 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001863 else
1864 ret = FALSE;
1865
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001866 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001867 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001868}
1869
1870/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001871 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001872 */
1873 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001874remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001875{
Bram Moolenaar77073442016-02-13 23:23:53 +01001876 if (node->cq_prev == NULL)
1877 head->cq_next = node->cq_next;
1878 else
1879 node->cq_prev->cq_next = node->cq_next;
1880 if (node->cq_next == NULL)
1881 head->cq_prev = node->cq_prev;
1882 else
1883 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001884}
1885
1886/*
1887 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001888 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001889 */
1890 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001891remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001892{
Bram Moolenaar77073442016-02-13 23:23:53 +01001893 if (node->jq_prev == NULL)
1894 head->jq_next = node->jq_next;
1895 else
1896 node->jq_prev->jq_next = node->jq_next;
1897 if (node->jq_next == NULL)
1898 head->jq_prev = node->jq_prev;
1899 else
1900 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001901 vim_free(node);
1902}
1903
1904/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001905 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001906 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001907 * When "id" is zero or negative jut get the first message. But not the one
1908 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001909 * Return OK when found and return the value in "rettv".
1910 * Return FAIL otherwise.
1911 */
1912 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001913channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001914{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001915 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001916 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001917
Bram Moolenaar77073442016-02-13 23:23:53 +01001918 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001919 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001920 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001921 typval_T *tv = &l->lv_first->li_tv;
1922
1923 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001924 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001925 || tv->vval.v_number == 0
1926 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001927 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001928 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001929 if (tv->v_type == VAR_NUMBER)
1930 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001931 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001932 return OK;
1933 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001934 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001935 }
1936 return FAIL;
1937}
1938
Bram Moolenaarece61b02016-02-20 21:39:05 +01001939#define CH_JSON_MAX_ARGS 4
1940
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001941/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001942 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001943 * "argv[0]" is the command string.
1944 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001945 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001946 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001947channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001948{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001949 char_u *cmd = argv[0].vval.v_string;
1950 char_u *arg;
1951 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001952
Bram Moolenaarece61b02016-02-20 21:39:05 +01001953 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001954 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001955 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001956 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001957 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001958 return;
1959 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001960 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001961 if (arg == NULL)
1962 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001963
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001964 if (STRCMP(cmd, "ex") == 0)
1965 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001966 int save_called_emsg = called_emsg;
1967
1968 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001969 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001970 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001971 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001972 --emsg_silent;
1973 if (called_emsg)
1974 ch_logs(channel, "Ex command error: '%s'",
1975 (char *)get_vim_var_str(VV_ERRMSG));
1976 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001977 }
1978 else if (STRCMP(cmd, "normal") == 0)
1979 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001980 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001981
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001982 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001983 ea.arg = arg;
1984 ea.addr_count = 0;
1985 ea.forceit = TRUE; /* no mapping */
1986 ex_normal(&ea);
1987 }
1988 else if (STRCMP(cmd, "redraw") == 0)
1989 {
1990 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001991
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001992 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001993 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001994 ex_redraw(&ea);
1995 showruler(FALSE);
1996 setcursor();
1997 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001998#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001999 if (gui.in_use)
2000 {
2001 gui_update_cursor(FALSE, FALSE);
2002 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002003 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002004#endif
2005 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002006 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002007 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002008 int is_call = cmd[0] == 'c';
2009 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002010
Bram Moolenaarece61b02016-02-20 21:39:05 +01002011 if (argv[id_idx].v_type != VAR_UNKNOWN
2012 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002013 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002014 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002015 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002016 EMSG("E904: last argument for expr/call must be a number");
2017 }
2018 else if (is_call && argv[2].v_type != VAR_LIST)
2019 {
2020 ch_error(channel, "third argument for call must be a list");
2021 if (p_verbose > 2)
2022 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002023 }
2024 else
2025 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002026 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002027 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002028 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002029 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002030
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002031 /* Don't pollute the display with errors. */
2032 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002033 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002034 {
2035 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002036 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002037 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002038 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002039 {
2040 ch_logs(channel, "Calling '%s'", (char *)arg);
2041 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2042 tv = &res_tv;
2043 else
2044 tv = NULL;
2045 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002046
2047 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002048 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002049 int id = argv[id_idx].vval.v_number;
2050
Bram Moolenaar55fab432016-02-07 16:53:13 +01002051 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002052 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002053 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002054 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002055 /* If evaluation failed or the result can't be encoded
2056 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002057 vim_free(json);
2058 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002059 err_tv.v_type = VAR_STRING;
2060 err_tv.vval.v_string = (char_u *)"ERROR";
2061 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002062 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002063 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002064 if (json != NULL)
2065 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002066 channel_send(channel,
2067 part == PART_SOCK ? PART_SOCK : PART_IN,
2068 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002069 vim_free(json);
2070 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002071 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002072 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002073 if (tv == &res_tv)
2074 clear_tv(tv);
2075 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002076 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002077 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002078 }
2079 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002080 {
2081 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002082 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002083 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002084}
2085
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002086/*
2087 * Invoke the callback at "cbhead".
2088 * Does not redraw but sets channel_need_redraw.
2089 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002090 static void
2091invoke_one_time_callback(
2092 channel_T *channel,
2093 cbq_T *cbhead,
2094 cbq_T *item,
2095 typval_T *argv)
2096{
2097 ch_logs(channel, "Invoking one-time callback %s",
2098 (char *)item->cq_callback);
2099 /* Remove the item from the list first, if the callback
2100 * invokes ch_close() the list will be cleared. */
2101 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002102 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002103 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002104 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002105 vim_free(item);
2106}
2107
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002108 static void
2109append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2110{
2111 buf_T *save_curbuf = curbuf;
2112 linenr_T lnum = buffer->b_ml.ml_line_count;
2113 int save_write_to = buffer->b_write_to_channel;
2114
2115 /* If the buffer is also used as input insert above the last
2116 * line. Don't write these lines. */
2117 if (save_write_to)
2118 {
2119 --lnum;
2120 buffer->b_write_to_channel = FALSE;
2121 }
2122
2123 /* Append to the buffer */
2124 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2125
2126 curbuf = buffer;
2127 u_sync(TRUE);
2128 /* ignore undo failure, undo is not very useful here */
2129 ignored = u_save(lnum, lnum + 1);
2130
2131 ml_append(lnum, msg, 0, FALSE);
2132 appended_lines_mark(lnum, 1L);
2133 curbuf = save_curbuf;
2134
2135 if (buffer->b_nwindows > 0)
2136 {
2137 win_T *wp;
2138 win_T *save_curwin;
2139
2140 FOR_ALL_WINDOWS(wp)
2141 {
2142 if (wp->w_buffer == buffer
2143 && (save_write_to
2144 ? wp->w_cursor.lnum == lnum + 1
2145 : (wp->w_cursor.lnum == lnum
2146 && wp->w_cursor.col == 0)))
2147 {
2148 ++wp->w_cursor.lnum;
2149 save_curwin = curwin;
2150 curwin = wp;
2151 curbuf = curwin->w_buffer;
2152 scroll_cursor_bot(0, FALSE);
2153 curwin = save_curwin;
2154 curbuf = curwin->w_buffer;
2155 }
2156 }
2157 redraw_buf_later(buffer, VALID);
2158 channel_need_redraw = TRUE;
2159 }
2160
2161 if (save_write_to)
2162 {
2163 channel_T *ch;
2164
2165 /* Find channels reading from this buffer and adjust their
2166 * next-to-read line number. */
2167 buffer->b_write_to_channel = TRUE;
2168 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2169 {
2170 chanpart_T *in_part = &ch->ch_part[PART_IN];
2171
2172 if (in_part->ch_buffer == buffer)
2173 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2174 }
2175 }
2176}
2177
Bram Moolenaar437905c2016-04-26 19:01:05 +02002178 static void
2179drop_messages(channel_T *channel, int part)
2180{
2181 char_u *msg;
2182
2183 while ((msg = channel_get(channel, part)) != NULL)
2184 {
2185 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2186 vim_free(msg);
2187 }
2188}
2189
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002190/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002191 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002192 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002193 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002194 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002195 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002196may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002197{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002198 char_u *msg = NULL;
2199 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002200 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002201 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002202 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002203 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002204 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002205 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002206 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002207 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002208
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002209 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002210 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002211 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002212
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002213 /* Use a message-specific callback, part callback or channel callback */
2214 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2215 if (cbitem->cq_seq_nr == 0)
2216 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002217 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002218 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002219 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002220 partial = cbitem->cq_partial;
2221 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002222 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002223 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002224 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002225 partial = channel->ch_part[part].ch_partial;
2226 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002227 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002228 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002229 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002230 partial = channel->ch_partial;
2231 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002232
Bram Moolenaar187db502016-02-27 14:44:26 +01002233 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002234 if (buffer != NULL && !buf_valid(buffer))
2235 {
2236 /* buffer was wiped out */
2237 channel->ch_part[part].ch_buffer = NULL;
2238 buffer = NULL;
2239 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002240
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002241 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002242 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002243 listitem_T *item;
2244 int argc = 0;
2245
Bram Moolenaard7ece102016-02-02 23:23:02 +01002246 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002247 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002248 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002249 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002250 channel_parse_json(channel, part);
2251 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002252 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002253 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002254
Bram Moolenaarece61b02016-02-20 21:39:05 +01002255 for (item = listtv->vval.v_list->lv_first;
2256 item != NULL && argc < CH_JSON_MAX_ARGS;
2257 item = item->li_next)
2258 argv[argc++] = item->li_tv;
2259 while (argc < CH_JSON_MAX_ARGS)
2260 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002261
Bram Moolenaarece61b02016-02-20 21:39:05 +01002262 if (argv[0].v_type == VAR_STRING)
2263 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002264 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002265 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002266 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002267 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002268 }
2269
Bram Moolenaarece61b02016-02-20 21:39:05 +01002270 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002271 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002272 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002273 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002274 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002275 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002276 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002277 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002278 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002279 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002280 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002281 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002282 return FALSE;
2283 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002284 else
2285 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002286 /* If there is no callback or buffer drop the message. */
2287 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002288 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002289 /* If there is a close callback it may use ch_read() to get the
2290 * messages. */
2291 if (channel->ch_close_cb == NULL)
2292 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002293 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002294 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002295
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002296 if (ch_mode == MODE_NL)
2297 {
2298 char_u *nl;
2299 char_u *buf;
2300
2301 /* See if we have a message ending in NL in the first buffer. If
2302 * not try to concatenate the first and the second buffer. */
2303 while (TRUE)
2304 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002305 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002306 nl = vim_strchr(buf, NL);
2307 if (nl != NULL)
2308 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002309 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002310 return FALSE; /* incomplete message */
2311 }
2312 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002313 {
2314 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002315 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002316 *nl = NUL;
2317 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002318 else
2319 {
2320 /* Copy the message into allocated memory and remove it from
2321 * the buffer. */
2322 msg = vim_strnsave(buf, (int)(nl - buf));
2323 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2324 }
2325 }
2326 else
2327 /* For a raw channel we don't know where the message ends, just
2328 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002329 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002330
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002331 if (msg == NULL)
2332 return FALSE; /* out of memory (and avoids Coverity warning) */
2333
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002334 argv[1].v_type = VAR_STRING;
2335 argv[1].vval.v_string = msg;
2336 }
2337
Bram Moolenaara07fec92016-02-05 21:04:08 +01002338 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002339 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002340 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002341
2342 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002343 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002344 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002345 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002346 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002347 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002348 break;
2349 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002350 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002351 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002352 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002353 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002354 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002355 if (buffer != NULL)
2356 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002357 if (msg == NULL)
2358 /* JSON or JS mode: re-encode the message. */
2359 msg = json_encode(listtv, ch_mode);
2360 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002361 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002362 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002363
Bram Moolenaar187db502016-02-27 14:44:26 +01002364 if (callback != NULL)
2365 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002366 if (cbitem != NULL)
2367 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2368 else
2369 {
2370 /* invoke the channel callback */
2371 ch_logs(channel, "Invoking channel callback %s",
2372 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002373 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002374 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002375 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002376 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002377 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002378 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002379
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002380 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002381 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002382 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002383
2384 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002385}
2386
2387/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002388 * Return TRUE when channel "channel" is open for writing to.
2389 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002390 */
2391 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002392channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002393{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002394 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002395 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002396}
2397
2398/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002399 * Return TRUE when channel "channel" is open for reading or writing.
2400 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002401 */
2402 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002403channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002404{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002405 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002406 || channel->CH_IN_FD != INVALID_FD
2407 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002408 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002409}
2410
2411/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002412 * Return TRUE if "channel" has JSON or other typeahead.
2413 */
2414 static int
2415channel_has_readahead(channel_T *channel, int part)
2416{
2417 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2418
2419 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2420 {
2421 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2422 jsonq_T *item = head->jq_next;
2423
2424 return item != NULL;
2425 }
2426 return channel_peek(channel, part) != NULL;
2427}
2428
2429/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002430 * Return a string indicating the status of the channel.
2431 */
2432 char *
2433channel_status(channel_T *channel)
2434{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002435 int part;
2436 int has_readahead = FALSE;
2437
Bram Moolenaar77073442016-02-13 23:23:53 +01002438 if (channel == NULL)
2439 return "fail";
2440 if (channel_is_open(channel))
2441 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002442 for (part = PART_SOCK; part <= PART_ERR; ++part)
2443 if (channel_has_readahead(channel, part))
2444 {
2445 has_readahead = TRUE;
2446 break;
2447 }
2448
2449 if (has_readahead)
2450 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002451 return "closed";
2452}
2453
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002454 static void
2455channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2456{
2457 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002458 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002459 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002460 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002461
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002462 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002463 STRCAT(namebuf, "_");
2464 tail = STRLEN(namebuf);
2465
2466 STRCPY(namebuf + tail, "status");
2467 dict_add_nr_str(dict, namebuf, 0,
2468 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2469
2470 STRCPY(namebuf + tail, "mode");
2471 switch (chanpart->ch_mode)
2472 {
2473 case MODE_NL: s = "NL"; break;
2474 case MODE_RAW: s = "RAW"; break;
2475 case MODE_JSON: s = "JSON"; break;
2476 case MODE_JS: s = "JS"; break;
2477 }
2478 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2479
2480 STRCPY(namebuf + tail, "io");
2481 if (part == PART_SOCK)
2482 s = "socket";
2483 else switch (chanpart->ch_io)
2484 {
2485 case JIO_NULL: s = "null"; break;
2486 case JIO_PIPE: s = "pipe"; break;
2487 case JIO_FILE: s = "file"; break;
2488 case JIO_BUFFER: s = "buffer"; break;
2489 case JIO_OUT: s = "out"; break;
2490 }
2491 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2492
2493 STRCPY(namebuf + tail, "timeout");
2494 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2495}
2496
2497 void
2498channel_info(channel_T *channel, dict_T *dict)
2499{
2500 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2501 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2502
2503 if (channel->ch_hostname != NULL)
2504 {
2505 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2506 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2507 channel_part_info(channel, dict, "sock", PART_SOCK);
2508 }
2509 else
2510 {
2511 channel_part_info(channel, dict, "out", PART_OUT);
2512 channel_part_info(channel, dict, "err", PART_ERR);
2513 channel_part_info(channel, dict, "in", PART_IN);
2514 }
2515}
2516
Bram Moolenaar77073442016-02-13 23:23:53 +01002517/*
2518 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002519 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002520 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002521 */
2522 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002523channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002524{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002525 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002526
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002527#ifdef FEAT_GUI
2528 channel_gui_unregister(channel);
2529#endif
2530
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002531 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002532 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002533 sock_close(channel->CH_SOCK_FD);
2534 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002535 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002536 may_close_part(&channel->CH_IN_FD);
2537 may_close_part(&channel->CH_OUT_FD);
2538 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002539
Bram Moolenaar8b374212016-02-24 20:43:06 +01002540 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002541 {
2542 typval_T argv[1];
2543 typval_T rettv;
2544 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002545 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002546
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002547 /* Invoke callbacks before the close callback, since it's weird to
2548 * first invoke the close callback. Increment the refcount to avoid
2549 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002550 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002551 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002552 for (part = PART_SOCK; part <= PART_ERR; ++part)
2553 while (may_invoke_callback(channel, part))
2554 ;
2555
2556 /* Invoke the close callback, if still set. */
2557 if (channel->ch_close_cb != NULL)
2558 {
2559 ch_logs(channel, "Invoking close callback %s",
2560 (char *)channel->ch_close_cb);
2561 argv[0].v_type = VAR_CHANNEL;
2562 argv[0].vval.v_channel = channel;
2563 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002564 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2565 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002566 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002567 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002568 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002569 --channel->ch_refcount;
2570
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 Moolenaarcefe4f92016-05-04 21:49:19 +02002577 if (channel_need_redraw)
2578 {
2579 channel_need_redraw = FALSE;
2580 redraw_after_callback();
2581 }
2582
Bram Moolenaar437905c2016-04-26 19:01:05 +02002583 /* any remaining messages are useless now */
2584 for (part = PART_SOCK; part <= PART_ERR; ++part)
2585 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002586 }
2587
2588 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002589}
2590
Bram Moolenaard04a0202016-01-26 23:30:18 +01002591/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002592 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002593 * Returns NULL if there is nothing.
2594 */
2595 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002596channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002597{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002598 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002599
Bram Moolenaar77073442016-02-13 23:23:53 +01002600 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002601 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002602 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002603}
2604
2605/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002606 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002607 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002608 static void
2609channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002610{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002611 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2612 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002613
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002614 while (channel_peek(channel, part) != NULL)
2615 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002616
2617 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002618 {
2619 cbq_T *node = cb_head->cq_next;
2620
2621 remove_cb_node(cb_head, node);
2622 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002623 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002624 vim_free(node);
2625 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002626
2627 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002628 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002629 free_tv(json_head->jq_next->jq_value);
2630 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002631 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002632
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002633 vim_free(channel->ch_part[part].ch_callback);
2634 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002635 partial_unref(channel->ch_part[part].ch_partial);
2636 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002637}
2638
2639/*
2640 * Clear all the read buffers on "channel".
2641 */
2642 void
2643channel_clear(channel_T *channel)
2644{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002645 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002646 vim_free(channel->ch_hostname);
2647 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002648 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002649 channel_clear_one(channel, PART_OUT);
2650 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002651 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002652 vim_free(channel->ch_callback);
2653 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002654 partial_unref(channel->ch_partial);
2655 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002656 vim_free(channel->ch_close_cb);
2657 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002658 partial_unref(channel->ch_close_partial);
2659 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002660}
2661
Bram Moolenaar77073442016-02-13 23:23:53 +01002662#if defined(EXITFREE) || defined(PROTO)
2663 void
2664channel_free_all(void)
2665{
2666 channel_T *channel;
2667
Bram Moolenaard6051b52016-02-28 15:49:03 +01002668 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002669 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2670 channel_clear(channel);
2671}
2672#endif
2673
2674
Bram Moolenaar715d2852016-04-30 17:06:31 +02002675/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002676#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002677
2678/* Buffer size for reading incoming messages. */
2679#define MAXMSGSIZE 4096
2680
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002681#if defined(HAVE_SELECT)
2682/*
2683 * Add write fds where we are waiting for writing to be possible.
2684 */
2685 static int
2686channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2687{
2688 int maxfd = maxfd_arg;
2689 channel_T *ch;
2690
2691 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2692 {
2693 chanpart_T *in_part = &ch->ch_part[PART_IN];
2694
2695 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2696 {
2697 FD_SET((int)in_part->ch_fd, wfds);
2698 if ((int)in_part->ch_fd >= maxfd)
2699 maxfd = (int)in_part->ch_fd + 1;
2700 }
2701 }
2702 return maxfd;
2703}
2704#else
2705/*
2706 * Add write fds where we are waiting for writing to be possible.
2707 */
2708 static int
2709channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2710{
2711 int nfd = nfd_in;
2712 channel_T *ch;
2713
2714 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2715 {
2716 chanpart_T *in_part = &ch->ch_part[PART_IN];
2717
2718 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2719 {
2720 in_part->ch_poll_idx = nfd;
2721 fds[nfd].fd = in_part->ch_fd;
2722 fds[nfd].events = POLLOUT;
2723 ++nfd;
2724 }
2725 else
2726 in_part->ch_poll_idx = -1;
2727 }
2728 return nfd;
2729}
2730#endif
2731
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002732typedef enum {
2733 CW_READY,
2734 CW_NOT_READY,
2735 CW_ERROR
2736} channel_wait_result;
2737
Bram Moolenaard04a0202016-01-26 23:30:18 +01002738/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002739 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002740 * Return CW_READY when there is something to read.
2741 * Return CW_NOT_READY when there is nothing to read.
2742 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002743 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002744 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002745channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002746{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002747 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002748 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002749
Bram Moolenaard8070362016-02-15 21:56:54 +01002750# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002751 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002752 {
2753 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002754 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002755 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002756 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002757
2758 /* reading from a pipe, not a socket */
2759 while (TRUE)
2760 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002761 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2762
2763 if (r && nread > 0)
2764 return CW_READY;
2765 if (r == 0)
2766 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002767
2768 /* perhaps write some buffer lines */
2769 channel_write_any_lines();
2770
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002771 sleep_time = deadline - GetTickCount();
2772 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002773 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002774 /* Wait for a little while. Very short at first, up to 10 msec
2775 * after looping a few times. */
2776 if (sleep_time > delay)
2777 sleep_time = delay;
2778 Sleep(sleep_time);
2779 delay = delay * 2;
2780 if (delay > 10)
2781 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002782 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002783 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002784 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002785#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002786 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002787#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002788 struct timeval tval;
2789 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002790 fd_set wfds;
2791 int ret;
2792 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002793
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002794 tval.tv_sec = timeout / 1000;
2795 tval.tv_usec = (timeout % 1000) * 1000;
2796 for (;;)
2797 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002798 FD_ZERO(&rfds);
2799 FD_SET((int)fd, &rfds);
2800
2801 /* Write lines to a pipe when a pipe can be written to. Need to
2802 * set this every time, some buffers may be done. */
2803 maxfd = (int)fd + 1;
2804 FD_ZERO(&wfds);
2805 maxfd = channel_fill_wfds(maxfd, &wfds);
2806
2807 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002808# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002809 SOCK_ERRNO;
2810 if (ret == -1 && errno == EINTR)
2811 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002812# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002813 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002814 {
2815 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002816 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002817 channel_write_any_lines();
2818 continue;
2819 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002820 break;
2821 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002822#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002823 for (;;)
2824 {
2825 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2826 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002827
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002828 fds[0].fd = fd;
2829 fds[0].events = POLLIN;
2830 nfd = channel_fill_poll_write(nfd, fds);
2831 if (poll(fds, nfd, timeout) > 0)
2832 {
2833 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002834 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002835 channel_write_any_lines();
2836 continue;
2837 }
2838 break;
2839 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002840#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002841 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002842 return CW_NOT_READY;
2843}
2844
2845 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002846channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002847{
2848 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002849 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2850 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002851
2852 /* Queue a "DETACH" netbeans message in the command queue in order to
2853 * terminate the netbeans session later. Do not end the session here
2854 * directly as we may be running in the context of a call to
2855 * netbeans_parse_messages():
2856 * netbeans_parse_messages
2857 * -> autocmd triggered while processing the netbeans cmd
2858 * -> ui_breakcheck
2859 * -> gui event loop or select loop
2860 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002861 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002862 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002863 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002864 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002865 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2866
2867 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002868 * died. Don't close the channel right away, it may be the wrong moment
2869 * to invoke callbacks. */
2870 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02002871
2872#ifdef FEAT_GUI
2873 /* Stop listening to GUI events right away. */
2874 channel_gui_unregister(channel);
2875#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002876}
2877
2878 static void
2879channel_close_now(channel_T *channel)
2880{
2881 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002882 channel_close(channel, TRUE);
2883 if (channel->ch_nb_close_cb != NULL)
2884 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002885}
2886
2887/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002888 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002889 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002890 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002891 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002892 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002893channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002894{
2895 static char_u *buf = NULL;
2896 int len = 0;
2897 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002898 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002899 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002900
Bram Moolenaar5850a762016-05-27 19:59:48 +02002901 /* If we detected a read error don't try reading again. */
2902 if (channel->ch_to_be_closed)
2903 return;
2904
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002905 fd = channel->ch_part[part].ch_fd;
2906 if (fd == INVALID_FD)
2907 {
2908 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002909 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002910 }
2911 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002912
2913 /* Allocate a buffer to read into. */
2914 if (buf == NULL)
2915 {
2916 buf = alloc(MAXMSGSIZE);
2917 if (buf == NULL)
2918 return; /* out of memory! */
2919 }
2920
2921 /* Keep on reading for as long as there is something to read.
2922 * Use select() or poll() to avoid blocking on a message that is exactly
2923 * MAXMSGSIZE long. */
2924 for (;;)
2925 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002926 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002927 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002928 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002929 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002930 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002931 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002932 if (len <= 0)
2933 break; /* error or nothing more to read */
2934
2935 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002936 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002937 readlen += len;
2938 if (len < MAXMSGSIZE)
2939 break; /* did read everything that's available */
2940 }
2941
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002942 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002943 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002944 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002945
2946#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002947 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002948 if (CH_HAS_GUI && gtk_main_level() > 0)
2949 gtk_main_quit();
2950#endif
2951}
2952
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002953/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002954 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002955 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002956 * Returns what was read in allocated memory.
2957 * Returns NULL in case of error or timeout.
2958 */
2959 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002960channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002961{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002962 char_u *buf;
2963 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002964 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002965 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002966 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002967
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002968 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002969 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002970
2971 while (TRUE)
2972 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002973 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002974 if (buf != NULL && (mode == MODE_RAW
2975 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2976 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002977 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002978 continue;
2979
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002980 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002981 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002982 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002983 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002984 {
2985 ch_log(channel, "Timed out");
2986 return NULL;
2987 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002988 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002989 }
2990
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002991 if (mode == MODE_RAW)
2992 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002993 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002994 }
2995 else
2996 {
2997 nl = vim_strchr(buf, NL);
2998 if (nl[1] == NUL)
2999 {
3000 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003001 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003002 *nl = NUL;
3003 }
3004 else
3005 {
3006 /* Copy the message into allocated memory and remove it from the
3007 * buffer. */
3008 msg = vim_strnsave(buf, (int)(nl - buf));
3009 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
3010 }
3011 }
3012 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003013 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003014 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003015}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003016
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003017/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003018 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003019 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003020 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003021 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003022 */
3023 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003024channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003025 channel_T *channel,
3026 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003027 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003028 int id,
3029 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003030{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003031 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003032 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003033 int timeout;
3034 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003035
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003036 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003037 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003038 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003039 for (;;)
3040 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003041 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003042
3043 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003044 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003045 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003046 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003047 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003048 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003049
Bram Moolenaard7ece102016-02-02 23:23:02 +01003050 if (!more)
3051 {
3052 /* Handle any other messages in the queue. If done some more
3053 * messages may have arrived. */
3054 if (channel_parse_messages())
3055 continue;
3056
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003057 /* Wait for up to the timeout. If there was an incomplete message
3058 * use the deadline for that. */
3059 timeout = timeout_arg;
3060 if (chanpart->ch_waiting)
3061 {
3062#ifdef WIN32
3063 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3064#else
3065 {
3066 struct timeval now_tv;
3067
3068 gettimeofday(&now_tv, NULL);
3069 timeout = (chanpart->ch_deadline.tv_sec
3070 - now_tv.tv_sec) * 1000
3071 + (chanpart->ch_deadline.tv_usec
3072 - now_tv.tv_usec) / 1000
3073 + 1;
3074 }
3075#endif
3076 if (timeout < 0)
3077 {
3078 /* Something went wrong, channel_parse_json() didn't
3079 * discard message. Cancel waiting. */
3080 chanpart->ch_waiting = FALSE;
3081 timeout = timeout_arg;
3082 }
3083 else if (timeout > timeout_arg)
3084 timeout = timeout_arg;
3085 }
3086 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003087 if (fd == INVALID_FD
3088 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003089 {
3090 if (timeout == timeout_arg)
3091 {
3092 if (fd != INVALID_FD)
3093 ch_log(channel, "Timed out");
3094 break;
3095 }
3096 }
3097 else
3098 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003099 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003100 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003101 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003102 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003103}
3104
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003105/*
3106 * Common for ch_read() and ch_readraw().
3107 */
3108 void
3109common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3110{
3111 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003112 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003113 jobopt_T opt;
3114 int mode;
3115 int timeout;
3116 int id = -1;
3117 typval_T *listtv = NULL;
3118
3119 /* return an empty string by default */
3120 rettv->v_type = VAR_STRING;
3121 rettv->vval.v_string = NULL;
3122
3123 clear_job_options(&opt);
3124 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3125 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003126 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003127
Bram Moolenaar437905c2016-04-26 19:01:05 +02003128 if (opt.jo_set & JO_PART)
3129 part = opt.jo_part;
3130 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003131 if (channel != NULL)
3132 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003133 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003134 part = channel_part_read(channel);
3135 mode = channel_get_mode(channel, part);
3136 timeout = channel_get_timeout(channel, part);
3137 if (opt.jo_set & JO_TIMEOUT)
3138 timeout = opt.jo_timeout;
3139
3140 if (raw || mode == MODE_RAW || mode == MODE_NL)
3141 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3142 else
3143 {
3144 if (opt.jo_set & JO_ID)
3145 id = opt.jo_id;
3146 channel_read_json_block(channel, part, timeout, id, &listtv);
3147 if (listtv != NULL)
3148 {
3149 *rettv = *listtv;
3150 vim_free(listtv);
3151 }
3152 else
3153 {
3154 rettv->v_type = VAR_SPECIAL;
3155 rettv->vval.v_number = VVAL_NONE;
3156 }
3157 }
3158 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003159
3160theend:
3161 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003162}
3163
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003164# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3165 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003166/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003167 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003168 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003169 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003170 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003171channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003172{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003173 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003174 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003175
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003176 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003177 for (channel = first_channel; channel != NULL;
3178 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003179 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003180 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003181 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003182 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003183 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003184 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003185 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003186 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003187 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003188}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003189# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003190
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003191# if defined(WIN32) || defined(PROTO)
3192/*
3193 * Check the channels for anything that is ready to be read.
3194 * The data is put in the read queue.
3195 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003196 void
3197channel_handle_events(void)
3198{
3199 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003200 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003201 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003202
3203 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3204 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003205 /* If we detected a read error don't try reading again. */
3206 if (channel->ch_to_be_closed)
3207 continue;
3208
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003209 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003210 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003211 {
3212 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003213 if (fd != INVALID_FD)
3214 {
3215 int r = channel_wait(channel, fd, 0);
3216
3217 if (r == CW_READY)
3218 channel_read(channel, part, "channel_handle_events");
3219 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003220 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003221 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003222 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003223 }
3224}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003225# endif
3226
Bram Moolenaard04a0202016-01-26 23:30:18 +01003227/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003228 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003229 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003230 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003231 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003232 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003233channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003234{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003235 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003236 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003237 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003238
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003239 fd = channel->ch_part[part].ch_fd;
3240 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003241 {
3242 if (!channel->ch_error && fun != NULL)
3243 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003244 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003245 EMSG2("E630: %s(): write while not connected", fun);
3246 }
3247 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003248 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003249 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003250
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003251 if (log_fd != NULL)
3252 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003253 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003254 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003255 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003256 fprintf(log_fd, "'\n");
3257 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003258 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003259 }
3260
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003261 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003262 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003263 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003264 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003265 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003266 {
3267 if (!channel->ch_error && fun != NULL)
3268 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003269 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003270 EMSG2("E631: %s(): write failed", fun);
3271 }
3272 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003273 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003274 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003275
3276 channel->ch_error = FALSE;
3277 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003278}
3279
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003280/*
3281 * Common for "ch_sendexpr()" and "ch_sendraw()".
3282 * Returns the channel if the caller should read the response.
3283 * Sets "part_read" to the the read fd.
3284 * Otherwise returns NULL.
3285 */
3286 channel_T *
3287send_common(
3288 typval_T *argvars,
3289 char_u *text,
3290 int id,
3291 int eval,
3292 jobopt_T *opt,
3293 char *fun,
3294 int *part_read)
3295{
3296 channel_T *channel;
3297 int part_send;
3298
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003299 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003300 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003301 if (channel == NULL)
3302 return NULL;
3303 part_send = channel_part_send(channel);
3304 *part_read = channel_part_read(channel);
3305
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003306 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3307 return NULL;
3308
3309 /* Set the callback. An empty callback means no callback and not reading
3310 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3311 * allowed. */
3312 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3313 {
3314 if (eval)
3315 {
3316 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3317 return NULL;
3318 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003319 channel_set_req_callback(channel, part_send,
3320 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003321 }
3322
3323 if (channel_send(channel, part_send, text, fun) == OK
3324 && opt->jo_callback == NULL)
3325 return channel;
3326 return NULL;
3327}
3328
3329/*
3330 * common for "ch_evalexpr()" and "ch_sendexpr()"
3331 */
3332 void
3333ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3334{
3335 char_u *text;
3336 typval_T *listtv;
3337 channel_T *channel;
3338 int id;
3339 ch_mode_T ch_mode;
3340 int part_send;
3341 int part_read;
3342 jobopt_T opt;
3343 int timeout;
3344
3345 /* return an empty string by default */
3346 rettv->v_type = VAR_STRING;
3347 rettv->vval.v_string = NULL;
3348
Bram Moolenaar437905c2016-04-26 19:01:05 +02003349 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003350 if (channel == NULL)
3351 return;
3352 part_send = channel_part_send(channel);
3353
3354 ch_mode = channel_get_mode(channel, part_send);
3355 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3356 {
3357 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3358 return;
3359 }
3360
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003361 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003362 text = json_encode_nr_expr(id, &argvars[1],
3363 ch_mode == MODE_JS ? JSON_JS : 0);
3364 if (text == NULL)
3365 return;
3366
3367 channel = send_common(argvars, text, id, eval, &opt,
3368 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3369 vim_free(text);
3370 if (channel != NULL && eval)
3371 {
3372 if (opt.jo_set & JO_TIMEOUT)
3373 timeout = opt.jo_timeout;
3374 else
3375 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003376 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3377 == OK)
3378 {
3379 list_T *list = listtv->vval.v_list;
3380
3381 /* Move the item from the list and then change the type to
3382 * avoid the value being freed. */
3383 *rettv = list->lv_last->li_tv;
3384 list->lv_last->li_tv.v_type = VAR_NUMBER;
3385 free_tv(listtv);
3386 }
3387 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003388 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003389}
3390
3391/*
3392 * common for "ch_evalraw()" and "ch_sendraw()"
3393 */
3394 void
3395ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3396{
3397 char_u buf[NUMBUFLEN];
3398 char_u *text;
3399 channel_T *channel;
3400 int part_read;
3401 jobopt_T opt;
3402 int timeout;
3403
3404 /* return an empty string by default */
3405 rettv->v_type = VAR_STRING;
3406 rettv->vval.v_string = NULL;
3407
3408 text = get_tv_string_buf(&argvars[1], buf);
3409 channel = send_common(argvars, text, 0, eval, &opt,
3410 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3411 if (channel != NULL && eval)
3412 {
3413 if (opt.jo_set & JO_TIMEOUT)
3414 timeout = opt.jo_timeout;
3415 else
3416 timeout = channel_get_timeout(channel, part_read);
3417 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3418 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003419 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003420}
3421
Bram Moolenaard04a0202016-01-26 23:30:18 +01003422# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003423/*
3424 * Add open channels to the poll struct.
3425 * Return the adjusted struct index.
3426 * The type of "fds" is hidden to avoid problems with the function proto.
3427 */
3428 int
3429channel_poll_setup(int nfd_in, void *fds_in)
3430{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003431 int nfd = nfd_in;
3432 channel_T *channel;
3433 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003434 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003435
Bram Moolenaar77073442016-02-13 23:23:53 +01003436 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003437 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003438 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003439 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003440 chanpart_T *ch_part = &channel->ch_part[part];
3441
3442 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003443 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003444 ch_part->ch_poll_idx = nfd;
3445 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003446 fds[nfd].events = POLLIN;
3447 nfd++;
3448 }
3449 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003450 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003451 }
3452 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003453
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003454 nfd = channel_fill_poll_write(nfd, fds);
3455
Bram Moolenaare0874f82016-01-24 20:36:41 +01003456 return nfd;
3457}
3458
3459/*
3460 * The type of "fds" is hidden to avoid problems with the function proto.
3461 */
3462 int
3463channel_poll_check(int ret_in, void *fds_in)
3464{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003465 int ret = ret_in;
3466 channel_T *channel;
3467 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003468 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003469 int idx;
3470 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003471
Bram Moolenaar77073442016-02-13 23:23:53 +01003472 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003473 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003474 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003475 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003476 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003477
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003478 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003479 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003480 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003481 --ret;
3482 }
3483 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003484
3485 in_part = &channel->ch_part[PART_IN];
3486 idx = in_part->ch_poll_idx;
3487 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3488 {
3489 if (in_part->ch_buf_append)
3490 {
3491 if (in_part->ch_buffer != NULL)
3492 channel_write_new_lines(in_part->ch_buffer);
3493 }
3494 else
3495 channel_write_in(channel);
3496 --ret;
3497 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003498 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003499
3500 return ret;
3501}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003502# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003503
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003504# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003505/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003506 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003507 */
3508 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003509channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003510{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003511 int maxfd = maxfd_in;
3512 channel_T *channel;
3513 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003514 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003515 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003516
Bram Moolenaar77073442016-02-13 23:23:53 +01003517 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003518 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003519 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003520 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003521 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003522
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003523 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003524 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003525 FD_SET((int)fd, rfds);
3526 if (maxfd < (int)fd)
3527 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003528 }
3529 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003530 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003531
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003532 maxfd = channel_fill_wfds(maxfd, wfds);
3533
Bram Moolenaare0874f82016-01-24 20:36:41 +01003534 return maxfd;
3535}
3536
3537/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003538 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003539 */
3540 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003541channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003542{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003543 int ret = ret_in;
3544 channel_T *channel;
3545 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003546 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003547 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003548 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003549
Bram Moolenaar77073442016-02-13 23:23:53 +01003550 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003551 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003552 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003553 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003554 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003555
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003556 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003557 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003558 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003559 --ret;
3560 }
3561 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003562
3563 in_part = &channel->ch_part[PART_IN];
3564 if (ret > 0 && in_part->ch_fd != INVALID_FD
3565 && FD_ISSET(in_part->ch_fd, wfds))
3566 {
3567 if (in_part->ch_buf_append)
3568 {
3569 if (in_part->ch_buffer != NULL)
3570 channel_write_new_lines(in_part->ch_buffer);
3571 }
3572 else
3573 channel_write_in(channel);
3574 --ret;
3575 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003576 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003577
3578 return ret;
3579}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003580# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003581
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003582/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003583 * Execute queued up commands.
3584 * Invoked from the main loop when it's safe to execute received commands.
3585 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003586 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003587 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003588channel_parse_messages(void)
3589{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003590 channel_T *channel = first_channel;
3591 int ret = FALSE;
3592 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003593 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003594
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003595 ++safe_to_invoke_callback;
3596
Bram Moolenaard0b65022016-03-06 21:50:33 +01003597 /* Only do this message when another message was given, otherwise we get
3598 * lots of them. */
3599 if (did_log_msg)
3600 {
3601 ch_log(NULL, "looking for messages on channels");
3602 did_log_msg = FALSE;
3603 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003604 while (channel != NULL)
3605 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003606 if (channel->ch_to_be_closed)
3607 {
3608 channel->ch_to_be_closed = FALSE;
3609 channel_close_now(channel);
3610 /* channel may have been freed, start over */
3611 channel = first_channel;
3612 continue;
3613 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003614 if (channel->ch_to_be_freed)
3615 {
3616 channel_free(channel);
3617 /* channel has been freed, start over */
3618 channel = first_channel;
3619 continue;
3620 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003621 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003622 {
3623 /* channel is no longer useful, free it */
3624 channel_free(channel);
3625 channel = first_channel;
3626 part = PART_SOCK;
3627 continue;
3628 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003629 if (channel->ch_part[part].ch_fd != INVALID_FD
3630 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003631 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003632 /* Increase the refcount, in case the handler causes the channel
3633 * to be unreferenced or closed. */
3634 ++channel->ch_refcount;
3635 r = may_invoke_callback(channel, part);
3636 if (r == OK)
3637 ret = TRUE;
3638 if (channel_unref(channel) || r == OK)
3639 {
3640 /* channel was freed or something was done, start over */
3641 channel = first_channel;
3642 part = PART_SOCK;
3643 continue;
3644 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003645 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003646 if (part < PART_ERR)
3647 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003648 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003649 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003650 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003651 part = PART_SOCK;
3652 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003653 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003654
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003655 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003656 {
3657 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003658 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003659 }
3660
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003661 --safe_to_invoke_callback;
3662
Bram Moolenaard7ece102016-02-02 23:23:02 +01003663 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003664}
3665
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003666/*
3667 * Mark references to lists used in channels.
3668 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003669 int
3670set_ref_in_channel(int copyID)
3671{
Bram Moolenaar77073442016-02-13 23:23:53 +01003672 int abort = FALSE;
3673 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003674 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003675
Bram Moolenaar77073442016-02-13 23:23:53 +01003676 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003677 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003678 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003679 tv.v_type = VAR_CHANNEL;
3680 tv.vval.v_channel = channel;
3681 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003682 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003683 return abort;
3684}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003685
3686/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003687 * Return the "part" to write to for "channel".
3688 */
3689 int
3690channel_part_send(channel_T *channel)
3691{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003692 if (channel->CH_SOCK_FD == INVALID_FD)
3693 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003694 return PART_SOCK;
3695}
3696
3697/*
3698 * Return the default "part" to read from for "channel".
3699 */
3700 int
3701channel_part_read(channel_T *channel)
3702{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003703 if (channel->CH_SOCK_FD == INVALID_FD)
3704 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003705 return PART_SOCK;
3706}
3707
3708/*
3709 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003710 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003711 */
3712 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003713channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003714{
Bram Moolenaar77073442016-02-13 23:23:53 +01003715 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003716 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003717 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003718}
3719
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003720/*
3721 * Return the timeout of "channel"/"part"
3722 */
3723 int
3724channel_get_timeout(channel_T *channel, int part)
3725{
3726 return channel->ch_part[part].ch_timeout;
3727}
3728
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003729 static int
3730handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3731{
3732 char_u *val = get_tv_string(item);
3733
3734 opt->jo_set |= jo;
3735 if (STRCMP(val, "nl") == 0)
3736 *modep = MODE_NL;
3737 else if (STRCMP(val, "raw") == 0)
3738 *modep = MODE_RAW;
3739 else if (STRCMP(val, "js") == 0)
3740 *modep = MODE_JS;
3741 else if (STRCMP(val, "json") == 0)
3742 *modep = MODE_JSON;
3743 else
3744 {
3745 EMSG2(_(e_invarg2), val);
3746 return FAIL;
3747 }
3748 return OK;
3749}
3750
3751 static int
3752handle_io(typval_T *item, int part, jobopt_T *opt)
3753{
3754 char_u *val = get_tv_string(item);
3755
3756 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3757 if (STRCMP(val, "null") == 0)
3758 opt->jo_io[part] = JIO_NULL;
3759 else if (STRCMP(val, "pipe") == 0)
3760 opt->jo_io[part] = JIO_PIPE;
3761 else if (STRCMP(val, "file") == 0)
3762 opt->jo_io[part] = JIO_FILE;
3763 else if (STRCMP(val, "buffer") == 0)
3764 opt->jo_io[part] = JIO_BUFFER;
3765 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3766 opt->jo_io[part] = JIO_OUT;
3767 else
3768 {
3769 EMSG2(_(e_invarg2), val);
3770 return FAIL;
3771 }
3772 return OK;
3773}
3774
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003775/*
3776 * Clear a jobopt_T before using it.
3777 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003778 void
3779clear_job_options(jobopt_T *opt)
3780{
3781 vim_memset(opt, 0, sizeof(jobopt_T));
3782}
3783
3784/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003785 * Free any members of a jobopt_T.
3786 */
3787 void
3788free_job_options(jobopt_T *opt)
3789{
3790 if (opt->jo_partial != NULL)
3791 partial_unref(opt->jo_partial);
3792 if (opt->jo_out_partial != NULL)
3793 partial_unref(opt->jo_out_partial);
3794 if (opt->jo_err_partial != NULL)
3795 partial_unref(opt->jo_err_partial);
3796 if (opt->jo_close_partial != NULL)
3797 partial_unref(opt->jo_close_partial);
3798}
3799
3800/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003801 * Get the PART_ number from the first character of an option name.
3802 */
3803 static int
3804part_from_char(int c)
3805{
3806 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3807}
3808
3809/*
3810 * Get the option entries from the dict in "tv", parse them and put the result
3811 * in "opt".
3812 * Only accept options in "supported".
3813 * If an option value is invalid return FAIL.
3814 */
3815 int
3816get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3817{
3818 typval_T *item;
3819 char_u *val;
3820 dict_T *dict;
3821 int todo;
3822 hashitem_T *hi;
3823 int part;
3824
3825 opt->jo_set = 0;
3826 if (tv->v_type == VAR_UNKNOWN)
3827 return OK;
3828 if (tv->v_type != VAR_DICT)
3829 {
3830 EMSG(_(e_invarg));
3831 return FAIL;
3832 }
3833 dict = tv->vval.v_dict;
3834 if (dict == NULL)
3835 return OK;
3836
3837 todo = (int)dict->dv_hashtab.ht_used;
3838 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3839 if (!HASHITEM_EMPTY(hi))
3840 {
3841 item = &dict_lookup(hi)->di_tv;
3842
3843 if (STRCMP(hi->hi_key, "mode") == 0)
3844 {
3845 if (!(supported & JO_MODE))
3846 break;
3847 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3848 return FAIL;
3849 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003850 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003851 {
3852 if (!(supported & JO_IN_MODE))
3853 break;
3854 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3855 == FAIL)
3856 return FAIL;
3857 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003858 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003859 {
3860 if (!(supported & JO_OUT_MODE))
3861 break;
3862 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3863 == FAIL)
3864 return FAIL;
3865 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003866 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003867 {
3868 if (!(supported & JO_ERR_MODE))
3869 break;
3870 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3871 == FAIL)
3872 return FAIL;
3873 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003874 else if (STRCMP(hi->hi_key, "in_io") == 0
3875 || STRCMP(hi->hi_key, "out_io") == 0
3876 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003877 {
3878 if (!(supported & JO_OUT_IO))
3879 break;
3880 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3881 return FAIL;
3882 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003883 else if (STRCMP(hi->hi_key, "in_name") == 0
3884 || STRCMP(hi->hi_key, "out_name") == 0
3885 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003886 {
3887 part = part_from_char(*hi->hi_key);
3888
3889 if (!(supported & JO_OUT_IO))
3890 break;
3891 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3892 opt->jo_io_name[part] =
3893 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3894 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003895 else if (STRCMP(hi->hi_key, "in_buf") == 0
3896 || STRCMP(hi->hi_key, "out_buf") == 0
3897 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003898 {
3899 part = part_from_char(*hi->hi_key);
3900
3901 if (!(supported & JO_OUT_IO))
3902 break;
3903 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3904 opt->jo_io_buf[part] = get_tv_number(item);
3905 if (opt->jo_io_buf[part] <= 0)
3906 {
3907 EMSG2(_(e_invarg2), get_tv_string(item));
3908 return FAIL;
3909 }
3910 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3911 {
3912 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3913 return FAIL;
3914 }
3915 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003916 else if (STRCMP(hi->hi_key, "in_top") == 0
3917 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003918 {
3919 linenr_T *lp;
3920
3921 if (!(supported & JO_OUT_IO))
3922 break;
3923 if (hi->hi_key[3] == 't')
3924 {
3925 lp = &opt->jo_in_top;
3926 opt->jo_set |= JO_IN_TOP;
3927 }
3928 else
3929 {
3930 lp = &opt->jo_in_bot;
3931 opt->jo_set |= JO_IN_BOT;
3932 }
3933 *lp = get_tv_number(item);
3934 if (*lp < 0)
3935 {
3936 EMSG2(_(e_invarg2), get_tv_string(item));
3937 return FAIL;
3938 }
3939 }
3940 else if (STRCMP(hi->hi_key, "channel") == 0)
3941 {
3942 if (!(supported & JO_OUT_IO))
3943 break;
3944 opt->jo_set |= JO_CHANNEL;
3945 if (item->v_type != VAR_CHANNEL)
3946 {
3947 EMSG2(_(e_invarg2), "channel");
3948 return FAIL;
3949 }
3950 opt->jo_channel = item->vval.v_channel;
3951 }
3952 else if (STRCMP(hi->hi_key, "callback") == 0)
3953 {
3954 if (!(supported & JO_CALLBACK))
3955 break;
3956 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003957 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003958 if (opt->jo_callback == NULL)
3959 {
3960 EMSG2(_(e_invarg2), "callback");
3961 return FAIL;
3962 }
3963 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003964 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003965 {
3966 if (!(supported & JO_OUT_CALLBACK))
3967 break;
3968 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003969 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003970 if (opt->jo_out_cb == NULL)
3971 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003972 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003973 return FAIL;
3974 }
3975 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003976 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003977 {
3978 if (!(supported & JO_ERR_CALLBACK))
3979 break;
3980 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003981 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003982 if (opt->jo_err_cb == NULL)
3983 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003984 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003985 return FAIL;
3986 }
3987 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003988 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003989 {
3990 if (!(supported & JO_CLOSE_CALLBACK))
3991 break;
3992 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003993 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003994 if (opt->jo_close_cb == NULL)
3995 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003996 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003997 return FAIL;
3998 }
3999 }
4000 else if (STRCMP(hi->hi_key, "waittime") == 0)
4001 {
4002 if (!(supported & JO_WAITTIME))
4003 break;
4004 opt->jo_set |= JO_WAITTIME;
4005 opt->jo_waittime = get_tv_number(item);
4006 }
4007 else if (STRCMP(hi->hi_key, "timeout") == 0)
4008 {
4009 if (!(supported & JO_TIMEOUT))
4010 break;
4011 opt->jo_set |= JO_TIMEOUT;
4012 opt->jo_timeout = get_tv_number(item);
4013 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004014 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004015 {
4016 if (!(supported & JO_OUT_TIMEOUT))
4017 break;
4018 opt->jo_set |= JO_OUT_TIMEOUT;
4019 opt->jo_out_timeout = get_tv_number(item);
4020 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004021 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004022 {
4023 if (!(supported & JO_ERR_TIMEOUT))
4024 break;
4025 opt->jo_set |= JO_ERR_TIMEOUT;
4026 opt->jo_err_timeout = get_tv_number(item);
4027 }
4028 else if (STRCMP(hi->hi_key, "part") == 0)
4029 {
4030 if (!(supported & JO_PART))
4031 break;
4032 opt->jo_set |= JO_PART;
4033 val = get_tv_string(item);
4034 if (STRCMP(val, "err") == 0)
4035 opt->jo_part = PART_ERR;
4036 else
4037 {
4038 EMSG2(_(e_invarg2), val);
4039 return FAIL;
4040 }
4041 }
4042 else if (STRCMP(hi->hi_key, "id") == 0)
4043 {
4044 if (!(supported & JO_ID))
4045 break;
4046 opt->jo_set |= JO_ID;
4047 opt->jo_id = get_tv_number(item);
4048 }
4049 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4050 {
4051 if (!(supported & JO_STOPONEXIT))
4052 break;
4053 opt->jo_set |= JO_STOPONEXIT;
4054 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4055 opt->jo_soe_buf);
4056 if (opt->jo_stoponexit == NULL)
4057 {
4058 EMSG2(_(e_invarg2), "stoponexit");
4059 return FAIL;
4060 }
4061 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004062 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004063 {
4064 if (!(supported & JO_EXIT_CB))
4065 break;
4066 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004067 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
4068 {
4069 opt->jo_exit_partial = item->vval.v_partial;
4070 opt->jo_exit_cb = item->vval.v_partial->pt_name;
4071 }
4072 else
4073 opt->jo_exit_cb = get_tv_string_buf_chk(
4074 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004075 if (opt->jo_exit_cb == NULL)
4076 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004077 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004078 return FAIL;
4079 }
4080 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004081 else if (STRCMP(hi->hi_key, "block_write") == 0)
4082 {
4083 if (!(supported & JO_BLOCK_WRITE))
4084 break;
4085 opt->jo_set |= JO_BLOCK_WRITE;
4086 opt->jo_block_write = get_tv_number(item);
4087 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004088 else
4089 break;
4090 --todo;
4091 }
4092 if (todo > 0)
4093 {
4094 EMSG2(_(e_invarg2), hi->hi_key);
4095 return FAIL;
4096 }
4097
4098 return OK;
4099}
4100
4101/*
4102 * Get the channel from the argument.
4103 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004104 * When "check_open" is TRUE check that the channel can be used.
4105 * When "reading" is TRUE "check_open" considers typeahead useful.
4106 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004107 */
4108 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004109get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004110{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004111 channel_T *channel = NULL;
4112 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004113
4114 if (tv->v_type == VAR_JOB)
4115 {
4116 if (tv->vval.v_job != NULL)
4117 channel = tv->vval.v_job->jv_channel;
4118 }
4119 else if (tv->v_type == VAR_CHANNEL)
4120 {
4121 channel = tv->vval.v_channel;
4122 }
4123 else
4124 {
4125 EMSG2(_(e_invarg2), get_tv_string(tv));
4126 return NULL;
4127 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004128 if (channel != NULL && reading)
4129 has_readahead = channel_has_readahead(channel,
4130 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004131
Bram Moolenaar437905c2016-04-26 19:01:05 +02004132 if (check_open && (channel == NULL || (!channel_is_open(channel)
4133 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004134 {
4135 EMSG(_("E906: not an open channel"));
4136 return NULL;
4137 }
4138 return channel;
4139}
4140
4141static job_T *first_job = NULL;
4142
4143 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004144job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004145{
4146 ch_log(job->jv_channel, "Freeing job");
4147 if (job->jv_channel != NULL)
4148 {
4149 /* The link from the channel to the job doesn't count as a reference,
4150 * thus don't decrement the refcount of the job. The reference from
4151 * the job to the channel does count the refrence, decrement it and
4152 * NULL the reference. We don't set ch_job_killed, unreferencing the
4153 * job doesn't mean it stops running. */
4154 job->jv_channel->ch_job = NULL;
4155 channel_unref(job->jv_channel);
4156 }
4157 mch_clear_job(job);
4158
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004159 vim_free(job->jv_stoponexit);
4160 vim_free(job->jv_exit_cb);
4161 partial_unref(job->jv_exit_partial);
4162}
4163
4164 static void
4165job_free_job(job_T *job)
4166{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004167 if (job->jv_next != NULL)
4168 job->jv_next->jv_prev = job->jv_prev;
4169 if (job->jv_prev == NULL)
4170 first_job = job->jv_next;
4171 else
4172 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004173 vim_free(job);
4174}
4175
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004176 static void
4177job_free(job_T *job)
4178{
4179 if (!in_free_unref_items)
4180 {
4181 job_free_contents(job);
4182 job_free_job(job);
4183 }
4184}
4185
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004186/*
4187 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004188 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4189 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004190 */
4191 static int
4192job_still_useful(job_T *job)
4193{
4194 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004195 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4196 || (job->jv_channel != NULL
4197 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004198}
4199
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004200/*
4201 * Mark references in jobs that are still useful.
4202 */
4203 int
4204set_ref_in_job(int copyID)
4205{
4206 int abort = FALSE;
4207 job_T *job;
4208 typval_T tv;
4209
4210 for (job = first_job; job != NULL; job = job->jv_next)
4211 if (job_still_useful(job))
4212 {
4213 tv.v_type = VAR_JOB;
4214 tv.vval.v_job = job;
4215 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4216 }
4217 return abort;
4218}
4219
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004220 void
4221job_unref(job_T *job)
4222{
4223 if (job != NULL && --job->jv_refcount <= 0)
4224 {
4225 /* Do not free the job when it has not ended yet and there is a
4226 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004227 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004228 {
4229 job_free(job);
4230 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004231 else if (job->jv_channel != NULL
4232 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004233 {
4234 /* Do remove the link to the channel, otherwise it hangs
4235 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004236 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004237 job->jv_channel->ch_job = NULL;
4238 channel_unref(job->jv_channel);
4239 job->jv_channel = NULL;
4240 }
4241 }
4242}
4243
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004244 int
4245free_unused_jobs_contents(int copyID, int mask)
4246{
4247 int did_free = FALSE;
4248 job_T *job;
4249
4250 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004251 if ((job->jv_copyID & mask) != (copyID & mask)
4252 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004253 {
4254 /* Free the channel and ordinary items it contains, but don't
4255 * recurse into Lists, Dictionaries etc. */
4256 job_free_contents(job);
4257 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004258 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004259 return did_free;
4260}
4261
4262 void
4263free_unused_jobs(int copyID, int mask)
4264{
4265 job_T *job;
4266 job_T *job_next;
4267
4268 for (job = first_job; job != NULL; job = job_next)
4269 {
4270 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004271 if ((job->jv_copyID & mask) != (copyID & mask)
4272 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004273 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004274 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004275 job_free_job(job);
4276 }
4277 }
4278}
4279
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004280/*
4281 * Allocate a job. Sets the refcount to one and sets options default.
4282 */
4283 static job_T *
4284job_alloc(void)
4285{
4286 job_T *job;
4287
4288 job = (job_T *)alloc_clear(sizeof(job_T));
4289 if (job != NULL)
4290 {
4291 job->jv_refcount = 1;
4292 job->jv_stoponexit = vim_strsave((char_u *)"term");
4293
4294 if (first_job != NULL)
4295 {
4296 first_job->jv_prev = job;
4297 job->jv_next = first_job;
4298 }
4299 first_job = job;
4300 }
4301 return job;
4302}
4303
4304 void
4305job_set_options(job_T *job, jobopt_T *opt)
4306{
4307 if (opt->jo_set & JO_STOPONEXIT)
4308 {
4309 vim_free(job->jv_stoponexit);
4310 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4311 job->jv_stoponexit = NULL;
4312 else
4313 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4314 }
4315 if (opt->jo_set & JO_EXIT_CB)
4316 {
4317 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004318 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004319 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004320 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004321 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004322 job->jv_exit_partial = NULL;
4323 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004324 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004325 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004326 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004327 job->jv_exit_partial = opt->jo_exit_partial;
4328 if (job->jv_exit_partial != NULL)
4329 ++job->jv_exit_partial->pt_refcount;
4330 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004331 }
4332}
4333
4334/*
4335 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4336 */
4337 void
4338job_stop_on_exit()
4339{
4340 job_T *job;
4341
4342 for (job = first_job; job != NULL; job = job->jv_next)
4343 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4344 mch_stop_job(job, job->jv_stoponexit);
4345}
4346
4347/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004348 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004349 */
4350 void
4351job_check_ended(void)
4352{
4353 static time_t last_check = 0;
4354 time_t now;
4355 job_T *job;
4356 job_T *next;
4357
4358 /* Only do this once in 10 seconds. */
4359 now = time(NULL);
4360 if (last_check + 10 < now)
4361 {
4362 last_check = now;
4363 for (job = first_job; job != NULL; job = next)
4364 {
4365 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004366 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004367 job_status(job); /* may free "job" */
4368 }
4369 }
4370}
4371
4372/*
4373 * "job_start()" function
4374 */
4375 job_T *
4376job_start(typval_T *argvars)
4377{
4378 job_T *job;
4379 char_u *cmd = NULL;
4380#if defined(UNIX)
4381# define USE_ARGV
4382 char **argv = NULL;
4383 int argc = 0;
4384#else
4385 garray_T ga;
4386#endif
4387 jobopt_T opt;
4388 int part;
4389
4390 job = job_alloc();
4391 if (job == NULL)
4392 return NULL;
4393
4394 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004395#ifndef USE_ARGV
4396 ga_init2(&ga, (int)sizeof(char*), 20);
4397#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004398
4399 /* Default mode is NL. */
4400 clear_job_options(&opt);
4401 opt.jo_mode = MODE_NL;
4402 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004403 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4404 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004405 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004406
4407 /* Check that when io is "file" that there is a file name. */
4408 for (part = PART_OUT; part <= PART_IN; ++part)
4409 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4410 && opt.jo_io[part] == JIO_FILE
4411 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4412 || *opt.jo_io_name[part] == NUL))
4413 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004414 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004415 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004416 }
4417
4418 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4419 {
4420 buf_T *buf = NULL;
4421
4422 /* check that we can find the buffer before starting the job */
4423 if (opt.jo_set & JO_IN_BUF)
4424 {
4425 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4426 if (buf == NULL)
4427 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4428 }
4429 else if (!(opt.jo_set & JO_IN_NAME))
4430 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004431 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004432 }
4433 else
4434 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4435 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004436 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004437 if (buf->b_ml.ml_mfp == NULL)
4438 {
4439 char_u numbuf[NUMBUFLEN];
4440 char_u *s;
4441
4442 if (opt.jo_set & JO_IN_BUF)
4443 {
4444 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4445 s = numbuf;
4446 }
4447 else
4448 s = opt.jo_io_name[PART_IN];
4449 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004450 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004451 }
4452 job->jv_in_buf = buf;
4453 }
4454
4455 job_set_options(job, &opt);
4456
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004457 if (argvars[0].v_type == VAR_STRING)
4458 {
4459 /* Command is a string. */
4460 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004461 if (cmd == NULL || *cmd == NUL)
4462 {
4463 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004464 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004465 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004466#ifdef USE_ARGV
4467 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004468 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004469 argv[argc] = NULL;
4470#endif
4471 }
4472 else if (argvars[0].v_type != VAR_LIST
4473 || argvars[0].vval.v_list == NULL
4474 || argvars[0].vval.v_list->lv_len < 1)
4475 {
4476 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004477 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004478 }
4479 else
4480 {
4481 list_T *l = argvars[0].vval.v_list;
4482 listitem_T *li;
4483 char_u *s;
4484
4485#ifdef USE_ARGV
4486 /* Pass argv[] to mch_call_shell(). */
4487 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4488 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004489 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004490#endif
4491 for (li = l->lv_first; li != NULL; li = li->li_next)
4492 {
4493 s = get_tv_string_chk(&li->li_tv);
4494 if (s == NULL)
4495 goto theend;
4496#ifdef USE_ARGV
4497 argv[argc++] = (char *)s;
4498#else
4499 /* Only escape when needed, double quotes are not always allowed. */
4500 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4501 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004502# ifdef WIN32
4503 int old_ssl = p_ssl;
4504
4505 /* This is using CreateProcess, not cmd.exe. Always use
4506 * double quote and backslashes. */
4507 p_ssl = 0;
4508# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004509 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004510# ifdef WIN32
4511 p_ssl = old_ssl;
4512# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004513 if (s == NULL)
4514 goto theend;
4515 ga_concat(&ga, s);
4516 vim_free(s);
4517 }
4518 else
4519 ga_concat(&ga, s);
4520 if (li->li_next != NULL)
4521 ga_append(&ga, ' ');
4522#endif
4523 }
4524#ifdef USE_ARGV
4525 argv[argc] = NULL;
4526#else
4527 cmd = ga.ga_data;
4528#endif
4529 }
4530
4531#ifdef USE_ARGV
4532 if (ch_log_active())
4533 {
4534 garray_T ga;
4535 int i;
4536
4537 ga_init2(&ga, (int)sizeof(char), 200);
4538 for (i = 0; i < argc; ++i)
4539 {
4540 if (i > 0)
4541 ga_concat(&ga, (char_u *)" ");
4542 ga_concat(&ga, (char_u *)argv[i]);
4543 }
4544 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4545 ga_clear(&ga);
4546 }
4547 mch_start_job(argv, job, &opt);
4548#else
4549 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4550 mch_start_job((char *)cmd, job, &opt);
4551#endif
4552
4553 /* If the channel is reading from a buffer, write lines now. */
4554 if (job->jv_channel != NULL)
4555 channel_write_in(job->jv_channel);
4556
4557theend:
4558#ifdef USE_ARGV
4559 vim_free(argv);
4560#else
4561 vim_free(ga.ga_data);
4562#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004563 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004564 return job;
4565}
4566
4567/*
4568 * Get the status of "job" and invoke the exit callback when needed.
4569 * The returned string is not allocated.
4570 */
4571 char *
4572job_status(job_T *job)
4573{
4574 char *result;
4575
4576 if (job->jv_status == JOB_ENDED)
4577 /* No need to check, dead is dead. */
4578 result = "dead";
4579 else if (job->jv_status == JOB_FAILED)
4580 result = "fail";
4581 else
4582 {
4583 result = mch_job_status(job);
4584 if (job->jv_status == JOB_ENDED)
4585 ch_log(job->jv_channel, "Job ended");
4586 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4587 {
4588 typval_T argv[3];
4589 typval_T rettv;
4590 int dummy;
4591
4592 /* invoke the exit callback; make sure the refcount is > 0 */
4593 ++job->jv_refcount;
4594 argv[0].v_type = VAR_JOB;
4595 argv[0].vval.v_job = job;
4596 argv[1].v_type = VAR_NUMBER;
4597 argv[1].vval.v_number = job->jv_exitval;
4598 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004599 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4600 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004601 clear_tv(&rettv);
4602 --job->jv_refcount;
4603 }
4604 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4605 {
4606 /* The job was already unreferenced, now that it ended it can be
4607 * freed. Careful: caller must not use "job" after this! */
4608 job_free(job);
4609 }
4610 }
4611 return result;
4612}
4613
Bram Moolenaar8950a562016-03-12 15:22:55 +01004614/*
4615 * Implementation of job_info().
4616 */
4617 void
4618job_info(job_T *job, dict_T *dict)
4619{
4620 dictitem_T *item;
4621 varnumber_T nr;
4622
4623 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4624
4625 item = dictitem_alloc((char_u *)"channel");
4626 if (item == NULL)
4627 return;
4628 item->di_tv.v_lock = 0;
4629 item->di_tv.v_type = VAR_CHANNEL;
4630 item->di_tv.vval.v_channel = job->jv_channel;
4631 if (job->jv_channel != NULL)
4632 ++job->jv_channel->ch_refcount;
4633 if (dict_add(dict, item) == FAIL)
4634 dictitem_free(item);
4635
4636#ifdef UNIX
4637 nr = job->jv_pid;
4638#else
4639 nr = job->jv_proc_info.dwProcessId;
4640#endif
4641 dict_add_nr_str(dict, "process", nr, NULL);
4642
4643 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004644 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004645 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4646}
4647
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004648 int
4649job_stop(job_T *job, typval_T *argvars)
4650{
4651 char_u *arg;
4652
4653 if (argvars[1].v_type == VAR_UNKNOWN)
4654 arg = (char_u *)"";
4655 else
4656 {
4657 arg = get_tv_string_chk(&argvars[1]);
4658 if (arg == NULL)
4659 {
4660 EMSG(_(e_invarg));
4661 return 0;
4662 }
4663 }
4664 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4665 if (mch_stop_job(job, arg) == FAIL)
4666 return 0;
4667
4668 /* Assume that "hup" does not kill the job. */
4669 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4670 job->jv_channel->ch_job_killed = TRUE;
4671
4672 /* We don't try freeing the job, obviously the caller still has a
4673 * reference to it. */
4674 return 1;
4675}
4676
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004677#endif /* FEAT_JOB_CHANNEL */