blob: bb818dcd17101ffeaf2789b87bb539e7a1e082c9 [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;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001316 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001317
Bram Moolenaar655da312016-05-28 22:22:34 +02001318 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001319 if ((p = alloc(len + 2)) == NULL)
1320 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001321 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001322
1323 for (i = 0; i < len; ++i)
1324 if (p[i] == NL)
1325 p[i] = NUL;
1326
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001327 p[len] = NL;
1328 p[len + 1] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001329 channel_send(channel, PART_IN, p, len + 1, "write_buf_line()");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001330 vim_free(p);
1331}
1332
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001333/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001334 * Return TRUE if "channel" can be written to.
1335 * Returns FALSE if the input is closed or the write would block.
1336 */
1337 static int
1338can_write_buf_line(channel_T *channel)
1339{
1340 chanpart_T *in_part = &channel->ch_part[PART_IN];
1341
1342 if (in_part->ch_fd == INVALID_FD)
1343 return FALSE; /* pipe was closed */
1344
1345 /* for testing: block every other attempt to write */
1346 if (in_part->ch_block_write == 1)
1347 in_part->ch_block_write = -1;
1348 else if (in_part->ch_block_write == -1)
1349 in_part->ch_block_write = 1;
1350
1351 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1352#ifndef WIN32
1353 {
1354# if defined(HAVE_SELECT)
1355 struct timeval tval;
1356 fd_set wfds;
1357 int ret;
1358
1359 FD_ZERO(&wfds);
1360 FD_SET((int)in_part->ch_fd, &wfds);
1361 tval.tv_sec = 0;
1362 tval.tv_usec = 0;
1363 for (;;)
1364 {
1365 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1366# ifdef EINTR
1367 SOCK_ERRNO;
1368 if (ret == -1 && errno == EINTR)
1369 continue;
1370# endif
1371 if (ret <= 0 || in_part->ch_block_write == 1)
1372 {
1373 if (ret > 0)
1374 ch_log(channel, "FAKED Input not ready for writing");
1375 else
1376 ch_log(channel, "Input not ready for writing");
1377 return FALSE;
1378 }
1379 break;
1380 }
1381# else
1382 struct pollfd fds;
1383
1384 fds.fd = in_part->ch_fd;
1385 fds.events = POLLOUT;
1386 if (poll(&fds, 1, 0) <= 0)
1387 {
1388 ch_log(channel, "Input not ready for writing");
1389 return FALSE;
1390 }
1391 if (in_part->ch_block_write == 1)
1392 {
1393 ch_log(channel, "FAKED Input not ready for writing");
1394 return FALSE;
1395 }
1396# endif
1397 }
1398#endif
1399 return TRUE;
1400}
1401
1402/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001403 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001404 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001405 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001406channel_write_in(channel_T *channel)
1407{
1408 chanpart_T *in_part = &channel->ch_part[PART_IN];
1409 linenr_T lnum;
1410 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001411 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001412
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001413 if (buf == NULL || in_part->ch_buf_append)
1414 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001415 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1416 {
1417 /* buffer was wiped out or unloaded */
1418 in_part->ch_buffer = NULL;
1419 return;
1420 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001421
1422 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1423 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1424 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001425 if (!can_write_buf_line(channel))
1426 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001427 write_buf_line(buf, lnum, channel);
1428 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001429 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001430
1431 if (written == 1)
1432 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1433 else if (written > 1)
1434 ch_logn(channel, "written %d lines to channel", written);
1435
Bram Moolenaar014069a2016-03-03 22:51:40 +01001436 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001437 if (lnum > buf->b_ml.ml_line_count)
1438 {
1439 /* Writing is done, no longer need the buffer. */
1440 in_part->ch_buffer = NULL;
1441 ch_log(channel, "Finished writing all lines to channel");
1442 }
1443 else
1444 ch_logn(channel, "Still %d more lines to write",
1445 buf->b_ml.ml_line_count - lnum + 1);
1446}
1447
1448/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001449 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001450 */
1451 void
1452channel_buffer_free(buf_T *buf)
1453{
1454 channel_T *channel;
1455 int part;
1456
1457 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1458 for (part = PART_SOCK; part <= PART_IN; ++part)
1459 {
1460 chanpart_T *ch_part = &channel->ch_part[part];
1461
1462 if (ch_part->ch_buffer == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001463 {
1464 ch_logs(channel, "%s buffer has been wiped out",
1465 part_names[part]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001466 ch_part->ch_buffer = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001467 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001468 }
1469}
1470
1471/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001472 * Write any lines waiting to be written to a channel.
1473 */
1474 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001475channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001476{
1477 channel_T *channel;
1478
1479 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1480 {
1481 chanpart_T *in_part = &channel->ch_part[PART_IN];
1482
1483 if (in_part->ch_buffer != NULL)
1484 {
1485 if (in_part->ch_buf_append)
1486 channel_write_new_lines(in_part->ch_buffer);
1487 else
1488 channel_write_in(channel);
1489 }
1490 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001491}
1492
1493/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001494 * Write appended lines above the last one in "buf" to the channel.
1495 */
1496 void
1497channel_write_new_lines(buf_T *buf)
1498{
1499 channel_T *channel;
1500 int found_one = FALSE;
1501
1502 /* There could be more than one channel for the buffer, loop over all of
1503 * them. */
1504 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1505 {
1506 chanpart_T *in_part = &channel->ch_part[PART_IN];
1507 linenr_T lnum;
1508 int written = 0;
1509
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001510 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001511 {
1512 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001513 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001514 found_one = TRUE;
1515 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1516 ++lnum)
1517 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001518 if (!can_write_buf_line(channel))
1519 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001520 write_buf_line(buf, lnum, channel);
1521 ++written;
1522 }
1523
1524 if (written == 1)
1525 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1526 else if (written > 1)
1527 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001528 if (lnum < buf->b_ml.ml_line_count)
1529 ch_logn(channel, "Still %d more lines to write",
1530 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001531
1532 in_part->ch_buf_bot = lnum;
1533 }
1534 }
1535 if (!found_one)
1536 buf->b_write_to_channel = FALSE;
1537}
1538
1539/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001540 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001541 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001542 */
1543 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001544invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1545 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001546{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001547 typval_T rettv;
1548 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001549
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001550 if (safe_to_invoke_callback == 0)
1551 EMSG("INTERNAL: Invoking callback when it is not safe");
1552
Bram Moolenaar77073442016-02-13 23:23:53 +01001553 argv[0].v_type = VAR_CHANNEL;
1554 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001555
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001556 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001557 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001558 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001559 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001560}
1561
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001562/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001563 * Return the first node from "channel"/"part" without removing it.
1564 * Returns NULL if there is nothing.
1565 */
1566 readq_T *
1567channel_peek(channel_T *channel, int part)
1568{
1569 readq_T *head = &channel->ch_part[part].ch_head;
1570
1571 return head->rq_next;
1572}
1573
1574/*
1575 * Return a pointer to the first NL in "node".
1576 * Skips over NUL characters.
1577 * Returns NULL if there is no NL.
1578 */
1579 char_u *
1580channel_first_nl(readq_T *node)
1581{
1582 char_u *buffer = node->rq_buffer;
1583 long_u i;
1584
1585 for (i = 0; i < node->rq_buflen; ++i)
1586 if (buffer[i] == NL)
1587 return buffer + i;
1588 return NULL;
1589}
1590
1591/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001592 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001593 * The caller must free it.
1594 * Returns NULL if there is nothing.
1595 */
1596 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001597channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001598{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001599 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001600 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001601 char_u *p;
1602
Bram Moolenaar77073442016-02-13 23:23:53 +01001603 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001604 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001605 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001606 p = node->rq_buffer;
1607 head->rq_next = node->rq_next;
1608 if (node->rq_next == NULL)
1609 head->rq_prev = NULL;
1610 else
1611 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001612 vim_free(node);
1613 return p;
1614}
1615
1616/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001617 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001618 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001619 */
1620 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001621channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001622{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001623 readq_T *head = &channel->ch_part[part].ch_head;
1624 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001625 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001626 char_u *res;
1627 char_u *p;
1628
1629 /* If there is only one buffer just get that one. */
1630 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1631 return channel_get(channel, part);
1632
1633 /* Concatenate everything into one buffer. */
1634 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001635 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001636 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001637 if (res == NULL)
1638 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001639 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001640 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001641 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001642 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001643 p += node->rq_buflen;
1644 }
1645 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001646
1647 /* Free all buffers */
1648 do
1649 {
1650 p = channel_get(channel, part);
1651 vim_free(p);
1652 } while (p != NULL);
1653
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001654 /* turn all NUL into NL */
1655 while (len > 0)
1656 {
1657 --len;
1658 if (res[len] == NUL)
1659 res[len] = NL;
1660 }
1661
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001662 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001663}
1664
1665/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001666 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001667 * Caller must check these bytes are available.
1668 */
1669 void
1670channel_consume(channel_T *channel, int part, int len)
1671{
1672 readq_T *head = &channel->ch_part[part].ch_head;
1673 readq_T *node = head->rq_next;
1674 char_u *buf = node->rq_buffer;
1675
1676 mch_memmove(buf, buf + len, node->rq_buflen - len);
1677 node->rq_buflen -= len;
1678}
1679
1680/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001681 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001682 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001683 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001684 */
1685 int
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001686channel_collapse(channel_T *channel, int part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001687{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001688 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001689 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001690 readq_T *last_node;
1691 readq_T *n;
1692 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001693 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001694 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001695
Bram Moolenaar77073442016-02-13 23:23:53 +01001696 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001697 return FAIL;
1698
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001699 last_node = node->rq_next;
1700 len = node->rq_buflen + last_node->rq_buflen + 1;
1701 if (want_nl)
1702 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001703 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001704 {
1705 last_node = last_node->rq_next;
1706 len += last_node->rq_buflen;
1707 }
1708
1709 p = newbuf = alloc(len);
1710 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001711 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001712 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001713 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001714 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001715 node->rq_buffer = newbuf;
1716 for (n = node; n != last_node; )
1717 {
1718 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001719 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001720 p += n->rq_buflen;
1721 vim_free(n->rq_buffer);
1722 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001723 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001724
1725 /* dispose of the collapsed nodes and their buffers */
1726 for (n = node->rq_next; n != last_node; )
1727 {
1728 n = n->rq_next;
1729 vim_free(n->rq_prev);
1730 }
1731 node->rq_next = last_node->rq_next;
1732 if (last_node->rq_next == NULL)
1733 head->rq_prev = node;
1734 else
1735 last_node->rq_next->rq_prev = node;
1736 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001737 return OK;
1738}
1739
1740/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001741 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001742 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001743 * Returns OK or FAIL.
1744 */
1745 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001746channel_save(channel_T *channel, int part, char_u *buf, int len,
1747 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001748{
1749 readq_T *node;
1750 readq_T *head = &channel->ch_part[part].ch_head;
1751 char_u *p;
1752 int i;
1753
1754 node = (readq_T *)alloc(sizeof(readq_T));
1755 if (node == NULL)
1756 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001757 /* A NUL is added at the end, because netbeans code expects that.
1758 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001759 node->rq_buffer = alloc(len + 1);
1760 if (node->rq_buffer == NULL)
1761 {
1762 vim_free(node);
1763 return FAIL; /* out of memory */
1764 }
1765
1766 if (channel->ch_part[part].ch_mode == MODE_NL)
1767 {
1768 /* Drop any CR before a NL. */
1769 p = node->rq_buffer;
1770 for (i = 0; i < len; ++i)
1771 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1772 *p++ = buf[i];
1773 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001774 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001775 }
1776 else
1777 {
1778 mch_memmove(node->rq_buffer, buf, len);
1779 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001780 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001781 }
1782
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001783 if (prepend)
1784 {
1785 /* preend node to the head of the queue */
1786 node->rq_next = head->rq_next;
1787 node->rq_prev = NULL;
1788 if (head->rq_next == NULL)
1789 head->rq_prev = node;
1790 else
1791 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001792 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001793 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001794 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001795 {
1796 /* append node to the tail of the queue */
1797 node->rq_next = NULL;
1798 node->rq_prev = head->rq_prev;
1799 if (head->rq_prev == NULL)
1800 head->rq_next = node;
1801 else
1802 head->rq_prev->rq_next = node;
1803 head->rq_prev = node;
1804 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001805
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001806 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001807 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001808 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001809 fprintf(log_fd, "'");
1810 if (fwrite(buf, len, 1, log_fd) != 1)
1811 return FAIL;
1812 fprintf(log_fd, "'\n");
1813 }
1814 return OK;
1815}
1816
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001817 static int
1818channel_fill(js_read_T *reader)
1819{
1820 channel_T *channel = (channel_T *)reader->js_cookie;
1821 int part = reader->js_cookie_arg;
1822 char_u *next = channel_get(channel, part);
1823 int unused;
1824 int len;
1825 char_u *p;
1826
1827 if (next == NULL)
1828 return FALSE;
1829
1830 unused = reader->js_end - reader->js_buf - reader->js_used;
1831 if (unused > 0)
1832 {
1833 /* Prepend unused text. */
1834 len = (int)STRLEN(next);
1835 p = alloc(unused + len + 1);
1836 if (p == NULL)
1837 {
1838 vim_free(next);
1839 return FALSE;
1840 }
1841 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1842 mch_memmove(p + unused, next, len + 1);
1843 vim_free(next);
1844 next = p;
1845 }
1846
1847 vim_free(reader->js_buf);
1848 reader->js_buf = next;
1849 reader->js_used = 0;
1850 return TRUE;
1851}
1852
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001853/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001854 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001855 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001856 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001857 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001858 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001859channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001860{
1861 js_read_T reader;
1862 typval_T listtv;
1863 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001864 chanpart_T *chanpart = &channel->ch_part[part];
1865 jsonq_T *head = &chanpart->ch_json_head;
1866 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001867 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001868
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001869 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001870 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001871
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001872 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001873 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001874 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001875 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001876 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001877
1878 /* When a message is incomplete we wait for a short while for more to
1879 * arrive. After the delay drop the input, otherwise a truncated string
1880 * or list will make us hang. */
1881 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001882 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001883 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001884 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001885 /* Only accept the response when it is a list with at least two
1886 * items. */
1887 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001888 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001889 if (listtv.v_type != VAR_LIST)
1890 ch_error(channel, "Did not receive a list, discarding");
1891 else
1892 ch_errorn(channel, "Expected list with two items, got %d",
1893 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001894 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001895 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001896 else
1897 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001898 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1899 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001900 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001901 else
1902 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001903 item->jq_value = alloc_tv();
1904 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001905 {
1906 vim_free(item);
1907 clear_tv(&listtv);
1908 }
1909 else
1910 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001911 *item->jq_value = listtv;
1912 item->jq_prev = head->jq_prev;
1913 head->jq_prev = item;
1914 item->jq_next = NULL;
1915 if (item->jq_prev == NULL)
1916 head->jq_next = item;
1917 else
1918 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001919 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001920 }
1921 }
1922 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001923
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001924 if (status == OK)
1925 chanpart->ch_waiting = FALSE;
1926 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001927 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001928 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001929 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001930 /* First time encountering incomplete message, set a deadline of
1931 * 100 msec. */
1932 ch_log(channel, "Incomplete message - wait for more");
1933 reader.js_used = 0;
1934 chanpart->ch_waiting = TRUE;
1935#ifdef WIN32
1936 chanpart->ch_deadline = GetTickCount() + 100L;
1937#else
1938 gettimeofday(&chanpart->ch_deadline, NULL);
1939 chanpart->ch_deadline.tv_usec += 100 * 1000;
1940 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1941 {
1942 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1943 ++chanpart->ch_deadline.tv_sec;
1944 }
1945#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001946 }
1947 else
1948 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001949 int timeout;
1950#ifdef WIN32
1951 timeout = GetTickCount() > chanpart->ch_deadline;
1952#else
1953 {
1954 struct timeval now_tv;
1955
1956 gettimeofday(&now_tv, NULL);
1957 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1958 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1959 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1960 }
1961#endif
1962 if (timeout)
1963 {
1964 status = FAIL;
1965 chanpart->ch_waiting = FALSE;
1966 }
1967 else
1968 {
1969 reader.js_used = 0;
1970 ch_log(channel, "still waiting on incomplete message");
1971 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001972 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001973 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001974
1975 if (status == FAIL)
1976 {
1977 ch_error(channel, "Decoding failed - discarding input");
1978 ret = FALSE;
1979 chanpart->ch_waiting = FALSE;
1980 }
1981 else if (reader.js_buf[reader.js_used] != NUL)
1982 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001983 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001984 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001985 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1986 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001987 ret = status == MAYBE ? FALSE: TRUE;
1988 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001989 else
1990 ret = FALSE;
1991
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001992 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001993 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001994}
1995
1996/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001997 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001998 */
1999 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002000remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002001{
Bram Moolenaar77073442016-02-13 23:23:53 +01002002 if (node->cq_prev == NULL)
2003 head->cq_next = node->cq_next;
2004 else
2005 node->cq_prev->cq_next = node->cq_next;
2006 if (node->cq_next == NULL)
2007 head->cq_prev = node->cq_prev;
2008 else
2009 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002010}
2011
2012/*
2013 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002014 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002015 */
2016 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002017remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002018{
Bram Moolenaar77073442016-02-13 23:23:53 +01002019 if (node->jq_prev == NULL)
2020 head->jq_next = node->jq_next;
2021 else
2022 node->jq_prev->jq_next = node->jq_next;
2023 if (node->jq_next == NULL)
2024 head->jq_prev = node->jq_prev;
2025 else
2026 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002027 vim_free(node);
2028}
2029
2030/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002031 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002032 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002033 * When "id" is zero or negative jut get the first message. But not the one
2034 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002035 * Return OK when found and return the value in "rettv".
2036 * Return FAIL otherwise.
2037 */
2038 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002039channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002040{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002041 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002042 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002043
Bram Moolenaar77073442016-02-13 23:23:53 +01002044 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002045 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002046 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002047 typval_T *tv = &l->lv_first->li_tv;
2048
2049 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002050 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002051 || tv->vval.v_number == 0
2052 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002053 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002054 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002055 if (tv->v_type == VAR_NUMBER)
2056 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002057 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002058 return OK;
2059 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002060 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002061 }
2062 return FAIL;
2063}
2064
Bram Moolenaarece61b02016-02-20 21:39:05 +01002065#define CH_JSON_MAX_ARGS 4
2066
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002067/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002068 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002069 * "argv[0]" is the command string.
2070 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002071 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002072 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002073channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002074{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002075 char_u *cmd = argv[0].vval.v_string;
2076 char_u *arg;
2077 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002078
Bram Moolenaarece61b02016-02-20 21:39:05 +01002079 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002080 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002081 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002082 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002083 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002084 return;
2085 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002086 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002087 if (arg == NULL)
2088 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002089
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002090 if (STRCMP(cmd, "ex") == 0)
2091 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002092 int save_called_emsg = called_emsg;
2093
2094 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002095 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002096 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002097 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002098 --emsg_silent;
2099 if (called_emsg)
2100 ch_logs(channel, "Ex command error: '%s'",
2101 (char *)get_vim_var_str(VV_ERRMSG));
2102 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002103 }
2104 else if (STRCMP(cmd, "normal") == 0)
2105 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002106 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002107
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002108 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002109 ea.arg = arg;
2110 ea.addr_count = 0;
2111 ea.forceit = TRUE; /* no mapping */
2112 ex_normal(&ea);
2113 }
2114 else if (STRCMP(cmd, "redraw") == 0)
2115 {
2116 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002117
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002118 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002119 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002120 ex_redraw(&ea);
2121 showruler(FALSE);
2122 setcursor();
2123 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002124#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002125 if (gui.in_use)
2126 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002127 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002128 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002129 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002130#endif
2131 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002132 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002133 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002134 int is_call = cmd[0] == 'c';
2135 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002136
Bram Moolenaarece61b02016-02-20 21:39:05 +01002137 if (argv[id_idx].v_type != VAR_UNKNOWN
2138 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002139 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002140 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002141 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002142 EMSG("E904: last argument for expr/call must be a number");
2143 }
2144 else if (is_call && argv[2].v_type != VAR_LIST)
2145 {
2146 ch_error(channel, "third argument for call must be a list");
2147 if (p_verbose > 2)
2148 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002149 }
2150 else
2151 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002152 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002153 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002154 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002155 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002156
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002157 /* Don't pollute the display with errors. */
2158 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002159 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002160 {
2161 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002162 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002163 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002164 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002165 {
2166 ch_logs(channel, "Calling '%s'", (char *)arg);
2167 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2168 tv = &res_tv;
2169 else
2170 tv = NULL;
2171 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002172
2173 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002174 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002175 int id = argv[id_idx].vval.v_number;
2176
Bram Moolenaar55fab432016-02-07 16:53:13 +01002177 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002178 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002179 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002180 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002181 /* If evaluation failed or the result can't be encoded
2182 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002183 vim_free(json);
2184 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002185 err_tv.v_type = VAR_STRING;
2186 err_tv.vval.v_string = (char_u *)"ERROR";
2187 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002188 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002189 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002190 if (json != NULL)
2191 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002192 channel_send(channel,
2193 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002194 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002195 vim_free(json);
2196 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002197 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002198 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002199 if (tv == &res_tv)
2200 clear_tv(tv);
2201 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002202 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002203 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002204 }
2205 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002206 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002207 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002208 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002209 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002210}
2211
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002212/*
2213 * Invoke the callback at "cbhead".
2214 * Does not redraw but sets channel_need_redraw.
2215 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002216 static void
2217invoke_one_time_callback(
2218 channel_T *channel,
2219 cbq_T *cbhead,
2220 cbq_T *item,
2221 typval_T *argv)
2222{
2223 ch_logs(channel, "Invoking one-time callback %s",
2224 (char *)item->cq_callback);
2225 /* Remove the item from the list first, if the callback
2226 * invokes ch_close() the list will be cleared. */
2227 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002228 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002229 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002230 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002231 vim_free(item);
2232}
2233
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002234 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002235append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002236{
2237 buf_T *save_curbuf = curbuf;
2238 linenr_T lnum = buffer->b_ml.ml_line_count;
2239 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002240 chanpart_T *ch_part = &channel->ch_part[part];
2241 int save_p_ma = buffer->b_p_ma;
2242
2243 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2244 {
2245 if (!ch_part->ch_nomod_error)
2246 {
2247 ch_error(channel, "Buffer is not modifiable, cannot append");
2248 ch_part->ch_nomod_error = TRUE;
2249 }
2250 return;
2251 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002252
2253 /* If the buffer is also used as input insert above the last
2254 * line. Don't write these lines. */
2255 if (save_write_to)
2256 {
2257 --lnum;
2258 buffer->b_write_to_channel = FALSE;
2259 }
2260
2261 /* Append to the buffer */
2262 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2263
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002264 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002265 curbuf = buffer;
2266 u_sync(TRUE);
2267 /* ignore undo failure, undo is not very useful here */
2268 ignored = u_save(lnum, lnum + 1);
2269
2270 ml_append(lnum, msg, 0, FALSE);
2271 appended_lines_mark(lnum, 1L);
2272 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002273 if (ch_part->ch_nomodifiable)
2274 buffer->b_p_ma = FALSE;
2275 else
2276 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002277
2278 if (buffer->b_nwindows > 0)
2279 {
2280 win_T *wp;
2281 win_T *save_curwin;
2282
2283 FOR_ALL_WINDOWS(wp)
2284 {
2285 if (wp->w_buffer == buffer
2286 && (save_write_to
2287 ? wp->w_cursor.lnum == lnum + 1
2288 : (wp->w_cursor.lnum == lnum
2289 && wp->w_cursor.col == 0)))
2290 {
2291 ++wp->w_cursor.lnum;
2292 save_curwin = curwin;
2293 curwin = wp;
2294 curbuf = curwin->w_buffer;
2295 scroll_cursor_bot(0, FALSE);
2296 curwin = save_curwin;
2297 curbuf = curwin->w_buffer;
2298 }
2299 }
2300 redraw_buf_later(buffer, VALID);
2301 channel_need_redraw = TRUE;
2302 }
2303
2304 if (save_write_to)
2305 {
2306 channel_T *ch;
2307
2308 /* Find channels reading from this buffer and adjust their
2309 * next-to-read line number. */
2310 buffer->b_write_to_channel = TRUE;
2311 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2312 {
2313 chanpart_T *in_part = &ch->ch_part[PART_IN];
2314
2315 if (in_part->ch_buffer == buffer)
2316 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2317 }
2318 }
2319}
2320
Bram Moolenaar437905c2016-04-26 19:01:05 +02002321 static void
2322drop_messages(channel_T *channel, int part)
2323{
2324 char_u *msg;
2325
2326 while ((msg = channel_get(channel, part)) != NULL)
2327 {
2328 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2329 vim_free(msg);
2330 }
2331}
2332
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002333/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002334 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002335 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002336 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002337 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002338 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002339may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002340{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002341 char_u *msg = NULL;
2342 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002343 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002344 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002345 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002346 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002347 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002348 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002349 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002350 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002351 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002352
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002353 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002354 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002355 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002356
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002357 /* Use a message-specific callback, part callback or channel callback */
2358 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2359 if (cbitem->cq_seq_nr == 0)
2360 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002361 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002362 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002363 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002364 partial = cbitem->cq_partial;
2365 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002366 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002367 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002368 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002369 partial = channel->ch_part[part].ch_partial;
2370 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002371 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002372 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002373 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002374 partial = channel->ch_partial;
2375 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002376
Bram Moolenaar187db502016-02-27 14:44:26 +01002377 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002378 if (buffer != NULL && !buf_valid(buffer))
2379 {
2380 /* buffer was wiped out */
2381 channel->ch_part[part].ch_buffer = NULL;
2382 buffer = NULL;
2383 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002384
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002385 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002386 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002387 listitem_T *item;
2388 int argc = 0;
2389
Bram Moolenaard7ece102016-02-02 23:23:02 +01002390 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002391 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002392 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002393 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002394 channel_parse_json(channel, part);
2395 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002396 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002397 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002398
Bram Moolenaarece61b02016-02-20 21:39:05 +01002399 for (item = listtv->vval.v_list->lv_first;
2400 item != NULL && argc < CH_JSON_MAX_ARGS;
2401 item = item->li_next)
2402 argv[argc++] = item->li_tv;
2403 while (argc < CH_JSON_MAX_ARGS)
2404 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002405
Bram Moolenaarece61b02016-02-20 21:39:05 +01002406 if (argv[0].v_type == VAR_STRING)
2407 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002408 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002409 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002410 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002411 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002412 }
2413
Bram Moolenaarece61b02016-02-20 21:39:05 +01002414 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002415 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002416 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002417 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002418 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002419 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002420 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002421 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002422 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002423 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002424 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002425 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002426 return FALSE;
2427 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002428 else
2429 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002430 /* If there is no callback or buffer drop the message. */
2431 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002432 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002433 /* If there is a close callback it may use ch_read() to get the
2434 * messages. */
2435 if (channel->ch_close_cb == NULL)
2436 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002437 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002438 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002439
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002440 if (ch_mode == MODE_NL)
2441 {
2442 char_u *nl;
2443 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002444 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002445
2446 /* See if we have a message ending in NL in the first buffer. If
2447 * not try to concatenate the first and the second buffer. */
2448 while (TRUE)
2449 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002450 node = channel_peek(channel, part);
2451 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002452 if (nl != NULL)
2453 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002454 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002455 return FALSE; /* incomplete message */
2456 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002457 buf = node->rq_buffer;
2458
2459 /* Convert NUL to NL, the internal representation. */
2460 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2461 if (*p == NUL)
2462 *p = NL;
2463
2464 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002465 {
2466 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002467 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002468 *nl = NUL;
2469 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002470 else
2471 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002472 /* Copy the message into allocated memory (excluding the NL)
2473 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002474 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002475 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002476 }
2477 }
2478 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002479 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002480 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002481 * get everything we have.
2482 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002483 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002484 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002485
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002486 if (msg == NULL)
2487 return FALSE; /* out of memory (and avoids Coverity warning) */
2488
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002489 argv[1].v_type = VAR_STRING;
2490 argv[1].vval.v_string = msg;
2491 }
2492
Bram Moolenaara07fec92016-02-05 21:04:08 +01002493 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002494 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002495 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002496
2497 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002498 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002499 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002500 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002501 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002502 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002503 break;
2504 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002505 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002506 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002507 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002508 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002509 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002510 if (buffer != NULL)
2511 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002512 if (msg == NULL)
2513 /* JSON or JS mode: re-encode the message. */
2514 msg = json_encode(listtv, ch_mode);
2515 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002516 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002517 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002518
Bram Moolenaar187db502016-02-27 14:44:26 +01002519 if (callback != NULL)
2520 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002521 if (cbitem != NULL)
2522 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2523 else
2524 {
2525 /* invoke the channel callback */
2526 ch_logs(channel, "Invoking channel callback %s",
2527 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002528 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002529 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002530 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002531 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002532 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002533 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002534
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002535 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002536 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002537 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002538
2539 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002540}
2541
2542/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002543 * Return TRUE when channel "channel" is open for writing to.
2544 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002545 */
2546 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002547channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002548{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002549 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002550 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002551}
2552
2553/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002554 * Return TRUE when channel "channel" is open for reading or writing.
2555 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002556 */
2557 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002558channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002559{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002560 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002561 || channel->CH_IN_FD != INVALID_FD
2562 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002563 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002564}
2565
2566/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002567 * Return TRUE if "channel" has JSON or other typeahead.
2568 */
2569 static int
2570channel_has_readahead(channel_T *channel, int part)
2571{
2572 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2573
2574 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2575 {
2576 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2577 jsonq_T *item = head->jq_next;
2578
2579 return item != NULL;
2580 }
2581 return channel_peek(channel, part) != NULL;
2582}
2583
2584/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002585 * Return a string indicating the status of the channel.
2586 */
2587 char *
2588channel_status(channel_T *channel)
2589{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002590 int part;
2591 int has_readahead = FALSE;
2592
Bram Moolenaar77073442016-02-13 23:23:53 +01002593 if (channel == NULL)
2594 return "fail";
2595 if (channel_is_open(channel))
2596 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002597 for (part = PART_SOCK; part <= PART_ERR; ++part)
2598 if (channel_has_readahead(channel, part))
2599 {
2600 has_readahead = TRUE;
2601 break;
2602 }
2603
2604 if (has_readahead)
2605 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002606 return "closed";
2607}
2608
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002609 static void
2610channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2611{
2612 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002613 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002614 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002615 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002616
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002617 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002618 STRCAT(namebuf, "_");
2619 tail = STRLEN(namebuf);
2620
2621 STRCPY(namebuf + tail, "status");
2622 dict_add_nr_str(dict, namebuf, 0,
2623 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2624
2625 STRCPY(namebuf + tail, "mode");
2626 switch (chanpart->ch_mode)
2627 {
2628 case MODE_NL: s = "NL"; break;
2629 case MODE_RAW: s = "RAW"; break;
2630 case MODE_JSON: s = "JSON"; break;
2631 case MODE_JS: s = "JS"; break;
2632 }
2633 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2634
2635 STRCPY(namebuf + tail, "io");
2636 if (part == PART_SOCK)
2637 s = "socket";
2638 else switch (chanpart->ch_io)
2639 {
2640 case JIO_NULL: s = "null"; break;
2641 case JIO_PIPE: s = "pipe"; break;
2642 case JIO_FILE: s = "file"; break;
2643 case JIO_BUFFER: s = "buffer"; break;
2644 case JIO_OUT: s = "out"; break;
2645 }
2646 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2647
2648 STRCPY(namebuf + tail, "timeout");
2649 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2650}
2651
2652 void
2653channel_info(channel_T *channel, dict_T *dict)
2654{
2655 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2656 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2657
2658 if (channel->ch_hostname != NULL)
2659 {
2660 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2661 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2662 channel_part_info(channel, dict, "sock", PART_SOCK);
2663 }
2664 else
2665 {
2666 channel_part_info(channel, dict, "out", PART_OUT);
2667 channel_part_info(channel, dict, "err", PART_ERR);
2668 channel_part_info(channel, dict, "in", PART_IN);
2669 }
2670}
2671
Bram Moolenaar77073442016-02-13 23:23:53 +01002672/*
2673 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002674 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002675 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002676 */
2677 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002678channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002679{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002680 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002681
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002682#ifdef FEAT_GUI
2683 channel_gui_unregister(channel);
2684#endif
2685
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002686 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002687 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002688 sock_close(channel->CH_SOCK_FD);
2689 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002690 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002691 may_close_part(&channel->CH_IN_FD);
2692 may_close_part(&channel->CH_OUT_FD);
2693 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002694
Bram Moolenaar8b374212016-02-24 20:43:06 +01002695 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002696 {
2697 typval_T argv[1];
2698 typval_T rettv;
2699 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002700 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002701
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002702 /* Invoke callbacks before the close callback, since it's weird to
2703 * first invoke the close callback. Increment the refcount to avoid
2704 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002705 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002706 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002707 for (part = PART_SOCK; part <= PART_ERR; ++part)
2708 while (may_invoke_callback(channel, part))
2709 ;
2710
2711 /* Invoke the close callback, if still set. */
2712 if (channel->ch_close_cb != NULL)
2713 {
2714 ch_logs(channel, "Invoking close callback %s",
2715 (char *)channel->ch_close_cb);
2716 argv[0].v_type = VAR_CHANNEL;
2717 argv[0].vval.v_channel = channel;
2718 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002719 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2720 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002721 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002722 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002723 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002724
2725 /* the callback is only called once */
2726 vim_free(channel->ch_close_cb);
2727 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002728 partial_unref(channel->ch_close_partial);
2729 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002730
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002731 --channel->ch_refcount;
2732
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002733 if (channel_need_redraw)
2734 {
2735 channel_need_redraw = FALSE;
2736 redraw_after_callback();
2737 }
2738
Bram Moolenaar437905c2016-04-26 19:01:05 +02002739 /* any remaining messages are useless now */
2740 for (part = PART_SOCK; part <= PART_ERR; ++part)
2741 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002742 }
2743
2744 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002745}
2746
Bram Moolenaard04a0202016-01-26 23:30:18 +01002747/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002748 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002749 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002750 static void
2751channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002752{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002753 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2754 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002755
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002756 while (channel_peek(channel, part) != NULL)
2757 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002758
2759 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002760 {
2761 cbq_T *node = cb_head->cq_next;
2762
2763 remove_cb_node(cb_head, node);
2764 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002765 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002766 vim_free(node);
2767 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002768
2769 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002770 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002771 free_tv(json_head->jq_next->jq_value);
2772 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002773 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002774
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002775 vim_free(channel->ch_part[part].ch_callback);
2776 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002777 partial_unref(channel->ch_part[part].ch_partial);
2778 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002779}
2780
2781/*
2782 * Clear all the read buffers on "channel".
2783 */
2784 void
2785channel_clear(channel_T *channel)
2786{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002787 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002788 vim_free(channel->ch_hostname);
2789 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002790 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002791 channel_clear_one(channel, PART_OUT);
2792 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002793 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002794 vim_free(channel->ch_callback);
2795 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002796 partial_unref(channel->ch_partial);
2797 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002798 vim_free(channel->ch_close_cb);
2799 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002800 partial_unref(channel->ch_close_partial);
2801 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002802}
2803
Bram Moolenaar77073442016-02-13 23:23:53 +01002804#if defined(EXITFREE) || defined(PROTO)
2805 void
2806channel_free_all(void)
2807{
2808 channel_T *channel;
2809
Bram Moolenaard6051b52016-02-28 15:49:03 +01002810 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002811 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2812 channel_clear(channel);
2813}
2814#endif
2815
2816
Bram Moolenaar715d2852016-04-30 17:06:31 +02002817/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002818#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002819
2820/* Buffer size for reading incoming messages. */
2821#define MAXMSGSIZE 4096
2822
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002823#if defined(HAVE_SELECT)
2824/*
2825 * Add write fds where we are waiting for writing to be possible.
2826 */
2827 static int
2828channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2829{
2830 int maxfd = maxfd_arg;
2831 channel_T *ch;
2832
2833 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2834 {
2835 chanpart_T *in_part = &ch->ch_part[PART_IN];
2836
2837 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2838 {
2839 FD_SET((int)in_part->ch_fd, wfds);
2840 if ((int)in_part->ch_fd >= maxfd)
2841 maxfd = (int)in_part->ch_fd + 1;
2842 }
2843 }
2844 return maxfd;
2845}
2846#else
2847/*
2848 * Add write fds where we are waiting for writing to be possible.
2849 */
2850 static int
2851channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2852{
2853 int nfd = nfd_in;
2854 channel_T *ch;
2855
2856 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2857 {
2858 chanpart_T *in_part = &ch->ch_part[PART_IN];
2859
2860 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2861 {
2862 in_part->ch_poll_idx = nfd;
2863 fds[nfd].fd = in_part->ch_fd;
2864 fds[nfd].events = POLLOUT;
2865 ++nfd;
2866 }
2867 else
2868 in_part->ch_poll_idx = -1;
2869 }
2870 return nfd;
2871}
2872#endif
2873
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002874typedef enum {
2875 CW_READY,
2876 CW_NOT_READY,
2877 CW_ERROR
2878} channel_wait_result;
2879
Bram Moolenaard04a0202016-01-26 23:30:18 +01002880/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002881 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002882 * Return CW_READY when there is something to read.
2883 * Return CW_NOT_READY when there is nothing to read.
2884 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002885 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002886 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002887channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002888{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002889 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002890 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002891
Bram Moolenaard8070362016-02-15 21:56:54 +01002892# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002893 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002894 {
2895 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002896 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002897 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002898 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002899
2900 /* reading from a pipe, not a socket */
2901 while (TRUE)
2902 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002903 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2904
2905 if (r && nread > 0)
2906 return CW_READY;
2907 if (r == 0)
2908 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002909
2910 /* perhaps write some buffer lines */
2911 channel_write_any_lines();
2912
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002913 sleep_time = deadline - GetTickCount();
2914 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002915 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002916 /* Wait for a little while. Very short at first, up to 10 msec
2917 * after looping a few times. */
2918 if (sleep_time > delay)
2919 sleep_time = delay;
2920 Sleep(sleep_time);
2921 delay = delay * 2;
2922 if (delay > 10)
2923 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002924 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002925 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002926 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002927#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002928 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002929#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002930 struct timeval tval;
2931 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002932 fd_set wfds;
2933 int ret;
2934 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002935
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002936 tval.tv_sec = timeout / 1000;
2937 tval.tv_usec = (timeout % 1000) * 1000;
2938 for (;;)
2939 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002940 FD_ZERO(&rfds);
2941 FD_SET((int)fd, &rfds);
2942
2943 /* Write lines to a pipe when a pipe can be written to. Need to
2944 * set this every time, some buffers may be done. */
2945 maxfd = (int)fd + 1;
2946 FD_ZERO(&wfds);
2947 maxfd = channel_fill_wfds(maxfd, &wfds);
2948
2949 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002950# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002951 SOCK_ERRNO;
2952 if (ret == -1 && errno == EINTR)
2953 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002954# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002955 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002956 {
2957 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002958 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002959 channel_write_any_lines();
2960 continue;
2961 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002962 break;
2963 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002964#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002965 for (;;)
2966 {
2967 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2968 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002969
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002970 fds[0].fd = fd;
2971 fds[0].events = POLLIN;
2972 nfd = channel_fill_poll_write(nfd, fds);
2973 if (poll(fds, nfd, timeout) > 0)
2974 {
2975 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002976 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002977 channel_write_any_lines();
2978 continue;
2979 }
2980 break;
2981 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002982#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002983 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002984 return CW_NOT_READY;
2985}
2986
2987 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002988channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002989{
2990 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002991 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2992 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002993
2994 /* Queue a "DETACH" netbeans message in the command queue in order to
2995 * terminate the netbeans session later. Do not end the session here
2996 * directly as we may be running in the context of a call to
2997 * netbeans_parse_messages():
2998 * netbeans_parse_messages
2999 * -> autocmd triggered while processing the netbeans cmd
3000 * -> ui_breakcheck
3001 * -> gui event loop or select loop
3002 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003003 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003004 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003005 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02003006 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003007 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3008
3009 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003010 * died. Don't close the channel right away, it may be the wrong moment
3011 * to invoke callbacks. */
3012 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003013
3014#ifdef FEAT_GUI
3015 /* Stop listening to GUI events right away. */
3016 channel_gui_unregister(channel);
3017#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003018}
3019
3020 static void
3021channel_close_now(channel_T *channel)
3022{
3023 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003024 channel_close(channel, TRUE);
3025 if (channel->ch_nb_close_cb != NULL)
3026 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003027}
3028
3029/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003030 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003031 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003032 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003033 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003034 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003035channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003036{
3037 static char_u *buf = NULL;
3038 int len = 0;
3039 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003040 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003041 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003042
Bram Moolenaar5850a762016-05-27 19:59:48 +02003043 /* If we detected a read error don't try reading again. */
3044 if (channel->ch_to_be_closed)
3045 return;
3046
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003047 fd = channel->ch_part[part].ch_fd;
3048 if (fd == INVALID_FD)
3049 {
3050 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003051 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003052 }
3053 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003054
3055 /* Allocate a buffer to read into. */
3056 if (buf == NULL)
3057 {
3058 buf = alloc(MAXMSGSIZE);
3059 if (buf == NULL)
3060 return; /* out of memory! */
3061 }
3062
3063 /* Keep on reading for as long as there is something to read.
3064 * Use select() or poll() to avoid blocking on a message that is exactly
3065 * MAXMSGSIZE long. */
3066 for (;;)
3067 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003068 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003069 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003070 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003071 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003072 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003073 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003074 if (len <= 0)
3075 break; /* error or nothing more to read */
3076
3077 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003078 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003079 readlen += len;
3080 if (len < MAXMSGSIZE)
3081 break; /* did read everything that's available */
3082 }
3083
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003084 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003085 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003086 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003087
3088#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003089 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003090 if (CH_HAS_GUI && gtk_main_level() > 0)
3091 gtk_main_quit();
3092#endif
3093}
3094
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003095/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003096 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003097 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003098 * Returns what was read in allocated memory.
3099 * Returns NULL in case of error or timeout.
3100 */
3101 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003102channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003103{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003104 char_u *buf;
3105 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003106 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003107 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003108 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003109 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003110
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003111 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003112 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003113
3114 while (TRUE)
3115 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003116 node = channel_peek(channel, part);
3117 if (node != NULL)
3118 {
3119 if (mode == MODE_RAW || (mode == MODE_NL
3120 && channel_first_nl(node) != NULL))
3121 /* got a complete message */
3122 break;
3123 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3124 continue;
3125 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003126
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003127 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003128 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003129 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003130 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003131 {
3132 ch_log(channel, "Timed out");
3133 return NULL;
3134 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003135 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003136 }
3137
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003138 if (mode == MODE_RAW)
3139 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003140 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003141 }
3142 else
3143 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003144 char_u *p;
3145
3146 buf = node->rq_buffer;
3147 nl = channel_first_nl(node);
3148
3149 /* Convert NUL to NL, the internal representation. */
3150 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3151 if (*p == NUL)
3152 *p = NL;
3153
3154 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003155 {
3156 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003157 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003158 *nl = NUL;
3159 }
3160 else
3161 {
3162 /* Copy the message into allocated memory and remove it from the
3163 * buffer. */
3164 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003165 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003166 }
3167 }
3168 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003169 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003170 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003171}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003172
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003173/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003174 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003175 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003176 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003177 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003178 */
3179 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003180channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003181 channel_T *channel,
3182 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003183 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003184 int id,
3185 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003186{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003187 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003188 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003189 int timeout;
3190 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003191
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003192 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003193 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003194 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003195 for (;;)
3196 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003197 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003198
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003199 /* search for message "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003200 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003201 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003202 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003203 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003204 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003205
Bram Moolenaard7ece102016-02-02 23:23:02 +01003206 if (!more)
3207 {
3208 /* Handle any other messages in the queue. If done some more
3209 * messages may have arrived. */
3210 if (channel_parse_messages())
3211 continue;
3212
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003213 /* Wait for up to the timeout. If there was an incomplete message
3214 * use the deadline for that. */
3215 timeout = timeout_arg;
3216 if (chanpart->ch_waiting)
3217 {
3218#ifdef WIN32
3219 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3220#else
3221 {
3222 struct timeval now_tv;
3223
3224 gettimeofday(&now_tv, NULL);
3225 timeout = (chanpart->ch_deadline.tv_sec
3226 - now_tv.tv_sec) * 1000
3227 + (chanpart->ch_deadline.tv_usec
3228 - now_tv.tv_usec) / 1000
3229 + 1;
3230 }
3231#endif
3232 if (timeout < 0)
3233 {
3234 /* Something went wrong, channel_parse_json() didn't
3235 * discard message. Cancel waiting. */
3236 chanpart->ch_waiting = FALSE;
3237 timeout = timeout_arg;
3238 }
3239 else if (timeout > timeout_arg)
3240 timeout = timeout_arg;
3241 }
3242 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003243 if (fd == INVALID_FD
3244 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003245 {
3246 if (timeout == timeout_arg)
3247 {
3248 if (fd != INVALID_FD)
3249 ch_log(channel, "Timed out");
3250 break;
3251 }
3252 }
3253 else
3254 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003255 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003256 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003257 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003258 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003259}
3260
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003261/*
3262 * Common for ch_read() and ch_readraw().
3263 */
3264 void
3265common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3266{
3267 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003268 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003269 jobopt_T opt;
3270 int mode;
3271 int timeout;
3272 int id = -1;
3273 typval_T *listtv = NULL;
3274
3275 /* return an empty string by default */
3276 rettv->v_type = VAR_STRING;
3277 rettv->vval.v_string = NULL;
3278
3279 clear_job_options(&opt);
3280 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3281 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003282 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003283
Bram Moolenaar437905c2016-04-26 19:01:05 +02003284 if (opt.jo_set & JO_PART)
3285 part = opt.jo_part;
3286 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003287 if (channel != NULL)
3288 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003289 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003290 part = channel_part_read(channel);
3291 mode = channel_get_mode(channel, part);
3292 timeout = channel_get_timeout(channel, part);
3293 if (opt.jo_set & JO_TIMEOUT)
3294 timeout = opt.jo_timeout;
3295
3296 if (raw || mode == MODE_RAW || mode == MODE_NL)
3297 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3298 else
3299 {
3300 if (opt.jo_set & JO_ID)
3301 id = opt.jo_id;
3302 channel_read_json_block(channel, part, timeout, id, &listtv);
3303 if (listtv != NULL)
3304 {
3305 *rettv = *listtv;
3306 vim_free(listtv);
3307 }
3308 else
3309 {
3310 rettv->v_type = VAR_SPECIAL;
3311 rettv->vval.v_number = VVAL_NONE;
3312 }
3313 }
3314 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003315
3316theend:
3317 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003318}
3319
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003320# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3321 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003322/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003323 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003324 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003325 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003326 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003327channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003328{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003329 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003330 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003331
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003332 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003333 for (channel = first_channel; channel != NULL;
3334 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003335 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003336 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003337 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003338 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003339 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003340 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003341 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003342 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003343 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003344}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003345# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003346
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003347# if defined(WIN32) || defined(PROTO)
3348/*
3349 * Check the channels for anything that is ready to be read.
3350 * The data is put in the read queue.
3351 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003352 void
3353channel_handle_events(void)
3354{
3355 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003356 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003357 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003358
3359 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3360 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003361 /* If we detected a read error don't try reading again. */
3362 if (channel->ch_to_be_closed)
3363 continue;
3364
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003365 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003366 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003367 {
3368 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003369 if (fd != INVALID_FD)
3370 {
3371 int r = channel_wait(channel, fd, 0);
3372
3373 if (r == CW_READY)
3374 channel_read(channel, part, "channel_handle_events");
3375 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003376 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003377 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003378 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003379 }
3380}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003381# endif
3382
Bram Moolenaard04a0202016-01-26 23:30:18 +01003383/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003384 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003385 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003386 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003387 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003388 int
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003389channel_send(channel_T *channel, int part, char_u *buf, int len, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003390{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003391 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003392 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003393
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003394 fd = channel->ch_part[part].ch_fd;
3395 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003396 {
3397 if (!channel->ch_error && fun != NULL)
3398 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003399 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003400 EMSG2("E630: %s(): write while not connected", fun);
3401 }
3402 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003403 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003404 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003405
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003406 if (log_fd != NULL)
3407 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003408 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003409 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003410 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003411 fprintf(log_fd, "'\n");
3412 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003413 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003414 }
3415
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003416 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003417 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003418 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003419 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003420 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003421 {
3422 if (!channel->ch_error && fun != NULL)
3423 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003424 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003425 EMSG2("E631: %s(): write failed", fun);
3426 }
3427 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003428 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003429 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003430
3431 channel->ch_error = FALSE;
3432 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003433}
3434
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003435/*
3436 * Common for "ch_sendexpr()" and "ch_sendraw()".
3437 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003438 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003439 * Otherwise returns NULL.
3440 */
3441 channel_T *
3442send_common(
3443 typval_T *argvars,
3444 char_u *text,
3445 int id,
3446 int eval,
3447 jobopt_T *opt,
3448 char *fun,
3449 int *part_read)
3450{
3451 channel_T *channel;
3452 int part_send;
3453
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003454 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003455 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003456 if (channel == NULL)
3457 return NULL;
3458 part_send = channel_part_send(channel);
3459 *part_read = channel_part_read(channel);
3460
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003461 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3462 return NULL;
3463
3464 /* Set the callback. An empty callback means no callback and not reading
3465 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3466 * allowed. */
3467 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3468 {
3469 if (eval)
3470 {
3471 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3472 return NULL;
3473 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003474 channel_set_req_callback(channel, part_send,
3475 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003476 }
3477
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003478 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003479 && opt->jo_callback == NULL)
3480 return channel;
3481 return NULL;
3482}
3483
3484/*
3485 * common for "ch_evalexpr()" and "ch_sendexpr()"
3486 */
3487 void
3488ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3489{
3490 char_u *text;
3491 typval_T *listtv;
3492 channel_T *channel;
3493 int id;
3494 ch_mode_T ch_mode;
3495 int part_send;
3496 int part_read;
3497 jobopt_T opt;
3498 int timeout;
3499
3500 /* return an empty string by default */
3501 rettv->v_type = VAR_STRING;
3502 rettv->vval.v_string = NULL;
3503
Bram Moolenaar437905c2016-04-26 19:01:05 +02003504 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003505 if (channel == NULL)
3506 return;
3507 part_send = channel_part_send(channel);
3508
3509 ch_mode = channel_get_mode(channel, part_send);
3510 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3511 {
3512 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3513 return;
3514 }
3515
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003516 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003517 text = json_encode_nr_expr(id, &argvars[1],
3518 ch_mode == MODE_JS ? JSON_JS : 0);
3519 if (text == NULL)
3520 return;
3521
3522 channel = send_common(argvars, text, id, eval, &opt,
3523 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3524 vim_free(text);
3525 if (channel != NULL && eval)
3526 {
3527 if (opt.jo_set & JO_TIMEOUT)
3528 timeout = opt.jo_timeout;
3529 else
3530 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003531 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3532 == OK)
3533 {
3534 list_T *list = listtv->vval.v_list;
3535
3536 /* Move the item from the list and then change the type to
3537 * avoid the value being freed. */
3538 *rettv = list->lv_last->li_tv;
3539 list->lv_last->li_tv.v_type = VAR_NUMBER;
3540 free_tv(listtv);
3541 }
3542 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003543 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003544}
3545
3546/*
3547 * common for "ch_evalraw()" and "ch_sendraw()"
3548 */
3549 void
3550ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3551{
3552 char_u buf[NUMBUFLEN];
3553 char_u *text;
3554 channel_T *channel;
3555 int part_read;
3556 jobopt_T opt;
3557 int timeout;
3558
3559 /* return an empty string by default */
3560 rettv->v_type = VAR_STRING;
3561 rettv->vval.v_string = NULL;
3562
3563 text = get_tv_string_buf(&argvars[1], buf);
3564 channel = send_common(argvars, text, 0, eval, &opt,
3565 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3566 if (channel != NULL && eval)
3567 {
3568 if (opt.jo_set & JO_TIMEOUT)
3569 timeout = opt.jo_timeout;
3570 else
3571 timeout = channel_get_timeout(channel, part_read);
3572 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3573 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003574 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003575}
3576
Bram Moolenaard04a0202016-01-26 23:30:18 +01003577# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003578/*
3579 * Add open channels to the poll struct.
3580 * Return the adjusted struct index.
3581 * The type of "fds" is hidden to avoid problems with the function proto.
3582 */
3583 int
3584channel_poll_setup(int nfd_in, void *fds_in)
3585{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003586 int nfd = nfd_in;
3587 channel_T *channel;
3588 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003589 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003590
Bram Moolenaar77073442016-02-13 23:23:53 +01003591 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003592 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003593 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003594 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003595 chanpart_T *ch_part = &channel->ch_part[part];
3596
3597 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003598 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003599 ch_part->ch_poll_idx = nfd;
3600 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003601 fds[nfd].events = POLLIN;
3602 nfd++;
3603 }
3604 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003605 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003606 }
3607 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003608
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003609 nfd = channel_fill_poll_write(nfd, fds);
3610
Bram Moolenaare0874f82016-01-24 20:36:41 +01003611 return nfd;
3612}
3613
3614/*
3615 * The type of "fds" is hidden to avoid problems with the function proto.
3616 */
3617 int
3618channel_poll_check(int ret_in, void *fds_in)
3619{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003620 int ret = ret_in;
3621 channel_T *channel;
3622 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003623 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003624 int idx;
3625 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003626
Bram Moolenaar77073442016-02-13 23:23:53 +01003627 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003628 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003629 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003630 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003631 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003632
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003633 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003634 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003635 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003636 --ret;
3637 }
3638 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003639
3640 in_part = &channel->ch_part[PART_IN];
3641 idx = in_part->ch_poll_idx;
3642 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3643 {
3644 if (in_part->ch_buf_append)
3645 {
3646 if (in_part->ch_buffer != NULL)
3647 channel_write_new_lines(in_part->ch_buffer);
3648 }
3649 else
3650 channel_write_in(channel);
3651 --ret;
3652 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003653 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003654
3655 return ret;
3656}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003657# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003658
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003659# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003660/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003661 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003662 */
3663 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003664channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003665{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003666 int maxfd = maxfd_in;
3667 channel_T *channel;
3668 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003669 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003670 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003671
Bram Moolenaar77073442016-02-13 23:23:53 +01003672 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003673 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003674 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003675 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003676 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003677
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003678 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003679 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003680 FD_SET((int)fd, rfds);
3681 if (maxfd < (int)fd)
3682 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003683 }
3684 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003685 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003686
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003687 maxfd = channel_fill_wfds(maxfd, wfds);
3688
Bram Moolenaare0874f82016-01-24 20:36:41 +01003689 return maxfd;
3690}
3691
3692/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003693 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003694 */
3695 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003696channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003697{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003698 int ret = ret_in;
3699 channel_T *channel;
3700 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003701 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003702 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003703 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003704
Bram Moolenaar77073442016-02-13 23:23:53 +01003705 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003706 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003707 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003708 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003709 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003710
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003711 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003712 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003713 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003714 --ret;
3715 }
3716 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003717
3718 in_part = &channel->ch_part[PART_IN];
3719 if (ret > 0 && in_part->ch_fd != INVALID_FD
3720 && FD_ISSET(in_part->ch_fd, wfds))
3721 {
3722 if (in_part->ch_buf_append)
3723 {
3724 if (in_part->ch_buffer != NULL)
3725 channel_write_new_lines(in_part->ch_buffer);
3726 }
3727 else
3728 channel_write_in(channel);
3729 --ret;
3730 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003731 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003732
3733 return ret;
3734}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003735# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003736
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003737/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003738 * Execute queued up commands.
3739 * Invoked from the main loop when it's safe to execute received commands.
3740 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003741 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003742 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003743channel_parse_messages(void)
3744{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003745 channel_T *channel = first_channel;
3746 int ret = FALSE;
3747 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003748 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003749
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003750 ++safe_to_invoke_callback;
3751
Bram Moolenaard0b65022016-03-06 21:50:33 +01003752 /* Only do this message when another message was given, otherwise we get
3753 * lots of them. */
3754 if (did_log_msg)
3755 {
3756 ch_log(NULL, "looking for messages on channels");
3757 did_log_msg = FALSE;
3758 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003759 while (channel != NULL)
3760 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003761 if (channel->ch_to_be_closed)
3762 {
3763 channel->ch_to_be_closed = FALSE;
3764 channel_close_now(channel);
3765 /* channel may have been freed, start over */
3766 channel = first_channel;
3767 continue;
3768 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003769 if (channel->ch_to_be_freed)
3770 {
3771 channel_free(channel);
3772 /* channel has been freed, start over */
3773 channel = first_channel;
3774 continue;
3775 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003776 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003777 {
3778 /* channel is no longer useful, free it */
3779 channel_free(channel);
3780 channel = first_channel;
3781 part = PART_SOCK;
3782 continue;
3783 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003784 if (channel->ch_part[part].ch_fd != INVALID_FD
3785 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003786 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003787 /* Increase the refcount, in case the handler causes the channel
3788 * to be unreferenced or closed. */
3789 ++channel->ch_refcount;
3790 r = may_invoke_callback(channel, part);
3791 if (r == OK)
3792 ret = TRUE;
3793 if (channel_unref(channel) || r == OK)
3794 {
3795 /* channel was freed or something was done, start over */
3796 channel = first_channel;
3797 part = PART_SOCK;
3798 continue;
3799 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003800 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003801 if (part < PART_ERR)
3802 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003803 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003804 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003805 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003806 part = PART_SOCK;
3807 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003808 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003809
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003810 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003811 {
3812 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003813 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003814 }
3815
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003816 --safe_to_invoke_callback;
3817
Bram Moolenaard7ece102016-02-02 23:23:02 +01003818 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003819}
3820
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003821/*
3822 * Mark references to lists used in channels.
3823 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003824 int
3825set_ref_in_channel(int copyID)
3826{
Bram Moolenaar77073442016-02-13 23:23:53 +01003827 int abort = FALSE;
3828 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003829 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003830
Bram Moolenaar77073442016-02-13 23:23:53 +01003831 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003832 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003833 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003834 tv.v_type = VAR_CHANNEL;
3835 tv.vval.v_channel = channel;
3836 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003837 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003838 return abort;
3839}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003840
3841/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003842 * Return the "part" to write to for "channel".
3843 */
3844 int
3845channel_part_send(channel_T *channel)
3846{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003847 if (channel->CH_SOCK_FD == INVALID_FD)
3848 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003849 return PART_SOCK;
3850}
3851
3852/*
3853 * Return the default "part" to read from for "channel".
3854 */
3855 int
3856channel_part_read(channel_T *channel)
3857{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003858 if (channel->CH_SOCK_FD == INVALID_FD)
3859 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003860 return PART_SOCK;
3861}
3862
3863/*
3864 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003865 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003866 */
3867 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003868channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003869{
Bram Moolenaar77073442016-02-13 23:23:53 +01003870 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003871 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003872 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003873}
3874
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003875/*
3876 * Return the timeout of "channel"/"part"
3877 */
3878 int
3879channel_get_timeout(channel_T *channel, int part)
3880{
3881 return channel->ch_part[part].ch_timeout;
3882}
3883
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003884 static int
3885handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3886{
3887 char_u *val = get_tv_string(item);
3888
3889 opt->jo_set |= jo;
3890 if (STRCMP(val, "nl") == 0)
3891 *modep = MODE_NL;
3892 else if (STRCMP(val, "raw") == 0)
3893 *modep = MODE_RAW;
3894 else if (STRCMP(val, "js") == 0)
3895 *modep = MODE_JS;
3896 else if (STRCMP(val, "json") == 0)
3897 *modep = MODE_JSON;
3898 else
3899 {
3900 EMSG2(_(e_invarg2), val);
3901 return FAIL;
3902 }
3903 return OK;
3904}
3905
3906 static int
3907handle_io(typval_T *item, int part, jobopt_T *opt)
3908{
3909 char_u *val = get_tv_string(item);
3910
3911 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3912 if (STRCMP(val, "null") == 0)
3913 opt->jo_io[part] = JIO_NULL;
3914 else if (STRCMP(val, "pipe") == 0)
3915 opt->jo_io[part] = JIO_PIPE;
3916 else if (STRCMP(val, "file") == 0)
3917 opt->jo_io[part] = JIO_FILE;
3918 else if (STRCMP(val, "buffer") == 0)
3919 opt->jo_io[part] = JIO_BUFFER;
3920 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3921 opt->jo_io[part] = JIO_OUT;
3922 else
3923 {
3924 EMSG2(_(e_invarg2), val);
3925 return FAIL;
3926 }
3927 return OK;
3928}
3929
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003930/*
3931 * Clear a jobopt_T before using it.
3932 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003933 void
3934clear_job_options(jobopt_T *opt)
3935{
3936 vim_memset(opt, 0, sizeof(jobopt_T));
3937}
3938
3939/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003940 * Free any members of a jobopt_T.
3941 */
3942 void
3943free_job_options(jobopt_T *opt)
3944{
3945 if (opt->jo_partial != NULL)
3946 partial_unref(opt->jo_partial);
3947 if (opt->jo_out_partial != NULL)
3948 partial_unref(opt->jo_out_partial);
3949 if (opt->jo_err_partial != NULL)
3950 partial_unref(opt->jo_err_partial);
3951 if (opt->jo_close_partial != NULL)
3952 partial_unref(opt->jo_close_partial);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003953 if (opt->jo_exit_partial != NULL)
3954 partial_unref(opt->jo_exit_partial);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003955}
3956
3957/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003958 * Get the PART_ number from the first character of an option name.
3959 */
3960 static int
3961part_from_char(int c)
3962{
3963 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3964}
3965
3966/*
3967 * Get the option entries from the dict in "tv", parse them and put the result
3968 * in "opt".
3969 * Only accept options in "supported".
3970 * If an option value is invalid return FAIL.
3971 */
3972 int
3973get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3974{
3975 typval_T *item;
3976 char_u *val;
3977 dict_T *dict;
3978 int todo;
3979 hashitem_T *hi;
3980 int part;
3981
3982 opt->jo_set = 0;
3983 if (tv->v_type == VAR_UNKNOWN)
3984 return OK;
3985 if (tv->v_type != VAR_DICT)
3986 {
3987 EMSG(_(e_invarg));
3988 return FAIL;
3989 }
3990 dict = tv->vval.v_dict;
3991 if (dict == NULL)
3992 return OK;
3993
3994 todo = (int)dict->dv_hashtab.ht_used;
3995 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3996 if (!HASHITEM_EMPTY(hi))
3997 {
3998 item = &dict_lookup(hi)->di_tv;
3999
4000 if (STRCMP(hi->hi_key, "mode") == 0)
4001 {
4002 if (!(supported & JO_MODE))
4003 break;
4004 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4005 return FAIL;
4006 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004007 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004008 {
4009 if (!(supported & JO_IN_MODE))
4010 break;
4011 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4012 == FAIL)
4013 return FAIL;
4014 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004015 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004016 {
4017 if (!(supported & JO_OUT_MODE))
4018 break;
4019 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4020 == FAIL)
4021 return FAIL;
4022 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004023 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004024 {
4025 if (!(supported & JO_ERR_MODE))
4026 break;
4027 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4028 == FAIL)
4029 return FAIL;
4030 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004031 else if (STRCMP(hi->hi_key, "in_io") == 0
4032 || STRCMP(hi->hi_key, "out_io") == 0
4033 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004034 {
4035 if (!(supported & JO_OUT_IO))
4036 break;
4037 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4038 return FAIL;
4039 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004040 else if (STRCMP(hi->hi_key, "in_name") == 0
4041 || STRCMP(hi->hi_key, "out_name") == 0
4042 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004043 {
4044 part = part_from_char(*hi->hi_key);
4045
4046 if (!(supported & JO_OUT_IO))
4047 break;
4048 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4049 opt->jo_io_name[part] =
4050 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4051 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004052 else if (STRCMP(hi->hi_key, "in_buf") == 0
4053 || STRCMP(hi->hi_key, "out_buf") == 0
4054 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004055 {
4056 part = part_from_char(*hi->hi_key);
4057
4058 if (!(supported & JO_OUT_IO))
4059 break;
4060 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4061 opt->jo_io_buf[part] = get_tv_number(item);
4062 if (opt->jo_io_buf[part] <= 0)
4063 {
4064 EMSG2(_(e_invarg2), get_tv_string(item));
4065 return FAIL;
4066 }
4067 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4068 {
4069 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4070 return FAIL;
4071 }
4072 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004073 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4074 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4075 {
4076 part = part_from_char(*hi->hi_key);
4077
4078 if (!(supported & JO_OUT_IO))
4079 break;
4080 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4081 opt->jo_modifiable[part] = get_tv_number(item);
4082 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004083 else if (STRCMP(hi->hi_key, "in_top") == 0
4084 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004085 {
4086 linenr_T *lp;
4087
4088 if (!(supported & JO_OUT_IO))
4089 break;
4090 if (hi->hi_key[3] == 't')
4091 {
4092 lp = &opt->jo_in_top;
4093 opt->jo_set |= JO_IN_TOP;
4094 }
4095 else
4096 {
4097 lp = &opt->jo_in_bot;
4098 opt->jo_set |= JO_IN_BOT;
4099 }
4100 *lp = get_tv_number(item);
4101 if (*lp < 0)
4102 {
4103 EMSG2(_(e_invarg2), get_tv_string(item));
4104 return FAIL;
4105 }
4106 }
4107 else if (STRCMP(hi->hi_key, "channel") == 0)
4108 {
4109 if (!(supported & JO_OUT_IO))
4110 break;
4111 opt->jo_set |= JO_CHANNEL;
4112 if (item->v_type != VAR_CHANNEL)
4113 {
4114 EMSG2(_(e_invarg2), "channel");
4115 return FAIL;
4116 }
4117 opt->jo_channel = item->vval.v_channel;
4118 }
4119 else if (STRCMP(hi->hi_key, "callback") == 0)
4120 {
4121 if (!(supported & JO_CALLBACK))
4122 break;
4123 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004124 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004125 if (opt->jo_callback == NULL)
4126 {
4127 EMSG2(_(e_invarg2), "callback");
4128 return FAIL;
4129 }
4130 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004131 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004132 {
4133 if (!(supported & JO_OUT_CALLBACK))
4134 break;
4135 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004136 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004137 if (opt->jo_out_cb == NULL)
4138 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004139 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004140 return FAIL;
4141 }
4142 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004143 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004144 {
4145 if (!(supported & JO_ERR_CALLBACK))
4146 break;
4147 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004148 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004149 if (opt->jo_err_cb == NULL)
4150 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004151 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004152 return FAIL;
4153 }
4154 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004155 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004156 {
4157 if (!(supported & JO_CLOSE_CALLBACK))
4158 break;
4159 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004160 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004161 if (opt->jo_close_cb == NULL)
4162 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004163 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004164 return FAIL;
4165 }
4166 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004167 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4168 {
4169 if (!(supported & JO_EXIT_CB))
4170 break;
4171 opt->jo_set |= JO_EXIT_CB;
4172 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4173 if (opt->jo_exit_cb == NULL)
4174 {
4175 EMSG2(_(e_invarg2), "exit_cb");
4176 return FAIL;
4177 }
4178 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004179 else if (STRCMP(hi->hi_key, "waittime") == 0)
4180 {
4181 if (!(supported & JO_WAITTIME))
4182 break;
4183 opt->jo_set |= JO_WAITTIME;
4184 opt->jo_waittime = get_tv_number(item);
4185 }
4186 else if (STRCMP(hi->hi_key, "timeout") == 0)
4187 {
4188 if (!(supported & JO_TIMEOUT))
4189 break;
4190 opt->jo_set |= JO_TIMEOUT;
4191 opt->jo_timeout = get_tv_number(item);
4192 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004193 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004194 {
4195 if (!(supported & JO_OUT_TIMEOUT))
4196 break;
4197 opt->jo_set |= JO_OUT_TIMEOUT;
4198 opt->jo_out_timeout = get_tv_number(item);
4199 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004200 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004201 {
4202 if (!(supported & JO_ERR_TIMEOUT))
4203 break;
4204 opt->jo_set |= JO_ERR_TIMEOUT;
4205 opt->jo_err_timeout = get_tv_number(item);
4206 }
4207 else if (STRCMP(hi->hi_key, "part") == 0)
4208 {
4209 if (!(supported & JO_PART))
4210 break;
4211 opt->jo_set |= JO_PART;
4212 val = get_tv_string(item);
4213 if (STRCMP(val, "err") == 0)
4214 opt->jo_part = PART_ERR;
4215 else
4216 {
4217 EMSG2(_(e_invarg2), val);
4218 return FAIL;
4219 }
4220 }
4221 else if (STRCMP(hi->hi_key, "id") == 0)
4222 {
4223 if (!(supported & JO_ID))
4224 break;
4225 opt->jo_set |= JO_ID;
4226 opt->jo_id = get_tv_number(item);
4227 }
4228 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4229 {
4230 if (!(supported & JO_STOPONEXIT))
4231 break;
4232 opt->jo_set |= JO_STOPONEXIT;
4233 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4234 opt->jo_soe_buf);
4235 if (opt->jo_stoponexit == NULL)
4236 {
4237 EMSG2(_(e_invarg2), "stoponexit");
4238 return FAIL;
4239 }
4240 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004241 else if (STRCMP(hi->hi_key, "block_write") == 0)
4242 {
4243 if (!(supported & JO_BLOCK_WRITE))
4244 break;
4245 opt->jo_set |= JO_BLOCK_WRITE;
4246 opt->jo_block_write = get_tv_number(item);
4247 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004248 else
4249 break;
4250 --todo;
4251 }
4252 if (todo > 0)
4253 {
4254 EMSG2(_(e_invarg2), hi->hi_key);
4255 return FAIL;
4256 }
4257
4258 return OK;
4259}
4260
4261/*
4262 * Get the channel from the argument.
4263 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004264 * When "check_open" is TRUE check that the channel can be used.
4265 * When "reading" is TRUE "check_open" considers typeahead useful.
4266 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004267 */
4268 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004269get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004270{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004271 channel_T *channel = NULL;
4272 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004273
4274 if (tv->v_type == VAR_JOB)
4275 {
4276 if (tv->vval.v_job != NULL)
4277 channel = tv->vval.v_job->jv_channel;
4278 }
4279 else if (tv->v_type == VAR_CHANNEL)
4280 {
4281 channel = tv->vval.v_channel;
4282 }
4283 else
4284 {
4285 EMSG2(_(e_invarg2), get_tv_string(tv));
4286 return NULL;
4287 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004288 if (channel != NULL && reading)
4289 has_readahead = channel_has_readahead(channel,
4290 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004291
Bram Moolenaar437905c2016-04-26 19:01:05 +02004292 if (check_open && (channel == NULL || (!channel_is_open(channel)
4293 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004294 {
4295 EMSG(_("E906: not an open channel"));
4296 return NULL;
4297 }
4298 return channel;
4299}
4300
4301static job_T *first_job = NULL;
4302
4303 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004304job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004305{
4306 ch_log(job->jv_channel, "Freeing job");
4307 if (job->jv_channel != NULL)
4308 {
4309 /* The link from the channel to the job doesn't count as a reference,
4310 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004311 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004312 * NULL the reference. We don't set ch_job_killed, unreferencing the
4313 * job doesn't mean it stops running. */
4314 job->jv_channel->ch_job = NULL;
4315 channel_unref(job->jv_channel);
4316 }
4317 mch_clear_job(job);
4318
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004319 vim_free(job->jv_stoponexit);
4320 vim_free(job->jv_exit_cb);
4321 partial_unref(job->jv_exit_partial);
4322}
4323
4324 static void
4325job_free_job(job_T *job)
4326{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004327 if (job->jv_next != NULL)
4328 job->jv_next->jv_prev = job->jv_prev;
4329 if (job->jv_prev == NULL)
4330 first_job = job->jv_next;
4331 else
4332 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004333 vim_free(job);
4334}
4335
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004336 static void
4337job_free(job_T *job)
4338{
4339 if (!in_free_unref_items)
4340 {
4341 job_free_contents(job);
4342 job_free_job(job);
4343 }
4344}
4345
Bram Moolenaar655da312016-05-28 22:22:34 +02004346#if defined(EXITFREE) || defined(PROTO)
4347 void
4348job_free_all(void)
4349{
4350 while (first_job != NULL)
4351 job_free(first_job);
4352}
4353#endif
4354
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004355/*
4356 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004357 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4358 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004359 */
4360 static int
4361job_still_useful(job_T *job)
4362{
4363 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004364 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4365 || (job->jv_channel != NULL
4366 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004367}
4368
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004369/*
4370 * Mark references in jobs that are still useful.
4371 */
4372 int
4373set_ref_in_job(int copyID)
4374{
4375 int abort = FALSE;
4376 job_T *job;
4377 typval_T tv;
4378
4379 for (job = first_job; job != NULL; job = job->jv_next)
4380 if (job_still_useful(job))
4381 {
4382 tv.v_type = VAR_JOB;
4383 tv.vval.v_job = job;
4384 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4385 }
4386 return abort;
4387}
4388
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004389 void
4390job_unref(job_T *job)
4391{
4392 if (job != NULL && --job->jv_refcount <= 0)
4393 {
4394 /* Do not free the job when it has not ended yet and there is a
4395 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004396 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004397 {
4398 job_free(job);
4399 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004400 else if (job->jv_channel != NULL
4401 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004402 {
4403 /* Do remove the link to the channel, otherwise it hangs
4404 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004405 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004406 job->jv_channel->ch_job = NULL;
4407 channel_unref(job->jv_channel);
4408 job->jv_channel = NULL;
4409 }
4410 }
4411}
4412
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004413 int
4414free_unused_jobs_contents(int copyID, int mask)
4415{
4416 int did_free = FALSE;
4417 job_T *job;
4418
4419 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004420 if ((job->jv_copyID & mask) != (copyID & mask)
4421 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004422 {
4423 /* Free the channel and ordinary items it contains, but don't
4424 * recurse into Lists, Dictionaries etc. */
4425 job_free_contents(job);
4426 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004427 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004428 return did_free;
4429}
4430
4431 void
4432free_unused_jobs(int copyID, int mask)
4433{
4434 job_T *job;
4435 job_T *job_next;
4436
4437 for (job = first_job; job != NULL; job = job_next)
4438 {
4439 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004440 if ((job->jv_copyID & mask) != (copyID & mask)
4441 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004442 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004443 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004444 job_free_job(job);
4445 }
4446 }
4447}
4448
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004449/*
4450 * Allocate a job. Sets the refcount to one and sets options default.
4451 */
4452 static job_T *
4453job_alloc(void)
4454{
4455 job_T *job;
4456
4457 job = (job_T *)alloc_clear(sizeof(job_T));
4458 if (job != NULL)
4459 {
4460 job->jv_refcount = 1;
4461 job->jv_stoponexit = vim_strsave((char_u *)"term");
4462
4463 if (first_job != NULL)
4464 {
4465 first_job->jv_prev = job;
4466 job->jv_next = first_job;
4467 }
4468 first_job = job;
4469 }
4470 return job;
4471}
4472
4473 void
4474job_set_options(job_T *job, jobopt_T *opt)
4475{
4476 if (opt->jo_set & JO_STOPONEXIT)
4477 {
4478 vim_free(job->jv_stoponexit);
4479 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4480 job->jv_stoponexit = NULL;
4481 else
4482 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4483 }
4484 if (opt->jo_set & JO_EXIT_CB)
4485 {
4486 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004487 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004488 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004489 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004490 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004491 job->jv_exit_partial = NULL;
4492 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004493 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004494 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004495 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004496 job->jv_exit_partial = opt->jo_exit_partial;
4497 if (job->jv_exit_partial != NULL)
4498 ++job->jv_exit_partial->pt_refcount;
4499 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004500 }
4501}
4502
4503/*
4504 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4505 */
4506 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004507job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004508{
4509 job_T *job;
4510
4511 for (job = first_job; job != NULL; job = job->jv_next)
4512 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4513 mch_stop_job(job, job->jv_stoponexit);
4514}
4515
4516/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004517 * Return TRUE when there is any job that might exit, which means
4518 * job_check_ended() should be called once in a while.
4519 */
4520 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004521has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004522{
4523 job_T *job;
4524
4525 for (job = first_job; job != NULL; job = job->jv_next)
4526 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4527 return TRUE;
4528 return FALSE;
4529}
4530
4531/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004532 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004533 */
4534 void
4535job_check_ended(void)
4536{
4537 static time_t last_check = 0;
4538 time_t now;
4539 job_T *job;
4540 job_T *next;
4541
4542 /* Only do this once in 10 seconds. */
4543 now = time(NULL);
4544 if (last_check + 10 < now)
4545 {
4546 last_check = now;
4547 for (job = first_job; job != NULL; job = next)
4548 {
4549 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004550 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004551 job_status(job); /* may free "job" */
4552 }
4553 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004554 if (channel_need_redraw)
4555 {
4556 channel_need_redraw = FALSE;
4557 redraw_after_callback();
4558 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004559}
4560
4561/*
4562 * "job_start()" function
4563 */
4564 job_T *
4565job_start(typval_T *argvars)
4566{
4567 job_T *job;
4568 char_u *cmd = NULL;
4569#if defined(UNIX)
4570# define USE_ARGV
4571 char **argv = NULL;
4572 int argc = 0;
4573#else
4574 garray_T ga;
4575#endif
4576 jobopt_T opt;
4577 int part;
4578
4579 job = job_alloc();
4580 if (job == NULL)
4581 return NULL;
4582
4583 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004584#ifndef USE_ARGV
4585 ga_init2(&ga, (int)sizeof(char*), 20);
4586#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004587
4588 /* Default mode is NL. */
4589 clear_job_options(&opt);
4590 opt.jo_mode = MODE_NL;
4591 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004592 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4593 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004594 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004595
4596 /* Check that when io is "file" that there is a file name. */
4597 for (part = PART_OUT; part <= PART_IN; ++part)
4598 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4599 && opt.jo_io[part] == JIO_FILE
4600 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4601 || *opt.jo_io_name[part] == NUL))
4602 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004603 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004604 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004605 }
4606
4607 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4608 {
4609 buf_T *buf = NULL;
4610
4611 /* check that we can find the buffer before starting the job */
4612 if (opt.jo_set & JO_IN_BUF)
4613 {
4614 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4615 if (buf == NULL)
4616 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4617 }
4618 else if (!(opt.jo_set & JO_IN_NAME))
4619 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004620 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004621 }
4622 else
4623 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4624 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004625 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004626 if (buf->b_ml.ml_mfp == NULL)
4627 {
4628 char_u numbuf[NUMBUFLEN];
4629 char_u *s;
4630
4631 if (opt.jo_set & JO_IN_BUF)
4632 {
4633 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4634 s = numbuf;
4635 }
4636 else
4637 s = opt.jo_io_name[PART_IN];
4638 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004639 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004640 }
4641 job->jv_in_buf = buf;
4642 }
4643
4644 job_set_options(job, &opt);
4645
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004646 if (argvars[0].v_type == VAR_STRING)
4647 {
4648 /* Command is a string. */
4649 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004650 if (cmd == NULL || *cmd == NUL)
4651 {
4652 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004653 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004654 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004655#ifdef USE_ARGV
4656 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004657 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004658 argv[argc] = NULL;
4659#endif
4660 }
4661 else if (argvars[0].v_type != VAR_LIST
4662 || argvars[0].vval.v_list == NULL
4663 || argvars[0].vval.v_list->lv_len < 1)
4664 {
4665 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004666 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004667 }
4668 else
4669 {
4670 list_T *l = argvars[0].vval.v_list;
4671 listitem_T *li;
4672 char_u *s;
4673
4674#ifdef USE_ARGV
4675 /* Pass argv[] to mch_call_shell(). */
4676 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4677 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004678 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004679#endif
4680 for (li = l->lv_first; li != NULL; li = li->li_next)
4681 {
4682 s = get_tv_string_chk(&li->li_tv);
4683 if (s == NULL)
4684 goto theend;
4685#ifdef USE_ARGV
4686 argv[argc++] = (char *)s;
4687#else
4688 /* Only escape when needed, double quotes are not always allowed. */
4689 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4690 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004691# ifdef WIN32
4692 int old_ssl = p_ssl;
4693
4694 /* This is using CreateProcess, not cmd.exe. Always use
4695 * double quote and backslashes. */
4696 p_ssl = 0;
4697# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004698 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004699# ifdef WIN32
4700 p_ssl = old_ssl;
4701# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004702 if (s == NULL)
4703 goto theend;
4704 ga_concat(&ga, s);
4705 vim_free(s);
4706 }
4707 else
4708 ga_concat(&ga, s);
4709 if (li->li_next != NULL)
4710 ga_append(&ga, ' ');
4711#endif
4712 }
4713#ifdef USE_ARGV
4714 argv[argc] = NULL;
4715#else
4716 cmd = ga.ga_data;
4717#endif
4718 }
4719
4720#ifdef USE_ARGV
4721 if (ch_log_active())
4722 {
4723 garray_T ga;
4724 int i;
4725
4726 ga_init2(&ga, (int)sizeof(char), 200);
4727 for (i = 0; i < argc; ++i)
4728 {
4729 if (i > 0)
4730 ga_concat(&ga, (char_u *)" ");
4731 ga_concat(&ga, (char_u *)argv[i]);
4732 }
4733 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4734 ga_clear(&ga);
4735 }
4736 mch_start_job(argv, job, &opt);
4737#else
4738 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4739 mch_start_job((char *)cmd, job, &opt);
4740#endif
4741
4742 /* If the channel is reading from a buffer, write lines now. */
4743 if (job->jv_channel != NULL)
4744 channel_write_in(job->jv_channel);
4745
4746theend:
4747#ifdef USE_ARGV
4748 vim_free(argv);
4749#else
4750 vim_free(ga.ga_data);
4751#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004752 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004753 return job;
4754}
4755
4756/*
4757 * Get the status of "job" and invoke the exit callback when needed.
4758 * The returned string is not allocated.
4759 */
4760 char *
4761job_status(job_T *job)
4762{
4763 char *result;
4764
4765 if (job->jv_status == JOB_ENDED)
4766 /* No need to check, dead is dead. */
4767 result = "dead";
4768 else if (job->jv_status == JOB_FAILED)
4769 result = "fail";
4770 else
4771 {
4772 result = mch_job_status(job);
4773 if (job->jv_status == JOB_ENDED)
4774 ch_log(job->jv_channel, "Job ended");
4775 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4776 {
4777 typval_T argv[3];
4778 typval_T rettv;
4779 int dummy;
4780
4781 /* invoke the exit callback; make sure the refcount is > 0 */
4782 ++job->jv_refcount;
4783 argv[0].v_type = VAR_JOB;
4784 argv[0].vval.v_job = job;
4785 argv[1].v_type = VAR_NUMBER;
4786 argv[1].vval.v_number = job->jv_exitval;
4787 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004788 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4789 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004790 clear_tv(&rettv);
4791 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004792 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004793 }
4794 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4795 {
4796 /* The job was already unreferenced, now that it ended it can be
4797 * freed. Careful: caller must not use "job" after this! */
4798 job_free(job);
4799 }
4800 }
4801 return result;
4802}
4803
Bram Moolenaar8950a562016-03-12 15:22:55 +01004804/*
4805 * Implementation of job_info().
4806 */
4807 void
4808job_info(job_T *job, dict_T *dict)
4809{
4810 dictitem_T *item;
4811 varnumber_T nr;
4812
4813 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4814
4815 item = dictitem_alloc((char_u *)"channel");
4816 if (item == NULL)
4817 return;
4818 item->di_tv.v_lock = 0;
4819 item->di_tv.v_type = VAR_CHANNEL;
4820 item->di_tv.vval.v_channel = job->jv_channel;
4821 if (job->jv_channel != NULL)
4822 ++job->jv_channel->ch_refcount;
4823 if (dict_add(dict, item) == FAIL)
4824 dictitem_free(item);
4825
4826#ifdef UNIX
4827 nr = job->jv_pid;
4828#else
4829 nr = job->jv_proc_info.dwProcessId;
4830#endif
4831 dict_add_nr_str(dict, "process", nr, NULL);
4832
4833 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004834 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004835 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4836}
4837
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004838 int
4839job_stop(job_T *job, typval_T *argvars)
4840{
4841 char_u *arg;
4842
4843 if (argvars[1].v_type == VAR_UNKNOWN)
4844 arg = (char_u *)"";
4845 else
4846 {
4847 arg = get_tv_string_chk(&argvars[1]);
4848 if (arg == NULL)
4849 {
4850 EMSG(_(e_invarg));
4851 return 0;
4852 }
4853 }
4854 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4855 if (mch_stop_job(job, arg) == FAIL)
4856 return 0;
4857
4858 /* Assume that "hup" does not kill the job. */
4859 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4860 job->jv_channel->ch_job_killed = TRUE;
4861
4862 /* We don't try freeing the job, obviously the caller still has a
4863 * reference to it. */
4864 return 1;
4865}
4866
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004867#endif /* FEAT_JOB_CHANNEL */