blob: f409e1d2093ad181590b359450c2ed0872388a6b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare0874f82016-01-24 20:36:41 +01002 *
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
Bram Moolenaar3f7d0902016-11-12 21:13:42 +010022/* Note: when making changes here also adjust configure.ac. */
Bram Moolenaard04a0202016-01-26 23:30:18 +010023#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 Moolenaardc0ccae2016-10-09 17:28:01 +020057static void channel_read(channel_T *channel, ch_part_T part, char *func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020058
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 Moolenaardc0ccae2016-10-09 17:28:01 +0200312 ch_part_T 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 Moolenaardc0ccae2016-10-09 17:28:01 +0200321 for (part = PART_SOCK; part < PART_COUNT; ++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 Moolenaarb9c31e72016-09-29 15:18:57 +0200344 int
345has_any_channel(void)
346{
347 return first_channel != NULL;
348}
349
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100350/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100351 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100352 * Return TRUE if "channel" has a callback and the associated job wasn't
353 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100354 */
355 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100356channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100357{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100359 int has_out_msg;
360 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100361
362 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100363 if (channel->ch_job_killed && channel->ch_job == NULL)
364 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100365
366 /* If there is a close callback it may still need to be invoked. */
367 if (channel->ch_close_cb != NULL)
368 return TRUE;
369
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200370 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200371 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200372 return TRUE;
373
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100374 /* If there is no callback then nobody can get readahead. If the fd is
375 * closed and there is no readahead then the callback won't be called. */
376 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
377 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
378 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100379 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
380 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
381 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
382 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
383 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
384 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100385 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100386 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200387 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200388 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
389 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200390 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200391 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
392 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100393}
394
395/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200396 * Close a channel and free all its resources.
397 */
398 static void
399channel_free_contents(channel_T *channel)
400{
401 channel_close(channel, TRUE);
402 channel_clear(channel);
403 ch_log(channel, "Freeing channel");
404}
405
406 static void
407channel_free_channel(channel_T *channel)
408{
409 if (channel->ch_next != NULL)
410 channel->ch_next->ch_prev = channel->ch_prev;
411 if (channel->ch_prev == NULL)
412 first_channel = channel->ch_next;
413 else
414 channel->ch_prev->ch_next = channel->ch_next;
415 vim_free(channel);
416}
417
418 static void
419channel_free(channel_T *channel)
420{
421 if (!in_free_unref_items)
422 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200423 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200424 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200425 else
426 {
427 channel_free_contents(channel);
428 channel_free_channel(channel);
429 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200430 }
431}
432
433/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100435 * possible, there is no callback to be invoked or the associated job was
436 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100437 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100438 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100439 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100440channel_may_free(channel_T *channel)
441{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100442 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100443 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100444 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100445 return TRUE;
446 }
447 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100448}
449
450/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100451 * Decrement the reference count on "channel" and maybe free it when it goes
452 * down to zero. Don't free it if there is a pending action.
453 * Returns TRUE when the channel is no longer referenced.
454 */
455 int
456channel_unref(channel_T *channel)
457{
458 if (channel != NULL && --channel->ch_refcount <= 0)
459 return channel_may_free(channel);
460 return FALSE;
461}
462
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200463 int
464free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100465{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 int did_free = FALSE;
467 channel_T *ch;
468
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200469 /* This is invoked from the garbage collector, which only runs at a safe
470 * point. */
471 ++safe_to_invoke_callback;
472
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200473 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200474 if (!channel_still_useful(ch)
475 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200476 {
477 /* Free the channel and ordinary items it contains, but don't
478 * recurse into Lists, Dictionaries etc. */
479 channel_free_contents(ch);
480 did_free = TRUE;
481 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200482
483 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200484 return did_free;
485}
486
487 void
488free_unused_channels(int copyID, int mask)
489{
490 channel_T *ch;
491 channel_T *ch_next;
492
493 for (ch = first_channel; ch != NULL; ch = ch_next)
494 {
495 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200496 if (!channel_still_useful(ch)
497 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200498 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200499 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200500 channel_free_channel(ch);
501 }
502 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100503}
504
Bram Moolenaard04a0202016-01-26 23:30:18 +0100505#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100506
507#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
508 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100509channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100510{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100511 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200512 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100513
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100514 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100516 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100517 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200518 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100519}
520#endif
521
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100523 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100524 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525#ifdef FEAT_GUI_X11
526 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200527messageFromServer(XtPointer clientData,
528 int *unused1 UNUSED,
529 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100530{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100531 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100532}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100533#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100534
Bram Moolenaard04a0202016-01-26 23:30:18 +0100535#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100536# if GTK_CHECK_VERSION(3,0,0)
537 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200538messageFromServer(GIOChannel *unused1 UNUSED,
539 GIOCondition unused2 UNUSED,
540 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100541{
542 channel_read_fd(GPOINTER_TO_INT(clientData));
543 return TRUE; /* Return FALSE instead in case the event source is to
544 * be removed after this function returns. */
545}
546# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100547 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200548messageFromServer(gpointer clientData,
549 gint unused1 UNUSED,
550 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100551{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100552 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100553}
Bram Moolenaar98921892016-02-23 17:14:37 +0100554# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100555#endif
556
557 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200558channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100559{
Bram Moolenaarde279892016-03-11 22:19:44 +0100560 if (!CH_HAS_GUI)
561 return;
562
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100563# ifdef FEAT_GUI_X11
564 /* Tell notifier we are interested in being called
565 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100566 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
567 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100568 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100569 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100570 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200571 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100572 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100573# else
574# ifdef FEAT_GUI_GTK
575 /* Tell gdk we are interested in being called when there
576 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100577 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100578# if GTK_CHECK_VERSION(3,0,0)
579 {
580 GIOChannel *chnnl = g_io_channel_unix_new(
581 (gint)channel->ch_part[part].ch_fd);
582
583 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
584 chnnl,
585 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200586 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100587 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
588
589 g_io_channel_unref(chnnl);
590 }
591# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100592 channel->ch_part[part].ch_inputHandler = gdk_input_add(
593 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100594 (GdkInputCondition)
595 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200596 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100597 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100598# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100599# endif
600# endif
601}
602
Bram Moolenaarde279892016-03-11 22:19:44 +0100603 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100604channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100605{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100606 if (channel->CH_SOCK_FD != INVALID_FD)
607 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100608 if (channel->CH_OUT_FD != INVALID_FD)
609 channel_gui_register_one(channel, PART_OUT);
610 if (channel->CH_ERR_FD != INVALID_FD)
611 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100612}
613
614/*
615 * Register any of our file descriptors with the GUI event handling system.
616 * Called when the GUI has started.
617 */
618 void
619channel_gui_register_all(void)
620{
Bram Moolenaar77073442016-02-13 23:23:53 +0100621 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100622
Bram Moolenaar77073442016-02-13 23:23:53 +0100623 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100624 channel_gui_register(channel);
625}
626
627 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200628channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100629{
630# ifdef FEAT_GUI_X11
631 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
632 {
633 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
634 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
635 }
636# else
637# ifdef FEAT_GUI_GTK
638 if (channel->ch_part[part].ch_inputHandler != 0)
639 {
640# if GTK_CHECK_VERSION(3,0,0)
641 g_source_remove(channel->ch_part[part].ch_inputHandler);
642# else
643 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
644# endif
645 channel->ch_part[part].ch_inputHandler = 0;
646 }
647# endif
648# endif
649}
650
651 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100652channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100653{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200654 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100655
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100656 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100657 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658}
659
660#endif
661
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100662static char *e_cannot_connect = N_("E902: Cannot connect to port");
663
Bram Moolenaard04a0202016-01-26 23:30:18 +0100664/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100665 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100666 * "waittime" is the time in msec to wait for the connection.
667 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100668 * Returns the channel for success.
669 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100670 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100671 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100672channel_open(
673 char *hostname,
674 int port_in,
675 int waittime,
676 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100677{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100678 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100681#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100682 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100683 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100684#else
685 int port = port_in;
686#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100687 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100688 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100689
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100690#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100691 channel_init_winsock();
692#endif
693
Bram Moolenaar77073442016-02-13 23:23:53 +0100694 channel = add_channel();
695 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100696 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100697 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100698 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100699 }
700
701 /* Get the server internet address and put into addr structure */
702 /* fill in the socket address structure and connect to server */
703 vim_memset((char *)&server, 0, sizeof(server));
704 server.sin_family = AF_INET;
705 server.sin_port = htons(port);
706 if ((host = gethostbyname(hostname)) == NULL)
707 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200709 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100710 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100711 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100712 }
713 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
714
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100715 /* On Mac and Solaris a zero timeout almost never works. At least wait
716 * one millisecond. Let's do it for all systems, because we don't know why
717 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100718 if (waittime == 0)
719 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720
721 /*
722 * For Unix we need to call connect() again after connect() failed.
723 * On Win32 one time is sufficient.
724 */
725 while (TRUE)
726 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100727 long elapsed_msec = 0;
728 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100729
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100730 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100731 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100732 sd = socket(AF_INET, SOCK_STREAM, 0);
733 if (sd == -1)
734 {
735 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200736 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100737 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100738 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100739 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100740
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100741 if (waittime >= 0)
742 {
743 /* Make connect() non-blocking. */
744 if (
745#ifdef _WIN32
746 ioctlsocket(sd, FIONBIO, &val) < 0
747#else
748 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
749#endif
750 )
751 {
752 SOCK_ERRNO;
753 ch_errorn(channel,
754 "channel_open: Connect failed with errno %d", errno);
755 sock_close(sd);
756 channel_free(channel);
757 return NULL;
758 }
759 }
760
761 /* Try connecting to the server. */
762 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
763 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
764
Bram Moolenaar045a2842016-03-08 22:33:07 +0100765 if (ret == 0)
766 /* The connection could be established. */
767 break;
768
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100769 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100770 if (waittime < 0 || (errno != EWOULDBLOCK
771 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100772#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100773 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100774#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100775 ))
776 {
777 ch_errorn(channel,
778 "channel_open: Connect failed with errno %d", errno);
779 PERROR(_(e_cannot_connect));
780 sock_close(sd);
781 channel_free(channel);
782 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100783 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100784
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100785 /* Limit the waittime to 50 msec. If it doesn't work within this
786 * time we close the socket and try creating it again. */
787 waitnow = waittime > 50 ? 50 : waittime;
788
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100790 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100791#ifndef WIN32
792 if (errno != ECONNREFUSED)
793#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100794 {
795 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100796 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100797 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100798#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100799 int so_error = 0;
800 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100801 struct timeval start_tv;
802 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100803#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100804 FD_ZERO(&rfds);
805 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100806 FD_ZERO(&wfds);
807 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100808
Bram Moolenaar562ca712016-03-09 21:50:05 +0100809 tv.tv_sec = waitnow / 1000;
810 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100811#ifndef WIN32
812 gettimeofday(&start_tv, NULL);
813#endif
814 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100815 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100816 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100817
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100818 if (ret < 0)
819 {
820 SOCK_ERRNO;
821 ch_errorn(channel,
822 "channel_open: Connect failed with errno %d", errno);
823 PERROR(_(e_cannot_connect));
824 sock_close(sd);
825 channel_free(channel);
826 return NULL;
827 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100828
Bram Moolenaare081e212016-02-28 22:33:46 +0100829#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100830 /* On Win32: select() is expected to work and wait for up to
831 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100832 if (FD_ISSET(sd, &wfds))
833 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100834 elapsed_msec = waitnow;
835 if (waittime > 1 && elapsed_msec < waittime)
836 {
837 waittime -= elapsed_msec;
838 continue;
839 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100840#else
841 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100842 * After putting the socket in non-blocking mode, connect() will
843 * return EINPROGRESS, select() will not wait (as if writing is
844 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100845 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100846 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100847 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100848 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100849 {
850 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100851 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100852 if (ret < 0 || (so_error != 0
853 && so_error != EWOULDBLOCK
854 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100855# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100856 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100857# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100858 ))
859 {
860 ch_errorn(channel,
861 "channel_open: Connect failed with errno %d",
862 so_error);
863 PERROR(_(e_cannot_connect));
864 sock_close(sd);
865 channel_free(channel);
866 return NULL;
867 }
868 }
869
Bram Moolenaar045a2842016-03-08 22:33:07 +0100870 if (FD_ISSET(sd, &wfds) && so_error == 0)
871 /* Did not detect an error, connection is established. */
872 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100873
Bram Moolenaar045a2842016-03-08 22:33:07 +0100874 gettimeofday(&end_tv, NULL);
875 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
876 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100877#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100878 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100879
880#ifndef WIN32
881 if (waittime > 1 && elapsed_msec < waittime)
882 {
883 /* The port isn't ready but we also didn't get an error.
884 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100885 * yet. Select() may return early, wait until the remaining
886 * "waitnow" and try again. */
887 waitnow -= elapsed_msec;
888 waittime -= elapsed_msec;
889 if (waitnow > 0)
890 {
891 mch_delay((long)waitnow, TRUE);
892 ui_breakcheck();
893 waittime -= waitnow;
894 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100895 if (!got_int)
896 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100897 if (waittime <= 0)
898 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100899 waittime = 1;
900 continue;
901 }
902 /* we were interrupted, behave as if timed out */
903 }
904#endif
905
906 /* We timed out. */
907 ch_error(channel, "Connection timed out");
908 sock_close(sd);
909 channel_free(channel);
910 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100911 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100912
Bram Moolenaar045a2842016-03-08 22:33:07 +0100913 ch_log(channel, "Connection made");
914
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100915 if (waittime >= 0)
916 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100917#ifdef _WIN32
918 val = 0;
919 ioctlsocket(sd, FIONBIO, &val);
920#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100921 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100922#endif
923 }
924
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100925 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100926 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100927 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
928 channel->ch_port = port_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200929 channel->ch_to_be_closed |= (1 << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100930
931#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100932 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100933#endif
934
Bram Moolenaar77073442016-02-13 23:23:53 +0100935 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100936}
937
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100938/*
939 * Implements ch_open().
940 */
941 channel_T *
942channel_open_func(typval_T *argvars)
943{
944 char_u *address;
945 char_u *p;
946 char *rest;
947 int port;
948 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200949 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100950
951 address = get_tv_string(&argvars[0]);
952 if (argvars[1].v_type != VAR_UNKNOWN
953 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
954 {
955 EMSG(_(e_invarg));
956 return NULL;
957 }
958
959 /* parse address */
960 p = vim_strchr(address, ':');
961 if (p == NULL)
962 {
963 EMSG2(_(e_invarg2), address);
964 return NULL;
965 }
966 *p++ = NUL;
967 port = strtol((char *)p, &rest, 10);
968 if (*address == NUL || port <= 0 || *rest != NUL)
969 {
970 p[-1] = ':';
971 EMSG2(_(e_invarg2), address);
972 return NULL;
973 }
974
975 /* parse options */
976 clear_job_options(&opt);
977 opt.jo_mode = MODE_JSON;
978 opt.jo_timeout = 2000;
979 if (get_job_options(&argvars[1], &opt,
980 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200981 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100982 if (opt.jo_timeout < 0)
983 {
984 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200985 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100986 }
987
988 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
989 if (channel != NULL)
990 {
991 opt.jo_set = JO_ALL;
992 channel_set_options(channel, &opt);
993 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200994theend:
995 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100996 return channel;
997}
998
Bram Moolenaarde279892016-03-11 22:19:44 +0100999 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001000ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001001{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001002 sock_T *fd = &channel->ch_part[part].ch_fd;
1003
Bram Moolenaarde279892016-03-11 22:19:44 +01001004 if (*fd != INVALID_FD)
1005 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001006 if (part == PART_SOCK)
1007 sock_close(*fd);
1008 else
1009 fd_close(*fd);
Bram Moolenaarde279892016-03-11 22:19:44 +01001010 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001011
1012 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001013 }
1014}
1015
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001016 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001017channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001018{
Bram Moolenaarde279892016-03-11 22:19:44 +01001019 if (in != INVALID_FD)
1020 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001021 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001022 channel->CH_IN_FD = in;
1023 }
1024 if (out != INVALID_FD)
1025 {
1026# if defined(FEAT_GUI)
1027 channel_gui_unregister_one(channel, PART_OUT);
1028# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001029 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001030 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001031 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001032# if defined(FEAT_GUI)
1033 channel_gui_register_one(channel, PART_OUT);
1034# endif
1035 }
1036 if (err != INVALID_FD)
1037 {
1038# if defined(FEAT_GUI)
1039 channel_gui_unregister_one(channel, PART_ERR);
1040# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001041 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001042 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001043 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001044# if defined(FEAT_GUI)
1045 channel_gui_register_one(channel, PART_ERR);
1046# endif
1047 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001048}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001049
Bram Moolenaard6051b52016-02-28 15:49:03 +01001050/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001051 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001052 * This does not keep a refcount, when the job is freed ch_job is cleared.
1053 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001054 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001055channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001056{
Bram Moolenaar77073442016-02-13 23:23:53 +01001057 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001058
1059 channel_set_options(channel, options);
1060
1061 if (job->jv_in_buf != NULL)
1062 {
1063 chanpart_T *in_part = &channel->ch_part[PART_IN];
1064
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001065 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001066 ch_logs(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001067 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001068 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001069 {
1070 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1071 {
1072 /* Special mode: send last-but-one line when appending a line
1073 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001074 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001075 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001076 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001077 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001078 }
1079 else
1080 in_part->ch_buf_top = options->jo_in_top;
1081 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001082 else
1083 in_part->ch_buf_top = 1;
1084 if (options->jo_set & JO_IN_BOT)
1085 in_part->ch_buf_bot = options->jo_in_bot;
1086 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001087 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001088 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001089}
1090
1091/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001092 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001093 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001094 */
1095 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001096find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001097{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001098 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001099 buf_T *save_curbuf = curbuf;
1100
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001101 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001102 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001103 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001104 if (buf == NULL)
1105 buf = buflist_findname_exp(name);
1106 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001107 if (buf == NULL)
1108 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001109 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001110 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001111 if (buf == NULL)
1112 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001113 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001114 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001115#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001116 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1117 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001118#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001119 if (curbuf->b_ml.ml_mfp == NULL)
1120 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001121 if (msg)
1122 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001123 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001124 changed_bytes(1, 0);
1125 curbuf = save_curbuf;
1126 }
1127
1128 return buf;
1129}
1130
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001131 static void
1132set_callback(
1133 char_u **cbp,
1134 partial_T **pp,
1135 char_u *callback,
1136 partial_T *partial)
1137{
1138 free_callback(*cbp, *pp);
1139 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001140 {
1141 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001142 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001143 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001144 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001145 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001146 func_ref(*cbp);
1147 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001148 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001149 else
1150 *cbp = NULL;
1151 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001152 if (partial != NULL)
1153 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001154}
1155
Bram Moolenaar187db502016-02-27 14:44:26 +01001156/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001157 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001158 */
1159 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001160channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001161{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001162 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001163
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001164 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001165 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001166 channel->ch_part[part].ch_mode = opt->jo_mode;
1167 if (opt->jo_set & JO_IN_MODE)
1168 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1169 if (opt->jo_set & JO_OUT_MODE)
1170 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1171 if (opt->jo_set & JO_ERR_MODE)
1172 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001173
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001174 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001175 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001176 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1177 if (opt->jo_set & JO_OUT_TIMEOUT)
1178 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1179 if (opt->jo_set & JO_ERR_TIMEOUT)
1180 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001181 if (opt->jo_set & JO_BLOCK_WRITE)
1182 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001183
1184 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001185 set_callback(&channel->ch_callback, &channel->ch_partial,
1186 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001187 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001188 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1189 &channel->ch_part[PART_OUT].ch_partial,
1190 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001191 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001192 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1193 &channel->ch_part[PART_ERR].ch_partial,
1194 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001195 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001196 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1197 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001198 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001199
1200 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1201 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001202 buf_T *buf;
1203
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001204 /* writing output to a buffer. Default mode is NL. */
1205 if (!(opt->jo_set & JO_OUT_MODE))
1206 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001207 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001208 {
1209 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1210 if (buf == NULL)
1211 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1212 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001213 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001214 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001215 int msg = TRUE;
1216
1217 if (opt->jo_set2 & JO2_OUT_MSG)
1218 msg = opt->jo_message[PART_OUT];
1219 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001220 }
1221 if (buf != NULL)
1222 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001223 if (opt->jo_set & JO_OUT_MODIFIABLE)
1224 channel->ch_part[PART_OUT].ch_nomodifiable =
1225 !opt->jo_modifiable[PART_OUT];
1226
1227 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1228 {
1229 EMSG(_(e_modifiable));
1230 }
1231 else
1232 {
1233 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001234 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001235 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001236 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001237 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001238 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001239
1240 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1241 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1242 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1243 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001244 buf_T *buf;
1245
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001246 /* writing err to a buffer. Default mode is NL. */
1247 if (!(opt->jo_set & JO_ERR_MODE))
1248 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1249 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001250 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001251 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001252 {
1253 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1254 if (buf == NULL)
1255 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1256 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001257 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001258 {
1259 int msg = TRUE;
1260
1261 if (opt->jo_set2 & JO2_ERR_MSG)
1262 msg = opt->jo_message[PART_ERR];
1263 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1264 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001265 if (buf != NULL)
1266 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001267 if (opt->jo_set & JO_ERR_MODIFIABLE)
1268 channel->ch_part[PART_ERR].ch_nomodifiable =
1269 !opt->jo_modifiable[PART_ERR];
1270 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1271 {
1272 EMSG(_(e_modifiable));
1273 }
1274 else
1275 {
1276 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001277 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001278 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001279 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001280 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001281 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001282
1283 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1284 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1285 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001286}
1287
1288/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001289 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001290 */
1291 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001292channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001293 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001294 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001295 char_u *callback,
1296 partial_T *partial,
1297 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001298{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001299 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001300 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1301
1302 if (item != NULL)
1303 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001304 item->cq_partial = partial;
1305 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001306 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001307 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001308 item->cq_callback = callback;
1309 }
1310 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001311 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001312 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001313 func_ref(item->cq_callback);
1314 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001315 item->cq_seq_nr = id;
1316 item->cq_prev = head->cq_prev;
1317 head->cq_prev = item;
1318 item->cq_next = NULL;
1319 if (item->cq_prev == NULL)
1320 head->cq_next = item;
1321 else
1322 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001323 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001324}
1325
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001326 static void
1327write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1328{
1329 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001330 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001331 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001332 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001333
Bram Moolenaar655da312016-05-28 22:22:34 +02001334 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001335 if ((p = alloc(len + 2)) == NULL)
1336 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001337 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001338
1339 for (i = 0; i < len; ++i)
1340 if (p[i] == NL)
1341 p[i] = NUL;
1342
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001343 p[len] = NL;
1344 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001345 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001346 vim_free(p);
1347}
1348
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001349/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001350 * Return TRUE if "channel" can be written to.
1351 * Returns FALSE if the input is closed or the write would block.
1352 */
1353 static int
1354can_write_buf_line(channel_T *channel)
1355{
1356 chanpart_T *in_part = &channel->ch_part[PART_IN];
1357
1358 if (in_part->ch_fd == INVALID_FD)
1359 return FALSE; /* pipe was closed */
1360
1361 /* for testing: block every other attempt to write */
1362 if (in_part->ch_block_write == 1)
1363 in_part->ch_block_write = -1;
1364 else if (in_part->ch_block_write == -1)
1365 in_part->ch_block_write = 1;
1366
1367 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1368#ifndef WIN32
1369 {
1370# if defined(HAVE_SELECT)
1371 struct timeval tval;
1372 fd_set wfds;
1373 int ret;
1374
1375 FD_ZERO(&wfds);
1376 FD_SET((int)in_part->ch_fd, &wfds);
1377 tval.tv_sec = 0;
1378 tval.tv_usec = 0;
1379 for (;;)
1380 {
1381 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1382# ifdef EINTR
1383 SOCK_ERRNO;
1384 if (ret == -1 && errno == EINTR)
1385 continue;
1386# endif
1387 if (ret <= 0 || in_part->ch_block_write == 1)
1388 {
1389 if (ret > 0)
1390 ch_log(channel, "FAKED Input not ready for writing");
1391 else
1392 ch_log(channel, "Input not ready for writing");
1393 return FALSE;
1394 }
1395 break;
1396 }
1397# else
1398 struct pollfd fds;
1399
1400 fds.fd = in_part->ch_fd;
1401 fds.events = POLLOUT;
1402 if (poll(&fds, 1, 0) <= 0)
1403 {
1404 ch_log(channel, "Input not ready for writing");
1405 return FALSE;
1406 }
1407 if (in_part->ch_block_write == 1)
1408 {
1409 ch_log(channel, "FAKED Input not ready for writing");
1410 return FALSE;
1411 }
1412# endif
1413 }
1414#endif
1415 return TRUE;
1416}
1417
1418/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001419 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001420 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001421 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001422channel_write_in(channel_T *channel)
1423{
1424 chanpart_T *in_part = &channel->ch_part[PART_IN];
1425 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001426 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001427 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001428
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001429 if (buf == NULL || in_part->ch_buf_append)
1430 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001431 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001432 {
1433 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001434 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001435 return;
1436 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001437
1438 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1439 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1440 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001441 if (!can_write_buf_line(channel))
1442 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001443 write_buf_line(buf, lnum, channel);
1444 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001445 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001446
1447 if (written == 1)
1448 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1449 else if (written > 1)
1450 ch_logn(channel, "written %d lines to channel", written);
1451
Bram Moolenaar014069a2016-03-03 22:51:40 +01001452 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001453 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001454 {
1455 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001456 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001457 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001458
1459 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001460 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001461 }
1462 else
1463 ch_logn(channel, "Still %d more lines to write",
1464 buf->b_ml.ml_line_count - lnum + 1);
1465}
1466
1467/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001468 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001469 */
1470 void
1471channel_buffer_free(buf_T *buf)
1472{
1473 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001474 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001475
1476 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001477 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001478 {
1479 chanpart_T *ch_part = &channel->ch_part[part];
1480
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001481 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001482 {
1483 ch_logs(channel, "%s buffer has been wiped out",
1484 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001485 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001486 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001487 }
1488}
1489
1490/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001491 * Write any lines waiting to be written to a channel.
1492 */
1493 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001494channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001495{
1496 channel_T *channel;
1497
1498 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1499 {
1500 chanpart_T *in_part = &channel->ch_part[PART_IN];
1501
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001502 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001503 {
1504 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001505 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001506 else
1507 channel_write_in(channel);
1508 }
1509 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001510}
1511
1512/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001513 * Write appended lines above the last one in "buf" to the channel.
1514 */
1515 void
1516channel_write_new_lines(buf_T *buf)
1517{
1518 channel_T *channel;
1519 int found_one = FALSE;
1520
1521 /* There could be more than one channel for the buffer, loop over all of
1522 * them. */
1523 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1524 {
1525 chanpart_T *in_part = &channel->ch_part[PART_IN];
1526 linenr_T lnum;
1527 int written = 0;
1528
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001529 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001530 {
1531 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001532 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001533 found_one = TRUE;
1534 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1535 ++lnum)
1536 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001537 if (!can_write_buf_line(channel))
1538 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001539 write_buf_line(buf, lnum, channel);
1540 ++written;
1541 }
1542
1543 if (written == 1)
1544 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1545 else if (written > 1)
1546 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001547 if (lnum < buf->b_ml.ml_line_count)
1548 ch_logn(channel, "Still %d more lines to write",
1549 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001550
1551 in_part->ch_buf_bot = lnum;
1552 }
1553 }
1554 if (!found_one)
1555 buf->b_write_to_channel = FALSE;
1556}
1557
1558/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001559 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001560 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001561 */
1562 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001563invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1564 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001565{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001566 typval_T rettv;
1567 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001568
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001569 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001570 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001571
Bram Moolenaar77073442016-02-13 23:23:53 +01001572 argv[0].v_type = VAR_CHANNEL;
1573 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001574
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001575 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1576 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001577 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001578 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001579}
1580
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001581/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001582 * Return the first node from "channel"/"part" without removing it.
1583 * Returns NULL if there is nothing.
1584 */
1585 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001586channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001587{
1588 readq_T *head = &channel->ch_part[part].ch_head;
1589
1590 return head->rq_next;
1591}
1592
1593/*
1594 * Return a pointer to the first NL in "node".
1595 * Skips over NUL characters.
1596 * Returns NULL if there is no NL.
1597 */
1598 char_u *
1599channel_first_nl(readq_T *node)
1600{
1601 char_u *buffer = node->rq_buffer;
1602 long_u i;
1603
1604 for (i = 0; i < node->rq_buflen; ++i)
1605 if (buffer[i] == NL)
1606 return buffer + i;
1607 return NULL;
1608}
1609
1610/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001612 * The caller must free it.
1613 * Returns NULL if there is nothing.
1614 */
1615 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001616channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001617{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001618 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001619 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001620 char_u *p;
1621
Bram Moolenaar77073442016-02-13 23:23:53 +01001622 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001623 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001624 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001625 p = node->rq_buffer;
1626 head->rq_next = node->rq_next;
1627 if (node->rq_next == NULL)
1628 head->rq_prev = NULL;
1629 else
1630 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001631 vim_free(node);
1632 return p;
1633}
1634
1635/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001636 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001637 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001638 */
1639 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001640channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001641{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001642 readq_T *head = &channel->ch_part[part].ch_head;
1643 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001644 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001645 char_u *res;
1646 char_u *p;
1647
1648 /* If there is only one buffer just get that one. */
1649 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1650 return channel_get(channel, part);
1651
1652 /* Concatenate everything into one buffer. */
1653 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001654 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001655 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001656 if (res == NULL)
1657 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001658 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001659 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001660 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001661 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001662 p += node->rq_buflen;
1663 }
1664 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001665
1666 /* Free all buffers */
1667 do
1668 {
1669 p = channel_get(channel, part);
1670 vim_free(p);
1671 } while (p != NULL);
1672
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001673 /* turn all NUL into NL */
1674 while (len > 0)
1675 {
1676 --len;
1677 if (res[len] == NUL)
1678 res[len] = NL;
1679 }
1680
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001681 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001682}
1683
1684/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001685 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001686 * Caller must check these bytes are available.
1687 */
1688 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001689channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001690{
1691 readq_T *head = &channel->ch_part[part].ch_head;
1692 readq_T *node = head->rq_next;
1693 char_u *buf = node->rq_buffer;
1694
1695 mch_memmove(buf, buf + len, node->rq_buflen - len);
1696 node->rq_buflen -= len;
1697}
1698
1699/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001700 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001701 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001702 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001703 */
1704 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001705channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001706{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001707 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001708 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001709 readq_T *last_node;
1710 readq_T *n;
1711 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001712 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001713 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001714
Bram Moolenaar77073442016-02-13 23:23:53 +01001715 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001716 return FAIL;
1717
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001718 last_node = node->rq_next;
1719 len = node->rq_buflen + last_node->rq_buflen + 1;
1720 if (want_nl)
1721 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001722 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001723 {
1724 last_node = last_node->rq_next;
1725 len += last_node->rq_buflen;
1726 }
1727
1728 p = newbuf = alloc(len);
1729 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001730 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001731 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001732 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001733 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001734 node->rq_buffer = newbuf;
1735 for (n = node; n != last_node; )
1736 {
1737 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001738 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001739 p += n->rq_buflen;
1740 vim_free(n->rq_buffer);
1741 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001742 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001743
1744 /* dispose of the collapsed nodes and their buffers */
1745 for (n = node->rq_next; n != last_node; )
1746 {
1747 n = n->rq_next;
1748 vim_free(n->rq_prev);
1749 }
1750 node->rq_next = last_node->rq_next;
1751 if (last_node->rq_next == NULL)
1752 head->rq_prev = node;
1753 else
1754 last_node->rq_next->rq_prev = node;
1755 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001756 return OK;
1757}
1758
1759/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001760 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001761 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001762 * Returns OK or FAIL.
1763 */
1764 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001765channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001766 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001767{
1768 readq_T *node;
1769 readq_T *head = &channel->ch_part[part].ch_head;
1770 char_u *p;
1771 int i;
1772
1773 node = (readq_T *)alloc(sizeof(readq_T));
1774 if (node == NULL)
1775 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001776 /* A NUL is added at the end, because netbeans code expects that.
1777 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001778 node->rq_buffer = alloc(len + 1);
1779 if (node->rq_buffer == NULL)
1780 {
1781 vim_free(node);
1782 return FAIL; /* out of memory */
1783 }
1784
1785 if (channel->ch_part[part].ch_mode == MODE_NL)
1786 {
1787 /* Drop any CR before a NL. */
1788 p = node->rq_buffer;
1789 for (i = 0; i < len; ++i)
1790 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1791 *p++ = buf[i];
1792 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001793 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001794 }
1795 else
1796 {
1797 mch_memmove(node->rq_buffer, buf, len);
1798 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001799 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001800 }
1801
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001802 if (prepend)
1803 {
1804 /* preend node to the head of the queue */
1805 node->rq_next = head->rq_next;
1806 node->rq_prev = NULL;
1807 if (head->rq_next == NULL)
1808 head->rq_prev = node;
1809 else
1810 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001811 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001812 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001813 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001814 {
1815 /* append node to the tail of the queue */
1816 node->rq_next = NULL;
1817 node->rq_prev = head->rq_prev;
1818 if (head->rq_prev == NULL)
1819 head->rq_next = node;
1820 else
1821 head->rq_prev->rq_next = node;
1822 head->rq_prev = node;
1823 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001824
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001825 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001826 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001827 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001828 fprintf(log_fd, "'");
1829 if (fwrite(buf, len, 1, log_fd) != 1)
1830 return FAIL;
1831 fprintf(log_fd, "'\n");
1832 }
1833 return OK;
1834}
1835
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001836 static int
1837channel_fill(js_read_T *reader)
1838{
1839 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001840 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001841 char_u *next = channel_get(channel, part);
1842 int unused;
1843 int len;
1844 char_u *p;
1845
1846 if (next == NULL)
1847 return FALSE;
1848
1849 unused = reader->js_end - reader->js_buf - reader->js_used;
1850 if (unused > 0)
1851 {
1852 /* Prepend unused text. */
1853 len = (int)STRLEN(next);
1854 p = alloc(unused + len + 1);
1855 if (p == NULL)
1856 {
1857 vim_free(next);
1858 return FALSE;
1859 }
1860 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1861 mch_memmove(p + unused, next, len + 1);
1862 vim_free(next);
1863 next = p;
1864 }
1865
1866 vim_free(reader->js_buf);
1867 reader->js_buf = next;
1868 reader->js_used = 0;
1869 return TRUE;
1870}
1871
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001872/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001873 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001874 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001875 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001876 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001877 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001878channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001879{
1880 js_read_T reader;
1881 typval_T listtv;
1882 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001883 chanpart_T *chanpart = &channel->ch_part[part];
1884 jsonq_T *head = &chanpart->ch_json_head;
1885 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001886 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001887
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001888 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001889 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001890
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001891 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001892 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001893 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001894 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001895 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001896
1897 /* When a message is incomplete we wait for a short while for more to
1898 * arrive. After the delay drop the input, otherwise a truncated string
1899 * or list will make us hang. */
1900 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001901 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001902 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001903 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001904 /* Only accept the response when it is a list with at least two
1905 * items. */
1906 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001907 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001908 if (listtv.v_type != VAR_LIST)
1909 ch_error(channel, "Did not receive a list, discarding");
1910 else
1911 ch_errorn(channel, "Expected list with two items, got %d",
1912 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001913 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001914 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001915 else
1916 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001917 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1918 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001919 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001920 else
1921 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001922 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001923 item->jq_value = alloc_tv();
1924 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001925 {
1926 vim_free(item);
1927 clear_tv(&listtv);
1928 }
1929 else
1930 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001931 *item->jq_value = listtv;
1932 item->jq_prev = head->jq_prev;
1933 head->jq_prev = item;
1934 item->jq_next = NULL;
1935 if (item->jq_prev == NULL)
1936 head->jq_next = item;
1937 else
1938 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001939 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001940 }
1941 }
1942 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001943
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001944 if (status == OK)
1945 chanpart->ch_waiting = FALSE;
1946 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001947 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001948 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001949 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001950 /* First time encountering incomplete message, set a deadline of
1951 * 100 msec. */
1952 ch_log(channel, "Incomplete message - wait for more");
1953 reader.js_used = 0;
1954 chanpart->ch_waiting = TRUE;
1955#ifdef WIN32
1956 chanpart->ch_deadline = GetTickCount() + 100L;
1957#else
1958 gettimeofday(&chanpart->ch_deadline, NULL);
1959 chanpart->ch_deadline.tv_usec += 100 * 1000;
1960 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1961 {
1962 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1963 ++chanpart->ch_deadline.tv_sec;
1964 }
1965#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001966 }
1967 else
1968 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001969 int timeout;
1970#ifdef WIN32
1971 timeout = GetTickCount() > chanpart->ch_deadline;
1972#else
1973 {
1974 struct timeval now_tv;
1975
1976 gettimeofday(&now_tv, NULL);
1977 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1978 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1979 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1980 }
1981#endif
1982 if (timeout)
1983 {
1984 status = FAIL;
1985 chanpart->ch_waiting = FALSE;
1986 }
1987 else
1988 {
1989 reader.js_used = 0;
1990 ch_log(channel, "still waiting on incomplete message");
1991 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001992 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001993 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001994
1995 if (status == FAIL)
1996 {
1997 ch_error(channel, "Decoding failed - discarding input");
1998 ret = FALSE;
1999 chanpart->ch_waiting = FALSE;
2000 }
2001 else if (reader.js_buf[reader.js_used] != NUL)
2002 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002003 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002004 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002005 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2006 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002007 ret = status == MAYBE ? FALSE: TRUE;
2008 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002009 else
2010 ret = FALSE;
2011
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002012 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002013 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002014}
2015
2016/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002017 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002018 */
2019 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002020remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002021{
Bram Moolenaar77073442016-02-13 23:23:53 +01002022 if (node->cq_prev == NULL)
2023 head->cq_next = node->cq_next;
2024 else
2025 node->cq_prev->cq_next = node->cq_next;
2026 if (node->cq_next == NULL)
2027 head->cq_prev = node->cq_prev;
2028 else
2029 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002030}
2031
2032/*
2033 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002034 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002035 */
2036 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002037remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002038{
Bram Moolenaar77073442016-02-13 23:23:53 +01002039 if (node->jq_prev == NULL)
2040 head->jq_next = node->jq_next;
2041 else
2042 node->jq_prev->jq_next = node->jq_next;
2043 if (node->jq_next == NULL)
2044 head->jq_prev = node->jq_prev;
2045 else
2046 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002047 vim_free(node);
2048}
2049
2050/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002051 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002052 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002053 * When "id" is zero or negative jut get the first message. But not the one
2054 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002055 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002056 * Return OK when found and return the value in "rettv".
2057 * Return FAIL otherwise.
2058 */
2059 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002060channel_get_json(
2061 channel_T *channel,
2062 ch_part_T part,
2063 int id,
2064 int without_callback,
2065 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002066{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002067 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002068 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002069
Bram Moolenaar77073442016-02-13 23:23:53 +01002070 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002071 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002072 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002073 typval_T *tv = &l->lv_first->li_tv;
2074
Bram Moolenaar958dc692016-12-01 15:34:12 +01002075 if ((without_callback || !item->jq_no_callback)
2076 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002077 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002078 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002079 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002080 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002081 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002082 if (tv->v_type == VAR_NUMBER)
2083 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002084 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002085 return OK;
2086 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002087 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002088 }
2089 return FAIL;
2090}
2091
Bram Moolenaar958dc692016-12-01 15:34:12 +01002092/*
2093 * Put back "rettv" into the JSON queue, there was no callback for it.
2094 * Takes over the values in "rettv".
2095 */
2096 static void
2097channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2098{
2099 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2100 jsonq_T *item = head->jq_next;
2101 jsonq_T *newitem;
2102
2103 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2104 /* last item was pushed back, append to the end */
2105 item = NULL;
2106 else while (item != NULL && item->jq_no_callback)
2107 /* append after the last item that was pushed back */
2108 item = item->jq_next;
2109
2110 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2111 if (newitem == NULL)
2112 clear_tv(rettv);
2113 else
2114 {
2115 newitem->jq_value = alloc_tv();
2116 if (newitem->jq_value == NULL)
2117 {
2118 vim_free(newitem);
2119 clear_tv(rettv);
2120 }
2121 else
2122 {
2123 newitem->jq_no_callback = FALSE;
2124 *newitem->jq_value = *rettv;
2125 if (item == NULL)
2126 {
2127 /* append to the end */
2128 newitem->jq_prev = head->jq_prev;
2129 head->jq_prev = newitem;
2130 newitem->jq_next = NULL;
2131 if (newitem->jq_prev == NULL)
2132 head->jq_next = newitem;
2133 else
2134 newitem->jq_prev->jq_next = newitem;
2135 }
2136 else
2137 {
2138 /* append after "item" */
2139 newitem->jq_prev = item;
2140 newitem->jq_next = item->jq_next;
2141 item->jq_next = newitem;
2142 if (newitem->jq_next == NULL)
2143 head->jq_prev = newitem;
2144 else
2145 newitem->jq_next->jq_prev = newitem;
2146 }
2147 }
2148 }
2149}
2150
Bram Moolenaarece61b02016-02-20 21:39:05 +01002151#define CH_JSON_MAX_ARGS 4
2152
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002153/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002154 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002155 * "argv[0]" is the command string.
2156 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002157 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002158 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002159channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002160{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002161 char_u *cmd = argv[0].vval.v_string;
2162 char_u *arg;
2163 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002164
Bram Moolenaarece61b02016-02-20 21:39:05 +01002165 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002166 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002167 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002168 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002169 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002170 return;
2171 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002172 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002173 if (arg == NULL)
2174 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002175
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002176 if (STRCMP(cmd, "ex") == 0)
2177 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002178 int save_called_emsg = called_emsg;
2179
2180 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002181 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002182 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002183 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002184 --emsg_silent;
2185 if (called_emsg)
2186 ch_logs(channel, "Ex command error: '%s'",
2187 (char *)get_vim_var_str(VV_ERRMSG));
2188 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002189 }
2190 else if (STRCMP(cmd, "normal") == 0)
2191 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002192 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002193
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002194 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002195 ea.arg = arg;
2196 ea.addr_count = 0;
2197 ea.forceit = TRUE; /* no mapping */
2198 ex_normal(&ea);
2199 }
2200 else if (STRCMP(cmd, "redraw") == 0)
2201 {
2202 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002203
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002204 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002205 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002206 ex_redraw(&ea);
2207 showruler(FALSE);
2208 setcursor();
2209 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002210#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002211 if (gui.in_use)
2212 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002213 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002214 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002215 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002216#endif
2217 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002218 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002219 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002220 int is_call = cmd[0] == 'c';
2221 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002222
Bram Moolenaarece61b02016-02-20 21:39:05 +01002223 if (argv[id_idx].v_type != VAR_UNKNOWN
2224 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002225 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002226 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002227 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002228 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002229 }
2230 else if (is_call && argv[2].v_type != VAR_LIST)
2231 {
2232 ch_error(channel, "third argument for call must be a list");
2233 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002234 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002235 }
2236 else
2237 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002238 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002239 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002240 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002241 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002242
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002243 /* Don't pollute the display with errors. */
2244 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002245 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002246 {
2247 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002248 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002249 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002250 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002251 {
2252 ch_logs(channel, "Calling '%s'", (char *)arg);
2253 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2254 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002255 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002256
2257 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002258 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002259 int id = argv[id_idx].vval.v_number;
2260
Bram Moolenaar55fab432016-02-07 16:53:13 +01002261 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002262 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002263 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002264 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002265 /* If evaluation failed or the result can't be encoded
2266 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002267 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002268 err_tv.v_type = VAR_STRING;
2269 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002270 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002271 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002272 if (json != NULL)
2273 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002274 channel_send(channel,
2275 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002276 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002277 vim_free(json);
2278 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002279 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002280 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002281 if (tv == &res_tv)
2282 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002283 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002284 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002285 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002286 }
2287 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002288 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002289 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002290 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002291 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002292}
2293
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002294/*
2295 * Invoke the callback at "cbhead".
2296 * Does not redraw but sets channel_need_redraw.
2297 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002298 static void
2299invoke_one_time_callback(
2300 channel_T *channel,
2301 cbq_T *cbhead,
2302 cbq_T *item,
2303 typval_T *argv)
2304{
2305 ch_logs(channel, "Invoking one-time callback %s",
2306 (char *)item->cq_callback);
2307 /* Remove the item from the list first, if the callback
2308 * invokes ch_close() the list will be cleared. */
2309 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002310 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002311 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002312 vim_free(item);
2313}
2314
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002315 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002316append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002317{
2318 buf_T *save_curbuf = curbuf;
2319 linenr_T lnum = buffer->b_ml.ml_line_count;
2320 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002321 chanpart_T *ch_part = &channel->ch_part[part];
2322 int save_p_ma = buffer->b_p_ma;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002323 int empty = (buffer->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002324
2325 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2326 {
2327 if (!ch_part->ch_nomod_error)
2328 {
2329 ch_error(channel, "Buffer is not modifiable, cannot append");
2330 ch_part->ch_nomod_error = TRUE;
2331 }
2332 return;
2333 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002334
2335 /* If the buffer is also used as input insert above the last
2336 * line. Don't write these lines. */
2337 if (save_write_to)
2338 {
2339 --lnum;
2340 buffer->b_write_to_channel = FALSE;
2341 }
2342
2343 /* Append to the buffer */
2344 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2345
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002346 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002347 curbuf = buffer;
2348 u_sync(TRUE);
2349 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002350 ignored = u_save(lnum, lnum + 1 + (empty ? 1 : 0));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002351
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002352 if (empty)
2353 {
2354 /* The buffer is empty, replace the first (dummy) line. */
2355 ml_replace(lnum, msg, TRUE);
2356 lnum = 0;
2357 }
2358 else
2359 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002360 appended_lines_mark(lnum, 1L);
2361 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002362 if (ch_part->ch_nomodifiable)
2363 buffer->b_p_ma = FALSE;
2364 else
2365 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002366
2367 if (buffer->b_nwindows > 0)
2368 {
2369 win_T *wp;
2370 win_T *save_curwin;
2371
2372 FOR_ALL_WINDOWS(wp)
2373 {
2374 if (wp->w_buffer == buffer
2375 && (save_write_to
2376 ? wp->w_cursor.lnum == lnum + 1
2377 : (wp->w_cursor.lnum == lnum
2378 && wp->w_cursor.col == 0)))
2379 {
2380 ++wp->w_cursor.lnum;
2381 save_curwin = curwin;
2382 curwin = wp;
2383 curbuf = curwin->w_buffer;
2384 scroll_cursor_bot(0, FALSE);
2385 curwin = save_curwin;
2386 curbuf = curwin->w_buffer;
2387 }
2388 }
2389 redraw_buf_later(buffer, VALID);
2390 channel_need_redraw = TRUE;
2391 }
2392
2393 if (save_write_to)
2394 {
2395 channel_T *ch;
2396
2397 /* Find channels reading from this buffer and adjust their
2398 * next-to-read line number. */
2399 buffer->b_write_to_channel = TRUE;
2400 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2401 {
2402 chanpart_T *in_part = &ch->ch_part[PART_IN];
2403
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002404 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002405 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2406 }
2407 }
2408}
2409
Bram Moolenaar437905c2016-04-26 19:01:05 +02002410 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002411drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002412{
2413 char_u *msg;
2414
2415 while ((msg = channel_get(channel, part)) != NULL)
2416 {
2417 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2418 vim_free(msg);
2419 }
2420}
2421
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002422/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002423 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002424 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002425 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002426 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002427 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002428may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002429{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002430 char_u *msg = NULL;
2431 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002432 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002433 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002434 chanpart_T *ch_part = &channel->ch_part[part];
2435 ch_mode_T ch_mode = ch_part->ch_mode;
2436 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002437 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002438 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002439 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002440 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002441 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002442
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002443 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002444 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002445 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002446
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002447 /* Use a message-specific callback, part callback or channel callback */
2448 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2449 if (cbitem->cq_seq_nr == 0)
2450 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002451 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002452 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002453 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002454 partial = cbitem->cq_partial;
2455 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002456 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002457 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002458 callback = ch_part->ch_callback;
2459 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002460 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002461 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002462 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002463 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002464 partial = channel->ch_partial;
2465 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002466
Bram Moolenaarec68a992016-10-03 21:37:41 +02002467 buffer = ch_part->ch_bufref.br_buf;
2468 if (buffer != NULL && !bufref_valid(&ch_part->ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002469 {
2470 /* buffer was wiped out */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002471 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002472 buffer = NULL;
2473 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002474
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002475 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002476 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002477 listitem_T *item;
2478 int argc = 0;
2479
Bram Moolenaard7ece102016-02-02 23:23:02 +01002480 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002481 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002482 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002483 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002484 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002485 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002486 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002487 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002488
Bram Moolenaarece61b02016-02-20 21:39:05 +01002489 for (item = listtv->vval.v_list->lv_first;
2490 item != NULL && argc < CH_JSON_MAX_ARGS;
2491 item = item->li_next)
2492 argv[argc++] = item->li_tv;
2493 while (argc < CH_JSON_MAX_ARGS)
2494 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002495
Bram Moolenaarece61b02016-02-20 21:39:05 +01002496 if (argv[0].v_type == VAR_STRING)
2497 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002498 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002499 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002500 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002501 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002502 }
2503
Bram Moolenaarece61b02016-02-20 21:39:05 +01002504 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002505 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002506 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002507 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002508 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002509 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002510 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002511 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002512 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002513 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002514 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002515 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002516 return FALSE;
2517 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002518 else
2519 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002520 /* If there is no callback or buffer drop the message. */
2521 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002522 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002523 /* If there is a close callback it may use ch_read() to get the
2524 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002525 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002526 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002527 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002528 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002529
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002530 if (ch_mode == MODE_NL)
2531 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002532 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002533 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002534 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002535
2536 /* See if we have a message ending in NL in the first buffer. If
2537 * not try to concatenate the first and the second buffer. */
2538 while (TRUE)
2539 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002540 node = channel_peek(channel, part);
2541 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002542 if (nl != NULL)
2543 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002544 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002545 {
2546 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2547 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002548 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002549 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002550 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002551 buf = node->rq_buffer;
2552
Bram Moolenaarec68a992016-10-03 21:37:41 +02002553 if (nl == NULL)
2554 {
2555 /* Flush remaining message that is missing a NL. */
2556 buf = vim_realloc(buf, node->rq_buflen + 1);
2557 if (buf == NULL)
2558 return FALSE;
2559 node->rq_buffer = buf;
2560 nl = buf + node->rq_buflen++;
2561 *nl = NUL;
2562 }
2563
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002564 /* Convert NUL to NL, the internal representation. */
2565 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2566 if (*p == NUL)
2567 *p = NL;
2568
2569 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002570 {
2571 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002572 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002573 *nl = NUL;
2574 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002575 else
2576 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002577 /* Copy the message into allocated memory (excluding the NL)
2578 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002579 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002580 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002581 }
2582 }
2583 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002584 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002585 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002586 * get everything we have.
2587 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002588 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002589 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002590
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002591 if (msg == NULL)
2592 return FALSE; /* out of memory (and avoids Coverity warning) */
2593
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002594 argv[1].v_type = VAR_STRING;
2595 argv[1].vval.v_string = msg;
2596 }
2597
Bram Moolenaara07fec92016-02-05 21:04:08 +01002598 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002599 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002600 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002601
Bram Moolenaar958dc692016-12-01 15:34:12 +01002602 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002603 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002604 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002605 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002606 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002607 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002608 break;
2609 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002610 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002611 {
2612 if (channel->ch_drop_never)
2613 {
2614 /* message must be read with ch_read() */
2615 channel_push_json(channel, part, listtv);
2616 listtv = NULL;
2617 }
2618 else
2619 ch_logn(channel, "Dropping message %d without callback",
2620 seq_nr);
2621 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002622 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002623 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002624 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002625 if (buffer != NULL)
2626 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002627 if (msg == NULL)
2628 /* JSON or JS mode: re-encode the message. */
2629 msg = json_encode(listtv, ch_mode);
2630 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002631 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002632 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002633
Bram Moolenaar187db502016-02-27 14:44:26 +01002634 if (callback != NULL)
2635 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002636 if (cbitem != NULL)
2637 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2638 else
2639 {
2640 /* invoke the channel callback */
2641 ch_logs(channel, "Invoking channel callback %s",
2642 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002643 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002644 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002645 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002646 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002647 else
Bram Moolenaar958dc692016-12-01 15:34:12 +01002648 ch_logn(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002649
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002650 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002651 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002652 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002653
2654 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002655}
2656
2657/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002658 * Return TRUE when channel "channel" is open for writing to.
2659 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002660 */
2661 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002662channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002663{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002664 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002665 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002666}
2667
2668/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002669 * Return TRUE when channel "channel" is open for reading or writing.
2670 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002671 */
2672 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002673channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002674{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002675 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002676 || channel->CH_IN_FD != INVALID_FD
2677 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002678 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002679}
2680
2681/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002682 * Return TRUE if "channel" has JSON or other typeahead.
2683 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002684 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002685channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002686{
2687 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2688
2689 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2690 {
2691 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2692 jsonq_T *item = head->jq_next;
2693
2694 return item != NULL;
2695 }
2696 return channel_peek(channel, part) != NULL;
2697}
2698
2699/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002700 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002701 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002702 */
2703 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002704channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002705{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002706 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002707 int has_readahead = FALSE;
2708
Bram Moolenaar77073442016-02-13 23:23:53 +01002709 if (channel == NULL)
2710 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002711 if (req_part == PART_OUT)
2712 {
2713 if (channel->CH_OUT_FD != INVALID_FD)
2714 return "open";
2715 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002716 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002717 }
2718 else if (req_part == PART_ERR)
2719 {
2720 if (channel->CH_ERR_FD != INVALID_FD)
2721 return "open";
2722 if (channel_has_readahead(channel, PART_ERR))
2723 has_readahead = TRUE;
2724 }
2725 else
2726 {
2727 if (channel_is_open(channel))
2728 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002729 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002730 if (channel_has_readahead(channel, part))
2731 {
2732 has_readahead = TRUE;
2733 break;
2734 }
2735 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002736
2737 if (has_readahead)
2738 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002739 return "closed";
2740}
2741
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002742 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002743channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002744{
2745 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002746 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002747 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002748 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002749 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002750
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002751 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002752 STRCAT(namebuf, "_");
2753 tail = STRLEN(namebuf);
2754
2755 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002756 if (chanpart->ch_fd != INVALID_FD)
2757 status = "open";
2758 else if (channel_has_readahead(channel, part))
2759 status = "buffered";
2760 else
2761 status = "closed";
2762 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002763
2764 STRCPY(namebuf + tail, "mode");
2765 switch (chanpart->ch_mode)
2766 {
2767 case MODE_NL: s = "NL"; break;
2768 case MODE_RAW: s = "RAW"; break;
2769 case MODE_JSON: s = "JSON"; break;
2770 case MODE_JS: s = "JS"; break;
2771 }
2772 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2773
2774 STRCPY(namebuf + tail, "io");
2775 if (part == PART_SOCK)
2776 s = "socket";
2777 else switch (chanpart->ch_io)
2778 {
2779 case JIO_NULL: s = "null"; break;
2780 case JIO_PIPE: s = "pipe"; break;
2781 case JIO_FILE: s = "file"; break;
2782 case JIO_BUFFER: s = "buffer"; break;
2783 case JIO_OUT: s = "out"; break;
2784 }
2785 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2786
2787 STRCPY(namebuf + tail, "timeout");
2788 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2789}
2790
2791 void
2792channel_info(channel_T *channel, dict_T *dict)
2793{
2794 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002795 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002796
2797 if (channel->ch_hostname != NULL)
2798 {
2799 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2800 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2801 channel_part_info(channel, dict, "sock", PART_SOCK);
2802 }
2803 else
2804 {
2805 channel_part_info(channel, dict, "out", PART_OUT);
2806 channel_part_info(channel, dict, "err", PART_ERR);
2807 channel_part_info(channel, dict, "in", PART_IN);
2808 }
2809}
2810
Bram Moolenaar77073442016-02-13 23:23:53 +01002811/*
2812 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002813 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002814 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002815 */
2816 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002817channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002818{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002819 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002820
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002821#ifdef FEAT_GUI
2822 channel_gui_unregister(channel);
2823#endif
2824
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002825 ch_close_part(channel, PART_SOCK);
2826 ch_close_part(channel, PART_IN);
2827 ch_close_part(channel, PART_OUT);
2828 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002829
Bram Moolenaar8b374212016-02-24 20:43:06 +01002830 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002831 {
2832 typval_T argv[1];
2833 typval_T rettv;
2834 int dummy;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002835 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002836
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002837 /* Invoke callbacks before the close callback, since it's weird to
2838 * first invoke the close callback. Increment the refcount to avoid
2839 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002840 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002841 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002842 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002843 while (may_invoke_callback(channel, part))
2844 ;
2845
2846 /* Invoke the close callback, if still set. */
2847 if (channel->ch_close_cb != NULL)
2848 {
2849 ch_logs(channel, "Invoking close callback %s",
2850 (char *)channel->ch_close_cb);
2851 argv[0].v_type = VAR_CHANNEL;
2852 argv[0].vval.v_channel = channel;
2853 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002854 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002855 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002856 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002857 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002858 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002859
2860 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002861 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002862 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002863 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002864
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002865 --channel->ch_refcount;
2866
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002867 if (channel_need_redraw)
2868 {
2869 channel_need_redraw = FALSE;
2870 redraw_after_callback();
2871 }
2872
Bram Moolenaar958dc692016-12-01 15:34:12 +01002873 if (!channel->ch_drop_never)
2874 /* any remaining messages are useless now */
2875 for (part = PART_SOCK; part < PART_IN; ++part)
2876 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002877 }
2878
2879 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002880}
2881
Bram Moolenaard04a0202016-01-26 23:30:18 +01002882/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002883 * Close the "in" part channel "channel".
2884 */
2885 void
2886channel_close_in(channel_T *channel)
2887{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002888 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002889}
2890
2891/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002892 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002893 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002894 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002895channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002896{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002897 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2898 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002899
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002900 while (channel_peek(channel, part) != NULL)
2901 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002902
2903 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002904 {
2905 cbq_T *node = cb_head->cq_next;
2906
2907 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002908 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002909 vim_free(node);
2910 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002911
2912 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002913 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002914 free_tv(json_head->jq_next->jq_value);
2915 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002916 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002917
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002918 free_callback(channel->ch_part[part].ch_callback,
2919 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002920 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002921 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002922}
2923
2924/*
2925 * Clear all the read buffers on "channel".
2926 */
2927 void
2928channel_clear(channel_T *channel)
2929{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002930 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002931 vim_free(channel->ch_hostname);
2932 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002933 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002934 channel_clear_one(channel, PART_OUT);
2935 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002936 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002937 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002938 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002939 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002940 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002941 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002942 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002943}
2944
Bram Moolenaar77073442016-02-13 23:23:53 +01002945#if defined(EXITFREE) || defined(PROTO)
2946 void
2947channel_free_all(void)
2948{
2949 channel_T *channel;
2950
Bram Moolenaard6051b52016-02-28 15:49:03 +01002951 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002952 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2953 channel_clear(channel);
2954}
2955#endif
2956
2957
Bram Moolenaar715d2852016-04-30 17:06:31 +02002958/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002959#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002960
2961/* Buffer size for reading incoming messages. */
2962#define MAXMSGSIZE 4096
2963
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002964#if defined(HAVE_SELECT)
2965/*
2966 * Add write fds where we are waiting for writing to be possible.
2967 */
2968 static int
2969channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2970{
2971 int maxfd = maxfd_arg;
2972 channel_T *ch;
2973
2974 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2975 {
2976 chanpart_T *in_part = &ch->ch_part[PART_IN];
2977
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002978 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002979 {
2980 FD_SET((int)in_part->ch_fd, wfds);
2981 if ((int)in_part->ch_fd >= maxfd)
2982 maxfd = (int)in_part->ch_fd + 1;
2983 }
2984 }
2985 return maxfd;
2986}
2987#else
2988/*
2989 * Add write fds where we are waiting for writing to be possible.
2990 */
2991 static int
2992channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2993{
2994 int nfd = nfd_in;
2995 channel_T *ch;
2996
2997 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2998 {
2999 chanpart_T *in_part = &ch->ch_part[PART_IN];
3000
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003001 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003002 {
3003 in_part->ch_poll_idx = nfd;
3004 fds[nfd].fd = in_part->ch_fd;
3005 fds[nfd].events = POLLOUT;
3006 ++nfd;
3007 }
3008 else
3009 in_part->ch_poll_idx = -1;
3010 }
3011 return nfd;
3012}
3013#endif
3014
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003015typedef enum {
3016 CW_READY,
3017 CW_NOT_READY,
3018 CW_ERROR
3019} channel_wait_result;
3020
Bram Moolenaard04a0202016-01-26 23:30:18 +01003021/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003022 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003023 * Return CW_READY when there is something to read.
3024 * Return CW_NOT_READY when there is nothing to read.
3025 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003026 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003027 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003028channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003029{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003030 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003031 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003032
Bram Moolenaard8070362016-02-15 21:56:54 +01003033# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003034 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003035 {
3036 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003037 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003038 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003039 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003040
3041 /* reading from a pipe, not a socket */
3042 while (TRUE)
3043 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003044 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3045
3046 if (r && nread > 0)
3047 return CW_READY;
3048 if (r == 0)
3049 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003050
3051 /* perhaps write some buffer lines */
3052 channel_write_any_lines();
3053
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003054 sleep_time = deadline - GetTickCount();
3055 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003056 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003057 /* Wait for a little while. Very short at first, up to 10 msec
3058 * after looping a few times. */
3059 if (sleep_time > delay)
3060 sleep_time = delay;
3061 Sleep(sleep_time);
3062 delay = delay * 2;
3063 if (delay > 10)
3064 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003065 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003066 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003067 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003068#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003069 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003070#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003071 struct timeval tval;
3072 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003073 fd_set wfds;
3074 int ret;
3075 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003076
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003077 tval.tv_sec = timeout / 1000;
3078 tval.tv_usec = (timeout % 1000) * 1000;
3079 for (;;)
3080 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003081 FD_ZERO(&rfds);
3082 FD_SET((int)fd, &rfds);
3083
3084 /* Write lines to a pipe when a pipe can be written to. Need to
3085 * set this every time, some buffers may be done. */
3086 maxfd = (int)fd + 1;
3087 FD_ZERO(&wfds);
3088 maxfd = channel_fill_wfds(maxfd, &wfds);
3089
3090 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003091# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003092 SOCK_ERRNO;
3093 if (ret == -1 && errno == EINTR)
3094 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003095# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003096 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003097 {
3098 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003099 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003100 channel_write_any_lines();
3101 continue;
3102 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003103 break;
3104 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003105#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003106 for (;;)
3107 {
3108 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3109 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003110
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003111 fds[0].fd = fd;
3112 fds[0].events = POLLIN;
3113 nfd = channel_fill_poll_write(nfd, fds);
3114 if (poll(fds, nfd, timeout) > 0)
3115 {
3116 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003117 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003118 channel_write_any_lines();
3119 continue;
3120 }
3121 break;
3122 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003123#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003124 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003125 return CW_NOT_READY;
3126}
3127
3128 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003129ch_close_part_on_error(
3130 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003131{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003132 char msgbuf[80];
3133
3134 vim_snprintf(msgbuf, sizeof(msgbuf),
3135 "%%s(): Read %s from ch_part[%d], closing",
3136 (is_err ? "error" : "EOF"), part);
3137
3138 if (is_err)
3139 /* Do not call emsg(), most likely the other end just exited. */
3140 ch_errors(channel, msgbuf, func);
3141 else
3142 ch_logs(channel, msgbuf, func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003143
3144 /* Queue a "DETACH" netbeans message in the command queue in order to
3145 * terminate the netbeans session later. Do not end the session here
3146 * directly as we may be running in the context of a call to
3147 * netbeans_parse_messages():
3148 * netbeans_parse_messages
3149 * -> autocmd triggered while processing the netbeans cmd
3150 * -> ui_breakcheck
3151 * -> gui event loop or select loop
3152 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003153 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003154 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003155 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003156 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003157 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3158
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003159 /* When reading is not possible close this part of the channel. Don't
3160 * close the channel yet, there may be something to read on another part. */
3161 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003162
3163#ifdef FEAT_GUI
3164 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003165 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003166#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003167}
3168
3169 static void
3170channel_close_now(channel_T *channel)
3171{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003172 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003173 if (channel->ch_nb_close_cb != NULL)
3174 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003175 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003176}
3177
3178/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003179 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003180 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003181 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003182 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003183 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003184channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003185{
3186 static char_u *buf = NULL;
3187 int len = 0;
3188 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003189 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003190 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003191
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003192 fd = channel->ch_part[part].ch_fd;
3193 if (fd == INVALID_FD)
3194 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003195 ch_errors(channel, "channel_read() called while %s part is closed",
3196 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003197 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003198 }
3199 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003200
3201 /* Allocate a buffer to read into. */
3202 if (buf == NULL)
3203 {
3204 buf = alloc(MAXMSGSIZE);
3205 if (buf == NULL)
3206 return; /* out of memory! */
3207 }
3208
3209 /* Keep on reading for as long as there is something to read.
3210 * Use select() or poll() to avoid blocking on a message that is exactly
3211 * MAXMSGSIZE long. */
3212 for (;;)
3213 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003214 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003215 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003216 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003217 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003218 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003219 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003220 if (len <= 0)
3221 break; /* error or nothing more to read */
3222
3223 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003224 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003225 readlen += len;
3226 if (len < MAXMSGSIZE)
3227 break; /* did read everything that's available */
3228 }
3229
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003230 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003231 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003232 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003233
3234#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003235 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003236 if (CH_HAS_GUI && gtk_main_level() > 0)
3237 gtk_main_quit();
3238#endif
3239}
3240
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003241/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003242 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003243 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003244 * Returns what was read in allocated memory.
3245 * Returns NULL in case of error or timeout.
3246 */
3247 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003248channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003249{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003250 char_u *buf;
3251 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003252 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003253 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003254 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003255 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003256
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003257 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003258 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003259
3260 while (TRUE)
3261 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003262 node = channel_peek(channel, part);
3263 if (node != NULL)
3264 {
3265 if (mode == MODE_RAW || (mode == MODE_NL
3266 && channel_first_nl(node) != NULL))
3267 /* got a complete message */
3268 break;
3269 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3270 continue;
3271 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003272
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003273 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003274 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003275 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003276 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003277 {
3278 ch_log(channel, "Timed out");
3279 return NULL;
3280 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003281 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003282 }
3283
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003284 if (mode == MODE_RAW)
3285 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003286 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003287 }
3288 else
3289 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003290 char_u *p;
3291
3292 buf = node->rq_buffer;
3293 nl = channel_first_nl(node);
3294
3295 /* Convert NUL to NL, the internal representation. */
3296 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3297 if (*p == NUL)
3298 *p = NL;
3299
3300 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003301 {
3302 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003303 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003304 *nl = NUL;
3305 }
3306 else
3307 {
3308 /* Copy the message into allocated memory and remove it from the
3309 * buffer. */
3310 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003311 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003312 }
3313 }
3314 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003315 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003316 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003317}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003318
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003319/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003320 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003321 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003322 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003323 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003324 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003325 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003326channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003327 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003328 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003329 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003330 int id,
3331 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003332{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003333 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003334 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003335 int timeout;
3336 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003337
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003338 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003339 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003340 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003341 for (;;)
3342 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003343 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003344
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003345 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003346 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003347 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003348 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003349 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003350 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003351
Bram Moolenaard7ece102016-02-02 23:23:02 +01003352 if (!more)
3353 {
3354 /* Handle any other messages in the queue. If done some more
3355 * messages may have arrived. */
3356 if (channel_parse_messages())
3357 continue;
3358
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003359 /* Wait for up to the timeout. If there was an incomplete message
3360 * use the deadline for that. */
3361 timeout = timeout_arg;
3362 if (chanpart->ch_waiting)
3363 {
3364#ifdef WIN32
3365 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3366#else
3367 {
3368 struct timeval now_tv;
3369
3370 gettimeofday(&now_tv, NULL);
3371 timeout = (chanpart->ch_deadline.tv_sec
3372 - now_tv.tv_sec) * 1000
3373 + (chanpart->ch_deadline.tv_usec
3374 - now_tv.tv_usec) / 1000
3375 + 1;
3376 }
3377#endif
3378 if (timeout < 0)
3379 {
3380 /* Something went wrong, channel_parse_json() didn't
3381 * discard message. Cancel waiting. */
3382 chanpart->ch_waiting = FALSE;
3383 timeout = timeout_arg;
3384 }
3385 else if (timeout > timeout_arg)
3386 timeout = timeout_arg;
3387 }
3388 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003389 if (fd == INVALID_FD
3390 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003391 {
3392 if (timeout == timeout_arg)
3393 {
3394 if (fd != INVALID_FD)
3395 ch_log(channel, "Timed out");
3396 break;
3397 }
3398 }
3399 else
3400 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003401 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003402 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003403 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003404 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003405}
3406
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003407/*
3408 * Common for ch_read() and ch_readraw().
3409 */
3410 void
3411common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3412{
3413 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003414 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003415 jobopt_T opt;
3416 int mode;
3417 int timeout;
3418 int id = -1;
3419 typval_T *listtv = NULL;
3420
3421 /* return an empty string by default */
3422 rettv->v_type = VAR_STRING;
3423 rettv->vval.v_string = NULL;
3424
3425 clear_job_options(&opt);
3426 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3427 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003428 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003429
Bram Moolenaar437905c2016-04-26 19:01:05 +02003430 if (opt.jo_set & JO_PART)
3431 part = opt.jo_part;
3432 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003433 if (channel != NULL)
3434 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003435 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003436 part = channel_part_read(channel);
3437 mode = channel_get_mode(channel, part);
3438 timeout = channel_get_timeout(channel, part);
3439 if (opt.jo_set & JO_TIMEOUT)
3440 timeout = opt.jo_timeout;
3441
3442 if (raw || mode == MODE_RAW || mode == MODE_NL)
3443 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3444 else
3445 {
3446 if (opt.jo_set & JO_ID)
3447 id = opt.jo_id;
3448 channel_read_json_block(channel, part, timeout, id, &listtv);
3449 if (listtv != NULL)
3450 {
3451 *rettv = *listtv;
3452 vim_free(listtv);
3453 }
3454 else
3455 {
3456 rettv->v_type = VAR_SPECIAL;
3457 rettv->vval.v_number = VVAL_NONE;
3458 }
3459 }
3460 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003461
3462theend:
3463 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003464}
3465
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003466# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3467 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003468/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003469 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003470 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003471 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003472 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003473channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003474{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003475 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003476 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003477
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003478 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003479 for (channel = first_channel; channel != NULL;
3480 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003481 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003482 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003483 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003484 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003485 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003486 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003487 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003488 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003489 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003490}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003491# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003492
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003493# if defined(WIN32) || defined(PROTO)
3494/*
3495 * Check the channels for anything that is ready to be read.
3496 * The data is put in the read queue.
3497 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003498 void
3499channel_handle_events(void)
3500{
3501 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003502 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003503 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003504
3505 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3506 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003507 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003508 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003509 {
3510 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003511 if (fd != INVALID_FD)
3512 {
3513 int r = channel_wait(channel, fd, 0);
3514
3515 if (r == CW_READY)
3516 channel_read(channel, part, "channel_handle_events");
3517 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003518 ch_close_part_on_error(channel, part, TRUE,
3519 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003520 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003521 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003522 }
3523}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003524# endif
3525
Bram Moolenaard04a0202016-01-26 23:30:18 +01003526/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003527 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003528 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003529 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003530 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003531 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003532channel_send(
3533 channel_T *channel,
3534 ch_part_T part,
3535 char_u *buf,
3536 int len,
3537 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003538{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003539 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003540 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003541
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003542 fd = channel->ch_part[part].ch_fd;
3543 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003544 {
3545 if (!channel->ch_error && fun != NULL)
3546 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003547 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003548 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003549 }
3550 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003551 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003552 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003553
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003554 if (log_fd != NULL)
3555 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003556 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003557 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003558 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003559 fprintf(log_fd, "'\n");
3560 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003561 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003562 }
3563
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003564 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003565 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003566 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003567 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003568 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003569 {
3570 if (!channel->ch_error && fun != NULL)
3571 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003572 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003573 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003574 }
3575 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003576 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003577 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003578
3579 channel->ch_error = FALSE;
3580 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003581}
3582
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003583/*
3584 * Common for "ch_sendexpr()" and "ch_sendraw()".
3585 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003586 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003587 * Otherwise returns NULL.
3588 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003589 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003590send_common(
3591 typval_T *argvars,
3592 char_u *text,
3593 int id,
3594 int eval,
3595 jobopt_T *opt,
3596 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003597 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003598{
3599 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003600 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003601
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003602 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003603 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003604 if (channel == NULL)
3605 return NULL;
3606 part_send = channel_part_send(channel);
3607 *part_read = channel_part_read(channel);
3608
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003609 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3610 return NULL;
3611
3612 /* Set the callback. An empty callback means no callback and not reading
3613 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3614 * allowed. */
3615 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3616 {
3617 if (eval)
3618 {
3619 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3620 return NULL;
3621 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003622 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003623 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003624 }
3625
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003626 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003627 && opt->jo_callback == NULL)
3628 return channel;
3629 return NULL;
3630}
3631
3632/*
3633 * common for "ch_evalexpr()" and "ch_sendexpr()"
3634 */
3635 void
3636ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3637{
3638 char_u *text;
3639 typval_T *listtv;
3640 channel_T *channel;
3641 int id;
3642 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003643 ch_part_T part_send;
3644 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003645 jobopt_T opt;
3646 int timeout;
3647
3648 /* return an empty string by default */
3649 rettv->v_type = VAR_STRING;
3650 rettv->vval.v_string = NULL;
3651
Bram Moolenaar437905c2016-04-26 19:01:05 +02003652 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003653 if (channel == NULL)
3654 return;
3655 part_send = channel_part_send(channel);
3656
3657 ch_mode = channel_get_mode(channel, part_send);
3658 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3659 {
3660 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3661 return;
3662 }
3663
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003664 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003665 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003666 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003667 if (text == NULL)
3668 return;
3669
3670 channel = send_common(argvars, text, id, eval, &opt,
3671 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3672 vim_free(text);
3673 if (channel != NULL && eval)
3674 {
3675 if (opt.jo_set & JO_TIMEOUT)
3676 timeout = opt.jo_timeout;
3677 else
3678 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003679 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3680 == OK)
3681 {
3682 list_T *list = listtv->vval.v_list;
3683
3684 /* Move the item from the list and then change the type to
3685 * avoid the value being freed. */
3686 *rettv = list->lv_last->li_tv;
3687 list->lv_last->li_tv.v_type = VAR_NUMBER;
3688 free_tv(listtv);
3689 }
3690 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003691 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003692}
3693
3694/*
3695 * common for "ch_evalraw()" and "ch_sendraw()"
3696 */
3697 void
3698ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3699{
3700 char_u buf[NUMBUFLEN];
3701 char_u *text;
3702 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003703 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003704 jobopt_T opt;
3705 int timeout;
3706
3707 /* return an empty string by default */
3708 rettv->v_type = VAR_STRING;
3709 rettv->vval.v_string = NULL;
3710
3711 text = get_tv_string_buf(&argvars[1], buf);
3712 channel = send_common(argvars, text, 0, eval, &opt,
3713 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3714 if (channel != NULL && eval)
3715 {
3716 if (opt.jo_set & JO_TIMEOUT)
3717 timeout = opt.jo_timeout;
3718 else
3719 timeout = channel_get_timeout(channel, part_read);
3720 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3721 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003722 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003723}
3724
Bram Moolenaard04a0202016-01-26 23:30:18 +01003725# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003726/*
3727 * Add open channels to the poll struct.
3728 * Return the adjusted struct index.
3729 * The type of "fds" is hidden to avoid problems with the function proto.
3730 */
3731 int
3732channel_poll_setup(int nfd_in, void *fds_in)
3733{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003734 int nfd = nfd_in;
3735 channel_T *channel;
3736 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003737 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003738
Bram Moolenaar77073442016-02-13 23:23:53 +01003739 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003740 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003741 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003742 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003743 chanpart_T *ch_part = &channel->ch_part[part];
3744
3745 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003746 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003747 ch_part->ch_poll_idx = nfd;
3748 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003749 fds[nfd].events = POLLIN;
3750 nfd++;
3751 }
3752 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003753 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003754 }
3755 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003756
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003757 nfd = channel_fill_poll_write(nfd, fds);
3758
Bram Moolenaare0874f82016-01-24 20:36:41 +01003759 return nfd;
3760}
3761
3762/*
3763 * The type of "fds" is hidden to avoid problems with the function proto.
3764 */
3765 int
3766channel_poll_check(int ret_in, void *fds_in)
3767{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003768 int ret = ret_in;
3769 channel_T *channel;
3770 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003771 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003772 int idx;
3773 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003774
Bram Moolenaar77073442016-02-13 23:23:53 +01003775 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003776 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003777 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003778 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003779 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003780
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003781 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003782 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003783 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003784 --ret;
3785 }
3786 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003787
3788 in_part = &channel->ch_part[PART_IN];
3789 idx = in_part->ch_poll_idx;
3790 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3791 {
3792 if (in_part->ch_buf_append)
3793 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003794 if (in_part->ch_bufref.br_buf != NULL)
3795 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003796 }
3797 else
3798 channel_write_in(channel);
3799 --ret;
3800 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003801 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003802
3803 return ret;
3804}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003805# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003806
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003807# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003808/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003809 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003810 */
3811 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003812channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003813{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003814 int maxfd = maxfd_in;
3815 channel_T *channel;
3816 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003817 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003818 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003819
Bram Moolenaar77073442016-02-13 23:23:53 +01003820 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003821 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003822 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003823 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003824 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003825
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003826 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003827 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003828 FD_SET((int)fd, rfds);
3829 if (maxfd < (int)fd)
3830 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003831 }
3832 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003833 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003834
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003835 maxfd = channel_fill_wfds(maxfd, wfds);
3836
Bram Moolenaare0874f82016-01-24 20:36:41 +01003837 return maxfd;
3838}
3839
3840/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003841 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003842 */
3843 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003844channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003845{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003846 int ret = ret_in;
3847 channel_T *channel;
3848 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003849 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003850 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003851 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003852
Bram Moolenaar77073442016-02-13 23:23:53 +01003853 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003854 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003855 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003856 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003857 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003858
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003859 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003860 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003861 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003862 --ret;
3863 }
3864 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003865
3866 in_part = &channel->ch_part[PART_IN];
3867 if (ret > 0 && in_part->ch_fd != INVALID_FD
3868 && FD_ISSET(in_part->ch_fd, wfds))
3869 {
3870 if (in_part->ch_buf_append)
3871 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003872 if (in_part->ch_bufref.br_buf != NULL)
3873 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003874 }
3875 else
3876 channel_write_in(channel);
3877 --ret;
3878 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003879 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003880
3881 return ret;
3882}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003883# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003884
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003885/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003886 * Execute queued up commands.
3887 * Invoked from the main loop when it's safe to execute received commands.
3888 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003889 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003890 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003891channel_parse_messages(void)
3892{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003893 channel_T *channel = first_channel;
3894 int ret = FALSE;
3895 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003896 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003897#ifdef ELAPSED_FUNC
3898 ELAPSED_TYPE start_tv;
3899
3900 ELAPSED_INIT(start_tv);
3901#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003902
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003903 ++safe_to_invoke_callback;
3904
Bram Moolenaard0b65022016-03-06 21:50:33 +01003905 /* Only do this message when another message was given, otherwise we get
3906 * lots of them. */
3907 if (did_log_msg)
3908 {
3909 ch_log(NULL, "looking for messages on channels");
3910 did_log_msg = FALSE;
3911 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003912 while (channel != NULL)
3913 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003914 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003915 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003916 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003917 channel_close_now(channel);
3918 /* channel may have been freed, start over */
3919 channel = first_channel;
3920 continue;
3921 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003922 if (channel->ch_to_be_freed)
3923 {
3924 channel_free(channel);
3925 /* channel has been freed, start over */
3926 channel = first_channel;
3927 continue;
3928 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003929 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003930 {
3931 /* channel is no longer useful, free it */
3932 channel_free(channel);
3933 channel = first_channel;
3934 part = PART_SOCK;
3935 continue;
3936 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003937 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003938 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003939 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003940 /* Increase the refcount, in case the handler causes the channel
3941 * to be unreferenced or closed. */
3942 ++channel->ch_refcount;
3943 r = may_invoke_callback(channel, part);
3944 if (r == OK)
3945 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003946 if (channel_unref(channel) || (r == OK
3947#ifdef ELAPSED_FUNC
3948 /* Limit the time we loop here to 100 msec, otherwise
3949 * Vim becomes unresponsive when the callback takes
3950 * more than a bit of time. */
3951 && ELAPSED_FUNC(start_tv) < 100L
3952#endif
3953 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003954 {
3955 /* channel was freed or something was done, start over */
3956 channel = first_channel;
3957 part = PART_SOCK;
3958 continue;
3959 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003960 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003961 if (part < PART_ERR)
3962 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003963 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003964 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003965 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003966 part = PART_SOCK;
3967 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003968 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003969
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003970 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003971 {
3972 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003973 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003974 }
3975
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003976 --safe_to_invoke_callback;
3977
Bram Moolenaard7ece102016-02-02 23:23:02 +01003978 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003979}
3980
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003981/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01003982 * Return TRUE if any channel has readahead. That means we should not block on
3983 * waiting for input.
3984 */
3985 int
3986channel_any_readahead(void)
3987{
3988 channel_T *channel = first_channel;
3989 ch_part_T part = PART_SOCK;
3990
3991 while (channel != NULL)
3992 {
3993 if (channel_has_readahead(channel, part))
3994 return TRUE;
3995 if (part < PART_ERR)
3996 ++part;
3997 else
3998 {
3999 channel = channel->ch_next;
4000 part = PART_SOCK;
4001 }
4002 }
4003 return FALSE;
4004}
4005
4006/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004007 * Mark references to lists used in channels.
4008 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004009 int
4010set_ref_in_channel(int copyID)
4011{
Bram Moolenaar77073442016-02-13 23:23:53 +01004012 int abort = FALSE;
4013 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004014 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004015
Bram Moolenaar77073442016-02-13 23:23:53 +01004016 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004017 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004018 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004019 tv.v_type = VAR_CHANNEL;
4020 tv.vval.v_channel = channel;
4021 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004022 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004023 return abort;
4024}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004025
4026/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004027 * Return the "part" to write to for "channel".
4028 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004029 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004030channel_part_send(channel_T *channel)
4031{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004032 if (channel->CH_SOCK_FD == INVALID_FD)
4033 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004034 return PART_SOCK;
4035}
4036
4037/*
4038 * Return the default "part" to read from for "channel".
4039 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004040 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004041channel_part_read(channel_T *channel)
4042{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004043 if (channel->CH_SOCK_FD == INVALID_FD)
4044 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004045 return PART_SOCK;
4046}
4047
4048/*
4049 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004050 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004051 */
4052 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004053channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004054{
Bram Moolenaar77073442016-02-13 23:23:53 +01004055 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004056 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004057 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004058}
4059
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004060/*
4061 * Return the timeout of "channel"/"part"
4062 */
4063 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004064channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004065{
4066 return channel->ch_part[part].ch_timeout;
4067}
4068
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004069 static int
4070handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4071{
4072 char_u *val = get_tv_string(item);
4073
4074 opt->jo_set |= jo;
4075 if (STRCMP(val, "nl") == 0)
4076 *modep = MODE_NL;
4077 else if (STRCMP(val, "raw") == 0)
4078 *modep = MODE_RAW;
4079 else if (STRCMP(val, "js") == 0)
4080 *modep = MODE_JS;
4081 else if (STRCMP(val, "json") == 0)
4082 *modep = MODE_JSON;
4083 else
4084 {
4085 EMSG2(_(e_invarg2), val);
4086 return FAIL;
4087 }
4088 return OK;
4089}
4090
4091 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004092handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004093{
4094 char_u *val = get_tv_string(item);
4095
4096 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4097 if (STRCMP(val, "null") == 0)
4098 opt->jo_io[part] = JIO_NULL;
4099 else if (STRCMP(val, "pipe") == 0)
4100 opt->jo_io[part] = JIO_PIPE;
4101 else if (STRCMP(val, "file") == 0)
4102 opt->jo_io[part] = JIO_FILE;
4103 else if (STRCMP(val, "buffer") == 0)
4104 opt->jo_io[part] = JIO_BUFFER;
4105 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4106 opt->jo_io[part] = JIO_OUT;
4107 else
4108 {
4109 EMSG2(_(e_invarg2), val);
4110 return FAIL;
4111 }
4112 return OK;
4113}
4114
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004115/*
4116 * Clear a jobopt_T before using it.
4117 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004118 void
4119clear_job_options(jobopt_T *opt)
4120{
4121 vim_memset(opt, 0, sizeof(jobopt_T));
4122}
4123
4124/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004125 * Free any members of a jobopt_T.
4126 */
4127 void
4128free_job_options(jobopt_T *opt)
4129{
4130 if (opt->jo_partial != NULL)
4131 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004132 else if (opt->jo_callback != NULL)
4133 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004134 if (opt->jo_out_partial != NULL)
4135 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004136 else if (opt->jo_out_cb != NULL)
4137 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004138 if (opt->jo_err_partial != NULL)
4139 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004140 else if (opt->jo_err_cb != NULL)
4141 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004142 if (opt->jo_close_partial != NULL)
4143 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144 else if (opt->jo_close_cb != NULL)
4145 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004146 if (opt->jo_exit_partial != NULL)
4147 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004148 else if (opt->jo_exit_cb != NULL)
4149 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004150}
4151
4152/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004153 * Get the PART_ number from the first character of an option name.
4154 */
4155 static int
4156part_from_char(int c)
4157{
4158 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4159}
4160
4161/*
4162 * Get the option entries from the dict in "tv", parse them and put the result
4163 * in "opt".
4164 * Only accept options in "supported".
4165 * If an option value is invalid return FAIL.
4166 */
4167 int
4168get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4169{
4170 typval_T *item;
4171 char_u *val;
4172 dict_T *dict;
4173 int todo;
4174 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004175 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004176
4177 opt->jo_set = 0;
4178 if (tv->v_type == VAR_UNKNOWN)
4179 return OK;
4180 if (tv->v_type != VAR_DICT)
4181 {
4182 EMSG(_(e_invarg));
4183 return FAIL;
4184 }
4185 dict = tv->vval.v_dict;
4186 if (dict == NULL)
4187 return OK;
4188
4189 todo = (int)dict->dv_hashtab.ht_used;
4190 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4191 if (!HASHITEM_EMPTY(hi))
4192 {
4193 item = &dict_lookup(hi)->di_tv;
4194
4195 if (STRCMP(hi->hi_key, "mode") == 0)
4196 {
4197 if (!(supported & JO_MODE))
4198 break;
4199 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4200 return FAIL;
4201 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004202 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004203 {
4204 if (!(supported & JO_IN_MODE))
4205 break;
4206 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4207 == FAIL)
4208 return FAIL;
4209 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004210 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004211 {
4212 if (!(supported & JO_OUT_MODE))
4213 break;
4214 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4215 == FAIL)
4216 return FAIL;
4217 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004218 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004219 {
4220 if (!(supported & JO_ERR_MODE))
4221 break;
4222 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4223 == FAIL)
4224 return FAIL;
4225 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004226 else if (STRCMP(hi->hi_key, "in_io") == 0
4227 || STRCMP(hi->hi_key, "out_io") == 0
4228 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004229 {
4230 if (!(supported & JO_OUT_IO))
4231 break;
4232 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4233 return FAIL;
4234 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004235 else if (STRCMP(hi->hi_key, "in_name") == 0
4236 || STRCMP(hi->hi_key, "out_name") == 0
4237 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004238 {
4239 part = part_from_char(*hi->hi_key);
4240
4241 if (!(supported & JO_OUT_IO))
4242 break;
4243 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4244 opt->jo_io_name[part] =
4245 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4246 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004247 else if (STRCMP(hi->hi_key, "in_buf") == 0
4248 || STRCMP(hi->hi_key, "out_buf") == 0
4249 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004250 {
4251 part = part_from_char(*hi->hi_key);
4252
4253 if (!(supported & JO_OUT_IO))
4254 break;
4255 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4256 opt->jo_io_buf[part] = get_tv_number(item);
4257 if (opt->jo_io_buf[part] <= 0)
4258 {
4259 EMSG2(_(e_invarg2), get_tv_string(item));
4260 return FAIL;
4261 }
4262 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4263 {
4264 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4265 return FAIL;
4266 }
4267 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004268 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4269 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4270 {
4271 part = part_from_char(*hi->hi_key);
4272
4273 if (!(supported & JO_OUT_IO))
4274 break;
4275 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4276 opt->jo_modifiable[part] = get_tv_number(item);
4277 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004278 else if (STRCMP(hi->hi_key, "out_msg") == 0
4279 || STRCMP(hi->hi_key, "err_msg") == 0)
4280 {
4281 part = part_from_char(*hi->hi_key);
4282
4283 if (!(supported & JO_OUT_IO))
4284 break;
4285 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4286 opt->jo_message[part] = get_tv_number(item);
4287 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004288 else if (STRCMP(hi->hi_key, "in_top") == 0
4289 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004290 {
4291 linenr_T *lp;
4292
4293 if (!(supported & JO_OUT_IO))
4294 break;
4295 if (hi->hi_key[3] == 't')
4296 {
4297 lp = &opt->jo_in_top;
4298 opt->jo_set |= JO_IN_TOP;
4299 }
4300 else
4301 {
4302 lp = &opt->jo_in_bot;
4303 opt->jo_set |= JO_IN_BOT;
4304 }
4305 *lp = get_tv_number(item);
4306 if (*lp < 0)
4307 {
4308 EMSG2(_(e_invarg2), get_tv_string(item));
4309 return FAIL;
4310 }
4311 }
4312 else if (STRCMP(hi->hi_key, "channel") == 0)
4313 {
4314 if (!(supported & JO_OUT_IO))
4315 break;
4316 opt->jo_set |= JO_CHANNEL;
4317 if (item->v_type != VAR_CHANNEL)
4318 {
4319 EMSG2(_(e_invarg2), "channel");
4320 return FAIL;
4321 }
4322 opt->jo_channel = item->vval.v_channel;
4323 }
4324 else if (STRCMP(hi->hi_key, "callback") == 0)
4325 {
4326 if (!(supported & JO_CALLBACK))
4327 break;
4328 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004329 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004330 if (opt->jo_callback == NULL)
4331 {
4332 EMSG2(_(e_invarg2), "callback");
4333 return FAIL;
4334 }
4335 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004336 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004337 {
4338 if (!(supported & JO_OUT_CALLBACK))
4339 break;
4340 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004341 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004342 if (opt->jo_out_cb == NULL)
4343 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004344 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004345 return FAIL;
4346 }
4347 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004348 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004349 {
4350 if (!(supported & JO_ERR_CALLBACK))
4351 break;
4352 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004353 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004354 if (opt->jo_err_cb == NULL)
4355 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004356 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004357 return FAIL;
4358 }
4359 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004360 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004361 {
4362 if (!(supported & JO_CLOSE_CALLBACK))
4363 break;
4364 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004365 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004366 if (opt->jo_close_cb == NULL)
4367 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004368 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004369 return FAIL;
4370 }
4371 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004372 else if (STRCMP(hi->hi_key, "drop") == 0)
4373 {
4374 int never = FALSE;
4375 val = get_tv_string(item);
4376
4377 if (STRCMP(val, "never") == 0)
4378 never = TRUE;
4379 else if (STRCMP(val, "auto") != 0)
4380 {
4381 EMSG2(_(e_invarg2), "drop");
4382 return FAIL;
4383 }
4384 opt->jo_drop_never = never;
4385 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004386 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4387 {
4388 if (!(supported & JO_EXIT_CB))
4389 break;
4390 opt->jo_set |= JO_EXIT_CB;
4391 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4392 if (opt->jo_exit_cb == NULL)
4393 {
4394 EMSG2(_(e_invarg2), "exit_cb");
4395 return FAIL;
4396 }
4397 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004398 else if (STRCMP(hi->hi_key, "waittime") == 0)
4399 {
4400 if (!(supported & JO_WAITTIME))
4401 break;
4402 opt->jo_set |= JO_WAITTIME;
4403 opt->jo_waittime = get_tv_number(item);
4404 }
4405 else if (STRCMP(hi->hi_key, "timeout") == 0)
4406 {
4407 if (!(supported & JO_TIMEOUT))
4408 break;
4409 opt->jo_set |= JO_TIMEOUT;
4410 opt->jo_timeout = get_tv_number(item);
4411 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004412 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004413 {
4414 if (!(supported & JO_OUT_TIMEOUT))
4415 break;
4416 opt->jo_set |= JO_OUT_TIMEOUT;
4417 opt->jo_out_timeout = get_tv_number(item);
4418 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004419 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004420 {
4421 if (!(supported & JO_ERR_TIMEOUT))
4422 break;
4423 opt->jo_set |= JO_ERR_TIMEOUT;
4424 opt->jo_err_timeout = get_tv_number(item);
4425 }
4426 else if (STRCMP(hi->hi_key, "part") == 0)
4427 {
4428 if (!(supported & JO_PART))
4429 break;
4430 opt->jo_set |= JO_PART;
4431 val = get_tv_string(item);
4432 if (STRCMP(val, "err") == 0)
4433 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004434 else if (STRCMP(val, "out") == 0)
4435 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004436 else
4437 {
4438 EMSG2(_(e_invarg2), val);
4439 return FAIL;
4440 }
4441 }
4442 else if (STRCMP(hi->hi_key, "id") == 0)
4443 {
4444 if (!(supported & JO_ID))
4445 break;
4446 opt->jo_set |= JO_ID;
4447 opt->jo_id = get_tv_number(item);
4448 }
4449 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4450 {
4451 if (!(supported & JO_STOPONEXIT))
4452 break;
4453 opt->jo_set |= JO_STOPONEXIT;
4454 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4455 opt->jo_soe_buf);
4456 if (opt->jo_stoponexit == NULL)
4457 {
4458 EMSG2(_(e_invarg2), "stoponexit");
4459 return FAIL;
4460 }
4461 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004462 else if (STRCMP(hi->hi_key, "block_write") == 0)
4463 {
4464 if (!(supported & JO_BLOCK_WRITE))
4465 break;
4466 opt->jo_set |= JO_BLOCK_WRITE;
4467 opt->jo_block_write = get_tv_number(item);
4468 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004469 else
4470 break;
4471 --todo;
4472 }
4473 if (todo > 0)
4474 {
4475 EMSG2(_(e_invarg2), hi->hi_key);
4476 return FAIL;
4477 }
4478
4479 return OK;
4480}
4481
4482/*
4483 * Get the channel from the argument.
4484 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004485 * When "check_open" is TRUE check that the channel can be used.
4486 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004487 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004488 */
4489 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004490get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004491{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004492 channel_T *channel = NULL;
4493 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004494
4495 if (tv->v_type == VAR_JOB)
4496 {
4497 if (tv->vval.v_job != NULL)
4498 channel = tv->vval.v_job->jv_channel;
4499 }
4500 else if (tv->v_type == VAR_CHANNEL)
4501 {
4502 channel = tv->vval.v_channel;
4503 }
4504 else
4505 {
4506 EMSG2(_(e_invarg2), get_tv_string(tv));
4507 return NULL;
4508 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004509 if (channel != NULL && reading)
4510 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004511 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004512
Bram Moolenaar437905c2016-04-26 19:01:05 +02004513 if (check_open && (channel == NULL || (!channel_is_open(channel)
4514 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004515 {
4516 EMSG(_("E906: not an open channel"));
4517 return NULL;
4518 }
4519 return channel;
4520}
4521
4522static job_T *first_job = NULL;
4523
4524 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004525job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004526{
4527 ch_log(job->jv_channel, "Freeing job");
4528 if (job->jv_channel != NULL)
4529 {
4530 /* The link from the channel to the job doesn't count as a reference,
4531 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004532 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004533 * NULL the reference. We don't set ch_job_killed, unreferencing the
4534 * job doesn't mean it stops running. */
4535 job->jv_channel->ch_job = NULL;
4536 channel_unref(job->jv_channel);
4537 }
4538 mch_clear_job(job);
4539
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004540 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004541 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004542}
4543
4544 static void
4545job_free_job(job_T *job)
4546{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004547 if (job->jv_next != NULL)
4548 job->jv_next->jv_prev = job->jv_prev;
4549 if (job->jv_prev == NULL)
4550 first_job = job->jv_next;
4551 else
4552 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004553 vim_free(job);
4554}
4555
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004556 static void
4557job_free(job_T *job)
4558{
4559 if (!in_free_unref_items)
4560 {
4561 job_free_contents(job);
4562 job_free_job(job);
4563 }
4564}
4565
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004566#if defined(EXITFREE) || defined(PROTO)
4567 void
4568job_free_all(void)
4569{
4570 while (first_job != NULL)
4571 job_free(first_job);
4572}
4573#endif
4574
4575/*
4576 * Return TRUE if we need to check if the process of "job" has ended.
4577 */
4578 static int
4579job_need_end_check(job_T *job)
4580{
4581 return job->jv_status == JOB_STARTED
4582 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4583}
4584
4585/*
4586 * Return TRUE if the channel of "job" is still useful.
4587 */
4588 static int
4589job_channel_still_useful(job_T *job)
4590{
4591 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4592}
4593
4594/*
4595 * Return TRUE if the job should not be freed yet. Do not free the job when
4596 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4597 * or when the associated channel will do something with the job output.
4598 */
4599 static int
4600job_still_useful(job_T *job)
4601{
4602 return job_need_end_check(job) || job_channel_still_useful(job);
4603}
4604
4605/*
4606 * NOTE: Must call job_cleanup() only once right after the status of "job"
4607 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4608 * mch_detect_ended_job() returned non-NULL).
4609 */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004610 static void
4611job_cleanup(job_T *job)
4612{
4613 if (job->jv_status != JOB_ENDED)
4614 return;
4615
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004616 /* Ready to cleanup the job. */
4617 job->jv_status = JOB_FINISHED;
4618
Bram Moolenaar97792de2016-10-15 18:36:49 +02004619 if (job->jv_exit_cb != NULL)
4620 {
4621 typval_T argv[3];
4622 typval_T rettv;
4623 int dummy;
4624
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004625 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004626 ++job->jv_refcount;
4627 argv[0].v_type = VAR_JOB;
4628 argv[0].vval.v_job = job;
4629 argv[1].v_type = VAR_NUMBER;
4630 argv[1].vval.v_number = job->jv_exitval;
4631 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4632 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4633 job->jv_exit_partial, NULL);
4634 clear_tv(&rettv);
4635 --job->jv_refcount;
4636 channel_need_redraw = TRUE;
4637 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004638
4639 /* Do not free the job in case the close callback of the associated channel
4640 * isn't invoked yet and may get information by job_info(). */
4641 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02004642 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004643 /* The job was already unreferenced and the associated channel was
4644 * detached, now that it ended it can be freed. Careful: caller must
4645 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004646 job_free(job);
4647 }
4648}
4649
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004650/*
4651 * Mark references in jobs that are still useful.
4652 */
4653 int
4654set_ref_in_job(int copyID)
4655{
4656 int abort = FALSE;
4657 job_T *job;
4658 typval_T tv;
4659
4660 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004661 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004662 {
4663 tv.v_type = VAR_JOB;
4664 tv.vval.v_job = job;
4665 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4666 }
4667 return abort;
4668}
4669
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004670/*
4671 * Dereference "job". Note that after this "job" may have been freed.
4672 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004673 void
4674job_unref(job_T *job)
4675{
4676 if (job != NULL && --job->jv_refcount <= 0)
4677 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004678 /* Do not free the job if there is a channel where the close callback
4679 * may get the job info. */
4680 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004681 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004682 /* Do not free the job when it has not ended yet and there is a
4683 * "stoponexit" flag or an exit callback. */
4684 if (!job_need_end_check(job))
4685 {
4686 job_free(job);
4687 }
4688 else if (job->jv_channel != NULL)
4689 {
4690 /* Do remove the link to the channel, otherwise it hangs
4691 * around until Vim exits. See job_free() for refcount. */
4692 ch_log(job->jv_channel, "detaching channel from job");
4693 job->jv_channel->ch_job = NULL;
4694 channel_unref(job->jv_channel);
4695 job->jv_channel = NULL;
4696 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004697 }
4698 }
4699}
4700
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004701 int
4702free_unused_jobs_contents(int copyID, int mask)
4703{
4704 int did_free = FALSE;
4705 job_T *job;
4706
4707 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004708 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004709 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004710 {
4711 /* Free the channel and ordinary items it contains, but don't
4712 * recurse into Lists, Dictionaries etc. */
4713 job_free_contents(job);
4714 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004715 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004716 return did_free;
4717}
4718
4719 void
4720free_unused_jobs(int copyID, int mask)
4721{
4722 job_T *job;
4723 job_T *job_next;
4724
4725 for (job = first_job; job != NULL; job = job_next)
4726 {
4727 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004728 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004729 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004730 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004731 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004732 job_free_job(job);
4733 }
4734 }
4735}
4736
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004737/*
4738 * Allocate a job. Sets the refcount to one and sets options default.
4739 */
4740 static job_T *
4741job_alloc(void)
4742{
4743 job_T *job;
4744
4745 job = (job_T *)alloc_clear(sizeof(job_T));
4746 if (job != NULL)
4747 {
4748 job->jv_refcount = 1;
4749 job->jv_stoponexit = vim_strsave((char_u *)"term");
4750
4751 if (first_job != NULL)
4752 {
4753 first_job->jv_prev = job;
4754 job->jv_next = first_job;
4755 }
4756 first_job = job;
4757 }
4758 return job;
4759}
4760
4761 void
4762job_set_options(job_T *job, jobopt_T *opt)
4763{
4764 if (opt->jo_set & JO_STOPONEXIT)
4765 {
4766 vim_free(job->jv_stoponexit);
4767 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4768 job->jv_stoponexit = NULL;
4769 else
4770 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4771 }
4772 if (opt->jo_set & JO_EXIT_CB)
4773 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004774 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004775 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004776 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004777 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004778 job->jv_exit_partial = NULL;
4779 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004780 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004781 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004782 job->jv_exit_partial = opt->jo_exit_partial;
4783 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004784 {
4785 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004786 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004787 }
4788 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004789 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004790 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004791 func_ref(job->jv_exit_cb);
4792 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004793 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004794 }
4795}
4796
4797/*
4798 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4799 */
4800 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004801job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004802{
4803 job_T *job;
4804
4805 for (job = first_job; job != NULL; job = job->jv_next)
4806 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4807 mch_stop_job(job, job->jv_stoponexit);
4808}
4809
4810/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004811 * Return TRUE when there is any job that has an exit callback and might exit,
4812 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004813 */
4814 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004815has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004816{
4817 job_T *job;
4818
4819 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004820 /* Only should check if the channel has been closed, if the channel is
4821 * open the job won't exit. */
4822 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004823 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004824 return TRUE;
4825 return FALSE;
4826}
4827
Bram Moolenaar97792de2016-10-15 18:36:49 +02004828#define MAX_CHECK_ENDED 8
4829
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004830/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004831 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004832 */
4833 void
4834job_check_ended(void)
4835{
Bram Moolenaar97792de2016-10-15 18:36:49 +02004836 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004837
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004838 if (first_job == NULL)
4839 return;
4840
Bram Moolenaar97792de2016-10-15 18:36:49 +02004841 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004842 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004843 /* NOTE: mch_detect_ended_job() must only return a job of which the
4844 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004845 job_T *job = mch_detect_ended_job(first_job);
4846
4847 if (job == NULL)
4848 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004849 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004850 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02004851
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004852 if (channel_need_redraw)
4853 {
4854 channel_need_redraw = FALSE;
4855 redraw_after_callback();
4856 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004857}
4858
4859/*
4860 * "job_start()" function
4861 */
4862 job_T *
4863job_start(typval_T *argvars)
4864{
4865 job_T *job;
4866 char_u *cmd = NULL;
4867#if defined(UNIX)
4868# define USE_ARGV
4869 char **argv = NULL;
4870 int argc = 0;
4871#else
4872 garray_T ga;
4873#endif
4874 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004875 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004876
4877 job = job_alloc();
4878 if (job == NULL)
4879 return NULL;
4880
4881 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004882#ifndef USE_ARGV
4883 ga_init2(&ga, (int)sizeof(char*), 20);
4884#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004885
4886 /* Default mode is NL. */
4887 clear_job_options(&opt);
4888 opt.jo_mode = MODE_NL;
4889 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004890 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4891 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004892 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004893
4894 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004895 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004896 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4897 && opt.jo_io[part] == JIO_FILE
4898 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4899 || *opt.jo_io_name[part] == NUL))
4900 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004901 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004902 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004903 }
4904
4905 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4906 {
4907 buf_T *buf = NULL;
4908
4909 /* check that we can find the buffer before starting the job */
4910 if (opt.jo_set & JO_IN_BUF)
4911 {
4912 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4913 if (buf == NULL)
4914 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4915 }
4916 else if (!(opt.jo_set & JO_IN_NAME))
4917 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004918 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004919 }
4920 else
4921 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4922 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004923 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004924 if (buf->b_ml.ml_mfp == NULL)
4925 {
4926 char_u numbuf[NUMBUFLEN];
4927 char_u *s;
4928
4929 if (opt.jo_set & JO_IN_BUF)
4930 {
4931 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4932 s = numbuf;
4933 }
4934 else
4935 s = opt.jo_io_name[PART_IN];
4936 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004937 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004938 }
4939 job->jv_in_buf = buf;
4940 }
4941
4942 job_set_options(job, &opt);
4943
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004944 if (argvars[0].v_type == VAR_STRING)
4945 {
4946 /* Command is a string. */
4947 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004948 if (cmd == NULL || *cmd == NUL)
4949 {
4950 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004951 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004952 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004953#ifdef USE_ARGV
4954 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004955 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004956 argv[argc] = NULL;
4957#endif
4958 }
4959 else if (argvars[0].v_type != VAR_LIST
4960 || argvars[0].vval.v_list == NULL
4961 || argvars[0].vval.v_list->lv_len < 1)
4962 {
4963 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004964 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004965 }
4966 else
4967 {
4968 list_T *l = argvars[0].vval.v_list;
4969 listitem_T *li;
4970 char_u *s;
4971
4972#ifdef USE_ARGV
4973 /* Pass argv[] to mch_call_shell(). */
4974 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4975 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004976 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004977#endif
4978 for (li = l->lv_first; li != NULL; li = li->li_next)
4979 {
4980 s = get_tv_string_chk(&li->li_tv);
4981 if (s == NULL)
4982 goto theend;
4983#ifdef USE_ARGV
4984 argv[argc++] = (char *)s;
4985#else
4986 /* Only escape when needed, double quotes are not always allowed. */
4987 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4988 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004989# ifdef WIN32
4990 int old_ssl = p_ssl;
4991
4992 /* This is using CreateProcess, not cmd.exe. Always use
4993 * double quote and backslashes. */
4994 p_ssl = 0;
4995# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004996 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004997# ifdef WIN32
4998 p_ssl = old_ssl;
4999# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005000 if (s == NULL)
5001 goto theend;
5002 ga_concat(&ga, s);
5003 vim_free(s);
5004 }
5005 else
5006 ga_concat(&ga, s);
5007 if (li->li_next != NULL)
5008 ga_append(&ga, ' ');
5009#endif
5010 }
5011#ifdef USE_ARGV
5012 argv[argc] = NULL;
5013#else
5014 cmd = ga.ga_data;
5015#endif
5016 }
5017
5018#ifdef USE_ARGV
5019 if (ch_log_active())
5020 {
5021 garray_T ga;
5022 int i;
5023
5024 ga_init2(&ga, (int)sizeof(char), 200);
5025 for (i = 0; i < argc; ++i)
5026 {
5027 if (i > 0)
5028 ga_concat(&ga, (char_u *)" ");
5029 ga_concat(&ga, (char_u *)argv[i]);
5030 }
5031 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
5032 ga_clear(&ga);
5033 }
5034 mch_start_job(argv, job, &opt);
5035#else
5036 ch_logs(NULL, "Starting job: %s", (char *)cmd);
5037 mch_start_job((char *)cmd, job, &opt);
5038#endif
5039
5040 /* If the channel is reading from a buffer, write lines now. */
5041 if (job->jv_channel != NULL)
5042 channel_write_in(job->jv_channel);
5043
5044theend:
5045#ifdef USE_ARGV
5046 vim_free(argv);
5047#else
5048 vim_free(ga.ga_data);
5049#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005050 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005051 return job;
5052}
5053
5054/*
5055 * Get the status of "job" and invoke the exit callback when needed.
5056 * The returned string is not allocated.
5057 */
5058 char *
5059job_status(job_T *job)
5060{
5061 char *result;
5062
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005063 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005064 /* No need to check, dead is dead. */
5065 result = "dead";
5066 else if (job->jv_status == JOB_FAILED)
5067 result = "fail";
5068 else
5069 {
5070 result = mch_job_status(job);
5071 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005072 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005073 }
5074 return result;
5075}
5076
Bram Moolenaar8950a562016-03-12 15:22:55 +01005077/*
5078 * Implementation of job_info().
5079 */
5080 void
5081job_info(job_T *job, dict_T *dict)
5082{
5083 dictitem_T *item;
5084 varnumber_T nr;
5085
5086 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5087
5088 item = dictitem_alloc((char_u *)"channel");
5089 if (item == NULL)
5090 return;
5091 item->di_tv.v_lock = 0;
5092 item->di_tv.v_type = VAR_CHANNEL;
5093 item->di_tv.vval.v_channel = job->jv_channel;
5094 if (job->jv_channel != NULL)
5095 ++job->jv_channel->ch_refcount;
5096 if (dict_add(dict, item) == FAIL)
5097 dictitem_free(item);
5098
5099#ifdef UNIX
5100 nr = job->jv_pid;
5101#else
5102 nr = job->jv_proc_info.dwProcessId;
5103#endif
5104 dict_add_nr_str(dict, "process", nr, NULL);
5105
5106 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005107 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005108 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5109}
5110
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005111 int
5112job_stop(job_T *job, typval_T *argvars)
5113{
5114 char_u *arg;
5115
5116 if (argvars[1].v_type == VAR_UNKNOWN)
5117 arg = (char_u *)"";
5118 else
5119 {
5120 arg = get_tv_string_chk(&argvars[1]);
5121 if (arg == NULL)
5122 {
5123 EMSG(_(e_invarg));
5124 return 0;
5125 }
5126 }
5127 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
5128 if (mch_stop_job(job, arg) == FAIL)
5129 return 0;
5130
5131 /* Assume that "hup" does not kill the job. */
5132 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
5133 job->jv_channel->ch_job_killed = TRUE;
5134
5135 /* We don't try freeing the job, obviously the caller still has a
5136 * reference to it. */
5137 return 1;
5138}
5139
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005140#endif /* FEAT_JOB_CHANNEL */