blob: 9a98c3a6c25cc3f83a53708acbac4af255abbe45 [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
Bram Moolenaarcf089462016-06-12 21:18:43 +0200135ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100136{
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)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001082 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001083 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001084 if (buf == NULL)
1085 buf = buflist_findname_exp(name);
1086 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001087 if (buf == NULL)
1088 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001089 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001090 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001091 if (buf == NULL)
1092 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001093 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001094 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001095#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001096 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1097 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001098#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001099 if (curbuf->b_ml.ml_mfp == NULL)
1100 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001101 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1102 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001103 changed_bytes(1, 0);
1104 curbuf = save_curbuf;
1105 }
1106
1107 return buf;
1108}
1109
1110/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001111 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001112 */
1113 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001114channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001115{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001116 int part;
1117 char_u **cbp;
1118 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001119
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001120 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001121 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001122 channel->ch_part[part].ch_mode = opt->jo_mode;
1123 if (opt->jo_set & JO_IN_MODE)
1124 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1125 if (opt->jo_set & JO_OUT_MODE)
1126 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1127 if (opt->jo_set & JO_ERR_MODE)
1128 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001129
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001130 if (opt->jo_set & JO_TIMEOUT)
1131 for (part = PART_SOCK; part <= PART_IN; ++part)
1132 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1133 if (opt->jo_set & JO_OUT_TIMEOUT)
1134 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1135 if (opt->jo_set & JO_ERR_TIMEOUT)
1136 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001137 if (opt->jo_set & JO_BLOCK_WRITE)
1138 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001139
1140 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001141 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001142 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001143 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001144 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001145 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001146 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1147 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001148 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001149 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001150 *pp = opt->jo_partial;
1151 if (*pp != NULL)
1152 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001153 }
1154 if (opt->jo_set & JO_OUT_CALLBACK)
1155 {
1156 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001157 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001158 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001159 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001160 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1161 *cbp = vim_strsave(opt->jo_out_cb);
1162 else
1163 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001164 *pp = opt->jo_out_partial;
1165 if (*pp != NULL)
1166 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001167 }
1168 if (opt->jo_set & JO_ERR_CALLBACK)
1169 {
1170 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001171 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001172 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001173 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001174 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1175 *cbp = vim_strsave(opt->jo_err_cb);
1176 else
1177 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001178 *pp = opt->jo_err_partial;
1179 if (*pp != NULL)
1180 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001181 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001182 if (opt->jo_set & JO_CLOSE_CALLBACK)
1183 {
1184 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001185 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001186 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001187 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001188 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1189 *cbp = vim_strsave(opt->jo_close_cb);
1190 else
1191 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001192 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001193 if (*pp != NULL)
1194 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001195 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001196
1197 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1198 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001199 buf_T *buf;
1200
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001201 /* writing output to a buffer. Default mode is NL. */
1202 if (!(opt->jo_set & JO_OUT_MODE))
1203 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001204 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001205 {
1206 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1207 if (buf == NULL)
1208 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1209 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001210 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001211 {
1212 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1213 }
1214 if (buf != NULL)
1215 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001216 if (opt->jo_set & JO_OUT_MODIFIABLE)
1217 channel->ch_part[PART_OUT].ch_nomodifiable =
1218 !opt->jo_modifiable[PART_OUT];
1219
1220 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1221 {
1222 EMSG(_(e_modifiable));
1223 }
1224 else
1225 {
1226 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001227 (char *)buf->b_ffname);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001228 channel->ch_part[PART_OUT].ch_buffer = buf;
1229 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001230 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001231 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001232
1233 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1234 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1235 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1236 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001237 buf_T *buf;
1238
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001239 /* writing err to a buffer. Default mode is NL. */
1240 if (!(opt->jo_set & JO_ERR_MODE))
1241 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1242 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001243 buf = channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001244 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001245 {
1246 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1247 if (buf == NULL)
1248 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1249 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001250 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001251 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1252 if (buf != NULL)
1253 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001254 if (opt->jo_set & JO_ERR_MODIFIABLE)
1255 channel->ch_part[PART_ERR].ch_nomodifiable =
1256 !opt->jo_modifiable[PART_ERR];
1257 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1258 {
1259 EMSG(_(e_modifiable));
1260 }
1261 else
1262 {
1263 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001264 (char *)buf->b_ffname);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001265 channel->ch_part[PART_ERR].ch_buffer = buf;
1266 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001267 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001268 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001269
1270 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1271 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1272 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001273}
1274
1275/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001276 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001277 */
1278 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001279channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001280 channel_T *channel,
1281 int part,
1282 char_u *callback,
1283 partial_T *partial,
1284 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001285{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001286 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001287 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1288
1289 if (item != NULL)
1290 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001291 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001292 item->cq_partial = partial;
1293 if (partial != NULL)
1294 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001295 item->cq_seq_nr = id;
1296 item->cq_prev = head->cq_prev;
1297 head->cq_prev = item;
1298 item->cq_next = NULL;
1299 if (item->cq_prev == NULL)
1300 head->cq_next = item;
1301 else
1302 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001303 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001304}
1305
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001306 static void
1307write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1308{
1309 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001310 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001311 char_u *p;
1312
Bram Moolenaar655da312016-05-28 22:22:34 +02001313 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001314 if ((p = alloc(len + 2)) == NULL)
1315 return;
1316 STRCPY(p, line);
1317 p[len] = NL;
1318 p[len + 1] = NUL;
1319 channel_send(channel, PART_IN, p, "write_buf_line()");
1320 vim_free(p);
1321}
1322
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001323/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001324 * Return TRUE if "channel" can be written to.
1325 * Returns FALSE if the input is closed or the write would block.
1326 */
1327 static int
1328can_write_buf_line(channel_T *channel)
1329{
1330 chanpart_T *in_part = &channel->ch_part[PART_IN];
1331
1332 if (in_part->ch_fd == INVALID_FD)
1333 return FALSE; /* pipe was closed */
1334
1335 /* for testing: block every other attempt to write */
1336 if (in_part->ch_block_write == 1)
1337 in_part->ch_block_write = -1;
1338 else if (in_part->ch_block_write == -1)
1339 in_part->ch_block_write = 1;
1340
1341 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1342#ifndef WIN32
1343 {
1344# if defined(HAVE_SELECT)
1345 struct timeval tval;
1346 fd_set wfds;
1347 int ret;
1348
1349 FD_ZERO(&wfds);
1350 FD_SET((int)in_part->ch_fd, &wfds);
1351 tval.tv_sec = 0;
1352 tval.tv_usec = 0;
1353 for (;;)
1354 {
1355 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1356# ifdef EINTR
1357 SOCK_ERRNO;
1358 if (ret == -1 && errno == EINTR)
1359 continue;
1360# endif
1361 if (ret <= 0 || in_part->ch_block_write == 1)
1362 {
1363 if (ret > 0)
1364 ch_log(channel, "FAKED Input not ready for writing");
1365 else
1366 ch_log(channel, "Input not ready for writing");
1367 return FALSE;
1368 }
1369 break;
1370 }
1371# else
1372 struct pollfd fds;
1373
1374 fds.fd = in_part->ch_fd;
1375 fds.events = POLLOUT;
1376 if (poll(&fds, 1, 0) <= 0)
1377 {
1378 ch_log(channel, "Input not ready for writing");
1379 return FALSE;
1380 }
1381 if (in_part->ch_block_write == 1)
1382 {
1383 ch_log(channel, "FAKED Input not ready for writing");
1384 return FALSE;
1385 }
1386# endif
1387 }
1388#endif
1389 return TRUE;
1390}
1391
1392/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001393 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001394 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001395 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001396channel_write_in(channel_T *channel)
1397{
1398 chanpart_T *in_part = &channel->ch_part[PART_IN];
1399 linenr_T lnum;
1400 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001401 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001402
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001403 if (buf == NULL || in_part->ch_buf_append)
1404 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001405 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1406 {
1407 /* buffer was wiped out or unloaded */
1408 in_part->ch_buffer = NULL;
1409 return;
1410 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001411
1412 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1413 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1414 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001415 if (!can_write_buf_line(channel))
1416 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001417 write_buf_line(buf, lnum, channel);
1418 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001419 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001420
1421 if (written == 1)
1422 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1423 else if (written > 1)
1424 ch_logn(channel, "written %d lines to channel", written);
1425
Bram Moolenaar014069a2016-03-03 22:51:40 +01001426 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001427 if (lnum > buf->b_ml.ml_line_count)
1428 {
1429 /* Writing is done, no longer need the buffer. */
1430 in_part->ch_buffer = NULL;
1431 ch_log(channel, "Finished writing all lines to channel");
1432 }
1433 else
1434 ch_logn(channel, "Still %d more lines to write",
1435 buf->b_ml.ml_line_count - lnum + 1);
1436}
1437
1438/*
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001439 * Handle buffer "buf" beeing freed, remove it from any channels.
1440 */
1441 void
1442channel_buffer_free(buf_T *buf)
1443{
1444 channel_T *channel;
1445 int part;
1446
1447 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1448 for (part = PART_SOCK; part <= PART_IN; ++part)
1449 {
1450 chanpart_T *ch_part = &channel->ch_part[part];
1451
1452 if (ch_part->ch_buffer == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001453 {
1454 ch_logs(channel, "%s buffer has been wiped out",
1455 part_names[part]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001456 ch_part->ch_buffer = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001457 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001458 }
1459}
1460
1461/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001462 * Write any lines waiting to be written to a channel.
1463 */
1464 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001465channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001466{
1467 channel_T *channel;
1468
1469 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1470 {
1471 chanpart_T *in_part = &channel->ch_part[PART_IN];
1472
1473 if (in_part->ch_buffer != NULL)
1474 {
1475 if (in_part->ch_buf_append)
1476 channel_write_new_lines(in_part->ch_buffer);
1477 else
1478 channel_write_in(channel);
1479 }
1480 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001481}
1482
1483/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001484 * Write appended lines above the last one in "buf" to the channel.
1485 */
1486 void
1487channel_write_new_lines(buf_T *buf)
1488{
1489 channel_T *channel;
1490 int found_one = FALSE;
1491
1492 /* There could be more than one channel for the buffer, loop over all of
1493 * them. */
1494 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1495 {
1496 chanpart_T *in_part = &channel->ch_part[PART_IN];
1497 linenr_T lnum;
1498 int written = 0;
1499
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001500 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001501 {
1502 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001503 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504 found_one = TRUE;
1505 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1506 ++lnum)
1507 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001508 if (!can_write_buf_line(channel))
1509 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001510 write_buf_line(buf, lnum, channel);
1511 ++written;
1512 }
1513
1514 if (written == 1)
1515 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1516 else if (written > 1)
1517 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001518 if (lnum < buf->b_ml.ml_line_count)
1519 ch_logn(channel, "Still %d more lines to write",
1520 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001521
1522 in_part->ch_buf_bot = lnum;
1523 }
1524 }
1525 if (!found_one)
1526 buf->b_write_to_channel = FALSE;
1527}
1528
1529/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001530 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001531 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001532 */
1533 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001534invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1535 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001536{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001537 typval_T rettv;
1538 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001539
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001540 if (safe_to_invoke_callback == 0)
1541 EMSG("INTERNAL: Invoking callback when it is not safe");
1542
Bram Moolenaar77073442016-02-13 23:23:53 +01001543 argv[0].v_type = VAR_CHANNEL;
1544 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001545
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001546 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001547 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001548 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001549 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001550}
1551
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001552/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001553 * Return the first node from "channel"/"part" without removing it.
1554 * Returns NULL if there is nothing.
1555 */
1556 readq_T *
1557channel_peek(channel_T *channel, int part)
1558{
1559 readq_T *head = &channel->ch_part[part].ch_head;
1560
1561 return head->rq_next;
1562}
1563
1564/*
1565 * Return a pointer to the first NL in "node".
1566 * Skips over NUL characters.
1567 * Returns NULL if there is no NL.
1568 */
1569 char_u *
1570channel_first_nl(readq_T *node)
1571{
1572 char_u *buffer = node->rq_buffer;
1573 long_u i;
1574
1575 for (i = 0; i < node->rq_buflen; ++i)
1576 if (buffer[i] == NL)
1577 return buffer + i;
1578 return NULL;
1579}
1580
1581/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001582 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001583 * The caller must free it.
1584 * Returns NULL if there is nothing.
1585 */
1586 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001587channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001588{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001589 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001590 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001591 char_u *p;
1592
Bram Moolenaar77073442016-02-13 23:23:53 +01001593 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001594 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001595 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001596 p = node->rq_buffer;
1597 head->rq_next = node->rq_next;
1598 if (node->rq_next == NULL)
1599 head->rq_prev = NULL;
1600 else
1601 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001602 vim_free(node);
1603 return p;
1604}
1605
1606/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001607 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001608 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001609 */
1610 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001612{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001613 readq_T *head = &channel->ch_part[part].ch_head;
1614 readq_T *node = head->rq_next;
1615 long_u len = 1;
1616 char_u *res;
1617 char_u *p;
1618
1619 /* If there is only one buffer just get that one. */
1620 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1621 return channel_get(channel, part);
1622
1623 /* Concatenate everything into one buffer. */
1624 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001625 len += node->rq_buflen;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001626 res = lalloc(len, TRUE);
1627 if (res == NULL)
1628 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001629 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001630 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001631 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001632 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001633 p += node->rq_buflen;
1634 }
1635 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001636
1637 /* Free all buffers */
1638 do
1639 {
1640 p = channel_get(channel, part);
1641 vim_free(p);
1642 } while (p != NULL);
1643
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001644 /* turn all NUL into NL */
1645 while (len > 0)
1646 {
1647 --len;
1648 if (res[len] == NUL)
1649 res[len] = NL;
1650 }
1651
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001652 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001653}
1654
1655/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001656 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001657 * Caller must check these bytes are available.
1658 */
1659 void
1660channel_consume(channel_T *channel, int part, int len)
1661{
1662 readq_T *head = &channel->ch_part[part].ch_head;
1663 readq_T *node = head->rq_next;
1664 char_u *buf = node->rq_buffer;
1665
1666 mch_memmove(buf, buf + len, node->rq_buflen - len);
1667 node->rq_buflen -= len;
1668}
1669
1670/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001671 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001672 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001673 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001674 */
1675 int
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001676channel_collapse(channel_T *channel, int part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001677{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001678 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001679 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001680 readq_T *last_node;
1681 readq_T *n;
1682 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001683 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001684 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001685
Bram Moolenaar77073442016-02-13 23:23:53 +01001686 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001687 return FAIL;
1688
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001689 last_node = node->rq_next;
1690 len = node->rq_buflen + last_node->rq_buflen + 1;
1691 if (want_nl)
1692 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001693 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001694 {
1695 last_node = last_node->rq_next;
1696 len += last_node->rq_buflen;
1697 }
1698
1699 p = newbuf = alloc(len);
1700 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001701 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001702 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001703 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001704 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001705 node->rq_buffer = newbuf;
1706 for (n = node; n != last_node; )
1707 {
1708 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001709 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001710 p += n->rq_buflen;
1711 vim_free(n->rq_buffer);
1712 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001713 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001714
1715 /* dispose of the collapsed nodes and their buffers */
1716 for (n = node->rq_next; n != last_node; )
1717 {
1718 n = n->rq_next;
1719 vim_free(n->rq_prev);
1720 }
1721 node->rq_next = last_node->rq_next;
1722 if (last_node->rq_next == NULL)
1723 head->rq_prev = node;
1724 else
1725 last_node->rq_next->rq_prev = node;
1726 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001727 return OK;
1728}
1729
1730/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001731 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001732 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001733 * Returns OK or FAIL.
1734 */
1735 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001736channel_save(channel_T *channel, int part, char_u *buf, int len,
1737 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001738{
1739 readq_T *node;
1740 readq_T *head = &channel->ch_part[part].ch_head;
1741 char_u *p;
1742 int i;
1743
1744 node = (readq_T *)alloc(sizeof(readq_T));
1745 if (node == NULL)
1746 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001747 /* A NUL is added at the end, because netbeans code expects that.
1748 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001749 node->rq_buffer = alloc(len + 1);
1750 if (node->rq_buffer == NULL)
1751 {
1752 vim_free(node);
1753 return FAIL; /* out of memory */
1754 }
1755
1756 if (channel->ch_part[part].ch_mode == MODE_NL)
1757 {
1758 /* Drop any CR before a NL. */
1759 p = node->rq_buffer;
1760 for (i = 0; i < len; ++i)
1761 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1762 *p++ = buf[i];
1763 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001764 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001765 }
1766 else
1767 {
1768 mch_memmove(node->rq_buffer, buf, len);
1769 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001770 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001771 }
1772
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001773 if (prepend)
1774 {
1775 /* preend node to the head of the queue */
1776 node->rq_next = head->rq_next;
1777 node->rq_prev = NULL;
1778 if (head->rq_next == NULL)
1779 head->rq_prev = node;
1780 else
1781 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001782 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001783 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001784 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001785 {
1786 /* append node to the tail of the queue */
1787 node->rq_next = NULL;
1788 node->rq_prev = head->rq_prev;
1789 if (head->rq_prev == NULL)
1790 head->rq_next = node;
1791 else
1792 head->rq_prev->rq_next = node;
1793 head->rq_prev = node;
1794 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001795
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001796 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001797 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001798 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001799 fprintf(log_fd, "'");
1800 if (fwrite(buf, len, 1, log_fd) != 1)
1801 return FAIL;
1802 fprintf(log_fd, "'\n");
1803 }
1804 return OK;
1805}
1806
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001807 static int
1808channel_fill(js_read_T *reader)
1809{
1810 channel_T *channel = (channel_T *)reader->js_cookie;
1811 int part = reader->js_cookie_arg;
1812 char_u *next = channel_get(channel, part);
1813 int unused;
1814 int len;
1815 char_u *p;
1816
1817 if (next == NULL)
1818 return FALSE;
1819
1820 unused = reader->js_end - reader->js_buf - reader->js_used;
1821 if (unused > 0)
1822 {
1823 /* Prepend unused text. */
1824 len = (int)STRLEN(next);
1825 p = alloc(unused + len + 1);
1826 if (p == NULL)
1827 {
1828 vim_free(next);
1829 return FALSE;
1830 }
1831 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1832 mch_memmove(p + unused, next, len + 1);
1833 vim_free(next);
1834 next = p;
1835 }
1836
1837 vim_free(reader->js_buf);
1838 reader->js_buf = next;
1839 reader->js_used = 0;
1840 return TRUE;
1841}
1842
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001844 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001845 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001846 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001847 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001848 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001849channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001850{
1851 js_read_T reader;
1852 typval_T listtv;
1853 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001854 chanpart_T *chanpart = &channel->ch_part[part];
1855 jsonq_T *head = &chanpart->ch_json_head;
1856 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001857 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001858
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001859 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001860 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001861
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001862 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001863 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001864 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001865 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001866 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001867
1868 /* When a message is incomplete we wait for a short while for more to
1869 * arrive. After the delay drop the input, otherwise a truncated string
1870 * or list will make us hang. */
1871 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001872 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001873 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001874 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001875 /* Only accept the response when it is a list with at least two
1876 * items. */
1877 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001878 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001879 if (listtv.v_type != VAR_LIST)
1880 ch_error(channel, "Did not receive a list, discarding");
1881 else
1882 ch_errorn(channel, "Expected list with two items, got %d",
1883 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001884 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001885 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001886 else
1887 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001888 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1889 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001890 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001891 else
1892 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001893 item->jq_value = alloc_tv();
1894 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001895 {
1896 vim_free(item);
1897 clear_tv(&listtv);
1898 }
1899 else
1900 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001901 *item->jq_value = listtv;
1902 item->jq_prev = head->jq_prev;
1903 head->jq_prev = item;
1904 item->jq_next = NULL;
1905 if (item->jq_prev == NULL)
1906 head->jq_next = item;
1907 else
1908 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001909 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001910 }
1911 }
1912 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001913
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001914 if (status == OK)
1915 chanpart->ch_waiting = FALSE;
1916 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001917 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001918 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001919 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001920 /* First time encountering incomplete message, set a deadline of
1921 * 100 msec. */
1922 ch_log(channel, "Incomplete message - wait for more");
1923 reader.js_used = 0;
1924 chanpart->ch_waiting = TRUE;
1925#ifdef WIN32
1926 chanpart->ch_deadline = GetTickCount() + 100L;
1927#else
1928 gettimeofday(&chanpart->ch_deadline, NULL);
1929 chanpart->ch_deadline.tv_usec += 100 * 1000;
1930 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1931 {
1932 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1933 ++chanpart->ch_deadline.tv_sec;
1934 }
1935#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001936 }
1937 else
1938 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001939 int timeout;
1940#ifdef WIN32
1941 timeout = GetTickCount() > chanpart->ch_deadline;
1942#else
1943 {
1944 struct timeval now_tv;
1945
1946 gettimeofday(&now_tv, NULL);
1947 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1948 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1949 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1950 }
1951#endif
1952 if (timeout)
1953 {
1954 status = FAIL;
1955 chanpart->ch_waiting = FALSE;
1956 }
1957 else
1958 {
1959 reader.js_used = 0;
1960 ch_log(channel, "still waiting on incomplete message");
1961 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001962 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001963 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001964
1965 if (status == FAIL)
1966 {
1967 ch_error(channel, "Decoding failed - discarding input");
1968 ret = FALSE;
1969 chanpart->ch_waiting = FALSE;
1970 }
1971 else if (reader.js_buf[reader.js_used] != NUL)
1972 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001973 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001974 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001975 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1976 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001977 ret = status == MAYBE ? FALSE: TRUE;
1978 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001979 else
1980 ret = FALSE;
1981
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001982 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001983 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001984}
1985
1986/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001987 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001988 */
1989 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001990remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001991{
Bram Moolenaar77073442016-02-13 23:23:53 +01001992 if (node->cq_prev == NULL)
1993 head->cq_next = node->cq_next;
1994 else
1995 node->cq_prev->cq_next = node->cq_next;
1996 if (node->cq_next == NULL)
1997 head->cq_prev = node->cq_prev;
1998 else
1999 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002000}
2001
2002/*
2003 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002004 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002005 */
2006 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002007remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002008{
Bram Moolenaar77073442016-02-13 23:23:53 +01002009 if (node->jq_prev == NULL)
2010 head->jq_next = node->jq_next;
2011 else
2012 node->jq_prev->jq_next = node->jq_next;
2013 if (node->jq_next == NULL)
2014 head->jq_prev = node->jq_prev;
2015 else
2016 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002017 vim_free(node);
2018}
2019
2020/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002021 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002022 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002023 * When "id" is zero or negative jut get the first message. But not the one
2024 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002025 * Return OK when found and return the value in "rettv".
2026 * Return FAIL otherwise.
2027 */
2028 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002029channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002030{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002031 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002032 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002033
Bram Moolenaar77073442016-02-13 23:23:53 +01002034 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002035 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002036 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002037 typval_T *tv = &l->lv_first->li_tv;
2038
2039 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002040 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002041 || tv->vval.v_number == 0
2042 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002043 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002044 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002045 if (tv->v_type == VAR_NUMBER)
2046 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002047 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002048 return OK;
2049 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002050 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002051 }
2052 return FAIL;
2053}
2054
Bram Moolenaarece61b02016-02-20 21:39:05 +01002055#define CH_JSON_MAX_ARGS 4
2056
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002057/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002058 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002059 * "argv[0]" is the command string.
2060 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002061 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002062 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002063channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002064{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002065 char_u *cmd = argv[0].vval.v_string;
2066 char_u *arg;
2067 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002068
Bram Moolenaarece61b02016-02-20 21:39:05 +01002069 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002070 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002071 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002072 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002073 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002074 return;
2075 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002076 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002077 if (arg == NULL)
2078 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002079
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002080 if (STRCMP(cmd, "ex") == 0)
2081 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002082 int save_called_emsg = called_emsg;
2083
2084 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002085 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002086 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002087 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002088 --emsg_silent;
2089 if (called_emsg)
2090 ch_logs(channel, "Ex command error: '%s'",
2091 (char *)get_vim_var_str(VV_ERRMSG));
2092 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002093 }
2094 else if (STRCMP(cmd, "normal") == 0)
2095 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002096 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002097
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002098 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002099 ea.arg = arg;
2100 ea.addr_count = 0;
2101 ea.forceit = TRUE; /* no mapping */
2102 ex_normal(&ea);
2103 }
2104 else if (STRCMP(cmd, "redraw") == 0)
2105 {
2106 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002107
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002108 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002109 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002110 ex_redraw(&ea);
2111 showruler(FALSE);
2112 setcursor();
2113 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002114#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002115 if (gui.in_use)
2116 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002117 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002118 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002119 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002120#endif
2121 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002122 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002123 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002124 int is_call = cmd[0] == 'c';
2125 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002126
Bram Moolenaarece61b02016-02-20 21:39:05 +01002127 if (argv[id_idx].v_type != VAR_UNKNOWN
2128 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002129 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002130 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002131 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002132 EMSG("E904: last argument for expr/call must be a number");
2133 }
2134 else if (is_call && argv[2].v_type != VAR_LIST)
2135 {
2136 ch_error(channel, "third argument for call must be a list");
2137 if (p_verbose > 2)
2138 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002139 }
2140 else
2141 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002142 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002143 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002144 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002145 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002146
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002147 /* Don't pollute the display with errors. */
2148 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002149 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002150 {
2151 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002152 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002153 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002154 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002155 {
2156 ch_logs(channel, "Calling '%s'", (char *)arg);
2157 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2158 tv = &res_tv;
2159 else
2160 tv = NULL;
2161 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002162
2163 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002164 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002165 int id = argv[id_idx].vval.v_number;
2166
Bram Moolenaar55fab432016-02-07 16:53:13 +01002167 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002168 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002169 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002170 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002171 /* If evaluation failed or the result can't be encoded
2172 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002173 vim_free(json);
2174 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002175 err_tv.v_type = VAR_STRING;
2176 err_tv.vval.v_string = (char_u *)"ERROR";
2177 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002178 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002179 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002180 if (json != NULL)
2181 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002182 channel_send(channel,
2183 part == PART_SOCK ? PART_SOCK : PART_IN,
2184 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002185 vim_free(json);
2186 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002187 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002188 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002189 if (tv == &res_tv)
2190 clear_tv(tv);
2191 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002192 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002193 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002194 }
2195 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002196 {
2197 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002198 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002199 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002200}
2201
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002202/*
2203 * Invoke the callback at "cbhead".
2204 * Does not redraw but sets channel_need_redraw.
2205 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002206 static void
2207invoke_one_time_callback(
2208 channel_T *channel,
2209 cbq_T *cbhead,
2210 cbq_T *item,
2211 typval_T *argv)
2212{
2213 ch_logs(channel, "Invoking one-time callback %s",
2214 (char *)item->cq_callback);
2215 /* Remove the item from the list first, if the callback
2216 * invokes ch_close() the list will be cleared. */
2217 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002218 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002219 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002220 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002221 vim_free(item);
2222}
2223
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002224 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002225append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002226{
2227 buf_T *save_curbuf = curbuf;
2228 linenr_T lnum = buffer->b_ml.ml_line_count;
2229 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002230 chanpart_T *ch_part = &channel->ch_part[part];
2231 int save_p_ma = buffer->b_p_ma;
2232
2233 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2234 {
2235 if (!ch_part->ch_nomod_error)
2236 {
2237 ch_error(channel, "Buffer is not modifiable, cannot append");
2238 ch_part->ch_nomod_error = TRUE;
2239 }
2240 return;
2241 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002242
2243 /* If the buffer is also used as input insert above the last
2244 * line. Don't write these lines. */
2245 if (save_write_to)
2246 {
2247 --lnum;
2248 buffer->b_write_to_channel = FALSE;
2249 }
2250
2251 /* Append to the buffer */
2252 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2253
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002254 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002255 curbuf = buffer;
2256 u_sync(TRUE);
2257 /* ignore undo failure, undo is not very useful here */
2258 ignored = u_save(lnum, lnum + 1);
2259
2260 ml_append(lnum, msg, 0, FALSE);
2261 appended_lines_mark(lnum, 1L);
2262 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002263 if (ch_part->ch_nomodifiable)
2264 buffer->b_p_ma = FALSE;
2265 else
2266 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002267
2268 if (buffer->b_nwindows > 0)
2269 {
2270 win_T *wp;
2271 win_T *save_curwin;
2272
2273 FOR_ALL_WINDOWS(wp)
2274 {
2275 if (wp->w_buffer == buffer
2276 && (save_write_to
2277 ? wp->w_cursor.lnum == lnum + 1
2278 : (wp->w_cursor.lnum == lnum
2279 && wp->w_cursor.col == 0)))
2280 {
2281 ++wp->w_cursor.lnum;
2282 save_curwin = curwin;
2283 curwin = wp;
2284 curbuf = curwin->w_buffer;
2285 scroll_cursor_bot(0, FALSE);
2286 curwin = save_curwin;
2287 curbuf = curwin->w_buffer;
2288 }
2289 }
2290 redraw_buf_later(buffer, VALID);
2291 channel_need_redraw = TRUE;
2292 }
2293
2294 if (save_write_to)
2295 {
2296 channel_T *ch;
2297
2298 /* Find channels reading from this buffer and adjust their
2299 * next-to-read line number. */
2300 buffer->b_write_to_channel = TRUE;
2301 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2302 {
2303 chanpart_T *in_part = &ch->ch_part[PART_IN];
2304
2305 if (in_part->ch_buffer == buffer)
2306 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2307 }
2308 }
2309}
2310
Bram Moolenaar437905c2016-04-26 19:01:05 +02002311 static void
2312drop_messages(channel_T *channel, int part)
2313{
2314 char_u *msg;
2315
2316 while ((msg = channel_get(channel, part)) != NULL)
2317 {
2318 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2319 vim_free(msg);
2320 }
2321}
2322
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002323/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002324 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002325 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002326 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002327 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002328 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002329may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002330{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002331 char_u *msg = NULL;
2332 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002333 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002334 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002335 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002336 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002337 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002338 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002339 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002340 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002341 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002342
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002343 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002344 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002345 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002346
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002347 /* Use a message-specific callback, part callback or channel callback */
2348 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2349 if (cbitem->cq_seq_nr == 0)
2350 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002351 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002352 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002353 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002354 partial = cbitem->cq_partial;
2355 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002356 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002357 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002358 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002359 partial = channel->ch_part[part].ch_partial;
2360 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002361 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002362 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002363 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002364 partial = channel->ch_partial;
2365 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002366
Bram Moolenaar187db502016-02-27 14:44:26 +01002367 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002368 if (buffer != NULL && !buf_valid(buffer))
2369 {
2370 /* buffer was wiped out */
2371 channel->ch_part[part].ch_buffer = NULL;
2372 buffer = NULL;
2373 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002374
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002375 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002376 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002377 listitem_T *item;
2378 int argc = 0;
2379
Bram Moolenaard7ece102016-02-02 23:23:02 +01002380 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002381 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002382 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002383 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002384 channel_parse_json(channel, part);
2385 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002386 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002387 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002388
Bram Moolenaarece61b02016-02-20 21:39:05 +01002389 for (item = listtv->vval.v_list->lv_first;
2390 item != NULL && argc < CH_JSON_MAX_ARGS;
2391 item = item->li_next)
2392 argv[argc++] = item->li_tv;
2393 while (argc < CH_JSON_MAX_ARGS)
2394 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002395
Bram Moolenaarece61b02016-02-20 21:39:05 +01002396 if (argv[0].v_type == VAR_STRING)
2397 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002398 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002399 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002400 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002401 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002402 }
2403
Bram Moolenaarece61b02016-02-20 21:39:05 +01002404 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002405 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002406 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002407 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002408 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002409 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002410 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002411 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002412 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002413 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002414 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002415 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002416 return FALSE;
2417 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002418 else
2419 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002420 /* If there is no callback or buffer drop the message. */
2421 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002422 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002423 /* If there is a close callback it may use ch_read() to get the
2424 * messages. */
2425 if (channel->ch_close_cb == NULL)
2426 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002427 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002428 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002429
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002430 if (ch_mode == MODE_NL)
2431 {
2432 char_u *nl;
2433 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002434 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002435
2436 /* See if we have a message ending in NL in the first buffer. If
2437 * not try to concatenate the first and the second buffer. */
2438 while (TRUE)
2439 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002440 node = channel_peek(channel, part);
2441 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002442 if (nl != NULL)
2443 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002444 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002445 return FALSE; /* incomplete message */
2446 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002447 buf = node->rq_buffer;
2448
2449 /* Convert NUL to NL, the internal representation. */
2450 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2451 if (*p == NUL)
2452 *p = NL;
2453
2454 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002455 {
2456 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002457 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002458 *nl = NUL;
2459 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002460 else
2461 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002462 /* Copy the message into allocated memory (excluding the NL)
2463 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002464 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002465 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002466 }
2467 }
2468 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002469 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002470 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002471 * get everything we have.
2472 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002473 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002474 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002475
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002476 if (msg == NULL)
2477 return FALSE; /* out of memory (and avoids Coverity warning) */
2478
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002479 argv[1].v_type = VAR_STRING;
2480 argv[1].vval.v_string = msg;
2481 }
2482
Bram Moolenaara07fec92016-02-05 21:04:08 +01002483 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002484 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002485 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002486
2487 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002488 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002489 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002490 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002491 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002492 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002493 break;
2494 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002495 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002496 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002497 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002498 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002499 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002500 if (buffer != NULL)
2501 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002502 if (msg == NULL)
2503 /* JSON or JS mode: re-encode the message. */
2504 msg = json_encode(listtv, ch_mode);
2505 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002506 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002507 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002508
Bram Moolenaar187db502016-02-27 14:44:26 +01002509 if (callback != NULL)
2510 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002511 if (cbitem != NULL)
2512 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2513 else
2514 {
2515 /* invoke the channel callback */
2516 ch_logs(channel, "Invoking channel callback %s",
2517 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002518 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002519 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002520 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002521 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002522 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002523 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002524
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002525 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002526 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002527 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002528
2529 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002530}
2531
2532/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002533 * Return TRUE when channel "channel" is open for writing to.
2534 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002535 */
2536 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002537channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002538{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002539 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002540 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002541}
2542
2543/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002544 * Return TRUE when channel "channel" is open for reading or writing.
2545 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002546 */
2547 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002548channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002549{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002550 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002551 || channel->CH_IN_FD != INVALID_FD
2552 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002553 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002554}
2555
2556/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002557 * Return TRUE if "channel" has JSON or other typeahead.
2558 */
2559 static int
2560channel_has_readahead(channel_T *channel, int part)
2561{
2562 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2563
2564 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2565 {
2566 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2567 jsonq_T *item = head->jq_next;
2568
2569 return item != NULL;
2570 }
2571 return channel_peek(channel, part) != NULL;
2572}
2573
2574/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002575 * Return a string indicating the status of the channel.
2576 */
2577 char *
2578channel_status(channel_T *channel)
2579{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002580 int part;
2581 int has_readahead = FALSE;
2582
Bram Moolenaar77073442016-02-13 23:23:53 +01002583 if (channel == NULL)
2584 return "fail";
2585 if (channel_is_open(channel))
2586 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002587 for (part = PART_SOCK; part <= PART_ERR; ++part)
2588 if (channel_has_readahead(channel, part))
2589 {
2590 has_readahead = TRUE;
2591 break;
2592 }
2593
2594 if (has_readahead)
2595 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002596 return "closed";
2597}
2598
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002599 static void
2600channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2601{
2602 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002603 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002604 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002605 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002606
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002607 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002608 STRCAT(namebuf, "_");
2609 tail = STRLEN(namebuf);
2610
2611 STRCPY(namebuf + tail, "status");
2612 dict_add_nr_str(dict, namebuf, 0,
2613 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2614
2615 STRCPY(namebuf + tail, "mode");
2616 switch (chanpart->ch_mode)
2617 {
2618 case MODE_NL: s = "NL"; break;
2619 case MODE_RAW: s = "RAW"; break;
2620 case MODE_JSON: s = "JSON"; break;
2621 case MODE_JS: s = "JS"; break;
2622 }
2623 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2624
2625 STRCPY(namebuf + tail, "io");
2626 if (part == PART_SOCK)
2627 s = "socket";
2628 else switch (chanpart->ch_io)
2629 {
2630 case JIO_NULL: s = "null"; break;
2631 case JIO_PIPE: s = "pipe"; break;
2632 case JIO_FILE: s = "file"; break;
2633 case JIO_BUFFER: s = "buffer"; break;
2634 case JIO_OUT: s = "out"; break;
2635 }
2636 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2637
2638 STRCPY(namebuf + tail, "timeout");
2639 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2640}
2641
2642 void
2643channel_info(channel_T *channel, dict_T *dict)
2644{
2645 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2646 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2647
2648 if (channel->ch_hostname != NULL)
2649 {
2650 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2651 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2652 channel_part_info(channel, dict, "sock", PART_SOCK);
2653 }
2654 else
2655 {
2656 channel_part_info(channel, dict, "out", PART_OUT);
2657 channel_part_info(channel, dict, "err", PART_ERR);
2658 channel_part_info(channel, dict, "in", PART_IN);
2659 }
2660}
2661
Bram Moolenaar77073442016-02-13 23:23:53 +01002662/*
2663 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002664 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002665 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002666 */
2667 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002668channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002669{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002670 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002671
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002672#ifdef FEAT_GUI
2673 channel_gui_unregister(channel);
2674#endif
2675
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002676 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002677 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002678 sock_close(channel->CH_SOCK_FD);
2679 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002680 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002681 may_close_part(&channel->CH_IN_FD);
2682 may_close_part(&channel->CH_OUT_FD);
2683 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002684
Bram Moolenaar8b374212016-02-24 20:43:06 +01002685 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002686 {
2687 typval_T argv[1];
2688 typval_T rettv;
2689 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002690 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002691
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002692 /* Invoke callbacks before the close callback, since it's weird to
2693 * first invoke the close callback. Increment the refcount to avoid
2694 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002695 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002696 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002697 for (part = PART_SOCK; part <= PART_ERR; ++part)
2698 while (may_invoke_callback(channel, part))
2699 ;
2700
2701 /* Invoke the close callback, if still set. */
2702 if (channel->ch_close_cb != NULL)
2703 {
2704 ch_logs(channel, "Invoking close callback %s",
2705 (char *)channel->ch_close_cb);
2706 argv[0].v_type = VAR_CHANNEL;
2707 argv[0].vval.v_channel = channel;
2708 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002709 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2710 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002711 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002712 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002713 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002714
2715 /* the callback is only called once */
2716 vim_free(channel->ch_close_cb);
2717 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002718 partial_unref(channel->ch_close_partial);
2719 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002720
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002721 --channel->ch_refcount;
2722
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002723 if (channel_need_redraw)
2724 {
2725 channel_need_redraw = FALSE;
2726 redraw_after_callback();
2727 }
2728
Bram Moolenaar437905c2016-04-26 19:01:05 +02002729 /* any remaining messages are useless now */
2730 for (part = PART_SOCK; part <= PART_ERR; ++part)
2731 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002732 }
2733
2734 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002735}
2736
Bram Moolenaard04a0202016-01-26 23:30:18 +01002737/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002738 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002739 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002740 static void
2741channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002742{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002743 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2744 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002745
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002746 while (channel_peek(channel, part) != NULL)
2747 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002748
2749 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002750 {
2751 cbq_T *node = cb_head->cq_next;
2752
2753 remove_cb_node(cb_head, node);
2754 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002755 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002756 vim_free(node);
2757 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002758
2759 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002760 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002761 free_tv(json_head->jq_next->jq_value);
2762 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002763 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002764
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002765 vim_free(channel->ch_part[part].ch_callback);
2766 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002767 partial_unref(channel->ch_part[part].ch_partial);
2768 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002769}
2770
2771/*
2772 * Clear all the read buffers on "channel".
2773 */
2774 void
2775channel_clear(channel_T *channel)
2776{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002777 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002778 vim_free(channel->ch_hostname);
2779 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002780 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002781 channel_clear_one(channel, PART_OUT);
2782 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002783 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002784 vim_free(channel->ch_callback);
2785 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002786 partial_unref(channel->ch_partial);
2787 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002788 vim_free(channel->ch_close_cb);
2789 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002790 partial_unref(channel->ch_close_partial);
2791 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002792}
2793
Bram Moolenaar77073442016-02-13 23:23:53 +01002794#if defined(EXITFREE) || defined(PROTO)
2795 void
2796channel_free_all(void)
2797{
2798 channel_T *channel;
2799
Bram Moolenaard6051b52016-02-28 15:49:03 +01002800 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002801 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2802 channel_clear(channel);
2803}
2804#endif
2805
2806
Bram Moolenaar715d2852016-04-30 17:06:31 +02002807/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002808#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002809
2810/* Buffer size for reading incoming messages. */
2811#define MAXMSGSIZE 4096
2812
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002813#if defined(HAVE_SELECT)
2814/*
2815 * Add write fds where we are waiting for writing to be possible.
2816 */
2817 static int
2818channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2819{
2820 int maxfd = maxfd_arg;
2821 channel_T *ch;
2822
2823 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2824 {
2825 chanpart_T *in_part = &ch->ch_part[PART_IN];
2826
2827 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2828 {
2829 FD_SET((int)in_part->ch_fd, wfds);
2830 if ((int)in_part->ch_fd >= maxfd)
2831 maxfd = (int)in_part->ch_fd + 1;
2832 }
2833 }
2834 return maxfd;
2835}
2836#else
2837/*
2838 * Add write fds where we are waiting for writing to be possible.
2839 */
2840 static int
2841channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2842{
2843 int nfd = nfd_in;
2844 channel_T *ch;
2845
2846 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2847 {
2848 chanpart_T *in_part = &ch->ch_part[PART_IN];
2849
2850 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2851 {
2852 in_part->ch_poll_idx = nfd;
2853 fds[nfd].fd = in_part->ch_fd;
2854 fds[nfd].events = POLLOUT;
2855 ++nfd;
2856 }
2857 else
2858 in_part->ch_poll_idx = -1;
2859 }
2860 return nfd;
2861}
2862#endif
2863
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002864typedef enum {
2865 CW_READY,
2866 CW_NOT_READY,
2867 CW_ERROR
2868} channel_wait_result;
2869
Bram Moolenaard04a0202016-01-26 23:30:18 +01002870/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002871 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002872 * Return CW_READY when there is something to read.
2873 * Return CW_NOT_READY when there is nothing to read.
2874 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002875 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002876 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002877channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002878{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002879 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002880 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002881
Bram Moolenaard8070362016-02-15 21:56:54 +01002882# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002883 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002884 {
2885 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002886 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002887 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002888 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002889
2890 /* reading from a pipe, not a socket */
2891 while (TRUE)
2892 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002893 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2894
2895 if (r && nread > 0)
2896 return CW_READY;
2897 if (r == 0)
2898 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002899
2900 /* perhaps write some buffer lines */
2901 channel_write_any_lines();
2902
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002903 sleep_time = deadline - GetTickCount();
2904 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002905 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002906 /* Wait for a little while. Very short at first, up to 10 msec
2907 * after looping a few times. */
2908 if (sleep_time > delay)
2909 sleep_time = delay;
2910 Sleep(sleep_time);
2911 delay = delay * 2;
2912 if (delay > 10)
2913 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002914 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002915 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002916 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002917#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002918 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002919#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002920 struct timeval tval;
2921 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002922 fd_set wfds;
2923 int ret;
2924 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002925
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002926 tval.tv_sec = timeout / 1000;
2927 tval.tv_usec = (timeout % 1000) * 1000;
2928 for (;;)
2929 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002930 FD_ZERO(&rfds);
2931 FD_SET((int)fd, &rfds);
2932
2933 /* Write lines to a pipe when a pipe can be written to. Need to
2934 * set this every time, some buffers may be done. */
2935 maxfd = (int)fd + 1;
2936 FD_ZERO(&wfds);
2937 maxfd = channel_fill_wfds(maxfd, &wfds);
2938
2939 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002940# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002941 SOCK_ERRNO;
2942 if (ret == -1 && errno == EINTR)
2943 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002944# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002945 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002946 {
2947 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002948 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002949 channel_write_any_lines();
2950 continue;
2951 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002952 break;
2953 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002954#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002955 for (;;)
2956 {
2957 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2958 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002959
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002960 fds[0].fd = fd;
2961 fds[0].events = POLLIN;
2962 nfd = channel_fill_poll_write(nfd, fds);
2963 if (poll(fds, nfd, timeout) > 0)
2964 {
2965 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002966 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002967 channel_write_any_lines();
2968 continue;
2969 }
2970 break;
2971 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002972#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002973 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002974 return CW_NOT_READY;
2975}
2976
2977 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002978channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002979{
2980 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002981 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2982 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002983
2984 /* Queue a "DETACH" netbeans message in the command queue in order to
2985 * terminate the netbeans session later. Do not end the session here
2986 * directly as we may be running in the context of a call to
2987 * netbeans_parse_messages():
2988 * netbeans_parse_messages
2989 * -> autocmd triggered while processing the netbeans cmd
2990 * -> ui_breakcheck
2991 * -> gui event loop or select loop
2992 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002993 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002994 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002995 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002996 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002997 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2998
2999 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003000 * died. Don't close the channel right away, it may be the wrong moment
3001 * to invoke callbacks. */
3002 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003003
3004#ifdef FEAT_GUI
3005 /* Stop listening to GUI events right away. */
3006 channel_gui_unregister(channel);
3007#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003008}
3009
3010 static void
3011channel_close_now(channel_T *channel)
3012{
3013 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003014 channel_close(channel, TRUE);
3015 if (channel->ch_nb_close_cb != NULL)
3016 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003017}
3018
3019/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003020 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003021 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003022 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003023 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003024 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003025channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003026{
3027 static char_u *buf = NULL;
3028 int len = 0;
3029 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003030 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003031 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003032
Bram Moolenaar5850a762016-05-27 19:59:48 +02003033 /* If we detected a read error don't try reading again. */
3034 if (channel->ch_to_be_closed)
3035 return;
3036
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003037 fd = channel->ch_part[part].ch_fd;
3038 if (fd == INVALID_FD)
3039 {
3040 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003041 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003042 }
3043 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003044
3045 /* Allocate a buffer to read into. */
3046 if (buf == NULL)
3047 {
3048 buf = alloc(MAXMSGSIZE);
3049 if (buf == NULL)
3050 return; /* out of memory! */
3051 }
3052
3053 /* Keep on reading for as long as there is something to read.
3054 * Use select() or poll() to avoid blocking on a message that is exactly
3055 * MAXMSGSIZE long. */
3056 for (;;)
3057 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003058 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003059 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003060 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003061 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003062 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003063 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003064 if (len <= 0)
3065 break; /* error or nothing more to read */
3066
3067 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003068 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003069 readlen += len;
3070 if (len < MAXMSGSIZE)
3071 break; /* did read everything that's available */
3072 }
3073
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003074 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003075 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003076 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003077
3078#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003079 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003080 if (CH_HAS_GUI && gtk_main_level() > 0)
3081 gtk_main_quit();
3082#endif
3083}
3084
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003085/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003086 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003087 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003088 * Returns what was read in allocated memory.
3089 * Returns NULL in case of error or timeout.
3090 */
3091 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003092channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003093{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003094 char_u *buf;
3095 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003096 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003097 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003098 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003099 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003100
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003101 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003102 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003103
3104 while (TRUE)
3105 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003106 node = channel_peek(channel, part);
3107 if (node != NULL)
3108 {
3109 if (mode == MODE_RAW || (mode == MODE_NL
3110 && channel_first_nl(node) != NULL))
3111 /* got a complete message */
3112 break;
3113 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3114 continue;
3115 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003116
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003117 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003118 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003119 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003120 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003121 {
3122 ch_log(channel, "Timed out");
3123 return NULL;
3124 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003125 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003126 }
3127
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003128 if (mode == MODE_RAW)
3129 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003130 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003131 }
3132 else
3133 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003134 char_u *p;
3135
3136 buf = node->rq_buffer;
3137 nl = channel_first_nl(node);
3138
3139 /* Convert NUL to NL, the internal representation. */
3140 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3141 if (*p == NUL)
3142 *p = NL;
3143
3144 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003145 {
3146 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003147 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003148 *nl = NUL;
3149 }
3150 else
3151 {
3152 /* Copy the message into allocated memory and remove it from the
3153 * buffer. */
3154 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003155 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003156 }
3157 }
3158 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003159 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003160 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003161}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003162
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003163/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003164 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003165 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003166 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003167 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003168 */
3169 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003170channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003171 channel_T *channel,
3172 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003173 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003174 int id,
3175 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003176{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003177 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003178 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003179 int timeout;
3180 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003181
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003182 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003183 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003184 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003185 for (;;)
3186 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003187 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003188
3189 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003190 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003191 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003192 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003193 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003194 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003195
Bram Moolenaard7ece102016-02-02 23:23:02 +01003196 if (!more)
3197 {
3198 /* Handle any other messages in the queue. If done some more
3199 * messages may have arrived. */
3200 if (channel_parse_messages())
3201 continue;
3202
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003203 /* Wait for up to the timeout. If there was an incomplete message
3204 * use the deadline for that. */
3205 timeout = timeout_arg;
3206 if (chanpart->ch_waiting)
3207 {
3208#ifdef WIN32
3209 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3210#else
3211 {
3212 struct timeval now_tv;
3213
3214 gettimeofday(&now_tv, NULL);
3215 timeout = (chanpart->ch_deadline.tv_sec
3216 - now_tv.tv_sec) * 1000
3217 + (chanpart->ch_deadline.tv_usec
3218 - now_tv.tv_usec) / 1000
3219 + 1;
3220 }
3221#endif
3222 if (timeout < 0)
3223 {
3224 /* Something went wrong, channel_parse_json() didn't
3225 * discard message. Cancel waiting. */
3226 chanpart->ch_waiting = FALSE;
3227 timeout = timeout_arg;
3228 }
3229 else if (timeout > timeout_arg)
3230 timeout = timeout_arg;
3231 }
3232 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003233 if (fd == INVALID_FD
3234 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003235 {
3236 if (timeout == timeout_arg)
3237 {
3238 if (fd != INVALID_FD)
3239 ch_log(channel, "Timed out");
3240 break;
3241 }
3242 }
3243 else
3244 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003245 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003246 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003247 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003248 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003249}
3250
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003251/*
3252 * Common for ch_read() and ch_readraw().
3253 */
3254 void
3255common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3256{
3257 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003258 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003259 jobopt_T opt;
3260 int mode;
3261 int timeout;
3262 int id = -1;
3263 typval_T *listtv = NULL;
3264
3265 /* return an empty string by default */
3266 rettv->v_type = VAR_STRING;
3267 rettv->vval.v_string = NULL;
3268
3269 clear_job_options(&opt);
3270 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3271 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003272 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003273
Bram Moolenaar437905c2016-04-26 19:01:05 +02003274 if (opt.jo_set & JO_PART)
3275 part = opt.jo_part;
3276 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003277 if (channel != NULL)
3278 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003279 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003280 part = channel_part_read(channel);
3281 mode = channel_get_mode(channel, part);
3282 timeout = channel_get_timeout(channel, part);
3283 if (opt.jo_set & JO_TIMEOUT)
3284 timeout = opt.jo_timeout;
3285
3286 if (raw || mode == MODE_RAW || mode == MODE_NL)
3287 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3288 else
3289 {
3290 if (opt.jo_set & JO_ID)
3291 id = opt.jo_id;
3292 channel_read_json_block(channel, part, timeout, id, &listtv);
3293 if (listtv != NULL)
3294 {
3295 *rettv = *listtv;
3296 vim_free(listtv);
3297 }
3298 else
3299 {
3300 rettv->v_type = VAR_SPECIAL;
3301 rettv->vval.v_number = VVAL_NONE;
3302 }
3303 }
3304 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003305
3306theend:
3307 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003308}
3309
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003310# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3311 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003312/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003313 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003314 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003315 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003316 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003317channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003318{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003319 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003320 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003321
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003322 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003323 for (channel = first_channel; channel != NULL;
3324 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003325 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003326 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003327 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003328 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003329 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003330 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003331 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003332 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003333 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003334}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003335# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003336
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003337# if defined(WIN32) || defined(PROTO)
3338/*
3339 * Check the channels for anything that is ready to be read.
3340 * The data is put in the read queue.
3341 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003342 void
3343channel_handle_events(void)
3344{
3345 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003346 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003347 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003348
3349 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3350 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003351 /* If we detected a read error don't try reading again. */
3352 if (channel->ch_to_be_closed)
3353 continue;
3354
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003355 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003356 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003357 {
3358 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003359 if (fd != INVALID_FD)
3360 {
3361 int r = channel_wait(channel, fd, 0);
3362
3363 if (r == CW_READY)
3364 channel_read(channel, part, "channel_handle_events");
3365 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003366 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003367 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003368 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003369 }
3370}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003371# endif
3372
Bram Moolenaard04a0202016-01-26 23:30:18 +01003373/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003374 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003375 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003376 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003377 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003378 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003379channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003380{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003381 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003382 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003383 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003384
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003385 fd = channel->ch_part[part].ch_fd;
3386 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003387 {
3388 if (!channel->ch_error && fun != NULL)
3389 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003390 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003391 EMSG2("E630: %s(): write while not connected", fun);
3392 }
3393 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003394 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003395 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003396
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003397 if (log_fd != NULL)
3398 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003399 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003400 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003401 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003402 fprintf(log_fd, "'\n");
3403 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003404 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003405 }
3406
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003407 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003408 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003409 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003410 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003411 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003412 {
3413 if (!channel->ch_error && fun != NULL)
3414 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003415 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003416 EMSG2("E631: %s(): write failed", fun);
3417 }
3418 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003419 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003420 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003421
3422 channel->ch_error = FALSE;
3423 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003424}
3425
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003426/*
3427 * Common for "ch_sendexpr()" and "ch_sendraw()".
3428 * Returns the channel if the caller should read the response.
3429 * Sets "part_read" to the the read fd.
3430 * Otherwise returns NULL.
3431 */
3432 channel_T *
3433send_common(
3434 typval_T *argvars,
3435 char_u *text,
3436 int id,
3437 int eval,
3438 jobopt_T *opt,
3439 char *fun,
3440 int *part_read)
3441{
3442 channel_T *channel;
3443 int part_send;
3444
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003445 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003446 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003447 if (channel == NULL)
3448 return NULL;
3449 part_send = channel_part_send(channel);
3450 *part_read = channel_part_read(channel);
3451
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003452 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3453 return NULL;
3454
3455 /* Set the callback. An empty callback means no callback and not reading
3456 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3457 * allowed. */
3458 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3459 {
3460 if (eval)
3461 {
3462 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3463 return NULL;
3464 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003465 channel_set_req_callback(channel, part_send,
3466 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003467 }
3468
3469 if (channel_send(channel, part_send, text, fun) == OK
3470 && opt->jo_callback == NULL)
3471 return channel;
3472 return NULL;
3473}
3474
3475/*
3476 * common for "ch_evalexpr()" and "ch_sendexpr()"
3477 */
3478 void
3479ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3480{
3481 char_u *text;
3482 typval_T *listtv;
3483 channel_T *channel;
3484 int id;
3485 ch_mode_T ch_mode;
3486 int part_send;
3487 int part_read;
3488 jobopt_T opt;
3489 int timeout;
3490
3491 /* return an empty string by default */
3492 rettv->v_type = VAR_STRING;
3493 rettv->vval.v_string = NULL;
3494
Bram Moolenaar437905c2016-04-26 19:01:05 +02003495 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003496 if (channel == NULL)
3497 return;
3498 part_send = channel_part_send(channel);
3499
3500 ch_mode = channel_get_mode(channel, part_send);
3501 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3502 {
3503 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3504 return;
3505 }
3506
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003507 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003508 text = json_encode_nr_expr(id, &argvars[1],
3509 ch_mode == MODE_JS ? JSON_JS : 0);
3510 if (text == NULL)
3511 return;
3512
3513 channel = send_common(argvars, text, id, eval, &opt,
3514 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3515 vim_free(text);
3516 if (channel != NULL && eval)
3517 {
3518 if (opt.jo_set & JO_TIMEOUT)
3519 timeout = opt.jo_timeout;
3520 else
3521 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003522 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3523 == OK)
3524 {
3525 list_T *list = listtv->vval.v_list;
3526
3527 /* Move the item from the list and then change the type to
3528 * avoid the value being freed. */
3529 *rettv = list->lv_last->li_tv;
3530 list->lv_last->li_tv.v_type = VAR_NUMBER;
3531 free_tv(listtv);
3532 }
3533 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003534 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003535}
3536
3537/*
3538 * common for "ch_evalraw()" and "ch_sendraw()"
3539 */
3540 void
3541ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3542{
3543 char_u buf[NUMBUFLEN];
3544 char_u *text;
3545 channel_T *channel;
3546 int part_read;
3547 jobopt_T opt;
3548 int timeout;
3549
3550 /* return an empty string by default */
3551 rettv->v_type = VAR_STRING;
3552 rettv->vval.v_string = NULL;
3553
3554 text = get_tv_string_buf(&argvars[1], buf);
3555 channel = send_common(argvars, text, 0, eval, &opt,
3556 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3557 if (channel != NULL && eval)
3558 {
3559 if (opt.jo_set & JO_TIMEOUT)
3560 timeout = opt.jo_timeout;
3561 else
3562 timeout = channel_get_timeout(channel, part_read);
3563 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3564 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003565 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003566}
3567
Bram Moolenaard04a0202016-01-26 23:30:18 +01003568# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003569/*
3570 * Add open channels to the poll struct.
3571 * Return the adjusted struct index.
3572 * The type of "fds" is hidden to avoid problems with the function proto.
3573 */
3574 int
3575channel_poll_setup(int nfd_in, void *fds_in)
3576{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003577 int nfd = nfd_in;
3578 channel_T *channel;
3579 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003580 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003581
Bram Moolenaar77073442016-02-13 23:23:53 +01003582 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003583 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003584 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003585 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003586 chanpart_T *ch_part = &channel->ch_part[part];
3587
3588 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003589 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003590 ch_part->ch_poll_idx = nfd;
3591 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003592 fds[nfd].events = POLLIN;
3593 nfd++;
3594 }
3595 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003596 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003597 }
3598 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003599
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003600 nfd = channel_fill_poll_write(nfd, fds);
3601
Bram Moolenaare0874f82016-01-24 20:36:41 +01003602 return nfd;
3603}
3604
3605/*
3606 * The type of "fds" is hidden to avoid problems with the function proto.
3607 */
3608 int
3609channel_poll_check(int ret_in, void *fds_in)
3610{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003611 int ret = ret_in;
3612 channel_T *channel;
3613 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003614 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003615 int idx;
3616 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003617
Bram Moolenaar77073442016-02-13 23:23:53 +01003618 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003619 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003620 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003621 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003622 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003623
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003624 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003625 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003626 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003627 --ret;
3628 }
3629 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003630
3631 in_part = &channel->ch_part[PART_IN];
3632 idx = in_part->ch_poll_idx;
3633 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3634 {
3635 if (in_part->ch_buf_append)
3636 {
3637 if (in_part->ch_buffer != NULL)
3638 channel_write_new_lines(in_part->ch_buffer);
3639 }
3640 else
3641 channel_write_in(channel);
3642 --ret;
3643 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003644 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003645
3646 return ret;
3647}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003648# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003649
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003650# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003651/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003652 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003653 */
3654 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003655channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003656{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003657 int maxfd = maxfd_in;
3658 channel_T *channel;
3659 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003660 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003661 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003662
Bram Moolenaar77073442016-02-13 23:23:53 +01003663 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003664 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003665 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003666 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003667 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003668
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003669 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003670 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003671 FD_SET((int)fd, rfds);
3672 if (maxfd < (int)fd)
3673 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003674 }
3675 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003676 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003677
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003678 maxfd = channel_fill_wfds(maxfd, wfds);
3679
Bram Moolenaare0874f82016-01-24 20:36:41 +01003680 return maxfd;
3681}
3682
3683/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003684 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003685 */
3686 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003687channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003688{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003689 int ret = ret_in;
3690 channel_T *channel;
3691 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003692 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003693 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003694 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003695
Bram Moolenaar77073442016-02-13 23:23:53 +01003696 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003697 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003698 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003699 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003700 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003701
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003702 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003703 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003704 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003705 --ret;
3706 }
3707 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003708
3709 in_part = &channel->ch_part[PART_IN];
3710 if (ret > 0 && in_part->ch_fd != INVALID_FD
3711 && FD_ISSET(in_part->ch_fd, wfds))
3712 {
3713 if (in_part->ch_buf_append)
3714 {
3715 if (in_part->ch_buffer != NULL)
3716 channel_write_new_lines(in_part->ch_buffer);
3717 }
3718 else
3719 channel_write_in(channel);
3720 --ret;
3721 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003722 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003723
3724 return ret;
3725}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003726# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003727
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003728/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003729 * Execute queued up commands.
3730 * Invoked from the main loop when it's safe to execute received commands.
3731 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003732 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003733 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003734channel_parse_messages(void)
3735{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003736 channel_T *channel = first_channel;
3737 int ret = FALSE;
3738 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003739 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003740
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003741 ++safe_to_invoke_callback;
3742
Bram Moolenaard0b65022016-03-06 21:50:33 +01003743 /* Only do this message when another message was given, otherwise we get
3744 * lots of them. */
3745 if (did_log_msg)
3746 {
3747 ch_log(NULL, "looking for messages on channels");
3748 did_log_msg = FALSE;
3749 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003750 while (channel != NULL)
3751 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003752 if (channel->ch_to_be_closed)
3753 {
3754 channel->ch_to_be_closed = FALSE;
3755 channel_close_now(channel);
3756 /* channel may have been freed, start over */
3757 channel = first_channel;
3758 continue;
3759 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003760 if (channel->ch_to_be_freed)
3761 {
3762 channel_free(channel);
3763 /* channel has been freed, start over */
3764 channel = first_channel;
3765 continue;
3766 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003767 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003768 {
3769 /* channel is no longer useful, free it */
3770 channel_free(channel);
3771 channel = first_channel;
3772 part = PART_SOCK;
3773 continue;
3774 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003775 if (channel->ch_part[part].ch_fd != INVALID_FD
3776 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003777 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003778 /* Increase the refcount, in case the handler causes the channel
3779 * to be unreferenced or closed. */
3780 ++channel->ch_refcount;
3781 r = may_invoke_callback(channel, part);
3782 if (r == OK)
3783 ret = TRUE;
3784 if (channel_unref(channel) || r == OK)
3785 {
3786 /* channel was freed or something was done, start over */
3787 channel = first_channel;
3788 part = PART_SOCK;
3789 continue;
3790 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003791 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003792 if (part < PART_ERR)
3793 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003794 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003795 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003796 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003797 part = PART_SOCK;
3798 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003799 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003800
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003801 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003802 {
3803 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003804 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003805 }
3806
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003807 --safe_to_invoke_callback;
3808
Bram Moolenaard7ece102016-02-02 23:23:02 +01003809 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003810}
3811
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003812/*
3813 * Mark references to lists used in channels.
3814 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003815 int
3816set_ref_in_channel(int copyID)
3817{
Bram Moolenaar77073442016-02-13 23:23:53 +01003818 int abort = FALSE;
3819 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003820 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003821
Bram Moolenaar77073442016-02-13 23:23:53 +01003822 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003823 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003824 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003825 tv.v_type = VAR_CHANNEL;
3826 tv.vval.v_channel = channel;
3827 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003828 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003829 return abort;
3830}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003831
3832/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003833 * Return the "part" to write to for "channel".
3834 */
3835 int
3836channel_part_send(channel_T *channel)
3837{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003838 if (channel->CH_SOCK_FD == INVALID_FD)
3839 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003840 return PART_SOCK;
3841}
3842
3843/*
3844 * Return the default "part" to read from for "channel".
3845 */
3846 int
3847channel_part_read(channel_T *channel)
3848{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003849 if (channel->CH_SOCK_FD == INVALID_FD)
3850 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003851 return PART_SOCK;
3852}
3853
3854/*
3855 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003856 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003857 */
3858 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003859channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003860{
Bram Moolenaar77073442016-02-13 23:23:53 +01003861 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003862 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003863 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003864}
3865
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003866/*
3867 * Return the timeout of "channel"/"part"
3868 */
3869 int
3870channel_get_timeout(channel_T *channel, int part)
3871{
3872 return channel->ch_part[part].ch_timeout;
3873}
3874
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003875 static int
3876handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3877{
3878 char_u *val = get_tv_string(item);
3879
3880 opt->jo_set |= jo;
3881 if (STRCMP(val, "nl") == 0)
3882 *modep = MODE_NL;
3883 else if (STRCMP(val, "raw") == 0)
3884 *modep = MODE_RAW;
3885 else if (STRCMP(val, "js") == 0)
3886 *modep = MODE_JS;
3887 else if (STRCMP(val, "json") == 0)
3888 *modep = MODE_JSON;
3889 else
3890 {
3891 EMSG2(_(e_invarg2), val);
3892 return FAIL;
3893 }
3894 return OK;
3895}
3896
3897 static int
3898handle_io(typval_T *item, int part, jobopt_T *opt)
3899{
3900 char_u *val = get_tv_string(item);
3901
3902 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3903 if (STRCMP(val, "null") == 0)
3904 opt->jo_io[part] = JIO_NULL;
3905 else if (STRCMP(val, "pipe") == 0)
3906 opt->jo_io[part] = JIO_PIPE;
3907 else if (STRCMP(val, "file") == 0)
3908 opt->jo_io[part] = JIO_FILE;
3909 else if (STRCMP(val, "buffer") == 0)
3910 opt->jo_io[part] = JIO_BUFFER;
3911 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3912 opt->jo_io[part] = JIO_OUT;
3913 else
3914 {
3915 EMSG2(_(e_invarg2), val);
3916 return FAIL;
3917 }
3918 return OK;
3919}
3920
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003921/*
3922 * Clear a jobopt_T before using it.
3923 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003924 void
3925clear_job_options(jobopt_T *opt)
3926{
3927 vim_memset(opt, 0, sizeof(jobopt_T));
3928}
3929
3930/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003931 * Free any members of a jobopt_T.
3932 */
3933 void
3934free_job_options(jobopt_T *opt)
3935{
3936 if (opt->jo_partial != NULL)
3937 partial_unref(opt->jo_partial);
3938 if (opt->jo_out_partial != NULL)
3939 partial_unref(opt->jo_out_partial);
3940 if (opt->jo_err_partial != NULL)
3941 partial_unref(opt->jo_err_partial);
3942 if (opt->jo_close_partial != NULL)
3943 partial_unref(opt->jo_close_partial);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003944 if (opt->jo_exit_partial != NULL)
3945 partial_unref(opt->jo_exit_partial);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003946}
3947
3948/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003949 * Get the PART_ number from the first character of an option name.
3950 */
3951 static int
3952part_from_char(int c)
3953{
3954 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3955}
3956
3957/*
3958 * Get the option entries from the dict in "tv", parse them and put the result
3959 * in "opt".
3960 * Only accept options in "supported".
3961 * If an option value is invalid return FAIL.
3962 */
3963 int
3964get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3965{
3966 typval_T *item;
3967 char_u *val;
3968 dict_T *dict;
3969 int todo;
3970 hashitem_T *hi;
3971 int part;
3972
3973 opt->jo_set = 0;
3974 if (tv->v_type == VAR_UNKNOWN)
3975 return OK;
3976 if (tv->v_type != VAR_DICT)
3977 {
3978 EMSG(_(e_invarg));
3979 return FAIL;
3980 }
3981 dict = tv->vval.v_dict;
3982 if (dict == NULL)
3983 return OK;
3984
3985 todo = (int)dict->dv_hashtab.ht_used;
3986 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3987 if (!HASHITEM_EMPTY(hi))
3988 {
3989 item = &dict_lookup(hi)->di_tv;
3990
3991 if (STRCMP(hi->hi_key, "mode") == 0)
3992 {
3993 if (!(supported & JO_MODE))
3994 break;
3995 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3996 return FAIL;
3997 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003998 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003999 {
4000 if (!(supported & JO_IN_MODE))
4001 break;
4002 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4003 == FAIL)
4004 return FAIL;
4005 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004006 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004007 {
4008 if (!(supported & JO_OUT_MODE))
4009 break;
4010 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4011 == FAIL)
4012 return FAIL;
4013 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004014 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004015 {
4016 if (!(supported & JO_ERR_MODE))
4017 break;
4018 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4019 == FAIL)
4020 return FAIL;
4021 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004022 else if (STRCMP(hi->hi_key, "in_io") == 0
4023 || STRCMP(hi->hi_key, "out_io") == 0
4024 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004025 {
4026 if (!(supported & JO_OUT_IO))
4027 break;
4028 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4029 return FAIL;
4030 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004031 else if (STRCMP(hi->hi_key, "in_name") == 0
4032 || STRCMP(hi->hi_key, "out_name") == 0
4033 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004034 {
4035 part = part_from_char(*hi->hi_key);
4036
4037 if (!(supported & JO_OUT_IO))
4038 break;
4039 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4040 opt->jo_io_name[part] =
4041 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4042 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004043 else if (STRCMP(hi->hi_key, "in_buf") == 0
4044 || STRCMP(hi->hi_key, "out_buf") == 0
4045 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004046 {
4047 part = part_from_char(*hi->hi_key);
4048
4049 if (!(supported & JO_OUT_IO))
4050 break;
4051 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4052 opt->jo_io_buf[part] = get_tv_number(item);
4053 if (opt->jo_io_buf[part] <= 0)
4054 {
4055 EMSG2(_(e_invarg2), get_tv_string(item));
4056 return FAIL;
4057 }
4058 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4059 {
4060 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4061 return FAIL;
4062 }
4063 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004064 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4065 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4066 {
4067 part = part_from_char(*hi->hi_key);
4068
4069 if (!(supported & JO_OUT_IO))
4070 break;
4071 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4072 opt->jo_modifiable[part] = get_tv_number(item);
4073 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004074 else if (STRCMP(hi->hi_key, "in_top") == 0
4075 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004076 {
4077 linenr_T *lp;
4078
4079 if (!(supported & JO_OUT_IO))
4080 break;
4081 if (hi->hi_key[3] == 't')
4082 {
4083 lp = &opt->jo_in_top;
4084 opt->jo_set |= JO_IN_TOP;
4085 }
4086 else
4087 {
4088 lp = &opt->jo_in_bot;
4089 opt->jo_set |= JO_IN_BOT;
4090 }
4091 *lp = get_tv_number(item);
4092 if (*lp < 0)
4093 {
4094 EMSG2(_(e_invarg2), get_tv_string(item));
4095 return FAIL;
4096 }
4097 }
4098 else if (STRCMP(hi->hi_key, "channel") == 0)
4099 {
4100 if (!(supported & JO_OUT_IO))
4101 break;
4102 opt->jo_set |= JO_CHANNEL;
4103 if (item->v_type != VAR_CHANNEL)
4104 {
4105 EMSG2(_(e_invarg2), "channel");
4106 return FAIL;
4107 }
4108 opt->jo_channel = item->vval.v_channel;
4109 }
4110 else if (STRCMP(hi->hi_key, "callback") == 0)
4111 {
4112 if (!(supported & JO_CALLBACK))
4113 break;
4114 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004115 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004116 if (opt->jo_callback == NULL)
4117 {
4118 EMSG2(_(e_invarg2), "callback");
4119 return FAIL;
4120 }
4121 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004122 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004123 {
4124 if (!(supported & JO_OUT_CALLBACK))
4125 break;
4126 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004127 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004128 if (opt->jo_out_cb == NULL)
4129 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004130 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004131 return FAIL;
4132 }
4133 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004134 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004135 {
4136 if (!(supported & JO_ERR_CALLBACK))
4137 break;
4138 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004139 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004140 if (opt->jo_err_cb == NULL)
4141 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004142 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004143 return FAIL;
4144 }
4145 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004146 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004147 {
4148 if (!(supported & JO_CLOSE_CALLBACK))
4149 break;
4150 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004151 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004152 if (opt->jo_close_cb == NULL)
4153 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004154 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004155 return FAIL;
4156 }
4157 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004158 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4159 {
4160 if (!(supported & JO_EXIT_CB))
4161 break;
4162 opt->jo_set |= JO_EXIT_CB;
4163 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4164 if (opt->jo_exit_cb == NULL)
4165 {
4166 EMSG2(_(e_invarg2), "exit_cb");
4167 return FAIL;
4168 }
4169 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004170 else if (STRCMP(hi->hi_key, "waittime") == 0)
4171 {
4172 if (!(supported & JO_WAITTIME))
4173 break;
4174 opt->jo_set |= JO_WAITTIME;
4175 opt->jo_waittime = get_tv_number(item);
4176 }
4177 else if (STRCMP(hi->hi_key, "timeout") == 0)
4178 {
4179 if (!(supported & JO_TIMEOUT))
4180 break;
4181 opt->jo_set |= JO_TIMEOUT;
4182 opt->jo_timeout = get_tv_number(item);
4183 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004184 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004185 {
4186 if (!(supported & JO_OUT_TIMEOUT))
4187 break;
4188 opt->jo_set |= JO_OUT_TIMEOUT;
4189 opt->jo_out_timeout = get_tv_number(item);
4190 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004191 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004192 {
4193 if (!(supported & JO_ERR_TIMEOUT))
4194 break;
4195 opt->jo_set |= JO_ERR_TIMEOUT;
4196 opt->jo_err_timeout = get_tv_number(item);
4197 }
4198 else if (STRCMP(hi->hi_key, "part") == 0)
4199 {
4200 if (!(supported & JO_PART))
4201 break;
4202 opt->jo_set |= JO_PART;
4203 val = get_tv_string(item);
4204 if (STRCMP(val, "err") == 0)
4205 opt->jo_part = PART_ERR;
4206 else
4207 {
4208 EMSG2(_(e_invarg2), val);
4209 return FAIL;
4210 }
4211 }
4212 else if (STRCMP(hi->hi_key, "id") == 0)
4213 {
4214 if (!(supported & JO_ID))
4215 break;
4216 opt->jo_set |= JO_ID;
4217 opt->jo_id = get_tv_number(item);
4218 }
4219 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4220 {
4221 if (!(supported & JO_STOPONEXIT))
4222 break;
4223 opt->jo_set |= JO_STOPONEXIT;
4224 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4225 opt->jo_soe_buf);
4226 if (opt->jo_stoponexit == NULL)
4227 {
4228 EMSG2(_(e_invarg2), "stoponexit");
4229 return FAIL;
4230 }
4231 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004232 else if (STRCMP(hi->hi_key, "block_write") == 0)
4233 {
4234 if (!(supported & JO_BLOCK_WRITE))
4235 break;
4236 opt->jo_set |= JO_BLOCK_WRITE;
4237 opt->jo_block_write = get_tv_number(item);
4238 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004239 else
4240 break;
4241 --todo;
4242 }
4243 if (todo > 0)
4244 {
4245 EMSG2(_(e_invarg2), hi->hi_key);
4246 return FAIL;
4247 }
4248
4249 return OK;
4250}
4251
4252/*
4253 * Get the channel from the argument.
4254 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004255 * When "check_open" is TRUE check that the channel can be used.
4256 * When "reading" is TRUE "check_open" considers typeahead useful.
4257 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004258 */
4259 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004260get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004261{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004262 channel_T *channel = NULL;
4263 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004264
4265 if (tv->v_type == VAR_JOB)
4266 {
4267 if (tv->vval.v_job != NULL)
4268 channel = tv->vval.v_job->jv_channel;
4269 }
4270 else if (tv->v_type == VAR_CHANNEL)
4271 {
4272 channel = tv->vval.v_channel;
4273 }
4274 else
4275 {
4276 EMSG2(_(e_invarg2), get_tv_string(tv));
4277 return NULL;
4278 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004279 if (channel != NULL && reading)
4280 has_readahead = channel_has_readahead(channel,
4281 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004282
Bram Moolenaar437905c2016-04-26 19:01:05 +02004283 if (check_open && (channel == NULL || (!channel_is_open(channel)
4284 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004285 {
4286 EMSG(_("E906: not an open channel"));
4287 return NULL;
4288 }
4289 return channel;
4290}
4291
4292static job_T *first_job = NULL;
4293
4294 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004295job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004296{
4297 ch_log(job->jv_channel, "Freeing job");
4298 if (job->jv_channel != NULL)
4299 {
4300 /* The link from the channel to the job doesn't count as a reference,
4301 * thus don't decrement the refcount of the job. The reference from
4302 * the job to the channel does count the refrence, decrement it and
4303 * NULL the reference. We don't set ch_job_killed, unreferencing the
4304 * job doesn't mean it stops running. */
4305 job->jv_channel->ch_job = NULL;
4306 channel_unref(job->jv_channel);
4307 }
4308 mch_clear_job(job);
4309
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004310 vim_free(job->jv_stoponexit);
4311 vim_free(job->jv_exit_cb);
4312 partial_unref(job->jv_exit_partial);
4313}
4314
4315 static void
4316job_free_job(job_T *job)
4317{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004318 if (job->jv_next != NULL)
4319 job->jv_next->jv_prev = job->jv_prev;
4320 if (job->jv_prev == NULL)
4321 first_job = job->jv_next;
4322 else
4323 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004324 vim_free(job);
4325}
4326
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004327 static void
4328job_free(job_T *job)
4329{
4330 if (!in_free_unref_items)
4331 {
4332 job_free_contents(job);
4333 job_free_job(job);
4334 }
4335}
4336
Bram Moolenaar655da312016-05-28 22:22:34 +02004337#if defined(EXITFREE) || defined(PROTO)
4338 void
4339job_free_all(void)
4340{
4341 while (first_job != NULL)
4342 job_free(first_job);
4343}
4344#endif
4345
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004346/*
4347 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004348 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4349 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004350 */
4351 static int
4352job_still_useful(job_T *job)
4353{
4354 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004355 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4356 || (job->jv_channel != NULL
4357 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004358}
4359
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004360/*
4361 * Mark references in jobs that are still useful.
4362 */
4363 int
4364set_ref_in_job(int copyID)
4365{
4366 int abort = FALSE;
4367 job_T *job;
4368 typval_T tv;
4369
4370 for (job = first_job; job != NULL; job = job->jv_next)
4371 if (job_still_useful(job))
4372 {
4373 tv.v_type = VAR_JOB;
4374 tv.vval.v_job = job;
4375 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4376 }
4377 return abort;
4378}
4379
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004380 void
4381job_unref(job_T *job)
4382{
4383 if (job != NULL && --job->jv_refcount <= 0)
4384 {
4385 /* Do not free the job when it has not ended yet and there is a
4386 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004387 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004388 {
4389 job_free(job);
4390 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004391 else if (job->jv_channel != NULL
4392 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004393 {
4394 /* Do remove the link to the channel, otherwise it hangs
4395 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004396 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004397 job->jv_channel->ch_job = NULL;
4398 channel_unref(job->jv_channel);
4399 job->jv_channel = NULL;
4400 }
4401 }
4402}
4403
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004404 int
4405free_unused_jobs_contents(int copyID, int mask)
4406{
4407 int did_free = FALSE;
4408 job_T *job;
4409
4410 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004411 if ((job->jv_copyID & mask) != (copyID & mask)
4412 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004413 {
4414 /* Free the channel and ordinary items it contains, but don't
4415 * recurse into Lists, Dictionaries etc. */
4416 job_free_contents(job);
4417 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004418 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004419 return did_free;
4420}
4421
4422 void
4423free_unused_jobs(int copyID, int mask)
4424{
4425 job_T *job;
4426 job_T *job_next;
4427
4428 for (job = first_job; job != NULL; job = job_next)
4429 {
4430 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004431 if ((job->jv_copyID & mask) != (copyID & mask)
4432 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004433 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004434 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004435 job_free_job(job);
4436 }
4437 }
4438}
4439
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004440/*
4441 * Allocate a job. Sets the refcount to one and sets options default.
4442 */
4443 static job_T *
4444job_alloc(void)
4445{
4446 job_T *job;
4447
4448 job = (job_T *)alloc_clear(sizeof(job_T));
4449 if (job != NULL)
4450 {
4451 job->jv_refcount = 1;
4452 job->jv_stoponexit = vim_strsave((char_u *)"term");
4453
4454 if (first_job != NULL)
4455 {
4456 first_job->jv_prev = job;
4457 job->jv_next = first_job;
4458 }
4459 first_job = job;
4460 }
4461 return job;
4462}
4463
4464 void
4465job_set_options(job_T *job, jobopt_T *opt)
4466{
4467 if (opt->jo_set & JO_STOPONEXIT)
4468 {
4469 vim_free(job->jv_stoponexit);
4470 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4471 job->jv_stoponexit = NULL;
4472 else
4473 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4474 }
4475 if (opt->jo_set & JO_EXIT_CB)
4476 {
4477 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004478 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004479 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004480 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004481 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004482 job->jv_exit_partial = NULL;
4483 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004484 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004485 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004486 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004487 job->jv_exit_partial = opt->jo_exit_partial;
4488 if (job->jv_exit_partial != NULL)
4489 ++job->jv_exit_partial->pt_refcount;
4490 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004491 }
4492}
4493
4494/*
4495 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4496 */
4497 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004498job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004499{
4500 job_T *job;
4501
4502 for (job = first_job; job != NULL; job = job->jv_next)
4503 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4504 mch_stop_job(job, job->jv_stoponexit);
4505}
4506
4507/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004508 * Return TRUE when there is any job that might exit, which means
4509 * job_check_ended() should be called once in a while.
4510 */
4511 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004512has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004513{
4514 job_T *job;
4515
4516 for (job = first_job; job != NULL; job = job->jv_next)
4517 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4518 return TRUE;
4519 return FALSE;
4520}
4521
4522/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004523 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004524 */
4525 void
4526job_check_ended(void)
4527{
4528 static time_t last_check = 0;
4529 time_t now;
4530 job_T *job;
4531 job_T *next;
4532
4533 /* Only do this once in 10 seconds. */
4534 now = time(NULL);
4535 if (last_check + 10 < now)
4536 {
4537 last_check = now;
4538 for (job = first_job; job != NULL; job = next)
4539 {
4540 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004541 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004542 job_status(job); /* may free "job" */
4543 }
4544 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004545 if (channel_need_redraw)
4546 {
4547 channel_need_redraw = FALSE;
4548 redraw_after_callback();
4549 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004550}
4551
4552/*
4553 * "job_start()" function
4554 */
4555 job_T *
4556job_start(typval_T *argvars)
4557{
4558 job_T *job;
4559 char_u *cmd = NULL;
4560#if defined(UNIX)
4561# define USE_ARGV
4562 char **argv = NULL;
4563 int argc = 0;
4564#else
4565 garray_T ga;
4566#endif
4567 jobopt_T opt;
4568 int part;
4569
4570 job = job_alloc();
4571 if (job == NULL)
4572 return NULL;
4573
4574 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004575#ifndef USE_ARGV
4576 ga_init2(&ga, (int)sizeof(char*), 20);
4577#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004578
4579 /* Default mode is NL. */
4580 clear_job_options(&opt);
4581 opt.jo_mode = MODE_NL;
4582 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004583 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4584 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004585 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004586
4587 /* Check that when io is "file" that there is a file name. */
4588 for (part = PART_OUT; part <= PART_IN; ++part)
4589 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4590 && opt.jo_io[part] == JIO_FILE
4591 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4592 || *opt.jo_io_name[part] == NUL))
4593 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004594 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004595 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004596 }
4597
4598 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4599 {
4600 buf_T *buf = NULL;
4601
4602 /* check that we can find the buffer before starting the job */
4603 if (opt.jo_set & JO_IN_BUF)
4604 {
4605 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4606 if (buf == NULL)
4607 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4608 }
4609 else if (!(opt.jo_set & JO_IN_NAME))
4610 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004611 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004612 }
4613 else
4614 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4615 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004616 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004617 if (buf->b_ml.ml_mfp == NULL)
4618 {
4619 char_u numbuf[NUMBUFLEN];
4620 char_u *s;
4621
4622 if (opt.jo_set & JO_IN_BUF)
4623 {
4624 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4625 s = numbuf;
4626 }
4627 else
4628 s = opt.jo_io_name[PART_IN];
4629 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004630 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004631 }
4632 job->jv_in_buf = buf;
4633 }
4634
4635 job_set_options(job, &opt);
4636
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004637 if (argvars[0].v_type == VAR_STRING)
4638 {
4639 /* Command is a string. */
4640 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004641 if (cmd == NULL || *cmd == NUL)
4642 {
4643 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004644 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004645 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004646#ifdef USE_ARGV
4647 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004648 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004649 argv[argc] = NULL;
4650#endif
4651 }
4652 else if (argvars[0].v_type != VAR_LIST
4653 || argvars[0].vval.v_list == NULL
4654 || argvars[0].vval.v_list->lv_len < 1)
4655 {
4656 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004657 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004658 }
4659 else
4660 {
4661 list_T *l = argvars[0].vval.v_list;
4662 listitem_T *li;
4663 char_u *s;
4664
4665#ifdef USE_ARGV
4666 /* Pass argv[] to mch_call_shell(). */
4667 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4668 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004669 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004670#endif
4671 for (li = l->lv_first; li != NULL; li = li->li_next)
4672 {
4673 s = get_tv_string_chk(&li->li_tv);
4674 if (s == NULL)
4675 goto theend;
4676#ifdef USE_ARGV
4677 argv[argc++] = (char *)s;
4678#else
4679 /* Only escape when needed, double quotes are not always allowed. */
4680 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4681 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004682# ifdef WIN32
4683 int old_ssl = p_ssl;
4684
4685 /* This is using CreateProcess, not cmd.exe. Always use
4686 * double quote and backslashes. */
4687 p_ssl = 0;
4688# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004689 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004690# ifdef WIN32
4691 p_ssl = old_ssl;
4692# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004693 if (s == NULL)
4694 goto theend;
4695 ga_concat(&ga, s);
4696 vim_free(s);
4697 }
4698 else
4699 ga_concat(&ga, s);
4700 if (li->li_next != NULL)
4701 ga_append(&ga, ' ');
4702#endif
4703 }
4704#ifdef USE_ARGV
4705 argv[argc] = NULL;
4706#else
4707 cmd = ga.ga_data;
4708#endif
4709 }
4710
4711#ifdef USE_ARGV
4712 if (ch_log_active())
4713 {
4714 garray_T ga;
4715 int i;
4716
4717 ga_init2(&ga, (int)sizeof(char), 200);
4718 for (i = 0; i < argc; ++i)
4719 {
4720 if (i > 0)
4721 ga_concat(&ga, (char_u *)" ");
4722 ga_concat(&ga, (char_u *)argv[i]);
4723 }
4724 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4725 ga_clear(&ga);
4726 }
4727 mch_start_job(argv, job, &opt);
4728#else
4729 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4730 mch_start_job((char *)cmd, job, &opt);
4731#endif
4732
4733 /* If the channel is reading from a buffer, write lines now. */
4734 if (job->jv_channel != NULL)
4735 channel_write_in(job->jv_channel);
4736
4737theend:
4738#ifdef USE_ARGV
4739 vim_free(argv);
4740#else
4741 vim_free(ga.ga_data);
4742#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004743 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004744 return job;
4745}
4746
4747/*
4748 * Get the status of "job" and invoke the exit callback when needed.
4749 * The returned string is not allocated.
4750 */
4751 char *
4752job_status(job_T *job)
4753{
4754 char *result;
4755
4756 if (job->jv_status == JOB_ENDED)
4757 /* No need to check, dead is dead. */
4758 result = "dead";
4759 else if (job->jv_status == JOB_FAILED)
4760 result = "fail";
4761 else
4762 {
4763 result = mch_job_status(job);
4764 if (job->jv_status == JOB_ENDED)
4765 ch_log(job->jv_channel, "Job ended");
4766 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4767 {
4768 typval_T argv[3];
4769 typval_T rettv;
4770 int dummy;
4771
4772 /* invoke the exit callback; make sure the refcount is > 0 */
4773 ++job->jv_refcount;
4774 argv[0].v_type = VAR_JOB;
4775 argv[0].vval.v_job = job;
4776 argv[1].v_type = VAR_NUMBER;
4777 argv[1].vval.v_number = job->jv_exitval;
4778 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004779 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4780 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004781 clear_tv(&rettv);
4782 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004783 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004784 }
4785 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4786 {
4787 /* The job was already unreferenced, now that it ended it can be
4788 * freed. Careful: caller must not use "job" after this! */
4789 job_free(job);
4790 }
4791 }
4792 return result;
4793}
4794
Bram Moolenaar8950a562016-03-12 15:22:55 +01004795/*
4796 * Implementation of job_info().
4797 */
4798 void
4799job_info(job_T *job, dict_T *dict)
4800{
4801 dictitem_T *item;
4802 varnumber_T nr;
4803
4804 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4805
4806 item = dictitem_alloc((char_u *)"channel");
4807 if (item == NULL)
4808 return;
4809 item->di_tv.v_lock = 0;
4810 item->di_tv.v_type = VAR_CHANNEL;
4811 item->di_tv.vval.v_channel = job->jv_channel;
4812 if (job->jv_channel != NULL)
4813 ++job->jv_channel->ch_refcount;
4814 if (dict_add(dict, item) == FAIL)
4815 dictitem_free(item);
4816
4817#ifdef UNIX
4818 nr = job->jv_pid;
4819#else
4820 nr = job->jv_proc_info.dwProcessId;
4821#endif
4822 dict_add_nr_str(dict, "process", nr, NULL);
4823
4824 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004825 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004826 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4827}
4828
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004829 int
4830job_stop(job_T *job, typval_T *argvars)
4831{
4832 char_u *arg;
4833
4834 if (argvars[1].v_type == VAR_UNKNOWN)
4835 arg = (char_u *)"";
4836 else
4837 {
4838 arg = get_tv_string_chk(&argvars[1]);
4839 if (arg == NULL)
4840 {
4841 EMSG(_(e_invarg));
4842 return 0;
4843 }
4844 }
4845 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4846 if (mch_stop_job(job, arg) == FAIL)
4847 return 0;
4848
4849 /* Assume that "hup" does not kill the job. */
4850 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4851 job->jv_channel->ch_job_killed = TRUE;
4852
4853 /* We don't try freeing the job, obviously the caller still has a
4854 * reference to it. */
4855 return 1;
4856}
4857
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004858#endif /* FEAT_JOB_CHANNEL */