blob: 94681ee89809e9ddab177274f3d698875e5bf39c [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
2570 /* the callback is only called once */
2571 vim_free(channel->ch_close_cb);
2572 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002573 partial_unref(channel->ch_close_partial);
2574 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002575
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002576 --channel->ch_refcount;
2577
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002578 if (channel_need_redraw)
2579 {
2580 channel_need_redraw = FALSE;
2581 redraw_after_callback();
2582 }
2583
Bram Moolenaar437905c2016-04-26 19:01:05 +02002584 /* any remaining messages are useless now */
2585 for (part = PART_SOCK; part <= PART_ERR; ++part)
2586 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002587 }
2588
2589 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002590}
2591
Bram Moolenaard04a0202016-01-26 23:30:18 +01002592/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002593 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002594 * Returns NULL if there is nothing.
2595 */
2596 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002597channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002598{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002599 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002600
Bram Moolenaar77073442016-02-13 23:23:53 +01002601 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002602 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002603 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002604}
2605
2606/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002607 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002608 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002609 static void
2610channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002611{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002612 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2613 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002614
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002615 while (channel_peek(channel, part) != NULL)
2616 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002617
2618 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002619 {
2620 cbq_T *node = cb_head->cq_next;
2621
2622 remove_cb_node(cb_head, node);
2623 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002624 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002625 vim_free(node);
2626 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002627
2628 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002629 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002630 free_tv(json_head->jq_next->jq_value);
2631 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002632 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002633
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002634 vim_free(channel->ch_part[part].ch_callback);
2635 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002636 partial_unref(channel->ch_part[part].ch_partial);
2637 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002638}
2639
2640/*
2641 * Clear all the read buffers on "channel".
2642 */
2643 void
2644channel_clear(channel_T *channel)
2645{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002646 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002647 vim_free(channel->ch_hostname);
2648 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002649 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002650 channel_clear_one(channel, PART_OUT);
2651 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002652 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002653 vim_free(channel->ch_callback);
2654 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002655 partial_unref(channel->ch_partial);
2656 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002657 vim_free(channel->ch_close_cb);
2658 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002659 partial_unref(channel->ch_close_partial);
2660 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002661}
2662
Bram Moolenaar77073442016-02-13 23:23:53 +01002663#if defined(EXITFREE) || defined(PROTO)
2664 void
2665channel_free_all(void)
2666{
2667 channel_T *channel;
2668
Bram Moolenaard6051b52016-02-28 15:49:03 +01002669 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002670 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2671 channel_clear(channel);
2672}
2673#endif
2674
2675
Bram Moolenaar715d2852016-04-30 17:06:31 +02002676/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002677#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002678
2679/* Buffer size for reading incoming messages. */
2680#define MAXMSGSIZE 4096
2681
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002682#if defined(HAVE_SELECT)
2683/*
2684 * Add write fds where we are waiting for writing to be possible.
2685 */
2686 static int
2687channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2688{
2689 int maxfd = maxfd_arg;
2690 channel_T *ch;
2691
2692 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2693 {
2694 chanpart_T *in_part = &ch->ch_part[PART_IN];
2695
2696 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2697 {
2698 FD_SET((int)in_part->ch_fd, wfds);
2699 if ((int)in_part->ch_fd >= maxfd)
2700 maxfd = (int)in_part->ch_fd + 1;
2701 }
2702 }
2703 return maxfd;
2704}
2705#else
2706/*
2707 * Add write fds where we are waiting for writing to be possible.
2708 */
2709 static int
2710channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2711{
2712 int nfd = nfd_in;
2713 channel_T *ch;
2714
2715 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2716 {
2717 chanpart_T *in_part = &ch->ch_part[PART_IN];
2718
2719 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2720 {
2721 in_part->ch_poll_idx = nfd;
2722 fds[nfd].fd = in_part->ch_fd;
2723 fds[nfd].events = POLLOUT;
2724 ++nfd;
2725 }
2726 else
2727 in_part->ch_poll_idx = -1;
2728 }
2729 return nfd;
2730}
2731#endif
2732
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002733typedef enum {
2734 CW_READY,
2735 CW_NOT_READY,
2736 CW_ERROR
2737} channel_wait_result;
2738
Bram Moolenaard04a0202016-01-26 23:30:18 +01002739/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002740 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002741 * Return CW_READY when there is something to read.
2742 * Return CW_NOT_READY when there is nothing to read.
2743 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002744 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002745 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002746channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002747{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002748 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002749 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002750
Bram Moolenaard8070362016-02-15 21:56:54 +01002751# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002752 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002753 {
2754 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002755 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002756 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002757 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002758
2759 /* reading from a pipe, not a socket */
2760 while (TRUE)
2761 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002762 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2763
2764 if (r && nread > 0)
2765 return CW_READY;
2766 if (r == 0)
2767 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002768
2769 /* perhaps write some buffer lines */
2770 channel_write_any_lines();
2771
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002772 sleep_time = deadline - GetTickCount();
2773 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002774 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002775 /* Wait for a little while. Very short at first, up to 10 msec
2776 * after looping a few times. */
2777 if (sleep_time > delay)
2778 sleep_time = delay;
2779 Sleep(sleep_time);
2780 delay = delay * 2;
2781 if (delay > 10)
2782 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002783 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002784 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002785 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002786#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002787 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002788#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002789 struct timeval tval;
2790 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002791 fd_set wfds;
2792 int ret;
2793 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002794
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002795 tval.tv_sec = timeout / 1000;
2796 tval.tv_usec = (timeout % 1000) * 1000;
2797 for (;;)
2798 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002799 FD_ZERO(&rfds);
2800 FD_SET((int)fd, &rfds);
2801
2802 /* Write lines to a pipe when a pipe can be written to. Need to
2803 * set this every time, some buffers may be done. */
2804 maxfd = (int)fd + 1;
2805 FD_ZERO(&wfds);
2806 maxfd = channel_fill_wfds(maxfd, &wfds);
2807
2808 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002809# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002810 SOCK_ERRNO;
2811 if (ret == -1 && errno == EINTR)
2812 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002813# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002814 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002815 {
2816 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002817 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002818 channel_write_any_lines();
2819 continue;
2820 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002821 break;
2822 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002823#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002824 for (;;)
2825 {
2826 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2827 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002828
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002829 fds[0].fd = fd;
2830 fds[0].events = POLLIN;
2831 nfd = channel_fill_poll_write(nfd, fds);
2832 if (poll(fds, nfd, timeout) > 0)
2833 {
2834 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002835 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002836 channel_write_any_lines();
2837 continue;
2838 }
2839 break;
2840 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002841#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002842 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002843 return CW_NOT_READY;
2844}
2845
2846 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002847channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002848{
2849 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002850 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2851 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002852
2853 /* Queue a "DETACH" netbeans message in the command queue in order to
2854 * terminate the netbeans session later. Do not end the session here
2855 * directly as we may be running in the context of a call to
2856 * netbeans_parse_messages():
2857 * netbeans_parse_messages
2858 * -> autocmd triggered while processing the netbeans cmd
2859 * -> ui_breakcheck
2860 * -> gui event loop or select loop
2861 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002862 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002863 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002864 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002865 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002866 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2867
2868 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002869 * died. Don't close the channel right away, it may be the wrong moment
2870 * to invoke callbacks. */
2871 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02002872
2873#ifdef FEAT_GUI
2874 /* Stop listening to GUI events right away. */
2875 channel_gui_unregister(channel);
2876#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002877}
2878
2879 static void
2880channel_close_now(channel_T *channel)
2881{
2882 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002883 channel_close(channel, TRUE);
2884 if (channel->ch_nb_close_cb != NULL)
2885 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002886}
2887
2888/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002889 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002890 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002891 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002892 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002893 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002894channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002895{
2896 static char_u *buf = NULL;
2897 int len = 0;
2898 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002899 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002900 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002901
Bram Moolenaar5850a762016-05-27 19:59:48 +02002902 /* If we detected a read error don't try reading again. */
2903 if (channel->ch_to_be_closed)
2904 return;
2905
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002906 fd = channel->ch_part[part].ch_fd;
2907 if (fd == INVALID_FD)
2908 {
2909 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002910 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002911 }
2912 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002913
2914 /* Allocate a buffer to read into. */
2915 if (buf == NULL)
2916 {
2917 buf = alloc(MAXMSGSIZE);
2918 if (buf == NULL)
2919 return; /* out of memory! */
2920 }
2921
2922 /* Keep on reading for as long as there is something to read.
2923 * Use select() or poll() to avoid blocking on a message that is exactly
2924 * MAXMSGSIZE long. */
2925 for (;;)
2926 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002927 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002928 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002929 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002930 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002931 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002932 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002933 if (len <= 0)
2934 break; /* error or nothing more to read */
2935
2936 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002937 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002938 readlen += len;
2939 if (len < MAXMSGSIZE)
2940 break; /* did read everything that's available */
2941 }
2942
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002943 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002944 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002945 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002946
2947#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002948 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002949 if (CH_HAS_GUI && gtk_main_level() > 0)
2950 gtk_main_quit();
2951#endif
2952}
2953
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002954/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002955 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002956 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002957 * Returns what was read in allocated memory.
2958 * Returns NULL in case of error or timeout.
2959 */
2960 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002961channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002962{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002963 char_u *buf;
2964 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002965 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002966 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002967 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002968
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002969 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002970 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002971
2972 while (TRUE)
2973 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002974 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002975 if (buf != NULL && (mode == MODE_RAW
2976 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2977 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002978 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002979 continue;
2980
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002981 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002982 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002983 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002984 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002985 {
2986 ch_log(channel, "Timed out");
2987 return NULL;
2988 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002989 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002990 }
2991
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002992 if (mode == MODE_RAW)
2993 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002994 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002995 }
2996 else
2997 {
2998 nl = vim_strchr(buf, NL);
2999 if (nl[1] == NUL)
3000 {
3001 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003002 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003003 *nl = NUL;
3004 }
3005 else
3006 {
3007 /* Copy the message into allocated memory and remove it from the
3008 * buffer. */
3009 msg = vim_strnsave(buf, (int)(nl - buf));
3010 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
3011 }
3012 }
3013 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003014 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003015 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003016}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003017
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003018/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003019 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003020 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003021 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003022 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003023 */
3024 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003025channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003026 channel_T *channel,
3027 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003028 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003029 int id,
3030 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003031{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003032 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003033 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003034 int timeout;
3035 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003036
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003037 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003038 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003039 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003040 for (;;)
3041 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003042 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003043
3044 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003045 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003046 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003047 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003048 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003049 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003050
Bram Moolenaard7ece102016-02-02 23:23:02 +01003051 if (!more)
3052 {
3053 /* Handle any other messages in the queue. If done some more
3054 * messages may have arrived. */
3055 if (channel_parse_messages())
3056 continue;
3057
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003058 /* Wait for up to the timeout. If there was an incomplete message
3059 * use the deadline for that. */
3060 timeout = timeout_arg;
3061 if (chanpart->ch_waiting)
3062 {
3063#ifdef WIN32
3064 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3065#else
3066 {
3067 struct timeval now_tv;
3068
3069 gettimeofday(&now_tv, NULL);
3070 timeout = (chanpart->ch_deadline.tv_sec
3071 - now_tv.tv_sec) * 1000
3072 + (chanpart->ch_deadline.tv_usec
3073 - now_tv.tv_usec) / 1000
3074 + 1;
3075 }
3076#endif
3077 if (timeout < 0)
3078 {
3079 /* Something went wrong, channel_parse_json() didn't
3080 * discard message. Cancel waiting. */
3081 chanpart->ch_waiting = FALSE;
3082 timeout = timeout_arg;
3083 }
3084 else if (timeout > timeout_arg)
3085 timeout = timeout_arg;
3086 }
3087 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003088 if (fd == INVALID_FD
3089 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003090 {
3091 if (timeout == timeout_arg)
3092 {
3093 if (fd != INVALID_FD)
3094 ch_log(channel, "Timed out");
3095 break;
3096 }
3097 }
3098 else
3099 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003100 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003101 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003102 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003103 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003104}
3105
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003106/*
3107 * Common for ch_read() and ch_readraw().
3108 */
3109 void
3110common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3111{
3112 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003113 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003114 jobopt_T opt;
3115 int mode;
3116 int timeout;
3117 int id = -1;
3118 typval_T *listtv = NULL;
3119
3120 /* return an empty string by default */
3121 rettv->v_type = VAR_STRING;
3122 rettv->vval.v_string = NULL;
3123
3124 clear_job_options(&opt);
3125 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3126 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003127 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003128
Bram Moolenaar437905c2016-04-26 19:01:05 +02003129 if (opt.jo_set & JO_PART)
3130 part = opt.jo_part;
3131 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003132 if (channel != NULL)
3133 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003134 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003135 part = channel_part_read(channel);
3136 mode = channel_get_mode(channel, part);
3137 timeout = channel_get_timeout(channel, part);
3138 if (opt.jo_set & JO_TIMEOUT)
3139 timeout = opt.jo_timeout;
3140
3141 if (raw || mode == MODE_RAW || mode == MODE_NL)
3142 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3143 else
3144 {
3145 if (opt.jo_set & JO_ID)
3146 id = opt.jo_id;
3147 channel_read_json_block(channel, part, timeout, id, &listtv);
3148 if (listtv != NULL)
3149 {
3150 *rettv = *listtv;
3151 vim_free(listtv);
3152 }
3153 else
3154 {
3155 rettv->v_type = VAR_SPECIAL;
3156 rettv->vval.v_number = VVAL_NONE;
3157 }
3158 }
3159 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003160
3161theend:
3162 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003163}
3164
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003165# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3166 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003167/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003168 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003169 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003170 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003171 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003172channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003173{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003174 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003175 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003176
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003177 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003178 for (channel = first_channel; channel != NULL;
3179 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003180 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003181 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003182 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003183 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003184 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003185 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003186 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003187 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003188 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003189}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003190# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003191
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003192# if defined(WIN32) || defined(PROTO)
3193/*
3194 * Check the channels for anything that is ready to be read.
3195 * The data is put in the read queue.
3196 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003197 void
3198channel_handle_events(void)
3199{
3200 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003201 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003202 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003203
3204 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3205 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003206 /* If we detected a read error don't try reading again. */
3207 if (channel->ch_to_be_closed)
3208 continue;
3209
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003210 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003211 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003212 {
3213 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003214 if (fd != INVALID_FD)
3215 {
3216 int r = channel_wait(channel, fd, 0);
3217
3218 if (r == CW_READY)
3219 channel_read(channel, part, "channel_handle_events");
3220 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003221 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003222 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003223 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003224 }
3225}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003226# endif
3227
Bram Moolenaard04a0202016-01-26 23:30:18 +01003228/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003229 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003230 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003231 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003232 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003233 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003234channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003235{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003236 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003237 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003238 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003239
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003240 fd = channel->ch_part[part].ch_fd;
3241 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003242 {
3243 if (!channel->ch_error && fun != NULL)
3244 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003245 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003246 EMSG2("E630: %s(): write while not connected", fun);
3247 }
3248 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003249 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003250 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003251
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003252 if (log_fd != NULL)
3253 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003254 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003255 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003256 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003257 fprintf(log_fd, "'\n");
3258 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003259 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003260 }
3261
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003262 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003263 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003264 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003265 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003266 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003267 {
3268 if (!channel->ch_error && fun != NULL)
3269 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003270 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003271 EMSG2("E631: %s(): write failed", fun);
3272 }
3273 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003274 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003275 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003276
3277 channel->ch_error = FALSE;
3278 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003279}
3280
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003281/*
3282 * Common for "ch_sendexpr()" and "ch_sendraw()".
3283 * Returns the channel if the caller should read the response.
3284 * Sets "part_read" to the the read fd.
3285 * Otherwise returns NULL.
3286 */
3287 channel_T *
3288send_common(
3289 typval_T *argvars,
3290 char_u *text,
3291 int id,
3292 int eval,
3293 jobopt_T *opt,
3294 char *fun,
3295 int *part_read)
3296{
3297 channel_T *channel;
3298 int part_send;
3299
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003300 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003301 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003302 if (channel == NULL)
3303 return NULL;
3304 part_send = channel_part_send(channel);
3305 *part_read = channel_part_read(channel);
3306
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003307 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3308 return NULL;
3309
3310 /* Set the callback. An empty callback means no callback and not reading
3311 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3312 * allowed. */
3313 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3314 {
3315 if (eval)
3316 {
3317 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3318 return NULL;
3319 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003320 channel_set_req_callback(channel, part_send,
3321 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003322 }
3323
3324 if (channel_send(channel, part_send, text, fun) == OK
3325 && opt->jo_callback == NULL)
3326 return channel;
3327 return NULL;
3328}
3329
3330/*
3331 * common for "ch_evalexpr()" and "ch_sendexpr()"
3332 */
3333 void
3334ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3335{
3336 char_u *text;
3337 typval_T *listtv;
3338 channel_T *channel;
3339 int id;
3340 ch_mode_T ch_mode;
3341 int part_send;
3342 int part_read;
3343 jobopt_T opt;
3344 int timeout;
3345
3346 /* return an empty string by default */
3347 rettv->v_type = VAR_STRING;
3348 rettv->vval.v_string = NULL;
3349
Bram Moolenaar437905c2016-04-26 19:01:05 +02003350 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003351 if (channel == NULL)
3352 return;
3353 part_send = channel_part_send(channel);
3354
3355 ch_mode = channel_get_mode(channel, part_send);
3356 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3357 {
3358 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3359 return;
3360 }
3361
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003362 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003363 text = json_encode_nr_expr(id, &argvars[1],
3364 ch_mode == MODE_JS ? JSON_JS : 0);
3365 if (text == NULL)
3366 return;
3367
3368 channel = send_common(argvars, text, id, eval, &opt,
3369 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3370 vim_free(text);
3371 if (channel != NULL && eval)
3372 {
3373 if (opt.jo_set & JO_TIMEOUT)
3374 timeout = opt.jo_timeout;
3375 else
3376 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003377 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3378 == OK)
3379 {
3380 list_T *list = listtv->vval.v_list;
3381
3382 /* Move the item from the list and then change the type to
3383 * avoid the value being freed. */
3384 *rettv = list->lv_last->li_tv;
3385 list->lv_last->li_tv.v_type = VAR_NUMBER;
3386 free_tv(listtv);
3387 }
3388 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003389 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003390}
3391
3392/*
3393 * common for "ch_evalraw()" and "ch_sendraw()"
3394 */
3395 void
3396ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3397{
3398 char_u buf[NUMBUFLEN];
3399 char_u *text;
3400 channel_T *channel;
3401 int part_read;
3402 jobopt_T opt;
3403 int timeout;
3404
3405 /* return an empty string by default */
3406 rettv->v_type = VAR_STRING;
3407 rettv->vval.v_string = NULL;
3408
3409 text = get_tv_string_buf(&argvars[1], buf);
3410 channel = send_common(argvars, text, 0, eval, &opt,
3411 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3412 if (channel != NULL && eval)
3413 {
3414 if (opt.jo_set & JO_TIMEOUT)
3415 timeout = opt.jo_timeout;
3416 else
3417 timeout = channel_get_timeout(channel, part_read);
3418 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3419 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003420 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003421}
3422
Bram Moolenaard04a0202016-01-26 23:30:18 +01003423# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003424/*
3425 * Add open channels to the poll struct.
3426 * Return the adjusted struct index.
3427 * The type of "fds" is hidden to avoid problems with the function proto.
3428 */
3429 int
3430channel_poll_setup(int nfd_in, void *fds_in)
3431{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003432 int nfd = nfd_in;
3433 channel_T *channel;
3434 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003435 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003436
Bram Moolenaar77073442016-02-13 23:23:53 +01003437 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003438 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003439 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003440 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003441 chanpart_T *ch_part = &channel->ch_part[part];
3442
3443 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003444 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003445 ch_part->ch_poll_idx = nfd;
3446 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003447 fds[nfd].events = POLLIN;
3448 nfd++;
3449 }
3450 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003451 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003452 }
3453 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003454
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003455 nfd = channel_fill_poll_write(nfd, fds);
3456
Bram Moolenaare0874f82016-01-24 20:36:41 +01003457 return nfd;
3458}
3459
3460/*
3461 * The type of "fds" is hidden to avoid problems with the function proto.
3462 */
3463 int
3464channel_poll_check(int ret_in, void *fds_in)
3465{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003466 int ret = ret_in;
3467 channel_T *channel;
3468 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003469 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003470 int idx;
3471 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003472
Bram Moolenaar77073442016-02-13 23:23:53 +01003473 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003474 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003475 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003476 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003477 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003478
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003479 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003480 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003481 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003482 --ret;
3483 }
3484 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003485
3486 in_part = &channel->ch_part[PART_IN];
3487 idx = in_part->ch_poll_idx;
3488 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3489 {
3490 if (in_part->ch_buf_append)
3491 {
3492 if (in_part->ch_buffer != NULL)
3493 channel_write_new_lines(in_part->ch_buffer);
3494 }
3495 else
3496 channel_write_in(channel);
3497 --ret;
3498 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003499 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003500
3501 return ret;
3502}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003503# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003504
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003505# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003506/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003507 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003508 */
3509 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003510channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003511{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003512 int maxfd = maxfd_in;
3513 channel_T *channel;
3514 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003515 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003516 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003517
Bram Moolenaar77073442016-02-13 23:23:53 +01003518 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003519 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003520 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003521 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003522 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003523
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003524 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003525 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003526 FD_SET((int)fd, rfds);
3527 if (maxfd < (int)fd)
3528 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003529 }
3530 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003531 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003532
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003533 maxfd = channel_fill_wfds(maxfd, wfds);
3534
Bram Moolenaare0874f82016-01-24 20:36:41 +01003535 return maxfd;
3536}
3537
3538/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003539 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003540 */
3541 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003542channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003543{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003544 int ret = ret_in;
3545 channel_T *channel;
3546 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003547 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003548 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003549 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003550
Bram Moolenaar77073442016-02-13 23:23:53 +01003551 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003552 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003553 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003554 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003555 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003556
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003557 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003558 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003559 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003560 --ret;
3561 }
3562 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003563
3564 in_part = &channel->ch_part[PART_IN];
3565 if (ret > 0 && in_part->ch_fd != INVALID_FD
3566 && FD_ISSET(in_part->ch_fd, wfds))
3567 {
3568 if (in_part->ch_buf_append)
3569 {
3570 if (in_part->ch_buffer != NULL)
3571 channel_write_new_lines(in_part->ch_buffer);
3572 }
3573 else
3574 channel_write_in(channel);
3575 --ret;
3576 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003577 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003578
3579 return ret;
3580}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003581# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003582
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003583/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003584 * Execute queued up commands.
3585 * Invoked from the main loop when it's safe to execute received commands.
3586 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003587 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003588 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003589channel_parse_messages(void)
3590{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003591 channel_T *channel = first_channel;
3592 int ret = FALSE;
3593 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003594 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003595
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003596 ++safe_to_invoke_callback;
3597
Bram Moolenaard0b65022016-03-06 21:50:33 +01003598 /* Only do this message when another message was given, otherwise we get
3599 * lots of them. */
3600 if (did_log_msg)
3601 {
3602 ch_log(NULL, "looking for messages on channels");
3603 did_log_msg = FALSE;
3604 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003605 while (channel != NULL)
3606 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003607 if (channel->ch_to_be_closed)
3608 {
3609 channel->ch_to_be_closed = FALSE;
3610 channel_close_now(channel);
3611 /* channel may have been freed, start over */
3612 channel = first_channel;
3613 continue;
3614 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003615 if (channel->ch_to_be_freed)
3616 {
3617 channel_free(channel);
3618 /* channel has been freed, start over */
3619 channel = first_channel;
3620 continue;
3621 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003622 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003623 {
3624 /* channel is no longer useful, free it */
3625 channel_free(channel);
3626 channel = first_channel;
3627 part = PART_SOCK;
3628 continue;
3629 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003630 if (channel->ch_part[part].ch_fd != INVALID_FD
3631 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003632 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003633 /* Increase the refcount, in case the handler causes the channel
3634 * to be unreferenced or closed. */
3635 ++channel->ch_refcount;
3636 r = may_invoke_callback(channel, part);
3637 if (r == OK)
3638 ret = TRUE;
3639 if (channel_unref(channel) || r == OK)
3640 {
3641 /* channel was freed or something was done, start over */
3642 channel = first_channel;
3643 part = PART_SOCK;
3644 continue;
3645 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003646 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003647 if (part < PART_ERR)
3648 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003649 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003650 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003651 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003652 part = PART_SOCK;
3653 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003654 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003655
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003656 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003657 {
3658 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003659 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003660 }
3661
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003662 --safe_to_invoke_callback;
3663
Bram Moolenaard7ece102016-02-02 23:23:02 +01003664 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003665}
3666
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003667/*
3668 * Mark references to lists used in channels.
3669 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003670 int
3671set_ref_in_channel(int copyID)
3672{
Bram Moolenaar77073442016-02-13 23:23:53 +01003673 int abort = FALSE;
3674 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003675 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003676
Bram Moolenaar77073442016-02-13 23:23:53 +01003677 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003678 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003679 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003680 tv.v_type = VAR_CHANNEL;
3681 tv.vval.v_channel = channel;
3682 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003683 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003684 return abort;
3685}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003686
3687/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003688 * Return the "part" to write to for "channel".
3689 */
3690 int
3691channel_part_send(channel_T *channel)
3692{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003693 if (channel->CH_SOCK_FD == INVALID_FD)
3694 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003695 return PART_SOCK;
3696}
3697
3698/*
3699 * Return the default "part" to read from for "channel".
3700 */
3701 int
3702channel_part_read(channel_T *channel)
3703{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003704 if (channel->CH_SOCK_FD == INVALID_FD)
3705 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003706 return PART_SOCK;
3707}
3708
3709/*
3710 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003711 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003712 */
3713 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003714channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003715{
Bram Moolenaar77073442016-02-13 23:23:53 +01003716 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003717 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003718 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003719}
3720
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003721/*
3722 * Return the timeout of "channel"/"part"
3723 */
3724 int
3725channel_get_timeout(channel_T *channel, int part)
3726{
3727 return channel->ch_part[part].ch_timeout;
3728}
3729
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003730 static int
3731handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3732{
3733 char_u *val = get_tv_string(item);
3734
3735 opt->jo_set |= jo;
3736 if (STRCMP(val, "nl") == 0)
3737 *modep = MODE_NL;
3738 else if (STRCMP(val, "raw") == 0)
3739 *modep = MODE_RAW;
3740 else if (STRCMP(val, "js") == 0)
3741 *modep = MODE_JS;
3742 else if (STRCMP(val, "json") == 0)
3743 *modep = MODE_JSON;
3744 else
3745 {
3746 EMSG2(_(e_invarg2), val);
3747 return FAIL;
3748 }
3749 return OK;
3750}
3751
3752 static int
3753handle_io(typval_T *item, int part, jobopt_T *opt)
3754{
3755 char_u *val = get_tv_string(item);
3756
3757 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3758 if (STRCMP(val, "null") == 0)
3759 opt->jo_io[part] = JIO_NULL;
3760 else if (STRCMP(val, "pipe") == 0)
3761 opt->jo_io[part] = JIO_PIPE;
3762 else if (STRCMP(val, "file") == 0)
3763 opt->jo_io[part] = JIO_FILE;
3764 else if (STRCMP(val, "buffer") == 0)
3765 opt->jo_io[part] = JIO_BUFFER;
3766 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3767 opt->jo_io[part] = JIO_OUT;
3768 else
3769 {
3770 EMSG2(_(e_invarg2), val);
3771 return FAIL;
3772 }
3773 return OK;
3774}
3775
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003776/*
3777 * Clear a jobopt_T before using it.
3778 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003779 void
3780clear_job_options(jobopt_T *opt)
3781{
3782 vim_memset(opt, 0, sizeof(jobopt_T));
3783}
3784
3785/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003786 * Free any members of a jobopt_T.
3787 */
3788 void
3789free_job_options(jobopt_T *opt)
3790{
3791 if (opt->jo_partial != NULL)
3792 partial_unref(opt->jo_partial);
3793 if (opt->jo_out_partial != NULL)
3794 partial_unref(opt->jo_out_partial);
3795 if (opt->jo_err_partial != NULL)
3796 partial_unref(opt->jo_err_partial);
3797 if (opt->jo_close_partial != NULL)
3798 partial_unref(opt->jo_close_partial);
3799}
3800
3801/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003802 * Get the PART_ number from the first character of an option name.
3803 */
3804 static int
3805part_from_char(int c)
3806{
3807 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3808}
3809
3810/*
3811 * Get the option entries from the dict in "tv", parse them and put the result
3812 * in "opt".
3813 * Only accept options in "supported".
3814 * If an option value is invalid return FAIL.
3815 */
3816 int
3817get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3818{
3819 typval_T *item;
3820 char_u *val;
3821 dict_T *dict;
3822 int todo;
3823 hashitem_T *hi;
3824 int part;
3825
3826 opt->jo_set = 0;
3827 if (tv->v_type == VAR_UNKNOWN)
3828 return OK;
3829 if (tv->v_type != VAR_DICT)
3830 {
3831 EMSG(_(e_invarg));
3832 return FAIL;
3833 }
3834 dict = tv->vval.v_dict;
3835 if (dict == NULL)
3836 return OK;
3837
3838 todo = (int)dict->dv_hashtab.ht_used;
3839 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3840 if (!HASHITEM_EMPTY(hi))
3841 {
3842 item = &dict_lookup(hi)->di_tv;
3843
3844 if (STRCMP(hi->hi_key, "mode") == 0)
3845 {
3846 if (!(supported & JO_MODE))
3847 break;
3848 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3849 return FAIL;
3850 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003851 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003852 {
3853 if (!(supported & JO_IN_MODE))
3854 break;
3855 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3856 == FAIL)
3857 return FAIL;
3858 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003859 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003860 {
3861 if (!(supported & JO_OUT_MODE))
3862 break;
3863 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3864 == FAIL)
3865 return FAIL;
3866 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003867 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003868 {
3869 if (!(supported & JO_ERR_MODE))
3870 break;
3871 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3872 == FAIL)
3873 return FAIL;
3874 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003875 else if (STRCMP(hi->hi_key, "in_io") == 0
3876 || STRCMP(hi->hi_key, "out_io") == 0
3877 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003878 {
3879 if (!(supported & JO_OUT_IO))
3880 break;
3881 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3882 return FAIL;
3883 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003884 else if (STRCMP(hi->hi_key, "in_name") == 0
3885 || STRCMP(hi->hi_key, "out_name") == 0
3886 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003887 {
3888 part = part_from_char(*hi->hi_key);
3889
3890 if (!(supported & JO_OUT_IO))
3891 break;
3892 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3893 opt->jo_io_name[part] =
3894 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3895 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003896 else if (STRCMP(hi->hi_key, "in_buf") == 0
3897 || STRCMP(hi->hi_key, "out_buf") == 0
3898 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003899 {
3900 part = part_from_char(*hi->hi_key);
3901
3902 if (!(supported & JO_OUT_IO))
3903 break;
3904 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3905 opt->jo_io_buf[part] = get_tv_number(item);
3906 if (opt->jo_io_buf[part] <= 0)
3907 {
3908 EMSG2(_(e_invarg2), get_tv_string(item));
3909 return FAIL;
3910 }
3911 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3912 {
3913 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3914 return FAIL;
3915 }
3916 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003917 else if (STRCMP(hi->hi_key, "in_top") == 0
3918 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003919 {
3920 linenr_T *lp;
3921
3922 if (!(supported & JO_OUT_IO))
3923 break;
3924 if (hi->hi_key[3] == 't')
3925 {
3926 lp = &opt->jo_in_top;
3927 opt->jo_set |= JO_IN_TOP;
3928 }
3929 else
3930 {
3931 lp = &opt->jo_in_bot;
3932 opt->jo_set |= JO_IN_BOT;
3933 }
3934 *lp = get_tv_number(item);
3935 if (*lp < 0)
3936 {
3937 EMSG2(_(e_invarg2), get_tv_string(item));
3938 return FAIL;
3939 }
3940 }
3941 else if (STRCMP(hi->hi_key, "channel") == 0)
3942 {
3943 if (!(supported & JO_OUT_IO))
3944 break;
3945 opt->jo_set |= JO_CHANNEL;
3946 if (item->v_type != VAR_CHANNEL)
3947 {
3948 EMSG2(_(e_invarg2), "channel");
3949 return FAIL;
3950 }
3951 opt->jo_channel = item->vval.v_channel;
3952 }
3953 else if (STRCMP(hi->hi_key, "callback") == 0)
3954 {
3955 if (!(supported & JO_CALLBACK))
3956 break;
3957 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003958 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003959 if (opt->jo_callback == NULL)
3960 {
3961 EMSG2(_(e_invarg2), "callback");
3962 return FAIL;
3963 }
3964 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003965 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003966 {
3967 if (!(supported & JO_OUT_CALLBACK))
3968 break;
3969 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003970 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003971 if (opt->jo_out_cb == NULL)
3972 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003973 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003974 return FAIL;
3975 }
3976 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003977 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003978 {
3979 if (!(supported & JO_ERR_CALLBACK))
3980 break;
3981 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003982 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003983 if (opt->jo_err_cb == NULL)
3984 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003985 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003986 return FAIL;
3987 }
3988 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003989 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003990 {
3991 if (!(supported & JO_CLOSE_CALLBACK))
3992 break;
3993 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003994 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003995 if (opt->jo_close_cb == NULL)
3996 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003997 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003998 return FAIL;
3999 }
4000 }
4001 else if (STRCMP(hi->hi_key, "waittime") == 0)
4002 {
4003 if (!(supported & JO_WAITTIME))
4004 break;
4005 opt->jo_set |= JO_WAITTIME;
4006 opt->jo_waittime = get_tv_number(item);
4007 }
4008 else if (STRCMP(hi->hi_key, "timeout") == 0)
4009 {
4010 if (!(supported & JO_TIMEOUT))
4011 break;
4012 opt->jo_set |= JO_TIMEOUT;
4013 opt->jo_timeout = get_tv_number(item);
4014 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004015 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004016 {
4017 if (!(supported & JO_OUT_TIMEOUT))
4018 break;
4019 opt->jo_set |= JO_OUT_TIMEOUT;
4020 opt->jo_out_timeout = get_tv_number(item);
4021 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004022 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004023 {
4024 if (!(supported & JO_ERR_TIMEOUT))
4025 break;
4026 opt->jo_set |= JO_ERR_TIMEOUT;
4027 opt->jo_err_timeout = get_tv_number(item);
4028 }
4029 else if (STRCMP(hi->hi_key, "part") == 0)
4030 {
4031 if (!(supported & JO_PART))
4032 break;
4033 opt->jo_set |= JO_PART;
4034 val = get_tv_string(item);
4035 if (STRCMP(val, "err") == 0)
4036 opt->jo_part = PART_ERR;
4037 else
4038 {
4039 EMSG2(_(e_invarg2), val);
4040 return FAIL;
4041 }
4042 }
4043 else if (STRCMP(hi->hi_key, "id") == 0)
4044 {
4045 if (!(supported & JO_ID))
4046 break;
4047 opt->jo_set |= JO_ID;
4048 opt->jo_id = get_tv_number(item);
4049 }
4050 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4051 {
4052 if (!(supported & JO_STOPONEXIT))
4053 break;
4054 opt->jo_set |= JO_STOPONEXIT;
4055 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4056 opt->jo_soe_buf);
4057 if (opt->jo_stoponexit == NULL)
4058 {
4059 EMSG2(_(e_invarg2), "stoponexit");
4060 return FAIL;
4061 }
4062 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004063 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004064 {
4065 if (!(supported & JO_EXIT_CB))
4066 break;
4067 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004068 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
4069 {
4070 opt->jo_exit_partial = item->vval.v_partial;
4071 opt->jo_exit_cb = item->vval.v_partial->pt_name;
4072 }
4073 else
4074 opt->jo_exit_cb = get_tv_string_buf_chk(
4075 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004076 if (opt->jo_exit_cb == NULL)
4077 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004078 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004079 return FAIL;
4080 }
4081 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004082 else if (STRCMP(hi->hi_key, "block_write") == 0)
4083 {
4084 if (!(supported & JO_BLOCK_WRITE))
4085 break;
4086 opt->jo_set |= JO_BLOCK_WRITE;
4087 opt->jo_block_write = get_tv_number(item);
4088 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004089 else
4090 break;
4091 --todo;
4092 }
4093 if (todo > 0)
4094 {
4095 EMSG2(_(e_invarg2), hi->hi_key);
4096 return FAIL;
4097 }
4098
4099 return OK;
4100}
4101
4102/*
4103 * Get the channel from the argument.
4104 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004105 * When "check_open" is TRUE check that the channel can be used.
4106 * When "reading" is TRUE "check_open" considers typeahead useful.
4107 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004108 */
4109 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004110get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004111{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004112 channel_T *channel = NULL;
4113 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004114
4115 if (tv->v_type == VAR_JOB)
4116 {
4117 if (tv->vval.v_job != NULL)
4118 channel = tv->vval.v_job->jv_channel;
4119 }
4120 else if (tv->v_type == VAR_CHANNEL)
4121 {
4122 channel = tv->vval.v_channel;
4123 }
4124 else
4125 {
4126 EMSG2(_(e_invarg2), get_tv_string(tv));
4127 return NULL;
4128 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004129 if (channel != NULL && reading)
4130 has_readahead = channel_has_readahead(channel,
4131 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004132
Bram Moolenaar437905c2016-04-26 19:01:05 +02004133 if (check_open && (channel == NULL || (!channel_is_open(channel)
4134 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004135 {
4136 EMSG(_("E906: not an open channel"));
4137 return NULL;
4138 }
4139 return channel;
4140}
4141
4142static job_T *first_job = NULL;
4143
4144 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004145job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004146{
4147 ch_log(job->jv_channel, "Freeing job");
4148 if (job->jv_channel != NULL)
4149 {
4150 /* The link from the channel to the job doesn't count as a reference,
4151 * thus don't decrement the refcount of the job. The reference from
4152 * the job to the channel does count the refrence, decrement it and
4153 * NULL the reference. We don't set ch_job_killed, unreferencing the
4154 * job doesn't mean it stops running. */
4155 job->jv_channel->ch_job = NULL;
4156 channel_unref(job->jv_channel);
4157 }
4158 mch_clear_job(job);
4159
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004160 vim_free(job->jv_stoponexit);
4161 vim_free(job->jv_exit_cb);
4162 partial_unref(job->jv_exit_partial);
4163}
4164
4165 static void
4166job_free_job(job_T *job)
4167{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004168 if (job->jv_next != NULL)
4169 job->jv_next->jv_prev = job->jv_prev;
4170 if (job->jv_prev == NULL)
4171 first_job = job->jv_next;
4172 else
4173 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004174 vim_free(job);
4175}
4176
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004177 static void
4178job_free(job_T *job)
4179{
4180 if (!in_free_unref_items)
4181 {
4182 job_free_contents(job);
4183 job_free_job(job);
4184 }
4185}
4186
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004187/*
4188 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004189 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4190 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004191 */
4192 static int
4193job_still_useful(job_T *job)
4194{
4195 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004196 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4197 || (job->jv_channel != NULL
4198 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004199}
4200
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004201/*
4202 * Mark references in jobs that are still useful.
4203 */
4204 int
4205set_ref_in_job(int copyID)
4206{
4207 int abort = FALSE;
4208 job_T *job;
4209 typval_T tv;
4210
4211 for (job = first_job; job != NULL; job = job->jv_next)
4212 if (job_still_useful(job))
4213 {
4214 tv.v_type = VAR_JOB;
4215 tv.vval.v_job = job;
4216 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4217 }
4218 return abort;
4219}
4220
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004221 void
4222job_unref(job_T *job)
4223{
4224 if (job != NULL && --job->jv_refcount <= 0)
4225 {
4226 /* Do not free the job when it has not ended yet and there is a
4227 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004228 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004229 {
4230 job_free(job);
4231 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004232 else if (job->jv_channel != NULL
4233 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004234 {
4235 /* Do remove the link to the channel, otherwise it hangs
4236 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004237 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004238 job->jv_channel->ch_job = NULL;
4239 channel_unref(job->jv_channel);
4240 job->jv_channel = NULL;
4241 }
4242 }
4243}
4244
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004245 int
4246free_unused_jobs_contents(int copyID, int mask)
4247{
4248 int did_free = FALSE;
4249 job_T *job;
4250
4251 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004252 if ((job->jv_copyID & mask) != (copyID & mask)
4253 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004254 {
4255 /* Free the channel and ordinary items it contains, but don't
4256 * recurse into Lists, Dictionaries etc. */
4257 job_free_contents(job);
4258 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004259 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004260 return did_free;
4261}
4262
4263 void
4264free_unused_jobs(int copyID, int mask)
4265{
4266 job_T *job;
4267 job_T *job_next;
4268
4269 for (job = first_job; job != NULL; job = job_next)
4270 {
4271 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004272 if ((job->jv_copyID & mask) != (copyID & mask)
4273 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004274 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004275 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004276 job_free_job(job);
4277 }
4278 }
4279}
4280
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004281/*
4282 * Allocate a job. Sets the refcount to one and sets options default.
4283 */
4284 static job_T *
4285job_alloc(void)
4286{
4287 job_T *job;
4288
4289 job = (job_T *)alloc_clear(sizeof(job_T));
4290 if (job != NULL)
4291 {
4292 job->jv_refcount = 1;
4293 job->jv_stoponexit = vim_strsave((char_u *)"term");
4294
4295 if (first_job != NULL)
4296 {
4297 first_job->jv_prev = job;
4298 job->jv_next = first_job;
4299 }
4300 first_job = job;
4301 }
4302 return job;
4303}
4304
4305 void
4306job_set_options(job_T *job, jobopt_T *opt)
4307{
4308 if (opt->jo_set & JO_STOPONEXIT)
4309 {
4310 vim_free(job->jv_stoponexit);
4311 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4312 job->jv_stoponexit = NULL;
4313 else
4314 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4315 }
4316 if (opt->jo_set & JO_EXIT_CB)
4317 {
4318 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004319 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004320 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004321 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004322 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004323 job->jv_exit_partial = NULL;
4324 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004325 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004326 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004327 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004328 job->jv_exit_partial = opt->jo_exit_partial;
4329 if (job->jv_exit_partial != NULL)
4330 ++job->jv_exit_partial->pt_refcount;
4331 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004332 }
4333}
4334
4335/*
4336 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4337 */
4338 void
4339job_stop_on_exit()
4340{
4341 job_T *job;
4342
4343 for (job = first_job; job != NULL; job = job->jv_next)
4344 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4345 mch_stop_job(job, job->jv_stoponexit);
4346}
4347
4348/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004349 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004350 */
4351 void
4352job_check_ended(void)
4353{
4354 static time_t last_check = 0;
4355 time_t now;
4356 job_T *job;
4357 job_T *next;
4358
4359 /* Only do this once in 10 seconds. */
4360 now = time(NULL);
4361 if (last_check + 10 < now)
4362 {
4363 last_check = now;
4364 for (job = first_job; job != NULL; job = next)
4365 {
4366 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004367 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004368 job_status(job); /* may free "job" */
4369 }
4370 }
4371}
4372
4373/*
4374 * "job_start()" function
4375 */
4376 job_T *
4377job_start(typval_T *argvars)
4378{
4379 job_T *job;
4380 char_u *cmd = NULL;
4381#if defined(UNIX)
4382# define USE_ARGV
4383 char **argv = NULL;
4384 int argc = 0;
4385#else
4386 garray_T ga;
4387#endif
4388 jobopt_T opt;
4389 int part;
4390
4391 job = job_alloc();
4392 if (job == NULL)
4393 return NULL;
4394
4395 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004396#ifndef USE_ARGV
4397 ga_init2(&ga, (int)sizeof(char*), 20);
4398#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004399
4400 /* Default mode is NL. */
4401 clear_job_options(&opt);
4402 opt.jo_mode = MODE_NL;
4403 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004404 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4405 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004406 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004407
4408 /* Check that when io is "file" that there is a file name. */
4409 for (part = PART_OUT; part <= PART_IN; ++part)
4410 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4411 && opt.jo_io[part] == JIO_FILE
4412 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4413 || *opt.jo_io_name[part] == NUL))
4414 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004415 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004416 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004417 }
4418
4419 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4420 {
4421 buf_T *buf = NULL;
4422
4423 /* check that we can find the buffer before starting the job */
4424 if (opt.jo_set & JO_IN_BUF)
4425 {
4426 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4427 if (buf == NULL)
4428 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4429 }
4430 else if (!(opt.jo_set & JO_IN_NAME))
4431 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004432 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004433 }
4434 else
4435 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4436 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004437 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004438 if (buf->b_ml.ml_mfp == NULL)
4439 {
4440 char_u numbuf[NUMBUFLEN];
4441 char_u *s;
4442
4443 if (opt.jo_set & JO_IN_BUF)
4444 {
4445 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4446 s = numbuf;
4447 }
4448 else
4449 s = opt.jo_io_name[PART_IN];
4450 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004451 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004452 }
4453 job->jv_in_buf = buf;
4454 }
4455
4456 job_set_options(job, &opt);
4457
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004458 if (argvars[0].v_type == VAR_STRING)
4459 {
4460 /* Command is a string. */
4461 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004462 if (cmd == NULL || *cmd == NUL)
4463 {
4464 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004465 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004466 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004467#ifdef USE_ARGV
4468 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004469 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004470 argv[argc] = NULL;
4471#endif
4472 }
4473 else if (argvars[0].v_type != VAR_LIST
4474 || argvars[0].vval.v_list == NULL
4475 || argvars[0].vval.v_list->lv_len < 1)
4476 {
4477 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004478 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004479 }
4480 else
4481 {
4482 list_T *l = argvars[0].vval.v_list;
4483 listitem_T *li;
4484 char_u *s;
4485
4486#ifdef USE_ARGV
4487 /* Pass argv[] to mch_call_shell(). */
4488 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4489 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004490 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004491#endif
4492 for (li = l->lv_first; li != NULL; li = li->li_next)
4493 {
4494 s = get_tv_string_chk(&li->li_tv);
4495 if (s == NULL)
4496 goto theend;
4497#ifdef USE_ARGV
4498 argv[argc++] = (char *)s;
4499#else
4500 /* Only escape when needed, double quotes are not always allowed. */
4501 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4502 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004503# ifdef WIN32
4504 int old_ssl = p_ssl;
4505
4506 /* This is using CreateProcess, not cmd.exe. Always use
4507 * double quote and backslashes. */
4508 p_ssl = 0;
4509# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004510 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004511# ifdef WIN32
4512 p_ssl = old_ssl;
4513# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004514 if (s == NULL)
4515 goto theend;
4516 ga_concat(&ga, s);
4517 vim_free(s);
4518 }
4519 else
4520 ga_concat(&ga, s);
4521 if (li->li_next != NULL)
4522 ga_append(&ga, ' ');
4523#endif
4524 }
4525#ifdef USE_ARGV
4526 argv[argc] = NULL;
4527#else
4528 cmd = ga.ga_data;
4529#endif
4530 }
4531
4532#ifdef USE_ARGV
4533 if (ch_log_active())
4534 {
4535 garray_T ga;
4536 int i;
4537
4538 ga_init2(&ga, (int)sizeof(char), 200);
4539 for (i = 0; i < argc; ++i)
4540 {
4541 if (i > 0)
4542 ga_concat(&ga, (char_u *)" ");
4543 ga_concat(&ga, (char_u *)argv[i]);
4544 }
4545 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4546 ga_clear(&ga);
4547 }
4548 mch_start_job(argv, job, &opt);
4549#else
4550 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4551 mch_start_job((char *)cmd, job, &opt);
4552#endif
4553
4554 /* If the channel is reading from a buffer, write lines now. */
4555 if (job->jv_channel != NULL)
4556 channel_write_in(job->jv_channel);
4557
4558theend:
4559#ifdef USE_ARGV
4560 vim_free(argv);
4561#else
4562 vim_free(ga.ga_data);
4563#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004564 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004565 return job;
4566}
4567
4568/*
4569 * Get the status of "job" and invoke the exit callback when needed.
4570 * The returned string is not allocated.
4571 */
4572 char *
4573job_status(job_T *job)
4574{
4575 char *result;
4576
4577 if (job->jv_status == JOB_ENDED)
4578 /* No need to check, dead is dead. */
4579 result = "dead";
4580 else if (job->jv_status == JOB_FAILED)
4581 result = "fail";
4582 else
4583 {
4584 result = mch_job_status(job);
4585 if (job->jv_status == JOB_ENDED)
4586 ch_log(job->jv_channel, "Job ended");
4587 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4588 {
4589 typval_T argv[3];
4590 typval_T rettv;
4591 int dummy;
4592
4593 /* invoke the exit callback; make sure the refcount is > 0 */
4594 ++job->jv_refcount;
4595 argv[0].v_type = VAR_JOB;
4596 argv[0].vval.v_job = job;
4597 argv[1].v_type = VAR_NUMBER;
4598 argv[1].vval.v_number = job->jv_exitval;
4599 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004600 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4601 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004602 clear_tv(&rettv);
4603 --job->jv_refcount;
4604 }
4605 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4606 {
4607 /* The job was already unreferenced, now that it ended it can be
4608 * freed. Careful: caller must not use "job" after this! */
4609 job_free(job);
4610 }
4611 }
4612 return result;
4613}
4614
Bram Moolenaar8950a562016-03-12 15:22:55 +01004615/*
4616 * Implementation of job_info().
4617 */
4618 void
4619job_info(job_T *job, dict_T *dict)
4620{
4621 dictitem_T *item;
4622 varnumber_T nr;
4623
4624 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4625
4626 item = dictitem_alloc((char_u *)"channel");
4627 if (item == NULL)
4628 return;
4629 item->di_tv.v_lock = 0;
4630 item->di_tv.v_type = VAR_CHANNEL;
4631 item->di_tv.vval.v_channel = job->jv_channel;
4632 if (job->jv_channel != NULL)
4633 ++job->jv_channel->ch_refcount;
4634 if (dict_add(dict, item) == FAIL)
4635 dictitem_free(item);
4636
4637#ifdef UNIX
4638 nr = job->jv_pid;
4639#else
4640 nr = job->jv_proc_info.dwProcessId;
4641#endif
4642 dict_add_nr_str(dict, "process", nr, NULL);
4643
4644 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004645 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004646 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4647}
4648
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004649 int
4650job_stop(job_T *job, typval_T *argvars)
4651{
4652 char_u *arg;
4653
4654 if (argvars[1].v_type == VAR_UNKNOWN)
4655 arg = (char_u *)"";
4656 else
4657 {
4658 arg = get_tv_string_chk(&argvars[1]);
4659 if (arg == NULL)
4660 {
4661 EMSG(_(e_invarg));
4662 return 0;
4663 }
4664 }
4665 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4666 if (mch_stop_job(job, arg) == FAIL)
4667 return 0;
4668
4669 /* Assume that "hup" does not kill the job. */
4670 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4671 job->jv_channel->ch_job_killed = TRUE;
4672
4673 /* We don't try freeing the job, obviously the caller still has a
4674 * reference to it. */
4675 return 1;
4676}
4677
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004678#endif /* FEAT_JOB_CHANNEL */