blob: 3b8b5afa05774d99eed17704cc06e259c4200307 [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)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200264 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100265 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200266 msgbuf = NULL;
267 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100268 FormatMessage(
269 FORMAT_MESSAGE_ALLOCATE_BUFFER |
270 FORMAT_MESSAGE_FROM_SYSTEM |
271 FORMAT_MESSAGE_IGNORE_INSERTS,
272 NULL,
273 eno,
274 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
275 (LPTSTR) &msgbuf,
276 0,
277 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200278 if (msgbuf != NULL)
279 /* chomp \r or \n */
280 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
281 switch (*ptr)
282 {
283 case '\r':
284 STRMOVE(ptr, ptr + 1);
285 ptr--;
286 break;
287 case '\n':
288 if (*(ptr + 1) == '\0')
289 *ptr = '\0';
290 else
291 *ptr = ' ';
292 break;
293 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100294 return msgbuf;
295}
296#endif
297
Bram Moolenaar77073442016-02-13 23:23:53 +0100298/*
299 * The list of all allocated channels.
300 */
301static channel_T *first_channel = NULL;
302static int next_ch_id = 0;
303
304/*
305 * Allocate a new channel. The refcount is set to 1.
306 * The channel isn't actually used until it is opened.
307 * Returns NULL if out of memory.
308 */
309 channel_T *
310add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100311{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100313 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100314
Bram Moolenaar77073442016-02-13 23:23:53 +0100315 if (channel == NULL)
316 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100317
Bram Moolenaar77073442016-02-13 23:23:53 +0100318 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100319 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100320
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100321 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100322 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100323 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100324#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100325 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100326#endif
327#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100328 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100329#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100330 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100331 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100332
Bram Moolenaar77073442016-02-13 23:23:53 +0100333 if (first_channel != NULL)
334 {
335 first_channel->ch_prev = channel;
336 channel->ch_next = first_channel;
337 }
338 first_channel = channel;
339
340 channel->ch_refcount = 1;
341 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100342}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100343
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100344/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100345 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100346 * Return TRUE if "channel" has a callback and the associated job wasn't
347 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100348 */
349 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100350channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100351{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100352 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100353 int has_out_msg;
354 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100355
356 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100357 if (channel->ch_job_killed && channel->ch_job == NULL)
358 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100359
360 /* If there is a close callback it may still need to be invoked. */
361 if (channel->ch_close_cb != NULL)
362 return TRUE;
363
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200364 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200365 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200366 return TRUE;
367
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100368 /* If there is no callback then nobody can get readahead. If the fd is
369 * closed and there is no readahead then the callback won't be called. */
370 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
371 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
372 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100373 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
374 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
375 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
376 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
377 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
378 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100379 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100380 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200381 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200382 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
383 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200384 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200385 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
386 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100387}
388
389/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200390 * Close a channel and free all its resources.
391 */
392 static void
393channel_free_contents(channel_T *channel)
394{
395 channel_close(channel, TRUE);
396 channel_clear(channel);
397 ch_log(channel, "Freeing channel");
398}
399
400 static void
401channel_free_channel(channel_T *channel)
402{
403 if (channel->ch_next != NULL)
404 channel->ch_next->ch_prev = channel->ch_prev;
405 if (channel->ch_prev == NULL)
406 first_channel = channel->ch_next;
407 else
408 channel->ch_prev->ch_next = channel->ch_next;
409 vim_free(channel);
410}
411
412 static void
413channel_free(channel_T *channel)
414{
415 if (!in_free_unref_items)
416 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200417 if (safe_to_invoke_callback == 0)
418 {
419 channel->ch_to_be_freed = TRUE;
420 }
421 else
422 {
423 channel_free_contents(channel);
424 channel_free_channel(channel);
425 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200426 }
427}
428
429/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100430 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100431 * possible, there is no callback to be invoked or the associated job was
432 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100433 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100435 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100436channel_may_free(channel_T *channel)
437{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100438 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100439 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100440 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100441 return TRUE;
442 }
443 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100444}
445
446/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100447 * Decrement the reference count on "channel" and maybe free it when it goes
448 * down to zero. Don't free it if there is a pending action.
449 * Returns TRUE when the channel is no longer referenced.
450 */
451 int
452channel_unref(channel_T *channel)
453{
454 if (channel != NULL && --channel->ch_refcount <= 0)
455 return channel_may_free(channel);
456 return FALSE;
457}
458
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200459 int
460free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100461{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200462 int did_free = FALSE;
463 channel_T *ch;
464
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200465 /* This is invoked from the garbage collector, which only runs at a safe
466 * point. */
467 ++safe_to_invoke_callback;
468
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200469 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200470 if (!channel_still_useful(ch)
471 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200472 {
473 /* Free the channel and ordinary items it contains, but don't
474 * recurse into Lists, Dictionaries etc. */
475 channel_free_contents(ch);
476 did_free = TRUE;
477 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200478
479 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200480 return did_free;
481}
482
483 void
484free_unused_channels(int copyID, int mask)
485{
486 channel_T *ch;
487 channel_T *ch_next;
488
489 for (ch = first_channel; ch != NULL; ch = ch_next)
490 {
491 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200492 if (!channel_still_useful(ch)
493 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200494 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200495 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200496 channel_free_channel(ch);
497 }
498 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100499}
500
Bram Moolenaard04a0202016-01-26 23:30:18 +0100501#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100502
503#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
504 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100505channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100506{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100507 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100508 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100509
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100510 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100511 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100512 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100513 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200514 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100515}
516#endif
517
Bram Moolenaare0874f82016-01-24 20:36:41 +0100518/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100519 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100520 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100521#ifdef FEAT_GUI_X11
522 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200523messageFromServer(XtPointer clientData,
524 int *unused1 UNUSED,
525 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100526{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100527 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100528}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100529#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100530
Bram Moolenaard04a0202016-01-26 23:30:18 +0100531#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100532# if GTK_CHECK_VERSION(3,0,0)
533 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200534messageFromServer(GIOChannel *unused1 UNUSED,
535 GIOCondition unused2 UNUSED,
536 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100537{
538 channel_read_fd(GPOINTER_TO_INT(clientData));
539 return TRUE; /* Return FALSE instead in case the event source is to
540 * be removed after this function returns. */
541}
542# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100543 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200544messageFromServer(gpointer clientData,
545 gint unused1 UNUSED,
546 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100547{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100548 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100549}
Bram Moolenaar98921892016-02-23 17:14:37 +0100550# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100551#endif
552
553 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100554channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100555{
Bram Moolenaarde279892016-03-11 22:19:44 +0100556 if (!CH_HAS_GUI)
557 return;
558
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100559# ifdef FEAT_GUI_X11
560 /* Tell notifier we are interested in being called
561 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100562 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
563 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100564 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100565 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100566 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200567 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100568 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100569# else
570# ifdef FEAT_GUI_GTK
571 /* Tell gdk we are interested in being called when there
572 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100573 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100574# if GTK_CHECK_VERSION(3,0,0)
575 {
576 GIOChannel *chnnl = g_io_channel_unix_new(
577 (gint)channel->ch_part[part].ch_fd);
578
579 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
580 chnnl,
581 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200582 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100583 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
584
585 g_io_channel_unref(chnnl);
586 }
587# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100588 channel->ch_part[part].ch_inputHandler = gdk_input_add(
589 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100590 (GdkInputCondition)
591 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200592 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100593 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100594# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100595# endif
596# endif
597}
598
Bram Moolenaarde279892016-03-11 22:19:44 +0100599 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100600channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100601{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100602 if (channel->CH_SOCK_FD != INVALID_FD)
603 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100604 if (channel->CH_OUT_FD != INVALID_FD)
605 channel_gui_register_one(channel, PART_OUT);
606 if (channel->CH_ERR_FD != INVALID_FD)
607 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100608}
609
610/*
611 * Register any of our file descriptors with the GUI event handling system.
612 * Called when the GUI has started.
613 */
614 void
615channel_gui_register_all(void)
616{
Bram Moolenaar77073442016-02-13 23:23:53 +0100617 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100618
Bram Moolenaar77073442016-02-13 23:23:53 +0100619 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100620 channel_gui_register(channel);
621}
622
623 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100624channel_gui_unregister_one(channel_T *channel, int part)
625{
626# ifdef FEAT_GUI_X11
627 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
628 {
629 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
630 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
631 }
632# else
633# ifdef FEAT_GUI_GTK
634 if (channel->ch_part[part].ch_inputHandler != 0)
635 {
636# if GTK_CHECK_VERSION(3,0,0)
637 g_source_remove(channel->ch_part[part].ch_inputHandler);
638# else
639 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
640# endif
641 channel->ch_part[part].ch_inputHandler = 0;
642 }
643# endif
644# endif
645}
646
647 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100648channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100649{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100650 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100651
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100652 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100653 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100654}
655
656#endif
657
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100658static char *e_cannot_connect = N_("E902: Cannot connect to port");
659
Bram Moolenaard04a0202016-01-26 23:30:18 +0100660/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100661 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100662 * "waittime" is the time in msec to wait for the connection.
663 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100664 * Returns the channel for success.
665 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100666 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100667 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100668channel_open(
669 char *hostname,
670 int port_in,
671 int waittime,
672 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100673{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100674 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100675 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100676 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100677#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100678 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100679 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100680#else
681 int port = port_in;
682#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100683 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100684 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100685
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100686#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100687 channel_init_winsock();
688#endif
689
Bram Moolenaar77073442016-02-13 23:23:53 +0100690 channel = add_channel();
691 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100692 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100693 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100694 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100695 }
696
697 /* Get the server internet address and put into addr structure */
698 /* fill in the socket address structure and connect to server */
699 vim_memset((char *)&server, 0, sizeof(server));
700 server.sin_family = AF_INET;
701 server.sin_port = htons(port);
702 if ((host = gethostbyname(hostname)) == NULL)
703 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100704 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100705 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100706 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100707 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100708 }
709 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
710
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100711 /* On Mac and Solaris a zero timeout almost never works. At least wait
712 * one millisecond. Let's do it for all systems, because we don't know why
713 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100714 if (waittime == 0)
715 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100716
717 /*
718 * For Unix we need to call connect() again after connect() failed.
719 * On Win32 one time is sufficient.
720 */
721 while (TRUE)
722 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100723 long elapsed_msec = 0;
724 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100725
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100726 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100727 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100728 sd = socket(AF_INET, SOCK_STREAM, 0);
729 if (sd == -1)
730 {
731 ch_error(channel, "in socket() in channel_open().");
732 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100733 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100734 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100735 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100736
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100737 if (waittime >= 0)
738 {
739 /* Make connect() non-blocking. */
740 if (
741#ifdef _WIN32
742 ioctlsocket(sd, FIONBIO, &val) < 0
743#else
744 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
745#endif
746 )
747 {
748 SOCK_ERRNO;
749 ch_errorn(channel,
750 "channel_open: Connect failed with errno %d", errno);
751 sock_close(sd);
752 channel_free(channel);
753 return NULL;
754 }
755 }
756
757 /* Try connecting to the server. */
758 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
759 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
760
Bram Moolenaar045a2842016-03-08 22:33:07 +0100761 if (ret == 0)
762 /* The connection could be established. */
763 break;
764
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100765 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100766 if (waittime < 0 || (errno != EWOULDBLOCK
767 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100768#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100769 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100770#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100771 ))
772 {
773 ch_errorn(channel,
774 "channel_open: Connect failed with errno %d", errno);
775 PERROR(_(e_cannot_connect));
776 sock_close(sd);
777 channel_free(channel);
778 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100779 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100780
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100781 /* Limit the waittime to 50 msec. If it doesn't work within this
782 * time we close the socket and try creating it again. */
783 waitnow = waittime > 50 ? 50 : waittime;
784
Bram Moolenaar045a2842016-03-08 22:33:07 +0100785 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100786 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100787#ifndef WIN32
788 if (errno != ECONNREFUSED)
789#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100790 {
791 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100792 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100793 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100794#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100795 int so_error = 0;
796 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100797 struct timeval start_tv;
798 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100799#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100800 FD_ZERO(&rfds);
801 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100802 FD_ZERO(&wfds);
803 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100804
Bram Moolenaar562ca712016-03-09 21:50:05 +0100805 tv.tv_sec = waitnow / 1000;
806 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100807#ifndef WIN32
808 gettimeofday(&start_tv, NULL);
809#endif
810 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100811 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100812 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100813
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100814 if (ret < 0)
815 {
816 SOCK_ERRNO;
817 ch_errorn(channel,
818 "channel_open: Connect failed with errno %d", errno);
819 PERROR(_(e_cannot_connect));
820 sock_close(sd);
821 channel_free(channel);
822 return NULL;
823 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100824
Bram Moolenaare081e212016-02-28 22:33:46 +0100825#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100826 /* On Win32: select() is expected to work and wait for up to
827 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100828 if (FD_ISSET(sd, &wfds))
829 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100830 elapsed_msec = waitnow;
831 if (waittime > 1 && elapsed_msec < waittime)
832 {
833 waittime -= elapsed_msec;
834 continue;
835 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100836#else
837 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100838 * After putting the socket in non-blocking mode, connect() will
839 * return EINPROGRESS, select() will not wait (as if writing is
840 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100841 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100842 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100843 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100844 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100845 {
846 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100847 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100848 if (ret < 0 || (so_error != 0
849 && so_error != EWOULDBLOCK
850 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100851# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100852 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100853# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100854 ))
855 {
856 ch_errorn(channel,
857 "channel_open: Connect failed with errno %d",
858 so_error);
859 PERROR(_(e_cannot_connect));
860 sock_close(sd);
861 channel_free(channel);
862 return NULL;
863 }
864 }
865
Bram Moolenaar045a2842016-03-08 22:33:07 +0100866 if (FD_ISSET(sd, &wfds) && so_error == 0)
867 /* Did not detect an error, connection is established. */
868 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100869
Bram Moolenaar045a2842016-03-08 22:33:07 +0100870 gettimeofday(&end_tv, NULL);
871 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
872 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100873#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100874 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100875
876#ifndef WIN32
877 if (waittime > 1 && elapsed_msec < waittime)
878 {
879 /* The port isn't ready but we also didn't get an error.
880 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100881 * yet. Select() may return early, wait until the remaining
882 * "waitnow" and try again. */
883 waitnow -= elapsed_msec;
884 waittime -= elapsed_msec;
885 if (waitnow > 0)
886 {
887 mch_delay((long)waitnow, TRUE);
888 ui_breakcheck();
889 waittime -= waitnow;
890 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100891 if (!got_int)
892 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100893 if (waittime <= 0)
894 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100895 waittime = 1;
896 continue;
897 }
898 /* we were interrupted, behave as if timed out */
899 }
900#endif
901
902 /* We timed out. */
903 ch_error(channel, "Connection timed out");
904 sock_close(sd);
905 channel_free(channel);
906 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100907 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100908
Bram Moolenaar045a2842016-03-08 22:33:07 +0100909 ch_log(channel, "Connection made");
910
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100911 if (waittime >= 0)
912 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100913#ifdef _WIN32
914 val = 0;
915 ioctlsocket(sd, FIONBIO, &val);
916#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100917 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100918#endif
919 }
920
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100921 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100922 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100923 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
924 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100925
926#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100927 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100928#endif
929
Bram Moolenaar77073442016-02-13 23:23:53 +0100930 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100931}
932
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100933/*
934 * Implements ch_open().
935 */
936 channel_T *
937channel_open_func(typval_T *argvars)
938{
939 char_u *address;
940 char_u *p;
941 char *rest;
942 int port;
943 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200944 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100945
946 address = get_tv_string(&argvars[0]);
947 if (argvars[1].v_type != VAR_UNKNOWN
948 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
949 {
950 EMSG(_(e_invarg));
951 return NULL;
952 }
953
954 /* parse address */
955 p = vim_strchr(address, ':');
956 if (p == NULL)
957 {
958 EMSG2(_(e_invarg2), address);
959 return NULL;
960 }
961 *p++ = NUL;
962 port = strtol((char *)p, &rest, 10);
963 if (*address == NUL || port <= 0 || *rest != NUL)
964 {
965 p[-1] = ':';
966 EMSG2(_(e_invarg2), address);
967 return NULL;
968 }
969
970 /* parse options */
971 clear_job_options(&opt);
972 opt.jo_mode = MODE_JSON;
973 opt.jo_timeout = 2000;
974 if (get_job_options(&argvars[1], &opt,
975 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200976 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100977 if (opt.jo_timeout < 0)
978 {
979 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200980 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100981 }
982
983 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
984 if (channel != NULL)
985 {
986 opt.jo_set = JO_ALL;
987 channel_set_options(channel, &opt);
988 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200989theend:
990 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100991 return channel;
992}
993
Bram Moolenaarde279892016-03-11 22:19:44 +0100994 static void
995may_close_part(sock_T *fd)
996{
997 if (*fd != INVALID_FD)
998 {
999 fd_close(*fd);
1000 *fd = INVALID_FD;
1001 }
1002}
1003
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001004 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001005channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001006{
Bram Moolenaarde279892016-03-11 22:19:44 +01001007 if (in != INVALID_FD)
1008 {
1009 may_close_part(&channel->CH_IN_FD);
1010 channel->CH_IN_FD = in;
1011 }
1012 if (out != INVALID_FD)
1013 {
1014# if defined(FEAT_GUI)
1015 channel_gui_unregister_one(channel, PART_OUT);
1016# endif
1017 may_close_part(&channel->CH_OUT_FD);
1018 channel->CH_OUT_FD = out;
1019# if defined(FEAT_GUI)
1020 channel_gui_register_one(channel, PART_OUT);
1021# endif
1022 }
1023 if (err != INVALID_FD)
1024 {
1025# if defined(FEAT_GUI)
1026 channel_gui_unregister_one(channel, PART_ERR);
1027# endif
1028 may_close_part(&channel->CH_ERR_FD);
1029 channel->CH_ERR_FD = err;
1030# if defined(FEAT_GUI)
1031 channel_gui_register_one(channel, PART_ERR);
1032# endif
1033 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001034}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001035
Bram Moolenaard6051b52016-02-28 15:49:03 +01001036/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001037 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001038 * This does not keep a refcount, when the job is freed ch_job is cleared.
1039 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001040 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001041channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001042{
Bram Moolenaar77073442016-02-13 23:23:53 +01001043 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001044
1045 channel_set_options(channel, options);
1046
1047 if (job->jv_in_buf != NULL)
1048 {
1049 chanpart_T *in_part = &channel->ch_part[PART_IN];
1050
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001051 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001052 ch_logs(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001053 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001054 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001055 {
1056 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1057 {
1058 /* Special mode: send last-but-one line when appending a line
1059 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001060 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001061 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001062 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001063 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001064 }
1065 else
1066 in_part->ch_buf_top = options->jo_in_top;
1067 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001068 else
1069 in_part->ch_buf_top = 1;
1070 if (options->jo_set & JO_IN_BOT)
1071 in_part->ch_buf_bot = options->jo_in_bot;
1072 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001073 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001074 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001075}
1076
1077/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001078 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001079 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001080 */
1081 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001082find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001083{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001084 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001085 buf_T *save_curbuf = curbuf;
1086
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001087 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001088 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001089 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001090 if (buf == NULL)
1091 buf = buflist_findname_exp(name);
1092 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001093 if (buf == NULL)
1094 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001095 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001096 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001097 if (buf == NULL)
1098 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001099 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001100 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001101#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001102 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1103 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001104#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001105 if (curbuf->b_ml.ml_mfp == NULL)
1106 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001107 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1108 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001109 changed_bytes(1, 0);
1110 curbuf = save_curbuf;
1111 }
1112
1113 return buf;
1114}
1115
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001116 static void
1117set_callback(
1118 char_u **cbp,
1119 partial_T **pp,
1120 char_u *callback,
1121 partial_T *partial)
1122{
1123 free_callback(*cbp, *pp);
1124 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001125 {
1126 if (partial != NULL)
1127 *cbp = partial->pt_name;
1128 else
1129 *cbp = vim_strsave(callback);
1130 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001131 else
1132 *cbp = NULL;
1133 *pp = partial;
1134 if (*pp != NULL)
1135 ++(*pp)->pt_refcount;
1136}
1137
Bram Moolenaar187db502016-02-27 14:44:26 +01001138/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001139 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001140 */
1141 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001142channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001143{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001144 int part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001145
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001146 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001147 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001148 channel->ch_part[part].ch_mode = opt->jo_mode;
1149 if (opt->jo_set & JO_IN_MODE)
1150 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1151 if (opt->jo_set & JO_OUT_MODE)
1152 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1153 if (opt->jo_set & JO_ERR_MODE)
1154 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001155
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001156 if (opt->jo_set & JO_TIMEOUT)
1157 for (part = PART_SOCK; part <= PART_IN; ++part)
1158 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1159 if (opt->jo_set & JO_OUT_TIMEOUT)
1160 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1161 if (opt->jo_set & JO_ERR_TIMEOUT)
1162 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001163 if (opt->jo_set & JO_BLOCK_WRITE)
1164 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001165
1166 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001167 set_callback(&channel->ch_callback, &channel->ch_partial,
1168 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001169 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001170 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1171 &channel->ch_part[PART_OUT].ch_partial,
1172 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001173 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001174 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1175 &channel->ch_part[PART_ERR].ch_partial,
1176 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001177 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001178 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1179 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar187db502016-02-27 14:44:26 +01001180
1181 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1182 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001183 buf_T *buf;
1184
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001185 /* writing output to a buffer. Default mode is NL. */
1186 if (!(opt->jo_set & JO_OUT_MODE))
1187 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001188 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001189 {
1190 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1191 if (buf == NULL)
1192 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1193 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001194 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001195 {
1196 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1197 }
1198 if (buf != NULL)
1199 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001200 if (opt->jo_set & JO_OUT_MODIFIABLE)
1201 channel->ch_part[PART_OUT].ch_nomodifiable =
1202 !opt->jo_modifiable[PART_OUT];
1203
1204 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1205 {
1206 EMSG(_(e_modifiable));
1207 }
1208 else
1209 {
1210 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001211 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001212 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001213 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001214 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001215 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001216
1217 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1218 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1219 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1220 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001221 buf_T *buf;
1222
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001223 /* writing err to a buffer. Default mode is NL. */
1224 if (!(opt->jo_set & JO_ERR_MODE))
1225 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1226 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001227 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001228 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001229 {
1230 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1231 if (buf == NULL)
1232 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1233 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001234 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001235 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1236 if (buf != NULL)
1237 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001238 if (opt->jo_set & JO_ERR_MODIFIABLE)
1239 channel->ch_part[PART_ERR].ch_nomodifiable =
1240 !opt->jo_modifiable[PART_ERR];
1241 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1242 {
1243 EMSG(_(e_modifiable));
1244 }
1245 else
1246 {
1247 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001248 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001249 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001250 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001251 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001252 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001253
1254 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1255 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1256 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001257}
1258
1259/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001260 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001261 */
1262 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001263channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001264 channel_T *channel,
1265 int part,
1266 char_u *callback,
1267 partial_T *partial,
1268 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001269{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001270 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001271 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1272
1273 if (item != NULL)
1274 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001275 item->cq_partial = partial;
1276 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001277 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001278 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001279 item->cq_callback = callback;
1280 }
1281 else
1282 item->cq_callback = vim_strsave(callback);
Bram Moolenaar77073442016-02-13 23:23:53 +01001283 item->cq_seq_nr = id;
1284 item->cq_prev = head->cq_prev;
1285 head->cq_prev = item;
1286 item->cq_next = NULL;
1287 if (item->cq_prev == NULL)
1288 head->cq_next = item;
1289 else
1290 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001291 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001292}
1293
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001294 static void
1295write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1296{
1297 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001298 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001299 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001300 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001301
Bram Moolenaar655da312016-05-28 22:22:34 +02001302 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001303 if ((p = alloc(len + 2)) == NULL)
1304 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001305 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001306
1307 for (i = 0; i < len; ++i)
1308 if (p[i] == NL)
1309 p[i] = NUL;
1310
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001311 p[len] = NL;
1312 p[len + 1] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001313 channel_send(channel, PART_IN, p, len + 1, "write_buf_line()");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001314 vim_free(p);
1315}
1316
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001317/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001318 * Return TRUE if "channel" can be written to.
1319 * Returns FALSE if the input is closed or the write would block.
1320 */
1321 static int
1322can_write_buf_line(channel_T *channel)
1323{
1324 chanpart_T *in_part = &channel->ch_part[PART_IN];
1325
1326 if (in_part->ch_fd == INVALID_FD)
1327 return FALSE; /* pipe was closed */
1328
1329 /* for testing: block every other attempt to write */
1330 if (in_part->ch_block_write == 1)
1331 in_part->ch_block_write = -1;
1332 else if (in_part->ch_block_write == -1)
1333 in_part->ch_block_write = 1;
1334
1335 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1336#ifndef WIN32
1337 {
1338# if defined(HAVE_SELECT)
1339 struct timeval tval;
1340 fd_set wfds;
1341 int ret;
1342
1343 FD_ZERO(&wfds);
1344 FD_SET((int)in_part->ch_fd, &wfds);
1345 tval.tv_sec = 0;
1346 tval.tv_usec = 0;
1347 for (;;)
1348 {
1349 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1350# ifdef EINTR
1351 SOCK_ERRNO;
1352 if (ret == -1 && errno == EINTR)
1353 continue;
1354# endif
1355 if (ret <= 0 || in_part->ch_block_write == 1)
1356 {
1357 if (ret > 0)
1358 ch_log(channel, "FAKED Input not ready for writing");
1359 else
1360 ch_log(channel, "Input not ready for writing");
1361 return FALSE;
1362 }
1363 break;
1364 }
1365# else
1366 struct pollfd fds;
1367
1368 fds.fd = in_part->ch_fd;
1369 fds.events = POLLOUT;
1370 if (poll(&fds, 1, 0) <= 0)
1371 {
1372 ch_log(channel, "Input not ready for writing");
1373 return FALSE;
1374 }
1375 if (in_part->ch_block_write == 1)
1376 {
1377 ch_log(channel, "FAKED Input not ready for writing");
1378 return FALSE;
1379 }
1380# endif
1381 }
1382#endif
1383 return TRUE;
1384}
1385
1386/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001387 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001388 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001389 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001390channel_write_in(channel_T *channel)
1391{
1392 chanpart_T *in_part = &channel->ch_part[PART_IN];
1393 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001394 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001395 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001396
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001397 if (buf == NULL || in_part->ch_buf_append)
1398 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001399 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001400 {
1401 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001402 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001403 return;
1404 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001405
1406 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1407 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1408 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001409 if (!can_write_buf_line(channel))
1410 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001411 write_buf_line(buf, lnum, channel);
1412 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001413 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001414
1415 if (written == 1)
1416 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1417 else if (written > 1)
1418 ch_logn(channel, "written %d lines to channel", written);
1419
Bram Moolenaar014069a2016-03-03 22:51:40 +01001420 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001421 if (lnum > buf->b_ml.ml_line_count)
1422 {
1423 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001424 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001425 ch_log(channel, "Finished writing all lines to channel");
1426 }
1427 else
1428 ch_logn(channel, "Still %d more lines to write",
1429 buf->b_ml.ml_line_count - lnum + 1);
1430}
1431
1432/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001433 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001434 */
1435 void
1436channel_buffer_free(buf_T *buf)
1437{
1438 channel_T *channel;
1439 int part;
1440
1441 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1442 for (part = PART_SOCK; part <= PART_IN; ++part)
1443 {
1444 chanpart_T *ch_part = &channel->ch_part[part];
1445
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001446 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001447 {
1448 ch_logs(channel, "%s buffer has been wiped out",
1449 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001450 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001451 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001452 }
1453}
1454
1455/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001456 * Write any lines waiting to be written to a channel.
1457 */
1458 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001459channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001460{
1461 channel_T *channel;
1462
1463 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1464 {
1465 chanpart_T *in_part = &channel->ch_part[PART_IN];
1466
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001467 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001468 {
1469 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001470 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001471 else
1472 channel_write_in(channel);
1473 }
1474 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001475}
1476
1477/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001478 * Write appended lines above the last one in "buf" to the channel.
1479 */
1480 void
1481channel_write_new_lines(buf_T *buf)
1482{
1483 channel_T *channel;
1484 int found_one = FALSE;
1485
1486 /* There could be more than one channel for the buffer, loop over all of
1487 * them. */
1488 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1489 {
1490 chanpart_T *in_part = &channel->ch_part[PART_IN];
1491 linenr_T lnum;
1492 int written = 0;
1493
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001494 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001495 {
1496 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001497 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001498 found_one = TRUE;
1499 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1500 ++lnum)
1501 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001502 if (!can_write_buf_line(channel))
1503 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504 write_buf_line(buf, lnum, channel);
1505 ++written;
1506 }
1507
1508 if (written == 1)
1509 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1510 else if (written > 1)
1511 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001512 if (lnum < buf->b_ml.ml_line_count)
1513 ch_logn(channel, "Still %d more lines to write",
1514 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001515
1516 in_part->ch_buf_bot = lnum;
1517 }
1518 }
1519 if (!found_one)
1520 buf->b_write_to_channel = FALSE;
1521}
1522
1523/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001524 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001525 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001526 */
1527 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001528invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1529 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001530{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001531 typval_T rettv;
1532 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001533
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001534 if (safe_to_invoke_callback == 0)
1535 EMSG("INTERNAL: Invoking callback when it is not safe");
1536
Bram Moolenaar77073442016-02-13 23:23:53 +01001537 argv[0].v_type = VAR_CHANNEL;
1538 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001539
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001540 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1541 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001542 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001543 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001544}
1545
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001546/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001547 * Return the first node from "channel"/"part" without removing it.
1548 * Returns NULL if there is nothing.
1549 */
1550 readq_T *
1551channel_peek(channel_T *channel, int part)
1552{
1553 readq_T *head = &channel->ch_part[part].ch_head;
1554
1555 return head->rq_next;
1556}
1557
1558/*
1559 * Return a pointer to the first NL in "node".
1560 * Skips over NUL characters.
1561 * Returns NULL if there is no NL.
1562 */
1563 char_u *
1564channel_first_nl(readq_T *node)
1565{
1566 char_u *buffer = node->rq_buffer;
1567 long_u i;
1568
1569 for (i = 0; i < node->rq_buflen; ++i)
1570 if (buffer[i] == NL)
1571 return buffer + i;
1572 return NULL;
1573}
1574
1575/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001576 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001577 * The caller must free it.
1578 * Returns NULL if there is nothing.
1579 */
1580 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001581channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001582{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001583 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001584 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001585 char_u *p;
1586
Bram Moolenaar77073442016-02-13 23:23:53 +01001587 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001588 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001589 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001590 p = node->rq_buffer;
1591 head->rq_next = node->rq_next;
1592 if (node->rq_next == NULL)
1593 head->rq_prev = NULL;
1594 else
1595 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001596 vim_free(node);
1597 return p;
1598}
1599
1600/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001601 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001602 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001603 */
1604 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001605channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001606{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001607 readq_T *head = &channel->ch_part[part].ch_head;
1608 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001609 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001610 char_u *res;
1611 char_u *p;
1612
1613 /* If there is only one buffer just get that one. */
1614 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1615 return channel_get(channel, part);
1616
1617 /* Concatenate everything into one buffer. */
1618 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001619 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001620 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001621 if (res == NULL)
1622 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001623 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001624 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001625 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001626 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001627 p += node->rq_buflen;
1628 }
1629 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001630
1631 /* Free all buffers */
1632 do
1633 {
1634 p = channel_get(channel, part);
1635 vim_free(p);
1636 } while (p != NULL);
1637
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001638 /* turn all NUL into NL */
1639 while (len > 0)
1640 {
1641 --len;
1642 if (res[len] == NUL)
1643 res[len] = NL;
1644 }
1645
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001646 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001647}
1648
1649/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001650 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001651 * Caller must check these bytes are available.
1652 */
1653 void
1654channel_consume(channel_T *channel, int part, int len)
1655{
1656 readq_T *head = &channel->ch_part[part].ch_head;
1657 readq_T *node = head->rq_next;
1658 char_u *buf = node->rq_buffer;
1659
1660 mch_memmove(buf, buf + len, node->rq_buflen - len);
1661 node->rq_buflen -= len;
1662}
1663
1664/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001665 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001666 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001667 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001668 */
1669 int
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001670channel_collapse(channel_T *channel, int part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001671{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001672 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001673 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001674 readq_T *last_node;
1675 readq_T *n;
1676 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001677 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001678 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001679
Bram Moolenaar77073442016-02-13 23:23:53 +01001680 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001681 return FAIL;
1682
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001683 last_node = node->rq_next;
1684 len = node->rq_buflen + last_node->rq_buflen + 1;
1685 if (want_nl)
1686 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001687 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001688 {
1689 last_node = last_node->rq_next;
1690 len += last_node->rq_buflen;
1691 }
1692
1693 p = newbuf = alloc(len);
1694 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001695 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001696 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001697 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001698 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001699 node->rq_buffer = newbuf;
1700 for (n = node; n != last_node; )
1701 {
1702 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001703 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001704 p += n->rq_buflen;
1705 vim_free(n->rq_buffer);
1706 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001707 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001708
1709 /* dispose of the collapsed nodes and their buffers */
1710 for (n = node->rq_next; n != last_node; )
1711 {
1712 n = n->rq_next;
1713 vim_free(n->rq_prev);
1714 }
1715 node->rq_next = last_node->rq_next;
1716 if (last_node->rq_next == NULL)
1717 head->rq_prev = node;
1718 else
1719 last_node->rq_next->rq_prev = node;
1720 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001721 return OK;
1722}
1723
1724/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001725 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001726 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001727 * Returns OK or FAIL.
1728 */
1729 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001730channel_save(channel_T *channel, int part, char_u *buf, int len,
1731 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001732{
1733 readq_T *node;
1734 readq_T *head = &channel->ch_part[part].ch_head;
1735 char_u *p;
1736 int i;
1737
1738 node = (readq_T *)alloc(sizeof(readq_T));
1739 if (node == NULL)
1740 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001741 /* A NUL is added at the end, because netbeans code expects that.
1742 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001743 node->rq_buffer = alloc(len + 1);
1744 if (node->rq_buffer == NULL)
1745 {
1746 vim_free(node);
1747 return FAIL; /* out of memory */
1748 }
1749
1750 if (channel->ch_part[part].ch_mode == MODE_NL)
1751 {
1752 /* Drop any CR before a NL. */
1753 p = node->rq_buffer;
1754 for (i = 0; i < len; ++i)
1755 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1756 *p++ = buf[i];
1757 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001758 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001759 }
1760 else
1761 {
1762 mch_memmove(node->rq_buffer, buf, len);
1763 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001764 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001765 }
1766
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001767 if (prepend)
1768 {
1769 /* preend node to the head of the queue */
1770 node->rq_next = head->rq_next;
1771 node->rq_prev = NULL;
1772 if (head->rq_next == NULL)
1773 head->rq_prev = node;
1774 else
1775 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001776 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001777 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001778 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001779 {
1780 /* append node to the tail of the queue */
1781 node->rq_next = NULL;
1782 node->rq_prev = head->rq_prev;
1783 if (head->rq_prev == NULL)
1784 head->rq_next = node;
1785 else
1786 head->rq_prev->rq_next = node;
1787 head->rq_prev = node;
1788 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001789
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001790 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001791 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001792 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001793 fprintf(log_fd, "'");
1794 if (fwrite(buf, len, 1, log_fd) != 1)
1795 return FAIL;
1796 fprintf(log_fd, "'\n");
1797 }
1798 return OK;
1799}
1800
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001801 static int
1802channel_fill(js_read_T *reader)
1803{
1804 channel_T *channel = (channel_T *)reader->js_cookie;
1805 int part = reader->js_cookie_arg;
1806 char_u *next = channel_get(channel, part);
1807 int unused;
1808 int len;
1809 char_u *p;
1810
1811 if (next == NULL)
1812 return FALSE;
1813
1814 unused = reader->js_end - reader->js_buf - reader->js_used;
1815 if (unused > 0)
1816 {
1817 /* Prepend unused text. */
1818 len = (int)STRLEN(next);
1819 p = alloc(unused + len + 1);
1820 if (p == NULL)
1821 {
1822 vim_free(next);
1823 return FALSE;
1824 }
1825 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1826 mch_memmove(p + unused, next, len + 1);
1827 vim_free(next);
1828 next = p;
1829 }
1830
1831 vim_free(reader->js_buf);
1832 reader->js_buf = next;
1833 reader->js_used = 0;
1834 return TRUE;
1835}
1836
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001837/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001838 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001839 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001840 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001841 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001842 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001844{
1845 js_read_T reader;
1846 typval_T listtv;
1847 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001848 chanpart_T *chanpart = &channel->ch_part[part];
1849 jsonq_T *head = &chanpart->ch_json_head;
1850 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001851 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001852
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001853 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001854 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001855
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001856 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001857 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001858 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001859 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001860 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001861
1862 /* When a message is incomplete we wait for a short while for more to
1863 * arrive. After the delay drop the input, otherwise a truncated string
1864 * or list will make us hang. */
1865 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001866 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001867 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001868 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001869 /* Only accept the response when it is a list with at least two
1870 * items. */
1871 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001872 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001873 if (listtv.v_type != VAR_LIST)
1874 ch_error(channel, "Did not receive a list, discarding");
1875 else
1876 ch_errorn(channel, "Expected list with two items, got %d",
1877 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001878 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001879 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001880 else
1881 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001882 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1883 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001884 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001885 else
1886 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001887 item->jq_value = alloc_tv();
1888 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001889 {
1890 vim_free(item);
1891 clear_tv(&listtv);
1892 }
1893 else
1894 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001895 *item->jq_value = listtv;
1896 item->jq_prev = head->jq_prev;
1897 head->jq_prev = item;
1898 item->jq_next = NULL;
1899 if (item->jq_prev == NULL)
1900 head->jq_next = item;
1901 else
1902 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001903 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001904 }
1905 }
1906 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001907
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001908 if (status == OK)
1909 chanpart->ch_waiting = FALSE;
1910 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001911 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001912 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001913 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001914 /* First time encountering incomplete message, set a deadline of
1915 * 100 msec. */
1916 ch_log(channel, "Incomplete message - wait for more");
1917 reader.js_used = 0;
1918 chanpart->ch_waiting = TRUE;
1919#ifdef WIN32
1920 chanpart->ch_deadline = GetTickCount() + 100L;
1921#else
1922 gettimeofday(&chanpart->ch_deadline, NULL);
1923 chanpart->ch_deadline.tv_usec += 100 * 1000;
1924 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1925 {
1926 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1927 ++chanpart->ch_deadline.tv_sec;
1928 }
1929#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001930 }
1931 else
1932 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001933 int timeout;
1934#ifdef WIN32
1935 timeout = GetTickCount() > chanpart->ch_deadline;
1936#else
1937 {
1938 struct timeval now_tv;
1939
1940 gettimeofday(&now_tv, NULL);
1941 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1942 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1943 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1944 }
1945#endif
1946 if (timeout)
1947 {
1948 status = FAIL;
1949 chanpart->ch_waiting = FALSE;
1950 }
1951 else
1952 {
1953 reader.js_used = 0;
1954 ch_log(channel, "still waiting on incomplete message");
1955 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001956 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001957 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001958
1959 if (status == FAIL)
1960 {
1961 ch_error(channel, "Decoding failed - discarding input");
1962 ret = FALSE;
1963 chanpart->ch_waiting = FALSE;
1964 }
1965 else if (reader.js_buf[reader.js_used] != NUL)
1966 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001967 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001968 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001969 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1970 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001971 ret = status == MAYBE ? FALSE: TRUE;
1972 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001973 else
1974 ret = FALSE;
1975
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001976 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001977 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001978}
1979
1980/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001981 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001982 */
1983 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001984remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001985{
Bram Moolenaar77073442016-02-13 23:23:53 +01001986 if (node->cq_prev == NULL)
1987 head->cq_next = node->cq_next;
1988 else
1989 node->cq_prev->cq_next = node->cq_next;
1990 if (node->cq_next == NULL)
1991 head->cq_prev = node->cq_prev;
1992 else
1993 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001994}
1995
1996/*
1997 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001998 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001999 */
2000 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002001remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002002{
Bram Moolenaar77073442016-02-13 23:23:53 +01002003 if (node->jq_prev == NULL)
2004 head->jq_next = node->jq_next;
2005 else
2006 node->jq_prev->jq_next = node->jq_next;
2007 if (node->jq_next == NULL)
2008 head->jq_prev = node->jq_prev;
2009 else
2010 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002011 vim_free(node);
2012}
2013
2014/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002015 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002016 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002017 * When "id" is zero or negative jut get the first message. But not the one
2018 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002019 * Return OK when found and return the value in "rettv".
2020 * Return FAIL otherwise.
2021 */
2022 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002023channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002024{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002025 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002026 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002027
Bram Moolenaar77073442016-02-13 23:23:53 +01002028 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002029 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002030 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002031 typval_T *tv = &l->lv_first->li_tv;
2032
2033 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002034 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002035 || tv->vval.v_number == 0
2036 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002037 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002038 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002039 if (tv->v_type == VAR_NUMBER)
2040 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002041 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002042 return OK;
2043 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002044 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002045 }
2046 return FAIL;
2047}
2048
Bram Moolenaarece61b02016-02-20 21:39:05 +01002049#define CH_JSON_MAX_ARGS 4
2050
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002051/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002052 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002053 * "argv[0]" is the command string.
2054 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002055 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002056 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002057channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002058{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002059 char_u *cmd = argv[0].vval.v_string;
2060 char_u *arg;
2061 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002062
Bram Moolenaarece61b02016-02-20 21:39:05 +01002063 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002064 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002065 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002066 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002067 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002068 return;
2069 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002070 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002071 if (arg == NULL)
2072 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002073
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002074 if (STRCMP(cmd, "ex") == 0)
2075 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002076 int save_called_emsg = called_emsg;
2077
2078 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002079 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002080 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002081 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002082 --emsg_silent;
2083 if (called_emsg)
2084 ch_logs(channel, "Ex command error: '%s'",
2085 (char *)get_vim_var_str(VV_ERRMSG));
2086 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002087 }
2088 else if (STRCMP(cmd, "normal") == 0)
2089 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002090 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002091
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002092 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002093 ea.arg = arg;
2094 ea.addr_count = 0;
2095 ea.forceit = TRUE; /* no mapping */
2096 ex_normal(&ea);
2097 }
2098 else if (STRCMP(cmd, "redraw") == 0)
2099 {
2100 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002101
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002102 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002103 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002104 ex_redraw(&ea);
2105 showruler(FALSE);
2106 setcursor();
2107 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002108#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002109 if (gui.in_use)
2110 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002111 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002112 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002113 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002114#endif
2115 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002116 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002117 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002118 int is_call = cmd[0] == 'c';
2119 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002120
Bram Moolenaarece61b02016-02-20 21:39:05 +01002121 if (argv[id_idx].v_type != VAR_UNKNOWN
2122 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002123 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002124 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002125 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002126 EMSG("E904: last argument for expr/call must be a number");
2127 }
2128 else if (is_call && argv[2].v_type != VAR_LIST)
2129 {
2130 ch_error(channel, "third argument for call must be a list");
2131 if (p_verbose > 2)
2132 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002133 }
2134 else
2135 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002136 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002137 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002138 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002139 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002140
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002141 /* Don't pollute the display with errors. */
2142 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002143 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002144 {
2145 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002146 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002147 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002148 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002149 {
2150 ch_logs(channel, "Calling '%s'", (char *)arg);
2151 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2152 tv = &res_tv;
2153 else
2154 tv = NULL;
2155 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002156
2157 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002158 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002159 int id = argv[id_idx].vval.v_number;
2160
Bram Moolenaar55fab432016-02-07 16:53:13 +01002161 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002162 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002163 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002164 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002165 /* If evaluation failed or the result can't be encoded
2166 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002167 vim_free(json);
2168 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002169 err_tv.v_type = VAR_STRING;
2170 err_tv.vval.v_string = (char_u *)"ERROR";
2171 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002172 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002173 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002174 if (json != NULL)
2175 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002176 channel_send(channel,
2177 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002178 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002179 vim_free(json);
2180 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002181 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002182 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002183 if (tv == &res_tv)
2184 clear_tv(tv);
2185 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002186 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002187 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002188 }
2189 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002190 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002191 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002192 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002193 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002194}
2195
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002196/*
2197 * Invoke the callback at "cbhead".
2198 * Does not redraw but sets channel_need_redraw.
2199 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002200 static void
2201invoke_one_time_callback(
2202 channel_T *channel,
2203 cbq_T *cbhead,
2204 cbq_T *item,
2205 typval_T *argv)
2206{
2207 ch_logs(channel, "Invoking one-time callback %s",
2208 (char *)item->cq_callback);
2209 /* Remove the item from the list first, if the callback
2210 * invokes ch_close() the list will be cleared. */
2211 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002212 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002213 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002214 vim_free(item);
2215}
2216
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002217 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002218append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002219{
2220 buf_T *save_curbuf = curbuf;
2221 linenr_T lnum = buffer->b_ml.ml_line_count;
2222 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002223 chanpart_T *ch_part = &channel->ch_part[part];
2224 int save_p_ma = buffer->b_p_ma;
2225
2226 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2227 {
2228 if (!ch_part->ch_nomod_error)
2229 {
2230 ch_error(channel, "Buffer is not modifiable, cannot append");
2231 ch_part->ch_nomod_error = TRUE;
2232 }
2233 return;
2234 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002235
2236 /* If the buffer is also used as input insert above the last
2237 * line. Don't write these lines. */
2238 if (save_write_to)
2239 {
2240 --lnum;
2241 buffer->b_write_to_channel = FALSE;
2242 }
2243
2244 /* Append to the buffer */
2245 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2246
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002247 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002248 curbuf = buffer;
2249 u_sync(TRUE);
2250 /* ignore undo failure, undo is not very useful here */
2251 ignored = u_save(lnum, lnum + 1);
2252
2253 ml_append(lnum, msg, 0, FALSE);
2254 appended_lines_mark(lnum, 1L);
2255 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002256 if (ch_part->ch_nomodifiable)
2257 buffer->b_p_ma = FALSE;
2258 else
2259 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002260
2261 if (buffer->b_nwindows > 0)
2262 {
2263 win_T *wp;
2264 win_T *save_curwin;
2265
2266 FOR_ALL_WINDOWS(wp)
2267 {
2268 if (wp->w_buffer == buffer
2269 && (save_write_to
2270 ? wp->w_cursor.lnum == lnum + 1
2271 : (wp->w_cursor.lnum == lnum
2272 && wp->w_cursor.col == 0)))
2273 {
2274 ++wp->w_cursor.lnum;
2275 save_curwin = curwin;
2276 curwin = wp;
2277 curbuf = curwin->w_buffer;
2278 scroll_cursor_bot(0, FALSE);
2279 curwin = save_curwin;
2280 curbuf = curwin->w_buffer;
2281 }
2282 }
2283 redraw_buf_later(buffer, VALID);
2284 channel_need_redraw = TRUE;
2285 }
2286
2287 if (save_write_to)
2288 {
2289 channel_T *ch;
2290
2291 /* Find channels reading from this buffer and adjust their
2292 * next-to-read line number. */
2293 buffer->b_write_to_channel = TRUE;
2294 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2295 {
2296 chanpart_T *in_part = &ch->ch_part[PART_IN];
2297
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002298 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002299 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2300 }
2301 }
2302}
2303
Bram Moolenaar437905c2016-04-26 19:01:05 +02002304 static void
2305drop_messages(channel_T *channel, int part)
2306{
2307 char_u *msg;
2308
2309 while ((msg = channel_get(channel, part)) != NULL)
2310 {
2311 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2312 vim_free(msg);
2313 }
2314}
2315
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002316/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002317 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002318 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002319 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002320 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002321 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002322may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002323{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002324 char_u *msg = NULL;
2325 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002326 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002327 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002328 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002329 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002330 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002331 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002332 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002333 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002334 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002335
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002336 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002337 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002338 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002339
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002340 /* Use a message-specific callback, part callback or channel callback */
2341 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2342 if (cbitem->cq_seq_nr == 0)
2343 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002344 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002345 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002346 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002347 partial = cbitem->cq_partial;
2348 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002349 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002350 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002351 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002352 partial = channel->ch_part[part].ch_partial;
2353 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002354 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002355 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002356 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002357 partial = channel->ch_partial;
2358 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002359
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002360 buffer = channel->ch_part[part].ch_bufref.br_buf;
2361 if (buffer != NULL && !bufref_valid(&channel->ch_part[part].ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002362 {
2363 /* buffer was wiped out */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002364 channel->ch_part[part].ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002365 buffer = NULL;
2366 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002367
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002368 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002369 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002370 listitem_T *item;
2371 int argc = 0;
2372
Bram Moolenaard7ece102016-02-02 23:23:02 +01002373 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002374 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002375 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002376 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002377 channel_parse_json(channel, part);
2378 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002379 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002380 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002381
Bram Moolenaarece61b02016-02-20 21:39:05 +01002382 for (item = listtv->vval.v_list->lv_first;
2383 item != NULL && argc < CH_JSON_MAX_ARGS;
2384 item = item->li_next)
2385 argv[argc++] = item->li_tv;
2386 while (argc < CH_JSON_MAX_ARGS)
2387 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002388
Bram Moolenaarece61b02016-02-20 21:39:05 +01002389 if (argv[0].v_type == VAR_STRING)
2390 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002391 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002392 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002393 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002394 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002395 }
2396
Bram Moolenaarece61b02016-02-20 21:39:05 +01002397 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002398 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002399 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002400 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002401 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002402 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002403 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002404 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002405 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002406 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002407 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002408 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002409 return FALSE;
2410 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002411 else
2412 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002413 /* If there is no callback or buffer drop the message. */
2414 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002415 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002416 /* If there is a close callback it may use ch_read() to get the
2417 * messages. */
2418 if (channel->ch_close_cb == NULL)
2419 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002420 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002421 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002422
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002423 if (ch_mode == MODE_NL)
2424 {
2425 char_u *nl;
2426 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002427 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002428
2429 /* See if we have a message ending in NL in the first buffer. If
2430 * not try to concatenate the first and the second buffer. */
2431 while (TRUE)
2432 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002433 node = channel_peek(channel, part);
2434 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002435 if (nl != NULL)
2436 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002437 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002438 return FALSE; /* incomplete message */
2439 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002440 buf = node->rq_buffer;
2441
2442 /* Convert NUL to NL, the internal representation. */
2443 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2444 if (*p == NUL)
2445 *p = NL;
2446
2447 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002448 {
2449 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002450 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002451 *nl = NUL;
2452 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002453 else
2454 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002455 /* Copy the message into allocated memory (excluding the NL)
2456 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002457 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002458 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002459 }
2460 }
2461 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002462 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002463 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002464 * get everything we have.
2465 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002466 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002467 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002468
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002469 if (msg == NULL)
2470 return FALSE; /* out of memory (and avoids Coverity warning) */
2471
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002472 argv[1].v_type = VAR_STRING;
2473 argv[1].vval.v_string = msg;
2474 }
2475
Bram Moolenaara07fec92016-02-05 21:04:08 +01002476 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002477 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002478 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002479
2480 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002481 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002482 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002483 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002484 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002485 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002486 break;
2487 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002488 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002489 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002490 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002491 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002492 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002493 if (buffer != NULL)
2494 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002495 if (msg == NULL)
2496 /* JSON or JS mode: re-encode the message. */
2497 msg = json_encode(listtv, ch_mode);
2498 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002499 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002500 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002501
Bram Moolenaar187db502016-02-27 14:44:26 +01002502 if (callback != NULL)
2503 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002504 if (cbitem != NULL)
2505 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2506 else
2507 {
2508 /* invoke the channel callback */
2509 ch_logs(channel, "Invoking channel callback %s",
2510 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002511 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002512 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002513 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002514 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002515 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002516 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002517
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002518 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002519 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002520 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002521
2522 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002523}
2524
2525/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002526 * Return TRUE when channel "channel" is open for writing to.
2527 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002528 */
2529 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002530channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002531{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002532 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002533 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002534}
2535
2536/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002537 * Return TRUE when channel "channel" is open for reading or writing.
2538 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002539 */
2540 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002541channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002542{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002543 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002544 || channel->CH_IN_FD != INVALID_FD
2545 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002546 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002547}
2548
2549/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002550 * Return TRUE if "channel" has JSON or other typeahead.
2551 */
2552 static int
2553channel_has_readahead(channel_T *channel, int part)
2554{
2555 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2556
2557 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2558 {
2559 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2560 jsonq_T *item = head->jq_next;
2561
2562 return item != NULL;
2563 }
2564 return channel_peek(channel, part) != NULL;
2565}
2566
2567/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002568 * Return a string indicating the status of the channel.
2569 */
2570 char *
2571channel_status(channel_T *channel)
2572{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002573 int part;
2574 int has_readahead = FALSE;
2575
Bram Moolenaar77073442016-02-13 23:23:53 +01002576 if (channel == NULL)
2577 return "fail";
2578 if (channel_is_open(channel))
2579 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002580 for (part = PART_SOCK; part <= PART_ERR; ++part)
2581 if (channel_has_readahead(channel, part))
2582 {
2583 has_readahead = TRUE;
2584 break;
2585 }
2586
2587 if (has_readahead)
2588 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002589 return "closed";
2590}
2591
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002592 static void
2593channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2594{
2595 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002596 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002597 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002598 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002599
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002600 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002601 STRCAT(namebuf, "_");
2602 tail = STRLEN(namebuf);
2603
2604 STRCPY(namebuf + tail, "status");
2605 dict_add_nr_str(dict, namebuf, 0,
2606 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2607
2608 STRCPY(namebuf + tail, "mode");
2609 switch (chanpart->ch_mode)
2610 {
2611 case MODE_NL: s = "NL"; break;
2612 case MODE_RAW: s = "RAW"; break;
2613 case MODE_JSON: s = "JSON"; break;
2614 case MODE_JS: s = "JS"; break;
2615 }
2616 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2617
2618 STRCPY(namebuf + tail, "io");
2619 if (part == PART_SOCK)
2620 s = "socket";
2621 else switch (chanpart->ch_io)
2622 {
2623 case JIO_NULL: s = "null"; break;
2624 case JIO_PIPE: s = "pipe"; break;
2625 case JIO_FILE: s = "file"; break;
2626 case JIO_BUFFER: s = "buffer"; break;
2627 case JIO_OUT: s = "out"; break;
2628 }
2629 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2630
2631 STRCPY(namebuf + tail, "timeout");
2632 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2633}
2634
2635 void
2636channel_info(channel_T *channel, dict_T *dict)
2637{
2638 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2639 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2640
2641 if (channel->ch_hostname != NULL)
2642 {
2643 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2644 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2645 channel_part_info(channel, dict, "sock", PART_SOCK);
2646 }
2647 else
2648 {
2649 channel_part_info(channel, dict, "out", PART_OUT);
2650 channel_part_info(channel, dict, "err", PART_ERR);
2651 channel_part_info(channel, dict, "in", PART_IN);
2652 }
2653}
2654
Bram Moolenaar77073442016-02-13 23:23:53 +01002655/*
2656 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002657 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002658 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002659 */
2660 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002661channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002662{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002663 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002664
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002665#ifdef FEAT_GUI
2666 channel_gui_unregister(channel);
2667#endif
2668
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002669 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002670 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002671 sock_close(channel->CH_SOCK_FD);
2672 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002673 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002674 may_close_part(&channel->CH_IN_FD);
2675 may_close_part(&channel->CH_OUT_FD);
2676 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002677
Bram Moolenaar8b374212016-02-24 20:43:06 +01002678 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002679 {
2680 typval_T argv[1];
2681 typval_T rettv;
2682 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002683 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002684
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002685 /* Invoke callbacks before the close callback, since it's weird to
2686 * first invoke the close callback. Increment the refcount to avoid
2687 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002688 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002689 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002690 for (part = PART_SOCK; part <= PART_ERR; ++part)
2691 while (may_invoke_callback(channel, part))
2692 ;
2693
2694 /* Invoke the close callback, if still set. */
2695 if (channel->ch_close_cb != NULL)
2696 {
2697 ch_logs(channel, "Invoking close callback %s",
2698 (char *)channel->ch_close_cb);
2699 argv[0].v_type = VAR_CHANNEL;
2700 argv[0].vval.v_channel = channel;
2701 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002702 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002703 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002704 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002705 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002706 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002707
2708 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002709 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002710 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002711 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002712
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002713 --channel->ch_refcount;
2714
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002715 if (channel_need_redraw)
2716 {
2717 channel_need_redraw = FALSE;
2718 redraw_after_callback();
2719 }
2720
Bram Moolenaar437905c2016-04-26 19:01:05 +02002721 /* any remaining messages are useless now */
2722 for (part = PART_SOCK; part <= PART_ERR; ++part)
2723 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002724 }
2725
2726 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002727}
2728
Bram Moolenaard04a0202016-01-26 23:30:18 +01002729/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002730 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002731 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002732 static void
2733channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002734{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002735 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2736 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002737
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002738 while (channel_peek(channel, part) != NULL)
2739 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002740
2741 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002742 {
2743 cbq_T *node = cb_head->cq_next;
2744
2745 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002746 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002747 vim_free(node);
2748 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002749
2750 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002751 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002752 free_tv(json_head->jq_next->jq_value);
2753 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002754 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002755
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002756 free_callback(channel->ch_part[part].ch_callback,
2757 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002758 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002759 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002760}
2761
2762/*
2763 * Clear all the read buffers on "channel".
2764 */
2765 void
2766channel_clear(channel_T *channel)
2767{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002768 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002769 vim_free(channel->ch_hostname);
2770 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002771 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002772 channel_clear_one(channel, PART_OUT);
2773 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002774 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002775 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002776 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002777 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002778 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002779 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002780 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002781}
2782
Bram Moolenaar77073442016-02-13 23:23:53 +01002783#if defined(EXITFREE) || defined(PROTO)
2784 void
2785channel_free_all(void)
2786{
2787 channel_T *channel;
2788
Bram Moolenaard6051b52016-02-28 15:49:03 +01002789 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002790 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2791 channel_clear(channel);
2792}
2793#endif
2794
2795
Bram Moolenaar715d2852016-04-30 17:06:31 +02002796/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002797#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002798
2799/* Buffer size for reading incoming messages. */
2800#define MAXMSGSIZE 4096
2801
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002802#if defined(HAVE_SELECT)
2803/*
2804 * Add write fds where we are waiting for writing to be possible.
2805 */
2806 static int
2807channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2808{
2809 int maxfd = maxfd_arg;
2810 channel_T *ch;
2811
2812 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2813 {
2814 chanpart_T *in_part = &ch->ch_part[PART_IN];
2815
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002816 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002817 {
2818 FD_SET((int)in_part->ch_fd, wfds);
2819 if ((int)in_part->ch_fd >= maxfd)
2820 maxfd = (int)in_part->ch_fd + 1;
2821 }
2822 }
2823 return maxfd;
2824}
2825#else
2826/*
2827 * Add write fds where we are waiting for writing to be possible.
2828 */
2829 static int
2830channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2831{
2832 int nfd = nfd_in;
2833 channel_T *ch;
2834
2835 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2836 {
2837 chanpart_T *in_part = &ch->ch_part[PART_IN];
2838
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002839 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002840 {
2841 in_part->ch_poll_idx = nfd;
2842 fds[nfd].fd = in_part->ch_fd;
2843 fds[nfd].events = POLLOUT;
2844 ++nfd;
2845 }
2846 else
2847 in_part->ch_poll_idx = -1;
2848 }
2849 return nfd;
2850}
2851#endif
2852
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002853typedef enum {
2854 CW_READY,
2855 CW_NOT_READY,
2856 CW_ERROR
2857} channel_wait_result;
2858
Bram Moolenaard04a0202016-01-26 23:30:18 +01002859/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002860 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002861 * Return CW_READY when there is something to read.
2862 * Return CW_NOT_READY when there is nothing to read.
2863 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002864 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002865 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002866channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002867{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002868 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002869 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002870
Bram Moolenaard8070362016-02-15 21:56:54 +01002871# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002872 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002873 {
2874 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002875 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002876 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002877 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002878
2879 /* reading from a pipe, not a socket */
2880 while (TRUE)
2881 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002882 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2883
2884 if (r && nread > 0)
2885 return CW_READY;
2886 if (r == 0)
2887 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002888
2889 /* perhaps write some buffer lines */
2890 channel_write_any_lines();
2891
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002892 sleep_time = deadline - GetTickCount();
2893 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002894 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002895 /* Wait for a little while. Very short at first, up to 10 msec
2896 * after looping a few times. */
2897 if (sleep_time > delay)
2898 sleep_time = delay;
2899 Sleep(sleep_time);
2900 delay = delay * 2;
2901 if (delay > 10)
2902 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002903 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002904 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002905 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002906#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002907 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002908#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002909 struct timeval tval;
2910 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002911 fd_set wfds;
2912 int ret;
2913 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002914
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002915 tval.tv_sec = timeout / 1000;
2916 tval.tv_usec = (timeout % 1000) * 1000;
2917 for (;;)
2918 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002919 FD_ZERO(&rfds);
2920 FD_SET((int)fd, &rfds);
2921
2922 /* Write lines to a pipe when a pipe can be written to. Need to
2923 * set this every time, some buffers may be done. */
2924 maxfd = (int)fd + 1;
2925 FD_ZERO(&wfds);
2926 maxfd = channel_fill_wfds(maxfd, &wfds);
2927
2928 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002929# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002930 SOCK_ERRNO;
2931 if (ret == -1 && errno == EINTR)
2932 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002933# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002934 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002935 {
2936 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002937 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002938 channel_write_any_lines();
2939 continue;
2940 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002941 break;
2942 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002943#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002944 for (;;)
2945 {
2946 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2947 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002948
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002949 fds[0].fd = fd;
2950 fds[0].events = POLLIN;
2951 nfd = channel_fill_poll_write(nfd, fds);
2952 if (poll(fds, nfd, timeout) > 0)
2953 {
2954 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002955 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002956 channel_write_any_lines();
2957 continue;
2958 }
2959 break;
2960 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002961#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002962 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002963 return CW_NOT_READY;
2964}
2965
2966 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002967channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002968{
2969 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002970 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2971 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002972
2973 /* Queue a "DETACH" netbeans message in the command queue in order to
2974 * terminate the netbeans session later. Do not end the session here
2975 * directly as we may be running in the context of a call to
2976 * netbeans_parse_messages():
2977 * netbeans_parse_messages
2978 * -> autocmd triggered while processing the netbeans cmd
2979 * -> ui_breakcheck
2980 * -> gui event loop or select loop
2981 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002982 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002983 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002984 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002985 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002986 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2987
2988 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002989 * died. Don't close the channel right away, it may be the wrong moment
2990 * to invoke callbacks. */
2991 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02002992
2993#ifdef FEAT_GUI
2994 /* Stop listening to GUI events right away. */
2995 channel_gui_unregister(channel);
2996#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002997}
2998
2999 static void
3000channel_close_now(channel_T *channel)
3001{
3002 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003003 channel_close(channel, TRUE);
3004 if (channel->ch_nb_close_cb != NULL)
3005 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003006}
3007
3008/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003009 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003010 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003011 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003012 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003013 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003014channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003015{
3016 static char_u *buf = NULL;
3017 int len = 0;
3018 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003019 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003020 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003021
Bram Moolenaar5850a762016-05-27 19:59:48 +02003022 /* If we detected a read error don't try reading again. */
3023 if (channel->ch_to_be_closed)
3024 return;
3025
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003026 fd = channel->ch_part[part].ch_fd;
3027 if (fd == INVALID_FD)
3028 {
3029 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003030 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003031 }
3032 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003033
3034 /* Allocate a buffer to read into. */
3035 if (buf == NULL)
3036 {
3037 buf = alloc(MAXMSGSIZE);
3038 if (buf == NULL)
3039 return; /* out of memory! */
3040 }
3041
3042 /* Keep on reading for as long as there is something to read.
3043 * Use select() or poll() to avoid blocking on a message that is exactly
3044 * MAXMSGSIZE long. */
3045 for (;;)
3046 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003047 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003048 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003049 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003050 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003051 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003052 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003053 if (len <= 0)
3054 break; /* error or nothing more to read */
3055
3056 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003057 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003058 readlen += len;
3059 if (len < MAXMSGSIZE)
3060 break; /* did read everything that's available */
3061 }
3062
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003063 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003064 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003065 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003066
3067#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003068 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003069 if (CH_HAS_GUI && gtk_main_level() > 0)
3070 gtk_main_quit();
3071#endif
3072}
3073
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003074/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003075 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003076 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003077 * Returns what was read in allocated memory.
3078 * Returns NULL in case of error or timeout.
3079 */
3080 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003081channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003082{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003083 char_u *buf;
3084 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003085 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003086 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003087 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003088 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003089
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003090 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003091 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003092
3093 while (TRUE)
3094 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003095 node = channel_peek(channel, part);
3096 if (node != NULL)
3097 {
3098 if (mode == MODE_RAW || (mode == MODE_NL
3099 && channel_first_nl(node) != NULL))
3100 /* got a complete message */
3101 break;
3102 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3103 continue;
3104 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003105
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003106 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003107 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003108 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003109 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003110 {
3111 ch_log(channel, "Timed out");
3112 return NULL;
3113 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003114 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003115 }
3116
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003117 if (mode == MODE_RAW)
3118 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003119 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003120 }
3121 else
3122 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003123 char_u *p;
3124
3125 buf = node->rq_buffer;
3126 nl = channel_first_nl(node);
3127
3128 /* Convert NUL to NL, the internal representation. */
3129 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3130 if (*p == NUL)
3131 *p = NL;
3132
3133 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003134 {
3135 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003136 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003137 *nl = NUL;
3138 }
3139 else
3140 {
3141 /* Copy the message into allocated memory and remove it from the
3142 * buffer. */
3143 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003144 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003145 }
3146 }
3147 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003148 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003149 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003150}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003151
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003152/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003153 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003154 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003155 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003156 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003157 */
3158 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003159channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003160 channel_T *channel,
3161 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003162 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003163 int id,
3164 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003165{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003166 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003167 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003168 int timeout;
3169 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003170
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003171 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003172 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003173 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003174 for (;;)
3175 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003176 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003177
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003178 /* search for message "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003179 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003180 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003181 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003182 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003183 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003184
Bram Moolenaard7ece102016-02-02 23:23:02 +01003185 if (!more)
3186 {
3187 /* Handle any other messages in the queue. If done some more
3188 * messages may have arrived. */
3189 if (channel_parse_messages())
3190 continue;
3191
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003192 /* Wait for up to the timeout. If there was an incomplete message
3193 * use the deadline for that. */
3194 timeout = timeout_arg;
3195 if (chanpart->ch_waiting)
3196 {
3197#ifdef WIN32
3198 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3199#else
3200 {
3201 struct timeval now_tv;
3202
3203 gettimeofday(&now_tv, NULL);
3204 timeout = (chanpart->ch_deadline.tv_sec
3205 - now_tv.tv_sec) * 1000
3206 + (chanpart->ch_deadline.tv_usec
3207 - now_tv.tv_usec) / 1000
3208 + 1;
3209 }
3210#endif
3211 if (timeout < 0)
3212 {
3213 /* Something went wrong, channel_parse_json() didn't
3214 * discard message. Cancel waiting. */
3215 chanpart->ch_waiting = FALSE;
3216 timeout = timeout_arg;
3217 }
3218 else if (timeout > timeout_arg)
3219 timeout = timeout_arg;
3220 }
3221 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003222 if (fd == INVALID_FD
3223 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003224 {
3225 if (timeout == timeout_arg)
3226 {
3227 if (fd != INVALID_FD)
3228 ch_log(channel, "Timed out");
3229 break;
3230 }
3231 }
3232 else
3233 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003234 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003235 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003236 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003237 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003238}
3239
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003240/*
3241 * Common for ch_read() and ch_readraw().
3242 */
3243 void
3244common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3245{
3246 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003247 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003248 jobopt_T opt;
3249 int mode;
3250 int timeout;
3251 int id = -1;
3252 typval_T *listtv = NULL;
3253
3254 /* return an empty string by default */
3255 rettv->v_type = VAR_STRING;
3256 rettv->vval.v_string = NULL;
3257
3258 clear_job_options(&opt);
3259 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3260 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003261 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003262
Bram Moolenaar437905c2016-04-26 19:01:05 +02003263 if (opt.jo_set & JO_PART)
3264 part = opt.jo_part;
3265 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003266 if (channel != NULL)
3267 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003268 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003269 part = channel_part_read(channel);
3270 mode = channel_get_mode(channel, part);
3271 timeout = channel_get_timeout(channel, part);
3272 if (opt.jo_set & JO_TIMEOUT)
3273 timeout = opt.jo_timeout;
3274
3275 if (raw || mode == MODE_RAW || mode == MODE_NL)
3276 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3277 else
3278 {
3279 if (opt.jo_set & JO_ID)
3280 id = opt.jo_id;
3281 channel_read_json_block(channel, part, timeout, id, &listtv);
3282 if (listtv != NULL)
3283 {
3284 *rettv = *listtv;
3285 vim_free(listtv);
3286 }
3287 else
3288 {
3289 rettv->v_type = VAR_SPECIAL;
3290 rettv->vval.v_number = VVAL_NONE;
3291 }
3292 }
3293 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003294
3295theend:
3296 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003297}
3298
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003299# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3300 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003301/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003302 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003303 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003304 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003305 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003306channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003307{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003308 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003309 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003310
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003311 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003312 for (channel = first_channel; channel != NULL;
3313 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003314 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003315 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003316 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003317 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003318 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003319 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003320 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003321 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003322 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003323}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003324# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003325
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003326# if defined(WIN32) || defined(PROTO)
3327/*
3328 * Check the channels for anything that is ready to be read.
3329 * The data is put in the read queue.
3330 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003331 void
3332channel_handle_events(void)
3333{
3334 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003335 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003336 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003337
3338 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3339 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003340 /* If we detected a read error don't try reading again. */
3341 if (channel->ch_to_be_closed)
3342 continue;
3343
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003344 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003345 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003346 {
3347 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003348 if (fd != INVALID_FD)
3349 {
3350 int r = channel_wait(channel, fd, 0);
3351
3352 if (r == CW_READY)
3353 channel_read(channel, part, "channel_handle_events");
3354 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003355 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003356 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003357 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003358 }
3359}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003360# endif
3361
Bram Moolenaard04a0202016-01-26 23:30:18 +01003362/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003363 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003364 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003365 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003366 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003367 int
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003368channel_send(channel_T *channel, int part, char_u *buf, int len, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003369{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003370 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003371 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003372
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003373 fd = channel->ch_part[part].ch_fd;
3374 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003375 {
3376 if (!channel->ch_error && fun != NULL)
3377 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003378 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003379 EMSG2("E630: %s(): write while not connected", fun);
3380 }
3381 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003382 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003383 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003384
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003385 if (log_fd != NULL)
3386 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003387 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003388 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003389 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003390 fprintf(log_fd, "'\n");
3391 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003392 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003393 }
3394
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003395 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003396 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003397 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003398 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003399 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003400 {
3401 if (!channel->ch_error && fun != NULL)
3402 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003403 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003404 EMSG2("E631: %s(): write failed", fun);
3405 }
3406 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003407 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003408 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003409
3410 channel->ch_error = FALSE;
3411 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003412}
3413
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003414/*
3415 * Common for "ch_sendexpr()" and "ch_sendraw()".
3416 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003417 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003418 * Otherwise returns NULL.
3419 */
3420 channel_T *
3421send_common(
3422 typval_T *argvars,
3423 char_u *text,
3424 int id,
3425 int eval,
3426 jobopt_T *opt,
3427 char *fun,
3428 int *part_read)
3429{
3430 channel_T *channel;
3431 int part_send;
3432
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003433 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003434 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003435 if (channel == NULL)
3436 return NULL;
3437 part_send = channel_part_send(channel);
3438 *part_read = channel_part_read(channel);
3439
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003440 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3441 return NULL;
3442
3443 /* Set the callback. An empty callback means no callback and not reading
3444 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3445 * allowed. */
3446 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3447 {
3448 if (eval)
3449 {
3450 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3451 return NULL;
3452 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003453 channel_set_req_callback(channel, part_send,
3454 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003455 }
3456
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003457 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003458 && opt->jo_callback == NULL)
3459 return channel;
3460 return NULL;
3461}
3462
3463/*
3464 * common for "ch_evalexpr()" and "ch_sendexpr()"
3465 */
3466 void
3467ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3468{
3469 char_u *text;
3470 typval_T *listtv;
3471 channel_T *channel;
3472 int id;
3473 ch_mode_T ch_mode;
3474 int part_send;
3475 int part_read;
3476 jobopt_T opt;
3477 int timeout;
3478
3479 /* return an empty string by default */
3480 rettv->v_type = VAR_STRING;
3481 rettv->vval.v_string = NULL;
3482
Bram Moolenaar437905c2016-04-26 19:01:05 +02003483 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003484 if (channel == NULL)
3485 return;
3486 part_send = channel_part_send(channel);
3487
3488 ch_mode = channel_get_mode(channel, part_send);
3489 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3490 {
3491 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3492 return;
3493 }
3494
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003495 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003496 text = json_encode_nr_expr(id, &argvars[1],
3497 ch_mode == MODE_JS ? JSON_JS : 0);
3498 if (text == NULL)
3499 return;
3500
3501 channel = send_common(argvars, text, id, eval, &opt,
3502 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3503 vim_free(text);
3504 if (channel != NULL && eval)
3505 {
3506 if (opt.jo_set & JO_TIMEOUT)
3507 timeout = opt.jo_timeout;
3508 else
3509 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003510 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3511 == OK)
3512 {
3513 list_T *list = listtv->vval.v_list;
3514
3515 /* Move the item from the list and then change the type to
3516 * avoid the value being freed. */
3517 *rettv = list->lv_last->li_tv;
3518 list->lv_last->li_tv.v_type = VAR_NUMBER;
3519 free_tv(listtv);
3520 }
3521 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003522 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003523}
3524
3525/*
3526 * common for "ch_evalraw()" and "ch_sendraw()"
3527 */
3528 void
3529ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3530{
3531 char_u buf[NUMBUFLEN];
3532 char_u *text;
3533 channel_T *channel;
3534 int part_read;
3535 jobopt_T opt;
3536 int timeout;
3537
3538 /* return an empty string by default */
3539 rettv->v_type = VAR_STRING;
3540 rettv->vval.v_string = NULL;
3541
3542 text = get_tv_string_buf(&argvars[1], buf);
3543 channel = send_common(argvars, text, 0, eval, &opt,
3544 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3545 if (channel != NULL && eval)
3546 {
3547 if (opt.jo_set & JO_TIMEOUT)
3548 timeout = opt.jo_timeout;
3549 else
3550 timeout = channel_get_timeout(channel, part_read);
3551 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3552 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003553 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003554}
3555
Bram Moolenaard04a0202016-01-26 23:30:18 +01003556# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003557/*
3558 * Add open channels to the poll struct.
3559 * Return the adjusted struct index.
3560 * The type of "fds" is hidden to avoid problems with the function proto.
3561 */
3562 int
3563channel_poll_setup(int nfd_in, void *fds_in)
3564{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003565 int nfd = nfd_in;
3566 channel_T *channel;
3567 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003568 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003569
Bram Moolenaar77073442016-02-13 23:23:53 +01003570 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003571 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003572 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003573 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003574 chanpart_T *ch_part = &channel->ch_part[part];
3575
3576 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003577 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003578 ch_part->ch_poll_idx = nfd;
3579 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003580 fds[nfd].events = POLLIN;
3581 nfd++;
3582 }
3583 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003584 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003585 }
3586 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003587
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003588 nfd = channel_fill_poll_write(nfd, fds);
3589
Bram Moolenaare0874f82016-01-24 20:36:41 +01003590 return nfd;
3591}
3592
3593/*
3594 * The type of "fds" is hidden to avoid problems with the function proto.
3595 */
3596 int
3597channel_poll_check(int ret_in, void *fds_in)
3598{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003599 int ret = ret_in;
3600 channel_T *channel;
3601 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003602 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003603 int idx;
3604 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003605
Bram Moolenaar77073442016-02-13 23:23:53 +01003606 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003607 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003608 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003609 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003610 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003611
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003612 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003613 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003614 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003615 --ret;
3616 }
3617 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003618
3619 in_part = &channel->ch_part[PART_IN];
3620 idx = in_part->ch_poll_idx;
3621 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3622 {
3623 if (in_part->ch_buf_append)
3624 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003625 if (in_part->ch_bufref.br_buf != NULL)
3626 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003627 }
3628 else
3629 channel_write_in(channel);
3630 --ret;
3631 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003632 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003633
3634 return ret;
3635}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003636# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003637
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003638# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003639/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003640 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003641 */
3642 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003643channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003644{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003645 int maxfd = maxfd_in;
3646 channel_T *channel;
3647 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003648 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003649 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003650
Bram Moolenaar77073442016-02-13 23:23:53 +01003651 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003652 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003653 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003654 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003655 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003656
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003657 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003658 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003659 FD_SET((int)fd, rfds);
3660 if (maxfd < (int)fd)
3661 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003662 }
3663 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003664 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003665
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003666 maxfd = channel_fill_wfds(maxfd, wfds);
3667
Bram Moolenaare0874f82016-01-24 20:36:41 +01003668 return maxfd;
3669}
3670
3671/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003672 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003673 */
3674 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003675channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003676{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003677 int ret = ret_in;
3678 channel_T *channel;
3679 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003680 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003681 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003682 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003683
Bram Moolenaar77073442016-02-13 23:23:53 +01003684 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003685 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003686 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003687 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003688 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003689
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003690 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003691 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003692 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003693 --ret;
3694 }
3695 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003696
3697 in_part = &channel->ch_part[PART_IN];
3698 if (ret > 0 && in_part->ch_fd != INVALID_FD
3699 && FD_ISSET(in_part->ch_fd, wfds))
3700 {
3701 if (in_part->ch_buf_append)
3702 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003703 if (in_part->ch_bufref.br_buf != NULL)
3704 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003705 }
3706 else
3707 channel_write_in(channel);
3708 --ret;
3709 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003710 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003711
3712 return ret;
3713}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003714# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003715
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003716/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003717 * Execute queued up commands.
3718 * Invoked from the main loop when it's safe to execute received commands.
3719 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003720 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003721 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003722channel_parse_messages(void)
3723{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003724 channel_T *channel = first_channel;
3725 int ret = FALSE;
3726 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003727 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003728
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003729 ++safe_to_invoke_callback;
3730
Bram Moolenaard0b65022016-03-06 21:50:33 +01003731 /* Only do this message when another message was given, otherwise we get
3732 * lots of them. */
3733 if (did_log_msg)
3734 {
3735 ch_log(NULL, "looking for messages on channels");
3736 did_log_msg = FALSE;
3737 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003738 while (channel != NULL)
3739 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003740 if (channel->ch_to_be_closed)
3741 {
3742 channel->ch_to_be_closed = FALSE;
3743 channel_close_now(channel);
3744 /* channel may have been freed, start over */
3745 channel = first_channel;
3746 continue;
3747 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003748 if (channel->ch_to_be_freed)
3749 {
3750 channel_free(channel);
3751 /* channel has been freed, start over */
3752 channel = first_channel;
3753 continue;
3754 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003755 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003756 {
3757 /* channel is no longer useful, free it */
3758 channel_free(channel);
3759 channel = first_channel;
3760 part = PART_SOCK;
3761 continue;
3762 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003763 if (channel->ch_part[part].ch_fd != INVALID_FD
3764 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003765 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003766 /* Increase the refcount, in case the handler causes the channel
3767 * to be unreferenced or closed. */
3768 ++channel->ch_refcount;
3769 r = may_invoke_callback(channel, part);
3770 if (r == OK)
3771 ret = TRUE;
3772 if (channel_unref(channel) || r == OK)
3773 {
3774 /* channel was freed or something was done, start over */
3775 channel = first_channel;
3776 part = PART_SOCK;
3777 continue;
3778 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003779 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003780 if (part < PART_ERR)
3781 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003782 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003783 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003784 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003785 part = PART_SOCK;
3786 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003787 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003788
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003789 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003790 {
3791 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003792 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003793 }
3794
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003795 --safe_to_invoke_callback;
3796
Bram Moolenaard7ece102016-02-02 23:23:02 +01003797 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003798}
3799
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003800/*
3801 * Mark references to lists used in channels.
3802 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003803 int
3804set_ref_in_channel(int copyID)
3805{
Bram Moolenaar77073442016-02-13 23:23:53 +01003806 int abort = FALSE;
3807 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003808 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003809
Bram Moolenaar77073442016-02-13 23:23:53 +01003810 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003811 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003812 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003813 tv.v_type = VAR_CHANNEL;
3814 tv.vval.v_channel = channel;
3815 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003816 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003817 return abort;
3818}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003819
3820/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003821 * Return the "part" to write to for "channel".
3822 */
3823 int
3824channel_part_send(channel_T *channel)
3825{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003826 if (channel->CH_SOCK_FD == INVALID_FD)
3827 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003828 return PART_SOCK;
3829}
3830
3831/*
3832 * Return the default "part" to read from for "channel".
3833 */
3834 int
3835channel_part_read(channel_T *channel)
3836{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003837 if (channel->CH_SOCK_FD == INVALID_FD)
3838 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003839 return PART_SOCK;
3840}
3841
3842/*
3843 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003844 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003845 */
3846 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003847channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003848{
Bram Moolenaar77073442016-02-13 23:23:53 +01003849 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003850 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003851 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003852}
3853
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003854/*
3855 * Return the timeout of "channel"/"part"
3856 */
3857 int
3858channel_get_timeout(channel_T *channel, int part)
3859{
3860 return channel->ch_part[part].ch_timeout;
3861}
3862
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003863 static int
3864handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3865{
3866 char_u *val = get_tv_string(item);
3867
3868 opt->jo_set |= jo;
3869 if (STRCMP(val, "nl") == 0)
3870 *modep = MODE_NL;
3871 else if (STRCMP(val, "raw") == 0)
3872 *modep = MODE_RAW;
3873 else if (STRCMP(val, "js") == 0)
3874 *modep = MODE_JS;
3875 else if (STRCMP(val, "json") == 0)
3876 *modep = MODE_JSON;
3877 else
3878 {
3879 EMSG2(_(e_invarg2), val);
3880 return FAIL;
3881 }
3882 return OK;
3883}
3884
3885 static int
3886handle_io(typval_T *item, int part, jobopt_T *opt)
3887{
3888 char_u *val = get_tv_string(item);
3889
3890 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3891 if (STRCMP(val, "null") == 0)
3892 opt->jo_io[part] = JIO_NULL;
3893 else if (STRCMP(val, "pipe") == 0)
3894 opt->jo_io[part] = JIO_PIPE;
3895 else if (STRCMP(val, "file") == 0)
3896 opt->jo_io[part] = JIO_FILE;
3897 else if (STRCMP(val, "buffer") == 0)
3898 opt->jo_io[part] = JIO_BUFFER;
3899 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3900 opt->jo_io[part] = JIO_OUT;
3901 else
3902 {
3903 EMSG2(_(e_invarg2), val);
3904 return FAIL;
3905 }
3906 return OK;
3907}
3908
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003909/*
3910 * Clear a jobopt_T before using it.
3911 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003912 void
3913clear_job_options(jobopt_T *opt)
3914{
3915 vim_memset(opt, 0, sizeof(jobopt_T));
3916}
3917
3918/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003919 * Free any members of a jobopt_T.
3920 */
3921 void
3922free_job_options(jobopt_T *opt)
3923{
3924 if (opt->jo_partial != NULL)
3925 partial_unref(opt->jo_partial);
3926 if (opt->jo_out_partial != NULL)
3927 partial_unref(opt->jo_out_partial);
3928 if (opt->jo_err_partial != NULL)
3929 partial_unref(opt->jo_err_partial);
3930 if (opt->jo_close_partial != NULL)
3931 partial_unref(opt->jo_close_partial);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003932 if (opt->jo_exit_partial != NULL)
3933 partial_unref(opt->jo_exit_partial);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003934}
3935
3936/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003937 * Get the PART_ number from the first character of an option name.
3938 */
3939 static int
3940part_from_char(int c)
3941{
3942 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3943}
3944
3945/*
3946 * Get the option entries from the dict in "tv", parse them and put the result
3947 * in "opt".
3948 * Only accept options in "supported".
3949 * If an option value is invalid return FAIL.
3950 */
3951 int
3952get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3953{
3954 typval_T *item;
3955 char_u *val;
3956 dict_T *dict;
3957 int todo;
3958 hashitem_T *hi;
3959 int part;
3960
3961 opt->jo_set = 0;
3962 if (tv->v_type == VAR_UNKNOWN)
3963 return OK;
3964 if (tv->v_type != VAR_DICT)
3965 {
3966 EMSG(_(e_invarg));
3967 return FAIL;
3968 }
3969 dict = tv->vval.v_dict;
3970 if (dict == NULL)
3971 return OK;
3972
3973 todo = (int)dict->dv_hashtab.ht_used;
3974 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3975 if (!HASHITEM_EMPTY(hi))
3976 {
3977 item = &dict_lookup(hi)->di_tv;
3978
3979 if (STRCMP(hi->hi_key, "mode") == 0)
3980 {
3981 if (!(supported & JO_MODE))
3982 break;
3983 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3984 return FAIL;
3985 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003986 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003987 {
3988 if (!(supported & JO_IN_MODE))
3989 break;
3990 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3991 == FAIL)
3992 return FAIL;
3993 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003994 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003995 {
3996 if (!(supported & JO_OUT_MODE))
3997 break;
3998 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3999 == FAIL)
4000 return FAIL;
4001 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004002 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004003 {
4004 if (!(supported & JO_ERR_MODE))
4005 break;
4006 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4007 == FAIL)
4008 return FAIL;
4009 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004010 else if (STRCMP(hi->hi_key, "in_io") == 0
4011 || STRCMP(hi->hi_key, "out_io") == 0
4012 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004013 {
4014 if (!(supported & JO_OUT_IO))
4015 break;
4016 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4017 return FAIL;
4018 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004019 else if (STRCMP(hi->hi_key, "in_name") == 0
4020 || STRCMP(hi->hi_key, "out_name") == 0
4021 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004022 {
4023 part = part_from_char(*hi->hi_key);
4024
4025 if (!(supported & JO_OUT_IO))
4026 break;
4027 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4028 opt->jo_io_name[part] =
4029 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4030 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004031 else if (STRCMP(hi->hi_key, "in_buf") == 0
4032 || STRCMP(hi->hi_key, "out_buf") == 0
4033 || STRCMP(hi->hi_key, "err_buf") == 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_BUF << (part - PART_OUT);
4040 opt->jo_io_buf[part] = get_tv_number(item);
4041 if (opt->jo_io_buf[part] <= 0)
4042 {
4043 EMSG2(_(e_invarg2), get_tv_string(item));
4044 return FAIL;
4045 }
4046 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4047 {
4048 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4049 return FAIL;
4050 }
4051 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004052 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4053 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4054 {
4055 part = part_from_char(*hi->hi_key);
4056
4057 if (!(supported & JO_OUT_IO))
4058 break;
4059 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4060 opt->jo_modifiable[part] = get_tv_number(item);
4061 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004062 else if (STRCMP(hi->hi_key, "in_top") == 0
4063 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004064 {
4065 linenr_T *lp;
4066
4067 if (!(supported & JO_OUT_IO))
4068 break;
4069 if (hi->hi_key[3] == 't')
4070 {
4071 lp = &opt->jo_in_top;
4072 opt->jo_set |= JO_IN_TOP;
4073 }
4074 else
4075 {
4076 lp = &opt->jo_in_bot;
4077 opt->jo_set |= JO_IN_BOT;
4078 }
4079 *lp = get_tv_number(item);
4080 if (*lp < 0)
4081 {
4082 EMSG2(_(e_invarg2), get_tv_string(item));
4083 return FAIL;
4084 }
4085 }
4086 else if (STRCMP(hi->hi_key, "channel") == 0)
4087 {
4088 if (!(supported & JO_OUT_IO))
4089 break;
4090 opt->jo_set |= JO_CHANNEL;
4091 if (item->v_type != VAR_CHANNEL)
4092 {
4093 EMSG2(_(e_invarg2), "channel");
4094 return FAIL;
4095 }
4096 opt->jo_channel = item->vval.v_channel;
4097 }
4098 else if (STRCMP(hi->hi_key, "callback") == 0)
4099 {
4100 if (!(supported & JO_CALLBACK))
4101 break;
4102 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004103 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004104 if (opt->jo_callback == NULL)
4105 {
4106 EMSG2(_(e_invarg2), "callback");
4107 return FAIL;
4108 }
4109 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004110 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004111 {
4112 if (!(supported & JO_OUT_CALLBACK))
4113 break;
4114 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004115 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004116 if (opt->jo_out_cb == NULL)
4117 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004118 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004119 return FAIL;
4120 }
4121 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004122 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004123 {
4124 if (!(supported & JO_ERR_CALLBACK))
4125 break;
4126 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004127 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004128 if (opt->jo_err_cb == NULL)
4129 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004130 EMSG2(_(e_invarg2), "err_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, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004135 {
4136 if (!(supported & JO_CLOSE_CALLBACK))
4137 break;
4138 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004139 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004140 if (opt->jo_close_cb == NULL)
4141 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004142 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004143 return FAIL;
4144 }
4145 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004146 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4147 {
4148 if (!(supported & JO_EXIT_CB))
4149 break;
4150 opt->jo_set |= JO_EXIT_CB;
4151 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4152 if (opt->jo_exit_cb == NULL)
4153 {
4154 EMSG2(_(e_invarg2), "exit_cb");
4155 return FAIL;
4156 }
4157 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004158 else if (STRCMP(hi->hi_key, "waittime") == 0)
4159 {
4160 if (!(supported & JO_WAITTIME))
4161 break;
4162 opt->jo_set |= JO_WAITTIME;
4163 opt->jo_waittime = get_tv_number(item);
4164 }
4165 else if (STRCMP(hi->hi_key, "timeout") == 0)
4166 {
4167 if (!(supported & JO_TIMEOUT))
4168 break;
4169 opt->jo_set |= JO_TIMEOUT;
4170 opt->jo_timeout = get_tv_number(item);
4171 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004172 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004173 {
4174 if (!(supported & JO_OUT_TIMEOUT))
4175 break;
4176 opt->jo_set |= JO_OUT_TIMEOUT;
4177 opt->jo_out_timeout = get_tv_number(item);
4178 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004179 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004180 {
4181 if (!(supported & JO_ERR_TIMEOUT))
4182 break;
4183 opt->jo_set |= JO_ERR_TIMEOUT;
4184 opt->jo_err_timeout = get_tv_number(item);
4185 }
4186 else if (STRCMP(hi->hi_key, "part") == 0)
4187 {
4188 if (!(supported & JO_PART))
4189 break;
4190 opt->jo_set |= JO_PART;
4191 val = get_tv_string(item);
4192 if (STRCMP(val, "err") == 0)
4193 opt->jo_part = PART_ERR;
4194 else
4195 {
4196 EMSG2(_(e_invarg2), val);
4197 return FAIL;
4198 }
4199 }
4200 else if (STRCMP(hi->hi_key, "id") == 0)
4201 {
4202 if (!(supported & JO_ID))
4203 break;
4204 opt->jo_set |= JO_ID;
4205 opt->jo_id = get_tv_number(item);
4206 }
4207 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4208 {
4209 if (!(supported & JO_STOPONEXIT))
4210 break;
4211 opt->jo_set |= JO_STOPONEXIT;
4212 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4213 opt->jo_soe_buf);
4214 if (opt->jo_stoponexit == NULL)
4215 {
4216 EMSG2(_(e_invarg2), "stoponexit");
4217 return FAIL;
4218 }
4219 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004220 else if (STRCMP(hi->hi_key, "block_write") == 0)
4221 {
4222 if (!(supported & JO_BLOCK_WRITE))
4223 break;
4224 opt->jo_set |= JO_BLOCK_WRITE;
4225 opt->jo_block_write = get_tv_number(item);
4226 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004227 else
4228 break;
4229 --todo;
4230 }
4231 if (todo > 0)
4232 {
4233 EMSG2(_(e_invarg2), hi->hi_key);
4234 return FAIL;
4235 }
4236
4237 return OK;
4238}
4239
4240/*
4241 * Get the channel from the argument.
4242 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004243 * When "check_open" is TRUE check that the channel can be used.
4244 * When "reading" is TRUE "check_open" considers typeahead useful.
4245 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004246 */
4247 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004248get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004249{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004250 channel_T *channel = NULL;
4251 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004252
4253 if (tv->v_type == VAR_JOB)
4254 {
4255 if (tv->vval.v_job != NULL)
4256 channel = tv->vval.v_job->jv_channel;
4257 }
4258 else if (tv->v_type == VAR_CHANNEL)
4259 {
4260 channel = tv->vval.v_channel;
4261 }
4262 else
4263 {
4264 EMSG2(_(e_invarg2), get_tv_string(tv));
4265 return NULL;
4266 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004267 if (channel != NULL && reading)
4268 has_readahead = channel_has_readahead(channel,
4269 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004270
Bram Moolenaar437905c2016-04-26 19:01:05 +02004271 if (check_open && (channel == NULL || (!channel_is_open(channel)
4272 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004273 {
4274 EMSG(_("E906: not an open channel"));
4275 return NULL;
4276 }
4277 return channel;
4278}
4279
4280static job_T *first_job = NULL;
4281
4282 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004283job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004284{
4285 ch_log(job->jv_channel, "Freeing job");
4286 if (job->jv_channel != NULL)
4287 {
4288 /* The link from the channel to the job doesn't count as a reference,
4289 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004290 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004291 * NULL the reference. We don't set ch_job_killed, unreferencing the
4292 * job doesn't mean it stops running. */
4293 job->jv_channel->ch_job = NULL;
4294 channel_unref(job->jv_channel);
4295 }
4296 mch_clear_job(job);
4297
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004298 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004299 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004300}
4301
4302 static void
4303job_free_job(job_T *job)
4304{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004305 if (job->jv_next != NULL)
4306 job->jv_next->jv_prev = job->jv_prev;
4307 if (job->jv_prev == NULL)
4308 first_job = job->jv_next;
4309 else
4310 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004311 vim_free(job);
4312}
4313
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004314 static void
4315job_free(job_T *job)
4316{
4317 if (!in_free_unref_items)
4318 {
4319 job_free_contents(job);
4320 job_free_job(job);
4321 }
4322}
4323
Bram Moolenaar655da312016-05-28 22:22:34 +02004324#if defined(EXITFREE) || defined(PROTO)
4325 void
4326job_free_all(void)
4327{
4328 while (first_job != NULL)
4329 job_free(first_job);
4330}
4331#endif
4332
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004333/*
4334 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004335 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4336 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004337 */
4338 static int
4339job_still_useful(job_T *job)
4340{
4341 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004342 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4343 || (job->jv_channel != NULL
4344 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004345}
4346
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004347/*
4348 * Mark references in jobs that are still useful.
4349 */
4350 int
4351set_ref_in_job(int copyID)
4352{
4353 int abort = FALSE;
4354 job_T *job;
4355 typval_T tv;
4356
4357 for (job = first_job; job != NULL; job = job->jv_next)
4358 if (job_still_useful(job))
4359 {
4360 tv.v_type = VAR_JOB;
4361 tv.vval.v_job = job;
4362 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4363 }
4364 return abort;
4365}
4366
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004367 void
4368job_unref(job_T *job)
4369{
4370 if (job != NULL && --job->jv_refcount <= 0)
4371 {
4372 /* Do not free the job when it has not ended yet and there is a
4373 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004374 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004375 {
4376 job_free(job);
4377 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004378 else if (job->jv_channel != NULL
4379 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004380 {
4381 /* Do remove the link to the channel, otherwise it hangs
4382 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004383 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004384 job->jv_channel->ch_job = NULL;
4385 channel_unref(job->jv_channel);
4386 job->jv_channel = NULL;
4387 }
4388 }
4389}
4390
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004391 int
4392free_unused_jobs_contents(int copyID, int mask)
4393{
4394 int did_free = FALSE;
4395 job_T *job;
4396
4397 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004398 if ((job->jv_copyID & mask) != (copyID & mask)
4399 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004400 {
4401 /* Free the channel and ordinary items it contains, but don't
4402 * recurse into Lists, Dictionaries etc. */
4403 job_free_contents(job);
4404 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004405 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004406 return did_free;
4407}
4408
4409 void
4410free_unused_jobs(int copyID, int mask)
4411{
4412 job_T *job;
4413 job_T *job_next;
4414
4415 for (job = first_job; job != NULL; job = job_next)
4416 {
4417 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004418 if ((job->jv_copyID & mask) != (copyID & mask)
4419 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004420 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004421 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004422 job_free_job(job);
4423 }
4424 }
4425}
4426
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004427/*
4428 * Allocate a job. Sets the refcount to one and sets options default.
4429 */
4430 static job_T *
4431job_alloc(void)
4432{
4433 job_T *job;
4434
4435 job = (job_T *)alloc_clear(sizeof(job_T));
4436 if (job != NULL)
4437 {
4438 job->jv_refcount = 1;
4439 job->jv_stoponexit = vim_strsave((char_u *)"term");
4440
4441 if (first_job != NULL)
4442 {
4443 first_job->jv_prev = job;
4444 job->jv_next = first_job;
4445 }
4446 first_job = job;
4447 }
4448 return job;
4449}
4450
4451 void
4452job_set_options(job_T *job, jobopt_T *opt)
4453{
4454 if (opt->jo_set & JO_STOPONEXIT)
4455 {
4456 vim_free(job->jv_stoponexit);
4457 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4458 job->jv_stoponexit = NULL;
4459 else
4460 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4461 }
4462 if (opt->jo_set & JO_EXIT_CB)
4463 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004464 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004465 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004466 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004467 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004468 job->jv_exit_partial = NULL;
4469 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004470 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004471 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004472 job->jv_exit_partial = opt->jo_exit_partial;
4473 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004474 {
4475 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004476 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004477 }
4478 else
4479 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004480 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004481 }
4482}
4483
4484/*
4485 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4486 */
4487 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004488job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004489{
4490 job_T *job;
4491
4492 for (job = first_job; job != NULL; job = job->jv_next)
4493 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4494 mch_stop_job(job, job->jv_stoponexit);
4495}
4496
4497/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004498 * Return TRUE when there is any job that might exit, which means
4499 * job_check_ended() should be called once in a while.
4500 */
4501 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004502has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004503{
4504 job_T *job;
4505
4506 for (job = first_job; job != NULL; job = job->jv_next)
4507 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4508 return TRUE;
4509 return FALSE;
4510}
4511
4512/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004513 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004514 */
4515 void
4516job_check_ended(void)
4517{
4518 static time_t last_check = 0;
4519 time_t now;
4520 job_T *job;
4521 job_T *next;
4522
4523 /* Only do this once in 10 seconds. */
4524 now = time(NULL);
4525 if (last_check + 10 < now)
4526 {
4527 last_check = now;
4528 for (job = first_job; job != NULL; job = next)
4529 {
4530 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004531 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004532 job_status(job); /* may free "job" */
4533 }
4534 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004535 if (channel_need_redraw)
4536 {
4537 channel_need_redraw = FALSE;
4538 redraw_after_callback();
4539 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004540}
4541
4542/*
4543 * "job_start()" function
4544 */
4545 job_T *
4546job_start(typval_T *argvars)
4547{
4548 job_T *job;
4549 char_u *cmd = NULL;
4550#if defined(UNIX)
4551# define USE_ARGV
4552 char **argv = NULL;
4553 int argc = 0;
4554#else
4555 garray_T ga;
4556#endif
4557 jobopt_T opt;
4558 int part;
4559
4560 job = job_alloc();
4561 if (job == NULL)
4562 return NULL;
4563
4564 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004565#ifndef USE_ARGV
4566 ga_init2(&ga, (int)sizeof(char*), 20);
4567#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004568
4569 /* Default mode is NL. */
4570 clear_job_options(&opt);
4571 opt.jo_mode = MODE_NL;
4572 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004573 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4574 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004575 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004576
4577 /* Check that when io is "file" that there is a file name. */
4578 for (part = PART_OUT; part <= PART_IN; ++part)
4579 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4580 && opt.jo_io[part] == JIO_FILE
4581 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4582 || *opt.jo_io_name[part] == NUL))
4583 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004584 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004585 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004586 }
4587
4588 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4589 {
4590 buf_T *buf = NULL;
4591
4592 /* check that we can find the buffer before starting the job */
4593 if (opt.jo_set & JO_IN_BUF)
4594 {
4595 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4596 if (buf == NULL)
4597 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4598 }
4599 else if (!(opt.jo_set & JO_IN_NAME))
4600 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004601 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004602 }
4603 else
4604 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4605 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004606 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004607 if (buf->b_ml.ml_mfp == NULL)
4608 {
4609 char_u numbuf[NUMBUFLEN];
4610 char_u *s;
4611
4612 if (opt.jo_set & JO_IN_BUF)
4613 {
4614 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4615 s = numbuf;
4616 }
4617 else
4618 s = opt.jo_io_name[PART_IN];
4619 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004620 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004621 }
4622 job->jv_in_buf = buf;
4623 }
4624
4625 job_set_options(job, &opt);
4626
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004627 if (argvars[0].v_type == VAR_STRING)
4628 {
4629 /* Command is a string. */
4630 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004631 if (cmd == NULL || *cmd == NUL)
4632 {
4633 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004634 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004635 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004636#ifdef USE_ARGV
4637 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004638 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004639 argv[argc] = NULL;
4640#endif
4641 }
4642 else if (argvars[0].v_type != VAR_LIST
4643 || argvars[0].vval.v_list == NULL
4644 || argvars[0].vval.v_list->lv_len < 1)
4645 {
4646 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004647 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004648 }
4649 else
4650 {
4651 list_T *l = argvars[0].vval.v_list;
4652 listitem_T *li;
4653 char_u *s;
4654
4655#ifdef USE_ARGV
4656 /* Pass argv[] to mch_call_shell(). */
4657 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4658 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004659 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004660#endif
4661 for (li = l->lv_first; li != NULL; li = li->li_next)
4662 {
4663 s = get_tv_string_chk(&li->li_tv);
4664 if (s == NULL)
4665 goto theend;
4666#ifdef USE_ARGV
4667 argv[argc++] = (char *)s;
4668#else
4669 /* Only escape when needed, double quotes are not always allowed. */
4670 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4671 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004672# ifdef WIN32
4673 int old_ssl = p_ssl;
4674
4675 /* This is using CreateProcess, not cmd.exe. Always use
4676 * double quote and backslashes. */
4677 p_ssl = 0;
4678# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004679 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004680# ifdef WIN32
4681 p_ssl = old_ssl;
4682# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004683 if (s == NULL)
4684 goto theend;
4685 ga_concat(&ga, s);
4686 vim_free(s);
4687 }
4688 else
4689 ga_concat(&ga, s);
4690 if (li->li_next != NULL)
4691 ga_append(&ga, ' ');
4692#endif
4693 }
4694#ifdef USE_ARGV
4695 argv[argc] = NULL;
4696#else
4697 cmd = ga.ga_data;
4698#endif
4699 }
4700
4701#ifdef USE_ARGV
4702 if (ch_log_active())
4703 {
4704 garray_T ga;
4705 int i;
4706
4707 ga_init2(&ga, (int)sizeof(char), 200);
4708 for (i = 0; i < argc; ++i)
4709 {
4710 if (i > 0)
4711 ga_concat(&ga, (char_u *)" ");
4712 ga_concat(&ga, (char_u *)argv[i]);
4713 }
4714 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4715 ga_clear(&ga);
4716 }
4717 mch_start_job(argv, job, &opt);
4718#else
4719 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4720 mch_start_job((char *)cmd, job, &opt);
4721#endif
4722
4723 /* If the channel is reading from a buffer, write lines now. */
4724 if (job->jv_channel != NULL)
4725 channel_write_in(job->jv_channel);
4726
4727theend:
4728#ifdef USE_ARGV
4729 vim_free(argv);
4730#else
4731 vim_free(ga.ga_data);
4732#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004733 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004734 return job;
4735}
4736
4737/*
4738 * Get the status of "job" and invoke the exit callback when needed.
4739 * The returned string is not allocated.
4740 */
4741 char *
4742job_status(job_T *job)
4743{
4744 char *result;
4745
4746 if (job->jv_status == JOB_ENDED)
4747 /* No need to check, dead is dead. */
4748 result = "dead";
4749 else if (job->jv_status == JOB_FAILED)
4750 result = "fail";
4751 else
4752 {
4753 result = mch_job_status(job);
4754 if (job->jv_status == JOB_ENDED)
4755 ch_log(job->jv_channel, "Job ended");
4756 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4757 {
4758 typval_T argv[3];
4759 typval_T rettv;
4760 int dummy;
4761
4762 /* invoke the exit callback; make sure the refcount is > 0 */
4763 ++job->jv_refcount;
4764 argv[0].v_type = VAR_JOB;
4765 argv[0].vval.v_job = job;
4766 argv[1].v_type = VAR_NUMBER;
4767 argv[1].vval.v_number = job->jv_exitval;
4768 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004769 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004770 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004771 clear_tv(&rettv);
4772 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004773 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004774 }
4775 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4776 {
4777 /* The job was already unreferenced, now that it ended it can be
4778 * freed. Careful: caller must not use "job" after this! */
4779 job_free(job);
4780 }
4781 }
4782 return result;
4783}
4784
Bram Moolenaar8950a562016-03-12 15:22:55 +01004785/*
4786 * Implementation of job_info().
4787 */
4788 void
4789job_info(job_T *job, dict_T *dict)
4790{
4791 dictitem_T *item;
4792 varnumber_T nr;
4793
4794 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4795
4796 item = dictitem_alloc((char_u *)"channel");
4797 if (item == NULL)
4798 return;
4799 item->di_tv.v_lock = 0;
4800 item->di_tv.v_type = VAR_CHANNEL;
4801 item->di_tv.vval.v_channel = job->jv_channel;
4802 if (job->jv_channel != NULL)
4803 ++job->jv_channel->ch_refcount;
4804 if (dict_add(dict, item) == FAIL)
4805 dictitem_free(item);
4806
4807#ifdef UNIX
4808 nr = job->jv_pid;
4809#else
4810 nr = job->jv_proc_info.dwProcessId;
4811#endif
4812 dict_add_nr_str(dict, "process", nr, NULL);
4813
4814 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004815 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004816 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4817}
4818
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004819 int
4820job_stop(job_T *job, typval_T *argvars)
4821{
4822 char_u *arg;
4823
4824 if (argvars[1].v_type == VAR_UNKNOWN)
4825 arg = (char_u *)"";
4826 else
4827 {
4828 arg = get_tv_string_chk(&argvars[1]);
4829 if (arg == NULL)
4830 {
4831 EMSG(_(e_invarg));
4832 return 0;
4833 }
4834 }
4835 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4836 if (mch_stop_job(job, arg) == FAIL)
4837 return 0;
4838
4839 /* Assume that "hup" does not kill the job. */
4840 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4841 job->jv_channel->ch_job_killed = TRUE;
4842
4843 /* We don't try freeing the job, obviously the caller still has a
4844 * reference to it. */
4845 return 1;
4846}
4847
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004848#endif /* FEAT_JOB_CHANNEL */