blob: ae5bdf220bb7afded9132f1890d750c9d25cf8bb [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 Moolenaar77073442016-02-13 23:23:53 +01001275 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001276 item->cq_partial = partial;
1277 if (partial != NULL)
1278 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001279 item->cq_seq_nr = id;
1280 item->cq_prev = head->cq_prev;
1281 head->cq_prev = item;
1282 item->cq_next = NULL;
1283 if (item->cq_prev == NULL)
1284 head->cq_next = item;
1285 else
1286 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001287 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001288}
1289
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001290 static void
1291write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1292{
1293 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001294 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001295 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001296 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001297
Bram Moolenaar655da312016-05-28 22:22:34 +02001298 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001299 if ((p = alloc(len + 2)) == NULL)
1300 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001301 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001302
1303 for (i = 0; i < len; ++i)
1304 if (p[i] == NL)
1305 p[i] = NUL;
1306
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001307 p[len] = NL;
1308 p[len + 1] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001309 channel_send(channel, PART_IN, p, len + 1, "write_buf_line()");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001310 vim_free(p);
1311}
1312
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001313/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001314 * Return TRUE if "channel" can be written to.
1315 * Returns FALSE if the input is closed or the write would block.
1316 */
1317 static int
1318can_write_buf_line(channel_T *channel)
1319{
1320 chanpart_T *in_part = &channel->ch_part[PART_IN];
1321
1322 if (in_part->ch_fd == INVALID_FD)
1323 return FALSE; /* pipe was closed */
1324
1325 /* for testing: block every other attempt to write */
1326 if (in_part->ch_block_write == 1)
1327 in_part->ch_block_write = -1;
1328 else if (in_part->ch_block_write == -1)
1329 in_part->ch_block_write = 1;
1330
1331 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1332#ifndef WIN32
1333 {
1334# if defined(HAVE_SELECT)
1335 struct timeval tval;
1336 fd_set wfds;
1337 int ret;
1338
1339 FD_ZERO(&wfds);
1340 FD_SET((int)in_part->ch_fd, &wfds);
1341 tval.tv_sec = 0;
1342 tval.tv_usec = 0;
1343 for (;;)
1344 {
1345 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1346# ifdef EINTR
1347 SOCK_ERRNO;
1348 if (ret == -1 && errno == EINTR)
1349 continue;
1350# endif
1351 if (ret <= 0 || in_part->ch_block_write == 1)
1352 {
1353 if (ret > 0)
1354 ch_log(channel, "FAKED Input not ready for writing");
1355 else
1356 ch_log(channel, "Input not ready for writing");
1357 return FALSE;
1358 }
1359 break;
1360 }
1361# else
1362 struct pollfd fds;
1363
1364 fds.fd = in_part->ch_fd;
1365 fds.events = POLLOUT;
1366 if (poll(&fds, 1, 0) <= 0)
1367 {
1368 ch_log(channel, "Input not ready for writing");
1369 return FALSE;
1370 }
1371 if (in_part->ch_block_write == 1)
1372 {
1373 ch_log(channel, "FAKED Input not ready for writing");
1374 return FALSE;
1375 }
1376# endif
1377 }
1378#endif
1379 return TRUE;
1380}
1381
1382/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001383 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001384 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001385 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001386channel_write_in(channel_T *channel)
1387{
1388 chanpart_T *in_part = &channel->ch_part[PART_IN];
1389 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001390 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001391 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001392
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001393 if (buf == NULL || in_part->ch_buf_append)
1394 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001395 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001396 {
1397 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001398 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001399 return;
1400 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001401
1402 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1403 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1404 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001405 if (!can_write_buf_line(channel))
1406 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001407 write_buf_line(buf, lnum, channel);
1408 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001409 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001410
1411 if (written == 1)
1412 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1413 else if (written > 1)
1414 ch_logn(channel, "written %d lines to channel", written);
1415
Bram Moolenaar014069a2016-03-03 22:51:40 +01001416 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001417 if (lnum > buf->b_ml.ml_line_count)
1418 {
1419 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001420 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001421 ch_log(channel, "Finished writing all lines to channel");
1422 }
1423 else
1424 ch_logn(channel, "Still %d more lines to write",
1425 buf->b_ml.ml_line_count - lnum + 1);
1426}
1427
1428/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001429 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001430 */
1431 void
1432channel_buffer_free(buf_T *buf)
1433{
1434 channel_T *channel;
1435 int part;
1436
1437 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1438 for (part = PART_SOCK; part <= PART_IN; ++part)
1439 {
1440 chanpart_T *ch_part = &channel->ch_part[part];
1441
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001442 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001443 {
1444 ch_logs(channel, "%s buffer has been wiped out",
1445 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001446 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001447 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001448 }
1449}
1450
1451/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001452 * Write any lines waiting to be written to a channel.
1453 */
1454 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001455channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001456{
1457 channel_T *channel;
1458
1459 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1460 {
1461 chanpart_T *in_part = &channel->ch_part[PART_IN];
1462
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001463 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001464 {
1465 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001466 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001467 else
1468 channel_write_in(channel);
1469 }
1470 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001471}
1472
1473/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001474 * Write appended lines above the last one in "buf" to the channel.
1475 */
1476 void
1477channel_write_new_lines(buf_T *buf)
1478{
1479 channel_T *channel;
1480 int found_one = FALSE;
1481
1482 /* There could be more than one channel for the buffer, loop over all of
1483 * them. */
1484 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1485 {
1486 chanpart_T *in_part = &channel->ch_part[PART_IN];
1487 linenr_T lnum;
1488 int written = 0;
1489
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001490 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001491 {
1492 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001493 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001494 found_one = TRUE;
1495 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1496 ++lnum)
1497 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001498 if (!can_write_buf_line(channel))
1499 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001500 write_buf_line(buf, lnum, channel);
1501 ++written;
1502 }
1503
1504 if (written == 1)
1505 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1506 else if (written > 1)
1507 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001508 if (lnum < buf->b_ml.ml_line_count)
1509 ch_logn(channel, "Still %d more lines to write",
1510 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001511
1512 in_part->ch_buf_bot = lnum;
1513 }
1514 }
1515 if (!found_one)
1516 buf->b_write_to_channel = FALSE;
1517}
1518
1519/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001520 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001521 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001522 */
1523 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001524invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1525 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001526{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001527 typval_T rettv;
1528 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001529
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001530 if (safe_to_invoke_callback == 0)
1531 EMSG("INTERNAL: Invoking callback when it is not safe");
1532
Bram Moolenaar77073442016-02-13 23:23:53 +01001533 argv[0].v_type = VAR_CHANNEL;
1534 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001535
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001536 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001537 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001538 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001539 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001540}
1541
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001542/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001543 * Return the first node from "channel"/"part" without removing it.
1544 * Returns NULL if there is nothing.
1545 */
1546 readq_T *
1547channel_peek(channel_T *channel, int part)
1548{
1549 readq_T *head = &channel->ch_part[part].ch_head;
1550
1551 return head->rq_next;
1552}
1553
1554/*
1555 * Return a pointer to the first NL in "node".
1556 * Skips over NUL characters.
1557 * Returns NULL if there is no NL.
1558 */
1559 char_u *
1560channel_first_nl(readq_T *node)
1561{
1562 char_u *buffer = node->rq_buffer;
1563 long_u i;
1564
1565 for (i = 0; i < node->rq_buflen; ++i)
1566 if (buffer[i] == NL)
1567 return buffer + i;
1568 return NULL;
1569}
1570
1571/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001572 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001573 * The caller must free it.
1574 * Returns NULL if there is nothing.
1575 */
1576 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001577channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001578{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001579 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001580 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001581 char_u *p;
1582
Bram Moolenaar77073442016-02-13 23:23:53 +01001583 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001584 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001585 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001586 p = node->rq_buffer;
1587 head->rq_next = node->rq_next;
1588 if (node->rq_next == NULL)
1589 head->rq_prev = NULL;
1590 else
1591 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001592 vim_free(node);
1593 return p;
1594}
1595
1596/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001597 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001598 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001599 */
1600 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001601channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001602{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001603 readq_T *head = &channel->ch_part[part].ch_head;
1604 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001605 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001606 char_u *res;
1607 char_u *p;
1608
1609 /* If there is only one buffer just get that one. */
1610 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1611 return channel_get(channel, part);
1612
1613 /* Concatenate everything into one buffer. */
1614 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001615 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001616 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001617 if (res == NULL)
1618 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001619 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001620 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001621 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001622 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001623 p += node->rq_buflen;
1624 }
1625 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001626
1627 /* Free all buffers */
1628 do
1629 {
1630 p = channel_get(channel, part);
1631 vim_free(p);
1632 } while (p != NULL);
1633
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001634 /* turn all NUL into NL */
1635 while (len > 0)
1636 {
1637 --len;
1638 if (res[len] == NUL)
1639 res[len] = NL;
1640 }
1641
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001642 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001643}
1644
1645/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001646 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001647 * Caller must check these bytes are available.
1648 */
1649 void
1650channel_consume(channel_T *channel, int part, int len)
1651{
1652 readq_T *head = &channel->ch_part[part].ch_head;
1653 readq_T *node = head->rq_next;
1654 char_u *buf = node->rq_buffer;
1655
1656 mch_memmove(buf, buf + len, node->rq_buflen - len);
1657 node->rq_buflen -= len;
1658}
1659
1660/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001661 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001662 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001663 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001664 */
1665 int
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001666channel_collapse(channel_T *channel, int part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001667{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001668 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001669 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001670 readq_T *last_node;
1671 readq_T *n;
1672 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001674 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001675
Bram Moolenaar77073442016-02-13 23:23:53 +01001676 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001677 return FAIL;
1678
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001679 last_node = node->rq_next;
1680 len = node->rq_buflen + last_node->rq_buflen + 1;
1681 if (want_nl)
1682 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001683 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001684 {
1685 last_node = last_node->rq_next;
1686 len += last_node->rq_buflen;
1687 }
1688
1689 p = newbuf = alloc(len);
1690 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001691 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001692 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001693 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001694 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001695 node->rq_buffer = newbuf;
1696 for (n = node; n != last_node; )
1697 {
1698 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001699 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001700 p += n->rq_buflen;
1701 vim_free(n->rq_buffer);
1702 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001703 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001704
1705 /* dispose of the collapsed nodes and their buffers */
1706 for (n = node->rq_next; n != last_node; )
1707 {
1708 n = n->rq_next;
1709 vim_free(n->rq_prev);
1710 }
1711 node->rq_next = last_node->rq_next;
1712 if (last_node->rq_next == NULL)
1713 head->rq_prev = node;
1714 else
1715 last_node->rq_next->rq_prev = node;
1716 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001717 return OK;
1718}
1719
1720/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001721 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001722 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001723 * Returns OK or FAIL.
1724 */
1725 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001726channel_save(channel_T *channel, int part, char_u *buf, int len,
1727 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001728{
1729 readq_T *node;
1730 readq_T *head = &channel->ch_part[part].ch_head;
1731 char_u *p;
1732 int i;
1733
1734 node = (readq_T *)alloc(sizeof(readq_T));
1735 if (node == NULL)
1736 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001737 /* A NUL is added at the end, because netbeans code expects that.
1738 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001739 node->rq_buffer = alloc(len + 1);
1740 if (node->rq_buffer == NULL)
1741 {
1742 vim_free(node);
1743 return FAIL; /* out of memory */
1744 }
1745
1746 if (channel->ch_part[part].ch_mode == MODE_NL)
1747 {
1748 /* Drop any CR before a NL. */
1749 p = node->rq_buffer;
1750 for (i = 0; i < len; ++i)
1751 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1752 *p++ = buf[i];
1753 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001754 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001755 }
1756 else
1757 {
1758 mch_memmove(node->rq_buffer, buf, len);
1759 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001760 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001761 }
1762
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001763 if (prepend)
1764 {
1765 /* preend node to the head of the queue */
1766 node->rq_next = head->rq_next;
1767 node->rq_prev = NULL;
1768 if (head->rq_next == NULL)
1769 head->rq_prev = node;
1770 else
1771 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001772 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001773 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001774 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001775 {
1776 /* append node to the tail of the queue */
1777 node->rq_next = NULL;
1778 node->rq_prev = head->rq_prev;
1779 if (head->rq_prev == NULL)
1780 head->rq_next = node;
1781 else
1782 head->rq_prev->rq_next = node;
1783 head->rq_prev = node;
1784 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001785
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001786 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001787 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001788 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001789 fprintf(log_fd, "'");
1790 if (fwrite(buf, len, 1, log_fd) != 1)
1791 return FAIL;
1792 fprintf(log_fd, "'\n");
1793 }
1794 return OK;
1795}
1796
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001797 static int
1798channel_fill(js_read_T *reader)
1799{
1800 channel_T *channel = (channel_T *)reader->js_cookie;
1801 int part = reader->js_cookie_arg;
1802 char_u *next = channel_get(channel, part);
1803 int unused;
1804 int len;
1805 char_u *p;
1806
1807 if (next == NULL)
1808 return FALSE;
1809
1810 unused = reader->js_end - reader->js_buf - reader->js_used;
1811 if (unused > 0)
1812 {
1813 /* Prepend unused text. */
1814 len = (int)STRLEN(next);
1815 p = alloc(unused + len + 1);
1816 if (p == NULL)
1817 {
1818 vim_free(next);
1819 return FALSE;
1820 }
1821 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1822 mch_memmove(p + unused, next, len + 1);
1823 vim_free(next);
1824 next = p;
1825 }
1826
1827 vim_free(reader->js_buf);
1828 reader->js_buf = next;
1829 reader->js_used = 0;
1830 return TRUE;
1831}
1832
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001833/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001834 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001835 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001836 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001837 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001838 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001839channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001840{
1841 js_read_T reader;
1842 typval_T listtv;
1843 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001844 chanpart_T *chanpart = &channel->ch_part[part];
1845 jsonq_T *head = &chanpart->ch_json_head;
1846 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001847 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001848
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001849 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001850 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001851
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001852 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001853 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001854 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001855 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001856 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001857
1858 /* When a message is incomplete we wait for a short while for more to
1859 * arrive. After the delay drop the input, otherwise a truncated string
1860 * or list will make us hang. */
1861 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001862 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001863 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001864 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001865 /* Only accept the response when it is a list with at least two
1866 * items. */
1867 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001868 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001869 if (listtv.v_type != VAR_LIST)
1870 ch_error(channel, "Did not receive a list, discarding");
1871 else
1872 ch_errorn(channel, "Expected list with two items, got %d",
1873 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001874 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001875 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001876 else
1877 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001878 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1879 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001880 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001881 else
1882 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001883 item->jq_value = alloc_tv();
1884 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001885 {
1886 vim_free(item);
1887 clear_tv(&listtv);
1888 }
1889 else
1890 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001891 *item->jq_value = listtv;
1892 item->jq_prev = head->jq_prev;
1893 head->jq_prev = item;
1894 item->jq_next = NULL;
1895 if (item->jq_prev == NULL)
1896 head->jq_next = item;
1897 else
1898 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001899 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001900 }
1901 }
1902 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001903
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001904 if (status == OK)
1905 chanpart->ch_waiting = FALSE;
1906 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001907 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001908 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001909 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001910 /* First time encountering incomplete message, set a deadline of
1911 * 100 msec. */
1912 ch_log(channel, "Incomplete message - wait for more");
1913 reader.js_used = 0;
1914 chanpart->ch_waiting = TRUE;
1915#ifdef WIN32
1916 chanpart->ch_deadline = GetTickCount() + 100L;
1917#else
1918 gettimeofday(&chanpart->ch_deadline, NULL);
1919 chanpart->ch_deadline.tv_usec += 100 * 1000;
1920 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1921 {
1922 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1923 ++chanpart->ch_deadline.tv_sec;
1924 }
1925#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001926 }
1927 else
1928 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001929 int timeout;
1930#ifdef WIN32
1931 timeout = GetTickCount() > chanpart->ch_deadline;
1932#else
1933 {
1934 struct timeval now_tv;
1935
1936 gettimeofday(&now_tv, NULL);
1937 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1938 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1939 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1940 }
1941#endif
1942 if (timeout)
1943 {
1944 status = FAIL;
1945 chanpart->ch_waiting = FALSE;
1946 }
1947 else
1948 {
1949 reader.js_used = 0;
1950 ch_log(channel, "still waiting on incomplete message");
1951 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001952 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001953 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001954
1955 if (status == FAIL)
1956 {
1957 ch_error(channel, "Decoding failed - discarding input");
1958 ret = FALSE;
1959 chanpart->ch_waiting = FALSE;
1960 }
1961 else if (reader.js_buf[reader.js_used] != NUL)
1962 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001963 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001964 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001965 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1966 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001967 ret = status == MAYBE ? FALSE: TRUE;
1968 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001969 else
1970 ret = FALSE;
1971
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001972 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001973 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001974}
1975
1976/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001977 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001978 */
1979 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001980remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001981{
Bram Moolenaar77073442016-02-13 23:23:53 +01001982 if (node->cq_prev == NULL)
1983 head->cq_next = node->cq_next;
1984 else
1985 node->cq_prev->cq_next = node->cq_next;
1986 if (node->cq_next == NULL)
1987 head->cq_prev = node->cq_prev;
1988 else
1989 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001990}
1991
1992/*
1993 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001994 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001995 */
1996 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001997remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001998{
Bram Moolenaar77073442016-02-13 23:23:53 +01001999 if (node->jq_prev == NULL)
2000 head->jq_next = node->jq_next;
2001 else
2002 node->jq_prev->jq_next = node->jq_next;
2003 if (node->jq_next == NULL)
2004 head->jq_prev = node->jq_prev;
2005 else
2006 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002007 vim_free(node);
2008}
2009
2010/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002011 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002012 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002013 * When "id" is zero or negative jut get the first message. But not the one
2014 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002015 * Return OK when found and return the value in "rettv".
2016 * Return FAIL otherwise.
2017 */
2018 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002019channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002020{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002021 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002022 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002023
Bram Moolenaar77073442016-02-13 23:23:53 +01002024 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002025 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002026 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002027 typval_T *tv = &l->lv_first->li_tv;
2028
2029 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002030 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002031 || tv->vval.v_number == 0
2032 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002033 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002034 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002035 if (tv->v_type == VAR_NUMBER)
2036 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002037 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002038 return OK;
2039 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002040 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002041 }
2042 return FAIL;
2043}
2044
Bram Moolenaarece61b02016-02-20 21:39:05 +01002045#define CH_JSON_MAX_ARGS 4
2046
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002047/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002048 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002049 * "argv[0]" is the command string.
2050 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002051 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002052 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002053channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002054{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002055 char_u *cmd = argv[0].vval.v_string;
2056 char_u *arg;
2057 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002058
Bram Moolenaarece61b02016-02-20 21:39:05 +01002059 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002060 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002061 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002062 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002063 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002064 return;
2065 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002066 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002067 if (arg == NULL)
2068 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002069
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002070 if (STRCMP(cmd, "ex") == 0)
2071 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002072 int save_called_emsg = called_emsg;
2073
2074 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002075 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002076 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002077 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002078 --emsg_silent;
2079 if (called_emsg)
2080 ch_logs(channel, "Ex command error: '%s'",
2081 (char *)get_vim_var_str(VV_ERRMSG));
2082 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002083 }
2084 else if (STRCMP(cmd, "normal") == 0)
2085 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002086 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002087
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002088 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002089 ea.arg = arg;
2090 ea.addr_count = 0;
2091 ea.forceit = TRUE; /* no mapping */
2092 ex_normal(&ea);
2093 }
2094 else if (STRCMP(cmd, "redraw") == 0)
2095 {
2096 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002097
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002098 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002099 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002100 ex_redraw(&ea);
2101 showruler(FALSE);
2102 setcursor();
2103 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002104#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002105 if (gui.in_use)
2106 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002107 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002108 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002109 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002110#endif
2111 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002112 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002113 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002114 int is_call = cmd[0] == 'c';
2115 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002116
Bram Moolenaarece61b02016-02-20 21:39:05 +01002117 if (argv[id_idx].v_type != VAR_UNKNOWN
2118 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002119 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002120 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002121 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002122 EMSG("E904: last argument for expr/call must be a number");
2123 }
2124 else if (is_call && argv[2].v_type != VAR_LIST)
2125 {
2126 ch_error(channel, "third argument for call must be a list");
2127 if (p_verbose > 2)
2128 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002129 }
2130 else
2131 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002132 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002133 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002134 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002135 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002136
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002137 /* Don't pollute the display with errors. */
2138 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002139 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002140 {
2141 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002142 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002143 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002144 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002145 {
2146 ch_logs(channel, "Calling '%s'", (char *)arg);
2147 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2148 tv = &res_tv;
2149 else
2150 tv = NULL;
2151 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002152
2153 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002154 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002155 int id = argv[id_idx].vval.v_number;
2156
Bram Moolenaar55fab432016-02-07 16:53:13 +01002157 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002158 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002159 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002160 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002161 /* If evaluation failed or the result can't be encoded
2162 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002163 vim_free(json);
2164 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002165 err_tv.v_type = VAR_STRING;
2166 err_tv.vval.v_string = (char_u *)"ERROR";
2167 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002168 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002169 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002170 if (json != NULL)
2171 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002172 channel_send(channel,
2173 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002174 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002175 vim_free(json);
2176 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002177 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002178 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002179 if (tv == &res_tv)
2180 clear_tv(tv);
2181 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002182 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002183 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002184 }
2185 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002186 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002187 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002188 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002189 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002190}
2191
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002192/*
2193 * Invoke the callback at "cbhead".
2194 * Does not redraw but sets channel_need_redraw.
2195 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002196 static void
2197invoke_one_time_callback(
2198 channel_T *channel,
2199 cbq_T *cbhead,
2200 cbq_T *item,
2201 typval_T *argv)
2202{
2203 ch_logs(channel, "Invoking one-time callback %s",
2204 (char *)item->cq_callback);
2205 /* Remove the item from the list first, if the callback
2206 * invokes ch_close() the list will be cleared. */
2207 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002208 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002209 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002210 vim_free(item);
2211}
2212
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002213 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002214append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002215{
2216 buf_T *save_curbuf = curbuf;
2217 linenr_T lnum = buffer->b_ml.ml_line_count;
2218 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002219 chanpart_T *ch_part = &channel->ch_part[part];
2220 int save_p_ma = buffer->b_p_ma;
2221
2222 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2223 {
2224 if (!ch_part->ch_nomod_error)
2225 {
2226 ch_error(channel, "Buffer is not modifiable, cannot append");
2227 ch_part->ch_nomod_error = TRUE;
2228 }
2229 return;
2230 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002231
2232 /* If the buffer is also used as input insert above the last
2233 * line. Don't write these lines. */
2234 if (save_write_to)
2235 {
2236 --lnum;
2237 buffer->b_write_to_channel = FALSE;
2238 }
2239
2240 /* Append to the buffer */
2241 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2242
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002243 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002244 curbuf = buffer;
2245 u_sync(TRUE);
2246 /* ignore undo failure, undo is not very useful here */
2247 ignored = u_save(lnum, lnum + 1);
2248
2249 ml_append(lnum, msg, 0, FALSE);
2250 appended_lines_mark(lnum, 1L);
2251 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002252 if (ch_part->ch_nomodifiable)
2253 buffer->b_p_ma = FALSE;
2254 else
2255 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002256
2257 if (buffer->b_nwindows > 0)
2258 {
2259 win_T *wp;
2260 win_T *save_curwin;
2261
2262 FOR_ALL_WINDOWS(wp)
2263 {
2264 if (wp->w_buffer == buffer
2265 && (save_write_to
2266 ? wp->w_cursor.lnum == lnum + 1
2267 : (wp->w_cursor.lnum == lnum
2268 && wp->w_cursor.col == 0)))
2269 {
2270 ++wp->w_cursor.lnum;
2271 save_curwin = curwin;
2272 curwin = wp;
2273 curbuf = curwin->w_buffer;
2274 scroll_cursor_bot(0, FALSE);
2275 curwin = save_curwin;
2276 curbuf = curwin->w_buffer;
2277 }
2278 }
2279 redraw_buf_later(buffer, VALID);
2280 channel_need_redraw = TRUE;
2281 }
2282
2283 if (save_write_to)
2284 {
2285 channel_T *ch;
2286
2287 /* Find channels reading from this buffer and adjust their
2288 * next-to-read line number. */
2289 buffer->b_write_to_channel = TRUE;
2290 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2291 {
2292 chanpart_T *in_part = &ch->ch_part[PART_IN];
2293
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002294 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002295 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2296 }
2297 }
2298}
2299
Bram Moolenaar437905c2016-04-26 19:01:05 +02002300 static void
2301drop_messages(channel_T *channel, int part)
2302{
2303 char_u *msg;
2304
2305 while ((msg = channel_get(channel, part)) != NULL)
2306 {
2307 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2308 vim_free(msg);
2309 }
2310}
2311
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002312/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002313 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002314 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002315 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002316 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002317 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002318may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002319{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002320 char_u *msg = NULL;
2321 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002322 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002323 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002324 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002325 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002326 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002327 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002328 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002329 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002330 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002331
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002332 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002333 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002334 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002335
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002336 /* Use a message-specific callback, part callback or channel callback */
2337 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2338 if (cbitem->cq_seq_nr == 0)
2339 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002340 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002341 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002342 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002343 partial = cbitem->cq_partial;
2344 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002345 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002346 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002347 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002348 partial = channel->ch_part[part].ch_partial;
2349 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002350 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002351 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002352 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002353 partial = channel->ch_partial;
2354 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002355
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002356 buffer = channel->ch_part[part].ch_bufref.br_buf;
2357 if (buffer != NULL && !bufref_valid(&channel->ch_part[part].ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002358 {
2359 /* buffer was wiped out */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002360 channel->ch_part[part].ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002361 buffer = NULL;
2362 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002363
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002364 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002365 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002366 listitem_T *item;
2367 int argc = 0;
2368
Bram Moolenaard7ece102016-02-02 23:23:02 +01002369 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002370 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002371 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002372 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002373 channel_parse_json(channel, part);
2374 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002375 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002376 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002377
Bram Moolenaarece61b02016-02-20 21:39:05 +01002378 for (item = listtv->vval.v_list->lv_first;
2379 item != NULL && argc < CH_JSON_MAX_ARGS;
2380 item = item->li_next)
2381 argv[argc++] = item->li_tv;
2382 while (argc < CH_JSON_MAX_ARGS)
2383 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002384
Bram Moolenaarece61b02016-02-20 21:39:05 +01002385 if (argv[0].v_type == VAR_STRING)
2386 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002387 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002388 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002389 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002390 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002391 }
2392
Bram Moolenaarece61b02016-02-20 21:39:05 +01002393 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002394 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002395 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002396 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002397 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002398 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002399 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002400 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002401 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002402 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002403 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002404 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002405 return FALSE;
2406 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002407 else
2408 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002409 /* If there is no callback or buffer drop the message. */
2410 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002411 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002412 /* If there is a close callback it may use ch_read() to get the
2413 * messages. */
2414 if (channel->ch_close_cb == NULL)
2415 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002416 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002417 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002418
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002419 if (ch_mode == MODE_NL)
2420 {
2421 char_u *nl;
2422 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002423 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002424
2425 /* See if we have a message ending in NL in the first buffer. If
2426 * not try to concatenate the first and the second buffer. */
2427 while (TRUE)
2428 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002429 node = channel_peek(channel, part);
2430 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002431 if (nl != NULL)
2432 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002433 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002434 return FALSE; /* incomplete message */
2435 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002436 buf = node->rq_buffer;
2437
2438 /* Convert NUL to NL, the internal representation. */
2439 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2440 if (*p == NUL)
2441 *p = NL;
2442
2443 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002444 {
2445 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002446 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002447 *nl = NUL;
2448 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002449 else
2450 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002451 /* Copy the message into allocated memory (excluding the NL)
2452 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002453 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002454 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002455 }
2456 }
2457 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002458 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002459 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002460 * get everything we have.
2461 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002462 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002463 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002464
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002465 if (msg == NULL)
2466 return FALSE; /* out of memory (and avoids Coverity warning) */
2467
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002468 argv[1].v_type = VAR_STRING;
2469 argv[1].vval.v_string = msg;
2470 }
2471
Bram Moolenaara07fec92016-02-05 21:04:08 +01002472 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002473 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002474 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002475
2476 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002477 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002478 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002479 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002480 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002481 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002482 break;
2483 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002484 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002485 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002486 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002487 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002488 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002489 if (buffer != NULL)
2490 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002491 if (msg == NULL)
2492 /* JSON or JS mode: re-encode the message. */
2493 msg = json_encode(listtv, ch_mode);
2494 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002495 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002496 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002497
Bram Moolenaar187db502016-02-27 14:44:26 +01002498 if (callback != NULL)
2499 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002500 if (cbitem != NULL)
2501 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2502 else
2503 {
2504 /* invoke the channel callback */
2505 ch_logs(channel, "Invoking channel callback %s",
2506 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002507 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002508 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002509 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002510 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002511 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002512 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002513
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002514 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002515 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002516 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002517
2518 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002519}
2520
2521/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002522 * Return TRUE when channel "channel" is open for writing to.
2523 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002524 */
2525 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002526channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002527{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002528 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002529 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002530}
2531
2532/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002533 * Return TRUE when channel "channel" is open for reading or writing.
2534 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002535 */
2536 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002537channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002538{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002539 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002540 || channel->CH_IN_FD != INVALID_FD
2541 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002542 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002543}
2544
2545/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002546 * Return TRUE if "channel" has JSON or other typeahead.
2547 */
2548 static int
2549channel_has_readahead(channel_T *channel, int part)
2550{
2551 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2552
2553 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2554 {
2555 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2556 jsonq_T *item = head->jq_next;
2557
2558 return item != NULL;
2559 }
2560 return channel_peek(channel, part) != NULL;
2561}
2562
2563/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002564 * Return a string indicating the status of the channel.
2565 */
2566 char *
2567channel_status(channel_T *channel)
2568{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002569 int part;
2570 int has_readahead = FALSE;
2571
Bram Moolenaar77073442016-02-13 23:23:53 +01002572 if (channel == NULL)
2573 return "fail";
2574 if (channel_is_open(channel))
2575 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002576 for (part = PART_SOCK; part <= PART_ERR; ++part)
2577 if (channel_has_readahead(channel, part))
2578 {
2579 has_readahead = TRUE;
2580 break;
2581 }
2582
2583 if (has_readahead)
2584 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002585 return "closed";
2586}
2587
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002588 static void
2589channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2590{
2591 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002592 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002593 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002594 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002595
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002596 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002597 STRCAT(namebuf, "_");
2598 tail = STRLEN(namebuf);
2599
2600 STRCPY(namebuf + tail, "status");
2601 dict_add_nr_str(dict, namebuf, 0,
2602 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2603
2604 STRCPY(namebuf + tail, "mode");
2605 switch (chanpart->ch_mode)
2606 {
2607 case MODE_NL: s = "NL"; break;
2608 case MODE_RAW: s = "RAW"; break;
2609 case MODE_JSON: s = "JSON"; break;
2610 case MODE_JS: s = "JS"; break;
2611 }
2612 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2613
2614 STRCPY(namebuf + tail, "io");
2615 if (part == PART_SOCK)
2616 s = "socket";
2617 else switch (chanpart->ch_io)
2618 {
2619 case JIO_NULL: s = "null"; break;
2620 case JIO_PIPE: s = "pipe"; break;
2621 case JIO_FILE: s = "file"; break;
2622 case JIO_BUFFER: s = "buffer"; break;
2623 case JIO_OUT: s = "out"; break;
2624 }
2625 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2626
2627 STRCPY(namebuf + tail, "timeout");
2628 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2629}
2630
2631 void
2632channel_info(channel_T *channel, dict_T *dict)
2633{
2634 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2635 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2636
2637 if (channel->ch_hostname != NULL)
2638 {
2639 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2640 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2641 channel_part_info(channel, dict, "sock", PART_SOCK);
2642 }
2643 else
2644 {
2645 channel_part_info(channel, dict, "out", PART_OUT);
2646 channel_part_info(channel, dict, "err", PART_ERR);
2647 channel_part_info(channel, dict, "in", PART_IN);
2648 }
2649}
2650
Bram Moolenaar77073442016-02-13 23:23:53 +01002651/*
2652 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002653 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002654 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002655 */
2656 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002657channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002658{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002659 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002660
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002661#ifdef FEAT_GUI
2662 channel_gui_unregister(channel);
2663#endif
2664
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002665 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002666 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002667 sock_close(channel->CH_SOCK_FD);
2668 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002669 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002670 may_close_part(&channel->CH_IN_FD);
2671 may_close_part(&channel->CH_OUT_FD);
2672 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002673
Bram Moolenaar8b374212016-02-24 20:43:06 +01002674 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002675 {
2676 typval_T argv[1];
2677 typval_T rettv;
2678 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002679 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002680
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002681 /* Invoke callbacks before the close callback, since it's weird to
2682 * first invoke the close callback. Increment the refcount to avoid
2683 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002684 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002685 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002686 for (part = PART_SOCK; part <= PART_ERR; ++part)
2687 while (may_invoke_callback(channel, part))
2688 ;
2689
2690 /* Invoke the close callback, if still set. */
2691 if (channel->ch_close_cb != NULL)
2692 {
2693 ch_logs(channel, "Invoking close callback %s",
2694 (char *)channel->ch_close_cb);
2695 argv[0].v_type = VAR_CHANNEL;
2696 argv[0].vval.v_channel = channel;
2697 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002698 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2699 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002700 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002701 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002702 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002703
2704 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002705 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002706 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002707 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002708
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002709 --channel->ch_refcount;
2710
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002711 if (channel_need_redraw)
2712 {
2713 channel_need_redraw = FALSE;
2714 redraw_after_callback();
2715 }
2716
Bram Moolenaar437905c2016-04-26 19:01:05 +02002717 /* any remaining messages are useless now */
2718 for (part = PART_SOCK; part <= PART_ERR; ++part)
2719 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002720 }
2721
2722 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002723}
2724
Bram Moolenaard04a0202016-01-26 23:30:18 +01002725/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002726 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002727 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002728 static void
2729channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002730{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002731 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2732 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002733
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002734 while (channel_peek(channel, part) != NULL)
2735 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002736
2737 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002738 {
2739 cbq_T *node = cb_head->cq_next;
2740
2741 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002742 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002743 vim_free(node);
2744 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002745
2746 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002747 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002748 free_tv(json_head->jq_next->jq_value);
2749 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002750 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002751
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002752 free_callback(channel->ch_part[part].ch_callback,
2753 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002754 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002755 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002756}
2757
2758/*
2759 * Clear all the read buffers on "channel".
2760 */
2761 void
2762channel_clear(channel_T *channel)
2763{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002764 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002765 vim_free(channel->ch_hostname);
2766 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002767 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002768 channel_clear_one(channel, PART_OUT);
2769 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002770 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002771 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002772 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002773 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002774 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002775 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002776 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002777}
2778
Bram Moolenaar77073442016-02-13 23:23:53 +01002779#if defined(EXITFREE) || defined(PROTO)
2780 void
2781channel_free_all(void)
2782{
2783 channel_T *channel;
2784
Bram Moolenaard6051b52016-02-28 15:49:03 +01002785 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002786 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2787 channel_clear(channel);
2788}
2789#endif
2790
2791
Bram Moolenaar715d2852016-04-30 17:06:31 +02002792/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002793#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002794
2795/* Buffer size for reading incoming messages. */
2796#define MAXMSGSIZE 4096
2797
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002798#if defined(HAVE_SELECT)
2799/*
2800 * Add write fds where we are waiting for writing to be possible.
2801 */
2802 static int
2803channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2804{
2805 int maxfd = maxfd_arg;
2806 channel_T *ch;
2807
2808 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2809 {
2810 chanpart_T *in_part = &ch->ch_part[PART_IN];
2811
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002812 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002813 {
2814 FD_SET((int)in_part->ch_fd, wfds);
2815 if ((int)in_part->ch_fd >= maxfd)
2816 maxfd = (int)in_part->ch_fd + 1;
2817 }
2818 }
2819 return maxfd;
2820}
2821#else
2822/*
2823 * Add write fds where we are waiting for writing to be possible.
2824 */
2825 static int
2826channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2827{
2828 int nfd = nfd_in;
2829 channel_T *ch;
2830
2831 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2832 {
2833 chanpart_T *in_part = &ch->ch_part[PART_IN];
2834
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002835 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002836 {
2837 in_part->ch_poll_idx = nfd;
2838 fds[nfd].fd = in_part->ch_fd;
2839 fds[nfd].events = POLLOUT;
2840 ++nfd;
2841 }
2842 else
2843 in_part->ch_poll_idx = -1;
2844 }
2845 return nfd;
2846}
2847#endif
2848
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002849typedef enum {
2850 CW_READY,
2851 CW_NOT_READY,
2852 CW_ERROR
2853} channel_wait_result;
2854
Bram Moolenaard04a0202016-01-26 23:30:18 +01002855/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002856 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002857 * Return CW_READY when there is something to read.
2858 * Return CW_NOT_READY when there is nothing to read.
2859 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002860 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002861 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002862channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002863{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002864 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002865 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002866
Bram Moolenaard8070362016-02-15 21:56:54 +01002867# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002868 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002869 {
2870 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002871 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002872 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002873 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002874
2875 /* reading from a pipe, not a socket */
2876 while (TRUE)
2877 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002878 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2879
2880 if (r && nread > 0)
2881 return CW_READY;
2882 if (r == 0)
2883 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002884
2885 /* perhaps write some buffer lines */
2886 channel_write_any_lines();
2887
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002888 sleep_time = deadline - GetTickCount();
2889 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002890 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002891 /* Wait for a little while. Very short at first, up to 10 msec
2892 * after looping a few times. */
2893 if (sleep_time > delay)
2894 sleep_time = delay;
2895 Sleep(sleep_time);
2896 delay = delay * 2;
2897 if (delay > 10)
2898 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002899 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002900 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002901 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002902#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002903 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002904#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002905 struct timeval tval;
2906 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002907 fd_set wfds;
2908 int ret;
2909 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002910
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002911 tval.tv_sec = timeout / 1000;
2912 tval.tv_usec = (timeout % 1000) * 1000;
2913 for (;;)
2914 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002915 FD_ZERO(&rfds);
2916 FD_SET((int)fd, &rfds);
2917
2918 /* Write lines to a pipe when a pipe can be written to. Need to
2919 * set this every time, some buffers may be done. */
2920 maxfd = (int)fd + 1;
2921 FD_ZERO(&wfds);
2922 maxfd = channel_fill_wfds(maxfd, &wfds);
2923
2924 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002925# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002926 SOCK_ERRNO;
2927 if (ret == -1 && errno == EINTR)
2928 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002929# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002930 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002931 {
2932 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002933 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002934 channel_write_any_lines();
2935 continue;
2936 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002937 break;
2938 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002939#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002940 for (;;)
2941 {
2942 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2943 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002944
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002945 fds[0].fd = fd;
2946 fds[0].events = POLLIN;
2947 nfd = channel_fill_poll_write(nfd, fds);
2948 if (poll(fds, nfd, timeout) > 0)
2949 {
2950 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002951 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002952 channel_write_any_lines();
2953 continue;
2954 }
2955 break;
2956 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002957#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002958 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002959 return CW_NOT_READY;
2960}
2961
2962 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002963channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002964{
2965 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002966 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2967 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002968
2969 /* Queue a "DETACH" netbeans message in the command queue in order to
2970 * terminate the netbeans session later. Do not end the session here
2971 * directly as we may be running in the context of a call to
2972 * netbeans_parse_messages():
2973 * netbeans_parse_messages
2974 * -> autocmd triggered while processing the netbeans cmd
2975 * -> ui_breakcheck
2976 * -> gui event loop or select loop
2977 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002978 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002979 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002980 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002981 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002982 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2983
2984 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002985 * died. Don't close the channel right away, it may be the wrong moment
2986 * to invoke callbacks. */
2987 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02002988
2989#ifdef FEAT_GUI
2990 /* Stop listening to GUI events right away. */
2991 channel_gui_unregister(channel);
2992#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002993}
2994
2995 static void
2996channel_close_now(channel_T *channel)
2997{
2998 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002999 channel_close(channel, TRUE);
3000 if (channel->ch_nb_close_cb != NULL)
3001 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003002}
3003
3004/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003005 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003006 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003007 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003008 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003009 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003010channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003011{
3012 static char_u *buf = NULL;
3013 int len = 0;
3014 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003015 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003016 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003017
Bram Moolenaar5850a762016-05-27 19:59:48 +02003018 /* If we detected a read error don't try reading again. */
3019 if (channel->ch_to_be_closed)
3020 return;
3021
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003022 fd = channel->ch_part[part].ch_fd;
3023 if (fd == INVALID_FD)
3024 {
3025 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003026 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003027 }
3028 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003029
3030 /* Allocate a buffer to read into. */
3031 if (buf == NULL)
3032 {
3033 buf = alloc(MAXMSGSIZE);
3034 if (buf == NULL)
3035 return; /* out of memory! */
3036 }
3037
3038 /* Keep on reading for as long as there is something to read.
3039 * Use select() or poll() to avoid blocking on a message that is exactly
3040 * MAXMSGSIZE long. */
3041 for (;;)
3042 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003043 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003044 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003045 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003046 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003047 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003048 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003049 if (len <= 0)
3050 break; /* error or nothing more to read */
3051
3052 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003053 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003054 readlen += len;
3055 if (len < MAXMSGSIZE)
3056 break; /* did read everything that's available */
3057 }
3058
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003059 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003060 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003061 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003062
3063#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003064 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003065 if (CH_HAS_GUI && gtk_main_level() > 0)
3066 gtk_main_quit();
3067#endif
3068}
3069
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003070/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003071 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003072 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003073 * Returns what was read in allocated memory.
3074 * Returns NULL in case of error or timeout.
3075 */
3076 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003077channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003078{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003079 char_u *buf;
3080 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003081 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003082 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003083 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003084 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003085
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003086 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003087 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003088
3089 while (TRUE)
3090 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003091 node = channel_peek(channel, part);
3092 if (node != NULL)
3093 {
3094 if (mode == MODE_RAW || (mode == MODE_NL
3095 && channel_first_nl(node) != NULL))
3096 /* got a complete message */
3097 break;
3098 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3099 continue;
3100 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003101
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003102 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003103 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003104 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003105 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003106 {
3107 ch_log(channel, "Timed out");
3108 return NULL;
3109 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003110 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003111 }
3112
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003113 if (mode == MODE_RAW)
3114 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003115 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003116 }
3117 else
3118 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003119 char_u *p;
3120
3121 buf = node->rq_buffer;
3122 nl = channel_first_nl(node);
3123
3124 /* Convert NUL to NL, the internal representation. */
3125 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3126 if (*p == NUL)
3127 *p = NL;
3128
3129 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003130 {
3131 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003132 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003133 *nl = NUL;
3134 }
3135 else
3136 {
3137 /* Copy the message into allocated memory and remove it from the
3138 * buffer. */
3139 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003140 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003141 }
3142 }
3143 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003144 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003145 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003146}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003147
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003148/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003149 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003150 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003151 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003152 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003153 */
3154 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003155channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003156 channel_T *channel,
3157 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003158 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003159 int id,
3160 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003161{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003162 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003163 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003164 int timeout;
3165 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003166
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003167 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003168 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003169 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003170 for (;;)
3171 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003172 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003173
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003174 /* search for message "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003175 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003176 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003177 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003178 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003179 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003180
Bram Moolenaard7ece102016-02-02 23:23:02 +01003181 if (!more)
3182 {
3183 /* Handle any other messages in the queue. If done some more
3184 * messages may have arrived. */
3185 if (channel_parse_messages())
3186 continue;
3187
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003188 /* Wait for up to the timeout. If there was an incomplete message
3189 * use the deadline for that. */
3190 timeout = timeout_arg;
3191 if (chanpart->ch_waiting)
3192 {
3193#ifdef WIN32
3194 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3195#else
3196 {
3197 struct timeval now_tv;
3198
3199 gettimeofday(&now_tv, NULL);
3200 timeout = (chanpart->ch_deadline.tv_sec
3201 - now_tv.tv_sec) * 1000
3202 + (chanpart->ch_deadline.tv_usec
3203 - now_tv.tv_usec) / 1000
3204 + 1;
3205 }
3206#endif
3207 if (timeout < 0)
3208 {
3209 /* Something went wrong, channel_parse_json() didn't
3210 * discard message. Cancel waiting. */
3211 chanpart->ch_waiting = FALSE;
3212 timeout = timeout_arg;
3213 }
3214 else if (timeout > timeout_arg)
3215 timeout = timeout_arg;
3216 }
3217 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003218 if (fd == INVALID_FD
3219 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003220 {
3221 if (timeout == timeout_arg)
3222 {
3223 if (fd != INVALID_FD)
3224 ch_log(channel, "Timed out");
3225 break;
3226 }
3227 }
3228 else
3229 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003230 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003231 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003232 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003233 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003234}
3235
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003236/*
3237 * Common for ch_read() and ch_readraw().
3238 */
3239 void
3240common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3241{
3242 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003243 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003244 jobopt_T opt;
3245 int mode;
3246 int timeout;
3247 int id = -1;
3248 typval_T *listtv = NULL;
3249
3250 /* return an empty string by default */
3251 rettv->v_type = VAR_STRING;
3252 rettv->vval.v_string = NULL;
3253
3254 clear_job_options(&opt);
3255 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3256 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003257 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003258
Bram Moolenaar437905c2016-04-26 19:01:05 +02003259 if (opt.jo_set & JO_PART)
3260 part = opt.jo_part;
3261 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003262 if (channel != NULL)
3263 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003264 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003265 part = channel_part_read(channel);
3266 mode = channel_get_mode(channel, part);
3267 timeout = channel_get_timeout(channel, part);
3268 if (opt.jo_set & JO_TIMEOUT)
3269 timeout = opt.jo_timeout;
3270
3271 if (raw || mode == MODE_RAW || mode == MODE_NL)
3272 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3273 else
3274 {
3275 if (opt.jo_set & JO_ID)
3276 id = opt.jo_id;
3277 channel_read_json_block(channel, part, timeout, id, &listtv);
3278 if (listtv != NULL)
3279 {
3280 *rettv = *listtv;
3281 vim_free(listtv);
3282 }
3283 else
3284 {
3285 rettv->v_type = VAR_SPECIAL;
3286 rettv->vval.v_number = VVAL_NONE;
3287 }
3288 }
3289 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003290
3291theend:
3292 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003293}
3294
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003295# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3296 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003297/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003298 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003299 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003300 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003301 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003302channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003303{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003304 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003305 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003306
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003307 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003308 for (channel = first_channel; channel != NULL;
3309 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003310 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003311 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003312 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003313 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003314 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003315 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003316 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003317 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003318 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003319}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003320# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003321
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003322# if defined(WIN32) || defined(PROTO)
3323/*
3324 * Check the channels for anything that is ready to be read.
3325 * The data is put in the read queue.
3326 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003327 void
3328channel_handle_events(void)
3329{
3330 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003331 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003332 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003333
3334 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3335 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003336 /* If we detected a read error don't try reading again. */
3337 if (channel->ch_to_be_closed)
3338 continue;
3339
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003340 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003341 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003342 {
3343 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003344 if (fd != INVALID_FD)
3345 {
3346 int r = channel_wait(channel, fd, 0);
3347
3348 if (r == CW_READY)
3349 channel_read(channel, part, "channel_handle_events");
3350 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003351 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003352 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003353 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003354 }
3355}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003356# endif
3357
Bram Moolenaard04a0202016-01-26 23:30:18 +01003358/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003359 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003360 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003361 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003362 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003363 int
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003364channel_send(channel_T *channel, int part, char_u *buf, int len, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003365{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003366 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003367 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003368
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003369 fd = channel->ch_part[part].ch_fd;
3370 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003371 {
3372 if (!channel->ch_error && fun != NULL)
3373 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003374 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003375 EMSG2("E630: %s(): write while not connected", fun);
3376 }
3377 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003378 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003379 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003380
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003381 if (log_fd != NULL)
3382 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003383 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003384 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003385 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003386 fprintf(log_fd, "'\n");
3387 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003388 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003389 }
3390
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003391 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003392 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003393 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003394 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003395 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003396 {
3397 if (!channel->ch_error && fun != NULL)
3398 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003399 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003400 EMSG2("E631: %s(): write failed", fun);
3401 }
3402 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003403 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003404 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003405
3406 channel->ch_error = FALSE;
3407 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003408}
3409
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003410/*
3411 * Common for "ch_sendexpr()" and "ch_sendraw()".
3412 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003413 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003414 * Otherwise returns NULL.
3415 */
3416 channel_T *
3417send_common(
3418 typval_T *argvars,
3419 char_u *text,
3420 int id,
3421 int eval,
3422 jobopt_T *opt,
3423 char *fun,
3424 int *part_read)
3425{
3426 channel_T *channel;
3427 int part_send;
3428
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003429 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003430 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003431 if (channel == NULL)
3432 return NULL;
3433 part_send = channel_part_send(channel);
3434 *part_read = channel_part_read(channel);
3435
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003436 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3437 return NULL;
3438
3439 /* Set the callback. An empty callback means no callback and not reading
3440 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3441 * allowed. */
3442 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3443 {
3444 if (eval)
3445 {
3446 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3447 return NULL;
3448 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003449 channel_set_req_callback(channel, part_send,
3450 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003451 }
3452
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003453 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003454 && opt->jo_callback == NULL)
3455 return channel;
3456 return NULL;
3457}
3458
3459/*
3460 * common for "ch_evalexpr()" and "ch_sendexpr()"
3461 */
3462 void
3463ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3464{
3465 char_u *text;
3466 typval_T *listtv;
3467 channel_T *channel;
3468 int id;
3469 ch_mode_T ch_mode;
3470 int part_send;
3471 int part_read;
3472 jobopt_T opt;
3473 int timeout;
3474
3475 /* return an empty string by default */
3476 rettv->v_type = VAR_STRING;
3477 rettv->vval.v_string = NULL;
3478
Bram Moolenaar437905c2016-04-26 19:01:05 +02003479 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003480 if (channel == NULL)
3481 return;
3482 part_send = channel_part_send(channel);
3483
3484 ch_mode = channel_get_mode(channel, part_send);
3485 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3486 {
3487 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3488 return;
3489 }
3490
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003491 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003492 text = json_encode_nr_expr(id, &argvars[1],
3493 ch_mode == MODE_JS ? JSON_JS : 0);
3494 if (text == NULL)
3495 return;
3496
3497 channel = send_common(argvars, text, id, eval, &opt,
3498 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3499 vim_free(text);
3500 if (channel != NULL && eval)
3501 {
3502 if (opt.jo_set & JO_TIMEOUT)
3503 timeout = opt.jo_timeout;
3504 else
3505 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003506 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3507 == OK)
3508 {
3509 list_T *list = listtv->vval.v_list;
3510
3511 /* Move the item from the list and then change the type to
3512 * avoid the value being freed. */
3513 *rettv = list->lv_last->li_tv;
3514 list->lv_last->li_tv.v_type = VAR_NUMBER;
3515 free_tv(listtv);
3516 }
3517 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003518 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003519}
3520
3521/*
3522 * common for "ch_evalraw()" and "ch_sendraw()"
3523 */
3524 void
3525ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3526{
3527 char_u buf[NUMBUFLEN];
3528 char_u *text;
3529 channel_T *channel;
3530 int part_read;
3531 jobopt_T opt;
3532 int timeout;
3533
3534 /* return an empty string by default */
3535 rettv->v_type = VAR_STRING;
3536 rettv->vval.v_string = NULL;
3537
3538 text = get_tv_string_buf(&argvars[1], buf);
3539 channel = send_common(argvars, text, 0, eval, &opt,
3540 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3541 if (channel != NULL && eval)
3542 {
3543 if (opt.jo_set & JO_TIMEOUT)
3544 timeout = opt.jo_timeout;
3545 else
3546 timeout = channel_get_timeout(channel, part_read);
3547 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3548 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003549 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003550}
3551
Bram Moolenaard04a0202016-01-26 23:30:18 +01003552# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003553/*
3554 * Add open channels to the poll struct.
3555 * Return the adjusted struct index.
3556 * The type of "fds" is hidden to avoid problems with the function proto.
3557 */
3558 int
3559channel_poll_setup(int nfd_in, void *fds_in)
3560{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003561 int nfd = nfd_in;
3562 channel_T *channel;
3563 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003564 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003565
Bram Moolenaar77073442016-02-13 23:23:53 +01003566 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003567 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003568 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003569 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003570 chanpart_T *ch_part = &channel->ch_part[part];
3571
3572 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003573 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003574 ch_part->ch_poll_idx = nfd;
3575 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003576 fds[nfd].events = POLLIN;
3577 nfd++;
3578 }
3579 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003580 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003581 }
3582 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003583
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003584 nfd = channel_fill_poll_write(nfd, fds);
3585
Bram Moolenaare0874f82016-01-24 20:36:41 +01003586 return nfd;
3587}
3588
3589/*
3590 * The type of "fds" is hidden to avoid problems with the function proto.
3591 */
3592 int
3593channel_poll_check(int ret_in, void *fds_in)
3594{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003595 int ret = ret_in;
3596 channel_T *channel;
3597 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003598 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003599 int idx;
3600 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003601
Bram Moolenaar77073442016-02-13 23:23:53 +01003602 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003603 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003604 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003605 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003606 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003607
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003608 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003609 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003610 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003611 --ret;
3612 }
3613 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003614
3615 in_part = &channel->ch_part[PART_IN];
3616 idx = in_part->ch_poll_idx;
3617 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3618 {
3619 if (in_part->ch_buf_append)
3620 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003621 if (in_part->ch_bufref.br_buf != NULL)
3622 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003623 }
3624 else
3625 channel_write_in(channel);
3626 --ret;
3627 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003628 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003629
3630 return ret;
3631}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003632# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003633
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003634# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003635/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003636 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003637 */
3638 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003639channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003640{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003641 int maxfd = maxfd_in;
3642 channel_T *channel;
3643 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003644 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003645 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003646
Bram Moolenaar77073442016-02-13 23:23:53 +01003647 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003648 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003649 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003650 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003651 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003652
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003653 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003654 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003655 FD_SET((int)fd, rfds);
3656 if (maxfd < (int)fd)
3657 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003658 }
3659 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003660 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003661
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003662 maxfd = channel_fill_wfds(maxfd, wfds);
3663
Bram Moolenaare0874f82016-01-24 20:36:41 +01003664 return maxfd;
3665}
3666
3667/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003668 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003669 */
3670 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003671channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003672{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003673 int ret = ret_in;
3674 channel_T *channel;
3675 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003676 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003677 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003678 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003679
Bram Moolenaar77073442016-02-13 23:23:53 +01003680 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003681 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003682 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003683 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003684 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003685
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003686 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003687 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003688 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003689 --ret;
3690 }
3691 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003692
3693 in_part = &channel->ch_part[PART_IN];
3694 if (ret > 0 && in_part->ch_fd != INVALID_FD
3695 && FD_ISSET(in_part->ch_fd, wfds))
3696 {
3697 if (in_part->ch_buf_append)
3698 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003699 if (in_part->ch_bufref.br_buf != NULL)
3700 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003701 }
3702 else
3703 channel_write_in(channel);
3704 --ret;
3705 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003706 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003707
3708 return ret;
3709}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003710# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003711
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003712/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003713 * Execute queued up commands.
3714 * Invoked from the main loop when it's safe to execute received commands.
3715 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003716 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003717 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003718channel_parse_messages(void)
3719{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003720 channel_T *channel = first_channel;
3721 int ret = FALSE;
3722 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003723 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003724
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003725 ++safe_to_invoke_callback;
3726
Bram Moolenaard0b65022016-03-06 21:50:33 +01003727 /* Only do this message when another message was given, otherwise we get
3728 * lots of them. */
3729 if (did_log_msg)
3730 {
3731 ch_log(NULL, "looking for messages on channels");
3732 did_log_msg = FALSE;
3733 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003734 while (channel != NULL)
3735 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003736 if (channel->ch_to_be_closed)
3737 {
3738 channel->ch_to_be_closed = FALSE;
3739 channel_close_now(channel);
3740 /* channel may have been freed, start over */
3741 channel = first_channel;
3742 continue;
3743 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003744 if (channel->ch_to_be_freed)
3745 {
3746 channel_free(channel);
3747 /* channel has been freed, start over */
3748 channel = first_channel;
3749 continue;
3750 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003751 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003752 {
3753 /* channel is no longer useful, free it */
3754 channel_free(channel);
3755 channel = first_channel;
3756 part = PART_SOCK;
3757 continue;
3758 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003759 if (channel->ch_part[part].ch_fd != INVALID_FD
3760 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003761 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003762 /* Increase the refcount, in case the handler causes the channel
3763 * to be unreferenced or closed. */
3764 ++channel->ch_refcount;
3765 r = may_invoke_callback(channel, part);
3766 if (r == OK)
3767 ret = TRUE;
3768 if (channel_unref(channel) || r == OK)
3769 {
3770 /* channel was freed or something was done, start over */
3771 channel = first_channel;
3772 part = PART_SOCK;
3773 continue;
3774 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003775 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003776 if (part < PART_ERR)
3777 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003778 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003779 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003780 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003781 part = PART_SOCK;
3782 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003783 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003784
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003785 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003786 {
3787 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003788 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003789 }
3790
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003791 --safe_to_invoke_callback;
3792
Bram Moolenaard7ece102016-02-02 23:23:02 +01003793 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003794}
3795
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003796/*
3797 * Mark references to lists used in channels.
3798 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003799 int
3800set_ref_in_channel(int copyID)
3801{
Bram Moolenaar77073442016-02-13 23:23:53 +01003802 int abort = FALSE;
3803 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003804 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003805
Bram Moolenaar77073442016-02-13 23:23:53 +01003806 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003807 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003808 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003809 tv.v_type = VAR_CHANNEL;
3810 tv.vval.v_channel = channel;
3811 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003812 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003813 return abort;
3814}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003815
3816/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003817 * Return the "part" to write to for "channel".
3818 */
3819 int
3820channel_part_send(channel_T *channel)
3821{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003822 if (channel->CH_SOCK_FD == INVALID_FD)
3823 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003824 return PART_SOCK;
3825}
3826
3827/*
3828 * Return the default "part" to read from for "channel".
3829 */
3830 int
3831channel_part_read(channel_T *channel)
3832{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003833 if (channel->CH_SOCK_FD == INVALID_FD)
3834 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003835 return PART_SOCK;
3836}
3837
3838/*
3839 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003840 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003841 */
3842 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003843channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003844{
Bram Moolenaar77073442016-02-13 23:23:53 +01003845 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003846 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003847 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003848}
3849
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003850/*
3851 * Return the timeout of "channel"/"part"
3852 */
3853 int
3854channel_get_timeout(channel_T *channel, int part)
3855{
3856 return channel->ch_part[part].ch_timeout;
3857}
3858
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003859 static int
3860handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3861{
3862 char_u *val = get_tv_string(item);
3863
3864 opt->jo_set |= jo;
3865 if (STRCMP(val, "nl") == 0)
3866 *modep = MODE_NL;
3867 else if (STRCMP(val, "raw") == 0)
3868 *modep = MODE_RAW;
3869 else if (STRCMP(val, "js") == 0)
3870 *modep = MODE_JS;
3871 else if (STRCMP(val, "json") == 0)
3872 *modep = MODE_JSON;
3873 else
3874 {
3875 EMSG2(_(e_invarg2), val);
3876 return FAIL;
3877 }
3878 return OK;
3879}
3880
3881 static int
3882handle_io(typval_T *item, int part, jobopt_T *opt)
3883{
3884 char_u *val = get_tv_string(item);
3885
3886 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3887 if (STRCMP(val, "null") == 0)
3888 opt->jo_io[part] = JIO_NULL;
3889 else if (STRCMP(val, "pipe") == 0)
3890 opt->jo_io[part] = JIO_PIPE;
3891 else if (STRCMP(val, "file") == 0)
3892 opt->jo_io[part] = JIO_FILE;
3893 else if (STRCMP(val, "buffer") == 0)
3894 opt->jo_io[part] = JIO_BUFFER;
3895 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3896 opt->jo_io[part] = JIO_OUT;
3897 else
3898 {
3899 EMSG2(_(e_invarg2), val);
3900 return FAIL;
3901 }
3902 return OK;
3903}
3904
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003905/*
3906 * Clear a jobopt_T before using it.
3907 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003908 void
3909clear_job_options(jobopt_T *opt)
3910{
3911 vim_memset(opt, 0, sizeof(jobopt_T));
3912}
3913
3914/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003915 * Free any members of a jobopt_T.
3916 */
3917 void
3918free_job_options(jobopt_T *opt)
3919{
3920 if (opt->jo_partial != NULL)
3921 partial_unref(opt->jo_partial);
3922 if (opt->jo_out_partial != NULL)
3923 partial_unref(opt->jo_out_partial);
3924 if (opt->jo_err_partial != NULL)
3925 partial_unref(opt->jo_err_partial);
3926 if (opt->jo_close_partial != NULL)
3927 partial_unref(opt->jo_close_partial);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003928 if (opt->jo_exit_partial != NULL)
3929 partial_unref(opt->jo_exit_partial);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003930}
3931
3932/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003933 * Get the PART_ number from the first character of an option name.
3934 */
3935 static int
3936part_from_char(int c)
3937{
3938 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3939}
3940
3941/*
3942 * Get the option entries from the dict in "tv", parse them and put the result
3943 * in "opt".
3944 * Only accept options in "supported".
3945 * If an option value is invalid return FAIL.
3946 */
3947 int
3948get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3949{
3950 typval_T *item;
3951 char_u *val;
3952 dict_T *dict;
3953 int todo;
3954 hashitem_T *hi;
3955 int part;
3956
3957 opt->jo_set = 0;
3958 if (tv->v_type == VAR_UNKNOWN)
3959 return OK;
3960 if (tv->v_type != VAR_DICT)
3961 {
3962 EMSG(_(e_invarg));
3963 return FAIL;
3964 }
3965 dict = tv->vval.v_dict;
3966 if (dict == NULL)
3967 return OK;
3968
3969 todo = (int)dict->dv_hashtab.ht_used;
3970 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3971 if (!HASHITEM_EMPTY(hi))
3972 {
3973 item = &dict_lookup(hi)->di_tv;
3974
3975 if (STRCMP(hi->hi_key, "mode") == 0)
3976 {
3977 if (!(supported & JO_MODE))
3978 break;
3979 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3980 return FAIL;
3981 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003982 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003983 {
3984 if (!(supported & JO_IN_MODE))
3985 break;
3986 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3987 == FAIL)
3988 return FAIL;
3989 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003990 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003991 {
3992 if (!(supported & JO_OUT_MODE))
3993 break;
3994 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3995 == FAIL)
3996 return FAIL;
3997 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003998 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003999 {
4000 if (!(supported & JO_ERR_MODE))
4001 break;
4002 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4003 == FAIL)
4004 return FAIL;
4005 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004006 else if (STRCMP(hi->hi_key, "in_io") == 0
4007 || STRCMP(hi->hi_key, "out_io") == 0
4008 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004009 {
4010 if (!(supported & JO_OUT_IO))
4011 break;
4012 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4013 return FAIL;
4014 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004015 else if (STRCMP(hi->hi_key, "in_name") == 0
4016 || STRCMP(hi->hi_key, "out_name") == 0
4017 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004018 {
4019 part = part_from_char(*hi->hi_key);
4020
4021 if (!(supported & JO_OUT_IO))
4022 break;
4023 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4024 opt->jo_io_name[part] =
4025 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4026 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004027 else if (STRCMP(hi->hi_key, "in_buf") == 0
4028 || STRCMP(hi->hi_key, "out_buf") == 0
4029 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004030 {
4031 part = part_from_char(*hi->hi_key);
4032
4033 if (!(supported & JO_OUT_IO))
4034 break;
4035 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4036 opt->jo_io_buf[part] = get_tv_number(item);
4037 if (opt->jo_io_buf[part] <= 0)
4038 {
4039 EMSG2(_(e_invarg2), get_tv_string(item));
4040 return FAIL;
4041 }
4042 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4043 {
4044 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4045 return FAIL;
4046 }
4047 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004048 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4049 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4050 {
4051 part = part_from_char(*hi->hi_key);
4052
4053 if (!(supported & JO_OUT_IO))
4054 break;
4055 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4056 opt->jo_modifiable[part] = get_tv_number(item);
4057 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004058 else if (STRCMP(hi->hi_key, "in_top") == 0
4059 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004060 {
4061 linenr_T *lp;
4062
4063 if (!(supported & JO_OUT_IO))
4064 break;
4065 if (hi->hi_key[3] == 't')
4066 {
4067 lp = &opt->jo_in_top;
4068 opt->jo_set |= JO_IN_TOP;
4069 }
4070 else
4071 {
4072 lp = &opt->jo_in_bot;
4073 opt->jo_set |= JO_IN_BOT;
4074 }
4075 *lp = get_tv_number(item);
4076 if (*lp < 0)
4077 {
4078 EMSG2(_(e_invarg2), get_tv_string(item));
4079 return FAIL;
4080 }
4081 }
4082 else if (STRCMP(hi->hi_key, "channel") == 0)
4083 {
4084 if (!(supported & JO_OUT_IO))
4085 break;
4086 opt->jo_set |= JO_CHANNEL;
4087 if (item->v_type != VAR_CHANNEL)
4088 {
4089 EMSG2(_(e_invarg2), "channel");
4090 return FAIL;
4091 }
4092 opt->jo_channel = item->vval.v_channel;
4093 }
4094 else if (STRCMP(hi->hi_key, "callback") == 0)
4095 {
4096 if (!(supported & JO_CALLBACK))
4097 break;
4098 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004099 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004100 if (opt->jo_callback == NULL)
4101 {
4102 EMSG2(_(e_invarg2), "callback");
4103 return FAIL;
4104 }
4105 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004106 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004107 {
4108 if (!(supported & JO_OUT_CALLBACK))
4109 break;
4110 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004111 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004112 if (opt->jo_out_cb == NULL)
4113 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004114 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004115 return FAIL;
4116 }
4117 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004118 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004119 {
4120 if (!(supported & JO_ERR_CALLBACK))
4121 break;
4122 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004123 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004124 if (opt->jo_err_cb == NULL)
4125 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004126 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004127 return FAIL;
4128 }
4129 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004130 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004131 {
4132 if (!(supported & JO_CLOSE_CALLBACK))
4133 break;
4134 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004135 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004136 if (opt->jo_close_cb == NULL)
4137 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004138 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004139 return FAIL;
4140 }
4141 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004142 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4143 {
4144 if (!(supported & JO_EXIT_CB))
4145 break;
4146 opt->jo_set |= JO_EXIT_CB;
4147 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4148 if (opt->jo_exit_cb == NULL)
4149 {
4150 EMSG2(_(e_invarg2), "exit_cb");
4151 return FAIL;
4152 }
4153 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004154 else if (STRCMP(hi->hi_key, "waittime") == 0)
4155 {
4156 if (!(supported & JO_WAITTIME))
4157 break;
4158 opt->jo_set |= JO_WAITTIME;
4159 opt->jo_waittime = get_tv_number(item);
4160 }
4161 else if (STRCMP(hi->hi_key, "timeout") == 0)
4162 {
4163 if (!(supported & JO_TIMEOUT))
4164 break;
4165 opt->jo_set |= JO_TIMEOUT;
4166 opt->jo_timeout = get_tv_number(item);
4167 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004168 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004169 {
4170 if (!(supported & JO_OUT_TIMEOUT))
4171 break;
4172 opt->jo_set |= JO_OUT_TIMEOUT;
4173 opt->jo_out_timeout = get_tv_number(item);
4174 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004175 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004176 {
4177 if (!(supported & JO_ERR_TIMEOUT))
4178 break;
4179 opt->jo_set |= JO_ERR_TIMEOUT;
4180 opt->jo_err_timeout = get_tv_number(item);
4181 }
4182 else if (STRCMP(hi->hi_key, "part") == 0)
4183 {
4184 if (!(supported & JO_PART))
4185 break;
4186 opt->jo_set |= JO_PART;
4187 val = get_tv_string(item);
4188 if (STRCMP(val, "err") == 0)
4189 opt->jo_part = PART_ERR;
4190 else
4191 {
4192 EMSG2(_(e_invarg2), val);
4193 return FAIL;
4194 }
4195 }
4196 else if (STRCMP(hi->hi_key, "id") == 0)
4197 {
4198 if (!(supported & JO_ID))
4199 break;
4200 opt->jo_set |= JO_ID;
4201 opt->jo_id = get_tv_number(item);
4202 }
4203 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4204 {
4205 if (!(supported & JO_STOPONEXIT))
4206 break;
4207 opt->jo_set |= JO_STOPONEXIT;
4208 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4209 opt->jo_soe_buf);
4210 if (opt->jo_stoponexit == NULL)
4211 {
4212 EMSG2(_(e_invarg2), "stoponexit");
4213 return FAIL;
4214 }
4215 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004216 else if (STRCMP(hi->hi_key, "block_write") == 0)
4217 {
4218 if (!(supported & JO_BLOCK_WRITE))
4219 break;
4220 opt->jo_set |= JO_BLOCK_WRITE;
4221 opt->jo_block_write = get_tv_number(item);
4222 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004223 else
4224 break;
4225 --todo;
4226 }
4227 if (todo > 0)
4228 {
4229 EMSG2(_(e_invarg2), hi->hi_key);
4230 return FAIL;
4231 }
4232
4233 return OK;
4234}
4235
4236/*
4237 * Get the channel from the argument.
4238 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004239 * When "check_open" is TRUE check that the channel can be used.
4240 * When "reading" is TRUE "check_open" considers typeahead useful.
4241 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004242 */
4243 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004244get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004245{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004246 channel_T *channel = NULL;
4247 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004248
4249 if (tv->v_type == VAR_JOB)
4250 {
4251 if (tv->vval.v_job != NULL)
4252 channel = tv->vval.v_job->jv_channel;
4253 }
4254 else if (tv->v_type == VAR_CHANNEL)
4255 {
4256 channel = tv->vval.v_channel;
4257 }
4258 else
4259 {
4260 EMSG2(_(e_invarg2), get_tv_string(tv));
4261 return NULL;
4262 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004263 if (channel != NULL && reading)
4264 has_readahead = channel_has_readahead(channel,
4265 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004266
Bram Moolenaar437905c2016-04-26 19:01:05 +02004267 if (check_open && (channel == NULL || (!channel_is_open(channel)
4268 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004269 {
4270 EMSG(_("E906: not an open channel"));
4271 return NULL;
4272 }
4273 return channel;
4274}
4275
4276static job_T *first_job = NULL;
4277
4278 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004279job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004280{
4281 ch_log(job->jv_channel, "Freeing job");
4282 if (job->jv_channel != NULL)
4283 {
4284 /* The link from the channel to the job doesn't count as a reference,
4285 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004286 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004287 * NULL the reference. We don't set ch_job_killed, unreferencing the
4288 * job doesn't mean it stops running. */
4289 job->jv_channel->ch_job = NULL;
4290 channel_unref(job->jv_channel);
4291 }
4292 mch_clear_job(job);
4293
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004294 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004295 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004296}
4297
4298 static void
4299job_free_job(job_T *job)
4300{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004301 if (job->jv_next != NULL)
4302 job->jv_next->jv_prev = job->jv_prev;
4303 if (job->jv_prev == NULL)
4304 first_job = job->jv_next;
4305 else
4306 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004307 vim_free(job);
4308}
4309
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004310 static void
4311job_free(job_T *job)
4312{
4313 if (!in_free_unref_items)
4314 {
4315 job_free_contents(job);
4316 job_free_job(job);
4317 }
4318}
4319
Bram Moolenaar655da312016-05-28 22:22:34 +02004320#if defined(EXITFREE) || defined(PROTO)
4321 void
4322job_free_all(void)
4323{
4324 while (first_job != NULL)
4325 job_free(first_job);
4326}
4327#endif
4328
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004329/*
4330 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004331 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4332 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004333 */
4334 static int
4335job_still_useful(job_T *job)
4336{
4337 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004338 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4339 || (job->jv_channel != NULL
4340 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004341}
4342
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004343/*
4344 * Mark references in jobs that are still useful.
4345 */
4346 int
4347set_ref_in_job(int copyID)
4348{
4349 int abort = FALSE;
4350 job_T *job;
4351 typval_T tv;
4352
4353 for (job = first_job; job != NULL; job = job->jv_next)
4354 if (job_still_useful(job))
4355 {
4356 tv.v_type = VAR_JOB;
4357 tv.vval.v_job = job;
4358 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4359 }
4360 return abort;
4361}
4362
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004363 void
4364job_unref(job_T *job)
4365{
4366 if (job != NULL && --job->jv_refcount <= 0)
4367 {
4368 /* Do not free the job when it has not ended yet and there is a
4369 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004370 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004371 {
4372 job_free(job);
4373 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004374 else if (job->jv_channel != NULL
4375 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004376 {
4377 /* Do remove the link to the channel, otherwise it hangs
4378 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004379 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004380 job->jv_channel->ch_job = NULL;
4381 channel_unref(job->jv_channel);
4382 job->jv_channel = NULL;
4383 }
4384 }
4385}
4386
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004387 int
4388free_unused_jobs_contents(int copyID, int mask)
4389{
4390 int did_free = FALSE;
4391 job_T *job;
4392
4393 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004394 if ((job->jv_copyID & mask) != (copyID & mask)
4395 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004396 {
4397 /* Free the channel and ordinary items it contains, but don't
4398 * recurse into Lists, Dictionaries etc. */
4399 job_free_contents(job);
4400 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004401 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004402 return did_free;
4403}
4404
4405 void
4406free_unused_jobs(int copyID, int mask)
4407{
4408 job_T *job;
4409 job_T *job_next;
4410
4411 for (job = first_job; job != NULL; job = job_next)
4412 {
4413 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004414 if ((job->jv_copyID & mask) != (copyID & mask)
4415 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004416 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004417 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004418 job_free_job(job);
4419 }
4420 }
4421}
4422
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004423/*
4424 * Allocate a job. Sets the refcount to one and sets options default.
4425 */
4426 static job_T *
4427job_alloc(void)
4428{
4429 job_T *job;
4430
4431 job = (job_T *)alloc_clear(sizeof(job_T));
4432 if (job != NULL)
4433 {
4434 job->jv_refcount = 1;
4435 job->jv_stoponexit = vim_strsave((char_u *)"term");
4436
4437 if (first_job != NULL)
4438 {
4439 first_job->jv_prev = job;
4440 job->jv_next = first_job;
4441 }
4442 first_job = job;
4443 }
4444 return job;
4445}
4446
4447 void
4448job_set_options(job_T *job, jobopt_T *opt)
4449{
4450 if (opt->jo_set & JO_STOPONEXIT)
4451 {
4452 vim_free(job->jv_stoponexit);
4453 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4454 job->jv_stoponexit = NULL;
4455 else
4456 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4457 }
4458 if (opt->jo_set & JO_EXIT_CB)
4459 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004460 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004461 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004462 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004463 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004464 job->jv_exit_partial = NULL;
4465 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004466 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004467 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004468 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004469 job->jv_exit_partial = opt->jo_exit_partial;
4470 if (job->jv_exit_partial != NULL)
4471 ++job->jv_exit_partial->pt_refcount;
4472 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004473 }
4474}
4475
4476/*
4477 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4478 */
4479 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004480job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004481{
4482 job_T *job;
4483
4484 for (job = first_job; job != NULL; job = job->jv_next)
4485 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4486 mch_stop_job(job, job->jv_stoponexit);
4487}
4488
4489/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004490 * Return TRUE when there is any job that might exit, which means
4491 * job_check_ended() should be called once in a while.
4492 */
4493 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004494has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004495{
4496 job_T *job;
4497
4498 for (job = first_job; job != NULL; job = job->jv_next)
4499 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4500 return TRUE;
4501 return FALSE;
4502}
4503
4504/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004505 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004506 */
4507 void
4508job_check_ended(void)
4509{
4510 static time_t last_check = 0;
4511 time_t now;
4512 job_T *job;
4513 job_T *next;
4514
4515 /* Only do this once in 10 seconds. */
4516 now = time(NULL);
4517 if (last_check + 10 < now)
4518 {
4519 last_check = now;
4520 for (job = first_job; job != NULL; job = next)
4521 {
4522 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004523 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004524 job_status(job); /* may free "job" */
4525 }
4526 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004527 if (channel_need_redraw)
4528 {
4529 channel_need_redraw = FALSE;
4530 redraw_after_callback();
4531 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004532}
4533
4534/*
4535 * "job_start()" function
4536 */
4537 job_T *
4538job_start(typval_T *argvars)
4539{
4540 job_T *job;
4541 char_u *cmd = NULL;
4542#if defined(UNIX)
4543# define USE_ARGV
4544 char **argv = NULL;
4545 int argc = 0;
4546#else
4547 garray_T ga;
4548#endif
4549 jobopt_T opt;
4550 int part;
4551
4552 job = job_alloc();
4553 if (job == NULL)
4554 return NULL;
4555
4556 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004557#ifndef USE_ARGV
4558 ga_init2(&ga, (int)sizeof(char*), 20);
4559#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004560
4561 /* Default mode is NL. */
4562 clear_job_options(&opt);
4563 opt.jo_mode = MODE_NL;
4564 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004565 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4566 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004567 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004568
4569 /* Check that when io is "file" that there is a file name. */
4570 for (part = PART_OUT; part <= PART_IN; ++part)
4571 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4572 && opt.jo_io[part] == JIO_FILE
4573 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4574 || *opt.jo_io_name[part] == NUL))
4575 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004576 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004577 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004578 }
4579
4580 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4581 {
4582 buf_T *buf = NULL;
4583
4584 /* check that we can find the buffer before starting the job */
4585 if (opt.jo_set & JO_IN_BUF)
4586 {
4587 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4588 if (buf == NULL)
4589 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4590 }
4591 else if (!(opt.jo_set & JO_IN_NAME))
4592 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004593 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004594 }
4595 else
4596 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4597 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004598 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004599 if (buf->b_ml.ml_mfp == NULL)
4600 {
4601 char_u numbuf[NUMBUFLEN];
4602 char_u *s;
4603
4604 if (opt.jo_set & JO_IN_BUF)
4605 {
4606 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4607 s = numbuf;
4608 }
4609 else
4610 s = opt.jo_io_name[PART_IN];
4611 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004612 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004613 }
4614 job->jv_in_buf = buf;
4615 }
4616
4617 job_set_options(job, &opt);
4618
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004619 if (argvars[0].v_type == VAR_STRING)
4620 {
4621 /* Command is a string. */
4622 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004623 if (cmd == NULL || *cmd == NUL)
4624 {
4625 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004626 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004627 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004628#ifdef USE_ARGV
4629 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004630 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004631 argv[argc] = NULL;
4632#endif
4633 }
4634 else if (argvars[0].v_type != VAR_LIST
4635 || argvars[0].vval.v_list == NULL
4636 || argvars[0].vval.v_list->lv_len < 1)
4637 {
4638 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004639 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004640 }
4641 else
4642 {
4643 list_T *l = argvars[0].vval.v_list;
4644 listitem_T *li;
4645 char_u *s;
4646
4647#ifdef USE_ARGV
4648 /* Pass argv[] to mch_call_shell(). */
4649 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4650 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004651 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004652#endif
4653 for (li = l->lv_first; li != NULL; li = li->li_next)
4654 {
4655 s = get_tv_string_chk(&li->li_tv);
4656 if (s == NULL)
4657 goto theend;
4658#ifdef USE_ARGV
4659 argv[argc++] = (char *)s;
4660#else
4661 /* Only escape when needed, double quotes are not always allowed. */
4662 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4663 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004664# ifdef WIN32
4665 int old_ssl = p_ssl;
4666
4667 /* This is using CreateProcess, not cmd.exe. Always use
4668 * double quote and backslashes. */
4669 p_ssl = 0;
4670# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004671 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004672# ifdef WIN32
4673 p_ssl = old_ssl;
4674# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004675 if (s == NULL)
4676 goto theend;
4677 ga_concat(&ga, s);
4678 vim_free(s);
4679 }
4680 else
4681 ga_concat(&ga, s);
4682 if (li->li_next != NULL)
4683 ga_append(&ga, ' ');
4684#endif
4685 }
4686#ifdef USE_ARGV
4687 argv[argc] = NULL;
4688#else
4689 cmd = ga.ga_data;
4690#endif
4691 }
4692
4693#ifdef USE_ARGV
4694 if (ch_log_active())
4695 {
4696 garray_T ga;
4697 int i;
4698
4699 ga_init2(&ga, (int)sizeof(char), 200);
4700 for (i = 0; i < argc; ++i)
4701 {
4702 if (i > 0)
4703 ga_concat(&ga, (char_u *)" ");
4704 ga_concat(&ga, (char_u *)argv[i]);
4705 }
4706 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4707 ga_clear(&ga);
4708 }
4709 mch_start_job(argv, job, &opt);
4710#else
4711 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4712 mch_start_job((char *)cmd, job, &opt);
4713#endif
4714
4715 /* If the channel is reading from a buffer, write lines now. */
4716 if (job->jv_channel != NULL)
4717 channel_write_in(job->jv_channel);
4718
4719theend:
4720#ifdef USE_ARGV
4721 vim_free(argv);
4722#else
4723 vim_free(ga.ga_data);
4724#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004725 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004726 return job;
4727}
4728
4729/*
4730 * Get the status of "job" and invoke the exit callback when needed.
4731 * The returned string is not allocated.
4732 */
4733 char *
4734job_status(job_T *job)
4735{
4736 char *result;
4737
4738 if (job->jv_status == JOB_ENDED)
4739 /* No need to check, dead is dead. */
4740 result = "dead";
4741 else if (job->jv_status == JOB_FAILED)
4742 result = "fail";
4743 else
4744 {
4745 result = mch_job_status(job);
4746 if (job->jv_status == JOB_ENDED)
4747 ch_log(job->jv_channel, "Job ended");
4748 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4749 {
4750 typval_T argv[3];
4751 typval_T rettv;
4752 int dummy;
4753
4754 /* invoke the exit callback; make sure the refcount is > 0 */
4755 ++job->jv_refcount;
4756 argv[0].v_type = VAR_JOB;
4757 argv[0].vval.v_job = job;
4758 argv[1].v_type = VAR_NUMBER;
4759 argv[1].vval.v_number = job->jv_exitval;
4760 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004761 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4762 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004763 clear_tv(&rettv);
4764 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004765 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004766 }
4767 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4768 {
4769 /* The job was already unreferenced, now that it ended it can be
4770 * freed. Careful: caller must not use "job" after this! */
4771 job_free(job);
4772 }
4773 }
4774 return result;
4775}
4776
Bram Moolenaar8950a562016-03-12 15:22:55 +01004777/*
4778 * Implementation of job_info().
4779 */
4780 void
4781job_info(job_T *job, dict_T *dict)
4782{
4783 dictitem_T *item;
4784 varnumber_T nr;
4785
4786 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4787
4788 item = dictitem_alloc((char_u *)"channel");
4789 if (item == NULL)
4790 return;
4791 item->di_tv.v_lock = 0;
4792 item->di_tv.v_type = VAR_CHANNEL;
4793 item->di_tv.vval.v_channel = job->jv_channel;
4794 if (job->jv_channel != NULL)
4795 ++job->jv_channel->ch_refcount;
4796 if (dict_add(dict, item) == FAIL)
4797 dictitem_free(item);
4798
4799#ifdef UNIX
4800 nr = job->jv_pid;
4801#else
4802 nr = job->jv_proc_info.dwProcessId;
4803#endif
4804 dict_add_nr_str(dict, "process", nr, NULL);
4805
4806 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004807 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004808 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4809}
4810
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004811 int
4812job_stop(job_T *job, typval_T *argvars)
4813{
4814 char_u *arg;
4815
4816 if (argvars[1].v_type == VAR_UNKNOWN)
4817 arg = (char_u *)"";
4818 else
4819 {
4820 arg = get_tv_string_chk(&argvars[1]);
4821 if (arg == NULL)
4822 {
4823 EMSG(_(e_invarg));
4824 return 0;
4825 }
4826 }
4827 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4828 if (mch_stop_job(job, arg) == FAIL)
4829 return 0;
4830
4831 /* Assume that "hup" does not kill the job. */
4832 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4833 job->jv_channel->ch_job_killed = TRUE;
4834
4835 /* We don't try freeing the job, obviously the caller still has a
4836 * reference to it. */
4837 return 1;
4838}
4839
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004840#endif /* FEAT_JOB_CHANNEL */