blob: 84dfcf7c0b7a17a21700cfb51a463460e18b03b5 [file] [log] [blame]
Bram Moolenaare0874f82016-01-24 20:36:41 +01001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
22/* Note: when making changes here also adjust configure.in. */
23#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaarb2658a12016-04-26 17:16:24 +020057static void channel_read(channel_T *channel, int part, char *func);
58
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaard8070362016-02-15 21:56:54 +010068#ifdef WIN32
69 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
83 HANDLE h = (HANDLE)fd;
84 DWORD nwrite;
85
86 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
87 return -1;
88 return (int)nwrite;
89}
90
91 static void
92fd_close(sock_T fd)
93{
94 HANDLE h = (HANDLE)fd;
95
96 CloseHandle(h);
97}
98#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010099
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100/* Log file opened with ch_logfile(). */
101static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100102#ifdef FEAT_RELTIME
103static proftime_T log_start;
104#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100105
106 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100107ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100108{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100109 FILE *file = NULL;
110
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100111 if (log_fd != NULL)
112 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100113
114 if (*fname != NUL)
115 {
116 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
117 if (file == NULL)
118 {
119 EMSG2(_(e_notopen), fname);
120 return;
121 }
122 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100124
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100125 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100126 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100128#ifdef FEAT_RELTIME
129 profile_start(&log_start);
130#endif
131 }
132}
133
134 int
135ch_log_active()
136{
137 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138}
139
140 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100141ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100142{
143 if (log_fd != NULL)
144 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100145#ifdef FEAT_RELTIME
146 proftime_T log_now;
147
148 profile_start(&log_now);
149 profile_sub(&log_now, &log_start);
150 fprintf(log_fd, "%s ", profile_msg(&log_now));
151#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100152 if (ch != NULL)
153 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 else
155 fprintf(log_fd, "%s: ", what);
156 }
157}
158
Bram Moolenaard0b65022016-03-06 21:50:33 +0100159static int did_log_msg = TRUE;
160
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100162ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100166 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100168 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100170 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171 }
172}
173
174 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100175ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100176{
177 if (log_fd != NULL)
178 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100181 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100183 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184 }
185}
186
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100187 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100188ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100189{
190 if (log_fd != NULL)
191 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100192 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100193 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100194 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100196 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197 }
198}
199
200 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100201ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100202{
203 if (log_fd != NULL)
204 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100205 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100206 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100207 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100209 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210 }
211}
212
213 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100214ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100215{
216 if (log_fd != NULL)
217 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100218 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100220 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100222 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223 }
224}
225
226 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100227ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100228{
229 if (log_fd != NULL)
230 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100233 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100235 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236 }
237}
238
239 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100240ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100241{
242 if (log_fd != NULL)
243 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100244 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100245 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100246 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100247 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100248 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100249 }
250}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100251
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252#ifdef _WIN32
253# undef PERROR
254# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
255 (char_u *)msg, (char_u *)strerror_win32(errno))
256
257 static char *
258strerror_win32(int eno)
259{
260 static LPVOID msgbuf = NULL;
261 char_u *ptr;
262
263 if (msgbuf)
264 LocalFree(msgbuf);
265 FormatMessage(
266 FORMAT_MESSAGE_ALLOCATE_BUFFER |
267 FORMAT_MESSAGE_FROM_SYSTEM |
268 FORMAT_MESSAGE_IGNORE_INSERTS,
269 NULL,
270 eno,
271 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
272 (LPTSTR) &msgbuf,
273 0,
274 NULL);
275 /* chomp \r or \n */
276 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
277 switch (*ptr)
278 {
279 case '\r':
280 STRMOVE(ptr, ptr + 1);
281 ptr--;
282 break;
283 case '\n':
284 if (*(ptr + 1) == '\0')
285 *ptr = '\0';
286 else
287 *ptr = ' ';
288 break;
289 }
290 return msgbuf;
291}
292#endif
293
Bram Moolenaar77073442016-02-13 23:23:53 +0100294/*
295 * The list of all allocated channels.
296 */
297static channel_T *first_channel = NULL;
298static int next_ch_id = 0;
299
300/*
301 * Allocate a new channel. The refcount is set to 1.
302 * The channel isn't actually used until it is opened.
303 * Returns NULL if out of memory.
304 */
305 channel_T *
306add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100307{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100308 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100309 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100310
Bram Moolenaar77073442016-02-13 23:23:53 +0100311 if (channel == NULL)
312 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313
Bram Moolenaar77073442016-02-13 23:23:53 +0100314 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100315 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100316
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100317 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100318 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100320#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100321 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100322#endif
323#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100324 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100325#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100326 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100327 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100328
Bram Moolenaar77073442016-02-13 23:23:53 +0100329 if (first_channel != NULL)
330 {
331 first_channel->ch_prev = channel;
332 channel->ch_next = first_channel;
333 }
334 first_channel = channel;
335
336 channel->ch_refcount = 1;
337 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100338}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100339
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100340/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100341 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100342 * Return TRUE if "channel" has a callback and the associated job wasn't
343 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100344 */
345 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100346channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100347{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100348 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100349 int has_out_msg;
350 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100351
352 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100353 if (channel->ch_job_killed && channel->ch_job == NULL)
354 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100355
356 /* If there is a close callback it may still need to be invoked. */
357 if (channel->ch_close_cb != NULL)
358 return TRUE;
359
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200360 /* If reading from or a buffer it's still useful. */
361 if (channel->ch_part[PART_IN].ch_buffer != NULL)
362 return TRUE;
363
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100364 /* If there is no callback then nobody can get readahead. If the fd is
365 * closed and there is no readahead then the callback won't be called. */
366 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
367 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
368 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100369 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
370 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
371 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
372 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
373 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
374 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100375 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100376 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200377 || ((channel->ch_part[PART_OUT].ch_callback != NULL
378 || channel->ch_part[PART_OUT].ch_buffer) && has_out_msg)
379 || ((channel->ch_part[PART_ERR].ch_callback != NULL
380 || channel->ch_part[PART_ERR].ch_buffer) && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100381}
382
383/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200384 * Close a channel and free all its resources.
385 */
386 static void
387channel_free_contents(channel_T *channel)
388{
389 channel_close(channel, TRUE);
390 channel_clear(channel);
391 ch_log(channel, "Freeing channel");
392}
393
394 static void
395channel_free_channel(channel_T *channel)
396{
397 if (channel->ch_next != NULL)
398 channel->ch_next->ch_prev = channel->ch_prev;
399 if (channel->ch_prev == NULL)
400 first_channel = channel->ch_next;
401 else
402 channel->ch_prev->ch_next = channel->ch_next;
403 vim_free(channel);
404}
405
406 static void
407channel_free(channel_T *channel)
408{
409 if (!in_free_unref_items)
410 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200411 if (safe_to_invoke_callback == 0)
412 {
413 channel->ch_to_be_freed = TRUE;
414 }
415 else
416 {
417 channel_free_contents(channel);
418 channel_free_channel(channel);
419 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200420 }
421}
422
423/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100424 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100425 * possible, there is no callback to be invoked or the associated job was
426 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100427 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100428 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100429 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100430channel_may_free(channel_T *channel)
431{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100432 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100433 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100435 return TRUE;
436 }
437 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100438}
439
440/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100441 * Decrement the reference count on "channel" and maybe free it when it goes
442 * down to zero. Don't free it if there is a pending action.
443 * Returns TRUE when the channel is no longer referenced.
444 */
445 int
446channel_unref(channel_T *channel)
447{
448 if (channel != NULL && --channel->ch_refcount <= 0)
449 return channel_may_free(channel);
450 return FALSE;
451}
452
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200453 int
454free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100455{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200456 int did_free = FALSE;
457 channel_T *ch;
458
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200459 /* This is invoked from the garbage collector, which only runs at a safe
460 * point. */
461 ++safe_to_invoke_callback;
462
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200463 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200464 if (!channel_still_useful(ch)
465 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 {
467 /* Free the channel and ordinary items it contains, but don't
468 * recurse into Lists, Dictionaries etc. */
469 channel_free_contents(ch);
470 did_free = TRUE;
471 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200472
473 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200474 return did_free;
475}
476
477 void
478free_unused_channels(int copyID, int mask)
479{
480 channel_T *ch;
481 channel_T *ch_next;
482
483 for (ch = first_channel; ch != NULL; ch = ch_next)
484 {
485 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200486 if (!channel_still_useful(ch)
487 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200488 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200489 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200490 channel_free_channel(ch);
491 }
492 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100493}
494
Bram Moolenaard04a0202016-01-26 23:30:18 +0100495#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100496
497#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
498 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100499channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100500{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100501 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100502 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100503
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100504 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100505 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100506 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100507 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200508 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100509}
510#endif
511
Bram Moolenaare0874f82016-01-24 20:36:41 +0100512/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100514 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100515#ifdef FEAT_GUI_X11
516 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200517messageFromServer(XtPointer clientData,
518 int *unused1 UNUSED,
519 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100520{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100521 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100523#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100524
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100526# if GTK_CHECK_VERSION(3,0,0)
527 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200528messageFromServer(GIOChannel *unused1 UNUSED,
529 GIOCondition unused2 UNUSED,
530 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100531{
532 channel_read_fd(GPOINTER_TO_INT(clientData));
533 return TRUE; /* Return FALSE instead in case the event source is to
534 * be removed after this function returns. */
535}
536# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100537 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200538messageFromServer(gpointer clientData,
539 gint unused1 UNUSED,
540 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100541{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100542 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100543}
Bram Moolenaar98921892016-02-23 17:14:37 +0100544# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100545#endif
546
547 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100548channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100549{
Bram Moolenaarde279892016-03-11 22:19:44 +0100550 if (!CH_HAS_GUI)
551 return;
552
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100553# ifdef FEAT_GUI_X11
554 /* Tell notifier we are interested in being called
555 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100556 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
557 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100558 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100559 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200561 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100562 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100563# else
564# ifdef FEAT_GUI_GTK
565 /* Tell gdk we are interested in being called when there
566 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100567 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100568# if GTK_CHECK_VERSION(3,0,0)
569 {
570 GIOChannel *chnnl = g_io_channel_unix_new(
571 (gint)channel->ch_part[part].ch_fd);
572
573 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
574 chnnl,
575 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200576 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100577 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
578
579 g_io_channel_unref(chnnl);
580 }
581# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100582 channel->ch_part[part].ch_inputHandler = gdk_input_add(
583 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100584 (GdkInputCondition)
585 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200586 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100587 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100588# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100589# endif
590# endif
591}
592
Bram Moolenaarde279892016-03-11 22:19:44 +0100593 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100594channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100595{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100596 if (channel->CH_SOCK_FD != INVALID_FD)
597 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100598 if (channel->CH_OUT_FD != INVALID_FD)
599 channel_gui_register_one(channel, PART_OUT);
600 if (channel->CH_ERR_FD != INVALID_FD)
601 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100602}
603
604/*
605 * Register any of our file descriptors with the GUI event handling system.
606 * Called when the GUI has started.
607 */
608 void
609channel_gui_register_all(void)
610{
Bram Moolenaar77073442016-02-13 23:23:53 +0100611 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100612
Bram Moolenaar77073442016-02-13 23:23:53 +0100613 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100614 channel_gui_register(channel);
615}
616
617 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100618channel_gui_unregister_one(channel_T *channel, int part)
619{
620# ifdef FEAT_GUI_X11
621 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
622 {
623 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
624 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
625 }
626# else
627# ifdef FEAT_GUI_GTK
628 if (channel->ch_part[part].ch_inputHandler != 0)
629 {
630# if GTK_CHECK_VERSION(3,0,0)
631 g_source_remove(channel->ch_part[part].ch_inputHandler);
632# else
633 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
634# endif
635 channel->ch_part[part].ch_inputHandler = 0;
636 }
637# endif
638# endif
639}
640
641 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100642channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100643{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100644 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100645
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100646 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100647 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100648}
649
650#endif
651
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100652static char *e_cannot_connect = N_("E902: Cannot connect to port");
653
Bram Moolenaard04a0202016-01-26 23:30:18 +0100654/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100655 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100656 * "waittime" is the time in msec to wait for the connection.
657 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100658 * Returns the channel for success.
659 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100660 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100661 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100662channel_open(
663 char *hostname,
664 int port_in,
665 int waittime,
666 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100667{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100668 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100669 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100670 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100671#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100672 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100673 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100674#else
675 int port = port_in;
676#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100677 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100678 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100680#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100681 channel_init_winsock();
682#endif
683
Bram Moolenaar77073442016-02-13 23:23:53 +0100684 channel = add_channel();
685 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100686 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100687 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100688 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100689 }
690
691 /* Get the server internet address and put into addr structure */
692 /* fill in the socket address structure and connect to server */
693 vim_memset((char *)&server, 0, sizeof(server));
694 server.sin_family = AF_INET;
695 server.sin_port = htons(port);
696 if ((host = gethostbyname(hostname)) == NULL)
697 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100698 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100699 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100700 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100701 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100702 }
703 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
704
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100705 /* On Mac and Solaris a zero timeout almost never works. At least wait
706 * one millisecond. Let's do it for all systems, because we don't know why
707 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708 if (waittime == 0)
709 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100710
711 /*
712 * For Unix we need to call connect() again after connect() failed.
713 * On Win32 one time is sufficient.
714 */
715 while (TRUE)
716 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100717 long elapsed_msec = 0;
718 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100719
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100721 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100722 sd = socket(AF_INET, SOCK_STREAM, 0);
723 if (sd == -1)
724 {
725 ch_error(channel, "in socket() in channel_open().");
726 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100727 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100728 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100729 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100730
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731 if (waittime >= 0)
732 {
733 /* Make connect() non-blocking. */
734 if (
735#ifdef _WIN32
736 ioctlsocket(sd, FIONBIO, &val) < 0
737#else
738 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
739#endif
740 )
741 {
742 SOCK_ERRNO;
743 ch_errorn(channel,
744 "channel_open: Connect failed with errno %d", errno);
745 sock_close(sd);
746 channel_free(channel);
747 return NULL;
748 }
749 }
750
751 /* Try connecting to the server. */
752 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
753 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
754
Bram Moolenaar045a2842016-03-08 22:33:07 +0100755 if (ret == 0)
756 /* The connection could be established. */
757 break;
758
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100759 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100760 if (waittime < 0 || (errno != EWOULDBLOCK
761 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100762#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100763 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100764#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100765 ))
766 {
767 ch_errorn(channel,
768 "channel_open: Connect failed with errno %d", errno);
769 PERROR(_(e_cannot_connect));
770 sock_close(sd);
771 channel_free(channel);
772 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100773 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100774
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100775 /* Limit the waittime to 50 msec. If it doesn't work within this
776 * time we close the socket and try creating it again. */
777 waitnow = waittime > 50 ? 50 : waittime;
778
Bram Moolenaar045a2842016-03-08 22:33:07 +0100779 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100780 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100781#ifndef WIN32
782 if (errno != ECONNREFUSED)
783#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100784 {
785 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100786 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100787 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100788#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100789 int so_error = 0;
790 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100791 struct timeval start_tv;
792 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100793#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100794 FD_ZERO(&rfds);
795 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100796 FD_ZERO(&wfds);
797 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100798
Bram Moolenaar562ca712016-03-09 21:50:05 +0100799 tv.tv_sec = waitnow / 1000;
800 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100801#ifndef WIN32
802 gettimeofday(&start_tv, NULL);
803#endif
804 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100805 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100807
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 if (ret < 0)
809 {
810 SOCK_ERRNO;
811 ch_errorn(channel,
812 "channel_open: Connect failed with errno %d", errno);
813 PERROR(_(e_cannot_connect));
814 sock_close(sd);
815 channel_free(channel);
816 return NULL;
817 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818
Bram Moolenaare081e212016-02-28 22:33:46 +0100819#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100820 /* On Win32: select() is expected to work and wait for up to
821 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100822 if (FD_ISSET(sd, &wfds))
823 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100824 elapsed_msec = waitnow;
825 if (waittime > 1 && elapsed_msec < waittime)
826 {
827 waittime -= elapsed_msec;
828 continue;
829 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100830#else
831 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100832 * After putting the socket in non-blocking mode, connect() will
833 * return EINPROGRESS, select() will not wait (as if writing is
834 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100835 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100837 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100838 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100839 {
840 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100841 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100842 if (ret < 0 || (so_error != 0
843 && so_error != EWOULDBLOCK
844 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100845# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100846 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100847# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100848 ))
849 {
850 ch_errorn(channel,
851 "channel_open: Connect failed with errno %d",
852 so_error);
853 PERROR(_(e_cannot_connect));
854 sock_close(sd);
855 channel_free(channel);
856 return NULL;
857 }
858 }
859
Bram Moolenaar045a2842016-03-08 22:33:07 +0100860 if (FD_ISSET(sd, &wfds) && so_error == 0)
861 /* Did not detect an error, connection is established. */
862 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100863
Bram Moolenaar045a2842016-03-08 22:33:07 +0100864 gettimeofday(&end_tv, NULL);
865 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
866 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100867#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100868 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100869
870#ifndef WIN32
871 if (waittime > 1 && elapsed_msec < waittime)
872 {
873 /* The port isn't ready but we also didn't get an error.
874 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100875 * yet. Select() may return early, wait until the remaining
876 * "waitnow" and try again. */
877 waitnow -= elapsed_msec;
878 waittime -= elapsed_msec;
879 if (waitnow > 0)
880 {
881 mch_delay((long)waitnow, TRUE);
882 ui_breakcheck();
883 waittime -= waitnow;
884 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100885 if (!got_int)
886 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100887 if (waittime <= 0)
888 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100889 waittime = 1;
890 continue;
891 }
892 /* we were interrupted, behave as if timed out */
893 }
894#endif
895
896 /* We timed out. */
897 ch_error(channel, "Connection timed out");
898 sock_close(sd);
899 channel_free(channel);
900 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100901 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100902
Bram Moolenaar045a2842016-03-08 22:33:07 +0100903 ch_log(channel, "Connection made");
904
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100905 if (waittime >= 0)
906 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100907#ifdef _WIN32
908 val = 0;
909 ioctlsocket(sd, FIONBIO, &val);
910#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100911 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100912#endif
913 }
914
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100915 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100916 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100917 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
918 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100919
920#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100921 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100922#endif
923
Bram Moolenaar77073442016-02-13 23:23:53 +0100924 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100925}
926
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100927/*
928 * Implements ch_open().
929 */
930 channel_T *
931channel_open_func(typval_T *argvars)
932{
933 char_u *address;
934 char_u *p;
935 char *rest;
936 int port;
937 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200938 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100939
940 address = get_tv_string(&argvars[0]);
941 if (argvars[1].v_type != VAR_UNKNOWN
942 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
943 {
944 EMSG(_(e_invarg));
945 return NULL;
946 }
947
948 /* parse address */
949 p = vim_strchr(address, ':');
950 if (p == NULL)
951 {
952 EMSG2(_(e_invarg2), address);
953 return NULL;
954 }
955 *p++ = NUL;
956 port = strtol((char *)p, &rest, 10);
957 if (*address == NUL || port <= 0 || *rest != NUL)
958 {
959 p[-1] = ':';
960 EMSG2(_(e_invarg2), address);
961 return NULL;
962 }
963
964 /* parse options */
965 clear_job_options(&opt);
966 opt.jo_mode = MODE_JSON;
967 opt.jo_timeout = 2000;
968 if (get_job_options(&argvars[1], &opt,
969 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200970 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100971 if (opt.jo_timeout < 0)
972 {
973 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200974 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100975 }
976
977 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
978 if (channel != NULL)
979 {
980 opt.jo_set = JO_ALL;
981 channel_set_options(channel, &opt);
982 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200983theend:
984 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100985 return channel;
986}
987
Bram Moolenaarde279892016-03-11 22:19:44 +0100988 static void
989may_close_part(sock_T *fd)
990{
991 if (*fd != INVALID_FD)
992 {
993 fd_close(*fd);
994 *fd = INVALID_FD;
995 }
996}
997
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100998 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100999channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001000{
Bram Moolenaarde279892016-03-11 22:19:44 +01001001 if (in != INVALID_FD)
1002 {
1003 may_close_part(&channel->CH_IN_FD);
1004 channel->CH_IN_FD = in;
1005 }
1006 if (out != INVALID_FD)
1007 {
1008# if defined(FEAT_GUI)
1009 channel_gui_unregister_one(channel, PART_OUT);
1010# endif
1011 may_close_part(&channel->CH_OUT_FD);
1012 channel->CH_OUT_FD = out;
1013# if defined(FEAT_GUI)
1014 channel_gui_register_one(channel, PART_OUT);
1015# endif
1016 }
1017 if (err != INVALID_FD)
1018 {
1019# if defined(FEAT_GUI)
1020 channel_gui_unregister_one(channel, PART_ERR);
1021# endif
1022 may_close_part(&channel->CH_ERR_FD);
1023 channel->CH_ERR_FD = err;
1024# if defined(FEAT_GUI)
1025 channel_gui_register_one(channel, PART_ERR);
1026# endif
1027 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001028}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001029
Bram Moolenaard6051b52016-02-28 15:49:03 +01001030/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001031 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001032 * This does not keep a refcount, when the job is freed ch_job is cleared.
1033 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001034 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001035channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001036{
Bram Moolenaar77073442016-02-13 23:23:53 +01001037 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001038
1039 channel_set_options(channel, options);
1040
1041 if (job->jv_in_buf != NULL)
1042 {
1043 chanpart_T *in_part = &channel->ch_part[PART_IN];
1044
1045 in_part->ch_buffer = job->jv_in_buf;
1046 ch_logs(channel, "reading from buffer '%s'",
1047 (char *)in_part->ch_buffer->b_ffname);
1048 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001049 {
1050 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1051 {
1052 /* Special mode: send last-but-one line when appending a line
1053 * to the buffer. */
1054 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001055 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001056 in_part->ch_buf_top =
1057 in_part->ch_buffer->b_ml.ml_line_count + 1;
1058 }
1059 else
1060 in_part->ch_buf_top = options->jo_in_top;
1061 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001062 else
1063 in_part->ch_buf_top = 1;
1064 if (options->jo_set & JO_IN_BOT)
1065 in_part->ch_buf_bot = options->jo_in_bot;
1066 else
1067 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1068 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001069}
1070
1071/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001072 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001073 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001074 */
1075 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001076find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001077{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001078 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001079 buf_T *save_curbuf = curbuf;
1080
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001081 if (name != NULL && *name != NUL)
1082 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001083 if (buf == NULL)
1084 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001085 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001086 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001087 if (buf == NULL)
1088 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001089 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001090 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001091#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001092 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1093 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001094#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001095 if (curbuf->b_ml.ml_mfp == NULL)
1096 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001097 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1098 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001099 changed_bytes(1, 0);
1100 curbuf = save_curbuf;
1101 }
1102
1103 return buf;
1104}
1105
1106/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001107 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001108 */
1109 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001110channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001111{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001112 int part;
1113 char_u **cbp;
1114 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001115
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001116 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001117 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001118 channel->ch_part[part].ch_mode = opt->jo_mode;
1119 if (opt->jo_set & JO_IN_MODE)
1120 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1121 if (opt->jo_set & JO_OUT_MODE)
1122 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1123 if (opt->jo_set & JO_ERR_MODE)
1124 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001125
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001126 if (opt->jo_set & JO_TIMEOUT)
1127 for (part = PART_SOCK; part <= PART_IN; ++part)
1128 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1129 if (opt->jo_set & JO_OUT_TIMEOUT)
1130 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1131 if (opt->jo_set & JO_ERR_TIMEOUT)
1132 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001133 if (opt->jo_set & JO_BLOCK_WRITE)
1134 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001135
1136 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001137 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001138 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001139 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001140 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001141 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001142 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1143 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001144 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001145 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001146 *pp = opt->jo_partial;
1147 if (*pp != NULL)
1148 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001149 }
1150 if (opt->jo_set & JO_OUT_CALLBACK)
1151 {
1152 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001153 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001154 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001155 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001156 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1157 *cbp = vim_strsave(opt->jo_out_cb);
1158 else
1159 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001160 *pp = opt->jo_out_partial;
1161 if (*pp != NULL)
1162 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001163 }
1164 if (opt->jo_set & JO_ERR_CALLBACK)
1165 {
1166 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001167 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001168 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001169 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001170 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1171 *cbp = vim_strsave(opt->jo_err_cb);
1172 else
1173 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001174 *pp = opt->jo_err_partial;
1175 if (*pp != NULL)
1176 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001177 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001178 if (opt->jo_set & JO_CLOSE_CALLBACK)
1179 {
1180 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001181 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001182 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001183 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001184 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1185 *cbp = vim_strsave(opt->jo_close_cb);
1186 else
1187 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001188 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001189 if (*pp != NULL)
1190 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001191 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001192
1193 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1194 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001195 buf_T *buf;
1196
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001197 /* writing output to a buffer. Default mode is NL. */
1198 if (!(opt->jo_set & JO_OUT_MODE))
1199 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001200 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001201 {
1202 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1203 if (buf == NULL)
1204 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1205 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001206 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001207 {
1208 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1209 }
1210 if (buf != NULL)
1211 {
1212 ch_logs(channel, "writing out to buffer '%s'",
1213 (char *)buf->b_ffname);
1214 channel->ch_part[PART_OUT].ch_buffer = buf;
1215 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001216 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001217
1218 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1219 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1220 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1221 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001222 buf_T *buf;
1223
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001224 /* writing err to a buffer. Default mode is NL. */
1225 if (!(opt->jo_set & JO_ERR_MODE))
1226 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1227 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001228 buf = channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001229 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001230 {
1231 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1232 if (buf == NULL)
1233 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1234 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001235 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001236 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1237 if (buf != NULL)
1238 {
1239 ch_logs(channel, "writing err to buffer '%s'",
1240 (char *)buf->b_ffname);
1241 channel->ch_part[PART_ERR].ch_buffer = buf;
1242 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001243 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001244
1245 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1246 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1247 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001248}
1249
1250/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001251 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001252 */
1253 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001254channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001255 channel_T *channel,
1256 int part,
1257 char_u *callback,
1258 partial_T *partial,
1259 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001260{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001261 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001262 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1263
1264 if (item != NULL)
1265 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001266 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001267 item->cq_partial = partial;
1268 if (partial != NULL)
1269 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001270 item->cq_seq_nr = id;
1271 item->cq_prev = head->cq_prev;
1272 head->cq_prev = item;
1273 item->cq_next = NULL;
1274 if (item->cq_prev == NULL)
1275 head->cq_next = item;
1276 else
1277 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001278 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001279}
1280
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001281 static void
1282write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1283{
1284 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001285 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001286 char_u *p;
1287
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001288 if ((p = alloc(len + 2)) == NULL)
1289 return;
1290 STRCPY(p, line);
1291 p[len] = NL;
1292 p[len + 1] = NUL;
1293 channel_send(channel, PART_IN, p, "write_buf_line()");
1294 vim_free(p);
1295}
1296
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001297/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001298 * Return TRUE if "channel" can be written to.
1299 * Returns FALSE if the input is closed or the write would block.
1300 */
1301 static int
1302can_write_buf_line(channel_T *channel)
1303{
1304 chanpart_T *in_part = &channel->ch_part[PART_IN];
1305
1306 if (in_part->ch_fd == INVALID_FD)
1307 return FALSE; /* pipe was closed */
1308
1309 /* for testing: block every other attempt to write */
1310 if (in_part->ch_block_write == 1)
1311 in_part->ch_block_write = -1;
1312 else if (in_part->ch_block_write == -1)
1313 in_part->ch_block_write = 1;
1314
1315 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1316#ifndef WIN32
1317 {
1318# if defined(HAVE_SELECT)
1319 struct timeval tval;
1320 fd_set wfds;
1321 int ret;
1322
1323 FD_ZERO(&wfds);
1324 FD_SET((int)in_part->ch_fd, &wfds);
1325 tval.tv_sec = 0;
1326 tval.tv_usec = 0;
1327 for (;;)
1328 {
1329 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1330# ifdef EINTR
1331 SOCK_ERRNO;
1332 if (ret == -1 && errno == EINTR)
1333 continue;
1334# endif
1335 if (ret <= 0 || in_part->ch_block_write == 1)
1336 {
1337 if (ret > 0)
1338 ch_log(channel, "FAKED Input not ready for writing");
1339 else
1340 ch_log(channel, "Input not ready for writing");
1341 return FALSE;
1342 }
1343 break;
1344 }
1345# else
1346 struct pollfd fds;
1347
1348 fds.fd = in_part->ch_fd;
1349 fds.events = POLLOUT;
1350 if (poll(&fds, 1, 0) <= 0)
1351 {
1352 ch_log(channel, "Input not ready for writing");
1353 return FALSE;
1354 }
1355 if (in_part->ch_block_write == 1)
1356 {
1357 ch_log(channel, "FAKED Input not ready for writing");
1358 return FALSE;
1359 }
1360# endif
1361 }
1362#endif
1363 return TRUE;
1364}
1365
1366/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001367 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001368 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001369 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001370channel_write_in(channel_T *channel)
1371{
1372 chanpart_T *in_part = &channel->ch_part[PART_IN];
1373 linenr_T lnum;
1374 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001375 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001376
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001377 if (buf == NULL || in_part->ch_buf_append)
1378 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001379 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1380 {
1381 /* buffer was wiped out or unloaded */
1382 in_part->ch_buffer = NULL;
1383 return;
1384 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001385
1386 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1387 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1388 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001389 if (!can_write_buf_line(channel))
1390 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001391 write_buf_line(buf, lnum, channel);
1392 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001393 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001394
1395 if (written == 1)
1396 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1397 else if (written > 1)
1398 ch_logn(channel, "written %d lines to channel", written);
1399
Bram Moolenaar014069a2016-03-03 22:51:40 +01001400 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001401 if (lnum > buf->b_ml.ml_line_count)
1402 {
1403 /* Writing is done, no longer need the buffer. */
1404 in_part->ch_buffer = NULL;
1405 ch_log(channel, "Finished writing all lines to channel");
1406 }
1407 else
1408 ch_logn(channel, "Still %d more lines to write",
1409 buf->b_ml.ml_line_count - lnum + 1);
1410}
1411
1412/*
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001413 * Handle buffer "buf" beeing freed, remove it from any channels.
1414 */
1415 void
1416channel_buffer_free(buf_T *buf)
1417{
1418 channel_T *channel;
1419 int part;
1420
1421 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1422 for (part = PART_SOCK; part <= PART_IN; ++part)
1423 {
1424 chanpart_T *ch_part = &channel->ch_part[part];
1425
1426 if (ch_part->ch_buffer == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001427 {
1428 ch_logs(channel, "%s buffer has been wiped out",
1429 part_names[part]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001430 ch_part->ch_buffer = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001431 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001432 }
1433}
1434
1435/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001436 * Write any lines waiting to be written to a channel.
1437 */
1438 void
1439channel_write_any_lines()
1440{
1441 channel_T *channel;
1442
1443 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1444 {
1445 chanpart_T *in_part = &channel->ch_part[PART_IN];
1446
1447 if (in_part->ch_buffer != NULL)
1448 {
1449 if (in_part->ch_buf_append)
1450 channel_write_new_lines(in_part->ch_buffer);
1451 else
1452 channel_write_in(channel);
1453 }
1454 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001455}
1456
1457/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001458 * Write appended lines above the last one in "buf" to the channel.
1459 */
1460 void
1461channel_write_new_lines(buf_T *buf)
1462{
1463 channel_T *channel;
1464 int found_one = FALSE;
1465
1466 /* There could be more than one channel for the buffer, loop over all of
1467 * them. */
1468 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1469 {
1470 chanpart_T *in_part = &channel->ch_part[PART_IN];
1471 linenr_T lnum;
1472 int written = 0;
1473
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001474 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001475 {
1476 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001477 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001478 found_one = TRUE;
1479 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1480 ++lnum)
1481 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001482 if (!can_write_buf_line(channel))
1483 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001484 write_buf_line(buf, lnum, channel);
1485 ++written;
1486 }
1487
1488 if (written == 1)
1489 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1490 else if (written > 1)
1491 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001492 if (lnum < buf->b_ml.ml_line_count)
1493 ch_logn(channel, "Still %d more lines to write",
1494 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001495
1496 in_part->ch_buf_bot = lnum;
1497 }
1498 }
1499 if (!found_one)
1500 buf->b_write_to_channel = FALSE;
1501}
1502
1503/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001504 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001505 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001506 */
1507 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001508invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1509 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001510{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001511 typval_T rettv;
1512 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001513
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001514 if (safe_to_invoke_callback == 0)
1515 EMSG("INTERNAL: Invoking callback when it is not safe");
1516
Bram Moolenaar77073442016-02-13 23:23:53 +01001517 argv[0].v_type = VAR_CHANNEL;
1518 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001519
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001520 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001521 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001522 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001523 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001524}
1525
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001526/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001527 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001528 * The caller must free it.
1529 * Returns NULL if there is nothing.
1530 */
1531 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001532channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001533{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001534 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001535 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001536 char_u *p;
1537
Bram Moolenaar77073442016-02-13 23:23:53 +01001538 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001539 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001540 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001541 p = node->rq_buffer;
1542 head->rq_next = node->rq_next;
1543 if (node->rq_next == NULL)
1544 head->rq_prev = NULL;
1545 else
1546 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001547 vim_free(node);
1548 return p;
1549}
1550
1551/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001552 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001553 */
1554 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001555channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001556{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001557 readq_T *head = &channel->ch_part[part].ch_head;
1558 readq_T *node = head->rq_next;
1559 long_u len = 1;
1560 char_u *res;
1561 char_u *p;
1562
1563 /* If there is only one buffer just get that one. */
1564 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1565 return channel_get(channel, part);
1566
1567 /* Concatenate everything into one buffer. */
1568 for (node = head->rq_next; node != NULL; node = node->rq_next)
1569 len += (long_u)STRLEN(node->rq_buffer);
1570 res = lalloc(len, TRUE);
1571 if (res == NULL)
1572 return NULL;
1573 *res = NUL;
1574 for (node = head->rq_next; node != NULL; node = node->rq_next)
1575 STRCAT(res, node->rq_buffer);
1576
1577 /* Free all buffers */
1578 do
1579 {
1580 p = channel_get(channel, part);
1581 vim_free(p);
1582 } while (p != NULL);
1583
1584 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001585}
1586
1587/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001588 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001589 * Returns FAIL if that is not possible.
1590 */
1591 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001592channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001593{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001594 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001595 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001596 char_u *p;
1597
Bram Moolenaar77073442016-02-13 23:23:53 +01001598 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001599 return FAIL;
1600
Bram Moolenaar77073442016-02-13 23:23:53 +01001601 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1602 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001603 if (p == NULL)
1604 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001605 STRCPY(p, node->rq_buffer);
1606 STRCAT(p, node->rq_next->rq_buffer);
1607 vim_free(node->rq_next->rq_buffer);
1608 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001609
Bram Moolenaar77073442016-02-13 23:23:53 +01001610 /* dispose of the node and its buffer */
1611 head->rq_next = node->rq_next;
1612 head->rq_next->rq_prev = NULL;
1613 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001614 vim_free(node);
1615 return OK;
1616}
1617
1618/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001619 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001620 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001621 * Returns OK or FAIL.
1622 */
1623 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001624channel_save(channel_T *channel, int part, char_u *buf, int len,
1625 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001626{
1627 readq_T *node;
1628 readq_T *head = &channel->ch_part[part].ch_head;
1629 char_u *p;
1630 int i;
1631
1632 node = (readq_T *)alloc(sizeof(readq_T));
1633 if (node == NULL)
1634 return FAIL; /* out of memory */
1635 node->rq_buffer = alloc(len + 1);
1636 if (node->rq_buffer == NULL)
1637 {
1638 vim_free(node);
1639 return FAIL; /* out of memory */
1640 }
1641
1642 if (channel->ch_part[part].ch_mode == MODE_NL)
1643 {
1644 /* Drop any CR before a NL. */
1645 p = node->rq_buffer;
1646 for (i = 0; i < len; ++i)
1647 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1648 *p++ = buf[i];
1649 *p = NUL;
1650 }
1651 else
1652 {
1653 mch_memmove(node->rq_buffer, buf, len);
1654 node->rq_buffer[len] = NUL;
1655 }
1656
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001657 if (prepend)
1658 {
1659 /* preend node to the head of the queue */
1660 node->rq_next = head->rq_next;
1661 node->rq_prev = NULL;
1662 if (head->rq_next == NULL)
1663 head->rq_prev = node;
1664 else
1665 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001666 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001667 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001668 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001669 {
1670 /* append node to the tail of the queue */
1671 node->rq_next = NULL;
1672 node->rq_prev = head->rq_prev;
1673 if (head->rq_prev == NULL)
1674 head->rq_next = node;
1675 else
1676 head->rq_prev->rq_next = node;
1677 head->rq_prev = node;
1678 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001679
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001680 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001681 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001682 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001683 fprintf(log_fd, "'");
1684 if (fwrite(buf, len, 1, log_fd) != 1)
1685 return FAIL;
1686 fprintf(log_fd, "'\n");
1687 }
1688 return OK;
1689}
1690
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001691 static int
1692channel_fill(js_read_T *reader)
1693{
1694 channel_T *channel = (channel_T *)reader->js_cookie;
1695 int part = reader->js_cookie_arg;
1696 char_u *next = channel_get(channel, part);
1697 int unused;
1698 int len;
1699 char_u *p;
1700
1701 if (next == NULL)
1702 return FALSE;
1703
1704 unused = reader->js_end - reader->js_buf - reader->js_used;
1705 if (unused > 0)
1706 {
1707 /* Prepend unused text. */
1708 len = (int)STRLEN(next);
1709 p = alloc(unused + len + 1);
1710 if (p == NULL)
1711 {
1712 vim_free(next);
1713 return FALSE;
1714 }
1715 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1716 mch_memmove(p + unused, next, len + 1);
1717 vim_free(next);
1718 next = p;
1719 }
1720
1721 vim_free(reader->js_buf);
1722 reader->js_buf = next;
1723 reader->js_used = 0;
1724 return TRUE;
1725}
1726
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001727/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001728 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001729 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001730 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001731 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001732 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001733channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001734{
1735 js_read_T reader;
1736 typval_T listtv;
1737 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001738 chanpart_T *chanpart = &channel->ch_part[part];
1739 jsonq_T *head = &chanpart->ch_json_head;
1740 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001741 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001742
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001743 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001744 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001745
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001746 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001747 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001748 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001749 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001750 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001751
1752 /* When a message is incomplete we wait for a short while for more to
1753 * arrive. After the delay drop the input, otherwise a truncated string
1754 * or list will make us hang. */
1755 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001756 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001757 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001758 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001759 /* Only accept the response when it is a list with at least two
1760 * items. */
1761 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001762 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001763 if (listtv.v_type != VAR_LIST)
1764 ch_error(channel, "Did not receive a list, discarding");
1765 else
1766 ch_errorn(channel, "Expected list with two items, got %d",
1767 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001768 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001769 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001770 else
1771 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001772 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1773 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001774 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001775 else
1776 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001777 item->jq_value = alloc_tv();
1778 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001779 {
1780 vim_free(item);
1781 clear_tv(&listtv);
1782 }
1783 else
1784 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001785 *item->jq_value = listtv;
1786 item->jq_prev = head->jq_prev;
1787 head->jq_prev = item;
1788 item->jq_next = NULL;
1789 if (item->jq_prev == NULL)
1790 head->jq_next = item;
1791 else
1792 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001793 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001794 }
1795 }
1796 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001797
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001798 if (status == OK)
1799 chanpart->ch_waiting = FALSE;
1800 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001801 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001802 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001803 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001804 /* First time encountering incomplete message, set a deadline of
1805 * 100 msec. */
1806 ch_log(channel, "Incomplete message - wait for more");
1807 reader.js_used = 0;
1808 chanpart->ch_waiting = TRUE;
1809#ifdef WIN32
1810 chanpart->ch_deadline = GetTickCount() + 100L;
1811#else
1812 gettimeofday(&chanpart->ch_deadline, NULL);
1813 chanpart->ch_deadline.tv_usec += 100 * 1000;
1814 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1815 {
1816 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1817 ++chanpart->ch_deadline.tv_sec;
1818 }
1819#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001820 }
1821 else
1822 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001823 int timeout;
1824#ifdef WIN32
1825 timeout = GetTickCount() > chanpart->ch_deadline;
1826#else
1827 {
1828 struct timeval now_tv;
1829
1830 gettimeofday(&now_tv, NULL);
1831 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1832 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1833 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1834 }
1835#endif
1836 if (timeout)
1837 {
1838 status = FAIL;
1839 chanpart->ch_waiting = FALSE;
1840 }
1841 else
1842 {
1843 reader.js_used = 0;
1844 ch_log(channel, "still waiting on incomplete message");
1845 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001846 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001847 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001848
1849 if (status == FAIL)
1850 {
1851 ch_error(channel, "Decoding failed - discarding input");
1852 ret = FALSE;
1853 chanpart->ch_waiting = FALSE;
1854 }
1855 else if (reader.js_buf[reader.js_used] != NUL)
1856 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001857 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001858 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001859 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1860 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001861 ret = status == MAYBE ? FALSE: TRUE;
1862 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001863 else
1864 ret = FALSE;
1865
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001866 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001867 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001868}
1869
1870/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001871 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001872 */
1873 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001874remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001875{
Bram Moolenaar77073442016-02-13 23:23:53 +01001876 if (node->cq_prev == NULL)
1877 head->cq_next = node->cq_next;
1878 else
1879 node->cq_prev->cq_next = node->cq_next;
1880 if (node->cq_next == NULL)
1881 head->cq_prev = node->cq_prev;
1882 else
1883 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001884}
1885
1886/*
1887 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001888 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001889 */
1890 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001891remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001892{
Bram Moolenaar77073442016-02-13 23:23:53 +01001893 if (node->jq_prev == NULL)
1894 head->jq_next = node->jq_next;
1895 else
1896 node->jq_prev->jq_next = node->jq_next;
1897 if (node->jq_next == NULL)
1898 head->jq_prev = node->jq_prev;
1899 else
1900 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001901 vim_free(node);
1902}
1903
1904/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001905 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001906 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001907 * When "id" is zero or negative jut get the first message. But not the one
1908 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001909 * Return OK when found and return the value in "rettv".
1910 * Return FAIL otherwise.
1911 */
1912 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001913channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001914{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001915 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001916 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001917
Bram Moolenaar77073442016-02-13 23:23:53 +01001918 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001919 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001920 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001921 typval_T *tv = &l->lv_first->li_tv;
1922
1923 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001924 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001925 || tv->vval.v_number == 0
1926 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001927 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001928 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001929 if (tv->v_type == VAR_NUMBER)
1930 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001931 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001932 return OK;
1933 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001934 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001935 }
1936 return FAIL;
1937}
1938
Bram Moolenaarece61b02016-02-20 21:39:05 +01001939#define CH_JSON_MAX_ARGS 4
1940
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001941/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001942 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001943 * "argv[0]" is the command string.
1944 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001945 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001946 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001947channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001948{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001949 char_u *cmd = argv[0].vval.v_string;
1950 char_u *arg;
1951 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001952
Bram Moolenaarece61b02016-02-20 21:39:05 +01001953 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001954 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001955 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001956 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001957 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001958 return;
1959 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001960 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001961 if (arg == NULL)
1962 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001963
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001964 if (STRCMP(cmd, "ex") == 0)
1965 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001966 int save_called_emsg = called_emsg;
1967
1968 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001969 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001970 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001971 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001972 --emsg_silent;
1973 if (called_emsg)
1974 ch_logs(channel, "Ex command error: '%s'",
1975 (char *)get_vim_var_str(VV_ERRMSG));
1976 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001977 }
1978 else if (STRCMP(cmd, "normal") == 0)
1979 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001980 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001981
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001982 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001983 ea.arg = arg;
1984 ea.addr_count = 0;
1985 ea.forceit = TRUE; /* no mapping */
1986 ex_normal(&ea);
1987 }
1988 else if (STRCMP(cmd, "redraw") == 0)
1989 {
1990 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001991
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001992 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001993 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001994 ex_redraw(&ea);
1995 showruler(FALSE);
1996 setcursor();
1997 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001998#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001999 if (gui.in_use)
2000 {
2001 gui_update_cursor(FALSE, FALSE);
2002 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002003 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002004#endif
2005 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002006 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002007 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002008 int is_call = cmd[0] == 'c';
2009 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002010
Bram Moolenaarece61b02016-02-20 21:39:05 +01002011 if (argv[id_idx].v_type != VAR_UNKNOWN
2012 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002013 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002014 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002015 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002016 EMSG("E904: last argument for expr/call must be a number");
2017 }
2018 else if (is_call && argv[2].v_type != VAR_LIST)
2019 {
2020 ch_error(channel, "third argument for call must be a list");
2021 if (p_verbose > 2)
2022 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002023 }
2024 else
2025 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002026 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002027 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002028 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002029 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002030
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002031 /* Don't pollute the display with errors. */
2032 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002033 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002034 {
2035 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002036 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002037 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002038 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002039 {
2040 ch_logs(channel, "Calling '%s'", (char *)arg);
2041 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2042 tv = &res_tv;
2043 else
2044 tv = NULL;
2045 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002046
2047 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002048 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002049 int id = argv[id_idx].vval.v_number;
2050
Bram Moolenaar55fab432016-02-07 16:53:13 +01002051 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002052 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002053 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002054 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002055 /* If evaluation failed or the result can't be encoded
2056 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002057 vim_free(json);
2058 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002059 err_tv.v_type = VAR_STRING;
2060 err_tv.vval.v_string = (char_u *)"ERROR";
2061 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002062 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002063 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002064 if (json != NULL)
2065 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002066 channel_send(channel,
2067 part == PART_SOCK ? PART_SOCK : PART_IN,
2068 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002069 vim_free(json);
2070 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002071 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002072 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002073 if (tv == &res_tv)
2074 clear_tv(tv);
2075 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002076 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002077 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002078 }
2079 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002080 {
2081 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002082 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002083 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002084}
2085
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002086/*
2087 * Invoke the callback at "cbhead".
2088 * Does not redraw but sets channel_need_redraw.
2089 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002090 static void
2091invoke_one_time_callback(
2092 channel_T *channel,
2093 cbq_T *cbhead,
2094 cbq_T *item,
2095 typval_T *argv)
2096{
2097 ch_logs(channel, "Invoking one-time callback %s",
2098 (char *)item->cq_callback);
2099 /* Remove the item from the list first, if the callback
2100 * invokes ch_close() the list will be cleared. */
2101 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002102 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002103 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002104 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002105 vim_free(item);
2106}
2107
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002108 static void
2109append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2110{
2111 buf_T *save_curbuf = curbuf;
2112 linenr_T lnum = buffer->b_ml.ml_line_count;
2113 int save_write_to = buffer->b_write_to_channel;
2114
2115 /* If the buffer is also used as input insert above the last
2116 * line. Don't write these lines. */
2117 if (save_write_to)
2118 {
2119 --lnum;
2120 buffer->b_write_to_channel = FALSE;
2121 }
2122
2123 /* Append to the buffer */
2124 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2125
2126 curbuf = buffer;
2127 u_sync(TRUE);
2128 /* ignore undo failure, undo is not very useful here */
2129 ignored = u_save(lnum, lnum + 1);
2130
2131 ml_append(lnum, msg, 0, FALSE);
2132 appended_lines_mark(lnum, 1L);
2133 curbuf = save_curbuf;
2134
2135 if (buffer->b_nwindows > 0)
2136 {
2137 win_T *wp;
2138 win_T *save_curwin;
2139
2140 FOR_ALL_WINDOWS(wp)
2141 {
2142 if (wp->w_buffer == buffer
2143 && (save_write_to
2144 ? wp->w_cursor.lnum == lnum + 1
2145 : (wp->w_cursor.lnum == lnum
2146 && wp->w_cursor.col == 0)))
2147 {
2148 ++wp->w_cursor.lnum;
2149 save_curwin = curwin;
2150 curwin = wp;
2151 curbuf = curwin->w_buffer;
2152 scroll_cursor_bot(0, FALSE);
2153 curwin = save_curwin;
2154 curbuf = curwin->w_buffer;
2155 }
2156 }
2157 redraw_buf_later(buffer, VALID);
2158 channel_need_redraw = TRUE;
2159 }
2160
2161 if (save_write_to)
2162 {
2163 channel_T *ch;
2164
2165 /* Find channels reading from this buffer and adjust their
2166 * next-to-read line number. */
2167 buffer->b_write_to_channel = TRUE;
2168 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2169 {
2170 chanpart_T *in_part = &ch->ch_part[PART_IN];
2171
2172 if (in_part->ch_buffer == buffer)
2173 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2174 }
2175 }
2176}
2177
Bram Moolenaar437905c2016-04-26 19:01:05 +02002178 static void
2179drop_messages(channel_T *channel, int part)
2180{
2181 char_u *msg;
2182
2183 while ((msg = channel_get(channel, part)) != NULL)
2184 {
2185 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2186 vim_free(msg);
2187 }
2188}
2189
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002190/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002191 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002192 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002193 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002194 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002195 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002196may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002197{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002198 char_u *msg = NULL;
2199 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002200 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002201 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002202 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002203 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002204 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002205 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002206 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002207 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002208
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002209 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002210 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002211 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002212
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002213 /* Use a message-specific callback, part callback or channel callback */
2214 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2215 if (cbitem->cq_seq_nr == 0)
2216 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002217 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002218 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002219 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002220 partial = cbitem->cq_partial;
2221 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002222 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002223 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002224 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002225 partial = channel->ch_part[part].ch_partial;
2226 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002227 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002228 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002229 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002230 partial = channel->ch_partial;
2231 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002232
Bram Moolenaar187db502016-02-27 14:44:26 +01002233 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002234 if (buffer != NULL && !buf_valid(buffer))
2235 {
2236 /* buffer was wiped out */
2237 channel->ch_part[part].ch_buffer = NULL;
2238 buffer = NULL;
2239 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002240
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002241 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002242 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002243 listitem_T *item;
2244 int argc = 0;
2245
Bram Moolenaard7ece102016-02-02 23:23:02 +01002246 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002247 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002248 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002249 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002250 channel_parse_json(channel, part);
2251 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002252 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002253 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002254
Bram Moolenaarece61b02016-02-20 21:39:05 +01002255 for (item = listtv->vval.v_list->lv_first;
2256 item != NULL && argc < CH_JSON_MAX_ARGS;
2257 item = item->li_next)
2258 argv[argc++] = item->li_tv;
2259 while (argc < CH_JSON_MAX_ARGS)
2260 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002261
Bram Moolenaarece61b02016-02-20 21:39:05 +01002262 if (argv[0].v_type == VAR_STRING)
2263 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002264 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002265 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002266 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002267 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002268 }
2269
Bram Moolenaarece61b02016-02-20 21:39:05 +01002270 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002271 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002272 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002273 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002274 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002275 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002276 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002277 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002278 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002279 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002280 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002281 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002282 return FALSE;
2283 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002284 else
2285 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002286 /* If there is no callback or buffer drop the message. */
2287 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002288 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002289 /* If there is a close callback it may use ch_read() to get the
2290 * messages. */
2291 if (channel->ch_close_cb == NULL)
2292 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002293 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002294 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002295
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002296 if (ch_mode == MODE_NL)
2297 {
2298 char_u *nl;
2299 char_u *buf;
2300
2301 /* See if we have a message ending in NL in the first buffer. If
2302 * not try to concatenate the first and the second buffer. */
2303 while (TRUE)
2304 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002305 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002306 nl = vim_strchr(buf, NL);
2307 if (nl != NULL)
2308 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002309 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002310 return FALSE; /* incomplete message */
2311 }
2312 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002313 {
2314 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002315 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002316 *nl = NUL;
2317 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002318 else
2319 {
2320 /* Copy the message into allocated memory and remove it from
2321 * the buffer. */
2322 msg = vim_strnsave(buf, (int)(nl - buf));
2323 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2324 }
2325 }
2326 else
2327 /* For a raw channel we don't know where the message ends, just
2328 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002329 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002330
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002331 if (msg == NULL)
2332 return FALSE; /* out of memory (and avoids Coverity warning) */
2333
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002334 argv[1].v_type = VAR_STRING;
2335 argv[1].vval.v_string = msg;
2336 }
2337
Bram Moolenaara07fec92016-02-05 21:04:08 +01002338 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002339 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002340 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002341
2342 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002343 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002344 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002345 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002346 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002347 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002348 break;
2349 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002350 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002351 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002352 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002353 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002354 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002355 if (buffer != NULL)
2356 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002357 if (msg == NULL)
2358 /* JSON or JS mode: re-encode the message. */
2359 msg = json_encode(listtv, ch_mode);
2360 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002361 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002362 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002363
Bram Moolenaar187db502016-02-27 14:44:26 +01002364 if (callback != NULL)
2365 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002366 if (cbitem != NULL)
2367 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2368 else
2369 {
2370 /* invoke the channel callback */
2371 ch_logs(channel, "Invoking channel callback %s",
2372 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002373 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002374 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002375 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002376 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002377 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002378 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002379
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002380 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002381 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002382 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002383
2384 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002385}
2386
2387/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002388 * Return TRUE when channel "channel" is open for writing to.
2389 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002390 */
2391 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002392channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002393{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002394 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002395 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002396}
2397
2398/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002399 * Return TRUE when channel "channel" is open for reading or writing.
2400 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002401 */
2402 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002403channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002404{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002405 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002406 || channel->CH_IN_FD != INVALID_FD
2407 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002408 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002409}
2410
2411/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002412 * Return TRUE if "channel" has JSON or other typeahead.
2413 */
2414 static int
2415channel_has_readahead(channel_T *channel, int part)
2416{
2417 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2418
2419 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2420 {
2421 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2422 jsonq_T *item = head->jq_next;
2423
2424 return item != NULL;
2425 }
2426 return channel_peek(channel, part) != NULL;
2427}
2428
2429/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002430 * Return a string indicating the status of the channel.
2431 */
2432 char *
2433channel_status(channel_T *channel)
2434{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002435 int part;
2436 int has_readahead = FALSE;
2437
Bram Moolenaar77073442016-02-13 23:23:53 +01002438 if (channel == NULL)
2439 return "fail";
2440 if (channel_is_open(channel))
2441 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002442 for (part = PART_SOCK; part <= PART_ERR; ++part)
2443 if (channel_has_readahead(channel, part))
2444 {
2445 has_readahead = TRUE;
2446 break;
2447 }
2448
2449 if (has_readahead)
2450 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002451 return "closed";
2452}
2453
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002454 static void
2455channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2456{
2457 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002458 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002459 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002460 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002461
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002462 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002463 STRCAT(namebuf, "_");
2464 tail = STRLEN(namebuf);
2465
2466 STRCPY(namebuf + tail, "status");
2467 dict_add_nr_str(dict, namebuf, 0,
2468 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2469
2470 STRCPY(namebuf + tail, "mode");
2471 switch (chanpart->ch_mode)
2472 {
2473 case MODE_NL: s = "NL"; break;
2474 case MODE_RAW: s = "RAW"; break;
2475 case MODE_JSON: s = "JSON"; break;
2476 case MODE_JS: s = "JS"; break;
2477 }
2478 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2479
2480 STRCPY(namebuf + tail, "io");
2481 if (part == PART_SOCK)
2482 s = "socket";
2483 else switch (chanpart->ch_io)
2484 {
2485 case JIO_NULL: s = "null"; break;
2486 case JIO_PIPE: s = "pipe"; break;
2487 case JIO_FILE: s = "file"; break;
2488 case JIO_BUFFER: s = "buffer"; break;
2489 case JIO_OUT: s = "out"; break;
2490 }
2491 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2492
2493 STRCPY(namebuf + tail, "timeout");
2494 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2495}
2496
2497 void
2498channel_info(channel_T *channel, dict_T *dict)
2499{
2500 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2501 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2502
2503 if (channel->ch_hostname != NULL)
2504 {
2505 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2506 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2507 channel_part_info(channel, dict, "sock", PART_SOCK);
2508 }
2509 else
2510 {
2511 channel_part_info(channel, dict, "out", PART_OUT);
2512 channel_part_info(channel, dict, "err", PART_ERR);
2513 channel_part_info(channel, dict, "in", PART_IN);
2514 }
2515}
2516
Bram Moolenaar77073442016-02-13 23:23:53 +01002517/*
2518 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002519 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002520 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002521 */
2522 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002523channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002524{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002525 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002526
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002527#ifdef FEAT_GUI
2528 channel_gui_unregister(channel);
2529#endif
2530
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002531 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002532 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002533 sock_close(channel->CH_SOCK_FD);
2534 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002535 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002536 may_close_part(&channel->CH_IN_FD);
2537 may_close_part(&channel->CH_OUT_FD);
2538 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002539
Bram Moolenaar8b374212016-02-24 20:43:06 +01002540 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002541 {
2542 typval_T argv[1];
2543 typval_T rettv;
2544 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002545 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002546
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002547 /* Invoke callbacks before the close callback, since it's weird to
2548 * first invoke the close callback. Increment the refcount to avoid
2549 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002550 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002551 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002552 for (part = PART_SOCK; part <= PART_ERR; ++part)
2553 while (may_invoke_callback(channel, part))
2554 ;
2555
2556 /* Invoke the close callback, if still set. */
2557 if (channel->ch_close_cb != NULL)
2558 {
2559 ch_logs(channel, "Invoking close callback %s",
2560 (char *)channel->ch_close_cb);
2561 argv[0].v_type = VAR_CHANNEL;
2562 argv[0].vval.v_channel = channel;
2563 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002564 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2565 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002566 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002567 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002568 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002569 --channel->ch_refcount;
2570
2571 /* the callback is only called once */
2572 vim_free(channel->ch_close_cb);
2573 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002574 partial_unref(channel->ch_close_partial);
2575 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002576
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002577 if (channel_need_redraw)
2578 {
2579 channel_need_redraw = FALSE;
2580 redraw_after_callback();
2581 }
2582
Bram Moolenaar437905c2016-04-26 19:01:05 +02002583 /* any remaining messages are useless now */
2584 for (part = PART_SOCK; part <= PART_ERR; ++part)
2585 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002586 }
2587
2588 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002589}
2590
Bram Moolenaard04a0202016-01-26 23:30:18 +01002591/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002592 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002593 * Returns NULL if there is nothing.
2594 */
2595 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002596channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002597{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002598 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002599
Bram Moolenaar77073442016-02-13 23:23:53 +01002600 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002601 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002602 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002603}
2604
2605/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002606 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002607 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002608 static void
2609channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002610{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002611 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2612 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002613
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002614 while (channel_peek(channel, part) != NULL)
2615 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002616
2617 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002618 {
2619 cbq_T *node = cb_head->cq_next;
2620
2621 remove_cb_node(cb_head, node);
2622 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002623 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002624 vim_free(node);
2625 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002626
2627 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002628 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002629 free_tv(json_head->jq_next->jq_value);
2630 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002631 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002632
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002633 vim_free(channel->ch_part[part].ch_callback);
2634 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002635 partial_unref(channel->ch_part[part].ch_partial);
2636 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002637}
2638
2639/*
2640 * Clear all the read buffers on "channel".
2641 */
2642 void
2643channel_clear(channel_T *channel)
2644{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002645 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002646 vim_free(channel->ch_hostname);
2647 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002648 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002649 channel_clear_one(channel, PART_OUT);
2650 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002651 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002652 vim_free(channel->ch_callback);
2653 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002654 partial_unref(channel->ch_partial);
2655 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002656 vim_free(channel->ch_close_cb);
2657 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002658 partial_unref(channel->ch_close_partial);
2659 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002660}
2661
Bram Moolenaar77073442016-02-13 23:23:53 +01002662#if defined(EXITFREE) || defined(PROTO)
2663 void
2664channel_free_all(void)
2665{
2666 channel_T *channel;
2667
Bram Moolenaard6051b52016-02-28 15:49:03 +01002668 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002669 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2670 channel_clear(channel);
2671}
2672#endif
2673
2674
Bram Moolenaar715d2852016-04-30 17:06:31 +02002675/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002676#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002677
2678/* Buffer size for reading incoming messages. */
2679#define MAXMSGSIZE 4096
2680
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002681#if defined(HAVE_SELECT)
2682/*
2683 * Add write fds where we are waiting for writing to be possible.
2684 */
2685 static int
2686channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2687{
2688 int maxfd = maxfd_arg;
2689 channel_T *ch;
2690
2691 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2692 {
2693 chanpart_T *in_part = &ch->ch_part[PART_IN];
2694
2695 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2696 {
2697 FD_SET((int)in_part->ch_fd, wfds);
2698 if ((int)in_part->ch_fd >= maxfd)
2699 maxfd = (int)in_part->ch_fd + 1;
2700 }
2701 }
2702 return maxfd;
2703}
2704#else
2705/*
2706 * Add write fds where we are waiting for writing to be possible.
2707 */
2708 static int
2709channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2710{
2711 int nfd = nfd_in;
2712 channel_T *ch;
2713
2714 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2715 {
2716 chanpart_T *in_part = &ch->ch_part[PART_IN];
2717
2718 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2719 {
2720 in_part->ch_poll_idx = nfd;
2721 fds[nfd].fd = in_part->ch_fd;
2722 fds[nfd].events = POLLOUT;
2723 ++nfd;
2724 }
2725 else
2726 in_part->ch_poll_idx = -1;
2727 }
2728 return nfd;
2729}
2730#endif
2731
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002732typedef enum {
2733 CW_READY,
2734 CW_NOT_READY,
2735 CW_ERROR
2736} channel_wait_result;
2737
Bram Moolenaard04a0202016-01-26 23:30:18 +01002738/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002739 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002740 * Return CW_READY when there is something to read.
2741 * Return CW_NOT_READY when there is nothing to read.
2742 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002743 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002744 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002745channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002746{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002747 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002748 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002749
Bram Moolenaard8070362016-02-15 21:56:54 +01002750# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002751 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002752 {
2753 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002754 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002755 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002756 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002757
2758 /* reading from a pipe, not a socket */
2759 while (TRUE)
2760 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002761 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2762
2763 if (r && nread > 0)
2764 return CW_READY;
2765 if (r == 0)
2766 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002767
2768 /* perhaps write some buffer lines */
2769 channel_write_any_lines();
2770
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002771 sleep_time = deadline - GetTickCount();
2772 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002773 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002774 /* Wait for a little while. Very short at first, up to 10 msec
2775 * after looping a few times. */
2776 if (sleep_time > delay)
2777 sleep_time = delay;
2778 Sleep(sleep_time);
2779 delay = delay * 2;
2780 if (delay > 10)
2781 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002782 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002783 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002784 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002785#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002786 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002787#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002788 struct timeval tval;
2789 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002790 fd_set wfds;
2791 int ret;
2792 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002793
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002794 tval.tv_sec = timeout / 1000;
2795 tval.tv_usec = (timeout % 1000) * 1000;
2796 for (;;)
2797 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002798 FD_ZERO(&rfds);
2799 FD_SET((int)fd, &rfds);
2800
2801 /* Write lines to a pipe when a pipe can be written to. Need to
2802 * set this every time, some buffers may be done. */
2803 maxfd = (int)fd + 1;
2804 FD_ZERO(&wfds);
2805 maxfd = channel_fill_wfds(maxfd, &wfds);
2806
2807 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002808# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002809 SOCK_ERRNO;
2810 if (ret == -1 && errno == EINTR)
2811 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002812# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002813 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002814 {
2815 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002816 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002817 channel_write_any_lines();
2818 continue;
2819 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002820 break;
2821 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002822#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002823 for (;;)
2824 {
2825 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2826 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002827
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002828 fds[0].fd = fd;
2829 fds[0].events = POLLIN;
2830 nfd = channel_fill_poll_write(nfd, fds);
2831 if (poll(fds, nfd, timeout) > 0)
2832 {
2833 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002834 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002835 channel_write_any_lines();
2836 continue;
2837 }
2838 break;
2839 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002840#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002841 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002842 return CW_NOT_READY;
2843}
2844
2845 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002846channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002847{
2848 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002849 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2850 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002851
2852 /* Queue a "DETACH" netbeans message in the command queue in order to
2853 * terminate the netbeans session later. Do not end the session here
2854 * directly as we may be running in the context of a call to
2855 * netbeans_parse_messages():
2856 * netbeans_parse_messages
2857 * -> autocmd triggered while processing the netbeans cmd
2858 * -> ui_breakcheck
2859 * -> gui event loop or select loop
2860 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002861 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002862 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002863 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002864 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002865 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2866
2867 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002868 * died. Don't close the channel right away, it may be the wrong moment
2869 * to invoke callbacks. */
2870 channel->ch_to_be_closed = TRUE;
2871}
2872
2873 static void
2874channel_close_now(channel_T *channel)
2875{
2876 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002877 channel_close(channel, TRUE);
2878 if (channel->ch_nb_close_cb != NULL)
2879 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002880}
2881
2882/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002883 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002884 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002885 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002886 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002887 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002888channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002889{
2890 static char_u *buf = NULL;
2891 int len = 0;
2892 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002893 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002894 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002895
Bram Moolenaar5850a762016-05-27 19:59:48 +02002896 /* If we detected a read error don't try reading again. */
2897 if (channel->ch_to_be_closed)
2898 return;
2899
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002900 fd = channel->ch_part[part].ch_fd;
2901 if (fd == INVALID_FD)
2902 {
2903 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002904 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002905 }
2906 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002907
2908 /* Allocate a buffer to read into. */
2909 if (buf == NULL)
2910 {
2911 buf = alloc(MAXMSGSIZE);
2912 if (buf == NULL)
2913 return; /* out of memory! */
2914 }
2915
2916 /* Keep on reading for as long as there is something to read.
2917 * Use select() or poll() to avoid blocking on a message that is exactly
2918 * MAXMSGSIZE long. */
2919 for (;;)
2920 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002921 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002922 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002923 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002924 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002925 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002926 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002927 if (len <= 0)
2928 break; /* error or nothing more to read */
2929
2930 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002931 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002932 readlen += len;
2933 if (len < MAXMSGSIZE)
2934 break; /* did read everything that's available */
2935 }
2936
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002937 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002938 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002939 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002940
2941#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002942 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002943 if (CH_HAS_GUI && gtk_main_level() > 0)
2944 gtk_main_quit();
2945#endif
2946}
2947
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002948/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002949 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002950 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002951 * Returns what was read in allocated memory.
2952 * Returns NULL in case of error or timeout.
2953 */
2954 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002955channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002956{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002957 char_u *buf;
2958 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002959 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002960 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002961 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002962
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002963 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002964 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002965
2966 while (TRUE)
2967 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002968 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002969 if (buf != NULL && (mode == MODE_RAW
2970 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2971 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002972 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002973 continue;
2974
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002975 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002976 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002977 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002978 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002979 {
2980 ch_log(channel, "Timed out");
2981 return NULL;
2982 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002983 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002984 }
2985
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002986 if (mode == MODE_RAW)
2987 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002988 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002989 }
2990 else
2991 {
2992 nl = vim_strchr(buf, NL);
2993 if (nl[1] == NUL)
2994 {
2995 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002996 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002997 *nl = NUL;
2998 }
2999 else
3000 {
3001 /* Copy the message into allocated memory and remove it from the
3002 * buffer. */
3003 msg = vim_strnsave(buf, (int)(nl - buf));
3004 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
3005 }
3006 }
3007 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003008 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003009 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003010}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003011
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003012/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003013 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003014 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003015 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003016 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003017 */
3018 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003019channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003020 channel_T *channel,
3021 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003022 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003023 int id,
3024 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003025{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003026 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003027 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003028 int timeout;
3029 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003030
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003031 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003032 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003033 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003034 for (;;)
3035 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003036 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003037
3038 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003039 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003040 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003041 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003042 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003043 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003044
Bram Moolenaard7ece102016-02-02 23:23:02 +01003045 if (!more)
3046 {
3047 /* Handle any other messages in the queue. If done some more
3048 * messages may have arrived. */
3049 if (channel_parse_messages())
3050 continue;
3051
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003052 /* Wait for up to the timeout. If there was an incomplete message
3053 * use the deadline for that. */
3054 timeout = timeout_arg;
3055 if (chanpart->ch_waiting)
3056 {
3057#ifdef WIN32
3058 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3059#else
3060 {
3061 struct timeval now_tv;
3062
3063 gettimeofday(&now_tv, NULL);
3064 timeout = (chanpart->ch_deadline.tv_sec
3065 - now_tv.tv_sec) * 1000
3066 + (chanpart->ch_deadline.tv_usec
3067 - now_tv.tv_usec) / 1000
3068 + 1;
3069 }
3070#endif
3071 if (timeout < 0)
3072 {
3073 /* Something went wrong, channel_parse_json() didn't
3074 * discard message. Cancel waiting. */
3075 chanpart->ch_waiting = FALSE;
3076 timeout = timeout_arg;
3077 }
3078 else if (timeout > timeout_arg)
3079 timeout = timeout_arg;
3080 }
3081 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003082 if (fd == INVALID_FD
3083 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003084 {
3085 if (timeout == timeout_arg)
3086 {
3087 if (fd != INVALID_FD)
3088 ch_log(channel, "Timed out");
3089 break;
3090 }
3091 }
3092 else
3093 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003094 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003095 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003096 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003097 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003098}
3099
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003100/*
3101 * Common for ch_read() and ch_readraw().
3102 */
3103 void
3104common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3105{
3106 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003107 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003108 jobopt_T opt;
3109 int mode;
3110 int timeout;
3111 int id = -1;
3112 typval_T *listtv = NULL;
3113
3114 /* return an empty string by default */
3115 rettv->v_type = VAR_STRING;
3116 rettv->vval.v_string = NULL;
3117
3118 clear_job_options(&opt);
3119 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3120 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003121 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003122
Bram Moolenaar437905c2016-04-26 19:01:05 +02003123 if (opt.jo_set & JO_PART)
3124 part = opt.jo_part;
3125 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003126 if (channel != NULL)
3127 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003128 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003129 part = channel_part_read(channel);
3130 mode = channel_get_mode(channel, part);
3131 timeout = channel_get_timeout(channel, part);
3132 if (opt.jo_set & JO_TIMEOUT)
3133 timeout = opt.jo_timeout;
3134
3135 if (raw || mode == MODE_RAW || mode == MODE_NL)
3136 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3137 else
3138 {
3139 if (opt.jo_set & JO_ID)
3140 id = opt.jo_id;
3141 channel_read_json_block(channel, part, timeout, id, &listtv);
3142 if (listtv != NULL)
3143 {
3144 *rettv = *listtv;
3145 vim_free(listtv);
3146 }
3147 else
3148 {
3149 rettv->v_type = VAR_SPECIAL;
3150 rettv->vval.v_number = VVAL_NONE;
3151 }
3152 }
3153 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003154
3155theend:
3156 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003157}
3158
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003159# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3160 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003161/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003162 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003163 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003164 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003165 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003166channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003167{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003168 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003169 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003170
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003171 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003172 for (channel = first_channel; channel != NULL;
3173 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003174 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003175 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003176 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003177 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003178 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003179 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003180 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003181 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003182 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003183}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003184# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003185
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003186# if defined(WIN32) || defined(PROTO)
3187/*
3188 * Check the channels for anything that is ready to be read.
3189 * The data is put in the read queue.
3190 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003191 void
3192channel_handle_events(void)
3193{
3194 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003195 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003196 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003197
3198 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3199 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003200 /* If we detected a read error don't try reading again. */
3201 if (channel->ch_to_be_closed)
3202 continue;
3203
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003204 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003205 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003206 {
3207 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003208 if (fd != INVALID_FD)
3209 {
3210 int r = channel_wait(channel, fd, 0);
3211
3212 if (r == CW_READY)
3213 channel_read(channel, part, "channel_handle_events");
3214 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003215 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003216 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003217 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003218 }
3219}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003220# endif
3221
Bram Moolenaard04a0202016-01-26 23:30:18 +01003222/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003223 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003224 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003225 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003226 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003227 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003228channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003229{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003230 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003231 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003232 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003233
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003234 fd = channel->ch_part[part].ch_fd;
3235 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003236 {
3237 if (!channel->ch_error && fun != NULL)
3238 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003239 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003240 EMSG2("E630: %s(): write while not connected", fun);
3241 }
3242 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003243 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003244 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003245
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003246 if (log_fd != NULL)
3247 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003248 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003249 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003250 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003251 fprintf(log_fd, "'\n");
3252 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003253 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003254 }
3255
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003256 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003257 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003258 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003259 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003260 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003261 {
3262 if (!channel->ch_error && fun != NULL)
3263 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003264 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003265 EMSG2("E631: %s(): write failed", fun);
3266 }
3267 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003268 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003269 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003270
3271 channel->ch_error = FALSE;
3272 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003273}
3274
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003275/*
3276 * Common for "ch_sendexpr()" and "ch_sendraw()".
3277 * Returns the channel if the caller should read the response.
3278 * Sets "part_read" to the the read fd.
3279 * Otherwise returns NULL.
3280 */
3281 channel_T *
3282send_common(
3283 typval_T *argvars,
3284 char_u *text,
3285 int id,
3286 int eval,
3287 jobopt_T *opt,
3288 char *fun,
3289 int *part_read)
3290{
3291 channel_T *channel;
3292 int part_send;
3293
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003294 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003295 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003296 if (channel == NULL)
3297 return NULL;
3298 part_send = channel_part_send(channel);
3299 *part_read = channel_part_read(channel);
3300
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003301 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3302 return NULL;
3303
3304 /* Set the callback. An empty callback means no callback and not reading
3305 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3306 * allowed. */
3307 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3308 {
3309 if (eval)
3310 {
3311 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3312 return NULL;
3313 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003314 channel_set_req_callback(channel, part_send,
3315 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003316 }
3317
3318 if (channel_send(channel, part_send, text, fun) == OK
3319 && opt->jo_callback == NULL)
3320 return channel;
3321 return NULL;
3322}
3323
3324/*
3325 * common for "ch_evalexpr()" and "ch_sendexpr()"
3326 */
3327 void
3328ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3329{
3330 char_u *text;
3331 typval_T *listtv;
3332 channel_T *channel;
3333 int id;
3334 ch_mode_T ch_mode;
3335 int part_send;
3336 int part_read;
3337 jobopt_T opt;
3338 int timeout;
3339
3340 /* return an empty string by default */
3341 rettv->v_type = VAR_STRING;
3342 rettv->vval.v_string = NULL;
3343
Bram Moolenaar437905c2016-04-26 19:01:05 +02003344 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003345 if (channel == NULL)
3346 return;
3347 part_send = channel_part_send(channel);
3348
3349 ch_mode = channel_get_mode(channel, part_send);
3350 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3351 {
3352 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3353 return;
3354 }
3355
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003356 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003357 text = json_encode_nr_expr(id, &argvars[1],
3358 ch_mode == MODE_JS ? JSON_JS : 0);
3359 if (text == NULL)
3360 return;
3361
3362 channel = send_common(argvars, text, id, eval, &opt,
3363 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3364 vim_free(text);
3365 if (channel != NULL && eval)
3366 {
3367 if (opt.jo_set & JO_TIMEOUT)
3368 timeout = opt.jo_timeout;
3369 else
3370 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003371 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3372 == OK)
3373 {
3374 list_T *list = listtv->vval.v_list;
3375
3376 /* Move the item from the list and then change the type to
3377 * avoid the value being freed. */
3378 *rettv = list->lv_last->li_tv;
3379 list->lv_last->li_tv.v_type = VAR_NUMBER;
3380 free_tv(listtv);
3381 }
3382 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003383 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003384}
3385
3386/*
3387 * common for "ch_evalraw()" and "ch_sendraw()"
3388 */
3389 void
3390ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3391{
3392 char_u buf[NUMBUFLEN];
3393 char_u *text;
3394 channel_T *channel;
3395 int part_read;
3396 jobopt_T opt;
3397 int timeout;
3398
3399 /* return an empty string by default */
3400 rettv->v_type = VAR_STRING;
3401 rettv->vval.v_string = NULL;
3402
3403 text = get_tv_string_buf(&argvars[1], buf);
3404 channel = send_common(argvars, text, 0, eval, &opt,
3405 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3406 if (channel != NULL && eval)
3407 {
3408 if (opt.jo_set & JO_TIMEOUT)
3409 timeout = opt.jo_timeout;
3410 else
3411 timeout = channel_get_timeout(channel, part_read);
3412 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3413 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003414 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003415}
3416
Bram Moolenaard04a0202016-01-26 23:30:18 +01003417# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003418/*
3419 * Add open channels to the poll struct.
3420 * Return the adjusted struct index.
3421 * The type of "fds" is hidden to avoid problems with the function proto.
3422 */
3423 int
3424channel_poll_setup(int nfd_in, void *fds_in)
3425{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003426 int nfd = nfd_in;
3427 channel_T *channel;
3428 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003429 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003430
Bram Moolenaar77073442016-02-13 23:23:53 +01003431 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003432 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003433 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003434 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003435 chanpart_T *ch_part = &channel->ch_part[part];
3436
3437 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003438 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003439 ch_part->ch_poll_idx = nfd;
3440 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003441 fds[nfd].events = POLLIN;
3442 nfd++;
3443 }
3444 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003445 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003446 }
3447 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003448
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003449 nfd = channel_fill_poll_write(nfd, fds);
3450
Bram Moolenaare0874f82016-01-24 20:36:41 +01003451 return nfd;
3452}
3453
3454/*
3455 * The type of "fds" is hidden to avoid problems with the function proto.
3456 */
3457 int
3458channel_poll_check(int ret_in, void *fds_in)
3459{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003460 int ret = ret_in;
3461 channel_T *channel;
3462 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003463 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003464 int idx;
3465 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003466
Bram Moolenaar77073442016-02-13 23:23:53 +01003467 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003468 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003469 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003470 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003471 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003472
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003473 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003474 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003475 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003476 --ret;
3477 }
3478 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003479
3480 in_part = &channel->ch_part[PART_IN];
3481 idx = in_part->ch_poll_idx;
3482 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3483 {
3484 if (in_part->ch_buf_append)
3485 {
3486 if (in_part->ch_buffer != NULL)
3487 channel_write_new_lines(in_part->ch_buffer);
3488 }
3489 else
3490 channel_write_in(channel);
3491 --ret;
3492 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003493 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003494
3495 return ret;
3496}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003497# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003498
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003499# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003500/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003501 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003502 */
3503 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003504channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003505{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003506 int maxfd = maxfd_in;
3507 channel_T *channel;
3508 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003509 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003510 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003511
Bram Moolenaar77073442016-02-13 23:23:53 +01003512 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003513 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003514 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003515 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003516 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003517
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003518 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003519 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003520 FD_SET((int)fd, rfds);
3521 if (maxfd < (int)fd)
3522 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003523 }
3524 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003525 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003526
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003527 maxfd = channel_fill_wfds(maxfd, wfds);
3528
Bram Moolenaare0874f82016-01-24 20:36:41 +01003529 return maxfd;
3530}
3531
3532/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003533 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003534 */
3535 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003536channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003537{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003538 int ret = ret_in;
3539 channel_T *channel;
3540 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003541 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003542 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003543 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003544
Bram Moolenaar77073442016-02-13 23:23:53 +01003545 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003546 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003547 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003548 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003549 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003550
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003551 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003552 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003553 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003554 --ret;
3555 }
3556 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003557
3558 in_part = &channel->ch_part[PART_IN];
3559 if (ret > 0 && in_part->ch_fd != INVALID_FD
3560 && FD_ISSET(in_part->ch_fd, wfds))
3561 {
3562 if (in_part->ch_buf_append)
3563 {
3564 if (in_part->ch_buffer != NULL)
3565 channel_write_new_lines(in_part->ch_buffer);
3566 }
3567 else
3568 channel_write_in(channel);
3569 --ret;
3570 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003571 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003572
3573 return ret;
3574}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003575# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003576
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003577/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003578 * Execute queued up commands.
3579 * Invoked from the main loop when it's safe to execute received commands.
3580 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003581 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003582 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003583channel_parse_messages(void)
3584{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003585 channel_T *channel = first_channel;
3586 int ret = FALSE;
3587 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003588 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003589
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003590 ++safe_to_invoke_callback;
3591
Bram Moolenaard0b65022016-03-06 21:50:33 +01003592 /* Only do this message when another message was given, otherwise we get
3593 * lots of them. */
3594 if (did_log_msg)
3595 {
3596 ch_log(NULL, "looking for messages on channels");
3597 did_log_msg = FALSE;
3598 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003599 while (channel != NULL)
3600 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003601 if (channel->ch_to_be_closed)
3602 {
3603 channel->ch_to_be_closed = FALSE;
3604 channel_close_now(channel);
3605 /* channel may have been freed, start over */
3606 channel = first_channel;
3607 continue;
3608 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003609 if (channel->ch_to_be_freed)
3610 {
3611 channel_free(channel);
3612 /* channel has been freed, start over */
3613 channel = first_channel;
3614 continue;
3615 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003616 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003617 {
3618 /* channel is no longer useful, free it */
3619 channel_free(channel);
3620 channel = first_channel;
3621 part = PART_SOCK;
3622 continue;
3623 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003624 if (channel->ch_part[part].ch_fd != INVALID_FD
3625 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003626 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003627 /* Increase the refcount, in case the handler causes the channel
3628 * to be unreferenced or closed. */
3629 ++channel->ch_refcount;
3630 r = may_invoke_callback(channel, part);
3631 if (r == OK)
3632 ret = TRUE;
3633 if (channel_unref(channel) || r == OK)
3634 {
3635 /* channel was freed or something was done, start over */
3636 channel = first_channel;
3637 part = PART_SOCK;
3638 continue;
3639 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003640 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003641 if (part < PART_ERR)
3642 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003643 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003644 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003645 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003646 part = PART_SOCK;
3647 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003648 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003649
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003650 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003651 {
3652 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003653 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003654 }
3655
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003656 --safe_to_invoke_callback;
3657
Bram Moolenaard7ece102016-02-02 23:23:02 +01003658 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003659}
3660
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003661/*
3662 * Mark references to lists used in channels.
3663 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003664 int
3665set_ref_in_channel(int copyID)
3666{
Bram Moolenaar77073442016-02-13 23:23:53 +01003667 int abort = FALSE;
3668 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003669 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003670
Bram Moolenaar77073442016-02-13 23:23:53 +01003671 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003672 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003673 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003674 tv.v_type = VAR_CHANNEL;
3675 tv.vval.v_channel = channel;
3676 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003677 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003678 return abort;
3679}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003680
3681/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003682 * Return the "part" to write to for "channel".
3683 */
3684 int
3685channel_part_send(channel_T *channel)
3686{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003687 if (channel->CH_SOCK_FD == INVALID_FD)
3688 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003689 return PART_SOCK;
3690}
3691
3692/*
3693 * Return the default "part" to read from for "channel".
3694 */
3695 int
3696channel_part_read(channel_T *channel)
3697{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003698 if (channel->CH_SOCK_FD == INVALID_FD)
3699 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003700 return PART_SOCK;
3701}
3702
3703/*
3704 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003705 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003706 */
3707 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003708channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003709{
Bram Moolenaar77073442016-02-13 23:23:53 +01003710 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003711 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003712 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003713}
3714
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003715/*
3716 * Return the timeout of "channel"/"part"
3717 */
3718 int
3719channel_get_timeout(channel_T *channel, int part)
3720{
3721 return channel->ch_part[part].ch_timeout;
3722}
3723
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003724 static int
3725handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3726{
3727 char_u *val = get_tv_string(item);
3728
3729 opt->jo_set |= jo;
3730 if (STRCMP(val, "nl") == 0)
3731 *modep = MODE_NL;
3732 else if (STRCMP(val, "raw") == 0)
3733 *modep = MODE_RAW;
3734 else if (STRCMP(val, "js") == 0)
3735 *modep = MODE_JS;
3736 else if (STRCMP(val, "json") == 0)
3737 *modep = MODE_JSON;
3738 else
3739 {
3740 EMSG2(_(e_invarg2), val);
3741 return FAIL;
3742 }
3743 return OK;
3744}
3745
3746 static int
3747handle_io(typval_T *item, int part, jobopt_T *opt)
3748{
3749 char_u *val = get_tv_string(item);
3750
3751 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3752 if (STRCMP(val, "null") == 0)
3753 opt->jo_io[part] = JIO_NULL;
3754 else if (STRCMP(val, "pipe") == 0)
3755 opt->jo_io[part] = JIO_PIPE;
3756 else if (STRCMP(val, "file") == 0)
3757 opt->jo_io[part] = JIO_FILE;
3758 else if (STRCMP(val, "buffer") == 0)
3759 opt->jo_io[part] = JIO_BUFFER;
3760 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3761 opt->jo_io[part] = JIO_OUT;
3762 else
3763 {
3764 EMSG2(_(e_invarg2), val);
3765 return FAIL;
3766 }
3767 return OK;
3768}
3769
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003770/*
3771 * Clear a jobopt_T before using it.
3772 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003773 void
3774clear_job_options(jobopt_T *opt)
3775{
3776 vim_memset(opt, 0, sizeof(jobopt_T));
3777}
3778
3779/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003780 * Free any members of a jobopt_T.
3781 */
3782 void
3783free_job_options(jobopt_T *opt)
3784{
3785 if (opt->jo_partial != NULL)
3786 partial_unref(opt->jo_partial);
3787 if (opt->jo_out_partial != NULL)
3788 partial_unref(opt->jo_out_partial);
3789 if (opt->jo_err_partial != NULL)
3790 partial_unref(opt->jo_err_partial);
3791 if (opt->jo_close_partial != NULL)
3792 partial_unref(opt->jo_close_partial);
3793}
3794
3795/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003796 * Get the PART_ number from the first character of an option name.
3797 */
3798 static int
3799part_from_char(int c)
3800{
3801 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3802}
3803
3804/*
3805 * Get the option entries from the dict in "tv", parse them and put the result
3806 * in "opt".
3807 * Only accept options in "supported".
3808 * If an option value is invalid return FAIL.
3809 */
3810 int
3811get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3812{
3813 typval_T *item;
3814 char_u *val;
3815 dict_T *dict;
3816 int todo;
3817 hashitem_T *hi;
3818 int part;
3819
3820 opt->jo_set = 0;
3821 if (tv->v_type == VAR_UNKNOWN)
3822 return OK;
3823 if (tv->v_type != VAR_DICT)
3824 {
3825 EMSG(_(e_invarg));
3826 return FAIL;
3827 }
3828 dict = tv->vval.v_dict;
3829 if (dict == NULL)
3830 return OK;
3831
3832 todo = (int)dict->dv_hashtab.ht_used;
3833 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3834 if (!HASHITEM_EMPTY(hi))
3835 {
3836 item = &dict_lookup(hi)->di_tv;
3837
3838 if (STRCMP(hi->hi_key, "mode") == 0)
3839 {
3840 if (!(supported & JO_MODE))
3841 break;
3842 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3843 return FAIL;
3844 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003845 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003846 {
3847 if (!(supported & JO_IN_MODE))
3848 break;
3849 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3850 == FAIL)
3851 return FAIL;
3852 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003853 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003854 {
3855 if (!(supported & JO_OUT_MODE))
3856 break;
3857 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3858 == FAIL)
3859 return FAIL;
3860 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003861 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003862 {
3863 if (!(supported & JO_ERR_MODE))
3864 break;
3865 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3866 == FAIL)
3867 return FAIL;
3868 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003869 else if (STRCMP(hi->hi_key, "in_io") == 0
3870 || STRCMP(hi->hi_key, "out_io") == 0
3871 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003872 {
3873 if (!(supported & JO_OUT_IO))
3874 break;
3875 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3876 return FAIL;
3877 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003878 else if (STRCMP(hi->hi_key, "in_name") == 0
3879 || STRCMP(hi->hi_key, "out_name") == 0
3880 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003881 {
3882 part = part_from_char(*hi->hi_key);
3883
3884 if (!(supported & JO_OUT_IO))
3885 break;
3886 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3887 opt->jo_io_name[part] =
3888 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3889 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003890 else if (STRCMP(hi->hi_key, "in_buf") == 0
3891 || STRCMP(hi->hi_key, "out_buf") == 0
3892 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003893 {
3894 part = part_from_char(*hi->hi_key);
3895
3896 if (!(supported & JO_OUT_IO))
3897 break;
3898 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3899 opt->jo_io_buf[part] = get_tv_number(item);
3900 if (opt->jo_io_buf[part] <= 0)
3901 {
3902 EMSG2(_(e_invarg2), get_tv_string(item));
3903 return FAIL;
3904 }
3905 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3906 {
3907 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3908 return FAIL;
3909 }
3910 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003911 else if (STRCMP(hi->hi_key, "in_top") == 0
3912 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003913 {
3914 linenr_T *lp;
3915
3916 if (!(supported & JO_OUT_IO))
3917 break;
3918 if (hi->hi_key[3] == 't')
3919 {
3920 lp = &opt->jo_in_top;
3921 opt->jo_set |= JO_IN_TOP;
3922 }
3923 else
3924 {
3925 lp = &opt->jo_in_bot;
3926 opt->jo_set |= JO_IN_BOT;
3927 }
3928 *lp = get_tv_number(item);
3929 if (*lp < 0)
3930 {
3931 EMSG2(_(e_invarg2), get_tv_string(item));
3932 return FAIL;
3933 }
3934 }
3935 else if (STRCMP(hi->hi_key, "channel") == 0)
3936 {
3937 if (!(supported & JO_OUT_IO))
3938 break;
3939 opt->jo_set |= JO_CHANNEL;
3940 if (item->v_type != VAR_CHANNEL)
3941 {
3942 EMSG2(_(e_invarg2), "channel");
3943 return FAIL;
3944 }
3945 opt->jo_channel = item->vval.v_channel;
3946 }
3947 else if (STRCMP(hi->hi_key, "callback") == 0)
3948 {
3949 if (!(supported & JO_CALLBACK))
3950 break;
3951 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003952 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003953 if (opt->jo_callback == NULL)
3954 {
3955 EMSG2(_(e_invarg2), "callback");
3956 return FAIL;
3957 }
3958 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003959 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003960 {
3961 if (!(supported & JO_OUT_CALLBACK))
3962 break;
3963 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003964 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003965 if (opt->jo_out_cb == NULL)
3966 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003967 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003968 return FAIL;
3969 }
3970 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003971 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003972 {
3973 if (!(supported & JO_ERR_CALLBACK))
3974 break;
3975 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003976 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003977 if (opt->jo_err_cb == NULL)
3978 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003979 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003980 return FAIL;
3981 }
3982 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003983 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003984 {
3985 if (!(supported & JO_CLOSE_CALLBACK))
3986 break;
3987 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003988 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003989 if (opt->jo_close_cb == NULL)
3990 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003991 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003992 return FAIL;
3993 }
3994 }
3995 else if (STRCMP(hi->hi_key, "waittime") == 0)
3996 {
3997 if (!(supported & JO_WAITTIME))
3998 break;
3999 opt->jo_set |= JO_WAITTIME;
4000 opt->jo_waittime = get_tv_number(item);
4001 }
4002 else if (STRCMP(hi->hi_key, "timeout") == 0)
4003 {
4004 if (!(supported & JO_TIMEOUT))
4005 break;
4006 opt->jo_set |= JO_TIMEOUT;
4007 opt->jo_timeout = get_tv_number(item);
4008 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004009 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004010 {
4011 if (!(supported & JO_OUT_TIMEOUT))
4012 break;
4013 opt->jo_set |= JO_OUT_TIMEOUT;
4014 opt->jo_out_timeout = get_tv_number(item);
4015 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004016 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004017 {
4018 if (!(supported & JO_ERR_TIMEOUT))
4019 break;
4020 opt->jo_set |= JO_ERR_TIMEOUT;
4021 opt->jo_err_timeout = get_tv_number(item);
4022 }
4023 else if (STRCMP(hi->hi_key, "part") == 0)
4024 {
4025 if (!(supported & JO_PART))
4026 break;
4027 opt->jo_set |= JO_PART;
4028 val = get_tv_string(item);
4029 if (STRCMP(val, "err") == 0)
4030 opt->jo_part = PART_ERR;
4031 else
4032 {
4033 EMSG2(_(e_invarg2), val);
4034 return FAIL;
4035 }
4036 }
4037 else if (STRCMP(hi->hi_key, "id") == 0)
4038 {
4039 if (!(supported & JO_ID))
4040 break;
4041 opt->jo_set |= JO_ID;
4042 opt->jo_id = get_tv_number(item);
4043 }
4044 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4045 {
4046 if (!(supported & JO_STOPONEXIT))
4047 break;
4048 opt->jo_set |= JO_STOPONEXIT;
4049 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4050 opt->jo_soe_buf);
4051 if (opt->jo_stoponexit == NULL)
4052 {
4053 EMSG2(_(e_invarg2), "stoponexit");
4054 return FAIL;
4055 }
4056 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004057 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004058 {
4059 if (!(supported & JO_EXIT_CB))
4060 break;
4061 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004062 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
4063 {
4064 opt->jo_exit_partial = item->vval.v_partial;
4065 opt->jo_exit_cb = item->vval.v_partial->pt_name;
4066 }
4067 else
4068 opt->jo_exit_cb = get_tv_string_buf_chk(
4069 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004070 if (opt->jo_exit_cb == NULL)
4071 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004072 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004073 return FAIL;
4074 }
4075 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004076 else if (STRCMP(hi->hi_key, "block_write") == 0)
4077 {
4078 if (!(supported & JO_BLOCK_WRITE))
4079 break;
4080 opt->jo_set |= JO_BLOCK_WRITE;
4081 opt->jo_block_write = get_tv_number(item);
4082 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004083 else
4084 break;
4085 --todo;
4086 }
4087 if (todo > 0)
4088 {
4089 EMSG2(_(e_invarg2), hi->hi_key);
4090 return FAIL;
4091 }
4092
4093 return OK;
4094}
4095
4096/*
4097 * Get the channel from the argument.
4098 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004099 * When "check_open" is TRUE check that the channel can be used.
4100 * When "reading" is TRUE "check_open" considers typeahead useful.
4101 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004102 */
4103 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004104get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004105{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004106 channel_T *channel = NULL;
4107 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004108
4109 if (tv->v_type == VAR_JOB)
4110 {
4111 if (tv->vval.v_job != NULL)
4112 channel = tv->vval.v_job->jv_channel;
4113 }
4114 else if (tv->v_type == VAR_CHANNEL)
4115 {
4116 channel = tv->vval.v_channel;
4117 }
4118 else
4119 {
4120 EMSG2(_(e_invarg2), get_tv_string(tv));
4121 return NULL;
4122 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004123 if (channel != NULL && reading)
4124 has_readahead = channel_has_readahead(channel,
4125 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004126
Bram Moolenaar437905c2016-04-26 19:01:05 +02004127 if (check_open && (channel == NULL || (!channel_is_open(channel)
4128 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004129 {
4130 EMSG(_("E906: not an open channel"));
4131 return NULL;
4132 }
4133 return channel;
4134}
4135
4136static job_T *first_job = NULL;
4137
4138 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004139job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004140{
4141 ch_log(job->jv_channel, "Freeing job");
4142 if (job->jv_channel != NULL)
4143 {
4144 /* The link from the channel to the job doesn't count as a reference,
4145 * thus don't decrement the refcount of the job. The reference from
4146 * the job to the channel does count the refrence, decrement it and
4147 * NULL the reference. We don't set ch_job_killed, unreferencing the
4148 * job doesn't mean it stops running. */
4149 job->jv_channel->ch_job = NULL;
4150 channel_unref(job->jv_channel);
4151 }
4152 mch_clear_job(job);
4153
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004154 vim_free(job->jv_stoponexit);
4155 vim_free(job->jv_exit_cb);
4156 partial_unref(job->jv_exit_partial);
4157}
4158
4159 static void
4160job_free_job(job_T *job)
4161{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004162 if (job->jv_next != NULL)
4163 job->jv_next->jv_prev = job->jv_prev;
4164 if (job->jv_prev == NULL)
4165 first_job = job->jv_next;
4166 else
4167 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004168 vim_free(job);
4169}
4170
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004171 static void
4172job_free(job_T *job)
4173{
4174 if (!in_free_unref_items)
4175 {
4176 job_free_contents(job);
4177 job_free_job(job);
4178 }
4179}
4180
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004181/*
4182 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004183 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4184 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004185 */
4186 static int
4187job_still_useful(job_T *job)
4188{
4189 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004190 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4191 || (job->jv_channel != NULL
4192 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004193}
4194
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004195/*
4196 * Mark references in jobs that are still useful.
4197 */
4198 int
4199set_ref_in_job(int copyID)
4200{
4201 int abort = FALSE;
4202 job_T *job;
4203 typval_T tv;
4204
4205 for (job = first_job; job != NULL; job = job->jv_next)
4206 if (job_still_useful(job))
4207 {
4208 tv.v_type = VAR_JOB;
4209 tv.vval.v_job = job;
4210 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4211 }
4212 return abort;
4213}
4214
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004215 void
4216job_unref(job_T *job)
4217{
4218 if (job != NULL && --job->jv_refcount <= 0)
4219 {
4220 /* Do not free the job when it has not ended yet and there is a
4221 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004222 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004223 {
4224 job_free(job);
4225 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004226 else if (job->jv_channel != NULL
4227 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004228 {
4229 /* Do remove the link to the channel, otherwise it hangs
4230 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004231 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004232 job->jv_channel->ch_job = NULL;
4233 channel_unref(job->jv_channel);
4234 job->jv_channel = NULL;
4235 }
4236 }
4237}
4238
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004239 int
4240free_unused_jobs_contents(int copyID, int mask)
4241{
4242 int did_free = FALSE;
4243 job_T *job;
4244
4245 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004246 if ((job->jv_copyID & mask) != (copyID & mask)
4247 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004248 {
4249 /* Free the channel and ordinary items it contains, but don't
4250 * recurse into Lists, Dictionaries etc. */
4251 job_free_contents(job);
4252 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004253 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004254 return did_free;
4255}
4256
4257 void
4258free_unused_jobs(int copyID, int mask)
4259{
4260 job_T *job;
4261 job_T *job_next;
4262
4263 for (job = first_job; job != NULL; job = job_next)
4264 {
4265 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004266 if ((job->jv_copyID & mask) != (copyID & mask)
4267 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004268 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004269 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004270 job_free_job(job);
4271 }
4272 }
4273}
4274
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004275/*
4276 * Allocate a job. Sets the refcount to one and sets options default.
4277 */
4278 static job_T *
4279job_alloc(void)
4280{
4281 job_T *job;
4282
4283 job = (job_T *)alloc_clear(sizeof(job_T));
4284 if (job != NULL)
4285 {
4286 job->jv_refcount = 1;
4287 job->jv_stoponexit = vim_strsave((char_u *)"term");
4288
4289 if (first_job != NULL)
4290 {
4291 first_job->jv_prev = job;
4292 job->jv_next = first_job;
4293 }
4294 first_job = job;
4295 }
4296 return job;
4297}
4298
4299 void
4300job_set_options(job_T *job, jobopt_T *opt)
4301{
4302 if (opt->jo_set & JO_STOPONEXIT)
4303 {
4304 vim_free(job->jv_stoponexit);
4305 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4306 job->jv_stoponexit = NULL;
4307 else
4308 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4309 }
4310 if (opt->jo_set & JO_EXIT_CB)
4311 {
4312 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004313 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004314 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004315 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004316 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004317 job->jv_exit_partial = NULL;
4318 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004319 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004320 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004321 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004322 job->jv_exit_partial = opt->jo_exit_partial;
4323 if (job->jv_exit_partial != NULL)
4324 ++job->jv_exit_partial->pt_refcount;
4325 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004326 }
4327}
4328
4329/*
4330 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4331 */
4332 void
4333job_stop_on_exit()
4334{
4335 job_T *job;
4336
4337 for (job = first_job; job != NULL; job = job->jv_next)
4338 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4339 mch_stop_job(job, job->jv_stoponexit);
4340}
4341
4342/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004343 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004344 */
4345 void
4346job_check_ended(void)
4347{
4348 static time_t last_check = 0;
4349 time_t now;
4350 job_T *job;
4351 job_T *next;
4352
4353 /* Only do this once in 10 seconds. */
4354 now = time(NULL);
4355 if (last_check + 10 < now)
4356 {
4357 last_check = now;
4358 for (job = first_job; job != NULL; job = next)
4359 {
4360 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004361 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004362 job_status(job); /* may free "job" */
4363 }
4364 }
4365}
4366
4367/*
4368 * "job_start()" function
4369 */
4370 job_T *
4371job_start(typval_T *argvars)
4372{
4373 job_T *job;
4374 char_u *cmd = NULL;
4375#if defined(UNIX)
4376# define USE_ARGV
4377 char **argv = NULL;
4378 int argc = 0;
4379#else
4380 garray_T ga;
4381#endif
4382 jobopt_T opt;
4383 int part;
4384
4385 job = job_alloc();
4386 if (job == NULL)
4387 return NULL;
4388
4389 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004390#ifndef USE_ARGV
4391 ga_init2(&ga, (int)sizeof(char*), 20);
4392#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004393
4394 /* Default mode is NL. */
4395 clear_job_options(&opt);
4396 opt.jo_mode = MODE_NL;
4397 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004398 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4399 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004400 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004401
4402 /* Check that when io is "file" that there is a file name. */
4403 for (part = PART_OUT; part <= PART_IN; ++part)
4404 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4405 && opt.jo_io[part] == JIO_FILE
4406 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4407 || *opt.jo_io_name[part] == NUL))
4408 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004409 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004410 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004411 }
4412
4413 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4414 {
4415 buf_T *buf = NULL;
4416
4417 /* check that we can find the buffer before starting the job */
4418 if (opt.jo_set & JO_IN_BUF)
4419 {
4420 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4421 if (buf == NULL)
4422 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4423 }
4424 else if (!(opt.jo_set & JO_IN_NAME))
4425 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004426 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004427 }
4428 else
4429 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4430 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004431 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004432 if (buf->b_ml.ml_mfp == NULL)
4433 {
4434 char_u numbuf[NUMBUFLEN];
4435 char_u *s;
4436
4437 if (opt.jo_set & JO_IN_BUF)
4438 {
4439 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4440 s = numbuf;
4441 }
4442 else
4443 s = opt.jo_io_name[PART_IN];
4444 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004445 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004446 }
4447 job->jv_in_buf = buf;
4448 }
4449
4450 job_set_options(job, &opt);
4451
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004452 if (argvars[0].v_type == VAR_STRING)
4453 {
4454 /* Command is a string. */
4455 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004456 if (cmd == NULL || *cmd == NUL)
4457 {
4458 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004459 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004460 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004461#ifdef USE_ARGV
4462 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004463 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004464 argv[argc] = NULL;
4465#endif
4466 }
4467 else if (argvars[0].v_type != VAR_LIST
4468 || argvars[0].vval.v_list == NULL
4469 || argvars[0].vval.v_list->lv_len < 1)
4470 {
4471 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004472 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004473 }
4474 else
4475 {
4476 list_T *l = argvars[0].vval.v_list;
4477 listitem_T *li;
4478 char_u *s;
4479
4480#ifdef USE_ARGV
4481 /* Pass argv[] to mch_call_shell(). */
4482 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4483 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004484 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004485#endif
4486 for (li = l->lv_first; li != NULL; li = li->li_next)
4487 {
4488 s = get_tv_string_chk(&li->li_tv);
4489 if (s == NULL)
4490 goto theend;
4491#ifdef USE_ARGV
4492 argv[argc++] = (char *)s;
4493#else
4494 /* Only escape when needed, double quotes are not always allowed. */
4495 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4496 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004497# ifdef WIN32
4498 int old_ssl = p_ssl;
4499
4500 /* This is using CreateProcess, not cmd.exe. Always use
4501 * double quote and backslashes. */
4502 p_ssl = 0;
4503# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004504 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004505# ifdef WIN32
4506 p_ssl = old_ssl;
4507# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004508 if (s == NULL)
4509 goto theend;
4510 ga_concat(&ga, s);
4511 vim_free(s);
4512 }
4513 else
4514 ga_concat(&ga, s);
4515 if (li->li_next != NULL)
4516 ga_append(&ga, ' ');
4517#endif
4518 }
4519#ifdef USE_ARGV
4520 argv[argc] = NULL;
4521#else
4522 cmd = ga.ga_data;
4523#endif
4524 }
4525
4526#ifdef USE_ARGV
4527 if (ch_log_active())
4528 {
4529 garray_T ga;
4530 int i;
4531
4532 ga_init2(&ga, (int)sizeof(char), 200);
4533 for (i = 0; i < argc; ++i)
4534 {
4535 if (i > 0)
4536 ga_concat(&ga, (char_u *)" ");
4537 ga_concat(&ga, (char_u *)argv[i]);
4538 }
4539 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4540 ga_clear(&ga);
4541 }
4542 mch_start_job(argv, job, &opt);
4543#else
4544 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4545 mch_start_job((char *)cmd, job, &opt);
4546#endif
4547
4548 /* If the channel is reading from a buffer, write lines now. */
4549 if (job->jv_channel != NULL)
4550 channel_write_in(job->jv_channel);
4551
4552theend:
4553#ifdef USE_ARGV
4554 vim_free(argv);
4555#else
4556 vim_free(ga.ga_data);
4557#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004558 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004559 return job;
4560}
4561
4562/*
4563 * Get the status of "job" and invoke the exit callback when needed.
4564 * The returned string is not allocated.
4565 */
4566 char *
4567job_status(job_T *job)
4568{
4569 char *result;
4570
4571 if (job->jv_status == JOB_ENDED)
4572 /* No need to check, dead is dead. */
4573 result = "dead";
4574 else if (job->jv_status == JOB_FAILED)
4575 result = "fail";
4576 else
4577 {
4578 result = mch_job_status(job);
4579 if (job->jv_status == JOB_ENDED)
4580 ch_log(job->jv_channel, "Job ended");
4581 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4582 {
4583 typval_T argv[3];
4584 typval_T rettv;
4585 int dummy;
4586
4587 /* invoke the exit callback; make sure the refcount is > 0 */
4588 ++job->jv_refcount;
4589 argv[0].v_type = VAR_JOB;
4590 argv[0].vval.v_job = job;
4591 argv[1].v_type = VAR_NUMBER;
4592 argv[1].vval.v_number = job->jv_exitval;
4593 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004594 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4595 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004596 clear_tv(&rettv);
4597 --job->jv_refcount;
4598 }
4599 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4600 {
4601 /* The job was already unreferenced, now that it ended it can be
4602 * freed. Careful: caller must not use "job" after this! */
4603 job_free(job);
4604 }
4605 }
4606 return result;
4607}
4608
Bram Moolenaar8950a562016-03-12 15:22:55 +01004609/*
4610 * Implementation of job_info().
4611 */
4612 void
4613job_info(job_T *job, dict_T *dict)
4614{
4615 dictitem_T *item;
4616 varnumber_T nr;
4617
4618 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4619
4620 item = dictitem_alloc((char_u *)"channel");
4621 if (item == NULL)
4622 return;
4623 item->di_tv.v_lock = 0;
4624 item->di_tv.v_type = VAR_CHANNEL;
4625 item->di_tv.vval.v_channel = job->jv_channel;
4626 if (job->jv_channel != NULL)
4627 ++job->jv_channel->ch_refcount;
4628 if (dict_add(dict, item) == FAIL)
4629 dictitem_free(item);
4630
4631#ifdef UNIX
4632 nr = job->jv_pid;
4633#else
4634 nr = job->jv_proc_info.dwProcessId;
4635#endif
4636 dict_add_nr_str(dict, "process", nr, NULL);
4637
4638 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004639 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004640 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4641}
4642
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004643 int
4644job_stop(job_T *job, typval_T *argvars)
4645{
4646 char_u *arg;
4647
4648 if (argvars[1].v_type == VAR_UNKNOWN)
4649 arg = (char_u *)"";
4650 else
4651 {
4652 arg = get_tv_string_chk(&argvars[1]);
4653 if (arg == NULL)
4654 {
4655 EMSG(_(e_invarg));
4656 return 0;
4657 }
4658 }
4659 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4660 if (mch_stop_job(job, arg) == FAIL)
4661 return 0;
4662
4663 /* Assume that "hup" does not kill the job. */
4664 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4665 job->jv_channel->ch_job_killed = TRUE;
4666
4667 /* We don't try freeing the job, obviously the caller still has a
4668 * reference to it. */
4669 return 1;
4670}
4671
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004672#endif /* FEAT_JOB_CHANNEL */