blob: be25f5ef1d44a9fc63cb6a07ebb16e390b8f6a93 [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
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200174 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
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001016 {
1017 /* When using a pty the same FD is set on multiple parts, only
1018 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001019 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1020 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1021 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001022 fd_close(*fd);
1023 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001024 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001025
1026 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001027 }
1028}
1029
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001030 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001031channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001032{
Bram Moolenaarde279892016-03-11 22:19:44 +01001033 if (in != INVALID_FD)
1034 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001035 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001036 channel->CH_IN_FD = in;
1037 }
1038 if (out != INVALID_FD)
1039 {
1040# if defined(FEAT_GUI)
1041 channel_gui_unregister_one(channel, PART_OUT);
1042# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001043 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001044 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001045 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001046# if defined(FEAT_GUI)
1047 channel_gui_register_one(channel, PART_OUT);
1048# endif
1049 }
1050 if (err != INVALID_FD)
1051 {
1052# if defined(FEAT_GUI)
1053 channel_gui_unregister_one(channel, PART_ERR);
1054# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001055 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001056 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001057 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001058# if defined(FEAT_GUI)
1059 channel_gui_register_one(channel, PART_ERR);
1060# endif
1061 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001062}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001063
Bram Moolenaard6051b52016-02-28 15:49:03 +01001064/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001065 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001066 * This does not keep a refcount, when the job is freed ch_job is cleared.
1067 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001068 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001069channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001070{
Bram Moolenaar77073442016-02-13 23:23:53 +01001071 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001072
1073 channel_set_options(channel, options);
1074
1075 if (job->jv_in_buf != NULL)
1076 {
1077 chanpart_T *in_part = &channel->ch_part[PART_IN];
1078
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001079 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001080 ch_logs(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001081 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001082 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001083 {
1084 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1085 {
1086 /* Special mode: send last-but-one line when appending a line
1087 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001088 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001089 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001090 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001091 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001092 }
1093 else
1094 in_part->ch_buf_top = options->jo_in_top;
1095 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001096 else
1097 in_part->ch_buf_top = 1;
1098 if (options->jo_set & JO_IN_BOT)
1099 in_part->ch_buf_bot = options->jo_in_bot;
1100 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001101 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001102 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001103}
1104
1105/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001106 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001107 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001108 */
1109 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001110find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001111{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001112 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001113 buf_T *save_curbuf = curbuf;
1114
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001115 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001116 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001117 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001118 if (buf == NULL)
1119 buf = buflist_findname_exp(name);
1120 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001121 if (buf == NULL)
1122 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001123 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001124 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001125 if (buf == NULL)
1126 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001127 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001128 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001129#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001130 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1131 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001132#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001133 if (curbuf->b_ml.ml_mfp == NULL)
1134 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001135 if (msg)
1136 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001137 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001138 changed_bytes(1, 0);
1139 curbuf = save_curbuf;
1140 }
1141
1142 return buf;
1143}
1144
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001145 static void
1146set_callback(
1147 char_u **cbp,
1148 partial_T **pp,
1149 char_u *callback,
1150 partial_T *partial)
1151{
1152 free_callback(*cbp, *pp);
1153 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001154 {
1155 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001156 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001157 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001158 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001159 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001160 func_ref(*cbp);
1161 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001162 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001163 else
1164 *cbp = NULL;
1165 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001166 if (partial != NULL)
1167 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001168}
1169
Bram Moolenaar187db502016-02-27 14:44:26 +01001170/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001171 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001172 */
1173 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001174channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001175{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001176 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001177
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001178 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001179 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001180 channel->ch_part[part].ch_mode = opt->jo_mode;
1181 if (opt->jo_set & JO_IN_MODE)
1182 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1183 if (opt->jo_set & JO_OUT_MODE)
1184 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1185 if (opt->jo_set & JO_ERR_MODE)
1186 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001187
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001188 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001189 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001190 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1191 if (opt->jo_set & JO_OUT_TIMEOUT)
1192 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1193 if (opt->jo_set & JO_ERR_TIMEOUT)
1194 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001195 if (opt->jo_set & JO_BLOCK_WRITE)
1196 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001197
1198 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001199 set_callback(&channel->ch_callback, &channel->ch_partial,
1200 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001201 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001202 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1203 &channel->ch_part[PART_OUT].ch_partial,
1204 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001205 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001206 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1207 &channel->ch_part[PART_ERR].ch_partial,
1208 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001209 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001210 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1211 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001212 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001213
1214 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1215 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001216 buf_T *buf;
1217
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001218 /* writing output to a buffer. Default mode is NL. */
1219 if (!(opt->jo_set & JO_OUT_MODE))
1220 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001221 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001222 {
1223 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1224 if (buf == NULL)
1225 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1226 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001227 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001228 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001229 int msg = TRUE;
1230
1231 if (opt->jo_set2 & JO2_OUT_MSG)
1232 msg = opt->jo_message[PART_OUT];
1233 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001234 }
1235 if (buf != NULL)
1236 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001237 if (opt->jo_set & JO_OUT_MODIFIABLE)
1238 channel->ch_part[PART_OUT].ch_nomodifiable =
1239 !opt->jo_modifiable[PART_OUT];
1240
1241 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1242 {
1243 EMSG(_(e_modifiable));
1244 }
1245 else
1246 {
1247 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001248 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001249 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001250 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001251 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001252 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001253
1254 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1255 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1256 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1257 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001258 buf_T *buf;
1259
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001260 /* writing err to a buffer. Default mode is NL. */
1261 if (!(opt->jo_set & JO_ERR_MODE))
1262 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1263 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001264 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001265 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001266 {
1267 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1268 if (buf == NULL)
1269 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1270 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001271 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001272 {
1273 int msg = TRUE;
1274
1275 if (opt->jo_set2 & JO2_ERR_MSG)
1276 msg = opt->jo_message[PART_ERR];
1277 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1278 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001279 if (buf != NULL)
1280 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001281 if (opt->jo_set & JO_ERR_MODIFIABLE)
1282 channel->ch_part[PART_ERR].ch_nomodifiable =
1283 !opt->jo_modifiable[PART_ERR];
1284 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1285 {
1286 EMSG(_(e_modifiable));
1287 }
1288 else
1289 {
1290 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001291 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001292 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001293 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001294 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001295 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001296
1297 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1298 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1299 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001300}
1301
1302/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001303 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001304 */
1305 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001306channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001307 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001308 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001309 char_u *callback,
1310 partial_T *partial,
1311 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001312{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001313 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001314 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1315
1316 if (item != NULL)
1317 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001318 item->cq_partial = partial;
1319 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001320 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001321 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001322 item->cq_callback = callback;
1323 }
1324 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001325 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001326 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001327 func_ref(item->cq_callback);
1328 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001329 item->cq_seq_nr = id;
1330 item->cq_prev = head->cq_prev;
1331 head->cq_prev = item;
1332 item->cq_next = NULL;
1333 if (item->cq_prev == NULL)
1334 head->cq_next = item;
1335 else
1336 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001337 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001338}
1339
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001340 static void
1341write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1342{
1343 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001344 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001345 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001346 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001347
Bram Moolenaar655da312016-05-28 22:22:34 +02001348 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001349 if ((p = alloc(len + 2)) == NULL)
1350 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001351 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001352
1353 for (i = 0; i < len; ++i)
1354 if (p[i] == NL)
1355 p[i] = NUL;
1356
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001357 p[len] = NL;
1358 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001359 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001360 vim_free(p);
1361}
1362
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001363/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001364 * Return TRUE if "channel" can be written to.
1365 * Returns FALSE if the input is closed or the write would block.
1366 */
1367 static int
1368can_write_buf_line(channel_T *channel)
1369{
1370 chanpart_T *in_part = &channel->ch_part[PART_IN];
1371
1372 if (in_part->ch_fd == INVALID_FD)
1373 return FALSE; /* pipe was closed */
1374
1375 /* for testing: block every other attempt to write */
1376 if (in_part->ch_block_write == 1)
1377 in_part->ch_block_write = -1;
1378 else if (in_part->ch_block_write == -1)
1379 in_part->ch_block_write = 1;
1380
1381 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1382#ifndef WIN32
1383 {
1384# if defined(HAVE_SELECT)
1385 struct timeval tval;
1386 fd_set wfds;
1387 int ret;
1388
1389 FD_ZERO(&wfds);
1390 FD_SET((int)in_part->ch_fd, &wfds);
1391 tval.tv_sec = 0;
1392 tval.tv_usec = 0;
1393 for (;;)
1394 {
1395 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1396# ifdef EINTR
1397 SOCK_ERRNO;
1398 if (ret == -1 && errno == EINTR)
1399 continue;
1400# endif
1401 if (ret <= 0 || in_part->ch_block_write == 1)
1402 {
1403 if (ret > 0)
1404 ch_log(channel, "FAKED Input not ready for writing");
1405 else
1406 ch_log(channel, "Input not ready for writing");
1407 return FALSE;
1408 }
1409 break;
1410 }
1411# else
1412 struct pollfd fds;
1413
1414 fds.fd = in_part->ch_fd;
1415 fds.events = POLLOUT;
1416 if (poll(&fds, 1, 0) <= 0)
1417 {
1418 ch_log(channel, "Input not ready for writing");
1419 return FALSE;
1420 }
1421 if (in_part->ch_block_write == 1)
1422 {
1423 ch_log(channel, "FAKED Input not ready for writing");
1424 return FALSE;
1425 }
1426# endif
1427 }
1428#endif
1429 return TRUE;
1430}
1431
1432/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001433 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001434 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001435 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001436channel_write_in(channel_T *channel)
1437{
1438 chanpart_T *in_part = &channel->ch_part[PART_IN];
1439 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001440 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001441 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001442
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001443 if (buf == NULL || in_part->ch_buf_append)
1444 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001445 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001446 {
1447 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001448 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001449 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001450 return;
1451 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001452
1453 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1454 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1455 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001456 if (!can_write_buf_line(channel))
1457 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001458 write_buf_line(buf, lnum, channel);
1459 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001460 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001461
1462 if (written == 1)
1463 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1464 else if (written > 1)
1465 ch_logn(channel, "written %d lines to channel", written);
1466
Bram Moolenaar014069a2016-03-03 22:51:40 +01001467 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001468 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001469 {
1470 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001471 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001472 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001473
1474 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001475 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001476 }
1477 else
1478 ch_logn(channel, "Still %d more lines to write",
1479 buf->b_ml.ml_line_count - lnum + 1);
1480}
1481
1482/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001483 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001484 */
1485 void
1486channel_buffer_free(buf_T *buf)
1487{
1488 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001489 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001490
1491 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001492 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001493 {
1494 chanpart_T *ch_part = &channel->ch_part[part];
1495
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001496 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001497 {
1498 ch_logs(channel, "%s buffer has been wiped out",
1499 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001500 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001501 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001502 }
1503}
1504
1505/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001506 * Write any lines waiting to be written to a channel.
1507 */
1508 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001509channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001510{
1511 channel_T *channel;
1512
1513 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1514 {
1515 chanpart_T *in_part = &channel->ch_part[PART_IN];
1516
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001517 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001518 {
1519 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001520 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001521 else
1522 channel_write_in(channel);
1523 }
1524 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001525}
1526
1527/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001528 * Write appended lines above the last one in "buf" to the channel.
1529 */
1530 void
1531channel_write_new_lines(buf_T *buf)
1532{
1533 channel_T *channel;
1534 int found_one = FALSE;
1535
1536 /* There could be more than one channel for the buffer, loop over all of
1537 * them. */
1538 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1539 {
1540 chanpart_T *in_part = &channel->ch_part[PART_IN];
1541 linenr_T lnum;
1542 int written = 0;
1543
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001544 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001545 {
1546 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001547 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001548 found_one = TRUE;
1549 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1550 ++lnum)
1551 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001552 if (!can_write_buf_line(channel))
1553 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001554 write_buf_line(buf, lnum, channel);
1555 ++written;
1556 }
1557
1558 if (written == 1)
1559 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1560 else if (written > 1)
1561 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001562 if (lnum < buf->b_ml.ml_line_count)
1563 ch_logn(channel, "Still %d more lines to write",
1564 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001565
1566 in_part->ch_buf_bot = lnum;
1567 }
1568 }
1569 if (!found_one)
1570 buf->b_write_to_channel = FALSE;
1571}
1572
1573/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001574 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001575 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001576 */
1577 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001578invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1579 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001580{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001581 typval_T rettv;
1582 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001583
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001584 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001585 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001586
Bram Moolenaar77073442016-02-13 23:23:53 +01001587 argv[0].v_type = VAR_CHANNEL;
1588 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001589
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001590 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1591 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001592 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001593 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001594}
1595
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001596/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001597 * Return the first node from "channel"/"part" without removing it.
1598 * Returns NULL if there is nothing.
1599 */
1600 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001601channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001602{
1603 readq_T *head = &channel->ch_part[part].ch_head;
1604
1605 return head->rq_next;
1606}
1607
1608/*
1609 * Return a pointer to the first NL in "node".
1610 * Skips over NUL characters.
1611 * Returns NULL if there is no NL.
1612 */
1613 char_u *
1614channel_first_nl(readq_T *node)
1615{
1616 char_u *buffer = node->rq_buffer;
1617 long_u i;
1618
1619 for (i = 0; i < node->rq_buflen; ++i)
1620 if (buffer[i] == NL)
1621 return buffer + i;
1622 return NULL;
1623}
1624
1625/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001626 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001627 * The caller must free it.
1628 * Returns NULL if there is nothing.
1629 */
1630 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001631channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001632{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001633 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001634 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001635 char_u *p;
1636
Bram Moolenaar77073442016-02-13 23:23:53 +01001637 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001638 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001639 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001640 p = node->rq_buffer;
1641 head->rq_next = node->rq_next;
1642 if (node->rq_next == NULL)
1643 head->rq_prev = NULL;
1644 else
1645 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001646 vim_free(node);
1647 return p;
1648}
1649
1650/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001651 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001652 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001653 */
1654 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001655channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001656{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001657 readq_T *head = &channel->ch_part[part].ch_head;
1658 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001659 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001660 char_u *res;
1661 char_u *p;
1662
1663 /* If there is only one buffer just get that one. */
1664 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1665 return channel_get(channel, part);
1666
1667 /* Concatenate everything into one buffer. */
1668 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001669 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001670 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001671 if (res == NULL)
1672 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001673 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001674 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001675 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001676 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001677 p += node->rq_buflen;
1678 }
1679 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001680
1681 /* Free all buffers */
1682 do
1683 {
1684 p = channel_get(channel, part);
1685 vim_free(p);
1686 } while (p != NULL);
1687
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001688 /* turn all NUL into NL */
1689 while (len > 0)
1690 {
1691 --len;
1692 if (res[len] == NUL)
1693 res[len] = NL;
1694 }
1695
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001696 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001697}
1698
1699/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001700 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001701 * Caller must check these bytes are available.
1702 */
1703 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001704channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001705{
1706 readq_T *head = &channel->ch_part[part].ch_head;
1707 readq_T *node = head->rq_next;
1708 char_u *buf = node->rq_buffer;
1709
1710 mch_memmove(buf, buf + len, node->rq_buflen - len);
1711 node->rq_buflen -= len;
1712}
1713
1714/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001715 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001716 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001717 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001718 */
1719 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001720channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001721{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001722 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001723 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001724 readq_T *last_node;
1725 readq_T *n;
1726 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001727 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001728 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001729
Bram Moolenaar77073442016-02-13 23:23:53 +01001730 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001731 return FAIL;
1732
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001733 last_node = node->rq_next;
1734 len = node->rq_buflen + last_node->rq_buflen + 1;
1735 if (want_nl)
1736 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001737 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001738 {
1739 last_node = last_node->rq_next;
1740 len += last_node->rq_buflen;
1741 }
1742
1743 p = newbuf = alloc(len);
1744 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001745 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001746 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001747 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001748 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001749 node->rq_buffer = newbuf;
1750 for (n = node; n != last_node; )
1751 {
1752 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001753 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001754 p += n->rq_buflen;
1755 vim_free(n->rq_buffer);
1756 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001757 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001758
1759 /* dispose of the collapsed nodes and their buffers */
1760 for (n = node->rq_next; n != last_node; )
1761 {
1762 n = n->rq_next;
1763 vim_free(n->rq_prev);
1764 }
1765 node->rq_next = last_node->rq_next;
1766 if (last_node->rq_next == NULL)
1767 head->rq_prev = node;
1768 else
1769 last_node->rq_next->rq_prev = node;
1770 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001771 return OK;
1772}
1773
1774/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001775 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001776 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001777 * Returns OK or FAIL.
1778 */
1779 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001780channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001781 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001782{
1783 readq_T *node;
1784 readq_T *head = &channel->ch_part[part].ch_head;
1785 char_u *p;
1786 int i;
1787
1788 node = (readq_T *)alloc(sizeof(readq_T));
1789 if (node == NULL)
1790 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001791 /* A NUL is added at the end, because netbeans code expects that.
1792 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001793 node->rq_buffer = alloc(len + 1);
1794 if (node->rq_buffer == NULL)
1795 {
1796 vim_free(node);
1797 return FAIL; /* out of memory */
1798 }
1799
1800 if (channel->ch_part[part].ch_mode == MODE_NL)
1801 {
1802 /* Drop any CR before a NL. */
1803 p = node->rq_buffer;
1804 for (i = 0; i < len; ++i)
1805 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1806 *p++ = buf[i];
1807 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001808 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001809 }
1810 else
1811 {
1812 mch_memmove(node->rq_buffer, buf, len);
1813 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001814 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001815 }
1816
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001817 if (prepend)
1818 {
1819 /* preend node to the head of the queue */
1820 node->rq_next = head->rq_next;
1821 node->rq_prev = NULL;
1822 if (head->rq_next == NULL)
1823 head->rq_prev = node;
1824 else
1825 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001826 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001827 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001828 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001829 {
1830 /* append node to the tail of the queue */
1831 node->rq_next = NULL;
1832 node->rq_prev = head->rq_prev;
1833 if (head->rq_prev == NULL)
1834 head->rq_next = node;
1835 else
1836 head->rq_prev->rq_next = node;
1837 head->rq_prev = node;
1838 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001839
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001840 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001841 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001842 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843 fprintf(log_fd, "'");
1844 if (fwrite(buf, len, 1, log_fd) != 1)
1845 return FAIL;
1846 fprintf(log_fd, "'\n");
1847 }
1848 return OK;
1849}
1850
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001851/*
1852 * Try to fill the buffer of "reader".
1853 * Returns FALSE when nothing was added.
1854 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001855 static int
1856channel_fill(js_read_T *reader)
1857{
1858 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001859 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001860 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001861 int keeplen;
1862 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001863 char_u *p;
1864
1865 if (next == NULL)
1866 return FALSE;
1867
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001868 keeplen = reader->js_end - reader->js_buf;
1869 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001870 {
1871 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001872 addlen = (int)STRLEN(next);
1873 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001874 if (p == NULL)
1875 {
1876 vim_free(next);
1877 return FALSE;
1878 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001879 mch_memmove(p, reader->js_buf, keeplen);
1880 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001881 vim_free(next);
1882 next = p;
1883 }
1884
1885 vim_free(reader->js_buf);
1886 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001887 return TRUE;
1888}
1889
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001890/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001891 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001892 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001893 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001894 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001895 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001896channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001897{
1898 js_read_T reader;
1899 typval_T listtv;
1900 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001901 chanpart_T *chanpart = &channel->ch_part[part];
1902 jsonq_T *head = &chanpart->ch_json_head;
1903 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001904 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001905
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001906 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001907 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001908
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001909 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001910 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001911 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001912 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001913 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001914
1915 /* When a message is incomplete we wait for a short while for more to
1916 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001917 * or list will make us hang.
1918 * Do not generate error messages, they will be written in a channel log. */
1919 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001920 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001921 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001922 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001923 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001924 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001925 /* Only accept the response when it is a list with at least two
1926 * items. */
1927 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001928 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001929 if (listtv.v_type != VAR_LIST)
1930 ch_error(channel, "Did not receive a list, discarding");
1931 else
1932 ch_errorn(channel, "Expected list with two items, got %d",
1933 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001934 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001935 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001936 else
1937 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001938 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1939 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001940 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001941 else
1942 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001943 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001944 item->jq_value = alloc_tv();
1945 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001946 {
1947 vim_free(item);
1948 clear_tv(&listtv);
1949 }
1950 else
1951 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001952 *item->jq_value = listtv;
1953 item->jq_prev = head->jq_prev;
1954 head->jq_prev = item;
1955 item->jq_next = NULL;
1956 if (item->jq_prev == NULL)
1957 head->jq_next = item;
1958 else
1959 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001960 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001961 }
1962 }
1963 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001964
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001965 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001966 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001967 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001968 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001969 size_t buflen = STRLEN(reader.js_buf);
1970
1971 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001972 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001973 /* First time encountering incomplete message or after receiving
1974 * more (but still incomplete): set a deadline of 100 msec. */
1975 ch_logn(channel,
1976 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001977 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001978 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001979 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001980#ifdef WIN32
1981 chanpart->ch_deadline = GetTickCount() + 100L;
1982#else
1983 gettimeofday(&chanpart->ch_deadline, NULL);
1984 chanpart->ch_deadline.tv_usec += 100 * 1000;
1985 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1986 {
1987 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1988 ++chanpart->ch_deadline.tv_sec;
1989 }
1990#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001991 }
1992 else
1993 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001994 int timeout;
1995#ifdef WIN32
1996 timeout = GetTickCount() > chanpart->ch_deadline;
1997#else
1998 {
1999 struct timeval now_tv;
2000
2001 gettimeofday(&now_tv, NULL);
2002 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2003 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2004 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2005 }
2006#endif
2007 if (timeout)
2008 {
2009 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002010 chanpart->ch_wait_len = 0;
2011 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002012 }
2013 else
2014 {
2015 reader.js_used = 0;
2016 ch_log(channel, "still waiting on incomplete message");
2017 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002018 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002019 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002020
2021 if (status == FAIL)
2022 {
2023 ch_error(channel, "Decoding failed - discarding input");
2024 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002025 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002026 }
2027 else if (reader.js_buf[reader.js_used] != NUL)
2028 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002029 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002030 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002031 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2032 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002033 ret = status == MAYBE ? FALSE: TRUE;
2034 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002035 else
2036 ret = FALSE;
2037
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002038 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002039 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002040}
2041
2042/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002043 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002044 */
2045 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002046remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002047{
Bram Moolenaar77073442016-02-13 23:23:53 +01002048 if (node->cq_prev == NULL)
2049 head->cq_next = node->cq_next;
2050 else
2051 node->cq_prev->cq_next = node->cq_next;
2052 if (node->cq_next == NULL)
2053 head->cq_prev = node->cq_prev;
2054 else
2055 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002056}
2057
2058/*
2059 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002060 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002061 */
2062 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002063remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002064{
Bram Moolenaar77073442016-02-13 23:23:53 +01002065 if (node->jq_prev == NULL)
2066 head->jq_next = node->jq_next;
2067 else
2068 node->jq_prev->jq_next = node->jq_next;
2069 if (node->jq_next == NULL)
2070 head->jq_prev = node->jq_prev;
2071 else
2072 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002073 vim_free(node);
2074}
2075
2076/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002077 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002078 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002079 * When "id" is zero or negative jut get the first message. But not the one
2080 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002081 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002082 * Return OK when found and return the value in "rettv".
2083 * Return FAIL otherwise.
2084 */
2085 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002086channel_get_json(
2087 channel_T *channel,
2088 ch_part_T part,
2089 int id,
2090 int without_callback,
2091 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002092{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002093 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002094 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002095
Bram Moolenaar77073442016-02-13 23:23:53 +01002096 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002097 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002098 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002099 typval_T *tv = &l->lv_first->li_tv;
2100
Bram Moolenaar958dc692016-12-01 15:34:12 +01002101 if ((without_callback || !item->jq_no_callback)
2102 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002103 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002104 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002105 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002106 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002107 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002108 if (tv->v_type == VAR_NUMBER)
2109 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002110 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002111 return OK;
2112 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002113 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002114 }
2115 return FAIL;
2116}
2117
Bram Moolenaar958dc692016-12-01 15:34:12 +01002118/*
2119 * Put back "rettv" into the JSON queue, there was no callback for it.
2120 * Takes over the values in "rettv".
2121 */
2122 static void
2123channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2124{
2125 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2126 jsonq_T *item = head->jq_next;
2127 jsonq_T *newitem;
2128
2129 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2130 /* last item was pushed back, append to the end */
2131 item = NULL;
2132 else while (item != NULL && item->jq_no_callback)
2133 /* append after the last item that was pushed back */
2134 item = item->jq_next;
2135
2136 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2137 if (newitem == NULL)
2138 clear_tv(rettv);
2139 else
2140 {
2141 newitem->jq_value = alloc_tv();
2142 if (newitem->jq_value == NULL)
2143 {
2144 vim_free(newitem);
2145 clear_tv(rettv);
2146 }
2147 else
2148 {
2149 newitem->jq_no_callback = FALSE;
2150 *newitem->jq_value = *rettv;
2151 if (item == NULL)
2152 {
2153 /* append to the end */
2154 newitem->jq_prev = head->jq_prev;
2155 head->jq_prev = newitem;
2156 newitem->jq_next = NULL;
2157 if (newitem->jq_prev == NULL)
2158 head->jq_next = newitem;
2159 else
2160 newitem->jq_prev->jq_next = newitem;
2161 }
2162 else
2163 {
2164 /* append after "item" */
2165 newitem->jq_prev = item;
2166 newitem->jq_next = item->jq_next;
2167 item->jq_next = newitem;
2168 if (newitem->jq_next == NULL)
2169 head->jq_prev = newitem;
2170 else
2171 newitem->jq_next->jq_prev = newitem;
2172 }
2173 }
2174 }
2175}
2176
Bram Moolenaarece61b02016-02-20 21:39:05 +01002177#define CH_JSON_MAX_ARGS 4
2178
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002179/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002180 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002181 * "argv[0]" is the command string.
2182 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002183 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002184 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002185channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002186{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002187 char_u *cmd = argv[0].vval.v_string;
2188 char_u *arg;
2189 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002190
Bram Moolenaarece61b02016-02-20 21:39:05 +01002191 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002192 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002193 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002194 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002195 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002196 return;
2197 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002198 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002199 if (arg == NULL)
2200 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002201
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002202 if (STRCMP(cmd, "ex") == 0)
2203 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002204 int save_called_emsg = called_emsg;
2205
2206 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002207 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002208 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002209 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002210 --emsg_silent;
2211 if (called_emsg)
2212 ch_logs(channel, "Ex command error: '%s'",
2213 (char *)get_vim_var_str(VV_ERRMSG));
2214 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002215 }
2216 else if (STRCMP(cmd, "normal") == 0)
2217 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002218 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002219
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002220 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002221 ea.arg = arg;
2222 ea.addr_count = 0;
2223 ea.forceit = TRUE; /* no mapping */
2224 ex_normal(&ea);
2225 }
2226 else if (STRCMP(cmd, "redraw") == 0)
2227 {
2228 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002229
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002230 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002231 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002232 ex_redraw(&ea);
2233 showruler(FALSE);
2234 setcursor();
2235 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002236#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002237 if (gui.in_use)
2238 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002239 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002240 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002241 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002242#endif
2243 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002244 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002245 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002246 int is_call = cmd[0] == 'c';
2247 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002248
Bram Moolenaarece61b02016-02-20 21:39:05 +01002249 if (argv[id_idx].v_type != VAR_UNKNOWN
2250 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002251 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002252 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002253 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002254 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002255 }
2256 else if (is_call && argv[2].v_type != VAR_LIST)
2257 {
2258 ch_error(channel, "third argument for call must be a list");
2259 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002260 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002261 }
2262 else
2263 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002264 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002265 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002266 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002267 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002268
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002269 /* Don't pollute the display with errors. */
2270 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002271 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002272 {
2273 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002274 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002275 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002276 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002277 {
2278 ch_logs(channel, "Calling '%s'", (char *)arg);
2279 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2280 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002281 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002282
2283 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002284 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002285 int id = argv[id_idx].vval.v_number;
2286
Bram Moolenaar55fab432016-02-07 16:53:13 +01002287 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002288 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002289 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002290 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002291 /* If evaluation failed or the result can't be encoded
2292 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002293 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002294 err_tv.v_type = VAR_STRING;
2295 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002296 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002297 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002298 if (json != NULL)
2299 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002300 channel_send(channel,
2301 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002302 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002303 vim_free(json);
2304 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002305 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002306 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002307 if (tv == &res_tv)
2308 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002309 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002310 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002311 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002312 }
2313 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002314 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002315 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002316 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002317 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002318}
2319
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002320/*
2321 * Invoke the callback at "cbhead".
2322 * Does not redraw but sets channel_need_redraw.
2323 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002324 static void
2325invoke_one_time_callback(
2326 channel_T *channel,
2327 cbq_T *cbhead,
2328 cbq_T *item,
2329 typval_T *argv)
2330{
2331 ch_logs(channel, "Invoking one-time callback %s",
2332 (char *)item->cq_callback);
2333 /* Remove the item from the list first, if the callback
2334 * invokes ch_close() the list will be cleared. */
2335 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002336 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002337 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002338 vim_free(item);
2339}
2340
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002341 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002342append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002343{
2344 buf_T *save_curbuf = curbuf;
2345 linenr_T lnum = buffer->b_ml.ml_line_count;
2346 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002347 chanpart_T *ch_part = &channel->ch_part[part];
2348 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002349 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002350
2351 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2352 {
2353 if (!ch_part->ch_nomod_error)
2354 {
2355 ch_error(channel, "Buffer is not modifiable, cannot append");
2356 ch_part->ch_nomod_error = TRUE;
2357 }
2358 return;
2359 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002360
2361 /* If the buffer is also used as input insert above the last
2362 * line. Don't write these lines. */
2363 if (save_write_to)
2364 {
2365 --lnum;
2366 buffer->b_write_to_channel = FALSE;
2367 }
2368
2369 /* Append to the buffer */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002370 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002371
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002372 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002373 curbuf = buffer;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002374 curwin->w_buffer = curbuf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002375 u_sync(TRUE);
2376 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002377 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002378
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002379 if (empty)
2380 {
2381 /* The buffer is empty, replace the first (dummy) line. */
2382 ml_replace(lnum, msg, TRUE);
2383 lnum = 0;
2384 }
2385 else
2386 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002387 appended_lines_mark(lnum, 1L);
2388 curbuf = save_curbuf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002389 curwin->w_buffer = curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002390 if (ch_part->ch_nomodifiable)
2391 buffer->b_p_ma = FALSE;
2392 else
2393 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002394
2395 if (buffer->b_nwindows > 0)
2396 {
2397 win_T *wp;
2398 win_T *save_curwin;
2399
2400 FOR_ALL_WINDOWS(wp)
2401 {
2402 if (wp->w_buffer == buffer
2403 && (save_write_to
2404 ? wp->w_cursor.lnum == lnum + 1
2405 : (wp->w_cursor.lnum == lnum
2406 && wp->w_cursor.col == 0)))
2407 {
2408 ++wp->w_cursor.lnum;
2409 save_curwin = curwin;
2410 curwin = wp;
2411 curbuf = curwin->w_buffer;
2412 scroll_cursor_bot(0, FALSE);
2413 curwin = save_curwin;
2414 curbuf = curwin->w_buffer;
2415 }
2416 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002417 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002418 channel_need_redraw = TRUE;
2419 }
2420
2421 if (save_write_to)
2422 {
2423 channel_T *ch;
2424
2425 /* Find channels reading from this buffer and adjust their
2426 * next-to-read line number. */
2427 buffer->b_write_to_channel = TRUE;
2428 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2429 {
2430 chanpart_T *in_part = &ch->ch_part[PART_IN];
2431
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002432 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002433 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2434 }
2435 }
2436}
2437
Bram Moolenaar437905c2016-04-26 19:01:05 +02002438 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002439drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002440{
2441 char_u *msg;
2442
2443 while ((msg = channel_get(channel, part)) != NULL)
2444 {
2445 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2446 vim_free(msg);
2447 }
2448}
2449
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002450/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002451 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002452 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002453 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002454 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002455 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002456may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002457{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002458 char_u *msg = NULL;
2459 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002460 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002461 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002462 chanpart_T *ch_part = &channel->ch_part[part];
2463 ch_mode_T ch_mode = ch_part->ch_mode;
2464 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002465 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002466 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002467 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002468 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002469 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002470
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002471 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002472 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002473 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002474
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002475 /* Use a message-specific callback, part callback or channel callback */
2476 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2477 if (cbitem->cq_seq_nr == 0)
2478 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002479 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002480 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002481 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002482 partial = cbitem->cq_partial;
2483 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002484 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002485 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002486 callback = ch_part->ch_callback;
2487 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002488 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002489 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002490 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002491 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002492 partial = channel->ch_partial;
2493 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002494
Bram Moolenaarec68a992016-10-03 21:37:41 +02002495 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002496 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2497 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002498 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002499 /* buffer was wiped out or unloaded */
2500 ch_logs(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002501 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002502 buffer = NULL;
2503 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002504
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002505 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002506 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002507 listitem_T *item;
2508 int argc = 0;
2509
Bram Moolenaard7ece102016-02-02 23:23:02 +01002510 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002511 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002512 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002513 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002514 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002515 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002516 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002517 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002518
Bram Moolenaarece61b02016-02-20 21:39:05 +01002519 for (item = listtv->vval.v_list->lv_first;
2520 item != NULL && argc < CH_JSON_MAX_ARGS;
2521 item = item->li_next)
2522 argv[argc++] = item->li_tv;
2523 while (argc < CH_JSON_MAX_ARGS)
2524 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002525
Bram Moolenaarece61b02016-02-20 21:39:05 +01002526 if (argv[0].v_type == VAR_STRING)
2527 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002528 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002529 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002530 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002531 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002532 }
2533
Bram Moolenaarece61b02016-02-20 21:39:05 +01002534 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002535 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002536 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002537 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002538 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002539 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002540 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002541 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002542 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002543 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002544 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002545 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002546 return FALSE;
2547 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002548 else
2549 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002550 /* If there is no callback or buffer drop the message. */
2551 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002552 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002553 /* If there is a close callback it may use ch_read() to get the
2554 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002555 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002556 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002557 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002558 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002559
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002560 if (ch_mode == MODE_NL)
2561 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002562 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002563 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002564 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002565
2566 /* See if we have a message ending in NL in the first buffer. If
2567 * not try to concatenate the first and the second buffer. */
2568 while (TRUE)
2569 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002570 node = channel_peek(channel, part);
2571 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002572 if (nl != NULL)
2573 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002574 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002575 {
2576 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2577 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002578 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002579 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002580 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002581 buf = node->rq_buffer;
2582
Bram Moolenaarec68a992016-10-03 21:37:41 +02002583 if (nl == NULL)
2584 {
2585 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002586 char_u *new_buf;
2587
2588 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2589 if (new_buf == NULL)
2590 /* This might fail over and over again, should the message
2591 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002592 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002593 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002594 node->rq_buffer = buf;
2595 nl = buf + node->rq_buflen++;
2596 *nl = NUL;
2597 }
2598
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002599 /* Convert NUL to NL, the internal representation. */
2600 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2601 if (*p == NUL)
2602 *p = NL;
2603
2604 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002605 {
2606 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002607 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002608 *nl = NUL;
2609 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002610 else
2611 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002612 /* Copy the message into allocated memory (excluding the NL)
2613 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002614 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002615 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002616 }
2617 }
2618 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002619 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002620 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002621 * get everything we have.
2622 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002623 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002624 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002625
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002626 if (msg == NULL)
2627 return FALSE; /* out of memory (and avoids Coverity warning) */
2628
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002629 argv[1].v_type = VAR_STRING;
2630 argv[1].vval.v_string = msg;
2631 }
2632
Bram Moolenaara07fec92016-02-05 21:04:08 +01002633 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002634 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002635 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002636
Bram Moolenaar958dc692016-12-01 15:34:12 +01002637 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002638 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002639 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002640 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002641 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002642 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002643 break;
2644 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002645 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002646 {
2647 if (channel->ch_drop_never)
2648 {
2649 /* message must be read with ch_read() */
2650 channel_push_json(channel, part, listtv);
2651 listtv = NULL;
2652 }
2653 else
2654 ch_logn(channel, "Dropping message %d without callback",
2655 seq_nr);
2656 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002657 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002658 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002659 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002660 if (buffer != NULL)
2661 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002662 if (msg == NULL)
2663 /* JSON or JS mode: re-encode the message. */
2664 msg = json_encode(listtv, ch_mode);
2665 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002666 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002667#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002668 if (buffer->b_term != NULL)
2669 write_to_term(buffer, msg, channel);
2670 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002671#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002672 append_to_buffer(buffer, msg, channel, part);
2673 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002674 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002675
Bram Moolenaar187db502016-02-27 14:44:26 +01002676 if (callback != NULL)
2677 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002678 if (cbitem != NULL)
2679 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2680 else
2681 {
2682 /* invoke the channel callback */
2683 ch_logs(channel, "Invoking channel callback %s",
2684 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002685 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002686 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002687 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002688 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002689 else
Bram Moolenaar958dc692016-12-01 15:34:12 +01002690 ch_logn(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002691
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002692 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002693 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002694 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002695
2696 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002697}
2698
2699/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002700 * Return TRUE when channel "channel" is open for writing to.
2701 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002702 */
2703 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002704channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002705{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002706 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002707 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002708}
2709
2710/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002711 * Return TRUE when channel "channel" is open for reading or writing.
2712 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002713 */
2714 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002715channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002716{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002717 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002718 || channel->CH_IN_FD != INVALID_FD
2719 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002720 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002721}
2722
2723/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002724 * Return TRUE if "channel" has JSON or other typeahead.
2725 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002726 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002727channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002728{
2729 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2730
2731 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2732 {
2733 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2734 jsonq_T *item = head->jq_next;
2735
2736 return item != NULL;
2737 }
2738 return channel_peek(channel, part) != NULL;
2739}
2740
2741/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002742 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002743 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002744 */
2745 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002746channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002747{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002748 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002749 int has_readahead = FALSE;
2750
Bram Moolenaar77073442016-02-13 23:23:53 +01002751 if (channel == NULL)
2752 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002753 if (req_part == PART_OUT)
2754 {
2755 if (channel->CH_OUT_FD != INVALID_FD)
2756 return "open";
2757 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002758 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002759 }
2760 else if (req_part == PART_ERR)
2761 {
2762 if (channel->CH_ERR_FD != INVALID_FD)
2763 return "open";
2764 if (channel_has_readahead(channel, PART_ERR))
2765 has_readahead = TRUE;
2766 }
2767 else
2768 {
2769 if (channel_is_open(channel))
2770 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002771 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002772 if (channel_has_readahead(channel, part))
2773 {
2774 has_readahead = TRUE;
2775 break;
2776 }
2777 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002778
2779 if (has_readahead)
2780 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002781 return "closed";
2782}
2783
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002784 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002785channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002786{
2787 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002788 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002789 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002790 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002791 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002792
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002793 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002794 STRCAT(namebuf, "_");
2795 tail = STRLEN(namebuf);
2796
2797 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002798 if (chanpart->ch_fd != INVALID_FD)
2799 status = "open";
2800 else if (channel_has_readahead(channel, part))
2801 status = "buffered";
2802 else
2803 status = "closed";
2804 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002805
2806 STRCPY(namebuf + tail, "mode");
2807 switch (chanpart->ch_mode)
2808 {
2809 case MODE_NL: s = "NL"; break;
2810 case MODE_RAW: s = "RAW"; break;
2811 case MODE_JSON: s = "JSON"; break;
2812 case MODE_JS: s = "JS"; break;
2813 }
2814 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2815
2816 STRCPY(namebuf + tail, "io");
2817 if (part == PART_SOCK)
2818 s = "socket";
2819 else switch (chanpart->ch_io)
2820 {
2821 case JIO_NULL: s = "null"; break;
2822 case JIO_PIPE: s = "pipe"; break;
2823 case JIO_FILE: s = "file"; break;
2824 case JIO_BUFFER: s = "buffer"; break;
2825 case JIO_OUT: s = "out"; break;
2826 }
2827 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2828
2829 STRCPY(namebuf + tail, "timeout");
2830 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2831}
2832
2833 void
2834channel_info(channel_T *channel, dict_T *dict)
2835{
2836 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002837 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002838
2839 if (channel->ch_hostname != NULL)
2840 {
2841 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2842 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2843 channel_part_info(channel, dict, "sock", PART_SOCK);
2844 }
2845 else
2846 {
2847 channel_part_info(channel, dict, "out", PART_OUT);
2848 channel_part_info(channel, dict, "err", PART_ERR);
2849 channel_part_info(channel, dict, "in", PART_IN);
2850 }
2851}
2852
Bram Moolenaar77073442016-02-13 23:23:53 +01002853/*
2854 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002855 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002856 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002857 */
2858 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002859channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002860{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002861 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002862
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002863#ifdef FEAT_GUI
2864 channel_gui_unregister(channel);
2865#endif
2866
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002867 ch_close_part(channel, PART_SOCK);
2868 ch_close_part(channel, PART_IN);
2869 ch_close_part(channel, PART_OUT);
2870 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002871
Bram Moolenaar8b374212016-02-24 20:43:06 +01002872 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002873 {
2874 typval_T argv[1];
2875 typval_T rettv;
2876 int dummy;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002877 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002878
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002879 /* Invoke callbacks before the close callback, since it's weird to
2880 * first invoke the close callback. Increment the refcount to avoid
2881 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002882 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002883 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002884 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002885 while (may_invoke_callback(channel, part))
2886 ;
2887
2888 /* Invoke the close callback, if still set. */
2889 if (channel->ch_close_cb != NULL)
2890 {
2891 ch_logs(channel, "Invoking close callback %s",
2892 (char *)channel->ch_close_cb);
2893 argv[0].v_type = VAR_CHANNEL;
2894 argv[0].vval.v_channel = channel;
2895 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002896 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002897 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002898 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002899 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002900 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002901
2902 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002903 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002904 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002905 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002906
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002907 --channel->ch_refcount;
2908
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002909 if (channel_need_redraw)
2910 {
2911 channel_need_redraw = FALSE;
2912 redraw_after_callback();
2913 }
2914
Bram Moolenaar958dc692016-12-01 15:34:12 +01002915 if (!channel->ch_drop_never)
2916 /* any remaining messages are useless now */
2917 for (part = PART_SOCK; part < PART_IN; ++part)
2918 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002919 }
2920
2921 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002922
2923#ifdef FEAT_TERMINAL
2924 term_channel_closed(channel);
2925#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002926}
2927
Bram Moolenaard04a0202016-01-26 23:30:18 +01002928/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002929 * Close the "in" part channel "channel".
2930 */
2931 void
2932channel_close_in(channel_T *channel)
2933{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002934 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002935}
2936
2937/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002938 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002939 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002940 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002941channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002942{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002943 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2944 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002945
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002946 while (channel_peek(channel, part) != NULL)
2947 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002948
2949 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002950 {
2951 cbq_T *node = cb_head->cq_next;
2952
2953 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002954 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002955 vim_free(node);
2956 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002957
2958 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002959 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002960 free_tv(json_head->jq_next->jq_value);
2961 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002962 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002963
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002964 free_callback(channel->ch_part[part].ch_callback,
2965 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002966 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002967 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002968}
2969
2970/*
2971 * Clear all the read buffers on "channel".
2972 */
2973 void
2974channel_clear(channel_T *channel)
2975{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002976 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002977 vim_free(channel->ch_hostname);
2978 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002979 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002980 channel_clear_one(channel, PART_OUT);
2981 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002982 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002983 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002984 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002985 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002986 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002987 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002988 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002989}
2990
Bram Moolenaar77073442016-02-13 23:23:53 +01002991#if defined(EXITFREE) || defined(PROTO)
2992 void
2993channel_free_all(void)
2994{
2995 channel_T *channel;
2996
Bram Moolenaard6051b52016-02-28 15:49:03 +01002997 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002998 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2999 channel_clear(channel);
3000}
3001#endif
3002
3003
Bram Moolenaar715d2852016-04-30 17:06:31 +02003004/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003005#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003006
3007/* Buffer size for reading incoming messages. */
3008#define MAXMSGSIZE 4096
3009
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003010#if defined(HAVE_SELECT)
3011/*
3012 * Add write fds where we are waiting for writing to be possible.
3013 */
3014 static int
3015channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3016{
3017 int maxfd = maxfd_arg;
3018 channel_T *ch;
3019
3020 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3021 {
3022 chanpart_T *in_part = &ch->ch_part[PART_IN];
3023
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003024 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003025 {
3026 FD_SET((int)in_part->ch_fd, wfds);
3027 if ((int)in_part->ch_fd >= maxfd)
3028 maxfd = (int)in_part->ch_fd + 1;
3029 }
3030 }
3031 return maxfd;
3032}
3033#else
3034/*
3035 * Add write fds where we are waiting for writing to be possible.
3036 */
3037 static int
3038channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3039{
3040 int nfd = nfd_in;
3041 channel_T *ch;
3042
3043 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3044 {
3045 chanpart_T *in_part = &ch->ch_part[PART_IN];
3046
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003047 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003048 {
3049 in_part->ch_poll_idx = nfd;
3050 fds[nfd].fd = in_part->ch_fd;
3051 fds[nfd].events = POLLOUT;
3052 ++nfd;
3053 }
3054 else
3055 in_part->ch_poll_idx = -1;
3056 }
3057 return nfd;
3058}
3059#endif
3060
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003061typedef enum {
3062 CW_READY,
3063 CW_NOT_READY,
3064 CW_ERROR
3065} channel_wait_result;
3066
Bram Moolenaard04a0202016-01-26 23:30:18 +01003067/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003068 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003069 * Return CW_READY when there is something to read.
3070 * Return CW_NOT_READY when there is nothing to read.
3071 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003072 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003073 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003074channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003075{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003076 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003077 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003078
Bram Moolenaard8070362016-02-15 21:56:54 +01003079# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003080 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003081 {
3082 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003083 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003084 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003085 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003086
3087 /* reading from a pipe, not a socket */
3088 while (TRUE)
3089 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003090 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3091
3092 if (r && nread > 0)
3093 return CW_READY;
3094 if (r == 0)
3095 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003096
3097 /* perhaps write some buffer lines */
3098 channel_write_any_lines();
3099
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003100 sleep_time = deadline - GetTickCount();
3101 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003102 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003103 /* Wait for a little while. Very short at first, up to 10 msec
3104 * after looping a few times. */
3105 if (sleep_time > delay)
3106 sleep_time = delay;
3107 Sleep(sleep_time);
3108 delay = delay * 2;
3109 if (delay > 10)
3110 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003111 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003112 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003113 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003114#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003115 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003116#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003117 struct timeval tval;
3118 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003119 fd_set wfds;
3120 int ret;
3121 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003122
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003123 tval.tv_sec = timeout / 1000;
3124 tval.tv_usec = (timeout % 1000) * 1000;
3125 for (;;)
3126 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003127 FD_ZERO(&rfds);
3128 FD_SET((int)fd, &rfds);
3129
3130 /* Write lines to a pipe when a pipe can be written to. Need to
3131 * set this every time, some buffers may be done. */
3132 maxfd = (int)fd + 1;
3133 FD_ZERO(&wfds);
3134 maxfd = channel_fill_wfds(maxfd, &wfds);
3135
3136 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003137# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003138 SOCK_ERRNO;
3139 if (ret == -1 && errno == EINTR)
3140 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003141# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003142 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003143 {
3144 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003145 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003146 channel_write_any_lines();
3147 continue;
3148 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003149 break;
3150 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003151#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003152 for (;;)
3153 {
3154 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3155 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003156
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003157 fds[0].fd = fd;
3158 fds[0].events = POLLIN;
3159 nfd = channel_fill_poll_write(nfd, fds);
3160 if (poll(fds, nfd, timeout) > 0)
3161 {
3162 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003163 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003164 channel_write_any_lines();
3165 continue;
3166 }
3167 break;
3168 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003169#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003170 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003171 return CW_NOT_READY;
3172}
3173
3174 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003175ch_close_part_on_error(
3176 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003177{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003178 char msgbuf[80];
3179
3180 vim_snprintf(msgbuf, sizeof(msgbuf),
3181 "%%s(): Read %s from ch_part[%d], closing",
3182 (is_err ? "error" : "EOF"), part);
3183
3184 if (is_err)
3185 /* Do not call emsg(), most likely the other end just exited. */
3186 ch_errors(channel, msgbuf, func);
3187 else
3188 ch_logs(channel, msgbuf, func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003189
3190 /* Queue a "DETACH" netbeans message in the command queue in order to
3191 * terminate the netbeans session later. Do not end the session here
3192 * directly as we may be running in the context of a call to
3193 * netbeans_parse_messages():
3194 * netbeans_parse_messages
3195 * -> autocmd triggered while processing the netbeans cmd
3196 * -> ui_breakcheck
3197 * -> gui event loop or select loop
3198 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003199 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003200 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003201 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003202 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003203 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3204
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003205 /* When reading is not possible close this part of the channel. Don't
3206 * close the channel yet, there may be something to read on another part. */
3207 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003208
3209#ifdef FEAT_GUI
3210 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003211 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003212#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003213}
3214
3215 static void
3216channel_close_now(channel_T *channel)
3217{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003218 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003219 if (channel->ch_nb_close_cb != NULL)
3220 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003221 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003222}
3223
3224/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003225 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003226 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003227 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003228 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003229 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003230channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003231{
3232 static char_u *buf = NULL;
3233 int len = 0;
3234 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003235 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003236 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003237
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003238 fd = channel->ch_part[part].ch_fd;
3239 if (fd == INVALID_FD)
3240 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003241 ch_errors(channel, "channel_read() called while %s part is closed",
3242 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003243 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003244 }
3245 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003246
3247 /* Allocate a buffer to read into. */
3248 if (buf == NULL)
3249 {
3250 buf = alloc(MAXMSGSIZE);
3251 if (buf == NULL)
3252 return; /* out of memory! */
3253 }
3254
3255 /* Keep on reading for as long as there is something to read.
3256 * Use select() or poll() to avoid blocking on a message that is exactly
3257 * MAXMSGSIZE long. */
3258 for (;;)
3259 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003260 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003261 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003262 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003263 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003264 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003265 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003266 if (len <= 0)
3267 break; /* error or nothing more to read */
3268
3269 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003270 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003271 readlen += len;
3272 if (len < MAXMSGSIZE)
3273 break; /* did read everything that's available */
3274 }
3275
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003276 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003277 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003278 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003279
3280#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003281 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003282 if (CH_HAS_GUI && gtk_main_level() > 0)
3283 gtk_main_quit();
3284#endif
3285}
3286
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003287/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003288 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003289 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003290 * Returns what was read in allocated memory.
3291 * Returns NULL in case of error or timeout.
3292 */
3293 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003294channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003295{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003296 char_u *buf;
3297 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003298 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003299 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003300 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003301 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003302
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003303 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003304 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003305
3306 while (TRUE)
3307 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003308 node = channel_peek(channel, part);
3309 if (node != NULL)
3310 {
3311 if (mode == MODE_RAW || (mode == MODE_NL
3312 && channel_first_nl(node) != NULL))
3313 /* got a complete message */
3314 break;
3315 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3316 continue;
3317 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003318
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003319 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003320 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003321 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003322 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003323 {
3324 ch_log(channel, "Timed out");
3325 return NULL;
3326 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003327 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003328 }
3329
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003330 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003331 if (mode == MODE_RAW)
3332 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003333 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003334 }
3335 else
3336 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003337 char_u *p;
3338
3339 buf = node->rq_buffer;
3340 nl = channel_first_nl(node);
3341
3342 /* Convert NUL to NL, the internal representation. */
3343 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3344 if (*p == NUL)
3345 *p = NL;
3346
3347 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003348 {
3349 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003350 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003351 *nl = NUL;
3352 }
3353 else
3354 {
3355 /* Copy the message into allocated memory and remove it from the
3356 * buffer. */
3357 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003358 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003359 }
3360 }
3361 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003362 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003363 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003364}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003365
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003366/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003367 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003368 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003369 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003370 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003371 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003372 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003373channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003374 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003375 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003376 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003377 int id,
3378 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003379{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003380 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003381 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003382 int timeout;
3383 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003384
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003385 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003386 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003387 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003388 for (;;)
3389 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003390 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003391
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003392 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003393 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003394 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003395 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003396 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003397 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003398
Bram Moolenaard7ece102016-02-02 23:23:02 +01003399 if (!more)
3400 {
3401 /* Handle any other messages in the queue. If done some more
3402 * messages may have arrived. */
3403 if (channel_parse_messages())
3404 continue;
3405
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003406 /* Wait for up to the timeout. If there was an incomplete message
3407 * use the deadline for that. */
3408 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003409 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003410 {
3411#ifdef WIN32
3412 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3413#else
3414 {
3415 struct timeval now_tv;
3416
3417 gettimeofday(&now_tv, NULL);
3418 timeout = (chanpart->ch_deadline.tv_sec
3419 - now_tv.tv_sec) * 1000
3420 + (chanpart->ch_deadline.tv_usec
3421 - now_tv.tv_usec) / 1000
3422 + 1;
3423 }
3424#endif
3425 if (timeout < 0)
3426 {
3427 /* Something went wrong, channel_parse_json() didn't
3428 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003429 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003430 timeout = timeout_arg;
3431 }
3432 else if (timeout > timeout_arg)
3433 timeout = timeout_arg;
3434 }
3435 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003436 if (fd == INVALID_FD
3437 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003438 {
3439 if (timeout == timeout_arg)
3440 {
3441 if (fd != INVALID_FD)
3442 ch_log(channel, "Timed out");
3443 break;
3444 }
3445 }
3446 else
3447 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003448 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003449 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003450 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003451 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003452}
3453
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003454/*
3455 * Common for ch_read() and ch_readraw().
3456 */
3457 void
3458common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3459{
3460 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003461 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003462 jobopt_T opt;
3463 int mode;
3464 int timeout;
3465 int id = -1;
3466 typval_T *listtv = NULL;
3467
3468 /* return an empty string by default */
3469 rettv->v_type = VAR_STRING;
3470 rettv->vval.v_string = NULL;
3471
3472 clear_job_options(&opt);
3473 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3474 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003475 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003476
Bram Moolenaar437905c2016-04-26 19:01:05 +02003477 if (opt.jo_set & JO_PART)
3478 part = opt.jo_part;
3479 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003480 if (channel != NULL)
3481 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003482 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003483 part = channel_part_read(channel);
3484 mode = channel_get_mode(channel, part);
3485 timeout = channel_get_timeout(channel, part);
3486 if (opt.jo_set & JO_TIMEOUT)
3487 timeout = opt.jo_timeout;
3488
3489 if (raw || mode == MODE_RAW || mode == MODE_NL)
3490 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3491 else
3492 {
3493 if (opt.jo_set & JO_ID)
3494 id = opt.jo_id;
3495 channel_read_json_block(channel, part, timeout, id, &listtv);
3496 if (listtv != NULL)
3497 {
3498 *rettv = *listtv;
3499 vim_free(listtv);
3500 }
3501 else
3502 {
3503 rettv->v_type = VAR_SPECIAL;
3504 rettv->vval.v_number = VVAL_NONE;
3505 }
3506 }
3507 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003508
3509theend:
3510 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003511}
3512
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003513# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3514 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003515/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003516 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003517 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003518 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003519 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003520channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003521{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003522 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003523 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003524
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003525 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003526 for (channel = first_channel; channel != NULL;
3527 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003528 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003529 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003530 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003531 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003532 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003533 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003534 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003535 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003536 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003537}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003538# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003539
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003540# if defined(WIN32) || defined(PROTO)
3541/*
3542 * Check the channels for anything that is ready to be read.
3543 * The data is put in the read queue.
3544 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003545 void
3546channel_handle_events(void)
3547{
3548 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003549 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003550 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003551
3552 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3553 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003554 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003555 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003556 {
3557 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003558 if (fd != INVALID_FD)
3559 {
3560 int r = channel_wait(channel, fd, 0);
3561
3562 if (r == CW_READY)
3563 channel_read(channel, part, "channel_handle_events");
3564 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003565 ch_close_part_on_error(channel, part, TRUE,
3566 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003567 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003568 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003569 }
3570}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003571# endif
3572
Bram Moolenaard04a0202016-01-26 23:30:18 +01003573/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003574 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003575 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003576 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003577 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003578 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003579channel_send(
3580 channel_T *channel,
3581 ch_part_T part,
3582 char_u *buf,
3583 int len,
3584 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003585{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003586 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003587 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003588
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003589 fd = channel->ch_part[part].ch_fd;
3590 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003591 {
3592 if (!channel->ch_error && fun != NULL)
3593 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003594 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003595 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003596 }
3597 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003598 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003599 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003600
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003601 if (log_fd != NULL)
3602 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003603 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003604 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003605 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003606 fprintf(log_fd, "'\n");
3607 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003608 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003609 }
3610
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003611 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003612 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003613 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003614 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003615 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003616 {
3617 if (!channel->ch_error && fun != NULL)
3618 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003619 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003620 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003621 }
3622 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003623 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003624 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003625
3626 channel->ch_error = FALSE;
3627 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003628}
3629
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003630/*
3631 * Common for "ch_sendexpr()" and "ch_sendraw()".
3632 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003633 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003634 * Otherwise returns NULL.
3635 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003636 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003637send_common(
3638 typval_T *argvars,
3639 char_u *text,
3640 int id,
3641 int eval,
3642 jobopt_T *opt,
3643 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003644 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003645{
3646 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003647 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003648
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003649 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003650 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003651 if (channel == NULL)
3652 return NULL;
3653 part_send = channel_part_send(channel);
3654 *part_read = channel_part_read(channel);
3655
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003656 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3657 return NULL;
3658
3659 /* Set the callback. An empty callback means no callback and not reading
3660 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3661 * allowed. */
3662 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3663 {
3664 if (eval)
3665 {
3666 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3667 return NULL;
3668 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003669 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003670 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003671 }
3672
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003673 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003674 && opt->jo_callback == NULL)
3675 return channel;
3676 return NULL;
3677}
3678
3679/*
3680 * common for "ch_evalexpr()" and "ch_sendexpr()"
3681 */
3682 void
3683ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3684{
3685 char_u *text;
3686 typval_T *listtv;
3687 channel_T *channel;
3688 int id;
3689 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003690 ch_part_T part_send;
3691 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003692 jobopt_T opt;
3693 int timeout;
3694
3695 /* return an empty string by default */
3696 rettv->v_type = VAR_STRING;
3697 rettv->vval.v_string = NULL;
3698
Bram Moolenaar437905c2016-04-26 19:01:05 +02003699 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003700 if (channel == NULL)
3701 return;
3702 part_send = channel_part_send(channel);
3703
3704 ch_mode = channel_get_mode(channel, part_send);
3705 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3706 {
3707 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3708 return;
3709 }
3710
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003711 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003712 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003713 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003714 if (text == NULL)
3715 return;
3716
3717 channel = send_common(argvars, text, id, eval, &opt,
3718 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3719 vim_free(text);
3720 if (channel != NULL && eval)
3721 {
3722 if (opt.jo_set & JO_TIMEOUT)
3723 timeout = opt.jo_timeout;
3724 else
3725 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003726 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3727 == OK)
3728 {
3729 list_T *list = listtv->vval.v_list;
3730
3731 /* Move the item from the list and then change the type to
3732 * avoid the value being freed. */
3733 *rettv = list->lv_last->li_tv;
3734 list->lv_last->li_tv.v_type = VAR_NUMBER;
3735 free_tv(listtv);
3736 }
3737 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003738 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003739}
3740
3741/*
3742 * common for "ch_evalraw()" and "ch_sendraw()"
3743 */
3744 void
3745ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3746{
3747 char_u buf[NUMBUFLEN];
3748 char_u *text;
3749 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003750 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003751 jobopt_T opt;
3752 int timeout;
3753
3754 /* return an empty string by default */
3755 rettv->v_type = VAR_STRING;
3756 rettv->vval.v_string = NULL;
3757
3758 text = get_tv_string_buf(&argvars[1], buf);
3759 channel = send_common(argvars, text, 0, eval, &opt,
3760 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3761 if (channel != NULL && eval)
3762 {
3763 if (opt.jo_set & JO_TIMEOUT)
3764 timeout = opt.jo_timeout;
3765 else
3766 timeout = channel_get_timeout(channel, part_read);
3767 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3768 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003769 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003770}
3771
Bram Moolenaard04a0202016-01-26 23:30:18 +01003772# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003773/*
3774 * Add open channels to the poll struct.
3775 * Return the adjusted struct index.
3776 * The type of "fds" is hidden to avoid problems with the function proto.
3777 */
3778 int
3779channel_poll_setup(int nfd_in, void *fds_in)
3780{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003781 int nfd = nfd_in;
3782 channel_T *channel;
3783 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003784 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003785
Bram Moolenaar77073442016-02-13 23:23:53 +01003786 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003787 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003788 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003789 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003790 chanpart_T *ch_part = &channel->ch_part[part];
3791
3792 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003793 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003794 ch_part->ch_poll_idx = nfd;
3795 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003796 fds[nfd].events = POLLIN;
3797 nfd++;
3798 }
3799 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003800 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003801 }
3802 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003803
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003804 nfd = channel_fill_poll_write(nfd, fds);
3805
Bram Moolenaare0874f82016-01-24 20:36:41 +01003806 return nfd;
3807}
3808
3809/*
3810 * The type of "fds" is hidden to avoid problems with the function proto.
3811 */
3812 int
3813channel_poll_check(int ret_in, void *fds_in)
3814{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003815 int ret = ret_in;
3816 channel_T *channel;
3817 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003818 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003819 int idx;
3820 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003821
Bram Moolenaar77073442016-02-13 23:23:53 +01003822 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003823 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003824 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003825 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003826 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003827
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003828 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003829 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003830 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003831 --ret;
3832 }
3833 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003834
3835 in_part = &channel->ch_part[PART_IN];
3836 idx = in_part->ch_poll_idx;
3837 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3838 {
3839 if (in_part->ch_buf_append)
3840 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003841 if (in_part->ch_bufref.br_buf != NULL)
3842 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003843 }
3844 else
3845 channel_write_in(channel);
3846 --ret;
3847 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003848 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003849
3850 return ret;
3851}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003852# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003853
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003854# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003855/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003856 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003857 */
3858 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003859channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003860{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003861 int maxfd = maxfd_in;
3862 channel_T *channel;
3863 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003864 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003865 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003866
Bram Moolenaar77073442016-02-13 23:23:53 +01003867 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003868 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003869 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003870 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003871 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003872
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003873 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003874 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003875 FD_SET((int)fd, rfds);
3876 if (maxfd < (int)fd)
3877 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003878 }
3879 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003880 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003881
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003882 maxfd = channel_fill_wfds(maxfd, wfds);
3883
Bram Moolenaare0874f82016-01-24 20:36:41 +01003884 return maxfd;
3885}
3886
3887/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003888 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003889 */
3890 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003891channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003892{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003893 int ret = ret_in;
3894 channel_T *channel;
3895 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003896 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003897 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003898 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003899
Bram Moolenaar77073442016-02-13 23:23:53 +01003900 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003901 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003902 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003903 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003904 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003905
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003906 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003907 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003908 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003909 --ret;
3910 }
3911 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003912
3913 in_part = &channel->ch_part[PART_IN];
3914 if (ret > 0 && in_part->ch_fd != INVALID_FD
3915 && FD_ISSET(in_part->ch_fd, wfds))
3916 {
3917 if (in_part->ch_buf_append)
3918 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003919 if (in_part->ch_bufref.br_buf != NULL)
3920 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003921 }
3922 else
3923 channel_write_in(channel);
3924 --ret;
3925 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003926 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003927
3928 return ret;
3929}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003930# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003931
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003932/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003933 * Execute queued up commands.
3934 * Invoked from the main loop when it's safe to execute received commands.
3935 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003936 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003937 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003938channel_parse_messages(void)
3939{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003940 channel_T *channel = first_channel;
3941 int ret = FALSE;
3942 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003943 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003944#ifdef ELAPSED_FUNC
3945 ELAPSED_TYPE start_tv;
3946
3947 ELAPSED_INIT(start_tv);
3948#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003949
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003950 ++safe_to_invoke_callback;
3951
Bram Moolenaard0b65022016-03-06 21:50:33 +01003952 /* Only do this message when another message was given, otherwise we get
3953 * lots of them. */
3954 if (did_log_msg)
3955 {
3956 ch_log(NULL, "looking for messages on channels");
3957 did_log_msg = FALSE;
3958 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003959 while (channel != NULL)
3960 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003961 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003962 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003963 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003964 channel_close_now(channel);
3965 /* channel may have been freed, start over */
3966 channel = first_channel;
3967 continue;
3968 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003969 if (channel->ch_to_be_freed)
3970 {
3971 channel_free(channel);
3972 /* channel has been freed, start over */
3973 channel = first_channel;
3974 continue;
3975 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003976 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003977 {
3978 /* channel is no longer useful, free it */
3979 channel_free(channel);
3980 channel = first_channel;
3981 part = PART_SOCK;
3982 continue;
3983 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003984 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003985 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003986 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003987 /* Increase the refcount, in case the handler causes the channel
3988 * to be unreferenced or closed. */
3989 ++channel->ch_refcount;
3990 r = may_invoke_callback(channel, part);
3991 if (r == OK)
3992 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003993 if (channel_unref(channel) || (r == OK
3994#ifdef ELAPSED_FUNC
3995 /* Limit the time we loop here to 100 msec, otherwise
3996 * Vim becomes unresponsive when the callback takes
3997 * more than a bit of time. */
3998 && ELAPSED_FUNC(start_tv) < 100L
3999#endif
4000 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004001 {
4002 /* channel was freed or something was done, start over */
4003 channel = first_channel;
4004 part = PART_SOCK;
4005 continue;
4006 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004007 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004008 if (part < PART_ERR)
4009 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004010 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004011 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004012 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004013 part = PART_SOCK;
4014 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004015 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004016
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004017 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004018 {
4019 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004020 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01004021 }
4022
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004023 --safe_to_invoke_callback;
4024
Bram Moolenaard7ece102016-02-02 23:23:02 +01004025 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004026}
4027
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004028/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004029 * Return TRUE if any channel has readahead. That means we should not block on
4030 * waiting for input.
4031 */
4032 int
4033channel_any_readahead(void)
4034{
4035 channel_T *channel = first_channel;
4036 ch_part_T part = PART_SOCK;
4037
4038 while (channel != NULL)
4039 {
4040 if (channel_has_readahead(channel, part))
4041 return TRUE;
4042 if (part < PART_ERR)
4043 ++part;
4044 else
4045 {
4046 channel = channel->ch_next;
4047 part = PART_SOCK;
4048 }
4049 }
4050 return FALSE;
4051}
4052
4053/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004054 * Mark references to lists used in channels.
4055 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004056 int
4057set_ref_in_channel(int copyID)
4058{
Bram Moolenaar77073442016-02-13 23:23:53 +01004059 int abort = FALSE;
4060 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004061 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004062
Bram Moolenaar77073442016-02-13 23:23:53 +01004063 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004064 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004065 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004066 tv.v_type = VAR_CHANNEL;
4067 tv.vval.v_channel = channel;
4068 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004069 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004070 return abort;
4071}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004072
4073/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004074 * Return the "part" to write to for "channel".
4075 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004076 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004077channel_part_send(channel_T *channel)
4078{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004079 if (channel->CH_SOCK_FD == INVALID_FD)
4080 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004081 return PART_SOCK;
4082}
4083
4084/*
4085 * Return the default "part" to read from for "channel".
4086 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004087 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004088channel_part_read(channel_T *channel)
4089{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004090 if (channel->CH_SOCK_FD == INVALID_FD)
4091 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004092 return PART_SOCK;
4093}
4094
4095/*
4096 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004097 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004098 */
4099 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004100channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004101{
Bram Moolenaar77073442016-02-13 23:23:53 +01004102 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004103 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004104 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004105}
4106
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004107/*
4108 * Return the timeout of "channel"/"part"
4109 */
4110 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004111channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004112{
4113 return channel->ch_part[part].ch_timeout;
4114}
4115
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004116 static int
4117handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4118{
4119 char_u *val = get_tv_string(item);
4120
4121 opt->jo_set |= jo;
4122 if (STRCMP(val, "nl") == 0)
4123 *modep = MODE_NL;
4124 else if (STRCMP(val, "raw") == 0)
4125 *modep = MODE_RAW;
4126 else if (STRCMP(val, "js") == 0)
4127 *modep = MODE_JS;
4128 else if (STRCMP(val, "json") == 0)
4129 *modep = MODE_JSON;
4130 else
4131 {
4132 EMSG2(_(e_invarg2), val);
4133 return FAIL;
4134 }
4135 return OK;
4136}
4137
4138 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004139handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004140{
4141 char_u *val = get_tv_string(item);
4142
4143 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4144 if (STRCMP(val, "null") == 0)
4145 opt->jo_io[part] = JIO_NULL;
4146 else if (STRCMP(val, "pipe") == 0)
4147 opt->jo_io[part] = JIO_PIPE;
4148 else if (STRCMP(val, "file") == 0)
4149 opt->jo_io[part] = JIO_FILE;
4150 else if (STRCMP(val, "buffer") == 0)
4151 opt->jo_io[part] = JIO_BUFFER;
4152 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4153 opt->jo_io[part] = JIO_OUT;
4154 else
4155 {
4156 EMSG2(_(e_invarg2), val);
4157 return FAIL;
4158 }
4159 return OK;
4160}
4161
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004162/*
4163 * Clear a jobopt_T before using it.
4164 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004165 void
4166clear_job_options(jobopt_T *opt)
4167{
4168 vim_memset(opt, 0, sizeof(jobopt_T));
4169}
4170
4171/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004172 * Free any members of a jobopt_T.
4173 */
4174 void
4175free_job_options(jobopt_T *opt)
4176{
4177 if (opt->jo_partial != NULL)
4178 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004179 else if (opt->jo_callback != NULL)
4180 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004181 if (opt->jo_out_partial != NULL)
4182 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004183 else if (opt->jo_out_cb != NULL)
4184 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004185 if (opt->jo_err_partial != NULL)
4186 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004187 else if (opt->jo_err_cb != NULL)
4188 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004189 if (opt->jo_close_partial != NULL)
4190 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004191 else if (opt->jo_close_cb != NULL)
4192 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004193 if (opt->jo_exit_partial != NULL)
4194 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004195 else if (opt->jo_exit_cb != NULL)
4196 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004197}
4198
4199/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004200 * Get the PART_ number from the first character of an option name.
4201 */
4202 static int
4203part_from_char(int c)
4204{
4205 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4206}
4207
4208/*
4209 * Get the option entries from the dict in "tv", parse them and put the result
4210 * in "opt".
4211 * Only accept options in "supported".
4212 * If an option value is invalid return FAIL.
4213 */
4214 int
4215get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4216{
4217 typval_T *item;
4218 char_u *val;
4219 dict_T *dict;
4220 int todo;
4221 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004222 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004223
4224 opt->jo_set = 0;
4225 if (tv->v_type == VAR_UNKNOWN)
4226 return OK;
4227 if (tv->v_type != VAR_DICT)
4228 {
4229 EMSG(_(e_invarg));
4230 return FAIL;
4231 }
4232 dict = tv->vval.v_dict;
4233 if (dict == NULL)
4234 return OK;
4235
4236 todo = (int)dict->dv_hashtab.ht_used;
4237 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4238 if (!HASHITEM_EMPTY(hi))
4239 {
4240 item = &dict_lookup(hi)->di_tv;
4241
4242 if (STRCMP(hi->hi_key, "mode") == 0)
4243 {
4244 if (!(supported & JO_MODE))
4245 break;
4246 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4247 return FAIL;
4248 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004249 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004250 {
4251 if (!(supported & JO_IN_MODE))
4252 break;
4253 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4254 == FAIL)
4255 return FAIL;
4256 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004257 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004258 {
4259 if (!(supported & JO_OUT_MODE))
4260 break;
4261 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4262 == FAIL)
4263 return FAIL;
4264 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004265 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004266 {
4267 if (!(supported & JO_ERR_MODE))
4268 break;
4269 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4270 == FAIL)
4271 return FAIL;
4272 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004273 else if (STRCMP(hi->hi_key, "in_io") == 0
4274 || STRCMP(hi->hi_key, "out_io") == 0
4275 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004276 {
4277 if (!(supported & JO_OUT_IO))
4278 break;
4279 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4280 return FAIL;
4281 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004282 else if (STRCMP(hi->hi_key, "in_name") == 0
4283 || STRCMP(hi->hi_key, "out_name") == 0
4284 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004285 {
4286 part = part_from_char(*hi->hi_key);
4287
4288 if (!(supported & JO_OUT_IO))
4289 break;
4290 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4291 opt->jo_io_name[part] =
4292 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4293 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004294 else if (STRCMP(hi->hi_key, "pty") == 0)
4295 {
4296 if (!(supported & JO_MODE))
4297 break;
4298 opt->jo_pty = get_tv_number(item);
4299 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004300 else if (STRCMP(hi->hi_key, "in_buf") == 0
4301 || STRCMP(hi->hi_key, "out_buf") == 0
4302 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004303 {
4304 part = part_from_char(*hi->hi_key);
4305
4306 if (!(supported & JO_OUT_IO))
4307 break;
4308 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4309 opt->jo_io_buf[part] = get_tv_number(item);
4310 if (opt->jo_io_buf[part] <= 0)
4311 {
4312 EMSG2(_(e_invarg2), get_tv_string(item));
4313 return FAIL;
4314 }
4315 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4316 {
4317 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4318 return FAIL;
4319 }
4320 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004321 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4322 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4323 {
4324 part = part_from_char(*hi->hi_key);
4325
4326 if (!(supported & JO_OUT_IO))
4327 break;
4328 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4329 opt->jo_modifiable[part] = get_tv_number(item);
4330 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004331 else if (STRCMP(hi->hi_key, "out_msg") == 0
4332 || STRCMP(hi->hi_key, "err_msg") == 0)
4333 {
4334 part = part_from_char(*hi->hi_key);
4335
4336 if (!(supported & JO_OUT_IO))
4337 break;
4338 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4339 opt->jo_message[part] = get_tv_number(item);
4340 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004341 else if (STRCMP(hi->hi_key, "in_top") == 0
4342 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004343 {
4344 linenr_T *lp;
4345
4346 if (!(supported & JO_OUT_IO))
4347 break;
4348 if (hi->hi_key[3] == 't')
4349 {
4350 lp = &opt->jo_in_top;
4351 opt->jo_set |= JO_IN_TOP;
4352 }
4353 else
4354 {
4355 lp = &opt->jo_in_bot;
4356 opt->jo_set |= JO_IN_BOT;
4357 }
4358 *lp = get_tv_number(item);
4359 if (*lp < 0)
4360 {
4361 EMSG2(_(e_invarg2), get_tv_string(item));
4362 return FAIL;
4363 }
4364 }
4365 else if (STRCMP(hi->hi_key, "channel") == 0)
4366 {
4367 if (!(supported & JO_OUT_IO))
4368 break;
4369 opt->jo_set |= JO_CHANNEL;
4370 if (item->v_type != VAR_CHANNEL)
4371 {
4372 EMSG2(_(e_invarg2), "channel");
4373 return FAIL;
4374 }
4375 opt->jo_channel = item->vval.v_channel;
4376 }
4377 else if (STRCMP(hi->hi_key, "callback") == 0)
4378 {
4379 if (!(supported & JO_CALLBACK))
4380 break;
4381 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004382 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004383 if (opt->jo_callback == NULL)
4384 {
4385 EMSG2(_(e_invarg2), "callback");
4386 return FAIL;
4387 }
4388 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004389 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004390 {
4391 if (!(supported & JO_OUT_CALLBACK))
4392 break;
4393 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004394 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004395 if (opt->jo_out_cb == NULL)
4396 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004397 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004398 return FAIL;
4399 }
4400 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004401 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004402 {
4403 if (!(supported & JO_ERR_CALLBACK))
4404 break;
4405 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004406 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004407 if (opt->jo_err_cb == NULL)
4408 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004409 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004410 return FAIL;
4411 }
4412 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004413 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004414 {
4415 if (!(supported & JO_CLOSE_CALLBACK))
4416 break;
4417 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004418 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004419 if (opt->jo_close_cb == NULL)
4420 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004421 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004422 return FAIL;
4423 }
4424 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004425 else if (STRCMP(hi->hi_key, "drop") == 0)
4426 {
4427 int never = FALSE;
4428 val = get_tv_string(item);
4429
4430 if (STRCMP(val, "never") == 0)
4431 never = TRUE;
4432 else if (STRCMP(val, "auto") != 0)
4433 {
4434 EMSG2(_(e_invarg2), "drop");
4435 return FAIL;
4436 }
4437 opt->jo_drop_never = never;
4438 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004439 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4440 {
4441 if (!(supported & JO_EXIT_CB))
4442 break;
4443 opt->jo_set |= JO_EXIT_CB;
4444 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4445 if (opt->jo_exit_cb == NULL)
4446 {
4447 EMSG2(_(e_invarg2), "exit_cb");
4448 return FAIL;
4449 }
4450 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004451 else if (STRCMP(hi->hi_key, "waittime") == 0)
4452 {
4453 if (!(supported & JO_WAITTIME))
4454 break;
4455 opt->jo_set |= JO_WAITTIME;
4456 opt->jo_waittime = get_tv_number(item);
4457 }
4458 else if (STRCMP(hi->hi_key, "timeout") == 0)
4459 {
4460 if (!(supported & JO_TIMEOUT))
4461 break;
4462 opt->jo_set |= JO_TIMEOUT;
4463 opt->jo_timeout = get_tv_number(item);
4464 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004465 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004466 {
4467 if (!(supported & JO_OUT_TIMEOUT))
4468 break;
4469 opt->jo_set |= JO_OUT_TIMEOUT;
4470 opt->jo_out_timeout = get_tv_number(item);
4471 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004472 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004473 {
4474 if (!(supported & JO_ERR_TIMEOUT))
4475 break;
4476 opt->jo_set |= JO_ERR_TIMEOUT;
4477 opt->jo_err_timeout = get_tv_number(item);
4478 }
4479 else if (STRCMP(hi->hi_key, "part") == 0)
4480 {
4481 if (!(supported & JO_PART))
4482 break;
4483 opt->jo_set |= JO_PART;
4484 val = get_tv_string(item);
4485 if (STRCMP(val, "err") == 0)
4486 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004487 else if (STRCMP(val, "out") == 0)
4488 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004489 else
4490 {
4491 EMSG2(_(e_invarg2), val);
4492 return FAIL;
4493 }
4494 }
4495 else if (STRCMP(hi->hi_key, "id") == 0)
4496 {
4497 if (!(supported & JO_ID))
4498 break;
4499 opt->jo_set |= JO_ID;
4500 opt->jo_id = get_tv_number(item);
4501 }
4502 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4503 {
4504 if (!(supported & JO_STOPONEXIT))
4505 break;
4506 opt->jo_set |= JO_STOPONEXIT;
4507 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4508 opt->jo_soe_buf);
4509 if (opt->jo_stoponexit == NULL)
4510 {
4511 EMSG2(_(e_invarg2), "stoponexit");
4512 return FAIL;
4513 }
4514 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004515 else if (STRCMP(hi->hi_key, "block_write") == 0)
4516 {
4517 if (!(supported & JO_BLOCK_WRITE))
4518 break;
4519 opt->jo_set |= JO_BLOCK_WRITE;
4520 opt->jo_block_write = get_tv_number(item);
4521 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004522 else
4523 break;
4524 --todo;
4525 }
4526 if (todo > 0)
4527 {
4528 EMSG2(_(e_invarg2), hi->hi_key);
4529 return FAIL;
4530 }
4531
4532 return OK;
4533}
4534
4535/*
4536 * Get the channel from the argument.
4537 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004538 * When "check_open" is TRUE check that the channel can be used.
4539 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004540 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004541 */
4542 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004543get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004544{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004545 channel_T *channel = NULL;
4546 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004547
4548 if (tv->v_type == VAR_JOB)
4549 {
4550 if (tv->vval.v_job != NULL)
4551 channel = tv->vval.v_job->jv_channel;
4552 }
4553 else if (tv->v_type == VAR_CHANNEL)
4554 {
4555 channel = tv->vval.v_channel;
4556 }
4557 else
4558 {
4559 EMSG2(_(e_invarg2), get_tv_string(tv));
4560 return NULL;
4561 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004562 if (channel != NULL && reading)
4563 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004564 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004565
Bram Moolenaar437905c2016-04-26 19:01:05 +02004566 if (check_open && (channel == NULL || (!channel_is_open(channel)
4567 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004568 {
4569 EMSG(_("E906: not an open channel"));
4570 return NULL;
4571 }
4572 return channel;
4573}
4574
4575static job_T *first_job = NULL;
4576
4577 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004578job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004579{
4580 ch_log(job->jv_channel, "Freeing job");
4581 if (job->jv_channel != NULL)
4582 {
4583 /* The link from the channel to the job doesn't count as a reference,
4584 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004585 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004586 * NULL the reference. We don't set ch_job_killed, unreferencing the
4587 * job doesn't mean it stops running. */
4588 job->jv_channel->ch_job = NULL;
4589 channel_unref(job->jv_channel);
4590 }
4591 mch_clear_job(job);
4592
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004593 vim_free(job->jv_tty_name);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004594 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004595 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004596}
4597
4598 static void
4599job_free_job(job_T *job)
4600{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004601 if (job->jv_next != NULL)
4602 job->jv_next->jv_prev = job->jv_prev;
4603 if (job->jv_prev == NULL)
4604 first_job = job->jv_next;
4605 else
4606 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004607 vim_free(job);
4608}
4609
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004610 static void
4611job_free(job_T *job)
4612{
4613 if (!in_free_unref_items)
4614 {
4615 job_free_contents(job);
4616 job_free_job(job);
4617 }
4618}
4619
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004620#if defined(EXITFREE) || defined(PROTO)
4621 void
4622job_free_all(void)
4623{
4624 while (first_job != NULL)
4625 job_free(first_job);
4626}
4627#endif
4628
4629/*
4630 * Return TRUE if we need to check if the process of "job" has ended.
4631 */
4632 static int
4633job_need_end_check(job_T *job)
4634{
4635 return job->jv_status == JOB_STARTED
4636 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4637}
4638
4639/*
4640 * Return TRUE if the channel of "job" is still useful.
4641 */
4642 static int
4643job_channel_still_useful(job_T *job)
4644{
4645 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4646}
4647
4648/*
4649 * Return TRUE if the job should not be freed yet. Do not free the job when
4650 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4651 * or when the associated channel will do something with the job output.
4652 */
4653 static int
4654job_still_useful(job_T *job)
4655{
4656 return job_need_end_check(job) || job_channel_still_useful(job);
4657}
4658
4659/*
4660 * NOTE: Must call job_cleanup() only once right after the status of "job"
4661 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4662 * mch_detect_ended_job() returned non-NULL).
4663 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02004664 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02004665job_cleanup(job_T *job)
4666{
4667 if (job->jv_status != JOB_ENDED)
4668 return;
4669
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004670 /* Ready to cleanup the job. */
4671 job->jv_status = JOB_FINISHED;
4672
Bram Moolenaar97792de2016-10-15 18:36:49 +02004673 if (job->jv_exit_cb != NULL)
4674 {
4675 typval_T argv[3];
4676 typval_T rettv;
4677 int dummy;
4678
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004679 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004680 ++job->jv_refcount;
4681 argv[0].v_type = VAR_JOB;
4682 argv[0].vval.v_job = job;
4683 argv[1].v_type = VAR_NUMBER;
4684 argv[1].vval.v_number = job->jv_exitval;
4685 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4686 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4687 job->jv_exit_partial, NULL);
4688 clear_tv(&rettv);
4689 --job->jv_refcount;
4690 channel_need_redraw = TRUE;
4691 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004692
4693 /* Do not free the job in case the close callback of the associated channel
4694 * isn't invoked yet and may get information by job_info(). */
4695 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02004696 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004697 /* The job was already unreferenced and the associated channel was
4698 * detached, now that it ended it can be freed. Careful: caller must
4699 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004700 job_free(job);
4701 }
4702}
4703
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004704/*
4705 * Mark references in jobs that are still useful.
4706 */
4707 int
4708set_ref_in_job(int copyID)
4709{
4710 int abort = FALSE;
4711 job_T *job;
4712 typval_T tv;
4713
4714 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004715 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004716 {
4717 tv.v_type = VAR_JOB;
4718 tv.vval.v_job = job;
4719 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4720 }
4721 return abort;
4722}
4723
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004724/*
4725 * Dereference "job". Note that after this "job" may have been freed.
4726 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004727 void
4728job_unref(job_T *job)
4729{
4730 if (job != NULL && --job->jv_refcount <= 0)
4731 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004732 /* Do not free the job if there is a channel where the close callback
4733 * may get the job info. */
4734 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004735 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004736 /* Do not free the job when it has not ended yet and there is a
4737 * "stoponexit" flag or an exit callback. */
4738 if (!job_need_end_check(job))
4739 {
4740 job_free(job);
4741 }
4742 else if (job->jv_channel != NULL)
4743 {
4744 /* Do remove the link to the channel, otherwise it hangs
4745 * around until Vim exits. See job_free() for refcount. */
4746 ch_log(job->jv_channel, "detaching channel from job");
4747 job->jv_channel->ch_job = NULL;
4748 channel_unref(job->jv_channel);
4749 job->jv_channel = NULL;
4750 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004751 }
4752 }
4753}
4754
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004755 int
4756free_unused_jobs_contents(int copyID, int mask)
4757{
4758 int did_free = FALSE;
4759 job_T *job;
4760
4761 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004762 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004763 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004764 {
4765 /* Free the channel and ordinary items it contains, but don't
4766 * recurse into Lists, Dictionaries etc. */
4767 job_free_contents(job);
4768 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004769 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004770 return did_free;
4771}
4772
4773 void
4774free_unused_jobs(int copyID, int mask)
4775{
4776 job_T *job;
4777 job_T *job_next;
4778
4779 for (job = first_job; job != NULL; job = job_next)
4780 {
4781 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004782 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004783 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004784 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004785 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004786 job_free_job(job);
4787 }
4788 }
4789}
4790
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004791/*
4792 * Allocate a job. Sets the refcount to one and sets options default.
4793 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02004794 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004795job_alloc(void)
4796{
4797 job_T *job;
4798
4799 job = (job_T *)alloc_clear(sizeof(job_T));
4800 if (job != NULL)
4801 {
4802 job->jv_refcount = 1;
4803 job->jv_stoponexit = vim_strsave((char_u *)"term");
4804
4805 if (first_job != NULL)
4806 {
4807 first_job->jv_prev = job;
4808 job->jv_next = first_job;
4809 }
4810 first_job = job;
4811 }
4812 return job;
4813}
4814
4815 void
4816job_set_options(job_T *job, jobopt_T *opt)
4817{
4818 if (opt->jo_set & JO_STOPONEXIT)
4819 {
4820 vim_free(job->jv_stoponexit);
4821 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4822 job->jv_stoponexit = NULL;
4823 else
4824 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4825 }
4826 if (opt->jo_set & JO_EXIT_CB)
4827 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004828 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004829 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004830 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004831 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004832 job->jv_exit_partial = NULL;
4833 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004834 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004835 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004836 job->jv_exit_partial = opt->jo_exit_partial;
4837 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004838 {
4839 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004840 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004841 }
4842 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004843 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004844 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004845 func_ref(job->jv_exit_cb);
4846 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004847 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004848 }
4849}
4850
4851/*
4852 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4853 */
4854 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004855job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004856{
4857 job_T *job;
4858
4859 for (job = first_job; job != NULL; job = job->jv_next)
4860 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4861 mch_stop_job(job, job->jv_stoponexit);
4862}
4863
4864/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004865 * Return TRUE when there is any job that has an exit callback and might exit,
4866 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004867 */
4868 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004869has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004870{
4871 job_T *job;
4872
4873 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004874 /* Only should check if the channel has been closed, if the channel is
4875 * open the job won't exit. */
4876 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004877 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004878 return TRUE;
4879 return FALSE;
4880}
4881
Bram Moolenaar97792de2016-10-15 18:36:49 +02004882#define MAX_CHECK_ENDED 8
4883
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004884/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004885 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004886 */
4887 void
4888job_check_ended(void)
4889{
Bram Moolenaar97792de2016-10-15 18:36:49 +02004890 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004891
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004892 if (first_job == NULL)
4893 return;
4894
Bram Moolenaar97792de2016-10-15 18:36:49 +02004895 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004896 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004897 /* NOTE: mch_detect_ended_job() must only return a job of which the
4898 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004899 job_T *job = mch_detect_ended_job(first_job);
4900
4901 if (job == NULL)
4902 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004903 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004904 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02004905
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004906 if (channel_need_redraw)
4907 {
4908 channel_need_redraw = FALSE;
4909 redraw_after_callback();
4910 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004911}
4912
4913/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02004914 * Create a job and return it. Implements job_start().
4915 * The returned job has a refcount of one.
4916 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004917 */
4918 job_T *
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004919job_start(typval_T *argvars, jobopt_T *opt_arg)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004920{
4921 job_T *job;
4922 char_u *cmd = NULL;
4923#if defined(UNIX)
4924# define USE_ARGV
4925 char **argv = NULL;
4926 int argc = 0;
4927#else
4928 garray_T ga;
4929#endif
4930 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004931 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004932
4933 job = job_alloc();
4934 if (job == NULL)
4935 return NULL;
4936
4937 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004938#ifndef USE_ARGV
4939 ga_init2(&ga, (int)sizeof(char*), 20);
4940#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004941
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004942 if (opt_arg != NULL)
4943 opt = *opt_arg;
4944 else
4945 {
4946 /* Default mode is NL. */
4947 clear_job_options(&opt);
4948 opt.jo_mode = MODE_NL;
4949 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004950 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4951 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004952 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004953 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004954
4955 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004956 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004957 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4958 && opt.jo_io[part] == JIO_FILE
4959 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4960 || *opt.jo_io_name[part] == NUL))
4961 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004962 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004963 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004964 }
4965
4966 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4967 {
4968 buf_T *buf = NULL;
4969
4970 /* check that we can find the buffer before starting the job */
4971 if (opt.jo_set & JO_IN_BUF)
4972 {
4973 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4974 if (buf == NULL)
4975 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4976 }
4977 else if (!(opt.jo_set & JO_IN_NAME))
4978 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004979 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004980 }
4981 else
4982 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4983 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004984 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004985 if (buf->b_ml.ml_mfp == NULL)
4986 {
4987 char_u numbuf[NUMBUFLEN];
4988 char_u *s;
4989
4990 if (opt.jo_set & JO_IN_BUF)
4991 {
4992 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4993 s = numbuf;
4994 }
4995 else
4996 s = opt.jo_io_name[PART_IN];
4997 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004998 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004999 }
5000 job->jv_in_buf = buf;
5001 }
5002
5003 job_set_options(job, &opt);
5004
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005005 if (argvars[0].v_type == VAR_STRING)
5006 {
5007 /* Command is a string. */
5008 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005009 if (cmd == NULL || *cmd == NUL)
5010 {
5011 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005012 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005013 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005014#ifdef USE_ARGV
5015 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005016 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005017 argv[argc] = NULL;
5018#endif
5019 }
5020 else if (argvars[0].v_type != VAR_LIST
5021 || argvars[0].vval.v_list == NULL
5022 || argvars[0].vval.v_list->lv_len < 1)
5023 {
5024 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005025 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005026 }
5027 else
5028 {
5029 list_T *l = argvars[0].vval.v_list;
5030 listitem_T *li;
5031 char_u *s;
5032
5033#ifdef USE_ARGV
5034 /* Pass argv[] to mch_call_shell(). */
5035 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
5036 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005037 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005038#endif
5039 for (li = l->lv_first; li != NULL; li = li->li_next)
5040 {
5041 s = get_tv_string_chk(&li->li_tv);
5042 if (s == NULL)
5043 goto theend;
5044#ifdef USE_ARGV
5045 argv[argc++] = (char *)s;
5046#else
5047 /* Only escape when needed, double quotes are not always allowed. */
5048 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
5049 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005050# ifdef WIN32
5051 int old_ssl = p_ssl;
5052
5053 /* This is using CreateProcess, not cmd.exe. Always use
5054 * double quote and backslashes. */
5055 p_ssl = 0;
5056# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005057 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005058# ifdef WIN32
5059 p_ssl = old_ssl;
5060# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005061 if (s == NULL)
5062 goto theend;
5063 ga_concat(&ga, s);
5064 vim_free(s);
5065 }
5066 else
5067 ga_concat(&ga, s);
5068 if (li->li_next != NULL)
5069 ga_append(&ga, ' ');
5070#endif
5071 }
5072#ifdef USE_ARGV
5073 argv[argc] = NULL;
5074#else
5075 cmd = ga.ga_data;
5076#endif
5077 }
5078
5079#ifdef USE_ARGV
5080 if (ch_log_active())
5081 {
5082 garray_T ga;
5083 int i;
5084
5085 ga_init2(&ga, (int)sizeof(char), 200);
5086 for (i = 0; i < argc; ++i)
5087 {
5088 if (i > 0)
5089 ga_concat(&ga, (char_u *)" ");
5090 ga_concat(&ga, (char_u *)argv[i]);
5091 }
5092 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
5093 ga_clear(&ga);
5094 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005095 mch_job_start(argv, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005096#else
5097 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005098 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005099#endif
5100
5101 /* If the channel is reading from a buffer, write lines now. */
5102 if (job->jv_channel != NULL)
5103 channel_write_in(job->jv_channel);
5104
5105theend:
5106#ifdef USE_ARGV
5107 vim_free(argv);
5108#else
5109 vim_free(ga.ga_data);
5110#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005111 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005112 return job;
5113}
5114
5115/*
5116 * Get the status of "job" and invoke the exit callback when needed.
5117 * The returned string is not allocated.
5118 */
5119 char *
5120job_status(job_T *job)
5121{
5122 char *result;
5123
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005124 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005125 /* No need to check, dead is dead. */
5126 result = "dead";
5127 else if (job->jv_status == JOB_FAILED)
5128 result = "fail";
5129 else
5130 {
5131 result = mch_job_status(job);
5132 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005133 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005134 }
5135 return result;
5136}
5137
Bram Moolenaar8950a562016-03-12 15:22:55 +01005138/*
5139 * Implementation of job_info().
5140 */
5141 void
5142job_info(job_T *job, dict_T *dict)
5143{
5144 dictitem_T *item;
5145 varnumber_T nr;
5146
5147 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5148
5149 item = dictitem_alloc((char_u *)"channel");
5150 if (item == NULL)
5151 return;
5152 item->di_tv.v_lock = 0;
5153 item->di_tv.v_type = VAR_CHANNEL;
5154 item->di_tv.vval.v_channel = job->jv_channel;
5155 if (job->jv_channel != NULL)
5156 ++job->jv_channel->ch_refcount;
5157 if (dict_add(dict, item) == FAIL)
5158 dictitem_free(item);
5159
5160#ifdef UNIX
5161 nr = job->jv_pid;
5162#else
5163 nr = job->jv_proc_info.dwProcessId;
5164#endif
5165 dict_add_nr_str(dict, "process", nr, NULL);
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005166 dict_add_nr_str(dict, "tty", 0L,
5167 job->jv_tty_name != NULL ? job->jv_tty_name : (char_u *)"");
Bram Moolenaar8950a562016-03-12 15:22:55 +01005168
5169 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005170 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005171 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5172}
5173
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005174/*
5175 * Send a signal to "job". Implements job_stop().
5176 * When "type" is not NULL use this for the type.
5177 * Otherwise use argvars[1] for the type.
5178 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005179 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005180job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005181{
5182 char_u *arg;
5183
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005184 if (type != NULL)
5185 arg = (char_u *)type;
5186 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005187 arg = (char_u *)"";
5188 else
5189 {
5190 arg = get_tv_string_chk(&argvars[1]);
5191 if (arg == NULL)
5192 {
5193 EMSG(_(e_invarg));
5194 return 0;
5195 }
5196 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005197 if (job->jv_status == JOB_FAILED)
5198 {
5199 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5200 return 0;
5201 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005202 if (job->jv_status == JOB_ENDED)
5203 {
5204 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5205 return 0;
5206 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005207 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
5208 if (mch_stop_job(job, arg) == FAIL)
5209 return 0;
5210
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005211 /* Assume that only "kill" will kill the job. */
5212 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005213 job->jv_channel->ch_job_killed = TRUE;
5214
5215 /* We don't try freeing the job, obviously the caller still has a
5216 * reference to it. */
5217 return 1;
5218}
5219
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005220#endif /* FEAT_JOB_CHANNEL */