blob: bd31bf3cbad55d61026bc341f6d4eef9b0ce1024 [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 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100713 {
714 char *p;
715
716 /* When using host->h_addr directly ubsan warns for it to not be
717 * aligned. First copy the pointer to aviod that. */
718 memcpy(&p, &host->h_addr, sizeof(p));
719 memcpy((char *)&server.sin_addr, p, host->h_length);
720 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100721
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100722 /* On Mac and Solaris a zero timeout almost never works. At least wait
723 * one millisecond. Let's do it for all systems, because we don't know why
724 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100725 if (waittime == 0)
726 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100727
728 /*
729 * For Unix we need to call connect() again after connect() failed.
730 * On Win32 one time is sufficient.
731 */
732 while (TRUE)
733 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100734 long elapsed_msec = 0;
735 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100736
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100737 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100738 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100739 sd = socket(AF_INET, SOCK_STREAM, 0);
740 if (sd == -1)
741 {
742 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200743 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100744 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100745 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100746 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100747
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100748 if (waittime >= 0)
749 {
750 /* Make connect() non-blocking. */
751 if (
752#ifdef _WIN32
753 ioctlsocket(sd, FIONBIO, &val) < 0
754#else
755 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
756#endif
757 )
758 {
759 SOCK_ERRNO;
760 ch_errorn(channel,
761 "channel_open: Connect failed with errno %d", errno);
762 sock_close(sd);
763 channel_free(channel);
764 return NULL;
765 }
766 }
767
768 /* Try connecting to the server. */
769 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
770 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
771
Bram Moolenaar045a2842016-03-08 22:33:07 +0100772 if (ret == 0)
773 /* The connection could be established. */
774 break;
775
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100776 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100777 if (waittime < 0 || (errno != EWOULDBLOCK
778 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100779#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100780 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100781#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100782 ))
783 {
784 ch_errorn(channel,
785 "channel_open: Connect failed with errno %d", errno);
786 PERROR(_(e_cannot_connect));
787 sock_close(sd);
788 channel_free(channel);
789 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100790 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100791
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100792 /* Limit the waittime to 50 msec. If it doesn't work within this
793 * time we close the socket and try creating it again. */
794 waitnow = waittime > 50 ? 50 : waittime;
795
Bram Moolenaar045a2842016-03-08 22:33:07 +0100796 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100797 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100798#ifndef WIN32
799 if (errno != ECONNREFUSED)
800#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100801 {
802 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100803 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100804 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100805#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 int so_error = 0;
807 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 struct timeval start_tv;
809 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100810#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100811 FD_ZERO(&rfds);
812 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100813 FD_ZERO(&wfds);
814 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100815
Bram Moolenaar562ca712016-03-09 21:50:05 +0100816 tv.tv_sec = waitnow / 1000;
817 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100818#ifndef WIN32
819 gettimeofday(&start_tv, NULL);
820#endif
821 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100822 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100823 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100824
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100825 if (ret < 0)
826 {
827 SOCK_ERRNO;
828 ch_errorn(channel,
829 "channel_open: Connect failed with errno %d", errno);
830 PERROR(_(e_cannot_connect));
831 sock_close(sd);
832 channel_free(channel);
833 return NULL;
834 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100835
Bram Moolenaare081e212016-02-28 22:33:46 +0100836#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100837 /* On Win32: select() is expected to work and wait for up to
838 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100839 if (FD_ISSET(sd, &wfds))
840 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100841 elapsed_msec = waitnow;
842 if (waittime > 1 && elapsed_msec < waittime)
843 {
844 waittime -= elapsed_msec;
845 continue;
846 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100847#else
848 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100849 * After putting the socket in non-blocking mode, connect() will
850 * return EINPROGRESS, select() will not wait (as if writing is
851 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100852 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100853 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100854 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100855 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100856 {
857 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100858 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100859 if (ret < 0 || (so_error != 0
860 && so_error != EWOULDBLOCK
861 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100862# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100863 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100864# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100865 ))
866 {
867 ch_errorn(channel,
868 "channel_open: Connect failed with errno %d",
869 so_error);
870 PERROR(_(e_cannot_connect));
871 sock_close(sd);
872 channel_free(channel);
873 return NULL;
874 }
875 }
876
Bram Moolenaar045a2842016-03-08 22:33:07 +0100877 if (FD_ISSET(sd, &wfds) && so_error == 0)
878 /* Did not detect an error, connection is established. */
879 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100880
Bram Moolenaar045a2842016-03-08 22:33:07 +0100881 gettimeofday(&end_tv, NULL);
882 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
883 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100884#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100885 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100886
887#ifndef WIN32
888 if (waittime > 1 && elapsed_msec < waittime)
889 {
890 /* The port isn't ready but we also didn't get an error.
891 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100892 * yet. Select() may return early, wait until the remaining
893 * "waitnow" and try again. */
894 waitnow -= elapsed_msec;
895 waittime -= elapsed_msec;
896 if (waitnow > 0)
897 {
898 mch_delay((long)waitnow, TRUE);
899 ui_breakcheck();
900 waittime -= waitnow;
901 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100902 if (!got_int)
903 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100904 if (waittime <= 0)
905 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100906 waittime = 1;
907 continue;
908 }
909 /* we were interrupted, behave as if timed out */
910 }
911#endif
912
913 /* We timed out. */
914 ch_error(channel, "Connection timed out");
915 sock_close(sd);
916 channel_free(channel);
917 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100918 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100919
Bram Moolenaar045a2842016-03-08 22:33:07 +0100920 ch_log(channel, "Connection made");
921
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100922 if (waittime >= 0)
923 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100924#ifdef _WIN32
925 val = 0;
926 ioctlsocket(sd, FIONBIO, &val);
927#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100928 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100929#endif
930 }
931
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100932 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100933 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100934 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
935 channel->ch_port = port_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200936 channel->ch_to_be_closed |= (1 << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100937
938#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100939 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100940#endif
941
Bram Moolenaar77073442016-02-13 23:23:53 +0100942 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100943}
944
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100945/*
946 * Implements ch_open().
947 */
948 channel_T *
949channel_open_func(typval_T *argvars)
950{
951 char_u *address;
952 char_u *p;
953 char *rest;
954 int port;
955 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200956 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100957
958 address = get_tv_string(&argvars[0]);
959 if (argvars[1].v_type != VAR_UNKNOWN
960 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
961 {
962 EMSG(_(e_invarg));
963 return NULL;
964 }
965
966 /* parse address */
967 p = vim_strchr(address, ':');
968 if (p == NULL)
969 {
970 EMSG2(_(e_invarg2), address);
971 return NULL;
972 }
973 *p++ = NUL;
974 port = strtol((char *)p, &rest, 10);
975 if (*address == NUL || port <= 0 || *rest != NUL)
976 {
977 p[-1] = ':';
978 EMSG2(_(e_invarg2), address);
979 return NULL;
980 }
981
982 /* parse options */
983 clear_job_options(&opt);
984 opt.jo_mode = MODE_JSON;
985 opt.jo_timeout = 2000;
986 if (get_job_options(&argvars[1], &opt,
987 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200988 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100989 if (opt.jo_timeout < 0)
990 {
991 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200992 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100993 }
994
995 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
996 if (channel != NULL)
997 {
998 opt.jo_set = JO_ALL;
999 channel_set_options(channel, &opt);
1000 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001001theend:
1002 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001003 return channel;
1004}
1005
Bram Moolenaarde279892016-03-11 22:19:44 +01001006 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001007ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001008{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001009 sock_T *fd = &channel->ch_part[part].ch_fd;
1010
Bram Moolenaarde279892016-03-11 22:19:44 +01001011 if (*fd != INVALID_FD)
1012 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001013 if (part == PART_SOCK)
1014 sock_close(*fd);
1015 else
1016 fd_close(*fd);
Bram Moolenaarde279892016-03-11 22:19:44 +01001017 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001018
1019 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001020 }
1021}
1022
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001023 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001024channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001025{
Bram Moolenaarde279892016-03-11 22:19:44 +01001026 if (in != INVALID_FD)
1027 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001028 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001029 channel->CH_IN_FD = in;
1030 }
1031 if (out != INVALID_FD)
1032 {
1033# if defined(FEAT_GUI)
1034 channel_gui_unregister_one(channel, PART_OUT);
1035# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001036 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001037 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001038 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001039# if defined(FEAT_GUI)
1040 channel_gui_register_one(channel, PART_OUT);
1041# endif
1042 }
1043 if (err != INVALID_FD)
1044 {
1045# if defined(FEAT_GUI)
1046 channel_gui_unregister_one(channel, PART_ERR);
1047# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001048 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001049 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001050 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001051# if defined(FEAT_GUI)
1052 channel_gui_register_one(channel, PART_ERR);
1053# endif
1054 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001055}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001056
Bram Moolenaard6051b52016-02-28 15:49:03 +01001057/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001058 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001059 * This does not keep a refcount, when the job is freed ch_job is cleared.
1060 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001061 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001062channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001063{
Bram Moolenaar77073442016-02-13 23:23:53 +01001064 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001065
1066 channel_set_options(channel, options);
1067
1068 if (job->jv_in_buf != NULL)
1069 {
1070 chanpart_T *in_part = &channel->ch_part[PART_IN];
1071
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001072 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001073 ch_logs(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001074 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001075 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001076 {
1077 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1078 {
1079 /* Special mode: send last-but-one line when appending a line
1080 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001081 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001082 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001083 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001084 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001085 }
1086 else
1087 in_part->ch_buf_top = options->jo_in_top;
1088 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001089 else
1090 in_part->ch_buf_top = 1;
1091 if (options->jo_set & JO_IN_BOT)
1092 in_part->ch_buf_bot = options->jo_in_bot;
1093 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001094 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001095 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001096}
1097
1098/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001099 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001100 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001101 */
1102 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001103find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001104{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001105 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001106 buf_T *save_curbuf = curbuf;
1107
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001108 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001109 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001110 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001111 if (buf == NULL)
1112 buf = buflist_findname_exp(name);
1113 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001114 if (buf == NULL)
1115 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001116 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001117 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001118 if (buf == NULL)
1119 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001120 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001121 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001122#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001123 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1124 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001125#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001126 if (curbuf->b_ml.ml_mfp == NULL)
1127 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001128 if (msg)
1129 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001130 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001131 changed_bytes(1, 0);
1132 curbuf = save_curbuf;
1133 }
1134
1135 return buf;
1136}
1137
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001138 static void
1139set_callback(
1140 char_u **cbp,
1141 partial_T **pp,
1142 char_u *callback,
1143 partial_T *partial)
1144{
1145 free_callback(*cbp, *pp);
1146 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001147 {
1148 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001149 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001150 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001151 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001152 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001153 func_ref(*cbp);
1154 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001155 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001156 else
1157 *cbp = NULL;
1158 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001159 if (partial != NULL)
1160 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001161}
1162
Bram Moolenaar187db502016-02-27 14:44:26 +01001163/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001164 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001165 */
1166 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001167channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001168{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001169 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001170
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001171 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001172 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001173 channel->ch_part[part].ch_mode = opt->jo_mode;
1174 if (opt->jo_set & JO_IN_MODE)
1175 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1176 if (opt->jo_set & JO_OUT_MODE)
1177 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1178 if (opt->jo_set & JO_ERR_MODE)
1179 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001180
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001181 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001182 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001183 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1184 if (opt->jo_set & JO_OUT_TIMEOUT)
1185 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1186 if (opt->jo_set & JO_ERR_TIMEOUT)
1187 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001188 if (opt->jo_set & JO_BLOCK_WRITE)
1189 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001190
1191 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001192 set_callback(&channel->ch_callback, &channel->ch_partial,
1193 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001194 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001195 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1196 &channel->ch_part[PART_OUT].ch_partial,
1197 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001198 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001199 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1200 &channel->ch_part[PART_ERR].ch_partial,
1201 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001202 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001203 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1204 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001205 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001206
1207 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1208 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001209 buf_T *buf;
1210
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001211 /* writing output to a buffer. Default mode is NL. */
1212 if (!(opt->jo_set & JO_OUT_MODE))
1213 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001214 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001215 {
1216 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1217 if (buf == NULL)
1218 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1219 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001220 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001221 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001222 int msg = TRUE;
1223
1224 if (opt->jo_set2 & JO2_OUT_MSG)
1225 msg = opt->jo_message[PART_OUT];
1226 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001227 }
1228 if (buf != NULL)
1229 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001230 if (opt->jo_set & JO_OUT_MODIFIABLE)
1231 channel->ch_part[PART_OUT].ch_nomodifiable =
1232 !opt->jo_modifiable[PART_OUT];
1233
1234 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1235 {
1236 EMSG(_(e_modifiable));
1237 }
1238 else
1239 {
1240 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001241 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001242 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001243 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001244 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001245 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001246
1247 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1248 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1249 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1250 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001251 buf_T *buf;
1252
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001253 /* writing err to a buffer. Default mode is NL. */
1254 if (!(opt->jo_set & JO_ERR_MODE))
1255 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1256 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001257 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001258 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001259 {
1260 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1261 if (buf == NULL)
1262 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1263 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001264 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001265 {
1266 int msg = TRUE;
1267
1268 if (opt->jo_set2 & JO2_ERR_MSG)
1269 msg = opt->jo_message[PART_ERR];
1270 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1271 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001272 if (buf != NULL)
1273 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001274 if (opt->jo_set & JO_ERR_MODIFIABLE)
1275 channel->ch_part[PART_ERR].ch_nomodifiable =
1276 !opt->jo_modifiable[PART_ERR];
1277 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1278 {
1279 EMSG(_(e_modifiable));
1280 }
1281 else
1282 {
1283 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001284 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001285 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001286 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001287 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001288 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001289
1290 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1291 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1292 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001293}
1294
1295/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001296 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001297 */
1298 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001299channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001300 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001301 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001302 char_u *callback,
1303 partial_T *partial,
1304 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001305{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001306 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001307 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1308
1309 if (item != NULL)
1310 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001311 item->cq_partial = partial;
1312 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001313 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001314 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001315 item->cq_callback = callback;
1316 }
1317 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001318 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001319 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001320 func_ref(item->cq_callback);
1321 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001322 item->cq_seq_nr = id;
1323 item->cq_prev = head->cq_prev;
1324 head->cq_prev = item;
1325 item->cq_next = NULL;
1326 if (item->cq_prev == NULL)
1327 head->cq_next = item;
1328 else
1329 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001330 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001331}
1332
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001333 static void
1334write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1335{
1336 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001337 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001338 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001339 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001340
Bram Moolenaar655da312016-05-28 22:22:34 +02001341 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001342 if ((p = alloc(len + 2)) == NULL)
1343 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001344 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001345
1346 for (i = 0; i < len; ++i)
1347 if (p[i] == NL)
1348 p[i] = NUL;
1349
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001350 p[len] = NL;
1351 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001352 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001353 vim_free(p);
1354}
1355
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001356/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001357 * Return TRUE if "channel" can be written to.
1358 * Returns FALSE if the input is closed or the write would block.
1359 */
1360 static int
1361can_write_buf_line(channel_T *channel)
1362{
1363 chanpart_T *in_part = &channel->ch_part[PART_IN];
1364
1365 if (in_part->ch_fd == INVALID_FD)
1366 return FALSE; /* pipe was closed */
1367
1368 /* for testing: block every other attempt to write */
1369 if (in_part->ch_block_write == 1)
1370 in_part->ch_block_write = -1;
1371 else if (in_part->ch_block_write == -1)
1372 in_part->ch_block_write = 1;
1373
1374 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1375#ifndef WIN32
1376 {
1377# if defined(HAVE_SELECT)
1378 struct timeval tval;
1379 fd_set wfds;
1380 int ret;
1381
1382 FD_ZERO(&wfds);
1383 FD_SET((int)in_part->ch_fd, &wfds);
1384 tval.tv_sec = 0;
1385 tval.tv_usec = 0;
1386 for (;;)
1387 {
1388 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1389# ifdef EINTR
1390 SOCK_ERRNO;
1391 if (ret == -1 && errno == EINTR)
1392 continue;
1393# endif
1394 if (ret <= 0 || in_part->ch_block_write == 1)
1395 {
1396 if (ret > 0)
1397 ch_log(channel, "FAKED Input not ready for writing");
1398 else
1399 ch_log(channel, "Input not ready for writing");
1400 return FALSE;
1401 }
1402 break;
1403 }
1404# else
1405 struct pollfd fds;
1406
1407 fds.fd = in_part->ch_fd;
1408 fds.events = POLLOUT;
1409 if (poll(&fds, 1, 0) <= 0)
1410 {
1411 ch_log(channel, "Input not ready for writing");
1412 return FALSE;
1413 }
1414 if (in_part->ch_block_write == 1)
1415 {
1416 ch_log(channel, "FAKED Input not ready for writing");
1417 return FALSE;
1418 }
1419# endif
1420 }
1421#endif
1422 return TRUE;
1423}
1424
1425/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001426 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001427 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001428 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001429channel_write_in(channel_T *channel)
1430{
1431 chanpart_T *in_part = &channel->ch_part[PART_IN];
1432 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001433 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001434 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001435
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001436 if (buf == NULL || in_part->ch_buf_append)
1437 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001438 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001439 {
1440 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001441 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001442 return;
1443 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001444
1445 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1446 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1447 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001448 if (!can_write_buf_line(channel))
1449 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001450 write_buf_line(buf, lnum, channel);
1451 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001452 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001453
1454 if (written == 1)
1455 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1456 else if (written > 1)
1457 ch_logn(channel, "written %d lines to channel", written);
1458
Bram Moolenaar014069a2016-03-03 22:51:40 +01001459 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001460 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001461 {
1462 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001463 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001464 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001465
1466 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001467 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001468 }
1469 else
1470 ch_logn(channel, "Still %d more lines to write",
1471 buf->b_ml.ml_line_count - lnum + 1);
1472}
1473
1474/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001475 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001476 */
1477 void
1478channel_buffer_free(buf_T *buf)
1479{
1480 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001481 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001482
1483 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001484 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001485 {
1486 chanpart_T *ch_part = &channel->ch_part[part];
1487
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001488 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001489 {
1490 ch_logs(channel, "%s buffer has been wiped out",
1491 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001492 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001493 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001494 }
1495}
1496
1497/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001498 * Write any lines waiting to be written to a channel.
1499 */
1500 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001501channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001502{
1503 channel_T *channel;
1504
1505 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1506 {
1507 chanpart_T *in_part = &channel->ch_part[PART_IN];
1508
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001509 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001510 {
1511 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001512 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001513 else
1514 channel_write_in(channel);
1515 }
1516 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001517}
1518
1519/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001520 * Write appended lines above the last one in "buf" to the channel.
1521 */
1522 void
1523channel_write_new_lines(buf_T *buf)
1524{
1525 channel_T *channel;
1526 int found_one = FALSE;
1527
1528 /* There could be more than one channel for the buffer, loop over all of
1529 * them. */
1530 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1531 {
1532 chanpart_T *in_part = &channel->ch_part[PART_IN];
1533 linenr_T lnum;
1534 int written = 0;
1535
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001536 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001537 {
1538 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001539 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001540 found_one = TRUE;
1541 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1542 ++lnum)
1543 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001544 if (!can_write_buf_line(channel))
1545 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001546 write_buf_line(buf, lnum, channel);
1547 ++written;
1548 }
1549
1550 if (written == 1)
1551 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1552 else if (written > 1)
1553 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001554 if (lnum < buf->b_ml.ml_line_count)
1555 ch_logn(channel, "Still %d more lines to write",
1556 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001557
1558 in_part->ch_buf_bot = lnum;
1559 }
1560 }
1561 if (!found_one)
1562 buf->b_write_to_channel = FALSE;
1563}
1564
1565/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001566 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001567 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001568 */
1569 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001570invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1571 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001572{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001573 typval_T rettv;
1574 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001575
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001576 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001577 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001578
Bram Moolenaar77073442016-02-13 23:23:53 +01001579 argv[0].v_type = VAR_CHANNEL;
1580 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001581
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001582 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1583 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001584 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001585 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001586}
1587
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001588/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001589 * Return the first node from "channel"/"part" without removing it.
1590 * Returns NULL if there is nothing.
1591 */
1592 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001593channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001594{
1595 readq_T *head = &channel->ch_part[part].ch_head;
1596
1597 return head->rq_next;
1598}
1599
1600/*
1601 * Return a pointer to the first NL in "node".
1602 * Skips over NUL characters.
1603 * Returns NULL if there is no NL.
1604 */
1605 char_u *
1606channel_first_nl(readq_T *node)
1607{
1608 char_u *buffer = node->rq_buffer;
1609 long_u i;
1610
1611 for (i = 0; i < node->rq_buflen; ++i)
1612 if (buffer[i] == NL)
1613 return buffer + i;
1614 return NULL;
1615}
1616
1617/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001618 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001619 * The caller must free it.
1620 * Returns NULL if there is nothing.
1621 */
1622 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001623channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001624{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001625 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001626 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001627 char_u *p;
1628
Bram Moolenaar77073442016-02-13 23:23:53 +01001629 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001630 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001631 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001632 p = node->rq_buffer;
1633 head->rq_next = node->rq_next;
1634 if (node->rq_next == NULL)
1635 head->rq_prev = NULL;
1636 else
1637 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001638 vim_free(node);
1639 return p;
1640}
1641
1642/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001643 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001644 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001645 */
1646 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001647channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001648{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001649 readq_T *head = &channel->ch_part[part].ch_head;
1650 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001651 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001652 char_u *res;
1653 char_u *p;
1654
1655 /* If there is only one buffer just get that one. */
1656 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1657 return channel_get(channel, part);
1658
1659 /* Concatenate everything into one buffer. */
1660 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001661 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001662 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001663 if (res == NULL)
1664 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001665 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001666 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001667 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001668 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001669 p += node->rq_buflen;
1670 }
1671 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001672
1673 /* Free all buffers */
1674 do
1675 {
1676 p = channel_get(channel, part);
1677 vim_free(p);
1678 } while (p != NULL);
1679
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001680 /* turn all NUL into NL */
1681 while (len > 0)
1682 {
1683 --len;
1684 if (res[len] == NUL)
1685 res[len] = NL;
1686 }
1687
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001688 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001689}
1690
1691/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001692 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001693 * Caller must check these bytes are available.
1694 */
1695 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001696channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001697{
1698 readq_T *head = &channel->ch_part[part].ch_head;
1699 readq_T *node = head->rq_next;
1700 char_u *buf = node->rq_buffer;
1701
1702 mch_memmove(buf, buf + len, node->rq_buflen - len);
1703 node->rq_buflen -= len;
1704}
1705
1706/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001707 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001708 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001709 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001710 */
1711 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001712channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001713{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001714 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001715 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001716 readq_T *last_node;
1717 readq_T *n;
1718 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001719 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001720 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001721
Bram Moolenaar77073442016-02-13 23:23:53 +01001722 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001723 return FAIL;
1724
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001725 last_node = node->rq_next;
1726 len = node->rq_buflen + last_node->rq_buflen + 1;
1727 if (want_nl)
1728 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001729 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001730 {
1731 last_node = last_node->rq_next;
1732 len += last_node->rq_buflen;
1733 }
1734
1735 p = newbuf = alloc(len);
1736 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001737 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001738 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001739 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001740 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001741 node->rq_buffer = newbuf;
1742 for (n = node; n != last_node; )
1743 {
1744 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001745 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001746 p += n->rq_buflen;
1747 vim_free(n->rq_buffer);
1748 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001749 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001750
1751 /* dispose of the collapsed nodes and their buffers */
1752 for (n = node->rq_next; n != last_node; )
1753 {
1754 n = n->rq_next;
1755 vim_free(n->rq_prev);
1756 }
1757 node->rq_next = last_node->rq_next;
1758 if (last_node->rq_next == NULL)
1759 head->rq_prev = node;
1760 else
1761 last_node->rq_next->rq_prev = node;
1762 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001763 return OK;
1764}
1765
1766/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001767 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001768 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001769 * Returns OK or FAIL.
1770 */
1771 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001772channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001773 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001774{
1775 readq_T *node;
1776 readq_T *head = &channel->ch_part[part].ch_head;
1777 char_u *p;
1778 int i;
1779
1780 node = (readq_T *)alloc(sizeof(readq_T));
1781 if (node == NULL)
1782 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001783 /* A NUL is added at the end, because netbeans code expects that.
1784 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001785 node->rq_buffer = alloc(len + 1);
1786 if (node->rq_buffer == NULL)
1787 {
1788 vim_free(node);
1789 return FAIL; /* out of memory */
1790 }
1791
1792 if (channel->ch_part[part].ch_mode == MODE_NL)
1793 {
1794 /* Drop any CR before a NL. */
1795 p = node->rq_buffer;
1796 for (i = 0; i < len; ++i)
1797 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1798 *p++ = buf[i];
1799 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001800 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001801 }
1802 else
1803 {
1804 mch_memmove(node->rq_buffer, buf, len);
1805 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001806 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001807 }
1808
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001809 if (prepend)
1810 {
1811 /* preend node to the head of the queue */
1812 node->rq_next = head->rq_next;
1813 node->rq_prev = NULL;
1814 if (head->rq_next == NULL)
1815 head->rq_prev = node;
1816 else
1817 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001818 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001819 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001820 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001821 {
1822 /* append node to the tail of the queue */
1823 node->rq_next = NULL;
1824 node->rq_prev = head->rq_prev;
1825 if (head->rq_prev == NULL)
1826 head->rq_next = node;
1827 else
1828 head->rq_prev->rq_next = node;
1829 head->rq_prev = node;
1830 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001831
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001832 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001833 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001834 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001835 fprintf(log_fd, "'");
1836 if (fwrite(buf, len, 1, log_fd) != 1)
1837 return FAIL;
1838 fprintf(log_fd, "'\n");
1839 }
1840 return OK;
1841}
1842
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001843 static int
1844channel_fill(js_read_T *reader)
1845{
1846 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001847 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001848 char_u *next = channel_get(channel, part);
1849 int unused;
1850 int len;
1851 char_u *p;
1852
1853 if (next == NULL)
1854 return FALSE;
1855
1856 unused = reader->js_end - reader->js_buf - reader->js_used;
1857 if (unused > 0)
1858 {
1859 /* Prepend unused text. */
1860 len = (int)STRLEN(next);
1861 p = alloc(unused + len + 1);
1862 if (p == NULL)
1863 {
1864 vim_free(next);
1865 return FALSE;
1866 }
1867 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1868 mch_memmove(p + unused, next, len + 1);
1869 vim_free(next);
1870 next = p;
1871 }
1872
1873 vim_free(reader->js_buf);
1874 reader->js_buf = next;
1875 reader->js_used = 0;
1876 return TRUE;
1877}
1878
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001879/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001880 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001881 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001882 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001883 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001884 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001885channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001886{
1887 js_read_T reader;
1888 typval_T listtv;
1889 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001890 chanpart_T *chanpart = &channel->ch_part[part];
1891 jsonq_T *head = &chanpart->ch_json_head;
1892 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001893 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001894
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001895 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001896 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001897
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001898 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001899 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001900 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001901 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001902 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001903
1904 /* When a message is incomplete we wait for a short while for more to
1905 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001906 * or list will make us hang.
1907 * Do not generate error messages, they will be written in a channel log. */
1908 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001909 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001910 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001911 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001912 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001913 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001914 /* Only accept the response when it is a list with at least two
1915 * items. */
1916 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001917 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001918 if (listtv.v_type != VAR_LIST)
1919 ch_error(channel, "Did not receive a list, discarding");
1920 else
1921 ch_errorn(channel, "Expected list with two items, got %d",
1922 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001923 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001924 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001925 else
1926 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001927 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1928 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001929 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001930 else
1931 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001932 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001933 item->jq_value = alloc_tv();
1934 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001935 {
1936 vim_free(item);
1937 clear_tv(&listtv);
1938 }
1939 else
1940 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001941 *item->jq_value = listtv;
1942 item->jq_prev = head->jq_prev;
1943 head->jq_prev = item;
1944 item->jq_next = NULL;
1945 if (item->jq_prev == NULL)
1946 head->jq_next = item;
1947 else
1948 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001949 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001950 }
1951 }
1952 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001953
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001954 if (status == OK)
1955 chanpart->ch_waiting = FALSE;
1956 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001957 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001958 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001959 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001960 /* First time encountering incomplete message, set a deadline of
1961 * 100 msec. */
1962 ch_log(channel, "Incomplete message - wait for more");
1963 reader.js_used = 0;
1964 chanpart->ch_waiting = TRUE;
1965#ifdef WIN32
1966 chanpart->ch_deadline = GetTickCount() + 100L;
1967#else
1968 gettimeofday(&chanpart->ch_deadline, NULL);
1969 chanpart->ch_deadline.tv_usec += 100 * 1000;
1970 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1971 {
1972 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1973 ++chanpart->ch_deadline.tv_sec;
1974 }
1975#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001976 }
1977 else
1978 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001979 int timeout;
1980#ifdef WIN32
1981 timeout = GetTickCount() > chanpart->ch_deadline;
1982#else
1983 {
1984 struct timeval now_tv;
1985
1986 gettimeofday(&now_tv, NULL);
1987 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1988 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1989 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1990 }
1991#endif
1992 if (timeout)
1993 {
1994 status = FAIL;
1995 chanpart->ch_waiting = FALSE;
1996 }
1997 else
1998 {
1999 reader.js_used = 0;
2000 ch_log(channel, "still waiting on incomplete message");
2001 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002002 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002003 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002004
2005 if (status == FAIL)
2006 {
2007 ch_error(channel, "Decoding failed - discarding input");
2008 ret = FALSE;
2009 chanpart->ch_waiting = FALSE;
2010 }
2011 else if (reader.js_buf[reader.js_used] != NUL)
2012 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002013 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002014 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002015 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2016 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002017 ret = status == MAYBE ? FALSE: TRUE;
2018 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002019 else
2020 ret = FALSE;
2021
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002022 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002023 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002024}
2025
2026/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002027 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002028 */
2029 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002030remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002031{
Bram Moolenaar77073442016-02-13 23:23:53 +01002032 if (node->cq_prev == NULL)
2033 head->cq_next = node->cq_next;
2034 else
2035 node->cq_prev->cq_next = node->cq_next;
2036 if (node->cq_next == NULL)
2037 head->cq_prev = node->cq_prev;
2038 else
2039 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002040}
2041
2042/*
2043 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002044 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002045 */
2046 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002047remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002048{
Bram Moolenaar77073442016-02-13 23:23:53 +01002049 if (node->jq_prev == NULL)
2050 head->jq_next = node->jq_next;
2051 else
2052 node->jq_prev->jq_next = node->jq_next;
2053 if (node->jq_next == NULL)
2054 head->jq_prev = node->jq_prev;
2055 else
2056 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002057 vim_free(node);
2058}
2059
2060/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002061 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002062 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002063 * When "id" is zero or negative jut get the first message. But not the one
2064 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002065 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002066 * Return OK when found and return the value in "rettv".
2067 * Return FAIL otherwise.
2068 */
2069 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002070channel_get_json(
2071 channel_T *channel,
2072 ch_part_T part,
2073 int id,
2074 int without_callback,
2075 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002076{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002077 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002078 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002079
Bram Moolenaar77073442016-02-13 23:23:53 +01002080 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002081 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002082 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002083 typval_T *tv = &l->lv_first->li_tv;
2084
Bram Moolenaar958dc692016-12-01 15:34:12 +01002085 if ((without_callback || !item->jq_no_callback)
2086 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002087 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002088 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002089 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002090 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002091 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002092 if (tv->v_type == VAR_NUMBER)
2093 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002094 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002095 return OK;
2096 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002097 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002098 }
2099 return FAIL;
2100}
2101
Bram Moolenaar958dc692016-12-01 15:34:12 +01002102/*
2103 * Put back "rettv" into the JSON queue, there was no callback for it.
2104 * Takes over the values in "rettv".
2105 */
2106 static void
2107channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2108{
2109 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2110 jsonq_T *item = head->jq_next;
2111 jsonq_T *newitem;
2112
2113 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2114 /* last item was pushed back, append to the end */
2115 item = NULL;
2116 else while (item != NULL && item->jq_no_callback)
2117 /* append after the last item that was pushed back */
2118 item = item->jq_next;
2119
2120 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2121 if (newitem == NULL)
2122 clear_tv(rettv);
2123 else
2124 {
2125 newitem->jq_value = alloc_tv();
2126 if (newitem->jq_value == NULL)
2127 {
2128 vim_free(newitem);
2129 clear_tv(rettv);
2130 }
2131 else
2132 {
2133 newitem->jq_no_callback = FALSE;
2134 *newitem->jq_value = *rettv;
2135 if (item == NULL)
2136 {
2137 /* append to the end */
2138 newitem->jq_prev = head->jq_prev;
2139 head->jq_prev = newitem;
2140 newitem->jq_next = NULL;
2141 if (newitem->jq_prev == NULL)
2142 head->jq_next = newitem;
2143 else
2144 newitem->jq_prev->jq_next = newitem;
2145 }
2146 else
2147 {
2148 /* append after "item" */
2149 newitem->jq_prev = item;
2150 newitem->jq_next = item->jq_next;
2151 item->jq_next = newitem;
2152 if (newitem->jq_next == NULL)
2153 head->jq_prev = newitem;
2154 else
2155 newitem->jq_next->jq_prev = newitem;
2156 }
2157 }
2158 }
2159}
2160
Bram Moolenaarece61b02016-02-20 21:39:05 +01002161#define CH_JSON_MAX_ARGS 4
2162
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002163/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002164 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002165 * "argv[0]" is the command string.
2166 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002167 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002168 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002169channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002170{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002171 char_u *cmd = argv[0].vval.v_string;
2172 char_u *arg;
2173 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002174
Bram Moolenaarece61b02016-02-20 21:39:05 +01002175 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002176 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002177 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002178 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002179 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002180 return;
2181 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002182 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002183 if (arg == NULL)
2184 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002185
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002186 if (STRCMP(cmd, "ex") == 0)
2187 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002188 int save_called_emsg = called_emsg;
2189
2190 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002191 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002192 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002193 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002194 --emsg_silent;
2195 if (called_emsg)
2196 ch_logs(channel, "Ex command error: '%s'",
2197 (char *)get_vim_var_str(VV_ERRMSG));
2198 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002199 }
2200 else if (STRCMP(cmd, "normal") == 0)
2201 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002202 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002203
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002204 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002205 ea.arg = arg;
2206 ea.addr_count = 0;
2207 ea.forceit = TRUE; /* no mapping */
2208 ex_normal(&ea);
2209 }
2210 else if (STRCMP(cmd, "redraw") == 0)
2211 {
2212 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002213
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002214 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002215 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002216 ex_redraw(&ea);
2217 showruler(FALSE);
2218 setcursor();
2219 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002220#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002221 if (gui.in_use)
2222 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002223 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002224 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002225 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002226#endif
2227 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002228 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002229 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002230 int is_call = cmd[0] == 'c';
2231 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002232
Bram Moolenaarece61b02016-02-20 21:39:05 +01002233 if (argv[id_idx].v_type != VAR_UNKNOWN
2234 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002235 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002236 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002237 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002238 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002239 }
2240 else if (is_call && argv[2].v_type != VAR_LIST)
2241 {
2242 ch_error(channel, "third argument for call must be a list");
2243 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002244 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002245 }
2246 else
2247 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002248 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002249 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002250 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002251 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002252
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002253 /* Don't pollute the display with errors. */
2254 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002255 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002256 {
2257 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002258 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002259 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002260 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002261 {
2262 ch_logs(channel, "Calling '%s'", (char *)arg);
2263 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2264 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002265 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002266
2267 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002268 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002269 int id = argv[id_idx].vval.v_number;
2270
Bram Moolenaar55fab432016-02-07 16:53:13 +01002271 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002272 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002273 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002274 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002275 /* If evaluation failed or the result can't be encoded
2276 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002277 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002278 err_tv.v_type = VAR_STRING;
2279 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002280 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002281 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002282 if (json != NULL)
2283 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002284 channel_send(channel,
2285 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002286 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002287 vim_free(json);
2288 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002289 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002290 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002291 if (tv == &res_tv)
2292 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002293 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002294 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002295 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002296 }
2297 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002298 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002299 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002300 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002301 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002302}
2303
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002304/*
2305 * Invoke the callback at "cbhead".
2306 * Does not redraw but sets channel_need_redraw.
2307 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002308 static void
2309invoke_one_time_callback(
2310 channel_T *channel,
2311 cbq_T *cbhead,
2312 cbq_T *item,
2313 typval_T *argv)
2314{
2315 ch_logs(channel, "Invoking one-time callback %s",
2316 (char *)item->cq_callback);
2317 /* Remove the item from the list first, if the callback
2318 * invokes ch_close() the list will be cleared. */
2319 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002320 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002321 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002322 vim_free(item);
2323}
2324
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002325 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002326append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002327{
2328 buf_T *save_curbuf = curbuf;
2329 linenr_T lnum = buffer->b_ml.ml_line_count;
2330 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002331 chanpart_T *ch_part = &channel->ch_part[part];
2332 int save_p_ma = buffer->b_p_ma;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002333 int empty = (buffer->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002334
2335 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2336 {
2337 if (!ch_part->ch_nomod_error)
2338 {
2339 ch_error(channel, "Buffer is not modifiable, cannot append");
2340 ch_part->ch_nomod_error = TRUE;
2341 }
2342 return;
2343 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002344
2345 /* If the buffer is also used as input insert above the last
2346 * line. Don't write these lines. */
2347 if (save_write_to)
2348 {
2349 --lnum;
2350 buffer->b_write_to_channel = FALSE;
2351 }
2352
2353 /* Append to the buffer */
2354 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2355
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002356 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002357 curbuf = buffer;
2358 u_sync(TRUE);
2359 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002360 ignored = u_save(lnum, lnum + 1 + (empty ? 1 : 0));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002361
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002362 if (empty)
2363 {
2364 /* The buffer is empty, replace the first (dummy) line. */
2365 ml_replace(lnum, msg, TRUE);
2366 lnum = 0;
2367 }
2368 else
2369 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002370 appended_lines_mark(lnum, 1L);
2371 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002372 if (ch_part->ch_nomodifiable)
2373 buffer->b_p_ma = FALSE;
2374 else
2375 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002376
2377 if (buffer->b_nwindows > 0)
2378 {
2379 win_T *wp;
2380 win_T *save_curwin;
2381
2382 FOR_ALL_WINDOWS(wp)
2383 {
2384 if (wp->w_buffer == buffer
2385 && (save_write_to
2386 ? wp->w_cursor.lnum == lnum + 1
2387 : (wp->w_cursor.lnum == lnum
2388 && wp->w_cursor.col == 0)))
2389 {
2390 ++wp->w_cursor.lnum;
2391 save_curwin = curwin;
2392 curwin = wp;
2393 curbuf = curwin->w_buffer;
2394 scroll_cursor_bot(0, FALSE);
2395 curwin = save_curwin;
2396 curbuf = curwin->w_buffer;
2397 }
2398 }
2399 redraw_buf_later(buffer, VALID);
2400 channel_need_redraw = TRUE;
2401 }
2402
2403 if (save_write_to)
2404 {
2405 channel_T *ch;
2406
2407 /* Find channels reading from this buffer and adjust their
2408 * next-to-read line number. */
2409 buffer->b_write_to_channel = TRUE;
2410 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2411 {
2412 chanpart_T *in_part = &ch->ch_part[PART_IN];
2413
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002414 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002415 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2416 }
2417 }
2418}
2419
Bram Moolenaar437905c2016-04-26 19:01:05 +02002420 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002421drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002422{
2423 char_u *msg;
2424
2425 while ((msg = channel_get(channel, part)) != NULL)
2426 {
2427 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2428 vim_free(msg);
2429 }
2430}
2431
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002432/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002433 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002434 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002435 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002436 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002437 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002438may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002439{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002440 char_u *msg = NULL;
2441 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002442 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002443 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002444 chanpart_T *ch_part = &channel->ch_part[part];
2445 ch_mode_T ch_mode = ch_part->ch_mode;
2446 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002447 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002448 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002449 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002450 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002451 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002452
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002453 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002454 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002455 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002456
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002457 /* Use a message-specific callback, part callback or channel callback */
2458 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2459 if (cbitem->cq_seq_nr == 0)
2460 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002461 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002462 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002463 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002464 partial = cbitem->cq_partial;
2465 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002466 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002467 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002468 callback = ch_part->ch_callback;
2469 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002470 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002471 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002472 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002473 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002474 partial = channel->ch_partial;
2475 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002476
Bram Moolenaarec68a992016-10-03 21:37:41 +02002477 buffer = ch_part->ch_bufref.br_buf;
2478 if (buffer != NULL && !bufref_valid(&ch_part->ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002479 {
2480 /* buffer was wiped out */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002481 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002482 buffer = NULL;
2483 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002484
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002485 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002486 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002487 listitem_T *item;
2488 int argc = 0;
2489
Bram Moolenaard7ece102016-02-02 23:23:02 +01002490 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002491 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002492 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002493 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002494 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002495 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002496 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002497 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002498
Bram Moolenaarece61b02016-02-20 21:39:05 +01002499 for (item = listtv->vval.v_list->lv_first;
2500 item != NULL && argc < CH_JSON_MAX_ARGS;
2501 item = item->li_next)
2502 argv[argc++] = item->li_tv;
2503 while (argc < CH_JSON_MAX_ARGS)
2504 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002505
Bram Moolenaarece61b02016-02-20 21:39:05 +01002506 if (argv[0].v_type == VAR_STRING)
2507 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002508 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002509 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002510 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002511 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002512 }
2513
Bram Moolenaarece61b02016-02-20 21:39:05 +01002514 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002515 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002516 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002517 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002518 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002519 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002520 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002521 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002522 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002523 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002524 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002525 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002526 return FALSE;
2527 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002528 else
2529 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002530 /* If there is no callback or buffer drop the message. */
2531 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002532 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002533 /* If there is a close callback it may use ch_read() to get the
2534 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002535 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002536 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002537 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002538 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002539
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002540 if (ch_mode == MODE_NL)
2541 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002542 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002543 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002544 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002545
2546 /* See if we have a message ending in NL in the first buffer. If
2547 * not try to concatenate the first and the second buffer. */
2548 while (TRUE)
2549 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002550 node = channel_peek(channel, part);
2551 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002552 if (nl != NULL)
2553 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002554 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002555 {
2556 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2557 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002558 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002559 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002560 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002561 buf = node->rq_buffer;
2562
Bram Moolenaarec68a992016-10-03 21:37:41 +02002563 if (nl == NULL)
2564 {
2565 /* Flush remaining message that is missing a NL. */
2566 buf = vim_realloc(buf, node->rq_buflen + 1);
2567 if (buf == NULL)
2568 return FALSE;
2569 node->rq_buffer = buf;
2570 nl = buf + node->rq_buflen++;
2571 *nl = NUL;
2572 }
2573
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002574 /* Convert NUL to NL, the internal representation. */
2575 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2576 if (*p == NUL)
2577 *p = NL;
2578
2579 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002580 {
2581 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002582 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002583 *nl = NUL;
2584 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002585 else
2586 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002587 /* Copy the message into allocated memory (excluding the NL)
2588 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002589 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002590 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002591 }
2592 }
2593 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002594 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002595 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002596 * get everything we have.
2597 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002598 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002599 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002600
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002601 if (msg == NULL)
2602 return FALSE; /* out of memory (and avoids Coverity warning) */
2603
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002604 argv[1].v_type = VAR_STRING;
2605 argv[1].vval.v_string = msg;
2606 }
2607
Bram Moolenaara07fec92016-02-05 21:04:08 +01002608 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002609 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002610 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002611
Bram Moolenaar958dc692016-12-01 15:34:12 +01002612 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002613 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002614 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002615 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002616 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002617 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002618 break;
2619 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002620 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002621 {
2622 if (channel->ch_drop_never)
2623 {
2624 /* message must be read with ch_read() */
2625 channel_push_json(channel, part, listtv);
2626 listtv = NULL;
2627 }
2628 else
2629 ch_logn(channel, "Dropping message %d without callback",
2630 seq_nr);
2631 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002632 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002633 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002634 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002635 if (buffer != NULL)
2636 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002637 if (msg == NULL)
2638 /* JSON or JS mode: re-encode the message. */
2639 msg = json_encode(listtv, ch_mode);
2640 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002641 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002642 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002643
Bram Moolenaar187db502016-02-27 14:44:26 +01002644 if (callback != NULL)
2645 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002646 if (cbitem != NULL)
2647 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2648 else
2649 {
2650 /* invoke the channel callback */
2651 ch_logs(channel, "Invoking channel callback %s",
2652 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002653 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002654 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002655 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002656 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002657 else
Bram Moolenaar958dc692016-12-01 15:34:12 +01002658 ch_logn(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002659
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002660 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002661 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002662 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002663
2664 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002665}
2666
2667/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002668 * Return TRUE when channel "channel" is open for writing to.
2669 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002670 */
2671 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002672channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002673{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002674 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002675 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002676}
2677
2678/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002679 * Return TRUE when channel "channel" is open for reading or writing.
2680 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002681 */
2682 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002683channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002684{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002685 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002686 || channel->CH_IN_FD != INVALID_FD
2687 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002688 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002689}
2690
2691/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002692 * Return TRUE if "channel" has JSON or other typeahead.
2693 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002694 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002695channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002696{
2697 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2698
2699 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2700 {
2701 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2702 jsonq_T *item = head->jq_next;
2703
2704 return item != NULL;
2705 }
2706 return channel_peek(channel, part) != NULL;
2707}
2708
2709/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002710 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002711 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002712 */
2713 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002714channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002715{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002716 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002717 int has_readahead = FALSE;
2718
Bram Moolenaar77073442016-02-13 23:23:53 +01002719 if (channel == NULL)
2720 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002721 if (req_part == PART_OUT)
2722 {
2723 if (channel->CH_OUT_FD != INVALID_FD)
2724 return "open";
2725 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002726 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002727 }
2728 else if (req_part == PART_ERR)
2729 {
2730 if (channel->CH_ERR_FD != INVALID_FD)
2731 return "open";
2732 if (channel_has_readahead(channel, PART_ERR))
2733 has_readahead = TRUE;
2734 }
2735 else
2736 {
2737 if (channel_is_open(channel))
2738 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002739 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002740 if (channel_has_readahead(channel, part))
2741 {
2742 has_readahead = TRUE;
2743 break;
2744 }
2745 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002746
2747 if (has_readahead)
2748 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002749 return "closed";
2750}
2751
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002752 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002753channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002754{
2755 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002756 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002757 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002758 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002759 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002760
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002761 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002762 STRCAT(namebuf, "_");
2763 tail = STRLEN(namebuf);
2764
2765 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002766 if (chanpart->ch_fd != INVALID_FD)
2767 status = "open";
2768 else if (channel_has_readahead(channel, part))
2769 status = "buffered";
2770 else
2771 status = "closed";
2772 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002773
2774 STRCPY(namebuf + tail, "mode");
2775 switch (chanpart->ch_mode)
2776 {
2777 case MODE_NL: s = "NL"; break;
2778 case MODE_RAW: s = "RAW"; break;
2779 case MODE_JSON: s = "JSON"; break;
2780 case MODE_JS: s = "JS"; break;
2781 }
2782 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2783
2784 STRCPY(namebuf + tail, "io");
2785 if (part == PART_SOCK)
2786 s = "socket";
2787 else switch (chanpart->ch_io)
2788 {
2789 case JIO_NULL: s = "null"; break;
2790 case JIO_PIPE: s = "pipe"; break;
2791 case JIO_FILE: s = "file"; break;
2792 case JIO_BUFFER: s = "buffer"; break;
2793 case JIO_OUT: s = "out"; break;
2794 }
2795 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2796
2797 STRCPY(namebuf + tail, "timeout");
2798 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2799}
2800
2801 void
2802channel_info(channel_T *channel, dict_T *dict)
2803{
2804 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002805 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002806
2807 if (channel->ch_hostname != NULL)
2808 {
2809 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2810 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2811 channel_part_info(channel, dict, "sock", PART_SOCK);
2812 }
2813 else
2814 {
2815 channel_part_info(channel, dict, "out", PART_OUT);
2816 channel_part_info(channel, dict, "err", PART_ERR);
2817 channel_part_info(channel, dict, "in", PART_IN);
2818 }
2819}
2820
Bram Moolenaar77073442016-02-13 23:23:53 +01002821/*
2822 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002823 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002824 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002825 */
2826 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002827channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002828{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002829 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002830
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002831#ifdef FEAT_GUI
2832 channel_gui_unregister(channel);
2833#endif
2834
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002835 ch_close_part(channel, PART_SOCK);
2836 ch_close_part(channel, PART_IN);
2837 ch_close_part(channel, PART_OUT);
2838 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002839
Bram Moolenaar8b374212016-02-24 20:43:06 +01002840 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002841 {
2842 typval_T argv[1];
2843 typval_T rettv;
2844 int dummy;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002845 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002846
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002847 /* Invoke callbacks before the close callback, since it's weird to
2848 * first invoke the close callback. Increment the refcount to avoid
2849 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002850 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002851 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002852 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002853 while (may_invoke_callback(channel, part))
2854 ;
2855
2856 /* Invoke the close callback, if still set. */
2857 if (channel->ch_close_cb != NULL)
2858 {
2859 ch_logs(channel, "Invoking close callback %s",
2860 (char *)channel->ch_close_cb);
2861 argv[0].v_type = VAR_CHANNEL;
2862 argv[0].vval.v_channel = channel;
2863 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002864 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002865 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002866 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002867 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002868 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002869
2870 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002871 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002872 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002873 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002874
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002875 --channel->ch_refcount;
2876
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002877 if (channel_need_redraw)
2878 {
2879 channel_need_redraw = FALSE;
2880 redraw_after_callback();
2881 }
2882
Bram Moolenaar958dc692016-12-01 15:34:12 +01002883 if (!channel->ch_drop_never)
2884 /* any remaining messages are useless now */
2885 for (part = PART_SOCK; part < PART_IN; ++part)
2886 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002887 }
2888
2889 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002890}
2891
Bram Moolenaard04a0202016-01-26 23:30:18 +01002892/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002893 * Close the "in" part channel "channel".
2894 */
2895 void
2896channel_close_in(channel_T *channel)
2897{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002898 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002899}
2900
2901/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002902 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002903 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002904 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002905channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002906{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002907 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2908 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002909
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002910 while (channel_peek(channel, part) != NULL)
2911 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002912
2913 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002914 {
2915 cbq_T *node = cb_head->cq_next;
2916
2917 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002918 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002919 vim_free(node);
2920 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002921
2922 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002923 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002924 free_tv(json_head->jq_next->jq_value);
2925 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002926 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002927
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002928 free_callback(channel->ch_part[part].ch_callback,
2929 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002930 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002931 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002932}
2933
2934/*
2935 * Clear all the read buffers on "channel".
2936 */
2937 void
2938channel_clear(channel_T *channel)
2939{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002940 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002941 vim_free(channel->ch_hostname);
2942 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002943 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002944 channel_clear_one(channel, PART_OUT);
2945 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002946 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002947 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002948 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002949 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002950 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002951 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002952 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002953}
2954
Bram Moolenaar77073442016-02-13 23:23:53 +01002955#if defined(EXITFREE) || defined(PROTO)
2956 void
2957channel_free_all(void)
2958{
2959 channel_T *channel;
2960
Bram Moolenaard6051b52016-02-28 15:49:03 +01002961 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002962 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2963 channel_clear(channel);
2964}
2965#endif
2966
2967
Bram Moolenaar715d2852016-04-30 17:06:31 +02002968/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002969#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002970
2971/* Buffer size for reading incoming messages. */
2972#define MAXMSGSIZE 4096
2973
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002974#if defined(HAVE_SELECT)
2975/*
2976 * Add write fds where we are waiting for writing to be possible.
2977 */
2978 static int
2979channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2980{
2981 int maxfd = maxfd_arg;
2982 channel_T *ch;
2983
2984 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2985 {
2986 chanpart_T *in_part = &ch->ch_part[PART_IN];
2987
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002988 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002989 {
2990 FD_SET((int)in_part->ch_fd, wfds);
2991 if ((int)in_part->ch_fd >= maxfd)
2992 maxfd = (int)in_part->ch_fd + 1;
2993 }
2994 }
2995 return maxfd;
2996}
2997#else
2998/*
2999 * Add write fds where we are waiting for writing to be possible.
3000 */
3001 static int
3002channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3003{
3004 int nfd = nfd_in;
3005 channel_T *ch;
3006
3007 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3008 {
3009 chanpart_T *in_part = &ch->ch_part[PART_IN];
3010
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003011 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003012 {
3013 in_part->ch_poll_idx = nfd;
3014 fds[nfd].fd = in_part->ch_fd;
3015 fds[nfd].events = POLLOUT;
3016 ++nfd;
3017 }
3018 else
3019 in_part->ch_poll_idx = -1;
3020 }
3021 return nfd;
3022}
3023#endif
3024
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003025typedef enum {
3026 CW_READY,
3027 CW_NOT_READY,
3028 CW_ERROR
3029} channel_wait_result;
3030
Bram Moolenaard04a0202016-01-26 23:30:18 +01003031/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003032 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003033 * Return CW_READY when there is something to read.
3034 * Return CW_NOT_READY when there is nothing to read.
3035 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003036 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003037 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003038channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003039{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003040 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003041 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003042
Bram Moolenaard8070362016-02-15 21:56:54 +01003043# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003044 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003045 {
3046 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003047 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003048 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003049 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003050
3051 /* reading from a pipe, not a socket */
3052 while (TRUE)
3053 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003054 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3055
3056 if (r && nread > 0)
3057 return CW_READY;
3058 if (r == 0)
3059 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003060
3061 /* perhaps write some buffer lines */
3062 channel_write_any_lines();
3063
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003064 sleep_time = deadline - GetTickCount();
3065 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003066 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003067 /* Wait for a little while. Very short at first, up to 10 msec
3068 * after looping a few times. */
3069 if (sleep_time > delay)
3070 sleep_time = delay;
3071 Sleep(sleep_time);
3072 delay = delay * 2;
3073 if (delay > 10)
3074 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003075 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003076 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003077 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003078#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003079 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003080#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003081 struct timeval tval;
3082 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003083 fd_set wfds;
3084 int ret;
3085 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003086
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003087 tval.tv_sec = timeout / 1000;
3088 tval.tv_usec = (timeout % 1000) * 1000;
3089 for (;;)
3090 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003091 FD_ZERO(&rfds);
3092 FD_SET((int)fd, &rfds);
3093
3094 /* Write lines to a pipe when a pipe can be written to. Need to
3095 * set this every time, some buffers may be done. */
3096 maxfd = (int)fd + 1;
3097 FD_ZERO(&wfds);
3098 maxfd = channel_fill_wfds(maxfd, &wfds);
3099
3100 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003101# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003102 SOCK_ERRNO;
3103 if (ret == -1 && errno == EINTR)
3104 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003105# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003106 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003107 {
3108 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003109 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003110 channel_write_any_lines();
3111 continue;
3112 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003113 break;
3114 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003115#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003116 for (;;)
3117 {
3118 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3119 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003120
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003121 fds[0].fd = fd;
3122 fds[0].events = POLLIN;
3123 nfd = channel_fill_poll_write(nfd, fds);
3124 if (poll(fds, nfd, timeout) > 0)
3125 {
3126 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003127 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003128 channel_write_any_lines();
3129 continue;
3130 }
3131 break;
3132 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003133#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003134 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003135 return CW_NOT_READY;
3136}
3137
3138 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003139ch_close_part_on_error(
3140 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003141{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003142 char msgbuf[80];
3143
3144 vim_snprintf(msgbuf, sizeof(msgbuf),
3145 "%%s(): Read %s from ch_part[%d], closing",
3146 (is_err ? "error" : "EOF"), part);
3147
3148 if (is_err)
3149 /* Do not call emsg(), most likely the other end just exited. */
3150 ch_errors(channel, msgbuf, func);
3151 else
3152 ch_logs(channel, msgbuf, func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003153
3154 /* Queue a "DETACH" netbeans message in the command queue in order to
3155 * terminate the netbeans session later. Do not end the session here
3156 * directly as we may be running in the context of a call to
3157 * netbeans_parse_messages():
3158 * netbeans_parse_messages
3159 * -> autocmd triggered while processing the netbeans cmd
3160 * -> ui_breakcheck
3161 * -> gui event loop or select loop
3162 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003163 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003164 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003165 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003166 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003167 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3168
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003169 /* When reading is not possible close this part of the channel. Don't
3170 * close the channel yet, there may be something to read on another part. */
3171 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003172
3173#ifdef FEAT_GUI
3174 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003175 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003176#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003177}
3178
3179 static void
3180channel_close_now(channel_T *channel)
3181{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003182 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003183 if (channel->ch_nb_close_cb != NULL)
3184 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003185 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003186}
3187
3188/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003189 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003190 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003191 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003192 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003193 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003194channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003195{
3196 static char_u *buf = NULL;
3197 int len = 0;
3198 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003199 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003200 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003201
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003202 fd = channel->ch_part[part].ch_fd;
3203 if (fd == INVALID_FD)
3204 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003205 ch_errors(channel, "channel_read() called while %s part is closed",
3206 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003207 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003208 }
3209 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003210
3211 /* Allocate a buffer to read into. */
3212 if (buf == NULL)
3213 {
3214 buf = alloc(MAXMSGSIZE);
3215 if (buf == NULL)
3216 return; /* out of memory! */
3217 }
3218
3219 /* Keep on reading for as long as there is something to read.
3220 * Use select() or poll() to avoid blocking on a message that is exactly
3221 * MAXMSGSIZE long. */
3222 for (;;)
3223 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003224 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003225 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003226 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003227 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003228 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003229 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003230 if (len <= 0)
3231 break; /* error or nothing more to read */
3232
3233 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003234 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003235 readlen += len;
3236 if (len < MAXMSGSIZE)
3237 break; /* did read everything that's available */
3238 }
3239
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003240 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003241 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003242 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003243
3244#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003245 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003246 if (CH_HAS_GUI && gtk_main_level() > 0)
3247 gtk_main_quit();
3248#endif
3249}
3250
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003251/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003252 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003253 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003254 * Returns what was read in allocated memory.
3255 * Returns NULL in case of error or timeout.
3256 */
3257 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003258channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003259{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003260 char_u *buf;
3261 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003262 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003263 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003264 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003265 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003266
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003267 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003268 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003269
3270 while (TRUE)
3271 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003272 node = channel_peek(channel, part);
3273 if (node != NULL)
3274 {
3275 if (mode == MODE_RAW || (mode == MODE_NL
3276 && channel_first_nl(node) != NULL))
3277 /* got a complete message */
3278 break;
3279 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3280 continue;
3281 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003282
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003283 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003284 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003285 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003286 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003287 {
3288 ch_log(channel, "Timed out");
3289 return NULL;
3290 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003291 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003292 }
3293
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003294 if (mode == MODE_RAW)
3295 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003296 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003297 }
3298 else
3299 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003300 char_u *p;
3301
3302 buf = node->rq_buffer;
3303 nl = channel_first_nl(node);
3304
3305 /* Convert NUL to NL, the internal representation. */
3306 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3307 if (*p == NUL)
3308 *p = NL;
3309
3310 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003311 {
3312 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003313 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003314 *nl = NUL;
3315 }
3316 else
3317 {
3318 /* Copy the message into allocated memory and remove it from the
3319 * buffer. */
3320 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003321 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003322 }
3323 }
3324 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003325 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003326 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003327}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003328
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003329/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003330 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003331 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003332 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003333 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003334 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003335 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003336channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003337 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003338 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003339 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003340 int id,
3341 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003342{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003343 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003344 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003345 int timeout;
3346 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003347
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003348 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003349 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003350 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003351 for (;;)
3352 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003353 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003354
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003355 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003356 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003357 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003358 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003359 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003360 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003361
Bram Moolenaard7ece102016-02-02 23:23:02 +01003362 if (!more)
3363 {
3364 /* Handle any other messages in the queue. If done some more
3365 * messages may have arrived. */
3366 if (channel_parse_messages())
3367 continue;
3368
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003369 /* Wait for up to the timeout. If there was an incomplete message
3370 * use the deadline for that. */
3371 timeout = timeout_arg;
3372 if (chanpart->ch_waiting)
3373 {
3374#ifdef WIN32
3375 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3376#else
3377 {
3378 struct timeval now_tv;
3379
3380 gettimeofday(&now_tv, NULL);
3381 timeout = (chanpart->ch_deadline.tv_sec
3382 - now_tv.tv_sec) * 1000
3383 + (chanpart->ch_deadline.tv_usec
3384 - now_tv.tv_usec) / 1000
3385 + 1;
3386 }
3387#endif
3388 if (timeout < 0)
3389 {
3390 /* Something went wrong, channel_parse_json() didn't
3391 * discard message. Cancel waiting. */
3392 chanpart->ch_waiting = FALSE;
3393 timeout = timeout_arg;
3394 }
3395 else if (timeout > timeout_arg)
3396 timeout = timeout_arg;
3397 }
3398 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003399 if (fd == INVALID_FD
3400 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003401 {
3402 if (timeout == timeout_arg)
3403 {
3404 if (fd != INVALID_FD)
3405 ch_log(channel, "Timed out");
3406 break;
3407 }
3408 }
3409 else
3410 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003411 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003412 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003413 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003414 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003415}
3416
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003417/*
3418 * Common for ch_read() and ch_readraw().
3419 */
3420 void
3421common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3422{
3423 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003424 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003425 jobopt_T opt;
3426 int mode;
3427 int timeout;
3428 int id = -1;
3429 typval_T *listtv = NULL;
3430
3431 /* return an empty string by default */
3432 rettv->v_type = VAR_STRING;
3433 rettv->vval.v_string = NULL;
3434
3435 clear_job_options(&opt);
3436 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3437 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003438 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003439
Bram Moolenaar437905c2016-04-26 19:01:05 +02003440 if (opt.jo_set & JO_PART)
3441 part = opt.jo_part;
3442 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003443 if (channel != NULL)
3444 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003445 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003446 part = channel_part_read(channel);
3447 mode = channel_get_mode(channel, part);
3448 timeout = channel_get_timeout(channel, part);
3449 if (opt.jo_set & JO_TIMEOUT)
3450 timeout = opt.jo_timeout;
3451
3452 if (raw || mode == MODE_RAW || mode == MODE_NL)
3453 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3454 else
3455 {
3456 if (opt.jo_set & JO_ID)
3457 id = opt.jo_id;
3458 channel_read_json_block(channel, part, timeout, id, &listtv);
3459 if (listtv != NULL)
3460 {
3461 *rettv = *listtv;
3462 vim_free(listtv);
3463 }
3464 else
3465 {
3466 rettv->v_type = VAR_SPECIAL;
3467 rettv->vval.v_number = VVAL_NONE;
3468 }
3469 }
3470 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003471
3472theend:
3473 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003474}
3475
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003476# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3477 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003478/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003479 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003480 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003481 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003482 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003483channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003484{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003485 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003486 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003487
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003488 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003489 for (channel = first_channel; channel != NULL;
3490 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003491 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003492 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003493 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003494 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003495 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003496 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003497 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003498 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003499 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003500}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003501# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003502
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003503# if defined(WIN32) || defined(PROTO)
3504/*
3505 * Check the channels for anything that is ready to be read.
3506 * The data is put in the read queue.
3507 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003508 void
3509channel_handle_events(void)
3510{
3511 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003512 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003513 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003514
3515 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3516 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003517 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003518 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003519 {
3520 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003521 if (fd != INVALID_FD)
3522 {
3523 int r = channel_wait(channel, fd, 0);
3524
3525 if (r == CW_READY)
3526 channel_read(channel, part, "channel_handle_events");
3527 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003528 ch_close_part_on_error(channel, part, TRUE,
3529 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003530 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003531 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003532 }
3533}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003534# endif
3535
Bram Moolenaard04a0202016-01-26 23:30:18 +01003536/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003537 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003538 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003539 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003540 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003541 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003542channel_send(
3543 channel_T *channel,
3544 ch_part_T part,
3545 char_u *buf,
3546 int len,
3547 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003548{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003549 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003550 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003551
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003552 fd = channel->ch_part[part].ch_fd;
3553 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003554 {
3555 if (!channel->ch_error && fun != NULL)
3556 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003557 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003558 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003559 }
3560 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003561 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003562 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003563
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003564 if (log_fd != NULL)
3565 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003566 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003567 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003568 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003569 fprintf(log_fd, "'\n");
3570 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003571 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003572 }
3573
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003574 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003575 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003576 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003577 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003578 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003579 {
3580 if (!channel->ch_error && fun != NULL)
3581 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003582 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003583 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003584 }
3585 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003586 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003587 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003588
3589 channel->ch_error = FALSE;
3590 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003591}
3592
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003593/*
3594 * Common for "ch_sendexpr()" and "ch_sendraw()".
3595 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003596 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003597 * Otherwise returns NULL.
3598 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003599 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003600send_common(
3601 typval_T *argvars,
3602 char_u *text,
3603 int id,
3604 int eval,
3605 jobopt_T *opt,
3606 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003607 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003608{
3609 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003610 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003611
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003612 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003613 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003614 if (channel == NULL)
3615 return NULL;
3616 part_send = channel_part_send(channel);
3617 *part_read = channel_part_read(channel);
3618
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003619 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3620 return NULL;
3621
3622 /* Set the callback. An empty callback means no callback and not reading
3623 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3624 * allowed. */
3625 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3626 {
3627 if (eval)
3628 {
3629 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3630 return NULL;
3631 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003632 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003633 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003634 }
3635
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003636 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003637 && opt->jo_callback == NULL)
3638 return channel;
3639 return NULL;
3640}
3641
3642/*
3643 * common for "ch_evalexpr()" and "ch_sendexpr()"
3644 */
3645 void
3646ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3647{
3648 char_u *text;
3649 typval_T *listtv;
3650 channel_T *channel;
3651 int id;
3652 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003653 ch_part_T part_send;
3654 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003655 jobopt_T opt;
3656 int timeout;
3657
3658 /* return an empty string by default */
3659 rettv->v_type = VAR_STRING;
3660 rettv->vval.v_string = NULL;
3661
Bram Moolenaar437905c2016-04-26 19:01:05 +02003662 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003663 if (channel == NULL)
3664 return;
3665 part_send = channel_part_send(channel);
3666
3667 ch_mode = channel_get_mode(channel, part_send);
3668 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3669 {
3670 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3671 return;
3672 }
3673
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003674 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003675 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003676 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003677 if (text == NULL)
3678 return;
3679
3680 channel = send_common(argvars, text, id, eval, &opt,
3681 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3682 vim_free(text);
3683 if (channel != NULL && eval)
3684 {
3685 if (opt.jo_set & JO_TIMEOUT)
3686 timeout = opt.jo_timeout;
3687 else
3688 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003689 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3690 == OK)
3691 {
3692 list_T *list = listtv->vval.v_list;
3693
3694 /* Move the item from the list and then change the type to
3695 * avoid the value being freed. */
3696 *rettv = list->lv_last->li_tv;
3697 list->lv_last->li_tv.v_type = VAR_NUMBER;
3698 free_tv(listtv);
3699 }
3700 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003701 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003702}
3703
3704/*
3705 * common for "ch_evalraw()" and "ch_sendraw()"
3706 */
3707 void
3708ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3709{
3710 char_u buf[NUMBUFLEN];
3711 char_u *text;
3712 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003713 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003714 jobopt_T opt;
3715 int timeout;
3716
3717 /* return an empty string by default */
3718 rettv->v_type = VAR_STRING;
3719 rettv->vval.v_string = NULL;
3720
3721 text = get_tv_string_buf(&argvars[1], buf);
3722 channel = send_common(argvars, text, 0, eval, &opt,
3723 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3724 if (channel != NULL && eval)
3725 {
3726 if (opt.jo_set & JO_TIMEOUT)
3727 timeout = opt.jo_timeout;
3728 else
3729 timeout = channel_get_timeout(channel, part_read);
3730 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3731 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003732 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003733}
3734
Bram Moolenaard04a0202016-01-26 23:30:18 +01003735# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003736/*
3737 * Add open channels to the poll struct.
3738 * Return the adjusted struct index.
3739 * The type of "fds" is hidden to avoid problems with the function proto.
3740 */
3741 int
3742channel_poll_setup(int nfd_in, void *fds_in)
3743{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003744 int nfd = nfd_in;
3745 channel_T *channel;
3746 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003747 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003748
Bram Moolenaar77073442016-02-13 23:23:53 +01003749 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003750 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003751 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003752 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003753 chanpart_T *ch_part = &channel->ch_part[part];
3754
3755 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003756 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003757 ch_part->ch_poll_idx = nfd;
3758 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003759 fds[nfd].events = POLLIN;
3760 nfd++;
3761 }
3762 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003763 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003764 }
3765 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003766
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003767 nfd = channel_fill_poll_write(nfd, fds);
3768
Bram Moolenaare0874f82016-01-24 20:36:41 +01003769 return nfd;
3770}
3771
3772/*
3773 * The type of "fds" is hidden to avoid problems with the function proto.
3774 */
3775 int
3776channel_poll_check(int ret_in, void *fds_in)
3777{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003778 int ret = ret_in;
3779 channel_T *channel;
3780 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003781 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003782 int idx;
3783 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003784
Bram Moolenaar77073442016-02-13 23:23:53 +01003785 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003786 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003787 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003788 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003789 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003790
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003791 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003792 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003793 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003794 --ret;
3795 }
3796 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003797
3798 in_part = &channel->ch_part[PART_IN];
3799 idx = in_part->ch_poll_idx;
3800 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3801 {
3802 if (in_part->ch_buf_append)
3803 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003804 if (in_part->ch_bufref.br_buf != NULL)
3805 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003806 }
3807 else
3808 channel_write_in(channel);
3809 --ret;
3810 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003811 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003812
3813 return ret;
3814}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003815# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003816
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003817# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003818/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003819 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003820 */
3821 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003822channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003823{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003824 int maxfd = maxfd_in;
3825 channel_T *channel;
3826 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003827 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003828 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003829
Bram Moolenaar77073442016-02-13 23:23:53 +01003830 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003831 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003832 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003833 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003834 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003835
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003836 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003837 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003838 FD_SET((int)fd, rfds);
3839 if (maxfd < (int)fd)
3840 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003841 }
3842 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003843 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003844
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003845 maxfd = channel_fill_wfds(maxfd, wfds);
3846
Bram Moolenaare0874f82016-01-24 20:36:41 +01003847 return maxfd;
3848}
3849
3850/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003851 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003852 */
3853 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003854channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003855{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003856 int ret = ret_in;
3857 channel_T *channel;
3858 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003859 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003860 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003861 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003862
Bram Moolenaar77073442016-02-13 23:23:53 +01003863 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003864 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003865 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003866 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003867 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003868
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003869 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003870 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003871 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003872 --ret;
3873 }
3874 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003875
3876 in_part = &channel->ch_part[PART_IN];
3877 if (ret > 0 && in_part->ch_fd != INVALID_FD
3878 && FD_ISSET(in_part->ch_fd, wfds))
3879 {
3880 if (in_part->ch_buf_append)
3881 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003882 if (in_part->ch_bufref.br_buf != NULL)
3883 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003884 }
3885 else
3886 channel_write_in(channel);
3887 --ret;
3888 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003889 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003890
3891 return ret;
3892}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003893# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003894
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003895/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003896 * Execute queued up commands.
3897 * Invoked from the main loop when it's safe to execute received commands.
3898 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003899 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003900 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003901channel_parse_messages(void)
3902{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003903 channel_T *channel = first_channel;
3904 int ret = FALSE;
3905 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003906 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003907#ifdef ELAPSED_FUNC
3908 ELAPSED_TYPE start_tv;
3909
3910 ELAPSED_INIT(start_tv);
3911#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003912
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003913 ++safe_to_invoke_callback;
3914
Bram Moolenaard0b65022016-03-06 21:50:33 +01003915 /* Only do this message when another message was given, otherwise we get
3916 * lots of them. */
3917 if (did_log_msg)
3918 {
3919 ch_log(NULL, "looking for messages on channels");
3920 did_log_msg = FALSE;
3921 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003922 while (channel != NULL)
3923 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003924 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003925 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003926 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003927 channel_close_now(channel);
3928 /* channel may have been freed, start over */
3929 channel = first_channel;
3930 continue;
3931 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003932 if (channel->ch_to_be_freed)
3933 {
3934 channel_free(channel);
3935 /* channel has been freed, start over */
3936 channel = first_channel;
3937 continue;
3938 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003939 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003940 {
3941 /* channel is no longer useful, free it */
3942 channel_free(channel);
3943 channel = first_channel;
3944 part = PART_SOCK;
3945 continue;
3946 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003947 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003948 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003949 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003950 /* Increase the refcount, in case the handler causes the channel
3951 * to be unreferenced or closed. */
3952 ++channel->ch_refcount;
3953 r = may_invoke_callback(channel, part);
3954 if (r == OK)
3955 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003956 if (channel_unref(channel) || (r == OK
3957#ifdef ELAPSED_FUNC
3958 /* Limit the time we loop here to 100 msec, otherwise
3959 * Vim becomes unresponsive when the callback takes
3960 * more than a bit of time. */
3961 && ELAPSED_FUNC(start_tv) < 100L
3962#endif
3963 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003964 {
3965 /* channel was freed or something was done, start over */
3966 channel = first_channel;
3967 part = PART_SOCK;
3968 continue;
3969 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003970 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003971 if (part < PART_ERR)
3972 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003973 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003974 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003975 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003976 part = PART_SOCK;
3977 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003978 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003979
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003980 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003981 {
3982 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003983 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003984 }
3985
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003986 --safe_to_invoke_callback;
3987
Bram Moolenaard7ece102016-02-02 23:23:02 +01003988 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003989}
3990
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003991/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01003992 * Return TRUE if any channel has readahead. That means we should not block on
3993 * waiting for input.
3994 */
3995 int
3996channel_any_readahead(void)
3997{
3998 channel_T *channel = first_channel;
3999 ch_part_T part = PART_SOCK;
4000
4001 while (channel != NULL)
4002 {
4003 if (channel_has_readahead(channel, part))
4004 return TRUE;
4005 if (part < PART_ERR)
4006 ++part;
4007 else
4008 {
4009 channel = channel->ch_next;
4010 part = PART_SOCK;
4011 }
4012 }
4013 return FALSE;
4014}
4015
4016/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004017 * Mark references to lists used in channels.
4018 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004019 int
4020set_ref_in_channel(int copyID)
4021{
Bram Moolenaar77073442016-02-13 23:23:53 +01004022 int abort = FALSE;
4023 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004024 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004025
Bram Moolenaar77073442016-02-13 23:23:53 +01004026 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004027 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004028 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004029 tv.v_type = VAR_CHANNEL;
4030 tv.vval.v_channel = channel;
4031 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004032 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004033 return abort;
4034}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004035
4036/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004037 * Return the "part" to write to for "channel".
4038 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004039 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004040channel_part_send(channel_T *channel)
4041{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004042 if (channel->CH_SOCK_FD == INVALID_FD)
4043 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004044 return PART_SOCK;
4045}
4046
4047/*
4048 * Return the default "part" to read from for "channel".
4049 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004050 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004051channel_part_read(channel_T *channel)
4052{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004053 if (channel->CH_SOCK_FD == INVALID_FD)
4054 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004055 return PART_SOCK;
4056}
4057
4058/*
4059 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004060 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004061 */
4062 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004063channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004064{
Bram Moolenaar77073442016-02-13 23:23:53 +01004065 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004066 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004067 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004068}
4069
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004070/*
4071 * Return the timeout of "channel"/"part"
4072 */
4073 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004074channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004075{
4076 return channel->ch_part[part].ch_timeout;
4077}
4078
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004079 static int
4080handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4081{
4082 char_u *val = get_tv_string(item);
4083
4084 opt->jo_set |= jo;
4085 if (STRCMP(val, "nl") == 0)
4086 *modep = MODE_NL;
4087 else if (STRCMP(val, "raw") == 0)
4088 *modep = MODE_RAW;
4089 else if (STRCMP(val, "js") == 0)
4090 *modep = MODE_JS;
4091 else if (STRCMP(val, "json") == 0)
4092 *modep = MODE_JSON;
4093 else
4094 {
4095 EMSG2(_(e_invarg2), val);
4096 return FAIL;
4097 }
4098 return OK;
4099}
4100
4101 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004102handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004103{
4104 char_u *val = get_tv_string(item);
4105
4106 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4107 if (STRCMP(val, "null") == 0)
4108 opt->jo_io[part] = JIO_NULL;
4109 else if (STRCMP(val, "pipe") == 0)
4110 opt->jo_io[part] = JIO_PIPE;
4111 else if (STRCMP(val, "file") == 0)
4112 opt->jo_io[part] = JIO_FILE;
4113 else if (STRCMP(val, "buffer") == 0)
4114 opt->jo_io[part] = JIO_BUFFER;
4115 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4116 opt->jo_io[part] = JIO_OUT;
4117 else
4118 {
4119 EMSG2(_(e_invarg2), val);
4120 return FAIL;
4121 }
4122 return OK;
4123}
4124
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004125/*
4126 * Clear a jobopt_T before using it.
4127 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004128 void
4129clear_job_options(jobopt_T *opt)
4130{
4131 vim_memset(opt, 0, sizeof(jobopt_T));
4132}
4133
4134/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004135 * Free any members of a jobopt_T.
4136 */
4137 void
4138free_job_options(jobopt_T *opt)
4139{
4140 if (opt->jo_partial != NULL)
4141 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004142 else if (opt->jo_callback != NULL)
4143 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004144 if (opt->jo_out_partial != NULL)
4145 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004146 else if (opt->jo_out_cb != NULL)
4147 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004148 if (opt->jo_err_partial != NULL)
4149 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004150 else if (opt->jo_err_cb != NULL)
4151 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004152 if (opt->jo_close_partial != NULL)
4153 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004154 else if (opt->jo_close_cb != NULL)
4155 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004156 if (opt->jo_exit_partial != NULL)
4157 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004158 else if (opt->jo_exit_cb != NULL)
4159 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004160}
4161
4162/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004163 * Get the PART_ number from the first character of an option name.
4164 */
4165 static int
4166part_from_char(int c)
4167{
4168 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4169}
4170
4171/*
4172 * Get the option entries from the dict in "tv", parse them and put the result
4173 * in "opt".
4174 * Only accept options in "supported".
4175 * If an option value is invalid return FAIL.
4176 */
4177 int
4178get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4179{
4180 typval_T *item;
4181 char_u *val;
4182 dict_T *dict;
4183 int todo;
4184 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004185 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004186
4187 opt->jo_set = 0;
4188 if (tv->v_type == VAR_UNKNOWN)
4189 return OK;
4190 if (tv->v_type != VAR_DICT)
4191 {
4192 EMSG(_(e_invarg));
4193 return FAIL;
4194 }
4195 dict = tv->vval.v_dict;
4196 if (dict == NULL)
4197 return OK;
4198
4199 todo = (int)dict->dv_hashtab.ht_used;
4200 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4201 if (!HASHITEM_EMPTY(hi))
4202 {
4203 item = &dict_lookup(hi)->di_tv;
4204
4205 if (STRCMP(hi->hi_key, "mode") == 0)
4206 {
4207 if (!(supported & JO_MODE))
4208 break;
4209 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4210 return FAIL;
4211 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004212 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004213 {
4214 if (!(supported & JO_IN_MODE))
4215 break;
4216 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4217 == FAIL)
4218 return FAIL;
4219 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004220 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004221 {
4222 if (!(supported & JO_OUT_MODE))
4223 break;
4224 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4225 == FAIL)
4226 return FAIL;
4227 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004228 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004229 {
4230 if (!(supported & JO_ERR_MODE))
4231 break;
4232 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4233 == FAIL)
4234 return FAIL;
4235 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004236 else if (STRCMP(hi->hi_key, "in_io") == 0
4237 || STRCMP(hi->hi_key, "out_io") == 0
4238 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004239 {
4240 if (!(supported & JO_OUT_IO))
4241 break;
4242 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4243 return FAIL;
4244 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004245 else if (STRCMP(hi->hi_key, "in_name") == 0
4246 || STRCMP(hi->hi_key, "out_name") == 0
4247 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004248 {
4249 part = part_from_char(*hi->hi_key);
4250
4251 if (!(supported & JO_OUT_IO))
4252 break;
4253 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4254 opt->jo_io_name[part] =
4255 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4256 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004257 else if (STRCMP(hi->hi_key, "in_buf") == 0
4258 || STRCMP(hi->hi_key, "out_buf") == 0
4259 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004260 {
4261 part = part_from_char(*hi->hi_key);
4262
4263 if (!(supported & JO_OUT_IO))
4264 break;
4265 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4266 opt->jo_io_buf[part] = get_tv_number(item);
4267 if (opt->jo_io_buf[part] <= 0)
4268 {
4269 EMSG2(_(e_invarg2), get_tv_string(item));
4270 return FAIL;
4271 }
4272 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4273 {
4274 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4275 return FAIL;
4276 }
4277 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004278 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4279 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4280 {
4281 part = part_from_char(*hi->hi_key);
4282
4283 if (!(supported & JO_OUT_IO))
4284 break;
4285 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4286 opt->jo_modifiable[part] = get_tv_number(item);
4287 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004288 else if (STRCMP(hi->hi_key, "out_msg") == 0
4289 || STRCMP(hi->hi_key, "err_msg") == 0)
4290 {
4291 part = part_from_char(*hi->hi_key);
4292
4293 if (!(supported & JO_OUT_IO))
4294 break;
4295 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4296 opt->jo_message[part] = get_tv_number(item);
4297 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004298 else if (STRCMP(hi->hi_key, "in_top") == 0
4299 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004300 {
4301 linenr_T *lp;
4302
4303 if (!(supported & JO_OUT_IO))
4304 break;
4305 if (hi->hi_key[3] == 't')
4306 {
4307 lp = &opt->jo_in_top;
4308 opt->jo_set |= JO_IN_TOP;
4309 }
4310 else
4311 {
4312 lp = &opt->jo_in_bot;
4313 opt->jo_set |= JO_IN_BOT;
4314 }
4315 *lp = get_tv_number(item);
4316 if (*lp < 0)
4317 {
4318 EMSG2(_(e_invarg2), get_tv_string(item));
4319 return FAIL;
4320 }
4321 }
4322 else if (STRCMP(hi->hi_key, "channel") == 0)
4323 {
4324 if (!(supported & JO_OUT_IO))
4325 break;
4326 opt->jo_set |= JO_CHANNEL;
4327 if (item->v_type != VAR_CHANNEL)
4328 {
4329 EMSG2(_(e_invarg2), "channel");
4330 return FAIL;
4331 }
4332 opt->jo_channel = item->vval.v_channel;
4333 }
4334 else if (STRCMP(hi->hi_key, "callback") == 0)
4335 {
4336 if (!(supported & JO_CALLBACK))
4337 break;
4338 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004339 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004340 if (opt->jo_callback == NULL)
4341 {
4342 EMSG2(_(e_invarg2), "callback");
4343 return FAIL;
4344 }
4345 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004346 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004347 {
4348 if (!(supported & JO_OUT_CALLBACK))
4349 break;
4350 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004351 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004352 if (opt->jo_out_cb == NULL)
4353 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004354 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004355 return FAIL;
4356 }
4357 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004358 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004359 {
4360 if (!(supported & JO_ERR_CALLBACK))
4361 break;
4362 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004363 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004364 if (opt->jo_err_cb == NULL)
4365 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004366 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004367 return FAIL;
4368 }
4369 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004370 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004371 {
4372 if (!(supported & JO_CLOSE_CALLBACK))
4373 break;
4374 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004375 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004376 if (opt->jo_close_cb == NULL)
4377 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004378 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004379 return FAIL;
4380 }
4381 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004382 else if (STRCMP(hi->hi_key, "drop") == 0)
4383 {
4384 int never = FALSE;
4385 val = get_tv_string(item);
4386
4387 if (STRCMP(val, "never") == 0)
4388 never = TRUE;
4389 else if (STRCMP(val, "auto") != 0)
4390 {
4391 EMSG2(_(e_invarg2), "drop");
4392 return FAIL;
4393 }
4394 opt->jo_drop_never = never;
4395 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004396 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4397 {
4398 if (!(supported & JO_EXIT_CB))
4399 break;
4400 opt->jo_set |= JO_EXIT_CB;
4401 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4402 if (opt->jo_exit_cb == NULL)
4403 {
4404 EMSG2(_(e_invarg2), "exit_cb");
4405 return FAIL;
4406 }
4407 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004408 else if (STRCMP(hi->hi_key, "waittime") == 0)
4409 {
4410 if (!(supported & JO_WAITTIME))
4411 break;
4412 opt->jo_set |= JO_WAITTIME;
4413 opt->jo_waittime = get_tv_number(item);
4414 }
4415 else if (STRCMP(hi->hi_key, "timeout") == 0)
4416 {
4417 if (!(supported & JO_TIMEOUT))
4418 break;
4419 opt->jo_set |= JO_TIMEOUT;
4420 opt->jo_timeout = get_tv_number(item);
4421 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004422 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004423 {
4424 if (!(supported & JO_OUT_TIMEOUT))
4425 break;
4426 opt->jo_set |= JO_OUT_TIMEOUT;
4427 opt->jo_out_timeout = get_tv_number(item);
4428 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004429 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004430 {
4431 if (!(supported & JO_ERR_TIMEOUT))
4432 break;
4433 opt->jo_set |= JO_ERR_TIMEOUT;
4434 opt->jo_err_timeout = get_tv_number(item);
4435 }
4436 else if (STRCMP(hi->hi_key, "part") == 0)
4437 {
4438 if (!(supported & JO_PART))
4439 break;
4440 opt->jo_set |= JO_PART;
4441 val = get_tv_string(item);
4442 if (STRCMP(val, "err") == 0)
4443 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004444 else if (STRCMP(val, "out") == 0)
4445 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004446 else
4447 {
4448 EMSG2(_(e_invarg2), val);
4449 return FAIL;
4450 }
4451 }
4452 else if (STRCMP(hi->hi_key, "id") == 0)
4453 {
4454 if (!(supported & JO_ID))
4455 break;
4456 opt->jo_set |= JO_ID;
4457 opt->jo_id = get_tv_number(item);
4458 }
4459 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4460 {
4461 if (!(supported & JO_STOPONEXIT))
4462 break;
4463 opt->jo_set |= JO_STOPONEXIT;
4464 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4465 opt->jo_soe_buf);
4466 if (opt->jo_stoponexit == NULL)
4467 {
4468 EMSG2(_(e_invarg2), "stoponexit");
4469 return FAIL;
4470 }
4471 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004472 else if (STRCMP(hi->hi_key, "block_write") == 0)
4473 {
4474 if (!(supported & JO_BLOCK_WRITE))
4475 break;
4476 opt->jo_set |= JO_BLOCK_WRITE;
4477 opt->jo_block_write = get_tv_number(item);
4478 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004479 else
4480 break;
4481 --todo;
4482 }
4483 if (todo > 0)
4484 {
4485 EMSG2(_(e_invarg2), hi->hi_key);
4486 return FAIL;
4487 }
4488
4489 return OK;
4490}
4491
4492/*
4493 * Get the channel from the argument.
4494 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004495 * When "check_open" is TRUE check that the channel can be used.
4496 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004497 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004498 */
4499 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004500get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004501{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004502 channel_T *channel = NULL;
4503 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004504
4505 if (tv->v_type == VAR_JOB)
4506 {
4507 if (tv->vval.v_job != NULL)
4508 channel = tv->vval.v_job->jv_channel;
4509 }
4510 else if (tv->v_type == VAR_CHANNEL)
4511 {
4512 channel = tv->vval.v_channel;
4513 }
4514 else
4515 {
4516 EMSG2(_(e_invarg2), get_tv_string(tv));
4517 return NULL;
4518 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004519 if (channel != NULL && reading)
4520 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004521 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004522
Bram Moolenaar437905c2016-04-26 19:01:05 +02004523 if (check_open && (channel == NULL || (!channel_is_open(channel)
4524 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004525 {
4526 EMSG(_("E906: not an open channel"));
4527 return NULL;
4528 }
4529 return channel;
4530}
4531
4532static job_T *first_job = NULL;
4533
4534 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004535job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004536{
4537 ch_log(job->jv_channel, "Freeing job");
4538 if (job->jv_channel != NULL)
4539 {
4540 /* The link from the channel to the job doesn't count as a reference,
4541 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004542 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004543 * NULL the reference. We don't set ch_job_killed, unreferencing the
4544 * job doesn't mean it stops running. */
4545 job->jv_channel->ch_job = NULL;
4546 channel_unref(job->jv_channel);
4547 }
4548 mch_clear_job(job);
4549
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004550 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004551 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004552}
4553
4554 static void
4555job_free_job(job_T *job)
4556{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004557 if (job->jv_next != NULL)
4558 job->jv_next->jv_prev = job->jv_prev;
4559 if (job->jv_prev == NULL)
4560 first_job = job->jv_next;
4561 else
4562 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004563 vim_free(job);
4564}
4565
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004566 static void
4567job_free(job_T *job)
4568{
4569 if (!in_free_unref_items)
4570 {
4571 job_free_contents(job);
4572 job_free_job(job);
4573 }
4574}
4575
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004576#if defined(EXITFREE) || defined(PROTO)
4577 void
4578job_free_all(void)
4579{
4580 while (first_job != NULL)
4581 job_free(first_job);
4582}
4583#endif
4584
4585/*
4586 * Return TRUE if we need to check if the process of "job" has ended.
4587 */
4588 static int
4589job_need_end_check(job_T *job)
4590{
4591 return job->jv_status == JOB_STARTED
4592 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4593}
4594
4595/*
4596 * Return TRUE if the channel of "job" is still useful.
4597 */
4598 static int
4599job_channel_still_useful(job_T *job)
4600{
4601 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4602}
4603
4604/*
4605 * Return TRUE if the job should not be freed yet. Do not free the job when
4606 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4607 * or when the associated channel will do something with the job output.
4608 */
4609 static int
4610job_still_useful(job_T *job)
4611{
4612 return job_need_end_check(job) || job_channel_still_useful(job);
4613}
4614
4615/*
4616 * NOTE: Must call job_cleanup() only once right after the status of "job"
4617 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4618 * mch_detect_ended_job() returned non-NULL).
4619 */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004620 static void
4621job_cleanup(job_T *job)
4622{
4623 if (job->jv_status != JOB_ENDED)
4624 return;
4625
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004626 /* Ready to cleanup the job. */
4627 job->jv_status = JOB_FINISHED;
4628
Bram Moolenaar97792de2016-10-15 18:36:49 +02004629 if (job->jv_exit_cb != NULL)
4630 {
4631 typval_T argv[3];
4632 typval_T rettv;
4633 int dummy;
4634
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004635 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004636 ++job->jv_refcount;
4637 argv[0].v_type = VAR_JOB;
4638 argv[0].vval.v_job = job;
4639 argv[1].v_type = VAR_NUMBER;
4640 argv[1].vval.v_number = job->jv_exitval;
4641 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4642 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4643 job->jv_exit_partial, NULL);
4644 clear_tv(&rettv);
4645 --job->jv_refcount;
4646 channel_need_redraw = TRUE;
4647 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004648
4649 /* Do not free the job in case the close callback of the associated channel
4650 * isn't invoked yet and may get information by job_info(). */
4651 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02004652 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004653 /* The job was already unreferenced and the associated channel was
4654 * detached, now that it ended it can be freed. Careful: caller must
4655 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004656 job_free(job);
4657 }
4658}
4659
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004660/*
4661 * Mark references in jobs that are still useful.
4662 */
4663 int
4664set_ref_in_job(int copyID)
4665{
4666 int abort = FALSE;
4667 job_T *job;
4668 typval_T tv;
4669
4670 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004671 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004672 {
4673 tv.v_type = VAR_JOB;
4674 tv.vval.v_job = job;
4675 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4676 }
4677 return abort;
4678}
4679
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004680/*
4681 * Dereference "job". Note that after this "job" may have been freed.
4682 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004683 void
4684job_unref(job_T *job)
4685{
4686 if (job != NULL && --job->jv_refcount <= 0)
4687 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004688 /* Do not free the job if there is a channel where the close callback
4689 * may get the job info. */
4690 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004691 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004692 /* Do not free the job when it has not ended yet and there is a
4693 * "stoponexit" flag or an exit callback. */
4694 if (!job_need_end_check(job))
4695 {
4696 job_free(job);
4697 }
4698 else if (job->jv_channel != NULL)
4699 {
4700 /* Do remove the link to the channel, otherwise it hangs
4701 * around until Vim exits. See job_free() for refcount. */
4702 ch_log(job->jv_channel, "detaching channel from job");
4703 job->jv_channel->ch_job = NULL;
4704 channel_unref(job->jv_channel);
4705 job->jv_channel = NULL;
4706 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004707 }
4708 }
4709}
4710
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004711 int
4712free_unused_jobs_contents(int copyID, int mask)
4713{
4714 int did_free = FALSE;
4715 job_T *job;
4716
4717 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004718 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004719 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004720 {
4721 /* Free the channel and ordinary items it contains, but don't
4722 * recurse into Lists, Dictionaries etc. */
4723 job_free_contents(job);
4724 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004725 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004726 return did_free;
4727}
4728
4729 void
4730free_unused_jobs(int copyID, int mask)
4731{
4732 job_T *job;
4733 job_T *job_next;
4734
4735 for (job = first_job; job != NULL; job = job_next)
4736 {
4737 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004738 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004739 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004740 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004741 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004742 job_free_job(job);
4743 }
4744 }
4745}
4746
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004747/*
4748 * Allocate a job. Sets the refcount to one and sets options default.
4749 */
4750 static job_T *
4751job_alloc(void)
4752{
4753 job_T *job;
4754
4755 job = (job_T *)alloc_clear(sizeof(job_T));
4756 if (job != NULL)
4757 {
4758 job->jv_refcount = 1;
4759 job->jv_stoponexit = vim_strsave((char_u *)"term");
4760
4761 if (first_job != NULL)
4762 {
4763 first_job->jv_prev = job;
4764 job->jv_next = first_job;
4765 }
4766 first_job = job;
4767 }
4768 return job;
4769}
4770
4771 void
4772job_set_options(job_T *job, jobopt_T *opt)
4773{
4774 if (opt->jo_set & JO_STOPONEXIT)
4775 {
4776 vim_free(job->jv_stoponexit);
4777 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4778 job->jv_stoponexit = NULL;
4779 else
4780 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4781 }
4782 if (opt->jo_set & JO_EXIT_CB)
4783 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004784 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004785 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004786 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004787 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004788 job->jv_exit_partial = NULL;
4789 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004790 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004791 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004792 job->jv_exit_partial = opt->jo_exit_partial;
4793 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004794 {
4795 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004796 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004797 }
4798 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004799 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004800 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004801 func_ref(job->jv_exit_cb);
4802 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004803 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004804 }
4805}
4806
4807/*
4808 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4809 */
4810 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004811job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004812{
4813 job_T *job;
4814
4815 for (job = first_job; job != NULL; job = job->jv_next)
4816 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4817 mch_stop_job(job, job->jv_stoponexit);
4818}
4819
4820/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004821 * Return TRUE when there is any job that has an exit callback and might exit,
4822 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004823 */
4824 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004825has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004826{
4827 job_T *job;
4828
4829 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004830 /* Only should check if the channel has been closed, if the channel is
4831 * open the job won't exit. */
4832 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004833 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004834 return TRUE;
4835 return FALSE;
4836}
4837
Bram Moolenaar97792de2016-10-15 18:36:49 +02004838#define MAX_CHECK_ENDED 8
4839
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004840/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004841 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004842 */
4843 void
4844job_check_ended(void)
4845{
Bram Moolenaar97792de2016-10-15 18:36:49 +02004846 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004847
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004848 if (first_job == NULL)
4849 return;
4850
Bram Moolenaar97792de2016-10-15 18:36:49 +02004851 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004852 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004853 /* NOTE: mch_detect_ended_job() must only return a job of which the
4854 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004855 job_T *job = mch_detect_ended_job(first_job);
4856
4857 if (job == NULL)
4858 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004859 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004860 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02004861
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004862 if (channel_need_redraw)
4863 {
4864 channel_need_redraw = FALSE;
4865 redraw_after_callback();
4866 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004867}
4868
4869/*
4870 * "job_start()" function
4871 */
4872 job_T *
4873job_start(typval_T *argvars)
4874{
4875 job_T *job;
4876 char_u *cmd = NULL;
4877#if defined(UNIX)
4878# define USE_ARGV
4879 char **argv = NULL;
4880 int argc = 0;
4881#else
4882 garray_T ga;
4883#endif
4884 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004885 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004886
4887 job = job_alloc();
4888 if (job == NULL)
4889 return NULL;
4890
4891 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004892#ifndef USE_ARGV
4893 ga_init2(&ga, (int)sizeof(char*), 20);
4894#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004895
4896 /* Default mode is NL. */
4897 clear_job_options(&opt);
4898 opt.jo_mode = MODE_NL;
4899 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004900 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4901 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004902 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004903
4904 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004905 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004906 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4907 && opt.jo_io[part] == JIO_FILE
4908 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4909 || *opt.jo_io_name[part] == NUL))
4910 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004911 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004912 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004913 }
4914
4915 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4916 {
4917 buf_T *buf = NULL;
4918
4919 /* check that we can find the buffer before starting the job */
4920 if (opt.jo_set & JO_IN_BUF)
4921 {
4922 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4923 if (buf == NULL)
4924 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4925 }
4926 else if (!(opt.jo_set & JO_IN_NAME))
4927 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004928 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004929 }
4930 else
4931 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4932 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004933 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004934 if (buf->b_ml.ml_mfp == NULL)
4935 {
4936 char_u numbuf[NUMBUFLEN];
4937 char_u *s;
4938
4939 if (opt.jo_set & JO_IN_BUF)
4940 {
4941 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4942 s = numbuf;
4943 }
4944 else
4945 s = opt.jo_io_name[PART_IN];
4946 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004947 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004948 }
4949 job->jv_in_buf = buf;
4950 }
4951
4952 job_set_options(job, &opt);
4953
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004954 if (argvars[0].v_type == VAR_STRING)
4955 {
4956 /* Command is a string. */
4957 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004958 if (cmd == NULL || *cmd == NUL)
4959 {
4960 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004961 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004962 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004963#ifdef USE_ARGV
4964 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004965 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004966 argv[argc] = NULL;
4967#endif
4968 }
4969 else if (argvars[0].v_type != VAR_LIST
4970 || argvars[0].vval.v_list == NULL
4971 || argvars[0].vval.v_list->lv_len < 1)
4972 {
4973 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004974 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004975 }
4976 else
4977 {
4978 list_T *l = argvars[0].vval.v_list;
4979 listitem_T *li;
4980 char_u *s;
4981
4982#ifdef USE_ARGV
4983 /* Pass argv[] to mch_call_shell(). */
4984 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4985 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004986 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004987#endif
4988 for (li = l->lv_first; li != NULL; li = li->li_next)
4989 {
4990 s = get_tv_string_chk(&li->li_tv);
4991 if (s == NULL)
4992 goto theend;
4993#ifdef USE_ARGV
4994 argv[argc++] = (char *)s;
4995#else
4996 /* Only escape when needed, double quotes are not always allowed. */
4997 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4998 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004999# ifdef WIN32
5000 int old_ssl = p_ssl;
5001
5002 /* This is using CreateProcess, not cmd.exe. Always use
5003 * double quote and backslashes. */
5004 p_ssl = 0;
5005# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005006 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005007# ifdef WIN32
5008 p_ssl = old_ssl;
5009# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005010 if (s == NULL)
5011 goto theend;
5012 ga_concat(&ga, s);
5013 vim_free(s);
5014 }
5015 else
5016 ga_concat(&ga, s);
5017 if (li->li_next != NULL)
5018 ga_append(&ga, ' ');
5019#endif
5020 }
5021#ifdef USE_ARGV
5022 argv[argc] = NULL;
5023#else
5024 cmd = ga.ga_data;
5025#endif
5026 }
5027
5028#ifdef USE_ARGV
5029 if (ch_log_active())
5030 {
5031 garray_T ga;
5032 int i;
5033
5034 ga_init2(&ga, (int)sizeof(char), 200);
5035 for (i = 0; i < argc; ++i)
5036 {
5037 if (i > 0)
5038 ga_concat(&ga, (char_u *)" ");
5039 ga_concat(&ga, (char_u *)argv[i]);
5040 }
5041 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
5042 ga_clear(&ga);
5043 }
5044 mch_start_job(argv, job, &opt);
5045#else
5046 ch_logs(NULL, "Starting job: %s", (char *)cmd);
5047 mch_start_job((char *)cmd, job, &opt);
5048#endif
5049
5050 /* If the channel is reading from a buffer, write lines now. */
5051 if (job->jv_channel != NULL)
5052 channel_write_in(job->jv_channel);
5053
5054theend:
5055#ifdef USE_ARGV
5056 vim_free(argv);
5057#else
5058 vim_free(ga.ga_data);
5059#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005060 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005061 return job;
5062}
5063
5064/*
5065 * Get the status of "job" and invoke the exit callback when needed.
5066 * The returned string is not allocated.
5067 */
5068 char *
5069job_status(job_T *job)
5070{
5071 char *result;
5072
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005073 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005074 /* No need to check, dead is dead. */
5075 result = "dead";
5076 else if (job->jv_status == JOB_FAILED)
5077 result = "fail";
5078 else
5079 {
5080 result = mch_job_status(job);
5081 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005082 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005083 }
5084 return result;
5085}
5086
Bram Moolenaar8950a562016-03-12 15:22:55 +01005087/*
5088 * Implementation of job_info().
5089 */
5090 void
5091job_info(job_T *job, dict_T *dict)
5092{
5093 dictitem_T *item;
5094 varnumber_T nr;
5095
5096 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5097
5098 item = dictitem_alloc((char_u *)"channel");
5099 if (item == NULL)
5100 return;
5101 item->di_tv.v_lock = 0;
5102 item->di_tv.v_type = VAR_CHANNEL;
5103 item->di_tv.vval.v_channel = job->jv_channel;
5104 if (job->jv_channel != NULL)
5105 ++job->jv_channel->ch_refcount;
5106 if (dict_add(dict, item) == FAIL)
5107 dictitem_free(item);
5108
5109#ifdef UNIX
5110 nr = job->jv_pid;
5111#else
5112 nr = job->jv_proc_info.dwProcessId;
5113#endif
5114 dict_add_nr_str(dict, "process", nr, NULL);
5115
5116 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005117 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005118 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5119}
5120
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005121 int
5122job_stop(job_T *job, typval_T *argvars)
5123{
5124 char_u *arg;
5125
5126 if (argvars[1].v_type == VAR_UNKNOWN)
5127 arg = (char_u *)"";
5128 else
5129 {
5130 arg = get_tv_string_chk(&argvars[1]);
5131 if (arg == NULL)
5132 {
5133 EMSG(_(e_invarg));
5134 return 0;
5135 }
5136 }
5137 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
5138 if (mch_stop_job(job, arg) == FAIL)
5139 return 0;
5140
5141 /* Assume that "hup" does not kill the job. */
5142 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
5143 job->jv_channel->ch_job_killed = TRUE;
5144
5145 /* We don't try freeing the job, obviously the caller still has a
5146 * reference to it. */
5147 return 1;
5148}
5149
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005150#endif /* FEAT_JOB_CHANNEL */