blob: 1c93b6c50ef562a09ce11d6c0d83386cc1427608 [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. */
365 if (channel->ch_part[PART_IN].ch_buffer != NULL)
366 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
382 || channel->ch_part[PART_OUT].ch_buffer) && has_out_msg)
383 || ((channel->ch_part[PART_ERR].ch_callback != NULL
384 || channel->ch_part[PART_ERR].ch_buffer) && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100385}
386
387/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200388 * Close a channel and free all its resources.
389 */
390 static void
391channel_free_contents(channel_T *channel)
392{
393 channel_close(channel, TRUE);
394 channel_clear(channel);
395 ch_log(channel, "Freeing channel");
396}
397
398 static void
399channel_free_channel(channel_T *channel)
400{
401 if (channel->ch_next != NULL)
402 channel->ch_next->ch_prev = channel->ch_prev;
403 if (channel->ch_prev == NULL)
404 first_channel = channel->ch_next;
405 else
406 channel->ch_prev->ch_next = channel->ch_next;
407 vim_free(channel);
408}
409
410 static void
411channel_free(channel_T *channel)
412{
413 if (!in_free_unref_items)
414 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200415 if (safe_to_invoke_callback == 0)
416 {
417 channel->ch_to_be_freed = TRUE;
418 }
419 else
420 {
421 channel_free_contents(channel);
422 channel_free_channel(channel);
423 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200424 }
425}
426
427/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100428 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100429 * possible, there is no callback to be invoked or the associated job was
430 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100431 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100432 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100433 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434channel_may_free(channel_T *channel)
435{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100436 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100437 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100438 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100439 return TRUE;
440 }
441 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100442}
443
444/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100445 * Decrement the reference count on "channel" and maybe free it when it goes
446 * down to zero. Don't free it if there is a pending action.
447 * Returns TRUE when the channel is no longer referenced.
448 */
449 int
450channel_unref(channel_T *channel)
451{
452 if (channel != NULL && --channel->ch_refcount <= 0)
453 return channel_may_free(channel);
454 return FALSE;
455}
456
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200457 int
458free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100459{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200460 int did_free = FALSE;
461 channel_T *ch;
462
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200463 /* This is invoked from the garbage collector, which only runs at a safe
464 * point. */
465 ++safe_to_invoke_callback;
466
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200467 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200468 if (!channel_still_useful(ch)
469 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200470 {
471 /* Free the channel and ordinary items it contains, but don't
472 * recurse into Lists, Dictionaries etc. */
473 channel_free_contents(ch);
474 did_free = TRUE;
475 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200476
477 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200478 return did_free;
479}
480
481 void
482free_unused_channels(int copyID, int mask)
483{
484 channel_T *ch;
485 channel_T *ch_next;
486
487 for (ch = first_channel; ch != NULL; ch = ch_next)
488 {
489 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200490 if (!channel_still_useful(ch)
491 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200492 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200493 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200494 channel_free_channel(ch);
495 }
496 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100497}
498
Bram Moolenaard04a0202016-01-26 23:30:18 +0100499#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100500
501#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
502 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100503channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100504{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100505 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100506 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100507
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100508 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100509 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100510 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100511 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200512 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100513}
514#endif
515
Bram Moolenaare0874f82016-01-24 20:36:41 +0100516/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100517 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100518 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100519#ifdef FEAT_GUI_X11
520 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200521messageFromServer(XtPointer clientData,
522 int *unused1 UNUSED,
523 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100524{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100525 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100526}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100527#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100528
Bram Moolenaard04a0202016-01-26 23:30:18 +0100529#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100530# if GTK_CHECK_VERSION(3,0,0)
531 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200532messageFromServer(GIOChannel *unused1 UNUSED,
533 GIOCondition unused2 UNUSED,
534 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100535{
536 channel_read_fd(GPOINTER_TO_INT(clientData));
537 return TRUE; /* Return FALSE instead in case the event source is to
538 * be removed after this function returns. */
539}
540# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100541 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200542messageFromServer(gpointer clientData,
543 gint unused1 UNUSED,
544 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100545{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100546 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100547}
Bram Moolenaar98921892016-02-23 17:14:37 +0100548# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100549#endif
550
551 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100552channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100553{
Bram Moolenaarde279892016-03-11 22:19:44 +0100554 if (!CH_HAS_GUI)
555 return;
556
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100557# ifdef FEAT_GUI_X11
558 /* Tell notifier we are interested in being called
559 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100560 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
561 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100562 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100563 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100564 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200565 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100566 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100567# else
568# ifdef FEAT_GUI_GTK
569 /* Tell gdk we are interested in being called when there
570 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100571 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100572# if GTK_CHECK_VERSION(3,0,0)
573 {
574 GIOChannel *chnnl = g_io_channel_unix_new(
575 (gint)channel->ch_part[part].ch_fd);
576
577 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
578 chnnl,
579 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200580 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100581 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
582
583 g_io_channel_unref(chnnl);
584 }
585# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100586 channel->ch_part[part].ch_inputHandler = gdk_input_add(
587 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100588 (GdkInputCondition)
589 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200590 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100591 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100592# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100593# endif
594# endif
595}
596
Bram Moolenaarde279892016-03-11 22:19:44 +0100597 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100598channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100599{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100600 if (channel->CH_SOCK_FD != INVALID_FD)
601 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100602 if (channel->CH_OUT_FD != INVALID_FD)
603 channel_gui_register_one(channel, PART_OUT);
604 if (channel->CH_ERR_FD != INVALID_FD)
605 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100606}
607
608/*
609 * Register any of our file descriptors with the GUI event handling system.
610 * Called when the GUI has started.
611 */
612 void
613channel_gui_register_all(void)
614{
Bram Moolenaar77073442016-02-13 23:23:53 +0100615 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100616
Bram Moolenaar77073442016-02-13 23:23:53 +0100617 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100618 channel_gui_register(channel);
619}
620
621 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100622channel_gui_unregister_one(channel_T *channel, int part)
623{
624# ifdef FEAT_GUI_X11
625 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
626 {
627 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
628 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
629 }
630# else
631# ifdef FEAT_GUI_GTK
632 if (channel->ch_part[part].ch_inputHandler != 0)
633 {
634# if GTK_CHECK_VERSION(3,0,0)
635 g_source_remove(channel->ch_part[part].ch_inputHandler);
636# else
637 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
638# endif
639 channel->ch_part[part].ch_inputHandler = 0;
640 }
641# endif
642# endif
643}
644
645 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100646channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100647{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100648 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100649
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100650 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100651 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100652}
653
654#endif
655
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100656static char *e_cannot_connect = N_("E902: Cannot connect to port");
657
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100659 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100660 * "waittime" is the time in msec to wait for the connection.
661 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100662 * Returns the channel for success.
663 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100664 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100665 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100666channel_open(
667 char *hostname,
668 int port_in,
669 int waittime,
670 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100671{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100672 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100673 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100674 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100675#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100676 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100677 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100678#else
679 int port = port_in;
680#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100681 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100682 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100683
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100684#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100685 channel_init_winsock();
686#endif
687
Bram Moolenaar77073442016-02-13 23:23:53 +0100688 channel = add_channel();
689 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100690 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100691 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100692 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693 }
694
695 /* Get the server internet address and put into addr structure */
696 /* fill in the socket address structure and connect to server */
697 vim_memset((char *)&server, 0, sizeof(server));
698 server.sin_family = AF_INET;
699 server.sin_port = htons(port);
700 if ((host = gethostbyname(hostname)) == NULL)
701 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100702 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100703 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100704 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100705 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100706 }
707 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
708
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100709 /* On Mac and Solaris a zero timeout almost never works. At least wait
710 * one millisecond. Let's do it for all systems, because we don't know why
711 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100712 if (waittime == 0)
713 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100714
715 /*
716 * For Unix we need to call connect() again after connect() failed.
717 * On Win32 one time is sufficient.
718 */
719 while (TRUE)
720 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100721 long elapsed_msec = 0;
722 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100723
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100724 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100725 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100726 sd = socket(AF_INET, SOCK_STREAM, 0);
727 if (sd == -1)
728 {
729 ch_error(channel, "in socket() in channel_open().");
730 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100731 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100732 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100733 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100734
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100735 if (waittime >= 0)
736 {
737 /* Make connect() non-blocking. */
738 if (
739#ifdef _WIN32
740 ioctlsocket(sd, FIONBIO, &val) < 0
741#else
742 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
743#endif
744 )
745 {
746 SOCK_ERRNO;
747 ch_errorn(channel,
748 "channel_open: Connect failed with errno %d", errno);
749 sock_close(sd);
750 channel_free(channel);
751 return NULL;
752 }
753 }
754
755 /* Try connecting to the server. */
756 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
757 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
758
Bram Moolenaar045a2842016-03-08 22:33:07 +0100759 if (ret == 0)
760 /* The connection could be established. */
761 break;
762
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100763 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100764 if (waittime < 0 || (errno != EWOULDBLOCK
765 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100766#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100767 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100768#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100769 ))
770 {
771 ch_errorn(channel,
772 "channel_open: Connect failed with errno %d", errno);
773 PERROR(_(e_cannot_connect));
774 sock_close(sd);
775 channel_free(channel);
776 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100777 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100778
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100779 /* Limit the waittime to 50 msec. If it doesn't work within this
780 * time we close the socket and try creating it again. */
781 waitnow = waittime > 50 ? 50 : waittime;
782
Bram Moolenaar045a2842016-03-08 22:33:07 +0100783 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100784 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100785#ifndef WIN32
786 if (errno != ECONNREFUSED)
787#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100788 {
789 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100790 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100791 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100792#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100793 int so_error = 0;
794 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100795 struct timeval start_tv;
796 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100797#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100798 FD_ZERO(&rfds);
799 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100800 FD_ZERO(&wfds);
801 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100802
Bram Moolenaar562ca712016-03-09 21:50:05 +0100803 tv.tv_sec = waitnow / 1000;
804 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100805#ifndef WIN32
806 gettimeofday(&start_tv, NULL);
807#endif
808 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100809 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100810 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100811
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100812 if (ret < 0)
813 {
814 SOCK_ERRNO;
815 ch_errorn(channel,
816 "channel_open: Connect failed with errno %d", errno);
817 PERROR(_(e_cannot_connect));
818 sock_close(sd);
819 channel_free(channel);
820 return NULL;
821 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100822
Bram Moolenaare081e212016-02-28 22:33:46 +0100823#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100824 /* On Win32: select() is expected to work and wait for up to
825 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100826 if (FD_ISSET(sd, &wfds))
827 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100828 elapsed_msec = waitnow;
829 if (waittime > 1 && elapsed_msec < waittime)
830 {
831 waittime -= elapsed_msec;
832 continue;
833 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100834#else
835 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100836 * After putting the socket in non-blocking mode, connect() will
837 * return EINPROGRESS, select() will not wait (as if writing is
838 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100839 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100840 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100841 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100842 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100843 {
844 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100845 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100846 if (ret < 0 || (so_error != 0
847 && so_error != EWOULDBLOCK
848 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100849# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100850 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100851# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100852 ))
853 {
854 ch_errorn(channel,
855 "channel_open: Connect failed with errno %d",
856 so_error);
857 PERROR(_(e_cannot_connect));
858 sock_close(sd);
859 channel_free(channel);
860 return NULL;
861 }
862 }
863
Bram Moolenaar045a2842016-03-08 22:33:07 +0100864 if (FD_ISSET(sd, &wfds) && so_error == 0)
865 /* Did not detect an error, connection is established. */
866 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100867
Bram Moolenaar045a2842016-03-08 22:33:07 +0100868 gettimeofday(&end_tv, NULL);
869 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
870 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100871#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100872 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100873
874#ifndef WIN32
875 if (waittime > 1 && elapsed_msec < waittime)
876 {
877 /* The port isn't ready but we also didn't get an error.
878 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100879 * yet. Select() may return early, wait until the remaining
880 * "waitnow" and try again. */
881 waitnow -= elapsed_msec;
882 waittime -= elapsed_msec;
883 if (waitnow > 0)
884 {
885 mch_delay((long)waitnow, TRUE);
886 ui_breakcheck();
887 waittime -= waitnow;
888 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100889 if (!got_int)
890 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100891 if (waittime <= 0)
892 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100893 waittime = 1;
894 continue;
895 }
896 /* we were interrupted, behave as if timed out */
897 }
898#endif
899
900 /* We timed out. */
901 ch_error(channel, "Connection timed out");
902 sock_close(sd);
903 channel_free(channel);
904 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100905 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100906
Bram Moolenaar045a2842016-03-08 22:33:07 +0100907 ch_log(channel, "Connection made");
908
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100909 if (waittime >= 0)
910 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100911#ifdef _WIN32
912 val = 0;
913 ioctlsocket(sd, FIONBIO, &val);
914#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100915 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100916#endif
917 }
918
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100919 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100920 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100921 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
922 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100923
924#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100925 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100926#endif
927
Bram Moolenaar77073442016-02-13 23:23:53 +0100928 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100929}
930
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100931/*
932 * Implements ch_open().
933 */
934 channel_T *
935channel_open_func(typval_T *argvars)
936{
937 char_u *address;
938 char_u *p;
939 char *rest;
940 int port;
941 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200942 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100943
944 address = get_tv_string(&argvars[0]);
945 if (argvars[1].v_type != VAR_UNKNOWN
946 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
947 {
948 EMSG(_(e_invarg));
949 return NULL;
950 }
951
952 /* parse address */
953 p = vim_strchr(address, ':');
954 if (p == NULL)
955 {
956 EMSG2(_(e_invarg2), address);
957 return NULL;
958 }
959 *p++ = NUL;
960 port = strtol((char *)p, &rest, 10);
961 if (*address == NUL || port <= 0 || *rest != NUL)
962 {
963 p[-1] = ':';
964 EMSG2(_(e_invarg2), address);
965 return NULL;
966 }
967
968 /* parse options */
969 clear_job_options(&opt);
970 opt.jo_mode = MODE_JSON;
971 opt.jo_timeout = 2000;
972 if (get_job_options(&argvars[1], &opt,
973 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200974 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100975 if (opt.jo_timeout < 0)
976 {
977 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200978 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100979 }
980
981 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
982 if (channel != NULL)
983 {
984 opt.jo_set = JO_ALL;
985 channel_set_options(channel, &opt);
986 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200987theend:
988 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100989 return channel;
990}
991
Bram Moolenaarde279892016-03-11 22:19:44 +0100992 static void
993may_close_part(sock_T *fd)
994{
995 if (*fd != INVALID_FD)
996 {
997 fd_close(*fd);
998 *fd = INVALID_FD;
999 }
1000}
1001
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001002 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001003channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001004{
Bram Moolenaarde279892016-03-11 22:19:44 +01001005 if (in != INVALID_FD)
1006 {
1007 may_close_part(&channel->CH_IN_FD);
1008 channel->CH_IN_FD = in;
1009 }
1010 if (out != INVALID_FD)
1011 {
1012# if defined(FEAT_GUI)
1013 channel_gui_unregister_one(channel, PART_OUT);
1014# endif
1015 may_close_part(&channel->CH_OUT_FD);
1016 channel->CH_OUT_FD = out;
1017# if defined(FEAT_GUI)
1018 channel_gui_register_one(channel, PART_OUT);
1019# endif
1020 }
1021 if (err != INVALID_FD)
1022 {
1023# if defined(FEAT_GUI)
1024 channel_gui_unregister_one(channel, PART_ERR);
1025# endif
1026 may_close_part(&channel->CH_ERR_FD);
1027 channel->CH_ERR_FD = err;
1028# if defined(FEAT_GUI)
1029 channel_gui_register_one(channel, PART_ERR);
1030# endif
1031 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001032}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001033
Bram Moolenaard6051b52016-02-28 15:49:03 +01001034/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001035 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001036 * This does not keep a refcount, when the job is freed ch_job is cleared.
1037 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001038 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001039channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001040{
Bram Moolenaar77073442016-02-13 23:23:53 +01001041 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001042
1043 channel_set_options(channel, options);
1044
1045 if (job->jv_in_buf != NULL)
1046 {
1047 chanpart_T *in_part = &channel->ch_part[PART_IN];
1048
1049 in_part->ch_buffer = job->jv_in_buf;
1050 ch_logs(channel, "reading from buffer '%s'",
1051 (char *)in_part->ch_buffer->b_ffname);
1052 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001053 {
1054 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1055 {
1056 /* Special mode: send last-but-one line when appending a line
1057 * to the buffer. */
1058 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001059 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001060 in_part->ch_buf_top =
1061 in_part->ch_buffer->b_ml.ml_line_count + 1;
1062 }
1063 else
1064 in_part->ch_buf_top = options->jo_in_top;
1065 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001066 else
1067 in_part->ch_buf_top = 1;
1068 if (options->jo_set & JO_IN_BOT)
1069 in_part->ch_buf_bot = options->jo_in_bot;
1070 else
1071 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1072 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001073}
1074
1075/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001076 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001077 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001078 */
1079 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001080find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001081{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001082 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001083 buf_T *save_curbuf = curbuf;
1084
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001085 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001086 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001087 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001088 if (buf == NULL)
1089 buf = buflist_findname_exp(name);
1090 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001091 if (buf == NULL)
1092 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001093 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001094 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001095 if (buf == NULL)
1096 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001097 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001098 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001099#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001100 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1101 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001102#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001103 if (curbuf->b_ml.ml_mfp == NULL)
1104 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001105 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1106 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001107 changed_bytes(1, 0);
1108 curbuf = save_curbuf;
1109 }
1110
1111 return buf;
1112}
1113
1114/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001115 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001116 */
1117 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001118channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001119{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001120 int part;
1121 char_u **cbp;
1122 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001123
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001124 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001125 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001126 channel->ch_part[part].ch_mode = opt->jo_mode;
1127 if (opt->jo_set & JO_IN_MODE)
1128 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1129 if (opt->jo_set & JO_OUT_MODE)
1130 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1131 if (opt->jo_set & JO_ERR_MODE)
1132 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001133
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001134 if (opt->jo_set & JO_TIMEOUT)
1135 for (part = PART_SOCK; part <= PART_IN; ++part)
1136 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1137 if (opt->jo_set & JO_OUT_TIMEOUT)
1138 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1139 if (opt->jo_set & JO_ERR_TIMEOUT)
1140 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001141 if (opt->jo_set & JO_BLOCK_WRITE)
1142 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001143
1144 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001145 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001146 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001147 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001148 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001149 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001150 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1151 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001152 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001153 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001154 *pp = opt->jo_partial;
1155 if (*pp != NULL)
1156 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001157 }
1158 if (opt->jo_set & JO_OUT_CALLBACK)
1159 {
1160 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001161 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001162 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001163 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001164 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1165 *cbp = vim_strsave(opt->jo_out_cb);
1166 else
1167 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001168 *pp = opt->jo_out_partial;
1169 if (*pp != NULL)
1170 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001171 }
1172 if (opt->jo_set & JO_ERR_CALLBACK)
1173 {
1174 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001175 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001176 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001177 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001178 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1179 *cbp = vim_strsave(opt->jo_err_cb);
1180 else
1181 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001182 *pp = opt->jo_err_partial;
1183 if (*pp != NULL)
1184 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001185 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001186 if (opt->jo_set & JO_CLOSE_CALLBACK)
1187 {
1188 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001189 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001190 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001191 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001192 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1193 *cbp = vim_strsave(opt->jo_close_cb);
1194 else
1195 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001196 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001197 if (*pp != NULL)
1198 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001199 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001200
1201 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1202 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001203 buf_T *buf;
1204
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001205 /* writing output to a buffer. Default mode is NL. */
1206 if (!(opt->jo_set & JO_OUT_MODE))
1207 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001208 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001209 {
1210 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1211 if (buf == NULL)
1212 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1213 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001214 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001215 {
1216 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1217 }
1218 if (buf != NULL)
1219 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001220 if (opt->jo_set & JO_OUT_MODIFIABLE)
1221 channel->ch_part[PART_OUT].ch_nomodifiable =
1222 !opt->jo_modifiable[PART_OUT];
1223
1224 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1225 {
1226 EMSG(_(e_modifiable));
1227 }
1228 else
1229 {
1230 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001231 (char *)buf->b_ffname);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001232 channel->ch_part[PART_OUT].ch_buffer = buf;
1233 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001234 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001235 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001236
1237 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1238 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1239 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1240 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001241 buf_T *buf;
1242
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001243 /* writing err to a buffer. Default mode is NL. */
1244 if (!(opt->jo_set & JO_ERR_MODE))
1245 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1246 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001247 buf = channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001248 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001249 {
1250 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1251 if (buf == NULL)
1252 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1253 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001254 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001255 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1256 if (buf != NULL)
1257 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001258 if (opt->jo_set & JO_ERR_MODIFIABLE)
1259 channel->ch_part[PART_ERR].ch_nomodifiable =
1260 !opt->jo_modifiable[PART_ERR];
1261 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1262 {
1263 EMSG(_(e_modifiable));
1264 }
1265 else
1266 {
1267 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001268 (char *)buf->b_ffname);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001269 channel->ch_part[PART_ERR].ch_buffer = buf;
1270 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001271 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001272 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001273
1274 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1275 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1276 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001277}
1278
1279/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001280 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001281 */
1282 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001283channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001284 channel_T *channel,
1285 int part,
1286 char_u *callback,
1287 partial_T *partial,
1288 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001289{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001290 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001291 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1292
1293 if (item != NULL)
1294 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001295 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001296 item->cq_partial = partial;
1297 if (partial != NULL)
1298 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001299 item->cq_seq_nr = id;
1300 item->cq_prev = head->cq_prev;
1301 head->cq_prev = item;
1302 item->cq_next = NULL;
1303 if (item->cq_prev == NULL)
1304 head->cq_next = item;
1305 else
1306 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001307 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001308}
1309
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001310 static void
1311write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1312{
1313 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001314 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001315 char_u *p;
1316
Bram Moolenaar655da312016-05-28 22:22:34 +02001317 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001318 if ((p = alloc(len + 2)) == NULL)
1319 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001320 memcpy((char *)p, (char *)line, len);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001321 p[len] = NL;
1322 p[len + 1] = NUL;
1323 channel_send(channel, PART_IN, p, "write_buf_line()");
1324 vim_free(p);
1325}
1326
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001327/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001328 * Return TRUE if "channel" can be written to.
1329 * Returns FALSE if the input is closed or the write would block.
1330 */
1331 static int
1332can_write_buf_line(channel_T *channel)
1333{
1334 chanpart_T *in_part = &channel->ch_part[PART_IN];
1335
1336 if (in_part->ch_fd == INVALID_FD)
1337 return FALSE; /* pipe was closed */
1338
1339 /* for testing: block every other attempt to write */
1340 if (in_part->ch_block_write == 1)
1341 in_part->ch_block_write = -1;
1342 else if (in_part->ch_block_write == -1)
1343 in_part->ch_block_write = 1;
1344
1345 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1346#ifndef WIN32
1347 {
1348# if defined(HAVE_SELECT)
1349 struct timeval tval;
1350 fd_set wfds;
1351 int ret;
1352
1353 FD_ZERO(&wfds);
1354 FD_SET((int)in_part->ch_fd, &wfds);
1355 tval.tv_sec = 0;
1356 tval.tv_usec = 0;
1357 for (;;)
1358 {
1359 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1360# ifdef EINTR
1361 SOCK_ERRNO;
1362 if (ret == -1 && errno == EINTR)
1363 continue;
1364# endif
1365 if (ret <= 0 || in_part->ch_block_write == 1)
1366 {
1367 if (ret > 0)
1368 ch_log(channel, "FAKED Input not ready for writing");
1369 else
1370 ch_log(channel, "Input not ready for writing");
1371 return FALSE;
1372 }
1373 break;
1374 }
1375# else
1376 struct pollfd fds;
1377
1378 fds.fd = in_part->ch_fd;
1379 fds.events = POLLOUT;
1380 if (poll(&fds, 1, 0) <= 0)
1381 {
1382 ch_log(channel, "Input not ready for writing");
1383 return FALSE;
1384 }
1385 if (in_part->ch_block_write == 1)
1386 {
1387 ch_log(channel, "FAKED Input not ready for writing");
1388 return FALSE;
1389 }
1390# endif
1391 }
1392#endif
1393 return TRUE;
1394}
1395
1396/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001397 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001398 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001399 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001400channel_write_in(channel_T *channel)
1401{
1402 chanpart_T *in_part = &channel->ch_part[PART_IN];
1403 linenr_T lnum;
1404 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001405 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001406
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001407 if (buf == NULL || in_part->ch_buf_append)
1408 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001409 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1410 {
1411 /* buffer was wiped out or unloaded */
1412 in_part->ch_buffer = NULL;
1413 return;
1414 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001415
1416 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1417 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1418 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001419 if (!can_write_buf_line(channel))
1420 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001421 write_buf_line(buf, lnum, channel);
1422 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001423 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001424
1425 if (written == 1)
1426 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1427 else if (written > 1)
1428 ch_logn(channel, "written %d lines to channel", written);
1429
Bram Moolenaar014069a2016-03-03 22:51:40 +01001430 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001431 if (lnum > buf->b_ml.ml_line_count)
1432 {
1433 /* Writing is done, no longer need the buffer. */
1434 in_part->ch_buffer = NULL;
1435 ch_log(channel, "Finished writing all lines to channel");
1436 }
1437 else
1438 ch_logn(channel, "Still %d more lines to write",
1439 buf->b_ml.ml_line_count - lnum + 1);
1440}
1441
1442/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001443 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001444 */
1445 void
1446channel_buffer_free(buf_T *buf)
1447{
1448 channel_T *channel;
1449 int part;
1450
1451 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1452 for (part = PART_SOCK; part <= PART_IN; ++part)
1453 {
1454 chanpart_T *ch_part = &channel->ch_part[part];
1455
1456 if (ch_part->ch_buffer == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001457 {
1458 ch_logs(channel, "%s buffer has been wiped out",
1459 part_names[part]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001460 ch_part->ch_buffer = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001461 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001462 }
1463}
1464
1465/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001466 * Write any lines waiting to be written to a channel.
1467 */
1468 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001469channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001470{
1471 channel_T *channel;
1472
1473 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1474 {
1475 chanpart_T *in_part = &channel->ch_part[PART_IN];
1476
1477 if (in_part->ch_buffer != NULL)
1478 {
1479 if (in_part->ch_buf_append)
1480 channel_write_new_lines(in_part->ch_buffer);
1481 else
1482 channel_write_in(channel);
1483 }
1484 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001485}
1486
1487/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001488 * Write appended lines above the last one in "buf" to the channel.
1489 */
1490 void
1491channel_write_new_lines(buf_T *buf)
1492{
1493 channel_T *channel;
1494 int found_one = FALSE;
1495
1496 /* There could be more than one channel for the buffer, loop over all of
1497 * them. */
1498 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1499 {
1500 chanpart_T *in_part = &channel->ch_part[PART_IN];
1501 linenr_T lnum;
1502 int written = 0;
1503
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001504 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001505 {
1506 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001507 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001508 found_one = TRUE;
1509 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1510 ++lnum)
1511 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001512 if (!can_write_buf_line(channel))
1513 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001514 write_buf_line(buf, lnum, channel);
1515 ++written;
1516 }
1517
1518 if (written == 1)
1519 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1520 else if (written > 1)
1521 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001522 if (lnum < buf->b_ml.ml_line_count)
1523 ch_logn(channel, "Still %d more lines to write",
1524 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001525
1526 in_part->ch_buf_bot = lnum;
1527 }
1528 }
1529 if (!found_one)
1530 buf->b_write_to_channel = FALSE;
1531}
1532
1533/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001534 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001535 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001536 */
1537 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001538invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1539 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001540{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001541 typval_T rettv;
1542 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001543
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001544 if (safe_to_invoke_callback == 0)
1545 EMSG("INTERNAL: Invoking callback when it is not safe");
1546
Bram Moolenaar77073442016-02-13 23:23:53 +01001547 argv[0].v_type = VAR_CHANNEL;
1548 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001549
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001550 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001551 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001552 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001553 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001554}
1555
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001556/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001557 * Return the first node from "channel"/"part" without removing it.
1558 * Returns NULL if there is nothing.
1559 */
1560 readq_T *
1561channel_peek(channel_T *channel, int part)
1562{
1563 readq_T *head = &channel->ch_part[part].ch_head;
1564
1565 return head->rq_next;
1566}
1567
1568/*
1569 * Return a pointer to the first NL in "node".
1570 * Skips over NUL characters.
1571 * Returns NULL if there is no NL.
1572 */
1573 char_u *
1574channel_first_nl(readq_T *node)
1575{
1576 char_u *buffer = node->rq_buffer;
1577 long_u i;
1578
1579 for (i = 0; i < node->rq_buflen; ++i)
1580 if (buffer[i] == NL)
1581 return buffer + i;
1582 return NULL;
1583}
1584
1585/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001586 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001587 * The caller must free it.
1588 * Returns NULL if there is nothing.
1589 */
1590 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001591channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001592{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001593 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001594 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001595 char_u *p;
1596
Bram Moolenaar77073442016-02-13 23:23:53 +01001597 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001598 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001599 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001600 p = node->rq_buffer;
1601 head->rq_next = node->rq_next;
1602 if (node->rq_next == NULL)
1603 head->rq_prev = NULL;
1604 else
1605 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001606 vim_free(node);
1607 return p;
1608}
1609
1610/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001612 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001613 */
1614 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001615channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001616{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001617 readq_T *head = &channel->ch_part[part].ch_head;
1618 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001619 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001620 char_u *res;
1621 char_u *p;
1622
1623 /* If there is only one buffer just get that one. */
1624 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1625 return channel_get(channel, part);
1626
1627 /* Concatenate everything into one buffer. */
1628 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001629 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001630 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001631 if (res == NULL)
1632 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001633 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001634 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001635 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001636 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001637 p += node->rq_buflen;
1638 }
1639 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001640
1641 /* Free all buffers */
1642 do
1643 {
1644 p = channel_get(channel, part);
1645 vim_free(p);
1646 } while (p != NULL);
1647
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001648 /* turn all NUL into NL */
1649 while (len > 0)
1650 {
1651 --len;
1652 if (res[len] == NUL)
1653 res[len] = NL;
1654 }
1655
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001656 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001657}
1658
1659/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001660 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001661 * Caller must check these bytes are available.
1662 */
1663 void
1664channel_consume(channel_T *channel, int part, int len)
1665{
1666 readq_T *head = &channel->ch_part[part].ch_head;
1667 readq_T *node = head->rq_next;
1668 char_u *buf = node->rq_buffer;
1669
1670 mch_memmove(buf, buf + len, node->rq_buflen - len);
1671 node->rq_buflen -= len;
1672}
1673
1674/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001675 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001676 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001677 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001678 */
1679 int
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001680channel_collapse(channel_T *channel, int part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001681{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001682 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001683 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001684 readq_T *last_node;
1685 readq_T *n;
1686 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001687 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001688 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001689
Bram Moolenaar77073442016-02-13 23:23:53 +01001690 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001691 return FAIL;
1692
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001693 last_node = node->rq_next;
1694 len = node->rq_buflen + last_node->rq_buflen + 1;
1695 if (want_nl)
1696 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001697 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001698 {
1699 last_node = last_node->rq_next;
1700 len += last_node->rq_buflen;
1701 }
1702
1703 p = newbuf = alloc(len);
1704 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001705 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001706 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001707 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001708 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001709 node->rq_buffer = newbuf;
1710 for (n = node; n != last_node; )
1711 {
1712 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001713 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001714 p += n->rq_buflen;
1715 vim_free(n->rq_buffer);
1716 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001717 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001718
1719 /* dispose of the collapsed nodes and their buffers */
1720 for (n = node->rq_next; n != last_node; )
1721 {
1722 n = n->rq_next;
1723 vim_free(n->rq_prev);
1724 }
1725 node->rq_next = last_node->rq_next;
1726 if (last_node->rq_next == NULL)
1727 head->rq_prev = node;
1728 else
1729 last_node->rq_next->rq_prev = node;
1730 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001731 return OK;
1732}
1733
1734/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001735 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001736 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001737 * Returns OK or FAIL.
1738 */
1739 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001740channel_save(channel_T *channel, int part, char_u *buf, int len,
1741 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001742{
1743 readq_T *node;
1744 readq_T *head = &channel->ch_part[part].ch_head;
1745 char_u *p;
1746 int i;
1747
1748 node = (readq_T *)alloc(sizeof(readq_T));
1749 if (node == NULL)
1750 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001751 /* A NUL is added at the end, because netbeans code expects that.
1752 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001753 node->rq_buffer = alloc(len + 1);
1754 if (node->rq_buffer == NULL)
1755 {
1756 vim_free(node);
1757 return FAIL; /* out of memory */
1758 }
1759
1760 if (channel->ch_part[part].ch_mode == MODE_NL)
1761 {
1762 /* Drop any CR before a NL. */
1763 p = node->rq_buffer;
1764 for (i = 0; i < len; ++i)
1765 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1766 *p++ = buf[i];
1767 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001768 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001769 }
1770 else
1771 {
1772 mch_memmove(node->rq_buffer, buf, len);
1773 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001774 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001775 }
1776
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001777 if (prepend)
1778 {
1779 /* preend node to the head of the queue */
1780 node->rq_next = head->rq_next;
1781 node->rq_prev = NULL;
1782 if (head->rq_next == NULL)
1783 head->rq_prev = node;
1784 else
1785 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001786 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001787 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001788 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001789 {
1790 /* append node to the tail of the queue */
1791 node->rq_next = NULL;
1792 node->rq_prev = head->rq_prev;
1793 if (head->rq_prev == NULL)
1794 head->rq_next = node;
1795 else
1796 head->rq_prev->rq_next = node;
1797 head->rq_prev = node;
1798 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001799
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001800 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001801 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001802 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001803 fprintf(log_fd, "'");
1804 if (fwrite(buf, len, 1, log_fd) != 1)
1805 return FAIL;
1806 fprintf(log_fd, "'\n");
1807 }
1808 return OK;
1809}
1810
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001811 static int
1812channel_fill(js_read_T *reader)
1813{
1814 channel_T *channel = (channel_T *)reader->js_cookie;
1815 int part = reader->js_cookie_arg;
1816 char_u *next = channel_get(channel, part);
1817 int unused;
1818 int len;
1819 char_u *p;
1820
1821 if (next == NULL)
1822 return FALSE;
1823
1824 unused = reader->js_end - reader->js_buf - reader->js_used;
1825 if (unused > 0)
1826 {
1827 /* Prepend unused text. */
1828 len = (int)STRLEN(next);
1829 p = alloc(unused + len + 1);
1830 if (p == NULL)
1831 {
1832 vim_free(next);
1833 return FALSE;
1834 }
1835 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1836 mch_memmove(p + unused, next, len + 1);
1837 vim_free(next);
1838 next = p;
1839 }
1840
1841 vim_free(reader->js_buf);
1842 reader->js_buf = next;
1843 reader->js_used = 0;
1844 return TRUE;
1845}
1846
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001847/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001848 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001849 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001850 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001851 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001852 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001853channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001854{
1855 js_read_T reader;
1856 typval_T listtv;
1857 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001858 chanpart_T *chanpart = &channel->ch_part[part];
1859 jsonq_T *head = &chanpart->ch_json_head;
1860 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001861 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001862
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001863 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001864 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001865
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001866 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001867 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001868 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001869 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001870 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001871
1872 /* When a message is incomplete we wait for a short while for more to
1873 * arrive. After the delay drop the input, otherwise a truncated string
1874 * or list will make us hang. */
1875 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001876 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001877 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001878 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001879 /* Only accept the response when it is a list with at least two
1880 * items. */
1881 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001882 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001883 if (listtv.v_type != VAR_LIST)
1884 ch_error(channel, "Did not receive a list, discarding");
1885 else
1886 ch_errorn(channel, "Expected list with two items, got %d",
1887 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001888 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001889 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001890 else
1891 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001892 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1893 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001894 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001895 else
1896 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001897 item->jq_value = alloc_tv();
1898 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001899 {
1900 vim_free(item);
1901 clear_tv(&listtv);
1902 }
1903 else
1904 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001905 *item->jq_value = listtv;
1906 item->jq_prev = head->jq_prev;
1907 head->jq_prev = item;
1908 item->jq_next = NULL;
1909 if (item->jq_prev == NULL)
1910 head->jq_next = item;
1911 else
1912 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001913 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001914 }
1915 }
1916 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001917
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001918 if (status == OK)
1919 chanpart->ch_waiting = FALSE;
1920 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001921 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001922 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001923 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001924 /* First time encountering incomplete message, set a deadline of
1925 * 100 msec. */
1926 ch_log(channel, "Incomplete message - wait for more");
1927 reader.js_used = 0;
1928 chanpart->ch_waiting = TRUE;
1929#ifdef WIN32
1930 chanpart->ch_deadline = GetTickCount() + 100L;
1931#else
1932 gettimeofday(&chanpart->ch_deadline, NULL);
1933 chanpart->ch_deadline.tv_usec += 100 * 1000;
1934 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1935 {
1936 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1937 ++chanpart->ch_deadline.tv_sec;
1938 }
1939#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001940 }
1941 else
1942 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001943 int timeout;
1944#ifdef WIN32
1945 timeout = GetTickCount() > chanpart->ch_deadline;
1946#else
1947 {
1948 struct timeval now_tv;
1949
1950 gettimeofday(&now_tv, NULL);
1951 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1952 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1953 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1954 }
1955#endif
1956 if (timeout)
1957 {
1958 status = FAIL;
1959 chanpart->ch_waiting = FALSE;
1960 }
1961 else
1962 {
1963 reader.js_used = 0;
1964 ch_log(channel, "still waiting on incomplete message");
1965 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001966 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001967 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001968
1969 if (status == FAIL)
1970 {
1971 ch_error(channel, "Decoding failed - discarding input");
1972 ret = FALSE;
1973 chanpart->ch_waiting = FALSE;
1974 }
1975 else if (reader.js_buf[reader.js_used] != NUL)
1976 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001977 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001978 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001979 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1980 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001981 ret = status == MAYBE ? FALSE: TRUE;
1982 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001983 else
1984 ret = FALSE;
1985
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001986 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001987 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001988}
1989
1990/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001991 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001992 */
1993 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001994remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001995{
Bram Moolenaar77073442016-02-13 23:23:53 +01001996 if (node->cq_prev == NULL)
1997 head->cq_next = node->cq_next;
1998 else
1999 node->cq_prev->cq_next = node->cq_next;
2000 if (node->cq_next == NULL)
2001 head->cq_prev = node->cq_prev;
2002 else
2003 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002004}
2005
2006/*
2007 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002008 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002009 */
2010 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002011remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002012{
Bram Moolenaar77073442016-02-13 23:23:53 +01002013 if (node->jq_prev == NULL)
2014 head->jq_next = node->jq_next;
2015 else
2016 node->jq_prev->jq_next = node->jq_next;
2017 if (node->jq_next == NULL)
2018 head->jq_prev = node->jq_prev;
2019 else
2020 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002021 vim_free(node);
2022}
2023
2024/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002025 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002026 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002027 * When "id" is zero or negative jut get the first message. But not the one
2028 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002029 * Return OK when found and return the value in "rettv".
2030 * Return FAIL otherwise.
2031 */
2032 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002033channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002034{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002035 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002036 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002037
Bram Moolenaar77073442016-02-13 23:23:53 +01002038 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002039 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002040 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002041 typval_T *tv = &l->lv_first->li_tv;
2042
2043 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002044 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002045 || tv->vval.v_number == 0
2046 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002047 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002048 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002049 if (tv->v_type == VAR_NUMBER)
2050 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002051 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002052 return OK;
2053 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002054 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002055 }
2056 return FAIL;
2057}
2058
Bram Moolenaarece61b02016-02-20 21:39:05 +01002059#define CH_JSON_MAX_ARGS 4
2060
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002061/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002062 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002063 * "argv[0]" is the command string.
2064 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002065 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002066 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002067channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002068{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002069 char_u *cmd = argv[0].vval.v_string;
2070 char_u *arg;
2071 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002072
Bram Moolenaarece61b02016-02-20 21:39:05 +01002073 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002074 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002075 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002076 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002077 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002078 return;
2079 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002080 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002081 if (arg == NULL)
2082 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002083
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002084 if (STRCMP(cmd, "ex") == 0)
2085 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002086 int save_called_emsg = called_emsg;
2087
2088 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002089 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002090 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002091 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002092 --emsg_silent;
2093 if (called_emsg)
2094 ch_logs(channel, "Ex command error: '%s'",
2095 (char *)get_vim_var_str(VV_ERRMSG));
2096 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002097 }
2098 else if (STRCMP(cmd, "normal") == 0)
2099 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002100 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002101
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002102 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002103 ea.arg = arg;
2104 ea.addr_count = 0;
2105 ea.forceit = TRUE; /* no mapping */
2106 ex_normal(&ea);
2107 }
2108 else if (STRCMP(cmd, "redraw") == 0)
2109 {
2110 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002111
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002112 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002113 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002114 ex_redraw(&ea);
2115 showruler(FALSE);
2116 setcursor();
2117 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002118#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002119 if (gui.in_use)
2120 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002121 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002122 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002123 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002124#endif
2125 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002126 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002127 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002128 int is_call = cmd[0] == 'c';
2129 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002130
Bram Moolenaarece61b02016-02-20 21:39:05 +01002131 if (argv[id_idx].v_type != VAR_UNKNOWN
2132 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002133 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002134 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002135 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002136 EMSG("E904: last argument for expr/call must be a number");
2137 }
2138 else if (is_call && argv[2].v_type != VAR_LIST)
2139 {
2140 ch_error(channel, "third argument for call must be a list");
2141 if (p_verbose > 2)
2142 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002143 }
2144 else
2145 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002146 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002147 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002148 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002149 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002150
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002151 /* Don't pollute the display with errors. */
2152 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002153 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002154 {
2155 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002156 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002157 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002158 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002159 {
2160 ch_logs(channel, "Calling '%s'", (char *)arg);
2161 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2162 tv = &res_tv;
2163 else
2164 tv = NULL;
2165 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002166
2167 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002168 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002169 int id = argv[id_idx].vval.v_number;
2170
Bram Moolenaar55fab432016-02-07 16:53:13 +01002171 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002172 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002173 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002174 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002175 /* If evaluation failed or the result can't be encoded
2176 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002177 vim_free(json);
2178 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002179 err_tv.v_type = VAR_STRING;
2180 err_tv.vval.v_string = (char_u *)"ERROR";
2181 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002182 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002183 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002184 if (json != NULL)
2185 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002186 channel_send(channel,
2187 part == PART_SOCK ? PART_SOCK : PART_IN,
2188 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002189 vim_free(json);
2190 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002191 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002192 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002193 if (tv == &res_tv)
2194 clear_tv(tv);
2195 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002196 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002197 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002198 }
2199 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002200 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002201 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002202 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002203 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002204}
2205
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002206/*
2207 * Invoke the callback at "cbhead".
2208 * Does not redraw but sets channel_need_redraw.
2209 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002210 static void
2211invoke_one_time_callback(
2212 channel_T *channel,
2213 cbq_T *cbhead,
2214 cbq_T *item,
2215 typval_T *argv)
2216{
2217 ch_logs(channel, "Invoking one-time callback %s",
2218 (char *)item->cq_callback);
2219 /* Remove the item from the list first, if the callback
2220 * invokes ch_close() the list will be cleared. */
2221 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002222 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002223 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002224 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002225 vim_free(item);
2226}
2227
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002228 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002229append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002230{
2231 buf_T *save_curbuf = curbuf;
2232 linenr_T lnum = buffer->b_ml.ml_line_count;
2233 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002234 chanpart_T *ch_part = &channel->ch_part[part];
2235 int save_p_ma = buffer->b_p_ma;
2236
2237 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2238 {
2239 if (!ch_part->ch_nomod_error)
2240 {
2241 ch_error(channel, "Buffer is not modifiable, cannot append");
2242 ch_part->ch_nomod_error = TRUE;
2243 }
2244 return;
2245 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002246
2247 /* If the buffer is also used as input insert above the last
2248 * line. Don't write these lines. */
2249 if (save_write_to)
2250 {
2251 --lnum;
2252 buffer->b_write_to_channel = FALSE;
2253 }
2254
2255 /* Append to the buffer */
2256 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2257
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002258 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002259 curbuf = buffer;
2260 u_sync(TRUE);
2261 /* ignore undo failure, undo is not very useful here */
2262 ignored = u_save(lnum, lnum + 1);
2263
2264 ml_append(lnum, msg, 0, FALSE);
2265 appended_lines_mark(lnum, 1L);
2266 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002267 if (ch_part->ch_nomodifiable)
2268 buffer->b_p_ma = FALSE;
2269 else
2270 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002271
2272 if (buffer->b_nwindows > 0)
2273 {
2274 win_T *wp;
2275 win_T *save_curwin;
2276
2277 FOR_ALL_WINDOWS(wp)
2278 {
2279 if (wp->w_buffer == buffer
2280 && (save_write_to
2281 ? wp->w_cursor.lnum == lnum + 1
2282 : (wp->w_cursor.lnum == lnum
2283 && wp->w_cursor.col == 0)))
2284 {
2285 ++wp->w_cursor.lnum;
2286 save_curwin = curwin;
2287 curwin = wp;
2288 curbuf = curwin->w_buffer;
2289 scroll_cursor_bot(0, FALSE);
2290 curwin = save_curwin;
2291 curbuf = curwin->w_buffer;
2292 }
2293 }
2294 redraw_buf_later(buffer, VALID);
2295 channel_need_redraw = TRUE;
2296 }
2297
2298 if (save_write_to)
2299 {
2300 channel_T *ch;
2301
2302 /* Find channels reading from this buffer and adjust their
2303 * next-to-read line number. */
2304 buffer->b_write_to_channel = TRUE;
2305 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2306 {
2307 chanpart_T *in_part = &ch->ch_part[PART_IN];
2308
2309 if (in_part->ch_buffer == buffer)
2310 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2311 }
2312 }
2313}
2314
Bram Moolenaar437905c2016-04-26 19:01:05 +02002315 static void
2316drop_messages(channel_T *channel, int part)
2317{
2318 char_u *msg;
2319
2320 while ((msg = channel_get(channel, part)) != NULL)
2321 {
2322 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2323 vim_free(msg);
2324 }
2325}
2326
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002327/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002328 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002329 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002330 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002331 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002332 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002333may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002334{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002335 char_u *msg = NULL;
2336 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002337 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002338 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002339 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002340 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002341 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002342 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002343 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002344 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002345 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002346
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002347 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002348 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002349 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002350
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002351 /* Use a message-specific callback, part callback or channel callback */
2352 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2353 if (cbitem->cq_seq_nr == 0)
2354 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002355 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002356 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002357 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002358 partial = cbitem->cq_partial;
2359 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002360 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002361 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002362 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002363 partial = channel->ch_part[part].ch_partial;
2364 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002365 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002366 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002367 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002368 partial = channel->ch_partial;
2369 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002370
Bram Moolenaar187db502016-02-27 14:44:26 +01002371 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002372 if (buffer != NULL && !buf_valid(buffer))
2373 {
2374 /* buffer was wiped out */
2375 channel->ch_part[part].ch_buffer = NULL;
2376 buffer = NULL;
2377 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002378
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002379 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002380 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002381 listitem_T *item;
2382 int argc = 0;
2383
Bram Moolenaard7ece102016-02-02 23:23:02 +01002384 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002385 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002386 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002387 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002388 channel_parse_json(channel, part);
2389 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002390 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002391 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002392
Bram Moolenaarece61b02016-02-20 21:39:05 +01002393 for (item = listtv->vval.v_list->lv_first;
2394 item != NULL && argc < CH_JSON_MAX_ARGS;
2395 item = item->li_next)
2396 argv[argc++] = item->li_tv;
2397 while (argc < CH_JSON_MAX_ARGS)
2398 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002399
Bram Moolenaarece61b02016-02-20 21:39:05 +01002400 if (argv[0].v_type == VAR_STRING)
2401 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002402 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002403 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002404 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002405 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002406 }
2407
Bram Moolenaarece61b02016-02-20 21:39:05 +01002408 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002409 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002410 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002411 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002412 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002413 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002414 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002415 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002416 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002417 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002418 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002419 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002420 return FALSE;
2421 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002422 else
2423 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002424 /* If there is no callback or buffer drop the message. */
2425 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002426 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002427 /* If there is a close callback it may use ch_read() to get the
2428 * messages. */
2429 if (channel->ch_close_cb == NULL)
2430 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002431 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002432 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002433
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002434 if (ch_mode == MODE_NL)
2435 {
2436 char_u *nl;
2437 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002438 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002439
2440 /* See if we have a message ending in NL in the first buffer. If
2441 * not try to concatenate the first and the second buffer. */
2442 while (TRUE)
2443 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002444 node = channel_peek(channel, part);
2445 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002446 if (nl != NULL)
2447 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002448 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002449 return FALSE; /* incomplete message */
2450 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002451 buf = node->rq_buffer;
2452
2453 /* Convert NUL to NL, the internal representation. */
2454 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2455 if (*p == NUL)
2456 *p = NL;
2457
2458 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002459 {
2460 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002461 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002462 *nl = NUL;
2463 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002464 else
2465 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002466 /* Copy the message into allocated memory (excluding the NL)
2467 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002468 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002469 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002470 }
2471 }
2472 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002473 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002474 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002475 * get everything we have.
2476 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002477 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002478 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002479
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002480 if (msg == NULL)
2481 return FALSE; /* out of memory (and avoids Coverity warning) */
2482
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002483 argv[1].v_type = VAR_STRING;
2484 argv[1].vval.v_string = msg;
2485 }
2486
Bram Moolenaara07fec92016-02-05 21:04:08 +01002487 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002488 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002489 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002490
2491 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002492 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002493 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002494 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002495 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002496 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002497 break;
2498 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002499 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002500 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002501 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002502 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002503 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002504 if (buffer != NULL)
2505 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002506 if (msg == NULL)
2507 /* JSON or JS mode: re-encode the message. */
2508 msg = json_encode(listtv, ch_mode);
2509 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002510 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002511 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002512
Bram Moolenaar187db502016-02-27 14:44:26 +01002513 if (callback != NULL)
2514 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002515 if (cbitem != NULL)
2516 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2517 else
2518 {
2519 /* invoke the channel callback */
2520 ch_logs(channel, "Invoking channel callback %s",
2521 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002522 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002523 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002524 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002525 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002526 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002527 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002528
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002529 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002530 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002531 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002532
2533 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002534}
2535
2536/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002537 * Return TRUE when channel "channel" is open for writing to.
2538 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002539 */
2540 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002541channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002542{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002543 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002544 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002545}
2546
2547/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002548 * Return TRUE when channel "channel" is open for reading or writing.
2549 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002550 */
2551 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002552channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002553{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002554 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002555 || channel->CH_IN_FD != INVALID_FD
2556 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002557 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002558}
2559
2560/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002561 * Return TRUE if "channel" has JSON or other typeahead.
2562 */
2563 static int
2564channel_has_readahead(channel_T *channel, int part)
2565{
2566 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2567
2568 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2569 {
2570 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2571 jsonq_T *item = head->jq_next;
2572
2573 return item != NULL;
2574 }
2575 return channel_peek(channel, part) != NULL;
2576}
2577
2578/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002579 * Return a string indicating the status of the channel.
2580 */
2581 char *
2582channel_status(channel_T *channel)
2583{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002584 int part;
2585 int has_readahead = FALSE;
2586
Bram Moolenaar77073442016-02-13 23:23:53 +01002587 if (channel == NULL)
2588 return "fail";
2589 if (channel_is_open(channel))
2590 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002591 for (part = PART_SOCK; part <= PART_ERR; ++part)
2592 if (channel_has_readahead(channel, part))
2593 {
2594 has_readahead = TRUE;
2595 break;
2596 }
2597
2598 if (has_readahead)
2599 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002600 return "closed";
2601}
2602
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002603 static void
2604channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2605{
2606 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002607 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002608 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002609 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002610
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002611 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002612 STRCAT(namebuf, "_");
2613 tail = STRLEN(namebuf);
2614
2615 STRCPY(namebuf + tail, "status");
2616 dict_add_nr_str(dict, namebuf, 0,
2617 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2618
2619 STRCPY(namebuf + tail, "mode");
2620 switch (chanpart->ch_mode)
2621 {
2622 case MODE_NL: s = "NL"; break;
2623 case MODE_RAW: s = "RAW"; break;
2624 case MODE_JSON: s = "JSON"; break;
2625 case MODE_JS: s = "JS"; break;
2626 }
2627 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2628
2629 STRCPY(namebuf + tail, "io");
2630 if (part == PART_SOCK)
2631 s = "socket";
2632 else switch (chanpart->ch_io)
2633 {
2634 case JIO_NULL: s = "null"; break;
2635 case JIO_PIPE: s = "pipe"; break;
2636 case JIO_FILE: s = "file"; break;
2637 case JIO_BUFFER: s = "buffer"; break;
2638 case JIO_OUT: s = "out"; break;
2639 }
2640 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2641
2642 STRCPY(namebuf + tail, "timeout");
2643 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2644}
2645
2646 void
2647channel_info(channel_T *channel, dict_T *dict)
2648{
2649 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2650 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2651
2652 if (channel->ch_hostname != NULL)
2653 {
2654 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2655 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2656 channel_part_info(channel, dict, "sock", PART_SOCK);
2657 }
2658 else
2659 {
2660 channel_part_info(channel, dict, "out", PART_OUT);
2661 channel_part_info(channel, dict, "err", PART_ERR);
2662 channel_part_info(channel, dict, "in", PART_IN);
2663 }
2664}
2665
Bram Moolenaar77073442016-02-13 23:23:53 +01002666/*
2667 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002668 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002669 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002670 */
2671 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002672channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002673{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002674 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002675
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002676#ifdef FEAT_GUI
2677 channel_gui_unregister(channel);
2678#endif
2679
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002680 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002681 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002682 sock_close(channel->CH_SOCK_FD);
2683 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002684 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002685 may_close_part(&channel->CH_IN_FD);
2686 may_close_part(&channel->CH_OUT_FD);
2687 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002688
Bram Moolenaar8b374212016-02-24 20:43:06 +01002689 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002690 {
2691 typval_T argv[1];
2692 typval_T rettv;
2693 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002694 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002695
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002696 /* Invoke callbacks before the close callback, since it's weird to
2697 * first invoke the close callback. Increment the refcount to avoid
2698 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002699 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002700 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002701 for (part = PART_SOCK; part <= PART_ERR; ++part)
2702 while (may_invoke_callback(channel, part))
2703 ;
2704
2705 /* Invoke the close callback, if still set. */
2706 if (channel->ch_close_cb != NULL)
2707 {
2708 ch_logs(channel, "Invoking close callback %s",
2709 (char *)channel->ch_close_cb);
2710 argv[0].v_type = VAR_CHANNEL;
2711 argv[0].vval.v_channel = channel;
2712 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002713 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2714 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002715 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002716 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002717 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002718
2719 /* the callback is only called once */
2720 vim_free(channel->ch_close_cb);
2721 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002722 partial_unref(channel->ch_close_partial);
2723 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002724
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002725 --channel->ch_refcount;
2726
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002727 if (channel_need_redraw)
2728 {
2729 channel_need_redraw = FALSE;
2730 redraw_after_callback();
2731 }
2732
Bram Moolenaar437905c2016-04-26 19:01:05 +02002733 /* any remaining messages are useless now */
2734 for (part = PART_SOCK; part <= PART_ERR; ++part)
2735 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002736 }
2737
2738 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002739}
2740
Bram Moolenaard04a0202016-01-26 23:30:18 +01002741/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002742 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002743 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002744 static void
2745channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002746{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002747 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2748 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002749
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002750 while (channel_peek(channel, part) != NULL)
2751 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002752
2753 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002754 {
2755 cbq_T *node = cb_head->cq_next;
2756
2757 remove_cb_node(cb_head, node);
2758 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002759 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002760 vim_free(node);
2761 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002762
2763 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002764 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002765 free_tv(json_head->jq_next->jq_value);
2766 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002767 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002768
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002769 vim_free(channel->ch_part[part].ch_callback);
2770 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002771 partial_unref(channel->ch_part[part].ch_partial);
2772 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002773}
2774
2775/*
2776 * Clear all the read buffers on "channel".
2777 */
2778 void
2779channel_clear(channel_T *channel)
2780{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002781 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002782 vim_free(channel->ch_hostname);
2783 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002784 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002785 channel_clear_one(channel, PART_OUT);
2786 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002787 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002788 vim_free(channel->ch_callback);
2789 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002790 partial_unref(channel->ch_partial);
2791 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002792 vim_free(channel->ch_close_cb);
2793 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002794 partial_unref(channel->ch_close_partial);
2795 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002796}
2797
Bram Moolenaar77073442016-02-13 23:23:53 +01002798#if defined(EXITFREE) || defined(PROTO)
2799 void
2800channel_free_all(void)
2801{
2802 channel_T *channel;
2803
Bram Moolenaard6051b52016-02-28 15:49:03 +01002804 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002805 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2806 channel_clear(channel);
2807}
2808#endif
2809
2810
Bram Moolenaar715d2852016-04-30 17:06:31 +02002811/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002812#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002813
2814/* Buffer size for reading incoming messages. */
2815#define MAXMSGSIZE 4096
2816
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002817#if defined(HAVE_SELECT)
2818/*
2819 * Add write fds where we are waiting for writing to be possible.
2820 */
2821 static int
2822channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2823{
2824 int maxfd = maxfd_arg;
2825 channel_T *ch;
2826
2827 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2828 {
2829 chanpart_T *in_part = &ch->ch_part[PART_IN];
2830
2831 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2832 {
2833 FD_SET((int)in_part->ch_fd, wfds);
2834 if ((int)in_part->ch_fd >= maxfd)
2835 maxfd = (int)in_part->ch_fd + 1;
2836 }
2837 }
2838 return maxfd;
2839}
2840#else
2841/*
2842 * Add write fds where we are waiting for writing to be possible.
2843 */
2844 static int
2845channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2846{
2847 int nfd = nfd_in;
2848 channel_T *ch;
2849
2850 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2851 {
2852 chanpart_T *in_part = &ch->ch_part[PART_IN];
2853
2854 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2855 {
2856 in_part->ch_poll_idx = nfd;
2857 fds[nfd].fd = in_part->ch_fd;
2858 fds[nfd].events = POLLOUT;
2859 ++nfd;
2860 }
2861 else
2862 in_part->ch_poll_idx = -1;
2863 }
2864 return nfd;
2865}
2866#endif
2867
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002868typedef enum {
2869 CW_READY,
2870 CW_NOT_READY,
2871 CW_ERROR
2872} channel_wait_result;
2873
Bram Moolenaard04a0202016-01-26 23:30:18 +01002874/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002875 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002876 * Return CW_READY when there is something to read.
2877 * Return CW_NOT_READY when there is nothing to read.
2878 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002879 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002880 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002881channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002882{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002883 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002884 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002885
Bram Moolenaard8070362016-02-15 21:56:54 +01002886# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002887 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002888 {
2889 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002890 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002891 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002892 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002893
2894 /* reading from a pipe, not a socket */
2895 while (TRUE)
2896 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002897 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2898
2899 if (r && nread > 0)
2900 return CW_READY;
2901 if (r == 0)
2902 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002903
2904 /* perhaps write some buffer lines */
2905 channel_write_any_lines();
2906
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002907 sleep_time = deadline - GetTickCount();
2908 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002909 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002910 /* Wait for a little while. Very short at first, up to 10 msec
2911 * after looping a few times. */
2912 if (sleep_time > delay)
2913 sleep_time = delay;
2914 Sleep(sleep_time);
2915 delay = delay * 2;
2916 if (delay > 10)
2917 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002918 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002919 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002920 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002921#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002922 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002923#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002924 struct timeval tval;
2925 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002926 fd_set wfds;
2927 int ret;
2928 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002929
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002930 tval.tv_sec = timeout / 1000;
2931 tval.tv_usec = (timeout % 1000) * 1000;
2932 for (;;)
2933 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002934 FD_ZERO(&rfds);
2935 FD_SET((int)fd, &rfds);
2936
2937 /* Write lines to a pipe when a pipe can be written to. Need to
2938 * set this every time, some buffers may be done. */
2939 maxfd = (int)fd + 1;
2940 FD_ZERO(&wfds);
2941 maxfd = channel_fill_wfds(maxfd, &wfds);
2942
2943 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002944# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002945 SOCK_ERRNO;
2946 if (ret == -1 && errno == EINTR)
2947 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002948# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002949 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002950 {
2951 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002952 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002953 channel_write_any_lines();
2954 continue;
2955 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002956 break;
2957 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002958#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002959 for (;;)
2960 {
2961 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2962 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002963
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002964 fds[0].fd = fd;
2965 fds[0].events = POLLIN;
2966 nfd = channel_fill_poll_write(nfd, fds);
2967 if (poll(fds, nfd, timeout) > 0)
2968 {
2969 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002970 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002971 channel_write_any_lines();
2972 continue;
2973 }
2974 break;
2975 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002976#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002977 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002978 return CW_NOT_READY;
2979}
2980
2981 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002982channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002983{
2984 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002985 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2986 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002987
2988 /* Queue a "DETACH" netbeans message in the command queue in order to
2989 * terminate the netbeans session later. Do not end the session here
2990 * directly as we may be running in the context of a call to
2991 * netbeans_parse_messages():
2992 * netbeans_parse_messages
2993 * -> autocmd triggered while processing the netbeans cmd
2994 * -> ui_breakcheck
2995 * -> gui event loop or select loop
2996 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002997 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002998 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002999 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02003000 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003001 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3002
3003 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003004 * died. Don't close the channel right away, it may be the wrong moment
3005 * to invoke callbacks. */
3006 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003007
3008#ifdef FEAT_GUI
3009 /* Stop listening to GUI events right away. */
3010 channel_gui_unregister(channel);
3011#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003012}
3013
3014 static void
3015channel_close_now(channel_T *channel)
3016{
3017 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003018 channel_close(channel, TRUE);
3019 if (channel->ch_nb_close_cb != NULL)
3020 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003021}
3022
3023/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003024 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003025 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003026 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003027 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003028 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003029channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003030{
3031 static char_u *buf = NULL;
3032 int len = 0;
3033 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003034 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003035 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003036
Bram Moolenaar5850a762016-05-27 19:59:48 +02003037 /* If we detected a read error don't try reading again. */
3038 if (channel->ch_to_be_closed)
3039 return;
3040
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003041 fd = channel->ch_part[part].ch_fd;
3042 if (fd == INVALID_FD)
3043 {
3044 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003045 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003046 }
3047 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003048
3049 /* Allocate a buffer to read into. */
3050 if (buf == NULL)
3051 {
3052 buf = alloc(MAXMSGSIZE);
3053 if (buf == NULL)
3054 return; /* out of memory! */
3055 }
3056
3057 /* Keep on reading for as long as there is something to read.
3058 * Use select() or poll() to avoid blocking on a message that is exactly
3059 * MAXMSGSIZE long. */
3060 for (;;)
3061 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003062 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003063 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003064 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003065 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003066 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003067 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003068 if (len <= 0)
3069 break; /* error or nothing more to read */
3070
3071 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003072 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003073 readlen += len;
3074 if (len < MAXMSGSIZE)
3075 break; /* did read everything that's available */
3076 }
3077
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003078 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003079 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003080 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003081
3082#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003083 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003084 if (CH_HAS_GUI && gtk_main_level() > 0)
3085 gtk_main_quit();
3086#endif
3087}
3088
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003089/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003090 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003091 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003092 * Returns what was read in allocated memory.
3093 * Returns NULL in case of error or timeout.
3094 */
3095 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003096channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003097{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003098 char_u *buf;
3099 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003100 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003101 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003102 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003103 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003104
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003105 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003106 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003107
3108 while (TRUE)
3109 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003110 node = channel_peek(channel, part);
3111 if (node != NULL)
3112 {
3113 if (mode == MODE_RAW || (mode == MODE_NL
3114 && channel_first_nl(node) != NULL))
3115 /* got a complete message */
3116 break;
3117 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3118 continue;
3119 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003120
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003121 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003122 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003123 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003124 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003125 {
3126 ch_log(channel, "Timed out");
3127 return NULL;
3128 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003129 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003130 }
3131
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003132 if (mode == MODE_RAW)
3133 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003134 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003135 }
3136 else
3137 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003138 char_u *p;
3139
3140 buf = node->rq_buffer;
3141 nl = channel_first_nl(node);
3142
3143 /* Convert NUL to NL, the internal representation. */
3144 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3145 if (*p == NUL)
3146 *p = NL;
3147
3148 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003149 {
3150 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003151 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003152 *nl = NUL;
3153 }
3154 else
3155 {
3156 /* Copy the message into allocated memory and remove it from the
3157 * buffer. */
3158 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003159 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003160 }
3161 }
3162 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003163 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003164 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003165}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003166
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003167/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003168 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003169 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003170 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003171 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003172 */
3173 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003174channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003175 channel_T *channel,
3176 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003177 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003178 int id,
3179 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003180{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003181 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003182 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003183 int timeout;
3184 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003185
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003186 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003187 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003188 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003189 for (;;)
3190 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003191 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003192
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003193 /* search for message "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003194 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003195 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003196 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003197 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003198 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003199
Bram Moolenaard7ece102016-02-02 23:23:02 +01003200 if (!more)
3201 {
3202 /* Handle any other messages in the queue. If done some more
3203 * messages may have arrived. */
3204 if (channel_parse_messages())
3205 continue;
3206
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003207 /* Wait for up to the timeout. If there was an incomplete message
3208 * use the deadline for that. */
3209 timeout = timeout_arg;
3210 if (chanpart->ch_waiting)
3211 {
3212#ifdef WIN32
3213 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3214#else
3215 {
3216 struct timeval now_tv;
3217
3218 gettimeofday(&now_tv, NULL);
3219 timeout = (chanpart->ch_deadline.tv_sec
3220 - now_tv.tv_sec) * 1000
3221 + (chanpart->ch_deadline.tv_usec
3222 - now_tv.tv_usec) / 1000
3223 + 1;
3224 }
3225#endif
3226 if (timeout < 0)
3227 {
3228 /* Something went wrong, channel_parse_json() didn't
3229 * discard message. Cancel waiting. */
3230 chanpart->ch_waiting = FALSE;
3231 timeout = timeout_arg;
3232 }
3233 else if (timeout > timeout_arg)
3234 timeout = timeout_arg;
3235 }
3236 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003237 if (fd == INVALID_FD
3238 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003239 {
3240 if (timeout == timeout_arg)
3241 {
3242 if (fd != INVALID_FD)
3243 ch_log(channel, "Timed out");
3244 break;
3245 }
3246 }
3247 else
3248 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003249 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003250 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003251 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003252 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003253}
3254
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003255/*
3256 * Common for ch_read() and ch_readraw().
3257 */
3258 void
3259common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3260{
3261 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003262 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003263 jobopt_T opt;
3264 int mode;
3265 int timeout;
3266 int id = -1;
3267 typval_T *listtv = NULL;
3268
3269 /* return an empty string by default */
3270 rettv->v_type = VAR_STRING;
3271 rettv->vval.v_string = NULL;
3272
3273 clear_job_options(&opt);
3274 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3275 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003276 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003277
Bram Moolenaar437905c2016-04-26 19:01:05 +02003278 if (opt.jo_set & JO_PART)
3279 part = opt.jo_part;
3280 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003281 if (channel != NULL)
3282 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003283 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003284 part = channel_part_read(channel);
3285 mode = channel_get_mode(channel, part);
3286 timeout = channel_get_timeout(channel, part);
3287 if (opt.jo_set & JO_TIMEOUT)
3288 timeout = opt.jo_timeout;
3289
3290 if (raw || mode == MODE_RAW || mode == MODE_NL)
3291 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3292 else
3293 {
3294 if (opt.jo_set & JO_ID)
3295 id = opt.jo_id;
3296 channel_read_json_block(channel, part, timeout, id, &listtv);
3297 if (listtv != NULL)
3298 {
3299 *rettv = *listtv;
3300 vim_free(listtv);
3301 }
3302 else
3303 {
3304 rettv->v_type = VAR_SPECIAL;
3305 rettv->vval.v_number = VVAL_NONE;
3306 }
3307 }
3308 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003309
3310theend:
3311 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003312}
3313
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003314# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3315 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003316/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003317 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003318 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003319 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003320 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003321channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003322{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003323 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003324 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003325
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003326 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003327 for (channel = first_channel; channel != NULL;
3328 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003329 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003330 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003331 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003332 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003333 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003334 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003335 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003336 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003337 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003338}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003339# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003340
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003341# if defined(WIN32) || defined(PROTO)
3342/*
3343 * Check the channels for anything that is ready to be read.
3344 * The data is put in the read queue.
3345 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003346 void
3347channel_handle_events(void)
3348{
3349 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003350 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003351 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003352
3353 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3354 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003355 /* If we detected a read error don't try reading again. */
3356 if (channel->ch_to_be_closed)
3357 continue;
3358
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003359 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003360 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003361 {
3362 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003363 if (fd != INVALID_FD)
3364 {
3365 int r = channel_wait(channel, fd, 0);
3366
3367 if (r == CW_READY)
3368 channel_read(channel, part, "channel_handle_events");
3369 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003370 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003371 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003372 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003373 }
3374}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003375# endif
3376
Bram Moolenaard04a0202016-01-26 23:30:18 +01003377/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003378 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003379 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003380 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003381 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003382 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003383channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003384{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003385 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003386 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003387 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003388
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003389 fd = channel->ch_part[part].ch_fd;
3390 if (fd == INVALID_FD)
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 while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003395 EMSG2("E630: %s(): write while not connected", 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
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003401 if (log_fd != NULL)
3402 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003403 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003404 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003405 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003406 fprintf(log_fd, "'\n");
3407 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003408 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003409 }
3410
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003411 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003412 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003413 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003414 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003415 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003416 {
3417 if (!channel->ch_error && fun != NULL)
3418 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003419 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003420 EMSG2("E631: %s(): write failed", fun);
3421 }
3422 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003423 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003424 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003425
3426 channel->ch_error = FALSE;
3427 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003428}
3429
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003430/*
3431 * Common for "ch_sendexpr()" and "ch_sendraw()".
3432 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003433 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003434 * Otherwise returns NULL.
3435 */
3436 channel_T *
3437send_common(
3438 typval_T *argvars,
3439 char_u *text,
3440 int id,
3441 int eval,
3442 jobopt_T *opt,
3443 char *fun,
3444 int *part_read)
3445{
3446 channel_T *channel;
3447 int part_send;
3448
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003449 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003450 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003451 if (channel == NULL)
3452 return NULL;
3453 part_send = channel_part_send(channel);
3454 *part_read = channel_part_read(channel);
3455
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003456 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3457 return NULL;
3458
3459 /* Set the callback. An empty callback means no callback and not reading
3460 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3461 * allowed. */
3462 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3463 {
3464 if (eval)
3465 {
3466 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3467 return NULL;
3468 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003469 channel_set_req_callback(channel, part_send,
3470 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003471 }
3472
3473 if (channel_send(channel, part_send, text, fun) == OK
3474 && opt->jo_callback == NULL)
3475 return channel;
3476 return NULL;
3477}
3478
3479/*
3480 * common for "ch_evalexpr()" and "ch_sendexpr()"
3481 */
3482 void
3483ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3484{
3485 char_u *text;
3486 typval_T *listtv;
3487 channel_T *channel;
3488 int id;
3489 ch_mode_T ch_mode;
3490 int part_send;
3491 int part_read;
3492 jobopt_T opt;
3493 int timeout;
3494
3495 /* return an empty string by default */
3496 rettv->v_type = VAR_STRING;
3497 rettv->vval.v_string = NULL;
3498
Bram Moolenaar437905c2016-04-26 19:01:05 +02003499 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003500 if (channel == NULL)
3501 return;
3502 part_send = channel_part_send(channel);
3503
3504 ch_mode = channel_get_mode(channel, part_send);
3505 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3506 {
3507 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3508 return;
3509 }
3510
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003511 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003512 text = json_encode_nr_expr(id, &argvars[1],
3513 ch_mode == MODE_JS ? JSON_JS : 0);
3514 if (text == NULL)
3515 return;
3516
3517 channel = send_common(argvars, text, id, eval, &opt,
3518 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3519 vim_free(text);
3520 if (channel != NULL && eval)
3521 {
3522 if (opt.jo_set & JO_TIMEOUT)
3523 timeout = opt.jo_timeout;
3524 else
3525 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003526 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3527 == OK)
3528 {
3529 list_T *list = listtv->vval.v_list;
3530
3531 /* Move the item from the list and then change the type to
3532 * avoid the value being freed. */
3533 *rettv = list->lv_last->li_tv;
3534 list->lv_last->li_tv.v_type = VAR_NUMBER;
3535 free_tv(listtv);
3536 }
3537 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003538 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003539}
3540
3541/*
3542 * common for "ch_evalraw()" and "ch_sendraw()"
3543 */
3544 void
3545ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3546{
3547 char_u buf[NUMBUFLEN];
3548 char_u *text;
3549 channel_T *channel;
3550 int part_read;
3551 jobopt_T opt;
3552 int timeout;
3553
3554 /* return an empty string by default */
3555 rettv->v_type = VAR_STRING;
3556 rettv->vval.v_string = NULL;
3557
3558 text = get_tv_string_buf(&argvars[1], buf);
3559 channel = send_common(argvars, text, 0, eval, &opt,
3560 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3561 if (channel != NULL && eval)
3562 {
3563 if (opt.jo_set & JO_TIMEOUT)
3564 timeout = opt.jo_timeout;
3565 else
3566 timeout = channel_get_timeout(channel, part_read);
3567 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3568 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003569 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003570}
3571
Bram Moolenaard04a0202016-01-26 23:30:18 +01003572# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003573/*
3574 * Add open channels to the poll struct.
3575 * Return the adjusted struct index.
3576 * The type of "fds" is hidden to avoid problems with the function proto.
3577 */
3578 int
3579channel_poll_setup(int nfd_in, void *fds_in)
3580{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003581 int nfd = nfd_in;
3582 channel_T *channel;
3583 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003584 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003585
Bram Moolenaar77073442016-02-13 23:23:53 +01003586 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003587 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003588 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003589 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003590 chanpart_T *ch_part = &channel->ch_part[part];
3591
3592 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003593 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003594 ch_part->ch_poll_idx = nfd;
3595 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003596 fds[nfd].events = POLLIN;
3597 nfd++;
3598 }
3599 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003600 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003601 }
3602 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003603
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003604 nfd = channel_fill_poll_write(nfd, fds);
3605
Bram Moolenaare0874f82016-01-24 20:36:41 +01003606 return nfd;
3607}
3608
3609/*
3610 * The type of "fds" is hidden to avoid problems with the function proto.
3611 */
3612 int
3613channel_poll_check(int ret_in, void *fds_in)
3614{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003615 int ret = ret_in;
3616 channel_T *channel;
3617 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003618 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003619 int idx;
3620 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003621
Bram Moolenaar77073442016-02-13 23:23:53 +01003622 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003623 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003624 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003625 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003626 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003627
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003628 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003629 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003630 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003631 --ret;
3632 }
3633 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003634
3635 in_part = &channel->ch_part[PART_IN];
3636 idx = in_part->ch_poll_idx;
3637 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3638 {
3639 if (in_part->ch_buf_append)
3640 {
3641 if (in_part->ch_buffer != NULL)
3642 channel_write_new_lines(in_part->ch_buffer);
3643 }
3644 else
3645 channel_write_in(channel);
3646 --ret;
3647 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003648 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003649
3650 return ret;
3651}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003652# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003653
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003654# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003655/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003656 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003657 */
3658 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003659channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003660{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003661 int maxfd = maxfd_in;
3662 channel_T *channel;
3663 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003664 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003665 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003666
Bram Moolenaar77073442016-02-13 23:23:53 +01003667 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003668 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003669 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003670 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003671 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003672
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003673 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003674 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003675 FD_SET((int)fd, rfds);
3676 if (maxfd < (int)fd)
3677 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003678 }
3679 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003680 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003681
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003682 maxfd = channel_fill_wfds(maxfd, wfds);
3683
Bram Moolenaare0874f82016-01-24 20:36:41 +01003684 return maxfd;
3685}
3686
3687/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003688 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003689 */
3690 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003691channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003692{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003693 int ret = ret_in;
3694 channel_T *channel;
3695 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003696 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003697 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003698 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003699
Bram Moolenaar77073442016-02-13 23:23:53 +01003700 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003701 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003702 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003703 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003704 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003705
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003706 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003707 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003708 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003709 --ret;
3710 }
3711 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003712
3713 in_part = &channel->ch_part[PART_IN];
3714 if (ret > 0 && in_part->ch_fd != INVALID_FD
3715 && FD_ISSET(in_part->ch_fd, wfds))
3716 {
3717 if (in_part->ch_buf_append)
3718 {
3719 if (in_part->ch_buffer != NULL)
3720 channel_write_new_lines(in_part->ch_buffer);
3721 }
3722 else
3723 channel_write_in(channel);
3724 --ret;
3725 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003726 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003727
3728 return ret;
3729}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003730# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003731
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003732/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003733 * Execute queued up commands.
3734 * Invoked from the main loop when it's safe to execute received commands.
3735 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003736 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003737 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003738channel_parse_messages(void)
3739{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003740 channel_T *channel = first_channel;
3741 int ret = FALSE;
3742 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003743 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003744
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003745 ++safe_to_invoke_callback;
3746
Bram Moolenaard0b65022016-03-06 21:50:33 +01003747 /* Only do this message when another message was given, otherwise we get
3748 * lots of them. */
3749 if (did_log_msg)
3750 {
3751 ch_log(NULL, "looking for messages on channels");
3752 did_log_msg = FALSE;
3753 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003754 while (channel != NULL)
3755 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003756 if (channel->ch_to_be_closed)
3757 {
3758 channel->ch_to_be_closed = FALSE;
3759 channel_close_now(channel);
3760 /* channel may have been freed, start over */
3761 channel = first_channel;
3762 continue;
3763 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003764 if (channel->ch_to_be_freed)
3765 {
3766 channel_free(channel);
3767 /* channel has been freed, start over */
3768 channel = first_channel;
3769 continue;
3770 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003771 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003772 {
3773 /* channel is no longer useful, free it */
3774 channel_free(channel);
3775 channel = first_channel;
3776 part = PART_SOCK;
3777 continue;
3778 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003779 if (channel->ch_part[part].ch_fd != INVALID_FD
3780 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003781 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003782 /* Increase the refcount, in case the handler causes the channel
3783 * to be unreferenced or closed. */
3784 ++channel->ch_refcount;
3785 r = may_invoke_callback(channel, part);
3786 if (r == OK)
3787 ret = TRUE;
3788 if (channel_unref(channel) || r == OK)
3789 {
3790 /* channel was freed or something was done, start over */
3791 channel = first_channel;
3792 part = PART_SOCK;
3793 continue;
3794 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003795 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003796 if (part < PART_ERR)
3797 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003798 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003799 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003800 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003801 part = PART_SOCK;
3802 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003803 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003804
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003805 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003806 {
3807 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003808 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003809 }
3810
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003811 --safe_to_invoke_callback;
3812
Bram Moolenaard7ece102016-02-02 23:23:02 +01003813 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003814}
3815
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003816/*
3817 * Mark references to lists used in channels.
3818 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003819 int
3820set_ref_in_channel(int copyID)
3821{
Bram Moolenaar77073442016-02-13 23:23:53 +01003822 int abort = FALSE;
3823 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003824 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003825
Bram Moolenaar77073442016-02-13 23:23:53 +01003826 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003827 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003828 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003829 tv.v_type = VAR_CHANNEL;
3830 tv.vval.v_channel = channel;
3831 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003832 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003833 return abort;
3834}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003835
3836/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003837 * Return the "part" to write to for "channel".
3838 */
3839 int
3840channel_part_send(channel_T *channel)
3841{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003842 if (channel->CH_SOCK_FD == INVALID_FD)
3843 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003844 return PART_SOCK;
3845}
3846
3847/*
3848 * Return the default "part" to read from for "channel".
3849 */
3850 int
3851channel_part_read(channel_T *channel)
3852{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003853 if (channel->CH_SOCK_FD == INVALID_FD)
3854 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003855 return PART_SOCK;
3856}
3857
3858/*
3859 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003860 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003861 */
3862 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003863channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003864{
Bram Moolenaar77073442016-02-13 23:23:53 +01003865 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003866 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003867 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003868}
3869
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003870/*
3871 * Return the timeout of "channel"/"part"
3872 */
3873 int
3874channel_get_timeout(channel_T *channel, int part)
3875{
3876 return channel->ch_part[part].ch_timeout;
3877}
3878
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003879 static int
3880handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3881{
3882 char_u *val = get_tv_string(item);
3883
3884 opt->jo_set |= jo;
3885 if (STRCMP(val, "nl") == 0)
3886 *modep = MODE_NL;
3887 else if (STRCMP(val, "raw") == 0)
3888 *modep = MODE_RAW;
3889 else if (STRCMP(val, "js") == 0)
3890 *modep = MODE_JS;
3891 else if (STRCMP(val, "json") == 0)
3892 *modep = MODE_JSON;
3893 else
3894 {
3895 EMSG2(_(e_invarg2), val);
3896 return FAIL;
3897 }
3898 return OK;
3899}
3900
3901 static int
3902handle_io(typval_T *item, int part, jobopt_T *opt)
3903{
3904 char_u *val = get_tv_string(item);
3905
3906 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3907 if (STRCMP(val, "null") == 0)
3908 opt->jo_io[part] = JIO_NULL;
3909 else if (STRCMP(val, "pipe") == 0)
3910 opt->jo_io[part] = JIO_PIPE;
3911 else if (STRCMP(val, "file") == 0)
3912 opt->jo_io[part] = JIO_FILE;
3913 else if (STRCMP(val, "buffer") == 0)
3914 opt->jo_io[part] = JIO_BUFFER;
3915 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3916 opt->jo_io[part] = JIO_OUT;
3917 else
3918 {
3919 EMSG2(_(e_invarg2), val);
3920 return FAIL;
3921 }
3922 return OK;
3923}
3924
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003925/*
3926 * Clear a jobopt_T before using it.
3927 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003928 void
3929clear_job_options(jobopt_T *opt)
3930{
3931 vim_memset(opt, 0, sizeof(jobopt_T));
3932}
3933
3934/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003935 * Free any members of a jobopt_T.
3936 */
3937 void
3938free_job_options(jobopt_T *opt)
3939{
3940 if (opt->jo_partial != NULL)
3941 partial_unref(opt->jo_partial);
3942 if (opt->jo_out_partial != NULL)
3943 partial_unref(opt->jo_out_partial);
3944 if (opt->jo_err_partial != NULL)
3945 partial_unref(opt->jo_err_partial);
3946 if (opt->jo_close_partial != NULL)
3947 partial_unref(opt->jo_close_partial);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003948 if (opt->jo_exit_partial != NULL)
3949 partial_unref(opt->jo_exit_partial);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003950}
3951
3952/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003953 * Get the PART_ number from the first character of an option name.
3954 */
3955 static int
3956part_from_char(int c)
3957{
3958 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3959}
3960
3961/*
3962 * Get the option entries from the dict in "tv", parse them and put the result
3963 * in "opt".
3964 * Only accept options in "supported".
3965 * If an option value is invalid return FAIL.
3966 */
3967 int
3968get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3969{
3970 typval_T *item;
3971 char_u *val;
3972 dict_T *dict;
3973 int todo;
3974 hashitem_T *hi;
3975 int part;
3976
3977 opt->jo_set = 0;
3978 if (tv->v_type == VAR_UNKNOWN)
3979 return OK;
3980 if (tv->v_type != VAR_DICT)
3981 {
3982 EMSG(_(e_invarg));
3983 return FAIL;
3984 }
3985 dict = tv->vval.v_dict;
3986 if (dict == NULL)
3987 return OK;
3988
3989 todo = (int)dict->dv_hashtab.ht_used;
3990 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3991 if (!HASHITEM_EMPTY(hi))
3992 {
3993 item = &dict_lookup(hi)->di_tv;
3994
3995 if (STRCMP(hi->hi_key, "mode") == 0)
3996 {
3997 if (!(supported & JO_MODE))
3998 break;
3999 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4000 return FAIL;
4001 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004002 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004003 {
4004 if (!(supported & JO_IN_MODE))
4005 break;
4006 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4007 == FAIL)
4008 return FAIL;
4009 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004010 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004011 {
4012 if (!(supported & JO_OUT_MODE))
4013 break;
4014 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4015 == FAIL)
4016 return FAIL;
4017 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004018 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004019 {
4020 if (!(supported & JO_ERR_MODE))
4021 break;
4022 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4023 == FAIL)
4024 return FAIL;
4025 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004026 else if (STRCMP(hi->hi_key, "in_io") == 0
4027 || STRCMP(hi->hi_key, "out_io") == 0
4028 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004029 {
4030 if (!(supported & JO_OUT_IO))
4031 break;
4032 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4033 return FAIL;
4034 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004035 else if (STRCMP(hi->hi_key, "in_name") == 0
4036 || STRCMP(hi->hi_key, "out_name") == 0
4037 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004038 {
4039 part = part_from_char(*hi->hi_key);
4040
4041 if (!(supported & JO_OUT_IO))
4042 break;
4043 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4044 opt->jo_io_name[part] =
4045 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4046 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004047 else if (STRCMP(hi->hi_key, "in_buf") == 0
4048 || STRCMP(hi->hi_key, "out_buf") == 0
4049 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004050 {
4051 part = part_from_char(*hi->hi_key);
4052
4053 if (!(supported & JO_OUT_IO))
4054 break;
4055 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4056 opt->jo_io_buf[part] = get_tv_number(item);
4057 if (opt->jo_io_buf[part] <= 0)
4058 {
4059 EMSG2(_(e_invarg2), get_tv_string(item));
4060 return FAIL;
4061 }
4062 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4063 {
4064 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4065 return FAIL;
4066 }
4067 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004068 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4069 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4070 {
4071 part = part_from_char(*hi->hi_key);
4072
4073 if (!(supported & JO_OUT_IO))
4074 break;
4075 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4076 opt->jo_modifiable[part] = get_tv_number(item);
4077 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004078 else if (STRCMP(hi->hi_key, "in_top") == 0
4079 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004080 {
4081 linenr_T *lp;
4082
4083 if (!(supported & JO_OUT_IO))
4084 break;
4085 if (hi->hi_key[3] == 't')
4086 {
4087 lp = &opt->jo_in_top;
4088 opt->jo_set |= JO_IN_TOP;
4089 }
4090 else
4091 {
4092 lp = &opt->jo_in_bot;
4093 opt->jo_set |= JO_IN_BOT;
4094 }
4095 *lp = get_tv_number(item);
4096 if (*lp < 0)
4097 {
4098 EMSG2(_(e_invarg2), get_tv_string(item));
4099 return FAIL;
4100 }
4101 }
4102 else if (STRCMP(hi->hi_key, "channel") == 0)
4103 {
4104 if (!(supported & JO_OUT_IO))
4105 break;
4106 opt->jo_set |= JO_CHANNEL;
4107 if (item->v_type != VAR_CHANNEL)
4108 {
4109 EMSG2(_(e_invarg2), "channel");
4110 return FAIL;
4111 }
4112 opt->jo_channel = item->vval.v_channel;
4113 }
4114 else if (STRCMP(hi->hi_key, "callback") == 0)
4115 {
4116 if (!(supported & JO_CALLBACK))
4117 break;
4118 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004119 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004120 if (opt->jo_callback == NULL)
4121 {
4122 EMSG2(_(e_invarg2), "callback");
4123 return FAIL;
4124 }
4125 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004126 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004127 {
4128 if (!(supported & JO_OUT_CALLBACK))
4129 break;
4130 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004131 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004132 if (opt->jo_out_cb == NULL)
4133 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004134 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004135 return FAIL;
4136 }
4137 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004138 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004139 {
4140 if (!(supported & JO_ERR_CALLBACK))
4141 break;
4142 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004143 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004144 if (opt->jo_err_cb == NULL)
4145 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004146 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004147 return FAIL;
4148 }
4149 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004150 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004151 {
4152 if (!(supported & JO_CLOSE_CALLBACK))
4153 break;
4154 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004155 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004156 if (opt->jo_close_cb == NULL)
4157 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004158 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004159 return FAIL;
4160 }
4161 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004162 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4163 {
4164 if (!(supported & JO_EXIT_CB))
4165 break;
4166 opt->jo_set |= JO_EXIT_CB;
4167 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4168 if (opt->jo_exit_cb == NULL)
4169 {
4170 EMSG2(_(e_invarg2), "exit_cb");
4171 return FAIL;
4172 }
4173 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004174 else if (STRCMP(hi->hi_key, "waittime") == 0)
4175 {
4176 if (!(supported & JO_WAITTIME))
4177 break;
4178 opt->jo_set |= JO_WAITTIME;
4179 opt->jo_waittime = get_tv_number(item);
4180 }
4181 else if (STRCMP(hi->hi_key, "timeout") == 0)
4182 {
4183 if (!(supported & JO_TIMEOUT))
4184 break;
4185 opt->jo_set |= JO_TIMEOUT;
4186 opt->jo_timeout = get_tv_number(item);
4187 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004188 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004189 {
4190 if (!(supported & JO_OUT_TIMEOUT))
4191 break;
4192 opt->jo_set |= JO_OUT_TIMEOUT;
4193 opt->jo_out_timeout = get_tv_number(item);
4194 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004195 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004196 {
4197 if (!(supported & JO_ERR_TIMEOUT))
4198 break;
4199 opt->jo_set |= JO_ERR_TIMEOUT;
4200 opt->jo_err_timeout = get_tv_number(item);
4201 }
4202 else if (STRCMP(hi->hi_key, "part") == 0)
4203 {
4204 if (!(supported & JO_PART))
4205 break;
4206 opt->jo_set |= JO_PART;
4207 val = get_tv_string(item);
4208 if (STRCMP(val, "err") == 0)
4209 opt->jo_part = PART_ERR;
4210 else
4211 {
4212 EMSG2(_(e_invarg2), val);
4213 return FAIL;
4214 }
4215 }
4216 else if (STRCMP(hi->hi_key, "id") == 0)
4217 {
4218 if (!(supported & JO_ID))
4219 break;
4220 opt->jo_set |= JO_ID;
4221 opt->jo_id = get_tv_number(item);
4222 }
4223 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4224 {
4225 if (!(supported & JO_STOPONEXIT))
4226 break;
4227 opt->jo_set |= JO_STOPONEXIT;
4228 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4229 opt->jo_soe_buf);
4230 if (opt->jo_stoponexit == NULL)
4231 {
4232 EMSG2(_(e_invarg2), "stoponexit");
4233 return FAIL;
4234 }
4235 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004236 else if (STRCMP(hi->hi_key, "block_write") == 0)
4237 {
4238 if (!(supported & JO_BLOCK_WRITE))
4239 break;
4240 opt->jo_set |= JO_BLOCK_WRITE;
4241 opt->jo_block_write = get_tv_number(item);
4242 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004243 else
4244 break;
4245 --todo;
4246 }
4247 if (todo > 0)
4248 {
4249 EMSG2(_(e_invarg2), hi->hi_key);
4250 return FAIL;
4251 }
4252
4253 return OK;
4254}
4255
4256/*
4257 * Get the channel from the argument.
4258 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004259 * When "check_open" is TRUE check that the channel can be used.
4260 * When "reading" is TRUE "check_open" considers typeahead useful.
4261 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004262 */
4263 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004264get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004265{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004266 channel_T *channel = NULL;
4267 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004268
4269 if (tv->v_type == VAR_JOB)
4270 {
4271 if (tv->vval.v_job != NULL)
4272 channel = tv->vval.v_job->jv_channel;
4273 }
4274 else if (tv->v_type == VAR_CHANNEL)
4275 {
4276 channel = tv->vval.v_channel;
4277 }
4278 else
4279 {
4280 EMSG2(_(e_invarg2), get_tv_string(tv));
4281 return NULL;
4282 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004283 if (channel != NULL && reading)
4284 has_readahead = channel_has_readahead(channel,
4285 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004286
Bram Moolenaar437905c2016-04-26 19:01:05 +02004287 if (check_open && (channel == NULL || (!channel_is_open(channel)
4288 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004289 {
4290 EMSG(_("E906: not an open channel"));
4291 return NULL;
4292 }
4293 return channel;
4294}
4295
4296static job_T *first_job = NULL;
4297
4298 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004299job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004300{
4301 ch_log(job->jv_channel, "Freeing job");
4302 if (job->jv_channel != NULL)
4303 {
4304 /* The link from the channel to the job doesn't count as a reference,
4305 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004306 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004307 * NULL the reference. We don't set ch_job_killed, unreferencing the
4308 * job doesn't mean it stops running. */
4309 job->jv_channel->ch_job = NULL;
4310 channel_unref(job->jv_channel);
4311 }
4312 mch_clear_job(job);
4313
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004314 vim_free(job->jv_stoponexit);
4315 vim_free(job->jv_exit_cb);
4316 partial_unref(job->jv_exit_partial);
4317}
4318
4319 static void
4320job_free_job(job_T *job)
4321{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004322 if (job->jv_next != NULL)
4323 job->jv_next->jv_prev = job->jv_prev;
4324 if (job->jv_prev == NULL)
4325 first_job = job->jv_next;
4326 else
4327 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004328 vim_free(job);
4329}
4330
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004331 static void
4332job_free(job_T *job)
4333{
4334 if (!in_free_unref_items)
4335 {
4336 job_free_contents(job);
4337 job_free_job(job);
4338 }
4339}
4340
Bram Moolenaar655da312016-05-28 22:22:34 +02004341#if defined(EXITFREE) || defined(PROTO)
4342 void
4343job_free_all(void)
4344{
4345 while (first_job != NULL)
4346 job_free(first_job);
4347}
4348#endif
4349
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004350/*
4351 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004352 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4353 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004354 */
4355 static int
4356job_still_useful(job_T *job)
4357{
4358 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004359 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4360 || (job->jv_channel != NULL
4361 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004362}
4363
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004364/*
4365 * Mark references in jobs that are still useful.
4366 */
4367 int
4368set_ref_in_job(int copyID)
4369{
4370 int abort = FALSE;
4371 job_T *job;
4372 typval_T tv;
4373
4374 for (job = first_job; job != NULL; job = job->jv_next)
4375 if (job_still_useful(job))
4376 {
4377 tv.v_type = VAR_JOB;
4378 tv.vval.v_job = job;
4379 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4380 }
4381 return abort;
4382}
4383
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004384 void
4385job_unref(job_T *job)
4386{
4387 if (job != NULL && --job->jv_refcount <= 0)
4388 {
4389 /* Do not free the job when it has not ended yet and there is a
4390 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004391 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004392 {
4393 job_free(job);
4394 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004395 else if (job->jv_channel != NULL
4396 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004397 {
4398 /* Do remove the link to the channel, otherwise it hangs
4399 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004400 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004401 job->jv_channel->ch_job = NULL;
4402 channel_unref(job->jv_channel);
4403 job->jv_channel = NULL;
4404 }
4405 }
4406}
4407
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004408 int
4409free_unused_jobs_contents(int copyID, int mask)
4410{
4411 int did_free = FALSE;
4412 job_T *job;
4413
4414 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004415 if ((job->jv_copyID & mask) != (copyID & mask)
4416 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004417 {
4418 /* Free the channel and ordinary items it contains, but don't
4419 * recurse into Lists, Dictionaries etc. */
4420 job_free_contents(job);
4421 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004422 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004423 return did_free;
4424}
4425
4426 void
4427free_unused_jobs(int copyID, int mask)
4428{
4429 job_T *job;
4430 job_T *job_next;
4431
4432 for (job = first_job; job != NULL; job = job_next)
4433 {
4434 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004435 if ((job->jv_copyID & mask) != (copyID & mask)
4436 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004437 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004438 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004439 job_free_job(job);
4440 }
4441 }
4442}
4443
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004444/*
4445 * Allocate a job. Sets the refcount to one and sets options default.
4446 */
4447 static job_T *
4448job_alloc(void)
4449{
4450 job_T *job;
4451
4452 job = (job_T *)alloc_clear(sizeof(job_T));
4453 if (job != NULL)
4454 {
4455 job->jv_refcount = 1;
4456 job->jv_stoponexit = vim_strsave((char_u *)"term");
4457
4458 if (first_job != NULL)
4459 {
4460 first_job->jv_prev = job;
4461 job->jv_next = first_job;
4462 }
4463 first_job = job;
4464 }
4465 return job;
4466}
4467
4468 void
4469job_set_options(job_T *job, jobopt_T *opt)
4470{
4471 if (opt->jo_set & JO_STOPONEXIT)
4472 {
4473 vim_free(job->jv_stoponexit);
4474 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4475 job->jv_stoponexit = NULL;
4476 else
4477 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4478 }
4479 if (opt->jo_set & JO_EXIT_CB)
4480 {
4481 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004482 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004483 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004484 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004485 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004486 job->jv_exit_partial = NULL;
4487 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004488 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004489 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004490 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004491 job->jv_exit_partial = opt->jo_exit_partial;
4492 if (job->jv_exit_partial != NULL)
4493 ++job->jv_exit_partial->pt_refcount;
4494 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004495 }
4496}
4497
4498/*
4499 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4500 */
4501 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004502job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004503{
4504 job_T *job;
4505
4506 for (job = first_job; job != NULL; job = job->jv_next)
4507 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4508 mch_stop_job(job, job->jv_stoponexit);
4509}
4510
4511/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004512 * Return TRUE when there is any job that might exit, which means
4513 * job_check_ended() should be called once in a while.
4514 */
4515 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004516has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004517{
4518 job_T *job;
4519
4520 for (job = first_job; job != NULL; job = job->jv_next)
4521 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4522 return TRUE;
4523 return FALSE;
4524}
4525
4526/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004527 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004528 */
4529 void
4530job_check_ended(void)
4531{
4532 static time_t last_check = 0;
4533 time_t now;
4534 job_T *job;
4535 job_T *next;
4536
4537 /* Only do this once in 10 seconds. */
4538 now = time(NULL);
4539 if (last_check + 10 < now)
4540 {
4541 last_check = now;
4542 for (job = first_job; job != NULL; job = next)
4543 {
4544 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004545 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004546 job_status(job); /* may free "job" */
4547 }
4548 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004549 if (channel_need_redraw)
4550 {
4551 channel_need_redraw = FALSE;
4552 redraw_after_callback();
4553 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004554}
4555
4556/*
4557 * "job_start()" function
4558 */
4559 job_T *
4560job_start(typval_T *argvars)
4561{
4562 job_T *job;
4563 char_u *cmd = NULL;
4564#if defined(UNIX)
4565# define USE_ARGV
4566 char **argv = NULL;
4567 int argc = 0;
4568#else
4569 garray_T ga;
4570#endif
4571 jobopt_T opt;
4572 int part;
4573
4574 job = job_alloc();
4575 if (job == NULL)
4576 return NULL;
4577
4578 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004579#ifndef USE_ARGV
4580 ga_init2(&ga, (int)sizeof(char*), 20);
4581#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004582
4583 /* Default mode is NL. */
4584 clear_job_options(&opt);
4585 opt.jo_mode = MODE_NL;
4586 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004587 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4588 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004589 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004590
4591 /* Check that when io is "file" that there is a file name. */
4592 for (part = PART_OUT; part <= PART_IN; ++part)
4593 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4594 && opt.jo_io[part] == JIO_FILE
4595 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4596 || *opt.jo_io_name[part] == NUL))
4597 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004598 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004599 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004600 }
4601
4602 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4603 {
4604 buf_T *buf = NULL;
4605
4606 /* check that we can find the buffer before starting the job */
4607 if (opt.jo_set & JO_IN_BUF)
4608 {
4609 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4610 if (buf == NULL)
4611 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4612 }
4613 else if (!(opt.jo_set & JO_IN_NAME))
4614 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004615 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004616 }
4617 else
4618 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4619 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004620 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004621 if (buf->b_ml.ml_mfp == NULL)
4622 {
4623 char_u numbuf[NUMBUFLEN];
4624 char_u *s;
4625
4626 if (opt.jo_set & JO_IN_BUF)
4627 {
4628 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4629 s = numbuf;
4630 }
4631 else
4632 s = opt.jo_io_name[PART_IN];
4633 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004634 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004635 }
4636 job->jv_in_buf = buf;
4637 }
4638
4639 job_set_options(job, &opt);
4640
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004641 if (argvars[0].v_type == VAR_STRING)
4642 {
4643 /* Command is a string. */
4644 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004645 if (cmd == NULL || *cmd == NUL)
4646 {
4647 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004648 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004649 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004650#ifdef USE_ARGV
4651 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004652 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004653 argv[argc] = NULL;
4654#endif
4655 }
4656 else if (argvars[0].v_type != VAR_LIST
4657 || argvars[0].vval.v_list == NULL
4658 || argvars[0].vval.v_list->lv_len < 1)
4659 {
4660 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004661 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004662 }
4663 else
4664 {
4665 list_T *l = argvars[0].vval.v_list;
4666 listitem_T *li;
4667 char_u *s;
4668
4669#ifdef USE_ARGV
4670 /* Pass argv[] to mch_call_shell(). */
4671 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4672 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004673 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004674#endif
4675 for (li = l->lv_first; li != NULL; li = li->li_next)
4676 {
4677 s = get_tv_string_chk(&li->li_tv);
4678 if (s == NULL)
4679 goto theend;
4680#ifdef USE_ARGV
4681 argv[argc++] = (char *)s;
4682#else
4683 /* Only escape when needed, double quotes are not always allowed. */
4684 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4685 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004686# ifdef WIN32
4687 int old_ssl = p_ssl;
4688
4689 /* This is using CreateProcess, not cmd.exe. Always use
4690 * double quote and backslashes. */
4691 p_ssl = 0;
4692# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004693 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004694# ifdef WIN32
4695 p_ssl = old_ssl;
4696# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004697 if (s == NULL)
4698 goto theend;
4699 ga_concat(&ga, s);
4700 vim_free(s);
4701 }
4702 else
4703 ga_concat(&ga, s);
4704 if (li->li_next != NULL)
4705 ga_append(&ga, ' ');
4706#endif
4707 }
4708#ifdef USE_ARGV
4709 argv[argc] = NULL;
4710#else
4711 cmd = ga.ga_data;
4712#endif
4713 }
4714
4715#ifdef USE_ARGV
4716 if (ch_log_active())
4717 {
4718 garray_T ga;
4719 int i;
4720
4721 ga_init2(&ga, (int)sizeof(char), 200);
4722 for (i = 0; i < argc; ++i)
4723 {
4724 if (i > 0)
4725 ga_concat(&ga, (char_u *)" ");
4726 ga_concat(&ga, (char_u *)argv[i]);
4727 }
4728 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4729 ga_clear(&ga);
4730 }
4731 mch_start_job(argv, job, &opt);
4732#else
4733 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4734 mch_start_job((char *)cmd, job, &opt);
4735#endif
4736
4737 /* If the channel is reading from a buffer, write lines now. */
4738 if (job->jv_channel != NULL)
4739 channel_write_in(job->jv_channel);
4740
4741theend:
4742#ifdef USE_ARGV
4743 vim_free(argv);
4744#else
4745 vim_free(ga.ga_data);
4746#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004747 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004748 return job;
4749}
4750
4751/*
4752 * Get the status of "job" and invoke the exit callback when needed.
4753 * The returned string is not allocated.
4754 */
4755 char *
4756job_status(job_T *job)
4757{
4758 char *result;
4759
4760 if (job->jv_status == JOB_ENDED)
4761 /* No need to check, dead is dead. */
4762 result = "dead";
4763 else if (job->jv_status == JOB_FAILED)
4764 result = "fail";
4765 else
4766 {
4767 result = mch_job_status(job);
4768 if (job->jv_status == JOB_ENDED)
4769 ch_log(job->jv_channel, "Job ended");
4770 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4771 {
4772 typval_T argv[3];
4773 typval_T rettv;
4774 int dummy;
4775
4776 /* invoke the exit callback; make sure the refcount is > 0 */
4777 ++job->jv_refcount;
4778 argv[0].v_type = VAR_JOB;
4779 argv[0].vval.v_job = job;
4780 argv[1].v_type = VAR_NUMBER;
4781 argv[1].vval.v_number = job->jv_exitval;
4782 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004783 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4784 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004785 clear_tv(&rettv);
4786 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004787 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004788 }
4789 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4790 {
4791 /* The job was already unreferenced, now that it ended it can be
4792 * freed. Careful: caller must not use "job" after this! */
4793 job_free(job);
4794 }
4795 }
4796 return result;
4797}
4798
Bram Moolenaar8950a562016-03-12 15:22:55 +01004799/*
4800 * Implementation of job_info().
4801 */
4802 void
4803job_info(job_T *job, dict_T *dict)
4804{
4805 dictitem_T *item;
4806 varnumber_T nr;
4807
4808 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4809
4810 item = dictitem_alloc((char_u *)"channel");
4811 if (item == NULL)
4812 return;
4813 item->di_tv.v_lock = 0;
4814 item->di_tv.v_type = VAR_CHANNEL;
4815 item->di_tv.vval.v_channel = job->jv_channel;
4816 if (job->jv_channel != NULL)
4817 ++job->jv_channel->ch_refcount;
4818 if (dict_add(dict, item) == FAIL)
4819 dictitem_free(item);
4820
4821#ifdef UNIX
4822 nr = job->jv_pid;
4823#else
4824 nr = job->jv_proc_info.dwProcessId;
4825#endif
4826 dict_add_nr_str(dict, "process", nr, NULL);
4827
4828 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004829 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004830 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4831}
4832
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004833 int
4834job_stop(job_T *job, typval_T *argvars)
4835{
4836 char_u *arg;
4837
4838 if (argvars[1].v_type == VAR_UNKNOWN)
4839 arg = (char_u *)"";
4840 else
4841 {
4842 arg = get_tv_string_chk(&argvars[1]);
4843 if (arg == NULL)
4844 {
4845 EMSG(_(e_invarg));
4846 return 0;
4847 }
4848 }
4849 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4850 if (mch_stop_job(job, arg) == FAIL)
4851 return 0;
4852
4853 /* Assume that "hup" does not kill the job. */
4854 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4855 job->jv_channel->ch_job_killed = TRUE;
4856
4857 /* We don't try freeing the job, obviously the caller still has a
4858 * reference to it. */
4859 return 1;
4860}
4861
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004862#endif /* FEAT_JOB_CHANNEL */