blob: 90ef9741af06dbbde7ba4bafaf6210ec10385ea9 [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)
1125 *cbp = vim_strsave(callback);
1126 else
1127 *cbp = NULL;
1128 *pp = partial;
1129 if (*pp != NULL)
1130 ++(*pp)->pt_refcount;
1131}
1132
Bram Moolenaar187db502016-02-27 14:44:26 +01001133/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001134 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001135 */
1136 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001137channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001138{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001139 int part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001140
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001141 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001142 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001143 channel->ch_part[part].ch_mode = opt->jo_mode;
1144 if (opt->jo_set & JO_IN_MODE)
1145 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1146 if (opt->jo_set & JO_OUT_MODE)
1147 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1148 if (opt->jo_set & JO_ERR_MODE)
1149 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001150
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001151 if (opt->jo_set & JO_TIMEOUT)
1152 for (part = PART_SOCK; part <= PART_IN; ++part)
1153 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1154 if (opt->jo_set & JO_OUT_TIMEOUT)
1155 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1156 if (opt->jo_set & JO_ERR_TIMEOUT)
1157 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001158 if (opt->jo_set & JO_BLOCK_WRITE)
1159 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001160
1161 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001162 set_callback(&channel->ch_callback, &channel->ch_partial,
1163 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001164 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001165 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1166 &channel->ch_part[PART_OUT].ch_partial,
1167 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001168 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001169 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1170 &channel->ch_part[PART_ERR].ch_partial,
1171 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001172 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001173 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1174 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar187db502016-02-27 14:44:26 +01001175
1176 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1177 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001178 buf_T *buf;
1179
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001180 /* writing output to a buffer. Default mode is NL. */
1181 if (!(opt->jo_set & JO_OUT_MODE))
1182 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001183 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001184 {
1185 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1186 if (buf == NULL)
1187 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1188 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001189 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001190 {
1191 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1192 }
1193 if (buf != NULL)
1194 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001195 if (opt->jo_set & JO_OUT_MODIFIABLE)
1196 channel->ch_part[PART_OUT].ch_nomodifiable =
1197 !opt->jo_modifiable[PART_OUT];
1198
1199 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1200 {
1201 EMSG(_(e_modifiable));
1202 }
1203 else
1204 {
1205 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001206 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001207 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001208 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001209 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001210 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001211
1212 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1213 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1214 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1215 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001216 buf_T *buf;
1217
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001218 /* writing err to a buffer. Default mode is NL. */
1219 if (!(opt->jo_set & JO_ERR_MODE))
1220 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1221 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001222 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001223 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001224 {
1225 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1226 if (buf == NULL)
1227 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1228 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001229 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001230 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1231 if (buf != NULL)
1232 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001233 if (opt->jo_set & JO_ERR_MODIFIABLE)
1234 channel->ch_part[PART_ERR].ch_nomodifiable =
1235 !opt->jo_modifiable[PART_ERR];
1236 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1237 {
1238 EMSG(_(e_modifiable));
1239 }
1240 else
1241 {
1242 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001243 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001244 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001245 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001246 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001247 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001248
1249 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1250 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1251 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001252}
1253
1254/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001255 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001256 */
1257 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001258channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001259 channel_T *channel,
1260 int part,
1261 char_u *callback,
1262 partial_T *partial,
1263 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001264{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001265 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001266 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1267
1268 if (item != NULL)
1269 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001270 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001271 item->cq_partial = partial;
1272 if (partial != NULL)
1273 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001274 item->cq_seq_nr = id;
1275 item->cq_prev = head->cq_prev;
1276 head->cq_prev = item;
1277 item->cq_next = NULL;
1278 if (item->cq_prev == NULL)
1279 head->cq_next = item;
1280 else
1281 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001282 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001283}
1284
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001285 static void
1286write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1287{
1288 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001289 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001290 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001291 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001292
Bram Moolenaar655da312016-05-28 22:22:34 +02001293 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001294 if ((p = alloc(len + 2)) == NULL)
1295 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001296 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001297
1298 for (i = 0; i < len; ++i)
1299 if (p[i] == NL)
1300 p[i] = NUL;
1301
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001302 p[len] = NL;
1303 p[len + 1] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001304 channel_send(channel, PART_IN, p, len + 1, "write_buf_line()");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001305 vim_free(p);
1306}
1307
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001308/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001309 * Return TRUE if "channel" can be written to.
1310 * Returns FALSE if the input is closed or the write would block.
1311 */
1312 static int
1313can_write_buf_line(channel_T *channel)
1314{
1315 chanpart_T *in_part = &channel->ch_part[PART_IN];
1316
1317 if (in_part->ch_fd == INVALID_FD)
1318 return FALSE; /* pipe was closed */
1319
1320 /* for testing: block every other attempt to write */
1321 if (in_part->ch_block_write == 1)
1322 in_part->ch_block_write = -1;
1323 else if (in_part->ch_block_write == -1)
1324 in_part->ch_block_write = 1;
1325
1326 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1327#ifndef WIN32
1328 {
1329# if defined(HAVE_SELECT)
1330 struct timeval tval;
1331 fd_set wfds;
1332 int ret;
1333
1334 FD_ZERO(&wfds);
1335 FD_SET((int)in_part->ch_fd, &wfds);
1336 tval.tv_sec = 0;
1337 tval.tv_usec = 0;
1338 for (;;)
1339 {
1340 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1341# ifdef EINTR
1342 SOCK_ERRNO;
1343 if (ret == -1 && errno == EINTR)
1344 continue;
1345# endif
1346 if (ret <= 0 || in_part->ch_block_write == 1)
1347 {
1348 if (ret > 0)
1349 ch_log(channel, "FAKED Input not ready for writing");
1350 else
1351 ch_log(channel, "Input not ready for writing");
1352 return FALSE;
1353 }
1354 break;
1355 }
1356# else
1357 struct pollfd fds;
1358
1359 fds.fd = in_part->ch_fd;
1360 fds.events = POLLOUT;
1361 if (poll(&fds, 1, 0) <= 0)
1362 {
1363 ch_log(channel, "Input not ready for writing");
1364 return FALSE;
1365 }
1366 if (in_part->ch_block_write == 1)
1367 {
1368 ch_log(channel, "FAKED Input not ready for writing");
1369 return FALSE;
1370 }
1371# endif
1372 }
1373#endif
1374 return TRUE;
1375}
1376
1377/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001378 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001379 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001380 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001381channel_write_in(channel_T *channel)
1382{
1383 chanpart_T *in_part = &channel->ch_part[PART_IN];
1384 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001385 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001386 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001387
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001388 if (buf == NULL || in_part->ch_buf_append)
1389 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001390 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001391 {
1392 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001393 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001394 return;
1395 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001396
1397 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1398 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1399 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001400 if (!can_write_buf_line(channel))
1401 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001402 write_buf_line(buf, lnum, channel);
1403 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001404 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001405
1406 if (written == 1)
1407 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1408 else if (written > 1)
1409 ch_logn(channel, "written %d lines to channel", written);
1410
Bram Moolenaar014069a2016-03-03 22:51:40 +01001411 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001412 if (lnum > buf->b_ml.ml_line_count)
1413 {
1414 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001415 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001416 ch_log(channel, "Finished writing all lines to channel");
1417 }
1418 else
1419 ch_logn(channel, "Still %d more lines to write",
1420 buf->b_ml.ml_line_count - lnum + 1);
1421}
1422
1423/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001424 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001425 */
1426 void
1427channel_buffer_free(buf_T *buf)
1428{
1429 channel_T *channel;
1430 int part;
1431
1432 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1433 for (part = PART_SOCK; part <= PART_IN; ++part)
1434 {
1435 chanpart_T *ch_part = &channel->ch_part[part];
1436
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001437 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001438 {
1439 ch_logs(channel, "%s buffer has been wiped out",
1440 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001441 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001442 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001443 }
1444}
1445
1446/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001447 * Write any lines waiting to be written to a channel.
1448 */
1449 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001450channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001451{
1452 channel_T *channel;
1453
1454 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1455 {
1456 chanpart_T *in_part = &channel->ch_part[PART_IN];
1457
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001458 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001459 {
1460 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001461 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001462 else
1463 channel_write_in(channel);
1464 }
1465 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001466}
1467
1468/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001469 * Write appended lines above the last one in "buf" to the channel.
1470 */
1471 void
1472channel_write_new_lines(buf_T *buf)
1473{
1474 channel_T *channel;
1475 int found_one = FALSE;
1476
1477 /* There could be more than one channel for the buffer, loop over all of
1478 * them. */
1479 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1480 {
1481 chanpart_T *in_part = &channel->ch_part[PART_IN];
1482 linenr_T lnum;
1483 int written = 0;
1484
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001485 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001486 {
1487 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001488 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001489 found_one = TRUE;
1490 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1491 ++lnum)
1492 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001493 if (!can_write_buf_line(channel))
1494 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001495 write_buf_line(buf, lnum, channel);
1496 ++written;
1497 }
1498
1499 if (written == 1)
1500 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1501 else if (written > 1)
1502 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001503 if (lnum < buf->b_ml.ml_line_count)
1504 ch_logn(channel, "Still %d more lines to write",
1505 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001506
1507 in_part->ch_buf_bot = lnum;
1508 }
1509 }
1510 if (!found_one)
1511 buf->b_write_to_channel = FALSE;
1512}
1513
1514/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001515 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001516 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001517 */
1518 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001519invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1520 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001521{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001522 typval_T rettv;
1523 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001524
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001525 if (safe_to_invoke_callback == 0)
1526 EMSG("INTERNAL: Invoking callback when it is not safe");
1527
Bram Moolenaar77073442016-02-13 23:23:53 +01001528 argv[0].v_type = VAR_CHANNEL;
1529 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001530
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001531 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001532 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001533 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001534 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001535}
1536
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001537/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001538 * Return the first node from "channel"/"part" without removing it.
1539 * Returns NULL if there is nothing.
1540 */
1541 readq_T *
1542channel_peek(channel_T *channel, int part)
1543{
1544 readq_T *head = &channel->ch_part[part].ch_head;
1545
1546 return head->rq_next;
1547}
1548
1549/*
1550 * Return a pointer to the first NL in "node".
1551 * Skips over NUL characters.
1552 * Returns NULL if there is no NL.
1553 */
1554 char_u *
1555channel_first_nl(readq_T *node)
1556{
1557 char_u *buffer = node->rq_buffer;
1558 long_u i;
1559
1560 for (i = 0; i < node->rq_buflen; ++i)
1561 if (buffer[i] == NL)
1562 return buffer + i;
1563 return NULL;
1564}
1565
1566/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001567 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001568 * The caller must free it.
1569 * Returns NULL if there is nothing.
1570 */
1571 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001572channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001573{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001574 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001575 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001576 char_u *p;
1577
Bram Moolenaar77073442016-02-13 23:23:53 +01001578 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001579 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001580 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001581 p = node->rq_buffer;
1582 head->rq_next = node->rq_next;
1583 if (node->rq_next == NULL)
1584 head->rq_prev = NULL;
1585 else
1586 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001587 vim_free(node);
1588 return p;
1589}
1590
1591/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001592 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001593 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001594 */
1595 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001596channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001597{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001598 readq_T *head = &channel->ch_part[part].ch_head;
1599 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001600 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001601 char_u *res;
1602 char_u *p;
1603
1604 /* If there is only one buffer just get that one. */
1605 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1606 return channel_get(channel, part);
1607
1608 /* Concatenate everything into one buffer. */
1609 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001610 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001611 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001612 if (res == NULL)
1613 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001614 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001615 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001616 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001617 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001618 p += node->rq_buflen;
1619 }
1620 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001621
1622 /* Free all buffers */
1623 do
1624 {
1625 p = channel_get(channel, part);
1626 vim_free(p);
1627 } while (p != NULL);
1628
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001629 /* turn all NUL into NL */
1630 while (len > 0)
1631 {
1632 --len;
1633 if (res[len] == NUL)
1634 res[len] = NL;
1635 }
1636
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001637 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001638}
1639
1640/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001641 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001642 * Caller must check these bytes are available.
1643 */
1644 void
1645channel_consume(channel_T *channel, int part, int len)
1646{
1647 readq_T *head = &channel->ch_part[part].ch_head;
1648 readq_T *node = head->rq_next;
1649 char_u *buf = node->rq_buffer;
1650
1651 mch_memmove(buf, buf + len, node->rq_buflen - len);
1652 node->rq_buflen -= len;
1653}
1654
1655/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001656 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001657 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001658 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001659 */
1660 int
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001661channel_collapse(channel_T *channel, int part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001662{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001663 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001664 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001665 readq_T *last_node;
1666 readq_T *n;
1667 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001668 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001669 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001670
Bram Moolenaar77073442016-02-13 23:23:53 +01001671 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001672 return FAIL;
1673
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001674 last_node = node->rq_next;
1675 len = node->rq_buflen + last_node->rq_buflen + 1;
1676 if (want_nl)
1677 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001678 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001679 {
1680 last_node = last_node->rq_next;
1681 len += last_node->rq_buflen;
1682 }
1683
1684 p = newbuf = alloc(len);
1685 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001687 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001688 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001689 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001690 node->rq_buffer = newbuf;
1691 for (n = node; n != last_node; )
1692 {
1693 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001694 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001695 p += n->rq_buflen;
1696 vim_free(n->rq_buffer);
1697 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001698 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001699
1700 /* dispose of the collapsed nodes and their buffers */
1701 for (n = node->rq_next; n != last_node; )
1702 {
1703 n = n->rq_next;
1704 vim_free(n->rq_prev);
1705 }
1706 node->rq_next = last_node->rq_next;
1707 if (last_node->rq_next == NULL)
1708 head->rq_prev = node;
1709 else
1710 last_node->rq_next->rq_prev = node;
1711 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001712 return OK;
1713}
1714
1715/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001716 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001717 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001718 * Returns OK or FAIL.
1719 */
1720 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001721channel_save(channel_T *channel, int part, char_u *buf, int len,
1722 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001723{
1724 readq_T *node;
1725 readq_T *head = &channel->ch_part[part].ch_head;
1726 char_u *p;
1727 int i;
1728
1729 node = (readq_T *)alloc(sizeof(readq_T));
1730 if (node == NULL)
1731 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001732 /* A NUL is added at the end, because netbeans code expects that.
1733 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001734 node->rq_buffer = alloc(len + 1);
1735 if (node->rq_buffer == NULL)
1736 {
1737 vim_free(node);
1738 return FAIL; /* out of memory */
1739 }
1740
1741 if (channel->ch_part[part].ch_mode == MODE_NL)
1742 {
1743 /* Drop any CR before a NL. */
1744 p = node->rq_buffer;
1745 for (i = 0; i < len; ++i)
1746 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1747 *p++ = buf[i];
1748 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001749 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001750 }
1751 else
1752 {
1753 mch_memmove(node->rq_buffer, buf, len);
1754 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001755 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001756 }
1757
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001758 if (prepend)
1759 {
1760 /* preend node to the head of the queue */
1761 node->rq_next = head->rq_next;
1762 node->rq_prev = NULL;
1763 if (head->rq_next == NULL)
1764 head->rq_prev = node;
1765 else
1766 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001767 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001768 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001769 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001770 {
1771 /* append node to the tail of the queue */
1772 node->rq_next = NULL;
1773 node->rq_prev = head->rq_prev;
1774 if (head->rq_prev == NULL)
1775 head->rq_next = node;
1776 else
1777 head->rq_prev->rq_next = node;
1778 head->rq_prev = node;
1779 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001780
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001781 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001782 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001783 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001784 fprintf(log_fd, "'");
1785 if (fwrite(buf, len, 1, log_fd) != 1)
1786 return FAIL;
1787 fprintf(log_fd, "'\n");
1788 }
1789 return OK;
1790}
1791
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001792 static int
1793channel_fill(js_read_T *reader)
1794{
1795 channel_T *channel = (channel_T *)reader->js_cookie;
1796 int part = reader->js_cookie_arg;
1797 char_u *next = channel_get(channel, part);
1798 int unused;
1799 int len;
1800 char_u *p;
1801
1802 if (next == NULL)
1803 return FALSE;
1804
1805 unused = reader->js_end - reader->js_buf - reader->js_used;
1806 if (unused > 0)
1807 {
1808 /* Prepend unused text. */
1809 len = (int)STRLEN(next);
1810 p = alloc(unused + len + 1);
1811 if (p == NULL)
1812 {
1813 vim_free(next);
1814 return FALSE;
1815 }
1816 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1817 mch_memmove(p + unused, next, len + 1);
1818 vim_free(next);
1819 next = p;
1820 }
1821
1822 vim_free(reader->js_buf);
1823 reader->js_buf = next;
1824 reader->js_used = 0;
1825 return TRUE;
1826}
1827
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001828/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001829 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001830 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001831 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001832 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001833 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001834channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001835{
1836 js_read_T reader;
1837 typval_T listtv;
1838 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001839 chanpart_T *chanpart = &channel->ch_part[part];
1840 jsonq_T *head = &chanpart->ch_json_head;
1841 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001842 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001843
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001844 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001845 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001846
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001847 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001848 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001849 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001850 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001851 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001852
1853 /* When a message is incomplete we wait for a short while for more to
1854 * arrive. After the delay drop the input, otherwise a truncated string
1855 * or list will make us hang. */
1856 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001857 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001858 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001859 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001860 /* Only accept the response when it is a list with at least two
1861 * items. */
1862 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001863 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001864 if (listtv.v_type != VAR_LIST)
1865 ch_error(channel, "Did not receive a list, discarding");
1866 else
1867 ch_errorn(channel, "Expected list with two items, got %d",
1868 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001869 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001870 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001871 else
1872 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001873 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1874 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001875 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001876 else
1877 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001878 item->jq_value = alloc_tv();
1879 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001880 {
1881 vim_free(item);
1882 clear_tv(&listtv);
1883 }
1884 else
1885 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001886 *item->jq_value = listtv;
1887 item->jq_prev = head->jq_prev;
1888 head->jq_prev = item;
1889 item->jq_next = NULL;
1890 if (item->jq_prev == NULL)
1891 head->jq_next = item;
1892 else
1893 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001894 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001895 }
1896 }
1897 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001898
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001899 if (status == OK)
1900 chanpart->ch_waiting = FALSE;
1901 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001902 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001903 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001904 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001905 /* First time encountering incomplete message, set a deadline of
1906 * 100 msec. */
1907 ch_log(channel, "Incomplete message - wait for more");
1908 reader.js_used = 0;
1909 chanpart->ch_waiting = TRUE;
1910#ifdef WIN32
1911 chanpart->ch_deadline = GetTickCount() + 100L;
1912#else
1913 gettimeofday(&chanpart->ch_deadline, NULL);
1914 chanpart->ch_deadline.tv_usec += 100 * 1000;
1915 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1916 {
1917 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1918 ++chanpart->ch_deadline.tv_sec;
1919 }
1920#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001921 }
1922 else
1923 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001924 int timeout;
1925#ifdef WIN32
1926 timeout = GetTickCount() > chanpart->ch_deadline;
1927#else
1928 {
1929 struct timeval now_tv;
1930
1931 gettimeofday(&now_tv, NULL);
1932 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1933 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1934 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1935 }
1936#endif
1937 if (timeout)
1938 {
1939 status = FAIL;
1940 chanpart->ch_waiting = FALSE;
1941 }
1942 else
1943 {
1944 reader.js_used = 0;
1945 ch_log(channel, "still waiting on incomplete message");
1946 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001947 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001948 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001949
1950 if (status == FAIL)
1951 {
1952 ch_error(channel, "Decoding failed - discarding input");
1953 ret = FALSE;
1954 chanpart->ch_waiting = FALSE;
1955 }
1956 else if (reader.js_buf[reader.js_used] != NUL)
1957 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001958 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001959 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001960 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1961 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001962 ret = status == MAYBE ? FALSE: TRUE;
1963 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001964 else
1965 ret = FALSE;
1966
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001967 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001968 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001969}
1970
1971/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001972 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001973 */
1974 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001975remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001976{
Bram Moolenaar77073442016-02-13 23:23:53 +01001977 if (node->cq_prev == NULL)
1978 head->cq_next = node->cq_next;
1979 else
1980 node->cq_prev->cq_next = node->cq_next;
1981 if (node->cq_next == NULL)
1982 head->cq_prev = node->cq_prev;
1983 else
1984 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001985}
1986
1987/*
1988 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001989 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001990 */
1991 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001992remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001993{
Bram Moolenaar77073442016-02-13 23:23:53 +01001994 if (node->jq_prev == NULL)
1995 head->jq_next = node->jq_next;
1996 else
1997 node->jq_prev->jq_next = node->jq_next;
1998 if (node->jq_next == NULL)
1999 head->jq_prev = node->jq_prev;
2000 else
2001 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002002 vim_free(node);
2003}
2004
2005/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002006 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002007 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002008 * When "id" is zero or negative jut get the first message. But not the one
2009 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002010 * Return OK when found and return the value in "rettv".
2011 * Return FAIL otherwise.
2012 */
2013 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002014channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002015{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002016 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002017 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002018
Bram Moolenaar77073442016-02-13 23:23:53 +01002019 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002020 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002021 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002022 typval_T *tv = &l->lv_first->li_tv;
2023
2024 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002025 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002026 || tv->vval.v_number == 0
2027 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002028 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002029 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002030 if (tv->v_type == VAR_NUMBER)
2031 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002032 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002033 return OK;
2034 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002035 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002036 }
2037 return FAIL;
2038}
2039
Bram Moolenaarece61b02016-02-20 21:39:05 +01002040#define CH_JSON_MAX_ARGS 4
2041
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002042/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002043 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002044 * "argv[0]" is the command string.
2045 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002046 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002047 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002048channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002049{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002050 char_u *cmd = argv[0].vval.v_string;
2051 char_u *arg;
2052 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002053
Bram Moolenaarece61b02016-02-20 21:39:05 +01002054 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002055 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002056 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002057 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002058 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002059 return;
2060 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002061 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002062 if (arg == NULL)
2063 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002064
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002065 if (STRCMP(cmd, "ex") == 0)
2066 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002067 int save_called_emsg = called_emsg;
2068
2069 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002070 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002071 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002072 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002073 --emsg_silent;
2074 if (called_emsg)
2075 ch_logs(channel, "Ex command error: '%s'",
2076 (char *)get_vim_var_str(VV_ERRMSG));
2077 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002078 }
2079 else if (STRCMP(cmd, "normal") == 0)
2080 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002081 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002082
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002083 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002084 ea.arg = arg;
2085 ea.addr_count = 0;
2086 ea.forceit = TRUE; /* no mapping */
2087 ex_normal(&ea);
2088 }
2089 else if (STRCMP(cmd, "redraw") == 0)
2090 {
2091 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002092
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002093 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002094 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002095 ex_redraw(&ea);
2096 showruler(FALSE);
2097 setcursor();
2098 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002099#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002100 if (gui.in_use)
2101 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002102 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002103 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002104 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002105#endif
2106 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002107 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002108 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002109 int is_call = cmd[0] == 'c';
2110 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002111
Bram Moolenaarece61b02016-02-20 21:39:05 +01002112 if (argv[id_idx].v_type != VAR_UNKNOWN
2113 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002114 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002115 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002116 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002117 EMSG("E904: last argument for expr/call must be a number");
2118 }
2119 else if (is_call && argv[2].v_type != VAR_LIST)
2120 {
2121 ch_error(channel, "third argument for call must be a list");
2122 if (p_verbose > 2)
2123 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002124 }
2125 else
2126 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002127 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002128 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002129 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002130 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002131
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002132 /* Don't pollute the display with errors. */
2133 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002134 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002135 {
2136 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002137 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002138 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002139 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002140 {
2141 ch_logs(channel, "Calling '%s'", (char *)arg);
2142 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2143 tv = &res_tv;
2144 else
2145 tv = NULL;
2146 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002147
2148 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002149 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002150 int id = argv[id_idx].vval.v_number;
2151
Bram Moolenaar55fab432016-02-07 16:53:13 +01002152 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002153 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002154 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002155 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002156 /* If evaluation failed or the result can't be encoded
2157 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002158 vim_free(json);
2159 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002160 err_tv.v_type = VAR_STRING;
2161 err_tv.vval.v_string = (char_u *)"ERROR";
2162 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002163 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002164 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002165 if (json != NULL)
2166 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002167 channel_send(channel,
2168 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002169 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002170 vim_free(json);
2171 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002172 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002173 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002174 if (tv == &res_tv)
2175 clear_tv(tv);
2176 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002177 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002178 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002179 }
2180 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002181 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002182 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002183 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002184 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002185}
2186
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002187/*
2188 * Invoke the callback at "cbhead".
2189 * Does not redraw but sets channel_need_redraw.
2190 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002191 static void
2192invoke_one_time_callback(
2193 channel_T *channel,
2194 cbq_T *cbhead,
2195 cbq_T *item,
2196 typval_T *argv)
2197{
2198 ch_logs(channel, "Invoking one-time callback %s",
2199 (char *)item->cq_callback);
2200 /* Remove the item from the list first, if the callback
2201 * invokes ch_close() the list will be cleared. */
2202 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002203 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002204 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002205 vim_free(item);
2206}
2207
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002208 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002209append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002210{
2211 buf_T *save_curbuf = curbuf;
2212 linenr_T lnum = buffer->b_ml.ml_line_count;
2213 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002214 chanpart_T *ch_part = &channel->ch_part[part];
2215 int save_p_ma = buffer->b_p_ma;
2216
2217 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2218 {
2219 if (!ch_part->ch_nomod_error)
2220 {
2221 ch_error(channel, "Buffer is not modifiable, cannot append");
2222 ch_part->ch_nomod_error = TRUE;
2223 }
2224 return;
2225 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002226
2227 /* If the buffer is also used as input insert above the last
2228 * line. Don't write these lines. */
2229 if (save_write_to)
2230 {
2231 --lnum;
2232 buffer->b_write_to_channel = FALSE;
2233 }
2234
2235 /* Append to the buffer */
2236 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2237
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002238 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002239 curbuf = buffer;
2240 u_sync(TRUE);
2241 /* ignore undo failure, undo is not very useful here */
2242 ignored = u_save(lnum, lnum + 1);
2243
2244 ml_append(lnum, msg, 0, FALSE);
2245 appended_lines_mark(lnum, 1L);
2246 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002247 if (ch_part->ch_nomodifiable)
2248 buffer->b_p_ma = FALSE;
2249 else
2250 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002251
2252 if (buffer->b_nwindows > 0)
2253 {
2254 win_T *wp;
2255 win_T *save_curwin;
2256
2257 FOR_ALL_WINDOWS(wp)
2258 {
2259 if (wp->w_buffer == buffer
2260 && (save_write_to
2261 ? wp->w_cursor.lnum == lnum + 1
2262 : (wp->w_cursor.lnum == lnum
2263 && wp->w_cursor.col == 0)))
2264 {
2265 ++wp->w_cursor.lnum;
2266 save_curwin = curwin;
2267 curwin = wp;
2268 curbuf = curwin->w_buffer;
2269 scroll_cursor_bot(0, FALSE);
2270 curwin = save_curwin;
2271 curbuf = curwin->w_buffer;
2272 }
2273 }
2274 redraw_buf_later(buffer, VALID);
2275 channel_need_redraw = TRUE;
2276 }
2277
2278 if (save_write_to)
2279 {
2280 channel_T *ch;
2281
2282 /* Find channels reading from this buffer and adjust their
2283 * next-to-read line number. */
2284 buffer->b_write_to_channel = TRUE;
2285 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2286 {
2287 chanpart_T *in_part = &ch->ch_part[PART_IN];
2288
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002289 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002290 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2291 }
2292 }
2293}
2294
Bram Moolenaar437905c2016-04-26 19:01:05 +02002295 static void
2296drop_messages(channel_T *channel, int part)
2297{
2298 char_u *msg;
2299
2300 while ((msg = channel_get(channel, part)) != NULL)
2301 {
2302 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2303 vim_free(msg);
2304 }
2305}
2306
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002307/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002308 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002309 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002310 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002311 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002312 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002313may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002314{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002315 char_u *msg = NULL;
2316 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002317 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002318 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002319 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002320 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002321 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002322 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002323 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002324 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002325 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002326
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002327 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002328 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002329 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002330
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002331 /* Use a message-specific callback, part callback or channel callback */
2332 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2333 if (cbitem->cq_seq_nr == 0)
2334 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002335 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002336 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002337 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002338 partial = cbitem->cq_partial;
2339 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002340 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002341 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002342 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002343 partial = channel->ch_part[part].ch_partial;
2344 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002345 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002346 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002347 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002348 partial = channel->ch_partial;
2349 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002350
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002351 buffer = channel->ch_part[part].ch_bufref.br_buf;
2352 if (buffer != NULL && !bufref_valid(&channel->ch_part[part].ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002353 {
2354 /* buffer was wiped out */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002355 channel->ch_part[part].ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002356 buffer = NULL;
2357 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002358
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002359 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002360 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002361 listitem_T *item;
2362 int argc = 0;
2363
Bram Moolenaard7ece102016-02-02 23:23:02 +01002364 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002365 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002366 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002367 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002368 channel_parse_json(channel, part);
2369 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002370 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002371 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002372
Bram Moolenaarece61b02016-02-20 21:39:05 +01002373 for (item = listtv->vval.v_list->lv_first;
2374 item != NULL && argc < CH_JSON_MAX_ARGS;
2375 item = item->li_next)
2376 argv[argc++] = item->li_tv;
2377 while (argc < CH_JSON_MAX_ARGS)
2378 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002379
Bram Moolenaarece61b02016-02-20 21:39:05 +01002380 if (argv[0].v_type == VAR_STRING)
2381 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002382 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002383 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002384 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002385 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002386 }
2387
Bram Moolenaarece61b02016-02-20 21:39:05 +01002388 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002389 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002390 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002391 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002392 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002393 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002394 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002395 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002396 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002397 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002398 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002399 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002400 return FALSE;
2401 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002402 else
2403 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002404 /* If there is no callback or buffer drop the message. */
2405 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002406 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002407 /* If there is a close callback it may use ch_read() to get the
2408 * messages. */
2409 if (channel->ch_close_cb == NULL)
2410 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002411 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002412 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002413
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002414 if (ch_mode == MODE_NL)
2415 {
2416 char_u *nl;
2417 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002418 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002419
2420 /* See if we have a message ending in NL in the first buffer. If
2421 * not try to concatenate the first and the second buffer. */
2422 while (TRUE)
2423 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002424 node = channel_peek(channel, part);
2425 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002426 if (nl != NULL)
2427 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002428 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002429 return FALSE; /* incomplete message */
2430 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002431 buf = node->rq_buffer;
2432
2433 /* Convert NUL to NL, the internal representation. */
2434 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2435 if (*p == NUL)
2436 *p = NL;
2437
2438 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002439 {
2440 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002441 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002442 *nl = NUL;
2443 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002444 else
2445 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002446 /* Copy the message into allocated memory (excluding the NL)
2447 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002448 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002449 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002450 }
2451 }
2452 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002453 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002454 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002455 * get everything we have.
2456 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002457 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002458 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002459
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002460 if (msg == NULL)
2461 return FALSE; /* out of memory (and avoids Coverity warning) */
2462
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002463 argv[1].v_type = VAR_STRING;
2464 argv[1].vval.v_string = msg;
2465 }
2466
Bram Moolenaara07fec92016-02-05 21:04:08 +01002467 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002468 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002469 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002470
2471 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002472 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002473 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002474 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002475 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002476 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002477 break;
2478 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002479 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002480 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002481 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002482 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002483 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002484 if (buffer != NULL)
2485 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002486 if (msg == NULL)
2487 /* JSON or JS mode: re-encode the message. */
2488 msg = json_encode(listtv, ch_mode);
2489 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002490 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002491 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002492
Bram Moolenaar187db502016-02-27 14:44:26 +01002493 if (callback != NULL)
2494 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002495 if (cbitem != NULL)
2496 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2497 else
2498 {
2499 /* invoke the channel callback */
2500 ch_logs(channel, "Invoking channel callback %s",
2501 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002502 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002503 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002504 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002505 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002506 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002507 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002508
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002509 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002510 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002511 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002512
2513 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002514}
2515
2516/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002517 * Return TRUE when channel "channel" is open for writing to.
2518 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002519 */
2520 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002521channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002522{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002523 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002524 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002525}
2526
2527/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002528 * Return TRUE when channel "channel" is open for reading or writing.
2529 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002530 */
2531 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002532channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002533{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002534 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002535 || channel->CH_IN_FD != INVALID_FD
2536 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002537 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002538}
2539
2540/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002541 * Return TRUE if "channel" has JSON or other typeahead.
2542 */
2543 static int
2544channel_has_readahead(channel_T *channel, int part)
2545{
2546 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2547
2548 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2549 {
2550 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2551 jsonq_T *item = head->jq_next;
2552
2553 return item != NULL;
2554 }
2555 return channel_peek(channel, part) != NULL;
2556}
2557
2558/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002559 * Return a string indicating the status of the channel.
2560 */
2561 char *
2562channel_status(channel_T *channel)
2563{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002564 int part;
2565 int has_readahead = FALSE;
2566
Bram Moolenaar77073442016-02-13 23:23:53 +01002567 if (channel == NULL)
2568 return "fail";
2569 if (channel_is_open(channel))
2570 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002571 for (part = PART_SOCK; part <= PART_ERR; ++part)
2572 if (channel_has_readahead(channel, part))
2573 {
2574 has_readahead = TRUE;
2575 break;
2576 }
2577
2578 if (has_readahead)
2579 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002580 return "closed";
2581}
2582
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002583 static void
2584channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2585{
2586 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002587 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002588 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002589 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002590
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002591 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002592 STRCAT(namebuf, "_");
2593 tail = STRLEN(namebuf);
2594
2595 STRCPY(namebuf + tail, "status");
2596 dict_add_nr_str(dict, namebuf, 0,
2597 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2598
2599 STRCPY(namebuf + tail, "mode");
2600 switch (chanpart->ch_mode)
2601 {
2602 case MODE_NL: s = "NL"; break;
2603 case MODE_RAW: s = "RAW"; break;
2604 case MODE_JSON: s = "JSON"; break;
2605 case MODE_JS: s = "JS"; break;
2606 }
2607 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2608
2609 STRCPY(namebuf + tail, "io");
2610 if (part == PART_SOCK)
2611 s = "socket";
2612 else switch (chanpart->ch_io)
2613 {
2614 case JIO_NULL: s = "null"; break;
2615 case JIO_PIPE: s = "pipe"; break;
2616 case JIO_FILE: s = "file"; break;
2617 case JIO_BUFFER: s = "buffer"; break;
2618 case JIO_OUT: s = "out"; break;
2619 }
2620 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2621
2622 STRCPY(namebuf + tail, "timeout");
2623 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2624}
2625
2626 void
2627channel_info(channel_T *channel, dict_T *dict)
2628{
2629 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2630 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2631
2632 if (channel->ch_hostname != NULL)
2633 {
2634 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2635 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2636 channel_part_info(channel, dict, "sock", PART_SOCK);
2637 }
2638 else
2639 {
2640 channel_part_info(channel, dict, "out", PART_OUT);
2641 channel_part_info(channel, dict, "err", PART_ERR);
2642 channel_part_info(channel, dict, "in", PART_IN);
2643 }
2644}
2645
Bram Moolenaar77073442016-02-13 23:23:53 +01002646/*
2647 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002648 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002649 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002650 */
2651 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002652channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002653{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002654 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002655
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002656#ifdef FEAT_GUI
2657 channel_gui_unregister(channel);
2658#endif
2659
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002660 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002661 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002662 sock_close(channel->CH_SOCK_FD);
2663 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002664 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002665 may_close_part(&channel->CH_IN_FD);
2666 may_close_part(&channel->CH_OUT_FD);
2667 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002668
Bram Moolenaar8b374212016-02-24 20:43:06 +01002669 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002670 {
2671 typval_T argv[1];
2672 typval_T rettv;
2673 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002674 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002675
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002676 /* Invoke callbacks before the close callback, since it's weird to
2677 * first invoke the close callback. Increment the refcount to avoid
2678 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002679 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002680 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002681 for (part = PART_SOCK; part <= PART_ERR; ++part)
2682 while (may_invoke_callback(channel, part))
2683 ;
2684
2685 /* Invoke the close callback, if still set. */
2686 if (channel->ch_close_cb != NULL)
2687 {
2688 ch_logs(channel, "Invoking close callback %s",
2689 (char *)channel->ch_close_cb);
2690 argv[0].v_type = VAR_CHANNEL;
2691 argv[0].vval.v_channel = channel;
2692 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002693 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2694 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002695 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002696 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002697 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002698
2699 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002700 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002701 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002702 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002703
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002704 --channel->ch_refcount;
2705
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002706 if (channel_need_redraw)
2707 {
2708 channel_need_redraw = FALSE;
2709 redraw_after_callback();
2710 }
2711
Bram Moolenaar437905c2016-04-26 19:01:05 +02002712 /* any remaining messages are useless now */
2713 for (part = PART_SOCK; part <= PART_ERR; ++part)
2714 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002715 }
2716
2717 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002718}
2719
Bram Moolenaard04a0202016-01-26 23:30:18 +01002720/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002721 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002722 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002723 static void
2724channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002725{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002726 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2727 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002728
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002729 while (channel_peek(channel, part) != NULL)
2730 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002731
2732 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002733 {
2734 cbq_T *node = cb_head->cq_next;
2735
2736 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002737 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002738 vim_free(node);
2739 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002740
2741 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002742 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002743 free_tv(json_head->jq_next->jq_value);
2744 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002745 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002746
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002747 free_callback(channel->ch_part[part].ch_callback,
2748 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002749 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002750 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002751}
2752
2753/*
2754 * Clear all the read buffers on "channel".
2755 */
2756 void
2757channel_clear(channel_T *channel)
2758{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002759 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002760 vim_free(channel->ch_hostname);
2761 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002762 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002763 channel_clear_one(channel, PART_OUT);
2764 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002765 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002766 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002767 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002768 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002769 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002770 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002771 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002772}
2773
Bram Moolenaar77073442016-02-13 23:23:53 +01002774#if defined(EXITFREE) || defined(PROTO)
2775 void
2776channel_free_all(void)
2777{
2778 channel_T *channel;
2779
Bram Moolenaard6051b52016-02-28 15:49:03 +01002780 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002781 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2782 channel_clear(channel);
2783}
2784#endif
2785
2786
Bram Moolenaar715d2852016-04-30 17:06:31 +02002787/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002788#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002789
2790/* Buffer size for reading incoming messages. */
2791#define MAXMSGSIZE 4096
2792
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002793#if defined(HAVE_SELECT)
2794/*
2795 * Add write fds where we are waiting for writing to be possible.
2796 */
2797 static int
2798channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2799{
2800 int maxfd = maxfd_arg;
2801 channel_T *ch;
2802
2803 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2804 {
2805 chanpart_T *in_part = &ch->ch_part[PART_IN];
2806
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002807 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002808 {
2809 FD_SET((int)in_part->ch_fd, wfds);
2810 if ((int)in_part->ch_fd >= maxfd)
2811 maxfd = (int)in_part->ch_fd + 1;
2812 }
2813 }
2814 return maxfd;
2815}
2816#else
2817/*
2818 * Add write fds where we are waiting for writing to be possible.
2819 */
2820 static int
2821channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2822{
2823 int nfd = nfd_in;
2824 channel_T *ch;
2825
2826 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2827 {
2828 chanpart_T *in_part = &ch->ch_part[PART_IN];
2829
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002830 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002831 {
2832 in_part->ch_poll_idx = nfd;
2833 fds[nfd].fd = in_part->ch_fd;
2834 fds[nfd].events = POLLOUT;
2835 ++nfd;
2836 }
2837 else
2838 in_part->ch_poll_idx = -1;
2839 }
2840 return nfd;
2841}
2842#endif
2843
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002844typedef enum {
2845 CW_READY,
2846 CW_NOT_READY,
2847 CW_ERROR
2848} channel_wait_result;
2849
Bram Moolenaard04a0202016-01-26 23:30:18 +01002850/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002851 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002852 * Return CW_READY when there is something to read.
2853 * Return CW_NOT_READY when there is nothing to read.
2854 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002855 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002856 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002857channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002858{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002859 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002860 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002861
Bram Moolenaard8070362016-02-15 21:56:54 +01002862# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002863 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002864 {
2865 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002866 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002867 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002868 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002869
2870 /* reading from a pipe, not a socket */
2871 while (TRUE)
2872 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002873 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2874
2875 if (r && nread > 0)
2876 return CW_READY;
2877 if (r == 0)
2878 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002879
2880 /* perhaps write some buffer lines */
2881 channel_write_any_lines();
2882
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002883 sleep_time = deadline - GetTickCount();
2884 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002885 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002886 /* Wait for a little while. Very short at first, up to 10 msec
2887 * after looping a few times. */
2888 if (sleep_time > delay)
2889 sleep_time = delay;
2890 Sleep(sleep_time);
2891 delay = delay * 2;
2892 if (delay > 10)
2893 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002894 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002895 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002896 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002897#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002898 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002899#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002900 struct timeval tval;
2901 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002902 fd_set wfds;
2903 int ret;
2904 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002905
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002906 tval.tv_sec = timeout / 1000;
2907 tval.tv_usec = (timeout % 1000) * 1000;
2908 for (;;)
2909 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002910 FD_ZERO(&rfds);
2911 FD_SET((int)fd, &rfds);
2912
2913 /* Write lines to a pipe when a pipe can be written to. Need to
2914 * set this every time, some buffers may be done. */
2915 maxfd = (int)fd + 1;
2916 FD_ZERO(&wfds);
2917 maxfd = channel_fill_wfds(maxfd, &wfds);
2918
2919 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002920# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002921 SOCK_ERRNO;
2922 if (ret == -1 && errno == EINTR)
2923 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002924# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002925 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002926 {
2927 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002928 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002929 channel_write_any_lines();
2930 continue;
2931 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002932 break;
2933 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002934#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002935 for (;;)
2936 {
2937 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2938 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002939
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002940 fds[0].fd = fd;
2941 fds[0].events = POLLIN;
2942 nfd = channel_fill_poll_write(nfd, fds);
2943 if (poll(fds, nfd, timeout) > 0)
2944 {
2945 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002946 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002947 channel_write_any_lines();
2948 continue;
2949 }
2950 break;
2951 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002952#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002953 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002954 return CW_NOT_READY;
2955}
2956
2957 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002958channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002959{
2960 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002961 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2962 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002963
2964 /* Queue a "DETACH" netbeans message in the command queue in order to
2965 * terminate the netbeans session later. Do not end the session here
2966 * directly as we may be running in the context of a call to
2967 * netbeans_parse_messages():
2968 * netbeans_parse_messages
2969 * -> autocmd triggered while processing the netbeans cmd
2970 * -> ui_breakcheck
2971 * -> gui event loop or select loop
2972 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002973 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002974 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002975 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002976 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002977 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2978
2979 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002980 * died. Don't close the channel right away, it may be the wrong moment
2981 * to invoke callbacks. */
2982 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02002983
2984#ifdef FEAT_GUI
2985 /* Stop listening to GUI events right away. */
2986 channel_gui_unregister(channel);
2987#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002988}
2989
2990 static void
2991channel_close_now(channel_T *channel)
2992{
2993 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002994 channel_close(channel, TRUE);
2995 if (channel->ch_nb_close_cb != NULL)
2996 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002997}
2998
2999/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003000 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003001 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003002 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003003 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003004 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003005channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003006{
3007 static char_u *buf = NULL;
3008 int len = 0;
3009 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003010 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003011 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003012
Bram Moolenaar5850a762016-05-27 19:59:48 +02003013 /* If we detected a read error don't try reading again. */
3014 if (channel->ch_to_be_closed)
3015 return;
3016
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003017 fd = channel->ch_part[part].ch_fd;
3018 if (fd == INVALID_FD)
3019 {
3020 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003021 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003022 }
3023 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003024
3025 /* Allocate a buffer to read into. */
3026 if (buf == NULL)
3027 {
3028 buf = alloc(MAXMSGSIZE);
3029 if (buf == NULL)
3030 return; /* out of memory! */
3031 }
3032
3033 /* Keep on reading for as long as there is something to read.
3034 * Use select() or poll() to avoid blocking on a message that is exactly
3035 * MAXMSGSIZE long. */
3036 for (;;)
3037 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003038 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003039 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003040 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003041 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003042 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003043 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003044 if (len <= 0)
3045 break; /* error or nothing more to read */
3046
3047 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003048 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003049 readlen += len;
3050 if (len < MAXMSGSIZE)
3051 break; /* did read everything that's available */
3052 }
3053
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003054 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003055 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003056 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003057
3058#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003059 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003060 if (CH_HAS_GUI && gtk_main_level() > 0)
3061 gtk_main_quit();
3062#endif
3063}
3064
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003065/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003066 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003067 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003068 * Returns what was read in allocated memory.
3069 * Returns NULL in case of error or timeout.
3070 */
3071 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003072channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003073{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003074 char_u *buf;
3075 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003076 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003077 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003078 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003079 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003080
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003081 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003082 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003083
3084 while (TRUE)
3085 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003086 node = channel_peek(channel, part);
3087 if (node != NULL)
3088 {
3089 if (mode == MODE_RAW || (mode == MODE_NL
3090 && channel_first_nl(node) != NULL))
3091 /* got a complete message */
3092 break;
3093 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3094 continue;
3095 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003096
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003097 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003098 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003099 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003100 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003101 {
3102 ch_log(channel, "Timed out");
3103 return NULL;
3104 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003105 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003106 }
3107
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003108 if (mode == MODE_RAW)
3109 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003110 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003111 }
3112 else
3113 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003114 char_u *p;
3115
3116 buf = node->rq_buffer;
3117 nl = channel_first_nl(node);
3118
3119 /* Convert NUL to NL, the internal representation. */
3120 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3121 if (*p == NUL)
3122 *p = NL;
3123
3124 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003125 {
3126 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003127 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003128 *nl = NUL;
3129 }
3130 else
3131 {
3132 /* Copy the message into allocated memory and remove it from the
3133 * buffer. */
3134 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003135 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003136 }
3137 }
3138 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003139 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003140 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003141}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003142
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003143/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003144 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003145 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003146 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003147 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003148 */
3149 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003150channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003151 channel_T *channel,
3152 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003153 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003154 int id,
3155 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003156{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003157 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003158 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003159 int timeout;
3160 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003161
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003162 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003163 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003164 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003165 for (;;)
3166 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003167 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003168
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003169 /* search for message "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003170 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003171 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003172 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003173 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003174 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003175
Bram Moolenaard7ece102016-02-02 23:23:02 +01003176 if (!more)
3177 {
3178 /* Handle any other messages in the queue. If done some more
3179 * messages may have arrived. */
3180 if (channel_parse_messages())
3181 continue;
3182
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003183 /* Wait for up to the timeout. If there was an incomplete message
3184 * use the deadline for that. */
3185 timeout = timeout_arg;
3186 if (chanpart->ch_waiting)
3187 {
3188#ifdef WIN32
3189 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3190#else
3191 {
3192 struct timeval now_tv;
3193
3194 gettimeofday(&now_tv, NULL);
3195 timeout = (chanpart->ch_deadline.tv_sec
3196 - now_tv.tv_sec) * 1000
3197 + (chanpart->ch_deadline.tv_usec
3198 - now_tv.tv_usec) / 1000
3199 + 1;
3200 }
3201#endif
3202 if (timeout < 0)
3203 {
3204 /* Something went wrong, channel_parse_json() didn't
3205 * discard message. Cancel waiting. */
3206 chanpart->ch_waiting = FALSE;
3207 timeout = timeout_arg;
3208 }
3209 else if (timeout > timeout_arg)
3210 timeout = timeout_arg;
3211 }
3212 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003213 if (fd == INVALID_FD
3214 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003215 {
3216 if (timeout == timeout_arg)
3217 {
3218 if (fd != INVALID_FD)
3219 ch_log(channel, "Timed out");
3220 break;
3221 }
3222 }
3223 else
3224 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003225 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003226 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003227 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003228 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003229}
3230
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003231/*
3232 * Common for ch_read() and ch_readraw().
3233 */
3234 void
3235common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3236{
3237 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003238 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003239 jobopt_T opt;
3240 int mode;
3241 int timeout;
3242 int id = -1;
3243 typval_T *listtv = NULL;
3244
3245 /* return an empty string by default */
3246 rettv->v_type = VAR_STRING;
3247 rettv->vval.v_string = NULL;
3248
3249 clear_job_options(&opt);
3250 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3251 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003252 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003253
Bram Moolenaar437905c2016-04-26 19:01:05 +02003254 if (opt.jo_set & JO_PART)
3255 part = opt.jo_part;
3256 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003257 if (channel != NULL)
3258 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003259 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003260 part = channel_part_read(channel);
3261 mode = channel_get_mode(channel, part);
3262 timeout = channel_get_timeout(channel, part);
3263 if (opt.jo_set & JO_TIMEOUT)
3264 timeout = opt.jo_timeout;
3265
3266 if (raw || mode == MODE_RAW || mode == MODE_NL)
3267 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3268 else
3269 {
3270 if (opt.jo_set & JO_ID)
3271 id = opt.jo_id;
3272 channel_read_json_block(channel, part, timeout, id, &listtv);
3273 if (listtv != NULL)
3274 {
3275 *rettv = *listtv;
3276 vim_free(listtv);
3277 }
3278 else
3279 {
3280 rettv->v_type = VAR_SPECIAL;
3281 rettv->vval.v_number = VVAL_NONE;
3282 }
3283 }
3284 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003285
3286theend:
3287 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003288}
3289
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003290# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3291 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003292/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003293 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003294 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003295 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003296 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003297channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003298{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003299 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003300 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003301
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003302 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003303 for (channel = first_channel; channel != NULL;
3304 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003305 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003306 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003307 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003308 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003309 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003310 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003311 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003312 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003313 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003314}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003315# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003316
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003317# if defined(WIN32) || defined(PROTO)
3318/*
3319 * Check the channels for anything that is ready to be read.
3320 * The data is put in the read queue.
3321 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003322 void
3323channel_handle_events(void)
3324{
3325 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003326 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003327 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003328
3329 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3330 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003331 /* If we detected a read error don't try reading again. */
3332 if (channel->ch_to_be_closed)
3333 continue;
3334
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003335 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003336 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003337 {
3338 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003339 if (fd != INVALID_FD)
3340 {
3341 int r = channel_wait(channel, fd, 0);
3342
3343 if (r == CW_READY)
3344 channel_read(channel, part, "channel_handle_events");
3345 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003346 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003347 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003348 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003349 }
3350}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003351# endif
3352
Bram Moolenaard04a0202016-01-26 23:30:18 +01003353/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003354 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003355 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003356 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003357 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003358 int
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003359channel_send(channel_T *channel, int part, char_u *buf, int len, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003360{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003361 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003362 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003363
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003364 fd = channel->ch_part[part].ch_fd;
3365 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003366 {
3367 if (!channel->ch_error && fun != NULL)
3368 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003369 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003370 EMSG2("E630: %s(): write while not connected", fun);
3371 }
3372 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003373 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003374 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003375
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003376 if (log_fd != NULL)
3377 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003378 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003379 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003380 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003381 fprintf(log_fd, "'\n");
3382 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003383 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003384 }
3385
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003386 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003387 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003388 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003389 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003390 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003391 {
3392 if (!channel->ch_error && fun != NULL)
3393 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003394 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003395 EMSG2("E631: %s(): write failed", fun);
3396 }
3397 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003398 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003399 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003400
3401 channel->ch_error = FALSE;
3402 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003403}
3404
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003405/*
3406 * Common for "ch_sendexpr()" and "ch_sendraw()".
3407 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003408 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003409 * Otherwise returns NULL.
3410 */
3411 channel_T *
3412send_common(
3413 typval_T *argvars,
3414 char_u *text,
3415 int id,
3416 int eval,
3417 jobopt_T *opt,
3418 char *fun,
3419 int *part_read)
3420{
3421 channel_T *channel;
3422 int part_send;
3423
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003424 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003425 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003426 if (channel == NULL)
3427 return NULL;
3428 part_send = channel_part_send(channel);
3429 *part_read = channel_part_read(channel);
3430
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003431 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3432 return NULL;
3433
3434 /* Set the callback. An empty callback means no callback and not reading
3435 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3436 * allowed. */
3437 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3438 {
3439 if (eval)
3440 {
3441 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3442 return NULL;
3443 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003444 channel_set_req_callback(channel, part_send,
3445 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003446 }
3447
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003448 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003449 && opt->jo_callback == NULL)
3450 return channel;
3451 return NULL;
3452}
3453
3454/*
3455 * common for "ch_evalexpr()" and "ch_sendexpr()"
3456 */
3457 void
3458ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3459{
3460 char_u *text;
3461 typval_T *listtv;
3462 channel_T *channel;
3463 int id;
3464 ch_mode_T ch_mode;
3465 int part_send;
3466 int part_read;
3467 jobopt_T opt;
3468 int timeout;
3469
3470 /* return an empty string by default */
3471 rettv->v_type = VAR_STRING;
3472 rettv->vval.v_string = NULL;
3473
Bram Moolenaar437905c2016-04-26 19:01:05 +02003474 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003475 if (channel == NULL)
3476 return;
3477 part_send = channel_part_send(channel);
3478
3479 ch_mode = channel_get_mode(channel, part_send);
3480 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3481 {
3482 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3483 return;
3484 }
3485
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003486 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003487 text = json_encode_nr_expr(id, &argvars[1],
3488 ch_mode == MODE_JS ? JSON_JS : 0);
3489 if (text == NULL)
3490 return;
3491
3492 channel = send_common(argvars, text, id, eval, &opt,
3493 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3494 vim_free(text);
3495 if (channel != NULL && eval)
3496 {
3497 if (opt.jo_set & JO_TIMEOUT)
3498 timeout = opt.jo_timeout;
3499 else
3500 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003501 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3502 == OK)
3503 {
3504 list_T *list = listtv->vval.v_list;
3505
3506 /* Move the item from the list and then change the type to
3507 * avoid the value being freed. */
3508 *rettv = list->lv_last->li_tv;
3509 list->lv_last->li_tv.v_type = VAR_NUMBER;
3510 free_tv(listtv);
3511 }
3512 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003513 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003514}
3515
3516/*
3517 * common for "ch_evalraw()" and "ch_sendraw()"
3518 */
3519 void
3520ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3521{
3522 char_u buf[NUMBUFLEN];
3523 char_u *text;
3524 channel_T *channel;
3525 int part_read;
3526 jobopt_T opt;
3527 int timeout;
3528
3529 /* return an empty string by default */
3530 rettv->v_type = VAR_STRING;
3531 rettv->vval.v_string = NULL;
3532
3533 text = get_tv_string_buf(&argvars[1], buf);
3534 channel = send_common(argvars, text, 0, eval, &opt,
3535 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3536 if (channel != NULL && eval)
3537 {
3538 if (opt.jo_set & JO_TIMEOUT)
3539 timeout = opt.jo_timeout;
3540 else
3541 timeout = channel_get_timeout(channel, part_read);
3542 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3543 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003544 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003545}
3546
Bram Moolenaard04a0202016-01-26 23:30:18 +01003547# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003548/*
3549 * Add open channels to the poll struct.
3550 * Return the adjusted struct index.
3551 * The type of "fds" is hidden to avoid problems with the function proto.
3552 */
3553 int
3554channel_poll_setup(int nfd_in, void *fds_in)
3555{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003556 int nfd = nfd_in;
3557 channel_T *channel;
3558 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003559 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003560
Bram Moolenaar77073442016-02-13 23:23:53 +01003561 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003562 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003563 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003564 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003565 chanpart_T *ch_part = &channel->ch_part[part];
3566
3567 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003568 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003569 ch_part->ch_poll_idx = nfd;
3570 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003571 fds[nfd].events = POLLIN;
3572 nfd++;
3573 }
3574 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003575 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003576 }
3577 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003578
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003579 nfd = channel_fill_poll_write(nfd, fds);
3580
Bram Moolenaare0874f82016-01-24 20:36:41 +01003581 return nfd;
3582}
3583
3584/*
3585 * The type of "fds" is hidden to avoid problems with the function proto.
3586 */
3587 int
3588channel_poll_check(int ret_in, void *fds_in)
3589{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003590 int ret = ret_in;
3591 channel_T *channel;
3592 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003593 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003594 int idx;
3595 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003596
Bram Moolenaar77073442016-02-13 23:23:53 +01003597 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003598 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003599 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003600 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003601 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003602
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003603 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003604 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003605 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003606 --ret;
3607 }
3608 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003609
3610 in_part = &channel->ch_part[PART_IN];
3611 idx = in_part->ch_poll_idx;
3612 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3613 {
3614 if (in_part->ch_buf_append)
3615 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003616 if (in_part->ch_bufref.br_buf != NULL)
3617 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003618 }
3619 else
3620 channel_write_in(channel);
3621 --ret;
3622 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003623 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003624
3625 return ret;
3626}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003627# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003628
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003629# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003630/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003631 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003632 */
3633 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003634channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003635{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003636 int maxfd = maxfd_in;
3637 channel_T *channel;
3638 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003639 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003640 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003641
Bram Moolenaar77073442016-02-13 23:23:53 +01003642 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003643 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003644 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003645 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003646 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003647
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003648 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003649 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003650 FD_SET((int)fd, rfds);
3651 if (maxfd < (int)fd)
3652 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003653 }
3654 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003655 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003656
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003657 maxfd = channel_fill_wfds(maxfd, wfds);
3658
Bram Moolenaare0874f82016-01-24 20:36:41 +01003659 return maxfd;
3660}
3661
3662/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003663 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003664 */
3665 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003666channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003667{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003668 int ret = ret_in;
3669 channel_T *channel;
3670 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003671 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003672 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003673 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003674
Bram Moolenaar77073442016-02-13 23:23:53 +01003675 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003676 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003677 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003678 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003679 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003680
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003681 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003682 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003683 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003684 --ret;
3685 }
3686 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003687
3688 in_part = &channel->ch_part[PART_IN];
3689 if (ret > 0 && in_part->ch_fd != INVALID_FD
3690 && FD_ISSET(in_part->ch_fd, wfds))
3691 {
3692 if (in_part->ch_buf_append)
3693 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003694 if (in_part->ch_bufref.br_buf != NULL)
3695 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003696 }
3697 else
3698 channel_write_in(channel);
3699 --ret;
3700 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003701 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003702
3703 return ret;
3704}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003705# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003706
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003707/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003708 * Execute queued up commands.
3709 * Invoked from the main loop when it's safe to execute received commands.
3710 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003711 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003712 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003713channel_parse_messages(void)
3714{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003715 channel_T *channel = first_channel;
3716 int ret = FALSE;
3717 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003718 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003719
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003720 ++safe_to_invoke_callback;
3721
Bram Moolenaard0b65022016-03-06 21:50:33 +01003722 /* Only do this message when another message was given, otherwise we get
3723 * lots of them. */
3724 if (did_log_msg)
3725 {
3726 ch_log(NULL, "looking for messages on channels");
3727 did_log_msg = FALSE;
3728 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003729 while (channel != NULL)
3730 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003731 if (channel->ch_to_be_closed)
3732 {
3733 channel->ch_to_be_closed = FALSE;
3734 channel_close_now(channel);
3735 /* channel may have been freed, start over */
3736 channel = first_channel;
3737 continue;
3738 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003739 if (channel->ch_to_be_freed)
3740 {
3741 channel_free(channel);
3742 /* channel has been freed, start over */
3743 channel = first_channel;
3744 continue;
3745 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003746 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003747 {
3748 /* channel is no longer useful, free it */
3749 channel_free(channel);
3750 channel = first_channel;
3751 part = PART_SOCK;
3752 continue;
3753 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003754 if (channel->ch_part[part].ch_fd != INVALID_FD
3755 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003756 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003757 /* Increase the refcount, in case the handler causes the channel
3758 * to be unreferenced or closed. */
3759 ++channel->ch_refcount;
3760 r = may_invoke_callback(channel, part);
3761 if (r == OK)
3762 ret = TRUE;
3763 if (channel_unref(channel) || r == OK)
3764 {
3765 /* channel was freed or something was done, start over */
3766 channel = first_channel;
3767 part = PART_SOCK;
3768 continue;
3769 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003770 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003771 if (part < PART_ERR)
3772 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003773 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003774 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003775 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003776 part = PART_SOCK;
3777 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003778 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003779
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003780 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003781 {
3782 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003783 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003784 }
3785
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003786 --safe_to_invoke_callback;
3787
Bram Moolenaard7ece102016-02-02 23:23:02 +01003788 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003789}
3790
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003791/*
3792 * Mark references to lists used in channels.
3793 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003794 int
3795set_ref_in_channel(int copyID)
3796{
Bram Moolenaar77073442016-02-13 23:23:53 +01003797 int abort = FALSE;
3798 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003799 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003800
Bram Moolenaar77073442016-02-13 23:23:53 +01003801 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003802 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003803 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003804 tv.v_type = VAR_CHANNEL;
3805 tv.vval.v_channel = channel;
3806 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003807 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003808 return abort;
3809}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003810
3811/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003812 * Return the "part" to write to for "channel".
3813 */
3814 int
3815channel_part_send(channel_T *channel)
3816{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003817 if (channel->CH_SOCK_FD == INVALID_FD)
3818 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003819 return PART_SOCK;
3820}
3821
3822/*
3823 * Return the default "part" to read from for "channel".
3824 */
3825 int
3826channel_part_read(channel_T *channel)
3827{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003828 if (channel->CH_SOCK_FD == INVALID_FD)
3829 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003830 return PART_SOCK;
3831}
3832
3833/*
3834 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003835 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003836 */
3837 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003838channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003839{
Bram Moolenaar77073442016-02-13 23:23:53 +01003840 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003841 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003842 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003843}
3844
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003845/*
3846 * Return the timeout of "channel"/"part"
3847 */
3848 int
3849channel_get_timeout(channel_T *channel, int part)
3850{
3851 return channel->ch_part[part].ch_timeout;
3852}
3853
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003854 static int
3855handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3856{
3857 char_u *val = get_tv_string(item);
3858
3859 opt->jo_set |= jo;
3860 if (STRCMP(val, "nl") == 0)
3861 *modep = MODE_NL;
3862 else if (STRCMP(val, "raw") == 0)
3863 *modep = MODE_RAW;
3864 else if (STRCMP(val, "js") == 0)
3865 *modep = MODE_JS;
3866 else if (STRCMP(val, "json") == 0)
3867 *modep = MODE_JSON;
3868 else
3869 {
3870 EMSG2(_(e_invarg2), val);
3871 return FAIL;
3872 }
3873 return OK;
3874}
3875
3876 static int
3877handle_io(typval_T *item, int part, jobopt_T *opt)
3878{
3879 char_u *val = get_tv_string(item);
3880
3881 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3882 if (STRCMP(val, "null") == 0)
3883 opt->jo_io[part] = JIO_NULL;
3884 else if (STRCMP(val, "pipe") == 0)
3885 opt->jo_io[part] = JIO_PIPE;
3886 else if (STRCMP(val, "file") == 0)
3887 opt->jo_io[part] = JIO_FILE;
3888 else if (STRCMP(val, "buffer") == 0)
3889 opt->jo_io[part] = JIO_BUFFER;
3890 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3891 opt->jo_io[part] = JIO_OUT;
3892 else
3893 {
3894 EMSG2(_(e_invarg2), val);
3895 return FAIL;
3896 }
3897 return OK;
3898}
3899
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003900/*
3901 * Clear a jobopt_T before using it.
3902 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003903 void
3904clear_job_options(jobopt_T *opt)
3905{
3906 vim_memset(opt, 0, sizeof(jobopt_T));
3907}
3908
3909/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003910 * Free any members of a jobopt_T.
3911 */
3912 void
3913free_job_options(jobopt_T *opt)
3914{
3915 if (opt->jo_partial != NULL)
3916 partial_unref(opt->jo_partial);
3917 if (opt->jo_out_partial != NULL)
3918 partial_unref(opt->jo_out_partial);
3919 if (opt->jo_err_partial != NULL)
3920 partial_unref(opt->jo_err_partial);
3921 if (opt->jo_close_partial != NULL)
3922 partial_unref(opt->jo_close_partial);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003923 if (opt->jo_exit_partial != NULL)
3924 partial_unref(opt->jo_exit_partial);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003925}
3926
3927/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003928 * Get the PART_ number from the first character of an option name.
3929 */
3930 static int
3931part_from_char(int c)
3932{
3933 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3934}
3935
3936/*
3937 * Get the option entries from the dict in "tv", parse them and put the result
3938 * in "opt".
3939 * Only accept options in "supported".
3940 * If an option value is invalid return FAIL.
3941 */
3942 int
3943get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3944{
3945 typval_T *item;
3946 char_u *val;
3947 dict_T *dict;
3948 int todo;
3949 hashitem_T *hi;
3950 int part;
3951
3952 opt->jo_set = 0;
3953 if (tv->v_type == VAR_UNKNOWN)
3954 return OK;
3955 if (tv->v_type != VAR_DICT)
3956 {
3957 EMSG(_(e_invarg));
3958 return FAIL;
3959 }
3960 dict = tv->vval.v_dict;
3961 if (dict == NULL)
3962 return OK;
3963
3964 todo = (int)dict->dv_hashtab.ht_used;
3965 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3966 if (!HASHITEM_EMPTY(hi))
3967 {
3968 item = &dict_lookup(hi)->di_tv;
3969
3970 if (STRCMP(hi->hi_key, "mode") == 0)
3971 {
3972 if (!(supported & JO_MODE))
3973 break;
3974 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3975 return FAIL;
3976 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003977 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003978 {
3979 if (!(supported & JO_IN_MODE))
3980 break;
3981 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3982 == FAIL)
3983 return FAIL;
3984 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003985 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003986 {
3987 if (!(supported & JO_OUT_MODE))
3988 break;
3989 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3990 == FAIL)
3991 return FAIL;
3992 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003993 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003994 {
3995 if (!(supported & JO_ERR_MODE))
3996 break;
3997 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3998 == FAIL)
3999 return FAIL;
4000 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004001 else if (STRCMP(hi->hi_key, "in_io") == 0
4002 || STRCMP(hi->hi_key, "out_io") == 0
4003 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004004 {
4005 if (!(supported & JO_OUT_IO))
4006 break;
4007 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4008 return FAIL;
4009 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004010 else if (STRCMP(hi->hi_key, "in_name") == 0
4011 || STRCMP(hi->hi_key, "out_name") == 0
4012 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004013 {
4014 part = part_from_char(*hi->hi_key);
4015
4016 if (!(supported & JO_OUT_IO))
4017 break;
4018 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4019 opt->jo_io_name[part] =
4020 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4021 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004022 else if (STRCMP(hi->hi_key, "in_buf") == 0
4023 || STRCMP(hi->hi_key, "out_buf") == 0
4024 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004025 {
4026 part = part_from_char(*hi->hi_key);
4027
4028 if (!(supported & JO_OUT_IO))
4029 break;
4030 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4031 opt->jo_io_buf[part] = get_tv_number(item);
4032 if (opt->jo_io_buf[part] <= 0)
4033 {
4034 EMSG2(_(e_invarg2), get_tv_string(item));
4035 return FAIL;
4036 }
4037 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4038 {
4039 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4040 return FAIL;
4041 }
4042 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004043 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4044 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4045 {
4046 part = part_from_char(*hi->hi_key);
4047
4048 if (!(supported & JO_OUT_IO))
4049 break;
4050 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4051 opt->jo_modifiable[part] = get_tv_number(item);
4052 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004053 else if (STRCMP(hi->hi_key, "in_top") == 0
4054 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004055 {
4056 linenr_T *lp;
4057
4058 if (!(supported & JO_OUT_IO))
4059 break;
4060 if (hi->hi_key[3] == 't')
4061 {
4062 lp = &opt->jo_in_top;
4063 opt->jo_set |= JO_IN_TOP;
4064 }
4065 else
4066 {
4067 lp = &opt->jo_in_bot;
4068 opt->jo_set |= JO_IN_BOT;
4069 }
4070 *lp = get_tv_number(item);
4071 if (*lp < 0)
4072 {
4073 EMSG2(_(e_invarg2), get_tv_string(item));
4074 return FAIL;
4075 }
4076 }
4077 else if (STRCMP(hi->hi_key, "channel") == 0)
4078 {
4079 if (!(supported & JO_OUT_IO))
4080 break;
4081 opt->jo_set |= JO_CHANNEL;
4082 if (item->v_type != VAR_CHANNEL)
4083 {
4084 EMSG2(_(e_invarg2), "channel");
4085 return FAIL;
4086 }
4087 opt->jo_channel = item->vval.v_channel;
4088 }
4089 else if (STRCMP(hi->hi_key, "callback") == 0)
4090 {
4091 if (!(supported & JO_CALLBACK))
4092 break;
4093 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004094 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004095 if (opt->jo_callback == NULL)
4096 {
4097 EMSG2(_(e_invarg2), "callback");
4098 return FAIL;
4099 }
4100 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004101 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004102 {
4103 if (!(supported & JO_OUT_CALLBACK))
4104 break;
4105 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004106 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004107 if (opt->jo_out_cb == NULL)
4108 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004109 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004110 return FAIL;
4111 }
4112 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004113 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004114 {
4115 if (!(supported & JO_ERR_CALLBACK))
4116 break;
4117 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004118 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004119 if (opt->jo_err_cb == NULL)
4120 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004121 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004122 return FAIL;
4123 }
4124 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004125 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004126 {
4127 if (!(supported & JO_CLOSE_CALLBACK))
4128 break;
4129 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004130 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004131 if (opt->jo_close_cb == NULL)
4132 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004133 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004134 return FAIL;
4135 }
4136 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004137 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4138 {
4139 if (!(supported & JO_EXIT_CB))
4140 break;
4141 opt->jo_set |= JO_EXIT_CB;
4142 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4143 if (opt->jo_exit_cb == NULL)
4144 {
4145 EMSG2(_(e_invarg2), "exit_cb");
4146 return FAIL;
4147 }
4148 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004149 else if (STRCMP(hi->hi_key, "waittime") == 0)
4150 {
4151 if (!(supported & JO_WAITTIME))
4152 break;
4153 opt->jo_set |= JO_WAITTIME;
4154 opt->jo_waittime = get_tv_number(item);
4155 }
4156 else if (STRCMP(hi->hi_key, "timeout") == 0)
4157 {
4158 if (!(supported & JO_TIMEOUT))
4159 break;
4160 opt->jo_set |= JO_TIMEOUT;
4161 opt->jo_timeout = get_tv_number(item);
4162 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004163 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004164 {
4165 if (!(supported & JO_OUT_TIMEOUT))
4166 break;
4167 opt->jo_set |= JO_OUT_TIMEOUT;
4168 opt->jo_out_timeout = get_tv_number(item);
4169 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004170 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004171 {
4172 if (!(supported & JO_ERR_TIMEOUT))
4173 break;
4174 opt->jo_set |= JO_ERR_TIMEOUT;
4175 opt->jo_err_timeout = get_tv_number(item);
4176 }
4177 else if (STRCMP(hi->hi_key, "part") == 0)
4178 {
4179 if (!(supported & JO_PART))
4180 break;
4181 opt->jo_set |= JO_PART;
4182 val = get_tv_string(item);
4183 if (STRCMP(val, "err") == 0)
4184 opt->jo_part = PART_ERR;
4185 else
4186 {
4187 EMSG2(_(e_invarg2), val);
4188 return FAIL;
4189 }
4190 }
4191 else if (STRCMP(hi->hi_key, "id") == 0)
4192 {
4193 if (!(supported & JO_ID))
4194 break;
4195 opt->jo_set |= JO_ID;
4196 opt->jo_id = get_tv_number(item);
4197 }
4198 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4199 {
4200 if (!(supported & JO_STOPONEXIT))
4201 break;
4202 opt->jo_set |= JO_STOPONEXIT;
4203 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4204 opt->jo_soe_buf);
4205 if (opt->jo_stoponexit == NULL)
4206 {
4207 EMSG2(_(e_invarg2), "stoponexit");
4208 return FAIL;
4209 }
4210 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004211 else if (STRCMP(hi->hi_key, "block_write") == 0)
4212 {
4213 if (!(supported & JO_BLOCK_WRITE))
4214 break;
4215 opt->jo_set |= JO_BLOCK_WRITE;
4216 opt->jo_block_write = get_tv_number(item);
4217 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004218 else
4219 break;
4220 --todo;
4221 }
4222 if (todo > 0)
4223 {
4224 EMSG2(_(e_invarg2), hi->hi_key);
4225 return FAIL;
4226 }
4227
4228 return OK;
4229}
4230
4231/*
4232 * Get the channel from the argument.
4233 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004234 * When "check_open" is TRUE check that the channel can be used.
4235 * When "reading" is TRUE "check_open" considers typeahead useful.
4236 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004237 */
4238 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004239get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004240{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004241 channel_T *channel = NULL;
4242 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004243
4244 if (tv->v_type == VAR_JOB)
4245 {
4246 if (tv->vval.v_job != NULL)
4247 channel = tv->vval.v_job->jv_channel;
4248 }
4249 else if (tv->v_type == VAR_CHANNEL)
4250 {
4251 channel = tv->vval.v_channel;
4252 }
4253 else
4254 {
4255 EMSG2(_(e_invarg2), get_tv_string(tv));
4256 return NULL;
4257 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004258 if (channel != NULL && reading)
4259 has_readahead = channel_has_readahead(channel,
4260 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004261
Bram Moolenaar437905c2016-04-26 19:01:05 +02004262 if (check_open && (channel == NULL || (!channel_is_open(channel)
4263 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004264 {
4265 EMSG(_("E906: not an open channel"));
4266 return NULL;
4267 }
4268 return channel;
4269}
4270
4271static job_T *first_job = NULL;
4272
4273 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004274job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004275{
4276 ch_log(job->jv_channel, "Freeing job");
4277 if (job->jv_channel != NULL)
4278 {
4279 /* The link from the channel to the job doesn't count as a reference,
4280 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004281 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004282 * NULL the reference. We don't set ch_job_killed, unreferencing the
4283 * job doesn't mean it stops running. */
4284 job->jv_channel->ch_job = NULL;
4285 channel_unref(job->jv_channel);
4286 }
4287 mch_clear_job(job);
4288
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004289 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004290 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004291}
4292
4293 static void
4294job_free_job(job_T *job)
4295{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004296 if (job->jv_next != NULL)
4297 job->jv_next->jv_prev = job->jv_prev;
4298 if (job->jv_prev == NULL)
4299 first_job = job->jv_next;
4300 else
4301 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004302 vim_free(job);
4303}
4304
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004305 static void
4306job_free(job_T *job)
4307{
4308 if (!in_free_unref_items)
4309 {
4310 job_free_contents(job);
4311 job_free_job(job);
4312 }
4313}
4314
Bram Moolenaar655da312016-05-28 22:22:34 +02004315#if defined(EXITFREE) || defined(PROTO)
4316 void
4317job_free_all(void)
4318{
4319 while (first_job != NULL)
4320 job_free(first_job);
4321}
4322#endif
4323
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004324/*
4325 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004326 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4327 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004328 */
4329 static int
4330job_still_useful(job_T *job)
4331{
4332 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004333 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4334 || (job->jv_channel != NULL
4335 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004336}
4337
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004338/*
4339 * Mark references in jobs that are still useful.
4340 */
4341 int
4342set_ref_in_job(int copyID)
4343{
4344 int abort = FALSE;
4345 job_T *job;
4346 typval_T tv;
4347
4348 for (job = first_job; job != NULL; job = job->jv_next)
4349 if (job_still_useful(job))
4350 {
4351 tv.v_type = VAR_JOB;
4352 tv.vval.v_job = job;
4353 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4354 }
4355 return abort;
4356}
4357
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004358 void
4359job_unref(job_T *job)
4360{
4361 if (job != NULL && --job->jv_refcount <= 0)
4362 {
4363 /* Do not free the job when it has not ended yet and there is a
4364 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004365 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004366 {
4367 job_free(job);
4368 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004369 else if (job->jv_channel != NULL
4370 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004371 {
4372 /* Do remove the link to the channel, otherwise it hangs
4373 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004374 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004375 job->jv_channel->ch_job = NULL;
4376 channel_unref(job->jv_channel);
4377 job->jv_channel = NULL;
4378 }
4379 }
4380}
4381
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004382 int
4383free_unused_jobs_contents(int copyID, int mask)
4384{
4385 int did_free = FALSE;
4386 job_T *job;
4387
4388 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004389 if ((job->jv_copyID & mask) != (copyID & mask)
4390 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004391 {
4392 /* Free the channel and ordinary items it contains, but don't
4393 * recurse into Lists, Dictionaries etc. */
4394 job_free_contents(job);
4395 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004396 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004397 return did_free;
4398}
4399
4400 void
4401free_unused_jobs(int copyID, int mask)
4402{
4403 job_T *job;
4404 job_T *job_next;
4405
4406 for (job = first_job; job != NULL; job = job_next)
4407 {
4408 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004409 if ((job->jv_copyID & mask) != (copyID & mask)
4410 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004411 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004412 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004413 job_free_job(job);
4414 }
4415 }
4416}
4417
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004418/*
4419 * Allocate a job. Sets the refcount to one and sets options default.
4420 */
4421 static job_T *
4422job_alloc(void)
4423{
4424 job_T *job;
4425
4426 job = (job_T *)alloc_clear(sizeof(job_T));
4427 if (job != NULL)
4428 {
4429 job->jv_refcount = 1;
4430 job->jv_stoponexit = vim_strsave((char_u *)"term");
4431
4432 if (first_job != NULL)
4433 {
4434 first_job->jv_prev = job;
4435 job->jv_next = first_job;
4436 }
4437 first_job = job;
4438 }
4439 return job;
4440}
4441
4442 void
4443job_set_options(job_T *job, jobopt_T *opt)
4444{
4445 if (opt->jo_set & JO_STOPONEXIT)
4446 {
4447 vim_free(job->jv_stoponexit);
4448 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4449 job->jv_stoponexit = NULL;
4450 else
4451 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4452 }
4453 if (opt->jo_set & JO_EXIT_CB)
4454 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004455 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004456 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004457 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004458 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004459 job->jv_exit_partial = NULL;
4460 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004461 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004462 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004463 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004464 job->jv_exit_partial = opt->jo_exit_partial;
4465 if (job->jv_exit_partial != NULL)
4466 ++job->jv_exit_partial->pt_refcount;
4467 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004468 }
4469}
4470
4471/*
4472 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4473 */
4474 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004475job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004476{
4477 job_T *job;
4478
4479 for (job = first_job; job != NULL; job = job->jv_next)
4480 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4481 mch_stop_job(job, job->jv_stoponexit);
4482}
4483
4484/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004485 * Return TRUE when there is any job that might exit, which means
4486 * job_check_ended() should be called once in a while.
4487 */
4488 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004489has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004490{
4491 job_T *job;
4492
4493 for (job = first_job; job != NULL; job = job->jv_next)
4494 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4495 return TRUE;
4496 return FALSE;
4497}
4498
4499/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004500 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004501 */
4502 void
4503job_check_ended(void)
4504{
4505 static time_t last_check = 0;
4506 time_t now;
4507 job_T *job;
4508 job_T *next;
4509
4510 /* Only do this once in 10 seconds. */
4511 now = time(NULL);
4512 if (last_check + 10 < now)
4513 {
4514 last_check = now;
4515 for (job = first_job; job != NULL; job = next)
4516 {
4517 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004518 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004519 job_status(job); /* may free "job" */
4520 }
4521 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004522 if (channel_need_redraw)
4523 {
4524 channel_need_redraw = FALSE;
4525 redraw_after_callback();
4526 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004527}
4528
4529/*
4530 * "job_start()" function
4531 */
4532 job_T *
4533job_start(typval_T *argvars)
4534{
4535 job_T *job;
4536 char_u *cmd = NULL;
4537#if defined(UNIX)
4538# define USE_ARGV
4539 char **argv = NULL;
4540 int argc = 0;
4541#else
4542 garray_T ga;
4543#endif
4544 jobopt_T opt;
4545 int part;
4546
4547 job = job_alloc();
4548 if (job == NULL)
4549 return NULL;
4550
4551 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004552#ifndef USE_ARGV
4553 ga_init2(&ga, (int)sizeof(char*), 20);
4554#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004555
4556 /* Default mode is NL. */
4557 clear_job_options(&opt);
4558 opt.jo_mode = MODE_NL;
4559 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004560 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4561 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004562 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004563
4564 /* Check that when io is "file" that there is a file name. */
4565 for (part = PART_OUT; part <= PART_IN; ++part)
4566 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4567 && opt.jo_io[part] == JIO_FILE
4568 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4569 || *opt.jo_io_name[part] == NUL))
4570 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004571 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004572 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004573 }
4574
4575 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4576 {
4577 buf_T *buf = NULL;
4578
4579 /* check that we can find the buffer before starting the job */
4580 if (opt.jo_set & JO_IN_BUF)
4581 {
4582 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4583 if (buf == NULL)
4584 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4585 }
4586 else if (!(opt.jo_set & JO_IN_NAME))
4587 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004588 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004589 }
4590 else
4591 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4592 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004593 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004594 if (buf->b_ml.ml_mfp == NULL)
4595 {
4596 char_u numbuf[NUMBUFLEN];
4597 char_u *s;
4598
4599 if (opt.jo_set & JO_IN_BUF)
4600 {
4601 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4602 s = numbuf;
4603 }
4604 else
4605 s = opt.jo_io_name[PART_IN];
4606 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004607 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004608 }
4609 job->jv_in_buf = buf;
4610 }
4611
4612 job_set_options(job, &opt);
4613
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004614 if (argvars[0].v_type == VAR_STRING)
4615 {
4616 /* Command is a string. */
4617 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004618 if (cmd == NULL || *cmd == NUL)
4619 {
4620 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004621 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004622 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004623#ifdef USE_ARGV
4624 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004625 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004626 argv[argc] = NULL;
4627#endif
4628 }
4629 else if (argvars[0].v_type != VAR_LIST
4630 || argvars[0].vval.v_list == NULL
4631 || argvars[0].vval.v_list->lv_len < 1)
4632 {
4633 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004634 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004635 }
4636 else
4637 {
4638 list_T *l = argvars[0].vval.v_list;
4639 listitem_T *li;
4640 char_u *s;
4641
4642#ifdef USE_ARGV
4643 /* Pass argv[] to mch_call_shell(). */
4644 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4645 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004646 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004647#endif
4648 for (li = l->lv_first; li != NULL; li = li->li_next)
4649 {
4650 s = get_tv_string_chk(&li->li_tv);
4651 if (s == NULL)
4652 goto theend;
4653#ifdef USE_ARGV
4654 argv[argc++] = (char *)s;
4655#else
4656 /* Only escape when needed, double quotes are not always allowed. */
4657 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4658 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004659# ifdef WIN32
4660 int old_ssl = p_ssl;
4661
4662 /* This is using CreateProcess, not cmd.exe. Always use
4663 * double quote and backslashes. */
4664 p_ssl = 0;
4665# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004666 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004667# ifdef WIN32
4668 p_ssl = old_ssl;
4669# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004670 if (s == NULL)
4671 goto theend;
4672 ga_concat(&ga, s);
4673 vim_free(s);
4674 }
4675 else
4676 ga_concat(&ga, s);
4677 if (li->li_next != NULL)
4678 ga_append(&ga, ' ');
4679#endif
4680 }
4681#ifdef USE_ARGV
4682 argv[argc] = NULL;
4683#else
4684 cmd = ga.ga_data;
4685#endif
4686 }
4687
4688#ifdef USE_ARGV
4689 if (ch_log_active())
4690 {
4691 garray_T ga;
4692 int i;
4693
4694 ga_init2(&ga, (int)sizeof(char), 200);
4695 for (i = 0; i < argc; ++i)
4696 {
4697 if (i > 0)
4698 ga_concat(&ga, (char_u *)" ");
4699 ga_concat(&ga, (char_u *)argv[i]);
4700 }
4701 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4702 ga_clear(&ga);
4703 }
4704 mch_start_job(argv, job, &opt);
4705#else
4706 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4707 mch_start_job((char *)cmd, job, &opt);
4708#endif
4709
4710 /* If the channel is reading from a buffer, write lines now. */
4711 if (job->jv_channel != NULL)
4712 channel_write_in(job->jv_channel);
4713
4714theend:
4715#ifdef USE_ARGV
4716 vim_free(argv);
4717#else
4718 vim_free(ga.ga_data);
4719#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004720 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004721 return job;
4722}
4723
4724/*
4725 * Get the status of "job" and invoke the exit callback when needed.
4726 * The returned string is not allocated.
4727 */
4728 char *
4729job_status(job_T *job)
4730{
4731 char *result;
4732
4733 if (job->jv_status == JOB_ENDED)
4734 /* No need to check, dead is dead. */
4735 result = "dead";
4736 else if (job->jv_status == JOB_FAILED)
4737 result = "fail";
4738 else
4739 {
4740 result = mch_job_status(job);
4741 if (job->jv_status == JOB_ENDED)
4742 ch_log(job->jv_channel, "Job ended");
4743 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4744 {
4745 typval_T argv[3];
4746 typval_T rettv;
4747 int dummy;
4748
4749 /* invoke the exit callback; make sure the refcount is > 0 */
4750 ++job->jv_refcount;
4751 argv[0].v_type = VAR_JOB;
4752 argv[0].vval.v_job = job;
4753 argv[1].v_type = VAR_NUMBER;
4754 argv[1].vval.v_number = job->jv_exitval;
4755 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004756 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4757 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004758 clear_tv(&rettv);
4759 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004760 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004761 }
4762 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4763 {
4764 /* The job was already unreferenced, now that it ended it can be
4765 * freed. Careful: caller must not use "job" after this! */
4766 job_free(job);
4767 }
4768 }
4769 return result;
4770}
4771
Bram Moolenaar8950a562016-03-12 15:22:55 +01004772/*
4773 * Implementation of job_info().
4774 */
4775 void
4776job_info(job_T *job, dict_T *dict)
4777{
4778 dictitem_T *item;
4779 varnumber_T nr;
4780
4781 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4782
4783 item = dictitem_alloc((char_u *)"channel");
4784 if (item == NULL)
4785 return;
4786 item->di_tv.v_lock = 0;
4787 item->di_tv.v_type = VAR_CHANNEL;
4788 item->di_tv.vval.v_channel = job->jv_channel;
4789 if (job->jv_channel != NULL)
4790 ++job->jv_channel->ch_refcount;
4791 if (dict_add(dict, item) == FAIL)
4792 dictitem_free(item);
4793
4794#ifdef UNIX
4795 nr = job->jv_pid;
4796#else
4797 nr = job->jv_proc_info.dwProcessId;
4798#endif
4799 dict_add_nr_str(dict, "process", nr, NULL);
4800
4801 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004802 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004803 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4804}
4805
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004806 int
4807job_stop(job_T *job, typval_T *argvars)
4808{
4809 char_u *arg;
4810
4811 if (argvars[1].v_type == VAR_UNKNOWN)
4812 arg = (char_u *)"";
4813 else
4814 {
4815 arg = get_tv_string_chk(&argvars[1]);
4816 if (arg == NULL)
4817 {
4818 EMSG(_(e_invarg));
4819 return 0;
4820 }
4821 }
4822 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4823 if (mch_stop_job(job, arg) == FAIL)
4824 return 0;
4825
4826 /* Assume that "hup" does not kill the job. */
4827 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4828 job->jv_channel->ch_job_killed = TRUE;
4829
4830 /* We don't try freeing the job, obviously the caller still has a
4831 * reference to it. */
4832 return 1;
4833}
4834
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004835#endif /* FEAT_JOB_CHANNEL */