blob: 973d234eac549db0e03ab170b24c2cd99b44222d [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 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001212 if (opt->jo_set & JO_OUT_MODIFIABLE)
1213 channel->ch_part[PART_OUT].ch_nomodifiable =
1214 !opt->jo_modifiable[PART_OUT];
1215
1216 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1217 {
1218 EMSG(_(e_modifiable));
1219 }
1220 else
1221 {
1222 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001223 (char *)buf->b_ffname);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001224 channel->ch_part[PART_OUT].ch_buffer = buf;
1225 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001226 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001227 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001228
1229 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1230 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1231 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1232 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001233 buf_T *buf;
1234
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001235 /* writing err to a buffer. Default mode is NL. */
1236 if (!(opt->jo_set & JO_ERR_MODE))
1237 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1238 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001239 buf = channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001240 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001241 {
1242 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1243 if (buf == NULL)
1244 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1245 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001246 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001247 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1248 if (buf != NULL)
1249 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001250 if (opt->jo_set & JO_ERR_MODIFIABLE)
1251 channel->ch_part[PART_ERR].ch_nomodifiable =
1252 !opt->jo_modifiable[PART_ERR];
1253 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1254 {
1255 EMSG(_(e_modifiable));
1256 }
1257 else
1258 {
1259 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001260 (char *)buf->b_ffname);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001261 channel->ch_part[PART_ERR].ch_buffer = buf;
1262 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001263 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001264 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001265
1266 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1267 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1268 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001269}
1270
1271/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001272 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001273 */
1274 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001275channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001276 channel_T *channel,
1277 int part,
1278 char_u *callback,
1279 partial_T *partial,
1280 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001281{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001282 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001283 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1284
1285 if (item != NULL)
1286 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001287 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001288 item->cq_partial = partial;
1289 if (partial != NULL)
1290 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001291 item->cq_seq_nr = id;
1292 item->cq_prev = head->cq_prev;
1293 head->cq_prev = item;
1294 item->cq_next = NULL;
1295 if (item->cq_prev == NULL)
1296 head->cq_next = item;
1297 else
1298 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001299 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001300}
1301
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001302 static void
1303write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1304{
1305 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001306 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001307 char_u *p;
1308
Bram Moolenaar655da312016-05-28 22:22:34 +02001309 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001310 if ((p = alloc(len + 2)) == NULL)
1311 return;
1312 STRCPY(p, line);
1313 p[len] = NL;
1314 p[len + 1] = NUL;
1315 channel_send(channel, PART_IN, p, "write_buf_line()");
1316 vim_free(p);
1317}
1318
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001319/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001320 * Return TRUE if "channel" can be written to.
1321 * Returns FALSE if the input is closed or the write would block.
1322 */
1323 static int
1324can_write_buf_line(channel_T *channel)
1325{
1326 chanpart_T *in_part = &channel->ch_part[PART_IN];
1327
1328 if (in_part->ch_fd == INVALID_FD)
1329 return FALSE; /* pipe was closed */
1330
1331 /* for testing: block every other attempt to write */
1332 if (in_part->ch_block_write == 1)
1333 in_part->ch_block_write = -1;
1334 else if (in_part->ch_block_write == -1)
1335 in_part->ch_block_write = 1;
1336
1337 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1338#ifndef WIN32
1339 {
1340# if defined(HAVE_SELECT)
1341 struct timeval tval;
1342 fd_set wfds;
1343 int ret;
1344
1345 FD_ZERO(&wfds);
1346 FD_SET((int)in_part->ch_fd, &wfds);
1347 tval.tv_sec = 0;
1348 tval.tv_usec = 0;
1349 for (;;)
1350 {
1351 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1352# ifdef EINTR
1353 SOCK_ERRNO;
1354 if (ret == -1 && errno == EINTR)
1355 continue;
1356# endif
1357 if (ret <= 0 || in_part->ch_block_write == 1)
1358 {
1359 if (ret > 0)
1360 ch_log(channel, "FAKED Input not ready for writing");
1361 else
1362 ch_log(channel, "Input not ready for writing");
1363 return FALSE;
1364 }
1365 break;
1366 }
1367# else
1368 struct pollfd fds;
1369
1370 fds.fd = in_part->ch_fd;
1371 fds.events = POLLOUT;
1372 if (poll(&fds, 1, 0) <= 0)
1373 {
1374 ch_log(channel, "Input not ready for writing");
1375 return FALSE;
1376 }
1377 if (in_part->ch_block_write == 1)
1378 {
1379 ch_log(channel, "FAKED Input not ready for writing");
1380 return FALSE;
1381 }
1382# endif
1383 }
1384#endif
1385 return TRUE;
1386}
1387
1388/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001389 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001390 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001391 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001392channel_write_in(channel_T *channel)
1393{
1394 chanpart_T *in_part = &channel->ch_part[PART_IN];
1395 linenr_T lnum;
1396 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001397 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001398
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001399 if (buf == NULL || in_part->ch_buf_append)
1400 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001401 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1402 {
1403 /* buffer was wiped out or unloaded */
1404 in_part->ch_buffer = NULL;
1405 return;
1406 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001407
1408 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1409 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1410 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001411 if (!can_write_buf_line(channel))
1412 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001413 write_buf_line(buf, lnum, channel);
1414 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001415 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001416
1417 if (written == 1)
1418 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1419 else if (written > 1)
1420 ch_logn(channel, "written %d lines to channel", written);
1421
Bram Moolenaar014069a2016-03-03 22:51:40 +01001422 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001423 if (lnum > buf->b_ml.ml_line_count)
1424 {
1425 /* Writing is done, no longer need the buffer. */
1426 in_part->ch_buffer = NULL;
1427 ch_log(channel, "Finished writing all lines to channel");
1428 }
1429 else
1430 ch_logn(channel, "Still %d more lines to write",
1431 buf->b_ml.ml_line_count - lnum + 1);
1432}
1433
1434/*
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001435 * Handle buffer "buf" beeing freed, remove it from any channels.
1436 */
1437 void
1438channel_buffer_free(buf_T *buf)
1439{
1440 channel_T *channel;
1441 int part;
1442
1443 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1444 for (part = PART_SOCK; part <= PART_IN; ++part)
1445 {
1446 chanpart_T *ch_part = &channel->ch_part[part];
1447
1448 if (ch_part->ch_buffer == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001449 {
1450 ch_logs(channel, "%s buffer has been wiped out",
1451 part_names[part]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001452 ch_part->ch_buffer = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001453 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001454 }
1455}
1456
1457/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001458 * Write any lines waiting to be written to a channel.
1459 */
1460 void
1461channel_write_any_lines()
1462{
1463 channel_T *channel;
1464
1465 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1466 {
1467 chanpart_T *in_part = &channel->ch_part[PART_IN];
1468
1469 if (in_part->ch_buffer != NULL)
1470 {
1471 if (in_part->ch_buf_append)
1472 channel_write_new_lines(in_part->ch_buffer);
1473 else
1474 channel_write_in(channel);
1475 }
1476 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001477}
1478
1479/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001480 * Write appended lines above the last one in "buf" to the channel.
1481 */
1482 void
1483channel_write_new_lines(buf_T *buf)
1484{
1485 channel_T *channel;
1486 int found_one = FALSE;
1487
1488 /* There could be more than one channel for the buffer, loop over all of
1489 * them. */
1490 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1491 {
1492 chanpart_T *in_part = &channel->ch_part[PART_IN];
1493 linenr_T lnum;
1494 int written = 0;
1495
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001496 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001497 {
1498 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001499 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001500 found_one = TRUE;
1501 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1502 ++lnum)
1503 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001504 if (!can_write_buf_line(channel))
1505 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001506 write_buf_line(buf, lnum, channel);
1507 ++written;
1508 }
1509
1510 if (written == 1)
1511 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1512 else if (written > 1)
1513 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001514 if (lnum < buf->b_ml.ml_line_count)
1515 ch_logn(channel, "Still %d more lines to write",
1516 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001517
1518 in_part->ch_buf_bot = lnum;
1519 }
1520 }
1521 if (!found_one)
1522 buf->b_write_to_channel = FALSE;
1523}
1524
1525/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001526 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001527 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001528 */
1529 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001530invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1531 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001532{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001533 typval_T rettv;
1534 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001535
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001536 if (safe_to_invoke_callback == 0)
1537 EMSG("INTERNAL: Invoking callback when it is not safe");
1538
Bram Moolenaar77073442016-02-13 23:23:53 +01001539 argv[0].v_type = VAR_CHANNEL;
1540 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001541
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001542 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001543 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001544 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001545 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001546}
1547
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001548/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001549 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001550 * The caller must free it.
1551 * Returns NULL if there is nothing.
1552 */
1553 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001554channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001555{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001556 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001557 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001558 char_u *p;
1559
Bram Moolenaar77073442016-02-13 23:23:53 +01001560 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001561 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001562 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001563 p = node->rq_buffer;
1564 head->rq_next = node->rq_next;
1565 if (node->rq_next == NULL)
1566 head->rq_prev = NULL;
1567 else
1568 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001569 vim_free(node);
1570 return p;
1571}
1572
1573/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001574 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001575 */
1576 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001577channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001578{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001579 readq_T *head = &channel->ch_part[part].ch_head;
1580 readq_T *node = head->rq_next;
1581 long_u len = 1;
1582 char_u *res;
1583 char_u *p;
1584
1585 /* If there is only one buffer just get that one. */
1586 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1587 return channel_get(channel, part);
1588
1589 /* Concatenate everything into one buffer. */
1590 for (node = head->rq_next; node != NULL; node = node->rq_next)
1591 len += (long_u)STRLEN(node->rq_buffer);
1592 res = lalloc(len, TRUE);
1593 if (res == NULL)
1594 return NULL;
1595 *res = NUL;
1596 for (node = head->rq_next; node != NULL; node = node->rq_next)
1597 STRCAT(res, node->rq_buffer);
1598
1599 /* Free all buffers */
1600 do
1601 {
1602 p = channel_get(channel, part);
1603 vim_free(p);
1604 } while (p != NULL);
1605
1606 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001607}
1608
1609/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001610 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001611 * Returns FAIL if that is not possible.
1612 */
1613 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001614channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001615{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001616 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001617 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001618 char_u *p;
1619
Bram Moolenaar77073442016-02-13 23:23:53 +01001620 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001621 return FAIL;
1622
Bram Moolenaar77073442016-02-13 23:23:53 +01001623 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1624 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001625 if (p == NULL)
1626 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001627 STRCPY(p, node->rq_buffer);
1628 STRCAT(p, node->rq_next->rq_buffer);
1629 vim_free(node->rq_next->rq_buffer);
1630 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001631
Bram Moolenaar77073442016-02-13 23:23:53 +01001632 /* dispose of the node and its buffer */
1633 head->rq_next = node->rq_next;
1634 head->rq_next->rq_prev = NULL;
1635 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001636 vim_free(node);
1637 return OK;
1638}
1639
1640/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001641 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001642 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001643 * Returns OK or FAIL.
1644 */
1645 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001646channel_save(channel_T *channel, int part, char_u *buf, int len,
1647 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001648{
1649 readq_T *node;
1650 readq_T *head = &channel->ch_part[part].ch_head;
1651 char_u *p;
1652 int i;
1653
1654 node = (readq_T *)alloc(sizeof(readq_T));
1655 if (node == NULL)
1656 return FAIL; /* out of memory */
1657 node->rq_buffer = alloc(len + 1);
1658 if (node->rq_buffer == NULL)
1659 {
1660 vim_free(node);
1661 return FAIL; /* out of memory */
1662 }
1663
1664 if (channel->ch_part[part].ch_mode == MODE_NL)
1665 {
1666 /* Drop any CR before a NL. */
1667 p = node->rq_buffer;
1668 for (i = 0; i < len; ++i)
1669 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1670 *p++ = buf[i];
1671 *p = NUL;
1672 }
1673 else
1674 {
1675 mch_memmove(node->rq_buffer, buf, len);
1676 node->rq_buffer[len] = NUL;
1677 }
1678
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001679 if (prepend)
1680 {
1681 /* preend node to the head of the queue */
1682 node->rq_next = head->rq_next;
1683 node->rq_prev = NULL;
1684 if (head->rq_next == NULL)
1685 head->rq_prev = node;
1686 else
1687 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001688 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001689 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001690 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001691 {
1692 /* append node to the tail of the queue */
1693 node->rq_next = NULL;
1694 node->rq_prev = head->rq_prev;
1695 if (head->rq_prev == NULL)
1696 head->rq_next = node;
1697 else
1698 head->rq_prev->rq_next = node;
1699 head->rq_prev = node;
1700 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001701
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001702 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001703 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001704 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001705 fprintf(log_fd, "'");
1706 if (fwrite(buf, len, 1, log_fd) != 1)
1707 return FAIL;
1708 fprintf(log_fd, "'\n");
1709 }
1710 return OK;
1711}
1712
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001713 static int
1714channel_fill(js_read_T *reader)
1715{
1716 channel_T *channel = (channel_T *)reader->js_cookie;
1717 int part = reader->js_cookie_arg;
1718 char_u *next = channel_get(channel, part);
1719 int unused;
1720 int len;
1721 char_u *p;
1722
1723 if (next == NULL)
1724 return FALSE;
1725
1726 unused = reader->js_end - reader->js_buf - reader->js_used;
1727 if (unused > 0)
1728 {
1729 /* Prepend unused text. */
1730 len = (int)STRLEN(next);
1731 p = alloc(unused + len + 1);
1732 if (p == NULL)
1733 {
1734 vim_free(next);
1735 return FALSE;
1736 }
1737 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1738 mch_memmove(p + unused, next, len + 1);
1739 vim_free(next);
1740 next = p;
1741 }
1742
1743 vim_free(reader->js_buf);
1744 reader->js_buf = next;
1745 reader->js_used = 0;
1746 return TRUE;
1747}
1748
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001749/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001750 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001751 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001752 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001753 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001754 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001755channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001756{
1757 js_read_T reader;
1758 typval_T listtv;
1759 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001760 chanpart_T *chanpart = &channel->ch_part[part];
1761 jsonq_T *head = &chanpart->ch_json_head;
1762 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001763 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001764
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001765 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001766 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001767
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001768 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001769 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001770 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001771 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001772 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001773
1774 /* When a message is incomplete we wait for a short while for more to
1775 * arrive. After the delay drop the input, otherwise a truncated string
1776 * or list will make us hang. */
1777 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001778 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001779 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001780 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001781 /* Only accept the response when it is a list with at least two
1782 * items. */
1783 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001784 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001785 if (listtv.v_type != VAR_LIST)
1786 ch_error(channel, "Did not receive a list, discarding");
1787 else
1788 ch_errorn(channel, "Expected list with two items, got %d",
1789 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001790 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001791 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001792 else
1793 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001794 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1795 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001796 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001797 else
1798 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001799 item->jq_value = alloc_tv();
1800 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001801 {
1802 vim_free(item);
1803 clear_tv(&listtv);
1804 }
1805 else
1806 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001807 *item->jq_value = listtv;
1808 item->jq_prev = head->jq_prev;
1809 head->jq_prev = item;
1810 item->jq_next = NULL;
1811 if (item->jq_prev == NULL)
1812 head->jq_next = item;
1813 else
1814 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001815 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001816 }
1817 }
1818 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001819
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001820 if (status == OK)
1821 chanpart->ch_waiting = FALSE;
1822 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001823 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001824 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001825 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001826 /* First time encountering incomplete message, set a deadline of
1827 * 100 msec. */
1828 ch_log(channel, "Incomplete message - wait for more");
1829 reader.js_used = 0;
1830 chanpart->ch_waiting = TRUE;
1831#ifdef WIN32
1832 chanpart->ch_deadline = GetTickCount() + 100L;
1833#else
1834 gettimeofday(&chanpart->ch_deadline, NULL);
1835 chanpart->ch_deadline.tv_usec += 100 * 1000;
1836 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1837 {
1838 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1839 ++chanpart->ch_deadline.tv_sec;
1840 }
1841#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001842 }
1843 else
1844 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001845 int timeout;
1846#ifdef WIN32
1847 timeout = GetTickCount() > chanpart->ch_deadline;
1848#else
1849 {
1850 struct timeval now_tv;
1851
1852 gettimeofday(&now_tv, NULL);
1853 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1854 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1855 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1856 }
1857#endif
1858 if (timeout)
1859 {
1860 status = FAIL;
1861 chanpart->ch_waiting = FALSE;
1862 }
1863 else
1864 {
1865 reader.js_used = 0;
1866 ch_log(channel, "still waiting on incomplete message");
1867 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001868 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001869 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001870
1871 if (status == FAIL)
1872 {
1873 ch_error(channel, "Decoding failed - discarding input");
1874 ret = FALSE;
1875 chanpart->ch_waiting = FALSE;
1876 }
1877 else if (reader.js_buf[reader.js_used] != NUL)
1878 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001879 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001880 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001881 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1882 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001883 ret = status == MAYBE ? FALSE: TRUE;
1884 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001885 else
1886 ret = FALSE;
1887
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001888 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001889 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001890}
1891
1892/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001893 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001894 */
1895 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001896remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001897{
Bram Moolenaar77073442016-02-13 23:23:53 +01001898 if (node->cq_prev == NULL)
1899 head->cq_next = node->cq_next;
1900 else
1901 node->cq_prev->cq_next = node->cq_next;
1902 if (node->cq_next == NULL)
1903 head->cq_prev = node->cq_prev;
1904 else
1905 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001906}
1907
1908/*
1909 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001910 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001911 */
1912 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001913remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001914{
Bram Moolenaar77073442016-02-13 23:23:53 +01001915 if (node->jq_prev == NULL)
1916 head->jq_next = node->jq_next;
1917 else
1918 node->jq_prev->jq_next = node->jq_next;
1919 if (node->jq_next == NULL)
1920 head->jq_prev = node->jq_prev;
1921 else
1922 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001923 vim_free(node);
1924}
1925
1926/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001927 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001928 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001929 * When "id" is zero or negative jut get the first message. But not the one
1930 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001931 * Return OK when found and return the value in "rettv".
1932 * Return FAIL otherwise.
1933 */
1934 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001935channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001936{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001937 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001938 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001939
Bram Moolenaar77073442016-02-13 23:23:53 +01001940 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001941 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001942 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001943 typval_T *tv = &l->lv_first->li_tv;
1944
1945 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001946 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001947 || tv->vval.v_number == 0
1948 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001949 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001950 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001951 if (tv->v_type == VAR_NUMBER)
1952 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001953 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001954 return OK;
1955 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001956 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001957 }
1958 return FAIL;
1959}
1960
Bram Moolenaarece61b02016-02-20 21:39:05 +01001961#define CH_JSON_MAX_ARGS 4
1962
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001963/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001964 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001965 * "argv[0]" is the command string.
1966 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001967 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001968 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001969channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001970{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001971 char_u *cmd = argv[0].vval.v_string;
1972 char_u *arg;
1973 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001974
Bram Moolenaarece61b02016-02-20 21:39:05 +01001975 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001976 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001977 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001978 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001979 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001980 return;
1981 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001982 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001983 if (arg == NULL)
1984 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001985
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001986 if (STRCMP(cmd, "ex") == 0)
1987 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001988 int save_called_emsg = called_emsg;
1989
1990 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001991 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001992 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001993 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001994 --emsg_silent;
1995 if (called_emsg)
1996 ch_logs(channel, "Ex command error: '%s'",
1997 (char *)get_vim_var_str(VV_ERRMSG));
1998 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001999 }
2000 else if (STRCMP(cmd, "normal") == 0)
2001 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002002 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002003
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002004 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002005 ea.arg = arg;
2006 ea.addr_count = 0;
2007 ea.forceit = TRUE; /* no mapping */
2008 ex_normal(&ea);
2009 }
2010 else if (STRCMP(cmd, "redraw") == 0)
2011 {
2012 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002013
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002014 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002015 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002016 ex_redraw(&ea);
2017 showruler(FALSE);
2018 setcursor();
2019 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002020#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002021 if (gui.in_use)
2022 {
2023 gui_update_cursor(FALSE, FALSE);
2024 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002025 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002026#endif
2027 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002028 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002029 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002030 int is_call = cmd[0] == 'c';
2031 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002032
Bram Moolenaarece61b02016-02-20 21:39:05 +01002033 if (argv[id_idx].v_type != VAR_UNKNOWN
2034 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002035 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002036 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002037 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002038 EMSG("E904: last argument for expr/call must be a number");
2039 }
2040 else if (is_call && argv[2].v_type != VAR_LIST)
2041 {
2042 ch_error(channel, "third argument for call must be a list");
2043 if (p_verbose > 2)
2044 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002045 }
2046 else
2047 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002048 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002049 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002050 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002051 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002052
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002053 /* Don't pollute the display with errors. */
2054 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002055 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002056 {
2057 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002058 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002059 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002060 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002061 {
2062 ch_logs(channel, "Calling '%s'", (char *)arg);
2063 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2064 tv = &res_tv;
2065 else
2066 tv = NULL;
2067 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002068
2069 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002070 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002071 int id = argv[id_idx].vval.v_number;
2072
Bram Moolenaar55fab432016-02-07 16:53:13 +01002073 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002074 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002075 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002076 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002077 /* If evaluation failed or the result can't be encoded
2078 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002079 vim_free(json);
2080 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002081 err_tv.v_type = VAR_STRING;
2082 err_tv.vval.v_string = (char_u *)"ERROR";
2083 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002084 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002085 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002086 if (json != NULL)
2087 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002088 channel_send(channel,
2089 part == PART_SOCK ? PART_SOCK : PART_IN,
2090 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002091 vim_free(json);
2092 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002093 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002094 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002095 if (tv == &res_tv)
2096 clear_tv(tv);
2097 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002098 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002099 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002100 }
2101 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002102 {
2103 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002104 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002105 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002106}
2107
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002108/*
2109 * Invoke the callback at "cbhead".
2110 * Does not redraw but sets channel_need_redraw.
2111 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002112 static void
2113invoke_one_time_callback(
2114 channel_T *channel,
2115 cbq_T *cbhead,
2116 cbq_T *item,
2117 typval_T *argv)
2118{
2119 ch_logs(channel, "Invoking one-time callback %s",
2120 (char *)item->cq_callback);
2121 /* Remove the item from the list first, if the callback
2122 * invokes ch_close() the list will be cleared. */
2123 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002124 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002125 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002126 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002127 vim_free(item);
2128}
2129
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002130 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002131append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002132{
2133 buf_T *save_curbuf = curbuf;
2134 linenr_T lnum = buffer->b_ml.ml_line_count;
2135 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002136 chanpart_T *ch_part = &channel->ch_part[part];
2137 int save_p_ma = buffer->b_p_ma;
2138
2139 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2140 {
2141 if (!ch_part->ch_nomod_error)
2142 {
2143 ch_error(channel, "Buffer is not modifiable, cannot append");
2144 ch_part->ch_nomod_error = TRUE;
2145 }
2146 return;
2147 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002148
2149 /* If the buffer is also used as input insert above the last
2150 * line. Don't write these lines. */
2151 if (save_write_to)
2152 {
2153 --lnum;
2154 buffer->b_write_to_channel = FALSE;
2155 }
2156
2157 /* Append to the buffer */
2158 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2159
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002160 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002161 curbuf = buffer;
2162 u_sync(TRUE);
2163 /* ignore undo failure, undo is not very useful here */
2164 ignored = u_save(lnum, lnum + 1);
2165
2166 ml_append(lnum, msg, 0, FALSE);
2167 appended_lines_mark(lnum, 1L);
2168 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002169 if (ch_part->ch_nomodifiable)
2170 buffer->b_p_ma = FALSE;
2171 else
2172 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002173
2174 if (buffer->b_nwindows > 0)
2175 {
2176 win_T *wp;
2177 win_T *save_curwin;
2178
2179 FOR_ALL_WINDOWS(wp)
2180 {
2181 if (wp->w_buffer == buffer
2182 && (save_write_to
2183 ? wp->w_cursor.lnum == lnum + 1
2184 : (wp->w_cursor.lnum == lnum
2185 && wp->w_cursor.col == 0)))
2186 {
2187 ++wp->w_cursor.lnum;
2188 save_curwin = curwin;
2189 curwin = wp;
2190 curbuf = curwin->w_buffer;
2191 scroll_cursor_bot(0, FALSE);
2192 curwin = save_curwin;
2193 curbuf = curwin->w_buffer;
2194 }
2195 }
2196 redraw_buf_later(buffer, VALID);
2197 channel_need_redraw = TRUE;
2198 }
2199
2200 if (save_write_to)
2201 {
2202 channel_T *ch;
2203
2204 /* Find channels reading from this buffer and adjust their
2205 * next-to-read line number. */
2206 buffer->b_write_to_channel = TRUE;
2207 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2208 {
2209 chanpart_T *in_part = &ch->ch_part[PART_IN];
2210
2211 if (in_part->ch_buffer == buffer)
2212 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2213 }
2214 }
2215}
2216
Bram Moolenaar437905c2016-04-26 19:01:05 +02002217 static void
2218drop_messages(channel_T *channel, int part)
2219{
2220 char_u *msg;
2221
2222 while ((msg = channel_get(channel, part)) != NULL)
2223 {
2224 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2225 vim_free(msg);
2226 }
2227}
2228
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002229/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002230 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002231 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002232 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002233 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002234 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002235may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002236{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002237 char_u *msg = NULL;
2238 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002239 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002240 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002241 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002242 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002243 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002244 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002245 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002246 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002247
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002248 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002249 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002250 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002251
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002252 /* Use a message-specific callback, part callback or channel callback */
2253 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2254 if (cbitem->cq_seq_nr == 0)
2255 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002256 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002257 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002258 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002259 partial = cbitem->cq_partial;
2260 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002261 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002262 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002263 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002264 partial = channel->ch_part[part].ch_partial;
2265 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002266 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002267 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002268 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002269 partial = channel->ch_partial;
2270 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002271
Bram Moolenaar187db502016-02-27 14:44:26 +01002272 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002273 if (buffer != NULL && !buf_valid(buffer))
2274 {
2275 /* buffer was wiped out */
2276 channel->ch_part[part].ch_buffer = NULL;
2277 buffer = NULL;
2278 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002279
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002280 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002281 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002282 listitem_T *item;
2283 int argc = 0;
2284
Bram Moolenaard7ece102016-02-02 23:23:02 +01002285 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002286 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002287 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002288 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002289 channel_parse_json(channel, part);
2290 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002291 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002292 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002293
Bram Moolenaarece61b02016-02-20 21:39:05 +01002294 for (item = listtv->vval.v_list->lv_first;
2295 item != NULL && argc < CH_JSON_MAX_ARGS;
2296 item = item->li_next)
2297 argv[argc++] = item->li_tv;
2298 while (argc < CH_JSON_MAX_ARGS)
2299 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002300
Bram Moolenaarece61b02016-02-20 21:39:05 +01002301 if (argv[0].v_type == VAR_STRING)
2302 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002303 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002304 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002305 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002306 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002307 }
2308
Bram Moolenaarece61b02016-02-20 21:39:05 +01002309 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002310 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002311 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002312 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002313 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002314 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002315 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002316 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002317 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002318 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002319 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002320 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002321 return FALSE;
2322 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002323 else
2324 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002325 /* If there is no callback or buffer drop the message. */
2326 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002327 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002328 /* If there is a close callback it may use ch_read() to get the
2329 * messages. */
2330 if (channel->ch_close_cb == NULL)
2331 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002332 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002333 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002334
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002335 if (ch_mode == MODE_NL)
2336 {
2337 char_u *nl;
2338 char_u *buf;
2339
2340 /* See if we have a message ending in NL in the first buffer. If
2341 * not try to concatenate the first and the second buffer. */
2342 while (TRUE)
2343 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002344 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002345 nl = vim_strchr(buf, NL);
2346 if (nl != NULL)
2347 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002348 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002349 return FALSE; /* incomplete message */
2350 }
2351 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002352 {
2353 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002354 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002355 *nl = NUL;
2356 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002357 else
2358 {
2359 /* Copy the message into allocated memory and remove it from
2360 * the buffer. */
2361 msg = vim_strnsave(buf, (int)(nl - buf));
2362 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2363 }
2364 }
2365 else
2366 /* For a raw channel we don't know where the message ends, just
2367 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002368 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002369
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002370 if (msg == NULL)
2371 return FALSE; /* out of memory (and avoids Coverity warning) */
2372
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002373 argv[1].v_type = VAR_STRING;
2374 argv[1].vval.v_string = msg;
2375 }
2376
Bram Moolenaara07fec92016-02-05 21:04:08 +01002377 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002378 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002379 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002380
2381 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002382 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002383 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002384 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002385 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002386 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002387 break;
2388 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002389 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002390 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002391 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002392 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002393 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002394 if (buffer != NULL)
2395 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002396 if (msg == NULL)
2397 /* JSON or JS mode: re-encode the message. */
2398 msg = json_encode(listtv, ch_mode);
2399 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002400 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002401 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002402
Bram Moolenaar187db502016-02-27 14:44:26 +01002403 if (callback != NULL)
2404 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002405 if (cbitem != NULL)
2406 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2407 else
2408 {
2409 /* invoke the channel callback */
2410 ch_logs(channel, "Invoking channel callback %s",
2411 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002412 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002413 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002414 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002415 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002416 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002417 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002418
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002419 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002420 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002421 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002422
2423 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002424}
2425
2426/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002427 * Return TRUE when channel "channel" is open for writing to.
2428 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002429 */
2430 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002431channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002432{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002433 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002434 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002435}
2436
2437/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002438 * Return TRUE when channel "channel" is open for reading or writing.
2439 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002440 */
2441 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002442channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002443{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002444 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002445 || channel->CH_IN_FD != INVALID_FD
2446 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002447 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002448}
2449
2450/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002451 * Return TRUE if "channel" has JSON or other typeahead.
2452 */
2453 static int
2454channel_has_readahead(channel_T *channel, int part)
2455{
2456 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2457
2458 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2459 {
2460 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2461 jsonq_T *item = head->jq_next;
2462
2463 return item != NULL;
2464 }
2465 return channel_peek(channel, part) != NULL;
2466}
2467
2468/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002469 * Return a string indicating the status of the channel.
2470 */
2471 char *
2472channel_status(channel_T *channel)
2473{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002474 int part;
2475 int has_readahead = FALSE;
2476
Bram Moolenaar77073442016-02-13 23:23:53 +01002477 if (channel == NULL)
2478 return "fail";
2479 if (channel_is_open(channel))
2480 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002481 for (part = PART_SOCK; part <= PART_ERR; ++part)
2482 if (channel_has_readahead(channel, part))
2483 {
2484 has_readahead = TRUE;
2485 break;
2486 }
2487
2488 if (has_readahead)
2489 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002490 return "closed";
2491}
2492
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002493 static void
2494channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2495{
2496 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002497 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002498 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002499 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002500
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002501 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002502 STRCAT(namebuf, "_");
2503 tail = STRLEN(namebuf);
2504
2505 STRCPY(namebuf + tail, "status");
2506 dict_add_nr_str(dict, namebuf, 0,
2507 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2508
2509 STRCPY(namebuf + tail, "mode");
2510 switch (chanpart->ch_mode)
2511 {
2512 case MODE_NL: s = "NL"; break;
2513 case MODE_RAW: s = "RAW"; break;
2514 case MODE_JSON: s = "JSON"; break;
2515 case MODE_JS: s = "JS"; break;
2516 }
2517 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2518
2519 STRCPY(namebuf + tail, "io");
2520 if (part == PART_SOCK)
2521 s = "socket";
2522 else switch (chanpart->ch_io)
2523 {
2524 case JIO_NULL: s = "null"; break;
2525 case JIO_PIPE: s = "pipe"; break;
2526 case JIO_FILE: s = "file"; break;
2527 case JIO_BUFFER: s = "buffer"; break;
2528 case JIO_OUT: s = "out"; break;
2529 }
2530 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2531
2532 STRCPY(namebuf + tail, "timeout");
2533 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2534}
2535
2536 void
2537channel_info(channel_T *channel, dict_T *dict)
2538{
2539 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2540 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2541
2542 if (channel->ch_hostname != NULL)
2543 {
2544 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2545 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2546 channel_part_info(channel, dict, "sock", PART_SOCK);
2547 }
2548 else
2549 {
2550 channel_part_info(channel, dict, "out", PART_OUT);
2551 channel_part_info(channel, dict, "err", PART_ERR);
2552 channel_part_info(channel, dict, "in", PART_IN);
2553 }
2554}
2555
Bram Moolenaar77073442016-02-13 23:23:53 +01002556/*
2557 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002558 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002559 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002560 */
2561 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002562channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002563{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002564 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002565
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002566#ifdef FEAT_GUI
2567 channel_gui_unregister(channel);
2568#endif
2569
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002570 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002571 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002572 sock_close(channel->CH_SOCK_FD);
2573 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002574 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002575 may_close_part(&channel->CH_IN_FD);
2576 may_close_part(&channel->CH_OUT_FD);
2577 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002578
Bram Moolenaar8b374212016-02-24 20:43:06 +01002579 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002580 {
2581 typval_T argv[1];
2582 typval_T rettv;
2583 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002584 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002585
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002586 /* Invoke callbacks before the close callback, since it's weird to
2587 * first invoke the close callback. Increment the refcount to avoid
2588 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002589 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002590 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002591 for (part = PART_SOCK; part <= PART_ERR; ++part)
2592 while (may_invoke_callback(channel, part))
2593 ;
2594
2595 /* Invoke the close callback, if still set. */
2596 if (channel->ch_close_cb != NULL)
2597 {
2598 ch_logs(channel, "Invoking close callback %s",
2599 (char *)channel->ch_close_cb);
2600 argv[0].v_type = VAR_CHANNEL;
2601 argv[0].vval.v_channel = channel;
2602 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002603 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2604 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002605 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002606 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002607 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002608
2609 /* the callback is only called once */
2610 vim_free(channel->ch_close_cb);
2611 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002612 partial_unref(channel->ch_close_partial);
2613 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002614
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002615 --channel->ch_refcount;
2616
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002617 if (channel_need_redraw)
2618 {
2619 channel_need_redraw = FALSE;
2620 redraw_after_callback();
2621 }
2622
Bram Moolenaar437905c2016-04-26 19:01:05 +02002623 /* any remaining messages are useless now */
2624 for (part = PART_SOCK; part <= PART_ERR; ++part)
2625 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002626 }
2627
2628 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002629}
2630
Bram Moolenaard04a0202016-01-26 23:30:18 +01002631/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002632 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002633 * Returns NULL if there is nothing.
2634 */
2635 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002636channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002637{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002638 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002639
Bram Moolenaar77073442016-02-13 23:23:53 +01002640 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002641 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002642 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002643}
2644
2645/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002646 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002647 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002648 static void
2649channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002650{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002651 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2652 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002653
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002654 while (channel_peek(channel, part) != NULL)
2655 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002656
2657 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002658 {
2659 cbq_T *node = cb_head->cq_next;
2660
2661 remove_cb_node(cb_head, node);
2662 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002663 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002664 vim_free(node);
2665 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002666
2667 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002668 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002669 free_tv(json_head->jq_next->jq_value);
2670 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002671 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002672
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002673 vim_free(channel->ch_part[part].ch_callback);
2674 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002675 partial_unref(channel->ch_part[part].ch_partial);
2676 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002677}
2678
2679/*
2680 * Clear all the read buffers on "channel".
2681 */
2682 void
2683channel_clear(channel_T *channel)
2684{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002685 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002686 vim_free(channel->ch_hostname);
2687 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002688 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002689 channel_clear_one(channel, PART_OUT);
2690 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002691 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002692 vim_free(channel->ch_callback);
2693 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002694 partial_unref(channel->ch_partial);
2695 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002696 vim_free(channel->ch_close_cb);
2697 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002698 partial_unref(channel->ch_close_partial);
2699 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002700}
2701
Bram Moolenaar77073442016-02-13 23:23:53 +01002702#if defined(EXITFREE) || defined(PROTO)
2703 void
2704channel_free_all(void)
2705{
2706 channel_T *channel;
2707
Bram Moolenaard6051b52016-02-28 15:49:03 +01002708 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002709 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2710 channel_clear(channel);
2711}
2712#endif
2713
2714
Bram Moolenaar715d2852016-04-30 17:06:31 +02002715/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002716#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002717
2718/* Buffer size for reading incoming messages. */
2719#define MAXMSGSIZE 4096
2720
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002721#if defined(HAVE_SELECT)
2722/*
2723 * Add write fds where we are waiting for writing to be possible.
2724 */
2725 static int
2726channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2727{
2728 int maxfd = maxfd_arg;
2729 channel_T *ch;
2730
2731 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2732 {
2733 chanpart_T *in_part = &ch->ch_part[PART_IN];
2734
2735 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2736 {
2737 FD_SET((int)in_part->ch_fd, wfds);
2738 if ((int)in_part->ch_fd >= maxfd)
2739 maxfd = (int)in_part->ch_fd + 1;
2740 }
2741 }
2742 return maxfd;
2743}
2744#else
2745/*
2746 * Add write fds where we are waiting for writing to be possible.
2747 */
2748 static int
2749channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2750{
2751 int nfd = nfd_in;
2752 channel_T *ch;
2753
2754 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2755 {
2756 chanpart_T *in_part = &ch->ch_part[PART_IN];
2757
2758 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2759 {
2760 in_part->ch_poll_idx = nfd;
2761 fds[nfd].fd = in_part->ch_fd;
2762 fds[nfd].events = POLLOUT;
2763 ++nfd;
2764 }
2765 else
2766 in_part->ch_poll_idx = -1;
2767 }
2768 return nfd;
2769}
2770#endif
2771
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002772typedef enum {
2773 CW_READY,
2774 CW_NOT_READY,
2775 CW_ERROR
2776} channel_wait_result;
2777
Bram Moolenaard04a0202016-01-26 23:30:18 +01002778/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002779 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002780 * Return CW_READY when there is something to read.
2781 * Return CW_NOT_READY when there is nothing to read.
2782 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002783 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002784 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002785channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002786{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002787 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002788 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002789
Bram Moolenaard8070362016-02-15 21:56:54 +01002790# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002791 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002792 {
2793 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002794 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002795 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002796 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002797
2798 /* reading from a pipe, not a socket */
2799 while (TRUE)
2800 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002801 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2802
2803 if (r && nread > 0)
2804 return CW_READY;
2805 if (r == 0)
2806 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002807
2808 /* perhaps write some buffer lines */
2809 channel_write_any_lines();
2810
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002811 sleep_time = deadline - GetTickCount();
2812 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002813 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002814 /* Wait for a little while. Very short at first, up to 10 msec
2815 * after looping a few times. */
2816 if (sleep_time > delay)
2817 sleep_time = delay;
2818 Sleep(sleep_time);
2819 delay = delay * 2;
2820 if (delay > 10)
2821 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002822 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002823 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002824 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002825#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002826 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002827#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002828 struct timeval tval;
2829 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002830 fd_set wfds;
2831 int ret;
2832 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002833
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002834 tval.tv_sec = timeout / 1000;
2835 tval.tv_usec = (timeout % 1000) * 1000;
2836 for (;;)
2837 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002838 FD_ZERO(&rfds);
2839 FD_SET((int)fd, &rfds);
2840
2841 /* Write lines to a pipe when a pipe can be written to. Need to
2842 * set this every time, some buffers may be done. */
2843 maxfd = (int)fd + 1;
2844 FD_ZERO(&wfds);
2845 maxfd = channel_fill_wfds(maxfd, &wfds);
2846
2847 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002848# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002849 SOCK_ERRNO;
2850 if (ret == -1 && errno == EINTR)
2851 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002852# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002853 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002854 {
2855 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002856 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002857 channel_write_any_lines();
2858 continue;
2859 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002860 break;
2861 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002862#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002863 for (;;)
2864 {
2865 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2866 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002867
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002868 fds[0].fd = fd;
2869 fds[0].events = POLLIN;
2870 nfd = channel_fill_poll_write(nfd, fds);
2871 if (poll(fds, nfd, timeout) > 0)
2872 {
2873 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002874 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002875 channel_write_any_lines();
2876 continue;
2877 }
2878 break;
2879 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002880#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002881 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002882 return CW_NOT_READY;
2883}
2884
2885 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002886channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002887{
2888 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002889 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2890 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002891
2892 /* Queue a "DETACH" netbeans message in the command queue in order to
2893 * terminate the netbeans session later. Do not end the session here
2894 * directly as we may be running in the context of a call to
2895 * netbeans_parse_messages():
2896 * netbeans_parse_messages
2897 * -> autocmd triggered while processing the netbeans cmd
2898 * -> ui_breakcheck
2899 * -> gui event loop or select loop
2900 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002901 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002902 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002903 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002904 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002905 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2906
2907 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002908 * died. Don't close the channel right away, it may be the wrong moment
2909 * to invoke callbacks. */
2910 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02002911
2912#ifdef FEAT_GUI
2913 /* Stop listening to GUI events right away. */
2914 channel_gui_unregister(channel);
2915#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002916}
2917
2918 static void
2919channel_close_now(channel_T *channel)
2920{
2921 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002922 channel_close(channel, TRUE);
2923 if (channel->ch_nb_close_cb != NULL)
2924 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002925}
2926
2927/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002928 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002929 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02002930 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002931 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002932 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002933channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002934{
2935 static char_u *buf = NULL;
2936 int len = 0;
2937 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002938 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002939 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002940
Bram Moolenaar5850a762016-05-27 19:59:48 +02002941 /* If we detected a read error don't try reading again. */
2942 if (channel->ch_to_be_closed)
2943 return;
2944
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002945 fd = channel->ch_part[part].ch_fd;
2946 if (fd == INVALID_FD)
2947 {
2948 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002949 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002950 }
2951 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002952
2953 /* Allocate a buffer to read into. */
2954 if (buf == NULL)
2955 {
2956 buf = alloc(MAXMSGSIZE);
2957 if (buf == NULL)
2958 return; /* out of memory! */
2959 }
2960
2961 /* Keep on reading for as long as there is something to read.
2962 * Use select() or poll() to avoid blocking on a message that is exactly
2963 * MAXMSGSIZE long. */
2964 for (;;)
2965 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002966 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002967 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002968 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002969 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002970 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002971 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002972 if (len <= 0)
2973 break; /* error or nothing more to read */
2974
2975 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002976 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002977 readlen += len;
2978 if (len < MAXMSGSIZE)
2979 break; /* did read everything that's available */
2980 }
2981
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002982 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002983 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002984 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002985
2986#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002987 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002988 if (CH_HAS_GUI && gtk_main_level() > 0)
2989 gtk_main_quit();
2990#endif
2991}
2992
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002993/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002994 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002995 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002996 * Returns what was read in allocated memory.
2997 * Returns NULL in case of error or timeout.
2998 */
2999 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003000channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003001{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003002 char_u *buf;
3003 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003004 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003005 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003006 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003007
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003008 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003009 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003010
3011 while (TRUE)
3012 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003013 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003014 if (buf != NULL && (mode == MODE_RAW
3015 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
3016 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003017 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003018 continue;
3019
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003020 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003021 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003022 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003023 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003024 {
3025 ch_log(channel, "Timed out");
3026 return NULL;
3027 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003028 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003029 }
3030
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003031 if (mode == MODE_RAW)
3032 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003033 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003034 }
3035 else
3036 {
3037 nl = vim_strchr(buf, NL);
3038 if (nl[1] == NUL)
3039 {
3040 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003041 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003042 *nl = NUL;
3043 }
3044 else
3045 {
3046 /* Copy the message into allocated memory and remove it from the
3047 * buffer. */
3048 msg = vim_strnsave(buf, (int)(nl - buf));
3049 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
3050 }
3051 }
3052 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003053 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003054 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003055}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003056
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003057/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003058 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003059 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003060 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003061 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003062 */
3063 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003064channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003065 channel_T *channel,
3066 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003067 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003068 int id,
3069 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003070{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003071 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003072 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003073 int timeout;
3074 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003075
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003076 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003077 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003078 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003079 for (;;)
3080 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003081 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003082
3083 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003084 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003085 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003086 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003087 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003088 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003089
Bram Moolenaard7ece102016-02-02 23:23:02 +01003090 if (!more)
3091 {
3092 /* Handle any other messages in the queue. If done some more
3093 * messages may have arrived. */
3094 if (channel_parse_messages())
3095 continue;
3096
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003097 /* Wait for up to the timeout. If there was an incomplete message
3098 * use the deadline for that. */
3099 timeout = timeout_arg;
3100 if (chanpart->ch_waiting)
3101 {
3102#ifdef WIN32
3103 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3104#else
3105 {
3106 struct timeval now_tv;
3107
3108 gettimeofday(&now_tv, NULL);
3109 timeout = (chanpart->ch_deadline.tv_sec
3110 - now_tv.tv_sec) * 1000
3111 + (chanpart->ch_deadline.tv_usec
3112 - now_tv.tv_usec) / 1000
3113 + 1;
3114 }
3115#endif
3116 if (timeout < 0)
3117 {
3118 /* Something went wrong, channel_parse_json() didn't
3119 * discard message. Cancel waiting. */
3120 chanpart->ch_waiting = FALSE;
3121 timeout = timeout_arg;
3122 }
3123 else if (timeout > timeout_arg)
3124 timeout = timeout_arg;
3125 }
3126 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003127 if (fd == INVALID_FD
3128 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003129 {
3130 if (timeout == timeout_arg)
3131 {
3132 if (fd != INVALID_FD)
3133 ch_log(channel, "Timed out");
3134 break;
3135 }
3136 }
3137 else
3138 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003139 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003140 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003141 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003142 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003143}
3144
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003145/*
3146 * Common for ch_read() and ch_readraw().
3147 */
3148 void
3149common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3150{
3151 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003152 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003153 jobopt_T opt;
3154 int mode;
3155 int timeout;
3156 int id = -1;
3157 typval_T *listtv = NULL;
3158
3159 /* return an empty string by default */
3160 rettv->v_type = VAR_STRING;
3161 rettv->vval.v_string = NULL;
3162
3163 clear_job_options(&opt);
3164 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3165 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003166 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003167
Bram Moolenaar437905c2016-04-26 19:01:05 +02003168 if (opt.jo_set & JO_PART)
3169 part = opt.jo_part;
3170 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003171 if (channel != NULL)
3172 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003173 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003174 part = channel_part_read(channel);
3175 mode = channel_get_mode(channel, part);
3176 timeout = channel_get_timeout(channel, part);
3177 if (opt.jo_set & JO_TIMEOUT)
3178 timeout = opt.jo_timeout;
3179
3180 if (raw || mode == MODE_RAW || mode == MODE_NL)
3181 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3182 else
3183 {
3184 if (opt.jo_set & JO_ID)
3185 id = opt.jo_id;
3186 channel_read_json_block(channel, part, timeout, id, &listtv);
3187 if (listtv != NULL)
3188 {
3189 *rettv = *listtv;
3190 vim_free(listtv);
3191 }
3192 else
3193 {
3194 rettv->v_type = VAR_SPECIAL;
3195 rettv->vval.v_number = VVAL_NONE;
3196 }
3197 }
3198 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003199
3200theend:
3201 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003202}
3203
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003204# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3205 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003206/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003207 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003208 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003209 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003210 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003211channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003212{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003213 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003214 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003215
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003216 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003217 for (channel = first_channel; channel != NULL;
3218 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003219 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003220 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003221 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003222 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003223 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003224 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003225 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003226 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003227 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003228}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003229# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003230
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003231# if defined(WIN32) || defined(PROTO)
3232/*
3233 * Check the channels for anything that is ready to be read.
3234 * The data is put in the read queue.
3235 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003236 void
3237channel_handle_events(void)
3238{
3239 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003240 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003241 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003242
3243 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3244 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003245 /* If we detected a read error don't try reading again. */
3246 if (channel->ch_to_be_closed)
3247 continue;
3248
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003249 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003250 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003251 {
3252 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003253 if (fd != INVALID_FD)
3254 {
3255 int r = channel_wait(channel, fd, 0);
3256
3257 if (r == CW_READY)
3258 channel_read(channel, part, "channel_handle_events");
3259 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003260 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003261 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003262 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003263 }
3264}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003265# endif
3266
Bram Moolenaard04a0202016-01-26 23:30:18 +01003267/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003268 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003269 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003270 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003271 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003272 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003273channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003274{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003275 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003276 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003277 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003278
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003279 fd = channel->ch_part[part].ch_fd;
3280 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003281 {
3282 if (!channel->ch_error && fun != NULL)
3283 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003284 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003285 EMSG2("E630: %s(): write while not connected", fun);
3286 }
3287 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003288 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003289 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003290
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003291 if (log_fd != NULL)
3292 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003293 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003294 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003295 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003296 fprintf(log_fd, "'\n");
3297 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003298 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003299 }
3300
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003301 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003302 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003303 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003304 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003305 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003306 {
3307 if (!channel->ch_error && fun != NULL)
3308 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003309 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003310 EMSG2("E631: %s(): write failed", fun);
3311 }
3312 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003313 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003314 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003315
3316 channel->ch_error = FALSE;
3317 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003318}
3319
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003320/*
3321 * Common for "ch_sendexpr()" and "ch_sendraw()".
3322 * Returns the channel if the caller should read the response.
3323 * Sets "part_read" to the the read fd.
3324 * Otherwise returns NULL.
3325 */
3326 channel_T *
3327send_common(
3328 typval_T *argvars,
3329 char_u *text,
3330 int id,
3331 int eval,
3332 jobopt_T *opt,
3333 char *fun,
3334 int *part_read)
3335{
3336 channel_T *channel;
3337 int part_send;
3338
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003339 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003340 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003341 if (channel == NULL)
3342 return NULL;
3343 part_send = channel_part_send(channel);
3344 *part_read = channel_part_read(channel);
3345
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003346 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3347 return NULL;
3348
3349 /* Set the callback. An empty callback means no callback and not reading
3350 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3351 * allowed. */
3352 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3353 {
3354 if (eval)
3355 {
3356 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3357 return NULL;
3358 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003359 channel_set_req_callback(channel, part_send,
3360 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003361 }
3362
3363 if (channel_send(channel, part_send, text, fun) == OK
3364 && opt->jo_callback == NULL)
3365 return channel;
3366 return NULL;
3367}
3368
3369/*
3370 * common for "ch_evalexpr()" and "ch_sendexpr()"
3371 */
3372 void
3373ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3374{
3375 char_u *text;
3376 typval_T *listtv;
3377 channel_T *channel;
3378 int id;
3379 ch_mode_T ch_mode;
3380 int part_send;
3381 int part_read;
3382 jobopt_T opt;
3383 int timeout;
3384
3385 /* return an empty string by default */
3386 rettv->v_type = VAR_STRING;
3387 rettv->vval.v_string = NULL;
3388
Bram Moolenaar437905c2016-04-26 19:01:05 +02003389 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003390 if (channel == NULL)
3391 return;
3392 part_send = channel_part_send(channel);
3393
3394 ch_mode = channel_get_mode(channel, part_send);
3395 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3396 {
3397 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3398 return;
3399 }
3400
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003401 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003402 text = json_encode_nr_expr(id, &argvars[1],
3403 ch_mode == MODE_JS ? JSON_JS : 0);
3404 if (text == NULL)
3405 return;
3406
3407 channel = send_common(argvars, text, id, eval, &opt,
3408 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3409 vim_free(text);
3410 if (channel != NULL && eval)
3411 {
3412 if (opt.jo_set & JO_TIMEOUT)
3413 timeout = opt.jo_timeout;
3414 else
3415 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003416 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3417 == OK)
3418 {
3419 list_T *list = listtv->vval.v_list;
3420
3421 /* Move the item from the list and then change the type to
3422 * avoid the value being freed. */
3423 *rettv = list->lv_last->li_tv;
3424 list->lv_last->li_tv.v_type = VAR_NUMBER;
3425 free_tv(listtv);
3426 }
3427 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003428 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003429}
3430
3431/*
3432 * common for "ch_evalraw()" and "ch_sendraw()"
3433 */
3434 void
3435ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3436{
3437 char_u buf[NUMBUFLEN];
3438 char_u *text;
3439 channel_T *channel;
3440 int part_read;
3441 jobopt_T opt;
3442 int timeout;
3443
3444 /* return an empty string by default */
3445 rettv->v_type = VAR_STRING;
3446 rettv->vval.v_string = NULL;
3447
3448 text = get_tv_string_buf(&argvars[1], buf);
3449 channel = send_common(argvars, text, 0, eval, &opt,
3450 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3451 if (channel != NULL && eval)
3452 {
3453 if (opt.jo_set & JO_TIMEOUT)
3454 timeout = opt.jo_timeout;
3455 else
3456 timeout = channel_get_timeout(channel, part_read);
3457 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3458 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003459 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003460}
3461
Bram Moolenaard04a0202016-01-26 23:30:18 +01003462# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003463/*
3464 * Add open channels to the poll struct.
3465 * Return the adjusted struct index.
3466 * The type of "fds" is hidden to avoid problems with the function proto.
3467 */
3468 int
3469channel_poll_setup(int nfd_in, void *fds_in)
3470{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003471 int nfd = nfd_in;
3472 channel_T *channel;
3473 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003474 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003475
Bram Moolenaar77073442016-02-13 23:23:53 +01003476 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003477 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003478 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003479 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003480 chanpart_T *ch_part = &channel->ch_part[part];
3481
3482 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003483 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003484 ch_part->ch_poll_idx = nfd;
3485 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003486 fds[nfd].events = POLLIN;
3487 nfd++;
3488 }
3489 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003490 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003491 }
3492 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003493
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003494 nfd = channel_fill_poll_write(nfd, fds);
3495
Bram Moolenaare0874f82016-01-24 20:36:41 +01003496 return nfd;
3497}
3498
3499/*
3500 * The type of "fds" is hidden to avoid problems with the function proto.
3501 */
3502 int
3503channel_poll_check(int ret_in, void *fds_in)
3504{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003505 int ret = ret_in;
3506 channel_T *channel;
3507 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003508 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003509 int idx;
3510 chanpart_T *in_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 Moolenaar8b877ac2016-03-28 19:16:20 +02003516 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003517
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003518 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003519 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003520 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003521 --ret;
3522 }
3523 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003524
3525 in_part = &channel->ch_part[PART_IN];
3526 idx = in_part->ch_poll_idx;
3527 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3528 {
3529 if (in_part->ch_buf_append)
3530 {
3531 if (in_part->ch_buffer != NULL)
3532 channel_write_new_lines(in_part->ch_buffer);
3533 }
3534 else
3535 channel_write_in(channel);
3536 --ret;
3537 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003538 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003539
3540 return ret;
3541}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003542# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003543
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003544# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003545/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003546 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003547 */
3548 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003549channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003550{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003551 int maxfd = maxfd_in;
3552 channel_T *channel;
3553 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003554 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003555 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003556
Bram Moolenaar77073442016-02-13 23:23:53 +01003557 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003558 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003559 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003560 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003561 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003562
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003563 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003564 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003565 FD_SET((int)fd, rfds);
3566 if (maxfd < (int)fd)
3567 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003568 }
3569 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003570 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003571
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003572 maxfd = channel_fill_wfds(maxfd, wfds);
3573
Bram Moolenaare0874f82016-01-24 20:36:41 +01003574 return maxfd;
3575}
3576
3577/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003578 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003579 */
3580 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003581channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003582{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003583 int ret = ret_in;
3584 channel_T *channel;
3585 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003586 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003587 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003588 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003589
Bram Moolenaar77073442016-02-13 23:23:53 +01003590 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003591 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003592 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003593 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003594 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003595
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003596 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003597 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003598 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003599 --ret;
3600 }
3601 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003602
3603 in_part = &channel->ch_part[PART_IN];
3604 if (ret > 0 && in_part->ch_fd != INVALID_FD
3605 && FD_ISSET(in_part->ch_fd, wfds))
3606 {
3607 if (in_part->ch_buf_append)
3608 {
3609 if (in_part->ch_buffer != NULL)
3610 channel_write_new_lines(in_part->ch_buffer);
3611 }
3612 else
3613 channel_write_in(channel);
3614 --ret;
3615 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003616 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003617
3618 return ret;
3619}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003620# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003621
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003622/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003623 * Execute queued up commands.
3624 * Invoked from the main loop when it's safe to execute received commands.
3625 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003626 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003627 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003628channel_parse_messages(void)
3629{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003630 channel_T *channel = first_channel;
3631 int ret = FALSE;
3632 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003633 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003634
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003635 ++safe_to_invoke_callback;
3636
Bram Moolenaard0b65022016-03-06 21:50:33 +01003637 /* Only do this message when another message was given, otherwise we get
3638 * lots of them. */
3639 if (did_log_msg)
3640 {
3641 ch_log(NULL, "looking for messages on channels");
3642 did_log_msg = FALSE;
3643 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003644 while (channel != NULL)
3645 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003646 if (channel->ch_to_be_closed)
3647 {
3648 channel->ch_to_be_closed = FALSE;
3649 channel_close_now(channel);
3650 /* channel may have been freed, start over */
3651 channel = first_channel;
3652 continue;
3653 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003654 if (channel->ch_to_be_freed)
3655 {
3656 channel_free(channel);
3657 /* channel has been freed, start over */
3658 channel = first_channel;
3659 continue;
3660 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003661 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003662 {
3663 /* channel is no longer useful, free it */
3664 channel_free(channel);
3665 channel = first_channel;
3666 part = PART_SOCK;
3667 continue;
3668 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003669 if (channel->ch_part[part].ch_fd != INVALID_FD
3670 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003671 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003672 /* Increase the refcount, in case the handler causes the channel
3673 * to be unreferenced or closed. */
3674 ++channel->ch_refcount;
3675 r = may_invoke_callback(channel, part);
3676 if (r == OK)
3677 ret = TRUE;
3678 if (channel_unref(channel) || r == OK)
3679 {
3680 /* channel was freed or something was done, start over */
3681 channel = first_channel;
3682 part = PART_SOCK;
3683 continue;
3684 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003685 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003686 if (part < PART_ERR)
3687 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003688 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003689 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003690 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003691 part = PART_SOCK;
3692 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003693 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003694
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003695 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003696 {
3697 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003698 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003699 }
3700
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003701 --safe_to_invoke_callback;
3702
Bram Moolenaard7ece102016-02-02 23:23:02 +01003703 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003704}
3705
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003706/*
3707 * Mark references to lists used in channels.
3708 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003709 int
3710set_ref_in_channel(int copyID)
3711{
Bram Moolenaar77073442016-02-13 23:23:53 +01003712 int abort = FALSE;
3713 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003714 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003715
Bram Moolenaar77073442016-02-13 23:23:53 +01003716 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003717 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003718 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003719 tv.v_type = VAR_CHANNEL;
3720 tv.vval.v_channel = channel;
3721 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003722 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003723 return abort;
3724}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003725
3726/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003727 * Return the "part" to write to for "channel".
3728 */
3729 int
3730channel_part_send(channel_T *channel)
3731{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003732 if (channel->CH_SOCK_FD == INVALID_FD)
3733 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003734 return PART_SOCK;
3735}
3736
3737/*
3738 * Return the default "part" to read from for "channel".
3739 */
3740 int
3741channel_part_read(channel_T *channel)
3742{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003743 if (channel->CH_SOCK_FD == INVALID_FD)
3744 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003745 return PART_SOCK;
3746}
3747
3748/*
3749 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003750 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003751 */
3752 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003753channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003754{
Bram Moolenaar77073442016-02-13 23:23:53 +01003755 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003756 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003757 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003758}
3759
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003760/*
3761 * Return the timeout of "channel"/"part"
3762 */
3763 int
3764channel_get_timeout(channel_T *channel, int part)
3765{
3766 return channel->ch_part[part].ch_timeout;
3767}
3768
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003769 static int
3770handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3771{
3772 char_u *val = get_tv_string(item);
3773
3774 opt->jo_set |= jo;
3775 if (STRCMP(val, "nl") == 0)
3776 *modep = MODE_NL;
3777 else if (STRCMP(val, "raw") == 0)
3778 *modep = MODE_RAW;
3779 else if (STRCMP(val, "js") == 0)
3780 *modep = MODE_JS;
3781 else if (STRCMP(val, "json") == 0)
3782 *modep = MODE_JSON;
3783 else
3784 {
3785 EMSG2(_(e_invarg2), val);
3786 return FAIL;
3787 }
3788 return OK;
3789}
3790
3791 static int
3792handle_io(typval_T *item, int part, jobopt_T *opt)
3793{
3794 char_u *val = get_tv_string(item);
3795
3796 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3797 if (STRCMP(val, "null") == 0)
3798 opt->jo_io[part] = JIO_NULL;
3799 else if (STRCMP(val, "pipe") == 0)
3800 opt->jo_io[part] = JIO_PIPE;
3801 else if (STRCMP(val, "file") == 0)
3802 opt->jo_io[part] = JIO_FILE;
3803 else if (STRCMP(val, "buffer") == 0)
3804 opt->jo_io[part] = JIO_BUFFER;
3805 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3806 opt->jo_io[part] = JIO_OUT;
3807 else
3808 {
3809 EMSG2(_(e_invarg2), val);
3810 return FAIL;
3811 }
3812 return OK;
3813}
3814
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003815/*
3816 * Clear a jobopt_T before using it.
3817 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003818 void
3819clear_job_options(jobopt_T *opt)
3820{
3821 vim_memset(opt, 0, sizeof(jobopt_T));
3822}
3823
3824/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003825 * Free any members of a jobopt_T.
3826 */
3827 void
3828free_job_options(jobopt_T *opt)
3829{
3830 if (opt->jo_partial != NULL)
3831 partial_unref(opt->jo_partial);
3832 if (opt->jo_out_partial != NULL)
3833 partial_unref(opt->jo_out_partial);
3834 if (opt->jo_err_partial != NULL)
3835 partial_unref(opt->jo_err_partial);
3836 if (opt->jo_close_partial != NULL)
3837 partial_unref(opt->jo_close_partial);
3838}
3839
3840/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003841 * Get the PART_ number from the first character of an option name.
3842 */
3843 static int
3844part_from_char(int c)
3845{
3846 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3847}
3848
3849/*
3850 * Get the option entries from the dict in "tv", parse them and put the result
3851 * in "opt".
3852 * Only accept options in "supported".
3853 * If an option value is invalid return FAIL.
3854 */
3855 int
3856get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3857{
3858 typval_T *item;
3859 char_u *val;
3860 dict_T *dict;
3861 int todo;
3862 hashitem_T *hi;
3863 int part;
3864
3865 opt->jo_set = 0;
3866 if (tv->v_type == VAR_UNKNOWN)
3867 return OK;
3868 if (tv->v_type != VAR_DICT)
3869 {
3870 EMSG(_(e_invarg));
3871 return FAIL;
3872 }
3873 dict = tv->vval.v_dict;
3874 if (dict == NULL)
3875 return OK;
3876
3877 todo = (int)dict->dv_hashtab.ht_used;
3878 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3879 if (!HASHITEM_EMPTY(hi))
3880 {
3881 item = &dict_lookup(hi)->di_tv;
3882
3883 if (STRCMP(hi->hi_key, "mode") == 0)
3884 {
3885 if (!(supported & JO_MODE))
3886 break;
3887 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3888 return FAIL;
3889 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003890 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003891 {
3892 if (!(supported & JO_IN_MODE))
3893 break;
3894 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3895 == FAIL)
3896 return FAIL;
3897 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003898 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003899 {
3900 if (!(supported & JO_OUT_MODE))
3901 break;
3902 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3903 == FAIL)
3904 return FAIL;
3905 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003906 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003907 {
3908 if (!(supported & JO_ERR_MODE))
3909 break;
3910 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3911 == FAIL)
3912 return FAIL;
3913 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003914 else if (STRCMP(hi->hi_key, "in_io") == 0
3915 || STRCMP(hi->hi_key, "out_io") == 0
3916 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003917 {
3918 if (!(supported & JO_OUT_IO))
3919 break;
3920 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3921 return FAIL;
3922 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003923 else if (STRCMP(hi->hi_key, "in_name") == 0
3924 || STRCMP(hi->hi_key, "out_name") == 0
3925 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003926 {
3927 part = part_from_char(*hi->hi_key);
3928
3929 if (!(supported & JO_OUT_IO))
3930 break;
3931 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3932 opt->jo_io_name[part] =
3933 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3934 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003935 else if (STRCMP(hi->hi_key, "in_buf") == 0
3936 || STRCMP(hi->hi_key, "out_buf") == 0
3937 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003938 {
3939 part = part_from_char(*hi->hi_key);
3940
3941 if (!(supported & JO_OUT_IO))
3942 break;
3943 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3944 opt->jo_io_buf[part] = get_tv_number(item);
3945 if (opt->jo_io_buf[part] <= 0)
3946 {
3947 EMSG2(_(e_invarg2), get_tv_string(item));
3948 return FAIL;
3949 }
3950 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3951 {
3952 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3953 return FAIL;
3954 }
3955 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02003956 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
3957 || STRCMP(hi->hi_key, "err_modifiable") == 0)
3958 {
3959 part = part_from_char(*hi->hi_key);
3960
3961 if (!(supported & JO_OUT_IO))
3962 break;
3963 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
3964 opt->jo_modifiable[part] = get_tv_number(item);
3965 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003966 else if (STRCMP(hi->hi_key, "in_top") == 0
3967 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003968 {
3969 linenr_T *lp;
3970
3971 if (!(supported & JO_OUT_IO))
3972 break;
3973 if (hi->hi_key[3] == 't')
3974 {
3975 lp = &opt->jo_in_top;
3976 opt->jo_set |= JO_IN_TOP;
3977 }
3978 else
3979 {
3980 lp = &opt->jo_in_bot;
3981 opt->jo_set |= JO_IN_BOT;
3982 }
3983 *lp = get_tv_number(item);
3984 if (*lp < 0)
3985 {
3986 EMSG2(_(e_invarg2), get_tv_string(item));
3987 return FAIL;
3988 }
3989 }
3990 else if (STRCMP(hi->hi_key, "channel") == 0)
3991 {
3992 if (!(supported & JO_OUT_IO))
3993 break;
3994 opt->jo_set |= JO_CHANNEL;
3995 if (item->v_type != VAR_CHANNEL)
3996 {
3997 EMSG2(_(e_invarg2), "channel");
3998 return FAIL;
3999 }
4000 opt->jo_channel = item->vval.v_channel;
4001 }
4002 else if (STRCMP(hi->hi_key, "callback") == 0)
4003 {
4004 if (!(supported & JO_CALLBACK))
4005 break;
4006 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004007 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004008 if (opt->jo_callback == NULL)
4009 {
4010 EMSG2(_(e_invarg2), "callback");
4011 return FAIL;
4012 }
4013 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004014 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004015 {
4016 if (!(supported & JO_OUT_CALLBACK))
4017 break;
4018 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004019 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004020 if (opt->jo_out_cb == NULL)
4021 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004022 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004023 return FAIL;
4024 }
4025 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004026 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004027 {
4028 if (!(supported & JO_ERR_CALLBACK))
4029 break;
4030 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004031 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004032 if (opt->jo_err_cb == NULL)
4033 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004034 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004035 return FAIL;
4036 }
4037 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004038 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004039 {
4040 if (!(supported & JO_CLOSE_CALLBACK))
4041 break;
4042 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004043 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004044 if (opt->jo_close_cb == NULL)
4045 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004046 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004047 return FAIL;
4048 }
4049 }
4050 else if (STRCMP(hi->hi_key, "waittime") == 0)
4051 {
4052 if (!(supported & JO_WAITTIME))
4053 break;
4054 opt->jo_set |= JO_WAITTIME;
4055 opt->jo_waittime = get_tv_number(item);
4056 }
4057 else if (STRCMP(hi->hi_key, "timeout") == 0)
4058 {
4059 if (!(supported & JO_TIMEOUT))
4060 break;
4061 opt->jo_set |= JO_TIMEOUT;
4062 opt->jo_timeout = get_tv_number(item);
4063 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004064 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004065 {
4066 if (!(supported & JO_OUT_TIMEOUT))
4067 break;
4068 opt->jo_set |= JO_OUT_TIMEOUT;
4069 opt->jo_out_timeout = get_tv_number(item);
4070 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004071 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004072 {
4073 if (!(supported & JO_ERR_TIMEOUT))
4074 break;
4075 opt->jo_set |= JO_ERR_TIMEOUT;
4076 opt->jo_err_timeout = get_tv_number(item);
4077 }
4078 else if (STRCMP(hi->hi_key, "part") == 0)
4079 {
4080 if (!(supported & JO_PART))
4081 break;
4082 opt->jo_set |= JO_PART;
4083 val = get_tv_string(item);
4084 if (STRCMP(val, "err") == 0)
4085 opt->jo_part = PART_ERR;
4086 else
4087 {
4088 EMSG2(_(e_invarg2), val);
4089 return FAIL;
4090 }
4091 }
4092 else if (STRCMP(hi->hi_key, "id") == 0)
4093 {
4094 if (!(supported & JO_ID))
4095 break;
4096 opt->jo_set |= JO_ID;
4097 opt->jo_id = get_tv_number(item);
4098 }
4099 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4100 {
4101 if (!(supported & JO_STOPONEXIT))
4102 break;
4103 opt->jo_set |= JO_STOPONEXIT;
4104 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4105 opt->jo_soe_buf);
4106 if (opt->jo_stoponexit == NULL)
4107 {
4108 EMSG2(_(e_invarg2), "stoponexit");
4109 return FAIL;
4110 }
4111 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004112 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004113 {
4114 if (!(supported & JO_EXIT_CB))
4115 break;
4116 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004117 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
4118 {
4119 opt->jo_exit_partial = item->vval.v_partial;
4120 opt->jo_exit_cb = item->vval.v_partial->pt_name;
4121 }
4122 else
4123 opt->jo_exit_cb = get_tv_string_buf_chk(
4124 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004125 if (opt->jo_exit_cb == NULL)
4126 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004127 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004128 return FAIL;
4129 }
4130 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004131 else if (STRCMP(hi->hi_key, "block_write") == 0)
4132 {
4133 if (!(supported & JO_BLOCK_WRITE))
4134 break;
4135 opt->jo_set |= JO_BLOCK_WRITE;
4136 opt->jo_block_write = get_tv_number(item);
4137 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004138 else
4139 break;
4140 --todo;
4141 }
4142 if (todo > 0)
4143 {
4144 EMSG2(_(e_invarg2), hi->hi_key);
4145 return FAIL;
4146 }
4147
4148 return OK;
4149}
4150
4151/*
4152 * Get the channel from the argument.
4153 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004154 * When "check_open" is TRUE check that the channel can be used.
4155 * When "reading" is TRUE "check_open" considers typeahead useful.
4156 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004157 */
4158 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004159get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004160{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004161 channel_T *channel = NULL;
4162 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004163
4164 if (tv->v_type == VAR_JOB)
4165 {
4166 if (tv->vval.v_job != NULL)
4167 channel = tv->vval.v_job->jv_channel;
4168 }
4169 else if (tv->v_type == VAR_CHANNEL)
4170 {
4171 channel = tv->vval.v_channel;
4172 }
4173 else
4174 {
4175 EMSG2(_(e_invarg2), get_tv_string(tv));
4176 return NULL;
4177 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004178 if (channel != NULL && reading)
4179 has_readahead = channel_has_readahead(channel,
4180 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004181
Bram Moolenaar437905c2016-04-26 19:01:05 +02004182 if (check_open && (channel == NULL || (!channel_is_open(channel)
4183 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004184 {
4185 EMSG(_("E906: not an open channel"));
4186 return NULL;
4187 }
4188 return channel;
4189}
4190
4191static job_T *first_job = NULL;
4192
4193 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004194job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004195{
4196 ch_log(job->jv_channel, "Freeing job");
4197 if (job->jv_channel != NULL)
4198 {
4199 /* The link from the channel to the job doesn't count as a reference,
4200 * thus don't decrement the refcount of the job. The reference from
4201 * the job to the channel does count the refrence, decrement it and
4202 * NULL the reference. We don't set ch_job_killed, unreferencing the
4203 * job doesn't mean it stops running. */
4204 job->jv_channel->ch_job = NULL;
4205 channel_unref(job->jv_channel);
4206 }
4207 mch_clear_job(job);
4208
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004209 vim_free(job->jv_stoponexit);
4210 vim_free(job->jv_exit_cb);
4211 partial_unref(job->jv_exit_partial);
4212}
4213
4214 static void
4215job_free_job(job_T *job)
4216{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004217 if (job->jv_next != NULL)
4218 job->jv_next->jv_prev = job->jv_prev;
4219 if (job->jv_prev == NULL)
4220 first_job = job->jv_next;
4221 else
4222 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004223 vim_free(job);
4224}
4225
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004226 static void
4227job_free(job_T *job)
4228{
4229 if (!in_free_unref_items)
4230 {
4231 job_free_contents(job);
4232 job_free_job(job);
4233 }
4234}
4235
Bram Moolenaar655da312016-05-28 22:22:34 +02004236#if defined(EXITFREE) || defined(PROTO)
4237 void
4238job_free_all(void)
4239{
4240 while (first_job != NULL)
4241 job_free(first_job);
4242}
4243#endif
4244
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004245/*
4246 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004247 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4248 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004249 */
4250 static int
4251job_still_useful(job_T *job)
4252{
4253 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004254 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4255 || (job->jv_channel != NULL
4256 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004257}
4258
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004259/*
4260 * Mark references in jobs that are still useful.
4261 */
4262 int
4263set_ref_in_job(int copyID)
4264{
4265 int abort = FALSE;
4266 job_T *job;
4267 typval_T tv;
4268
4269 for (job = first_job; job != NULL; job = job->jv_next)
4270 if (job_still_useful(job))
4271 {
4272 tv.v_type = VAR_JOB;
4273 tv.vval.v_job = job;
4274 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4275 }
4276 return abort;
4277}
4278
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004279 void
4280job_unref(job_T *job)
4281{
4282 if (job != NULL && --job->jv_refcount <= 0)
4283 {
4284 /* Do not free the job when it has not ended yet and there is a
4285 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004286 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004287 {
4288 job_free(job);
4289 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004290 else if (job->jv_channel != NULL
4291 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004292 {
4293 /* Do remove the link to the channel, otherwise it hangs
4294 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004295 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004296 job->jv_channel->ch_job = NULL;
4297 channel_unref(job->jv_channel);
4298 job->jv_channel = NULL;
4299 }
4300 }
4301}
4302
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004303 int
4304free_unused_jobs_contents(int copyID, int mask)
4305{
4306 int did_free = FALSE;
4307 job_T *job;
4308
4309 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004310 if ((job->jv_copyID & mask) != (copyID & mask)
4311 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004312 {
4313 /* Free the channel and ordinary items it contains, but don't
4314 * recurse into Lists, Dictionaries etc. */
4315 job_free_contents(job);
4316 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004317 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004318 return did_free;
4319}
4320
4321 void
4322free_unused_jobs(int copyID, int mask)
4323{
4324 job_T *job;
4325 job_T *job_next;
4326
4327 for (job = first_job; job != NULL; job = job_next)
4328 {
4329 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004330 if ((job->jv_copyID & mask) != (copyID & mask)
4331 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004332 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004333 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004334 job_free_job(job);
4335 }
4336 }
4337}
4338
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004339/*
4340 * Allocate a job. Sets the refcount to one and sets options default.
4341 */
4342 static job_T *
4343job_alloc(void)
4344{
4345 job_T *job;
4346
4347 job = (job_T *)alloc_clear(sizeof(job_T));
4348 if (job != NULL)
4349 {
4350 job->jv_refcount = 1;
4351 job->jv_stoponexit = vim_strsave((char_u *)"term");
4352
4353 if (first_job != NULL)
4354 {
4355 first_job->jv_prev = job;
4356 job->jv_next = first_job;
4357 }
4358 first_job = job;
4359 }
4360 return job;
4361}
4362
4363 void
4364job_set_options(job_T *job, jobopt_T *opt)
4365{
4366 if (opt->jo_set & JO_STOPONEXIT)
4367 {
4368 vim_free(job->jv_stoponexit);
4369 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4370 job->jv_stoponexit = NULL;
4371 else
4372 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4373 }
4374 if (opt->jo_set & JO_EXIT_CB)
4375 {
4376 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004377 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004378 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004379 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004380 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004381 job->jv_exit_partial = NULL;
4382 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004383 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004384 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004385 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004386 job->jv_exit_partial = opt->jo_exit_partial;
4387 if (job->jv_exit_partial != NULL)
4388 ++job->jv_exit_partial->pt_refcount;
4389 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004390 }
4391}
4392
4393/*
4394 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4395 */
4396 void
4397job_stop_on_exit()
4398{
4399 job_T *job;
4400
4401 for (job = first_job; job != NULL; job = job->jv_next)
4402 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4403 mch_stop_job(job, job->jv_stoponexit);
4404}
4405
4406/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004407 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004408 */
4409 void
4410job_check_ended(void)
4411{
4412 static time_t last_check = 0;
4413 time_t now;
4414 job_T *job;
4415 job_T *next;
4416
4417 /* Only do this once in 10 seconds. */
4418 now = time(NULL);
4419 if (last_check + 10 < now)
4420 {
4421 last_check = now;
4422 for (job = first_job; job != NULL; job = next)
4423 {
4424 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004425 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004426 job_status(job); /* may free "job" */
4427 }
4428 }
4429}
4430
4431/*
4432 * "job_start()" function
4433 */
4434 job_T *
4435job_start(typval_T *argvars)
4436{
4437 job_T *job;
4438 char_u *cmd = NULL;
4439#if defined(UNIX)
4440# define USE_ARGV
4441 char **argv = NULL;
4442 int argc = 0;
4443#else
4444 garray_T ga;
4445#endif
4446 jobopt_T opt;
4447 int part;
4448
4449 job = job_alloc();
4450 if (job == NULL)
4451 return NULL;
4452
4453 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004454#ifndef USE_ARGV
4455 ga_init2(&ga, (int)sizeof(char*), 20);
4456#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004457
4458 /* Default mode is NL. */
4459 clear_job_options(&opt);
4460 opt.jo_mode = MODE_NL;
4461 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004462 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4463 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004464 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004465
4466 /* Check that when io is "file" that there is a file name. */
4467 for (part = PART_OUT; part <= PART_IN; ++part)
4468 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4469 && opt.jo_io[part] == JIO_FILE
4470 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4471 || *opt.jo_io_name[part] == NUL))
4472 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004473 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004474 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004475 }
4476
4477 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4478 {
4479 buf_T *buf = NULL;
4480
4481 /* check that we can find the buffer before starting the job */
4482 if (opt.jo_set & JO_IN_BUF)
4483 {
4484 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4485 if (buf == NULL)
4486 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4487 }
4488 else if (!(opt.jo_set & JO_IN_NAME))
4489 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004490 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004491 }
4492 else
4493 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4494 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004495 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004496 if (buf->b_ml.ml_mfp == NULL)
4497 {
4498 char_u numbuf[NUMBUFLEN];
4499 char_u *s;
4500
4501 if (opt.jo_set & JO_IN_BUF)
4502 {
4503 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4504 s = numbuf;
4505 }
4506 else
4507 s = opt.jo_io_name[PART_IN];
4508 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004509 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004510 }
4511 job->jv_in_buf = buf;
4512 }
4513
4514 job_set_options(job, &opt);
4515
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004516 if (argvars[0].v_type == VAR_STRING)
4517 {
4518 /* Command is a string. */
4519 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004520 if (cmd == NULL || *cmd == NUL)
4521 {
4522 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004523 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004524 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004525#ifdef USE_ARGV
4526 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004527 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004528 argv[argc] = NULL;
4529#endif
4530 }
4531 else if (argvars[0].v_type != VAR_LIST
4532 || argvars[0].vval.v_list == NULL
4533 || argvars[0].vval.v_list->lv_len < 1)
4534 {
4535 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004536 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004537 }
4538 else
4539 {
4540 list_T *l = argvars[0].vval.v_list;
4541 listitem_T *li;
4542 char_u *s;
4543
4544#ifdef USE_ARGV
4545 /* Pass argv[] to mch_call_shell(). */
4546 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4547 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004548 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004549#endif
4550 for (li = l->lv_first; li != NULL; li = li->li_next)
4551 {
4552 s = get_tv_string_chk(&li->li_tv);
4553 if (s == NULL)
4554 goto theend;
4555#ifdef USE_ARGV
4556 argv[argc++] = (char *)s;
4557#else
4558 /* Only escape when needed, double quotes are not always allowed. */
4559 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4560 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004561# ifdef WIN32
4562 int old_ssl = p_ssl;
4563
4564 /* This is using CreateProcess, not cmd.exe. Always use
4565 * double quote and backslashes. */
4566 p_ssl = 0;
4567# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004568 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004569# ifdef WIN32
4570 p_ssl = old_ssl;
4571# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004572 if (s == NULL)
4573 goto theend;
4574 ga_concat(&ga, s);
4575 vim_free(s);
4576 }
4577 else
4578 ga_concat(&ga, s);
4579 if (li->li_next != NULL)
4580 ga_append(&ga, ' ');
4581#endif
4582 }
4583#ifdef USE_ARGV
4584 argv[argc] = NULL;
4585#else
4586 cmd = ga.ga_data;
4587#endif
4588 }
4589
4590#ifdef USE_ARGV
4591 if (ch_log_active())
4592 {
4593 garray_T ga;
4594 int i;
4595
4596 ga_init2(&ga, (int)sizeof(char), 200);
4597 for (i = 0; i < argc; ++i)
4598 {
4599 if (i > 0)
4600 ga_concat(&ga, (char_u *)" ");
4601 ga_concat(&ga, (char_u *)argv[i]);
4602 }
4603 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4604 ga_clear(&ga);
4605 }
4606 mch_start_job(argv, job, &opt);
4607#else
4608 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4609 mch_start_job((char *)cmd, job, &opt);
4610#endif
4611
4612 /* If the channel is reading from a buffer, write lines now. */
4613 if (job->jv_channel != NULL)
4614 channel_write_in(job->jv_channel);
4615
4616theend:
4617#ifdef USE_ARGV
4618 vim_free(argv);
4619#else
4620 vim_free(ga.ga_data);
4621#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004622 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004623 return job;
4624}
4625
4626/*
4627 * Get the status of "job" and invoke the exit callback when needed.
4628 * The returned string is not allocated.
4629 */
4630 char *
4631job_status(job_T *job)
4632{
4633 char *result;
4634
4635 if (job->jv_status == JOB_ENDED)
4636 /* No need to check, dead is dead. */
4637 result = "dead";
4638 else if (job->jv_status == JOB_FAILED)
4639 result = "fail";
4640 else
4641 {
4642 result = mch_job_status(job);
4643 if (job->jv_status == JOB_ENDED)
4644 ch_log(job->jv_channel, "Job ended");
4645 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4646 {
4647 typval_T argv[3];
4648 typval_T rettv;
4649 int dummy;
4650
4651 /* invoke the exit callback; make sure the refcount is > 0 */
4652 ++job->jv_refcount;
4653 argv[0].v_type = VAR_JOB;
4654 argv[0].vval.v_job = job;
4655 argv[1].v_type = VAR_NUMBER;
4656 argv[1].vval.v_number = job->jv_exitval;
4657 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004658 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4659 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004660 clear_tv(&rettv);
4661 --job->jv_refcount;
4662 }
4663 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4664 {
4665 /* The job was already unreferenced, now that it ended it can be
4666 * freed. Careful: caller must not use "job" after this! */
4667 job_free(job);
4668 }
4669 }
4670 return result;
4671}
4672
Bram Moolenaar8950a562016-03-12 15:22:55 +01004673/*
4674 * Implementation of job_info().
4675 */
4676 void
4677job_info(job_T *job, dict_T *dict)
4678{
4679 dictitem_T *item;
4680 varnumber_T nr;
4681
4682 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4683
4684 item = dictitem_alloc((char_u *)"channel");
4685 if (item == NULL)
4686 return;
4687 item->di_tv.v_lock = 0;
4688 item->di_tv.v_type = VAR_CHANNEL;
4689 item->di_tv.vval.v_channel = job->jv_channel;
4690 if (job->jv_channel != NULL)
4691 ++job->jv_channel->ch_refcount;
4692 if (dict_add(dict, item) == FAIL)
4693 dictitem_free(item);
4694
4695#ifdef UNIX
4696 nr = job->jv_pid;
4697#else
4698 nr = job->jv_proc_info.dwProcessId;
4699#endif
4700 dict_add_nr_str(dict, "process", nr, NULL);
4701
4702 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004703 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004704 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4705}
4706
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004707 int
4708job_stop(job_T *job, typval_T *argvars)
4709{
4710 char_u *arg;
4711
4712 if (argvars[1].v_type == VAR_UNKNOWN)
4713 arg = (char_u *)"";
4714 else
4715 {
4716 arg = get_tv_string_chk(&argvars[1]);
4717 if (arg == NULL)
4718 {
4719 EMSG(_(e_invarg));
4720 return 0;
4721 }
4722 }
4723 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4724 if (mch_stop_job(job, arg) == FAIL)
4725 return 0;
4726
4727 /* Assume that "hup" does not kill the job. */
4728 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4729 job->jv_channel->ch_job_killed = TRUE;
4730
4731 /* We don't try freeing the job, obviously the caller still has a
4732 * reference to it. */
4733 return 1;
4734}
4735
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004736#endif /* FEAT_JOB_CHANNEL */